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, 2013, 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, int ealen, void *ea2, int ea2len,
106 struct ptlrpc_request **request, struct md_open_data **mod)
108 struct list_head cancels = LIST_HEAD_INIT(cancels);
109 struct ptlrpc_request *req;
110 struct mdc_rpc_lock *rpc_lock;
111 struct obd_device *obd = exp->exp_obd;
116 LASSERT(op_data != NULL);
118 bits = MDS_INODELOCK_UPDATE;
119 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
120 bits |= MDS_INODELOCK_LOOKUP;
121 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
122 (fid_is_sane(&op_data->op_fid1)) &&
123 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
124 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
125 &cancels, LCK_EX, bits);
126 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
127 &RQF_MDS_REINT_SETATTR);
129 ldlm_lock_list_put(&cancels, l_bl_ast, count);
132 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
133 if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
134 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
136 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
137 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
140 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
142 ptlrpc_request_free(req);
146 rpc_lock = obd->u.cli.cl_rpc_lock;
148 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
149 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
150 ", ctime "CFS_TIME_T"\n",
151 LTIME_S(op_data->op_attr.ia_mtime),
152 LTIME_S(op_data->op_attr.ia_ctime));
153 mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
155 ptlrpc_request_set_replen(req);
156 if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
157 req->rq_import->imp_replayable)
159 LASSERT(*mod == NULL);
161 *mod = obd_mod_alloc();
163 DEBUG_REQ(D_ERROR, req, "Can't allocate "
167 req->rq_cb_data = *mod;
168 (*mod)->mod_open_req = req;
169 req->rq_commit_cb = mdc_commit_open;
170 (*mod)->mod_is_create = true;
172 * Take an extra reference on \var mod, it protects \var
173 * mod from being freed on eviction (commit callback is
174 * called despite rq_replay flag).
175 * Will be put on mdc_done_writing().
181 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
183 /* Save the obtained info in the original RPC for the replay case. */
184 if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
185 struct mdt_ioepoch *epoch;
186 struct mdt_body *body;
188 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
189 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
190 LASSERT(epoch != NULL);
191 LASSERT(body != NULL);
192 epoch->handle = body->mbo_handle;
193 epoch->ioepoch = body->mbo_ioepoch;
194 req->rq_replay_cb = mdc_replay_open;
195 /** bug 3633, open may be committed and estale answer is not error */
196 } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
198 } else if (rc == -ERESTARTSYS) {
202 if (rc && req->rq_commit_cb) {
203 /* Put an extra reference on \var mod on error case. */
204 if (mod != NULL && *mod != NULL)
206 req->rq_commit_cb(req);
211 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
212 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
213 cfs_cap_t cap_effective, __u64 rdev,
214 struct ptlrpc_request **request)
216 struct ptlrpc_request *req;
218 int count, resends = 0;
219 struct obd_import *import = exp->exp_obd->u.cli.cl_import;
220 int generation = import->imp_generation;
221 struct list_head cancels = LIST_HEAD_INIT(cancels);
224 /* For case if upper layer did not alloc fid, do it now. */
225 if (!fid_is_sane(&op_data->op_fid2)) {
227 * mdc_fid_alloc() may return errno 1 in case of switch to new
228 * sequence, handle this.
230 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
237 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
238 (fid_is_sane(&op_data->op_fid1)))
239 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
241 MDS_INODELOCK_UPDATE);
243 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
244 &RQF_MDS_REINT_CREATE_RMT_ACL);
246 ldlm_lock_list_put(&cancels, l_bl_ast, count);
249 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
250 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
251 op_data->op_namelen + 1);
252 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
253 data && datalen ? datalen : 0);
255 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
257 ptlrpc_request_free(req);
262 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
263 * tgt, for symlinks or lov MD data.
265 mdc_create_pack(req, op_data, data, datalen, mode, uid,
266 gid, cap_effective, rdev);
268 ptlrpc_request_set_replen(req);
270 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
272 req->rq_no_retry_einprogress = 1;
275 req->rq_generation_set = 1;
276 req->rq_import_generation = generation;
277 req->rq_sent = cfs_time_current_sec() + resends;
279 level = LUSTRE_IMP_FULL;
281 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
283 /* Resend if we were told to. */
284 if (rc == -ERESTARTSYS) {
285 level = LUSTRE_IMP_RECOVER;
287 } else if (rc == -EINPROGRESS) {
288 /* Retry create infinitely until succeed or get other
290 ptlrpc_req_finished(req);
293 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
294 exp->exp_obd->obd_name, resends,
295 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
297 if (generation == import->imp_generation) {
300 CDEBUG(D_HA, "resend cross eviction\n");
303 } else if (rc == 0) {
304 struct mdt_body *body;
305 struct lustre_capa *capa;
307 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
309 if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
310 capa = req_capsule_server_get(&req->rq_pill,
321 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
322 struct ptlrpc_request **request)
324 struct list_head cancels = LIST_HEAD_INIT(cancels);
325 struct obd_device *obd = class_exp2obd(exp);
326 struct ptlrpc_request *req = *request;
330 LASSERT(req == NULL);
332 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
333 (fid_is_sane(&op_data->op_fid1)) &&
334 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
335 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
337 MDS_INODELOCK_UPDATE);
338 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
339 (fid_is_sane(&op_data->op_fid3)) &&
340 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
341 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
344 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
345 &RQF_MDS_REINT_UNLINK);
347 ldlm_lock_list_put(&cancels, l_bl_ast, count);
350 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
351 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
352 op_data->op_namelen + 1);
354 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
356 ptlrpc_request_free(req);
360 mdc_unlink_pack(req, op_data);
362 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
363 obd->u.cli.cl_default_mds_easize);
364 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
365 obd->u.cli.cl_default_mds_cookiesize);
366 ptlrpc_request_set_replen(req);
370 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
371 if (rc == -ERESTARTSYS)
376 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
377 struct ptlrpc_request **request)
379 struct list_head cancels = LIST_HEAD_INIT(cancels);
380 struct obd_device *obd = exp->exp_obd;
381 struct ptlrpc_request *req;
385 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
386 (fid_is_sane(&op_data->op_fid2)))
387 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
389 MDS_INODELOCK_UPDATE);
390 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
391 (fid_is_sane(&op_data->op_fid1)))
392 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
394 MDS_INODELOCK_UPDATE);
396 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
398 ldlm_lock_list_put(&cancels, l_bl_ast, count);
401 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
402 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
403 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
404 op_data->op_namelen + 1);
406 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
408 ptlrpc_request_free(req);
412 mdc_link_pack(req, op_data);
413 ptlrpc_request_set_replen(req);
415 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
417 if (rc == -ERESTARTSYS)
423 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
424 const char *old, int oldlen, const char *new, int newlen,
425 struct ptlrpc_request **request)
427 struct list_head cancels = LIST_HEAD_INIT(cancels);
428 struct obd_device *obd = exp->exp_obd;
429 struct ptlrpc_request *req;
433 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
434 (fid_is_sane(&op_data->op_fid1)))
435 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
437 MDS_INODELOCK_UPDATE);
438 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
439 (fid_is_sane(&op_data->op_fid2)))
440 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
442 MDS_INODELOCK_UPDATE);
443 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
444 (fid_is_sane(&op_data->op_fid3)))
445 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
447 MDS_INODELOCK_LOOKUP);
448 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
449 (fid_is_sane(&op_data->op_fid4)))
450 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
454 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
455 &RQF_MDS_REINT_RENAME);
457 ldlm_lock_list_put(&cancels, l_bl_ast, count);
461 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
462 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
463 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
464 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
466 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
468 ptlrpc_request_free(req);
472 if (exp_connect_cancelset(exp) && req)
473 ldlm_cli_cancel_list(&cancels, count, req, 0);
475 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
477 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
478 obd->u.cli.cl_default_mds_easize);
479 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
480 obd->u.cli.cl_default_mds_cookiesize);
481 ptlrpc_request_set_replen(req);
483 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
485 if (rc == -ERESTARTSYS)