xref: /openbmc/linux/fs/reiserfs/file.c (revision 4b6f5d20b04dcbc3d888555522b90ba6d36c4106)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/time.h>
61da177e4SLinus Torvalds #include <linux/reiserfs_fs.h>
71da177e4SLinus Torvalds #include <linux/reiserfs_acl.h>
81da177e4SLinus Torvalds #include <linux/reiserfs_xattr.h>
91da177e4SLinus Torvalds #include <linux/smp_lock.h>
101da177e4SLinus Torvalds #include <asm/uaccess.h>
111da177e4SLinus Torvalds #include <linux/pagemap.h>
121da177e4SLinus Torvalds #include <linux/swap.h>
131da177e4SLinus Torvalds #include <linux/writeback.h>
141da177e4SLinus Torvalds #include <linux/blkdev.h>
151da177e4SLinus Torvalds #include <linux/buffer_head.h>
161da177e4SLinus Torvalds #include <linux/quotaops.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds ** We pack the tails of files on file close, not at the time they are written.
201da177e4SLinus Torvalds ** This implies an unnecessary copy of the tail and an unnecessary indirect item
211da177e4SLinus Torvalds ** insertion/balancing, for files that are written in one write.
221da177e4SLinus Torvalds ** It avoids unnecessary tail packings (balances) for files that are written in
231da177e4SLinus Torvalds ** multiple writes and are small enough to have tails.
241da177e4SLinus Torvalds **
251da177e4SLinus Torvalds ** file_release is called by the VFS layer when the file is closed.  If
261da177e4SLinus Torvalds ** this is the last open file descriptor, and the file
271da177e4SLinus Torvalds ** small enough to have a tail, and the tail is currently in an
281da177e4SLinus Torvalds ** unformatted node, the tail is converted back into a direct item.
291da177e4SLinus Torvalds **
301da177e4SLinus Torvalds ** We use reiserfs_truncate_file to pack the tail, since it already has
311da177e4SLinus Torvalds ** all the conditions coded.
321da177e4SLinus Torvalds */
331da177e4SLinus Torvalds static int reiserfs_file_release(struct inode *inode, struct file *filp)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	struct reiserfs_transaction_handle th;
371da177e4SLinus Torvalds 	int err;
381da177e4SLinus Torvalds 	int jbegin_failure = 0;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
411da177e4SLinus Torvalds 		BUG();
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	/* fast out for when nothing needs to be done */
441da177e4SLinus Torvalds 	if ((atomic_read(&inode->i_count) > 1 ||
451da177e4SLinus Torvalds 	     !(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
461da177e4SLinus Torvalds 	     !tail_has_to_be_packed(inode)) &&
471da177e4SLinus Torvalds 	    REISERFS_I(inode)->i_prealloc_count <= 0) {
481da177e4SLinus Torvalds 		return 0;
491da177e4SLinus Torvalds 	}
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	reiserfs_write_lock(inode->i_sb);
521b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
531da177e4SLinus Torvalds 	/* freeing preallocation only involves relogging blocks that
541da177e4SLinus Torvalds 	 * are already in the current transaction.  preallocation gets
551da177e4SLinus Torvalds 	 * freed at the end of each transaction, so it is impossible for
561da177e4SLinus Torvalds 	 * us to log any additional blocks (including quota blocks)
571da177e4SLinus Torvalds 	 */
581da177e4SLinus Torvalds 	err = journal_begin(&th, inode->i_sb, 1);
591da177e4SLinus Torvalds 	if (err) {
601da177e4SLinus Torvalds 		/* uh oh, we can't allow the inode to go away while there
611da177e4SLinus Torvalds 		 * is still preallocation blocks pending.  Try to join the
621da177e4SLinus Torvalds 		 * aborted transaction
631da177e4SLinus Torvalds 		 */
641da177e4SLinus Torvalds 		jbegin_failure = err;
651da177e4SLinus Torvalds 		err = journal_join_abort(&th, inode->i_sb, 1);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 		if (err) {
681da177e4SLinus Torvalds 			/* hmpf, our choices here aren't good.  We can pin the inode
691da177e4SLinus Torvalds 			 * which will disallow unmount from every happening, we can
701da177e4SLinus Torvalds 			 * do nothing, which will corrupt random memory on unmount,
711da177e4SLinus Torvalds 			 * or we can forcibly remove the file from the preallocation
721da177e4SLinus Torvalds 			 * list, which will leak blocks on disk.  Lets pin the inode
731da177e4SLinus Torvalds 			 * and let the admin know what is going on.
741da177e4SLinus Torvalds 			 */
751da177e4SLinus Torvalds 			igrab(inode);
76bd4c625cSLinus Torvalds 			reiserfs_warning(inode->i_sb,
77bd4c625cSLinus Torvalds 					 "pinning inode %lu because the "
781da177e4SLinus Torvalds 					 "preallocation can't be freed");
791da177e4SLinus Torvalds 			goto out;
801da177e4SLinus Torvalds 		}
811da177e4SLinus Torvalds 	}
821da177e4SLinus Torvalds 	reiserfs_update_inode_transaction(inode);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #ifdef REISERFS_PREALLOCATE
851da177e4SLinus Torvalds 	reiserfs_discard_prealloc(&th, inode);
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds 	err = journal_end(&th, inode->i_sb, 1);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/* copy back the error code from journal_begin */
901da177e4SLinus Torvalds 	if (!err)
911da177e4SLinus Torvalds 		err = jbegin_failure;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	if (!err && atomic_read(&inode->i_count) <= 1 &&
941da177e4SLinus Torvalds 	    (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
951da177e4SLinus Torvalds 	    tail_has_to_be_packed(inode)) {
961da177e4SLinus Torvalds 		/* if regular file is released by last holder and it has been
971da177e4SLinus Torvalds 		   appended (we append by unformatted node only) or its direct
981da177e4SLinus Torvalds 		   item(s) had to be converted, then it may have to be
991da177e4SLinus Torvalds 		   indirect2direct converted */
1001da177e4SLinus Torvalds 		err = reiserfs_truncate_file(inode, 0);
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds       out:
1031b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
1041da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
1051da177e4SLinus Torvalds 	return err;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108bd4c625cSLinus Torvalds static void reiserfs_vfs_truncate_file(struct inode *inode)
109bd4c625cSLinus Torvalds {
1101da177e4SLinus Torvalds 	reiserfs_truncate_file(inode, 1);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* Sync a reiserfs file. */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  * FIXME: sync_mapping_buffers() never has anything to sync.  Can
1171da177e4SLinus Torvalds  * be removed...
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds 
120bd4c625cSLinus Torvalds static int reiserfs_sync_file(struct file *p_s_filp,
121bd4c625cSLinus Torvalds 			      struct dentry *p_s_dentry, int datasync)
122bd4c625cSLinus Torvalds {
1231da177e4SLinus Torvalds 	struct inode *p_s_inode = p_s_dentry->d_inode;
1241da177e4SLinus Torvalds 	int n_err;
1251da177e4SLinus Torvalds 	int barrier_done;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	if (!S_ISREG(p_s_inode->i_mode))
1281da177e4SLinus Torvalds 		BUG();
1291da177e4SLinus Torvalds 	n_err = sync_mapping_buffers(p_s_inode->i_mapping);
1301da177e4SLinus Torvalds 	reiserfs_write_lock(p_s_inode->i_sb);
1311da177e4SLinus Torvalds 	barrier_done = reiserfs_commit_for_inode(p_s_inode);
1321da177e4SLinus Torvalds 	reiserfs_write_unlock(p_s_inode->i_sb);
1331da177e4SLinus Torvalds 	if (barrier_done != 1)
1341da177e4SLinus Torvalds 		blkdev_issue_flush(p_s_inode->i_sb->s_bdev, NULL);
1351da177e4SLinus Torvalds 	if (barrier_done < 0)
1361da177e4SLinus Torvalds 		return barrier_done;
1371da177e4SLinus Torvalds 	return (n_err < 0) ? -EIO : 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /* I really do not want to play with memory shortage right now, so
1411da177e4SLinus Torvalds    to simplify the code, we are not going to write more than this much pages at
1421da177e4SLinus Torvalds    a time. This still should considerably improve performance compared to 4k
1431da177e4SLinus Torvalds    at a time case. This is 32 pages of 4k size. */
1441da177e4SLinus Torvalds #define REISERFS_WRITE_PAGES_AT_A_TIME (128 * 1024) / PAGE_CACHE_SIZE
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /* Allocates blocks for a file to fulfil write request.
1471da177e4SLinus Torvalds    Maps all unmapped but prepared pages from the list.
1481da177e4SLinus Torvalds    Updates metadata with newly allocated blocknumbers as needed */
149bd4c625cSLinus Torvalds static int reiserfs_allocate_blocks_for_region(struct reiserfs_transaction_handle *th, struct inode *inode,	/* Inode we work with */
1501da177e4SLinus Torvalds 					       loff_t pos,	/* Writing position */
1511da177e4SLinus Torvalds 					       int num_pages,	/* number of pages write going
1521da177e4SLinus Torvalds 								   to touch */
1531da177e4SLinus Torvalds 					       int write_bytes,	/* amount of bytes to write */
1541da177e4SLinus Torvalds 					       struct page **prepared_pages,	/* array of
1551da177e4SLinus Torvalds 										   prepared pages
1561da177e4SLinus Torvalds 										 */
1571da177e4SLinus Torvalds 					       int blocks_to_allocate	/* Amount of blocks we
1581da177e4SLinus Torvalds 									   need to allocate to
1591da177e4SLinus Torvalds 									   fit the data into file
1601da177e4SLinus Torvalds 									 */
1611da177e4SLinus Torvalds     )
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	struct cpu_key key;	// cpu key of item that we are going to deal with
1641da177e4SLinus Torvalds 	struct item_head *ih;	// pointer to item head that we are going to deal with
1651da177e4SLinus Torvalds 	struct buffer_head *bh;	// Buffer head that contains items that we are going to deal with
1663e8962beSAl Viro 	__le32 *item;		// pointer to item we are going to deal with
1671da177e4SLinus Torvalds 	INITIALIZE_PATH(path);	// path to item, that we are going to deal with.
1681da177e4SLinus Torvalds 	b_blocknr_t *allocated_blocks;	// Pointer to a place where allocated blocknumbers would be stored.
1691da177e4SLinus Torvalds 	reiserfs_blocknr_hint_t hint;	// hint structure for block allocator.
1701da177e4SLinus Torvalds 	size_t res;		// return value of various functions that we call.
1711da177e4SLinus Torvalds 	int curr_block;		// current block used to keep track of unmapped blocks.
1721da177e4SLinus Torvalds 	int i;			// loop counter
1731da177e4SLinus Torvalds 	int itempos;		// position in item
1741da177e4SLinus Torvalds 	unsigned int from = (pos & (PAGE_CACHE_SIZE - 1));	// writing position in
1751da177e4SLinus Torvalds 	// first page
1761da177e4SLinus Torvalds 	unsigned int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1;	/* last modified byte offset in last page */
1771da177e4SLinus Torvalds 	__u64 hole_size;	// amount of blocks for a file hole, if it needed to be created.
1781da177e4SLinus Torvalds 	int modifying_this_item = 0;	// Flag for items traversal code to keep track
1791da177e4SLinus Torvalds 	// of the fact that we already prepared
1801da177e4SLinus Torvalds 	// current block for journal
1811da177e4SLinus Torvalds 	int will_prealloc = 0;
182bd4c625cSLinus Torvalds 	RFALSE(!blocks_to_allocate,
183bd4c625cSLinus Torvalds 	       "green-9004: tried to allocate zero blocks?");
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	/* only preallocate if this is a small write */
1861da177e4SLinus Torvalds 	if (REISERFS_I(inode)->i_prealloc_count ||
1871da177e4SLinus Torvalds 	    (!(write_bytes & (inode->i_sb->s_blocksize - 1)) &&
1881da177e4SLinus Torvalds 	     blocks_to_allocate <
1891da177e4SLinus Torvalds 	     REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize))
190bd4c625cSLinus Torvalds 		will_prealloc =
191bd4c625cSLinus Torvalds 		    REISERFS_SB(inode->i_sb)->s_alloc_options.preallocsize;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	allocated_blocks = kmalloc((blocks_to_allocate + will_prealloc) *
1941da177e4SLinus Torvalds 				   sizeof(b_blocknr_t), GFP_NOFS);
195e5dd259fSDiego Calleja 	if (!allocated_blocks)
196e5dd259fSDiego Calleja 		return -ENOMEM;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/* First we compose a key to point at the writing position, we want to do
1991da177e4SLinus Torvalds 	   that outside of any locking region. */
2001da177e4SLinus Torvalds 	make_cpu_key(&key, inode, pos + 1, TYPE_ANY, 3 /*key length */ );
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/* If we came here, it means we absolutely need to open a transaction,
2031da177e4SLinus Torvalds 	   since we need to allocate some blocks */
2041da177e4SLinus Torvalds 	reiserfs_write_lock(inode->i_sb);	// Journaling stuff and we need that.
205556a2a45SJan Kara 	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
2061da177e4SLinus Torvalds 	if (res)
2071da177e4SLinus Torvalds 		goto error_exit;
2081da177e4SLinus Torvalds 	reiserfs_update_inode_transaction(inode);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	/* Look for the in-tree position of our write, need path for block allocator */
2111da177e4SLinus Torvalds 	res = search_for_position_by_key(inode->i_sb, &key, &path);
2121da177e4SLinus Torvalds 	if (res == IO_ERROR) {
2131da177e4SLinus Torvalds 		res = -EIO;
2141da177e4SLinus Torvalds 		goto error_exit;
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Allocate blocks */
2181da177e4SLinus Torvalds 	/* First fill in "hint" structure for block allocator */
2191da177e4SLinus Torvalds 	hint.th = th;		// transaction handle.
2201da177e4SLinus Torvalds 	hint.path = &path;	// Path, so that block allocator can determine packing locality or whatever it needs to determine.
2211da177e4SLinus Torvalds 	hint.inode = inode;	// Inode is needed by block allocator too.
2221da177e4SLinus Torvalds 	hint.search_start = 0;	// We have no hint on where to search free blocks for block allocator.
2231da177e4SLinus Torvalds 	hint.key = key.on_disk_key;	// on disk key of file.
2241da177e4SLinus Torvalds 	hint.block = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);	// Number of disk blocks this file occupies already.
2251da177e4SLinus Torvalds 	hint.formatted_node = 0;	// We are allocating blocks for unformatted node.
2261da177e4SLinus Torvalds 	hint.preallocate = will_prealloc;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* Call block allocator to allocate blocks */
229bd4c625cSLinus Torvalds 	res =
230bd4c625cSLinus Torvalds 	    reiserfs_allocate_blocknrs(&hint, allocated_blocks,
231bd4c625cSLinus Torvalds 				       blocks_to_allocate, blocks_to_allocate);
2321da177e4SLinus Torvalds 	if (res != CARRY_ON) {
2331da177e4SLinus Torvalds 		if (res == NO_DISK_SPACE) {
2341da177e4SLinus Torvalds 			/* We flush the transaction in case of no space. This way some
2351da177e4SLinus Torvalds 			   blocks might become free */
2361da177e4SLinus Torvalds 			SB_JOURNAL(inode->i_sb)->j_must_wait = 1;
2371da177e4SLinus Torvalds 			res = restart_transaction(th, inode, &path);
2381da177e4SLinus Torvalds 			if (res)
2391da177e4SLinus Torvalds 				goto error_exit;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 			/* We might have scheduled, so search again */
242bd4c625cSLinus Torvalds 			res =
243bd4c625cSLinus Torvalds 			    search_for_position_by_key(inode->i_sb, &key,
244bd4c625cSLinus Torvalds 						       &path);
2451da177e4SLinus Torvalds 			if (res == IO_ERROR) {
2461da177e4SLinus Torvalds 				res = -EIO;
2471da177e4SLinus Torvalds 				goto error_exit;
2481da177e4SLinus Torvalds 			}
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 			/* update changed info for hint structure. */
251bd4c625cSLinus Torvalds 			res =
252bd4c625cSLinus Torvalds 			    reiserfs_allocate_blocknrs(&hint, allocated_blocks,
253bd4c625cSLinus Torvalds 						       blocks_to_allocate,
254bd4c625cSLinus Torvalds 						       blocks_to_allocate);
2551da177e4SLinus Torvalds 			if (res != CARRY_ON) {
2560ad74ffaSJan Kara 				res = res == QUOTA_EXCEEDED ? -EDQUOT : -ENOSPC;
2571da177e4SLinus Torvalds 				pathrelse(&path);
2581da177e4SLinus Torvalds 				goto error_exit;
2591da177e4SLinus Torvalds 			}
2601da177e4SLinus Torvalds 		} else {
2610ad74ffaSJan Kara 			res = res == QUOTA_EXCEEDED ? -EDQUOT : -ENOSPC;
2621da177e4SLinus Torvalds 			pathrelse(&path);
2631da177e4SLinus Torvalds 			goto error_exit;
2641da177e4SLinus Torvalds 		}
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds #ifdef __BIG_ENDIAN
2671da177e4SLinus Torvalds 	// Too bad, I have not found any way to convert a given region from
2681da177e4SLinus Torvalds 	// cpu format to little endian format
2691da177e4SLinus Torvalds 	{
2701da177e4SLinus Torvalds 		int i;
2711da177e4SLinus Torvalds 		for (i = 0; i < blocks_to_allocate; i++)
2721da177e4SLinus Torvalds 			allocated_blocks[i] = cpu_to_le32(allocated_blocks[i]);
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds #endif
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* Blocks allocating well might have scheduled and tree might have changed,
2771da177e4SLinus Torvalds 	   let's search the tree again */
2781da177e4SLinus Torvalds 	/* find where in the tree our write should go */
2791da177e4SLinus Torvalds 	res = search_for_position_by_key(inode->i_sb, &key, &path);
2801da177e4SLinus Torvalds 	if (res == IO_ERROR) {
2811da177e4SLinus Torvalds 		res = -EIO;
2821da177e4SLinus Torvalds 		goto error_exit_free_blocks;
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	bh = get_last_bh(&path);	// Get a bufferhead for last element in path.
2861da177e4SLinus Torvalds 	ih = get_ih(&path);	// Get a pointer to last item head in path.
2871da177e4SLinus Torvalds 	item = get_item(&path);	// Get a pointer to last item in path
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	/* Let's see what we have found */
2901da177e4SLinus Torvalds 	if (res != POSITION_FOUND) {	/* position not found, this means that we
2911da177e4SLinus Torvalds 					   might need to append file with holes
2921da177e4SLinus Torvalds 					   first */
2931da177e4SLinus Torvalds 		// Since we are writing past the file's end, we need to find out if
2941da177e4SLinus Torvalds 		// there is a hole that needs to be inserted before our writing
2951da177e4SLinus Torvalds 		// position, and how many blocks it is going to cover (we need to
2961da177e4SLinus Torvalds 		//  populate pointers to file blocks representing the hole with zeros)
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 		{
2991da177e4SLinus Torvalds 			int item_offset = 1;
3001da177e4SLinus Torvalds 			/*
3011da177e4SLinus Torvalds 			 * if ih is stat data, its offset is 0 and we don't want to
3021da177e4SLinus Torvalds 			 * add 1 to pos in the hole_size calculation
3031da177e4SLinus Torvalds 			 */
3041da177e4SLinus Torvalds 			if (is_statdata_le_ih(ih))
3051da177e4SLinus Torvalds 				item_offset = 0;
3061da177e4SLinus Torvalds 			hole_size = (pos + item_offset -
307bd4c625cSLinus Torvalds 				     (le_key_k_offset
308bd4c625cSLinus Torvalds 				      (get_inode_item_key_version(inode),
309bd4c625cSLinus Torvalds 				       &(ih->ih_key)) + op_bytes_number(ih,
310bd4c625cSLinus Torvalds 									inode->
311bd4c625cSLinus Torvalds 									i_sb->
312bd4c625cSLinus Torvalds 									s_blocksize)))
313bd4c625cSLinus Torvalds 			    >> inode->i_sb->s_blocksize_bits;
3141da177e4SLinus Torvalds 		}
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		if (hole_size > 0) {
3171da177e4SLinus Torvalds 			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.
3181da177e4SLinus Torvalds 			/* area filled with zeroes, to supply as list of zero blocknumbers
3191da177e4SLinus Torvalds 			   We allocate it outside of loop just in case loop would spin for
3201da177e4SLinus Torvalds 			   several iterations. */
3211da177e4SLinus Torvalds 			char *zeros = kmalloc(to_paste * UNFM_P_SIZE, GFP_ATOMIC);	// We cannot insert more than MAX_ITEM_LEN bytes anyway.
3221da177e4SLinus Torvalds 			if (!zeros) {
3231da177e4SLinus Torvalds 				res = -ENOMEM;
3241da177e4SLinus Torvalds 				goto error_exit_free_blocks;
3251da177e4SLinus Torvalds 			}
3261da177e4SLinus Torvalds 			memset(zeros, 0, to_paste * UNFM_P_SIZE);
3271da177e4SLinus Torvalds 			do {
328bd4c625cSLinus Torvalds 				to_paste =
329bd4c625cSLinus Torvalds 				    min_t(__u64, hole_size,
330bd4c625cSLinus Torvalds 					  MAX_ITEM_LEN(inode->i_sb->
331bd4c625cSLinus Torvalds 						       s_blocksize) /
332bd4c625cSLinus Torvalds 					  UNFM_P_SIZE);
3331da177e4SLinus Torvalds 				if (is_indirect_le_ih(ih)) {
3341da177e4SLinus Torvalds 					/* Ok, there is existing indirect item already. Need to append it */
3351da177e4SLinus Torvalds 					/* Calculate position past inserted item */
336bd4c625cSLinus Torvalds 					make_cpu_key(&key, inode,
337bd4c625cSLinus Torvalds 						     le_key_k_offset
338bd4c625cSLinus Torvalds 						     (get_inode_item_key_version
339bd4c625cSLinus Torvalds 						      (inode),
340bd4c625cSLinus Torvalds 						      &(ih->ih_key)) +
341bd4c625cSLinus Torvalds 						     op_bytes_number(ih,
342bd4c625cSLinus Torvalds 								     inode->
343bd4c625cSLinus Torvalds 								     i_sb->
344bd4c625cSLinus Torvalds 								     s_blocksize),
345bd4c625cSLinus Torvalds 						     TYPE_INDIRECT, 3);
346bd4c625cSLinus Torvalds 					res =
347bd4c625cSLinus Torvalds 					    reiserfs_paste_into_item(th, &path,
348bd4c625cSLinus Torvalds 								     &key,
349bd4c625cSLinus Torvalds 								     inode,
350bd4c625cSLinus Torvalds 								     (char *)
351bd4c625cSLinus Torvalds 								     zeros,
352bd4c625cSLinus Torvalds 								     UNFM_P_SIZE
353bd4c625cSLinus Torvalds 								     *
354bd4c625cSLinus Torvalds 								     to_paste);
3551da177e4SLinus Torvalds 					if (res) {
3561da177e4SLinus Torvalds 						kfree(zeros);
3571da177e4SLinus Torvalds 						goto error_exit_free_blocks;
3581da177e4SLinus Torvalds 					}
3591da177e4SLinus Torvalds 				} else if (is_statdata_le_ih(ih)) {
3601da177e4SLinus Torvalds 					/* No existing item, create it */
3611da177e4SLinus Torvalds 					/* item head for new item */
3621da177e4SLinus Torvalds 					struct item_head ins_ih;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 					/* create a key for our new item */
365bd4c625cSLinus Torvalds 					make_cpu_key(&key, inode, 1,
366bd4c625cSLinus Torvalds 						     TYPE_INDIRECT, 3);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 					/* Create new item head for our new item */
369bd4c625cSLinus Torvalds 					make_le_item_head(&ins_ih, &key,
370bd4c625cSLinus Torvalds 							  key.version, 1,
371bd4c625cSLinus Torvalds 							  TYPE_INDIRECT,
372bd4c625cSLinus Torvalds 							  to_paste *
373bd4c625cSLinus Torvalds 							  UNFM_P_SIZE,
3741da177e4SLinus Torvalds 							  0 /* free space */ );
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 					/* Find where such item should live in the tree */
377bd4c625cSLinus Torvalds 					res =
378bd4c625cSLinus Torvalds 					    search_item(inode->i_sb, &key,
379bd4c625cSLinus Torvalds 							&path);
3801da177e4SLinus Torvalds 					if (res != ITEM_NOT_FOUND) {
3811da177e4SLinus Torvalds 						/* item should not exist, otherwise we have error */
3821da177e4SLinus Torvalds 						if (res != -ENOSPC) {
383bd4c625cSLinus Torvalds 							reiserfs_warning(inode->
384bd4c625cSLinus Torvalds 									 i_sb,
3851da177e4SLinus Torvalds 									 "green-9008: search_by_key (%K) returned %d",
386bd4c625cSLinus Torvalds 									 &key,
387bd4c625cSLinus Torvalds 									 res);
3881da177e4SLinus Torvalds 						}
3891da177e4SLinus Torvalds 						res = -EIO;
3901da177e4SLinus Torvalds 						kfree(zeros);
3911da177e4SLinus Torvalds 						goto error_exit_free_blocks;
3921da177e4SLinus Torvalds 					}
393bd4c625cSLinus Torvalds 					res =
394bd4c625cSLinus Torvalds 					    reiserfs_insert_item(th, &path,
395bd4c625cSLinus Torvalds 								 &key, &ins_ih,
396bd4c625cSLinus Torvalds 								 inode,
397bd4c625cSLinus Torvalds 								 (char *)zeros);
3981da177e4SLinus Torvalds 				} else {
399bd4c625cSLinus Torvalds 					reiserfs_panic(inode->i_sb,
400bd4c625cSLinus Torvalds 						       "green-9011: Unexpected key type %K\n",
401bd4c625cSLinus Torvalds 						       &key);
4021da177e4SLinus Torvalds 				}
4031da177e4SLinus Torvalds 				if (res) {
4041da177e4SLinus Torvalds 					kfree(zeros);
4051da177e4SLinus Torvalds 					goto error_exit_free_blocks;
4061da177e4SLinus Torvalds 				}
4071da177e4SLinus Torvalds 				/* Now we want to check if transaction is too full, and if it is
4081da177e4SLinus Torvalds 				   we restart it. This will also free the path. */
409bd4c625cSLinus Torvalds 				if (journal_transaction_should_end
410bd4c625cSLinus Torvalds 				    (th, th->t_blocks_allocated)) {
411bd4c625cSLinus Torvalds 					res =
412bd4c625cSLinus Torvalds 					    restart_transaction(th, inode,
413bd4c625cSLinus Torvalds 								&path);
4141da177e4SLinus Torvalds 					if (res) {
4151da177e4SLinus Torvalds 						pathrelse(&path);
4161da177e4SLinus Torvalds 						kfree(zeros);
4171da177e4SLinus Torvalds 						goto error_exit;
4181da177e4SLinus Torvalds 					}
4191da177e4SLinus Torvalds 				}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 				/* Well, need to recalculate path and stuff */
422bd4c625cSLinus Torvalds 				set_cpu_key_k_offset(&key,
423bd4c625cSLinus Torvalds 						     cpu_key_k_offset(&key) +
424bd4c625cSLinus Torvalds 						     (to_paste << inode->
425bd4c625cSLinus Torvalds 						      i_blkbits));
426bd4c625cSLinus Torvalds 				res =
427bd4c625cSLinus Torvalds 				    search_for_position_by_key(inode->i_sb,
428bd4c625cSLinus Torvalds 							       &key, &path);
4291da177e4SLinus Torvalds 				if (res == IO_ERROR) {
4301da177e4SLinus Torvalds 					res = -EIO;
4311da177e4SLinus Torvalds 					kfree(zeros);
4321da177e4SLinus Torvalds 					goto error_exit_free_blocks;
4331da177e4SLinus Torvalds 				}
4341da177e4SLinus Torvalds 				bh = get_last_bh(&path);
4351da177e4SLinus Torvalds 				ih = get_ih(&path);
4361da177e4SLinus Torvalds 				item = get_item(&path);
4371da177e4SLinus Torvalds 				hole_size -= to_paste;
4381da177e4SLinus Torvalds 			} while (hole_size);
4391da177e4SLinus Torvalds 			kfree(zeros);
4401da177e4SLinus Torvalds 		}
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 	// Go through existing indirect items first
4431da177e4SLinus Torvalds 	// replace all zeroes with blocknumbers from list
4441da177e4SLinus Torvalds 	// Note that if no corresponding item was found, by previous search,
4451da177e4SLinus Torvalds 	// it means there are no existing in-tree representation for file area
4461da177e4SLinus Torvalds 	// we are going to overwrite, so there is nothing to scan through for holes.
447bd4c625cSLinus Torvalds 	for (curr_block = 0, itempos = path.pos_in_item;
448bd4c625cSLinus Torvalds 	     curr_block < blocks_to_allocate && res == POSITION_FOUND;) {
4491da177e4SLinus Torvalds 	      retry:
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		if (itempos >= ih_item_len(ih) / UNFM_P_SIZE) {
4521da177e4SLinus Torvalds 			/* We run out of data in this indirect item, let's look for another
4531da177e4SLinus Torvalds 			   one. */
4541da177e4SLinus Torvalds 			/* First if we are already modifying current item, log it */
4551da177e4SLinus Torvalds 			if (modifying_this_item) {
4561da177e4SLinus Torvalds 				journal_mark_dirty(th, inode->i_sb, bh);
4571da177e4SLinus Torvalds 				modifying_this_item = 0;
4581da177e4SLinus Torvalds 			}
4591da177e4SLinus Torvalds 			/* Then set the key to look for a new indirect item (offset of old
4601da177e4SLinus Torvalds 			   item is added to old item length */
461bd4c625cSLinus Torvalds 			set_cpu_key_k_offset(&key,
462bd4c625cSLinus Torvalds 					     le_key_k_offset
463bd4c625cSLinus Torvalds 					     (get_inode_item_key_version(inode),
464bd4c625cSLinus Torvalds 					      &(ih->ih_key)) +
465bd4c625cSLinus Torvalds 					     op_bytes_number(ih,
466bd4c625cSLinus Torvalds 							     inode->i_sb->
467bd4c625cSLinus Torvalds 							     s_blocksize));
4681da177e4SLinus Torvalds 			/* Search ofor position of new key in the tree. */
469bd4c625cSLinus Torvalds 			res =
470bd4c625cSLinus Torvalds 			    search_for_position_by_key(inode->i_sb, &key,
471bd4c625cSLinus Torvalds 						       &path);
4721da177e4SLinus Torvalds 			if (res == IO_ERROR) {
4731da177e4SLinus Torvalds 				res = -EIO;
4741da177e4SLinus Torvalds 				goto error_exit_free_blocks;
4751da177e4SLinus Torvalds 			}
4761da177e4SLinus Torvalds 			bh = get_last_bh(&path);
4771da177e4SLinus Torvalds 			ih = get_ih(&path);
4781da177e4SLinus Torvalds 			item = get_item(&path);
4791da177e4SLinus Torvalds 			itempos = path.pos_in_item;
4801da177e4SLinus Torvalds 			continue;	// loop to check all kinds of conditions and so on.
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 		/* Ok, we have correct position in item now, so let's see if it is
4831da177e4SLinus Torvalds 		   representing file hole (blocknumber is zero) and fill it if needed */
4841da177e4SLinus Torvalds 		if (!item[itempos]) {
4851da177e4SLinus Torvalds 			/* Ok, a hole. Now we need to check if we already prepared this
4861da177e4SLinus Torvalds 			   block to be journaled */
4871da177e4SLinus Torvalds 			while (!modifying_this_item) {	// loop until succeed
4881da177e4SLinus Torvalds 				/* Well, this item is not journaled yet, so we must prepare
4891da177e4SLinus Torvalds 				   it for journal first, before we can change it */
4901da177e4SLinus Torvalds 				struct item_head tmp_ih;	// We copy item head of found item,
4911da177e4SLinus Torvalds 				// here to detect if fs changed under
4921da177e4SLinus Torvalds 				// us while we were preparing for
4931da177e4SLinus Torvalds 				// journal.
4941da177e4SLinus Torvalds 				int fs_gen;	// We store fs generation here to find if someone
4951da177e4SLinus Torvalds 				// changes fs under our feet
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 				copy_item_head(&tmp_ih, ih);	// Remember itemhead
4981da177e4SLinus Torvalds 				fs_gen = get_generation(inode->i_sb);	// remember fs generation
4991da177e4SLinus Torvalds 				reiserfs_prepare_for_journal(inode->i_sb, bh, 1);	// Prepare a buffer within which indirect item is stored for changing.
500bd4c625cSLinus Torvalds 				if (fs_changed(fs_gen, inode->i_sb)
501bd4c625cSLinus Torvalds 				    && item_moved(&tmp_ih, &path)) {
5021da177e4SLinus Torvalds 					// Sigh, fs was changed under us, we need to look for new
5031da177e4SLinus Torvalds 					// location of item we are working with
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 					/* unmark prepaerd area as journaled and search for it's
5061da177e4SLinus Torvalds 					   new position */
507bd4c625cSLinus Torvalds 					reiserfs_restore_prepared_buffer(inode->
508bd4c625cSLinus Torvalds 									 i_sb,
509bd4c625cSLinus Torvalds 									 bh);
510bd4c625cSLinus Torvalds 					res =
511bd4c625cSLinus Torvalds 					    search_for_position_by_key(inode->
512bd4c625cSLinus Torvalds 								       i_sb,
513bd4c625cSLinus Torvalds 								       &key,
514bd4c625cSLinus Torvalds 								       &path);
5151da177e4SLinus Torvalds 					if (res == IO_ERROR) {
5161da177e4SLinus Torvalds 						res = -EIO;
5171da177e4SLinus Torvalds 						goto error_exit_free_blocks;
5181da177e4SLinus Torvalds 					}
5191da177e4SLinus Torvalds 					bh = get_last_bh(&path);
5201da177e4SLinus Torvalds 					ih = get_ih(&path);
5211da177e4SLinus Torvalds 					item = get_item(&path);
5221da177e4SLinus Torvalds 					itempos = path.pos_in_item;
5231da177e4SLinus Torvalds 					goto retry;
5241da177e4SLinus Torvalds 				}
5251da177e4SLinus Torvalds 				modifying_this_item = 1;
5261da177e4SLinus Torvalds 			}
5271da177e4SLinus Torvalds 			item[itempos] = allocated_blocks[curr_block];	// Assign new block
5281da177e4SLinus Torvalds 			curr_block++;
5291da177e4SLinus Torvalds 		}
5301da177e4SLinus Torvalds 		itempos++;
5311da177e4SLinus Torvalds 	}
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	if (modifying_this_item) {	// We need to log last-accessed block, if it
5341da177e4SLinus Torvalds 		// was modified, but not logged yet.
5351da177e4SLinus Torvalds 		journal_mark_dirty(th, inode->i_sb, bh);
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	if (curr_block < blocks_to_allocate) {
5391da177e4SLinus Torvalds 		// Oh, well need to append to indirect item, or to create indirect item
5401da177e4SLinus Torvalds 		// if there weren't any
5411da177e4SLinus Torvalds 		if (is_indirect_le_ih(ih)) {
5421da177e4SLinus Torvalds 			// Existing indirect item - append. First calculate key for append
5431da177e4SLinus Torvalds 			// position. We do not need to recalculate path as it should
5441da177e4SLinus Torvalds 			// already point to correct place.
545bd4c625cSLinus Torvalds 			make_cpu_key(&key, inode,
546bd4c625cSLinus Torvalds 				     le_key_k_offset(get_inode_item_key_version
547bd4c625cSLinus Torvalds 						     (inode),
548bd4c625cSLinus Torvalds 						     &(ih->ih_key)) +
549bd4c625cSLinus Torvalds 				     op_bytes_number(ih,
550bd4c625cSLinus Torvalds 						     inode->i_sb->s_blocksize),
551bd4c625cSLinus Torvalds 				     TYPE_INDIRECT, 3);
552bd4c625cSLinus Torvalds 			res =
553bd4c625cSLinus Torvalds 			    reiserfs_paste_into_item(th, &path, &key, inode,
554bd4c625cSLinus Torvalds 						     (char *)(allocated_blocks +
555bd4c625cSLinus Torvalds 							      curr_block),
556bd4c625cSLinus Torvalds 						     UNFM_P_SIZE *
557bd4c625cSLinus Torvalds 						     (blocks_to_allocate -
558bd4c625cSLinus Torvalds 						      curr_block));
5591da177e4SLinus Torvalds 			if (res) {
5601da177e4SLinus Torvalds 				goto error_exit_free_blocks;
5611da177e4SLinus Torvalds 			}
5621da177e4SLinus Torvalds 		} else if (is_statdata_le_ih(ih)) {
5631da177e4SLinus Torvalds 			// Last found item was statdata. That means we need to create indirect item.
5641da177e4SLinus Torvalds 			struct item_head ins_ih;	/* itemhead for new item */
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 			/* create a key for our new item */
5671da177e4SLinus Torvalds 			make_cpu_key(&key, inode, 1, TYPE_INDIRECT, 3);	// Position one,
5681da177e4SLinus Torvalds 			// because that's
5691da177e4SLinus Torvalds 			// where first
5701da177e4SLinus Torvalds 			// indirect item
5711da177e4SLinus Torvalds 			// begins
5721da177e4SLinus Torvalds 			/* Create new item head for our new item */
573bd4c625cSLinus Torvalds 			make_le_item_head(&ins_ih, &key, key.version, 1,
574bd4c625cSLinus Torvalds 					  TYPE_INDIRECT,
575bd4c625cSLinus Torvalds 					  (blocks_to_allocate -
576bd4c625cSLinus Torvalds 					   curr_block) * UNFM_P_SIZE,
5771da177e4SLinus Torvalds 					  0 /* free space */ );
5781da177e4SLinus Torvalds 			/* Find where such item should live in the tree */
5791da177e4SLinus Torvalds 			res = search_item(inode->i_sb, &key, &path);
5801da177e4SLinus Torvalds 			if (res != ITEM_NOT_FOUND) {
5811da177e4SLinus Torvalds 				/* Well, if we have found such item already, or some error
5821da177e4SLinus Torvalds 				   occured, we need to warn user and return error */
5831da177e4SLinus Torvalds 				if (res != -ENOSPC) {
5841da177e4SLinus Torvalds 					reiserfs_warning(inode->i_sb,
5851da177e4SLinus Torvalds 							 "green-9009: search_by_key (%K) "
586bd4c625cSLinus Torvalds 							 "returned %d", &key,
587bd4c625cSLinus Torvalds 							 res);
5881da177e4SLinus Torvalds 				}
5891da177e4SLinus Torvalds 				res = -EIO;
5901da177e4SLinus Torvalds 				goto error_exit_free_blocks;
5911da177e4SLinus Torvalds 			}
5921da177e4SLinus Torvalds 			/* Insert item into the tree with the data as its body */
593bd4c625cSLinus Torvalds 			res =
594bd4c625cSLinus Torvalds 			    reiserfs_insert_item(th, &path, &key, &ins_ih,
595bd4c625cSLinus Torvalds 						 inode,
596bd4c625cSLinus Torvalds 						 (char *)(allocated_blocks +
597bd4c625cSLinus Torvalds 							  curr_block));
5981da177e4SLinus Torvalds 		} else {
599bd4c625cSLinus Torvalds 			reiserfs_panic(inode->i_sb,
600bd4c625cSLinus Torvalds 				       "green-9010: unexpected item type for key %K\n",
601bd4c625cSLinus Torvalds 				       &key);
6021da177e4SLinus Torvalds 		}
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 	// the caller is responsible for closing the transaction
6051da177e4SLinus Torvalds 	// unless we return an error, they are also responsible for logging
6061da177e4SLinus Torvalds 	// the inode.
6071da177e4SLinus Torvalds 	//
6081da177e4SLinus Torvalds 	pathrelse(&path);
6091da177e4SLinus Torvalds 	/*
6101da177e4SLinus Torvalds 	 * cleanup prellocation from previous writes
6111da177e4SLinus Torvalds 	 * if this is a partial block write
6121da177e4SLinus Torvalds 	 */
6131da177e4SLinus Torvalds 	if (write_bytes & (inode->i_sb->s_blocksize - 1))
6141da177e4SLinus Torvalds 		reiserfs_discard_prealloc(th, inode);
6151da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	// go through all the pages/buffers and map the buffers to newly allocated
6181da177e4SLinus Torvalds 	// blocks (so that system knows where to write these pages later).
6191da177e4SLinus Torvalds 	curr_block = 0;
6201da177e4SLinus Torvalds 	for (i = 0; i < num_pages; i++) {
6211da177e4SLinus Torvalds 		struct page *page = prepared_pages[i];	//current page
6221da177e4SLinus Torvalds 		struct buffer_head *head = page_buffers(page);	// first buffer for a page
6231da177e4SLinus Torvalds 		int block_start, block_end;	// in-page offsets for buffers.
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 		if (!page_buffers(page))
626bd4c625cSLinus Torvalds 			reiserfs_panic(inode->i_sb,
627bd4c625cSLinus Torvalds 				       "green-9005: No buffers for prepared page???");
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		/* For each buffer in page */
6301da177e4SLinus Torvalds 		for (bh = head, block_start = 0; bh != head || !block_start;
6311da177e4SLinus Torvalds 		     block_start = block_end, bh = bh->b_this_page) {
6321da177e4SLinus Torvalds 			if (!bh)
633bd4c625cSLinus Torvalds 				reiserfs_panic(inode->i_sb,
634bd4c625cSLinus Torvalds 					       "green-9006: Allocated but absent buffer for a page?");
6351da177e4SLinus Torvalds 			block_end = block_start + inode->i_sb->s_blocksize;
6361da177e4SLinus Torvalds 			if (i == 0 && block_end <= from)
6371da177e4SLinus Torvalds 				/* if this buffer is before requested data to map, skip it */
6381da177e4SLinus Torvalds 				continue;
6391da177e4SLinus Torvalds 			if (i == num_pages - 1 && block_start >= to)
6401da177e4SLinus Torvalds 				/* If this buffer is after requested data to map, abort
6411da177e4SLinus Torvalds 				   processing of current page */
6421da177e4SLinus Torvalds 				break;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 			if (!buffer_mapped(bh)) {	// Ok, unmapped buffer, need to map it
645bd4c625cSLinus Torvalds 				map_bh(bh, inode->i_sb,
646bd4c625cSLinus Torvalds 				       le32_to_cpu(allocated_blocks
647bd4c625cSLinus Torvalds 						   [curr_block]));
6481da177e4SLinus Torvalds 				curr_block++;
6491da177e4SLinus Torvalds 				set_buffer_new(bh);
6501da177e4SLinus Torvalds 			}
6511da177e4SLinus Torvalds 		}
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 
654bd4c625cSLinus Torvalds 	RFALSE(curr_block > blocks_to_allocate,
655bd4c625cSLinus Torvalds 	       "green-9007: Used too many blocks? weird");
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	kfree(allocated_blocks);
6581da177e4SLinus Torvalds 	return 0;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds // Need to deal with transaction here.
6611da177e4SLinus Torvalds       error_exit_free_blocks:
6621da177e4SLinus Torvalds 	pathrelse(&path);
6631da177e4SLinus Torvalds 	// free blocks
6641da177e4SLinus Torvalds 	for (i = 0; i < blocks_to_allocate; i++)
665bd4c625cSLinus Torvalds 		reiserfs_free_block(th, inode, le32_to_cpu(allocated_blocks[i]),
666bd4c625cSLinus Torvalds 				    1);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds       error_exit:
6691da177e4SLinus Torvalds 	if (th->t_trans_id) {
6701da177e4SLinus Torvalds 		int err;
6711da177e4SLinus Torvalds 		// update any changes we made to blk count
6729f03783cSChris Mason 		mark_inode_dirty(inode);
673bd4c625cSLinus Torvalds 		err =
674bd4c625cSLinus Torvalds 		    journal_end(th, inode->i_sb,
675bd4c625cSLinus Torvalds 				JOURNAL_PER_BALANCE_CNT * 3 + 1 +
676bd4c625cSLinus Torvalds 				2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb));
6771da177e4SLinus Torvalds 		if (err)
6781da177e4SLinus Torvalds 			res = err;
6791da177e4SLinus Torvalds 	}
6801da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
6811da177e4SLinus Torvalds 	kfree(allocated_blocks);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	return res;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds /* Unlock pages prepared by reiserfs_prepare_file_region_for_write */
6871da177e4SLinus Torvalds static void reiserfs_unprepare_pages(struct page **prepared_pages,	/* list of locked pages */
688bd4c625cSLinus Torvalds 				     size_t num_pages /* amount of pages */ )
689bd4c625cSLinus Torvalds {
6901da177e4SLinus Torvalds 	int i;			// loop counter
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	for (i = 0; i < num_pages; i++) {
6931da177e4SLinus Torvalds 		struct page *page = prepared_pages[i];
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 		try_to_free_buffers(page);
6961da177e4SLinus Torvalds 		unlock_page(page);
6971da177e4SLinus Torvalds 		page_cache_release(page);
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds /* This function will copy data from userspace to specified pages within
7021da177e4SLinus Torvalds    supplied byte range */
703bd4c625cSLinus Torvalds static int reiserfs_copy_from_user_to_file_region(loff_t pos,	/* In-file position */
7041da177e4SLinus Torvalds 						  int num_pages,	/* Number of pages affected */
7051da177e4SLinus Torvalds 						  int write_bytes,	/* Amount of bytes to write */
7061da177e4SLinus Torvalds 						  struct page **prepared_pages,	/* pointer to
7071da177e4SLinus Torvalds 										   array to
7081da177e4SLinus Torvalds 										   prepared pages
7091da177e4SLinus Torvalds 										 */
7101da177e4SLinus Torvalds 						  const char __user * buf	/* Pointer to user-supplied
7111da177e4SLinus Torvalds 										   data */
7121da177e4SLinus Torvalds     )
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	long page_fault = 0;	// status of copy_from_user.
7151da177e4SLinus Torvalds 	int i;			// loop counter.
7161da177e4SLinus Torvalds 	int offset;		// offset in page
7171da177e4SLinus Torvalds 
718bd4c625cSLinus Torvalds 	for (i = 0, offset = (pos & (PAGE_CACHE_SIZE - 1)); i < num_pages;
719bd4c625cSLinus Torvalds 	     i++, offset = 0) {
7201da177e4SLinus Torvalds 		size_t count = min_t(size_t, PAGE_CACHE_SIZE - offset, write_bytes);	// How much of bytes to write to this page
7211da177e4SLinus Torvalds 		struct page *page = prepared_pages[i];	// Current page we process.
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 		fault_in_pages_readable(buf, count);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 		/* Copy data from userspace to the current page */
7261da177e4SLinus Torvalds 		kmap(page);
7271da177e4SLinus Torvalds 		page_fault = __copy_from_user(page_address(page) + offset, buf, count);	// Copy the data.
7281da177e4SLinus Torvalds 		/* Flush processor's dcache for this page */
7291da177e4SLinus Torvalds 		flush_dcache_page(page);
7301da177e4SLinus Torvalds 		kunmap(page);
7311da177e4SLinus Torvalds 		buf += count;
7321da177e4SLinus Torvalds 		write_bytes -= count;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 		if (page_fault)
7351da177e4SLinus Torvalds 			break;	// Was there a fault? abort.
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	return page_fault ? -EFAULT : 0;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds /* taken fs/buffer.c:__block_commit_write */
7421da177e4SLinus Torvalds int reiserfs_commit_page(struct inode *inode, struct page *page,
7431da177e4SLinus Torvalds 			 unsigned from, unsigned to)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	unsigned block_start, block_end;
7461da177e4SLinus Torvalds 	int partial = 0;
7471da177e4SLinus Torvalds 	unsigned blocksize;
7481da177e4SLinus Torvalds 	struct buffer_head *bh, *head;
7491da177e4SLinus Torvalds 	unsigned long i_size_index = inode->i_size >> PAGE_CACHE_SHIFT;
7501da177e4SLinus Torvalds 	int new;
7511da177e4SLinus Torvalds 	int logit = reiserfs_file_data_log(inode);
7521da177e4SLinus Torvalds 	struct super_block *s = inode->i_sb;
7531da177e4SLinus Torvalds 	int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize;
7541da177e4SLinus Torvalds 	struct reiserfs_transaction_handle th;
7551da177e4SLinus Torvalds 	int ret = 0;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	th.t_trans_id = 0;
7581da177e4SLinus Torvalds 	blocksize = 1 << inode->i_blkbits;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (logit) {
7611da177e4SLinus Torvalds 		reiserfs_write_lock(s);
7621da177e4SLinus Torvalds 		ret = journal_begin(&th, s, bh_per_page + 1);
7631da177e4SLinus Torvalds 		if (ret)
7641da177e4SLinus Torvalds 			goto drop_write_lock;
7651da177e4SLinus Torvalds 		reiserfs_update_inode_transaction(inode);
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 	for (bh = head = page_buffers(page), block_start = 0;
7681da177e4SLinus Torvalds 	     bh != head || !block_start;
769bd4c625cSLinus Torvalds 	     block_start = block_end, bh = bh->b_this_page) {
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 		new = buffer_new(bh);
7721da177e4SLinus Torvalds 		clear_buffer_new(bh);
7731da177e4SLinus Torvalds 		block_end = block_start + blocksize;
7741da177e4SLinus Torvalds 		if (block_end <= from || block_start >= to) {
7751da177e4SLinus Torvalds 			if (!buffer_uptodate(bh))
7761da177e4SLinus Torvalds 				partial = 1;
7771da177e4SLinus Torvalds 		} else {
7781da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
7791da177e4SLinus Torvalds 			if (logit) {
7801da177e4SLinus Torvalds 				reiserfs_prepare_for_journal(s, bh, 1);
7811da177e4SLinus Torvalds 				journal_mark_dirty(&th, s, bh);
7821da177e4SLinus Torvalds 			} else if (!buffer_dirty(bh)) {
7831da177e4SLinus Torvalds 				mark_buffer_dirty(bh);
7841da177e4SLinus Torvalds 				/* do data=ordered on any page past the end
7851da177e4SLinus Torvalds 				 * of file and any buffer marked BH_New.
7861da177e4SLinus Torvalds 				 */
7871da177e4SLinus Torvalds 				if (reiserfs_data_ordered(inode->i_sb) &&
7881da177e4SLinus Torvalds 				    (new || page->index >= i_size_index)) {
7891da177e4SLinus Torvalds 					reiserfs_add_ordered_list(inode, bh);
7901da177e4SLinus Torvalds 				}
7911da177e4SLinus Torvalds 			}
7921da177e4SLinus Torvalds 		}
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 	if (logit) {
7951da177e4SLinus Torvalds 		ret = journal_end(&th, s, bh_per_page + 1);
7961da177e4SLinus Torvalds 	      drop_write_lock:
7971da177e4SLinus Torvalds 		reiserfs_write_unlock(s);
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 	/*
8001da177e4SLinus Torvalds 	 * If this is a partial write which happened to make all buffers
8011da177e4SLinus Torvalds 	 * uptodate then we can optimize away a bogus readpage() for
8021da177e4SLinus Torvalds 	 * the next read(). Here we 'discover' whether the page went
8031da177e4SLinus Torvalds 	 * uptodate as a result of this (potentially partial) write.
8041da177e4SLinus Torvalds 	 */
8051da177e4SLinus Torvalds 	if (!partial)
8061da177e4SLinus Torvalds 		SetPageUptodate(page);
8071da177e4SLinus Torvalds 	return ret;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds /* Submit pages for write. This was separated from actual file copying
8111da177e4SLinus Torvalds    because we might want to allocate block numbers in-between.
8121da177e4SLinus Torvalds    This function assumes that caller will adjust file size to correct value. */
813bd4c625cSLinus Torvalds static int reiserfs_submit_file_region_for_write(struct reiserfs_transaction_handle *th, struct inode *inode, loff_t pos,	/* Writing position offset */
8141da177e4SLinus Torvalds 						 size_t num_pages,	/* Number of pages to write */
8151da177e4SLinus Torvalds 						 size_t write_bytes,	/* number of bytes to write */
8161da177e4SLinus Torvalds 						 struct page **prepared_pages	/* list of pages */
8171da177e4SLinus Torvalds     )
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	int status;		// return status of block_commit_write.
8201da177e4SLinus Torvalds 	int retval = 0;		// Return value we are going to return.
8211da177e4SLinus Torvalds 	int i;			// loop counter
8221da177e4SLinus Torvalds 	int offset;		// Writing offset in page.
8231da177e4SLinus Torvalds 	int orig_write_bytes = write_bytes;
8241da177e4SLinus Torvalds 	int sd_update = 0;
8251da177e4SLinus Torvalds 
826bd4c625cSLinus Torvalds 	for (i = 0, offset = (pos & (PAGE_CACHE_SIZE - 1)); i < num_pages;
827bd4c625cSLinus Torvalds 	     i++, offset = 0) {
8281da177e4SLinus Torvalds 		int count = min_t(int, PAGE_CACHE_SIZE - offset, write_bytes);	// How much of bytes to write to this page
8291da177e4SLinus Torvalds 		struct page *page = prepared_pages[i];	// Current page we process.
8301da177e4SLinus Torvalds 
831bd4c625cSLinus Torvalds 		status =
832bd4c625cSLinus Torvalds 		    reiserfs_commit_page(inode, page, offset, offset + count);
8331da177e4SLinus Torvalds 		if (status)
8341da177e4SLinus Torvalds 			retval = status;	// To not overcomplicate matters We are going to
8351da177e4SLinus Torvalds 		// submit all the pages even if there was error.
8361da177e4SLinus Torvalds 		// we only remember error status to report it on
8371da177e4SLinus Torvalds 		// exit.
8381da177e4SLinus Torvalds 		write_bytes -= count;
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds 	/* now that we've gotten all the ordered buffers marked dirty,
8411da177e4SLinus Torvalds 	 * we can safely update i_size and close any running transaction
8421da177e4SLinus Torvalds 	 */
8431da177e4SLinus Torvalds 	if (pos + orig_write_bytes > inode->i_size) {
8441da177e4SLinus Torvalds 		inode->i_size = pos + orig_write_bytes;	// Set new size
8451da177e4SLinus Torvalds 		/* If the file have grown so much that tail packing is no
8461da177e4SLinus Torvalds 		 * longer possible, reset "need to pack" flag */
8471da177e4SLinus Torvalds 		if ((have_large_tails(inode->i_sb) &&
8481da177e4SLinus Torvalds 		     inode->i_size > i_block_size(inode) * 4) ||
8491da177e4SLinus Torvalds 		    (have_small_tails(inode->i_sb) &&
8501da177e4SLinus Torvalds 		     inode->i_size > i_block_size(inode)))
8511da177e4SLinus Torvalds 			REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
8521da177e4SLinus Torvalds 		else if ((have_large_tails(inode->i_sb) &&
8531da177e4SLinus Torvalds 			  inode->i_size < i_block_size(inode) * 4) ||
8541da177e4SLinus Torvalds 			 (have_small_tails(inode->i_sb) &&
8551da177e4SLinus Torvalds 			  inode->i_size < i_block_size(inode)))
8561da177e4SLinus Torvalds 			REISERFS_I(inode)->i_flags |= i_pack_on_close_mask;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		if (th->t_trans_id) {
8591da177e4SLinus Torvalds 			reiserfs_write_lock(inode->i_sb);
8609f03783cSChris Mason 			// this sets the proper flags for O_SYNC to trigger a commit
8619f03783cSChris Mason 			mark_inode_dirty(inode);
8621da177e4SLinus Torvalds 			reiserfs_write_unlock(inode->i_sb);
8631da177e4SLinus Torvalds 		} else
8649f03783cSChris Mason 			mark_inode_dirty(inode);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 		sd_update = 1;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 	if (th->t_trans_id) {
8691da177e4SLinus Torvalds 		reiserfs_write_lock(inode->i_sb);
8701da177e4SLinus Torvalds 		if (!sd_update)
8719f03783cSChris Mason 			mark_inode_dirty(inode);
8721da177e4SLinus Torvalds 		status = journal_end(th, th->t_super, th->t_blocks_allocated);
8731da177e4SLinus Torvalds 		if (status)
8741da177e4SLinus Torvalds 			retval = status;
8751da177e4SLinus Torvalds 		reiserfs_write_unlock(inode->i_sb);
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 	th->t_trans_id = 0;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	/*
8801da177e4SLinus Torvalds 	 * we have to unlock the pages after updating i_size, otherwise
8811da177e4SLinus Torvalds 	 * we race with writepage
8821da177e4SLinus Torvalds 	 */
8831da177e4SLinus Torvalds 	for (i = 0; i < num_pages; i++) {
8841da177e4SLinus Torvalds 		struct page *page = prepared_pages[i];
8851da177e4SLinus Torvalds 		unlock_page(page);
8861da177e4SLinus Torvalds 		mark_page_accessed(page);
8871da177e4SLinus Torvalds 		page_cache_release(page);
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds 	return retval;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds /* Look if passed writing region is going to touch file's tail
8931da177e4SLinus Torvalds    (if it is present). And if it is, convert the tail to unformatted node */
8941da177e4SLinus Torvalds static int reiserfs_check_for_tail_and_convert(struct inode *inode,	/* inode to deal with */
8951da177e4SLinus Torvalds 					       loff_t pos,	/* Writing position */
8961da177e4SLinus Torvalds 					       int write_bytes	/* amount of bytes to write */
8971da177e4SLinus Torvalds     )
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	INITIALIZE_PATH(path);	// needed for search_for_position
9001da177e4SLinus Torvalds 	struct cpu_key key;	// Key that would represent last touched writing byte.
9011da177e4SLinus Torvalds 	struct item_head *ih;	// item header of found block;
9021da177e4SLinus Torvalds 	int res;		// Return value of various functions we call.
9031da177e4SLinus Torvalds 	int cont_expand_offset;	// We will put offset for generic_cont_expand here
9041da177e4SLinus Torvalds 	// This can be int just because tails are created
9051da177e4SLinus Torvalds 	// only for small files.
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds /* this embodies a dependency on a particular tail policy */
9081da177e4SLinus Torvalds 	if (inode->i_size >= inode->i_sb->s_blocksize * 4) {
9091da177e4SLinus Torvalds 		/* such a big files do not have tails, so we won't bother ourselves
9101da177e4SLinus Torvalds 		   to look for tails, simply return */
9111da177e4SLinus Torvalds 		return 0;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	reiserfs_write_lock(inode->i_sb);
9151da177e4SLinus Torvalds 	/* find the item containing the last byte to be written, or if
9161da177e4SLinus Torvalds 	 * writing past the end of the file then the last item of the
9171da177e4SLinus Torvalds 	 * file (and then we check its type). */
918bd4c625cSLinus Torvalds 	make_cpu_key(&key, inode, pos + write_bytes + 1, TYPE_ANY,
919bd4c625cSLinus Torvalds 		     3 /*key length */ );
9201da177e4SLinus Torvalds 	res = search_for_position_by_key(inode->i_sb, &key, &path);
9211da177e4SLinus Torvalds 	if (res == IO_ERROR) {
9221da177e4SLinus Torvalds 		reiserfs_write_unlock(inode->i_sb);
9231da177e4SLinus Torvalds 		return -EIO;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 	ih = get_ih(&path);
9261da177e4SLinus Torvalds 	res = 0;
9271da177e4SLinus Torvalds 	if (is_direct_le_ih(ih)) {
9281da177e4SLinus Torvalds 		/* Ok, closest item is file tail (tails are stored in "direct"
9291da177e4SLinus Torvalds 		 * items), so we need to unpack it. */
9301da177e4SLinus Torvalds 		/* To not overcomplicate matters, we just call generic_cont_expand
9311da177e4SLinus Torvalds 		   which will in turn call other stuff and finally will boil down to
9321da177e4SLinus Torvalds 		   reiserfs_get_block() that would do necessary conversion. */
933bd4c625cSLinus Torvalds 		cont_expand_offset =
934bd4c625cSLinus Torvalds 		    le_key_k_offset(get_inode_item_key_version(inode),
935bd4c625cSLinus Torvalds 				    &(ih->ih_key));
9361da177e4SLinus Torvalds 		pathrelse(&path);
9371da177e4SLinus Torvalds 		res = generic_cont_expand(inode, cont_expand_offset);
9381da177e4SLinus Torvalds 	} else
9391da177e4SLinus Torvalds 		pathrelse(&path);
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
9421da177e4SLinus Torvalds 	return res;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds /* This function locks pages starting from @pos for @inode.
9461da177e4SLinus Torvalds    @num_pages pages are locked and stored in
9471da177e4SLinus Torvalds    @prepared_pages array. Also buffers are allocated for these pages.
9481da177e4SLinus Torvalds    First and last page of the region is read if it is overwritten only
9491da177e4SLinus Torvalds    partially. If last page did not exist before write (file hole or file
9501da177e4SLinus Torvalds    append), it is zeroed, then.
9511da177e4SLinus Torvalds    Returns number of unallocated blocks that should be allocated to cover
9521da177e4SLinus Torvalds    new file data.*/
953bd4c625cSLinus Torvalds static int reiserfs_prepare_file_region_for_write(struct inode *inode
954bd4c625cSLinus Torvalds 						  /* Inode of the file */ ,
9551da177e4SLinus Torvalds 						  loff_t pos,	/* position in the file */
9561da177e4SLinus Torvalds 						  size_t num_pages,	/* number of pages to
9571da177e4SLinus Torvalds 									   prepare */
9581da177e4SLinus Torvalds 						  size_t write_bytes,	/* Amount of bytes to be
9591da177e4SLinus Torvalds 									   overwritten from
9601da177e4SLinus Torvalds 									   @pos */
9611da177e4SLinus Torvalds 						  struct page **prepared_pages	/* pointer to array
9621da177e4SLinus Torvalds 										   where to store
9631da177e4SLinus Torvalds 										   prepared pages */
9641da177e4SLinus Torvalds     )
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	int res = 0;		// Return values of different functions we call.
9671da177e4SLinus Torvalds 	unsigned long index = pos >> PAGE_CACHE_SHIFT;	// Offset in file in pages.
9681da177e4SLinus Torvalds 	int from = (pos & (PAGE_CACHE_SIZE - 1));	// Writing offset in first page
9691da177e4SLinus Torvalds 	int to = ((pos + write_bytes - 1) & (PAGE_CACHE_SIZE - 1)) + 1;
9701da177e4SLinus Torvalds 	/* offset of last modified byte in last
9711da177e4SLinus Torvalds 	   page */
9721da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;	// Pages are mapped here.
9731da177e4SLinus Torvalds 	int i;			// Simple counter
9741da177e4SLinus Torvalds 	int blocks = 0;		/* Return value (blocks that should be allocated) */
9751da177e4SLinus Torvalds 	struct buffer_head *bh, *head;	// Current bufferhead and first bufferhead
9761da177e4SLinus Torvalds 	// of a page.
9771da177e4SLinus Torvalds 	unsigned block_start, block_end;	// Starting and ending offsets of current
9781da177e4SLinus Torvalds 	// buffer in the page.
9791da177e4SLinus Torvalds 	struct buffer_head *wait[2], **wait_bh = wait;	// Buffers for page, if
9801da177e4SLinus Torvalds 	// Page appeared to be not up
9811da177e4SLinus Torvalds 	// to date. Note how we have
9821da177e4SLinus Torvalds 	// at most 2 buffers, this is
9831da177e4SLinus Torvalds 	// because we at most may
9841da177e4SLinus Torvalds 	// partially overwrite two
9851da177e4SLinus Torvalds 	// buffers for one page. One at                                                 // the beginning of write area
9861da177e4SLinus Torvalds 	// and one at the end.
9871da177e4SLinus Torvalds 	// Everything inthe middle gets                                                 // overwritten totally.
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	struct cpu_key key;	// cpu key of item that we are going to deal with
9901da177e4SLinus Torvalds 	struct item_head *ih = NULL;	// pointer to item head that we are going to deal with
9911da177e4SLinus Torvalds 	struct buffer_head *itembuf = NULL;	// Buffer head that contains items that we are going to deal with
9921da177e4SLinus Torvalds 	INITIALIZE_PATH(path);	// path to item, that we are going to deal with.
9933e8962beSAl Viro 	__le32 *item = NULL;	// pointer to item we are going to deal with
9941da177e4SLinus Torvalds 	int item_pos = -1;	/* Position in indirect item */
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (num_pages < 1) {
9971da177e4SLinus Torvalds 		reiserfs_warning(inode->i_sb,
9981da177e4SLinus Torvalds 				 "green-9001: reiserfs_prepare_file_region_for_write "
9991da177e4SLinus Torvalds 				 "called with zero number of pages to process");
10001da177e4SLinus Torvalds 		return -EFAULT;
10011da177e4SLinus Torvalds 	}
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	/* We have 2 loops for pages. In first loop we grab and lock the pages, so
10041da177e4SLinus Torvalds 	   that nobody would touch these until we release the pages. Then
10051da177e4SLinus Torvalds 	   we'd start to deal with mapping buffers to blocks. */
10061da177e4SLinus Torvalds 	for (i = 0; i < num_pages; i++) {
10071da177e4SLinus Torvalds 		prepared_pages[i] = grab_cache_page(mapping, index + i);	// locks the page
10081da177e4SLinus Torvalds 		if (!prepared_pages[i]) {
10091da177e4SLinus Torvalds 			res = -ENOMEM;
10101da177e4SLinus Torvalds 			goto failed_page_grabbing;
10111da177e4SLinus Torvalds 		}
10121da177e4SLinus Torvalds 		if (!page_has_buffers(prepared_pages[i]))
1013bd4c625cSLinus Torvalds 			create_empty_buffers(prepared_pages[i],
1014bd4c625cSLinus Torvalds 					     inode->i_sb->s_blocksize, 0);
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	/* Let's count amount of blocks for a case where all the blocks
10181da177e4SLinus Torvalds 	   overwritten are new (we will substract already allocated blocks later) */
10191da177e4SLinus Torvalds 	if (num_pages > 2)
10201da177e4SLinus Torvalds 		/* These are full-overwritten pages so we count all the blocks in
10211da177e4SLinus Torvalds 		   these pages are counted as needed to be allocated */
1022bd4c625cSLinus Torvalds 		blocks =
1023bd4c625cSLinus Torvalds 		    (num_pages - 2) << (PAGE_CACHE_SHIFT - inode->i_blkbits);
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	/* count blocks needed for first page (possibly partially written) */
1026bd4c625cSLinus Torvalds 	blocks += ((PAGE_CACHE_SIZE - from) >> inode->i_blkbits) + !!(from & (inode->i_sb->s_blocksize - 1));	/* roundup */
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	/* Now we account for last page. If last page == first page (we
10291da177e4SLinus Torvalds 	   overwrite only one page), we substract all the blocks past the
10301da177e4SLinus Torvalds 	   last writing position in a page out of already calculated number
10311da177e4SLinus Torvalds 	   of blocks */
10321da177e4SLinus Torvalds 	blocks += ((num_pages > 1) << (PAGE_CACHE_SHIFT - inode->i_blkbits)) -
10331da177e4SLinus Torvalds 	    ((PAGE_CACHE_SIZE - to) >> inode->i_blkbits);
10341da177e4SLinus Torvalds 	/* Note how we do not roundup here since partial blocks still
10351da177e4SLinus Torvalds 	   should be allocated */
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	/* Now if all the write area lies past the file end, no point in
10381da177e4SLinus Torvalds 	   maping blocks, since there is none, so we just zero out remaining
10391da177e4SLinus Torvalds 	   parts of first and last pages in write area (if needed) */
10401da177e4SLinus Torvalds 	if ((pos & ~((loff_t) PAGE_CACHE_SIZE - 1)) > inode->i_size) {
10411da177e4SLinus Torvalds 		if (from != 0) {	/* First page needs to be partially zeroed */
10421da177e4SLinus Torvalds 			char *kaddr = kmap_atomic(prepared_pages[0], KM_USER0);
10431da177e4SLinus Torvalds 			memset(kaddr, 0, from);
10441da177e4SLinus Torvalds 			kunmap_atomic(kaddr, KM_USER0);
10451da177e4SLinus Torvalds 		}
10461da177e4SLinus Torvalds 		if (to != PAGE_CACHE_SIZE) {	/* Last page needs to be partially zeroed */
1047bd4c625cSLinus Torvalds 			char *kaddr =
1048bd4c625cSLinus Torvalds 			    kmap_atomic(prepared_pages[num_pages - 1],
1049bd4c625cSLinus Torvalds 					KM_USER0);
10501da177e4SLinus Torvalds 			memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
10511da177e4SLinus Torvalds 			kunmap_atomic(kaddr, KM_USER0);
10521da177e4SLinus Torvalds 		}
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 		/* Since all blocks are new - use already calculated value */
10551da177e4SLinus Torvalds 		return blocks;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	/* Well, since we write somewhere into the middle of a file, there is
10591da177e4SLinus Torvalds 	   possibility we are writing over some already allocated blocks, so
10601da177e4SLinus Torvalds 	   let's map these blocks and substract number of such blocks out of blocks
10611da177e4SLinus Torvalds 	   we need to allocate (calculated above) */
10621da177e4SLinus Torvalds 	/* Mask write position to start on blocksize, we do it out of the
10631da177e4SLinus Torvalds 	   loop for performance reasons */
10641da177e4SLinus Torvalds 	pos &= ~((loff_t) inode->i_sb->s_blocksize - 1);
10651da177e4SLinus Torvalds 	/* Set cpu key to the starting position in a file (on left block boundary) */
1066bd4c625cSLinus Torvalds 	make_cpu_key(&key, inode,
1067bd4c625cSLinus Torvalds 		     1 + ((pos) & ~((loff_t) inode->i_sb->s_blocksize - 1)),
1068bd4c625cSLinus Torvalds 		     TYPE_ANY, 3 /*key length */ );
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	reiserfs_write_lock(inode->i_sb);	// We need that for at least search_by_key()
10711da177e4SLinus Torvalds 	for (i = 0; i < num_pages; i++) {
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 		head = page_buffers(prepared_pages[i]);
10741da177e4SLinus Torvalds 		/* For each buffer in the page */
10751da177e4SLinus Torvalds 		for (bh = head, block_start = 0; bh != head || !block_start;
10761da177e4SLinus Torvalds 		     block_start = block_end, bh = bh->b_this_page) {
10771da177e4SLinus Torvalds 			if (!bh)
1078bd4c625cSLinus Torvalds 				reiserfs_panic(inode->i_sb,
1079bd4c625cSLinus Torvalds 					       "green-9002: Allocated but absent buffer for a page?");
10801da177e4SLinus Torvalds 			/* Find where this buffer ends */
10811da177e4SLinus Torvalds 			block_end = block_start + inode->i_sb->s_blocksize;
10821da177e4SLinus Torvalds 			if (i == 0 && block_end <= from)
10831da177e4SLinus Torvalds 				/* if this buffer is before requested data to map, skip it */
10841da177e4SLinus Torvalds 				continue;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 			if (i == num_pages - 1 && block_start >= to) {
10871da177e4SLinus Torvalds 				/* If this buffer is after requested data to map, abort
10881da177e4SLinus Torvalds 				   processing of current page */
10891da177e4SLinus Torvalds 				break;
10901da177e4SLinus Torvalds 			}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 			if (buffer_mapped(bh) && bh->b_blocknr != 0) {
10931da177e4SLinus Torvalds 				/* This is optimisation for a case where buffer is mapped
10941da177e4SLinus Torvalds 				   and have blocknumber assigned. In case significant amount
10951da177e4SLinus Torvalds 				   of such buffers are present, we may avoid some amount
10961da177e4SLinus Torvalds 				   of search_by_key calls.
10971da177e4SLinus Torvalds 				   Probably it would be possible to move parts of this code
10981da177e4SLinus Torvalds 				   out of BKL, but I afraid that would overcomplicate code
10991da177e4SLinus Torvalds 				   without any noticeable benefit.
11001da177e4SLinus Torvalds 				 */
11011da177e4SLinus Torvalds 				item_pos++;
11021da177e4SLinus Torvalds 				/* Update the key */
1103bd4c625cSLinus Torvalds 				set_cpu_key_k_offset(&key,
1104bd4c625cSLinus Torvalds 						     cpu_key_k_offset(&key) +
1105bd4c625cSLinus Torvalds 						     inode->i_sb->s_blocksize);
11061da177e4SLinus Torvalds 				blocks--;	// Decrease the amount of blocks that need to be
11071da177e4SLinus Torvalds 				// allocated
11081da177e4SLinus Torvalds 				continue;	// Go to the next buffer
11091da177e4SLinus Torvalds 			}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 			if (!itembuf ||	/* if first iteration */
1112bd4c625cSLinus Torvalds 			    item_pos >= ih_item_len(ih) / UNFM_P_SIZE) {	/* or if we progressed past the
11131da177e4SLinus Torvalds 										   current unformatted_item */
11141da177e4SLinus Torvalds 				/* Try to find next item */
1115bd4c625cSLinus Torvalds 				res =
1116bd4c625cSLinus Torvalds 				    search_for_position_by_key(inode->i_sb,
1117bd4c625cSLinus Torvalds 							       &key, &path);
11181da177e4SLinus Torvalds 				/* Abort if no more items */
11191da177e4SLinus Torvalds 				if (res != POSITION_FOUND) {
11201da177e4SLinus Torvalds 					/* make sure later loops don't use this item */
11211da177e4SLinus Torvalds 					itembuf = NULL;
11221da177e4SLinus Torvalds 					item = NULL;
11231da177e4SLinus Torvalds 					break;
11241da177e4SLinus Torvalds 				}
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 				/* Update information about current indirect item */
11271da177e4SLinus Torvalds 				itembuf = get_last_bh(&path);
11281da177e4SLinus Torvalds 				ih = get_ih(&path);
11291da177e4SLinus Torvalds 				item = get_item(&path);
11301da177e4SLinus Torvalds 				item_pos = path.pos_in_item;
11311da177e4SLinus Torvalds 
1132bd4c625cSLinus Torvalds 				RFALSE(!is_indirect_le_ih(ih),
1133bd4c625cSLinus Torvalds 				       "green-9003: indirect item expected");
11341da177e4SLinus Torvalds 			}
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 			/* See if there is some block associated with the file
11371da177e4SLinus Torvalds 			   at that position, map the buffer to this block */
11381da177e4SLinus Torvalds 			if (get_block_num(item, item_pos)) {
1139bd4c625cSLinus Torvalds 				map_bh(bh, inode->i_sb,
1140bd4c625cSLinus Torvalds 				       get_block_num(item, item_pos));
11411da177e4SLinus Torvalds 				blocks--;	// Decrease the amount of blocks that need to be
11421da177e4SLinus Torvalds 				// allocated
11431da177e4SLinus Torvalds 			}
11441da177e4SLinus Torvalds 			item_pos++;
11451da177e4SLinus Torvalds 			/* Update the key */
1146bd4c625cSLinus Torvalds 			set_cpu_key_k_offset(&key,
1147bd4c625cSLinus Torvalds 					     cpu_key_k_offset(&key) +
1148bd4c625cSLinus Torvalds 					     inode->i_sb->s_blocksize);
11491da177e4SLinus Torvalds 		}
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds 	pathrelse(&path);	// Free the path
11521da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	/* Now zero out unmappend buffers for the first and last pages of
11551da177e4SLinus Torvalds 	   write area or issue read requests if page is mapped. */
11561da177e4SLinus Torvalds 	/* First page, see if it is not uptodate */
11571da177e4SLinus Torvalds 	if (!PageUptodate(prepared_pages[0])) {
11581da177e4SLinus Torvalds 		head = page_buffers(prepared_pages[0]);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 		/* For each buffer in page */
11611da177e4SLinus Torvalds 		for (bh = head, block_start = 0; bh != head || !block_start;
11621da177e4SLinus Torvalds 		     block_start = block_end, bh = bh->b_this_page) {
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 			if (!bh)
1165bd4c625cSLinus Torvalds 				reiserfs_panic(inode->i_sb,
1166bd4c625cSLinus Torvalds 					       "green-9002: Allocated but absent buffer for a page?");
11671da177e4SLinus Torvalds 			/* Find where this buffer ends */
11681da177e4SLinus Torvalds 			block_end = block_start + inode->i_sb->s_blocksize;
11691da177e4SLinus Torvalds 			if (block_end <= from)
11701da177e4SLinus Torvalds 				/* if this buffer is before requested data to map, skip it */
11711da177e4SLinus Torvalds 				continue;
11721da177e4SLinus Torvalds 			if (block_start < from) {	/* Aha, our partial buffer */
11731da177e4SLinus Torvalds 				if (buffer_mapped(bh)) {	/* If it is mapped, we need to
11741da177e4SLinus Torvalds 								   issue READ request for it to
11751da177e4SLinus Torvalds 								   not loose data */
11761da177e4SLinus Torvalds 					ll_rw_block(READ, 1, &bh);
11771da177e4SLinus Torvalds 					*wait_bh++ = bh;
11781da177e4SLinus Torvalds 				} else {	/* Not mapped, zero it */
1179bd4c625cSLinus Torvalds 					char *kaddr =
1180bd4c625cSLinus Torvalds 					    kmap_atomic(prepared_pages[0],
1181bd4c625cSLinus Torvalds 							KM_USER0);
1182bd4c625cSLinus Torvalds 					memset(kaddr + block_start, 0,
1183bd4c625cSLinus Torvalds 					       from - block_start);
11841da177e4SLinus Torvalds 					kunmap_atomic(kaddr, KM_USER0);
11851da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
11861da177e4SLinus Torvalds 				}
11871da177e4SLinus Torvalds 			}
11881da177e4SLinus Torvalds 		}
11891da177e4SLinus Torvalds 	}
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	/* Last page, see if it is not uptodate, or if the last page is past the end of the file. */
11921da177e4SLinus Torvalds 	if (!PageUptodate(prepared_pages[num_pages - 1]) ||
1193bd4c625cSLinus Torvalds 	    ((pos + write_bytes) >> PAGE_CACHE_SHIFT) >
1194bd4c625cSLinus Torvalds 	    (inode->i_size >> PAGE_CACHE_SHIFT)) {
11951da177e4SLinus Torvalds 		head = page_buffers(prepared_pages[num_pages - 1]);
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		/* for each buffer in page */
11981da177e4SLinus Torvalds 		for (bh = head, block_start = 0; bh != head || !block_start;
11991da177e4SLinus Torvalds 		     block_start = block_end, bh = bh->b_this_page) {
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 			if (!bh)
1202bd4c625cSLinus Torvalds 				reiserfs_panic(inode->i_sb,
1203bd4c625cSLinus Torvalds 					       "green-9002: Allocated but absent buffer for a page?");
12041da177e4SLinus Torvalds 			/* Find where this buffer ends */
12051da177e4SLinus Torvalds 			block_end = block_start + inode->i_sb->s_blocksize;
12061da177e4SLinus Torvalds 			if (block_start >= to)
12071da177e4SLinus Torvalds 				/* if this buffer is after requested data to map, skip it */
12081da177e4SLinus Torvalds 				break;
12091da177e4SLinus Torvalds 			if (block_end > to) {	/* Aha, our partial buffer */
12101da177e4SLinus Torvalds 				if (buffer_mapped(bh)) {	/* If it is mapped, we need to
12111da177e4SLinus Torvalds 								   issue READ request for it to
12121da177e4SLinus Torvalds 								   not loose data */
12131da177e4SLinus Torvalds 					ll_rw_block(READ, 1, &bh);
12141da177e4SLinus Torvalds 					*wait_bh++ = bh;
12151da177e4SLinus Torvalds 				} else {	/* Not mapped, zero it */
1216bd4c625cSLinus Torvalds 					char *kaddr =
1217bd4c625cSLinus Torvalds 					    kmap_atomic(prepared_pages
1218bd4c625cSLinus Torvalds 							[num_pages - 1],
1219bd4c625cSLinus Torvalds 							KM_USER0);
12201da177e4SLinus Torvalds 					memset(kaddr + to, 0, block_end - to);
12211da177e4SLinus Torvalds 					kunmap_atomic(kaddr, KM_USER0);
12221da177e4SLinus Torvalds 					set_buffer_uptodate(bh);
12231da177e4SLinus Torvalds 				}
12241da177e4SLinus Torvalds 			}
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/* Wait for read requests we made to happen, if necessary */
12291da177e4SLinus Torvalds 	while (wait_bh > wait) {
12301da177e4SLinus Torvalds 		wait_on_buffer(*--wait_bh);
12311da177e4SLinus Torvalds 		if (!buffer_uptodate(*wait_bh)) {
12321da177e4SLinus Torvalds 			res = -EIO;
12331da177e4SLinus Torvalds 			goto failed_read;
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	return blocks;
12381da177e4SLinus Torvalds       failed_page_grabbing:
12391da177e4SLinus Torvalds 	num_pages = i;
12401da177e4SLinus Torvalds       failed_read:
12411da177e4SLinus Torvalds 	reiserfs_unprepare_pages(prepared_pages, num_pages);
12421da177e4SLinus Torvalds 	return res;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds /* Write @count bytes at position @ppos in a file indicated by @file
12461da177e4SLinus Torvalds    from the buffer @buf.
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds    generic_file_write() is only appropriate for filesystems that are not seeking to optimize performance and want
12491da177e4SLinus Torvalds    something simple that works.  It is not for serious use by general purpose filesystems, excepting the one that it was
12501da177e4SLinus Torvalds    written for (ext2/3).  This is for several reasons:
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds    * It has no understanding of any filesystem specific optimizations.
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds    * It enters the filesystem repeatedly for each page that is written.
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds    * It depends on reiserfs_get_block() function which if implemented by reiserfs performs costly search_by_key
12571da177e4SLinus Torvalds    * operation for each page it is supplied with. By contrast reiserfs_file_write() feeds as much as possible at a time
12581da177e4SLinus Torvalds    * to reiserfs which allows for fewer tree traversals.
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds    * Each indirect pointer insertion takes a lot of cpu, because it involves memory moves inside of blocks.
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds    * Asking the block allocation code for blocks one at a time is slightly less efficient.
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds    All of these reasons for not using only generic file write were understood back when reiserfs was first miscoded to
12651da177e4SLinus Torvalds    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
12661da177e4SLinus Torvalds    things right finally.
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds    Future Features: providing search_by_key with hints.
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds */
12711da177e4SLinus Torvalds static ssize_t reiserfs_file_write(struct file *file,	/* the file we are going to write into */
12721da177e4SLinus Torvalds 				   const char __user * buf,	/*  pointer to user supplied data
12731da177e4SLinus Torvalds 								   (in userspace) */
12741da177e4SLinus Torvalds 				   size_t count,	/* amount of bytes to write */
12751da177e4SLinus Torvalds 				   loff_t * ppos	/* pointer to position in file that we start writing at. Should be updated to
1276bd4c625cSLinus Torvalds 							 * new current position before returning. */
1277bd4c625cSLinus Torvalds 				   )
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	size_t already_written = 0;	// Number of bytes already written to the file.
12801da177e4SLinus Torvalds 	loff_t pos;		// Current position in the file.
12811da177e4SLinus Torvalds 	ssize_t res;		// return value of various functions that we call.
12821da177e4SLinus Torvalds 	int err = 0;
12831da177e4SLinus Torvalds 	struct inode *inode = file->f_dentry->d_inode;	// Inode of the file that we are writing to.
12841da177e4SLinus Torvalds 	/* To simplify coding at this time, we store
12851da177e4SLinus Torvalds 	   locked pages in array for now */
12861da177e4SLinus Torvalds 	struct page *prepared_pages[REISERFS_WRITE_PAGES_AT_A_TIME];
12871da177e4SLinus Torvalds 	struct reiserfs_transaction_handle th;
12881da177e4SLinus Torvalds 	th.t_trans_id = 0;
12891da177e4SLinus Torvalds 
1290fa385befSJeff Mahoney 	/* If a filesystem is converted from 3.5 to 3.6, we'll have v3.5 items
1291fa385befSJeff Mahoney 	* lying around (most of the disk, in fact). Despite the filesystem
1292fa385befSJeff Mahoney 	* now being a v3.6 format, the old items still can't support large
1293fa385befSJeff Mahoney 	* file sizes. Catch this case here, as the rest of the VFS layer is
1294fa385befSJeff Mahoney 	* oblivious to the different limitations between old and new items.
1295fa385befSJeff Mahoney 	* reiserfs_setattr catches this for truncates. This chunk is lifted
1296fa385befSJeff Mahoney 	* from generic_write_checks. */
1297fa385befSJeff Mahoney 	if (get_inode_item_key_version (inode) == KEY_FORMAT_3_5 &&
1298fa385befSJeff Mahoney 	    *ppos + count > MAX_NON_LFS) {
1299fa385befSJeff Mahoney 		if (*ppos >= MAX_NON_LFS) {
1300fa385befSJeff Mahoney 			send_sig(SIGXFSZ, current, 0);
1301fa385befSJeff Mahoney 			return -EFBIG;
1302fa385befSJeff Mahoney 		}
1303fa385befSJeff Mahoney 		if (count > MAX_NON_LFS - (unsigned long)*ppos)
1304fa385befSJeff Mahoney 			count = MAX_NON_LFS - (unsigned long)*ppos;
1305fa385befSJeff Mahoney 	}
1306fa385befSJeff Mahoney 
13071da177e4SLinus Torvalds 	if (file->f_flags & O_DIRECT) {	// Direct IO needs treatment
13081da177e4SLinus Torvalds 		ssize_t result, after_file_end = 0;
1309bd4c625cSLinus Torvalds 		if ((*ppos + count >= inode->i_size)
1310bd4c625cSLinus Torvalds 		    || (file->f_flags & O_APPEND)) {
13111da177e4SLinus Torvalds 			/* If we are appending a file, we need to put this savelink in here.
13121da177e4SLinus Torvalds 			   If we will crash while doing direct io, finish_unfinished will
13131da177e4SLinus Torvalds 			   cut the garbage from the file end. */
13141da177e4SLinus Torvalds 			reiserfs_write_lock(inode->i_sb);
1315bd4c625cSLinus Torvalds 			err =
1316bd4c625cSLinus Torvalds 			    journal_begin(&th, inode->i_sb,
1317bd4c625cSLinus Torvalds 					  JOURNAL_PER_BALANCE_CNT);
13181da177e4SLinus Torvalds 			if (err) {
13191da177e4SLinus Torvalds 				reiserfs_write_unlock(inode->i_sb);
13201da177e4SLinus Torvalds 				return err;
13211da177e4SLinus Torvalds 			}
13221da177e4SLinus Torvalds 			reiserfs_update_inode_transaction(inode);
13231da177e4SLinus Torvalds 			add_save_link(&th, inode, 1 /* Truncate */ );
13241da177e4SLinus Torvalds 			after_file_end = 1;
1325bd4c625cSLinus Torvalds 			err =
1326bd4c625cSLinus Torvalds 			    journal_end(&th, inode->i_sb,
1327bd4c625cSLinus Torvalds 					JOURNAL_PER_BALANCE_CNT);
13281da177e4SLinus Torvalds 			reiserfs_write_unlock(inode->i_sb);
13291da177e4SLinus Torvalds 			if (err)
13301da177e4SLinus Torvalds 				return err;
13311da177e4SLinus Torvalds 		}
13321da177e4SLinus Torvalds 		result = generic_file_write(file, buf, count, ppos);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		if (after_file_end) {	/* Now update i_size and remove the savelink */
13351da177e4SLinus Torvalds 			struct reiserfs_transaction_handle th;
13361da177e4SLinus Torvalds 			reiserfs_write_lock(inode->i_sb);
13371da177e4SLinus Torvalds 			err = journal_begin(&th, inode->i_sb, 1);
13381da177e4SLinus Torvalds 			if (err) {
13391da177e4SLinus Torvalds 				reiserfs_write_unlock(inode->i_sb);
13401da177e4SLinus Torvalds 				return err;
13411da177e4SLinus Torvalds 			}
13421da177e4SLinus Torvalds 			reiserfs_update_inode_transaction(inode);
13439f03783cSChris Mason 			mark_inode_dirty(inode);
13441da177e4SLinus Torvalds 			err = journal_end(&th, inode->i_sb, 1);
13451da177e4SLinus Torvalds 			if (err) {
13461da177e4SLinus Torvalds 				reiserfs_write_unlock(inode->i_sb);
13471da177e4SLinus Torvalds 				return err;
13481da177e4SLinus Torvalds 			}
13491da177e4SLinus Torvalds 			err = remove_save_link(inode, 1 /* truncate */ );
13501da177e4SLinus Torvalds 			reiserfs_write_unlock(inode->i_sb);
13511da177e4SLinus Torvalds 			if (err)
13521da177e4SLinus Torvalds 				return err;
13531da177e4SLinus Torvalds 		}
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 		return result;
13561da177e4SLinus Torvalds 	}
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	if (unlikely((ssize_t) count < 0))
13591da177e4SLinus Torvalds 		return -EINVAL;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	if (unlikely(!access_ok(VERIFY_READ, buf, count)))
13621da177e4SLinus Torvalds 		return -EFAULT;
13631da177e4SLinus Torvalds 
13641b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);	// locks the entire file for just us
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	pos = *ppos;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	/* Check if we can write to specified region of file, file
13691da177e4SLinus Torvalds 	   is not overly big and this kind of stuff. Adjust pos and
13701da177e4SLinus Torvalds 	   count, if needed */
13711da177e4SLinus Torvalds 	res = generic_write_checks(file, &pos, &count, 0);
13721da177e4SLinus Torvalds 	if (res)
13731da177e4SLinus Torvalds 		goto out;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	if (count == 0)
13761da177e4SLinus Torvalds 		goto out;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	res = remove_suid(file->f_dentry);
13791da177e4SLinus Torvalds 	if (res)
13801da177e4SLinus Torvalds 		goto out;
13811da177e4SLinus Torvalds 
1382870f4817SChristoph Hellwig 	file_update_time(file);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	// Ok, we are done with all the checks.
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	// Now we should start real work
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	/* If we are going to write past the file's packed tail or if we are going
13891da177e4SLinus Torvalds 	   to overwrite part of the tail, we need that tail to be converted into
13901da177e4SLinus Torvalds 	   unformatted node */
13911da177e4SLinus Torvalds 	res = reiserfs_check_for_tail_and_convert(inode, pos, count);
13921da177e4SLinus Torvalds 	if (res)
13931da177e4SLinus Torvalds 		goto out;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	while (count > 0) {
13961da177e4SLinus Torvalds 		/* This is the main loop in which we running until some error occures
13971da177e4SLinus Torvalds 		   or until we write all of the data. */
13981da177e4SLinus Torvalds 		size_t num_pages;	/* amount of pages we are going to write this iteration */
13991da177e4SLinus Torvalds 		size_t write_bytes;	/* amount of bytes to write during this iteration */
14001da177e4SLinus Torvalds 		size_t blocks_to_allocate;	/* how much blocks we need to allocate for this iteration */
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 		/*  (pos & (PAGE_CACHE_SIZE-1)) is an idiom for offset into a page of pos */
14031da177e4SLinus Torvalds 		num_pages = !!((pos + count) & (PAGE_CACHE_SIZE - 1)) +	/* round up partial
14041da177e4SLinus Torvalds 									   pages */
1405bd4c625cSLinus Torvalds 		    ((count +
1406bd4c625cSLinus Torvalds 		      (pos & (PAGE_CACHE_SIZE - 1))) >> PAGE_CACHE_SHIFT);
14071da177e4SLinus Torvalds 		/* convert size to amount of
14081da177e4SLinus Torvalds 		   pages */
14091da177e4SLinus Torvalds 		reiserfs_write_lock(inode->i_sb);
14101da177e4SLinus Torvalds 		if (num_pages > REISERFS_WRITE_PAGES_AT_A_TIME
14111da177e4SLinus Torvalds 		    || num_pages > reiserfs_can_fit_pages(inode->i_sb)) {
14121da177e4SLinus Torvalds 			/* If we were asked to write more data than we want to or if there
14131da177e4SLinus Torvalds 			   is not that much space, then we shorten amount of data to write
14141da177e4SLinus Torvalds 			   for this iteration. */
1415bd4c625cSLinus Torvalds 			num_pages =
1416bd4c625cSLinus Torvalds 			    min_t(size_t, REISERFS_WRITE_PAGES_AT_A_TIME,
1417bd4c625cSLinus Torvalds 				  reiserfs_can_fit_pages(inode->i_sb));
14181da177e4SLinus Torvalds 			/* Also we should not forget to set size in bytes accordingly */
14191da177e4SLinus Torvalds 			write_bytes = (num_pages << PAGE_CACHE_SHIFT) -
14201da177e4SLinus Torvalds 			    (pos & (PAGE_CACHE_SIZE - 1));
14211da177e4SLinus Torvalds 			/* If position is not on the
14221da177e4SLinus Torvalds 			   start of the page, we need
14231da177e4SLinus Torvalds 			   to substract the offset
14241da177e4SLinus Torvalds 			   within page */
14251da177e4SLinus Torvalds 		} else
14261da177e4SLinus Torvalds 			write_bytes = count;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 		/* reserve the blocks to be allocated later, so that later on
14291da177e4SLinus Torvalds 		   we still have the space to write the blocks to */
1430bd4c625cSLinus Torvalds 		reiserfs_claim_blocks_to_be_allocated(inode->i_sb,
1431bd4c625cSLinus Torvalds 						      num_pages <<
1432bd4c625cSLinus Torvalds 						      (PAGE_CACHE_SHIFT -
1433bd4c625cSLinus Torvalds 						       inode->i_blkbits));
14341da177e4SLinus Torvalds 		reiserfs_write_unlock(inode->i_sb);
14351da177e4SLinus Torvalds 
1436127144dfSJan Kara 		if (!num_pages) {	/* If we do not have enough space even for a single page... */
1437bd4c625cSLinus Torvalds 			if (pos >
1438bd4c625cSLinus Torvalds 			    inode->i_size + inode->i_sb->s_blocksize -
1439bd4c625cSLinus Torvalds 			    (pos & (inode->i_sb->s_blocksize - 1))) {
1440127144dfSJan Kara 				res = -ENOSPC;
1441127144dfSJan Kara 				break;	// In case we are writing past the end of the last file block, break.
1442127144dfSJan Kara 			}
14431da177e4SLinus Torvalds 			// Otherwise we are possibly overwriting the file, so
14441da177e4SLinus Torvalds 			// let's set write size to be equal or less than blocksize.
14451da177e4SLinus Torvalds 			// This way we get it correctly for file holes.
14461da177e4SLinus Torvalds 			// But overwriting files on absolutelly full volumes would not
14471da177e4SLinus Torvalds 			// be very efficient. Well, people are not supposed to fill
14481da177e4SLinus Torvalds 			// 100% of disk space anyway.
1449bd4c625cSLinus Torvalds 			write_bytes =
1450bd4c625cSLinus Torvalds 			    min_t(size_t, count,
1451bd4c625cSLinus Torvalds 				  inode->i_sb->s_blocksize -
1452bd4c625cSLinus Torvalds 				  (pos & (inode->i_sb->s_blocksize - 1)));
14531da177e4SLinus Torvalds 			num_pages = 1;
14541da177e4SLinus Torvalds 			// No blocks were claimed before, so do it now.
1455bd4c625cSLinus Torvalds 			reiserfs_claim_blocks_to_be_allocated(inode->i_sb,
1456bd4c625cSLinus Torvalds 							      1 <<
1457bd4c625cSLinus Torvalds 							      (PAGE_CACHE_SHIFT
1458bd4c625cSLinus Torvalds 							       -
1459bd4c625cSLinus Torvalds 							       inode->
1460bd4c625cSLinus Torvalds 							       i_blkbits));
14611da177e4SLinus Torvalds 		}
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 		/* Prepare for writing into the region, read in all the
14641da177e4SLinus Torvalds 		   partially overwritten pages, if needed. And lock the pages,
14651da177e4SLinus Torvalds 		   so that nobody else can access these until we are done.
14661da177e4SLinus Torvalds 		   We get number of actual blocks needed as a result. */
1467c499ec24SVladimir V. Saveliev 		res = reiserfs_prepare_file_region_for_write(inode, pos,
1468bd4c625cSLinus Torvalds 							     num_pages,
1469bd4c625cSLinus Torvalds 							     write_bytes,
1470bd4c625cSLinus Torvalds 							     prepared_pages);
1471c499ec24SVladimir V. Saveliev 		if (res < 0) {
1472bd4c625cSLinus Torvalds 			reiserfs_release_claimed_blocks(inode->i_sb,
1473bd4c625cSLinus Torvalds 							num_pages <<
1474bd4c625cSLinus Torvalds 							(PAGE_CACHE_SHIFT -
1475bd4c625cSLinus Torvalds 							 inode->i_blkbits));
14761da177e4SLinus Torvalds 			break;
14771da177e4SLinus Torvalds 		}
14781da177e4SLinus Torvalds 
1479c499ec24SVladimir V. Saveliev 		blocks_to_allocate = res;
1480c499ec24SVladimir V. Saveliev 
14811da177e4SLinus Torvalds 		/* First we correct our estimate of how many blocks we need */
1482bd4c625cSLinus Torvalds 		reiserfs_release_claimed_blocks(inode->i_sb,
1483bd4c625cSLinus Torvalds 						(num_pages <<
1484bd4c625cSLinus Torvalds 						 (PAGE_CACHE_SHIFT -
1485bd4c625cSLinus Torvalds 						  inode->i_sb->
1486bd4c625cSLinus Torvalds 						  s_blocksize_bits)) -
1487bd4c625cSLinus Torvalds 						blocks_to_allocate);
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 		if (blocks_to_allocate > 0) {	/*We only allocate blocks if we need to */
14901da177e4SLinus Torvalds 			/* Fill in all the possible holes and append the file if needed */
1491bd4c625cSLinus Torvalds 			res =
1492bd4c625cSLinus Torvalds 			    reiserfs_allocate_blocks_for_region(&th, inode, pos,
1493bd4c625cSLinus Torvalds 								num_pages,
1494bd4c625cSLinus Torvalds 								write_bytes,
1495bd4c625cSLinus Torvalds 								prepared_pages,
1496bd4c625cSLinus Torvalds 								blocks_to_allocate);
14971da177e4SLinus Torvalds 		}
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 		/* well, we have allocated the blocks, so it is time to free
15001da177e4SLinus Torvalds 		   the reservation we made earlier. */
1501bd4c625cSLinus Torvalds 		reiserfs_release_claimed_blocks(inode->i_sb,
1502bd4c625cSLinus Torvalds 						blocks_to_allocate);
15031da177e4SLinus Torvalds 		if (res) {
15041da177e4SLinus Torvalds 			reiserfs_unprepare_pages(prepared_pages, num_pages);
15051da177e4SLinus Torvalds 			break;
15061da177e4SLinus Torvalds 		}
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds /* NOTE that allocating blocks and filling blocks can be done in reverse order
15091da177e4SLinus Torvalds    and probably we would do that just to get rid of garbage in files after a
15101da177e4SLinus Torvalds    crash */
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 		/* Copy data from user-supplied buffer to file's pages */
1513bd4c625cSLinus Torvalds 		res =
1514bd4c625cSLinus Torvalds 		    reiserfs_copy_from_user_to_file_region(pos, num_pages,
1515bd4c625cSLinus Torvalds 							   write_bytes,
1516bd4c625cSLinus Torvalds 							   prepared_pages, buf);
15171da177e4SLinus Torvalds 		if (res) {
15181da177e4SLinus Torvalds 			reiserfs_unprepare_pages(prepared_pages, num_pages);
15191da177e4SLinus Torvalds 			break;
15201da177e4SLinus Torvalds 		}
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 		/* Send the pages to disk and unlock them. */
1523bd4c625cSLinus Torvalds 		res =
1524bd4c625cSLinus Torvalds 		    reiserfs_submit_file_region_for_write(&th, inode, pos,
1525bd4c625cSLinus Torvalds 							  num_pages,
1526bd4c625cSLinus Torvalds 							  write_bytes,
1527bd4c625cSLinus Torvalds 							  prepared_pages);
15281da177e4SLinus Torvalds 		if (res)
15291da177e4SLinus Torvalds 			break;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 		already_written += write_bytes;
15321da177e4SLinus Torvalds 		buf += write_bytes;
15331da177e4SLinus Torvalds 		*ppos = pos += write_bytes;
15341da177e4SLinus Torvalds 		count -= write_bytes;
153559308602SAlexander Zarochentsev 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
15361da177e4SLinus Torvalds 	}
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	/* this is only true on error */
15391da177e4SLinus Torvalds 	if (th.t_trans_id) {
15401da177e4SLinus Torvalds 		reiserfs_write_lock(inode->i_sb);
15411da177e4SLinus Torvalds 		err = journal_end(&th, th.t_super, th.t_blocks_allocated);
15421da177e4SLinus Torvalds 		reiserfs_write_unlock(inode->i_sb);
15431da177e4SLinus Torvalds 		if (err) {
15441da177e4SLinus Torvalds 			res = err;
15451da177e4SLinus Torvalds 			goto out;
15461da177e4SLinus Torvalds 		}
15471da177e4SLinus Torvalds 	}
15481da177e4SLinus Torvalds 
1549619d5d8aSJeff Mahoney 	if (likely(res >= 0) &&
1550619d5d8aSJeff Mahoney 	    (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))))
1551619d5d8aSJeff Mahoney 		res = generic_osync_inode(inode, file->f_mapping,
1552bd4c625cSLinus Torvalds 		                          OSYNC_METADATA | OSYNC_DATA);
15531da177e4SLinus Torvalds 
15541b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
15551da177e4SLinus Torvalds 	reiserfs_async_progress_wait(inode->i_sb);
15561da177e4SLinus Torvalds 	return (already_written != 0) ? already_written : res;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds       out:
15591b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);	// unlock the file on exit.
15601da177e4SLinus Torvalds 	return res;
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds static ssize_t reiserfs_aio_write(struct kiocb *iocb, const char __user * buf,
15641da177e4SLinus Torvalds 				  size_t count, loff_t pos)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	return generic_file_aio_write(iocb, buf, count, pos);
15671da177e4SLinus Torvalds }
15681da177e4SLinus Torvalds 
1569*4b6f5d20SArjan van de Ven const struct file_operations reiserfs_file_operations = {
15701da177e4SLinus Torvalds 	.read = generic_file_read,
15711da177e4SLinus Torvalds 	.write = reiserfs_file_write,
15721da177e4SLinus Torvalds 	.ioctl = reiserfs_ioctl,
15731da177e4SLinus Torvalds 	.mmap = generic_file_mmap,
15741da177e4SLinus Torvalds 	.release = reiserfs_file_release,
15751da177e4SLinus Torvalds 	.fsync = reiserfs_sync_file,
15761da177e4SLinus Torvalds 	.sendfile = generic_file_sendfile,
15771da177e4SLinus Torvalds 	.aio_read = generic_file_aio_read,
15781da177e4SLinus Torvalds 	.aio_write = reiserfs_aio_write,
15791da177e4SLinus Torvalds };
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds struct inode_operations reiserfs_file_inode_operations = {
15821da177e4SLinus Torvalds 	.truncate = reiserfs_vfs_truncate_file,
15831da177e4SLinus Torvalds 	.setattr = reiserfs_setattr,
15841da177e4SLinus Torvalds 	.setxattr = reiserfs_setxattr,
15851da177e4SLinus Torvalds 	.getxattr = reiserfs_getxattr,
15861da177e4SLinus Torvalds 	.listxattr = reiserfs_listxattr,
15871da177e4SLinus Torvalds 	.removexattr = reiserfs_removexattr,
15881da177e4SLinus Torvalds 	.permission = reiserfs_permission,
15891da177e4SLinus Torvalds };
1590