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