patch-2.3.40 linux/fs/cramfs/inode.c

Next file: linux/fs/cramfs/uncompress.c
Previous file: linux/fs/cramfs/cramfs.h
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.39/linux/fs/cramfs/inode.c linux/fs/cramfs/inode.c
@@ -27,7 +27,10 @@
 static struct inode_operations cramfs_dir_inode_operations;
 static struct inode_operations cramfs_symlink_inode_operations;
 
+/* These two macros may change in future, to provide better st_ino
+   semantics. */
 #define CRAMINO(x)	((x)->offset?(x)->offset<<2:1)
+#define OFFSET(x)	((x)->i_ino)
 
 static struct inode *get_cramfs_inode(struct super_block *sb, struct cramfs_inode * cramfs_inode)
 {
@@ -41,6 +44,12 @@
 		inode->i_ino = CRAMINO(cramfs_inode);
 		inode->i_sb = sb;
 		inode->i_dev = sb->s_dev;
+		inode->i_nlink = 1; /* arguably wrong for directories,
+				       but it's the best we can do
+				       without reading the directory
+				       contents.  1 yields the right
+				       result in GNU find, even
+				       without -noleaf option. */
 		insert_inode_hash(inode);
 		if (S_ISREG(inode->i_mode))
 			inode->i_op = &cramfs_file_inode_operations;
@@ -62,45 +71,75 @@
  * up the accesses should be fairly regular and cached in the
  * page cache and dentry tree anyway..
  *
- * This also acts as a way to guarantee contiguous areas of
- * up to 2*PAGE_CACHE_SIZE, so that the caller doesn't need
- * to worry about end-of-buffer issues even when decompressing
- * a full page cache.
+ * This also acts as a way to guarantee contiguous areas of up to
+ * BLKS_PER_BUF*PAGE_CACHE_SIZE, so that the caller doesn't need to
+ * worry about end-of-buffer issues even when decompressing a full
+ * page cache.
  */
 #define READ_BUFFERS (2)
-static unsigned char read_buffers[READ_BUFFERS][PAGE_CACHE_SIZE*4];
-static int buffer_blocknr[READ_BUFFERS];
-static int last_buffer = 0;
+/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
+#define NEXT_BUFFER(_ix) ((_ix) ^ 1)
 
-static void *cramfs_read(struct super_block *sb, unsigned int offset)
+/*
+ * BLKS_PER_BUF_SHIFT must be at least 1 to allow for "compressed"
+ * data that takes up more space than the original.  1 is guaranteed
+ * to suffice, though.  Larger values provide more read-ahead and
+ * proportionally less wastage at the end of the buffer.
+ */
+#define BLKS_PER_BUF_SHIFT (2)
+#define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT)
+static unsigned char read_buffers[READ_BUFFERS][BLKS_PER_BUF][PAGE_CACHE_SIZE];
+static unsigned buffer_blocknr[READ_BUFFERS];
+static int next_buffer = 0;
+
+/*
+ * Returns a pointer to a buffer containing at least LEN bytes of
+ * filesystem starting at byte offset OFFSET into the filesystem.
+ */
+static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
 {
-	struct buffer_head * bh_array[4];
-	int i, blocknr, buffer;
+	struct buffer_head * bh_array[BLKS_PER_BUF];
+	unsigned i, blocknr, last_blocknr, buffer;
 
+	if (!len)
+		return NULL;
 	blocknr = offset >> PAGE_CACHE_SHIFT;
-	offset &= PAGE_CACHE_SIZE-1;
+	last_blocknr = (offset + len - 1) >> PAGE_CACHE_SHIFT;
+	if (last_blocknr - blocknr >= BLKS_PER_BUF)
+		goto eek; resume:
+	offset &= PAGE_CACHE_SIZE - 1;
 	for (i = 0; i < READ_BUFFERS; i++) {
-		if (blocknr == buffer_blocknr[i])
-			return read_buffers[i] + offset;
+		if ((blocknr >= buffer_blocknr[i]) &&
+		    (last_blocknr - buffer_blocknr[i] < BLKS_PER_BUF))
+			return &read_buffers[i][blocknr - buffer_blocknr[i]][offset];
 	}
 
-	/* Ok, read in four buffers completely first */
-	for (i = 0; i < 4; i++)
+	/* Ok, read in BLKS_PER_BUF pages completely first. */
+	for (i = 0; i < BLKS_PER_BUF; i++)
 		bh_array[i] = bread(sb->s_dev, blocknr + i, PAGE_CACHE_SIZE);
 
-	/* Ok, copy them to the staging area without sleeping.. */
-	buffer = last_buffer;
-	last_buffer = buffer ^ 1;
+	/* Ok, copy them to the staging area without sleeping. */
+	buffer = next_buffer;
+	next_buffer = NEXT_BUFFER(buffer);
 	buffer_blocknr[buffer] = blocknr;
-	for (i = 0; i < 4; i++) {
+	for (i = 0; i < BLKS_PER_BUF; i++) {
 		struct buffer_head * bh = bh_array[i];
 		if (bh) {
-			memcpy(read_buffers[buffer] + i*PAGE_CACHE_SIZE, bh->b_data, PAGE_CACHE_SIZE);
+			memcpy(read_buffers[buffer][i], bh->b_data, PAGE_CACHE_SIZE);
 			bforget(bh);
-		}
-		blocknr++;
+		} else
+			memset(read_buffers[buffer][i], 0, PAGE_CACHE_SIZE);
 	}
-	return read_buffers[buffer] + offset;
+	return read_buffers[buffer][0] + offset;
+
+ eek:
+	printk(KERN_ERR
+	       "cramfs (device %s): requested chunk (%u:+%u) bigger than buffer\n",
+	       bdevname(sb->s_dev), offset, len);
+	/* TODO: return EIO to process or kill the current process
+           instead of resuming. */
+	*((int *)0) = 0; /* XXX: doesn't work on all archs */
+	goto resume;
 }
 			
 
@@ -121,7 +160,7 @@
 		buffer_blocknr[i] = -1;
 
 	/* Read the first block and get the superblock from it */
-	memcpy(&super, cramfs_read(sb, 0), sizeof(super));
+	memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
 
 	/* Do sanity checks on the superblock */
 	if (super.magic != CRAMFS_MAGIC) {
@@ -132,21 +171,23 @@
 		printk("wrong signature\n");
 		goto out;
 	}
-
-	/* Check that the root inode is in a sane state */
-	root_offset = super.root.offset << 2;
-	if (root_offset < sizeof(struct cramfs_super)) {
-		printk("root offset too small\n");
-		goto out;
-	}
-	if (root_offset >= super.size) {
-		printk("root offset too large (%lu %u)\n", root_offset, super.size);
+	if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
+		printk("unsupported filesystem features\n");
 		goto out;
 	}
+
+	/* Check that the root inode is in a sane state */
 	if (!S_ISDIR(super.root.mode)) {
 		printk("root is not a directory\n");
 		goto out;
 	}
+	root_offset = super.root.offset << 2;
+	if (root_offset == 0)
+		printk(KERN_INFO "cramfs: note: empty filesystem");
+	else if (root_offset != sizeof(struct cramfs_super)) {
+		printk("bad root offset %lu\n", root_offset);
+		goto out;
+	}
 
 	/* Set it all up.. */
 	sb->s_op	= &cramfs_ops;
@@ -168,19 +209,20 @@
 {
 	struct statfs tmp;
 
-	memset(&tmp, 0, sizeof(tmp));
+	/* Unsupported fields set to -1 as per man page. */
+	memset(&tmp, 0xff, sizeof(tmp));
+
 	tmp.f_type = CRAMFS_MAGIC;
 	tmp.f_bsize = PAGE_CACHE_SIZE;
-	tmp.f_blocks = 0;
+	tmp.f_bfree = 0;
+	tmp.f_bavail = 0;
+	tmp.f_ffree = 0;
 	tmp.f_namelen = 255;
 	return copy_to_user(buf, &tmp, bufsize) ? -EFAULT : 0;
 }
 
 /*
- * Read a cramfs directory entry..
- *
- * Remember: the inode number is the byte offset of the start
- * of the directory..
+ * Read a cramfs directory entry.
  */
 static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
@@ -189,7 +231,7 @@
 	unsigned int offset;
 	int copied;
 
-	/* Offset within the thing.. */
+	/* Offset within the thing. */
 	offset = filp->f_pos;
 	if (offset >= inode->i_size)
 		return 0;
@@ -204,7 +246,7 @@
 		char *name;
 		int namelen, error;
 
-		de = cramfs_read(sb, offset + inode->i_ino);
+		de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+256);
 		name = (char *)(de+1);
 
 		/*
@@ -244,7 +286,7 @@
 		char *name;
 		int namelen;
 
-		de = cramfs_read(dir->i_sb, offset + dir->i_ino);
+		de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+256);
 		name = (char *)(de+1);
 		namelen = de->namelen << 2;
 		offset += sizeof(*de) + namelen;
@@ -274,27 +316,29 @@
 static int cramfs_readpage(struct dentry *dentry, struct page * page)
 {
 	struct inode *inode = dentry->d_inode;
-	unsigned long maxblock, bytes;
+	u32 maxblock, bytes_filled;
 
 	maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
-	bytes = 0;
+	bytes_filled = 0;
 	if (page->index < maxblock) {
 		struct super_block *sb = inode->i_sb;
-		unsigned long block_offset = inode->i_ino + page->index*4;
-		unsigned long start_offset = inode->i_ino + maxblock*4;
-		unsigned long end_offset;
+		u32 blkptr_offset = OFFSET(inode) + page->index*4;
+		u32 start_offset, compr_len;
 
-		end_offset = *(u32 *) cramfs_read(sb, block_offset);
+		start_offset = OFFSET(inode) + maxblock*4;
 		if (page->index)
-			start_offset = *(u32 *) cramfs_read(sb, block_offset-4);
-
-		bytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
-		if (page->index < maxblock)
-			bytes = PAGE_CACHE_SIZE;
-
-		cramfs_uncompress_block((void *) page_address(page), PAGE_CACHE_SIZE, cramfs_read(sb, start_offset), end_offset - start_offset);
+			start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, 4);
+		compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4)
+			     - start_offset);
+		if (compr_len == 0)
+			; /* hole */
+		else
+			bytes_filled = cramfs_uncompress_block((void *) page_address(page),
+				 PAGE_CACHE_SIZE,
+				 cramfs_read(sb, start_offset, compr_len),
+				 compr_len);
 	}
-	memset((void *) (page_address(page) + bytes), 0, PAGE_CACHE_SIZE - bytes);
+	memset((void *) (page_address(page) + bytes_filled), 0, PAGE_CACHE_SIZE - bytes_filled);
 	SetPageUptodate(page);
 	UnlockPage(page);
 	return 0;

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)