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