minix: unify itree_v1, itree_v2, and itree_common
cover minix: unify itree_v1, itree_v2, and itree_common
For as far back as the git history goes and then some, minix's itree functions have been split across three files: itree_v1.c, itree_v2.c, and itree_common.c. The first two of these files had defines, types, static helper functions, and some wrapper functions tailored for version 1 and versions 2 and 3 of the Minix file systems respectively. Each of these files then included itree_common.c.
The reason for this odd arrangement is that there are some stark differences between version 1 and versions 2 and 3 of the Minix fs. Version 1 has doubly indirect blocks and 16 bit block pointers, while versions 2 and 3 have trebly indirect blocks and 32 bit block pointers. By having the separate itree_v1.c and itree_v2.c files that then included itree_common.c, DIRECT, DEPTH, block_t, and Indirect could be defined differently for the two broad types of Minix filesystems while sharing the bulk of their code because the same code in itree_common.c would be treated differently by the preprocessor and compiler depending on which file included it. In other words, DEPTH could mean 3 or 4 depending on if it had been included from itree_v1.c or itree_v2.c.
This whole thing came up while some patches I submitted recently to move minix over to use iomap were being reviewed. Having the two separate code paths and includes made that task much more difficult, and a reviewer commented at one part about how it was one way to avoid keeping disk geometry information in the superblock info struct. Parts of this code do not seem to have been modified for a very, very long time, possibly since 1991, but that's no reason necessarily to keep it in place.
This patch unifies the two code paths previously used for itree functions. Most of it runs the same no matter what version of the filesystem is being used, but in the places where it doesn't version checks, information stored in the superblock, dynamically allocating formerly static arrays, careful pointer casting, and judicious use of accessor functions for more type safety handle the different cases while still keeping the code more contained and hopefully less confusing to others.
As this is an RFC patch, there are many more comments explaning my thought processes and what's going on than would usually be found in a kernel patch submission. Depending on how this is received, I will clean out excessive comments before submitting it for real.
This file unavoidably lands as one relatively large patch, but it ended up not breaking down well into smaller chunks that would still build a working kernel.
Jeremy Bingham (1):
minix: unify the v1 and v2/v3 itree code paths
fs/minix/Makefile | 2 +- fs/minix/inode.c | 35 +- fs/minix/itree.c | 744 ++++++++++++++++++++++++++++++++++++++++ fs/minix/itree_common.c | 374 -------------------- fs/minix/itree_v1.c | 67 ---- fs/minix/itree_v2.c | 75 ---- fs/minix/minix.h | 26 +- 7 files changed, 774 insertions(+), 549 deletions(-) create mode 100644 fs/minix/itree.c delete mode 100644 fs/minix/itree_common.c delete mode 100644 fs/minix/itree_v1.c delete mode 100644 fs/minix/itree_v2.c
Signature · 1 line
2.47.3
RFC PATCH 1/1 minix: unify the v1 and v2/v3 itree code paths
Instead of having two separate itree_v1.c and itree_v2.c files for the different versions, this patch brings them together. Version 1 of the Minix filesystem has doubly indirect block pointers and 16 bit block pointers, while versions 2 and 3 have trebly indirect block pointers and 32 bit block pointers. Both itree_v1.c and itree_v2.c then included itree_common.c and provided version specific wrappers for itree_common.c's functions. Code in other files would then call one wrapper or the other depending on the filesystem's version number. All of this was in lieu of just defining the number of direct blocks and levels of indirect blocks in the superblock info struct. Now, itree.c brings those paths together.
Added superblock info fields to the superblock info struct to store the numbers of direct blocks and levels of indirect blocks inodes have, then storing that information while filling the superblock struct. The different versions of the minix filesystem have different levels of indirect blocks, and this makes that information available in one central place.
Also added a define for number of elements in the arrays in the union in the inode information struct.
This patch also removes itree_v1.c and itree_v2.c from the Makefile, adds itree.c in their place, deletes the dead itree files, removes some wrapper functions in inode.c that aren't necessary anymore, update some function calls in inode.c that no longer need versioned calls, and update function definitions in minix.h.
--- fs/minix/Makefile | 2 +- fs/minix/inode.c | 35 +- fs/minix/itree.c | 744 ++++++++++++++++++++++++++++++++++++++++ fs/minix/itree_common.c | 374 -------------------- fs/minix/itree_v1.c | 67 ---- fs/minix/itree_v2.c | 75 ---- fs/minix/minix.h | 26 +- 7 files changed, 774 insertions(+), 549 deletions(-) create mode 100644 fs/minix/itree.c delete mode 100644 fs/minix/itree_common.c delete mode 100644 fs/minix/itree_v1.c delete mode 100644 fs/minix/itree_v2.c
fs/minix/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_MINIX_FS) += minix.o -minix-objs := bitmap.o itree_v1.o itree_v2.o namei.o inode.o file.o dir.o +minix-objs := bitmap.o itree.o namei.o inode.o file.o dir.o
fs/minix/inode.c +11 -24
@@ -298,6 +298,15 @@ static int minix_fill_super(struct super_block *s, struct fs_context *fc) }else goto out_no_fs; +/* Set the direct block and indirect block depth now that the rest of +* the version-specific settings have been set. +*/ + sbi->s_direct =MINIX_DIRECT ;/* Always the same. */ +if (sbi->s_version ==MINIX_V1 ) + sbi->s_depth =MINIX_V1_DEPTH ; +else + sbi->s_depth =MINIX_V2_DEPTH ; +if (!minix_check_superblock (s)) goto out_illegal_sb;
@@ -436,15 +445,6 @@ static int minix_statfs(struct dentry *dentry, struct kstatfs *buf)return 0 ; } -static int minix_get_block (struct inode *inode,sector_t block, -struct buffer_head *bh_result,int create) -{ -if (INODE_VERSION (inode) ==MINIX_V1 ) -return V1_minix_get_block (inode, block, bh_result, create); -else -return V2_minix_get_block (inode, block, bh_result, create); -} -static int minix_writepages (struct address_space *mapping,struct writeback_control *wbc) {
@@ -714,26 +714,13 @@ int minix_getattr(struct mnt_idmap *idmap, const struct path *path,generic_fillattr (&nop_mnt_idmap, request_mask, inode, stat);if (INODE_VERSION (inode) ==MINIX_V1 ) - stat->blocks = (BLOCK_SIZE /512 ) *V1_minix_blocks (stat->size, sb); + stat->blocks = (BLOCK_SIZE /512 ) *minix_blocks (stat->size, sb);else - stat->blocks = (sb->s_blocksize /512 ) *V2_minix_blocks (stat->size, sb); + stat->blocks = (sb->s_blocksize /512 ) *minix_blocks (stat->size, sb); stat->blksize = sb->s_blocksize;return 0 ; } -/* -* The function that is called for file truncation. -*/ -void minix_truncate (struct inode * inode) -{ -if (!(S_ISREG (inode->i_mode) ||S_ISDIR (inode->i_mode) ||S_ISLNK (inode->i_mode))) -return ; -if (INODE_VERSION (inode) ==MINIX_V1 ) -V1_minix_truncate (inode); -else -V2_minix_truncate (inode); -} -static struct file_system_type minix_fs_type = { .owner =THIS_MODULE , .name ="minix" ,
fs/minix/itree.c new file +744 -0
744 lines not on this page · raw message
fs/minix/itree_common.c deleted +0 -374
374 lines not on this page · raw message
fs/minix/itree_v1.c deleted +0 -67
67 lines not on this page · raw message
fs/minix/itree_v2.c deleted +0 -75
75 lines not on this page · raw message
fs/minix/minix.h +18 -8
@@ -11,13 +11,21 @@#define MINIX_V2 0x0002/* minix V2 fs */ #define MINIX_V3 0x0003/* minix V3 fs */ +/* Block storage layout. All versions have 7 direct blocks, but they have +* differing depths: V1 only has doubly indirect blocks, while V2/V3 have +* trebly indirect blocks. Thus, V1 depth is 3 while V2 and V3 are 4. +*/ +enum {MINIX_DIRECT =7 ,MINIX_V1_DEPTH =3 ,MINIX_V2_DEPTH =4 }; +/* * minix fs inode data in memory */ +#define MINIX_INODE_DATA_ELEMENTS 16 +struct minix_inode_info {union { -__u16 i1_data[16 ]; -__u32 i2_data[16 ]; +__u16 i1_data[MINIX_INODE_DATA_ELEMENTS ]; +__u32 i2_data[MINIX_INODE_DATA_ELEMENTS ]; } u;struct mapping_metadata_bhs i_metadata_bhs;struct inode vfs_inode;
@@ -41,6 +49,11 @@ struct minix_sb_info {struct minix_super_block * s_ms;unsigned short s_mount_state;unsigned short s_version; +/* Just set the # of direct blocks and depth of indirect blocks once +* in the sb info instead of having split itree functions and all that. +*/ +unsigned short s_direct; +unsigned short s_depth; }; void __minix_error_inode(struct inode *inode, const char *function,
@@ -61,14 +74,11 @@ int minix_prepare_chunk(struct folio *folio, loff_t pos, unsigned len);struct mapping_metadata_bhs *minix_get_metadata_bhs (struct inode *inode);int minix_fsync (struct file *file,loff_t start,loff_t end,int datasync); -extern void V1_minix_truncate (struct inode *); -extern void V2_minix_truncate (struct inode *);extern void minix_truncate (struct inode *);extern void minix_set_inode (struct inode *,dev_t ); -extern int V1_minix_get_block (struct inode *,long ,struct buffer_head *,int ); -extern int V2_minix_get_block (struct inode *,long ,struct buffer_head *,int ); -extern unsigned V1_minix_blocks (loff_t ,struct super_block *); -extern unsigned V2_minix_blocks (loff_t ,struct super_block *); +extern int minix_get_block (struct inode *inode,sector_t block, +struct buffer_head *bh,int create); +extern unsigned int minix_blocks (loff_t size,struct super_block *sb);struct minix_dir_entry *minix_find_entry (struct dentry *,struct folio **);int minix_add_link (struct dentry *,struct inode *);
Signature · 1 line
2.47.3
kernel test robot
build botRe: [RFC PATCH 1/1] minix: unify the v1 and v2/v3 itree code paths
kernel test robot noticed the following build warnings:
Hi Jeremy,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on v7.2-rc7]
[also build test WARNING on linus/master next-20260814] [cannot apply to brauner-vfs/vfs.all]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jeremy-Bingham/minix-unify-the-v1-and-v2-v3-itree-code-paths/20260814-012958 base: v7.2-rc7 patch link: https://lore.kernel.org/r/d9f07a9037d019043ed24d8245d9a58ce2c669f0.1783324260.git.jbingham%40gmail.com patch subject: [RFC PATCH 1/1] minix: unify the v1 and v2/v3 itree code paths config: s390-randconfig-r062-20260817 (https://download.01.org/0day-ci/archive/20260817/[email protected]/config) compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
cocci warnings: (new ones prefixed by >>)
>> fs/minix/itree.c:420:3-8: WARNING: NULL check before some freeing functions is not needed.
fs/minix/itree.c:422:3-8: WARNING: NULL check before some freeing functions is not needed. fs/minix/itree.c:717:2-7: WARNING: NULL check before some freeing functions is not needed. fs/minix/itree.c:719:2-7: WARNING: NULL check before some freeing functions is not needed.
vim +420 fs/minix/itree.c
401
402 /* offsets and chain have DEPTH elements. Since DEPTH varies between versions of
403 * the minix filesystems, instead of having two different definitions of DEPTH
404 * and two different versions of this function just allocate the offsets and
405 * chain arrays dynamically. This does require remembering to free them.
406 */
407 int minix_get_block(struct inode *inode, sector_t block,
408 struct buffer_head *bh, int create)
409 {
410 struct super_block *sb = inode->i_sb;
411 struct minix_sb_info *sbi = minix_sb(sb);
412 u8 s_depth = sbi->s_depth;
413 int err = -EIO;
414 int *offsets = kmalloc_array(s_depth, sizeof(int), GFP_KERNEL);
415 Indirect *chain = kmalloc_array(s_depth, sizeof(Indirect), GFP_KERNEL);
416
417 if (offsets == NULL || chain == NULL) {
418 err = -ENOMEM;
419 if (offsets != NULL)
> 420 kfree(offsets);
421 if (chain != NULL)
422 kfree(chain);
423 goto out;
424 }
425
426 Indirect *partial;
427 int left;
428 int depth = block_to_path(inode, block, offsets);
429
430 if (depth == 0)
431 goto out;
432
433 reread:
434 partial = get_branch(inode, depth, offsets, chain, &err);
435
436 /* Simplest case - block found, no allocation needed */
437 if (!partial) {
438 got_it:
439 map_bh(bh, inode->i_sb, block_to_cpu(chain[depth-1].key));
440 /* Clean up and exit */
441 partial = chain+depth-1; /* the whole chain */
442 goto cleanup;
443 }
444
445 /* Next simple case - plain lookup or failed read of indirect block */
446 if (!create || err == -EIO) {
447 cleanup:
448 while (partial > chain) {
449 brelse(partial->bh);
450 partial--;
451 }
452
453 kfree(offsets);
454 kfree(chain);
455 out:
456 return err;
457 }
458
459 /*
460 * Indirect block might be removed by truncate while we were
461 * reading it. Handling of that case (forget what we've got and
462 * reread) is taken out of the main path.
463 */
464 if (err == -EAGAIN)
465 goto changed;
466
467 left = (chain + depth) - partial;
468 err = alloc_branch(inode, left, offsets+(partial-chain), partial);
469 if (err)
470 goto cleanup;
471
472 if (splice_branch(inode, chain, partial, left) < 0)
473 goto changed;
474
475 set_buffer_new(bh);
476 goto got_it;
477
478 changed:
479 while (partial > chain) {
480 brelse(partial->bh);
481 partial--;
482 }
483 goto reread;
484 }
485
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH 1/1] minix: unify the v1 and v2/v3 itree code paths
In reply to Jeremy BinghamI think this is a good idea, and should make the code more maintainable forward, as well as simply your iomap conversion.
A few comments:
fs/minix/inode.c static int minix_fill_super(struct super_block *s, struct fs_context *fc) · line 303
Quoted 3 lines
fs/minix/inode.c +3
-0+/* Set the direct block and indirect block depth now that the rest of +* the version-specific settings have been set. +*/
Please use the normal kernel comment style:
/* * Set the direct block and indirect block depth now that the rest of * the version-specific settings have been set. */
fs/minix/inode.c static int minix_fill_super(struct super_block *s, struct fs_context *fc) · line 304
Quoted 1 line
fs/minix/inode.c +1
-0+ sbi->s_direct =MINIX_DIRECT ;/* Always the same. */
No need for the comment I think.
fs/minix/inode.c int minix_getattr(struct mnt_idmap *idmap, const struct path *path, · line 719
Quoted 7 lines
fs/minix/inode.c +2
-2generic_fillattr (&nop_mnt_idmap, request_mask, inode, stat);if (INODE_VERSION (inode) ==MINIX_V1 ) - stat->blocks = (BLOCK_SIZE /512 ) *V1_minix_blocks (stat->size, sb); + stat->blocks = (BLOCK_SIZE /512 ) *minix_blocks (stat->size, sb);else - stat->blocks = (sb->s_blocksize /512 ) *V2_minix_blocks (stat->size, sb); + stat->blocks = (sb->s_blocksize /512 ) *minix_blocks (stat->size, sb);
v1 always sets s_blocksize to BLOCK_SIZE, so this can simply become and unconditional:
stat->blocks = (sb->s_blocksize / 512) * minix_blocks(stat->size, sb);
fs/minix/itree.c line 4
Quoted 6 lines
+++
b/fs/minix/itree.cfs/minix/itree.c new file +4
-0@@ -0,0 +1,744 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* linux/fs/minix/inode.c
Please do not add (or move) file names in top of file comments, they are a bit pointless. Also we usually don't have an empty line between the SPDX tag and the top of file comment.
Quoted 5 lines
fs/minix/itree.c new file +5
-0+#include <linux/slab.h> +#include "minix.h" + +#define DIRCOUNT 7 +#define INDIRCOUNT (sb) (1 << ((sb)->s_blocksize_bits - 2))
fs/minix/itree.c line 32
Quoted 10 lines
fs/minix/itree.c new file +10
-0+#define MINIX_V1_BLK 512 +#define MINIX_V1_BLK_SHIFT 9 + +/* The different versions of the Minix filesystem also have different block +* sizes. In order to unify the itree functions and not have the split that's +* been in place for decades, we're going to have two separate types for v1 and +* v2/v3 block sizes. This does require having explicit version checks and +* casting block pointers to v1_block_t and a few cases where there's a special +* v1 version of a function that gets called where it's necessary to do it that +* way.
Comment usually should describe the current code and not the history. There's a few exception where the history really matters like for bad on-disk or on-wire formats set in stone.
fs/minix/itree.c line 64
Quoted 2 lines
fs/minix/itree.c new file +2
-0+* files, the last master commit before they were merged and altered was +* 87320be9f0d24fce67631b7eef919f0b79c3e45c.
Also not needed, git log/blame will tell us easily.
fs/minix/itree.c line 193
Quoted 1 line
fs/minix/itree.c new file +1
-0+static inline int v2_block_to_path (struct inode *inode,long block,int *offsets)
Overly long line. (a few more below)
fs/minix/itree.c line 367
Quoted 5 lines
fs/minix/itree.c new file +5
-0+/* same business with chain as before */ +static inline int splice_branch (struct inode *inode, +Indirect *chain, +Indirect *where, +int num)
static inline int splice_branch(struct inode *inode, Indirect *chain,
Indirect *where, int num)
Similar for a few other functions.
fs/minix/minix.h int minix_prepare_chunk(struct folio *folio, loff_t pos, unsigned len); · line 81
Quoted 3 lines
fs/minix/minix.h +3
-0+extern int minix_get_block (struct inode *inode,sector_t block, +struct buffer_head *bh,int create); +extern unsigned int minix_blocks (loff_t size,struct super_block *sb);
Please drop the extern for all function declarations that you touch.
Re: [RFC PATCH 1/1] minix: unify the v1 and v2/v3 itree code paths
Quoted 2 lines · Christoph Hellwig
I think this is a good idea, and should make the code more maintainable forward, as well as simply your iomap conversion.
It should help immensely with both of those things, I think. More than one reviewer of the iomap conversion has been left scratching their heads over the weird tricks both required for the iomap conversion and simply already present in the module.
Quoted 8 lines
A few comments:
fs/minix/inode.c static int minix_fill_super(struct super_block *s, struct fs_context *fc) · line 303
Quoted 3 lines
fs/minix/inode.c +3
-0+/* Set the direct block and indirect block depth now that the rest of +* the version-specific settings have been set. +*/ Please use the normal kernel comment style:
Quoted 8 lines
/* * Set the direct block and indirect block depth now that the rest of * the version-specific settings have been set. */fs/minix/inode.c static int minix_fill_super(struct super_block *s, struct fs_context *fc) · line 304
Quoted 1 line
fs/minix/inode.c +1
-0+ sbi->s_direct =MINIX_DIRECT ;/* Always the same. */ No need for the comment I think.
As I believe I said somewhere, I explicitly loaded this RFC patch to explain what I was doing since I was looking for comments on it. I erred on the side of verbosity because I was looking to undo something that had been this way for at least 21 years, and probably much longer. I was already planning on stripping the excess comments out, but thank you for the reminders about style and line length. This applies to all the comment and style comments below, to save space.
Quoted 11 lines
fs/minix/inode.c int minix_getattr(struct mnt_idmap *idmap, const struct path *path, · line 719
Quoted 7 lines
fs/minix/inode.c +2
-2generic_fillattr (&nop_mnt_idmap, request_mask, inode, stat);if (INODE_VERSION (inode) ==MINIX_V1 ) - stat->blocks = (BLOCK_SIZE /512 ) *V1_minix_blocks (stat->size, sb); + stat->blocks = (BLOCK_SIZE /512 ) *minix_blocks (stat->size, sb);else - stat->blocks = (sb->s_blocksize /512 ) *V2_minix_blocks (stat->size, sb); + stat->blocks = (sb->s_blocksize /512 ) *minix_blocks (stat->size, sb);v1 always sets
s_blocksizetoBLOCK_SIZE, so this can simply become and unconditional:
Quoted 1 line
stat->blocks = (sb->s_blocksize / 512) * minix_blocks(stat->size, sb);
Noted.
Quoted 5 lines
fs/minix/minix.h int minix_prepare_chunk(struct folio *folio, loff_t pos, unsigned len); · line 81
Quoted 3 lines
fs/minix/minix.h +3
-0+extern int minix_get_block (struct inode *inode,sector_t block, +struct buffer_head *bh,int create); +extern unsigned int minix_blocks (loff_t size,struct super_block *sb);Please drop the extern for all function declarations that you touch.
I figured out that we no longer need to have 'extern' in the header declarations after I submitted this patch, but thank you for the reminder.
Somehow this reply hasn't shown up in my inbox yet, but fortunately I was able to find it. I'll get this turned around and send out the new and improved versio shortly.
Thanks again,
-j