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