Whamcloud - gitweb
LU-13799 llite: Implement lower/upper aio
[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, NULL, 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         ENTRY;
173
174         /* For case if upper layer did not alloc fid, do it now. */
175         if (!fid_is_sane(&op_data->op_fid2)) {
176                 /*
177                  * mdc_fid_alloc() may return errno 1 in case of switch to new
178                  * sequence, handle this.
179                  */
180                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
181                 if (rc < 0)
182                         RETURN(rc);
183         }
184
185 rebuild:
186         count = 0;
187         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
188             (fid_is_sane(&op_data->op_fid1)))
189                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
190                                                 &cancels, LCK_EX,
191                                                 MDS_INODELOCK_UPDATE);
192
193         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
194                                    &RQF_MDS_REINT_CREATE_ACL);
195         if (req == NULL) {
196                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
197                 RETURN(-ENOMEM);
198         }
199
200         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
201                              op_data->op_namelen + 1);
202         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
203                              data && datalen ? datalen : 0);
204
205         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
206                              RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
207                              strlen(op_data->op_file_secctx_name) + 1 : 0);
208
209         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
210                              op_data->op_file_secctx_size);
211
212         req_capsule_set_size(&req->rq_pill, &RMF_FILE_ENCCTX, RCL_CLIENT,
213                              op_data->op_file_encctx_size);
214
215         /* get SELinux policy info if any */
216         rc = sptlrpc_get_sepol(req);
217         if (rc < 0) {
218                 ptlrpc_request_free(req);
219                 RETURN(rc);
220         }
221         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
222                              strlen(req->rq_sepol) ?
223                              strlen(req->rq_sepol) + 1 : 0);
224
225         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
226         if (rc) {
227                 ptlrpc_request_free(req);
228                 RETURN(rc);
229         }
230
231         /*
232          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
233          * tgt, for symlinks or lov MD data.
234          */
235         mdc_create_pack(&req->rq_pill, op_data, data, datalen, mode, uid,
236                         gid, cap_effective, rdev);
237
238         ptlrpc_request_set_replen(req);
239
240         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
241          * logic here */
242         req->rq_no_retry_einprogress = 1;
243
244         if (resends) {
245                 req->rq_generation_set = 1;
246                 req->rq_import_generation = generation;
247                 req->rq_sent = ktime_get_real_seconds() + resends;
248         }
249         level = LUSTRE_IMP_FULL;
250  resend:
251         rc = mdc_reint(req, level);
252
253         /* Resend if we were told to. */
254         if (rc == -ERESTARTSYS) {
255                 level = LUSTRE_IMP_RECOVER;
256                 goto resend;
257         } else if (rc == -EINPROGRESS) {
258                 /* Retry create infinitely until succeed or get other
259                  * error code or interrupted. */
260                 ptlrpc_req_finished(req);
261                 if (generation == import->imp_generation) {
262                         if (signal_pending(current))
263                                 RETURN(-EINTR);
264
265                         resends++;
266                         CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
267                                exp->exp_obd->obd_name, resends,
268                                PFID(&op_data->op_fid1),
269                                PFID(&op_data->op_fid2));
270                         goto rebuild;
271                 } else {
272                         CDEBUG(D_HA, "resend cross eviction\n");
273                         RETURN(-EIO);
274                 }
275         }
276
277         *request = req;
278         RETURN(rc);
279 }
280
281 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
282                struct ptlrpc_request **request)
283 {
284         LIST_HEAD(cancels);
285         struct obd_device *obd = class_exp2obd(exp);
286         struct ptlrpc_request *req = *request;
287         int count = 0, rc;
288         ENTRY;
289
290         LASSERT(req == NULL);
291
292         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
293             (fid_is_sane(&op_data->op_fid1)))
294                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
295                                                 &cancels, LCK_EX,
296                                                 MDS_INODELOCK_UPDATE);
297         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
298             (fid_is_sane(&op_data->op_fid3)))
299                 /* cancel DOM lock only if it has no data to flush */
300                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
301                                                  &cancels, LCK_EX,
302                                                  op_data->op_cli_flags &
303                                                  CLI_DIRTY_DATA ?
304                                                  MDS_INODELOCK_ELC :
305                                                  MDS_INODELOCK_FULL);
306         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
307                                    &RQF_MDS_REINT_UNLINK);
308         if (req == NULL) {
309                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
310                 RETURN(-ENOMEM);
311         }
312
313         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
314                              op_data->op_namelen + 1);
315
316         /* get SELinux policy info if any */
317         rc = sptlrpc_get_sepol(req);
318         if (rc < 0) {
319                 ptlrpc_request_free(req);
320                 RETURN(rc);
321         }
322         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
323                              strlen(req->rq_sepol) ?
324                              strlen(req->rq_sepol) + 1 : 0);
325
326         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
327         if (rc) {
328                 ptlrpc_request_free(req);
329                 RETURN(rc);
330         }
331
332         mdc_unlink_pack(&req->rq_pill, op_data);
333
334         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
335                              obd->u.cli.cl_default_mds_easize);
336         ptlrpc_request_set_replen(req);
337
338         *request = req;
339
340         rc = mdc_reint(req, LUSTRE_IMP_FULL);
341         if (rc == -ERESTARTSYS)
342                 rc = 0;
343         RETURN(rc);
344 }
345
346 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
347              struct ptlrpc_request **request)
348 {
349         LIST_HEAD(cancels);
350         struct ptlrpc_request *req;
351         int count = 0, rc;
352         ENTRY;
353
354         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
355             (fid_is_sane(&op_data->op_fid2)))
356                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
357                                                 &cancels, LCK_EX,
358                                                 MDS_INODELOCK_UPDATE);
359         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
360             (fid_is_sane(&op_data->op_fid1)))
361                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
362                                                  &cancels, LCK_EX,
363                                                  MDS_INODELOCK_UPDATE);
364
365         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
366         if (req == NULL) {
367                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
368                 RETURN(-ENOMEM);
369         }
370
371         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
372                              op_data->op_namelen + 1);
373
374         /* get SELinux policy info if any */
375         rc = sptlrpc_get_sepol(req);
376         if (rc < 0) {
377                 ptlrpc_request_free(req);
378                 RETURN(rc);
379         }
380         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
381                              strlen(req->rq_sepol) ?
382                              strlen(req->rq_sepol) + 1 : 0);
383
384         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
385         if (rc) {
386                 ptlrpc_request_free(req);
387                 RETURN(rc);
388         }
389
390         mdc_link_pack(&req->rq_pill, op_data);
391         ptlrpc_request_set_replen(req);
392
393         rc = mdc_reint(req, LUSTRE_IMP_FULL);
394         *request = req;
395         if (rc == -ERESTARTSYS)
396                 rc = 0;
397
398         RETURN(rc);
399 }
400
401 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
402                 const char *old, size_t oldlen, const char *new, size_t newlen,
403                 struct ptlrpc_request **request)
404 {
405         LIST_HEAD(cancels);
406         struct obd_device *obd = exp->exp_obd;
407         struct ptlrpc_request *req;
408         int count = 0, rc;
409
410         ENTRY;
411
412         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
413             (fid_is_sane(&op_data->op_fid1)))
414                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
415                                                 &cancels, LCK_EX,
416                                                 MDS_INODELOCK_UPDATE);
417         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
418             (fid_is_sane(&op_data->op_fid2)))
419                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
420                                                  &cancels, LCK_EX,
421                                                  MDS_INODELOCK_UPDATE);
422         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
423             (fid_is_sane(&op_data->op_fid3)))
424                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
425                                                  &cancels, LCK_EX,
426                                                  MDS_INODELOCK_LOOKUP);
427         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
428             (fid_is_sane(&op_data->op_fid4)))
429                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
430                                                  &cancels, LCK_EX,
431                                                  MDS_INODELOCK_ELC);
432
433         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
434                            op_data->op_cli_flags & CLI_MIGRATE ?
435                            &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
436         if (req == NULL) {
437                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
438                 RETURN(-ENOMEM);
439         }
440
441         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
442         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
443         if (op_data->op_cli_flags & CLI_MIGRATE)
444                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
445                                      op_data->op_data_size);
446
447         /* get SELinux policy info if any */
448         rc = sptlrpc_get_sepol(req);
449         if (rc < 0) {
450                 ptlrpc_request_free(req);
451                 RETURN(rc);
452         }
453         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
454                              strlen(req->rq_sepol) ?
455                              strlen(req->rq_sepol) + 1 : 0);
456
457         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
458         if (rc) {
459                 ptlrpc_request_free(req);
460                 RETURN(rc);
461         }
462
463         if (exp_connect_cancelset(exp) && req)
464                 ldlm_cli_cancel_list(&cancels, count, req, 0);
465
466         if (op_data->op_cli_flags & CLI_MIGRATE)
467                 mdc_migrate_pack(&req->rq_pill, op_data, old, oldlen);
468         else
469                 mdc_rename_pack(&req->rq_pill, op_data, old, oldlen,
470                                 new, newlen);
471
472         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
473                              obd->u.cli.cl_default_mds_easize);
474         ptlrpc_request_set_replen(req);
475
476         rc = mdc_reint(req, LUSTRE_IMP_FULL);
477         *request = req;
478         if (rc == -ERESTARTSYS)
479                 rc = 0;
480
481         RETURN(rc);
482 }
483
484 int mdc_file_resync(struct obd_export *exp, struct md_op_data *op_data)
485 {
486         LIST_HEAD(cancels);
487         struct ptlrpc_request *req;
488         struct ldlm_lock *lock;
489         struct mdt_rec_resync *rec;
490         int count = 0, rc;
491         ENTRY;
492
493         if (op_data->op_flags & MF_MDC_CANCEL_FID1 &&
494             fid_is_sane(&op_data->op_fid1))
495                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
496                                                 &cancels, LCK_EX,
497                                                 MDS_INODELOCK_LAYOUT);
498
499         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
500                                    &RQF_MDS_REINT_RESYNC);
501         if (req == NULL) {
502                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
503                 RETURN(-ENOMEM);
504         }
505
506         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
507         if (rc) {
508                 ptlrpc_request_free(req);
509                 RETURN(rc);
510         }
511
512         BUILD_BUG_ON(sizeof(*rec) != sizeof(struct mdt_rec_reint));
513         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
514         rec->rs_opcode  = REINT_RESYNC;
515         rec->rs_fsuid   = op_data->op_fsuid;
516         rec->rs_fsgid   = op_data->op_fsgid;
517         rec->rs_cap     = op_data->op_cap.cap[0];
518         rec->rs_fid     = op_data->op_fid1;
519         rec->rs_bias    = op_data->op_bias;
520         rec->rs_mirror_id = op_data->op_mirror_id;
521
522         lock = ldlm_handle2lock(&op_data->op_lease_handle);
523         if (lock != NULL) {
524                 rec->rs_lease_handle = lock->l_remote_handle;
525                 LDLM_LOCK_PUT(lock);
526         }
527
528         ptlrpc_request_set_replen(req);
529
530         rc = mdc_reint(req, LUSTRE_IMP_FULL);
531         if (rc == -ERESTARTSYS)
532                 rc = 0;
533
534         ptlrpc_req_finished(req);
535         RETURN(rc);
536 }