blob: 9977df9f3a541b34d27b968da7d6ce918d062a33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/time.h>
6#include <linux/reiserfs_fs.h>
7#include <linux/reiserfs_acl.h>
8#include <linux/reiserfs_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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.
Jeff Mahoney0222e652009-03-30 14:02:44 -040023**
Linus Torvalds1da177e2005-04-16 15:20:36 -070024** 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.
Jeff Mahoney0222e652009-03-30 14:02:44 -040028**
Linus Torvalds1da177e2005-04-16 15:20:36 -070029** We use reiserfs_truncate_file to pack the tail, since it already has
Jeff Mahoney0222e652009-03-30 14:02:44 -040030** all the conditions coded.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070032static int reiserfs_file_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070035 struct reiserfs_transaction_handle th;
36 int err;
37 int jbegin_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Eric Sesterhenn14a61442006-10-03 23:36:38 +020039 BUG_ON(!S_ISREG(inode->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070041 /* fast out for when nothing needs to be done */
42 if ((atomic_read(&inode->i_count) > 1 ||
43 !(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
44 !tail_has_to_be_packed(inode)) &&
45 REISERFS_I(inode)->i_prealloc_count <= 0) {
46 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080049 mutex_lock(&inode->i_mutex);
Vladimir Savelievde145692007-01-22 20:40:46 -080050
51 mutex_lock(&(REISERFS_I(inode)->i_mmap));
52 if (REISERFS_I(inode)->i_flags & i_ever_mapped)
53 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
54
Chris Masonb5f39532006-08-05 12:15:08 -070055 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070056 /* freeing preallocation only involves relogging blocks that
57 * are already in the current transaction. preallocation gets
58 * freed at the end of each transaction, so it is impossible for
59 * us to log any additional blocks (including quota blocks)
60 */
61 err = journal_begin(&th, inode->i_sb, 1);
62 if (err) {
63 /* uh oh, we can't allow the inode to go away while there
64 * is still preallocation blocks pending. Try to join the
65 * aborted transaction
66 */
67 jbegin_failure = err;
68 err = journal_join_abort(&th, inode->i_sb, 1);
69
70 if (err) {
71 /* hmpf, our choices here aren't good. We can pin the inode
72 * which will disallow unmount from every happening, we can
73 * do nothing, which will corrupt random memory on unmount,
74 * or we can forcibly remove the file from the preallocation
75 * list, which will leak blocks on disk. Lets pin the inode
76 * and let the admin know what is going on.
77 */
78 igrab(inode);
Jeff Mahoney45b03d52009-03-30 14:02:21 -040079 reiserfs_warning(inode->i_sb, "clm-9001",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070080 "pinning inode %lu because the "
Alexey Dobriyan533221f2006-11-25 11:09:30 -080081 "preallocation can't be freed",
82 inode->i_ino);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 goto out;
84 }
85 }
86 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#ifdef REISERFS_PREALLOCATE
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 reiserfs_discard_prealloc(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091 err = journal_end(&th, inode->i_sb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 /* copy back the error code from journal_begin */
94 if (!err)
95 err = jbegin_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 if (!err && atomic_read(&inode->i_count) <= 1 &&
98 (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
99 tail_has_to_be_packed(inode)) {
100 /* if regular file is released by last holder and it has been
101 appended (we append by unformatted node only) or its direct
102 item(s) had to be converted, then it may have to be
103 indirect2direct converted */
104 err = reiserfs_truncate_file(inode, 0);
105 }
106 out:
Vladimir Savelievde145692007-01-22 20:40:46 -0800107 mutex_unlock(&(REISERFS_I(inode)->i_mmap));
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800108 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700109 reiserfs_write_unlock(inode->i_sb);
110 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Vladimir Savelievde145692007-01-22 20:40:46 -0800113static int reiserfs_file_mmap(struct file *file, struct vm_area_struct *vma)
114{
115 struct inode *inode;
116
117 inode = file->f_path.dentry->d_inode;
118 mutex_lock(&(REISERFS_I(inode)->i_mmap));
119 REISERFS_I(inode)->i_flags |= i_ever_mapped;
120 mutex_unlock(&(REISERFS_I(inode)->i_mmap));
121
122 return generic_file_mmap(file, vma);
123}
124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125static void reiserfs_vfs_truncate_file(struct inode *inode)
126{
127 reiserfs_truncate_file(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/* Sync a reiserfs file. */
131
132/*
133 * FIXME: sync_mapping_buffers() never has anything to sync. Can
134 * be removed...
135 */
136
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400137static int reiserfs_sync_file(struct file *filp,
138 struct dentry *dentry, int datasync)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400140 struct inode *inode = dentry->d_inode;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400141 int err;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142 int barrier_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jeff Mahoney995c7622009-03-30 14:02:47 -0400144 BUG_ON(!S_ISREG(inode->i_mode));
Jeff Mahoneyee939612009-03-30 14:02:50 -0400145 err = sync_mapping_buffers(inode->i_mapping);
Jeff Mahoney995c7622009-03-30 14:02:47 -0400146 reiserfs_write_lock(inode->i_sb);
147 barrier_done = reiserfs_commit_for_inode(inode);
148 reiserfs_write_unlock(inode->i_sb);
149 if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400150 blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL,
151 BLKDEV_IFL_WAIT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152 if (barrier_done < 0)
153 return barrier_done;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400154 return (err < 0) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* taken fs/buffer.c:__block_commit_write */
158int reiserfs_commit_page(struct inode *inode, struct page *page,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 unsigned from, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 unsigned block_start, block_end;
162 int partial = 0;
163 unsigned blocksize;
164 struct buffer_head *bh, *head;
165 unsigned long i_size_index = inode->i_size >> PAGE_CACHE_SHIFT;
166 int new;
167 int logit = reiserfs_file_data_log(inode);
168 struct super_block *s = inode->i_sb;
169 int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize;
170 struct reiserfs_transaction_handle th;
171 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 th.t_trans_id = 0;
174 blocksize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 if (logit) {
177 reiserfs_write_lock(s);
178 ret = journal_begin(&th, s, bh_per_page + 1);
179 if (ret)
180 goto drop_write_lock;
181 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183 for (bh = head = page_buffers(page), block_start = 0;
184 bh != head || !block_start;
185 block_start = block_end, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 new = buffer_new(bh);
188 clear_buffer_new(bh);
189 block_end = block_start + blocksize;
190 if (block_end <= from || block_start >= to) {
191 if (!buffer_uptodate(bh))
192 partial = 1;
193 } else {
194 set_buffer_uptodate(bh);
195 if (logit) {
196 reiserfs_prepare_for_journal(s, bh, 1);
197 journal_mark_dirty(&th, s, bh);
198 } else if (!buffer_dirty(bh)) {
199 mark_buffer_dirty(bh);
200 /* do data=ordered on any page past the end
201 * of file and any buffer marked BH_New.
202 */
203 if (reiserfs_data_ordered(inode->i_sb) &&
204 (new || page->index >= i_size_index)) {
205 reiserfs_add_ordered_list(inode, bh);
206 }
207 }
208 }
209 }
210 if (logit) {
211 ret = journal_end(&th, s, bh_per_page + 1);
212 drop_write_lock:
213 reiserfs_write_unlock(s);
214 }
215 /*
216 * If this is a partial write which happened to make all buffers
217 * uptodate then we can optimize away a bogus readpage() for
218 * the next read(). Here we 'discover' whether the page went
219 * uptodate as a result of this (potentially partial) write.
220 */
221 if (!partial)
222 SetPageUptodate(page);
223 return ret;
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Write @count bytes at position @ppos in a file indicated by @file
Jeff Mahoney0222e652009-03-30 14:02:44 -0400227 from the buffer @buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 generic_file_write() is only appropriate for filesystems that are not seeking to optimize performance and want
230 something simple that works. It is not for serious use by general purpose filesystems, excepting the one that it was
231 written for (ext2/3). This is for several reasons:
232
233 * It has no understanding of any filesystem specific optimizations.
234
235 * It enters the filesystem repeatedly for each page that is written.
236
237 * It depends on reiserfs_get_block() function which if implemented by reiserfs performs costly search_by_key
238 * operation for each page it is supplied with. By contrast reiserfs_file_write() feeds as much as possible at a time
239 * to reiserfs which allows for fewer tree traversals.
240
241 * Each indirect pointer insertion takes a lot of cpu, because it involves memory moves inside of blocks.
242
243 * Asking the block allocation code for blocks one at a time is slightly less efficient.
244
245 All of these reasons for not using only generic file write were understood back when reiserfs was first miscoded to
246 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
247 things right finally.
248
249 Future Features: providing search_by_key with hints.
250
251*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252static ssize_t reiserfs_file_write(struct file *file, /* the file we are going to write into */
253 const char __user * buf, /* pointer to user supplied data
254 (in userspace) */
255 size_t count, /* amount of bytes to write */
256 loff_t * ppos /* pointer to position in file that we start writing at. Should be updated to
257 * new current position before returning. */
258 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800260 struct inode *inode = file->f_path.dentry->d_inode; // Inode of the file that we are writing to.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 /* To simplify coding at this time, we store
262 locked pages in array for now */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 struct reiserfs_transaction_handle th;
264 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jeff Mahoneyfa385be2006-02-01 03:06:51 -0800266 /* If a filesystem is converted from 3.5 to 3.6, we'll have v3.5 items
267 * lying around (most of the disk, in fact). Despite the filesystem
268 * now being a v3.6 format, the old items still can't support large
269 * file sizes. Catch this case here, as the rest of the VFS layer is
270 * oblivious to the different limitations between old and new items.
271 * reiserfs_setattr catches this for truncates. This chunk is lifted
272 * from generic_write_checks. */
273 if (get_inode_item_key_version (inode) == KEY_FORMAT_3_5 &&
274 *ppos + count > MAX_NON_LFS) {
275 if (*ppos >= MAX_NON_LFS) {
Jeff Mahoneyfa385be2006-02-01 03:06:51 -0800276 return -EFBIG;
277 }
278 if (count > MAX_NON_LFS - (unsigned long)*ppos)
279 count = MAX_NON_LFS - (unsigned long)*ppos;
280 }
281
Vladimir Saveliev797b4cf2007-10-16 01:25:12 -0700282 return do_sync_write(file, buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800285const struct file_operations reiserfs_file_operations = {
Badari Pulavarty027445c2006-09-30 23:28:46 -0700286 .read = do_sync_read,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 .write = reiserfs_file_write,
Frederic Weisbecker205cb372009-10-14 23:22:17 +0200288 .unlocked_ioctl = reiserfs_ioctl,
David Howells52b499c2006-08-29 19:06:18 +0100289#ifdef CONFIG_COMPAT
290 .compat_ioctl = reiserfs_compat_ioctl,
291#endif
Vladimir Savelievde145692007-01-22 20:40:46 -0800292 .mmap = reiserfs_file_mmap,
Christoph Hellwig907f4552010-03-03 09:05:06 -0500293 .open = dquot_file_open,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 .release = reiserfs_file_release,
295 .fsync = reiserfs_sync_file,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700296 .aio_read = generic_file_aio_read,
Alexey Dobriyan9637f282006-06-26 00:24:57 -0700297 .aio_write = generic_file_aio_write,
Jens Axboe5274f052006-03-30 15:15:30 +0200298 .splice_read = generic_file_splice_read,
299 .splice_write = generic_file_splice_write,
Christoph Hellwig91efc162008-09-08 19:42:50 +0200300 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301};
302
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800303const struct inode_operations reiserfs_file_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700304 .truncate = reiserfs_vfs_truncate_file,
305 .setattr = reiserfs_setattr,
306 .setxattr = reiserfs_setxattr,
307 .getxattr = reiserfs_getxattr,
308 .listxattr = reiserfs_listxattr,
309 .removexattr = reiserfs_removexattr,
310 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};