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