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