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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_MDC
35 #include <linux/module.h>
36 #include <linux/kernel.h>
38 #include <obd_class.h>
39 #include "mdc_internal.h"
40 #include <lustre_fid.h>
42 /* mdc_setattr does its own semaphore handling */
43 static int mdc_reint(struct ptlrpc_request *request, int level)
47 request->rq_send_state = level;
49 mdc_get_mod_rpc_slot(request, NULL);
50 rc = ptlrpc_queue_wait(request);
51 mdc_put_mod_rpc_slot(request, NULL);
53 CDEBUG(D_INFO, "error in handling %d\n", rc);
54 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
60 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
61 * found by @fid. Found locks are added into @cancel list. Returns the amount of
62 * locks added to @cancels list. */
63 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
64 struct list_head *cancels, enum ldlm_mode mode,
67 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
68 union ldlm_policy_data policy = { {0} };
69 struct ldlm_res_id res_id;
70 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 fid_build_reg_res_name(fid, &res_id);
84 res = ldlm_resource_get(exp->exp_obd->obd_namespace,
88 LDLM_RESOURCE_ADDREF(res);
89 /* Initialize ibits lock policy. */
90 policy.l_inodebits.bits = bits;
91 count = ldlm_cancel_resource_local(res, cancels, &policy,
93 LDLM_RESOURCE_DELREF(res);
94 ldlm_resource_putref(res);
98 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
99 void *ea, size_t ealen, struct ptlrpc_request **request)
101 struct list_head cancels = LIST_HEAD_INIT(cancels);
102 struct ptlrpc_request *req;
107 LASSERT(op_data != NULL);
109 bits = MDS_INODELOCK_UPDATE;
110 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
111 bits |= MDS_INODELOCK_LOOKUP;
112 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
113 (fid_is_sane(&op_data->op_fid1)))
114 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
115 &cancels, LCK_EX, bits);
116 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
117 &RQF_MDS_REINT_SETATTR);
119 ldlm_lock_list_put(&cancels, l_bl_ast, count);
123 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
124 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
125 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
127 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
129 ptlrpc_request_free(req);
133 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
134 CDEBUG(D_INODE, "setting mtime %ld, ctime %ld\n",
135 LTIME_S(op_data->op_attr.ia_mtime),
136 LTIME_S(op_data->op_attr.ia_ctime));
137 mdc_setattr_pack(req, op_data, ea, ealen);
139 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
140 req->rq_import->imp_connect_data.ocd_max_easize);
141 ptlrpc_request_set_replen(req);
143 rc = mdc_reint(req, LUSTRE_IMP_FULL);
144 if (rc == -ERESTARTSYS)
152 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
153 const void *data, size_t datalen,
154 umode_t mode, uid_t uid, gid_t gid,
155 cfs_cap_t cap_effective, __u64 rdev,
156 struct ptlrpc_request **request)
158 struct ptlrpc_request *req;
160 int count, resends = 0;
161 struct obd_import *import = exp->exp_obd->u.cli.cl_import;
162 int generation = import->imp_generation;
163 struct list_head cancels = LIST_HEAD_INIT(cancels);
166 /* For case if upper layer did not alloc fid, do it now. */
167 if (!fid_is_sane(&op_data->op_fid2)) {
169 * mdc_fid_alloc() may return errno 1 in case of switch to new
170 * sequence, handle this.
172 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
179 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
180 (fid_is_sane(&op_data->op_fid1)))
181 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
183 MDS_INODELOCK_UPDATE);
185 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
186 &RQF_MDS_REINT_CREATE_ACL);
188 ldlm_lock_list_put(&cancels, l_bl_ast, count);
192 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
193 op_data->op_namelen + 1);
194 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
195 data && datalen ? datalen : 0);
197 req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
198 RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
199 strlen(op_data->op_file_secctx_name) + 1 : 0);
201 req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
202 op_data->op_file_secctx_size);
204 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
206 ptlrpc_request_free(req);
211 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
212 * tgt, for symlinks or lov MD data.
214 mdc_create_pack(req, op_data, data, datalen, mode, uid,
215 gid, cap_effective, rdev);
217 ptlrpc_request_set_replen(req);
219 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
221 req->rq_no_retry_einprogress = 1;
224 req->rq_generation_set = 1;
225 req->rq_import_generation = generation;
226 req->rq_sent = ktime_get_real_seconds() + resends;
228 level = LUSTRE_IMP_FULL;
230 rc = mdc_reint(req, level);
232 /* Resend if we were told to. */
233 if (rc == -ERESTARTSYS) {
234 level = LUSTRE_IMP_RECOVER;
236 } else if (rc == -EINPROGRESS) {
237 /* Retry create infinitely until succeed or get other
238 * error code or interrupted. */
239 ptlrpc_req_finished(req);
240 if (generation == import->imp_generation) {
241 if (signal_pending(current))
245 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
246 exp->exp_obd->obd_name, resends,
247 PFID(&op_data->op_fid1),
248 PFID(&op_data->op_fid2));
251 CDEBUG(D_HA, "resend cross eviction\n");
260 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
261 struct ptlrpc_request **request)
263 struct list_head cancels = LIST_HEAD_INIT(cancels);
264 struct obd_device *obd = class_exp2obd(exp);
265 struct ptlrpc_request *req = *request;
269 LASSERT(req == NULL);
271 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
272 (fid_is_sane(&op_data->op_fid1)))
273 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
275 MDS_INODELOCK_UPDATE);
276 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
277 (fid_is_sane(&op_data->op_fid3)))
278 /* don't cancel DoM lock which may cause data flush */
279 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
282 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
283 &RQF_MDS_REINT_UNLINK);
285 ldlm_lock_list_put(&cancels, l_bl_ast, count);
289 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
290 op_data->op_namelen + 1);
292 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
294 ptlrpc_request_free(req);
298 mdc_unlink_pack(req, op_data);
300 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
301 obd->u.cli.cl_default_mds_easize);
302 ptlrpc_request_set_replen(req);
306 rc = mdc_reint(req, LUSTRE_IMP_FULL);
307 if (rc == -ERESTARTSYS)
312 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
313 struct ptlrpc_request **request)
315 struct list_head cancels = LIST_HEAD_INIT(cancels);
316 struct ptlrpc_request *req;
320 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
321 (fid_is_sane(&op_data->op_fid2)))
322 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
324 MDS_INODELOCK_UPDATE);
325 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
326 (fid_is_sane(&op_data->op_fid1)))
327 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
329 MDS_INODELOCK_UPDATE);
331 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
333 ldlm_lock_list_put(&cancels, l_bl_ast, count);
337 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
338 op_data->op_namelen + 1);
340 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
342 ptlrpc_request_free(req);
346 mdc_link_pack(req, op_data);
347 ptlrpc_request_set_replen(req);
349 rc = mdc_reint(req, LUSTRE_IMP_FULL);
351 if (rc == -ERESTARTSYS)
357 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
358 const char *old, size_t oldlen, const char *new, size_t newlen,
359 struct ptlrpc_request **request)
361 struct list_head cancels = LIST_HEAD_INIT(cancels);
362 struct obd_device *obd = exp->exp_obd;
363 struct ptlrpc_request *req;
367 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
368 (fid_is_sane(&op_data->op_fid1)))
369 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
371 MDS_INODELOCK_UPDATE);
372 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
373 (fid_is_sane(&op_data->op_fid2)))
374 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
376 MDS_INODELOCK_UPDATE);
377 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
378 (fid_is_sane(&op_data->op_fid3)))
379 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
381 MDS_INODELOCK_LOOKUP);
382 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
383 (fid_is_sane(&op_data->op_fid4)))
384 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
388 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
389 op_data->op_cli_flags & CLI_MIGRATE ?
390 &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
392 ldlm_lock_list_put(&cancels, l_bl_ast, count);
396 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
397 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
399 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
401 ptlrpc_request_free(req);
405 if (op_data->op_cli_flags & CLI_MIGRATE && op_data->op_data != NULL) {
406 struct md_open_data *mod = op_data->op_data;
408 LASSERTF(mod->mod_open_req != NULL &&
409 mod->mod_open_req->rq_type != LI_POISON,
410 "POISONED open %p!\n", mod->mod_open_req);
412 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
413 /* We no longer want to preserve this open for replay even
414 * though the open was committed. b=3632, b=3633 */
415 spin_lock(&mod->mod_open_req->rq_lock);
416 mod->mod_open_req->rq_replay = 0;
417 spin_unlock(&mod->mod_open_req->rq_lock);
420 if (exp_connect_cancelset(exp) && req)
421 ldlm_cli_cancel_list(&cancels, count, req, 0);
423 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
425 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
426 obd->u.cli.cl_default_mds_easize);
427 ptlrpc_request_set_replen(req);
429 rc = mdc_reint(req, LUSTRE_IMP_FULL);
431 if (rc == -ERESTARTSYS)
437 int mdc_file_resync(struct obd_export *exp, struct md_op_data *op_data)
439 struct list_head cancels = LIST_HEAD_INIT(cancels);
440 struct ptlrpc_request *req;
441 struct ldlm_lock *lock;
442 struct mdt_rec_resync *rec;
446 if (op_data->op_flags & MF_MDC_CANCEL_FID1 &&
447 fid_is_sane(&op_data->op_fid1))
448 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
450 MDS_INODELOCK_LAYOUT);
452 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
453 &RQF_MDS_REINT_RESYNC);
455 ldlm_lock_list_put(&cancels, l_bl_ast, count);
459 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
461 ptlrpc_request_free(req);
465 CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
466 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
467 rec->rs_opcode = REINT_RESYNC;
468 rec->rs_fsuid = op_data->op_fsuid;
469 rec->rs_fsgid = op_data->op_fsgid;
470 rec->rs_cap = op_data->op_cap;
471 rec->rs_fid = op_data->op_fid1;
472 rec->rs_bias = op_data->op_bias;
474 lock = ldlm_handle2lock(&op_data->op_handle);
476 rec->rs_handle = lock->l_remote_handle;
480 ptlrpc_request_set_replen(req);
482 rc = mdc_reint(req, LUSTRE_IMP_FULL);
483 if (rc == -ERESTARTSYS)
486 ptlrpc_req_finished(req);