xref: /OK3568_Linux_fs/kernel/fs/reiserfs/file.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3*4882a593Smuzhiyun  */
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/time.h>
6*4882a593Smuzhiyun #include "reiserfs.h"
7*4882a593Smuzhiyun #include "acl.h"
8*4882a593Smuzhiyun #include "xattr.h"
9*4882a593Smuzhiyun #include <linux/uaccess.h>
10*4882a593Smuzhiyun #include <linux/pagemap.h>
11*4882a593Smuzhiyun #include <linux/swap.h>
12*4882a593Smuzhiyun #include <linux/writeback.h>
13*4882a593Smuzhiyun #include <linux/blkdev.h>
14*4882a593Smuzhiyun #include <linux/buffer_head.h>
15*4882a593Smuzhiyun #include <linux/quotaops.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun  * We pack the tails of files on file close, not at the time they are written.
19*4882a593Smuzhiyun  * This implies an unnecessary copy of the tail and an unnecessary indirect item
20*4882a593Smuzhiyun  * insertion/balancing, for files that are written in one write.
21*4882a593Smuzhiyun  * It avoids unnecessary tail packings (balances) for files that are written in
22*4882a593Smuzhiyun  * multiple writes and are small enough to have tails.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * file_release is called by the VFS layer when the file is closed.  If
25*4882a593Smuzhiyun  * this is the last open file descriptor, and the file
26*4882a593Smuzhiyun  * small enough to have a tail, and the tail is currently in an
27*4882a593Smuzhiyun  * unformatted node, the tail is converted back into a direct item.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * We use reiserfs_truncate_file to pack the tail, since it already has
30*4882a593Smuzhiyun  * all the conditions coded.
31*4882a593Smuzhiyun  */
reiserfs_file_release(struct inode * inode,struct file * filp)32*4882a593Smuzhiyun static int reiserfs_file_release(struct inode *inode, struct file *filp)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	struct reiserfs_transaction_handle th;
36*4882a593Smuzhiyun 	int err;
37*4882a593Smuzhiyun 	int jbegin_failure = 0;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	BUG_ON(!S_ISREG(inode->i_mode));
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	if (!atomic_dec_and_mutex_lock(&REISERFS_I(inode)->openers,
42*4882a593Smuzhiyun 				       &REISERFS_I(inode)->tailpack))
43*4882a593Smuzhiyun 		return 0;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	/* fast out for when nothing needs to be done */
46*4882a593Smuzhiyun 	if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
47*4882a593Smuzhiyun 	     !tail_has_to_be_packed(inode)) &&
48*4882a593Smuzhiyun 	    REISERFS_I(inode)->i_prealloc_count <= 0) {
49*4882a593Smuzhiyun 		mutex_unlock(&REISERFS_I(inode)->tailpack);
50*4882a593Smuzhiyun 		return 0;
51*4882a593Smuzhiyun 	}
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	reiserfs_write_lock(inode->i_sb);
54*4882a593Smuzhiyun 	/*
55*4882a593Smuzhiyun 	 * freeing preallocation only involves relogging blocks that
56*4882a593Smuzhiyun 	 * are already in the current transaction.  preallocation gets
57*4882a593Smuzhiyun 	 * freed at the end of each transaction, so it is impossible for
58*4882a593Smuzhiyun 	 * us to log any additional blocks (including quota blocks)
59*4882a593Smuzhiyun 	 */
60*4882a593Smuzhiyun 	err = journal_begin(&th, inode->i_sb, 1);
61*4882a593Smuzhiyun 	if (err) {
62*4882a593Smuzhiyun 		/*
63*4882a593Smuzhiyun 		 * uh oh, we can't allow the inode to go away while there
64*4882a593Smuzhiyun 		 * is still preallocation blocks pending.  Try to join the
65*4882a593Smuzhiyun 		 * aborted transaction
66*4882a593Smuzhiyun 		 */
67*4882a593Smuzhiyun 		jbegin_failure = err;
68*4882a593Smuzhiyun 		err = journal_join_abort(&th, inode->i_sb);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 		if (err) {
71*4882a593Smuzhiyun 			/*
72*4882a593Smuzhiyun 			 * hmpf, our choices here aren't good.  We can pin
73*4882a593Smuzhiyun 			 * the inode which will disallow unmount from ever
74*4882a593Smuzhiyun 			 * happening, we can do nothing, which will corrupt
75*4882a593Smuzhiyun 			 * random memory on unmount, or we can forcibly
76*4882a593Smuzhiyun 			 * remove the file from the preallocation list, which
77*4882a593Smuzhiyun 			 * will leak blocks on disk.  Lets pin the inode
78*4882a593Smuzhiyun 			 * and let the admin know what is going on.
79*4882a593Smuzhiyun 			 */
80*4882a593Smuzhiyun 			igrab(inode);
81*4882a593Smuzhiyun 			reiserfs_warning(inode->i_sb, "clm-9001",
82*4882a593Smuzhiyun 					 "pinning inode %lu because the "
83*4882a593Smuzhiyun 					 "preallocation can't be freed",
84*4882a593Smuzhiyun 					 inode->i_ino);
85*4882a593Smuzhiyun 			goto out;
86*4882a593Smuzhiyun 		}
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 	reiserfs_update_inode_transaction(inode);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #ifdef REISERFS_PREALLOCATE
91*4882a593Smuzhiyun 	reiserfs_discard_prealloc(&th, inode);
92*4882a593Smuzhiyun #endif
93*4882a593Smuzhiyun 	err = journal_end(&th);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* copy back the error code from journal_begin */
96*4882a593Smuzhiyun 	if (!err)
97*4882a593Smuzhiyun 		err = jbegin_failure;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	if (!err &&
100*4882a593Smuzhiyun 	    (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
101*4882a593Smuzhiyun 	    tail_has_to_be_packed(inode)) {
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		/*
104*4882a593Smuzhiyun 		 * if regular file is released by last holder and it has been
105*4882a593Smuzhiyun 		 * appended (we append by unformatted node only) or its direct
106*4882a593Smuzhiyun 		 * item(s) had to be converted, then it may have to be
107*4882a593Smuzhiyun 		 * indirect2direct converted
108*4882a593Smuzhiyun 		 */
109*4882a593Smuzhiyun 		err = reiserfs_truncate_file(inode, 0);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun out:
112*4882a593Smuzhiyun 	reiserfs_write_unlock(inode->i_sb);
113*4882a593Smuzhiyun 	mutex_unlock(&REISERFS_I(inode)->tailpack);
114*4882a593Smuzhiyun 	return err;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
reiserfs_file_open(struct inode * inode,struct file * file)117*4882a593Smuzhiyun static int reiserfs_file_open(struct inode *inode, struct file *file)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	int err = dquot_file_open(inode, file);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* somebody might be tailpacking on final close; wait for it */
122*4882a593Smuzhiyun         if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
123*4882a593Smuzhiyun 		mutex_lock(&REISERFS_I(inode)->tailpack);
124*4882a593Smuzhiyun 		atomic_inc(&REISERFS_I(inode)->openers);
125*4882a593Smuzhiyun 		mutex_unlock(&REISERFS_I(inode)->tailpack);
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 	return err;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
reiserfs_vfs_truncate_file(struct inode * inode)130*4882a593Smuzhiyun void reiserfs_vfs_truncate_file(struct inode *inode)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	mutex_lock(&REISERFS_I(inode)->tailpack);
133*4882a593Smuzhiyun 	reiserfs_truncate_file(inode, 1);
134*4882a593Smuzhiyun 	mutex_unlock(&REISERFS_I(inode)->tailpack);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /* Sync a reiserfs file. */
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun  * FIXME: sync_mapping_buffers() never has anything to sync.  Can
141*4882a593Smuzhiyun  * be removed...
142*4882a593Smuzhiyun  */
143*4882a593Smuzhiyun 
reiserfs_sync_file(struct file * filp,loff_t start,loff_t end,int datasync)144*4882a593Smuzhiyun static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,
145*4882a593Smuzhiyun 			      int datasync)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct inode *inode = filp->f_mapping->host;
148*4882a593Smuzhiyun 	int err;
149*4882a593Smuzhiyun 	int barrier_done;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	err = file_write_and_wait_range(filp, start, end);
152*4882a593Smuzhiyun 	if (err)
153*4882a593Smuzhiyun 		return err;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	inode_lock(inode);
156*4882a593Smuzhiyun 	BUG_ON(!S_ISREG(inode->i_mode));
157*4882a593Smuzhiyun 	err = sync_mapping_buffers(inode->i_mapping);
158*4882a593Smuzhiyun 	reiserfs_write_lock(inode->i_sb);
159*4882a593Smuzhiyun 	barrier_done = reiserfs_commit_for_inode(inode);
160*4882a593Smuzhiyun 	reiserfs_write_unlock(inode->i_sb);
161*4882a593Smuzhiyun 	if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
162*4882a593Smuzhiyun 		blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
163*4882a593Smuzhiyun 	inode_unlock(inode);
164*4882a593Smuzhiyun 	if (barrier_done < 0)
165*4882a593Smuzhiyun 		return barrier_done;
166*4882a593Smuzhiyun 	return (err < 0) ? -EIO : 0;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /* taken fs/buffer.c:__block_commit_write */
reiserfs_commit_page(struct inode * inode,struct page * page,unsigned from,unsigned to)170*4882a593Smuzhiyun int reiserfs_commit_page(struct inode *inode, struct page *page,
171*4882a593Smuzhiyun 			 unsigned from, unsigned to)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	unsigned block_start, block_end;
174*4882a593Smuzhiyun 	int partial = 0;
175*4882a593Smuzhiyun 	unsigned blocksize;
176*4882a593Smuzhiyun 	struct buffer_head *bh, *head;
177*4882a593Smuzhiyun 	unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;
178*4882a593Smuzhiyun 	int new;
179*4882a593Smuzhiyun 	int logit = reiserfs_file_data_log(inode);
180*4882a593Smuzhiyun 	struct super_block *s = inode->i_sb;
181*4882a593Smuzhiyun 	int bh_per_page = PAGE_SIZE / s->s_blocksize;
182*4882a593Smuzhiyun 	struct reiserfs_transaction_handle th;
183*4882a593Smuzhiyun 	int ret = 0;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	th.t_trans_id = 0;
186*4882a593Smuzhiyun 	blocksize = i_blocksize(inode);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (logit) {
189*4882a593Smuzhiyun 		reiserfs_write_lock(s);
190*4882a593Smuzhiyun 		ret = journal_begin(&th, s, bh_per_page + 1);
191*4882a593Smuzhiyun 		if (ret)
192*4882a593Smuzhiyun 			goto drop_write_lock;
193*4882a593Smuzhiyun 		reiserfs_update_inode_transaction(inode);
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 	for (bh = head = page_buffers(page), block_start = 0;
196*4882a593Smuzhiyun 	     bh != head || !block_start;
197*4882a593Smuzhiyun 	     block_start = block_end, bh = bh->b_this_page) {
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 		new = buffer_new(bh);
200*4882a593Smuzhiyun 		clear_buffer_new(bh);
201*4882a593Smuzhiyun 		block_end = block_start + blocksize;
202*4882a593Smuzhiyun 		if (block_end <= from || block_start >= to) {
203*4882a593Smuzhiyun 			if (!buffer_uptodate(bh))
204*4882a593Smuzhiyun 				partial = 1;
205*4882a593Smuzhiyun 		} else {
206*4882a593Smuzhiyun 			set_buffer_uptodate(bh);
207*4882a593Smuzhiyun 			if (logit) {
208*4882a593Smuzhiyun 				reiserfs_prepare_for_journal(s, bh, 1);
209*4882a593Smuzhiyun 				journal_mark_dirty(&th, bh);
210*4882a593Smuzhiyun 			} else if (!buffer_dirty(bh)) {
211*4882a593Smuzhiyun 				mark_buffer_dirty(bh);
212*4882a593Smuzhiyun 				/*
213*4882a593Smuzhiyun 				 * do data=ordered on any page past the end
214*4882a593Smuzhiyun 				 * of file and any buffer marked BH_New.
215*4882a593Smuzhiyun 				 */
216*4882a593Smuzhiyun 				if (reiserfs_data_ordered(inode->i_sb) &&
217*4882a593Smuzhiyun 				    (new || page->index >= i_size_index)) {
218*4882a593Smuzhiyun 					reiserfs_add_ordered_list(inode, bh);
219*4882a593Smuzhiyun 				}
220*4882a593Smuzhiyun 			}
221*4882a593Smuzhiyun 		}
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 	if (logit) {
224*4882a593Smuzhiyun 		ret = journal_end(&th);
225*4882a593Smuzhiyun drop_write_lock:
226*4882a593Smuzhiyun 		reiserfs_write_unlock(s);
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 	/*
229*4882a593Smuzhiyun 	 * If this is a partial write which happened to make all buffers
230*4882a593Smuzhiyun 	 * uptodate then we can optimize away a bogus readpage() for
231*4882a593Smuzhiyun 	 * the next read(). Here we 'discover' whether the page went
232*4882a593Smuzhiyun 	 * uptodate as a result of this (potentially partial) write.
233*4882a593Smuzhiyun 	 */
234*4882a593Smuzhiyun 	if (!partial)
235*4882a593Smuzhiyun 		SetPageUptodate(page);
236*4882a593Smuzhiyun 	return ret;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun const struct file_operations reiserfs_file_operations = {
240*4882a593Smuzhiyun 	.unlocked_ioctl = reiserfs_ioctl,
241*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
242*4882a593Smuzhiyun 	.compat_ioctl = reiserfs_compat_ioctl,
243*4882a593Smuzhiyun #endif
244*4882a593Smuzhiyun 	.mmap = generic_file_mmap,
245*4882a593Smuzhiyun 	.open = reiserfs_file_open,
246*4882a593Smuzhiyun 	.release = reiserfs_file_release,
247*4882a593Smuzhiyun 	.fsync = reiserfs_sync_file,
248*4882a593Smuzhiyun 	.read_iter = generic_file_read_iter,
249*4882a593Smuzhiyun 	.write_iter = generic_file_write_iter,
250*4882a593Smuzhiyun 	.splice_read = generic_file_splice_read,
251*4882a593Smuzhiyun 	.splice_write = iter_file_splice_write,
252*4882a593Smuzhiyun 	.llseek = generic_file_llseek,
253*4882a593Smuzhiyun };
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun const struct inode_operations reiserfs_file_inode_operations = {
256*4882a593Smuzhiyun 	.setattr = reiserfs_setattr,
257*4882a593Smuzhiyun 	.listxattr = reiserfs_listxattr,
258*4882a593Smuzhiyun 	.permission = reiserfs_permission,
259*4882a593Smuzhiyun 	.get_acl = reiserfs_get_acl,
260*4882a593Smuzhiyun 	.set_acl = reiserfs_set_acl,
261*4882a593Smuzhiyun };
262