1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/minix/inode.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1996 Gertjan van Wingerde
10 * Modified for 680x0 by Andreas Schwab
11 * Updated to filesystem version 3 by Daniel Aragones
14 #include <linux/module.h>
16 #include <linux/buffer_head.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/highuid.h>
20 #include <linux/vfs.h>
21 #include <linux/writeback.h>
23 static int minix_write_inode(struct inode *inode,
24 struct writeback_control *wbc);
25 static int minix_statfs(struct dentry *dentry, struct kstatfs *buf);
26 static int minix_remount (struct super_block * sb, int * flags, char * data);
28 static void minix_evict_inode(struct inode *inode)
30 truncate_inode_pages_final(&inode->i_data);
31 if (!inode->i_nlink) {
33 minix_truncate(inode);
35 invalidate_inode_buffers(inode);
38 minix_free_inode(inode);
41 static void minix_put_super(struct super_block *sb)
44 struct minix_sb_info *sbi = minix_sb(sb);
47 if (sbi->s_version != MINIX_V3) /* s_state is now out from V3 sb */
48 sbi->s_ms->s_state = sbi->s_mount_state;
49 mark_buffer_dirty(sbi->s_sbh);
51 for (i = 0; i < sbi->s_imap_blocks; i++)
52 brelse(sbi->s_imap[i]);
53 for (i = 0; i < sbi->s_zmap_blocks; i++)
54 brelse(sbi->s_zmap[i]);
61 static struct kmem_cache * minix_inode_cachep;
63 static struct inode *minix_alloc_inode(struct super_block *sb)
65 struct minix_inode_info *ei;
66 ei = alloc_inode_sb(sb, minix_inode_cachep, GFP_KERNEL);
69 return &ei->vfs_inode;
72 static void minix_free_in_core_inode(struct inode *inode)
74 kmem_cache_free(minix_inode_cachep, minix_i(inode));
77 static void init_once(void *foo)
79 struct minix_inode_info *ei = (struct minix_inode_info *) foo;
81 inode_init_once(&ei->vfs_inode);
84 static int __init init_inodecache(void)
86 minix_inode_cachep = kmem_cache_create("minix_inode_cache",
87 sizeof(struct minix_inode_info),
88 0, (SLAB_RECLAIM_ACCOUNT|
89 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
91 if (minix_inode_cachep == NULL)
96 static void destroy_inodecache(void)
99 * Make sure all delayed rcu free inodes are flushed before we
103 kmem_cache_destroy(minix_inode_cachep);
106 static const struct super_operations minix_sops = {
107 .alloc_inode = minix_alloc_inode,
108 .free_inode = minix_free_in_core_inode,
109 .write_inode = minix_write_inode,
110 .evict_inode = minix_evict_inode,
111 .put_super = minix_put_super,
112 .statfs = minix_statfs,
113 .remount_fs = minix_remount,
116 static int minix_remount (struct super_block * sb, int * flags, char * data)
118 struct minix_sb_info * sbi = minix_sb(sb);
119 struct minix_super_block * ms;
123 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
125 if (*flags & SB_RDONLY) {
126 if (ms->s_state & MINIX_VALID_FS ||
127 !(sbi->s_mount_state & MINIX_VALID_FS))
129 /* Mounting a rw partition read-only. */
130 if (sbi->s_version != MINIX_V3)
131 ms->s_state = sbi->s_mount_state;
132 mark_buffer_dirty(sbi->s_sbh);
134 /* Mount a partition which is read-only, read-write. */
135 if (sbi->s_version != MINIX_V3) {
136 sbi->s_mount_state = ms->s_state;
137 ms->s_state &= ~MINIX_VALID_FS;
139 sbi->s_mount_state = MINIX_VALID_FS;
141 mark_buffer_dirty(sbi->s_sbh);
143 if (!(sbi->s_mount_state & MINIX_VALID_FS))
144 printk("MINIX-fs warning: remounting unchecked fs, "
145 "running fsck is recommended\n");
146 else if ((sbi->s_mount_state & MINIX_ERROR_FS))
147 printk("MINIX-fs warning: remounting fs with errors, "
148 "running fsck is recommended\n");
153 static bool minix_check_superblock(struct super_block *sb)
155 struct minix_sb_info *sbi = minix_sb(sb);
157 if (sbi->s_imap_blocks == 0 || sbi->s_zmap_blocks == 0)
161 * s_max_size must not exceed the block mapping limitation. This check
162 * is only needed for V1 filesystems, since V2/V3 support an extra level
163 * of indirect blocks which places the limit well above U32_MAX.
165 if (sbi->s_version == MINIX_V1 &&
166 sb->s_maxbytes > (7 + 512 + 512*512) * BLOCK_SIZE)
172 static int minix_fill_super(struct super_block *s, void *data, int silent)
174 struct buffer_head *bh;
175 struct buffer_head **map;
176 struct minix_super_block *ms;
177 struct minix3_super_block *m3s = NULL;
178 unsigned long i, block;
179 struct inode *root_inode;
180 struct minix_sb_info *sbi;
183 sbi = kzalloc(sizeof(struct minix_sb_info), GFP_KERNEL);
188 BUILD_BUG_ON(32 != sizeof (struct minix_inode));
189 BUILD_BUG_ON(64 != sizeof(struct minix2_inode));
191 if (!sb_set_blocksize(s, BLOCK_SIZE))
194 if (!(bh = sb_bread(s, 1)))
197 ms = (struct minix_super_block *) bh->b_data;
200 sbi->s_mount_state = ms->s_state;
201 sbi->s_ninodes = ms->s_ninodes;
202 sbi->s_nzones = ms->s_nzones;
203 sbi->s_imap_blocks = ms->s_imap_blocks;
204 sbi->s_zmap_blocks = ms->s_zmap_blocks;
205 sbi->s_firstdatazone = ms->s_firstdatazone;
206 sbi->s_log_zone_size = ms->s_log_zone_size;
207 s->s_maxbytes = ms->s_max_size;
208 s->s_magic = ms->s_magic;
209 if (s->s_magic == MINIX_SUPER_MAGIC) {
210 sbi->s_version = MINIX_V1;
213 s->s_max_links = MINIX_LINK_MAX;
214 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
215 sbi->s_version = MINIX_V1;
218 s->s_max_links = MINIX_LINK_MAX;
219 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
220 sbi->s_version = MINIX_V2;
221 sbi->s_nzones = ms->s_zones;
224 s->s_max_links = MINIX2_LINK_MAX;
225 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
226 sbi->s_version = MINIX_V2;
227 sbi->s_nzones = ms->s_zones;
230 s->s_max_links = MINIX2_LINK_MAX;
231 } else if ( *(__u16 *)(bh->b_data + 24) == MINIX3_SUPER_MAGIC) {
232 m3s = (struct minix3_super_block *) bh->b_data;
233 s->s_magic = m3s->s_magic;
234 sbi->s_imap_blocks = m3s->s_imap_blocks;
235 sbi->s_zmap_blocks = m3s->s_zmap_blocks;
236 sbi->s_firstdatazone = m3s->s_firstdatazone;
237 sbi->s_log_zone_size = m3s->s_log_zone_size;
238 s->s_maxbytes = m3s->s_max_size;
239 sbi->s_ninodes = m3s->s_ninodes;
240 sbi->s_nzones = m3s->s_zones;
243 sbi->s_version = MINIX_V3;
244 sbi->s_mount_state = MINIX_VALID_FS;
245 sb_set_blocksize(s, m3s->s_blocksize);
246 s->s_max_links = MINIX2_LINK_MAX;
250 if (!minix_check_superblock(s))
254 * Allocate the buffer map to keep the superblock small.
256 i = (sbi->s_imap_blocks + sbi->s_zmap_blocks) * sizeof(bh);
257 map = kzalloc(i, GFP_KERNEL);
260 sbi->s_imap = &map[0];
261 sbi->s_zmap = &map[sbi->s_imap_blocks];
264 for (i=0 ; i < sbi->s_imap_blocks ; i++) {
265 if (!(sbi->s_imap[i]=sb_bread(s, block)))
269 for (i=0 ; i < sbi->s_zmap_blocks ; i++) {
270 if (!(sbi->s_zmap[i]=sb_bread(s, block)))
275 minix_set_bit(0,sbi->s_imap[0]->b_data);
276 minix_set_bit(0,sbi->s_zmap[0]->b_data);
278 /* Apparently minix can create filesystems that allocate more blocks for
279 * the bitmaps than needed. We simply ignore that, but verify it didn't
280 * create one with not enough blocks and bail out if so.
282 block = minix_blocks_needed(sbi->s_ninodes, s->s_blocksize);
283 if (sbi->s_imap_blocks < block) {
284 printk("MINIX-fs: file system does not have enough "
285 "imap blocks allocated. Refusing to mount.\n");
289 block = minix_blocks_needed(
290 (sbi->s_nzones - sbi->s_firstdatazone + 1),
292 if (sbi->s_zmap_blocks < block) {
293 printk("MINIX-fs: file system does not have enough "
294 "zmap blocks allocated. Refusing to mount.\n");
298 /* set up enough so that it can read an inode */
299 s->s_op = &minix_sops;
301 s->s_time_max = U32_MAX;
302 root_inode = minix_iget(s, MINIX_ROOT_INO);
303 if (IS_ERR(root_inode)) {
304 ret = PTR_ERR(root_inode);
309 s->s_root = d_make_root(root_inode);
314 if (sbi->s_version != MINIX_V3) /* s_state is now out from V3 sb */
315 ms->s_state &= ~MINIX_VALID_FS;
316 mark_buffer_dirty(bh);
318 if (!(sbi->s_mount_state & MINIX_VALID_FS))
319 printk("MINIX-fs: mounting unchecked file system, "
320 "running fsck is recommended\n");
321 else if (sbi->s_mount_state & MINIX_ERROR_FS)
322 printk("MINIX-fs: mounting file system with errors, "
323 "running fsck is recommended\n");
329 printk("MINIX-fs: get root inode failed\n");
333 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
335 for (i = 0; i < sbi->s_imap_blocks; i++)
336 brelse(sbi->s_imap[i]);
337 for (i = 0; i < sbi->s_zmap_blocks; i++)
338 brelse(sbi->s_zmap[i]);
345 printk("MINIX-fs: can't allocate map\n");
350 printk("MINIX-fs: bad superblock\n");
355 printk("VFS: Can't find a Minix filesystem V1 | V2 | V3 "
356 "on device %s.\n", s->s_id);
362 printk("MINIX-fs: blocksize too small for device\n");
366 printk("MINIX-fs: unable to read superblock\n");
373 static int minix_statfs(struct dentry *dentry, struct kstatfs *buf)
375 struct super_block *sb = dentry->d_sb;
376 struct minix_sb_info *sbi = minix_sb(sb);
377 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
378 buf->f_type = sb->s_magic;
379 buf->f_bsize = sb->s_blocksize;
380 buf->f_blocks = (sbi->s_nzones - sbi->s_firstdatazone) << sbi->s_log_zone_size;
381 buf->f_bfree = minix_count_free_blocks(sb);
382 buf->f_bavail = buf->f_bfree;
383 buf->f_files = sbi->s_ninodes;
384 buf->f_ffree = minix_count_free_inodes(sb);
385 buf->f_namelen = sbi->s_namelen;
386 buf->f_fsid = u64_to_fsid(id);
391 static int minix_get_block(struct inode *inode, sector_t block,
392 struct buffer_head *bh_result, int create)
394 if (INODE_VERSION(inode) == MINIX_V1)
395 return V1_minix_get_block(inode, block, bh_result, create);
397 return V2_minix_get_block(inode, block, bh_result, create);
400 static int minix_writepage(struct page *page, struct writeback_control *wbc)
402 return block_write_full_page(page, minix_get_block, wbc);
405 static int minix_read_folio(struct file *file, struct folio *folio)
407 return block_read_full_folio(folio, minix_get_block);
410 int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len)
412 return __block_write_begin(page, pos, len, minix_get_block);
415 static void minix_write_failed(struct address_space *mapping, loff_t to)
417 struct inode *inode = mapping->host;
419 if (to > inode->i_size) {
420 truncate_pagecache(inode, inode->i_size);
421 minix_truncate(inode);
425 static int minix_write_begin(struct file *file, struct address_space *mapping,
426 loff_t pos, unsigned len,
427 struct page **pagep, void **fsdata)
431 ret = block_write_begin(mapping, pos, len, pagep, minix_get_block);
433 minix_write_failed(mapping, pos + len);
438 static sector_t minix_bmap(struct address_space *mapping, sector_t block)
440 return generic_block_bmap(mapping,block,minix_get_block);
443 static const struct address_space_operations minix_aops = {
444 .dirty_folio = block_dirty_folio,
445 .invalidate_folio = block_invalidate_folio,
446 .read_folio = minix_read_folio,
447 .writepage = minix_writepage,
448 .write_begin = minix_write_begin,
449 .write_end = generic_write_end,
451 .direct_IO = noop_direct_IO
454 static const struct inode_operations minix_symlink_inode_operations = {
455 .get_link = page_get_link,
456 .getattr = minix_getattr,
459 void minix_set_inode(struct inode *inode, dev_t rdev)
461 if (S_ISREG(inode->i_mode)) {
462 inode->i_op = &minix_file_inode_operations;
463 inode->i_fop = &minix_file_operations;
464 inode->i_mapping->a_ops = &minix_aops;
465 } else if (S_ISDIR(inode->i_mode)) {
466 inode->i_op = &minix_dir_inode_operations;
467 inode->i_fop = &minix_dir_operations;
468 inode->i_mapping->a_ops = &minix_aops;
469 } else if (S_ISLNK(inode->i_mode)) {
470 inode->i_op = &minix_symlink_inode_operations;
471 inode_nohighmem(inode);
472 inode->i_mapping->a_ops = &minix_aops;
474 init_special_inode(inode, inode->i_mode, rdev);
478 * The minix V1 function to read an inode.
480 static struct inode *V1_minix_iget(struct inode *inode)
482 struct buffer_head * bh;
483 struct minix_inode * raw_inode;
484 struct minix_inode_info *minix_inode = minix_i(inode);
487 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
490 return ERR_PTR(-EIO);
492 if (raw_inode->i_nlinks == 0) {
493 printk("MINIX-fs: deleted inode referenced: %lu\n",
497 return ERR_PTR(-ESTALE);
499 inode->i_mode = raw_inode->i_mode;
500 i_uid_write(inode, raw_inode->i_uid);
501 i_gid_write(inode, raw_inode->i_gid);
502 set_nlink(inode, raw_inode->i_nlinks);
503 inode->i_size = raw_inode->i_size;
504 inode->i_mtime = inode->i_atime = inode_set_ctime(inode, raw_inode->i_time, 0);
506 for (i = 0; i < 9; i++)
507 minix_inode->u.i1_data[i] = raw_inode->i_zone[i];
508 minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
510 unlock_new_inode(inode);
515 * The minix V2 function to read an inode.
517 static struct inode *V2_minix_iget(struct inode *inode)
519 struct buffer_head * bh;
520 struct minix2_inode * raw_inode;
521 struct minix_inode_info *minix_inode = minix_i(inode);
524 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
527 return ERR_PTR(-EIO);
529 if (raw_inode->i_nlinks == 0) {
530 printk("MINIX-fs: deleted inode referenced: %lu\n",
534 return ERR_PTR(-ESTALE);
536 inode->i_mode = raw_inode->i_mode;
537 i_uid_write(inode, raw_inode->i_uid);
538 i_gid_write(inode, raw_inode->i_gid);
539 set_nlink(inode, raw_inode->i_nlinks);
540 inode->i_size = raw_inode->i_size;
541 inode->i_mtime.tv_sec = raw_inode->i_mtime;
542 inode->i_atime.tv_sec = raw_inode->i_atime;
543 inode_set_ctime(inode, raw_inode->i_ctime, 0);
544 inode->i_mtime.tv_nsec = 0;
545 inode->i_atime.tv_nsec = 0;
547 for (i = 0; i < 10; i++)
548 minix_inode->u.i2_data[i] = raw_inode->i_zone[i];
549 minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
551 unlock_new_inode(inode);
556 * The global function to read an inode.
558 struct inode *minix_iget(struct super_block *sb, unsigned long ino)
562 inode = iget_locked(sb, ino);
564 return ERR_PTR(-ENOMEM);
565 if (!(inode->i_state & I_NEW))
568 if (INODE_VERSION(inode) == MINIX_V1)
569 return V1_minix_iget(inode);
571 return V2_minix_iget(inode);
575 * The minix V1 function to synchronize an inode.
577 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
579 struct buffer_head * bh;
580 struct minix_inode * raw_inode;
581 struct minix_inode_info *minix_inode = minix_i(inode);
584 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
587 raw_inode->i_mode = inode->i_mode;
588 raw_inode->i_uid = fs_high2lowuid(i_uid_read(inode));
589 raw_inode->i_gid = fs_high2lowgid(i_gid_read(inode));
590 raw_inode->i_nlinks = inode->i_nlink;
591 raw_inode->i_size = inode->i_size;
592 raw_inode->i_time = inode->i_mtime.tv_sec;
593 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
594 raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
595 else for (i = 0; i < 9; i++)
596 raw_inode->i_zone[i] = minix_inode->u.i1_data[i];
597 mark_buffer_dirty(bh);
602 * The minix V2 function to synchronize an inode.
604 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
606 struct buffer_head * bh;
607 struct minix2_inode * raw_inode;
608 struct minix_inode_info *minix_inode = minix_i(inode);
611 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
614 raw_inode->i_mode = inode->i_mode;
615 raw_inode->i_uid = fs_high2lowuid(i_uid_read(inode));
616 raw_inode->i_gid = fs_high2lowgid(i_gid_read(inode));
617 raw_inode->i_nlinks = inode->i_nlink;
618 raw_inode->i_size = inode->i_size;
619 raw_inode->i_mtime = inode->i_mtime.tv_sec;
620 raw_inode->i_atime = inode->i_atime.tv_sec;
621 raw_inode->i_ctime = inode_get_ctime(inode).tv_sec;
622 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
623 raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
624 else for (i = 0; i < 10; i++)
625 raw_inode->i_zone[i] = minix_inode->u.i2_data[i];
626 mark_buffer_dirty(bh);
630 static int minix_write_inode(struct inode *inode, struct writeback_control *wbc)
633 struct buffer_head *bh;
635 if (INODE_VERSION(inode) == MINIX_V1)
636 bh = V1_minix_update_inode(inode);
638 bh = V2_minix_update_inode(inode);
641 if (wbc->sync_mode == WB_SYNC_ALL && buffer_dirty(bh)) {
642 sync_dirty_buffer(bh);
643 if (buffer_req(bh) && !buffer_uptodate(bh)) {
644 printk("IO error syncing minix inode [%s:%08lx]\n",
645 inode->i_sb->s_id, inode->i_ino);
653 int minix_getattr(struct mnt_idmap *idmap, const struct path *path,
654 struct kstat *stat, u32 request_mask, unsigned int flags)
656 struct super_block *sb = path->dentry->d_sb;
657 struct inode *inode = d_inode(path->dentry);
659 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
660 if (INODE_VERSION(inode) == MINIX_V1)
661 stat->blocks = (BLOCK_SIZE / 512) * V1_minix_blocks(stat->size, sb);
663 stat->blocks = (sb->s_blocksize / 512) * V2_minix_blocks(stat->size, sb);
664 stat->blksize = sb->s_blocksize;
669 * The function that is called for file truncation.
671 void minix_truncate(struct inode * inode)
673 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
675 if (INODE_VERSION(inode) == MINIX_V1)
676 V1_minix_truncate(inode);
678 V2_minix_truncate(inode);
681 static struct dentry *minix_mount(struct file_system_type *fs_type,
682 int flags, const char *dev_name, void *data)
684 return mount_bdev(fs_type, flags, dev_name, data, minix_fill_super);
687 static struct file_system_type minix_fs_type = {
688 .owner = THIS_MODULE,
690 .mount = minix_mount,
691 .kill_sb = kill_block_super,
692 .fs_flags = FS_REQUIRES_DEV,
694 MODULE_ALIAS_FS("minix");
696 static int __init init_minix_fs(void)
698 int err = init_inodecache();
701 err = register_filesystem(&minix_fs_type);
706 destroy_inodecache();
711 static void __exit exit_minix_fs(void)
713 unregister_filesystem(&minix_fs_type);
714 destroy_inodecache();
717 module_init(init_minix_fs)
718 module_exit(exit_minix_fs)
719 MODULE_LICENSE("GPL");