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