Whamcloud - gitweb
LU-2558 test: recovery-small 19a FAIL no eviction
[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) 2011, 2012, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_MDC
38
39 #ifdef __KERNEL__
40 # include <linux/module.h>
41 # include <linux/kernel.h>
42 #else
43 # include <liblustre.h>
44 #endif
45
46 #include <obd_class.h>
47 #include "mdc_internal.h"
48 #include <lustre_fid.h>
49
50 /* mdc_setattr does its own semaphore handling */
51 static int mdc_reint(struct ptlrpc_request *request,
52                      struct mdc_rpc_lock *rpc_lock,
53                      int level)
54 {
55         int rc;
56
57         request->rq_send_state = level;
58
59         mdc_get_rpc_lock(rpc_lock, NULL);
60         rc = ptlrpc_queue_wait(request);
61         mdc_put_rpc_lock(rpc_lock, NULL);
62         if (rc)
63                 CDEBUG(D_INFO, "error in handling %d\n", rc);
64         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
65                 rc = -EPROTO;
66         }
67         return rc;
68 }
69
70 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
71  * found by @fid. Found locks are added into @cancel list. Returns the amount of
72  * locks added to @cancels list. */
73 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
74                             cfs_list_t *cancels, ldlm_mode_t mode,
75                             __u64 bits)
76 {
77         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
78         ldlm_policy_data_t policy = {{0}};
79         struct ldlm_res_id res_id;
80         struct ldlm_resource *res;
81         int count;
82         ENTRY;
83
84         /* Return, i.e. cancel nothing, only if ELC is supported (flag in
85          * export) but disabled through procfs (flag in NS).
86          *
87          * This distinguishes from a case when ELC is not supported originally,
88          * when we still want to cancel locks in advance and just cancel them
89          * locally, without sending any RPC. */
90         if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
91                 RETURN(0);
92
93         fid_build_reg_res_name(fid, &res_id);
94         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
95                                 NULL, &res_id, 0, 0);
96         if (res == NULL)
97                 RETURN(0);
98         LDLM_RESOURCE_ADDREF(res);
99         /* Initialize ibits lock policy. */
100         policy.l_inodebits.bits = bits;
101         count = ldlm_cancel_resource_local(res, cancels, &policy,
102                                            mode, 0, 0, NULL);
103         LDLM_RESOURCE_DELREF(res);
104         ldlm_resource_putref(res);
105         RETURN(count);
106 }
107
108 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
109                             cfs_list_t *cancels, int count)
110 {
111         return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
112                                  0, cancels, count);
113 }
114
115 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
116                 void *ea, int ealen, void *ea2, int ea2len,
117                 struct ptlrpc_request **request, struct md_open_data **mod)
118 {
119         CFS_LIST_HEAD(cancels);
120         struct ptlrpc_request *req;
121         struct mdc_rpc_lock *rpc_lock;
122         struct obd_device *obd = exp->exp_obd;
123         int count = 0, rc;
124         __u64 bits;
125         ENTRY;
126
127         LASSERT(op_data != NULL);
128
129         bits = MDS_INODELOCK_UPDATE;
130         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
131                 bits |= MDS_INODELOCK_LOOKUP;
132         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
133             (fid_is_sane(&op_data->op_fid1)) &&
134             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
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         rpc_lock = obd->u.cli.cl_rpc_lock;
158
159         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
160                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
161                        ", ctime "CFS_TIME_T"\n",
162                        LTIME_S(op_data->op_attr.ia_mtime),
163                        LTIME_S(op_data->op_attr.ia_ctime));
164         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
165
166         ptlrpc_request_set_replen(req);
167         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
168             req->rq_import->imp_replayable)
169         {
170                 LASSERT(*mod == NULL);
171
172                 *mod = obd_mod_alloc();
173                 if (*mod == NULL) {
174                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
175                                   "md_open_data");
176                 } else {
177                         req->rq_replay = 1;
178                         req->rq_cb_data = *mod;
179                         (*mod)->mod_open_req = req;
180                         req->rq_commit_cb = mdc_commit_open;
181                         /**
182                          * Take an extra reference on \var mod, it protects \var
183                          * mod from being freed on eviction (commit callback is
184                          * called despite rq_replay flag).
185                          * Will be put on mdc_done_writing().
186                          */
187                         obd_mod_get(*mod);
188                 }
189         }
190
191         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
192
193         /* Save the obtained info in the original RPC for the replay case. */
194         if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
195                 struct mdt_ioepoch *epoch;
196                 struct mdt_body  *body;
197
198                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
199                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
200                 LASSERT(epoch != NULL);
201                 LASSERT(body != NULL);
202                 epoch->handle = body->handle;
203                 epoch->ioepoch = body->ioepoch;
204                 req->rq_replay_cb = mdc_replay_open;
205         /** bug 3633, open may be committed and estale answer is not error */
206         } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
207                 rc = 0;
208         } else if (rc == -ERESTARTSYS) {
209                 rc = 0;
210         }
211         *request = req;
212         if (rc && req->rq_commit_cb) {
213                 /* Put an extra reference on \var mod on error case. */
214                 obd_mod_put(*mod);
215                 req->rq_commit_cb(req);
216         }
217         RETURN(rc);
218 }
219
220 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
221                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
222                cfs_cap_t cap_effective, __u64 rdev,
223                struct ptlrpc_request **request)
224 {
225         struct ptlrpc_request *req;
226         int level, rc;
227         int count, resends = 0;
228         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
229         int generation = import->imp_generation;
230         CFS_LIST_HEAD(cancels);
231         ENTRY;
232
233         /* For case if upper layer did not alloc fid, do it now. */
234         if (!fid_is_sane(&op_data->op_fid2)) {
235                 /*
236                  * mdc_fid_alloc() may return errno 1 in case of switch to new
237                  * sequence, handle this.
238                  */
239                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
240                 if (rc < 0) {
241                         CERROR("Can't alloc new fid, rc %d\n", rc);
242                         RETURN(rc);
243                 }
244         }
245
246 rebuild:
247         count = 0;
248         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
249             (fid_is_sane(&op_data->op_fid1)))
250                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
251                                                 &cancels, LCK_EX,
252                                                 MDS_INODELOCK_UPDATE);
253
254         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
255                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
256         if (req == NULL) {
257                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
258                 RETURN(-ENOMEM);
259         }
260         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
261         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
262                              op_data->op_namelen + 1);
263         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
264                              data && datalen ? datalen : 0);
265
266         rc = mdc_prep_elc_req(exp, req, &cancels, count);
267         if (rc) {
268                 ptlrpc_request_free(req);
269                 RETURN(rc);
270         }
271
272         /*
273          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
274          * tgt, for symlinks or lov MD data.
275          */
276         mdc_create_pack(req, op_data, data, datalen, mode, uid,
277                         gid, cap_effective, rdev);
278
279         ptlrpc_request_set_replen(req);
280
281         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
282          * logic here */
283         req->rq_no_retry_einprogress = 1;
284
285         if (resends) {
286                 req->rq_generation_set = 1;
287                 req->rq_import_generation = generation;
288                 req->rq_sent = cfs_time_current_sec() + resends;
289         }
290         level = LUSTRE_IMP_FULL;
291  resend:
292         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
293
294         /* Resend if we were told to. */
295         if (rc == -ERESTARTSYS) {
296                 level = LUSTRE_IMP_RECOVER;
297                 goto resend;
298         } else if (rc == -EINPROGRESS) {
299                 /* Retry create infinitely until succeed or get other
300                  * error code. */
301                 ptlrpc_req_finished(req);
302                 resends++;
303
304                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
305                        exp->exp_obd->obd_name, resends,
306                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
307
308                 if (generation == import->imp_generation) {
309                         goto rebuild;
310                 } else {
311                         CDEBUG(D_HA, "resend cross eviction\n");
312                         RETURN(-EIO);
313                 }
314         } else if (rc == 0) {
315                 struct mdt_body *body;
316                 struct lustre_capa *capa;
317
318                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
319                 LASSERT(body);
320                 if (body->valid & OBD_MD_FLMDSCAPA) {
321                         capa = req_capsule_server_get(&req->rq_pill,
322                                                       &RMF_CAPA1);
323                         if (capa == NULL)
324                                 rc = -EPROTO;
325                 }
326         }
327
328         *request = req;
329         RETURN(rc);
330 }
331
332 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
333                struct ptlrpc_request **request)
334 {
335         CFS_LIST_HEAD(cancels);
336         struct obd_device *obd = class_exp2obd(exp);
337         struct ptlrpc_request *req = *request;
338         int count = 0, rc;
339         ENTRY;
340
341         LASSERT(req == NULL);
342
343         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
344             (fid_is_sane(&op_data->op_fid1)) &&
345             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
346                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
347                                                 &cancels, LCK_EX,
348                                                 MDS_INODELOCK_UPDATE);
349         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
350             (fid_is_sane(&op_data->op_fid3)) &&
351             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
352                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
353                                                  &cancels, LCK_EX,
354                                                  MDS_INODELOCK_FULL);
355         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
356                                    &RQF_MDS_REINT_UNLINK);
357         if (req == NULL) {
358                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
359                 RETURN(-ENOMEM);
360         }
361         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
362         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
363                              op_data->op_namelen + 1);
364
365         rc = mdc_prep_elc_req(exp, req, &cancels, count);
366         if (rc) {
367                 ptlrpc_request_free(req);
368                 RETURN(rc);
369         }
370
371         mdc_unlink_pack(req, op_data);
372
373         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
374                              obd->u.cli.cl_max_mds_easize);
375         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
376                              obd->u.cli.cl_max_mds_cookiesize);
377         ptlrpc_request_set_replen(req);
378
379         *request = req;
380
381         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
382         if (rc == -ERESTARTSYS)
383                 rc = 0;
384         RETURN(rc);
385 }
386
387 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
388              struct ptlrpc_request **request)
389 {
390         CFS_LIST_HEAD(cancels);
391         struct obd_device *obd = exp->exp_obd;
392         struct ptlrpc_request *req;
393         int count = 0, rc;
394         ENTRY;
395
396         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
397             (fid_is_sane(&op_data->op_fid2)))
398                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
399                                                 &cancels, LCK_EX,
400                                                 MDS_INODELOCK_UPDATE);
401         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
402             (fid_is_sane(&op_data->op_fid1)))
403                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
404                                                  &cancels, LCK_EX,
405                                                  MDS_INODELOCK_UPDATE);
406
407         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
408         if (req == NULL) {
409                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
410                 RETURN(-ENOMEM);
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,
415                              op_data->op_namelen + 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         mdc_link_pack(req, op_data);
424         ptlrpc_request_set_replen(req);
425
426         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
427         *request = req;
428         if (rc == -ERESTARTSYS)
429                 rc = 0;
430
431         RETURN(rc);
432 }
433
434 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
435                const char *old, int oldlen, const char *new, int newlen,
436                struct ptlrpc_request **request)
437 {
438         CFS_LIST_HEAD(cancels);
439         struct obd_device *obd = exp->exp_obd;
440         struct ptlrpc_request *req;
441         int count = 0, rc;
442         ENTRY;
443
444         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
445             (fid_is_sane(&op_data->op_fid1)))
446                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
447                                                 &cancels, LCK_EX,
448                                                 MDS_INODELOCK_UPDATE);
449         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
450             (fid_is_sane(&op_data->op_fid2)))
451                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
452                                                  &cancels, LCK_EX,
453                                                  MDS_INODELOCK_UPDATE);
454         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
455             (fid_is_sane(&op_data->op_fid3)))
456                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
457                                                  &cancels, LCK_EX,
458                                                  MDS_INODELOCK_LOOKUP);
459         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
460              (fid_is_sane(&op_data->op_fid4)))
461                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
462                                                  &cancels, LCK_EX,
463                                                  MDS_INODELOCK_FULL);
464
465         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
466                                    &RQF_MDS_REINT_RENAME);
467         if (req == NULL) {
468                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
469                 RETURN(-ENOMEM);
470         }
471
472         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
473         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
474         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
475         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
476
477         rc = mdc_prep_elc_req(exp, req, &cancels, count);
478         if (rc) {
479                 ptlrpc_request_free(req);
480                 RETURN(rc);
481         }
482
483         if (exp_connect_cancelset(exp) && req)
484                 ldlm_cli_cancel_list(&cancels, count, req, 0);
485
486         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
487
488         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
489                              obd->u.cli.cl_max_mds_easize);
490         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
491                              obd->u.cli.cl_max_mds_cookiesize);
492         ptlrpc_request_set_replen(req);
493
494         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
495         *request = req;
496         if (rc == -ERESTARTSYS)
497                 rc = 0;
498
499         RETURN(rc);
500 }