1
2
3
4
5#include <linux/time.h>
6#include "reiserfs.h"
7#include "acl.h"
8#include "xattr.h"
9#include <linux/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
19
20
21
22
23
24
25
26
27
28
29
30
31
32static int reiserfs_file_release(struct inode *inode, struct file *filp)
33{
34
35 struct reiserfs_transaction_handle th;
36 int err;
37 int jbegin_failure = 0;
38
39 BUG_ON(!S_ISREG(inode->i_mode));
40
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
51
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) {
55 mutex_unlock(&REISERFS_I(inode)->tailpack);
56 return 0;
57 }
58
59 reiserfs_write_lock(inode->i_sb);
60
61
62
63
64
65
66 err = journal_begin(&th, inode->i_sb, 1);
67 if (err) {
68
69
70
71
72
73 jbegin_failure = err;
74 err = journal_join_abort(&th, inode->i_sb);
75
76 if (err) {
77
78
79
80
81
82
83
84
85
86 igrab(inode);
87 reiserfs_warning(inode->i_sb, "clm-9001",
88 "pinning inode %lu because the "
89 "preallocation can't be freed",
90 inode->i_ino);
91 goto out;
92 }
93 }
94 reiserfs_update_inode_transaction(inode);
95
96#ifdef REISERFS_PREALLOCATE
97 reiserfs_discard_prealloc(&th, inode);
98#endif
99 err = journal_end(&th);
100
101
102 if (!err)
103 err = jbegin_failure;
104
105 if (!err &&
106 (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
107 tail_has_to_be_packed(inode)) {
108
109
110
111
112
113
114
115 err = reiserfs_truncate_file(inode, 0);
116 }
117out:
118 reiserfs_write_unlock(inode->i_sb);
119 mutex_unlock(&REISERFS_I(inode)->tailpack);
120 return err;
121}
122
123static int reiserfs_file_open(struct inode *inode, struct file *file)
124{
125 int err = dquot_file_open(inode, file);
126
127
128 if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
129 mutex_lock(&REISERFS_I(inode)->tailpack);
130 atomic_inc(&REISERFS_I(inode)->openers);
131 mutex_unlock(&REISERFS_I(inode)->tailpack);
132 }
133 return err;
134}
135
136void reiserfs_vfs_truncate_file(struct inode *inode)
137{
138 mutex_lock(&REISERFS_I(inode)->tailpack);
139 reiserfs_truncate_file(inode, 1);
140 mutex_unlock(&REISERFS_I(inode)->tailpack);
141}
142
143
144
145
146
147
148
149
150static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,
151 int datasync)
152{
153 struct inode *inode = filp->f_mapping->host;
154 int err;
155 int barrier_done;
156
157 err = file_write_and_wait_range(filp, start, end);
158 if (err)
159 return err;
160
161 inode_lock(inode);
162 BUG_ON(!S_ISREG(inode->i_mode));
163 err = sync_mapping_buffers(inode->i_mapping);
164 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))
168 blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
169 inode_unlock(inode);
170 if (barrier_done < 0)
171 return barrier_done;
172 return (err < 0) ? -EIO : 0;
173}
174
175
176int reiserfs_commit_page(struct inode *inode, struct page *page,
177 unsigned from, unsigned to)
178{
179 unsigned block_start, block_end;
180 int partial = 0;
181 unsigned blocksize;
182 struct buffer_head *bh, *head;
183 unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;
184 int new;
185 int logit = reiserfs_file_data_log(inode);
186 struct super_block *s = inode->i_sb;
187 int bh_per_page = PAGE_SIZE / s->s_blocksize;
188 struct reiserfs_transaction_handle th;
189 int ret = 0;
190
191 th.t_trans_id = 0;
192 blocksize = i_blocksize(inode);
193
194 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);
200 }
201 for (bh = head = page_buffers(page), block_start = 0;
202 bh != head || !block_start;
203 block_start = block_end, bh = bh->b_this_page) {
204
205 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);
215 journal_mark_dirty(&th, bh);
216 } else if (!buffer_dirty(bh)) {
217 mark_buffer_dirty(bh);
218
219
220
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) {
230 ret = journal_end(&th);
231drop_write_lock:
232 reiserfs_write_unlock(s);
233 }
234
235
236
237
238
239
240 if (!partial)
241 SetPageUptodate(page);
242 return ret;
243}
244
245const struct file_operations reiserfs_file_operations = {
246 .unlocked_ioctl = reiserfs_ioctl,
247#ifdef CONFIG_COMPAT
248 .compat_ioctl = reiserfs_compat_ioctl,
249#endif
250 .mmap = generic_file_mmap,
251 .open = reiserfs_file_open,
252 .release = reiserfs_file_release,
253 .fsync = reiserfs_sync_file,
254 .read_iter = generic_file_read_iter,
255 .write_iter = generic_file_write_iter,
256 .splice_read = generic_file_splice_read,
257 .splice_write = iter_file_splice_write,
258 .llseek = generic_file_llseek,
259};
260
261const struct inode_operations reiserfs_file_inode_operations = {
262 .setattr = reiserfs_setattr,
263 .listxattr = reiserfs_listxattr,
264 .permission = reiserfs_permission,
265 .get_acl = reiserfs_get_acl,
266 .set_acl = reiserfs_set_acl,
267};
268