Whamcloud - gitweb
fix typo
[fs/lustre-release.git] / lustre / lov / lov_qos.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LOV
41
42 #ifdef __KERNEL__
43 #include <libcfs/libcfs.h>
44 #else
45 #include <liblustre.h>
46 #endif
47
48 #include <obd_class.h>
49 #include <obd_lov.h>
50 #include <lustre/lustre_idl.h>
51 #include "lov_internal.h"
52
53 /* #define QOS_DEBUG 1 */
54 #define D_QOS D_OTHER
55
56 #define TGT_BAVAIL(i)  (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail*\
57                         lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
58 #define TGT_FFREE(i)   (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
59
60
61 int qos_add_tgt(struct obd_device *obd, __u32 index)
62 {
63         struct lov_obd *lov = &obd->u.lov;
64         struct lov_qos_oss *oss, *temposs;
65         struct obd_export *exp = lov->lov_tgts[index]->ltd_exp;
66         int rc = 0, found = 0;
67         ENTRY;
68
69         /* We only need this QOS struct on MDT, not clients - but we may not
70          * have registered the LOV's observer yet, so there's no way to know */
71         if (!exp || !exp->exp_connection) {
72                 CERROR("Missing connection\n");
73                 RETURN(-ENOTCONN);
74         }
75
76         down_write(&lov->lov_qos.lq_rw_sem);
77         mutex_down(&lov->lov_lock);
78         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
79                 if (obd_uuid_equals(&oss->lqo_uuid,
80                                     &exp->exp_connection->c_remote_uuid)) {
81                         found++;
82                         break;
83                 }
84         }
85
86         if (!found) {
87                 OBD_ALLOC_PTR(oss);
88                 if (!oss)
89                         GOTO(out, rc = -ENOMEM);
90                 memcpy(&oss->lqo_uuid,
91                        &exp->exp_connection->c_remote_uuid,
92                        sizeof(oss->lqo_uuid));
93         } else {
94                 /* Assume we have to move this one */
95                 list_del(&oss->lqo_oss_list);
96         }
97
98         oss->lqo_ost_count++;
99         lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
100
101         /* Add sorted by # of OSTs.  Find the first entry that we're
102            bigger than... */
103         list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
104                 if (oss->lqo_ost_count > temposs->lqo_ost_count)
105                         break;
106         }
107         /* ...and add before it.  If we're the first or smallest, temposs
108            points to the list head, and we add to the end. */
109         list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
110
111         lov->lov_qos.lq_dirty = 1;
112         lov->lov_qos.lq_rr.lqr_dirty = 1;
113
114         CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
115                obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
116                obd_uuid2str(&oss->lqo_uuid),
117                oss->lqo_ost_count);
118
119 out:
120         mutex_up(&lov->lov_lock);
121         up_write(&lov->lov_qos.lq_rw_sem);
122         RETURN(rc);
123 }
124
125 int qos_del_tgt(struct obd_device *obd, struct lov_tgt_desc *tgt)
126 {
127         struct lov_obd *lov = &obd->u.lov;
128         struct lov_qos_oss *oss;
129         int rc = 0;
130         ENTRY;
131
132         down_write(&lov->lov_qos.lq_rw_sem);
133
134         oss = tgt->ltd_qos.ltq_oss;
135         if (!oss)
136                 GOTO(out, rc = -ENOENT);
137
138         oss->lqo_ost_count--;
139         if (oss->lqo_ost_count == 0) {
140                 CDEBUG(D_QOS, "removing OSS %s\n",
141                        obd_uuid2str(&oss->lqo_uuid));
142                 list_del(&oss->lqo_oss_list);
143                 OBD_FREE_PTR(oss);
144         }
145
146         lov->lov_qos.lq_dirty = 1;
147         lov->lov_qos.lq_rr.lqr_dirty = 1;
148 out:
149         up_write(&lov->lov_qos.lq_rw_sem);
150         RETURN(rc);
151 }
152
153 /* Recalculate per-object penalties for OSSs and OSTs,
154    depends on size of each ost in an oss */
155 static int qos_calc_ppo(struct obd_device *obd)
156 {
157         struct lov_obd *lov = &obd->u.lov;
158         struct lov_qos_oss *oss;
159         __u64 ba_max, ba_min, temp;
160         __u32 num_active;
161         int rc, i, prio_wide;
162         time_t now, age;
163         ENTRY;
164
165         if (!lov->lov_qos.lq_dirty)
166                 GOTO(out, rc = 0);
167
168         num_active = lov->desc.ld_active_tgt_count - 1;
169         if (num_active < 1)
170                 GOTO(out, rc = -EAGAIN);
171
172         /* find bavail on each OSS */
173         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
174                 oss->lqo_bavail = 0;
175         }
176         lov->lov_qos.lq_active_oss_count = 0;
177
178         /* How badly user wants to select osts "widely" (not recently chosen
179            and not on recent oss's).  As opposed to "freely" (free space
180            avail.) 0-256. */
181         prio_wide = 256 - lov->lov_qos.lq_prio_free;
182
183         ba_min = (__u64)(-1);
184         ba_max = 0;
185         now = cfs_time_current_sec();
186         /* Calculate OST penalty per object */
187         /* (lov ref taken in alloc_qos) */
188         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
189                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
190                         continue;
191                 temp = TGT_BAVAIL(i);
192                 if (!temp)
193                         continue;
194                 ba_min = min(temp, ba_min);
195                 ba_max = max(temp, ba_max);
196
197                 /* Count the number of usable OSS's */
198                 if (lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail == 0)
199                         lov->lov_qos.lq_active_oss_count++;
200                 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail += temp;
201
202                 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
203                 temp >>= 1;
204                 do_div(temp, num_active);
205                 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj =
206                         (temp * prio_wide) >> 8;
207
208                 age = (now - lov->lov_tgts[i]->ltd_qos.ltq_used) >> 3;
209                 if (lov->lov_qos.lq_reset || age > 32 * lov->desc.ld_qos_maxage)
210                         lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
211                 else if (age > lov->desc.ld_qos_maxage)
212                         /* Decay the penalty by half for every 8x the update
213                          * interval that the device has been idle.  That gives
214                          * lots of time for the statfs information to be
215                          * updated (which the penalty is only a proxy for),
216                          * and avoids penalizing OSS/OSTs under light load. */
217                         lov->lov_tgts[i]->ltd_qos.ltq_penalty >>=
218                                 (age / lov->desc.ld_qos_maxage);
219         }
220
221         num_active = lov->lov_qos.lq_active_oss_count - 1;
222         if (num_active < 1) {
223                 /* If there's only 1 OSS, we can't penalize it, so instead
224                    we have to double the OST penalty */
225                 num_active = 1;
226                 for (i = 0; i < lov->desc.ld_tgt_count; i++)
227                         if (lov->lov_tgts[i])
228                             lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
229         }
230
231         /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
232         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
233                 temp = oss->lqo_bavail >> 1;
234                 do_div(temp, oss->lqo_ost_count * num_active);
235                 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
236
237                 age = (now - oss->lqo_used) >> 3;
238                 if (lov->lov_qos.lq_reset || age > 32 * lov->desc.ld_qos_maxage)
239                         oss->lqo_penalty = 0;
240                 else if (age > lov->desc.ld_qos_maxage)
241                         /* Decay the penalty by half for every 8x the update
242                          * interval that the device has been idle.  That gives
243                          * lots of time for the statfs information to be
244                          * updated (which the penalty is only a proxy for),
245                          * and avoids penalizing OSS/OSTs under light load. */
246                         oss->lqo_penalty >>= (age / lov->desc.ld_qos_maxage);
247         }
248
249         lov->lov_qos.lq_dirty = 0;
250         lov->lov_qos.lq_reset = 0;
251
252         /* If each ost has almost same free space,
253          * do rr allocation for better creation performance */
254         lov->lov_qos.lq_same_space = 0;
255         if ((ba_max * (256 - lov->lov_qos.lq_threshold_rr)) >> 8 < ba_min) {
256                 lov->lov_qos.lq_same_space = 1;
257                 /* Reset weights for the next time we enter qos mode */
258                 lov->lov_qos.lq_reset = 1;
259         }
260         rc = 0;
261
262 out:
263         if (!rc && lov->lov_qos.lq_same_space)
264                 RETURN(-EAGAIN);
265         RETURN(rc);
266 }
267
268 static int qos_calc_weight(struct lov_obd *lov, int i)
269 {
270         __u64 temp, temp2;
271
272         /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
273         temp = TGT_BAVAIL(i);
274         temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty +
275                 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
276         if (temp < temp2)
277                 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
278         else
279                 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
280         return 0;
281 }
282
283 /* We just used this index for a stripe; adjust everyone's weights */
284 static int qos_used(struct lov_obd *lov, struct ost_pool *osts,
285                     __u32 index, __u64 *total_wt)
286 {
287         struct lov_qos_oss *oss;
288         int j;
289         ENTRY;
290
291         /* Don't allocate from this stripe anymore, until the next alloc_qos */
292         lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
293
294         oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
295
296         /* Decay old penalty by half (we're adding max penalty, and don't
297            want it to run away.) */
298         lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
299         oss->lqo_penalty >>= 1;
300
301         /* mark the OSS and OST as recently used */
302         lov->lov_tgts[index]->ltd_qos.ltq_used =
303                 oss->lqo_used = cfs_time_current_sec();
304
305         /* Set max penalties for this OST and OSS */
306         lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
307                 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
308                 lov->desc.ld_active_tgt_count;
309         oss->lqo_penalty += oss->lqo_penalty_per_obj *
310                 lov->lov_qos.lq_active_oss_count;
311
312         /* Decrease all OSS penalties */
313         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
314                 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
315                         oss->lqo_penalty = 0;
316                 else
317                         oss->lqo_penalty -= oss->lqo_penalty_per_obj;
318         }
319
320         *total_wt = 0;
321         /* Decrease all OST penalties */
322         for (j = 0; j < osts->op_count; j++) {
323                 int i;
324
325                 i = osts->op_array[j];
326                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
327                         continue;
328                 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
329                     lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
330                         lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
331                 else
332                         lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
333                         lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
334
335                 qos_calc_weight(lov, i);
336
337                 /* Recalc the total weight of usable osts */
338                 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
339                         *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
340
341 #ifdef QOS_DEBUG
342                 CDEBUG(D_QOS, "recalc tgt %d usable=%d avail="LPU64
343                        " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
344                        " ossp="LPU64" wt="LPU64"\n",
345                        i, lov->lov_tgts[i]->ltd_qos.ltq_usable,
346                        TGT_BAVAIL(i) >> 10,
347                        lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
348                        lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
349                        lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
350                        lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty >> 10,
351                        lov->lov_tgts[i]->ltd_qos.ltq_weight >> 10);
352 #endif
353         }
354
355         RETURN(0);
356 }
357
358 #define LOV_QOS_EMPTY ((__u32)-1)
359 /* compute optimal round-robin order, based on OSTs per OSS
360  */
361 static int qos_calc_rr(struct lov_obd *lov, struct ost_pool *src_pool,
362                        struct lov_qos_rr *lqr)
363 {
364         struct lov_qos_oss *oss;
365         unsigned placed, real_count;
366         int i, rc;
367         ENTRY;
368
369         if (!lqr->lqr_dirty) {
370                 LASSERT(lqr->lqr_pool.op_size);
371                 RETURN(0);
372         }
373
374         /* Do actual allocation. */
375         down_write(&lov->lov_qos.lq_rw_sem);
376
377         real_count = src_pool->op_count;
378
379         /* Zero the pool array */
380         /* alloc_rr is holding a read lock on the pool, so nobody is adding/
381            deleting from the pool. The lq_rw_sem insures that nobody else
382            is reading. */
383         lqr->lqr_pool.op_count = real_count;
384         rc = lov_ost_pool_extend(&lqr->lqr_pool, real_count);
385         if (rc) {
386                 up_write(&lov->lov_qos.lq_rw_sem);
387                 RETURN(rc);
388         }
389         for (i = 0; i < lqr->lqr_pool.op_count; i++)
390                 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
391
392         /* Place all the OSTs from 1 OSS at the same time. */
393         placed = 0;
394         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
395                 int j = 0;
396                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
397                         if (lov->lov_tgts[src_pool->op_array[i]] &&
398                             (lov->lov_tgts[src_pool->op_array[i]]->ltd_qos.ltq_oss == oss)) {
399                               /* Evenly space these OSTs across arrayspace */
400                               int next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
401                               while (lqr->lqr_pool.op_array[next] !=
402                                      LOV_QOS_EMPTY)
403                                       next = (next + 1) % lqr->lqr_pool.op_count;
404                               lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
405                               j++;
406                               placed++;
407                         }
408                 }
409         }
410
411         lqr->lqr_dirty = 0;
412         up_write(&lov->lov_qos.lq_rw_sem);
413
414         if (placed != real_count) {
415                 /* This should never happen */
416                 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
417                                    "round-robin list (%d of %d).\n",
418                                    placed, real_count);
419                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
420                         LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
421                                  lqr->lqr_pool.op_array[i]);
422                 }
423                 lqr->lqr_dirty = 1;
424                 RETURN(-EAGAIN);
425         }
426
427 #ifdef QOS_DEBUG
428         for (i = 0; i < lqr->lqr_pool.op_count; i++) {
429                 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
430                          lqr->lqr_pool.op_array[i]);
431         }
432 #endif
433
434         RETURN(0);
435 }
436
437
438 void qos_shrink_lsm(struct lov_request_set *set)
439 {
440         struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
441         /* XXX LOV STACKING call into osc for sizes */
442         unsigned oldsize, newsize;
443
444         if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
445                 struct llog_cookie *cookies;
446                 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
447                 newsize = set->set_count * sizeof(*cookies);
448
449                 cookies = set->set_cookies;
450                 oti_alloc_cookies(set->set_oti, set->set_count);
451                 if (set->set_oti->oti_logcookies) {
452                         memcpy(set->set_oti->oti_logcookies, cookies, newsize);
453                         OBD_FREE(cookies, oldsize);
454                         set->set_cookies = set->set_oti->oti_logcookies;
455                 } else {
456                         CWARN("'leaking' %d bytes\n", oldsize - newsize);
457                 }
458         }
459
460         CWARN("using fewer stripes for object "LPU64": old %u new %u\n",
461               lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
462         LASSERT(lsm->lsm_stripe_count >= set->set_count);
463
464         newsize = lov_stripe_md_size(set->set_count);
465         OBD_ALLOC(lsm_new, newsize);
466         if (lsm_new != NULL) {
467                 int i;
468                 memcpy(lsm_new, lsm, sizeof(*lsm));
469                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
470                         if (i < set->set_count) {
471                                 lsm_new->lsm_oinfo[i] = lsm->lsm_oinfo[i];
472                                 continue;
473                         }
474                         OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
475                                       sizeof(struct lov_oinfo));
476                 }
477                 lsm_new->lsm_stripe_count = set->set_count;
478                 OBD_FREE(lsm, sizeof(struct lov_stripe_md) +
479                          lsm->lsm_stripe_count * sizeof(struct lov_oinfo *));
480                 set->set_oi->oi_md = lsm_new;
481         } else {
482                 CWARN("'leaking' few bytes\n");
483         }
484 }
485
486 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
487 {
488         struct lov_stripe_md *lsm = set->set_oi->oi_md;
489         struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
490         unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
491         int stripe, i, rc = -EIO;
492         ENTRY;
493
494         ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
495         for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
496                 if (!lov->lov_tgts[ost_idx] ||
497                     !lov->lov_tgts[ost_idx]->ltd_active)
498                         continue;
499                 /* check if objects has been created on this ost */
500                 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
501                         /* we try send create to this ost but he is failed */
502                         if (stripe == req->rq_stripe)
503                                 continue;
504                         /* already have object at this stripe */
505                         if (ost_idx == lsm->lsm_oinfo[stripe]->loi_ost_idx)
506                                 break;
507                 }
508                 if (stripe >= lsm->lsm_stripe_count) {
509                         req->rq_idx = ost_idx;
510                         rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
511                                         req->rq_oi.oi_oa, &req->rq_oi.oi_md,
512                                         set->set_oti);
513                         if (!rc)
514                                 break;
515                 }
516         }
517         RETURN(rc);
518 }
519
520 static int min_stripe_count(int stripe_cnt, int flags)
521 {
522         return (flags & LOV_USES_DEFAULT_STRIPE ?
523                 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
524 }
525
526 #define LOV_CREATE_RESEED_MULT 4
527 #define LOV_CREATE_RESEED_MIN  1000
528 /* Allocate objects on osts with round-robin algorithm */
529 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt,
530                     char *poolname, int flags)
531 {
532         unsigned array_idx;
533         int i, rc, *idx_pos;
534         __u32 ost_idx;
535         int ost_start_idx_temp;
536         int speed = 0;
537         int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
538         struct pool_desc *pool;
539         struct ost_pool *osts;
540         struct lov_qos_rr *lqr;
541         ENTRY;
542
543         pool = lov_find_pool(lov, poolname);
544         if (pool == NULL) {
545                 osts = &(lov->lov_packed);
546                 lqr = &(lov->lov_qos.lq_rr);
547         } else {
548                 down_read(&pool_tgt_rw_sem(pool));
549                 osts = &(pool->pool_obds);
550                 lqr = &(pool->pool_rr);
551         }
552
553         rc = qos_calc_rr(lov, osts, lqr);
554         if (rc)
555                 GOTO(out, rc);
556
557         if (--lqr->lqr_start_count <= 0) {
558                 lqr->lqr_start_idx = ll_rand() % osts->op_count;
559                 lqr->lqr_start_count =
560                         (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
561                          LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
562         } else if (stripe_cnt_min >= osts->op_count ||
563                    lqr->lqr_start_idx > osts->op_count) {
564                 /* If we have allocated from all of the OSTs, slowly
565                  * precess the next start if the OST/stripe count isn't
566                  * already doing this for us. */
567                 lqr->lqr_start_idx %= osts->op_count;
568                 if (*stripe_cnt > 1 && (osts->op_count % (*stripe_cnt)) != 1)
569                         ++lqr->lqr_offset_idx;
570         }
571         down_read(&lov->lov_qos.lq_rw_sem);
572         ost_start_idx_temp = lqr->lqr_start_idx;
573
574 repeat_find:
575         array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) % osts->op_count;
576         idx_pos = idx_arr;
577 #ifdef QOS_DEBUG
578         CDEBUG(D_QOS, "pool '%s' want %d startidx %d startcnt %d offset %d "
579                "active %d count %d arrayidx %d\n", poolname,
580                *stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
581                lqr->lqr_offset_idx, osts->op_count, osts->op_count, array_idx);
582 #endif
583
584         for (i = 0; i < osts->op_count;
585                     i++, array_idx=(array_idx + 1) % osts->op_count) {
586                 ++lqr->lqr_start_idx;
587                 ost_idx = lqr->lqr_pool.op_array[array_idx];
588 #ifdef QOS_DEBUG
589                 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
590                        i, lqr->lqr_start_idx,
591                        ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
592                        lov->lov_tgts[ost_idx]->ltd_active : 0,
593                        idx_pos - idx_arr, array_idx, ost_idx);
594 #endif
595                 if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
596                     !lov->lov_tgts[ost_idx]->ltd_active)
597                         continue;
598
599                 /* Fail Check before osc_precreate() is called
600                    so we can only 'fail' single OSC. */
601                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
602                         continue;
603
604                 /* Drop slow OSCs if we can */
605                 if (obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed)
606                         continue;
607
608                 *idx_pos = ost_idx;
609                 idx_pos++;
610                 /* We have enough stripes */
611                 if (idx_pos - idx_arr == *stripe_cnt)
612                         break;
613         }
614         if ((speed < 2) && (idx_pos - idx_arr < stripe_cnt_min)) {
615                 /* Try again, allowing slower OSCs */
616                 speed++;
617                 lqr->lqr_start_idx = ost_start_idx_temp;
618                 goto repeat_find;
619         }
620
621         up_read(&lov->lov_qos.lq_rw_sem);
622
623         *stripe_cnt = idx_pos - idx_arr;
624 out:
625         if (pool != NULL) {
626                 up_read(&pool_tgt_rw_sem(pool));
627                 /* put back ref got by lov_find_pool() */
628                 lov_pool_putref(pool);
629         }
630
631         RETURN(rc);
632 }
633
634 /* alloc objects on osts with specific stripe offset */
635 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
636                           int *idx_arr)
637 {
638         unsigned ost_idx, array_idx, ost_count;
639         int i, rc, *idx_pos;
640         int speed = 0;
641         struct pool_desc *pool;
642         struct ost_pool *osts;
643         ENTRY;
644
645         pool = lov_find_pool(lov, lsm->lsm_pool_name);
646         if (pool == NULL) {
647                 osts = &(lov->lov_packed);
648         } else {
649                 down_read(&pool_tgt_rw_sem(pool));
650                 osts = &(pool->pool_obds);
651         }
652
653         ost_count = osts->op_count;
654
655 repeat_find:
656         /* search loi_ost_idx in ost array */
657         array_idx = 0;
658         for (i = 0; i < ost_count; i++) {
659                 if (osts->op_array[i] == lsm->lsm_oinfo[0]->loi_ost_idx) {
660                         array_idx = i;
661                         break;
662                 }
663         }
664         if (i == ost_count) {
665                 CERROR("Start index %d not found in pool '%s'\n",
666                        lsm->lsm_oinfo[0]->loi_ost_idx, lsm->lsm_pool_name);
667                 GOTO(out, rc = -EINVAL);
668         }
669
670         idx_pos = idx_arr;
671         for (i = 0; i < ost_count;
672              i++, array_idx = (array_idx + 1) % ost_count) {
673                 ost_idx = osts->op_array[array_idx];
674
675                 if (!lov->lov_tgts[ost_idx] ||
676                     !lov->lov_tgts[ost_idx]->ltd_active) {
677                         continue;
678                 }
679
680                 /* Fail Check before osc_precreate() is called
681                    so we can only 'fail' single OSC. */
682                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
683                         continue;
684
685                 /* Drop slow OSCs if we can, but not for requested start idx.
686                  *
687                  * This means "if OSC is slow and it is not the requested
688                  * start OST, then it can be skipped, otherwise skip it only
689                  * if it is inactive/recovering/out-of-space." */
690                 if ((obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed) &&
691                     (i != 0 || speed >= 2))
692                         continue;
693
694                 *idx_pos = ost_idx;
695                 idx_pos++;
696                 /* We have enough stripes */
697                 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
698                         GOTO(out, rc = 0);
699         }
700         if (speed < 2) {
701                 /* Try again, allowing slower OSCs */
702                 speed++;
703                 goto repeat_find;
704         }
705
706         /* If we were passed specific striping params, then a failure to
707          * meet those requirements is an error, since we can't reallocate
708          * that memory (it might be part of a larger array or something).
709          *
710          * We can only get here if lsm_stripe_count was originally > 1.
711          */
712         CERROR("can't lstripe objid "LPX64": have %d want %u\n",
713                lsm->lsm_object_id, (int)(idx_pos - idx_arr),
714                lsm->lsm_stripe_count);
715         rc = -EFBIG;
716 out:
717         if (pool != NULL) {
718                 up_read(&pool_tgt_rw_sem(pool));
719                 /* put back ref got by lov_find_pool() */
720                 lov_pool_putref(pool);
721         }
722         RETURN(rc);
723 }
724
725 /* Alloc objects on osts with optimization based on:
726    - free space
727    - network resources (shared OSS's)
728 */
729 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt,
730                      char *poolname, int flags)
731 {
732         struct lov_obd *lov = &exp->exp_obd->u.lov;
733         static time_t last_warn = 0;
734         time_t now = cfs_time_current_sec();
735         __u64 total_bavail, total_weight = 0;
736         int nfound, good_osts, i, warn = 0, rc = 0;
737         int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
738         struct pool_desc *pool;
739         struct ost_pool *osts;
740         struct lov_qos_rr *lqr;
741         ENTRY;
742
743         if (stripe_cnt_min < 1)
744                 RETURN(-EINVAL);
745
746         pool = lov_find_pool(lov, poolname);
747         if (pool == NULL) {
748                 osts = &(lov->lov_packed);
749                 lqr = &(lov->lov_qos.lq_rr);
750         } else {
751                 down_read(&pool_tgt_rw_sem(pool));
752                 osts = &(pool->pool_obds);
753                 lqr = &(pool->pool_rr);
754         }
755
756         obd_getref(exp->exp_obd);
757         /* wait for fresh statfs info if needed, the rpcs are sent in
758          * lov_create() */
759         qos_statfs_update(exp->exp_obd,
760                           cfs_time_shift_64(-2 * lov->desc.ld_qos_maxage), 1);
761
762         down_write(&lov->lov_qos.lq_rw_sem);
763
764         if (lov->desc.ld_active_tgt_count < 2)
765                 GOTO(out, rc = -EAGAIN);
766
767         rc = qos_calc_ppo(exp->exp_obd);
768         if (rc)
769                 GOTO(out, rc);
770
771         total_bavail = 0;
772         good_osts = 0;
773         /* Warn users about zero available space/inode every 30 min */
774         if (cfs_time_sub(now, last_warn) > 60 * 30)
775                 warn = 1;
776         /* Find all the OSTs that are valid stripe candidates */
777         for (i = 0; i < osts->op_count; i++) {
778                 __u64 bavail;
779
780                 if (!lov->lov_tgts[osts->op_array[i]] ||
781                     !lov->lov_tgts[osts->op_array[i]]->ltd_active)
782                         continue;
783                 bavail = TGT_BAVAIL(osts->op_array[i]);
784                 if (!bavail) {
785                         if (warn) {
786                                 CDEBUG(D_QOS, "no free space on %s\n",
787                                      obd_uuid2str(&lov->lov_tgts[osts->op_array[i]]->ltd_uuid));
788                                 last_warn = now;
789                         }
790                         continue;
791                 }
792                 if (!TGT_FFREE(osts->op_array[i])) {
793                         if (warn) {
794                                 CDEBUG(D_QOS, "no free inodes on %s\n",
795                                      obd_uuid2str(&lov->lov_tgts[osts->op_array[i]]->ltd_uuid));
796                                 last_warn = now;
797                         }
798                         continue;
799                 }
800
801                 /* Fail Check before osc_precreate() is called
802                    so we can only 'fail' single OSC. */
803                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && osts->op_array[i] == 0)
804                         continue;
805
806                 if (obd_precreate(lov->lov_tgts[osts->op_array[i]]->ltd_exp) > 2)
807                         continue;
808
809                 lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable = 1;
810                 qos_calc_weight(lov, osts->op_array[i]);
811                 total_bavail += bavail;
812                 total_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
813
814                 good_osts++;
815         }
816
817 #ifdef QOS_DEBUG
818         CDEBUG(D_QOS, "found %d good osts\n", good_osts);
819 #endif
820
821         if (good_osts < stripe_cnt_min)
822                 GOTO(out, rc = -EAGAIN);
823
824         if (!total_bavail)
825                 GOTO(out, rc = -ENOSPC);
826
827         /* We have enough osts */
828         if (good_osts < *stripe_cnt)
829                 *stripe_cnt = good_osts;
830
831         /* Find enough OSTs with weighted random allocation. */
832         nfound = 0;
833         while (nfound < *stripe_cnt) {
834                 __u64 rand, cur_weight;
835
836                 cur_weight = 0;
837                 rc = -ENODEV;
838
839                 if (total_weight) {
840 #if BITS_PER_LONG == 32
841                         rand = ll_rand() % (unsigned)total_weight;
842                         /* If total_weight > 32-bit, first generate the high
843                          * 32 bits of the random number, then add in the low
844                          * 32 bits (truncated to the upper limit, if needed) */
845                         if (total_weight > 0xffffffffULL)
846                                 rand = (__u64)(ll_rand() %
847                                           (unsigned)(total_weight >> 32)) << 32;
848                         else
849                                 rand = 0;
850
851                         if (rand == (total_weight & 0xffffffff00000000ULL))
852                                 rand |= ll_rand() % (unsigned)total_weight;
853                         else
854                                 rand |= ll_rand();
855
856 #else
857                         rand = ((__u64)ll_rand() << 32 | ll_rand()) %
858                                 total_weight;
859 #endif
860                 } else {
861                         rand = 0;
862                 }
863
864                 /* On average, this will hit larger-weighted osts more often.
865                    0-weight osts will always get used last (only when rand=0).*/
866                 for (i = 0; i < osts->op_count; i++) {
867                         if (!lov->lov_tgts[osts->op_array[i]] ||
868                             !lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable)
869                                 continue;
870
871                         cur_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
872 #ifdef QOS_DEBUG
873                         CDEBUG(D_QOS, "stripe_cnt=%d nfound=%d cur_weight="LPU64
874                                       " rand="LPU64" total_weight="LPU64"\n",
875                                *stripe_cnt, nfound, cur_weight, rand, total_weight);
876 #endif
877                         if (cur_weight >= rand) {
878 #ifdef QOS_DEBUG
879                                 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
880                                        nfound, osts->op_array[i]);
881 #endif
882                                 idx_arr[nfound++] = osts->op_array[i];
883                                 qos_used(lov, osts, osts->op_array[i], &total_weight);
884                                 rc = 0;
885                                 break;
886                         }
887                 }
888                 if (rc) {
889                         CDEBUG(D_QOS, "Didn't find any OSTs? Reduce total weight\n");
890                         if (total_weight == 0)
891                                 break;
892                         else
893                                 total_weight = 0;
894                 }
895         }
896
897         LASSERT(nfound == *stripe_cnt);
898
899 out:
900         up_write(&lov->lov_qos.lq_rw_sem);
901
902         if (pool != NULL) {
903                 up_read(&pool_tgt_rw_sem(pool));
904                 /* put back ref got by lov_find_pool() */
905                 lov_pool_putref(pool);
906         }
907
908         if (rc == -EAGAIN)
909                 rc = alloc_rr(lov, idx_arr, stripe_cnt, poolname, flags);
910
911         obd_putref(exp->exp_obd);
912         RETURN(rc);
913 }
914
915 /* return new alloced stripe count on success */
916 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
917                            int newea, int **idx_arr, int *arr_cnt, int flags)
918 {
919         struct lov_obd *lov = &exp->exp_obd->u.lov;
920         int stripe_cnt = lsm->lsm_stripe_count;
921         int i, rc = 0;
922         int *tmp_arr = NULL;
923         ENTRY;
924
925         *arr_cnt = stripe_cnt;
926         OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
927         if (tmp_arr == NULL)
928                 RETURN(-ENOMEM);
929         for (i = 0; i < *arr_cnt; i++)
930                 tmp_arr[i] = -1;
931
932         if (newea ||
933             lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
934                 rc = alloc_qos(exp, tmp_arr, &stripe_cnt,
935                                lsm->lsm_pool_name, flags);
936         else
937                 rc = alloc_specific(lov, lsm, tmp_arr);
938
939         if (rc)
940                 GOTO(out_arr, rc);
941
942         *idx_arr = tmp_arr;
943         RETURN(stripe_cnt);
944 out_arr:
945         OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
946         *arr_cnt = 0;
947         RETURN(rc);
948 }
949
950 static void free_idx_array(int *idx_arr, int arr_cnt)
951 {
952         if (arr_cnt)
953                 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
954 }
955
956 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
957 {
958         struct lov_obd *lov = &exp->exp_obd->u.lov;
959         struct lov_stripe_md *lsm;
960         struct obdo *src_oa = set->set_oi->oi_oa;
961         struct obd_trans_info *oti = set->set_oti;
962         int i, stripes, rc = 0, newea = 0;
963         int flag = LOV_USES_ASSIGNED_STRIPE;
964         int *idx_arr = NULL, idx_cnt = 0;
965         ENTRY;
966
967         LASSERT(src_oa->o_valid & OBD_MD_FLID);
968
969         if (set->set_oi->oi_md == NULL) {
970                 int stripes_def = lov_get_stripecnt(lov, 0);
971
972                 /* If the MDS file was truncated up to some size, stripe over
973                  * enough OSTs to allow the file to be created at that size.
974                  * This may mean we use more than the default # of stripes. */
975                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
976                         obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
977
978                         /* Find a small number of stripes we can use
979                            (up to # of active osts). */
980                         stripes = 1;
981                         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
982                                 if (!lov->lov_tgts[i] ||
983                                     !lov->lov_tgts[i]->ltd_active)
984                                         continue;
985                                 min_bavail = min(min_bavail, TGT_BAVAIL(i));
986                                 if (min_bavail * stripes > src_oa->o_size)
987                                         break;
988                                 stripes++;
989                         }
990
991                         if (stripes < stripes_def)
992                                 stripes = stripes_def;
993                 } else {
994                          flag = LOV_USES_DEFAULT_STRIPE;
995                          stripes = stripes_def;
996                 }
997
998                 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
999                                      lov->desc.ld_pattern ?
1000                                      lov->desc.ld_pattern : LOV_PATTERN_RAID0,
1001                                      LOV_MAGIC);
1002                 if (rc < 0)
1003                         GOTO(out_err, rc);
1004                 newea = 1;
1005                 rc = 0;
1006         }
1007
1008         lsm = set->set_oi->oi_md;
1009         lsm->lsm_object_id = src_oa->o_id;
1010         if (!lsm->lsm_stripe_size)
1011                 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
1012         if (!lsm->lsm_pattern) {
1013                 LASSERT(lov->desc.ld_pattern);
1014                 lsm->lsm_pattern = lov->desc.ld_pattern;
1015         }
1016
1017         stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt, flag);
1018         if (stripes <= 0)
1019                 GOTO(out_err, rc = stripes ? stripes : -EIO);
1020         LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
1021                  lsm->lsm_stripe_count, stripes);
1022
1023         for (i = 0; i < stripes; i++) {
1024                 struct lov_request *req;
1025                 int ost_idx = idx_arr[i];
1026                 LASSERT(ost_idx >= 0);
1027
1028                 OBD_ALLOC(req, sizeof(*req));
1029                 if (req == NULL)
1030                         GOTO(out_err, rc = -ENOMEM);
1031                 lov_set_add_req(req, set);
1032
1033                 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
1034                 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
1035                 if (req->rq_oi.oi_md == NULL)
1036                         GOTO(out_err, rc = -ENOMEM);
1037
1038                 OBDO_ALLOC(req->rq_oi.oi_oa);
1039                 if (req->rq_oi.oi_oa == NULL)
1040                         GOTO(out_err, rc = -ENOMEM);
1041
1042                 req->rq_idx = ost_idx;
1043                 req->rq_stripe = i;
1044                 /* create data objects with "parent" OA */
1045                 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
1046                 req->rq_oi.oi_cb_up = cb_create_update;
1047
1048                 /* XXX When we start creating objects on demand, we need to
1049                  *     make sure that we always create the object on the
1050                  *     stripe which holds the existing file size.
1051                  */
1052                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1053                         req->rq_oi.oi_oa->o_size =
1054                                 lov_size_to_stripe(lsm, src_oa->o_size, i);
1055
1056                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1057                                i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
1058                 }
1059         }
1060         LASSERT(set->set_count == stripes);
1061
1062         if (stripes < lsm->lsm_stripe_count)
1063                 qos_shrink_lsm(set);
1064         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LOV_PREP_CREATE)) {
1065                 qos_shrink_lsm(set);
1066                 rc = -EIO;
1067         }
1068
1069         if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
1070                 oti_alloc_cookies(oti, set->set_count);
1071                 if (!oti->oti_logcookies)
1072                         GOTO(out_err, rc = -ENOMEM);
1073                 set->set_cookies = oti->oti_logcookies;
1074         }
1075 out_err:
1076         if (newea && rc)
1077                 obd_free_memmd(exp, &set->set_oi->oi_md);
1078         if (idx_arr)
1079                 free_idx_array(idx_arr, idx_cnt);
1080         EXIT;
1081         return rc;
1082 }
1083
1084 void qos_update(struct lov_obd *lov)
1085 {
1086         ENTRY;
1087         lov->lov_qos.lq_dirty = 1;
1088 }
1089
1090 void qos_statfs_done(struct lov_obd *lov)
1091 {
1092         LASSERT(lov->lov_qos.lq_statfs_in_progress);
1093         down_write(&lov->lov_qos.lq_rw_sem);
1094         lov->lov_qos.lq_statfs_in_progress = 0;
1095         /* wake up any threads waiting for the statfs rpcs to complete */
1096         cfs_waitq_signal(&lov->lov_qos.lq_statfs_waitq);
1097         up_write(&lov->lov_qos.lq_rw_sem);
1098 }
1099
1100 static int qos_statfs_ready(struct obd_device *obd, __u64 max_age)
1101 {
1102         struct lov_obd         *lov = &obd->u.lov;
1103         int rc;
1104         ENTRY;
1105         down_read(&lov->lov_qos.lq_rw_sem);
1106         rc = lov->lov_qos.lq_statfs_in_progress == 0 ||
1107              cfs_time_beforeq_64(max_age, obd->obd_osfs_age);
1108         up_read(&lov->lov_qos.lq_rw_sem);
1109         RETURN(rc);
1110 }
1111
1112 /*
1113  * Update statfs data if the current osfs age is older than max_age.
1114  * If wait is not set, it means that we are called from lov_create()
1115  * and we should just issue the rpcs without waiting for them to complete.
1116  * If wait is set, we are called from alloc_qos() and we just have
1117  * to wait for the request set to complete.
1118  */
1119 void qos_statfs_update(struct obd_device *obd, __u64 max_age, int wait)
1120 {
1121         struct lov_obd         *lov = &obd->u.lov;
1122         struct obd_info        *oinfo;
1123         int                     rc = 0;
1124         struct ptlrpc_request_set *set = NULL;
1125         ENTRY;
1126
1127         if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
1128                 /* statfs data are quite recent, don't need to refresh it */
1129                 RETURN_EXIT;
1130
1131         if (!wait && lov->lov_qos.lq_statfs_in_progress)
1132                 /* statfs already in progress */
1133                 RETURN_EXIT;
1134
1135         down_write(&lov->lov_qos.lq_rw_sem);
1136         if (lov->lov_qos.lq_statfs_in_progress) {
1137                 up_write(&lov->lov_qos.lq_rw_sem);
1138                 GOTO(out, rc = 0);
1139         }
1140         /* no statfs in flight, send rpcs */
1141         lov->lov_qos.lq_statfs_in_progress = 1;
1142         up_write(&lov->lov_qos.lq_rw_sem);
1143
1144         if (wait)
1145                 CDEBUG(D_QOS, "%s: did not manage to get fresh statfs data "
1146                        "in a timely manner (osfs age "LPU64", max age "LPU64")"
1147                        ", sending new statfs rpcs\n",
1148                        obd_uuid2str(&lov->desc.ld_uuid), obd->obd_osfs_age,
1149                        max_age);
1150
1151         /* need to send statfs rpcs */
1152         CDEBUG(D_QOS, "sending new statfs requests\n");
1153         memset(lov->lov_qos.lq_statfs_data, 0,
1154                sizeof(*lov->lov_qos.lq_statfs_data));
1155         oinfo = &lov->lov_qos.lq_statfs_data->lsd_oi;
1156         oinfo->oi_osfs = &lov->lov_qos.lq_statfs_data->lsd_statfs;
1157         oinfo->oi_flags = OBD_STATFS_NODELAY;
1158         set = ptlrpc_prep_set();
1159         if (!set)
1160                 GOTO(out_failed, rc = -ENOMEM);
1161
1162         rc = obd_statfs_async(obd, oinfo, max_age, set);
1163         if (rc || list_empty(&set->set_requests)) {
1164                 if (rc)
1165                         CWARN("statfs failed with %d\n", rc);
1166                 GOTO(out_failed, rc);
1167         }
1168         /* send requests via ptlrpcd */
1169         oinfo->oi_flags |= OBD_STATFS_PTLRPCD;
1170         ptlrpcd_add_rqset(set);
1171         GOTO(out, rc);
1172
1173 out_failed:
1174         down_write(&lov->lov_qos.lq_rw_sem);
1175         lov->lov_qos.lq_statfs_in_progress = 0;
1176         /* wake up any threads waiting for the statfs rpcs to complete */
1177         cfs_waitq_signal(&lov->lov_qos.lq_statfs_waitq);
1178         up_write(&lov->lov_qos.lq_rw_sem);
1179         wait = 0;
1180 out:
1181         if (set)
1182                 ptlrpc_set_destroy(set);
1183         if (wait) {
1184                 struct l_wait_info lwi = { 0 };
1185                 CDEBUG(D_QOS, "waiting for statfs requests to complete\n");
1186                 l_wait_event(lov->lov_qos.lq_statfs_waitq,
1187                              qos_statfs_ready(obd, max_age), &lwi);
1188                 if (cfs_time_before_64(obd->obd_osfs_age, max_age))
1189                         CDEBUG(D_QOS, "%s: still no fresh statfs data after "
1190                                       "waiting (osfs age "LPU64", max age "
1191                                       LPU64")\n",
1192                                       obd_uuid2str(&lov->desc.ld_uuid),
1193                                       obd->obd_osfs_age, max_age);
1194         }
1195 }