1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LMV
42 #include <linux/slab.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/pagemap.h>
47 #include <asm/div64.h>
48 #include <linux/seq_file.h>
49 #include <linux/namei.h>
50 #include <linux/lustre_intent.h>
52 #include <liblustre.h>
55 #include <obd_support.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre_lib.h>
58 #include <lustre_net.h>
59 #include <lustre_dlm.h>
60 #include <obd_class.h>
61 #include <lprocfs_status.h>
62 #include "lmv_internal.h"
64 int lmv_intent_remote(struct obd_export *exp, void *lmm,
65 int lmmsize, struct lookup_intent *it,
66 int flags, struct ptlrpc_request **reqp,
67 ldlm_blocking_callback cb_blocking,
70 struct obd_device *obd = exp->exp_obd;
71 struct lmv_obd *lmv = &obd->u.lmv;
72 struct ptlrpc_request *req = NULL;
73 struct lustre_handle plock;
74 struct md_op_data *op_data;
75 struct lmv_tgt_desc *tgt;
76 struct mdt_body *body;
81 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
86 * Not cross-ref case, just get out of here.
88 if (!(body->valid & OBD_MD_MDS))
92 * Unfortunately, we have to lie to MDC/MDS to retrieve
93 * attributes llite needs and provideproper locking.
95 if (it->it_op & IT_LOOKUP)
96 it->it_op = IT_GETATTR;
99 * We got LOOKUP lock, but we really need attrs.
101 pmode = it->d.lustre.it_lock_mode;
103 plock.cookie = it->d.lustre.it_lock_handle;
104 it->d.lustre.it_lock_mode = 0;
105 it->d.lustre.it_data = NULL;
108 LASSERT(fid_is_sane(&body->fid1));
110 tgt = lmv_find_target(lmv, &body->fid1);
112 GOTO(out, rc = PTR_ERR(tgt));
114 OBD_ALLOC_PTR(op_data);
116 GOTO(out, rc = -ENOMEM);
118 op_data->op_fid1 = body->fid1;
119 op_data->op_bias = MDS_CROSS_REF;
122 "REMOTE_INTENT with fid="DFID" -> mds #%d\n",
123 PFID(&body->fid1), tgt->ltd_idx);
125 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
126 rc = md_intent_lock(tgt->ltd_exp, op_data, lmm, lmmsize, it,
127 flags, &req, cb_blocking, extra_lock_flags);
129 GOTO(out_free_op_data, rc);
132 * LLite needs LOOKUP lock to track dentry revocation in order to
133 * maintain dcache consistency. Thus drop UPDATE lock here and put
136 if (it->d.lustre.it_lock_mode != 0) {
137 ldlm_lock_decref((void *)&it->d.lustre.it_lock_handle,
138 it->d.lustre.it_lock_mode);
139 it->d.lustre.it_lock_mode = 0;
141 it->d.lustre.it_lock_handle = plock.cookie;
142 it->d.lustre.it_lock_mode = pmode;
146 OBD_FREE_PTR(op_data);
149 ldlm_lock_decref(&plock, pmode);
151 ptlrpc_req_finished(*reqp);
157 * IT_OPEN is intended to open (and create, possible) an object. Parent (pid)
160 int lmv_intent_open(struct obd_export *exp, struct md_op_data *op_data,
161 void *lmm, int lmmsize, struct lookup_intent *it,
162 int flags, struct ptlrpc_request **reqp,
163 ldlm_blocking_callback cb_blocking,
164 int extra_lock_flags)
166 struct obd_device *obd = exp->exp_obd;
167 struct lu_fid rpid = op_data->op_fid1;
168 struct lmv_obd *lmv = &obd->u.lmv;
169 struct md_op_data *sop_data;
170 struct lmv_stripe_md *mea;
171 struct lmv_tgt_desc *tgt;
172 struct mdt_body *body;
173 struct lmv_object *obj;
179 OBD_ALLOC_PTR(sop_data);
180 if (sop_data == NULL)
183 /* save op_data fro repeat case */
184 *sop_data = *op_data;
190 obj = lmv_object_find(obd, &rpid);
193 * Directory is already split, so we have to forward request to
196 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
197 (char *)op_data->op_name,
198 op_data->op_namelen);
200 rpid = obj->lo_stripes[sidx].ls_fid;
202 sop_data->op_mds = obj->lo_stripes[sidx].ls_mds;
203 tgt = lmv_get_target(lmv, sop_data->op_mds);
204 sop_data->op_bias &= ~MDS_CHECK_SPLIT;
208 "Choose slave dir ("DFID") -> mds #%d\n",
209 PFID(&rpid), tgt->ltd_idx);
211 sop_data->op_bias |= MDS_CHECK_SPLIT;
212 tgt = lmv_find_target(lmv, &rpid);
213 sop_data->op_mds = tgt->ltd_idx;
216 GOTO(out_free_sop_data, rc = PTR_ERR(tgt));
218 sop_data->op_fid1 = rpid;
220 if (it->it_op & IT_CREAT) {
222 * For open with IT_CREATE and for IT_CREATE cases allocate new
223 * fid and setup FLD for it.
225 sop_data->op_fid3 = sop_data->op_fid2;
226 rc = lmv_fid_alloc(exp, &sop_data->op_fid2, sop_data);
228 GOTO(out_free_sop_data, rc);
233 GOTO(out_free_sop_data, rc);
237 "OPEN_INTENT with fid1="DFID", fid2="DFID", name='%s' -> mds #%d\n",
238 PFID(&sop_data->op_fid1), PFID(&sop_data->op_fid2),
239 sop_data->op_name, tgt->ltd_idx);
241 rc = md_intent_lock(tgt->ltd_exp, sop_data, lmm, lmmsize, it, flags,
242 reqp, cb_blocking, extra_lock_flags);
244 if (rc == -ERESTART) {
245 LASSERT(*reqp != NULL);
246 DEBUG_REQ(D_WARNING|D_RPCTRACE, *reqp,
247 "Got -ERESTART during open!\n");
248 ptlrpc_req_finished(*reqp);
250 it->d.lustre.it_data = NULL;
253 * Directory got split. Time to update local object and repeat
254 * the request with proper MDS.
256 LASSERT(lu_fid_eq(&op_data->op_fid1, &rpid));
257 rc = lmv_handle_split(exp, &rpid);
259 /* We should reallocate child FID. */
260 rc = lmv_allocate_slaves(obd, &rpid, op_data,
268 GOTO(out_free_sop_data, rc);
271 * Nothing is found, do not access body->fid1 as it is zero and thus
274 if ((it->d.lustre.it_disposition & DISP_LOOKUP_NEG) &&
275 !(it->d.lustre.it_disposition & DISP_OPEN_CREATE) &&
276 !(it->d.lustre.it_disposition & DISP_OPEN_OPEN))
277 GOTO(out_free_sop_data, rc = 0);
280 * Okay, MDS has returned success. Probably name has been resolved in
283 rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags, reqp,
284 cb_blocking, extra_lock_flags);
288 * This is possible, that some userspace application will try to
289 * open file as directory and we will have -ENOTDIR here. As
290 * this is normal situation, we should not print error here,
293 CDEBUG(D_INODE, "Can't handle remote %s: dir "DFID"("DFID"):"
294 "%*s: %d\n", LL_IT2STR(it), PFID(&op_data->op_fid2),
295 PFID(&rpid), op_data->op_namelen, op_data->op_name, rc);
296 GOTO(out_free_sop_data, rc);
300 * Caller may use attrs MDS returns on IT_OPEN lock request so, we have
301 * to update them for split dir.
303 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
304 LASSERT(body != NULL);
307 * Could not find object, FID is not present in response.
309 if (!(body->valid & OBD_MD_FLID))
310 GOTO(out_free_sop_data, rc = 0);
312 obj = lmv_object_find(obd, &body->fid1);
315 * XXX: Capability for remote call!
317 mea = lmv_get_mea(*reqp);
319 obj = lmv_object_create(exp, &body->fid1, mea);
321 GOTO(out_free_sop_data, rc = (int)PTR_ERR(obj));
327 * This is split dir and we'd want to get attrs.
329 CDEBUG(D_INODE, "Slave attributes for "DFID"\n",
332 rc = lmv_revalidate_slaves(exp, reqp, &body->fid1, it, 1,
333 cb_blocking, extra_lock_flags);
338 OBD_FREE_PTR(sop_data);
343 * Handler for: getattr, lookup and revalidate cases.
345 int lmv_intent_lookup(struct obd_export *exp, struct md_op_data *op_data,
346 void *lmm, int lmmsize, struct lookup_intent *it,
347 int flags, struct ptlrpc_request **reqp,
348 ldlm_blocking_callback cb_blocking,
349 int extra_lock_flags)
351 struct obd_device *obd = exp->exp_obd;
352 struct lu_fid rpid = op_data->op_fid1;
353 struct lmv_obd *lmv = &obd->u.lmv;
354 struct lmv_object *obj = NULL;
355 struct md_op_data *sop_data;
356 struct lmv_stripe_md *mea;
357 struct lmv_tgt_desc *tgt = NULL;
358 struct mdt_body *body;
364 OBD_ALLOC_PTR(sop_data);
365 if (sop_data == NULL)
368 *sop_data = *op_data;
374 obj = lmv_object_find(obd, &op_data->op_fid1);
375 if (obj && op_data->op_namelen) {
376 sidx = raw_name2idx(obj->lo_hashtype,
378 (char *)op_data->op_name,
379 op_data->op_namelen);
380 rpid = obj->lo_stripes[sidx].ls_fid;
381 tgt = lmv_get_target(lmv,
382 obj->lo_stripes[sidx].ls_mds);
384 "Choose slave dir ("DFID") -> mds #%d\n",
385 PFID(&rpid), tgt->ltd_idx);
386 sop_data->op_bias &= ~MDS_CHECK_SPLIT;
388 tgt = lmv_find_target(lmv, &op_data->op_fid1);
389 sop_data->op_bias |= MDS_CHECK_SPLIT;
395 GOTO(out_free_sop_data, rc = PTR_ERR(tgt));
397 if (!fid_is_sane(&sop_data->op_fid2))
398 fid_zero(&sop_data->op_fid2);
401 "LOOKUP_INTENT with fid1="DFID", fid2="DFID
402 ", name='%s' -> mds #%d\n",
403 PFID(&sop_data->op_fid1), PFID(&sop_data->op_fid2),
404 sop_data->op_name ? sop_data->op_name : "<NULL>",
407 sop_data->op_bias &= ~MDS_CROSS_REF;
408 sop_data->op_fid1 = rpid;
410 rc = md_intent_lock(tgt->ltd_exp, sop_data, lmm, lmmsize, it,
411 flags, reqp, cb_blocking, extra_lock_flags);
413 if (rc == -ERESTART) {
414 LASSERT(*reqp != NULL);
415 DEBUG_REQ(D_WARNING|D_RPCTRACE, *reqp,
416 "Got -ERESTART during lookup!\n");
417 ptlrpc_req_finished(*reqp);
419 it->d.lustre.it_data = 0;
422 * Directory got split since last update. This shouldn't be
423 * because splitting causes lock revocation, so revalidate had
424 * to fail and lookup on dir had to return mea.
426 LASSERT(obj == NULL);
428 obj = lmv_object_create(exp, &rpid, NULL);
430 GOTO(out_free_sop_data, rc = PTR_ERR(obj));
436 GOTO(out_free_sop_data, rc);
440 * This is split dir. In order to optimize things a bit, we
441 * consider obj valid updating missing parts.
444 "Revalidate slaves for "DFID", rc %d\n",
445 PFID(&op_data->op_fid1), rc);
447 LASSERT(fid_is_sane(&op_data->op_fid2));
448 rc = lmv_revalidate_slaves(exp, reqp, &op_data->op_fid1, it, rc,
449 cb_blocking, extra_lock_flags);
450 GOTO(out_free_sop_data, rc);
454 GOTO(out_free_sop_data, rc);
457 * MDS has returned success. Probably name has been resolved in
458 * remote inode. Let's check this.
460 rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags,
461 reqp, cb_blocking, extra_lock_flags);
463 GOTO(out_free_sop_data, rc);
466 * Nothing is found, do not access body->fid1 as it is zero and thus
469 if (it->d.lustre.it_disposition & DISP_LOOKUP_NEG)
470 GOTO(out_free_sop_data, rc = 0);
472 LASSERT(*reqp != NULL);
473 LASSERT((*reqp)->rq_repmsg != NULL);
474 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
475 LASSERT(body != NULL);
478 * Could not find object, FID is not present in response.
480 if (!(body->valid & OBD_MD_FLID))
481 GOTO(out_free_sop_data, rc = 0);
483 obj = lmv_object_find(obd, &body->fid1);
486 * XXX: Remote capability is not handled.
488 mea = lmv_get_mea(*reqp);
490 obj = lmv_object_create(exp, &body->fid1, mea);
492 GOTO(out_free_sop_data, rc = (int)PTR_ERR(obj));
495 CDEBUG(D_INODE, "Slave attributes for "DFID", rc %d\n",
496 PFID(&body->fid1), rc);
498 rc = lmv_revalidate_slaves(exp, reqp, &body->fid1, it, 1,
499 cb_blocking, extra_lock_flags);
505 OBD_FREE_PTR(sop_data);
509 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
510 void *lmm, int lmmsize, struct lookup_intent *it,
511 int flags, struct ptlrpc_request **reqp,
512 ldlm_blocking_callback cb_blocking,
513 int extra_lock_flags)
515 struct obd_device *obd = exp->exp_obd;
520 LASSERT(fid_is_sane(&op_data->op_fid1));
522 CDEBUG(D_INODE, "INTENT LOCK '%s' for '%*s' on "DFID"\n",
523 LL_IT2STR(it), op_data->op_namelen, op_data->op_name,
524 PFID(&op_data->op_fid1));
526 rc = lmv_check_connect(obd);
530 if (it->it_op & (IT_LOOKUP | IT_GETATTR))
531 rc = lmv_intent_lookup(exp, op_data, lmm, lmmsize, it,
532 flags, reqp, cb_blocking,
534 else if (it->it_op & IT_OPEN)
535 rc = lmv_intent_open(exp, op_data, lmm, lmmsize, it,
536 flags, reqp, cb_blocking,
543 int lmv_revalidate_slaves(struct obd_export *exp, struct ptlrpc_request **reqp,
544 const struct lu_fid *mid, struct lookup_intent *oit,
545 int master_valid, ldlm_blocking_callback cb_blocking,
546 int extra_lock_flags)
548 struct obd_device *obd = exp->exp_obd;
549 struct lmv_obd *lmv = &obd->u.lmv;
550 int master_lockm = 0;
551 struct lustre_handle *lockh = NULL;
552 struct ptlrpc_request *mreq = *reqp;
553 struct lustre_handle master_lockh = { 0 };
554 struct md_op_data *op_data;
555 struct ldlm_lock *lock;
556 unsigned long size = 0;
557 struct mdt_body *body;
558 struct lmv_object *obj;
562 struct ptlrpc_request *req;
563 ldlm_blocking_callback cb;
564 struct lookup_intent it;
565 struct lmv_tgt_desc *tgt;
569 CDEBUG(D_INODE, "Revalidate master obj "DFID"\n", PFID(mid));
571 OBD_ALLOC_PTR(op_data);
576 * We have to loop over the subobjects, check validity and update them
577 * from MDS if needed. It's very useful that we need not to update all
578 * the fields. Say, common fields (that are equal on all the subojects
579 * need not to be update, another fields (i_size, for example) are
580 * cached all the time.
582 obj = lmv_object_find_lock(obd, mid);
586 for (i = 0; i < obj->lo_objcount; i++) {
587 fid = obj->lo_stripes[i].ls_fid;
588 master = lu_fid_eq(&fid, &obj->lo_fid);
589 cb = master ? cb_blocking : lmv_blocking_ast;
592 * We need i_size and we would like to check possible cached locks,
593 * so this is is IT_GETATTR intent.
595 memset(&it, 0, sizeof(it));
596 it.it_op = IT_GETATTR;
598 if (master && master_valid) {
600 * lmv_intent_lookup() already checked
601 * validness and took the lock.
604 body = req_capsule_server_get(&mreq->rq_pill,
606 LASSERT(body != NULL);
610 * Take already cached attrs into account.
613 "Master "DFID"is locked and cached\n",
619 * Prepare op_data for revalidating. Note that @fid2 shuld be
620 * defined otherwise it will go to server and take new lock
621 * which is what we reall not need here.
623 memset(op_data, 0, sizeof(*op_data));
624 op_data->op_bias = MDS_CROSS_REF;
625 op_data->op_fid1 = fid;
626 op_data->op_fid2 = fid;
629 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
631 GOTO(cleanup, rc = PTR_ERR(tgt));
633 CDEBUG(D_INODE, "Revalidate slave obj "DFID" -> mds #%d\n",
634 PFID(&fid), tgt->ltd_idx);
636 rc = md_intent_lock(tgt->ltd_exp, op_data, NULL, 0, &it, 0,
637 &req, cb, extra_lock_flags);
639 lockh = (struct lustre_handle *)&it.d.lustre.it_lock_handle;
640 if (rc > 0 && req == NULL) {
642 * Nice, this slave is valid.
644 CDEBUG(D_INODE, "Cached slave "DFID"\n", PFID(&fid));
653 * Save lock on master to be returned to the caller.
655 CDEBUG(D_INODE, "No lock on master "DFID" yet\n",
657 memcpy(&master_lockh, lockh, sizeof(master_lockh));
658 master_lockm = it.d.lustre.it_lock_mode;
659 it.d.lustre.it_lock_mode = 0;
662 * This is slave. We want to control it.
664 lock = ldlm_handle2lock(lockh);
665 LASSERT(lock != NULL);
666 lock->l_ast_data = lmv_object_get(obj);
672 * This is first reply, we'll use it to return updated
673 * data back to the caller.
675 LASSERT(req != NULL);
676 ptlrpc_request_addref(req);
680 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
681 LASSERT(body != NULL);
684 obj->lo_stripes[i].ls_size = body->size;
686 CDEBUG(D_INODE, "Fresh size %lu from "DFID"\n",
687 (unsigned long)obj->lo_stripes[i].ls_size, PFID(&fid));
690 ptlrpc_req_finished(req);
692 size += obj->lo_stripes[i].ls_size;
694 if (it.d.lustre.it_lock_mode && lockh) {
695 ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
696 it.d.lustre.it_lock_mode = 0;
702 * Some attrs got refreshed, we have reply and it's time to put
705 CDEBUG(D_INODE, "Return refreshed attrs: size = %lu for "DFID"\n",
706 (unsigned long)size, PFID(mid));
708 body = req_capsule_server_get(&(*reqp)->rq_pill, &RMF_MDT_BODY);
709 LASSERT(body != NULL);
714 * Very important to maintain mds num the same because
715 * of revalidation. mreq == NULL means that caller has
716 * no reply and the only attr we can return is size.
718 body->valid = OBD_MD_FLSIZE;
720 if (master_valid == 0) {
721 oit->d.lustre.it_lock_handle = master_lockh.cookie;
722 oit->d.lustre.it_lock_mode = master_lockm;
727 * It seems all the attrs are fresh and we did no request.
729 CDEBUG(D_INODE, "All the attrs were fresh on "DFID"\n",
731 if (master_valid == 0)
732 oit->d.lustre.it_lock_mode = master_lockm;
738 OBD_FREE_PTR(op_data);
739 lmv_object_put_unlock(obj);
743 int lmv_allocate_slaves(struct obd_device *obd, struct lu_fid *pid,
744 struct md_op_data *op, struct lu_fid *fid)
746 struct lmv_obd *lmv = &obd->u.lmv;
747 struct lmv_object *obj;
753 obj = lmv_object_find(obd, pid);
757 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
758 (char *)op->op_name, op->op_namelen);
759 mds = obj->lo_stripes[sidx].ls_mds;
762 rc = __lmv_fid_alloc(lmv, fid, mds);
764 CERROR("Can't allocate fid, rc %d\n", rc);
768 CDEBUG(D_INODE, "Allocate new fid "DFID" for slave "
769 "obj -> mds #%x\n", PFID(fid), mds);