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