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/
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/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>
44 #include <obd_support.h>
45 #include <lustre/lustre_idl.h>
46 #include <lustre_lib.h>
47 #include <lustre_net.h>
48 #include <lustre_dlm.h>
49 #include <lustre_mdc.h>
50 #include <obd_class.h>
51 #include <lprocfs_status.h>
52 #include "lmv_internal.h"
54 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
55 const struct lu_fid *parent_fid,
56 struct ptlrpc_request **reqp,
57 ldlm_blocking_callback cb_blocking,
58 __u64 extra_lock_flags)
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;
71 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
75 LASSERT((body->mbo_valid & OBD_MD_MDS));
78 * Unfortunately, we have to lie to MDC/MDS to retrieve
79 * attributes llite needs and provideproper locking.
81 if (it->it_op & IT_LOOKUP)
82 it->it_op = IT_GETATTR;
85 * We got LOOKUP lock, but we really need attrs.
87 pmode = it->it_lock_mode;
89 plock.cookie = it->it_lock_handle;
91 it->it_request = NULL;
94 LASSERT(fid_is_sane(&body->mbo_fid1));
96 tgt = lmv_find_target(lmv, &body->mbo_fid1);
98 GOTO(out, rc = PTR_ERR(tgt));
100 OBD_ALLOC_PTR(op_data);
102 GOTO(out, rc = -ENOMEM);
104 op_data->op_fid1 = body->mbo_fid1;
105 /* Sent the parent FID to the remote MDT */
106 if (parent_fid != NULL) {
107 /* The parent fid is only for remote open to
108 * check whether the open is from OBF,
109 * see mdt_cross_open */
110 LASSERT(it->it_op & IT_OPEN);
111 op_data->op_fid2 = *parent_fid;
114 op_data->op_bias = MDS_CROSS_REF;
115 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%u\n",
116 PFID(&body->mbo_fid1), tgt->ltd_idx);
118 rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
121 GOTO(out_free_op_data, rc);
124 * LLite needs LOOKUP lock to track dentry revocation in order to
125 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
126 * and put LOOKUP in request.
128 if (it->it_lock_mode != 0) {
129 it->it_remote_lock_handle =
131 it->it_remote_lock_mode = it->it_lock_mode;
135 it->it_lock_handle = plock.cookie;
136 it->it_lock_mode = pmode;
141 OBD_FREE_PTR(op_data);
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;
167 * revalidate slaves has some problems, temporarily return,
168 * we may not need that
170 OBD_ALLOC_PTR(op_data);
175 * Loop over the stripe information, check validity and update them
176 * from MDS if needed.
178 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
180 struct lookup_intent it = { .it_op = IT_GETATTR };
181 struct lustre_handle *lockh = NULL;
182 struct lmv_tgt_desc *tgt = NULL;
185 fid = lsm->lsm_md_oinfo[i].lmo_fid;
186 inode = lsm->lsm_md_oinfo[i].lmo_root;
189 * Prepare op_data for revalidating. Note that @fid2 shluld be
190 * defined otherwise it will go to server and take new lock
191 * which is not needed here.
193 memset(op_data, 0, sizeof(*op_data));
194 op_data->op_fid1 = fid;
195 op_data->op_fid2 = fid;
197 tgt = lmv_locate_mds(lmv, op_data, &fid);
199 GOTO(cleanup, rc = PTR_ERR(tgt));
201 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%u\n",
202 PFID(&fid), tgt->ltd_idx);
205 ptlrpc_req_finished(req);
209 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
210 cb_blocking, extra_lock_flags);
214 lockh = (struct lustre_handle *)&it.it_lock_handle;
215 if (rc > 0 && req == NULL) {
216 /* slave inode is still valid */
217 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
221 /* refresh slave from server */
222 body = req_capsule_server_get(&req->rq_pill,
225 if (it.it_lock_mode && lockh) {
226 ldlm_lock_decref(lockh,
230 GOTO(cleanup, rc = -ENOENT);
233 i_size_write(inode, body->mbo_size);
234 inode->i_blocks = body->mbo_blocks;
235 set_nlink(inode, body->mbo_nlink);
236 LTIME_S(inode->i_atime) = body->mbo_atime;
237 LTIME_S(inode->i_ctime) = body->mbo_ctime;
238 LTIME_S(inode->i_mtime) = body->mbo_mtime;
241 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
242 if (it.it_lock_mode != 0 && lockh != NULL) {
243 ldlm_lock_decref(lockh, it.it_lock_mode);
250 ptlrpc_req_finished(req);
252 OBD_FREE_PTR(op_data);
258 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
261 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
262 struct lookup_intent *it,
263 struct ptlrpc_request **reqp,
264 ldlm_blocking_callback cb_blocking,
265 __u64 extra_lock_flags)
267 struct obd_device *obd = exp->exp_obd;
268 struct lmv_obd *lmv = &obd->u.lmv;
269 struct lmv_tgt_desc *tgt;
270 struct mdt_body *body;
274 if (it->it_flags & MDS_OPEN_BY_FID) {
275 LASSERT(fid_is_sane(&op_data->op_fid2));
277 /* for striped directory, we can't know parent stripe fid
278 * without name, but we can set it to child fid, and MDT
279 * will obtain it from linkea in open in such case. */
280 if (op_data->op_mea1 != NULL)
281 op_data->op_fid1 = op_data->op_fid2;
283 tgt = lmv_find_target(lmv, &op_data->op_fid2);
285 RETURN(PTR_ERR(tgt));
287 op_data->op_mds = tgt->ltd_idx;
289 LASSERT(fid_is_sane(&op_data->op_fid1));
290 LASSERT(fid_is_zero(&op_data->op_fid2));
291 LASSERT(op_data->op_name != NULL);
293 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
295 RETURN(PTR_ERR(tgt));
298 /* If it is ready to open the file by FID, do not need
299 * allocate FID at all, otherwise it will confuse MDT */
300 if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
302 * For lookup(IT_CREATE) cases allocate new fid and setup FLD
305 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
310 CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
311 " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
312 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
314 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
319 * Nothing is found, do not access body->fid1 as it is zero and thus
322 if ((it->it_disposition & DISP_LOOKUP_NEG) &&
323 !(it->it_disposition & DISP_OPEN_CREATE) &&
324 !(it->it_disposition & DISP_OPEN_OPEN))
327 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
331 /* Not cross-ref case, just get out of here. */
332 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
333 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
334 cb_blocking, extra_lock_flags);
338 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
347 * Handler for: getattr, lookup and revalidate cases.
350 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
351 struct lookup_intent *it, struct ptlrpc_request **reqp,
352 ldlm_blocking_callback cb_blocking,
353 __u64 extra_lock_flags)
355 struct obd_device *obd = exp->exp_obd;
356 struct lmv_obd *lmv = &obd->u.lmv;
357 struct lmv_tgt_desc *tgt = NULL;
358 struct mdt_body *body;
359 struct lmv_stripe_md *lsm = op_data->op_mea1;
363 /* If it returns ERR_PTR(-EBADFD) then it is an unknown hash type
364 * it will try all stripes to locate the object */
365 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
366 if (IS_ERR(tgt) && (PTR_ERR(tgt) != -EBADFD))
367 RETURN(PTR_ERR(tgt));
369 /* Both migrating dir and unknown hash dir need to try
370 * all of sub-stripes */
371 if (lsm != NULL && !lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
372 struct lmv_oinfo *oinfo;
374 oinfo = &lsm->lsm_md_oinfo[0];
376 op_data->op_fid1 = oinfo->lmo_fid;
377 op_data->op_mds = oinfo->lmo_mds;
378 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
380 RETURN(PTR_ERR(tgt));
383 if (!fid_is_sane(&op_data->op_fid2))
384 fid_zero(&op_data->op_fid2);
386 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
387 ", name='%s' -> mds #%u lsm=%p lsm_magic=%x\n",
388 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
389 op_data->op_name ? op_data->op_name : "<NULL>",
390 tgt->ltd_idx, lsm, lsm == NULL ? -1 : lsm->lsm_md_magic);
392 op_data->op_bias &= ~MDS_CROSS_REF;
394 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
400 /* If RPC happens, lsm information will be revalidated
401 * during update_inode process (see ll_update_lsm_md) */
402 if (op_data->op_mea2 != NULL) {
403 rc = lmv_revalidate_slaves(exp, op_data->op_mea2,
410 } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm != NULL &&
411 lmv_need_try_all_stripes(lsm)) {
412 /* For migrating and unknown hash type directory, it will
413 * try to target the entry on other stripes */
416 for (stripe_index = 1;
417 stripe_index < lsm->lsm_md_stripe_count &&
418 it_disposition(it, DISP_LOOKUP_NEG); stripe_index++) {
419 struct lmv_oinfo *oinfo;
421 /* release the previous request */
422 ptlrpc_req_finished(*reqp);
423 it->it_request = NULL;
426 oinfo = &lsm->lsm_md_oinfo[stripe_index];
427 tgt = lmv_find_target(lmv, &oinfo->lmo_fid);
429 RETURN(PTR_ERR(tgt));
431 CDEBUG(D_INODE, "Try other stripes " DFID"\n",
432 PFID(&oinfo->lmo_fid));
434 op_data->op_fid1 = oinfo->lmo_fid;
435 it->it_disposition &= ~DISP_ENQ_COMPLETE;
436 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
437 cb_blocking, extra_lock_flags);
443 if (!it_has_reply_body(it))
447 * MDS has returned success. Probably name has been resolved in
448 * remote inode. Let's check this.
450 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
454 /* Not cross-ref case, just get out of here. */
455 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
456 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
460 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
468 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
469 struct lookup_intent *it, struct ptlrpc_request **reqp,
470 ldlm_blocking_callback cb_blocking,
471 __u64 extra_lock_flags)
477 LASSERT(fid_is_sane(&op_data->op_fid1));
479 CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
480 LL_IT2STR(it), PFID(&op_data->op_fid2),
481 (int)op_data->op_namelen, op_data->op_name,
482 PFID(&op_data->op_fid1));
484 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT | IT_GETXATTR))
485 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
487 else if (it->it_op & IT_OPEN)
488 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
494 struct lustre_handle lock_handle;
496 if (it->it_lock_mode != 0) {
497 lock_handle.cookie = it->it_lock_handle;
498 ldlm_lock_decref_and_cancel(&lock_handle,
502 it->it_lock_handle = 0;
503 it->it_lock_mode = 0;
505 if (it->it_remote_lock_mode != 0) {
506 lock_handle.cookie = it->it_remote_lock_handle;
507 ldlm_lock_decref_and_cancel(&lock_handle,
508 it->it_remote_lock_mode);
511 it->it_remote_lock_handle = 0;
512 it->it_remote_lock_mode = 0;