Whamcloud - gitweb
375d0c8fd42462de17c63bb6156587d0d3bdb7d0
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDC
38
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41
42 #include <obd_class.h>
43 #include "mdc_internal.h"
44 #include <lustre_fid.h>
45
46 /* mdc_setattr does its own semaphore handling */
47 static int mdc_reint(struct ptlrpc_request *request,
48                      struct mdc_rpc_lock *rpc_lock,
49                      int level)
50 {
51         int rc;
52
53         request->rq_send_state = level;
54
55         mdc_get_rpc_lock(rpc_lock, NULL);
56         rc = ptlrpc_queue_wait(request);
57         mdc_put_rpc_lock(rpc_lock, NULL);
58         if (rc)
59                 CDEBUG(D_INFO, "error in handling %d\n", rc);
60         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
61                 rc = -EPROTO;
62         }
63         return rc;
64 }
65
66 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
67  * found by @fid. Found locks are added into @cancel list. Returns the amount of
68  * locks added to @cancels list. */
69 int mdc_resource_get_unused(struct obd_export *exp, const struct lu_fid *fid,
70                             struct list_head *cancels, ldlm_mode_t mode,
71                             __u64 bits)
72 {
73         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
74         ldlm_policy_data_t policy = { {0} };
75         struct ldlm_res_id res_id;
76         struct ldlm_resource *res;
77         int count;
78         ENTRY;
79
80         /* Return, i.e. cancel nothing, only if ELC is supported (flag in
81          * export) but disabled through procfs (flag in NS).
82          *
83          * This distinguishes from a case when ELC is not supported originally,
84          * when we still want to cancel locks in advance and just cancel them
85          * locally, without sending any RPC. */
86         if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
87                 RETURN(0);
88
89         fid_build_reg_res_name(fid, &res_id);
90         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
91                                 NULL, &res_id, 0, 0);
92         if (IS_ERR(res))
93                 RETURN(0);
94         LDLM_RESOURCE_ADDREF(res);
95         /* Initialize ibits lock policy. */
96         policy.l_inodebits.bits = bits;
97         count = ldlm_cancel_resource_local(res, cancels, &policy,
98                                            mode, 0, 0, NULL);
99         LDLM_RESOURCE_DELREF(res);
100         ldlm_resource_putref(res);
101         RETURN(count);
102 }
103
104 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
105                 void *ea, size_t ealen, struct ptlrpc_request **request)
106 {
107         struct list_head cancels = LIST_HEAD_INIT(cancels);
108         struct ptlrpc_request *req;
109         struct mdc_rpc_lock *rpc_lock;
110         struct obd_device *obd = exp->exp_obd;
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         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
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         rpc_lock = obd->u.cli.cl_rpc_lock;
142
143         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
144                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
145                        ", ctime "CFS_TIME_T"\n",
146                        LTIME_S(op_data->op_attr.ia_mtime),
147                        LTIME_S(op_data->op_attr.ia_ctime));
148         mdc_setattr_pack(req, op_data, ea, ealen);
149
150         ptlrpc_request_set_replen(req);
151
152         rc = mdc_reint(req, rpc_lock, 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         struct list_head cancels = LIST_HEAD_INIT(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_RMT_ACL);
196         if (req == NULL) {
197                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
198                 RETURN(-ENOMEM);
199         }
200         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
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         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
207         if (rc) {
208                 ptlrpc_request_free(req);
209                 RETURN(rc);
210         }
211
212         /*
213          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
214          * tgt, for symlinks or lov MD data.
215          */
216         mdc_create_pack(req, op_data, data, datalen, mode, uid,
217                         gid, cap_effective, rdev);
218
219         ptlrpc_request_set_replen(req);
220
221         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
222          * logic here */
223         req->rq_no_retry_einprogress = 1;
224
225         if (resends) {
226                 req->rq_generation_set = 1;
227                 req->rq_import_generation = generation;
228                 req->rq_sent = cfs_time_current_sec() + resends;
229         }
230         level = LUSTRE_IMP_FULL;
231  resend:
232         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
233
234         /* Resend if we were told to. */
235         if (rc == -ERESTARTSYS) {
236                 level = LUSTRE_IMP_RECOVER;
237                 goto resend;
238         } else if (rc == -EINPROGRESS) {
239                 /* Retry create infinitely until succeed or get other
240                  * error code. */
241                 ptlrpc_req_finished(req);
242                 resends++;
243
244                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
245                        exp->exp_obd->obd_name, resends,
246                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
247
248                 if (generation == import->imp_generation) {
249                         goto rebuild;
250                 } else {
251                         CDEBUG(D_HA, "resend cross eviction\n");
252                         RETURN(-EIO);
253                 }
254         } else if (rc == 0) {
255                 struct mdt_body *body;
256                 struct lustre_capa *capa;
257
258                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
259                 LASSERT(body);
260                 if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
261                         capa = req_capsule_server_get(&req->rq_pill,
262                                                       &RMF_CAPA1);
263                         if (capa == NULL)
264                                 rc = -EPROTO;
265                 }
266         }
267
268         *request = req;
269         RETURN(rc);
270 }
271
272 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
273                struct ptlrpc_request **request)
274 {
275         struct list_head cancels = LIST_HEAD_INIT(cancels);
276         struct obd_device *obd = class_exp2obd(exp);
277         struct ptlrpc_request *req = *request;
278         int count = 0, rc;
279         ENTRY;
280
281         LASSERT(req == NULL);
282
283         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
284             (fid_is_sane(&op_data->op_fid1)))
285                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
286                                                 &cancels, LCK_EX,
287                                                 MDS_INODELOCK_UPDATE);
288         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
289             (fid_is_sane(&op_data->op_fid3)))
290                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
291                                                  &cancels, LCK_EX,
292                                                  MDS_INODELOCK_FULL);
293         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
294                                    &RQF_MDS_REINT_UNLINK);
295         if (req == NULL) {
296                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
297                 RETURN(-ENOMEM);
298         }
299         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
300         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
301                              op_data->op_namelen + 1);
302
303         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
304         if (rc) {
305                 ptlrpc_request_free(req);
306                 RETURN(rc);
307         }
308
309         mdc_unlink_pack(req, op_data);
310
311         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
312                              obd->u.cli.cl_default_mds_easize);
313         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
314                              obd->u.cli.cl_default_mds_cookiesize);
315         ptlrpc_request_set_replen(req);
316
317         *request = req;
318
319         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
320         if (rc == -ERESTARTSYS)
321                 rc = 0;
322         RETURN(rc);
323 }
324
325 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
326              struct ptlrpc_request **request)
327 {
328         struct list_head cancels = LIST_HEAD_INIT(cancels);
329         struct obd_device *obd = exp->exp_obd;
330         struct ptlrpc_request *req;
331         int count = 0, rc;
332         ENTRY;
333
334         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
335             (fid_is_sane(&op_data->op_fid2)))
336                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
337                                                 &cancels, LCK_EX,
338                                                 MDS_INODELOCK_UPDATE);
339         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
340             (fid_is_sane(&op_data->op_fid1)))
341                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
342                                                  &cancels, LCK_EX,
343                                                  MDS_INODELOCK_UPDATE);
344
345         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
346         if (req == NULL) {
347                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
348                 RETURN(-ENOMEM);
349         }
350         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
351         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
352         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
353                              op_data->op_namelen + 1);
354
355         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
356         if (rc) {
357                 ptlrpc_request_free(req);
358                 RETURN(rc);
359         }
360
361         mdc_link_pack(req, op_data);
362         ptlrpc_request_set_replen(req);
363
364         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
365         *request = req;
366         if (rc == -ERESTARTSYS)
367                 rc = 0;
368
369         RETURN(rc);
370 }
371
372 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
373                 const char *old, size_t oldlen, const char *new, size_t newlen,
374                 struct ptlrpc_request **request)
375 {
376         struct list_head cancels = LIST_HEAD_INIT(cancels);
377         struct obd_device *obd = exp->exp_obd;
378         struct ptlrpc_request *req;
379         int count = 0, rc;
380         ENTRY;
381
382         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
383             (fid_is_sane(&op_data->op_fid1)))
384                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
385                                                 &cancels, LCK_EX,
386                                                 MDS_INODELOCK_UPDATE);
387         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
388             (fid_is_sane(&op_data->op_fid2)))
389                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
390                                                  &cancels, LCK_EX,
391                                                  MDS_INODELOCK_UPDATE);
392         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
393             (fid_is_sane(&op_data->op_fid3)))
394                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
395                                                  &cancels, LCK_EX,
396                                                  MDS_INODELOCK_LOOKUP);
397         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
398              (fid_is_sane(&op_data->op_fid4)))
399                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
400                                                  &cancels, LCK_EX,
401                                                  MDS_INODELOCK_FULL);
402
403         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
404                                    &RQF_MDS_REINT_RENAME);
405         if (req == NULL) {
406                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
407                 RETURN(-ENOMEM);
408         }
409
410         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
411         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
412         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
413         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
414
415         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
416         if (rc) {
417                 ptlrpc_request_free(req);
418                 RETURN(rc);
419         }
420
421         if (exp_connect_cancelset(exp) && req)
422                 ldlm_cli_cancel_list(&cancels, count, req, 0);
423
424         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
425
426         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
427                              obd->u.cli.cl_default_mds_easize);
428         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
429                              obd->u.cli.cl_default_mds_cookiesize);
430         ptlrpc_request_set_replen(req);
431
432         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
433         *request = req;
434         if (rc == -ERESTARTSYS)
435                 rc = 0;
436
437         RETURN(rc);
438 }