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