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