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"
43 /* mdc_setattr does its own semaphore handling */
44 static int mdc_reint(struct ptlrpc_request *request,
45 struct mdc_rpc_lock *rpc_lock, int level)
49 request->rq_send_state = level;
51 mdc_get_rpc_lock(rpc_lock, NULL);
52 rc = ptlrpc_queue_wait(request);
53 mdc_put_rpc_lock(rpc_lock, NULL);
55 CDEBUG(D_INFO, "error in handling %d\n", rc);
56 else if (!lustre_swab_repbuf(request, REPLY_REC_OFF,
57 sizeof(struct mds_body),
58 lustre_swab_mds_body)) {
59 CERROR ("Can't unpack mds_body\n");
65 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
66 * found by @fid. Found locks are added into @cancel list. Returns the amount of
67 * locks added to @cancels list. */
68 int mdc_resource_get_unused(struct obd_export *exp, struct ll_fid *fid,
69 struct list_head *cancels, ldlm_mode_t mode,
72 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
73 struct ldlm_res_id res_id = { .name = {fid->id, fid->generation} };
74 struct ldlm_resource *res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
75 ldlm_policy_data_t policy = {{0}};
82 /* Initialize ibits lock policy. */
83 policy.l_inodebits.bits = bits;
84 count = ldlm_cancel_resource_local(res, cancels, &policy,
86 ldlm_resource_putref(res);
90 struct ptlrpc_request *mdc_prep_elc_req(struct obd_export *exp,
91 int bufcount, int *size, int off,
92 struct list_head *cancels, int count)
94 return ldlm_prep_elc_req(exp, LUSTRE_MDS_VERSION, MDS_REINT,
95 bufcount, size, off, 0, cancels, count);
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 mdc_op_data *op_data,
105 struct iattr *iattr, void *ea, int ealen, void *ea2, int ea2len,
106 struct ptlrpc_request **request)
108 CFS_LIST_HEAD(cancels);
109 struct ptlrpc_request *req;
110 struct mds_rec_setattr *rec;
111 struct mdc_rpc_lock *rpc_lock;
112 struct obd_device *obd = exp->exp_obd;
113 int size[5] = { sizeof(struct ptlrpc_body),
114 sizeof(*rec), ealen, ea2len,
115 sizeof(struct ldlm_request) };
116 int count, bufcount = 2, rc;
120 LASSERT(iattr != NULL);
128 bits = MDS_INODELOCK_UPDATE;
129 if (iattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
130 bits |= MDS_INODELOCK_LOOKUP;
131 count = mdc_resource_get_unused(exp, &op_data->fid1,
132 &cancels, LCK_EX, bits);
133 if (exp_connect_cancelset(exp))
135 req = mdc_prep_elc_req(exp, bufcount, size,
136 REQ_REC_OFF + 3, &cancels, count);
140 if (iattr->ia_valid & ATTR_FROM_OPEN) {
141 req->rq_request_portal = MDS_SETATTR_PORTAL;
142 ptlrpc_at_set_req_timeout(req);
143 rpc_lock = obd->u.cli.cl_setattr_lock;
145 rpc_lock = obd->u.cli.cl_rpc_lock;
148 if (iattr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
149 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
150 LTIME_S(iattr->ia_mtime), LTIME_S(iattr->ia_ctime));
151 mdc_setattr_pack(req, REQ_REC_OFF, op_data, iattr,
152 ea, ealen, ea2, ea2len);
154 size[REPLY_REC_OFF] = sizeof(struct mds_body);
155 ptlrpc_req_set_repsize(req, 2, size);
157 rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
159 if (rc == -ERESTARTSYS)
165 int mdc_create(struct obd_export *exp, struct mdc_op_data *op_data,
166 const void *data, int datalen, int mode, __u32 uid, __u32 gid,
167 __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
169 CFS_LIST_HEAD(cancels);
170 struct obd_device *obd = exp->exp_obd;
171 struct ptlrpc_request *req;
172 int level, bufcount = 3, rc;
173 int size[5] = { sizeof(struct ptlrpc_body),
174 sizeof(struct mds_rec_create),
175 op_data->namelen + 1, 0, sizeof(struct ldlm_request) };
179 if (data && datalen) {
180 size[bufcount] = datalen;
184 count = mdc_resource_get_unused(exp, &op_data->fid1, &cancels,
185 LCK_EX, MDS_INODELOCK_UPDATE);
186 if (exp_connect_cancelset(exp))
188 req = mdc_prep_elc_req(exp, bufcount, size,
189 REQ_REC_OFF + 3, &cancels, count);
193 /* mdc_create_pack fills msg->bufs[1] with name
194 * and msg->bufs[2] with tgt, for symlinks or lov MD data */
195 mdc_create_pack(req, REQ_REC_OFF, op_data, data, datalen, mode, uid,
196 gid, cap_effective, rdev);
198 size[REPLY_REC_OFF] = sizeof(struct mds_body);
199 ptlrpc_req_set_repsize(req, 2, size);
201 level = LUSTRE_IMP_FULL;
203 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
204 /* Resend if we were told to. */
205 if (rc == -ERESTARTSYS) {
206 level = LUSTRE_IMP_RECOVER;
211 mdc_store_inode_generation(req, REQ_REC_OFF, REPLY_REC_OFF);
217 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *op_data,
218 struct ptlrpc_request **request)
220 CFS_LIST_HEAD(cancels);
221 struct obd_device *obd = class_exp2obd(exp);
222 struct ptlrpc_request *req = *request;
223 int size[4] = { sizeof(struct ptlrpc_body),
224 sizeof(struct mds_rec_unlink),
225 op_data->namelen + 1, sizeof(struct ldlm_request) };
226 int count, rc, bufcount = 3;
229 LASSERT(req == NULL);
230 count = mdc_resource_get_unused(exp, &op_data->fid1, &cancels,
231 LCK_EX, MDS_INODELOCK_UPDATE);
232 if (op_data->fid3.id)
233 count += mdc_resource_get_unused(exp, &op_data->fid3, &cancels,
234 LCK_EX, MDS_INODELOCK_FULL);
235 if (exp_connect_cancelset(exp))
237 req = mdc_prep_elc_req(exp, bufcount, size,
238 REQ_REC_OFF + 2, &cancels, count);
243 size[REPLY_REC_OFF] = sizeof(struct mds_body);
244 size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
245 size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
246 ptlrpc_req_set_repsize(req, 4, size);
248 mdc_unlink_pack(req, REQ_REC_OFF, op_data);
250 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
251 if (rc == -ERESTARTSYS)
256 int mdc_link(struct obd_export *exp, struct mdc_op_data *op_data,
257 struct ptlrpc_request **request)
259 CFS_LIST_HEAD(cancels);
260 struct obd_device *obd = exp->exp_obd;
261 struct ptlrpc_request *req;
262 int size[4] = { sizeof(struct ptlrpc_body),
263 sizeof(struct mds_rec_link),
264 op_data->namelen + 1, sizeof(struct ldlm_request) };
265 int count, rc, bufcount = 3;
268 count = mdc_resource_get_unused(exp, &op_data->fid1, &cancels,
269 LCK_EX, MDS_INODELOCK_UPDATE);
270 count += mdc_resource_get_unused(exp, &op_data->fid2, &cancels,
271 LCK_EX, MDS_INODELOCK_UPDATE);
272 if (exp_connect_cancelset(exp))
274 req = mdc_prep_elc_req(exp, bufcount, size,
275 REQ_REC_OFF + 2, &cancels, count);
279 mdc_link_pack(req, REQ_REC_OFF, op_data);
281 size[REPLY_REC_OFF] = sizeof(struct mds_body);
282 ptlrpc_req_set_repsize(req, 2, size);
284 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
286 if (rc == -ERESTARTSYS)
292 int mdc_rename(struct obd_export *exp, struct mdc_op_data *op_data,
293 const char *old, int oldlen, const char *new, int newlen,
294 struct ptlrpc_request **request)
296 CFS_LIST_HEAD(cancels);
297 struct obd_device *obd = exp->exp_obd;
298 struct ptlrpc_request *req;
299 int size[5] = { sizeof(struct ptlrpc_body),
300 sizeof(struct mds_rec_rename),
301 oldlen + 1, newlen + 1, sizeof(struct ldlm_request) };
302 int count, rc, bufcount = 4;
305 count = mdc_resource_get_unused(exp, &op_data->fid1, &cancels,
306 LCK_EX, MDS_INODELOCK_UPDATE);
307 count += mdc_resource_get_unused(exp, &op_data->fid2, &cancels,
308 LCK_EX, MDS_INODELOCK_UPDATE);
309 if (op_data->fid3.id)
310 count += mdc_resource_get_unused(exp, &op_data->fid3, &cancels,
311 LCK_EX, MDS_INODELOCK_LOOKUP);
312 if (op_data->fid4.id)
313 count += mdc_resource_get_unused(exp, &op_data->fid4, &cancels,
314 LCK_EX, MDS_INODELOCK_FULL);
315 if (exp_connect_cancelset(exp))
317 req = mdc_prep_elc_req(exp, bufcount, size,
318 REQ_REC_OFF + 3, &cancels, count);
322 mdc_rename_pack(req, REQ_REC_OFF, op_data, old, oldlen, new, newlen);
324 size[REPLY_REC_OFF] = sizeof(struct mds_body);
325 size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
326 size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
327 ptlrpc_req_set_repsize(req, 4, size);
329 rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
331 if (rc == -ERESTARTSYS)