Blame view

fs/omfs/file.c 9.16 KB
8f09e9876   Bob Copeland   omfs: add file ro...
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   * OMFS (as used by RIO Karma) file operations.
   * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
   * Released under GPL v2.
   */
  
  #include <linux/version.h>
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/buffer_head.h>
  #include <linux/mpage.h>
  #include "omfs.h"
9419fc1c9   Bob Copeland   omfs: fix oops wh...
13
14
15
16
17
18
  static u32 omfs_max_extents(struct omfs_sb_info *sbi, int offset)
  {
  	return (sbi->s_sys_blocksize - offset -
  		sizeof(struct omfs_extent)) /
  		sizeof(struct omfs_extent_entry) + 1;
  }
8f09e9876   Bob Copeland   omfs: add file ro...
19
20
21
  void omfs_make_empty_table(struct buffer_head *bh, int offset)
  {
  	struct omfs_extent *oe = (struct omfs_extent *) &bh->b_data[offset];
d406f66dd   Harvey Harrison   omfs: sparse anno...
22
  	oe->e_next = ~cpu_to_be64(0ULL);
8f09e9876   Bob Copeland   omfs: add file ro...
23
24
  	oe->e_extent_count = cpu_to_be32(1),
  	oe->e_fill = cpu_to_be32(0x22),
d406f66dd   Harvey Harrison   omfs: sparse anno...
25
26
  	oe->e_entry.e_cluster = ~cpu_to_be64(0ULL);
  	oe->e_entry.e_blocks = ~cpu_to_be64(0ULL);
8f09e9876   Bob Copeland   omfs: add file ro...
27
28
29
30
31
32
33
34
35
36
  }
  
  int omfs_shrink_inode(struct inode *inode)
  {
  	struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  	struct omfs_extent *oe;
  	struct omfs_extent_entry *entry;
  	struct buffer_head *bh;
  	u64 next, last;
  	u32 extent_count;
9419fc1c9   Bob Copeland   omfs: fix oops wh...
37
  	u32 max_extents;
8f09e9876   Bob Copeland   omfs: add file ro...
38
39
40
41
42
43
44
45
46
47
48
  	int ret;
  
  	/* traverse extent table, freeing each entry that is greater
  	 * than inode->i_size;
  	 */
  	next = inode->i_ino;
  
  	/* only support truncate -> 0 for now */
  	ret = -EIO;
  	if (inode->i_size != 0)
  		goto out;
f068272cb   Bob Copeland   omfs: check bound...
49
  	bh = omfs_bread(inode->i_sb, next);
8f09e9876   Bob Copeland   omfs: add file ro...
50
51
52
53
  	if (!bh)
  		goto out;
  
  	oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
9419fc1c9   Bob Copeland   omfs: fix oops wh...
54
  	max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
8f09e9876   Bob Copeland   omfs: add file ro...
55
56
  
  	for (;;) {
9419fc1c9   Bob Copeland   omfs: fix oops wh...
57
58
  		if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
  			goto out_brelse;
8f09e9876   Bob Copeland   omfs: add file ro...
59
60
  
  		extent_count = be32_to_cpu(oe->e_extent_count);
9419fc1c9   Bob Copeland   omfs: fix oops wh...
61
62
63
  
  		if (extent_count > max_extents)
  			goto out_brelse;
8f09e9876   Bob Copeland   omfs: add file ro...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  		last = next;
  		next = be64_to_cpu(oe->e_next);
  		entry = &oe->e_entry;
  
  		/* ignore last entry as it is the terminator */
  		for (; extent_count > 1; extent_count--) {
  			u64 start, count;
  			start = be64_to_cpu(entry->e_cluster);
  			count = be64_to_cpu(entry->e_blocks);
  
  			omfs_clear_range(inode->i_sb, start, (int) count);
  			entry++;
  		}
  		omfs_make_empty_table(bh, (char *) oe - bh->b_data);
  		mark_buffer_dirty(bh);
  		brelse(bh);
  
  		if (last != inode->i_ino)
  			omfs_clear_range(inode->i_sb, last, sbi->s_mirrors);
  
  		if (next == ~0)
  			break;
f068272cb   Bob Copeland   omfs: check bound...
86
  		bh = omfs_bread(inode->i_sb, next);
8f09e9876   Bob Copeland   omfs: add file ro...
87
88
89
  		if (!bh)
  			goto out;
  		oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
9419fc1c9   Bob Copeland   omfs: fix oops wh...
90
  		max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
8f09e9876   Bob Copeland   omfs: add file ro...
91
92
93
94
  	}
  	ret = 0;
  out:
  	return ret;
9419fc1c9   Bob Copeland   omfs: fix oops wh...
95
96
97
  out_brelse:
  	brelse(bh);
  	return ret;
8f09e9876   Bob Copeland   omfs: add file ro...
98
99
100
101
102
103
104
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
145
146
147
148
149
  }
  
  static void omfs_truncate(struct inode *inode)
  {
  	omfs_shrink_inode(inode);
  	mark_inode_dirty(inode);
  }
  
  /*
   * Add new blocks to the current extent, or create new entries/continuations
   * as necessary.
   */
  static int omfs_grow_extent(struct inode *inode, struct omfs_extent *oe,
  			u64 *ret_block)
  {
  	struct omfs_extent_entry *terminator;
  	struct omfs_extent_entry *entry = &oe->e_entry;
  	struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  	u32 extent_count = be32_to_cpu(oe->e_extent_count);
  	u64 new_block = 0;
  	u32 max_count;
  	int new_count;
  	int ret = 0;
  
  	/* reached the end of the extent table with no blocks mapped.
  	 * there are three possibilities for adding: grow last extent,
  	 * add a new extent to the current extent table, and add a
  	 * continuation inode.  in last two cases need an allocator for
  	 * sbi->s_cluster_size
  	 */
  
  	/* TODO: handle holes */
  
  	/* should always have a terminator */
  	if (extent_count < 1)
  		return -EIO;
  
  	/* trivially grow current extent, if next block is not taken */
  	terminator = entry + extent_count - 1;
  	if (extent_count > 1) {
  		entry = terminator-1;
  		new_block = be64_to_cpu(entry->e_cluster) +
  			be64_to_cpu(entry->e_blocks);
  
  		if (omfs_allocate_block(inode->i_sb, new_block)) {
  			entry->e_blocks =
  				cpu_to_be64(be64_to_cpu(entry->e_blocks) + 1);
  			terminator->e_blocks = ~(cpu_to_be64(
  				be64_to_cpu(~terminator->e_blocks) + 1));
  			goto out;
  		}
  	}
9419fc1c9   Bob Copeland   omfs: fix oops wh...
150
  	max_count = omfs_max_extents(sbi, OMFS_EXTENT_START);
8f09e9876   Bob Copeland   omfs: add file ro...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
  
  	/* TODO: add a continuation block here */
  	if (be32_to_cpu(oe->e_extent_count) > max_count-1)
  		return -EIO;
  
  	/* try to allocate a new cluster */
  	ret = omfs_allocate_range(inode->i_sb, 1, sbi->s_clustersize,
  		&new_block, &new_count);
  	if (ret)
  		goto out_fail;
  
  	/* copy terminator down an entry */
  	entry = terminator;
  	terminator++;
  	memcpy(terminator, entry, sizeof(struct omfs_extent_entry));
  
  	entry->e_cluster = cpu_to_be64(new_block);
  	entry->e_blocks = cpu_to_be64((u64) new_count);
  
  	terminator->e_blocks = ~(cpu_to_be64(
  		be64_to_cpu(~terminator->e_blocks) + (u64) new_count));
  
  	/* write in new entry */
  	oe->e_extent_count = cpu_to_be32(1 + be32_to_cpu(oe->e_extent_count));
  
  out:
  	*ret_block = new_block;
  out_fail:
  	return ret;
  }
  
  /*
   * Scans across the directory table for a given file block number.
   * If block not found, return 0.
   */
  static sector_t find_block(struct inode *inode, struct omfs_extent_entry *ent,
  			sector_t block, int count, int *left)
  {
  	/* count > 1 because of terminator */
  	sector_t searched = 0;
  	for (; count > 1; count--) {
  		int numblocks = clus_to_blk(OMFS_SB(inode->i_sb),
  			be64_to_cpu(ent->e_blocks));
  
  		if (block >= searched  &&
  		    block < searched + numblocks) {
  			/*
  			 * found it at cluster + (block - searched)
  			 * numblocks - (block - searched) is remainder
  			 */
  			*left = numblocks - (block - searched);
  			return clus_to_blk(OMFS_SB(inode->i_sb),
  				be64_to_cpu(ent->e_cluster)) +
  				block - searched;
  		}
  		searched += numblocks;
  		ent++;
  	}
  	return 0;
  }
  
  static int omfs_get_block(struct inode *inode, sector_t block,
  			  struct buffer_head *bh_result, int create)
  {
  	struct buffer_head *bh;
  	sector_t next, offset;
  	int ret;
ffc188799   Bill Pemberton   omfs: fix uniniti...
218
  	u64 uninitialized_var(new_block);
9419fc1c9   Bob Copeland   omfs: fix oops wh...
219
  	u32 max_extents;
8f09e9876   Bob Copeland   omfs: add file ro...
220
221
222
223
224
225
226
227
  	int extent_count;
  	struct omfs_extent *oe;
  	struct omfs_extent_entry *entry;
  	struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  	int max_blocks = bh_result->b_size >> inode->i_blkbits;
  	int remain;
  
  	ret = -EIO;
f068272cb   Bob Copeland   omfs: check bound...
228
  	bh = omfs_bread(inode->i_sb, inode->i_ino);
8f09e9876   Bob Copeland   omfs: add file ro...
229
230
231
232
  	if (!bh)
  		goto out;
  
  	oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
9419fc1c9   Bob Copeland   omfs: fix oops wh...
233
  	max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
8f09e9876   Bob Copeland   omfs: add file ro...
234
235
236
237
238
239
240
241
242
243
  	next = inode->i_ino;
  
  	for (;;) {
  
  		if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
  			goto out_brelse;
  
  		extent_count = be32_to_cpu(oe->e_extent_count);
  		next = be64_to_cpu(oe->e_next);
  		entry = &oe->e_entry;
9419fc1c9   Bob Copeland   omfs: fix oops wh...
244
245
  		if (extent_count > max_extents)
  			goto out_brelse;
8f09e9876   Bob Copeland   omfs: add file ro...
246
247
248
249
250
251
252
253
254
255
256
257
258
  		offset = find_block(inode, entry, block, extent_count, &remain);
  		if (offset > 0) {
  			ret = 0;
  			map_bh(bh_result, inode->i_sb, offset);
  			if (remain > max_blocks)
  				remain = max_blocks;
  			bh_result->b_size = (remain << inode->i_blkbits);
  			goto out_brelse;
  		}
  		if (next == ~0)
  			break;
  
  		brelse(bh);
f068272cb   Bob Copeland   omfs: check bound...
259
  		bh = omfs_bread(inode->i_sb, next);
8f09e9876   Bob Copeland   omfs: add file ro...
260
261
262
  		if (!bh)
  			goto out;
  		oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
9419fc1c9   Bob Copeland   omfs: fix oops wh...
263
  		max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
8f09e9876   Bob Copeland   omfs: add file ro...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  	}
  	if (create) {
  		ret = omfs_grow_extent(inode, oe, &new_block);
  		if (ret == 0) {
  			mark_buffer_dirty(bh);
  			mark_inode_dirty(inode);
  			map_bh(bh_result, inode->i_sb,
  					clus_to_blk(sbi, new_block));
  		}
  	}
  out_brelse:
  	brelse(bh);
  out:
  	return ret;
  }
  
  static int omfs_readpage(struct file *file, struct page *page)
  {
  	return block_read_full_page(page, omfs_get_block);
  }
  
  static int omfs_readpages(struct file *file, struct address_space *mapping,
  		struct list_head *pages, unsigned nr_pages)
  {
  	return mpage_readpages(mapping, pages, nr_pages, omfs_get_block);
  }
  
  static int omfs_writepage(struct page *page, struct writeback_control *wbc)
  {
  	return block_write_full_page(page, omfs_get_block, wbc);
  }
  
  static int
  omfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  {
  	return mpage_writepages(mapping, wbc, omfs_get_block);
  }
  
  static int omfs_write_begin(struct file *file, struct address_space *mapping,
  			loff_t pos, unsigned len, unsigned flags,
  			struct page **pagep, void **fsdata)
  {
155130a4f   Christoph Hellwig   get rid of block_...
306
307
308
309
310
311
312
313
314
315
316
  	int ret;
  
  	ret = block_write_begin(mapping, pos, len, flags, pagep,
  				omfs_get_block);
  	if (unlikely(ret)) {
  		loff_t isize = mapping->host->i_size;
  		if (pos + len > isize)
  			vmtruncate(mapping->host, isize);
  	}
  
  	return ret;
8f09e9876   Bob Copeland   omfs: add file ro...
317
318
319
320
321
322
  }
  
  static sector_t omfs_bmap(struct address_space *mapping, sector_t block)
  {
  	return generic_block_bmap(mapping, block, omfs_get_block);
  }
828c09509   Alexey Dobriyan   const: constify r...
323
  const struct file_operations omfs_file_operations = {
8f09e9876   Bob Copeland   omfs: add file ro...
324
325
326
327
328
329
  	.llseek = generic_file_llseek,
  	.read = do_sync_read,
  	.write = do_sync_write,
  	.aio_read = generic_file_aio_read,
  	.aio_write = generic_file_aio_write,
  	.mmap = generic_file_mmap,
1b061d924   Christoph Hellwig   rename the generi...
330
  	.fsync = generic_file_fsync,
8f09e9876   Bob Copeland   omfs: add file ro...
331
332
  	.splice_read = generic_file_splice_read,
  };
d39aae9ec   Christoph Hellwig   add missing setat...
333
334
335
336
337
338
339
340
  static int omfs_setattr(struct dentry *dentry, struct iattr *attr)
  {
  	struct inode *inode = dentry->d_inode;
  	int error;
  
  	error = inode_change_ok(inode, attr);
  	if (error)
  		return error;
1025774ce   Christoph Hellwig   remove inode_setattr
341
342
343
344
345
346
347
348
349
350
351
  
  	if ((attr->ia_valid & ATTR_SIZE) &&
  	    attr->ia_size != i_size_read(inode)) {
  		error = vmtruncate(inode, attr->ia_size);
  		if (error)
  			return error;
  	}
  
  	setattr_copy(inode, attr);
  	mark_inode_dirty(inode);
  	return 0;
d39aae9ec   Christoph Hellwig   add missing setat...
352
  }
6e1d5dcc2   Alexey Dobriyan   const: mark remai...
353
  const struct inode_operations omfs_file_inops = {
d39aae9ec   Christoph Hellwig   add missing setat...
354
  	.setattr = omfs_setattr,
8f09e9876   Bob Copeland   omfs: add file ro...
355
356
  	.truncate = omfs_truncate
  };
7f09410bb   Alexey Dobriyan   const: mark remai...
357
  const struct address_space_operations omfs_aops = {
8f09e9876   Bob Copeland   omfs: add file ro...
358
359
360
361
362
363
364
365
366
  	.readpage = omfs_readpage,
  	.readpages = omfs_readpages,
  	.writepage = omfs_writepage,
  	.writepages = omfs_writepages,
  	.sync_page = block_sync_page,
  	.write_begin = omfs_write_begin,
  	.write_end = generic_write_end,
  	.bmap = omfs_bmap,
  };