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 void omfs_make_empty_table(struct buffer_head
*bh
, int offset
)
31 struct omfs_extent
*oe
= (struct omfs_extent
*) &bh
->b_data
[offset
];
33 oe
->e_next
= ~cpu_to_be64(0ULL);
34 oe
->e_extent_count
= cpu_to_be32(1),
35 oe
->e_fill
= cpu_to_be32(0x22),
36 oe
->e_entry
.e_cluster
= ~cpu_to_be64(0ULL);
37 oe
->e_entry
.e_blocks
= ~cpu_to_be64(0ULL);
40 int omfs_shrink_inode(struct inode
*inode
)
42 struct omfs_sb_info
*sbi
= OMFS_SB(inode
->i_sb
);
43 struct omfs_extent
*oe
;
44 struct omfs_extent_entry
*entry
;
45 struct buffer_head
*bh
;
50 /* traverse extent table, freeing each entry that is greater
55 /* only support truncate -> 0 for now */
57 if (inode
->i_size
!= 0)
60 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, next
));
64 oe
= (struct omfs_extent
*)(&bh
->b_data
[OMFS_EXTENT_START
]);
68 if (omfs_is_bad(sbi
, (struct omfs_header
*) bh
->b_data
, next
)) {
73 extent_count
= be32_to_cpu(oe
->e_extent_count
);
75 next
= be64_to_cpu(oe
->e_next
);
78 /* ignore last entry as it is the terminator */
79 for (; extent_count
> 1; extent_count
--) {
81 start
= be64_to_cpu(entry
->e_cluster
);
82 count
= be64_to_cpu(entry
->e_blocks
);
84 omfs_clear_range(inode
->i_sb
, start
, (int) count
);
87 omfs_make_empty_table(bh
, (char *) oe
- bh
->b_data
);
88 mark_buffer_dirty(bh
);
91 if (last
!= inode
->i_ino
)
92 omfs_clear_range(inode
->i_sb
, last
, sbi
->s_mirrors
);
97 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, next
));
100 oe
= (struct omfs_extent
*) (&bh
->b_data
[OMFS_EXTENT_CONT
]);
107 static void omfs_truncate(struct inode
*inode
)
109 omfs_shrink_inode(inode
);
110 mark_inode_dirty(inode
);
114 * Add new blocks to the current extent, or create new entries/continuations
117 static int omfs_grow_extent(struct inode
*inode
, struct omfs_extent
*oe
,
120 struct omfs_extent_entry
*terminator
;
121 struct omfs_extent_entry
*entry
= &oe
->e_entry
;
122 struct omfs_sb_info
*sbi
= OMFS_SB(inode
->i_sb
);
123 u32 extent_count
= be32_to_cpu(oe
->e_extent_count
);
129 /* reached the end of the extent table with no blocks mapped.
130 * there are three possibilities for adding: grow last extent,
131 * add a new extent to the current extent table, and add a
132 * continuation inode. in last two cases need an allocator for
133 * sbi->s_cluster_size
136 /* TODO: handle holes */
138 /* should always have a terminator */
139 if (extent_count
< 1)
142 /* trivially grow current extent, if next block is not taken */
143 terminator
= entry
+ extent_count
- 1;
144 if (extent_count
> 1) {
145 entry
= terminator
-1;
146 new_block
= be64_to_cpu(entry
->e_cluster
) +
147 be64_to_cpu(entry
->e_blocks
);
149 if (omfs_allocate_block(inode
->i_sb
, new_block
)) {
151 cpu_to_be64(be64_to_cpu(entry
->e_blocks
) + 1);
152 terminator
->e_blocks
= ~(cpu_to_be64(
153 be64_to_cpu(~terminator
->e_blocks
) + 1));
157 max_count
= (sbi
->s_sys_blocksize
- OMFS_EXTENT_START
-
158 sizeof(struct omfs_extent
)) /
159 sizeof(struct omfs_extent_entry
) + 1;
161 /* TODO: add a continuation block here */
162 if (be32_to_cpu(oe
->e_extent_count
) > max_count
-1)
165 /* try to allocate a new cluster */
166 ret
= omfs_allocate_range(inode
->i_sb
, 1, sbi
->s_clustersize
,
167 &new_block
, &new_count
);
171 /* copy terminator down an entry */
174 memcpy(terminator
, entry
, sizeof(struct omfs_extent_entry
));
176 entry
->e_cluster
= cpu_to_be64(new_block
);
177 entry
->e_blocks
= cpu_to_be64((u64
) new_count
);
179 terminator
->e_blocks
= ~(cpu_to_be64(
180 be64_to_cpu(~terminator
->e_blocks
) + (u64
) new_count
));
182 /* write in new entry */
183 oe
->e_extent_count
= cpu_to_be32(1 + be32_to_cpu(oe
->e_extent_count
));
186 *ret_block
= new_block
;
192 * Scans across the directory table for a given file block number.
193 * If block not found, return 0.
195 static sector_t
find_block(struct inode
*inode
, struct omfs_extent_entry
*ent
,
196 sector_t block
, int count
, int *left
)
198 /* count > 1 because of terminator */
199 sector_t searched
= 0;
200 for (; count
> 1; count
--) {
201 int numblocks
= clus_to_blk(OMFS_SB(inode
->i_sb
),
202 be64_to_cpu(ent
->e_blocks
));
204 if (block
>= searched
&&
205 block
< searched
+ numblocks
) {
207 * found it at cluster + (block - searched)
208 * numblocks - (block - searched) is remainder
210 *left
= numblocks
- (block
- searched
);
211 return clus_to_blk(OMFS_SB(inode
->i_sb
),
212 be64_to_cpu(ent
->e_cluster
)) +
215 searched
+= numblocks
;
221 static int omfs_get_block(struct inode
*inode
, sector_t block
,
222 struct buffer_head
*bh_result
, int create
)
224 struct buffer_head
*bh
;
225 sector_t next
, offset
;
229 struct omfs_extent
*oe
;
230 struct omfs_extent_entry
*entry
;
231 struct omfs_sb_info
*sbi
= OMFS_SB(inode
->i_sb
);
232 int max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
236 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, inode
->i_ino
));
240 oe
= (struct omfs_extent
*)(&bh
->b_data
[OMFS_EXTENT_START
]);
245 if (omfs_is_bad(sbi
, (struct omfs_header
*) bh
->b_data
, next
))
248 extent_count
= be32_to_cpu(oe
->e_extent_count
);
249 next
= be64_to_cpu(oe
->e_next
);
250 entry
= &oe
->e_entry
;
252 offset
= find_block(inode
, entry
, block
, extent_count
, &remain
);
255 map_bh(bh_result
, inode
->i_sb
, offset
);
256 if (remain
> max_blocks
)
258 bh_result
->b_size
= (remain
<< inode
->i_blkbits
);
265 bh
= sb_bread(inode
->i_sb
, clus_to_blk(sbi
, next
));
268 oe
= (struct omfs_extent
*) (&bh
->b_data
[OMFS_EXTENT_CONT
]);
271 ret
= omfs_grow_extent(inode
, oe
, &new_block
);
273 mark_buffer_dirty(bh
);
274 mark_inode_dirty(inode
);
275 map_bh(bh_result
, inode
->i_sb
,
276 clus_to_blk(sbi
, new_block
));
285 static int omfs_readpage(struct file
*file
, struct page
*page
)
287 return block_read_full_page(page
, omfs_get_block
);
290 static int omfs_readpages(struct file
*file
, struct address_space
*mapping
,
291 struct list_head
*pages
, unsigned nr_pages
)
293 return mpage_readpages(mapping
, pages
, nr_pages
, omfs_get_block
);
296 static int omfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
298 return block_write_full_page(page
, omfs_get_block
, wbc
);
302 omfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
304 return mpage_writepages(mapping
, wbc
, omfs_get_block
);
307 static int omfs_write_begin(struct file
*file
, struct address_space
*mapping
,
308 loff_t pos
, unsigned len
, unsigned flags
,
309 struct page
**pagep
, void **fsdata
)
312 return block_write_begin(file
, mapping
, pos
, len
, flags
,
313 pagep
, fsdata
, omfs_get_block
);
316 static sector_t
omfs_bmap(struct address_space
*mapping
, sector_t block
)
318 return generic_block_bmap(mapping
, block
, omfs_get_block
);
321 struct file_operations omfs_file_operations
= {
322 .llseek
= generic_file_llseek
,
323 .read
= do_sync_read
,
324 .write
= do_sync_write
,
325 .aio_read
= generic_file_aio_read
,
326 .aio_write
= generic_file_aio_write
,
327 .mmap
= generic_file_mmap
,
328 .fsync
= omfs_sync_file
,
329 .splice_read
= generic_file_splice_read
,
332 struct inode_operations omfs_file_inops
= {
333 .truncate
= omfs_truncate
336 struct address_space_operations omfs_aops
= {
337 .readpage
= omfs_readpage
,
338 .readpages
= omfs_readpages
,
339 .writepage
= omfs_writepage
,
340 .writepages
= omfs_writepages
,
341 .sync_page
= block_sync_page
,
342 .write_begin
= omfs_write_begin
,
343 .write_end
= generic_write_end
,