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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * Copyright (c) 2012, Intel Corporation.
32 * Use is subject to license terms.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd-zfs/osd_oi.c
39 * OI functions to map fid to dnode
41 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42 * Author: Mike Pershin <tappro@whamcloud.com>
43 * Author: Di Wang <di.wang@intel.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_OSD
51 #include <lustre_ver.h>
52 #include <libcfs/libcfs.h>
53 #include <obd_support.h>
54 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
60 #include "osd_internal.h"
62 #include <sys/dnode.h>
67 #include <sys/spa_impl.h>
68 #include <sys/zfs_znode.h>
69 #include <sys/dmu_tx.h>
70 #include <sys/dmu_objset.h>
71 #include <sys/dsl_prop.h>
72 #include <sys/sa_impl.h>
75 static char *oi_tag = "osd_mount, oi";
77 #define OSD_OI_FID_NR (1UL << 7)
78 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
79 unsigned int osd_oi_count = OSD_OI_FID_NR;
83 * zfs osd maintains names for known fids in the name hierarchy
84 * so that one can mount filesystem with regular ZFS stack and
92 static const struct named_oid oids[] = {
93 { LAST_RECV_OID, LAST_RCVD },
94 { OFD_LAST_GROUP_OID, "LAST_GROUP" },
95 { LLOG_CATALOGS_OID, "CATALOGS" },
96 { MGS_CONFIGS_OID, NULL /*MOUNT_CONFIGS_DIR*/ },
97 { FID_SEQ_SRV_OID, "seq_srv" },
98 { FID_SEQ_CTL_OID, "seq_ctl" },
99 { MDD_CAPA_KEYS_OID, NULL /*CAPA_KEYS*/ },
100 { FLD_INDEX_OID, "fld" },
101 { MDD_LOV_OBJ_OID, LOV_OBJID },
102 { OFD_HEALTH_CHECK_OID, HEALTH_CHECK },
103 { ACCT_USER_OID, "acct_usr_inode" },
104 { ACCT_GROUP_OID, "acct_grp_inode" },
105 { MDD_ROOT_INDEX_OID, NULL },
106 { MDD_ORPHAN_OID, NULL },
110 static char *oid2name(const unsigned long oid)
114 while (oids[i].oid) {
115 if (oids[i].oid == oid)
123 * Lookup an existing OI by the given name.
126 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
127 uint64_t parent, const char *name, struct osd_oi *oi)
129 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
132 rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
136 strncpy(oi->oi_name, name, OSD_OI_NAME_SIZE - 1);
137 oi->oi_zapid = zde->zde_dnode;
143 * Create a new OI with the given name.
146 osd_oi_create(const struct lu_env *env, struct osd_device *o,
147 uint64_t parent, const char *name, uint64_t *child)
149 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
150 struct lu_attr *la = &osd_oti_get(env)->oti_la;
155 /* verify it doesn't already exist */
156 rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
160 /* create fid-to-dnode index */
161 tx = dmu_tx_create(o->od_objset.os);
165 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
166 dmu_tx_hold_bonus(tx, parent);
167 dmu_tx_hold_zap(tx, parent, TRUE, name);
168 LASSERT(tx->tx_objset->os_sa);
169 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
171 rc = -dmu_tx_assign(tx, TXG_WAIT);
177 la->la_valid = LA_MODE | LA_UID | LA_GID;
178 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
179 la->la_uid = la->la_gid = 0;
180 __osd_zap_create(env, &o->od_objset, &db, tx, la, oi_tag, 0);
182 zde->zde_dnode = db->db_object;
184 zde->zde_type = IFTODT(S_IFDIR);
186 rc = -zap_add(o->od_objset.os, parent, name, 8, 1, (void *)zde, tx);
190 *child = db->db_object;
191 sa_buf_rele(db, oi_tag);
197 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
198 uint64_t parent, const char *name, uint64_t *child)
203 rc = osd_oi_lookup(env, o, parent, name, &oi);
205 *child = oi.oi_zapid;
206 else if (rc == -ENOENT)
207 rc = osd_oi_create(env, o, parent, name, child);
213 * Lookup the target index/flags of the fid, so it will know where
214 * the object is located (tgt index) and it is MDT or OST object.
216 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
217 const struct lu_fid *fid, struct lu_seq_range *range)
219 struct seq_server_site *ss = osd_seq_site(osd);
222 if (fid_is_idif(fid)) {
223 range->lsr_flags = LU_SEQ_RANGE_OST;
224 range->lsr_index = fid_idif_ost_idx(fid);
228 if (!fid_is_norm(fid)) {
229 range->lsr_flags = LU_SEQ_RANGE_MDT;
231 range->lsr_index = ss->ss_node_id;
236 range->lsr_flags = -1;
237 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
239 CERROR("%s can not find "DFID": rc = %d\n",
240 osd2lu_dev(osd)->ld_obd->obd_name, PFID(fid), rc);
245 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
246 const struct lu_fid *fid)
248 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
252 if (fid_is_idif(fid))
255 rc = osd_fld_lookup(env, osd, fid, range);
257 CERROR("%s: Can not lookup fld for "DFID"\n",
258 osd2lu_dev(osd)->ld_obd->obd_name, PFID(fid));
262 CDEBUG(D_INFO, "fid "DFID" range "DRANGE"\n", PFID(fid),
265 if (range->lsr_flags == LU_SEQ_RANGE_OST)
271 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
274 struct osd_seq *osd_seq;
276 cfs_list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
277 if (osd_seq->os_seq == seq)
283 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list,
286 struct osd_seq *osd_seq;
288 read_lock(&seq_list->osl_seq_list_lock);
289 osd_seq = osd_seq_find_locked(seq_list, seq);
290 read_unlock(&seq_list->osl_seq_list_lock);
295 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
296 struct osd_device *osd, obd_seq seq)
298 struct osd_seq_list *seq_list = &osd->od_seq_list;
299 struct osd_seq *osd_seq;
300 char *key = osd_oti_get(env)->oti_buf;
301 char *seq_name = osd_oti_get(env)->oti_str;
308 osd_seq = osd_seq_find(seq_list, seq);
312 down(&seq_list->osl_seq_init_sem);
313 /* Check again, in case some one else already add it
315 osd_seq = osd_seq_find(seq_list, seq);
319 OBD_ALLOC_PTR(osd_seq);
321 GOTO(out, rc = -ENOMEM);
323 CFS_INIT_LIST_HEAD(&osd_seq->os_seq_list);
324 osd_seq->os_seq = seq;
326 /* Init subdir count to be 32, but each seq can have
327 * different subdir count */
328 osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
329 OBD_ALLOC(osd_seq->os_compat_dirs,
330 sizeof(uint64_t) * osd_seq->os_subdir_count);
331 if (osd_seq->os_compat_dirs == NULL)
332 GOTO(out, rc = -ENOMEM);
334 rc = osd_oi_lookup(env, osd, osd->od_root, "O", &oi);
336 CERROR("%s: Can not find O: rc = %d\n", osd_name(osd), rc);
340 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
341 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
342 fid_seq_is_idif(seq) ? 0 : seq);
344 rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
346 CERROR("%s: Can not create %s : rc = %d\n",
347 osd_name(osd), seq_name, rc);
352 osd->od_ost_compat_grp0 = odb;
354 for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
355 sprintf(key, "d%d", i);
356 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
358 GOTO(out, osd_seq = ERR_PTR(rc));
359 osd_seq->os_compat_dirs[i] = sdb;
362 write_lock(&seq_list->osl_seq_list_lock);
363 cfs_list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
364 write_unlock(&seq_list->osl_seq_list_lock);
366 up(&seq_list->osl_seq_init_sem);
368 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
369 OBD_FREE(osd_seq->os_compat_dirs,
370 sizeof(uint64_t) * osd_seq->os_subdir_count);
372 OBD_FREE_PTR(osd_seq);
373 osd_seq = ERR_PTR(rc);
379 * objects w/o a natural reference (unlike a file on a MDS)
380 * are put under a special hierarchy /O/<seq>/d0..dXX
381 * this function returns a directory specific fid belongs to
384 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
385 const struct lu_fid *fid, char *buf)
387 struct osd_seq *osd_seq;
391 osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
392 if (IS_ERR(osd_seq)) {
393 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
395 return PTR_ERR(osd_seq);
397 rc = fid_ostid_pack(fid, &osd_oti_get(env)->oti_ostid);
398 LASSERT(rc == 0); /* we should not get here with IGIF */
399 b = osd_oti_get(env)->oti_ostid.oi_id % OSD_OST_MAP_SIZE;
400 LASSERT(osd_seq->os_compat_dirs[b]);
402 sprintf(buf, LPU64, osd_oti_get(env)->oti_ostid.oi_id);
404 return osd_seq->os_compat_dirs[b];
407 /* XXX: f_ver is not counted, but may differ too */
408 static void osd_fid2str(char *buf, const struct lu_fid *fid)
410 sprintf(buf, DFID_NOBRACE, PFID(fid));
414 * Determine the zap object id which is being used as the OI for the
415 * given fid. The lowest N bits in the sequence ID are used as the
416 * index key. On failure 0 is returned which zfs treats internally
417 * as an invalid object id.
420 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
425 LASSERT(osd->od_oi_table != NULL);
426 oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
427 osd_fid2str(buf, fid);
432 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
433 const struct lu_fid *fid, char *buf)
440 if (fid_is_on_ost(env, osd, fid) == 1) {
441 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf);
442 } else if (fid_is_last_id(fid)) {
443 zapid = osd->od_ost_compat_grp0;
444 } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
445 /* special objects with fixed known fids get their name */
446 char *name = oid2name(fid_oid(fid));
449 zapid = osd->od_root;
451 if (fid_is_acct(fid))
452 zapid = MASTER_NODE_OBJ;
454 zapid = osd_get_idx_for_fid(osd, fid, buf);
457 zapid = osd_get_idx_for_fid(osd, fid, buf);
463 static inline int fid_is_fs_root(const struct lu_fid *fid)
465 /* Map root inode to special local object FID */
466 return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
467 fid_oid(fid) == OSD_FS_ROOT_OID;
470 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
471 const struct lu_fid *fid, uint64_t *oid)
473 struct osd_thread_info *info = osd_oti_get(env);
474 char *buf = info->oti_buf;
479 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
482 if (unlikely(fid_is_acct(fid))) {
483 if (fid_oid(fid) == ACCT_USER_OID)
484 *oid = dev->od_iusr_oid;
486 *oid = dev->od_igrp_oid;
487 } else if (unlikely(fid_is_fs_root(fid))) {
490 zapid = osd_get_name_n_idx(env, dev, fid, buf);
492 rc = -zap_lookup(dev->od_objset.os, zapid, buf,
493 8, 1, &info->oti_zde);
496 *oid = info->oti_zde.lzd_reg.zde_dnode;
503 * Close an entry in a specific slot.
506 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
510 LASSERT(key < o->od_oi_count);
512 oi = o->od_oi_table[key];
515 o->od_oi_table[key] = NULL;
520 * Allocate and open a new entry in the specified unused slot.
523 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
529 LASSERT(key < o->od_oi_count);
530 LASSERT(o->od_oi_table[key] == NULL);
536 rc = osd_oi_lookup(env, o, o->od_root, name, oi);
542 o->od_oi_table[key] = oi;
548 * Depopulate the OI table.
551 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
555 for (i = 0; i < o->od_oi_count; i++)
556 osd_oi_remove_table(env, o, i);
560 * Populate the OI table based.
563 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
569 for (i = 0; i < count; i++) {
570 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
571 rc = osd_oi_add_table(env, o, name, i);
573 osd_oi_close_table(env, o);
582 * Determine if the type and number of OIs used by this file system.
585 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
587 uint64_t root_oid = o->od_root;
594 * Check for multiple OIs and determine the count. There is no
595 * gap handling, if an OI is missing the wrong size can be returned.
596 * The only safeguard is that we know the number of OIs must be a
597 * power of two and this is checked for basic sanity.
599 for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
600 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
601 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
609 if ((*count & (*count - 1)) != 0)
619 * No OIs exist, this must be a new filesystem.
626 static void osd_ost_seq_init(const struct lu_env *env, struct osd_device *osd)
628 struct osd_seq_list *osl = &osd->od_seq_list;
630 CFS_INIT_LIST_HEAD(&osl->osl_seq_list);
631 rwlock_init(&osl->osl_seq_list_lock);
632 sema_init(&osl->osl_seq_init_sem, 1);
635 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
637 struct osd_seq_list *osl = &osd->od_seq_list;
638 struct osd_seq *osd_seq, *tmp;
640 write_lock(&osl->osl_seq_list_lock);
641 cfs_list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
643 cfs_list_del(&osd_seq->os_seq_list);
644 OBD_FREE(osd_seq->os_compat_dirs,
645 sizeof(uint64_t) * osd_seq->os_subdir_count);
646 OBD_FREE(osd_seq, sizeof(*osd_seq));
648 write_unlock(&osl->osl_seq_list_lock);
654 * Create /O subdirectory to map legacy OST objects for compatibility.
657 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
663 rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
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);
676 o->od_iusr_oid = odb;
678 rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
679 oid2name(ACCT_GROUP_OID), &odb);
682 o->od_igrp_oid = odb;
688 * Initialize the OIs by either opening or creating them as needed.
690 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
692 char *key = osd_oti_get(env)->oti_buf;
693 int i, rc, count = 0;
696 rc = osd_oi_probe(env, o, &count);
703 count = osd_oi_count;
706 for (i = 0; i < count; i++) {
707 sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
708 rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
714 rc = osd_oi_init_compat(env, o);
718 LASSERT((count & (count - 1)) == 0);
719 o->od_oi_count = count;
720 OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
721 if (o->od_oi_table == NULL)
724 rc = osd_oi_open_table(env, o, count);
726 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
727 o->od_oi_table = NULL;
733 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
737 osd_ost_seq_fini(env, o);
739 if (o->od_oi_table != NULL) {
740 (void) osd_oi_close_table(env, o);
741 OBD_FREE(o->od_oi_table,
742 sizeof(struct osd_oi *) * o->od_oi_count);
743 o->od_oi_table = NULL;
750 int osd_options_init(void)
752 /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
753 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
754 osd_oi_count = OSD_OI_FID_NR;
756 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
757 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
758 osd_oi_count, size_roundup_power2(osd_oi_count));
759 osd_oi_count = size_roundup_power2(osd_oi_count);