Whamcloud - gitweb
e059cf7e26b7819128245b28e1924b978ce8f7df
[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         { 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         { OFD_HEALTH_CHECK_OID,         HEALTH_CHECK },
103         { ACCT_USER_OID,                "acct_usr_inode" },
104         { ACCT_GROUP_OID,               "acct_grp_inode" },
105         { MDD_ORPHAN_OID,               NULL },
106         { 0,                            NULL }
107 };
108
109 static char *oid2name(const unsigned long oid)
110 {
111         int i = 0;
112
113         while (oids[i].oid) {
114                 if (oids[i].oid == oid)
115                         return oids[i].name;
116                 i++;
117         }
118         return NULL;
119 }
120
121 /**
122  * Lookup an existing OI by the given name.
123  */
124 static int
125 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
126               uint64_t parent, const char *name, struct osd_oi *oi)
127 {
128         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
129         int                      rc;
130
131         rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
132         if (rc)
133                 return rc;
134
135         strncpy(oi->oi_name, name, OSD_OI_NAME_SIZE - 1);
136         oi->oi_zapid = zde->zde_dnode;
137
138         return rc;
139 }
140
141 /**
142  * Create a new OI with the given name.
143  */
144 static int
145 osd_oi_create(const struct lu_env *env, struct osd_device *o,
146               uint64_t parent, const char *name, uint64_t *child)
147 {
148         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
149         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
150         dmu_buf_t               *db;
151         dmu_tx_t                *tx;
152         int                      rc;
153
154         /* verify it doesn't already exist */
155         rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
156         if (rc == 0)
157                 return -EEXIST;
158
159         /* create fid-to-dnode index */
160         tx = dmu_tx_create(o->od_objset.os);
161         if (tx == NULL)
162                 return -ENOMEM;
163
164         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
165         dmu_tx_hold_bonus(tx, parent);
166         dmu_tx_hold_zap(tx, parent, TRUE, name);
167         LASSERT(tx->tx_objset->os_sa);
168         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
169
170         rc = -dmu_tx_assign(tx, TXG_WAIT);
171         if (rc) {
172                 dmu_tx_abort(tx);
173                 return rc;
174         }
175
176         la->la_valid = LA_MODE | LA_UID | LA_GID;
177         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
178         la->la_uid = la->la_gid = 0;
179         __osd_zap_create(env, &o->od_objset, &db, tx, la, parent, oi_tag, 0);
180
181         zde->zde_dnode = db->db_object;
182         zde->zde_pad = 0;
183         zde->zde_type = IFTODT(S_IFDIR);
184
185         rc = -zap_add(o->od_objset.os, parent, name, 8, 1, (void *)zde, tx);
186
187         dmu_tx_commit(tx);
188
189         *child = db->db_object;
190         sa_buf_rele(db, oi_tag);
191
192         return rc;
193 }
194
195 static int
196 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
197                       uint64_t parent, const char *name, uint64_t *child)
198 {
199         struct osd_oi   oi;
200         int             rc;
201
202         rc = osd_oi_lookup(env, o, parent, name, &oi);
203         if (rc == 0)
204                 *child = oi.oi_zapid;
205         else if (rc == -ENOENT)
206                 rc = osd_oi_create(env, o, parent, name, child);
207
208         return rc;
209 }
210
211 /**
212  * Lookup the target index/flags of the fid, so it will know where
213  * the object is located (tgt index) and it is MDT or OST object.
214  */
215 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
216                    const struct lu_fid *fid, struct lu_seq_range *range)
217 {
218         struct seq_server_site  *ss = osd_seq_site(osd);
219         int                     rc;
220
221         if (fid_is_idif(fid)) {
222                 fld_range_set_ost(range);
223                 range->lsr_index = fid_idif_ost_idx(fid);
224                 return 0;
225         }
226
227         if (!fid_seq_in_fldb(fid_seq(fid))) {
228                 fld_range_set_mdt(range);
229                 if (ss != NULL)
230                         /* FIXME: If ss is NULL, it suppose not get lsr_index
231                          * at all */
232                         range->lsr_index = ss->ss_node_id;
233                 return 0;
234         }
235
236         LASSERT(ss != NULL);
237         fld_range_set_any(range);
238         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
239         if (rc != 0)
240                 CERROR("%s: cannot find FLD range for "DFID": rc = %d\n",
241                        osd_name(osd), PFID(fid), rc);
242         return rc;
243 }
244
245 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
246                   const struct lu_fid *fid)
247 {
248         struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
249         int rc;
250         ENTRY;
251
252         if (fid_is_idif(fid))
253                 RETURN(1);
254
255         rc = osd_fld_lookup(env, osd, fid, range);
256         if (rc != 0) {
257                 CERROR("%s: Can not lookup fld for "DFID"\n",
258                        osd_name(osd), PFID(fid));
259                 RETURN(rc);
260         }
261
262         CDEBUG(D_INFO, "fid "DFID" range "DRANGE"\n", PFID(fid),
263                PRANGE(range));
264
265         if (fld_range_is_ost(range))
266                 RETURN(1);
267
268         RETURN(0);
269 }
270
271 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
272                                            obd_seq seq)
273 {
274         struct osd_seq *osd_seq;
275
276         cfs_list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
277                 if (osd_seq->os_seq == seq)
278                         return osd_seq;
279         }
280         return NULL;
281 }
282
283 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list,
284                                     obd_seq seq)
285 {
286         struct osd_seq *osd_seq;
287
288         read_lock(&seq_list->osl_seq_list_lock);
289         osd_seq = osd_seq_find_locked(seq_list, seq);
290         read_unlock(&seq_list->osl_seq_list_lock);
291
292         return osd_seq;
293 }
294
295 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
296                                            struct osd_device *osd, obd_seq seq)
297 {
298         struct osd_seq_list     *seq_list = &osd->od_seq_list;
299         struct osd_seq          *osd_seq;
300         char                    *key = osd_oti_get(env)->oti_buf;
301         char                    *seq_name = osd_oti_get(env)->oti_str;
302         struct osd_oi           oi;
303         uint64_t                sdb, odb;
304         int                     i;
305         int                     rc = 0;
306         ENTRY;
307
308         osd_seq = osd_seq_find(seq_list, seq);
309         if (osd_seq != NULL)
310                 RETURN(osd_seq);
311
312         down(&seq_list->osl_seq_init_sem);
313         /* Check again, in case some one else already add it
314          * to the list */
315         osd_seq = osd_seq_find(seq_list, seq);
316         if (osd_seq != NULL)
317                 GOTO(out, rc = 0);
318
319         OBD_ALLOC_PTR(osd_seq);
320         if (osd_seq == NULL)
321                 GOTO(out, rc = -ENOMEM);
322
323         CFS_INIT_LIST_HEAD(&osd_seq->os_seq_list);
324         osd_seq->os_seq = seq;
325
326         /* Init subdir count to be 32, but each seq can have
327          * different subdir count */
328         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
329         OBD_ALLOC(osd_seq->os_compat_dirs,
330                   sizeof(uint64_t) * osd_seq->os_subdir_count);
331         if (osd_seq->os_compat_dirs == NULL)
332                 GOTO(out, rc = -ENOMEM);
333
334         rc = osd_oi_lookup(env, osd, osd->od_root, "O", &oi);
335         if (rc != 0) {
336                 CERROR("%s: Can not find O: rc = %d\n", osd_name(osd), rc);
337                 GOTO(out, rc);
338         }
339
340         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
341                 fid_seq_is_mdt0(seq)) ?  LPU64 : LPX64i,
342                 fid_seq_is_idif(seq) ? 0 : seq);
343
344         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
345         if (rc != 0) {
346                 CERROR("%s: Can not create %s : rc = %d\n",
347                        osd_name(osd), seq_name, rc);
348                 GOTO(out, rc);
349         }
350
351         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
352                 sprintf(key, "d%d", i);
353                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
354                 if (rc)
355                         GOTO(out, osd_seq = ERR_PTR(rc));
356                 osd_seq->os_compat_dirs[i] = sdb;
357         }
358
359         write_lock(&seq_list->osl_seq_list_lock);
360         cfs_list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
361         write_unlock(&seq_list->osl_seq_list_lock);
362 out:
363         up(&seq_list->osl_seq_init_sem);
364         if (rc != 0) {
365                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
366                         OBD_FREE(osd_seq->os_compat_dirs,
367                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
368                 if (osd_seq != NULL)
369                         OBD_FREE_PTR(osd_seq);
370                 osd_seq = ERR_PTR(rc);
371         }
372         RETURN(osd_seq);
373 }
374
375 /*
376  * objects w/o a natural reference (unlike a file on a MDS)
377  * are put under a special hierarchy /O/<seq>/d0..dXX
378  * this function returns a directory specific fid belongs to
379  */
380 static uint64_t
381 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
382                         const struct lu_fid *fid, char *buf)
383 {
384         struct osd_seq  *osd_seq;
385         unsigned long   b;
386         int             rc;
387
388         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
389         if (IS_ERR(osd_seq)) {
390                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
391                        PFID(fid));
392                 return PTR_ERR(osd_seq);
393         }
394
395         rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
396         LASSERT(rc == 0); /* we should not get here with IGIF */
397         b = ostid_id(&osd_oti_get(env)->oti_ostid) % OSD_OST_MAP_SIZE;
398         LASSERT(osd_seq->os_compat_dirs[b]);
399
400         sprintf(buf, LPU64, ostid_id(&osd_oti_get(env)->oti_ostid));
401
402         return osd_seq->os_compat_dirs[b];
403 }
404
405 /* XXX: f_ver is not counted, but may differ too */
406 static void osd_fid2str(char *buf, const struct lu_fid *fid)
407 {
408         sprintf(buf, DFID_NOBRACE, PFID(fid));
409 }
410
411 /*
412  * Determine the zap object id which is being used as the OI for the
413  * given fid.  The lowest N bits in the sequence ID are used as the
414  * index key.  On failure 0 is returned which zfs treats internally
415  * as an invalid object id.
416  */
417 static uint64_t
418 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
419                     char *buf)
420 {
421         struct osd_oi *oi;
422
423         LASSERT(osd->od_oi_table != NULL);
424         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
425         osd_fid2str(buf, fid);
426
427         return oi->oi_zapid;
428 }
429
430 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
431                             const struct lu_fid *fid, char *buf)
432 {
433         uint64_t zapid;
434
435         LASSERT(fid);
436         LASSERT(buf);
437
438         if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
439                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf);
440         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
441                 /* special objects with fixed known fids get their name */
442                 char *name = oid2name(fid_oid(fid));
443
444                 if (name) {
445                         zapid = osd->od_root;
446                         strcpy(buf, name);
447                         if (fid_is_acct(fid))
448                                 zapid = MASTER_NODE_OBJ;
449                 } else {
450                         zapid = osd_get_idx_for_fid(osd, fid, buf);
451                 }
452         } else {
453                 zapid = osd_get_idx_for_fid(osd, fid, buf);
454         }
455
456         return zapid;
457 }
458
459 static inline int fid_is_fs_root(const struct lu_fid *fid)
460 {
461         /* Map root inode to special local object FID */
462         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
463                 fid_oid(fid) == OSD_FS_ROOT_OID;
464 }
465
466 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
467                    const struct lu_fid *fid, uint64_t *oid)
468 {
469         struct osd_thread_info  *info = osd_oti_get(env);
470         char                    *buf = info->oti_buf;
471         uint64_t                zapid;
472         int                     rc = 0;
473         ENTRY;
474
475         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
476                 RETURN(-ENOENT);
477
478         if (unlikely(fid_is_acct(fid))) {
479                 if (fid_oid(fid) == ACCT_USER_OID)
480                         *oid = dev->od_iusr_oid;
481                 else
482                         *oid = dev->od_igrp_oid;
483         } else if (unlikely(fid_is_fs_root(fid))) {
484                 *oid = dev->od_root;
485         } else {
486                 zapid = osd_get_name_n_idx(env, dev, fid, buf);
487
488                 rc = -zap_lookup(dev->od_objset.os, zapid, buf,
489                                 8, 1, &info->oti_zde);
490                 if (rc)
491                         RETURN(rc);
492                 *oid = info->oti_zde.lzd_reg.zde_dnode;
493         }
494
495         RETURN(rc);
496 }
497
498 /**
499  * Close an entry in a specific slot.
500  */
501 static void
502 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
503 {
504         struct osd_oi *oi;
505
506         LASSERT(key < o->od_oi_count);
507
508         oi = o->od_oi_table[key];
509         if (oi) {
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
540         return 0;
541 }
542
543 /**
544  * Depopulate the OI table.
545  */
546 static void
547 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
548 {
549         int i;
550
551         for (i = 0; i < o->od_oi_count; i++)
552                 osd_oi_remove_table(env, o, i);
553 }
554
555 /**
556  * Populate the OI table based.
557  */
558 static int
559 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
560 {
561         char name[16];
562         int  i, rc = 0;
563         ENTRY;
564
565         for (i = 0; i < count; i++) {
566                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
567                 rc = osd_oi_add_table(env, o, name, i);
568                 if (rc) {
569                         osd_oi_close_table(env, o);
570                         break;
571                 }
572         }
573
574         RETURN(rc);
575 }
576
577 /**
578  * Determine if the type and number of OIs used by this file system.
579  */
580 static int
581 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
582 {
583         uint64_t        root_oid = o->od_root;
584         struct osd_oi   oi;
585         char            name[16];
586         int             rc;
587         ENTRY;
588
589         /*
590          * Check for multiple OIs and determine the count.  There is no
591          * gap handling, if an OI is missing the wrong size can be returned.
592          * The only safeguard is that we know the number of OIs must be a
593          * power of two and this is checked for basic sanity.
594          */
595         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
596                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
597                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
598                 if (rc == 0)
599                         continue;
600
601                 if (rc == -ENOENT) {
602                         if (*count == 0)
603                                 break;
604
605                         if ((*count & (*count - 1)) != 0)
606                                 RETURN(-EDOM);
607
608                         RETURN(0);
609                 }
610
611                 RETURN(rc);
612         }
613
614         /*
615          * No OIs exist, this must be a new filesystem.
616          */
617         *count = 0;
618
619         RETURN(0);
620 }
621
622 static void osd_ost_seq_init(const struct lu_env *env, struct osd_device *osd)
623 {
624         struct osd_seq_list *osl = &osd->od_seq_list;
625
626         CFS_INIT_LIST_HEAD(&osl->osl_seq_list);
627         rwlock_init(&osl->osl_seq_list_lock);
628         sema_init(&osl->osl_seq_init_sem, 1);
629 }
630
631 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
632 {
633         struct osd_seq_list     *osl = &osd->od_seq_list;
634         struct osd_seq          *osd_seq, *tmp;
635
636         write_lock(&osl->osl_seq_list_lock);
637         cfs_list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
638                                      os_seq_list) {
639                 cfs_list_del(&osd_seq->os_seq_list);
640                 OBD_FREE(osd_seq->os_compat_dirs,
641                          sizeof(uint64_t) * osd_seq->os_subdir_count);
642                 OBD_FREE(osd_seq, sizeof(*osd_seq));
643         }
644         write_unlock(&osl->osl_seq_list_lock);
645
646         return;
647 }
648
649 /**
650  * Create /O subdirectory to map legacy OST objects for compatibility.
651  */
652 static int
653 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
654 {
655         uint64_t         odb, sdb;
656         int              rc;
657         ENTRY;
658
659         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
660         if (rc)
661                 RETURN(rc);
662
663         osd_ost_seq_init(env, o);
664         /* Create on-disk indexes to maintain per-UID/GID inode usage.
665          * Those new indexes are created in the top-level ZAP outside the
666          * namespace in order not to confuse ZPL which might interpret those
667          * indexes as directories and assume the values are object IDs */
668         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
669                         oid2name(ACCT_USER_OID), &odb);
670         if (rc)
671                 RETURN(rc);
672         o->od_iusr_oid = odb;
673
674         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
675                         oid2name(ACCT_GROUP_OID), &odb);
676         if (rc)
677                 RETURN(rc);
678         o->od_igrp_oid = odb;
679
680         RETURN(rc);
681 }
682
683 static char *root2convert = "ROOT";
684 /*
685  * due to DNE requirements we have to change sequence of /ROOT object
686  * so that it doesn't belong to the local sequence FID_SEQ_LOCAL_FILE
687  * but a normal sequence living on MDS#0
688  * this is the sole purpose of this function.
689  *
690  * This is only needed for pre-production 2.4 ZFS filesystems, and
691  * can be removed in the future.
692  */
693 int osd_convert_root_to_new_seq(const struct lu_env *env,
694                                         struct osd_device *o)
695 {
696         struct luz_direntry *lze = &osd_oti_get(env)->oti_zde;
697         char                *buf = osd_oti_get(env)->oti_str;
698         struct lu_fid        newfid;
699         uint64_t             zapid;
700         dmu_tx_t            *tx = NULL;
701         int                  rc;
702         ENTRY;
703
704         /* ignore OSTs */
705         if (strstr(o->od_svname, "MDT") == NULL)
706                 RETURN(0);
707
708         /* lookup /ROOT */
709         rc = -zap_lookup(o->od_objset.os, o->od_root, root2convert, 8,
710                          sizeof(*lze) / 8, (void *)lze);
711         /* doesn't exist or let actual user to handle the error */
712         if (rc)
713                 RETURN(0);
714
715         CDEBUG(D_OTHER, "%s: /ROOT -> "DFID" -> "LPU64"\n", o->od_svname,
716                PFID(&lze->lzd_fid), (long long int) lze->lzd_reg.zde_dnode);
717
718         /* already right one? */
719         if (fid_seq(&lze->lzd_fid) == FID_SEQ_ROOT)
720                 return 0;
721
722         tx = dmu_tx_create(o->od_objset.os);
723         if (tx == NULL)
724                 return -ENOMEM;
725
726         dmu_tx_hold_bonus(tx, o->od_root);
727
728         /* declare delete/insert of the name */
729         dmu_tx_hold_zap(tx, o->od_root, TRUE, root2convert);
730         dmu_tx_hold_zap(tx, o->od_root, FALSE, root2convert);
731
732         /* declare that we'll remove object from fid-dnode mapping */
733         zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
734         dmu_tx_hold_bonus(tx, zapid);
735         dmu_tx_hold_zap(tx, zapid, FALSE, buf);
736
737         /* declare that we'll add object to fid-dnode mapping */
738         newfid.f_seq = FID_SEQ_ROOT;
739         newfid.f_oid = 1;
740         newfid.f_ver = 0;
741         zapid = osd_get_name_n_idx(env, o, &newfid, buf);
742         dmu_tx_hold_bonus(tx, zapid);
743         dmu_tx_hold_zap(tx, zapid, TRUE, buf);
744
745         rc = -dmu_tx_assign(tx, TXG_WAIT);
746         if (rc)
747                 GOTO(err, rc);
748
749         rc = -zap_remove(o->od_objset.os, o->od_root, root2convert, tx);
750         if (rc)
751                 GOTO(err, rc);
752
753         /* remove from OI */
754         zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
755         rc = -zap_remove(o->od_objset.os, zapid, buf, tx);
756         if (rc)
757                 GOTO(err, rc);
758
759         lze->lzd_fid = newfid;
760         rc = -zap_add(o->od_objset.os, o->od_root, root2convert,
761                       8, sizeof(*lze) / 8, (void *)lze, tx);
762         if (rc)
763                 GOTO(err, rc);
764
765         /* add to OI with the new fid */
766         zapid = osd_get_name_n_idx(env, o, &newfid, buf);
767         rc = -zap_add(o->od_objset.os, zapid, buf, 8, 1, &lze->lzd_reg, tx);
768         if (rc)
769                 GOTO(err, rc);
770
771
772         /* LMA will be updated in mdd_compat_fixes */
773         dmu_tx_commit(tx);
774
775         RETURN(rc);
776
777 err:
778         if (tx)
779                 dmu_tx_abort(tx);
780         CERROR("%s: can't convert to new fid: rc = %d\n", o->od_svname, rc);
781         RETURN(rc);
782 }
783
784 /**
785  * Initialize the OIs by either opening or creating them as needed.
786  */
787 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
788 {
789         char    *key = osd_oti_get(env)->oti_buf;
790         int      i, rc, count = 0;
791         ENTRY;
792
793         rc = osd_oi_probe(env, o, &count);
794         if (rc)
795                 RETURN(rc);
796
797         if (count == 0) {
798                 uint64_t odb, sdb;
799
800                 count = osd_oi_count;
801                 odb = o->od_root;
802
803                 for (i = 0; i < count; i++) {
804                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
805                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
806                         if (rc)
807                                 RETURN(rc);
808                 }
809         }
810
811         rc = osd_oi_init_compat(env, o);
812         if (rc)
813                 RETURN(rc);
814
815         LASSERT((count & (count - 1)) == 0);
816         o->od_oi_count = count;
817         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
818         if (o->od_oi_table == NULL)
819                 RETURN(-ENOMEM);
820
821         rc = osd_oi_open_table(env, o, count);
822         if (rc) {
823                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
824                 o->od_oi_table = NULL;
825         }
826
827         RETURN(rc);
828 }
829
830 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
831 {
832         ENTRY;
833
834         osd_ost_seq_fini(env, o);
835
836         if (o->od_oi_table != NULL) {
837                 (void) osd_oi_close_table(env, o);
838                 OBD_FREE(o->od_oi_table,
839                          sizeof(struct osd_oi *) * o->od_oi_count);
840                 o->od_oi_table = NULL;
841                 o->od_oi_count = 0;
842         }
843
844         EXIT;
845 }
846
847 int osd_options_init(void)
848 {
849         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
850         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
851                 osd_oi_count = OSD_OI_FID_NR;
852
853         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
854                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
855                         osd_oi_count, size_roundup_power2(osd_oi_count));
856                 osd_oi_count = size_roundup_power2(osd_oi_count);
857         }
858
859         return 0;
860 }
861
862