Whamcloud - gitweb
LU-6910 osp: add procfs values for OST reserved size
[fs/lustre-release.git] / lustre / lod / lod_qos.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/lod/lod_qos.c
33  *
34  * Implementation of different allocation algorithm used
35  * to distribute objects and data among OSTs.
36  */
37
38 #define DEBUG_SUBSYSTEM S_LOV
39
40 #include <asm/div64.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_class.h>
43 #include <lustre/lustre_idl.h>
44 #include "lod_internal.h"
45
46 /*
47  * force QoS policy (not RR) to be used for testing purposes
48  */
49 #define FORCE_QOS_
50
51 #define D_QOS   D_OTHER
52
53 #define QOS_DEBUG(fmt, ...)     CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
54 #define QOS_CONSOLE(fmt, ...)   LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
55
56 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
57                        OST_TGT(lod,i)->ltd_statfs.os_bsize)
58
59 /**
60  * Add a new target to Quality of Service (QoS) target table.
61  *
62  * Add a new OST target to the structure representing an OSS. Resort the list
63  * of known OSSs by the number of OSTs attached to each OSS. The OSS list is
64  * protected internally and no external locking is required.
65  *
66  * \param[in] lod               LOD device
67  * \param[in] ost_desc          OST description
68  *
69  * \retval 0                    on success
70  * \retval -ENOMEM              on error
71  */
72 int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
73 {
74         struct lod_qos_oss *oss = NULL, *temposs;
75         struct obd_export  *exp = ost_desc->ltd_exp;
76         int                 rc = 0, found = 0;
77         struct list_head   *list;
78         ENTRY;
79
80         down_write(&lod->lod_qos.lq_rw_sem);
81         /*
82          * a bit hacky approach to learn NID of corresponding connection
83          * but there is no official API to access information like this
84          * with OSD API.
85          */
86         list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
87                 if (obd_uuid_equals(&oss->lqo_uuid,
88                                     &exp->exp_connection->c_remote_uuid)) {
89                         found++;
90                         break;
91                 }
92         }
93
94         if (!found) {
95                 OBD_ALLOC_PTR(oss);
96                 if (!oss)
97                         GOTO(out, rc = -ENOMEM);
98                 memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
99                        sizeof(oss->lqo_uuid));
100         } else {
101                 /* Assume we have to move this one */
102                 list_del(&oss->lqo_oss_list);
103         }
104
105         oss->lqo_ost_count++;
106         ost_desc->ltd_qos.ltq_oss = oss;
107
108         CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
109                obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
110                oss->lqo_ost_count);
111
112         /* Add sorted by # of OSTs.  Find the first entry that we're
113            bigger than... */
114         list = &lod->lod_qos.lq_oss_list;
115         list_for_each_entry(temposs, list, lqo_oss_list) {
116                 if (oss->lqo_ost_count > temposs->lqo_ost_count)
117                         break;
118         }
119         /* ...and add before it.  If we're the first or smallest, temposs
120            points to the list head, and we add to the end. */
121         list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
122
123         lod->lod_qos.lq_dirty = 1;
124         lod->lod_qos.lq_rr.lqr_dirty = 1;
125
126 out:
127         up_write(&lod->lod_qos.lq_rw_sem);
128         RETURN(rc);
129 }
130
131 /**
132  * Remove OST target from QoS table.
133  *
134  * Removes given OST target from QoS table and releases related OSS structure
135  * if no OSTs remain on the OSS.
136  *
137  * \param[in] lod               LOD device
138  * \param[in] ost_desc          OST description
139  *
140  * \retval 0                    on success
141  * \retval -ENOENT              if no OSS was found
142  */
143 int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
144 {
145         struct lod_qos_oss *oss;
146         int                 rc = 0;
147         ENTRY;
148
149         down_write(&lod->lod_qos.lq_rw_sem);
150         oss = ost_desc->ltd_qos.ltq_oss;
151         if (!oss)
152                 GOTO(out, rc = -ENOENT);
153
154         oss->lqo_ost_count--;
155         if (oss->lqo_ost_count == 0) {
156                 CDEBUG(D_QOS, "removing OSS %s\n",
157                        obd_uuid2str(&oss->lqo_uuid));
158                 list_del(&oss->lqo_oss_list);
159                 ost_desc->ltd_qos.ltq_oss = NULL;
160                 OBD_FREE_PTR(oss);
161         }
162
163         lod->lod_qos.lq_dirty = 1;
164         lod->lod_qos.lq_rr.lqr_dirty = 1;
165 out:
166         up_write(&lod->lod_qos.lq_rw_sem);
167         RETURN(rc);
168 }
169
170 /**
171  * Check whether the target is available for new OST objects.
172  *
173  * Request statfs data from the given target and verify it's active and not
174  * read-only. If so, then it can be used to place new OST objects. This
175  * function also maintains the number of active/inactive targets and sets
176  * dirty flags if those numbers change so others can run re-balance procedures.
177  * No external locking is required.
178  *
179  * \param[in] env       execution environment for this thread
180  * \param[in] d         LOD device
181  * \param[in] index     index of OST target to check
182  * \param[out] sfs      buffer for statfs data
183  *
184  * \retval 0            if the target is good
185  * \retval negative     negated errno on error
186
187  */
188 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
189                                 int index, struct obd_statfs *sfs)
190 {
191         struct lod_tgt_desc *ost;
192         int                  rc;
193         ENTRY;
194
195         LASSERT(d);
196         ost = OST_TGT(d,index);
197         LASSERT(ost);
198
199         rc = dt_statfs(env, ost->ltd_ost, sfs);
200
201         if (rc == -ENOSPC)
202                 RETURN(rc);
203
204         if (rc && rc != -ENOTCONN)
205                 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
206
207         /* If the OST is readonly then we can't allocate objects there */
208         if (sfs->os_state & OS_STATE_READONLY)
209                 rc = -EROFS;
210
211         /* check whether device has changed state (active, inactive) */
212         if (rc != 0 && ost->ltd_active) {
213                 /* turned inactive? */
214                 spin_lock(&d->lod_desc_lock);
215                 if (ost->ltd_active) {
216                         ost->ltd_active = 0;
217                         LASSERT(d->lod_desc.ld_active_tgt_count > 0);
218                         d->lod_desc.ld_active_tgt_count--;
219                         d->lod_qos.lq_dirty = 1;
220                         d->lod_qos.lq_rr.lqr_dirty = 1;
221                         CDEBUG(D_CONFIG, "%s: turns inactive\n",
222                                ost->ltd_exp->exp_obd->obd_name);
223                 }
224                 spin_unlock(&d->lod_desc_lock);
225         } else if (rc == 0 && ost->ltd_active == 0) {
226                 /* turned active? */
227                 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
228                          "active tgt count %d, ost nr %d\n",
229                          d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
230                 spin_lock(&d->lod_desc_lock);
231                 if (ost->ltd_active == 0) {
232                         ost->ltd_active = 1;
233                         d->lod_desc.ld_active_tgt_count++;
234                         d->lod_qos.lq_dirty = 1;
235                         d->lod_qos.lq_rr.lqr_dirty = 1;
236                         CDEBUG(D_CONFIG, "%s: turns active\n",
237                                ost->ltd_exp->exp_obd->obd_name);
238                 }
239                 spin_unlock(&d->lod_desc_lock);
240         }
241
242         RETURN(rc);
243 }
244
245 /**
246  * Maintain per-target statfs data.
247  *
248  * The function refreshes statfs data for all the targets every N seconds.
249  * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
250  * initially.
251  *
252  * \param[in] env       execution environment for this thread
253  * \param[in] lod       LOD device
254  */
255 static void lod_qos_statfs_update(const struct lu_env *env,
256                                   struct lod_device *lod)
257 {
258         struct obd_device *obd = lod2obd(lod);
259         struct ost_pool   *osts = &(lod->lod_pool_info);
260         unsigned int       i;
261         int                idx;
262         __u64              max_age, avail;
263         ENTRY;
264
265         max_age = cfs_time_shift_64(-2 * lod->lod_desc.ld_qos_maxage);
266
267         if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
268                 /* statfs data are quite recent, don't need to refresh it */
269                 RETURN_EXIT;
270
271         down_write(&lod->lod_qos.lq_rw_sem);
272         if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
273                 goto out;
274
275         for (i = 0; i < osts->op_count; i++) {
276                 idx = osts->op_array[i];
277                 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
278                 if (lod_statfs_and_check(env, lod, idx,
279                                          &OST_TGT(lod, idx)->ltd_statfs))
280                         continue;
281                 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
282                         /* recalculate weigths */
283                         lod->lod_qos.lq_dirty = 1;
284         }
285         obd->obd_osfs_age = cfs_time_current_64();
286
287 out:
288         up_write(&lod->lod_qos.lq_rw_sem);
289         EXIT;
290 }
291
292 /**
293  * Calculate per-OST and per-OSS penalties
294  *
295  * Re-calculate penalties when the configuration changes, active targets
296  * change and after statfs refresh (all these are reflected by lq_dirty flag).
297  * On every OST and OSS: decay the penalty by half for every 8x the update
298  * interval that the device has been idle. That gives lots of time for the
299  * statfs information to be updated (which the penalty is only a proxy for),
300  * and avoids penalizing OSS/OSTs under light load.
301  * See lod_qos_calc_weight() for how penalties are factored into the weight.
302  *
303  * \param[in] lod       LOD device
304  *
305  * \retval 0            on success
306  * \retval -EAGAIN      the number of OSTs isn't enough
307  */
308 static int lod_qos_calc_ppo(struct lod_device *lod)
309 {
310         struct lod_qos_oss *oss;
311         __u64               ba_max, ba_min, temp;
312         __u32               num_active;
313         unsigned int        i;
314         int                 rc, prio_wide;
315         time_t              now, age;
316         ENTRY;
317
318         if (!lod->lod_qos.lq_dirty)
319                 GOTO(out, rc = 0);
320
321         num_active = lod->lod_desc.ld_active_tgt_count - 1;
322         if (num_active < 1)
323                 GOTO(out, rc = -EAGAIN);
324
325         /* find bavail on each OSS */
326         list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
327                             oss->lqo_bavail = 0;
328         lod->lod_qos.lq_active_oss_count = 0;
329
330         /*
331          * How badly user wants to select OSTs "widely" (not recently chosen
332          * and not on recent OSS's).  As opposed to "freely" (free space
333          * avail.) 0-256
334          */
335         prio_wide = 256 - lod->lod_qos.lq_prio_free;
336
337         ba_min = (__u64)(-1);
338         ba_max = 0;
339         now = cfs_time_current_sec();
340         /* Calculate OST penalty per object
341          * (lod ref taken in lod_qos_prep_create()) */
342         cfs_foreach_bit(lod->lod_ost_bitmap, i) {
343                 LASSERT(OST_TGT(lod,i));
344                 temp = TGT_BAVAIL(i);
345                 if (!temp)
346                         continue;
347                 ba_min = min(temp, ba_min);
348                 ba_max = max(temp, ba_max);
349
350                 /* Count the number of usable OSS's */
351                 if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
352                         lod->lod_qos.lq_active_oss_count++;
353                 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
354
355                 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
356                 temp >>= 1;
357                 do_div(temp, num_active);
358                 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
359                         (temp * prio_wide) >> 8;
360
361                 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
362                 if (lod->lod_qos.lq_reset ||
363                     age > 32 * lod->lod_desc.ld_qos_maxage)
364                         OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
365                 else if (age > lod->lod_desc.ld_qos_maxage)
366                         /* Decay OST penalty. */
367                         OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
368                                 (age / lod->lod_desc.ld_qos_maxage);
369         }
370
371         num_active = lod->lod_qos.lq_active_oss_count - 1;
372         if (num_active < 1) {
373                 /* If there's only 1 OSS, we can't penalize it, so instead
374                    we have to double the OST penalty */
375                 num_active = 1;
376                 cfs_foreach_bit(lod->lod_ost_bitmap, i)
377                         OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
378         }
379
380         /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
381         list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
382                 temp = oss->lqo_bavail >> 1;
383                 do_div(temp, oss->lqo_ost_count * num_active);
384                 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
385
386                 age = (now - oss->lqo_used) >> 3;
387                 if (lod->lod_qos.lq_reset ||
388                     age > 32 * lod->lod_desc.ld_qos_maxage)
389                         oss->lqo_penalty = 0;
390                 else if (age > lod->lod_desc.ld_qos_maxage)
391                         /* Decay OSS penalty. */
392                         oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
393         }
394
395         lod->lod_qos.lq_dirty = 0;
396         lod->lod_qos.lq_reset = 0;
397
398         /* If each ost has almost same free space,
399          * do rr allocation for better creation performance */
400         lod->lod_qos.lq_same_space = 0;
401         if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
402                 lod->lod_qos.lq_same_space = 1;
403                 /* Reset weights for the next time we enter qos mode */
404                 lod->lod_qos.lq_reset = 1;
405         }
406         rc = 0;
407
408 out:
409 #ifndef FORCE_QOS
410         if (!rc && lod->lod_qos.lq_same_space)
411                 RETURN(-EAGAIN);
412 #endif
413         RETURN(rc);
414 }
415
416 /**
417  * Calculate weight for a given OST target.
418  *
419  * The final OST weight is the number of bytes available minus the OST and
420  * OSS penalties.  See lod_qos_calc_ppo() for how penalties are calculated.
421  *
422  * \param[in] lod       LOD device, where OST targets are listed
423  * \param[in] i         OST target index
424  *
425  * \retval              0
426  */
427 static int lod_qos_calc_weight(struct lod_device *lod, int i)
428 {
429         __u64 temp, temp2;
430
431         temp = TGT_BAVAIL(i);
432         temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
433                 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
434         if (temp < temp2)
435                 OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
436         else
437                 OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
438         return 0;
439 }
440
441 /**
442  * Re-calculate weights.
443  *
444  * The function is called when some OST target was used for a new object. In
445  * this case we should re-calculate all the weights to keep new allocations
446  * balanced well.
447  *
448  * \param[in] lod       LOD device
449  * \param[in] osts      OST pool where a new object was placed
450  * \param[in] index     OST target where a new object was placed
451  * \param[out] total_wt new total weight for the pool
452  *
453  * \retval              0
454  */
455 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
456                         __u32 index, __u64 *total_wt)
457 {
458         struct lod_tgt_desc *ost;
459         struct lod_qos_oss  *oss;
460         unsigned int j;
461         ENTRY;
462
463         ost = OST_TGT(lod,index);
464         LASSERT(ost);
465
466         /* Don't allocate on this devuce anymore, until the next alloc_qos */
467         ost->ltd_qos.ltq_usable = 0;
468
469         oss = ost->ltd_qos.ltq_oss;
470
471         /* Decay old penalty by half (we're adding max penalty, and don't
472            want it to run away.) */
473         ost->ltd_qos.ltq_penalty >>= 1;
474         oss->lqo_penalty >>= 1;
475
476         /* mark the OSS and OST as recently used */
477         ost->ltd_qos.ltq_used = oss->lqo_used = cfs_time_current_sec();
478
479         /* Set max penalties for this OST and OSS */
480         ost->ltd_qos.ltq_penalty +=
481                 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
482         oss->lqo_penalty += oss->lqo_penalty_per_obj *
483                 lod->lod_qos.lq_active_oss_count;
484
485         /* Decrease all OSS penalties */
486         list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
487                 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
488                         oss->lqo_penalty = 0;
489                 else
490                         oss->lqo_penalty -= oss->lqo_penalty_per_obj;
491         }
492
493         *total_wt = 0;
494         /* Decrease all OST penalties */
495         for (j = 0; j < osts->op_count; j++) {
496                 int i;
497
498                 i = osts->op_array[j];
499                 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
500                         continue;
501
502                 ost = OST_TGT(lod,i);
503                 LASSERT(ost);
504
505                 if (ost->ltd_qos.ltq_penalty <
506                                 ost->ltd_qos.ltq_penalty_per_obj)
507                         ost->ltd_qos.ltq_penalty = 0;
508                 else
509                         ost->ltd_qos.ltq_penalty -=
510                                 ost->ltd_qos.ltq_penalty_per_obj;
511
512                 lod_qos_calc_weight(lod, i);
513
514                 /* Recalc the total weight of usable osts */
515                 if (ost->ltd_qos.ltq_usable)
516                         *total_wt += ost->ltd_qos.ltq_weight;
517
518                 QOS_DEBUG("recalc tgt %d usable=%d avail="LPU64
519                           " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
520                           " ossp="LPU64" wt="LPU64"\n",
521                           i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
522                           ost->ltd_qos.ltq_penalty_per_obj >> 10,
523                           ost->ltd_qos.ltq_penalty >> 10,
524                           ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
525                           ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
526                           ost->ltd_qos.ltq_weight >> 10);
527         }
528
529         RETURN(0);
530 }
531
532 void lod_qos_rr_init(struct lod_qos_rr *lqr)
533 {
534         spin_lock_init(&lqr->lqr_alloc);
535         lqr->lqr_dirty = 1;
536 }
537
538
539 #define LOV_QOS_EMPTY ((__u32)-1)
540
541 /**
542  * Calculate optimal round-robin order with regard to OSSes.
543  *
544  * Place all the OSTs from pool \a src_pool in a special array to be used for
545  * round-robin (RR) stripe allocation.  The placement algorithm interleaves
546  * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
547  * Resorts the targets when the number of active targets changes (because of
548  * a new target or activation/deactivation).
549  *
550  * \param[in] lod       LOD device
551  * \param[in] src_pool  OST pool
552  * \param[in] lqr       round-robin list
553  *
554  * \retval 0            on success
555  * \retval -ENOMEM      fails to allocate the array
556  */
557 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
558                            struct lod_qos_rr *lqr)
559 {
560         struct lod_qos_oss  *oss;
561         struct lod_tgt_desc *ost;
562         unsigned placed, real_count;
563         unsigned int i;
564         int rc;
565         ENTRY;
566
567         if (!lqr->lqr_dirty) {
568                 LASSERT(lqr->lqr_pool.op_size);
569                 RETURN(0);
570         }
571
572         /* Do actual allocation. */
573         down_write(&lod->lod_qos.lq_rw_sem);
574
575         /*
576          * Check again. While we were sleeping on @lq_rw_sem something could
577          * change.
578          */
579         if (!lqr->lqr_dirty) {
580                 LASSERT(lqr->lqr_pool.op_size);
581                 up_write(&lod->lod_qos.lq_rw_sem);
582                 RETURN(0);
583         }
584
585         real_count = src_pool->op_count;
586
587         /* Zero the pool array */
588         /* alloc_rr is holding a read lock on the pool, so nobody is adding/
589            deleting from the pool. The lq_rw_sem insures that nobody else
590            is reading. */
591         lqr->lqr_pool.op_count = real_count;
592         rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
593         if (rc) {
594                 up_write(&lod->lod_qos.lq_rw_sem);
595                 RETURN(rc);
596         }
597         for (i = 0; i < lqr->lqr_pool.op_count; i++)
598                 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
599
600         /* Place all the OSTs from 1 OSS at the same time. */
601         placed = 0;
602         list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
603                 int j = 0;
604
605                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
606                         int next;
607
608                         if (!cfs_bitmap_check(lod->lod_ost_bitmap,
609                                                 src_pool->op_array[i]))
610                                 continue;
611
612                         ost = OST_TGT(lod,src_pool->op_array[i]);
613                         LASSERT(ost && ost->ltd_ost);
614                         if (ost->ltd_qos.ltq_oss != oss)
615                                 continue;
616
617                         /* Evenly space these OSTs across arrayspace */
618                         next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
619                         while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
620                                 next = (next + 1) % lqr->lqr_pool.op_count;
621
622                         lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
623                         j++;
624                         placed++;
625                 }
626         }
627
628         lqr->lqr_dirty = 0;
629         up_write(&lod->lod_qos.lq_rw_sem);
630
631         if (placed != real_count) {
632                 /* This should never happen */
633                 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
634                                    "round-robin list (%d of %d).\n",
635                                    placed, real_count);
636                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
637                         LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
638                                  lqr->lqr_pool.op_array[i]);
639                 }
640                 lqr->lqr_dirty = 1;
641                 RETURN(-EAGAIN);
642         }
643
644 #if 0
645         for (i = 0; i < lqr->lqr_pool.op_count; i++)
646                 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
647 #endif
648
649         RETURN(0);
650 }
651
652 /**
653  * Instantiate and declare creation of a new object.
654  *
655  * The function instantiates LU representation for a new object on the
656  * specified device. Also it declares an intention to create that
657  * object on the storage target.
658  *
659  * Note lu_object_anon() is used which is a trick with regard to LU/OSD
660  * infrastructure - in the existing precreation framework we can't assign FID
661  * at this moment, we do this later once a transaction is started. So the
662  * special method instantiates FID-less object in the cache and later it
663  * will get a FID and proper placement in LU cache.
664  *
665  * \param[in] env       execution environment for this thread
666  * \param[in] d         LOD device
667  * \param[in] ost_idx   OST target index where the object is being created
668  * \param[in] th        transaction handle
669  *
670  * \retval              object ptr on success, ERR_PTR() otherwise
671  */
672 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
673                                                    struct lod_device *d,
674                                                    __u32 ost_idx,
675                                                    struct thandle *th)
676 {
677         struct lod_tgt_desc *ost;
678         struct lu_object *o, *n;
679         struct lu_device *nd;
680         struct dt_object *dt;
681         int               rc;
682         ENTRY;
683
684         LASSERT(d);
685         LASSERT(ost_idx < d->lod_osts_size);
686         ost = OST_TGT(d,ost_idx);
687         LASSERT(ost);
688         LASSERT(ost->ltd_ost);
689
690         nd = &ost->ltd_ost->dd_lu_dev;
691
692         /*
693          * allocate anonymous object with zero fid, real fid
694          * will be assigned by OSP within transaction
695          * XXX: to be fixed with fully-functional OST fids
696          */
697         o = lu_object_anon(env, nd, NULL);
698         if (IS_ERR(o))
699                 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
700
701         n = lu_object_locate(o->lo_header, nd->ld_type);
702         if (unlikely(n == NULL)) {
703                 CERROR("can't find slice\n");
704                 lu_object_put(env, o);
705                 GOTO(out, dt = ERR_PTR(-EINVAL));
706         }
707
708         dt = container_of(n, struct dt_object, do_lu);
709
710         rc = lod_sub_object_declare_create(env, dt, NULL, NULL, NULL, th);
711         if (rc < 0) {
712                 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
713                        ost_idx, rc);
714                 lu_object_put(env, o);
715                 dt = ERR_PTR(rc);
716         }
717
718 out:
719         RETURN(dt);
720 }
721
722 /**
723  * Calculate a minimum acceptable stripe count.
724  *
725  * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
726  * all stripes or 3/4 of stripes.
727  *
728  * \param[in] stripe_cnt        number of stripes requested
729  * \param[in] flags             0 or LOV_USES_DEFAULT_STRIPE
730  *
731  * \retval                      acceptable stripecount
732  */
733 static int min_stripe_count(__u32 stripe_cnt, int flags)
734 {
735         return (flags & LOV_USES_DEFAULT_STRIPE ?
736                         stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
737 }
738
739 #define LOV_CREATE_RESEED_MULT 30
740 #define LOV_CREATE_RESEED_MIN  2000
741
742 /**
743  * Initialize temporary OST-in-use array.
744  *
745  * Allocate or extend the array used to mark targets already assigned to a new
746  * striping so they are not used more than once.
747  *
748  * \param[in] env       execution environment for this thread
749  * \param[in] stripes   number of items needed in the array
750  *
751  * \retval 0            on success
752  * \retval -ENOMEM      on error
753  */
754 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
755                                            __u32 stripes)
756 {
757         struct lod_thread_info *info = lod_env_info(env);
758
759         if (info->lti_ea_store_size < sizeof(int) * stripes)
760                 lod_ea_store_resize(info, stripes * sizeof(int));
761         if (info->lti_ea_store_size < sizeof(int) * stripes) {
762                 CERROR("can't allocate memory for ost-in-use array\n");
763                 return -ENOMEM;
764         }
765         memset(info->lti_ea_store, -1, sizeof(int) * stripes);
766         return 0;
767 }
768
769 /**
770  * Remember a target in the array of used targets.
771  *
772  * Mark the given target as used for a new striping being created. The status
773  * of an OST in a striping can be checked with lod_qos_is_ost_used().
774  *
775  * \param[in] env       execution environment for this thread
776  * \param[in] idx       index in the array
777  * \param[in] ost       OST target index to mark as used
778  */
779 static inline void lod_qos_ost_in_use(const struct lu_env *env,
780                                       int idx, int ost)
781 {
782         struct lod_thread_info *info = lod_env_info(env);
783         int *osts = info->lti_ea_store;
784
785         LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
786         osts[idx] = ost;
787 }
788
789 /**
790  * Check is OST used in a striping.
791  *
792  * Checks whether OST with the given index is marked as used in the temporary
793  * array (see lod_qos_ost_in_use()).
794  *
795  * \param[in] env       execution environment for this thread
796  * \param[in] ost       OST target index to check
797  * \param[in] stripes   the number of items used in the array already
798  *
799  * \retval 0            not used
800  * \retval 1            used
801  */
802 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
803 {
804         struct lod_thread_info *info = lod_env_info(env);
805         int *osts = info->lti_ea_store;
806         __u32 j;
807
808         for (j = 0; j < stripes; j++) {
809                 if (osts[j] == ost)
810                         return 1;
811         }
812         return 0;
813 }
814
815 static int lod_check_and_reserve_ost(const struct lu_env *env,
816                                      struct lod_device *m,
817                                      struct obd_statfs *sfs, __u32 ost_idx,
818                                      __u32 speed, __u32 *s_idx,
819                                      struct dt_object **stripe,
820                                      struct thandle *th)
821 {
822         struct dt_object   *o;
823         __u32 stripe_idx = *s_idx;
824         int rc;
825
826         rc = lod_statfs_and_check(env, m, ost_idx, sfs);
827         if (rc) {
828                 /* this OSP doesn't feel well */
829                 goto out_return;
830         }
831
832         /*
833          * We expect number of precreated objects in f_ffree at
834          * the first iteration, skip OSPs with no objects ready
835          */
836         if (sfs->os_fprecreated == 0 && speed == 0) {
837                 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
838                 goto out_return;
839         }
840
841         /*
842          * try to use another OSP if this one is degraded
843          */
844         if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
845                 QOS_DEBUG("#%d: degraded\n", ost_idx);
846                 goto out_return;
847         }
848
849         /*
850          * do not put >1 objects on a single OST
851          */
852         if (speed && lod_qos_is_ost_used(env, ost_idx, stripe_idx))
853                 goto out_return;
854
855         o = lod_qos_declare_object_on(env, m, ost_idx, th);
856         if (IS_ERR(o)) {
857                 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
858                        ost_idx, (int) PTR_ERR(o));
859                 rc = PTR_ERR(o);
860                 goto out_return;
861         }
862
863         /*
864          * We've successfully declared (reserved) an object
865          */
866         lod_qos_ost_in_use(env, stripe_idx, ost_idx);
867         stripe[stripe_idx] = o;
868         stripe_idx++;
869         *s_idx = stripe_idx;
870
871 out_return:
872         return rc;
873 }
874
875 /**
876  * Allocate a striping using round-robin algorithm.
877  *
878  * Allocates a new striping using round-robin algorithm. The function refreshes
879  * all the internal structures (statfs cache, array of available OSTs sorted
880  * with regard to OSS, etc). The number of stripes required is taken from the
881  * object (must be prepared by the caller), but can change if the flag
882  * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
883  * is trying to create a striping on the object in parallel. All the internal
884  * structures (like pools, etc) are protected and no additional locking is
885  * required. The function succeeds even if a single stripe is allocated. To save
886  * time we give priority to targets which already have objects precreated.
887  * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
888  *
889  * \param[in] env       execution environment for this thread
890  * \param[in] lo        LOD object
891  * \param[out] stripe   striping created
892  * \param[in] flags     allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
893  * \param[in] th        transaction handle
894  *
895  * \retval 0            on success
896  * \retval -ENOSPC      if not enough OSTs are found
897  * \retval negative     negated errno for other failures
898  */
899 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
900                         struct dt_object **stripe, int flags,
901                         struct thandle *th)
902 {
903         struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
904         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
905         struct pool_desc  *pool = NULL;
906         struct ost_pool   *osts;
907         struct lod_qos_rr *lqr;
908         unsigned int       i, array_idx;
909         int                rc;
910         __u32              ost_start_idx_temp;
911         int                speed = 0;
912         __u32              stripe_idx = 0;
913         __u32              stripe_cnt = lo->ldo_stripenr;
914         __u32              stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
915         __u32              ost_idx;
916         ENTRY;
917
918         if (lo->ldo_pool)
919                 pool = lod_find_pool(m, lo->ldo_pool);
920
921         if (pool != NULL) {
922                 down_read(&pool_tgt_rw_sem(pool));
923                 osts = &(pool->pool_obds);
924                 lqr = &(pool->pool_rr);
925         } else {
926                 osts = &(m->lod_pool_info);
927                 lqr = &(m->lod_qos.lq_rr);
928         }
929
930         rc = lod_qos_calc_rr(m, osts, lqr);
931         if (rc)
932                 GOTO(out, rc);
933
934         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
935         if (rc)
936                 GOTO(out, rc);
937
938         down_read(&m->lod_qos.lq_rw_sem);
939         spin_lock(&lqr->lqr_alloc);
940         if (--lqr->lqr_start_count <= 0) {
941                 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
942                 lqr->lqr_start_count =
943                         (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
944                          LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
945         } else if (stripe_cnt_min >= osts->op_count ||
946                         lqr->lqr_start_idx > osts->op_count) {
947                 /* If we have allocated from all of the OSTs, slowly
948                  * precess the next start if the OST/stripe count isn't
949                  * already doing this for us. */
950                 lqr->lqr_start_idx %= osts->op_count;
951                 if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
952                         ++lqr->lqr_offset_idx;
953         }
954         ost_start_idx_temp = lqr->lqr_start_idx;
955
956 repeat_find:
957
958         QOS_DEBUG("pool '%s' want %d startidx %d startcnt %d offset %d "
959                   "active %d count %d\n",
960                   lo->ldo_pool ? lo->ldo_pool : "",
961                   stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
962                   lqr->lqr_offset_idx, osts->op_count, osts->op_count);
963
964         for (i = 0; i < osts->op_count && stripe_idx < lo->ldo_stripenr; i++) {
965                 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
966                                 osts->op_count;
967                 ++lqr->lqr_start_idx;
968                 ost_idx = lqr->lqr_pool.op_array[array_idx];
969
970                 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
971                           i, lqr->lqr_start_idx, /* XXX: active*/ 0,
972                           stripe_idx, array_idx, ost_idx);
973
974                 if ((ost_idx == LOV_QOS_EMPTY) ||
975                     !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
976                         continue;
977
978                 /* Fail Check before osc_precreate() is called
979                    so we can only 'fail' single OSC. */
980                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
981                         continue;
982
983                 spin_unlock(&lqr->lqr_alloc);
984                 rc = lod_check_and_reserve_ost(env, m, sfs, ost_idx, speed,
985                                                &stripe_idx, stripe, th);
986                 spin_lock(&lqr->lqr_alloc);
987         }
988         if ((speed < 2) && (stripe_idx < stripe_cnt_min)) {
989                 /* Try again, allowing slower OSCs */
990                 speed++;
991                 lqr->lqr_start_idx = ost_start_idx_temp;
992                 goto repeat_find;
993         }
994
995         spin_unlock(&lqr->lqr_alloc);
996         up_read(&m->lod_qos.lq_rw_sem);
997
998         if (stripe_idx) {
999                 lo->ldo_stripenr = stripe_idx;
1000                 /* at least one stripe is allocated */
1001                 rc = 0;
1002         } else {
1003                 /* nobody provided us with a single object */
1004                 rc = -ENOSPC;
1005         }
1006
1007 out:
1008         if (pool != NULL) {
1009                 up_read(&pool_tgt_rw_sem(pool));
1010                 /* put back ref got by lod_find_pool() */
1011                 lod_pool_putref(pool);
1012         }
1013
1014         RETURN(rc);
1015 }
1016
1017 /**
1018  * Allocate a specific striping layout on a user defined set of OSTs.
1019  *
1020  * Allocates new striping using the OST index range provided by the data from
1021  * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1022  * OSTs are not considered. The exact order of OSTs requested by the user
1023  * is respected as much as possible depending on OST status. The number of
1024  * stripes needed and stripe offset are taken from the object. If that number
1025  * can not be met, then the function returns a failure and then it's the
1026  * caller's responsibility to release the stripes allocated. All the internal
1027  * structures are protected, but no concurrent allocation is allowed on the
1028  * same objects.
1029  *
1030  * \param[in] env       execution environment for this thread
1031  * \param[in] lo        LOD object
1032  * \param[out] stripe   striping created
1033  * \param[in] lum       stripe md to specify list of OSTs
1034  * \param[in] th        transaction handle
1035  *
1036  * \retval 0            on success
1037  * \retval -ENODEV      OST index does not exist on file system
1038  * \retval -EINVAL      requested OST index is invalid
1039  * \retval negative     negated errno on error
1040  */
1041 static int lod_alloc_ost_list(const struct lu_env *env,
1042                               struct lod_object *lo, struct dt_object **stripe,
1043                               struct lov_user_md *lum, struct thandle *th)
1044 {
1045         struct lod_device       *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1046         struct obd_statfs       *sfs = &lod_env_info(env)->lti_osfs;
1047         struct dt_object        *o;
1048         struct lov_user_md_v3   *v3;
1049         unsigned int            array_idx = 0;
1050         int                     stripe_count = 0;
1051         int                     i;
1052         int                     rc;
1053         ENTRY;
1054
1055         /* for specific OSTs layout */
1056         LASSERT(lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC);
1057         lustre_print_user_md(D_OTHER, lum, __func__);
1058
1059         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1060         if (rc < 0)
1061                 RETURN(rc);
1062
1063         v3 = (struct lov_user_md_v3 *)lum;
1064         for (i = 0; i < lo->ldo_stripenr; i++) {
1065                 if (v3->lmm_objects[i].l_ost_idx == lo->ldo_def_stripe_offset) {
1066                         array_idx = i;
1067                         break;
1068                 }
1069         }
1070         if (i == lo->ldo_stripenr) {
1071                 CDEBUG(D_OTHER,
1072                        "%s: start index %d not in the specified list of OSTs\n",
1073                        lod2obd(m)->obd_name, lo->ldo_def_stripe_offset);
1074                 RETURN(-EINVAL);
1075         }
1076
1077         for (i = 0; i < lo->ldo_stripenr;
1078              i++, array_idx = (array_idx + 1) % lo->ldo_stripenr) {
1079                 __u32 ost_idx = v3->lmm_objects[array_idx].l_ost_idx;
1080
1081                 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1082                         rc = -ENODEV;
1083                         break;
1084                 }
1085
1086                 /*
1087                  * do not put >1 objects on a single OST
1088                  */
1089                 if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
1090                         rc = -EINVAL;
1091                         break;
1092                 }
1093
1094                 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1095                 if (rc < 0) /* this OSP doesn't feel well */
1096                         break;
1097
1098                 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1099                 if (IS_ERR(o)) {
1100                         rc = PTR_ERR(o);
1101                         CDEBUG(D_OTHER,
1102                                "%s: can't declare new object on #%u: %d\n",
1103                                lod2obd(m)->obd_name, ost_idx, rc);
1104                         break;
1105                 }
1106
1107                 /*
1108                  * We've successfully declared (reserved) an object
1109                  */
1110                 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1111                 stripe[stripe_count] = o;
1112                 stripe_count++;
1113         }
1114
1115         RETURN(rc);
1116 }
1117
1118 /**
1119  * Allocate a striping on a predefined set of OSTs.
1120  *
1121  * Allocates new layout starting from OST index in lo->ldo_def_stripe_offset.
1122  * Full OSTs are not considered. The exact order of OSTs is not important and
1123  * varies depending on OST status. The allocation procedure prefers the targets
1124  * with precreated objects ready. The number of stripes needed and stripe
1125  * offset are taken from the object. If that number cannot be met, then the
1126  * function returns an error and then it's the caller's responsibility to
1127  * release the stripes allocated. All the internal structures are protected,
1128  * but no concurrent allocation is allowed on the same objects.
1129  *
1130  * \param[in] env       execution environment for this thread
1131  * \param[in] lo        LOD object
1132  * \param[out] stripe   striping created
1133  * \param[in] flags     not used
1134  * \param[in] th        transaction handle
1135  *
1136  * \retval 0            on success
1137  * \retval -ENOSPC      if no OST objects are available at all
1138  * \retval -EFBIG       if not enough OST objects are found
1139  * \retval -EINVAL      requested offset is invalid
1140  * \retval negative     errno on failure
1141  */
1142 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1143                               struct dt_object **stripe, int flags,
1144                               struct thandle *th)
1145 {
1146         struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1147         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1148         struct dt_object  *o;
1149         __u32              ost_idx;
1150         unsigned int       i, array_idx, ost_count;
1151         int                rc, stripe_num = 0;
1152         int                speed = 0;
1153         struct pool_desc  *pool = NULL;
1154         struct ost_pool   *osts;
1155         ENTRY;
1156
1157         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1158         if (rc)
1159                 GOTO(out, rc);
1160
1161         if (lo->ldo_pool)
1162                 pool = lod_find_pool(m, lo->ldo_pool);
1163
1164         if (pool != NULL) {
1165                 down_read(&pool_tgt_rw_sem(pool));
1166                 osts = &(pool->pool_obds);
1167         } else {
1168                 osts = &(m->lod_pool_info);
1169         }
1170
1171         ost_count = osts->op_count;
1172
1173 repeat_find:
1174         /* search loi_ost_idx in ost array */
1175         array_idx = 0;
1176         for (i = 0; i < ost_count; i++) {
1177                 if (osts->op_array[i] == lo->ldo_def_stripe_offset) {
1178                         array_idx = i;
1179                         break;
1180                 }
1181         }
1182         if (i == ost_count) {
1183                 CERROR("Start index %d not found in pool '%s'\n",
1184                        lo->ldo_def_stripe_offset,
1185                        lo->ldo_pool ? lo->ldo_pool : "");
1186                 GOTO(out, rc = -EINVAL);
1187         }
1188
1189         for (i = 0; i < ost_count;
1190                         i++, array_idx = (array_idx + 1) % ost_count) {
1191                 ost_idx = osts->op_array[array_idx];
1192
1193                 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1194                         continue;
1195
1196                 /* Fail Check before osc_precreate() is called
1197                    so we can only 'fail' single OSC. */
1198                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1199                         continue;
1200
1201                 /*
1202                  * do not put >1 objects on a single OST
1203                  */
1204                 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1205                         continue;
1206
1207                 /* Drop slow OSCs if we can, but not for requested start idx.
1208                  *
1209                  * This means "if OSC is slow and it is not the requested
1210                  * start OST, then it can be skipped, otherwise skip it only
1211                  * if it is inactive/recovering/out-of-space." */
1212
1213                 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1214                 if (rc) {
1215                         /* this OSP doesn't feel well */
1216                         continue;
1217                 }
1218
1219                 /*
1220                  * We expect number of precreated objects in f_ffree at
1221                  * the first iteration, skip OSPs with no objects ready
1222                  * don't apply this logic to OST specified with stripe_offset
1223                  */
1224                 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1225                         continue;
1226
1227                 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1228                 if (IS_ERR(o)) {
1229                         CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1230                                ost_idx, (int) PTR_ERR(o));
1231                         continue;
1232                 }
1233
1234                 /*
1235                  * We've successfully declared (reserved) an object
1236                  */
1237                 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1238                 stripe[stripe_num] = o;
1239                 stripe_num++;
1240
1241                 /* We have enough stripes */
1242                 if (stripe_num == lo->ldo_stripenr)
1243                         GOTO(out, rc = 0);
1244         }
1245         if (speed < 2) {
1246                 /* Try again, allowing slower OSCs */
1247                 speed++;
1248                 goto repeat_find;
1249         }
1250
1251         /* If we were passed specific striping params, then a failure to
1252          * meet those requirements is an error, since we can't reallocate
1253          * that memory (it might be part of a larger array or something).
1254          */
1255         CERROR("can't lstripe objid "DFID": have %d want %u\n",
1256                PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1257                lo->ldo_stripenr);
1258         rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1259 out:
1260         if (pool != NULL) {
1261                 up_read(&pool_tgt_rw_sem(pool));
1262                 /* put back ref got by lod_find_pool() */
1263                 lod_pool_putref(pool);
1264         }
1265
1266         RETURN(rc);
1267 }
1268
1269 /**
1270  * Check whether QoS allocation should be used.
1271  *
1272  * A simple helper to decide when QoS allocation should be used:
1273  * if it's just a single available target or the used space is
1274  * evenly distributed among the targets at the moment, then QoS
1275  * allocation algorithm should not be used.
1276  *
1277  * \param[in] lod       LOD device
1278  *
1279  * \retval 0            should not be used
1280  * \retval 1            should be used
1281  */
1282 static inline int lod_qos_is_usable(struct lod_device *lod)
1283 {
1284 #ifdef FORCE_QOS
1285         /* to be able to debug QoS code */
1286         return 1;
1287 #endif
1288
1289         /* Detect -EAGAIN early, before expensive lock is taken. */
1290         if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1291                 return 0;
1292
1293         if (lod->lod_desc.ld_active_tgt_count < 2)
1294                 return 0;
1295
1296         return 1;
1297 }
1298
1299 /**
1300  * Allocate a striping using an algorithm with weights.
1301  *
1302  * The function allocates OST objects to create a striping. The algorithm
1303  * used is based on weights (currently only using the free space), and it's
1304  * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1305  * configuration (# of stripes, offset, pool) is taken from the object and
1306  * is prepared by the caller.
1307  *
1308  * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1309  * be met due to too few OSTs, then allocation fails. If the flag is passed
1310  * fewer than 3/4 of the requested number of stripes can be allocated, then
1311  * allocation fails.
1312  *
1313  * No concurrent allocation is allowed on the object and this must be ensured
1314  * by the caller. All the internal structures are protected by the function.
1315  *
1316  * The algorithm has two steps: find available OSTs and calculate their
1317  * weights, then select the OSTs with their weights used as the probability.
1318  * An OST with a higher weight is proportionately more likely to be selected
1319  * than one with a lower weight.
1320  *
1321  * \param[in] env       execution environment for this thread
1322  * \param[in] lo        LOD object
1323  * \param[out] stripe   striping created
1324  * \param[in] flags     0 or LOV_USES_DEFAULT_STRIPE
1325  * \param[in] th        transaction handle
1326  *
1327  * \retval 0            on success
1328  * \retval -EAGAIN      not enough OSTs are found for specified stripe count
1329  * \retval -EINVAL      requested OST index is invalid
1330  * \retval negative     errno on failure
1331  */
1332 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1333                          struct dt_object **stripe, int flags,
1334                          struct thandle *th)
1335 {
1336         struct lod_device   *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1337         struct obd_statfs   *sfs = &lod_env_info(env)->lti_osfs;
1338         struct lod_tgt_desc *ost;
1339         struct dt_object    *o;
1340         __u64                total_weight = 0;
1341         unsigned int         i;
1342         int                  rc = 0;
1343         __u32                nfound, good_osts;
1344         __u32                stripe_cnt = lo->ldo_stripenr;
1345         __u32                stripe_cnt_min;
1346         struct pool_desc    *pool = NULL;
1347         struct ost_pool    *osts;
1348         ENTRY;
1349
1350         stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1351         if (stripe_cnt_min < 1)
1352                 RETURN(-EINVAL);
1353
1354         if (lo->ldo_pool)
1355                 pool = lod_find_pool(m, lo->ldo_pool);
1356
1357         if (pool != NULL) {
1358                 down_read(&pool_tgt_rw_sem(pool));
1359                 osts = &(pool->pool_obds);
1360         } else {
1361                 osts = &(m->lod_pool_info);
1362         }
1363
1364         /* Detect -EAGAIN early, before expensive lock is taken. */
1365         if (!lod_qos_is_usable(m))
1366                 GOTO(out_nolock, rc = -EAGAIN);
1367
1368         /* Do actual allocation, use write lock here. */
1369         down_write(&m->lod_qos.lq_rw_sem);
1370
1371         /*
1372          * Check again, while we were sleeping on @lq_rw_sem things could
1373          * change.
1374          */
1375         if (!lod_qos_is_usable(m))
1376                 GOTO(out, rc = -EAGAIN);
1377
1378         rc = lod_qos_calc_ppo(m);
1379         if (rc)
1380                 GOTO(out, rc);
1381
1382         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1383         if (rc)
1384                 GOTO(out, rc);
1385
1386         good_osts = 0;
1387         /* Find all the OSTs that are valid stripe candidates */
1388         for (i = 0; i < osts->op_count; i++) {
1389                 if (!cfs_bitmap_check(m->lod_ost_bitmap, osts->op_array[i]))
1390                         continue;
1391
1392                 rc = lod_statfs_and_check(env, m, osts->op_array[i], sfs);
1393                 if (rc) {
1394                         /* this OSP doesn't feel well */
1395                         continue;
1396                 }
1397
1398                 /* Fail Check before osc_precreate() is called
1399                    so we can only 'fail' single OSC. */
1400                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1401                                    osts->op_array[i] == 0)
1402                         continue;
1403
1404                 ost = OST_TGT(m,osts->op_array[i]);
1405                 ost->ltd_qos.ltq_usable = 1;
1406                 lod_qos_calc_weight(m, osts->op_array[i]);
1407                 total_weight += ost->ltd_qos.ltq_weight;
1408
1409                 good_osts++;
1410         }
1411
1412         QOS_DEBUG("found %d good osts\n", good_osts);
1413
1414         if (good_osts < stripe_cnt_min)
1415                 GOTO(out, rc = -EAGAIN);
1416
1417         /* We have enough osts */
1418         if (good_osts < stripe_cnt)
1419                 stripe_cnt = good_osts;
1420
1421         /* Find enough OSTs with weighted random allocation. */
1422         nfound = 0;
1423         while (nfound < stripe_cnt) {
1424                 __u64 rand, cur_weight;
1425
1426                 cur_weight = 0;
1427                 rc = -ENOSPC;
1428
1429                 if (total_weight) {
1430 #if BITS_PER_LONG == 32
1431                         rand = cfs_rand() % (unsigned)total_weight;
1432                         /* If total_weight > 32-bit, first generate the high
1433                          * 32 bits of the random number, then add in the low
1434                          * 32 bits (truncated to the upper limit, if needed) */
1435                         if (total_weight > 0xffffffffULL)
1436                                 rand = (__u64)(cfs_rand() %
1437                                         (unsigned)(total_weight >> 32)) << 32;
1438                         else
1439                                 rand = 0;
1440
1441                         if (rand == (total_weight & 0xffffffff00000000ULL))
1442                                 rand |= cfs_rand() % (unsigned)total_weight;
1443                         else
1444                                 rand |= cfs_rand();
1445
1446 #else
1447                         rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1448                                 total_weight;
1449 #endif
1450                 } else {
1451                         rand = 0;
1452                 }
1453
1454                 /* On average, this will hit larger-weighted OSTs more often.
1455                  * 0-weight OSTs will always get used last (only when rand=0) */
1456                 for (i = 0; i < osts->op_count; i++) {
1457                         __u32 idx = osts->op_array[i];
1458
1459                         if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
1460                                 continue;
1461
1462                         ost = OST_TGT(m,idx);
1463
1464                         if (!ost->ltd_qos.ltq_usable)
1465                                 continue;
1466
1467                         cur_weight += ost->ltd_qos.ltq_weight;
1468                         QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight="LPU64
1469                                   " rand="LPU64" total_weight="LPU64"\n",
1470                                   stripe_cnt, nfound, cur_weight, rand,
1471                                   total_weight);
1472
1473                         if (cur_weight < rand)
1474                                 continue;
1475
1476                         QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1477
1478                         /*
1479                          * do not put >1 objects on a single OST
1480                          */
1481                         if (lod_qos_is_ost_used(env, idx, nfound))
1482                                 continue;
1483                         lod_qos_ost_in_use(env, nfound, idx);
1484
1485                         o = lod_qos_declare_object_on(env, m, idx, th);
1486                         if (IS_ERR(o)) {
1487                                 QOS_DEBUG("can't declare object on #%u: %d\n",
1488                                           idx, (int) PTR_ERR(o));
1489                                 continue;
1490                         }
1491                         stripe[nfound++] = o;
1492                         lod_qos_used(m, osts, idx, &total_weight);
1493                         rc = 0;
1494                         break;
1495                 }
1496
1497                 if (rc) {
1498                         /* no OST found on this iteration, give up */
1499                         break;
1500                 }
1501         }
1502
1503         if (unlikely(nfound != stripe_cnt)) {
1504                 /*
1505                  * when the decision to use weighted algorithm was made
1506                  * we had enough appropriate OSPs, but this state can
1507                  * change anytime (no space on OST, broken connection, etc)
1508                  * so it's possible OSP won't be able to provide us with
1509                  * an object due to just changed state
1510                  */
1511                 LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
1512                 for (i = 0; i < nfound; i++) {
1513                         LASSERT(stripe[i] != NULL);
1514                         lu_object_put(env, &stripe[i]->do_lu);
1515                         stripe[i] = NULL;
1516                 }
1517
1518                 /* makes sense to rebalance next time */
1519                 m->lod_qos.lq_dirty = 1;
1520                 m->lod_qos.lq_same_space = 0;
1521
1522                 rc = -EAGAIN;
1523         }
1524
1525 out:
1526         up_write(&m->lod_qos.lq_rw_sem);
1527
1528 out_nolock:
1529         if (pool != NULL) {
1530                 up_read(&pool_tgt_rw_sem(pool));
1531                 /* put back ref got by lod_find_pool() */
1532                 lod_pool_putref(pool);
1533         }
1534
1535         RETURN(rc);
1536 }
1537
1538 /**
1539  * Find largest stripe count the caller can use.
1540  *
1541  * Find the maximal possible stripe count not greater than \a stripe_count.
1542  * Sometimes suggested stripecount can't be reached for a number of reasons:
1543  * lack of enough active OSTs or the backend does not support EAs that large.
1544  * If the passed one is 0, then the filesystem's default one is used.
1545  *
1546  * \param[in] lod       LOD device
1547  * \param[in] magic     the format if striping
1548  * \param[in] stripe_count      count the caller would like to use
1549  *
1550  * \retval              the maximum usable stripe count
1551  */
1552 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1553                                __u16 stripe_count)
1554 {
1555         __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1556
1557         if (!stripe_count)
1558                 stripe_count = lod->lod_desc.ld_default_stripe_count;
1559         if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1560                 stripe_count = lod->lod_desc.ld_active_tgt_count;
1561         if (!stripe_count)
1562                 stripe_count = 1;
1563
1564         /* stripe count is based on whether OSD can handle larger EA sizes */
1565         if (lod->lod_osd_max_easize > 0)
1566                 max_stripes = lov_mds_md_max_stripe_count(
1567                         lod->lod_osd_max_easize, magic);
1568
1569         return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1570 }
1571
1572 /**
1573  * Create in-core respresentation for a fully-defined striping
1574  *
1575  * When the caller passes a fully-defined striping (i.e. everything including
1576  * OST object FIDs are defined), then we still need to instantiate LU-cache
1577  * with the objects representing the stripes defined. This function completes
1578  * that task.
1579  *
1580  * \param[in] env       execution environment for this thread
1581  * \param[in] mo        LOD object
1582  * \param[in] buf       buffer containing the striping
1583  *
1584  * \retval 0            on success
1585  * \retval negative     negated errno on error
1586  */
1587 static int lod_use_defined_striping(const struct lu_env *env,
1588                                     struct lod_object *mo,
1589                                     const struct lu_buf *buf)
1590 {
1591         struct lov_mds_md_v1   *v1 = buf->lb_buf;
1592         struct lov_mds_md_v3   *v3 = buf->lb_buf;
1593         struct lov_ost_data_v1 *objs;
1594         __u32                   magic;
1595         int                     rc = 0;
1596         ENTRY;
1597
1598         magic = le32_to_cpu(v1->lmm_magic);
1599         if (magic == LOV_MAGIC_V1_DEF) {
1600                 magic = LOV_MAGIC_V1;
1601                 objs = &v1->lmm_objects[0];
1602         } else if (magic == LOV_MAGIC_V3_DEF) {
1603                 magic = LOV_MAGIC_V3;
1604                 objs = &v3->lmm_objects[0];
1605                 lod_object_set_pool(mo, v3->lmm_pool_name);
1606         } else {
1607                 GOTO(out, rc = -EINVAL);
1608         }
1609
1610         mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1611         mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1612         mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1613         mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1614
1615         /* fixup for released file before object initialization */
1616         if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1617                 mo->ldo_released_stripenr = mo->ldo_stripenr;
1618                 mo->ldo_stripenr = 0;
1619         }
1620
1621         LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1622
1623         if (mo->ldo_stripenr > 0)
1624                 rc = lod_initialize_objects(env, mo, objs);
1625
1626 out:
1627         RETURN(rc);
1628 }
1629
1630 /**
1631  * Parse suggested striping configuration.
1632  *
1633  * The caller gets a suggested striping configuration from a number of sources
1634  * including per-directory default and applications. Then it needs to verify
1635  * the suggested striping is valid, apply missing bits and store the resulting
1636  * configuration in the object to be used by the allocator later. Must not be
1637  * called concurrently against the same object. It's OK to provide a
1638  * fully-defined striping.
1639  *
1640  * \param[in] env       execution environment for this thread
1641  * \param[in] lo        LOD object
1642  * \param[in] buf       buffer containing the striping
1643  *
1644  * \retval 0            on success
1645  * \retval negative     negated errno on error
1646  */
1647 static int lod_qos_parse_config(const struct lu_env *env,
1648                                 struct lod_object *lo,
1649                                 const struct lu_buf *buf)
1650 {
1651         struct lod_device     *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1652         struct lov_user_md_v1 *v1 = NULL;
1653         struct lov_user_md_v3 *v3 = NULL;
1654         char                  *pool_name = NULL;
1655         __u32                  magic;
1656         int                    rc;
1657         unsigned int           size;
1658         ENTRY;
1659
1660         if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1661                 RETURN(0);
1662
1663         v3 = buf->lb_buf;
1664         v1 = buf->lb_buf;
1665         magic = v1->lmm_magic;
1666
1667         if (unlikely(magic == LOV_MAGIC_V1_DEF || magic == LOV_MAGIC_V3_DEF)) {
1668                 /* try to use as fully defined striping */
1669                 rc = lod_use_defined_striping(env, lo, buf);
1670                 RETURN(rc);
1671         }
1672
1673         switch (magic) {
1674         case __swab32(LOV_USER_MAGIC_V1):
1675                 lustre_swab_lov_user_md_v1(v1);
1676                 magic = v1->lmm_magic;
1677                 /* fall through */
1678         case LOV_USER_MAGIC_V1:
1679                 size = sizeof(*v1);
1680                 break;
1681
1682         case __swab32(LOV_USER_MAGIC_V3):
1683                 lustre_swab_lov_user_md_v3(v3);
1684                 magic = v3->lmm_magic;
1685                 /* fall through */
1686         case LOV_USER_MAGIC_V3:
1687                 size = sizeof(*v3);
1688                 pool_name = v3->lmm_pool_name;
1689                 break;
1690
1691         case __swab32(LOV_USER_MAGIC_SPECIFIC):
1692                 lustre_swab_lov_user_md_v3(v3);
1693                 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1694                                                 v3->lmm_stripe_count);
1695                 magic = v3->lmm_magic;
1696                 /* fall through */
1697         case LOV_USER_MAGIC_SPECIFIC:
1698                 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
1699                         v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
1700                 if (v3->lmm_pool_name[0] != '\0')
1701                         pool_name = v3->lmm_pool_name;
1702                 size = lov_user_md_size(v3->lmm_stripe_count,
1703                                         LOV_USER_MAGIC_SPECIFIC);
1704                 break;
1705
1706         default:
1707                 CERROR("%s: unrecognized magic %X\n",
1708                        lod2obd(d)->obd_name, magic);
1709                 RETURN(-EINVAL);
1710         }
1711
1712         if (unlikely(buf->lb_len < size)) {
1713                 CERROR("%s: wrong size: %zd, expect: %u\n",
1714                        lod2obd(d)->obd_name, buf->lb_len, size);
1715                 RETURN(-EINVAL);
1716         }
1717
1718         lustre_print_user_md(D_OTHER, v1, "parse config");
1719
1720         v1->lmm_magic = magic;
1721         if (v1->lmm_pattern == 0)
1722                 v1->lmm_pattern = LOV_PATTERN_RAID0;
1723         if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1724                 CERROR("%s: invalid pattern: %x\n",
1725                        lod2obd(d)->obd_name, v1->lmm_pattern);
1726                 RETURN(-EINVAL);
1727         }
1728         lo->ldo_pattern = v1->lmm_pattern;
1729
1730         if (v1->lmm_stripe_size > 0)
1731                 lo->ldo_stripe_size = v1->lmm_stripe_size;
1732
1733         if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1734                 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1735
1736         if (v1->lmm_stripe_count > 0)
1737                 lo->ldo_stripenr = v1->lmm_stripe_count;
1738
1739         lo->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1740
1741         lod_object_set_pool(lo, NULL);
1742         if (pool_name != NULL) {
1743                 struct pool_desc *pool;
1744
1745                 /* In the function below, .hs_keycmp resolves to
1746                  * pool_hashkey_keycmp() */
1747                 /* coverity[overrun-buffer-val] */
1748                 pool = lod_find_pool(d, pool_name);
1749                 if (pool != NULL) {
1750                         if (lo->ldo_def_stripe_offset != LOV_OFFSET_DEFAULT) {
1751                                 rc = lod_check_index_in_pool(
1752                                                lo->ldo_def_stripe_offset, pool);
1753                                 if (rc < 0) {
1754                                         lod_pool_putref(pool);
1755                                         CERROR("%s: invalid offset, %u\n",
1756                                                lod2obd(d)->obd_name,
1757                                                lo->ldo_def_stripe_offset);
1758                                         RETURN(-EINVAL);
1759                                 }
1760                         }
1761
1762                         if (lo->ldo_stripenr > pool_tgt_count(pool))
1763                                 lo->ldo_stripenr = pool_tgt_count(pool);
1764
1765                         lod_pool_putref(pool);
1766                 }
1767
1768                 lod_object_set_pool(lo, pool_name);
1769         }
1770
1771         /* fixup for released file */
1772         if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1773                 lo->ldo_released_stripenr = lo->ldo_stripenr;
1774                 lo->ldo_stripenr = 0;
1775         }
1776
1777         RETURN(0);
1778 }
1779
1780 /**
1781  * Create a striping for an obejct.
1782  *
1783  * The function creates a new striping for the object. A buffer containing
1784  * configuration hints can be provided optionally. The function tries QoS
1785  * algorithm first unless free space is distributed evenly among OSTs, but
1786  * by default RR algorithm is preferred due to internal concurrency (QoS is
1787  * serialized). The caller must ensure no concurrent calls to the function
1788  * are made against the same object.
1789  *
1790  * \param[in] env       execution environment for this thread
1791  * \param[in] lo        LOD object
1792  * \param[in] attr      attributes OST objects will be declared with
1793  * \param[in] buf       suggested striping configuration or NULL
1794  * \param[in] th        transaction handle
1795  *
1796  * \retval 0            on success
1797  * \retval negative     negated errno on error
1798  */
1799 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1800                         struct lu_attr *attr, const struct lu_buf *buf,
1801                         struct thandle *th)
1802 {
1803         struct lod_device      *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1804         struct dt_object      **stripe;
1805         int                     stripe_len;
1806         int                     flag = LOV_USES_ASSIGNED_STRIPE;
1807         int                     i, rc;
1808         ENTRY;
1809
1810         LASSERT(lo);
1811
1812         /* no OST available */
1813         /* XXX: should we be waiting a bit to prevent failures during
1814          * cluster initialization? */
1815         if (d->lod_ostnr == 0)
1816                 GOTO(out, rc = -EIO);
1817
1818         /*
1819          * by this time, the object's ldo_stripenr and ldo_stripe_size
1820          * contain default value for striping: taken from the parent
1821          * or from filesystem defaults
1822          *
1823          * in case the caller is passing lovea with new striping config,
1824          * we may need to parse lovea and apply new configuration
1825          */
1826         rc = lod_qos_parse_config(env, lo, buf);
1827         if (rc)
1828                 GOTO(out, rc);
1829
1830         /* A released file is being created */
1831         if (lo->ldo_stripenr == 0)
1832                 GOTO(out, rc = 0);
1833
1834         if (likely(lo->ldo_stripe == NULL)) {
1835                 struct lov_user_md *lum = NULL;
1836
1837                 /*
1838                  * no striping has been created so far
1839                  */
1840                 LASSERT(lo->ldo_stripenr > 0);
1841                 /*
1842                  * statfs and check OST targets now, since ld_active_tgt_count
1843                  * could be changed if some OSTs are [de]activated manually.
1844                  */
1845                 lod_qos_statfs_update(env, d);
1846                 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1847                                                      lo->ldo_stripenr);
1848
1849                 stripe_len = lo->ldo_stripenr;
1850                 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1851                 if (stripe == NULL)
1852                         GOTO(out, rc = -ENOMEM);
1853
1854                 lod_getref(&d->lod_ost_descs);
1855                 /* XXX: support for non-0 files w/o objects */
1856                 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1857                                 d->lod_desc.ld_tgt_count, stripe_len);
1858
1859                 if (buf != NULL && buf->lb_buf != NULL)
1860                         lum = buf->lb_buf;
1861
1862                 if (lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
1863                         rc = lod_alloc_ost_list(env, lo, stripe, lum, th);
1864                 } else if (lo->ldo_def_stripe_offset == LOV_OFFSET_DEFAULT) {
1865                         rc = lod_alloc_qos(env, lo, stripe, flag, th);
1866                         if (rc == -EAGAIN)
1867                                 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1868                 } else {
1869                         rc = lod_alloc_specific(env, lo, stripe, flag, th);
1870                 }
1871                 lod_putref(d, &d->lod_ost_descs);
1872
1873                 if (rc < 0) {
1874                         for (i = 0; i < stripe_len; i++)
1875                                 if (stripe[i] != NULL)
1876                                         lu_object_put(env, &stripe[i]->do_lu);
1877
1878                         OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1879                         lo->ldo_stripenr = 0;
1880                 } else {
1881                         lo->ldo_stripe = stripe;
1882                         lo->ldo_stripes_allocated = stripe_len;
1883                 }
1884         } else {
1885                 /*
1886                  * lod_qos_parse_config() found supplied buf as a predefined
1887                  * striping (not a hint), so it allocated all the object
1888                  * now we need to create them
1889                  */
1890                 for (i = 0; i < lo->ldo_stripenr; i++) {
1891                         struct dt_object  *o;
1892
1893                         o = lo->ldo_stripe[i];
1894                         LASSERT(o);
1895
1896                         rc = lod_sub_object_declare_create(env, o, attr, NULL,
1897                                                            NULL, th);
1898                         if (rc < 0) {
1899                                 CERROR("can't declare create: %d\n", rc);
1900                                 break;
1901                         }
1902                 }
1903         }
1904
1905 out:
1906         RETURN(rc);
1907 }
1908