1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_MDC
31 #ifndef AUTOCONF_INCLUDED
32 # include <linux/config.h>
34 # include <linux/module.h>
35 # include <linux/kernel.h>
37 # include <liblustre.h>
40 #include <obd_class.h>
41 #include "mdc_internal.h"
42 #include <lustre_fid.h>
44 /* mdc_setattr does its own semaphore handling */
45 static int mdc_reint(struct ptlrpc_request *request,
46 struct mdc_rpc_lock *rpc_lock,
51 request->rq_send_state = level;
53 mdc_get_rpc_lock(rpc_lock, NULL);
54 rc = ptlrpc_queue_wait(request);
55 mdc_put_rpc_lock(rpc_lock, NULL);
57 CDEBUG(D_INFO, "error in handling %d\n", rc);
58 else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
64 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
65 * found by @fid. Found locks are added into @cancel list. Returns the amount of
66 * locks added to @cancels list. */
67 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
68 struct list_head *cancels, ldlm_mode_t mode,
71 ldlm_policy_data_t policy = {{0}};
72 struct ldlm_res_id res_id;
73 struct ldlm_resource *res;
77 fid_build_reg_res_name(fid, &res_id);
78 res = ldlm_resource_get(exp->exp_obd->obd_namespace,
83 /* Initialize ibits lock policy. */
84 policy.l_inodebits.bits = bits;
85 count = ldlm_cancel_resource_local(res, cancels, &policy,
87 ldlm_resource_putref(res);
91 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
92 struct list_head *cancels, int count)
94 return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
98 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
99 * should take the normal semaphore and go to the normal portal.
101 * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
102 * magic open-path setattr that should take the setattr semaphore and
103 * go to the setattr portal. */
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 CFS_LIST_HEAD(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 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
124 &cancels, LCK_EX, bits);
125 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
126 &RQF_MDS_REINT_SETATTR);
128 ldlm_lock_list_put(&cancels, l_bl_ast, count);
131 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
132 if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
133 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
135 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
136 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
139 rc = mdc_prep_elc_req(exp, req, &cancels, count);
141 ldlm_lock_list_put(&cancels, l_bl_ast, count);
142 ptlrpc_request_free(req);
146 if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
147 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
148 rpc_lock = obd->u.cli.cl_setattr_lock;
150 rpc_lock = obd->u.cli.cl_rpc_lock;
153 if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
154 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\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);
167 DEBUG_REQ(D_ERROR, req, "Can't allocate "
170 CFS_INIT_LIST_HEAD(&(*mod)->mod_replay_list);
174 req->rq_cb_data = *mod;
175 req->rq_commit_cb = mdc_commit_delayed;
176 list_add_tail(&req->rq_mod_list, &(*mod)->mod_replay_list);
177 /* This is not the last request in sequence for truncate. */
178 if (op_data->op_flags & MF_EPOCH_OPEN)
181 req->rq_sequence = 1;
184 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
186 if (rc == -ERESTARTSYS)
188 if (rc && req->rq_commit_cb)
189 req->rq_commit_cb(req);
193 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
194 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
195 __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
197 struct ptlrpc_request *req;
200 CFS_LIST_HEAD(cancels);
203 /* For case if upper layer did not alloc fid, do it now. */
204 if (!fid_is_sane(&op_data->op_fid2)) {
206 * mdc_fid_alloc() may return errno 1 in case of switch to new
207 * sequence, handle this.
209 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
211 CERROR("Can't alloc new fid, rc %d\n", rc);
216 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
217 (fid_is_sane(&op_data->op_fid1)))
218 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
220 MDS_INODELOCK_UPDATE);
222 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
223 &RQF_MDS_REINT_CREATE_RMT_ACL);
225 ldlm_lock_list_put(&cancels, l_bl_ast, count);
228 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
229 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
230 op_data->op_namelen + 1);
231 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
232 data && datalen ? datalen : 0);
234 rc = mdc_prep_elc_req(exp, req, &cancels, count);
236 ldlm_lock_list_put(&cancels, l_bl_ast, count);
237 ptlrpc_request_free(req);
242 * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
243 * tgt, for symlinks or lov MD data.
245 mdc_create_pack(req, op_data, data, datalen, mode, uid,
246 gid, cap_effective, rdev);
248 ptlrpc_request_set_replen(req);
250 level = LUSTRE_IMP_FULL;
252 rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
254 /* Resend if we were told to. */
255 if (rc == -ERESTARTSYS) {
256 level = LUSTRE_IMP_RECOVER;
258 } else if (rc == 0) {
259 struct mdt_body *body;
260 struct lustre_capa *capa;
262 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
264 if (body->valid & OBD_MD_FLMDSCAPA) {
265 capa = req_capsule_server_get(&req->rq_pill,
276 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
277 struct ptlrpc_request **request)
279 CFS_LIST_HEAD(cancels);
280 struct obd_device *obd = class_exp2obd(exp);
281 struct ptlrpc_request *req = *request;
285 LASSERT(req == NULL);
287 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
288 (fid_is_sane(&op_data->op_fid1)))
289 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
291 MDS_INODELOCK_UPDATE);
292 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
293 (fid_is_sane(&op_data->op_fid3)))
294 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
297 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
298 &RQF_MDS_REINT_UNLINK);
300 ldlm_lock_list_put(&cancels, l_bl_ast, count);
303 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
304 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
305 op_data->op_namelen + 1);
307 rc = mdc_prep_elc_req(exp, req, &cancels, count);
309 ldlm_lock_list_put(&cancels, l_bl_ast, count);
310 ptlrpc_request_free(req);
314 mdc_unlink_pack(req, op_data);
316 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
317 obd->u.cli.cl_max_mds_easize);
318 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
319 obd->u.cli.cl_max_mds_cookiesize);
320 ptlrpc_request_set_replen(req);
324 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
325 if (rc == -ERESTARTSYS)
330 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
331 struct ptlrpc_request **request)
333 CFS_LIST_HEAD(cancels);
334 struct obd_device *obd = exp->exp_obd;
335 struct ptlrpc_request *req;
339 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
340 (fid_is_sane(&op_data->op_fid2)))
341 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
343 MDS_INODELOCK_UPDATE);
344 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
345 (fid_is_sane(&op_data->op_fid1)))
346 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
348 MDS_INODELOCK_UPDATE);
350 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
352 ldlm_lock_list_put(&cancels, l_bl_ast, count);
355 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
356 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
357 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
358 op_data->op_namelen + 1);
360 rc = mdc_prep_elc_req(exp, req, &cancels, count);
362 ldlm_lock_list_put(&cancels, l_bl_ast, count);
363 ptlrpc_request_free(req);
367 mdc_link_pack(req, op_data);
368 ptlrpc_request_set_replen(req);
370 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
372 if (rc == -ERESTARTSYS)
378 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
379 const char *old, int oldlen, const char *new, int newlen,
380 struct ptlrpc_request **request)
382 CFS_LIST_HEAD(cancels);
383 struct obd_device *obd = exp->exp_obd;
384 struct ptlrpc_request *req;
388 if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
389 (fid_is_sane(&op_data->op_fid1)))
390 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
392 MDS_INODELOCK_UPDATE);
393 if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
394 (fid_is_sane(&op_data->op_fid2)))
395 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
397 MDS_INODELOCK_UPDATE);
398 if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
399 (fid_is_sane(&op_data->op_fid3)))
400 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
402 MDS_INODELOCK_LOOKUP);
403 if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
404 (fid_is_sane(&op_data->op_fid4)))
405 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
409 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
410 &RQF_MDS_REINT_RENAME);
412 ldlm_lock_list_put(&cancels, l_bl_ast, count);
416 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
417 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
418 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
419 req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
421 rc = mdc_prep_elc_req(exp, req, &cancels, count);
423 ldlm_lock_list_put(&cancels, l_bl_ast, count);
424 ptlrpc_request_free(req);
428 if (exp_connect_cancelset(exp) && req)
429 ldlm_cli_cancel_list(&cancels, count, req, 0);
431 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
433 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
434 obd->u.cli.cl_max_mds_easize);
435 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
436 obd->u.cli.cl_max_mds_cookiesize);
437 ptlrpc_request_set_replen(req);
439 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
441 if (rc == -ERESTARTSYS)