blob: 970ecd9dbe69e225b7c4ba822cdf4d17514acc6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/time.h>
6#include <linux/reiserfs_fs.h>
7#include <linux/reiserfs_acl.h>
8#include <linux/reiserfs_xattr.h>
9#include <linux/smp_lock.h>
10#include <asm/uaccess.h>
11#include <linux/pagemap.h>
12#include <linux/swap.h>
13#include <linux/writeback.h>
14#include <linux/blkdev.h>
15#include <linux/buffer_head.h>
16#include <linux/quotaops.h>
17
18/*
19** We pack the tails of files on file close, not at the time they are written.
20** This implies an unnecessary copy of the tail and an unnecessary indirect item
21** insertion/balancing, for files that are written in one write.
22** It avoids unnecessary tail packings (balances) for files that are written in
23** multiple writes and are small enough to have tails.
24**
25** file_release is called by the VFS layer when the file is closed. If
26** this is the last open file descriptor, and the file
27** small enough to have a tail, and the tail is currently in an
28** unformatted node, the tail is converted back into a direct item.
29**
30** We use reiserfs_truncate_file to pack the tail, since it already has
31** all the conditions coded.
32*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070033static int reiserfs_file_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070036 struct reiserfs_transaction_handle th;
37 int err;
38 int jbegin_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Eric Sesterhenn14a61442006-10-03 23:36:38 +020040 BUG_ON(!S_ISREG(inode->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070042 /* fast out for when nothing needs to be done */
43 if ((atomic_read(&inode->i_count) > 1 ||
44 !(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
45 !tail_has_to_be_packed(inode)) &&
46 REISERFS_I(inode)->i_prealloc_count <= 0) {
47 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070049
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080050 mutex_lock(&inode->i_mutex);
Chris Masonb5f39532006-08-05 12:15:08 -070051 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 /* freeing preallocation only involves relogging blocks that
53 * are already in the current transaction. preallocation gets
54 * freed at the end of each transaction, so it is impossible for
55 * us to log any additional blocks (including quota blocks)
56 */
57 err = journal_begin(&th, inode->i_sb, 1);
58 if (err) {
59 /* uh oh, we can't allow the inode to go away while there
60 * is still preallocation blocks pending. Try to join the
61 * aborted transaction
62 */
63 jbegin_failure = err;
64 err = journal_join_abort(&th, inode->i_sb, 1);
65
66 if (err) {
67 /* hmpf, our choices here aren't good. We can pin the inode
68 * which will disallow unmount from every happening, we can
69 * do nothing, which will corrupt random memory on unmount,
70 * or we can forcibly remove the file from the preallocation
71 * list, which will leak blocks on disk. Lets pin the inode
72 * and let the admin know what is going on.
73 */
74 igrab(inode);
75 reiserfs_warning(inode->i_sb,
76 "pinning inode %lu because the "
Alexey Dobriyan533221f2006-11-25 11:09:30 -080077 "preallocation can't be freed",
78 inode->i_ino);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079 goto out;
80 }
81 }
82 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84#ifdef REISERFS_PREALLOCATE
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 reiserfs_discard_prealloc(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087 err = journal_end(&th, inode->i_sb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 /* copy back the error code from journal_begin */
90 if (!err)
91 err = jbegin_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 if (!err && atomic_read(&inode->i_count) <= 1 &&
94 (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
95 tail_has_to_be_packed(inode)) {
96 /* if regular file is released by last holder and it has been
97 appended (we append by unformatted node only) or its direct
98 item(s) had to be converted, then it may have to be
99 indirect2direct converted */
100 err = reiserfs_truncate_file(inode, 0);
101 }
102 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800103 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700104 reiserfs_write_unlock(inode->i_sb);
105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108static void reiserfs_vfs_truncate_file(struct inode *inode)
109{
110 reiserfs_truncate_file(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/* Sync a reiserfs file. */
114
115/*
116 * FIXME: sync_mapping_buffers() never has anything to sync. Can
117 * be removed...
118 */
119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120static int reiserfs_sync_file(struct file *p_s_filp,
121 struct dentry *p_s_dentry, int datasync)
122{
123 struct inode *p_s_inode = p_s_dentry->d_inode;
124 int n_err;
125 int barrier_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Eric Sesterhenn14a61442006-10-03 23:36:38 +0200127 BUG_ON(!S_ISREG(p_s_inode->i_mode));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 n_err = sync_mapping_buffers(p_s_inode->i_mapping);
129 reiserfs_write_lock(p_s_inode->i_sb);
130 barrier_done = reiserfs_commit_for_inode(p_s_inode);
131 reiserfs_write_unlock(p_s_inode->i_sb);
Chris Mason25736b12006-09-29 01:59:54 -0700132 if (barrier_done != 1 && reiserfs_barrier_flush(p_s_inode->i_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700133 blkdev_issue_flush(p_s_inode->i_sb->s_bdev, NULL);
134 if (barrier_done < 0)
135 return barrier_done;
136 return (n_err < 0) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/* I really do not want to play with memory shortage right now, so
140 to simplify the code, we are not going to write more than this much pages at
141 a time. This still should considerably improve performance compared to 4k
142 at a time case. This is 32 pages of 4k size. */
143#define REISERFS_WRITE_PAGES_AT_A_TIME (128 * 1024) / PAGE_CACHE_SIZE
144
145/* Allocates blocks for a file to fulfil write request.
146 Maps all unmapped but prepared pages from the list.
147 Updates metadata with newly allocated blocknumbers as needed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148static int reiserfs_allocate_blocks_for_region(struct reiserfs_transaction_handle *th, struct inode *inode, /* Inode we work with */
149 loff_t pos, /* Writing position */
150 int num_pages, /* number of pages write going
151 to touch */
152 int write_bytes, /* amount of bytes to write */
153 struct page **prepared_pages, /* array of
154 prepared pages
155 */
156 int blocks_to_allocate /* Amount of blocks we
157 need to allocate to
158 fit the data into file
159 */
160 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 struct cpu_key key; // cpu key of item that we are going to deal with
163 struct item_head *ih; // pointer to item head that we are going to deal with
164 struct buffer_head *bh; // Buffer head that contains items that we are going to deal with
165 __le32 *item; // pointer to item we are going to deal with
166 INITIALIZE_PATH(path); // path to item, that we are going to deal with.
167 b_blocknr_t *allocated_blocks; // Pointer to a place where allocated blocknumbers would be stored.
168 reiserfs_blocknr_hint_t hint; // hint structure for block allocator.
169 size_t res; // return value of various functions that we call.
170 int curr_block; // current block used to keep track of unmapped blocks.
171 int i; // loop counter
172 int itempos; // position in item
173 unsigned int from = (pos & (PAGE_CACHE_SIZE - 1)); // writing position in
174 // first page
175 unsigned int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1; /* last modified byte offset in last page */
176 __u64 hole_size; // amount of blocks for a file hole, if it needed to be created.
177 int modifying_this_item = 0; // Flag for items traversal code to keep track
178 // of the fact that we already prepared
179 // current block for journal
180 int will_prealloc = 0;
181 RFALSE(!blocks_to_allocate,
182 "green-9004: tried to allocate zero blocks?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 /* only preallocate if this is a small write */
185 if (REISERFS_I(inode)->i_prealloc_count ||
186 (!(write_bytes & (inode->i_sb->s_blocksize - 1)) &&
187 blocks_to_allocate <
188 REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize))
189 will_prealloc =
190 REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 allocated_blocks = kmalloc((blocks_to_allocate + will_prealloc) *
193 sizeof(b_blocknr_t), GFP_NOFS);
Diego Callejae5dd2592006-02-01 03:06:44 -0800194 if (!allocated_blocks)
195 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 /* First we compose a key to point at the writing position, we want to do
198 that outside of any locking region. */
199 make_cpu_key(&key, inode, pos + 1, TYPE_ANY, 3 /*key length */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 /* If we came here, it means we absolutely need to open a transaction,
202 since we need to allocate some blocks */
203 reiserfs_write_lock(inode->i_sb); // Journaling stuff and we need that.
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
205 if (res)
206 goto error_exit;
207 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700209 /* 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 res = -EIO;
213 goto error_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 /* 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 =
229 reiserfs_allocate_blocknrs(&hint, allocated_blocks,
230 blocks_to_allocate, blocks_to_allocate);
231 if (res != CARRY_ON) {
232 if (res == NO_DISK_SPACE) {
233 /* We flush the transaction in case of no space. This way some
234 blocks might become free */
235 SB_JOURNAL(inode->i_sb)->j_must_wait = 1;
236 res = restart_transaction(th, inode, &path);
237 if (res)
238 goto error_exit;
239
240 /* We might have scheduled, so search again */
241 res =
242 search_for_position_by_key(inode->i_sb, &key,
243 &path);
244 if (res == IO_ERROR) {
245 res = -EIO;
246 goto error_exit;
247 }
248
249 /* update changed info for hint structure. */
250 res =
251 reiserfs_allocate_blocknrs(&hint, allocated_blocks,
252 blocks_to_allocate,
253 blocks_to_allocate);
254 if (res != CARRY_ON) {
Jan Kara0ad74ff2005-11-08 21:34:58 -0800255 res = res == QUOTA_EXCEEDED ? -EDQUOT : -ENOSPC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700256 pathrelse(&path);
257 goto error_exit;
258 }
259 } else {
Jan Kara0ad74ff2005-11-08 21:34:58 -0800260 res = res == QUOTA_EXCEEDED ? -EDQUOT : -ENOSPC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 pathrelse(&path);
262 goto error_exit;
263 }
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#ifdef __BIG_ENDIAN
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700266 // Too bad, I have not found any way to convert a given region from
267 // cpu format to little endian format
268 {
269 int i;
270 for (i = 0; i < blocks_to_allocate; i++)
271 allocated_blocks[i] = cpu_to_le32(allocated_blocks[i]);
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275 /* Blocks allocating well might have scheduled and tree might have changed,
276 let's search the tree again */
277 /* find where in the tree our write should go */
278 res = search_for_position_by_key(inode->i_sb, &key, &path);
279 if (res == IO_ERROR) {
280 res = -EIO;
281 goto error_exit_free_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700284 bh = get_last_bh(&path); // Get a bufferhead for last element in path.
285 ih = get_ih(&path); // Get a pointer to last item head in path.
286 item = get_item(&path); // Get a pointer to last item in path
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 /* Let's see what we have found */
289 if (res != POSITION_FOUND) { /* position not found, this means that we
290 might need to append file with holes
291 first */
292 // Since we are writing past the file's end, we need to find out if
293 // there is a hole that needs to be inserted before our writing
294 // position, and how many blocks it is going to cover (we need to
295 // populate pointers to file blocks representing the hole with zeros)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700297 {
298 int item_offset = 1;
299 /*
300 * if ih is stat data, its offset is 0 and we don't want to
301 * add 1 to pos in the hole_size calculation
302 */
303 if (is_statdata_le_ih(ih))
304 item_offset = 0;
305 hole_size = (pos + item_offset -
306 (le_key_k_offset
307 (get_inode_item_key_version(inode),
308 &(ih->ih_key)) + op_bytes_number(ih,
309 inode->
310 i_sb->
311 s_blocksize)))
312 >> inode->i_sb->s_blocksize_bits;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700315 if (hole_size > 0) {
316 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.
317 /* area filled with zeroes, to supply as list of zero blocknumbers
318 We allocate it outside of loop just in case loop would spin for
319 several iterations. */
Yan Burman01afb212006-12-06 20:39:01 -0800320 char *zeros = kzalloc(to_paste * UNFM_P_SIZE, GFP_ATOMIC); // We cannot insert more than MAX_ITEM_LEN bytes anyway.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700321 if (!zeros) {
322 res = -ENOMEM;
323 goto error_exit_free_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325 do {
326 to_paste =
327 min_t(__u64, hole_size,
328 MAX_ITEM_LEN(inode->i_sb->
329 s_blocksize) /
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);
438 }
439 }
440 // Go through existing indirect items first
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.
479 }
480 /* Ok, we have correct position in item now, so let's see if it is
481 representing file hole (blocknumber is zero) and fill it if needed */
482 if (!item[itempos]) {
483 /* Ok, a hole. Now we need to check if we already prepared this
484 block to be journaled */
485 while (!modifying_this_item) { // loop until succeed
486 /* Well, this item is not journaled yet, so we must prepare
487 it for journal first, before we can change it */
488 struct item_head tmp_ih; // We copy item head of found item,
489 // here to detect if fs changed under
490 // us while we were preparing for
491 // journal.
492 int fs_gen; // We store fs generation here to find if someone
493 // changes fs under our feet
494
495 copy_item_head(&tmp_ih, ih); // Remember itemhead
496 fs_gen = get_generation(inode->i_sb); // remember fs generation
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++;
527 }
528 itempos++;
529 }
530
531 if (modifying_this_item) { // We need to log last-accessed block, if it
532 // was modified, but not logged yet.
533 journal_mark_dirty(th, inode->i_sb, bh);
534 }
535
536 if (curr_block < blocks_to_allocate) {
537 // Oh, well need to append to indirect item, or to create indirect item
538 // if there weren't any
539 if (is_indirect_le_ih(ih)) {
540 // Existing indirect item - append. First calculate key for append
541 // position. We do not need to recalculate path as it should
542 // already point to correct place.
543 make_cpu_key(&key, inode,
544 le_key_k_offset(get_inode_item_key_version
545 (inode),
546 &(ih->ih_key)) +
547 op_bytes_number(ih,
548 inode->i_sb->s_blocksize),
549 TYPE_INDIRECT, 3);
550 res =
551 reiserfs_paste_into_item(th, &path, &key, inode,
552 (char *)(allocated_blocks +
553 curr_block),
554 UNFM_P_SIZE *
555 (blocks_to_allocate -
556 curr_block));
557 if (res) {
558 goto error_exit_free_blocks;
559 }
560 } else if (is_statdata_le_ih(ih)) {
561 // Last found item was statdata. That means we need to create indirect item.
562 struct item_head ins_ih; /* itemhead for new item */
563
564 /* create a key for our new item */
565 make_cpu_key(&key, inode, 1, TYPE_INDIRECT, 3); // Position one,
566 // because that's
567 // where first
568 // indirect item
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 reiserfs_panic(inode->i_sb,
598 "green-9010: unexpected item type for key %K\n",
599 &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601 }
602 // the caller is responsible for closing the transaction
603 // unless we return an error, they are also responsible for logging
604 // the inode.
605 //
606 pathrelse(&path);
607 /*
608 * cleanup prellocation from previous writes
609 * if this is a partial block write
610 */
611 if (write_bytes & (inode->i_sb->s_blocksize - 1))
612 reiserfs_discard_prealloc(th, inode);
613 reiserfs_write_unlock(inode->i_sb);
614
615 // go through all the pages/buffers and map the buffers to newly allocated
616 // blocks (so that system knows where to write these pages later).
617 curr_block = 0;
618 for (i = 0; i < num_pages; i++) {
619 struct page *page = prepared_pages[i]; //current page
620 struct buffer_head *head = page_buffers(page); // first buffer for a page
621 int block_start, block_end; // in-page offsets for buffers.
622
623 if (!page_buffers(page))
624 reiserfs_panic(inode->i_sb,
625 "green-9005: No buffers for prepared page???");
626
627 /* For each buffer in page */
628 for (bh = head, block_start = 0; bh != head || !block_start;
629 block_start = block_end, bh = bh->b_this_page) {
630 if (!bh)
631 reiserfs_panic(inode->i_sb,
632 "green-9006: Allocated but absent buffer for a page?");
633 block_end = block_start + inode->i_sb->s_blocksize;
634 if (i == 0 && block_end <= from)
635 /* if this buffer is before requested data to map, skip it */
636 continue;
637 if (i == num_pages - 1 && block_start >= to)
638 /* If this buffer is after requested data to map, abort
639 processing of current page */
640 break;
641
642 if (!buffer_mapped(bh)) { // Ok, unmapped buffer, need to map it
643 map_bh(bh, inode->i_sb,
644 le32_to_cpu(allocated_blocks
645 [curr_block]));
646 curr_block++;
647 set_buffer_new(bh);
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700652 RFALSE(curr_block > blocks_to_allocate,
653 "green-9007: Used too many blocks? weird");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 kfree(allocated_blocks);
656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658// Need to deal with transaction here.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700659 error_exit_free_blocks:
660 pathrelse(&path);
661 // free blocks
662 for (i = 0; i < blocks_to_allocate; i++)
663 reiserfs_free_block(th, inode, le32_to_cpu(allocated_blocks[i]),
664 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700666 error_exit:
667 if (th->t_trans_id) {
668 int err;
669 // update any changes we made to blk count
Chris Mason9f037832005-09-13 01:25:17 -0700670 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700671 err =
672 journal_end(th, inode->i_sb,
673 JOURNAL_PER_BALANCE_CNT * 3 + 1 +
674 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb));
675 if (err)
676 res = err;
677 }
678 reiserfs_write_unlock(inode->i_sb);
679 kfree(allocated_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/* Unlock pages prepared by reiserfs_prepare_file_region_for_write */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685static void reiserfs_unprepare_pages(struct page **prepared_pages, /* list of locked pages */
686 size_t num_pages /* amount of pages */ )
687{
688 int i; // loop counter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 for (i = 0; i < num_pages; i++) {
691 struct page *page = prepared_pages[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 try_to_free_buffers(page);
694 unlock_page(page);
695 page_cache_release(page);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/* This function will copy data from userspace to specified pages within
700 supplied byte range */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701static int reiserfs_copy_from_user_to_file_region(loff_t pos, /* In-file position */
702 int num_pages, /* Number of pages affected */
703 int write_bytes, /* Amount of bytes to write */
704 struct page **prepared_pages, /* pointer to
705 array to
706 prepared pages
707 */
708 const char __user * buf /* Pointer to user-supplied
709 data */
710 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700712 long page_fault = 0; // status of copy_from_user.
713 int i; // loop counter.
714 int offset; // offset in page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 for (i = 0, offset = (pos & (PAGE_CACHE_SIZE - 1)); i < num_pages;
717 i++, offset = 0) {
718 size_t count = min_t(size_t, PAGE_CACHE_SIZE - offset, write_bytes); // How much of bytes to write to this page
719 struct page *page = prepared_pages[i]; // Current page we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700721 fault_in_pages_readable(buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700723 /* Copy data from userspace to the current page */
724 kmap(page);
725 page_fault = __copy_from_user(page_address(page) + offset, buf, count); // Copy the data.
726 /* Flush processor's dcache for this page */
727 flush_dcache_page(page);
728 kunmap(page);
729 buf += count;
730 write_bytes -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700732 if (page_fault)
733 break; // Was there a fault? abort.
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 return page_fault ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739/* taken fs/buffer.c:__block_commit_write */
740int reiserfs_commit_page(struct inode *inode, struct page *page,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700741 unsigned from, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 unsigned block_start, block_end;
744 int partial = 0;
745 unsigned blocksize;
746 struct buffer_head *bh, *head;
747 unsigned long i_size_index = inode->i_size >> PAGE_CACHE_SHIFT;
748 int new;
749 int logit = reiserfs_file_data_log(inode);
750 struct super_block *s = inode->i_sb;
751 int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize;
752 struct reiserfs_transaction_handle th;
753 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755 th.t_trans_id = 0;
756 blocksize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700758 if (logit) {
759 reiserfs_write_lock(s);
760 ret = journal_begin(&th, s, bh_per_page + 1);
761 if (ret)
762 goto drop_write_lock;
763 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765 for (bh = head = page_buffers(page), block_start = 0;
766 bh != head || !block_start;
767 block_start = block_end, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700769 new = buffer_new(bh);
770 clear_buffer_new(bh);
771 block_end = block_start + blocksize;
772 if (block_end <= from || block_start >= to) {
773 if (!buffer_uptodate(bh))
774 partial = 1;
775 } else {
776 set_buffer_uptodate(bh);
777 if (logit) {
778 reiserfs_prepare_for_journal(s, bh, 1);
779 journal_mark_dirty(&th, s, bh);
780 } else if (!buffer_dirty(bh)) {
781 mark_buffer_dirty(bh);
782 /* do data=ordered on any page past the end
783 * of file and any buffer marked BH_New.
784 */
785 if (reiserfs_data_ordered(inode->i_sb) &&
786 (new || page->index >= i_size_index)) {
787 reiserfs_add_ordered_list(inode, bh);
788 }
789 }
790 }
791 }
792 if (logit) {
793 ret = journal_end(&th, s, bh_per_page + 1);
794 drop_write_lock:
795 reiserfs_write_unlock(s);
796 }
797 /*
798 * If this is a partial write which happened to make all buffers
799 * uptodate then we can optimize away a bogus readpage() for
800 * the next read(). Here we 'discover' whether the page went
801 * uptodate as a result of this (potentially partial) write.
802 */
803 if (!partial)
804 SetPageUptodate(page);
805 return ret;
806}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808/* Submit pages for write. This was separated from actual file copying
809 because we might want to allocate block numbers in-between.
810 This function assumes that caller will adjust file size to correct value. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700811static int reiserfs_submit_file_region_for_write(struct reiserfs_transaction_handle *th, struct inode *inode, loff_t pos, /* Writing position offset */
812 size_t num_pages, /* Number of pages to write */
813 size_t write_bytes, /* number of bytes to write */
814 struct page **prepared_pages /* list of pages */
815 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700817 int status; // return status of block_commit_write.
818 int retval = 0; // Return value we are going to return.
819 int i; // loop counter
820 int offset; // Writing offset in page.
821 int orig_write_bytes = write_bytes;
822 int sd_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700824 for (i = 0, offset = (pos & (PAGE_CACHE_SIZE - 1)); i < num_pages;
825 i++, offset = 0) {
826 int count = min_t(int, PAGE_CACHE_SIZE - offset, write_bytes); // How much of bytes to write to this page
827 struct page *page = prepared_pages[i]; // Current page we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829 status =
830 reiserfs_commit_page(inode, page, offset, offset + count);
831 if (status)
832 retval = status; // To not overcomplicate matters We are going to
833 // submit all the pages even if there was error.
834 // we only remember error status to report it on
835 // exit.
836 write_bytes -= count;
837 }
838 /* now that we've gotten all the ordered buffers marked dirty,
839 * we can safely update i_size and close any running transaction
840 */
841 if (pos + orig_write_bytes > inode->i_size) {
842 inode->i_size = pos + orig_write_bytes; // Set new size
843 /* If the file have grown so much that tail packing is no
844 * longer possible, reset "need to pack" flag */
845 if ((have_large_tails(inode->i_sb) &&
846 inode->i_size > i_block_size(inode) * 4) ||
847 (have_small_tails(inode->i_sb) &&
848 inode->i_size > i_block_size(inode)))
849 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
850 else if ((have_large_tails(inode->i_sb) &&
851 inode->i_size < i_block_size(inode) * 4) ||
852 (have_small_tails(inode->i_sb) &&
853 inode->i_size < i_block_size(inode)))
854 REISERFS_I(inode)->i_flags |= i_pack_on_close_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856 if (th->t_trans_id) {
857 reiserfs_write_lock(inode->i_sb);
Chris Mason9f037832005-09-13 01:25:17 -0700858 // this sets the proper flags for O_SYNC to trigger a commit
859 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 reiserfs_write_unlock(inode->i_sb);
Hisashi Hifumi73ce5932006-07-10 04:43:56 -0700861 } else {
862 reiserfs_write_lock(inode->i_sb);
863 reiserfs_update_inode_transaction(inode);
Chris Mason9f037832005-09-13 01:25:17 -0700864 mark_inode_dirty(inode);
Hisashi Hifumi73ce5932006-07-10 04:43:56 -0700865 reiserfs_write_unlock(inode->i_sb);
866 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700867
868 sd_update = 1;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (th->t_trans_id) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700871 reiserfs_write_lock(inode->i_sb);
872 if (!sd_update)
Chris Mason9f037832005-09-13 01:25:17 -0700873 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700874 status = journal_end(th, th->t_super, th->t_blocks_allocated);
875 if (status)
876 retval = status;
877 reiserfs_write_unlock(inode->i_sb);
878 }
879 th->t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700881 /*
882 * we have to unlock the pages after updating i_size, otherwise
883 * we race with writepage
884 */
885 for (i = 0; i < num_pages; i++) {
886 struct page *page = prepared_pages[i];
887 unlock_page(page);
888 mark_page_accessed(page);
889 page_cache_release(page);
890 }
891 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/* Look if passed writing region is going to touch file's tail
895 (if it is present). And if it is, convert the tail to unformatted node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700896static int reiserfs_check_for_tail_and_convert(struct inode *inode, /* inode to deal with */
897 loff_t pos, /* Writing position */
898 int write_bytes /* amount of bytes to write */
899 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700901 INITIALIZE_PATH(path); // needed for search_for_position
902 struct cpu_key key; // Key that would represent last touched writing byte.
903 struct item_head *ih; // item header of found block;
904 int res; // Return value of various functions we call.
905 int cont_expand_offset; // We will put offset for generic_cont_expand here
906 // This can be int just because tails are created
907 // only for small files.
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/* this embodies a dependency on a particular tail policy */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700910 if (inode->i_size >= inode->i_sb->s_blocksize * 4) {
911 /* such a big files do not have tails, so we won't bother ourselves
912 to look for tails, simply return */
913 return 0;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700916 reiserfs_write_lock(inode->i_sb);
917 /* find the item containing the last byte to be written, or if
918 * writing past the end of the file then the last item of the
919 * file (and then we check its type). */
920 make_cpu_key(&key, inode, pos + write_bytes + 1, TYPE_ANY,
921 3 /*key length */ );
922 res = search_for_position_by_key(inode->i_sb, &key, &path);
923 if (res == IO_ERROR) {
924 reiserfs_write_unlock(inode->i_sb);
925 return -EIO;
926 }
927 ih = get_ih(&path);
928 res = 0;
929 if (is_direct_le_ih(ih)) {
930 /* Ok, closest item is file tail (tails are stored in "direct"
931 * items), so we need to unpack it. */
932 /* To not overcomplicate matters, we just call generic_cont_expand
933 which will in turn call other stuff and finally will boil down to
934 reiserfs_get_block() that would do necessary conversion. */
935 cont_expand_offset =
936 le_key_k_offset(get_inode_item_key_version(inode),
937 &(ih->ih_key));
938 pathrelse(&path);
939 res = generic_cont_expand(inode, cont_expand_offset);
940 } else
941 pathrelse(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700943 reiserfs_write_unlock(inode->i_sb);
944 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/* This function locks pages starting from @pos for @inode.
948 @num_pages pages are locked and stored in
949 @prepared_pages array. Also buffers are allocated for these pages.
950 First and last page of the region is read if it is overwritten only
951 partially. If last page did not exist before write (file hole or file
952 append), it is zeroed, then.
953 Returns number of unallocated blocks that should be allocated to cover
954 new file data.*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700955static int reiserfs_prepare_file_region_for_write(struct inode *inode
956 /* Inode of the file */ ,
957 loff_t pos, /* position in the file */
958 size_t num_pages, /* number of pages to
959 prepare */
960 size_t write_bytes, /* Amount of bytes to be
961 overwritten from
962 @pos */
963 struct page **prepared_pages /* pointer to array
964 where to store
965 prepared pages */
966 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700968 int res = 0; // Return values of different functions we call.
969 unsigned long index = pos >> PAGE_CACHE_SHIFT; // Offset in file in pages.
970 int from = (pos & (PAGE_CACHE_SIZE - 1)); // Writing offset in first page
971 int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1;
972 /* offset of last modified byte in last
973 page */
974 struct address_space *mapping = inode->i_mapping; // Pages are mapped here.
975 int i; // Simple counter
976 int blocks = 0; /* Return value (blocks that should be allocated) */
977 struct buffer_head *bh, *head; // Current bufferhead and first bufferhead
978 // of a page.
979 unsigned block_start, block_end; // Starting and ending offsets of current
980 // buffer in the page.
981 struct buffer_head *wait[2], **wait_bh = wait; // Buffers for page, if
982 // Page appeared to be not up
983 // to date. Note how we have
984 // at most 2 buffers, this is
985 // because we at most may
986 // partially overwrite two
987 // buffers for one page. One at // the beginning of write area
988 // and one at the end.
989 // Everything inthe middle gets // overwritten totally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700991 struct cpu_key key; // cpu key of item that we are going to deal with
992 struct item_head *ih = NULL; // pointer to item head that we are going to deal with
993 struct buffer_head *itembuf = NULL; // Buffer head that contains items that we are going to deal with
994 INITIALIZE_PATH(path); // path to item, that we are going to deal with.
995 __le32 *item = NULL; // pointer to item we are going to deal with
996 int item_pos = -1; /* Position in indirect item */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 if (num_pages < 1) {
999 reiserfs_warning(inode->i_sb,
1000 "green-9001: reiserfs_prepare_file_region_for_write "
1001 "called with zero number of pages to process");
1002 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001005 /* We have 2 loops for pages. In first loop we grab and lock the pages, so
1006 that nobody would touch these until we release the pages. Then
1007 we'd start to deal with mapping buffers to blocks. */
1008 for (i = 0; i < num_pages; i++) {
1009 prepared_pages[i] = grab_cache_page(mapping, index + i); // locks the page
1010 if (!prepared_pages[i]) {
1011 res = -ENOMEM;
1012 goto failed_page_grabbing;
1013 }
1014 if (!page_has_buffers(prepared_pages[i]))
1015 create_empty_buffers(prepared_pages[i],
1016 inode->i_sb->s_blocksize, 0);
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001019 /* Let's count amount of blocks for a case where all the blocks
1020 overwritten are new (we will substract already allocated blocks later) */
1021 if (num_pages > 2)
1022 /* These are full-overwritten pages so we count all the blocks in
1023 these pages are counted as needed to be allocated */
1024 blocks =
1025 (num_pages - 2) << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001027 /* count blocks needed for first page (possibly partially written) */
1028 blocks += ((PAGE_CACHE_SIZE - from) >> inode->i_blkbits) + !!(from & (inode->i_sb->s_blocksize - 1)); /* roundup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001030 /* Now we account for last page. If last page == first page (we
1031 overwrite only one page), we substract all the blocks past the
1032 last writing position in a page out of already calculated number
1033 of blocks */
1034 blocks += ((num_pages > 1) << (PAGE_CACHE_SHIFT - inode->i_blkbits)) -
1035 ((PAGE_CACHE_SIZE - to) >> inode->i_blkbits);
1036 /* Note how we do not roundup here since partial blocks still
1037 should be allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 /* Now if all the write area lies past the file end, no point in
1040 maping blocks, since there is none, so we just zero out remaining
1041 parts of first and last pages in write area (if needed) */
1042 if ((pos & ~((loff_t) PAGE_CACHE_SIZE - 1)) > inode->i_size) {
1043 if (from != 0) { /* First page needs to be partially zeroed */
1044 char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0);
1045 memset(kaddr, 0, from);
1046 kunmap_atomic(kaddr, KM_USER0);
Alexey Dobriyande21c572006-12-06 20:38:02 -08001047 flush_dcache_page(prepared_pages[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001048 }
1049 if (to != PAGE_CACHE_SIZE) { /* Last page needs to be partially zeroed */
1050 char *kaddr =
1051 kmap_atomic(prepared_pages[num_pages - 1],
1052 KM_USER0);
1053 memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
1054 kunmap_atomic(kaddr, KM_USER0);
Alexey Dobriyande21c572006-12-06 20:38:02 -08001055 flush_dcache_page(prepared_pages[num_pages - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001058 /* Since all blocks are new - use already calculated value */
1059 return blocks;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 /* Well, since we write somewhere into the middle of a file, there is
1063 possibility we are writing over some already allocated blocks, so
1064 let's map these blocks and substract number of such blocks out of blocks
1065 we need to allocate (calculated above) */
1066 /* Mask write position to start on blocksize, we do it out of the
1067 loop for performance reasons */
1068 pos &= ~((loff_t) inode->i_sb->s_blocksize - 1);
1069 /* Set cpu key to the starting position in a file (on left block boundary) */
1070 make_cpu_key(&key, inode,
1071 1 + ((pos) & ~((loff_t) inode->i_sb->s_blocksize - 1)),
1072 TYPE_ANY, 3 /*key length */ );
1073
1074 reiserfs_write_lock(inode->i_sb); // We need that for at least search_by_key()
1075 for (i = 0; i < num_pages; i++) {
1076
1077 head = page_buffers(prepared_pages[i]);
1078 /* For each buffer in the page */
1079 for (bh = head, block_start = 0; bh != head || !block_start;
1080 block_start = block_end, bh = bh->b_this_page) {
1081 if (!bh)
1082 reiserfs_panic(inode->i_sb,
1083 "green-9002: Allocated but absent buffer for a page?");
1084 /* Find where this buffer ends */
1085 block_end = block_start + inode->i_sb->s_blocksize;
1086 if (i == 0 && block_end <= from)
1087 /* if this buffer is before requested data to map, skip it */
1088 continue;
1089
1090 if (i == num_pages - 1 && block_start >= to) {
1091 /* If this buffer is after requested data to map, abort
1092 processing of current page */
1093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1097 /* This is optimisation for a case where buffer is mapped
1098 and have blocknumber assigned. In case significant amount
1099 of such buffers are present, we may avoid some amount
1100 of search_by_key calls.
1101 Probably it would be possible to move parts of this code
1102 out of BKL, but I afraid that would overcomplicate code
1103 without any noticeable benefit.
1104 */
1105 item_pos++;
1106 /* Update the key */
1107 set_cpu_key_k_offset(&key,
1108 cpu_key_k_offset(&key) +
1109 inode->i_sb->s_blocksize);
1110 blocks--; // Decrease the amount of blocks that need to be
1111 // allocated
1112 continue; // Go to the next buffer
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001115 if (!itembuf || /* if first iteration */
1116 item_pos >= ih_item_len(ih) / UNFM_P_SIZE) { /* or if we progressed past the
1117 current unformatted_item */
1118 /* Try to find next item */
1119 res =
1120 search_for_position_by_key(inode->i_sb,
1121 &key, &path);
1122 /* Abort if no more items */
1123 if (res != POSITION_FOUND) {
1124 /* make sure later loops don't use this item */
1125 itembuf = NULL;
1126 item = NULL;
1127 break;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130 /* Update information about current indirect item */
1131 itembuf = get_last_bh(&path);
1132 ih = get_ih(&path);
1133 item = get_item(&path);
1134 item_pos = path.pos_in_item;
1135
1136 RFALSE(!is_indirect_le_ih(ih),
1137 "green-9003: indirect item expected");
1138 }
1139
1140 /* See if there is some block associated with the file
1141 at that position, map the buffer to this block */
1142 if (get_block_num(item, item_pos)) {
1143 map_bh(bh, inode->i_sb,
1144 get_block_num(item, item_pos));
1145 blocks--; // Decrease the amount of blocks that need to be
1146 // allocated
1147 }
1148 item_pos++;
1149 /* Update the key */
1150 set_cpu_key_k_offset(&key,
1151 cpu_key_k_offset(&key) +
1152 inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001155 pathrelse(&path); // Free the path
1156 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* Now zero out unmappend buffers for the first and last pages of
1159 write area or issue read requests if page is mapped. */
1160 /* First page, see if it is not uptodate */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161 if (!PageUptodate(prepared_pages[0])) {
1162 head = page_buffers(prepared_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001164 /* For each buffer in page */
1165 for (bh = head, block_start = 0; bh != head || !block_start;
1166 block_start = block_end, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 if (!bh)
1169 reiserfs_panic(inode->i_sb,
1170 "green-9002: Allocated but absent buffer for a page?");
1171 /* Find where this buffer ends */
1172 block_end = block_start + inode->i_sb->s_blocksize;
1173 if (block_end <= from)
1174 /* if this buffer is before requested data to map, skip it */
1175 continue;
1176 if (block_start < from) { /* Aha, our partial buffer */
1177 if (buffer_mapped(bh)) { /* If it is mapped, we need to
1178 issue READ request for it to
1179 not loose data */
1180 ll_rw_block(READ, 1, &bh);
1181 *wait_bh++ = bh;
1182 } else { /* Not mapped, zero it */
1183 char *kaddr =
1184 kmap_atomic(prepared_pages[0],
1185 KM_USER0);
1186 memset(kaddr + block_start, 0,
1187 from - block_start);
1188 kunmap_atomic(kaddr, KM_USER0);
Alexey Dobriyande21c572006-12-06 20:38:02 -08001189 flush_dcache_page(prepared_pages[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001190 set_buffer_uptodate(bh);
1191 }
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 /* Last page, see if it is not uptodate, or if the last page is past the end of the file. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 if (!PageUptodate(prepared_pages[num_pages - 1]) ||
1198 ((pos + write_bytes) >> PAGE_CACHE_SHIFT) >
1199 (inode->i_size >> PAGE_CACHE_SHIFT)) {
1200 head = page_buffers(prepared_pages[num_pages - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001202 /* for each buffer in page */
1203 for (bh = head, block_start = 0; bh != head || !block_start;
1204 block_start = block_end, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001206 if (!bh)
1207 reiserfs_panic(inode->i_sb,
1208 "green-9002: Allocated but absent buffer for a page?");
1209 /* Find where this buffer ends */
1210 block_end = block_start + inode->i_sb->s_blocksize;
1211 if (block_start >= to)
1212 /* if this buffer is after requested data to map, skip it */
1213 break;
1214 if (block_end > to) { /* Aha, our partial buffer */
1215 if (buffer_mapped(bh)) { /* If it is mapped, we need to
1216 issue READ request for it to
1217 not loose data */
1218 ll_rw_block(READ, 1, &bh);
1219 *wait_bh++ = bh;
1220 } else { /* Not mapped, zero it */
1221 char *kaddr =
1222 kmap_atomic(prepared_pages
1223 [num_pages - 1],
1224 KM_USER0);
1225 memset(kaddr + to, 0, block_end - to);
1226 kunmap_atomic(kaddr, KM_USER0);
Alexey Dobriyande21c572006-12-06 20:38:02 -08001227 flush_dcache_page(prepared_pages[num_pages - 1]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001228 set_buffer_uptodate(bh);
1229 }
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001234 /* Wait for read requests we made to happen, if necessary */
1235 while (wait_bh > wait) {
1236 wait_on_buffer(*--wait_bh);
1237 if (!buffer_uptodate(*wait_bh)) {
1238 res = -EIO;
1239 goto failed_read;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001243 return blocks;
1244 failed_page_grabbing:
1245 num_pages = i;
1246 failed_read:
1247 reiserfs_unprepare_pages(prepared_pages, num_pages);
1248 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/* Write @count bytes at position @ppos in a file indicated by @file
1252 from the buffer @buf.
1253
1254 generic_file_write() is only appropriate for filesystems that are not seeking to optimize performance and want
1255 something simple that works. It is not for serious use by general purpose filesystems, excepting the one that it was
1256 written for (ext2/3). This is for several reasons:
1257
1258 * It has no understanding of any filesystem specific optimizations.
1259
1260 * It enters the filesystem repeatedly for each page that is written.
1261
1262 * It depends on reiserfs_get_block() function which if implemented by reiserfs performs costly search_by_key
1263 * operation for each page it is supplied with. By contrast reiserfs_file_write() feeds as much as possible at a time
1264 * to reiserfs which allows for fewer tree traversals.
1265
1266 * Each indirect pointer insertion takes a lot of cpu, because it involves memory moves inside of blocks.
1267
1268 * Asking the block allocation code for blocks one at a time is slightly less efficient.
1269
1270 All of these reasons for not using only generic file write were understood back when reiserfs was first miscoded to
1271 use it, but we were in a hurry to make code freeze, and so it couldn't be revised then. This new code should make
1272 things right finally.
1273
1274 Future Features: providing search_by_key with hints.
1275
1276*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277static ssize_t reiserfs_file_write(struct file *file, /* the file we are going to write into */
1278 const char __user * buf, /* pointer to user supplied data
1279 (in userspace) */
1280 size_t count, /* amount of bytes to write */
1281 loff_t * ppos /* pointer to position in file that we start writing at. Should be updated to
1282 * new current position before returning. */
1283 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285 size_t already_written = 0; // Number of bytes already written to the file.
1286 loff_t pos; // Current position in the file.
1287 ssize_t res; // return value of various functions that we call.
1288 int err = 0;
1289 struct inode *inode = file->f_dentry->d_inode; // Inode of the file that we are writing to.
1290 /* To simplify coding at this time, we store
1291 locked pages in array for now */
1292 struct page *prepared_pages[REISERFS_WRITE_PAGES_AT_A_TIME];
1293 struct reiserfs_transaction_handle th;
1294 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jeff Mahoneyfa385be2006-02-01 03:06:51 -08001296 /* If a filesystem is converted from 3.5 to 3.6, we'll have v3.5 items
1297 * lying around (most of the disk, in fact). Despite the filesystem
1298 * now being a v3.6 format, the old items still can't support large
1299 * file sizes. Catch this case here, as the rest of the VFS layer is
1300 * oblivious to the different limitations between old and new items.
1301 * reiserfs_setattr catches this for truncates. This chunk is lifted
1302 * from generic_write_checks. */
1303 if (get_inode_item_key_version (inode) == KEY_FORMAT_3_5 &&
1304 *ppos + count > MAX_NON_LFS) {
1305 if (*ppos >= MAX_NON_LFS) {
1306 send_sig(SIGXFSZ, current, 0);
1307 return -EFBIG;
1308 }
1309 if (count > MAX_NON_LFS - (unsigned long)*ppos)
1310 count = MAX_NON_LFS - (unsigned long)*ppos;
1311 }
1312
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001313 if (file->f_flags & O_DIRECT) { // Direct IO needs treatment
1314 ssize_t result, after_file_end = 0;
1315 if ((*ppos + count >= inode->i_size)
1316 || (file->f_flags & O_APPEND)) {
1317 /* If we are appending a file, we need to put this savelink in here.
1318 If we will crash while doing direct io, finish_unfinished will
1319 cut the garbage from the file end. */
1320 reiserfs_write_lock(inode->i_sb);
1321 err =
1322 journal_begin(&th, inode->i_sb,
1323 JOURNAL_PER_BALANCE_CNT);
1324 if (err) {
1325 reiserfs_write_unlock(inode->i_sb);
1326 return err;
1327 }
1328 reiserfs_update_inode_transaction(inode);
1329 add_save_link(&th, inode, 1 /* Truncate */ );
1330 after_file_end = 1;
1331 err =
1332 journal_end(&th, inode->i_sb,
1333 JOURNAL_PER_BALANCE_CNT);
1334 reiserfs_write_unlock(inode->i_sb);
1335 if (err)
1336 return err;
1337 }
Badari Pulavarty027445c2006-09-30 23:28:46 -07001338 result = do_sync_write(file, buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001340 if (after_file_end) { /* Now update i_size and remove the savelink */
1341 struct reiserfs_transaction_handle th;
1342 reiserfs_write_lock(inode->i_sb);
1343 err = journal_begin(&th, inode->i_sb, 1);
1344 if (err) {
1345 reiserfs_write_unlock(inode->i_sb);
1346 return err;
1347 }
1348 reiserfs_update_inode_transaction(inode);
Chris Mason9f037832005-09-13 01:25:17 -07001349 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001350 err = journal_end(&th, inode->i_sb, 1);
1351 if (err) {
1352 reiserfs_write_unlock(inode->i_sb);
1353 return err;
1354 }
1355 err = remove_save_link(inode, 1 /* truncate */ );
1356 reiserfs_write_unlock(inode->i_sb);
1357 if (err)
1358 return err;
1359 }
1360
1361 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001364 if (unlikely((ssize_t) count < 0))
1365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001367 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
1368 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001370 mutex_lock(&inode->i_mutex); // locks the entire file for just us
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001372 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001374 /* Check if we can write to specified region of file, file
1375 is not overly big and this kind of stuff. Adjust pos and
1376 count, if needed */
1377 res = generic_write_checks(file, &pos, &count, 0);
1378 if (res)
1379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001381 if (count == 0)
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 res = remove_suid(file->f_dentry);
1385 if (res)
1386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Christoph Hellwig870f4812006-01-09 20:52:01 -08001388 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001390 // Ok, we are done with all the checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001392 // Now we should start real work
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001394 /* If we are going to write past the file's packed tail or if we are going
1395 to overwrite part of the tail, we need that tail to be converted into
1396 unformatted node */
1397 res = reiserfs_check_for_tail_and_convert(inode, pos, count);
1398 if (res)
1399 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001401 while (count > 0) {
1402 /* This is the main loop in which we running until some error occures
1403 or until we write all of the data. */
1404 size_t num_pages; /* amount of pages we are going to write this iteration */
1405 size_t write_bytes; /* amount of bytes to write during this iteration */
1406 size_t blocks_to_allocate; /* how much blocks we need to allocate for this iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408 /* (pos & (PAGE_CACHE_SIZE-1)) is an idiom for offset into a page of pos */
1409 num_pages = !!((pos + count) & (PAGE_CACHE_SIZE - 1)) + /* round up partial
1410 pages */
1411 ((count +
1412 (pos & (PAGE_CACHE_SIZE - 1))) >> PAGE_CACHE_SHIFT);
1413 /* convert size to amount of
1414 pages */
1415 reiserfs_write_lock(inode->i_sb);
1416 if (num_pages > REISERFS_WRITE_PAGES_AT_A_TIME
1417 || num_pages > reiserfs_can_fit_pages(inode->i_sb)) {
1418 /* If we were asked to write more data than we want to or if there
1419 is not that much space, then we shorten amount of data to write
1420 for this iteration. */
1421 num_pages =
1422 min_t(size_t, REISERFS_WRITE_PAGES_AT_A_TIME,
1423 reiserfs_can_fit_pages(inode->i_sb));
1424 /* Also we should not forget to set size in bytes accordingly */
1425 write_bytes = (num_pages << PAGE_CACHE_SHIFT) -
1426 (pos & (PAGE_CACHE_SIZE - 1));
1427 /* If position is not on the
1428 start of the page, we need
1429 to substract the offset
1430 within page */
1431 } else
1432 write_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001434 /* reserve the blocks to be allocated later, so that later on
1435 we still have the space to write the blocks to */
1436 reiserfs_claim_blocks_to_be_allocated(inode->i_sb,
1437 num_pages <<
1438 (PAGE_CACHE_SHIFT -
1439 inode->i_blkbits));
1440 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001442 if (!num_pages) { /* If we do not have enough space even for a single page... */
1443 if (pos >
1444 inode->i_size + inode->i_sb->s_blocksize -
1445 (pos & (inode->i_sb->s_blocksize - 1))) {
1446 res = -ENOSPC;
1447 break; // In case we are writing past the end of the last file block, break.
1448 }
1449 // Otherwise we are possibly overwriting the file, so
1450 // let's set write size to be equal or less than blocksize.
1451 // This way we get it correctly for file holes.
1452 // But overwriting files on absolutelly full volumes would not
1453 // be very efficient. Well, people are not supposed to fill
1454 // 100% of disk space anyway.
1455 write_bytes =
1456 min_t(size_t, count,
1457 inode->i_sb->s_blocksize -
1458 (pos & (inode->i_sb->s_blocksize - 1)));
1459 num_pages = 1;
1460 // No blocks were claimed before, so do it now.
1461 reiserfs_claim_blocks_to_be_allocated(inode->i_sb,
1462 1 <<
1463 (PAGE_CACHE_SHIFT
1464 -
1465 inode->
1466 i_blkbits));
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001469 /* Prepare for writing into the region, read in all the
1470 partially overwritten pages, if needed. And lock the pages,
1471 so that nobody else can access these until we are done.
1472 We get number of actual blocks needed as a result. */
Vladimir V. Savelievc499ec22006-03-02 02:54:39 -08001473 res = reiserfs_prepare_file_region_for_write(inode, pos,
1474 num_pages,
1475 write_bytes,
1476 prepared_pages);
1477 if (res < 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001478 reiserfs_release_claimed_blocks(inode->i_sb,
1479 num_pages <<
1480 (PAGE_CACHE_SHIFT -
1481 inode->i_blkbits));
1482 break;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Vladimir V. Savelievc499ec22006-03-02 02:54:39 -08001485 blocks_to_allocate = res;
1486
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487 /* First we correct our estimate of how many blocks we need */
1488 reiserfs_release_claimed_blocks(inode->i_sb,
1489 (num_pages <<
1490 (PAGE_CACHE_SHIFT -
1491 inode->i_sb->
1492 s_blocksize_bits)) -
1493 blocks_to_allocate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001495 if (blocks_to_allocate > 0) { /*We only allocate blocks if we need to */
1496 /* Fill in all the possible holes and append the file if needed */
1497 res =
1498 reiserfs_allocate_blocks_for_region(&th, inode, pos,
1499 num_pages,
1500 write_bytes,
1501 prepared_pages,
1502 blocks_to_allocate);
1503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001505 /* well, we have allocated the blocks, so it is time to free
1506 the reservation we made earlier. */
1507 reiserfs_release_claimed_blocks(inode->i_sb,
1508 blocks_to_allocate);
1509 if (res) {
1510 reiserfs_unprepare_pages(prepared_pages, num_pages);
1511 break;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514/* NOTE that allocating blocks and filling blocks can be done in reverse order
1515 and probably we would do that just to get rid of garbage in files after a
1516 crash */
1517
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001518 /* Copy data from user-supplied buffer to file's pages */
1519 res =
1520 reiserfs_copy_from_user_to_file_region(pos, num_pages,
1521 write_bytes,
1522 prepared_pages, buf);
1523 if (res) {
1524 reiserfs_unprepare_pages(prepared_pages, num_pages);
1525 break;
1526 }
1527
1528 /* Send the pages to disk and unlock them. */
1529 res =
1530 reiserfs_submit_file_region_for_write(&th, inode, pos,
1531 num_pages,
1532 write_bytes,
1533 prepared_pages);
1534 if (res)
1535 break;
1536
1537 already_written += write_bytes;
1538 buf += write_bytes;
1539 *ppos = pos += write_bytes;
1540 count -= write_bytes;
Alexander Zarochentsev59308602006-03-25 03:07:16 -08001541 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001544 /* this is only true on error */
1545 if (th.t_trans_id) {
1546 reiserfs_write_lock(inode->i_sb);
1547 err = journal_end(&th, th.t_super, th.t_blocks_allocated);
1548 reiserfs_write_unlock(inode->i_sb);
1549 if (err) {
1550 res = err;
1551 goto out;
1552 }
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Jeff Mahoney619d5d82006-03-25 03:07:00 -08001555 if (likely(res >= 0) &&
1556 (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))))
1557 res = generic_osync_inode(inode, file->f_mapping,
1558 OSYNC_METADATA | OSYNC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001560 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001561 reiserfs_async_progress_wait(inode->i_sb);
1562 return (already_written != 0) ? already_written : res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001564 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001565 mutex_unlock(&inode->i_mutex); // unlock the file on exit.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001566 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001569const struct file_operations reiserfs_file_operations = {
Badari Pulavarty027445c2006-09-30 23:28:46 -07001570 .read = do_sync_read,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001571 .write = reiserfs_file_write,
1572 .ioctl = reiserfs_ioctl,
David Howells52b499c2006-08-29 19:06:18 +01001573#ifdef CONFIG_COMPAT
1574 .compat_ioctl = reiserfs_compat_ioctl,
1575#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001576 .mmap = generic_file_mmap,
Jeff Mahoney5a2618e2006-09-30 23:28:44 -07001577 .open = generic_file_open,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001578 .release = reiserfs_file_release,
1579 .fsync = reiserfs_sync_file,
1580 .sendfile = generic_file_sendfile,
1581 .aio_read = generic_file_aio_read,
Alexey Dobriyan9637f282006-06-26 00:24:57 -07001582 .aio_write = generic_file_aio_write,
Jens Axboe5274f052006-03-30 15:15:30 +02001583 .splice_read = generic_file_splice_read,
1584 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585};
1586
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001587struct inode_operations reiserfs_file_inode_operations = {
1588 .truncate = reiserfs_vfs_truncate_file,
1589 .setattr = reiserfs_setattr,
1590 .setxattr = reiserfs_setxattr,
1591 .getxattr = reiserfs_getxattr,
1592 .listxattr = reiserfs_listxattr,
1593 .removexattr = reiserfs_removexattr,
1594 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595};