4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_LMV
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/pagemap.h>
38 #include <linux/math64.h>
39 #include <linux/seq_file.h>
40 #include <linux/namei.h>
41 #include <lustre_intent.h>
43 #include <obd_support.h>
44 #include <lustre_lib.h>
45 #include <lustre_net.h>
46 #include <lustre_dlm.h>
47 #include <lustre_mdc.h>
48 #include <obd_class.h>
49 #include <lprocfs_status.h>
50 #include "lmv_internal.h"
52 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
53 const struct lu_fid *parent_fid,
54 struct ptlrpc_request **reqp,
55 ldlm_blocking_callback cb_blocking,
56 __u64 extra_lock_flags,
57 const char *secctx_name, __u32 secctx_name_size)
59 struct obd_device *obd = exp->exp_obd;
60 struct lmv_obd *lmv = &obd->u.lmv;
61 struct ptlrpc_request *req = NULL;
62 struct lustre_handle plock;
63 struct md_op_data *op_data;
64 struct lmv_tgt_desc *tgt;
65 struct mdt_body *body;
70 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
74 LASSERT((body->mbo_valid & OBD_MD_MDS));
77 * We got LOOKUP lock, but we really need attrs.
79 pmode = it->it_lock_mode;
81 plock.cookie = it->it_lock_handle;
83 it->it_request = NULL;
86 LASSERT(fid_is_sane(&body->mbo_fid1));
88 tgt = lmv_fid2tgt(lmv, &body->mbo_fid1);
90 GOTO(out, rc = PTR_ERR(tgt));
92 OBD_ALLOC_PTR(op_data);
94 GOTO(out, rc = -ENOMEM);
96 op_data->op_fid1 = body->mbo_fid1;
97 /* Sent the parent FID to the remote MDT */
98 if (parent_fid != NULL) {
99 /* The parent fid is only for remote open to
100 * check whether the open is from OBF,
101 * see mdt_cross_open */
102 LASSERT(it->it_op & IT_OPEN);
103 op_data->op_fid2 = *parent_fid;
106 op_data->op_bias = MDS_CROSS_REF;
107 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%u\n",
108 PFID(&body->mbo_fid1), tgt->ltd_index);
110 /* ask for security context upon intent */
111 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_OPEN) &&
112 secctx_name_size != 0 && secctx_name != NULL) {
113 op_data->op_file_secctx_name = secctx_name;
114 op_data->op_file_secctx_name_size = secctx_name_size;
115 CDEBUG(D_SEC, "'%.*s' is security xattr to fetch for "
117 secctx_name_size, secctx_name, PFID(&body->mbo_fid1));
120 rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
123 GOTO(out_free_op_data, rc);
126 * LLite needs LOOKUP lock to track dentry revocation in order to
127 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
128 * and put LOOKUP in request.
130 if (it->it_lock_mode != 0) {
131 it->it_remote_lock_handle =
133 it->it_remote_lock_mode = it->it_lock_mode;
137 it->it_lock_handle = plock.cookie;
138 it->it_lock_mode = pmode;
143 OBD_FREE_PTR(op_data);
146 ldlm_lock_decref(&plock, pmode);
148 ptlrpc_req_finished(*reqp);
153 int lmv_revalidate_slaves(struct obd_export *exp,
154 const struct lmv_stripe_md *lsm,
155 ldlm_blocking_callback cb_blocking,
156 int extra_lock_flags)
158 struct obd_device *obd = exp->exp_obd;
159 struct lmv_obd *lmv = &obd->u.lmv;
160 struct ptlrpc_request *req = NULL;
161 struct mdt_body *body;
162 struct md_op_data *op_data;
164 int valid_stripe_count = 0;
170 * revalidate slaves has some problems, temporarily return,
171 * we may not need that
173 OBD_ALLOC_PTR(op_data);
178 * Loop over the stripe information, check validity and update them
179 * from MDS if needed.
181 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
183 struct lookup_intent it = { .it_op = IT_GETATTR };
184 struct lustre_handle *lockh = NULL;
185 struct lmv_tgt_desc *tgt = NULL;
188 fid = lsm->lsm_md_oinfo[i].lmo_fid;
189 inode = lsm->lsm_md_oinfo[i].lmo_root;
195 * Prepare op_data for revalidating. Note that @fid2 shluld be
196 * defined otherwise it will go to server and take new lock
197 * which is not needed here.
199 memset(op_data, 0, sizeof(*op_data));
200 op_data->op_fid1 = fid;
201 op_data->op_fid2 = fid;
202 /* shard revalidate only needs to fetch attributes and UPDATE
203 * lock, which is similar to the bottom half of remote object
204 * getattr, set this flag so that MDT skips checking whether
205 * it's remote object.
207 op_data->op_bias = MDS_CROSS_REF;
209 tgt = lmv_tgt(lmv, lsm->lsm_md_oinfo[i].lmo_mds);
211 GOTO(cleanup, rc = -ENODEV);
213 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%u\n",
214 PFID(&fid), tgt->ltd_index);
217 ptlrpc_req_finished(req);
221 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
222 cb_blocking, extra_lock_flags);
224 /* skip stripe is not exists */
232 lockh = (struct lustre_handle *)&it.it_lock_handle;
233 if (rc > 0 && req == NULL) {
234 /* slave inode is still valid */
235 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
239 /* refresh slave from server */
240 body = req_capsule_server_get(&req->rq_pill,
243 if (it.it_lock_mode && lockh) {
244 ldlm_lock_decref(lockh,
248 GOTO(cleanup, rc = -ENOENT);
251 i_size_write(inode, body->mbo_size);
252 inode->i_blocks = body->mbo_blocks;
253 set_nlink(inode, body->mbo_nlink);
254 inode->i_atime.tv_sec = body->mbo_atime;
255 inode->i_ctime.tv_sec = body->mbo_ctime;
256 inode->i_mtime.tv_sec = body->mbo_mtime;
259 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
260 if (it.it_lock_mode != 0 && lockh != NULL) {
261 ldlm_lock_decref(lockh, it.it_lock_mode);
265 valid_stripe_count++;
270 ptlrpc_req_finished(req);
272 /* if all stripes are invalid, return -ENOENT to notify user */
273 if (!rc && !valid_stripe_count)
276 OBD_FREE_PTR(op_data);
281 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
284 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
285 struct lookup_intent *it,
286 struct ptlrpc_request **reqp,
287 ldlm_blocking_callback cb_blocking,
288 __u64 extra_lock_flags)
290 struct obd_device *obd = exp->exp_obd;
291 struct lmv_obd *lmv = &obd->u.lmv;
292 struct lmv_tgt_desc *tgt;
293 struct mdt_body *body;
294 __u64 flags = it->it_flags;
299 /* do not allow file creation in foreign dir */
300 if ((it->it_op & IT_CREAT) && lmv_dir_foreign(op_data->op_mea1))
303 if ((it->it_op & IT_CREAT) && !(flags & MDS_OPEN_BY_FID)) {
304 /* don't allow create under dir with bad hash */
305 if (lmv_dir_bad_hash(op_data->op_mea1))
308 if (lmv_dir_layout_changing(op_data->op_mea1)) {
309 if (flags & O_EXCL) {
311 * open(O_CREAT | O_EXCL) needs to check
312 * existing name, which should be done on both
313 * old and new layout, check old layout on
316 rc = lmv_old_layout_lookup(lmv, op_data);
320 op_data->op_new_layout = true;
323 * open(O_CREAT) will be sent to MDT in old
324 * layout first, to avoid creating new file
325 * under old layout, clear O_CREAT.
327 it->it_flags &= ~O_CREAT;
333 if (it->it_flags & MDS_OPEN_BY_FID) {
334 LASSERT(fid_is_sane(&op_data->op_fid2));
336 /* for striped directory, we can't know parent stripe fid
337 * without name, but we can set it to child fid, and MDT
338 * will obtain it from linkea in open in such case. */
339 if (lmv_dir_striped(op_data->op_mea1))
340 op_data->op_fid1 = op_data->op_fid2;
342 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
344 RETURN(PTR_ERR(tgt));
346 op_data->op_mds = tgt->ltd_index;
348 LASSERT(fid_is_sane(&op_data->op_fid1));
349 LASSERT(fid_is_zero(&op_data->op_fid2));
350 LASSERT(op_data->op_name != NULL);
352 tgt = lmv_locate_tgt(lmv, op_data);
354 RETURN(PTR_ERR(tgt));
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)) {
361 * For lookup(IT_CREATE) cases allocate new fid and setup FLD
364 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
369 CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
370 " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
371 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_index);
373 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
378 * Nothing is found, do not access body->fid1 as it is zero and thus
381 if ((it->it_disposition & DISP_LOOKUP_NEG) &&
382 !(it->it_disposition & DISP_OPEN_CREATE) &&
383 !(it->it_disposition & DISP_OPEN_OPEN)) {
384 if (!(it->it_flags & MDS_OPEN_BY_FID) &&
385 lmv_dir_retry_check_update(op_data)) {
386 ptlrpc_req_finished(*reqp);
387 it->it_request = NULL;
388 it->it_disposition = 0;
391 it->it_flags = flags;
392 fid_zero(&op_data->op_fid2);
399 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
403 /* Not cross-ref case, just get out of here. */
404 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
405 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
406 cb_blocking, extra_lock_flags,
407 op_data->op_file_secctx_name,
408 op_data->op_file_secctx_name_size);
412 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
421 * Handler for: getattr, lookup and revalidate cases.
424 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
425 struct lookup_intent *it, struct ptlrpc_request **reqp,
426 ldlm_blocking_callback cb_blocking,
427 __u64 extra_lock_flags)
429 struct obd_device *obd = exp->exp_obd;
430 struct lmv_obd *lmv = &obd->u.lmv;
431 struct lmv_tgt_desc *tgt = NULL;
432 struct mdt_body *body;
436 /* foreign dir is not striped */
437 if (lmv_dir_foreign(op_data->op_mea1)) {
438 /* only allow getattr/lookup for itself */
439 if (op_data->op_name != NULL)
445 if (op_data->op_flags & MF_GETATTR_BY_FID) {
446 /* getattr by FID, replace fid1 with stripe FID,
447 * NB, don't replace if name is "/", because it may be a subtree
448 * mount, and if it's a striped directory, fid1 will be replaced
449 * to stripe FID by hash, while fid2 is master object FID, which
450 * will be treated as a remote object if the two FIDs are
451 * located on different MDTs, and LOOKUP lock can't be fetched.
453 LASSERT(op_data->op_name);
454 if (op_data->op_namelen != 1 ||
455 strncmp(op_data->op_name, "/", 1) != 0) {
456 tgt = lmv_locate_tgt(lmv, op_data);
458 RETURN(PTR_ERR(tgt));
461 /* name is used to locate stripe target, clear it here
462 * to avoid packing name in request, so that MDS knows
463 * it's getattr by FID.
465 op_data->op_name = NULL;
466 op_data->op_namelen = 0;
468 /* getattr request is sent to MDT where fid2 inode is */
469 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
470 } else if (op_data->op_name) {
471 /* getattr by name */
472 tgt = lmv_locate_tgt(lmv, op_data);
473 if (!fid_is_sane(&op_data->op_fid2))
474 fid_zero(&op_data->op_fid2);
476 /* old way to getattr by FID, parent FID not packed */
477 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
480 RETURN(PTR_ERR(tgt));
482 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
483 ", name='%s' -> mds #%u\n",
484 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
485 op_data->op_name ? op_data->op_name : "<NULL>",
488 op_data->op_bias &= ~MDS_CROSS_REF;
490 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
496 /* If RPC happens, lsm information will be revalidated
497 * during update_inode process (see ll_update_lsm_md) */
498 if (lmv_dir_striped(op_data->op_mea2)) {
499 rc = lmv_revalidate_slaves(exp, op_data->op_mea2,
506 } else if (it_disposition(it, DISP_LOOKUP_NEG) &&
507 lmv_dir_retry_check_update(op_data)) {
508 ptlrpc_req_finished(*reqp);
509 it->it_request = NULL;
510 it->it_disposition = 0;
516 if (!it_has_reply_body(it))
520 * MDS has returned success. Probably name has been resolved in
521 * remote inode. Let's check this.
523 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
527 /* Not cross-ref case, just get out of here. */
528 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
529 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
531 op_data->op_file_secctx_name,
532 op_data->op_file_secctx_name_size);
535 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
543 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
544 struct lookup_intent *it, struct ptlrpc_request **reqp,
545 ldlm_blocking_callback cb_blocking,
546 __u64 extra_lock_flags)
552 LASSERT(fid_is_sane(&op_data->op_fid1));
554 CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
555 LL_IT2STR(it), PFID(&op_data->op_fid2),
556 (int)op_data->op_namelen, op_data->op_name,
557 PFID(&op_data->op_fid1));
559 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT | IT_GETXATTR))
560 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
562 else if (it->it_op & IT_OPEN)
563 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
569 struct lustre_handle lock_handle;
571 if (it->it_lock_mode != 0) {
572 lock_handle.cookie = it->it_lock_handle;
573 ldlm_lock_decref_and_cancel(&lock_handle,
577 it->it_lock_handle = 0;
578 it->it_lock_mode = 0;
580 if (it->it_remote_lock_mode != 0) {
581 lock_handle.cookie = it->it_remote_lock_handle;
582 ldlm_lock_decref_and_cancel(&lock_handle,
583 it->it_remote_lock_mode);
586 it->it_remote_lock_handle = 0;
587 it->it_remote_lock_mode = 0;