diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2020-05-28 15:48:30 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2020-05-28 15:48:30 +0000 |
commit | e4fe65347da83215efa273f99ad7db13e99f7658 (patch) | |
tree | 671ee987ec5337fa008127dde705192b1715f129 /sys | |
parent | a91ee5b7beebd6cafd108ea10ff85cf84f4f9243 (diff) |
Make generation numbers unsigned and fill them using a random number
from the range [1..UINT_MAX] initially. On inode re-use increment
and on wrap refill from the range [1..UINT_MAX-1] to avoid
assigning UINT_MAX (the original value). Zero still means uninitialized.
ok millert@
Diffstat (limited to 'sys')
-rw-r--r-- | sys/ufs/ffs/ffs_alloc.c | 18 | ||||
-rw-r--r-- | sys/ufs/ffs/ffs_vfsops.c | 7 | ||||
-rw-r--r-- | sys/ufs/ufs/dinode.h | 4 | ||||
-rw-r--r-- | sys/ufs/ufs/inode.h | 4 |
4 files changed, 15 insertions, 18 deletions
diff --git a/sys/ufs/ffs/ffs_alloc.c b/sys/ufs/ffs/ffs_alloc.c index 4a3267b1d5f..cb0bfeda58a 100644 --- a/sys/ufs/ffs/ffs_alloc.c +++ b/sys/ufs/ffs/ffs_alloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ffs_alloc.c,v 1.110 2020/02/21 11:13:55 otto Exp $ */ +/* $OpenBSD: ffs_alloc.c,v 1.111 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: ffs_alloc.c,v 1.11 1996/05/11 18:27:09 mycroft Exp $ */ /* @@ -413,16 +413,13 @@ ffs_inode_alloc(struct inode *pip, mode_t mode, struct ucred *cred, /* * Set up a new generation number for this inode. - * XXX - just increment for now, this is wrong! (millert) - * Need a way to preserve randomization. + * On wrap, we make sure to assign a number != 0 and != UINT_MAX + * (the origial value). */ if (DIP(ip, gen) != 0) DIP_ADD(ip, gen, 1); - if (DIP(ip, gen) == 0) - DIP_ASSIGN(ip, gen, arc4random() & INT_MAX); - - if (DIP(ip, gen) == 0 || DIP(ip, gen) == -1) - DIP_ASSIGN(ip, gen, 1); /* Shouldn't happen */ + while (DIP(ip, gen) == 0) + DIP_ASSIGN(ip, gen, arc4random_uniform(UINT_MAX)); return (0); @@ -1205,9 +1202,10 @@ gotit: memset(ibp->b_data, 0, fs->fs_bsize); dp2 = (struct ufs2_dinode *)(ibp->b_data); - /* Give each inode a positive generation number */ + /* Give each inode a generation number */ for (i = 0; i < INOPB(fs); i++) { - dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1; + while (dp2->di_gen == 0) + dp2->di_gen = arc4random(); dp2++; } diff --git a/sys/ufs/ffs/ffs_vfsops.c b/sys/ufs/ffs/ffs_vfsops.c index 453111b39dd..b8a7b0a590b 100644 --- a/sys/ufs/ffs/ffs_vfsops.c +++ b/sys/ufs/ffs/ffs_vfsops.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ffs_vfsops.c,v 1.183 2020/02/21 11:11:15 otto Exp $ */ +/* $OpenBSD: ffs_vfsops.c,v 1.184 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: ffs_vfsops.c,v 1.19 1996/02/09 22:22:26 christos Exp $ */ /* @@ -1422,9 +1422,8 @@ retry: * already have one. This should only happen on old filesystems. */ if (DIP(ip, gen) == 0) { - DIP_ASSIGN(ip, gen, arc4random() & INT_MAX); - if (DIP(ip, gen) == 0 || DIP(ip, gen) == -1) - DIP_ASSIGN(ip, gen, 1); /* Shouldn't happen */ + while (DIP(ip, gen) == 0) + DIP_ASSIGN(ip, gen, arc4random()); if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) ip->i_flag |= IN_MODIFIED; } diff --git a/sys/ufs/ufs/dinode.h b/sys/ufs/ufs/dinode.h index 5ad69be286e..7a2d55b45f2 100644 --- a/sys/ufs/ufs/dinode.h +++ b/sys/ufs/ufs/dinode.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dinode.h,v 1.18 2013/05/30 19:19:09 guenther Exp $ */ +/* $OpenBSD: dinode.h,v 1.19 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: dinode.h,v 1.7 1995/06/15 23:22:48 cgd Exp $ */ /* @@ -82,7 +82,7 @@ struct ufs1_dinode { int32_t di_ib[NIADDR]; /* 88: Indirect disk blocks. */ u_int32_t di_flags; /* 100: Status flags (chflags). */ int32_t di_blocks; /* 104: Blocks actually held. */ - int32_t di_gen; /* 108: Generation number. */ + u_int32_t di_gen; /* 108: Generation number. */ u_int32_t di_uid; /* 112: File owner. */ u_int32_t di_gid; /* 116: File group. */ int32_t di_spare[2]; /* 120: Reserved; currently unused */ diff --git a/sys/ufs/ufs/inode.h b/sys/ufs/ufs/inode.h index 4675dc7e38d..4530c4e304d 100644 --- a/sys/ufs/ufs/inode.h +++ b/sys/ufs/ufs/inode.h @@ -1,4 +1,4 @@ -/* $OpenBSD: inode.h,v 1.51 2019/01/21 18:09:21 anton Exp $ */ +/* $OpenBSD: inode.h,v 1.52 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: inode.h,v 1.8 1995/06/15 23:22:50 cgd Exp $ */ /* @@ -344,6 +344,6 @@ struct ufid { u_int16_t ufid_len; /* Length of structure. */ u_int16_t ufid_pad; /* Force 32-bit alignment. */ ufsino_t ufid_ino; /* File number (ino). */ - int32_t ufid_gen; /* Generation number. */ + u_int32_t ufid_gen; /* Generation number. */ }; #endif /* _KERNEL */ |