Whamcloud - gitweb
LU-5577 mdc: fix comparison between signed and unsigned
[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, 2013, 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, void *ea2, size_t ea2len,
106                 struct ptlrpc_request **request, struct md_open_data **mod)
107 {
108         struct list_head cancels = LIST_HEAD_INIT(cancels);
109         struct ptlrpc_request *req;
110         struct mdc_rpc_lock *rpc_lock;
111         struct obd_device *obd = exp->exp_obd;
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         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
132         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
133                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
134                                      0);
135         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
136         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
137                              ea2len);
138
139         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
140         if (rc) {
141                 ptlrpc_request_free(req);
142                 RETURN(rc);
143         }
144
145         rpc_lock = obd->u.cli.cl_rpc_lock;
146
147         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
148                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
149                        ", ctime "CFS_TIME_T"\n",
150                        LTIME_S(op_data->op_attr.ia_mtime),
151                        LTIME_S(op_data->op_attr.ia_ctime));
152         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
153
154         ptlrpc_request_set_replen(req);
155         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
156             req->rq_import->imp_replayable)
157         {
158                 LASSERT(*mod == NULL);
159
160                 *mod = obd_mod_alloc();
161                 if (*mod == NULL) {
162                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
163                                   "md_open_data");
164                 } else {
165                         req->rq_replay = 1;
166                         req->rq_cb_data = *mod;
167                         (*mod)->mod_open_req = req;
168                         req->rq_commit_cb = mdc_commit_open;
169                         (*mod)->mod_is_create = true;
170                         /**
171                          * Take an extra reference on \var mod, it protects \var
172                          * mod from being freed on eviction (commit callback is
173                          * called despite rq_replay flag).
174                          * Will be put on mdc_done_writing().
175                          */
176                         obd_mod_get(*mod);
177                 }
178         }
179
180         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
181
182         /* Save the obtained info in the original RPC for the replay case. */
183         if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
184                 struct mdt_ioepoch *epoch;
185                 struct mdt_body  *body;
186
187                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
188                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
189                 LASSERT(epoch != NULL);
190                 LASSERT(body != NULL);
191                 epoch->handle = body->mbo_handle;
192                 epoch->ioepoch = body->mbo_ioepoch;
193                 req->rq_replay_cb = mdc_replay_open;
194         /** bug 3633, open may be committed and estale answer is not error */
195         } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
196                 rc = 0;
197         } else if (rc == -ERESTARTSYS) {
198                 rc = 0;
199         }
200         *request = req;
201         if (rc && req->rq_commit_cb) {
202                 /* Put an extra reference on \var mod on error case. */
203                 if (mod != NULL && *mod != NULL)
204                         obd_mod_put(*mod);
205                 req->rq_commit_cb(req);
206         }
207         RETURN(rc);
208 }
209
210 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
211                 const void *data, size_t datalen,
212                 umode_t mode, uid_t uid, gid_t gid,
213                 cfs_cap_t cap_effective, __u64 rdev,
214                 struct ptlrpc_request **request)
215 {
216         struct ptlrpc_request *req;
217         int level, rc;
218         int count, resends = 0;
219         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
220         int generation = import->imp_generation;
221         struct list_head cancels = LIST_HEAD_INIT(cancels);
222         ENTRY;
223
224         /* For case if upper layer did not alloc fid, do it now. */
225         if (!fid_is_sane(&op_data->op_fid2)) {
226                 /*
227                  * mdc_fid_alloc() may return errno 1 in case of switch to new
228                  * sequence, handle this.
229                  */
230                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
231                 if (rc < 0)
232                         RETURN(rc);
233         }
234
235 rebuild:
236         count = 0;
237         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
238             (fid_is_sane(&op_data->op_fid1)))
239                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
240                                                 &cancels, LCK_EX,
241                                                 MDS_INODELOCK_UPDATE);
242
243         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
244                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
245         if (req == NULL) {
246                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
247                 RETURN(-ENOMEM);
248         }
249         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
250         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
251                              op_data->op_namelen + 1);
252         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
253                              data && datalen ? datalen : 0);
254
255         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
256         if (rc) {
257                 ptlrpc_request_free(req);
258                 RETURN(rc);
259         }
260
261         /*
262          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
263          * tgt, for symlinks or lov MD data.
264          */
265         mdc_create_pack(req, op_data, data, datalen, mode, uid,
266                         gid, cap_effective, rdev);
267
268         ptlrpc_request_set_replen(req);
269
270         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
271          * logic here */
272         req->rq_no_retry_einprogress = 1;
273
274         if (resends) {
275                 req->rq_generation_set = 1;
276                 req->rq_import_generation = generation;
277                 req->rq_sent = cfs_time_current_sec() + resends;
278         }
279         level = LUSTRE_IMP_FULL;
280  resend:
281         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
282
283         /* Resend if we were told to. */
284         if (rc == -ERESTARTSYS) {
285                 level = LUSTRE_IMP_RECOVER;
286                 goto resend;
287         } else if (rc == -EINPROGRESS) {
288                 /* Retry create infinitely until succeed or get other
289                  * error code. */
290                 ptlrpc_req_finished(req);
291                 resends++;
292
293                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
294                        exp->exp_obd->obd_name, resends,
295                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
296
297                 if (generation == import->imp_generation) {
298                         goto rebuild;
299                 } else {
300                         CDEBUG(D_HA, "resend cross eviction\n");
301                         RETURN(-EIO);
302                 }
303         } else if (rc == 0) {
304                 struct mdt_body *body;
305                 struct lustre_capa *capa;
306
307                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
308                 LASSERT(body);
309                 if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
310                         capa = req_capsule_server_get(&req->rq_pill,
311                                                       &RMF_CAPA1);
312                         if (capa == NULL)
313                                 rc = -EPROTO;
314                 }
315         }
316
317         *request = req;
318         RETURN(rc);
319 }
320
321 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
322                struct ptlrpc_request **request)
323 {
324         struct list_head cancels = LIST_HEAD_INIT(cancels);
325         struct obd_device *obd = class_exp2obd(exp);
326         struct ptlrpc_request *req = *request;
327         int count = 0, rc;
328         ENTRY;
329
330         LASSERT(req == NULL);
331
332         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
333             (fid_is_sane(&op_data->op_fid1)))
334                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
335                                                 &cancels, LCK_EX,
336                                                 MDS_INODELOCK_UPDATE);
337         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
338             (fid_is_sane(&op_data->op_fid3)))
339                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
340                                                  &cancels, LCK_EX,
341                                                  MDS_INODELOCK_FULL);
342         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
343                                    &RQF_MDS_REINT_UNLINK);
344         if (req == NULL) {
345                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
346                 RETURN(-ENOMEM);
347         }
348         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
349         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
350                              op_data->op_namelen + 1);
351
352         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
353         if (rc) {
354                 ptlrpc_request_free(req);
355                 RETURN(rc);
356         }
357
358         mdc_unlink_pack(req, op_data);
359
360         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
361                              obd->u.cli.cl_default_mds_easize);
362         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
363                              obd->u.cli.cl_default_mds_cookiesize);
364         ptlrpc_request_set_replen(req);
365
366         *request = req;
367
368         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
369         if (rc == -ERESTARTSYS)
370                 rc = 0;
371         RETURN(rc);
372 }
373
374 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
375              struct ptlrpc_request **request)
376 {
377         struct list_head cancels = LIST_HEAD_INIT(cancels);
378         struct obd_device *obd = exp->exp_obd;
379         struct ptlrpc_request *req;
380         int count = 0, rc;
381         ENTRY;
382
383         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
384             (fid_is_sane(&op_data->op_fid2)))
385                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
386                                                 &cancels, LCK_EX,
387                                                 MDS_INODELOCK_UPDATE);
388         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
389             (fid_is_sane(&op_data->op_fid1)))
390                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
391                                                  &cancels, LCK_EX,
392                                                  MDS_INODELOCK_UPDATE);
393
394         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
395         if (req == NULL) {
396                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
397                 RETURN(-ENOMEM);
398         }
399         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
400         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
401         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
402                              op_data->op_namelen + 1);
403
404         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
405         if (rc) {
406                 ptlrpc_request_free(req);
407                 RETURN(rc);
408         }
409
410         mdc_link_pack(req, op_data);
411         ptlrpc_request_set_replen(req);
412
413         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
414         *request = req;
415         if (rc == -ERESTARTSYS)
416                 rc = 0;
417
418         RETURN(rc);
419 }
420
421 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
422                 const char *old, size_t oldlen, const char *new, size_t newlen,
423                 struct ptlrpc_request **request)
424 {
425         struct list_head cancels = LIST_HEAD_INIT(cancels);
426         struct obd_device *obd = exp->exp_obd;
427         struct ptlrpc_request *req;
428         int count = 0, rc;
429         ENTRY;
430
431         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
432             (fid_is_sane(&op_data->op_fid1)))
433                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
434                                                 &cancels, LCK_EX,
435                                                 MDS_INODELOCK_UPDATE);
436         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
437             (fid_is_sane(&op_data->op_fid2)))
438                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
439                                                  &cancels, LCK_EX,
440                                                  MDS_INODELOCK_UPDATE);
441         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
442             (fid_is_sane(&op_data->op_fid3)))
443                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
444                                                  &cancels, LCK_EX,
445                                                  MDS_INODELOCK_LOOKUP);
446         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
447              (fid_is_sane(&op_data->op_fid4)))
448                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
449                                                  &cancels, LCK_EX,
450                                                  MDS_INODELOCK_FULL);
451
452         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
453                                    &RQF_MDS_REINT_RENAME);
454         if (req == NULL) {
455                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
456                 RETURN(-ENOMEM);
457         }
458
459         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
460         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
461         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
462         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
463
464         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
465         if (rc) {
466                 ptlrpc_request_free(req);
467                 RETURN(rc);
468         }
469
470         if (exp_connect_cancelset(exp) && req)
471                 ldlm_cli_cancel_list(&cancels, count, req, 0);
472
473         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
474
475         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
476                              obd->u.cli.cl_default_mds_easize);
477         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
478                              obd->u.cli.cl_default_mds_cookiesize);
479         ptlrpc_request_set_replen(req);
480
481         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
482         *request = req;
483         if (rc == -ERESTARTSYS)
484                 rc = 0;
485
486         RETURN(rc);
487 }