Whamcloud - gitweb
050751021c2ef1d316005048fb300dd5f4d3ab12
[fs/lustre-release.git] / lustre / lov / lov_request.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #ifdef __KERNEL__
40 #include <libcfs/libcfs.h>
41 #else
42 #include <liblustre.h>
43 #endif
44
45 #include <obd_class.h>
46 #include <lustre/lustre_idl.h>
47
48 #include "lov_internal.h"
49
50 static void lov_init_set(struct lov_request_set *set)
51 {
52         set->set_count = 0;
53         atomic_set(&set->set_completes, 0);
54         atomic_set(&set->set_success, 0);
55         atomic_set(&set->set_finish_checked, 0);
56         set->set_cookies = 0;
57         INIT_LIST_HEAD(&set->set_list);
58         atomic_set(&set->set_refcount, 1);
59         init_waitqueue_head(&set->set_waitq);
60 }
61
62 void lov_finish_set(struct lov_request_set *set)
63 {
64         struct list_head *pos, *n;
65         struct lov_request *req;
66         ENTRY;
67
68         LASSERT(set != NULL);
69         list_for_each_safe(pos, n, &set->set_list) {
70                 req = list_entry(pos, struct lov_request, rq_link);
71                 list_del_init(&req->rq_link);
72
73                 if (req->rq_oi.oi_oa != NULL)
74                         OBDO_FREE(req->rq_oi.oi_oa);
75
76                 if (req->rq_oi.oi_osfs != NULL)
77                         OBD_FREE_PTR(req->rq_oi.oi_osfs);
78
79                 OBD_FREE_PTR(req);
80         }
81
82         OBD_FREE_PTR(set);
83         EXIT;
84 }
85
86 int lov_set_finished(struct lov_request_set *set, int idempotent)
87 {
88         int completes = atomic_read(&set->set_completes);
89
90         CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
91
92         if (completes == set->set_count) {
93                 if (idempotent)
94                         return 1;
95                 if (atomic_inc_return(&set->set_finish_checked) == 1)
96                         return 1;
97         }
98         return 0;
99 }
100
101 void lov_update_set(struct lov_request_set *set,
102                     struct lov_request *req, int rc)
103 {
104         req->rq_complete = 1;
105         req->rq_rc = rc;
106
107         atomic_inc(&set->set_completes);
108         if (rc == 0)
109                 atomic_inc(&set->set_success);
110
111         wake_up(&set->set_waitq);
112 }
113
114 int lov_update_common_set(struct lov_request_set *set,
115                           struct lov_request *req, int rc)
116 {
117         struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
118         ENTRY;
119
120         lov_update_set(set, req, rc);
121
122         /* grace error on inactive ost */
123         if (rc && !(lov->lov_tgts[req->rq_idx] &&
124                     lov->lov_tgts[req->rq_idx]->ltd_active))
125                 rc = 0;
126
127         /* FIXME in raid1 regime, should return 0 */
128         RETURN(rc);
129 }
130
131 void lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
132 {
133         list_add_tail(&req->rq_link, &set->set_list);
134         set->set_count++;
135         req->rq_rqset = set;
136 }
137
138 static int lov_check_set(struct lov_obd *lov, int idx)
139 {
140         int rc = 0;
141         mutex_lock(&lov->lov_lock);
142
143         if (lov->lov_tgts[idx] == NULL ||
144             lov->lov_tgts[idx]->ltd_active ||
145             (lov->lov_tgts[idx]->ltd_exp != NULL &&
146              class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
147                 rc = 1;
148
149         mutex_unlock(&lov->lov_lock);
150         return rc;
151 }
152
153 /* Check if the OSC connection exists and is active.
154  * If the OSC has not yet had a chance to connect to the OST the first time,
155  * wait once for it to connect instead of returning an error.
156  */
157 int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
158 {
159         wait_queue_head_t waitq;
160         struct l_wait_info lwi;
161         struct lov_tgt_desc *tgt;
162         int rc = 0;
163
164         mutex_lock(&lov->lov_lock);
165
166         tgt = lov->lov_tgts[ost_idx];
167
168         if (unlikely(tgt == NULL))
169                 GOTO(out, rc = 0);
170
171         if (likely(tgt->ltd_active))
172                 GOTO(out, rc = 1);
173
174         if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
175                 GOTO(out, rc = 0);
176
177         mutex_unlock(&lov->lov_lock);
178
179         init_waitqueue_head(&waitq);
180         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
181                                    cfs_time_seconds(1), NULL, NULL);
182
183         rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
184         if (tgt->ltd_active)
185                 return 1;
186
187         return 0;
188
189 out:
190         mutex_unlock(&lov->lov_lock);
191         return rc;
192 }
193
194 static int common_attr_done(struct lov_request_set *set)
195 {
196         struct list_head *pos;
197         struct lov_request *req;
198         struct obdo *tmp_oa;
199         int rc = 0, attrset = 0;
200         ENTRY;
201
202         LASSERT(set->set_oi != NULL);
203
204         if (set->set_oi->oi_oa == NULL)
205                 RETURN(0);
206
207         if (!atomic_read(&set->set_success))
208                 RETURN(-EIO);
209
210         OBDO_ALLOC(tmp_oa);
211         if (tmp_oa == NULL)
212                 GOTO(out, rc = -ENOMEM);
213
214         list_for_each(pos, &set->set_list) {
215                 req = list_entry(pos, struct lov_request, rq_link);
216
217                 if (!req->rq_complete || req->rq_rc)
218                         continue;
219                 if (req->rq_oi.oi_oa->o_valid == 0)   /* inactive stripe */
220                         continue;
221                 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
222                                 req->rq_oi.oi_oa->o_valid,
223                                 set->set_oi->oi_md, req->rq_stripe, &attrset);
224         }
225         if (!attrset) {
226                 CERROR("No stripes had valid attrs\n");
227                 rc = -EIO;
228         }
229         if ((set->set_oi->oi_oa->o_valid & OBD_MD_FLEPOCH) &&
230             (set->set_oi->oi_md->lsm_stripe_count != attrset)) {
231                 /* When we take attributes of some epoch, we require all the
232                  * ost to be active. */
233                 CERROR("Not all the stripes had valid attrs\n");
234                 GOTO(out, rc = -EIO);
235         }
236
237         tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
238         memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
239 out:
240         if (tmp_oa)
241                 OBDO_FREE(tmp_oa);
242         RETURN(rc);
243
244 }
245
246 int lov_fini_getattr_set(struct lov_request_set *set)
247 {
248         int rc = 0;
249         ENTRY;
250
251         if (set == NULL)
252                 RETURN(0);
253         LASSERT(set->set_exp);
254         if (atomic_read(&set->set_completes))
255                 rc = common_attr_done(set);
256
257         lov_put_reqset(set);
258
259         RETURN(rc);
260 }
261
262 /* The callback for osc_getattr_async that finilizes a request info when a
263  * response is received. */
264 static int cb_getattr_update(void *cookie, int rc)
265 {
266         struct obd_info *oinfo = cookie;
267         struct lov_request *lovreq;
268         lovreq = container_of(oinfo, struct lov_request, rq_oi);
269         return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
270 }
271
272 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
273                          struct lov_request_set **reqset)
274 {
275         struct lov_request_set *set;
276         struct lov_obd *lov = &exp->exp_obd->u.lov;
277         int rc = 0, i;
278         ENTRY;
279
280         OBD_ALLOC(set, sizeof(*set));
281         if (set == NULL)
282                 RETURN(-ENOMEM);
283         lov_init_set(set);
284
285         set->set_exp = exp;
286         set->set_oi = oinfo;
287
288         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
289                 struct lov_oinfo *loi;
290                 struct lov_request *req;
291
292                 loi = oinfo->oi_md->lsm_oinfo[i];
293                 if (lov_oinfo_is_dummy(loi))
294                         continue;
295
296                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
297                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
298                         if (oinfo->oi_oa->o_valid & OBD_MD_FLEPOCH)
299                                 /* SOM requires all the OSTs to be active. */
300                                 GOTO(out_set, rc = -EIO);
301                         continue;
302                 }
303
304                 OBD_ALLOC(req, sizeof(*req));
305                 if (req == NULL)
306                         GOTO(out_set, rc = -ENOMEM);
307
308                 req->rq_stripe = i;
309                 req->rq_idx = loi->loi_ost_idx;
310
311                 OBDO_ALLOC(req->rq_oi.oi_oa);
312                 if (req->rq_oi.oi_oa == NULL) {
313                         OBD_FREE(req, sizeof(*req));
314                         GOTO(out_set, rc = -ENOMEM);
315                 }
316                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
317                        sizeof(*req->rq_oi.oi_oa));
318                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
319                 req->rq_oi.oi_cb_up = cb_getattr_update;
320                 req->rq_oi.oi_capa = oinfo->oi_capa;
321
322                 lov_set_add_req(req, set);
323         }
324         if (!set->set_count)
325                 GOTO(out_set, rc = -EIO);
326         *reqset = set;
327         RETURN(rc);
328 out_set:
329         lov_fini_getattr_set(set);
330         RETURN(rc);
331 }
332
333 int lov_fini_destroy_set(struct lov_request_set *set)
334 {
335         ENTRY;
336
337         if (set == NULL)
338                 RETURN(0);
339         LASSERT(set->set_exp);
340         if (atomic_read(&set->set_completes)) {
341                 /* FIXME update qos data here */
342         }
343
344         lov_put_reqset(set);
345
346         RETURN(0);
347 }
348
349 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
350                          struct obdo *src_oa, struct lov_stripe_md *lsm,
351                          struct obd_trans_info *oti,
352                          struct lov_request_set **reqset)
353 {
354         struct lov_request_set *set;
355         struct lov_obd *lov = &exp->exp_obd->u.lov;
356         int rc = 0, i;
357         ENTRY;
358
359         OBD_ALLOC(set, sizeof(*set));
360         if (set == NULL)
361                 RETURN(-ENOMEM);
362         lov_init_set(set);
363
364         set->set_exp = exp;
365         set->set_oi = oinfo;
366         set->set_oi->oi_md = lsm;
367         set->set_oi->oi_oa = src_oa;
368         if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
369                 set->set_cookies = oti->oti_logcookies;
370
371         for (i = 0; i < lsm->lsm_stripe_count; i++) {
372                 struct lov_oinfo *loi;
373                 struct lov_request *req;
374
375                 loi = lsm->lsm_oinfo[i];
376                 if (lov_oinfo_is_dummy(loi))
377                         continue;
378
379                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
380                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
381                         continue;
382                 }
383
384                 OBD_ALLOC(req, sizeof(*req));
385                 if (req == NULL)
386                         GOTO(out_set, rc = -ENOMEM);
387
388                 req->rq_stripe = i;
389                 req->rq_idx = loi->loi_ost_idx;
390
391                 OBDO_ALLOC(req->rq_oi.oi_oa);
392                 if (req->rq_oi.oi_oa == NULL) {
393                         OBD_FREE(req, sizeof(*req));
394                         GOTO(out_set, rc = -ENOMEM);
395                 }
396                 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
397                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
398                 lov_set_add_req(req, set);
399         }
400         if (!set->set_count)
401                 GOTO(out_set, rc = -EIO);
402         *reqset = set;
403         RETURN(rc);
404 out_set:
405         lov_fini_destroy_set(set);
406         RETURN(rc);
407 }
408
409 int lov_fini_setattr_set(struct lov_request_set *set)
410 {
411         int rc = 0;
412         ENTRY;
413
414         if (set == NULL)
415                 RETURN(0);
416         LASSERT(set->set_exp);
417         if (atomic_read(&set->set_completes)) {
418                 rc = common_attr_done(set);
419                 /* FIXME update qos data here */
420         }
421
422         lov_put_reqset(set);
423         RETURN(rc);
424 }
425
426 int lov_update_setattr_set(struct lov_request_set *set,
427                            struct lov_request *req, int rc)
428 {
429         struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
430         struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
431         ENTRY;
432
433         lov_update_set(set, req, rc);
434
435         /* grace error on inactive ost */
436         if (rc && !(lov->lov_tgts[req->rq_idx] &&
437                     lov->lov_tgts[req->rq_idx]->ltd_active))
438                 rc = 0;
439
440         if (rc == 0) {
441                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
442                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
443                                 req->rq_oi.oi_oa->o_ctime;
444                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
445                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
446                                 req->rq_oi.oi_oa->o_mtime;
447                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
448                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
449                                 req->rq_oi.oi_oa->o_atime;
450         }
451
452         RETURN(rc);
453 }
454
455 /* The callback for osc_setattr_async that finilizes a request info when a
456  * response is received. */
457 static int cb_setattr_update(void *cookie, int rc)
458 {
459         struct obd_info *oinfo = cookie;
460         struct lov_request *lovreq;
461         lovreq = container_of(oinfo, struct lov_request, rq_oi);
462         return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
463 }
464
465 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
466                          struct obd_trans_info *oti,
467                          struct lov_request_set **reqset)
468 {
469         struct lov_request_set *set;
470         struct lov_obd *lov = &exp->exp_obd->u.lov;
471         int rc = 0, i;
472         ENTRY;
473
474         OBD_ALLOC(set, sizeof(*set));
475         if (set == NULL)
476                 RETURN(-ENOMEM);
477         lov_init_set(set);
478
479         set->set_exp = exp;
480         set->set_oi = oinfo;
481         if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
482                 set->set_cookies = oti->oti_logcookies;
483
484         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
485                 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
486                 struct lov_request *req;
487
488                 if (lov_oinfo_is_dummy(loi))
489                         continue;
490
491                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
492                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
493                         continue;
494                 }
495
496                 OBD_ALLOC(req, sizeof(*req));
497                 if (req == NULL)
498                         GOTO(out_set, rc = -ENOMEM);
499                 req->rq_stripe = i;
500                 req->rq_idx = loi->loi_ost_idx;
501
502                 OBDO_ALLOC(req->rq_oi.oi_oa);
503                 if (req->rq_oi.oi_oa == NULL) {
504                         OBD_FREE(req, sizeof(*req));
505                         GOTO(out_set, rc = -ENOMEM);
506                 }
507                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
508                        sizeof(*req->rq_oi.oi_oa));
509                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
510                 req->rq_oi.oi_oa->o_stripe_idx = i;
511                 req->rq_oi.oi_cb_up = cb_setattr_update;
512                 req->rq_oi.oi_capa = oinfo->oi_capa;
513
514                 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
515                         int off = lov_stripe_offset(oinfo->oi_md,
516                                                     oinfo->oi_oa->o_size, i,
517                                                     &req->rq_oi.oi_oa->o_size);
518
519                         if (off < 0 && req->rq_oi.oi_oa->o_size)
520                                 req->rq_oi.oi_oa->o_size--;
521
522                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
523                                i, req->rq_oi.oi_oa->o_size,
524                                oinfo->oi_oa->o_size);
525                 }
526                 lov_set_add_req(req, set);
527         }
528         if (!set->set_count)
529                 GOTO(out_set, rc = -EIO);
530         *reqset = set;
531         RETURN(rc);
532 out_set:
533         lov_fini_setattr_set(set);
534         RETURN(rc);
535 }
536
537 #define LOV_U64_MAX ((__u64)~0ULL)
538 #define LOV_SUM_MAX(tot, add)                                           \
539         do {                                                            \
540                 if ((tot) + (add) < (tot))                              \
541                         (tot) = LOV_U64_MAX;                            \
542                 else                                                    \
543                         (tot) += (add);                                 \
544         } while(0)
545
546 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
547 {
548         ENTRY;
549
550         if (success) {
551                 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
552                                                            LOV_MAGIC, 0);
553                 if (osfs->os_files != LOV_U64_MAX)
554                         lov_do_div64(osfs->os_files, expected_stripes);
555                 if (osfs->os_ffree != LOV_U64_MAX)
556                         lov_do_div64(osfs->os_ffree, expected_stripes);
557
558                 spin_lock(&obd->obd_osfs_lock);
559                 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
560                 obd->obd_osfs_age = cfs_time_current_64();
561                 spin_unlock(&obd->obd_osfs_lock);
562                 RETURN(0);
563         }
564
565         RETURN(-EIO);
566 }
567
568 int lov_fini_statfs_set(struct lov_request_set *set)
569 {
570         int rc = 0;
571         ENTRY;
572
573         if (set == NULL)
574                 RETURN(0);
575
576         if (atomic_read(&set->set_completes)) {
577                 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
578                                      atomic_read(&set->set_success));
579         }
580         lov_put_reqset(set);
581         RETURN(rc);
582 }
583
584 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
585                        int success)
586 {
587         int shift = 0, quit = 0;
588         __u64 tmp;
589
590         if (success == 0) {
591                 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
592         } else {
593                 if (osfs->os_bsize != lov_sfs->os_bsize) {
594                         /* assume all block sizes are always powers of 2 */
595                         /* get the bits difference */
596                         tmp = osfs->os_bsize | lov_sfs->os_bsize;
597                         for (shift = 0; shift <= 64; ++shift) {
598                                 if (tmp & 1) {
599                                         if (quit)
600                                                 break;
601                                         else
602                                                 quit = 1;
603                                         shift = 0;
604                                 }
605                                 tmp >>= 1;
606                         }
607                 }
608
609                 if (osfs->os_bsize < lov_sfs->os_bsize) {
610                         osfs->os_bsize = lov_sfs->os_bsize;
611
612                         osfs->os_bfree  >>= shift;
613                         osfs->os_bavail >>= shift;
614                         osfs->os_blocks >>= shift;
615                 } else if (shift != 0) {
616                         lov_sfs->os_bfree  >>= shift;
617                         lov_sfs->os_bavail >>= shift;
618                         lov_sfs->os_blocks >>= shift;
619                 }
620 #ifdef MIN_DF
621                 /* Sandia requested that df (and so, statfs) only
622                    returned minimal available space on
623                    a single OST, so people would be able to
624                    write this much data guaranteed. */
625                 if (osfs->os_bavail > lov_sfs->os_bavail) {
626                         /* Presumably if new bavail is smaller,
627                            new bfree is bigger as well */
628                         osfs->os_bfree = lov_sfs->os_bfree;
629                         osfs->os_bavail = lov_sfs->os_bavail;
630                 }
631 #else
632                 osfs->os_bfree += lov_sfs->os_bfree;
633                 osfs->os_bavail += lov_sfs->os_bavail;
634 #endif
635                 osfs->os_blocks += lov_sfs->os_blocks;
636                 /* XXX not sure about this one - depends on policy.
637                  *   - could be minimum if we always stripe on all OBDs
638                  *     (but that would be wrong for any other policy,
639                  *     if one of the OBDs has no more objects left)
640                  *   - could be sum if we stripe whole objects
641                  *   - could be average, just to give a nice number
642                  *
643                  * To give a "reasonable" (if not wholly accurate)
644                  * number, we divide the total number of free objects
645                  * by expected stripe count (watch out for overflow).
646                  */
647                 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
648                 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
649         }
650 }
651
652 /* The callback for osc_statfs_async that finilizes a request info when a
653  * response is received. */
654 static int cb_statfs_update(void *cookie, int rc)
655 {
656         struct obd_info *oinfo = cookie;
657         struct lov_request *lovreq;
658         struct lov_request_set *set;
659         struct obd_statfs *osfs, *lov_sfs;
660         struct lov_obd *lov;
661         struct lov_tgt_desc *tgt;
662         struct obd_device *lovobd, *tgtobd;
663         int success;
664         ENTRY;
665
666         lovreq = container_of(oinfo, struct lov_request, rq_oi);
667         set = lovreq->rq_rqset;
668         lovobd = set->set_obd;
669         lov = &lovobd->u.lov;
670         osfs = set->set_oi->oi_osfs;
671         lov_sfs = oinfo->oi_osfs;
672         success = atomic_read(&set->set_success);
673         /* XXX: the same is done in lov_update_common_set, however
674            lovset->set_exp is not initialized. */
675         lov_update_set(set, lovreq, rc);
676         if (rc)
677                 GOTO(out, rc);
678
679         obd_getref(lovobd);
680         tgt = lov->lov_tgts[lovreq->rq_idx];
681         if (!tgt || !tgt->ltd_active)
682                 GOTO(out_update, rc);
683
684         tgtobd = class_exp2obd(tgt->ltd_exp);
685         spin_lock(&tgtobd->obd_osfs_lock);
686         memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
687         if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
688                 tgtobd->obd_osfs_age = cfs_time_current_64();
689         spin_unlock(&tgtobd->obd_osfs_lock);
690
691 out_update:
692         lov_update_statfs(osfs, lov_sfs, success);
693         obd_putref(lovobd);
694
695 out:
696         if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
697             lov_set_finished(set, 0)) {
698                 lov_statfs_interpret(NULL, set, set->set_count !=
699                                      atomic_read(&set->set_success));
700         }
701
702         RETURN(0);
703 }
704
705 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
706                         struct lov_request_set **reqset)
707 {
708         struct lov_request_set *set;
709         struct lov_obd *lov = &obd->u.lov;
710         int rc = 0, i;
711         ENTRY;
712
713         OBD_ALLOC(set, sizeof(*set));
714         if (set == NULL)
715                 RETURN(-ENOMEM);
716         lov_init_set(set);
717
718         set->set_obd = obd;
719         set->set_oi = oinfo;
720
721         /* We only get block data from the OBD */
722         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
723                 struct lov_request *req;
724
725                 if (lov->lov_tgts[i] == NULL ||
726                     (oinfo->oi_flags & OBD_STATFS_NODELAY &&
727                      !lov->lov_tgts[i]->ltd_active)) {
728                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
729                         continue;
730                 }
731
732                 if (!lov->lov_tgts[i]->ltd_active)
733                         lov_check_and_wait_active(lov, i);
734
735                 /* skip targets that have been explicitely disabled by the
736                  * administrator */
737                 if (!lov->lov_tgts[i]->ltd_exp) {
738                         CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
739                         continue;
740                 }
741
742                 OBD_ALLOC(req, sizeof(*req));
743                 if (req == NULL)
744                         GOTO(out_set, rc = -ENOMEM);
745
746                 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
747                 if (req->rq_oi.oi_osfs == NULL) {
748                         OBD_FREE(req, sizeof(*req));
749                         GOTO(out_set, rc = -ENOMEM);
750                 }
751
752                 req->rq_idx = i;
753                 req->rq_oi.oi_cb_up = cb_statfs_update;
754                 req->rq_oi.oi_flags = oinfo->oi_flags;
755
756                 lov_set_add_req(req, set);
757         }
758         if (!set->set_count)
759                 GOTO(out_set, rc = -EIO);
760         *reqset = set;
761         RETURN(rc);
762 out_set:
763         lov_fini_statfs_set(set);
764         RETURN(rc);
765 }