4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd-zfs/osd_oi.c
33 * OI functions to map fid to dnode
35 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
36 * Author: Mike Pershin <tappro@whamcloud.com>
37 * Author: Di Wang <di.wang@intel.com>
40 #define DEBUG_SUBSYSTEM S_OSD
42 #include <libcfs/libcfs.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
46 #include <obd_class.h>
47 #include <lustre_disk.h>
48 #include <lustre_fid.h>
50 #include "osd_internal.h"
52 #include <sys/dnode.h>
57 #include <sys/spa_impl.h>
58 #include <sys/zfs_znode.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/sa_impl.h>
64 #include <lustre_scrub.h>
66 #define OSD_OI_FID_NR (1UL << 7)
67 unsigned int osd_oi_count = OSD_OI_FID_NR;
71 * zfs osd maintains names for known fids in the name hierarchy
72 * so that one can mount filesystem with regular ZFS stack and
80 static const struct named_oid oids[] = {
81 { .oid = LAST_RECV_OID, .name = LAST_RCVD },
82 { .oid = OFD_LAST_GROUP_OID, .name = "LAST_GROUP" },
83 { .oid = LLOG_CATALOGS_OID, .name = "CATALOGS" },
84 { .oid = MGS_CONFIGS_OID, /*MOUNT_CONFIGS_DIR*/ },
85 { .oid = FID_SEQ_SRV_OID, .name = "seq_srv" },
86 { .oid = FID_SEQ_CTL_OID, .name = "seq_ctl" },
87 { .oid = FLD_INDEX_OID, .name = "fld" },
88 { .oid = MDD_LOV_OBJ_OID, .name = LOV_OBJID },
89 { .oid = OFD_HEALTH_CHECK_OID, .name = HEALTH_CHECK },
90 { .oid = REPLY_DATA_OID, .name = REPLY_DATA },
91 { .oid = MDD_LOV_OBJ_OSEQ, .name = LOV_OBJSEQ },
92 { .oid = BATCHID_COMMITTED_OID, .name = "BATCHID" },
96 static inline bool fid_is_objseq(const struct lu_fid *fid)
98 return fid->f_seq == FID_SEQ_LOCAL_FILE &&
99 fid->f_oid == MDD_LOV_OBJ_OSEQ;
102 static inline bool fid_is_batchid(const struct lu_fid *fid)
104 return fid->f_seq == FID_SEQ_LOCAL_FILE &&
105 fid->f_oid == BATCHID_COMMITTED_OID;
108 static char *oid2name(const unsigned long oid)
112 while (oids[i].oid) {
113 if (oids[i].oid == oid)
121 * Lookup an existing OI by the given name.
124 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
125 uint64_t parent, const char *name, struct osd_oi *oi)
127 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
130 rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
134 rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
135 if (rc >= sizeof(oi->oi_name))
138 oi->oi_zapid = zde->zde_dnode;
143 static int osd_obj_create(const struct lu_env *env, struct osd_device *o,
144 uint64_t parent, const char *name, uint64_t *child,
145 const struct lu_fid *fid, bool isdir)
147 struct osd_thread_info *info = osd_oti_get(env);
148 struct zpl_direntry *zde = &info->oti_zde.lzd_reg;
149 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
150 struct lu_attr *la = &info->oti_la;
151 sa_handle_t *sa_hdl = NULL;
152 nvlist_t *nvbuf = NULL;
155 __u32 compat = LMAC_NOT_IN_OI;
159 if (o->od_dt_dev.dd_rdonly)
162 memset(la, 0, sizeof(*la));
163 la->la_valid = LA_MODE | LA_UID | LA_GID;
164 la->la_mode = S_IRUGO | S_IWUSR | (isdir ? S_IXUGO | S_IFDIR : S_IFREG);
167 rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
172 compat |= LMAC_FID_ON_OST;
173 lustre_lma_init(lma, fid, compat, 0);
174 lustre_lma_swab(lma);
175 rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA,
176 (uchar_t *)lma, sizeof(*lma));
181 /* create fid-to-dnode index */
182 tx = dmu_tx_create(o->od_os);
184 GOTO(out, rc = -ENOMEM);
186 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
187 dmu_tx_hold_bonus(tx, parent);
188 dmu_tx_hold_zap(tx, parent, TRUE, name);
189 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
190 rc = -dmu_tx_assign(tx, TXG_WAIT);
197 oid = osd_zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
198 DMU_OT_DIRECTORY_CONTENTS,
199 14, DN_MAX_INDBLKSHIFT, 0, tx);
201 oid = osd_dmu_object_alloc(o->od_os, DMU_OTN_UINT8_METADATA,
203 rc = -sa_handle_get(o->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
207 rc = __osd_attr_init(env, o, NULL, sa_hdl, tx, la, parent, nvbuf);
208 sa_handle_destroy(sa_hdl);
212 zde->zde_dnode = oid;
214 zde->zde_type = IFTODT(isdir ? S_IFDIR : S_IFREG);
215 rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
221 dmu_object_free(o->od_os, oid, tx);
232 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
233 uint64_t parent, const char *name, uint64_t *child)
238 rc = osd_oi_lookup(env, o, parent, name, &oi);
240 *child = oi.oi_zapid;
241 else if (rc == -ENOENT)
242 rc = osd_obj_create(env, o, parent, name, child, NULL, true);
247 int osd_obj_find_or_create(const struct lu_env *env, struct osd_device *o,
248 uint64_t parent, const char *name, uint64_t *child,
249 const struct lu_fid *fid, bool isdir)
254 rc = osd_oi_lookup(env, o, parent, name, &oi);
256 *child = oi.oi_zapid;
257 else if (rc == -ENOENT)
258 rc = osd_obj_create(env, o, parent, name, child, fid, isdir);
264 * Lookup the target index/flags of the fid, so it will know where
265 * the object is located (tgt index) and it is MDT or OST object.
267 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
268 u64 seq, struct lu_seq_range *range)
270 struct seq_server_site *ss = osd_seq_site(osd);
272 if (fid_seq_is_idif(seq)) {
273 fld_range_set_ost(range);
274 range->lsr_index = idif_ost_idx(seq);
278 if (!fid_seq_in_fldb(seq)) {
279 fld_range_set_mdt(range);
281 /* FIXME: If ss is NULL, it suppose not get lsr_index
283 range->lsr_index = ss->ss_node_id;
287 /* The seq_server_site may be NOT ready during initial OI scrub */
288 if (unlikely(!ss || !ss->ss_server_fld ||
289 !ss->ss_server_fld->lsf_cache))
292 fld_range_set_any(range);
293 /* OSD will only do local fld lookup */
294 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
297 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
298 const struct lu_fid *fid)
300 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
304 if (fid_is_idif(fid))
307 if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
308 fid_is_name_llog(fid) || fid_is_quota(fid) ||
312 rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
314 /* During upgrade, OST FLDB might not be loaded because
315 * OST FLDB is not created until 2.6, so if some DNE
316 * filesystem upgrade from 2.5 to 2.7/2.8, they will
317 * not be able to find the sequence from local FLDB
318 * cache see fld_index_init(). */
319 if (rc == -ENOENT && osd->od_is_ost)
323 CERROR("%s: "DFID" lookup failed: rc = %d\n",
324 osd_name(osd), PFID(fid), rc);
328 if (fld_range_is_ost(range))
334 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
337 struct osd_seq *osd_seq;
339 list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
340 if (osd_seq->os_seq == seq)
346 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
348 struct osd_seq *osd_seq;
350 read_lock(&seq_list->osl_seq_list_lock);
351 osd_seq = osd_seq_find_locked(seq_list, seq);
352 read_unlock(&seq_list->osl_seq_list_lock);
357 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
358 struct osd_device *osd, u64 seq)
360 struct osd_seq_list *seq_list = &osd->od_seq_list;
361 struct osd_seq *osd_seq;
362 char *key = osd_oti_get(env)->oti_buf;
363 char *seq_name = osd_oti_get(env)->oti_str;
370 osd_seq = osd_seq_find(seq_list, seq);
374 down(&seq_list->osl_seq_init_sem);
375 /* Check again, in case some one else already add it
377 osd_seq = osd_seq_find(seq_list, seq);
381 OBD_ALLOC_PTR(osd_seq);
383 GOTO(out, rc = -ENOMEM);
385 INIT_LIST_HEAD(&osd_seq->os_seq_list);
386 osd_seq->os_seq = seq;
388 /* Init subdir count to be 32, but each seq can have
389 * different subdir count */
390 osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
391 OBD_ALLOC_PTR_ARRAY(osd_seq->os_compat_dirs, osd_seq->os_subdir_count);
392 if (osd_seq->os_compat_dirs == NULL)
393 GOTO(out, rc = -ENOMEM);
395 oi.oi_zapid = osd->od_O_id;
396 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
397 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
398 fid_seq_is_idif(seq) ? 0 : seq);
400 rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
402 CERROR("%s: Can not create %s : rc = %d\n",
403 osd_name(osd), seq_name, rc);
407 osd_seq->os_oid = odb;
408 for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
409 sprintf(key, "d%d", i);
410 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
413 osd_seq->os_compat_dirs[i] = sdb;
416 write_lock(&seq_list->osl_seq_list_lock);
417 list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
418 write_unlock(&seq_list->osl_seq_list_lock);
420 up(&seq_list->osl_seq_init_sem);
422 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
423 OBD_FREE_PTR_ARRAY(osd_seq->os_compat_dirs,
424 osd_seq->os_subdir_count);
426 OBD_FREE_PTR(osd_seq);
427 osd_seq = ERR_PTR(rc);
433 osd_get_idx_for_ost_obj_compat(const struct lu_env *env, struct osd_device *osd,
434 const struct lu_fid *fid, char *buf, int bufsize)
436 struct osd_seq *osd_seq;
441 osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
442 if (IS_ERR(osd_seq)) {
443 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
445 return PTR_ERR(osd_seq);
448 if (fid_is_last_id(fid)) {
451 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
452 LASSERT(rc == 0); /* we should not get here with IGIF */
453 id = ostid_id(&osd_oti_get(env)->oti_ostid);
456 b = id % OSD_OST_MAP_SIZE;
457 LASSERT(osd_seq->os_compat_dirs[b]);
460 snprintf(buf, bufsize, "%llu", id);
462 return osd_seq->os_compat_dirs[b];
466 * objects w/o a natural reference (unlike a file on a MDS)
467 * are put under a special hierarchy /O/<seq>/d0..dXX
468 * this function returns a directory specific fid belongs to
471 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
472 const struct lu_fid *fid, char *buf, int bufsize)
474 struct osd_seq *osd_seq;
479 osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
480 if (IS_ERR(osd_seq)) {
481 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
483 return PTR_ERR(osd_seq);
486 if (fid_is_last_id(fid)) {
488 snprintf(buf, bufsize, "LAST_ID");
490 return osd_seq->os_oid;
493 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
494 LASSERT(rc == 0); /* we should not get here with IGIF */
496 id = ostid_id(&osd_oti_get(env)->oti_ostid);
497 b = id % OSD_OST_MAP_SIZE;
498 LASSERT(osd_seq->os_compat_dirs[b]);
501 snprintf(buf, bufsize, "%llu", id);
503 return osd_seq->os_compat_dirs[b];
507 * Determine the zap object id which is being used as the OI for the
508 * given fid. The lowest N bits in the sequence ID are used as the
509 * index key. On failure 0 is returned which zfs treats internally
510 * as an invalid object id.
513 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
514 char *buf, dnode_t **zdn, int bufsize)
518 oi = osd_fid2oi(osd, fid);
520 osd_fid2str(buf, fid, bufsize);
528 osd_get_name_n_idx_compat(const struct lu_env *env, struct osd_device *osd,
529 const struct lu_fid *fid, char *buf, int bufsize,
535 LASSERT(!fid_is_acct(fid));
540 if (fid_is_echo(fid) || fid_is_on_ost(env, osd, fid)) {
541 zapid = osd_get_idx_for_ost_obj_compat(env, osd, fid,
543 } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
544 /* special objects with fixed known fids get their name */
545 char *name = oid2name(fid_oid(fid));
548 zapid = osd->od_root;
550 strncpy(buf, name, bufsize);
552 zapid = osd_get_idx_for_fid(osd, fid, buf, NULL,
556 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn, bufsize);
562 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
563 const struct lu_fid *fid, char *buf, int bufsize,
569 LASSERT(!fid_is_acct(fid));
574 if (fid_is_echo(fid) || fid_is_last_id(fid) ||
575 fid_is_on_ost(env, osd, fid)) {
576 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
577 } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
578 /* special objects with fixed known fids get their name */
579 char *name = oid2name(fid_oid(fid));
582 zapid = osd->od_root;
584 strncpy(buf, name, bufsize);
586 zapid = osd_get_idx_for_fid(osd, fid, buf, NULL,
590 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn, bufsize);
596 static inline int fid_is_fs_root(const struct lu_fid *fid)
598 /* Map root inode to special local object FID */
599 return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
600 fid_oid(fid) == OSD_FS_ROOT_OID;
603 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
604 const struct lu_fid *fid, uint64_t *oid)
606 struct osd_thread_info *info = osd_oti_get(env);
607 char *buf = info->oti_buf;
613 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
616 LASSERT(!fid_is_acct(fid));
618 if (unlikely(fid_is_fs_root(fid))) {
621 zapid = osd_get_name_n_idx(env, dev, fid, buf,
622 sizeof(info->oti_buf), &zdn);
623 rc = osd_zap_lookup(dev, zapid, zdn, buf,
624 8, 1, &info->oti_zde);
626 if (unlikely(fid_is_last_id(fid))) {
627 zapid = osd_get_name_n_idx_compat(env, dev, fid,
628 buf, sizeof(info->oti_buf), &zdn);
629 rc = osd_zap_lookup(dev, zapid, zdn, buf,
630 8, 1, &info->oti_zde);
631 } else if (fid_is_objseq(fid) || fid_is_batchid(fid)) {
632 zapid = osd_get_idx_for_fid(dev, fid,
633 buf, NULL, sizeof(info->oti_buf));
634 rc = osd_zap_lookup(dev, zapid, zdn, buf,
635 8, 1, &info->oti_zde);
641 *oid = info->oti_zde.lzd_reg.zde_dnode;
645 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
646 ZIO_PRIORITY_ASYNC_READ);
652 * Close an entry in a specific slot.
655 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
659 LASSERT(key < o->od_oi_count);
661 oi = o->od_oi_table[key];
664 osd_dnode_rele(oi->oi_dn);
666 o->od_oi_table[key] = NULL;
671 * Allocate and open a new entry in the specified unused slot.
674 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
680 LASSERT(key < o->od_oi_count);
681 LASSERT(o->od_oi_table[key] == NULL);
687 rc = osd_oi_lookup(env, o, o->od_root, name, oi);
693 o->od_oi_table[key] = oi;
694 __osd_obj2dnode(o->od_os, oi->oi_zapid, &oi->oi_dn);
700 * Depopulate the OI table.
703 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
707 for (i = 0; i < o->od_oi_count; i++)
708 osd_oi_remove_table(env, o, i);
712 * Populate the OI table based.
715 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
721 for (i = 0; i < count; i++) {
722 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
723 rc = osd_oi_add_table(env, o, name, i);
725 osd_oi_close_table(env, o);
734 * Determine if the type and number of OIs used by this file system.
736 static int osd_oi_probe(const struct lu_env *env, struct osd_device *o)
738 struct lustre_scrub *scrub = &o->od_scrub;
739 struct scrub_file *sf = &scrub->os_file;
742 int max = sf->sf_oi_count > 0 ? sf->sf_oi_count : OSD_OI_FID_NR_MAX;
748 * Check for multiple OIs and determine the count. There is no
749 * gap handling, if an OI is missing the wrong size can be returned.
750 * The only safeguard is that we know the number of OIs must be a
751 * power of two and this is checked for basic sanity.
753 for (count = 0; count < max; count++) {
754 snprintf(name, sizeof(name) - 1, "%s.%d",
755 DMU_OSD_OI_NAME_BASE, count);
756 rc = osd_oi_lookup(env, o, o->od_root, name, &oi);
761 if (sf->sf_oi_count == 0)
764 zfs_set_bit(count, sf->sf_oi_bitmap);
775 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
777 struct osd_seq_list *osl = &osd->od_seq_list;
778 struct osd_seq *osd_seq, *tmp;
780 write_lock(&osl->osl_seq_list_lock);
781 list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
783 list_del(&osd_seq->os_seq_list);
784 OBD_FREE_PTR_ARRAY(osd_seq->os_compat_dirs,
785 osd_seq->os_subdir_count);
786 OBD_FREE(osd_seq, sizeof(*osd_seq));
788 write_unlock(&osl->osl_seq_list_lock);
792 * Create /O subdirectory to map legacy OST objects for compatibility.
795 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
801 rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
809 osd_oi_init_index_backup(const struct lu_env *env, struct osd_device *o)
811 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
815 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
816 rc = osd_obj_find_or_create(env, o, o->od_root, INDEX_BACKUP_DIR,
817 &o->od_index_backup_id, fid, true);
823 osd_oi_init_remote_parent(const struct lu_env *env, struct osd_device *o)
830 o->od_remote_parent_dir = ZFS_NO_OBJECT;
832 /* Remote parent only used for cross-MDT objects,
833 * it is usless for single MDT case or under read
834 * only mode. So ignore the failure. */
835 rc = osd_oi_find_or_create(env, o, o->od_root,
836 REMOTE_PARENT_DIR, &sdb);
838 o->od_remote_parent_dir = sdb;
840 o->od_remote_parent_dir = ZFS_NO_OBJECT;
845 * Initialize the OIs by either opening or creating them as needed.
847 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
849 struct lustre_scrub *scrub = &o->od_scrub;
850 struct scrub_file *sf = &scrub->os_file;
851 char *key = osd_oti_get(env)->oti_buf;
856 LASSERTF((sf->sf_oi_count & (sf->sf_oi_count - 1)) == 0,
857 "Invalid OI count in scrub file %d\n", sf->sf_oi_count);
859 rc = osd_oi_init_index_backup(env, o);
863 osd_oi_init_remote_parent(env, o);
865 rc = osd_oi_init_compat(env, o);
869 count = osd_oi_probe(env, o);
871 GOTO(out, rc = count);
874 if (count == sf->sf_oi_count)
877 if (sf->sf_oi_count == 0) {
878 if (likely((count & (count - 1)) == 0)) {
879 sf->sf_oi_count = count;
880 rc = scrub_file_store(env, scrub);
887 LCONSOLE_ERROR("%s: invalid oi count %d. You can "
888 "remove all OIs, then remount it\n",
890 GOTO(out, rc = -EDOM);
893 scrub_file_reset(scrub, o->od_uuid, SF_RECREATED);
894 count = sf->sf_oi_count;
896 if (sf->sf_oi_count > 0) {
897 count = sf->sf_oi_count;
898 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
899 for (i = 0; i < count; i++)
900 zfs_set_bit(i, sf->sf_oi_bitmap);
901 scrub_file_reset(scrub, o->od_uuid, SF_RECREATED);
903 count = sf->sf_oi_count = osd_oi_count;
907 rc = scrub_file_store(env, scrub);
911 for (i = 0; i < count; i++) {
912 LASSERT(sizeof(osd_oti_get(env)->oti_buf) >= 32);
914 snprintf(key, sizeof(osd_oti_get(env)->oti_buf) - 1,
915 "%s.%d", DMU_OSD_OI_NAME_BASE, i);
916 rc = osd_oi_find_or_create(env, o, o->od_root, key, &sdb);
922 LASSERT((count & (count - 1)) == 0);
923 o->od_oi_count = count;
924 OBD_ALLOC_PTR_ARRAY(o->od_oi_table, count);
925 if (o->od_oi_table == NULL)
926 GOTO(out, rc = -ENOMEM);
928 rc = osd_oi_open_table(env, o, count);
934 osd_ost_seq_fini(env, o);
936 if (o->od_oi_table) {
937 OBD_FREE_PTR_ARRAY(o->od_oi_table, count);
938 o->od_oi_table = NULL;
945 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
949 osd_ost_seq_fini(env, o);
951 if (o->od_oi_table != NULL) {
952 (void) osd_oi_close_table(env, o);
953 OBD_FREE_PTR_ARRAY(o->od_oi_table, o->od_oi_count);
954 o->od_oi_table = NULL;
961 int osd_options_init(void)
963 /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
964 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
965 osd_oi_count = OSD_OI_FID_NR;
967 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
968 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
969 osd_oi_count, size_roundup_power2(osd_oi_count));
970 osd_oi_count = size_roundup_power2(osd_oi_count);
977 * the following set of functions are used to maintain per-thread
978 * cache of FID->ino mapping. this mechanism is used to avoid
979 * expensive LU/OI lookups.
981 struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
982 struct osd_device *osd,
983 const struct lu_fid *fid)
985 struct osd_thread_info *oti = osd_oti_get(env);
986 struct osd_idmap_cache *idc = oti->oti_ins_cache;
989 for (i = 0; i < oti->oti_ins_cache_used; i++) {
990 if (!lu_fid_eq(&idc[i].oic_fid, fid))
992 if (idc[i].oic_dev != osd)
1001 struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
1002 struct osd_device *osd,
1003 const struct lu_fid *fid)
1005 struct osd_thread_info *oti = osd_oti_get(env);
1006 struct osd_idmap_cache *idc;
1009 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
1010 i = oti->oti_ins_cache_size * 2;
1012 i = OSD_INS_CACHE_SIZE;
1013 OBD_ALLOC_PTR_ARRAY_LARGE(idc, i);
1015 return ERR_PTR(-ENOMEM);
1016 if (oti->oti_ins_cache != NULL) {
1017 memcpy(idc, oti->oti_ins_cache,
1018 oti->oti_ins_cache_used * sizeof(*idc));
1019 OBD_FREE_PTR_ARRAY_LARGE(oti->oti_ins_cache,
1020 oti->oti_ins_cache_used);
1022 oti->oti_ins_cache = idc;
1023 oti->oti_ins_cache_size = i;
1026 idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
1027 idc->oic_fid = *fid;
1030 idc->oic_remote = 0;
1036 * Lookup mapping for the given fid in the cache
1038 * Initialize a new one if not found. the initialization checks whether
1039 * the object is local or remote. for the local objects, OI is used to
1040 * learn dnode#. the function is used when the caller has no information
1041 * about the object, e.g. at dt_insert().
1043 struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
1044 struct osd_device *osd,
1045 const struct lu_fid *fid)
1047 struct osd_idmap_cache *idc;
1050 LASSERT(!fid_is_acct(fid));
1052 idc = osd_idc_find(env, osd, fid);
1056 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
1057 osd->od_svname, PFID(fid));
1059 /* new mapping is needed */
1060 idc = osd_idc_add(env, osd, fid);
1062 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
1063 osd->od_svname, PFID(fid), PTR_ERR(idc));
1068 rc = osd_remote_fid(env, osd, fid);
1069 if (unlikely(rc < 0))
1073 /* the object is local, lookup in OI */
1076 rc = osd_fid_lookup(env, osd, fid, &dnode);
1077 if (unlikely(rc < 0)) {
1078 CERROR("%s: can't lookup: rc = %d\n",
1079 osd->od_svname, rc);
1082 LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
1083 idc->oic_dnode = dnode;
1085 /* the object is remote */
1086 idc->oic_remote = 1;
1093 * lookup mapping for given FID and fill it from the given object.
1094 * the object is local by definition.
1096 int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
1097 struct osd_object *obj)
1099 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
1100 struct osd_idmap_cache *idc;
1102 idc = osd_idc_find(env, osd, fid);
1104 if (obj->oo_dn == NULL)
1106 idc->oic_dnode = obj->oo_dn->dn_object;
1110 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
1111 osd->od_svname, PFID(fid));
1113 /* new mapping is needed */
1114 idc = osd_idc_add(env, osd, fid);
1116 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
1117 osd->od_svname, PFID(fid), PTR_ERR(idc));
1118 return PTR_ERR(idc);
1122 idc->oic_dnode = obj->oo_dn->dn_object;
1127 int osd_idc_find_and_init_with_oid(const struct lu_env *env,
1128 struct osd_device *osd,
1129 const struct lu_fid *fid,
1132 struct osd_idmap_cache *idc;
1134 idc = osd_idc_find(env, osd, fid);
1136 idc = osd_idc_add(env, osd, fid);
1138 return PTR_ERR(idc);
1141 idc->oic_dnode = oid;
1142 idc->oic_remote = 0;