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