Commit | Line | Data |
---|---|---|
ccd979bd MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * file.c | |
5 | * | |
6 | * File open, close, extend, truncate | |
7 | * | |
8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public | |
21 | * License along with this program; if not, write to the | |
22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | * Boston, MA 021110-1307, USA. | |
24 | */ | |
25 | ||
16f7e0fe | 26 | #include <linux/capability.h> |
ccd979bd MF |
27 | #include <linux/fs.h> |
28 | #include <linux/types.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/highmem.h> | |
31 | #include <linux/pagemap.h> | |
32 | #include <linux/uio.h> | |
e2057c5a | 33 | #include <linux/sched.h> |
d6b29d7c | 34 | #include <linux/splice.h> |
7f1a37e3 | 35 | #include <linux/mount.h> |
9517bac6 | 36 | #include <linux/writeback.h> |
385820a3 | 37 | #include <linux/falloc.h> |
a90714c1 | 38 | #include <linux/quotaops.h> |
04eda1a1 | 39 | #include <linux/blkdev.h> |
66114cad | 40 | #include <linux/backing-dev.h> |
ccd979bd | 41 | |
ccd979bd MF |
42 | #include <cluster/masklog.h> |
43 | ||
44 | #include "ocfs2.h" | |
45 | ||
46 | #include "alloc.h" | |
47 | #include "aops.h" | |
48 | #include "dir.h" | |
49 | #include "dlmglue.h" | |
50 | #include "extent_map.h" | |
51 | #include "file.h" | |
52 | #include "sysfile.h" | |
53 | #include "inode.h" | |
ca4d147e | 54 | #include "ioctl.h" |
ccd979bd | 55 | #include "journal.h" |
53fc622b | 56 | #include "locks.h" |
ccd979bd MF |
57 | #include "mmap.h" |
58 | #include "suballoc.h" | |
59 | #include "super.h" | |
cf1d6c76 | 60 | #include "xattr.h" |
23fc2702 | 61 | #include "acl.h" |
a90714c1 | 62 | #include "quota.h" |
293b2f70 | 63 | #include "refcounttree.h" |
468eedde | 64 | #include "ocfs2_trace.h" |
ccd979bd MF |
65 | |
66 | #include "buffer_head_io.h" | |
67 | ||
53fc622b MF |
68 | static int ocfs2_init_file_private(struct inode *inode, struct file *file) |
69 | { | |
70 | struct ocfs2_file_private *fp; | |
71 | ||
72 | fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL); | |
73 | if (!fp) | |
74 | return -ENOMEM; | |
75 | ||
76 | fp->fp_file = file; | |
77 | mutex_init(&fp->fp_mutex); | |
78 | ocfs2_file_lock_res_init(&fp->fp_flock, fp); | |
79 | file->private_data = fp; | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | static void ocfs2_free_file_private(struct inode *inode, struct file *file) | |
85 | { | |
86 | struct ocfs2_file_private *fp = file->private_data; | |
87 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
88 | ||
89 | if (fp) { | |
90 | ocfs2_simple_drop_lockres(osb, &fp->fp_flock); | |
91 | ocfs2_lock_res_free(&fp->fp_flock); | |
92 | kfree(fp); | |
93 | file->private_data = NULL; | |
94 | } | |
95 | } | |
96 | ||
ccd979bd MF |
97 | static int ocfs2_file_open(struct inode *inode, struct file *file) |
98 | { | |
99 | int status; | |
100 | int mode = file->f_flags; | |
101 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
102 | ||
468eedde TM |
103 | trace_ocfs2_file_open(inode, file, file->f_path.dentry, |
104 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
105 | file->f_path.dentry->d_name.len, | |
106 | file->f_path.dentry->d_name.name, mode); | |
ccd979bd | 107 | |
907f4554 | 108 | if (file->f_mode & FMODE_WRITE) |
871a2931 | 109 | dquot_initialize(inode); |
907f4554 | 110 | |
ccd979bd MF |
111 | spin_lock(&oi->ip_lock); |
112 | ||
113 | /* Check that the inode hasn't been wiped from disk by another | |
114 | * node. If it hasn't then we're safe as long as we hold the | |
115 | * spin lock until our increment of open count. */ | |
116 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { | |
117 | spin_unlock(&oi->ip_lock); | |
118 | ||
119 | status = -ENOENT; | |
120 | goto leave; | |
121 | } | |
122 | ||
123 | if (mode & O_DIRECT) | |
124 | oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT; | |
125 | ||
126 | oi->ip_open_count++; | |
127 | spin_unlock(&oi->ip_lock); | |
53fc622b MF |
128 | |
129 | status = ocfs2_init_file_private(inode, file); | |
130 | if (status) { | |
131 | /* | |
132 | * We want to set open count back if we're failing the | |
133 | * open. | |
134 | */ | |
135 | spin_lock(&oi->ip_lock); | |
136 | oi->ip_open_count--; | |
137 | spin_unlock(&oi->ip_lock); | |
138 | } | |
139 | ||
ccd979bd | 140 | leave: |
ccd979bd MF |
141 | return status; |
142 | } | |
143 | ||
144 | static int ocfs2_file_release(struct inode *inode, struct file *file) | |
145 | { | |
146 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
147 | ||
ccd979bd MF |
148 | spin_lock(&oi->ip_lock); |
149 | if (!--oi->ip_open_count) | |
150 | oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; | |
468eedde TM |
151 | |
152 | trace_ocfs2_file_release(inode, file, file->f_path.dentry, | |
153 | oi->ip_blkno, | |
154 | file->f_path.dentry->d_name.len, | |
155 | file->f_path.dentry->d_name.name, | |
156 | oi->ip_open_count); | |
ccd979bd MF |
157 | spin_unlock(&oi->ip_lock); |
158 | ||
53fc622b MF |
159 | ocfs2_free_file_private(inode, file); |
160 | ||
ccd979bd MF |
161 | return 0; |
162 | } | |
163 | ||
53fc622b MF |
164 | static int ocfs2_dir_open(struct inode *inode, struct file *file) |
165 | { | |
166 | return ocfs2_init_file_private(inode, file); | |
167 | } | |
168 | ||
169 | static int ocfs2_dir_release(struct inode *inode, struct file *file) | |
170 | { | |
171 | ocfs2_free_file_private(inode, file); | |
172 | return 0; | |
173 | } | |
174 | ||
02c24a82 JB |
175 | static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end, |
176 | int datasync) | |
ccd979bd MF |
177 | { |
178 | int err = 0; | |
7ea80859 | 179 | struct inode *inode = file->f_mapping->host; |
ccd979bd | 180 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
2931cdcb DW |
181 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
182 | journal_t *journal = osb->journal->j_journal; | |
183 | int ret; | |
184 | tid_t commit_tid; | |
185 | bool needs_barrier = false; | |
ccd979bd | 186 | |
468eedde TM |
187 | trace_ocfs2_sync_file(inode, file, file->f_path.dentry, |
188 | OCFS2_I(inode)->ip_blkno, | |
189 | file->f_path.dentry->d_name.len, | |
190 | file->f_path.dentry->d_name.name, | |
191 | (unsigned long long)datasync); | |
ccd979bd | 192 | |
a987c7ca YL |
193 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
194 | return -EROFS; | |
195 | ||
02c24a82 JB |
196 | err = filemap_write_and_wait_range(inode->i_mapping, start, end); |
197 | if (err) | |
198 | return err; | |
199 | ||
2931cdcb DW |
200 | commit_tid = datasync ? oi->i_datasync_tid : oi->i_sync_tid; |
201 | if (journal->j_flags & JBD2_BARRIER && | |
202 | !jbd2_trans_will_send_data_barrier(journal, commit_tid)) | |
203 | needs_barrier = true; | |
204 | err = jbd2_complete_transaction(journal, commit_tid); | |
205 | if (needs_barrier) { | |
206 | ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); | |
207 | if (!err) | |
208 | err = ret; | |
04eda1a1 | 209 | } |
e04cc15f | 210 | |
c1e8d35e TM |
211 | if (err) |
212 | mlog_errno(err); | |
ccd979bd MF |
213 | |
214 | return (err < 0) ? -EIO : 0; | |
215 | } | |
216 | ||
7f1a37e3 TY |
217 | int ocfs2_should_update_atime(struct inode *inode, |
218 | struct vfsmount *vfsmnt) | |
219 | { | |
220 | struct timespec now; | |
221 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
222 | ||
223 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) | |
224 | return 0; | |
225 | ||
226 | if ((inode->i_flags & S_NOATIME) || | |
227 | ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
228 | return 0; | |
229 | ||
6c2aad05 MF |
230 | /* |
231 | * We can be called with no vfsmnt structure - NFSD will | |
232 | * sometimes do this. | |
233 | * | |
234 | * Note that our action here is different than touch_atime() - | |
235 | * if we can't tell whether this is a noatime mount, then we | |
236 | * don't know whether to trust the value of s_atime_quantum. | |
237 | */ | |
238 | if (vfsmnt == NULL) | |
239 | return 0; | |
240 | ||
7f1a37e3 TY |
241 | if ((vfsmnt->mnt_flags & MNT_NOATIME) || |
242 | ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
243 | return 0; | |
244 | ||
7e913c53 MF |
245 | if (vfsmnt->mnt_flags & MNT_RELATIME) { |
246 | if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) || | |
247 | (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0)) | |
248 | return 1; | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
7f1a37e3 TY |
253 | now = CURRENT_TIME; |
254 | if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum)) | |
255 | return 0; | |
256 | else | |
257 | return 1; | |
258 | } | |
259 | ||
260 | int ocfs2_update_inode_atime(struct inode *inode, | |
261 | struct buffer_head *bh) | |
262 | { | |
263 | int ret; | |
264 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
265 | handle_t *handle; | |
c11e9faf | 266 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data; |
7f1a37e3 | 267 | |
7f1a37e3 | 268 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
fa38e92c JK |
269 | if (IS_ERR(handle)) { |
270 | ret = PTR_ERR(handle); | |
7f1a37e3 TY |
271 | mlog_errno(ret); |
272 | goto out; | |
273 | } | |
274 | ||
0cf2f763 | 275 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh, |
13723d00 | 276 | OCFS2_JOURNAL_ACCESS_WRITE); |
c11e9faf MF |
277 | if (ret) { |
278 | mlog_errno(ret); | |
279 | goto out_commit; | |
280 | } | |
281 | ||
282 | /* | |
283 | * Don't use ocfs2_mark_inode_dirty() here as we don't always | |
284 | * have i_mutex to guard against concurrent changes to other | |
285 | * inode fields. | |
286 | */ | |
7f1a37e3 | 287 | inode->i_atime = CURRENT_TIME; |
c11e9faf MF |
288 | di->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
289 | di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); | |
6fdb702d | 290 | ocfs2_update_inode_fsync_trans(handle, inode, 0); |
ec20cec7 | 291 | ocfs2_journal_dirty(handle, bh); |
7f1a37e3 | 292 | |
c11e9faf | 293 | out_commit: |
7f1a37e3 TY |
294 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
295 | out: | |
7f1a37e3 TY |
296 | return ret; |
297 | } | |
298 | ||
026749a8 | 299 | int ocfs2_set_inode_size(handle_t *handle, |
6cb129f5 AB |
300 | struct inode *inode, |
301 | struct buffer_head *fe_bh, | |
302 | u64 new_i_size) | |
ccd979bd MF |
303 | { |
304 | int status; | |
305 | ||
ccd979bd | 306 | i_size_write(inode, new_i_size); |
8110b073 | 307 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
ccd979bd MF |
308 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
309 | ||
310 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
311 | if (status < 0) { | |
312 | mlog_errno(status); | |
313 | goto bail; | |
314 | } | |
315 | ||
316 | bail: | |
ccd979bd MF |
317 | return status; |
318 | } | |
319 | ||
9e33d69f JK |
320 | int ocfs2_simple_size_update(struct inode *inode, |
321 | struct buffer_head *di_bh, | |
322 | u64 new_i_size) | |
ccd979bd MF |
323 | { |
324 | int ret; | |
325 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1fabe148 | 326 | handle_t *handle = NULL; |
ccd979bd | 327 | |
65eff9cc | 328 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
fa38e92c JK |
329 | if (IS_ERR(handle)) { |
330 | ret = PTR_ERR(handle); | |
ccd979bd MF |
331 | mlog_errno(ret); |
332 | goto out; | |
333 | } | |
334 | ||
335 | ret = ocfs2_set_inode_size(handle, inode, di_bh, | |
336 | new_i_size); | |
337 | if (ret < 0) | |
338 | mlog_errno(ret); | |
339 | ||
6fdb702d | 340 | ocfs2_update_inode_fsync_trans(handle, inode, 0); |
02dc1af4 | 341 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
342 | out: |
343 | return ret; | |
344 | } | |
345 | ||
37f8a2bf TM |
346 | static int ocfs2_cow_file_pos(struct inode *inode, |
347 | struct buffer_head *fe_bh, | |
348 | u64 offset) | |
349 | { | |
350 | int status; | |
351 | u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits; | |
352 | unsigned int num_clusters = 0; | |
353 | unsigned int ext_flags = 0; | |
354 | ||
355 | /* | |
356 | * If the new offset is aligned to the range of the cluster, there is | |
357 | * no space for ocfs2_zero_range_for_truncate to fill, so no need to | |
358 | * CoW either. | |
359 | */ | |
360 | if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0) | |
361 | return 0; | |
362 | ||
363 | status = ocfs2_get_clusters(inode, cpos, &phys, | |
364 | &num_clusters, &ext_flags); | |
365 | if (status) { | |
366 | mlog_errno(status); | |
367 | goto out; | |
368 | } | |
369 | ||
370 | if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) | |
371 | goto out; | |
372 | ||
c7dd3392 | 373 | return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1); |
37f8a2bf TM |
374 | |
375 | out: | |
376 | return status; | |
377 | } | |
378 | ||
ccd979bd MF |
379 | static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb, |
380 | struct inode *inode, | |
381 | struct buffer_head *fe_bh, | |
382 | u64 new_i_size) | |
383 | { | |
384 | int status; | |
1fabe148 | 385 | handle_t *handle; |
60b11392 | 386 | struct ocfs2_dinode *di; |
35edec1d | 387 | u64 cluster_bytes; |
ccd979bd | 388 | |
37f8a2bf TM |
389 | /* |
390 | * We need to CoW the cluster contains the offset if it is reflinked | |
391 | * since we will call ocfs2_zero_range_for_truncate later which will | |
392 | * write "0" from offset to the end of the cluster. | |
393 | */ | |
394 | status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size); | |
395 | if (status) { | |
396 | mlog_errno(status); | |
397 | return status; | |
398 | } | |
399 | ||
ccd979bd MF |
400 | /* TODO: This needs to actually orphan the inode in this |
401 | * transaction. */ | |
402 | ||
65eff9cc | 403 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
404 | if (IS_ERR(handle)) { |
405 | status = PTR_ERR(handle); | |
406 | mlog_errno(status); | |
407 | goto out; | |
408 | } | |
409 | ||
0cf2f763 | 410 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh, |
13723d00 | 411 | OCFS2_JOURNAL_ACCESS_WRITE); |
60b11392 MF |
412 | if (status < 0) { |
413 | mlog_errno(status); | |
414 | goto out_commit; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Do this before setting i_size. | |
419 | */ | |
35edec1d MF |
420 | cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size); |
421 | status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size, | |
422 | cluster_bytes); | |
60b11392 MF |
423 | if (status) { |
424 | mlog_errno(status); | |
425 | goto out_commit; | |
426 | } | |
427 | ||
428 | i_size_write(inode, new_i_size); | |
60b11392 MF |
429 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
430 | ||
431 | di = (struct ocfs2_dinode *) fe_bh->b_data; | |
432 | di->i_size = cpu_to_le64(new_i_size); | |
433 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); | |
434 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); | |
6fdb702d | 435 | ocfs2_update_inode_fsync_trans(handle, inode, 0); |
60b11392 | 436 | |
ec20cec7 | 437 | ocfs2_journal_dirty(handle, fe_bh); |
ccd979bd | 438 | |
60b11392 | 439 | out_commit: |
02dc1af4 | 440 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 441 | out: |
ccd979bd MF |
442 | return status; |
443 | } | |
444 | ||
026749a8 | 445 | int ocfs2_truncate_file(struct inode *inode, |
ccd979bd MF |
446 | struct buffer_head *di_bh, |
447 | u64 new_i_size) | |
448 | { | |
449 | int status = 0; | |
450 | struct ocfs2_dinode *fe = NULL; | |
451 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
ccd979bd | 452 | |
b657c95c JB |
453 | /* We trust di_bh because it comes from ocfs2_inode_lock(), which |
454 | * already validated it */ | |
ccd979bd | 455 | fe = (struct ocfs2_dinode *) di_bh->b_data; |
ccd979bd | 456 | |
468eedde TM |
457 | trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno, |
458 | (unsigned long long)le64_to_cpu(fe->i_size), | |
459 | (unsigned long long)new_i_size); | |
460 | ||
ccd979bd | 461 | mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode), |
b0697053 MF |
462 | "Inode %llu, inode i_size = %lld != di " |
463 | "i_size = %llu, i_flags = 0x%x\n", | |
464 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
ccd979bd | 465 | i_size_read(inode), |
b0697053 MF |
466 | (unsigned long long)le64_to_cpu(fe->i_size), |
467 | le32_to_cpu(fe->i_flags)); | |
ccd979bd MF |
468 | |
469 | if (new_i_size > le64_to_cpu(fe->i_size)) { | |
468eedde TM |
470 | trace_ocfs2_truncate_file_error( |
471 | (unsigned long long)le64_to_cpu(fe->i_size), | |
472 | (unsigned long long)new_i_size); | |
ccd979bd MF |
473 | status = -EINVAL; |
474 | mlog_errno(status); | |
475 | goto bail; | |
476 | } | |
477 | ||
2e89b2e4 MF |
478 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
479 | ||
4fe370af MF |
480 | ocfs2_resv_discard(&osb->osb_la_resmap, |
481 | &OCFS2_I(inode)->ip_la_data_resv); | |
482 | ||
c934a92d MF |
483 | /* |
484 | * The inode lock forced other nodes to sync and drop their | |
485 | * pages, which (correctly) happens even if we have a truncate | |
486 | * without allocation change - ocfs2 cluster sizes can be much | |
487 | * greater than page size, so we have to truncate them | |
488 | * anyway. | |
489 | */ | |
2e89b2e4 MF |
490 | unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1); |
491 | truncate_inode_pages(inode->i_mapping, new_i_size); | |
492 | ||
1afc32b9 MF |
493 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
494 | status = ocfs2_truncate_inline(inode, di_bh, new_i_size, | |
b1967d0e | 495 | i_size_read(inode), 1); |
1afc32b9 MF |
496 | if (status) |
497 | mlog_errno(status); | |
498 | ||
c934a92d | 499 | goto bail_unlock_sem; |
1afc32b9 MF |
500 | } |
501 | ||
ccd979bd MF |
502 | /* alright, we're going to need to do a full blown alloc size |
503 | * change. Orphan the inode so that recovery can complete the | |
504 | * truncate if necessary. This does the task of marking | |
505 | * i_size. */ | |
506 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); | |
507 | if (status < 0) { | |
508 | mlog_errno(status); | |
c934a92d | 509 | goto bail_unlock_sem; |
ccd979bd MF |
510 | } |
511 | ||
78f94673 | 512 | status = ocfs2_commit_truncate(osb, inode, di_bh); |
ccd979bd MF |
513 | if (status < 0) { |
514 | mlog_errno(status); | |
c934a92d | 515 | goto bail_unlock_sem; |
ccd979bd MF |
516 | } |
517 | ||
518 | /* TODO: orphan dir cleanup here. */ | |
c934a92d | 519 | bail_unlock_sem: |
2e89b2e4 MF |
520 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
521 | ||
ccd979bd | 522 | bail: |
8b2c0dba TM |
523 | if (!status && OCFS2_I(inode)->ip_clusters == 0) |
524 | status = ocfs2_try_remove_refcount_tree(inode, di_bh); | |
ccd979bd | 525 | |
ccd979bd MF |
526 | return status; |
527 | } | |
528 | ||
529 | /* | |
0eb8d47e | 530 | * extend file allocation only here. |
ccd979bd MF |
531 | * we'll update all the disk stuff, and oip->alloc_size |
532 | * | |
533 | * expect stuff to be locked, a transaction started and enough data / | |
534 | * metadata reservations in the contexts. | |
535 | * | |
536 | * Will return -EAGAIN, and a reason if a restart is needed. | |
537 | * If passed in, *reason will always be set, even in error. | |
538 | */ | |
0eb8d47e TM |
539 | int ocfs2_add_inode_data(struct ocfs2_super *osb, |
540 | struct inode *inode, | |
541 | u32 *logical_offset, | |
542 | u32 clusters_to_add, | |
543 | int mark_unwritten, | |
544 | struct buffer_head *fe_bh, | |
545 | handle_t *handle, | |
546 | struct ocfs2_alloc_context *data_ac, | |
547 | struct ocfs2_alloc_context *meta_ac, | |
548 | enum ocfs2_alloc_restarted *reason_ret) | |
ccd979bd | 549 | { |
f99b9b7c JB |
550 | int ret; |
551 | struct ocfs2_extent_tree et; | |
ccd979bd | 552 | |
5e404e9e | 553 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh); |
cbee7e1a JB |
554 | ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset, |
555 | clusters_to_add, mark_unwritten, | |
556 | data_ac, meta_ac, reason_ret); | |
f99b9b7c JB |
557 | |
558 | return ret; | |
ccd979bd MF |
559 | } |
560 | ||
2ae99a60 MF |
561 | static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, |
562 | u32 clusters_to_add, int mark_unwritten) | |
ccd979bd MF |
563 | { |
564 | int status = 0; | |
565 | int restart_func = 0; | |
abf8b156 | 566 | int credits; |
2ae99a60 | 567 | u32 prev_clusters; |
ccd979bd MF |
568 | struct buffer_head *bh = NULL; |
569 | struct ocfs2_dinode *fe = NULL; | |
1fabe148 | 570 | handle_t *handle = NULL; |
ccd979bd MF |
571 | struct ocfs2_alloc_context *data_ac = NULL; |
572 | struct ocfs2_alloc_context *meta_ac = NULL; | |
696cdf73 | 573 | enum ocfs2_alloc_restarted why = RESTART_NONE; |
ccd979bd | 574 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
f99b9b7c | 575 | struct ocfs2_extent_tree et; |
a90714c1 | 576 | int did_quota = 0; |
ccd979bd | 577 | |
dcd0538f | 578 | /* |
f0cb0f0b | 579 | * Unwritten extent only exists for file systems which |
dcd0538f MF |
580 | * support holes. |
581 | */ | |
2ae99a60 | 582 | BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb)); |
dcd0538f | 583 | |
b657c95c | 584 | status = ocfs2_read_inode_block(inode, &bh); |
ccd979bd MF |
585 | if (status < 0) { |
586 | mlog_errno(status); | |
587 | goto leave; | |
588 | } | |
ccd979bd | 589 | fe = (struct ocfs2_dinode *) bh->b_data; |
ccd979bd MF |
590 | |
591 | restart_all: | |
592 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); | |
593 | ||
5e404e9e | 594 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh); |
f99b9b7c JB |
595 | status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0, |
596 | &data_ac, &meta_ac); | |
9517bac6 MF |
597 | if (status) { |
598 | mlog_errno(status); | |
599 | goto leave; | |
600 | } | |
601 | ||
06f9da6e | 602 | credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list); |
65eff9cc | 603 | handle = ocfs2_start_trans(osb, credits); |
ccd979bd MF |
604 | if (IS_ERR(handle)) { |
605 | status = PTR_ERR(handle); | |
606 | handle = NULL; | |
607 | mlog_errno(status); | |
608 | goto leave; | |
609 | } | |
610 | ||
611 | restarted_transaction: | |
468eedde TM |
612 | trace_ocfs2_extend_allocation( |
613 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
614 | (unsigned long long)i_size_read(inode), | |
615 | le32_to_cpu(fe->i_clusters), clusters_to_add, | |
616 | why, restart_func); | |
617 | ||
5dd4056d CH |
618 | status = dquot_alloc_space_nodirty(inode, |
619 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_add)); | |
620 | if (status) | |
a90714c1 | 621 | goto leave; |
a90714c1 JK |
622 | did_quota = 1; |
623 | ||
ccd979bd MF |
624 | /* reserve a write to the file entry early on - that we if we |
625 | * run out of credits in the allocation path, we can still | |
626 | * update i_size. */ | |
0cf2f763 | 627 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh, |
13723d00 | 628 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
629 | if (status < 0) { |
630 | mlog_errno(status); | |
631 | goto leave; | |
632 | } | |
633 | ||
634 | prev_clusters = OCFS2_I(inode)->ip_clusters; | |
635 | ||
0eb8d47e TM |
636 | status = ocfs2_add_inode_data(osb, |
637 | inode, | |
638 | &logical_start, | |
639 | clusters_to_add, | |
640 | mark_unwritten, | |
641 | bh, | |
642 | handle, | |
643 | data_ac, | |
644 | meta_ac, | |
645 | &why); | |
ccd979bd MF |
646 | if ((status < 0) && (status != -EAGAIN)) { |
647 | if (status != -ENOSPC) | |
648 | mlog_errno(status); | |
649 | goto leave; | |
650 | } | |
2931cdcb | 651 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
ec20cec7 | 652 | ocfs2_journal_dirty(handle, bh); |
ccd979bd MF |
653 | |
654 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
655 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); | |
656 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
a90714c1 | 657 | /* Release unused quota reservation */ |
5dd4056d | 658 | dquot_free_space(inode, |
a90714c1 JK |
659 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_add)); |
660 | did_quota = 0; | |
ccd979bd MF |
661 | |
662 | if (why != RESTART_NONE && clusters_to_add) { | |
663 | if (why == RESTART_META) { | |
ccd979bd | 664 | restart_func = 1; |
79681842 | 665 | status = 0; |
ccd979bd MF |
666 | } else { |
667 | BUG_ON(why != RESTART_TRANS); | |
668 | ||
2b1e55c3 | 669 | status = ocfs2_allocate_extend_trans(handle, 1); |
ccd979bd MF |
670 | if (status < 0) { |
671 | /* handle still has to be committed at | |
672 | * this point. */ | |
673 | status = -ENOMEM; | |
674 | mlog_errno(status); | |
675 | goto leave; | |
676 | } | |
677 | goto restarted_transaction; | |
678 | } | |
679 | } | |
680 | ||
468eedde | 681 | trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno, |
1ca1a111 | 682 | le32_to_cpu(fe->i_clusters), |
468eedde TM |
683 | (unsigned long long)le64_to_cpu(fe->i_size), |
684 | OCFS2_I(inode)->ip_clusters, | |
685 | (unsigned long long)i_size_read(inode)); | |
ccd979bd MF |
686 | |
687 | leave: | |
a90714c1 | 688 | if (status < 0 && did_quota) |
5dd4056d | 689 | dquot_free_space(inode, |
a90714c1 | 690 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_add)); |
ccd979bd | 691 | if (handle) { |
02dc1af4 | 692 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
693 | handle = NULL; |
694 | } | |
695 | if (data_ac) { | |
696 | ocfs2_free_alloc_context(data_ac); | |
697 | data_ac = NULL; | |
698 | } | |
699 | if (meta_ac) { | |
700 | ocfs2_free_alloc_context(meta_ac); | |
701 | meta_ac = NULL; | |
702 | } | |
703 | if ((!status) && restart_func) { | |
704 | restart_func = 0; | |
705 | goto restart_all; | |
706 | } | |
a81cb88b MF |
707 | brelse(bh); |
708 | bh = NULL; | |
ccd979bd | 709 | |
ccd979bd MF |
710 | return status; |
711 | } | |
712 | ||
026749a8 JQ |
713 | int ocfs2_extend_allocation(struct inode *inode, u32 logical_start, |
714 | u32 clusters_to_add, int mark_unwritten) | |
715 | { | |
716 | return __ocfs2_extend_allocation(inode, logical_start, | |
717 | clusters_to_add, mark_unwritten); | |
718 | } | |
719 | ||
a4bfb4cf JB |
720 | /* |
721 | * While a write will already be ordering the data, a truncate will not. | |
722 | * Thus, we need to explicitly order the zeroed pages. | |
723 | */ | |
c7d2cbc3 JB |
724 | static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode, |
725 | struct buffer_head *di_bh) | |
a4bfb4cf JB |
726 | { |
727 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
728 | handle_t *handle = NULL; | |
729 | int ret = 0; | |
730 | ||
731 | if (!ocfs2_should_order_data(inode)) | |
732 | goto out; | |
733 | ||
734 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
735 | if (IS_ERR(handle)) { | |
736 | ret = -ENOMEM; | |
737 | mlog_errno(ret); | |
738 | goto out; | |
739 | } | |
740 | ||
741 | ret = ocfs2_jbd2_file_inode(handle, inode); | |
c7d2cbc3 JB |
742 | if (ret < 0) { |
743 | mlog_errno(ret); | |
744 | goto out; | |
745 | } | |
746 | ||
747 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, | |
748 | OCFS2_JOURNAL_ACCESS_WRITE); | |
749 | if (ret) | |
a4bfb4cf | 750 | mlog_errno(ret); |
6fdb702d | 751 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
a4bfb4cf JB |
752 | |
753 | out: | |
754 | if (ret) { | |
755 | if (!IS_ERR(handle)) | |
756 | ocfs2_commit_trans(osb, handle); | |
757 | handle = ERR_PTR(ret); | |
758 | } | |
759 | return handle; | |
760 | } | |
761 | ||
ccd979bd MF |
762 | /* Some parts of this taken from generic_cont_expand, which turned out |
763 | * to be too fragile to do exactly what we need without us having to | |
4e02ed4b | 764 | * worry about recursive locking in ->write_begin() and ->write_end(). */ |
a4bfb4cf | 765 | static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from, |
c7d2cbc3 | 766 | u64 abs_to, struct buffer_head *di_bh) |
ccd979bd MF |
767 | { |
768 | struct address_space *mapping = inode->i_mapping; | |
769 | struct page *page; | |
a4bfb4cf | 770 | unsigned long index = abs_from >> PAGE_CACHE_SHIFT; |
f775da2f | 771 | handle_t *handle; |
5453258d | 772 | int ret = 0; |
a4bfb4cf | 773 | unsigned zero_from, zero_to, block_start, block_end; |
c7d2cbc3 | 774 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
ccd979bd | 775 | |
a4bfb4cf JB |
776 | BUG_ON(abs_from >= abs_to); |
777 | BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT)); | |
778 | BUG_ON(abs_from & (inode->i_blkbits - 1)); | |
ccd979bd | 779 | |
f775da2f JB |
780 | handle = ocfs2_zero_start_ordered_transaction(inode, di_bh); |
781 | if (IS_ERR(handle)) { | |
782 | ret = PTR_ERR(handle); | |
783 | goto out; | |
784 | } | |
785 | ||
9b4c0ff3 | 786 | page = find_or_create_page(mapping, index, GFP_NOFS); |
ccd979bd MF |
787 | if (!page) { |
788 | ret = -ENOMEM; | |
789 | mlog_errno(ret); | |
f775da2f | 790 | goto out_commit_trans; |
ccd979bd MF |
791 | } |
792 | ||
a4bfb4cf JB |
793 | /* Get the offsets within the page that we want to zero */ |
794 | zero_from = abs_from & (PAGE_CACHE_SIZE - 1); | |
795 | zero_to = abs_to & (PAGE_CACHE_SIZE - 1); | |
796 | if (!zero_to) | |
797 | zero_to = PAGE_CACHE_SIZE; | |
ccd979bd | 798 | |
468eedde TM |
799 | trace_ocfs2_write_zero_page( |
800 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
801 | (unsigned long long)abs_from, | |
802 | (unsigned long long)abs_to, | |
803 | index, zero_from, zero_to); | |
5693486b | 804 | |
a4bfb4cf JB |
805 | /* We know that zero_from is block aligned */ |
806 | for (block_start = zero_from; block_start < zero_to; | |
807 | block_start = block_end) { | |
808 | block_end = block_start + (1 << inode->i_blkbits); | |
809 | ||
810 | /* | |
ebdec241 CH |
811 | * block_start is block-aligned. Bump it by one to force |
812 | * __block_write_begin and block_commit_write to zero the | |
a4bfb4cf JB |
813 | * whole block. |
814 | */ | |
ebdec241 CH |
815 | ret = __block_write_begin(page, block_start + 1, 0, |
816 | ocfs2_get_block); | |
a4bfb4cf JB |
817 | if (ret < 0) { |
818 | mlog_errno(ret); | |
ccd979bd MF |
819 | goto out_unlock; |
820 | } | |
ccd979bd | 821 | |
a4bfb4cf JB |
822 | |
823 | /* must not update i_size! */ | |
824 | ret = block_commit_write(page, block_start + 1, | |
825 | block_start + 1); | |
826 | if (ret < 0) | |
827 | mlog_errno(ret); | |
828 | else | |
829 | ret = 0; | |
830 | } | |
ccd979bd | 831 | |
f775da2f JB |
832 | /* |
833 | * fs-writeback will release the dirty pages without page lock | |
834 | * whose offset are over inode size, the release happens at | |
835 | * block_write_full_page(). | |
836 | */ | |
837 | i_size_write(inode, abs_to); | |
838 | inode->i_blocks = ocfs2_inode_sector_count(inode); | |
839 | di->i_size = cpu_to_le64((u64)i_size_read(inode)); | |
840 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
841 | di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec); | |
842 | di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
843 | di->i_mtime_nsec = di->i_ctime_nsec; | |
c7d2cbc3 | 844 | if (handle) { |
c7d2cbc3 | 845 | ocfs2_journal_dirty(handle, di_bh); |
6fdb702d | 846 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
c7d2cbc3 | 847 | } |
a4bfb4cf | 848 | |
ccd979bd MF |
849 | out_unlock: |
850 | unlock_page(page); | |
851 | page_cache_release(page); | |
f775da2f JB |
852 | out_commit_trans: |
853 | if (handle) | |
854 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
ccd979bd MF |
855 | out: |
856 | return ret; | |
857 | } | |
858 | ||
5693486b JB |
859 | /* |
860 | * Find the next range to zero. We do this in terms of bytes because | |
861 | * that's what ocfs2_zero_extend() wants, and it is dealing with the | |
862 | * pagecache. We may return multiple extents. | |
863 | * | |
864 | * zero_start and zero_end are ocfs2_zero_extend()s current idea of what | |
865 | * needs to be zeroed. range_start and range_end return the next zeroing | |
866 | * range. A subsequent call should pass the previous range_end as its | |
867 | * zero_start. If range_end is 0, there's nothing to do. | |
868 | * | |
869 | * Unwritten extents are skipped over. Refcounted extents are CoWd. | |
870 | */ | |
871 | static int ocfs2_zero_extend_get_range(struct inode *inode, | |
872 | struct buffer_head *di_bh, | |
873 | u64 zero_start, u64 zero_end, | |
874 | u64 *range_start, u64 *range_end) | |
ccd979bd | 875 | { |
5693486b JB |
876 | int rc = 0, needs_cow = 0; |
877 | u32 p_cpos, zero_clusters = 0; | |
878 | u32 zero_cpos = | |
879 | zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits; | |
880 | u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end); | |
881 | unsigned int num_clusters = 0; | |
882 | unsigned int ext_flags = 0; | |
ccd979bd | 883 | |
5693486b JB |
884 | while (zero_cpos < last_cpos) { |
885 | rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos, | |
886 | &num_clusters, &ext_flags); | |
887 | if (rc) { | |
888 | mlog_errno(rc); | |
ccd979bd MF |
889 | goto out; |
890 | } | |
891 | ||
5693486b JB |
892 | if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) { |
893 | zero_clusters = num_clusters; | |
894 | if (ext_flags & OCFS2_EXT_REFCOUNTED) | |
895 | needs_cow = 1; | |
896 | break; | |
897 | } | |
898 | ||
899 | zero_cpos += num_clusters; | |
900 | } | |
901 | if (!zero_clusters) { | |
902 | *range_end = 0; | |
903 | goto out; | |
904 | } | |
905 | ||
906 | while ((zero_cpos + zero_clusters) < last_cpos) { | |
907 | rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters, | |
908 | &p_cpos, &num_clusters, | |
909 | &ext_flags); | |
910 | if (rc) { | |
911 | mlog_errno(rc); | |
912 | goto out; | |
913 | } | |
914 | ||
915 | if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN)) | |
916 | break; | |
917 | if (ext_flags & OCFS2_EXT_REFCOUNTED) | |
918 | needs_cow = 1; | |
919 | zero_clusters += num_clusters; | |
920 | } | |
921 | if ((zero_cpos + zero_clusters) > last_cpos) | |
922 | zero_clusters = last_cpos - zero_cpos; | |
923 | ||
924 | if (needs_cow) { | |
c7dd3392 | 925 | rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos, |
15502712 | 926 | zero_clusters, UINT_MAX); |
5693486b JB |
927 | if (rc) { |
928 | mlog_errno(rc); | |
929 | goto out; | |
930 | } | |
931 | } | |
932 | ||
933 | *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos); | |
934 | *range_end = ocfs2_clusters_to_bytes(inode->i_sb, | |
935 | zero_cpos + zero_clusters); | |
936 | ||
937 | out: | |
938 | return rc; | |
939 | } | |
940 | ||
941 | /* | |
942 | * Zero one range returned from ocfs2_zero_extend_get_range(). The caller | |
943 | * has made sure that the entire range needs zeroing. | |
944 | */ | |
945 | static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start, | |
c7d2cbc3 | 946 | u64 range_end, struct buffer_head *di_bh) |
5693486b JB |
947 | { |
948 | int rc = 0; | |
949 | u64 next_pos; | |
950 | u64 zero_pos = range_start; | |
951 | ||
468eedde TM |
952 | trace_ocfs2_zero_extend_range( |
953 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
954 | (unsigned long long)range_start, | |
955 | (unsigned long long)range_end); | |
5693486b JB |
956 | BUG_ON(range_start >= range_end); |
957 | ||
958 | while (zero_pos < range_end) { | |
959 | next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE; | |
960 | if (next_pos > range_end) | |
961 | next_pos = range_end; | |
c7d2cbc3 | 962 | rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh); |
5693486b JB |
963 | if (rc < 0) { |
964 | mlog_errno(rc); | |
965 | break; | |
966 | } | |
967 | zero_pos = next_pos; | |
e2057c5a MF |
968 | |
969 | /* | |
970 | * Very large extends have the potential to lock up | |
971 | * the cpu for extended periods of time. | |
972 | */ | |
973 | cond_resched(); | |
ccd979bd MF |
974 | } |
975 | ||
5693486b JB |
976 | return rc; |
977 | } | |
978 | ||
979 | int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh, | |
980 | loff_t zero_to_size) | |
981 | { | |
982 | int ret = 0; | |
983 | u64 zero_start, range_start = 0, range_end = 0; | |
984 | struct super_block *sb = inode->i_sb; | |
985 | ||
986 | zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); | |
468eedde TM |
987 | trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno, |
988 | (unsigned long long)zero_start, | |
989 | (unsigned long long)i_size_read(inode)); | |
5693486b JB |
990 | while (zero_start < zero_to_size) { |
991 | ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start, | |
992 | zero_to_size, | |
993 | &range_start, | |
994 | &range_end); | |
995 | if (ret) { | |
996 | mlog_errno(ret); | |
997 | break; | |
998 | } | |
999 | if (!range_end) | |
1000 | break; | |
1001 | /* Trim the ends */ | |
1002 | if (range_start < zero_start) | |
1003 | range_start = zero_start; | |
1004 | if (range_end > zero_to_size) | |
1005 | range_end = zero_to_size; | |
1006 | ||
1007 | ret = ocfs2_zero_extend_range(inode, range_start, | |
c7d2cbc3 | 1008 | range_end, di_bh); |
5693486b JB |
1009 | if (ret) { |
1010 | mlog_errno(ret); | |
1011 | break; | |
1012 | } | |
1013 | zero_start = range_end; | |
1014 | } | |
1015 | ||
ccd979bd MF |
1016 | return ret; |
1017 | } | |
1018 | ||
5693486b JB |
1019 | int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh, |
1020 | u64 new_i_size, u64 zero_to) | |
65ed39d6 MF |
1021 | { |
1022 | int ret; | |
1023 | u32 clusters_to_add; | |
1024 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1025 | ||
5693486b JB |
1026 | /* |
1027 | * Only quota files call this without a bh, and they can't be | |
1028 | * refcounted. | |
1029 | */ | |
1030 | BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)); | |
1031 | BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE)); | |
1032 | ||
65ed39d6 MF |
1033 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size); |
1034 | if (clusters_to_add < oi->ip_clusters) | |
1035 | clusters_to_add = 0; | |
1036 | else | |
1037 | clusters_to_add -= oi->ip_clusters; | |
1038 | ||
1039 | if (clusters_to_add) { | |
1040 | ret = __ocfs2_extend_allocation(inode, oi->ip_clusters, | |
1041 | clusters_to_add, 0); | |
1042 | if (ret) { | |
1043 | mlog_errno(ret); | |
1044 | goto out; | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | /* | |
1049 | * Call this even if we don't add any clusters to the tree. We | |
1050 | * still need to zero the area between the old i_size and the | |
1051 | * new i_size. | |
1052 | */ | |
5693486b | 1053 | ret = ocfs2_zero_extend(inode, di_bh, zero_to); |
65ed39d6 MF |
1054 | if (ret < 0) |
1055 | mlog_errno(ret); | |
1056 | ||
1057 | out: | |
1058 | return ret; | |
1059 | } | |
1060 | ||
ccd979bd MF |
1061 | static int ocfs2_extend_file(struct inode *inode, |
1062 | struct buffer_head *di_bh, | |
65ed39d6 | 1063 | u64 new_i_size) |
ccd979bd | 1064 | { |
c934a92d | 1065 | int ret = 0; |
1afc32b9 | 1066 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
ccd979bd | 1067 | |
65ed39d6 | 1068 | BUG_ON(!di_bh); |
53013cba | 1069 | |
ccd979bd MF |
1070 | /* setattr sometimes calls us like this. */ |
1071 | if (new_i_size == 0) | |
1072 | goto out; | |
1073 | ||
1074 | if (i_size_read(inode) == new_i_size) | |
5693486b | 1075 | goto out; |
ccd979bd MF |
1076 | BUG_ON(new_i_size < i_size_read(inode)); |
1077 | ||
0effef77 | 1078 | /* |
65ed39d6 MF |
1079 | * The alloc sem blocks people in read/write from reading our |
1080 | * allocation until we're done changing it. We depend on | |
1081 | * i_mutex to block other extend/truncate calls while we're | |
5693486b JB |
1082 | * here. We even have to hold it for sparse files because there |
1083 | * might be some tail zeroing. | |
0effef77 | 1084 | */ |
1afc32b9 MF |
1085 | down_write(&oi->ip_alloc_sem); |
1086 | ||
1087 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | |
1088 | /* | |
1089 | * We can optimize small extends by keeping the inodes | |
1090 | * inline data. | |
1091 | */ | |
1092 | if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) { | |
1093 | up_write(&oi->ip_alloc_sem); | |
1094 | goto out_update_size; | |
1095 | } | |
1096 | ||
1097 | ret = ocfs2_convert_inline_data_to_extents(inode, di_bh); | |
1098 | if (ret) { | |
1099 | up_write(&oi->ip_alloc_sem); | |
1afc32b9 | 1100 | mlog_errno(ret); |
c934a92d | 1101 | goto out; |
1afc32b9 MF |
1102 | } |
1103 | } | |
1104 | ||
5693486b JB |
1105 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) |
1106 | ret = ocfs2_zero_extend(inode, di_bh, new_i_size); | |
1107 | else | |
1108 | ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size, | |
1109 | new_i_size); | |
1afc32b9 MF |
1110 | |
1111 | up_write(&oi->ip_alloc_sem); | |
65ed39d6 | 1112 | |
0effef77 MF |
1113 | if (ret < 0) { |
1114 | mlog_errno(ret); | |
c934a92d | 1115 | goto out; |
53013cba MF |
1116 | } |
1117 | ||
3a0782d0 | 1118 | out_update_size: |
65ed39d6 MF |
1119 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); |
1120 | if (ret < 0) | |
1121 | mlog_errno(ret); | |
ccd979bd MF |
1122 | |
1123 | out: | |
1124 | return ret; | |
1125 | } | |
1126 | ||
1127 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) | |
1128 | { | |
1129 | int status = 0, size_change; | |
2b0143b5 | 1130 | struct inode *inode = d_inode(dentry); |
ccd979bd MF |
1131 | struct super_block *sb = inode->i_sb; |
1132 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
1133 | struct buffer_head *bh = NULL; | |
1fabe148 | 1134 | handle_t *handle = NULL; |
65bac575 | 1135 | struct dquot *transfer_to[MAXQUOTAS] = { }; |
52a9ee28 | 1136 | int qtype; |
ccd979bd | 1137 | |
468eedde TM |
1138 | trace_ocfs2_setattr(inode, dentry, |
1139 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1140 | dentry->d_name.len, dentry->d_name.name, | |
1141 | attr->ia_valid, attr->ia_mode, | |
ba613560 EB |
1142 | from_kuid(&init_user_ns, attr->ia_uid), |
1143 | from_kgid(&init_user_ns, attr->ia_gid)); | |
ccd979bd | 1144 | |
bc535809 SM |
1145 | /* ensuring we don't even attempt to truncate a symlink */ |
1146 | if (S_ISLNK(inode->i_mode)) | |
1147 | attr->ia_valid &= ~ATTR_SIZE; | |
1148 | ||
ccd979bd MF |
1149 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ |
1150 | | ATTR_GID | ATTR_UID | ATTR_MODE) | |
468eedde | 1151 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) |
ccd979bd | 1152 | return 0; |
ccd979bd MF |
1153 | |
1154 | status = inode_change_ok(inode, attr); | |
1155 | if (status) | |
1156 | return status; | |
1157 | ||
12755627 DM |
1158 | if (is_quota_modification(inode, attr)) |
1159 | dquot_initialize(inode); | |
ccd979bd MF |
1160 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; |
1161 | if (size_change) { | |
1162 | status = ocfs2_rw_lock(inode, 1); | |
1163 | if (status < 0) { | |
1164 | mlog_errno(status); | |
1165 | goto bail; | |
1166 | } | |
1167 | } | |
1168 | ||
e63aecb6 | 1169 | status = ocfs2_inode_lock(inode, &bh, 1); |
ccd979bd MF |
1170 | if (status < 0) { |
1171 | if (status != -ENOENT) | |
1172 | mlog_errno(status); | |
1173 | goto bail_unlock_rw; | |
1174 | } | |
1175 | ||
d62e74be | 1176 | if (size_change) { |
5051f768 WW |
1177 | status = inode_newsize_ok(inode, attr->ia_size); |
1178 | if (status) | |
ce76fd30 | 1179 | goto bail_unlock; |
ce76fd30 | 1180 | |
562c72aa CH |
1181 | inode_dio_wait(inode); |
1182 | ||
d62e74be | 1183 | if (i_size_read(inode) >= attr->ia_size) { |
2b4e30fb JB |
1184 | if (ocfs2_should_order_data(inode)) { |
1185 | status = ocfs2_begin_ordered_truncate(inode, | |
1186 | attr->ia_size); | |
1187 | if (status) | |
1188 | goto bail_unlock; | |
1189 | } | |
ccd979bd | 1190 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); |
2b4e30fb | 1191 | } else |
65ed39d6 | 1192 | status = ocfs2_extend_file(inode, bh, attr->ia_size); |
ccd979bd MF |
1193 | if (status < 0) { |
1194 | if (status != -ENOSPC) | |
1195 | mlog_errno(status); | |
1196 | status = -ENOSPC; | |
1197 | goto bail_unlock; | |
1198 | } | |
1199 | } | |
1200 | ||
488c8ef0 EB |
1201 | if ((attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || |
1202 | (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { | |
65bac575 JK |
1203 | /* |
1204 | * Gather pointers to quota structures so that allocation / | |
1205 | * freeing of quota structures happens here and not inside | |
b43fa828 | 1206 | * dquot_transfer() where we have problems with lock ordering |
65bac575 | 1207 | */ |
488c8ef0 | 1208 | if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid) |
a90714c1 JK |
1209 | && OCFS2_HAS_RO_COMPAT_FEATURE(sb, |
1210 | OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) { | |
aca645a6 | 1211 | transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid)); |
52a9ee28 | 1212 | if (!transfer_to[USRQUOTA]) { |
65bac575 | 1213 | status = -ESRCH; |
a90714c1 | 1214 | goto bail_unlock; |
65bac575 | 1215 | } |
a90714c1 | 1216 | } |
488c8ef0 | 1217 | if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid) |
a90714c1 JK |
1218 | && OCFS2_HAS_RO_COMPAT_FEATURE(sb, |
1219 | OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) { | |
aca645a6 | 1220 | transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid)); |
52a9ee28 | 1221 | if (!transfer_to[GRPQUOTA]) { |
65bac575 | 1222 | status = -ESRCH; |
a90714c1 | 1223 | goto bail_unlock; |
65bac575 | 1224 | } |
a90714c1 | 1225 | } |
65bac575 JK |
1226 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS + |
1227 | 2 * ocfs2_quota_trans_credits(sb)); | |
a90714c1 JK |
1228 | if (IS_ERR(handle)) { |
1229 | status = PTR_ERR(handle); | |
1230 | mlog_errno(status); | |
1231 | goto bail_unlock; | |
1232 | } | |
52a9ee28 | 1233 | status = __dquot_transfer(inode, transfer_to); |
a90714c1 JK |
1234 | if (status < 0) |
1235 | goto bail_commit; | |
1236 | } else { | |
1237 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
1238 | if (IS_ERR(handle)) { | |
1239 | status = PTR_ERR(handle); | |
1240 | mlog_errno(status); | |
1241 | goto bail_unlock; | |
1242 | } | |
ccd979bd MF |
1243 | } |
1244 | ||
1025774c CH |
1245 | setattr_copy(inode, attr); |
1246 | mark_inode_dirty(inode); | |
1247 | ||
ccd979bd MF |
1248 | status = ocfs2_mark_inode_dirty(handle, inode, bh); |
1249 | if (status < 0) | |
1250 | mlog_errno(status); | |
1251 | ||
1252 | bail_commit: | |
02dc1af4 | 1253 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 1254 | bail_unlock: |
e63aecb6 | 1255 | ocfs2_inode_unlock(inode, 1); |
ccd979bd MF |
1256 | bail_unlock_rw: |
1257 | if (size_change) | |
1258 | ocfs2_rw_unlock(inode, 1); | |
1259 | bail: | |
a81cb88b | 1260 | brelse(bh); |
ccd979bd | 1261 | |
65bac575 | 1262 | /* Release quota pointers in case we acquired them */ |
52362810 | 1263 | for (qtype = 0; qtype < OCFS2_MAXQUOTAS; qtype++) |
65bac575 | 1264 | dqput(transfer_to[qtype]); |
65bac575 | 1265 | |
060bc66d | 1266 | if (!status && attr->ia_valid & ATTR_MODE) { |
702e5bc6 | 1267 | status = posix_acl_chmod(inode, inode->i_mode); |
060bc66d TY |
1268 | if (status < 0) |
1269 | mlog_errno(status); | |
1270 | } | |
1271 | ||
ccd979bd MF |
1272 | return status; |
1273 | } | |
1274 | ||
1275 | int ocfs2_getattr(struct vfsmount *mnt, | |
1276 | struct dentry *dentry, | |
1277 | struct kstat *stat) | |
1278 | { | |
2b0143b5 DH |
1279 | struct inode *inode = d_inode(dentry); |
1280 | struct super_block *sb = d_inode(dentry)->i_sb; | |
ccd979bd MF |
1281 | struct ocfs2_super *osb = sb->s_fs_info; |
1282 | int err; | |
1283 | ||
ccd979bd MF |
1284 | err = ocfs2_inode_revalidate(dentry); |
1285 | if (err) { | |
1286 | if (err != -ENOENT) | |
1287 | mlog_errno(err); | |
1288 | goto bail; | |
1289 | } | |
1290 | ||
1291 | generic_fillattr(inode, stat); | |
1292 | ||
1293 | /* We set the blksize from the cluster size for performance */ | |
1294 | stat->blksize = osb->s_clustersize; | |
1295 | ||
1296 | bail: | |
ccd979bd MF |
1297 | return err; |
1298 | } | |
1299 | ||
10556cb2 | 1300 | int ocfs2_permission(struct inode *inode, int mask) |
d38eb8db TY |
1301 | { |
1302 | int ret; | |
1303 | ||
10556cb2 | 1304 | if (mask & MAY_NOT_BLOCK) |
b74c79e9 NP |
1305 | return -ECHILD; |
1306 | ||
e63aecb6 | 1307 | ret = ocfs2_inode_lock(inode, NULL, 0); |
d38eb8db | 1308 | if (ret) { |
a9f5f707 MF |
1309 | if (ret != -ENOENT) |
1310 | mlog_errno(ret); | |
d38eb8db TY |
1311 | goto out; |
1312 | } | |
1313 | ||
2830ba7f | 1314 | ret = generic_permission(inode, mask); |
d38eb8db | 1315 | |
e63aecb6 | 1316 | ocfs2_inode_unlock(inode, 0); |
d38eb8db | 1317 | out: |
d38eb8db TY |
1318 | return ret; |
1319 | } | |
1320 | ||
b2580103 MF |
1321 | static int __ocfs2_write_remove_suid(struct inode *inode, |
1322 | struct buffer_head *bh) | |
ccd979bd MF |
1323 | { |
1324 | int ret; | |
1fabe148 | 1325 | handle_t *handle; |
ccd979bd MF |
1326 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
1327 | struct ocfs2_dinode *di; | |
1328 | ||
468eedde TM |
1329 | trace_ocfs2_write_remove_suid( |
1330 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1331 | inode->i_mode); | |
ccd979bd | 1332 | |
65eff9cc | 1333 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
fa38e92c JK |
1334 | if (IS_ERR(handle)) { |
1335 | ret = PTR_ERR(handle); | |
ccd979bd MF |
1336 | mlog_errno(ret); |
1337 | goto out; | |
1338 | } | |
1339 | ||
0cf2f763 | 1340 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh, |
13723d00 | 1341 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
1342 | if (ret < 0) { |
1343 | mlog_errno(ret); | |
b2580103 | 1344 | goto out_trans; |
ccd979bd MF |
1345 | } |
1346 | ||
1347 | inode->i_mode &= ~S_ISUID; | |
1348 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) | |
1349 | inode->i_mode &= ~S_ISGID; | |
1350 | ||
1351 | di = (struct ocfs2_dinode *) bh->b_data; | |
1352 | di->i_mode = cpu_to_le16(inode->i_mode); | |
6fdb702d | 1353 | ocfs2_update_inode_fsync_trans(handle, inode, 0); |
ccd979bd | 1354 | |
ec20cec7 | 1355 | ocfs2_journal_dirty(handle, bh); |
b2580103 | 1356 | |
ccd979bd | 1357 | out_trans: |
02dc1af4 | 1358 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 1359 | out: |
ccd979bd MF |
1360 | return ret; |
1361 | } | |
1362 | ||
9517bac6 MF |
1363 | /* |
1364 | * Will look for holes and unwritten extents in the range starting at | |
1365 | * pos for count bytes (inclusive). | |
1366 | */ | |
1367 | static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos, | |
1368 | size_t count) | |
1369 | { | |
1370 | int ret = 0; | |
49cb8d2d | 1371 | unsigned int extent_flags; |
9517bac6 MF |
1372 | u32 cpos, clusters, extent_len, phys_cpos; |
1373 | struct super_block *sb = inode->i_sb; | |
1374 | ||
1375 | cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits; | |
1376 | clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos; | |
1377 | ||
1378 | while (clusters) { | |
49cb8d2d MF |
1379 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len, |
1380 | &extent_flags); | |
9517bac6 MF |
1381 | if (ret < 0) { |
1382 | mlog_errno(ret); | |
1383 | goto out; | |
1384 | } | |
1385 | ||
49cb8d2d | 1386 | if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) { |
9517bac6 MF |
1387 | ret = 1; |
1388 | break; | |
1389 | } | |
1390 | ||
1391 | if (extent_len > clusters) | |
1392 | extent_len = clusters; | |
1393 | ||
1394 | clusters -= extent_len; | |
1395 | cpos += extent_len; | |
1396 | } | |
1397 | out: | |
1398 | return ret; | |
1399 | } | |
1400 | ||
b2580103 MF |
1401 | static int ocfs2_write_remove_suid(struct inode *inode) |
1402 | { | |
1403 | int ret; | |
1404 | struct buffer_head *bh = NULL; | |
b2580103 | 1405 | |
b657c95c | 1406 | ret = ocfs2_read_inode_block(inode, &bh); |
b2580103 MF |
1407 | if (ret < 0) { |
1408 | mlog_errno(ret); | |
1409 | goto out; | |
1410 | } | |
1411 | ||
1412 | ret = __ocfs2_write_remove_suid(inode, bh); | |
1413 | out: | |
1414 | brelse(bh); | |
1415 | return ret; | |
1416 | } | |
1417 | ||
2ae99a60 MF |
1418 | /* |
1419 | * Allocate enough extents to cover the region starting at byte offset | |
1420 | * start for len bytes. Existing extents are skipped, any extents | |
1421 | * added are marked as "unwritten". | |
1422 | */ | |
1423 | static int ocfs2_allocate_unwritten_extents(struct inode *inode, | |
1424 | u64 start, u64 len) | |
1425 | { | |
1426 | int ret; | |
1427 | u32 cpos, phys_cpos, clusters, alloc_size; | |
1afc32b9 MF |
1428 | u64 end = start + len; |
1429 | struct buffer_head *di_bh = NULL; | |
1430 | ||
1431 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | |
b657c95c | 1432 | ret = ocfs2_read_inode_block(inode, &di_bh); |
1afc32b9 MF |
1433 | if (ret) { |
1434 | mlog_errno(ret); | |
1435 | goto out; | |
1436 | } | |
1437 | ||
1438 | /* | |
1439 | * Nothing to do if the requested reservation range | |
1440 | * fits within the inode. | |
1441 | */ | |
1442 | if (ocfs2_size_fits_inline_data(di_bh, end)) | |
1443 | goto out; | |
1444 | ||
1445 | ret = ocfs2_convert_inline_data_to_extents(inode, di_bh); | |
1446 | if (ret) { | |
1447 | mlog_errno(ret); | |
1448 | goto out; | |
1449 | } | |
1450 | } | |
2ae99a60 MF |
1451 | |
1452 | /* | |
1453 | * We consider both start and len to be inclusive. | |
1454 | */ | |
1455 | cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits; | |
1456 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len); | |
1457 | clusters -= cpos; | |
1458 | ||
1459 | while (clusters) { | |
1460 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, | |
1461 | &alloc_size, NULL); | |
1462 | if (ret) { | |
1463 | mlog_errno(ret); | |
1464 | goto out; | |
1465 | } | |
1466 | ||
1467 | /* | |
1468 | * Hole or existing extent len can be arbitrary, so | |
1469 | * cap it to our own allocation request. | |
1470 | */ | |
1471 | if (alloc_size > clusters) | |
1472 | alloc_size = clusters; | |
1473 | ||
1474 | if (phys_cpos) { | |
1475 | /* | |
1476 | * We already have an allocation at this | |
1477 | * region so we can safely skip it. | |
1478 | */ | |
1479 | goto next; | |
1480 | } | |
1481 | ||
1482 | ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1); | |
1483 | if (ret) { | |
1484 | if (ret != -ENOSPC) | |
1485 | mlog_errno(ret); | |
1486 | goto out; | |
1487 | } | |
1488 | ||
1489 | next: | |
1490 | cpos += alloc_size; | |
1491 | clusters -= alloc_size; | |
1492 | } | |
1493 | ||
1494 | ret = 0; | |
1495 | out: | |
1afc32b9 MF |
1496 | |
1497 | brelse(di_bh); | |
2ae99a60 MF |
1498 | return ret; |
1499 | } | |
1500 | ||
063c4561 MF |
1501 | /* |
1502 | * Truncate a byte range, avoiding pages within partial clusters. This | |
1503 | * preserves those pages for the zeroing code to write to. | |
1504 | */ | |
1505 | static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start, | |
1506 | u64 byte_len) | |
1507 | { | |
1508 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1509 | loff_t start, end; | |
1510 | struct address_space *mapping = inode->i_mapping; | |
1511 | ||
1512 | start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start); | |
1513 | end = byte_start + byte_len; | |
1514 | end = end & ~(osb->s_clustersize - 1); | |
1515 | ||
1516 | if (start < end) { | |
1517 | unmap_mapping_range(mapping, start, end - start, 0); | |
1518 | truncate_inode_pages_range(mapping, start, end - 1); | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | static int ocfs2_zero_partial_clusters(struct inode *inode, | |
1523 | u64 start, u64 len) | |
1524 | { | |
1525 | int ret = 0; | |
1526 | u64 tmpend, end = start + len; | |
1527 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1528 | unsigned int csize = osb->s_clustersize; | |
1529 | handle_t *handle; | |
1530 | ||
1531 | /* | |
1532 | * The "start" and "end" values are NOT necessarily part of | |
1533 | * the range whose allocation is being deleted. Rather, this | |
1534 | * is what the user passed in with the request. We must zero | |
1535 | * partial clusters here. There's no need to worry about | |
1536 | * physical allocation - the zeroing code knows to skip holes. | |
1537 | */ | |
468eedde TM |
1538 | trace_ocfs2_zero_partial_clusters( |
1539 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1540 | (unsigned long long)start, (unsigned long long)end); | |
063c4561 MF |
1541 | |
1542 | /* | |
1543 | * If both edges are on a cluster boundary then there's no | |
1544 | * zeroing required as the region is part of the allocation to | |
1545 | * be truncated. | |
1546 | */ | |
1547 | if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0) | |
1548 | goto out; | |
1549 | ||
1550 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
fa38e92c JK |
1551 | if (IS_ERR(handle)) { |
1552 | ret = PTR_ERR(handle); | |
063c4561 MF |
1553 | mlog_errno(ret); |
1554 | goto out; | |
1555 | } | |
1556 | ||
1557 | /* | |
1558 | * We want to get the byte offset of the end of the 1st cluster. | |
1559 | */ | |
1560 | tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1)); | |
1561 | if (tmpend > end) | |
1562 | tmpend = end; | |
1563 | ||
468eedde TM |
1564 | trace_ocfs2_zero_partial_clusters_range1((unsigned long long)start, |
1565 | (unsigned long long)tmpend); | |
063c4561 MF |
1566 | |
1567 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend); | |
1568 | if (ret) | |
1569 | mlog_errno(ret); | |
1570 | ||
1571 | if (tmpend < end) { | |
1572 | /* | |
1573 | * This may make start and end equal, but the zeroing | |
1574 | * code will skip any work in that case so there's no | |
1575 | * need to catch it up here. | |
1576 | */ | |
1577 | start = end & ~(osb->s_clustersize - 1); | |
1578 | ||
468eedde TM |
1579 | trace_ocfs2_zero_partial_clusters_range2( |
1580 | (unsigned long long)start, (unsigned long long)end); | |
063c4561 MF |
1581 | |
1582 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, end); | |
1583 | if (ret) | |
1584 | mlog_errno(ret); | |
1585 | } | |
6fdb702d | 1586 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
063c4561 MF |
1587 | |
1588 | ocfs2_commit_trans(osb, handle); | |
1589 | out: | |
1590 | return ret; | |
1591 | } | |
1592 | ||
c1631d4a TY |
1593 | static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos) |
1594 | { | |
1595 | int i; | |
1596 | struct ocfs2_extent_rec *rec = NULL; | |
1597 | ||
1598 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { | |
1599 | ||
1600 | rec = &el->l_recs[i]; | |
1601 | ||
1602 | if (le32_to_cpu(rec->e_cpos) < pos) | |
1603 | break; | |
1604 | } | |
1605 | ||
1606 | return i; | |
1607 | } | |
1608 | ||
1609 | /* | |
1610 | * Helper to calculate the punching pos and length in one run, we handle the | |
1611 | * following three cases in order: | |
1612 | * | |
1613 | * - remove the entire record | |
1614 | * - remove a partial record | |
1615 | * - no record needs to be removed (hole-punching completed) | |
1616 | */ | |
1617 | static void ocfs2_calc_trunc_pos(struct inode *inode, | |
1618 | struct ocfs2_extent_list *el, | |
1619 | struct ocfs2_extent_rec *rec, | |
1620 | u32 trunc_start, u32 *trunc_cpos, | |
1621 | u32 *trunc_len, u32 *trunc_end, | |
1622 | u64 *blkno, int *done) | |
1623 | { | |
1624 | int ret = 0; | |
1625 | u32 coff, range; | |
1626 | ||
1627 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); | |
1628 | ||
1629 | if (le32_to_cpu(rec->e_cpos) >= trunc_start) { | |
9a790ba1 TY |
1630 | /* |
1631 | * remove an entire extent record. | |
1632 | */ | |
c1631d4a TY |
1633 | *trunc_cpos = le32_to_cpu(rec->e_cpos); |
1634 | /* | |
1635 | * Skip holes if any. | |
1636 | */ | |
1637 | if (range < *trunc_end) | |
1638 | *trunc_end = range; | |
1639 | *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos); | |
1640 | *blkno = le64_to_cpu(rec->e_blkno); | |
1641 | *trunc_end = le32_to_cpu(rec->e_cpos); | |
1642 | } else if (range > trunc_start) { | |
9a790ba1 TY |
1643 | /* |
1644 | * remove a partial extent record, which means we're | |
1645 | * removing the last extent record. | |
1646 | */ | |
c1631d4a | 1647 | *trunc_cpos = trunc_start; |
9a790ba1 TY |
1648 | /* |
1649 | * skip hole if any. | |
1650 | */ | |
1651 | if (range < *trunc_end) | |
1652 | *trunc_end = range; | |
c1631d4a TY |
1653 | *trunc_len = *trunc_end - trunc_start; |
1654 | coff = trunc_start - le32_to_cpu(rec->e_cpos); | |
1655 | *blkno = le64_to_cpu(rec->e_blkno) + | |
1656 | ocfs2_clusters_to_blocks(inode->i_sb, coff); | |
1657 | *trunc_end = trunc_start; | |
1658 | } else { | |
1659 | /* | |
1660 | * It may have two following possibilities: | |
1661 | * | |
1662 | * - last record has been removed | |
1663 | * - trunc_start was within a hole | |
1664 | * | |
1665 | * both two cases mean the completion of hole punching. | |
1666 | */ | |
1667 | ret = 1; | |
1668 | } | |
1669 | ||
1670 | *done = ret; | |
1671 | } | |
1672 | ||
063c4561 MF |
1673 | static int ocfs2_remove_inode_range(struct inode *inode, |
1674 | struct buffer_head *di_bh, u64 byte_start, | |
1675 | u64 byte_len) | |
1676 | { | |
c1631d4a TY |
1677 | int ret = 0, flags = 0, done = 0, i; |
1678 | u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos; | |
1679 | u32 cluster_in_el; | |
063c4561 MF |
1680 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
1681 | struct ocfs2_cached_dealloc_ctxt dealloc; | |
b1967d0e | 1682 | struct address_space *mapping = inode->i_mapping; |
fecc0112 | 1683 | struct ocfs2_extent_tree et; |
c1631d4a TY |
1684 | struct ocfs2_path *path = NULL; |
1685 | struct ocfs2_extent_list *el = NULL; | |
1686 | struct ocfs2_extent_rec *rec = NULL; | |
e8aec068 | 1687 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
c1631d4a | 1688 | u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc); |
063c4561 | 1689 | |
5e404e9e | 1690 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
063c4561 MF |
1691 | ocfs2_init_dealloc_ctxt(&dealloc); |
1692 | ||
468eedde TM |
1693 | trace_ocfs2_remove_inode_range( |
1694 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1695 | (unsigned long long)byte_start, | |
1696 | (unsigned long long)byte_len); | |
1697 | ||
063c4561 MF |
1698 | if (byte_len == 0) |
1699 | return 0; | |
1700 | ||
1afc32b9 MF |
1701 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
1702 | ret = ocfs2_truncate_inline(inode, di_bh, byte_start, | |
b1967d0e MF |
1703 | byte_start + byte_len, 0); |
1704 | if (ret) { | |
1afc32b9 | 1705 | mlog_errno(ret); |
b1967d0e MF |
1706 | goto out; |
1707 | } | |
1708 | /* | |
1709 | * There's no need to get fancy with the page cache | |
1710 | * truncate of an inline-data inode. We're talking | |
1711 | * about less than a page here, which will be cached | |
1712 | * in the dinode buffer anyway. | |
1713 | */ | |
1714 | unmap_mapping_range(mapping, 0, 0, 0); | |
1715 | truncate_inode_pages(mapping, 0); | |
1716 | goto out; | |
1afc32b9 MF |
1717 | } |
1718 | ||
e8aec068 TY |
1719 | /* |
1720 | * For reflinks, we may need to CoW 2 clusters which might be | |
1721 | * partially zero'd later, if hole's start and end offset were | |
1722 | * within one cluster(means is not exactly aligned to clustersize). | |
1723 | */ | |
1724 | ||
1725 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) { | |
1726 | ||
1727 | ret = ocfs2_cow_file_pos(inode, di_bh, byte_start); | |
1728 | if (ret) { | |
1729 | mlog_errno(ret); | |
1730 | goto out; | |
1731 | } | |
1732 | ||
1733 | ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len); | |
1734 | if (ret) { | |
1735 | mlog_errno(ret); | |
1736 | goto out; | |
1737 | } | |
1738 | } | |
1739 | ||
063c4561 | 1740 | trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start); |
c1631d4a TY |
1741 | trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits; |
1742 | cluster_in_el = trunc_end; | |
063c4561 | 1743 | |
063c4561 MF |
1744 | ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len); |
1745 | if (ret) { | |
1746 | mlog_errno(ret); | |
1747 | goto out; | |
1748 | } | |
1749 | ||
c1631d4a TY |
1750 | path = ocfs2_new_path_from_et(&et); |
1751 | if (!path) { | |
1752 | ret = -ENOMEM; | |
1753 | mlog_errno(ret); | |
1754 | goto out; | |
1755 | } | |
1756 | ||
1757 | while (trunc_end > trunc_start) { | |
1758 | ||
1759 | ret = ocfs2_find_path(INODE_CACHE(inode), path, | |
1760 | cluster_in_el); | |
063c4561 MF |
1761 | if (ret) { |
1762 | mlog_errno(ret); | |
1763 | goto out; | |
1764 | } | |
1765 | ||
c1631d4a | 1766 | el = path_leaf_el(path); |
063c4561 | 1767 | |
c1631d4a TY |
1768 | i = ocfs2_find_rec(el, trunc_end); |
1769 | /* | |
1770 | * Need to go to previous extent block. | |
1771 | */ | |
1772 | if (i < 0) { | |
1773 | if (path->p_tree_depth == 0) | |
1774 | break; | |
063c4561 | 1775 | |
c1631d4a TY |
1776 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, |
1777 | path, | |
1778 | &cluster_in_el); | |
063c4561 MF |
1779 | if (ret) { |
1780 | mlog_errno(ret); | |
1781 | goto out; | |
1782 | } | |
c1631d4a TY |
1783 | |
1784 | /* | |
1785 | * We've reached the leftmost extent block, | |
1786 | * it's safe to leave. | |
1787 | */ | |
1788 | if (cluster_in_el == 0) | |
1789 | break; | |
1790 | ||
1791 | /* | |
1792 | * The 'pos' searched for previous extent block is | |
1793 | * always one cluster less than actual trunc_end. | |
1794 | */ | |
1795 | trunc_end = cluster_in_el + 1; | |
1796 | ||
1797 | ocfs2_reinit_path(path, 1); | |
1798 | ||
1799 | continue; | |
1800 | ||
1801 | } else | |
1802 | rec = &el->l_recs[i]; | |
1803 | ||
1804 | ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos, | |
1805 | &trunc_len, &trunc_end, &blkno, &done); | |
1806 | if (done) | |
1807 | break; | |
1808 | ||
1809 | flags = rec->e_flags; | |
1810 | phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno); | |
1811 | ||
1812 | ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos, | |
1813 | phys_cpos, trunc_len, flags, | |
f62f12b3 | 1814 | &dealloc, refcount_loc, false); |
c1631d4a TY |
1815 | if (ret < 0) { |
1816 | mlog_errno(ret); | |
1817 | goto out; | |
063c4561 MF |
1818 | } |
1819 | ||
c1631d4a TY |
1820 | cluster_in_el = trunc_end; |
1821 | ||
1822 | ocfs2_reinit_path(path, 1); | |
063c4561 MF |
1823 | } |
1824 | ||
1825 | ocfs2_truncate_cluster_pages(inode, byte_start, byte_len); | |
1826 | ||
1827 | out: | |
7aebff18 | 1828 | ocfs2_free_path(path); |
063c4561 MF |
1829 | ocfs2_schedule_truncate_log_flush(osb, 1); |
1830 | ocfs2_run_deallocs(osb, &dealloc); | |
1831 | ||
1832 | return ret; | |
1833 | } | |
1834 | ||
b2580103 MF |
1835 | /* |
1836 | * Parts of this function taken from xfs_change_file_space() | |
1837 | */ | |
385820a3 MF |
1838 | static int __ocfs2_change_file_space(struct file *file, struct inode *inode, |
1839 | loff_t f_pos, unsigned int cmd, | |
1840 | struct ocfs2_space_resv *sr, | |
1841 | int change_size) | |
b2580103 MF |
1842 | { |
1843 | int ret; | |
1844 | s64 llen; | |
385820a3 | 1845 | loff_t size; |
b2580103 MF |
1846 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
1847 | struct buffer_head *di_bh = NULL; | |
1848 | handle_t *handle; | |
a00cce35 | 1849 | unsigned long long max_off = inode->i_sb->s_maxbytes; |
b2580103 | 1850 | |
b2580103 MF |
1851 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
1852 | return -EROFS; | |
1853 | ||
1854 | mutex_lock(&inode->i_mutex); | |
1855 | ||
1856 | /* | |
1857 | * This prevents concurrent writes on other nodes | |
1858 | */ | |
1859 | ret = ocfs2_rw_lock(inode, 1); | |
1860 | if (ret) { | |
1861 | mlog_errno(ret); | |
1862 | goto out; | |
1863 | } | |
1864 | ||
e63aecb6 | 1865 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
b2580103 MF |
1866 | if (ret) { |
1867 | mlog_errno(ret); | |
1868 | goto out_rw_unlock; | |
1869 | } | |
1870 | ||
1871 | if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) { | |
1872 | ret = -EPERM; | |
e63aecb6 | 1873 | goto out_inode_unlock; |
b2580103 MF |
1874 | } |
1875 | ||
1876 | switch (sr->l_whence) { | |
1877 | case 0: /*SEEK_SET*/ | |
1878 | break; | |
1879 | case 1: /*SEEK_CUR*/ | |
385820a3 | 1880 | sr->l_start += f_pos; |
b2580103 MF |
1881 | break; |
1882 | case 2: /*SEEK_END*/ | |
1883 | sr->l_start += i_size_read(inode); | |
1884 | break; | |
1885 | default: | |
1886 | ret = -EINVAL; | |
e63aecb6 | 1887 | goto out_inode_unlock; |
b2580103 MF |
1888 | } |
1889 | sr->l_whence = 0; | |
1890 | ||
1891 | llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len; | |
1892 | ||
1893 | if (sr->l_start < 0 | |
1894 | || sr->l_start > max_off | |
1895 | || (sr->l_start + llen) < 0 | |
1896 | || (sr->l_start + llen) > max_off) { | |
1897 | ret = -EINVAL; | |
e63aecb6 | 1898 | goto out_inode_unlock; |
b2580103 | 1899 | } |
385820a3 | 1900 | size = sr->l_start + sr->l_len; |
b2580103 | 1901 | |
a2a3b398 TS |
1902 | if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 || |
1903 | cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) { | |
b2580103 MF |
1904 | if (sr->l_len <= 0) { |
1905 | ret = -EINVAL; | |
e63aecb6 | 1906 | goto out_inode_unlock; |
b2580103 MF |
1907 | } |
1908 | } | |
1909 | ||
385820a3 | 1910 | if (file && should_remove_suid(file->f_path.dentry)) { |
b2580103 MF |
1911 | ret = __ocfs2_write_remove_suid(inode, di_bh); |
1912 | if (ret) { | |
1913 | mlog_errno(ret); | |
e63aecb6 | 1914 | goto out_inode_unlock; |
b2580103 MF |
1915 | } |
1916 | } | |
1917 | ||
1918 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1919 | switch (cmd) { | |
1920 | case OCFS2_IOC_RESVSP: | |
1921 | case OCFS2_IOC_RESVSP64: | |
1922 | /* | |
1923 | * This takes unsigned offsets, but the signed ones we | |
1924 | * pass have been checked against overflow above. | |
1925 | */ | |
1926 | ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start, | |
1927 | sr->l_len); | |
1928 | break; | |
1929 | case OCFS2_IOC_UNRESVSP: | |
1930 | case OCFS2_IOC_UNRESVSP64: | |
1931 | ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start, | |
1932 | sr->l_len); | |
1933 | break; | |
1934 | default: | |
1935 | ret = -EINVAL; | |
1936 | } | |
1937 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1938 | if (ret) { | |
1939 | mlog_errno(ret); | |
e63aecb6 | 1940 | goto out_inode_unlock; |
b2580103 MF |
1941 | } |
1942 | ||
1943 | /* | |
1944 | * We update c/mtime for these changes | |
1945 | */ | |
1946 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
1947 | if (IS_ERR(handle)) { | |
1948 | ret = PTR_ERR(handle); | |
1949 | mlog_errno(ret); | |
e63aecb6 | 1950 | goto out_inode_unlock; |
b2580103 MF |
1951 | } |
1952 | ||
385820a3 MF |
1953 | if (change_size && i_size_read(inode) < size) |
1954 | i_size_write(inode, size); | |
1955 | ||
b2580103 MF |
1956 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
1957 | ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); | |
1958 | if (ret < 0) | |
1959 | mlog_errno(ret); | |
1960 | ||
a4e08d00 | 1961 | if (file && (file->f_flags & O_SYNC)) |
df295d4a MF |
1962 | handle->h_sync = 1; |
1963 | ||
b2580103 MF |
1964 | ocfs2_commit_trans(osb, handle); |
1965 | ||
e63aecb6 | 1966 | out_inode_unlock: |
b2580103 | 1967 | brelse(di_bh); |
e63aecb6 | 1968 | ocfs2_inode_unlock(inode, 1); |
b2580103 MF |
1969 | out_rw_unlock: |
1970 | ocfs2_rw_unlock(inode, 1); | |
1971 | ||
b2580103 | 1972 | out: |
c259ae52 | 1973 | mutex_unlock(&inode->i_mutex); |
b2580103 MF |
1974 | return ret; |
1975 | } | |
1976 | ||
385820a3 MF |
1977 | int ocfs2_change_file_space(struct file *file, unsigned int cmd, |
1978 | struct ocfs2_space_resv *sr) | |
1979 | { | |
496ad9aa | 1980 | struct inode *inode = file_inode(file); |
c19a28e1 | 1981 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
fef6925c | 1982 | int ret; |
385820a3 MF |
1983 | |
1984 | if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) && | |
1985 | !ocfs2_writes_unwritten_extents(osb)) | |
1986 | return -ENOTTY; | |
1987 | else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) && | |
1988 | !ocfs2_sparse_alloc(osb)) | |
1989 | return -ENOTTY; | |
1990 | ||
1991 | if (!S_ISREG(inode->i_mode)) | |
1992 | return -EINVAL; | |
1993 | ||
1994 | if (!(file->f_mode & FMODE_WRITE)) | |
1995 | return -EBADF; | |
1996 | ||
fef6925c JK |
1997 | ret = mnt_want_write_file(file); |
1998 | if (ret) | |
1999 | return ret; | |
2000 | ret = __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0); | |
2001 | mnt_drop_write_file(file); | |
2002 | return ret; | |
385820a3 MF |
2003 | } |
2004 | ||
2fe17c10 | 2005 | static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, |
385820a3 MF |
2006 | loff_t len) |
2007 | { | |
496ad9aa | 2008 | struct inode *inode = file_inode(file); |
385820a3 MF |
2009 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
2010 | struct ocfs2_space_resv sr; | |
2011 | int change_size = 1; | |
db47fef2 | 2012 | int cmd = OCFS2_IOC_RESVSP64; |
385820a3 | 2013 | |
64c23e86 CH |
2014 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
2015 | return -EOPNOTSUPP; | |
385820a3 MF |
2016 | if (!ocfs2_writes_unwritten_extents(osb)) |
2017 | return -EOPNOTSUPP; | |
2018 | ||
385820a3 MF |
2019 | if (mode & FALLOC_FL_KEEP_SIZE) |
2020 | change_size = 0; | |
2021 | ||
db47fef2 JB |
2022 | if (mode & FALLOC_FL_PUNCH_HOLE) |
2023 | cmd = OCFS2_IOC_UNRESVSP64; | |
2024 | ||
385820a3 MF |
2025 | sr.l_whence = 0; |
2026 | sr.l_start = (s64)offset; | |
2027 | sr.l_len = (s64)len; | |
2028 | ||
db47fef2 JB |
2029 | return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr, |
2030 | change_size); | |
385820a3 MF |
2031 | } |
2032 | ||
293b2f70 TM |
2033 | int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos, |
2034 | size_t count) | |
2035 | { | |
2036 | int ret = 0; | |
2037 | unsigned int extent_flags; | |
2038 | u32 cpos, clusters, extent_len, phys_cpos; | |
2039 | struct super_block *sb = inode->i_sb; | |
2040 | ||
2041 | if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) || | |
2f48d593 TM |
2042 | !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) || |
2043 | OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
293b2f70 TM |
2044 | return 0; |
2045 | ||
2046 | cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits; | |
2047 | clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos; | |
2048 | ||
2049 | while (clusters) { | |
2050 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len, | |
2051 | &extent_flags); | |
2052 | if (ret < 0) { | |
2053 | mlog_errno(ret); | |
2054 | goto out; | |
2055 | } | |
2056 | ||
2057 | if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) { | |
2058 | ret = 1; | |
2059 | break; | |
2060 | } | |
2061 | ||
2062 | if (extent_len > clusters) | |
2063 | extent_len = clusters; | |
2064 | ||
2065 | clusters -= extent_len; | |
2066 | cpos += extent_len; | |
2067 | } | |
2068 | out: | |
2069 | return ret; | |
2070 | } | |
2071 | ||
a11f7e63 MF |
2072 | static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos) |
2073 | { | |
2074 | int blockmask = inode->i_sb->s_blocksize - 1; | |
2075 | loff_t final_size = pos + count; | |
2076 | ||
2077 | if ((pos & blockmask) || (final_size & blockmask)) | |
2078 | return 1; | |
2079 | return 0; | |
2080 | } | |
2081 | ||
293b2f70 | 2082 | static int ocfs2_prepare_inode_for_refcount(struct inode *inode, |
b8908236 | 2083 | struct file *file, |
293b2f70 TM |
2084 | loff_t pos, size_t count, |
2085 | int *meta_level) | |
2086 | { | |
2087 | int ret; | |
2088 | struct buffer_head *di_bh = NULL; | |
2089 | u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits; | |
2090 | u32 clusters = | |
2091 | ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos; | |
2092 | ||
2093 | ret = ocfs2_inode_lock(inode, &di_bh, 1); | |
2094 | if (ret) { | |
2095 | mlog_errno(ret); | |
2096 | goto out; | |
2097 | } | |
2098 | ||
2099 | *meta_level = 1; | |
2100 | ||
c7dd3392 | 2101 | ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX); |
293b2f70 TM |
2102 | if (ret) |
2103 | mlog_errno(ret); | |
2104 | out: | |
2105 | brelse(di_bh); | |
2106 | return ret; | |
2107 | } | |
2108 | ||
b8908236 | 2109 | static int ocfs2_prepare_inode_for_write(struct file *file, |
90320251 | 2110 | loff_t pos, |
8659ac25 | 2111 | size_t count, |
9517bac6 | 2112 | int appending, |
86470e98 TM |
2113 | int *direct_io, |
2114 | int *has_refcount) | |
ccd979bd | 2115 | { |
65ed39d6 | 2116 | int ret = 0, meta_level = 0; |
b8908236 | 2117 | struct dentry *dentry = file->f_path.dentry; |
2b0143b5 | 2118 | struct inode *inode = d_inode(dentry); |
90320251 | 2119 | loff_t end; |
d943d59d JQ |
2120 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
2121 | int full_coherency = !(osb->s_mount_opt & | |
2122 | OCFS2_MOUNT_COHERENCY_BUFFERED); | |
ccd979bd | 2123 | |
2bd63216 | 2124 | /* |
65ed39d6 MF |
2125 | * We start with a read level meta lock and only jump to an ex |
2126 | * if we need to make modifications here. | |
ccd979bd | 2127 | */ |
ccd979bd | 2128 | for(;;) { |
e63aecb6 | 2129 | ret = ocfs2_inode_lock(inode, NULL, meta_level); |
ccd979bd MF |
2130 | if (ret < 0) { |
2131 | meta_level = -1; | |
2132 | mlog_errno(ret); | |
2133 | goto out; | |
2134 | } | |
2135 | ||
2136 | /* Clear suid / sgid if necessary. We do this here | |
2137 | * instead of later in the write path because | |
2138 | * remove_suid() calls ->setattr without any hint that | |
2139 | * we may have already done our cluster locking. Since | |
2140 | * ocfs2_setattr() *must* take cluster locks to | |
42b2aa86 | 2141 | * proceed, this will lead us to recursively lock the |
ccd979bd MF |
2142 | * inode. There's also the dinode i_size state which |
2143 | * can be lost via setattr during extending writes (we | |
2144 | * set inode->i_size at the end of a write. */ | |
8659ac25 | 2145 | if (should_remove_suid(dentry)) { |
ccd979bd | 2146 | if (meta_level == 0) { |
e63aecb6 | 2147 | ocfs2_inode_unlock(inode, meta_level); |
ccd979bd MF |
2148 | meta_level = 1; |
2149 | continue; | |
2150 | } | |
2151 | ||
2152 | ret = ocfs2_write_remove_suid(inode); | |
2153 | if (ret < 0) { | |
2154 | mlog_errno(ret); | |
8659ac25 | 2155 | goto out_unlock; |
ccd979bd MF |
2156 | } |
2157 | } | |
2158 | ||
90320251 | 2159 | end = pos + count; |
9517bac6 | 2160 | |
90320251 | 2161 | ret = ocfs2_check_range_for_refcount(inode, pos, count); |
293b2f70 TM |
2162 | if (ret == 1) { |
2163 | ocfs2_inode_unlock(inode, meta_level); | |
2164 | meta_level = -1; | |
2165 | ||
2166 | ret = ocfs2_prepare_inode_for_refcount(inode, | |
b8908236 | 2167 | file, |
90320251 | 2168 | pos, |
293b2f70 TM |
2169 | count, |
2170 | &meta_level); | |
86470e98 TM |
2171 | if (has_refcount) |
2172 | *has_refcount = 1; | |
96a1cc73 WW |
2173 | if (direct_io) |
2174 | *direct_io = 0; | |
293b2f70 TM |
2175 | } |
2176 | ||
2177 | if (ret < 0) { | |
2178 | mlog_errno(ret); | |
2179 | goto out_unlock; | |
2180 | } | |
2181 | ||
65ed39d6 MF |
2182 | /* |
2183 | * Skip the O_DIRECT checks if we don't need | |
2184 | * them. | |
2185 | */ | |
2186 | if (!direct_io || !(*direct_io)) | |
9517bac6 | 2187 | break; |
9517bac6 | 2188 | |
1afc32b9 MF |
2189 | /* |
2190 | * There's no sane way to do direct writes to an inode | |
2191 | * with inline data. | |
2192 | */ | |
2193 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | |
2194 | *direct_io = 0; | |
2195 | break; | |
2196 | } | |
2197 | ||
3a0782d0 | 2198 | /* |
65ed39d6 MF |
2199 | * Allowing concurrent direct writes means |
2200 | * i_size changes wouldn't be synchronized, so | |
2201 | * one node could wind up truncating another | |
2202 | * nodes writes. | |
3a0782d0 | 2203 | */ |
d943d59d | 2204 | if (end > i_size_read(inode) && !full_coherency) { |
65ed39d6 | 2205 | *direct_io = 0; |
ccd979bd | 2206 | break; |
ccd979bd MF |
2207 | } |
2208 | ||
160cc266 JQ |
2209 | /* |
2210 | * Fallback to old way if the feature bit is not set. | |
2211 | */ | |
2212 | if (end > i_size_read(inode) && | |
2213 | !ocfs2_supports_append_dio(osb)) { | |
2214 | *direct_io = 0; | |
2215 | break; | |
2216 | } | |
2217 | ||
65ed39d6 MF |
2218 | /* |
2219 | * We don't fill holes during direct io, so | |
2220 | * check for them here. If any are found, the | |
2221 | * caller will have to retake some cluster | |
2222 | * locks and initiate the io as buffered. | |
2223 | */ | |
90320251 | 2224 | ret = ocfs2_check_range_for_holes(inode, pos, count); |
65ed39d6 | 2225 | if (ret == 1) { |
160cc266 JQ |
2226 | /* |
2227 | * Fallback to old way if the feature bit is not set. | |
2228 | * Otherwise try dio first and then complete the rest | |
2229 | * request through buffer io. | |
2230 | */ | |
2231 | if (!ocfs2_supports_append_dio(osb)) | |
2232 | *direct_io = 0; | |
65ed39d6 MF |
2233 | ret = 0; |
2234 | } else if (ret < 0) | |
2235 | mlog_errno(ret); | |
ccd979bd MF |
2236 | break; |
2237 | } | |
2238 | ||
8659ac25 | 2239 | out_unlock: |
468eedde | 2240 | trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno, |
90320251 | 2241 | pos, appending, count, |
468eedde TM |
2242 | direct_io, has_refcount); |
2243 | ||
293b2f70 TM |
2244 | if (meta_level >= 0) |
2245 | ocfs2_inode_unlock(inode, meta_level); | |
8659ac25 TY |
2246 | |
2247 | out: | |
2248 | return ret; | |
2249 | } | |
2250 | ||
3ef045c3 AV |
2251 | static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, |
2252 | struct iov_iter *from) | |
8659ac25 | 2253 | { |
fa5a0eb3 | 2254 | int direct_io, appending, rw_level; |
86470e98 | 2255 | int can_do_direct, has_refcount = 0; |
9517bac6 | 2256 | ssize_t written = 0; |
3309dd04 | 2257 | ssize_t ret; |
90320251 | 2258 | size_t count = iov_iter_count(from), orig_count; |
5dc3161c | 2259 | loff_t old_size; |
9ea2d32f | 2260 | u32 old_clusters; |
9517bac6 | 2261 | struct file *file = iocb->ki_filp; |
496ad9aa | 2262 | struct inode *inode = file_inode(file); |
9ea2d32f | 2263 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
7bdb0d18 TY |
2264 | int full_coherency = !(osb->s_mount_opt & |
2265 | OCFS2_MOUNT_COHERENCY_BUFFERED); | |
a11f7e63 | 2266 | int unaligned_dio = 0; |
7da839c4 | 2267 | int dropped_dio = 0; |
9517bac6 | 2268 | |
468eedde TM |
2269 | trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry, |
2270 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
2271 | file->f_path.dentry->d_name.len, | |
2272 | file->f_path.dentry->d_name.name, | |
3ef045c3 | 2273 | (unsigned int)from->nr_segs); /* GRRRRR */ |
8659ac25 | 2274 | |
66ee59af | 2275 | if (count == 0) |
8659ac25 TY |
2276 | return 0; |
2277 | ||
2ba48ce5 AV |
2278 | appending = iocb->ki_flags & IOCB_APPEND ? 1 : 0; |
2279 | direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0; | |
9517bac6 | 2280 | |
8659ac25 | 2281 | mutex_lock(&inode->i_mutex); |
9517bac6 MF |
2282 | |
2283 | relock: | |
7bdb0d18 TY |
2284 | /* |
2285 | * Concurrent O_DIRECT writes are allowed with | |
2286 | * mount_option "coherency=buffered". | |
2287 | */ | |
2288 | rw_level = (!direct_io || full_coherency); | |
2289 | ||
8659ac25 TY |
2290 | ret = ocfs2_rw_lock(inode, rw_level); |
2291 | if (ret < 0) { | |
8659ac25 | 2292 | mlog_errno(ret); |
fa5a0eb3 | 2293 | goto out_mutex; |
8659ac25 TY |
2294 | } |
2295 | ||
7bdb0d18 TY |
2296 | /* |
2297 | * O_DIRECT writes with "coherency=full" need to take EX cluster | |
2298 | * inode_lock to guarantee coherency. | |
2299 | */ | |
2300 | if (direct_io && full_coherency) { | |
2301 | /* | |
2302 | * We need to take and drop the inode lock to force | |
2303 | * other nodes to drop their caches. Buffered I/O | |
2304 | * already does this in write_begin(). | |
2305 | */ | |
2306 | ret = ocfs2_inode_lock(inode, NULL, 1); | |
2307 | if (ret < 0) { | |
2308 | mlog_errno(ret); | |
afe1bb73 | 2309 | goto out; |
7bdb0d18 TY |
2310 | } |
2311 | ||
2312 | ocfs2_inode_unlock(inode, 1); | |
2313 | } | |
2314 | ||
3309dd04 AV |
2315 | orig_count = iov_iter_count(from); |
2316 | ret = generic_write_checks(iocb, from); | |
2317 | if (ret <= 0) { | |
2318 | if (ret) | |
2319 | mlog_errno(ret); | |
90320251 AV |
2320 | goto out; |
2321 | } | |
3309dd04 | 2322 | count = ret; |
90320251 | 2323 | |
9517bac6 | 2324 | can_do_direct = direct_io; |
90320251 | 2325 | ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count, appending, |
86470e98 | 2326 | &can_do_direct, &has_refcount); |
8659ac25 TY |
2327 | if (ret < 0) { |
2328 | mlog_errno(ret); | |
2329 | goto out; | |
2330 | } | |
ccd979bd | 2331 | |
a11f7e63 | 2332 | if (direct_io && !is_sync_kiocb(iocb)) |
5dc3161c | 2333 | unaligned_dio = ocfs2_is_io_unaligned(inode, count, iocb->ki_pos); |
a11f7e63 | 2334 | |
9517bac6 MF |
2335 | /* |
2336 | * We can't complete the direct I/O as requested, fall back to | |
2337 | * buffered I/O. | |
2338 | */ | |
2339 | if (direct_io && !can_do_direct) { | |
2340 | ocfs2_rw_unlock(inode, rw_level); | |
9517bac6 | 2341 | |
9517bac6 MF |
2342 | rw_level = -1; |
2343 | ||
2344 | direct_io = 0; | |
7da839c4 | 2345 | iocb->ki_flags &= ~IOCB_DIRECT; |
3309dd04 | 2346 | iov_iter_reexpand(from, orig_count); |
7da839c4 | 2347 | dropped_dio = 1; |
9517bac6 MF |
2348 | goto relock; |
2349 | } | |
2350 | ||
a11f7e63 MF |
2351 | if (unaligned_dio) { |
2352 | /* | |
2353 | * Wait on previous unaligned aio to complete before | |
2354 | * proceeding. | |
2355 | */ | |
c18ceab0 WW |
2356 | mutex_lock(&OCFS2_I(inode)->ip_unaligned_aio); |
2357 | /* Mark the iocb as needing an unlock in ocfs2_dio_end_io */ | |
a11f7e63 MF |
2358 | ocfs2_iocb_set_unaligned_aio(iocb); |
2359 | } | |
2360 | ||
9ea2d32f MF |
2361 | /* |
2362 | * To later detect whether a journal commit for sync writes is | |
2363 | * necessary, we sample i_size, and cluster count here. | |
2364 | */ | |
2365 | old_size = i_size_read(inode); | |
2366 | old_clusters = OCFS2_I(inode)->ip_clusters; | |
2367 | ||
ccd979bd | 2368 | /* communicate with ocfs2_dio_end_io */ |
7cdfc3a1 | 2369 | ocfs2_iocb_set_rw_locked(iocb, rw_level); |
ccd979bd | 2370 | |
7da839c4 | 2371 | written = __generic_file_write_iter(iocb, from); |
ccd979bd | 2372 | /* buffered aio wouldn't have proper lock coverage today */ |
7da839c4 | 2373 | BUG_ON(written == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT)); |
ccd979bd | 2374 | |
64b4e252 AV |
2375 | if (unlikely(written <= 0)) |
2376 | goto no_sync; | |
2377 | ||
7da839c4 AV |
2378 | if (((file->f_flags & O_DSYNC) && !direct_io) || |
2379 | IS_SYNC(inode) || dropped_dio) { | |
64b4e252 AV |
2380 | ret = filemap_fdatawrite_range(file->f_mapping, |
2381 | iocb->ki_pos - written, | |
2382 | iocb->ki_pos - 1); | |
918941a3 JK |
2383 | if (ret < 0) |
2384 | written = ret; | |
2385 | ||
86b9c6f3 | 2386 | if (!ret) { |
2b4e30fb | 2387 | ret = jbd2_journal_force_commit(osb->journal->j_journal); |
9ea2d32f MF |
2388 | if (ret < 0) |
2389 | written = ret; | |
2390 | } | |
918941a3 JK |
2391 | |
2392 | if (!ret) | |
64b4e252 AV |
2393 | ret = filemap_fdatawait_range(file->f_mapping, |
2394 | iocb->ki_pos - written, | |
2395 | iocb->ki_pos - 1); | |
9ea2d32f MF |
2396 | } |
2397 | ||
64b4e252 | 2398 | no_sync: |
2bd63216 | 2399 | /* |
ccd979bd MF |
2400 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io |
2401 | * function pointer which is called when o_direct io completes so that | |
bd5fe6c5 | 2402 | * it can unlock our rw lock. |
ccd979bd MF |
2403 | * Unfortunately there are error cases which call end_io and others |
2404 | * that don't. so we don't have to unlock the rw_lock if either an | |
2405 | * async dio is going to do it in the future or an end_io after an | |
2406 | * error has already done it. | |
2407 | */ | |
66b116c9 | 2408 | if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) { |
ccd979bd | 2409 | rw_level = -1; |
a11f7e63 | 2410 | unaligned_dio = 0; |
ccd979bd MF |
2411 | } |
2412 | ||
3e5d3c35 JB |
2413 | if (unaligned_dio) { |
2414 | ocfs2_iocb_clear_unaligned_aio(iocb); | |
c18ceab0 | 2415 | mutex_unlock(&OCFS2_I(inode)->ip_unaligned_aio); |
3e5d3c35 | 2416 | } |
a11f7e63 | 2417 | |
ccd979bd | 2418 | out: |
9517bac6 MF |
2419 | if (rw_level != -1) |
2420 | ocfs2_rw_unlock(inode, rw_level); | |
2421 | ||
fa5a0eb3 | 2422 | out_mutex: |
1b1dcc1b | 2423 | mutex_unlock(&inode->i_mutex); |
ccd979bd | 2424 | |
812e7a6a WW |
2425 | if (written) |
2426 | ret = written; | |
812e7a6a | 2427 | return ret; |
ccd979bd MF |
2428 | } |
2429 | ||
8659ac25 TY |
2430 | static ssize_t ocfs2_file_splice_read(struct file *in, |
2431 | loff_t *ppos, | |
2432 | struct pipe_inode_info *pipe, | |
2433 | size_t len, | |
2434 | unsigned int flags) | |
2435 | { | |
1962f39a | 2436 | int ret = 0, lock_level = 0; |
496ad9aa | 2437 | struct inode *inode = file_inode(in); |
8659ac25 | 2438 | |
468eedde TM |
2439 | trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry, |
2440 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
2441 | in->f_path.dentry->d_name.len, | |
2442 | in->f_path.dentry->d_name.name, len); | |
8659ac25 TY |
2443 | |
2444 | /* | |
3cd9ad5a | 2445 | * See the comment in ocfs2_file_read_iter() |
8659ac25 | 2446 | */ |
182be684 | 2447 | ret = ocfs2_inode_lock_atime(inode, in->f_path.mnt, &lock_level); |
8659ac25 TY |
2448 | if (ret < 0) { |
2449 | mlog_errno(ret); | |
2450 | goto bail; | |
2451 | } | |
1962f39a | 2452 | ocfs2_inode_unlock(inode, lock_level); |
8659ac25 TY |
2453 | |
2454 | ret = generic_file_splice_read(in, ppos, pipe, len, flags); | |
2455 | ||
2456 | bail: | |
8659ac25 TY |
2457 | return ret; |
2458 | } | |
2459 | ||
3cd9ad5a AV |
2460 | static ssize_t ocfs2_file_read_iter(struct kiocb *iocb, |
2461 | struct iov_iter *to) | |
ccd979bd | 2462 | { |
fa5a0eb3 | 2463 | int ret = 0, rw_level = -1, lock_level = 0; |
ccd979bd | 2464 | struct file *filp = iocb->ki_filp; |
496ad9aa | 2465 | struct inode *inode = file_inode(filp); |
ccd979bd | 2466 | |
468eedde TM |
2467 | trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry, |
2468 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
2469 | filp->f_path.dentry->d_name.len, | |
3cd9ad5a AV |
2470 | filp->f_path.dentry->d_name.name, |
2471 | to->nr_segs); /* GRRRRR */ | |
468eedde | 2472 | |
ccd979bd MF |
2473 | |
2474 | if (!inode) { | |
2475 | ret = -EINVAL; | |
2476 | mlog_errno(ret); | |
2477 | goto bail; | |
2478 | } | |
2479 | ||
2bd63216 | 2480 | /* |
ccd979bd MF |
2481 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads |
2482 | * need locks to protect pending reads from racing with truncate. | |
2483 | */ | |
2ba48ce5 | 2484 | if (iocb->ki_flags & IOCB_DIRECT) { |
ccd979bd MF |
2485 | ret = ocfs2_rw_lock(inode, 0); |
2486 | if (ret < 0) { | |
2487 | mlog_errno(ret); | |
2488 | goto bail; | |
2489 | } | |
2490 | rw_level = 0; | |
2491 | /* communicate with ocfs2_dio_end_io */ | |
7cdfc3a1 | 2492 | ocfs2_iocb_set_rw_locked(iocb, rw_level); |
ccd979bd MF |
2493 | } |
2494 | ||
c4374f8a MF |
2495 | /* |
2496 | * We're fine letting folks race truncates and extending | |
2497 | * writes with read across the cluster, just like they can | |
2498 | * locally. Hence no rw_lock during read. | |
2bd63216 | 2499 | * |
c4374f8a MF |
2500 | * Take and drop the meta data lock to update inode fields |
2501 | * like i_size. This allows the checks down below | |
2bd63216 | 2502 | * generic_file_aio_read() a chance of actually working. |
c4374f8a | 2503 | */ |
182be684 | 2504 | ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level); |
c4374f8a MF |
2505 | if (ret < 0) { |
2506 | mlog_errno(ret); | |
2507 | goto bail; | |
2508 | } | |
e63aecb6 | 2509 | ocfs2_inode_unlock(inode, lock_level); |
c4374f8a | 2510 | |
3cd9ad5a | 2511 | ret = generic_file_read_iter(iocb, to); |
468eedde | 2512 | trace_generic_file_aio_read_ret(ret); |
ccd979bd MF |
2513 | |
2514 | /* buffered aio wouldn't have proper lock coverage today */ | |
2ba48ce5 | 2515 | BUG_ON(ret == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT)); |
ccd979bd | 2516 | |
3ef045c3 | 2517 | /* see ocfs2_file_write_iter */ |
ccd979bd MF |
2518 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
2519 | rw_level = -1; | |
ccd979bd MF |
2520 | } |
2521 | ||
2522 | bail: | |
2bd63216 | 2523 | if (rw_level != -1) |
ccd979bd | 2524 | ocfs2_rw_unlock(inode, rw_level); |
ccd979bd MF |
2525 | |
2526 | return ret; | |
2527 | } | |
2528 | ||
93862d5e | 2529 | /* Refer generic_file_llseek_unlocked() */ |
965c8e59 | 2530 | static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence) |
93862d5e SM |
2531 | { |
2532 | struct inode *inode = file->f_mapping->host; | |
2533 | int ret = 0; | |
2534 | ||
2535 | mutex_lock(&inode->i_mutex); | |
2536 | ||
965c8e59 | 2537 | switch (whence) { |
93862d5e SM |
2538 | case SEEK_SET: |
2539 | break; | |
2540 | case SEEK_END: | |
c8d888d9 J |
2541 | /* SEEK_END requires the OCFS2 inode lock for the file |
2542 | * because it references the file's size. | |
2543 | */ | |
2544 | ret = ocfs2_inode_lock(inode, NULL, 0); | |
2545 | if (ret < 0) { | |
2546 | mlog_errno(ret); | |
2547 | goto out; | |
2548 | } | |
2549 | offset += i_size_read(inode); | |
2550 | ocfs2_inode_unlock(inode, 0); | |
93862d5e SM |
2551 | break; |
2552 | case SEEK_CUR: | |
2553 | if (offset == 0) { | |
2554 | offset = file->f_pos; | |
2555 | goto out; | |
2556 | } | |
2557 | offset += file->f_pos; | |
2558 | break; | |
2559 | case SEEK_DATA: | |
2560 | case SEEK_HOLE: | |
965c8e59 | 2561 | ret = ocfs2_seek_data_hole_offset(file, &offset, whence); |
93862d5e SM |
2562 | if (ret) |
2563 | goto out; | |
2564 | break; | |
2565 | default: | |
2566 | ret = -EINVAL; | |
2567 | goto out; | |
2568 | } | |
2569 | ||
46a1c2c7 | 2570 | offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes); |
93862d5e SM |
2571 | |
2572 | out: | |
2573 | mutex_unlock(&inode->i_mutex); | |
2574 | if (ret) | |
2575 | return ret; | |
2576 | return offset; | |
2577 | } | |
2578 | ||
92e1d5be | 2579 | const struct inode_operations ocfs2_file_iops = { |
ccd979bd MF |
2580 | .setattr = ocfs2_setattr, |
2581 | .getattr = ocfs2_getattr, | |
d38eb8db | 2582 | .permission = ocfs2_permission, |
cf1d6c76 TY |
2583 | .setxattr = generic_setxattr, |
2584 | .getxattr = generic_getxattr, | |
2585 | .listxattr = ocfs2_listxattr, | |
2586 | .removexattr = generic_removexattr, | |
00dc417f | 2587 | .fiemap = ocfs2_fiemap, |
4e34e719 | 2588 | .get_acl = ocfs2_iop_get_acl, |
702e5bc6 | 2589 | .set_acl = ocfs2_iop_set_acl, |
ccd979bd MF |
2590 | }; |
2591 | ||
92e1d5be | 2592 | const struct inode_operations ocfs2_special_file_iops = { |
ccd979bd MF |
2593 | .setattr = ocfs2_setattr, |
2594 | .getattr = ocfs2_getattr, | |
d38eb8db | 2595 | .permission = ocfs2_permission, |
4e34e719 | 2596 | .get_acl = ocfs2_iop_get_acl, |
702e5bc6 | 2597 | .set_acl = ocfs2_iop_set_acl, |
ccd979bd MF |
2598 | }; |
2599 | ||
53da4939 MF |
2600 | /* |
2601 | * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with | |
2602 | * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks! | |
2603 | */ | |
4b6f5d20 | 2604 | const struct file_operations ocfs2_fops = { |
93862d5e | 2605 | .llseek = ocfs2_file_llseek, |
ccd979bd MF |
2606 | .mmap = ocfs2_mmap, |
2607 | .fsync = ocfs2_sync_file, | |
2608 | .release = ocfs2_file_release, | |
2609 | .open = ocfs2_file_open, | |
3cd9ad5a | 2610 | .read_iter = ocfs2_file_read_iter, |
3ef045c3 | 2611 | .write_iter = ocfs2_file_write_iter, |
c9ec1488 | 2612 | .unlocked_ioctl = ocfs2_ioctl, |
586d232b MF |
2613 | #ifdef CONFIG_COMPAT |
2614 | .compat_ioctl = ocfs2_compat_ioctl, | |
2615 | #endif | |
53da4939 | 2616 | .lock = ocfs2_lock, |
53fc622b | 2617 | .flock = ocfs2_flock, |
8659ac25 | 2618 | .splice_read = ocfs2_file_splice_read, |
6dc8bc0f | 2619 | .splice_write = iter_file_splice_write, |
2fe17c10 | 2620 | .fallocate = ocfs2_fallocate, |
ccd979bd MF |
2621 | }; |
2622 | ||
4b6f5d20 | 2623 | const struct file_operations ocfs2_dops = { |
32c3c0e2 | 2624 | .llseek = generic_file_llseek, |
ccd979bd | 2625 | .read = generic_read_dir, |
3704412b | 2626 | .iterate = ocfs2_readdir, |
ccd979bd | 2627 | .fsync = ocfs2_sync_file, |
53fc622b MF |
2628 | .release = ocfs2_dir_release, |
2629 | .open = ocfs2_dir_open, | |
c9ec1488 | 2630 | .unlocked_ioctl = ocfs2_ioctl, |
586d232b MF |
2631 | #ifdef CONFIG_COMPAT |
2632 | .compat_ioctl = ocfs2_compat_ioctl, | |
53da4939 MF |
2633 | #endif |
2634 | .lock = ocfs2_lock, | |
2635 | .flock = ocfs2_flock, | |
2636 | }; | |
2637 | ||
2638 | /* | |
2639 | * POSIX-lockless variants of our file_operations. | |
2640 | * | |
2641 | * These will be used if the underlying cluster stack does not support | |
2642 | * posix file locking, if the user passes the "localflocks" mount | |
2643 | * option, or if we have a local-only fs. | |
2644 | * | |
2645 | * ocfs2_flock is in here because all stacks handle UNIX file locks, | |
2646 | * so we still want it in the case of no stack support for | |
2647 | * plocks. Internally, it will do the right thing when asked to ignore | |
2648 | * the cluster. | |
2649 | */ | |
2650 | const struct file_operations ocfs2_fops_no_plocks = { | |
93862d5e | 2651 | .llseek = ocfs2_file_llseek, |
53da4939 MF |
2652 | .mmap = ocfs2_mmap, |
2653 | .fsync = ocfs2_sync_file, | |
2654 | .release = ocfs2_file_release, | |
2655 | .open = ocfs2_file_open, | |
3cd9ad5a | 2656 | .read_iter = ocfs2_file_read_iter, |
3ef045c3 | 2657 | .write_iter = ocfs2_file_write_iter, |
53da4939 MF |
2658 | .unlocked_ioctl = ocfs2_ioctl, |
2659 | #ifdef CONFIG_COMPAT | |
2660 | .compat_ioctl = ocfs2_compat_ioctl, | |
2661 | #endif | |
2662 | .flock = ocfs2_flock, | |
2663 | .splice_read = ocfs2_file_splice_read, | |
6dc8bc0f | 2664 | .splice_write = iter_file_splice_write, |
3d1c1829 | 2665 | .fallocate = ocfs2_fallocate, |
53da4939 MF |
2666 | }; |
2667 | ||
2668 | const struct file_operations ocfs2_dops_no_plocks = { | |
2669 | .llseek = generic_file_llseek, | |
2670 | .read = generic_read_dir, | |
3704412b | 2671 | .iterate = ocfs2_readdir, |
53da4939 MF |
2672 | .fsync = ocfs2_sync_file, |
2673 | .release = ocfs2_dir_release, | |
2674 | .open = ocfs2_dir_open, | |
2675 | .unlocked_ioctl = ocfs2_ioctl, | |
2676 | #ifdef CONFIG_COMPAT | |
2677 | .compat_ioctl = ocfs2_compat_ioctl, | |
586d232b | 2678 | #endif |
53fc622b | 2679 | .flock = ocfs2_flock, |
ccd979bd | 2680 | }; |