Whamcloud - gitweb
b=14538
[fs/lustre-release.git] / lustre / mdc / mdc_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 #ifndef AUTOCONF_INCLUDED
32 # include <linux/config.h>
33 #endif
34 # include <linux/module.h>
35 # include <linux/kernel.h>
36 #else
37 # include <liblustre.h>
38 #endif
39
40 #include <obd_class.h>
41 #include "mdc_internal.h"
42
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)
46 {
47         int rc;
48
49         request->rq_send_state = level;
50
51         mdc_get_rpc_lock(rpc_lock, NULL);
52         rc = ptlrpc_queue_wait(request);
53         mdc_put_rpc_lock(rpc_lock, NULL);
54         if (rc)
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");
60                 rc = -EPROTO;
61         }
62         return rc;
63 }
64
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,
70                             __u64 bits)
71 {
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}};
76         int count;
77         ENTRY;
78
79         if (res == NULL)
80                 RETURN(0);
81
82         /* Initialize ibits lock policy. */
83         policy.l_inodebits.bits = bits;
84         count = ldlm_cancel_resource_local(res, cancels, &policy,
85                                            mode, 0, 0, NULL);
86         ldlm_resource_putref(res);
87         RETURN(count);
88 }
89
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)
93 {
94         return ldlm_prep_elc_req(exp, LUSTRE_MDS_VERSION, MDS_REINT,
95                                  bufcount, size, off, 0, cancels, count);
96 }
97
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.
100  *
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)
107 {
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;
117         __u64 bits;
118         ENTRY;
119
120         LASSERT(iattr != NULL);
121
122         if (ealen > 0) {
123                 bufcount++;
124                 if (ea2len > 0)
125                         bufcount++;
126         }
127
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))
134                 bufcount = 5;
135         req = mdc_prep_elc_req(exp, bufcount, size,
136                                REQ_REC_OFF + 3, &cancels, count);
137         if (req == NULL)
138                 RETURN(-ENOMEM);
139
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;
144         } else {
145                 rpc_lock = obd->u.cli.cl_rpc_lock;
146         }
147
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);
153
154         size[REPLY_REC_OFF] = sizeof(struct mds_body);
155         ptlrpc_req_set_repsize(req, 2, size);
156
157         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
158         *request = req;
159         if (rc == -ERESTARTSYS)
160                 rc = 0;
161
162         RETURN(rc);
163 }
164
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)
168 {
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) };
176         int count;
177         ENTRY;
178
179         if (data && datalen) {
180                 size[bufcount] = datalen;
181                 bufcount++;
182         }
183
184         count = mdc_resource_get_unused(exp, &op_data->fid1, &cancels,
185                                         LCK_EX, MDS_INODELOCK_UPDATE);
186         if (exp_connect_cancelset(exp))
187                 bufcount = 5;
188         req = mdc_prep_elc_req(exp, bufcount, size,
189                                REQ_REC_OFF + 3, &cancels, count);
190         if (req == NULL)
191                 RETURN(-ENOMEM);
192
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);
197
198         size[REPLY_REC_OFF] = sizeof(struct mds_body);
199         ptlrpc_req_set_repsize(req, 2, size);
200
201         level = LUSTRE_IMP_FULL;
202  resend:
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;
207                 goto resend;
208         }
209
210         if (!rc)
211                 mdc_store_inode_generation(req, REQ_REC_OFF, REPLY_REC_OFF);
212
213         *request = req;
214         RETURN(rc);
215 }
216
217 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *op_data,
218                struct ptlrpc_request **request)
219 {
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;
227         ENTRY;
228
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))
236                 bufcount = 4;
237         req = mdc_prep_elc_req(exp, bufcount, size,
238                                REQ_REC_OFF + 2, &cancels, count);
239         if (req == NULL)
240                 RETURN(-ENOMEM);
241         *request = req;
242
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);
247
248         mdc_unlink_pack(req, REQ_REC_OFF, op_data);
249
250         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
251         if (rc == -ERESTARTSYS)
252                 rc = 0;
253         RETURN(rc);
254 }
255
256 int mdc_link(struct obd_export *exp, struct mdc_op_data *op_data,
257              struct ptlrpc_request **request)
258 {
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;
266         ENTRY;
267
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))
273                 bufcount = 4;
274         req = mdc_prep_elc_req(exp, bufcount, size,
275                                REQ_REC_OFF + 2, &cancels, count);
276         if (req == NULL)
277                 RETURN(-ENOMEM);
278
279         mdc_link_pack(req, REQ_REC_OFF, op_data);
280
281         size[REPLY_REC_OFF] = sizeof(struct mds_body);
282         ptlrpc_req_set_repsize(req, 2, size);
283
284         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
285         *request = req;
286         if (rc == -ERESTARTSYS)
287                 rc = 0;
288
289         RETURN(rc);
290 }
291
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)
295 {
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;
303         ENTRY;
304
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))
316                 bufcount = 5;
317         req = mdc_prep_elc_req(exp, bufcount, size,
318                                REQ_REC_OFF + 3, &cancels, count);
319         if (req == NULL)
320                 RETURN(-ENOMEM);
321
322         mdc_rename_pack(req, REQ_REC_OFF, op_data, old, oldlen, new, newlen);
323
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);
328
329         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
330         *request = req;
331         if (rc == -ERESTARTSYS)
332                 rc = 0;
333
334         RETURN(rc);
335 }