Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / mdc / mdc_reint.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Whamcloud, Inc.
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, resends = 0;
224         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
225         int generation = import->imp_generation;
226         CFS_LIST_HEAD(cancels);
227         ENTRY;
228
229         /* For case if upper layer did not alloc fid, do it now. */
230         if (!fid_is_sane(&op_data->op_fid2)) {
231                 /*
232                  * mdc_fid_alloc() may return errno 1 in case of switch to new
233                  * sequence, handle this.
234                  */
235                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
236                 if (rc < 0) {
237                         CERROR("Can't alloc new fid, rc %d\n", rc);
238                         RETURN(rc);
239                 }
240         }
241
242 rebuild:
243         count = 0;
244         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
245             (fid_is_sane(&op_data->op_fid1)))
246                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
247                                                 &cancels, LCK_EX,
248                                                 MDS_INODELOCK_UPDATE);
249
250         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
251                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
252         if (req == NULL) {
253                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
254                 RETURN(-ENOMEM);
255         }
256         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
257         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
258                              op_data->op_namelen + 1);
259         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
260                              data && datalen ? datalen : 0);
261
262         rc = mdc_prep_elc_req(exp, req, &cancels, count);
263         if (rc) {
264                 ptlrpc_request_free(req);
265                 RETURN(rc);
266         }
267
268         /*
269          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
270          * tgt, for symlinks or lov MD data.
271          */
272         mdc_create_pack(req, op_data, data, datalen, mode, uid,
273                         gid, cap_effective, rdev);
274
275         ptlrpc_request_set_replen(req);
276
277         if (resends) {
278                 req->rq_generation_set = 1;
279                 req->rq_import_generation = generation;
280                 req->rq_sent = cfs_time_current_sec() + resends;
281         }
282         level = LUSTRE_IMP_FULL;
283  resend:
284         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
285
286         /* Resend if we were told to. */
287         if (rc == -ERESTARTSYS) {
288                 level = LUSTRE_IMP_RECOVER;
289                 goto resend;
290         } else if (rc == -EINPROGRESS) {
291                 /* Retry create infinitely until succeed or get other
292                  * error code. */
293                 ptlrpc_req_finished(req);
294                 resends++;
295
296                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
297                        exp->exp_obd->obd_name, resends,
298                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
299
300                 if (generation == import->imp_generation) {
301                         goto rebuild;
302                 } else {
303                         CDEBUG(D_HA, "resend cross eviction\n");
304                         RETURN(-EIO);
305                 }
306         } else if (rc == 0) {
307                 struct mdt_body *body;
308                 struct lustre_capa *capa;
309
310                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
311                 LASSERT(body);
312                 if (body->valid & OBD_MD_FLMDSCAPA) {
313                         capa = req_capsule_server_get(&req->rq_pill,
314                                                       &RMF_CAPA1);
315                         if (capa == NULL)
316                                 rc = -EPROTO;
317                 }
318         }
319
320         *request = req;
321         RETURN(rc);
322 }
323
324 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
325                struct ptlrpc_request **request)
326 {
327         CFS_LIST_HEAD(cancels);
328         struct obd_device *obd = class_exp2obd(exp);
329         struct ptlrpc_request *req = *request;
330         int count = 0, rc;
331         ENTRY;
332
333         LASSERT(req == NULL);
334
335         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
336             (fid_is_sane(&op_data->op_fid1)) &&
337             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
338                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
339                                                 &cancels, LCK_EX,
340                                                 MDS_INODELOCK_UPDATE);
341         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
342             (fid_is_sane(&op_data->op_fid3)) &&
343             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
344                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
345                                                  &cancels, LCK_EX,
346                                                  MDS_INODELOCK_FULL);
347         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
348                                    &RQF_MDS_REINT_UNLINK);
349         if (req == NULL) {
350                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
351                 RETURN(-ENOMEM);
352         }
353         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
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_unlink_pack(req, op_data);
364
365         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
366                              obd->u.cli.cl_max_mds_easize);
367         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
368                              obd->u.cli.cl_max_mds_cookiesize);
369         ptlrpc_request_set_replen(req);
370
371         *request = req;
372
373         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
374         if (rc == -ERESTARTSYS)
375                 rc = 0;
376         RETURN(rc);
377 }
378
379 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
380              struct ptlrpc_request **request)
381 {
382         CFS_LIST_HEAD(cancels);
383         struct obd_device *obd = exp->exp_obd;
384         struct ptlrpc_request *req;
385         int count = 0, rc;
386         ENTRY;
387
388         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
389             (fid_is_sane(&op_data->op_fid2)))
390                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
391                                                 &cancels, LCK_EX,
392                                                 MDS_INODELOCK_UPDATE);
393         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
394             (fid_is_sane(&op_data->op_fid1)))
395                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
396                                                  &cancels, LCK_EX,
397                                                  MDS_INODELOCK_UPDATE);
398
399         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
400         if (req == NULL) {
401                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
402                 RETURN(-ENOMEM);
403         }
404         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
405         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
406         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
407                              op_data->op_namelen + 1);
408
409         rc = mdc_prep_elc_req(exp, req, &cancels, count);
410         if (rc) {
411                 ptlrpc_request_free(req);
412                 RETURN(rc);
413         }
414
415         mdc_link_pack(req, op_data);
416         ptlrpc_request_set_replen(req);
417
418         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
419         *request = req;
420         if (rc == -ERESTARTSYS)
421                 rc = 0;
422
423         RETURN(rc);
424 }
425
426 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
427                const char *old, int oldlen, const char *new, int newlen,
428                struct ptlrpc_request **request)
429 {
430         CFS_LIST_HEAD(cancels);
431         struct obd_device *obd = exp->exp_obd;
432         struct ptlrpc_request *req;
433         int count = 0, rc;
434         ENTRY;
435
436         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
437             (fid_is_sane(&op_data->op_fid1)))
438                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
439                                                 &cancels, LCK_EX,
440                                                 MDS_INODELOCK_UPDATE);
441         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
442             (fid_is_sane(&op_data->op_fid2)))
443                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
444                                                  &cancels, LCK_EX,
445                                                  MDS_INODELOCK_UPDATE);
446         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
447             (fid_is_sane(&op_data->op_fid3)))
448                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
449                                                  &cancels, LCK_EX,
450                                                  MDS_INODELOCK_LOOKUP);
451         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
452              (fid_is_sane(&op_data->op_fid4)))
453                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
454                                                  &cancels, LCK_EX,
455                                                  MDS_INODELOCK_FULL);
456
457         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
458                                    &RQF_MDS_REINT_RENAME);
459         if (req == NULL) {
460                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
461                 RETURN(-ENOMEM);
462         }
463
464         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
465         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
466         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
467         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
468
469         rc = mdc_prep_elc_req(exp, req, &cancels, count);
470         if (rc) {
471                 ptlrpc_request_free(req);
472                 RETURN(rc);
473         }
474
475         if (exp_connect_cancelset(exp) && req)
476                 ldlm_cli_cancel_list(&cancels, count, req, 0);
477
478         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
479
480         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
481                              obd->u.cli.cl_max_mds_easize);
482         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
483                              obd->u.cli.cl_max_mds_cookiesize);
484         ptlrpc_request_set_replen(req);
485
486         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
487         *request = req;
488         if (rc == -ERESTARTSYS)
489                 rc = 0;
490
491         RETURN(rc);
492 }