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