]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/mdc/mdc_reint.c
staging/lustre/mdc: Adjust NULL comparison codestyle
[karo-tx-linux.git] / drivers / staging / lustre / 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) 2011, 2015, 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 "../include/obd_class.h"
43 #include "mdc_internal.h"
44 #include "../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 = {};
75         struct ldlm_res_id res_id;
76         struct ldlm_resource *res;
77         int count;
78
79         /* Return, i.e. cancel nothing, only if ELC is supported (flag in
80          * export) but disabled through procfs (flag in NS).
81          *
82          * This distinguishes from a case when ELC is not supported originally,
83          * when we still want to cancel locks in advance and just cancel them
84          * locally, without sending any RPC. */
85         if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
86                 return 0;
87
88         fid_build_reg_res_name(fid, &res_id);
89         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
90                                 NULL, &res_id, 0, 0);
91         if (!res)
92                 return 0;
93         LDLM_RESOURCE_ADDREF(res);
94         /* Initialize ibits lock policy. */
95         policy.l_inodebits.bits = bits;
96         count = ldlm_cancel_resource_local(res, cancels, &policy,
97                                            mode, 0, 0, NULL);
98         LDLM_RESOURCE_DELREF(res);
99         ldlm_resource_putref(res);
100         return count;
101 }
102
103 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
104                 void *ea, int ealen, void *ea2, int ea2len,
105                 struct ptlrpc_request **request, struct md_open_data **mod)
106 {
107         LIST_HEAD(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
114         bits = MDS_INODELOCK_UPDATE;
115         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
116                 bits |= MDS_INODELOCK_LOOKUP;
117         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
118             (fid_is_sane(&op_data->op_fid1)) &&
119             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
120                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
121                                                 &cancels, LCK_EX, bits);
122         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
123                                    &RQF_MDS_REINT_SETATTR);
124         if (!req) {
125                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
126                 return -ENOMEM;
127         }
128         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
129                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
130                                      0);
131         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
132         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
133                              ea2len);
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 %ld, ctime %ld\n",
145                        LTIME_S(op_data->op_attr.ia_mtime),
146                        LTIME_S(op_data->op_attr.ia_ctime));
147         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
148
149         ptlrpc_request_set_replen(req);
150         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
151             req->rq_import->imp_replayable) {
152                 LASSERT(!*mod);
153
154                 *mod = obd_mod_alloc();
155                 if (!*mod) {
156                         DEBUG_REQ(D_ERROR, req, "Can't allocate md_open_data");
157                 } else {
158                         req->rq_replay = 1;
159                         req->rq_cb_data = *mod;
160                         (*mod)->mod_open_req = req;
161                         req->rq_commit_cb = mdc_commit_open;
162                         (*mod)->mod_is_create = true;
163                         /**
164                          * Take an extra reference on \var mod, it protects \var
165                          * mod from being freed on eviction (commit callback is
166                          * called despite rq_replay flag).
167                          * Will be put on mdc_done_writing().
168                          */
169                         obd_mod_get(*mod);
170                 }
171         }
172
173         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
174
175         /* Save the obtained info in the original RPC for the replay case. */
176         if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
177                 struct mdt_ioepoch *epoch;
178                 struct mdt_body  *body;
179
180                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
181                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
182                 epoch->handle = body->handle;
183                 epoch->ioepoch = body->ioepoch;
184                 req->rq_replay_cb = mdc_replay_open;
185         /** bug 3633, open may be committed and estale answer is not error */
186         } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
187                 rc = 0;
188         } else if (rc == -ERESTARTSYS) {
189                 rc = 0;
190         }
191         *request = req;
192         if (rc && req->rq_commit_cb) {
193                 /* Put an extra reference on \var mod on error case. */
194                 if (mod && *mod)
195                         obd_mod_put(*mod);
196                 req->rq_commit_cb(req);
197         }
198         return rc;
199 }
200
201 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
202                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
203                cfs_cap_t cap_effective, __u64 rdev,
204                struct ptlrpc_request **request)
205 {
206         struct ptlrpc_request *req;
207         int level, rc;
208         int count, resends = 0;
209         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
210         int generation = import->imp_generation;
211         LIST_HEAD(cancels);
212
213         /* For case if upper layer did not alloc fid, do it now. */
214         if (!fid_is_sane(&op_data->op_fid2)) {
215                 /*
216                  * mdc_fid_alloc() may return errno 1 in case of switch to new
217                  * sequence, handle this.
218                  */
219                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
220                 if (rc < 0) {
221                         CERROR("Can't alloc new fid, rc %d\n", rc);
222                         return rc;
223                 }
224         }
225
226 rebuild:
227         count = 0;
228         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
229             (fid_is_sane(&op_data->op_fid1)))
230                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
231                                                 &cancels, LCK_EX,
232                                                 MDS_INODELOCK_UPDATE);
233
234         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
235                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
236         if (!req) {
237                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
238                 return -ENOMEM;
239         }
240         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
241                              op_data->op_namelen + 1);
242         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
243                              data && datalen ? datalen : 0);
244
245         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
246         if (rc) {
247                 ptlrpc_request_free(req);
248                 return rc;
249         }
250
251         /*
252          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
253          * tgt, for symlinks or lov MD data.
254          */
255         mdc_create_pack(req, op_data, data, datalen, mode, uid,
256                         gid, cap_effective, rdev);
257
258         ptlrpc_request_set_replen(req);
259
260         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
261          * logic here */
262         req->rq_no_retry_einprogress = 1;
263
264         if (resends) {
265                 req->rq_generation_set = 1;
266                 req->rq_import_generation = generation;
267                 req->rq_sent = ktime_get_real_seconds() + resends;
268         }
269         level = LUSTRE_IMP_FULL;
270  resend:
271         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
272
273         /* Resend if we were told to. */
274         if (rc == -ERESTARTSYS) {
275                 level = LUSTRE_IMP_RECOVER;
276                 goto resend;
277         } else if (rc == -EINPROGRESS) {
278                 /* Retry create infinitely until succeed or get other
279                  * error code. */
280                 ptlrpc_req_finished(req);
281                 resends++;
282
283                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
284                        exp->exp_obd->obd_name, resends,
285                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
286
287                 if (generation == import->imp_generation) {
288                         goto rebuild;
289                 } else {
290                         CDEBUG(D_HA, "resend cross eviction\n");
291                         return -EIO;
292                 }
293         }
294
295         *request = req;
296         return rc;
297 }
298
299 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
300                struct ptlrpc_request **request)
301 {
302         LIST_HEAD(cancels);
303         struct obd_device *obd = class_exp2obd(exp);
304         struct ptlrpc_request *req = *request;
305         int count = 0, rc;
306
307         LASSERT(!req);
308
309         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
310             (fid_is_sane(&op_data->op_fid1)) &&
311             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
312                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
313                                                 &cancels, LCK_EX,
314                                                 MDS_INODELOCK_UPDATE);
315         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
316             (fid_is_sane(&op_data->op_fid3)) &&
317             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
318                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
319                                                  &cancels, LCK_EX,
320                                                  MDS_INODELOCK_FULL);
321         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
322                                    &RQF_MDS_REINT_UNLINK);
323         if (!req) {
324                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
325                 return -ENOMEM;
326         }
327         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
328                              op_data->op_namelen + 1);
329
330         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
331         if (rc) {
332                 ptlrpc_request_free(req);
333                 return rc;
334         }
335
336         mdc_unlink_pack(req, op_data);
337
338         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
339                              obd->u.cli.cl_default_mds_easize);
340         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
341                              obd->u.cli.cl_default_mds_cookiesize);
342         ptlrpc_request_set_replen(req);
343
344         *request = req;
345
346         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
347         if (rc == -ERESTARTSYS)
348                 rc = 0;
349         return rc;
350 }
351
352 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
353              struct ptlrpc_request **request)
354 {
355         LIST_HEAD(cancels);
356         struct obd_device *obd = exp->exp_obd;
357         struct ptlrpc_request *req;
358         int count = 0, rc;
359
360         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
361             (fid_is_sane(&op_data->op_fid2)))
362                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
363                                                 &cancels, LCK_EX,
364                                                 MDS_INODELOCK_UPDATE);
365         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
366             (fid_is_sane(&op_data->op_fid1)))
367                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
368                                                  &cancels, LCK_EX,
369                                                  MDS_INODELOCK_UPDATE);
370
371         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
372         if (!req) {
373                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
374                 return -ENOMEM;
375         }
376         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
377                              op_data->op_namelen + 1);
378
379         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
380         if (rc) {
381                 ptlrpc_request_free(req);
382                 return rc;
383         }
384
385         mdc_link_pack(req, op_data);
386         ptlrpc_request_set_replen(req);
387
388         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
389         *request = req;
390         if (rc == -ERESTARTSYS)
391                 rc = 0;
392
393         return rc;
394 }
395
396 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
397                const char *old, int oldlen, const char *new, int newlen,
398                struct ptlrpc_request **request)
399 {
400         LIST_HEAD(cancels);
401         struct obd_device *obd = exp->exp_obd;
402         struct ptlrpc_request *req;
403         int count = 0, rc;
404
405         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
406             (fid_is_sane(&op_data->op_fid1)))
407                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
408                                                 &cancels, LCK_EX,
409                                                 MDS_INODELOCK_UPDATE);
410         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
411             (fid_is_sane(&op_data->op_fid2)))
412                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
413                                                  &cancels, LCK_EX,
414                                                  MDS_INODELOCK_UPDATE);
415         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
416             (fid_is_sane(&op_data->op_fid3)))
417                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
418                                                  &cancels, LCK_EX,
419                                                  MDS_INODELOCK_LOOKUP);
420         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
421              (fid_is_sane(&op_data->op_fid4)))
422                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
423                                                  &cancels, LCK_EX,
424                                                  MDS_INODELOCK_FULL);
425
426         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
427                                    &RQF_MDS_REINT_RENAME);
428         if (!req) {
429                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
430                 return -ENOMEM;
431         }
432
433         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
434         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
435
436         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
437         if (rc) {
438                 ptlrpc_request_free(req);
439                 return rc;
440         }
441
442         if (exp_connect_cancelset(exp) && req)
443                 ldlm_cli_cancel_list(&cancels, count, req, 0);
444
445         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
446
447         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
448                              obd->u.cli.cl_default_mds_easize);
449         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
450                              obd->u.cli.cl_default_mds_cookiesize);
451         ptlrpc_request_set_replen(req);
452
453         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
454         *request = req;
455         if (rc == -ERESTARTSYS)
456                 rc = 0;
457
458         return rc;
459 }