Whamcloud - gitweb
Branch HEAD
[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 #include <lustre_fid.h>
43
44 /* mdc_setattr does its own semaphore handling */
45 static int mdc_reint(struct ptlrpc_request *request,
46                      struct mdc_rpc_lock *rpc_lock, int level)
47 {
48         int rc;
49
50         request->rq_send_state = level;
51
52         mdc_get_rpc_lock(rpc_lock, NULL);
53         rc = ptlrpc_queue_wait(request);
54         mdc_put_rpc_lock(rpc_lock, NULL);
55         if (rc)
56                 CDEBUG(D_INFO, "error in handling %d\n", rc);
57         else if (!lustre_swab_repbuf(request, REPLY_REC_OFF,
58                                      sizeof(struct mdt_body),
59                                      lustre_swab_mdt_body)) {
60                 CERROR ("Can't unpack mdt_body\n");
61                 rc = -EPROTO;
62         }
63         return rc;
64 }
65
66 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
67  * found by @fid. Found locks are added into @cancel list. Returns the amount of
68  * locks added to @cancels list. */
69 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
70                             struct list_head *cancels, ldlm_mode_t mode,
71                             __u64 bits)
72 {
73         ldlm_policy_data_t policy = {{0}};
74         struct ldlm_res_id res_id;
75         struct ldlm_resource *res;
76         int count;
77         ENTRY;
78
79         fid_build_reg_res_name(fid, &res_id);
80         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
81                                 NULL, &res_id, 0, 0);
82         if (res == NULL)
83                 RETURN(0);
84
85         /* Initialize ibits lock policy. */
86         policy.l_inodebits.bits = bits;
87         count = ldlm_cancel_resource_local(res, cancels, &policy,
88                                            mode, 0, 0, NULL);
89         ldlm_resource_putref(res);
90         RETURN(count);
91 }
92
93 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
94  * should take the normal semaphore and go to the normal portal.
95  *
96  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
97  * magic open-path setattr that should take the setattr semaphore and
98  * go to the setattr portal. */
99 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
100                 void *ea, int ealen, void *ea2, int ea2len,
101                 struct ptlrpc_request **request)
102 {
103         CFS_LIST_HEAD(cancels);
104         struct ptlrpc_request *req;
105         struct mdt_rec_setattr *rec;
106         struct mdc_rpc_lock *rpc_lock;
107         struct obd_device *obd = exp->exp_obd;
108         int size[7] = { sizeof(struct ptlrpc_body),
109                         sizeof(*rec), 0, 0, ealen, ea2len, 0 };
110         int count = 0, bufcount = 4, rc;
111         __u64 bits;
112         ENTRY;
113
114         LASSERT(op_data != NULL);
115
116         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
117                 sizeof(struct lustre_capa) : 0;
118
119         if (op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN))
120                 size[REQ_REC_OFF + 2] = sizeof(struct mdt_epoch);
121
122         if (ealen > 0) {
123                 bufcount++;
124                 if (ea2len > 0)
125                         bufcount++;
126         }
127
128         bits = MDS_INODELOCK_UPDATE;
129         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
130                 bits |= MDS_INODELOCK_LOOKUP;
131         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
132             (fid_is_sane(&op_data->op_fid1)))
133                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
134                                                 &cancels, LCK_EX, bits);
135         if (exp_connect_cancelset(exp) && count) {
136                 bufcount = 7;
137                 size[REQ_REC_OFF + 5] = ldlm_request_bufsize(count, MDS_REINT);
138         }
139         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
140                               MDS_REINT, bufcount, size, NULL);
141         if (exp_connect_cancelset(exp) && req)
142                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 5, 0);
143         else
144                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
145
146         if (req == NULL)
147                 RETURN(-ENOMEM);
148
149         if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
150                 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
151                 rpc_lock = obd->u.cli.cl_setattr_lock;
152         } else {
153                 rpc_lock = obd->u.cli.cl_rpc_lock;
154         }
155
156         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
157                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
158                        LTIME_S(op_data->op_attr.ia_mtime),
159                        LTIME_S(op_data->op_attr.ia_ctime));
160         mdc_setattr_pack(req, REQ_REC_OFF, op_data, ea, ealen, ea2, ea2len);
161
162         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
163         size[REPLY_REC_OFF + 1] = sizeof(struct lustre_capa);
164         ptlrpc_req_set_repsize(req, 3, size);
165
166         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
167         *request = req;
168         if (rc == -ERESTARTSYS)
169                 rc = 0;
170
171         RETURN(rc);
172 }
173
174 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
175                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
176                __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
177 {
178         int size[6] = { sizeof(struct ptlrpc_body),
179                         sizeof(struct mdt_rec_create),
180                         0, op_data->op_namelen + 1, 0, 0 };
181         struct obd_device *obd = exp->exp_obd;
182         int level, bufcount = 4, rc;
183         struct ptlrpc_request *req;
184         int count = 0;
185         CFS_LIST_HEAD(cancels);
186         ENTRY;
187
188         /* For case if upper layer did not alloc fid, do it now. */
189         if (!fid_is_sane(&op_data->op_fid2)) {
190                 /*
191                  * mdc_fid_alloc() may return errno 1 in case of switch to new
192                  * sequence, handle this.
193                  */
194                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
195                 if (rc < 0) {
196                         CERROR("Can't alloc new fid, rc %d\n", rc);
197                         RETURN(rc);
198                 }
199         }
200
201         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
202                 sizeof(struct lustre_capa) : 0;
203         
204         if (data && datalen) {
205                 size[bufcount] = datalen;
206                 bufcount++;
207         }
208
209         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
210             (fid_is_sane(&op_data->op_fid1)))
211                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
212                                                 &cancels, LCK_EX,
213                                                 MDS_INODELOCK_UPDATE);
214         if (exp_connect_cancelset(exp) && count) {
215                 bufcount = 6;
216                 size[REQ_REC_OFF + 4] = ldlm_request_bufsize(count, MDS_REINT);
217         }
218         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
219                               MDS_REINT, bufcount, size, NULL);
220         if (exp_connect_cancelset(exp) && req)
221                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 4, 0);
222         else
223                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
224
225         if (req == NULL)
226                 RETURN(-ENOMEM);
227
228         /*
229          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
230          * tgt, for symlinks or lov MD data.
231          */
232         mdc_create_pack(req, REQ_REC_OFF, op_data, data, datalen, mode, uid,
233                         gid, cap_effective, rdev);
234
235         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
236         size[REPLY_REC_OFF + 1] = sizeof(struct lustre_capa);
237         ptlrpc_req_set_repsize(req, 3, size);
238
239         level = LUSTRE_IMP_FULL;
240  resend:
241         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
242         
243         /* Resend if we were told to. */
244         if (rc == -ERESTARTSYS) {
245                 level = LUSTRE_IMP_RECOVER;
246                 goto resend;
247         } else if (rc == 0) {
248                 struct mdt_body *body;
249                 struct lustre_capa *capa;
250
251                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
252                                       sizeof(*body));
253                 LASSERT(body);
254                 if (body->valid & OBD_MD_FLMDSCAPA) {
255                         capa = lustre_unpack_capa(req->rq_repmsg,
256                                                   REPLY_REC_OFF + 1);
257                         if (capa == NULL) {
258                                 CERROR("Missing/short MDS capability\n");
259                                 rc = -EPROTO;
260                         }
261                 }
262         }
263
264         *request = req;
265         RETURN(rc);
266 }
267
268 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
269                struct ptlrpc_request **request)
270 {
271         CFS_LIST_HEAD(cancels);
272         struct obd_device *obd = class_exp2obd(exp);
273         struct ptlrpc_request *req = *request;
274         int size[5] = { sizeof(struct ptlrpc_body),
275                         sizeof(struct mdt_rec_unlink),
276                         0, op_data->op_namelen + 1, 0 };
277         int count = 0, rc, bufcount = 4;
278         ENTRY;
279
280         LASSERT(req == NULL);
281
282         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
283                 sizeof(struct lustre_capa) : 0;
284
285         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
286             (fid_is_sane(&op_data->op_fid1)))
287                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
288                                                 &cancels, LCK_EX,
289                                                 MDS_INODELOCK_UPDATE);
290         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
291             (fid_is_sane(&op_data->op_fid3)))
292                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
293                                                  &cancels, LCK_EX,
294                                                  MDS_INODELOCK_FULL);
295         if (exp_connect_cancelset(exp) && count) {
296                 bufcount = 5;
297                 size[REQ_REC_OFF + 3] = ldlm_request_bufsize(count, MDS_REINT);
298         }
299         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
300                               MDS_REINT, bufcount, size, NULL);
301         if (exp_connect_cancelset(exp) && req)
302                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 3, 0);
303         else
304                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
305
306         if (req == NULL)
307                 RETURN(-ENOMEM);
308         *request = req;
309
310         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
311         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
312         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
313         ptlrpc_req_set_repsize(req, 4, size);
314
315         mdc_unlink_pack(req, REQ_REC_OFF, op_data);
316
317         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
318         if (rc == -ERESTARTSYS)
319                 rc = 0;
320         RETURN(rc);
321 }
322
323 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
324              struct ptlrpc_request **request)
325 {
326         CFS_LIST_HEAD(cancels);
327         struct obd_device *obd = exp->exp_obd;
328         struct ptlrpc_request *req;
329         int size[6] = { sizeof(struct ptlrpc_body),
330                         sizeof(struct mdt_rec_link),
331                         0, 0, op_data->op_namelen + 1, 0 };
332         int count = 0, rc, bufcount = 5;
333         ENTRY;
334
335         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
336                 sizeof(struct lustre_capa) : 0;
337         size[REQ_REC_OFF + 2] = op_data->op_capa2 ?
338                 sizeof(struct lustre_capa) : 0;
339
340         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
341             (fid_is_sane(&op_data->op_fid2)))
342                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
343                                                 &cancels, LCK_EX,
344                                                 MDS_INODELOCK_UPDATE);
345         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
346             (fid_is_sane(&op_data->op_fid1)))
347                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
348                                                  &cancels, LCK_EX,
349                                                  MDS_INODELOCK_UPDATE);
350         if (exp_connect_cancelset(exp) && count) {
351                 bufcount = 6;
352                 size[REQ_REC_OFF + 4] = ldlm_request_bufsize(count, MDS_REINT);
353         }
354
355         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
356                               MDS_REINT, bufcount, size, NULL);
357         if (exp_connect_cancelset(exp) && req)
358                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 4, 0);
359         else
360                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
361
362         if (req == NULL)
363                 RETURN(-ENOMEM);
364
365         mdc_link_pack(req, REQ_REC_OFF, op_data);
366
367         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
368         ptlrpc_req_set_repsize(req, 2, size);
369
370         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
371         *request = req;
372         if (rc == -ERESTARTSYS)
373                 rc = 0;
374
375         RETURN(rc);
376 }
377
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)
381 {
382         CFS_LIST_HEAD(cancels);
383         struct obd_device *obd = exp->exp_obd;
384         struct ptlrpc_request *req;
385         int size[7] = { sizeof(struct ptlrpc_body),
386                         sizeof(struct mdt_rec_rename),
387                         0, 0, oldlen + 1, newlen + 1, 0 };
388         int count = 0, rc, bufcount = 6;
389         ENTRY;
390
391         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
392                                         sizeof(struct lustre_capa) : 0;
393         size[REQ_REC_OFF + 2] = op_data->op_capa2 ?
394                                         sizeof(struct lustre_capa) : 0;
395
396         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
397             (fid_is_sane(&op_data->op_fid1)))
398                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
399                                                 &cancels, LCK_EX,
400                                                 MDS_INODELOCK_UPDATE);
401         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
402             (fid_is_sane(&op_data->op_fid2)))
403                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
404                                                  &cancels, LCK_EX,
405                                                  MDS_INODELOCK_UPDATE);
406         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
407             (fid_is_sane(&op_data->op_fid3)))
408                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
409                                                  &cancels, LCK_EX,
410                                                  MDS_INODELOCK_LOOKUP);
411         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
412              (fid_is_sane(&op_data->op_fid4)))
413                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
414                                                  &cancels, LCK_EX,
415                                                  MDS_INODELOCK_FULL);
416         if (exp_connect_cancelset(exp) && count) {
417                 bufcount = 7;
418                 size[REQ_REC_OFF + 5] = ldlm_request_bufsize(count, MDS_REINT);
419         }
420         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
421                               MDS_REINT, bufcount, size, NULL);
422         if (exp_connect_cancelset(exp) && req)
423                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 5, 0);
424         else
425                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
426
427         if (req == NULL)
428                 RETURN(-ENOMEM);
429
430         mdc_rename_pack(req, REQ_REC_OFF, op_data, old, oldlen, new, newlen);
431
432         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
433         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
434         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
435         ptlrpc_req_set_repsize(req, 4, size);
436
437         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
438         *request = req;
439         if (rc == -ERESTARTSYS)
440                 rc = 0;
441
442         RETURN(rc);
443 }