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