4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_MDC
34 #include <linux/module.h>
35 #include <linux/kernel.h>
37 #include <obd_class.h>
38 #include "mdc_internal.h"
39 #include <lustre_fid.h>
41 /* mdc_setattr does its own semaphore handling */
42 static int mdc_reint(struct ptlrpc_request *request, int level)
46 request->rq_send_state = level;
48 ptlrpc_get_mod_rpc_slot(request);
49 rc = ptlrpc_queue_wait(request);
50 ptlrpc_put_mod_rpc_slot(request);
52 CDEBUG(D_INFO, "error in handling %d\n", rc);
53 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
59 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
60 * found by @fid. Found locks are added into @cancel list. Returns the amount of
61 * locks added to @cancels list. */
62 int mdc_resource_get_unused_res(struct obd_export *exp,
63 struct ldlm_res_id *res_id,
64 struct list_head *cancels,
65 enum ldlm_mode mode, __u64 bits)
67 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
68 union ldlm_policy_data policy = { { 0 } };
69 struct ldlm_resource *res;
74 /* Return, i.e. cancel nothing, only if ELC is supported (flag in
75 * export) but disabled through procfs (flag in NS).
77 * This distinguishes from a case when ELC is not supported originally,
78 * when we still want to cancel locks in advance and just cancel them
79 * locally, without sending any RPC. */
80 if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
83 res = ldlm_resource_get(ns, res_id, 0, 0);
86 LDLM_RESOURCE_ADDREF(res);
87 /* Initialize ibits lock policy. */
88 policy.l_inodebits.bits = bits;
89 count = ldlm_cancel_resource_local(res, cancels, &policy, mode, 0, 0,
91 LDLM_RESOURCE_DELREF(res);
92 ldlm_resource_putref(res);
96 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
97 struct list_head *cancels, enum ldlm_mode mode,
100 struct ldlm_res_id res_id;
102 fid_build_reg_res_name(fid, &res_id);
103 return mdc_resource_get_unused_res(exp, &res_id, cancels, mode, bits);
106 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
107 void *ea, size_t ealen, struct ptlrpc_request **request)
110 struct ptlrpc_request *req;
115 LASSERT(op_data != NULL);
117 bits = MDS_INODELOCK_UPDATE;
118 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
119 bits |= MDS_INODELOCK_LOOKUP;
120 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
121 (fid_is_sane(&op_data->op_fid1)))
122 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
123 &cancels, LCK_EX, bits);
124 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
125 &RQF_MDS_REINT_SETATTR);
127 ldlm_lock_list_put(&cancels, l_bl_ast, count);
131 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
132 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
133 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
135 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
137 ptlrpc_request_free(req);
141 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
142 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld\n",
143 (s64)op_data->op_attr.ia_mtime.tv_sec,
144 (s64)op_data->op_attr.ia_ctime.tv_sec);
145 mdc_setattr_pack(&req->rq_pill, op_data, ea, ealen);
147 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER, 0);
149 ptlrpc_request_set_replen(req);
151 rc = mdc_reint(req, LUSTRE_IMP_FULL);
152 if (rc == -ERESTARTSYS)
160 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
161 const void *data, size_t datalen,
162 umode_t mode, uid_t uid, gid_t gid,
163 kernel_cap_t cap_effective, __u64 rdev,
164 struct ptlrpc_request **request)
166 struct ptlrpc_request *req;
168 int count, resends = 0;
169 struct obd_import *import = exp->exp_obd->u.cli.cl_import;
170 int generation = import->imp_generation;
175 /* For case if upper layer did not alloc fid, do it now. */
176 if (!fid_is_sane(&op_data->op_fid2)) {
178 * mdc_fid_alloc() may return errno 1 in case of switch to new
179 * sequence, handle this.
181 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
188 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
189 (fid_is_sane(&op_data->op_fid1)))
190 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
192 MDS_INODELOCK_UPDATE);
194 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
195 &RQF_MDS_REINT_CREATE_ACL);
197 ldlm_lock_list_put(&cancels, l_bl_ast, count);
201 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
202 op_data->op_namelen + 1);
203 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
204 data && datalen ? datalen : 0);
206 req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
207 RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
208 strlen(op_data->op_file_secctx_name) + 1 : 0);
210 req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
211 op_data->op_file_secctx_size);
213 req_capsule_set_size(&req->rq_pill, &RMF_FILE_ENCCTX, RCL_CLIENT,
214 op_data->op_file_encctx_size);
216 /* get SELinux policy info if any */
217 rc = sptlrpc_get_sepol(req);
219 ptlrpc_request_free(req);
222 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
223 strlen(req->rq_sepol) ?
224 strlen(req->rq_sepol) + 1 : 0);
226 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
228 ptlrpc_request_free(req);
233 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
234 * tgt, for symlinks or lov MD data.
236 mdc_create_pack(&req->rq_pill, op_data, data, datalen, mode, uid,
237 gid, cap_effective, rdev);
239 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
240 exp->exp_obd->u.cli.cl_default_mds_easize);
241 ptlrpc_request_set_replen(req);
243 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
245 req->rq_no_retry_einprogress = 1;
248 req->rq_generation_set = 1;
249 req->rq_import_generation = generation;
250 req->rq_sent = ktime_get_real_seconds() + resends;
252 level = LUSTRE_IMP_FULL;
254 rc = mdc_reint(req, level);
256 /* Resend if we were told to. */
257 if (rc == -ERESTARTSYS) {
258 level = LUSTRE_IMP_RECOVER;
260 } else if (rc == -EINPROGRESS) {
261 /* Retry create infinitely until succeed or get other
262 * error code or interrupted. */
263 ptlrpc_req_finished(req);
264 if (generation == import->imp_generation) {
265 if (signal_pending(current))
269 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
270 exp->exp_obd->obd_name, resends,
271 PFID(&op_data->op_fid1),
272 PFID(&op_data->op_fid2));
275 CDEBUG(D_HA, "resend cross eviction\n");
278 } else if (rc == 0 && S_ISDIR(mode)) {
279 struct mdt_body *body;
281 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
284 CERROR("%s: cannot swab mdt_body: rc = %d\n",
285 exp->exp_obd->obd_name, rc);
289 if ((body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_MEA)) ==
290 (OBD_MD_FLDIREA | OBD_MD_MEA)) {
293 /* clear valid, because mkdir doesn't need to initialize
294 * LMV, which will be delayed to lookup.
296 body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_MEA);
297 mdc_update_max_ea_from_body(exp, body);
298 /* The eadata is opaque; just check that it is there.
299 * Eventually, obd_unpackmd() will check the contents.
301 eadata = req_capsule_server_sized_get(&req->rq_pill,
303 body->mbo_eadatasize);
307 /* save the reply LMV EA in case we have to replay a
308 * create for recovery.
310 rc = mdc_save_lmm(req, eadata, body->mbo_eadatasize);
318 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
319 struct ptlrpc_request **request)
322 struct obd_device *obd = class_exp2obd(exp);
323 struct ptlrpc_request *req = *request;
327 LASSERT(req == NULL);
329 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
330 (fid_is_sane(&op_data->op_fid1)))
331 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
333 MDS_INODELOCK_UPDATE);
334 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
335 (fid_is_sane(&op_data->op_fid3)))
336 /* cancel DOM lock only if it has no data to flush */
337 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
339 op_data->op_cli_flags &
343 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
344 &RQF_MDS_REINT_UNLINK);
346 ldlm_lock_list_put(&cancels, l_bl_ast, count);
350 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
351 op_data->op_namelen + 1);
353 /* get SELinux policy info if any */
354 rc = sptlrpc_get_sepol(req);
356 ptlrpc_request_free(req);
359 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
360 strlen(req->rq_sepol) ?
361 strlen(req->rq_sepol) + 1 : 0);
363 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
365 ptlrpc_request_free(req);
369 mdc_unlink_pack(&req->rq_pill, op_data);
371 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
372 obd->u.cli.cl_default_mds_easize);
373 ptlrpc_request_set_replen(req);
377 rc = mdc_reint(req, LUSTRE_IMP_FULL);
378 if (rc == -ERESTARTSYS)
383 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
384 struct ptlrpc_request **request)
387 struct ptlrpc_request *req;
391 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
392 (fid_is_sane(&op_data->op_fid2)))
393 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
395 MDS_INODELOCK_UPDATE);
396 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
397 (fid_is_sane(&op_data->op_fid1)))
398 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
400 MDS_INODELOCK_UPDATE);
402 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
404 ldlm_lock_list_put(&cancels, l_bl_ast, count);
408 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
409 op_data->op_namelen + 1);
411 /* get SELinux policy info if any */
412 rc = sptlrpc_get_sepol(req);
414 ptlrpc_request_free(req);
417 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
418 strlen(req->rq_sepol) ?
419 strlen(req->rq_sepol) + 1 : 0);
421 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
423 ptlrpc_request_free(req);
427 mdc_link_pack(&req->rq_pill, op_data);
428 ptlrpc_request_set_replen(req);
430 rc = mdc_reint(req, LUSTRE_IMP_FULL);
432 if (rc == -ERESTARTSYS)
438 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
439 const char *old, size_t oldlen, const char *new, size_t newlen,
440 struct ptlrpc_request **request)
443 struct obd_device *obd = exp->exp_obd;
444 struct ptlrpc_request *req;
449 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
450 (fid_is_sane(&op_data->op_fid1)))
451 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
453 MDS_INODELOCK_UPDATE);
454 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
455 (fid_is_sane(&op_data->op_fid2)))
456 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
458 MDS_INODELOCK_UPDATE);
459 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
460 (fid_is_sane(&op_data->op_fid3)))
461 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
463 MDS_INODELOCK_LOOKUP);
464 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
465 (fid_is_sane(&op_data->op_fid4)))
466 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
470 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
471 op_data->op_cli_flags & CLI_MIGRATE ?
472 &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
474 ldlm_lock_list_put(&cancels, l_bl_ast, count);
478 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
479 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
480 if (op_data->op_cli_flags & CLI_MIGRATE)
481 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
482 op_data->op_data_size);
484 /* get SELinux policy info if any */
485 rc = sptlrpc_get_sepol(req);
487 ptlrpc_request_free(req);
490 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
491 strlen(req->rq_sepol) ?
492 strlen(req->rq_sepol) + 1 : 0);
494 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
496 ptlrpc_request_free(req);
500 if (exp_connect_cancelset(exp) && req)
501 ldlm_cli_cancel_list(&cancels, count, req, 0);
503 if (op_data->op_cli_flags & CLI_MIGRATE)
504 mdc_migrate_pack(&req->rq_pill, op_data, old, oldlen);
506 mdc_rename_pack(&req->rq_pill, op_data, old, oldlen,
509 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
510 obd->u.cli.cl_default_mds_easize);
511 ptlrpc_request_set_replen(req);
513 rc = mdc_reint(req, LUSTRE_IMP_FULL);
515 if (rc == -ERESTARTSYS)
521 int mdc_file_resync(struct obd_export *exp, struct md_op_data *op_data)
524 struct ptlrpc_request *req;
525 struct ldlm_lock *lock;
526 struct mdt_rec_resync *rec;
530 if (op_data->op_flags & MF_MDC_CANCEL_FID1 &&
531 fid_is_sane(&op_data->op_fid1))
532 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
534 MDS_INODELOCK_LAYOUT);
536 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
537 &RQF_MDS_REINT_RESYNC);
539 ldlm_lock_list_put(&cancels, l_bl_ast, count);
543 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
545 ptlrpc_request_free(req);
549 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
550 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
551 rec->rs_opcode = REINT_RESYNC;
552 rec->rs_fsuid = op_data->op_fsuid;
553 rec->rs_fsgid = op_data->op_fsgid;
554 rec->rs_cap = ll_capability_u32(op_data->op_cap);
555 rec->rs_fid = op_data->op_fid1;
556 rec->rs_bias = op_data->op_bias;
557 rec->rs_mirror_id = op_data->op_mirror_id;
559 lock = ldlm_handle2lock(&op_data->op_lease_handle);
561 rec->rs_lease_handle = lock->l_remote_handle;
565 ptlrpc_request_set_replen(req);
567 rc = mdc_reint(req, LUSTRE_IMP_FULL);
568 if (rc == -ERESTARTSYS)
571 ptlrpc_req_finished(req);