1/*
2 * Compressed rom filesystem for Linux.
3 *
4 * Copyright (C) 1999 Linus Torvalds.
5 *
6 * This file is released under the GPL.
7 */
8
9/*
10 * These are the VFS interfaces to the compressed rom filesystem.
11 * The actual compression is based on zlib, see the other files.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/pagemap.h>
19#include <linux/init.h>
20#include <linux/string.h>
21#include <linux/blkdev.h>
22#include <linux/slab.h>
23#include <linux/vfs.h>
24#include <linux/mutex.h>
25#include <uapi/linux/cramfs_fs.h>
26#include <linux/uaccess.h>
27
28#include "internal.h"
29
30/*
31 * cramfs super-block data in memory
32 */
33struct cramfs_sb_info {
34	unsigned long magic;
35	unsigned long size;
36	unsigned long blocks;
37	unsigned long files;
38	unsigned long flags;
39};
40
41static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
42{
43	return sb->s_fs_info;
44}
45
46static const struct super_operations cramfs_ops;
47static const struct inode_operations cramfs_dir_inode_operations;
48static const struct file_operations cramfs_directory_operations;
49static const struct address_space_operations cramfs_aops;
50
51static DEFINE_MUTEX(read_mutex);
52
53
54/* These macros may change in future, to provide better st_ino semantics. */
55#define OFFSET(x)	((x)->i_ino)
56
57static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
58{
59	if (!cino->offset)
60		return offset + 1;
61	if (!cino->size)
62		return offset + 1;
63
64	/*
65	 * The file mode test fixes buggy mkcramfs implementations where
66	 * cramfs_inode->offset is set to a non zero value for entries
67	 * which did not contain data, like devices node and fifos.
68	 */
69	switch (cino->mode & S_IFMT) {
70	case S_IFREG:
71	case S_IFDIR:
72	case S_IFLNK:
73		return cino->offset << 2;
74	default:
75		break;
76	}
77	return offset + 1;
78}
79
80static struct inode *get_cramfs_inode(struct super_block *sb,
81	const struct cramfs_inode *cramfs_inode, unsigned int offset)
82{
83	struct inode *inode;
84	static struct timespec zerotime;
85
86	inode = iget_locked(sb, cramino(cramfs_inode, offset));
87	if (!inode)
88		return ERR_PTR(-ENOMEM);
89	if (!(inode->i_state & I_NEW))
90		return inode;
91
92	switch (cramfs_inode->mode & S_IFMT) {
93	case S_IFREG:
94		inode->i_fop = &generic_ro_fops;
95		inode->i_data.a_ops = &cramfs_aops;
96		break;
97	case S_IFDIR:
98		inode->i_op = &cramfs_dir_inode_operations;
99		inode->i_fop = &cramfs_directory_operations;
100		break;
101	case S_IFLNK:
102		inode->i_op = &page_symlink_inode_operations;
103		inode->i_data.a_ops = &cramfs_aops;
104		break;
105	default:
106		init_special_inode(inode, cramfs_inode->mode,
107				old_decode_dev(cramfs_inode->size));
108	}
109
110	inode->i_mode = cramfs_inode->mode;
111	i_uid_write(inode, cramfs_inode->uid);
112	i_gid_write(inode, cramfs_inode->gid);
113
114	/* if the lower 2 bits are zero, the inode contains data */
115	if (!(inode->i_ino & 3)) {
116		inode->i_size = cramfs_inode->size;
117		inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
118	}
119
120	/* Struct copy intentional */
121	inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
122	/* inode->i_nlink is left 1 - arguably wrong for directories,
123	   but it's the best we can do without reading the directory
124	   contents.  1 yields the right result in GNU find, even
125	   without -noleaf option. */
126
127	unlock_new_inode(inode);
128
129	return inode;
130}
131
132/*
133 * We have our own block cache: don't fill up the buffer cache
134 * with the rom-image, because the way the filesystem is set
135 * up the accesses should be fairly regular and cached in the
136 * page cache and dentry tree anyway..
137 *
138 * This also acts as a way to guarantee contiguous areas of up to
139 * BLKS_PER_BUF*PAGE_CACHE_SIZE, so that the caller doesn't need to
140 * worry about end-of-buffer issues even when decompressing a full
141 * page cache.
142 */
143#define READ_BUFFERS (2)
144/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
145#define NEXT_BUFFER(_ix) ((_ix) ^ 1)
146
147/*
148 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
149 * data that takes up more space than the original and with unlucky
150 * alignment.
151 */
152#define BLKS_PER_BUF_SHIFT	(2)
153#define BLKS_PER_BUF		(1 << BLKS_PER_BUF_SHIFT)
154#define BUFFER_SIZE		(BLKS_PER_BUF*PAGE_CACHE_SIZE)
155
156static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
157static unsigned buffer_blocknr[READ_BUFFERS];
158static struct super_block *buffer_dev[READ_BUFFERS];
159static int next_buffer;
160
161/*
162 * Returns a pointer to a buffer containing at least LEN bytes of
163 * filesystem starting at byte offset OFFSET into the filesystem.
164 */
165static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
166{
167	struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
168	struct page *pages[BLKS_PER_BUF];
169	unsigned i, blocknr, buffer;
170	unsigned long devsize;
171	char *data;
172
173	if (!len)
174		return NULL;
175	blocknr = offset >> PAGE_CACHE_SHIFT;
176	offset &= PAGE_CACHE_SIZE - 1;
177
178	/* Check if an existing buffer already has the data.. */
179	for (i = 0; i < READ_BUFFERS; i++) {
180		unsigned int blk_offset;
181
182		if (buffer_dev[i] != sb)
183			continue;
184		if (blocknr < buffer_blocknr[i])
185			continue;
186		blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_CACHE_SHIFT;
187		blk_offset += offset;
188		if (blk_offset + len > BUFFER_SIZE)
189			continue;
190		return read_buffers[i] + blk_offset;
191	}
192
193	devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
194
195	/* Ok, read in BLKS_PER_BUF pages completely first. */
196	for (i = 0; i < BLKS_PER_BUF; i++) {
197		struct page *page = NULL;
198
199		if (blocknr + i < devsize) {
200			page = read_mapping_page(mapping, blocknr + i, NULL);
201			/* synchronous error? */
202			if (IS_ERR(page))
203				page = NULL;
204		}
205		pages[i] = page;
206	}
207
208	for (i = 0; i < BLKS_PER_BUF; i++) {
209		struct page *page = pages[i];
210
211		if (page) {
212			wait_on_page_locked(page);
213			if (!PageUptodate(page)) {
214				/* asynchronous error */
215				page_cache_release(page);
216				pages[i] = NULL;
217			}
218		}
219	}
220
221	buffer = next_buffer;
222	next_buffer = NEXT_BUFFER(buffer);
223	buffer_blocknr[buffer] = blocknr;
224	buffer_dev[buffer] = sb;
225
226	data = read_buffers[buffer];
227	for (i = 0; i < BLKS_PER_BUF; i++) {
228		struct page *page = pages[i];
229
230		if (page) {
231			memcpy(data, kmap(page), PAGE_CACHE_SIZE);
232			kunmap(page);
233			page_cache_release(page);
234		} else
235			memset(data, 0, PAGE_CACHE_SIZE);
236		data += PAGE_CACHE_SIZE;
237	}
238	return read_buffers[buffer] + offset;
239}
240
241static void cramfs_kill_sb(struct super_block *sb)
242{
243	struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
244
245	kill_block_super(sb);
246	kfree(sbi);
247}
248
249static int cramfs_remount(struct super_block *sb, int *flags, char *data)
250{
251	sync_filesystem(sb);
252	*flags |= MS_RDONLY;
253	return 0;
254}
255
256static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
257{
258	int i;
259	struct cramfs_super super;
260	unsigned long root_offset;
261	struct cramfs_sb_info *sbi;
262	struct inode *root;
263
264	sb->s_flags |= MS_RDONLY;
265
266	sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
267	if (!sbi)
268		return -ENOMEM;
269	sb->s_fs_info = sbi;
270
271	/* Invalidate the read buffers on mount: think disk change.. */
272	mutex_lock(&read_mutex);
273	for (i = 0; i < READ_BUFFERS; i++)
274		buffer_blocknr[i] = -1;
275
276	/* Read the first block and get the superblock from it */
277	memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
278	mutex_unlock(&read_mutex);
279
280	/* Do sanity checks on the superblock */
281	if (super.magic != CRAMFS_MAGIC) {
282		/* check for wrong endianness */
283		if (super.magic == CRAMFS_MAGIC_WEND) {
284			if (!silent)
285				pr_err("wrong endianness\n");
286			return -EINVAL;
287		}
288
289		/* check at 512 byte offset */
290		mutex_lock(&read_mutex);
291		memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
292		mutex_unlock(&read_mutex);
293		if (super.magic != CRAMFS_MAGIC) {
294			if (super.magic == CRAMFS_MAGIC_WEND && !silent)
295				pr_err("wrong endianness\n");
296			else if (!silent)
297				pr_err("wrong magic\n");
298			return -EINVAL;
299		}
300	}
301
302	/* get feature flags first */
303	if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
304		pr_err("unsupported filesystem features\n");
305		return -EINVAL;
306	}
307
308	/* Check that the root inode is in a sane state */
309	if (!S_ISDIR(super.root.mode)) {
310		pr_err("root is not a directory\n");
311		return -EINVAL;
312	}
313	/* correct strange, hard-coded permissions of mkcramfs */
314	super.root.mode |= (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
315
316	root_offset = super.root.offset << 2;
317	if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
318		sbi->size = super.size;
319		sbi->blocks = super.fsid.blocks;
320		sbi->files = super.fsid.files;
321	} else {
322		sbi->size = 1<<28;
323		sbi->blocks = 0;
324		sbi->files = 0;
325	}
326	sbi->magic = super.magic;
327	sbi->flags = super.flags;
328	if (root_offset == 0)
329		pr_info("empty filesystem");
330	else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
331		 ((root_offset != sizeof(struct cramfs_super)) &&
332		  (root_offset != 512 + sizeof(struct cramfs_super))))
333	{
334		pr_err("bad root offset %lu\n", root_offset);
335		return -EINVAL;
336	}
337
338	/* Set it all up.. */
339	sb->s_op = &cramfs_ops;
340	root = get_cramfs_inode(sb, &super.root, 0);
341	if (IS_ERR(root))
342		return PTR_ERR(root);
343	sb->s_root = d_make_root(root);
344	if (!sb->s_root)
345		return -ENOMEM;
346	return 0;
347}
348
349static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
350{
351	struct super_block *sb = dentry->d_sb;
352	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
353
354	buf->f_type = CRAMFS_MAGIC;
355	buf->f_bsize = PAGE_CACHE_SIZE;
356	buf->f_blocks = CRAMFS_SB(sb)->blocks;
357	buf->f_bfree = 0;
358	buf->f_bavail = 0;
359	buf->f_files = CRAMFS_SB(sb)->files;
360	buf->f_ffree = 0;
361	buf->f_fsid.val[0] = (u32)id;
362	buf->f_fsid.val[1] = (u32)(id >> 32);
363	buf->f_namelen = CRAMFS_MAXPATHLEN;
364	return 0;
365}
366
367/*
368 * Read a cramfs directory entry.
369 */
370static int cramfs_readdir(struct file *file, struct dir_context *ctx)
371{
372	struct inode *inode = file_inode(file);
373	struct super_block *sb = inode->i_sb;
374	char *buf;
375	unsigned int offset;
376
377	/* Offset within the thing. */
378	if (ctx->pos >= inode->i_size)
379		return 0;
380	offset = ctx->pos;
381	/* Directory entries are always 4-byte aligned */
382	if (offset & 3)
383		return -EINVAL;
384
385	buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
386	if (!buf)
387		return -ENOMEM;
388
389	while (offset < inode->i_size) {
390		struct cramfs_inode *de;
391		unsigned long nextoffset;
392		char *name;
393		ino_t ino;
394		umode_t mode;
395		int namelen;
396
397		mutex_lock(&read_mutex);
398		de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
399		name = (char *)(de+1);
400
401		/*
402		 * Namelengths on disk are shifted by two
403		 * and the name padded out to 4-byte boundaries
404		 * with zeroes.
405		 */
406		namelen = de->namelen << 2;
407		memcpy(buf, name, namelen);
408		ino = cramino(de, OFFSET(inode) + offset);
409		mode = de->mode;
410		mutex_unlock(&read_mutex);
411		nextoffset = offset + sizeof(*de) + namelen;
412		for (;;) {
413			if (!namelen) {
414				kfree(buf);
415				return -EIO;
416			}
417			if (buf[namelen-1])
418				break;
419			namelen--;
420		}
421		if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
422			break;
423
424		ctx->pos = offset = nextoffset;
425	}
426	kfree(buf);
427	return 0;
428}
429
430/*
431 * Lookup and fill in the inode data..
432 */
433static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
434{
435	unsigned int offset = 0;
436	struct inode *inode = NULL;
437	int sorted;
438
439	mutex_lock(&read_mutex);
440	sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
441	while (offset < dir->i_size) {
442		struct cramfs_inode *de;
443		char *name;
444		int namelen, retval;
445		int dir_off = OFFSET(dir) + offset;
446
447		de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
448		name = (char *)(de+1);
449
450		/* Try to take advantage of sorted directories */
451		if (sorted && (dentry->d_name.name[0] < name[0]))
452			break;
453
454		namelen = de->namelen << 2;
455		offset += sizeof(*de) + namelen;
456
457		/* Quick check that the name is roughly the right length */
458		if (((dentry->d_name.len + 3) & ~3) != namelen)
459			continue;
460
461		for (;;) {
462			if (!namelen) {
463				inode = ERR_PTR(-EIO);
464				goto out;
465			}
466			if (name[namelen-1])
467				break;
468			namelen--;
469		}
470		if (namelen != dentry->d_name.len)
471			continue;
472		retval = memcmp(dentry->d_name.name, name, namelen);
473		if (retval > 0)
474			continue;
475		if (!retval) {
476			inode = get_cramfs_inode(dir->i_sb, de, dir_off);
477			break;
478		}
479		/* else (retval < 0) */
480		if (sorted)
481			break;
482	}
483out:
484	mutex_unlock(&read_mutex);
485	if (IS_ERR(inode))
486		return ERR_CAST(inode);
487	d_add(dentry, inode);
488	return NULL;
489}
490
491static int cramfs_readpage(struct file *file, struct page *page)
492{
493	struct inode *inode = page->mapping->host;
494	u32 maxblock;
495	int bytes_filled;
496	void *pgdata;
497
498	maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
499	bytes_filled = 0;
500	pgdata = kmap(page);
501
502	if (page->index < maxblock) {
503		struct super_block *sb = inode->i_sb;
504		u32 blkptr_offset = OFFSET(inode) + page->index*4;
505		u32 start_offset, compr_len;
506
507		start_offset = OFFSET(inode) + maxblock*4;
508		mutex_lock(&read_mutex);
509		if (page->index)
510			start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4,
511				4);
512		compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) -
513			start_offset);
514		mutex_unlock(&read_mutex);
515
516		if (compr_len == 0)
517			; /* hole */
518		else if (unlikely(compr_len > (PAGE_CACHE_SIZE << 1))) {
519			pr_err("bad compressed blocksize %u\n",
520				compr_len);
521			goto err;
522		} else {
523			mutex_lock(&read_mutex);
524			bytes_filled = cramfs_uncompress_block(pgdata,
525				 PAGE_CACHE_SIZE,
526				 cramfs_read(sb, start_offset, compr_len),
527				 compr_len);
528			mutex_unlock(&read_mutex);
529			if (unlikely(bytes_filled < 0))
530				goto err;
531		}
532	}
533
534	memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
535	flush_dcache_page(page);
536	kunmap(page);
537	SetPageUptodate(page);
538	unlock_page(page);
539	return 0;
540
541err:
542	kunmap(page);
543	ClearPageUptodate(page);
544	SetPageError(page);
545	unlock_page(page);
546	return 0;
547}
548
549static const struct address_space_operations cramfs_aops = {
550	.readpage = cramfs_readpage
551};
552
553/*
554 * Our operations:
555 */
556
557/*
558 * A directory can only readdir
559 */
560static const struct file_operations cramfs_directory_operations = {
561	.llseek		= generic_file_llseek,
562	.read		= generic_read_dir,
563	.iterate	= cramfs_readdir,
564};
565
566static const struct inode_operations cramfs_dir_inode_operations = {
567	.lookup		= cramfs_lookup,
568};
569
570static const struct super_operations cramfs_ops = {
571	.remount_fs	= cramfs_remount,
572	.statfs		= cramfs_statfs,
573};
574
575static struct dentry *cramfs_mount(struct file_system_type *fs_type,
576	int flags, const char *dev_name, void *data)
577{
578	return mount_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
579}
580
581static struct file_system_type cramfs_fs_type = {
582	.owner		= THIS_MODULE,
583	.name		= "cramfs",
584	.mount		= cramfs_mount,
585	.kill_sb	= cramfs_kill_sb,
586	.fs_flags	= FS_REQUIRES_DEV,
587};
588MODULE_ALIAS_FS("cramfs");
589
590static int __init init_cramfs_fs(void)
591{
592	int rv;
593
594	rv = cramfs_uncompress_init();
595	if (rv < 0)
596		return rv;
597	rv = register_filesystem(&cramfs_fs_type);
598	if (rv < 0)
599		cramfs_uncompress_exit();
600	return rv;
601}
602
603static void __exit exit_cramfs_fs(void)
604{
605	cramfs_uncompress_exit();
606	unregister_filesystem(&cramfs_fs_type);
607}
608
609module_init(init_cramfs_fs)
610module_exit(exit_cramfs_fs)
611MODULE_LICENSE("GPL");
612