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, 2014, 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,
48 struct mdc_rpc_lock *rpc_lock,
53 request->rq_send_state = level;
55 mdc_get_rpc_lock(rpc_lock, NULL);
56 rc = ptlrpc_queue_wait(request);
57 mdc_put_rpc_lock(rpc_lock, NULL);
59 CDEBUG(D_INFO, "error in handling %d\n", rc);
60 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
66 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
67 * found by @fid. Found locks are added into @cancel list. Returns the amount of
68 * locks added to @cancels list. */
69 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
70 struct list_head *cancels, ldlm_mode_t mode,
73 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
74 ldlm_policy_data_t policy = { {0} };
75 struct ldlm_res_id res_id;
76 struct ldlm_resource *res;
80 /* Return, i.e. cancel nothing, only if ELC is supported (flag in
81 * export) but disabled through procfs (flag in NS).
83 * This distinguishes from a case when ELC is not supported originally,
84 * when we still want to cancel locks in advance and just cancel them
85 * locally, without sending any RPC. */
86 if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
89 fid_build_reg_res_name(fid, &res_id);
90 res = ldlm_resource_get(exp->exp_obd->obd_namespace,
94 LDLM_RESOURCE_ADDREF(res);
95 /* Initialize ibits lock policy. */
96 policy.l_inodebits.bits = bits;
97 count = ldlm_cancel_resource_local(res, cancels, &policy,
99 LDLM_RESOURCE_DELREF(res);
100 ldlm_resource_putref(res);
104 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
105 void *ea, size_t ealen, struct ptlrpc_request **request)
107 struct list_head cancels = LIST_HEAD_INIT(cancels);
108 struct ptlrpc_request *req;
109 struct mdc_rpc_lock *rpc_lock;
110 struct obd_device *obd = exp->exp_obd;
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 rpc_lock = obd->u.cli.cl_rpc_lock;
143 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
144 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
145 ", ctime "CFS_TIME_T"\n",
146 LTIME_S(op_data->op_attr.ia_mtime),
147 LTIME_S(op_data->op_attr.ia_ctime));
148 mdc_setattr_pack(req, op_data, ea, ealen);
150 ptlrpc_request_set_replen(req);
152 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
153 if (rc == -ERESTARTSYS)
161 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
162 const void *data, size_t datalen,
163 umode_t mode, uid_t uid, gid_t gid,
164 cfs_cap_t cap_effective, __u64 rdev,
165 struct ptlrpc_request **request)
167 struct ptlrpc_request *req;
169 int count, resends = 0;
170 struct obd_import *import = exp->exp_obd->u.cli.cl_import;
171 int generation = import->imp_generation;
172 struct list_head cancels = LIST_HEAD_INIT(cancels);
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_RMT_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 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
208 ptlrpc_request_free(req);
213 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
214 * tgt, for symlinks or lov MD data.
216 mdc_create_pack(req, op_data, data, datalen, mode, uid,
217 gid, cap_effective, rdev);
219 ptlrpc_request_set_replen(req);
221 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
223 req->rq_no_retry_einprogress = 1;
226 req->rq_generation_set = 1;
227 req->rq_import_generation = generation;
228 req->rq_sent = cfs_time_current_sec() + resends;
230 level = LUSTRE_IMP_FULL;
232 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
234 /* Resend if we were told to. */
235 if (rc == -ERESTARTSYS) {
236 level = LUSTRE_IMP_RECOVER;
238 } else if (rc == -EINPROGRESS) {
239 /* Retry create infinitely until succeed or get other
241 ptlrpc_req_finished(req);
244 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
245 exp->exp_obd->obd_name, resends,
246 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
248 if (generation == import->imp_generation) {
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 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
281 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
282 &RQF_MDS_REINT_UNLINK);
284 ldlm_lock_list_put(&cancels, l_bl_ast, count);
288 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
289 op_data->op_namelen + 1);
291 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
293 ptlrpc_request_free(req);
297 mdc_unlink_pack(req, op_data);
299 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
300 obd->u.cli.cl_default_mds_easize);
301 ptlrpc_request_set_replen(req);
305 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
306 if (rc == -ERESTARTSYS)
311 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
312 struct ptlrpc_request **request)
314 struct list_head cancels = LIST_HEAD_INIT(cancels);
315 struct obd_device *obd = exp->exp_obd;
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, obd->u.cli.cl_rpc_lock, 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 &RQF_MDS_REINT_RENAME);
391 ldlm_lock_list_put(&cancels, l_bl_ast, count);
395 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
396 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
398 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
400 ptlrpc_request_free(req);
404 if (exp_connect_cancelset(exp) && req)
405 ldlm_cli_cancel_list(&cancels, count, req, 0);
407 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
409 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
410 obd->u.cli.cl_default_mds_easize);
411 ptlrpc_request_set_replen(req);
413 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
415 if (rc == -ERESTARTSYS)