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