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