10 int tfs_read(struct file
*file
, void *buf
, int blocks
)
12 struct tfs_sb_info
*sbi
= TFS_SBI(file
->fs
);
13 struct cache_struct
*cs
;
15 int index
= file
->offset
>> sbi
->s_block_shift
;
18 TFS_DEBUG("rbuf: %p, blocks: %d, offset: %d\n", buf
, blocks
, file
->offset
);
22 if (file
->offset
>= file
->inode
->i_size
)
26 block
= tfs_bmap(file
->inode
, index
++);
29 cs
= get_cache_block(file
->fs
, block
);
32 memcpy(buf
, cs
->data
, sbi
->s_block_size
);
33 bytes_read
+= sbi
->s_block_size
;
34 buf
+= sbi
->s_block_size
;
41 #define min(a, b) ((a) < (b) ? a : b)
42 int tfs_write(struct file
*file
, void *buf
, int blocks
)
44 struct tfs_sb_info
*sbi
= TFS_SBI(file
->fs
);
45 struct cache_struct
*cs
;
46 int index
= file
->offset
>> sbi
->s_block_shift
;
48 int bufoff
= file
->offset
& (sbi
->s_block_size
- 1);
49 int bytes_written
= 0;
51 TFS_DEBUG("wbuf: %p, blocks: %d, offset: %d\n", buf
, blocks
, file
->offset
);
56 block
= tfs_bmap(file
->inode
, index
++);
58 if (index
- 1 < TFS_N_BLOCKS
) {
59 block
= tfs_alloc_block(sbi
, sbi
->s_data_area
);
62 file
->inode
->i_data
[index
- 1] = block
;
68 cs
= get_cache_block(file
->fs
, block
);
71 bytes_written
= sbi
->s_block_size
- bufoff
;
72 memmove(cs
->data
+ bufoff
, buf
, bytes_written
);
74 file
->inode
->i_size
+= MAX(0, file
->offset
+ bytes_written
- file
->inode
->i_size
);
75 /* write back to disk */
76 if (tfs_bwrite(sbi
, block
, cs
->data
))
82 block
= tfs_bmap(file
->inode
, index
++);
84 if (index
- 1 < TFS_N_BLOCKS
) {
85 block
= tfs_alloc_block(sbi
, sbi
->s_data_area
);
88 file
->inode
->i_data
[index
- 1] = block
;
94 bytes_need
= sbi
->s_block_size
;
95 cs
= get_cache_block(file
->fs
, block
);
98 memcpy(cs
->data
, buf
, bytes_need
);
99 bytes_written
+= bytes_need
;
101 file
->inode
->i_size
+= MAX(0, file
->offset
+ bytes_written
- file
->inode
->i_size
);
102 if (tfs_bwrite(sbi
, block
, cs
->data
))
106 if (tfs_iwrite(file
->inode
))
109 return bytes_written
;
112 void tfs_close(struct file
*file
)
115 free_inode(file
->inode
);
120 struct file_operations tfs_file_ops
= {
124 .readdir
= tfs_readdir