Whamcloud - gitweb
b=23428 Fix lustre built with --enable-lu_ref
[fs/lustre-release.git] / lustre / mdc / mdc_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_MDC
41
42 #ifdef __KERNEL__
43 #ifndef AUTOCONF_INCLUDED
44 # include <linux/config.h>
45 #endif
46 # include <linux/module.h>
47 # include <linux/kernel.h>
48 #else
49 # include <liblustre.h>
50 #endif
51
52 #include <obd_class.h>
53 #include "mdc_internal.h"
54 #include <lustre_fid.h>
55
56 /* mdc_setattr does its own semaphore handling */
57 static int mdc_reint(struct ptlrpc_request *request,
58                      struct mdc_rpc_lock *rpc_lock,
59                      int level)
60 {
61         int rc;
62
63         request->rq_send_state = level;
64
65         mdc_get_rpc_lock(rpc_lock, NULL);
66         rc = ptlrpc_queue_wait(request);
67         mdc_put_rpc_lock(rpc_lock, NULL);
68         if (rc)
69                 CDEBUG(D_INFO, "error in handling %d\n", rc);
70         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
71                 rc = -EPROTO;
72         }
73         return rc;
74 }
75
76 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
77  * found by @fid. Found locks are added into @cancel list. Returns the amount of
78  * locks added to @cancels list. */
79 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
80                             cfs_list_t *cancels, ldlm_mode_t mode,
81                             __u64 bits)
82 {
83         ldlm_policy_data_t policy = {{0}};
84         struct ldlm_res_id res_id;
85         struct ldlm_resource *res;
86         int count;
87         ENTRY;
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 (res == NULL)
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 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
105                             cfs_list_t *cancels, int count)
106 {
107         return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
108                                  0, cancels, count);
109 }
110
111 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
112  * should take the normal semaphore and go to the normal portal.
113  *
114  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
115  * magic open-path setattr that should take the setattr semaphore and
116  * go to the setattr portal. */
117 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
118                 void *ea, int ealen, void *ea2, int ea2len,
119                 struct ptlrpc_request **request, struct md_open_data **mod)
120 {
121         CFS_LIST_HEAD(cancels);
122         struct ptlrpc_request *req;
123         struct mdc_rpc_lock *rpc_lock;
124         struct obd_device *obd = exp->exp_obd;
125         int count = 0, rc;
126         __u64 bits;
127         ENTRY;
128
129         LASSERT(op_data != NULL);
130
131         bits = MDS_INODELOCK_UPDATE;
132         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
133                 bits |= MDS_INODELOCK_LOOKUP;
134         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
135             (fid_is_sane(&op_data->op_fid1)))
136                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
137                                                 &cancels, LCK_EX, bits);
138         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
139                                    &RQF_MDS_REINT_SETATTR);
140         if (req == NULL) {
141                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
142                 RETURN(-ENOMEM);
143         }
144         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
145         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
146                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
147                                      0);
148         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
149         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
150                              ea2len);
151
152         rc = mdc_prep_elc_req(exp, req, &cancels, count);
153         if (rc) {
154                 ptlrpc_request_free(req);
155                 RETURN(rc);
156         }
157
158         if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
159                 req->rq_request_portal = MDS_SETATTR_PORTAL;
160                 ptlrpc_at_set_req_timeout(req);
161                 rpc_lock = obd->u.cli.cl_setattr_lock;
162         } else {
163                 rpc_lock = obd->u.cli.cl_rpc_lock;
164         }
165
166         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
167                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
168                        ", ctime "CFS_TIME_T"\n",
169                        LTIME_S(op_data->op_attr.ia_mtime),
170                        LTIME_S(op_data->op_attr.ia_ctime));
171         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
172
173         ptlrpc_request_set_replen(req);
174         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
175             req->rq_import->imp_replayable)
176         {
177                 LASSERT(*mod == NULL);
178
179                 *mod = obd_mod_alloc();
180                 if (*mod == NULL) {
181                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
182                                   "md_open_data");
183                 } else {
184                         req->rq_replay = 1;
185                         req->rq_cb_data = *mod;
186                         (*mod)->mod_open_req = req;
187                         req->rq_commit_cb = mdc_commit_open;
188                         /**
189                          * Take an extra reference on \var mod, it protects \var
190                          * mod from being freed on eviction (commit callback is
191                          * called despite rq_replay flag).
192                          * Will be put on mdc_done_writing().
193                          */
194                         obd_mod_get(*mod);
195                 }
196         }
197
198         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
199
200         /* Save the obtained info in the original RPC for the replay case. */
201         if (rc == 0 && (op_data->op_flags & MF_EPOCH_OPEN)) {
202                 struct mdt_ioepoch *epoch;
203                 struct mdt_body  *body;
204
205                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
206                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
207                 LASSERT(epoch != NULL);
208                 LASSERT(body != NULL);
209                 epoch->handle = body->handle;
210                 epoch->ioepoch = body->ioepoch;
211                 req->rq_replay_cb = mdc_replay_open;
212         /** bug 3633, open may be committed and estale answer is not error */
213         } else if (rc == -ESTALE && (op_data->op_flags & MF_SOM_CHANGE)) {
214                 rc = 0;
215         } else if (rc == -ERESTARTSYS) {
216                 rc = 0;
217         }
218         *request = req;
219         if (rc && req->rq_commit_cb) {
220                 /* Put an extra reference on \var mod on error case. */
221                 obd_mod_put(*mod);
222                 req->rq_commit_cb(req);
223         }
224         RETURN(rc);
225 }
226
227 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
228                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
229                cfs_cap_t cap_effective, __u64 rdev,
230                struct ptlrpc_request **request)
231 {
232         struct ptlrpc_request *req;
233         int level, rc;
234         int count = 0;
235         CFS_LIST_HEAD(cancels);
236         ENTRY;
237
238         /* For case if upper layer did not alloc fid, do it now. */
239         if (!fid_is_sane(&op_data->op_fid2)) {
240                 /*
241                  * mdc_fid_alloc() may return errno 1 in case of switch to new
242                  * sequence, handle this.
243                  */
244                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
245                 if (rc < 0) {
246                         CERROR("Can't alloc new fid, rc %d\n", rc);
247                         RETURN(rc);
248                 }
249         }
250
251         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
252             (fid_is_sane(&op_data->op_fid1)))
253                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
254                                                 &cancels, LCK_EX,
255                                                 MDS_INODELOCK_UPDATE);
256
257         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
258                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
259         if (req == NULL) {
260                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
261                 RETURN(-ENOMEM);
262         }
263         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
264         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
265                              op_data->op_namelen + 1);
266         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
267                              data && datalen ? datalen : 0);
268
269         rc = mdc_prep_elc_req(exp, req, &cancels, count);
270         if (rc) {
271                 ptlrpc_request_free(req);
272                 RETURN(rc);
273         }
274
275         /*
276          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
277          * tgt, for symlinks or lov MD data.
278          */
279         mdc_create_pack(req, op_data, data, datalen, mode, uid,
280                         gid, cap_effective, rdev);
281
282         ptlrpc_request_set_replen(req);
283
284         level = LUSTRE_IMP_FULL;
285  resend:
286         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
287
288         /* Resend if we were told to. */
289         if (rc == -ERESTARTSYS) {
290                 level = LUSTRE_IMP_RECOVER;
291                 goto resend;
292         } else if (rc == 0) {
293                 struct mdt_body *body;
294                 struct lustre_capa *capa;
295
296                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
297                 LASSERT(body);
298                 if (body->valid & OBD_MD_FLMDSCAPA) {
299                         capa = req_capsule_server_get(&req->rq_pill,
300                                                       &RMF_CAPA1);
301                         if (capa == NULL)
302                                 rc = -EPROTO;
303                 }
304         }
305
306         *request = req;
307         RETURN(rc);
308 }
309
310 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
311                struct ptlrpc_request **request)
312 {
313         CFS_LIST_HEAD(cancels);
314         struct obd_device *obd = class_exp2obd(exp);
315         struct ptlrpc_request *req = *request;
316         int count = 0, rc;
317         ENTRY;
318
319         LASSERT(req == NULL);
320
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         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
327             (fid_is_sane(&op_data->op_fid3)))
328                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
329                                                  &cancels, LCK_EX,
330                                                  MDS_INODELOCK_FULL);
331         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
332                                    &RQF_MDS_REINT_UNLINK);
333         if (req == NULL) {
334                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
335                 RETURN(-ENOMEM);
336         }
337         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
338         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
339                              op_data->op_namelen + 1);
340
341         rc = mdc_prep_elc_req(exp, req, &cancels, count);
342         if (rc) {
343                 ptlrpc_request_free(req);
344                 RETURN(rc);
345         }
346
347         mdc_unlink_pack(req, op_data);
348
349         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
350                              obd->u.cli.cl_max_mds_easize);
351         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
352                              obd->u.cli.cl_max_mds_cookiesize);
353         ptlrpc_request_set_replen(req);
354
355         *request = req;
356
357         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
358         if (rc == -ERESTARTSYS)
359                 rc = 0;
360         RETURN(rc);
361 }
362
363 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
364              struct ptlrpc_request **request)
365 {
366         CFS_LIST_HEAD(cancels);
367         struct obd_device *obd = exp->exp_obd;
368         struct ptlrpc_request *req;
369         int count = 0, rc;
370         ENTRY;
371
372         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
373             (fid_is_sane(&op_data->op_fid2)))
374                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
375                                                 &cancels, LCK_EX,
376                                                 MDS_INODELOCK_UPDATE);
377         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
378             (fid_is_sane(&op_data->op_fid1)))
379                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
380                                                  &cancels, LCK_EX,
381                                                  MDS_INODELOCK_UPDATE);
382
383         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
384         if (req == NULL) {
385                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
386                 RETURN(-ENOMEM);
387         }
388         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
389         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
390         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
391                              op_data->op_namelen + 1);
392
393         rc = mdc_prep_elc_req(exp, req, &cancels, count);
394         if (rc) {
395                 ptlrpc_request_free(req);
396                 RETURN(rc);
397         }
398
399         mdc_link_pack(req, op_data);
400         ptlrpc_request_set_replen(req);
401
402         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
403         *request = req;
404         if (rc == -ERESTARTSYS)
405                 rc = 0;
406
407         RETURN(rc);
408 }
409
410 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
411                const char *old, int oldlen, const char *new, int newlen,
412                struct ptlrpc_request **request)
413 {
414         CFS_LIST_HEAD(cancels);
415         struct obd_device *obd = exp->exp_obd;
416         struct ptlrpc_request *req;
417         int count = 0, rc;
418         ENTRY;
419
420         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
421             (fid_is_sane(&op_data->op_fid1)))
422                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
423                                                 &cancels, LCK_EX,
424                                                 MDS_INODELOCK_UPDATE);
425         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
426             (fid_is_sane(&op_data->op_fid2)))
427                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
428                                                  &cancels, LCK_EX,
429                                                  MDS_INODELOCK_UPDATE);
430         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
431             (fid_is_sane(&op_data->op_fid3)))
432                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
433                                                  &cancels, LCK_EX,
434                                                  MDS_INODELOCK_LOOKUP);
435         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
436              (fid_is_sane(&op_data->op_fid4)))
437                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
438                                                  &cancels, LCK_EX,
439                                                  MDS_INODELOCK_FULL);
440
441         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
442                                    &RQF_MDS_REINT_RENAME);
443         if (req == NULL) {
444                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
445                 RETURN(-ENOMEM);
446         }
447
448         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
449         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
450         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
451         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
452
453         rc = mdc_prep_elc_req(exp, req, &cancels, count);
454         if (rc) {
455                 ptlrpc_request_free(req);
456                 RETURN(rc);
457         }
458
459         if (exp_connect_cancelset(exp) && req)
460                 ldlm_cli_cancel_list(&cancels, count, req, 0);
461
462         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
463
464         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
465                              obd->u.cli.cl_max_mds_easize);
466         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
467                              obd->u.cli.cl_max_mds_cookiesize);
468         ptlrpc_request_set_replen(req);
469
470         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
471         *request = req;
472         if (rc == -ERESTARTSYS)
473                 rc = 0;
474
475         RETURN(rc);
476 }