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