Whamcloud - gitweb
LU-1303 lod: directories to inherit default striping
[fs/lustre-release.git] / lustre / lod / lod_lov.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 /*
27  * lustre/lod/lod_lov.c
28  *
29  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com> 
30  */
31
32 #ifndef EXPORT_SYMTAB
33 # define EXPORT_SYMTAB
34 #endif
35 #define DEBUG_SUBSYSTEM S_MDS
36
37 #include <obd_class.h>
38 #include <obd_lov.h>
39
40 #include "lod_internal.h"
41
42 /*
43  * Keep a refcount of lod->lod_osts usage to prevent racing with
44  * addition/deletion. Any function that expects lov_tgts to remain stationary
45  * must take a ref.
46  *
47  * \param lod - is the lod device from which we want to grab a reference
48  */
49 void lod_getref(struct lod_device *lod)
50 {
51         cfs_down_read(&lod->lod_rw_sem);
52         cfs_mutex_lock(&lod->lod_mutex);
53         lod->lod_refcount++;
54         cfs_mutex_unlock(&lod->lod_mutex);
55 }
56
57 /*
58  * Companion of lod_getref() to release a reference on the lod table.
59  * If this is the last reference and the ost entry was scheduled for deletion,
60  * the descriptor is removed from the array.
61  *
62  * \param lod - is the lod device from which we release a reference
63  */
64 void lod_putref(struct lod_device *lod)
65 {
66         cfs_mutex_lock(&lod->lod_mutex);
67         lod->lod_refcount--;
68         if (lod->lod_refcount == 0 && lod->lod_death_row) {
69                 struct lod_ost_desc *ost_desc, *tmp;
70                 int                  idx;
71                 CFS_LIST_HEAD(kill);
72
73                 CDEBUG(D_CONFIG, "destroying %d lod desc\n",
74                        lod->lod_death_row);
75
76                 cfs_foreach_bit(lod->lod_ost_bitmap, idx) {
77                         ost_desc = OST_TGT(lod, idx);
78                         LASSERT(ost_desc);
79
80                         if (!ost_desc->ltd_reap)
81                                 continue;
82
83                         cfs_list_add(&ost_desc->ltd_kill, &kill);
84
85                         lod_ost_pool_remove(&lod->lod_pool_info, idx);
86                         OST_TGT(lod, idx) = NULL;
87                         lod->lod_ostnr--;
88                         cfs_bitmap_clear(lod->lod_ost_bitmap, idx);
89                         if (ost_desc->ltd_active)
90                                 lod->lod_desc.ld_active_tgt_count--;
91                         lod->lod_death_row--;
92                 }
93                 cfs_mutex_unlock(&lod->lod_mutex);
94                 cfs_up_read(&lod->lod_rw_sem);
95
96                 cfs_list_for_each_entry_safe(ost_desc, tmp, &kill, ltd_kill) {
97                         int rc;
98                         cfs_list_del(&ost_desc->ltd_kill);
99                         /* remove from QoS structures */
100                         rc = qos_del_tgt(lod, ost_desc);
101                         if (rc)
102                                 CERROR("%s: qos_del_tgt(%s) failed: rc = %d\n",
103                                        lod2obd(lod)->obd_name,
104                                        obd_uuid2str(&ost_desc->ltd_uuid), rc);
105                         rc = obd_disconnect(ost_desc->ltd_exp);
106                         if (rc)
107                                 CERROR("%s: failed to disconnect %s: rc = %d\n",
108                                        lod2obd(lod)->obd_name,
109                                        obd_uuid2str(&ost_desc->ltd_uuid), rc);
110                         OBD_FREE_PTR(ost_desc);
111                 }
112         } else {
113                 cfs_mutex_unlock(&lod->lod_mutex);
114                 cfs_up_read(&lod->lod_rw_sem);
115         }
116 }
117
118 static int lod_bitmap_resize(struct lod_device *lod, __u32 newsize)
119 {
120         cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
121         int           rc = 0;
122         ENTRY;
123
124         /* grab write reference on the lod. Relocating the array requires
125          * exclusive access */
126         cfs_down_write(&lod->lod_rw_sem);
127
128         if (newsize <= lod->lod_osts_size)
129                 /* someone else has already resize the array */
130                 GOTO(out, rc = 0);
131
132         /* allocate new bitmap */
133         new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
134         if (!new_bitmap)
135                 GOTO(out, rc = -ENOMEM);
136
137         if (lod->lod_osts_size > 0) {
138                 /* the bitmap already exists, we need
139                  * to copy data from old one */
140                 cfs_bitmap_copy(new_bitmap, lod->lod_ost_bitmap);
141                 old_bitmap = lod->lod_ost_bitmap;
142         }
143
144         lod->lod_osts_size  = newsize;
145         lod->lod_ost_bitmap = new_bitmap;
146
147         if (old_bitmap)
148                 CFS_FREE_BITMAP(old_bitmap);
149
150         CDEBUG(D_CONFIG, "ost size: %d\n", lod->lod_osts_size);
151
152         EXIT;
153 out:
154         cfs_up_write(&lod->lod_rw_sem);
155         return rc;
156 }
157
158 /*
159  * Connect LOD to a new OSP and add it to the device table.
160  *
161  * \param env - is the environment passed by the caller
162  * \param lod - is the LOD device to be connected to the new OSP
163  * \param osp - is the name of OSP device name about to be added
164  * \param index - is the OSP index
165  * \param gen - is the generation number
166  */
167 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
168                    char *osp, unsigned index, unsigned gen, int active)
169 {
170         struct obd_connect_data *data = NULL;
171         struct obd_export       *exp = NULL;
172         struct obd_device       *obd;
173         struct lu_device        *ldev;
174         struct dt_device        *d;
175         int                      rc;
176         struct lod_ost_desc     *ost_desc;
177         struct obd_uuid          obd_uuid;
178
179         ENTRY;
180
181         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
182
183         if (gen <= 0) {
184                 CERROR("request to add OBD %s with invalid generation: %d\n",
185                        osp, gen);
186                 RETURN(-EINVAL);
187         }
188
189         obd_str2uuid(&obd_uuid, osp);
190
191         obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
192                                 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
193         if (obd == NULL) {
194                 CERROR("can't find %s device\n", osp);
195                 RETURN(-EINVAL);
196         }
197
198         OBD_ALLOC_PTR(data);
199         if (data == NULL)
200                 RETURN(-ENOMEM);
201
202         data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
203         data->ocd_version = LUSTRE_VERSION_CODE;
204         data->ocd_index = index;
205
206         rc = obd_connect(env, &exp, obd, &obd->obd_uuid, data, NULL);
207         OBD_FREE_PTR(data);
208         if (rc) {
209                 CERROR("%s: cannot connect to next dev %s (%d)\n",
210                        obd->obd_name, osp, rc);
211                 GOTO(out_free, rc);
212         }
213
214         LASSERT(obd->obd_lu_dev);
215         LASSERT(obd->obd_lu_dev->ld_site = lod->lod_dt_dev.dd_lu_dev.ld_site);
216
217         ldev = obd->obd_lu_dev;
218         d = lu2dt_dev(ldev);
219
220         /* Allocate ost descriptor and fill it */
221         OBD_ALLOC_PTR(ost_desc);
222         if (!ost_desc)
223                 GOTO(out_conn, rc = -ENOMEM);
224
225         ost_desc->ltd_ost    = d;
226         ost_desc->ltd_exp    = exp;
227         ost_desc->ltd_uuid   = obd->u.cli.cl_target_uuid;
228         ost_desc->ltd_gen    = gen;
229         ost_desc->ltd_index  = index;
230         ost_desc->ltd_active = active;
231
232         lod_getref(lod);
233         if (index >= lod->lod_osts_size) {
234                 /* we have to increase the size of the lod_osts array */
235                 __u32  newsize;
236
237                 newsize = max(lod->lod_osts_size, (__u32)2);
238                 while (newsize < index + 1)
239                         newsize = newsize << 1;
240
241                 /* lod_bitmap_resize() needs lod_rw_sem
242                  * which we hold with th reference */
243                 lod_putref(lod);
244
245                 rc = lod_bitmap_resize(lod, newsize);
246                 if (rc)
247                         GOTO(out_desc, rc);
248
249                 lod_getref(lod);
250         }
251
252         cfs_mutex_lock(&lod->lod_mutex);
253         if (cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
254                 CERROR("%s: device %d is registered already\n", obd->obd_name,
255                        index);
256                 GOTO(out_mutex, rc = -EEXIST);
257         }
258
259         if (lod->lod_ost_idx[index / OST_PTRS_PER_BLOCK] == NULL) {
260                 OBD_ALLOC_PTR(lod->lod_ost_idx[index / OST_PTRS_PER_BLOCK]);
261                 if (lod->lod_ost_idx[index / OST_PTRS_PER_BLOCK] == NULL) {
262                         CERROR("can't allocate index to add %s\n",
263                                obd->obd_name);
264                         GOTO(out_mutex, rc = -ENOMEM);
265                 }
266         }
267
268         rc = lod_ost_pool_add(&lod->lod_pool_info, index, lod->lod_osts_size);
269         if (rc) {
270                 CERROR("%s: can't set up pool, failed with %d\n",
271                        obd->obd_name, rc);
272                 GOTO(out_mutex, rc);
273         }
274
275         rc = qos_add_tgt(lod, ost_desc);
276         if (rc) {
277                 CERROR("%s: qos_add_tgt(%s) failed: rc = %d\n", obd->obd_name,
278                        obd_uuid2str(&ost_desc->ltd_uuid), rc);
279                 GOTO(out_pool, rc);
280         }
281
282         /* The new OST is now a full citizen */
283         if (index >= lod->lod_desc.ld_tgt_count)
284                 lod->lod_desc.ld_tgt_count = index + 1;
285         if (active)
286                 lod->lod_desc.ld_active_tgt_count++;
287         OST_TGT(lod, index) = ost_desc;
288         cfs_bitmap_set(lod->lod_ost_bitmap, index);
289         lod->lod_ostnr++;
290         cfs_mutex_unlock(&lod->lod_mutex);
291         lod_putref(lod);
292
293         if (lod->lod_recovery_completed)
294                 ldev->ld_ops->ldo_recovery_complete(env, ldev);
295
296         RETURN(0);
297
298 out_pool:
299         lod_ost_pool_remove(&lod->lod_pool_info, index);
300 out_mutex:
301         cfs_mutex_unlock(&lod->lod_mutex);
302         lod_putref(lod);
303 out_desc:
304         OBD_FREE_PTR(ost_desc);
305 out_conn:
306         obd_disconnect(exp);
307 out_free:
308         return rc;
309 }
310
311 /*
312  * helper function to schedule OST removal from the device table
313  */
314 static void __lod_del_device(struct lod_device *lod, unsigned idx)
315 {
316         LASSERT(OST_TGT(lod,idx));
317         if (OST_TGT(lod,idx)->ltd_reap == 0) {
318                 OST_TGT(lod,idx)->ltd_reap = 1;
319                 lod->lod_death_row++;
320         }
321 }
322
323 /*
324  * Add support for administratively disabled OST (through the MGS).
325  * Schedule a target for deletion.  Disconnection and real removal from the
326  * table takes place in lod_putref() once the last table user release its
327  * reference.
328  *
329  * \param env - is the environment passed by the caller
330  * \param lod - is the lod device currently connected to the OSP about to be
331  *              removed
332  * \param osp - is the name of OSP device about to be removed
333  * \param idx - is the OSP index
334  * \param gen - is the generation number, not used currently
335  */
336 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
337                    char *osp, unsigned idx, unsigned gen)
338 {
339         struct obd_device *obd;
340         int                rc = 0;
341         struct obd_uuid    uuid;
342         ENTRY;
343
344         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
345
346         obd = class_name2obd(osp);
347         if (obd == NULL) {
348                 CERROR("can't find %s device\n", osp);
349                 RETURN(-EINVAL);
350         }
351
352         if (gen <= 0) {
353                 CERROR("%s: request to remove OBD %s with invalid generation %d"
354                        "\n", obd->obd_name, osp, gen);
355                 RETURN(-EINVAL);
356         }
357
358         obd_str2uuid(&uuid,  osp);
359
360         lod_getref(lod);
361         cfs_mutex_lock(&lod->lod_mutex);
362         /* check that the index is allocated in the bitmap */
363         if (!cfs_bitmap_check(lod->lod_ost_bitmap, idx) || !OST_TGT(lod,idx)) {
364                 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
365                 GOTO(out, rc = -EINVAL);
366         }
367
368         /* check that the UUID matches */
369         if (!obd_uuid_equals(&uuid, &OST_TGT(lod,idx)->ltd_uuid)) {
370                 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
371                        obd->obd_name, obd_uuid2str(&OST_TGT(lod,idx)->ltd_uuid),
372                        idx, osp);
373                 GOTO(out, rc = -EINVAL);
374         }
375
376         __lod_del_device(lod, idx);
377         EXIT;
378 out:
379         cfs_mutex_unlock(&lod->lod_mutex);
380         lod_putref(lod);
381         return(rc);
382 }
383
384 int lod_ea_store_resize(struct lod_thread_info *info, int size)
385 {
386         int round = size_roundup_power2(size);
387
388         LASSERT(round <= lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
389         if (info->lti_ea_store) {
390                 LASSERT(info->lti_ea_store_size);
391                 LASSERT(info->lti_ea_store_size < round);
392                 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
393                        info->lti_ea_store_size, round);
394                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
395                 info->lti_ea_store = NULL;
396                 info->lti_ea_store_size = 0;
397         }
398
399         OBD_ALLOC_LARGE(info->lti_ea_store, round);
400         if (info->lti_ea_store == NULL)
401                 RETURN(-ENOMEM);
402         info->lti_ea_store_size = round;
403         RETURN(0);
404 }
405
406 int lod_get_lov_ea(const struct lu_env *env, struct lod_object *lo)
407 {
408         struct lod_thread_info *info = lod_env_info(env);
409         struct dt_object       *next = dt_object_child(&lo->ldo_obj);
410         int                     rc;
411         ENTRY;
412
413         LASSERT(info);
414
415         if (unlikely(info->lti_ea_store_size == 0)) {
416                 /* just to enter in allocation block below */
417                 rc = -ERANGE;
418         } else {
419 repeat:
420                 info->lti_buf.lb_buf = info->lti_ea_store;
421                 info->lti_buf.lb_len = info->lti_ea_store_size;
422                 rc = dt_xattr_get(env, next, &info->lti_buf, XATTR_NAME_LOV,
423                                   BYPASS_CAPA);
424         }
425         /* if object is not striped or inaccessible */
426         if (rc == -ENODATA)
427                 RETURN(0);
428
429         if (rc == -ERANGE) {
430                 /* EA doesn't fit, reallocate new buffer */
431                 rc = dt_xattr_get(env, next, &LU_BUF_NULL, XATTR_NAME_LOV,
432                                   BYPASS_CAPA);
433                 if (rc == -ENODATA)
434                         RETURN(0);
435                 else if (rc < 0)
436                         RETURN(rc);
437
438                 LASSERT(rc > 0);
439                 rc = lod_ea_store_resize(info, rc);
440                 if (rc)
441                         RETURN(rc);
442                 goto repeat;
443         }
444
445         RETURN(rc);
446 }
447
448 int lod_store_def_striping(const struct lu_env *env, struct dt_object *dt,
449                            struct thandle *th)
450 {
451         struct lod_thread_info  *info = lod_env_info(env);
452         struct lod_object       *lo = lod_dt_obj(dt);
453         struct dt_object        *next = dt_object_child(dt);
454         struct lov_user_md_v3   *v3;
455         int                      rc;
456         ENTRY;
457
458         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
459
460         /*
461          * store striping defaults into new directory
462          * used to implement defaults inheritance
463          */
464
465         /* probably nothing to inherite */
466         if (lo->ldo_striping_cached == 0)
467                 RETURN(0);
468
469         if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size, lo->ldo_def_stripenr,
470                                 lo->ldo_def_stripe_offset))
471                 RETURN(0);
472
473         /* XXX: use thread info */
474         OBD_ALLOC_PTR(v3);
475         if (v3 == NULL)
476                 RETURN(-ENOMEM);
477
478         v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
479         v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
480         v3->lmm_object_id = 0;
481         v3->lmm_object_seq = 0;
482         v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
483         v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
484         v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
485         if (lo->ldo_pool)
486                 strncpy(v3->lmm_pool_name, lo->ldo_pool, LOV_MAXPOOLNAME);
487
488         info->lti_buf.lb_buf = v3;
489         info->lti_buf.lb_len = sizeof(*v3);
490         rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0, th,
491                         BYPASS_CAPA);
492
493         OBD_FREE_PTR(v3);
494
495         RETURN(rc);
496 }
497
498 /*
499  * allocate array of objects pointers, find/create objects
500  * stripenr and other fields should be initialized by this moment
501  */
502 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
503                            struct lov_ost_data_v1 *objs)
504 {
505         struct lod_thread_info  *info = lod_env_info(env);
506         struct lod_device       *md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
507         struct lu_object        *o, *n;
508         struct lu_device        *nd;
509         int                     i, idx, rc = 0;
510         ENTRY;
511
512         LASSERT(lo);
513         LASSERT(lo->ldo_stripe == NULL);
514         LASSERT(lo->ldo_stripenr > 0);
515         LASSERT(lo->ldo_stripe_size > 0);
516
517         i = sizeof(struct dt_object *) * lo->ldo_stripenr;
518         OBD_ALLOC(lo->ldo_stripe, i);
519         if (lo->ldo_stripe == NULL)
520                 GOTO(out, rc = -ENOMEM);
521         lo->ldo_stripes_allocated = lo->ldo_stripenr;
522
523         for (i = 0; i < lo->ldo_stripenr; i++) {
524
525                 info->lti_ostid.oi_id = le64_to_cpu(objs[i].l_object_id);
526                 /* XXX: support for DNE? */
527                 info->lti_ostid.oi_seq = le64_to_cpu(objs[i].l_object_seq);
528                 idx = le64_to_cpu(objs[i].l_ost_idx);
529                 fid_ostid_unpack(&info->lti_fid, &info->lti_ostid, idx);
530
531                 /*
532                  * XXX: assertion is left for testing, to make
533                  * sure we never process requests till configuration
534                  * is completed. to be changed to -EINVAL
535                  */
536
537                 lod_getref(md);
538                 LASSERT(cfs_bitmap_check(md->lod_ost_bitmap, idx));
539                 LASSERT(OST_TGT(md,idx));
540                 LASSERTF(OST_TGT(md,idx)->ltd_ost, "idx %d\n", idx);
541                 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
542                 lod_putref(md);
543
544                 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
545                 if (IS_ERR(o))
546                         GOTO(out, rc = PTR_ERR(o));
547
548                 n = lu_object_locate(o->lo_header, nd->ld_type);
549                 LASSERT(n);
550
551                 lo->ldo_stripe[i] = container_of(n, struct dt_object, do_lu);
552         }
553
554 out:
555         RETURN(rc);
556 }
557
558 /*
559  * Parse striping information stored in lti_ea_store
560  */
561 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
562                        const struct lu_buf *buf)
563 {
564         struct lov_mds_md_v1    *lmm;
565         struct lov_ost_data_v1  *objs;
566         __u32                    magic;
567         int                      rc = 0;
568         ENTRY;
569
570         LASSERT(buf);
571         LASSERT(buf->lb_buf);
572         LASSERT(buf->lb_len);
573
574         lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
575         magic = le32_to_cpu(lmm->lmm_magic);
576
577         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
578                 GOTO(out, rc = -EINVAL);
579         if (le32_to_cpu(lmm->lmm_pattern) != LOV_PATTERN_RAID0)
580                 GOTO(out, rc = -EINVAL);
581
582         lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
583         lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
584         lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
585
586         LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
587
588         if (magic == LOV_MAGIC_V3) {
589                 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
590                 objs = &v3->lmm_objects[0];
591                 lod_object_set_pool(lo, v3->lmm_pool_name);
592         } else {
593                 objs = &lmm->lmm_objects[0];
594         }
595
596         rc = lod_initialize_objects(env, lo, objs);
597
598 out:
599         RETURN(rc);
600 }
601
602 /*
603  * Load and parse striping information, create in-core representation for the
604  * stripes
605  */
606 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
607 {
608         struct lod_thread_info  *info = lod_env_info(env);
609         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
610         int                      rc;
611         ENTRY;
612
613         /*
614          * currently this code is supposed to be called from declaration
615          * phase only, thus the object is not expected to be locked by caller
616          */
617         dt_write_lock(env, next, 0);
618         /* already initialized? */
619         if (lo->ldo_stripe) {
620                 int i;
621                 /* check validity */
622                 for (i = 0; i < lo->ldo_stripenr; i++)
623                         LASSERTF(lo->ldo_stripe[i], "stripe %d is NULL\n", i);
624                 GOTO(out, rc = 0);
625         }
626
627         if (!dt_object_exists(next))
628                 GOTO(out, rc = 0);
629
630         /* only regular files can be striped */
631         if (!(lu_object_attr(lod2lu_obj(lo)) & S_IFREG))
632                 GOTO(out, rc = 0);
633
634         LASSERT(lo->ldo_stripenr == 0);
635
636         rc = lod_get_lov_ea(env, lo);
637         if (rc <= 0)
638                 GOTO(out, rc);
639
640         /*
641          * there is LOV EA (striping information) in this object
642          * let's parse it and create in-core objects for the stripes
643          */
644         info->lti_buf.lb_buf = info->lti_ea_store;
645         info->lti_buf.lb_len = info->lti_ea_store_size;
646         rc = lod_parse_striping(env, lo, &info->lti_buf);
647 out:
648         dt_write_unlock(env, next);
649         RETURN(rc);
650 }
651
652 int lod_verify_striping(struct lod_device *d, const struct lu_buf *buf,
653                         int specific)
654 {
655         struct lov_user_md_v1   *lum;
656         struct lov_user_md_v3   *v3 = NULL;
657         struct pool_desc        *pool = NULL;
658         int                      rc;
659         ENTRY;
660
661         lum = buf->lb_buf;
662
663         if (lum->lmm_magic != LOV_USER_MAGIC_V1 &&
664             lum->lmm_magic != LOV_USER_MAGIC_V3 &&
665             lum->lmm_magic != LOV_MAGIC_V1_DEF &&
666             lum->lmm_magic != LOV_MAGIC_V3_DEF) {
667                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#x\n",
668                        lum->lmm_magic);
669                 RETURN(-EINVAL);
670         }
671
672         if ((specific && lum->lmm_pattern != LOV_PATTERN_RAID0) ||
673             (specific == 0 && lum->lmm_pattern != 0)) {
674                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
675                        lum->lmm_pattern);
676                 RETURN(-EINVAL);
677         }
678
679         /* 64kB is the largest common page size we see (ia64), and matches the
680          * check in lfs */
681         if (lum->lmm_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
682                 CDEBUG(D_IOCTL, "stripe size %u not multiple of %u, fixing\n",
683                        lum->lmm_stripe_size, LOV_MIN_STRIPE_SIZE);
684                 RETURN(-EINVAL);
685         }
686
687         /* an offset of -1 is treated as a "special" valid offset */
688         if (lum->lmm_stripe_offset != (typeof(lum->lmm_stripe_offset))(-1)) {
689                 /* if offset is not within valid range [0, osts_size) */
690                 if (lum->lmm_stripe_offset >= d->lod_osts_size) {
691                         CDEBUG(D_IOCTL, "stripe offset %u >= bitmap size %u\n",
692                                lum->lmm_stripe_offset, d->lod_osts_size);
693                         RETURN(-EINVAL);
694                 }
695
696                 /* if lmm_stripe_offset is *not* in bitmap */
697                 if (!cfs_bitmap_check(d->lod_ost_bitmap,
698                                       lum->lmm_stripe_offset)) {
699                         CDEBUG(D_IOCTL, "stripe offset %u not in bitmap\n",
700                                lum->lmm_stripe_offset);
701                         RETURN(-EINVAL);
702                 }
703         }
704
705         if (lum->lmm_magic == LOV_USER_MAGIC_V3)
706                 v3 = buf->lb_buf;
707
708         if (v3)
709                 pool = lod_find_pool(d, v3->lmm_pool_name);
710
711         if (pool != NULL) {
712                 __u16 offs = v3->lmm_stripe_offset;
713
714                 if (offs != (typeof(v3->lmm_stripe_offset))(-1)) {
715                         rc = lod_check_index_in_pool(offs, pool);
716                         if (rc < 0) {
717                                 lod_pool_putref(pool);
718                                 RETURN(-EINVAL);
719                         }
720                 }
721
722                 if (specific && lum->lmm_stripe_count > pool_tgt_count(pool)) {
723                         CDEBUG(D_IOCTL,
724                                "stripe count %u > # OSTs %u in the pool\n",
725                                lum->lmm_stripe_count, pool_tgt_count(pool));
726                         lod_pool_putref(pool);
727                         RETURN(-EINVAL);
728                 }
729
730                 lod_pool_putref(pool);
731         }
732
733         RETURN(0);
734 }
735
736 void lod_fix_desc_stripe_size(__u64 *val)
737 {
738         if (*val < PTLRPC_MAX_BRW_SIZE) {
739                 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
740                               PTLRPC_MAX_BRW_SIZE);
741                 *val = PTLRPC_MAX_BRW_SIZE;
742         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
743                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
744                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
745                               "multiple of %u)\n",
746                               *val, LOV_MIN_STRIPE_SIZE);
747         }
748 }
749
750 void lod_fix_desc_stripe_count(__u32 *val)
751 {
752         if (*val == 0)
753                 *val = 1;
754 }
755
756 void lod_fix_desc_pattern(__u32 *val)
757 {
758         /* from lov_setstripe */
759         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
760                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
761                 *val = 0;
762         }
763 }
764
765 void lod_fix_desc_qos_maxage(__u32 *val)
766 {
767         /* fix qos_maxage */
768         if (*val == 0)
769                 *val = QOS_DEFAULT_MAXAGE;
770 }
771
772 void lod_fix_desc(struct lov_desc *desc)
773 {
774         lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
775         lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
776         lod_fix_desc_pattern(&desc->ld_pattern);
777         lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
778 }
779
780 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
781 {
782         struct lprocfs_static_vars  lvars = { 0 };
783         struct obd_device          *obd;
784         struct lov_desc            *desc;
785         int                         rc;
786         ENTRY;
787
788         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
789         LASSERT(obd != NULL);
790         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
791
792         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
793                 CERROR("LOD setup requires a descriptor\n");
794                 RETURN(-EINVAL);
795         }
796
797         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
798
799         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
800                 CERROR("descriptor size wrong: %d > %d\n",
801                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
802                 RETURN(-EINVAL);
803         }
804
805         if (desc->ld_magic != LOV_DESC_MAGIC) {
806                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
807                         CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
808                                obd->obd_name, desc);
809                         lustre_swab_lov_desc(desc);
810                 } else {
811                         CERROR("%s: Bad lov desc magic: %#x\n",
812                                obd->obd_name, desc->ld_magic);
813                         RETURN(-EINVAL);
814                 }
815         }
816
817         lod_fix_desc(desc);
818
819         desc->ld_active_tgt_count = 0;
820         lod->lod_desc = *desc;
821
822         lod->lod_sp_me = LUSTRE_SP_CLI;
823
824         /* Set up allocation policy (QoS and RR) */
825         CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
826         cfs_init_rwsem(&lod->lod_qos.lq_rw_sem);
827         lod->lod_qos.lq_dirty = 1;
828         lod->lod_qos.lq_rr.lqr_dirty = 1;
829         lod->lod_qos.lq_reset = 1;
830         /* Default priority is toward free space balance */
831         lod->lod_qos.lq_prio_free = 232;
832         /* Default threshold for rr (roughly 17%) */
833         lod->lod_qos.lq_threshold_rr = 43;
834         /* Init statfs fields */
835         OBD_ALLOC_PTR(lod->lod_qos.lq_statfs_data);
836         if (NULL == lod->lod_qos.lq_statfs_data)
837                 RETURN(-ENOMEM);
838         cfs_waitq_init(&lod->lod_qos.lq_statfs_waitq);
839
840         /* Set up OST pool environment */
841         lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
842                                                    HASH_POOLS_MAX_BITS,
843                                                    HASH_POOLS_BKT_BITS, 0,
844                                                    CFS_HASH_MIN_THETA,
845                                                    CFS_HASH_MAX_THETA,
846                                                    &pool_hash_operations,
847                                                    CFS_HASH_DEFAULT);
848         if (!lod->lod_pools_hash_body)
849                 GOTO(out_statfs, rc = -ENOMEM);
850         CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
851         lod->lod_pool_count = 0;
852         rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
853         if (rc)
854                 GOTO(out_hash, rc);
855         rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
856         if (rc)
857                 GOTO(out_pool_info, rc);
858
859         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
860          * added to the LOD, see lod_add_device() */
861         lod->lod_ost_bitmap = NULL;
862         lod->lod_osts_size  = 0;
863         lod->lod_ostnr      = 0;
864
865         lod->lod_death_row = 0;
866         lod->lod_refcount  = 0;
867
868         lprocfs_lod_init_vars(&lvars);
869         lprocfs_obd_setup(obd, lvars.obd_vars);
870
871 #ifdef LPROCFS
872         rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
873                                 0444, &lod_proc_target_fops, obd);
874         if (rc) {
875                 CWARN("%s: Error adding the target_obd file %d\n",
876                       obd->obd_name, rc);
877                 GOTO(out_lproc, rc);
878         }
879         lod->lod_pool_proc_entry = lprocfs_register("pools",
880                                                     obd->obd_proc_entry,
881                                                     NULL, NULL);
882         if (IS_ERR(lod->lod_pool_proc_entry)) {
883                 int ret = PTR_ERR(lod->lod_pool_proc_entry);
884                 lod->lod_pool_proc_entry = NULL;
885                 CWARN("%s: Failed to create pool proc file %d\n",
886                       obd->obd_name, ret);
887                 rc = lod_pools_fini(lod);
888                 RETURN(ret);
889         }
890 #endif
891
892         RETURN(0);
893
894 out_lproc:
895         lprocfs_obd_cleanup(obd);
896         lod_ost_pool_free(&lod->lod_qos.lq_rr.lqr_pool);
897 out_pool_info:
898         lod_ost_pool_free(&lod->lod_pool_info);
899 out_hash:
900         cfs_hash_putref(lod->lod_pools_hash_body);
901 out_statfs:
902         OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
903         return rc;
904 }
905
906 int lod_pools_fini(struct lod_device *lod)
907 {
908         struct obd_device   *obd = lod2obd(lod);
909         cfs_list_t          *pos, *tmp;
910         struct pool_desc    *pool;
911         ENTRY;
912
913         cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
914                 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
915                 /* free pool structs */
916                 CDEBUG(D_INFO, "delete pool %p\n", pool);
917                 lod_pool_del(obd, pool->pool_name);
918         }
919
920         if (lod->lod_osts_size > 0) {
921                 int idx;
922                 lod_getref(lod);
923                 cfs_mutex_lock(&lod->lod_mutex);
924                 cfs_foreach_bit(lod->lod_ost_bitmap, idx)
925                         __lod_del_device(lod, idx);
926                 cfs_mutex_unlock(&lod->lod_mutex);
927                 lod_putref(lod);
928                 CFS_FREE_BITMAP(lod->lod_ost_bitmap);
929                 for (idx = 0; idx < OST_PTRS; idx++) {
930                         if (lod->lod_ost_idx[idx])
931                                 OBD_FREE_PTR(lod->lod_ost_idx[idx]);
932                 }
933                 lod->lod_osts_size = 0;
934         }
935
936         cfs_hash_putref(lod->lod_pools_hash_body);
937         lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
938         lod_ost_pool_free(&lod->lod_pool_info);
939
940         /* clear pools parent proc entry only after all pools are killed */
941         if (lod->lod_pool_proc_entry) {
942                 lprocfs_remove(&lod->lod_pool_proc_entry);
943                 lod->lod_pool_proc_entry = NULL;
944         }
945
946         lprocfs_obd_cleanup(obd);
947
948         OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
949         RETURN(0);
950 }
951