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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LMV
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
44 #include <linux/math64.h>
45 #include <linux/seq_file.h>
46 #include <linux/namei.h>
47 #include <linux/lustre_intent.h>
49 #include <liblustre.h>
52 #include <obd_support.h>
53 #include <lustre/lustre_idl.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <lustre_dlm.h>
57 #include <lustre_mdc.h>
58 #include <obd_class.h>
59 #include <lprocfs_status.h>
60 #include "lmv_internal.h"
62 static int lmv_intent_remote(struct obd_export *exp, struct lookup_intent *it,
63 const struct lu_fid *parent_fid,
64 struct ptlrpc_request **reqp,
65 ldlm_blocking_callback cb_blocking,
66 __u64 extra_lock_flags)
68 struct obd_device *obd = exp->exp_obd;
69 struct lmv_obd *lmv = &obd->u.lmv;
70 struct ptlrpc_request *req = NULL;
71 struct lustre_handle plock;
72 struct md_op_data *op_data;
73 struct lmv_tgt_desc *tgt;
74 struct mdt_body *body;
79 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
83 LASSERT((body->mbo_valid & OBD_MD_MDS));
86 * Unfortunately, we have to lie to MDC/MDS to retrieve
87 * attributes llite needs and provideproper locking.
89 if (it->it_op & IT_LOOKUP)
90 it->it_op = IT_GETATTR;
93 * We got LOOKUP lock, but we really need attrs.
95 pmode = it->d.lustre.it_lock_mode;
97 plock.cookie = it->d.lustre.it_lock_handle;
98 it->d.lustre.it_lock_mode = 0;
99 it->d.lustre.it_data = NULL;
102 LASSERT(fid_is_sane(&body->mbo_fid1));
104 tgt = lmv_find_target(lmv, &body->mbo_fid1);
106 GOTO(out, rc = PTR_ERR(tgt));
108 OBD_ALLOC_PTR(op_data);
110 GOTO(out, rc = -ENOMEM);
112 op_data->op_fid1 = body->mbo_fid1;
113 /* Sent the parent FID to the remote MDT */
114 if (parent_fid != NULL) {
115 /* The parent fid is only for remote open to
116 * check whether the open is from OBF,
117 * see mdt_cross_open */
118 LASSERT(it->it_op & IT_OPEN);
119 op_data->op_fid2 = *parent_fid;
120 /* Add object FID to op_fid3, in case it needs to check stale
121 * (M_CHECK_STALE), see mdc_finish_intent_lock */
122 op_data->op_fid3 = body->mbo_fid1;
125 op_data->op_bias = MDS_CROSS_REF;
126 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
127 PFID(&body->mbo_fid1), tgt->ltd_idx);
129 rc = md_intent_lock(tgt->ltd_exp, op_data, it, &req, cb_blocking,
132 GOTO(out_free_op_data, rc);
135 * LLite needs LOOKUP lock to track dentry revocation in order to
136 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
137 * and put LOOKUP in request.
139 if (it->d.lustre.it_lock_mode != 0) {
140 it->d.lustre.it_remote_lock_handle =
141 it->d.lustre.it_lock_handle;
142 it->d.lustre.it_remote_lock_mode = it->d.lustre.it_lock_mode;
146 it->d.lustre.it_lock_handle = plock.cookie;
147 it->d.lustre.it_lock_mode = pmode;
152 OBD_FREE_PTR(op_data);
155 ldlm_lock_decref(&plock, pmode);
157 ptlrpc_req_finished(*reqp);
163 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
164 struct lmv_stripe_md *lsm,
165 ldlm_blocking_callback cb_blocking,
166 int extra_lock_flags)
168 struct obd_device *obd = exp->exp_obd;
169 struct lmv_obd *lmv = &obd->u.lmv;
170 struct mdt_body *body;
171 struct md_op_data *op_data;
172 unsigned long size = 0;
173 unsigned long nlink = 0;
183 * revalidate slaves has some problems, temporarily return,
184 * we may not need that
186 OBD_ALLOC_PTR(op_data);
191 * Loop over the stripe information, check validity and update them
192 * from MDS if needed.
194 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
196 struct lookup_intent it = { .it_op = IT_GETATTR };
197 struct ptlrpc_request *req = NULL;
198 struct lustre_handle *lockh = NULL;
199 struct lmv_tgt_desc *tgt = NULL;
202 fid = lsm->lsm_md_oinfo[i].lmo_fid;
203 inode = lsm->lsm_md_oinfo[i].lmo_root;
206 * Prepare op_data for revalidating. Note that @fid2 shluld be
207 * defined otherwise it will go to server and take new lock
208 * which is not needed here.
210 memset(op_data, 0, sizeof(*op_data));
211 op_data->op_fid1 = fid;
212 op_data->op_fid2 = fid;
214 tgt = lmv_locate_mds(lmv, op_data, &fid);
216 GOTO(cleanup, rc = PTR_ERR(tgt));
218 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%d\n",
219 PFID(&fid), tgt->ltd_idx);
221 rc = md_intent_lock(tgt->ltd_exp, op_data, &it, &req,
222 cb_blocking, extra_lock_flags);
226 lockh = (struct lustre_handle *)&it.d.lustre.it_lock_handle;
227 if (rc > 0 && req == NULL) {
228 /* slave inode is still valid */
229 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
233 /* refresh slave from server */
234 body = req_capsule_server_get(&req->rq_pill,
236 LASSERT(body != NULL);
237 if (unlikely(body->mbo_nlink < 2)) {
238 CERROR("%s: nlink %d < 2 corrupt stripe %d "DFID
240 obd->obd_name, body->mbo_nlink, i,
241 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
242 PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
245 ptlrpc_req_finished(req);
247 if (it.d.lustre.it_lock_mode && lockh) {
248 ldlm_lock_decref(lockh,
249 it.d.lustre.it_lock_mode);
250 it.d.lustre.it_lock_mode = 0;
253 GOTO(cleanup, rc = -EIO);
257 i_size_write(inode, body->mbo_size);
258 set_nlink(inode, body->mbo_nlink);
259 LTIME_S(inode->i_atime) = body->mbo_atime;
260 LTIME_S(inode->i_ctime) = body->mbo_ctime;
261 LTIME_S(inode->i_mtime) = body->mbo_mtime;
264 ptlrpc_req_finished(req);
267 md_set_lock_data(tgt->ltd_exp, &lockh->cookie, inode, NULL);
269 size += i_size_read(inode);
272 nlink += inode->i_nlink - 2;
274 nlink += inode->i_nlink;
276 atime = LTIME_S(inode->i_atime) > atime ?
277 LTIME_S(inode->i_atime) : atime;
278 ctime = LTIME_S(inode->i_ctime) > ctime ?
279 LTIME_S(inode->i_ctime) : ctime;
280 mtime = LTIME_S(inode->i_mtime) > mtime ?
281 LTIME_S(inode->i_mtime) : mtime;
283 if (it.d.lustre.it_lock_mode != 0 && lockh != NULL) {
284 ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
285 it.d.lustre.it_lock_mode = 0;
288 CDEBUG(D_INODE, "i %d "DFID" size %llu, nlink %u, atime "
289 "%lu, mtime %lu, ctime %lu.\n", i, PFID(&fid),
290 i_size_read(inode), inode->i_nlink,
291 LTIME_S(inode->i_atime), LTIME_S(inode->i_mtime),
292 LTIME_S(inode->i_ctime));
296 * update attr of master request.
298 CDEBUG(D_INODE, "Return refreshed attrs: size = %lu nlink %lu atime "
299 LPU64 "ctime "LPU64" mtime "LPU64" for " DFID"\n", size, nlink,
300 atime, ctime, mtime, PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
303 mbody->mbo_atime = atime;
304 mbody->mbo_ctime = ctime;
305 mbody->mbo_mtime = mtime;
308 OBD_FREE_PTR(op_data);
314 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
315 struct lmv_stripe_md *lsm,
316 ldlm_blocking_callback cb_blocking,
317 int extra_lock_flags)
325 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
328 int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
329 struct lookup_intent *it, struct ptlrpc_request **reqp,
330 ldlm_blocking_callback cb_blocking, __u64 extra_lock_flags)
332 struct obd_device *obd = exp->exp_obd;
333 struct lmv_obd *lmv = &obd->u.lmv;
334 struct lmv_tgt_desc *tgt;
335 struct mdt_body *body;
339 /* Note: client might open with some random flags(sanity 33b), so we can
340 * not make sure op_fid2 is being initialized with BY_FID flag */
341 if (it->it_flags & MDS_OPEN_BY_FID && fid_is_sane(&op_data->op_fid2)) {
342 if (op_data->op_mea1 != NULL) {
343 struct lmv_stripe_md *lsm = op_data->op_mea1;
344 const struct lmv_oinfo *oinfo;
346 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
347 op_data->op_namelen);
349 RETURN(PTR_ERR(oinfo));
350 op_data->op_fid1 = oinfo->lmo_fid;
353 tgt = lmv_find_target(lmv, &op_data->op_fid2);
355 RETURN(PTR_ERR(tgt));
357 op_data->op_mds = tgt->ltd_idx;
359 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
361 RETURN(PTR_ERR(tgt));
364 /* If it is ready to open the file by FID, do not need
365 * allocate FID at all, otherwise it will confuse MDT */
366 if ((it->it_op & IT_CREAT) &&
367 !(it->it_flags & MDS_OPEN_BY_FID)) {
369 * For open with IT_CREATE and for IT_CREATE cases allocate new
370 * fid and setup FLD for it.
372 op_data->op_fid3 = op_data->op_fid2;
373 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
378 CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
379 " name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
380 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
382 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
387 * Nothing is found, do not access body->fid1 as it is zero and thus
390 if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
391 !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
392 !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
395 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
399 /* Not cross-ref case, just get out of here. */
400 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
401 rc = lmv_intent_remote(exp, it, &op_data->op_fid1, reqp,
402 cb_blocking, extra_lock_flags);
406 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
415 * Handler for: getattr, lookup and revalidate cases.
418 lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
419 struct lookup_intent *it, struct ptlrpc_request **reqp,
420 ldlm_blocking_callback cb_blocking,
421 __u64 extra_lock_flags)
423 struct obd_device *obd = exp->exp_obd;
424 struct lmv_obd *lmv = &obd->u.lmv;
425 struct lmv_tgt_desc *tgt = NULL;
426 struct mdt_body *body;
427 struct lmv_stripe_md *lsm = op_data->op_mea1;
431 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
433 RETURN(PTR_ERR(tgt));
435 if (!fid_is_sane(&op_data->op_fid2))
436 fid_zero(&op_data->op_fid2);
438 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
439 ", name='%s' -> mds #%d lsm=%p lsm_magic=%x\n",
440 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
441 op_data->op_name ? op_data->op_name : "<NULL>",
442 tgt->ltd_idx, lsm, lsm == NULL ? -1 : lsm->lsm_md_magic);
444 op_data->op_bias &= ~MDS_CROSS_REF;
446 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp, cb_blocking,
452 /* If RPC happens, lsm information will be revalidated
453 * during update_inode process (see ll_update_lsm_md) */
454 if (op_data->op_mea2 != NULL) {
455 rc = lmv_revalidate_slaves(exp, NULL, op_data->op_mea2,
462 } else if (it_disposition(it, DISP_LOOKUP_NEG) && lsm != NULL &&
463 lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) {
464 /* For migrating directory, if it can not find the child in
465 * the source directory(master stripe), try the targeting
466 * directory(stripe 1) */
467 tgt = lmv_find_target(lmv, &lsm->lsm_md_oinfo[1].lmo_fid);
469 RETURN(PTR_ERR(tgt));
471 ptlrpc_req_finished(*reqp);
472 it->d.lustre.it_data = NULL;
475 CDEBUG(D_INODE, "For migrating dir, try target dir "DFID"\n",
476 PFID(&lsm->lsm_md_oinfo[1].lmo_fid));
478 op_data->op_fid1 = lsm->lsm_md_oinfo[1].lmo_fid;
479 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
480 rc = md_intent_lock(tgt->ltd_exp, op_data, it, reqp,
481 cb_blocking, extra_lock_flags);
484 * MDS has returned success. Probably name has been resolved in
485 * remote inode. Let's check this.
487 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
491 /* Not cross-ref case, just get out of here. */
492 if (unlikely((body->mbo_valid & OBD_MD_MDS))) {
493 rc = lmv_intent_remote(exp, it, NULL, reqp, cb_blocking,
497 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
505 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
506 struct lookup_intent *it, struct ptlrpc_request **reqp,
507 ldlm_blocking_callback cb_blocking,
508 __u64 extra_lock_flags)
510 struct obd_device *obd = exp->exp_obd;
515 LASSERT(fid_is_sane(&op_data->op_fid1));
517 CDEBUG(D_INODE, "INTENT LOCK '%s' for '%*s' on "DFID"\n",
518 LL_IT2STR(it), op_data->op_namelen, op_data->op_name,
519 PFID(&op_data->op_fid1));
521 rc = lmv_check_connect(obd);
525 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
526 rc = lmv_intent_lookup(exp, op_data, it, reqp, cb_blocking,
528 else if (it->it_op & IT_OPEN)
529 rc = lmv_intent_open(exp, op_data, it, reqp, cb_blocking,