Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[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, Whamcloud, Inc.
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 #ifdef __KERNEL__
40 #ifndef AUTOCONF_INCLUDED
41 # include <linux/config.h>
42 #endif
43 # include <linux/module.h>
44 # include <linux/kernel.h>
45 #else
46 # include <liblustre.h>
47 #endif
48
49 #include <obd_class.h>
50 #include "mdc_internal.h"
51 #include <lustre_fid.h>
52
53 /* mdc_setattr does its own semaphore handling */
54 static int mdc_reint(struct ptlrpc_request *request,
55                      struct mdc_rpc_lock *rpc_lock,
56                      int level)
57 {
58         int rc;
59
60         request->rq_send_state = level;
61
62         mdc_get_rpc_lock(rpc_lock, NULL);
63         rc = ptlrpc_queue_wait(request);
64         mdc_put_rpc_lock(rpc_lock, NULL);
65         if (rc)
66                 CDEBUG(D_INFO, "error in handling %d\n", rc);
67         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
68                 rc = -EPROTO;
69         }
70         return rc;
71 }
72
73 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
74  * found by @fid. Found locks are added into @cancel list. Returns the amount of
75  * locks added to @cancels list. */
76 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
77                             cfs_list_t *cancels, ldlm_mode_t mode,
78                             __u64 bits)
79 {
80         ldlm_policy_data_t policy = {{0}};
81         struct ldlm_res_id res_id;
82         struct ldlm_resource *res;
83         int count;
84         ENTRY;
85
86         fid_build_reg_res_name(fid, &res_id);
87         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
88                                 NULL, &res_id, 0, 0);
89         if (res == NULL)
90                 RETURN(0);
91         LDLM_RESOURCE_ADDREF(res);
92         /* Initialize ibits lock policy. */
93         policy.l_inodebits.bits = bits;
94         count = ldlm_cancel_resource_local(res, cancels, &policy,
95                                            mode, 0, 0, NULL);
96         LDLM_RESOURCE_DELREF(res);
97         ldlm_resource_putref(res);
98         RETURN(count);
99 }
100
101 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
102                             cfs_list_t *cancels, int count)
103 {
104         return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
105                                  0, cancels, count);
106 }
107
108 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
109                 void *ea, int ealen, void *ea2, int ea2len,
110                 struct ptlrpc_request **request, struct md_open_data **mod)
111 {
112         CFS_LIST_HEAD(cancels);
113         struct ptlrpc_request *req;
114         struct mdc_rpc_lock *rpc_lock;
115         struct obd_device *obd = exp->exp_obd;
116         int count = 0, rc;
117         __u64 bits;
118         ENTRY;
119
120         LASSERT(op_data != NULL);
121
122         bits = MDS_INODELOCK_UPDATE;
123         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
124                 bits |= MDS_INODELOCK_LOOKUP;
125         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
126             (fid_is_sane(&op_data->op_fid1)) &&
127             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
128                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
129                                                 &cancels, LCK_EX, bits);
130         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
131                                    &RQF_MDS_REINT_SETATTR);
132         if (req == NULL) {
133                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
134                 RETURN(-ENOMEM);
135         }
136         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
137         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
138                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
139                                      0);
140         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
141         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
142                              ea2len);
143
144         rc = mdc_prep_elc_req(exp, req, &cancels, count);
145         if (rc) {
146                 ptlrpc_request_free(req);
147                 RETURN(rc);
148         }
149
150         rpc_lock = obd->u.cli.cl_rpc_lock;
151
152         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
153                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
154                        ", ctime "CFS_TIME_T"\n",
155                        LTIME_S(op_data->op_attr.ia_mtime),
156                        LTIME_S(op_data->op_attr.ia_ctime));
157         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
158
159         ptlrpc_request_set_replen(req);
160         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
161             req->rq_import->imp_replayable)
162         {
163                 LASSERT(*mod == NULL);
164
165                 *mod = obd_mod_alloc();
166                 if (*mod == NULL) {
167                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
168                                   "md_open_data");
169                 } else {
170                         req->rq_replay = 1;
171                         req->rq_cb_data = *mod;
172                         (*mod)->mod_open_req = req;
173                         req->rq_commit_cb = mdc_commit_open;
174                         /**
175                          * Take an extra reference on \var mod, it protects \var
176                          * mod from being freed on eviction (commit callback is
177                          * called despite rq_replay flag).
178                          * Will be put on mdc_done_writing().
179                          */
180                         obd_mod_get(*mod);
181                 }
182         }
183
184         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
185
186         /* Save the obtained info in the original RPC for the replay case. */
187         if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
188                 struct mdt_ioepoch *epoch;
189                 struct mdt_body  *body;
190
191                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
192                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
193                 LASSERT(epoch != NULL);
194                 LASSERT(body != NULL);
195                 epoch->handle = body->handle;
196                 epoch->ioepoch = body->ioepoch;
197                 req->rq_replay_cb = mdc_replay_open;
198         /** bug 3633, open may be committed and estale answer is not error */
199         } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
200                 rc = 0;
201         } else if (rc == -ERESTARTSYS) {
202                 rc = 0;
203         }
204         *request = req;
205         if (rc && req->rq_commit_cb) {
206                 /* Put an extra reference on \var mod on error case. */
207                 obd_mod_put(*mod);
208                 req->rq_commit_cb(req);
209         }
210         RETURN(rc);
211 }
212
213 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
214                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
215                cfs_cap_t cap_effective, __u64 rdev,
216                struct ptlrpc_request **request)
217 {
218         struct ptlrpc_request *req;
219         int level, rc;
220         int count, resends = 0;
221         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
222         int generation = import->imp_generation;
223         CFS_LIST_HEAD(cancels);
224         ENTRY;
225
226         /* For case if upper layer did not alloc fid, do it now. */
227         if (!fid_is_sane(&op_data->op_fid2)) {
228                 /*
229                  * mdc_fid_alloc() may return errno 1 in case of switch to new
230                  * sequence, handle this.
231                  */
232                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
233                 if (rc < 0) {
234                         CERROR("Can't alloc new fid, rc %d\n", rc);
235                         RETURN(rc);
236                 }
237         }
238
239 rebuild:
240         count = 0;
241         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
242             (fid_is_sane(&op_data->op_fid1)))
243                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
244                                                 &cancels, LCK_EX,
245                                                 MDS_INODELOCK_UPDATE);
246
247         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
248                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
249         if (req == NULL) {
250                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
251                 RETURN(-ENOMEM);
252         }
253         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
254         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
255                              op_data->op_namelen + 1);
256         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
257                              data && datalen ? datalen : 0);
258
259         rc = mdc_prep_elc_req(exp, req, &cancels, count);
260         if (rc) {
261                 ptlrpc_request_free(req);
262                 RETURN(rc);
263         }
264
265         /*
266          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
267          * tgt, for symlinks or lov MD data.
268          */
269         mdc_create_pack(req, op_data, data, datalen, mode, uid,
270                         gid, cap_effective, rdev);
271
272         ptlrpc_request_set_replen(req);
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->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         CFS_LIST_HEAD(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             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
335                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
336                                                 &cancels, LCK_EX,
337                                                 MDS_INODELOCK_UPDATE);
338         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
339             (fid_is_sane(&op_data->op_fid3)) &&
340             !OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
341                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
342                                                  &cancels, LCK_EX,
343                                                  MDS_INODELOCK_FULL);
344         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
345                                    &RQF_MDS_REINT_UNLINK);
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         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
352                              op_data->op_namelen + 1);
353
354         rc = mdc_prep_elc_req(exp, req, &cancels, count);
355         if (rc) {
356                 ptlrpc_request_free(req);
357                 RETURN(rc);
358         }
359
360         mdc_unlink_pack(req, op_data);
361
362         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
363                              obd->u.cli.cl_max_mds_easize);
364         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
365                              obd->u.cli.cl_max_mds_cookiesize);
366         ptlrpc_request_set_replen(req);
367
368         *request = req;
369
370         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
371         if (rc == -ERESTARTSYS)
372                 rc = 0;
373         RETURN(rc);
374 }
375
376 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
377              struct ptlrpc_request **request)
378 {
379         CFS_LIST_HEAD(cancels);
380         struct obd_device *obd = exp->exp_obd;
381         struct ptlrpc_request *req;
382         int count = 0, rc;
383         ENTRY;
384
385         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
386             (fid_is_sane(&op_data->op_fid2)))
387                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
388                                                 &cancels, LCK_EX,
389                                                 MDS_INODELOCK_UPDATE);
390         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
391             (fid_is_sane(&op_data->op_fid1)))
392                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
393                                                  &cancels, LCK_EX,
394                                                  MDS_INODELOCK_UPDATE);
395
396         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
397         if (req == NULL) {
398                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
399                 RETURN(-ENOMEM);
400         }
401         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
402         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
403         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
404                              op_data->op_namelen + 1);
405
406         rc = mdc_prep_elc_req(exp, req, &cancels, count);
407         if (rc) {
408                 ptlrpc_request_free(req);
409                 RETURN(rc);
410         }
411
412         mdc_link_pack(req, op_data);
413         ptlrpc_request_set_replen(req);
414
415         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
416         *request = req;
417         if (rc == -ERESTARTSYS)
418                 rc = 0;
419
420         RETURN(rc);
421 }
422
423 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
424                const char *old, int oldlen, const char *new, int newlen,
425                struct ptlrpc_request **request)
426 {
427         CFS_LIST_HEAD(cancels);
428         struct obd_device *obd = exp->exp_obd;
429         struct ptlrpc_request *req;
430         int count = 0, rc;
431         ENTRY;
432
433         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
434             (fid_is_sane(&op_data->op_fid1)))
435                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
436                                                 &cancels, LCK_EX,
437                                                 MDS_INODELOCK_UPDATE);
438         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
439             (fid_is_sane(&op_data->op_fid2)))
440                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
441                                                  &cancels, LCK_EX,
442                                                  MDS_INODELOCK_UPDATE);
443         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
444             (fid_is_sane(&op_data->op_fid3)))
445                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
446                                                  &cancels, LCK_EX,
447                                                  MDS_INODELOCK_LOOKUP);
448         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
449              (fid_is_sane(&op_data->op_fid4)))
450                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
451                                                  &cancels, LCK_EX,
452                                                  MDS_INODELOCK_FULL);
453
454         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
455                                    &RQF_MDS_REINT_RENAME);
456         if (req == NULL) {
457                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
458                 RETURN(-ENOMEM);
459         }
460
461         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
462         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
463         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
464         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
465
466         rc = mdc_prep_elc_req(exp, req, &cancels, count);
467         if (rc) {
468                 ptlrpc_request_free(req);
469                 RETURN(rc);
470         }
471
472         if (exp_connect_cancelset(exp) && req)
473                 ldlm_cli_cancel_list(&cancels, count, req, 0);
474
475         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
476
477         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
478                              obd->u.cli.cl_max_mds_easize);
479         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
480                              obd->u.cli.cl_max_mds_cookiesize);
481         ptlrpc_request_set_replen(req);
482
483         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
484         *request = req;
485         if (rc == -ERESTARTSYS)
486                 rc = 0;
487
488         RETURN(rc);
489 }