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