Whamcloud - gitweb
LU-1187 mdt: Add MDS_INODELOCK_PERM lock for remote dir
[fs/lustre-release.git] / lustre / lmv / lmv_intent.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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, 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_LMV
38 #ifdef __KERNEL__
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
44 #include <asm/div64.h>
45 #include <linux/seq_file.h>
46 #include <linux/namei.h>
47 #include <linux/lustre_intent.h>
48 #else
49 #include <liblustre.h>
50 #endif
51
52 #include <obd_support.h>
53 #include <lustre/lustre_idl.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <lustre_dlm.h>
57 #include <obd_class.h>
58 #include <lprocfs_status.h>
59 #include "lmv_internal.h"
60
61 int lmv_intent_remote(struct obd_export *exp, void *lmm,
62                       int lmmsize, struct lookup_intent *it,
63                       int flags, struct ptlrpc_request **reqp,
64                       ldlm_blocking_callback cb_blocking,
65                       __u64 extra_lock_flags)
66 {
67         struct obd_device       *obd = exp->exp_obd;
68         struct lmv_obd          *lmv = &obd->u.lmv;
69         struct ptlrpc_request   *req = NULL;
70         struct lustre_handle    plock;
71         struct md_op_data       *op_data;
72         struct lmv_tgt_desc     *tgt;
73         struct mdt_body         *body;
74         int                     pmode;
75         int                     rc = 0;
76         ENTRY;
77
78         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
79         if (body == NULL)
80                 RETURN(-EPROTO);
81
82         LASSERT((body->valid & OBD_MD_MDS));
83
84         /*
85          * Unfortunately, we have to lie to MDC/MDS to retrieve
86          * attributes llite needs and provideproper locking.
87          */
88         if (it->it_op & IT_LOOKUP)
89                 it->it_op = IT_GETATTR;
90
91         /*
92          * We got LOOKUP lock, but we really need attrs.
93          */
94         pmode = it->d.lustre.it_lock_mode;
95         if (pmode) {
96                 plock.cookie = it->d.lustre.it_lock_handle;
97                 it->d.lustre.it_lock_mode = 0;
98                 it->d.lustre.it_data = NULL;
99         }
100
101         LASSERT(fid_is_sane(&body->fid1));
102
103         tgt = lmv_find_target(lmv, &body->fid1);
104         if (IS_ERR(tgt))
105                 GOTO(out, rc = PTR_ERR(tgt));
106
107         OBD_ALLOC_PTR(op_data);
108         if (op_data == NULL)
109                 GOTO(out, rc = -ENOMEM);
110
111         op_data->op_fid1 = body->fid1;
112         op_data->op_bias = MDS_CROSS_REF;
113
114         CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
115                PFID(&body->fid1), tgt->ltd_idx);
116
117         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
118         rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
119                             flags, &req, cb_blocking, extra_lock_flags);
120         if (rc)
121                 GOTO(out_free_op_data, rc);
122
123         /*
124          * LLite needs LOOKUP lock to track dentry revocation in order to
125          * maintain dcache consistency. Thus drop UPDATE|PERM lock here
126          * and put LOOKUP in request.
127          */
128         if (it->d.lustre.it_lock_mode != 0) {
129                 it->d.lustre.it_remote_lock_handle =
130                                         it->d.lustre.it_lock_handle;
131                 it->d.lustre.it_remote_lock_mode = it->d.lustre.it_lock_mode;
132         }
133
134         it->d.lustre.it_lock_handle = plock.cookie;
135         it->d.lustre.it_lock_mode = pmode;
136
137         EXIT;
138 out_free_op_data:
139         OBD_FREE_PTR(op_data);
140 out:
141         if (rc && pmode)
142                 ldlm_lock_decref(&plock, pmode);
143
144         ptlrpc_req_finished(*reqp);
145         *reqp = req;
146         return rc;
147 }
148
149 /*
150  * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
151  * may be split dir.
152  */
153 int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
154                     void *lmm, int lmmsize, struct lookup_intent *it,
155                     int flags, struct ptlrpc_request **reqp,
156                     ldlm_blocking_callback cb_blocking,
157                     __u64 extra_lock_flags)
158 {
159         struct obd_device       *obd = exp->exp_obd;
160         struct lmv_obd          *lmv = &obd->u.lmv;
161         struct lmv_tgt_desc     *tgt;
162         struct mdt_body         *body;
163         int                     rc;
164         ENTRY;
165
166         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
167         if (IS_ERR(tgt))
168                 RETURN(PTR_ERR(tgt));
169
170         if (it->it_op & IT_CREAT) {
171                 /*
172                  * For open with IT_CREATE and for IT_CREATE cases allocate new
173                  * fid and setup FLD for it.
174                  */
175                 op_data->op_fid3 = op_data->op_fid2;
176                 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
177                 if (rc != 0)
178                         RETURN(rc);
179         }
180
181         CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
182                " name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
183                PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
184
185         rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it, flags,
186                             reqp, cb_blocking, extra_lock_flags);
187         if (rc != 0)
188                 RETURN(rc);
189         /*
190          * Nothing is found, do not access body->fid1 as it is zero and thus
191          * pointless.
192          */
193         if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
194             !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
195             !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
196                 RETURN(rc);
197
198         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
199         if (body == NULL)
200                 RETURN(-EPROTO);
201         /*
202          * Not cross-ref case, just get out of here.
203          */
204         if (likely(!(body->valid & OBD_MD_MDS)))
205                 RETURN(0);
206
207         /*
208          * Okay, MDS has returned success. Probably name has been resolved in
209          * remote inode.
210          */
211         rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags, reqp,
212                                cb_blocking, extra_lock_flags);
213         if (rc != 0) {
214                 LASSERT(rc < 0);
215                 /*
216                  * This is possible, that some userspace application will try to
217                  * open file as directory and we will have -ENOTDIR here. As
218                  * this is normal situation, we should not print error here,
219                  * only debug info.
220                  */
221                 CDEBUG(D_INODE, "Can't handle remote %s: dir "DFID"("DFID"):"
222                        "%*s: %d\n", LL_IT2STR(it), PFID(&op_data->op_fid2),
223                        PFID(&op_data->op_fid1), op_data->op_namelen,
224                        op_data->op_name, rc);
225                 RETURN(rc);
226         }
227
228         RETURN(rc);
229 }
230
231 /*
232  * Handler for: getattr, lookup and revalidate cases.
233  */
234 int lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
235                       void *lmm, int lmmsize, struct lookup_intent *it,
236                       int flags, struct ptlrpc_request **reqp,
237                       ldlm_blocking_callback cb_blocking,
238                       __u64 extra_lock_flags)
239 {
240         struct obd_device      *obd = exp->exp_obd;
241         struct lmv_obd         *lmv = &obd->u.lmv;
242         struct lmv_tgt_desc    *tgt = NULL;
243         struct mdt_body        *body;
244         int                     rc = 0;
245         ENTRY;
246
247         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
248         if (IS_ERR(tgt))
249                 RETURN(PTR_ERR(tgt));
250
251         if (!fid_is_sane(&op_data->op_fid2))
252                 fid_zero(&op_data->op_fid2);
253
254         CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
255                ", name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
256                PFID(&op_data->op_fid2),
257                op_data->op_name ? op_data->op_name : "<NULL>",
258                tgt->ltd_idx);
259
260         op_data->op_bias &= ~MDS_CROSS_REF;
261
262         rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
263                              flags, reqp, cb_blocking, extra_lock_flags);
264
265         if (rc < 0 || *reqp == NULL)
266                 RETURN(rc);
267
268         /*
269          * MDS has returned success. Probably name has been resolved in
270          * remote inode. Let's check this.
271          */
272         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
273         if (body == NULL)
274                 RETURN(-EPROTO);
275         /* Not cross-ref case, just get out of here. */
276         if (likely(!(body->valid & OBD_MD_MDS)))
277                 RETURN(0);
278
279         rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags, reqp,
280                                cb_blocking, extra_lock_flags);
281
282         RETURN(rc);
283 }
284
285 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
286                     void *lmm, int lmmsize, struct lookup_intent *it,
287                     int flags, struct ptlrpc_request **reqp,
288                     ldlm_blocking_callback cb_blocking,
289                     __u64 extra_lock_flags)
290 {
291         struct obd_device *obd = exp->exp_obd;
292         int                rc;
293         ENTRY;
294
295         LASSERT(it != NULL);
296         LASSERT(fid_is_sane(&op_data->op_fid1));
297
298         CDEBUG(D_INODE, "INTENT LOCK '%s' for '%*s' on "DFID"\n",
299                LL_IT2STR(it), op_data->op_namelen, op_data->op_name,
300                PFID(&op_data->op_fid1));
301
302         rc = lmv_check_connect(obd);
303         if (rc)
304                 RETURN(rc);
305
306         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
307                 rc = lmv_intent_lookup(exp, op_data, lmm, lmmsize, it,
308                                        flags, reqp, cb_blocking,
309                                        extra_lock_flags);
310         else if (it->it_op & IT_OPEN)
311                 rc = lmv_intent_open(exp, op_data, lmm, lmmsize, it,
312                                      flags, reqp, cb_blocking,
313                                      extra_lock_flags);
314         else
315                 LBUG();
316         RETURN(rc);
317 }