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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_MDC
43 #ifndef AUTOCONF_INCLUDED
44 # include <linux/config.h>
46 # include <linux/module.h>
47 # include <linux/kernel.h>
49 # include <liblustre.h>
52 #include <obd_class.h>
53 #include "mdc_internal.h"
54 #include <lustre_fid.h>
56 /* mdc_setattr does its own semaphore handling */
57 static int mdc_reint(struct ptlrpc_request *request,
58 struct mdc_rpc_lock *rpc_lock,
63 request->rq_send_state = level;
65 mdc_get_rpc_lock(rpc_lock, NULL);
66 rc = ptlrpc_queue_wait(request);
67 mdc_put_rpc_lock(rpc_lock, NULL);
69 CDEBUG(D_INFO, "error in handling %d\n", rc);
70 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
76 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
77 * found by @fid. Found locks are added into @cancel list. Returns the amount of
78 * locks added to @cancels list. */
79 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
80 struct list_head *cancels, ldlm_mode_t mode,
83 ldlm_policy_data_t policy = {{0}};
84 struct ldlm_res_id res_id;
85 struct ldlm_resource *res;
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 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
105 struct list_head *cancels, int count)
107 return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
111 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
112 * should take the normal semaphore and go to the normal portal.
114 * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
115 * magic open-path setattr that should take the setattr semaphore and
116 * go to the setattr portal. */
117 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
118 void *ea, int ealen, void *ea2, int ea2len,
119 struct ptlrpc_request **request, struct md_open_data **mod)
121 CFS_LIST_HEAD(cancels);
122 struct ptlrpc_request *req;
123 struct mdc_rpc_lock *rpc_lock;
124 struct obd_device *obd = exp->exp_obd;
129 LASSERT(op_data != NULL);
131 bits = MDS_INODELOCK_UPDATE;
132 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
133 bits |= MDS_INODELOCK_LOOKUP;
134 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
135 (fid_is_sane(&op_data->op_fid1)))
136 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
137 &cancels, LCK_EX, bits);
138 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
139 &RQF_MDS_REINT_SETATTR);
141 ldlm_lock_list_put(&cancels, l_bl_ast, count);
144 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
145 if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
146 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
148 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
149 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
152 rc = mdc_prep_elc_req(exp, req, &cancels, count);
154 ptlrpc_request_free(req);
158 if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
159 req->rq_request_portal = MDS_SETATTR_PORTAL;
160 ptlrpc_at_set_req_timeout(req);
161 rpc_lock = obd->u.cli.cl_setattr_lock;
163 rpc_lock = obd->u.cli.cl_rpc_lock;
166 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
167 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
168 ", ctime "CFS_TIME_T"\n",
169 LTIME_S(op_data->op_attr.ia_mtime),
170 LTIME_S(op_data->op_attr.ia_ctime));
171 mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
173 ptlrpc_request_set_replen(req);
174 if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
175 req->rq_import->imp_replayable)
177 LASSERT(*mod == NULL);
181 DEBUG_REQ(D_ERROR, req, "Can't allocate "
185 req->rq_cb_data = *mod;
186 (*mod)->mod_open_req = req;
187 req->rq_commit_cb = mdc_commit_open;
191 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
193 /* Save the obtained info in the original RPC for the replay case. */
194 if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
195 struct mdt_epoch *epoch;
196 struct mdt_body *body;
198 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
199 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
200 LASSERT(epoch != NULL);
201 LASSERT(body != NULL);
202 epoch->handle = body->handle;
203 epoch->ioepoch = body->ioepoch;
204 req->rq_replay_cb = mdc_replay_open;
205 /** bug 3633, open may be committed and estale answer is not error */
206 } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
208 } else if (rc == -ERESTARTSYS) {
212 if (rc && req->rq_commit_cb)
213 req->rq_commit_cb(req);
217 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
218 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
219 cfs_cap_t cap_effective, __u64 rdev,
220 struct ptlrpc_request **request)
222 struct ptlrpc_request *req;
225 CFS_LIST_HEAD(cancels);
228 /* For case if upper layer did not alloc fid, do it now. */
229 if (!fid_is_sane(&op_data->op_fid2)) {
231 * mdc_fid_alloc() may return errno 1 in case of switch to new
232 * sequence, handle this.
234 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
236 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, &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 level = LUSTRE_IMP_FULL;
276 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
278 /* Resend if we were told to. */
279 if (rc == -ERESTARTSYS) {
280 level = LUSTRE_IMP_RECOVER;
282 } else if (rc == 0) {
283 struct mdt_body *body;
284 struct lustre_capa *capa;
286 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
288 if (body->valid & OBD_MD_FLMDSCAPA) {
289 capa = req_capsule_server_get(&req->rq_pill,
300 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
301 struct ptlrpc_request **request)
303 CFS_LIST_HEAD(cancels);
304 struct obd_device *obd = class_exp2obd(exp);
305 struct ptlrpc_request *req = *request;
309 LASSERT(req == NULL);
311 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
312 (fid_is_sane(&op_data->op_fid1)))
313 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
315 MDS_INODELOCK_UPDATE);
316 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
317 (fid_is_sane(&op_data->op_fid3)))
318 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
321 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
322 &RQF_MDS_REINT_UNLINK);
324 ldlm_lock_list_put(&cancels, l_bl_ast, count);
327 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
328 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
329 op_data->op_namelen + 1);
331 rc = mdc_prep_elc_req(exp, req, &cancels, count);
333 ptlrpc_request_free(req);
337 mdc_unlink_pack(req, op_data);
339 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
340 obd->u.cli.cl_max_mds_easize);
341 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
342 obd->u.cli.cl_max_mds_cookiesize);
343 ptlrpc_request_set_replen(req);
347 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
348 if (rc == -ERESTARTSYS)
353 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
354 struct ptlrpc_request **request)
356 CFS_LIST_HEAD(cancels);
357 struct obd_device *obd = exp->exp_obd;
358 struct ptlrpc_request *req;
362 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
363 (fid_is_sane(&op_data->op_fid2)))
364 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
366 MDS_INODELOCK_UPDATE);
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);
373 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
375 ldlm_lock_list_put(&cancels, l_bl_ast, count);
378 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
379 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
380 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
381 op_data->op_namelen + 1);
383 rc = mdc_prep_elc_req(exp, req, &cancels, count);
385 ptlrpc_request_free(req);
389 mdc_link_pack(req, op_data);
390 ptlrpc_request_set_replen(req);
392 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
394 if (rc == -ERESTARTSYS)
400 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
401 const char *old, int oldlen, const char *new, int newlen,
402 struct ptlrpc_request **request)
404 CFS_LIST_HEAD(cancels);
405 struct obd_device *obd = exp->exp_obd;
406 struct ptlrpc_request *req;
410 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
411 (fid_is_sane(&op_data->op_fid1)))
412 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
414 MDS_INODELOCK_UPDATE);
415 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
416 (fid_is_sane(&op_data->op_fid2)))
417 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
419 MDS_INODELOCK_UPDATE);
420 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
421 (fid_is_sane(&op_data->op_fid3)))
422 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
424 MDS_INODELOCK_LOOKUP);
425 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
426 (fid_is_sane(&op_data->op_fid4)))
427 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
431 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
432 &RQF_MDS_REINT_RENAME);
434 ldlm_lock_list_put(&cancels, l_bl_ast, count);
438 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
439 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
440 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
441 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
443 rc = mdc_prep_elc_req(exp, req, &cancels, count);
445 ptlrpc_request_free(req);
449 if (exp_connect_cancelset(exp) && req)
450 ldlm_cli_cancel_list(&cancels, count, req, 0);
452 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
454 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
455 obd->u.cli.cl_max_mds_easize);
456 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
457 obd->u.cli.cl_max_mds_cookiesize);
458 ptlrpc_request_set_replen(req);
460 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
462 if (rc == -ERESTARTSYS)