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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LMV
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/pagemap.h>
37 #include <asm/div64.h>
38 #include <linux/seq_file.h>
39 #include <linux/namei.h>
40 #include "../include/lustre_intent.h"
41 #include "../include/obd_support.h"
42 #include "../include/lustre/lustre_idl.h"
43 #include "../include/lustre_lib.h"
44 #include "../include/lustre_net.h"
45 #include "../include/lustre_dlm.h"
46 #include "../include/lustre_mdc.h"
47 #include "../include/obd_class.h"
48 #include "../include/lprocfs_status.h"
49 #include "lmv_internal.h"
51 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
52 const struct lu_fid *parent_fid,
53 struct ptlrpc_request **reqp,
54 ldlm_blocking_callback cb_blocking,
55 __u64 extra_lock_flags)
57 struct obd_device *obd = exp->exp_obd;
58 struct lmv_obd *lmv = &obd->u.lmv;
59 struct ptlrpc_request *req = NULL;
60 struct lustre_handle plock;
61 struct md_op_data *op_data;
62 struct lmv_tgt_desc *tgt;
63 struct mdt_body *body;
67 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
71 LASSERT((body->mbo_valid & OBD_MD_MDS));
74 * Unfortunately, we have to lie to MDC/MDS to retrieve
75 * attributes llite needs and provideproper locking.
77 if (it->it_op & IT_LOOKUP)
78 it->it_op = IT_GETATTR;
81 * We got LOOKUP lock, but we really need attrs.
83 pmode = it->it_lock_mode;
85 plock.cookie = it->it_lock_handle;
87 it->it_request = NULL;
90 LASSERT(fid_is_sane(&body->mbo_fid1));
92 tgt = lmv_find_target(lmv, &body->mbo_fid1);
98 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
104 op_data->op_fid1 = body->mbo_fid1;
105 /* Sent the parent FID to the remote MDT */
107 /* The parent fid is only for remote open to
108 * check whether the open is from OBF,
111 LASSERT(it->it_op & IT_OPEN);
112 op_data->op_fid2 = *parent_fid;
115 op_data->op_bias = MDS_CROSS_REF;
116 CDEBUG(D_INODE, "REMOTE_INTENT with fid=" DFID " -> mds #%u\n",
117 PFID(&body->mbo_fid1), tgt->ltd_idx);
119 rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
122 goto out_free_op_data;
125 * LLite needs LOOKUP lock to track dentry revocation in order to
126 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
127 * and put LOOKUP in request.
129 if (it->it_lock_mode != 0) {
130 it->it_remote_lock_handle =
132 it->it_remote_lock_mode = it->it_lock_mode;
136 it->it_lock_handle = plock.cookie;
137 it->it_lock_mode = pmode;
144 ldlm_lock_decref(&plock, pmode);
146 ptlrpc_req_finished(*reqp);
151 int lmv_revalidate_slaves(struct obd_export *exp,
152 const struct lmv_stripe_md *lsm,
153 ldlm_blocking_callback cb_blocking,
154 int extra_lock_flags)
156 struct obd_device *obd = exp->exp_obd;
157 struct lmv_obd *lmv = &obd->u.lmv;
158 struct ptlrpc_request *req = NULL;
159 struct mdt_body *body;
160 struct md_op_data *op_data;
164 * revalidate slaves has some problems, temporarily return,
165 * we may not need that
167 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
172 * Loop over the stripe information, check validity and update them
173 * from MDS if needed.
175 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
176 struct lookup_intent it = { .it_op = IT_GETATTR };
177 struct lustre_handle *lockh = NULL;
178 struct lmv_tgt_desc *tgt = NULL;
182 fid = lsm->lsm_md_oinfo[i].lmo_fid;
183 inode = lsm->lsm_md_oinfo[i].lmo_root;
186 * Prepare op_data for revalidating. Note that @fid2 shluld be
187 * defined otherwise it will go to server and take new lock
188 * which is not needed here.
190 memset(op_data, 0, sizeof(*op_data));
191 op_data->op_fid1 = fid;
192 op_data->op_fid2 = fid;
194 tgt = lmv_locate_mds(lmv, op_data, &fid);
200 CDEBUG(D_INODE, "Revalidate slave " DFID " -> mds #%u\n",
201 PFID(&fid), tgt->ltd_idx);
204 ptlrpc_req_finished(req);
208 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
209 cb_blocking, extra_lock_flags);
213 lockh = (struct lustre_handle *)&it.it_lock_handle;
214 if (rc > 0 && !req) {
215 /* slave inode is still valid */
216 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
220 /* refresh slave from server */
221 body = req_capsule_server_get(&req->rq_pill,
225 if (unlikely(body->mbo_nlink < 2)) {
226 CERROR("%s: nlink %d < 2 corrupt stripe %d "DFID":" DFID"\n",
227 obd->obd_name, body->mbo_nlink, i,
228 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
229 PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
231 if (it.it_lock_mode && lockh) {
232 ldlm_lock_decref(lockh, it.it_lock_mode);
240 i_size_write(inode, body->mbo_size);
241 inode->i_blocks = body->mbo_blocks;
242 set_nlink(inode, body->mbo_nlink);
243 LTIME_S(inode->i_atime) = body->mbo_atime;
244 LTIME_S(inode->i_ctime) = body->mbo_ctime;
245 LTIME_S(inode->i_mtime) = body->mbo_mtime;
248 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
250 if (it.it_lock_mode && lockh) {
251 ldlm_lock_decref(lockh, it.it_lock_mode);
258 ptlrpc_req_finished(req);
265 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
268 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
269 struct lookup_intent *it,
270 struct ptlrpc_request **reqp,
271 ldlm_blocking_callback cb_blocking,
272 __u64 extra_lock_flags)
274 struct obd_device *obd = exp->exp_obd;
275 struct lmv_obd *lmv = &obd->u.lmv;
276 struct lmv_tgt_desc *tgt;
277 struct mdt_body *body;
280 if (it->it_flags & MDS_OPEN_BY_FID) {
281 LASSERT(fid_is_sane(&op_data->op_fid2));
284 * for striped directory, we can't know parent stripe fid
285 * without name, but we can set it to child fid, and MDT
286 * will obtain it from linkea in open in such case.
288 if (op_data->op_mea1)
289 op_data->op_fid1 = op_data->op_fid2;
291 tgt = lmv_find_target(lmv, &op_data->op_fid2);
295 op_data->op_mds = tgt->ltd_idx;
297 LASSERT(fid_is_sane(&op_data->op_fid1));
298 LASSERT(fid_is_zero(&op_data->op_fid2));
299 LASSERT(op_data->op_name);
301 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
306 /* If it is ready to open the file by FID, do not need
307 * allocate FID at all, otherwise it will confuse MDT
309 if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
311 * For lookup(IT_CREATE) cases allocate new fid and setup FLD
314 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
319 CDEBUG(D_INODE, "OPEN_INTENT with fid1=" DFID ", fid2=" DFID ", name='%s' -> mds #%u\n",
320 PFID(&op_data->op_fid1),
321 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
323 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
328 * Nothing is found, do not access body->mbo_fid1 as it is zero and thus
331 if ((it->it_disposition & DISP_LOOKUP_NEG) &&
332 !(it->it_disposition & DISP_OPEN_CREATE) &&
333 !(it->it_disposition & DISP_OPEN_OPEN))
336 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
340 /* Not cross-ref case, just get out of here. */
341 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
342 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
343 cb_blocking, extra_lock_flags);
347 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
356 * Handler for: getattr, lookup and revalidate cases.
358 static int lmv_intent_lookup(struct obd_export *exp,
359 struct md_op_data *op_data,
360 struct lookup_intent *it,
361 struct ptlrpc_request **reqp,
362 ldlm_blocking_callback cb_blocking,
363 __u64 extra_lock_flags)
365 struct lmv_stripe_md *lsm = op_data->op_mea1;
366 struct obd_device *obd = exp->exp_obd;
367 struct lmv_obd *lmv = &obd->u.lmv;
368 struct lmv_tgt_desc *tgt = NULL;
369 struct mdt_body *body;
373 * If it returns ERR_PTR(-EBADFD) then it is an unknown hash type
374 * it will try all stripes to locate the object
376 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
377 if (IS_ERR(tgt) && (PTR_ERR(tgt) != -EBADFD))
381 * Both migrating dir and unknown hash dir need to try
384 if (lsm && !lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
385 struct lmv_oinfo *oinfo = &lsm->lsm_md_oinfo[0];
387 op_data->op_fid1 = oinfo->lmo_fid;
388 op_data->op_mds = oinfo->lmo_mds;
389 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
394 if (!fid_is_sane(&op_data->op_fid2))
395 fid_zero(&op_data->op_fid2);
397 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1=" DFID ", fid2=" DFID ", name='%s' -> mds #%u lsm=%p lsm_magic=%x\n",
398 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
399 op_data->op_name ? op_data->op_name : "<NULL>",
400 tgt->ltd_idx, lsm, !lsm ? -1 : lsm->lsm_md_magic);
402 op_data->op_bias &= ~MDS_CROSS_REF;
404 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
411 * If RPC happens, lsm information will be revalidated
412 * during update_inode process (see ll_update_lsm_md)
414 if (op_data->op_mea2) {
415 rc = lmv_revalidate_slaves(exp, op_data->op_mea2,
422 } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm &&
423 lmv_need_try_all_stripes(lsm)) {
425 * For migrating and unknown hash type directory, it will
426 * try to target the entry on other stripes
430 for (stripe_index = 1;
431 stripe_index < lsm->lsm_md_stripe_count &&
432 it_disposition(it, DISP_LOOKUP_NEG); stripe_index++) {
433 struct lmv_oinfo *oinfo;
435 /* release the previous request */
436 ptlrpc_req_finished(*reqp);
437 it->it_request = NULL;
440 oinfo = &lsm->lsm_md_oinfo[stripe_index];
441 tgt = lmv_find_target(lmv, &oinfo->lmo_fid);
445 CDEBUG(D_INODE, "Try other stripes " DFID"\n",
446 PFID(&oinfo->lmo_fid));
448 op_data->op_fid1 = oinfo->lmo_fid;
449 it->it_disposition &= ~DISP_ENQ_COMPLETE;
450 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
451 cb_blocking, extra_lock_flags);
458 * MDS has returned success. Probably name has been resolved in
459 * remote inode. Let's check this.
461 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
465 /* Not cross-ref case, just get out of here. */
466 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
467 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
471 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
479 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
480 struct lookup_intent *it, struct ptlrpc_request **reqp,
481 ldlm_blocking_callback cb_blocking,
482 __u64 extra_lock_flags)
484 struct obd_device *obd = exp->exp_obd;
487 LASSERT(fid_is_sane(&op_data->op_fid1));
489 CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%*s' on "DFID"\n",
490 LL_IT2STR(it), PFID(&op_data->op_fid2),
491 (int)op_data->op_namelen, op_data->op_name,
492 PFID(&op_data->op_fid1));
494 rc = lmv_check_connect(obd);
498 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
499 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
501 else if (it->it_op & IT_OPEN)
502 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
508 struct lustre_handle lock_handle;
510 if (it->it_lock_mode) {
511 lock_handle.cookie = it->it_lock_handle;
512 ldlm_lock_decref(&lock_handle, it->it_lock_mode);
515 it->it_lock_handle = 0;
516 it->it_lock_mode = 0;
518 if (it->it_remote_lock_mode) {
519 lock_handle.cookie = it->it_remote_lock_handle;
520 ldlm_lock_decref(&lock_handle,
521 it->it_remote_lock_mode);
524 it->it_remote_lock_handle = 0;
525 it->it_remote_lock_mode = 0;