Whamcloud - gitweb
fa0f194b4df9a6ec129f558a40cc20b519dc378f
[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, 2013, 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 <linux/math64.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 <lustre_mdc.h>
58 #include <obd_class.h>
59 #include <lprocfs_status.h>
60 #include "lmv_internal.h"
61
62 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
63                              const struct lu_fid *parent_fid,
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->mbo_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->mbo_fid1));
103
104         tgt = lmv_find_target(lmv, &body->mbo_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->mbo_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         }
121
122         op_data->op_bias = MDS_CROSS_REF;
123         CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
124                PFID(&body->mbo_fid1), tgt->ltd_idx);
125
126         rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
127                             extra_lock_flags);
128         if (rc)
129                 GOTO(out_free_op_data, rc);
130
131         /*
132          * LLite needs LOOKUP lock to track dentry revocation in order to
133          * maintain dcache consistency. Thus drop UPDATE|PERM lock here
134          * and put LOOKUP in request.
135          */
136         if (it->d.lustre.it_lock_mode != 0) {
137                 it->d.lustre.it_remote_lock_handle =
138                                         it->d.lustre.it_lock_handle;
139                 it->d.lustre.it_remote_lock_mode = it->d.lustre.it_lock_mode;
140         }
141
142         if (pmode) {
143                 it->d.lustre.it_lock_handle = plock.cookie;
144                 it->d.lustre.it_lock_mode = pmode;
145         }
146
147         EXIT;
148 out_free_op_data:
149         OBD_FREE_PTR(op_data);
150 out:
151         if (rc && pmode)
152                 ldlm_lock_decref(&plock, pmode);
153
154         ptlrpc_req_finished(*reqp);
155         *reqp = req;
156         return rc;
157 }
158
159 #ifdef __KERNEL__
160 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
161                           struct lmv_stripe_md *lsm,
162                           ldlm_blocking_callback cb_blocking,
163                           int extra_lock_flags)
164 {
165         struct obd_device      *obd = exp->exp_obd;
166         struct lmv_obd         *lmv = &obd->u.lmv;
167         struct mdt_body         *body;
168         struct md_op_data      *op_data;
169         unsigned long           size = 0;
170         unsigned long           nlink = 0;
171         obd_time                atime = 0;
172         obd_time                ctime = 0;
173         obd_time                mtime = 0;
174         int                     i;
175         int                     rc = 0;
176
177         ENTRY;
178
179         /**
180          * revalidate slaves has some problems, temporarily return,
181          * we may not need that
182          */
183         OBD_ALLOC_PTR(op_data);
184         if (op_data == NULL)
185                 RETURN(-ENOMEM);
186
187         /**
188          * Loop over the stripe information, check validity and update them
189          * from MDS if needed.
190          */
191         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
192                 struct lu_fid           fid;
193                 struct lookup_intent    it = { .it_op = IT_GETATTR };
194                 struct ptlrpc_request   *req = NULL;
195                 struct lustre_handle    *lockh = NULL;
196                 struct lmv_tgt_desc     *tgt = NULL;
197                 struct inode            *inode;
198
199                 fid = lsm->lsm_md_oinfo[i].lmo_fid;
200                 inode = lsm->lsm_md_oinfo[i].lmo_root;
201
202                 /*
203                  * Prepare op_data for revalidating. Note that @fid2 shluld be
204                  * defined otherwise it will go to server and take new lock
205                  * which is not needed here.
206                  */
207                 memset(op_data, 0, sizeof(*op_data));
208                 op_data->op_fid1 = fid;
209                 op_data->op_fid2 = fid;
210
211                 tgt = lmv_locate_mds(lmv, op_data, &fid);
212                 if (IS_ERR(tgt))
213                         GOTO(cleanup, rc = PTR_ERR(tgt));
214
215                 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%d\n",
216                        PFID(&fid), tgt->ltd_idx);
217
218                 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
219                                     cb_blocking, extra_lock_flags);
220                 if (rc < 0)
221                         GOTO(cleanup, rc);
222
223                 lockh = (struct lustre_handle *)&it.d.lustre.it_lock_handle;
224                 if (rc > 0 && req == NULL) {
225                         /* slave inode is still valid */
226                         CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
227                                PFID(&fid));
228                         rc = 0;
229                 } else {
230                         /* refresh slave from server */
231                         body = req_capsule_server_get(&req->rq_pill,
232                                                       &RMF_MDT_BODY);
233                         LASSERT(body != NULL);
234                         if (unlikely(body->mbo_nlink < 2)) {
235                                 CERROR("%s: nlink %d < 2 corrupt stripe %d "DFID
236                                        ":" DFID"\n",
237                                        obd->obd_name, body->mbo_nlink, i,
238                                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
239                                        PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
240
241                                 if (req != NULL)
242                                         ptlrpc_req_finished(req);
243
244                                 if (it.d.lustre.it_lock_mode && lockh) {
245                                         ldlm_lock_decref(lockh,
246                                                  it.d.lustre.it_lock_mode);
247                                         it.d.lustre.it_lock_mode = 0;
248                                 }
249
250                                 GOTO(cleanup, rc = -EIO);
251                         }
252
253
254                         i_size_write(inode, body->mbo_size);
255                         set_nlink(inode, body->mbo_nlink);
256                         LTIME_S(inode->i_atime) = body->mbo_atime;
257                         LTIME_S(inode->i_ctime) = body->mbo_ctime;
258                         LTIME_S(inode->i_mtime) = body->mbo_mtime;
259
260                         if (req != NULL)
261                                 ptlrpc_req_finished(req);
262                 }
263
264                 md_set_lock_data(tgt->ltd_exp, &lockh->cookie, inode, NULL);
265
266                 size += i_size_read(inode);
267
268                 if (i != 0)
269                         nlink += inode->i_nlink - 2;
270                 else
271                         nlink += inode->i_nlink;
272
273                 atime = LTIME_S(inode->i_atime) > atime ?
274                                 LTIME_S(inode->i_atime) : atime;
275                 ctime = LTIME_S(inode->i_ctime) > ctime ?
276                                 LTIME_S(inode->i_ctime) : ctime;
277                 mtime = LTIME_S(inode->i_mtime) > mtime ?
278                                 LTIME_S(inode->i_mtime) : mtime;
279
280                 if (it.d.lustre.it_lock_mode != 0 && lockh != NULL) {
281                         ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
282                         it.d.lustre.it_lock_mode = 0;
283                 }
284
285                 CDEBUG(D_INODE, "i %d "DFID" size %llu, nlink %u, atime "
286                        "%lu, mtime %lu, ctime %lu.\n", i, PFID(&fid),
287                        i_size_read(inode), inode->i_nlink,
288                        LTIME_S(inode->i_atime), LTIME_S(inode->i_mtime),
289                        LTIME_S(inode->i_ctime));
290         }
291
292         /*
293          * update attr of master request.
294          */
295         CDEBUG(D_INODE, "Return refreshed attrs: size = %lu nlink %lu atime "
296                LPU64 "ctime "LPU64" mtime "LPU64" for " DFID"\n", size, nlink,
297                atime, ctime, mtime, PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
298
299         if (mbody != NULL) {
300                 mbody->mbo_atime = atime;
301                 mbody->mbo_ctime = ctime;
302                 mbody->mbo_mtime = mtime;
303         }
304 cleanup:
305         OBD_FREE_PTR(op_data);
306         RETURN(rc);
307 }
308
309 #else
310
311 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
312                           struct lmv_stripe_md *lsm,
313                           ldlm_blocking_callback cb_blocking,
314                           int extra_lock_flags)
315 {
316         return 0;
317 }
318
319 #endif
320
321 /*
322  * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
323  * may be split dir.
324  */
325 int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
326                     struct lookup_intent *it, struct ptlrpc_request **reqp,
327                     ldlm_blocking_callback cb_blocking, __u64 extra_lock_flags)
328 {
329         struct obd_device       *obd = exp->exp_obd;
330         struct lmv_obd          *lmv = &obd->u.lmv;
331         struct lmv_tgt_desc     *tgt;
332         struct mdt_body         *body;
333         int                     rc;
334         ENTRY;
335
336         if (it->it_flags & MDS_OPEN_BY_FID) {
337                 LASSERT(fid_is_sane(&op_data->op_fid2));
338
339                 /* for striped directory, we can't know parent stripe fid
340                  * without name, but we can set it to child fid, and MDT
341                  * will obtain it from linkea in open in such case. */
342                 if (op_data->op_mea1 != NULL)
343                         op_data->op_fid1 = op_data->op_fid2;
344
345                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
346                 if (IS_ERR(tgt))
347                         RETURN(PTR_ERR(tgt));
348
349                 op_data->op_mds = tgt->ltd_idx;
350         } else {
351                 LASSERT(fid_is_sane(&op_data->op_fid1));
352                 LASSERT(fid_is_zero(&op_data->op_fid2));
353                 LASSERT(op_data->op_name != NULL);
354
355                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
356                 if (IS_ERR(tgt))
357                         RETURN(PTR_ERR(tgt));
358         }
359
360         /* If it is ready to open the file by FID, do not need
361          * allocate FID at all, otherwise it will confuse MDT */
362         if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
363                 /*
364                  * For lookup(IT_CREATE) cases allocate new fid and setup FLD
365                  * for it.
366                  */
367                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
368                 if (rc != 0)
369                         RETURN(rc);
370         }
371
372         CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
373                " name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
374                PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
375
376         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
377                             extra_lock_flags);
378         if (rc != 0)
379                 RETURN(rc);
380         /*
381          * Nothing is found, do not access body->fid1 as it is zero and thus
382          * pointless.
383          */
384         if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
385             !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
386             !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
387                 RETURN(rc);
388
389         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
390         if (body == NULL)
391                 RETURN(-EPROTO);
392
393         /* Not cross-ref case, just get out of here. */
394         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
395                 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
396                                        cb_blocking, extra_lock_flags);
397                 if (rc != 0)
398                         RETURN(rc);
399
400                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
401                 if (body == NULL)
402                         RETURN(-EPROTO);
403         }
404
405         RETURN(rc);
406 }
407
408 /*
409  * Handler for: getattr, lookup and revalidate cases.
410  */
411 static int
412 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
413                   struct lookup_intent *it, struct ptlrpc_request **reqp,
414                   ldlm_blocking_callback cb_blocking,
415                   __u64 extra_lock_flags)
416 {
417         struct obd_device       *obd = exp->exp_obd;
418         struct lmv_obd          *lmv = &obd->u.lmv;
419         struct lmv_tgt_desc     *tgt = NULL;
420         struct mdt_body         *body;
421         struct lmv_stripe_md    *lsm = op_data->op_mea1;
422         int                     rc = 0;
423         ENTRY;
424
425         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
426         if (IS_ERR(tgt))
427                 RETURN(PTR_ERR(tgt));
428
429         if (!fid_is_sane(&op_data->op_fid2))
430                 fid_zero(&op_data->op_fid2);
431
432         CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
433                ", name='%s' -> mds #%d lsm=%p lsm_magic=%x\n",
434                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
435                op_data->op_name ? op_data->op_name : "<NULL>",
436                tgt->ltd_idx, lsm, lsm == NULL ? -1 : lsm->lsm_md_magic);
437
438         op_data->op_bias &= ~MDS_CROSS_REF;
439
440         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
441                             extra_lock_flags);
442         if (rc < 0)
443                 RETURN(rc);
444
445         if (*reqp == NULL) {
446                 /* If RPC happens, lsm information will be revalidated
447                  * during update_inode process (see ll_update_lsm_md) */
448                 if (op_data->op_mea2 != NULL) {
449                         rc = lmv_revalidate_slaves(exp, NULL, op_data->op_mea2,
450                                                    cb_blocking,
451                                                    extra_lock_flags);
452                         if (rc != 0)
453                                 RETURN(rc);
454                 }
455                 RETURN(rc);
456         } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm != NULL &&
457                    lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) {
458                 /* For migrating directory, if it can not find the child in
459                  * the source directory(master stripe), try the targeting
460                  * directory(stripe 1) */
461                 tgt = lmv_find_target(lmv, &lsm->lsm_md_oinfo[1].lmo_fid);
462                 if (IS_ERR(tgt))
463                         RETURN(PTR_ERR(tgt));
464
465                 ptlrpc_req_finished(*reqp);
466                 it->d.lustre.it_data = NULL;
467                 *reqp = NULL;
468
469                 CDEBUG(D_INODE, "For migrating dir, try target dir "DFID"\n",
470                        PFID(&lsm->lsm_md_oinfo[1].lmo_fid));
471
472                 op_data->op_fid1 = lsm->lsm_md_oinfo[1].lmo_fid;
473                 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
474                 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
475                                     cb_blocking, extra_lock_flags);
476         }
477         /*
478          * MDS has returned success. Probably name has been resolved in
479          * remote inode. Let's check this.
480          */
481         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
482         if (body == NULL)
483                 RETURN(-EPROTO);
484
485         /* Not cross-ref case, just get out of here. */
486         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
487                 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
488                                        extra_lock_flags);
489                 if (rc != 0)
490                         RETURN(rc);
491                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
492                 if (body == NULL)
493                         RETURN(-EPROTO);
494         }
495
496         RETURN(rc);
497 }
498
499 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
500                     struct lookup_intent *it, struct ptlrpc_request **reqp,
501                     ldlm_blocking_callback cb_blocking,
502                     __u64 extra_lock_flags)
503 {
504         struct obd_device *obd = exp->exp_obd;
505         int                rc;
506         ENTRY;
507
508         LASSERT(it != NULL);
509         LASSERT(fid_is_sane(&op_data->op_fid1));
510
511         CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
512                 LL_IT2STR(it), PFID(&op_data->op_fid2), op_data->op_namelen,
513                 op_data->op_name, PFID(&op_data->op_fid1));
514
515         rc = lmv_check_connect(obd);
516         if (rc)
517                 RETURN(rc);
518
519         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
520                 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
521                                        extra_lock_flags);
522         else if (it->it_op & IT_OPEN)
523                 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
524                                      extra_lock_flags);
525         else
526                 LBUG();
527
528         RETURN(rc);
529 }