2 * OMFS (as used by RIO Karma) file operations.
3 * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
4 * Released under GPL v2.
7 #include <linux/version.h>
8 #include <linux/module.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
14 static int omfs_sync_file(struct file
*file
, struct dentry
*dentry
,
17 struct inode
*inode
= dentry
->d_inode
;
20 err
= sync_mapping_buffers(inode
->i_mapping
);
21 if (!(inode
->i_state
& I_DIRTY
))
23 if (datasync
&& !(inode
->i_state
& I_DIRTY_DATASYNC
))
25 err
|= omfs_sync_inode(inode
);
26 return err
? -EIO
: 0;
29 static u32
omfs_max_extents(struct omfs_sb_info
*sbi
, int offset
)
31 return (sbi
->s_sys_blocksize
- offset
-
32 sizeof(struct omfs_extent
)) /
33 sizeof(struct omfs_extent_entry
) + 1;
36 void omfs_make_empty_table(struct buffer_head
*bh
, int offset
)
38 struct omfs_extent
*oe
= (struct omfs_extent
*) &bh
->b_data
[offset
];
40 oe
->e_next
= ~cpu_to_be64(0ULL);
41 oe
->e_extent_count
= cpu_to_be32(1),
42 oe
->e_fill
= cpu_to_be32(0x22),
43 oe
->e_entry
.e_cluster
= ~cpu_to_be64(0ULL);
44 oe
->e_entry
.e_blocks
= ~cpu_to_be64(0ULL);
47 int omfs_shrink_inode(struct inode
*inode
)
49 struct omfs_sb_info
*sbi
= OMFS_SB(inode
->i_sb
);
50 struct omfs_extent
*oe
;
51 struct omfs_extent_entry
*entry
;
52 struct buffer_head
*bh
;
58 /* traverse extent table, freeing each entry that is greater
63 /* only support truncate -> 0 for now */
65 if (inode
->i_size
!= 0)
68 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, next
));
72 oe
= (struct omfs_extent
*)(&bh
->b_data
[OMFS_EXTENT_START
]);
73 max_extents
= omfs_max_extents(sbi
, OMFS_EXTENT_START
);
77 if (omfs_is_bad(sbi
, (struct omfs_header
*) bh
->b_data
, next
))
80 extent_count
= be32_to_cpu(oe
->e_extent_count
);
82 if (extent_count
> max_extents
)
86 next
= be64_to_cpu(oe
->e_next
);
89 /* ignore last entry as it is the terminator */
90 for (; extent_count
> 1; extent_count
--) {
92 start
= be64_to_cpu(entry
->e_cluster
);
93 count
= be64_to_cpu(entry
->e_blocks
);
95 omfs_clear_range(inode
->i_sb
, start
, (int) count
);
98 omfs_make_empty_table(bh
, (char *) oe
- bh
->b_data
);
99 mark_buffer_dirty(bh
);
102 if (last
!= inode
->i_ino
)
103 omfs_clear_range(inode
->i_sb
, last
, sbi
->s_mirrors
);
108 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, next
));
111 oe
= (struct omfs_extent
*) (&bh
->b_data
[OMFS_EXTENT_CONT
]);
112 max_extents
= omfs_max_extents(sbi
, OMFS_EXTENT_CONT
);
122 static void omfs_truncate(struct inode
*inode
)
124 omfs_shrink_inode(inode
);
125 mark_inode_dirty(inode
);
129 * Add new blocks to the current extent, or create new entries/continuations
132 static int omfs_grow_extent(struct inode
*inode
, struct omfs_extent
*oe
,
135 struct omfs_extent_entry
*terminator
;
136 struct omfs_extent_entry
*entry
= &oe
->e_entry
;
137 struct omfs_sb_info
*sbi
= OMFS_SB(inode
->i_sb
);
138 u32 extent_count
= be32_to_cpu(oe
->e_extent_count
);
144 /* reached the end of the extent table with no blocks mapped.
145 * there are three possibilities for adding: grow last extent,
146 * add a new extent to the current extent table, and add a
147 * continuation inode. in last two cases need an allocator for
148 * sbi->s_cluster_size
151 /* TODO: handle holes */
153 /* should always have a terminator */
154 if (extent_count
< 1)
157 /* trivially grow current extent, if next block is not taken */
158 terminator
= entry
+ extent_count
- 1;
159 if (extent_count
> 1) {
160 entry
= terminator
-1;
161 new_block
= be64_to_cpu(entry
->e_cluster
) +
162 be64_to_cpu(entry
->e_blocks
);
164 if (omfs_allocate_block(inode
->i_sb
, new_block
)) {
166 cpu_to_be64(be64_to_cpu(entry
->e_blocks
) + 1);
167 terminator
->e_blocks
= ~(cpu_to_be64(
168 be64_to_cpu(~terminator
->e_blocks
) + 1));
172 max_count
= omfs_max_extents(sbi
, OMFS_EXTENT_START
);
174 /* TODO: add a continuation block here */
175 if (be32_to_cpu(oe
->e_extent_count
) > max_count
-1)
178 /* try to allocate a new cluster */
179 ret
= omfs_allocate_range(inode
->i_sb
, 1, sbi
->s_clustersize
,
180 &new_block
, &new_count
);
184 /* copy terminator down an entry */
187 memcpy(terminator
, entry
, sizeof(struct omfs_extent_entry
));
189 entry
->e_cluster
= cpu_to_be64(new_block
);
190 entry
->e_blocks
= cpu_to_be64((u64
) new_count
);
192 terminator
->e_blocks
= ~(cpu_to_be64(
193 be64_to_cpu(~terminator
->e_blocks
) + (u64
) new_count
));
195 /* write in new entry */
196 oe
->e_extent_count
= cpu_to_be32(1 + be32_to_cpu(oe
->e_extent_count
));
199 *ret_block
= new_block
;
205 * Scans across the directory table for a given file block number.
206 * If block not found, return 0.
208 static sector_t
find_block(struct inode
*inode
, struct omfs_extent_entry
*ent
,
209 sector_t block
, int count
, int *left
)
211 /* count > 1 because of terminator */
212 sector_t searched
= 0;
213 for (; count
> 1; count
--) {
214 int numblocks
= clus_to_blk(OMFS_SB(inode
->i_sb
),
215 be64_to_cpu(ent
->e_blocks
));
217 if (block
>= searched
&&
218 block
< searched
+ numblocks
) {
220 * found it at cluster + (block - searched)
221 * numblocks - (block - searched) is remainder
223 *left
= numblocks
- (block
- searched
);
224 return clus_to_blk(OMFS_SB(inode
->i_sb
),
225 be64_to_cpu(ent
->e_cluster
)) +
228 searched
+= numblocks
;
234 static int omfs_get_block(struct inode
*inode
, sector_t block
,
235 struct buffer_head
*bh_result
, int create
)
237 struct buffer_head
*bh
;
238 sector_t next
, offset
;
243 struct omfs_extent
*oe
;
244 struct omfs_extent_entry
*entry
;
245 struct omfs_sb_info
*sbi
= OMFS_SB(inode
->i_sb
);
246 int max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
250 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, inode
->i_ino
));
254 oe
= (struct omfs_extent
*)(&bh
->b_data
[OMFS_EXTENT_START
]);
255 max_extents
= omfs_max_extents(sbi
, OMFS_EXTENT_START
);
260 if (omfs_is_bad(sbi
, (struct omfs_header
*) bh
->b_data
, next
))
263 extent_count
= be32_to_cpu(oe
->e_extent_count
);
264 next
= be64_to_cpu(oe
->e_next
);
265 entry
= &oe
->e_entry
;
267 if (extent_count
> max_extents
)
270 offset
= find_block(inode
, entry
, block
, extent_count
, &remain
);
273 map_bh(bh_result
, inode
->i_sb
, offset
);
274 if (remain
> max_blocks
)
276 bh_result
->b_size
= (remain
<< inode
->i_blkbits
);
283 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, next
));
286 oe
= (struct omfs_extent
*) (&bh
->b_data
[OMFS_EXTENT_CONT
]);
287 max_extents
= omfs_max_extents(sbi
, OMFS_EXTENT_CONT
);
290 ret
= omfs_grow_extent(inode
, oe
, &new_block
);
292 mark_buffer_dirty(bh
);
293 mark_inode_dirty(inode
);
294 map_bh(bh_result
, inode
->i_sb
,
295 clus_to_blk(sbi
, new_block
));
304 static int omfs_readpage(struct file
*file
, struct page
*page
)
306 return block_read_full_page(page
, omfs_get_block
);
309 static int omfs_readpages(struct file
*file
, struct address_space
*mapping
,
310 struct list_head
*pages
, unsigned nr_pages
)
312 return mpage_readpages(mapping
, pages
, nr_pages
, omfs_get_block
);
315 static int omfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
317 return block_write_full_page(page
, omfs_get_block
, wbc
);
321 omfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
323 return mpage_writepages(mapping
, wbc
, omfs_get_block
);
326 static int omfs_write_begin(struct file
*file
, struct address_space
*mapping
,
327 loff_t pos
, unsigned len
, unsigned flags
,
328 struct page
**pagep
, void **fsdata
)
331 return block_write_begin(file
, mapping
, pos
, len
, flags
,
332 pagep
, fsdata
, omfs_get_block
);
335 static sector_t
omfs_bmap(struct address_space
*mapping
, sector_t block
)
337 return generic_block_bmap(mapping
, block
, omfs_get_block
);
340 struct file_operations omfs_file_operations
= {
341 .llseek
= generic_file_llseek
,
342 .read
= do_sync_read
,
343 .write
= do_sync_write
,
344 .aio_read
= generic_file_aio_read
,
345 .aio_write
= generic_file_aio_write
,
346 .mmap
= generic_file_mmap
,
347 .fsync
= omfs_sync_file
,
348 .splice_read
= generic_file_splice_read
,
351 struct inode_operations omfs_file_inops
= {
352 .truncate
= omfs_truncate
355 struct address_space_operations omfs_aops
= {
356 .readpage
= omfs_readpage
,
357 .readpages
= omfs_readpages
,
358 .writepage
= omfs_writepage
,
359 .writepages
= omfs_writepages
,
360 .sync_page
= block_sync_page
,
361 .write_begin
= omfs_write_begin
,
362 .write_end
= generic_write_end
,