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