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