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