Whamcloud - gitweb
548ed584d3d72a606f6baf7b4b583689686ebfa3
[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         CFS_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         cfs_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         cfs_list_t *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         cfs_list_for_each (pos, &set->set_list) {
215                 req = cfs_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_check_and_wait_active(lov, loi->loi_ost_idx)) {
294                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
295                         if (oinfo->oi_oa->o_valid & OBD_MD_FLEPOCH)
296                                 /* SOM requires all the OSTs to be active. */
297                                 GOTO(out_set, rc = -EIO);
298                         continue;
299                 }
300
301                 OBD_ALLOC(req, sizeof(*req));
302                 if (req == NULL)
303                         GOTO(out_set, rc = -ENOMEM);
304
305                 req->rq_stripe = i;
306                 req->rq_idx = loi->loi_ost_idx;
307
308                 OBDO_ALLOC(req->rq_oi.oi_oa);
309                 if (req->rq_oi.oi_oa == NULL) {
310                         OBD_FREE(req, sizeof(*req));
311                         GOTO(out_set, rc = -ENOMEM);
312                 }
313                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
314                        sizeof(*req->rq_oi.oi_oa));
315                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
316                 req->rq_oi.oi_cb_up = cb_getattr_update;
317                 req->rq_oi.oi_capa = oinfo->oi_capa;
318
319                 lov_set_add_req(req, set);
320         }
321         if (!set->set_count)
322                 GOTO(out_set, rc = -EIO);
323         *reqset = set;
324         RETURN(rc);
325 out_set:
326         lov_fini_getattr_set(set);
327         RETURN(rc);
328 }
329
330 int lov_fini_destroy_set(struct lov_request_set *set)
331 {
332         ENTRY;
333
334         if (set == NULL)
335                 RETURN(0);
336         LASSERT(set->set_exp);
337         if (atomic_read(&set->set_completes)) {
338                 /* FIXME update qos data here */
339         }
340
341         lov_put_reqset(set);
342
343         RETURN(0);
344 }
345
346 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
347                          struct obdo *src_oa, struct lov_stripe_md *lsm,
348                          struct obd_trans_info *oti,
349                          struct lov_request_set **reqset)
350 {
351         struct lov_request_set *set;
352         struct lov_obd *lov = &exp->exp_obd->u.lov;
353         int rc = 0, i;
354         ENTRY;
355
356         OBD_ALLOC(set, sizeof(*set));
357         if (set == NULL)
358                 RETURN(-ENOMEM);
359         lov_init_set(set);
360
361         set->set_exp = exp;
362         set->set_oi = oinfo;
363         set->set_oi->oi_md = lsm;
364         set->set_oi->oi_oa = src_oa;
365         if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
366                 set->set_cookies = oti->oti_logcookies;
367
368         for (i = 0; i < lsm->lsm_stripe_count; i++) {
369                 struct lov_oinfo *loi;
370                 struct lov_request *req;
371
372                 loi = lsm->lsm_oinfo[i];
373                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
374                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
375                         continue;
376                 }
377
378                 OBD_ALLOC(req, sizeof(*req));
379                 if (req == NULL)
380                         GOTO(out_set, rc = -ENOMEM);
381
382                 req->rq_stripe = i;
383                 req->rq_idx = loi->loi_ost_idx;
384
385                 OBDO_ALLOC(req->rq_oi.oi_oa);
386                 if (req->rq_oi.oi_oa == NULL) {
387                         OBD_FREE(req, sizeof(*req));
388                         GOTO(out_set, rc = -ENOMEM);
389                 }
390                 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
391                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
392                 lov_set_add_req(req, set);
393         }
394         if (!set->set_count)
395                 GOTO(out_set, rc = -EIO);
396         *reqset = set;
397         RETURN(rc);
398 out_set:
399         lov_fini_destroy_set(set);
400         RETURN(rc);
401 }
402
403 int lov_fini_setattr_set(struct lov_request_set *set)
404 {
405         int rc = 0;
406         ENTRY;
407
408         if (set == NULL)
409                 RETURN(0);
410         LASSERT(set->set_exp);
411         if (atomic_read(&set->set_completes)) {
412                 rc = common_attr_done(set);
413                 /* FIXME update qos data here */
414         }
415
416         lov_put_reqset(set);
417         RETURN(rc);
418 }
419
420 int lov_update_setattr_set(struct lov_request_set *set,
421                            struct lov_request *req, int rc)
422 {
423         struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
424         struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
425         ENTRY;
426
427         lov_update_set(set, req, rc);
428
429         /* grace error on inactive ost */
430         if (rc && !(lov->lov_tgts[req->rq_idx] &&
431                     lov->lov_tgts[req->rq_idx]->ltd_active))
432                 rc = 0;
433
434         if (rc == 0) {
435                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
436                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
437                                 req->rq_oi.oi_oa->o_ctime;
438                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
439                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
440                                 req->rq_oi.oi_oa->o_mtime;
441                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
442                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
443                                 req->rq_oi.oi_oa->o_atime;
444         }
445
446         RETURN(rc);
447 }
448
449 /* The callback for osc_setattr_async that finilizes a request info when a
450  * response is received. */
451 static int cb_setattr_update(void *cookie, int rc)
452 {
453         struct obd_info *oinfo = cookie;
454         struct lov_request *lovreq;
455         lovreq = container_of(oinfo, struct lov_request, rq_oi);
456         return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
457 }
458
459 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
460                          struct obd_trans_info *oti,
461                          struct lov_request_set **reqset)
462 {
463         struct lov_request_set *set;
464         struct lov_obd *lov = &exp->exp_obd->u.lov;
465         int rc = 0, i;
466         ENTRY;
467
468         OBD_ALLOC(set, sizeof(*set));
469         if (set == NULL)
470                 RETURN(-ENOMEM);
471         lov_init_set(set);
472
473         set->set_exp = exp;
474         set->set_oi = oinfo;
475         if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
476                 set->set_cookies = oti->oti_logcookies;
477
478         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
479                 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
480                 struct lov_request *req;
481
482                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
483                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
484                         continue;
485                 }
486
487                 OBD_ALLOC(req, sizeof(*req));
488                 if (req == NULL)
489                         GOTO(out_set, rc = -ENOMEM);
490                 req->rq_stripe = i;
491                 req->rq_idx = loi->loi_ost_idx;
492
493                 OBDO_ALLOC(req->rq_oi.oi_oa);
494                 if (req->rq_oi.oi_oa == NULL) {
495                         OBD_FREE(req, sizeof(*req));
496                         GOTO(out_set, rc = -ENOMEM);
497                 }
498                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
499                        sizeof(*req->rq_oi.oi_oa));
500                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
501                 req->rq_oi.oi_oa->o_stripe_idx = i;
502                 req->rq_oi.oi_cb_up = cb_setattr_update;
503                 req->rq_oi.oi_capa = oinfo->oi_capa;
504
505                 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
506                         int off = lov_stripe_offset(oinfo->oi_md,
507                                                     oinfo->oi_oa->o_size, i,
508                                                     &req->rq_oi.oi_oa->o_size);
509
510                         if (off < 0 && req->rq_oi.oi_oa->o_size)
511                                 req->rq_oi.oi_oa->o_size--;
512
513                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
514                                i, req->rq_oi.oi_oa->o_size,
515                                oinfo->oi_oa->o_size);
516                 }
517                 lov_set_add_req(req, set);
518         }
519         if (!set->set_count)
520                 GOTO(out_set, rc = -EIO);
521         *reqset = set;
522         RETURN(rc);
523 out_set:
524         lov_fini_setattr_set(set);
525         RETURN(rc);
526 }
527
528 #define LOV_U64_MAX ((__u64)~0ULL)
529 #define LOV_SUM_MAX(tot, add)                                           \
530         do {                                                            \
531                 if ((tot) + (add) < (tot))                              \
532                         (tot) = LOV_U64_MAX;                            \
533                 else                                                    \
534                         (tot) += (add);                                 \
535         } while(0)
536
537 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
538 {
539         ENTRY;
540
541         if (success) {
542                 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
543                                                            LOV_MAGIC, 0);
544                 if (osfs->os_files != LOV_U64_MAX)
545                         lov_do_div64(osfs->os_files, expected_stripes);
546                 if (osfs->os_ffree != LOV_U64_MAX)
547                         lov_do_div64(osfs->os_ffree, expected_stripes);
548
549                 spin_lock(&obd->obd_osfs_lock);
550                 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
551                 obd->obd_osfs_age = cfs_time_current_64();
552                 spin_unlock(&obd->obd_osfs_lock);
553                 RETURN(0);
554         }
555
556         RETURN(-EIO);
557 }
558
559 int lov_fini_statfs_set(struct lov_request_set *set)
560 {
561         int rc = 0;
562         ENTRY;
563
564         if (set == NULL)
565                 RETURN(0);
566
567         if (atomic_read(&set->set_completes)) {
568                 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
569                                      atomic_read(&set->set_success));
570         }
571         lov_put_reqset(set);
572         RETURN(rc);
573 }
574
575 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
576                        int success)
577 {
578         int shift = 0, quit = 0;
579         __u64 tmp;
580
581         if (success == 0) {
582                 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
583         } else {
584                 if (osfs->os_bsize != lov_sfs->os_bsize) {
585                         /* assume all block sizes are always powers of 2 */
586                         /* get the bits difference */
587                         tmp = osfs->os_bsize | lov_sfs->os_bsize;
588                         for (shift = 0; shift <= 64; ++shift) {
589                                 if (tmp & 1) {
590                                         if (quit)
591                                                 break;
592                                         else
593                                                 quit = 1;
594                                         shift = 0;
595                                 }
596                                 tmp >>= 1;
597                         }
598                 }
599
600                 if (osfs->os_bsize < lov_sfs->os_bsize) {
601                         osfs->os_bsize = lov_sfs->os_bsize;
602
603                         osfs->os_bfree  >>= shift;
604                         osfs->os_bavail >>= shift;
605                         osfs->os_blocks >>= shift;
606                 } else if (shift != 0) {
607                         lov_sfs->os_bfree  >>= shift;
608                         lov_sfs->os_bavail >>= shift;
609                         lov_sfs->os_blocks >>= shift;
610                 }
611 #ifdef MIN_DF
612                 /* Sandia requested that df (and so, statfs) only
613                    returned minimal available space on
614                    a single OST, so people would be able to
615                    write this much data guaranteed. */
616                 if (osfs->os_bavail > lov_sfs->os_bavail) {
617                         /* Presumably if new bavail is smaller,
618                            new bfree is bigger as well */
619                         osfs->os_bfree = lov_sfs->os_bfree;
620                         osfs->os_bavail = lov_sfs->os_bavail;
621                 }
622 #else
623                 osfs->os_bfree += lov_sfs->os_bfree;
624                 osfs->os_bavail += lov_sfs->os_bavail;
625 #endif
626                 osfs->os_blocks += lov_sfs->os_blocks;
627                 /* XXX not sure about this one - depends on policy.
628                  *   - could be minimum if we always stripe on all OBDs
629                  *     (but that would be wrong for any other policy,
630                  *     if one of the OBDs has no more objects left)
631                  *   - could be sum if we stripe whole objects
632                  *   - could be average, just to give a nice number
633                  *
634                  * To give a "reasonable" (if not wholly accurate)
635                  * number, we divide the total number of free objects
636                  * by expected stripe count (watch out for overflow).
637                  */
638                 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
639                 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
640         }
641 }
642
643 /* The callback for osc_statfs_async that finilizes a request info when a
644  * response is received. */
645 static int cb_statfs_update(void *cookie, int rc)
646 {
647         struct obd_info *oinfo = cookie;
648         struct lov_request *lovreq;
649         struct lov_request_set *set;
650         struct obd_statfs *osfs, *lov_sfs;
651         struct lov_obd *lov;
652         struct lov_tgt_desc *tgt;
653         struct obd_device *lovobd, *tgtobd;
654         int success;
655         ENTRY;
656
657         lovreq = container_of(oinfo, struct lov_request, rq_oi);
658         set = lovreq->rq_rqset;
659         lovobd = set->set_obd;
660         lov = &lovobd->u.lov;
661         osfs = set->set_oi->oi_osfs;
662         lov_sfs = oinfo->oi_osfs;
663         success = atomic_read(&set->set_success);
664         /* XXX: the same is done in lov_update_common_set, however
665            lovset->set_exp is not initialized. */
666         lov_update_set(set, lovreq, rc);
667         if (rc)
668                 GOTO(out, rc);
669
670         obd_getref(lovobd);
671         tgt = lov->lov_tgts[lovreq->rq_idx];
672         if (!tgt || !tgt->ltd_active)
673                 GOTO(out_update, rc);
674
675         tgtobd = class_exp2obd(tgt->ltd_exp);
676         spin_lock(&tgtobd->obd_osfs_lock);
677         memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
678         if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
679                 tgtobd->obd_osfs_age = cfs_time_current_64();
680         spin_unlock(&tgtobd->obd_osfs_lock);
681
682 out_update:
683         lov_update_statfs(osfs, lov_sfs, success);
684         obd_putref(lovobd);
685
686 out:
687         if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
688             lov_set_finished(set, 0)) {
689                 lov_statfs_interpret(NULL, set, set->set_count !=
690                                      atomic_read(&set->set_success));
691         }
692
693         RETURN(0);
694 }
695
696 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
697                         struct lov_request_set **reqset)
698 {
699         struct lov_request_set *set;
700         struct lov_obd *lov = &obd->u.lov;
701         int rc = 0, i;
702         ENTRY;
703
704         OBD_ALLOC(set, sizeof(*set));
705         if (set == NULL)
706                 RETURN(-ENOMEM);
707         lov_init_set(set);
708
709         set->set_obd = obd;
710         set->set_oi = oinfo;
711
712         /* We only get block data from the OBD */
713         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
714                 struct lov_request *req;
715
716                 if (lov->lov_tgts[i] == NULL ||
717                     (oinfo->oi_flags & OBD_STATFS_NODELAY &&
718                      !lov->lov_tgts[i]->ltd_active)) {
719                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
720                         continue;
721                 }
722
723                 if (!lov->lov_tgts[i]->ltd_active)
724                         lov_check_and_wait_active(lov, i);
725
726                 /* skip targets that have been explicitely disabled by the
727                  * administrator */
728                 if (!lov->lov_tgts[i]->ltd_exp) {
729                         CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
730                         continue;
731                 }
732
733                 OBD_ALLOC(req, sizeof(*req));
734                 if (req == NULL)
735                         GOTO(out_set, rc = -ENOMEM);
736
737                 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
738                 if (req->rq_oi.oi_osfs == NULL) {
739                         OBD_FREE(req, sizeof(*req));
740                         GOTO(out_set, rc = -ENOMEM);
741                 }
742
743                 req->rq_idx = i;
744                 req->rq_oi.oi_cb_up = cb_statfs_update;
745                 req->rq_oi.oi_flags = oinfo->oi_flags;
746
747                 lov_set_add_req(req, set);
748         }
749         if (!set->set_count)
750                 GOTO(out_set, rc = -EIO);
751         *reqset = set;
752         RETURN(rc);
753 out_set:
754         lov_fini_statfs_set(set);
755         RETURN(rc);
756 }