Whamcloud - gitweb
LU-14470 dne: striped mkdir replay by client request
[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  */
31
32 #define DEBUG_SUBSYSTEM S_MDC
33
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36
37 #include <obd_class.h>
38 #include "mdc_internal.h"
39 #include <lustre_fid.h>
40
41 /* mdc_setattr does its own semaphore handling */
42 static int mdc_reint(struct ptlrpc_request *request, int level)
43 {
44         int rc;
45
46         request->rq_send_state = level;
47
48         ptlrpc_get_mod_rpc_slot(request);
49         rc = ptlrpc_queue_wait(request);
50         ptlrpc_put_mod_rpc_slot(request);
51         if (rc)
52                 CDEBUG(D_INFO, "error in handling %d\n", rc);
53         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
54                 rc = -EPROTO;
55         }
56         return rc;
57 }
58
59 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
60  * found by @fid. Found locks are added into @cancel list. Returns the amount of
61  * locks added to @cancels list. */
62 int mdc_resource_get_unused_res(struct obd_export *exp,
63                                 struct ldlm_res_id *res_id,
64                                 struct list_head *cancels,
65                                 enum ldlm_mode mode, __u64 bits)
66 {
67         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
68         union ldlm_policy_data policy = { { 0 } };
69         struct ldlm_resource *res;
70         int count;
71
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         res = ldlm_resource_get(ns, res_id, 0, 0);
84         if (IS_ERR(res))
85                 RETURN(0);
86         LDLM_RESOURCE_ADDREF(res);
87         /* Initialize ibits lock policy. */
88         policy.l_inodebits.bits = bits;
89         count = ldlm_cancel_resource_local(res, cancels, &policy, mode, 0, 0,
90                                            NULL);
91         LDLM_RESOURCE_DELREF(res);
92         ldlm_resource_putref(res);
93         RETURN(count);
94 }
95
96 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
97                             struct list_head *cancels, enum ldlm_mode mode,
98                             __u64 bits)
99 {
100         struct ldlm_res_id res_id;
101
102         fid_build_reg_res_name(fid, &res_id);
103         return mdc_resource_get_unused_res(exp, &res_id, cancels, mode, bits);
104 }
105
106 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
107                 void *ea, size_t ealen, struct ptlrpc_request **request)
108 {
109         LIST_HEAD(cancels);
110         struct ptlrpc_request *req;
111         int count = 0, rc;
112         __u64 bits;
113         ENTRY;
114
115         LASSERT(op_data != NULL);
116
117         bits = MDS_INODELOCK_UPDATE;
118         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
119                 bits |= MDS_INODELOCK_LOOKUP;
120         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
121             (fid_is_sane(&op_data->op_fid1)))
122                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
123                                                 &cancels, LCK_EX, bits);
124         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
125                                    &RQF_MDS_REINT_SETATTR);
126         if (req == NULL) {
127                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
128                 RETURN(-ENOMEM);
129         }
130
131         req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
132         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
133         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
134
135         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
136         if (rc) {
137                 ptlrpc_request_free(req);
138                 RETURN(rc);
139         }
140
141         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
142                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld\n",
143                        (s64)op_data->op_attr.ia_mtime.tv_sec,
144                        (s64)op_data->op_attr.ia_ctime.tv_sec);
145         mdc_setattr_pack(&req->rq_pill, op_data, ea, ealen);
146
147         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER, 0);
148
149         ptlrpc_request_set_replen(req);
150
151         rc = mdc_reint(req, LUSTRE_IMP_FULL);
152         if (rc == -ERESTARTSYS)
153                 rc = 0;
154
155         *request = req;
156
157         RETURN(rc);
158 }
159
160 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
161                 const void *data, size_t datalen,
162                 umode_t mode, uid_t uid, gid_t gid,
163                 kernel_cap_t cap_effective, __u64 rdev,
164                 struct ptlrpc_request **request)
165 {
166         struct ptlrpc_request *req;
167         int level, rc;
168         int count, resends = 0;
169         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
170         int generation = import->imp_generation;
171         LIST_HEAD(cancels);
172
173         ENTRY;
174
175         /* For case if upper layer did not alloc fid, do it now. */
176         if (!fid_is_sane(&op_data->op_fid2)) {
177                 /*
178                  * mdc_fid_alloc() may return errno 1 in case of switch to new
179                  * sequence, handle this.
180                  */
181                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
182                 if (rc < 0)
183                         RETURN(rc);
184         }
185
186 rebuild:
187         count = 0;
188         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
189             (fid_is_sane(&op_data->op_fid1)))
190                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
191                                                 &cancels, LCK_EX,
192                                                 MDS_INODELOCK_UPDATE);
193
194         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
195                                    &RQF_MDS_REINT_CREATE_ACL);
196         if (req == NULL) {
197                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
198                 RETURN(-ENOMEM);
199         }
200
201         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
202                              op_data->op_namelen + 1);
203         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
204                              data && datalen ? datalen : 0);
205
206         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
207                              RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
208                              strlen(op_data->op_file_secctx_name) + 1 : 0);
209
210         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
211                              op_data->op_file_secctx_size);
212
213         req_capsule_set_size(&req->rq_pill, &RMF_FILE_ENCCTX, RCL_CLIENT,
214                              op_data->op_file_encctx_size);
215
216         /* get SELinux policy info if any */
217         rc = sptlrpc_get_sepol(req);
218         if (rc < 0) {
219                 ptlrpc_request_free(req);
220                 RETURN(rc);
221         }
222         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
223                              strlen(req->rq_sepol) ?
224                              strlen(req->rq_sepol) + 1 : 0);
225
226         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
227         if (rc) {
228                 ptlrpc_request_free(req);
229                 RETURN(rc);
230         }
231
232         /*
233          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
234          * tgt, for symlinks or lov MD data.
235          */
236         mdc_create_pack(&req->rq_pill, op_data, data, datalen, mode, uid,
237                         gid, cap_effective, rdev);
238
239         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
240                              exp->exp_obd->u.cli.cl_default_mds_easize);
241         ptlrpc_request_set_replen(req);
242
243         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
244          * logic here */
245         req->rq_no_retry_einprogress = 1;
246
247         if (resends) {
248                 req->rq_generation_set = 1;
249                 req->rq_import_generation = generation;
250                 req->rq_sent = ktime_get_real_seconds() + resends;
251         }
252         level = LUSTRE_IMP_FULL;
253  resend:
254         rc = mdc_reint(req, level);
255
256         /* Resend if we were told to. */
257         if (rc == -ERESTARTSYS) {
258                 level = LUSTRE_IMP_RECOVER;
259                 goto resend;
260         } else if (rc == -EINPROGRESS) {
261                 /* Retry create infinitely until succeed or get other
262                  * error code or interrupted. */
263                 ptlrpc_req_finished(req);
264                 if (generation == import->imp_generation) {
265                         if (signal_pending(current))
266                                 RETURN(-EINTR);
267
268                         resends++;
269                         CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
270                                exp->exp_obd->obd_name, resends,
271                                PFID(&op_data->op_fid1),
272                                PFID(&op_data->op_fid2));
273                         goto rebuild;
274                 } else {
275                         CDEBUG(D_HA, "resend cross eviction\n");
276                         RETURN(-EIO);
277                 }
278         } else if (rc == 0 && S_ISDIR(mode)) {
279                 struct mdt_body *body;
280
281                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
282                 if (body == NULL) {
283                         rc = -EPROTO;
284                         CERROR("%s: cannot swab mdt_body: rc = %d\n",
285                                exp->exp_obd->obd_name, rc);
286                         RETURN(rc);
287                 }
288
289                 if ((body->mbo_valid & (OBD_MD_FLDIREA | OBD_MD_MEA)) ==
290                     (OBD_MD_FLDIREA | OBD_MD_MEA)) {
291                         void *eadata;
292
293                         /* clear valid, because mkdir doesn't need to initialize
294                          * LMV, which will be delayed to lookup.
295                          */
296                         body->mbo_valid &= ~(OBD_MD_FLDIREA | OBD_MD_MEA);
297                         mdc_update_max_ea_from_body(exp, body);
298                         /* The eadata is opaque; just check that it is there.
299                          * Eventually, obd_unpackmd() will check the contents.
300                          */
301                         eadata = req_capsule_server_sized_get(&req->rq_pill,
302                                                           &RMF_MDT_MD,
303                                                           body->mbo_eadatasize);
304                         if (eadata == NULL)
305                                 RETURN(-EPROTO);
306
307                         /* save the reply LMV EA in case we have to replay a
308                          * create for recovery.
309                          */
310                         rc = mdc_save_lmm(req, eadata, body->mbo_eadatasize);
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         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                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
332                                                 &cancels, LCK_EX,
333                                                 MDS_INODELOCK_UPDATE);
334         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
335             (fid_is_sane(&op_data->op_fid3)))
336                 /* cancel DOM lock only if it has no data to flush */
337                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
338                                                  &cancels, LCK_EX,
339                                                  op_data->op_cli_flags &
340                                                  CLI_DIRTY_DATA ?
341                                                  MDS_INODELOCK_ELC :
342                                                  MDS_INODELOCK_FULL);
343         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
344                                    &RQF_MDS_REINT_UNLINK);
345         if (req == NULL) {
346                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
347                 RETURN(-ENOMEM);
348         }
349
350         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
351                              op_data->op_namelen + 1);
352
353         /* get SELinux policy info if any */
354         rc = sptlrpc_get_sepol(req);
355         if (rc < 0) {
356                 ptlrpc_request_free(req);
357                 RETURN(rc);
358         }
359         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
360                              strlen(req->rq_sepol) ?
361                              strlen(req->rq_sepol) + 1 : 0);
362
363         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
364         if (rc) {
365                 ptlrpc_request_free(req);
366                 RETURN(rc);
367         }
368
369         mdc_unlink_pack(&req->rq_pill, op_data);
370
371         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
372                              obd->u.cli.cl_default_mds_easize);
373         ptlrpc_request_set_replen(req);
374
375         *request = req;
376
377         rc = mdc_reint(req, LUSTRE_IMP_FULL);
378         if (rc == -ERESTARTSYS)
379                 rc = 0;
380         RETURN(rc);
381 }
382
383 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
384              struct ptlrpc_request **request)
385 {
386         LIST_HEAD(cancels);
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
408         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
409                              op_data->op_namelen + 1);
410
411         /* get SELinux policy info if any */
412         rc = sptlrpc_get_sepol(req);
413         if (rc < 0) {
414                 ptlrpc_request_free(req);
415                 RETURN(rc);
416         }
417         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
418                              strlen(req->rq_sepol) ?
419                              strlen(req->rq_sepol) + 1 : 0);
420
421         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
422         if (rc) {
423                 ptlrpc_request_free(req);
424                 RETURN(rc);
425         }
426
427         mdc_link_pack(&req->rq_pill, op_data);
428         ptlrpc_request_set_replen(req);
429
430         rc = mdc_reint(req, LUSTRE_IMP_FULL);
431         *request = req;
432         if (rc == -ERESTARTSYS)
433                 rc = 0;
434
435         RETURN(rc);
436 }
437
438 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
439                 const char *old, size_t oldlen, const char *new, size_t newlen,
440                 struct ptlrpc_request **request)
441 {
442         LIST_HEAD(cancels);
443         struct obd_device *obd = exp->exp_obd;
444         struct ptlrpc_request *req;
445         int count = 0, rc;
446
447         ENTRY;
448
449         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
450             (fid_is_sane(&op_data->op_fid1)))
451                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
452                                                 &cancels, LCK_EX,
453                                                 MDS_INODELOCK_UPDATE);
454         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
455             (fid_is_sane(&op_data->op_fid2)))
456                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
457                                                  &cancels, LCK_EX,
458                                                  MDS_INODELOCK_UPDATE);
459         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
460             (fid_is_sane(&op_data->op_fid3)))
461                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
462                                                  &cancels, LCK_EX,
463                                                  MDS_INODELOCK_LOOKUP);
464         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
465             (fid_is_sane(&op_data->op_fid4)))
466                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
467                                                  &cancels, LCK_EX,
468                                                  MDS_INODELOCK_ELC);
469
470         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
471                            op_data->op_cli_flags & CLI_MIGRATE ?
472                            &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
473         if (req == NULL) {
474                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
475                 RETURN(-ENOMEM);
476         }
477
478         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
479         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
480         if (op_data->op_cli_flags & CLI_MIGRATE)
481                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
482                                      op_data->op_data_size);
483
484         /* get SELinux policy info if any */
485         rc = sptlrpc_get_sepol(req);
486         if (rc < 0) {
487                 ptlrpc_request_free(req);
488                 RETURN(rc);
489         }
490         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
491                              strlen(req->rq_sepol) ?
492                              strlen(req->rq_sepol) + 1 : 0);
493
494         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
495         if (rc) {
496                 ptlrpc_request_free(req);
497                 RETURN(rc);
498         }
499
500         if (exp_connect_cancelset(exp) && req)
501                 ldlm_cli_cancel_list(&cancels, count, req, 0);
502
503         if (op_data->op_cli_flags & CLI_MIGRATE)
504                 mdc_migrate_pack(&req->rq_pill, op_data, old, oldlen);
505         else
506                 mdc_rename_pack(&req->rq_pill, op_data, old, oldlen,
507                                 new, newlen);
508
509         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
510                              obd->u.cli.cl_default_mds_easize);
511         ptlrpc_request_set_replen(req);
512
513         rc = mdc_reint(req, LUSTRE_IMP_FULL);
514         *request = req;
515         if (rc == -ERESTARTSYS)
516                 rc = 0;
517
518         RETURN(rc);
519 }
520
521 int mdc_file_resync(struct obd_export *exp, struct md_op_data *op_data)
522 {
523         LIST_HEAD(cancels);
524         struct ptlrpc_request *req;
525         struct ldlm_lock *lock;
526         struct mdt_rec_resync *rec;
527         int count = 0, rc;
528         ENTRY;
529
530         if (op_data->op_flags & MF_MDC_CANCEL_FID1 &&
531             fid_is_sane(&op_data->op_fid1))
532                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
533                                                 &cancels, LCK_EX,
534                                                 MDS_INODELOCK_LAYOUT);
535
536         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
537                                    &RQF_MDS_REINT_RESYNC);
538         if (req == NULL) {
539                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
540                 RETURN(-ENOMEM);
541         }
542
543         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
544         if (rc) {
545                 ptlrpc_request_free(req);
546                 RETURN(rc);
547         }
548
549         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
550         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
551         rec->rs_opcode  = REINT_RESYNC;
552         rec->rs_fsuid   = op_data->op_fsuid;
553         rec->rs_fsgid   = op_data->op_fsgid;
554         rec->rs_cap     = ll_capability_u32(op_data->op_cap);
555         rec->rs_fid     = op_data->op_fid1;
556         rec->rs_bias    = op_data->op_bias;
557         rec->rs_mirror_id = op_data->op_mirror_id;
558
559         lock = ldlm_handle2lock(&op_data->op_lease_handle);
560         if (lock != NULL) {
561                 rec->rs_lease_handle = lock->l_remote_handle;
562                 LDLM_LOCK_PUT(lock);
563         }
564
565         ptlrpc_request_set_replen(req);
566
567         rc = mdc_reint(req, LUSTRE_IMP_FULL);
568         if (rc == -ERESTARTSYS)
569                 rc = 0;
570
571         ptlrpc_req_finished(req);
572         RETURN(rc);
573 }