1/*
2 * Optimized MPEG FS - inode and super operations.
3 * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com>
4 * Released under GPL v2.
5 */
6#include <linux/module.h>
7#include <linux/sched.h>
8#include <linux/slab.h>
9#include <linux/fs.h>
10#include <linux/vfs.h>
11#include <linux/parser.h>
12#include <linux/buffer_head.h>
13#include <linux/vmalloc.h>
14#include <linux/writeback.h>
15#include <linux/crc-itu-t.h>
16#include "omfs.h"
17
18MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
19MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
20MODULE_LICENSE("GPL");
21
22struct buffer_head *omfs_bread(struct super_block *sb, sector_t block)
23{
24	struct omfs_sb_info *sbi = OMFS_SB(sb);
25	if (block >= sbi->s_num_blocks)
26		return NULL;
27
28	return sb_bread(sb, clus_to_blk(sbi, block));
29}
30
31struct inode *omfs_new_inode(struct inode *dir, umode_t mode)
32{
33	struct inode *inode;
34	u64 new_block;
35	int err;
36	int len;
37	struct omfs_sb_info *sbi = OMFS_SB(dir->i_sb);
38
39	inode = new_inode(dir->i_sb);
40	if (!inode)
41		return ERR_PTR(-ENOMEM);
42
43	err = omfs_allocate_range(dir->i_sb, sbi->s_mirrors, sbi->s_mirrors,
44			&new_block, &len);
45	if (err)
46		goto fail;
47
48	inode->i_ino = new_block;
49	inode_init_owner(inode, NULL, mode);
50	inode->i_mapping->a_ops = &omfs_aops;
51
52	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
53	switch (mode & S_IFMT) {
54	case S_IFDIR:
55		inode->i_op = &omfs_dir_inops;
56		inode->i_fop = &omfs_dir_operations;
57		inode->i_size = sbi->s_sys_blocksize;
58		inc_nlink(inode);
59		break;
60	case S_IFREG:
61		inode->i_op = &omfs_file_inops;
62		inode->i_fop = &omfs_file_operations;
63		inode->i_size = 0;
64		break;
65	}
66
67	insert_inode_hash(inode);
68	mark_inode_dirty(inode);
69	return inode;
70fail:
71	make_bad_inode(inode);
72	iput(inode);
73	return ERR_PTR(err);
74}
75
76/*
77 * Update the header checksums for a dirty inode based on its contents.
78 * Caller is expected to hold the buffer head underlying oi and mark it
79 * dirty.
80 */
81static void omfs_update_checksums(struct omfs_inode *oi)
82{
83	int xor, i, ofs = 0, count;
84	u16 crc = 0;
85	unsigned char *ptr = (unsigned char *) oi;
86
87	count = be32_to_cpu(oi->i_head.h_body_size);
88	ofs = sizeof(struct omfs_header);
89
90	crc = crc_itu_t(crc, ptr + ofs, count);
91	oi->i_head.h_crc = cpu_to_be16(crc);
92
93	xor = ptr[0];
94	for (i = 1; i < OMFS_XOR_COUNT; i++)
95		xor ^= ptr[i];
96
97	oi->i_head.h_check_xor = xor;
98}
99
100static int __omfs_write_inode(struct inode *inode, int wait)
101{
102	struct omfs_inode *oi;
103	struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
104	struct buffer_head *bh, *bh2;
105	u64 ctime;
106	int i;
107	int ret = -EIO;
108	int sync_failed = 0;
109
110	/* get current inode since we may have written sibling ptrs etc. */
111	bh = omfs_bread(inode->i_sb, inode->i_ino);
112	if (!bh)
113		goto out;
114
115	oi = (struct omfs_inode *) bh->b_data;
116
117	oi->i_head.h_self = cpu_to_be64(inode->i_ino);
118	if (S_ISDIR(inode->i_mode))
119		oi->i_type = OMFS_DIR;
120	else if (S_ISREG(inode->i_mode))
121		oi->i_type = OMFS_FILE;
122	else {
123		printk(KERN_WARNING "omfs: unknown file type: %d\n",
124			inode->i_mode);
125		goto out_brelse;
126	}
127
128	oi->i_head.h_body_size = cpu_to_be32(sbi->s_sys_blocksize -
129		sizeof(struct omfs_header));
130	oi->i_head.h_version = 1;
131	oi->i_head.h_type = OMFS_INODE_NORMAL;
132	oi->i_head.h_magic = OMFS_IMAGIC;
133	oi->i_size = cpu_to_be64(inode->i_size);
134
135	ctime = inode->i_ctime.tv_sec * 1000LL +
136		((inode->i_ctime.tv_nsec + 999)/1000);
137	oi->i_ctime = cpu_to_be64(ctime);
138
139	omfs_update_checksums(oi);
140
141	mark_buffer_dirty(bh);
142	if (wait) {
143		sync_dirty_buffer(bh);
144		if (buffer_req(bh) && !buffer_uptodate(bh))
145			sync_failed = 1;
146	}
147
148	/* if mirroring writes, copy to next fsblock */
149	for (i = 1; i < sbi->s_mirrors; i++) {
150		bh2 = omfs_bread(inode->i_sb, inode->i_ino + i);
151		if (!bh2)
152			goto out_brelse;
153
154		memcpy(bh2->b_data, bh->b_data, bh->b_size);
155		mark_buffer_dirty(bh2);
156		if (wait) {
157			sync_dirty_buffer(bh2);
158			if (buffer_req(bh2) && !buffer_uptodate(bh2))
159				sync_failed = 1;
160		}
161		brelse(bh2);
162	}
163	ret = (sync_failed) ? -EIO : 0;
164out_brelse:
165	brelse(bh);
166out:
167	return ret;
168}
169
170static int omfs_write_inode(struct inode *inode, struct writeback_control *wbc)
171{
172	return __omfs_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
173}
174
175int omfs_sync_inode(struct inode *inode)
176{
177	return __omfs_write_inode(inode, 1);
178}
179
180/*
181 * called when an entry is deleted, need to clear the bits in the
182 * bitmaps.
183 */
184static void omfs_evict_inode(struct inode *inode)
185{
186	truncate_inode_pages_final(&inode->i_data);
187	clear_inode(inode);
188
189	if (inode->i_nlink)
190		return;
191
192	if (S_ISREG(inode->i_mode)) {
193		inode->i_size = 0;
194		omfs_shrink_inode(inode);
195	}
196
197	omfs_clear_range(inode->i_sb, inode->i_ino, 2);
198}
199
200struct inode *omfs_iget(struct super_block *sb, ino_t ino)
201{
202	struct omfs_sb_info *sbi = OMFS_SB(sb);
203	struct omfs_inode *oi;
204	struct buffer_head *bh;
205	u64 ctime;
206	unsigned long nsecs;
207	struct inode *inode;
208
209	inode = iget_locked(sb, ino);
210	if (!inode)
211		return ERR_PTR(-ENOMEM);
212	if (!(inode->i_state & I_NEW))
213		return inode;
214
215	bh = omfs_bread(inode->i_sb, ino);
216	if (!bh)
217		goto iget_failed;
218
219	oi = (struct omfs_inode *)bh->b_data;
220
221	/* check self */
222	if (ino != be64_to_cpu(oi->i_head.h_self))
223		goto fail_bh;
224
225	inode->i_uid = sbi->s_uid;
226	inode->i_gid = sbi->s_gid;
227
228	ctime = be64_to_cpu(oi->i_ctime);
229	nsecs = do_div(ctime, 1000) * 1000L;
230
231	inode->i_atime.tv_sec = ctime;
232	inode->i_mtime.tv_sec = ctime;
233	inode->i_ctime.tv_sec = ctime;
234	inode->i_atime.tv_nsec = nsecs;
235	inode->i_mtime.tv_nsec = nsecs;
236	inode->i_ctime.tv_nsec = nsecs;
237
238	inode->i_mapping->a_ops = &omfs_aops;
239
240	switch (oi->i_type) {
241	case OMFS_DIR:
242		inode->i_mode = S_IFDIR | (S_IRWXUGO & ~sbi->s_dmask);
243		inode->i_op = &omfs_dir_inops;
244		inode->i_fop = &omfs_dir_operations;
245		inode->i_size = sbi->s_sys_blocksize;
246		inc_nlink(inode);
247		break;
248	case OMFS_FILE:
249		inode->i_mode = S_IFREG | (S_IRWXUGO & ~sbi->s_fmask);
250		inode->i_fop = &omfs_file_operations;
251		inode->i_size = be64_to_cpu(oi->i_size);
252		break;
253	}
254	brelse(bh);
255	unlock_new_inode(inode);
256	return inode;
257fail_bh:
258	brelse(bh);
259iget_failed:
260	iget_failed(inode);
261	return ERR_PTR(-EIO);
262}
263
264static void omfs_put_super(struct super_block *sb)
265{
266	struct omfs_sb_info *sbi = OMFS_SB(sb);
267	kfree(sbi->s_imap);
268	kfree(sbi);
269	sb->s_fs_info = NULL;
270}
271
272static int omfs_statfs(struct dentry *dentry, struct kstatfs *buf)
273{
274	struct super_block *s = dentry->d_sb;
275	struct omfs_sb_info *sbi = OMFS_SB(s);
276	u64 id = huge_encode_dev(s->s_bdev->bd_dev);
277
278	buf->f_type = OMFS_MAGIC;
279	buf->f_bsize = sbi->s_blocksize;
280	buf->f_blocks = sbi->s_num_blocks;
281	buf->f_files = sbi->s_num_blocks;
282	buf->f_namelen = OMFS_NAMELEN;
283	buf->f_fsid.val[0] = (u32)id;
284	buf->f_fsid.val[1] = (u32)(id >> 32);
285
286	buf->f_bfree = buf->f_bavail = buf->f_ffree =
287		omfs_count_free(s);
288
289	return 0;
290}
291
292static const struct super_operations omfs_sops = {
293	.write_inode	= omfs_write_inode,
294	.evict_inode	= omfs_evict_inode,
295	.put_super	= omfs_put_super,
296	.statfs		= omfs_statfs,
297	.show_options	= generic_show_options,
298};
299
300/*
301 * For Rio Karma, there is an on-disk free bitmap whose location is
302 * stored in the root block.  For ReplayTV, there is no such free bitmap
303 * so we have to walk the tree.  Both inodes and file data are allocated
304 * from the same map.  This array can be big (300k) so we allocate
305 * in units of the blocksize.
306 */
307static int omfs_get_imap(struct super_block *sb)
308{
309	unsigned int bitmap_size, array_size;
310	int count;
311	struct omfs_sb_info *sbi = OMFS_SB(sb);
312	struct buffer_head *bh;
313	unsigned long **ptr;
314	sector_t block;
315
316	bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8);
317	array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize);
318
319	if (sbi->s_bitmap_ino == ~0ULL)
320		goto out;
321
322	sbi->s_imap_size = array_size;
323	sbi->s_imap = kcalloc(array_size, sizeof(unsigned long *), GFP_KERNEL);
324	if (!sbi->s_imap)
325		goto nomem;
326
327	block = clus_to_blk(sbi, sbi->s_bitmap_ino);
328	if (block >= sbi->s_num_blocks)
329		goto nomem;
330
331	ptr = sbi->s_imap;
332	for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
333		bh = sb_bread(sb, block++);
334		if (!bh)
335			goto nomem_free;
336		*ptr = kmalloc(sb->s_blocksize, GFP_KERNEL);
337		if (!*ptr) {
338			brelse(bh);
339			goto nomem_free;
340		}
341		memcpy(*ptr, bh->b_data, sb->s_blocksize);
342		if (count < sb->s_blocksize)
343			memset((void *)*ptr + count, 0xff,
344				sb->s_blocksize - count);
345		brelse(bh);
346		ptr++;
347	}
348out:
349	return 0;
350
351nomem_free:
352	for (count = 0; count < array_size; count++)
353		kfree(sbi->s_imap[count]);
354
355	kfree(sbi->s_imap);
356nomem:
357	sbi->s_imap = NULL;
358	sbi->s_imap_size = 0;
359	return -ENOMEM;
360}
361
362enum {
363	Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, Opt_err
364};
365
366static const match_table_t tokens = {
367	{Opt_uid, "uid=%u"},
368	{Opt_gid, "gid=%u"},
369	{Opt_umask, "umask=%o"},
370	{Opt_dmask, "dmask=%o"},
371	{Opt_fmask, "fmask=%o"},
372	{Opt_err, NULL},
373};
374
375static int parse_options(char *options, struct omfs_sb_info *sbi)
376{
377	char *p;
378	substring_t args[MAX_OPT_ARGS];
379	int option;
380
381	if (!options)
382		return 1;
383
384	while ((p = strsep(&options, ",")) != NULL) {
385		int token;
386		if (!*p)
387			continue;
388
389		token = match_token(p, tokens, args);
390		switch (token) {
391		case Opt_uid:
392			if (match_int(&args[0], &option))
393				return 0;
394			sbi->s_uid = make_kuid(current_user_ns(), option);
395			if (!uid_valid(sbi->s_uid))
396				return 0;
397			break;
398		case Opt_gid:
399			if (match_int(&args[0], &option))
400				return 0;
401			sbi->s_gid = make_kgid(current_user_ns(), option);
402			if (!gid_valid(sbi->s_gid))
403				return 0;
404			break;
405		case Opt_umask:
406			if (match_octal(&args[0], &option))
407				return 0;
408			sbi->s_fmask = sbi->s_dmask = option;
409			break;
410		case Opt_dmask:
411			if (match_octal(&args[0], &option))
412				return 0;
413			sbi->s_dmask = option;
414			break;
415		case Opt_fmask:
416			if (match_octal(&args[0], &option))
417				return 0;
418			sbi->s_fmask = option;
419			break;
420		default:
421			return 0;
422		}
423	}
424	return 1;
425}
426
427static int omfs_fill_super(struct super_block *sb, void *data, int silent)
428{
429	struct buffer_head *bh, *bh2;
430	struct omfs_super_block *omfs_sb;
431	struct omfs_root_block *omfs_rb;
432	struct omfs_sb_info *sbi;
433	struct inode *root;
434	int ret = -EINVAL;
435
436	save_mount_options(sb, (char *) data);
437
438	sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL);
439	if (!sbi)
440		return -ENOMEM;
441
442	sb->s_fs_info = sbi;
443
444	sbi->s_uid = current_uid();
445	sbi->s_gid = current_gid();
446	sbi->s_dmask = sbi->s_fmask = current_umask();
447
448	if (!parse_options((char *) data, sbi))
449		goto end;
450
451	sb->s_maxbytes = 0xffffffff;
452
453	sb_set_blocksize(sb, 0x200);
454
455	bh = sb_bread(sb, 0);
456	if (!bh)
457		goto end;
458
459	omfs_sb = (struct omfs_super_block *)bh->b_data;
460
461	if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) {
462		if (!silent)
463			printk(KERN_ERR "omfs: Invalid superblock (%x)\n",
464				   omfs_sb->s_magic);
465		goto out_brelse_bh;
466	}
467	sb->s_magic = OMFS_MAGIC;
468
469	sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks);
470	sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize);
471	sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors);
472	sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block);
473	sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize);
474	mutex_init(&sbi->s_bitmap_lock);
475
476	if (sbi->s_num_blocks > OMFS_MAX_BLOCKS) {
477		printk(KERN_ERR "omfs: sysblock number (%llx) is out of range\n",
478		       (unsigned long long)sbi->s_num_blocks);
479		goto out_brelse_bh;
480	}
481
482	if (sbi->s_sys_blocksize > PAGE_SIZE) {
483		printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n",
484			sbi->s_sys_blocksize);
485		goto out_brelse_bh;
486	}
487
488	if (sbi->s_blocksize < sbi->s_sys_blocksize ||
489	    sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) {
490		printk(KERN_ERR "omfs: block size (%d) is out of range\n",
491			sbi->s_blocksize);
492		goto out_brelse_bh;
493	}
494
495	/*
496	 * Use sys_blocksize as the fs block since it is smaller than a
497	 * page while the fs blocksize can be larger.
498	 */
499	sb_set_blocksize(sb, sbi->s_sys_blocksize);
500
501	/*
502	 * ...and the difference goes into a shift.  sys_blocksize is always
503	 * a power of two factor of blocksize.
504	 */
505	sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
506		get_bitmask_order(sbi->s_sys_blocksize);
507
508	bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
509	if (!bh2)
510		goto out_brelse_bh;
511
512	omfs_rb = (struct omfs_root_block *)bh2->b_data;
513
514	sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap);
515	sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize);
516
517	if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) {
518		printk(KERN_ERR "omfs: block count discrepancy between "
519			"super and root blocks (%llx, %llx)\n",
520			(unsigned long long)sbi->s_num_blocks,
521			(unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks));
522		goto out_brelse_bh2;
523	}
524
525	if (sbi->s_bitmap_ino != ~0ULL &&
526	    sbi->s_bitmap_ino > sbi->s_num_blocks) {
527		printk(KERN_ERR "omfs: free space bitmap location is corrupt "
528			"(%llx, total blocks %llx)\n",
529			(unsigned long long) sbi->s_bitmap_ino,
530			(unsigned long long) sbi->s_num_blocks);
531		goto out_brelse_bh2;
532	}
533	if (sbi->s_clustersize < 1 ||
534	    sbi->s_clustersize > OMFS_MAX_CLUSTER_SIZE) {
535		printk(KERN_ERR "omfs: cluster size out of range (%d)",
536			sbi->s_clustersize);
537		goto out_brelse_bh2;
538	}
539
540	ret = omfs_get_imap(sb);
541	if (ret)
542		goto out_brelse_bh2;
543
544	sb->s_op = &omfs_sops;
545
546	root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir));
547	if (IS_ERR(root)) {
548		ret = PTR_ERR(root);
549		goto out_brelse_bh2;
550	}
551
552	sb->s_root = d_make_root(root);
553	if (!sb->s_root) {
554		ret = -ENOMEM;
555		goto out_brelse_bh2;
556	}
557	printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name);
558
559	ret = 0;
560out_brelse_bh2:
561	brelse(bh2);
562out_brelse_bh:
563	brelse(bh);
564end:
565	if (ret)
566		kfree(sbi);
567	return ret;
568}
569
570static struct dentry *omfs_mount(struct file_system_type *fs_type,
571			int flags, const char *dev_name, void *data)
572{
573	return mount_bdev(fs_type, flags, dev_name, data, omfs_fill_super);
574}
575
576static struct file_system_type omfs_fs_type = {
577	.owner = THIS_MODULE,
578	.name = "omfs",
579	.mount = omfs_mount,
580	.kill_sb = kill_block_super,
581	.fs_flags = FS_REQUIRES_DEV,
582};
583MODULE_ALIAS_FS("omfs");
584
585static int __init init_omfs_fs(void)
586{
587	return register_filesystem(&omfs_fs_type);
588}
589
590static void __exit exit_omfs_fs(void)
591{
592	unregister_filesystem(&omfs_fs_type);
593}
594
595module_init(init_omfs_fs);
596module_exit(exit_omfs_fs);
597