]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README | |
3 | */ | |
4 | ||
1da177e4 | 5 | #include <linux/time.h> |
f466c6fd | 6 | #include "reiserfs.h" |
a3063ab8 | 7 | #include "acl.h" |
c45ac888 | 8 | #include "xattr.h" |
17093991 | 9 | #include <linux/uaccess.h> |
1da177e4 LT |
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 | /* | |
098297b2 JM |
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. | |
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 | */ | |
bd4c625c | 32 | static 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 | ||
a228bf8f | 44 | mutex_lock(&REISERFS_I(inode)->tailpack); |
0e4f6a79 AV |
45 | |
46 | if (!atomic_dec_and_test(&REISERFS_I(inode)->openers)) { | |
a228bf8f | 47 | mutex_unlock(&REISERFS_I(inode)->tailpack); |
0e4f6a79 AV |
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) { | |
a228bf8f | 55 | mutex_unlock(&REISERFS_I(inode)->tailpack); |
bd4c625c LT |
56 | return 0; |
57 | } | |
1da177e4 | 58 | |
b5f3953c | 59 | reiserfs_write_lock(inode->i_sb); |
098297b2 JM |
60 | /* |
61 | * freeing preallocation only involves relogging blocks that | |
bd4c625c LT |
62 | * 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); | |
1da177e4 | 67 | if (err) { |
098297b2 JM |
68 | /* |
69 | * uh oh, we can't allow the inode to go away while there | |
bd4c625c LT |
70 | * is still preallocation blocks pending. Try to join the |
71 | * aborted transaction | |
72 | */ | |
73 | jbegin_failure = err; | |
b491dd17 | 74 | err = journal_join_abort(&th, inode->i_sb); |
bd4c625c LT |
75 | |
76 | if (err) { | |
098297b2 JM |
77 | /* |
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 | |
bd4c625c LT |
84 | * and let the admin know what is going on. |
85 | */ | |
86 | igrab(inode); | |
45b03d5e | 87 | reiserfs_warning(inode->i_sb, "clm-9001", |
bd4c625c | 88 | "pinning inode %lu because the " |
533221fb AD |
89 | "preallocation can't be freed", |
90 | inode->i_ino); | |
bd4c625c LT |
91 | goto out; |
92 | } | |
1da177e4 | 93 | } |
bd4c625c | 94 | reiserfs_update_inode_transaction(inode); |
1da177e4 LT |
95 | |
96 | #ifdef REISERFS_PREALLOCATE | |
bd4c625c | 97 | reiserfs_discard_prealloc(&th, inode); |
1da177e4 | 98 | #endif |
58d85426 | 99 | err = journal_end(&th); |
bd4c625c LT |
100 | |
101 | /* copy back the error code from journal_begin */ | |
102 | if (!err) | |
103 | err = jbegin_failure; | |
104 | ||
0e4f6a79 | 105 | if (!err && |
bd4c625c LT |
106 | (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) && |
107 | tail_has_to_be_packed(inode)) { | |
0e4f6a79 | 108 | |
098297b2 JM |
109 | /* |
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 | */ | |
bd4c625c LT |
115 | err = reiserfs_truncate_file(inode, 0); |
116 | } | |
cf776a7a | 117 | out: |
bd4c625c | 118 | reiserfs_write_unlock(inode->i_sb); |
a228bf8f | 119 | mutex_unlock(&REISERFS_I(inode)->tailpack); |
bd4c625c | 120 | return err; |
1da177e4 LT |
121 | } |
122 | ||
0e4f6a79 | 123 | static int reiserfs_file_open(struct inode *inode, struct file *file) |
de14569f | 124 | { |
0e4f6a79 | 125 | int err = dquot_file_open(inode, file); |
098297b2 JM |
126 | |
127 | /* somebody might be tailpacking on final close; wait for it */ | |
0e4f6a79 | 128 | if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) { |
a228bf8f | 129 | mutex_lock(&REISERFS_I(inode)->tailpack); |
0e4f6a79 | 130 | atomic_inc(&REISERFS_I(inode)->openers); |
a228bf8f | 131 | mutex_unlock(&REISERFS_I(inode)->tailpack); |
0e4f6a79 AV |
132 | } |
133 | return err; | |
de14569f VS |
134 | } |
135 | ||
cfac4b47 | 136 | void reiserfs_vfs_truncate_file(struct inode *inode) |
bd4c625c | 137 | { |
a228bf8f | 138 | mutex_lock(&REISERFS_I(inode)->tailpack); |
bd4c625c | 139 | reiserfs_truncate_file(inode, 1); |
a228bf8f | 140 | mutex_unlock(&REISERFS_I(inode)->tailpack); |
1da177e4 LT |
141 | } |
142 | ||
143 | /* Sync a reiserfs file. */ | |
144 | ||
145 | /* | |
146 | * FIXME: sync_mapping_buffers() never has anything to sync. Can | |
147 | * be removed... | |
148 | */ | |
149 | ||
02c24a82 JB |
150 | static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end, |
151 | int datasync) | |
bd4c625c | 152 | { |
7ea80859 | 153 | struct inode *inode = filp->f_mapping->host; |
ee93961b | 154 | int err; |
bd4c625c LT |
155 | int barrier_done; |
156 | ||
3b49c9a1 | 157 | err = file_write_and_wait_range(filp, start, end); |
02c24a82 JB |
158 | if (err) |
159 | return err; | |
160 | ||
5955102c | 161 | inode_lock(inode); |
995c762e | 162 | BUG_ON(!S_ISREG(inode->i_mode)); |
ee93961b | 163 | err = sync_mapping_buffers(inode->i_mapping); |
995c762e JM |
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)) | |
dd3932ed | 168 | blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); |
5955102c | 169 | inode_unlock(inode); |
bd4c625c LT |
170 | if (barrier_done < 0) |
171 | return barrier_done; | |
ee93961b | 172 | return (err < 0) ? -EIO : 0; |
1da177e4 LT |
173 | } |
174 | ||
1da177e4 LT |
175 | /* taken fs/buffer.c:__block_commit_write */ |
176 | int reiserfs_commit_page(struct inode *inode, struct page *page, | |
bd4c625c | 177 | unsigned from, unsigned to) |
1da177e4 | 178 | { |
bd4c625c LT |
179 | unsigned block_start, block_end; |
180 | int partial = 0; | |
181 | unsigned blocksize; | |
182 | struct buffer_head *bh, *head; | |
09cbfeaf | 183 | unsigned long i_size_index = inode->i_size >> PAGE_SHIFT; |
bd4c625c LT |
184 | int new; |
185 | int logit = reiserfs_file_data_log(inode); | |
186 | struct super_block *s = inode->i_sb; | |
09cbfeaf | 187 | int bh_per_page = PAGE_SIZE / s->s_blocksize; |
bd4c625c LT |
188 | struct reiserfs_transaction_handle th; |
189 | int ret = 0; | |
190 | ||
191 | th.t_trans_id = 0; | |
93407472 | 192 | blocksize = i_blocksize(inode); |
bd4c625c LT |
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); | |
09f1b80b | 215 | journal_mark_dirty(&th, bh); |
bd4c625c LT |
216 | } else if (!buffer_dirty(bh)) { |
217 | mark_buffer_dirty(bh); | |
098297b2 JM |
218 | /* |
219 | * do data=ordered on any page past the end | |
bd4c625c LT |
220 | * 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 | } | |
1da177e4 | 228 | } |
bd4c625c | 229 | if (logit) { |
58d85426 | 230 | ret = journal_end(&th); |
cf776a7a | 231 | drop_write_lock: |
bd4c625c LT |
232 | 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; | |
1da177e4 LT |
243 | } |
244 | ||
4b6f5d20 | 245 | const struct file_operations reiserfs_file_operations = { |
205cb37b | 246 | .unlocked_ioctl = reiserfs_ioctl, |
52b499c4 DH |
247 | #ifdef CONFIG_COMPAT |
248 | .compat_ioctl = reiserfs_compat_ioctl, | |
249 | #endif | |
0e4f6a79 AV |
250 | .mmap = generic_file_mmap, |
251 | .open = reiserfs_file_open, | |
bd4c625c LT |
252 | .release = reiserfs_file_release, |
253 | .fsync = reiserfs_sync_file, | |
aad4f8bb | 254 | .read_iter = generic_file_read_iter, |
8174202b | 255 | .write_iter = generic_file_write_iter, |
5274f052 | 256 | .splice_read = generic_file_splice_read, |
8d020765 | 257 | .splice_write = iter_file_splice_write, |
91efc167 | 258 | .llseek = generic_file_llseek, |
1da177e4 LT |
259 | }; |
260 | ||
c5ef1c42 | 261 | const struct inode_operations reiserfs_file_inode_operations = { |
bd4c625c | 262 | .setattr = reiserfs_setattr, |
bd4c625c | 263 | .listxattr = reiserfs_listxattr, |
bd4c625c | 264 | .permission = reiserfs_permission, |
4e34e719 | 265 | .get_acl = reiserfs_get_acl, |
47f70d08 | 266 | .set_acl = reiserfs_set_acl, |
1da177e4 | 267 | }; |