1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LMV
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <asm/div64.h>
33 #include <linux/seq_file.h>
34 #include <linux/namei.h>
36 #include <liblustre.h>
39 #include <linux/obd_support.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_net.h>
42 #include <linux/lustre_idl.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_mds.h>
45 #include <linux/obd_class.h>
46 #include <linux/obd_ost.h>
47 #include <linux/lprocfs_status.h>
48 #include <linux/lustre_fsfilt.h>
49 #include <linux/obd_lmv.h>
50 #include <linux/lustre_lite.h>
51 #include "lmv_internal.h"
53 static inline void lmv_drop_intent_lock(struct lookup_intent *it)
55 if (LUSTRE_IT(it)->it_lock_mode != 0)
56 ldlm_lock_decref((void *)&LUSTRE_IT(it)->it_lock_handle,
57 LUSTRE_IT(it)->it_lock_mode);
60 int lmv_intent_remote(struct obd_export *exp, void *lmm,
61 int lmmsize, struct lookup_intent *it,
62 int flags, struct ptlrpc_request **reqp,
63 ldlm_blocking_callback cb_blocking)
65 struct obd_device *obd = exp->exp_obd;
66 struct lmv_obd *lmv = &obd->u.lmv;
67 struct ptlrpc_request *req = NULL;
68 struct mds_body *body = NULL;
69 struct lustre_handle plock;
74 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
75 LASSERT(body != NULL);
77 if (!(body->valid & OBD_MD_MDS))
81 * oh, MDS reports that this is remote inode case i.e. we have to ask
82 * for real attrs on another MDS.
84 if (it->it_op == IT_LOOKUP || it->it_op == IT_CHDIR) {
86 * unfortunately, we have to lie to MDC/MDS to retrieve
87 * attributes llite needs.
89 it->it_op = IT_GETATTR;
92 /* we got LOOKUP lock, but we really need attrs */
93 pmode = LUSTRE_IT(it)->it_lock_mode;
95 memcpy(&plock, &LUSTRE_IT(it)->it_lock_handle,
97 LUSTRE_IT(it)->it_lock_mode = 0;
98 LUSTRE_IT(it)->it_data = 0;
101 LASSERT((body->valid & OBD_MD_FID) != 0);
104 LUSTRE_IT(it)->it_disposition &= ~DISP_ENQ_COMPLETE;
105 rc = md_intent_lock(lmv->tgts[id_group(&nid)].ltd_exp, &nid,
106 NULL, 0, lmm, lmmsize, NULL, it, flags,
110 * llite needs LOOKUP lock to track dentry revocation in order to
111 * maintain dcache consistency. Thus drop UPDATE lock here and put
115 lmv_drop_intent_lock(it);
116 memcpy(&LUSTRE_IT(it)->it_lock_handle, &plock,
118 LUSTRE_IT(it)->it_lock_mode = pmode;
120 ldlm_lock_decref(&plock, pmode);
123 ptlrpc_req_finished(*reqp);
128 int lmv_intent_open(struct obd_export *exp, struct lustre_id *pid,
129 const char *name, int len, void *lmm, int lmmsize,
130 struct lustre_id *cid, struct lookup_intent *it,
131 int flags, struct ptlrpc_request **reqp,
132 ldlm_blocking_callback cb_blocking)
134 struct obd_device *obd = exp->exp_obd;
135 struct lmv_obd *lmv = &obd->u.lmv;
136 struct mds_body *body = NULL;
137 struct lustre_id rpid = *pid;
138 int rc, mds, loop = 0;
143 /* IT_OPEN is intended to open (and create, possible) an object. Parent
144 * (pid) may be splitted dir */
147 LASSERT(++loop <= 2);
148 mds = id_group(&rpid);
149 obj = lmv_grab_obj(obd, &rpid);
151 /* directory is already splitted, so we have to forward
152 * request to the right MDS */
153 mds = raw_name2idx(obj->hashtype, obj->objcount,
156 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n",
158 rpid = obj->objs[mds].id;
162 rc = md_intent_lock(lmv->tgts[id_group(&rpid)].ltd_exp, &rpid, name,
163 len, lmm, lmmsize, cid, it, flags, reqp, cb_blocking);
164 if (rc == -ERESTART) {
165 /* directory got splitted. time to update local object and
166 * repeat the request with proper MDS */
167 LASSERT(id_equal_fid(pid, &rpid));
168 rc = lmv_get_mea_and_update_object(exp, &rpid);
170 ptlrpc_req_finished(*reqp);
177 /* okay, MDS has returned success. Probably name has been resolved in
179 rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags, reqp, cb_blocking);
184 * this is possible, that some userspace application will try to
185 * open file as directory and we will have -ENOTDIR here. As
186 * this is "usual" situation, we should not print error here,
189 CDEBUG(D_OTHER, "can't handle remote %s: dir "DLID4"("DLID4"):"
190 "%*s: %d\n", LL_IT2STR(it), OLID4(pid), OLID4(&rpid),
196 * nothing is found, do not access body->id1 as it is zero and thus
199 if ((LUSTRE_IT(it)->it_disposition & DISP_LOOKUP_NEG) &&
200 !(LUSTRE_IT(it)->it_disposition & DISP_OPEN_CREATE) &&
201 !(LUSTRE_IT(it)->it_disposition & DISP_OPEN_OPEN))
204 /* caller may use attrs MDS returns on IT_OPEN lock request so, we have
205 * to update them for splitted dir */
206 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
207 LASSERT(body != NULL);
209 /* could not find object, FID is not present in response. */
210 if (!(body->valid & OBD_MD_FID))
214 obj = lmv_grab_obj(obd, cid);
215 if (!obj && (mea = lmv_splitted_dir_body(*reqp, 1))) {
216 /* wow! this is splitted dir, we'd like to handle it */
217 obj = lmv_create_obj(exp, &body->id1, mea);
219 RETURN(PTR_ERR(obj));
223 /* this is splitted dir and we'd want to get attrs */
224 CDEBUG(D_OTHER, "attrs from slaves for "DLID4"\n",
227 rc = lmv_revalidate_slaves(exp, reqp, cid, it, 1,
229 } else if (S_ISDIR(body->mode)) {
230 CDEBUG(D_OTHER, "object "DLID4" has not lmv obj?\n",
240 int lmv_intent_getattr(struct obd_export *exp, struct lustre_id *pid,
241 const char *name, int len, void *lmm, int lmmsize,
242 struct lustre_id *cid, struct lookup_intent *it,
243 int flags, struct ptlrpc_request **reqp,
244 ldlm_blocking_callback cb_blocking)
246 struct obd_device *obd = exp->exp_obd;
247 struct lmv_obd *lmv = &obd->u.lmv;
248 struct mds_body *body = NULL;
249 struct lustre_id rpid = *pid;
250 struct lmv_obj *obj = NULL, *obj2 = NULL;
256 /* caller wants to revalidate attrs of obj we have to revalidate
257 * slaves if requested object is splitted directory */
258 CDEBUG(D_OTHER, "revalidate attrs for "DLID4"\n", OLID4(cid));
261 obj = lmv_grab_obj(obd, cid);
263 /* in fact, we need not this with current intent_lock(),
264 * but it may change some day */
265 if (!id_equal_fid(pid, cid)){
266 rpid = obj->objs[mds].id;
267 mds = id_group(&rpid);
273 CDEBUG(D_OTHER, "INTENT getattr for %*s on "DLID4"\n",
274 len, name, OLID4(pid));
276 obj = lmv_grab_obj(obd, pid);
278 /* directory is already splitted. calculate mds */
279 mds = raw_name2idx(obj->hashtype, obj->objcount,
281 rpid = obj->objs[mds].id;
282 mds = id_group(&rpid);
285 CDEBUG(D_OTHER, "forward to MDS #%u (slave "DLID4")\n",
290 /* the same about fid returning. */
291 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, &rpid, name, len, lmm,
292 lmmsize, cid, it, flags, reqp, cb_blocking);
297 /* this is splitted dir. In order to optimize things a
298 * bit, we consider obj valid updating missing parts.
300 * FIXME: do we need to return any lock here? It would
301 * be fine if we don't. this means that nobody should
302 * use UPDATE lock to notify about object * removal */
304 "revalidate slaves for "DLID4", rc %d\n",
308 rc = lmv_revalidate_slaves(exp, reqp, cid, it, rc,
316 /* okay, MDS has returned success. probably name has been
317 * resolved in remote inode */
318 rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags,
324 * nothing is found, do not access body->id1 as it is zero and thus
327 if (LUSTRE_IT(it)->it_disposition & DISP_LOOKUP_NEG)
330 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
331 LASSERT(body != NULL);
333 /* could not find object, FID is not present in response. */
334 if (!(body->valid & OBD_MD_FID))
338 obj2 = lmv_grab_obj(obd, cid);
340 if (!obj2 && (mea = lmv_splitted_dir_body(*reqp, 1))) {
341 /* wow! this is splitted dir, we'd like to handle it. */
342 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
343 LASSERT(body != NULL);
345 obj2 = lmv_create_obj(exp, &body->id1, mea);
347 RETURN(PTR_ERR(obj2));
351 /* this is splitted dir and we'd want to get attrs */
352 CDEBUG(D_OTHER, "attrs from slaves for "DLID4", rc %d\n",
355 rc = lmv_revalidate_slaves(exp, reqp, cid, it, 1,
362 void lmv_update_body_from_obj(struct mds_body *body, struct lmv_inode *obj)
365 body->size += obj->size;
368 int lmv_lookup_slaves(struct obd_export *exp, struct ptlrpc_request **reqp)
370 struct obd_device *obd = exp->exp_obd;
371 struct lmv_obd *lmv = &obd->u.lmv;
372 struct mds_body *body = NULL;
373 struct lustre_handle *lockh;
374 struct ldlm_lock *lock;
375 struct mds_body *body2;
383 /* master is locked. we'd like to take locks on slaves and update
384 * attributes to be returned from the slaves it's important that lookup
385 * is called in two cases:
387 * - for first time (dcache has no such a resolving yet).
388 * - ->d_revalidate() returned false.
390 * last case possible only if all the objs (master and all slaves aren't
393 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
394 LASSERT(body != NULL);
395 LASSERT((body->valid & OBD_MD_FID) != 0);
397 obj = lmv_grab_obj(obd, &body->id1);
398 LASSERT(obj != NULL);
400 CDEBUG(D_OTHER, "lookup slaves for "DLID4"\n",
405 for (i = 0; i < obj->objcount; i++) {
406 struct lustre_id id = obj->objs[i].id;
407 struct ptlrpc_request *req = NULL;
408 struct lookup_intent it;
410 if (id_equal_fid(&id, &obj->id))
411 /* skip master obj */
414 CDEBUG(D_OTHER, "lookup slave "DLID4"\n", OLID4(&id));
417 memset(&it, 0, sizeof(it));
418 it.it_op = IT_GETATTR;
419 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
421 rc = md_intent_lock(lmv->tgts[id_group(&id)].ltd_exp, &id,
422 NULL, 0, NULL, 0, &id, &it, 0, &req,
423 lmv_dirobj_blocking_ast);
425 lockh = (struct lustre_handle *)&LUSTRE_IT(&it)->it_lock_handle;
426 if (rc > 0 && req == NULL) {
427 /* nice, this slave is valid */
428 LASSERT(req == NULL);
429 CDEBUG(D_OTHER, "cached\n");
434 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
435 /* error during lookup */
438 lock = ldlm_handle2lock(lockh);
441 lock->l_ast_data = lmv_get_obj(obj);
443 body2 = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body2));
446 obj->objs[i].size = body2->size;
448 CDEBUG(D_OTHER, "fresh: %lu\n",
449 (unsigned long)obj->objs[i].size);
454 ptlrpc_req_finished(req);
456 lmv_update_body_from_obj(body, obj->objs + i);
458 if (LUSTRE_IT(&it)->it_lock_mode)
459 ldlm_lock_decref(lockh, LUSTRE_IT(&it)->it_lock_mode);
460 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
470 int lmv_intent_lookup(struct obd_export *exp, struct lustre_id *pid,
471 const char *name, int len, void *lmm, int lmmsize,
472 struct lustre_id *cid, struct lookup_intent *it,
473 int flags, struct ptlrpc_request **reqp,
474 ldlm_blocking_callback cb_blocking)
476 struct obd_device *obd = exp->exp_obd;
477 struct lmv_obd *lmv = &obd->u.lmv;
478 struct mds_body *body = NULL;
479 struct lustre_id rpid = *pid;
482 int rc, mds, loop = 0;
486 * IT_LOOKUP is intended to produce name -> id resolving (let's call
487 * this lookup below) or to confirm requested resolving is still valid
488 * (let's call this revalidation) cid != NULL specifies revalidation.
492 * this is revalidation: we have to check is LOOKUP lock still
493 * valid for given id. Very important part is that we have to
494 * choose right mds because namespace is per mds.
497 obj = lmv_grab_obj(obd, pid);
499 mds = raw_name2idx(obj->hashtype, obj->objcount,
501 rpid = obj->objs[mds].id;
504 mds = id_group(&rpid);
506 CDEBUG(D_OTHER, "revalidate lookup for "DLID4" to %d MDS\n",
512 LASSERT(++loop <= 2);
514 /* this is lookup. during lookup we have to update all the
515 * attributes, because returned values will be put in struct
518 obj = lmv_grab_obj(obd, pid);
521 /* directory is already splitted. calculate mds */
522 mds = raw_name2idx(obj->hashtype, obj->objcount,
524 rpid = obj->objs[mds].id;
525 mds = id_group(&rpid);
530 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, &rpid, name,
531 len, lmm, lmmsize, cid, it, flags,
538 /* very interesting. it seems object is still valid but for some
539 * reason llite calls lookup, not revalidate */
540 CDEBUG(D_OTHER, "lookup for "DLID4" and data should be uptodate\n",
542 LASSERT(*reqp == NULL);
546 if (rc == 0 && *reqp == NULL) {
547 /* once again, we're asked for lookup, not revalidate */
548 CDEBUG(D_OTHER, "lookup for "DLID4" and data should be uptodate\n",
553 if (rc == -ERESTART) {
554 /* directory got splitted since last update. this shouldn't be
555 * becasue splitting causes lock revocation, so revalidate had
556 * to fail and lookup on dir had to return mea */
557 CWARN("we haven't knew about directory splitting!\n");
558 LASSERT(obj == NULL);
560 obj = lmv_create_obj(exp, &rpid, NULL);
562 RETURN(PTR_ERR(obj));
570 /* okay, MDS has returned success. Probably name has been resolved in
572 rc = lmv_intent_remote(exp, lmm, lmmsize, it, flags, reqp, cb_blocking);
574 if (rc == 0 && (mea = lmv_splitted_dir_body(*reqp, 1))) {
575 /* wow! this is splitted dir, we'd like to handle it */
576 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
577 LASSERT(body != NULL);
578 LASSERT((body->valid & OBD_MD_FID) != 0);
580 obj = lmv_grab_obj(obd, &body->id1);
582 obj = lmv_create_obj(exp, &body->id1, mea);
584 RETURN(PTR_ERR(obj));
592 int lmv_intent_lock(struct obd_export *exp, struct lustre_id *pid,
593 const char *name, int len, void *lmm, int lmmsize,
594 struct lustre_id *cid, struct lookup_intent *it,
595 int flags, struct ptlrpc_request **reqp,
596 ldlm_blocking_callback cb_blocking)
598 struct obd_device *obd = exp->exp_obd;
605 CDEBUG(D_OTHER, "INTENT LOCK '%s' for '%*s' on %lu/%lu -> %lu\n",
606 LL_IT2STR(it), len, name, (unsigned long)id_ino(pid),
607 (unsigned long)id_gen(pid), (unsigned long)id_group(pid));
609 rc = lmv_check_connect(obd);
613 if (it->it_op == IT_LOOKUP)
614 rc = lmv_intent_lookup(exp, pid, name, len, lmm,
615 lmmsize, cid, it, flags, reqp,
617 else if (it->it_op & IT_OPEN)
618 rc = lmv_intent_open(exp, pid, name, len, lmm,
619 lmmsize, cid, it, flags, reqp,
621 else if (it->it_op == IT_GETATTR || it->it_op == IT_CHDIR)
622 rc = lmv_intent_getattr(exp, pid, name, len, lmm,
623 lmmsize, cid, it, flags, reqp,
630 int lmv_revalidate_slaves(struct obd_export *exp, struct ptlrpc_request **reqp,
631 struct lustre_id *mid, struct lookup_intent *oit,
632 int master_valid, ldlm_blocking_callback cb_blocking)
634 struct obd_device *obd = exp->exp_obd;
635 struct ptlrpc_request *mreq = *reqp;
636 struct lmv_obd *lmv = &obd->u.lmv;
637 struct lustre_handle master_lockh;
638 struct ldlm_lock *lock;
639 unsigned long size = 0;
640 struct mds_body *body;
642 int master_lock_mode;
646 /* we have to loop over the subobjects, check validity and update them
647 * from MDSs if needed. it's very useful that we need not to update all
648 * the fields. say, common fields (that are equal on all the subojects
649 * need not to be update, another fields (i_size, for example) are
650 * cached all the time */
651 obj = lmv_grab_obj(obd, mid);
652 LASSERT(obj != NULL);
654 master_lock_mode = 0;
658 for (i = 0; i < obj->objcount; i++) {
659 struct lustre_id id = obj->objs[i].id;
660 struct lustre_handle *lockh = NULL;
661 struct ptlrpc_request *req = NULL;
662 ldlm_blocking_callback cb;
663 struct lookup_intent it;
666 CDEBUG(D_OTHER, "revalidate subobj "DLID4"\n",
669 memset(&it, 0, sizeof(it));
670 it.it_op = IT_GETATTR;
672 cb = lmv_dirobj_blocking_ast;
674 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
675 if (id_equal_fid(&id, &obj->id)) {
677 /* lmv_intent_getattr() already checked
678 * validness and took the lock */
680 /* it even got the reply refresh attrs
682 body = lustre_msg_buf(mreq->rq_repmsg,
684 LASSERT(body != NULL);
687 /* take already cached attrs into account */
689 "master is locked and cached\n");
698 rc = md_intent_lock(lmv->tgts[id_group(&id)].ltd_exp,
699 &id, NULL, 0, NULL, 0, &id, &it, 0,
701 lockh = (struct lustre_handle *) &LUSTRE_IT(&it)->it_lock_handle;
702 if (rc > 0 && req == NULL) {
703 /* nice, this slave is valid */
704 LASSERT(req == NULL);
705 CDEBUG(D_OTHER, "cached\n");
710 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
711 /* error during revalidation */
715 LASSERT(master_valid == 0);
716 /* save lock on master to be returned to the caller */
717 CDEBUG(D_OTHER, "no lock on master yet\n");
718 memcpy(&master_lockh, lockh, sizeof(master_lockh));
719 master_lock_mode = LUSTRE_IT(&it)->it_lock_mode;
720 LUSTRE_IT(&it)->it_lock_mode = 0;
722 /* this is slave. we want to control it */
723 lock = ldlm_handle2lock(lockh);
725 lock->l_ast_data = lmv_get_obj(obj);
730 /* this is first reply, we'll use it to return updated
731 * data back to the caller */
733 ptlrpc_request_addref(req);
738 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
742 obj->objs[i].size = body->size;
744 CDEBUG(D_OTHER, "fresh: %lu\n",
745 (unsigned long)obj->objs[i].size);
748 ptlrpc_req_finished(req);
750 size += obj->objs[i].size;
752 if (LUSTRE_IT(&it)->it_lock_mode)
753 ldlm_lock_decref(lockh, LUSTRE_IT(&it)->it_lock_mode);
754 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
758 /* some attrs got refreshed, we have reply and it's time to put
759 * fresh attrs to it */
760 CDEBUG(D_OTHER, "return refreshed attrs: size = %lu\n",
761 (unsigned long)size);
763 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
766 /* FIXME: what about other attributes? */
770 /* very important to maintain id_group(lli->lli_id) the
771 * same because of revalidation. mreq == NULL means that
772 * caller has no reply and the only attr we can return
774 body->valid = OBD_MD_FLSIZE;
775 // body->mds = id_group(&obj->id);
777 if (master_valid == 0) {
778 memcpy(&LUSTRE_IT(oit)->it_lock_handle,
779 &master_lockh, sizeof(master_lockh));
780 LUSTRE_IT(oit)->it_lock_mode = master_lock_mode;
784 /* it seems all the attrs are fresh and we did no request */
785 CDEBUG(D_OTHER, "all the attrs were fresh\n");
786 if (master_valid == 0)
787 LUSTRE_IT(oit)->it_lock_mode = master_lock_mode;