xref: /openbmc/linux/fs/omfs/inode.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2555e3775SBob Copeland /*
3555e3775SBob Copeland  * Optimized MPEG FS - inode and super operations.
4555e3775SBob Copeland  * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com>
5555e3775SBob Copeland  */
6555e3775SBob Copeland #include <linux/module.h>
7555e3775SBob Copeland #include <linux/sched.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
9555e3775SBob Copeland #include <linux/fs.h>
10555e3775SBob Copeland #include <linux/vfs.h>
115b825c3aSIngo Molnar #include <linux/cred.h>
12555e3775SBob Copeland #include <linux/parser.h>
13555e3775SBob Copeland #include <linux/buffer_head.h>
14555e3775SBob Copeland #include <linux/vmalloc.h>
15a9185b41SChristoph Hellwig #include <linux/writeback.h>
16d86efb0dSDavid Howells #include <linux/seq_file.h>
17555e3775SBob Copeland #include <linux/crc-itu-t.h>
18555e3775SBob Copeland #include "omfs.h"
19555e3775SBob Copeland 
20555e3775SBob Copeland MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
21555e3775SBob Copeland MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
22555e3775SBob Copeland MODULE_LICENSE("GPL");
23555e3775SBob Copeland 
omfs_bread(struct super_block * sb,sector_t block)24f068272cSBob Copeland struct buffer_head *omfs_bread(struct super_block *sb, sector_t block)
25f068272cSBob Copeland {
26f068272cSBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(sb);
27f068272cSBob Copeland 	if (block >= sbi->s_num_blocks)
28f068272cSBob Copeland 		return NULL;
29f068272cSBob Copeland 
30f068272cSBob Copeland 	return sb_bread(sb, clus_to_blk(sbi, block));
31f068272cSBob Copeland }
32f068272cSBob Copeland 
omfs_new_inode(struct inode * dir,umode_t mode)33587228beSAl Viro struct inode *omfs_new_inode(struct inode *dir, umode_t mode)
34555e3775SBob Copeland {
35555e3775SBob Copeland 	struct inode *inode;
36555e3775SBob Copeland 	u64 new_block;
37555e3775SBob Copeland 	int err;
38555e3775SBob Copeland 	int len;
39555e3775SBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(dir->i_sb);
40555e3775SBob Copeland 
41555e3775SBob Copeland 	inode = new_inode(dir->i_sb);
42555e3775SBob Copeland 	if (!inode)
43555e3775SBob Copeland 		return ERR_PTR(-ENOMEM);
44555e3775SBob Copeland 
45555e3775SBob Copeland 	err = omfs_allocate_range(dir->i_sb, sbi->s_mirrors, sbi->s_mirrors,
46555e3775SBob Copeland 			&new_block, &len);
47555e3775SBob Copeland 	if (err)
48555e3775SBob Copeland 		goto fail;
49555e3775SBob Copeland 
50555e3775SBob Copeland 	inode->i_ino = new_block;
51f2d40141SChristian Brauner 	inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
52555e3775SBob Copeland 	inode->i_mapping->a_ops = &omfs_aops;
53555e3775SBob Copeland 
54*906effbfSJeff Layton 	inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
55555e3775SBob Copeland 	switch (mode & S_IFMT) {
56555e3775SBob Copeland 	case S_IFDIR:
57555e3775SBob Copeland 		inode->i_op = &omfs_dir_inops;
58555e3775SBob Copeland 		inode->i_fop = &omfs_dir_operations;
59555e3775SBob Copeland 		inode->i_size = sbi->s_sys_blocksize;
60555e3775SBob Copeland 		inc_nlink(inode);
61555e3775SBob Copeland 		break;
62555e3775SBob Copeland 	case S_IFREG:
63555e3775SBob Copeland 		inode->i_op = &omfs_file_inops;
64555e3775SBob Copeland 		inode->i_fop = &omfs_file_operations;
65555e3775SBob Copeland 		inode->i_size = 0;
66555e3775SBob Copeland 		break;
67555e3775SBob Copeland 	}
68555e3775SBob Copeland 
69555e3775SBob Copeland 	insert_inode_hash(inode);
70555e3775SBob Copeland 	mark_inode_dirty(inode);
71555e3775SBob Copeland 	return inode;
72555e3775SBob Copeland fail:
73555e3775SBob Copeland 	make_bad_inode(inode);
74555e3775SBob Copeland 	iput(inode);
75555e3775SBob Copeland 	return ERR_PTR(err);
76555e3775SBob Copeland }
77555e3775SBob Copeland 
78555e3775SBob Copeland /*
79555e3775SBob Copeland  * Update the header checksums for a dirty inode based on its contents.
80555e3775SBob Copeland  * Caller is expected to hold the buffer head underlying oi and mark it
81555e3775SBob Copeland  * dirty.
82555e3775SBob Copeland  */
omfs_update_checksums(struct omfs_inode * oi)83555e3775SBob Copeland static void omfs_update_checksums(struct omfs_inode *oi)
84555e3775SBob Copeland {
85555e3775SBob Copeland 	int xor, i, ofs = 0, count;
86555e3775SBob Copeland 	u16 crc = 0;
87555e3775SBob Copeland 	unsigned char *ptr = (unsigned char *) oi;
88555e3775SBob Copeland 
89555e3775SBob Copeland 	count = be32_to_cpu(oi->i_head.h_body_size);
90555e3775SBob Copeland 	ofs = sizeof(struct omfs_header);
91555e3775SBob Copeland 
92555e3775SBob Copeland 	crc = crc_itu_t(crc, ptr + ofs, count);
93555e3775SBob Copeland 	oi->i_head.h_crc = cpu_to_be16(crc);
94555e3775SBob Copeland 
95555e3775SBob Copeland 	xor = ptr[0];
96555e3775SBob Copeland 	for (i = 1; i < OMFS_XOR_COUNT; i++)
97555e3775SBob Copeland 		xor ^= ptr[i];
98555e3775SBob Copeland 
99555e3775SBob Copeland 	oi->i_head.h_check_xor = xor;
100555e3775SBob Copeland }
101555e3775SBob Copeland 
__omfs_write_inode(struct inode * inode,int wait)102a9185b41SChristoph Hellwig static int __omfs_write_inode(struct inode *inode, int wait)
103555e3775SBob Copeland {
104555e3775SBob Copeland 	struct omfs_inode *oi;
105555e3775SBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
106555e3775SBob Copeland 	struct buffer_head *bh, *bh2;
107555e3775SBob Copeland 	u64 ctime;
108555e3775SBob Copeland 	int i;
109555e3775SBob Copeland 	int ret = -EIO;
110555e3775SBob Copeland 	int sync_failed = 0;
111555e3775SBob Copeland 
112555e3775SBob Copeland 	/* get current inode since we may have written sibling ptrs etc. */
113f068272cSBob Copeland 	bh = omfs_bread(inode->i_sb, inode->i_ino);
114555e3775SBob Copeland 	if (!bh)
115555e3775SBob Copeland 		goto out;
116555e3775SBob Copeland 
117555e3775SBob Copeland 	oi = (struct omfs_inode *) bh->b_data;
118555e3775SBob Copeland 
119555e3775SBob Copeland 	oi->i_head.h_self = cpu_to_be64(inode->i_ino);
120555e3775SBob Copeland 	if (S_ISDIR(inode->i_mode))
121555e3775SBob Copeland 		oi->i_type = OMFS_DIR;
122555e3775SBob Copeland 	else if (S_ISREG(inode->i_mode))
123555e3775SBob Copeland 		oi->i_type = OMFS_FILE;
124555e3775SBob Copeland 	else {
125555e3775SBob Copeland 		printk(KERN_WARNING "omfs: unknown file type: %d\n",
126555e3775SBob Copeland 			inode->i_mode);
127555e3775SBob Copeland 		goto out_brelse;
128555e3775SBob Copeland 	}
129555e3775SBob Copeland 
130555e3775SBob Copeland 	oi->i_head.h_body_size = cpu_to_be32(sbi->s_sys_blocksize -
131555e3775SBob Copeland 		sizeof(struct omfs_header));
132555e3775SBob Copeland 	oi->i_head.h_version = 1;
133555e3775SBob Copeland 	oi->i_head.h_type = OMFS_INODE_NORMAL;
134555e3775SBob Copeland 	oi->i_head.h_magic = OMFS_IMAGIC;
135555e3775SBob Copeland 	oi->i_size = cpu_to_be64(inode->i_size);
136555e3775SBob Copeland 
137*906effbfSJeff Layton 	ctime = inode_get_ctime(inode).tv_sec * 1000LL +
138*906effbfSJeff Layton 		((inode_get_ctime(inode).tv_nsec + 999)/1000);
139555e3775SBob Copeland 	oi->i_ctime = cpu_to_be64(ctime);
140555e3775SBob Copeland 
141555e3775SBob Copeland 	omfs_update_checksums(oi);
142555e3775SBob Copeland 
143555e3775SBob Copeland 	mark_buffer_dirty(bh);
144555e3775SBob Copeland 	if (wait) {
145555e3775SBob Copeland 		sync_dirty_buffer(bh);
146555e3775SBob Copeland 		if (buffer_req(bh) && !buffer_uptodate(bh))
147555e3775SBob Copeland 			sync_failed = 1;
148555e3775SBob Copeland 	}
149555e3775SBob Copeland 
150555e3775SBob Copeland 	/* if mirroring writes, copy to next fsblock */
151555e3775SBob Copeland 	for (i = 1; i < sbi->s_mirrors; i++) {
152f068272cSBob Copeland 		bh2 = omfs_bread(inode->i_sb, inode->i_ino + i);
153555e3775SBob Copeland 		if (!bh2)
154555e3775SBob Copeland 			goto out_brelse;
155555e3775SBob Copeland 
156555e3775SBob Copeland 		memcpy(bh2->b_data, bh->b_data, bh->b_size);
157555e3775SBob Copeland 		mark_buffer_dirty(bh2);
158555e3775SBob Copeland 		if (wait) {
159555e3775SBob Copeland 			sync_dirty_buffer(bh2);
160555e3775SBob Copeland 			if (buffer_req(bh2) && !buffer_uptodate(bh2))
161555e3775SBob Copeland 				sync_failed = 1;
162555e3775SBob Copeland 		}
163555e3775SBob Copeland 		brelse(bh2);
164555e3775SBob Copeland 	}
165555e3775SBob Copeland 	ret = (sync_failed) ? -EIO : 0;
166555e3775SBob Copeland out_brelse:
167555e3775SBob Copeland 	brelse(bh);
168555e3775SBob Copeland out:
169555e3775SBob Copeland 	return ret;
170555e3775SBob Copeland }
171555e3775SBob Copeland 
omfs_write_inode(struct inode * inode,struct writeback_control * wbc)172a9185b41SChristoph Hellwig static int omfs_write_inode(struct inode *inode, struct writeback_control *wbc)
173a9185b41SChristoph Hellwig {
174a9185b41SChristoph Hellwig 	return __omfs_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
175a9185b41SChristoph Hellwig }
176a9185b41SChristoph Hellwig 
omfs_sync_inode(struct inode * inode)177555e3775SBob Copeland int omfs_sync_inode(struct inode *inode)
178555e3775SBob Copeland {
179a9185b41SChristoph Hellwig 	return __omfs_write_inode(inode, 1);
180555e3775SBob Copeland }
181555e3775SBob Copeland 
182555e3775SBob Copeland /*
183555e3775SBob Copeland  * called when an entry is deleted, need to clear the bits in the
184555e3775SBob Copeland  * bitmaps.
185555e3775SBob Copeland  */
omfs_evict_inode(struct inode * inode)18669c9e750SAl Viro static void omfs_evict_inode(struct inode *inode)
187555e3775SBob Copeland {
18891b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
189dbd5768fSJan Kara 	clear_inode(inode);
19069c9e750SAl Viro 
19169c9e750SAl Viro 	if (inode->i_nlink)
19269c9e750SAl Viro 		return;
193555e3775SBob Copeland 
194555e3775SBob Copeland 	if (S_ISREG(inode->i_mode)) {
195555e3775SBob Copeland 		inode->i_size = 0;
196555e3775SBob Copeland 		omfs_shrink_inode(inode);
197555e3775SBob Copeland 	}
198555e3775SBob Copeland 
199555e3775SBob Copeland 	omfs_clear_range(inode->i_sb, inode->i_ino, 2);
200555e3775SBob Copeland }
201555e3775SBob Copeland 
omfs_iget(struct super_block * sb,ino_t ino)202555e3775SBob Copeland struct inode *omfs_iget(struct super_block *sb, ino_t ino)
203555e3775SBob Copeland {
204555e3775SBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(sb);
205555e3775SBob Copeland 	struct omfs_inode *oi;
206555e3775SBob Copeland 	struct buffer_head *bh;
207555e3775SBob Copeland 	u64 ctime;
208555e3775SBob Copeland 	unsigned long nsecs;
209555e3775SBob Copeland 	struct inode *inode;
210555e3775SBob Copeland 
211555e3775SBob Copeland 	inode = iget_locked(sb, ino);
212555e3775SBob Copeland 	if (!inode)
213555e3775SBob Copeland 		return ERR_PTR(-ENOMEM);
214555e3775SBob Copeland 	if (!(inode->i_state & I_NEW))
215555e3775SBob Copeland 		return inode;
216555e3775SBob Copeland 
217f068272cSBob Copeland 	bh = omfs_bread(inode->i_sb, ino);
218555e3775SBob Copeland 	if (!bh)
219555e3775SBob Copeland 		goto iget_failed;
220555e3775SBob Copeland 
221555e3775SBob Copeland 	oi = (struct omfs_inode *)bh->b_data;
222555e3775SBob Copeland 
223555e3775SBob Copeland 	/* check self */
224555e3775SBob Copeland 	if (ino != be64_to_cpu(oi->i_head.h_self))
225555e3775SBob Copeland 		goto fail_bh;
226555e3775SBob Copeland 
227555e3775SBob Copeland 	inode->i_uid = sbi->s_uid;
228555e3775SBob Copeland 	inode->i_gid = sbi->s_gid;
229555e3775SBob Copeland 
230555e3775SBob Copeland 	ctime = be64_to_cpu(oi->i_ctime);
231555e3775SBob Copeland 	nsecs = do_div(ctime, 1000) * 1000L;
232555e3775SBob Copeland 
233555e3775SBob Copeland 	inode->i_atime.tv_sec = ctime;
234555e3775SBob Copeland 	inode->i_mtime.tv_sec = ctime;
235*906effbfSJeff Layton 	inode_set_ctime(inode, ctime, nsecs);
236555e3775SBob Copeland 	inode->i_atime.tv_nsec = nsecs;
237555e3775SBob Copeland 	inode->i_mtime.tv_nsec = nsecs;
238555e3775SBob Copeland 
239555e3775SBob Copeland 	inode->i_mapping->a_ops = &omfs_aops;
240555e3775SBob Copeland 
241555e3775SBob Copeland 	switch (oi->i_type) {
242555e3775SBob Copeland 	case OMFS_DIR:
243555e3775SBob Copeland 		inode->i_mode = S_IFDIR | (S_IRWXUGO & ~sbi->s_dmask);
244555e3775SBob Copeland 		inode->i_op = &omfs_dir_inops;
245555e3775SBob Copeland 		inode->i_fop = &omfs_dir_operations;
246c963343aSBob Copeland 		inode->i_size = sbi->s_sys_blocksize;
247555e3775SBob Copeland 		inc_nlink(inode);
248555e3775SBob Copeland 		break;
249555e3775SBob Copeland 	case OMFS_FILE:
250555e3775SBob Copeland 		inode->i_mode = S_IFREG | (S_IRWXUGO & ~sbi->s_fmask);
251555e3775SBob Copeland 		inode->i_fop = &omfs_file_operations;
252555e3775SBob Copeland 		inode->i_size = be64_to_cpu(oi->i_size);
253555e3775SBob Copeland 		break;
254555e3775SBob Copeland 	}
255555e3775SBob Copeland 	brelse(bh);
256555e3775SBob Copeland 	unlock_new_inode(inode);
257555e3775SBob Copeland 	return inode;
258555e3775SBob Copeland fail_bh:
259555e3775SBob Copeland 	brelse(bh);
260555e3775SBob Copeland iget_failed:
261555e3775SBob Copeland 	iget_failed(inode);
262555e3775SBob Copeland 	return ERR_PTR(-EIO);
263555e3775SBob Copeland }
264555e3775SBob Copeland 
omfs_put_super(struct super_block * sb)265555e3775SBob Copeland static void omfs_put_super(struct super_block *sb)
266555e3775SBob Copeland {
267555e3775SBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(sb);
268555e3775SBob Copeland 	kfree(sbi->s_imap);
269555e3775SBob Copeland 	kfree(sbi);
270555e3775SBob Copeland 	sb->s_fs_info = NULL;
271555e3775SBob Copeland }
272555e3775SBob Copeland 
omfs_statfs(struct dentry * dentry,struct kstatfs * buf)273555e3775SBob Copeland static int omfs_statfs(struct dentry *dentry, struct kstatfs *buf)
274555e3775SBob Copeland {
275555e3775SBob Copeland 	struct super_block *s = dentry->d_sb;
276555e3775SBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(s);
277197e671eSColy Li 	u64 id = huge_encode_dev(s->s_bdev->bd_dev);
278197e671eSColy Li 
279555e3775SBob Copeland 	buf->f_type = OMFS_MAGIC;
280555e3775SBob Copeland 	buf->f_bsize = sbi->s_blocksize;
281555e3775SBob Copeland 	buf->f_blocks = sbi->s_num_blocks;
282555e3775SBob Copeland 	buf->f_files = sbi->s_num_blocks;
283555e3775SBob Copeland 	buf->f_namelen = OMFS_NAMELEN;
2846d1349c7SAl Viro 	buf->f_fsid = u64_to_fsid(id);
285555e3775SBob Copeland 
286555e3775SBob Copeland 	buf->f_bfree = buf->f_bavail = buf->f_ffree =
287555e3775SBob Copeland 		omfs_count_free(s);
288197e671eSColy Li 
289555e3775SBob Copeland 	return 0;
290555e3775SBob Copeland }
291555e3775SBob Copeland 
292d86efb0dSDavid Howells /*
293d86efb0dSDavid Howells  * Display the mount options in /proc/mounts.
294d86efb0dSDavid Howells  */
omfs_show_options(struct seq_file * m,struct dentry * root)295d86efb0dSDavid Howells static int omfs_show_options(struct seq_file *m, struct dentry *root)
296d86efb0dSDavid Howells {
297d86efb0dSDavid Howells 	struct omfs_sb_info *sbi = OMFS_SB(root->d_sb);
298d86efb0dSDavid Howells 	umode_t cur_umask = current_umask();
299d86efb0dSDavid Howells 
300d86efb0dSDavid Howells 	if (!uid_eq(sbi->s_uid, current_uid()))
301d86efb0dSDavid Howells 		seq_printf(m, ",uid=%u",
302d86efb0dSDavid Howells 			   from_kuid_munged(&init_user_ns, sbi->s_uid));
303d86efb0dSDavid Howells 	if (!gid_eq(sbi->s_gid, current_gid()))
304d86efb0dSDavid Howells 		seq_printf(m, ",gid=%u",
305d86efb0dSDavid Howells 			   from_kgid_munged(&init_user_ns, sbi->s_gid));
306d86efb0dSDavid Howells 
307d86efb0dSDavid Howells 	if (sbi->s_dmask == sbi->s_fmask) {
308d86efb0dSDavid Howells 		if (sbi->s_fmask != cur_umask)
309d86efb0dSDavid Howells 			seq_printf(m, ",umask=%o", sbi->s_fmask);
310d86efb0dSDavid Howells 	} else {
311d86efb0dSDavid Howells 		if (sbi->s_dmask != cur_umask)
312d86efb0dSDavid Howells 			seq_printf(m, ",dmask=%o", sbi->s_dmask);
313d86efb0dSDavid Howells 		if (sbi->s_fmask != cur_umask)
314d86efb0dSDavid Howells 			seq_printf(m, ",fmask=%o", sbi->s_fmask);
315d86efb0dSDavid Howells 	}
316d86efb0dSDavid Howells 
317d86efb0dSDavid Howells 	return 0;
318d86efb0dSDavid Howells }
319d86efb0dSDavid Howells 
320b87221deSAlexey Dobriyan static const struct super_operations omfs_sops = {
321555e3775SBob Copeland 	.write_inode	= omfs_write_inode,
32269c9e750SAl Viro 	.evict_inode	= omfs_evict_inode,
323555e3775SBob Copeland 	.put_super	= omfs_put_super,
324555e3775SBob Copeland 	.statfs		= omfs_statfs,
325d86efb0dSDavid Howells 	.show_options	= omfs_show_options,
326555e3775SBob Copeland };
327555e3775SBob Copeland 
328555e3775SBob Copeland /*
329555e3775SBob Copeland  * For Rio Karma, there is an on-disk free bitmap whose location is
330555e3775SBob Copeland  * stored in the root block.  For ReplayTV, there is no such free bitmap
331555e3775SBob Copeland  * so we have to walk the tree.  Both inodes and file data are allocated
332555e3775SBob Copeland  * from the same map.  This array can be big (300k) so we allocate
333555e3775SBob Copeland  * in units of the blocksize.
334555e3775SBob Copeland  */
omfs_get_imap(struct super_block * sb)335555e3775SBob Copeland static int omfs_get_imap(struct super_block *sb)
336555e3775SBob Copeland {
337c0345ee5SBob Copeland 	unsigned int bitmap_size, array_size;
338c0345ee5SBob Copeland 	int count;
339555e3775SBob Copeland 	struct omfs_sb_info *sbi = OMFS_SB(sb);
340555e3775SBob Copeland 	struct buffer_head *bh;
341555e3775SBob Copeland 	unsigned long **ptr;
342555e3775SBob Copeland 	sector_t block;
343555e3775SBob Copeland 
344555e3775SBob Copeland 	bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8);
345555e3775SBob Copeland 	array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize);
346555e3775SBob Copeland 
347555e3775SBob Copeland 	if (sbi->s_bitmap_ino == ~0ULL)
348555e3775SBob Copeland 		goto out;
349555e3775SBob Copeland 
350555e3775SBob Copeland 	sbi->s_imap_size = array_size;
351998d6688SFabian Frederick 	sbi->s_imap = kcalloc(array_size, sizeof(unsigned long *), GFP_KERNEL);
352555e3775SBob Copeland 	if (!sbi->s_imap)
353555e3775SBob Copeland 		goto nomem;
354555e3775SBob Copeland 
355555e3775SBob Copeland 	block = clus_to_blk(sbi, sbi->s_bitmap_ino);
356f068272cSBob Copeland 	if (block >= sbi->s_num_blocks)
357f068272cSBob Copeland 		goto nomem;
358f068272cSBob Copeland 
359555e3775SBob Copeland 	ptr = sbi->s_imap;
360555e3775SBob Copeland 	for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
361555e3775SBob Copeland 		bh = sb_bread(sb, block++);
362555e3775SBob Copeland 		if (!bh)
363555e3775SBob Copeland 			goto nomem_free;
364a7c9df04SAlex Dewar 		*ptr = kmemdup(bh->b_data, sb->s_blocksize, GFP_KERNEL);
365555e3775SBob Copeland 		if (!*ptr) {
366555e3775SBob Copeland 			brelse(bh);
367555e3775SBob Copeland 			goto nomem_free;
368555e3775SBob Copeland 		}
369555e3775SBob Copeland 		if (count < sb->s_blocksize)
370555e3775SBob Copeland 			memset((void *)*ptr + count, 0xff,
371555e3775SBob Copeland 				sb->s_blocksize - count);
372555e3775SBob Copeland 		brelse(bh);
373555e3775SBob Copeland 		ptr++;
374555e3775SBob Copeland 	}
375555e3775SBob Copeland out:
376555e3775SBob Copeland 	return 0;
377555e3775SBob Copeland 
378555e3775SBob Copeland nomem_free:
379555e3775SBob Copeland 	for (count = 0; count < array_size; count++)
380555e3775SBob Copeland 		kfree(sbi->s_imap[count]);
381555e3775SBob Copeland 
382555e3775SBob Copeland 	kfree(sbi->s_imap);
383555e3775SBob Copeland nomem:
384555e3775SBob Copeland 	sbi->s_imap = NULL;
385555e3775SBob Copeland 	sbi->s_imap_size = 0;
386555e3775SBob Copeland 	return -ENOMEM;
387555e3775SBob Copeland }
388555e3775SBob Copeland 
389555e3775SBob Copeland enum {
390dcbff39dSSasha Levin 	Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, Opt_err
391555e3775SBob Copeland };
392555e3775SBob Copeland 
393a447c093SSteven Whitehouse static const match_table_t tokens = {
394555e3775SBob Copeland 	{Opt_uid, "uid=%u"},
395555e3775SBob Copeland 	{Opt_gid, "gid=%u"},
396555e3775SBob Copeland 	{Opt_umask, "umask=%o"},
397555e3775SBob Copeland 	{Opt_dmask, "dmask=%o"},
398555e3775SBob Copeland 	{Opt_fmask, "fmask=%o"},
399dcbff39dSSasha Levin 	{Opt_err, NULL},
400555e3775SBob Copeland };
401555e3775SBob Copeland 
parse_options(char * options,struct omfs_sb_info * sbi)402555e3775SBob Copeland static int parse_options(char *options, struct omfs_sb_info *sbi)
403555e3775SBob Copeland {
404555e3775SBob Copeland 	char *p;
405555e3775SBob Copeland 	substring_t args[MAX_OPT_ARGS];
406555e3775SBob Copeland 	int option;
407555e3775SBob Copeland 
408555e3775SBob Copeland 	if (!options)
409555e3775SBob Copeland 		return 1;
410555e3775SBob Copeland 
411555e3775SBob Copeland 	while ((p = strsep(&options, ",")) != NULL) {
412555e3775SBob Copeland 		int token;
413555e3775SBob Copeland 		if (!*p)
414555e3775SBob Copeland 			continue;
415555e3775SBob Copeland 
416555e3775SBob Copeland 		token = match_token(p, tokens, args);
417555e3775SBob Copeland 		switch (token) {
418555e3775SBob Copeland 		case Opt_uid:
419555e3775SBob Copeland 			if (match_int(&args[0], &option))
420555e3775SBob Copeland 				return 0;
42180fcbe75SEric W. Biederman 			sbi->s_uid = make_kuid(current_user_ns(), option);
42280fcbe75SEric W. Biederman 			if (!uid_valid(sbi->s_uid))
42380fcbe75SEric W. Biederman 				return 0;
424555e3775SBob Copeland 			break;
425555e3775SBob Copeland 		case Opt_gid:
426555e3775SBob Copeland 			if (match_int(&args[0], &option))
427555e3775SBob Copeland 				return 0;
42880fcbe75SEric W. Biederman 			sbi->s_gid = make_kgid(current_user_ns(), option);
42980fcbe75SEric W. Biederman 			if (!gid_valid(sbi->s_gid))
43080fcbe75SEric W. Biederman 				return 0;
431555e3775SBob Copeland 			break;
432555e3775SBob Copeland 		case Opt_umask:
433555e3775SBob Copeland 			if (match_octal(&args[0], &option))
434555e3775SBob Copeland 				return 0;
435555e3775SBob Copeland 			sbi->s_fmask = sbi->s_dmask = option;
436555e3775SBob Copeland 			break;
437555e3775SBob Copeland 		case Opt_dmask:
438555e3775SBob Copeland 			if (match_octal(&args[0], &option))
439555e3775SBob Copeland 				return 0;
440555e3775SBob Copeland 			sbi->s_dmask = option;
441555e3775SBob Copeland 			break;
442555e3775SBob Copeland 		case Opt_fmask:
443555e3775SBob Copeland 			if (match_octal(&args[0], &option))
444555e3775SBob Copeland 				return 0;
445555e3775SBob Copeland 			sbi->s_fmask = option;
446555e3775SBob Copeland 			break;
447555e3775SBob Copeland 		default:
448555e3775SBob Copeland 			return 0;
449555e3775SBob Copeland 		}
450555e3775SBob Copeland 	}
451555e3775SBob Copeland 	return 1;
452555e3775SBob Copeland }
453555e3775SBob Copeland 
omfs_fill_super(struct super_block * sb,void * data,int silent)454555e3775SBob Copeland static int omfs_fill_super(struct super_block *sb, void *data, int silent)
455555e3775SBob Copeland {
456555e3775SBob Copeland 	struct buffer_head *bh, *bh2;
457555e3775SBob Copeland 	struct omfs_super_block *omfs_sb;
458555e3775SBob Copeland 	struct omfs_root_block *omfs_rb;
459555e3775SBob Copeland 	struct omfs_sb_info *sbi;
460555e3775SBob Copeland 	struct inode *root;
461555e3775SBob Copeland 	int ret = -EINVAL;
462555e3775SBob Copeland 
463555e3775SBob Copeland 	sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL);
464555e3775SBob Copeland 	if (!sbi)
465555e3775SBob Copeland 		return -ENOMEM;
466555e3775SBob Copeland 
467555e3775SBob Copeland 	sb->s_fs_info = sbi;
468555e3775SBob Copeland 
469c222d53eSDavid Howells 	sbi->s_uid = current_uid();
470c222d53eSDavid Howells 	sbi->s_gid = current_gid();
471ce3b0f8dSAl Viro 	sbi->s_dmask = sbi->s_fmask = current_umask();
472555e3775SBob Copeland 
473555e3775SBob Copeland 	if (!parse_options((char *) data, sbi))
474555e3775SBob Copeland 		goto end;
475555e3775SBob Copeland 
476555e3775SBob Copeland 	sb->s_maxbytes = 0xffffffff;
477555e3775SBob Copeland 
4788833293dSDeepa Dinamani 	sb->s_time_gran = NSEC_PER_MSEC;
4798833293dSDeepa Dinamani 	sb->s_time_min = 0;
4808833293dSDeepa Dinamani 	sb->s_time_max = U64_MAX / MSEC_PER_SEC;
4818833293dSDeepa Dinamani 
482555e3775SBob Copeland 	sb_set_blocksize(sb, 0x200);
483555e3775SBob Copeland 
484555e3775SBob Copeland 	bh = sb_bread(sb, 0);
485555e3775SBob Copeland 	if (!bh)
486555e3775SBob Copeland 		goto end;
487555e3775SBob Copeland 
488555e3775SBob Copeland 	omfs_sb = (struct omfs_super_block *)bh->b_data;
489555e3775SBob Copeland 
490555e3775SBob Copeland 	if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) {
491555e3775SBob Copeland 		if (!silent)
492555e3775SBob Copeland 			printk(KERN_ERR "omfs: Invalid superblock (%x)\n",
493555e3775SBob Copeland 				   omfs_sb->s_magic);
494555e3775SBob Copeland 		goto out_brelse_bh;
495555e3775SBob Copeland 	}
496555e3775SBob Copeland 	sb->s_magic = OMFS_MAGIC;
497555e3775SBob Copeland 
498555e3775SBob Copeland 	sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks);
499555e3775SBob Copeland 	sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize);
500555e3775SBob Copeland 	sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors);
501555e3775SBob Copeland 	sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block);
502555e3775SBob Copeland 	sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize);
503555e3775SBob Copeland 	mutex_init(&sbi->s_bitmap_lock);
504555e3775SBob Copeland 
50576e51210SFabian Frederick 	if (sbi->s_num_blocks > OMFS_MAX_BLOCKS) {
50676e51210SFabian Frederick 		printk(KERN_ERR "omfs: sysblock number (%llx) is out of range\n",
50776e51210SFabian Frederick 		       (unsigned long long)sbi->s_num_blocks);
50876e51210SFabian Frederick 		goto out_brelse_bh;
50976e51210SFabian Frederick 	}
51076e51210SFabian Frederick 
511555e3775SBob Copeland 	if (sbi->s_sys_blocksize > PAGE_SIZE) {
512555e3775SBob Copeland 		printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n",
513555e3775SBob Copeland 			sbi->s_sys_blocksize);
514555e3775SBob Copeland 		goto out_brelse_bh;
515555e3775SBob Copeland 	}
516555e3775SBob Copeland 
517555e3775SBob Copeland 	if (sbi->s_blocksize < sbi->s_sys_blocksize ||
518555e3775SBob Copeland 	    sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) {
519555e3775SBob Copeland 		printk(KERN_ERR "omfs: block size (%d) is out of range\n",
520555e3775SBob Copeland 			sbi->s_blocksize);
521555e3775SBob Copeland 		goto out_brelse_bh;
522555e3775SBob Copeland 	}
523555e3775SBob Copeland 
524555e3775SBob Copeland 	/*
525555e3775SBob Copeland 	 * Use sys_blocksize as the fs block since it is smaller than a
526555e3775SBob Copeland 	 * page while the fs blocksize can be larger.
527555e3775SBob Copeland 	 */
528555e3775SBob Copeland 	sb_set_blocksize(sb, sbi->s_sys_blocksize);
529555e3775SBob Copeland 
530555e3775SBob Copeland 	/*
531555e3775SBob Copeland 	 * ...and the difference goes into a shift.  sys_blocksize is always
532555e3775SBob Copeland 	 * a power of two factor of blocksize.
533555e3775SBob Copeland 	 */
534555e3775SBob Copeland 	sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
535555e3775SBob Copeland 		get_bitmask_order(sbi->s_sys_blocksize);
536555e3775SBob Copeland 
537f068272cSBob Copeland 	bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
538555e3775SBob Copeland 	if (!bh2)
539555e3775SBob Copeland 		goto out_brelse_bh;
540555e3775SBob Copeland 
541555e3775SBob Copeland 	omfs_rb = (struct omfs_root_block *)bh2->b_data;
542555e3775SBob Copeland 
543555e3775SBob Copeland 	sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap);
544555e3775SBob Copeland 	sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize);
545555e3775SBob Copeland 
546555e3775SBob Copeland 	if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) {
547555e3775SBob Copeland 		printk(KERN_ERR "omfs: block count discrepancy between "
548555e3775SBob Copeland 			"super and root blocks (%llx, %llx)\n",
549dc60bf1dSAlexander Beregalov 			(unsigned long long)sbi->s_num_blocks,
550dc60bf1dSAlexander Beregalov 			(unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks));
551555e3775SBob Copeland 		goto out_brelse_bh2;
552555e3775SBob Copeland 	}
553555e3775SBob Copeland 
5549442e54fSBob Copeland 	if (sbi->s_bitmap_ino != ~0ULL &&
5559442e54fSBob Copeland 	    sbi->s_bitmap_ino > sbi->s_num_blocks) {
5569442e54fSBob Copeland 		printk(KERN_ERR "omfs: free space bitmap location is corrupt "
5579442e54fSBob Copeland 			"(%llx, total blocks %llx)\n",
5589442e54fSBob Copeland 			(unsigned long long) sbi->s_bitmap_ino,
5599442e54fSBob Copeland 			(unsigned long long) sbi->s_num_blocks);
5609442e54fSBob Copeland 		goto out_brelse_bh2;
5619442e54fSBob Copeland 	}
5628800a044SBob Copeland 	if (sbi->s_clustersize < 1 ||
5638800a044SBob Copeland 	    sbi->s_clustersize > OMFS_MAX_CLUSTER_SIZE) {
5648800a044SBob Copeland 		printk(KERN_ERR "omfs: cluster size out of range (%d)",
5658800a044SBob Copeland 			sbi->s_clustersize);
5668800a044SBob Copeland 		goto out_brelse_bh2;
5678800a044SBob Copeland 	}
5689442e54fSBob Copeland 
569555e3775SBob Copeland 	ret = omfs_get_imap(sb);
570555e3775SBob Copeland 	if (ret)
571555e3775SBob Copeland 		goto out_brelse_bh2;
572555e3775SBob Copeland 
573555e3775SBob Copeland 	sb->s_op = &omfs_sops;
574555e3775SBob Copeland 
575555e3775SBob Copeland 	root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir));
576555e3775SBob Copeland 	if (IS_ERR(root)) {
577555e3775SBob Copeland 		ret = PTR_ERR(root);
578555e3775SBob Copeland 		goto out_brelse_bh2;
579555e3775SBob Copeland 	}
580555e3775SBob Copeland 
58148fde701SAl Viro 	sb->s_root = d_make_root(root);
5823a281f94SBob Copeland 	if (!sb->s_root) {
5833a281f94SBob Copeland 		ret = -ENOMEM;
584555e3775SBob Copeland 		goto out_brelse_bh2;
5853a281f94SBob Copeland 	}
586555e3775SBob Copeland 	printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name);
587555e3775SBob Copeland 
588555e3775SBob Copeland 	ret = 0;
589555e3775SBob Copeland out_brelse_bh2:
590555e3775SBob Copeland 	brelse(bh2);
591555e3775SBob Copeland out_brelse_bh:
592555e3775SBob Copeland 	brelse(bh);
593555e3775SBob Copeland end:
59470d9e384SDavidlohr Bueso 	if (ret)
59570d9e384SDavidlohr Bueso 		kfree(sbi);
596555e3775SBob Copeland 	return ret;
597555e3775SBob Copeland }
598555e3775SBob Copeland 
omfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)599152a0836SAl Viro static struct dentry *omfs_mount(struct file_system_type *fs_type,
600152a0836SAl Viro 			int flags, const char *dev_name, void *data)
601555e3775SBob Copeland {
602152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, omfs_fill_super);
603555e3775SBob Copeland }
604555e3775SBob Copeland 
605555e3775SBob Copeland static struct file_system_type omfs_fs_type = {
606555e3775SBob Copeland 	.owner = THIS_MODULE,
607555e3775SBob Copeland 	.name = "omfs",
608152a0836SAl Viro 	.mount = omfs_mount,
609555e3775SBob Copeland 	.kill_sb = kill_block_super,
610555e3775SBob Copeland 	.fs_flags = FS_REQUIRES_DEV,
611555e3775SBob Copeland };
6127f78e035SEric W. Biederman MODULE_ALIAS_FS("omfs");
613555e3775SBob Copeland 
init_omfs_fs(void)614555e3775SBob Copeland static int __init init_omfs_fs(void)
615555e3775SBob Copeland {
616555e3775SBob Copeland 	return register_filesystem(&omfs_fs_type);
617555e3775SBob Copeland }
618555e3775SBob Copeland 
exit_omfs_fs(void)619555e3775SBob Copeland static void __exit exit_omfs_fs(void)
620555e3775SBob Copeland {
621555e3775SBob Copeland 	unregister_filesystem(&omfs_fs_type);
622555e3775SBob Copeland }
623555e3775SBob Copeland 
624555e3775SBob Copeland module_init(init_omfs_fs);
625555e3775SBob Copeland module_exit(exit_omfs_fs);
626