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