Blame view

fs/cramfs/inode.c 14.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * Compressed rom filesystem for Linux.
   *
   * Copyright (C) 1999 Linus Torvalds.
   *
   * This file is released under the GPL.
   */
  
  /*
   * These are the VFS interfaces to the compressed rom filesystem.
   * The actual compression is based on zlib, see the other files.
   */
  
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/pagemap.h>
  #include <linux/init.h>
  #include <linux/string.h>
  #include <linux/blkdev.h>
  #include <linux/cramfs_fs.h>
  #include <linux/slab.h>
  #include <linux/cramfs_fs_sb.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/vfs.h>
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
24
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
  
  #include <asm/uaccess.h>
ee9b6d61a   Josef 'Jeff' Sipek   [PATCH] Mark stru...
27
  static const struct super_operations cramfs_ops;
754661f14   Arjan van de Ven   [PATCH] mark stru...
28
  static const struct inode_operations cramfs_dir_inode_operations;
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
29
  static const struct file_operations cramfs_directory_operations;
f5e54d6e5   Christoph Hellwig   [PATCH] mark addr...
30
  static const struct address_space_operations cramfs_aops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
32
  static DEFINE_MUTEX(read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33

6f772fe65   Stefani Seibold   cramfs: generate ...
34
  /* These macros may change in future, to provide better st_ino semantics. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
  #define OFFSET(x)	((x)->i_ino)
0577d1ba4   Al Viro   cramfs: get_cramf...
36
  static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
a97c9bf33   Dave Johnson   [PATCH] fix cramf...
37
  {
6f772fe65   Stefani Seibold   cramfs: generate ...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  	if (!cino->offset)
  		return offset + 1;
  	if (!cino->size)
  		return offset + 1;
  
  	/*
  	 * The file mode test fixes buggy mkcramfs implementations where
  	 * cramfs_inode->offset is set to a non zero value for entries
  	 * which did not contain data, like devices node and fifos.
  	 */
  	switch (cino->mode & S_IFMT) {
  	case S_IFREG:
  	case S_IFDIR:
  	case S_IFLNK:
  		return cino->offset << 2;
  	default:
  		break;
  	}
  	return offset + 1;
  }
  
  static struct inode *get_cramfs_inode(struct super_block *sb,
0577d1ba4   Al Viro   cramfs: get_cramf...
60
  	const struct cramfs_inode *cramfs_inode, unsigned int offset)
6f772fe65   Stefani Seibold   cramfs: generate ...
61
62
  {
  	struct inode *inode;
77b8a75f5   Al Viro   simplify get_cram...
63
  	static struct timespec zerotime;
6f772fe65   Stefani Seibold   cramfs: generate ...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  
  	inode = iget_locked(sb, cramino(cramfs_inode, offset));
  	if (!inode)
  		return ERR_PTR(-ENOMEM);
  	if (!(inode->i_state & I_NEW))
  		return inode;
  
  	switch (cramfs_inode->mode & S_IFMT) {
  	case S_IFREG:
  		inode->i_fop = &generic_ro_fops;
  		inode->i_data.a_ops = &cramfs_aops;
  		break;
  	case S_IFDIR:
  		inode->i_op = &cramfs_dir_inode_operations;
  		inode->i_fop = &cramfs_directory_operations;
  		break;
  	case S_IFLNK:
  		inode->i_op = &page_symlink_inode_operations;
  		inode->i_data.a_ops = &cramfs_aops;
  		break;
  	default:
  		init_special_inode(inode, cramfs_inode->mode,
  				old_decode_dev(cramfs_inode->size));
  	}
77b8a75f5   Al Viro   simplify get_cram...
88
89
  	inode->i_mode = cramfs_inode->mode;
  	inode->i_uid = cramfs_inode->uid;
77b8a75f5   Al Viro   simplify get_cram...
90
  	inode->i_gid = cramfs_inode->gid;
6f772fe65   Stefani Seibold   cramfs: generate ...
91
92
93
94
95
96
  
  	/* if the lower 2 bits are zero, the inode contains data */
  	if (!(inode->i_ino & 3)) {
  		inode->i_size = cramfs_inode->size;
  		inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
  	}
77b8a75f5   Al Viro   simplify get_cram...
97
98
99
100
101
102
  	/* Struct copy intentional */
  	inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
  	/* inode->i_nlink is left 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. */
a97c9bf33   Dave Johnson   [PATCH] fix cramf...
103

6f772fe65   Stefani Seibold   cramfs: generate ...
104
  	unlock_new_inode(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  	return inode;
  }
  
  /*
   * We have our own block cache: don't fill up the buffer cache
   * with the rom-image, because the way the filesystem is set
   * 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
   * 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)
  /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
  #define NEXT_BUFFER(_ix) ((_ix) ^ 1)
  
  /*
   * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
   * data that takes up more space than the original and with unlucky
   * alignment.
   */
  #define BLKS_PER_BUF_SHIFT	(2)
  #define BLKS_PER_BUF		(1 << BLKS_PER_BUF_SHIFT)
  #define BUFFER_SIZE		(BLKS_PER_BUF*PAGE_CACHE_SIZE)
  
  static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
  static unsigned buffer_blocknr[READ_BUFFERS];
  static struct super_block * buffer_dev[READ_BUFFERS];
  static int next_buffer;
  
  /*
   * 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 address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
  	struct page *pages[BLKS_PER_BUF];
6bbfb0776   Andi Drebes   fs/cramfs/inode.c...
145
  	unsigned i, blocknr, buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  	unsigned long devsize;
  	char *data;
  
  	if (!len)
  		return NULL;
  	blocknr = offset >> PAGE_CACHE_SHIFT;
  	offset &= PAGE_CACHE_SIZE - 1;
  
  	/* Check if an existing buffer already has the data.. */
  	for (i = 0; i < READ_BUFFERS; i++) {
  		unsigned int blk_offset;
  
  		if (buffer_dev[i] != sb)
  			continue;
  		if (blocknr < buffer_blocknr[i])
  			continue;
  		blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_CACHE_SHIFT;
  		blk_offset += offset;
  		if (blk_offset + len > BUFFER_SIZE)
  			continue;
  		return read_buffers[i] + blk_offset;
  	}
  
  	devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
  
  	/* Ok, read in BLKS_PER_BUF pages completely first. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
174
175
  	for (i = 0; i < BLKS_PER_BUF; i++) {
  		struct page *page = NULL;
  
  		if (blocknr + i < devsize) {
6fe6900e1   Nick Piggin   mm: make read_cac...
176
177
  			page = read_mapping_page_async(mapping, blocknr + i,
  									NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  			/* synchronous error? */
  			if (IS_ERR(page))
  				page = NULL;
  		}
  		pages[i] = page;
  	}
  
  	for (i = 0; i < BLKS_PER_BUF; i++) {
  		struct page *page = pages[i];
  		if (page) {
  			wait_on_page_locked(page);
  			if (!PageUptodate(page)) {
  				/* asynchronous error */
  				page_cache_release(page);
  				pages[i] = NULL;
  			}
  		}
  	}
  
  	buffer = next_buffer;
  	next_buffer = NEXT_BUFFER(buffer);
  	buffer_blocknr[buffer] = blocknr;
  	buffer_dev[buffer] = sb;
  
  	data = read_buffers[buffer];
  	for (i = 0; i < BLKS_PER_BUF; i++) {
  		struct page *page = pages[i];
  		if (page) {
  			memcpy(data, kmap(page), PAGE_CACHE_SIZE);
  			kunmap(page);
  			page_cache_release(page);
  		} else
  			memset(data, 0, PAGE_CACHE_SIZE);
  		data += PAGE_CACHE_SIZE;
  	}
  	return read_buffers[buffer] + offset;
  }
  
  static void cramfs_put_super(struct super_block *sb)
  {
  	kfree(sb->s_fs_info);
  	sb->s_fs_info = NULL;
  }
  
  static int cramfs_remount(struct super_block *sb, int *flags, char *data)
  {
  	*flags |= MS_RDONLY;
  	return 0;
  }
  
  static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
  {
  	int i;
  	struct cramfs_super super;
  	unsigned long root_offset;
  	struct cramfs_sb_info *sbi;
  	struct inode *root;
  
  	sb->s_flags |= MS_RDONLY;
f8314dc60   Panagiotis Issaris   [PATCH] fs: Conve...
237
  	sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
239
240
  	if (!sbi)
  		return -ENOMEM;
  	sb->s_fs_info = sbi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
  
  	/* Invalidate the read buffers on mount: think disk change.. */
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
243
  	mutex_lock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
  	for (i = 0; i < READ_BUFFERS; i++)
  		buffer_blocknr[i] = -1;
  
  	/* Read the first block and get the superblock from it */
  	memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
249
  	mutex_unlock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
252
  
  	/* Do sanity checks on the superblock */
  	if (super.magic != CRAMFS_MAGIC) {
ac8d35c56   Andi Drebes   cramfs: error mes...
253
254
255
256
257
258
259
  		/* check for wrong endianess */
  		if (super.magic == CRAMFS_MAGIC_WEND) {
  			if (!silent)
  				printk(KERN_ERR "cramfs: wrong endianess
  ");
  			goto out;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  		/* check at 512 byte offset */
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
261
  		mutex_lock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  		memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
263
  		mutex_unlock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  		if (super.magic != CRAMFS_MAGIC) {
ac8d35c56   Andi Drebes   cramfs: error mes...
265
266
267
268
  			if (super.magic == CRAMFS_MAGIC_WEND && !silent)
  				printk(KERN_ERR "cramfs: wrong endianess
  ");
  			else if (!silent)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  				printk(KERN_ERR "cramfs: wrong magic
  ");
  			goto out;
  		}
  	}
  
  	/* get feature flags first */
  	if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
  		printk(KERN_ERR "cramfs: unsupported filesystem features
  ");
  		goto out;
  	}
  
  	/* Check that the root inode is in a sane state */
  	if (!S_ISDIR(super.root.mode)) {
  		printk(KERN_ERR "cramfs: root is not a directory
  ");
  		goto out;
  	}
6f772fe65   Stefani Seibold   cramfs: generate ...
288
289
  	/* correct strange, hard-coded permissions of mkcramfs */
  	super.root.mode |= (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  	root_offset = super.root.offset << 2;
  	if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
  		sbi->size=super.size;
  		sbi->blocks=super.fsid.blocks;
  		sbi->files=super.fsid.files;
  	} else {
  		sbi->size=1<<28;
  		sbi->blocks=0;
  		sbi->files=0;
  	}
  	sbi->magic=super.magic;
  	sbi->flags=super.flags;
  	if (root_offset == 0)
  		printk(KERN_INFO "cramfs: empty filesystem");
  	else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
  		 ((root_offset != sizeof(struct cramfs_super)) &&
  		  (root_offset != 512 + sizeof(struct cramfs_super))))
  	{
  		printk(KERN_ERR "cramfs: bad root offset %lu
  ", root_offset);
  		goto out;
  	}
  
  	/* Set it all up.. */
  	sb->s_op = &cramfs_ops;
6f772fe65   Stefani Seibold   cramfs: generate ...
315
  	root = get_cramfs_inode(sb, &super.root, 0);
0577d1ba4   Al Viro   cramfs: get_cramf...
316
  	if (IS_ERR(root))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
322
323
324
325
326
327
328
  		goto out;
  	sb->s_root = d_alloc_root(root);
  	if (!sb->s_root) {
  		iput(root);
  		goto out;
  	}
  	return 0;
  out:
  	kfree(sbi);
  	sb->s_fs_info = NULL;
  	return -EINVAL;
  }
726c33422   David Howells   [PATCH] VFS: Perm...
329
  static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  {
726c33422   David Howells   [PATCH] VFS: Perm...
331
  	struct super_block *sb = dentry->d_sb;
94ea77ac6   Coly Li   fs/cramfs: return...
332
  	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
726c33422   David Howells   [PATCH] VFS: Perm...
333

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
336
337
338
339
340
  	buf->f_type = CRAMFS_MAGIC;
  	buf->f_bsize = PAGE_CACHE_SIZE;
  	buf->f_blocks = CRAMFS_SB(sb)->blocks;
  	buf->f_bfree = 0;
  	buf->f_bavail = 0;
  	buf->f_files = CRAMFS_SB(sb)->files;
  	buf->f_ffree = 0;
94ea77ac6   Coly Li   fs/cramfs: return...
341
342
  	buf->f_fsid.val[0] = (u32)id;
  	buf->f_fsid.val[1] = (u32)(id >> 32);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
347
348
349
350
351
  	buf->f_namelen = CRAMFS_MAXPATHLEN;
  	return 0;
  }
  
  /*
   * Read a cramfs directory entry.
   */
  static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  {
352d8af74   Josef Sipek   [PATCH] struct pa...
352
  	struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
355
356
357
358
359
360
361
362
363
364
  	struct super_block *sb = inode->i_sb;
  	char *buf;
  	unsigned int offset;
  	int copied;
  
  	/* Offset within the thing. */
  	offset = filp->f_pos;
  	if (offset >= inode->i_size)
  		return 0;
  	/* Directory entries are always 4-byte aligned */
  	if (offset & 3)
  		return -EINVAL;
4176ed593   Andi Drebes   fs/cramfs/inode.c...
365
  	buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
367
368
369
370
371
372
373
374
  	if (!buf)
  		return -ENOMEM;
  
  	copied = 0;
  	while (offset < inode->i_size) {
  		struct cramfs_inode *de;
  		unsigned long nextoffset;
  		char *name;
  		ino_t ino;
175a4eb7e   Al Viro   fs: propagate umo...
375
  		umode_t mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
  		int namelen, error;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
377
  		mutex_lock(&read_mutex);
4176ed593   Andi Drebes   fs/cramfs/inode.c...
378
  		de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
382
383
384
385
386
387
  		name = (char *)(de+1);
  
  		/*
  		 * Namelengths on disk are shifted by two
  		 * and the name padded out to 4-byte boundaries
  		 * with zeroes.
  		 */
  		namelen = de->namelen << 2;
  		memcpy(buf, name, namelen);
6f772fe65   Stefani Seibold   cramfs: generate ...
388
  		ino = cramino(de, OFFSET(inode) + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389
  		mode = de->mode;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
390
  		mutex_unlock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  		nextoffset = offset + sizeof(*de) + namelen;
  		for (;;) {
  			if (!namelen) {
  				kfree(buf);
  				return -EIO;
  			}
  			if (buf[namelen-1])
  				break;
  			namelen--;
  		}
  		error = filldir(dirent, buf, namelen, offset, ino, mode >> 12);
  		if (error)
  			break;
  
  		offset = nextoffset;
  		filp->f_pos = offset;
  		copied++;
  	}
  	kfree(buf);
  	return 0;
  }
  
  /*
   * Lookup and fill in the inode data..
   */
  static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  {
  	unsigned int offset = 0;
0577d1ba4   Al Viro   cramfs: get_cramf...
419
  	struct inode *inode = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
  	int sorted;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
421
  	mutex_lock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
426
  	sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
  	while (offset < dir->i_size) {
  		struct cramfs_inode *de;
  		char *name;
  		int namelen, retval;
6f772fe65   Stefani Seibold   cramfs: generate ...
427
  		int dir_off = OFFSET(dir) + offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428

6f772fe65   Stefani Seibold   cramfs: generate ...
429
  		de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  		name = (char *)(de+1);
  
  		/* Try to take advantage of sorted directories */
  		if (sorted && (dentry->d_name.name[0] < name[0]))
  			break;
  
  		namelen = de->namelen << 2;
  		offset += sizeof(*de) + namelen;
  
  		/* Quick check that the name is roughly the right length */
  		if (((dentry->d_name.len + 3) & ~3) != namelen)
  			continue;
  
  		for (;;) {
  			if (!namelen) {
0577d1ba4   Al Viro   cramfs: get_cramf...
445
446
  				inode = ERR_PTR(-EIO);
  				goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
449
450
451
452
453
454
455
456
457
  			}
  			if (name[namelen-1])
  				break;
  			namelen--;
  		}
  		if (namelen != dentry->d_name.len)
  			continue;
  		retval = memcmp(dentry->d_name.name, name, namelen);
  		if (retval > 0)
  			continue;
  		if (!retval) {
0577d1ba4   Al Viro   cramfs: get_cramf...
458
459
  			inode = get_cramfs_inode(dir->i_sb, de, dir_off);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
461
462
463
464
  		}
  		/* else (retval < 0) */
  		if (sorted)
  			break;
  	}
0577d1ba4   Al Viro   cramfs: get_cramf...
465
  out:
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
466
  	mutex_unlock(&read_mutex);
0577d1ba4   Al Viro   cramfs: get_cramf...
467
468
469
  	if (IS_ERR(inode))
  		return ERR_CAST(inode);
  	d_add(dentry, inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
475
  	return NULL;
  }
  
  static int cramfs_readpage(struct file *file, struct page * page)
  {
  	struct inode *inode = page->mapping->host;
98310e581   David VomLehn   cramfs: propagate...
476
477
  	u32 maxblock;
  	int bytes_filled;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
481
  	void *pgdata;
  
  	maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  	bytes_filled = 0;
98310e581   David VomLehn   cramfs: propagate...
482
  	pgdata = kmap(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
484
485
486
487
488
  	if (page->index < maxblock) {
  		struct super_block *sb = inode->i_sb;
  		u32 blkptr_offset = OFFSET(inode) + page->index*4;
  		u32 start_offset, compr_len;
  
  		start_offset = OFFSET(inode) + maxblock*4;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
489
  		mutex_lock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
  		if (page->index)
98310e581   David VomLehn   cramfs: propagate...
491
492
493
494
  			start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4,
  				4);
  		compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) -
  			start_offset);
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
495
  		mutex_unlock(&read_mutex);
98310e581   David VomLehn   cramfs: propagate...
496

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
  		if (compr_len == 0)
  			; /* hole */
98310e581   David VomLehn   cramfs: propagate...
499
500
501
502
503
504
  		else if (unlikely(compr_len > (PAGE_CACHE_SIZE << 1))) {
  			pr_err("cramfs: bad compressed blocksize %u
  ",
  				compr_len);
  			goto err;
  		} else {
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
505
  			mutex_lock(&read_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
508
509
  			bytes_filled = cramfs_uncompress_block(pgdata,
  				 PAGE_CACHE_SIZE,
  				 cramfs_read(sb, start_offset, compr_len),
  				 compr_len);
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
510
  			mutex_unlock(&read_mutex);
98310e581   David VomLehn   cramfs: propagate...
511
512
  			if (unlikely(bytes_filled < 0))
  				goto err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  		}
98310e581   David VomLehn   cramfs: propagate...
514
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
  	memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
  	flush_dcache_page(page);
98310e581   David VomLehn   cramfs: propagate...
517
  	kunmap(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
520
  	SetPageUptodate(page);
  	unlock_page(page);
  	return 0;
98310e581   David VomLehn   cramfs: propagate...
521
522
523
524
525
526
527
  
  err:
  	kunmap(page);
  	ClearPageUptodate(page);
  	SetPageError(page);
  	unlock_page(page);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
  }
f5e54d6e5   Christoph Hellwig   [PATCH] mark addr...
529
  static const struct address_space_operations cramfs_aops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
531
532
533
534
535
536
537
538
539
  	.readpage = cramfs_readpage
  };
  
  /*
   * Our operations:
   */
  
  /*
   * A directory can only readdir
   */
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
540
  static const struct file_operations cramfs_directory_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
542
543
544
  	.llseek		= generic_file_llseek,
  	.read		= generic_read_dir,
  	.readdir	= cramfs_readdir,
  };
754661f14   Arjan van de Ven   [PATCH] mark stru...
545
  static const struct inode_operations cramfs_dir_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
547
  	.lookup		= cramfs_lookup,
  };
ee9b6d61a   Josef 'Jeff' Sipek   [PATCH] Mark stru...
548
  static const struct super_operations cramfs_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
550
551
552
  	.put_super	= cramfs_put_super,
  	.remount_fs	= cramfs_remount,
  	.statfs		= cramfs_statfs,
  };
152a08366   Al Viro   new helper: mount...
553
554
  static struct dentry *cramfs_mount(struct file_system_type *fs_type,
  	int flags, const char *dev_name, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
  {
152a08366   Al Viro   new helper: mount...
556
  	return mount_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
558
559
560
561
  }
  
  static struct file_system_type cramfs_fs_type = {
  	.owner		= THIS_MODULE,
  	.name		= "cramfs",
152a08366   Al Viro   new helper: mount...
562
  	.mount		= cramfs_mount,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
565
566
567
568
  	.kill_sb	= kill_block_super,
  	.fs_flags	= FS_REQUIRES_DEV,
  };
  
  static int __init init_cramfs_fs(void)
  {
50d44ed00   Alexey Dobriyan   [PATCH] cramfs: r...
569
570
571
572
573
574
575
576
577
  	int rv;
  
  	rv = cramfs_uncompress_init();
  	if (rv < 0)
  		return rv;
  	rv = register_filesystem(&cramfs_fs_type);
  	if (rv < 0)
  		cramfs_uncompress_exit();
  	return rv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
579
580
581
582
583
584
585
586
587
588
  }
  
  static void __exit exit_cramfs_fs(void)
  {
  	cramfs_uncompress_exit();
  	unregister_filesystem(&cramfs_fs_type);
  }
  
  module_init(init_cramfs_fs)
  module_exit(exit_cramfs_fs)
  MODULE_LICENSE("GPL");