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_handle_remote_inode(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 mds_body *body = NULL;
71 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
72 LASSERT(body != NULL);
74 if (body->valid & OBD_MD_MDS) {
76 * oh, MDS reports that this is remote inode case i.e. we have
77 * to ask for real attrs on another MDS.
79 struct ptlrpc_request *req = NULL;
80 struct lustre_handle plock;
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;
100 LASSERT((body->valid & OBD_MD_FID) != 0);
103 LUSTRE_IT(it)->it_disposition &= ~DISP_ENQ_COMPLETE;
104 rc = md_intent_lock(lmv->tgts[id_group(&nid)].ltd_exp, &nid, NULL,
105 0, lmm, lmmsize, NULL, it, flags, &req, cb_blocking);
108 * llite needs LOOKUP lock to track dentry revocation in order
109 * to maintain dcache consistency. Thus drop UPDATE lock here
110 * and put LOOKUP in request.
113 lmv_drop_intent_lock(it);
114 memcpy(&LUSTRE_IT(it)->it_lock_handle, &plock,
116 LUSTRE_IT(it)->it_lock_mode = pmode;
118 ldlm_lock_decref(&plock, pmode);
120 ptlrpc_req_finished(*reqp);
126 int lmv_intent_open(struct obd_export *exp, struct lustre_id *pid,
127 const char *name, int len, void *lmm, int lmmsize,
128 struct lustre_id *cid, struct lookup_intent *it,
129 int flags, struct ptlrpc_request **reqp,
130 ldlm_blocking_callback cb_blocking)
132 struct obd_device *obd = exp->exp_obd;
133 struct lmv_obd *lmv = &obd->u.lmv;
134 struct mds_body *body = NULL;
135 struct lustre_id rpid = *pid;
136 int rc, mds, loop = 0;
141 /* IT_OPEN is intended to open (and create, possible) an object. Parent
142 * (pid) may be splitted dir */
145 LASSERT(++loop <= 2);
146 mds = id_group(&rpid);
147 obj = lmv_grab_obj(obd, &rpid);
149 /* directory is already splitted, so we have to forward
150 * request to the right MDS */
151 mds = raw_name2idx(obj->hashtype, obj->objcount,
154 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n",
156 rpid = obj->objs[mds].id;
160 rc = md_intent_lock(lmv->tgts[id_group(&rpid)].ltd_exp, &rpid, name,
161 len, lmm, lmmsize, cid, it, flags, reqp, cb_blocking);
162 if (rc == -ERESTART) {
163 /* directory got splitted. time to update local object and
164 * repeat the request with proper MDS */
165 LASSERT(id_equal_fid(pid, &rpid));
166 rc = lmv_get_mea_and_update_object(exp, &rpid);
168 ptlrpc_req_finished(*reqp);
175 /* okay, MDS has returned success. Probably name has been resolved in
177 rc = lmv_handle_remote_inode(exp, lmm, lmmsize, it,
178 flags, reqp, cb_blocking);
183 * this is possible, that some userspace application will try to
184 * open file as directory and we will have error -20 here. As
185 * this is "usual" situation, we should not print error here,
188 CDEBUG(D_OTHER, "can't handle remote %s: dir "DLID4"("DLID4"):"
189 "%*s: %d\n", LL_IT2STR(it), OLID4(pid), OLID4(&rpid),
195 * nothing is found, do not access body->id1 as it is zero and thus
198 if (LUSTRE_IT(it)->it_disposition & DISP_LOOKUP_NEG)
201 /* caller may use attrs MDS returns on IT_OPEN lock request so, we have
202 * to update them for splitted dir */
203 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
204 LASSERT(body != NULL);
205 LASSERT((body->valid & OBD_MD_FID) != 0);
208 obj = lmv_grab_obj(obd, cid);
209 if (!obj && (mea = lmv_splitted_dir_body(*reqp, 1))) {
210 /* wow! this is splitted dir, we'd like to handle it */
211 obj = lmv_create_obj(exp, &body->id1, mea);
213 RETURN(PTR_ERR(obj));
217 /* this is splitted dir and we'd want to get attrs */
218 CDEBUG(D_OTHER, "attrs from slaves for "DLID4"\n",
221 rc = lmv_revalidate_slaves(exp, reqp, cid, it, 1,
223 } else if (S_ISDIR(body->mode)) {
224 CDEBUG(D_OTHER, "object "DLID4" has not lmv obj?\n",
234 int lmv_intent_getattr(struct obd_export *exp, struct lustre_id *pid,
235 const char *name, int len, void *lmm, int lmmsize,
236 struct lustre_id *cid, struct lookup_intent *it,
237 int flags, struct ptlrpc_request **reqp,
238 ldlm_blocking_callback cb_blocking)
240 struct obd_device *obd = exp->exp_obd;
241 struct lmv_obd *lmv = &obd->u.lmv;
242 struct mds_body *body = NULL;
243 struct lustre_id rpid = *pid;
244 struct lmv_obj *obj = NULL, *obj2 = NULL;
250 /* caller wants to revalidate attrs of obj we have to revalidate
251 * slaves if requested object is splitted directory */
252 CDEBUG(D_OTHER, "revalidate attrs for "DLID4"\n", OLID4(cid));
255 obj = lmv_grab_obj(obd, cid);
257 /* in fact, we need not this with current intent_lock(),
258 * but it may change some day */
259 if (!id_equal_fid(pid, cid)){
260 rpid = obj->objs[mds].id;
261 mds = id_group(&rpid);
267 CDEBUG(D_OTHER, "INTENT getattr for %*s on "DLID4"\n",
268 len, name, OLID4(pid));
270 obj = lmv_grab_obj(obd, pid);
272 /* directory is already splitted. calculate mds */
273 mds = raw_name2idx(obj->hashtype, obj->objcount,
275 rpid = obj->objs[mds].id;
276 mds = id_group(&rpid);
279 CDEBUG(D_OTHER, "forward to MDS #%u (slave "DLID4")\n",
284 /* the same about fid returning. */
285 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, &rpid, name, len, lmm,
286 lmmsize, cid, it, flags, reqp, cb_blocking);
291 /* this is splitted dir. In order to optimize things a
292 * bit, we consider obj valid updating missing parts.
294 * FIXME: do we need to return any lock here? It would
295 * be fine if we don't. this means that nobody should
296 * use UPDATE lock to notify about object * removal */
298 "revalidate slaves for "DLID4", rc %d\n",
302 rc = lmv_revalidate_slaves(exp, reqp, cid, it, rc,
310 /* okay, MDS has returned success. probably name has been
311 * resolved in remote inode */
312 rc = lmv_handle_remote_inode(exp, lmm, lmmsize, it,
313 flags, reqp, cb_blocking);
318 * nothing is found, do not access body->id1 as it is zero and thus
321 if (LUSTRE_IT(it)->it_disposition & DISP_LOOKUP_NEG)
324 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
325 LASSERT(body != NULL);
326 LASSERT((body->valid & OBD_MD_FID) != 0);
329 obj2 = lmv_grab_obj(obd, cid);
331 if (!obj2 && (mea = lmv_splitted_dir_body(*reqp, 1))) {
332 /* wow! this is splitted dir, we'd like to handle it. */
333 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
334 LASSERT(body != NULL);
336 obj2 = lmv_create_obj(exp, &body->id1, mea);
338 RETURN(PTR_ERR(obj2));
342 /* this is splitted dir and we'd want to get attrs */
343 CDEBUG(D_OTHER, "attrs from slaves for "DLID4", rc %d\n",
346 rc = lmv_revalidate_slaves(exp, reqp, cid, it, 1,
353 void lmv_update_body_from_obj(struct mds_body *body, struct lmv_inode *obj)
356 body->size += obj->size;
359 int lmv_lookup_slaves(struct obd_export *exp, struct ptlrpc_request **reqp)
361 struct obd_device *obd = exp->exp_obd;
362 struct lmv_obd *lmv = &obd->u.lmv;
363 struct mds_body *body = NULL;
364 struct lustre_handle *lockh;
365 struct ldlm_lock *lock;
366 struct mds_body *body2;
374 /* master is locked. we'd like to take locks on slaves and update
375 * attributes to be returned from the slaves it's important that lookup
376 * is called in two cases:
378 * - for first time (dcache has no such a resolving yet).
379 * - ->d_revalidate() returned false.
381 * last case possible only if all the objs (master and all slaves aren't
384 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
385 LASSERT(body != NULL);
386 LASSERT((body->valid & OBD_MD_FID) != 0);
388 obj = lmv_grab_obj(obd, &body->id1);
389 LASSERT(obj != NULL);
391 CDEBUG(D_OTHER, "lookup slaves for "DLID4"\n",
396 for (i = 0; i < obj->objcount; i++) {
397 struct lustre_id id = obj->objs[i].id;
398 struct ptlrpc_request *req = NULL;
399 struct lookup_intent it;
401 if (id_equal_fid(&id, &obj->id))
402 /* skip master obj */
405 CDEBUG(D_OTHER, "lookup slave "DLID4"\n", OLID4(&id));
408 memset(&it, 0, sizeof(it));
409 it.it_op = IT_GETATTR;
410 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
412 rc = md_intent_lock(lmv->tgts[id_group(&id)].ltd_exp, &id,
413 NULL, 0, NULL, 0, &id, &it, 0, &req,
414 lmv_dirobj_blocking_ast);
416 lockh = (struct lustre_handle *)&LUSTRE_IT(&it)->it_lock_handle;
417 if (rc > 0 && req == NULL) {
418 /* nice, this slave is valid */
419 LASSERT(req == NULL);
420 CDEBUG(D_OTHER, "cached\n");
425 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
426 /* error during lookup */
429 lock = ldlm_handle2lock(lockh);
432 lock->l_ast_data = lmv_get_obj(obj);
434 body2 = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body2));
437 obj->objs[i].size = body2->size;
439 CDEBUG(D_OTHER, "fresh: %lu\n",
440 (unsigned long)obj->objs[i].size);
445 ptlrpc_req_finished(req);
447 lmv_update_body_from_obj(body, obj->objs + i);
449 if (LUSTRE_IT(&it)->it_lock_mode)
450 ldlm_lock_decref(lockh, LUSTRE_IT(&it)->it_lock_mode);
451 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
461 int lmv_intent_lookup(struct obd_export *exp, struct lustre_id *pid,
462 const char *name, int len, void *lmm, int lmmsize,
463 struct lustre_id *cid, struct lookup_intent *it,
464 int flags, struct ptlrpc_request **reqp,
465 ldlm_blocking_callback cb_blocking)
467 struct obd_device *obd = exp->exp_obd;
468 struct lmv_obd *lmv = &obd->u.lmv;
469 struct mds_body *body = NULL;
470 struct lustre_id rpid = *pid;
473 int rc, mds, loop = 0;
477 * IT_LOOKUP is intended to produce name -> id resolving (let's call
478 * this lookup below) or to confirm requested resolving is still valid
479 * (let's call this revalidation) cid != NULL specifies revalidation.
483 * this is revalidation: we have to check is LOOKUP lock still
484 * valid for given id. Very important part is that we have to
485 * choose right mds because namespace is per mds.
488 obj = lmv_grab_obj(obd, pid);
490 mds = raw_name2idx(obj->hashtype, obj->objcount,
492 rpid = obj->objs[mds].id;
495 mds = id_group(&rpid);
497 CDEBUG(D_OTHER, "revalidate lookup for "DLID4" to %d MDS\n",
503 LASSERT(++loop <= 2);
505 /* this is lookup. during lookup we have to update all the
506 * attributes, because returned values will be put in struct
509 obj = lmv_grab_obj(obd, pid);
512 /* directory is already splitted. calculate mds */
513 mds = raw_name2idx(obj->hashtype, obj->objcount,
515 rpid = obj->objs[mds].id;
516 mds = id_group(&rpid);
521 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, &rpid, name,
522 len, lmm, lmmsize, cid, it, flags,
529 /* very interesting. it seems object is still valid but for some
530 * reason llite calls lookup, not revalidate */
531 CDEBUG(D_OTHER, "lookup for "DLID4" and data should be uptodate\n",
533 LASSERT(*reqp == NULL);
537 if (rc == 0 && *reqp == NULL) {
538 /* once again, we're asked for lookup, not revalidate */
539 CDEBUG(D_OTHER, "lookup for "DLID4" and data should be uptodate\n",
544 if (rc == -ERESTART) {
545 /* directory got splitted since last update. this shouldn't be
546 * becasue splitting causes lock revocation, so revalidate had
547 * to fail and lookup on dir had to return mea */
548 CWARN("we haven't knew about directory splitting!\n");
549 LASSERT(obj == NULL);
551 obj = lmv_create_obj(exp, &rpid, NULL);
553 RETURN(PTR_ERR(obj));
561 /* okay, MDS has returned success. Probably name has been resolved in
563 rc = lmv_handle_remote_inode(exp, lmm, lmmsize, it,
564 flags, reqp, cb_blocking);
566 if (rc == 0 && (mea = lmv_splitted_dir_body(*reqp, 1))) {
567 /* wow! this is splitted dir, we'd like to handle it */
568 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
569 LASSERT(body != NULL);
570 LASSERT((body->valid & OBD_MD_FID) != 0);
572 obj = lmv_grab_obj(obd, &body->id1);
574 obj = lmv_create_obj(exp, &body->id1, mea);
576 RETURN(PTR_ERR(obj));
584 int lmv_intent_lock(struct obd_export *exp, struct lustre_id *pid,
585 const char *name, int len, void *lmm, int lmmsize,
586 struct lustre_id *cid, struct lookup_intent *it,
587 int flags, struct ptlrpc_request **reqp,
588 ldlm_blocking_callback cb_blocking)
590 struct obd_device *obd = exp->exp_obd;
597 CDEBUG(D_OTHER, "INTENT LOCK '%s' for '%*s' on %lu/%lu -> %lu\n",
598 LL_IT2STR(it), len, name, (unsigned long)id_ino(pid),
599 (unsigned long)id_gen(pid), (unsigned long)id_group(pid));
601 rc = lmv_check_connect(obd);
605 if (it->it_op == IT_LOOKUP)
606 rc = lmv_intent_lookup(exp, pid, name, len, lmm,
607 lmmsize, cid, it, flags, reqp,
609 else if (it->it_op & IT_OPEN)
610 rc = lmv_intent_open(exp, pid, name, len, lmm,
611 lmmsize, cid, it, flags, reqp,
613 else if (it->it_op == IT_GETATTR || it->it_op == IT_CHDIR)
614 rc = lmv_intent_getattr(exp, pid, name, len, lmm,
615 lmmsize, cid, it, flags, reqp,
622 int lmv_revalidate_slaves(struct obd_export *exp, struct ptlrpc_request **reqp,
623 struct lustre_id *mid, struct lookup_intent *oit,
624 int master_valid, ldlm_blocking_callback cb_blocking)
626 struct obd_device *obd = exp->exp_obd;
627 struct ptlrpc_request *mreq = *reqp;
628 struct lmv_obd *lmv = &obd->u.lmv;
629 struct lustre_handle master_lockh;
630 struct ldlm_lock *lock;
631 unsigned long size = 0;
632 struct mds_body *body;
634 int master_lock_mode;
638 /* we have to loop over the subobjects, check validity and update them
639 * from MDSs if needed. it's very useful that we need not to update all
640 * the fields. say, common fields (that are equal on all the subojects
641 * need not to be update, another fields (i_size, for example) are
642 * cached all the time */
643 obj = lmv_grab_obj(obd, mid);
644 LASSERT(obj != NULL);
646 master_lock_mode = 0;
650 for (i = 0; i < obj->objcount; i++) {
651 struct lustre_id id = obj->objs[i].id;
652 struct lustre_handle *lockh = NULL;
653 struct ptlrpc_request *req = NULL;
654 ldlm_blocking_callback cb;
655 struct lookup_intent it;
658 CDEBUG(D_OTHER, "revalidate subobj "DLID4"\n",
661 memset(&it, 0, sizeof(it));
662 it.it_op = IT_GETATTR;
664 cb = lmv_dirobj_blocking_ast;
666 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
667 if (id_equal_fid(&id, &obj->id)) {
669 /* lmv_intent_getattr() already checked
670 * validness and took the lock */
672 /* it even got the reply refresh attrs
674 body = lustre_msg_buf(mreq->rq_repmsg,
676 LASSERT(body != NULL);
679 /* take already cached attrs into account */
681 "master is locked and cached\n");
690 rc = md_intent_lock(lmv->tgts[id_group(&id)].ltd_exp,
691 &id, NULL, 0, NULL, 0, &id, &it, 0,
693 lockh = (struct lustre_handle *) &LUSTRE_IT(&it)->it_lock_handle;
694 if (rc > 0 && req == NULL) {
695 /* nice, this slave is valid */
696 LASSERT(req == NULL);
697 CDEBUG(D_OTHER, "cached\n");
702 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
703 /* error during revalidation */
707 LASSERT(master_valid == 0);
708 /* save lock on master to be returned to the caller */
709 CDEBUG(D_OTHER, "no lock on master yet\n");
710 memcpy(&master_lockh, lockh, sizeof(master_lockh));
711 master_lock_mode = LUSTRE_IT(&it)->it_lock_mode;
712 LUSTRE_IT(&it)->it_lock_mode = 0;
714 /* this is slave. we want to control it */
715 lock = ldlm_handle2lock(lockh);
717 lock->l_ast_data = lmv_get_obj(obj);
722 /* this is first reply, we'll use it to return updated
723 * data back to the caller */
725 ptlrpc_request_addref(req);
730 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
734 obj->objs[i].size = body->size;
736 CDEBUG(D_OTHER, "fresh: %lu\n",
737 (unsigned long)obj->objs[i].size);
740 ptlrpc_req_finished(req);
742 size += obj->objs[i].size;
744 if (LUSTRE_IT(&it)->it_lock_mode)
745 ldlm_lock_decref(lockh, LUSTRE_IT(&it)->it_lock_mode);
746 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
750 /* some attrs got refreshed, we have reply and it's time to put
751 * fresh attrs to it */
752 CDEBUG(D_OTHER, "return refreshed attrs: size = %lu\n",
753 (unsigned long)size);
755 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
758 /* FIXME: what about other attributes? */
762 /* very important to maintain id_group(lli->lli_id) the
763 * same because of revalidation. mreq == NULL means that
764 * caller has no reply and the only attr we can return
766 body->valid = OBD_MD_FLSIZE;
767 // body->mds = id_group(&obj->id);
769 if (master_valid == 0) {
770 memcpy(&LUSTRE_IT(oit)->it_lock_handle,
771 &master_lockh, sizeof(master_lockh));
772 LUSTRE_IT(oit)->it_lock_mode = master_lock_mode;
776 /* it seems all the attrs are fresh and we did no request */
777 CDEBUG(D_OTHER, "all the attrs were fresh\n");
778 if (master_valid == 0)
779 LUSTRE_IT(oit)->it_lock_mode = master_lock_mode;