Whamcloud - gitweb
b8331e6c10bd03a455d50db0307eb9d068f9c705
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_MDC
34
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37
38 #include <obd_class.h>
39 #include "mdc_internal.h"
40 #include <lustre_fid.h>
41
42 /* mdc_setattr does its own semaphore handling */
43 static int mdc_reint(struct ptlrpc_request *request, int level)
44 {
45         int rc;
46
47         request->rq_send_state = level;
48
49         mdc_get_mod_rpc_slot(request, NULL);
50         rc = ptlrpc_queue_wait(request);
51         mdc_put_mod_rpc_slot(request, NULL);
52         if (rc)
53                 CDEBUG(D_INFO, "error in handling %d\n", rc);
54         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
55                 rc = -EPROTO;
56         }
57         return rc;
58 }
59
60 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
61  * found by @fid. Found locks are added into @cancel list. Returns the amount of
62  * locks added to @cancels list. */
63 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
64                             struct list_head *cancels, enum ldlm_mode mode,
65                             __u64 bits)
66 {
67         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
68         union ldlm_policy_data policy = { {0} };
69         struct ldlm_res_id res_id;
70         struct ldlm_resource *res;
71         int count;
72         ENTRY;
73
74         /* Return, i.e. cancel nothing, only if ELC is supported (flag in
75          * export) but disabled through procfs (flag in NS).
76          *
77          * This distinguishes from a case when ELC is not supported originally,
78          * when we still want to cancel locks in advance and just cancel them
79          * locally, without sending any RPC. */
80         if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
81                 RETURN(0);
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 (IS_ERR(res))
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 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
99                 void *ea, size_t ealen, struct ptlrpc_request **request)
100 {
101         struct list_head cancels = LIST_HEAD_INIT(cancels);
102         struct ptlrpc_request *req;
103         int count = 0, rc;
104         __u64 bits;
105         ENTRY;
106
107         LASSERT(op_data != NULL);
108
109         bits = MDS_INODELOCK_UPDATE;
110         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
111                 bits |= MDS_INODELOCK_LOOKUP;
112         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
113             (fid_is_sane(&op_data->op_fid1)))
114                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
115                                                 &cancels, LCK_EX, bits);
116         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
117                                    &RQF_MDS_REINT_SETATTR);
118         if (req == NULL) {
119                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
120                 RETURN(-ENOMEM);
121         }
122
123         req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
124         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
125         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
126
127         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
128         if (rc) {
129                 ptlrpc_request_free(req);
130                 RETURN(rc);
131         }
132
133         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
134                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld\n",
135                        (s64)op_data->op_attr.ia_mtime.tv_sec,
136                        (s64)op_data->op_attr.ia_ctime.tv_sec);
137         mdc_setattr_pack(req, op_data, ea, ealen);
138
139         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER, 0);
140
141         ptlrpc_request_set_replen(req);
142
143         rc = mdc_reint(req, LUSTRE_IMP_FULL);
144         if (rc == -ERESTARTSYS)
145                 rc = 0;
146
147         *request = req;
148
149         RETURN(rc);
150 }
151
152 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
153                 const void *data, size_t datalen,
154                 umode_t mode, uid_t uid, gid_t gid,
155                 cfs_cap_t cap_effective, __u64 rdev,
156                 struct ptlrpc_request **request)
157 {
158         struct ptlrpc_request *req;
159         int level, rc;
160         int count, resends = 0;
161         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
162         int generation = import->imp_generation;
163         struct list_head cancels = LIST_HEAD_INIT(cancels);
164         ENTRY;
165
166         /* For case if upper layer did not alloc fid, do it now. */
167         if (!fid_is_sane(&op_data->op_fid2)) {
168                 /*
169                  * mdc_fid_alloc() may return errno 1 in case of switch to new
170                  * sequence, handle this.
171                  */
172                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
173                 if (rc < 0)
174                         RETURN(rc);
175         }
176
177 rebuild:
178         count = 0;
179         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
180             (fid_is_sane(&op_data->op_fid1)))
181                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
182                                                 &cancels, LCK_EX,
183                                                 MDS_INODELOCK_UPDATE);
184
185         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
186                                    &RQF_MDS_REINT_CREATE_ACL);
187         if (req == NULL) {
188                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
189                 RETURN(-ENOMEM);
190         }
191
192         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
193                              op_data->op_namelen + 1);
194         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
195                              data && datalen ? datalen : 0);
196
197         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
198                              RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
199                              strlen(op_data->op_file_secctx_name) + 1 : 0);
200
201         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
202                              op_data->op_file_secctx_size);
203
204         /* get SELinux policy info if any */
205         rc = sptlrpc_get_sepol(req);
206         if (rc < 0) {
207                 ptlrpc_request_free(req);
208                 RETURN(rc);
209         }
210         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
211                              strlen(req->rq_sepol) ?
212                              strlen(req->rq_sepol) + 1 : 0);
213
214         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
215         if (rc) {
216                 ptlrpc_request_free(req);
217                 RETURN(rc);
218         }
219
220         /*
221          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
222          * tgt, for symlinks or lov MD data.
223          */
224         mdc_create_pack(req, op_data, data, datalen, mode, uid,
225                         gid, cap_effective, rdev);
226
227         ptlrpc_request_set_replen(req);
228
229         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
230          * logic here */
231         req->rq_no_retry_einprogress = 1;
232
233         if (resends) {
234                 req->rq_generation_set = 1;
235                 req->rq_import_generation = generation;
236                 req->rq_sent = ktime_get_real_seconds() + resends;
237         }
238         level = LUSTRE_IMP_FULL;
239  resend:
240         rc = mdc_reint(req, level);
241
242         /* Resend if we were told to. */
243         if (rc == -ERESTARTSYS) {
244                 level = LUSTRE_IMP_RECOVER;
245                 goto resend;
246         } else if (rc == -EINPROGRESS) {
247                 /* Retry create infinitely until succeed or get other
248                  * error code or interrupted. */
249                 ptlrpc_req_finished(req);
250                 if (generation == import->imp_generation) {
251                         if (signal_pending(current))
252                                 RETURN(-EINTR);
253
254                         resends++;
255                         CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
256                                exp->exp_obd->obd_name, resends,
257                                PFID(&op_data->op_fid1),
258                                PFID(&op_data->op_fid2));
259                         goto rebuild;
260                 } else {
261                         CDEBUG(D_HA, "resend cross eviction\n");
262                         RETURN(-EIO);
263                 }
264         }
265
266         *request = req;
267         RETURN(rc);
268 }
269
270 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
271                struct ptlrpc_request **request)
272 {
273         struct list_head cancels = LIST_HEAD_INIT(cancels);
274         struct obd_device *obd = class_exp2obd(exp);
275         struct ptlrpc_request *req = *request;
276         int count = 0, rc;
277         ENTRY;
278
279         LASSERT(req == NULL);
280
281         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
282             (fid_is_sane(&op_data->op_fid1)))
283                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
284                                                 &cancels, LCK_EX,
285                                                 MDS_INODELOCK_UPDATE);
286         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
287             (fid_is_sane(&op_data->op_fid3)))
288                 /* don't cancel DoM lock which may cause data flush */
289                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
290                                                  &cancels, LCK_EX,
291                                                  MDS_INODELOCK_ELC);
292         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
293                                    &RQF_MDS_REINT_UNLINK);
294         if (req == NULL) {
295                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
296                 RETURN(-ENOMEM);
297         }
298
299         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
300                              op_data->op_namelen + 1);
301
302         /* get SELinux policy info if any */
303         rc = sptlrpc_get_sepol(req);
304         if (rc < 0) {
305                 ptlrpc_request_free(req);
306                 RETURN(rc);
307         }
308         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
309                              strlen(req->rq_sepol) ?
310                              strlen(req->rq_sepol) + 1 : 0);
311
312         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
313         if (rc) {
314                 ptlrpc_request_free(req);
315                 RETURN(rc);
316         }
317
318         mdc_unlink_pack(req, op_data);
319
320         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
321                              obd->u.cli.cl_default_mds_easize);
322         ptlrpc_request_set_replen(req);
323
324         *request = req;
325
326         rc = mdc_reint(req, LUSTRE_IMP_FULL);
327         if (rc == -ERESTARTSYS)
328                 rc = 0;
329         RETURN(rc);
330 }
331
332 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
333              struct ptlrpc_request **request)
334 {
335         struct list_head cancels = LIST_HEAD_INIT(cancels);
336         struct ptlrpc_request *req;
337         int count = 0, rc;
338         ENTRY;
339
340         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
341             (fid_is_sane(&op_data->op_fid2)))
342                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
343                                                 &cancels, LCK_EX,
344                                                 MDS_INODELOCK_UPDATE);
345         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
346             (fid_is_sane(&op_data->op_fid1)))
347                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
348                                                  &cancels, LCK_EX,
349                                                  MDS_INODELOCK_UPDATE);
350
351         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
352         if (req == NULL) {
353                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
354                 RETURN(-ENOMEM);
355         }
356
357         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
358                              op_data->op_namelen + 1);
359
360         /* get SELinux policy info if any */
361         rc = sptlrpc_get_sepol(req);
362         if (rc < 0) {
363                 ptlrpc_request_free(req);
364                 RETURN(rc);
365         }
366         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
367                              strlen(req->rq_sepol) ?
368                              strlen(req->rq_sepol) + 1 : 0);
369
370         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
371         if (rc) {
372                 ptlrpc_request_free(req);
373                 RETURN(rc);
374         }
375
376         mdc_link_pack(req, op_data);
377         ptlrpc_request_set_replen(req);
378
379         rc = mdc_reint(req, LUSTRE_IMP_FULL);
380         *request = req;
381         if (rc == -ERESTARTSYS)
382                 rc = 0;
383
384         RETURN(rc);
385 }
386
387 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
388                 const char *old, size_t oldlen, const char *new, size_t newlen,
389                 struct ptlrpc_request **request)
390 {
391         struct list_head cancels = LIST_HEAD_INIT(cancels);
392         struct obd_device *obd = exp->exp_obd;
393         struct ptlrpc_request *req;
394         int count = 0, rc;
395
396         ENTRY;
397
398         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
399             (fid_is_sane(&op_data->op_fid1)))
400                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
401                                                 &cancels, LCK_EX,
402                                                 MDS_INODELOCK_UPDATE);
403         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
404             (fid_is_sane(&op_data->op_fid2)))
405                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
406                                                  &cancels, LCK_EX,
407                                                  MDS_INODELOCK_UPDATE);
408         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
409             (fid_is_sane(&op_data->op_fid3)))
410                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
411                                                  &cancels, LCK_EX,
412                                                  MDS_INODELOCK_LOOKUP);
413         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
414             (fid_is_sane(&op_data->op_fid4)))
415                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
416                                                  &cancels, LCK_EX,
417                                                  MDS_INODELOCK_ELC);
418
419         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
420                            op_data->op_cli_flags & CLI_MIGRATE ?
421                            &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
422         if (req == NULL) {
423                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
424                 RETURN(-ENOMEM);
425         }
426
427         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
428         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
429         if (op_data->op_cli_flags & CLI_MIGRATE)
430                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
431                                      op_data->op_data_size);
432
433         /* get SELinux policy info if any */
434         rc = sptlrpc_get_sepol(req);
435         if (rc < 0) {
436                 ptlrpc_request_free(req);
437                 RETURN(rc);
438         }
439         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
440                              strlen(req->rq_sepol) ?
441                              strlen(req->rq_sepol) + 1 : 0);
442
443         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
444         if (rc) {
445                 ptlrpc_request_free(req);
446                 RETURN(rc);
447         }
448
449         if (exp_connect_cancelset(exp) && req)
450                 ldlm_cli_cancel_list(&cancels, count, req, 0);
451
452         if (op_data->op_cli_flags & CLI_MIGRATE)
453                 mdc_migrate_pack(req, op_data, old, oldlen);
454         else
455                 mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
456
457         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
458                              obd->u.cli.cl_default_mds_easize);
459         ptlrpc_request_set_replen(req);
460
461         rc = mdc_reint(req, LUSTRE_IMP_FULL);
462         *request = req;
463         if (rc == -ERESTARTSYS)
464                 rc = 0;
465
466         RETURN(rc);
467 }
468
469 int mdc_file_resync(struct obd_export *exp, struct md_op_data *op_data)
470 {
471         struct list_head cancels = LIST_HEAD_INIT(cancels);
472         struct ptlrpc_request *req;
473         struct ldlm_lock *lock;
474         struct mdt_rec_resync *rec;
475         int count = 0, rc;
476         ENTRY;
477
478         if (op_data->op_flags & MF_MDC_CANCEL_FID1 &&
479             fid_is_sane(&op_data->op_fid1))
480                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
481                                                 &cancels, LCK_EX,
482                                                 MDS_INODELOCK_LAYOUT);
483
484         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
485                                    &RQF_MDS_REINT_RESYNC);
486         if (req == NULL) {
487                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
488                 RETURN(-ENOMEM);
489         }
490
491         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
492         if (rc) {
493                 ptlrpc_request_free(req);
494                 RETURN(rc);
495         }
496
497         CLASSERT(sizeof(*rec) == sizeof(struct mdt_rec_reint));
498         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
499         rec->rs_opcode  = REINT_RESYNC;
500         rec->rs_fsuid   = op_data->op_fsuid;
501         rec->rs_fsgid   = op_data->op_fsgid;
502         rec->rs_cap     = op_data->op_cap;
503         rec->rs_fid     = op_data->op_fid1;
504         rec->rs_bias    = op_data->op_bias;
505         rec->rs_mirror_id = op_data->op_mirror_id;
506
507         lock = ldlm_handle2lock(&op_data->op_lease_handle);
508         if (lock != NULL) {
509                 rec->rs_lease_handle = lock->l_remote_handle;
510                 LDLM_LOCK_PUT(lock);
511         }
512
513         ptlrpc_request_set_replen(req);
514
515         rc = mdc_reint(req, LUSTRE_IMP_FULL);
516         if (rc == -ERESTARTSYS)
517                 rc = 0;
518
519         ptlrpc_req_finished(req);
520         RETURN(rc);
521 }