Whamcloud - gitweb
LU-10070 lod: SEL: Implement basic spillover space
[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, 2017, 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 <linux/random.h>
42
43 #include <libcfs/libcfs.h>
44 #include <uapi/linux/lustre/lustre_idl.h>
45 #include <lustre_swab.h>
46 #include <obd_class.h>
47
48 #include "lod_internal.h"
49
50 /*
51  * force QoS policy (not RR) to be used for testing purposes
52  */
53 #define FORCE_QOS_
54
55 #define D_QOS   D_OTHER
56
57 #define QOS_DEBUG(fmt, ...)     CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
58 #define QOS_CONSOLE(fmt, ...)   LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
59
60 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
61                        OST_TGT(lod,i)->ltd_statfs.os_bsize)
62
63 /**
64  * Check whether the target is available for new OST objects.
65  *
66  * Request statfs data from the given target and verify it's active and not
67  * read-only. If so, then it can be used to place new OST objects. This
68  * function also maintains the number of active/inactive targets and sets
69  * dirty flags if those numbers change so others can run re-balance procedures.
70  * No external locking is required.
71  *
72  * \param[in] env       execution environment for this thread
73  * \param[in] d         LOD device
74  * \param[in] index     index of OST target to check
75  * \param[out] sfs      buffer for statfs data
76  *
77  * \retval 0            if the target is good
78  * \retval negative     negated errno on error
79
80  */
81 int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
82                          int index, struct obd_statfs *sfs,
83                          struct obd_statfs_info *info)
84 {
85         struct lod_tgt_desc *ost;
86         int                  rc;
87         ENTRY;
88
89         LASSERT(d);
90         ost = OST_TGT(d,index);
91         LASSERT(ost);
92
93         rc = dt_statfs(env, ost->ltd_ost, sfs, info);
94
95         if (rc == 0 && ((sfs->os_state & OS_STATE_ENOSPC) ||
96             (sfs->os_state & OS_STATE_ENOINO && sfs->os_fprecreated == 0)))
97                 RETURN(-ENOSPC);
98
99         if (rc && rc != -ENOTCONN)
100                 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
101
102         /* If the OST is readonly then we can't allocate objects there */
103         if (sfs->os_state & OS_STATE_READONLY)
104                 rc = -EROFS;
105
106         /* object precreation is skipped on the OST with max_create_count=0 */
107         if (sfs->os_state & OS_STATE_NOPRECREATE)
108                 rc = -ENOBUFS;
109
110         /* check whether device has changed state (active, inactive) */
111         if (rc != 0 && ost->ltd_active) {
112                 /* turned inactive? */
113                 spin_lock(&d->lod_lock);
114                 if (ost->ltd_active) {
115                         ost->ltd_active = 0;
116                         if (rc == -ENOTCONN)
117                                 ost->ltd_connecting = 1;
118
119                         LASSERT(d->lod_desc.ld_active_tgt_count > 0);
120                         d->lod_desc.ld_active_tgt_count--;
121                         d->lod_qos.lq_dirty = 1;
122                         d->lod_qos.lq_rr.lqr_dirty = 1;
123                         CDEBUG(D_CONFIG, "%s: turns inactive\n",
124                                ost->ltd_exp->exp_obd->obd_name);
125                 }
126                 spin_unlock(&d->lod_lock);
127         } else if (rc == 0 && ost->ltd_active == 0) {
128                 /* turned active? */
129                 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
130                          "active tgt count %d, ost nr %d\n",
131                          d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
132                 spin_lock(&d->lod_lock);
133                 if (ost->ltd_active == 0) {
134                         ost->ltd_active = 1;
135                         ost->ltd_connecting = 0;
136                         d->lod_desc.ld_active_tgt_count++;
137                         d->lod_qos.lq_dirty = 1;
138                         d->lod_qos.lq_rr.lqr_dirty = 1;
139                         CDEBUG(D_CONFIG, "%s: turns active\n",
140                                ost->ltd_exp->exp_obd->obd_name);
141                 }
142                 spin_unlock(&d->lod_lock);
143         }
144
145         RETURN(rc);
146 }
147
148 /**
149  * Maintain per-target statfs data.
150  *
151  * The function refreshes statfs data for all the targets every N seconds.
152  * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
153  * initially.
154  *
155  * \param[in] env       execution environment for this thread
156  * \param[in] lod       LOD device
157  */
158 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod)
159 {
160         struct obd_device *obd = lod2obd(lod);
161         struct ost_pool *osts = &(lod->lod_pool_info);
162         time64_t max_age;
163         unsigned int i;
164         u64 avail;
165         int idx;
166         ENTRY;
167
168         max_age = ktime_get_seconds() - 2 * lod->lod_desc.ld_qos_maxage;
169
170         if (obd->obd_osfs_age > max_age)
171                 /* statfs data are quite recent, don't need to refresh it */
172                 RETURN_EXIT;
173
174         down_write(&lod->lod_qos.lq_rw_sem);
175
176         if (obd->obd_osfs_age > max_age)
177                 goto out;
178
179         for (i = 0; i < osts->op_count; i++) {
180                 idx = osts->op_array[i];
181                 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
182                 if (lod_statfs_and_check(env, lod, idx,
183                                          &OST_TGT(lod, idx)->ltd_statfs, NULL))
184                         continue;
185                 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
186                         /* recalculate weigths */
187                         lod->lod_qos.lq_dirty = 1;
188         }
189         obd->obd_osfs_age = ktime_get_seconds();
190
191 out:
192         up_write(&lod->lod_qos.lq_rw_sem);
193         EXIT;
194 }
195
196 /**
197  * Calculate per-OST and per-OSS penalties
198  *
199  * Re-calculate penalties when the configuration changes, active targets
200  * change and after statfs refresh (all these are reflected by lq_dirty flag).
201  * On every OST and OSS: decay the penalty by half for every 8x the update
202  * interval that the device has been idle. That gives lots of time for the
203  * statfs information to be updated (which the penalty is only a proxy for),
204  * and avoids penalizing OSS/OSTs under light load.
205  * See lod_qos_calc_weight() for how penalties are factored into the weight.
206  *
207  * \param[in] lod       LOD device
208  *
209  * \retval 0            on success
210  * \retval -EAGAIN      the number of OSTs isn't enough
211  */
212 static int lod_qos_calc_ppo(struct lod_device *lod)
213 {
214         struct lu_svr_qos *oss;
215         __u64 ba_max, ba_min, temp;
216         __u32 num_active;
217         unsigned int i;
218         int rc, prio_wide;
219         time64_t now, age;
220
221         ENTRY;
222
223         if (!lod->lod_qos.lq_dirty)
224                 GOTO(out, rc = 0);
225
226         num_active = lod->lod_desc.ld_active_tgt_count - 1;
227         if (num_active < 1)
228                 GOTO(out, rc = -EAGAIN);
229
230         /* find bavail on each OSS */
231         list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list)
232                 oss->lsq_bavail = 0;
233         lod->lod_qos.lq_active_svr_count = 0;
234
235         /*
236          * How badly user wants to select OSTs "widely" (not recently chosen
237          * and not on recent OSS's).  As opposed to "freely" (free space
238          * avail.) 0-256
239          */
240         prio_wide = 256 - lod->lod_qos.lq_prio_free;
241
242         ba_min = (__u64)(-1);
243         ba_max = 0;
244         now = ktime_get_real_seconds();
245         /* Calculate OST penalty per object
246          * (lod ref taken in lod_qos_prep_create())
247          */
248         cfs_foreach_bit(lod->lod_ost_bitmap, i) {
249                 LASSERT(OST_TGT(lod,i));
250                 temp = TGT_BAVAIL(i);
251                 if (!temp)
252                         continue;
253                 ba_min = min(temp, ba_min);
254                 ba_max = max(temp, ba_max);
255
256                 /* Count the number of usable OSS's */
257                 if (OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail == 0)
258                         lod->lod_qos.lq_active_svr_count++;
259                 OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail += temp;
260
261                 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
262                 temp >>= 1;
263                 do_div(temp, num_active);
264                 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
265                         (temp * prio_wide) >> 8;
266
267                 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
268                 if (lod->lod_qos.lq_reset ||
269                     age > 32 * lod->lod_desc.ld_qos_maxage)
270                         OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
271                 else if (age > lod->lod_desc.ld_qos_maxage)
272                         /* Decay OST penalty. */
273                         OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
274                                 (age / lod->lod_desc.ld_qos_maxage);
275         }
276
277         num_active = lod->lod_qos.lq_active_svr_count - 1;
278         if (num_active < 1) {
279                 /* If there's only 1 OSS, we can't penalize it, so instead
280                    we have to double the OST penalty */
281                 num_active = 1;
282                 cfs_foreach_bit(lod->lod_ost_bitmap, i)
283                         OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
284         }
285
286         /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
287         list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
288                 temp = oss->lsq_bavail >> 1;
289                 do_div(temp, oss->lsq_tgt_count * num_active);
290                 oss->lsq_penalty_per_obj = (temp * prio_wide) >> 8;
291
292                 age = (now - oss->lsq_used) >> 3;
293                 if (lod->lod_qos.lq_reset ||
294                     age > 32 * lod->lod_desc.ld_qos_maxage)
295                         oss->lsq_penalty = 0;
296                 else if (age > lod->lod_desc.ld_qos_maxage)
297                         /* Decay OSS penalty. */
298                         oss->lsq_penalty >>= age / lod->lod_desc.ld_qos_maxage;
299         }
300
301         lod->lod_qos.lq_dirty = 0;
302         lod->lod_qos.lq_reset = 0;
303
304         /* If each ost has almost same free space,
305          * do rr allocation for better creation performance */
306         lod->lod_qos.lq_same_space = 0;
307         if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
308                 lod->lod_qos.lq_same_space = 1;
309                 /* Reset weights for the next time we enter qos mode */
310                 lod->lod_qos.lq_reset = 1;
311         }
312         rc = 0;
313
314 out:
315 #ifndef FORCE_QOS
316         if (!rc && lod->lod_qos.lq_same_space)
317                 RETURN(-EAGAIN);
318 #endif
319         RETURN(rc);
320 }
321
322 /**
323  * Calculate weight for a given OST target.
324  *
325  * The final OST weight is the number of bytes available minus the OST and
326  * OSS penalties.  See lod_qos_calc_ppo() for how penalties are calculated.
327  *
328  * \param[in] lod       LOD device, where OST targets are listed
329  * \param[in] i         OST target index
330  *
331  * \retval              0
332  */
333 static int lod_qos_calc_weight(struct lod_device *lod, int i)
334 {
335         __u64 temp, temp2;
336
337         temp = TGT_BAVAIL(i);
338         temp2 = OST_TGT(lod, i)->ltd_qos.ltq_penalty +
339                 OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_penalty;
340         if (temp < temp2)
341                 OST_TGT(lod, i)->ltd_qos.ltq_weight = 0;
342         else
343                 OST_TGT(lod, i)->ltd_qos.ltq_weight = temp - temp2;
344         return 0;
345 }
346
347 /**
348  * Re-calculate weights.
349  *
350  * The function is called when some OST target was used for a new object. In
351  * this case we should re-calculate all the weights to keep new allocations
352  * balanced well.
353  *
354  * \param[in] lod       LOD device
355  * \param[in] osts      OST pool where a new object was placed
356  * \param[in] index     OST target where a new object was placed
357  * \param[out] total_wt new total weight for the pool
358  *
359  * \retval              0
360  */
361 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
362                         __u32 index, __u64 *total_wt)
363 {
364         struct lod_tgt_desc *ost;
365         struct lu_svr_qos  *oss;
366         unsigned int j;
367         ENTRY;
368
369         ost = OST_TGT(lod,index);
370         LASSERT(ost);
371
372         /* Don't allocate on this devuce anymore, until the next alloc_qos */
373         ost->ltd_qos.ltq_usable = 0;
374
375         oss = ost->ltd_qos.ltq_svr;
376
377         /* Decay old penalty by half (we're adding max penalty, and don't
378            want it to run away.) */
379         ost->ltd_qos.ltq_penalty >>= 1;
380         oss->lsq_penalty >>= 1;
381
382         /* mark the OSS and OST as recently used */
383         ost->ltd_qos.ltq_used = oss->lsq_used = ktime_get_real_seconds();
384
385         /* Set max penalties for this OST and OSS */
386         ost->ltd_qos.ltq_penalty +=
387                 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
388         oss->lsq_penalty += oss->lsq_penalty_per_obj *
389                 lod->lod_qos.lq_active_svr_count;
390
391         /* Decrease all OSS penalties */
392         list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
393                 if (oss->lsq_penalty < oss->lsq_penalty_per_obj)
394                         oss->lsq_penalty = 0;
395                 else
396                         oss->lsq_penalty -= oss->lsq_penalty_per_obj;
397         }
398
399         *total_wt = 0;
400         /* Decrease all OST penalties */
401         for (j = 0; j < osts->op_count; j++) {
402                 int i;
403
404                 i = osts->op_array[j];
405                 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
406                         continue;
407
408                 ost = OST_TGT(lod,i);
409                 LASSERT(ost);
410
411                 if (ost->ltd_qos.ltq_penalty <
412                                 ost->ltd_qos.ltq_penalty_per_obj)
413                         ost->ltd_qos.ltq_penalty = 0;
414                 else
415                         ost->ltd_qos.ltq_penalty -=
416                                 ost->ltd_qos.ltq_penalty_per_obj;
417
418                 lod_qos_calc_weight(lod, i);
419
420                 /* Recalc the total weight of usable osts */
421                 if (ost->ltd_qos.ltq_usable)
422                         *total_wt += ost->ltd_qos.ltq_weight;
423
424                 QOS_DEBUG("recalc tgt %d usable=%d avail=%llu"
425                           " ostppo=%llu ostp=%llu ossppo=%llu"
426                           " ossp=%llu wt=%llu\n",
427                           i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
428                           ost->ltd_qos.ltq_penalty_per_obj >> 10,
429                           ost->ltd_qos.ltq_penalty >> 10,
430                           ost->ltd_qos.ltq_svr->lsq_penalty_per_obj >> 10,
431                           ost->ltd_qos.ltq_svr->lsq_penalty >> 10,
432                           ost->ltd_qos.ltq_weight >> 10);
433         }
434
435         RETURN(0);
436 }
437
438 void lod_qos_rr_init(struct lu_qos_rr *lqr)
439 {
440         spin_lock_init(&lqr->lqr_alloc);
441         lqr->lqr_dirty = 1;
442 }
443
444 #define LOV_QOS_EMPTY ((__u32)-1)
445
446 /**
447  * Calculate optimal round-robin order with regard to OSSes.
448  *
449  * Place all the OSTs from pool \a src_pool in a special array to be used for
450  * round-robin (RR) stripe allocation.  The placement algorithm interleaves
451  * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
452  * Resorts the targets when the number of active targets changes (because of
453  * a new target or activation/deactivation).
454  *
455  * \param[in] lod       LOD device
456  * \param[in] src_pool  OST pool
457  * \param[in] lqr       round-robin list
458  *
459  * \retval 0            on success
460  * \retval -ENOMEM      fails to allocate the array
461  */
462 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
463                            struct lu_qos_rr *lqr)
464 {
465         struct lu_svr_qos  *oss;
466         struct lod_tgt_desc *ost;
467         unsigned placed, real_count;
468         unsigned int i;
469         int rc;
470         ENTRY;
471
472         if (!lqr->lqr_dirty) {
473                 LASSERT(lqr->lqr_pool.op_size);
474                 RETURN(0);
475         }
476
477         /* Do actual allocation. */
478         down_write(&lod->lod_qos.lq_rw_sem);
479
480         /*
481          * Check again. While we were sleeping on @lq_rw_sem something could
482          * change.
483          */
484         if (!lqr->lqr_dirty) {
485                 LASSERT(lqr->lqr_pool.op_size);
486                 up_write(&lod->lod_qos.lq_rw_sem);
487                 RETURN(0);
488         }
489
490         real_count = src_pool->op_count;
491
492         /* Zero the pool array */
493         /* alloc_rr is holding a read lock on the pool, so nobody is adding/
494            deleting from the pool. The lq_rw_sem insures that nobody else
495            is reading. */
496         lqr->lqr_pool.op_count = real_count;
497         rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
498         if (rc) {
499                 up_write(&lod->lod_qos.lq_rw_sem);
500                 RETURN(rc);
501         }
502         for (i = 0; i < lqr->lqr_pool.op_count; i++)
503                 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
504
505         /* Place all the OSTs from 1 OSS at the same time. */
506         placed = 0;
507         list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
508                 int j = 0;
509
510                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
511                         int next;
512
513                         if (!cfs_bitmap_check(lod->lod_ost_bitmap,
514                                                 src_pool->op_array[i]))
515                                 continue;
516
517                         ost = OST_TGT(lod,src_pool->op_array[i]);
518                         LASSERT(ost && ost->ltd_ost);
519                         if (ost->ltd_qos.ltq_svr != oss)
520                                 continue;
521
522                         /* Evenly space these OSTs across arrayspace */
523                         next = j * lqr->lqr_pool.op_count / oss->lsq_tgt_count;
524                         while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
525                                 next = (next + 1) % lqr->lqr_pool.op_count;
526
527                         lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
528                         j++;
529                         placed++;
530                 }
531         }
532
533         lqr->lqr_dirty = 0;
534         up_write(&lod->lod_qos.lq_rw_sem);
535
536         if (placed != real_count) {
537                 /* This should never happen */
538                 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
539                                    "round-robin list (%d of %d).\n",
540                                    placed, real_count);
541                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
542                         LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
543                                  lqr->lqr_pool.op_array[i]);
544                 }
545                 lqr->lqr_dirty = 1;
546                 RETURN(-EAGAIN);
547         }
548
549 #if 0
550         for (i = 0; i < lqr->lqr_pool.op_count; i++)
551                 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
552 #endif
553
554         RETURN(0);
555 }
556
557 /**
558  * Instantiate and declare creation of a new object.
559  *
560  * The function instantiates LU representation for a new object on the
561  * specified device. Also it declares an intention to create that
562  * object on the storage target.
563  *
564  * Note lu_object_anon() is used which is a trick with regard to LU/OSD
565  * infrastructure - in the existing precreation framework we can't assign FID
566  * at this moment, we do this later once a transaction is started. So the
567  * special method instantiates FID-less object in the cache and later it
568  * will get a FID and proper placement in LU cache.
569  *
570  * \param[in] env       execution environment for this thread
571  * \param[in] d         LOD device
572  * \param[in] ost_idx   OST target index where the object is being created
573  * \param[in] th        transaction handle
574  *
575  * \retval              object ptr on success, ERR_PTR() otherwise
576  */
577 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
578                                                    struct lod_device *d,
579                                                    __u32 ost_idx,
580                                                    struct thandle *th)
581 {
582         struct lod_tgt_desc *ost;
583         struct lu_object *o, *n;
584         struct lu_device *nd;
585         struct dt_object *dt;
586         int               rc;
587         ENTRY;
588
589         LASSERT(d);
590         LASSERT(ost_idx < d->lod_osts_size);
591         ost = OST_TGT(d,ost_idx);
592         LASSERT(ost);
593         LASSERT(ost->ltd_ost);
594
595         nd = &ost->ltd_ost->dd_lu_dev;
596
597         /*
598          * allocate anonymous object with zero fid, real fid
599          * will be assigned by OSP within transaction
600          * XXX: to be fixed with fully-functional OST fids
601          */
602         o = lu_object_anon(env, nd, NULL);
603         if (IS_ERR(o))
604                 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
605
606         n = lu_object_locate(o->lo_header, nd->ld_type);
607         if (unlikely(n == NULL)) {
608                 CERROR("can't find slice\n");
609                 lu_object_put(env, o);
610                 GOTO(out, dt = ERR_PTR(-EINVAL));
611         }
612
613         dt = container_of(n, struct dt_object, do_lu);
614
615         rc = lod_sub_declare_create(env, dt, NULL, NULL, NULL, th);
616         if (rc < 0) {
617                 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
618                        ost_idx, rc);
619                 lu_object_put(env, o);
620                 dt = ERR_PTR(rc);
621         }
622
623 out:
624         RETURN(dt);
625 }
626
627 /**
628  * Calculate a minimum acceptable stripe count.
629  *
630  * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
631  * all stripes or 3/4 of stripes.
632  *
633  * \param[in] stripe_count      number of stripes requested
634  * \param[in] flags             0 or LOV_USES_DEFAULT_STRIPE
635  *
636  * \retval                      acceptable stripecount
637  */
638 static int min_stripe_count(__u32 stripe_count, int flags)
639 {
640         return (flags & LOV_USES_DEFAULT_STRIPE ?
641                 stripe_count - (stripe_count / 4) : stripe_count);
642 }
643
644 #define LOV_CREATE_RESEED_MULT 30
645 #define LOV_CREATE_RESEED_MIN  2000
646
647 /**
648  * Initialize temporary OST-in-use array.
649  *
650  * Allocate or extend the array used to mark targets already assigned to a new
651  * striping so they are not used more than once.
652  *
653  * \param[in] env       execution environment for this thread
654  * \param[in] stripes   number of items needed in the array
655  *
656  * \retval 0            on success
657  * \retval -ENOMEM      on error
658  */
659 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
660                                            __u32 stripes)
661 {
662         struct lod_thread_info *info = lod_env_info(env);
663
664         if (info->lti_ea_store_size < sizeof(int) * stripes)
665                 lod_ea_store_resize(info, stripes * sizeof(int));
666         if (info->lti_ea_store_size < sizeof(int) * stripes) {
667                 CERROR("can't allocate memory for ost-in-use array\n");
668                 return -ENOMEM;
669         }
670         memset(info->lti_ea_store, -1, sizeof(int) * stripes);
671         return 0;
672 }
673
674 /**
675  * Remember a target in the array of used targets.
676  *
677  * Mark the given target as used for a new striping being created. The status
678  * of an OST in a striping can be checked with lod_qos_is_ost_used().
679  *
680  * \param[in] env       execution environment for this thread
681  * \param[in] idx       index in the array
682  * \param[in] ost       OST target index to mark as used
683  */
684 static inline void lod_qos_ost_in_use(const struct lu_env *env,
685                                       int idx, int ost)
686 {
687         struct lod_thread_info *info = lod_env_info(env);
688         int *osts = info->lti_ea_store;
689
690         LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
691         osts[idx] = ost;
692 }
693
694 /**
695  * Check is OST used in a striping.
696  *
697  * Checks whether OST with the given index is marked as used in the temporary
698  * array (see lod_qos_ost_in_use()).
699  *
700  * \param[in] env       execution environment for this thread
701  * \param[in] ost       OST target index to check
702  * \param[in] stripes   the number of items used in the array already
703  *
704  * \retval 0            not used
705  * \retval 1            used
706  */
707 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
708 {
709         struct lod_thread_info *info = lod_env_info(env);
710         int *osts = info->lti_ea_store;
711         __u32 j;
712
713         for (j = 0; j < stripes; j++) {
714                 if (osts[j] == ost)
715                         return 1;
716         }
717         return 0;
718 }
719
720 static inline bool
721 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
722                            int comp_idx, struct lod_obj_stripe_cb_data *data)
723 {
724         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
725
726         return comp->llc_ost_indices == NULL;
727 }
728
729 static inline int
730 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
731                       int comp_idx, struct lod_obj_stripe_cb_data *data)
732 {
733         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
734         int i;
735
736         for (i = 0; i < comp->llc_stripe_count; i++) {
737                 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
738                         data->locd_ost_index = -1;
739                         return -EEXIST;
740                 }
741         }
742
743         return 0;
744 }
745
746 /**
747  * Check is OST used in a composite layout
748  *
749  * \param[in] lo        lod object
750  * \param[in] ost       OST target index to check
751  *
752  * \retval false        not used
753  * \retval true         used
754  */
755 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
756                                        struct lod_object *lo, int ost)
757 {
758         struct lod_obj_stripe_cb_data data = { { 0 } };
759
760         data.locd_ost_index = ost;
761         data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
762         data.locd_comp_cb = lod_obj_is_ost_use_cb;
763
764         (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
765
766         return data.locd_ost_index == -1;
767 }
768
769 static inline void lod_avoid_update(struct lod_object *lo,
770                                     struct lod_avoid_guide *lag)
771 {
772         if (!lod_is_flr(lo))
773                 return;
774
775         lag->lag_ost_avail--;
776 }
777
778 static inline bool lod_should_avoid_ost(struct lod_object *lo,
779                                         struct lod_avoid_guide *lag,
780                                         __u32 index)
781 {
782         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
783         struct lod_tgt_desc *ost = OST_TGT(lod, index);
784         struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
785         bool used = false;
786         int i;
787
788         if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
789                 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
790                           index);
791                 return true;
792         }
793
794         /**
795          * we've tried our best, all available OSTs have been used in
796          * overlapped components in the other mirror
797          */
798         if (lag->lag_ost_avail == 0)
799                 return false;
800
801         /* check OSS use */
802         for (i = 0; i < lag->lag_oaa_count; i++) {
803                 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
804                         used = true;
805                         break;
806                 }
807         }
808         /**
809          * if the OSS which OST[index] resides has not been used, we'd like to
810          * use it
811          */
812         if (!used)
813                 return false;
814
815         /* if the OSS has been used, check whether the OST has been used */
816         if (!cfs_bitmap_check(lag->lag_ost_avoid_bitmap, index))
817                 used = false;
818         else
819                 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
820                           index);
821         return used;
822 }
823
824 static int lod_check_and_reserve_ost(const struct lu_env *env,
825                                      struct lod_object *lo,
826                                      struct lod_layout_component *lod_comp,
827                                      struct obd_statfs *sfs, __u32 ost_idx,
828                                      __u32 speed, __u32 *s_idx,
829                                      struct dt_object **stripe,
830                                      __u32 *ost_indices,
831                                      struct thandle *th,
832                                      bool *overstriped)
833 {
834         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
835         struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
836         struct dt_object   *o;
837         __u32 stripe_idx = *s_idx;
838         int rc;
839         ENTRY;
840
841         rc = lod_statfs_and_check(env, lod, ost_idx, sfs, NULL);
842         if (rc)
843                 RETURN(rc);
844
845         /*
846          * We expect number of precreated objects in f_ffree at
847          * the first iteration, skip OSPs with no objects ready
848          */
849         if (sfs->os_fprecreated == 0 && speed == 0) {
850                 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
851                 RETURN(rc);
852         }
853
854         /*
855          * try to use another OSP if this one is degraded
856          */
857         if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
858                 QOS_DEBUG("#%d: degraded\n", ost_idx);
859                 RETURN(rc);
860         }
861
862         /*
863          * try not allocate on OST which has been used by other
864          * component
865          */
866         if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
867                 QOS_DEBUG("iter %d: OST%d used by other component\n",
868                           speed, ost_idx);
869                 RETURN(rc);
870         }
871
872         /**
873          * try not allocate OSTs used by conflicting component of other mirrors
874          * for the first and second time.
875          */
876         if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
877                 QOS_DEBUG("iter %d: OST%d used by conflicting mirror "
878                           "component\n", speed, ost_idx);
879                 RETURN(rc);
880         }
881
882         /* do not put >1 objects on a single OST, except for overstriping */
883         if (lod_qos_is_ost_used(env, ost_idx, stripe_idx)) {
884                 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
885                         *overstriped = true;
886                 else
887                         RETURN(rc);
888         }
889
890         o = lod_qos_declare_object_on(env, lod, ost_idx, th);
891         if (IS_ERR(o)) {
892                 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
893                        ost_idx, (int) PTR_ERR(o));
894                 rc = PTR_ERR(o);
895                 RETURN(rc);
896         }
897
898         /*
899          * We've successfully declared (reserved) an object
900          */
901         lod_avoid_update(lo, lag);
902         lod_qos_ost_in_use(env, stripe_idx, ost_idx);
903         stripe[stripe_idx] = o;
904         ost_indices[stripe_idx] = ost_idx;
905         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
906         stripe_idx++;
907         *s_idx = stripe_idx;
908
909         RETURN(rc);
910 }
911
912 /**
913  * Allocate a striping using round-robin algorithm.
914  *
915  * Allocates a new striping using round-robin algorithm. The function refreshes
916  * all the internal structures (statfs cache, array of available OSTs sorted
917  * with regard to OSS, etc). The number of stripes required is taken from the
918  * object (must be prepared by the caller), but can change if the flag
919  * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
920  * is trying to create a striping on the object in parallel. All the internal
921  * structures (like pools, etc) are protected and no additional locking is
922  * required. The function succeeds even if a single stripe is allocated. To save
923  * time we give priority to targets which already have objects precreated.
924  * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
925  *
926  * \param[in] env               execution environment for this thread
927  * \param[in] lo                LOD object
928  * \param[out] stripe           striping created
929  * \param[out] ost_indices      ost indices of striping created
930  * \param[in] flags             allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
931  * \param[in] th                transaction handle
932  * \param[in] comp_idx          index of ldo_comp_entries
933  *
934  * \retval 0            on success
935  * \retval -ENOSPC      if not enough OSTs are found
936  * \retval negative     negated errno for other failures
937  */
938 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
939                         struct dt_object **stripe, __u32 *ost_indices,
940                         int flags, struct thandle *th, int comp_idx)
941 {
942         struct lod_layout_component *lod_comp;
943         struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
944         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
945         struct pool_desc  *pool = NULL;
946         struct ost_pool   *osts;
947         struct lu_qos_rr *lqr;
948         unsigned int    i, array_idx;
949         __u32 ost_start_idx_temp;
950         __u32 stripe_idx = 0;
951         __u32 stripe_count, stripe_count_min, ost_idx;
952         int rc, speed = 0, ost_connecting = 0;
953         int stripes_per_ost = 1;
954         bool overstriped = false;
955         ENTRY;
956
957         LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
958         lod_comp = &lo->ldo_comp_entries[comp_idx];
959         stripe_count = lod_comp->llc_stripe_count;
960         stripe_count_min = min_stripe_count(stripe_count, flags);
961
962         if (lod_comp->llc_pool != NULL)
963                 pool = lod_find_pool(m, lod_comp->llc_pool);
964
965         if (pool != NULL) {
966                 down_read(&pool_tgt_rw_sem(pool));
967                 osts = &(pool->pool_obds);
968                 lqr = &(pool->pool_rr);
969         } else {
970                 osts = &(m->lod_pool_info);
971                 lqr = &(m->lod_qos.lq_rr);
972         }
973
974         rc = lod_qos_calc_rr(m, osts, lqr);
975         if (rc)
976                 GOTO(out, rc);
977
978         rc = lod_qos_ost_in_use_clear(env, stripe_count);
979         if (rc)
980                 GOTO(out, rc);
981
982         down_read(&m->lod_qos.lq_rw_sem);
983         spin_lock(&lqr->lqr_alloc);
984         if (--lqr->lqr_start_count <= 0) {
985                 lqr->lqr_start_idx = prandom_u32_max(osts->op_count);
986                 lqr->lqr_start_count =
987                         (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
988                          LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
989         } else if (stripe_count_min >= osts->op_count ||
990                         lqr->lqr_start_idx > osts->op_count) {
991                 /* If we have allocated from all of the OSTs, slowly
992                  * precess the next start if the OST/stripe count isn't
993                  * already doing this for us. */
994                 lqr->lqr_start_idx %= osts->op_count;
995                 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
996                         ++lqr->lqr_offset_idx;
997         }
998         ost_start_idx_temp = lqr->lqr_start_idx;
999
1000 repeat_find:
1001
1002         QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
1003                   "active %d count %d\n",
1004                   lod_comp->llc_pool ? lod_comp->llc_pool : "",
1005                   stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
1006                   lqr->lqr_offset_idx, osts->op_count, osts->op_count);
1007
1008         if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1009                 stripes_per_ost =
1010                         (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1011
1012         for (i = 0; i < osts->op_count * stripes_per_ost
1013              && stripe_idx < stripe_count; i++) {
1014                 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
1015                                 osts->op_count;
1016                 ++lqr->lqr_start_idx;
1017                 ost_idx = lqr->lqr_pool.op_array[array_idx];
1018
1019                 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1020                           i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1021                           stripe_idx, array_idx, ost_idx);
1022
1023                 if ((ost_idx == LOV_QOS_EMPTY) ||
1024                     !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1025                         continue;
1026
1027                 /* Fail Check before osc_precreate() is called
1028                    so we can only 'fail' single OSC. */
1029                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1030                         continue;
1031
1032                 spin_unlock(&lqr->lqr_alloc);
1033                 rc = lod_check_and_reserve_ost(env, lo, lod_comp, sfs, ost_idx,
1034                                                speed, &stripe_idx, stripe,
1035                                                ost_indices, th, &overstriped);
1036                 spin_lock(&lqr->lqr_alloc);
1037
1038                 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
1039                         ost_connecting = 1;
1040         }
1041         if ((speed < 2) && (stripe_idx < stripe_count_min)) {
1042                 /* Try again, allowing slower OSCs */
1043                 speed++;
1044                 lqr->lqr_start_idx = ost_start_idx_temp;
1045
1046                 ost_connecting = 0;
1047                 goto repeat_find;
1048         }
1049
1050         spin_unlock(&lqr->lqr_alloc);
1051         up_read(&m->lod_qos.lq_rw_sem);
1052
1053         /* If there are enough OSTs, a component with overstriping requested
1054          * will not actually end up overstriped.  The comp should reflect this.
1055          */
1056         if (!overstriped)
1057                 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1058
1059         if (stripe_idx) {
1060                 lod_comp->llc_stripe_count = stripe_idx;
1061                 /* at least one stripe is allocated */
1062                 rc = 0;
1063         } else {
1064                 /* nobody provided us with a single object */
1065                 if (ost_connecting)
1066                         rc = -EINPROGRESS;
1067                 else
1068                         rc = -ENOSPC;
1069         }
1070
1071 out:
1072         if (pool != NULL) {
1073                 up_read(&pool_tgt_rw_sem(pool));
1074                 /* put back ref got by lod_find_pool() */
1075                 lod_pool_putref(pool);
1076         }
1077
1078         RETURN(rc);
1079 }
1080
1081 /**
1082  * Allocate a specific striping layout on a user defined set of OSTs.
1083  *
1084  * Allocates new striping using the OST index range provided by the data from
1085  * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1086  * OSTs are not considered. The exact order of OSTs requested by the user
1087  * is respected as much as possible depending on OST status. The number of
1088  * stripes needed and stripe offset are taken from the object. If that number
1089  * can not be met, then the function returns a failure and then it's the
1090  * caller's responsibility to release the stripes allocated. All the internal
1091  * structures are protected, but no concurrent allocation is allowed on the
1092  * same objects.
1093  *
1094  * \param[in] env               execution environment for this thread
1095  * \param[in] lo                LOD object
1096  * \param[out] stripe           striping created
1097  * \param[out] ost_indices      ost indices of striping created
1098  * \param[in] th                transaction handle
1099  * \param[in] comp_idx          index of ldo_comp_entries
1100  *
1101  * \retval 0            on success
1102  * \retval -ENODEV      OST index does not exist on file system
1103  * \retval -EINVAL      requested OST index is invalid
1104  * \retval negative     negated errno on error
1105  */
1106 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1107                               struct dt_object **stripe, __u32 *ost_indices,
1108                               struct thandle *th, int comp_idx)
1109 {
1110         struct lod_layout_component *lod_comp;
1111         struct lod_device       *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1112         struct obd_statfs       *sfs = &lod_env_info(env)->lti_osfs;
1113         struct dt_object        *o;
1114         unsigned int            array_idx = 0;
1115         int                     stripe_count = 0;
1116         int                     i;
1117         int                     rc = -EINVAL;
1118         ENTRY;
1119
1120         /* for specific OSTs layout */
1121         LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1122         lod_comp = &lo->ldo_comp_entries[comp_idx];
1123         LASSERT(lod_comp->llc_ostlist.op_array);
1124         LASSERT(lod_comp->llc_ostlist.op_count);
1125
1126         rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1127         if (rc < 0)
1128                 RETURN(rc);
1129
1130         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1131                 lod_comp->llc_stripe_offset =
1132                                 lod_comp->llc_ostlist.op_array[0];
1133
1134         for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1135                 if (lod_comp->llc_ostlist.op_array[i] ==
1136                     lod_comp->llc_stripe_offset) {
1137                         array_idx = i;
1138                         break;
1139                 }
1140         }
1141         if (i == lod_comp->llc_stripe_count) {
1142                 CDEBUG(D_OTHER,
1143                        "%s: start index %d not in the specified list of OSTs\n",
1144                        lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1145                 RETURN(-EINVAL);
1146         }
1147
1148         for (i = 0; i < lod_comp->llc_stripe_count;
1149              i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1150                 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1151
1152                 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1153                         rc = -ENODEV;
1154                         break;
1155                 }
1156
1157                 /* do not put >1 objects on a single OST, except for
1158                  * overstriping
1159                  */
1160                 if (lod_qos_is_ost_used(env, ost_idx, stripe_count) &&
1161                     !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1162                         rc = -EINVAL;
1163                         break;
1164                 }
1165
1166                 rc = lod_statfs_and_check(env, m, ost_idx, sfs, NULL);
1167                 if (rc < 0) /* this OSP doesn't feel well */
1168                         break;
1169
1170                 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1171                 if (IS_ERR(o)) {
1172                         rc = PTR_ERR(o);
1173                         CDEBUG(D_OTHER,
1174                                "%s: can't declare new object on #%u: %d\n",
1175                                lod2obd(m)->obd_name, ost_idx, rc);
1176                         break;
1177                 }
1178
1179                 /*
1180                  * We've successfully declared (reserved) an object
1181                  */
1182                 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1183                 stripe[stripe_count] = o;
1184                 ost_indices[stripe_count] = ost_idx;
1185                 stripe_count++;
1186         }
1187
1188         RETURN(rc);
1189 }
1190
1191 /**
1192  * Allocate a striping on a predefined set of OSTs.
1193  *
1194  * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1195  * Full OSTs are not considered. The exact order of OSTs is not important and
1196  * varies depending on OST status. The allocation procedure prefers the targets
1197  * with precreated objects ready. The number of stripes needed and stripe
1198  * offset are taken from the object. If that number cannot be met, then the
1199  * function returns an error and then it's the caller's responsibility to
1200  * release the stripes allocated. All the internal structures are protected,
1201  * but no concurrent allocation is allowed on the same objects.
1202  *
1203  * \param[in] env               execution environment for this thread
1204  * \param[in] lo                LOD object
1205  * \param[out] stripe           striping created
1206  * \param[out] ost_indices      ost indices of striping created
1207  * \param[in] flags             not used
1208  * \param[in] th                transaction handle
1209  * \param[in] comp_idx          index of ldo_comp_entries
1210  *
1211  * \retval 0            on success
1212  * \retval -ENOSPC      if no OST objects are available at all
1213  * \retval -EFBIG       if not enough OST objects are found
1214  * \retval -EINVAL      requested offset is invalid
1215  * \retval negative     errno on failure
1216  */
1217 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1218                               struct dt_object **stripe, __u32 *ost_indices,
1219                               int flags, struct thandle *th, int comp_idx)
1220 {
1221         struct lod_layout_component *lod_comp;
1222         struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1223         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1224         struct dt_object *o;
1225         __u32 ost_idx;
1226         unsigned int i, array_idx, ost_count;
1227         int rc, stripe_num = 0;
1228         int speed = 0;
1229         struct pool_desc  *pool = NULL;
1230         struct ost_pool   *osts;
1231         int stripes_per_ost = 1;
1232         bool overstriped = false;
1233         ENTRY;
1234
1235         LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1236         lod_comp = &lo->ldo_comp_entries[comp_idx];
1237
1238         rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1239         if (rc)
1240                 GOTO(out, rc);
1241
1242         if (lod_comp->llc_pool != NULL)
1243                 pool = lod_find_pool(m, lod_comp->llc_pool);
1244
1245         if (pool != NULL) {
1246                 down_read(&pool_tgt_rw_sem(pool));
1247                 osts = &(pool->pool_obds);
1248         } else {
1249                 osts = &(m->lod_pool_info);
1250         }
1251
1252         ost_count = osts->op_count;
1253
1254 repeat_find:
1255         /* search loi_ost_idx in ost array */
1256         array_idx = 0;
1257         for (i = 0; i < ost_count; i++) {
1258                 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1259                         array_idx = i;
1260                         break;
1261                 }
1262         }
1263         if (i == ost_count) {
1264                 CERROR("Start index %d not found in pool '%s'\n",
1265                        lod_comp->llc_stripe_offset,
1266                        lod_comp->llc_pool ? lod_comp->llc_pool : "");
1267                 GOTO(out, rc = -EINVAL);
1268         }
1269
1270         if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1271                 stripes_per_ost =
1272                         (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1273
1274         for (i = 0; i < ost_count * stripes_per_ost;
1275                         i++, array_idx = (array_idx + 1) % ost_count) {
1276                 ost_idx = osts->op_array[array_idx];
1277
1278                 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1279                         continue;
1280
1281                 /* Fail Check before osc_precreate() is called
1282                    so we can only 'fail' single OSC. */
1283                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1284                         continue;
1285
1286                 /*
1287                  * do not put >1 objects on a single OST, except for
1288                  * overstriping, where it is intended
1289                  */
1290                 if (lod_qos_is_ost_used(env, ost_idx, stripe_num)) {
1291                         if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1292                                 overstriped = true;
1293                         else
1294                                 continue;
1295                 }
1296
1297                 /*
1298                  * try not allocate on the OST used by other component
1299                  */
1300                 if (speed == 0 && i != 0 &&
1301                     lod_comp_is_ost_used(env, lo, ost_idx))
1302                         continue;
1303
1304                 /* Drop slow OSCs if we can, but not for requested start idx.
1305                  *
1306                  * This means "if OSC is slow and it is not the requested
1307                  * start OST, then it can be skipped, otherwise skip it only
1308                  * if it is inactive/recovering/out-of-space." */
1309
1310                 rc = lod_statfs_and_check(env, m, ost_idx, sfs, NULL);
1311                 if (rc) {
1312                         /* this OSP doesn't feel well */
1313                         continue;
1314                 }
1315
1316                 /*
1317                  * We expect number of precreated objects at the first
1318                  * iteration.  Skip OSPs with no objects ready.  Don't apply
1319                  * this logic to OST specified with stripe_offset.
1320                  */
1321                 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1322                         continue;
1323
1324                 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1325                 if (IS_ERR(o)) {
1326                         CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1327                                ost_idx, (int) PTR_ERR(o));
1328                         continue;
1329                 }
1330
1331                 /*
1332                  * We've successfully declared (reserved) an object
1333                  */
1334                 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1335                 stripe[stripe_num] = o;
1336                 ost_indices[stripe_num] = ost_idx;
1337                 stripe_num++;
1338
1339                 /* We have enough stripes */
1340                 if (stripe_num == lod_comp->llc_stripe_count)
1341                         GOTO(out, rc = 0);
1342         }
1343         if (speed < 2) {
1344                 /* Try again, allowing slower OSCs */
1345                 speed++;
1346                 goto repeat_find;
1347         }
1348
1349         /* If we were passed specific striping params, then a failure to
1350          * meet those requirements is an error, since we can't reallocate
1351          * that memory (it might be part of a larger array or something).
1352          */
1353         CERROR("can't lstripe objid "DFID": have %d want %u\n",
1354                PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1355                lod_comp->llc_stripe_count);
1356         rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1357
1358         /* If there are enough OSTs, a component with overstriping requessted
1359          * will not actually end up overstriped.  The comp should reflect this.
1360          */
1361         if (rc == 0 && !overstriped)
1362                 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1363
1364 out:
1365         if (pool != NULL) {
1366                 up_read(&pool_tgt_rw_sem(pool));
1367                 /* put back ref got by lod_find_pool() */
1368                 lod_pool_putref(pool);
1369         }
1370
1371         RETURN(rc);
1372 }
1373
1374 /**
1375  * Check whether QoS allocation should be used.
1376  *
1377  * A simple helper to decide when QoS allocation should be used:
1378  * if it's just a single available target or the used space is
1379  * evenly distributed among the targets at the moment, then QoS
1380  * allocation algorithm should not be used.
1381  *
1382  * \param[in] lod       LOD device
1383  *
1384  * \retval 0            should not be used
1385  * \retval 1            should be used
1386  */
1387 static inline int lod_qos_is_usable(struct lod_device *lod)
1388 {
1389 #ifdef FORCE_QOS
1390         /* to be able to debug QoS code */
1391         return 1;
1392 #endif
1393
1394         /* Detect -EAGAIN early, before expensive lock is taken. */
1395         if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1396                 return 0;
1397
1398         if (lod->lod_desc.ld_active_tgt_count < 2)
1399                 return 0;
1400
1401         return 1;
1402 }
1403
1404 /**
1405  * Allocate a striping using an algorithm with weights.
1406  *
1407  * The function allocates OST objects to create a striping. The algorithm
1408  * used is based on weights (currently only using the free space), and it's
1409  * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1410  * configuration (# of stripes, offset, pool) is taken from the object and
1411  * is prepared by the caller.
1412  *
1413  * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1414  * be met due to too few OSTs, then allocation fails. If the flag is passed
1415  * fewer than 3/4 of the requested number of stripes can be allocated, then
1416  * allocation fails.
1417  *
1418  * No concurrent allocation is allowed on the object and this must be ensured
1419  * by the caller. All the internal structures are protected by the function.
1420  *
1421  * The algorithm has two steps: find available OSTs and calculate their
1422  * weights, then select the OSTs with their weights used as the probability.
1423  * An OST with a higher weight is proportionately more likely to be selected
1424  * than one with a lower weight.
1425  *
1426  * \param[in] env               execution environment for this thread
1427  * \param[in] lo                LOD object
1428  * \param[out] stripe           striping created
1429  * \param[out] ost_indices      ost indices of striping created
1430  * \param[in] flags             0 or LOV_USES_DEFAULT_STRIPE
1431  * \param[in] th                transaction handle
1432  * \param[in] comp_idx          index of ldo_comp_entries
1433  *
1434  * \retval 0            on success
1435  * \retval -EAGAIN      not enough OSTs are found for specified stripe count
1436  * \retval -EINVAL      requested OST index is invalid
1437  * \retval negative     errno on failure
1438  */
1439 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1440                          struct dt_object **stripe, __u32 *ost_indices,
1441                          int flags, struct thandle *th, int comp_idx)
1442 {
1443         struct lod_layout_component *lod_comp;
1444         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1445         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1446         struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1447         struct lod_tgt_desc *ost;
1448         struct dt_object *o;
1449         __u64 total_weight = 0;
1450         struct pool_desc *pool = NULL;
1451         struct ost_pool *osts;
1452         unsigned int i;
1453         __u32 nfound, good_osts, stripe_count, stripe_count_min;
1454         bool overstriped = false;
1455         int stripes_per_ost = 1;
1456         int rc = 0;
1457         ENTRY;
1458
1459         LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1460         lod_comp = &lo->ldo_comp_entries[comp_idx];
1461         stripe_count = lod_comp->llc_stripe_count;
1462         stripe_count_min = min_stripe_count(stripe_count, flags);
1463         if (stripe_count_min < 1)
1464                 RETURN(-EINVAL);
1465
1466         if (lod_comp->llc_pool != NULL)
1467                 pool = lod_find_pool(lod, lod_comp->llc_pool);
1468
1469         if (pool != NULL) {
1470                 down_read(&pool_tgt_rw_sem(pool));
1471                 osts = &(pool->pool_obds);
1472         } else {
1473                 osts = &(lod->lod_pool_info);
1474         }
1475
1476         /* Detect -EAGAIN early, before expensive lock is taken. */
1477         if (!lod_qos_is_usable(lod))
1478                 GOTO(out_nolock, rc = -EAGAIN);
1479
1480         if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1481                 stripes_per_ost =
1482                         (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1483
1484         /* Do actual allocation, use write lock here. */
1485         down_write(&lod->lod_qos.lq_rw_sem);
1486
1487         /*
1488          * Check again, while we were sleeping on @lq_rw_sem things could
1489          * change.
1490          */
1491         if (!lod_qos_is_usable(lod))
1492                 GOTO(out, rc = -EAGAIN);
1493
1494         rc = lod_qos_calc_ppo(lod);
1495         if (rc)
1496                 GOTO(out, rc);
1497
1498         rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1499         if (rc)
1500                 GOTO(out, rc);
1501
1502         good_osts = 0;
1503         /* Find all the OSTs that are valid stripe candidates */
1504         for (i = 0; i < osts->op_count; i++) {
1505                 if (!cfs_bitmap_check(lod->lod_ost_bitmap, osts->op_array[i]))
1506                         continue;
1507
1508                 ost = OST_TGT(lod, osts->op_array[i]);
1509                 ost->ltd_qos.ltq_usable = 0;
1510
1511                 rc = lod_statfs_and_check(env, lod, osts->op_array[i],
1512                                           sfs, NULL);
1513                 if (rc) {
1514                         /* this OSP doesn't feel well */
1515                         continue;
1516                 }
1517
1518                 if (sfs->os_state & OS_STATE_DEGRADED)
1519                         continue;
1520
1521                 /* Fail Check before osc_precreate() is called
1522                    so we can only 'fail' single OSC. */
1523                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1524                                    osts->op_array[i] == 0)
1525                         continue;
1526
1527                 ost->ltd_qos.ltq_usable = 1;
1528                 lod_qos_calc_weight(lod, osts->op_array[i]);
1529                 total_weight += ost->ltd_qos.ltq_weight;
1530
1531                 good_osts++;
1532         }
1533
1534         QOS_DEBUG("found %d good osts\n", good_osts);
1535
1536         if (good_osts < stripe_count_min)
1537                 GOTO(out, rc = -EAGAIN);
1538
1539         /* If we do not have enough OSTs for the requested stripe count, do not
1540          * put more stripes per OST than requested.
1541          */
1542         if (stripe_count / stripes_per_ost > good_osts)
1543                 stripe_count = good_osts * stripes_per_ost;
1544
1545         /* Find enough OSTs with weighted random allocation. */
1546         nfound = 0;
1547         while (nfound < stripe_count) {
1548                 u64 rand, cur_weight;
1549
1550                 cur_weight = 0;
1551                 rc = -ENOSPC;
1552
1553                 rand = lu_prandom_u64_max(total_weight);
1554
1555                 /* On average, this will hit larger-weighted OSTs more often.
1556                  * 0-weight OSTs will always get used last (only when rand=0) */
1557                 for (i = 0; i < osts->op_count; i++) {
1558                         __u32 idx = osts->op_array[i];
1559
1560                         if (lod_should_avoid_ost(lo, lag, idx))
1561                                 continue;
1562
1563                         ost = OST_TGT(lod, idx);
1564
1565                         if (!ost->ltd_qos.ltq_usable)
1566                                 continue;
1567
1568                         cur_weight += ost->ltd_qos.ltq_weight;
1569                         QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1570                                   "rand=%llu total_weight=%llu\n",
1571                                   stripe_count, nfound, cur_weight, rand,
1572                                   total_weight);
1573
1574                         if (cur_weight < rand)
1575                                 continue;
1576
1577                         QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1578                         /*
1579                          * do not put >1 objects on a single OST, except for
1580                          * overstriping
1581                          */
1582                         if ((lod_comp_is_ost_used(env, lo, idx)) &&
1583                             !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1584                                 continue;
1585
1586                         if (lod_qos_is_ost_used(env, idx, nfound)) {
1587                                 if (lod_comp->llc_pattern &
1588                                     LOV_PATTERN_OVERSTRIPING)
1589                                         overstriped = true;
1590                                 else
1591                                         continue;
1592                         }
1593
1594                         o = lod_qos_declare_object_on(env, lod, idx, th);
1595                         if (IS_ERR(o)) {
1596                                 QOS_DEBUG("can't declare object on #%u: %d\n",
1597                                           idx, (int) PTR_ERR(o));
1598                                 continue;
1599                         }
1600
1601                         lod_avoid_update(lo, lag);
1602                         lod_qos_ost_in_use(env, nfound, idx);
1603                         stripe[nfound] = o;
1604                         ost_indices[nfound] = idx;
1605                         lod_qos_used(lod, osts, idx, &total_weight);
1606                         nfound++;
1607                         rc = 0;
1608                         break;
1609                 }
1610
1611                 if (rc) {
1612                         /* no OST found on this iteration, give up */
1613                         break;
1614                 }
1615         }
1616
1617         if (unlikely(nfound != stripe_count)) {
1618                 /*
1619                  * when the decision to use weighted algorithm was made
1620                  * we had enough appropriate OSPs, but this state can
1621                  * change anytime (no space on OST, broken connection, etc)
1622                  * so it's possible OSP won't be able to provide us with
1623                  * an object due to just changed state
1624                  */
1625                 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1626                           lod2obd(lod)->obd_name, stripe_count, nfound);
1627                 for (i = 0; i < nfound; i++) {
1628                         LASSERT(stripe[i] != NULL);
1629                         dt_object_put(env, stripe[i]);
1630                         stripe[i] = NULL;
1631                 }
1632
1633                 /* makes sense to rebalance next time */
1634                 lod->lod_qos.lq_dirty = 1;
1635                 lod->lod_qos.lq_same_space = 0;
1636
1637                 rc = -EAGAIN;
1638         }
1639
1640         /* If there are enough OSTs, a component with overstriping requessted
1641          * will not actually end up overstriped.  The comp should reflect this.
1642          */
1643         if (rc == 0 && !overstriped)
1644                 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1645
1646 out:
1647         up_write(&lod->lod_qos.lq_rw_sem);
1648
1649 out_nolock:
1650         if (pool != NULL) {
1651                 up_read(&pool_tgt_rw_sem(pool));
1652                 /* put back ref got by lod_find_pool() */
1653                 lod_pool_putref(pool);
1654         }
1655
1656         RETURN(rc);
1657 }
1658
1659 /**
1660  * Check stripe count the caller can use.
1661  *
1662  * For new layouts (no initialized components), check the total size of the
1663  * layout against the maximum EA size from the backing file system.  This
1664  * stops us from creating a layout which will be too large once initialized.
1665  *
1666  * For existing layouts (with initialized components):
1667  * Find the maximal possible stripe count not greater than \a stripe_count.
1668  * If the provided stripe count is 0, then the filesystem's default is used.
1669  *
1670  * \param[in] lod       LOD device
1671  * \param[in] lo        The lod_object
1672  * \param[in] stripe_count      count the caller would like to use
1673  *
1674  * \retval              the maximum usable stripe count
1675  */
1676 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1677                            __u16 stripe_count, bool overstriping)
1678 {
1679         __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1680         /* max stripe count is based on OSD ea size */
1681         unsigned int easize = lod->lod_osd_max_easize;
1682         int i;
1683
1684
1685         if (!stripe_count)
1686                 stripe_count = lod->lod_desc.ld_default_stripe_count;
1687         if (!stripe_count)
1688                 stripe_count = 1;
1689         /* Overstriping allows more stripes than targets */
1690         if (stripe_count > lod->lod_desc.ld_active_tgt_count && !overstriping)
1691                 stripe_count = lod->lod_desc.ld_active_tgt_count;
1692
1693         if (lo->ldo_is_composite) {
1694                 struct lod_layout_component *lod_comp;
1695                 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1696                 unsigned int init_comp_sz = 0;
1697                 unsigned int total_comp_sz = 0;
1698                 unsigned int comp_sz;
1699
1700                 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1701                                 lo->ldo_comp_cnt;
1702
1703                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1704                         lod_comp = &lo->ldo_comp_entries[i];
1705                         comp_sz = lov_mds_md_size(lod_comp->llc_stripe_count,
1706                                                   LOV_MAGIC_V3);
1707                         total_comp_sz += comp_sz;
1708                         if (lod_comp->llc_flags & LCME_FL_INIT)
1709                                 init_comp_sz += comp_sz;
1710                 }
1711
1712                 if (init_comp_sz > 0)
1713                         total_comp_sz = init_comp_sz;
1714
1715                 header_sz += total_comp_sz;
1716
1717                 if (easize > header_sz)
1718                         easize -= header_sz;
1719                 else
1720                         easize = 0;
1721         }
1722
1723         max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1724
1725         return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1726 }
1727
1728 /**
1729  * Create in-core respresentation for a fully-defined striping
1730  *
1731  * When the caller passes a fully-defined striping (i.e. everything including
1732  * OST object FIDs are defined), then we still need to instantiate LU-cache
1733  * with the objects representing the stripes defined. This function completes
1734  * that task.
1735  *
1736  * \param[in] env       execution environment for this thread
1737  * \param[in] mo        LOD object
1738  * \param[in] buf       buffer containing the striping
1739  *
1740  * \retval 0            on success
1741  * \retval negative     negated errno on error
1742  */
1743 int lod_use_defined_striping(const struct lu_env *env,
1744                              struct lod_object *mo,
1745                              const struct lu_buf *buf)
1746 {
1747         struct lod_layout_component *lod_comp;
1748         struct lov_mds_md_v1   *v1 = buf->lb_buf;
1749         struct lov_mds_md_v3   *v3 = buf->lb_buf;
1750         struct lov_comp_md_v1  *comp_v1 = NULL;
1751         struct lov_ost_data_v1 *objs;
1752         __u32   magic;
1753         __u16   comp_cnt;
1754         __u16   mirror_cnt;
1755         int     rc = 0, i;
1756         ENTRY;
1757
1758         mutex_lock(&mo->ldo_layout_mutex);
1759         lod_striping_free_nolock(env, mo);
1760
1761         magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1762
1763         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1764             magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
1765                 GOTO(unlock, rc = -EINVAL);
1766
1767         if (magic == LOV_MAGIC_COMP_V1) {
1768                 comp_v1 = buf->lb_buf;
1769                 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1770                 if (comp_cnt == 0)
1771                         GOTO(unlock, rc = -EINVAL);
1772                 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1773                 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1774                                         LCM_FL_FLR_MASK;
1775                 mo->ldo_is_composite = 1;
1776         } else if (magic == LOV_MAGIC_FOREIGN) {
1777                 struct lov_foreign_md *foreign;
1778                 size_t length;
1779
1780                 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
1781                         CDEBUG(D_LAYOUT,
1782                                "buf len %zu < min lov_foreign_md size (%zu)\n",
1783                                buf->lb_len,
1784                                offsetof(typeof(*foreign), lfm_value));
1785                         GOTO(out, rc = -EINVAL);
1786                 }
1787                 foreign = (struct lov_foreign_md *)buf->lb_buf;
1788                 length = foreign_size_le(foreign);
1789                 if (buf->lb_len < length) {
1790                         CDEBUG(D_LAYOUT,
1791                                "buf len %zu < this lov_foreign_md size (%zu)\n",
1792                                buf->lb_len, length);
1793                         GOTO(out, rc = -EINVAL);
1794                 }
1795
1796                 /* just cache foreign LOV EA raw */
1797                 rc = lod_alloc_foreign_lov(mo, length);
1798                 if (rc)
1799                         GOTO(out, rc);
1800                 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
1801                 GOTO(out, rc);
1802         } else {
1803                 mo->ldo_is_composite = 0;
1804                 comp_cnt = 1;
1805                 mirror_cnt = 0;
1806         }
1807         mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1808
1809         rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
1810         if (rc)
1811                 GOTO(unlock, rc);
1812
1813         for (i = 0; i < comp_cnt; i++) {
1814                 struct lu_extent *ext;
1815                 char    *pool_name;
1816                 __u32   offs;
1817
1818                 lod_comp = &mo->ldo_comp_entries[i];
1819
1820                 if (mo->ldo_is_composite) {
1821                         offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1822                         v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
1823                         v3 = (struct lov_mds_md_v3 *)v1;
1824                         magic = le32_to_cpu(v1->lmm_magic);
1825
1826                         ext = &comp_v1->lcm_entries[i].lcme_extent;
1827                         lod_comp->llc_extent.e_start =
1828                                 le64_to_cpu(ext->e_start);
1829                         lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
1830                         lod_comp->llc_flags =
1831                                 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
1832                         if (lod_comp->llc_flags & LCME_FL_NOSYNC)
1833                                 lod_comp->llc_timestamp = le64_to_cpu(
1834                                         comp_v1->lcm_entries[i].lcme_timestamp);
1835                         lod_comp->llc_id =
1836                                 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
1837                         if (lod_comp->llc_id == LCME_ID_INVAL)
1838                                 GOTO(out, rc = -EINVAL);
1839                 }
1840
1841                 pool_name = NULL;
1842                 if (magic == LOV_MAGIC_V1) {
1843                         objs = &v1->lmm_objects[0];
1844                 } else if (magic == LOV_MAGIC_V3) {
1845                         objs = &v3->lmm_objects[0];
1846                         if (v3->lmm_pool_name[0] != '\0')
1847                                 pool_name = v3->lmm_pool_name;
1848                 } else {
1849                         CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
1850                         GOTO(out, rc = -EINVAL);
1851                 }
1852
1853                 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
1854                 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1855                 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
1856                 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1857                 /**
1858                  * The stripe_offset of an uninit-ed component is stored in
1859                  * the lmm_layout_gen
1860                  */
1861                 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
1862                         lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
1863                 lod_obj_set_pool(mo, i, pool_name);
1864
1865                 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
1866                     !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
1867                     !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
1868                         rc = lod_initialize_objects(env, mo, objs, i);
1869                         if (rc)
1870                                 GOTO(out, rc);
1871                 }
1872         }
1873
1874         rc = lod_fill_mirrors(mo);
1875         GOTO(out, rc);
1876 out:
1877         if (rc)
1878                 lod_striping_free_nolock(env, mo);
1879 unlock:
1880         mutex_unlock(&mo->ldo_layout_mutex);
1881
1882         RETURN(rc);
1883 }
1884
1885 /**
1886  * Parse suggested striping configuration.
1887  *
1888  * The caller gets a suggested striping configuration from a number of sources
1889  * including per-directory default and applications. Then it needs to verify
1890  * the suggested striping is valid, apply missing bits and store the resulting
1891  * configuration in the object to be used by the allocator later. Must not be
1892  * called concurrently against the same object. It's OK to provide a
1893  * fully-defined striping.
1894  *
1895  * \param[in] env       execution environment for this thread
1896  * \param[in] lo        LOD object
1897  * \param[in] buf       buffer containing the striping
1898  *
1899  * \retval 0            on success
1900  * \retval negative     negated errno on error
1901  */
1902 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
1903                          const struct lu_buf *buf)
1904 {
1905         struct lod_layout_component *lod_comp;
1906         struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1907         struct lov_desc *desc = &d->lod_desc;
1908         struct lov_user_md_v1 *v1 = NULL;
1909         struct lov_user_md_v3 *v3 = NULL;
1910         struct lov_comp_md_v1 *comp_v1 = NULL;
1911         struct lov_foreign_md *lfm = NULL;
1912         char def_pool[LOV_MAXPOOLNAME + 1];
1913         __u32 magic;
1914         __u16 comp_cnt;
1915         __u16 mirror_cnt;
1916         int i, rc;
1917         ENTRY;
1918
1919         if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1920                 RETURN(0);
1921
1922         memset(def_pool, 0, sizeof(def_pool));
1923         if (lo->ldo_comp_entries != NULL)
1924                 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
1925                                     def_pool, sizeof(def_pool));
1926
1927         /* free default striping info */
1928         if (lo->ldo_is_foreign)
1929                 lod_free_foreign_lov(lo);
1930         else
1931                 lod_free_comp_entries(lo);
1932
1933         rc = lod_verify_striping(d, lo, buf, false);
1934         if (rc)
1935                 RETURN(-EINVAL);
1936
1937         v3 = buf->lb_buf;
1938         v1 = buf->lb_buf;
1939         comp_v1 = buf->lb_buf;
1940         /* {lmm,lfm}_magic position/length work for all LOV formats */
1941         magic = v1->lmm_magic;
1942
1943         if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
1944                 /* try to use as fully defined striping */
1945                 rc = lod_use_defined_striping(env, lo, buf);
1946                 RETURN(rc);
1947         }
1948
1949         switch (magic) {
1950         case __swab32(LOV_USER_MAGIC_V1):
1951                 lustre_swab_lov_user_md_v1(v1);
1952                 magic = v1->lmm_magic;
1953                 /* fall through */
1954         case LOV_USER_MAGIC_V1:
1955                 break;
1956         case __swab32(LOV_USER_MAGIC_V3):
1957                 lustre_swab_lov_user_md_v3(v3);
1958                 magic = v3->lmm_magic;
1959                 /* fall through */
1960         case LOV_USER_MAGIC_V3:
1961                 break;
1962         case __swab32(LOV_USER_MAGIC_SPECIFIC):
1963                 lustre_swab_lov_user_md_v3(v3);
1964                 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1965                                                 v3->lmm_stripe_count);
1966                 magic = v3->lmm_magic;
1967                 /* fall through */
1968         case LOV_USER_MAGIC_SPECIFIC:
1969                 break;
1970         case __swab32(LOV_USER_MAGIC_COMP_V1):
1971                 lustre_swab_lov_comp_md_v1(comp_v1);
1972                 magic = comp_v1->lcm_magic;
1973                 /* fall trhough */
1974         case LOV_USER_MAGIC_COMP_V1:
1975                 break;
1976         case __swab32(LOV_USER_MAGIC_FOREIGN):
1977                 lfm = buf->lb_buf;
1978                 __swab32s(&lfm->lfm_magic);
1979                 __swab32s(&lfm->lfm_length);
1980                 __swab32s(&lfm->lfm_type);
1981                 __swab32s(&lfm->lfm_flags);
1982                 magic = lfm->lfm_magic;
1983                 /* fall through */
1984         case LOV_USER_MAGIC_FOREIGN:
1985                 if (!lfm)
1986                         lfm = buf->lb_buf;
1987                 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
1988                 if (rc)
1989                         RETURN(rc);
1990                 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
1991                 RETURN(0);
1992         default:
1993                 CERROR("%s: unrecognized magic %X\n",
1994                        lod2obd(d)->obd_name, magic);
1995                 RETURN(-EINVAL);
1996         }
1997
1998         lustre_print_user_md(D_OTHER, v1, "parse config");
1999
2000         if (magic == LOV_USER_MAGIC_COMP_V1) {
2001                 comp_cnt = comp_v1->lcm_entry_count;
2002                 if (comp_cnt == 0)
2003                         RETURN(-EINVAL);
2004                 mirror_cnt =  comp_v1->lcm_mirror_count + 1;
2005                 if (mirror_cnt > 1)
2006                         lo->ldo_flr_state = LCM_FL_RDONLY;
2007                 lo->ldo_is_composite = 1;
2008         } else {
2009                 comp_cnt = 1;
2010                 mirror_cnt = 0;
2011                 lo->ldo_is_composite = 0;
2012         }
2013
2014         rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2015         if (rc)
2016                 RETURN(rc);
2017
2018         LASSERT(lo->ldo_comp_entries);
2019
2020         for (i = 0; i < comp_cnt; i++) {
2021                 struct pool_desc        *pool;
2022                 struct lu_extent        *ext;
2023                 char    *pool_name;
2024
2025                 lod_comp = &lo->ldo_comp_entries[i];
2026
2027                 if (lo->ldo_is_composite) {
2028                         v1 = (struct lov_user_md *)((char *)comp_v1 +
2029                                         comp_v1->lcm_entries[i].lcme_offset);
2030                         ext = &comp_v1->lcm_entries[i].lcme_extent;
2031                         lod_comp->llc_extent = *ext;
2032                         lod_comp->llc_flags =
2033                                 comp_v1->lcm_entries[i].lcme_flags &
2034                                         LCME_CL_COMP_FLAGS;
2035                 }
2036
2037                 pool_name = NULL;
2038                 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2039                     v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2040                         v3 = (struct lov_user_md_v3 *)v1;
2041                         if (v3->lmm_pool_name[0] != '\0')
2042                                 pool_name = v3->lmm_pool_name;
2043
2044                         if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2045                                 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2046                                 if (rc)
2047                                         GOTO(free_comp, rc);
2048                         }
2049                 }
2050
2051                 if (pool_name == NULL && def_pool[0] != '\0')
2052                         pool_name = def_pool;
2053
2054                 if (v1->lmm_pattern == 0)
2055                         v1->lmm_pattern = LOV_PATTERN_RAID0;
2056                 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2057                     lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2058                     lov_pattern(v1->lmm_pattern) !=
2059                         (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2060                         CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2061                                lod2obd(d)->obd_name, v1->lmm_pattern);
2062                         GOTO(free_comp, rc = -EINVAL);
2063                 }
2064
2065                 lod_comp->llc_pattern = v1->lmm_pattern;
2066                 lod_comp->llc_stripe_size = desc->ld_default_stripe_size;
2067                 if (v1->lmm_stripe_size)
2068                         lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2069
2070                 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2071                 if (v1->lmm_stripe_count ||
2072                     lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2073                         lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2074
2075                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2076                 lod_obj_set_pool(lo, i, pool_name);
2077
2078                 LASSERT(ergo(lov_pattern(lod_comp->llc_pattern) ==
2079                              LOV_PATTERN_MDT, lod_comp->llc_stripe_count == 0));
2080
2081                 if (pool_name == NULL)
2082                         continue;
2083
2084                 /* In the function below, .hs_keycmp resolves to
2085                  * pool_hashkey_keycmp() */
2086                 /* coverity[overrun-buffer-val] */
2087                 pool = lod_find_pool(d, pool_name);
2088                 if (pool == NULL)
2089                         continue;
2090
2091                 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2092                         rc = lod_check_index_in_pool(
2093                                         lod_comp->llc_stripe_offset, pool);
2094                         if (rc < 0) {
2095                                 lod_pool_putref(pool);
2096                                 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2097                                        lod2obd(d)->obd_name,
2098                                        lod_comp->llc_stripe_offset);
2099                                 GOTO(free_comp, rc = -EINVAL);
2100                         }
2101                 }
2102
2103                 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2104                     !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2105                         lod_comp->llc_stripe_count = pool_tgt_count(pool);
2106
2107                 lod_pool_putref(pool);
2108         }
2109
2110         RETURN(0);
2111
2112 free_comp:
2113         lod_free_comp_entries(lo);
2114         RETURN(rc);
2115 }
2116
2117 /**
2118  * prepare enough OST avoidance bitmap space
2119  */
2120 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2121 {
2122         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2123         struct lod_tgt_descs *ltds = &lod->lod_ost_descs;
2124         struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2125         struct cfs_bitmap *bitmap = NULL;
2126         __u32 *new_oss = NULL;
2127
2128         lag->lag_ost_avail = ltds->ltd_tgtnr;
2129
2130         /* reset OSS avoid guide array */
2131         lag->lag_oaa_count = 0;
2132         if (lag->lag_oss_avoid_array && lag->lag_oaa_size < ltds->ltd_tgtnr) {
2133                 OBD_FREE(lag->lag_oss_avoid_array,
2134                          sizeof(__u32) * lag->lag_oaa_size);
2135                 lag->lag_oss_avoid_array = NULL;
2136                 lag->lag_oaa_size = 0;
2137         }
2138
2139         /* init OST avoid guide bitmap */
2140         if (lag->lag_ost_avoid_bitmap) {
2141                 if (ltds->ltd_tgtnr <= lag->lag_ost_avoid_bitmap->size) {
2142                         CFS_RESET_BITMAP(lag->lag_ost_avoid_bitmap);
2143                 } else {
2144                         CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
2145                         lag->lag_ost_avoid_bitmap = NULL;
2146                 }
2147         }
2148
2149         if (!lag->lag_ost_avoid_bitmap) {
2150                 bitmap = CFS_ALLOCATE_BITMAP(ltds->ltd_tgtnr);
2151                 if (!bitmap)
2152                         return -ENOMEM;
2153         }
2154
2155         if (!lag->lag_oss_avoid_array) {
2156                 /**
2157                  * usually there are multiple OSTs in one OSS, but we don't
2158                  * know the exact OSS number, so we choose a safe option,
2159                  * using OST count to allocate the array to store the OSS
2160                  * id.
2161                  */
2162                 OBD_ALLOC(new_oss, sizeof(*new_oss) * ltds->ltd_tgtnr);
2163                 if (!new_oss) {
2164                         CFS_FREE_BITMAP(bitmap);
2165                         return -ENOMEM;
2166                 }
2167         }
2168
2169         if (new_oss) {
2170                 lag->lag_oss_avoid_array = new_oss;
2171                 lag->lag_oaa_size = ltds->ltd_tgtnr;
2172         }
2173         if (bitmap)
2174                 lag->lag_ost_avoid_bitmap = bitmap;
2175
2176         return 0;
2177 }
2178
2179 /**
2180  * Collect information of used OSTs and OSSs in the overlapped components
2181  * of other mirrors
2182  */
2183 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2184                            int comp_idx)
2185 {
2186         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2187         struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2188         struct cfs_bitmap *bitmap = lag->lag_ost_avoid_bitmap;
2189         int i, j;
2190
2191         /* iterate mirrors */
2192         for (i = 0; i < lo->ldo_mirror_count; i++) {
2193                 struct lod_layout_component *comp;
2194
2195                 /**
2196                  * skip mirror containing component[comp_idx], we only
2197                  * collect OSTs info of conflicting component in other mirrors,
2198                  * so that during read, if OSTs of a mirror's component are
2199                  * not available, we still have other mirror with different
2200                  * OSTs to read the data.
2201                  */
2202                 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2203                 if (comp->llc_id != LCME_ID_INVAL &&
2204                     mirror_id_of(comp->llc_id) ==
2205                                                 mirror_id_of(lod_comp->llc_id))
2206                         continue;
2207
2208                 /* iterate components of a mirror */
2209                 lod_foreach_mirror_comp(comp, lo, i) {
2210                         /**
2211                          * skip non-overlapped or un-instantiated components,
2212                          * NOTE: don't use lod_comp_inited(comp) to judge
2213                          * whether @comp has been inited, since during
2214                          * declare phase, comp->llc_stripe has been allocated
2215                          * while it's init flag not been set until the exec
2216                          * phase.
2217                          */
2218                         if (!lu_extent_is_overlapped(&comp->llc_extent,
2219                                                      &lod_comp->llc_extent) ||
2220                             !comp->llc_stripe)
2221                                 continue;
2222
2223                         /**
2224                          * collect used OSTs index and OSS info from a
2225                          * component
2226                          */
2227                         for (j = 0; j < comp->llc_stripe_count; j++) {
2228                                 struct lod_tgt_desc *ost;
2229                                 struct lu_svr_qos *lsq;
2230                                 int k;
2231
2232                                 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2233                                 lsq = ost->ltd_qos.ltq_svr;
2234
2235                                 if (cfs_bitmap_check(bitmap, ost->ltd_index))
2236                                         continue;
2237
2238                                 QOS_DEBUG("OST%d used in conflicting mirror "
2239                                           "component\n", ost->ltd_index);
2240                                 cfs_bitmap_set(bitmap, ost->ltd_index);
2241                                 lag->lag_ost_avail--;
2242
2243                                 for (k = 0; k < lag->lag_oaa_count; k++) {
2244                                         if (lag->lag_oss_avoid_array[k] ==
2245                                             lsq->lsq_id)
2246                                                 break;
2247                                 }
2248                                 if (k == lag->lag_oaa_count) {
2249                                         lag->lag_oss_avoid_array[k] =
2250                                                                 lsq->lsq_id;
2251                                         lag->lag_oaa_count++;
2252                                 }
2253                         }
2254                 }
2255         }
2256 }
2257
2258 /**
2259  * Create a striping for an obejct.
2260  *
2261  * The function creates a new striping for the object. The function tries QoS
2262  * algorithm first unless free space is distributed evenly among OSTs, but
2263  * by default RR algorithm is preferred due to internal concurrency (QoS is
2264  * serialized). The caller must ensure no concurrent calls to the function
2265  * are made against the same object.
2266  *
2267  * \param[in] env       execution environment for this thread
2268  * \param[in] lo        LOD object
2269  * \param[in] attr      attributes OST objects will be declared with
2270  * \param[in] th        transaction handle
2271  * \param[in] comp_idx  index of ldo_comp_entries
2272  *
2273  * \retval 0            on success
2274  * \retval negative     negated errno on error
2275  */
2276 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2277                         struct lu_attr *attr, struct thandle *th,
2278                         int comp_idx)
2279 {
2280         struct lod_layout_component *lod_comp;
2281         struct lod_device      *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2282         int                     stripe_len;
2283         int                     flag = LOV_USES_ASSIGNED_STRIPE;
2284         int                     i, rc = 0;
2285         struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2286         struct dt_object **stripe = NULL;
2287         __u32 *ost_indices = NULL;
2288         ENTRY;
2289
2290         LASSERT(lo);
2291         LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2292         lod_comp = &lo->ldo_comp_entries[comp_idx];
2293         LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2294
2295         /* A released component is being created */
2296         if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2297                 RETURN(0);
2298
2299         /* A Data-on-MDT component is being created */
2300         if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2301                 RETURN(0);
2302
2303         if (likely(lod_comp->llc_stripe == NULL)) {
2304                 /*
2305                  * no striping has been created so far
2306                  */
2307                 LASSERT(lod_comp->llc_stripe_count);
2308                 /*
2309                  * statfs and check OST targets now, since ld_active_tgt_count
2310                  * could be changed if some OSTs are [de]activated manually.
2311                  */
2312                 lod_qos_statfs_update(env, d);
2313                 stripe_len = lod_get_stripe_count(d, lo,
2314                                                   lod_comp->llc_stripe_count,
2315                                                   lod_comp->llc_pattern &
2316                                                   LOV_PATTERN_OVERSTRIPING);
2317
2318                 if (stripe_len == 0)
2319                         GOTO(out, rc = -ERANGE);
2320                 lod_comp->llc_stripe_count = stripe_len;
2321                 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
2322                 if (stripe == NULL)
2323                         GOTO(out, rc = -ENOMEM);
2324                 OBD_ALLOC(ost_indices, sizeof(*ost_indices) * stripe_len);
2325                 if (!ost_indices)
2326                         GOTO(out, rc = -ENOMEM);
2327
2328                 lod_getref(&d->lod_ost_descs);
2329                 /* XXX: support for non-0 files w/o objects */
2330                 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2331                                 d->lod_desc.ld_tgt_count, stripe_len);
2332
2333                 if (lod_comp->llc_ostlist.op_array &&
2334                     lod_comp->llc_ostlist.op_count) {
2335                         rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2336                                                 th, comp_idx);
2337                 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2338                         /**
2339                          * collect OSTs and OSSs used in other mirrors whose
2340                          * components cross the ldo_comp_entries[comp_idx]
2341                          */
2342                         rc = lod_prepare_avoidance(env, lo);
2343                         if (rc)
2344                                 GOTO(put_ldts, rc);
2345
2346                         QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2347                                   comp_idx);
2348                         lod_collect_avoidance(lo, lag, comp_idx);
2349
2350                         rc = lod_alloc_qos(env, lo, stripe, ost_indices, flag,
2351                                            th, comp_idx);
2352                         if (rc == -EAGAIN)
2353                                 rc = lod_alloc_rr(env, lo, stripe, ost_indices,
2354                                                   flag, th, comp_idx);
2355                 } else {
2356                         rc = lod_alloc_specific(env, lo, stripe, ost_indices,
2357                                                 flag, th, comp_idx);
2358                 }
2359 put_ldts:
2360                 lod_putref(d, &d->lod_ost_descs);
2361                 if (rc < 0) {
2362                         for (i = 0; i < stripe_len; i++)
2363                                 if (stripe[i] != NULL)
2364                                         dt_object_put(env, stripe[i]);
2365                         lod_comp->llc_stripe_count = 0;
2366                 } else {
2367                         lod_comp->llc_stripe = stripe;
2368                         lod_comp->llc_ost_indices = ost_indices;
2369                         lod_comp->llc_stripes_allocated = stripe_len;
2370                 }
2371         } else {
2372                 /*
2373                  * lod_qos_parse_config() found supplied buf as a predefined
2374                  * striping (not a hint), so it allocated all the object
2375                  * now we need to create them
2376                  */
2377                 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2378                         struct dt_object  *o;
2379
2380                         o = lod_comp->llc_stripe[i];
2381                         LASSERT(o);
2382
2383                         rc = lod_sub_declare_create(env, o, attr, NULL,
2384                                                     NULL, th);
2385                         if (rc < 0) {
2386                                 CERROR("can't declare create: %d\n", rc);
2387                                 break;
2388                         }
2389                 }
2390                 /**
2391                  * Clear LCME_FL_INIT for the component so that
2392                  * lod_striping_create() can create the striping objects
2393                  * in replay.
2394                  */
2395                 lod_comp_unset_init(lod_comp);
2396         }
2397
2398 out:
2399         if (rc < 0) {
2400                 if (stripe)
2401                         OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
2402                 if (ost_indices)
2403                         OBD_FREE(ost_indices,
2404                                  sizeof(*ost_indices) * stripe_len);
2405         }
2406         RETURN(rc);
2407 }
2408
2409 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2410                        struct lu_attr *attr, const struct lu_buf *buf,
2411                        struct thandle *th)
2412
2413 {
2414         struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2415         uint64_t size = 0;
2416         int i;
2417         int rc;
2418         ENTRY;
2419
2420         LASSERT(lo);
2421
2422         /* no OST available */
2423         /* XXX: should we be waiting a bit to prevent failures during
2424          * cluster initialization? */
2425         if (d->lod_ostnr == 0)
2426                 RETURN(-EIO);
2427
2428         /*
2429          * by this time, the object's ldo_stripe_count and ldo_stripe_size
2430          * contain default value for striping: taken from the parent
2431          * or from filesystem defaults
2432          *
2433          * in case the caller is passing lovea with new striping config,
2434          * we may need to parse lovea and apply new configuration
2435          */
2436         rc = lod_qos_parse_config(env, lo, buf);
2437         if (rc)
2438                 RETURN(rc);
2439
2440         if (attr->la_valid & LA_SIZE)
2441                 size = attr->la_size;
2442
2443         /**
2444          * prepare OST object creation for the component covering file's
2445          * size, the 1st component (including plain layout file) is always
2446          * instantiated.
2447          */
2448         for (i = 0; i < lo->ldo_comp_cnt; i++) {
2449                 struct lod_layout_component *lod_comp;
2450                 struct lu_extent *extent;
2451
2452                 lod_comp = &lo->ldo_comp_entries[i];
2453                 extent = &lod_comp->llc_extent;
2454                 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2455                 if (!lo->ldo_is_composite || size >= extent->e_start) {
2456                         rc = lod_qos_prep_create(env, lo, attr, th, i);
2457                         if (rc)
2458                                 break;
2459                 }
2460         }
2461
2462         RETURN(rc);
2463 }