Whamcloud - gitweb
LU-7898 osd: remove unnecessary declarations
[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         sa_handle_t             *sa_hdl = NULL;
146         dmu_tx_t                *tx;
147         uint64_t                 oid;
148         int                      rc;
149
150         /* verify it doesn't already exist */
151         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
152         if (rc == 0)
153                 return -EEXIST;
154
155         /* create fid-to-dnode index */
156         tx = dmu_tx_create(o->od_os);
157         if (tx == NULL)
158                 return -ENOMEM;
159
160         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
161         dmu_tx_hold_bonus(tx, parent);
162         dmu_tx_hold_zap(tx, parent, TRUE, name);
163         LASSERT(tx->tx_objset->os_sa);
164         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
165
166         rc = -dmu_tx_assign(tx, TXG_WAIT);
167         if (rc) {
168                 dmu_tx_abort(tx);
169                 return rc;
170         }
171
172         oid = zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
173                                DMU_OT_DIRECTORY_CONTENTS,
174                                14, /* == ZFS fzap_default_block_shift */
175                                DN_MAX_INDBLKSHIFT, /* indirect block shift */
176                                DMU_OT_SA, DN_MAX_BONUSLEN, tx);
177
178         rc = -sa_handle_get(o->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
179         if (rc)
180                 goto commit;
181         la->la_valid = LA_MODE | LA_UID | LA_GID;
182         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
183         la->la_uid = la->la_gid = 0;
184         rc = __osd_attr_init(env, o, sa_hdl, tx, la, parent);
185         sa_handle_destroy(sa_hdl);
186         if (rc)
187                 goto commit;
188
189         zde->zde_dnode = oid;
190         zde->zde_pad = 0;
191         zde->zde_type = IFTODT(S_IFDIR);
192
193         rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
194
195 commit:
196         if (rc)
197                 dmu_object_free(o->od_os, oid, tx);
198         dmu_tx_commit(tx);
199
200         if (rc == 0)
201                 *child = oid;
202
203         return rc;
204 }
205
206 static int
207 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
208                       uint64_t parent, const char *name, uint64_t *child)
209 {
210         struct osd_oi   oi;
211         int             rc;
212
213         rc = osd_oi_lookup(env, o, parent, name, &oi);
214         if (rc == 0)
215                 *child = oi.oi_zapid;
216         else if (rc == -ENOENT)
217                 rc = osd_oi_create(env, o, parent, name, child);
218
219         return rc;
220 }
221
222 /**
223  * Lookup the target index/flags of the fid, so it will know where
224  * the object is located (tgt index) and it is MDT or OST object.
225  */
226 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
227                    u64 seq, struct lu_seq_range *range)
228 {
229         struct seq_server_site  *ss = osd_seq_site(osd);
230
231         if (fid_seq_is_idif(seq)) {
232                 fld_range_set_ost(range);
233                 range->lsr_index = idif_ost_idx(seq);
234                 return 0;
235         }
236
237         if (!fid_seq_in_fldb(seq)) {
238                 fld_range_set_mdt(range);
239                 if (ss != NULL)
240                         /* FIXME: If ss is NULL, it suppose not get lsr_index
241                          * at all */
242                         range->lsr_index = ss->ss_node_id;
243                 return 0;
244         }
245
246         LASSERT(ss != NULL);
247         fld_range_set_any(range);
248         /* OSD will only do local fld lookup */
249         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
250 }
251
252 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
253                   const struct lu_fid *fid)
254 {
255         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
256         int                     rc;
257         ENTRY;
258
259         if (fid_is_idif(fid))
260                 RETURN(1);
261
262         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
263                      fid_is_name_llog(fid) || fid_is_quota(fid))
264                 RETURN(0);
265
266         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
267         if (rc != 0) {
268                 if (rc != -ENOENT)
269                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
270                                osd_name(osd), PFID(fid), rc);
271                 RETURN(0);
272         }
273
274         if (fld_range_is_ost(range))
275                 RETURN(1);
276
277         RETURN(0);
278 }
279
280 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
281                                            u64 seq)
282 {
283         struct osd_seq *osd_seq;
284
285         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
286                 if (osd_seq->os_seq == seq)
287                         return osd_seq;
288         }
289         return NULL;
290 }
291
292 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
293 {
294         struct osd_seq *osd_seq;
295
296         read_lock(&seq_list->osl_seq_list_lock);
297         osd_seq = osd_seq_find_locked(seq_list, seq);
298         read_unlock(&seq_list->osl_seq_list_lock);
299
300         return osd_seq;
301 }
302
303 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
304                                            struct osd_device *osd, u64 seq)
305 {
306         struct osd_seq_list     *seq_list = &osd->od_seq_list;
307         struct osd_seq          *osd_seq;
308         char                    *key = osd_oti_get(env)->oti_buf;
309         char                    *seq_name = osd_oti_get(env)->oti_str;
310         struct osd_oi           oi;
311         uint64_t                sdb, odb;
312         int                     i;
313         int                     rc = 0;
314         ENTRY;
315
316         osd_seq = osd_seq_find(seq_list, seq);
317         if (osd_seq != NULL)
318                 RETURN(osd_seq);
319
320         down(&seq_list->osl_seq_init_sem);
321         /* Check again, in case some one else already add it
322          * to the list */
323         osd_seq = osd_seq_find(seq_list, seq);
324         if (osd_seq != NULL)
325                 GOTO(out, rc = 0);
326
327         OBD_ALLOC_PTR(osd_seq);
328         if (osd_seq == NULL)
329                 GOTO(out, rc = -ENOMEM);
330
331         INIT_LIST_HEAD(&osd_seq->os_seq_list);
332         osd_seq->os_seq = seq;
333
334         /* Init subdir count to be 32, but each seq can have
335          * different subdir count */
336         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
337         OBD_ALLOC(osd_seq->os_compat_dirs,
338                   sizeof(uint64_t) * osd_seq->os_subdir_count);
339         if (osd_seq->os_compat_dirs == NULL)
340                 GOTO(out, rc = -ENOMEM);
341
342         oi.oi_zapid = osd->od_O_id;
343         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
344                 fid_seq_is_mdt0(seq)) ?  LPU64 : LPX64i,
345                 fid_seq_is_idif(seq) ? 0 : seq);
346
347         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
348         if (rc != 0) {
349                 CERROR("%s: Can not create %s : rc = %d\n",
350                        osd_name(osd), seq_name, rc);
351                 GOTO(out, rc);
352         }
353
354         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
355                 sprintf(key, "d%d", i);
356                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
357                 if (rc)
358                         GOTO(out, rc);
359                 osd_seq->os_compat_dirs[i] = sdb;
360         }
361
362         write_lock(&seq_list->osl_seq_list_lock);
363         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
364         write_unlock(&seq_list->osl_seq_list_lock);
365 out:
366         up(&seq_list->osl_seq_init_sem);
367         if (rc != 0) {
368                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
369                         OBD_FREE(osd_seq->os_compat_dirs,
370                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
371                 if (osd_seq != NULL)
372                         OBD_FREE_PTR(osd_seq);
373                 osd_seq = ERR_PTR(rc);
374         }
375         RETURN(osd_seq);
376 }
377
378 /*
379  * objects w/o a natural reference (unlike a file on a MDS)
380  * are put under a special hierarchy /O/<seq>/d0..dXX
381  * this function returns a directory specific fid belongs to
382  */
383 static uint64_t
384 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
385                         const struct lu_fid *fid, char *buf, int bufsize)
386 {
387         struct osd_seq  *osd_seq;
388         unsigned long   b;
389         u64             id;
390         int             rc;
391
392         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
393         if (IS_ERR(osd_seq)) {
394                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
395                        PFID(fid));
396                 return PTR_ERR(osd_seq);
397         }
398
399         if (fid_is_last_id(fid)) {
400                 id = 0;
401         } else {
402                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
403                 LASSERT(rc == 0); /* we should not get here with IGIF */
404                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
405         }
406
407         b = id % OSD_OST_MAP_SIZE;
408         LASSERT(osd_seq->os_compat_dirs[b]);
409
410         if (buf)
411                 snprintf(buf, bufsize, LPU64, id);
412
413         return osd_seq->os_compat_dirs[b];
414 }
415
416 /* XXX: f_ver is not counted, but may differ too */
417 static void osd_fid2str(char *buf, const struct lu_fid *fid)
418 {
419         sprintf(buf, DFID_NOBRACE, PFID(fid));
420 }
421
422 /*
423  * Determine the zap object id which is being used as the OI for the
424  * given fid.  The lowest N bits in the sequence ID are used as the
425  * index key.  On failure 0 is returned which zfs treats internally
426  * as an invalid object id.
427  */
428 static uint64_t
429 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
430                     char *buf)
431 {
432         struct osd_oi *oi;
433
434         LASSERT(osd->od_oi_table != NULL);
435         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
436         if (buf)
437                 osd_fid2str(buf, fid);
438
439         return oi->oi_zapid;
440 }
441
442 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
443                             const struct lu_fid *fid, char *buf, int bufsize)
444 {
445         uint64_t zapid;
446
447         LASSERT(fid);
448
449         if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
450                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
451         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
452                 /* special objects with fixed known fids get their name */
453                 char *name = oid2name(fid_oid(fid));
454
455                 if (name) {
456                         zapid = osd->od_root;
457                         if (buf)
458                                 strncpy(buf, name, bufsize);
459                         if (fid_is_acct(fid))
460                                 zapid = MASTER_NODE_OBJ;
461                 } else {
462                         zapid = osd_get_idx_for_fid(osd, fid, buf);
463                 }
464         } else {
465                 zapid = osd_get_idx_for_fid(osd, fid, buf);
466         }
467
468         return zapid;
469 }
470
471 static inline int fid_is_fs_root(const struct lu_fid *fid)
472 {
473         /* Map root inode to special local object FID */
474         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
475                 fid_oid(fid) == OSD_FS_ROOT_OID;
476 }
477
478 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
479                    const struct lu_fid *fid, uint64_t *oid)
480 {
481         struct osd_thread_info  *info = osd_oti_get(env);
482         char                    *buf = info->oti_buf;
483         uint64_t                zapid;
484         int                     rc = 0;
485         ENTRY;
486
487         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
488                 RETURN(-ENOENT);
489
490         if (unlikely(fid_is_acct(fid))) {
491                 if (fid_oid(fid) == ACCT_USER_OID)
492                         *oid = dev->od_iusr_oid;
493                 else
494                         *oid = dev->od_igrp_oid;
495         } else if (unlikely(fid_is_fs_root(fid))) {
496                 *oid = dev->od_root;
497         } else {
498                 zapid = osd_get_name_n_idx(env, dev, fid, buf,
499                                            sizeof(info->oti_buf));
500                 rc = -zap_lookup(dev->od_os, zapid, buf,
501                                 8, 1, &info->oti_zde);
502                 if (rc)
503                         RETURN(rc);
504                 *oid = info->oti_zde.lzd_reg.zde_dnode;
505         }
506
507         if (rc == 0)
508                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
509                                  ZIO_PRIORITY_ASYNC_READ);
510
511         RETURN(rc);
512 }
513
514 /**
515  * Close an entry in a specific slot.
516  */
517 static void
518 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
519 {
520         struct osd_oi *oi;
521
522         LASSERT(key < o->od_oi_count);
523
524         oi = o->od_oi_table[key];
525         if (oi) {
526                 if (oi->oi_db)
527                         sa_buf_rele(oi->oi_db, osd_obj_tag);
528                 OBD_FREE_PTR(oi);
529                 o->od_oi_table[key] = NULL;
530         }
531 }
532
533 /**
534  * Allocate and open a new entry in the specified unused slot.
535  */
536 static int
537 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
538                  char *name, int key)
539 {
540         struct osd_oi *oi;
541         int rc;
542
543         LASSERT(key < o->od_oi_count);
544         LASSERT(o->od_oi_table[key] == NULL);
545
546         OBD_ALLOC_PTR(oi);
547         if (oi == NULL)
548                 return -ENOMEM;
549
550         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
551         if (rc) {
552                 OBD_FREE_PTR(oi);
553                 return rc;
554         }
555
556         o->od_oi_table[key] = oi;
557         __osd_obj2dbuf(env, o->od_os, oi->oi_zapid, &oi->oi_db);
558
559         return 0;
560 }
561
562 /**
563  * Depopulate the OI table.
564  */
565 static void
566 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
567 {
568         int i;
569
570         for (i = 0; i < o->od_oi_count; i++)
571                 osd_oi_remove_table(env, o, i);
572 }
573
574 /**
575  * Populate the OI table based.
576  */
577 static int
578 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
579 {
580         char name[16];
581         int  i, rc = 0;
582         ENTRY;
583
584         for (i = 0; i < count; i++) {
585                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
586                 rc = osd_oi_add_table(env, o, name, i);
587                 if (rc) {
588                         osd_oi_close_table(env, o);
589                         break;
590                 }
591         }
592
593         RETURN(rc);
594 }
595
596 /**
597  * Determine if the type and number of OIs used by this file system.
598  */
599 static int
600 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
601 {
602         uint64_t        root_oid = o->od_root;
603         struct osd_oi   oi;
604         char            name[16];
605         int             rc;
606         ENTRY;
607
608         /*
609          * Check for multiple OIs and determine the count.  There is no
610          * gap handling, if an OI is missing the wrong size can be returned.
611          * The only safeguard is that we know the number of OIs must be a
612          * power of two and this is checked for basic sanity.
613          */
614         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
615                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
616                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
617                 if (rc == 0)
618                         continue;
619
620                 if (rc == -ENOENT) {
621                         if (*count == 0)
622                                 break;
623
624                         if ((*count & (*count - 1)) != 0)
625                                 RETURN(-EDOM);
626
627                         RETURN(0);
628                 }
629
630                 RETURN(rc);
631         }
632
633         /*
634          * No OIs exist, this must be a new filesystem.
635          */
636         *count = 0;
637
638         RETURN(0);
639 }
640
641 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
642 {
643         struct osd_seq_list     *osl = &osd->od_seq_list;
644         struct osd_seq          *osd_seq, *tmp;
645
646         write_lock(&osl->osl_seq_list_lock);
647         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
648                                  os_seq_list) {
649                 list_del(&osd_seq->os_seq_list);
650                 OBD_FREE(osd_seq->os_compat_dirs,
651                          sizeof(uint64_t) * osd_seq->os_subdir_count);
652                 OBD_FREE(osd_seq, sizeof(*osd_seq));
653         }
654         write_unlock(&osl->osl_seq_list_lock);
655
656         return;
657 }
658
659 /**
660  * Create /O subdirectory to map legacy OST objects for compatibility.
661  */
662 static int
663 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
664 {
665         uint64_t         odb, sdb;
666         int              rc;
667         ENTRY;
668
669         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
670         if (rc)
671                 RETURN(rc);
672
673         o->od_O_id = sdb;
674
675         /* Create on-disk indexes to maintain per-UID/GID inode usage.
676          * Those new indexes are created in the top-level ZAP outside the
677          * namespace in order not to confuse ZPL which might interpret those
678          * indexes as directories and assume the values are object IDs */
679         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
680                         oid2name(ACCT_USER_OID), &odb);
681         if (rc)
682                 RETURN(rc);
683         o->od_iusr_oid = odb;
684
685         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
686                         oid2name(ACCT_GROUP_OID), &odb);
687         if (rc)
688                 RETURN(rc);
689         o->od_igrp_oid = odb;
690
691         RETURN(rc);
692 }
693
694 /**
695  * Initialize the OIs by either opening or creating them as needed.
696  */
697 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
698 {
699         char    *key = osd_oti_get(env)->oti_buf;
700         int      i, rc, count = 0;
701         ENTRY;
702
703         rc = osd_oi_probe(env, o, &count);
704         if (rc)
705                 RETURN(rc);
706
707         if (count == 0) {
708                 uint64_t odb, sdb;
709
710                 count = osd_oi_count;
711                 odb = o->od_root;
712
713                 for (i = 0; i < count; i++) {
714                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
715                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
716                         if (rc)
717                                 RETURN(rc);
718                 }
719         }
720
721         rc = osd_oi_init_compat(env, o);
722         if (rc)
723                 RETURN(rc);
724
725         LASSERT((count & (count - 1)) == 0);
726         o->od_oi_count = count;
727         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
728         if (o->od_oi_table == NULL)
729                 RETURN(-ENOMEM);
730
731         rc = osd_oi_open_table(env, o, count);
732         if (rc) {
733                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
734                 o->od_oi_table = NULL;
735         }
736
737         RETURN(rc);
738 }
739
740 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
741 {
742         ENTRY;
743
744         osd_ost_seq_fini(env, o);
745
746         if (o->od_oi_table != NULL) {
747                 (void) osd_oi_close_table(env, o);
748                 OBD_FREE(o->od_oi_table,
749                          sizeof(struct osd_oi *) * o->od_oi_count);
750                 o->od_oi_table = NULL;
751                 o->od_oi_count = 0;
752         }
753
754         EXIT;
755 }
756
757 int osd_options_init(void)
758 {
759         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
760         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
761                 osd_oi_count = OSD_OI_FID_NR;
762
763         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
764                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
765                         osd_oi_count, size_roundup_power2(osd_oi_count));
766                 osd_oi_count = size_roundup_power2(osd_oi_count);
767         }
768
769         return 0;
770 }