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