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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_MDC
39 #include <linux/module.h>
40 #include <linux/kernel.h>
42 #include <obd_class.h>
43 #include "mdc_internal.h"
44 #include <lustre_fid.h>
46 /* mdc_setattr does its own semaphore handling */
47 static int mdc_reint(struct ptlrpc_request *request, int level)
51 request->rq_send_state = level;
53 mdc_get_mod_rpc_slot(request, NULL);
54 rc = ptlrpc_queue_wait(request);
55 mdc_put_mod_rpc_slot(request, NULL);
57 CDEBUG(D_INFO, "error in handling %d\n", rc);
58 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
64 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
65 * found by @fid. Found locks are added into @cancel list. Returns the amount of
66 * locks added to @cancels list. */
67 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
68 struct list_head *cancels, enum ldlm_mode mode,
71 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
72 union ldlm_policy_data policy = { {0} };
73 struct ldlm_res_id res_id;
74 struct ldlm_resource *res;
78 /* Return, i.e. cancel nothing, only if ELC is supported (flag in
79 * export) but disabled through procfs (flag in NS).
81 * This distinguishes from a case when ELC is not supported originally,
82 * when we still want to cancel locks in advance and just cancel them
83 * locally, without sending any RPC. */
84 if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
87 fid_build_reg_res_name(fid, &res_id);
88 res = ldlm_resource_get(exp->exp_obd->obd_namespace,
92 LDLM_RESOURCE_ADDREF(res);
93 /* Initialize ibits lock policy. */
94 policy.l_inodebits.bits = bits;
95 count = ldlm_cancel_resource_local(res, cancels, &policy,
97 LDLM_RESOURCE_DELREF(res);
98 ldlm_resource_putref(res);
102 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
103 void *ea, size_t ealen, struct ptlrpc_request **request)
105 struct list_head cancels = LIST_HEAD_INIT(cancels);
106 struct ptlrpc_request *req;
111 LASSERT(op_data != NULL);
113 bits = MDS_INODELOCK_UPDATE;
114 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
115 bits |= MDS_INODELOCK_LOOKUP;
116 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
117 (fid_is_sane(&op_data->op_fid1)))
118 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
119 &cancels, LCK_EX, bits);
120 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
121 &RQF_MDS_REINT_SETATTR);
123 ldlm_lock_list_put(&cancels, l_bl_ast, count);
127 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
128 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
129 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
131 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
133 ptlrpc_request_free(req);
137 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
138 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
139 ", ctime "CFS_TIME_T"\n",
140 LTIME_S(op_data->op_attr.ia_mtime),
141 LTIME_S(op_data->op_attr.ia_ctime));
142 mdc_setattr_pack(req, op_data, ea, ealen);
144 ptlrpc_request_set_replen(req);
146 rc = mdc_reint(req, LUSTRE_IMP_FULL);
147 if (rc == -ERESTARTSYS)
155 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
156 const void *data, size_t datalen,
157 umode_t mode, uid_t uid, gid_t gid,
158 cfs_cap_t cap_effective, __u64 rdev,
159 struct ptlrpc_request **request)
161 struct ptlrpc_request *req;
163 int count, resends = 0;
164 struct obd_import *import = exp->exp_obd->u.cli.cl_import;
165 int generation = import->imp_generation;
166 struct list_head cancels = LIST_HEAD_INIT(cancels);
169 /* For case if upper layer did not alloc fid, do it now. */
170 if (!fid_is_sane(&op_data->op_fid2)) {
172 * mdc_fid_alloc() may return errno 1 in case of switch to new
173 * sequence, handle this.
175 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
182 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
183 (fid_is_sane(&op_data->op_fid1)))
184 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
186 MDS_INODELOCK_UPDATE);
188 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
189 &RQF_MDS_REINT_CREATE_RMT_ACL);
191 ldlm_lock_list_put(&cancels, l_bl_ast, count);
195 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
196 op_data->op_namelen + 1);
197 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
198 data && datalen ? datalen : 0);
200 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
202 ptlrpc_request_free(req);
207 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
208 * tgt, for symlinks or lov MD data.
210 mdc_create_pack(req, op_data, data, datalen, mode, uid,
211 gid, cap_effective, rdev);
213 ptlrpc_request_set_replen(req);
215 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
217 req->rq_no_retry_einprogress = 1;
220 req->rq_generation_set = 1;
221 req->rq_import_generation = generation;
222 req->rq_sent = cfs_time_current_sec() + resends;
224 level = LUSTRE_IMP_FULL;
226 rc = mdc_reint(req, level);
228 /* Resend if we were told to. */
229 if (rc == -ERESTARTSYS) {
230 level = LUSTRE_IMP_RECOVER;
232 } else if (rc == -EINPROGRESS) {
233 /* Retry create infinitely until succeed or get other
235 ptlrpc_req_finished(req);
238 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
239 exp->exp_obd->obd_name, resends,
240 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
242 if (generation == import->imp_generation) {
245 CDEBUG(D_HA, "resend cross eviction\n");
254 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
255 struct ptlrpc_request **request)
257 struct list_head cancels = LIST_HEAD_INIT(cancels);
258 struct obd_device *obd = class_exp2obd(exp);
259 struct ptlrpc_request *req = *request;
263 LASSERT(req == NULL);
265 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
266 (fid_is_sane(&op_data->op_fid1)))
267 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
269 MDS_INODELOCK_UPDATE);
270 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
271 (fid_is_sane(&op_data->op_fid3)))
272 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
275 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
276 &RQF_MDS_REINT_UNLINK);
278 ldlm_lock_list_put(&cancels, l_bl_ast, count);
282 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
283 op_data->op_namelen + 1);
285 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
287 ptlrpc_request_free(req);
291 mdc_unlink_pack(req, op_data);
293 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
294 obd->u.cli.cl_default_mds_easize);
295 ptlrpc_request_set_replen(req);
299 rc = mdc_reint(req, LUSTRE_IMP_FULL);
300 if (rc == -ERESTARTSYS)
305 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
306 struct ptlrpc_request **request)
308 struct list_head cancels = LIST_HEAD_INIT(cancels);
309 struct ptlrpc_request *req;
313 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
314 (fid_is_sane(&op_data->op_fid2)))
315 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
317 MDS_INODELOCK_UPDATE);
318 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
319 (fid_is_sane(&op_data->op_fid1)))
320 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
322 MDS_INODELOCK_UPDATE);
324 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
326 ldlm_lock_list_put(&cancels, l_bl_ast, count);
330 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
331 op_data->op_namelen + 1);
333 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
335 ptlrpc_request_free(req);
339 mdc_link_pack(req, op_data);
340 ptlrpc_request_set_replen(req);
342 rc = mdc_reint(req, LUSTRE_IMP_FULL);
344 if (rc == -ERESTARTSYS)
350 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
351 const char *old, size_t oldlen, const char *new, size_t newlen,
352 struct ptlrpc_request **request)
354 struct list_head cancels = LIST_HEAD_INIT(cancels);
355 struct obd_device *obd = exp->exp_obd;
356 struct ptlrpc_request *req;
360 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
361 (fid_is_sane(&op_data->op_fid1)))
362 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
364 MDS_INODELOCK_UPDATE);
365 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
366 (fid_is_sane(&op_data->op_fid2)))
367 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
369 MDS_INODELOCK_UPDATE);
370 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
371 (fid_is_sane(&op_data->op_fid3)))
372 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
374 MDS_INODELOCK_LOOKUP);
375 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
376 (fid_is_sane(&op_data->op_fid4)))
377 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
381 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
382 op_data->op_cli_flags & CLI_MIGRATE ?
383 &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
385 ldlm_lock_list_put(&cancels, l_bl_ast, count);
389 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
390 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
392 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
394 ptlrpc_request_free(req);
398 if (op_data->op_cli_flags & CLI_MIGRATE && op_data->op_data != NULL) {
399 struct md_open_data *mod = op_data->op_data;
401 LASSERTF(mod->mod_open_req != NULL &&
402 mod->mod_open_req->rq_type != LI_POISON,
403 "POISONED open %p!\n", mod->mod_open_req);
405 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
406 /* We no longer want to preserve this open for replay even
407 * though the open was committed. b=3632, b=3633 */
408 spin_lock(&mod->mod_open_req->rq_lock);
409 mod->mod_open_req->rq_replay = 0;
410 spin_unlock(&mod->mod_open_req->rq_lock);
413 if (exp_connect_cancelset(exp) && req)
414 ldlm_cli_cancel_list(&cancels, count, req, 0);
416 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
418 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
419 obd->u.cli.cl_default_mds_easize);
420 ptlrpc_request_set_replen(req);
422 rc = mdc_reint(req, LUSTRE_IMP_FULL);
424 if (rc == -ERESTARTSYS)