Whamcloud - gitweb
b=23848 mdc_clear_open_replay_data() ASSERTION(mod != LP_POISON && mod != NULL)
[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 (c) 2002, 2010, Oracle and/or its affiliates. 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                             cfs_list_t *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         LDLM_RESOURCE_ADDREF(res);
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_DELREF(res);
100         ldlm_resource_putref(res);
101         RETURN(count);
102 }
103
104 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
105                             cfs_list_t *cancels, int count)
106 {
107         return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
108                                  0, cancels, count);
109 }
110
111 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
112  * should take the normal semaphore and go to the normal portal.
113  *
114  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
115  * magic open-path setattr that should take the setattr semaphore and
116  * go to the setattr portal. */
117 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
118                 void *ea, int ealen, void *ea2, int ea2len,
119                 struct ptlrpc_request **request, struct md_open_data **mod)
120 {
121         CFS_LIST_HEAD(cancels);
122         struct ptlrpc_request *req;
123         struct mdc_rpc_lock *rpc_lock;
124         struct obd_device *obd = exp->exp_obd;
125         int count = 0, rc;
126         __u64 bits;
127         ENTRY;
128
129         LASSERT(op_data != NULL);
130
131         bits = MDS_INODELOCK_UPDATE;
132         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
133                 bits |= MDS_INODELOCK_LOOKUP;
134         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
135             (fid_is_sane(&op_data->op_fid1)) &&
136             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
137                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
138                                                 &cancels, LCK_EX, bits);
139         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
140                                    &RQF_MDS_REINT_SETATTR);
141         if (req == NULL) {
142                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
143                 RETURN(-ENOMEM);
144         }
145         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
146         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
147                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
148                                      0);
149         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
150         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
151                              ea2len);
152
153         rc = mdc_prep_elc_req(exp, req, &cancels, count);
154         if (rc) {
155                 ptlrpc_request_free(req);
156                 RETURN(rc);
157         }
158
159         if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
160                 req->rq_request_portal = MDS_SETATTR_PORTAL;
161                 ptlrpc_at_set_req_timeout(req);
162                 rpc_lock = obd->u.cli.cl_setattr_lock;
163         } else {
164                 rpc_lock = obd->u.cli.cl_rpc_lock;
165         }
166
167         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
168                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
169                        ", ctime "CFS_TIME_T"\n",
170                        LTIME_S(op_data->op_attr.ia_mtime),
171                        LTIME_S(op_data->op_attr.ia_ctime));
172         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
173
174         ptlrpc_request_set_replen(req);
175         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
176             req->rq_import->imp_replayable)
177         {
178                 LASSERT(*mod == NULL);
179
180                 *mod = obd_mod_alloc();
181                 if (*mod == NULL) {
182                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
183                                   "md_open_data");
184                 } else {
185                         req->rq_replay = 1;
186                         req->rq_cb_data = *mod;
187                         (*mod)->mod_open_req = req;
188                         req->rq_commit_cb = mdc_commit_open;
189                         /**
190                          * Take an extra reference on \var mod, it protects \var
191                          * mod from being freed on eviction (commit callback is
192                          * called despite rq_replay flag).
193                          * Will be put on mdc_done_writing().
194                          */
195                         obd_mod_get(*mod);
196                 }
197         }
198
199         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
200
201         /* Save the obtained info in the original RPC for the replay case. */
202         if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
203                 struct mdt_ioepoch *epoch;
204                 struct mdt_body  *body;
205
206                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
207                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
208                 LASSERT(epoch != NULL);
209                 LASSERT(body != NULL);
210                 epoch->handle = body->handle;
211                 epoch->ioepoch = body->ioepoch;
212                 req->rq_replay_cb = mdc_replay_open;
213         /** bug 3633, open may be committed and estale answer is not error */
214         } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
215                 rc = 0;
216         } else if (rc == -ERESTARTSYS) {
217                 rc = 0;
218         }
219         *request = req;
220         if (rc && req->rq_commit_cb) {
221                 /* Put an extra reference on \var mod on error case. */
222                 obd_mod_put(*mod);
223                 req->rq_commit_cb(req);
224         }
225         RETURN(rc);
226 }
227
228 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
229                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
230                cfs_cap_t cap_effective, __u64 rdev,
231                struct ptlrpc_request **request)
232 {
233         struct ptlrpc_request *req;
234         int level, rc;
235         int count = 0;
236         CFS_LIST_HEAD(cancels);
237         ENTRY;
238
239         /* For case if upper layer did not alloc fid, do it now. */
240         if (!fid_is_sane(&op_data->op_fid2)) {
241                 /*
242                  * mdc_fid_alloc() may return errno 1 in case of switch to new
243                  * sequence, handle this.
244                  */
245                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
246                 if (rc < 0) {
247                         CERROR("Can't alloc new fid, rc %d\n", rc);
248                         RETURN(rc);
249                 }
250         }
251
252         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
253             (fid_is_sane(&op_data->op_fid1)))
254                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
255                                                 &cancels, LCK_EX,
256                                                 MDS_INODELOCK_UPDATE);
257
258         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
259                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
260         if (req == NULL) {
261                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
262                 RETURN(-ENOMEM);
263         }
264         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
265         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
266                              op_data->op_namelen + 1);
267         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
268                              data && datalen ? datalen : 0);
269
270         rc = mdc_prep_elc_req(exp, req, &cancels, count);
271         if (rc) {
272                 ptlrpc_request_free(req);
273                 RETURN(rc);
274         }
275
276         /*
277          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
278          * tgt, for symlinks or lov MD data.
279          */
280         mdc_create_pack(req, op_data, data, datalen, mode, uid,
281                         gid, cap_effective, rdev);
282
283         ptlrpc_request_set_replen(req);
284
285         level = LUSTRE_IMP_FULL;
286  resend:
287         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
288
289         /* Resend if we were told to. */
290         if (rc == -ERESTARTSYS) {
291                 level = LUSTRE_IMP_RECOVER;
292                 goto resend;
293         } else if (rc == 0) {
294                 struct mdt_body *body;
295                 struct lustre_capa *capa;
296
297                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
298                 LASSERT(body);
299                 if (body->valid & OBD_MD_FLMDSCAPA) {
300                         capa = req_capsule_server_get(&req->rq_pill,
301                                                       &RMF_CAPA1);
302                         if (capa == NULL)
303                                 rc = -EPROTO;
304                 }
305         }
306
307         *request = req;
308         RETURN(rc);
309 }
310
311 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
312                struct ptlrpc_request **request)
313 {
314         CFS_LIST_HEAD(cancels);
315         struct obd_device *obd = class_exp2obd(exp);
316         struct ptlrpc_request *req = *request;
317         int count = 0, rc;
318         ENTRY;
319
320         LASSERT(req == NULL);
321
322         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
323             (fid_is_sane(&op_data->op_fid1)) &&
324             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
325                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
326                                                 &cancels, LCK_EX,
327                                                 MDS_INODELOCK_UPDATE);
328         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
329             (fid_is_sane(&op_data->op_fid3)) &&
330             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
331                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
332                                                  &cancels, LCK_EX,
333                                                  MDS_INODELOCK_FULL);
334         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
335                                    &RQF_MDS_REINT_UNLINK);
336         if (req == NULL) {
337                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
338                 RETURN(-ENOMEM);
339         }
340         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
341         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
342                              op_data->op_namelen + 1);
343
344         rc = mdc_prep_elc_req(exp, req, &cancels, count);
345         if (rc) {
346                 ptlrpc_request_free(req);
347                 RETURN(rc);
348         }
349
350         mdc_unlink_pack(req, op_data);
351
352         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
353                              obd->u.cli.cl_max_mds_easize);
354         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
355                              obd->u.cli.cl_max_mds_cookiesize);
356         ptlrpc_request_set_replen(req);
357
358         *request = req;
359
360         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
361         if (rc == -ERESTARTSYS)
362                 rc = 0;
363         RETURN(rc);
364 }
365
366 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
367              struct ptlrpc_request **request)
368 {
369         CFS_LIST_HEAD(cancels);
370         struct obd_device *obd = exp->exp_obd;
371         struct ptlrpc_request *req;
372         int count = 0, rc;
373         ENTRY;
374
375         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
376             (fid_is_sane(&op_data->op_fid2)))
377                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
378                                                 &cancels, LCK_EX,
379                                                 MDS_INODELOCK_UPDATE);
380         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
381             (fid_is_sane(&op_data->op_fid1)))
382                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
383                                                  &cancels, LCK_EX,
384                                                  MDS_INODELOCK_UPDATE);
385
386         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
387         if (req == NULL) {
388                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
389                 RETURN(-ENOMEM);
390         }
391         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
392         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
393         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
394                              op_data->op_namelen + 1);
395
396         rc = mdc_prep_elc_req(exp, req, &cancels, count);
397         if (rc) {
398                 ptlrpc_request_free(req);
399                 RETURN(rc);
400         }
401
402         mdc_link_pack(req, op_data);
403         ptlrpc_request_set_replen(req);
404
405         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
406         *request = req;
407         if (rc == -ERESTARTSYS)
408                 rc = 0;
409
410         RETURN(rc);
411 }
412
413 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
414                const char *old, int oldlen, const char *new, int newlen,
415                struct ptlrpc_request **request)
416 {
417         CFS_LIST_HEAD(cancels);
418         struct obd_device *obd = exp->exp_obd;
419         struct ptlrpc_request *req;
420         int count = 0, rc;
421         ENTRY;
422
423         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
424             (fid_is_sane(&op_data->op_fid1)))
425                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
426                                                 &cancels, LCK_EX,
427                                                 MDS_INODELOCK_UPDATE);
428         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
429             (fid_is_sane(&op_data->op_fid2)))
430                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
431                                                  &cancels, LCK_EX,
432                                                  MDS_INODELOCK_UPDATE);
433         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
434             (fid_is_sane(&op_data->op_fid3)))
435                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
436                                                  &cancels, LCK_EX,
437                                                  MDS_INODELOCK_LOOKUP);
438         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
439              (fid_is_sane(&op_data->op_fid4)))
440                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
441                                                  &cancels, LCK_EX,
442                                                  MDS_INODELOCK_FULL);
443
444         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
445                                    &RQF_MDS_REINT_RENAME);
446         if (req == NULL) {
447                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
448                 RETURN(-ENOMEM);
449         }
450
451         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
452         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
453         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
454         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
455
456         rc = mdc_prep_elc_req(exp, req, &cancels, count);
457         if (rc) {
458                 ptlrpc_request_free(req);
459                 RETURN(rc);
460         }
461
462         if (exp_connect_cancelset(exp) && req)
463                 ldlm_cli_cancel_list(&cancels, count, req, 0);
464
465         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
466
467         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
468                              obd->u.cli.cl_max_mds_easize);
469         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
470                              obd->u.cli.cl_max_mds_cookiesize);
471         ptlrpc_request_set_replen(req);
472
473         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
474         *request = req;
475         if (rc == -ERESTARTSYS)
476                 rc = 0;
477
478         RETURN(rc);
479 }