1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 
37 #define DEBUG_SUBSYSTEM S_MDC
38 
39 # include <linux/module.h>
40 # include <linux/pagemap.h>
41 # include <linux/miscdevice.h>
42 # include <linux/init.h>
43 # include <linux/utsname.h>
44 
45 #include "../include/lustre_acl.h"
46 #include "../include/obd_class.h"
47 #include "../include/lustre_fid.h"
48 #include "../include/lprocfs_status.h"
49 #include "../include/lustre_param.h"
50 #include "../include/lustre_log.h"
51 
52 #include "mdc_internal.h"
53 
54 #define REQUEST_MINOR 244
55 
56 static int mdc_cleanup(struct obd_device *obd);
57 
mdc_queue_wait(struct ptlrpc_request * req)58 static inline int mdc_queue_wait(struct ptlrpc_request *req)
59 {
60 	struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
61 	int rc;
62 
63 	/* mdc_enter_request() ensures that this client has no more
64 	 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
65 	 * against an MDT. */
66 	rc = mdc_enter_request(cli);
67 	if (rc != 0)
68 		return rc;
69 
70 	rc = ptlrpc_queue_wait(req);
71 	mdc_exit_request(cli);
72 
73 	return rc;
74 }
75 
mdc_getstatus(struct obd_export * exp,struct lu_fid * rootfid)76 static int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid)
77 {
78 	struct ptlrpc_request *req;
79 	struct mdt_body       *body;
80 	int		    rc;
81 
82 	req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
83 					&RQF_MDS_GETSTATUS,
84 					LUSTRE_MDS_VERSION, MDS_GETSTATUS);
85 	if (req == NULL)
86 		return -ENOMEM;
87 
88 	mdc_pack_body(req, NULL, 0, 0, -1, 0);
89 	req->rq_send_state = LUSTRE_IMP_FULL;
90 
91 	ptlrpc_request_set_replen(req);
92 
93 	rc = ptlrpc_queue_wait(req);
94 	if (rc)
95 		goto out;
96 
97 	body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
98 	if (body == NULL) {
99 		rc = -EPROTO;
100 		goto out;
101 	}
102 
103 	*rootfid = body->fid1;
104 	CDEBUG(D_NET,
105 	       "root fid="DFID", last_committed=%llu\n",
106 	       PFID(rootfid),
107 	       lustre_msg_get_last_committed(req->rq_repmsg));
108 out:
109 	ptlrpc_req_finished(req);
110 	return rc;
111 }
112 
113 /*
114  * This function now is known to always saying that it will receive 4 buffers
115  * from server. Even for cases when acl_size and md_size is zero, RPC header
116  * will contain 4 fields and RPC itself will contain zero size fields. This is
117  * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
118  * and thus zero, it shrinks it, making zero size. The same story about
119  * md_size. And this is course of problem when client waits for smaller number
120  * of fields. This issue will be fixed later when client gets aware of RPC
121  * layouts.  --umka
122  */
mdc_getattr_common(struct obd_export * exp,struct ptlrpc_request * req)123 static int mdc_getattr_common(struct obd_export *exp,
124 			      struct ptlrpc_request *req)
125 {
126 	struct req_capsule *pill = &req->rq_pill;
127 	struct mdt_body    *body;
128 	void	       *eadata;
129 	int		 rc;
130 
131 	/* Request message already built. */
132 	rc = ptlrpc_queue_wait(req);
133 	if (rc != 0)
134 		return rc;
135 
136 	/* sanity check for the reply */
137 	body = req_capsule_server_get(pill, &RMF_MDT_BODY);
138 	if (body == NULL)
139 		return -EPROTO;
140 
141 	CDEBUG(D_NET, "mode: %o\n", body->mode);
142 
143 	if (body->eadatasize != 0) {
144 		mdc_update_max_ea_from_body(exp, body);
145 
146 		eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
147 						      body->eadatasize);
148 		if (eadata == NULL)
149 			return -EPROTO;
150 	}
151 
152 	if (body->valid & OBD_MD_FLRMTPERM) {
153 		struct mdt_remote_perm *perm;
154 
155 		LASSERT(client_is_remote(exp));
156 		perm = req_capsule_server_swab_get(pill, &RMF_ACL,
157 						lustre_swab_mdt_remote_perm);
158 		if (perm == NULL)
159 			return -EPROTO;
160 	}
161 
162 	return 0;
163 }
164 
mdc_getattr(struct obd_export * exp,struct md_op_data * op_data,struct ptlrpc_request ** request)165 static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
166 		struct ptlrpc_request **request)
167 {
168 	struct ptlrpc_request *req;
169 	int		    rc;
170 
171 	/* Single MDS without an LMV case */
172 	if (op_data->op_flags & MF_GET_MDT_IDX) {
173 		op_data->op_mds = 0;
174 		return 0;
175 	}
176 	*request = NULL;
177 	req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
178 	if (req == NULL)
179 		return -ENOMEM;
180 
181 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
182 	if (rc) {
183 		ptlrpc_request_free(req);
184 		return rc;
185 	}
186 
187 	mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
188 		      op_data->op_mode, -1, 0);
189 
190 	req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
191 			     op_data->op_mode);
192 	if (op_data->op_valid & OBD_MD_FLRMTPERM) {
193 		LASSERT(client_is_remote(exp));
194 		req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
195 				     sizeof(struct mdt_remote_perm));
196 	}
197 	ptlrpc_request_set_replen(req);
198 
199 	rc = mdc_getattr_common(exp, req);
200 	if (rc)
201 		ptlrpc_req_finished(req);
202 	else
203 		*request = req;
204 	return rc;
205 }
206 
mdc_getattr_name(struct obd_export * exp,struct md_op_data * op_data,struct ptlrpc_request ** request)207 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
208 		     struct ptlrpc_request **request)
209 {
210 	struct ptlrpc_request *req;
211 	int		    rc;
212 
213 	*request = NULL;
214 	req = ptlrpc_request_alloc(class_exp2cliimp(exp),
215 				   &RQF_MDS_GETATTR_NAME);
216 	if (req == NULL)
217 		return -ENOMEM;
218 
219 	req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
220 			     op_data->op_namelen + 1);
221 
222 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
223 	if (rc) {
224 		ptlrpc_request_free(req);
225 		return rc;
226 	}
227 
228 	mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
229 		      op_data->op_mode, op_data->op_suppgids[0], 0);
230 
231 	if (op_data->op_name) {
232 		char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
233 
234 		LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
235 				op_data->op_namelen);
236 		memcpy(name, op_data->op_name, op_data->op_namelen);
237 	}
238 
239 	req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
240 			     op_data->op_mode);
241 	ptlrpc_request_set_replen(req);
242 
243 	rc = mdc_getattr_common(exp, req);
244 	if (rc)
245 		ptlrpc_req_finished(req);
246 	else
247 		*request = req;
248 	return rc;
249 }
250 
mdc_is_subdir(struct obd_export * exp,const struct lu_fid * pfid,const struct lu_fid * cfid,struct ptlrpc_request ** request)251 static int mdc_is_subdir(struct obd_export *exp,
252 			 const struct lu_fid *pfid,
253 			 const struct lu_fid *cfid,
254 			 struct ptlrpc_request **request)
255 {
256 	struct ptlrpc_request  *req;
257 	int		     rc;
258 
259 	*request = NULL;
260 	req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
261 					&RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
262 					MDS_IS_SUBDIR);
263 	if (req == NULL)
264 		return -ENOMEM;
265 
266 	mdc_is_subdir_pack(req, pfid, cfid, 0);
267 	ptlrpc_request_set_replen(req);
268 
269 	rc = ptlrpc_queue_wait(req);
270 	if (rc && rc != -EREMOTE)
271 		ptlrpc_req_finished(req);
272 	else
273 		*request = req;
274 	return rc;
275 }
276 
mdc_xattr_common(struct obd_export * exp,const struct req_format * fmt,const struct lu_fid * fid,int opcode,u64 valid,const char * xattr_name,const char * input,int input_size,int output_size,int flags,__u32 suppgid,struct ptlrpc_request ** request)277 static int mdc_xattr_common(struct obd_export *exp,
278 			    const struct req_format *fmt,
279 			    const struct lu_fid *fid,
280 			    int opcode, u64 valid,
281 			    const char *xattr_name, const char *input,
282 			    int input_size, int output_size, int flags,
283 			    __u32 suppgid, struct ptlrpc_request **request)
284 {
285 	struct ptlrpc_request *req;
286 	int   xattr_namelen = 0;
287 	char *tmp;
288 	int   rc;
289 
290 	*request = NULL;
291 	req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
292 	if (req == NULL)
293 		return -ENOMEM;
294 
295 	if (xattr_name) {
296 		xattr_namelen = strlen(xattr_name) + 1;
297 		req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
298 				     xattr_namelen);
299 	}
300 	if (input_size) {
301 		LASSERT(input);
302 		req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
303 				     input_size);
304 	}
305 
306 	/* Flush local XATTR locks to get rid of a possible cancel RPC */
307 	if (opcode == MDS_REINT && fid_is_sane(fid) &&
308 	    exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
309 		LIST_HEAD(cancels);
310 		int count;
311 
312 		/* Without that packing would fail */
313 		if (input_size == 0)
314 			req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
315 					     RCL_CLIENT, 0);
316 
317 		count = mdc_resource_get_unused(exp, fid,
318 						&cancels, LCK_EX,
319 						MDS_INODELOCK_XATTR);
320 
321 		rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
322 		if (rc) {
323 			ptlrpc_request_free(req);
324 			return rc;
325 		}
326 	} else {
327 		rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
328 		if (rc) {
329 			ptlrpc_request_free(req);
330 			return rc;
331 		}
332 	}
333 
334 	if (opcode == MDS_REINT) {
335 		struct mdt_rec_setxattr *rec;
336 
337 		CLASSERT(sizeof(struct mdt_rec_setxattr) ==
338 			 sizeof(struct mdt_rec_reint));
339 		rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
340 		rec->sx_opcode = REINT_SETXATTR;
341 		rec->sx_fsuid  = from_kuid(&init_user_ns, current_fsuid());
342 		rec->sx_fsgid  = from_kgid(&init_user_ns, current_fsgid());
343 		rec->sx_cap    = cfs_curproc_cap_pack();
344 		rec->sx_suppgid1 = suppgid;
345 		rec->sx_suppgid2 = -1;
346 		rec->sx_fid    = *fid;
347 		rec->sx_valid  = valid | OBD_MD_FLCTIME;
348 		rec->sx_time   = ktime_get_real_seconds();
349 		rec->sx_size   = output_size;
350 		rec->sx_flags  = flags;
351 
352 	} else {
353 		mdc_pack_body(req, fid, valid, output_size, suppgid, flags);
354 	}
355 
356 	if (xattr_name) {
357 		tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
358 		memcpy(tmp, xattr_name, xattr_namelen);
359 	}
360 	if (input_size) {
361 		tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
362 		memcpy(tmp, input, input_size);
363 	}
364 
365 	if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
366 		req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
367 				     RCL_SERVER, output_size);
368 	ptlrpc_request_set_replen(req);
369 
370 	/* make rpc */
371 	if (opcode == MDS_REINT)
372 		mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
373 
374 	rc = ptlrpc_queue_wait(req);
375 
376 	if (opcode == MDS_REINT)
377 		mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
378 
379 	if (rc)
380 		ptlrpc_req_finished(req);
381 	else
382 		*request = req;
383 	return rc;
384 }
385 
mdc_setxattr(struct obd_export * exp,const struct lu_fid * fid,u64 valid,const char * xattr_name,const char * input,int input_size,int output_size,int flags,__u32 suppgid,struct ptlrpc_request ** request)386 static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
387 			u64 valid, const char *xattr_name,
388 			const char *input, int input_size, int output_size,
389 			int flags, __u32 suppgid,
390 			struct ptlrpc_request **request)
391 {
392 	return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
393 				fid, MDS_REINT, valid, xattr_name,
394 				input, input_size, output_size, flags,
395 				suppgid, request);
396 }
397 
mdc_getxattr(struct obd_export * exp,const struct lu_fid * fid,u64 valid,const char * xattr_name,const char * input,int input_size,int output_size,int flags,struct ptlrpc_request ** request)398 static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
399 			u64 valid, const char *xattr_name,
400 			const char *input, int input_size, int output_size,
401 			int flags, struct ptlrpc_request **request)
402 {
403 	return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
404 				fid, MDS_GETXATTR, valid, xattr_name,
405 				input, input_size, output_size, flags,
406 				-1, request);
407 }
408 
409 #ifdef CONFIG_FS_POSIX_ACL
mdc_unpack_acl(struct ptlrpc_request * req,struct lustre_md * md)410 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
411 {
412 	struct req_capsule     *pill = &req->rq_pill;
413 	struct mdt_body	*body = md->body;
414 	struct posix_acl       *acl;
415 	void		   *buf;
416 	int		     rc;
417 
418 	if (!body->aclsize)
419 		return 0;
420 
421 	buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
422 
423 	if (!buf)
424 		return -EPROTO;
425 
426 	acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
427 	if (acl == NULL)
428 		return 0;
429 
430 	if (IS_ERR(acl)) {
431 		rc = PTR_ERR(acl);
432 		CERROR("convert xattr to acl: %d\n", rc);
433 		return rc;
434 	}
435 
436 	rc = posix_acl_valid(acl);
437 	if (rc) {
438 		CERROR("validate acl: %d\n", rc);
439 		posix_acl_release(acl);
440 		return rc;
441 	}
442 
443 	md->posix_acl = acl;
444 	return 0;
445 }
446 #else
447 #define mdc_unpack_acl(req, md) 0
448 #endif
449 
mdc_get_lustre_md(struct obd_export * exp,struct ptlrpc_request * req,struct obd_export * dt_exp,struct obd_export * md_exp,struct lustre_md * md)450 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
451 		      struct obd_export *dt_exp, struct obd_export *md_exp,
452 		      struct lustre_md *md)
453 {
454 	struct req_capsule *pill = &req->rq_pill;
455 	int rc;
456 
457 	LASSERT(md);
458 	memset(md, 0, sizeof(*md));
459 
460 	md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
461 	LASSERT(md->body != NULL);
462 
463 	if (md->body->valid & OBD_MD_FLEASIZE) {
464 		int lmmsize;
465 		struct lov_mds_md *lmm;
466 
467 		if (!S_ISREG(md->body->mode)) {
468 			CDEBUG(D_INFO,
469 			       "OBD_MD_FLEASIZE set, should be a regular file, but is not\n");
470 			rc = -EPROTO;
471 			goto out;
472 		}
473 
474 		if (md->body->eadatasize == 0) {
475 			CDEBUG(D_INFO,
476 			       "OBD_MD_FLEASIZE set, but eadatasize 0\n");
477 			rc = -EPROTO;
478 			goto out;
479 		}
480 		lmmsize = md->body->eadatasize;
481 		lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
482 		if (!lmm) {
483 			rc = -EPROTO;
484 			goto out;
485 		}
486 
487 		rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
488 		if (rc < 0)
489 			goto out;
490 
491 		if (rc < sizeof(*md->lsm)) {
492 			CDEBUG(D_INFO,
493 			       "lsm size too small: rc < sizeof (*md->lsm) (%d < %d)\n",
494 			       rc, (int)sizeof(*md->lsm));
495 			rc = -EPROTO;
496 			goto out;
497 		}
498 
499 	} else if (md->body->valid & OBD_MD_FLDIREA) {
500 		int lmvsize;
501 		struct lov_mds_md *lmv;
502 
503 		if (!S_ISDIR(md->body->mode)) {
504 			CDEBUG(D_INFO,
505 			       "OBD_MD_FLDIREA set, should be a directory, but is not\n");
506 			rc = -EPROTO;
507 			goto out;
508 		}
509 
510 		if (md->body->eadatasize == 0) {
511 			CDEBUG(D_INFO,
512 			       "OBD_MD_FLDIREA is set, but eadatasize 0\n");
513 			return -EPROTO;
514 		}
515 		if (md->body->valid & OBD_MD_MEA) {
516 			lmvsize = md->body->eadatasize;
517 			lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
518 							   lmvsize);
519 			if (!lmv) {
520 				rc = -EPROTO;
521 				goto out;
522 			}
523 
524 			rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
525 					  lmvsize);
526 			if (rc < 0)
527 				goto out;
528 
529 			if (rc < sizeof(*md->mea)) {
530 				CDEBUG(D_INFO,
531 				       "size too small: rc < sizeof(*md->mea) (%d < %d)\n",
532 					rc, (int)sizeof(*md->mea));
533 				rc = -EPROTO;
534 				goto out;
535 			}
536 		}
537 	}
538 	rc = 0;
539 
540 	if (md->body->valid & OBD_MD_FLRMTPERM) {
541 		/* remote permission */
542 		LASSERT(client_is_remote(exp));
543 		md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
544 						lustre_swab_mdt_remote_perm);
545 		if (!md->remote_perm) {
546 			rc = -EPROTO;
547 			goto out;
548 		}
549 	} else if (md->body->valid & OBD_MD_FLACL) {
550 		/* for ACL, it's possible that FLACL is set but aclsize is zero.
551 		 * only when aclsize != 0 there's an actual segment for ACL
552 		 * in reply buffer.
553 		 */
554 		if (md->body->aclsize) {
555 			rc = mdc_unpack_acl(req, md);
556 			if (rc)
557 				goto out;
558 #ifdef CONFIG_FS_POSIX_ACL
559 		} else {
560 			md->posix_acl = NULL;
561 #endif
562 		}
563 	}
564 
565 out:
566 	if (rc) {
567 #ifdef CONFIG_FS_POSIX_ACL
568 		posix_acl_release(md->posix_acl);
569 #endif
570 		if (md->lsm)
571 			obd_free_memmd(dt_exp, &md->lsm);
572 	}
573 	return rc;
574 }
575 
mdc_free_lustre_md(struct obd_export * exp,struct lustre_md * md)576 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
577 {
578 	return 0;
579 }
580 
581 /**
582  * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
583  * RPC chains.
584  */
mdc_replay_open(struct ptlrpc_request * req)585 void mdc_replay_open(struct ptlrpc_request *req)
586 {
587 	struct md_open_data *mod = req->rq_cb_data;
588 	struct ptlrpc_request *close_req;
589 	struct obd_client_handle *och;
590 	struct lustre_handle old;
591 	struct mdt_body *body;
592 
593 	if (mod == NULL) {
594 		DEBUG_REQ(D_ERROR, req,
595 			  "Can't properly replay without open data.");
596 		return;
597 	}
598 
599 	body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
600 	LASSERT(body != NULL);
601 
602 	och = mod->mod_och;
603 	if (och != NULL) {
604 		struct lustre_handle *file_fh;
605 
606 		LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
607 
608 		file_fh = &och->och_fh;
609 		CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
610 		       file_fh->cookie, body->handle.cookie);
611 		old = *file_fh;
612 		*file_fh = body->handle;
613 	}
614 	close_req = mod->mod_close_req;
615 	if (close_req != NULL) {
616 		__u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
617 		struct mdt_ioepoch *epoch;
618 
619 		LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
620 		epoch = req_capsule_client_get(&close_req->rq_pill,
621 					       &RMF_MDT_EPOCH);
622 		LASSERT(epoch);
623 
624 		if (och != NULL)
625 			LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
626 		DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
627 		epoch->handle = body->handle;
628 	}
629 }
630 
mdc_commit_open(struct ptlrpc_request * req)631 void mdc_commit_open(struct ptlrpc_request *req)
632 {
633 	struct md_open_data *mod = req->rq_cb_data;
634 
635 	if (mod == NULL)
636 		return;
637 
638 	/**
639 	 * No need to touch md_open_data::mod_och, it holds a reference on
640 	 * \var mod and will zero references to each other, \var mod will be
641 	 * freed after that when md_open_data::mod_och will put the reference.
642 	 */
643 
644 	/**
645 	 * Do not let open request to disappear as it still may be needed
646 	 * for close rpc to happen (it may happen on evict only, otherwise
647 	 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
648 	 * called), just mark this rpc as committed to distinguish these 2
649 	 * cases, see mdc_close() for details. The open request reference will
650 	 * be put along with freeing \var mod.
651 	 */
652 	ptlrpc_request_addref(req);
653 	spin_lock(&req->rq_lock);
654 	req->rq_committed = 1;
655 	spin_unlock(&req->rq_lock);
656 	req->rq_cb_data = NULL;
657 	obd_mod_put(mod);
658 }
659 
mdc_set_open_replay_data(struct obd_export * exp,struct obd_client_handle * och,struct lookup_intent * it)660 int mdc_set_open_replay_data(struct obd_export *exp,
661 			     struct obd_client_handle *och,
662 			     struct lookup_intent *it)
663 {
664 	struct md_open_data   *mod;
665 	struct mdt_rec_create *rec;
666 	struct mdt_body       *body;
667 	struct ptlrpc_request *open_req = it->d.lustre.it_data;
668 	struct obd_import     *imp = open_req->rq_import;
669 
670 	if (!open_req->rq_replay)
671 		return 0;
672 
673 	rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
674 	body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
675 	LASSERT(rec != NULL);
676 	/* Incoming message in my byte order (it's been swabbed). */
677 	/* Outgoing messages always in my byte order. */
678 	LASSERT(body != NULL);
679 
680 	/* Only if the import is replayable, we set replay_open data */
681 	if (och && imp->imp_replayable) {
682 		mod = obd_mod_alloc();
683 		if (mod == NULL) {
684 			DEBUG_REQ(D_ERROR, open_req,
685 				  "Can't allocate md_open_data");
686 			return 0;
687 		}
688 
689 		/**
690 		 * Take a reference on \var mod, to be freed on mdc_close().
691 		 * It protects \var mod from being freed on eviction (commit
692 		 * callback is called despite rq_replay flag).
693 		 * Another reference for \var och.
694 		 */
695 		obd_mod_get(mod);
696 		obd_mod_get(mod);
697 
698 		spin_lock(&open_req->rq_lock);
699 		och->och_mod = mod;
700 		mod->mod_och = och;
701 		mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
702 				     it_disposition(it, DISP_OPEN_STRIPE);
703 		mod->mod_open_req = open_req;
704 		open_req->rq_cb_data = mod;
705 		open_req->rq_commit_cb = mdc_commit_open;
706 		spin_unlock(&open_req->rq_lock);
707 	}
708 
709 	rec->cr_fid2 = body->fid1;
710 	rec->cr_ioepoch = body->ioepoch;
711 	rec->cr_old_handle.cookie = body->handle.cookie;
712 	open_req->rq_replay_cb = mdc_replay_open;
713 	if (!fid_is_sane(&body->fid1)) {
714 		DEBUG_REQ(D_ERROR, open_req,
715 			  "Saving replay request with insane fid");
716 		LBUG();
717 	}
718 
719 	DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
720 	return 0;
721 }
722 
mdc_free_open(struct md_open_data * mod)723 static void mdc_free_open(struct md_open_data *mod)
724 {
725 	int committed = 0;
726 
727 	if (mod->mod_is_create == 0 &&
728 	    imp_connect_disp_stripe(mod->mod_open_req->rq_import))
729 		committed = 1;
730 
731 	LASSERT(mod->mod_open_req->rq_replay == 0);
732 
733 	DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
734 
735 	ptlrpc_request_committed(mod->mod_open_req, committed);
736 	if (mod->mod_close_req)
737 		ptlrpc_request_committed(mod->mod_close_req, committed);
738 }
739 
mdc_clear_open_replay_data(struct obd_export * exp,struct obd_client_handle * och)740 int mdc_clear_open_replay_data(struct obd_export *exp,
741 			       struct obd_client_handle *och)
742 {
743 	struct md_open_data *mod = och->och_mod;
744 
745 	/**
746 	 * It is possible to not have \var mod in a case of eviction between
747 	 * lookup and ll_file_open().
748 	 **/
749 	if (mod == NULL)
750 		return 0;
751 
752 	LASSERT(mod != LP_POISON);
753 	LASSERT(mod->mod_open_req != NULL);
754 	mdc_free_open(mod);
755 
756 	mod->mod_och = NULL;
757 	och->och_mod = NULL;
758 	obd_mod_put(mod);
759 
760 	return 0;
761 }
762 
763 /* Prepares the request for the replay by the given reply */
mdc_close_handle_reply(struct ptlrpc_request * req,struct md_op_data * op_data,int rc)764 static void mdc_close_handle_reply(struct ptlrpc_request *req,
765 				   struct md_op_data *op_data, int rc) {
766 	struct mdt_body  *repbody;
767 	struct mdt_ioepoch *epoch;
768 
769 	if (req && rc == -EAGAIN) {
770 		repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
771 		epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
772 
773 		epoch->flags |= MF_SOM_AU;
774 		if (repbody->valid & OBD_MD_FLGETATTRLOCK)
775 			op_data->op_flags |= MF_GETATTR_LOCK;
776 	}
777 }
778 
mdc_close(struct obd_export * exp,struct md_op_data * op_data,struct md_open_data * mod,struct ptlrpc_request ** request)779 static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
780 		     struct md_open_data *mod, struct ptlrpc_request **request)
781 {
782 	struct obd_device     *obd = class_exp2obd(exp);
783 	struct ptlrpc_request *req;
784 	struct req_format     *req_fmt;
785 	int                    rc;
786 	int		       saved_rc = 0;
787 
788 	req_fmt = &RQF_MDS_CLOSE;
789 	if (op_data->op_bias & MDS_HSM_RELEASE) {
790 		req_fmt = &RQF_MDS_RELEASE_CLOSE;
791 
792 		/* allocate a FID for volatile file */
793 		rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
794 		if (rc < 0) {
795 			CERROR("%s: "DFID" failed to allocate FID: %d\n",
796 			       obd->obd_name, PFID(&op_data->op_fid1), rc);
797 			/* save the errcode and proceed to close */
798 			saved_rc = rc;
799 		}
800 	}
801 
802 	*request = NULL;
803 	req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
804 	if (req == NULL)
805 		return -ENOMEM;
806 
807 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
808 	if (rc) {
809 		ptlrpc_request_free(req);
810 		return rc;
811 	}
812 
813 	/* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
814 	 * portal whose threads are not taking any DLM locks and are therefore
815 	 * always progressing */
816 	req->rq_request_portal = MDS_READPAGE_PORTAL;
817 	ptlrpc_at_set_req_timeout(req);
818 
819 	/* Ensure that this close's handle is fixed up during replay. */
820 	if (likely(mod != NULL)) {
821 		LASSERTF(mod->mod_open_req != NULL &&
822 			 mod->mod_open_req->rq_type != LI_POISON,
823 			 "POISONED open %p!\n", mod->mod_open_req);
824 
825 		mod->mod_close_req = req;
826 
827 		DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
828 		/* We no longer want to preserve this open for replay even
829 		 * though the open was committed. b=3632, b=3633 */
830 		spin_lock(&mod->mod_open_req->rq_lock);
831 		mod->mod_open_req->rq_replay = 0;
832 		spin_unlock(&mod->mod_open_req->rq_lock);
833 	} else {
834 		 CDEBUG(D_HA,
835 			"couldn't find open req; expecting close error\n");
836 	}
837 
838 	mdc_close_pack(req, op_data);
839 
840 	req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
841 			     obd->u.cli.cl_default_mds_easize);
842 	req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
843 			     obd->u.cli.cl_default_mds_cookiesize);
844 
845 	ptlrpc_request_set_replen(req);
846 
847 	mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
848 	rc = ptlrpc_queue_wait(req);
849 	mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
850 
851 	if (req->rq_repmsg == NULL) {
852 		CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
853 		       req->rq_status);
854 		if (rc == 0)
855 			rc = req->rq_status ?: -EIO;
856 	} else if (rc == 0 || rc == -EAGAIN) {
857 		struct mdt_body *body;
858 
859 		rc = lustre_msg_get_status(req->rq_repmsg);
860 		if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
861 			DEBUG_REQ(D_ERROR, req,
862 				  "type == PTL_RPC_MSG_ERR, err = %d", rc);
863 			if (rc > 0)
864 				rc = -rc;
865 		}
866 		body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
867 		if (body == NULL)
868 			rc = -EPROTO;
869 	} else if (rc == -ESTALE) {
870 		/**
871 		 * it can be allowed error after 3633 if open was committed and
872 		 * server failed before close was sent. Let's check if mod
873 		 * exists and return no error in that case
874 		 */
875 		if (mod) {
876 			DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
877 			LASSERT(mod->mod_open_req != NULL);
878 			if (mod->mod_open_req->rq_committed)
879 				rc = 0;
880 		}
881 	}
882 
883 	if (mod) {
884 		if (rc != 0)
885 			mod->mod_close_req = NULL;
886 		/* Since now, mod is accessed through open_req only,
887 		 * thus close req does not keep a reference on mod anymore. */
888 		obd_mod_put(mod);
889 	}
890 	*request = req;
891 	mdc_close_handle_reply(req, op_data, rc);
892 	return rc < 0 ? rc : saved_rc;
893 }
894 
mdc_done_writing(struct obd_export * exp,struct md_op_data * op_data,struct md_open_data * mod)895 static int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
896 			    struct md_open_data *mod)
897 {
898 	struct obd_device     *obd = class_exp2obd(exp);
899 	struct ptlrpc_request *req;
900 	int		    rc;
901 
902 	req = ptlrpc_request_alloc(class_exp2cliimp(exp),
903 				   &RQF_MDS_DONE_WRITING);
904 	if (req == NULL)
905 		return -ENOMEM;
906 
907 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
908 	if (rc) {
909 		ptlrpc_request_free(req);
910 		return rc;
911 	}
912 
913 	if (mod != NULL) {
914 		LASSERTF(mod->mod_open_req != NULL &&
915 			 mod->mod_open_req->rq_type != LI_POISON,
916 			 "POISONED setattr %p!\n", mod->mod_open_req);
917 
918 		mod->mod_close_req = req;
919 		DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
920 		/* We no longer want to preserve this setattr for replay even
921 		 * though the open was committed. b=3632, b=3633 */
922 		spin_lock(&mod->mod_open_req->rq_lock);
923 		mod->mod_open_req->rq_replay = 0;
924 		spin_unlock(&mod->mod_open_req->rq_lock);
925 	}
926 
927 	mdc_close_pack(req, op_data);
928 	ptlrpc_request_set_replen(req);
929 
930 	mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
931 	rc = ptlrpc_queue_wait(req);
932 	mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
933 
934 	if (rc == -ESTALE) {
935 		/**
936 		 * it can be allowed error after 3633 if open or setattr were
937 		 * committed and server failed before close was sent.
938 		 * Let's check if mod exists and return no error in that case
939 		 */
940 		if (mod) {
941 			LASSERT(mod->mod_open_req != NULL);
942 			if (mod->mod_open_req->rq_committed)
943 				rc = 0;
944 		}
945 	}
946 
947 	if (mod) {
948 		if (rc != 0)
949 			mod->mod_close_req = NULL;
950 		LASSERT(mod->mod_open_req != NULL);
951 		mdc_free_open(mod);
952 
953 		/* Since now, mod is accessed through setattr req only,
954 		 * thus DW req does not keep a reference on mod anymore. */
955 		obd_mod_put(mod);
956 	}
957 
958 	mdc_close_handle_reply(req, op_data, rc);
959 	ptlrpc_req_finished(req);
960 	return rc;
961 }
962 
mdc_readpage(struct obd_export * exp,struct md_op_data * op_data,struct page ** pages,struct ptlrpc_request ** request)963 static int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
964 			struct page **pages, struct ptlrpc_request **request)
965 {
966 	struct ptlrpc_request   *req;
967 	struct ptlrpc_bulk_desc *desc;
968 	int		      i;
969 	wait_queue_head_t	      waitq;
970 	int		      resends = 0;
971 	struct l_wait_info       lwi;
972 	int		      rc;
973 
974 	*request = NULL;
975 	init_waitqueue_head(&waitq);
976 
977 restart_bulk:
978 	req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
979 	if (req == NULL)
980 		return -ENOMEM;
981 
982 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
983 	if (rc) {
984 		ptlrpc_request_free(req);
985 		return rc;
986 	}
987 
988 	req->rq_request_portal = MDS_READPAGE_PORTAL;
989 	ptlrpc_at_set_req_timeout(req);
990 
991 	desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
992 				    MDS_BULK_PORTAL);
993 	if (desc == NULL) {
994 		ptlrpc_request_free(req);
995 		return -ENOMEM;
996 	}
997 
998 	/* NB req now owns desc and will free it when it gets freed */
999 	for (i = 0; i < op_data->op_npages; i++)
1000 		ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1001 
1002 	mdc_readdir_pack(req, op_data->op_offset,
1003 			 PAGE_CACHE_SIZE * op_data->op_npages,
1004 			 &op_data->op_fid1);
1005 
1006 	ptlrpc_request_set_replen(req);
1007 	rc = ptlrpc_queue_wait(req);
1008 	if (rc) {
1009 		ptlrpc_req_finished(req);
1010 		if (rc != -ETIMEDOUT)
1011 			return rc;
1012 
1013 		resends++;
1014 		if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1015 			CERROR("too many resend retries, returning error\n");
1016 			return -EIO;
1017 		}
1018 		lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends),
1019 				       NULL, NULL, NULL);
1020 		l_wait_event(waitq, 0, &lwi);
1021 
1022 		goto restart_bulk;
1023 	}
1024 
1025 	rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1026 					  req->rq_bulk->bd_nob_transferred);
1027 	if (rc < 0) {
1028 		ptlrpc_req_finished(req);
1029 		return rc;
1030 	}
1031 
1032 	if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1033 		CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1034 			req->rq_bulk->bd_nob_transferred,
1035 			PAGE_CACHE_SIZE * op_data->op_npages);
1036 		ptlrpc_req_finished(req);
1037 		return -EPROTO;
1038 	}
1039 
1040 	*request = req;
1041 	return 0;
1042 }
1043 
mdc_statfs(const struct lu_env * env,struct obd_export * exp,struct obd_statfs * osfs,__u64 max_age,__u32 flags)1044 static int mdc_statfs(const struct lu_env *env,
1045 		      struct obd_export *exp, struct obd_statfs *osfs,
1046 		      __u64 max_age, __u32 flags)
1047 {
1048 	struct obd_device     *obd = class_exp2obd(exp);
1049 	struct ptlrpc_request *req;
1050 	struct obd_statfs     *msfs;
1051 	struct obd_import     *imp = NULL;
1052 	int		    rc;
1053 
1054 	/*
1055 	 * Since the request might also come from lprocfs, so we need
1056 	 * sync this with client_disconnect_export Bug15684
1057 	 */
1058 	down_read(&obd->u.cli.cl_sem);
1059 	if (obd->u.cli.cl_import)
1060 		imp = class_import_get(obd->u.cli.cl_import);
1061 	up_read(&obd->u.cli.cl_sem);
1062 	if (!imp)
1063 		return -ENODEV;
1064 
1065 	req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1066 					LUSTRE_MDS_VERSION, MDS_STATFS);
1067 	if (req == NULL) {
1068 		rc = -ENOMEM;
1069 		goto output;
1070 	}
1071 
1072 	ptlrpc_request_set_replen(req);
1073 
1074 	if (flags & OBD_STATFS_NODELAY) {
1075 		/* procfs requests not want stay in wait for avoid deadlock */
1076 		req->rq_no_resend = 1;
1077 		req->rq_no_delay = 1;
1078 	}
1079 
1080 	rc = ptlrpc_queue_wait(req);
1081 	if (rc) {
1082 		/* check connection error first */
1083 		if (imp->imp_connect_error)
1084 			rc = imp->imp_connect_error;
1085 		goto out;
1086 	}
1087 
1088 	msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1089 	if (msfs == NULL) {
1090 		rc = -EPROTO;
1091 		goto out;
1092 	}
1093 
1094 	*osfs = *msfs;
1095 out:
1096 	ptlrpc_req_finished(req);
1097 output:
1098 	class_import_put(imp);
1099 	return rc;
1100 }
1101 
mdc_ioc_fid2path(struct obd_export * exp,struct getinfo_fid2path * gf)1102 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1103 {
1104 	__u32 keylen, vallen;
1105 	void *key;
1106 	int rc;
1107 
1108 	if (gf->gf_pathlen > PATH_MAX)
1109 		return -ENAMETOOLONG;
1110 	if (gf->gf_pathlen < 2)
1111 		return -EOVERFLOW;
1112 
1113 	/* Key is KEY_FID2PATH + getinfo_fid2path description */
1114 	keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1115 	key = kzalloc(keylen, GFP_NOFS);
1116 	if (!key)
1117 		return -ENOMEM;
1118 	memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1119 	memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1120 
1121 	CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
1122 	       PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1123 
1124 	if (!fid_is_sane(&gf->gf_fid)) {
1125 		rc = -EINVAL;
1126 		goto out;
1127 	}
1128 
1129 	/* Val is struct getinfo_fid2path result plus path */
1130 	vallen = sizeof(*gf) + gf->gf_pathlen;
1131 
1132 	rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1133 	if (rc != 0 && rc != -EREMOTE)
1134 		goto out;
1135 
1136 	if (vallen <= sizeof(*gf)) {
1137 		rc = -EPROTO;
1138 		goto out;
1139 	} else if (vallen > sizeof(*gf) + gf->gf_pathlen) {
1140 		rc = -EOVERFLOW;
1141 		goto out;
1142 	}
1143 
1144 	CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n%s\n",
1145 	       PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1146 
1147 out:
1148 	kfree(key);
1149 	return rc;
1150 }
1151 
mdc_ioc_hsm_progress(struct obd_export * exp,struct hsm_progress_kernel * hpk)1152 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1153 				struct hsm_progress_kernel *hpk)
1154 {
1155 	struct obd_import		*imp = class_exp2cliimp(exp);
1156 	struct hsm_progress_kernel	*req_hpk;
1157 	struct ptlrpc_request		*req;
1158 	int				 rc;
1159 
1160 	req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1161 					LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1162 	if (req == NULL) {
1163 		rc = -ENOMEM;
1164 		goto out;
1165 	}
1166 
1167 	mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1168 
1169 	/* Copy hsm_progress struct */
1170 	req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1171 	if (req_hpk == NULL) {
1172 		rc = -EPROTO;
1173 		goto out;
1174 	}
1175 
1176 	*req_hpk = *hpk;
1177 	req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1178 
1179 	ptlrpc_request_set_replen(req);
1180 
1181 	rc = mdc_queue_wait(req);
1182 	goto out;
1183 out:
1184 	ptlrpc_req_finished(req);
1185 	return rc;
1186 }
1187 
mdc_ioc_hsm_ct_register(struct obd_import * imp,__u32 archives)1188 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1189 {
1190 	__u32			*archive_mask;
1191 	struct ptlrpc_request	*req;
1192 	int			 rc;
1193 
1194 	req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1195 					LUSTRE_MDS_VERSION,
1196 					MDS_HSM_CT_REGISTER);
1197 	if (req == NULL) {
1198 		rc = -ENOMEM;
1199 		goto out;
1200 	}
1201 
1202 	mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1203 
1204 	/* Copy hsm_progress struct */
1205 	archive_mask = req_capsule_client_get(&req->rq_pill,
1206 					      &RMF_MDS_HSM_ARCHIVE);
1207 	if (archive_mask == NULL) {
1208 		rc = -EPROTO;
1209 		goto out;
1210 	}
1211 
1212 	*archive_mask = archives;
1213 
1214 	ptlrpc_request_set_replen(req);
1215 
1216 	rc = mdc_queue_wait(req);
1217 	goto out;
1218 out:
1219 	ptlrpc_req_finished(req);
1220 	return rc;
1221 }
1222 
mdc_ioc_hsm_current_action(struct obd_export * exp,struct md_op_data * op_data)1223 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1224 				      struct md_op_data *op_data)
1225 {
1226 	struct hsm_current_action	*hca = op_data->op_data;
1227 	struct hsm_current_action	*req_hca;
1228 	struct ptlrpc_request		*req;
1229 	int				 rc;
1230 
1231 	req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1232 				   &RQF_MDS_HSM_ACTION);
1233 	if (req == NULL)
1234 		return -ENOMEM;
1235 
1236 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1237 	if (rc) {
1238 		ptlrpc_request_free(req);
1239 		return rc;
1240 	}
1241 
1242 	mdc_pack_body(req, &op_data->op_fid1, OBD_MD_FLRMTPERM, 0,
1243 		      op_data->op_suppgids[0], 0);
1244 
1245 	ptlrpc_request_set_replen(req);
1246 
1247 	rc = mdc_queue_wait(req);
1248 	if (rc)
1249 		goto out;
1250 
1251 	req_hca = req_capsule_server_get(&req->rq_pill,
1252 					 &RMF_MDS_HSM_CURRENT_ACTION);
1253 	if (req_hca == NULL) {
1254 		rc = -EPROTO;
1255 		goto out;
1256 	}
1257 
1258 	*hca = *req_hca;
1259 
1260 out:
1261 	ptlrpc_req_finished(req);
1262 	return rc;
1263 }
1264 
mdc_ioc_hsm_ct_unregister(struct obd_import * imp)1265 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1266 {
1267 	struct ptlrpc_request	*req;
1268 	int			 rc;
1269 
1270 	req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1271 					LUSTRE_MDS_VERSION,
1272 					MDS_HSM_CT_UNREGISTER);
1273 	if (req == NULL) {
1274 		rc = -ENOMEM;
1275 		goto out;
1276 	}
1277 
1278 	mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1279 
1280 	ptlrpc_request_set_replen(req);
1281 
1282 	rc = mdc_queue_wait(req);
1283 	goto out;
1284 out:
1285 	ptlrpc_req_finished(req);
1286 	return rc;
1287 }
1288 
mdc_ioc_hsm_state_get(struct obd_export * exp,struct md_op_data * op_data)1289 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1290 				 struct md_op_data *op_data)
1291 {
1292 	struct hsm_user_state	*hus = op_data->op_data;
1293 	struct hsm_user_state	*req_hus;
1294 	struct ptlrpc_request	*req;
1295 	int			 rc;
1296 
1297 	req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1298 				   &RQF_MDS_HSM_STATE_GET);
1299 	if (req == NULL)
1300 		return -ENOMEM;
1301 
1302 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1303 	if (rc != 0) {
1304 		ptlrpc_request_free(req);
1305 		return rc;
1306 	}
1307 
1308 	mdc_pack_body(req, &op_data->op_fid1, OBD_MD_FLRMTPERM, 0,
1309 		      op_data->op_suppgids[0], 0);
1310 
1311 	ptlrpc_request_set_replen(req);
1312 
1313 	rc = mdc_queue_wait(req);
1314 	if (rc)
1315 		goto out;
1316 
1317 	req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1318 	if (req_hus == NULL) {
1319 		rc = -EPROTO;
1320 		goto out;
1321 	}
1322 
1323 	*hus = *req_hus;
1324 
1325 out:
1326 	ptlrpc_req_finished(req);
1327 	return rc;
1328 }
1329 
mdc_ioc_hsm_state_set(struct obd_export * exp,struct md_op_data * op_data)1330 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1331 				 struct md_op_data *op_data)
1332 {
1333 	struct hsm_state_set	*hss = op_data->op_data;
1334 	struct hsm_state_set	*req_hss;
1335 	struct ptlrpc_request	*req;
1336 	int			 rc;
1337 
1338 	req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1339 				   &RQF_MDS_HSM_STATE_SET);
1340 	if (req == NULL)
1341 		return -ENOMEM;
1342 
1343 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1344 	if (rc) {
1345 		ptlrpc_request_free(req);
1346 		return rc;
1347 	}
1348 
1349 	mdc_pack_body(req, &op_data->op_fid1, OBD_MD_FLRMTPERM, 0,
1350 		      op_data->op_suppgids[0], 0);
1351 
1352 	/* Copy states */
1353 	req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1354 	if (req_hss == NULL) {
1355 		rc = -EPROTO;
1356 		goto out;
1357 	}
1358 	*req_hss = *hss;
1359 
1360 	ptlrpc_request_set_replen(req);
1361 
1362 	rc = mdc_queue_wait(req);
1363 	goto out;
1364 
1365 out:
1366 	ptlrpc_req_finished(req);
1367 	return rc;
1368 }
1369 
mdc_ioc_hsm_request(struct obd_export * exp,struct hsm_user_request * hur)1370 static int mdc_ioc_hsm_request(struct obd_export *exp,
1371 			       struct hsm_user_request *hur)
1372 {
1373 	struct obd_import	*imp = class_exp2cliimp(exp);
1374 	struct ptlrpc_request	*req;
1375 	struct hsm_request	*req_hr;
1376 	struct hsm_user_item	*req_hui;
1377 	char			*req_opaque;
1378 	int			 rc;
1379 
1380 	req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1381 	if (req == NULL) {
1382 		rc = -ENOMEM;
1383 		goto out;
1384 	}
1385 
1386 	req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1387 			     hur->hur_request.hr_itemcount
1388 			     * sizeof(struct hsm_user_item));
1389 	req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1390 			     hur->hur_request.hr_data_len);
1391 
1392 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1393 	if (rc) {
1394 		ptlrpc_request_free(req);
1395 		return rc;
1396 	}
1397 
1398 	mdc_pack_body(req, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1399 
1400 	/* Copy hsm_request struct */
1401 	req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1402 	if (req_hr == NULL) {
1403 		rc = -EPROTO;
1404 		goto out;
1405 	}
1406 	*req_hr = hur->hur_request;
1407 
1408 	/* Copy hsm_user_item structs */
1409 	req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1410 	if (req_hui == NULL) {
1411 		rc = -EPROTO;
1412 		goto out;
1413 	}
1414 	memcpy(req_hui, hur->hur_user_item,
1415 	       hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1416 
1417 	/* Copy opaque field */
1418 	req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1419 	if (req_opaque == NULL) {
1420 		rc = -EPROTO;
1421 		goto out;
1422 	}
1423 	memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1424 
1425 	ptlrpc_request_set_replen(req);
1426 
1427 	rc = mdc_queue_wait(req);
1428 	goto out;
1429 
1430 out:
1431 	ptlrpc_req_finished(req);
1432 	return rc;
1433 }
1434 
changelog_kuc_hdr(char * buf,int len,int flags)1435 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1436 {
1437 	struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1438 
1439 	LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1440 
1441 	lh->kuc_magic = KUC_MAGIC;
1442 	lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1443 	lh->kuc_flags = flags;
1444 	lh->kuc_msgtype = CL_RECORD;
1445 	lh->kuc_msglen = len;
1446 	return lh;
1447 }
1448 
1449 #define D_CHANGELOG 0
1450 
1451 struct changelog_show {
1452 	__u64		cs_startrec;
1453 	__u32		cs_flags;
1454 	struct file	*cs_fp;
1455 	char		*cs_buf;
1456 	struct obd_device *cs_obd;
1457 };
1458 
changelog_kkuc_cb(const struct lu_env * env,struct llog_handle * llh,struct llog_rec_hdr * hdr,void * data)1459 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1460 			     struct llog_rec_hdr *hdr, void *data)
1461 {
1462 	struct changelog_show *cs = data;
1463 	struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1464 	struct kuc_hdr *lh;
1465 	int len, rc;
1466 
1467 	if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1468 		rc = -EINVAL;
1469 		CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1470 		       cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1471 		       rec->cr.cr_type, rc);
1472 		return rc;
1473 	}
1474 
1475 	if (rec->cr.cr_index < cs->cs_startrec) {
1476 		/* Skip entries earlier than what we are interested in */
1477 		CDEBUG(D_CHANGELOG, "rec=%llu start=%llu\n",
1478 		       rec->cr.cr_index, cs->cs_startrec);
1479 		return 0;
1480 	}
1481 
1482 	CDEBUG(D_CHANGELOG, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID
1483 		" %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1484 		changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1485 		rec->cr.cr_flags & CLF_FLAGMASK,
1486 		PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1487 		rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1488 
1489 	len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1490 
1491 	/* Set up the message */
1492 	lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1493 	memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1494 
1495 	rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1496 	CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len, rc);
1497 
1498 	return rc;
1499 }
1500 
mdc_changelog_send_thread(void * csdata)1501 static int mdc_changelog_send_thread(void *csdata)
1502 {
1503 	struct changelog_show *cs = csdata;
1504 	struct llog_ctxt *ctxt = NULL;
1505 	struct llog_handle *llh = NULL;
1506 	struct kuc_hdr *kuch;
1507 	int rc;
1508 
1509 	CDEBUG(D_CHANGELOG, "changelog to fp=%p start %llu\n",
1510 	       cs->cs_fp, cs->cs_startrec);
1511 
1512 	cs->cs_buf = kzalloc(KUC_CHANGELOG_MSG_MAXSIZE, GFP_NOFS);
1513 	if (!cs->cs_buf) {
1514 		rc = -ENOMEM;
1515 		goto out;
1516 	}
1517 
1518 	/* Set up the remote catalog handle */
1519 	ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1520 	if (ctxt == NULL) {
1521 		rc = -ENOENT;
1522 		goto out;
1523 	}
1524 	rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1525 		       LLOG_OPEN_EXISTS);
1526 	if (rc) {
1527 		CERROR("%s: fail to open changelog catalog: rc = %d\n",
1528 		       cs->cs_obd->obd_name, rc);
1529 		goto out;
1530 	}
1531 	rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1532 	if (rc) {
1533 		CERROR("llog_init_handle failed %d\n", rc);
1534 		goto out;
1535 	}
1536 
1537 	rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1538 
1539 	/* Send EOF no matter what our result */
1540 	kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1541 	if (kuch) {
1542 		kuch->kuc_msgtype = CL_EOF;
1543 		libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1544 	}
1545 
1546 out:
1547 	fput(cs->cs_fp);
1548 	if (llh)
1549 		llog_cat_close(NULL, llh);
1550 	if (ctxt)
1551 		llog_ctxt_put(ctxt);
1552 	kfree(cs->cs_buf);
1553 	kfree(cs);
1554 	return rc;
1555 }
1556 
mdc_ioc_changelog_send(struct obd_device * obd,struct ioc_changelog * icc)1557 static int mdc_ioc_changelog_send(struct obd_device *obd,
1558 				  struct ioc_changelog *icc)
1559 {
1560 	struct changelog_show *cs;
1561 	int rc;
1562 
1563 	/* Freed in mdc_changelog_send_thread */
1564 	cs = kzalloc(sizeof(*cs), GFP_NOFS);
1565 	if (!cs)
1566 		return -ENOMEM;
1567 
1568 	cs->cs_obd = obd;
1569 	cs->cs_startrec = icc->icc_recno;
1570 	/* matching fput in mdc_changelog_send_thread */
1571 	cs->cs_fp = fget(icc->icc_id);
1572 	cs->cs_flags = icc->icc_flags;
1573 
1574 	/*
1575 	 * New thread because we should return to user app before
1576 	 * writing into our pipe
1577 	 */
1578 	rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1579 				 "mdc_clg_send_thread"));
1580 	if (!IS_ERR_VALUE(rc)) {
1581 		CDEBUG(D_CHANGELOG, "start changelog thread\n");
1582 		return 0;
1583 	}
1584 
1585 	CERROR("Failed to start changelog thread: %d\n", rc);
1586 	kfree(cs);
1587 	return rc;
1588 }
1589 
1590 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1591 				struct lustre_kernelcomm *lk);
1592 
mdc_quotacheck(struct obd_device * unused,struct obd_export * exp,struct obd_quotactl * oqctl)1593 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1594 			  struct obd_quotactl *oqctl)
1595 {
1596 	struct client_obd       *cli = &exp->exp_obd->u.cli;
1597 	struct ptlrpc_request   *req;
1598 	struct obd_quotactl     *body;
1599 	int		      rc;
1600 
1601 	req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1602 					&RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1603 					MDS_QUOTACHECK);
1604 	if (req == NULL)
1605 		return -ENOMEM;
1606 
1607 	body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1608 	*body = *oqctl;
1609 
1610 	ptlrpc_request_set_replen(req);
1611 
1612 	/* the next poll will find -ENODATA, that means quotacheck is
1613 	 * going on */
1614 	cli->cl_qchk_stat = -ENODATA;
1615 	rc = ptlrpc_queue_wait(req);
1616 	if (rc)
1617 		cli->cl_qchk_stat = rc;
1618 	ptlrpc_req_finished(req);
1619 	return rc;
1620 }
1621 
mdc_quota_poll_check(struct obd_export * exp,struct if_quotacheck * qchk)1622 static int mdc_quota_poll_check(struct obd_export *exp,
1623 				struct if_quotacheck *qchk)
1624 {
1625 	struct client_obd *cli = &exp->exp_obd->u.cli;
1626 	int rc;
1627 
1628 	qchk->obd_uuid = cli->cl_target_uuid;
1629 	memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1630 
1631 	rc = cli->cl_qchk_stat;
1632 	/* the client is not the previous one */
1633 	if (rc == CL_NOT_QUOTACHECKED)
1634 		rc = -EINTR;
1635 	return rc;
1636 }
1637 
mdc_quotactl(struct obd_device * unused,struct obd_export * exp,struct obd_quotactl * oqctl)1638 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1639 			struct obd_quotactl *oqctl)
1640 {
1641 	struct ptlrpc_request   *req;
1642 	struct obd_quotactl     *oqc;
1643 	int		      rc;
1644 
1645 	req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1646 					&RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1647 					MDS_QUOTACTL);
1648 	if (req == NULL)
1649 		return -ENOMEM;
1650 
1651 	oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1652 	*oqc = *oqctl;
1653 
1654 	ptlrpc_request_set_replen(req);
1655 	ptlrpc_at_set_req_timeout(req);
1656 	req->rq_no_resend = 1;
1657 
1658 	rc = ptlrpc_queue_wait(req);
1659 	if (rc)
1660 		CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1661 
1662 	if (req->rq_repmsg) {
1663 		oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1664 		if (oqc) {
1665 			*oqctl = *oqc;
1666 		} else if (!rc) {
1667 			CERROR("Can't unpack obd_quotactl\n");
1668 			rc = -EPROTO;
1669 		}
1670 	} else if (!rc) {
1671 		CERROR("Can't unpack obd_quotactl\n");
1672 		rc = -EPROTO;
1673 	}
1674 	ptlrpc_req_finished(req);
1675 
1676 	return rc;
1677 }
1678 
mdc_ioc_swap_layouts(struct obd_export * exp,struct md_op_data * op_data)1679 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1680 				struct md_op_data *op_data)
1681 {
1682 	LIST_HEAD(cancels);
1683 	struct ptlrpc_request	*req;
1684 	int			 rc, count;
1685 	struct mdc_swap_layouts *msl, *payload;
1686 
1687 	msl = op_data->op_data;
1688 
1689 	/* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1690 	 * first thing it will do is to cancel the 2 layout
1691 	 * locks hold by this client.
1692 	 * So the client must cancel its layout locks on the 2 fids
1693 	 * with the request RPC to avoid extra RPC round trips
1694 	 */
1695 	count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1696 					LCK_CR, MDS_INODELOCK_LAYOUT);
1697 	count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1698 					 LCK_CR, MDS_INODELOCK_LAYOUT);
1699 
1700 	req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1701 				   &RQF_MDS_SWAP_LAYOUTS);
1702 	if (req == NULL) {
1703 		ldlm_lock_list_put(&cancels, l_bl_ast, count);
1704 		return -ENOMEM;
1705 	}
1706 
1707 	rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1708 	if (rc) {
1709 		ptlrpc_request_free(req);
1710 		return rc;
1711 	}
1712 
1713 	mdc_swap_layouts_pack(req, op_data);
1714 
1715 	payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1716 	LASSERT(payload);
1717 
1718 	*payload = *msl;
1719 
1720 	ptlrpc_request_set_replen(req);
1721 
1722 	rc = ptlrpc_queue_wait(req);
1723 
1724 	ptlrpc_req_finished(req);
1725 	return rc;
1726 }
1727 
mdc_iocontrol(unsigned int cmd,struct obd_export * exp,int len,void * karg,void * uarg)1728 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1729 			 void *karg, void *uarg)
1730 {
1731 	struct obd_device *obd = exp->exp_obd;
1732 	struct obd_ioctl_data *data = karg;
1733 	struct obd_import *imp = obd->u.cli.cl_import;
1734 	int rc;
1735 
1736 	if (!try_module_get(THIS_MODULE)) {
1737 		CERROR("Can't get module. Is it alive?");
1738 		return -EINVAL;
1739 	}
1740 	switch (cmd) {
1741 	case OBD_IOC_CHANGELOG_SEND:
1742 		rc = mdc_ioc_changelog_send(obd, karg);
1743 		goto out;
1744 	case OBD_IOC_CHANGELOG_CLEAR: {
1745 		struct ioc_changelog *icc = karg;
1746 		struct changelog_setinfo cs = {
1747 			.cs_recno = icc->icc_recno,
1748 			.cs_id = icc->icc_id
1749 		};
1750 
1751 		rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1752 					KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1753 					NULL);
1754 		goto out;
1755 	}
1756 	case OBD_IOC_FID2PATH:
1757 		rc = mdc_ioc_fid2path(exp, karg);
1758 		goto out;
1759 	case LL_IOC_HSM_CT_START:
1760 		rc = mdc_ioc_hsm_ct_start(exp, karg);
1761 		/* ignore if it was already registered on this MDS. */
1762 		if (rc == -EEXIST)
1763 			rc = 0;
1764 		goto out;
1765 	case LL_IOC_HSM_PROGRESS:
1766 		rc = mdc_ioc_hsm_progress(exp, karg);
1767 		goto out;
1768 	case LL_IOC_HSM_STATE_GET:
1769 		rc = mdc_ioc_hsm_state_get(exp, karg);
1770 		goto out;
1771 	case LL_IOC_HSM_STATE_SET:
1772 		rc = mdc_ioc_hsm_state_set(exp, karg);
1773 		goto out;
1774 	case LL_IOC_HSM_ACTION:
1775 		rc = mdc_ioc_hsm_current_action(exp, karg);
1776 		goto out;
1777 	case LL_IOC_HSM_REQUEST:
1778 		rc = mdc_ioc_hsm_request(exp, karg);
1779 		goto out;
1780 	case OBD_IOC_CLIENT_RECOVER:
1781 		rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1782 		if (rc < 0)
1783 			goto out;
1784 		rc = 0;
1785 		goto out;
1786 	case IOC_OSC_SET_ACTIVE:
1787 		rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1788 		goto out;
1789 	case OBD_IOC_POLL_QUOTACHECK:
1790 		rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1791 		goto out;
1792 	case OBD_IOC_PING_TARGET:
1793 		rc = ptlrpc_obd_ping(obd);
1794 		goto out;
1795 	/*
1796 	 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1797 	 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1798 	 * there'd be no LMV layer thus we might be called here. Eventually
1799 	 * this code should be removed.
1800 	 * bz20731, LU-592.
1801 	 */
1802 	case IOC_OBD_STATFS: {
1803 		struct obd_statfs stat_buf = {0};
1804 
1805 		if (*((__u32 *) data->ioc_inlbuf2) != 0) {
1806 			rc = -ENODEV;
1807 			goto out;
1808 		}
1809 
1810 		/* copy UUID */
1811 		if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1812 				 min_t(size_t, data->ioc_plen2,
1813 					       sizeof(struct obd_uuid)))) {
1814 			rc = -EFAULT;
1815 			goto out;
1816 		}
1817 
1818 		rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1819 				cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1820 				0);
1821 		if (rc != 0)
1822 			goto out;
1823 
1824 		if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1825 				 min_t(size_t, data->ioc_plen1,
1826 					       sizeof(stat_buf)))) {
1827 			rc = -EFAULT;
1828 			goto out;
1829 		}
1830 
1831 		rc = 0;
1832 		goto out;
1833 	}
1834 	case OBD_IOC_QUOTACTL: {
1835 		struct if_quotactl *qctl = karg;
1836 		struct obd_quotactl *oqctl;
1837 
1838 		oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
1839 		if (!oqctl) {
1840 			rc = -ENOMEM;
1841 			goto out;
1842 		}
1843 
1844 		QCTL_COPY(oqctl, qctl);
1845 		rc = obd_quotactl(exp, oqctl);
1846 		if (rc == 0) {
1847 			QCTL_COPY(qctl, oqctl);
1848 			qctl->qc_valid = QC_MDTIDX;
1849 			qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1850 		}
1851 
1852 		kfree(oqctl);
1853 		goto out;
1854 	}
1855 	case LL_IOC_GET_CONNECT_FLAGS:
1856 		if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1857 				 sizeof(*exp_connect_flags_ptr(exp)))) {
1858 			rc = -EFAULT;
1859 			goto out;
1860 		}
1861 
1862 		rc = 0;
1863 		goto out;
1864 	case LL_IOC_LOV_SWAP_LAYOUTS:
1865 		rc = mdc_ioc_swap_layouts(exp, karg);
1866 		goto out;
1867 	default:
1868 		CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1869 		rc = -ENOTTY;
1870 		goto out;
1871 	}
1872 out:
1873 	module_put(THIS_MODULE);
1874 
1875 	return rc;
1876 }
1877 
mdc_get_info_rpc(struct obd_export * exp,u32 keylen,void * key,int vallen,void * val)1878 static int mdc_get_info_rpc(struct obd_export *exp,
1879 			    u32 keylen, void *key,
1880 			    int vallen, void *val)
1881 {
1882 	struct obd_import      *imp = class_exp2cliimp(exp);
1883 	struct ptlrpc_request  *req;
1884 	char		   *tmp;
1885 	int		     rc = -EINVAL;
1886 
1887 	req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1888 	if (req == NULL)
1889 		return -ENOMEM;
1890 
1891 	req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1892 			     RCL_CLIENT, keylen);
1893 	req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1894 			     RCL_CLIENT, sizeof(__u32));
1895 
1896 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1897 	if (rc) {
1898 		ptlrpc_request_free(req);
1899 		return rc;
1900 	}
1901 
1902 	tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1903 	memcpy(tmp, key, keylen);
1904 	tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1905 	memcpy(tmp, &vallen, sizeof(__u32));
1906 
1907 	req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1908 			     RCL_SERVER, vallen);
1909 	ptlrpc_request_set_replen(req);
1910 
1911 	rc = ptlrpc_queue_wait(req);
1912 	/* -EREMOTE means the get_info result is partial, and it needs to
1913 	 * continue on another MDT, see fid2path part in lmv_iocontrol */
1914 	if (rc == 0 || rc == -EREMOTE) {
1915 		tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1916 		memcpy(val, tmp, vallen);
1917 		if (ptlrpc_rep_need_swab(req)) {
1918 			if (KEY_IS(KEY_FID2PATH))
1919 				lustre_swab_fid2path(val);
1920 		}
1921 	}
1922 	ptlrpc_req_finished(req);
1923 
1924 	return rc;
1925 }
1926 
lustre_swab_hai(struct hsm_action_item * h)1927 static void lustre_swab_hai(struct hsm_action_item *h)
1928 {
1929 	__swab32s(&h->hai_len);
1930 	__swab32s(&h->hai_action);
1931 	lustre_swab_lu_fid(&h->hai_fid);
1932 	lustre_swab_lu_fid(&h->hai_dfid);
1933 	__swab64s(&h->hai_cookie);
1934 	__swab64s(&h->hai_extent.offset);
1935 	__swab64s(&h->hai_extent.length);
1936 	__swab64s(&h->hai_gid);
1937 }
1938 
lustre_swab_hal(struct hsm_action_list * h)1939 static void lustre_swab_hal(struct hsm_action_list *h)
1940 {
1941 	struct hsm_action_item	*hai;
1942 	int			 i;
1943 
1944 	__swab32s(&h->hal_version);
1945 	__swab32s(&h->hal_count);
1946 	__swab32s(&h->hal_archive_id);
1947 	__swab64s(&h->hal_flags);
1948 	hai = hai_zero(h);
1949 	for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
1950 		lustre_swab_hai(hai);
1951 }
1952 
lustre_swab_kuch(struct kuc_hdr * l)1953 static void lustre_swab_kuch(struct kuc_hdr *l)
1954 {
1955 	__swab16s(&l->kuc_magic);
1956 	/* __u8 l->kuc_transport */
1957 	__swab16s(&l->kuc_msgtype);
1958 	__swab16s(&l->kuc_msglen);
1959 }
1960 
mdc_ioc_hsm_ct_start(struct obd_export * exp,struct lustre_kernelcomm * lk)1961 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1962 				struct lustre_kernelcomm *lk)
1963 {
1964 	struct obd_import  *imp = class_exp2cliimp(exp);
1965 	__u32		    archive = lk->lk_data;
1966 	int		    rc = 0;
1967 
1968 	if (lk->lk_group != KUC_GRP_HSM) {
1969 		CERROR("Bad copytool group %d\n", lk->lk_group);
1970 		return -EINVAL;
1971 	}
1972 
1973 	CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1974 	       lk->lk_uid, lk->lk_group, lk->lk_flags);
1975 
1976 	if (lk->lk_flags & LK_FLG_STOP) {
1977 		/* Unregister with the coordinator */
1978 		rc = mdc_ioc_hsm_ct_unregister(imp);
1979 	} else {
1980 		rc = mdc_ioc_hsm_ct_register(imp, archive);
1981 	}
1982 
1983 	return rc;
1984 }
1985 
1986 /**
1987  * Send a message to any listening copytools
1988  * @param val KUC message (kuc_hdr + hsm_action_list)
1989  * @param len total length of message
1990  */
mdc_hsm_copytool_send(int len,void * val)1991 static int mdc_hsm_copytool_send(int len, void *val)
1992 {
1993 	struct kuc_hdr		*lh = (struct kuc_hdr *)val;
1994 	struct hsm_action_list	*hal = (struct hsm_action_list *)(lh + 1);
1995 
1996 	if (len < sizeof(*lh) + sizeof(*hal)) {
1997 		CERROR("Short HSM message %d < %d\n", len,
1998 		       (int) (sizeof(*lh) + sizeof(*hal)));
1999 		return -EPROTO;
2000 	}
2001 	if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2002 		lustre_swab_kuch(lh);
2003 		lustre_swab_hal(hal);
2004 	} else if (lh->kuc_magic != KUC_MAGIC) {
2005 		CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2006 		return -EPROTO;
2007 	}
2008 
2009 	CDEBUG(D_HSM,
2010 	       "Received message mg=%x t=%d m=%d l=%d actions=%d on %s\n",
2011 	       lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2012 	       lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2013 
2014 	/* Broadcast to HSM listeners */
2015 	return libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2016 }
2017 
2018 /**
2019  * callback function passed to kuc for re-registering each HSM copytool
2020  * running on MDC, after MDT shutdown/recovery.
2021  * @param data archive id served by the copytool
2022  * @param cb_arg callback argument (obd_import)
2023  */
mdc_hsm_ct_reregister(__u32 data,void * cb_arg)2024 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2025 {
2026 	struct obd_import	*imp = (struct obd_import *)cb_arg;
2027 	__u32			 archive = data;
2028 	int			 rc;
2029 
2030 	CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2031 	       archive);
2032 	rc = mdc_ioc_hsm_ct_register(imp, archive);
2033 
2034 	/* ignore error if the copytool is already registered */
2035 	return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2036 }
2037 
2038 /**
2039  * Re-establish all kuc contexts with MDT
2040  * after MDT shutdown/recovery.
2041  */
mdc_kuc_reregister(struct obd_import * imp)2042 static int mdc_kuc_reregister(struct obd_import *imp)
2043 {
2044 	/* re-register HSM agents */
2045 	return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2046 					 (void *)imp);
2047 }
2048 
mdc_set_info_async(const struct lu_env * env,struct obd_export * exp,u32 keylen,void * key,u32 vallen,void * val,struct ptlrpc_request_set * set)2049 static int mdc_set_info_async(const struct lu_env *env,
2050 			      struct obd_export *exp,
2051 			      u32 keylen, void *key,
2052 			      u32 vallen, void *val,
2053 			      struct ptlrpc_request_set *set)
2054 {
2055 	struct obd_import	*imp = class_exp2cliimp(exp);
2056 	int			 rc;
2057 
2058 	if (KEY_IS(KEY_READ_ONLY)) {
2059 		if (vallen != sizeof(int))
2060 			return -EINVAL;
2061 
2062 		spin_lock(&imp->imp_lock);
2063 		if (*((int *)val)) {
2064 			imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2065 			imp->imp_connect_data.ocd_connect_flags |=
2066 							OBD_CONNECT_RDONLY;
2067 		} else {
2068 			imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2069 			imp->imp_connect_data.ocd_connect_flags &=
2070 							~OBD_CONNECT_RDONLY;
2071 		}
2072 		spin_unlock(&imp->imp_lock);
2073 
2074 		rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2075 				       keylen, key, vallen, val, set);
2076 		return rc;
2077 	}
2078 	if (KEY_IS(KEY_SPTLRPC_CONF)) {
2079 		sptlrpc_conf_client_adapt(exp->exp_obd);
2080 		return 0;
2081 	}
2082 	if (KEY_IS(KEY_FLUSH_CTX)) {
2083 		sptlrpc_import_flush_my_ctx(imp);
2084 		return 0;
2085 	}
2086 	if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2087 		rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2088 				       keylen, key, vallen, val, set);
2089 		return rc;
2090 	}
2091 	if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2092 		rc = mdc_hsm_copytool_send(vallen, val);
2093 		return rc;
2094 	}
2095 
2096 	CERROR("Unknown key %s\n", (char *)key);
2097 	return -EINVAL;
2098 }
2099 
mdc_get_info(const struct lu_env * env,struct obd_export * exp,__u32 keylen,void * key,__u32 * vallen,void * val,struct lov_stripe_md * lsm)2100 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2101 			__u32 keylen, void *key, __u32 *vallen, void *val,
2102 			struct lov_stripe_md *lsm)
2103 {
2104 	int rc = -EINVAL;
2105 
2106 	if (KEY_IS(KEY_MAX_EASIZE)) {
2107 		int mdsize, *max_easize;
2108 
2109 		if (*vallen != sizeof(int))
2110 			return -EINVAL;
2111 		mdsize = *(int *)val;
2112 		if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2113 			exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2114 		max_easize = val;
2115 		*max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2116 		return 0;
2117 	} else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2118 		int *default_easize;
2119 
2120 		if (*vallen != sizeof(int))
2121 			return -EINVAL;
2122 		default_easize = val;
2123 		*default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2124 		return 0;
2125 	} else if (KEY_IS(KEY_CONN_DATA)) {
2126 		struct obd_import *imp = class_exp2cliimp(exp);
2127 		struct obd_connect_data *data = val;
2128 
2129 		if (*vallen != sizeof(*data))
2130 			return -EINVAL;
2131 
2132 		*data = imp->imp_connect_data;
2133 		return 0;
2134 	} else if (KEY_IS(KEY_TGT_COUNT)) {
2135 		*((int *)val) = 1;
2136 		return 0;
2137 	}
2138 
2139 	rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2140 
2141 	return rc;
2142 }
2143 
mdc_sync(struct obd_export * exp,const struct lu_fid * fid,struct ptlrpc_request ** request)2144 static int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2145 		    struct ptlrpc_request **request)
2146 {
2147 	struct ptlrpc_request *req;
2148 	int		    rc;
2149 
2150 	*request = NULL;
2151 	req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2152 	if (req == NULL)
2153 		return -ENOMEM;
2154 
2155 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2156 	if (rc) {
2157 		ptlrpc_request_free(req);
2158 		return rc;
2159 	}
2160 
2161 	mdc_pack_body(req, fid, 0, 0, -1, 0);
2162 
2163 	ptlrpc_request_set_replen(req);
2164 
2165 	rc = ptlrpc_queue_wait(req);
2166 	if (rc)
2167 		ptlrpc_req_finished(req);
2168 	else
2169 		*request = req;
2170 	return rc;
2171 }
2172 
mdc_import_event(struct obd_device * obd,struct obd_import * imp,enum obd_import_event event)2173 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2174 			    enum obd_import_event event)
2175 {
2176 	int rc = 0;
2177 
2178 	LASSERT(imp->imp_obd == obd);
2179 
2180 	switch (event) {
2181 	case IMP_EVENT_DISCON: {
2182 #if 0
2183 		/* XXX Pass event up to OBDs stack. used only for FLD now */
2184 		rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2185 #endif
2186 		break;
2187 	}
2188 	case IMP_EVENT_INACTIVE: {
2189 		struct client_obd *cli = &obd->u.cli;
2190 		/*
2191 		 * Flush current sequence to make client obtain new one
2192 		 * from server in case of disconnect/reconnect.
2193 		 */
2194 		if (cli->cl_seq != NULL)
2195 			seq_client_flush(cli->cl_seq);
2196 
2197 		rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2198 		break;
2199 	}
2200 	case IMP_EVENT_INVALIDATE: {
2201 		struct ldlm_namespace *ns = obd->obd_namespace;
2202 
2203 		ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2204 
2205 		break;
2206 	}
2207 	case IMP_EVENT_ACTIVE:
2208 		rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2209 		/* redo the kuc registration after reconnecting */
2210 		if (rc == 0)
2211 			rc = mdc_kuc_reregister(imp);
2212 		break;
2213 	case IMP_EVENT_OCD:
2214 		rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2215 		break;
2216 	case IMP_EVENT_DEACTIVATE:
2217 	case IMP_EVENT_ACTIVATE:
2218 		break;
2219 	default:
2220 		CERROR("Unknown import event %x\n", event);
2221 		LBUG();
2222 	}
2223 	return rc;
2224 }
2225 
mdc_fid_alloc(struct obd_export * exp,struct lu_fid * fid,struct md_op_data * op_data)2226 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2227 		  struct md_op_data *op_data)
2228 {
2229 	struct client_obd *cli = &exp->exp_obd->u.cli;
2230 	struct lu_client_seq *seq = cli->cl_seq;
2231 
2232 	return seq_client_alloc_fid(NULL, seq, fid);
2233 }
2234 
mdc_get_uuid(struct obd_export * exp)2235 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2236 {
2237 	struct client_obd *cli = &exp->exp_obd->u.cli;
2238 
2239 	return &cli->cl_target_uuid;
2240 }
2241 
2242 /**
2243  * Determine whether the lock can be canceled before replaying it during
2244  * recovery, non zero value will be return if the lock can be canceled,
2245  * or zero returned for not
2246  */
mdc_cancel_for_recovery(struct ldlm_lock * lock)2247 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2248 {
2249 	if (lock->l_resource->lr_type != LDLM_IBITS)
2250 		return 0;
2251 
2252 	/* FIXME: if we ever get into a situation where there are too many
2253 	 * opened files with open locks on a single node, then we really
2254 	 * should replay these open locks to reget it */
2255 	if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2256 		return 0;
2257 
2258 	return 1;
2259 }
2260 
mdc_resource_inode_free(struct ldlm_resource * res)2261 static int mdc_resource_inode_free(struct ldlm_resource *res)
2262 {
2263 	if (res->lr_lvb_inode)
2264 		res->lr_lvb_inode = NULL;
2265 
2266 	return 0;
2267 }
2268 
2269 static struct ldlm_valblock_ops inode_lvbo = {
2270 	.lvbo_free = mdc_resource_inode_free,
2271 };
2272 
mdc_llog_init(struct obd_device * obd)2273 static int mdc_llog_init(struct obd_device *obd)
2274 {
2275 	struct obd_llog_group	*olg = &obd->obd_olg;
2276 	struct llog_ctxt	*ctxt;
2277 	int			 rc;
2278 
2279 	rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2280 			&llog_client_ops);
2281 	if (rc)
2282 		return rc;
2283 
2284 	ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2285 	llog_initiator_connect(ctxt);
2286 	llog_ctxt_put(ctxt);
2287 
2288 	return 0;
2289 }
2290 
mdc_llog_finish(struct obd_device * obd)2291 static void mdc_llog_finish(struct obd_device *obd)
2292 {
2293 	struct llog_ctxt *ctxt;
2294 
2295 	ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2296 	if (ctxt)
2297 		llog_cleanup(NULL, ctxt);
2298 }
2299 
mdc_setup(struct obd_device * obd,struct lustre_cfg * cfg)2300 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2301 {
2302 	struct client_obd *cli = &obd->u.cli;
2303 	struct lprocfs_static_vars lvars = { NULL };
2304 	int rc;
2305 
2306 	cli->cl_rpc_lock = kzalloc(sizeof(*cli->cl_rpc_lock), GFP_NOFS);
2307 	if (!cli->cl_rpc_lock)
2308 		return -ENOMEM;
2309 	mdc_init_rpc_lock(cli->cl_rpc_lock);
2310 
2311 	ptlrpcd_addref();
2312 
2313 	cli->cl_close_lock = kzalloc(sizeof(*cli->cl_close_lock), GFP_NOFS);
2314 	if (!cli->cl_close_lock) {
2315 		rc = -ENOMEM;
2316 		goto err_rpc_lock;
2317 	}
2318 	mdc_init_rpc_lock(cli->cl_close_lock);
2319 
2320 	rc = client_obd_setup(obd, cfg);
2321 	if (rc)
2322 		goto err_close_lock;
2323 	lprocfs_mdc_init_vars(&lvars);
2324 	lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
2325 	sptlrpc_lprocfs_cliobd_attach(obd);
2326 	ptlrpc_lprocfs_register_obd(obd);
2327 
2328 	ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2329 
2330 	obd->obd_namespace->ns_lvbo = &inode_lvbo;
2331 
2332 	rc = mdc_llog_init(obd);
2333 	if (rc) {
2334 		mdc_cleanup(obd);
2335 		CERROR("failed to setup llogging subsystems\n");
2336 	}
2337 
2338 	return rc;
2339 
2340 err_close_lock:
2341 	kfree(cli->cl_close_lock);
2342 err_rpc_lock:
2343 	kfree(cli->cl_rpc_lock);
2344 	ptlrpcd_decref();
2345 	return rc;
2346 }
2347 
2348 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2349  * us to make MDS RPCs with large enough reply buffers to hold a default
2350  * sized EA and cookie without having to calculate this (via a call into the
2351  * LOV + OSCs) each time we make an RPC.  The maximum size is also tracked
2352  * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2353  * a large number of stripes is possible.  If a larger reply buffer is
2354  * required it will be reallocated in the ptlrpc layer due to overflow.
2355  */
mdc_init_ea_size(struct obd_export * exp,int easize,int def_easize,int cookiesize,int def_cookiesize)2356 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2357 			    int def_easize, int cookiesize, int def_cookiesize)
2358 {
2359 	struct obd_device *obd = exp->exp_obd;
2360 	struct client_obd *cli = &obd->u.cli;
2361 
2362 	if (cli->cl_max_mds_easize < easize)
2363 		cli->cl_max_mds_easize = easize;
2364 
2365 	if (cli->cl_default_mds_easize < def_easize)
2366 		cli->cl_default_mds_easize = def_easize;
2367 
2368 	if (cli->cl_max_mds_cookiesize < cookiesize)
2369 		cli->cl_max_mds_cookiesize = cookiesize;
2370 
2371 	if (cli->cl_default_mds_cookiesize < def_cookiesize)
2372 		cli->cl_default_mds_cookiesize = def_cookiesize;
2373 
2374 	return 0;
2375 }
2376 
mdc_precleanup(struct obd_device * obd,enum obd_cleanup_stage stage)2377 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2378 {
2379 	switch (stage) {
2380 	case OBD_CLEANUP_EARLY:
2381 		break;
2382 	case OBD_CLEANUP_EXPORTS:
2383 		/* Failsafe, ok if racy */
2384 		if (obd->obd_type->typ_refcnt <= 1)
2385 			libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2386 
2387 		obd_cleanup_client_import(obd);
2388 		ptlrpc_lprocfs_unregister_obd(obd);
2389 		lprocfs_obd_cleanup(obd);
2390 
2391 		mdc_llog_finish(obd);
2392 		break;
2393 	}
2394 	return 0;
2395 }
2396 
mdc_cleanup(struct obd_device * obd)2397 static int mdc_cleanup(struct obd_device *obd)
2398 {
2399 	struct client_obd *cli = &obd->u.cli;
2400 
2401 	kfree(cli->cl_rpc_lock);
2402 	kfree(cli->cl_close_lock);
2403 
2404 	ptlrpcd_decref();
2405 
2406 	return client_obd_cleanup(obd);
2407 }
2408 
mdc_process_config(struct obd_device * obd,u32 len,void * buf)2409 static int mdc_process_config(struct obd_device *obd, u32 len, void *buf)
2410 {
2411 	struct lustre_cfg *lcfg = buf;
2412 	struct lprocfs_static_vars lvars = { NULL };
2413 	int rc = 0;
2414 
2415 	lprocfs_mdc_init_vars(&lvars);
2416 	switch (lcfg->lcfg_command) {
2417 	default:
2418 		rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2419 					      lcfg, obd);
2420 		if (rc > 0)
2421 			rc = 0;
2422 		break;
2423 	}
2424 	return rc;
2425 }
2426 
2427 /* get remote permission for current user on fid */
mdc_get_remote_perm(struct obd_export * exp,const struct lu_fid * fid,__u32 suppgid,struct ptlrpc_request ** request)2428 static int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2429 			       __u32 suppgid, struct ptlrpc_request **request)
2430 {
2431 	struct ptlrpc_request  *req;
2432 	int		    rc;
2433 
2434 	LASSERT(client_is_remote(exp));
2435 
2436 	*request = NULL;
2437 	req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2438 	if (req == NULL)
2439 		return -ENOMEM;
2440 
2441 	rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2442 	if (rc) {
2443 		ptlrpc_request_free(req);
2444 		return rc;
2445 	}
2446 
2447 	mdc_pack_body(req, fid, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2448 
2449 	req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2450 			     sizeof(struct mdt_remote_perm));
2451 
2452 	ptlrpc_request_set_replen(req);
2453 
2454 	rc = ptlrpc_queue_wait(req);
2455 	if (rc)
2456 		ptlrpc_req_finished(req);
2457 	else
2458 		*request = req;
2459 	return rc;
2460 }
2461 
2462 static struct obd_ops mdc_obd_ops = {
2463 	.o_owner	    = THIS_MODULE,
2464 	.o_setup	    = mdc_setup,
2465 	.o_precleanup       = mdc_precleanup,
2466 	.o_cleanup	  = mdc_cleanup,
2467 	.o_add_conn	 = client_import_add_conn,
2468 	.o_del_conn	 = client_import_del_conn,
2469 	.o_connect          = client_connect_import,
2470 	.o_disconnect       = client_disconnect_export,
2471 	.o_iocontrol	= mdc_iocontrol,
2472 	.o_set_info_async   = mdc_set_info_async,
2473 	.o_statfs	   = mdc_statfs,
2474 	.o_fid_init	    = client_fid_init,
2475 	.o_fid_fini	    = client_fid_fini,
2476 	.o_fid_alloc	= mdc_fid_alloc,
2477 	.o_import_event     = mdc_import_event,
2478 	.o_get_info	 = mdc_get_info,
2479 	.o_process_config   = mdc_process_config,
2480 	.o_get_uuid	 = mdc_get_uuid,
2481 	.o_quotactl	 = mdc_quotactl,
2482 	.o_quotacheck       = mdc_quotacheck
2483 };
2484 
2485 static struct md_ops mdc_md_ops = {
2486 	.m_getstatus	= mdc_getstatus,
2487 	.m_null_inode	    = mdc_null_inode,
2488 	.m_find_cbdata      = mdc_find_cbdata,
2489 	.m_close	    = mdc_close,
2490 	.m_create	   = mdc_create,
2491 	.m_done_writing     = mdc_done_writing,
2492 	.m_enqueue	  = mdc_enqueue,
2493 	.m_getattr	  = mdc_getattr,
2494 	.m_getattr_name     = mdc_getattr_name,
2495 	.m_intent_lock      = mdc_intent_lock,
2496 	.m_link	     = mdc_link,
2497 	.m_is_subdir	= mdc_is_subdir,
2498 	.m_rename	   = mdc_rename,
2499 	.m_setattr	  = mdc_setattr,
2500 	.m_setxattr	 = mdc_setxattr,
2501 	.m_getxattr	 = mdc_getxattr,
2502 	.m_sync	     = mdc_sync,
2503 	.m_readpage	 = mdc_readpage,
2504 	.m_unlink	   = mdc_unlink,
2505 	.m_cancel_unused    = mdc_cancel_unused,
2506 	.m_init_ea_size     = mdc_init_ea_size,
2507 	.m_set_lock_data    = mdc_set_lock_data,
2508 	.m_lock_match       = mdc_lock_match,
2509 	.m_get_lustre_md    = mdc_get_lustre_md,
2510 	.m_free_lustre_md   = mdc_free_lustre_md,
2511 	.m_set_open_replay_data = mdc_set_open_replay_data,
2512 	.m_clear_open_replay_data = mdc_clear_open_replay_data,
2513 	.m_get_remote_perm  = mdc_get_remote_perm,
2514 	.m_intent_getattr_async = mdc_intent_getattr_async,
2515 	.m_revalidate_lock      = mdc_revalidate_lock
2516 };
2517 
mdc_init(void)2518 static int __init mdc_init(void)
2519 {
2520 	struct lprocfs_static_vars lvars = { NULL };
2521 
2522 	lprocfs_mdc_init_vars(&lvars);
2523 
2524 	return class_register_type(&mdc_obd_ops, &mdc_md_ops,
2525 				 LUSTRE_MDC_NAME, NULL);
2526 }
2527 
mdc_exit(void)2528 static void /*__exit*/ mdc_exit(void)
2529 {
2530 	class_unregister_type(LUSTRE_MDC_NAME);
2531 }
2532 
2533 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2534 MODULE_DESCRIPTION("Lustre Metadata Client");
2535 MODULE_LICENSE("GPL");
2536 
2537 module_init(mdc_init);
2538 module_exit(mdc_exit);
2539