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>
35 #include <liblustre.h>
38 #include <linux/obd_support.h>
39 #include <linux/lustre_lib.h>
40 #include <linux/lustre_net.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/lustre_mds.h>
44 #include <linux/obd_class.h>
45 #include <linux/obd_ost.h>
46 #include <linux/lprocfs_status.h>
47 #include <linux/lustre_fsfilt.h>
48 #include <linux/obd_lmv.h>
49 #include <linux/namei.h>
50 #include <linux/lustre_lite.h>
51 #include "lmv_internal.h"
54 static inline void lmv_drop_intent_lock(struct lookup_intent *it)
56 if (LUSTRE_IT(it)->it_lock_mode != 0)
57 ldlm_lock_decref((void *)&LUSTRE_IT(it)->it_lock_handle,
58 LUSTRE_IT(it)->it_lock_mode);
61 int lmv_handle_remote_inode(struct obd_export *exp, void *lmm,
62 int lmmsize, struct lookup_intent *it,
63 int flags, struct ptlrpc_request **reqp,
64 ldlm_blocking_callback cb_blocking)
66 struct obd_device *obd = exp->exp_obd;
67 struct lmv_obd *lmv = &obd->u.lmv;
68 struct mds_body *body = NULL;
72 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
73 LASSERT(body != NULL);
75 if (body->valid & OBD_MD_MDS) {
77 * oh, MDS reports that this is remote inode case i.e. we have
78 * to ask for real attrs on another MDS.
80 struct ptlrpc_request *req = NULL;
81 struct lustre_handle plock;
85 if (it->it_op == IT_LOOKUP || it->it_op == IT_CHDIR) {
87 * unfortunately, we have to lie to MDC/MDS to retrieve
88 * attributes llite needs.
90 it->it_op = IT_GETATTR;
93 /* we got LOOKUP lock, but we really need attrs */
94 pmode = LUSTRE_IT(it)->it_lock_mode;
96 memcpy(&plock, &LUSTRE_IT(it)->it_lock_handle,
98 LUSTRE_IT(it)->it_lock_mode = 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, NULL,
106 0, lmm, lmmsize, NULL, it, flags, &req, cb_blocking);
109 * llite needs LOOKUP lock to track dentry revocation in order
110 * to maintain dcache consistency. Thus drop UPDATE lock here
111 * and put LOOKUP in request.
114 lmv_drop_intent_lock(it);
115 memcpy(&LUSTRE_IT(it)->it_lock_handle, &plock,
117 LUSTRE_IT(it)->it_lock_mode = pmode;
119 ldlm_lock_decref(&plock, pmode);
121 ptlrpc_req_finished(*reqp);
127 int lmv_intent_open(struct obd_export *exp, struct lustre_id *pid,
128 const char *name, int len, void *lmm, int lmmsize,
129 struct lustre_id *cid, struct lookup_intent *it,
130 int flags, struct ptlrpc_request **reqp,
131 ldlm_blocking_callback cb_blocking)
133 struct obd_device *obd = exp->exp_obd;
134 struct lmv_obd *lmv = &obd->u.lmv;
135 struct mds_body *body = NULL;
136 struct lustre_id rpid = *pid;
137 int rc, mds, loop = 0;
142 /* IT_OPEN is intended to open (and create, possible) an object. Parent
143 * (pid) may be splitted dir */
146 LASSERT(++loop <= 2);
147 mds = id_group(&rpid);
148 obj = lmv_grab_obj(obd, &rpid);
150 /* directory is already splitted, so we have to forward
151 * request to the right MDS */
152 mds = raw_name2idx(obj->hashtype, obj->objcount,
155 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n",
157 rpid = obj->objs[mds].id;
161 rc = md_intent_lock(lmv->tgts[id_group(&rpid)].ltd_exp, &rpid, name,
162 len, lmm, lmmsize, cid, it, flags, reqp, cb_blocking);
163 if (rc == -ERESTART) {
164 /* directory got splitted. time to update local object and
165 * repeat the request with proper MDS */
166 LASSERT(id_equal_fid(pid, &rpid));
167 rc = lmv_get_mea_and_update_object(exp, &rpid);
169 ptlrpc_req_finished(*reqp);
176 /* okay, MDS has returned success. Probably name has been resolved in
178 rc = lmv_handle_remote_inode(exp, lmm, lmmsize, it,
179 flags, reqp, cb_blocking);
184 * this is possible, that some userspace application will try to
185 * open file as directory and we will have error -20 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)
202 /* caller may use attrs MDS returns on IT_OPEN lock request so, we have
203 * to update them for splitted dir */
204 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
205 LASSERT(body != NULL);
206 LASSERT((body->valid & OBD_MD_FID) != 0);
209 obj = lmv_grab_obj(obd, cid);
210 if (!obj && (mea = lmv_splitted_dir_body(*reqp, 1))) {
211 /* wow! this is splitted dir, we'd like to handle it */
212 obj = lmv_create_obj(exp, &body->id1, mea);
214 RETURN(PTR_ERR(obj));
218 /* this is splitted dir and we'd want to get attrs */
219 CDEBUG(D_OTHER, "attrs from slaves for "DLID4"\n",
222 rc = lmv_revalidate_slaves(exp, reqp, cid, it, 1,
224 } else if (S_ISDIR(body->mode)) {
225 CDEBUG(D_OTHER, "object "DLID4" has not lmv obj?\n",
235 int lmv_intent_getattr(struct obd_export *exp, struct lustre_id *pid,
236 const char *name, int len, void *lmm, int lmmsize,
237 struct lustre_id *cid, struct lookup_intent *it,
238 int flags, struct ptlrpc_request **reqp,
239 ldlm_blocking_callback cb_blocking)
241 struct obd_device *obd = exp->exp_obd;
242 struct lmv_obd *lmv = &obd->u.lmv;
243 struct mds_body *body = NULL;
244 struct lustre_id rpid = *pid;
245 struct lmv_obj *obj = NULL, *obj2 = NULL;
251 /* caller wants to revalidate attrs of obj we have to revalidate
252 * slaves if requested object is splitted directory */
253 CDEBUG(D_OTHER, "revalidate attrs for "DLID4"\n", OLID4(cid));
256 obj = lmv_grab_obj(obd, cid);
258 /* in fact, we need not this with current intent_lock(),
259 * but it may change some day */
260 if (!id_equal_fid(pid, cid)){
261 rpid = obj->objs[mds].id;
262 mds = id_group(&rpid);
268 CDEBUG(D_OTHER, "INTENT getattr for %*s on "DLID4"\n",
269 len, name, OLID4(pid));
271 obj = lmv_grab_obj(obd, pid);
273 /* directory is already splitted. calculate mds */
274 mds = raw_name2idx(obj->hashtype, obj->objcount,
276 rpid = obj->objs[mds].id;
277 mds = id_group(&rpid);
280 CDEBUG(D_OTHER, "forward to MDS #%u (slave "DLID4")\n",
285 /* the same about fid returning. */
286 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, &rpid, name, len, lmm,
287 lmmsize, cid, it, flags, reqp, cb_blocking);
292 /* this is splitted dir. In order to optimize things a
293 * bit, we consider obj valid updating missing parts.
295 * FIXME: do we need to return any lock here? It would
296 * be fine if we don't. this means that nobody should
297 * use UPDATE lock to notify about object * removal */
299 "revalidate slaves for "DLID4", rc %d\n",
303 rc = lmv_revalidate_slaves(exp, reqp, cid, it, rc,
311 /* okay, MDS has returned success. probably name has been
312 * resolved in remote inode */
313 rc = lmv_handle_remote_inode(exp, lmm, lmmsize, it,
314 flags, reqp, cb_blocking);
319 * nothing is found, do not access body->id1 as it is zero and thus
322 if (LUSTRE_IT(it)->it_disposition & DISP_LOOKUP_NEG)
325 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
326 LASSERT(body != NULL);
327 LASSERT((body->valid & OBD_MD_FID) != 0);
330 obj2 = lmv_grab_obj(obd, cid);
332 if (!obj2 && (mea = lmv_splitted_dir_body(*reqp, 1))) {
333 /* wow! this is splitted dir, we'd like to handle it. */
334 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
335 LASSERT(body != NULL);
337 obj2 = lmv_create_obj(exp, &body->id1, mea);
339 RETURN(PTR_ERR(obj2));
343 /* this is splitted dir and we'd want to get attrs */
344 CDEBUG(D_OTHER, "attrs from slaves for "DLID4", rc %d\n",
347 rc = lmv_revalidate_slaves(exp, reqp, cid, it, 1,
354 void lmv_update_body_from_obj(struct mds_body *body, struct lmv_inode *obj)
357 body->size += obj->size;
360 int lmv_lookup_slaves(struct obd_export *exp, struct ptlrpc_request **reqp)
362 struct obd_device *obd = exp->exp_obd;
363 struct lmv_obd *lmv = &obd->u.lmv;
364 struct mds_body *body = NULL;
365 struct lustre_handle *lockh;
366 struct ldlm_lock *lock;
367 struct mds_body *body2;
375 /* master is locked. we'd like to take locks on slaves and update
376 * attributes to be returned from the slaves it's important that lookup
377 * is called in two cases:
379 * - for first time (dcache has no such a resolving yet).
380 * - ->d_revalidate() returned false.
382 * last case possible only if all the objs (master and all slaves aren't
385 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
386 LASSERT(body != NULL);
387 LASSERT((body->valid & OBD_MD_FID) != 0);
389 obj = lmv_grab_obj(obd, &body->id1);
390 LASSERT(obj != NULL);
392 CDEBUG(D_OTHER, "lookup slaves for "DLID4"\n",
397 for (i = 0; i < obj->objcount; i++) {
398 struct lustre_id id = obj->objs[i].id;
399 struct ptlrpc_request *req = NULL;
400 struct lookup_intent it;
402 if (id_equal_fid(&id, &obj->id))
403 /* skip master obj */
406 CDEBUG(D_OTHER, "lookup slave "DLID4"\n", OLID4(&id));
409 memset(&it, 0, sizeof(it));
410 it.it_op = IT_GETATTR;
411 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
413 rc = md_intent_lock(lmv->tgts[id_group(&id)].ltd_exp, &id,
414 NULL, 0, NULL, 0, &id, &it, 0, &req,
415 lmv_dirobj_blocking_ast);
417 lockh = (struct lustre_handle *)&LUSTRE_IT(&it)->it_lock_handle;
418 if (rc > 0 && req == NULL) {
419 /* nice, this slave is valid */
420 LASSERT(req == NULL);
421 CDEBUG(D_OTHER, "cached\n");
426 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
427 /* error during lookup */
430 lock = ldlm_handle2lock(lockh);
433 lock->l_ast_data = lmv_get_obj(obj);
435 body2 = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body2));
438 obj->objs[i].size = body2->size;
440 CDEBUG(D_OTHER, "fresh: %lu\n",
441 (unsigned long)obj->objs[i].size);
446 ptlrpc_req_finished(req);
448 lmv_update_body_from_obj(body, obj->objs + i);
450 if (LUSTRE_IT(&it)->it_lock_mode)
451 ldlm_lock_decref(lockh, LUSTRE_IT(&it)->it_lock_mode);
452 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
462 int lmv_intent_lookup(struct obd_export *exp, struct lustre_id *pid,
463 const char *name, int len, void *lmm, int lmmsize,
464 struct lustre_id *cid, struct lookup_intent *it,
465 int flags, struct ptlrpc_request **reqp,
466 ldlm_blocking_callback cb_blocking)
468 struct obd_device *obd = exp->exp_obd;
469 struct lmv_obd *lmv = &obd->u.lmv;
470 struct mds_body *body = NULL;
471 struct lustre_id rpid = *pid;
474 int rc, mds, loop = 0;
478 * IT_LOOKUP is intended to produce name -> id resolving (let's call
479 * this lookup below) or to confirm requested resolving is still valid
480 * (let's call this revalidation) cid != NULL specifies revalidation.
484 * this is revalidation: we have to check is LOOKUP lock still
485 * valid for given id. Very important part is that we have to
486 * choose right mds because namespace is per mds.
489 obj = lmv_grab_obj(obd, pid);
491 mds = raw_name2idx(obj->hashtype, obj->objcount,
493 rpid = obj->objs[mds].id;
496 mds = id_group(&rpid);
498 CDEBUG(D_OTHER, "revalidate lookup for "DLID4" to %d MDS\n",
504 LASSERT(++loop <= 2);
506 /* this is lookup. during lookup we have to update all the
507 * attributes, because returned values will be put in struct
510 obj = lmv_grab_obj(obd, pid);
513 /* directory is already splitted. calculate mds */
514 mds = raw_name2idx(obj->hashtype, obj->objcount,
516 rpid = obj->objs[mds].id;
517 mds = id_group(&rpid);
522 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, &rpid, name,
523 len, lmm, lmmsize, cid, it, flags,
530 /* very interesting. it seems object is still valid but for some
531 * reason llite calls lookup, not revalidate */
532 CDEBUG(D_OTHER, "lookup for "DLID4" and data should be uptodate\n",
534 LASSERT(*reqp == NULL);
538 if (rc == 0 && *reqp == NULL) {
539 /* once again, we're asked for lookup, not revalidate */
540 CDEBUG(D_OTHER, "lookup for "DLID4" and data should be uptodate\n",
545 if (rc == -ERESTART) {
546 /* directory got splitted since last update. this shouldn't be
547 * becasue splitting causes lock revocation, so revalidate had
548 * to fail and lookup on dir had to return mea */
549 CWARN("we haven't knew about directory splitting!\n");
550 LASSERT(obj == NULL);
552 obj = lmv_create_obj(exp, &rpid, NULL);
554 RETURN(PTR_ERR(obj));
562 /* okay, MDS has returned success. Probably name has been resolved in
564 rc = lmv_handle_remote_inode(exp, lmm, lmmsize, it,
565 flags, reqp, cb_blocking);
567 if (rc == 0 && (mea = lmv_splitted_dir_body(*reqp, 1))) {
568 /* wow! this is splitted dir, we'd like to handle it */
569 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
570 LASSERT(body != NULL);
571 LASSERT((body->valid & OBD_MD_FID) != 0);
573 obj = lmv_grab_obj(obd, &body->id1);
575 obj = lmv_create_obj(exp, &body->id1, mea);
577 RETURN(PTR_ERR(obj));
585 int lmv_intent_lock(struct obd_export *exp, struct lustre_id *pid,
586 const char *name, int len, void *lmm, int lmmsize,
587 struct lustre_id *cid, struct lookup_intent *it,
588 int flags, struct ptlrpc_request **reqp,
589 ldlm_blocking_callback cb_blocking)
591 struct obd_device *obd = exp->exp_obd;
598 CDEBUG(D_OTHER, "INTENT LOCK '%s' for '%*s' on %lu/%lu -> %lu\n",
599 LL_IT2STR(it), len, name, (unsigned long)id_ino(pid),
600 (unsigned long)id_gen(pid), (unsigned long)id_group(pid));
602 rc = lmv_check_connect(obd);
606 if (it->it_op == IT_LOOKUP)
607 rc = lmv_intent_lookup(exp, pid, name, len, lmm,
608 lmmsize, cid, it, flags, reqp,
610 else if (it->it_op & IT_OPEN)
611 rc = lmv_intent_open(exp, pid, name, len, lmm,
612 lmmsize, cid, it, flags, reqp,
614 else if (it->it_op == IT_GETATTR || it->it_op == IT_CHDIR)
615 rc = lmv_intent_getattr(exp, pid, name, len, lmm,
616 lmmsize, cid, it, flags, reqp,
623 int lmv_revalidate_slaves(struct obd_export *exp, struct ptlrpc_request **reqp,
624 struct lustre_id *mid, struct lookup_intent *oit,
625 int master_valid, ldlm_blocking_callback cb_blocking)
627 struct obd_device *obd = exp->exp_obd;
628 struct ptlrpc_request *mreq = *reqp;
629 struct lmv_obd *lmv = &obd->u.lmv;
630 struct lustre_handle master_lockh;
631 struct ldlm_lock *lock;
632 unsigned long size = 0;
633 struct mds_body *body;
635 int master_lock_mode;
639 /* we have to loop over the subobjects, check validity and update them
640 * from MDSs if needed. it's very useful that we need not to update all
641 * the fields. say, common fields (that are equal on all the subojects
642 * need not to be update, another fields (i_size, for example) are
643 * cached all the time */
644 obj = lmv_grab_obj(obd, mid);
645 LASSERT(obj != NULL);
647 master_lock_mode = 0;
651 for (i = 0; i < obj->objcount; i++) {
652 struct lustre_id id = obj->objs[i].id;
653 struct lustre_handle *lockh = NULL;
654 struct ptlrpc_request *req = NULL;
655 ldlm_blocking_callback cb;
656 struct lookup_intent it;
659 CDEBUG(D_OTHER, "revalidate subobj "DLID4"\n",
662 memset(&it, 0, sizeof(it));
663 it.it_op = IT_GETATTR;
665 cb = lmv_dirobj_blocking_ast;
667 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
668 if (id_equal_fid(&id, &obj->id)) {
670 /* lmv_intent_getattr() already checked
671 * validness and took the lock */
673 /* it even got the reply refresh attrs
675 body = lustre_msg_buf(mreq->rq_repmsg,
677 LASSERT(body != NULL);
680 /* take already cached attrs into account */
682 "master is locked and cached\n");
691 rc = md_intent_lock(lmv->tgts[id_group(&id)].ltd_exp,
692 &id, NULL, 0, NULL, 0, &id, &it, 0,
694 lockh = (struct lustre_handle *) &LUSTRE_IT(&it)->it_lock_handle;
695 if (rc > 0 && req == NULL) {
696 /* nice, this slave is valid */
697 LASSERT(req == NULL);
698 CDEBUG(D_OTHER, "cached\n");
703 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
704 /* error during revalidation */
708 LASSERT(master_valid == 0);
709 /* save lock on master to be returned to the caller */
710 CDEBUG(D_OTHER, "no lock on master yet\n");
711 memcpy(&master_lockh, lockh, sizeof(master_lockh));
712 master_lock_mode = LUSTRE_IT(&it)->it_lock_mode;
713 LUSTRE_IT(&it)->it_lock_mode = 0;
715 /* this is slave. we want to control it */
716 lock = ldlm_handle2lock(lockh);
718 lock->l_ast_data = lmv_get_obj(obj);
723 /* this is first reply, we'll use it to return updated
724 * data back to the caller */
726 ptlrpc_request_addref(req);
731 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
735 obj->objs[i].size = body->size;
737 CDEBUG(D_OTHER, "fresh: %lu\n",
738 (unsigned long)obj->objs[i].size);
741 ptlrpc_req_finished(req);
743 size += obj->objs[i].size;
745 if (LUSTRE_IT(&it)->it_lock_mode)
746 ldlm_lock_decref(lockh, LUSTRE_IT(&it)->it_lock_mode);
747 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
751 /* some attrs got refreshed, we have reply and it's time to put
752 * fresh attrs to it */
753 CDEBUG(D_OTHER, "return refreshed attrs: size = %lu\n",
754 (unsigned long)size);
756 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
759 /* FIXME: what about other attributes? */
763 /* very important to maintain id_group(lli->lli_id) the
764 * same because of revalidation. mreq == NULL means that
765 * caller has no reply and the only attr we can return
767 body->valid = OBD_MD_FLSIZE;
768 // body->mds = id_group(&obj->id);
770 if (master_valid == 0) {
771 memcpy(&LUSTRE_IT(oit)->it_lock_handle,
772 &master_lockh, sizeof(master_lockh));
773 LUSTRE_IT(oit)->it_lock_mode = master_lock_mode;
777 /* it seems all the attrs are fresh and we did no request */
778 CDEBUG(D_OTHER, "all the attrs were fresh\n");
779 if (master_valid == 0)
780 LUSTRE_IT(oit)->it_lock_mode = master_lock_mode;