Whamcloud - gitweb
22f6ff0e2dc35643306f2c659afab3ab194026d8
[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  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd-zfs/osd_oi.c
37  * OI functions to map fid to dnode
38  *
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mike Pershin <tappro@whamcloud.com>
41  * Author: Di Wang <di.wang@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_OSD
45
46 #include <lustre_ver.h>
47 #include <libcfs/libcfs.h>
48 #include <obd_support.h>
49 #include <lustre_net.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_disk.h>
53 #include <lustre_fid.h>
54
55 #include "osd_internal.h"
56
57 #include <sys/dnode.h>
58 #include <sys/dbuf.h>
59 #include <sys/spa.h>
60 #include <sys/stat.h>
61 #include <sys/zap.h>
62 #include <sys/spa_impl.h>
63 #include <sys/zfs_znode.h>
64 #include <sys/dmu_tx.h>
65 #include <sys/dmu_objset.h>
66 #include <sys/dsl_prop.h>
67 #include <sys/sa_impl.h>
68 #include <sys/txg.h>
69
70 #define OSD_OI_FID_NR         (1UL << 7)
71 #define OSD_OI_FID_NR_MAX     (1UL << OSD_OI_FID_OID_BITS_MAX)
72 unsigned int osd_oi_count = OSD_OI_FID_NR;
73
74
75 /*
76  * zfs osd maintains names for known fids in the name hierarchy
77  * so that one can mount filesystem with regular ZFS stack and
78  * access files
79  */
80 struct named_oid {
81         unsigned long    oid;
82         char            *name;
83 };
84
85 static const struct named_oid oids[] = {
86         { LAST_RECV_OID,                LAST_RCVD },
87         { OFD_LAST_GROUP_OID,           "LAST_GROUP" },
88         { LLOG_CATALOGS_OID,            "CATALOGS" },
89         { MGS_CONFIGS_OID,              NULL /*MOUNT_CONFIGS_DIR*/ },
90         { FID_SEQ_SRV_OID,              "seq_srv" },
91         { FID_SEQ_CTL_OID,              "seq_ctl" },
92         { FLD_INDEX_OID,                "fld" },
93         { MDD_LOV_OBJ_OID,              LOV_OBJID },
94         { OFD_HEALTH_CHECK_OID,         HEALTH_CHECK },
95         { ACCT_USER_OID,                "acct_usr_inode" },
96         { ACCT_GROUP_OID,               "acct_grp_inode" },
97         { REPLY_DATA_OID,               REPLY_DATA },
98         { 0,                            NULL }
99 };
100
101 static char *oid2name(const unsigned long oid)
102 {
103         int i = 0;
104
105         while (oids[i].oid) {
106                 if (oids[i].oid == oid)
107                         return oids[i].name;
108                 i++;
109         }
110         return NULL;
111 }
112
113 /**
114  * Lookup an existing OI by the given name.
115  */
116 static int
117 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
118               uint64_t parent, const char *name, struct osd_oi *oi)
119 {
120         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
121         int                      rc;
122
123         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
124         if (rc)
125                 return rc;
126
127         rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
128         if (rc >= sizeof(oi->oi_name))
129                 return -E2BIG;
130
131         rc = 0;
132         oi->oi_zapid = zde->zde_dnode;
133
134         return rc;
135 }
136
137 /**
138  * Create a new OI with the given name.
139  */
140 static int
141 osd_oi_create(const struct lu_env *env, struct osd_device *o,
142               uint64_t parent, const char *name, uint64_t *child)
143 {
144         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
145         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
146         dmu_buf_t               *db;
147         dmu_tx_t                *tx;
148         int                      rc;
149
150         /* verify it doesn't already exist */
151         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
152         if (rc == 0)
153                 return -EEXIST;
154
155         /* create fid-to-dnode index */
156         tx = dmu_tx_create(o->od_os);
157         if (tx == NULL)
158                 return -ENOMEM;
159
160         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
161         dmu_tx_hold_bonus(tx, parent);
162         dmu_tx_hold_zap(tx, parent, TRUE, name);
163         LASSERT(tx->tx_objset->os_sa);
164         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
165
166         rc = -dmu_tx_assign(tx, TXG_WAIT);
167         if (rc) {
168                 dmu_tx_abort(tx);
169                 return rc;
170         }
171
172         la->la_valid = LA_MODE | LA_UID | LA_GID;
173         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
174         la->la_uid = la->la_gid = 0;
175         __osd_zap_create(env, o, &db, tx, la, parent, 0);
176
177         zde->zde_dnode = db->db_object;
178         zde->zde_pad = 0;
179         zde->zde_type = IFTODT(S_IFDIR);
180
181         rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
182
183         dmu_tx_commit(tx);
184
185         *child = db->db_object;
186         sa_buf_rele(db, osd_obj_tag);
187
188         return rc;
189 }
190
191 static int
192 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
193                       uint64_t parent, const char *name, uint64_t *child)
194 {
195         struct osd_oi   oi;
196         int             rc;
197
198         rc = osd_oi_lookup(env, o, parent, name, &oi);
199         if (rc == 0)
200                 *child = oi.oi_zapid;
201         else if (rc == -ENOENT)
202                 rc = osd_oi_create(env, o, parent, name, child);
203
204         return rc;
205 }
206
207 /**
208  * Lookup the target index/flags of the fid, so it will know where
209  * the object is located (tgt index) and it is MDT or OST object.
210  */
211 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
212                    u64 seq, struct lu_seq_range *range)
213 {
214         struct seq_server_site  *ss = osd_seq_site(osd);
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         /* OSD will only do local fld lookup */
234         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
235 }
236
237 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
238                   const struct lu_fid *fid)
239 {
240         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
241         int                     rc;
242         ENTRY;
243
244         if (fid_is_idif(fid))
245                 RETURN(1);
246
247         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
248                      fid_is_name_llog(fid) || fid_is_quota(fid))
249                 RETURN(0);
250
251         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
252         if (rc != 0) {
253                 if (rc != -ENOENT)
254                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
255                                osd_name(osd), PFID(fid), rc);
256                 RETURN(0);
257         }
258
259         if (fld_range_is_ost(range))
260                 RETURN(1);
261
262         RETURN(0);
263 }
264
265 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
266                                            u64 seq)
267 {
268         struct osd_seq *osd_seq;
269
270         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
271                 if (osd_seq->os_seq == seq)
272                         return osd_seq;
273         }
274         return NULL;
275 }
276
277 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
278 {
279         struct osd_seq *osd_seq;
280
281         read_lock(&seq_list->osl_seq_list_lock);
282         osd_seq = osd_seq_find_locked(seq_list, seq);
283         read_unlock(&seq_list->osl_seq_list_lock);
284
285         return osd_seq;
286 }
287
288 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
289                                            struct osd_device *osd, u64 seq)
290 {
291         struct osd_seq_list     *seq_list = &osd->od_seq_list;
292         struct osd_seq          *osd_seq;
293         char                    *key = osd_oti_get(env)->oti_buf;
294         char                    *seq_name = osd_oti_get(env)->oti_str;
295         struct osd_oi           oi;
296         uint64_t                sdb, odb;
297         int                     i;
298         int                     rc = 0;
299         ENTRY;
300
301         osd_seq = osd_seq_find(seq_list, seq);
302         if (osd_seq != NULL)
303                 RETURN(osd_seq);
304
305         down(&seq_list->osl_seq_init_sem);
306         /* Check again, in case some one else already add it
307          * to the list */
308         osd_seq = osd_seq_find(seq_list, seq);
309         if (osd_seq != NULL)
310                 GOTO(out, rc = 0);
311
312         OBD_ALLOC_PTR(osd_seq);
313         if (osd_seq == NULL)
314                 GOTO(out, rc = -ENOMEM);
315
316         INIT_LIST_HEAD(&osd_seq->os_seq_list);
317         osd_seq->os_seq = seq;
318
319         /* Init subdir count to be 32, but each seq can have
320          * different subdir count */
321         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
322         OBD_ALLOC(osd_seq->os_compat_dirs,
323                   sizeof(uint64_t) * osd_seq->os_subdir_count);
324         if (osd_seq->os_compat_dirs == NULL)
325                 GOTO(out, rc = -ENOMEM);
326
327         oi.oi_zapid = osd->od_O_id;
328         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
329                 fid_seq_is_mdt0(seq)) ?  LPU64 : LPX64i,
330                 fid_seq_is_idif(seq) ? 0 : seq);
331
332         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
333         if (rc != 0) {
334                 CERROR("%s: Can not create %s : rc = %d\n",
335                        osd_name(osd), seq_name, rc);
336                 GOTO(out, rc);
337         }
338
339         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
340                 sprintf(key, "d%d", i);
341                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
342                 if (rc)
343                         GOTO(out, rc);
344                 osd_seq->os_compat_dirs[i] = sdb;
345         }
346
347         write_lock(&seq_list->osl_seq_list_lock);
348         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
349         write_unlock(&seq_list->osl_seq_list_lock);
350 out:
351         up(&seq_list->osl_seq_init_sem);
352         if (rc != 0) {
353                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
354                         OBD_FREE(osd_seq->os_compat_dirs,
355                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
356                 if (osd_seq != NULL)
357                         OBD_FREE_PTR(osd_seq);
358                 osd_seq = ERR_PTR(rc);
359         }
360         RETURN(osd_seq);
361 }
362
363 /*
364  * objects w/o a natural reference (unlike a file on a MDS)
365  * are put under a special hierarchy /O/<seq>/d0..dXX
366  * this function returns a directory specific fid belongs to
367  */
368 static uint64_t
369 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
370                         const struct lu_fid *fid, char *buf)
371 {
372         struct osd_seq  *osd_seq;
373         unsigned long   b;
374         u64             id;
375         int             rc;
376
377         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
378         if (IS_ERR(osd_seq)) {
379                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
380                        PFID(fid));
381                 return PTR_ERR(osd_seq);
382         }
383
384         if (fid_is_last_id(fid)) {
385                 id = 0;
386         } else {
387                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
388                 LASSERT(rc == 0); /* we should not get here with IGIF */
389                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
390         }
391
392         b = id % OSD_OST_MAP_SIZE;
393         LASSERT(osd_seq->os_compat_dirs[b]);
394
395         sprintf(buf, LPU64, id);
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_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         if (rc == 0)
491                 dmu_prefetch(dev->od_os, *oid, 0, 0);
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         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         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
636                                  os_seq_list) {
637                 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         o->od_O_id = sdb;
662
663         osd_ost_seq_init(env, o);
664         /* Create on-disk indexes to maintain per-UID/GID inode usage.
665          * Those new indexes are created in the top-level ZAP outside the
666          * namespace in order not to confuse ZPL which might interpret those
667          * indexes as directories and assume the values are object IDs */
668         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
669                         oid2name(ACCT_USER_OID), &odb);
670         if (rc)
671                 RETURN(rc);
672         o->od_iusr_oid = odb;
673
674         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
675                         oid2name(ACCT_GROUP_OID), &odb);
676         if (rc)
677                 RETURN(rc);
678         o->od_igrp_oid = odb;
679
680         RETURN(rc);
681 }
682
683 static char *root2convert = "ROOT";
684 /*
685  * due to DNE requirements we have to change sequence of /ROOT object
686  * so that it doesn't belong to the local sequence FID_SEQ_LOCAL_FILE
687  * but a normal sequence living on MDS#0
688  * this is the sole purpose of this function.
689  *
690  * This is only needed for pre-production 2.4 ZFS filesystems, and
691  * can be removed in the future.
692  */
693 int osd_convert_root_to_new_seq(const struct lu_env *env, struct osd_device *o)
694 {
695         struct luz_direntry *lze = &osd_oti_get(env)->oti_zde;
696         char                *buf = osd_oti_get(env)->oti_str;
697         struct lu_fid        newfid;
698         uint64_t             zapid;
699         dmu_tx_t            *tx = NULL;
700         int                  rc;
701         ENTRY;
702
703         /* ignore OSTs */
704         if (strstr(o->od_svname, "MDT") == NULL)
705                 RETURN(0);
706
707         /* lookup /ROOT */
708         rc = -zap_lookup(o->od_os, o->od_root, root2convert, 8,
709                          sizeof(*lze) / 8, (void *)lze);
710         /* doesn't exist or let actual user to handle the error */
711         if (rc)
712                 RETURN(0);
713
714         CDEBUG(D_OTHER, "%s: /ROOT -> "DFID" -> "LPU64"\n", o->od_svname,
715                PFID(&lze->lzd_fid), (long long int) lze->lzd_reg.zde_dnode);
716
717         /* already right one? */
718         if (fid_seq(&lze->lzd_fid) == FID_SEQ_ROOT)
719                 return 0;
720
721         tx = dmu_tx_create(o->od_os);
722         if (tx == NULL)
723                 return -ENOMEM;
724
725         dmu_tx_hold_bonus(tx, o->od_root);
726
727         /* declare delete/insert of the name */
728         dmu_tx_hold_zap(tx, o->od_root, TRUE, root2convert);
729         dmu_tx_hold_zap(tx, o->od_root, FALSE, root2convert);
730
731         /* declare that we'll remove object from fid-dnode mapping */
732         zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
733         dmu_tx_hold_bonus(tx, zapid);
734         dmu_tx_hold_zap(tx, zapid, FALSE, buf);
735
736         /* declare that we'll add object to fid-dnode mapping */
737         newfid.f_seq = FID_SEQ_ROOT;
738         newfid.f_oid = FID_OID_ROOT;
739         newfid.f_ver = 0;
740         zapid = osd_get_name_n_idx(env, o, &newfid, buf);
741         dmu_tx_hold_bonus(tx, zapid);
742         dmu_tx_hold_zap(tx, zapid, TRUE, buf);
743
744         rc = -dmu_tx_assign(tx, TXG_WAIT);
745         if (rc)
746                 GOTO(err, rc);
747
748         rc = -zap_remove(o->od_os, o->od_root, root2convert, tx);
749         if (rc)
750                 GOTO(err, rc);
751
752         /* remove from OI */
753         zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
754         rc = -zap_remove(o->od_os, zapid, buf, tx);
755         if (rc)
756                 GOTO(err, rc);
757
758         lze->lzd_fid = newfid;
759         rc = -zap_add(o->od_os, o->od_root, root2convert,
760                       8, sizeof(*lze) / 8, (void *)lze, tx);
761         if (rc)
762                 GOTO(err, rc);
763
764         /* add to OI with the new fid */
765         zapid = osd_get_name_n_idx(env, o, &newfid, buf);
766         rc = -zap_add(o->od_os, zapid, buf, 8, 1, &lze->lzd_reg, tx);
767         if (rc)
768                 GOTO(err, rc);
769
770
771         /* LMA will be updated in mdd_compat_fixes */
772         dmu_tx_commit(tx);
773
774         RETURN(rc);
775
776 err:
777         if (tx)
778                 dmu_tx_abort(tx);
779         CERROR("%s: can't convert to new fid: rc = %d\n", o->od_svname, rc);
780         RETURN(rc);
781 }
782
783 /**
784  * Initialize the OIs by either opening or creating them as needed.
785  */
786 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
787 {
788         char    *key = osd_oti_get(env)->oti_buf;
789         int      i, rc, count = 0;
790         ENTRY;
791
792         rc = osd_oi_probe(env, o, &count);
793         if (rc)
794                 RETURN(rc);
795
796         if (count == 0) {
797                 uint64_t odb, sdb;
798
799                 count = osd_oi_count;
800                 odb = o->od_root;
801
802                 for (i = 0; i < count; i++) {
803                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
804                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
805                         if (rc)
806                                 RETURN(rc);
807                 }
808         }
809
810         rc = osd_oi_init_compat(env, o);
811         if (rc)
812                 RETURN(rc);
813
814         LASSERT((count & (count - 1)) == 0);
815         o->od_oi_count = count;
816         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
817         if (o->od_oi_table == NULL)
818                 RETURN(-ENOMEM);
819
820         rc = osd_oi_open_table(env, o, count);
821         if (rc) {
822                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
823                 o->od_oi_table = NULL;
824         }
825
826         RETURN(rc);
827 }
828
829 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
830 {
831         ENTRY;
832
833         osd_ost_seq_fini(env, o);
834
835         if (o->od_oi_table != NULL) {
836                 (void) osd_oi_close_table(env, o);
837                 OBD_FREE(o->od_oi_table,
838                          sizeof(struct osd_oi *) * o->od_oi_count);
839                 o->od_oi_table = NULL;
840                 o->od_oi_count = 0;
841         }
842
843         EXIT;
844 }
845
846 int osd_options_init(void)
847 {
848         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
849         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
850                 osd_oi_count = OSD_OI_FID_NR;
851
852         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
853                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
854                         osd_oi_count, size_roundup_power2(osd_oi_count));
855                 osd_oi_count = size_roundup_power2(osd_oi_count);
856         }
857
858         return 0;
859 }