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 <asm/div64.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, void *lmm,
63 int lmmsize, struct lookup_intent *it,
64 const struct lu_fid *parent_fid, int flags,
65 struct ptlrpc_request **reqp,
66 ldlm_blocking_callback cb_blocking,
67 __u64 extra_lock_flags)
69 struct obd_device *obd = exp->exp_obd;
70 struct lmv_obd *lmv = &obd->u.lmv;
71 struct ptlrpc_request *req = NULL;
72 struct lustre_handle plock;
73 struct md_op_data *op_data;
74 struct lmv_tgt_desc *tgt;
75 struct mdt_body *body;
80 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
84 LASSERT((body->valid & OBD_MD_MDS));
87 * Unfortunately, we have to lie to MDC/MDS to retrieve
88 * attributes llite needs and provideproper locking.
90 if (it->it_op & IT_LOOKUP)
91 it->it_op = IT_GETATTR;
94 * We got LOOKUP lock, but we really need attrs.
96 pmode = it->d.lustre.it_lock_mode;
98 plock.cookie = it->d.lustre.it_lock_handle;
99 it->d.lustre.it_lock_mode = 0;
100 it->d.lustre.it_data = NULL;
103 LASSERT(fid_is_sane(&body->fid1));
105 tgt = lmv_find_target(lmv, &body->fid1);
107 GOTO(out, rc = PTR_ERR(tgt));
109 OBD_ALLOC_PTR(op_data);
111 GOTO(out, rc = -ENOMEM);
113 op_data->op_fid1 = body->fid1;
114 /* Sent the parent FID to the remote MDT */
115 if (parent_fid != NULL) {
116 /* The parent fid is only for remote open to
117 * check whether the open is from OBF,
118 * see mdt_cross_open */
119 LASSERT(it->it_op & IT_OPEN);
120 op_data->op_fid2 = *parent_fid;
121 /* Add object FID to op_fid3, in case it needs to check stale
122 * (M_CHECK_STALE), see mdc_finish_intent_lock */
123 op_data->op_fid3 = body->fid1;
126 op_data->op_bias = MDS_CROSS_REF;
127 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
128 PFID(&body->fid1), tgt->ltd_idx);
130 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
131 flags, &req, cb_blocking, extra_lock_flags);
133 GOTO(out_free_op_data, rc);
136 * LLite needs LOOKUP lock to track dentry revocation in order to
137 * maintain dcache consistency. Thus drop UPDATE|PERM lock here
138 * and put LOOKUP in request.
140 if (it->d.lustre.it_lock_mode != 0) {
141 it->d.lustre.it_remote_lock_handle =
142 it->d.lustre.it_lock_handle;
143 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;
151 OBD_FREE_PTR(op_data);
154 ldlm_lock_decref(&plock, pmode);
156 ptlrpc_req_finished(*reqp);
162 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
163 struct lmv_stripe_md *lsm,
164 ldlm_blocking_callback cb_blocking,
165 int extra_lock_flags)
167 struct obd_device *obd = exp->exp_obd;
168 struct lmv_obd *lmv = &obd->u.lmv;
169 struct mdt_body *body;
170 struct md_op_data *op_data;
171 unsigned long size = 0;
172 unsigned long nlink = 0;
182 * revalidate slaves has some problems, temporarily return,
183 * we may not need that
185 if (lsm->lsm_md_stripe_count <= 1)
188 OBD_ALLOC_PTR(op_data);
193 * Loop over the stripe information, check validity and update them
194 * from MDS if needed.
196 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
198 struct lookup_intent it = { .it_op = IT_GETATTR };
199 struct ptlrpc_request *req = NULL;
200 struct lustre_handle *lockh = NULL;
201 struct lmv_tgt_desc *tgt = NULL;
204 fid = lsm->lsm_md_oinfo[i].lmo_fid;
205 inode = lsm->lsm_md_oinfo[i].lmo_root;
216 * Prepare op_data for revalidating. Note that @fid2 shluld be
217 * defined otherwise it will go to server and take new lock
218 * which is not needed here.
220 memset(op_data, 0, sizeof(*op_data));
221 op_data->op_fid1 = fid;
222 op_data->op_fid2 = fid;
224 tgt = lmv_locate_mds(lmv, op_data, &fid);
226 GOTO(cleanup, rc = PTR_ERR(tgt));
228 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%d\n",
229 PFID(&fid), tgt->ltd_idx);
231 rc = md_intent_lock(tgt->ltd_exp, op_data, NULL, 0, &it, 0,
232 &req, cb_blocking, extra_lock_flags);
236 lockh = (struct lustre_handle *)&it.d.lustre.it_lock_handle;
237 if (rc > 0 && req == NULL) {
238 /* slave inode is still valid */
239 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
243 /* refresh slave from server */
244 body = req_capsule_server_get(&req->rq_pill,
246 LASSERT(body != NULL);
248 if (unlikely(body->nlink < 2)) {
249 CERROR("%s: nlink %d < 2 corrupt stripe %d "DFID
250 ":" DFID"\n", obd->obd_name, body->nlink,
251 i, PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
252 PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
255 ptlrpc_req_finished(req);
257 if (it.d.lustre.it_lock_mode && lockh) {
258 ldlm_lock_decref(lockh,
259 it.d.lustre.it_lock_mode);
260 it.d.lustre.it_lock_mode = 0;
263 GOTO(cleanup, rc = -EIO);
267 md_set_lock_data(tgt->ltd_exp, &lockh->cookie,
270 i_size_write(inode, body->size);
271 set_nlink(inode, body->nlink);
272 LTIME_S(inode->i_atime) = body->atime;
273 LTIME_S(inode->i_ctime) = body->ctime;
274 LTIME_S(inode->i_mtime) = body->mtime;
277 ptlrpc_req_finished(req);
280 size += i_size_read(inode);
283 nlink += inode->i_nlink - 2;
285 nlink += inode->i_nlink;
287 atime = LTIME_S(inode->i_atime) > atime ?
288 LTIME_S(inode->i_atime) : atime;
289 ctime = LTIME_S(inode->i_ctime) > ctime ?
290 LTIME_S(inode->i_ctime) : ctime;
291 mtime = LTIME_S(inode->i_mtime) > mtime ?
292 LTIME_S(inode->i_mtime) : mtime;
294 if (it.d.lustre.it_lock_mode != 0 && lockh != NULL) {
295 ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
296 it.d.lustre.it_lock_mode = 0;
299 CDEBUG(D_INODE, "i %d "DFID" size %llu, nlink %u, atime "
300 "%lu, mtime %lu, ctime %lu.\n", i, PFID(&fid),
301 i_size_read(inode), inode->i_nlink,
302 LTIME_S(inode->i_atime), LTIME_S(inode->i_mtime),
303 LTIME_S(inode->i_ctime));
307 * update attr of master request.
309 CDEBUG(D_INODE, "Return refreshed attrs: size = %lu nlink %lu atime "
310 LPU64 "ctime "LPU64" mtime "LPU64" for " DFID"\n", size, nlink,
311 atime, ctime, mtime, PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
314 mbody->atime = atime;
315 mbody->ctime = ctime;
316 mbody->mtime = mtime;
319 OBD_FREE_PTR(op_data);
325 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
326 struct lmv_stripe_md *lsm,
327 ldlm_blocking_callback cb_blocking,
328 int extra_lock_flags)
336 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
339 int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
340 void *lmm, int lmmsize, struct lookup_intent *it,
341 int flags, struct ptlrpc_request **reqp,
342 ldlm_blocking_callback cb_blocking,
343 __u64 extra_lock_flags)
345 struct obd_device *obd = exp->exp_obd;
346 struct lmv_obd *lmv = &obd->u.lmv;
347 struct lmv_tgt_desc *tgt;
348 struct mdt_body *body;
352 /* Note: client might open with some random flags(sanity 33b), so we can
353 * not make sure op_fid2 is being initialized with BY_FID flag */
354 if (it->it_flags & MDS_OPEN_BY_FID && fid_is_sane(&op_data->op_fid2)) {
355 if (op_data->op_mea1 != NULL) {
356 struct lmv_stripe_md *lsm = op_data->op_mea1;
357 const struct lmv_oinfo *oinfo;
359 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
360 op_data->op_namelen);
362 RETURN(PTR_ERR(oinfo));
363 op_data->op_fid1 = oinfo->lmo_fid;
366 tgt = lmv_find_target(lmv, &op_data->op_fid2);
368 RETURN(PTR_ERR(tgt));
370 op_data->op_mds = tgt->ltd_idx;
372 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
374 RETURN(PTR_ERR(tgt));
377 /* If it is ready to open the file by FID, do not need
378 * allocate FID at all, otherwise it will confuse MDT */
379 if ((it->it_op & IT_CREAT) &&
380 !(it->it_flags & MDS_OPEN_BY_FID)) {
382 * For open with IT_CREATE and for IT_CREATE cases allocate new
383 * fid and setup FLD for it.
385 op_data->op_fid3 = op_data->op_fid2;
386 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
391 CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
392 " name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
393 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
395 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it, flags,
396 reqp, cb_blocking, extra_lock_flags);
400 * Nothing is found, do not access body->fid1 as it is zero and thus
403 if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
404 !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
405 !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
408 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
412 /* Not cross-ref case, just get out of here. */
413 if (unlikely((body->valid & OBD_MD_MDS))) {
414 rc = lmv_intent_remote(exp, lmm, lmmsize, it, &op_data->op_fid1,
415 flags, reqp, cb_blocking,
420 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
429 * Handler for: getattr, lookup and revalidate cases.
431 int lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
432 void *lmm, int lmmsize, struct lookup_intent *it,
433 int flags, struct ptlrpc_request **reqp,
434 ldlm_blocking_callback cb_blocking,
435 __u64 extra_lock_flags)
437 struct obd_device *obd = exp->exp_obd;
438 struct lmv_obd *lmv = &obd->u.lmv;
439 struct lmv_tgt_desc *tgt = NULL;
440 struct mdt_body *body;
441 struct lmv_stripe_md *lsm = op_data->op_mea1;
445 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
447 RETURN(PTR_ERR(tgt));
449 if (!fid_is_sane(&op_data->op_fid2))
450 fid_zero(&op_data->op_fid2);
452 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
453 ", name='%s' -> mds #%d lsm=%p lsm_magic=%x\n",
454 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
455 op_data->op_name ? op_data->op_name : "<NULL>",
456 tgt->ltd_idx, lsm, lsm == NULL ? -1 : lsm->lsm_md_magic);
458 op_data->op_bias &= ~MDS_CROSS_REF;
460 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
461 flags, reqp, cb_blocking, extra_lock_flags);
466 /* If RPC happens, lsm information will be revalidated
467 * during update_inode process (see ll_update_lsm_md) */
468 if (op_data->op_mea2 != NULL) {
469 rc = lmv_revalidate_slaves(exp, NULL, op_data->op_mea2,
476 } else if (it_disposition(it, DISP_LOOKUP_NEG) &&
477 lsm != NULL && lsm->lsm_md_magic == LMV_MAGIC_MIGRATE) {
478 /* For migrating directory, if it can not find the child in
479 * the source directory(master stripe), try the targeting
480 * directory(stripe 1) */
481 tgt = lmv_find_target(lmv, &lsm->lsm_md_oinfo[1].lmo_fid);
483 RETURN(PTR_ERR(tgt));
485 ptlrpc_req_finished(*reqp);
486 CDEBUG(D_INODE, "For migrating dir, try target dir "DFID"\n",
487 PFID(&lsm->lsm_md_oinfo[1].lmo_fid));
489 op_data->op_fid1 = lsm->lsm_md_oinfo[1].lmo_fid;
490 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
491 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
492 flags, reqp, cb_blocking, extra_lock_flags);
496 * MDS has returned success. Probably name has been resolved in
497 * remote inode. Let's check this.
499 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
503 /* Not cross-ref case, just get out of here. */
504 if (unlikely((body->valid & OBD_MD_MDS))) {
505 rc = lmv_intent_remote(exp, lmm, lmmsize, it, NULL, flags,
506 reqp, cb_blocking, extra_lock_flags);
509 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
517 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
518 void *lmm, int lmmsize, struct lookup_intent *it,
519 int flags, struct ptlrpc_request **reqp,
520 ldlm_blocking_callback cb_blocking,
521 __u64 extra_lock_flags)
523 struct obd_device *obd = exp->exp_obd;
528 LASSERT(fid_is_sane(&op_data->op_fid1));
530 CDEBUG(D_INODE, "INTENT LOCK '%s' for '%*s' on "DFID"\n",
531 LL_IT2STR(it), op_data->op_namelen, op_data->op_name,
532 PFID(&op_data->op_fid1));
534 rc = lmv_check_connect(obd);
538 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
539 rc = lmv_intent_lookup(exp, op_data, lmm, lmmsize, it,
540 flags, reqp, cb_blocking,
542 else if (it->it_op & IT_OPEN)
543 rc = lmv_intent_open(exp, op_data, lmm, lmmsize, it,
544 flags, reqp, cb_blocking,