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, NULL, 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;
174 /* For case if upper layer did not alloc fid, do it now. */
175 if (!fid_is_sane(&op_data->op_fid2)) {
177 * mdc_fid_alloc() may return errno 1 in case of switch to new
178 * sequence, handle this.
180 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
187 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
188 (fid_is_sane(&op_data->op_fid1)))
189 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
191 MDS_INODELOCK_UPDATE);
193 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
194 &RQF_MDS_REINT_CREATE_ACL);
196 ldlm_lock_list_put(&cancels, l_bl_ast, count);
200 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
201 op_data->op_namelen + 1);
202 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
203 data && datalen ? datalen : 0);
205 req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
206 RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
207 strlen(op_data->op_file_secctx_name) + 1 : 0);
209 req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
210 op_data->op_file_secctx_size);
212 req_capsule_set_size(&req->rq_pill, &RMF_FILE_ENCCTX, RCL_CLIENT,
213 op_data->op_file_encctx_size);
215 /* get SELinux policy info if any */
216 rc = sptlrpc_get_sepol(req);
218 ptlrpc_request_free(req);
221 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
222 strlen(req->rq_sepol) ?
223 strlen(req->rq_sepol) + 1 : 0);
225 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
227 ptlrpc_request_free(req);
232 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
233 * tgt, for symlinks or lov MD data.
235 mdc_create_pack(&req->rq_pill, op_data, data, datalen, mode, uid,
236 gid, cap_effective, rdev);
238 ptlrpc_request_set_replen(req);
240 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
242 req->rq_no_retry_einprogress = 1;
245 req->rq_generation_set = 1;
246 req->rq_import_generation = generation;
247 req->rq_sent = ktime_get_real_seconds() + resends;
249 level = LUSTRE_IMP_FULL;
251 rc = mdc_reint(req, level);
253 /* Resend if we were told to. */
254 if (rc == -ERESTARTSYS) {
255 level = LUSTRE_IMP_RECOVER;
257 } else if (rc == -EINPROGRESS) {
258 /* Retry create infinitely until succeed or get other
259 * error code or interrupted. */
260 ptlrpc_req_finished(req);
261 if (generation == import->imp_generation) {
262 if (signal_pending(current))
266 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
267 exp->exp_obd->obd_name, resends,
268 PFID(&op_data->op_fid1),
269 PFID(&op_data->op_fid2));
272 CDEBUG(D_HA, "resend cross eviction\n");
281 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
282 struct ptlrpc_request **request)
285 struct obd_device *obd = class_exp2obd(exp);
286 struct ptlrpc_request *req = *request;
290 LASSERT(req == NULL);
292 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
293 (fid_is_sane(&op_data->op_fid1)))
294 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
296 MDS_INODELOCK_UPDATE);
297 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
298 (fid_is_sane(&op_data->op_fid3)))
299 /* cancel DOM lock only if it has no data to flush */
300 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
302 op_data->op_cli_flags &
306 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
307 &RQF_MDS_REINT_UNLINK);
309 ldlm_lock_list_put(&cancels, l_bl_ast, count);
313 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
314 op_data->op_namelen + 1);
316 /* get SELinux policy info if any */
317 rc = sptlrpc_get_sepol(req);
319 ptlrpc_request_free(req);
322 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
323 strlen(req->rq_sepol) ?
324 strlen(req->rq_sepol) + 1 : 0);
326 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
328 ptlrpc_request_free(req);
332 mdc_unlink_pack(&req->rq_pill, op_data);
334 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
335 obd->u.cli.cl_default_mds_easize);
336 ptlrpc_request_set_replen(req);
340 rc = mdc_reint(req, LUSTRE_IMP_FULL);
341 if (rc == -ERESTARTSYS)
346 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
347 struct ptlrpc_request **request)
350 struct ptlrpc_request *req;
354 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
355 (fid_is_sane(&op_data->op_fid2)))
356 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
358 MDS_INODELOCK_UPDATE);
359 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
360 (fid_is_sane(&op_data->op_fid1)))
361 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
363 MDS_INODELOCK_UPDATE);
365 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
367 ldlm_lock_list_put(&cancels, l_bl_ast, count);
371 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
372 op_data->op_namelen + 1);
374 /* get SELinux policy info if any */
375 rc = sptlrpc_get_sepol(req);
377 ptlrpc_request_free(req);
380 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
381 strlen(req->rq_sepol) ?
382 strlen(req->rq_sepol) + 1 : 0);
384 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
386 ptlrpc_request_free(req);
390 mdc_link_pack(&req->rq_pill, op_data);
391 ptlrpc_request_set_replen(req);
393 rc = mdc_reint(req, LUSTRE_IMP_FULL);
395 if (rc == -ERESTARTSYS)
401 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
402 const char *old, size_t oldlen, const char *new, size_t newlen,
403 struct ptlrpc_request **request)
406 struct obd_device *obd = exp->exp_obd;
407 struct ptlrpc_request *req;
412 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
413 (fid_is_sane(&op_data->op_fid1)))
414 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
416 MDS_INODELOCK_UPDATE);
417 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
418 (fid_is_sane(&op_data->op_fid2)))
419 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
421 MDS_INODELOCK_UPDATE);
422 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
423 (fid_is_sane(&op_data->op_fid3)))
424 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
426 MDS_INODELOCK_LOOKUP);
427 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
428 (fid_is_sane(&op_data->op_fid4)))
429 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
433 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
434 op_data->op_cli_flags & CLI_MIGRATE ?
435 &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
437 ldlm_lock_list_put(&cancels, l_bl_ast, count);
441 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
442 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
443 if (op_data->op_cli_flags & CLI_MIGRATE)
444 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
445 op_data->op_data_size);
447 /* get SELinux policy info if any */
448 rc = sptlrpc_get_sepol(req);
450 ptlrpc_request_free(req);
453 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
454 strlen(req->rq_sepol) ?
455 strlen(req->rq_sepol) + 1 : 0);
457 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
459 ptlrpc_request_free(req);
463 if (exp_connect_cancelset(exp) && req)
464 ldlm_cli_cancel_list(&cancels, count, req, 0);
466 if (op_data->op_cli_flags & CLI_MIGRATE)
467 mdc_migrate_pack(&req->rq_pill, op_data, old, oldlen);
469 mdc_rename_pack(&req->rq_pill, op_data, old, oldlen,
472 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
473 obd->u.cli.cl_default_mds_easize);
474 ptlrpc_request_set_replen(req);
476 rc = mdc_reint(req, LUSTRE_IMP_FULL);
478 if (rc == -ERESTARTSYS)
484 int mdc_file_resync(struct obd_export *exp, struct md_op_data *op_data)
487 struct ptlrpc_request *req;
488 struct ldlm_lock *lock;
489 struct mdt_rec_resync *rec;
493 if (op_data->op_flags & MF_MDC_CANCEL_FID1 &&
494 fid_is_sane(&op_data->op_fid1))
495 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
497 MDS_INODELOCK_LAYOUT);
499 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
500 &RQF_MDS_REINT_RESYNC);
502 ldlm_lock_list_put(&cancels, l_bl_ast, count);
506 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
508 ptlrpc_request_free(req);
512 BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
513 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
514 rec->rs_opcode = REINT_RESYNC;
515 rec->rs_fsuid = op_data->op_fsuid;
516 rec->rs_fsgid = op_data->op_fsgid;
517 rec->rs_cap = op_data->op_cap.cap[0];
518 rec->rs_fid = op_data->op_fid1;
519 rec->rs_bias = op_data->op_bias;
520 rec->rs_mirror_id = op_data->op_mirror_id;
522 lock = ldlm_handle2lock(&op_data->op_lease_handle);
524 rec->rs_lease_handle = lock->l_remote_handle;
528 ptlrpc_request_set_replen(req);
530 rc = mdc_reint(req, LUSTRE_IMP_FULL);
531 if (rc == -ERESTARTSYS)
534 ptlrpc_req_finished(req);