blob: 3f18c21198d7e2a848573e539316844c3fe2e142 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/minix/inode.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Copyright (C) 1996 Gertjan van Wingerde (gertjan@cs.vu.nl)
7 * Minix V2 fs support.
8 *
9 * Modified for 680x0 by Andreas Schwab
10 */
11
12#include <linux/module.h>
13#include "minix.h"
14#include <linux/buffer_head.h>
15#include <linux/slab.h>
16#include <linux/init.h>
17#include <linux/highuid.h>
18#include <linux/vfs.h>
19
20static void minix_read_inode(struct inode * inode);
21static int minix_write_inode(struct inode * inode, int wait);
22static int minix_statfs(struct super_block *sb, struct kstatfs *buf);
23static int minix_remount (struct super_block * sb, int * flags, char * data);
24
25static void minix_delete_inode(struct inode *inode)
26{
27 inode->i_size = 0;
28 minix_truncate(inode);
29 minix_free_inode(inode);
30}
31
32static void minix_put_super(struct super_block *sb)
33{
34 int i;
35 struct minix_sb_info *sbi = minix_sb(sb);
36
37 if (!(sb->s_flags & MS_RDONLY)) {
38 sbi->s_ms->s_state = sbi->s_mount_state;
39 mark_buffer_dirty(sbi->s_sbh);
40 }
41 for (i = 0; i < sbi->s_imap_blocks; i++)
42 brelse(sbi->s_imap[i]);
43 for (i = 0; i < sbi->s_zmap_blocks; i++)
44 brelse(sbi->s_zmap[i]);
45 brelse (sbi->s_sbh);
46 kfree(sbi->s_imap);
47 sb->s_fs_info = NULL;
48 kfree(sbi);
49
50 return;
51}
52
53static kmem_cache_t * minix_inode_cachep;
54
55static struct inode *minix_alloc_inode(struct super_block *sb)
56{
57 struct minix_inode_info *ei;
58 ei = (struct minix_inode_info *)kmem_cache_alloc(minix_inode_cachep, SLAB_KERNEL);
59 if (!ei)
60 return NULL;
61 return &ei->vfs_inode;
62}
63
64static void minix_destroy_inode(struct inode *inode)
65{
66 kmem_cache_free(minix_inode_cachep, minix_i(inode));
67}
68
69static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
70{
71 struct minix_inode_info *ei = (struct minix_inode_info *) foo;
72
73 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
74 SLAB_CTOR_CONSTRUCTOR)
75 inode_init_once(&ei->vfs_inode);
76}
77
78static int init_inodecache(void)
79{
80 minix_inode_cachep = kmem_cache_create("minix_inode_cache",
81 sizeof(struct minix_inode_info),
82 0, SLAB_RECLAIM_ACCOUNT,
83 init_once, NULL);
84 if (minix_inode_cachep == NULL)
85 return -ENOMEM;
86 return 0;
87}
88
89static void destroy_inodecache(void)
90{
91 if (kmem_cache_destroy(minix_inode_cachep))
92 printk(KERN_INFO "minix_inode_cache: not all structures were freed\n");
93}
94
95static struct super_operations minix_sops = {
96 .alloc_inode = minix_alloc_inode,
97 .destroy_inode = minix_destroy_inode,
98 .read_inode = minix_read_inode,
99 .write_inode = minix_write_inode,
100 .delete_inode = minix_delete_inode,
101 .put_super = minix_put_super,
102 .statfs = minix_statfs,
103 .remount_fs = minix_remount,
104};
105
106static int minix_remount (struct super_block * sb, int * flags, char * data)
107{
108 struct minix_sb_info * sbi = minix_sb(sb);
109 struct minix_super_block * ms;
110
111 ms = sbi->s_ms;
112 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
113 return 0;
114 if (*flags & MS_RDONLY) {
115 if (ms->s_state & MINIX_VALID_FS ||
116 !(sbi->s_mount_state & MINIX_VALID_FS))
117 return 0;
118 /* Mounting a rw partition read-only. */
119 ms->s_state = sbi->s_mount_state;
120 mark_buffer_dirty(sbi->s_sbh);
121 } else {
122 /* Mount a partition which is read-only, read-write. */
123 sbi->s_mount_state = ms->s_state;
124 ms->s_state &= ~MINIX_VALID_FS;
125 mark_buffer_dirty(sbi->s_sbh);
126
127 if (!(sbi->s_mount_state & MINIX_VALID_FS))
128 printk ("MINIX-fs warning: remounting unchecked fs, "
129 "running fsck is recommended.\n");
130 else if ((sbi->s_mount_state & MINIX_ERROR_FS))
131 printk ("MINIX-fs warning: remounting fs with errors, "
132 "running fsck is recommended.\n");
133 }
134 return 0;
135}
136
137static int minix_fill_super(struct super_block *s, void *data, int silent)
138{
139 struct buffer_head *bh;
140 struct buffer_head **map;
141 struct minix_super_block *ms;
142 int i, block;
143 struct inode *root_inode;
144 struct minix_sb_info *sbi;
145
146 sbi = kmalloc(sizeof(struct minix_sb_info), GFP_KERNEL);
147 if (!sbi)
148 return -ENOMEM;
149 s->s_fs_info = sbi;
150 memset(sbi, 0, sizeof(struct minix_sb_info));
151
152 /* N.B. These should be compile-time tests.
153 Unfortunately that is impossible. */
154 if (32 != sizeof (struct minix_inode))
155 panic("bad V1 i-node size");
156 if (64 != sizeof(struct minix2_inode))
157 panic("bad V2 i-node size");
158
159 if (!sb_set_blocksize(s, BLOCK_SIZE))
160 goto out_bad_hblock;
161
162 if (!(bh = sb_bread(s, 1)))
163 goto out_bad_sb;
164
165 ms = (struct minix_super_block *) bh->b_data;
166 sbi->s_ms = ms;
167 sbi->s_sbh = bh;
168 sbi->s_mount_state = ms->s_state;
169 sbi->s_ninodes = ms->s_ninodes;
170 sbi->s_nzones = ms->s_nzones;
171 sbi->s_imap_blocks = ms->s_imap_blocks;
172 sbi->s_zmap_blocks = ms->s_zmap_blocks;
173 sbi->s_firstdatazone = ms->s_firstdatazone;
174 sbi->s_log_zone_size = ms->s_log_zone_size;
175 sbi->s_max_size = ms->s_max_size;
176 s->s_magic = ms->s_magic;
177 if (s->s_magic == MINIX_SUPER_MAGIC) {
178 sbi->s_version = MINIX_V1;
179 sbi->s_dirsize = 16;
180 sbi->s_namelen = 14;
181 sbi->s_link_max = MINIX_LINK_MAX;
182 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
183 sbi->s_version = MINIX_V1;
184 sbi->s_dirsize = 32;
185 sbi->s_namelen = 30;
186 sbi->s_link_max = MINIX_LINK_MAX;
187 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
188 sbi->s_version = MINIX_V2;
189 sbi->s_nzones = ms->s_zones;
190 sbi->s_dirsize = 16;
191 sbi->s_namelen = 14;
192 sbi->s_link_max = MINIX2_LINK_MAX;
193 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
194 sbi->s_version = MINIX_V2;
195 sbi->s_nzones = ms->s_zones;
196 sbi->s_dirsize = 32;
197 sbi->s_namelen = 30;
198 sbi->s_link_max = MINIX2_LINK_MAX;
199 } else
200 goto out_no_fs;
201
202 /*
203 * Allocate the buffer map to keep the superblock small.
204 */
205 i = (sbi->s_imap_blocks + sbi->s_zmap_blocks) * sizeof(bh);
206 map = kmalloc(i, GFP_KERNEL);
207 if (!map)
208 goto out_no_map;
209 memset(map, 0, i);
210 sbi->s_imap = &map[0];
211 sbi->s_zmap = &map[sbi->s_imap_blocks];
212
213 block=2;
214 for (i=0 ; i < sbi->s_imap_blocks ; i++) {
215 if (!(sbi->s_imap[i]=sb_bread(s, block)))
216 goto out_no_bitmap;
217 block++;
218 }
219 for (i=0 ; i < sbi->s_zmap_blocks ; i++) {
220 if (!(sbi->s_zmap[i]=sb_bread(s, block)))
221 goto out_no_bitmap;
222 block++;
223 }
224
225 minix_set_bit(0,sbi->s_imap[0]->b_data);
226 minix_set_bit(0,sbi->s_zmap[0]->b_data);
227
228 /* set up enough so that it can read an inode */
229 s->s_op = &minix_sops;
230 root_inode = iget(s, MINIX_ROOT_INO);
231 if (!root_inode || is_bad_inode(root_inode))
232 goto out_no_root;
233
234 s->s_root = d_alloc_root(root_inode);
235 if (!s->s_root)
236 goto out_iput;
237
238 if (!NO_TRUNCATE)
239 s->s_root->d_op = &minix_dentry_operations;
240
241 if (!(s->s_flags & MS_RDONLY)) {
242 ms->s_state &= ~MINIX_VALID_FS;
243 mark_buffer_dirty(bh);
244 }
245 if (!(sbi->s_mount_state & MINIX_VALID_FS))
246 printk ("MINIX-fs: mounting unchecked file system, "
247 "running fsck is recommended.\n");
248 else if (sbi->s_mount_state & MINIX_ERROR_FS)
249 printk ("MINIX-fs: mounting file system with errors, "
250 "running fsck is recommended.\n");
251 return 0;
252
253out_iput:
254 iput(root_inode);
255 goto out_freemap;
256
257out_no_root:
258 if (!silent)
259 printk("MINIX-fs: get root inode failed\n");
260 goto out_freemap;
261
262out_no_bitmap:
263 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
264 out_freemap:
265 for (i = 0; i < sbi->s_imap_blocks; i++)
266 brelse(sbi->s_imap[i]);
267 for (i = 0; i < sbi->s_zmap_blocks; i++)
268 brelse(sbi->s_zmap[i]);
269 kfree(sbi->s_imap);
270 goto out_release;
271
272out_no_map:
273 if (!silent)
274 printk ("MINIX-fs: can't allocate map\n");
275 goto out_release;
276
277out_no_fs:
278 if (!silent)
279 printk("VFS: Can't find a Minix or Minix V2 filesystem on device "
280 "%s.\n", s->s_id);
281 out_release:
282 brelse(bh);
283 goto out;
284
285out_bad_hblock:
286 printk("MINIX-fs: blocksize too small for device.\n");
287 goto out;
288
289out_bad_sb:
290 printk("MINIX-fs: unable to read superblock\n");
291 out:
292 s->s_fs_info = NULL;
293 kfree(sbi);
294 return -EINVAL;
295}
296
297static int minix_statfs(struct super_block *sb, struct kstatfs *buf)
298{
299 struct minix_sb_info *sbi = minix_sb(sb);
300 buf->f_type = sb->s_magic;
301 buf->f_bsize = sb->s_blocksize;
302 buf->f_blocks = (sbi->s_nzones - sbi->s_firstdatazone) << sbi->s_log_zone_size;
303 buf->f_bfree = minix_count_free_blocks(sbi);
304 buf->f_bavail = buf->f_bfree;
305 buf->f_files = sbi->s_ninodes;
306 buf->f_ffree = minix_count_free_inodes(sbi);
307 buf->f_namelen = sbi->s_namelen;
308 return 0;
309}
310
311static int minix_get_block(struct inode *inode, sector_t block,
312 struct buffer_head *bh_result, int create)
313{
314 if (INODE_VERSION(inode) == MINIX_V1)
315 return V1_minix_get_block(inode, block, bh_result, create);
316 else
317 return V2_minix_get_block(inode, block, bh_result, create);
318}
319
320static int minix_writepage(struct page *page, struct writeback_control *wbc)
321{
322 return block_write_full_page(page, minix_get_block, wbc);
323}
324static int minix_readpage(struct file *file, struct page *page)
325{
326 return block_read_full_page(page,minix_get_block);
327}
328static int minix_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
329{
330 return block_prepare_write(page,from,to,minix_get_block);
331}
332static sector_t minix_bmap(struct address_space *mapping, sector_t block)
333{
334 return generic_block_bmap(mapping,block,minix_get_block);
335}
336static struct address_space_operations minix_aops = {
337 .readpage = minix_readpage,
338 .writepage = minix_writepage,
339 .sync_page = block_sync_page,
340 .prepare_write = minix_prepare_write,
341 .commit_write = generic_commit_write,
342 .bmap = minix_bmap
343};
344
345static struct inode_operations minix_symlink_inode_operations = {
346 .readlink = generic_readlink,
347 .follow_link = page_follow_link_light,
348 .put_link = page_put_link,
349 .getattr = minix_getattr,
350};
351
352void minix_set_inode(struct inode *inode, dev_t rdev)
353{
354 if (S_ISREG(inode->i_mode)) {
355 inode->i_op = &minix_file_inode_operations;
356 inode->i_fop = &minix_file_operations;
357 inode->i_mapping->a_ops = &minix_aops;
358 } else if (S_ISDIR(inode->i_mode)) {
359 inode->i_op = &minix_dir_inode_operations;
360 inode->i_fop = &minix_dir_operations;
361 inode->i_mapping->a_ops = &minix_aops;
362 } else if (S_ISLNK(inode->i_mode)) {
363 inode->i_op = &minix_symlink_inode_operations;
364 inode->i_mapping->a_ops = &minix_aops;
365 } else
366 init_special_inode(inode, inode->i_mode, rdev);
367}
368
369/*
370 * The minix V1 function to read an inode.
371 */
372static void V1_minix_read_inode(struct inode * inode)
373{
374 struct buffer_head * bh;
375 struct minix_inode * raw_inode;
376 struct minix_inode_info *minix_inode = minix_i(inode);
377 int i;
378
379 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
380 if (!raw_inode) {
381 make_bad_inode(inode);
382 return;
383 }
384 inode->i_mode = raw_inode->i_mode;
385 inode->i_uid = (uid_t)raw_inode->i_uid;
386 inode->i_gid = (gid_t)raw_inode->i_gid;
387 inode->i_nlink = raw_inode->i_nlinks;
388 inode->i_size = raw_inode->i_size;
389 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = raw_inode->i_time;
390 inode->i_mtime.tv_nsec = 0;
391 inode->i_atime.tv_nsec = 0;
392 inode->i_ctime.tv_nsec = 0;
393 inode->i_blocks = inode->i_blksize = 0;
394 for (i = 0; i < 9; i++)
395 minix_inode->u.i1_data[i] = raw_inode->i_zone[i];
396 minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
397 brelse(bh);
398}
399
400/*
401 * The minix V2 function to read an inode.
402 */
403static void V2_minix_read_inode(struct inode * inode)
404{
405 struct buffer_head * bh;
406 struct minix2_inode * raw_inode;
407 struct minix_inode_info *minix_inode = minix_i(inode);
408 int i;
409
410 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
411 if (!raw_inode) {
412 make_bad_inode(inode);
413 return;
414 }
415 inode->i_mode = raw_inode->i_mode;
416 inode->i_uid = (uid_t)raw_inode->i_uid;
417 inode->i_gid = (gid_t)raw_inode->i_gid;
418 inode->i_nlink = raw_inode->i_nlinks;
419 inode->i_size = raw_inode->i_size;
420 inode->i_mtime.tv_sec = raw_inode->i_mtime;
421 inode->i_atime.tv_sec = raw_inode->i_atime;
422 inode->i_ctime.tv_sec = raw_inode->i_ctime;
423 inode->i_mtime.tv_nsec = 0;
424 inode->i_atime.tv_nsec = 0;
425 inode->i_ctime.tv_nsec = 0;
426 inode->i_blocks = inode->i_blksize = 0;
427 for (i = 0; i < 10; i++)
428 minix_inode->u.i2_data[i] = raw_inode->i_zone[i];
429 minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
430 brelse(bh);
431}
432
433/*
434 * The global function to read an inode.
435 */
436static void minix_read_inode(struct inode * inode)
437{
438 if (INODE_VERSION(inode) == MINIX_V1)
439 V1_minix_read_inode(inode);
440 else
441 V2_minix_read_inode(inode);
442}
443
444/*
445 * The minix V1 function to synchronize an inode.
446 */
447static struct buffer_head * V1_minix_update_inode(struct inode * inode)
448{
449 struct buffer_head * bh;
450 struct minix_inode * raw_inode;
451 struct minix_inode_info *minix_inode = minix_i(inode);
452 int i;
453
454 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
455 if (!raw_inode)
456 return NULL;
457 raw_inode->i_mode = inode->i_mode;
458 raw_inode->i_uid = fs_high2lowuid(inode->i_uid);
459 raw_inode->i_gid = fs_high2lowgid(inode->i_gid);
460 raw_inode->i_nlinks = inode->i_nlink;
461 raw_inode->i_size = inode->i_size;
462 raw_inode->i_time = inode->i_mtime.tv_sec;
463 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
464 raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
465 else for (i = 0; i < 9; i++)
466 raw_inode->i_zone[i] = minix_inode->u.i1_data[i];
467 mark_buffer_dirty(bh);
468 return bh;
469}
470
471/*
472 * The minix V2 function to synchronize an inode.
473 */
474static struct buffer_head * V2_minix_update_inode(struct inode * inode)
475{
476 struct buffer_head * bh;
477 struct minix2_inode * raw_inode;
478 struct minix_inode_info *minix_inode = minix_i(inode);
479 int i;
480
481 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
482 if (!raw_inode)
483 return NULL;
484 raw_inode->i_mode = inode->i_mode;
485 raw_inode->i_uid = fs_high2lowuid(inode->i_uid);
486 raw_inode->i_gid = fs_high2lowgid(inode->i_gid);
487 raw_inode->i_nlinks = inode->i_nlink;
488 raw_inode->i_size = inode->i_size;
489 raw_inode->i_mtime = inode->i_mtime.tv_sec;
490 raw_inode->i_atime = inode->i_atime.tv_sec;
491 raw_inode->i_ctime = inode->i_ctime.tv_sec;
492 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
493 raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
494 else for (i = 0; i < 10; i++)
495 raw_inode->i_zone[i] = minix_inode->u.i2_data[i];
496 mark_buffer_dirty(bh);
497 return bh;
498}
499
500static struct buffer_head *minix_update_inode(struct inode *inode)
501{
502 if (INODE_VERSION(inode) == MINIX_V1)
503 return V1_minix_update_inode(inode);
504 else
505 return V2_minix_update_inode(inode);
506}
507
508static int minix_write_inode(struct inode * inode, int wait)
509{
510 brelse(minix_update_inode(inode));
511 return 0;
512}
513
514int minix_sync_inode(struct inode * inode)
515{
516 int err = 0;
517 struct buffer_head *bh;
518
519 bh = minix_update_inode(inode);
520 if (bh && buffer_dirty(bh))
521 {
522 sync_dirty_buffer(bh);
523 if (buffer_req(bh) && !buffer_uptodate(bh))
524 {
525 printk ("IO error syncing minix inode [%s:%08lx]\n",
526 inode->i_sb->s_id, inode->i_ino);
527 err = -1;
528 }
529 }
530 else if (!bh)
531 err = -1;
532 brelse (bh);
533 return err;
534}
535
536int minix_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
537{
538 generic_fillattr(dentry->d_inode, stat);
539 if (INODE_VERSION(dentry->d_inode) == MINIX_V1)
540 stat->blocks = (BLOCK_SIZE / 512) * V1_minix_blocks(stat->size);
541 else
542 stat->blocks = (BLOCK_SIZE / 512) * V2_minix_blocks(stat->size);
543 stat->blksize = BLOCK_SIZE;
544 return 0;
545}
546
547/*
548 * The function that is called for file truncation.
549 */
550void minix_truncate(struct inode * inode)
551{
552 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
553 return;
554 if (INODE_VERSION(inode) == MINIX_V1)
555 V1_minix_truncate(inode);
556 else
557 V2_minix_truncate(inode);
558}
559
560static struct super_block *minix_get_sb(struct file_system_type *fs_type,
561 int flags, const char *dev_name, void *data)
562{
563 return get_sb_bdev(fs_type, flags, dev_name, data, minix_fill_super);
564}
565
566static struct file_system_type minix_fs_type = {
567 .owner = THIS_MODULE,
568 .name = "minix",
569 .get_sb = minix_get_sb,
570 .kill_sb = kill_block_super,
571 .fs_flags = FS_REQUIRES_DEV,
572};
573
574static int __init init_minix_fs(void)
575{
576 int err = init_inodecache();
577 if (err)
578 goto out1;
579 err = register_filesystem(&minix_fs_type);
580 if (err)
581 goto out;
582 return 0;
583out:
584 destroy_inodecache();
585out1:
586 return err;
587}
588
589static void __exit exit_minix_fs(void)
590{
591 unregister_filesystem(&minix_fs_type);
592 destroy_inodecache();
593}
594
595module_init(init_minix_fs)
596module_exit(exit_minix_fs)
597MODULE_LICENSE("GPL");
598