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