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