1 /*
2  *  linux/fs/affs/inode.c
3  *
4  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
5  *
6  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
7  *
8  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
9  *
10  *  (C) 1991  Linus Torvalds - minix filesystem
11  */
12 
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/slab.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
19 #include <linux/affs_fs.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/locks.h>
24 #include <linux/genhd.h>
25 #include <linux/amigaffs.h>
26 #include <linux/major.h>
27 #include <linux/blkdev.h>
28 #include <linux/init.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 
32 extern int *blk_size[];
33 extern struct timezone sys_tz;
34 
35 static int affs_statfs(struct super_block *sb, struct statfs *buf);
36 static int affs_remount (struct super_block *sb, int *flags, char *data);
37 
38 static void
affs_put_super(struct super_block * sb)39 affs_put_super(struct super_block *sb)
40 {
41 	pr_debug("AFFS: put_super()\n");
42 
43 	if (!(sb->s_flags & MS_RDONLY)) {
44 		AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->bm_flag = be32_to_cpu(1);
45 		secs_to_datestamp(CURRENT_TIME,
46 				  &AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->disk_change);
47 		affs_fix_checksum(sb, AFFS_SB->s_root_bh);
48 		mark_buffer_dirty(AFFS_SB->s_root_bh);
49 	}
50 
51 	if (AFFS_SB->s_prefix)
52 		kfree(AFFS_SB->s_prefix);
53 	affs_free_bitmap(sb);
54 	affs_brelse(AFFS_SB->s_root_bh);
55 
56 	return;
57 }
58 
59 static void
affs_write_super(struct super_block * sb)60 affs_write_super(struct super_block *sb)
61 {
62 	int clean = 2;
63 
64 	if (!(sb->s_flags & MS_RDONLY)) {
65 		//	if (AFFS_SB->s_bitmap[i].bm_bh) {
66 		//		if (buffer_dirty(AFFS_SB->s_bitmap[i].bm_bh)) {
67 		//			clean = 0;
68 		AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->bm_flag = be32_to_cpu(clean);
69 		secs_to_datestamp(CURRENT_TIME,
70 				  &AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->disk_change);
71 		affs_fix_checksum(sb, AFFS_SB->s_root_bh);
72 		mark_buffer_dirty(AFFS_SB->s_root_bh);
73 		sb->s_dirt = !clean;	/* redo until bitmap synced */
74 	} else
75 		sb->s_dirt = 0;
76 
77 	pr_debug("AFFS: write_super() at %lu, clean=%d\n", CURRENT_TIME, clean);
78 }
79 
80 static struct super_operations affs_sops = {
81 	read_inode:	affs_read_inode,
82 	write_inode:	affs_write_inode,
83 	put_inode:	affs_put_inode,
84 	delete_inode:	affs_delete_inode,
85 	clear_inode:	affs_clear_inode,
86 	put_super:	affs_put_super,
87 	write_super:	affs_write_super,
88 	statfs:		affs_statfs,
89 	remount_fs:	affs_remount,
90 };
91 
92 static int
parse_options(char * options,uid_t * uid,gid_t * gid,int * mode,int * reserved,s32 * root,int * blocksize,char ** prefix,char * volume,unsigned long * mount_opts)93 parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
94 		int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
95 {
96 	char	*this_char, *value, *optn;
97 	int	 f;
98 
99 	/* Fill in defaults */
100 
101 	*uid        = current->uid;
102 	*gid        = current->gid;
103 	*reserved   = 2;
104 	*root       = -1;
105 	*blocksize  = -1;
106 	volume[0]   = ':';
107 	volume[1]   = 0;
108 	*mount_opts = 0;
109 	if (!options)
110 		return 1;
111 	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
112 		f = 0;
113 		if ((value = strchr(this_char,'=')) != NULL)
114 			*value++ = 0;
115 		if ((optn = "protect") && !strcmp(this_char, optn)) {
116 			if (value)
117 				goto out_inv_arg;
118 			*mount_opts |= SF_IMMUTABLE;
119 		} else if ((optn = "verbose") && !strcmp(this_char, optn)) {
120 			if (value)
121 				goto out_inv_arg;
122 			*mount_opts |= SF_VERBOSE;
123 		} else if ((optn = "mufs") && !strcmp(this_char, optn)) {
124 			if (value)
125 				goto out_inv_arg;
126 			*mount_opts |= SF_MUFS;
127 		} else if ((f = !strcmp(this_char,"setuid")) || !strcmp(this_char,"setgid")) {
128 			if (value) {
129 				if (!*value) {
130 					printk("AFFS: Argument for set[ug]id option missing\n");
131 					return 0;
132 				} else {
133 					*(f ? uid : gid) = simple_strtoul(value,&value,0);
134 					if (*value) {
135 						printk("AFFS: Bad set[ug]id argument\n");
136 						return 0;
137 					}
138 					*mount_opts |= f ? SF_SETUID : SF_SETGID;
139 				}
140 			}
141 		} else if (!strcmp(this_char,"prefix")) {
142 			optn = "prefix";
143 			if (!value || !*value)
144 				goto out_no_arg;
145 			if (*prefix) {		/* Free any previous prefix */
146 				kfree(*prefix);
147 				*prefix = NULL;
148 			}
149 			*prefix = kmalloc(strlen(value) + 1,GFP_KERNEL);
150 			if (!*prefix)
151 				return 0;
152 			strcpy(*prefix,value);
153 			*mount_opts |= SF_PREFIX;
154 		} else if (!strcmp(this_char,"volume")) {
155 			optn = "volume";
156 			if (!value || !*value)
157 				goto out_no_arg;
158 			if (strlen(value) > 30)
159 				value[30] = 0;
160 			strncpy(volume,value,30);
161 		} else if (!strcmp(this_char,"mode")) {
162 			optn = "mode";
163 			if (!value || !*value)
164 				goto out_no_arg;
165 			*mode = simple_strtoul(value,&value,8) & 0777;
166 			if (*value)
167 				return 0;
168 			*mount_opts |= SF_SETMODE;
169 		} else if (!strcmp(this_char,"reserved")) {
170 			optn = "reserved";
171 			if (!value || !*value)
172 				goto out_no_arg;
173 			*reserved = simple_strtoul(value,&value,0);
174 			if (*value)
175 				return 0;
176 		} else if (!strcmp(this_char,"root")) {
177 			optn = "root";
178 			if (!value || !*value)
179 				goto out_no_arg;
180 			*root = simple_strtoul(value,&value,0);
181 			if (*value)
182 				return 0;
183 		} else if (!strcmp(this_char,"bs")) {
184 			optn = "bs";
185 			if (!value || !*value)
186 				goto out_no_arg;
187 			*blocksize = simple_strtoul(value,&value,0);
188 			if (*value)
189 				return 0;
190 			if (*blocksize != 512 && *blocksize != 1024 && *blocksize != 2048
191 			    && *blocksize != 4096) {
192 				printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
193 				return 0;
194 			}
195 		} else if (!strcmp (this_char, "grpquota")
196 			 || !strcmp (this_char, "noquota")
197 			 || !strcmp (this_char, "quota")
198 			 || !strcmp (this_char, "usrquota"))
199 			 /* Silently ignore the quota options */
200 			;
201 		else {
202 			printk("AFFS: Unrecognized mount option %s\n", this_char);
203 			return 0;
204 		}
205 	}
206 	return 1;
207 
208 out_no_arg:
209 	printk("AFFS: The %s option requires an argument\n", optn);
210 	return 0;
211 out_inv_arg:
212 	printk("AFFS: Option %s does not take an argument\n", optn);
213 	return 0;
214 }
215 
216 /* This function definitely needs to be split up. Some fine day I'll
217  * hopefully have the guts to do so. Until then: sorry for the mess.
218  */
219 
220 static struct super_block *
affs_read_super(struct super_block * sb,void * data,int silent)221 affs_read_super(struct super_block *sb, void *data, int silent)
222 {
223 	struct buffer_head	*root_bh = NULL;
224 	struct buffer_head	*boot_bh;
225 	struct inode		*root_inode = NULL;
226 	kdev_t			 dev = sb->s_dev;
227 	s32			 root_block;
228 	int			 blocks, size, blocksize;
229 	u32			 chksum;
230 	int			 num_bm;
231 	int			 i, j;
232 	s32			 key;
233 	uid_t			 uid;
234 	gid_t			 gid;
235 	int			 reserved;
236 	unsigned long		 mount_flags;
237 	int			 tmp_flags;	/* fix remount prototype... */
238 
239 	pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
240 
241 	sb->s_magic             = AFFS_SUPER_MAGIC;
242 	sb->s_op                = &affs_sops;
243 	memset(AFFS_SB, 0, sizeof(*AFFS_SB));
244 	init_MUTEX(&AFFS_SB->s_bmlock);
245 
246 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
247 				&blocksize,&AFFS_SB->s_prefix,
248 				AFFS_SB->s_volume, &mount_flags)) {
249 		printk(KERN_ERR "AFFS: Error parsing options\n");
250 		return NULL;
251 	}
252 	/* N.B. after this point s_prefix must be released */
253 
254 	AFFS_SB->s_flags   = mount_flags;
255 	AFFS_SB->s_mode    = i;
256 	AFFS_SB->s_uid     = uid;
257 	AFFS_SB->s_gid     = gid;
258 	AFFS_SB->s_reserved= reserved;
259 
260 	/* Get the size of the device in 512-byte blocks.
261 	 * If we later see that the partition uses bigger
262 	 * blocks, we will have to change it.
263 	 */
264 
265 	blocks = blk_size[MAJOR(dev)] ? blk_size[MAJOR(dev)][MINOR(dev)] : 0;
266 	if (!blocks) {
267 		printk(KERN_ERR "AFFS: Could not determine device size\n");
268 		goto out_error;
269 	}
270 	size = (BLOCK_SIZE / 512) * blocks;
271 	pr_debug("AFFS: initial blksize=%d, blocks=%d\n", 512, blocks);
272 
273 	affs_set_blocksize(sb, PAGE_SIZE);
274 	/* Try to find root block. Its location depends on the block size. */
275 
276 	i = 512;
277 	j = 4096;
278 	if (blocksize > 0) {
279 		i = j = blocksize;
280 		size = size / (blocksize / 512);
281 	}
282 	for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
283 		AFFS_SB->s_root_block = root_block;
284 		if (root_block < 0)
285 			AFFS_SB->s_root_block = (reserved + size - 1) / 2;
286 		pr_debug("AFFS: setting blocksize to %d\n", blocksize);
287 		affs_set_blocksize(sb, blocksize);
288 		AFFS_SB->s_partition_size = size;
289 
290 		/* The root block location that was calculated above is not
291 		 * correct if the partition size is an odd number of 512-
292 		 * byte blocks, which will be rounded down to a number of
293 		 * 1024-byte blocks, and if there were an even number of
294 		 * reserved blocks. Ideally, all partition checkers should
295 		 * report the real number of blocks of the real blocksize,
296 		 * but since this just cannot be done, we have to try to
297 		 * find the root block anyways. In the above case, it is one
298 		 * block behind the calculated one. So we check this one, too.
299 		 */
300 		for (num_bm = 0; num_bm < 2; num_bm++) {
301 			pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
302 				"size=%d, reserved=%d\n",
303 				kdevname(dev),
304 				AFFS_SB->s_root_block + num_bm,
305 				blocksize, size, reserved);
306 			root_bh = affs_bread(sb, AFFS_SB->s_root_block + num_bm);
307 			if (!root_bh)
308 				continue;
309 			if (!affs_checksum_block(sb, root_bh) &&
310 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
311 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
312 				AFFS_SB->s_hashsize    = blocksize / 4 - 56;
313 				AFFS_SB->s_root_block += num_bm;
314 				key                        = 1;
315 				goto got_root;
316 			}
317 			affs_brelse(root_bh);
318 			root_bh = NULL;
319 		}
320 	}
321 	if (!silent)
322 		printk(KERN_ERR "AFFS: No valid root block on device %s\n",
323 			kdevname(dev));
324 	goto out_error;
325 
326 	/* N.B. after this point bh must be released */
327 got_root:
328 	root_block = AFFS_SB->s_root_block;
329 
330 	sb->s_blocksize_bits = blocksize == 512 ? 9 :
331 			       blocksize == 1024 ? 10 :
332 			       blocksize == 2048 ? 11 : 12;
333 
334 	/* Find out which kind of FS we have */
335 	boot_bh = sb_bread(sb, 0);
336 	if (!boot_bh) {
337 		printk(KERN_ERR "AFFS: Cannot read boot block\n");
338 		goto out_error;
339 	}
340 	chksum = be32_to_cpu(*(u32 *)boot_bh->b_data);
341 	brelse(boot_bh);
342 
343 	/* Dircache filesystems are compatible with non-dircache ones
344 	 * when reading. As long as they aren't supported, writing is
345 	 * not recommended.
346 	 */
347 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
348 	     || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
349 		printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
350 			kdevname(dev));
351 		sb->s_flags |= MS_RDONLY;
352 	}
353 	switch (chksum) {
354 		case MUFS_FS:
355 		case MUFS_INTLFFS:
356 		case MUFS_DCFFS:
357 			AFFS_SB->s_flags |= SF_MUFS;
358 			/* fall thru */
359 		case FS_INTLFFS:
360 		case FS_DCFFS:
361 			AFFS_SB->s_flags |= SF_INTL;
362 			break;
363 		case MUFS_FFS:
364 			AFFS_SB->s_flags |= SF_MUFS;
365 			break;
366 		case FS_FFS:
367 			break;
368 		case MUFS_OFS:
369 			AFFS_SB->s_flags |= SF_MUFS;
370 			/* fall thru */
371 		case FS_OFS:
372 			AFFS_SB->s_flags |= SF_OFS;
373 			sb->s_flags |= MS_NOEXEC;
374 			break;
375 		case MUFS_DCOFS:
376 		case MUFS_INTLOFS:
377 			AFFS_SB->s_flags |= SF_MUFS;
378 		case FS_DCOFS:
379 		case FS_INTLOFS:
380 			AFFS_SB->s_flags |= SF_INTL | SF_OFS;
381 			sb->s_flags |= MS_NOEXEC;
382 			break;
383 		default:
384 			printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
385 				kdevname(dev), chksum);
386 			goto out_error;
387 	}
388 
389 	if (mount_flags & SF_VERBOSE) {
390 		chksum = cpu_to_be32(chksum);
391 		printk(KERN_NOTICE "AFFS: Mounting volume \"%*s\": Type=%.3s\\%c, Blocksize=%d\n",
392 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0],
393 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
394 			(char *)&chksum,((char *)&chksum)[3] + '0',blocksize);
395 	}
396 
397 	sb->s_flags |= MS_NODEV | MS_NOSUID;
398 
399 	AFFS_SB->s_data_blksize = sb->s_blocksize;
400 	if (AFFS_SB->s_flags & SF_OFS)
401 		AFFS_SB->s_data_blksize -= 24;
402 
403 	/* Keep super block in cache */
404 	AFFS_SB->s_root_bh = root_bh;
405 	/* N.B. after this point s_root_bh must be released */
406 
407 	tmp_flags = sb->s_flags;
408 	if (affs_init_bitmap(sb, &tmp_flags))
409 		goto out_error;
410 	sb->s_flags = tmp_flags;
411 
412 	/* set up enough so that it can read an inode */
413 
414 	root_inode = iget(sb, root_block);
415 	sb->s_root = d_alloc_root(root_inode);
416 	if (!sb->s_root) {
417 		printk(KERN_ERR "AFFS: Get root inode failed\n");
418 		goto out_error;
419 	}
420 	sb->s_root->d_op = &affs_dentry_operations;
421 
422 	pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
423 	return sb;
424 
425 	/*
426 	 * Begin the cascaded cleanup ...
427 	 */
428 out_error:
429 	if (root_inode)
430 		iput(root_inode);
431 	if (AFFS_SB->s_bitmap)
432 		kfree(AFFS_SB->s_bitmap);
433 	affs_brelse(root_bh);
434 	if (AFFS_SB->s_prefix)
435 		kfree(AFFS_SB->s_prefix);
436 	return NULL;
437 }
438 
439 static int
affs_remount(struct super_block * sb,int * flags,char * data)440 affs_remount(struct super_block *sb, int *flags, char *data)
441 {
442 	int			 blocksize;
443 	uid_t			 uid;
444 	gid_t			 gid;
445 	int			 mode;
446 	int			 reserved;
447 	int			 root_block;
448 	unsigned long		 mount_flags;
449 	int			 res = 0;
450 
451 	pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
452 
453 	if (!parse_options(data,&uid,&gid,&mode,&reserved,&root_block,
454 	    &blocksize,&AFFS_SB->s_prefix,AFFS_SB->s_volume,&mount_flags))
455 		return -EINVAL;
456 	AFFS_SB->s_flags = mount_flags;
457 	AFFS_SB->s_mode  = mode;
458 	AFFS_SB->s_uid   = uid;
459 	AFFS_SB->s_gid   = gid;
460 
461 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
462 		return 0;
463 	if (*flags & MS_RDONLY) {
464 		sb->s_dirt = 1;
465 		while (sb->s_dirt)
466 			affs_write_super(sb);
467 		affs_free_bitmap(sb);
468 	} else
469 		res = affs_init_bitmap(sb, flags);
470 
471 	return res;
472 }
473 
474 static int
affs_statfs(struct super_block * sb,struct statfs * buf)475 affs_statfs(struct super_block *sb, struct statfs *buf)
476 {
477 	int		 free;
478 
479 	pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB->s_partition_size,
480 	     AFFS_SB->s_reserved);
481 
482 	free          = affs_count_free_blocks(sb);
483 	buf->f_type    = AFFS_SUPER_MAGIC;
484 	buf->f_bsize   = sb->s_blocksize;
485 	buf->f_blocks  = AFFS_SB->s_partition_size - AFFS_SB->s_reserved;
486 	buf->f_bfree   = free;
487 	buf->f_bavail  = free;
488 	return 0;
489 }
490 
491 static DECLARE_FSTYPE_DEV(affs_fs_type, "affs", affs_read_super);
492 
init_affs_fs(void)493 static int __init init_affs_fs(void)
494 {
495 	return register_filesystem(&affs_fs_type);
496 }
497 
exit_affs_fs(void)498 static void __exit exit_affs_fs(void)
499 {
500 	unregister_filesystem(&affs_fs_type);
501 }
502 
503 EXPORT_NO_SYMBOLS;
504 
505 MODULE_DESCRIPTION("Amiga filesystem support for Linux");
506 MODULE_LICENSE("GPL");
507 
508 module_init(init_affs_fs)
509 module_exit(exit_affs_fs)
510