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