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