diff options
Diffstat (limited to 'fs/reiserfs/file.c')
-rw-r--r-- | fs/reiserfs/file.c | 2564 |
1 files changed, 1362 insertions, 1202 deletions
diff --git a/fs/reiserfs/file.c b/fs/reiserfs/file.c index 12e91209544e..c9f178fb494f 100644 --- a/fs/reiserfs/file.c +++ b/fs/reiserfs/file.c | |||
@@ -2,7 +2,6 @@ | |||
2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README | 2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README |
3 | */ | 3 | */ |
4 | 4 | ||
5 | |||
6 | #include <linux/time.h> | 5 | #include <linux/time.h> |
7 | #include <linux/reiserfs_fs.h> | 6 | #include <linux/reiserfs_fs.h> |
8 | #include <linux/reiserfs_acl.h> | 7 | #include <linux/reiserfs_acl.h> |
@@ -31,82 +30,84 @@ | |||
31 | ** We use reiserfs_truncate_file to pack the tail, since it already has | 30 | ** We use reiserfs_truncate_file to pack the tail, since it already has |
32 | ** all the conditions coded. | 31 | ** all the conditions coded. |
33 | */ | 32 | */ |
34 | static int reiserfs_file_release (struct inode * inode, struct file * filp) | 33 | static int reiserfs_file_release(struct inode *inode, struct file *filp) |
35 | { | 34 | { |
36 | 35 | ||
37 | struct reiserfs_transaction_handle th ; | 36 | struct reiserfs_transaction_handle th; |
38 | int err; | 37 | int err; |
39 | int jbegin_failure = 0; | 38 | int jbegin_failure = 0; |
40 | 39 | ||
41 | if (!S_ISREG (inode->i_mode)) | 40 | if (!S_ISREG(inode->i_mode)) |
42 | BUG (); | 41 | BUG(); |
43 | 42 | ||
44 | /* fast out for when nothing needs to be done */ | 43 | /* fast out for when nothing needs to be done */ |
45 | if ((atomic_read(&inode->i_count) > 1 || | 44 | if ((atomic_read(&inode->i_count) > 1 || |
46 | !(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) || | 45 | !(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) || |
47 | !tail_has_to_be_packed(inode)) && | 46 | !tail_has_to_be_packed(inode)) && |
48 | REISERFS_I(inode)->i_prealloc_count <= 0) { | 47 | REISERFS_I(inode)->i_prealloc_count <= 0) { |
49 | return 0; | 48 | return 0; |
50 | } | 49 | } |
51 | |||
52 | reiserfs_write_lock(inode->i_sb); | ||
53 | down (&inode->i_sem); | ||
54 | /* freeing preallocation only involves relogging blocks that | ||
55 | * are already in the current transaction. preallocation gets | ||
56 | * freed at the end of each transaction, so it is impossible for | ||
57 | * us to log any additional blocks (including quota blocks) | ||
58 | */ | ||
59 | err = journal_begin(&th, inode->i_sb, 1); | ||
60 | if (err) { | ||
61 | /* uh oh, we can't allow the inode to go away while there | ||
62 | * is still preallocation blocks pending. Try to join the | ||
63 | * aborted transaction | ||
64 | */ | ||
65 | jbegin_failure = err; | ||
66 | err = journal_join_abort(&th, inode->i_sb, 1); | ||
67 | 50 | ||
51 | reiserfs_write_lock(inode->i_sb); | ||
52 | down(&inode->i_sem); | ||
53 | /* freeing preallocation only involves relogging blocks that | ||
54 | * are already in the current transaction. preallocation gets | ||
55 | * freed at the end of each transaction, so it is impossible for | ||
56 | * us to log any additional blocks (including quota blocks) | ||
57 | */ | ||
58 | err = journal_begin(&th, inode->i_sb, 1); | ||
68 | if (err) { | 59 | if (err) { |
69 | /* hmpf, our choices here aren't good. We can pin the inode | 60 | /* uh oh, we can't allow the inode to go away while there |
70 | * which will disallow unmount from every happening, we can | 61 | * is still preallocation blocks pending. Try to join the |
71 | * do nothing, which will corrupt random memory on unmount, | 62 | * aborted transaction |
72 | * or we can forcibly remove the file from the preallocation | 63 | */ |
73 | * list, which will leak blocks on disk. Lets pin the inode | 64 | jbegin_failure = err; |
74 | * and let the admin know what is going on. | 65 | err = journal_join_abort(&th, inode->i_sb, 1); |
75 | */ | 66 | |
76 | igrab(inode); | 67 | if (err) { |
77 | reiserfs_warning(inode->i_sb, "pinning inode %lu because the " | 68 | /* hmpf, our choices here aren't good. We can pin the inode |
78 | "preallocation can't be freed"); | 69 | * which will disallow unmount from every happening, we can |
79 | goto out; | 70 | * do nothing, which will corrupt random memory on unmount, |
71 | * or we can forcibly remove the file from the preallocation | ||
72 | * list, which will leak blocks on disk. Lets pin the inode | ||
73 | * and let the admin know what is going on. | ||
74 | */ | ||
75 | igrab(inode); | ||
76 | reiserfs_warning(inode->i_sb, | ||
77 | "pinning inode %lu because the " | ||
78 | "preallocation can't be freed"); | ||
79 | goto out; | ||
80 | } | ||
80 | } | 81 | } |
81 | } | 82 | reiserfs_update_inode_transaction(inode); |
82 | reiserfs_update_inode_transaction(inode) ; | ||
83 | 83 | ||
84 | #ifdef REISERFS_PREALLOCATE | 84 | #ifdef REISERFS_PREALLOCATE |
85 | reiserfs_discard_prealloc (&th, inode); | 85 | reiserfs_discard_prealloc(&th, inode); |
86 | #endif | 86 | #endif |
87 | err = journal_end(&th, inode->i_sb, 1); | 87 | err = journal_end(&th, inode->i_sb, 1); |
88 | 88 | ||
89 | /* copy back the error code from journal_begin */ | 89 | /* copy back the error code from journal_begin */ |
90 | if (!err) | 90 | if (!err) |
91 | err = jbegin_failure; | 91 | err = jbegin_failure; |
92 | 92 | ||
93 | if (!err && atomic_read(&inode->i_count) <= 1 && | 93 | if (!err && atomic_read(&inode->i_count) <= 1 && |
94 | (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) && | 94 | (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) && |
95 | tail_has_to_be_packed (inode)) { | 95 | tail_has_to_be_packed(inode)) { |
96 | /* if regular file is released by last holder and it has been | 96 | /* if regular file is released by last holder and it has been |
97 | appended (we append by unformatted node only) or its direct | 97 | appended (we append by unformatted node only) or its direct |
98 | item(s) had to be converted, then it may have to be | 98 | item(s) had to be converted, then it may have to be |
99 | indirect2direct converted */ | 99 | indirect2direct converted */ |
100 | err = reiserfs_truncate_file(inode, 0) ; | 100 | err = reiserfs_truncate_file(inode, 0); |
101 | } | 101 | } |
102 | out: | 102 | out: |
103 | up (&inode->i_sem); | 103 | up(&inode->i_sem); |
104 | reiserfs_write_unlock(inode->i_sb); | 104 | reiserfs_write_unlock(inode->i_sb); |
105 | return err; | 105 | return err; |
106 | } | 106 | } |
107 | 107 | ||
108 | static void reiserfs_vfs_truncate_file(struct inode *inode) { | 108 | static void reiserfs_vfs_truncate_file(struct inode *inode) |
109 | reiserfs_truncate_file(inode, 1) ; | 109 | { |
110 | reiserfs_truncate_file(inode, 1); | ||
110 | } | 111 | } |
111 | 112 | ||
112 | /* Sync a reiserfs file. */ | 113 | /* Sync a reiserfs file. */ |
@@ -116,26 +117,24 @@ static void reiserfs_vfs_truncate_file(struct inode *inode) { | |||
116 | * be removed... | 117 | * be removed... |
117 | */ | 118 | */ |
118 | 119 | ||
119 | static int reiserfs_sync_file( | 120 | static int reiserfs_sync_file(struct file *p_s_filp, |
120 | struct file * p_s_filp, | 121 | struct dentry *p_s_dentry, int datasync) |
121 | struct dentry * p_s_dentry, | 122 | { |
122 | int datasync | 123 | struct inode *p_s_inode = p_s_dentry->d_inode; |
123 | ) { | 124 | int n_err; |
124 | struct inode * p_s_inode = p_s_dentry->d_inode; | 125 | int barrier_done; |
125 | int n_err; | 126 | |
126 | int barrier_done; | 127 | if (!S_ISREG(p_s_inode->i_mode)) |
127 | 128 | BUG(); | |
128 | if (!S_ISREG(p_s_inode->i_mode)) | 129 | n_err = sync_mapping_buffers(p_s_inode->i_mapping); |
129 | BUG (); | 130 | reiserfs_write_lock(p_s_inode->i_sb); |
130 | n_err = sync_mapping_buffers(p_s_inode->i_mapping) ; | 131 | barrier_done = reiserfs_commit_for_inode(p_s_inode); |
131 | reiserfs_write_lock(p_s_inode->i_sb); | 132 | reiserfs_write_unlock(p_s_inode->i_sb); |
132 | barrier_done = reiserfs_commit_for_inode(p_s_inode); | 133 | if (barrier_done != 1) |
133 | reiserfs_write_unlock(p_s_inode->i_sb); | 134 | blkdev_issue_flush(p_s_inode->i_sb->s_bdev, NULL); |
134 | if (barrier_done != 1) | 135 | if (barrier_done < 0) |
135 | blkdev_issue_flush(p_s_inode->i_sb->s_bdev, NULL); | 136 | return barrier_done; |
136 | if (barrier_done < 0) | 137 | return (n_err < 0) ? -EIO : 0; |
137 | return barrier_done; | ||
138 | return ( n_err < 0 ) ? -EIO : 0; | ||
139 | } | 138 | } |
140 | 139 | ||
141 | /* I really do not want to play with memory shortage right now, so | 140 | /* I really do not want to play with memory shortage right now, so |
@@ -147,700 +146,797 @@ static int reiserfs_sync_file( | |||
147 | /* Allocates blocks for a file to fulfil write request. | 146 | /* Allocates blocks for a file to fulfil write request. |
148 | Maps all unmapped but prepared pages from the list. | 147 | Maps all unmapped but prepared pages from the list. |
149 | Updates metadata with newly allocated blocknumbers as needed */ | 148 | Updates metadata with newly allocated blocknumbers as needed */ |
150 | static int reiserfs_allocate_blocks_for_region( | 149 | static int reiserfs_allocate_blocks_for_region(struct reiserfs_transaction_handle *th, struct inode *inode, /* Inode we work with */ |
151 | struct reiserfs_transaction_handle *th, | 150 | loff_t pos, /* Writing position */ |
152 | struct inode *inode, /* Inode we work with */ | 151 | int num_pages, /* number of pages write going |
153 | loff_t pos, /* Writing position */ | 152 | to touch */ |
154 | int num_pages, /* number of pages write going | 153 | int write_bytes, /* amount of bytes to write */ |
155 | to touch */ | 154 | struct page **prepared_pages, /* array of |
156 | int write_bytes, /* amount of bytes to write */ | 155 | prepared pages |
157 | struct page **prepared_pages, /* array of | 156 | */ |
158 | prepared pages | 157 | int blocks_to_allocate /* Amount of blocks we |
159 | */ | 158 | need to allocate to |
160 | int blocks_to_allocate /* Amount of blocks we | 159 | fit the data into file |
161 | need to allocate to | 160 | */ |
162 | fit the data into file | 161 | ) |
163 | */ | ||
164 | ) | ||
165 | { | 162 | { |
166 | struct cpu_key key; // cpu key of item that we are going to deal with | 163 | struct cpu_key key; // cpu key of item that we are going to deal with |
167 | struct item_head *ih; // pointer to item head that we are going to deal with | 164 | struct item_head *ih; // pointer to item head that we are going to deal with |
168 | struct buffer_head *bh; // Buffer head that contains items that we are going to deal with | 165 | struct buffer_head *bh; // Buffer head that contains items that we are going to deal with |
169 | __le32 * item; // pointer to item we are going to deal with | 166 | __le32 *item; // pointer to item we are going to deal with |
170 | INITIALIZE_PATH(path); // path to item, that we are going to deal with. | 167 | INITIALIZE_PATH(path); // path to item, that we are going to deal with. |
171 | b_blocknr_t *allocated_blocks; // Pointer to a place where allocated blocknumbers would be stored. | 168 | b_blocknr_t *allocated_blocks; // Pointer to a place where allocated blocknumbers would be stored. |
172 | reiserfs_blocknr_hint_t hint; // hint structure for block allocator. | 169 | reiserfs_blocknr_hint_t hint; // hint structure for block allocator. |
173 | size_t res; // return value of various functions that we call. | 170 | size_t res; // return value of various functions that we call. |
174 | int curr_block; // current block used to keep track of unmapped blocks. | 171 | int curr_block; // current block used to keep track of unmapped blocks. |
175 | int i; // loop counter | 172 | int i; // loop counter |
176 | int itempos; // position in item | 173 | int itempos; // position in item |
177 | unsigned int from = (pos & (PAGE_CACHE_SIZE - 1)); // writing position in | 174 | unsigned int from = (pos & (PAGE_CACHE_SIZE - 1)); // writing position in |
178 | // first page | 175 | // first page |
179 | unsigned int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1; /* last modified byte offset in last page */ | 176 | unsigned int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1; /* last modified byte offset in last page */ |
180 | __u64 hole_size ; // amount of blocks for a file hole, if it needed to be created. | 177 | __u64 hole_size; // amount of blocks for a file hole, if it needed to be created. |
181 | int modifying_this_item = 0; // Flag for items traversal code to keep track | 178 | int modifying_this_item = 0; // Flag for items traversal code to keep track |
182 | // of the fact that we already prepared | 179 | // of the fact that we already prepared |
183 | // current block for journal | 180 | // current block for journal |
184 | int will_prealloc = 0; | 181 | int will_prealloc = 0; |
185 | RFALSE(!blocks_to_allocate, "green-9004: tried to allocate zero blocks?"); | 182 | RFALSE(!blocks_to_allocate, |
186 | 183 | "green-9004: tried to allocate zero blocks?"); | |
187 | /* only preallocate if this is a small write */ | 184 | |
188 | if (REISERFS_I(inode)->i_prealloc_count || | 185 | /* only preallocate if this is a small write */ |
189 | (!(write_bytes & (inode->i_sb->s_blocksize -1)) && | 186 | if (REISERFS_I(inode)->i_prealloc_count || |
190 | blocks_to_allocate < | 187 | (!(write_bytes & (inode->i_sb->s_blocksize - 1)) && |
191 | REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize)) | 188 | blocks_to_allocate < |
192 | will_prealloc = REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize; | 189 | REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize)) |
193 | 190 | will_prealloc = | |
194 | allocated_blocks = kmalloc((blocks_to_allocate + will_prealloc) * | 191 | REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize; |
195 | sizeof(b_blocknr_t), GFP_NOFS); | 192 | |
196 | 193 | allocated_blocks = kmalloc((blocks_to_allocate + will_prealloc) * | |
197 | /* First we compose a key to point at the writing position, we want to do | 194 | sizeof(b_blocknr_t), GFP_NOFS); |
198 | that outside of any locking region. */ | 195 | |
199 | make_cpu_key (&key, inode, pos+1, TYPE_ANY, 3/*key length*/); | 196 | /* First we compose a key to point at the writing position, we want to do |
200 | 197 | that outside of any locking region. */ | |
201 | /* If we came here, it means we absolutely need to open a transaction, | 198 | make_cpu_key(&key, inode, pos + 1, TYPE_ANY, 3 /*key length */ ); |
202 | since we need to allocate some blocks */ | 199 | |
203 | reiserfs_write_lock(inode->i_sb); // Journaling stuff and we need that. | 200 | /* If we came here, it means we absolutely need to open a transaction, |
204 | res = journal_begin(th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3 + 1 + 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb)); // Wish I know if this number enough | 201 | since we need to allocate some blocks */ |
205 | if (res) | 202 | reiserfs_write_lock(inode->i_sb); // Journaling stuff and we need that. |
206 | goto error_exit; | 203 | res = journal_begin(th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3 + 1 + 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb)); // Wish I know if this number enough |
207 | reiserfs_update_inode_transaction(inode) ; | 204 | if (res) |
208 | |||
209 | /* Look for the in-tree position of our write, need path for block allocator */ | ||
210 | res = search_for_position_by_key(inode->i_sb, &key, &path); | ||
211 | if ( res == IO_ERROR ) { | ||
212 | res = -EIO; | ||
213 | goto error_exit; | ||
214 | } | ||
215 | |||
216 | /* Allocate blocks */ | ||
217 | /* First fill in "hint" structure for block allocator */ | ||
218 | hint.th = th; // transaction handle. | ||
219 | hint.path = &path; // Path, so that block allocator can determine packing locality or whatever it needs to determine. | ||
220 | hint.inode = inode; // Inode is needed by block allocator too. | ||
221 | hint.search_start = 0; // We have no hint on where to search free blocks for block allocator. | ||
222 | hint.key = key.on_disk_key; // on disk key of file. | ||
223 | hint.block = inode->i_blocks>>(inode->i_sb->s_blocksize_bits-9); // Number of disk blocks this file occupies already. | ||
224 | hint.formatted_node = 0; // We are allocating blocks for unformatted node. | ||
225 | hint.preallocate = will_prealloc; | ||
226 | |||
227 | /* Call block allocator to allocate blocks */ | ||
228 | res = reiserfs_allocate_blocknrs(&hint, allocated_blocks, blocks_to_allocate, blocks_to_allocate); | ||
229 | if ( res != CARRY_ON ) { | ||
230 | if ( res == NO_DISK_SPACE ) { | ||
231 | /* We flush the transaction in case of no space. This way some | ||
232 | blocks might become free */ | ||
233 | SB_JOURNAL(inode->i_sb)->j_must_wait = 1; | ||
234 | res = restart_transaction(th, inode, &path); | ||
235 | if (res) | ||
236 | goto error_exit; | ||
237 | |||
238 | /* We might have scheduled, so search again */ | ||
239 | res = search_for_position_by_key(inode->i_sb, &key, &path); | ||
240 | if ( res == IO_ERROR ) { | ||
241 | res = -EIO; | ||
242 | goto error_exit; | 205 | goto error_exit; |
243 | } | 206 | reiserfs_update_inode_transaction(inode); |
244 | 207 | ||
245 | /* update changed info for hint structure. */ | 208 | /* Look for the in-tree position of our write, need path for block allocator */ |
246 | res = reiserfs_allocate_blocknrs(&hint, allocated_blocks, blocks_to_allocate, blocks_to_allocate); | 209 | res = search_for_position_by_key(inode->i_sb, &key, &path); |
247 | if ( res != CARRY_ON ) { | 210 | if (res == IO_ERROR) { |
248 | res = -ENOSPC; | 211 | res = -EIO; |
249 | pathrelse(&path); | ||
250 | goto error_exit; | 212 | goto error_exit; |
251 | } | ||
252 | } else { | ||
253 | res = -ENOSPC; | ||
254 | pathrelse(&path); | ||
255 | goto error_exit; | ||
256 | } | 213 | } |
257 | } | ||
258 | 214 | ||
259 | #ifdef __BIG_ENDIAN | 215 | /* Allocate blocks */ |
260 | // Too bad, I have not found any way to convert a given region from | 216 | /* First fill in "hint" structure for block allocator */ |
261 | // cpu format to little endian format | 217 | hint.th = th; // transaction handle. |
262 | { | 218 | hint.path = &path; // Path, so that block allocator can determine packing locality or whatever it needs to determine. |
263 | int i; | 219 | hint.inode = inode; // Inode is needed by block allocator too. |
264 | for ( i = 0; i < blocks_to_allocate ; i++) | 220 | hint.search_start = 0; // We have no hint on where to search free blocks for block allocator. |
265 | allocated_blocks[i]=cpu_to_le32(allocated_blocks[i]); | 221 | hint.key = key.on_disk_key; // on disk key of file. |
266 | } | 222 | hint.block = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); // Number of disk blocks this file occupies already. |
267 | #endif | 223 | hint.formatted_node = 0; // We are allocating blocks for unformatted node. |
268 | 224 | hint.preallocate = will_prealloc; | |
269 | /* Blocks allocating well might have scheduled and tree might have changed, | 225 | |
270 | let's search the tree again */ | 226 | /* Call block allocator to allocate blocks */ |
271 | /* find where in the tree our write should go */ | 227 | res = |
272 | res = search_for_position_by_key(inode->i_sb, &key, &path); | 228 | reiserfs_allocate_blocknrs(&hint, allocated_blocks, |
273 | if ( res == IO_ERROR ) { | 229 | blocks_to_allocate, blocks_to_allocate); |
274 | res = -EIO; | 230 | if (res != CARRY_ON) { |
275 | goto error_exit_free_blocks; | 231 | if (res == NO_DISK_SPACE) { |
276 | } | 232 | /* We flush the transaction in case of no space. This way some |
277 | 233 | blocks might become free */ | |
278 | bh = get_last_bh( &path ); // Get a bufferhead for last element in path. | 234 | SB_JOURNAL(inode->i_sb)->j_must_wait = 1; |
279 | ih = get_ih( &path ); // Get a pointer to last item head in path. | 235 | res = restart_transaction(th, inode, &path); |
280 | item = get_item( &path ); // Get a pointer to last item in path | 236 | if (res) |
281 | 237 | goto error_exit; | |
282 | /* Let's see what we have found */ | 238 | |
283 | if ( res != POSITION_FOUND ) { /* position not found, this means that we | 239 | /* We might have scheduled, so search again */ |
284 | might need to append file with holes | 240 | res = |
285 | first */ | 241 | search_for_position_by_key(inode->i_sb, &key, |
286 | // Since we are writing past the file's end, we need to find out if | 242 | &path); |
287 | // there is a hole that needs to be inserted before our writing | 243 | if (res == IO_ERROR) { |
288 | // position, and how many blocks it is going to cover (we need to | 244 | res = -EIO; |
289 | // populate pointers to file blocks representing the hole with zeros) | 245 | goto error_exit; |
246 | } | ||
290 | 247 | ||
248 | /* update changed info for hint structure. */ | ||
249 | res = | ||
250 | reiserfs_allocate_blocknrs(&hint, allocated_blocks, | ||
251 | blocks_to_allocate, | ||
252 | blocks_to_allocate); | ||
253 | if (res != CARRY_ON) { | ||
254 | res = -ENOSPC; | ||
255 | pathrelse(&path); | ||
256 | goto error_exit; | ||
257 | } | ||
258 | } else { | ||
259 | res = -ENOSPC; | ||
260 | pathrelse(&path); | ||
261 | goto error_exit; | ||
262 | } | ||
263 | } | ||
264 | #ifdef __BIG_ENDIAN | ||
265 | // Too bad, I have not found any way to convert a given region from | ||
266 | // cpu format to little endian format | ||
291 | { | 267 | { |
292 | int item_offset = 1; | 268 | int i; |
293 | /* | 269 | for (i = 0; i < blocks_to_allocate; i++) |
294 | * if ih is stat data, its offset is 0 and we don't want to | 270 | allocated_blocks[i] = cpu_to_le32(allocated_blocks[i]); |
295 | * add 1 to pos in the hole_size calculation | ||
296 | */ | ||
297 | if (is_statdata_le_ih(ih)) | ||
298 | item_offset = 0; | ||
299 | hole_size = (pos + item_offset - | ||
300 | (le_key_k_offset( get_inode_item_key_version(inode), | ||
301 | &(ih->ih_key)) + | ||
302 | op_bytes_number(ih, inode->i_sb->s_blocksize))) >> | ||
303 | inode->i_sb->s_blocksize_bits; | ||
304 | } | 271 | } |
272 | #endif | ||
305 | 273 | ||
306 | if ( hole_size > 0 ) { | 274 | /* Blocks allocating well might have scheduled and tree might have changed, |
307 | int to_paste = min_t(__u64, hole_size, MAX_ITEM_LEN(inode->i_sb->s_blocksize)/UNFM_P_SIZE ); // How much data to insert first time. | 275 | let's search the tree again */ |
308 | /* area filled with zeroes, to supply as list of zero blocknumbers | 276 | /* find where in the tree our write should go */ |
309 | We allocate it outside of loop just in case loop would spin for | 277 | res = search_for_position_by_key(inode->i_sb, &key, &path); |
310 | several iterations. */ | 278 | if (res == IO_ERROR) { |
311 | char *zeros = kmalloc(to_paste*UNFM_P_SIZE, GFP_ATOMIC); // We cannot insert more than MAX_ITEM_LEN bytes anyway. | 279 | res = -EIO; |
312 | if ( !zeros ) { | ||
313 | res = -ENOMEM; | ||
314 | goto error_exit_free_blocks; | 280 | goto error_exit_free_blocks; |
315 | } | 281 | } |
316 | memset ( zeros, 0, to_paste*UNFM_P_SIZE); | 282 | |
317 | do { | 283 | bh = get_last_bh(&path); // Get a bufferhead for last element in path. |
318 | to_paste = min_t(__u64, hole_size, MAX_ITEM_LEN(inode->i_sb->s_blocksize)/UNFM_P_SIZE ); | 284 | ih = get_ih(&path); // Get a pointer to last item head in path. |
319 | if ( is_indirect_le_ih(ih) ) { | 285 | item = get_item(&path); // Get a pointer to last item in path |
320 | /* Ok, there is existing indirect item already. Need to append it */ | 286 | |
321 | /* Calculate position past inserted item */ | 287 | /* Let's see what we have found */ |
322 | make_cpu_key( &key, inode, le_key_k_offset( get_inode_item_key_version(inode), &(ih->ih_key)) + op_bytes_number(ih, inode->i_sb->s_blocksize), TYPE_INDIRECT, 3); | 288 | if (res != POSITION_FOUND) { /* position not found, this means that we |
323 | res = reiserfs_paste_into_item( th, &path, &key, inode, (char *)zeros, UNFM_P_SIZE*to_paste); | 289 | might need to append file with holes |
324 | if ( res ) { | 290 | first */ |
325 | kfree(zeros); | 291 | // Since we are writing past the file's end, we need to find out if |
326 | goto error_exit_free_blocks; | 292 | // there is a hole that needs to be inserted before our writing |
327 | } | 293 | // position, and how many blocks it is going to cover (we need to |
328 | } else if ( is_statdata_le_ih(ih) ) { | 294 | // populate pointers to file blocks representing the hole with zeros) |
329 | /* No existing item, create it */ | 295 | |
330 | /* item head for new item */ | 296 | { |
331 | struct item_head ins_ih; | 297 | int item_offset = 1; |
332 | 298 | /* | |
333 | /* create a key for our new item */ | 299 | * if ih is stat data, its offset is 0 and we don't want to |
334 | make_cpu_key( &key, inode, 1, TYPE_INDIRECT, 3); | 300 | * add 1 to pos in the hole_size calculation |
335 | 301 | */ | |
336 | /* Create new item head for our new item */ | 302 | if (is_statdata_le_ih(ih)) |
337 | make_le_item_head (&ins_ih, &key, key.version, 1, | 303 | item_offset = 0; |
338 | TYPE_INDIRECT, to_paste*UNFM_P_SIZE, | 304 | hole_size = (pos + item_offset - |
339 | 0 /* free space */); | 305 | (le_key_k_offset |
340 | 306 | (get_inode_item_key_version(inode), | |
341 | /* Find where such item should live in the tree */ | 307 | &(ih->ih_key)) + op_bytes_number(ih, |
342 | res = search_item (inode->i_sb, &key, &path); | 308 | inode-> |
343 | if ( res != ITEM_NOT_FOUND ) { | 309 | i_sb-> |
344 | /* item should not exist, otherwise we have error */ | 310 | s_blocksize))) |
345 | if ( res != -ENOSPC ) { | 311 | >> inode->i_sb->s_blocksize_bits; |
346 | reiserfs_warning (inode->i_sb, | 312 | } |
347 | "green-9008: search_by_key (%K) returned %d", | 313 | |
348 | &key, res); | 314 | if (hole_size > 0) { |
315 | int to_paste = min_t(__u64, hole_size, MAX_ITEM_LEN(inode->i_sb->s_blocksize) / UNFM_P_SIZE); // How much data to insert first time. | ||
316 | /* area filled with zeroes, to supply as list of zero blocknumbers | ||
317 | We allocate it outside of loop just in case loop would spin for | ||
318 | several iterations. */ | ||
319 | char *zeros = kmalloc(to_paste * UNFM_P_SIZE, GFP_ATOMIC); // We cannot insert more than MAX_ITEM_LEN bytes anyway. | ||
320 | if (!zeros) { | ||
321 | res = -ENOMEM; | ||
322 | goto error_exit_free_blocks; | ||
349 | } | 323 | } |
350 | res = -EIO; | 324 | memset(zeros, 0, to_paste * UNFM_P_SIZE); |
351 | kfree(zeros); | 325 | do { |
352 | goto error_exit_free_blocks; | 326 | to_paste = |
353 | } | 327 | min_t(__u64, hole_size, |
354 | res = reiserfs_insert_item( th, &path, &key, &ins_ih, inode, (char *)zeros); | 328 | MAX_ITEM_LEN(inode->i_sb-> |
355 | } else { | 329 | s_blocksize) / |
356 | reiserfs_panic(inode->i_sb, "green-9011: Unexpected key type %K\n", &key); | 330 | UNFM_P_SIZE); |
331 | if (is_indirect_le_ih(ih)) { | ||
332 | /* Ok, there is existing indirect item already. Need to append it */ | ||
333 | /* Calculate position past inserted item */ | ||
334 | make_cpu_key(&key, inode, | ||
335 | le_key_k_offset | ||
336 | (get_inode_item_key_version | ||
337 | (inode), | ||
338 | &(ih->ih_key)) + | ||
339 | op_bytes_number(ih, | ||
340 | inode-> | ||
341 | i_sb-> | ||
342 | s_blocksize), | ||
343 | TYPE_INDIRECT, 3); | ||
344 | res = | ||
345 | reiserfs_paste_into_item(th, &path, | ||
346 | &key, | ||
347 | inode, | ||
348 | (char *) | ||
349 | zeros, | ||
350 | UNFM_P_SIZE | ||
351 | * | ||
352 | to_paste); | ||
353 | if (res) { | ||
354 | kfree(zeros); | ||
355 | goto error_exit_free_blocks; | ||
356 | } | ||
357 | } else if (is_statdata_le_ih(ih)) { | ||
358 | /* No existing item, create it */ | ||
359 | /* item head for new item */ | ||
360 | struct item_head ins_ih; | ||
361 | |||
362 | /* create a key for our new item */ | ||
363 | make_cpu_key(&key, inode, 1, | ||
364 | TYPE_INDIRECT, 3); | ||
365 | |||
366 | /* Create new item head for our new item */ | ||
367 | make_le_item_head(&ins_ih, &key, | ||
368 | key.version, 1, | ||
369 | TYPE_INDIRECT, | ||
370 | to_paste * | ||
371 | UNFM_P_SIZE, | ||
372 | 0 /* free space */ ); | ||
373 | |||
374 | /* Find where such item should live in the tree */ | ||
375 | res = | ||
376 | search_item(inode->i_sb, &key, | ||
377 | &path); | ||
378 | if (res != ITEM_NOT_FOUND) { | ||
379 | /* item should not exist, otherwise we have error */ | ||
380 | if (res != -ENOSPC) { | ||
381 | reiserfs_warning(inode-> | ||
382 | i_sb, | ||
383 | "green-9008: search_by_key (%K) returned %d", | ||
384 | &key, | ||
385 | res); | ||
386 | } | ||
387 | res = -EIO; | ||
388 | kfree(zeros); | ||
389 | goto error_exit_free_blocks; | ||
390 | } | ||
391 | res = | ||
392 | reiserfs_insert_item(th, &path, | ||
393 | &key, &ins_ih, | ||
394 | inode, | ||
395 | (char *)zeros); | ||
396 | } else { | ||
397 | reiserfs_panic(inode->i_sb, | ||
398 | "green-9011: Unexpected key type %K\n", | ||
399 | &key); | ||
400 | } | ||
401 | if (res) { | ||
402 | kfree(zeros); | ||
403 | goto error_exit_free_blocks; | ||
404 | } | ||
405 | /* Now we want to check if transaction is too full, and if it is | ||
406 | we restart it. This will also free the path. */ | ||
407 | if (journal_transaction_should_end | ||
408 | (th, th->t_blocks_allocated)) { | ||
409 | res = | ||
410 | restart_transaction(th, inode, | ||
411 | &path); | ||
412 | if (res) { | ||
413 | pathrelse(&path); | ||
414 | kfree(zeros); | ||
415 | goto error_exit; | ||
416 | } | ||
417 | } | ||
418 | |||
419 | /* Well, need to recalculate path and stuff */ | ||
420 | set_cpu_key_k_offset(&key, | ||
421 | cpu_key_k_offset(&key) + | ||
422 | (to_paste << inode-> | ||
423 | i_blkbits)); | ||
424 | res = | ||
425 | search_for_position_by_key(inode->i_sb, | ||
426 | &key, &path); | ||
427 | if (res == IO_ERROR) { | ||
428 | res = -EIO; | ||
429 | kfree(zeros); | ||
430 | goto error_exit_free_blocks; | ||
431 | } | ||
432 | bh = get_last_bh(&path); | ||
433 | ih = get_ih(&path); | ||
434 | item = get_item(&path); | ||
435 | hole_size -= to_paste; | ||
436 | } while (hole_size); | ||
437 | kfree(zeros); | ||
357 | } | 438 | } |
358 | if ( res ) { | 439 | } |
359 | kfree(zeros); | 440 | // Go through existing indirect items first |
360 | goto error_exit_free_blocks; | 441 | // replace all zeroes with blocknumbers from list |
442 | // Note that if no corresponding item was found, by previous search, | ||
443 | // it means there are no existing in-tree representation for file area | ||
444 | // we are going to overwrite, so there is nothing to scan through for holes. | ||
445 | for (curr_block = 0, itempos = path.pos_in_item; | ||
446 | curr_block < blocks_to_allocate && res == POSITION_FOUND;) { | ||
447 | retry: | ||
448 | |||
449 | if (itempos >= ih_item_len(ih) / UNFM_P_SIZE) { | ||
450 | /* We run out of data in this indirect item, let's look for another | ||
451 | one. */ | ||
452 | /* First if we are already modifying current item, log it */ | ||
453 | if (modifying_this_item) { | ||
454 | journal_mark_dirty(th, inode->i_sb, bh); | ||
455 | modifying_this_item = 0; | ||
456 | } | ||
457 | /* Then set the key to look for a new indirect item (offset of old | ||
458 | item is added to old item length */ | ||
459 | set_cpu_key_k_offset(&key, | ||
460 | le_key_k_offset | ||
461 | (get_inode_item_key_version(inode), | ||
462 | &(ih->ih_key)) + | ||
463 | op_bytes_number(ih, | ||
464 | inode->i_sb-> | ||
465 | s_blocksize)); | ||
466 | /* Search ofor position of new key in the tree. */ | ||
467 | res = | ||
468 | search_for_position_by_key(inode->i_sb, &key, | ||
469 | &path); | ||
470 | if (res == IO_ERROR) { | ||
471 | res = -EIO; | ||
472 | goto error_exit_free_blocks; | ||
473 | } | ||
474 | bh = get_last_bh(&path); | ||
475 | ih = get_ih(&path); | ||
476 | item = get_item(&path); | ||
477 | itempos = path.pos_in_item; | ||
478 | continue; // loop to check all kinds of conditions and so on. | ||
361 | } | 479 | } |
362 | /* Now we want to check if transaction is too full, and if it is | 480 | /* Ok, we have correct position in item now, so let's see if it is |
363 | we restart it. This will also free the path. */ | 481 | representing file hole (blocknumber is zero) and fill it if needed */ |
364 | if (journal_transaction_should_end(th, th->t_blocks_allocated)) { | 482 | if (!item[itempos]) { |
365 | res = restart_transaction(th, inode, &path); | 483 | /* Ok, a hole. Now we need to check if we already prepared this |
366 | if (res) { | 484 | block to be journaled */ |
367 | pathrelse (&path); | 485 | while (!modifying_this_item) { // loop until succeed |
368 | kfree(zeros); | 486 | /* Well, this item is not journaled yet, so we must prepare |
369 | goto error_exit; | 487 | it for journal first, before we can change it */ |
370 | } | 488 | struct item_head tmp_ih; // We copy item head of found item, |
371 | } | 489 | // here to detect if fs changed under |
372 | 490 | // us while we were preparing for | |
373 | /* Well, need to recalculate path and stuff */ | 491 | // journal. |
374 | set_cpu_key_k_offset( &key, cpu_key_k_offset(&key) + (to_paste << inode->i_blkbits)); | 492 | int fs_gen; // We store fs generation here to find if someone |
375 | res = search_for_position_by_key(inode->i_sb, &key, &path); | 493 | // changes fs under our feet |
376 | if ( res == IO_ERROR ) { | 494 | |
377 | res = -EIO; | 495 | copy_item_head(&tmp_ih, ih); // Remember itemhead |
378 | kfree(zeros); | 496 | fs_gen = get_generation(inode->i_sb); // remember fs generation |
379 | goto error_exit_free_blocks; | 497 | reiserfs_prepare_for_journal(inode->i_sb, bh, 1); // Prepare a buffer within which indirect item is stored for changing. |
498 | if (fs_changed(fs_gen, inode->i_sb) | ||
499 | && item_moved(&tmp_ih, &path)) { | ||
500 | // Sigh, fs was changed under us, we need to look for new | ||
501 | // location of item we are working with | ||
502 | |||
503 | /* unmark prepaerd area as journaled and search for it's | ||
504 | new position */ | ||
505 | reiserfs_restore_prepared_buffer(inode-> | ||
506 | i_sb, | ||
507 | bh); | ||
508 | res = | ||
509 | search_for_position_by_key(inode-> | ||
510 | i_sb, | ||
511 | &key, | ||
512 | &path); | ||
513 | if (res == IO_ERROR) { | ||
514 | res = -EIO; | ||
515 | goto error_exit_free_blocks; | ||
516 | } | ||
517 | bh = get_last_bh(&path); | ||
518 | ih = get_ih(&path); | ||
519 | item = get_item(&path); | ||
520 | itempos = path.pos_in_item; | ||
521 | goto retry; | ||
522 | } | ||
523 | modifying_this_item = 1; | ||
524 | } | ||
525 | item[itempos] = allocated_blocks[curr_block]; // Assign new block | ||
526 | curr_block++; | ||
380 | } | 527 | } |
381 | bh=get_last_bh(&path); | 528 | itempos++; |
382 | ih=get_ih(&path); | ||
383 | item = get_item(&path); | ||
384 | hole_size -= to_paste; | ||
385 | } while ( hole_size ); | ||
386 | kfree(zeros); | ||
387 | } | 529 | } |
388 | } | 530 | |
389 | 531 | if (modifying_this_item) { // We need to log last-accessed block, if it | |
390 | // Go through existing indirect items first | 532 | // was modified, but not logged yet. |
391 | // replace all zeroes with blocknumbers from list | 533 | journal_mark_dirty(th, inode->i_sb, bh); |
392 | // Note that if no corresponding item was found, by previous search, | ||
393 | // it means there are no existing in-tree representation for file area | ||
394 | // we are going to overwrite, so there is nothing to scan through for holes. | ||
395 | for ( curr_block = 0, itempos = path.pos_in_item ; curr_block < blocks_to_allocate && res == POSITION_FOUND ; ) { | ||
396 | retry: | ||
397 | |||
398 | if ( itempos >= ih_item_len(ih)/UNFM_P_SIZE ) { | ||
399 | /* We run out of data in this indirect item, let's look for another | ||
400 | one. */ | ||
401 | /* First if we are already modifying current item, log it */ | ||
402 | if ( modifying_this_item ) { | ||
403 | journal_mark_dirty (th, inode->i_sb, bh); | ||
404 | modifying_this_item = 0; | ||
405 | } | ||
406 | /* Then set the key to look for a new indirect item (offset of old | ||
407 | item is added to old item length */ | ||
408 | set_cpu_key_k_offset( &key, le_key_k_offset( get_inode_item_key_version(inode), &(ih->ih_key)) + op_bytes_number(ih, inode->i_sb->s_blocksize)); | ||
409 | /* Search ofor position of new key in the tree. */ | ||
410 | res = search_for_position_by_key(inode->i_sb, &key, &path); | ||
411 | if ( res == IO_ERROR) { | ||
412 | res = -EIO; | ||
413 | goto error_exit_free_blocks; | ||
414 | } | ||
415 | bh=get_last_bh(&path); | ||
416 | ih=get_ih(&path); | ||
417 | item = get_item(&path); | ||
418 | itempos = path.pos_in_item; | ||
419 | continue; // loop to check all kinds of conditions and so on. | ||
420 | } | 534 | } |
421 | /* Ok, we have correct position in item now, so let's see if it is | 535 | |
422 | representing file hole (blocknumber is zero) and fill it if needed */ | 536 | if (curr_block < blocks_to_allocate) { |
423 | if ( !item[itempos] ) { | 537 | // Oh, well need to append to indirect item, or to create indirect item |
424 | /* Ok, a hole. Now we need to check if we already prepared this | 538 | // if there weren't any |
425 | block to be journaled */ | 539 | if (is_indirect_le_ih(ih)) { |
426 | while ( !modifying_this_item ) { // loop until succeed | 540 | // Existing indirect item - append. First calculate key for append |
427 | /* Well, this item is not journaled yet, so we must prepare | 541 | // position. We do not need to recalculate path as it should |
428 | it for journal first, before we can change it */ | 542 | // already point to correct place. |
429 | struct item_head tmp_ih; // We copy item head of found item, | 543 | make_cpu_key(&key, inode, |
430 | // here to detect if fs changed under | 544 | le_key_k_offset(get_inode_item_key_version |
431 | // us while we were preparing for | 545 | (inode), |
432 | // journal. | 546 | &(ih->ih_key)) + |
433 | int fs_gen; // We store fs generation here to find if someone | 547 | op_bytes_number(ih, |
434 | // changes fs under our feet | 548 | inode->i_sb->s_blocksize), |
435 | 549 | TYPE_INDIRECT, 3); | |
436 | copy_item_head (&tmp_ih, ih); // Remember itemhead | 550 | res = |
437 | fs_gen = get_generation (inode->i_sb); // remember fs generation | 551 | reiserfs_paste_into_item(th, &path, &key, inode, |
438 | reiserfs_prepare_for_journal(inode->i_sb, bh, 1); // Prepare a buffer within which indirect item is stored for changing. | 552 | (char *)(allocated_blocks + |
439 | if (fs_changed (fs_gen, inode->i_sb) && item_moved (&tmp_ih, &path)) { | 553 | curr_block), |
440 | // Sigh, fs was changed under us, we need to look for new | 554 | UNFM_P_SIZE * |
441 | // location of item we are working with | 555 | (blocks_to_allocate - |
442 | 556 | curr_block)); | |
443 | /* unmark prepaerd area as journaled and search for it's | 557 | if (res) { |
444 | new position */ | 558 | goto error_exit_free_blocks; |
445 | reiserfs_restore_prepared_buffer(inode->i_sb, bh); | 559 | } |
446 | res = search_for_position_by_key(inode->i_sb, &key, &path); | 560 | } else if (is_statdata_le_ih(ih)) { |
447 | if ( res == IO_ERROR) { | 561 | // Last found item was statdata. That means we need to create indirect item. |
448 | res = -EIO; | 562 | struct item_head ins_ih; /* itemhead for new item */ |
449 | goto error_exit_free_blocks; | 563 | |
450 | } | 564 | /* create a key for our new item */ |
451 | bh=get_last_bh(&path); | 565 | make_cpu_key(&key, inode, 1, TYPE_INDIRECT, 3); // Position one, |
452 | ih=get_ih(&path); | 566 | // because that's |
453 | item = get_item(&path); | 567 | // where first |
454 | itempos = path.pos_in_item; | 568 | // indirect item |
455 | goto retry; | 569 | // begins |
570 | /* Create new item head for our new item */ | ||
571 | make_le_item_head(&ins_ih, &key, key.version, 1, | ||
572 | TYPE_INDIRECT, | ||
573 | (blocks_to_allocate - | ||
574 | curr_block) * UNFM_P_SIZE, | ||
575 | 0 /* free space */ ); | ||
576 | /* Find where such item should live in the tree */ | ||
577 | res = search_item(inode->i_sb, &key, &path); | ||
578 | if (res != ITEM_NOT_FOUND) { | ||
579 | /* Well, if we have found such item already, or some error | ||
580 | occured, we need to warn user and return error */ | ||
581 | if (res != -ENOSPC) { | ||
582 | reiserfs_warning(inode->i_sb, | ||
583 | "green-9009: search_by_key (%K) " | ||
584 | "returned %d", &key, | ||
585 | res); | ||
586 | } | ||
587 | res = -EIO; | ||
588 | goto error_exit_free_blocks; | ||
589 | } | ||
590 | /* Insert item into the tree with the data as its body */ | ||
591 | res = | ||
592 | reiserfs_insert_item(th, &path, &key, &ins_ih, | ||
593 | inode, | ||
594 | (char *)(allocated_blocks + | ||
595 | curr_block)); | ||
596 | } else { | ||
597 | reiserfs_panic(inode->i_sb, | ||
598 | "green-9010: unexpected item type for key %K\n", | ||
599 | &key); | ||
456 | } | 600 | } |
457 | modifying_this_item = 1; | ||
458 | } | ||
459 | item[itempos] = allocated_blocks[curr_block]; // Assign new block | ||
460 | curr_block++; | ||
461 | } | 601 | } |
462 | itempos++; | 602 | // the caller is responsible for closing the transaction |
463 | } | 603 | // unless we return an error, they are also responsible for logging |
464 | 604 | // the inode. | |
465 | if ( modifying_this_item ) { // We need to log last-accessed block, if it | 605 | // |
466 | // was modified, but not logged yet. | 606 | pathrelse(&path); |
467 | journal_mark_dirty (th, inode->i_sb, bh); | 607 | /* |
468 | } | 608 | * cleanup prellocation from previous writes |
469 | 609 | * if this is a partial block write | |
470 | if ( curr_block < blocks_to_allocate ) { | 610 | */ |
471 | // Oh, well need to append to indirect item, or to create indirect item | 611 | if (write_bytes & (inode->i_sb->s_blocksize - 1)) |
472 | // if there weren't any | 612 | reiserfs_discard_prealloc(th, inode); |
473 | if ( is_indirect_le_ih(ih) ) { | 613 | reiserfs_write_unlock(inode->i_sb); |
474 | // Existing indirect item - append. First calculate key for append | 614 | |
475 | // position. We do not need to recalculate path as it should | 615 | // go through all the pages/buffers and map the buffers to newly allocated |
476 | // already point to correct place. | 616 | // blocks (so that system knows where to write these pages later). |
477 | make_cpu_key( &key, inode, le_key_k_offset( get_inode_item_key_version(inode), &(ih->ih_key)) + op_bytes_number(ih, inode->i_sb->s_blocksize), TYPE_INDIRECT, 3); | 617 | curr_block = 0; |
478 | res = reiserfs_paste_into_item( th, &path, &key, inode, (char *)(allocated_blocks+curr_block), UNFM_P_SIZE*(blocks_to_allocate-curr_block)); | 618 | for (i = 0; i < num_pages; i++) { |
479 | if ( res ) { | 619 | struct page *page = prepared_pages[i]; //current page |
480 | goto error_exit_free_blocks; | 620 | struct buffer_head *head = page_buffers(page); // first buffer for a page |
481 | } | 621 | int block_start, block_end; // in-page offsets for buffers. |
482 | } else if (is_statdata_le_ih(ih) ) { | 622 | |
483 | // Last found item was statdata. That means we need to create indirect item. | 623 | if (!page_buffers(page)) |
484 | struct item_head ins_ih; /* itemhead for new item */ | 624 | reiserfs_panic(inode->i_sb, |
485 | 625 | "green-9005: No buffers for prepared page???"); | |
486 | /* create a key for our new item */ | 626 | |
487 | make_cpu_key( &key, inode, 1, TYPE_INDIRECT, 3); // Position one, | 627 | /* For each buffer in page */ |
488 | // because that's | 628 | for (bh = head, block_start = 0; bh != head || !block_start; |
489 | // where first | 629 | block_start = block_end, bh = bh->b_this_page) { |
490 | // indirect item | 630 | if (!bh) |
491 | // begins | 631 | reiserfs_panic(inode->i_sb, |
492 | /* Create new item head for our new item */ | 632 | "green-9006: Allocated but absent buffer for a page?"); |
493 | make_le_item_head (&ins_ih, &key, key.version, 1, TYPE_INDIRECT, | 633 | block_end = block_start + inode->i_sb->s_blocksize; |
494 | (blocks_to_allocate-curr_block)*UNFM_P_SIZE, | 634 | if (i == 0 && block_end <= from) |
495 | 0 /* free space */); | 635 | /* if this buffer is before requested data to map, skip it */ |
496 | /* Find where such item should live in the tree */ | 636 | continue; |
497 | res = search_item (inode->i_sb, &key, &path); | 637 | if (i == num_pages - 1 && block_start >= to) |
498 | if ( res != ITEM_NOT_FOUND ) { | 638 | /* If this buffer is after requested data to map, abort |
499 | /* Well, if we have found such item already, or some error | 639 | processing of current page */ |
500 | occured, we need to warn user and return error */ | 640 | break; |
501 | if ( res != -ENOSPC ) { | 641 | |
502 | reiserfs_warning (inode->i_sb, | 642 | if (!buffer_mapped(bh)) { // Ok, unmapped buffer, need to map it |
503 | "green-9009: search_by_key (%K) " | 643 | map_bh(bh, inode->i_sb, |
504 | "returned %d", &key, res); | 644 | le32_to_cpu(allocated_blocks |
645 | [curr_block])); | ||
646 | curr_block++; | ||
647 | set_buffer_new(bh); | ||
648 | } | ||
505 | } | 649 | } |
506 | res = -EIO; | ||
507 | goto error_exit_free_blocks; | ||
508 | } | ||
509 | /* Insert item into the tree with the data as its body */ | ||
510 | res = reiserfs_insert_item( th, &path, &key, &ins_ih, inode, (char *)(allocated_blocks+curr_block)); | ||
511 | } else { | ||
512 | reiserfs_panic(inode->i_sb, "green-9010: unexpected item type for key %K\n",&key); | ||
513 | } | ||
514 | } | ||
515 | |||
516 | // the caller is responsible for closing the transaction | ||
517 | // unless we return an error, they are also responsible for logging | ||
518 | // the inode. | ||
519 | // | ||
520 | pathrelse(&path); | ||
521 | /* | ||
522 | * cleanup prellocation from previous writes | ||
523 | * if this is a partial block write | ||
524 | */ | ||
525 | if (write_bytes & (inode->i_sb->s_blocksize -1)) | ||
526 | reiserfs_discard_prealloc(th, inode); | ||
527 | reiserfs_write_unlock(inode->i_sb); | ||
528 | |||
529 | // go through all the pages/buffers and map the buffers to newly allocated | ||
530 | // blocks (so that system knows where to write these pages later). | ||
531 | curr_block = 0; | ||
532 | for ( i = 0; i < num_pages ; i++ ) { | ||
533 | struct page *page=prepared_pages[i]; //current page | ||
534 | struct buffer_head *head = page_buffers(page);// first buffer for a page | ||
535 | int block_start, block_end; // in-page offsets for buffers. | ||
536 | |||
537 | if (!page_buffers(page)) | ||
538 | reiserfs_panic(inode->i_sb, "green-9005: No buffers for prepared page???"); | ||
539 | |||
540 | /* For each buffer in page */ | ||
541 | for(bh = head, block_start = 0; bh != head || !block_start; | ||
542 | block_start=block_end, bh = bh->b_this_page) { | ||
543 | if (!bh) | ||
544 | reiserfs_panic(inode->i_sb, "green-9006: Allocated but absent buffer for a page?"); | ||
545 | block_end = block_start+inode->i_sb->s_blocksize; | ||
546 | if (i == 0 && block_end <= from ) | ||
547 | /* if this buffer is before requested data to map, skip it */ | ||
548 | continue; | ||
549 | if (i == num_pages - 1 && block_start >= to) | ||
550 | /* If this buffer is after requested data to map, abort | ||
551 | processing of current page */ | ||
552 | break; | ||
553 | |||
554 | if ( !buffer_mapped(bh) ) { // Ok, unmapped buffer, need to map it | ||
555 | map_bh( bh, inode->i_sb, le32_to_cpu(allocated_blocks[curr_block])); | ||
556 | curr_block++; | ||
557 | set_buffer_new(bh); | ||
558 | } | ||
559 | } | 650 | } |
560 | } | ||
561 | 651 | ||
562 | RFALSE( curr_block > blocks_to_allocate, "green-9007: Used too many blocks? weird"); | 652 | RFALSE(curr_block > blocks_to_allocate, |
653 | "green-9007: Used too many blocks? weird"); | ||
563 | 654 | ||
564 | kfree(allocated_blocks); | 655 | kfree(allocated_blocks); |
565 | return 0; | 656 | return 0; |
566 | 657 | ||
567 | // Need to deal with transaction here. | 658 | // Need to deal with transaction here. |
568 | error_exit_free_blocks: | 659 | error_exit_free_blocks: |
569 | pathrelse(&path); | 660 | pathrelse(&path); |
570 | // free blocks | 661 | // free blocks |
571 | for( i = 0; i < blocks_to_allocate; i++ ) | 662 | for (i = 0; i < blocks_to_allocate; i++) |
572 | reiserfs_free_block(th, inode, le32_to_cpu(allocated_blocks[i]), 1); | 663 | reiserfs_free_block(th, inode, le32_to_cpu(allocated_blocks[i]), |
573 | 664 | 1); | |
574 | error_exit: | 665 | |
575 | if (th->t_trans_id) { | 666 | error_exit: |
576 | int err; | 667 | if (th->t_trans_id) { |
577 | // update any changes we made to blk count | 668 | int err; |
578 | reiserfs_update_sd(th, inode); | 669 | // update any changes we made to blk count |
579 | err = journal_end(th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3 + 1 + 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb)); | 670 | reiserfs_update_sd(th, inode); |
580 | if (err) | 671 | err = |
581 | res = err; | 672 | journal_end(th, inode->i_sb, |
582 | } | 673 | JOURNAL_PER_BALANCE_CNT * 3 + 1 + |
583 | reiserfs_write_unlock(inode->i_sb); | 674 | 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb)); |
584 | kfree(allocated_blocks); | 675 | if (err) |
585 | 676 | res = err; | |
586 | return res; | 677 | } |
678 | reiserfs_write_unlock(inode->i_sb); | ||
679 | kfree(allocated_blocks); | ||
680 | |||
681 | return res; | ||
587 | } | 682 | } |
588 | 683 | ||
589 | /* Unlock pages prepared by reiserfs_prepare_file_region_for_write */ | 684 | /* Unlock pages prepared by reiserfs_prepare_file_region_for_write */ |
590 | static void reiserfs_unprepare_pages(struct page **prepared_pages, /* list of locked pages */ | 685 | static void reiserfs_unprepare_pages(struct page **prepared_pages, /* list of locked pages */ |
591 | size_t num_pages /* amount of pages */) { | 686 | size_t num_pages /* amount of pages */ ) |
592 | int i; // loop counter | 687 | { |
688 | int i; // loop counter | ||
593 | 689 | ||
594 | for (i=0; i < num_pages ; i++) { | 690 | for (i = 0; i < num_pages; i++) { |
595 | struct page *page = prepared_pages[i]; | 691 | struct page *page = prepared_pages[i]; |
596 | 692 | ||
597 | try_to_free_buffers(page); | 693 | try_to_free_buffers(page); |
598 | unlock_page(page); | 694 | unlock_page(page); |
599 | page_cache_release(page); | 695 | page_cache_release(page); |
600 | } | 696 | } |
601 | } | 697 | } |
602 | 698 | ||
603 | /* This function will copy data from userspace to specified pages within | 699 | /* This function will copy data from userspace to specified pages within |
604 | supplied byte range */ | 700 | supplied byte range */ |
605 | static int reiserfs_copy_from_user_to_file_region( | 701 | static int reiserfs_copy_from_user_to_file_region(loff_t pos, /* In-file position */ |
606 | loff_t pos, /* In-file position */ | 702 | int num_pages, /* Number of pages affected */ |
607 | int num_pages, /* Number of pages affected */ | 703 | int write_bytes, /* Amount of bytes to write */ |
608 | int write_bytes, /* Amount of bytes to write */ | 704 | struct page **prepared_pages, /* pointer to |
609 | struct page **prepared_pages, /* pointer to | 705 | array to |
610 | array to | 706 | prepared pages |
611 | prepared pages | 707 | */ |
612 | */ | 708 | const char __user * buf /* Pointer to user-supplied |
613 | const char __user *buf /* Pointer to user-supplied | 709 | data */ |
614 | data*/ | 710 | ) |
615 | ) | ||
616 | { | 711 | { |
617 | long page_fault=0; // status of copy_from_user. | 712 | long page_fault = 0; // status of copy_from_user. |
618 | int i; // loop counter. | 713 | int i; // loop counter. |
619 | int offset; // offset in page | 714 | int offset; // offset in page |
620 | 715 | ||
621 | for ( i = 0, offset = (pos & (PAGE_CACHE_SIZE-1)); i < num_pages ; i++,offset=0) { | 716 | for (i = 0, offset = (pos & (PAGE_CACHE_SIZE - 1)); i < num_pages; |
622 | size_t count = min_t(size_t,PAGE_CACHE_SIZE-offset,write_bytes); // How much of bytes to write to this page | 717 | i++, offset = 0) { |
623 | struct page *page=prepared_pages[i]; // Current page we process. | 718 | size_t count = min_t(size_t, PAGE_CACHE_SIZE - offset, write_bytes); // How much of bytes to write to this page |
624 | 719 | struct page *page = prepared_pages[i]; // Current page we process. | |
625 | fault_in_pages_readable( buf, count); | 720 | |
626 | 721 | fault_in_pages_readable(buf, count); | |
627 | /* Copy data from userspace to the current page */ | 722 | |
628 | kmap(page); | 723 | /* Copy data from userspace to the current page */ |
629 | page_fault = __copy_from_user(page_address(page)+offset, buf, count); // Copy the data. | 724 | kmap(page); |
630 | /* Flush processor's dcache for this page */ | 725 | page_fault = __copy_from_user(page_address(page) + offset, buf, count); // Copy the data. |
631 | flush_dcache_page(page); | 726 | /* Flush processor's dcache for this page */ |
632 | kunmap(page); | 727 | flush_dcache_page(page); |
633 | buf+=count; | 728 | kunmap(page); |
634 | write_bytes-=count; | 729 | buf += count; |
635 | 730 | write_bytes -= count; | |
636 | if (page_fault) | 731 | |
637 | break; // Was there a fault? abort. | 732 | if (page_fault) |
638 | } | 733 | break; // Was there a fault? abort. |
639 | 734 | } | |
640 | return page_fault?-EFAULT:0; | 735 | |
736 | return page_fault ? -EFAULT : 0; | ||
641 | } | 737 | } |
642 | 738 | ||
643 | /* taken fs/buffer.c:__block_commit_write */ | 739 | /* taken fs/buffer.c:__block_commit_write */ |
644 | int reiserfs_commit_page(struct inode *inode, struct page *page, | 740 | int reiserfs_commit_page(struct inode *inode, struct page *page, |
645 | unsigned from, unsigned to) | 741 | unsigned from, unsigned to) |
646 | { | 742 | { |
647 | unsigned block_start, block_end; | 743 | unsigned block_start, block_end; |
648 | int partial = 0; | 744 | int partial = 0; |
649 | unsigned blocksize; | 745 | unsigned blocksize; |
650 | struct buffer_head *bh, *head; | 746 | struct buffer_head *bh, *head; |
651 | unsigned long i_size_index = inode->i_size >> PAGE_CACHE_SHIFT; | 747 | unsigned long i_size_index = inode->i_size >> PAGE_CACHE_SHIFT; |
652 | int new; | 748 | int new; |
653 | int logit = reiserfs_file_data_log(inode); | 749 | int logit = reiserfs_file_data_log(inode); |
654 | struct super_block *s = inode->i_sb; | 750 | struct super_block *s = inode->i_sb; |
655 | int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize; | 751 | int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize; |
656 | struct reiserfs_transaction_handle th; | 752 | struct reiserfs_transaction_handle th; |
657 | int ret = 0; | 753 | int ret = 0; |
658 | 754 | ||
659 | th.t_trans_id = 0; | 755 | th.t_trans_id = 0; |
660 | blocksize = 1 << inode->i_blkbits; | 756 | blocksize = 1 << inode->i_blkbits; |
661 | 757 | ||
662 | if (logit) { | 758 | if (logit) { |
663 | reiserfs_write_lock(s); | 759 | reiserfs_write_lock(s); |
664 | ret = journal_begin(&th, s, bh_per_page + 1); | 760 | ret = journal_begin(&th, s, bh_per_page + 1); |
665 | if (ret) | 761 | if (ret) |
666 | goto drop_write_lock; | 762 | goto drop_write_lock; |
667 | reiserfs_update_inode_transaction(inode); | 763 | reiserfs_update_inode_transaction(inode); |
668 | } | 764 | } |
669 | for(bh = head = page_buffers(page), block_start = 0; | 765 | for (bh = head = page_buffers(page), block_start = 0; |
670 | bh != head || !block_start; | 766 | bh != head || !block_start; |
671 | block_start=block_end, bh = bh->b_this_page) | 767 | block_start = block_end, bh = bh->b_this_page) { |
672 | { | 768 | |
673 | 769 | new = buffer_new(bh); | |
674 | new = buffer_new(bh); | 770 | clear_buffer_new(bh); |
675 | clear_buffer_new(bh); | 771 | block_end = block_start + blocksize; |
676 | block_end = block_start + blocksize; | 772 | if (block_end <= from || block_start >= to) { |
677 | if (block_end <= from || block_start >= to) { | 773 | if (!buffer_uptodate(bh)) |
678 | if (!buffer_uptodate(bh)) | 774 | partial = 1; |
679 | partial = 1; | 775 | } else { |
680 | } else { | 776 | set_buffer_uptodate(bh); |
681 | set_buffer_uptodate(bh); | 777 | if (logit) { |
682 | if (logit) { | 778 | reiserfs_prepare_for_journal(s, bh, 1); |
683 | reiserfs_prepare_for_journal(s, bh, 1); | 779 | journal_mark_dirty(&th, s, bh); |
684 | journal_mark_dirty(&th, s, bh); | 780 | } else if (!buffer_dirty(bh)) { |
685 | } else if (!buffer_dirty(bh)) { | 781 | mark_buffer_dirty(bh); |
686 | mark_buffer_dirty(bh); | 782 | /* do data=ordered on any page past the end |
687 | /* do data=ordered on any page past the end | 783 | * of file and any buffer marked BH_New. |
688 | * of file and any buffer marked BH_New. | 784 | */ |
689 | */ | 785 | if (reiserfs_data_ordered(inode->i_sb) && |
690 | if (reiserfs_data_ordered(inode->i_sb) && | 786 | (new || page->index >= i_size_index)) { |
691 | (new || page->index >= i_size_index)) { | 787 | reiserfs_add_ordered_list(inode, bh); |
692 | reiserfs_add_ordered_list(inode, bh); | 788 | } |
693 | } | 789 | } |
694 | } | 790 | } |
695 | } | 791 | } |
696 | } | 792 | if (logit) { |
697 | if (logit) { | 793 | ret = journal_end(&th, s, bh_per_page + 1); |
698 | ret = journal_end(&th, s, bh_per_page + 1); | 794 | drop_write_lock: |
699 | drop_write_lock: | 795 | reiserfs_write_unlock(s); |
700 | reiserfs_write_unlock(s); | 796 | } |
701 | } | 797 | /* |
702 | /* | 798 | * If this is a partial write which happened to make all buffers |
703 | * If this is a partial write which happened to make all buffers | 799 | * uptodate then we can optimize away a bogus readpage() for |
704 | * uptodate then we can optimize away a bogus readpage() for | 800 | * the next read(). Here we 'discover' whether the page went |
705 | * the next read(). Here we 'discover' whether the page went | 801 | * uptodate as a result of this (potentially partial) write. |
706 | * uptodate as a result of this (potentially partial) write. | 802 | */ |
707 | */ | 803 | if (!partial) |
708 | if (!partial) | 804 | SetPageUptodate(page); |
709 | SetPageUptodate(page); | 805 | return ret; |
710 | return ret; | ||
711 | } | 806 | } |
712 | 807 | ||
713 | |||
714 | /* Submit pages for write. This was separated from actual file copying | 808 | /* Submit pages for write. This was separated from actual file copying |
715 | because we might want to allocate block numbers in-between. | 809 | because we might want to allocate block numbers in-between. |
716 | This function assumes that caller will adjust file size to correct value. */ | 810 | This function assumes that caller will adjust file size to correct value. */ |
717 | static int reiserfs_submit_file_region_for_write( | 811 | static int reiserfs_submit_file_region_for_write(struct reiserfs_transaction_handle *th, struct inode *inode, loff_t pos, /* Writing position offset */ |
718 | struct reiserfs_transaction_handle *th, | 812 | size_t num_pages, /* Number of pages to write */ |
719 | struct inode *inode, | 813 | size_t write_bytes, /* number of bytes to write */ |
720 | loff_t pos, /* Writing position offset */ | 814 | struct page **prepared_pages /* list of pages */ |
721 | size_t num_pages, /* Number of pages to write */ | 815 | ) |
722 | size_t write_bytes, /* number of bytes to write */ | ||
723 | struct page **prepared_pages /* list of pages */ | ||
724 | ) | ||
725 | { | 816 | { |
726 | int status; // return status of block_commit_write. | 817 | int status; // return status of block_commit_write. |
727 | int retval = 0; // Return value we are going to return. | 818 | int retval = 0; // Return value we are going to return. |
728 | int i; // loop counter | 819 | int i; // loop counter |
729 | int offset; // Writing offset in page. | 820 | int offset; // Writing offset in page. |
730 | int orig_write_bytes = write_bytes; | 821 | int orig_write_bytes = write_bytes; |
731 | int sd_update = 0; | 822 | int sd_update = 0; |
732 | 823 | ||
733 | for ( i = 0, offset = (pos & (PAGE_CACHE_SIZE-1)); i < num_pages ; i++,offset=0) { | 824 | for (i = 0, offset = (pos & (PAGE_CACHE_SIZE - 1)); i < num_pages; |
734 | int count = min_t(int,PAGE_CACHE_SIZE-offset,write_bytes); // How much of bytes to write to this page | 825 | i++, offset = 0) { |
735 | struct page *page=prepared_pages[i]; // Current page we process. | 826 | int count = min_t(int, PAGE_CACHE_SIZE - offset, write_bytes); // How much of bytes to write to this page |
736 | 827 | struct page *page = prepared_pages[i]; // Current page we process. | |
737 | status = reiserfs_commit_page(inode, page, offset, offset+count); | 828 | |
738 | if ( status ) | 829 | status = |
739 | retval = status; // To not overcomplicate matters We are going to | 830 | reiserfs_commit_page(inode, page, offset, offset + count); |
740 | // submit all the pages even if there was error. | 831 | if (status) |
741 | // we only remember error status to report it on | 832 | retval = status; // To not overcomplicate matters We are going to |
742 | // exit. | 833 | // submit all the pages even if there was error. |
743 | write_bytes-=count; | 834 | // we only remember error status to report it on |
744 | } | 835 | // exit. |
745 | /* now that we've gotten all the ordered buffers marked dirty, | 836 | write_bytes -= count; |
746 | * we can safely update i_size and close any running transaction | 837 | } |
747 | */ | 838 | /* now that we've gotten all the ordered buffers marked dirty, |
748 | if ( pos + orig_write_bytes > inode->i_size) { | 839 | * we can safely update i_size and close any running transaction |
749 | inode->i_size = pos + orig_write_bytes; // Set new size | 840 | */ |
750 | /* If the file have grown so much that tail packing is no | 841 | if (pos + orig_write_bytes > inode->i_size) { |
751 | * longer possible, reset "need to pack" flag */ | 842 | inode->i_size = pos + orig_write_bytes; // Set new size |
752 | if ( (have_large_tails (inode->i_sb) && | 843 | /* If the file have grown so much that tail packing is no |
753 | inode->i_size > i_block_size (inode)*4) || | 844 | * longer possible, reset "need to pack" flag */ |
754 | (have_small_tails (inode->i_sb) && | 845 | if ((have_large_tails(inode->i_sb) && |
755 | inode->i_size > i_block_size(inode)) ) | 846 | inode->i_size > i_block_size(inode) * 4) || |
756 | REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask ; | 847 | (have_small_tails(inode->i_sb) && |
757 | else if ( (have_large_tails (inode->i_sb) && | 848 | inode->i_size > i_block_size(inode))) |
758 | inode->i_size < i_block_size (inode)*4) || | 849 | REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask; |
759 | (have_small_tails (inode->i_sb) && | 850 | else if ((have_large_tails(inode->i_sb) && |
760 | inode->i_size < i_block_size(inode)) ) | 851 | inode->i_size < i_block_size(inode) * 4) || |
761 | REISERFS_I(inode)->i_flags |= i_pack_on_close_mask ; | 852 | (have_small_tails(inode->i_sb) && |
762 | 853 | inode->i_size < i_block_size(inode))) | |
854 | REISERFS_I(inode)->i_flags |= i_pack_on_close_mask; | ||
855 | |||
856 | if (th->t_trans_id) { | ||
857 | reiserfs_write_lock(inode->i_sb); | ||
858 | reiserfs_update_sd(th, inode); // And update on-disk metadata | ||
859 | reiserfs_write_unlock(inode->i_sb); | ||
860 | } else | ||
861 | inode->i_sb->s_op->dirty_inode(inode); | ||
862 | |||
863 | sd_update = 1; | ||
864 | } | ||
763 | if (th->t_trans_id) { | 865 | if (th->t_trans_id) { |
764 | reiserfs_write_lock(inode->i_sb); | 866 | reiserfs_write_lock(inode->i_sb); |
765 | reiserfs_update_sd(th, inode); // And update on-disk metadata | 867 | if (!sd_update) |
766 | reiserfs_write_unlock(inode->i_sb); | 868 | reiserfs_update_sd(th, inode); |
767 | } else | 869 | status = journal_end(th, th->t_super, th->t_blocks_allocated); |
768 | inode->i_sb->s_op->dirty_inode(inode); | 870 | if (status) |
871 | retval = status; | ||
872 | reiserfs_write_unlock(inode->i_sb); | ||
873 | } | ||
874 | th->t_trans_id = 0; | ||
769 | 875 | ||
770 | sd_update = 1; | 876 | /* |
771 | } | 877 | * we have to unlock the pages after updating i_size, otherwise |
772 | if (th->t_trans_id) { | 878 | * we race with writepage |
773 | reiserfs_write_lock(inode->i_sb); | 879 | */ |
774 | if (!sd_update) | 880 | for (i = 0; i < num_pages; i++) { |
775 | reiserfs_update_sd(th, inode); | 881 | struct page *page = prepared_pages[i]; |
776 | status = journal_end(th, th->t_super, th->t_blocks_allocated); | 882 | unlock_page(page); |
777 | if (status) | 883 | mark_page_accessed(page); |
778 | retval = status; | 884 | page_cache_release(page); |
779 | reiserfs_write_unlock(inode->i_sb); | 885 | } |
780 | } | 886 | return retval; |
781 | th->t_trans_id = 0; | ||
782 | |||
783 | /* | ||
784 | * we have to unlock the pages after updating i_size, otherwise | ||
785 | * we race with writepage | ||
786 | */ | ||
787 | for ( i = 0; i < num_pages ; i++) { | ||
788 | struct page *page=prepared_pages[i]; | ||
789 | unlock_page(page); | ||
790 | mark_page_accessed(page); | ||
791 | page_cache_release(page); | ||
792 | } | ||
793 | return retval; | ||
794 | } | 887 | } |
795 | 888 | ||
796 | /* Look if passed writing region is going to touch file's tail | 889 | /* Look if passed writing region is going to touch file's tail |
797 | (if it is present). And if it is, convert the tail to unformatted node */ | 890 | (if it is present). And if it is, convert the tail to unformatted node */ |
798 | static int reiserfs_check_for_tail_and_convert( struct inode *inode, /* inode to deal with */ | 891 | static int reiserfs_check_for_tail_and_convert(struct inode *inode, /* inode to deal with */ |
799 | loff_t pos, /* Writing position */ | 892 | loff_t pos, /* Writing position */ |
800 | int write_bytes /* amount of bytes to write */ | 893 | int write_bytes /* amount of bytes to write */ |
801 | ) | 894 | ) |
802 | { | 895 | { |
803 | INITIALIZE_PATH(path); // needed for search_for_position | 896 | INITIALIZE_PATH(path); // needed for search_for_position |
804 | struct cpu_key key; // Key that would represent last touched writing byte. | 897 | struct cpu_key key; // Key that would represent last touched writing byte. |
805 | struct item_head *ih; // item header of found block; | 898 | struct item_head *ih; // item header of found block; |
806 | int res; // Return value of various functions we call. | 899 | int res; // Return value of various functions we call. |
807 | int cont_expand_offset; // We will put offset for generic_cont_expand here | 900 | int cont_expand_offset; // We will put offset for generic_cont_expand here |
808 | // This can be int just because tails are created | 901 | // This can be int just because tails are created |
809 | // only for small files. | 902 | // only for small files. |
810 | 903 | ||
811 | /* this embodies a dependency on a particular tail policy */ | 904 | /* this embodies a dependency on a particular tail policy */ |
812 | if ( inode->i_size >= inode->i_sb->s_blocksize*4 ) { | 905 | if (inode->i_size >= inode->i_sb->s_blocksize * 4) { |
813 | /* such a big files do not have tails, so we won't bother ourselves | 906 | /* such a big files do not have tails, so we won't bother ourselves |
814 | to look for tails, simply return */ | 907 | to look for tails, simply return */ |
815 | return 0; | 908 | return 0; |
816 | } | 909 | } |
817 | |||
818 | reiserfs_write_lock(inode->i_sb); | ||
819 | /* find the item containing the last byte to be written, or if | ||
820 | * writing past the end of the file then the last item of the | ||
821 | * file (and then we check its type). */ | ||
822 | make_cpu_key (&key, inode, pos+write_bytes+1, TYPE_ANY, 3/*key length*/); | ||
823 | res = search_for_position_by_key(inode->i_sb, &key, &path); | ||
824 | if ( res == IO_ERROR ) { | ||
825 | reiserfs_write_unlock(inode->i_sb); | ||
826 | return -EIO; | ||
827 | } | ||
828 | ih = get_ih(&path); | ||
829 | res = 0; | ||
830 | if ( is_direct_le_ih(ih) ) { | ||
831 | /* Ok, closest item is file tail (tails are stored in "direct" | ||
832 | * items), so we need to unpack it. */ | ||
833 | /* To not overcomplicate matters, we just call generic_cont_expand | ||
834 | which will in turn call other stuff and finally will boil down to | ||
835 | reiserfs_get_block() that would do necessary conversion. */ | ||
836 | cont_expand_offset = le_key_k_offset(get_inode_item_key_version(inode), &(ih->ih_key)); | ||
837 | pathrelse(&path); | ||
838 | res = generic_cont_expand( inode, cont_expand_offset); | ||
839 | } else | ||
840 | pathrelse(&path); | ||
841 | 910 | ||
842 | reiserfs_write_unlock(inode->i_sb); | 911 | reiserfs_write_lock(inode->i_sb); |
843 | return res; | 912 | /* find the item containing the last byte to be written, or if |
913 | * writing past the end of the file then the last item of the | ||
914 | * file (and then we check its type). */ | ||
915 | make_cpu_key(&key, inode, pos + write_bytes + 1, TYPE_ANY, | ||
916 | 3 /*key length */ ); | ||
917 | res = search_for_position_by_key(inode->i_sb, &key, &path); | ||
918 | if (res == IO_ERROR) { | ||
919 | reiserfs_write_unlock(inode->i_sb); | ||
920 | return -EIO; | ||
921 | } | ||
922 | ih = get_ih(&path); | ||
923 | res = 0; | ||
924 | if (is_direct_le_ih(ih)) { | ||
925 | /* Ok, closest item is file tail (tails are stored in "direct" | ||
926 | * items), so we need to unpack it. */ | ||
927 | /* To not overcomplicate matters, we just call generic_cont_expand | ||
928 | which will in turn call other stuff and finally will boil down to | ||
929 | reiserfs_get_block() that would do necessary conversion. */ | ||
930 | cont_expand_offset = | ||
931 | le_key_k_offset(get_inode_item_key_version(inode), | ||
932 | &(ih->ih_key)); | ||
933 | pathrelse(&path); | ||
934 | res = generic_cont_expand(inode, cont_expand_offset); | ||
935 | } else | ||
936 | pathrelse(&path); | ||
937 | |||
938 | reiserfs_write_unlock(inode->i_sb); | ||
939 | return res; | ||
844 | } | 940 | } |
845 | 941 | ||
846 | /* This function locks pages starting from @pos for @inode. | 942 | /* This function locks pages starting from @pos for @inode. |
@@ -851,275 +947,296 @@ static int reiserfs_check_for_tail_and_convert( struct inode *inode, /* inode to | |||
851 | append), it is zeroed, then. | 947 | append), it is zeroed, then. |
852 | Returns number of unallocated blocks that should be allocated to cover | 948 | Returns number of unallocated blocks that should be allocated to cover |
853 | new file data.*/ | 949 | new file data.*/ |
854 | static int reiserfs_prepare_file_region_for_write( | 950 | static int reiserfs_prepare_file_region_for_write(struct inode *inode |
855 | struct inode *inode /* Inode of the file */, | 951 | /* Inode of the file */ , |
856 | loff_t pos, /* position in the file */ | 952 | loff_t pos, /* position in the file */ |
857 | size_t num_pages, /* number of pages to | 953 | size_t num_pages, /* number of pages to |
858 | prepare */ | 954 | prepare */ |
859 | size_t write_bytes, /* Amount of bytes to be | 955 | size_t write_bytes, /* Amount of bytes to be |
860 | overwritten from | 956 | overwritten from |
861 | @pos */ | 957 | @pos */ |
862 | struct page **prepared_pages /* pointer to array | 958 | struct page **prepared_pages /* pointer to array |
863 | where to store | 959 | where to store |
864 | prepared pages */ | 960 | prepared pages */ |
865 | ) | 961 | ) |
866 | { | 962 | { |
867 | int res=0; // Return values of different functions we call. | 963 | int res = 0; // Return values of different functions we call. |
868 | unsigned long index = pos >> PAGE_CACHE_SHIFT; // Offset in file in pages. | 964 | unsigned long index = pos >> PAGE_CACHE_SHIFT; // Offset in file in pages. |
869 | int from = (pos & (PAGE_CACHE_SIZE - 1)); // Writing offset in first page | 965 | int from = (pos & (PAGE_CACHE_SIZE - 1)); // Writing offset in first page |
870 | int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1; | 966 | int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1; |
871 | /* offset of last modified byte in last | 967 | /* offset of last modified byte in last |
872 | page */ | 968 | page */ |
873 | struct address_space *mapping = inode->i_mapping; // Pages are mapped here. | 969 | struct address_space *mapping = inode->i_mapping; // Pages are mapped here. |
874 | int i; // Simple counter | 970 | int i; // Simple counter |
875 | int blocks = 0; /* Return value (blocks that should be allocated) */ | 971 | int blocks = 0; /* Return value (blocks that should be allocated) */ |
876 | struct buffer_head *bh, *head; // Current bufferhead and first bufferhead | 972 | struct buffer_head *bh, *head; // Current bufferhead and first bufferhead |
877 | // of a page. | 973 | // of a page. |
878 | unsigned block_start, block_end; // Starting and ending offsets of current | 974 | unsigned block_start, block_end; // Starting and ending offsets of current |
879 | // buffer in the page. | 975 | // buffer in the page. |
880 | struct buffer_head *wait[2], **wait_bh=wait; // Buffers for page, if | 976 | struct buffer_head *wait[2], **wait_bh = wait; // Buffers for page, if |
881 | // Page appeared to be not up | 977 | // Page appeared to be not up |
882 | // to date. Note how we have | 978 | // to date. Note how we have |
883 | // at most 2 buffers, this is | 979 | // at most 2 buffers, this is |
884 | // because we at most may | 980 | // because we at most may |
885 | // partially overwrite two | 981 | // partially overwrite two |
886 | // buffers for one page. One at // the beginning of write area | 982 | // buffers for one page. One at // the beginning of write area |
887 | // and one at the end. | 983 | // and one at the end. |
888 | // Everything inthe middle gets // overwritten totally. | 984 | // Everything inthe middle gets // overwritten totally. |
889 | 985 | ||
890 | struct cpu_key key; // cpu key of item that we are going to deal with | 986 | struct cpu_key key; // cpu key of item that we are going to deal with |
891 | struct item_head *ih = NULL; // pointer to item head that we are going to deal with | 987 | struct item_head *ih = NULL; // pointer to item head that we are going to deal with |
892 | struct buffer_head *itembuf=NULL; // Buffer head that contains items that we are going to deal with | 988 | struct buffer_head *itembuf = NULL; // Buffer head that contains items that we are going to deal with |
893 | INITIALIZE_PATH(path); // path to item, that we are going to deal with. | 989 | INITIALIZE_PATH(path); // path to item, that we are going to deal with. |
894 | __le32 * item=NULL; // pointer to item we are going to deal with | 990 | __le32 *item = NULL; // pointer to item we are going to deal with |
895 | int item_pos=-1; /* Position in indirect item */ | 991 | int item_pos = -1; /* Position in indirect item */ |
896 | 992 | ||
897 | 993 | if (num_pages < 1) { | |
898 | if ( num_pages < 1 ) { | 994 | reiserfs_warning(inode->i_sb, |
899 | reiserfs_warning (inode->i_sb, | 995 | "green-9001: reiserfs_prepare_file_region_for_write " |
900 | "green-9001: reiserfs_prepare_file_region_for_write " | 996 | "called with zero number of pages to process"); |
901 | "called with zero number of pages to process"); | 997 | return -EFAULT; |
902 | return -EFAULT; | ||
903 | } | ||
904 | |||
905 | /* We have 2 loops for pages. In first loop we grab and lock the pages, so | ||
906 | that nobody would touch these until we release the pages. Then | ||
907 | we'd start to deal with mapping buffers to blocks. */ | ||
908 | for ( i = 0; i < num_pages; i++) { | ||
909 | prepared_pages[i] = grab_cache_page(mapping, index + i); // locks the page | ||
910 | if ( !prepared_pages[i]) { | ||
911 | res = -ENOMEM; | ||
912 | goto failed_page_grabbing; | ||
913 | } | ||
914 | if (!page_has_buffers(prepared_pages[i])) | ||
915 | create_empty_buffers(prepared_pages[i], inode->i_sb->s_blocksize, 0); | ||
916 | } | ||
917 | |||
918 | /* Let's count amount of blocks for a case where all the blocks | ||
919 | overwritten are new (we will substract already allocated blocks later)*/ | ||
920 | if ( num_pages > 2 ) | ||
921 | /* These are full-overwritten pages so we count all the blocks in | ||
922 | these pages are counted as needed to be allocated */ | ||
923 | blocks = (num_pages - 2) << (PAGE_CACHE_SHIFT - inode->i_blkbits); | ||
924 | |||
925 | /* count blocks needed for first page (possibly partially written) */ | ||
926 | blocks += ((PAGE_CACHE_SIZE - from) >> inode->i_blkbits) + | ||
927 | !!(from & (inode->i_sb->s_blocksize-1)); /* roundup */ | ||
928 | |||
929 | /* Now we account for last page. If last page == first page (we | ||
930 | overwrite only one page), we substract all the blocks past the | ||
931 | last writing position in a page out of already calculated number | ||
932 | of blocks */ | ||
933 | blocks += ((num_pages > 1) << (PAGE_CACHE_SHIFT-inode->i_blkbits)) - | ||
934 | ((PAGE_CACHE_SIZE - to) >> inode->i_blkbits); | ||
935 | /* Note how we do not roundup here since partial blocks still | ||
936 | should be allocated */ | ||
937 | |||
938 | /* Now if all the write area lies past the file end, no point in | ||
939 | maping blocks, since there is none, so we just zero out remaining | ||
940 | parts of first and last pages in write area (if needed) */ | ||
941 | if ( (pos & ~((loff_t)PAGE_CACHE_SIZE - 1)) > inode->i_size ) { | ||
942 | if ( from != 0 ) {/* First page needs to be partially zeroed */ | ||
943 | char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0); | ||
944 | memset(kaddr, 0, from); | ||
945 | kunmap_atomic( kaddr, KM_USER0); | ||
946 | } | ||
947 | if ( to != PAGE_CACHE_SIZE ) { /* Last page needs to be partially zeroed */ | ||
948 | char *kaddr = kmap_atomic(prepared_pages[num_pages-1], KM_USER0); | ||
949 | memset(kaddr+to, 0, PAGE_CACHE_SIZE - to); | ||
950 | kunmap_atomic( kaddr, KM_USER0); | ||
951 | } | 998 | } |
952 | 999 | ||
953 | /* Since all blocks are new - use already calculated value */ | 1000 | /* We have 2 loops for pages. In first loop we grab and lock the pages, so |
954 | return blocks; | 1001 | that nobody would touch these until we release the pages. Then |
955 | } | 1002 | we'd start to deal with mapping buffers to blocks. */ |
956 | 1003 | for (i = 0; i < num_pages; i++) { | |
957 | /* Well, since we write somewhere into the middle of a file, there is | 1004 | prepared_pages[i] = grab_cache_page(mapping, index + i); // locks the page |
958 | possibility we are writing over some already allocated blocks, so | 1005 | if (!prepared_pages[i]) { |
959 | let's map these blocks and substract number of such blocks out of blocks | 1006 | res = -ENOMEM; |
960 | we need to allocate (calculated above) */ | 1007 | goto failed_page_grabbing; |
961 | /* Mask write position to start on blocksize, we do it out of the | ||
962 | loop for performance reasons */ | ||
963 | pos &= ~((loff_t) inode->i_sb->s_blocksize - 1); | ||
964 | /* Set cpu key to the starting position in a file (on left block boundary)*/ | ||
965 | make_cpu_key (&key, inode, 1 + ((pos) & ~((loff_t) inode->i_sb->s_blocksize - 1)), TYPE_ANY, 3/*key length*/); | ||
966 | |||
967 | reiserfs_write_lock(inode->i_sb); // We need that for at least search_by_key() | ||
968 | for ( i = 0; i < num_pages ; i++ ) { | ||
969 | |||
970 | head = page_buffers(prepared_pages[i]); | ||
971 | /* For each buffer in the page */ | ||
972 | for(bh = head, block_start = 0; bh != head || !block_start; | ||
973 | block_start=block_end, bh = bh->b_this_page) { | ||
974 | if (!bh) | ||
975 | reiserfs_panic(inode->i_sb, "green-9002: Allocated but absent buffer for a page?"); | ||
976 | /* Find where this buffer ends */ | ||
977 | block_end = block_start+inode->i_sb->s_blocksize; | ||
978 | if (i == 0 && block_end <= from ) | ||
979 | /* if this buffer is before requested data to map, skip it*/ | ||
980 | continue; | ||
981 | |||
982 | if (i == num_pages - 1 && block_start >= to) { | ||
983 | /* If this buffer is after requested data to map, abort | ||
984 | processing of current page */ | ||
985 | break; | ||
986 | } | 1008 | } |
1009 | if (!page_has_buffers(prepared_pages[i])) | ||
1010 | create_empty_buffers(prepared_pages[i], | ||
1011 | inode->i_sb->s_blocksize, 0); | ||
1012 | } | ||
987 | 1013 | ||
988 | if ( buffer_mapped(bh) && bh->b_blocknr !=0 ) { | 1014 | /* Let's count amount of blocks for a case where all the blocks |
989 | /* This is optimisation for a case where buffer is mapped | 1015 | overwritten are new (we will substract already allocated blocks later) */ |
990 | and have blocknumber assigned. In case significant amount | 1016 | if (num_pages > 2) |
991 | of such buffers are present, we may avoid some amount | 1017 | /* These are full-overwritten pages so we count all the blocks in |
992 | of search_by_key calls. | 1018 | these pages are counted as needed to be allocated */ |
993 | Probably it would be possible to move parts of this code | 1019 | blocks = |
994 | out of BKL, but I afraid that would overcomplicate code | 1020 | (num_pages - 2) << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
995 | without any noticeable benefit. | 1021 | |
996 | */ | 1022 | /* count blocks needed for first page (possibly partially written) */ |
997 | item_pos++; | 1023 | blocks += ((PAGE_CACHE_SIZE - from) >> inode->i_blkbits) + !!(from & (inode->i_sb->s_blocksize - 1)); /* roundup */ |
998 | /* Update the key */ | 1024 | |
999 | set_cpu_key_k_offset( &key, cpu_key_k_offset(&key) + inode->i_sb->s_blocksize); | 1025 | /* Now we account for last page. If last page == first page (we |
1000 | blocks--; // Decrease the amount of blocks that need to be | 1026 | overwrite only one page), we substract all the blocks past the |
1001 | // allocated | 1027 | last writing position in a page out of already calculated number |
1002 | continue; // Go to the next buffer | 1028 | of blocks */ |
1029 | blocks += ((num_pages > 1) << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - | ||
1030 | ((PAGE_CACHE_SIZE - to) >> inode->i_blkbits); | ||
1031 | /* Note how we do not roundup here since partial blocks still | ||
1032 | should be allocated */ | ||
1033 | |||
1034 | /* Now if all the write area lies past the file end, no point in | ||
1035 | maping blocks, since there is none, so we just zero out remaining | ||
1036 | parts of first and last pages in write area (if needed) */ | ||
1037 | if ((pos & ~((loff_t) PAGE_CACHE_SIZE - 1)) > inode->i_size) { | ||
1038 | if (from != 0) { /* First page needs to be partially zeroed */ | ||
1039 | char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0); | ||
1040 | memset(kaddr, 0, from); | ||
1041 | kunmap_atomic(kaddr, KM_USER0); | ||
1042 | } | ||
1043 | if (to != PAGE_CACHE_SIZE) { /* Last page needs to be partially zeroed */ | ||
1044 | char *kaddr = | ||
1045 | kmap_atomic(prepared_pages[num_pages - 1], | ||
1046 | KM_USER0); | ||
1047 | memset(kaddr + to, 0, PAGE_CACHE_SIZE - to); | ||
1048 | kunmap_atomic(kaddr, KM_USER0); | ||
1003 | } | 1049 | } |
1004 | 1050 | ||
1005 | if ( !itembuf || /* if first iteration */ | 1051 | /* Since all blocks are new - use already calculated value */ |
1006 | item_pos >= ih_item_len(ih)/UNFM_P_SIZE) | 1052 | return blocks; |
1007 | { /* or if we progressed past the | 1053 | } |
1008 | current unformatted_item */ | 1054 | |
1009 | /* Try to find next item */ | 1055 | /* Well, since we write somewhere into the middle of a file, there is |
1010 | res = search_for_position_by_key(inode->i_sb, &key, &path); | 1056 | possibility we are writing over some already allocated blocks, so |
1011 | /* Abort if no more items */ | 1057 | let's map these blocks and substract number of such blocks out of blocks |
1012 | if ( res != POSITION_FOUND ) { | 1058 | we need to allocate (calculated above) */ |
1013 | /* make sure later loops don't use this item */ | 1059 | /* Mask write position to start on blocksize, we do it out of the |
1014 | itembuf = NULL; | 1060 | loop for performance reasons */ |
1015 | item = NULL; | 1061 | pos &= ~((loff_t) inode->i_sb->s_blocksize - 1); |
1016 | break; | 1062 | /* Set cpu key to the starting position in a file (on left block boundary) */ |
1063 | make_cpu_key(&key, inode, | ||
1064 | 1 + ((pos) & ~((loff_t) inode->i_sb->s_blocksize - 1)), | ||
1065 | TYPE_ANY, 3 /*key length */ ); | ||
1066 | |||
1067 | reiserfs_write_lock(inode->i_sb); // We need that for at least search_by_key() | ||
1068 | for (i = 0; i < num_pages; i++) { | ||
1069 | |||
1070 | head = page_buffers(prepared_pages[i]); | ||
1071 | /* For each buffer in the page */ | ||
1072 | for (bh = head, block_start = 0; bh != head || !block_start; | ||
1073 | block_start = block_end, bh = bh->b_this_page) { | ||
1074 | if (!bh) | ||
1075 | reiserfs_panic(inode->i_sb, | ||
1076 | "green-9002: Allocated but absent buffer for a page?"); | ||
1077 | /* Find where this buffer ends */ | ||
1078 | block_end = block_start + inode->i_sb->s_blocksize; | ||
1079 | if (i == 0 && block_end <= from) | ||
1080 | /* if this buffer is before requested data to map, skip it */ | ||
1081 | continue; | ||
1082 | |||
1083 | if (i == num_pages - 1 && block_start >= to) { | ||
1084 | /* If this buffer is after requested data to map, abort | ||
1085 | processing of current page */ | ||
1086 | break; | ||
1017 | } | 1087 | } |
1018 | 1088 | ||
1019 | /* Update information about current indirect item */ | 1089 | if (buffer_mapped(bh) && bh->b_blocknr != 0) { |
1020 | itembuf = get_last_bh( &path ); | 1090 | /* This is optimisation for a case where buffer is mapped |
1021 | ih = get_ih( &path ); | 1091 | and have blocknumber assigned. In case significant amount |
1022 | item = get_item( &path ); | 1092 | of such buffers are present, we may avoid some amount |
1023 | item_pos = path.pos_in_item; | 1093 | of search_by_key calls. |
1094 | Probably it would be possible to move parts of this code | ||
1095 | out of BKL, but I afraid that would overcomplicate code | ||
1096 | without any noticeable benefit. | ||
1097 | */ | ||
1098 | item_pos++; | ||
1099 | /* Update the key */ | ||
1100 | set_cpu_key_k_offset(&key, | ||
1101 | cpu_key_k_offset(&key) + | ||
1102 | inode->i_sb->s_blocksize); | ||
1103 | blocks--; // Decrease the amount of blocks that need to be | ||
1104 | // allocated | ||
1105 | continue; // Go to the next buffer | ||
1106 | } | ||
1024 | 1107 | ||
1025 | RFALSE( !is_indirect_le_ih (ih), "green-9003: indirect item expected"); | 1108 | if (!itembuf || /* if first iteration */ |
1026 | } | 1109 | item_pos >= ih_item_len(ih) / UNFM_P_SIZE) { /* or if we progressed past the |
1110 | current unformatted_item */ | ||
1111 | /* Try to find next item */ | ||
1112 | res = | ||
1113 | search_for_position_by_key(inode->i_sb, | ||
1114 | &key, &path); | ||
1115 | /* Abort if no more items */ | ||
1116 | if (res != POSITION_FOUND) { | ||
1117 | /* make sure later loops don't use this item */ | ||
1118 | itembuf = NULL; | ||
1119 | item = NULL; | ||
1120 | break; | ||
1121 | } | ||
1122 | |||
1123 | /* Update information about current indirect item */ | ||
1124 | itembuf = get_last_bh(&path); | ||
1125 | ih = get_ih(&path); | ||
1126 | item = get_item(&path); | ||
1127 | item_pos = path.pos_in_item; | ||
1128 | |||
1129 | RFALSE(!is_indirect_le_ih(ih), | ||
1130 | "green-9003: indirect item expected"); | ||
1131 | } | ||
1027 | 1132 | ||
1028 | /* See if there is some block associated with the file | 1133 | /* See if there is some block associated with the file |
1029 | at that position, map the buffer to this block */ | 1134 | at that position, map the buffer to this block */ |
1030 | if ( get_block_num(item,item_pos) ) { | 1135 | if (get_block_num(item, item_pos)) { |
1031 | map_bh(bh, inode->i_sb, get_block_num(item,item_pos)); | 1136 | map_bh(bh, inode->i_sb, |
1032 | blocks--; // Decrease the amount of blocks that need to be | 1137 | get_block_num(item, item_pos)); |
1033 | // allocated | 1138 | blocks--; // Decrease the amount of blocks that need to be |
1139 | // allocated | ||
1140 | } | ||
1141 | item_pos++; | ||
1142 | /* Update the key */ | ||
1143 | set_cpu_key_k_offset(&key, | ||
1144 | cpu_key_k_offset(&key) + | ||
1145 | inode->i_sb->s_blocksize); | ||
1034 | } | 1146 | } |
1035 | item_pos++; | ||
1036 | /* Update the key */ | ||
1037 | set_cpu_key_k_offset( &key, cpu_key_k_offset(&key) + inode->i_sb->s_blocksize); | ||
1038 | } | 1147 | } |
1039 | } | 1148 | pathrelse(&path); // Free the path |
1040 | pathrelse(&path); // Free the path | 1149 | reiserfs_write_unlock(inode->i_sb); |
1041 | reiserfs_write_unlock(inode->i_sb); | ||
1042 | 1150 | ||
1043 | /* Now zero out unmappend buffers for the first and last pages of | 1151 | /* Now zero out unmappend buffers for the first and last pages of |
1044 | write area or issue read requests if page is mapped. */ | 1152 | write area or issue read requests if page is mapped. */ |
1045 | /* First page, see if it is not uptodate */ | 1153 | /* First page, see if it is not uptodate */ |
1046 | if ( !PageUptodate(prepared_pages[0]) ) { | 1154 | if (!PageUptodate(prepared_pages[0])) { |
1047 | head = page_buffers(prepared_pages[0]); | 1155 | head = page_buffers(prepared_pages[0]); |
1048 | 1156 | ||
1049 | /* For each buffer in page */ | 1157 | /* For each buffer in page */ |
1050 | for(bh = head, block_start = 0; bh != head || !block_start; | 1158 | for (bh = head, block_start = 0; bh != head || !block_start; |
1051 | block_start=block_end, bh = bh->b_this_page) { | 1159 | block_start = block_end, bh = bh->b_this_page) { |
1052 | 1160 | ||
1053 | if (!bh) | 1161 | if (!bh) |
1054 | reiserfs_panic(inode->i_sb, "green-9002: Allocated but absent buffer for a page?"); | 1162 | reiserfs_panic(inode->i_sb, |
1055 | /* Find where this buffer ends */ | 1163 | "green-9002: Allocated but absent buffer for a page?"); |
1056 | block_end = block_start+inode->i_sb->s_blocksize; | 1164 | /* Find where this buffer ends */ |
1057 | if ( block_end <= from ) | 1165 | block_end = block_start + inode->i_sb->s_blocksize; |
1058 | /* if this buffer is before requested data to map, skip it*/ | 1166 | if (block_end <= from) |
1059 | continue; | 1167 | /* if this buffer is before requested data to map, skip it */ |
1060 | if ( block_start < from ) { /* Aha, our partial buffer */ | 1168 | continue; |
1061 | if ( buffer_mapped(bh) ) { /* If it is mapped, we need to | 1169 | if (block_start < from) { /* Aha, our partial buffer */ |
1062 | issue READ request for it to | 1170 | if (buffer_mapped(bh)) { /* If it is mapped, we need to |
1063 | not loose data */ | 1171 | issue READ request for it to |
1064 | ll_rw_block(READ, 1, &bh); | 1172 | not loose data */ |
1065 | *wait_bh++=bh; | 1173 | ll_rw_block(READ, 1, &bh); |
1066 | } else { /* Not mapped, zero it */ | 1174 | *wait_bh++ = bh; |
1067 | char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0); | 1175 | } else { /* Not mapped, zero it */ |
1068 | memset(kaddr+block_start, 0, from-block_start); | 1176 | char *kaddr = |
1069 | kunmap_atomic( kaddr, KM_USER0); | 1177 | kmap_atomic(prepared_pages[0], |
1070 | set_buffer_uptodate(bh); | 1178 | KM_USER0); |
1071 | } | 1179 | memset(kaddr + block_start, 0, |
1180 | from - block_start); | ||
1181 | kunmap_atomic(kaddr, KM_USER0); | ||
1182 | set_buffer_uptodate(bh); | ||
1183 | } | ||
1184 | } | ||
1072 | } | 1185 | } |
1073 | } | ||
1074 | } | 1186 | } |
1075 | 1187 | ||
1076 | /* Last page, see if it is not uptodate, or if the last page is past the end of the file. */ | 1188 | /* Last page, see if it is not uptodate, or if the last page is past the end of the file. */ |
1077 | if ( !PageUptodate(prepared_pages[num_pages-1]) || | 1189 | if (!PageUptodate(prepared_pages[num_pages - 1]) || |
1078 | ((pos+write_bytes)>>PAGE_CACHE_SHIFT) > (inode->i_size>>PAGE_CACHE_SHIFT) ) { | 1190 | ((pos + write_bytes) >> PAGE_CACHE_SHIFT) > |
1079 | head = page_buffers(prepared_pages[num_pages-1]); | 1191 | (inode->i_size >> PAGE_CACHE_SHIFT)) { |
1080 | 1192 | head = page_buffers(prepared_pages[num_pages - 1]); | |
1081 | /* for each buffer in page */ | 1193 | |
1082 | for(bh = head, block_start = 0; bh != head || !block_start; | 1194 | /* for each buffer in page */ |
1083 | block_start=block_end, bh = bh->b_this_page) { | 1195 | for (bh = head, block_start = 0; bh != head || !block_start; |
1084 | 1196 | block_start = block_end, bh = bh->b_this_page) { | |
1085 | if (!bh) | 1197 | |
1086 | reiserfs_panic(inode->i_sb, "green-9002: Allocated but absent buffer for a page?"); | 1198 | if (!bh) |
1087 | /* Find where this buffer ends */ | 1199 | reiserfs_panic(inode->i_sb, |
1088 | block_end = block_start+inode->i_sb->s_blocksize; | 1200 | "green-9002: Allocated but absent buffer for a page?"); |
1089 | if ( block_start >= to ) | 1201 | /* Find where this buffer ends */ |
1090 | /* if this buffer is after requested data to map, skip it*/ | 1202 | block_end = block_start + inode->i_sb->s_blocksize; |
1091 | break; | 1203 | if (block_start >= to) |
1092 | if ( block_end > to ) { /* Aha, our partial buffer */ | 1204 | /* if this buffer is after requested data to map, skip it */ |
1093 | if ( buffer_mapped(bh) ) { /* If it is mapped, we need to | 1205 | break; |
1094 | issue READ request for it to | 1206 | if (block_end > to) { /* Aha, our partial buffer */ |
1095 | not loose data */ | 1207 | if (buffer_mapped(bh)) { /* If it is mapped, we need to |
1096 | ll_rw_block(READ, 1, &bh); | 1208 | issue READ request for it to |
1097 | *wait_bh++=bh; | 1209 | not loose data */ |
1098 | } else { /* Not mapped, zero it */ | 1210 | ll_rw_block(READ, 1, &bh); |
1099 | char *kaddr = kmap_atomic(prepared_pages[num_pages-1], KM_USER0); | 1211 | *wait_bh++ = bh; |
1100 | memset(kaddr+to, 0, block_end-to); | 1212 | } else { /* Not mapped, zero it */ |
1101 | kunmap_atomic( kaddr, KM_USER0); | 1213 | char *kaddr = |
1102 | set_buffer_uptodate(bh); | 1214 | kmap_atomic(prepared_pages |
1103 | } | 1215 | [num_pages - 1], |
1216 | KM_USER0); | ||
1217 | memset(kaddr + to, 0, block_end - to); | ||
1218 | kunmap_atomic(kaddr, KM_USER0); | ||
1219 | set_buffer_uptodate(bh); | ||
1220 | } | ||
1221 | } | ||
1104 | } | 1222 | } |
1105 | } | ||
1106 | } | 1223 | } |
1107 | 1224 | ||
1108 | /* Wait for read requests we made to happen, if necessary */ | 1225 | /* Wait for read requests we made to happen, if necessary */ |
1109 | while(wait_bh > wait) { | 1226 | while (wait_bh > wait) { |
1110 | wait_on_buffer(*--wait_bh); | 1227 | wait_on_buffer(*--wait_bh); |
1111 | if (!buffer_uptodate(*wait_bh)) { | 1228 | if (!buffer_uptodate(*wait_bh)) { |
1112 | res = -EIO; | 1229 | res = -EIO; |
1113 | goto failed_read; | 1230 | goto failed_read; |
1231 | } | ||
1114 | } | 1232 | } |
1115 | } | 1233 | |
1116 | 1234 | return blocks; | |
1117 | return blocks; | 1235 | failed_page_grabbing: |
1118 | failed_page_grabbing: | 1236 | num_pages = i; |
1119 | num_pages = i; | 1237 | failed_read: |
1120 | failed_read: | 1238 | reiserfs_unprepare_pages(prepared_pages, num_pages); |
1121 | reiserfs_unprepare_pages(prepared_pages, num_pages); | 1239 | return res; |
1122 | return res; | ||
1123 | } | 1240 | } |
1124 | 1241 | ||
1125 | /* Write @count bytes at position @ppos in a file indicated by @file | 1242 | /* Write @count bytes at position @ppos in a file indicated by @file |
@@ -1148,262 +1265,305 @@ failed_read: | |||
1148 | Future Features: providing search_by_key with hints. | 1265 | Future Features: providing search_by_key with hints. |
1149 | 1266 | ||
1150 | */ | 1267 | */ |
1151 | static ssize_t reiserfs_file_write( struct file *file, /* the file we are going to write into */ | 1268 | static ssize_t reiserfs_file_write(struct file *file, /* the file we are going to write into */ |
1152 | const char __user *buf, /* pointer to user supplied data | 1269 | const char __user * buf, /* pointer to user supplied data |
1153 | (in userspace) */ | 1270 | (in userspace) */ |
1154 | size_t count, /* amount of bytes to write */ | 1271 | size_t count, /* amount of bytes to write */ |
1155 | loff_t *ppos /* pointer to position in file that we start writing at. Should be updated to | 1272 | loff_t * ppos /* pointer to position in file that we start writing at. Should be updated to |
1156 | * new current position before returning. */ ) | 1273 | * new current position before returning. */ |
1274 | ) | ||
1157 | { | 1275 | { |
1158 | size_t already_written = 0; // Number of bytes already written to the file. | 1276 | size_t already_written = 0; // Number of bytes already written to the file. |
1159 | loff_t pos; // Current position in the file. | 1277 | loff_t pos; // Current position in the file. |
1160 | ssize_t res; // return value of various functions that we call. | 1278 | ssize_t res; // return value of various functions that we call. |
1161 | int err = 0; | 1279 | int err = 0; |
1162 | struct inode *inode = file->f_dentry->d_inode; // Inode of the file that we are writing to. | 1280 | struct inode *inode = file->f_dentry->d_inode; // Inode of the file that we are writing to. |
1163 | /* To simplify coding at this time, we store | 1281 | /* To simplify coding at this time, we store |
1164 | locked pages in array for now */ | 1282 | locked pages in array for now */ |
1165 | struct page * prepared_pages[REISERFS_WRITE_PAGES_AT_A_TIME]; | 1283 | struct page *prepared_pages[REISERFS_WRITE_PAGES_AT_A_TIME]; |
1166 | struct reiserfs_transaction_handle th; | 1284 | struct reiserfs_transaction_handle th; |
1167 | th.t_trans_id = 0; | 1285 | th.t_trans_id = 0; |
1168 | 1286 | ||
1169 | if ( file->f_flags & O_DIRECT) { // Direct IO needs treatment | 1287 | if (file->f_flags & O_DIRECT) { // Direct IO needs treatment |
1170 | ssize_t result, after_file_end = 0; | 1288 | ssize_t result, after_file_end = 0; |
1171 | if ( (*ppos + count >= inode->i_size) || (file->f_flags & O_APPEND) ) { | 1289 | if ((*ppos + count >= inode->i_size) |
1172 | /* If we are appending a file, we need to put this savelink in here. | 1290 | || (file->f_flags & O_APPEND)) { |
1173 | If we will crash while doing direct io, finish_unfinished will | 1291 | /* If we are appending a file, we need to put this savelink in here. |
1174 | cut the garbage from the file end. */ | 1292 | If we will crash while doing direct io, finish_unfinished will |
1175 | reiserfs_write_lock(inode->i_sb); | 1293 | cut the garbage from the file end. */ |
1176 | err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT ); | 1294 | reiserfs_write_lock(inode->i_sb); |
1177 | if (err) { | 1295 | err = |
1178 | reiserfs_write_unlock (inode->i_sb); | 1296 | journal_begin(&th, inode->i_sb, |
1179 | return err; | 1297 | JOURNAL_PER_BALANCE_CNT); |
1180 | } | 1298 | if (err) { |
1181 | reiserfs_update_inode_transaction(inode); | 1299 | reiserfs_write_unlock(inode->i_sb); |
1182 | add_save_link (&th, inode, 1 /* Truncate */); | 1300 | return err; |
1183 | after_file_end = 1; | 1301 | } |
1184 | err = journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT ); | 1302 | reiserfs_update_inode_transaction(inode); |
1185 | reiserfs_write_unlock(inode->i_sb); | 1303 | add_save_link(&th, inode, 1 /* Truncate */ ); |
1186 | if (err) | 1304 | after_file_end = 1; |
1187 | return err; | 1305 | err = |
1188 | } | 1306 | journal_end(&th, inode->i_sb, |
1189 | result = generic_file_write(file, buf, count, ppos); | 1307 | JOURNAL_PER_BALANCE_CNT); |
1190 | 1308 | reiserfs_write_unlock(inode->i_sb); | |
1191 | if ( after_file_end ) { /* Now update i_size and remove the savelink */ | 1309 | if (err) |
1192 | struct reiserfs_transaction_handle th; | 1310 | return err; |
1193 | reiserfs_write_lock(inode->i_sb); | 1311 | } |
1194 | err = journal_begin(&th, inode->i_sb, 1); | 1312 | result = generic_file_write(file, buf, count, ppos); |
1195 | if (err) { | 1313 | |
1196 | reiserfs_write_unlock (inode->i_sb); | 1314 | if (after_file_end) { /* Now update i_size and remove the savelink */ |
1197 | return err; | 1315 | struct reiserfs_transaction_handle th; |
1198 | } | 1316 | reiserfs_write_lock(inode->i_sb); |
1199 | reiserfs_update_inode_transaction(inode); | 1317 | err = journal_begin(&th, inode->i_sb, 1); |
1200 | reiserfs_update_sd(&th, inode); | 1318 | if (err) { |
1201 | err = journal_end(&th, inode->i_sb, 1); | 1319 | reiserfs_write_unlock(inode->i_sb); |
1202 | if (err) { | 1320 | return err; |
1203 | reiserfs_write_unlock (inode->i_sb); | 1321 | } |
1204 | return err; | 1322 | reiserfs_update_inode_transaction(inode); |
1205 | } | 1323 | reiserfs_update_sd(&th, inode); |
1206 | err = remove_save_link (inode, 1/* truncate */); | 1324 | err = journal_end(&th, inode->i_sb, 1); |
1207 | reiserfs_write_unlock(inode->i_sb); | 1325 | if (err) { |
1208 | if (err) | 1326 | reiserfs_write_unlock(inode->i_sb); |
1209 | return err; | 1327 | return err; |
1210 | } | 1328 | } |
1211 | 1329 | err = remove_save_link(inode, 1 /* truncate */ ); | |
1212 | return result; | 1330 | reiserfs_write_unlock(inode->i_sb); |
1213 | } | 1331 | if (err) |
1214 | 1332 | return err; | |
1215 | if ( unlikely((ssize_t) count < 0 )) | 1333 | } |
1216 | return -EINVAL; | ||
1217 | |||
1218 | if (unlikely(!access_ok(VERIFY_READ, buf, count))) | ||
1219 | return -EFAULT; | ||
1220 | |||
1221 | down(&inode->i_sem); // locks the entire file for just us | ||
1222 | |||
1223 | pos = *ppos; | ||
1224 | |||
1225 | /* Check if we can write to specified region of file, file | ||
1226 | is not overly big and this kind of stuff. Adjust pos and | ||
1227 | count, if needed */ | ||
1228 | res = generic_write_checks(file, &pos, &count, 0); | ||
1229 | if (res) | ||
1230 | goto out; | ||
1231 | |||
1232 | if ( count == 0 ) | ||
1233 | goto out; | ||
1234 | |||
1235 | res = remove_suid(file->f_dentry); | ||
1236 | if (res) | ||
1237 | goto out; | ||
1238 | |||
1239 | inode_update_time(inode, 1); /* Both mtime and ctime */ | ||
1240 | |||
1241 | // Ok, we are done with all the checks. | ||
1242 | 1334 | ||
1243 | // Now we should start real work | 1335 | return result; |
1336 | } | ||
1244 | 1337 | ||
1245 | /* If we are going to write past the file's packed tail or if we are going | 1338 | if (unlikely((ssize_t) count < 0)) |
1246 | to overwrite part of the tail, we need that tail to be converted into | 1339 | return -EINVAL; |
1247 | unformatted node */ | 1340 | |
1248 | res = reiserfs_check_for_tail_and_convert( inode, pos, count); | 1341 | if (unlikely(!access_ok(VERIFY_READ, buf, count))) |
1249 | if (res) | 1342 | return -EFAULT; |
1250 | goto out; | 1343 | |
1344 | down(&inode->i_sem); // locks the entire file for just us | ||
1345 | |||
1346 | pos = *ppos; | ||
1347 | |||
1348 | /* Check if we can write to specified region of file, file | ||
1349 | is not overly big and this kind of stuff. Adjust pos and | ||
1350 | count, if needed */ | ||
1351 | res = generic_write_checks(file, &pos, &count, 0); | ||
1352 | if (res) | ||
1353 | goto out; | ||
1354 | |||
1355 | if (count == 0) | ||
1356 | goto out; | ||
1357 | |||
1358 | res = remove_suid(file->f_dentry); | ||
1359 | if (res) | ||
1360 | goto out; | ||
1361 | |||
1362 | inode_update_time(inode, 1); /* Both mtime and ctime */ | ||
1363 | |||
1364 | // Ok, we are done with all the checks. | ||
1365 | |||
1366 | // Now we should start real work | ||
1367 | |||
1368 | /* If we are going to write past the file's packed tail or if we are going | ||
1369 | to overwrite part of the tail, we need that tail to be converted into | ||
1370 | unformatted node */ | ||
1371 | res = reiserfs_check_for_tail_and_convert(inode, pos, count); | ||
1372 | if (res) | ||
1373 | goto out; | ||
1374 | |||
1375 | while (count > 0) { | ||
1376 | /* This is the main loop in which we running until some error occures | ||
1377 | or until we write all of the data. */ | ||
1378 | size_t num_pages; /* amount of pages we are going to write this iteration */ | ||
1379 | size_t write_bytes; /* amount of bytes to write during this iteration */ | ||
1380 | size_t blocks_to_allocate; /* how much blocks we need to allocate for this iteration */ | ||
1381 | |||
1382 | /* (pos & (PAGE_CACHE_SIZE-1)) is an idiom for offset into a page of pos */ | ||
1383 | num_pages = !!((pos + count) & (PAGE_CACHE_SIZE - 1)) + /* round up partial | ||
1384 | pages */ | ||
1385 | ((count + | ||
1386 | (pos & (PAGE_CACHE_SIZE - 1))) >> PAGE_CACHE_SHIFT); | ||
1387 | /* convert size to amount of | ||
1388 | pages */ | ||
1389 | reiserfs_write_lock(inode->i_sb); | ||
1390 | if (num_pages > REISERFS_WRITE_PAGES_AT_A_TIME | ||
1391 | || num_pages > reiserfs_can_fit_pages(inode->i_sb)) { | ||
1392 | /* If we were asked to write more data than we want to or if there | ||
1393 | is not that much space, then we shorten amount of data to write | ||
1394 | for this iteration. */ | ||
1395 | num_pages = | ||
1396 | min_t(size_t, REISERFS_WRITE_PAGES_AT_A_TIME, | ||
1397 | reiserfs_can_fit_pages(inode->i_sb)); | ||
1398 | /* Also we should not forget to set size in bytes accordingly */ | ||
1399 | write_bytes = (num_pages << PAGE_CACHE_SHIFT) - | ||
1400 | (pos & (PAGE_CACHE_SIZE - 1)); | ||
1401 | /* If position is not on the | ||
1402 | start of the page, we need | ||
1403 | to substract the offset | ||
1404 | within page */ | ||
1405 | } else | ||
1406 | write_bytes = count; | ||
1407 | |||
1408 | /* reserve the blocks to be allocated later, so that later on | ||
1409 | we still have the space to write the blocks to */ | ||
1410 | reiserfs_claim_blocks_to_be_allocated(inode->i_sb, | ||
1411 | num_pages << | ||
1412 | (PAGE_CACHE_SHIFT - | ||
1413 | inode->i_blkbits)); | ||
1414 | reiserfs_write_unlock(inode->i_sb); | ||
1415 | |||
1416 | if (!num_pages) { /* If we do not have enough space even for a single page... */ | ||
1417 | if (pos > | ||
1418 | inode->i_size + inode->i_sb->s_blocksize - | ||
1419 | (pos & (inode->i_sb->s_blocksize - 1))) { | ||
1420 | res = -ENOSPC; | ||
1421 | break; // In case we are writing past the end of the last file block, break. | ||
1422 | } | ||
1423 | // Otherwise we are possibly overwriting the file, so | ||
1424 | // let's set write size to be equal or less than blocksize. | ||
1425 | // This way we get it correctly for file holes. | ||
1426 | // But overwriting files on absolutelly full volumes would not | ||
1427 | // be very efficient. Well, people are not supposed to fill | ||
1428 | // 100% of disk space anyway. | ||
1429 | write_bytes = | ||
1430 | min_t(size_t, count, | ||
1431 | inode->i_sb->s_blocksize - | ||
1432 | (pos & (inode->i_sb->s_blocksize - 1))); | ||
1433 | num_pages = 1; | ||
1434 | // No blocks were claimed before, so do it now. | ||
1435 | reiserfs_claim_blocks_to_be_allocated(inode->i_sb, | ||
1436 | 1 << | ||
1437 | (PAGE_CACHE_SHIFT | ||
1438 | - | ||
1439 | inode-> | ||
1440 | i_blkbits)); | ||
1441 | } | ||
1251 | 1442 | ||
1252 | while ( count > 0) { | 1443 | /* Prepare for writing into the region, read in all the |
1253 | /* This is the main loop in which we running until some error occures | 1444 | partially overwritten pages, if needed. And lock the pages, |
1254 | or until we write all of the data. */ | 1445 | so that nobody else can access these until we are done. |
1255 | size_t num_pages;/* amount of pages we are going to write this iteration */ | 1446 | We get number of actual blocks needed as a result. */ |
1256 | size_t write_bytes; /* amount of bytes to write during this iteration */ | 1447 | blocks_to_allocate = |
1257 | size_t blocks_to_allocate; /* how much blocks we need to allocate for this iteration */ | 1448 | reiserfs_prepare_file_region_for_write(inode, pos, |
1258 | 1449 | num_pages, | |
1259 | /* (pos & (PAGE_CACHE_SIZE-1)) is an idiom for offset into a page of pos*/ | 1450 | write_bytes, |
1260 | num_pages = !!((pos+count) & (PAGE_CACHE_SIZE - 1)) + /* round up partial | 1451 | prepared_pages); |
1261 | pages */ | 1452 | if (blocks_to_allocate < 0) { |
1262 | ((count + (pos & (PAGE_CACHE_SIZE-1))) >> PAGE_CACHE_SHIFT); | 1453 | res = blocks_to_allocate; |
1263 | /* convert size to amount of | 1454 | reiserfs_release_claimed_blocks(inode->i_sb, |
1264 | pages */ | 1455 | num_pages << |
1265 | reiserfs_write_lock(inode->i_sb); | 1456 | (PAGE_CACHE_SHIFT - |
1266 | if ( num_pages > REISERFS_WRITE_PAGES_AT_A_TIME | 1457 | inode->i_blkbits)); |
1267 | || num_pages > reiserfs_can_fit_pages(inode->i_sb) ) { | 1458 | break; |
1268 | /* If we were asked to write more data than we want to or if there | 1459 | } |
1269 | is not that much space, then we shorten amount of data to write | ||
1270 | for this iteration. */ | ||
1271 | num_pages = min_t(size_t, REISERFS_WRITE_PAGES_AT_A_TIME, reiserfs_can_fit_pages(inode->i_sb)); | ||
1272 | /* Also we should not forget to set size in bytes accordingly */ | ||
1273 | write_bytes = (num_pages << PAGE_CACHE_SHIFT) - | ||
1274 | (pos & (PAGE_CACHE_SIZE-1)); | ||
1275 | /* If position is not on the | ||
1276 | start of the page, we need | ||
1277 | to substract the offset | ||
1278 | within page */ | ||
1279 | } else | ||
1280 | write_bytes = count; | ||
1281 | 1460 | ||
1282 | /* reserve the blocks to be allocated later, so that later on | 1461 | /* First we correct our estimate of how many blocks we need */ |
1283 | we still have the space to write the blocks to */ | 1462 | reiserfs_release_claimed_blocks(inode->i_sb, |
1284 | reiserfs_claim_blocks_to_be_allocated(inode->i_sb, num_pages << (PAGE_CACHE_SHIFT - inode->i_blkbits)); | 1463 | (num_pages << |
1285 | reiserfs_write_unlock(inode->i_sb); | 1464 | (PAGE_CACHE_SHIFT - |
1465 | inode->i_sb-> | ||
1466 | s_blocksize_bits)) - | ||
1467 | blocks_to_allocate); | ||
1468 | |||
1469 | if (blocks_to_allocate > 0) { /*We only allocate blocks if we need to */ | ||
1470 | /* Fill in all the possible holes and append the file if needed */ | ||
1471 | res = | ||
1472 | reiserfs_allocate_blocks_for_region(&th, inode, pos, | ||
1473 | num_pages, | ||
1474 | write_bytes, | ||
1475 | prepared_pages, | ||
1476 | blocks_to_allocate); | ||
1477 | } | ||
1286 | 1478 | ||
1287 | if ( !num_pages ) { /* If we do not have enough space even for a single page... */ | 1479 | /* well, we have allocated the blocks, so it is time to free |
1288 | if ( pos > inode->i_size+inode->i_sb->s_blocksize-(pos & (inode->i_sb->s_blocksize-1))) { | 1480 | the reservation we made earlier. */ |
1289 | res = -ENOSPC; | 1481 | reiserfs_release_claimed_blocks(inode->i_sb, |
1290 | break; // In case we are writing past the end of the last file block, break. | 1482 | blocks_to_allocate); |
1291 | } | 1483 | if (res) { |
1292 | // Otherwise we are possibly overwriting the file, so | 1484 | reiserfs_unprepare_pages(prepared_pages, num_pages); |
1293 | // let's set write size to be equal or less than blocksize. | 1485 | break; |
1294 | // This way we get it correctly for file holes. | 1486 | } |
1295 | // But overwriting files on absolutelly full volumes would not | ||
1296 | // be very efficient. Well, people are not supposed to fill | ||
1297 | // 100% of disk space anyway. | ||
1298 | write_bytes = min_t(size_t, count, inode->i_sb->s_blocksize - (pos & (inode->i_sb->s_blocksize - 1))); | ||
1299 | num_pages = 1; | ||
1300 | // No blocks were claimed before, so do it now. | ||
1301 | reiserfs_claim_blocks_to_be_allocated(inode->i_sb, 1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)); | ||
1302 | } | ||
1303 | 1487 | ||
1304 | /* Prepare for writing into the region, read in all the | 1488 | /* NOTE that allocating blocks and filling blocks can be done in reverse order |
1305 | partially overwritten pages, if needed. And lock the pages, | 1489 | and probably we would do that just to get rid of garbage in files after a |
1306 | so that nobody else can access these until we are done. | 1490 | crash */ |
1307 | We get number of actual blocks needed as a result.*/ | ||
1308 | blocks_to_allocate = reiserfs_prepare_file_region_for_write(inode, pos, num_pages, write_bytes, prepared_pages); | ||
1309 | if ( blocks_to_allocate < 0 ) { | ||
1310 | res = blocks_to_allocate; | ||
1311 | reiserfs_release_claimed_blocks(inode->i_sb, num_pages << (PAGE_CACHE_SHIFT - inode->i_blkbits)); | ||
1312 | break; | ||
1313 | } | ||
1314 | 1491 | ||
1315 | /* First we correct our estimate of how many blocks we need */ | 1492 | /* Copy data from user-supplied buffer to file's pages */ |
1316 | reiserfs_release_claimed_blocks(inode->i_sb, (num_pages << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits)) - blocks_to_allocate ); | 1493 | res = |
1494 | reiserfs_copy_from_user_to_file_region(pos, num_pages, | ||
1495 | write_bytes, | ||
1496 | prepared_pages, buf); | ||
1497 | if (res) { | ||
1498 | reiserfs_unprepare_pages(prepared_pages, num_pages); | ||
1499 | break; | ||
1500 | } | ||
1317 | 1501 | ||
1318 | if ( blocks_to_allocate > 0) {/*We only allocate blocks if we need to*/ | 1502 | /* Send the pages to disk and unlock them. */ |
1319 | /* Fill in all the possible holes and append the file if needed */ | 1503 | res = |
1320 | res = reiserfs_allocate_blocks_for_region(&th, inode, pos, num_pages, write_bytes, prepared_pages, blocks_to_allocate); | 1504 | reiserfs_submit_file_region_for_write(&th, inode, pos, |
1505 | num_pages, | ||
1506 | write_bytes, | ||
1507 | prepared_pages); | ||
1508 | if (res) | ||
1509 | break; | ||
1510 | |||
1511 | already_written += write_bytes; | ||
1512 | buf += write_bytes; | ||
1513 | *ppos = pos += write_bytes; | ||
1514 | count -= write_bytes; | ||
1515 | balance_dirty_pages_ratelimited(inode->i_mapping); | ||
1321 | } | 1516 | } |
1322 | 1517 | ||
1323 | /* well, we have allocated the blocks, so it is time to free | 1518 | /* this is only true on error */ |
1324 | the reservation we made earlier. */ | 1519 | if (th.t_trans_id) { |
1325 | reiserfs_release_claimed_blocks(inode->i_sb, blocks_to_allocate); | 1520 | reiserfs_write_lock(inode->i_sb); |
1326 | if ( res ) { | 1521 | err = journal_end(&th, th.t_super, th.t_blocks_allocated); |
1327 | reiserfs_unprepare_pages(prepared_pages, num_pages); | 1522 | reiserfs_write_unlock(inode->i_sb); |
1328 | break; | 1523 | if (err) { |
1524 | res = err; | ||
1525 | goto out; | ||
1526 | } | ||
1329 | } | 1527 | } |
1330 | 1528 | ||
1331 | /* NOTE that allocating blocks and filling blocks can be done in reverse order | 1529 | if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) |
1332 | and probably we would do that just to get rid of garbage in files after a | 1530 | res = |
1333 | crash */ | 1531 | generic_osync_inode(inode, file->f_mapping, |
1532 | OSYNC_METADATA | OSYNC_DATA); | ||
1334 | 1533 | ||
1335 | /* Copy data from user-supplied buffer to file's pages */ | 1534 | up(&inode->i_sem); |
1336 | res = reiserfs_copy_from_user_to_file_region(pos, num_pages, write_bytes, prepared_pages, buf); | 1535 | reiserfs_async_progress_wait(inode->i_sb); |
1337 | if ( res ) { | 1536 | return (already_written != 0) ? already_written : res; |
1338 | reiserfs_unprepare_pages(prepared_pages, num_pages); | ||
1339 | break; | ||
1340 | } | ||
1341 | 1537 | ||
1342 | /* Send the pages to disk and unlock them. */ | 1538 | out: |
1343 | res = reiserfs_submit_file_region_for_write(&th, inode, pos, num_pages, | 1539 | up(&inode->i_sem); // unlock the file on exit. |
1344 | write_bytes,prepared_pages); | 1540 | return res; |
1345 | if ( res ) | ||
1346 | break; | ||
1347 | |||
1348 | already_written += write_bytes; | ||
1349 | buf += write_bytes; | ||
1350 | *ppos = pos += write_bytes; | ||
1351 | count -= write_bytes; | ||
1352 | balance_dirty_pages_ratelimited(inode->i_mapping); | ||
1353 | } | ||
1354 | |||
1355 | /* this is only true on error */ | ||
1356 | if (th.t_trans_id) { | ||
1357 | reiserfs_write_lock(inode->i_sb); | ||
1358 | err = journal_end(&th, th.t_super, th.t_blocks_allocated); | ||
1359 | reiserfs_write_unlock(inode->i_sb); | ||
1360 | if (err) { | ||
1361 | res = err; | ||
1362 | goto out; | ||
1363 | } | ||
1364 | } | ||
1365 | |||
1366 | if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) | ||
1367 | res = generic_osync_inode(inode, file->f_mapping, OSYNC_METADATA|OSYNC_DATA); | ||
1368 | |||
1369 | up(&inode->i_sem); | ||
1370 | reiserfs_async_progress_wait(inode->i_sb); | ||
1371 | return (already_written != 0)?already_written:res; | ||
1372 | |||
1373 | out: | ||
1374 | up(&inode->i_sem); // unlock the file on exit. | ||
1375 | return res; | ||
1376 | } | 1541 | } |
1377 | 1542 | ||
1378 | static ssize_t reiserfs_aio_write(struct kiocb *iocb, const char __user *buf, | 1543 | static ssize_t reiserfs_aio_write(struct kiocb *iocb, const char __user * buf, |
1379 | size_t count, loff_t pos) | 1544 | size_t count, loff_t pos) |
1380 | { | 1545 | { |
1381 | return generic_file_aio_write(iocb, buf, count, pos); | 1546 | return generic_file_aio_write(iocb, buf, count, pos); |
1382 | } | 1547 | } |
1383 | 1548 | ||
1384 | |||
1385 | |||
1386 | struct file_operations reiserfs_file_operations = { | 1549 | struct file_operations reiserfs_file_operations = { |
1387 | .read = generic_file_read, | 1550 | .read = generic_file_read, |
1388 | .write = reiserfs_file_write, | 1551 | .write = reiserfs_file_write, |
1389 | .ioctl = reiserfs_ioctl, | 1552 | .ioctl = reiserfs_ioctl, |
1390 | .mmap = generic_file_mmap, | 1553 | .mmap = generic_file_mmap, |
1391 | .release = reiserfs_file_release, | 1554 | .release = reiserfs_file_release, |
1392 | .fsync = reiserfs_sync_file, | 1555 | .fsync = reiserfs_sync_file, |
1393 | .sendfile = generic_file_sendfile, | 1556 | .sendfile = generic_file_sendfile, |
1394 | .aio_read = generic_file_aio_read, | 1557 | .aio_read = generic_file_aio_read, |
1395 | .aio_write = reiserfs_aio_write, | 1558 | .aio_write = reiserfs_aio_write, |
1396 | }; | 1559 | }; |
1397 | 1560 | ||
1398 | 1561 | struct inode_operations reiserfs_file_inode_operations = { | |
1399 | struct inode_operations reiserfs_file_inode_operations = { | 1562 | .truncate = reiserfs_vfs_truncate_file, |
1400 | .truncate = reiserfs_vfs_truncate_file, | 1563 | .setattr = reiserfs_setattr, |
1401 | .setattr = reiserfs_setattr, | 1564 | .setxattr = reiserfs_setxattr, |
1402 | .setxattr = reiserfs_setxattr, | 1565 | .getxattr = reiserfs_getxattr, |
1403 | .getxattr = reiserfs_getxattr, | 1566 | .listxattr = reiserfs_listxattr, |
1404 | .listxattr = reiserfs_listxattr, | 1567 | .removexattr = reiserfs_removexattr, |
1405 | .removexattr = reiserfs_removexattr, | 1568 | .permission = reiserfs_permission, |
1406 | .permission = reiserfs_permission, | ||
1407 | }; | 1569 | }; |
1408 | |||
1409 | |||