Whamcloud - gitweb
- more debug messages for #46 failure
[fs/lustre-release.git] / lustre / lmv / lmv_intent.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_LMV
26 #ifdef __KERNEL__
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 #else
35 #include <liblustre.h>
36 #endif
37
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 "lmv_internal.h"
50
51
52 static inline void lmv_drop_intent_lock(struct lookup_intent *it)
53 {
54         if (it->d.lustre.it_lock_mode != 0)
55                 ldlm_lock_decref((void *)&it->d.lustre.it_lock_handle,
56                                  it->d.lustre.it_lock_mode);
57 }
58
59 int lmv_handle_remote_inode(struct obd_export *exp, struct ll_uctxt *uctxt,
60                             void *lmm, int lmmsize, 
61                             struct lookup_intent *it, int flags,
62                             struct ptlrpc_request **reqp,
63                             ldlm_blocking_callback cb_blocking)
64 {
65         struct obd_device *obd = exp->exp_obd;
66         struct lmv_obd *lmv = &obd->u.lmv;
67         struct mds_body *body = NULL;
68         int rc = 0;
69         ENTRY;
70
71         body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
72         LASSERT(body != NULL);
73
74         if (body->valid & OBD_MD_MDS) {
75                 /* oh, MDS reports that this is remote inode case
76                  * i.e. we have to ask for real attrs on another MDS */
77                 struct ptlrpc_request *req;
78                 struct ll_fid nfid;
79                 struct lustre_handle plock;
80                 int pmode;
81
82                 if (it->it_op == IT_LOOKUP) {
83                         /* unfortunately, we have to lie to MDC/MDS to
84                          * retrieve attributes llite needs */
85                         it->it_op = IT_GETATTR;
86                 }
87
88                 /* we got LOOKUP lock, but we really need attrs */
89                 pmode = it->d.lustre.it_lock_mode;
90                 if (pmode) {
91                         memcpy(&plock, &it->d.lustre.it_lock_handle,
92                                         sizeof(plock));
93                         it->d.lustre.it_lock_mode = 0;
94                 }
95
96                 nfid = body->fid1;
97                 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
98                 rc = md_intent_lock(lmv->tgts[nfid.mds].ltd_exp, uctxt, &nfid,
99                                     NULL, 0, lmm, lmmsize, NULL, it, flags,
100                                     &req, cb_blocking);
101
102                 /* llite needs LOOKUP lock to track dentry revocation in 
103                  * order to maintain dcache consistency. thus drop UPDATE
104                  * lock here and put LOOKUP in request */
105                 if (rc == 0) {
106                         lmv_drop_intent_lock(it);
107                         memcpy(&it->d.lustre.it_lock_handle, &plock,
108                                         sizeof(plock));
109                         it->d.lustre.it_lock_mode = pmode;
110                         
111                 } else if (pmode)
112                         ldlm_lock_decref(&plock, pmode);
113
114                 ptlrpc_req_finished(*reqp);
115                 *reqp = req;
116         }
117         RETURN(rc);
118 }
119
120 int lmv_intent_open(struct obd_export *exp, struct ll_uctxt *uctxt,
121                     struct ll_fid *pfid, const char *name, int len,
122                     void *lmm, int lmmsize, struct ll_fid *cfid,
123                     struct lookup_intent *it, int flags,
124                     struct ptlrpc_request **reqp,
125                     ldlm_blocking_callback cb_blocking)
126 {
127         struct obd_device *obd = exp->exp_obd;
128         struct lmv_obd *lmv = &obd->u.lmv;
129         struct mds_body *body = NULL;
130         struct ll_fid rpfid = *pfid;
131         struct lmv_obj *obj;
132         struct mea *mea;
133         int rc, mds;
134         ENTRY;
135
136         /* IT_OPEN is intended to open (and create, possible) an object. Parent
137          * (pfid) may be splitted dir */
138
139 repeat:
140         mds = rpfid.mds;
141         obj = lmv_grab_obj(obd, &rpfid);
142         if (obj) {
143                 /* directory is already splitted, so we have to forward
144                  * request to the right MDS */
145                 mds = raw_name2idx(obj->objcount, (char *)name, len);
146                 CDEBUG(D_OTHER, "forward to MDS #%u\n", mds);
147                 rpfid = obj->objs[mds].fid;
148                 lmv_put_obj(obj);
149         }
150
151         rc = md_intent_lock(lmv->tgts[mds].ltd_exp, uctxt, &rpfid, name,
152                             len, lmm, lmmsize, cfid, it, flags, reqp,
153                             cb_blocking);
154         if (rc == -ERESTART) {
155                 /* directory got splitted. time to update local object
156                  * and repeat the request with proper MDS */
157                 LASSERT(fid_equal(pfid, &rpfid));
158                 rc = lmv_get_mea_and_update_object(exp, &rpfid);
159                 if (rc == 0) {
160                         ptlrpc_req_finished(*reqp);
161                         goto repeat;
162                 }
163         }
164         if (rc != 0)
165                 RETURN(rc);
166
167         /* okay, MDS has returned success. Probably name has been resolved in
168          * remote inode */
169         rc = lmv_handle_remote_inode(exp, uctxt, lmm, lmmsize, it,
170                                      flags, reqp, cb_blocking);
171         if (rc != 0) {
172                 LASSERT(rc < 0);
173                 CERROR("can't handle remote %s: dir %lu/%lu/%lu(%lu/%lu/%lu):"
174                        "%*s: %d\n", LL_IT2STR(it),
175                        (unsigned long) pfid->mds,
176                        (unsigned long) pfid->id,
177                        (unsigned long) pfid->generation,
178                        (unsigned long) rpfid.mds,
179                        (unsigned long) rpfid.id,
180                        (unsigned long) rpfid.generation,
181                        len, name, rc);
182                 RETURN(rc);
183         }
184
185         /* caller may use attrs MDS returns on IT_OPEN lock request so, we have
186          * to update them for splitted dir */
187         body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
188         LASSERT(body != NULL);
189
190         cfid = &body->fid1;
191         obj = lmv_grab_obj(obd, cfid);
192         if (!obj && (mea = body_of_splitted_dir(*reqp, 1))) {
193                 /* wow! this is splitted dir, we'd like to handle it */
194                 obj = lmv_create_obj(exp, &body->fid1, mea);
195                 if (IS_ERR(obj))
196                         RETURN(PTR_ERR(obj));
197         }
198
199         if (obj) {
200                 /* this is splitted dir and we'd want to get attrs */
201                 CDEBUG(D_OTHER, "attrs from slaves for %lu/%lu/%lu\n",
202                        (unsigned long)cfid->mds, (unsigned long)cfid->id,
203                        (unsigned long)cfid->generation);
204                 rc = lmv_revalidate_slaves(exp, reqp, cfid,
205                                            it, 1, cb_blocking);
206         } else if (S_ISDIR(body->mode)) {
207                 /*CWARN("hmmm, %lu/%lu/%lu has not lmv obj?!\n",
208                                 (unsigned long) cfid->mds,
209                                 (unsigned long) cfid->id,
210                                 (unsigned long) cfid->generation);*/
211         }
212         
213         if (obj)
214                 lmv_put_obj(obj);
215         
216         RETURN(rc);
217 }
218
219 int lmv_intent_getattr(struct obd_export *exp, struct ll_uctxt *uctxt,
220                        struct ll_fid *pfid, const char *name, int len,
221                        void *lmm, int lmmsize, struct ll_fid *cfid,
222                        struct lookup_intent *it, int flags,
223                        struct ptlrpc_request **reqp,
224                        ldlm_blocking_callback cb_blocking)
225 {
226         struct obd_device *obd = exp->exp_obd;
227         struct lmv_obd *lmv = &obd->u.lmv;
228         struct mds_body *body = NULL;
229         struct ll_fid rpfid = *pfid;
230         struct lmv_obj *obj, *obj2;
231         struct mea *mea;
232         int rc = 0, mds;
233         ENTRY;
234
235         if (cfid) {
236                 /* caller wants to revalidate attrs of obj we have to revalidate
237                  * slaves if requested object is splitted directory */
238                 CDEBUG(D_OTHER, "revalidate attrs for %lu/%lu/%lu\n",
239                        (unsigned long)cfid->mds, (unsigned long)cfid->id,
240                        (unsigned long)cfid->generation);
241                 mds = cfid->mds;
242                 obj = lmv_grab_obj(obd, cfid);
243                 if (obj) {
244                         /* in fact, we need not this with current intent_lock(),
245                          * but it may change some day */
246                         rpfid = obj->objs[mds].fid;
247                         lmv_put_obj(obj);
248                 }
249                 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, uctxt, &rpfid, name,
250                                     len, lmm, lmmsize, cfid, it, flags, reqp,
251                                     cb_blocking);
252                 if (obj && rc >= 0) {
253                         /* this is splitted dir. In order to optimize things a
254                          * bit, we consider obj valid updating missing parts.
255
256                          * FIXME: do we need to return any lock here? It would
257                          * be fine if we don't. this means that nobody should
258                          * use UPDATE lock to notify about object * removal */
259                         CDEBUG(D_OTHER,
260                                "revalidate slaves for %lu/%lu/%lu, rc %d\n",
261                                (unsigned long)cfid->mds, (unsigned long)cfid->id,
262                                (unsigned long)cfid->generation, rc);
263                         
264                         rc = lmv_revalidate_slaves(exp, reqp, cfid, it, rc,
265                                                    cb_blocking);
266                 }
267
268                 RETURN(rc);                
269         }
270
271         CDEBUG(D_OTHER, "INTENT getattr for %*s on %lu/%lu/%lu\n",
272                len, name, (unsigned long)pfid->mds, (unsigned long)pfid->id,
273                (unsigned long)pfid->generation);
274
275         mds = pfid->mds;
276         obj = lmv_grab_obj(obd, pfid);
277         if (obj && len) {
278                 /* directory is already splitted. calculate mds */
279                 mds = raw_name2idx(obj->objcount, (char *) name, len);
280                 rpfid = obj->objs[mds].fid;
281                 lmv_put_obj(obj);
282                 
283                 CDEBUG(D_OTHER, "forward to MDS #%u (slave %lu/%lu/%lu)\n",
284                        mds, (unsigned long)rpfid.mds, (unsigned long)rpfid.id,
285                        (unsigned long)rpfid.generation);
286         }
287         
288         rc = md_intent_lock(lmv->tgts[mds].ltd_exp, uctxt, &rpfid, name,
289                             len, lmm, lmmsize, NULL, it, flags, reqp,
290                             cb_blocking);
291         
292         if (rc < 0)
293                 RETURN(rc);
294         
295         LASSERT(rc == 0);
296
297         /* okay, MDS has returned success. probably name has been
298          * resolved in remote inode */
299         rc = lmv_handle_remote_inode(exp, uctxt, lmm, lmmsize, it,
300                                      flags, reqp, cb_blocking);
301         if (rc < 0)
302                 RETURN(rc);
303
304         body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
305         LASSERT(body != NULL);
306         
307         cfid = &body->fid1;
308         obj2 = lmv_grab_obj(obd, cfid);
309
310         if (!obj2 && (mea = body_of_splitted_dir(*reqp, 1))) {
311                 /* wow! this is splitted dir, we'd like to handle it. */
312                 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
313                 LASSERT(body != NULL);
314
315                 obj2 = lmv_create_obj(exp, &body->fid1, mea);
316                 if (IS_ERR(obj2))
317                         RETURN(PTR_ERR(obj2));
318         }
319
320         if (obj2) {
321                 /* this is splitted dir and we'd want to get attrs */
322                 CDEBUG(D_OTHER, "attrs from slaves for %lu/%lu/%lu, rc %d\n",
323                        (unsigned long)cfid->mds, (unsigned long)cfid->id,
324                        (unsigned long)cfid->generation, rc);
325                 
326                 rc = lmv_revalidate_slaves(exp, reqp, cfid, it, 1, cb_blocking);
327                 lmv_put_obj(obj2);
328         }
329         RETURN(rc);
330 }
331
332 void lmv_update_body_from_obj(struct mds_body *body, struct lmv_inode *obj)
333 {
334         /* update size */
335         body->size += obj->size;
336 /*        body->atime = obj->atime;
337         body->ctime = obj->ctime;
338         body->mtime = obj->mtime;
339         body->nlink = obj->nlink;*/
340 }
341
342 int lmv_lookup_slaves(struct obd_export *exp, struct ptlrpc_request **reqp)
343 {
344         struct obd_device *obd = exp->exp_obd;
345         struct lmv_obd *lmv = &obd->u.lmv;
346         struct mds_body *body = NULL;
347         struct lustre_handle *lockh;
348         struct ldlm_lock *lock;
349         struct mds_body *body2;
350         struct ll_uctxt uctxt;
351         struct lmv_obj *obj;
352         int i, rc = 0;
353         ENTRY;
354
355         LASSERT(reqp);
356         LASSERT(*reqp);
357
358         /* master is locked. we'd like to take locks on slaves and update
359          * attributes to be returned from the slaves it's important that lookup
360          * is called in two cases:
361          
362          *  - for first time (dcache has no such a resolving yet).
363          *  - ->d_revalidate() returned false.
364          
365          * last case possible only if all the objs (master and all slaves aren't
366          * valid */
367
368         body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
369         LASSERT(body != NULL);
370
371         obj = lmv_grab_obj(obd, &body->fid1);
372         LASSERT(obj != NULL);
373
374         CDEBUG(D_OTHER, "lookup slaves for %lu/%lu/%lu\n",
375                (unsigned long)body->fid1.mds,
376                (unsigned long)body->fid1.id,
377                (unsigned long)body->fid1.generation);
378
379         uctxt.gid1 = 0;
380         uctxt.gid2 = 0;
381
382         lmv_lock_obj(obj);
383         
384         for (i = 0; i < obj->objcount; i++) {
385                 struct ll_fid fid = obj->objs[i].fid;
386                 struct ptlrpc_request *req = NULL;
387                 struct lookup_intent it;
388
389                 if (fid_equal(&fid, &obj->fid))
390                         /* skip master obj */
391                         continue;
392
393                 CDEBUG(D_OTHER, "lookup slave %lu/%lu/%lu\n",
394                        (unsigned long)fid.mds, (unsigned long)fid.id,
395                        (unsigned long)fid.generation);
396
397                 /* is obj valid? */
398                 memset(&it, 0, sizeof(it));
399                 it.it_op = IT_GETATTR;
400                 rc = md_intent_lock(lmv->tgts[fid.mds].ltd_exp, &uctxt, &fid,
401                                     NULL, 0, NULL, 0, &fid, &it, 0, &req,
402                                     lmv_dirobj_blocking_ast);
403                 
404                 lockh = (struct lustre_handle *)&it.d.lustre.it_lock_handle;
405                 if (rc > 0) {
406                         /* nice, this slave is valid */
407                         LASSERT(req == NULL);
408                         CDEBUG(D_OTHER, "cached\n");
409                         goto release_lock;
410                 }
411
412                 if (rc < 0)
413                         /* error during revalidation */
414                         GOTO(cleanup, rc);
415
416                 /* rc == 0, this means we have no such a lock and can't think
417                  * obj is still valid. lookup it again */
418                 LASSERT(req == NULL);
419                 req = NULL;
420
421                 memset(&it, 0, sizeof(it));
422                 it.it_op = IT_GETATTR;
423                 rc = md_intent_lock(lmv->tgts[fid.mds].ltd_exp, &uctxt, &fid,
424                                     NULL, 0, NULL, 0, NULL, &it, 0, &req,
425                                     lmv_dirobj_blocking_ast);
426
427                 lockh = (struct lustre_handle *) &it.d.lustre.it_lock_handle;
428                 LASSERT(rc <= 0);
429
430                 if (rc < 0)
431                         /* error during lookup */
432                         GOTO(cleanup, rc);
433                 
434                 lock = ldlm_handle2lock(lockh);
435                 LASSERT(lock);
436
437                 lock->l_ast_data = lmv_get_obj(obj);
438
439                 body2 = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body2));
440                 LASSERT(body2);
441
442                 obj->objs[i].size = body2->size;
443                 
444                 CDEBUG(D_OTHER, "fresh: %lu\n",
445                        (unsigned long)obj->objs[i].size);
446
447                 LDLM_LOCK_PUT(lock);
448
449                 if (req)
450                         ptlrpc_req_finished(req);
451 release_lock:
452                 lmv_update_body_from_obj(body, obj->objs + i);
453
454                 if (it.d.lustre.it_lock_mode)
455                         ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
456         }
457 cleanup:
458         lmv_unlock_obj(obj);
459         lmv_put_obj(obj);
460         RETURN(rc);
461 }
462
463 int lmv_intent_lookup(struct obd_export *exp, struct ll_uctxt *uctxt,
464                       struct ll_fid *pfid, const char *name, int len,
465                       void *lmm, int lmmsize, struct ll_fid *cfid,
466                       struct lookup_intent *it, int flags,
467                       struct ptlrpc_request **reqp,
468                       ldlm_blocking_callback cb_blocking)
469 {
470         struct obd_device *obd = exp->exp_obd;
471         struct lmv_obd *lmv = &obd->u.lmv;
472         struct mds_body *body = NULL;
473         struct ll_fid rpfid = *pfid;
474         struct lmv_obj *obj;
475         struct mea *mea;
476         int rc, mds;
477         ENTRY;
478
479         /* IT_LOOKUP is intended to produce name -> fid resolving (let's call
480          * this lookup below) or to confirm requested resolving is still valid
481          * (let's call this revalidation) cfid != NULL specifies revalidation */
482
483         if (cfid) {
484                 /* this is revalidation: we have to check is LOOKUP lock still
485                  * valid for given fid. very important part is that we have to
486                  * choose right mds because namespace is per mds */
487                 rpfid = *pfid;
488                 obj = lmv_grab_obj(obd, pfid);
489                 if (obj) {
490                         mds = raw_name2idx(obj->objcount, (char *) name, len);
491                         rpfid = obj->objs[mds].fid;
492                         lmv_put_obj(obj);
493                 }
494                 mds = rpfid.mds;
495                 
496                 CDEBUG(D_OTHER, "revalidate lookup for %lu/%lu/%lu to %d MDS\n",
497                        (unsigned long)cfid->mds, (unsigned long)cfid->id,
498                        (unsigned long)cfid->generation, mds);
499                 
500                 rc = md_intent_lock(lmv->tgts[mds].ltd_exp, uctxt, pfid, name,
501                                     len, lmm, lmmsize, cfid, it, flags,
502                                     reqp, cb_blocking);
503                 RETURN(rc);
504         }
505
506         mds = pfid->mds;
507 repeat:
508         /* this is lookup. during lookup we have to update all the attributes,
509          * because returned values will be put in struct inode */
510
511         obj = lmv_grab_obj(obd, pfid);
512         if (obj) {
513                 if (len) {
514                         /* directory is already splitted. calculate mds */
515                         mds = raw_name2idx(obj->objcount, (char *)name, len);
516                         rpfid = obj->objs[mds].fid;
517                 }
518                 lmv_put_obj(obj);
519         }
520
521         rc = md_intent_lock(lmv->tgts[mds].ltd_exp, uctxt, &rpfid, name,
522                             len, lmm, lmmsize, NULL, it, flags, reqp,
523                             cb_blocking);
524         if (rc > 0) {
525                 /* very interesting. it seems object is still valid but for some
526                  * reason llite calls lookup, not revalidate */
527                 CWARN("lookup for %lu/%lu/%lu and data should be uptodate\n",
528                       (unsigned long)rpfid.mds, (unsigned long)rpfid.id,
529                       (unsigned long)rpfid.generation);
530                 LASSERT(*reqp == NULL);
531                 RETURN(rc);
532         }
533
534         if (rc == 0 && *reqp == NULL) {
535                 /* once again, we're asked for lookup, not revalidate */
536                 CWARN("lookup for %lu/%lu/%lu and data should be uptodate\n",
537                        (unsigned long)rpfid.mds, (unsigned long)rpfid.id,
538                        (unsigned long)rpfid.generation);
539                 RETURN(rc);
540         }
541        
542         if (rc == -ERESTART) {
543                 /* directory got splitted since last update. this shouldn't be
544                  * becasue splitting causes lock revocation, so revalidate had
545                  * to fail and lookup on dir had to return mea */
546                 CWARN("we haven't knew about directory splitting!\n");
547                 LASSERT(obj == NULL);
548
549                 obj = lmv_create_obj(exp, &rpfid, NULL);
550                 if (IS_ERR(obj))
551                         RETURN(PTR_ERR(obj));
552                 lmv_put_obj(obj);
553                 goto repeat;
554         }
555
556         if (rc < 0)
557                 RETURN(rc);
558
559         /* okay, MDS has returned success. probably name has been resolved in
560          * remote inode */
561         rc = lmv_handle_remote_inode(exp, uctxt, lmm, lmmsize, it, flags,
562                                      reqp, cb_blocking);
563
564         if (rc == 0 && (mea = body_of_splitted_dir(*reqp, 1))) {
565                 /* wow! this is splitted dir, we'd like to handle it */
566                 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
567                 LASSERT(body != NULL);
568                 
569                 obj = lmv_grab_obj(obd, &body->fid1);
570                 if (!obj) {
571                         obj = lmv_create_obj(exp, &body->fid1, mea);
572                         if (IS_ERR(obj))
573                                 RETURN(PTR_ERR(obj));
574                 }
575                 lmv_put_obj(obj);
576         }
577
578         RETURN(rc);
579 }
580
581 int lmv_intent_lock(struct obd_export *exp, struct ll_uctxt *uctxt,
582                     struct ll_fid *pfid, const char *name, int len,
583                     void *lmm, int lmmsize, struct ll_fid *cfid,
584                     struct lookup_intent *it, int flags,
585                     struct ptlrpc_request **reqp,
586                     ldlm_blocking_callback cb_blocking)
587 {
588         struct obd_device *obd = exp->exp_obd;
589         int rc = 0;
590         ENTRY;
591
592         LASSERT(it);
593         LASSERT(pfid);
594
595         CDEBUG(D_OTHER, "INTENT LOCK '%s' for '%*s' on %lu/%lu -> %u\n",
596                         LL_IT2STR(it), len, name, (unsigned long) pfid->id,
597                         (unsigned long) pfid->generation, pfid->mds);
598
599         rc = lmv_check_connect(obd);
600         if (rc)
601                 RETURN(rc);
602
603         if (it->it_op == IT_LOOKUP)
604                 rc = lmv_intent_lookup(exp, uctxt, pfid, name, len, lmm,
605                                        lmmsize, cfid, it, flags, reqp,
606                                        cb_blocking);
607         else if (it->it_op & IT_OPEN)
608                 rc = lmv_intent_open(exp, uctxt, pfid, name, len, lmm,
609                                      lmmsize, cfid, it, flags, reqp,
610                                      cb_blocking);
611         else if (it->it_op == IT_GETATTR || it->it_op == IT_CHDIR)
612                 rc = lmv_intent_getattr(exp, uctxt, pfid, name, len, lmm,
613                                         lmmsize, cfid, it, flags, reqp,
614                                         cb_blocking);
615         else
616                 LBUG();
617         RETURN(rc);
618 }
619
620 int lmv_revalidate_slaves(struct obd_export *exp, struct ptlrpc_request **reqp,
621                           struct ll_fid *mfid, struct lookup_intent *oit,
622                           int master_valid, ldlm_blocking_callback cb_blocking)
623 {
624         struct obd_device *obd = exp->exp_obd;
625         struct ptlrpc_request *mreq = *reqp;
626         struct lmv_obd *lmv = &obd->u.lmv;
627         struct lustre_handle master_lockh;
628         struct ldlm_lock *lock;
629         unsigned long size = 0;
630         struct mds_body *body;
631         struct ll_uctxt uctxt;
632         struct lmv_obj *obj;
633         int master_lock_mode;
634         int i, rc = 0;
635         ENTRY;
636
637         /* we have to loop over the subobjects, check validity and update them
638          * from MDSs if needed. it's very useful that we need not to update all
639          * the fields. say, common fields (that are equal on all the subojects
640          * need not to be update, another fields (i_size, for example) are
641          * cached all the time */
642         obj = lmv_grab_obj(obd, mfid);
643         LASSERT(obj != NULL);
644
645         uctxt.gid1 = 0;
646         uctxt.gid2 = 0;
647         master_lock_mode = 0;
648
649         lmv_lock_obj(obj);
650         
651         for (i = 0; i < obj->objcount; i++) {
652                 struct ll_fid fid = obj->objs[i].fid;
653                 struct lustre_handle *lockh = NULL;
654                 struct ptlrpc_request *req = NULL;
655                 ldlm_blocking_callback cb;
656                 struct lookup_intent it;
657                 int master = 0;
658
659                 CDEBUG(D_OTHER, "revalidate subobj %lu/%lu/%lu\n",
660                        (unsigned long)fid.mds, (unsigned long)fid.id,
661                        (unsigned long) fid.generation);
662
663                 memset(&it, 0, sizeof(it));
664                 it.it_op = IT_GETATTR;
665                 cb = lmv_dirobj_blocking_ast;
666
667                 if (fid_equal(&fid, &obj->fid)) {
668                         if (master_valid) {
669                                 /* lmv_intent_getattr() already checked
670                                  * validness and took the lock */
671                                 if (mreq) {
672                                         /* it even got the reply refresh attrs
673                                          * from that reply */
674                                         body = lustre_msg_buf(mreq->rq_repmsg,
675                                                               1, sizeof(*body));
676                                         LASSERT(body != NULL);
677                                         goto update; 
678                                 }
679                                 /* take already cached attrs into account */
680                                 CDEBUG(D_OTHER,
681                                        "master is locked and cached\n");
682                                 goto release_lock;
683                         }
684                         master = 1;
685                         cb = cb_blocking;
686                 }
687
688                 /* is obj valid? */
689                 rc = md_intent_lock(lmv->tgts[fid.mds].ltd_exp, &uctxt, &fid,
690                                     NULL, 0, NULL, 0, &fid, &it, 0, &req, cb);
691                 lockh = (struct lustre_handle *) &it.d.lustre.it_lock_handle;
692                 if (rc > 0) {
693                         /* nice, this slave is valid */
694                         LASSERT(req == NULL);
695                         CDEBUG(D_OTHER, "cached\n");
696                         goto release_lock;
697                 }
698
699                 if (rc < 0)
700                         /* error during revalidation */
701                         GOTO(cleanup, rc);
702
703                 /* rc == 0, this means we have no such a lock and can't think
704                  * obj is still valid. lookup it again */
705                 LASSERT(req == NULL);
706                 req = NULL;
707                 
708                 memset(&it, 0, sizeof(it));
709                 it.it_op = IT_GETATTR;
710                 rc = md_intent_lock(lmv->tgts[fid.mds].ltd_exp, &uctxt, &fid,
711                                     NULL, 0, NULL, 0, NULL, &it, 0, &req, cb);
712                 lockh = (struct lustre_handle *) &it.d.lustre.it_lock_handle;
713                 LASSERT(rc <= 0);
714
715                 if (rc < 0)
716                         /* error during lookup */
717                         GOTO(cleanup, rc);
718               
719                 if (master) {
720                         LASSERT(master_valid == 0);
721                         /* save lock on master to be returned to the caller */
722                         CDEBUG(D_OTHER, "no lock on master yet\n");
723                         memcpy(&master_lockh, lockh, sizeof(master_lockh));
724                         master_lock_mode = it.d.lustre.it_lock_mode;
725                         it.d.lustre.it_lock_mode = 0;
726                 } else {
727                         /* this is slave. we want to control it */
728                         lock = ldlm_handle2lock(lockh);
729                         LASSERT(lock);
730                         lock->l_ast_data = lmv_get_obj(obj);
731                         LDLM_LOCK_PUT(lock);
732                 }
733
734                 if (*reqp == NULL) {
735                         /* this is first reply, we'll use it to return
736                          * updated data back to the caller */
737                         LASSERT(req);
738                         ptlrpc_request_addref(req);
739                         *reqp = req;
740
741                 }
742
743                 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
744                 LASSERT(body);
745                 
746 update:
747                 obj->objs[i].size = body->size;
748                 
749                 CDEBUG(D_OTHER, "fresh: %lu\n",
750                        (unsigned long)obj->objs[i].size);
751
752                 if (req)
753                         ptlrpc_req_finished(req);
754 release_lock:
755                 size += obj->objs[i].size;
756
757                 if (it.d.lustre.it_lock_mode)
758                         ldlm_lock_decref(lockh, it.d.lustre.it_lock_mode);
759         }
760
761         if (*reqp) {
762                 /* some attrs got refreshed, we have reply and it's time to put
763                  * fresh attrs to it */
764                 CDEBUG(D_OTHER, "return refreshed attrs: size = %lu\n",
765                        (unsigned long)size);
766                 
767                 body = lustre_msg_buf((*reqp)->rq_repmsg, 1, sizeof(*body));
768                 LASSERT(body);
769
770                 /* FIXME: what about other attributes? */
771                 body->size = size;
772                 
773                 if (mreq == NULL) {
774                         /* very important to maintain lli->mds the same because
775                          * of revalidation. mreq == NULL means that caller has
776                          * no reply and the only attr we can return is size */
777                         body->valid = OBD_MD_FLSIZE;
778                         body->mds = obj->fid.mds;
779                 }
780                 if (master_valid == 0) {
781                         memcpy(&oit->d.lustre.it_lock_handle,
782                                &master_lockh, sizeof(master_lockh));
783                         oit->d.lustre.it_lock_mode = master_lock_mode;
784                 }
785                 rc = 0;
786         } else {
787                 /* it seems all the attrs are fresh and we did no request */
788                 CDEBUG(D_OTHER, "all the attrs were fresh\n");
789                 if (master_valid == 0)
790                         oit->d.lustre.it_lock_mode = master_lock_mode;
791                 rc = 1;
792         }
793 cleanup:
794         lmv_unlock_obj(obj);
795         lmv_put_obj(obj);
796         RETURN(rc);
797 }