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
40 # include <linux/module.h>
41 # include <linux/kernel.h>
43 # include <liblustre.h>
46 #include <obd_class.h>
47 #include "mdc_internal.h"
48 #include <lustre_fid.h>
50 /* mdc_setattr does its own semaphore handling */
51 static int mdc_reint(struct ptlrpc_request *request,
52 struct mdc_rpc_lock *rpc_lock,
57 request->rq_send_state = level;
59 mdc_get_rpc_lock(rpc_lock, NULL);
60 rc = ptlrpc_queue_wait(request);
61 mdc_put_rpc_lock(rpc_lock, NULL);
63 CDEBUG(D_INFO, "error in handling %d\n", rc);
64 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
70 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
71 * found by @fid. Found locks are added into @cancel list. Returns the amount of
72 * locks added to @cancels list. */
73 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
74 cfs_list_t *cancels, ldlm_mode_t mode,
77 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
78 ldlm_policy_data_t policy = {{0}};
79 struct ldlm_res_id res_id;
80 struct ldlm_resource *res;
84 /* Return, i.e. cancel nothing, only if ELC is supported (flag in
85 * export) but disabled through procfs (flag in NS).
87 * This distinguishes from a case when ELC is not supported originally,
88 * when we still want to cancel locks in advance and just cancel them
89 * locally, without sending any RPC. */
90 if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
93 fid_build_reg_res_name(fid, &res_id);
94 res = ldlm_resource_get(exp->exp_obd->obd_namespace,
98 LDLM_RESOURCE_ADDREF(res);
99 /* Initialize ibits lock policy. */
100 policy.l_inodebits.bits = bits;
101 count = ldlm_cancel_resource_local(res, cancels, &policy,
103 LDLM_RESOURCE_DELREF(res);
104 ldlm_resource_putref(res);
108 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
109 void *ea, int ealen, void *ea2, int ea2len,
110 struct ptlrpc_request **request, struct md_open_data **mod)
112 CFS_LIST_HEAD(cancels);
113 struct ptlrpc_request *req;
114 struct mdc_rpc_lock *rpc_lock;
115 struct obd_device *obd = exp->exp_obd;
120 LASSERT(op_data != NULL);
122 bits = MDS_INODELOCK_UPDATE;
123 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
124 bits |= MDS_INODELOCK_LOOKUP;
125 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
126 (fid_is_sane(&op_data->op_fid1)) &&
127 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
128 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
129 &cancels, LCK_EX, bits);
130 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
131 &RQF_MDS_REINT_SETATTR);
133 ldlm_lock_list_put(&cancels, l_bl_ast, count);
136 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
137 if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
138 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
140 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
141 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
144 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
146 ptlrpc_request_free(req);
150 rpc_lock = obd->u.cli.cl_rpc_lock;
152 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
153 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
154 ", ctime "CFS_TIME_T"\n",
155 LTIME_S(op_data->op_attr.ia_mtime),
156 LTIME_S(op_data->op_attr.ia_ctime));
157 mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
159 ptlrpc_request_set_replen(req);
160 if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
161 req->rq_import->imp_replayable)
163 LASSERT(*mod == NULL);
165 *mod = obd_mod_alloc();
167 DEBUG_REQ(D_ERROR, req, "Can't allocate "
171 req->rq_cb_data = *mod;
172 (*mod)->mod_open_req = req;
173 req->rq_commit_cb = mdc_commit_open;
175 * Take an extra reference on \var mod, it protects \var
176 * mod from being freed on eviction (commit callback is
177 * called despite rq_replay flag).
178 * Will be put on mdc_done_writing().
184 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
186 /* Save the obtained info in the original RPC for the replay case. */
187 if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
188 struct mdt_ioepoch *epoch;
189 struct mdt_body *body;
191 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
192 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
193 LASSERT(epoch != NULL);
194 LASSERT(body != NULL);
195 epoch->handle = body->handle;
196 epoch->ioepoch = body->ioepoch;
197 req->rq_replay_cb = mdc_replay_open;
198 /** bug 3633, open may be committed and estale answer is not error */
199 } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
201 } else if (rc == -ERESTARTSYS) {
205 if (rc && req->rq_commit_cb) {
206 /* Put an extra reference on \var mod on error case. */
208 req->rq_commit_cb(req);
213 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
214 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
215 cfs_cap_t cap_effective, __u64 rdev,
216 struct ptlrpc_request **request)
218 struct ptlrpc_request *req;
220 int count, resends = 0;
221 struct obd_import *import = exp->exp_obd->u.cli.cl_import;
222 int generation = import->imp_generation;
223 CFS_LIST_HEAD(cancels);
226 /* For case if upper layer did not alloc fid, do it now. */
227 if (!fid_is_sane(&op_data->op_fid2)) {
229 * mdc_fid_alloc() may return errno 1 in case of switch to new
230 * sequence, handle this.
232 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
234 CERROR("Can't alloc new fid, rc %d\n", rc);
241 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
242 (fid_is_sane(&op_data->op_fid1)))
243 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
245 MDS_INODELOCK_UPDATE);
247 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
248 &RQF_MDS_REINT_CREATE_RMT_ACL);
250 ldlm_lock_list_put(&cancels, l_bl_ast, count);
253 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
254 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
255 op_data->op_namelen + 1);
256 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
257 data && datalen ? datalen : 0);
259 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
261 ptlrpc_request_free(req);
266 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
267 * tgt, for symlinks or lov MD data.
269 mdc_create_pack(req, op_data, data, datalen, mode, uid,
270 gid, cap_effective, rdev);
272 ptlrpc_request_set_replen(req);
274 /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
276 req->rq_no_retry_einprogress = 1;
279 req->rq_generation_set = 1;
280 req->rq_import_generation = generation;
281 req->rq_sent = cfs_time_current_sec() + resends;
283 level = LUSTRE_IMP_FULL;
285 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
287 /* Resend if we were told to. */
288 if (rc == -ERESTARTSYS) {
289 level = LUSTRE_IMP_RECOVER;
291 } else if (rc == -EINPROGRESS) {
292 /* Retry create infinitely until succeed or get other
294 ptlrpc_req_finished(req);
297 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
298 exp->exp_obd->obd_name, resends,
299 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
301 if (generation == import->imp_generation) {
304 CDEBUG(D_HA, "resend cross eviction\n");
307 } else if (rc == 0) {
308 struct mdt_body *body;
309 struct lustre_capa *capa;
311 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
313 if (body->valid & OBD_MD_FLMDSCAPA) {
314 capa = req_capsule_server_get(&req->rq_pill,
325 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
326 struct ptlrpc_request **request)
328 CFS_LIST_HEAD(cancels);
329 struct obd_device *obd = class_exp2obd(exp);
330 struct ptlrpc_request *req = *request;
334 LASSERT(req == NULL);
336 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
337 (fid_is_sane(&op_data->op_fid1)) &&
338 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
339 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
341 MDS_INODELOCK_UPDATE);
342 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
343 (fid_is_sane(&op_data->op_fid3)) &&
344 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
345 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
348 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
349 &RQF_MDS_REINT_UNLINK);
351 ldlm_lock_list_put(&cancels, l_bl_ast, count);
354 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
355 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
356 op_data->op_namelen + 1);
358 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
360 ptlrpc_request_free(req);
364 mdc_unlink_pack(req, op_data);
366 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
367 obd->u.cli.cl_max_mds_easize);
368 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
369 obd->u.cli.cl_max_mds_cookiesize);
370 ptlrpc_request_set_replen(req);
374 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
375 if (rc == -ERESTARTSYS)
380 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
381 struct ptlrpc_request **request)
383 CFS_LIST_HEAD(cancels);
384 struct obd_device *obd = exp->exp_obd;
385 struct ptlrpc_request *req;
389 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
390 (fid_is_sane(&op_data->op_fid2)))
391 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
393 MDS_INODELOCK_UPDATE);
394 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
395 (fid_is_sane(&op_data->op_fid1)))
396 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
398 MDS_INODELOCK_UPDATE);
400 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
402 ldlm_lock_list_put(&cancels, l_bl_ast, count);
405 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
406 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
407 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
408 op_data->op_namelen + 1);
410 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
412 ptlrpc_request_free(req);
416 mdc_link_pack(req, op_data);
417 ptlrpc_request_set_replen(req);
419 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
421 if (rc == -ERESTARTSYS)
427 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
428 const char *old, int oldlen, const char *new, int newlen,
429 struct ptlrpc_request **request)
431 CFS_LIST_HEAD(cancels);
432 struct obd_device *obd = exp->exp_obd;
433 struct ptlrpc_request *req;
437 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
438 (fid_is_sane(&op_data->op_fid1)))
439 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
441 MDS_INODELOCK_UPDATE);
442 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
443 (fid_is_sane(&op_data->op_fid2)))
444 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
446 MDS_INODELOCK_UPDATE);
447 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
448 (fid_is_sane(&op_data->op_fid3)))
449 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
451 MDS_INODELOCK_LOOKUP);
452 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
453 (fid_is_sane(&op_data->op_fid4)))
454 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
458 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
459 &RQF_MDS_REINT_RENAME);
461 ldlm_lock_list_put(&cancels, l_bl_ast, count);
465 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
466 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
467 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
468 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
470 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
472 ptlrpc_request_free(req);
476 if (exp_connect_cancelset(exp) && req)
477 ldlm_cli_cancel_list(&cancels, count, req, 0);
479 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
481 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
482 obd->u.cli.cl_max_mds_easize);
483 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
484 obd->u.cli.cl_max_mds_cookiesize);
485 ptlrpc_request_set_replen(req);
487 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
489 if (rc == -ERESTARTSYS)