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