1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 # define EXPORT_SYMTAB
42 #define DEBUG_SUBSYSTEM S_MDC
45 #ifndef AUTOCONF_INCLUDED
46 # include <linux/config.h>
48 # include <linux/module.h>
49 # include <linux/kernel.h>
51 # include <liblustre.h>
54 #include <obd_class.h>
55 #include "mdc_internal.h"
56 #include <lustre_fid.h>
58 /* mdc_setattr does its own semaphore handling */
59 static int mdc_reint(struct ptlrpc_request *request,
60 struct mdc_rpc_lock *rpc_lock,
65 request->rq_send_state = level;
67 mdc_get_rpc_lock(rpc_lock, NULL);
68 rc = ptlrpc_queue_wait(request);
69 mdc_put_rpc_lock(rpc_lock, NULL);
71 CDEBUG(D_INFO, "error in handling %d\n", rc);
72 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
78 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
79 * found by @fid. Found locks are added into @cancel list. Returns the amount of
80 * locks added to @cancels list. */
81 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
82 cfs_list_t *cancels, ldlm_mode_t mode,
85 ldlm_policy_data_t policy = {{0}};
86 struct ldlm_res_id res_id;
87 struct ldlm_resource *res;
91 fid_build_reg_res_name(fid, &res_id);
92 res = ldlm_resource_get(exp->exp_obd->obd_namespace,
96 LDLM_RESOURCE_ADDREF(res);
97 /* Initialize ibits lock policy. */
98 policy.l_inodebits.bits = bits;
99 count = ldlm_cancel_resource_local(res, cancels, &policy,
101 LDLM_RESOURCE_DELREF(res);
102 ldlm_resource_putref(res);
106 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
107 cfs_list_t *cancels, int count)
109 return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
113 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
114 void *ea, int ealen, void *ea2, int ea2len,
115 struct ptlrpc_request **request, struct md_open_data **mod)
117 CFS_LIST_HEAD(cancels);
118 struct ptlrpc_request *req;
119 struct mdc_rpc_lock *rpc_lock;
120 struct obd_device *obd = exp->exp_obd;
125 LASSERT(op_data != NULL);
127 bits = MDS_INODELOCK_UPDATE;
128 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
129 bits |= MDS_INODELOCK_LOOKUP;
130 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
131 (fid_is_sane(&op_data->op_fid1)) &&
132 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
133 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
134 &cancels, LCK_EX, bits);
135 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
136 &RQF_MDS_REINT_SETATTR);
138 ldlm_lock_list_put(&cancels, l_bl_ast, count);
141 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
142 if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
143 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
145 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
146 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
149 rc = mdc_prep_elc_req(exp, req, &cancels, count);
151 ptlrpc_request_free(req);
155 rpc_lock = obd->u.cli.cl_rpc_lock;
157 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
158 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
159 ", ctime "CFS_TIME_T"\n",
160 LTIME_S(op_data->op_attr.ia_mtime),
161 LTIME_S(op_data->op_attr.ia_ctime));
162 mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
164 ptlrpc_request_set_replen(req);
165 if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
166 req->rq_import->imp_replayable)
168 LASSERT(*mod == NULL);
170 *mod = obd_mod_alloc();
172 DEBUG_REQ(D_ERROR, req, "Can't allocate "
176 req->rq_cb_data = *mod;
177 (*mod)->mod_open_req = req;
178 req->rq_commit_cb = mdc_commit_open;
180 * Take an extra reference on \var mod, it protects \var
181 * mod from being freed on eviction (commit callback is
182 * called despite rq_replay flag).
183 * Will be put on mdc_done_writing().
189 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
191 /* Save the obtained info in the original RPC for the replay case. */
192 if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
193 struct mdt_ioepoch *epoch;
194 struct mdt_body *body;
196 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
197 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
198 LASSERT(epoch != NULL);
199 LASSERT(body != NULL);
200 epoch->handle = body->handle;
201 epoch->ioepoch = body->ioepoch;
202 req->rq_replay_cb = mdc_replay_open;
203 /** bug 3633, open may be committed and estale answer is not error */
204 } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
206 } else if (rc == -ERESTARTSYS) {
210 if (rc && req->rq_commit_cb) {
211 /* Put an extra reference on \var mod on error case. */
213 req->rq_commit_cb(req);
218 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
219 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
220 cfs_cap_t cap_effective, __u64 rdev,
221 struct ptlrpc_request **request)
223 struct ptlrpc_request *req;
226 CFS_LIST_HEAD(cancels);
229 /* For case if upper layer did not alloc fid, do it now. */
230 if (!fid_is_sane(&op_data->op_fid2)) {
232 * mdc_fid_alloc() may return errno 1 in case of switch to new
233 * sequence, handle this.
235 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
237 CERROR("Can't alloc new fid, rc %d\n", rc);
242 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
243 (fid_is_sane(&op_data->op_fid1)))
244 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
246 MDS_INODELOCK_UPDATE);
248 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
249 &RQF_MDS_REINT_CREATE_RMT_ACL);
251 ldlm_lock_list_put(&cancels, l_bl_ast, count);
254 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
255 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
256 op_data->op_namelen + 1);
257 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
258 data && datalen ? datalen : 0);
260 rc = mdc_prep_elc_req(exp, req, &cancels, count);
262 ptlrpc_request_free(req);
267 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
268 * tgt, for symlinks or lov MD data.
270 mdc_create_pack(req, op_data, data, datalen, mode, uid,
271 gid, cap_effective, rdev);
273 ptlrpc_request_set_replen(req);
275 level = LUSTRE_IMP_FULL;
277 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
279 /* Resend if we were told to. */
280 if (rc == -ERESTARTSYS) {
281 level = LUSTRE_IMP_RECOVER;
283 } else if (rc == 0) {
284 struct mdt_body *body;
285 struct lustre_capa *capa;
287 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
289 if (body->valid & OBD_MD_FLMDSCAPA) {
290 capa = req_capsule_server_get(&req->rq_pill,
301 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
302 struct ptlrpc_request **request)
304 CFS_LIST_HEAD(cancels);
305 struct obd_device *obd = class_exp2obd(exp);
306 struct ptlrpc_request *req = *request;
310 LASSERT(req == NULL);
312 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
313 (fid_is_sane(&op_data->op_fid1)) &&
314 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
315 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
317 MDS_INODELOCK_UPDATE);
318 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
319 (fid_is_sane(&op_data->op_fid3)) &&
320 !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
321 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
324 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
325 &RQF_MDS_REINT_UNLINK);
327 ldlm_lock_list_put(&cancels, l_bl_ast, count);
330 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
331 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
332 op_data->op_namelen + 1);
334 rc = mdc_prep_elc_req(exp, req, &cancels, count);
336 ptlrpc_request_free(req);
340 mdc_unlink_pack(req, op_data);
342 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
343 obd->u.cli.cl_max_mds_easize);
344 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
345 obd->u.cli.cl_max_mds_cookiesize);
346 ptlrpc_request_set_replen(req);
350 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
351 if (rc == -ERESTARTSYS)
356 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
357 struct ptlrpc_request **request)
359 CFS_LIST_HEAD(cancels);
360 struct obd_device *obd = exp->exp_obd;
361 struct ptlrpc_request *req;
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_FID1) &&
371 (fid_is_sane(&op_data->op_fid1)))
372 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
374 MDS_INODELOCK_UPDATE);
376 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
378 ldlm_lock_list_put(&cancels, l_bl_ast, count);
381 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
382 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
383 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
384 op_data->op_namelen + 1);
386 rc = mdc_prep_elc_req(exp, req, &cancels, count);
388 ptlrpc_request_free(req);
392 mdc_link_pack(req, op_data);
393 ptlrpc_request_set_replen(req);
395 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
397 if (rc == -ERESTARTSYS)
403 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
404 const char *old, int oldlen, const char *new, int newlen,
405 struct ptlrpc_request **request)
407 CFS_LIST_HEAD(cancels);
408 struct obd_device *obd = exp->exp_obd;
409 struct ptlrpc_request *req;
413 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
414 (fid_is_sane(&op_data->op_fid1)))
415 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
417 MDS_INODELOCK_UPDATE);
418 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
419 (fid_is_sane(&op_data->op_fid2)))
420 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
422 MDS_INODELOCK_UPDATE);
423 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
424 (fid_is_sane(&op_data->op_fid3)))
425 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
427 MDS_INODELOCK_LOOKUP);
428 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
429 (fid_is_sane(&op_data->op_fid4)))
430 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
434 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
435 &RQF_MDS_REINT_RENAME);
437 ldlm_lock_list_put(&cancels, l_bl_ast, count);
441 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
442 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
443 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
444 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
446 rc = mdc_prep_elc_req(exp, req, &cancels, count);
448 ptlrpc_request_free(req);
452 if (exp_connect_cancelset(exp) && req)
453 ldlm_cli_cancel_list(&cancels, count, req, 0);
455 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
457 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
458 obd->u.cli.cl_max_mds_easize);
459 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
460 obd->u.cli.cl_max_mds_cookiesize);
461 ptlrpc_request_set_replen(req);
463 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
465 if (rc == -ERESTARTSYS)