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 #define OSD_OI_FID_NR (1UL << 7)
73 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
74 unsigned int osd_oi_count = OSD_OI_FID_NR;
78 * zfs osd maintains names for known fids in the name hierarchy
79 * so that one can mount filesystem with regular ZFS stack and
87 static const struct named_oid oids[] = {
88 { LAST_RECV_OID, LAST_RCVD },
89 { OFD_LAST_GROUP_OID, "LAST_GROUP" },
90 { LLOG_CATALOGS_OID, "CATALOGS" },
91 { MGS_CONFIGS_OID, NULL /*MOUNT_CONFIGS_DIR*/ },
92 { FID_SEQ_SRV_OID, "seq_srv" },
93 { FID_SEQ_CTL_OID, "seq_ctl" },
94 { FLD_INDEX_OID, "fld" },
95 { MDD_LOV_OBJ_OID, LOV_OBJID },
96 { OFD_HEALTH_CHECK_OID, HEALTH_CHECK },
97 { ACCT_USER_OID, "acct_usr_inode" },
98 { ACCT_GROUP_OID, "acct_grp_inode" },
102 static char *oid2name(const unsigned long oid)
106 while (oids[i].oid) {
107 if (oids[i].oid == oid)
115 * Lookup an existing OI by the given name.
118 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
119 uint64_t parent, const char *name, struct osd_oi *oi)
121 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
124 rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
128 rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
129 if (rc >= sizeof(oi->oi_name))
133 oi->oi_zapid = zde->zde_dnode;
139 * Create a new OI with the given name.
142 osd_oi_create(const struct lu_env *env, struct osd_device *o,
143 uint64_t parent, const char *name, uint64_t *child)
145 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
146 struct lu_attr *la = &osd_oti_get(env)->oti_la;
151 /* verify it doesn't already exist */
152 rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
156 /* create fid-to-dnode index */
157 tx = dmu_tx_create(o->od_os);
161 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
162 dmu_tx_hold_bonus(tx, parent);
163 dmu_tx_hold_zap(tx, parent, TRUE, name);
164 LASSERT(tx->tx_objset->os_sa);
165 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
167 rc = -dmu_tx_assign(tx, TXG_WAIT);
173 la->la_valid = LA_MODE | LA_UID | LA_GID;
174 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
175 la->la_uid = la->la_gid = 0;
176 __osd_zap_create(env, o, &db, tx, la, parent, 0);
178 zde->zde_dnode = db->db_object;
180 zde->zde_type = IFTODT(S_IFDIR);
182 rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
186 *child = db->db_object;
187 sa_buf_rele(db, osd_obj_tag);
193 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
194 uint64_t parent, const char *name, uint64_t *child)
199 rc = osd_oi_lookup(env, o, parent, name, &oi);
201 *child = oi.oi_zapid;
202 else if (rc == -ENOENT)
203 rc = osd_oi_create(env, o, parent, name, child);
209 * Lookup the target index/flags of the fid, so it will know where
210 * the object is located (tgt index) and it is MDT or OST object.
212 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
213 obd_seq seq, struct lu_seq_range *range)
215 struct seq_server_site *ss = osd_seq_site(osd);
217 if (fid_seq_is_idif(seq)) {
218 fld_range_set_ost(range);
219 range->lsr_index = idif_ost_idx(seq);
223 if (!fid_seq_in_fldb(seq)) {
224 fld_range_set_mdt(range);
226 /* FIXME: If ss is NULL, it suppose not get lsr_index
228 range->lsr_index = ss->ss_node_id;
233 fld_range_set_any(range);
234 /* OSD will only do local fld lookup */
235 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
238 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
239 const struct lu_fid *fid)
241 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
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))
252 rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
255 CERROR("%s: "DFID" lookup failed: rc = %d\n",
256 osd_name(osd), PFID(fid), rc);
260 if (fld_range_is_ost(range))
266 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
269 struct osd_seq *osd_seq;
271 list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
272 if (osd_seq->os_seq == seq)
278 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list,
281 struct osd_seq *osd_seq;
283 read_lock(&seq_list->osl_seq_list_lock);
284 osd_seq = osd_seq_find_locked(seq_list, seq);
285 read_unlock(&seq_list->osl_seq_list_lock);
290 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
291 struct osd_device *osd, obd_seq seq)
293 struct osd_seq_list *seq_list = &osd->od_seq_list;
294 struct osd_seq *osd_seq;
295 char *key = osd_oti_get(env)->oti_buf;
296 char *seq_name = osd_oti_get(env)->oti_str;
303 osd_seq = osd_seq_find(seq_list, seq);
307 down(&seq_list->osl_seq_init_sem);
308 /* Check again, in case some one else already add it
310 osd_seq = osd_seq_find(seq_list, seq);
314 OBD_ALLOC_PTR(osd_seq);
316 GOTO(out, rc = -ENOMEM);
318 INIT_LIST_HEAD(&osd_seq->os_seq_list);
319 osd_seq->os_seq = seq;
321 /* Init subdir count to be 32, but each seq can have
322 * different subdir count */
323 osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
324 OBD_ALLOC(osd_seq->os_compat_dirs,
325 sizeof(uint64_t) * osd_seq->os_subdir_count);
326 if (osd_seq->os_compat_dirs == NULL)
327 GOTO(out, rc = -ENOMEM);
329 oi.oi_zapid = osd->od_O_id;
330 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
331 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
332 fid_seq_is_idif(seq) ? 0 : seq);
334 rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
336 CERROR("%s: Can not create %s : rc = %d\n",
337 osd_name(osd), seq_name, rc);
341 for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
342 sprintf(key, "d%d", i);
343 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
346 osd_seq->os_compat_dirs[i] = sdb;
349 write_lock(&seq_list->osl_seq_list_lock);
350 list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
351 write_unlock(&seq_list->osl_seq_list_lock);
353 up(&seq_list->osl_seq_init_sem);
355 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
356 OBD_FREE(osd_seq->os_compat_dirs,
357 sizeof(uint64_t) * osd_seq->os_subdir_count);
359 OBD_FREE_PTR(osd_seq);
360 osd_seq = ERR_PTR(rc);
366 * objects w/o a natural reference (unlike a file on a MDS)
367 * are put under a special hierarchy /O/<seq>/d0..dXX
368 * this function returns a directory specific fid belongs to
371 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
372 const struct lu_fid *fid, char *buf)
374 struct osd_seq *osd_seq;
379 osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
380 if (IS_ERR(osd_seq)) {
381 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
383 return PTR_ERR(osd_seq);
386 if (fid_is_last_id(fid)) {
389 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
390 LASSERT(rc == 0); /* we should not get here with IGIF */
391 id = ostid_id(&osd_oti_get(env)->oti_ostid);
394 b = id % OSD_OST_MAP_SIZE;
395 LASSERT(osd_seq->os_compat_dirs[b]);
397 sprintf(buf, LPU64, id);
399 return osd_seq->os_compat_dirs[b];
402 /* XXX: f_ver is not counted, but may differ too */
403 static void osd_fid2str(char *buf, const struct lu_fid *fid)
405 sprintf(buf, DFID_NOBRACE, PFID(fid));
409 * Determine the zap object id which is being used as the OI for the
410 * given fid. The lowest N bits in the sequence ID are used as the
411 * index key. On failure 0 is returned which zfs treats internally
412 * as an invalid object id.
415 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
420 LASSERT(osd->od_oi_table != NULL);
421 oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
422 osd_fid2str(buf, fid);
427 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
428 const struct lu_fid *fid, char *buf)
435 if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
436 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf);
437 } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
438 /* special objects with fixed known fids get their name */
439 char *name = oid2name(fid_oid(fid));
442 zapid = osd->od_root;
444 if (fid_is_acct(fid))
445 zapid = MASTER_NODE_OBJ;
447 zapid = osd_get_idx_for_fid(osd, fid, buf);
450 zapid = osd_get_idx_for_fid(osd, fid, buf);
456 static inline int fid_is_fs_root(const struct lu_fid *fid)
458 /* Map root inode to special local object FID */
459 return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
460 fid_oid(fid) == OSD_FS_ROOT_OID;
463 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
464 const struct lu_fid *fid, uint64_t *oid)
466 struct osd_thread_info *info = osd_oti_get(env);
467 char *buf = info->oti_buf;
472 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
475 if (unlikely(fid_is_acct(fid))) {
476 if (fid_oid(fid) == ACCT_USER_OID)
477 *oid = dev->od_iusr_oid;
479 *oid = dev->od_igrp_oid;
480 } else if (unlikely(fid_is_fs_root(fid))) {
483 zapid = osd_get_name_n_idx(env, dev, fid, buf);
485 rc = -zap_lookup(dev->od_os, zapid, buf,
486 8, 1, &info->oti_zde);
489 *oid = info->oti_zde.lzd_reg.zde_dnode;
493 dmu_prefetch(dev->od_os, *oid, 0, 0);
499 * Close an entry in a specific slot.
502 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
506 LASSERT(key < o->od_oi_count);
508 oi = o->od_oi_table[key];
511 o->od_oi_table[key] = NULL;
516 * Allocate and open a new entry in the specified unused slot.
519 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
525 LASSERT(key < o->od_oi_count);
526 LASSERT(o->od_oi_table[key] == NULL);
532 rc = osd_oi_lookup(env, o, o->od_root, name, oi);
538 o->od_oi_table[key] = oi;
544 * Depopulate the OI table.
547 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
551 for (i = 0; i < o->od_oi_count; i++)
552 osd_oi_remove_table(env, o, i);
556 * Populate the OI table based.
559 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
565 for (i = 0; i < count; i++) {
566 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
567 rc = osd_oi_add_table(env, o, name, i);
569 osd_oi_close_table(env, o);
578 * Determine if the type and number of OIs used by this file system.
581 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
583 uint64_t root_oid = o->od_root;
590 * Check for multiple OIs and determine the count. There is no
591 * gap handling, if an OI is missing the wrong size can be returned.
592 * The only safeguard is that we know the number of OIs must be a
593 * power of two and this is checked for basic sanity.
595 for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
596 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
597 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
605 if ((*count & (*count - 1)) != 0)
615 * No OIs exist, this must be a new filesystem.
622 static void osd_ost_seq_init(const struct lu_env *env, struct osd_device *osd)
624 struct osd_seq_list *osl = &osd->od_seq_list;
626 INIT_LIST_HEAD(&osl->osl_seq_list);
627 rwlock_init(&osl->osl_seq_list_lock);
628 sema_init(&osl->osl_seq_init_sem, 1);
631 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
633 struct osd_seq_list *osl = &osd->od_seq_list;
634 struct osd_seq *osd_seq, *tmp;
636 write_lock(&osl->osl_seq_list_lock);
637 list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
639 list_del(&osd_seq->os_seq_list);
640 OBD_FREE(osd_seq->os_compat_dirs,
641 sizeof(uint64_t) * osd_seq->os_subdir_count);
642 OBD_FREE(osd_seq, sizeof(*osd_seq));
644 write_unlock(&osl->osl_seq_list_lock);
650 * Create /O subdirectory to map legacy OST objects for compatibility.
653 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
659 rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
665 osd_ost_seq_init(env, o);
666 /* Create on-disk indexes to maintain per-UID/GID inode usage.
667 * Those new indexes are created in the top-level ZAP outside the
668 * namespace in order not to confuse ZPL which might interpret those
669 * indexes as directories and assume the values are object IDs */
670 rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
671 oid2name(ACCT_USER_OID), &odb);
674 o->od_iusr_oid = odb;
676 rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
677 oid2name(ACCT_GROUP_OID), &odb);
680 o->od_igrp_oid = odb;
685 static char *root2convert = "ROOT";
687 * due to DNE requirements we have to change sequence of /ROOT object
688 * so that it doesn't belong to the local sequence FID_SEQ_LOCAL_FILE
689 * but a normal sequence living on MDS#0
690 * this is the sole purpose of this function.
692 * This is only needed for pre-production 2.4 ZFS filesystems, and
693 * can be removed in the future.
695 int osd_convert_root_to_new_seq(const struct lu_env *env,
696 struct osd_device *o)
698 struct luz_direntry *lze = &osd_oti_get(env)->oti_zde;
699 char *buf = osd_oti_get(env)->oti_str;
700 struct lu_fid newfid;
707 if (strstr(o->od_svname, "MDT") == NULL)
711 rc = -zap_lookup(o->od_os, o->od_root, root2convert, 8,
712 sizeof(*lze) / 8, (void *)lze);
713 /* doesn't exist or let actual user to handle the error */
717 CDEBUG(D_OTHER, "%s: /ROOT -> "DFID" -> "LPU64"\n", o->od_svname,
718 PFID(&lze->lzd_fid), (long long int) lze->lzd_reg.zde_dnode);
720 /* already right one? */
721 if (fid_seq(&lze->lzd_fid) == FID_SEQ_ROOT)
724 tx = dmu_tx_create(o->od_os);
728 dmu_tx_hold_bonus(tx, o->od_root);
730 /* declare delete/insert of the name */
731 dmu_tx_hold_zap(tx, o->od_root, TRUE, root2convert);
732 dmu_tx_hold_zap(tx, o->od_root, FALSE, root2convert);
734 /* declare that we'll remove object from fid-dnode mapping */
735 zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
736 dmu_tx_hold_bonus(tx, zapid);
737 dmu_tx_hold_zap(tx, zapid, FALSE, buf);
739 /* declare that we'll add object to fid-dnode mapping */
740 newfid.f_seq = FID_SEQ_ROOT;
743 zapid = osd_get_name_n_idx(env, o, &newfid, buf);
744 dmu_tx_hold_bonus(tx, zapid);
745 dmu_tx_hold_zap(tx, zapid, TRUE, buf);
747 rc = -dmu_tx_assign(tx, TXG_WAIT);
751 rc = -zap_remove(o->od_os, o->od_root, root2convert, tx);
756 zapid = osd_get_name_n_idx(env, o, &lze->lzd_fid, buf);
757 rc = -zap_remove(o->od_os, zapid, buf, tx);
761 lze->lzd_fid = newfid;
762 rc = -zap_add(o->od_os, o->od_root, root2convert,
763 8, sizeof(*lze) / 8, (void *)lze, tx);
767 /* add to OI with the new fid */
768 zapid = osd_get_name_n_idx(env, o, &newfid, buf);
769 rc = -zap_add(o->od_os, zapid, buf, 8, 1, &lze->lzd_reg, tx);
774 /* LMA will be updated in mdd_compat_fixes */
782 CERROR("%s: can't convert to new fid: rc = %d\n", o->od_svname, rc);
787 * Initialize the OIs by either opening or creating them as needed.
789 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
791 char *key = osd_oti_get(env)->oti_buf;
792 int i, rc, count = 0;
795 rc = osd_oi_probe(env, o, &count);
802 count = osd_oi_count;
805 for (i = 0; i < count; i++) {
806 sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
807 rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
813 rc = osd_oi_init_compat(env, o);
817 LASSERT((count & (count - 1)) == 0);
818 o->od_oi_count = count;
819 OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
820 if (o->od_oi_table == NULL)
823 rc = osd_oi_open_table(env, o, count);
825 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
826 o->od_oi_table = NULL;
832 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
836 osd_ost_seq_fini(env, o);
838 if (o->od_oi_table != NULL) {
839 (void) osd_oi_close_table(env, o);
840 OBD_FREE(o->od_oi_table,
841 sizeof(struct osd_oi *) * o->od_oi_count);
842 o->od_oi_table = NULL;
849 int osd_options_init(void)
851 /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
852 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
853 osd_oi_count = OSD_OI_FID_NR;
855 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
856 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
857 osd_oi_count, size_roundup_power2(osd_oi_count));
858 osd_oi_count = size_roundup_power2(osd_oi_count);