Whamcloud - gitweb
LU-14172 lmv: optimize dir shard revalidate
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LMV
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/pagemap.h>
39 #include <linux/math64.h>
40 #include <linux/seq_file.h>
41 #include <linux/namei.h>
42 #include <lustre_intent.h>
43
44 #include <obd_support.h>
45 #include <lustre_lib.h>
46 #include <lustre_net.h>
47 #include <lustre_dlm.h>
48 #include <lustre_mdc.h>
49 #include <obd_class.h>
50 #include <lprocfs_status.h>
51 #include "lmv_internal.h"
52
53 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
54                              const struct lu_fid *parent_fid,
55                              struct ptlrpc_request **reqp,
56                              ldlm_blocking_callback cb_blocking,
57                              __u64 extra_lock_flags)
58 {
59         struct obd_device       *obd = exp->exp_obd;
60         struct lmv_obd          *lmv = &obd->u.lmv;
61         struct ptlrpc_request   *req = NULL;
62         struct lustre_handle    plock;
63         struct md_op_data       *op_data;
64         struct lmv_tgt_desc     *tgt;
65         struct mdt_body         *body;
66         int                     pmode;
67         int                     rc = 0;
68         ENTRY;
69
70         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
71         if (body == NULL)
72                 RETURN(-EPROTO);
73
74         LASSERT((body->mbo_valid & OBD_MD_MDS));
75
76         /*
77          * We got LOOKUP lock, but we really need attrs.
78          */
79         pmode = it->it_lock_mode;
80         if (pmode) {
81                 plock.cookie = it->it_lock_handle;
82                 it->it_lock_mode = 0;
83                 it->it_request = NULL;
84         }
85
86         LASSERT(fid_is_sane(&body->mbo_fid1));
87
88         tgt = lmv_find_target(lmv, &body->mbo_fid1);
89         if (IS_ERR(tgt))
90                 GOTO(out, rc = PTR_ERR(tgt));
91
92         OBD_ALLOC_PTR(op_data);
93         if (op_data == NULL)
94                 GOTO(out, rc = -ENOMEM);
95
96         op_data->op_fid1 = body->mbo_fid1;
97         /* Sent the parent FID to the remote MDT */
98         if (parent_fid != NULL) {
99                 /* The parent fid is only for remote open to
100                  * check whether the open is from OBF,
101                  * see mdt_cross_open */
102                 LASSERT(it->it_op & IT_OPEN);
103                 op_data->op_fid2 = *parent_fid;
104         }
105
106         op_data->op_bias = MDS_CROSS_REF;
107         CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%u\n",
108                PFID(&body->mbo_fid1), tgt->ltd_idx);
109
110         rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
111                             extra_lock_flags);
112         if (rc)
113                 GOTO(out_free_op_data, rc);
114
115         /*
116          * LLite needs LOOKUP lock to track dentry revocation in order to
117          * maintain dcache consistency. Thus drop UPDATE|PERM lock here
118          * and put LOOKUP in request.
119          */
120         if (it->it_lock_mode != 0) {
121                 it->it_remote_lock_handle =
122                                         it->it_lock_handle;
123                 it->it_remote_lock_mode = it->it_lock_mode;
124         }
125
126         if (pmode) {
127                 it->it_lock_handle = plock.cookie;
128                 it->it_lock_mode = pmode;
129         }
130
131         EXIT;
132 out_free_op_data:
133         OBD_FREE_PTR(op_data);
134 out:
135         if (rc && pmode)
136                 ldlm_lock_decref(&plock, pmode);
137
138         ptlrpc_req_finished(*reqp);
139         *reqp = req;
140         return rc;
141 }
142
143 int lmv_revalidate_slaves(struct obd_export *exp,
144                           const struct lmv_stripe_md *lsm,
145                           ldlm_blocking_callback cb_blocking,
146                           int extra_lock_flags)
147 {
148         struct obd_device *obd = exp->exp_obd;
149         struct lmv_obd *lmv = &obd->u.lmv;
150         struct ptlrpc_request *req = NULL;
151         struct mdt_body *body;
152         struct md_op_data *op_data;
153         int i;
154         int valid_stripe_count = 0;
155         int rc = 0;
156
157         ENTRY;
158
159         /**
160          * revalidate slaves has some problems, temporarily return,
161          * we may not need that
162          */
163         OBD_ALLOC_PTR(op_data);
164         if (op_data == NULL)
165                 RETURN(-ENOMEM);
166
167         /**
168          * Loop over the stripe information, check validity and update them
169          * from MDS if needed.
170          */
171         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
172                 struct lu_fid           fid;
173                 struct lookup_intent    it = { .it_op = IT_GETATTR };
174                 struct lustre_handle    *lockh = NULL;
175                 struct lmv_tgt_desc     *tgt = NULL;
176                 struct inode            *inode;
177
178                 fid = lsm->lsm_md_oinfo[i].lmo_fid;
179                 inode = lsm->lsm_md_oinfo[i].lmo_root;
180
181                 if (!inode)
182                         continue;
183
184                 /*
185                  * Prepare op_data for revalidating. Note that @fid2 shluld be
186                  * defined otherwise it will go to server and take new lock
187                  * which is not needed here.
188                  */
189                 memset(op_data, 0, sizeof(*op_data));
190                 op_data->op_fid1 = fid;
191                 op_data->op_fid2 = fid;
192                 /* shard revalidate only needs to fetch attributes and UPDATE
193                  * lock, which is similar to the bottom half of remote object
194                  * getattr, set this flag so that MDT skips checking whether
195                  * it's remote object.
196                  */
197                 op_data->op_bias = MDS_CROSS_REF;
198
199                 tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds, NULL);
200                 if (IS_ERR(tgt))
201                         GOTO(cleanup, rc = PTR_ERR(tgt));
202
203                 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%u\n",
204                        PFID(&fid), tgt->ltd_idx);
205
206                 if (req != NULL) {
207                         ptlrpc_req_finished(req);
208                         req = NULL;
209                 }
210
211                 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
212                                     cb_blocking, extra_lock_flags);
213                 if (rc == -ENOENT) {
214                         /* skip stripe is not exists */
215                         rc = 0;
216                         continue;
217                 }
218
219                 if (rc < 0)
220                         GOTO(cleanup, rc);
221
222                 lockh = (struct lustre_handle *)&it.it_lock_handle;
223                 if (rc > 0 && req == NULL) {
224                         /* slave inode is still valid */
225                         CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
226                                PFID(&fid));
227                         rc = 0;
228                 } else {
229                         /* refresh slave from server */
230                         body = req_capsule_server_get(&req->rq_pill,
231                                                       &RMF_MDT_BODY);
232                         if (body == NULL) {
233                                 if (it.it_lock_mode && lockh) {
234                                         ldlm_lock_decref(lockh,
235                                                  it.it_lock_mode);
236                                         it.it_lock_mode = 0;
237                                 }
238                                 GOTO(cleanup, rc = -ENOENT);
239                         }
240
241                         i_size_write(inode, body->mbo_size);
242                         inode->i_blocks = body->mbo_blocks;
243                         set_nlink(inode, body->mbo_nlink);
244                         inode->i_atime.tv_sec = body->mbo_atime;
245                         inode->i_ctime.tv_sec = body->mbo_ctime;
246                         inode->i_mtime.tv_sec = body->mbo_mtime;
247                 }
248
249                 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
250                 if (it.it_lock_mode != 0 && lockh != NULL) {
251                         ldlm_lock_decref(lockh, it.it_lock_mode);
252                         it.it_lock_mode = 0;
253                 }
254
255                 valid_stripe_count++;
256         }
257
258 cleanup:
259         if (req != NULL)
260                 ptlrpc_req_finished(req);
261
262         /* if all stripes are invalid, return -ENOENT to notify user */
263         if (!rc && !valid_stripe_count)
264                 rc = -ENOENT;
265
266         OBD_FREE_PTR(op_data);
267         RETURN(rc);
268 }
269
270 /*
271  * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
272  * may be split dir.
273  */
274 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
275                            struct lookup_intent *it,
276                            struct ptlrpc_request **reqp,
277                            ldlm_blocking_callback cb_blocking,
278                            __u64 extra_lock_flags)
279 {
280         struct obd_device *obd = exp->exp_obd;
281         struct lmv_obd *lmv = &obd->u.lmv;
282         struct lmv_tgt_desc *tgt;
283         struct mdt_body *body;
284         __u64 flags = it->it_flags;
285         int rc;
286
287         ENTRY;
288
289         if ((it->it_op & IT_CREAT) && !(flags & MDS_OPEN_BY_FID)) {
290                 /* don't allow create under dir with bad hash */
291                 if (lmv_is_dir_bad_hash(op_data->op_mea1))
292                         RETURN(-EBADF);
293
294                 if (lmv_is_dir_migrating(op_data->op_mea1)) {
295                         if (flags & O_EXCL) {
296                                 /*
297                                  * open(O_CREAT | O_EXCL) needs to check
298                                  * existing name, which should be done on both
299                                  * old and new layout, to avoid creating new
300                                  * file under old layout, check old layout on
301                                  * client side.
302                                  */
303                                 tgt = lmv_locate_tgt(lmv, op_data,
304                                                      &op_data->op_fid1);
305                                 if (IS_ERR(tgt))
306                                         RETURN(PTR_ERR(tgt));
307
308                                 rc = md_getattr_name(tgt->ltd_exp, op_data,
309                                                      reqp);
310                                 if (!rc) {
311                                         ptlrpc_req_finished(*reqp);
312                                         *reqp = NULL;
313                                         RETURN(-EEXIST);
314                                 }
315
316                                 if (rc != -ENOENT)
317                                         RETURN(rc);
318
319                                 op_data->op_post_migrate = true;
320                         } else {
321                                 /*
322                                  * open(O_CREAT) will be sent to MDT in old
323                                  * layout first, to avoid creating new file
324                                  * under old layout, clear O_CREAT.
325                                  */
326                                 it->it_flags &= ~O_CREAT;
327                         }
328                 }
329         }
330
331 retry:
332         if (it->it_flags & MDS_OPEN_BY_FID) {
333                 LASSERT(fid_is_sane(&op_data->op_fid2));
334
335                 /* for striped directory, we can't know parent stripe fid
336                  * without name, but we can set it to child fid, and MDT
337                  * will obtain it from linkea in open in such case. */
338                 if (op_data->op_mea1 != NULL)
339                         op_data->op_fid1 = op_data->op_fid2;
340
341                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
342                 if (IS_ERR(tgt))
343                         RETURN(PTR_ERR(tgt));
344
345                 op_data->op_mds = tgt->ltd_idx;
346         } else {
347                 LASSERT(fid_is_sane(&op_data->op_fid1));
348                 LASSERT(fid_is_zero(&op_data->op_fid2));
349                 LASSERT(op_data->op_name != NULL);
350
351                 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
352                 if (IS_ERR(tgt))
353                         RETURN(PTR_ERR(tgt));
354         }
355
356         /* If it is ready to open the file by FID, do not need
357          * allocate FID at all, otherwise it will confuse MDT */
358         if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
359                 /*
360                  * For lookup(IT_CREATE) cases allocate new fid and setup FLD
361                  * for it.
362                  */
363                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
364                 if (rc != 0)
365                         RETURN(rc);
366         }
367
368         CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
369                " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
370                PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
371
372         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
373                             extra_lock_flags);
374         if (rc != 0)
375                 RETURN(rc);
376         /*
377          * Nothing is found, do not access body->fid1 as it is zero and thus
378          * pointless.
379          */
380         if ((it->it_disposition & DISP_LOOKUP_NEG) &&
381             !(it->it_disposition & DISP_OPEN_CREATE) &&
382             !(it->it_disposition & DISP_OPEN_OPEN)) {
383                 if (!(it->it_flags & MDS_OPEN_BY_FID) &&
384                     lmv_dir_retry_check_update(op_data)) {
385                         ptlrpc_req_finished(*reqp);
386                         it->it_request = NULL;
387                         it->it_disposition = 0;
388                         *reqp = NULL;
389
390                         it->it_flags = flags;
391                         fid_zero(&op_data->op_fid2);
392                         goto retry;
393                 }
394
395                 RETURN(rc);
396         }
397
398         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
399         if (body == NULL)
400                 RETURN(-EPROTO);
401
402         /* Not cross-ref case, just get out of here. */
403         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
404                 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
405                                        cb_blocking, extra_lock_flags);
406                 if (rc != 0)
407                         RETURN(rc);
408
409                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
410                 if (body == NULL)
411                         RETURN(-EPROTO);
412         }
413
414         RETURN(rc);
415 }
416
417 /*
418  * Handler for: getattr, lookup and revalidate cases.
419  */
420 static int
421 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
422                   struct lookup_intent *it, struct ptlrpc_request **reqp,
423                   ldlm_blocking_callback cb_blocking,
424                   __u64 extra_lock_flags)
425 {
426         struct obd_device *obd = exp->exp_obd;
427         struct lmv_obd *lmv = &obd->u.lmv;
428         struct lmv_tgt_desc *tgt = NULL;
429         struct mdt_body *body;
430         int rc;
431         ENTRY;
432
433 retry:
434         if (op_data->op_flags & MF_GETATTR_BY_FID) {
435                 /* getattr by FID, replace fid1 with stripe FID */
436                 LASSERT(op_data->op_name);
437                 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
438                 if (IS_ERR(tgt))
439                         RETURN(PTR_ERR(tgt));
440
441                 /* name is used to locate stripe target, clear it here
442                  * to avoid packing name in request, so that MDS knows
443                  * it's getattr by FID.
444                  */
445                 op_data->op_name = NULL;
446                 op_data->op_namelen = 0;
447
448                 /* getattr request is sent to MDT where fid2 inode is */
449                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
450         } else if (op_data->op_name) {
451                 /* getattr by name */
452                 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
453                 if (!fid_is_sane(&op_data->op_fid2))
454                         fid_zero(&op_data->op_fid2);
455         } else {
456                 /* old way to getattr by FID, parent FID not packed */
457                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
458         }
459         if (IS_ERR(tgt))
460                 RETURN(PTR_ERR(tgt));
461
462         CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
463                ", name='%s' -> mds #%u\n",
464                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
465                op_data->op_name ? op_data->op_name : "<NULL>",
466                tgt->ltd_idx);
467
468         op_data->op_bias &= ~MDS_CROSS_REF;
469
470         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
471                             extra_lock_flags);
472         if (rc < 0)
473                 RETURN(rc);
474
475         if (*reqp == NULL) {
476                 /* If RPC happens, lsm information will be revalidated
477                  * during update_inode process (see ll_update_lsm_md) */
478                 if (op_data->op_mea2 != NULL) {
479                         rc = lmv_revalidate_slaves(exp, op_data->op_mea2,
480                                                    cb_blocking,
481                                                    extra_lock_flags);
482                         if (rc != 0)
483                                 RETURN(rc);
484                 }
485                 RETURN(rc);
486         } else if (it_disposition(it, DISP_LOOKUP_NEG) &&
487                    lmv_dir_retry_check_update(op_data)) {
488                 ptlrpc_req_finished(*reqp);
489                 it->it_request = NULL;
490                 it->it_disposition = 0;
491                 *reqp = NULL;
492
493                 goto retry;
494         }
495
496         if (!it_has_reply_body(it))
497                 RETURN(0);
498
499         /*
500          * MDS has returned success. Probably name has been resolved in
501          * remote inode. Let's check this.
502          */
503         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
504         if (body == NULL)
505                 RETURN(-EPROTO);
506
507         /* Not cross-ref case, just get out of here. */
508         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
509                 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
510                                        extra_lock_flags);
511                 if (rc != 0)
512                         RETURN(rc);
513                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
514                 if (body == NULL)
515                         RETURN(-EPROTO);
516         }
517
518         RETURN(rc);
519 }
520
521 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
522                     struct lookup_intent *it, struct ptlrpc_request **reqp,
523                     ldlm_blocking_callback cb_blocking,
524                     __u64 extra_lock_flags)
525 {
526         int rc;
527         ENTRY;
528
529         LASSERT(it != NULL);
530         LASSERT(fid_is_sane(&op_data->op_fid1));
531
532         CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
533                 LL_IT2STR(it), PFID(&op_data->op_fid2),
534                 (int)op_data->op_namelen, op_data->op_name,
535                 PFID(&op_data->op_fid1));
536
537         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT | IT_GETXATTR))
538                 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
539                                        extra_lock_flags);
540         else if (it->it_op & IT_OPEN)
541                 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
542                                      extra_lock_flags);
543         else
544                 LBUG();
545
546         if (rc < 0) {
547                 struct lustre_handle lock_handle;
548
549                 if (it->it_lock_mode != 0) {
550                         lock_handle.cookie = it->it_lock_handle;
551                         ldlm_lock_decref_and_cancel(&lock_handle,
552                                                     it->it_lock_mode);
553                 }
554
555                 it->it_lock_handle = 0;
556                 it->it_lock_mode = 0;
557
558                 if (it->it_remote_lock_mode != 0) {
559                         lock_handle.cookie = it->it_remote_lock_handle;
560                         ldlm_lock_decref_and_cancel(&lock_handle,
561                                                     it->it_remote_lock_mode);
562                 }
563
564                 it->it_remote_lock_handle = 0;
565                 it->it_remote_lock_mode = 0;
566         }
567
568         RETURN(rc);
569 }