2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
6 #include <linux/time.h>
7 #include <linux/reiserfs_fs.h>
8 #include <linux/smp_lock.h>
9 #include <asm/uaccess.h>
10 #include <linux/pagemap.h>
13 ** We pack the tails of files on file close, not at the time they are written.
14 ** This implies an unnecessary copy of the tail and an unnecessary indirect item
15 ** insertion/balancing, for files that are written in one write.
16 ** It avoids unnecessary tail packings (balances) for files that are written in
17 ** multiple writes and are small enough to have tails.
19 ** file_release is called by the VFS layer when the file is closed. If
20 ** this is the last open file descriptor, and the file
21 ** small enough to have a tail, and the tail is currently in an
22 ** unformatted node, the tail is converted back into a direct item.
24 ** We use reiserfs_truncate_file to pack the tail, since it already has
25 ** all the conditions coded.
27 static int reiserfs_file_release (struct inode * inode, struct file * filp)
30 struct reiserfs_transaction_handle th ;
33 if (!S_ISREG (inode->i_mode))
36 /* fast out for when nothing needs to be done */
37 if ((atomic_read(&inode->i_count) > 1 ||
38 !(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
39 !tail_has_to_be_packed(inode)) &&
40 REISERFS_I(inode)->i_prealloc_count <= 0) {
44 reiserfs_write_lock(inode->i_sb);
46 journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3) ;
47 reiserfs_update_inode_transaction(inode) ;
49 #ifdef REISERFS_PREALLOCATE
50 reiserfs_discard_prealloc (&th, inode);
52 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3) ;
54 if (atomic_read(&inode->i_count) <= 1 &&
55 (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
56 tail_has_to_be_packed (inode)) {
57 /* if regular file is released by last holder and it has been
58 appended (we append by unformatted node only) or its direct
59 item(s) had to be converted, then it may have to be
60 indirect2direct converted */
61 windex = push_journal_writer("file_release") ;
62 reiserfs_truncate_file(inode, 0) ;
63 pop_journal_writer(windex) ;
66 reiserfs_write_unlock(inode->i_sb);
70 static void reiserfs_vfs_truncate_file(struct inode *inode) {
71 reiserfs_truncate_file(inode, 1) ;
74 /* Sync a reiserfs file. */
77 * FIXME: sync_mapping_buffers() never has anything to sync. Can
81 static int reiserfs_sync_file(
82 struct file * p_s_filp,
83 struct dentry * p_s_dentry,
86 struct inode * p_s_inode = p_s_dentry->d_inode;
89 reiserfs_write_lock(p_s_inode->i_sb);
91 if (!S_ISREG(p_s_inode->i_mode))
94 n_err = sync_mapping_buffers(p_s_inode->i_mapping) ;
95 reiserfs_commit_for_inode(p_s_inode) ;
96 reiserfs_write_unlock(p_s_inode->i_sb);
97 return ( n_err < 0 ) ? -EIO : 0;
100 static int reiserfs_setattr(struct dentry *dentry, struct iattr *attr) {
101 struct inode *inode = dentry->d_inode ;
103 reiserfs_write_lock(inode->i_sb);
104 if (attr->ia_valid & ATTR_SIZE) {
105 /* version 2 items will be caught by the s_maxbytes check
106 ** done for us in vmtruncate
108 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_5 &&
109 attr->ia_size > MAX_NON_LFS) {
113 /* fill in hole pointers in the expanding truncate case. */
114 if (attr->ia_size > inode->i_size) {
115 error = generic_cont_expand(inode, attr->ia_size) ;
116 if (REISERFS_I(inode)->i_prealloc_count > 0) {
117 struct reiserfs_transaction_handle th ;
118 /* we're changing at most 2 bitmaps, inode + super */
119 journal_begin(&th, inode->i_sb, 4) ;
120 reiserfs_discard_prealloc (&th, inode);
121 journal_end(&th, inode->i_sb, 4) ;
128 if ((((attr->ia_valid & ATTR_UID) && (attr->ia_uid & ~0xffff)) ||
129 ((attr->ia_valid & ATTR_GID) && (attr->ia_gid & ~0xffff))) &&
130 (get_inode_sd_version (inode) == STAT_DATA_V1)) {
131 /* stat data of format v3.5 has 16 bit uid and gid */
136 error = inode_change_ok(inode, attr) ;
138 inode_setattr(inode, attr) ;
141 reiserfs_write_unlock(inode->i_sb);
145 /* I really do not want to play with memory shortage right now, so
146 to simplify the code, we are not going to write more than this much pages at
147 a time. This still should considerably improve performance compared to 4k
148 at a time case. This is 32 pages of 4k size. */
149 #define REISERFS_WRITE_PAGES_AT_A_TIME (128 * 1024) / PAGE_CACHE_SIZE
151 /* Allocates blocks for a file to fulfil write request.
152 Maps all unmapped but prepared pages from the list.
153 Updates metadata with newly allocated blocknumbers as needed */
154 int reiserfs_allocate_blocks_for_region(
155 struct inode *inode, /* Inode we work with */
156 loff_t pos, /* Writing position */
157 int num_pages, /* number of pages write going
159 int write_bytes, /* amount of bytes to write */
160 struct page **prepared_pages, /* array of
163 int blocks_to_allocate /* Amount of blocks we
165 fit the data into file
169 struct cpu_key key; // cpu key of item that we are going to deal with
170 struct item_head *ih; // pointer to item head that we are going to deal with
171 struct buffer_head *bh; // Buffer head that contains items that we are going to deal with
172 struct reiserfs_transaction_handle th; // transaction handle for transaction we are going to create.
173 __u32 * item; // pointer to item we are going to deal with
174 INITIALIZE_PATH(path); // path to item, that we are going to deal with.
175 b_blocknr_t allocated_blocks[blocks_to_allocate]; // Pointer to a place where allocated blocknumbers would be stored. Right now statically allocated, later that will change.
176 reiserfs_blocknr_hint_t hint; // hint structure for block allocator.
177 size_t res; // return value of various functions that we call.
178 int curr_block; // current block used to keep track of unmapped blocks.
179 int i; // loop counter
180 int itempos; // position in item
181 unsigned int from = (pos & (PAGE_CACHE_SIZE - 1)); // writing position in
183 unsigned int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1; /* last modified byte offset in last page */
184 __u64 hole_size ; // amount of blocks for a file hole, if it needed to be created.
185 int modifying_this_item = 0; // Flag for items traversal code to keep track
186 // of the fact that we already prepared
187 // current block for journal
190 RFALSE(!blocks_to_allocate, "green-9004: tried to allocate zero blocks?");
192 /* First we compose a key to point at the writing position, we want to do
193 that outside of any locking region. */
194 make_cpu_key (&key, inode, pos+1, TYPE_ANY, 3/*key length*/);
196 /* If we came here, it means we absolutely need to open a transaction,
197 since we need to allocate some blocks */
198 reiserfs_write_lock(inode->i_sb); // Journaling stuff and we need that.
199 journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3 + 1); // Wish I know if this number enough
200 reiserfs_update_inode_transaction(inode) ;
202 /* Look for the in-tree position of our write, need path for block allocator */
203 res = search_for_position_by_key(inode->i_sb, &key, &path);
204 if ( res == IO_ERROR ) {
209 /* Allocate blocks */
210 /* First fill in "hint" structure for block allocator */
211 hint.th = &th; // transaction handle.
212 hint.path = &path; // Path, so that block allocator can determine packing locality or whatever it needs to determine.
213 hint.inode = inode; // Inode is needed by block allocator too.
214 hint.search_start = 0; // We have no hint on where to search free blocks for block allocator.
215 hint.key = key.on_disk_key; // on disk key of file.
216 hint.block = inode->i_blocks>>(inode->i_sb->s_blocksize_bits-9); // Number of disk blocks this file occupies already.
217 hint.formatted_node = 0; // We are allocating blocks for unformatted node.
218 hint.preallocate = 0; // We do not do any preallocation for now.
220 /* Call block allocator to allocate blocks */
221 res = reiserfs_allocate_blocknrs(&hint, allocated_blocks, blocks_to_allocate, blocks_to_allocate);
222 if ( res != CARRY_ON ) {
223 if ( res == NO_DISK_SPACE ) {
224 /* We flush the transaction in case of no space. This way some
225 blocks might become free */
226 SB_JOURNAL(inode->i_sb)->j_must_wait = 1;
227 restart_transaction(&th, inode, &path);
229 /* We might have scheduled, so search again */
230 res = search_for_position_by_key(inode->i_sb, &key, &path);
231 if ( res == IO_ERROR ) {
236 /* update changed info for hint structure. */
237 res = reiserfs_allocate_blocknrs(&hint, allocated_blocks, blocks_to_allocate, blocks_to_allocate);
238 if ( res != CARRY_ON ) {
251 // Too bad, I have not found any way to convert a given region from
252 // cpu format to little endian format
255 for ( i = 0; i < blocks_to_allocate ; i++)
256 allocated_blocks[i]=cpu_to_le32(allocated_blocks[i]);
260 /* Blocks allocating well might have scheduled and tree might have changed,
261 let's search the tree again */
262 /* find where in the tree our write should go */
263 res = search_for_position_by_key(inode->i_sb, &key, &path);
264 if ( res == IO_ERROR ) {
266 goto error_exit_free_blocks;
269 bh = get_last_bh( &path ); // Get a bufferhead for last element in path.
270 ih = get_ih( &path ); // Get a pointer to last item head in path.
271 item = get_item( &path ); // Get a pointer to last item in path
273 /* Let's see what we have found */
274 if ( res != POSITION_FOUND ) { /* position not found, this means that we
275 might need to append file with holes
277 // Since we are writing past the file's end, we need to find out if
278 // there is a hole that needs to be inserted before our writing
279 // position, and how many blocks it is going to cover (we need to
280 // populate pointers to file blocks representing the hole with zeros)
282 hole_size = (pos + 1 - (le_key_k_offset( get_inode_item_key_version(inode), &(ih->ih_key))+op_bytes_number(ih, inode->i_sb->s_blocksize))) >> inode->i_sb->s_blocksize_bits;
284 if ( hole_size > 0 ) {
285 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.
286 /* area filled with zeroes, to supply as list of zero blocknumbers
287 We allocate it outside of loop just in case loop would spin for
288 several iterations. */
289 char *zeros = kmalloc(to_paste*UNFM_P_SIZE, GFP_ATOMIC); // We cannot insert more than MAX_ITEM_LEN bytes anyway.
292 goto error_exit_free_blocks;
294 memset ( zeros, 0, to_paste*UNFM_P_SIZE);
296 to_paste = min_t(__u64, hole_size, MAX_ITEM_LEN(inode->i_sb->s_blocksize)/UNFM_P_SIZE );
297 if ( is_indirect_le_ih(ih) ) {
298 /* Ok, there is existing indirect item already. Need to append it */
299 /* Calculate position past inserted item */
300 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);
301 res = reiserfs_paste_into_item( &th, &path, &key, (char *)zeros, UNFM_P_SIZE*to_paste);
304 goto error_exit_free_blocks;
306 } else if ( is_statdata_le_ih(ih) ) {
307 /* No existing item, create it */
308 /* item head for new item */
309 struct item_head ins_ih;
311 /* create a key for our new item */
312 make_cpu_key( &key, inode, 1, TYPE_INDIRECT, 3);
314 /* Create new item head for our new item */
315 make_le_item_head (&ins_ih, &key, key.version, 1,
316 TYPE_INDIRECT, to_paste*UNFM_P_SIZE,
319 /* Find where such item should live in the tree */
320 res = search_item (inode->i_sb, &key, &path);
321 if ( res != ITEM_NOT_FOUND ) {
322 /* item should not exist, otherwise we have error */
323 if ( res != -ENOSPC ) {
324 reiserfs_warning ("green-9008: search_by_key (%K) returned %d\n",
329 goto error_exit_free_blocks;
331 res = reiserfs_insert_item( &th, &path, &key, &ins_ih, (char *)zeros);
333 reiserfs_panic(inode->i_sb, "green-9011: Unexpected key type %K\n", &key);
337 goto error_exit_free_blocks;
339 /* Now we want to check if transaction is too full, and if it is
340 we restart it. This will also free the path. */
341 if (journal_transaction_should_end(&th, th.t_blocks_allocated))
342 restart_transaction(&th, inode, &path);
344 /* Well, need to recalculate path and stuff */
345 set_cpu_key_k_offset( &key, cpu_key_k_offset(&key) + (to_paste << inode->i_blkbits));
346 res = search_for_position_by_key(inode->i_sb, &key, &path);
347 if ( res == IO_ERROR ) {
350 goto error_exit_free_blocks;
352 bh=get_last_bh(&path);
354 item = get_item(&path);
355 hole_size -= to_paste;
356 } while ( hole_size );
361 // Go through existing indirect items first
362 // replace all zeroes with blocknumbers from list
363 // Note that if no corresponding item was found, by previous search,
364 // it means there are no existing in-tree representation for file area
365 // we are going to overwrite, so there is nothing to scan through for holes.
366 for ( curr_block = 0, itempos = path.pos_in_item ; curr_block < blocks_to_allocate && res == POSITION_FOUND ; ) {
368 if ( itempos >= ih_item_len(ih)/UNFM_P_SIZE ) {
369 /* We run out of data in this indirect item, let's look for another
371 /* First if we are already modifying current item, log it */
372 if ( modifying_this_item ) {
373 journal_mark_dirty (&th, inode->i_sb, bh);
374 modifying_this_item = 0;
376 /* Then set the key to look for a new indirect item (offset of old
377 item is added to old item length */
378 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));
379 /* Search ofor position of new key in the tree. */
380 res = search_for_position_by_key(inode->i_sb, &key, &path);
381 if ( res == IO_ERROR) {
383 goto error_exit_free_blocks;
385 bh=get_last_bh(&path);
387 item = get_item(&path);
388 itempos = path.pos_in_item;
389 continue; // loop to check all kinds of conditions and so on.
391 /* Ok, we have correct position in item now, so let's see if it is
392 representing file hole (blocknumber is zero) and fill it if needed */
393 if ( !item[itempos] ) {
394 /* Ok, a hole. Now we need to check if we already prepared this
395 block to be journaled */
396 while ( !modifying_this_item ) { // loop until succeed
397 /* Well, this item is not journaled yet, so we must prepare
398 it for journal first, before we can change it */
399 struct item_head tmp_ih; // We copy item head of found item,
400 // here to detect if fs changed under
401 // us while we were preparing for
403 int fs_gen; // We store fs generation here to find if someone
404 // changes fs under our feet
406 copy_item_head (&tmp_ih, ih); // Remember itemhead
407 fs_gen = get_generation (inode->i_sb); // remember fs generation
408 reiserfs_prepare_for_journal(inode->i_sb, bh, 1); // Prepare a buffer within which indirect item is stored for changing.
409 if (fs_changed (fs_gen, inode->i_sb) && item_moved (&tmp_ih, &path)) {
410 // Sigh, fs was changed under us, we need to look for new
411 // location of item we are working with
413 /* unmark prepaerd area as journaled and search for it's
415 reiserfs_restore_prepared_buffer(inode->i_sb, bh);
416 res = search_for_position_by_key(inode->i_sb, &key, &path);
417 if ( res == IO_ERROR) {
419 goto error_exit_free_blocks;
421 bh=get_last_bh(&path);
423 item = get_item(&path);
424 // Itempos is still the same
427 modifying_this_item = 1;
429 item[itempos] = allocated_blocks[curr_block]; // Assign new block
435 if ( modifying_this_item ) { // We need to log last-accessed block, if it
436 // was modified, but not logged yet.
437 journal_mark_dirty (&th, inode->i_sb, bh);
440 if ( curr_block < blocks_to_allocate ) {
441 // Oh, well need to append to indirect item, or to create indirect item
442 // if there weren't any
443 if ( is_indirect_le_ih(ih) ) {
444 // Existing indirect item - append. First calculate key for append
445 // position. We do not need to recalculate path as it should
446 // already point to correct place.
447 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);
448 res = reiserfs_paste_into_item( &th, &path, &key, (char *)(allocated_blocks+curr_block), UNFM_P_SIZE*(blocks_to_allocate-curr_block));
450 goto error_exit_free_blocks;
452 } else if (is_statdata_le_ih(ih) ) {
453 // Last found item was statdata. That means we need to create indirect item.
454 struct item_head ins_ih; /* itemhead for new item */
456 /* create a key for our new item */
457 make_cpu_key( &key, inode, 1, TYPE_INDIRECT, 3); // Position one,
462 /* Create new item head for our new item */
463 make_le_item_head (&ins_ih, &key, key.version, 1, TYPE_INDIRECT,
464 (blocks_to_allocate-curr_block)*UNFM_P_SIZE,
466 /* Find where such item should live in the tree */
467 res = search_item (inode->i_sb, &key, &path);
468 if ( res != ITEM_NOT_FOUND ) {
469 /* Well, if we have found such item already, or some error
470 occured, we need to warn user and return error */
471 if ( res != -ENOSPC ) {
472 reiserfs_warning ("green-9009: search_by_key (%K) returned %d\n",
476 goto error_exit_free_blocks;
478 /* Insert item into the tree with the data as its body */
479 res = reiserfs_insert_item( &th, &path, &key, &ins_ih, (char *)(allocated_blocks+curr_block));
481 reiserfs_panic(inode->i_sb, "green-9010: unexpected item type for key %K\n",&key);
485 /* Now the final thing, if we have grew the file, we must update it's size*/
486 if ( pos + write_bytes > inode->i_size) {
487 inode->i_size = pos + write_bytes; // Set new size
490 /* Amount of on-disk blocks used by file have changed, update it */
491 inode->i_blocks += blocks_to_allocate << (inode->i_blkbits - 9);
492 reiserfs_update_sd(&th, inode); // And update on-disk metadata
493 // finish all journal stuff now, We are not going to play with metadata
496 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3 + 1);
497 reiserfs_write_unlock(inode->i_sb);
499 // go through all the pages/buffers and map the buffers to newly allocated
500 // blocks (so that system knows where to write these pages later).
502 for ( i = 0; i < num_pages ; i++ ) {
503 struct page *page=prepared_pages[i]; //current page
504 struct buffer_head *head = page_buffers(page);// first buffer for a page
505 int block_start, block_end; // in-page offsets for buffers.
507 if (!page_buffers(page))
508 reiserfs_panic(inode->i_sb, "green-9005: No buffers for prepared page???");
510 /* For each buffer in page */
511 for(bh = head, block_start = 0; bh != head || !block_start;
512 block_start=block_end, bh = bh->b_this_page) {
514 reiserfs_panic(inode->i_sb, "green-9006: Allocated but absent buffer for a page?");
515 block_end = block_start+inode->i_sb->s_blocksize;
516 if (i == 0 && block_end <= from )
517 /* if this buffer is before requested data to map, skip it */
519 if (i == num_pages - 1 && block_start >= to)
520 /* If this buffer is after requested data to map, abort
521 processing of current page */
524 if ( !buffer_mapped(bh) ) { // Ok, unmapped buffer, need to map it
525 map_bh( bh, inode->i_sb, le32_to_cpu(allocated_blocks[curr_block]));
531 RFALSE( curr_block > blocks_to_allocate, "green-9007: Used too many blocks? weird");
535 // Need to deal with transaction here.
536 error_exit_free_blocks:
539 for( i = 0; i < blocks_to_allocate; i++ )
540 reiserfs_free_block( &th, le32_to_cpu(allocated_blocks[i]));
543 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 3 + 1);
544 reiserfs_write_unlock(inode->i_sb);
549 /* Unlock pages prepared by reiserfs_prepare_file_region_for_write */
550 void reiserfs_unprepare_pages(struct page **prepared_pages, /* list of locked pages */
551 int num_pages /* amount of pages */) {
552 int i; // loop counter
554 for (i=0; i < num_pages ; i++) {
555 struct page *page = prepared_pages[i];
557 try_to_free_buffers(page);
560 page_cache_release(page);
564 /* This function will copy data from userspace to specified pages within
565 supplied byte range */
566 int reiserfs_copy_from_user_to_file_region(
567 loff_t pos, /* In-file position */
568 int num_pages, /* Number of pages affected */
569 int write_bytes, /* Amount of bytes to write */
570 struct page **prepared_pages, /* pointer to
574 const char *buf /* Pointer to user-supplied
578 long page_fault=0; // status of copy_from_user.
579 int i; // loop counter.
580 int offset; // offset in page
582 for ( i = 0, offset = (pos & (PAGE_CACHE_SIZE-1)); i < num_pages ; i++,offset=0) {
583 int count = min_t(int,PAGE_CACHE_SIZE-offset,write_bytes); // How much of bytes to write to this page
584 struct page *page=prepared_pages[i]; // Current page we process.
586 fault_in_pages_readable( buf, count);
588 /* Copy data from userspace to the current page */
590 page_fault = __copy_from_user(page_address(page)+offset, buf, count); // Copy the data.
591 /* Flush processor's dcache for this page */
592 flush_dcache_page(page);
598 break; // Was there a fault? abort.
601 return page_fault?-EFAULT:0;
606 /* Submit pages for write. This was separated from actual file copying
607 because we might want to allocate block numbers in-between.
608 This function assumes that caller will adjust file size to correct value. */
609 int reiserfs_submit_file_region_for_write(
610 loff_t pos, /* Writing position offset */
611 int num_pages, /* Number of pages to write */
612 int write_bytes, /* number of bytes to write */
613 struct page **prepared_pages /* list of pages */
616 int status; // return status of block_commit_write.
617 int retval = 0; // Return value we are going to return.
618 int i; // loop counter
619 int offset; // Writing offset in page.
621 for ( i = 0, offset = (pos & (PAGE_CACHE_SIZE-1)); i < num_pages ; i++,offset=0) {
622 int count = min_t(int,PAGE_CACHE_SIZE-offset,write_bytes); // How much of bytes to write to this page
623 struct page *page=prepared_pages[i]; // Current page we process.
625 status = block_commit_write(page, offset, offset+count);
627 retval = status; // To not overcomplicate matters We are going to
628 // submit all the pages even if there was error.
629 // we only remember error status to report it on
632 SetPageReferenced(page);
633 unlock_page(page); // We unlock the page as it was locked by earlier call
634 // to grab_cache_page
635 page_cache_release(page);
640 /* Look if passed writing region is going to touch file's tail
641 (if it is present). And if it is, convert the tail to unformatted node */
642 int reiserfs_check_for_tail_and_convert( struct inode *inode, /* inode to deal with */
643 loff_t pos, /* Writing position */
644 int write_bytes /* amount of bytes to write */
647 INITIALIZE_PATH(path); // needed for search_for_position
648 struct cpu_key key; // Key that would represent last touched writing byte.
649 struct item_head *ih; // item header of found block;
650 int res; // Return value of various functions we call.
651 int cont_expand_offset; // We will put offset for generic_cont_expand here
652 // This can be int just because tails are created
653 // only for small files.
655 /* this embodies a dependency on a particular tail policy */
656 if ( inode->i_size >= inode->i_sb->s_blocksize*4 ) {
657 /* such a big files do not have tails, so we won't bother ourselves
658 to look for tails, simply return */
662 reiserfs_write_lock(inode->i_sb);
663 /* find the item containing the last byte to be written, or if
664 * writing past the end of the file then the last item of the
665 * file (and then we check its type). */
666 make_cpu_key (&key, inode, pos+write_bytes+1, TYPE_ANY, 3/*key length*/);
667 res = search_for_position_by_key(inode->i_sb, &key, &path);
668 if ( res == IO_ERROR ) {
669 reiserfs_write_unlock(inode->i_sb);
674 if ( is_direct_le_ih(ih) ) {
675 /* Ok, closest item is file tail (tails are stored in "direct"
676 * items), so we need to unpack it. */
677 /* To not overcomplicate matters, we just call generic_cont_expand
678 which will in turn call other stuff and finally will boil down to
679 reiserfs_get_block() that would do necessary conversion. */
680 cont_expand_offset = le_key_k_offset(get_inode_item_key_version(inode), &(ih->ih_key));
682 res = generic_cont_expand( inode, cont_expand_offset);
686 reiserfs_write_unlock(inode->i_sb);
690 /* This function locks pages starting from @pos for @inode.
691 @num_pages pages are locked and stored in
692 @prepared_pages array. Also buffers are allocated for these pages.
693 First and last page of the region is read if it is overwritten only
694 partially. If last page did not exist before write (file hole or file
695 append), it is zeroed, then.
696 Returns number of unallocated blocks that should be allocated to cover
698 int reiserfs_prepare_file_region_for_write(
699 struct inode *inode /* Inode of the file */,
700 loff_t pos, /* position in the file */
701 int num_pages, /* number of pages to
703 int write_bytes, /* Amount of bytes to be
706 struct page **prepared_pages /* pointer to array
711 int res=0; // Return values of different functions we call.
712 unsigned long index = pos >> PAGE_CACHE_SHIFT; // Offset in file in pages.
713 int from = (pos & (PAGE_CACHE_SIZE - 1)); // Writing offset in first page
714 int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1;
715 /* offset of last modified byte in last
717 struct address_space *mapping = inode->i_mapping; // Pages are mapped here.
718 int i; // Simple counter
719 int blocks = 0; /* Return value (blocks that should be allocated) */
720 struct buffer_head *bh, *head; // Current bufferhead and first bufferhead
722 unsigned block_start, block_end; // Starting and ending offsets of current
723 // buffer in the page.
724 struct buffer_head *wait[2], **wait_bh=wait; // Buffers for page, if
725 // Page appeared to be not up
726 // to date. Note how we have
727 // at most 2 buffers, this is
728 // because we at most may
729 // partially overwrite two
730 // buffers for one page. One at // the beginning of write area
731 // and one at the end.
732 // Everything inthe middle gets // overwritten totally.
734 struct cpu_key key; // cpu key of item that we are going to deal with
735 struct item_head *ih = NULL; // pointer to item head that we are going to deal with
736 struct buffer_head *itembuf=NULL; // Buffer head that contains items that we are going to deal with
737 INITIALIZE_PATH(path); // path to item, that we are going to deal with.
738 __u32 * item=0; // pointer to item we are going to deal with
739 int item_pos=-1; /* Position in indirect item */
742 if ( num_pages < 1 ) {
743 reiserfs_warning("green-9001: reiserfs_prepare_file_region_for_write called with zero number of pages to process\n");
747 /* We have 2 loops for pages. In first loop we grab and lock the pages, so
748 that nobody would touch these until we release the pages. Then
749 we'd start to deal with mapping buffers to blocks. */
750 for ( i = 0; i < num_pages; i++) {
751 prepared_pages[i] = grab_cache_page(mapping, index + i); // locks the page
752 if ( !prepared_pages[i]) {
754 goto failed_page_grabbing;
756 if (!page_has_buffers(prepared_pages[i]))
757 create_empty_buffers(prepared_pages[i], inode->i_sb->s_blocksize, 0);
760 /* Let's count amount of blocks for a case where all the blocks
761 overwritten are new (we will substract already allocated blocks later)*/
763 /* These are full-overwritten pages so we count all the blocks in
764 these pages are counted as needed to be allocated */
765 blocks = (num_pages - 2) << (PAGE_CACHE_SHIFT - inode->i_blkbits);
767 /* count blocks needed for first page (possibly partially written) */
768 blocks += ((PAGE_CACHE_SIZE - from) >> inode->i_blkbits) +
769 !!(from & (inode->i_sb->s_blocksize-1)); /* roundup */
771 /* Now we account for last page. If last page == first page (we
772 overwrite only one page), we substract all the blocks past the
773 last writing position in a page out of already calculated number
775 blocks += ((num_pages > 1) << (PAGE_CACHE_SHIFT-inode->i_blkbits)) -
776 ((PAGE_CACHE_SIZE - to) >> inode->i_blkbits);
777 /* Note how we do not roundup here since partial blocks still
778 should be allocated */
780 /* Now if all the write area lies past the file end, no point in
781 maping blocks, since there is none, so we just zero out remaining
782 parts of first and last pages in write area (if needed) */
783 if ( (pos & ~(PAGE_CACHE_SIZE - 1)) > inode->i_size ) {
784 if ( from != 0 ) {/* First page needs to be partially zeroed */
785 char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0);
786 memset(kaddr, 0, from);
787 kunmap_atomic( kaddr, KM_USER0);
789 if ( to != PAGE_CACHE_SIZE ) { /* Last page needs to be partially zeroed */
790 char *kaddr = kmap_atomic(prepared_pages[num_pages-1], KM_USER0);
791 memset(kaddr+to, 0, PAGE_CACHE_SIZE - to);
792 kunmap_atomic( kaddr, KM_USER0);
795 /* Since all blocks are new - use already calculated value */
799 /* Well, since we write somewhere into the middle of a file, there is
800 possibility we are writing over some already allocated blocks, so
801 let's map these blocks and substract number of such blocks out of blocks
802 we need to allocate (calculated above) */
803 /* Mask write position to start on blocksize, we do it out of the
804 loop for performance reasons */
805 pos &= ~(inode->i_sb->s_blocksize - 1);
806 /* Set cpu key to the starting position in a file (on left block boundary)*/
807 make_cpu_key (&key, inode, 1 + ((pos) & ~(inode->i_sb->s_blocksize - 1)), TYPE_ANY, 3/*key length*/);
809 reiserfs_write_lock(inode->i_sb); // We need that for at least search_by_key()
810 for ( i = 0; i < num_pages ; i++ ) {
812 head = page_buffers(prepared_pages[i]);
813 /* For each buffer in the page */
814 for(bh = head, block_start = 0; bh != head || !block_start;
815 block_start=block_end, bh = bh->b_this_page) {
817 reiserfs_panic(inode->i_sb, "green-9002: Allocated but absent buffer for a page?");
818 /* Find where this buffer ends */
819 block_end = block_start+inode->i_sb->s_blocksize;
820 if (i == 0 && block_end <= from )
821 /* if this buffer is before requested data to map, skip it*/
824 if (i == num_pages - 1 && block_start >= to) {
825 /* If this buffer is after requested data to map, abort
826 processing of current page */
830 if ( buffer_mapped(bh) && bh->b_blocknr !=0 ) {
831 /* This is optimisation for a case where buffer is mapped
832 and have blocknumber assigned. In case significant amount
833 of such buffers are present, we may avoid some amount
834 of search_by_key calls.
835 Probably it would be possible to move parts of this code
836 out of BKL, but I afraid that would overcomplicate code
837 without any noticeable benefit.
841 set_cpu_key_k_offset( &key, cpu_key_k_offset(&key) + inode->i_sb->s_blocksize);
842 blocks--; // Decrease the amount of blocks that need to be
844 continue; // Go to the next buffer
847 if ( !itembuf || /* if first iteration */
848 item_pos >= ih_item_len(ih)/UNFM_P_SIZE)
849 { /* or if we progressed past the
850 current unformatted_item */
851 /* Try to find next item */
852 res = search_for_position_by_key(inode->i_sb, &key, &path);
853 /* Abort if no more items */
854 if ( res != POSITION_FOUND )
857 /* Update information about current indirect item */
858 itembuf = get_last_bh( &path );
859 ih = get_ih( &path );
860 item = get_item( &path );
861 item_pos = path.pos_in_item;
863 RFALSE( !is_indirect_le_ih (ih), "green-9003: indirect item expected");
866 /* See if there is some block associated with the file
867 at that position, map the buffer to this block */
868 if ( get_block_num(item,item_pos) ) {
869 map_bh(bh, inode->i_sb, get_block_num(item,item_pos));
870 blocks--; // Decrease the amount of blocks that need to be
875 set_cpu_key_k_offset( &key, cpu_key_k_offset(&key) + inode->i_sb->s_blocksize);
878 pathrelse(&path); // Free the path
879 reiserfs_write_unlock(inode->i_sb);
881 /* Now zero out unmappend buffers for the first and last pages of
882 write area or issue read requests if page is mapped. */
883 /* First page, see if it is not uptodate */
884 if ( !PageUptodate(prepared_pages[0]) ) {
885 head = page_buffers(prepared_pages[0]);
887 /* For each buffer in page */
888 for(bh = head, block_start = 0; bh != head || !block_start;
889 block_start=block_end, bh = bh->b_this_page) {
892 reiserfs_panic(inode->i_sb, "green-9002: Allocated but absent buffer for a page?");
893 /* Find where this buffer ends */
894 block_end = block_start+inode->i_sb->s_blocksize;
895 if ( block_end <= from )
896 /* if this buffer is before requested data to map, skip it*/
898 if ( block_start < from ) { /* Aha, our partial buffer */
899 if ( buffer_mapped(bh) ) { /* If it is mapped, we need to
900 issue READ request for it to
902 ll_rw_block(READ, 1, &bh);
904 } else { /* Not mapped, zero it */
905 char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0);
906 memset(kaddr+block_start, 0, from-block_start);
907 kunmap_atomic( kaddr, KM_USER0);
908 set_buffer_uptodate(bh);
914 /* Last page, see if it is not uptodate, or if the last page is past the end of the file. */
915 if ( !PageUptodate(prepared_pages[num_pages-1]) ||
916 ((pos+write_bytes)>>PAGE_CACHE_SHIFT) > (inode->i_size>>PAGE_CACHE_SHIFT) ) {
917 head = page_buffers(prepared_pages[num_pages-1]);
919 /* for each buffer in page */
920 for(bh = head, block_start = 0; bh != head || !block_start;
921 block_start=block_end, bh = bh->b_this_page) {
924 reiserfs_panic(inode->i_sb, "green-9002: Allocated but absent buffer for a page?");
925 /* Find where this buffer ends */
926 block_end = block_start+inode->i_sb->s_blocksize;
927 if ( block_start >= to )
928 /* if this buffer is after requested data to map, skip it*/
930 if ( block_end > to ) { /* Aha, our partial buffer */
931 if ( buffer_mapped(bh) ) { /* If it is mapped, we need to
932 issue READ request for it to
934 ll_rw_block(READ, 1, &bh);
936 } else { /* Not mapped, zero it */
937 char *kaddr = kmap_atomic(prepared_pages[num_pages-1], KM_USER0);
938 memset(kaddr+to, 0, block_end-to);
939 kunmap_atomic( kaddr, KM_USER0);
940 set_buffer_uptodate(bh);
946 /* Wait for read requests we made to happen, if necessary */
947 while(wait_bh > wait) {
948 wait_on_buffer(*--wait_bh);
949 if (!buffer_uptodate(*wait_bh)) {
956 failed_page_grabbing:
959 reiserfs_unprepare_pages(prepared_pages, num_pages);
963 /* Write @count bytes at position @ppos in a file indicated by @file
964 from the buffer @buf.
966 generic_file_write() is only appropriate for filesystems that are not seeking to optimize performance and want
967 something simple that works. It is not for serious use by general purpose filesystems, excepting the one that it was
968 written for (ext2/3). This is for several reasons:
970 * It has no understanding of any filesystem specific optimizations.
972 * It enters the filesystem repeatedly for each page that is written.
974 * It depends on reiserfs_get_block() function which if implemented by reiserfs performs costly search_by_key
975 * operation for each page it is supplied with. By contrast reiserfs_file_write() feeds as much as possible at a time
976 * to reiserfs which allows for fewer tree traversals.
978 * Each indirect pointer insertion takes a lot of cpu, because it involves memory moves inside of blocks.
980 * Asking the block allocation code for blocks one at a time is slightly less efficient.
982 All of these reasons for not using only generic file write were understood back when reiserfs was first miscoded to
983 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
984 things right finally.
986 Future Features: providing search_by_key with hints.
989 ssize_t reiserfs_file_write( struct file *file, /* the file we are going to write into */
990 const char *buf, /* pointer to user supplied data
992 size_t count, /* amount of bytes to write */
993 loff_t *ppos /* pointer to position in file that we start writing at. Should be updated to
994 * new current position before returning. */ )
996 size_t already_written = 0; // Number of bytes already written to the file.
997 loff_t pos; // Current position in the file.
998 size_t res; // return value of various functions that we call.
999 struct inode *inode = file->f_dentry->d_inode; // Inode of the file that we are writing to.
1000 struct page * prepared_pages[REISERFS_WRITE_PAGES_AT_A_TIME];
1001 /* To simplify coding at this time, we store
1002 locked pages in array for now */
1003 if ( count <= PAGE_CACHE_SIZE || file->f_flags & O_DIRECT)
1004 return generic_file_write(file, buf, count, ppos);
1006 if ( unlikely((ssize_t) count < 0 ))
1009 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
1012 down(&inode->i_sem); // locks the entire file for just us
1016 /* Check if we can write to specified region of file, file
1017 is not overly big and this kind of stuff. Adjust pos and
1019 res = generic_write_checks(inode, file, &pos, &count, 0);
1026 remove_suid(file->f_dentry);
1027 inode_update_time(inode, 1); /* Both mtime and ctime */
1029 // Ok, we are done with all the checks.
1031 // Now we should start real work
1033 /* If we are going to write past the file's packed tail or if we are going
1034 to overwrite part of the tail, we need that tail to be converted into
1036 res = reiserfs_check_for_tail_and_convert( inode, pos, count);
1040 while ( count > 0) {
1041 /* This is the main loop in which we running until some error occures
1042 or until we write all of the data. */
1043 int num_pages;/* amount of pages we are going to write this iteration */
1044 int write_bytes; /* amount of bytes to write during this iteration */
1045 int blocks_to_allocate; /* how much blocks we need to allocate for
1048 /* (pos & (PAGE_CACHE_SIZE-1)) is an idiom for offset into a page of pos*/
1049 num_pages = !!((pos+count) & (PAGE_CACHE_SIZE - 1)) + /* round up partial
1051 ((count + (pos & (PAGE_CACHE_SIZE-1))) >> PAGE_CACHE_SHIFT);
1052 /* convert size to amount of
1054 reiserfs_write_lock(inode->i_sb);
1055 if ( num_pages > REISERFS_WRITE_PAGES_AT_A_TIME
1056 || num_pages > reiserfs_can_fit_pages(inode->i_sb) ) {
1057 /* If we were asked to write more data than we want to or if there
1058 is not that much space, then we shorten amount of data to write
1059 for this iteration. */
1060 num_pages = min_t(int, REISERFS_WRITE_PAGES_AT_A_TIME, reiserfs_can_fit_pages(inode->i_sb));
1061 /* Also we should not forget to set size in bytes accordingly */
1062 write_bytes = (num_pages << PAGE_CACHE_SHIFT) -
1063 (pos & (PAGE_CACHE_SIZE-1));
1064 /* If position is not on the
1065 start of the page, we need
1066 to substract the offset
1069 write_bytes = count;
1071 /* reserve the blocks to be allocated later, so that later on
1072 we still have the space to write the blocks to */
1073 reiserfs_claim_blocks_to_be_allocated(inode->i_sb, num_pages << (PAGE_CACHE_SHIFT - inode->i_blkbits));
1074 reiserfs_write_unlock(inode->i_sb);
1076 if ( !num_pages ) { /* If we do not have enough space even for */
1077 res = -ENOSPC; /* single page, return -ENOSPC */
1078 if ( pos > (inode->i_size & (inode->i_sb->s_blocksize-1)))
1079 break; // In case we are writing past the file end, break.
1080 // Otherwise we are possibly overwriting the file, so
1081 // let's set write size to be equal or less than blocksize.
1082 // This way we get it correctly for file holes.
1083 // But overwriting files on absolutelly full volumes would not
1084 // be very efficient. Well, people are not supposed to fill
1085 // 100% of disk space anyway.
1086 write_bytes = min_t(int, count, inode->i_sb->s_blocksize - (pos & (inode->i_sb->s_blocksize - 1)));
1088 // No blocks were claimed before, so do it now.
1089 reiserfs_claim_blocks_to_be_allocated(inode->i_sb, 1 << (PAGE_CACHE_SHIFT - inode->i_blkbits));
1092 /* Prepare for writing into the region, read in all the
1093 partially overwritten pages, if needed. And lock the pages,
1094 so that nobody else can access these until we are done.
1095 We get number of actual blocks needed as a result.*/
1096 blocks_to_allocate = reiserfs_prepare_file_region_for_write(inode, pos, num_pages, write_bytes, prepared_pages);
1097 if ( blocks_to_allocate < 0 ) {
1098 res = blocks_to_allocate;
1099 reiserfs_release_claimed_blocks(inode->i_sb, num_pages << (PAGE_CACHE_SHIFT - inode->i_blkbits));
1103 /* First we correct our estimate of how many blocks we need */
1104 reiserfs_release_claimed_blocks(inode->i_sb, (num_pages << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits)) - blocks_to_allocate );
1106 if ( blocks_to_allocate > 0) {/*We only allocate blocks if we need to*/
1107 /* Fill in all the possible holes and append the file if needed */
1108 res = reiserfs_allocate_blocks_for_region(inode, pos, num_pages, write_bytes, prepared_pages, blocks_to_allocate);
1109 } else if ( pos + write_bytes > inode->i_size ) {
1110 /* File might have grown even though no new blocks were added */
1111 inode->i_size = pos + write_bytes;
1112 inode->i_sb->s_op->dirty_inode(inode);
1115 /* well, we have allocated the blocks, so it is time to free
1116 the reservation we made earlier. */
1117 reiserfs_release_claimed_blocks(inode->i_sb, blocks_to_allocate);
1119 reiserfs_unprepare_pages(prepared_pages, num_pages);
1123 /* NOTE that allocating blocks and filling blocks can be done in reverse order
1124 and probably we would do that just to get rid of garbage in files after a
1127 /* Copy data from user-supplied buffer to file's pages */
1128 res = reiserfs_copy_from_user_to_file_region(pos, num_pages, write_bytes, prepared_pages, buf);
1130 reiserfs_unprepare_pages(prepared_pages, num_pages);
1134 /* Send the pages to disk and unlock them. */
1135 res = reiserfs_submit_file_region_for_write(pos, num_pages, write_bytes, prepared_pages);
1139 already_written += write_bytes;
1141 *ppos = pos += write_bytes;
1142 count -= write_bytes;
1145 if ((file->f_flags & O_SYNC) || IS_SYNC(inode))
1146 res = generic_osync_inode(inode, OSYNC_METADATA|OSYNC_DATA);
1149 return (already_written != 0)?already_written:res;
1152 up(&inode->i_sem); // unlock the file on exit.
1156 struct file_operations reiserfs_file_operations = {
1157 .read = generic_file_read,
1158 .write = reiserfs_file_write,
1159 .ioctl = reiserfs_ioctl,
1160 .mmap = generic_file_mmap,
1161 .release = reiserfs_file_release,
1162 .fsync = reiserfs_sync_file,
1163 .sendfile = generic_file_sendfile,
1167 struct inode_operations reiserfs_file_inode_operations = {
1168 .truncate = reiserfs_vfs_truncate_file,
1169 .setattr = reiserfs_setattr,