2 * linux/fs/minix/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 1996 Gertjan van Wingerde
9 * Modified for 680x0 by Andreas Schwab
10 * Updated to filesystem version 3 by Daniel Aragones
13 #include <linux/module.h>
15 #include <linux/buffer_head.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/highuid.h>
19 #include <linux/vfs.h>
20 #include <linux/writeback.h>
22 static int minix_write_inode(struct inode *inode,
23 struct writeback_control *wbc);
24 static int minix_statfs(struct dentry *dentry, struct kstatfs *buf);
25 static int minix_remount (struct super_block * sb, int * flags, char * data);
27 static void minix_evict_inode(struct inode *inode)
29 truncate_inode_pages_final(&inode->i_data);
30 if (!inode->i_nlink) {
32 minix_truncate(inode);
34 invalidate_inode_buffers(inode);
37 minix_free_inode(inode);
40 static void minix_put_super(struct super_block *sb)
43 struct minix_sb_info *sbi = minix_sb(sb);
46 if (sbi->s_version != MINIX_V3) /* s_state is now out from V3 sb */
47 sbi->s_ms->s_state = sbi->s_mount_state;
48 mark_buffer_dirty(sbi->s_sbh);
50 for (i = 0; i < sbi->s_imap_blocks; i++)
51 brelse(sbi->s_imap[i]);
52 for (i = 0; i < sbi->s_zmap_blocks; i++)
53 brelse(sbi->s_zmap[i]);
60 static struct kmem_cache * minix_inode_cachep;
62 static struct inode *minix_alloc_inode(struct super_block *sb)
64 struct minix_inode_info *ei;
65 ei = kmem_cache_alloc(minix_inode_cachep, GFP_KERNEL);
68 return &ei->vfs_inode;
71 static void minix_i_callback(struct rcu_head *head)
73 struct inode *inode = container_of(head, struct inode, i_rcu);
74 kmem_cache_free(minix_inode_cachep, minix_i(inode));
77 static void minix_destroy_inode(struct inode *inode)
79 call_rcu(&inode->i_rcu, minix_i_callback);
82 static void init_once(void *foo)
84 struct minix_inode_info *ei = (struct minix_inode_info *) foo;
86 inode_init_once(&ei->vfs_inode);
89 static int __init init_inodecache(void)
91 minix_inode_cachep = kmem_cache_create("minix_inode_cache",
92 sizeof(struct minix_inode_info),
93 0, (SLAB_RECLAIM_ACCOUNT|
94 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
96 if (minix_inode_cachep == NULL)
101 static void destroy_inodecache(void)
104 * Make sure all delayed rcu free inodes are flushed before we
108 kmem_cache_destroy(minix_inode_cachep);
111 static const struct super_operations minix_sops = {
112 .alloc_inode = minix_alloc_inode,
113 .destroy_inode = minix_destroy_inode,
114 .write_inode = minix_write_inode,
115 .evict_inode = minix_evict_inode,
116 .put_super = minix_put_super,
117 .statfs = minix_statfs,
118 .remount_fs = minix_remount,
121 static int minix_remount (struct super_block * sb, int * flags, char * data)
123 struct minix_sb_info * sbi = minix_sb(sb);
124 struct minix_super_block * ms;
128 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
130 if (*flags & SB_RDONLY) {
131 if (ms->s_state & MINIX_VALID_FS ||
132 !(sbi->s_mount_state & MINIX_VALID_FS))
134 /* Mounting a rw partition read-only. */
135 if (sbi->s_version != MINIX_V3)
136 ms->s_state = sbi->s_mount_state;
137 mark_buffer_dirty(sbi->s_sbh);
139 /* Mount a partition which is read-only, read-write. */
140 if (sbi->s_version != MINIX_V3) {
141 sbi->s_mount_state = ms->s_state;
142 ms->s_state &= ~MINIX_VALID_FS;
144 sbi->s_mount_state = MINIX_VALID_FS;
146 mark_buffer_dirty(sbi->s_sbh);
148 if (!(sbi->s_mount_state & MINIX_VALID_FS))
149 printk("MINIX-fs warning: remounting unchecked fs, "
150 "running fsck is recommended\n");
151 else if ((sbi->s_mount_state & MINIX_ERROR_FS))
152 printk("MINIX-fs warning: remounting fs with errors, "
153 "running fsck is recommended\n");
158 static bool minix_check_superblock(struct super_block *sb)
160 struct minix_sb_info *sbi = minix_sb(sb);
162 if (sbi->s_imap_blocks == 0 || sbi->s_zmap_blocks == 0)
166 * s_max_size must not exceed the block mapping limitation. This check
167 * is only needed for V1 filesystems, since V2/V3 support an extra level
168 * of indirect blocks which places the limit well above U32_MAX.
170 if (sbi->s_version == MINIX_V1 &&
171 sb->s_maxbytes > (7 + 512 + 512*512) * BLOCK_SIZE)
177 static int minix_fill_super(struct super_block *s, void *data, int silent)
179 struct buffer_head *bh;
180 struct buffer_head **map;
181 struct minix_super_block *ms;
182 struct minix3_super_block *m3s = NULL;
183 unsigned long i, block;
184 struct inode *root_inode;
185 struct minix_sb_info *sbi;
188 sbi = kzalloc(sizeof(struct minix_sb_info), GFP_KERNEL);
193 BUILD_BUG_ON(32 != sizeof (struct minix_inode));
194 BUILD_BUG_ON(64 != sizeof(struct minix2_inode));
196 if (!sb_set_blocksize(s, BLOCK_SIZE))
199 if (!(bh = sb_bread(s, 1)))
202 ms = (struct minix_super_block *) bh->b_data;
205 sbi->s_mount_state = ms->s_state;
206 sbi->s_ninodes = ms->s_ninodes;
207 sbi->s_nzones = ms->s_nzones;
208 sbi->s_imap_blocks = ms->s_imap_blocks;
209 sbi->s_zmap_blocks = ms->s_zmap_blocks;
210 sbi->s_firstdatazone = ms->s_firstdatazone;
211 sbi->s_log_zone_size = ms->s_log_zone_size;
212 s->s_maxbytes = ms->s_max_size;
213 s->s_magic = ms->s_magic;
214 if (s->s_magic == MINIX_SUPER_MAGIC) {
215 sbi->s_version = MINIX_V1;
218 s->s_max_links = MINIX_LINK_MAX;
219 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
220 sbi->s_version = MINIX_V1;
223 s->s_max_links = MINIX_LINK_MAX;
224 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
225 sbi->s_version = MINIX_V2;
226 sbi->s_nzones = ms->s_zones;
229 s->s_max_links = MINIX2_LINK_MAX;
230 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
231 sbi->s_version = MINIX_V2;
232 sbi->s_nzones = ms->s_zones;
235 s->s_max_links = MINIX2_LINK_MAX;
236 } else if ( *(__u16 *)(bh->b_data + 24) == MINIX3_SUPER_MAGIC) {
237 m3s = (struct minix3_super_block *) bh->b_data;
238 s->s_magic = m3s->s_magic;
239 sbi->s_imap_blocks = m3s->s_imap_blocks;
240 sbi->s_zmap_blocks = m3s->s_zmap_blocks;
241 sbi->s_firstdatazone = m3s->s_firstdatazone;
242 sbi->s_log_zone_size = m3s->s_log_zone_size;
243 s->s_maxbytes = m3s->s_max_size;
244 sbi->s_ninodes = m3s->s_ninodes;
245 sbi->s_nzones = m3s->s_zones;
248 sbi->s_version = MINIX_V3;
249 sbi->s_mount_state = MINIX_VALID_FS;
250 sb_set_blocksize(s, m3s->s_blocksize);
251 s->s_max_links = MINIX2_LINK_MAX;
255 if (!minix_check_superblock(s))
259 * Allocate the buffer map to keep the superblock small.
261 i = (sbi->s_imap_blocks + sbi->s_zmap_blocks) * sizeof(bh);
262 map = kzalloc(i, GFP_KERNEL);
265 sbi->s_imap = &map[0];
266 sbi->s_zmap = &map[sbi->s_imap_blocks];
269 for (i=0 ; i < sbi->s_imap_blocks ; i++) {
270 if (!(sbi->s_imap[i]=sb_bread(s, block)))
274 for (i=0 ; i < sbi->s_zmap_blocks ; i++) {
275 if (!(sbi->s_zmap[i]=sb_bread(s, block)))
280 minix_set_bit(0,sbi->s_imap[0]->b_data);
281 minix_set_bit(0,sbi->s_zmap[0]->b_data);
283 /* Apparently minix can create filesystems that allocate more blocks for
284 * the bitmaps than needed. We simply ignore that, but verify it didn't
285 * create one with not enough blocks and bail out if so.
287 block = minix_blocks_needed(sbi->s_ninodes, s->s_blocksize);
288 if (sbi->s_imap_blocks < block) {
289 printk("MINIX-fs: file system does not have enough "
290 "imap blocks allocated. Refusing to mount.\n");
294 block = minix_blocks_needed(
295 (sbi->s_nzones - sbi->s_firstdatazone + 1),
297 if (sbi->s_zmap_blocks < block) {
298 printk("MINIX-fs: file system does not have enough "
299 "zmap blocks allocated. Refusing to mount.\n");
303 /* set up enough so that it can read an inode */
304 s->s_op = &minix_sops;
305 root_inode = minix_iget(s, MINIX_ROOT_INO);
306 if (IS_ERR(root_inode)) {
307 ret = PTR_ERR(root_inode);
312 s->s_root = d_make_root(root_inode);
317 if (sbi->s_version != MINIX_V3) /* s_state is now out from V3 sb */
318 ms->s_state &= ~MINIX_VALID_FS;
319 mark_buffer_dirty(bh);
321 if (!(sbi->s_mount_state & MINIX_VALID_FS))
322 printk("MINIX-fs: mounting unchecked file system, "
323 "running fsck is recommended\n");
324 else if (sbi->s_mount_state & MINIX_ERROR_FS)
325 printk("MINIX-fs: mounting file system with errors, "
326 "running fsck is recommended\n");
332 printk("MINIX-fs: get root inode failed\n");
336 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
338 for (i = 0; i < sbi->s_imap_blocks; i++)
339 brelse(sbi->s_imap[i]);
340 for (i = 0; i < sbi->s_zmap_blocks; i++)
341 brelse(sbi->s_zmap[i]);
348 printk("MINIX-fs: can't allocate map\n");
353 printk("MINIX-fs: bad superblock\n");
358 printk("VFS: Can't find a Minix filesystem V1 | V2 | V3 "
359 "on device %s.\n", s->s_id);
365 printk("MINIX-fs: blocksize too small for device\n");
369 printk("MINIX-fs: unable to read superblock\n");
376 static int minix_statfs(struct dentry *dentry, struct kstatfs *buf)
378 struct super_block *sb = dentry->d_sb;
379 struct minix_sb_info *sbi = minix_sb(sb);
380 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
381 buf->f_type = sb->s_magic;
382 buf->f_bsize = sb->s_blocksize;
383 buf->f_blocks = (sbi->s_nzones - sbi->s_firstdatazone) << sbi->s_log_zone_size;
384 buf->f_bfree = minix_count_free_blocks(sb);
385 buf->f_bavail = buf->f_bfree;
386 buf->f_files = sbi->s_ninodes;
387 buf->f_ffree = minix_count_free_inodes(sb);
388 buf->f_namelen = sbi->s_namelen;
389 buf->f_fsid.val[0] = (u32)id;
390 buf->f_fsid.val[1] = (u32)(id >> 32);
395 static int minix_get_block(struct inode *inode, sector_t block,
396 struct buffer_head *bh_result, int create)
398 if (INODE_VERSION(inode) == MINIX_V1)
399 return V1_minix_get_block(inode, block, bh_result, create);
401 return V2_minix_get_block(inode, block, bh_result, create);
404 static int minix_writepage(struct page *page, struct writeback_control *wbc)
406 return block_write_full_page(page, minix_get_block, wbc);
409 static int minix_readpage(struct file *file, struct page *page)
411 return block_read_full_page(page,minix_get_block);
414 int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len)
416 return __block_write_begin(page, pos, len, minix_get_block);
419 static void minix_write_failed(struct address_space *mapping, loff_t to)
421 struct inode *inode = mapping->host;
423 if (to > inode->i_size) {
424 truncate_pagecache(inode, inode->i_size);
425 minix_truncate(inode);
429 static int minix_write_begin(struct file *file, struct address_space *mapping,
430 loff_t pos, unsigned len, unsigned flags,
431 struct page **pagep, void **fsdata)
435 ret = block_write_begin(mapping, pos, len, flags, pagep,
438 minix_write_failed(mapping, pos + len);
443 static sector_t minix_bmap(struct address_space *mapping, sector_t block)
445 return generic_block_bmap(mapping,block,minix_get_block);
448 static const struct address_space_operations minix_aops = {
449 .readpage = minix_readpage,
450 .writepage = minix_writepage,
451 .write_begin = minix_write_begin,
452 .write_end = generic_write_end,
456 static const struct inode_operations minix_symlink_inode_operations = {
457 .get_link = page_get_link,
458 .getattr = minix_getattr,
461 void minix_set_inode(struct inode *inode, dev_t rdev)
463 if (S_ISREG(inode->i_mode)) {
464 inode->i_op = &minix_file_inode_operations;
465 inode->i_fop = &minix_file_operations;
466 inode->i_mapping->a_ops = &minix_aops;
467 } else if (S_ISDIR(inode->i_mode)) {
468 inode->i_op = &minix_dir_inode_operations;
469 inode->i_fop = &minix_dir_operations;
470 inode->i_mapping->a_ops = &minix_aops;
471 } else if (S_ISLNK(inode->i_mode)) {
472 inode->i_op = &minix_symlink_inode_operations;
473 inode_nohighmem(inode);
474 inode->i_mapping->a_ops = &minix_aops;
476 init_special_inode(inode, inode->i_mode, rdev);
480 * The minix V1 function to read an inode.
482 static struct inode *V1_minix_iget(struct inode *inode)
484 struct buffer_head * bh;
485 struct minix_inode * raw_inode;
486 struct minix_inode_info *minix_inode = minix_i(inode);
489 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
492 return ERR_PTR(-EIO);
494 if (raw_inode->i_nlinks == 0) {
495 printk("MINIX-fs: deleted inode referenced: %lu\n",
499 return ERR_PTR(-ESTALE);
501 inode->i_mode = raw_inode->i_mode;
502 i_uid_write(inode, raw_inode->i_uid);
503 i_gid_write(inode, raw_inode->i_gid);
504 set_nlink(inode, raw_inode->i_nlinks);
505 inode->i_size = raw_inode->i_size;
506 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = raw_inode->i_time;
507 inode->i_mtime.tv_nsec = 0;
508 inode->i_atime.tv_nsec = 0;
509 inode->i_ctime.tv_nsec = 0;
511 for (i = 0; i < 9; i++)
512 minix_inode->u.i1_data[i] = raw_inode->i_zone[i];
513 minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
515 unlock_new_inode(inode);
520 * The minix V2 function to read an inode.
522 static struct inode *V2_minix_iget(struct inode *inode)
524 struct buffer_head * bh;
525 struct minix2_inode * raw_inode;
526 struct minix_inode_info *minix_inode = minix_i(inode);
529 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
532 return ERR_PTR(-EIO);
534 if (raw_inode->i_nlinks == 0) {
535 printk("MINIX-fs: deleted inode referenced: %lu\n",
539 return ERR_PTR(-ESTALE);
541 inode->i_mode = raw_inode->i_mode;
542 i_uid_write(inode, raw_inode->i_uid);
543 i_gid_write(inode, raw_inode->i_gid);
544 set_nlink(inode, raw_inode->i_nlinks);
545 inode->i_size = raw_inode->i_size;
546 inode->i_mtime.tv_sec = raw_inode->i_mtime;
547 inode->i_atime.tv_sec = raw_inode->i_atime;
548 inode->i_ctime.tv_sec = raw_inode->i_ctime;
549 inode->i_mtime.tv_nsec = 0;
550 inode->i_atime.tv_nsec = 0;
551 inode->i_ctime.tv_nsec = 0;
553 for (i = 0; i < 10; i++)
554 minix_inode->u.i2_data[i] = raw_inode->i_zone[i];
555 minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
557 unlock_new_inode(inode);
562 * The global function to read an inode.
564 struct inode *minix_iget(struct super_block *sb, unsigned long ino)
568 inode = iget_locked(sb, ino);
570 return ERR_PTR(-ENOMEM);
571 if (!(inode->i_state & I_NEW))
574 if (INODE_VERSION(inode) == MINIX_V1)
575 return V1_minix_iget(inode);
577 return V2_minix_iget(inode);
581 * The minix V1 function to synchronize an inode.
583 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
585 struct buffer_head * bh;
586 struct minix_inode * raw_inode;
587 struct minix_inode_info *minix_inode = minix_i(inode);
590 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
593 raw_inode->i_mode = inode->i_mode;
594 raw_inode->i_uid = fs_high2lowuid(i_uid_read(inode));
595 raw_inode->i_gid = fs_high2lowgid(i_gid_read(inode));
596 raw_inode->i_nlinks = inode->i_nlink;
597 raw_inode->i_size = inode->i_size;
598 raw_inode->i_time = inode->i_mtime.tv_sec;
599 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
600 raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
601 else for (i = 0; i < 9; i++)
602 raw_inode->i_zone[i] = minix_inode->u.i1_data[i];
603 mark_buffer_dirty(bh);
608 * The minix V2 function to synchronize an inode.
610 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
612 struct buffer_head * bh;
613 struct minix2_inode * raw_inode;
614 struct minix_inode_info *minix_inode = minix_i(inode);
617 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
620 raw_inode->i_mode = inode->i_mode;
621 raw_inode->i_uid = fs_high2lowuid(i_uid_read(inode));
622 raw_inode->i_gid = fs_high2lowgid(i_gid_read(inode));
623 raw_inode->i_nlinks = inode->i_nlink;
624 raw_inode->i_size = inode->i_size;
625 raw_inode->i_mtime = inode->i_mtime.tv_sec;
626 raw_inode->i_atime = inode->i_atime.tv_sec;
627 raw_inode->i_ctime = inode->i_ctime.tv_sec;
628 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
629 raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
630 else for (i = 0; i < 10; i++)
631 raw_inode->i_zone[i] = minix_inode->u.i2_data[i];
632 mark_buffer_dirty(bh);
636 static int minix_write_inode(struct inode *inode, struct writeback_control *wbc)
639 struct buffer_head *bh;
641 if (INODE_VERSION(inode) == MINIX_V1)
642 bh = V1_minix_update_inode(inode);
644 bh = V2_minix_update_inode(inode);
647 if (wbc->sync_mode == WB_SYNC_ALL && buffer_dirty(bh)) {
648 sync_dirty_buffer(bh);
649 if (buffer_req(bh) && !buffer_uptodate(bh)) {
650 printk("IO error syncing minix inode [%s:%08lx]\n",
651 inode->i_sb->s_id, inode->i_ino);
659 int minix_getattr(const struct path *path, struct kstat *stat,
660 u32 request_mask, unsigned int flags)
662 struct super_block *sb = path->dentry->d_sb;
663 struct inode *inode = d_inode(path->dentry);
665 generic_fillattr(inode, stat);
666 if (INODE_VERSION(inode) == MINIX_V1)
667 stat->blocks = (BLOCK_SIZE / 512) * V1_minix_blocks(stat->size, sb);
669 stat->blocks = (sb->s_blocksize / 512) * V2_minix_blocks(stat->size, sb);
670 stat->blksize = sb->s_blocksize;
675 * The function that is called for file truncation.
677 void minix_truncate(struct inode * inode)
679 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
681 if (INODE_VERSION(inode) == MINIX_V1)
682 V1_minix_truncate(inode);
684 V2_minix_truncate(inode);
687 static struct dentry *minix_mount(struct file_system_type *fs_type,
688 int flags, const char *dev_name, void *data)
690 return mount_bdev(fs_type, flags, dev_name, data, minix_fill_super);
693 static struct file_system_type minix_fs_type = {
694 .owner = THIS_MODULE,
696 .mount = minix_mount,
697 .kill_sb = kill_block_super,
698 .fs_flags = FS_REQUIRES_DEV,
700 MODULE_ALIAS_FS("minix");
702 static int __init init_minix_fs(void)
704 int err = init_inodecache();
707 err = register_filesystem(&minix_fs_type);
712 destroy_inodecache();
717 static void __exit exit_minix_fs(void)
719 unregister_filesystem(&minix_fs_type);
720 destroy_inodecache();
723 module_init(init_minix_fs)
724 module_exit(exit_minix_fs)
725 MODULE_LICENSE("GPL");