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