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, struct md_open_data **mod)
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         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
166             req->rq_import->imp_replayable)
167         {
168                 LASSERT(*mod == NULL);
169
170                 OBD_ALLOC_PTR(*mod);
171                 if (*mod == NULL) {
172                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
173                                   "md_open_data");
174                 } else {
175                         CFS_INIT_LIST_HEAD(&(*mod)->mod_replay_list);
176                 }
177         }
178         if (mod && *mod) {
179                 req->rq_cb_data = *mod;
180                 req->rq_commit_cb = mdc_commit_delayed;
181                 list_add_tail(&req->rq_mod_list, &(*mod)->mod_replay_list);
182                 /* This is not the last request in sequence for truncate. */
183                 if (op_data->op_flags & MF_EPOCH_OPEN)
184                         req->rq_replay = 1;
185                 else
186                         req->rq_sequence = 1;
187         }
188
189         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
190         *request = req;
191         if (rc == -ERESTARTSYS)
192                 rc = 0;
193         if (rc && req->rq_commit_cb)
194                 req->rq_commit_cb(req);
195         RETURN(rc);
196 }
197
198 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
199                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
200                __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
201 {
202         int size[6] = { sizeof(struct ptlrpc_body),
203                         sizeof(struct mdt_rec_create),
204                         0, op_data->op_namelen + 1, 0, 0 };
205         struct obd_device *obd = exp->exp_obd;
206         int level, bufcount = 4, rc;
207         struct ptlrpc_request *req;
208         int count = 0;
209         CFS_LIST_HEAD(cancels);
210         ENTRY;
211
212         /* For case if upper layer did not alloc fid, do it now. */
213         if (!fid_is_sane(&op_data->op_fid2)) {
214                 /*
215                  * mdc_fid_alloc() may return errno 1 in case of switch to new
216                  * sequence, handle this.
217                  */
218                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
219                 if (rc < 0) {
220                         CERROR("Can't alloc new fid, rc %d\n", rc);
221                         RETURN(rc);
222                 }
223         }
224
225         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
226                 sizeof(struct lustre_capa) : 0;
227         
228         if (data && datalen) {
229                 size[bufcount] = datalen;
230                 bufcount++;
231         }
232
233         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
234             (fid_is_sane(&op_data->op_fid1)))
235                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
236                                                 &cancels, LCK_EX,
237                                                 MDS_INODELOCK_UPDATE);
238         if (exp_connect_cancelset(exp) && count) {
239                 bufcount = 6;
240                 size[REQ_REC_OFF + 4] = ldlm_request_bufsize(count, MDS_REINT);
241         }
242         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
243                               MDS_REINT, bufcount, size, NULL);
244         if (exp_connect_cancelset(exp) && req)
245                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 4, 0);
246         else
247                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
248
249         if (req == NULL)
250                 RETURN(-ENOMEM);
251
252         /*
253          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
254          * tgt, for symlinks or lov MD data.
255          */
256         mdc_create_pack(req, REQ_REC_OFF, op_data, data, datalen, mode, uid,
257                         gid, cap_effective, rdev);
258
259         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
260         size[REPLY_REC_OFF + 1] = sizeof(struct lustre_capa);
261         ptlrpc_req_set_repsize(req, 3, size);
262
263         level = LUSTRE_IMP_FULL;
264  resend:
265         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
266         
267         /* Resend if we were told to. */
268         if (rc == -ERESTARTSYS) {
269                 level = LUSTRE_IMP_RECOVER;
270                 goto resend;
271         } else if (rc == 0) {
272                 struct mdt_body *body;
273                 struct lustre_capa *capa;
274
275                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
276                                       sizeof(*body));
277                 LASSERT(body);
278                 if (body->valid & OBD_MD_FLMDSCAPA) {
279                         capa = lustre_unpack_capa(req->rq_repmsg,
280                                                   REPLY_REC_OFF + 1);
281                         if (capa == NULL) {
282                                 CERROR("Missing/short MDS capability\n");
283                                 rc = -EPROTO;
284                         }
285                 }
286         }
287
288         *request = req;
289         RETURN(rc);
290 }
291
292 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
293                struct ptlrpc_request **request)
294 {
295         CFS_LIST_HEAD(cancels);
296         struct obd_device *obd = class_exp2obd(exp);
297         struct ptlrpc_request *req = *request;
298         int size[5] = { sizeof(struct ptlrpc_body),
299                         sizeof(struct mdt_rec_unlink),
300                         0, op_data->op_namelen + 1, 0 };
301         int count = 0, rc, bufcount = 4;
302         ENTRY;
303
304         LASSERT(req == NULL);
305
306         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
307                 sizeof(struct lustre_capa) : 0;
308
309         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
310             (fid_is_sane(&op_data->op_fid1)))
311                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
312                                                 &cancels, LCK_EX,
313                                                 MDS_INODELOCK_UPDATE);
314         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
315             (fid_is_sane(&op_data->op_fid3)))
316                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
317                                                  &cancels, LCK_EX,
318                                                  MDS_INODELOCK_FULL);
319         if (exp_connect_cancelset(exp) && count) {
320                 bufcount = 5;
321                 size[REQ_REC_OFF + 3] = ldlm_request_bufsize(count, MDS_REINT);
322         }
323         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
324                               MDS_REINT, bufcount, size, NULL);
325         if (exp_connect_cancelset(exp) && req)
326                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 3, 0);
327         else
328                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
329
330         if (req == NULL)
331                 RETURN(-ENOMEM);
332         *request = req;
333
334         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
335         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
336         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
337         ptlrpc_req_set_repsize(req, 4, size);
338
339         mdc_unlink_pack(req, REQ_REC_OFF, op_data);
340
341         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
342         if (rc == -ERESTARTSYS)
343                 rc = 0;
344         RETURN(rc);
345 }
346
347 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
348              struct ptlrpc_request **request)
349 {
350         CFS_LIST_HEAD(cancels);
351         struct obd_device *obd = exp->exp_obd;
352         struct ptlrpc_request *req;
353         int size[6] = { sizeof(struct ptlrpc_body),
354                         sizeof(struct mdt_rec_link),
355                         0, 0, op_data->op_namelen + 1, 0 };
356         int count = 0, rc, bufcount = 5;
357         ENTRY;
358
359         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
360                 sizeof(struct lustre_capa) : 0;
361         size[REQ_REC_OFF + 2] = op_data->op_capa2 ?
362                 sizeof(struct lustre_capa) : 0;
363
364         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
365             (fid_is_sane(&op_data->op_fid2)))
366                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
367                                                 &cancels, LCK_EX,
368                                                 MDS_INODELOCK_UPDATE);
369         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
370             (fid_is_sane(&op_data->op_fid1)))
371                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
372                                                  &cancels, LCK_EX,
373                                                  MDS_INODELOCK_UPDATE);
374         if (exp_connect_cancelset(exp) && count) {
375                 bufcount = 6;
376                 size[REQ_REC_OFF + 4] = ldlm_request_bufsize(count, MDS_REINT);
377         }
378
379         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
380                               MDS_REINT, bufcount, size, NULL);
381         if (exp_connect_cancelset(exp) && req)
382                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 4, 0);
383         else
384                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
385
386         if (req == NULL)
387                 RETURN(-ENOMEM);
388
389         mdc_link_pack(req, REQ_REC_OFF, op_data);
390
391         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
392         ptlrpc_req_set_repsize(req, 2, size);
393
394         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
395         *request = req;
396         if (rc == -ERESTARTSYS)
397                 rc = 0;
398
399         RETURN(rc);
400 }
401
402 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
403                const char *old, int oldlen, const char *new, int newlen,
404                struct ptlrpc_request **request)
405 {
406         CFS_LIST_HEAD(cancels);
407         struct obd_device *obd = exp->exp_obd;
408         struct ptlrpc_request *req;
409         int size[7] = { sizeof(struct ptlrpc_body),
410                         sizeof(struct mdt_rec_rename),
411                         0, 0, oldlen + 1, newlen + 1, 0 };
412         int count = 0, rc, bufcount = 6;
413         ENTRY;
414
415         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
416                                         sizeof(struct lustre_capa) : 0;
417         size[REQ_REC_OFF + 2] = op_data->op_capa2 ?
418                                         sizeof(struct lustre_capa) : 0;
419
420         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
421             (fid_is_sane(&op_data->op_fid1)))
422                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
423                                                 &cancels, LCK_EX,
424                                                 MDS_INODELOCK_UPDATE);
425         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
426             (fid_is_sane(&op_data->op_fid2)))
427                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
428                                                  &cancels, LCK_EX,
429                                                  MDS_INODELOCK_UPDATE);
430         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
431             (fid_is_sane(&op_data->op_fid3)))
432                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
433                                                  &cancels, LCK_EX,
434                                                  MDS_INODELOCK_LOOKUP);
435         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
436              (fid_is_sane(&op_data->op_fid4)))
437                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
438                                                  &cancels, LCK_EX,
439                                                  MDS_INODELOCK_FULL);
440         if (exp_connect_cancelset(exp) && count) {
441                 bufcount = 7;
442                 size[REQ_REC_OFF + 5] = ldlm_request_bufsize(count, MDS_REINT);
443         }
444         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
445                               MDS_REINT, bufcount, size, NULL);
446         if (exp_connect_cancelset(exp) && req)
447                 ldlm_cli_cancel_list(&cancels, count, req, REQ_REC_OFF + 5, 0);
448         else
449                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
450
451         if (req == NULL)
452                 RETURN(-ENOMEM);
453
454         mdc_rename_pack(req, REQ_REC_OFF, op_data, old, oldlen, new, newlen);
455
456         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
457         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
458         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
459         ptlrpc_req_set_repsize(req, 4, size);
460
461         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
462         *request = req;
463         if (rc == -ERESTARTSYS)
464                 rc = 0;
465
466         RETURN(rc);
467 }