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