blob: 2f8c5c9bdaf600abdee1883ceab6633d5caf6467 [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>
Al Virof466c6f2012-03-17 01:16:43 -04006#include "reiserfs.h"
Al Viroa3063ab2012-03-17 01:03:10 -04007#include "acl.h"
Al Viroc45ac882012-03-17 00:59:06 -04008#include "xattr.h"
Fabian Frederick170939912014-08-08 14:21:12 -07009#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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/*
Jeff Mahoney098297b2014-04-23 10:00:36 -040018 * 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.
23 *
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.
28 *
29 * We use reiserfs_truncate_file to pack the tail, since it already has
30 * all the conditions coded.
31 */
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
Al Viro0e4f6a72010-07-04 12:18:57 +040041 if (atomic_add_unless(&REISERFS_I(inode)->openers, -1, 1))
42 return 0;
43
Jeff Mahoneya228bf82014-04-23 10:00:42 -040044 mutex_lock(&REISERFS_I(inode)->tailpack);
Al Viro0e4f6a72010-07-04 12:18:57 +040045
46 if (!atomic_dec_and_test(&REISERFS_I(inode)->openers)) {
Jeff Mahoneya228bf82014-04-23 10:00:42 -040047 mutex_unlock(&REISERFS_I(inode)->tailpack);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070050
Al Viro0e4f6a72010-07-04 12:18:57 +040051 /* fast out for when nothing needs to be done */
52 if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
53 !tail_has_to_be_packed(inode)) &&
54 REISERFS_I(inode)->i_prealloc_count <= 0) {
Jeff Mahoneya228bf82014-04-23 10:00:42 -040055 mutex_unlock(&REISERFS_I(inode)->tailpack);
Al Viro0e4f6a72010-07-04 12:18:57 +040056 return 0;
57 }
Vladimir Savelievde145692007-01-22 20:40:46 -080058
Chris Masonb5f39532006-08-05 12:15:08 -070059 reiserfs_write_lock(inode->i_sb);
Jeff Mahoney098297b2014-04-23 10:00:36 -040060 /*
61 * freeing preallocation only involves relogging blocks that
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062 * are already in the current transaction. preallocation gets
63 * freed at the end of each transaction, so it is impossible for
64 * us to log any additional blocks (including quota blocks)
65 */
66 err = journal_begin(&th, inode->i_sb, 1);
67 if (err) {
Jeff Mahoney098297b2014-04-23 10:00:36 -040068 /*
69 * uh oh, we can't allow the inode to go away while there
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070 * is still preallocation blocks pending. Try to join the
71 * aborted transaction
72 */
73 jbegin_failure = err;
Jeff Mahoneyb491dd12014-04-23 10:00:40 -040074 err = journal_join_abort(&th, inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070075
76 if (err) {
Jeff Mahoney098297b2014-04-23 10:00:36 -040077 /*
78 * hmpf, our choices here aren't good. We can pin
79 * the inode which will disallow unmount from ever
80 * happening, we can do nothing, which will corrupt
81 * random memory on unmount, or we can forcibly
82 * remove the file from the preallocation list, which
83 * will leak blocks on disk. Lets pin the inode
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 * and let the admin know what is going on.
85 */
86 igrab(inode);
Jeff Mahoney45b03d52009-03-30 14:02:21 -040087 reiserfs_warning(inode->i_sb, "clm-9001",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 "pinning inode %lu because the "
Alexey Dobriyan533221f2006-11-25 11:09:30 -080089 "preallocation can't be freed",
90 inode->i_ino);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091 goto out;
92 }
93 }
94 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#ifdef REISERFS_PREALLOCATE
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 reiserfs_discard_prealloc(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#endif
Jeff Mahoney58d85422014-04-23 10:00:38 -040099 err = journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700101 /* copy back the error code from journal_begin */
102 if (!err)
103 err = jbegin_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Al Viro0e4f6a72010-07-04 12:18:57 +0400105 if (!err &&
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700106 (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
107 tail_has_to_be_packed(inode)) {
Al Viro0e4f6a72010-07-04 12:18:57 +0400108
Jeff Mahoney098297b2014-04-23 10:00:36 -0400109 /*
110 * if regular file is released by last holder and it has been
111 * appended (we append by unformatted node only) or its direct
112 * item(s) had to be converted, then it may have to be
113 * indirect2direct converted
114 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700115 err = reiserfs_truncate_file(inode, 0);
116 }
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400117out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400119 mutex_unlock(&REISERFS_I(inode)->tailpack);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Al Viro0e4f6a72010-07-04 12:18:57 +0400123static int reiserfs_file_open(struct inode *inode, struct file *file)
Vladimir Savelievde145692007-01-22 20:40:46 -0800124{
Al Viro0e4f6a72010-07-04 12:18:57 +0400125 int err = dquot_file_open(inode, file);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400126
127 /* somebody might be tailpacking on final close; wait for it */
Al Viro0e4f6a72010-07-04 12:18:57 +0400128 if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400129 mutex_lock(&REISERFS_I(inode)->tailpack);
Al Viro0e4f6a72010-07-04 12:18:57 +0400130 atomic_inc(&REISERFS_I(inode)->openers);
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400131 mutex_unlock(&REISERFS_I(inode)->tailpack);
Al Viro0e4f6a72010-07-04 12:18:57 +0400132 }
133 return err;
Vladimir Savelievde145692007-01-22 20:40:46 -0800134}
135
Marco Stornellicfac4b42012-12-15 11:47:31 +0100136void reiserfs_vfs_truncate_file(struct inode *inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700137{
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400138 mutex_lock(&REISERFS_I(inode)->tailpack);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 reiserfs_truncate_file(inode, 1);
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400140 mutex_unlock(&REISERFS_I(inode)->tailpack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/* Sync a reiserfs file. */
144
145/*
146 * FIXME: sync_mapping_buffers() never has anything to sync. Can
147 * be removed...
148 */
149
Josef Bacik02c24a82011-07-16 20:44:56 -0400150static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,
151 int datasync)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200153 struct inode *inode = filp->f_mapping->host;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400154 int err;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 int barrier_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Josef Bacik02c24a82011-07-16 20:44:56 -0400157 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
158 if (err)
159 return err;
160
Al Viro59551022016-01-22 15:40:57 -0500161 inode_lock(inode);
Jeff Mahoney995c7622009-03-30 14:02:47 -0400162 BUG_ON(!S_ISREG(inode->i_mode));
Jeff Mahoneyee939612009-03-30 14:02:50 -0400163 err = sync_mapping_buffers(inode->i_mapping);
Jeff Mahoney995c7622009-03-30 14:02:47 -0400164 reiserfs_write_lock(inode->i_sb);
165 barrier_done = reiserfs_commit_for_inode(inode);
166 reiserfs_write_unlock(inode->i_sb);
167 if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200168 blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
Al Viro59551022016-01-22 15:40:57 -0500169 inode_unlock(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 if (barrier_done < 0)
171 return barrier_done;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400172 return (err < 0) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* taken fs/buffer.c:__block_commit_write */
176int reiserfs_commit_page(struct inode *inode, struct page *page,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 unsigned from, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 unsigned block_start, block_end;
180 int partial = 0;
181 unsigned blocksize;
182 struct buffer_head *bh, *head;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300183 unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 int new;
185 int logit = reiserfs_file_data_log(inode);
186 struct super_block *s = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300187 int bh_per_page = PAGE_SIZE / s->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 struct reiserfs_transaction_handle th;
189 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 th.t_trans_id = 0;
192 blocksize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 if (logit) {
195 reiserfs_write_lock(s);
196 ret = journal_begin(&th, s, bh_per_page + 1);
197 if (ret)
198 goto drop_write_lock;
199 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 for (bh = head = page_buffers(page), block_start = 0;
202 bh != head || !block_start;
203 block_start = block_end, bh = bh->b_this_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 new = buffer_new(bh);
206 clear_buffer_new(bh);
207 block_end = block_start + blocksize;
208 if (block_end <= from || block_start >= to) {
209 if (!buffer_uptodate(bh))
210 partial = 1;
211 } else {
212 set_buffer_uptodate(bh);
213 if (logit) {
214 reiserfs_prepare_for_journal(s, bh, 1);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400215 journal_mark_dirty(&th, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 } else if (!buffer_dirty(bh)) {
217 mark_buffer_dirty(bh);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400218 /*
219 * do data=ordered on any page past the end
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 * of file and any buffer marked BH_New.
221 */
222 if (reiserfs_data_ordered(inode->i_sb) &&
223 (new || page->index >= i_size_index)) {
224 reiserfs_add_ordered_list(inode, bh);
225 }
226 }
227 }
228 }
229 if (logit) {
Jeff Mahoney58d85422014-04-23 10:00:38 -0400230 ret = journal_end(&th);
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400231drop_write_lock:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 reiserfs_write_unlock(s);
233 }
234 /*
235 * If this is a partial write which happened to make all buffers
236 * uptodate then we can optimize away a bogus readpage() for
237 * the next read(). Here we 'discover' whether the page went
238 * uptodate as a result of this (potentially partial) write.
239 */
240 if (!partial)
241 SetPageUptodate(page);
242 return ret;
243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800245const struct file_operations reiserfs_file_operations = {
Frederic Weisbecker205cb372009-10-14 23:22:17 +0200246 .unlocked_ioctl = reiserfs_ioctl,
David Howells52b499c2006-08-29 19:06:18 +0100247#ifdef CONFIG_COMPAT
248 .compat_ioctl = reiserfs_compat_ioctl,
249#endif
Al Viro0e4f6a72010-07-04 12:18:57 +0400250 .mmap = generic_file_mmap,
251 .open = reiserfs_file_open,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 .release = reiserfs_file_release,
253 .fsync = reiserfs_sync_file,
Al Viroaad4f8b2014-04-02 14:33:16 -0400254 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400255 .write_iter = generic_file_write_iter,
Jens Axboe5274f052006-03-30 15:15:30 +0200256 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400257 .splice_write = iter_file_splice_write,
Christoph Hellwig91efc162008-09-08 19:42:50 +0200258 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800261const struct inode_operations reiserfs_file_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200265 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -0800266 .set_acl = reiserfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};