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 <obd_class.h>
58 #include <lprocfs_status.h>
59 #include "lmv_internal.h"
61 static int lmv_intent_remote(struct obd_export *exp, void *lmm,
62 int lmmsize, struct lookup_intent *it,
63 const struct lu_fid *parent_fid, int flags,
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->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->fid1));
104 tgt = lmv_find_target(lmv, &body->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->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->fid1;
125 op_data->op_bias = MDS_CROSS_REF;
126 CDEBUG(D_INODE, "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
127 PFID(&body->fid1), tgt->ltd_idx);
129 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
130 flags, &req, cb_blocking, extra_lock_flags);
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;
145 it->d.lustre.it_lock_handle = plock.cookie;
146 it->d.lustre.it_lock_mode = pmode;
150 OBD_FREE_PTR(op_data);
153 ldlm_lock_decref(&plock, pmode);
155 ptlrpc_req_finished(*reqp);
161 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
162 struct lmv_stripe_md *lsm,
163 ldlm_blocking_callback cb_blocking,
164 int extra_lock_flags)
166 struct obd_device *obd = exp->exp_obd;
167 struct lmv_obd *lmv = &obd->u.lmv;
168 struct mdt_body *body;
169 struct md_op_data *op_data;
170 unsigned long size = 0;
171 unsigned long nlink = 0;
181 * revalidate slaves has some problems, temporarily return,
182 * we may not need that
184 if (lsm->lsm_md_stripe_count <= 1)
187 OBD_ALLOC_PTR(op_data);
192 * Loop over the stripe information, check validity and update them
193 * from MDS if needed.
195 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
197 struct lookup_intent it = { .it_op = IT_GETATTR };
198 struct ptlrpc_request *req = NULL;
199 struct lustre_handle *lockh = NULL;
200 struct lmv_tgt_desc *tgt = NULL;
203 fid = lsm->lsm_md_oinfo[i].lmo_fid;
204 inode = lsm->lsm_md_oinfo[i].lmo_root;
215 * Prepare op_data for revalidating. Note that @fid2 shluld be
216 * defined otherwise it will go to server and take new lock
217 * which is not needed here.
219 memset(op_data, 0, sizeof(*op_data));
220 op_data->op_fid1 = fid;
221 op_data->op_fid2 = fid;
223 tgt = lmv_locate_mds(lmv, op_data, &fid);
225 GOTO(cleanup, rc = PTR_ERR(tgt));
227 CDEBUG(D_INODE, "Revalidate slave "DFID" -> mds #%d\n",
228 PFID(&fid), tgt->ltd_idx);
230 rc = md_intent_lock(tgt->ltd_exp, op_data, NULL, 0, &it, 0,
231 &req, cb_blocking, extra_lock_flags);
235 lockh = (struct lustre_handle *)&it.d.lustre.it_lock_handle;
236 if (rc > 0 && req == NULL) {
237 /* slave inode is still valid */
238 CDEBUG(D_INODE, "slave "DFID" is still valid.\n",
242 /* refresh slave from server */
243 body = req_capsule_server_get(&req->rq_pill,
245 LASSERT(body != NULL);
247 if (unlikely(body->nlink < 2)) {
248 CERROR("%s: nlink %d < 2 corrupt stripe %d "DFID
249 ":" DFID"\n", obd->obd_name, body->nlink,
250 i, PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
251 PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
254 ptlrpc_req_finished(req);
256 GOTO(cleanup, rc = -EIO);
260 md_set_lock_data(tgt->ltd_exp, &lockh->cookie,
263 i_size_write(inode, body->size);
264 set_nlink(inode, body->nlink);
265 LTIME_S(inode->i_atime) = body->atime;
266 LTIME_S(inode->i_ctime) = body->ctime;
267 LTIME_S(inode->i_mtime) = body->mtime;
270 ptlrpc_req_finished(req);
273 size += i_size_read(inode);
276 nlink += inode->i_nlink - 2;
278 nlink += inode->i_nlink;
280 atime = LTIME_S(inode->i_atime) > atime ?
281 LTIME_S(inode->i_atime) : atime;
282 ctime = LTIME_S(inode->i_ctime) > ctime ?
283 LTIME_S(inode->i_ctime) : ctime;
284 mtime = LTIME_S(inode->i_mtime) > mtime ?
285 LTIME_S(inode->i_mtime) : mtime;
287 if (it.d.lustre.it_lock_mode != 0 && lockh != NULL) {
288 ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
289 it.d.lustre.it_lock_mode = 0;
292 CDEBUG(D_INODE, "i %d "DFID" size %llu, nlink %u, atime "
293 "%lu, mtime %lu, ctime %lu.\n", i, PFID(&fid),
294 i_size_read(inode), inode->i_nlink,
295 LTIME_S(inode->i_atime), LTIME_S(inode->i_mtime),
296 LTIME_S(inode->i_ctime));
300 * update attr of master request.
302 CDEBUG(D_INODE, "Return refreshed attrs: size = %lu nlink %lu atime "
303 LPU64 "ctime "LPU64" mtime "LPU64" for " DFID"\n", size, nlink,
304 atime, ctime, mtime, PFID(&lsm->lsm_md_oinfo[0].lmo_fid));
307 mbody->atime = atime;
308 mbody->ctime = ctime;
309 mbody->mtime = mtime;
312 OBD_FREE_PTR(op_data);
318 int lmv_revalidate_slaves(struct obd_export *exp, struct mdt_body *mbody,
319 struct lmv_stripe_md *lsm,
320 ldlm_blocking_callback cb_blocking,
321 int extra_lock_flags)
329 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
332 int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
333 void *lmm, int lmmsize, struct lookup_intent *it,
334 int flags, struct ptlrpc_request **reqp,
335 ldlm_blocking_callback cb_blocking,
336 __u64 extra_lock_flags)
338 struct obd_device *obd = exp->exp_obd;
339 struct lmv_obd *lmv = &obd->u.lmv;
340 struct lmv_tgt_desc *tgt;
341 struct mdt_body *body;
345 /* Note: client might open with some random flags(sanity 33b), so we can
346 * not make sure op_fid2 is being initialized with BY_FID flag */
347 if (it->it_flags & MDS_OPEN_BY_FID && fid_is_sane(&op_data->op_fid2)) {
348 if (op_data->op_mea1 != NULL) {
349 struct lmv_stripe_md *lsm = op_data->op_mea1;
350 const struct lmv_oinfo *oinfo;
352 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
353 op_data->op_namelen);
354 op_data->op_fid1 = oinfo->lmo_fid;
357 tgt = lmv_find_target(lmv, &op_data->op_fid2);
359 RETURN(PTR_ERR(tgt));
361 op_data->op_mds = tgt->ltd_idx;
363 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
365 RETURN(PTR_ERR(tgt));
368 /* If it is ready to open the file by FID, do not need
369 * allocate FID at all, otherwise it will confuse MDT */
370 if ((it->it_op & IT_CREAT) &&
371 !(it->it_flags & MDS_OPEN_BY_FID)) {
373 * For open with IT_CREATE and for IT_CREATE cases allocate new
374 * fid and setup FLD for it.
376 op_data->op_fid3 = op_data->op_fid2;
377 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
382 CDEBUG(D_INODE, "OPEN_INTENT with fid1="DFID", fid2="DFID","
383 " name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
384 PFID(&op_data->op_fid2), op_data->op_name, tgt->ltd_idx);
386 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it, flags,
387 reqp, cb_blocking, extra_lock_flags);
391 * Nothing is found, do not access body->fid1 as it is zero and thus
394 if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
395 !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
396 !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
399 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
403 /* Not cross-ref case, just get out of here. */
404 if (unlikely((body->valid & OBD_MD_MDS))) {
405 rc = lmv_intent_remote(exp, lmm, lmmsize, it, &op_data->op_fid1,
406 flags, reqp, cb_blocking,
411 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
420 * Handler for: getattr, lookup and revalidate cases.
422 int lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
423 void *lmm, int lmmsize, struct lookup_intent *it,
424 int flags, struct ptlrpc_request **reqp,
425 ldlm_blocking_callback cb_blocking,
426 __u64 extra_lock_flags)
428 struct obd_device *obd = exp->exp_obd;
429 struct lmv_obd *lmv = &obd->u.lmv;
430 struct lmv_tgt_desc *tgt = NULL;
431 struct mdt_body *body;
435 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
437 RETURN(PTR_ERR(tgt));
439 if (!fid_is_sane(&op_data->op_fid2))
440 fid_zero(&op_data->op_fid2);
442 CDEBUG(D_INODE, "LOOKUP_INTENT with fid1="DFID", fid2="DFID
443 ", name='%s' -> mds #%d\n", PFID(&op_data->op_fid1),
444 PFID(&op_data->op_fid2),
445 op_data->op_name ? op_data->op_name : "<NULL>",
448 op_data->op_bias &= ~MDS_CROSS_REF;
450 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
451 flags, reqp, cb_blocking, extra_lock_flags);
457 /* If RPC happens, lsm information will be revalidated
458 * during update_inode process (see ll_update_lsm_md) */
459 if (op_data->op_mea2 != NULL) {
460 rc = lmv_revalidate_slaves(exp, NULL, op_data->op_mea2,
461 cb_blocking, extra_lock_flags);
469 * MDS has returned success. Probably name has been resolved in
470 * remote inode. Let's check this.
472 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
476 /* Not cross-ref case, just get out of here. */
477 if (unlikely((body->valid & OBD_MD_MDS))) {
478 rc = lmv_intent_remote(exp, lmm, lmmsize, it, NULL, flags,
479 reqp, cb_blocking, extra_lock_flags);
482 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
490 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
491 void *lmm, int lmmsize, struct lookup_intent *it,
492 int flags, struct ptlrpc_request **reqp,
493 ldlm_blocking_callback cb_blocking,
494 __u64 extra_lock_flags)
496 struct obd_device *obd = exp->exp_obd;
501 LASSERT(fid_is_sane(&op_data->op_fid1));
503 CDEBUG(D_INODE, "INTENT LOCK '%s' for '%*s' on "DFID"\n",
504 LL_IT2STR(it), op_data->op_namelen, op_data->op_name,
505 PFID(&op_data->op_fid1));
507 rc = lmv_check_connect(obd);
511 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_LAYOUT))
512 rc = lmv_intent_lookup(exp, op_data, lmm, lmmsize, it,
513 flags, reqp, cb_blocking,
515 else if (it->it_op & IT_OPEN)
516 rc = lmv_intent_open(exp, op_data, lmm, lmmsize, it,
517 flags, reqp, cb_blocking,