Whamcloud - gitweb
LU-7863 osd-zfs: dmu_prefetch change in ZFS master
[fs/lustre-release.git] / lustre / osd-zfs / osd_oi.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd-zfs/osd_oi.c
37  * OI functions to map fid to dnode
38  *
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mike Pershin <tappro@whamcloud.com>
41  * Author: Di Wang <di.wang@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_OSD
45
46 #include <lustre_ver.h>
47 #include <libcfs/libcfs.h>
48 #include <obd_support.h>
49 #include <lustre_net.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_disk.h>
53 #include <lustre_fid.h>
54
55 #include "osd_internal.h"
56
57 #include <sys/dnode.h>
58 #include <sys/dbuf.h>
59 #include <sys/spa.h>
60 #include <sys/stat.h>
61 #include <sys/zap.h>
62 #include <sys/spa_impl.h>
63 #include <sys/zfs_znode.h>
64 #include <sys/dmu_tx.h>
65 #include <sys/dmu_objset.h>
66 #include <sys/dsl_prop.h>
67 #include <sys/sa_impl.h>
68 #include <sys/txg.h>
69
70 #define OSD_OI_FID_NR         (1UL << 7)
71 #define OSD_OI_FID_NR_MAX     (1UL << OSD_OI_FID_OID_BITS_MAX)
72 unsigned int osd_oi_count = OSD_OI_FID_NR;
73
74
75 /*
76  * zfs osd maintains names for known fids in the name hierarchy
77  * so that one can mount filesystem with regular ZFS stack and
78  * access files
79  */
80 struct named_oid {
81         unsigned long    oid;
82         char            *name;
83 };
84
85 static const struct named_oid oids[] = {
86         { LAST_RECV_OID,                LAST_RCVD },
87         { OFD_LAST_GROUP_OID,           "LAST_GROUP" },
88         { LLOG_CATALOGS_OID,            "CATALOGS" },
89         { MGS_CONFIGS_OID,              NULL /*MOUNT_CONFIGS_DIR*/ },
90         { FID_SEQ_SRV_OID,              "seq_srv" },
91         { FID_SEQ_CTL_OID,              "seq_ctl" },
92         { FLD_INDEX_OID,                "fld" },
93         { MDD_LOV_OBJ_OID,              LOV_OBJID },
94         { OFD_HEALTH_CHECK_OID,         HEALTH_CHECK },
95         { ACCT_USER_OID,                "acct_usr_inode" },
96         { ACCT_GROUP_OID,               "acct_grp_inode" },
97         { REPLY_DATA_OID,               REPLY_DATA },
98         { 0,                            NULL }
99 };
100
101 static char *oid2name(const unsigned long oid)
102 {
103         int i = 0;
104
105         while (oids[i].oid) {
106                 if (oids[i].oid == oid)
107                         return oids[i].name;
108                 i++;
109         }
110         return NULL;
111 }
112
113 /**
114  * Lookup an existing OI by the given name.
115  */
116 static int
117 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
118               uint64_t parent, const char *name, struct osd_oi *oi)
119 {
120         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
121         int                      rc;
122
123         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
124         if (rc)
125                 return rc;
126
127         rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
128         if (rc >= sizeof(oi->oi_name))
129                 return -E2BIG;
130
131         oi->oi_zapid = zde->zde_dnode;
132
133         return 0;
134 }
135
136 /**
137  * Create a new OI with the given name.
138  */
139 static int
140 osd_oi_create(const struct lu_env *env, struct osd_device *o,
141               uint64_t parent, const char *name, uint64_t *child)
142 {
143         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
144         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
145         dmu_buf_t               *db;
146         dmu_tx_t                *tx;
147         int                      rc;
148
149         /* verify it doesn't already exist */
150         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
151         if (rc == 0)
152                 return -EEXIST;
153
154         /* create fid-to-dnode index */
155         tx = dmu_tx_create(o->od_os);
156         if (tx == NULL)
157                 return -ENOMEM;
158
159         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
160         dmu_tx_hold_bonus(tx, parent);
161         dmu_tx_hold_zap(tx, parent, TRUE, name);
162         LASSERT(tx->tx_objset->os_sa);
163         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
164
165         rc = -dmu_tx_assign(tx, TXG_WAIT);
166         if (rc) {
167                 dmu_tx_abort(tx);
168                 return rc;
169         }
170
171         la->la_valid = LA_MODE | LA_UID | LA_GID;
172         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
173         la->la_uid = la->la_gid = 0;
174         __osd_zap_create(env, o, &db, tx, la, parent, 0);
175
176         zde->zde_dnode = db->db_object;
177         zde->zde_pad = 0;
178         zde->zde_type = IFTODT(S_IFDIR);
179
180         rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
181
182         dmu_tx_commit(tx);
183
184         *child = db->db_object;
185         sa_buf_rele(db, osd_obj_tag);
186
187         return rc;
188 }
189
190 static int
191 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
192                       uint64_t parent, const char *name, uint64_t *child)
193 {
194         struct osd_oi   oi;
195         int             rc;
196
197         rc = osd_oi_lookup(env, o, parent, name, &oi);
198         if (rc == 0)
199                 *child = oi.oi_zapid;
200         else if (rc == -ENOENT)
201                 rc = osd_oi_create(env, o, parent, name, child);
202
203         return rc;
204 }
205
206 /**
207  * Lookup the target index/flags of the fid, so it will know where
208  * the object is located (tgt index) and it is MDT or OST object.
209  */
210 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
211                    u64 seq, struct lu_seq_range *range)
212 {
213         struct seq_server_site  *ss = osd_seq_site(osd);
214
215         if (fid_seq_is_idif(seq)) {
216                 fld_range_set_ost(range);
217                 range->lsr_index = idif_ost_idx(seq);
218                 return 0;
219         }
220
221         if (!fid_seq_in_fldb(seq)) {
222                 fld_range_set_mdt(range);
223                 if (ss != NULL)
224                         /* FIXME: If ss is NULL, it suppose not get lsr_index
225                          * at all */
226                         range->lsr_index = ss->ss_node_id;
227                 return 0;
228         }
229
230         LASSERT(ss != NULL);
231         fld_range_set_any(range);
232         /* OSD will only do local fld lookup */
233         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
234 }
235
236 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
237                   const struct lu_fid *fid)
238 {
239         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
240         int                     rc;
241         ENTRY;
242
243         if (fid_is_idif(fid))
244                 RETURN(1);
245
246         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
247                      fid_is_name_llog(fid) || fid_is_quota(fid))
248                 RETURN(0);
249
250         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
251         if (rc != 0) {
252                 if (rc != -ENOENT)
253                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
254                                osd_name(osd), PFID(fid), rc);
255                 RETURN(0);
256         }
257
258         if (fld_range_is_ost(range))
259                 RETURN(1);
260
261         RETURN(0);
262 }
263
264 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
265                                            u64 seq)
266 {
267         struct osd_seq *osd_seq;
268
269         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
270                 if (osd_seq->os_seq == seq)
271                         return osd_seq;
272         }
273         return NULL;
274 }
275
276 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
277 {
278         struct osd_seq *osd_seq;
279
280         read_lock(&seq_list->osl_seq_list_lock);
281         osd_seq = osd_seq_find_locked(seq_list, seq);
282         read_unlock(&seq_list->osl_seq_list_lock);
283
284         return osd_seq;
285 }
286
287 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
288                                            struct osd_device *osd, u64 seq)
289 {
290         struct osd_seq_list     *seq_list = &osd->od_seq_list;
291         struct osd_seq          *osd_seq;
292         char                    *key = osd_oti_get(env)->oti_buf;
293         char                    *seq_name = osd_oti_get(env)->oti_str;
294         struct osd_oi           oi;
295         uint64_t                sdb, odb;
296         int                     i;
297         int                     rc = 0;
298         ENTRY;
299
300         osd_seq = osd_seq_find(seq_list, seq);
301         if (osd_seq != NULL)
302                 RETURN(osd_seq);
303
304         down(&seq_list->osl_seq_init_sem);
305         /* Check again, in case some one else already add it
306          * to the list */
307         osd_seq = osd_seq_find(seq_list, seq);
308         if (osd_seq != NULL)
309                 GOTO(out, rc = 0);
310
311         OBD_ALLOC_PTR(osd_seq);
312         if (osd_seq == NULL)
313                 GOTO(out, rc = -ENOMEM);
314
315         INIT_LIST_HEAD(&osd_seq->os_seq_list);
316         osd_seq->os_seq = seq;
317
318         /* Init subdir count to be 32, but each seq can have
319          * different subdir count */
320         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
321         OBD_ALLOC(osd_seq->os_compat_dirs,
322                   sizeof(uint64_t) * osd_seq->os_subdir_count);
323         if (osd_seq->os_compat_dirs == NULL)
324                 GOTO(out, rc = -ENOMEM);
325
326         oi.oi_zapid = osd->od_O_id;
327         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
328                 fid_seq_is_mdt0(seq)) ?  LPU64 : LPX64i,
329                 fid_seq_is_idif(seq) ? 0 : seq);
330
331         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
332         if (rc != 0) {
333                 CERROR("%s: Can not create %s : rc = %d\n",
334                        osd_name(osd), seq_name, rc);
335                 GOTO(out, rc);
336         }
337
338         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
339                 sprintf(key, "d%d", i);
340                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
341                 if (rc)
342                         GOTO(out, rc);
343                 osd_seq->os_compat_dirs[i] = sdb;
344         }
345
346         write_lock(&seq_list->osl_seq_list_lock);
347         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
348         write_unlock(&seq_list->osl_seq_list_lock);
349 out:
350         up(&seq_list->osl_seq_init_sem);
351         if (rc != 0) {
352                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
353                         OBD_FREE(osd_seq->os_compat_dirs,
354                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
355                 if (osd_seq != NULL)
356                         OBD_FREE_PTR(osd_seq);
357                 osd_seq = ERR_PTR(rc);
358         }
359         RETURN(osd_seq);
360 }
361
362 /*
363  * objects w/o a natural reference (unlike a file on a MDS)
364  * are put under a special hierarchy /O/<seq>/d0..dXX
365  * this function returns a directory specific fid belongs to
366  */
367 static uint64_t
368 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
369                         const struct lu_fid *fid, char *buf)
370 {
371         struct osd_seq  *osd_seq;
372         unsigned long   b;
373         u64             id;
374         int             rc;
375
376         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
377         if (IS_ERR(osd_seq)) {
378                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
379                        PFID(fid));
380                 return PTR_ERR(osd_seq);
381         }
382
383         if (fid_is_last_id(fid)) {
384                 id = 0;
385         } else {
386                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
387                 LASSERT(rc == 0); /* we should not get here with IGIF */
388                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
389         }
390
391         b = id % OSD_OST_MAP_SIZE;
392         LASSERT(osd_seq->os_compat_dirs[b]);
393
394         sprintf(buf, LPU64, id);
395
396         return osd_seq->os_compat_dirs[b];
397 }
398
399 /* XXX: f_ver is not counted, but may differ too */
400 static void osd_fid2str(char *buf, const struct lu_fid *fid)
401 {
402         sprintf(buf, DFID_NOBRACE, PFID(fid));
403 }
404
405 /*
406  * Determine the zap object id which is being used as the OI for the
407  * given fid.  The lowest N bits in the sequence ID are used as the
408  * index key.  On failure 0 is returned which zfs treats internally
409  * as an invalid object id.
410  */
411 static uint64_t
412 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
413                     char *buf)
414 {
415         struct osd_oi *oi;
416
417         LASSERT(osd->od_oi_table != NULL);
418         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
419         osd_fid2str(buf, fid);
420
421         return oi->oi_zapid;
422 }
423
424 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
425                             const struct lu_fid *fid, char *buf)
426 {
427         uint64_t zapid;
428
429         LASSERT(fid);
430         LASSERT(buf);
431
432         if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
433                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf);
434         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
435                 /* special objects with fixed known fids get their name */
436                 char *name = oid2name(fid_oid(fid));
437
438                 if (name) {
439                         zapid = osd->od_root;
440                         strcpy(buf, name);
441                         if (fid_is_acct(fid))
442                                 zapid = MASTER_NODE_OBJ;
443                 } else {
444                         zapid = osd_get_idx_for_fid(osd, fid, buf);
445                 }
446         } else {
447                 zapid = osd_get_idx_for_fid(osd, fid, buf);
448         }
449
450         return zapid;
451 }
452
453 static inline int fid_is_fs_root(const struct lu_fid *fid)
454 {
455         /* Map root inode to special local object FID */
456         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
457                 fid_oid(fid) == OSD_FS_ROOT_OID;
458 }
459
460 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
461                    const struct lu_fid *fid, uint64_t *oid)
462 {
463         struct osd_thread_info  *info = osd_oti_get(env);
464         char                    *buf = info->oti_buf;
465         uint64_t                zapid;
466         int                     rc = 0;
467         ENTRY;
468
469         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
470                 RETURN(-ENOENT);
471
472         if (unlikely(fid_is_acct(fid))) {
473                 if (fid_oid(fid) == ACCT_USER_OID)
474                         *oid = dev->od_iusr_oid;
475                 else
476                         *oid = dev->od_igrp_oid;
477         } else if (unlikely(fid_is_fs_root(fid))) {
478                 *oid = dev->od_root;
479         } else {
480                 zapid = osd_get_name_n_idx(env, dev, fid, buf);
481
482                 rc = -zap_lookup(dev->od_os, zapid, buf,
483                                 8, 1, &info->oti_zde);
484                 if (rc)
485                         RETURN(rc);
486                 *oid = info->oti_zde.lzd_reg.zde_dnode;
487         }
488
489         if (rc == 0)
490                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
491                                  ZIO_PRIORITY_ASYNC_READ);
492
493         RETURN(rc);
494 }
495
496 /**
497  * Close an entry in a specific slot.
498  */
499 static void
500 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
501 {
502         struct osd_oi *oi;
503
504         LASSERT(key < o->od_oi_count);
505
506         oi = o->od_oi_table[key];
507         if (oi) {
508                 if (oi->oi_db)
509                         sa_buf_rele(oi->oi_db, osd_obj_tag);
510                 OBD_FREE_PTR(oi);
511                 o->od_oi_table[key] = NULL;
512         }
513 }
514
515 /**
516  * Allocate and open a new entry in the specified unused slot.
517  */
518 static int
519 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
520                  char *name, int key)
521 {
522         struct osd_oi *oi;
523         int rc;
524
525         LASSERT(key < o->od_oi_count);
526         LASSERT(o->od_oi_table[key] == NULL);
527
528         OBD_ALLOC_PTR(oi);
529         if (oi == NULL)
530                 return -ENOMEM;
531
532         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
533         if (rc) {
534                 OBD_FREE_PTR(oi);
535                 return rc;
536         }
537
538         o->od_oi_table[key] = oi;
539         __osd_obj2dbuf(env, o->od_os, oi->oi_zapid, &oi->oi_db);
540
541         return 0;
542 }
543
544 /**
545  * Depopulate the OI table.
546  */
547 static void
548 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
549 {
550         int i;
551
552         for (i = 0; i < o->od_oi_count; i++)
553                 osd_oi_remove_table(env, o, i);
554 }
555
556 /**
557  * Populate the OI table based.
558  */
559 static int
560 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
561 {
562         char name[16];
563         int  i, rc = 0;
564         ENTRY;
565
566         for (i = 0; i < count; i++) {
567                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
568                 rc = osd_oi_add_table(env, o, name, i);
569                 if (rc) {
570                         osd_oi_close_table(env, o);
571                         break;
572                 }
573         }
574
575         RETURN(rc);
576 }
577
578 /**
579  * Determine if the type and number of OIs used by this file system.
580  */
581 static int
582 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
583 {
584         uint64_t        root_oid = o->od_root;
585         struct osd_oi   oi;
586         char            name[16];
587         int             rc;
588         ENTRY;
589
590         /*
591          * Check for multiple OIs and determine the count.  There is no
592          * gap handling, if an OI is missing the wrong size can be returned.
593          * The only safeguard is that we know the number of OIs must be a
594          * power of two and this is checked for basic sanity.
595          */
596         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
597                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
598                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
599                 if (rc == 0)
600                         continue;
601
602                 if (rc == -ENOENT) {
603                         if (*count == 0)
604                                 break;
605
606                         if ((*count & (*count - 1)) != 0)
607                                 RETURN(-EDOM);
608
609                         RETURN(0);
610                 }
611
612                 RETURN(rc);
613         }
614
615         /*
616          * No OIs exist, this must be a new filesystem.
617          */
618         *count = 0;
619
620         RETURN(0);
621 }
622
623 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
624 {
625         struct osd_seq_list     *osl = &osd->od_seq_list;
626         struct osd_seq          *osd_seq, *tmp;
627
628         write_lock(&osl->osl_seq_list_lock);
629         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
630                                  os_seq_list) {
631                 list_del(&osd_seq->os_seq_list);
632                 OBD_FREE(osd_seq->os_compat_dirs,
633                          sizeof(uint64_t) * osd_seq->os_subdir_count);
634                 OBD_FREE(osd_seq, sizeof(*osd_seq));
635         }
636         write_unlock(&osl->osl_seq_list_lock);
637
638         return;
639 }
640
641 /**
642  * Create /O subdirectory to map legacy OST objects for compatibility.
643  */
644 static int
645 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
646 {
647         uint64_t         odb, sdb;
648         int              rc;
649         ENTRY;
650
651         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
652         if (rc)
653                 RETURN(rc);
654
655         o->od_O_id = sdb;
656
657         /* Create on-disk indexes to maintain per-UID/GID inode usage.
658          * Those new indexes are created in the top-level ZAP outside the
659          * namespace in order not to confuse ZPL which might interpret those
660          * indexes as directories and assume the values are object IDs */
661         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
662                         oid2name(ACCT_USER_OID), &odb);
663         if (rc)
664                 RETURN(rc);
665         o->od_iusr_oid = odb;
666
667         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
668                         oid2name(ACCT_GROUP_OID), &odb);
669         if (rc)
670                 RETURN(rc);
671         o->od_igrp_oid = odb;
672
673         RETURN(rc);
674 }
675
676 /**
677  * Initialize the OIs by either opening or creating them as needed.
678  */
679 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
680 {
681         char    *key = osd_oti_get(env)->oti_buf;
682         int      i, rc, count = 0;
683         ENTRY;
684
685         rc = osd_oi_probe(env, o, &count);
686         if (rc)
687                 RETURN(rc);
688
689         if (count == 0) {
690                 uint64_t odb, sdb;
691
692                 count = osd_oi_count;
693                 odb = o->od_root;
694
695                 for (i = 0; i < count; i++) {
696                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
697                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
698                         if (rc)
699                                 RETURN(rc);
700                 }
701         }
702
703         rc = osd_oi_init_compat(env, o);
704         if (rc)
705                 RETURN(rc);
706
707         LASSERT((count & (count - 1)) == 0);
708         o->od_oi_count = count;
709         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
710         if (o->od_oi_table == NULL)
711                 RETURN(-ENOMEM);
712
713         rc = osd_oi_open_table(env, o, count);
714         if (rc) {
715                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
716                 o->od_oi_table = NULL;
717         }
718
719         RETURN(rc);
720 }
721
722 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
723 {
724         ENTRY;
725
726         osd_ost_seq_fini(env, o);
727
728         if (o->od_oi_table != NULL) {
729                 (void) osd_oi_close_table(env, o);
730                 OBD_FREE(o->od_oi_table,
731                          sizeof(struct osd_oi *) * o->od_oi_count);
732                 o->od_oi_table = NULL;
733                 o->od_oi_count = 0;
734         }
735
736         EXIT;
737 }
738
739 int osd_options_init(void)
740 {
741         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
742         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
743                 osd_oi_count = OSD_OI_FID_NR;
744
745         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
746                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
747                         osd_oi_count, size_roundup_power2(osd_oi_count));
748                 osd_oi_count = size_roundup_power2(osd_oi_count);
749         }
750
751         return 0;
752 }