Whamcloud - gitweb
LU-13437 llite: pack parent FID in getattr
[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 lu_fid *pfid,
156                           const struct lmv_stripe_md *lsm,
157                           ldlm_blocking_callback cb_blocking,
158                           int extra_lock_flags)
159 {
160         struct obd_device *obd = exp->exp_obd;
161         struct lmv_obd *lmv = &obd->u.lmv;
162         struct ptlrpc_request *req = NULL;
163         struct mdt_body *body;
164         struct md_op_data *op_data;
165         int i;
166         int valid_stripe_count = 0;
167         int rc = 0;
168
169         ENTRY;
170
171         /**
172          * revalidate slaves has some problems, temporarily return,
173          * we may not need that
174          */
175         OBD_ALLOC_PTR(op_data);
176         if (op_data == NULL)
177                 RETURN(-ENOMEM);
178
179         /**
180          * Loop over the stripe information, check validity and update them
181          * from MDS if needed.
182          */
183         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
184                 struct lu_fid           fid;
185                 struct lookup_intent    it = { .it_op = IT_GETATTR };
186                 struct lustre_handle    *lockh = NULL;
187                 struct lmv_tgt_desc     *tgt = NULL;
188                 struct inode            *inode;
189
190                 fid = lsm->lsm_md_oinfo[i].lmo_fid;
191                 inode = lsm->lsm_md_oinfo[i].lmo_root;
192
193                 if (!inode)
194                         continue;
195
196                 /*
197                  * Prepare op_data for revalidating. Note that @fid2 shluld be
198                  * defined otherwise it will go to server and take new lock
199                  * which is not needed here.
200                  */
201                 memset(op_data, 0, sizeof(*op_data));
202                 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
203                         op_data->op_fid1 = *pfid;
204                 else
205                         op_data->op_fid1 = fid;
206                 op_data->op_fid2 = fid;
207
208                 tgt = lmv_tgt(lmv, lsm->lsm_md_oinfo[i].lmo_mds);
209                 if (!tgt)
210                         GOTO(cleanup, rc = -ENODEV);
211
212                 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%u\n",
213                        PFID(&fid), tgt->ltd_index);
214
215                 if (req != NULL) {
216                         ptlrpc_req_finished(req);
217                         req = NULL;
218                 }
219
220                 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
221                                     cb_blocking, extra_lock_flags);
222                 if (rc == -ENOENT) {
223                         /* skip stripe is not exists */
224                         rc = 0;
225                         continue;
226                 }
227
228                 if (rc < 0)
229                         GOTO(cleanup, rc);
230
231                 lockh = (struct lustre_handle *)&it.it_lock_handle;
232                 if (rc > 0 && req == NULL) {
233                         /* slave inode is still valid */
234                         CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
235                                PFID(&fid));
236                         rc = 0;
237                 } else {
238                         /* refresh slave from server */
239                         body = req_capsule_server_get(&req->rq_pill,
240                                                       &RMF_MDT_BODY);
241                         if (body == NULL) {
242                                 if (it.it_lock_mode && lockh) {
243                                         ldlm_lock_decref(lockh,
244                                                  it.it_lock_mode);
245                                         it.it_lock_mode = 0;
246                                 }
247                                 GOTO(cleanup, rc = -ENOENT);
248                         }
249
250                         i_size_write(inode, body->mbo_size);
251                         inode->i_blocks = body->mbo_blocks;
252                         set_nlink(inode, body->mbo_nlink);
253                         inode->i_atime.tv_sec = body->mbo_atime;
254                         inode->i_ctime.tv_sec = body->mbo_ctime;
255                         inode->i_mtime.tv_sec = body->mbo_mtime;
256                 }
257
258                 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
259                 if (it.it_lock_mode != 0 && lockh != NULL) {
260                         ldlm_lock_decref(lockh, it.it_lock_mode);
261                         it.it_lock_mode = 0;
262                 }
263
264                 valid_stripe_count++;
265         }
266
267 cleanup:
268         if (req != NULL)
269                 ptlrpc_req_finished(req);
270
271         /* if all stripes are invalid, return -ENOENT to notify user */
272         if (!rc && !valid_stripe_count)
273                 rc = -ENOENT;
274
275         OBD_FREE_PTR(op_data);
276         RETURN(rc);
277 }
278
279 /*
280  * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
281  * may be split dir.
282  */
283 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
284                            struct lookup_intent *it,
285                            struct ptlrpc_request **reqp,
286                            ldlm_blocking_callback cb_blocking,
287                            __u64 extra_lock_flags)
288 {
289         struct obd_device *obd = exp->exp_obd;
290         struct lmv_obd *lmv = &obd->u.lmv;
291         struct lmv_tgt_desc *tgt;
292         struct mdt_body *body;
293         __u64 flags = it->it_flags;
294         int rc;
295
296         ENTRY;
297
298         /* do not allow file creation in foreign dir */
299         if ((it->it_op & IT_CREAT) && lmv_dir_foreign(op_data->op_mea1))
300                 RETURN(-ENODATA);
301
302         if ((it->it_op & IT_CREAT) && !(flags & MDS_OPEN_BY_FID)) {
303                 /* don't allow create under dir with bad hash */
304                 if (lmv_dir_bad_hash(op_data->op_mea1))
305                         RETURN(-EBADF);
306
307                 if (lmv_dir_layout_changing(op_data->op_mea1)) {
308                         if (flags & O_EXCL) {
309                                 /*
310                                  * open(O_CREAT | O_EXCL) needs to check
311                                  * existing name, which should be done on both
312                                  * old and new layout, check old layout on
313                                  * client side.
314                                  */
315                                 rc = lmv_old_layout_lookup(lmv, op_data);
316                                 if (rc != -ENOENT)
317                                         RETURN(rc);
318
319                                 op_data->op_new_layout = 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 (lmv_dir_striped(op_data->op_mea1))
339                         op_data->op_fid1 = op_data->op_fid2;
340
341                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
342                 if (IS_ERR(tgt))
343                         RETURN(PTR_ERR(tgt));
344
345                 op_data->op_mds = tgt->ltd_index;
346         } else {
347                 LASSERT(fid_is_sane(&op_data->op_fid1));
348                 LASSERT(it->it_flags & MDS_OPEN_PCC ||
349                         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                                         it->it_flags & MDS_OPEN_PCC)) {
361                 /*
362                  * For lookup(IT_CREATE) cases allocate new fid and setup FLD
363                  * for it.
364                  */
365                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
366                 if (rc != 0)
367                         RETURN(rc);
368         }
369
370         CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
371                " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
372                PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_index);
373
374         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
375                             extra_lock_flags);
376         if (rc != 0)
377                 RETURN(rc);
378         /*
379          * Nothing is found, do not access body->fid1 as it is zero and thus
380          * pointless.
381          */
382         if ((it->it_disposition & DISP_LOOKUP_NEG) &&
383             !(it->it_disposition & DISP_OPEN_CREATE) &&
384             !(it->it_disposition & DISP_OPEN_OPEN)) {
385                 if (!(it->it_flags & MDS_OPEN_BY_FID) &&
386                     lmv_dir_retry_check_update(op_data)) {
387                         ptlrpc_req_finished(*reqp);
388                         it->it_request = NULL;
389                         it->it_disposition = 0;
390                         *reqp = NULL;
391
392                         it->it_flags = flags;
393                         fid_zero(&op_data->op_fid2);
394                         goto retry;
395                 }
396
397                 RETURN(rc);
398         }
399
400         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
401         if (body == NULL)
402                 RETURN(-EPROTO);
403
404         /* Not cross-ref case, just get out of here. */
405         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
406                 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
407                                        cb_blocking, extra_lock_flags,
408                                        op_data->op_file_secctx_name,
409                                        op_data->op_file_secctx_name_size);
410                 if (rc != 0)
411                         RETURN(rc);
412
413                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
414                 if (body == NULL)
415                         RETURN(-EPROTO);
416         }
417
418         RETURN(rc);
419 }
420
421 /*
422  * Handler for: getattr, lookup and revalidate cases.
423  */
424 static int
425 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
426                   struct lookup_intent *it, struct ptlrpc_request **reqp,
427                   ldlm_blocking_callback cb_blocking,
428                   __u64 extra_lock_flags)
429 {
430         struct obd_device *obd = exp->exp_obd;
431         struct lmv_obd *lmv = &obd->u.lmv;
432         struct lmv_tgt_desc *tgt = NULL;
433         struct mdt_body *body;
434         int rc;
435         ENTRY;
436
437         /* foreign dir is not striped */
438         if (lmv_dir_foreign(op_data->op_mea1)) {
439                 /* only allow getattr/lookup for itself */
440                 if (op_data->op_name != NULL)
441                         RETURN(-ENODATA);
442                 RETURN(0);
443         }
444
445 retry:
446         if (op_data->op_name) {
447                 tgt = lmv_locate_tgt(lmv, op_data);
448                 if (!fid_is_sane(&op_data->op_fid2))
449                         fid_zero(&op_data->op_fid2);
450         } else if (fid_is_sane(&op_data->op_fid2)) {
451                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
452         } else {
453                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
454         }
455         if (IS_ERR(tgt))
456                 RETURN(PTR_ERR(tgt));
457
458         CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
459                ", name='%s' -> mds #%u\n",
460                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
461                op_data->op_name ? op_data->op_name : "<NULL>",
462                tgt->ltd_index);
463
464         op_data->op_bias &= ~MDS_CROSS_REF;
465
466         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
467                             extra_lock_flags);
468         if (rc < 0)
469                 RETURN(rc);
470
471         if (*reqp == NULL) {
472                 /* If RPC happens, lsm information will be revalidated
473                  * during update_inode process (see ll_update_lsm_md) */
474                 if (lmv_dir_striped(op_data->op_mea2)) {
475                         rc = lmv_revalidate_slaves(exp, &op_data->op_fid2,
476                                                    op_data->op_mea2,
477                                                    cb_blocking,
478                                                    extra_lock_flags);
479                         if (rc != 0)
480                                 RETURN(rc);
481                 }
482                 RETURN(rc);
483         } else if (it_disposition(it, DISP_LOOKUP_NEG) &&
484                    lmv_dir_retry_check_update(op_data)) {
485                 ptlrpc_req_finished(*reqp);
486                 it->it_request = NULL;
487                 it->it_disposition = 0;
488                 *reqp = NULL;
489
490                 goto retry;
491         }
492
493         if (!it_has_reply_body(it))
494                 RETURN(0);
495
496         /*
497          * MDS has returned success. Probably name has been resolved in
498          * remote inode. Let's check this.
499          */
500         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
501         if (body == NULL)
502                 RETURN(-EPROTO);
503
504         /* Not cross-ref case, just get out of here. */
505         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
506                 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
507                                        extra_lock_flags,
508                                        op_data->op_file_secctx_name,
509                                        op_data->op_file_secctx_name_size);
510                 if (rc != 0)
511                         RETURN(rc);
512                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
513                 if (body == NULL)
514                         RETURN(-EPROTO);
515         }
516
517         RETURN(rc);
518 }
519
520 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
521                     struct lookup_intent *it, struct ptlrpc_request **reqp,
522                     ldlm_blocking_callback cb_blocking,
523                     __u64 extra_lock_flags)
524 {
525         int rc;
526         ENTRY;
527
528         LASSERT(it != NULL);
529         LASSERT(fid_is_sane(&op_data->op_fid1));
530
531         CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
532                 LL_IT2STR(it), PFID(&op_data->op_fid2),
533                 (int)op_data->op_namelen, op_data->op_name,
534                 PFID(&op_data->op_fid1));
535
536         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT | IT_GETXATTR))
537                 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
538                                        extra_lock_flags);
539         else if (it->it_op & IT_OPEN)
540                 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
541                                      extra_lock_flags);
542         else
543                 LBUG();
544
545         if (rc < 0) {
546                 struct lustre_handle lock_handle;
547
548                 if (it->it_lock_mode != 0) {
549                         lock_handle.cookie = it->it_lock_handle;
550                         ldlm_lock_decref_and_cancel(&lock_handle,
551                                                     it->it_lock_mode);
552                 }
553
554                 it->it_lock_handle = 0;
555                 it->it_lock_mode = 0;
556
557                 if (it->it_remote_lock_mode != 0) {
558                         lock_handle.cookie = it->it_remote_lock_handle;
559                         ldlm_lock_decref_and_cancel(&lock_handle,
560                                                     it->it_remote_lock_mode);
561                 }
562
563                 it->it_remote_lock_handle = 0;
564                 it->it_remote_lock_mode = 0;
565         }
566
567         RETURN(rc);
568 }