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