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