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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_MDC
41
42 #ifdef __KERNEL__
43 #ifndef AUTOCONF_INCLUDED
44 # include <linux/config.h>
45 #endif
46 # include <linux/module.h>
47 # include <linux/kernel.h>
48 #else
49 # include <liblustre.h>
50 #endif
51
52 #include <obd_class.h>
53 #include "mdc_internal.h"
54 #include <lustre_fid.h>
55
56 /* mdc_setattr does its own semaphore handling */
57 static int mdc_reint(struct ptlrpc_request *request,
58                      struct mdc_rpc_lock *rpc_lock,
59                      int level)
60 {
61         int rc;
62
63         request->rq_send_state = level;
64
65         mdc_get_rpc_lock(rpc_lock, NULL);
66         rc = ptlrpc_queue_wait(request);
67         mdc_put_rpc_lock(rpc_lock, NULL);
68         if (rc)
69                 CDEBUG(D_INFO, "error in handling %d\n", rc);
70         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
71                 rc = -EPROTO;
72         }
73         return rc;
74 }
75
76 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
77  * found by @fid. Found locks are added into @cancel list. Returns the amount of
78  * locks added to @cancels list. */
79 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
80                             struct list_head *cancels, ldlm_mode_t mode,
81                             __u64 bits)
82 {
83         ldlm_policy_data_t policy = {{0}};
84         struct ldlm_res_id res_id;
85         struct ldlm_resource *res;
86         int count;
87         ENTRY;
88
89         fid_build_reg_res_name(fid, &res_id);
90         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
91                                 NULL, &res_id, 0, 0);
92         if (res == NULL)
93                 RETURN(0);
94
95         /* Initialize ibits lock policy. */
96         policy.l_inodebits.bits = bits;
97         count = ldlm_cancel_resource_local(res, cancels, &policy,
98                                            mode, 0, 0, NULL);
99         ldlm_resource_putref(res);
100         RETURN(count);
101 }
102
103 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
104                             struct list_head *cancels, int count)
105 {
106         return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
107                                  0, cancels, count);
108 }
109
110 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
111  * should take the normal semaphore and go to the normal portal.
112  *
113  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
114  * magic open-path setattr that should take the setattr semaphore and
115  * go to the setattr portal. */
116 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
117                 void *ea, int ealen, void *ea2, int ea2len,
118                 struct ptlrpc_request **request, struct md_open_data **mod)
119 {
120         CFS_LIST_HEAD(cancels);
121         struct ptlrpc_request *req;
122         struct mdc_rpc_lock *rpc_lock;
123         struct obd_device *obd = exp->exp_obd;
124         int count = 0, rc;
125         __u64 bits;
126         ENTRY;
127
128         LASSERT(op_data != NULL);
129
130         bits = MDS_INODELOCK_UPDATE;
131         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
132                 bits |= MDS_INODELOCK_LOOKUP;
133         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
134             (fid_is_sane(&op_data->op_fid1)))
135                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
136                                                 &cancels, LCK_EX, bits);
137         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
138                                    &RQF_MDS_REINT_SETATTR);
139         if (req == NULL) {
140                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
141                 RETURN(-ENOMEM);
142         }
143         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
144         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
145                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
146                                      0);
147         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
148         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
149                              ea2len);
150
151         rc = mdc_prep_elc_req(exp, req, &cancels, count);
152         if (rc) {
153                 ptlrpc_request_free(req);
154                 RETURN(rc);
155         }
156
157         if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
158                 req->rq_request_portal = MDS_SETATTR_PORTAL;
159                 ptlrpc_at_set_req_timeout(req);
160                 rpc_lock = obd->u.cli.cl_setattr_lock;
161         } else {
162                 rpc_lock = obd->u.cli.cl_rpc_lock;
163         }
164
165         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
166                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
167                        ", ctime "CFS_TIME_T"\n",
168                        LTIME_S(op_data->op_attr.ia_mtime),
169                        LTIME_S(op_data->op_attr.ia_ctime));
170         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
171
172         ptlrpc_request_set_replen(req);
173         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
174             req->rq_import->imp_replayable)
175         {
176                 LASSERT(*mod == NULL);
177
178                 OBD_ALLOC_PTR(*mod);
179                 if (*mod == NULL) {
180                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
181                                   "md_open_data");
182                 } else {
183                         CFS_INIT_LIST_HEAD(&(*mod)->mod_replay_list);
184                 }
185         }
186         if (mod && *mod) {
187                 req->rq_cb_data = *mod;
188                 req->rq_commit_cb = mdc_commit_delayed;
189                 list_add_tail(&req->rq_mod_list, &(*mod)->mod_replay_list);
190                 /* This is not the last request in sequence for truncate. */
191                 if (op_data->op_flags & MF_EPOCH_OPEN)
192                         req->rq_replay = 1;
193                 else
194                         req->rq_sequence = 1;
195         }
196
197         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
198         *request = req;
199         if (rc == -ERESTARTSYS)
200                 rc = 0;
201         if (rc && req->rq_commit_cb)
202                 req->rq_commit_cb(req);
203         RETURN(rc);
204 }
205
206 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
207                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
208                cfs_cap_t cap_effective, __u64 rdev,
209                struct ptlrpc_request **request)
210 {
211         struct ptlrpc_request *req;
212         int level, rc;
213         int count = 0;
214         CFS_LIST_HEAD(cancels);
215         ENTRY;
216
217         /* For case if upper layer did not alloc fid, do it now. */
218         if (!fid_is_sane(&op_data->op_fid2)) {
219                 /*
220                  * mdc_fid_alloc() may return errno 1 in case of switch to new
221                  * sequence, handle this.
222                  */
223                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
224                 if (rc < 0) {
225                         CERROR("Can't alloc new fid, rc %d\n", rc);
226                         RETURN(rc);
227                 }
228         }
229
230         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
231             (fid_is_sane(&op_data->op_fid1)))
232                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
233                                                 &cancels, LCK_EX,
234                                                 MDS_INODELOCK_UPDATE);
235
236         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
237                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
238         if (req == NULL) {
239                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
240                 RETURN(-ENOMEM);
241         }
242         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
243         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
244                              op_data->op_namelen + 1);
245         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
246                              data && datalen ? datalen : 0);
247
248         rc = mdc_prep_elc_req(exp, req, &cancels, count);
249         if (rc) {
250                 ptlrpc_request_free(req);
251                 RETURN(rc);
252         }
253
254         /*
255          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
256          * tgt, for symlinks or lov MD data.
257          */
258         mdc_create_pack(req, op_data, data, datalen, mode, uid,
259                         gid, cap_effective, rdev);
260
261         ptlrpc_request_set_replen(req);
262
263         level = LUSTRE_IMP_FULL;
264  resend:
265         rc = mdc_reint(req, exp->exp_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 = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
276                 LASSERT(body);
277                 if (body->valid & OBD_MD_FLMDSCAPA) {
278                         capa = req_capsule_server_get(&req->rq_pill,
279                                                       &RMF_CAPA1);
280                         if (capa == NULL)
281                                 rc = -EPROTO;
282                 }
283         }
284
285         *request = req;
286         RETURN(rc);
287 }
288
289 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
290                struct ptlrpc_request **request)
291 {
292         CFS_LIST_HEAD(cancels);
293         struct obd_device *obd = class_exp2obd(exp);
294         struct ptlrpc_request *req = *request;
295         int count = 0, rc;
296         ENTRY;
297
298         LASSERT(req == NULL);
299
300         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
301             (fid_is_sane(&op_data->op_fid1)))
302                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
303                                                 &cancels, LCK_EX,
304                                                 MDS_INODELOCK_UPDATE);
305         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
306             (fid_is_sane(&op_data->op_fid3)))
307                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
308                                                  &cancels, LCK_EX,
309                                                  MDS_INODELOCK_FULL);
310         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
311                                    &RQF_MDS_REINT_UNLINK);
312         if (req == NULL) {
313                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
314                 RETURN(-ENOMEM);
315         }
316         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
317         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
318                              op_data->op_namelen + 1);
319
320         rc = mdc_prep_elc_req(exp, req, &cancels, count);
321         if (rc) {
322                 ptlrpc_request_free(req);
323                 RETURN(rc);
324         }
325
326         mdc_unlink_pack(req, op_data);
327
328         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
329                              obd->u.cli.cl_max_mds_easize);
330         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
331                              obd->u.cli.cl_max_mds_cookiesize);
332         ptlrpc_request_set_replen(req);
333
334         *request = req;
335
336         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
337         if (rc == -ERESTARTSYS)
338                 rc = 0;
339         RETURN(rc);
340 }
341
342 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
343              struct ptlrpc_request **request)
344 {
345         CFS_LIST_HEAD(cancels);
346         struct obd_device *obd = exp->exp_obd;
347         struct ptlrpc_request *req;
348         int count = 0, rc;
349         ENTRY;
350
351         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
352             (fid_is_sane(&op_data->op_fid2)))
353                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
354                                                 &cancels, LCK_EX,
355                                                 MDS_INODELOCK_UPDATE);
356         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
357             (fid_is_sane(&op_data->op_fid1)))
358                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
359                                                  &cancels, LCK_EX,
360                                                  MDS_INODELOCK_UPDATE);
361
362         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
363         if (req == NULL) {
364                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
365                 RETURN(-ENOMEM);
366         }
367         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
368         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
369         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
370                              op_data->op_namelen + 1);
371
372         rc = mdc_prep_elc_req(exp, req, &cancels, count);
373         if (rc) {
374                 ptlrpc_request_free(req);
375                 RETURN(rc);
376         }
377
378         mdc_link_pack(req, op_data);
379         ptlrpc_request_set_replen(req);
380
381         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
382         *request = req;
383         if (rc == -ERESTARTSYS)
384                 rc = 0;
385
386         RETURN(rc);
387 }
388
389 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
390                const char *old, int oldlen, const char *new, int newlen,
391                struct ptlrpc_request **request)
392 {
393         CFS_LIST_HEAD(cancels);
394         struct obd_device *obd = exp->exp_obd;
395         struct ptlrpc_request *req;
396         int count = 0, rc;
397         ENTRY;
398
399         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
400             (fid_is_sane(&op_data->op_fid1)))
401                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
402                                                 &cancels, LCK_EX,
403                                                 MDS_INODELOCK_UPDATE);
404         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
405             (fid_is_sane(&op_data->op_fid2)))
406                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
407                                                  &cancels, LCK_EX,
408                                                  MDS_INODELOCK_UPDATE);
409         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
410             (fid_is_sane(&op_data->op_fid3)))
411                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
412                                                  &cancels, LCK_EX,
413                                                  MDS_INODELOCK_LOOKUP);
414         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
415              (fid_is_sane(&op_data->op_fid4)))
416                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
417                                                  &cancels, LCK_EX,
418                                                  MDS_INODELOCK_FULL);
419
420         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
421                                    &RQF_MDS_REINT_RENAME);
422         if (req == NULL) {
423                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
424                 RETURN(-ENOMEM);
425         }
426
427         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
428         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
429         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
430         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
431
432         rc = mdc_prep_elc_req(exp, req, &cancels, count);
433         if (rc) {
434                 ptlrpc_request_free(req);
435                 RETURN(rc);
436         }
437
438         if (exp_connect_cancelset(exp) && req)
439                 ldlm_cli_cancel_list(&cancels, count, req, 0);
440
441         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
442
443         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
444                              obd->u.cli.cl_max_mds_easize);
445         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
446                              obd->u.cli.cl_max_mds_cookiesize);
447         ptlrpc_request_set_replen(req);
448
449         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
450         *request = req;
451         if (rc == -ERESTARTSYS)
452                 rc = 0;
453
454         RETURN(rc);
455 }