Whamcloud - gitweb
LU-4423 ptlrpc: use 64-bit times for request times
[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, 2016, 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 "CFS_TIME_T
135                        ", ctime "CFS_TIME_T"\n",
136                        LTIME_S(op_data->op_attr.ia_mtime),
137                        LTIME_S(op_data->op_attr.ia_ctime));
138         mdc_setattr_pack(req, op_data, ea, ealen);
139
140         ptlrpc_request_set_replen(req);
141
142         rc = mdc_reint(req, LUSTRE_IMP_FULL);
143         if (rc == -ERESTARTSYS)
144                 rc = 0;
145
146         *request = req;
147
148         RETURN(rc);
149 }
150
151 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
152                 const void *data, size_t datalen,
153                 umode_t mode, uid_t uid, gid_t gid,
154                 cfs_cap_t cap_effective, __u64 rdev,
155                 struct ptlrpc_request **request)
156 {
157         struct ptlrpc_request *req;
158         int level, rc;
159         int count, resends = 0;
160         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
161         int generation = import->imp_generation;
162         struct list_head cancels = LIST_HEAD_INIT(cancels);
163         ENTRY;
164
165         /* For case if upper layer did not alloc fid, do it now. */
166         if (!fid_is_sane(&op_data->op_fid2)) {
167                 /*
168                  * mdc_fid_alloc() may return errno 1 in case of switch to new
169                  * sequence, handle this.
170                  */
171                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
172                 if (rc < 0)
173                         RETURN(rc);
174         }
175
176 rebuild:
177         count = 0;
178         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
179             (fid_is_sane(&op_data->op_fid1)))
180                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
181                                                 &cancels, LCK_EX,
182                                                 MDS_INODELOCK_UPDATE);
183
184         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
185                                    &RQF_MDS_REINT_CREATE_ACL);
186         if (req == NULL) {
187                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
188                 RETURN(-ENOMEM);
189         }
190
191         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
192                              op_data->op_namelen + 1);
193         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
194                              data && datalen ? datalen : 0);
195
196         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
197                              RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
198                              strlen(op_data->op_file_secctx_name) + 1 : 0);
199
200         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
201                              op_data->op_file_secctx_size);
202
203         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
204         if (rc) {
205                 ptlrpc_request_free(req);
206                 RETURN(rc);
207         }
208
209         /*
210          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
211          * tgt, for symlinks or lov MD data.
212          */
213         mdc_create_pack(req, op_data, data, datalen, mode, uid,
214                         gid, cap_effective, rdev);
215
216         ptlrpc_request_set_replen(req);
217
218         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
219          * logic here */
220         req->rq_no_retry_einprogress = 1;
221
222         if (resends) {
223                 req->rq_generation_set = 1;
224                 req->rq_import_generation = generation;
225                 req->rq_sent = ktime_get_real_seconds() + resends;
226         }
227         level = LUSTRE_IMP_FULL;
228  resend:
229         rc = mdc_reint(req, level);
230
231         /* Resend if we were told to. */
232         if (rc == -ERESTARTSYS) {
233                 level = LUSTRE_IMP_RECOVER;
234                 goto resend;
235         } else if (rc == -EINPROGRESS) {
236                 /* Retry create infinitely until succeed or get other
237                  * error code. */
238                 ptlrpc_req_finished(req);
239                 resends++;
240
241                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
242                        exp->exp_obd->obd_name, resends,
243                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
244
245                 if (generation == import->imp_generation) {
246                         goto rebuild;
247                 } else {
248                         CDEBUG(D_HA, "resend cross eviction\n");
249                         RETURN(-EIO);
250                 }
251         }
252
253         *request = req;
254         RETURN(rc);
255 }
256
257 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
258                struct ptlrpc_request **request)
259 {
260         struct list_head cancels = LIST_HEAD_INIT(cancels);
261         struct obd_device *obd = class_exp2obd(exp);
262         struct ptlrpc_request *req = *request;
263         int count = 0, rc;
264         ENTRY;
265
266         LASSERT(req == NULL);
267
268         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
269             (fid_is_sane(&op_data->op_fid1)))
270                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
271                                                 &cancels, LCK_EX,
272                                                 MDS_INODELOCK_UPDATE);
273         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
274             (fid_is_sane(&op_data->op_fid3)))
275                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
276                                                  &cancels, LCK_EX,
277                                                  MDS_INODELOCK_FULL);
278         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
279                                    &RQF_MDS_REINT_UNLINK);
280         if (req == NULL) {
281                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
282                 RETURN(-ENOMEM);
283         }
284
285         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
286                              op_data->op_namelen + 1);
287
288         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
289         if (rc) {
290                 ptlrpc_request_free(req);
291                 RETURN(rc);
292         }
293
294         mdc_unlink_pack(req, op_data);
295
296         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
297                              obd->u.cli.cl_default_mds_easize);
298         ptlrpc_request_set_replen(req);
299
300         *request = req;
301
302         rc = mdc_reint(req, LUSTRE_IMP_FULL);
303         if (rc == -ERESTARTSYS)
304                 rc = 0;
305         RETURN(rc);
306 }
307
308 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
309              struct ptlrpc_request **request)
310 {
311         struct list_head cancels = LIST_HEAD_INIT(cancels);
312         struct ptlrpc_request *req;
313         int count = 0, rc;
314         ENTRY;
315
316         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
317             (fid_is_sane(&op_data->op_fid2)))
318                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
319                                                 &cancels, LCK_EX,
320                                                 MDS_INODELOCK_UPDATE);
321         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
322             (fid_is_sane(&op_data->op_fid1)))
323                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
324                                                  &cancels, LCK_EX,
325                                                  MDS_INODELOCK_UPDATE);
326
327         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
328         if (req == NULL) {
329                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
330                 RETURN(-ENOMEM);
331         }
332
333         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
334                              op_data->op_namelen + 1);
335
336         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
337         if (rc) {
338                 ptlrpc_request_free(req);
339                 RETURN(rc);
340         }
341
342         mdc_link_pack(req, op_data);
343         ptlrpc_request_set_replen(req);
344
345         rc = mdc_reint(req, LUSTRE_IMP_FULL);
346         *request = req;
347         if (rc == -ERESTARTSYS)
348                 rc = 0;
349
350         RETURN(rc);
351 }
352
353 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
354                 const char *old, size_t oldlen, const char *new, size_t newlen,
355                 struct ptlrpc_request **request)
356 {
357         struct list_head cancels = LIST_HEAD_INIT(cancels);
358         struct obd_device *obd = exp->exp_obd;
359         struct ptlrpc_request *req;
360         int count = 0, rc;
361         ENTRY;
362
363         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
364             (fid_is_sane(&op_data->op_fid1)))
365                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
366                                                 &cancels, LCK_EX,
367                                                 MDS_INODELOCK_UPDATE);
368         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
369             (fid_is_sane(&op_data->op_fid2)))
370                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
371                                                  &cancels, LCK_EX,
372                                                  MDS_INODELOCK_UPDATE);
373         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
374             (fid_is_sane(&op_data->op_fid3)))
375                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
376                                                  &cancels, LCK_EX,
377                                                  MDS_INODELOCK_LOOKUP);
378         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
379              (fid_is_sane(&op_data->op_fid4)))
380                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
381                                                  &cancels, LCK_EX,
382                                                  MDS_INODELOCK_FULL);
383
384         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
385                            op_data->op_cli_flags & CLI_MIGRATE ?
386                            &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
387         if (req == NULL) {
388                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
389                 RETURN(-ENOMEM);
390         }
391
392         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
393         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
394
395         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
396         if (rc) {
397                 ptlrpc_request_free(req);
398                 RETURN(rc);
399         }
400
401         if (op_data->op_cli_flags & CLI_MIGRATE && op_data->op_data != NULL) {
402                 struct md_open_data *mod = op_data->op_data;
403
404                 LASSERTF(mod->mod_open_req != NULL &&
405                          mod->mod_open_req->rq_type != LI_POISON,
406                          "POISONED open %p!\n", mod->mod_open_req);
407
408                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
409                 /* We no longer want to preserve this open for replay even
410                  * though the open was committed. b=3632, b=3633 */
411                 spin_lock(&mod->mod_open_req->rq_lock);
412                 mod->mod_open_req->rq_replay = 0;
413                 spin_unlock(&mod->mod_open_req->rq_lock);
414         }
415
416         if (exp_connect_cancelset(exp) && req)
417                 ldlm_cli_cancel_list(&cancels, count, req, 0);
418
419         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
420
421         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
422                              obd->u.cli.cl_default_mds_easize);
423         ptlrpc_request_set_replen(req);
424
425         rc = mdc_reint(req, LUSTRE_IMP_FULL);
426         *request = req;
427         if (rc == -ERESTARTSYS)
428                 rc = 0;
429
430         RETURN(rc);
431 }