Whamcloud - gitweb
269e4cac673c6b932575632cbb5050e33e605752
[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, 2013, 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  */
35
36 #define DEBUG_SUBSYSTEM S_LOV
37
38 #include <libcfs/libcfs.h>
39 #include <obd_class.h>
40 #include <obd_lov.h>
41 #include <lustre/lustre_idl.h>
42 #include "lod_internal.h"
43
44 /*
45  * force QoS policy (not RR) to be used for testing purposes
46  */
47 #define FORCE_QOS_
48
49 #define D_QOS   D_OTHER
50
51 #if 0
52 #define QOS_DEBUG(fmt, ...)     CDEBUG(D_OTHER, fmt, ## __VA_ARGS__)
53 #define QOS_CONSOLE(fmt, ...)   LCONSOLE(D_OTHER, fmt, ## __VA_ARGS__)
54 #else
55 #define QOS_DEBUG(fmt, ...)
56 #define QOS_CONSOLE(fmt, ...)
57 #endif
58
59 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
60                        OST_TGT(lod,i)->ltd_statfs.os_bsize)
61
62 int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
63 {
64         struct lod_qos_oss *oss = NULL, *temposs;
65         struct obd_export  *exp = ost_desc->ltd_exp;
66         int                 rc = 0, found = 0;
67         cfs_list_t         *list;
68         ENTRY;
69
70         down_write(&lod->lod_qos.lq_rw_sem);
71         /*
72          * a bit hacky approach to learn NID of corresponding connection
73          * but there is no official API to access information like this
74          * with OSD API.
75          */
76         cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
77                 if (obd_uuid_equals(&oss->lqo_uuid,
78                                     &exp->exp_connection->c_remote_uuid)) {
79                         found++;
80                         break;
81                 }
82         }
83
84         if (!found) {
85                 OBD_ALLOC_PTR(oss);
86                 if (!oss)
87                         GOTO(out, rc = -ENOMEM);
88                 memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
89                        sizeof(oss->lqo_uuid));
90         } else {
91                 /* Assume we have to move this one */
92                 cfs_list_del(&oss->lqo_oss_list);
93         }
94
95         oss->lqo_ost_count++;
96         ost_desc->ltd_qos.ltq_oss = oss;
97
98         CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
99                obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
100                oss->lqo_ost_count);
101
102         /* Add sorted by # of OSTs.  Find the first entry that we're
103            bigger than... */
104         list = &lod->lod_qos.lq_oss_list;
105         cfs_list_for_each_entry(temposs, list, lqo_oss_list) {
106                 if (oss->lqo_ost_count > temposs->lqo_ost_count)
107                         break;
108         }
109         /* ...and add before it.  If we're the first or smallest, temposs
110            points to the list head, and we add to the end. */
111         cfs_list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
112
113         lod->lod_qos.lq_dirty = 1;
114         lod->lod_qos.lq_rr.lqr_dirty = 1;
115
116 out:
117         up_write(&lod->lod_qos.lq_rw_sem);
118         RETURN(rc);
119 }
120
121 int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
122 {
123         struct lod_qos_oss *oss;
124         int                 rc = 0;
125         ENTRY;
126
127         down_write(&lod->lod_qos.lq_rw_sem);
128         oss = ost_desc->ltd_qos.ltq_oss;
129         if (!oss)
130                 GOTO(out, rc = -ENOENT);
131
132         oss->lqo_ost_count--;
133         if (oss->lqo_ost_count == 0) {
134                 CDEBUG(D_QOS, "removing OSS %s\n",
135                        obd_uuid2str(&oss->lqo_uuid));
136                 cfs_list_del(&oss->lqo_oss_list);
137                 ost_desc->ltd_qos.ltq_oss = NULL;
138                 OBD_FREE_PTR(oss);
139         }
140
141         lod->lod_qos.lq_dirty = 1;
142         lod->lod_qos.lq_rr.lqr_dirty = 1;
143 out:
144         up_write(&lod->lod_qos.lq_rw_sem);
145         RETURN(rc);
146 }
147
148 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
149                                 int index, struct obd_statfs *sfs)
150 {
151         struct lod_tgt_desc *ost;
152         int                  rc;
153
154         LASSERT(d);
155         ost = OST_TGT(d,index);
156         LASSERT(ost);
157
158         rc = dt_statfs(env, ost->ltd_ost, sfs);
159         if (rc && rc != -ENOTCONN)
160                 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
161
162         /* If the OST is readonly then we can't allocate objects there */
163         if (sfs->os_state & OS_STATE_READONLY)
164                 rc = -EROFS;
165
166         /* check whether device has changed state (active, inactive) */
167         if (rc != 0 && ost->ltd_active) {
168                 /* turned inactive? */
169                 spin_lock(&d->lod_desc_lock);
170                 if (ost->ltd_active) {
171                         ost->ltd_active = 0;
172                         LASSERT(d->lod_desc.ld_active_tgt_count > 0);
173                         d->lod_desc.ld_active_tgt_count--;
174                         d->lod_qos.lq_dirty = 1;
175                         d->lod_qos.lq_rr.lqr_dirty = 1;
176                         CDEBUG(D_CONFIG, "%s: turns inactive\n",
177                                ost->ltd_exp->exp_obd->obd_name);
178                 }
179                 spin_unlock(&d->lod_desc_lock);
180         } else if (rc == 0 && ost->ltd_active == 0) {
181                 /* turned active? */
182                 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
183                          "active tgt count %d, ost nr %d\n",
184                          d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
185                 spin_lock(&d->lod_desc_lock);
186                 if (ost->ltd_active == 0) {
187                         ost->ltd_active = 1;
188                         d->lod_desc.ld_active_tgt_count++;
189                         d->lod_qos.lq_dirty = 1;
190                         d->lod_qos.lq_rr.lqr_dirty = 1;
191                         CDEBUG(D_CONFIG, "%s: turns active\n",
192                                ost->ltd_exp->exp_obd->obd_name);
193                 }
194                 spin_unlock(&d->lod_desc_lock);
195         }
196
197         RETURN(rc);
198 }
199
200 static void lod_qos_statfs_update(const struct lu_env *env,
201                                   struct lod_device *lod)
202 {
203         struct obd_device *obd = lod2obd(lod);
204         struct ost_pool   *osts = &(lod->lod_pool_info);
205         int                i, idx, rc = 0;
206         __u64              max_age, avail;
207         ENTRY;
208
209         max_age = cfs_time_shift_64(-2 * lod->lod_desc.ld_qos_maxage);
210
211         if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
212                 /* statfs data are quite recent, don't need to refresh it */
213                 RETURN_EXIT;
214
215         down_write(&lod->lod_qos.lq_rw_sem);
216         if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
217                 GOTO(out, rc = 0);
218
219         for (i = 0; i < osts->op_count; i++) {
220                 idx = osts->op_array[i];
221                 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
222                 rc = lod_statfs_and_check(env, lod, idx,
223                                           &OST_TGT(lod,idx)->ltd_statfs);
224                 if (rc)
225                         break;
226                 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
227                         /* recalculate weigths */
228                         lod->lod_qos.lq_dirty = 1;
229         }
230         obd->obd_osfs_age = cfs_time_current_64();
231
232 out:
233         up_write(&lod->lod_qos.lq_rw_sem);
234         EXIT;
235 }
236
237 /* Recalculate per-object penalties for OSSs and OSTs,
238    depends on size of each ost in an oss */
239 static int lod_qos_calc_ppo(struct lod_device *lod)
240 {
241         struct lod_qos_oss *oss;
242         __u64               ba_max, ba_min, temp;
243         __u32               num_active;
244         int                 rc, i, prio_wide;
245         time_t              now, age;
246         ENTRY;
247
248         if (!lod->lod_qos.lq_dirty)
249                 GOTO(out, rc = 0);
250
251         num_active = lod->lod_desc.ld_active_tgt_count - 1;
252         if (num_active < 1)
253                 GOTO(out, rc = -EAGAIN);
254
255         /* find bavail on each OSS */
256         cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
257                                 oss->lqo_bavail = 0;
258         lod->lod_qos.lq_active_oss_count = 0;
259
260         /*
261          * How badly user wants to select OSTs "widely" (not recently chosen
262          * and not on recent OSS's).  As opposed to "freely" (free space
263          * avail.) 0-256
264          */
265         prio_wide = 256 - lod->lod_qos.lq_prio_free;
266
267         ba_min = (__u64)(-1);
268         ba_max = 0;
269         now = cfs_time_current_sec();
270         /* Calculate OST penalty per object
271          * (lod ref taken in lod_qos_prep_create()) */
272         cfs_foreach_bit(lod->lod_ost_bitmap, i) {
273                 LASSERT(OST_TGT(lod,i));
274                 temp = TGT_BAVAIL(i);
275                 if (!temp)
276                         continue;
277                 ba_min = min(temp, ba_min);
278                 ba_max = max(temp, ba_max);
279
280                 /* Count the number of usable OSS's */
281                 if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
282                         lod->lod_qos.lq_active_oss_count++;
283                 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
284
285                 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
286                 temp >>= 1;
287                 lov_do_div64(temp, num_active);
288                 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
289                         (temp * prio_wide) >> 8;
290
291                 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
292                 if (lod->lod_qos.lq_reset ||
293                     age > 32 * lod->lod_desc.ld_qos_maxage)
294                         OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
295                 else if (age > lod->lod_desc.ld_qos_maxage)
296                         /* Decay the penalty by half for every 8x the update
297                          * interval that the device has been idle.  That gives
298                          * lots of time for the statfs information to be
299                          * updated (which the penalty is only a proxy for),
300                          * and avoids penalizing OSS/OSTs under light load. */
301                         OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
302                                 (age / lod->lod_desc.ld_qos_maxage);
303         }
304
305         num_active = lod->lod_qos.lq_active_oss_count - 1;
306         if (num_active < 1) {
307                 /* If there's only 1 OSS, we can't penalize it, so instead
308                    we have to double the OST penalty */
309                 num_active = 1;
310                 cfs_foreach_bit(lod->lod_ost_bitmap, i)
311                         OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
312         }
313
314         /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
315         cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
316                 temp = oss->lqo_bavail >> 1;
317                 lov_do_div64(temp, oss->lqo_ost_count * num_active);
318                 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
319
320                 age = (now - oss->lqo_used) >> 3;
321                 if (lod->lod_qos.lq_reset ||
322                     age > 32 * lod->lod_desc.ld_qos_maxage)
323                         oss->lqo_penalty = 0;
324                 else if (age > lod->lod_desc.ld_qos_maxage)
325                         /* Decay the penalty by half for every 8x the update
326                          * interval that the device has been idle.  That gives
327                          * lots of time for the statfs information to be
328                          * updated (which the penalty is only a proxy for),
329                          * and avoids penalizing OSS/OSTs under light load. */
330                         oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
331         }
332
333         lod->lod_qos.lq_dirty = 0;
334         lod->lod_qos.lq_reset = 0;
335
336         /* If each ost has almost same free space,
337          * do rr allocation for better creation performance */
338         lod->lod_qos.lq_same_space = 0;
339         if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
340                 lod->lod_qos.lq_same_space = 1;
341                 /* Reset weights for the next time we enter qos mode */
342                 lod->lod_qos.lq_reset = 1;
343         }
344         rc = 0;
345
346 out:
347 #ifndef FORCE_QOS
348         if (!rc && lod->lod_qos.lq_same_space)
349                 RETURN(-EAGAIN);
350 #endif
351         RETURN(rc);
352 }
353
354 static int lod_qos_calc_weight(struct lod_device *lod, int i)
355 {
356         __u64 temp, temp2;
357
358         /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
359         temp = TGT_BAVAIL(i);
360         temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
361                 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
362         if (temp < temp2)
363                 OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
364         else
365                 OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
366         return 0;
367 }
368
369 /* We just used this index for a stripe; adjust everyone's weights */
370 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
371                         __u32 index, __u64 *total_wt)
372 {
373         struct lod_tgt_desc *ost;
374         struct lod_qos_oss  *oss;
375         int j;
376         ENTRY;
377
378         ost = OST_TGT(lod,index);
379         LASSERT(ost);
380
381         /* Don't allocate on this devuce anymore, until the next alloc_qos */
382         ost->ltd_qos.ltq_usable = 0;
383
384         oss = ost->ltd_qos.ltq_oss;
385
386         /* Decay old penalty by half (we're adding max penalty, and don't
387            want it to run away.) */
388         ost->ltd_qos.ltq_penalty >>= 1;
389         oss->lqo_penalty >>= 1;
390
391         /* mark the OSS and OST as recently used */
392         ost->ltd_qos.ltq_used = oss->lqo_used = cfs_time_current_sec();
393
394         /* Set max penalties for this OST and OSS */
395         ost->ltd_qos.ltq_penalty +=
396                 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
397         oss->lqo_penalty += oss->lqo_penalty_per_obj *
398                 lod->lod_qos.lq_active_oss_count;
399
400         /* Decrease all OSS penalties */
401         cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
402                 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
403                         oss->lqo_penalty = 0;
404                 else
405                         oss->lqo_penalty -= oss->lqo_penalty_per_obj;
406         }
407
408         *total_wt = 0;
409         /* Decrease all OST penalties */
410         for (j = 0; j < osts->op_count; j++) {
411                 int i;
412
413                 i = osts->op_array[j];
414                 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
415                         continue;
416
417                 ost = OST_TGT(lod,i);
418                 LASSERT(ost);
419
420                 if (ost->ltd_qos.ltq_penalty <
421                                 ost->ltd_qos.ltq_penalty_per_obj)
422                         ost->ltd_qos.ltq_penalty = 0;
423                 else
424                         ost->ltd_qos.ltq_penalty -=
425                                 ost->ltd_qos.ltq_penalty_per_obj;
426
427                 lod_qos_calc_weight(lod, i);
428
429                 /* Recalc the total weight of usable osts */
430                 if (ost->ltd_qos.ltq_usable)
431                         *total_wt += ost->ltd_qos.ltq_weight;
432
433                 QOS_DEBUG("recalc tgt %d usable=%d avail="LPU64
434                           " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
435                           " ossp="LPU64" wt="LPU64"\n",
436                           i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
437                           ost->ltd_qos.ltq_penalty_per_obj >> 10,
438                           ost->ltd_qos.ltq_penalty >> 10,
439                           ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
440                           ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
441                           ost->ltd_qos.ltq_weight >> 10);
442         }
443
444         RETURN(0);
445 }
446
447 #define LOV_QOS_EMPTY ((__u32)-1)
448 /* compute optimal round-robin order, based on OSTs per OSS */
449 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
450                            struct lod_qos_rr *lqr)
451 {
452         struct lod_qos_oss  *oss;
453         struct lod_tgt_desc *ost;
454         unsigned placed, real_count;
455         int i, rc;
456         ENTRY;
457
458         if (!lqr->lqr_dirty) {
459                 LASSERT(lqr->lqr_pool.op_size);
460                 RETURN(0);
461         }
462
463         /* Do actual allocation. */
464         down_write(&lod->lod_qos.lq_rw_sem);
465
466         /*
467          * Check again. While we were sleeping on @lq_rw_sem something could
468          * change.
469          */
470         if (!lqr->lqr_dirty) {
471                 LASSERT(lqr->lqr_pool.op_size);
472                 up_write(&lod->lod_qos.lq_rw_sem);
473                 RETURN(0);
474         }
475
476         real_count = src_pool->op_count;
477
478         /* Zero the pool array */
479         /* alloc_rr is holding a read lock on the pool, so nobody is adding/
480            deleting from the pool. The lq_rw_sem insures that nobody else
481            is reading. */
482         lqr->lqr_pool.op_count = real_count;
483         rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
484         if (rc) {
485                 up_write(&lod->lod_qos.lq_rw_sem);
486                 RETURN(rc);
487         }
488         for (i = 0; i < lqr->lqr_pool.op_count; i++)
489                 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
490
491         /* Place all the OSTs from 1 OSS at the same time. */
492         placed = 0;
493         cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
494                 int j = 0;
495
496                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
497                         int next;
498
499                         if (!cfs_bitmap_check(lod->lod_ost_bitmap,
500                                                 src_pool->op_array[i]))
501                                 continue;
502
503                         ost = OST_TGT(lod,src_pool->op_array[i]);
504                         LASSERT(ost && ost->ltd_ost);
505                         if (ost->ltd_qos.ltq_oss != oss)
506                                 continue;
507
508                         /* Evenly space these OSTs across arrayspace */
509                         next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
510                         while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
511                                 next = (next + 1) % lqr->lqr_pool.op_count;
512
513                         lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
514                         j++;
515                         placed++;
516                 }
517         }
518
519         lqr->lqr_dirty = 0;
520         up_write(&lod->lod_qos.lq_rw_sem);
521
522         if (placed != real_count) {
523                 /* This should never happen */
524                 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
525                                    "round-robin list (%d of %d).\n",
526                                    placed, real_count);
527                 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
528                         LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
529                                  lqr->lqr_pool.op_array[i]);
530                 }
531                 lqr->lqr_dirty = 1;
532                 RETURN(-EAGAIN);
533         }
534
535 #if 0
536         for (i = 0; i < lqr->lqr_pool.op_count; i++)
537                 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
538 #endif
539
540         RETURN(0);
541 }
542
543 /**
544  * A helper function to:
545  *   create in-core lu object on the specified OSP
546  *   declare creation of the object
547  * IMPORTANT: at this stage object is anonymouos - it has no fid assigned
548  *            this is a workaround till we have natural FIDs on OST
549  *
550  *            at this point we want to declare (reserve) object for us as
551  *            we can't block at execution (when create method is called).
552  *            otherwise we'd block whole transaction batch
553  */
554 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
555                                                    struct lod_device *d,
556                                                    int ost_idx,
557                                                    struct thandle *th)
558 {
559         struct lod_tgt_desc *ost;
560         struct lu_object *o, *n;
561         struct lu_device *nd;
562         struct dt_object *dt;
563         int               rc;
564         ENTRY;
565
566         LASSERT(d);
567         LASSERT(ost_idx >= 0);
568         LASSERT(ost_idx < d->lod_osts_size);
569         ost = OST_TGT(d,ost_idx);
570         LASSERT(ost);
571         LASSERT(ost->ltd_ost);
572
573         nd = &ost->ltd_ost->dd_lu_dev;
574
575         /*
576          * allocate anonymous object with zero fid, real fid
577          * will be assigned by OSP within transaction
578          * XXX: to be fixed with fully-functional OST fids
579          */
580         o = lu_object_anon(env, nd, NULL);
581         if (IS_ERR(o))
582                 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
583
584         n = lu_object_locate(o->lo_header, nd->ld_type);
585         if (unlikely(n == NULL)) {
586                 CERROR("can't find slice\n");
587                 lu_object_put(env, o);
588                 GOTO(out, dt = ERR_PTR(-EINVAL));
589         }
590
591         dt = container_of(n, struct dt_object, do_lu);
592
593         rc = dt_declare_create(env, dt, NULL, NULL, NULL, th);
594         if (rc) {
595                 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
596                        ost_idx, rc);
597                 lu_object_put(env, o);
598                 dt = ERR_PTR(rc);
599         }
600
601 out:
602         RETURN(dt);
603 }
604
605 static int min_stripe_count(int stripe_cnt, int flags)
606 {
607         return (flags & LOV_USES_DEFAULT_STRIPE ?
608                         stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
609 }
610
611 #define LOV_CREATE_RESEED_MULT 30
612 #define LOV_CREATE_RESEED_MIN  2000
613
614 static int inline lod_qos_dev_is_full(struct obd_statfs *msfs)
615 {
616         __u64 used;
617         int   bs = msfs->os_bsize;
618
619         LASSERT(((bs - 1) & bs) == 0);
620
621         /* the minimum of 0.1% used blocks and 1GB bytes. */
622         used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
623                         1 << (31 - ffs(bs)));
624         return (msfs->os_bavail < used);
625 }
626
627 int lod_ea_store_resize(struct lod_thread_info *info, int size);
628
629 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env, int stripes)
630 {
631         struct lod_thread_info *info = lod_env_info(env);
632
633         if (info->lti_ea_store_size < sizeof(int) * stripes)
634                 lod_ea_store_resize(info, stripes * sizeof(int));
635         if (info->lti_ea_store_size < sizeof(int) * stripes) {
636                 CERROR("can't allocate memory for ost-in-use array\n");
637                 return -ENOMEM;
638         }
639         memset(info->lti_ea_store, -1, sizeof(int) * stripes);
640         return 0;
641 }
642
643 static inline void lod_qos_ost_in_use(const struct lu_env *env, int idx, int ost)
644 {
645         struct lod_thread_info *info = lod_env_info(env);
646         int *osts = info->lti_ea_store;
647
648         LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
649         osts[idx] = ost;
650 }
651
652 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, int stripes)
653 {
654         struct lod_thread_info *info = lod_env_info(env);
655         int *osts = info->lti_ea_store;
656         int j;
657
658         for (j = 0; j < stripes; j++) {
659                 if (osts[j] == ost)
660                         return 1;
661         }
662         return 0;
663 }
664
665 /* Allocate objects on OSTs with round-robin algorithm */
666 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
667                         struct dt_object **stripe, int flags,
668                         struct thandle *th)
669 {
670         struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
671         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
672         struct pool_desc  *pool = NULL;
673         struct ost_pool   *osts;
674         struct lod_qos_rr *lqr;
675         struct dt_object  *o;
676         unsigned           array_idx;
677         int                i, rc;
678         int                ost_start_idx_temp;
679         int                speed = 0;
680         int                stripe_idx = 0;
681         int                stripe_cnt = lo->ldo_stripenr;
682         int                stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
683         __u32              ost_idx;
684         ENTRY;
685
686         if (lo->ldo_pool)
687                 pool = lod_find_pool(m, lo->ldo_pool);
688
689         if (pool != NULL) {
690                 down_read(&pool_tgt_rw_sem(pool));
691                 osts = &(pool->pool_obds);
692                 lqr = &(pool->pool_rr);
693         } else {
694                 osts = &(m->lod_pool_info);
695                 lqr = &(m->lod_qos.lq_rr);
696         }
697
698         rc = lod_qos_calc_rr(m, osts, lqr);
699         if (rc)
700                 GOTO(out, rc);
701
702         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
703         if (rc)
704                 GOTO(out, rc);
705
706         if (--lqr->lqr_start_count <= 0) {
707                 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
708                 lqr->lqr_start_count =
709                         (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
710                          LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
711         } else if (stripe_cnt_min >= osts->op_count ||
712                         lqr->lqr_start_idx > osts->op_count) {
713                 /* If we have allocated from all of the OSTs, slowly
714                  * precess the next start if the OST/stripe count isn't
715                  * already doing this for us. */
716                 lqr->lqr_start_idx %= osts->op_count;
717                 if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
718                         ++lqr->lqr_offset_idx;
719         }
720         down_read(&m->lod_qos.lq_rw_sem);
721         ost_start_idx_temp = lqr->lqr_start_idx;
722
723 repeat_find:
724         array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
725                         osts->op_count;
726
727         QOS_DEBUG("pool '%s' want %d startidx %d startcnt %d offset %d "
728                   "active %d count %d arrayidx %d\n",
729                   lo->ldo_pool ? lo->ldo_pool : "",
730                   stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
731                   lqr->lqr_offset_idx, osts->op_count, osts->op_count,
732                   array_idx);
733
734         for (i = 0; i < osts->op_count && stripe_idx < lo->ldo_stripenr;
735              i++, array_idx = (array_idx + 1) % osts->op_count) {
736                 ++lqr->lqr_start_idx;
737                 ost_idx = lqr->lqr_pool.op_array[array_idx];
738
739                 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
740                           i, lqr->lqr_start_idx, /* XXX: active*/ 0,
741                           stripe_idx, array_idx, ost_idx);
742
743                 if ((ost_idx == LOV_QOS_EMPTY) ||
744                     !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
745                         continue;
746
747                 /* Fail Check before osc_precreate() is called
748                    so we can only 'fail' single OSC. */
749                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
750                         continue;
751
752                 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
753                 if (rc) {
754                         /* this OSP doesn't feel well */
755                         continue;
756                 }
757
758                 /*
759                  * skip full devices
760                  */
761                 if (lod_qos_dev_is_full(sfs)) {
762                         QOS_DEBUG("#%d is full\n", ost_idx);
763                         continue;
764                 }
765
766                 /*
767                  * We expect number of precreated objects in f_ffree at
768                  * the first iteration, skip OSPs with no objects ready
769                  */
770                 if (sfs->os_fprecreated == 0 && speed == 0) {
771                         QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
772                         continue;
773                 }
774
775                 /*
776                  * try to use another OSP if this one is degraded
777                  */
778                 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
779                         QOS_DEBUG("#%d: degraded\n", ost_idx);
780                         continue;
781                 }
782
783                 /*
784                  * do not put >1 objects on a single OST
785                  */
786                 if (speed && lod_qos_is_ost_used(env, ost_idx, stripe_idx))
787                         continue;
788
789                 o = lod_qos_declare_object_on(env, m, ost_idx, th);
790                 if (IS_ERR(o)) {
791                         CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
792                                ost_idx, (int) PTR_ERR(o));
793                         rc = PTR_ERR(o);
794                         continue;
795                 }
796
797                 /*
798                  * We've successfuly declared (reserved) an object
799                  */
800                 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
801                 stripe[stripe_idx] = o;
802                 stripe_idx++;
803
804         }
805         if ((speed < 2) && (stripe_idx < stripe_cnt_min)) {
806                 /* Try again, allowing slower OSCs */
807                 speed++;
808                 lqr->lqr_start_idx = ost_start_idx_temp;
809                 goto repeat_find;
810         }
811
812         up_read(&m->lod_qos.lq_rw_sem);
813
814         if (stripe_idx) {
815                 lo->ldo_stripenr = stripe_idx;
816                 /* at least one stripe is allocated */
817                 rc = 0;
818         } else {
819                 /* nobody provided us with a single object */
820                 rc = -ENOSPC;
821         }
822
823 out:
824         if (pool != NULL) {
825                 up_read(&pool_tgt_rw_sem(pool));
826                 /* put back ref got by lod_find_pool() */
827                 lod_pool_putref(pool);
828         }
829
830         RETURN(rc);
831 }
832
833 /* alloc objects on osts with specific stripe offset */
834 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
835                               struct dt_object **stripe, int flags,
836                               struct thandle *th)
837 {
838         struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
839         struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
840         struct dt_object  *o;
841         unsigned           ost_idx, array_idx, ost_count;
842         int                i, rc, stripe_num = 0;
843         int                speed = 0;
844         struct pool_desc  *pool = NULL;
845         struct ost_pool   *osts;
846         ENTRY;
847
848         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
849         if (rc)
850                 GOTO(out, rc);
851
852         if (lo->ldo_pool)
853                 pool = lod_find_pool(m, lo->ldo_pool);
854
855         if (pool != NULL) {
856                 down_read(&pool_tgt_rw_sem(pool));
857                 osts = &(pool->pool_obds);
858         } else {
859                 osts = &(m->lod_pool_info);
860         }
861
862         ost_count = osts->op_count;
863
864 repeat_find:
865         /* search loi_ost_idx in ost array */
866         array_idx = 0;
867         for (i = 0; i < ost_count; i++) {
868                 if (osts->op_array[i] == lo->ldo_def_stripe_offset) {
869                         array_idx = i;
870                         break;
871                 }
872         }
873         if (i == ost_count) {
874                 CERROR("Start index %d not found in pool '%s'\n",
875                        lo->ldo_def_stripe_offset,
876                        lo->ldo_pool ? lo->ldo_pool : "");
877                 GOTO(out, rc = -EINVAL);
878         }
879
880         for (i = 0; i < ost_count;
881                         i++, array_idx = (array_idx + 1) % ost_count) {
882                 ost_idx = osts->op_array[array_idx];
883
884                 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
885                         continue;
886
887                 /* Fail Check before osc_precreate() is called
888                    so we can only 'fail' single OSC. */
889                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
890                         continue;
891
892                 /*
893                  * do not put >1 objects on a single OST
894                  */
895                 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
896                         continue;
897
898                 /* Drop slow OSCs if we can, but not for requested start idx.
899                  *
900                  * This means "if OSC is slow and it is not the requested
901                  * start OST, then it can be skipped, otherwise skip it only
902                  * if it is inactive/recovering/out-of-space." */
903
904                 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
905                 if (rc) {
906                         /* this OSP doesn't feel well */
907                         continue;
908                 }
909
910                 /*
911                  * We expect number of precreated objects in f_ffree at
912                  * the first iteration, skip OSPs with no objects ready
913                  * don't apply this logic to OST specified with stripe_offset
914                  */
915                 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
916                         continue;
917
918                 o = lod_qos_declare_object_on(env, m, ost_idx, th);
919                 if (IS_ERR(o)) {
920                         CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
921                                ost_idx, (int) PTR_ERR(o));
922                         continue;
923                 }
924
925                 /*
926                  * We've successfuly declared (reserved) an object
927                  */
928                 lod_qos_ost_in_use(env, stripe_num, ost_idx);
929                 stripe[stripe_num] = o;
930                 stripe_num++;
931
932                 /* We have enough stripes */
933                 if (stripe_num == lo->ldo_stripenr)
934                         GOTO(out, rc = 0);
935         }
936         if (speed < 2) {
937                 /* Try again, allowing slower OSCs */
938                 speed++;
939                 goto repeat_find;
940         }
941
942         /* If we were passed specific striping params, then a failure to
943          * meet those requirements is an error, since we can't reallocate
944          * that memory (it might be part of a larger array or something).
945          *
946          * We can only get here if lsm_stripe_count was originally > 1.
947          */
948         CERROR("can't lstripe objid "DFID": have %d want %u\n",
949                PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
950                lo->ldo_stripenr);
951         rc = -EFBIG;
952 out:
953         if (pool != NULL) {
954                 up_read(&pool_tgt_rw_sem(pool));
955                 /* put back ref got by lod_find_pool() */
956                 lod_pool_putref(pool);
957         }
958
959         RETURN(rc);
960 }
961
962 static inline int lod_qos_is_usable(struct lod_device *lod)
963 {
964 #ifdef FORCE_QOS
965         /* to be able to debug QoS code */
966         return 1;
967 #endif
968
969         /* Detect -EAGAIN early, before expensive lock is taken. */
970         if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
971                 return 0;
972
973         if (lod->lod_desc.ld_active_tgt_count < 2)
974                 return 0;
975
976         return 1;
977 }
978
979 /* Alloc objects on OSTs with optimization based on:
980    - free space
981    - network resources (shared OSS's)
982  */
983 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
984                          struct dt_object **stripe, int flags,
985                          struct thandle *th)
986 {
987         struct lod_device   *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
988         struct obd_statfs   *sfs = &lod_env_info(env)->lti_osfs;
989         struct lod_tgt_desc *ost;
990         struct dt_object    *o;
991         __u64                total_weight = 0;
992         int                  nfound, good_osts, i, rc = 0;
993         int                  stripe_cnt = lo->ldo_stripenr;
994         int                  stripe_cnt_min;
995         struct pool_desc    *pool = NULL;
996         struct ost_pool    *osts;
997         ENTRY;
998
999         stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1000         if (stripe_cnt_min < 1)
1001                 RETURN(-EINVAL);
1002
1003         if (lo->ldo_pool)
1004                 pool = lod_find_pool(m, lo->ldo_pool);
1005
1006         if (pool != NULL) {
1007                 down_read(&pool_tgt_rw_sem(pool));
1008                 osts = &(pool->pool_obds);
1009         } else {
1010                 osts = &(m->lod_pool_info);
1011         }
1012
1013         /* Detect -EAGAIN early, before expensive lock is taken. */
1014         if (!lod_qos_is_usable(m))
1015                 GOTO(out_nolock, rc = -EAGAIN);
1016
1017         /* Do actual allocation, use write lock here. */
1018         down_write(&m->lod_qos.lq_rw_sem);
1019
1020         /*
1021          * Check again, while we were sleeping on @lq_rw_sem things could
1022          * change.
1023          */
1024         if (!lod_qos_is_usable(m))
1025                 GOTO(out, rc = -EAGAIN);
1026
1027         rc = lod_qos_calc_ppo(m);
1028         if (rc)
1029                 GOTO(out, rc);
1030
1031         rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1032         if (rc)
1033                 GOTO(out, rc);
1034
1035         good_osts = 0;
1036         /* Find all the OSTs that are valid stripe candidates */
1037         for (i = 0; i < osts->op_count; i++) {
1038                 if (!cfs_bitmap_check(m->lod_ost_bitmap, osts->op_array[i]))
1039                         continue;
1040
1041                 rc = lod_statfs_and_check(env, m, osts->op_array[i], sfs);
1042                 if (rc) {
1043                         /* this OSP doesn't feel well */
1044                         continue;
1045                 }
1046
1047                 /*
1048                  * skip full devices
1049                  */
1050                 if (lod_qos_dev_is_full(sfs))
1051                         continue;
1052
1053                 /* Fail Check before osc_precreate() is called
1054                    so we can only 'fail' single OSC. */
1055                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1056                                    osts->op_array[i] == 0)
1057                         continue;
1058
1059                 ost = OST_TGT(m,osts->op_array[i]);
1060                 ost->ltd_qos.ltq_usable = 1;
1061                 lod_qos_calc_weight(m, osts->op_array[i]);
1062                 total_weight += ost->ltd_qos.ltq_weight;
1063
1064                 good_osts++;
1065         }
1066
1067         QOS_DEBUG("found %d good osts\n", good_osts);
1068
1069         if (good_osts < stripe_cnt_min)
1070                 GOTO(out, rc = -EAGAIN);
1071
1072         /* We have enough osts */
1073         if (good_osts < stripe_cnt)
1074                 stripe_cnt = good_osts;
1075
1076         /* Find enough OSTs with weighted random allocation. */
1077         nfound = 0;
1078         while (nfound < stripe_cnt) {
1079                 __u64 rand, cur_weight;
1080
1081                 cur_weight = 0;
1082                 rc = -ENOSPC;
1083
1084                 if (total_weight) {
1085 #if BITS_PER_LONG == 32
1086                         rand = cfs_rand() % (unsigned)total_weight;
1087                         /* If total_weight > 32-bit, first generate the high
1088                          * 32 bits of the random number, then add in the low
1089                          * 32 bits (truncated to the upper limit, if needed) */
1090                         if (total_weight > 0xffffffffULL)
1091                                 rand = (__u64)(cfs_rand() %
1092                                         (unsigned)(total_weight >> 32)) << 32;
1093                         else
1094                                 rand = 0;
1095
1096                         if (rand == (total_weight & 0xffffffff00000000ULL))
1097                                 rand |= cfs_rand() % (unsigned)total_weight;
1098                         else
1099                                 rand |= cfs_rand();
1100
1101 #else
1102                         rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1103                                 total_weight;
1104 #endif
1105                 } else {
1106                         rand = 0;
1107                 }
1108
1109                 /* On average, this will hit larger-weighted osts more often.
1110                    0-weight osts will always get used last (only when rand=0) */
1111                 for (i = 0; i < osts->op_count; i++) {
1112                         int idx = osts->op_array[i];
1113
1114                         if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
1115                                 continue;
1116
1117                         ost = OST_TGT(m,idx);
1118
1119                         if (!ost->ltd_qos.ltq_usable)
1120                                 continue;
1121
1122                         cur_weight += ost->ltd_qos.ltq_weight;
1123                         QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight="LPU64
1124                                   " rand="LPU64" total_weight="LPU64"\n",
1125                                   stripe_cnt, nfound, cur_weight, rand,
1126                                   total_weight);
1127
1128                         if (cur_weight < rand)
1129                                 continue;
1130
1131                         QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1132
1133                         /*
1134                          * do not put >1 objects on a single OST
1135                          */
1136                         if (lod_qos_is_ost_used(env, idx, nfound))
1137                                 continue;
1138                         lod_qos_ost_in_use(env, nfound, idx);
1139
1140                         o = lod_qos_declare_object_on(env, m, idx, th);
1141                         if (IS_ERR(o)) {
1142                                 QOS_DEBUG("can't declare object on #%u: %d\n",
1143                                           idx, (int) PTR_ERR(o));
1144                                 continue;
1145                         }
1146                         stripe[nfound++] = o;
1147                         lod_qos_used(m, osts, idx, &total_weight);
1148                         rc = 0;
1149                         break;
1150                 }
1151
1152                 if (rc) {
1153                         /* no OST found on this iteration, give up */
1154                         break;
1155                 }
1156         }
1157
1158         if (unlikely(nfound != stripe_cnt)) {
1159                 /*
1160                  * when the decision to use weighted algorithm was made
1161                  * we had enough appropriate OSPs, but this state can
1162                  * change anytime (no space on OST, broken connection, etc)
1163                  * so it's possible OSP won't be able to provide us with
1164                  * an object due to just changed state
1165                  */
1166                 LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
1167                 for (i = 0; i < nfound; i++) {
1168                         LASSERT(stripe[i] != NULL);
1169                         lu_object_put(env, &stripe[i]->do_lu);
1170                         stripe[i] = NULL;
1171                 }
1172
1173                 /* makes sense to rebalance next time */
1174                 m->lod_qos.lq_dirty = 1;
1175                 m->lod_qos.lq_same_space = 0;
1176
1177                 rc = -EAGAIN;
1178         }
1179
1180 out:
1181         up_write(&m->lod_qos.lq_rw_sem);
1182
1183 out_nolock:
1184         if (pool != NULL) {
1185                 up_read(&pool_tgt_rw_sem(pool));
1186                 /* put back ref got by lod_find_pool() */
1187                 lod_pool_putref(pool);
1188         }
1189
1190         RETURN(rc);
1191 }
1192
1193 /* Find the max stripecount we should use */
1194 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1195                                __u16 stripe_count)
1196 {
1197         __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1198
1199         if (!stripe_count)
1200                 stripe_count = lod->lod_desc.ld_default_stripe_count;
1201         if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1202                 stripe_count = lod->lod_desc.ld_active_tgt_count;
1203         if (!stripe_count)
1204                 stripe_count = 1;
1205
1206         /* stripe count is based on whether OSD can handle larger EA sizes */
1207         if (lod->lod_osd_max_easize > 0)
1208                 max_stripes = lov_mds_md_stripecnt(lod->lod_osd_max_easize,
1209                                                    magic);
1210
1211         return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1212 }
1213
1214 static int lod_use_defined_striping(const struct lu_env *env,
1215                                     struct lod_object *mo,
1216                                     const struct lu_buf *buf)
1217 {
1218         struct lov_mds_md_v1   *v1 = buf->lb_buf;
1219         struct lov_mds_md_v3   *v3 = buf->lb_buf;
1220         struct lov_ost_data_v1 *objs;
1221         __u32                   magic;
1222         int                     rc = 0;
1223         ENTRY;
1224
1225         magic = le32_to_cpu(v1->lmm_magic);
1226         if (magic == LOV_MAGIC_V1_DEF) {
1227                 magic = LOV_MAGIC_V1;
1228                 objs = &v1->lmm_objects[0];
1229         } else if (magic == LOV_MAGIC_V3_DEF) {
1230                 magic = LOV_MAGIC_V3;
1231                 objs = &v3->lmm_objects[0];
1232                 lod_object_set_pool(mo, v3->lmm_pool_name);
1233         } else {
1234                 GOTO(out, rc = -EINVAL);
1235         }
1236
1237         mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1238         mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1239         mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1240         mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1241
1242         /* fixup for released file before object initialization */
1243         if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1244                 mo->ldo_released_stripenr = mo->ldo_stripenr;
1245                 mo->ldo_stripenr = 0;
1246         }
1247
1248         LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1249
1250         if (mo->ldo_stripenr > 0)
1251                 rc = lod_initialize_objects(env, mo, objs);
1252
1253 out:
1254         RETURN(rc);
1255 }
1256
1257 static int lod_qos_parse_config(const struct lu_env *env,
1258                                 struct lod_object *lo,
1259                                 const struct lu_buf *buf)
1260 {
1261         struct lod_device     *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1262         struct lov_user_md_v1 *v1 = NULL;
1263         struct lov_user_md_v3 *v3 = NULL;
1264         struct pool_desc      *pool;
1265         __u32                  magic;
1266         int                    rc;
1267         ENTRY;
1268
1269         if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1270                 RETURN(0);
1271
1272         v1 = buf->lb_buf;
1273         magic = v1->lmm_magic;
1274
1275         if (magic == __swab32(LOV_USER_MAGIC_V1)) {
1276                 lustre_swab_lov_user_md_v1(v1);
1277                 magic = v1->lmm_magic;
1278         } else if (magic == __swab32(LOV_USER_MAGIC_V3)) {
1279                 v3 = buf->lb_buf;
1280                 lustre_swab_lov_user_md_v3(v3);
1281                 magic = v3->lmm_magic;
1282         }
1283
1284         if (unlikely(magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)) {
1285                 /* try to use as fully defined striping */
1286                 rc = lod_use_defined_striping(env, lo, buf);
1287                 RETURN(rc);
1288         }
1289
1290         if (unlikely(buf->lb_len < sizeof(*v1))) {
1291                 CERROR("wrong size: %u\n", (unsigned) buf->lb_len);
1292                 RETURN(-EINVAL);
1293         }
1294
1295         v1->lmm_magic = magic;
1296         if (v1->lmm_pattern == 0)
1297                 v1->lmm_pattern = LOV_PATTERN_RAID0;
1298         if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1299                 CERROR("invalid pattern: %x\n", v1->lmm_pattern);
1300                 RETURN(-EINVAL);
1301         }
1302         lo->ldo_pattern = v1->lmm_pattern;
1303
1304         if (v1->lmm_stripe_size)
1305                 lo->ldo_stripe_size = v1->lmm_stripe_size;
1306         if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1307                 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1308
1309         if (v1->lmm_stripe_count)
1310                 lo->ldo_stripenr = v1->lmm_stripe_count;
1311
1312         if ((v1->lmm_stripe_offset >= d->lod_desc.ld_tgt_count) &&
1313             (v1->lmm_stripe_offset != (typeof(v1->lmm_stripe_offset))(-1))) {
1314                 CERROR("invalid offset: %x\n", v1->lmm_stripe_offset);
1315                 RETURN(-EINVAL);
1316         }
1317         lo->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1318
1319         CDEBUG(D_OTHER, "lsm: %u size, %u stripes, %u offset\n",
1320                v1->lmm_stripe_size, v1->lmm_stripe_count,
1321                v1->lmm_stripe_offset);
1322
1323         if (v1->lmm_magic == LOV_MAGIC_V3) {
1324                 if (buf->lb_len < sizeof(*v3)) {
1325                         CERROR("wrong size: %u\n", (unsigned) buf->lb_len);
1326                         RETURN(-EINVAL);
1327                 }
1328
1329                 v3 = buf->lb_buf;
1330                 lod_object_set_pool(lo, v3->lmm_pool_name);
1331
1332                 /* In the function below, .hs_keycmp resolves to
1333                  * pool_hashkey_keycmp() */
1334                 /* coverity[overrun-buffer-val] */
1335                 pool = lod_find_pool(d, v3->lmm_pool_name);
1336                 if (pool != NULL) {
1337                         if (lo->ldo_def_stripe_offset !=
1338                             (typeof(v1->lmm_stripe_offset))(-1)) {
1339                                 rc = lo->ldo_def_stripe_offset;
1340                                 rc = lod_check_index_in_pool(rc, pool);
1341                                 if (rc < 0) {
1342                                         lod_pool_putref(pool);
1343                                         CERROR("invalid offset\n");
1344                                         RETURN(-EINVAL);
1345                                 }
1346                         }
1347
1348                         if (lo->ldo_stripenr > pool_tgt_count(pool))
1349                                 lo->ldo_stripenr= pool_tgt_count(pool);
1350
1351                         lod_pool_putref(pool);
1352                 }
1353         } else
1354                 lod_object_set_pool(lo, NULL);
1355
1356         /* fixup for released file */
1357         if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1358                 lo->ldo_released_stripenr = lo->ldo_stripenr;
1359                 lo->ldo_stripenr = 0;
1360         }
1361
1362         RETURN(0);
1363 }
1364
1365 /*
1366  * buf should be NULL or contain striping settings
1367  */
1368 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1369                         struct lu_attr *attr, const struct lu_buf *buf,
1370                         struct thandle *th)
1371 {
1372         struct lod_device      *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1373         struct dt_object      **stripe;
1374         int                     stripe_len;
1375         int                     flag = LOV_USES_ASSIGNED_STRIPE;
1376         int                     i, rc;
1377         ENTRY;
1378
1379         LASSERT(lo);
1380
1381         /* no OST available */
1382         /* XXX: should we be waiting a bit to prevent failures during
1383          * cluster initialization? */
1384         if (d->lod_ostnr == 0)
1385                 GOTO(out, rc = -EIO);
1386
1387         /*
1388          * by this time, the object's ldo_stripenr and ldo_stripe_size
1389          * contain default value for striping: taken from the parent
1390          * or from filesystem defaults
1391          *
1392          * in case the caller is passing lovea with new striping config,
1393          * we may need to parse lovea and apply new configuration
1394          */
1395         rc = lod_qos_parse_config(env, lo, buf);
1396         if (rc)
1397                 GOTO(out, rc);
1398
1399         /* A released file is being created */
1400         if (lo->ldo_stripenr == 0)
1401                 GOTO(out, rc = 0);
1402
1403         if (likely(lo->ldo_stripe == NULL)) {
1404                 /*
1405                  * no striping has been created so far
1406                  */
1407                 LASSERT(lo->ldo_stripenr > 0);
1408                 /*
1409                  * statfs and check OST targets now, since ld_active_tgt_count
1410                  * could be changed if some OSTs are [de]activated manually.
1411                  */
1412                 lod_qos_statfs_update(env, d);
1413                 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1414                                 lo->ldo_stripenr);
1415
1416                 stripe_len = lo->ldo_stripenr;
1417                 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1418                 if (stripe == NULL)
1419                         GOTO(out, rc = -ENOMEM);
1420
1421                 lod_getref(&d->lod_ost_descs);
1422                 /* XXX: support for non-0 files w/o objects */
1423                 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1424                                 d->lod_desc.ld_tgt_count, stripe_len);
1425                 if (lo->ldo_def_stripe_offset >= d->lod_desc.ld_tgt_count) {
1426                         rc = lod_alloc_qos(env, lo, stripe, flag, th);
1427                         if (rc == -EAGAIN)
1428                                 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1429                 } else {
1430                         rc = lod_alloc_specific(env, lo, stripe, flag, th);
1431                 }
1432                 lod_putref(d, &d->lod_ost_descs);
1433
1434                 if (rc < 0) {
1435                         for (i = 0; i < stripe_len; i++)
1436                                 if (stripe[i] != NULL)
1437                                         lu_object_put(env, &stripe[i]->do_lu);
1438
1439                         OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1440                 } else {
1441                         lo->ldo_stripe = stripe;
1442                         lo->ldo_stripes_allocated = stripe_len;
1443                 }
1444         } else {
1445                 /*
1446                  * lod_qos_parse_config() found supplied buf as a predefined
1447                  * striping (not a hint), so it allocated all the object
1448                  * now we need to create them
1449                  */
1450                 for (i = 0; i < lo->ldo_stripenr; i++) {
1451                         struct dt_object  *o;
1452
1453                         o = lo->ldo_stripe[i];
1454                         LASSERT(o);
1455
1456                         rc = dt_declare_create(env, o, attr, NULL, NULL, th);
1457                         if (rc) {
1458                                 CERROR("can't declare create: %d\n", rc);
1459                                 break;
1460                         }
1461                 }
1462         }
1463
1464 out:
1465         RETURN(rc);
1466 }
1467