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_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"
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)
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 * Unfortunately, we have to lie to MDC/MDS to retrieve
78 * attributes llite needs and provideproper locking.
80 if (it->it_op & IT_LOOKUP)
81 it->it_op = IT_GETATTR;
84 * We got LOOKUP lock, but we really need attrs.
86 pmode = it->it_lock_mode;
88 plock.cookie = it->it_lock_handle;
90 it->it_request = NULL;
93 LASSERT(fid_is_sane(&body->mbo_fid1));
95 tgt = lmv_find_target(lmv, &body->mbo_fid1);
97 GOTO(out, rc = PTR_ERR(tgt));
99 OBD_ALLOC_PTR(op_data);
101 GOTO(out, rc = -ENOMEM);
103 op_data->op_fid1 = body->mbo_fid1;
104 /* Sent the parent FID to the remote MDT */
105 if (parent_fid != NULL) {
106 /* The parent fid is only for remote open to
107 * check whether the open is from OBF,
108 * see mdt_cross_open */
109 LASSERT(it->it_op & IT_OPEN);
110 op_data->op_fid2 = *parent_fid;
113 op_data->op_bias = MDS_CROSS_REF;
114 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%u\n",
115 PFID(&body->mbo_fid1), tgt->ltd_idx);
117 rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
120 GOTO(out_free_op_data, rc);
123 * LLite needs LOOKUP lock to track dentry revocation in order to
124 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
125 * and put LOOKUP in request.
127 if (it->it_lock_mode != 0) {
128 it->it_remote_lock_handle =
130 it->it_remote_lock_mode = it->it_lock_mode;
134 it->it_lock_handle = plock.cookie;
135 it->it_lock_mode = pmode;
140 OBD_FREE_PTR(op_data);
143 ldlm_lock_decref(&plock, pmode);
145 ptlrpc_req_finished(*reqp);
150 int lmv_revalidate_slaves(struct obd_export *exp,
151 const struct lmv_stripe_md *lsm,
152 ldlm_blocking_callback cb_blocking,
153 int extra_lock_flags)
155 struct obd_device *obd = exp->exp_obd;
156 struct lmv_obd *lmv = &obd->u.lmv;
157 struct ptlrpc_request *req = NULL;
158 struct mdt_body *body;
159 struct md_op_data *op_data;
166 * revalidate slaves has some problems, temporarily return,
167 * we may not need that
169 OBD_ALLOC_PTR(op_data);
174 * Loop over the stripe information, check validity and update them
175 * from MDS if needed.
177 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
179 struct lookup_intent it = { .it_op = IT_GETATTR };
180 struct lustre_handle *lockh = NULL;
181 struct lmv_tgt_desc *tgt = NULL;
184 fid = lsm->lsm_md_oinfo[i].lmo_fid;
185 inode = lsm->lsm_md_oinfo[i].lmo_root;
188 * Prepare op_data for revalidating. Note that @fid2 shluld be
189 * defined otherwise it will go to server and take new lock
190 * which is not needed here.
192 memset(op_data, 0, sizeof(*op_data));
193 op_data->op_fid1 = fid;
194 op_data->op_fid2 = fid;
196 tgt = lmv_locate_mds(lmv, op_data, &fid);
198 GOTO(cleanup, rc = PTR_ERR(tgt));
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 == NULL) {
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,
224 if (it.it_lock_mode && lockh) {
225 ldlm_lock_decref(lockh,
229 GOTO(cleanup, rc = -ENOENT);
232 i_size_write(inode, body->mbo_size);
233 inode->i_blocks = body->mbo_blocks;
234 set_nlink(inode, body->mbo_nlink);
235 LTIME_S(inode->i_atime) = body->mbo_atime;
236 LTIME_S(inode->i_ctime) = body->mbo_ctime;
237 LTIME_S(inode->i_mtime) = body->mbo_mtime;
240 md_set_lock_data(tgt->ltd_exp, lockh, inode, NULL);
241 if (it.it_lock_mode != 0 && lockh != NULL) {
242 ldlm_lock_decref(lockh, it.it_lock_mode);
249 ptlrpc_req_finished(req);
251 OBD_FREE_PTR(op_data);
257 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
260 static int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
261 struct lookup_intent *it,
262 struct ptlrpc_request **reqp,
263 ldlm_blocking_callback cb_blocking,
264 __u64 extra_lock_flags)
266 struct obd_device *obd = exp->exp_obd;
267 struct lmv_obd *lmv = &obd->u.lmv;
268 struct lmv_tgt_desc *tgt;
269 struct mdt_body *body;
273 if (it->it_flags & MDS_OPEN_BY_FID) {
274 LASSERT(fid_is_sane(&op_data->op_fid2));
276 /* for striped directory, we can't know parent stripe fid
277 * without name, but we can set it to child fid, and MDT
278 * will obtain it from linkea in open in such case. */
279 if (op_data->op_mea1 != NULL)
280 op_data->op_fid1 = op_data->op_fid2;
282 tgt = lmv_find_target(lmv, &op_data->op_fid2);
284 RETURN(PTR_ERR(tgt));
286 op_data->op_mds = tgt->ltd_idx;
288 LASSERT(fid_is_sane(&op_data->op_fid1));
289 LASSERT(fid_is_zero(&op_data->op_fid2));
290 LASSERT(op_data->op_name != NULL);
292 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
294 RETURN(PTR_ERR(tgt));
297 /* If it is ready to open the file by FID, do not need
298 * allocate FID at all, otherwise it will confuse MDT */
299 if ((it->it_op & IT_CREAT) && !(it->it_flags & MDS_OPEN_BY_FID)) {
301 * For lookup(IT_CREATE) cases allocate new fid and setup FLD
304 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
309 CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
310 " name='%s' -> mds #%u\n", PFID(&op_data->op_fid1),
311 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
313 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
318 * Nothing is found, do not access body->fid1 as it is zero and thus
321 if ((it->it_disposition & DISP_LOOKUP_NEG) &&
322 !(it->it_disposition & DISP_OPEN_CREATE) &&
323 !(it->it_disposition & DISP_OPEN_OPEN))
326 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
330 /* Not cross-ref case, just get out of here. */
331 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
332 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
333 cb_blocking, extra_lock_flags);
337 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
346 * Handler for: getattr, lookup and revalidate cases.
349 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
350 struct lookup_intent *it, struct ptlrpc_request **reqp,
351 ldlm_blocking_callback cb_blocking,
352 __u64 extra_lock_flags)
354 struct obd_device *obd = exp->exp_obd;
355 struct lmv_obd *lmv = &obd->u.lmv;
356 struct lmv_tgt_desc *tgt = NULL;
357 struct mdt_body *body;
358 struct lmv_stripe_md *lsm = op_data->op_mea1;
362 /* If it returns ERR_PTR(-EBADFD) then it is an unknown hash type
363 * it will try all stripes to locate the object */
364 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
365 if (IS_ERR(tgt) && (PTR_ERR(tgt) != -EBADFD))
366 RETURN(PTR_ERR(tgt));
368 /* Both migrating dir and unknown hash dir need to try
369 * all of sub-stripes */
370 if (lsm != NULL && !lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
371 struct lmv_oinfo *oinfo;
373 oinfo = &lsm->lsm_md_oinfo[0];
375 op_data->op_fid1 = oinfo->lmo_fid;
376 op_data->op_mds = oinfo->lmo_mds;
377 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
379 RETURN(PTR_ERR(tgt));
382 if (!fid_is_sane(&op_data->op_fid2))
383 fid_zero(&op_data->op_fid2);
385 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
386 ", name='%s' -> mds #%u lsm=%p lsm_magic=%x\n",
387 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
388 op_data->op_name ? op_data->op_name : "<NULL>",
389 tgt->ltd_idx, lsm, lsm == NULL ? -1 : lsm->lsm_md_magic);
391 op_data->op_bias &= ~MDS_CROSS_REF;
393 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
399 /* If RPC happens, lsm information will be revalidated
400 * during update_inode process (see ll_update_lsm_md) */
401 if (op_data->op_mea2 != NULL) {
402 rc = lmv_revalidate_slaves(exp, op_data->op_mea2,
409 } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm != NULL &&
410 lmv_need_try_all_stripes(lsm)) {
411 /* For migrating and unknown hash type directory, it will
412 * try to target the entry on other stripes */
415 for (stripe_index = 1;
416 stripe_index < lsm->lsm_md_stripe_count &&
417 it_disposition(it, DISP_LOOKUP_NEG); stripe_index++) {
418 struct lmv_oinfo *oinfo;
420 /* release the previous request */
421 ptlrpc_req_finished(*reqp);
422 it->it_request = NULL;
425 oinfo = &lsm->lsm_md_oinfo[stripe_index];
426 tgt = lmv_find_target(lmv, &oinfo->lmo_fid);
428 RETURN(PTR_ERR(tgt));
430 CDEBUG(D_INODE, "Try other stripes " DFID"\n",
431 PFID(&oinfo->lmo_fid));
433 op_data->op_fid1 = oinfo->lmo_fid;
434 it->it_disposition &= ~DISP_ENQ_COMPLETE;
435 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
436 cb_blocking, extra_lock_flags);
442 if (!it_has_reply_body(it))
446 * MDS has returned success. Probably name has been resolved in
447 * remote inode. Let's check this.
449 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
453 /* Not cross-ref case, just get out of here. */
454 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
455 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
459 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
467 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
468 struct lookup_intent *it, struct ptlrpc_request **reqp,
469 ldlm_blocking_callback cb_blocking,
470 __u64 extra_lock_flags)
476 LASSERT(fid_is_sane(&op_data->op_fid1));
478 CDEBUG(D_INODE, "INTENT LOCK '%s' for "DFID" '%.*s' on "DFID"\n",
479 LL_IT2STR(it), PFID(&op_data->op_fid2),
480 (int)op_data->op_namelen, op_data->op_name,
481 PFID(&op_data->op_fid1));
483 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT | IT_GETXATTR))
484 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
486 else if (it->it_op & IT_OPEN)
487 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,
493 struct lustre_handle lock_handle;
495 if (it->it_lock_mode != 0) {
496 lock_handle.cookie = it->it_lock_handle;
497 ldlm_lock_decref_and_cancel(&lock_handle,
501 it->it_lock_handle = 0;
502 it->it_lock_mode = 0;
504 if (it->it_remote_lock_mode != 0) {
505 lock_handle.cookie = it->it_remote_lock_handle;
506 ldlm_lock_decref_and_cancel(&lock_handle,
507 it->it_remote_lock_mode);
510 it->it_remote_lock_handle = 0;
511 it->it_remote_lock_mode = 0;