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,
95 /* Initialize ibits lock policy. */
96 policy.l_inodebits.bits = bits;
97 count = ldlm_cancel_resource_local(res, cancels, &policy,
99 ldlm_resource_putref(res);
103 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
104 struct list_head *cancels, int count)
106 return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
110 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
111 * should take the normal semaphore and go to the normal portal.
113 * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
114 * magic open-path setattr that should take the setattr semaphore and
115 * go to the setattr portal. */
116 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
117 void *ea, int ealen, void *ea2, int ea2len,
118 struct ptlrpc_request **request, struct md_open_data **mod)
120 CFS_LIST_HEAD(cancels);
121 struct ptlrpc_request *req;
122 struct mdc_rpc_lock *rpc_lock;
123 struct obd_device *obd = exp->exp_obd;
128 LASSERT(op_data != NULL);
130 bits = MDS_INODELOCK_UPDATE;
131 if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
132 bits |= MDS_INODELOCK_LOOKUP;
133 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
134 (fid_is_sane(&op_data->op_fid1)))
135 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
136 &cancels, LCK_EX, bits);
137 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
138 &RQF_MDS_REINT_SETATTR);
140 ldlm_lock_list_put(&cancels, l_bl_ast, count);
143 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
144 if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
145 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
147 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
148 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
151 rc = mdc_prep_elc_req(exp, req, &cancels, count);
153 ptlrpc_request_free(req);
157 if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
158 req->rq_request_portal = MDS_SETATTR_PORTAL;
159 ptlrpc_at_set_req_timeout(req);
160 rpc_lock = obd->u.cli.cl_setattr_lock;
162 rpc_lock = obd->u.cli.cl_rpc_lock;
165 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
166 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
167 ", ctime "CFS_TIME_T"\n",
168 LTIME_S(op_data->op_attr.ia_mtime),
169 LTIME_S(op_data->op_attr.ia_ctime));
170 mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
172 ptlrpc_request_set_replen(req);
173 if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
174 req->rq_import->imp_replayable)
176 LASSERT(*mod == NULL);
180 DEBUG_REQ(D_ERROR, req, "Can't allocate "
183 CFS_INIT_LIST_HEAD(&(*mod)->mod_replay_list);
187 req->rq_cb_data = *mod;
188 req->rq_commit_cb = mdc_commit_delayed;
189 list_add_tail(&req->rq_mod_list, &(*mod)->mod_replay_list);
190 /* This is not the last request in sequence for truncate. */
191 if (op_data->op_flags & MF_EPOCH_OPEN)
194 req->rq_sequence = 1;
197 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
199 if (rc == -ERESTARTSYS)
201 if (rc && req->rq_commit_cb)
202 req->rq_commit_cb(req);
206 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
207 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
208 __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
210 struct ptlrpc_request *req;
213 CFS_LIST_HEAD(cancels);
216 /* For case if upper layer did not alloc fid, do it now. */
217 if (!fid_is_sane(&op_data->op_fid2)) {
219 * mdc_fid_alloc() may return errno 1 in case of switch to new
220 * sequence, handle this.
222 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
224 CERROR("Can't alloc new fid, rc %d\n", rc);
229 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
230 (fid_is_sane(&op_data->op_fid1)))
231 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
233 MDS_INODELOCK_UPDATE);
235 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
236 &RQF_MDS_REINT_CREATE_RMT_ACL);
238 ldlm_lock_list_put(&cancels, l_bl_ast, count);
241 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
242 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
243 op_data->op_namelen + 1);
244 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
245 data && datalen ? datalen : 0);
247 rc = mdc_prep_elc_req(exp, req, &cancels, count);
249 ptlrpc_request_free(req);
254 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
255 * tgt, for symlinks or lov MD data.
257 mdc_create_pack(req, op_data, data, datalen, mode, uid,
258 gid, cap_effective, rdev);
260 ptlrpc_request_set_replen(req);
262 level = LUSTRE_IMP_FULL;
264 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
266 /* Resend if we were told to. */
267 if (rc == -ERESTARTSYS) {
268 level = LUSTRE_IMP_RECOVER;
270 } else if (rc == 0) {
271 struct mdt_body *body;
272 struct lustre_capa *capa;
274 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
276 if (body->valid & OBD_MD_FLMDSCAPA) {
277 capa = req_capsule_server_get(&req->rq_pill,
288 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
289 struct ptlrpc_request **request)
291 CFS_LIST_HEAD(cancels);
292 struct obd_device *obd = class_exp2obd(exp);
293 struct ptlrpc_request *req = *request;
297 LASSERT(req == NULL);
299 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
300 (fid_is_sane(&op_data->op_fid1)))
301 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
303 MDS_INODELOCK_UPDATE);
304 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
305 (fid_is_sane(&op_data->op_fid3)))
306 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
309 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
310 &RQF_MDS_REINT_UNLINK);
312 ldlm_lock_list_put(&cancels, l_bl_ast, count);
315 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
316 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
317 op_data->op_namelen + 1);
319 rc = mdc_prep_elc_req(exp, req, &cancels, count);
321 ptlrpc_request_free(req);
325 mdc_unlink_pack(req, op_data);
327 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
328 obd->u.cli.cl_max_mds_easize);
329 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
330 obd->u.cli.cl_max_mds_cookiesize);
331 ptlrpc_request_set_replen(req);
335 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
336 if (rc == -ERESTARTSYS)
341 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
342 struct ptlrpc_request **request)
344 CFS_LIST_HEAD(cancels);
345 struct obd_device *obd = exp->exp_obd;
346 struct ptlrpc_request *req;
350 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
351 (fid_is_sane(&op_data->op_fid2)))
352 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
354 MDS_INODELOCK_UPDATE);
355 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
356 (fid_is_sane(&op_data->op_fid1)))
357 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
359 MDS_INODELOCK_UPDATE);
361 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
363 ldlm_lock_list_put(&cancels, l_bl_ast, count);
366 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
367 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
368 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
369 op_data->op_namelen + 1);
371 rc = mdc_prep_elc_req(exp, req, &cancels, count);
373 ptlrpc_request_free(req);
377 mdc_link_pack(req, op_data);
378 ptlrpc_request_set_replen(req);
380 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
382 if (rc == -ERESTARTSYS)
388 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
389 const char *old, int oldlen, const char *new, int newlen,
390 struct ptlrpc_request **request)
392 CFS_LIST_HEAD(cancels);
393 struct obd_device *obd = exp->exp_obd;
394 struct ptlrpc_request *req;
398 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
399 (fid_is_sane(&op_data->op_fid1)))
400 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
402 MDS_INODELOCK_UPDATE);
403 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
404 (fid_is_sane(&op_data->op_fid2)))
405 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
407 MDS_INODELOCK_UPDATE);
408 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
409 (fid_is_sane(&op_data->op_fid3)))
410 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
412 MDS_INODELOCK_LOOKUP);
413 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
414 (fid_is_sane(&op_data->op_fid4)))
415 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
419 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
420 &RQF_MDS_REINT_RENAME);
422 ldlm_lock_list_put(&cancels, l_bl_ast, count);
426 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
427 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
428 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
429 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
431 rc = mdc_prep_elc_req(exp, req, &cancels, count);
433 ptlrpc_request_free(req);
437 if (exp_connect_cancelset(exp) && req)
438 ldlm_cli_cancel_list(&cancels, count, req, 0);
440 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
442 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
443 obd->u.cli.cl_max_mds_easize);
444 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
445 obd->u.cli.cl_max_mds_cookiesize);
446 ptlrpc_request_set_replen(req);
448 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
450 if (rc == -ERESTARTSYS)