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