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, 2013, 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>
46 #define DEBUG_SUBSYSTEM S_OSD
48 #include <lustre_ver.h>
49 #include <libcfs/libcfs.h>
50 #include <obd_support.h>
51 #include <lustre_net.h>
53 #include <obd_class.h>
54 #include <lustre_disk.h>
55 #include <lustre_fid.h>
57 #include "osd_internal.h"
59 #include <sys/dnode.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>
72 static char *oi_tag = "osd_mount, oi";
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;
80 * zfs osd maintains names for known fids in the name hierarchy
81 * so that one can mount filesystem with regular ZFS stack and
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" },
104 static char *oid2name(const unsigned long oid)
108 while (oids[i].oid) {
109 if (oids[i].oid == oid)
117 * Lookup an existing OI by the given name.
120 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
121 uint64_t parent, const char *name, struct osd_oi *oi)
123 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
126 rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
130 strncpy(oi->oi_name, name, OSD_OI_NAME_SIZE - 1);
131 oi->oi_zapid = zde->zde_dnode;
137 * Create a new OI with the given name.
140 osd_oi_create(const struct lu_env *env, struct osd_device *o,
141 uint64_t parent, const char *name, uint64_t *child)
143 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
144 struct lu_attr *la = &osd_oti_get(env)->oti_la;
149 /* verify it doesn't already exist */
150 rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
154 /* create fid-to-dnode index */
155 tx = dmu_tx_create(o->od_objset.os);
159 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
160 dmu_tx_hold_bonus(tx, parent);
161 dmu_tx_hold_zap(tx, parent, TRUE, name);
162 LASSERT(tx->tx_objset->os_sa);
163 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
165 rc = -dmu_tx_assign(tx, TXG_WAIT);
171 la->la_valid = LA_MODE | LA_UID | LA_GID;
172 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
173 la->la_uid = la->la_gid = 0;
174 __osd_zap_create(env, &o->od_objset, &db, tx, la, parent, oi_tag, 0);
176 zde->zde_dnode = db->db_object;
178 zde->zde_type = IFTODT(S_IFDIR);
180 rc = -zap_add(o->od_objset.os, parent, name, 8, 1, (void *)zde, tx);
184 *child = db->db_object;
185 sa_buf_rele(db, oi_tag);
191 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
192 uint64_t parent, const char *name, uint64_t *child)
197 rc = osd_oi_lookup(env, o, parent, name, &oi);
199 *child = oi.oi_zapid;
200 else if (rc == -ENOENT)
201 rc = osd_oi_create(env, o, parent, name, child);
207 * Lookup the target index/flags of the fid, so it will know where
208 * the object is located (tgt index) and it is MDT or OST object.
210 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
211 obd_seq seq, struct lu_seq_range *range)
213 struct seq_server_site *ss = osd_seq_site(osd);
216 if (fid_seq_is_idif(seq)) {
217 fld_range_set_ost(range);
218 range->lsr_index = idif_ost_idx(seq);
222 if (!fid_seq_in_fldb(seq)) {
223 fld_range_set_mdt(range);
225 /* FIXME: If ss is NULL, it suppose not get lsr_index
227 range->lsr_index = ss->ss_node_id;
232 fld_range_set_any(range);
233 rc = fld_server_lookup(env, ss->ss_server_fld, seq, range);
235 CERROR("%s: cannot find FLD range for "LPX64": rc = %d\n",
236 osd_name(osd), seq, rc);
240 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
241 const struct lu_fid *fid)
245 if (fid_is_idif(fid))
248 if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
249 fid_is_name_llog(fid) || fid_is_quota(fid))
258 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
261 struct osd_seq *osd_seq;
263 cfs_list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
264 if (osd_seq->os_seq == seq)
270 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list,
273 struct osd_seq *osd_seq;
275 read_lock(&seq_list->osl_seq_list_lock);
276 osd_seq = osd_seq_find_locked(seq_list, seq);
277 read_unlock(&seq_list->osl_seq_list_lock);
282 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
283 struct osd_device *osd, obd_seq seq)
285 struct osd_seq_list *seq_list = &osd->od_seq_list;
286 struct osd_seq *osd_seq;
287 char *key = osd_oti_get(env)->oti_buf;
288 char *seq_name = osd_oti_get(env)->oti_str;
295 osd_seq = osd_seq_find(seq_list, seq);
299 down(&seq_list->osl_seq_init_sem);
300 /* Check again, in case some one else already add it
302 osd_seq = osd_seq_find(seq_list, seq);
306 OBD_ALLOC_PTR(osd_seq);
308 GOTO(out, rc = -ENOMEM);
310 CFS_INIT_LIST_HEAD(&osd_seq->os_seq_list);
311 osd_seq->os_seq = seq;
313 /* Init subdir count to be 32, but each seq can have
314 * different subdir count */
315 osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
316 OBD_ALLOC(osd_seq->os_compat_dirs,
317 sizeof(uint64_t) * osd_seq->os_subdir_count);
318 if (osd_seq->os_compat_dirs == NULL)
319 GOTO(out, rc = -ENOMEM);
321 oi.oi_zapid = osd->od_O_id;
322 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
323 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
324 fid_seq_is_idif(seq) ? 0 : seq);
326 rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
328 CERROR("%s: Can not create %s : rc = %d\n",
329 osd_name(osd), seq_name, rc);
333 for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
334 sprintf(key, "d%d", i);
335 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
338 osd_seq->os_compat_dirs[i] = sdb;
341 write_lock(&seq_list->osl_seq_list_lock);
342 cfs_list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
343 write_unlock(&seq_list->osl_seq_list_lock);
345 up(&seq_list->osl_seq_init_sem);
347 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
348 OBD_FREE(osd_seq->os_compat_dirs,
349 sizeof(uint64_t) * osd_seq->os_subdir_count);
351 OBD_FREE_PTR(osd_seq);
352 osd_seq = ERR_PTR(rc);
358 * objects w/o a natural reference (unlike a file on a MDS)
359 * are put under a special hierarchy /O/<seq>/d0..dXX
360 * this function returns a directory specific fid belongs to
363 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
364 const struct lu_fid *fid, char *buf)
366 struct osd_seq *osd_seq;
370 osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
371 if (IS_ERR(osd_seq)) {
372 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
374 return PTR_ERR(osd_seq);
377 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
378 LASSERT(rc == 0); /* we should not get here with IGIF */
379 b = ostid_id(&osd_oti_get(env)->oti_ostid) % OSD_OST_MAP_SIZE;
380 LASSERT(osd_seq->os_compat_dirs[b]);
382 sprintf(buf, LPU64, ostid_id(&osd_oti_get(env)->oti_ostid));
384 return osd_seq->os_compat_dirs[b];
387 /* XXX: f_ver is not counted, but may differ too */
388 static void osd_fid2str(char *buf, const struct lu_fid *fid)
390 sprintf(buf, DFID_NOBRACE, PFID(fid));
394 * Determine the zap object id which is being used as the OI for the
395 * given fid. The lowest N bits in the sequence ID are used as the
396 * index key. On failure 0 is returned which zfs treats internally
397 * as an invalid object id.
400 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
405 LASSERT(osd->od_oi_table != NULL);
406 oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
407 osd_fid2str(buf, fid);
412 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
413 const struct lu_fid *fid, char *buf)
420 if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
421 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf);
422 } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
423 /* special objects with fixed known fids get their name */
424 char *name = oid2name(fid_oid(fid));
427 zapid = osd->od_root;
429 if (fid_is_acct(fid))
430 zapid = MASTER_NODE_OBJ;
432 zapid = osd_get_idx_for_fid(osd, fid, buf);
435 zapid = osd_get_idx_for_fid(osd, fid, buf);
441 static inline int fid_is_fs_root(const struct lu_fid *fid)
443 /* Map root inode to special local object FID */
444 return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
445 fid_oid(fid) == OSD_FS_ROOT_OID;
448 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
449 const struct lu_fid *fid, uint64_t *oid)
451 struct osd_thread_info *info = osd_oti_get(env);
452 char *buf = info->oti_buf;
457 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
460 if (unlikely(fid_is_acct(fid))) {
461 if (fid_oid(fid) == ACCT_USER_OID)
462 *oid = dev->od_iusr_oid;
464 *oid = dev->od_igrp_oid;
465 } else if (unlikely(fid_is_fs_root(fid))) {
468 zapid = osd_get_name_n_idx(env, dev, fid, buf);
470 rc = -zap_lookup(dev->od_objset.os, zapid, buf,
471 8, 1, &info->oti_zde);
474 *oid = info->oti_zde.lzd_reg.zde_dnode;
481 * Close an entry in a specific slot.
484 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
488 LASSERT(key < o->od_oi_count);
490 oi = o->od_oi_table[key];
493 o->od_oi_table[key] = NULL;
498 * Allocate and open a new entry in the specified unused slot.
501 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
507 LASSERT(key < o->od_oi_count);
508 LASSERT(o->od_oi_table[key] == NULL);
514 rc = osd_oi_lookup(env, o, o->od_root, name, oi);
520 o->od_oi_table[key] = oi;
526 * Depopulate the OI table.
529 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
533 for (i = 0; i < o->od_oi_count; i++)
534 osd_oi_remove_table(env, o, i);
538 * Populate the OI table based.
541 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
547 for (i = 0; i < count; i++) {
548 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
549 rc = osd_oi_add_table(env, o, name, i);
551 osd_oi_close_table(env, o);
560 * Determine if the type and number of OIs used by this file system.
563 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
565 uint64_t root_oid = o->od_root;
572 * Check for multiple OIs and determine the count. There is no
573 * gap handling, if an OI is missing the wrong size can be returned.
574 * The only safeguard is that we know the number of OIs must be a
575 * power of two and this is checked for basic sanity.
577 for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
578 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
579 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
587 if ((*count & (*count - 1)) != 0)
597 * No OIs exist, this must be a new filesystem.
604 static void osd_ost_seq_init(const struct lu_env *env, struct osd_device *osd)
606 struct osd_seq_list *osl = &osd->od_seq_list;
608 CFS_INIT_LIST_HEAD(&osl->osl_seq_list);
609 rwlock_init(&osl->osl_seq_list_lock);
610 sema_init(&osl->osl_seq_init_sem, 1);
613 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
615 struct osd_seq_list *osl = &osd->od_seq_list;
616 struct osd_seq *osd_seq, *tmp;
618 write_lock(&osl->osl_seq_list_lock);
619 cfs_list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
621 cfs_list_del(&osd_seq->os_seq_list);
622 OBD_FREE(osd_seq->os_compat_dirs,
623 sizeof(uint64_t) * osd_seq->os_subdir_count);
624 OBD_FREE(osd_seq, sizeof(*osd_seq));
626 write_unlock(&osl->osl_seq_list_lock);
632 * Create /O subdirectory to map legacy OST objects for compatibility.
635 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
641 rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
647 osd_ost_seq_init(env, o);
648 /* Create on-disk indexes to maintain per-UID/GID inode usage.
649 * Those new indexes are created in the top-level ZAP outside the
650 * namespace in order not to confuse ZPL which might interpret those
651 * indexes as directories and assume the values are object IDs */
652 rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
653 oid2name(ACCT_USER_OID), &odb);
656 o->od_iusr_oid = odb;
658 rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
659 oid2name(ACCT_GROUP_OID), &odb);
662 o->od_igrp_oid = odb;
667 static char *root2convert = "ROOT";
669 * due to DNE requirements we have to change sequence of /ROOT object
670 * so that it doesn't belong to the local sequence FID_SEQ_LOCAL_FILE
671 * but a normal sequence living on MDS#0
672 * this is the sole purpose of this function.
674 * This is only needed for pre-production 2.4 ZFS filesystems, and
675 * can be removed in the future.
677 int osd_convert_root_to_new_seq(const struct lu_env *env,
678 struct osd_device *o)
680 struct luz_direntry *lze = &osd_oti_get(env)->oti_zde;
681 char *buf = osd_oti_get(env)->oti_str;
682 struct lu_fid newfid;
689 if (strstr(o->od_svname, "MDT") == NULL)
693 rc = -zap_lookup(o->od_objset.os, o->od_root, root2convert, 8,
694 sizeof(*lze) / 8, (void *)lze);
695 /* doesn't exist or let actual user to handle the error */
699 CDEBUG(D_OTHER, "%s: /ROOT -> "DFID" -> "LPU64"\n", o->od_svname,
700 PFID(&lze->lzd_fid), (long long int) lze->lzd_reg.zde_dnode);
702 /* already right one? */
703 if (fid_seq(&lze->lzd_fid) == FID_SEQ_ROOT)
706 tx = dmu_tx_create(o->od_objset.os);
710 dmu_tx_hold_bonus(tx, o->od_root);
712 /* declare delete/insert of the name */
713 dmu_tx_hold_zap(tx, o->od_root, TRUE, root2convert);
714 dmu_tx_hold_zap(tx, o->od_root, FALSE, root2convert);
716 /* declare that we'll remove object from fid-dnode mapping */
717 zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
718 dmu_tx_hold_bonus(tx, zapid);
719 dmu_tx_hold_zap(tx, zapid, FALSE, buf);
721 /* declare that we'll add object to fid-dnode mapping */
722 newfid.f_seq = FID_SEQ_ROOT;
725 zapid = osd_get_name_n_idx(env, o, &newfid, buf);
726 dmu_tx_hold_bonus(tx, zapid);
727 dmu_tx_hold_zap(tx, zapid, TRUE, buf);
729 rc = -dmu_tx_assign(tx, TXG_WAIT);
733 rc = -zap_remove(o->od_objset.os, o->od_root, root2convert, tx);
738 zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
739 rc = -zap_remove(o->od_objset.os, zapid, buf, tx);
743 lze->lzd_fid = newfid;
744 rc = -zap_add(o->od_objset.os, o->od_root, root2convert,
745 8, sizeof(*lze) / 8, (void *)lze, tx);
749 /* add to OI with the new fid */
750 zapid = osd_get_name_n_idx(env, o, &newfid, buf);
751 rc = -zap_add(o->od_objset.os, zapid, buf, 8, 1, &lze->lzd_reg, tx);
756 /* LMA will be updated in mdd_compat_fixes */
764 CERROR("%s: can't convert to new fid: rc = %d\n", o->od_svname, rc);
769 * Initialize the OIs by either opening or creating them as needed.
771 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
773 char *key = osd_oti_get(env)->oti_buf;
774 int i, rc, count = 0;
777 rc = osd_oi_probe(env, o, &count);
784 count = osd_oi_count;
787 for (i = 0; i < count; i++) {
788 sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
789 rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
795 rc = osd_oi_init_compat(env, o);
799 LASSERT((count & (count - 1)) == 0);
800 o->od_oi_count = count;
801 OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
802 if (o->od_oi_table == NULL)
805 rc = osd_oi_open_table(env, o, count);
807 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
808 o->od_oi_table = NULL;
814 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
818 osd_ost_seq_fini(env, o);
820 if (o->od_oi_table != NULL) {
821 (void) osd_oi_close_table(env, o);
822 OBD_FREE(o->od_oi_table,
823 sizeof(struct osd_oi *) * o->od_oi_count);
824 o->od_oi_table = NULL;
831 int osd_options_init(void)
833 /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
834 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
835 osd_oi_count = OSD_OI_FID_NR;
837 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
838 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
839 osd_oi_count, size_roundup_power2(osd_oi_count));
840 osd_oi_count = size_roundup_power2(osd_oi_count);