Whamcloud - gitweb
LU-1303 lod: destroy and setattr to use 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                         /* XXX: remove from QoS structures */
100                         /* disconnect from OSP */
101                         rc = obd_disconnect(ost_desc->ltd_exp);
102                         if (rc)
103                                 CERROR("%s: failed to disconnect %s (%d)\n",
104                                        lod2obd(lod)->obd_name,
105                                        obd_uuid2str(&ost_desc->ltd_uuid), rc);
106                         OBD_FREE_PTR(ost_desc);
107                 }
108         } else {
109                 cfs_mutex_unlock(&lod->lod_mutex);
110                 cfs_up_read(&lod->lod_rw_sem);
111         }
112 }
113
114 static int lod_bitmap_resize(struct lod_device *lod, __u32 newsize)
115 {
116         cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
117         int           rc = 0;
118         ENTRY;
119
120         /* grab write reference on the lod. Relocating the array requires
121          * exclusive access */
122         cfs_down_write(&lod->lod_rw_sem);
123
124         if (newsize <= lod->lod_osts_size)
125                 /* someone else has already resize the array */
126                 GOTO(out, rc = 0);
127
128         /* allocate new bitmap */
129         new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
130         if (!new_bitmap)
131                 GOTO(out, rc = -ENOMEM);
132
133         if (lod->lod_osts_size > 0) {
134                 /* the bitmap already exists, we need
135                  * to copy data from old one */
136                 cfs_bitmap_copy(new_bitmap, lod->lod_ost_bitmap);
137                 old_bitmap = lod->lod_ost_bitmap;
138         }
139
140         lod->lod_osts_size  = newsize;
141         lod->lod_ost_bitmap = new_bitmap;
142
143         if (old_bitmap)
144                 CFS_FREE_BITMAP(old_bitmap);
145
146         CDEBUG(D_CONFIG, "ost size: %d\n", lod->lod_osts_size);
147
148         EXIT;
149 out:
150         cfs_up_write(&lod->lod_rw_sem);
151         return rc;
152 }
153
154 /*
155  * Connect LOD to a new OSP and add it to the device table.
156  *
157  * \param env - is the environment passed by the caller
158  * \param lod - is the LOD device to be connected to the new OSP
159  * \param osp - is the name of OSP device name about to be added
160  * \param index - is the OSP index
161  * \param gen - is the generation number
162  */
163 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
164                    char *osp, unsigned index, unsigned gen, int active)
165 {
166         struct obd_connect_data *data = NULL;
167         struct obd_export       *exp = NULL;
168         struct obd_device       *obd;
169         struct lu_device        *ldev;
170         struct dt_device        *d;
171         int                      rc;
172         struct lod_ost_desc     *ost_desc;
173         struct obd_uuid          obd_uuid;
174
175         ENTRY;
176
177         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
178
179         if (gen <= 0) {
180                 CERROR("request to add OBD %s with invalid generation: %d\n",
181                        osp, gen);
182                 RETURN(-EINVAL);
183         }
184
185         obd_str2uuid(&obd_uuid, osp);
186
187         obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
188                                 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
189         if (obd == NULL) {
190                 CERROR("can't find %s device\n", osp);
191                 RETURN(-EINVAL);
192         }
193
194         OBD_ALLOC_PTR(data);
195         if (data == NULL)
196                 RETURN(-ENOMEM);
197
198         data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
199         data->ocd_version = LUSTRE_VERSION_CODE;
200         data->ocd_index = index;
201
202         rc = obd_connect(env, &exp, obd, &obd->obd_uuid, data, NULL);
203         OBD_FREE_PTR(data);
204         if (rc) {
205                 CERROR("%s: cannot connect to next dev %s (%d)\n",
206                        obd->obd_name, osp, rc);
207                 GOTO(out_free, rc);
208         }
209
210         LASSERT(obd->obd_lu_dev);
211         LASSERT(obd->obd_lu_dev->ld_site = lod->lod_dt_dev.dd_lu_dev.ld_site);
212
213         ldev = obd->obd_lu_dev;
214         d = lu2dt_dev(ldev);
215
216         /* Allocate ost descriptor and fill it */
217         OBD_ALLOC_PTR(ost_desc);
218         if (!ost_desc)
219                 GOTO(out_conn, rc = -ENOMEM);
220
221         ost_desc->ltd_ost    = d;
222         ost_desc->ltd_exp    = exp;
223         ost_desc->ltd_uuid   = obd->u.cli.cl_target_uuid;
224         ost_desc->ltd_gen    = gen;
225         ost_desc->ltd_index  = index;
226         ost_desc->ltd_active = active;
227
228         lod_getref(lod);
229         if (index >= lod->lod_osts_size) {
230                 /* we have to increase the size of the lod_osts array */
231                 __u32  newsize;
232
233                 newsize = max(lod->lod_osts_size, (__u32)2);
234                 while (newsize < index + 1)
235                         newsize = newsize << 1;
236
237                 /* lod_bitmap_resize() needs lod_rw_sem
238                  * which we hold with th reference */
239                 lod_putref(lod);
240
241                 rc = lod_bitmap_resize(lod, newsize);
242                 if (rc)
243                         GOTO(out_desc, rc);
244
245                 lod_getref(lod);
246         }
247
248         cfs_mutex_lock(&lod->lod_mutex);
249         if (cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
250                 CERROR("%s: device %d is registered already\n", obd->obd_name,
251                        index);
252                 GOTO(out_mutex, rc = -EEXIST);
253         }
254
255         if (lod->lod_ost_idx[index / OST_PTRS_PER_BLOCK] == NULL) {
256                 OBD_ALLOC_PTR(lod->lod_ost_idx[index / OST_PTRS_PER_BLOCK]);
257                 if (lod->lod_ost_idx[index / OST_PTRS_PER_BLOCK] == NULL) {
258                         CERROR("can't allocate index to add %s\n",
259                                obd->obd_name);
260                         GOTO(out_mutex, rc = -ENOMEM);
261                 }
262         }
263
264         rc = lod_ost_pool_add(&lod->lod_pool_info, index, lod->lod_osts_size);
265         if (rc) {
266                 CERROR("%s: can't set up pool, failed with %d\n",
267                        obd->obd_name, rc);
268                 GOTO(out_mutex, rc);
269         }
270
271         /* The new OST is now a full citizen */
272         if (index >= lod->lod_desc.ld_tgt_count)
273                 lod->lod_desc.ld_tgt_count = index + 1;
274         if (active)
275                 lod->lod_desc.ld_active_tgt_count++;
276         OST_TGT(lod, index) = ost_desc;
277         cfs_bitmap_set(lod->lod_ost_bitmap, index);
278         lod->lod_ostnr++;
279         cfs_mutex_unlock(&lod->lod_mutex);
280         lod_putref(lod);
281
282         if (lod->lod_recovery_completed)
283                 ldev->ld_ops->ldo_recovery_complete(env, ldev);
284
285         RETURN(0);
286
287         lod_ost_pool_remove(&lod->lod_pool_info, index);
288 out_mutex:
289         cfs_mutex_unlock(&lod->lod_mutex);
290         lod_putref(lod);
291 out_desc:
292         OBD_FREE_PTR(ost_desc);
293 out_conn:
294         obd_disconnect(exp);
295 out_free:
296         return rc;
297 }
298
299 /*
300  * helper function to schedule OST removal from the device table
301  */
302 static void __lod_del_device(struct lod_device *lod, unsigned idx)
303 {
304         LASSERT(OST_TGT(lod,idx));
305         if (OST_TGT(lod,idx)->ltd_reap == 0) {
306                 OST_TGT(lod,idx)->ltd_reap = 1;
307                 lod->lod_death_row++;
308         }
309 }
310
311 /*
312  * Add support for administratively disabled OST (through the MGS).
313  * Schedule a target for deletion.  Disconnection and real removal from the
314  * table takes place in lod_putref() once the last table user release its
315  * reference.
316  *
317  * \param env - is the environment passed by the caller
318  * \param lod - is the lod device currently connected to the OSP about to be
319  *              removed
320  * \param osp - is the name of OSP device about to be removed
321  * \param idx - is the OSP index
322  * \param gen - is the generation number, not used currently
323  */
324 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
325                    char *osp, unsigned idx, unsigned gen)
326 {
327         struct obd_device *obd;
328         int                rc = 0;
329         struct obd_uuid    uuid;
330         ENTRY;
331
332         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
333
334         obd = class_name2obd(osp);
335         if (obd == NULL) {
336                 CERROR("can't find %s device\n", osp);
337                 RETURN(-EINVAL);
338         }
339
340         if (gen <= 0) {
341                 CERROR("%s: request to remove OBD %s with invalid generation %d"
342                        "\n", obd->obd_name, osp, gen);
343                 RETURN(-EINVAL);
344         }
345
346         obd_str2uuid(&uuid,  osp);
347
348         lod_getref(lod);
349         cfs_mutex_lock(&lod->lod_mutex);
350         /* check that the index is allocated in the bitmap */
351         if (!cfs_bitmap_check(lod->lod_ost_bitmap, idx) || !OST_TGT(lod,idx)) {
352                 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
353                 GOTO(out, rc = -EINVAL);
354         }
355
356         /* check that the UUID matches */
357         if (!obd_uuid_equals(&uuid, &OST_TGT(lod,idx)->ltd_uuid)) {
358                 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
359                        obd->obd_name, obd_uuid2str(&OST_TGT(lod,idx)->ltd_uuid),
360                        idx, osp);
361                 GOTO(out, rc = -EINVAL);
362         }
363
364         __lod_del_device(lod, idx);
365         EXIT;
366 out:
367         cfs_mutex_unlock(&lod->lod_mutex);
368         lod_putref(lod);
369         return(rc);
370 }
371
372 int lod_ea_store_resize(struct lod_thread_info *info, int size)
373 {
374         int round = size_roundup_power2(size);
375
376         LASSERT(round <= lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
377         if (info->lti_ea_store) {
378                 LASSERT(info->lti_ea_store_size);
379                 LASSERT(info->lti_ea_store_size < round);
380                 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
381                        info->lti_ea_store_size, round);
382                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
383                 info->lti_ea_store = NULL;
384                 info->lti_ea_store_size = 0;
385         }
386
387         OBD_ALLOC_LARGE(info->lti_ea_store, round);
388         if (info->lti_ea_store == NULL)
389                 RETURN(-ENOMEM);
390         info->lti_ea_store_size = round;
391         RETURN(0);
392 }
393
394 int lod_get_lov_ea(const struct lu_env *env, struct lod_object *lo)
395 {
396         struct lod_thread_info *info = lod_env_info(env);
397         struct dt_object       *next = dt_object_child(&lo->ldo_obj);
398         int                     rc;
399         ENTRY;
400
401         LASSERT(info);
402
403         if (unlikely(info->lti_ea_store_size == 0)) {
404                 /* just to enter in allocation block below */
405                 rc = -ERANGE;
406         } else {
407 repeat:
408                 info->lti_buf.lb_buf = info->lti_ea_store;
409                 info->lti_buf.lb_len = info->lti_ea_store_size;
410                 rc = dt_xattr_get(env, next, &info->lti_buf, XATTR_NAME_LOV,
411                                   BYPASS_CAPA);
412         }
413         /* if object is not striped or inaccessible */
414         if (rc == -ENODATA)
415                 RETURN(0);
416
417         if (rc == -ERANGE) {
418                 /* EA doesn't fit, reallocate new buffer */
419                 rc = dt_xattr_get(env, next, &LU_BUF_NULL, XATTR_NAME_LOV,
420                                   BYPASS_CAPA);
421                 if (rc == -ENODATA)
422                         RETURN(0);
423                 else if (rc < 0)
424                         RETURN(rc);
425
426                 LASSERT(rc > 0);
427                 rc = lod_ea_store_resize(info, rc);
428                 if (rc)
429                         RETURN(rc);
430                 goto repeat;
431         }
432
433         RETURN(rc);
434 }
435
436 /*
437  * allocate array of objects pointers, find/create objects
438  * stripenr and other fields should be initialized by this moment
439  */
440 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
441                            struct lov_ost_data_v1 *objs)
442 {
443         struct lod_thread_info  *info = lod_env_info(env);
444         struct lod_device       *md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
445         struct lu_object        *o, *n;
446         struct lu_device        *nd;
447         int                     i, idx, rc = 0;
448         ENTRY;
449
450         LASSERT(lo);
451         LASSERT(lo->ldo_stripe == NULL);
452         LASSERT(lo->ldo_stripenr > 0);
453         LASSERT(lo->ldo_stripe_size > 0);
454
455         i = sizeof(struct dt_object *) * lo->ldo_stripenr;
456         OBD_ALLOC(lo->ldo_stripe, i);
457         if (lo->ldo_stripe == NULL)
458                 GOTO(out, rc = -ENOMEM);
459         lo->ldo_stripes_allocated = lo->ldo_stripenr;
460
461         for (i = 0; i < lo->ldo_stripenr; i++) {
462
463                 info->lti_ostid.oi_id = le64_to_cpu(objs[i].l_object_id);
464                 /* XXX: support for DNE? */
465                 info->lti_ostid.oi_seq = le64_to_cpu(objs[i].l_object_seq);
466                 idx = le64_to_cpu(objs[i].l_ost_idx);
467                 fid_ostid_unpack(&info->lti_fid, &info->lti_ostid, idx);
468
469                 /*
470                  * XXX: assertion is left for testing, to make
471                  * sure we never process requests till configuration
472                  * is completed. to be changed to -EINVAL
473                  */
474
475                 lod_getref(md);
476                 LASSERT(cfs_bitmap_check(md->lod_ost_bitmap, idx));
477                 LASSERT(OST_TGT(md,idx));
478                 LASSERTF(OST_TGT(md,idx)->ltd_ost, "idx %d\n", idx);
479                 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
480                 lod_putref(md);
481
482                 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
483                 if (IS_ERR(o))
484                         GOTO(out, rc = PTR_ERR(o));
485
486                 n = lu_object_locate(o->lo_header, nd->ld_type);
487                 LASSERT(n);
488
489                 lo->ldo_stripe[i] = container_of(n, struct dt_object, do_lu);
490         }
491
492 out:
493         RETURN(rc);
494 }
495
496 /*
497  * Parse striping information stored in lti_ea_store
498  */
499 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
500                        const struct lu_buf *buf)
501 {
502         struct lov_mds_md_v1    *lmm;
503         struct lov_ost_data_v1  *objs;
504         __u32                    magic;
505         int                      rc = 0;
506         ENTRY;
507
508         LASSERT(buf);
509         LASSERT(buf->lb_buf);
510         LASSERT(buf->lb_len);
511
512         lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
513         magic = le32_to_cpu(lmm->lmm_magic);
514
515         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
516                 GOTO(out, rc = -EINVAL);
517         if (le32_to_cpu(lmm->lmm_pattern) != LOV_PATTERN_RAID0)
518                 GOTO(out, rc = -EINVAL);
519
520         lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
521         lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
522         lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
523
524         LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
525
526         if (magic == LOV_MAGIC_V3) {
527                 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
528                 objs = &v3->lmm_objects[0];
529                 lod_object_set_pool(lo, v3->lmm_pool_name);
530         } else {
531                 objs = &lmm->lmm_objects[0];
532         }
533
534         rc = lod_initialize_objects(env, lo, objs);
535
536 out:
537         RETURN(rc);
538 }
539
540 /*
541  * Load and parse striping information, create in-core representation for the
542  * stripes
543  */
544 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
545 {
546         struct lod_thread_info  *info = lod_env_info(env);
547         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
548         int                      rc;
549         ENTRY;
550
551         /*
552          * currently this code is supposed to be called from declaration
553          * phase only, thus the object is not expected to be locked by caller
554          */
555         dt_write_lock(env, next, 0);
556         /* already initialized? */
557         if (lo->ldo_stripe) {
558                 int i;
559                 /* check validity */
560                 for (i = 0; i < lo->ldo_stripenr; i++)
561                         LASSERTF(lo->ldo_stripe[i], "stripe %d is NULL\n", i);
562                 GOTO(out, rc = 0);
563         }
564
565         if (!dt_object_exists(next))
566                 GOTO(out, rc = 0);
567
568         /* only regular files can be striped */
569         if (!(lu_object_attr(lod2lu_obj(lo)) & S_IFREG))
570                 GOTO(out, rc = 0);
571
572         LASSERT(lo->ldo_stripenr == 0);
573
574         rc = lod_get_lov_ea(env, lo);
575         if (rc <= 0)
576                 GOTO(out, rc);
577
578         /*
579          * there is LOV EA (striping information) in this object
580          * let's parse it and create in-core objects for the stripes
581          */
582         info->lti_buf.lb_buf = info->lti_ea_store;
583         info->lti_buf.lb_len = info->lti_ea_store_size;
584         rc = lod_parse_striping(env, lo, &info->lti_buf);
585 out:
586         dt_write_unlock(env, next);
587         RETURN(rc);
588 }
589
590 void lod_fix_desc_stripe_size(__u64 *val)
591 {
592         if (*val < PTLRPC_MAX_BRW_SIZE) {
593                 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
594                               PTLRPC_MAX_BRW_SIZE);
595                 *val = PTLRPC_MAX_BRW_SIZE;
596         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
597                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
598                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
599                               "multiple of %u)\n",
600                               *val, LOV_MIN_STRIPE_SIZE);
601         }
602 }
603
604 void lod_fix_desc_stripe_count(__u32 *val)
605 {
606         if (*val == 0)
607                 *val = 1;
608 }
609
610 void lod_fix_desc_pattern(__u32 *val)
611 {
612         /* from lov_setstripe */
613         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
614                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
615                 *val = 0;
616         }
617 }
618
619 void lod_fix_desc_qos_maxage(__u32 *val)
620 {
621         /* fix qos_maxage */
622         if (*val == 0)
623                 *val = QOS_DEFAULT_MAXAGE;
624 }
625
626 void lod_fix_desc(struct lov_desc *desc)
627 {
628         lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
629         lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
630         lod_fix_desc_pattern(&desc->ld_pattern);
631         lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
632 }
633
634 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
635 {
636         struct lprocfs_static_vars  lvars = { 0 };
637         struct obd_device          *obd;
638         struct lov_desc            *desc;
639         int                         rc;
640         ENTRY;
641
642         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
643         LASSERT(obd != NULL);
644         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
645
646         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
647                 CERROR("LOD setup requires a descriptor\n");
648                 RETURN(-EINVAL);
649         }
650
651         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
652
653         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
654                 CERROR("descriptor size wrong: %d > %d\n",
655                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
656                 RETURN(-EINVAL);
657         }
658
659         if (desc->ld_magic != LOV_DESC_MAGIC) {
660                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
661                         CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
662                                obd->obd_name, desc);
663                         lustre_swab_lov_desc(desc);
664                 } else {
665                         CERROR("%s: Bad lov desc magic: %#x\n",
666                                obd->obd_name, desc->ld_magic);
667                         RETURN(-EINVAL);
668                 }
669         }
670
671         lod_fix_desc(desc);
672
673         desc->ld_active_tgt_count = 0;
674         lod->lod_desc = *desc;
675
676         lod->lod_sp_me = LUSTRE_SP_CLI;
677
678         /* Set up allocation policy (QoS and RR) */
679         CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
680         cfs_init_rwsem(&lod->lod_qos.lq_rw_sem);
681         lod->lod_qos.lq_dirty = 1;
682         lod->lod_qos.lq_rr.lqr_dirty = 1;
683         lod->lod_qos.lq_reset = 1;
684         /* Default priority is toward free space balance */
685         lod->lod_qos.lq_prio_free = 232;
686         /* Default threshold for rr (roughly 17%) */
687         lod->lod_qos.lq_threshold_rr = 43;
688         /* Init statfs fields */
689         OBD_ALLOC_PTR(lod->lod_qos.lq_statfs_data);
690         if (NULL == lod->lod_qos.lq_statfs_data)
691                 RETURN(-ENOMEM);
692         cfs_waitq_init(&lod->lod_qos.lq_statfs_waitq);
693
694         /* Set up OST pool environment */
695         lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
696                                                    HASH_POOLS_MAX_BITS,
697                                                    HASH_POOLS_BKT_BITS, 0,
698                                                    CFS_HASH_MIN_THETA,
699                                                    CFS_HASH_MAX_THETA,
700                                                    &pool_hash_operations,
701                                                    CFS_HASH_DEFAULT);
702         if (!lod->lod_pools_hash_body)
703                 GOTO(out_statfs, rc = -ENOMEM);
704         CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
705         lod->lod_pool_count = 0;
706         rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
707         if (rc)
708                 GOTO(out_hash, rc);
709         rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
710         if (rc)
711                 GOTO(out_pool_info, rc);
712
713         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
714          * added to the LOD, see lod_add_device() */
715         lod->lod_ost_bitmap = NULL;
716         lod->lod_osts_size  = 0;
717         lod->lod_ostnr      = 0;
718
719         lod->lod_death_row = 0;
720         lod->lod_refcount  = 0;
721
722         lprocfs_lod_init_vars(&lvars);
723         lprocfs_obd_setup(obd, lvars.obd_vars);
724
725 #ifdef LPROCFS
726         rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
727                                 0444, &lod_proc_target_fops, obd);
728         if (rc) {
729                 CWARN("%s: Error adding the target_obd file %d\n",
730                       obd->obd_name, rc);
731                 GOTO(out_lproc, rc);
732         }
733         lod->lod_pool_proc_entry = lprocfs_register("pools",
734                                                     obd->obd_proc_entry,
735                                                     NULL, NULL);
736         if (IS_ERR(lod->lod_pool_proc_entry)) {
737                 int ret = PTR_ERR(lod->lod_pool_proc_entry);
738                 lod->lod_pool_proc_entry = NULL;
739                 CWARN("%s: Failed to create pool proc file %d\n",
740                       obd->obd_name, ret);
741                 rc = lod_pools_fini(lod);
742                 RETURN(ret);
743         }
744 #endif
745
746         RETURN(0);
747
748 out_lproc:
749         lprocfs_obd_cleanup(obd);
750         lod_ost_pool_free(&lod->lod_qos.lq_rr.lqr_pool);
751 out_pool_info:
752         lod_ost_pool_free(&lod->lod_pool_info);
753 out_hash:
754         cfs_hash_putref(lod->lod_pools_hash_body);
755 out_statfs:
756         OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
757         return rc;
758 }
759
760 int lod_pools_fini(struct lod_device *lod)
761 {
762         struct obd_device   *obd = lod2obd(lod);
763         cfs_list_t          *pos, *tmp;
764         struct pool_desc    *pool;
765         ENTRY;
766
767         cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
768                 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
769                 /* free pool structs */
770                 CDEBUG(D_INFO, "delete pool %p\n", pool);
771                 lod_pool_del(obd, pool->pool_name);
772         }
773
774         if (lod->lod_osts_size > 0) {
775                 int idx;
776                 lod_getref(lod);
777                 cfs_mutex_lock(&lod->lod_mutex);
778                 cfs_foreach_bit(lod->lod_ost_bitmap, idx)
779                         __lod_del_device(lod, idx);
780                 cfs_mutex_unlock(&lod->lod_mutex);
781                 lod_putref(lod);
782                 CFS_FREE_BITMAP(lod->lod_ost_bitmap);
783                 for (idx = 0; idx < OST_PTRS; idx++) {
784                         if (lod->lod_ost_idx[idx])
785                                 OBD_FREE_PTR(lod->lod_ost_idx[idx]);
786                 }
787                 lod->lod_osts_size = 0;
788         }
789
790         cfs_hash_putref(lod->lod_pools_hash_body);
791         lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
792         lod_ost_pool_free(&lod->lod_pool_info);
793
794         /* clear pools parent proc entry only after all pools are killed */
795         if (lod->lod_pool_proc_entry) {
796                 lprocfs_remove(&lod->lod_pool_proc_entry);
797                 lod->lod_pool_proc_entry = NULL;
798         }
799
800         lprocfs_obd_cleanup(obd);
801
802         OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
803         RETURN(0);
804 }
805