Whamcloud - gitweb
LU-6142 mdc: minor function cleanups.
[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         ptlrpc_get_mod_rpc_slot(request);
50         rc = ptlrpc_queue_wait(request);
51         ptlrpc_put_mod_rpc_slot(request);
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_res(struct obd_export *exp,
64                                 struct ldlm_res_id *res_id,
65                                 struct list_head *cancels,
66                                 enum ldlm_mode mode, __u64 bits)
67 {
68         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
69         union ldlm_policy_data policy = { { 0 } };
70         struct ldlm_resource *res;
71         int count;
72
73         ENTRY;
74
75         /* Return, i.e. cancel nothing, only if ELC is supported (flag in
76          * export) but disabled through procfs (flag in NS).
77          *
78          * This distinguishes from a case when ELC is not supported originally,
79          * when we still want to cancel locks in advance and just cancel them
80          * locally, without sending any RPC. */
81         if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
82                 RETURN(0);
83
84         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
85         if (IS_ERR(res))
86                 RETURN(0);
87         LDLM_RESOURCE_ADDREF(res);
88         /* Initialize ibits lock policy. */
89         policy.l_inodebits.bits = bits;
90         count = ldlm_cancel_resource_local(res, cancels, &policy, mode, 0, 0,
91                                            NULL);
92         LDLM_RESOURCE_DELREF(res);
93         ldlm_resource_putref(res);
94         RETURN(count);
95 }
96
97 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
98                             struct list_head *cancels, enum ldlm_mode mode,
99                             __u64 bits)
100 {
101         struct ldlm_res_id res_id;
102
103         fid_build_reg_res_name(fid, &res_id);
104         return mdc_resource_get_unused_res(exp, &res_id, cancels, mode, bits);
105 }
106
107 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
108                 void *ea, size_t ealen, struct ptlrpc_request **request)
109 {
110         LIST_HEAD(cancels);
111         struct ptlrpc_request *req;
112         int count = 0, rc;
113         __u64 bits;
114         ENTRY;
115
116         LASSERT(op_data != NULL);
117
118         bits = MDS_INODELOCK_UPDATE;
119         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
120                 bits |= MDS_INODELOCK_LOOKUP;
121         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
122             (fid_is_sane(&op_data->op_fid1)))
123                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
124                                                 &cancels, LCK_EX, bits);
125         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
126                                    &RQF_MDS_REINT_SETATTR);
127         if (req == NULL) {
128                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
129                 RETURN(-ENOMEM);
130         }
131
132         req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
133         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
134         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
135
136         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
137         if (rc) {
138                 ptlrpc_request_free(req);
139                 RETURN(rc);
140         }
141
142         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
143                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld\n",
144                        (s64)op_data->op_attr.ia_mtime.tv_sec,
145                        (s64)op_data->op_attr.ia_ctime.tv_sec);
146         mdc_setattr_pack(req, op_data, ea, ealen);
147
148         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER, 0);
149
150         ptlrpc_request_set_replen(req);
151
152         rc = mdc_reint(req, LUSTRE_IMP_FULL);
153         if (rc == -ERESTARTSYS)
154                 rc = 0;
155
156         *request = req;
157
158         RETURN(rc);
159 }
160
161 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
162                 const void *data, size_t datalen,
163                 umode_t mode, uid_t uid, gid_t gid,
164                 cfs_cap_t cap_effective, __u64 rdev,
165                 struct ptlrpc_request **request)
166 {
167         struct ptlrpc_request *req;
168         int level, rc;
169         int count, resends = 0;
170         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
171         int generation = import->imp_generation;
172         LIST_HEAD(cancels);
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, op_data, data, datalen, mode, uid,
237                         gid, cap_effective, rdev);
238
239         ptlrpc_request_set_replen(req);
240
241         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
242          * logic here */
243         req->rq_no_retry_einprogress = 1;
244
245         if (resends) {
246                 req->rq_generation_set = 1;
247                 req->rq_import_generation = generation;
248                 req->rq_sent = ktime_get_real_seconds() + resends;
249         }
250         level = LUSTRE_IMP_FULL;
251  resend:
252         rc = mdc_reint(req, level);
253
254         /* Resend if we were told to. */
255         if (rc == -ERESTARTSYS) {
256                 level = LUSTRE_IMP_RECOVER;
257                 goto resend;
258         } else if (rc == -EINPROGRESS) {
259                 /* Retry create infinitely until succeed or get other
260                  * error code or interrupted. */
261                 ptlrpc_req_finished(req);
262                 if (generation == import->imp_generation) {
263                         if (signal_pending(current))
264                                 RETURN(-EINTR);
265
266                         resends++;
267                         CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
268                                exp->exp_obd->obd_name, resends,
269                                PFID(&op_data->op_fid1),
270                                PFID(&op_data->op_fid2));
271                         goto rebuild;
272                 } else {
273                         CDEBUG(D_HA, "resend cross eviction\n");
274                         RETURN(-EIO);
275                 }
276         }
277
278         *request = req;
279         RETURN(rc);
280 }
281
282 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
283                struct ptlrpc_request **request)
284 {
285         LIST_HEAD(cancels);
286         struct obd_device *obd = class_exp2obd(exp);
287         struct ptlrpc_request *req = *request;
288         int count = 0, rc;
289         ENTRY;
290
291         LASSERT(req == NULL);
292
293         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
294             (fid_is_sane(&op_data->op_fid1)))
295                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
296                                                 &cancels, LCK_EX,
297                                                 MDS_INODELOCK_UPDATE);
298         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
299             (fid_is_sane(&op_data->op_fid3)))
300                 /* cancel DOM lock only if it has no data to flush */
301                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
302                                                  &cancels, LCK_EX,
303                                                  op_data->op_cli_flags &
304                                                  CLI_DIRTY_DATA ?
305                                                  MDS_INODELOCK_ELC :
306                                                  MDS_INODELOCK_FULL);
307         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
308                                    &RQF_MDS_REINT_UNLINK);
309         if (req == NULL) {
310                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
311                 RETURN(-ENOMEM);
312         }
313
314         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
315                              op_data->op_namelen + 1);
316
317         /* get SELinux policy info if any */
318         rc = sptlrpc_get_sepol(req);
319         if (rc < 0) {
320                 ptlrpc_request_free(req);
321                 RETURN(rc);
322         }
323         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
324                              strlen(req->rq_sepol) ?
325                              strlen(req->rq_sepol) + 1 : 0);
326
327         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
328         if (rc) {
329                 ptlrpc_request_free(req);
330                 RETURN(rc);
331         }
332
333         mdc_unlink_pack(req, op_data);
334
335         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
336                              obd->u.cli.cl_default_mds_easize);
337         ptlrpc_request_set_replen(req);
338
339         *request = req;
340
341         rc = mdc_reint(req, LUSTRE_IMP_FULL);
342         if (rc == -ERESTARTSYS)
343                 rc = 0;
344         RETURN(rc);
345 }
346
347 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
348              struct ptlrpc_request **request)
349 {
350         LIST_HEAD(cancels);
351         struct ptlrpc_request *req;
352         int count = 0, rc;
353         ENTRY;
354
355         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
356             (fid_is_sane(&op_data->op_fid2)))
357                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
358                                                 &cancels, LCK_EX,
359                                                 MDS_INODELOCK_UPDATE);
360         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
361             (fid_is_sane(&op_data->op_fid1)))
362                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
363                                                  &cancels, LCK_EX,
364                                                  MDS_INODELOCK_UPDATE);
365
366         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
367         if (req == NULL) {
368                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
369                 RETURN(-ENOMEM);
370         }
371
372         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
373                              op_data->op_namelen + 1);
374
375         /* get SELinux policy info if any */
376         rc = sptlrpc_get_sepol(req);
377         if (rc < 0) {
378                 ptlrpc_request_free(req);
379                 RETURN(rc);
380         }
381         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
382                              strlen(req->rq_sepol) ?
383                              strlen(req->rq_sepol) + 1 : 0);
384
385         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
386         if (rc) {
387                 ptlrpc_request_free(req);
388                 RETURN(rc);
389         }
390
391         mdc_link_pack(req, op_data);
392         ptlrpc_request_set_replen(req);
393
394         rc = mdc_reint(req, LUSTRE_IMP_FULL);
395         *request = req;
396         if (rc == -ERESTARTSYS)
397                 rc = 0;
398
399         RETURN(rc);
400 }
401
402 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
403                 const char *old, size_t oldlen, const char *new, size_t newlen,
404                 struct ptlrpc_request **request)
405 {
406         LIST_HEAD(cancels);
407         struct obd_device *obd = exp->exp_obd;
408         struct ptlrpc_request *req;
409         int count = 0, rc;
410
411         ENTRY;
412
413         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
414             (fid_is_sane(&op_data->op_fid1)))
415                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
416                                                 &cancels, LCK_EX,
417                                                 MDS_INODELOCK_UPDATE);
418         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
419             (fid_is_sane(&op_data->op_fid2)))
420                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
421                                                  &cancels, LCK_EX,
422                                                  MDS_INODELOCK_UPDATE);
423         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
424             (fid_is_sane(&op_data->op_fid3)))
425                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
426                                                  &cancels, LCK_EX,
427                                                  MDS_INODELOCK_LOOKUP);
428         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
429             (fid_is_sane(&op_data->op_fid4)))
430                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
431                                                  &cancels, LCK_EX,
432                                                  MDS_INODELOCK_ELC);
433
434         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
435                            op_data->op_cli_flags & CLI_MIGRATE ?
436                            &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
437         if (req == NULL) {
438                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
439                 RETURN(-ENOMEM);
440         }
441
442         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
443         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
444         if (op_data->op_cli_flags & CLI_MIGRATE)
445                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
446                                      op_data->op_data_size);
447
448         /* get SELinux policy info if any */
449         rc = sptlrpc_get_sepol(req);
450         if (rc < 0) {
451                 ptlrpc_request_free(req);
452                 RETURN(rc);
453         }
454         req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
455                              strlen(req->rq_sepol) ?
456                              strlen(req->rq_sepol) + 1 : 0);
457
458         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
459         if (rc) {
460                 ptlrpc_request_free(req);
461                 RETURN(rc);
462         }
463
464         if (exp_connect_cancelset(exp) && req)
465                 ldlm_cli_cancel_list(&cancels, count, req, 0);
466
467         if (op_data->op_cli_flags & CLI_MIGRATE)
468                 mdc_migrate_pack(req, op_data, old, oldlen);
469         else
470                 mdc_rename_pack(req, op_data, old, oldlen, 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;
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 }