]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/reiserfs/file.c
cpuimx27: fix compile when ULPI is selected
[net-next-2.6.git] / fs / reiserfs / file.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
1da177e4
LT
5#include <linux/time.h>
6#include <linux/reiserfs_fs.h>
7#include <linux/reiserfs_acl.h>
8#include <linux/reiserfs_xattr.h>
1da177e4
LT
9#include <asm/uaccess.h>
10#include <linux/pagemap.h>
11#include <linux/swap.h>
12#include <linux/writeback.h>
13#include <linux/blkdev.h>
14#include <linux/buffer_head.h>
15#include <linux/quotaops.h>
16
17/*
18** We pack the tails of files on file close, not at the time they are written.
19** This implies an unnecessary copy of the tail and an unnecessary indirect item
20** insertion/balancing, for files that are written in one write.
21** It avoids unnecessary tail packings (balances) for files that are written in
22** multiple writes and are small enough to have tails.
0222e657 23**
1da177e4
LT
24** file_release is called by the VFS layer when the file is closed. If
25** this is the last open file descriptor, and the file
26** small enough to have a tail, and the tail is currently in an
27** unformatted node, the tail is converted back into a direct item.
0222e657 28**
1da177e4 29** We use reiserfs_truncate_file to pack the tail, since it already has
0222e657 30** all the conditions coded.
1da177e4 31*/
bd4c625c 32static int reiserfs_file_release(struct inode *inode, struct file *filp)
1da177e4
LT
33{
34
bd4c625c
LT
35 struct reiserfs_transaction_handle th;
36 int err;
37 int jbegin_failure = 0;
1da177e4 38
14a61442 39 BUG_ON(!S_ISREG(inode->i_mode));
1da177e4 40
0e4f6a79
AV
41 if (atomic_add_unless(&REISERFS_I(inode)->openers, -1, 1))
42 return 0;
43
44 mutex_lock(&(REISERFS_I(inode)->tailpack));
45
46 if (!atomic_dec_and_test(&REISERFS_I(inode)->openers)) {
47 mutex_unlock(&(REISERFS_I(inode)->tailpack));
48 return 0;
49 }
50
bd4c625c 51 /* fast out for when nothing needs to be done */
0e4f6a79 52 if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
bd4c625c
LT
53 !tail_has_to_be_packed(inode)) &&
54 REISERFS_I(inode)->i_prealloc_count <= 0) {
0e4f6a79 55 mutex_unlock(&(REISERFS_I(inode)->tailpack));
bd4c625c
LT
56 return 0;
57 }
1da177e4 58
b5f3953c 59 reiserfs_write_lock(inode->i_sb);
bd4c625c
LT
60 /* freeing preallocation only involves relogging blocks that
61 * are already in the current transaction. preallocation gets
62 * freed at the end of each transaction, so it is impossible for
63 * us to log any additional blocks (including quota blocks)
64 */
65 err = journal_begin(&th, inode->i_sb, 1);
1da177e4 66 if (err) {
bd4c625c
LT
67 /* uh oh, we can't allow the inode to go away while there
68 * is still preallocation blocks pending. Try to join the
69 * aborted transaction
70 */
71 jbegin_failure = err;
72 err = journal_join_abort(&th, inode->i_sb, 1);
73
74 if (err) {
75 /* hmpf, our choices here aren't good. We can pin the inode
76 * which will disallow unmount from every happening, we can
77 * do nothing, which will corrupt random memory on unmount,
78 * or we can forcibly remove the file from the preallocation
79 * list, which will leak blocks on disk. Lets pin the inode
80 * and let the admin know what is going on.
81 */
82 igrab(inode);
45b03d5e 83 reiserfs_warning(inode->i_sb, "clm-9001",
bd4c625c 84 "pinning inode %lu because the "
533221fb
AD
85 "preallocation can't be freed",
86 inode->i_ino);
bd4c625c
LT
87 goto out;
88 }
1da177e4 89 }
bd4c625c 90 reiserfs_update_inode_transaction(inode);
1da177e4
LT
91
92#ifdef REISERFS_PREALLOCATE
bd4c625c 93 reiserfs_discard_prealloc(&th, inode);
1da177e4 94#endif
bd4c625c
LT
95 err = journal_end(&th, inode->i_sb, 1);
96
97 /* copy back the error code from journal_begin */
98 if (!err)
99 err = jbegin_failure;
100
0e4f6a79 101 if (!err &&
bd4c625c
LT
102 (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
103 tail_has_to_be_packed(inode)) {
0e4f6a79 104
bd4c625c
LT
105 /* if regular file is released by last holder and it has been
106 appended (we append by unformatted node only) or its direct
107 item(s) had to be converted, then it may have to be
108 indirect2direct converted */
109 err = reiserfs_truncate_file(inode, 0);
110 }
111 out:
bd4c625c 112 reiserfs_write_unlock(inode->i_sb);
0e4f6a79 113 mutex_unlock(&(REISERFS_I(inode)->tailpack));
bd4c625c 114 return err;
1da177e4
LT
115}
116
0e4f6a79 117static int reiserfs_file_open(struct inode *inode, struct file *file)
de14569f 118{
0e4f6a79
AV
119 int err = dquot_file_open(inode, file);
120 if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
121 /* somebody might be tailpacking on final close; wait for it */
122 mutex_lock(&(REISERFS_I(inode)->tailpack));
123 atomic_inc(&REISERFS_I(inode)->openers);
124 mutex_unlock(&(REISERFS_I(inode)->tailpack));
125 }
126 return err;
de14569f
VS
127}
128
bd4c625c
LT
129static void reiserfs_vfs_truncate_file(struct inode *inode)
130{
0e4f6a79 131 mutex_lock(&(REISERFS_I(inode)->tailpack));
bd4c625c 132 reiserfs_truncate_file(inode, 1);
0e4f6a79 133 mutex_unlock(&(REISERFS_I(inode)->tailpack));
1da177e4
LT
134}
135
136/* Sync a reiserfs file. */
137
138/*
139 * FIXME: sync_mapping_buffers() never has anything to sync. Can
140 * be removed...
141 */
142
7ea80859 143static int reiserfs_sync_file(struct file *filp, int datasync)
bd4c625c 144{
7ea80859 145 struct inode *inode = filp->f_mapping->host;
ee93961b 146 int err;
bd4c625c
LT
147 int barrier_done;
148
995c762e 149 BUG_ON(!S_ISREG(inode->i_mode));
ee93961b 150 err = sync_mapping_buffers(inode->i_mapping);
995c762e
JM
151 reiserfs_write_lock(inode->i_sb);
152 barrier_done = reiserfs_commit_for_inode(inode);
153 reiserfs_write_unlock(inode->i_sb);
154 if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
fbd9b09a
DM
155 blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL,
156 BLKDEV_IFL_WAIT);
bd4c625c
LT
157 if (barrier_done < 0)
158 return barrier_done;
ee93961b 159 return (err < 0) ? -EIO : 0;
1da177e4
LT
160}
161
1da177e4
LT
162/* taken fs/buffer.c:__block_commit_write */
163int reiserfs_commit_page(struct inode *inode, struct page *page,
bd4c625c 164 unsigned from, unsigned to)
1da177e4 165{
bd4c625c
LT
166 unsigned block_start, block_end;
167 int partial = 0;
168 unsigned blocksize;
169 struct buffer_head *bh, *head;
170 unsigned long i_size_index = inode->i_size >> PAGE_CACHE_SHIFT;
171 int new;
172 int logit = reiserfs_file_data_log(inode);
173 struct super_block *s = inode->i_sb;
174 int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize;
175 struct reiserfs_transaction_handle th;
176 int ret = 0;
177
178 th.t_trans_id = 0;
179 blocksize = 1 << inode->i_blkbits;
180
181 if (logit) {
182 reiserfs_write_lock(s);
183 ret = journal_begin(&th, s, bh_per_page + 1);
184 if (ret)
185 goto drop_write_lock;
186 reiserfs_update_inode_transaction(inode);
187 }
188 for (bh = head = page_buffers(page), block_start = 0;
189 bh != head || !block_start;
190 block_start = block_end, bh = bh->b_this_page) {
191
192 new = buffer_new(bh);
193 clear_buffer_new(bh);
194 block_end = block_start + blocksize;
195 if (block_end <= from || block_start >= to) {
196 if (!buffer_uptodate(bh))
197 partial = 1;
198 } else {
199 set_buffer_uptodate(bh);
200 if (logit) {
201 reiserfs_prepare_for_journal(s, bh, 1);
202 journal_mark_dirty(&th, s, bh);
203 } else if (!buffer_dirty(bh)) {
204 mark_buffer_dirty(bh);
205 /* do data=ordered on any page past the end
206 * of file and any buffer marked BH_New.
207 */
208 if (reiserfs_data_ordered(inode->i_sb) &&
209 (new || page->index >= i_size_index)) {
210 reiserfs_add_ordered_list(inode, bh);
211 }
212 }
213 }
1da177e4 214 }
bd4c625c
LT
215 if (logit) {
216 ret = journal_end(&th, s, bh_per_page + 1);
217 drop_write_lock:
218 reiserfs_write_unlock(s);
219 }
220 /*
221 * If this is a partial write which happened to make all buffers
222 * uptodate then we can optimize away a bogus readpage() for
223 * the next read(). Here we 'discover' whether the page went
224 * uptodate as a result of this (potentially partial) write.
225 */
226 if (!partial)
227 SetPageUptodate(page);
228 return ret;
1da177e4
LT
229}
230
1da177e4 231/* Write @count bytes at position @ppos in a file indicated by @file
0222e657 232 from the buffer @buf.
1da177e4
LT
233
234 generic_file_write() is only appropriate for filesystems that are not seeking to optimize performance and want
235 something simple that works. It is not for serious use by general purpose filesystems, excepting the one that it was
236 written for (ext2/3). This is for several reasons:
237
238 * It has no understanding of any filesystem specific optimizations.
239
240 * It enters the filesystem repeatedly for each page that is written.
241
242 * It depends on reiserfs_get_block() function which if implemented by reiserfs performs costly search_by_key
243 * operation for each page it is supplied with. By contrast reiserfs_file_write() feeds as much as possible at a time
244 * to reiserfs which allows for fewer tree traversals.
245
246 * Each indirect pointer insertion takes a lot of cpu, because it involves memory moves inside of blocks.
247
248 * Asking the block allocation code for blocks one at a time is slightly less efficient.
249
250 All of these reasons for not using only generic file write were understood back when reiserfs was first miscoded to
251 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
252 things right finally.
253
254 Future Features: providing search_by_key with hints.
255
256*/
bd4c625c
LT
257static ssize_t reiserfs_file_write(struct file *file, /* the file we are going to write into */
258 const char __user * buf, /* pointer to user supplied data
259 (in userspace) */
260 size_t count, /* amount of bytes to write */
261 loff_t * ppos /* pointer to position in file that we start writing at. Should be updated to
262 * new current position before returning. */
263 )
1da177e4 264{
1fc5adbd 265 struct inode *inode = file->f_path.dentry->d_inode; // Inode of the file that we are writing to.
bd4c625c
LT
266 /* To simplify coding at this time, we store
267 locked pages in array for now */
bd4c625c
LT
268 struct reiserfs_transaction_handle th;
269 th.t_trans_id = 0;
270
fa385bef
JM
271 /* If a filesystem is converted from 3.5 to 3.6, we'll have v3.5 items
272 * lying around (most of the disk, in fact). Despite the filesystem
273 * now being a v3.6 format, the old items still can't support large
274 * file sizes. Catch this case here, as the rest of the VFS layer is
275 * oblivious to the different limitations between old and new items.
276 * reiserfs_setattr catches this for truncates. This chunk is lifted
277 * from generic_write_checks. */
278 if (get_inode_item_key_version (inode) == KEY_FORMAT_3_5 &&
279 *ppos + count > MAX_NON_LFS) {
280 if (*ppos >= MAX_NON_LFS) {
fa385bef
JM
281 return -EFBIG;
282 }
283 if (count > MAX_NON_LFS - (unsigned long)*ppos)
284 count = MAX_NON_LFS - (unsigned long)*ppos;
285 }
286
797b4cff 287 return do_sync_write(file, buf, count, ppos);
1da177e4
LT
288}
289
4b6f5d20 290const struct file_operations reiserfs_file_operations = {
027445c3 291 .read = do_sync_read,
bd4c625c 292 .write = reiserfs_file_write,
205cb37b 293 .unlocked_ioctl = reiserfs_ioctl,
52b499c4
DH
294#ifdef CONFIG_COMPAT
295 .compat_ioctl = reiserfs_compat_ioctl,
296#endif
0e4f6a79
AV
297 .mmap = generic_file_mmap,
298 .open = reiserfs_file_open,
bd4c625c
LT
299 .release = reiserfs_file_release,
300 .fsync = reiserfs_sync_file,
bd4c625c 301 .aio_read = generic_file_aio_read,
9637f28f 302 .aio_write = generic_file_aio_write,
5274f052
JA
303 .splice_read = generic_file_splice_read,
304 .splice_write = generic_file_splice_write,
91efc167 305 .llseek = generic_file_llseek,
1da177e4
LT
306};
307
c5ef1c42 308const struct inode_operations reiserfs_file_inode_operations = {
bd4c625c
LT
309 .truncate = reiserfs_vfs_truncate_file,
310 .setattr = reiserfs_setattr,
311 .setxattr = reiserfs_setxattr,
312 .getxattr = reiserfs_getxattr,
313 .listxattr = reiserfs_listxattr,
314 .removexattr = reiserfs_removexattr,
315 .permission = reiserfs_permission,
1da177e4 316};