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