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