Whamcloud - gitweb
LU-10092 llite: Add persistent cache on client
[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_find_target(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_idx);
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
204                 tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds, NULL);
205                 if (IS_ERR(tgt))
206                         GOTO(cleanup, rc = PTR_ERR(tgt));
207
208                 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%u\n",
209                        PFID(&fid), tgt->ltd_idx);
210
211                 if (req != NULL) {
212                         ptlrpc_req_finished(req);
213                         req = NULL;
214                 }
215
216                 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
217                                     cb_blocking, extra_lock_flags);
218                 if (rc == -ENOENT) {
219                         /* skip stripe is not exists */
220                         rc = 0;
221                         continue;
222                 }
223
224                 if (rc < 0)
225                         GOTO(cleanup, rc);
226
227                 lockh = (struct lustre_handle *)&it.it_lock_handle;
228                 if (rc > 0 && req == NULL) {
229                         /* slave inode is still valid */
230                         CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
231                                PFID(&fid));
232                         rc = 0;
233                 } else {
234                         /* refresh slave from server */
235                         body = req_capsule_server_get(&req->rq_pill,
236                                                       &RMF_MDT_BODY);
237                         if (body == NULL) {
238                                 if (it.it_lock_mode && lockh) {
239                                         ldlm_lock_decref(lockh,
240                                                  it.it_lock_mode);
241                                         it.it_lock_mode = 0;
242                                 }
243                                 GOTO(cleanup, rc = -ENOENT);
244                         }
245
246                         i_size_write(inode, body->mbo_size);
247                         inode->i_blocks = body->mbo_blocks;
248                         set_nlink(inode, body->mbo_nlink);
249                         inode->i_atime.tv_sec = body->mbo_atime;
250                         inode->i_ctime.tv_sec = body->mbo_ctime;
251                         inode->i_mtime.tv_sec = body->mbo_mtime;
252                 }
253
254                 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
255                 if (it.it_lock_mode != 0 && lockh != NULL) {
256                         ldlm_lock_decref(lockh, it.it_lock_mode);
257                         it.it_lock_mode = 0;
258                 }
259
260                 valid_stripe_count++;
261         }
262
263 cleanup:
264         if (req != NULL)
265                 ptlrpc_req_finished(req);
266
267         /* if all stripes are invalid, return -ENOENT to notify user */
268         if (!rc && !valid_stripe_count)
269                 rc = -ENOENT;
270
271         OBD_FREE_PTR(op_data);
272         RETURN(rc);
273 }
274
275 /*
276  * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
277  * may be split dir.
278  */
279 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
280                            struct lookup_intent *it,
281                            struct ptlrpc_request **reqp,
282                            ldlm_blocking_callback cb_blocking,
283                            __u64 extra_lock_flags)
284 {
285         struct obd_device *obd = exp->exp_obd;
286         struct lmv_obd *lmv = &obd->u.lmv;
287         struct lmv_tgt_desc *tgt;
288         struct mdt_body *body;
289         __u64 flags = it->it_flags;
290         int rc;
291
292         ENTRY;
293
294         /* do not allow file creation in foreign dir */
295         if ((it->it_op & IT_CREAT) && op_data->op_mea1 != NULL &&
296             op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)
297                 RETURN(-ENODATA);
298
299         if ((it->it_op & IT_CREAT) && !(flags & MDS_OPEN_BY_FID)) {
300                 /* don't allow create under dir with bad hash */
301                 if (lmv_is_dir_bad_hash(op_data->op_mea1))
302                         RETURN(-EBADF);
303
304                 if (lmv_is_dir_migrating(op_data->op_mea1)) {
305                         if (flags & O_EXCL) {
306                                 /*
307                                  * open(O_CREAT | O_EXCL) needs to check
308                                  * existing name, which should be done on both
309                                  * old and new layout, to avoid creating new
310                                  * file under old layout, check old layout on
311                                  * client side.
312                                  */
313                                 tgt = lmv_locate_tgt(lmv, op_data,
314                                                      &op_data->op_fid1);
315                                 if (IS_ERR(tgt))
316                                         RETURN(PTR_ERR(tgt));
317
318                                 rc = md_getattr_name(tgt->ltd_exp, op_data,
319                                                      reqp);
320                                 if (!rc) {
321                                         ptlrpc_req_finished(*reqp);
322                                         *reqp = NULL;
323                                         RETURN(-EEXIST);
324                                 }
325
326                                 if (rc != -ENOENT)
327                                         RETURN(rc);
328
329                                 op_data->op_post_migrate = true;
330                         } else {
331                                 /*
332                                  * open(O_CREAT) will be sent to MDT in old
333                                  * layout first, to avoid creating new file
334                                  * under old layout, clear O_CREAT.
335                                  */
336                                 it->it_flags &= ~O_CREAT;
337                         }
338                 }
339         }
340
341 retry:
342         if (it->it_flags & MDS_OPEN_BY_FID) {
343                 LASSERT(fid_is_sane(&op_data->op_fid2));
344
345                 /* for striped directory, we can't know parent stripe fid
346                  * without name, but we can set it to child fid, and MDT
347                  * will obtain it from linkea in open in such case. */
348                 if (op_data->op_mea1 != NULL)
349                         op_data->op_fid1 = op_data->op_fid2;
350
351                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
352                 if (IS_ERR(tgt))
353                         RETURN(PTR_ERR(tgt));
354
355                 op_data->op_mds = tgt->ltd_idx;
356         } else {
357                 LASSERT(fid_is_sane(&op_data->op_fid1));
358                 LASSERT(it->it_flags & MDS_OPEN_PCC ||
359                         fid_is_zero(&op_data->op_fid2));
360                 LASSERT(op_data->op_name != NULL);
361
362                 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
363                 if (IS_ERR(tgt))
364                         RETURN(PTR_ERR(tgt));
365         }
366
367         /* If it is ready to open the file by FID, do not need
368          * allocate FID at all, otherwise it will confuse MDT */
369         if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID ||
370                                         it->it_flags & MDS_OPEN_PCC)) {
371                 /*
372                  * For lookup(IT_CREATE) cases allocate new fid and setup FLD
373                  * for it.
374                  */
375                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
376                 if (rc != 0)
377                         RETURN(rc);
378         }
379
380         CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
381                " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
382                PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
383
384         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
385                             extra_lock_flags);
386         if (rc != 0)
387                 RETURN(rc);
388         /*
389          * Nothing is found, do not access body->fid1 as it is zero and thus
390          * pointless.
391          */
392         if ((it->it_disposition & DISP_LOOKUP_NEG) &&
393             !(it->it_disposition & DISP_OPEN_CREATE) &&
394             !(it->it_disposition & DISP_OPEN_OPEN)) {
395                 if (!(it->it_flags & MDS_OPEN_BY_FID) &&
396                     lmv_dir_retry_check_update(op_data)) {
397                         ptlrpc_req_finished(*reqp);
398                         it->it_request = NULL;
399                         it->it_disposition = 0;
400                         *reqp = NULL;
401
402                         it->it_flags = flags;
403                         fid_zero(&op_data->op_fid2);
404                         goto retry;
405                 }
406
407                 RETURN(rc);
408         }
409
410         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
411         if (body == NULL)
412                 RETURN(-EPROTO);
413
414         /* Not cross-ref case, just get out of here. */
415         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
416                 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
417                                        cb_blocking, extra_lock_flags,
418                                        op_data->op_file_secctx_name,
419                                        op_data->op_file_secctx_name_size);
420                 if (rc != 0)
421                         RETURN(rc);
422
423                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
424                 if (body == NULL)
425                         RETURN(-EPROTO);
426         }
427
428         RETURN(rc);
429 }
430
431 /*
432  * Handler for: getattr, lookup and revalidate cases.
433  */
434 static int
435 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
436                   struct lookup_intent *it, struct ptlrpc_request **reqp,
437                   ldlm_blocking_callback cb_blocking,
438                   __u64 extra_lock_flags)
439 {
440         struct obd_device *obd = exp->exp_obd;
441         struct lmv_obd *lmv = &obd->u.lmv;
442         struct lmv_tgt_desc *tgt = NULL;
443         struct mdt_body *body;
444         int rc;
445         ENTRY;
446
447         /* foreign dir is not striped */
448         if (op_data->op_mea1 &&
449             op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN) {
450                 /* only allow getattr/lookup for itself */
451                 if (op_data->op_name != NULL)
452                         RETURN(-ENODATA);
453                 RETURN(0);
454         }
455
456 retry:
457         tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
458         if (IS_ERR(tgt))
459                 RETURN(PTR_ERR(tgt));
460
461         if (!fid_is_sane(&op_data->op_fid2))
462                 fid_zero(&op_data->op_fid2);
463
464         CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
465                ", name='%s' -> mds #%u\n",
466                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
467                op_data->op_name ? op_data->op_name : "<NULL>",
468                tgt->ltd_idx);
469
470         op_data->op_bias &= ~MDS_CROSS_REF;
471
472         rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
473                             extra_lock_flags);
474         if (rc < 0)
475                 RETURN(rc);
476
477         if (*reqp == NULL) {
478                 /* If RPC happens, lsm information will be revalidated
479                  * during update_inode process (see ll_update_lsm_md) */
480                 if (op_data->op_mea2 != NULL) {
481                         rc = lmv_revalidate_slaves(exp, op_data->op_mea2,
482                                                    cb_blocking,
483                                                    extra_lock_flags);
484                         if (rc != 0)
485                                 RETURN(rc);
486                 }
487                 RETURN(rc);
488         } else if (it_disposition(it, DISP_LOOKUP_NEG) &&
489                    lmv_dir_retry_check_update(op_data)) {
490                 ptlrpc_req_finished(*reqp);
491                 it->it_request = NULL;
492                 it->it_disposition = 0;
493                 *reqp = NULL;
494
495                 goto retry;
496         }
497
498         if (!it_has_reply_body(it))
499                 RETURN(0);
500
501         /*
502          * MDS has returned success. Probably name has been resolved in
503          * remote inode. Let's check this.
504          */
505         body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
506         if (body == NULL)
507                 RETURN(-EPROTO);
508
509         /* Not cross-ref case, just get out of here. */
510         if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
511                 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
512                                        extra_lock_flags,
513                                        op_data->op_file_secctx_name,
514                                        op_data->op_file_secctx_name_size);
515                 if (rc != 0)
516                         RETURN(rc);
517                 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
518                 if (body == NULL)
519                         RETURN(-EPROTO);
520         }
521
522         RETURN(rc);
523 }
524
525 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
526                     struct lookup_intent *it, struct ptlrpc_request **reqp,
527                     ldlm_blocking_callback cb_blocking,
528                     __u64 extra_lock_flags)
529 {
530         int rc;
531         ENTRY;
532
533         LASSERT(it != NULL);
534         LASSERT(fid_is_sane(&op_data->op_fid1));
535
536         CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
537                 LL_IT2STR(it), PFID(&op_data->op_fid2),
538                 (int)op_data->op_namelen, op_data->op_name,
539                 PFID(&op_data->op_fid1));
540
541         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT | IT_GETXATTR))
542                 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
543                                        extra_lock_flags);
544         else if (it->it_op & IT_OPEN)
545                 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
546                                      extra_lock_flags);
547         else
548                 LBUG();
549
550         if (rc < 0) {
551                 struct lustre_handle lock_handle;
552
553                 if (it->it_lock_mode != 0) {
554                         lock_handle.cookie = it->it_lock_handle;
555                         ldlm_lock_decref_and_cancel(&lock_handle,
556                                                     it->it_lock_mode);
557                 }
558
559                 it->it_lock_handle = 0;
560                 it->it_lock_mode = 0;
561
562                 if (it->it_remote_lock_mode != 0) {
563                         lock_handle.cookie = it->it_remote_lock_handle;
564                         ldlm_lock_decref_and_cancel(&lock_handle,
565                                                     it->it_remote_lock_mode);
566                 }
567
568                 it->it_remote_lock_handle = 0;
569                 it->it_remote_lock_mode = 0;
570         }
571
572         RETURN(rc);
573 }