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