Whamcloud - gitweb
LU-5396 mdc: (and lmv, mgc, osc) make some functions static
[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 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/pagemap.h>
43 #include <linux/math64.h>
44 #include <linux/seq_file.h>
45 #include <linux/namei.h>
46 #include <lustre_intent.h>
47
48 #include <obd_support.h>
49 #include <lustre/lustre_idl.h>
50 #include <lustre_lib.h>
51 #include <lustre_net.h>
52 #include <lustre_dlm.h>
53 #include <lustre_mdc.h>
54 #include <obd_class.h>
55 #include <lprocfs_status.h>
56 #include "lmv_internal.h"
57
58 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
59                              const struct lu_fid *parent_fid,
60                              struct ptlrpc_request **reqp,
61                              ldlm_blocking_callback cb_blocking,
62                              __u64 extra_lock_flags)
63 {
64         struct obd_device       *obd = exp->exp_obd;
65         struct lmv_obd          *lmv = &obd->u.lmv;
66         struct ptlrpc_request   *req = NULL;
67         struct lustre_handle    plock;
68         struct md_op_data       *op_data;
69         struct lmv_tgt_desc     *tgt;
70         struct mdt_body         *body;
71         int                     pmode;
72         int                     rc = 0;
73         ENTRY;
74
75         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
76         if (body == NULL)
77                 RETURN(-EPROTO);
78
79         LASSERT((body->mbo_valid & OBD_MD_MDS));
80
81         /*
82          * Unfortunately, we have to lie to MDC/MDS to retrieve
83          * attributes llite needs and provideproper locking.
84          */
85         if (it->it_op & IT_LOOKUP)
86                 it->it_op = IT_GETATTR;
87
88         /*
89          * We got LOOKUP lock, but we really need attrs.
90          */
91         pmode = it->d.lustre.it_lock_mode;
92         if (pmode) {
93                 plock.cookie = it->d.lustre.it_lock_handle;
94                 it->d.lustre.it_lock_mode = 0;
95                 it->d.lustre.it_data = NULL;
96         }
97
98         LASSERT(fid_is_sane(&body->mbo_fid1));
99
100         tgt = lmv_find_target(lmv, &body->mbo_fid1);
101         if (IS_ERR(tgt))
102                 GOTO(out, rc = PTR_ERR(tgt));
103
104         OBD_ALLOC_PTR(op_data);
105         if (op_data == NULL)
106                 GOTO(out, rc = -ENOMEM);
107
108         op_data->op_fid1 = body->mbo_fid1;
109         /* Sent the parent FID to the remote MDT */
110         if (parent_fid != NULL) {
111                 /* The parent fid is only for remote open to
112                  * check whether the open is from OBF,
113                  * see mdt_cross_open */
114                 LASSERT(it->it_op & IT_OPEN);
115                 op_data->op_fid2 = *parent_fid;
116         }
117
118         op_data->op_bias = MDS_CROSS_REF;
119         CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%u\n",
120                PFID(&body->mbo_fid1), tgt->ltd_idx);
121
122         rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
123                             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         if (pmode) {
139                 it->d.lustre.it_lock_handle = plock.cookie;
140                 it->d.lustre.it_lock_mode = pmode;
141         }
142
143         EXIT;
144 out_free_op_data:
145         OBD_FREE_PTR(op_data);
146 out:
147         if (rc && pmode)
148                 ldlm_lock_decref(&plock, pmode);
149
150         ptlrpc_req_finished(*reqp);
151         *reqp = req;
152         return rc;
153 }
154
155 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
156                           struct lmv_stripe_md *lsm,
157                           ldlm_blocking_callback cb_blocking,
158                           int extra_lock_flags)
159 {
160         struct obd_device      *obd = exp->exp_obd;
161         struct lmv_obd         *lmv = &obd->u.lmv;
162         struct ptlrpc_request   *req = NULL;
163         struct mdt_body         *body;
164         struct md_op_data      *op_data;
165         unsigned long           size = 0;
166         unsigned long           nlink = 0;
167         obd_time                atime = 0;
168         obd_time                ctime = 0;
169         obd_time                mtime = 0;
170         int                     i;
171         int                     rc = 0;
172
173         ENTRY;
174
175         /**
176          * revalidate slaves has some problems, temporarily return,
177          * we may not need that
178          */
179         OBD_ALLOC_PTR(op_data);
180         if (op_data == NULL)
181                 RETURN(-ENOMEM);
182
183         /**
184          * Loop over the stripe information, check validity and update them
185          * from MDS if needed.
186          */
187         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
188                 struct lu_fid           fid;
189                 struct lookup_intent    it = { .it_op = IT_GETATTR };
190                 struct lustre_handle    *lockh = NULL;
191                 struct lmv_tgt_desc     *tgt = NULL;
192                 struct inode            *inode;
193
194                 fid = lsm->lsm_md_oinfo[i].lmo_fid;
195                 inode = lsm->lsm_md_oinfo[i].lmo_root;
196
197                 /*
198                  * Prepare op_data for revalidating. Note that @fid2 shluld be
199                  * defined otherwise it will go to server and take new lock
200                  * which is not needed here.
201                  */
202                 memset(op_data, 0, sizeof(*op_data));
203                 op_data->op_fid1 = fid;
204                 op_data->op_fid2 = fid;
205
206                 tgt = lmv_locate_mds(lmv, op_data, &fid);
207                 if (IS_ERR(tgt))
208                         GOTO(cleanup, rc = PTR_ERR(tgt));
209
210                 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%u\n",
211                        PFID(&fid), tgt->ltd_idx);
212
213                 if (req != NULL) {
214                         ptlrpc_req_finished(req);
215                         req = NULL;
216                 }
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 (it.d.lustre.it_lock_mode && lockh) {
242                                         ldlm_lock_decref(lockh,
243                                                  it.d.lustre.it_lock_mode);
244                                         it.d.lustre.it_lock_mode = 0;
245                                 }
246
247                                 GOTO(cleanup, rc = -EIO);
248                         }
249
250
251                         i_size_write(inode, body->mbo_size);
252                         set_nlink(inode, body->mbo_nlink);
253                         LTIME_S(inode->i_atime) = body->mbo_atime;
254                         LTIME_S(inode->i_ctime) = body->mbo_ctime;
255                         LTIME_S(inode->i_mtime) = body->mbo_mtime;
256                 }
257
258                 md_set_lock_data(tgt->ltd_exp, &lockh->cookie, inode, NULL);
259
260                 size += i_size_read(inode);
261
262                 if (i != 0)
263                         nlink += inode->i_nlink - 2;
264                 else
265                         nlink += inode->i_nlink;
266
267                 atime = LTIME_S(inode->i_atime) > atime ?
268                                 LTIME_S(inode->i_atime) : atime;
269                 ctime = LTIME_S(inode->i_ctime) > ctime ?
270                                 LTIME_S(inode->i_ctime) : ctime;
271                 mtime = LTIME_S(inode->i_mtime) > mtime ?
272                                 LTIME_S(inode->i_mtime) : mtime;
273
274                 if (it.d.lustre.it_lock_mode != 0 && lockh != NULL) {
275                         ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
276                         it.d.lustre.it_lock_mode = 0;
277                 }
278
279                 CDEBUG(D_INODE, "i %d "DFID" size %llu, nlink %u, atime "
280                        "%lu, mtime %lu, ctime %lu.\n", i, PFID(&fid),
281                        i_size_read(inode), inode->i_nlink,
282                        LTIME_S(inode->i_atime), LTIME_S(inode->i_mtime),
283                        LTIME_S(inode->i_ctime));
284         }
285
286         /*
287          * update attr of master request.
288          */
289         CDEBUG(D_INODE, "Return refreshed attrs: size = %lu nlink %lu atime "
290                LPU64 "ctime "LPU64" mtime "LPU64" for " DFID"\n", size, nlink,
291                atime, ctime, mtime, PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
292
293         if (mbody != NULL) {
294                 mbody->mbo_atime = atime;
295                 mbody->mbo_ctime = ctime;
296                 mbody->mbo_mtime = mtime;
297         }
298 cleanup:
299         if (req != NULL)
300                 ptlrpc_req_finished(req);
301
302         OBD_FREE_PTR(op_data);
303         RETURN(rc);
304 }
305
306
307 /*
308  * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
309  * may be split dir.
310  */
311 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
312                            struct lookup_intent *it,
313                            struct ptlrpc_request **reqp,
314                            ldlm_blocking_callback cb_blocking,
315                            __u64 extra_lock_flags)
316 {
317         struct obd_device       *obd = exp->exp_obd;
318         struct lmv_obd          *lmv = &obd->u.lmv;
319         struct lmv_tgt_desc     *tgt;
320         struct mdt_body         *body;
321         int                     rc;
322         ENTRY;
323
324         if (it->it_flags & MDS_OPEN_BY_FID) {
325                 LASSERT(fid_is_sane(&op_data->op_fid2));
326
327                 /* for striped directory, we can't know parent stripe fid
328                  * without name, but we can set it to child fid, and MDT
329                  * will obtain it from linkea in open in such case. */
330                 if (op_data->op_mea1 != NULL)
331                         op_data->op_fid1 = op_data->op_fid2;
332
333                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
334                 if (IS_ERR(tgt))
335                         RETURN(PTR_ERR(tgt));
336
337                 op_data->op_mds = tgt->ltd_idx;
338         } else {
339                 LASSERT(fid_is_sane(&op_data->op_fid1));
340                 LASSERT(fid_is_zero(&op_data->op_fid2));
341                 LASSERT(op_data->op_name != NULL);
342
343                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
344                 if (IS_ERR(tgt))
345                         RETURN(PTR_ERR(tgt));
346         }
347
348         /* If it is ready to open the file by FID, do not need
349          * allocate FID at all, otherwise it will confuse MDT */
350         if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
351                 /*
352                  * For lookup(IT_CREATE) cases allocate new fid and setup FLD
353                  * for it.
354                  */
355                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
356                 if (rc != 0)
357                         RETURN(rc);
358         }
359
360         CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
361                " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
362                PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
363
364         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
365                             extra_lock_flags);
366         if (rc != 0)
367                 RETURN(rc);
368         /*
369          * Nothing is found, do not access body->fid1 as it is zero and thus
370          * pointless.
371          */
372         if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
373             !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
374             !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
375                 RETURN(rc);
376
377         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
378         if (body == NULL)
379                 RETURN(-EPROTO);
380
381         /* Not cross-ref case, just get out of here. */
382         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
383                 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
384                                        cb_blocking, extra_lock_flags);
385                 if (rc != 0)
386                         RETURN(rc);
387
388                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
389                 if (body == NULL)
390                         RETURN(-EPROTO);
391         }
392
393         RETURN(rc);
394 }
395
396 /*
397  * Handler for: getattr, lookup and revalidate cases.
398  */
399 static int
400 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
401                   struct lookup_intent *it, struct ptlrpc_request **reqp,
402                   ldlm_blocking_callback cb_blocking,
403                   __u64 extra_lock_flags)
404 {
405         struct obd_device       *obd = exp->exp_obd;
406         struct lmv_obd          *lmv = &obd->u.lmv;
407         struct lmv_tgt_desc     *tgt = NULL;
408         struct mdt_body         *body;
409         struct lmv_stripe_md    *lsm = op_data->op_mea1;
410         int                     rc = 0;
411         ENTRY;
412
413         /* If it returns ERR_PTR(-EBADFD) then it is an unknown hash type
414          * it will try all stripes to locate the object */
415         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
416         if (IS_ERR(tgt) && (PTR_ERR(tgt) != -EBADFD))
417                 RETURN(PTR_ERR(tgt));
418
419         /* Both migrating dir and unknown hash dir need to try
420          * all of sub-stripes */
421         if (lsm != NULL && !lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
422                 struct lmv_oinfo *oinfo;
423
424                 oinfo = &lsm->lsm_md_oinfo[0];
425
426                 op_data->op_fid1 = oinfo->lmo_fid;
427                 op_data->op_mds = oinfo->lmo_mds;
428                 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
429                 if (IS_ERR(tgt))
430                         RETURN(PTR_ERR(tgt));
431         }
432
433         if (!fid_is_sane(&op_data->op_fid2))
434                 fid_zero(&op_data->op_fid2);
435
436         CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
437                ", name='%s' -> mds #%u lsm=%p lsm_magic=%x\n",
438                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
439                op_data->op_name ? op_data->op_name : "<NULL>",
440                tgt->ltd_idx, lsm, lsm == NULL ? -1 : lsm->lsm_md_magic);
441
442         op_data->op_bias &= ~MDS_CROSS_REF;
443
444         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
445                             extra_lock_flags);
446         if (rc < 0)
447                 RETURN(rc);
448
449         if (*reqp == NULL) {
450                 /* If RPC happens, lsm information will be revalidated
451                  * during update_inode process (see ll_update_lsm_md) */
452                 if (op_data->op_mea2 != NULL) {
453                         rc = lmv_revalidate_slaves(exp, NULL, op_data->op_mea2,
454                                                    cb_blocking,
455                                                    extra_lock_flags);
456                         if (rc != 0)
457                                 RETURN(rc);
458                 }
459                 RETURN(rc);
460         } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm != NULL &&
461                    lmv_need_try_all_stripes(lsm)) {
462                 /* For migrating and unknown hash type directory, it will
463                  * try to target the entry on other stripes */
464                 int stripe_index;
465
466                 for (stripe_index = 1;
467                      stripe_index < lsm->lsm_md_stripe_count &&
468                      it_disposition(it, DISP_LOOKUP_NEG); stripe_index++) {
469                         struct lmv_oinfo *oinfo;
470
471                         /* release the previous request */
472                         ptlrpc_req_finished(*reqp);
473                         it->d.lustre.it_data = NULL;
474                         *reqp = NULL;
475
476                         oinfo = &lsm->lsm_md_oinfo[stripe_index];
477                         tgt = lmv_find_target(lmv, &oinfo->lmo_fid);
478                         if (IS_ERR(tgt))
479                                 RETURN(PTR_ERR(tgt));
480
481                         CDEBUG(D_INODE, "Try other stripes " DFID"\n",
482                                PFID(&oinfo->lmo_fid));
483
484                         op_data->op_fid1 = oinfo->lmo_fid;
485                         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
486                         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
487                                             cb_blocking, extra_lock_flags);
488                         if (rc != 0)
489                                 RETURN(rc);
490                 }
491         }
492
493         /*
494          * MDS has returned success. Probably name has been resolved in
495          * remote inode. Let's check this.
496          */
497         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
498         if (body == NULL)
499                 RETURN(-EPROTO);
500
501         /* Not cross-ref case, just get out of here. */
502         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
503                 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
504                                        extra_lock_flags);
505                 if (rc != 0)
506                         RETURN(rc);
507                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
508                 if (body == NULL)
509                         RETURN(-EPROTO);
510         }
511
512         RETURN(rc);
513 }
514
515 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
516                     struct lookup_intent *it, struct ptlrpc_request **reqp,
517                     ldlm_blocking_callback cb_blocking,
518                     __u64 extra_lock_flags)
519 {
520         struct obd_device *obd = exp->exp_obd;
521         int                rc;
522         ENTRY;
523
524         LASSERT(it != NULL);
525         LASSERT(fid_is_sane(&op_data->op_fid1));
526
527         CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
528                 LL_IT2STR(it), PFID(&op_data->op_fid2),
529                 (int)op_data->op_namelen, op_data->op_name,
530                 PFID(&op_data->op_fid1));
531
532         rc = lmv_check_connect(obd);
533         if (rc)
534                 RETURN(rc);
535
536         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
537                 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
538                                        extra_lock_flags);
539         else if (it->it_op & IT_OPEN)
540                 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
541                                      extra_lock_flags);
542         else
543                 LBUG();
544
545         if (rc < 0) {
546                 struct lustre_handle lock_handle;
547
548                 if (it->d.lustre.it_lock_mode != 0) {
549                         lock_handle.cookie = it->d.lustre.it_lock_handle;
550                         ldlm_lock_decref(&lock_handle,
551                                          it->d.lustre.it_lock_mode);
552                 }
553
554                 it->d.lustre.it_lock_handle = 0;
555                 it->d.lustre.it_lock_mode = 0;
556
557                 if (it->d.lustre.it_remote_lock_mode != 0) {
558                         lock_handle.cookie = it->d.lustre.it_remote_lock_handle;
559                         ldlm_lock_decref(&lock_handle,
560                                          it->d.lustre.it_remote_lock_mode);
561                 }
562
563                 it->d.lustre.it_remote_lock_handle = 0;
564                 it->d.lustre.it_remote_lock_mode = 0;
565         }
566
567         RETURN(rc);
568 }