sys/vfs/hammer: Add lo_to_pfs()/pfs_to_lo() macros
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 6 Dec 2015 10:36:05 +0000 (19:36 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 6 Dec 2015 16:41:51 +0000 (01:41 +0900)
These macros hide 16 bits shift and cast to int/uint32_t
that are necessary on localization to/from PFS id conversion.

Some code are better without details being hidden by macros,
but this seems to be more readable with x_to_y macros that
hide bunch of >>16, <<16, and casts.

(No diff in binary on x86_64/gcc5)

sbin/hammer/cmd_recover.c
sys/vfs/hammer/hammer_btree.h
sys/vfs/hammer/hammer_mirror.c
sys/vfs/hammer/hammer_pfs.c
sys/vfs/hammer/hammer_rebalance.c
sys/vfs/hammer/hammer_reblock.c
sys/vfs/hammer/hammer_subs.c
sys/vfs/hammer/hammer_vfsops.c
sys/vfs/hammer/hammer_vnops.c

index 5f69e1c..9ce2992 100644 (file)
@@ -174,7 +174,7 @@ recover_elm(hammer_btree_leaf_elm_t leaf)
        if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
                goto done;
 
-       llid = leaf->base.localization >> 16;
+       llid = lo_to_pfs(leaf->base.localization);
 
        dict = get_dict(leaf->base.obj_id, llid);
 
index 3bdcdbd..5e66839 100644 (file)
@@ -139,6 +139,11 @@ typedef struct hammer_base_elm *hammer_base_elm_t;
 #define HAMMER_MAX_LOCALIZATION                0x0000FFFFU
 #define HAMMER_DEF_LOCALIZATION                0x00000000U
 
+#define lo_to_pfs(lo)                                  \
+       ((int)(((lo) & HAMMER_LOCALIZE_PSEUDOFS_MASK) >> 16))
+#define pfs_to_lo(pfs)                                 \
+       ((((uint32_t)(pfs)) << 16) & HAMMER_LOCALIZE_PSEUDOFS_MASK)
+
 /*
  * Internal element (40 + 24 = 64 bytes).
  *
index 3c9eff7..405d173 100644 (file)
@@ -87,7 +87,7 @@ hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip,
        uint32_t localization;
        uint32_t rec_crc;
 
-       localization = (uint32_t)mirror->pfs_id << 16;
+       localization = pfs_to_lo(mirror->pfs_id);
 
        if ((mirror->key_beg.localization | mirror->key_end.localization) &
            HAMMER_LOCALIZE_PSEUDOFS_MASK) {
@@ -344,7 +344,7 @@ hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
        char *uptr;
        int seq;
 
-       localization = (uint32_t)mirror->pfs_id << 16;
+       localization = pfs_to_lo(mirror->pfs_id);
        seq = trans->hmp->flusher.done;
 
        /*
index cd6aa02..917a9c9 100644 (file)
@@ -61,7 +61,7 @@ hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
 
        if ((error = hammer_pfs_autodetect(pfs, ip)) != 0)
                return(error);
-       localization = (uint32_t)pfs->pfs_id << 16;
+       localization = pfs_to_lo(pfs->pfs_id);
        pfs->bytes = sizeof(struct hammer_pseudofs_data);
        pfs->version = HAMMER_IOC_PSEUDOFS_VERSION;
 
@@ -109,7 +109,7 @@ hammer_ioc_set_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
 
        if ((error = hammer_pfs_autodetect(pfs, ip)) != 0)
                return(error);
-       localization = (uint32_t)pfs->pfs_id << 16;
+       localization = pfs_to_lo(pfs->pfs_id);
        if (pfs->version != HAMMER_IOC_PSEUDOFS_VERSION)
                error = EINVAL;
 
@@ -164,7 +164,7 @@ hammer_ioc_upgrade_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
 
        if ((error = hammer_pfs_autodetect(pfs, ip)) != 0)
                return(error);
-       localization = (uint32_t)pfs->pfs_id << 16;
+       localization = pfs_to_lo(pfs->pfs_id);
        if ((error = hammer_unload_pseudofs(trans, localization)) != 0)
                return(error);
 
@@ -214,7 +214,7 @@ hammer_ioc_downgrade_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
 
        if ((error = hammer_pfs_autodetect(pfs, ip)) != 0)
                return(error);
-       localization = (uint32_t)pfs->pfs_id << 16;
+       localization = pfs_to_lo(pfs->pfs_id);
        if ((error = hammer_unload_pseudofs(trans, localization)) != 0)
                return(error);
 
@@ -251,7 +251,7 @@ hammer_ioc_destroy_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
 
        if ((error = hammer_pfs_autodetect(pfs, ip)) != 0)
                return(error);
-       localization = (uint32_t)pfs->pfs_id << 16;
+       localization = pfs_to_lo(pfs->pfs_id);
 
        if ((error = hammer_unload_pseudofs(trans, localization)) != 0)
                return(error);
@@ -288,7 +288,7 @@ hammer_ioc_wait_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
 
        if ((error = hammer_pfs_autodetect(pfs, ip)) != 0)
                return(error);
-       localization = (uint32_t)pfs->pfs_id << 16;
+       localization = pfs_to_lo(pfs->pfs_id);
 
        if ((error = copyin(pfs->ondisk, &pfsd, sizeof(pfsd))) != 0)
                return(error);
@@ -343,7 +343,7 @@ hammer_ioc_iterate_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
        if ((error = hammer_pfs_autodetect(&pfs, ip)) != 0)
                return(error);
        pi->pos = pfs.pfs_id;
-       localization = (uint32_t)pi->pos << 16;
+       localization = pfs_to_lo(pi->pos);
 
        dip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
                HAMMER_DEF_LOCALIZATION, 0, &error);
@@ -371,7 +371,7 @@ hammer_ioc_iterate_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
                        if (pi->ondisk)
                                copyout(cursor.data, pi->ondisk, cursor.leaf->data_len);
                        localization = cursor.leaf->base.key;
-                       pi->pos = localization >> 16;
+                       pi->pos = lo_to_pfs(localization);
                        /*
                         * Caller needs to increment pi->pos each time calling
                         * this ioctl. This ioctl only restores current PFS id.
@@ -395,7 +395,7 @@ hammer_pfs_autodetect(struct hammer_ioc_pseudofs_rw *pfs, hammer_inode_t ip)
        int error = 0;
 
        if (pfs->pfs_id == -1)
-               pfs->pfs_id = (int)(ip->obj_localization >> 16);
+               pfs->pfs_id = lo_to_pfs(ip->obj_localization);
        if (pfs->pfs_id < 0 || pfs->pfs_id >= HAMMER_MAX_PFS)
                error = EINVAL;
        if (pfs->bytes < sizeof(struct hammer_pseudofs_data))
index 68970e1..c6f6f38 100644 (file)
@@ -91,7 +91,7 @@ hammer_ioc_rebalance(hammer_transaction_t trans, hammer_inode_t ip,
        if (rebal->allpfs == 0)
                key_end_localization |= ip->obj_localization;
        else
-               key_end_localization |= (HAMMER_MAX_PFSID << 16);
+               key_end_localization |= pfs_to_lo(HAMMER_MAX_PFSID);
 
        hammer_btree_lcache_init(trans->hmp, &lcache, 2);
 
index 82f3940..d4a356e 100644 (file)
@@ -100,7 +100,7 @@ hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
        if (reblock->allpfs == 0)
                key_end_localization |= ip->obj_localization;
        else
-               key_end_localization |= (HAMMER_MAX_PFSID << 16);
+               key_end_localization |= pfs_to_lo(HAMMER_MAX_PFSID);
 
        checkspace_count = 0;
        seq = trans->hmp->flusher.done;
index 7b98c1a..5a3b1cb 100644 (file)
@@ -1070,7 +1070,7 @@ hammer_str_to_tid(const char *str, int *ispfsp,
         */
        str = ptr;
        if (*str == ':') {
-               localization = strtoul(str + 1, &ptr, 10) << 16;
+               localization = pfs_to_lo(strtoul(str + 1, &ptr, 10));
                if (ptr - str != 6)
                        return(EINVAL);
                str = ptr;
index aa8a4d3..2d2424a 100644 (file)
@@ -1134,7 +1134,7 @@ hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
        KKASSERT(MAXFIDSZ >= 16);
        ip = VTOI(vp);
        fhp->fid_len = offsetof(struct fid, fid_data[16]);
-       fhp->fid_ext = ip->obj_localization >> 16;
+       fhp->fid_ext = lo_to_pfs(ip->obj_localization);
        bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
        bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
        return(0);
@@ -1163,7 +1163,7 @@ hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
        if (rootvp)
                localization = VTOI(rootvp)->obj_localization;
        else
-               localization = (uint32_t)fhp->fid_ext << 16;
+               localization = pfs_to_lo(fhp->fid_ext);
 
        lwkt_gettoken(&hmp->fs_token);
        hammer_simple_transaction(&trans, hmp);
index bc4a75d..653f20f 100644 (file)
@@ -1815,7 +1815,7 @@ hammer_vop_readlink(struct vop_readlink_args *ap)
                        hammer_simple_transaction(&trans, hmp);
                        bcopy(ptr + 5, buf, 5);
                        buf[5] = 0;
-                       localization = strtoul(buf, NULL, 10) << 16;
+                       localization = pfs_to_lo(strtoul(buf, NULL, 10));
                        pfsm = hammer_load_pseudofs(&trans, localization,
                                                    &error);
                        if (error == 0) {
@@ -1825,12 +1825,12 @@ hammer_vop_readlink(struct vop_readlink_args *ap)
                                        ksnprintf(buf, sizeof(buf),
                                                  "@@0x%016llx:%05d",
                                                  (long long)pfsm->pfsd.sync_end_tid,
-                                                 localization >> 16);
+                                                 lo_to_pfs(localization));
                                } else {
                                        /* vap->va_size == 10 */
                                        ksnprintf(buf, sizeof(buf),
                                                  "@@-1:%05d",
-                                                 localization >> 16);
+                                                 lo_to_pfs(localization));
                                }
                                ptr = buf;
                                bytes = strlen(buf);