Whamcloud - gitweb
land b1_5 onto HEAD
[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                
41 /* #define QOS_DEBUG 1 */
42 #define D_QOS D_OTHER
43
44 #define TGT_BAVAIL(i)  (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail * \
45                         lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize) 
46 #define TGT_FFREE(i)   (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
47
48
49 int qos_add_tgt(struct obd_device *obd, __u32 index)
50 {
51         struct lov_obd *lov = &obd->u.lov;
52         struct lov_qos_oss *oss, *temposs;
53         struct obd_export *exp = lov->lov_tgts[index]->ltd_exp;
54         int rc = 0, found = 0;
55         ENTRY;
56
57         /* We only need this QOS struct on MDT, not clients - but we may not
58            have registered the lov's observer yet, so there's no way to check
59            here. */
60                             
61         if (!exp || !exp->exp_connection) {
62                 CERROR("Missing connection\n");
63                 RETURN(-ENOTCONN);
64         }
65
66         down_write(&lov->lov_qos.lq_rw_sem);
67         mutex_down(&lov->lov_lock);
68         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
69                 if (obd_uuid_equals(&oss->lqo_uuid, 
70                                     &exp->exp_connection->c_remote_uuid)) {
71                         found++;
72                         break;
73                 }
74         }
75
76         if (!found) {
77                 OBD_ALLOC_PTR(oss);
78                 if (!oss) 
79                         GOTO(out, rc = -ENOMEM);
80                 memcpy(&oss->lqo_uuid,
81                        &exp->exp_connection->c_remote_uuid,
82                        sizeof(oss->lqo_uuid));
83         } else {
84                 /* Assume we have to move this one */
85                 list_del(&oss->lqo_oss_list);
86         }
87                         
88         oss->lqo_ost_count++;
89         lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
90
91         /* Add sorted by # of OSTs.  Find the first entry that we're
92            bigger than... */
93         list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
94                 if (oss->lqo_ost_count > temposs->lqo_ost_count) 
95                         break;
96         }
97         /* ...and add before it.  If we're the first or smallest, temposs
98            points to the list head, and we add to the end. */
99         list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
100
101         lov->lov_qos.lq_dirty = 1;
102         lov->lov_qos.lq_dirty_rr = 1;
103
104         CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n", 
105                obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
106                obd_uuid2str(&oss->lqo_uuid),
107                oss->lqo_ost_count);
108
109 out:
110         mutex_up(&lov->lov_lock);
111         up_write(&lov->lov_qos.lq_rw_sem);
112         RETURN(rc);
113 }
114
115 int qos_del_tgt(struct obd_device *obd, __u32 index)
116 {
117         struct lov_obd *lov = &obd->u.lov;
118         struct lov_qos_oss *oss;
119         int rc = 0;
120         ENTRY;
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                         lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
207         }
208         
209         /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
210         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
211                 temp = oss->lqo_bavail >> 1;
212                 do_div(temp, oss->lqo_ost_count * num_active);
213                 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
214                 if (lov->lov_qos.lq_reset == 0) 
215                         oss->lqo_penalty = 0;
216         }
217
218         lov->lov_qos.lq_dirty = 0;
219         lov->lov_qos.lq_reset = 0;
220
221         /* If each ost has almost same free space, 
222          * do rr allocation for better creation performance */
223         lov->lov_qos.lq_same_space = 0;
224         temp = ba_max - ba_min;
225         ba_min = (ba_min * 51) >> 8;     /* 51/256 = .20 */  
226         if (temp < ba_min) {
227                 /* Difference is less than 20% */ 
228                 lov->lov_qos.lq_same_space = 1;
229                 /* Reset weights for the next time we enter qos mode */
230                 lov->lov_qos.lq_reset = 0;
231         }
232         rc = 0;
233
234 out:
235         if (!rc && lov->lov_qos.lq_same_space)
236                 RETURN(-EAGAIN);
237         RETURN(rc);
238 }
239
240 static int qos_calc_weight(struct lov_obd *lov, int i)
241 {
242         __u64 temp, temp2;
243         
244         /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
245         temp = TGT_BAVAIL(i);
246         temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty + 
247                 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
248         if (temp < temp2) 
249                 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
250         else
251                 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
252         return 0;
253 }
254
255 /* We just used this index for a stripe; adjust everyone's weights */
256 static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
257 {
258         struct lov_qos_oss *oss;
259         int i;
260         ENTRY;
261
262         /* Don't allocate from this stripe anymore, until the next alloc_qos */
263         lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
264
265         oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
266         
267         /* Decay old penalty by half (we're adding max penalty, and don't
268            want it to run away.) */
269         lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
270         oss->lqo_penalty >>= 1;
271
272         /* Set max penalties for this OST and OSS */
273         lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
274                 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
275                 lov->desc.ld_active_tgt_count;
276         oss->lqo_penalty += oss->lqo_penalty_per_obj * 
277                 lov->lov_qos.lq_active_oss_count;
278         
279         /* Decrease all OSS penalties */
280         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
281                 if (oss->lqo_penalty < oss->lqo_penalty_per_obj) 
282                         oss->lqo_penalty = 0;
283                 else
284                         oss->lqo_penalty -= oss->lqo_penalty_per_obj;
285         }
286
287         *total_wt = 0;
288         /* Decrease all OST penalties */
289         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
290                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) 
291                         continue;
292                 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
293                     lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
294                         lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
295                 else
296                         lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
297                         lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
298                 
299                 qos_calc_weight(lov, i);
300
301                 /* Recalc the total weight of usable osts */
302                 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
303                         *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
304
305 #ifdef QOS_DEBUG
306                 CDEBUG(D_QOS, "recalc tgt %d avail="LPU64
307                        " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
308                        " ossp="LPU64" wt="LPU64"\n",
309                        i, TGT_BAVAIL(i)>>10, 
310                        lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj>>10,
311                        lov->lov_tgts[i]->ltd_qos.ltq_penalty>>10, 
312                        lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
313                        lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty>>10,
314                        lov->lov_tgts[i]->ltd_qos.ltq_weight>>10);
315 #endif
316         }
317
318         RETURN(0);
319 }
320
321 #define LOV_QOS_EMPTY ((__u32)-1)
322 /* compute optimal round-robin order, based on OSTs per OSS */
323 static int qos_calc_rr(struct lov_obd *lov)
324 {
325         struct lov_qos_oss *oss;
326         unsigned ost_count, placed;
327         int i;
328         ENTRY;
329
330         if (!lov->lov_qos.lq_dirty_rr) {
331                 LASSERT(lov->lov_qos.lq_rr_size);
332                 RETURN(0);
333         }
334
335         down_write(&lov->lov_qos.lq_rw_sem);
336         ost_count = lov->desc.ld_tgt_count;
337
338         if (lov->lov_qos.lq_rr_size) 
339                 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
340         lov->lov_qos.lq_rr_size = ost_count * 
341                 sizeof(lov->lov_qos.lq_rr_array[0]);
342         OBD_ALLOC(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
343         if (!lov->lov_qos.lq_rr_array) {
344                 lov->lov_qos.lq_rr_size = 0;
345                 up_write(&lov->lov_qos.lq_rw_sem);
346                 RETURN(-ENOMEM);
347         }
348
349         for (i = 0; i < ost_count; i++) 
350                 lov->lov_qos.lq_rr_array[i] = LOV_QOS_EMPTY;
351
352         /* Place all the OSTs from 1 OSS at the same time. */
353         placed = 0;
354         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
355                 int j = 0;
356                 for (i = 0; i < ost_count; i++) {
357                       if (lov->lov_tgts[i]->ltd_qos.ltq_oss == oss) {
358                               /* Evenly space these OSTs across arrayspace */
359                               int next = j * ost_count / oss->lqo_ost_count;
360                               while (lov->lov_qos.lq_rr_array[next] !=
361                                      LOV_QOS_EMPTY)
362                                       next = (next + 1) % ost_count;
363                               lov->lov_qos.lq_rr_array[next] = i;
364                               j++;
365                               placed++;
366                       }
367                 }
368                 LASSERT(j == oss->lqo_ost_count);
369         }
370
371         lov->lov_qos.lq_dirty_rr = 0;
372         up_write(&lov->lov_qos.lq_rw_sem);
373
374         if (placed != ost_count) {
375                 /* This should never happen */
376                 LCONSOLE_ERROR("Failed to place all OSTs in the round-robin "
377                                "list (%d of %d).\n", placed, ost_count);
378                 for (i = 0; i < ost_count; i++) {
379                         LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
380                                  lov->lov_qos.lq_rr_array[i]);
381                 }
382                 lov->lov_qos.lq_dirty_rr = 1;
383                 RETURN(-EAGAIN);
384         }
385         
386 #ifdef QOS_DEBUG
387         for (i = 0; i < ost_count; i++) {
388                 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i, 
389                          lov->lov_qos.lq_rr_array[i]);
390         }
391 #endif
392
393         RETURN(0);
394 }
395
396
397 void qos_shrink_lsm(struct lov_request_set *set)
398 {
399         struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
400         /* XXX LOV STACKING call into osc for sizes */
401         unsigned oldsize, newsize;
402
403         if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
404                 struct llog_cookie *cookies;
405                 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
406                 newsize = set->set_count * sizeof(*cookies);
407
408                 cookies = set->set_cookies;
409                 oti_alloc_cookies(set->set_oti, set->set_count);
410                 if (set->set_oti->oti_logcookies) {
411                         memcpy(set->set_oti->oti_logcookies, cookies, newsize);
412                         OBD_FREE(cookies, oldsize);
413                         set->set_cookies = set->set_oti->oti_logcookies;
414                 } else {
415                         CWARN("'leaking' %d bytes\n", oldsize - newsize);
416                 }
417         }
418
419         CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
420               lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
421
422         oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
423         newsize = lov_stripe_md_size(set->set_count);
424         OBD_ALLOC(lsm_new, newsize);
425         if (lsm_new != NULL) {
426                 memcpy(lsm_new, lsm, newsize);
427                 lsm_new->lsm_stripe_count = set->set_count;
428                 OBD_FREE(lsm, oldsize);
429                 set->set_oi->oi_md = lsm_new;
430         } else {
431                 CWARN("'leaking' %d bytes\n", oldsize - newsize);
432         }
433 }
434
435 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
436 {
437         struct lov_stripe_md *lsm = set->set_oi->oi_md;
438         struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
439         unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
440         int stripe, i, rc = -EIO;
441         ENTRY;
442
443         ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
444         for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
445                 if (!lov->lov_tgts[ost_idx] || 
446                     !lov->lov_tgts[ost_idx]->ltd_active) 
447                         continue;
448                 /* check if objects has been created on this ost */
449                 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
450                         if (stripe == req->rq_stripe)
451                                 continue;
452                         if (ost_idx == lsm->lsm_oinfo[stripe].loi_ost_idx)
453                                 break;
454                 }
455
456                 if (stripe >= lsm->lsm_stripe_count) {
457                         req->rq_idx = ost_idx;
458                         rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
459                                         req->rq_oi.oi_oa, &req->rq_oi.oi_md,
460                                         set->set_oti);
461                         if (!rc)
462                                 break;
463                 }
464         }
465         RETURN(rc);
466 }
467
468 #define LOV_CREATE_RESEED_MULT 4
469 #define LOV_CREATE_RESEED_MIN  1000
470 /* Allocate objects on osts with round-robin algorithm */
471 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt)
472 {
473         unsigned array_idx, ost_count = lov->desc.ld_tgt_count;
474         unsigned ost_active_count = lov->desc.ld_active_tgt_count;
475         int i, *idx_pos = idx_arr;
476         __u32 ost_idx;
477         ENTRY;
478
479         i = qos_calc_rr(lov);
480         if (i) 
481                 RETURN(i);
482
483         if (--lov->lov_start_count <= 0) {
484                 lov->lov_start_idx = ll_rand() % ost_count;
485                 lov->lov_start_count =
486                         (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) +
487                          LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U);
488         } else if (*stripe_cnt >= ost_active_count || 
489                    lov->lov_start_idx > ost_count) {
490                 /* If we have allocated from all of the OSTs, slowly
491                    precess the next start */
492                 lov->lov_start_idx %= ost_count;
493                 ++lov->lov_offset_idx;
494         }
495         array_idx = (lov->lov_start_idx + lov->lov_offset_idx) % ost_count;
496 #ifdef QOS_DEBUG
497         CDEBUG(D_QOS, "want %d startidx %d startcnt %d offset %d arrayidx %d\n",
498                *stripe_cnt, lov->lov_start_idx, lov->lov_start_count,
499                lov->lov_offset_idx, array_idx);
500 #endif
501
502         down_read(&lov->lov_qos.lq_rw_sem);
503         for (i = 0; i < ost_count; i++, array_idx=(array_idx + 1) % ost_count) {
504                 ++lov->lov_start_idx;
505                 ost_idx = lov->lov_qos.lq_rr_array[array_idx];
506 #ifdef QOS_DEBUG
507                 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
508                        i, lov->lov_start_idx, 
509                        lov->lov_tgts[ost_idx] ? 
510                        lov->lov_tgts[ost_idx]->ltd_active : 0,
511                        idx_pos - idx_arr, array_idx, ost_idx);
512 #endif
513                 if (!lov->lov_tgts[ost_idx] || 
514                     !lov->lov_tgts[ost_idx]->ltd_active) 
515                         continue;
516                 *idx_pos = ost_idx;
517                 idx_pos++;
518                 /* We have enough stripes */
519                 if (idx_pos - idx_arr == *stripe_cnt) 
520                         break;
521         }
522         up_read(&lov->lov_qos.lq_rw_sem);
523
524         *stripe_cnt = idx_pos - idx_arr;
525         RETURN(0);
526 }
527
528 /* alloc objects on osts with specific stripe offset */
529 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
530                           int *idx_arr)
531 {
532         unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
533         int i, *idx_pos = idx_arr;
534         ENTRY;
535
536         ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
537         for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
538                 if (!lov->lov_tgts[ost_idx] || 
539                     !lov->lov_tgts[ost_idx]->ltd_active) {
540                         continue;
541                 }
542                 *idx_pos = ost_idx;
543                 idx_pos++;
544                 /* got enough ost */
545                 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
546                         RETURN(0);
547         }
548         /* If we were passed specific striping params, then a failure to
549          * meet those requirements is an error, since we can't reallocate
550          * that memory (it might be part of a larger array or something).
551          *
552          * We can only get here if lsm_stripe_count was originally > 1.
553          */
554         CERROR("can't lstripe objid "LPX64": have "LPSZ" want %u\n",
555                lsm->lsm_object_id, idx_pos - idx_arr, lsm->lsm_stripe_count);
556         RETURN(-EFBIG);
557 }
558
559 /* Alloc objects on osts with optimization based on:
560    - free space
561    - network resources (shared OSS's)
562 */
563 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
564 {
565         struct lov_obd *lov = &exp->exp_obd->u.lov;
566         static time_t last_warn = 0;
567         time_t now = cfs_time_current_sec();
568         __u64 total_bavail, total_weight = 0;
569         __u32 ost_count;
570         int nfound, good_osts, i, warn = 0, rc = 0;
571         ENTRY;
572
573         lov_getref(exp->exp_obd);
574         down_write(&lov->lov_qos.lq_rw_sem);
575
576         ost_count = lov->desc.ld_tgt_count;
577
578         if (lov->desc.ld_active_tgt_count < 2)
579                 GOTO(out, rc = -EAGAIN);
580
581         rc = qos_calc_ppo(exp->exp_obd);
582         if (rc)
583                 GOTO(out, rc);
584
585         total_bavail = 0;
586         good_osts = 0;
587         /* Warn users about zero available space/inode every 30 min */
588         if (cfs_time_sub(now, last_warn) > 60 * 30)
589                 warn = 1;
590         /* Find all the OSTs that are valid stripe candidates */
591         for (i = 0; i < ost_count; i++) {
592                 __u64 bavail;
593
594                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
595                         continue;
596                 bavail = TGT_BAVAIL(i);
597                 if (!bavail) {
598                         if (warn) {
599                                 CDEBUG(D_QOS, "no free space on %s\n",
600                                      obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
601                                 last_warn = now;
602                         }
603                         continue;
604                 }
605                 if (!TGT_FFREE(i)) {
606                         if (warn) {
607                                 CDEBUG(D_QOS, "no free inodes on %s\n",
608                                      obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
609                                 last_warn = now;
610                         }
611                         continue;
612                 }
613
614                 lov->lov_tgts[i]->ltd_qos.ltq_usable = 1;
615                 qos_calc_weight(lov, i);
616                 total_bavail += bavail;
617                 total_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
618
619                 good_osts++;
620         }
621
622         if (!total_bavail)
623                 GOTO(out, rc = -ENOSPC);
624
625         /* if we don't have enough good OSTs, we reduce the stripe count. */
626         if (good_osts < *stripe_cnt)
627                 *stripe_cnt = good_osts;
628
629         if (!*stripe_cnt)
630                 GOTO(out, rc = -EAGAIN);
631
632         /* Find enough OSTs with weighted random allocation. */
633         nfound = 0;
634         while (nfound < *stripe_cnt) {
635                 __u64 rand, cur_weight;
636
637                 cur_weight = 0;
638                 rc = -ENODEV;
639
640                 if (total_weight) {
641 #if BITS_PER_LONG == 32
642                         rand = ll_rand() % (unsigned)total_weight;
643                         /* If total_weight > 32-bit, first generate the high
644                          * 32 bits of the random number, then add in the low
645                          * 32 bits (truncated to the upper limit, if needed) */
646                         if (total_weight > 0xffffffffULL)
647                                 rand = (__u64)(ll_rand() %
648                                           (unsigned)(total_weight >> 32)) << 32;
649                         else
650                                 rand = 0;
651
652                         if (rand == (total_weight & 0xffffffff00000000ULL))
653                                 rand |= ll_rand() % (unsigned)total_weight;
654                         else
655                                 rand |= ll_rand();
656
657 #else
658                         rand = ((__u64)ll_rand() << 32 | ll_rand()) %
659                                 total_weight;
660 #endif
661                 } else {
662                         rand = 0;
663                 }
664
665                 /* On average, this will hit larger-weighted osts more often.
666                    0-weight osts will always get used last (only when rand=0).*/
667                 for (i = 0; i < ost_count; i++) {
668                         if (!lov->lov_tgts[i]->ltd_qos.ltq_usable)
669                                 continue;
670                         cur_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
671                         if (cur_weight >= rand) {
672 #ifdef QOS_DEBUG
673                                 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
674                                        nfound, i);
675 #endif
676                                 idx_arr[nfound++] = i;
677                                 qos_used(lov, i, &total_weight);
678                                 rc = 0;
679                                 break;
680                         }
681                 }
682                 /* should never satisfy below condition */
683                 if (rc) {
684                         CERROR("Didn't find any OSTs?\n");
685                         break;
686                 }
687         }
688         LASSERT(nfound == *stripe_cnt);
689
690 out:
691         up_write(&lov->lov_qos.lq_rw_sem);
692
693         if (rc == -EAGAIN)
694                 rc = alloc_rr(lov, idx_arr, stripe_cnt);
695
696         lov_putref(exp->exp_obd);
697         RETURN(rc);
698 }
699
700 /* return new alloced stripe count on success */
701 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm, 
702                            int newea, int **idx_arr, int *arr_cnt)
703 {
704         struct lov_obd *lov = &exp->exp_obd->u.lov;
705         int stripe_cnt = lsm->lsm_stripe_count;
706         int i, rc = 0;
707         int *tmp_arr = NULL;
708         ENTRY;
709
710         *arr_cnt = stripe_cnt;
711         OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
712         if (tmp_arr == NULL)
713                 RETURN(-ENOMEM);
714         for (i = 0; i < *arr_cnt; i++)
715                 tmp_arr[i] = -1;
716
717         if (newea || 
718             lsm->lsm_oinfo[0].loi_ost_idx >= lov->desc.ld_tgt_count) 
719                 rc = alloc_qos(exp, tmp_arr, &stripe_cnt);
720         else
721                 rc = alloc_specific(lov, lsm, tmp_arr);
722
723         if (rc)
724                 GOTO(out_arr, rc);
725
726         *idx_arr = tmp_arr;
727         RETURN(stripe_cnt);
728 out_arr:
729         OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
730         *arr_cnt = 0;
731         RETURN(rc);
732 }
733
734 static void free_idx_array(int *idx_arr, int arr_cnt)
735 {
736         if (arr_cnt)
737                 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
738 }
739
740 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
741 {
742         struct lov_obd *lov = &exp->exp_obd->u.lov;
743         struct lov_stripe_md *lsm;
744         struct obdo *src_oa = set->set_oi->oi_oa;
745         struct obd_trans_info *oti = set->set_oti;
746         int i, stripes, rc = 0, newea = 0;
747         int *idx_arr, idx_cnt = 0;
748         ENTRY;
749
750         LASSERT(src_oa->o_valid & OBD_MD_FLID);
751
752         if (set->set_oi->oi_md == NULL) {
753                 int stripe_cnt = lov_get_stripecnt(lov, 0);
754
755                 /* If the MDS file was truncated up to some size, stripe over
756                  * enough OSTs to allow the file to be created at that size.
757                  * This may mean we use more than the default # of stripes. */
758                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
759                         obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
760
761                         /* Find a small number of stripes we can use
762                            (up to # of active osts). */
763                         stripes = 1;
764                         lov_getref(exp->exp_obd);
765                         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
766                                 if (!lov->lov_tgts[i] || 
767                                     !lov->lov_tgts[i]->ltd_active)
768                                         continue;
769                                 min_bavail = min(min_bavail, TGT_BAVAIL(i));
770                                 if (min_bavail * stripes > src_oa->o_size)
771                                         break;
772                                 stripes++;
773                         }
774                         lov_putref(exp->exp_obd);
775
776                         if (stripes < stripe_cnt)
777                                 stripes = stripe_cnt;
778                 } else {
779                         stripes = stripe_cnt;
780                 }
781
782                 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes, 
783                                      lov->desc.ld_pattern ?
784                                      lov->desc.ld_pattern : LOV_PATTERN_RAID0,
785                                      LOV_MAGIC);
786                 if (rc < 0)
787                         GOTO(out_err, rc);
788                 rc = 0;
789                 newea = 1;
790         }
791
792         lsm = set->set_oi->oi_md;
793         lsm->lsm_object_id = src_oa->o_id;
794         if (!lsm->lsm_stripe_size)
795                 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
796         if (!lsm->lsm_pattern) {
797                 LASSERT(lov->desc.ld_pattern);
798                 lsm->lsm_pattern = lov->desc.ld_pattern;
799         }
800
801         stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt);
802         LASSERT(stripes <= lsm->lsm_stripe_count);
803         if (stripes <= 0)
804                 GOTO(out_err, rc = stripes ? stripes : -EIO);
805         
806         for (i = 0; i < stripes; i++) {
807                 struct lov_request *req;
808                 int ost_idx = idx_arr[i];
809                 LASSERT(ost_idx >= 0);
810
811                 OBD_ALLOC(req, sizeof(*req));
812                 if (req == NULL)
813                         GOTO(out_err, rc = -ENOMEM);
814                 lov_set_add_req(req, set);
815
816                 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
817                 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
818                 if (req->rq_oi.oi_md == NULL)
819                         GOTO(out_err, rc = -ENOMEM);
820
821                 req->rq_oi.oi_oa = obdo_alloc();
822                 if (req->rq_oi.oi_oa == NULL)
823                         GOTO(out_err, rc = -ENOMEM);
824
825                 req->rq_idx = ost_idx;
826                 req->rq_stripe = i;
827                 /* create data objects with "parent" OA */
828                 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
829
830                 /* XXX When we start creating objects on demand, we need to
831                  *     make sure that we always create the object on the
832                  *     stripe which holds the existing file size.
833                  */
834                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
835                         req->rq_oi.oi_oa->o_size = 
836                                 lov_size_to_stripe(lsm, src_oa->o_size, i);
837
838                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
839                                i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
840                 }
841
842         }
843         LASSERT(set->set_count == stripes);
844
845         if (stripes < lsm->lsm_stripe_count)
846                 qos_shrink_lsm(set);
847
848         if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
849                 oti_alloc_cookies(oti, set->set_count);
850                 if (!oti->oti_logcookies)
851                         GOTO(out_err, rc = -ENOMEM);
852                 set->set_cookies = oti->oti_logcookies;
853         }
854 out_err:
855         if (newea && rc)
856                 obd_free_memmd(exp, &set->set_oi->oi_md);
857         free_idx_array(idx_arr, idx_cnt);
858         EXIT;
859         return rc;
860 }
861
862 void qos_update(struct lov_obd *lov)
863 {
864         ENTRY;
865         lov->lov_qos.lq_dirty = 1;
866 }
867