1 /*
2  *  linux/fs/minix/inode.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Copyright (C) 1996  Gertjan van Wingerde
7  *	Minix V2 fs support.
8  *
9  *  Modified for 680x0 by Andreas Schwab
10  *  Updated to filesystem version 3 by Daniel Aragones
11  */
12 
13 #include <linux/module.h>
14 #include "minix.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>
21 
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);
26 
minix_evict_inode(struct inode * inode)27 static void minix_evict_inode(struct inode *inode)
28 {
29 	truncate_inode_pages_final(&inode->i_data);
30 	if (!inode->i_nlink) {
31 		inode->i_size = 0;
32 		minix_truncate(inode);
33 	}
34 	invalidate_inode_buffers(inode);
35 	clear_inode(inode);
36 	if (!inode->i_nlink)
37 		minix_free_inode(inode);
38 }
39 
minix_put_super(struct super_block * sb)40 static void minix_put_super(struct super_block *sb)
41 {
42 	int i;
43 	struct minix_sb_info *sbi = minix_sb(sb);
44 
45 	if (!sb_rdonly(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);
49 	}
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]);
54 	brelse (sbi->s_sbh);
55 	kfree(sbi->s_imap);
56 	sb->s_fs_info = NULL;
57 	kfree(sbi);
58 }
59 
60 static struct kmem_cache * minix_inode_cachep;
61 
minix_alloc_inode(struct super_block * sb)62 static struct inode *minix_alloc_inode(struct super_block *sb)
63 {
64 	struct minix_inode_info *ei;
65 	ei = kmem_cache_alloc(minix_inode_cachep, GFP_KERNEL);
66 	if (!ei)
67 		return NULL;
68 	return &ei->vfs_inode;
69 }
70 
minix_i_callback(struct rcu_head * head)71 static void minix_i_callback(struct rcu_head *head)
72 {
73 	struct inode *inode = container_of(head, struct inode, i_rcu);
74 	kmem_cache_free(minix_inode_cachep, minix_i(inode));
75 }
76 
minix_destroy_inode(struct inode * inode)77 static void minix_destroy_inode(struct inode *inode)
78 {
79 	call_rcu(&inode->i_rcu, minix_i_callback);
80 }
81 
init_once(void * foo)82 static void init_once(void *foo)
83 {
84 	struct minix_inode_info *ei = (struct minix_inode_info *) foo;
85 
86 	inode_init_once(&ei->vfs_inode);
87 }
88 
init_inodecache(void)89 static int __init init_inodecache(void)
90 {
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),
95 					     init_once);
96 	if (minix_inode_cachep == NULL)
97 		return -ENOMEM;
98 	return 0;
99 }
100 
destroy_inodecache(void)101 static void destroy_inodecache(void)
102 {
103 	/*
104 	 * Make sure all delayed rcu free inodes are flushed before we
105 	 * destroy cache.
106 	 */
107 	rcu_barrier();
108 	kmem_cache_destroy(minix_inode_cachep);
109 }
110 
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,
119 };
120 
minix_remount(struct super_block * sb,int * flags,char * data)121 static int minix_remount (struct super_block * sb, int * flags, char * data)
122 {
123 	struct minix_sb_info * sbi = minix_sb(sb);
124 	struct minix_super_block * ms;
125 
126 	sync_filesystem(sb);
127 	ms = sbi->s_ms;
128 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
129 		return 0;
130 	if (*flags & SB_RDONLY) {
131 		if (ms->s_state & MINIX_VALID_FS ||
132 		    !(sbi->s_mount_state & MINIX_VALID_FS))
133 			return 0;
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);
138 	} else {
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;
143 		} else {
144 			sbi->s_mount_state = MINIX_VALID_FS;
145 		}
146 		mark_buffer_dirty(sbi->s_sbh);
147 
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");
154 	}
155 	return 0;
156 }
157 
minix_check_superblock(struct super_block * sb)158 static bool minix_check_superblock(struct super_block *sb)
159 {
160 	struct minix_sb_info *sbi = minix_sb(sb);
161 
162 	if (sbi->s_imap_blocks == 0 || sbi->s_zmap_blocks == 0)
163 		return false;
164 
165 	/*
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.
169 	 */
170 	if (sbi->s_version == MINIX_V1 &&
171 	    sb->s_maxbytes > (7 + 512 + 512*512) * BLOCK_SIZE)
172 		return false;
173 
174 	return true;
175 }
176 
minix_fill_super(struct super_block * s,void * data,int silent)177 static int minix_fill_super(struct super_block *s, void *data, int silent)
178 {
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;
186 	int ret = -EINVAL;
187 
188 	sbi = kzalloc(sizeof(struct minix_sb_info), GFP_KERNEL);
189 	if (!sbi)
190 		return -ENOMEM;
191 	s->s_fs_info = sbi;
192 
193 	BUILD_BUG_ON(32 != sizeof (struct minix_inode));
194 	BUILD_BUG_ON(64 != sizeof(struct minix2_inode));
195 
196 	if (!sb_set_blocksize(s, BLOCK_SIZE))
197 		goto out_bad_hblock;
198 
199 	if (!(bh = sb_bread(s, 1)))
200 		goto out_bad_sb;
201 
202 	ms = (struct minix_super_block *) bh->b_data;
203 	sbi->s_ms = ms;
204 	sbi->s_sbh = bh;
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;
216 		sbi->s_dirsize = 16;
217 		sbi->s_namelen = 14;
218 		s->s_max_links = MINIX_LINK_MAX;
219 	} else if (s->s_magic == MINIX_SUPER_MAGIC2) {
220 		sbi->s_version = MINIX_V1;
221 		sbi->s_dirsize = 32;
222 		sbi->s_namelen = 30;
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;
227 		sbi->s_dirsize = 16;
228 		sbi->s_namelen = 14;
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;
233 		sbi->s_dirsize = 32;
234 		sbi->s_namelen = 30;
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;
246 		sbi->s_dirsize = 64;
247 		sbi->s_namelen = 60;
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;
252 	} else
253 		goto out_no_fs;
254 
255 	if (!minix_check_superblock(s))
256 		goto out_illegal_sb;
257 
258 	/*
259 	 * Allocate the buffer map to keep the superblock small.
260 	 */
261 	i = (sbi->s_imap_blocks + sbi->s_zmap_blocks) * sizeof(bh);
262 	map = kzalloc(i, GFP_KERNEL);
263 	if (!map)
264 		goto out_no_map;
265 	sbi->s_imap = &map[0];
266 	sbi->s_zmap = &map[sbi->s_imap_blocks];
267 
268 	block=2;
269 	for (i=0 ; i < sbi->s_imap_blocks ; i++) {
270 		if (!(sbi->s_imap[i]=sb_bread(s, block)))
271 			goto out_no_bitmap;
272 		block++;
273 	}
274 	for (i=0 ; i < sbi->s_zmap_blocks ; i++) {
275 		if (!(sbi->s_zmap[i]=sb_bread(s, block)))
276 			goto out_no_bitmap;
277 		block++;
278 	}
279 
280 	minix_set_bit(0,sbi->s_imap[0]->b_data);
281 	minix_set_bit(0,sbi->s_zmap[0]->b_data);
282 
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.
286 	 */
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");
291 		goto out_no_bitmap;
292 	}
293 
294 	block = minix_blocks_needed(
295 			(sbi->s_nzones - sbi->s_firstdatazone + 1),
296 			s->s_blocksize);
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");
300 		goto out_no_bitmap;
301 	}
302 
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);
308 		goto out_no_root;
309 	}
310 
311 	ret = -ENOMEM;
312 	s->s_root = d_make_root(root_inode);
313 	if (!s->s_root)
314 		goto out_no_root;
315 
316 	if (!sb_rdonly(s)) {
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);
320 	}
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");
327 
328 	return 0;
329 
330 out_no_root:
331 	if (!silent)
332 		printk("MINIX-fs: get root inode failed\n");
333 	goto out_freemap;
334 
335 out_no_bitmap:
336 	printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
337 out_freemap:
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]);
342 	kfree(sbi->s_imap);
343 	goto out_release;
344 
345 out_no_map:
346 	ret = -ENOMEM;
347 	if (!silent)
348 		printk("MINIX-fs: can't allocate map\n");
349 	goto out_release;
350 
351 out_illegal_sb:
352 	if (!silent)
353 		printk("MINIX-fs: bad superblock\n");
354 	goto out_release;
355 
356 out_no_fs:
357 	if (!silent)
358 		printk("VFS: Can't find a Minix filesystem V1 | V2 | V3 "
359 		       "on device %s.\n", s->s_id);
360 out_release:
361 	brelse(bh);
362 	goto out;
363 
364 out_bad_hblock:
365 	printk("MINIX-fs: blocksize too small for device\n");
366 	goto out;
367 
368 out_bad_sb:
369 	printk("MINIX-fs: unable to read superblock\n");
370 out:
371 	s->s_fs_info = NULL;
372 	kfree(sbi);
373 	return ret;
374 }
375 
minix_statfs(struct dentry * dentry,struct kstatfs * buf)376 static int minix_statfs(struct dentry *dentry, struct kstatfs *buf)
377 {
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);
391 
392 	return 0;
393 }
394 
minix_get_block(struct inode * inode,sector_t block,struct buffer_head * bh_result,int create)395 static int minix_get_block(struct inode *inode, sector_t block,
396 		    struct buffer_head *bh_result, int create)
397 {
398 	if (INODE_VERSION(inode) == MINIX_V1)
399 		return V1_minix_get_block(inode, block, bh_result, create);
400 	else
401 		return V2_minix_get_block(inode, block, bh_result, create);
402 }
403 
minix_writepage(struct page * page,struct writeback_control * wbc)404 static int minix_writepage(struct page *page, struct writeback_control *wbc)
405 {
406 	return block_write_full_page(page, minix_get_block, wbc);
407 }
408 
minix_readpage(struct file * file,struct page * page)409 static int minix_readpage(struct file *file, struct page *page)
410 {
411 	return block_read_full_page(page,minix_get_block);
412 }
413 
minix_prepare_chunk(struct page * page,loff_t pos,unsigned len)414 int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len)
415 {
416 	return __block_write_begin(page, pos, len, minix_get_block);
417 }
418 
minix_write_failed(struct address_space * mapping,loff_t to)419 static void minix_write_failed(struct address_space *mapping, loff_t to)
420 {
421 	struct inode *inode = mapping->host;
422 
423 	if (to > inode->i_size) {
424 		truncate_pagecache(inode, inode->i_size);
425 		minix_truncate(inode);
426 	}
427 }
428 
minix_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)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)
432 {
433 	int ret;
434 
435 	ret = block_write_begin(mapping, pos, len, flags, pagep,
436 				minix_get_block);
437 	if (unlikely(ret))
438 		minix_write_failed(mapping, pos + len);
439 
440 	return ret;
441 }
442 
minix_bmap(struct address_space * mapping,sector_t block)443 static sector_t minix_bmap(struct address_space *mapping, sector_t block)
444 {
445 	return generic_block_bmap(mapping,block,minix_get_block);
446 }
447 
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,
453 	.bmap = minix_bmap,
454 	.direct_IO = noop_direct_IO
455 };
456 
457 static const struct inode_operations minix_symlink_inode_operations = {
458 	.get_link	= page_get_link,
459 	.getattr	= minix_getattr,
460 };
461 
minix_set_inode(struct inode * inode,dev_t rdev)462 void minix_set_inode(struct inode *inode, dev_t rdev)
463 {
464 	if (S_ISREG(inode->i_mode)) {
465 		inode->i_op = &minix_file_inode_operations;
466 		inode->i_fop = &minix_file_operations;
467 		inode->i_mapping->a_ops = &minix_aops;
468 	} else if (S_ISDIR(inode->i_mode)) {
469 		inode->i_op = &minix_dir_inode_operations;
470 		inode->i_fop = &minix_dir_operations;
471 		inode->i_mapping->a_ops = &minix_aops;
472 	} else if (S_ISLNK(inode->i_mode)) {
473 		inode->i_op = &minix_symlink_inode_operations;
474 		inode_nohighmem(inode);
475 		inode->i_mapping->a_ops = &minix_aops;
476 	} else
477 		init_special_inode(inode, inode->i_mode, rdev);
478 }
479 
480 /*
481  * The minix V1 function to read an inode.
482  */
V1_minix_iget(struct inode * inode)483 static struct inode *V1_minix_iget(struct inode *inode)
484 {
485 	struct buffer_head * bh;
486 	struct minix_inode * raw_inode;
487 	struct minix_inode_info *minix_inode = minix_i(inode);
488 	int i;
489 
490 	raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
491 	if (!raw_inode) {
492 		iget_failed(inode);
493 		return ERR_PTR(-EIO);
494 	}
495 	if (raw_inode->i_nlinks == 0) {
496 		printk("MINIX-fs: deleted inode referenced: %lu\n",
497 		       inode->i_ino);
498 		brelse(bh);
499 		iget_failed(inode);
500 		return ERR_PTR(-ESTALE);
501 	}
502 	inode->i_mode = raw_inode->i_mode;
503 	i_uid_write(inode, raw_inode->i_uid);
504 	i_gid_write(inode, raw_inode->i_gid);
505 	set_nlink(inode, raw_inode->i_nlinks);
506 	inode->i_size = raw_inode->i_size;
507 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = raw_inode->i_time;
508 	inode->i_mtime.tv_nsec = 0;
509 	inode->i_atime.tv_nsec = 0;
510 	inode->i_ctime.tv_nsec = 0;
511 	inode->i_blocks = 0;
512 	for (i = 0; i < 9; i++)
513 		minix_inode->u.i1_data[i] = raw_inode->i_zone[i];
514 	minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
515 	brelse(bh);
516 	unlock_new_inode(inode);
517 	return inode;
518 }
519 
520 /*
521  * The minix V2 function to read an inode.
522  */
V2_minix_iget(struct inode * inode)523 static struct inode *V2_minix_iget(struct inode *inode)
524 {
525 	struct buffer_head * bh;
526 	struct minix2_inode * raw_inode;
527 	struct minix_inode_info *minix_inode = minix_i(inode);
528 	int i;
529 
530 	raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
531 	if (!raw_inode) {
532 		iget_failed(inode);
533 		return ERR_PTR(-EIO);
534 	}
535 	if (raw_inode->i_nlinks == 0) {
536 		printk("MINIX-fs: deleted inode referenced: %lu\n",
537 		       inode->i_ino);
538 		brelse(bh);
539 		iget_failed(inode);
540 		return ERR_PTR(-ESTALE);
541 	}
542 	inode->i_mode = raw_inode->i_mode;
543 	i_uid_write(inode, raw_inode->i_uid);
544 	i_gid_write(inode, raw_inode->i_gid);
545 	set_nlink(inode, raw_inode->i_nlinks);
546 	inode->i_size = raw_inode->i_size;
547 	inode->i_mtime.tv_sec = raw_inode->i_mtime;
548 	inode->i_atime.tv_sec = raw_inode->i_atime;
549 	inode->i_ctime.tv_sec = raw_inode->i_ctime;
550 	inode->i_mtime.tv_nsec = 0;
551 	inode->i_atime.tv_nsec = 0;
552 	inode->i_ctime.tv_nsec = 0;
553 	inode->i_blocks = 0;
554 	for (i = 0; i < 10; i++)
555 		minix_inode->u.i2_data[i] = raw_inode->i_zone[i];
556 	minix_set_inode(inode, old_decode_dev(raw_inode->i_zone[0]));
557 	brelse(bh);
558 	unlock_new_inode(inode);
559 	return inode;
560 }
561 
562 /*
563  * The global function to read an inode.
564  */
minix_iget(struct super_block * sb,unsigned long ino)565 struct inode *minix_iget(struct super_block *sb, unsigned long ino)
566 {
567 	struct inode *inode;
568 
569 	inode = iget_locked(sb, ino);
570 	if (!inode)
571 		return ERR_PTR(-ENOMEM);
572 	if (!(inode->i_state & I_NEW))
573 		return inode;
574 
575 	if (INODE_VERSION(inode) == MINIX_V1)
576 		return V1_minix_iget(inode);
577 	else
578 		return V2_minix_iget(inode);
579 }
580 
581 /*
582  * The minix V1 function to synchronize an inode.
583  */
V1_minix_update_inode(struct inode * inode)584 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
585 {
586 	struct buffer_head * bh;
587 	struct minix_inode * raw_inode;
588 	struct minix_inode_info *minix_inode = minix_i(inode);
589 	int i;
590 
591 	raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
592 	if (!raw_inode)
593 		return NULL;
594 	raw_inode->i_mode = inode->i_mode;
595 	raw_inode->i_uid = fs_high2lowuid(i_uid_read(inode));
596 	raw_inode->i_gid = fs_high2lowgid(i_gid_read(inode));
597 	raw_inode->i_nlinks = inode->i_nlink;
598 	raw_inode->i_size = inode->i_size;
599 	raw_inode->i_time = inode->i_mtime.tv_sec;
600 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
601 		raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
602 	else for (i = 0; i < 9; i++)
603 		raw_inode->i_zone[i] = minix_inode->u.i1_data[i];
604 	mark_buffer_dirty(bh);
605 	return bh;
606 }
607 
608 /*
609  * The minix V2 function to synchronize an inode.
610  */
V2_minix_update_inode(struct inode * inode)611 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
612 {
613 	struct buffer_head * bh;
614 	struct minix2_inode * raw_inode;
615 	struct minix_inode_info *minix_inode = minix_i(inode);
616 	int i;
617 
618 	raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
619 	if (!raw_inode)
620 		return NULL;
621 	raw_inode->i_mode = inode->i_mode;
622 	raw_inode->i_uid = fs_high2lowuid(i_uid_read(inode));
623 	raw_inode->i_gid = fs_high2lowgid(i_gid_read(inode));
624 	raw_inode->i_nlinks = inode->i_nlink;
625 	raw_inode->i_size = inode->i_size;
626 	raw_inode->i_mtime = inode->i_mtime.tv_sec;
627 	raw_inode->i_atime = inode->i_atime.tv_sec;
628 	raw_inode->i_ctime = inode->i_ctime.tv_sec;
629 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
630 		raw_inode->i_zone[0] = old_encode_dev(inode->i_rdev);
631 	else for (i = 0; i < 10; i++)
632 		raw_inode->i_zone[i] = minix_inode->u.i2_data[i];
633 	mark_buffer_dirty(bh);
634 	return bh;
635 }
636 
minix_write_inode(struct inode * inode,struct writeback_control * wbc)637 static int minix_write_inode(struct inode *inode, struct writeback_control *wbc)
638 {
639 	int err = 0;
640 	struct buffer_head *bh;
641 
642 	if (INODE_VERSION(inode) == MINIX_V1)
643 		bh = V1_minix_update_inode(inode);
644 	else
645 		bh = V2_minix_update_inode(inode);
646 	if (!bh)
647 		return -EIO;
648 	if (wbc->sync_mode == WB_SYNC_ALL && buffer_dirty(bh)) {
649 		sync_dirty_buffer(bh);
650 		if (buffer_req(bh) && !buffer_uptodate(bh)) {
651 			printk("IO error syncing minix inode [%s:%08lx]\n",
652 				inode->i_sb->s_id, inode->i_ino);
653 			err = -EIO;
654 		}
655 	}
656 	brelse (bh);
657 	return err;
658 }
659 
minix_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)660 int minix_getattr(const struct path *path, struct kstat *stat,
661 		  u32 request_mask, unsigned int flags)
662 {
663 	struct super_block *sb = path->dentry->d_sb;
664 	struct inode *inode = d_inode(path->dentry);
665 
666 	generic_fillattr(inode, stat);
667 	if (INODE_VERSION(inode) == MINIX_V1)
668 		stat->blocks = (BLOCK_SIZE / 512) * V1_minix_blocks(stat->size, sb);
669 	else
670 		stat->blocks = (sb->s_blocksize / 512) * V2_minix_blocks(stat->size, sb);
671 	stat->blksize = sb->s_blocksize;
672 	return 0;
673 }
674 
675 /*
676  * The function that is called for file truncation.
677  */
minix_truncate(struct inode * inode)678 void minix_truncate(struct inode * inode)
679 {
680 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
681 		return;
682 	if (INODE_VERSION(inode) == MINIX_V1)
683 		V1_minix_truncate(inode);
684 	else
685 		V2_minix_truncate(inode);
686 }
687 
minix_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)688 static struct dentry *minix_mount(struct file_system_type *fs_type,
689 	int flags, const char *dev_name, void *data)
690 {
691 	return mount_bdev(fs_type, flags, dev_name, data, minix_fill_super);
692 }
693 
694 static struct file_system_type minix_fs_type = {
695 	.owner		= THIS_MODULE,
696 	.name		= "minix",
697 	.mount		= minix_mount,
698 	.kill_sb	= kill_block_super,
699 	.fs_flags	= FS_REQUIRES_DEV,
700 };
701 MODULE_ALIAS_FS("minix");
702 
init_minix_fs(void)703 static int __init init_minix_fs(void)
704 {
705 	int err = init_inodecache();
706 	if (err)
707 		goto out1;
708 	err = register_filesystem(&minix_fs_type);
709 	if (err)
710 		goto out;
711 	return 0;
712 out:
713 	destroy_inodecache();
714 out1:
715 	return err;
716 }
717 
exit_minix_fs(void)718 static void __exit exit_minix_fs(void)
719 {
720         unregister_filesystem(&minix_fs_type);
721 	destroy_inodecache();
722 }
723 
724 module_init(init_minix_fs)
725 module_exit(exit_minix_fs)
726 MODULE_LICENSE("GPL");
727 
728