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