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> |
ccd979bd MF |
38 | |
39 | #define MLOG_MASK_PREFIX ML_INODE | |
40 | #include <cluster/masklog.h> | |
41 | ||
42 | #include "ocfs2.h" | |
43 | ||
44 | #include "alloc.h" | |
45 | #include "aops.h" | |
46 | #include "dir.h" | |
47 | #include "dlmglue.h" | |
48 | #include "extent_map.h" | |
49 | #include "file.h" | |
50 | #include "sysfile.h" | |
51 | #include "inode.h" | |
ca4d147e | 52 | #include "ioctl.h" |
ccd979bd MF |
53 | #include "journal.h" |
54 | #include "mmap.h" | |
55 | #include "suballoc.h" | |
56 | #include "super.h" | |
57 | ||
58 | #include "buffer_head_io.h" | |
59 | ||
60 | static int ocfs2_sync_inode(struct inode *inode) | |
61 | { | |
62 | filemap_fdatawrite(inode->i_mapping); | |
63 | return sync_mapping_buffers(inode->i_mapping); | |
64 | } | |
65 | ||
66 | static int ocfs2_file_open(struct inode *inode, struct file *file) | |
67 | { | |
68 | int status; | |
69 | int mode = file->f_flags; | |
70 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
71 | ||
72 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, | |
d28c9174 | 73 | file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name); |
ccd979bd MF |
74 | |
75 | spin_lock(&oi->ip_lock); | |
76 | ||
77 | /* Check that the inode hasn't been wiped from disk by another | |
78 | * node. If it hasn't then we're safe as long as we hold the | |
79 | * spin lock until our increment of open count. */ | |
80 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { | |
81 | spin_unlock(&oi->ip_lock); | |
82 | ||
83 | status = -ENOENT; | |
84 | goto leave; | |
85 | } | |
86 | ||
87 | if (mode & O_DIRECT) | |
88 | oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT; | |
89 | ||
90 | oi->ip_open_count++; | |
91 | spin_unlock(&oi->ip_lock); | |
92 | status = 0; | |
93 | leave: | |
94 | mlog_exit(status); | |
95 | return status; | |
96 | } | |
97 | ||
98 | static int ocfs2_file_release(struct inode *inode, struct file *file) | |
99 | { | |
100 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
101 | ||
102 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, | |
d28c9174 JS |
103 | file->f_path.dentry->d_name.len, |
104 | file->f_path.dentry->d_name.name); | |
ccd979bd MF |
105 | |
106 | spin_lock(&oi->ip_lock); | |
107 | if (!--oi->ip_open_count) | |
108 | oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; | |
109 | spin_unlock(&oi->ip_lock); | |
110 | ||
111 | mlog_exit(0); | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | static int ocfs2_sync_file(struct file *file, | |
117 | struct dentry *dentry, | |
118 | int datasync) | |
119 | { | |
120 | int err = 0; | |
121 | journal_t *journal; | |
122 | struct inode *inode = dentry->d_inode; | |
123 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
124 | ||
125 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync, | |
126 | dentry->d_name.len, dentry->d_name.name); | |
127 | ||
128 | err = ocfs2_sync_inode(dentry->d_inode); | |
129 | if (err) | |
130 | goto bail; | |
131 | ||
132 | journal = osb->journal->j_journal; | |
133 | err = journal_force_commit(journal); | |
134 | ||
135 | bail: | |
136 | mlog_exit(err); | |
137 | ||
138 | return (err < 0) ? -EIO : 0; | |
139 | } | |
140 | ||
7f1a37e3 TY |
141 | int ocfs2_should_update_atime(struct inode *inode, |
142 | struct vfsmount *vfsmnt) | |
143 | { | |
144 | struct timespec now; | |
145 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
146 | ||
147 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) | |
148 | return 0; | |
149 | ||
150 | if ((inode->i_flags & S_NOATIME) || | |
151 | ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
152 | return 0; | |
153 | ||
6c2aad05 MF |
154 | /* |
155 | * We can be called with no vfsmnt structure - NFSD will | |
156 | * sometimes do this. | |
157 | * | |
158 | * Note that our action here is different than touch_atime() - | |
159 | * if we can't tell whether this is a noatime mount, then we | |
160 | * don't know whether to trust the value of s_atime_quantum. | |
161 | */ | |
162 | if (vfsmnt == NULL) | |
163 | return 0; | |
164 | ||
7f1a37e3 TY |
165 | if ((vfsmnt->mnt_flags & MNT_NOATIME) || |
166 | ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
167 | return 0; | |
168 | ||
7e913c53 MF |
169 | if (vfsmnt->mnt_flags & MNT_RELATIME) { |
170 | if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) || | |
171 | (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0)) | |
172 | return 1; | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
7f1a37e3 TY |
177 | now = CURRENT_TIME; |
178 | if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum)) | |
179 | return 0; | |
180 | else | |
181 | return 1; | |
182 | } | |
183 | ||
184 | int ocfs2_update_inode_atime(struct inode *inode, | |
185 | struct buffer_head *bh) | |
186 | { | |
187 | int ret; | |
188 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
189 | handle_t *handle; | |
c11e9faf | 190 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data; |
7f1a37e3 TY |
191 | |
192 | mlog_entry_void(); | |
193 | ||
194 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
195 | if (handle == NULL) { | |
196 | ret = -ENOMEM; | |
197 | mlog_errno(ret); | |
198 | goto out; | |
199 | } | |
200 | ||
c11e9faf MF |
201 | ret = ocfs2_journal_access(handle, inode, bh, |
202 | OCFS2_JOURNAL_ACCESS_WRITE); | |
203 | if (ret) { | |
204 | mlog_errno(ret); | |
205 | goto out_commit; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Don't use ocfs2_mark_inode_dirty() here as we don't always | |
210 | * have i_mutex to guard against concurrent changes to other | |
211 | * inode fields. | |
212 | */ | |
7f1a37e3 | 213 | inode->i_atime = CURRENT_TIME; |
c11e9faf MF |
214 | di->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
215 | di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); | |
216 | ||
217 | ret = ocfs2_journal_dirty(handle, bh); | |
7f1a37e3 TY |
218 | if (ret < 0) |
219 | mlog_errno(ret); | |
220 | ||
c11e9faf | 221 | out_commit: |
7f1a37e3 TY |
222 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
223 | out: | |
224 | mlog_exit(ret); | |
225 | return ret; | |
226 | } | |
227 | ||
6cb129f5 AB |
228 | static int ocfs2_set_inode_size(handle_t *handle, |
229 | struct inode *inode, | |
230 | struct buffer_head *fe_bh, | |
231 | u64 new_i_size) | |
ccd979bd MF |
232 | { |
233 | int status; | |
234 | ||
235 | mlog_entry_void(); | |
236 | i_size_write(inode, new_i_size); | |
8110b073 | 237 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
ccd979bd MF |
238 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
239 | ||
240 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
241 | if (status < 0) { | |
242 | mlog_errno(status); | |
243 | goto bail; | |
244 | } | |
245 | ||
246 | bail: | |
247 | mlog_exit(status); | |
248 | return status; | |
249 | } | |
250 | ||
251 | static int ocfs2_simple_size_update(struct inode *inode, | |
252 | struct buffer_head *di_bh, | |
253 | u64 new_i_size) | |
254 | { | |
255 | int ret; | |
256 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1fabe148 | 257 | handle_t *handle = NULL; |
ccd979bd | 258 | |
65eff9cc | 259 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
260 | if (handle == NULL) { |
261 | ret = -ENOMEM; | |
262 | mlog_errno(ret); | |
263 | goto out; | |
264 | } | |
265 | ||
266 | ret = ocfs2_set_inode_size(handle, inode, di_bh, | |
267 | new_i_size); | |
268 | if (ret < 0) | |
269 | mlog_errno(ret); | |
270 | ||
02dc1af4 | 271 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
272 | out: |
273 | return ret; | |
274 | } | |
275 | ||
276 | static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb, | |
277 | struct inode *inode, | |
278 | struct buffer_head *fe_bh, | |
279 | u64 new_i_size) | |
280 | { | |
281 | int status; | |
1fabe148 | 282 | handle_t *handle; |
60b11392 | 283 | struct ocfs2_dinode *di; |
35edec1d | 284 | u64 cluster_bytes; |
ccd979bd MF |
285 | |
286 | mlog_entry_void(); | |
287 | ||
288 | /* TODO: This needs to actually orphan the inode in this | |
289 | * transaction. */ | |
290 | ||
65eff9cc | 291 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
292 | if (IS_ERR(handle)) { |
293 | status = PTR_ERR(handle); | |
294 | mlog_errno(status); | |
295 | goto out; | |
296 | } | |
297 | ||
60b11392 MF |
298 | status = ocfs2_journal_access(handle, inode, fe_bh, |
299 | OCFS2_JOURNAL_ACCESS_WRITE); | |
300 | if (status < 0) { | |
301 | mlog_errno(status); | |
302 | goto out_commit; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Do this before setting i_size. | |
307 | */ | |
35edec1d MF |
308 | cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size); |
309 | status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size, | |
310 | cluster_bytes); | |
60b11392 MF |
311 | if (status) { |
312 | mlog_errno(status); | |
313 | goto out_commit; | |
314 | } | |
315 | ||
316 | i_size_write(inode, new_i_size); | |
317 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); | |
318 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
319 | ||
320 | di = (struct ocfs2_dinode *) fe_bh->b_data; | |
321 | di->i_size = cpu_to_le64(new_i_size); | |
322 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); | |
323 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); | |
324 | ||
325 | status = ocfs2_journal_dirty(handle, fe_bh); | |
ccd979bd MF |
326 | if (status < 0) |
327 | mlog_errno(status); | |
328 | ||
60b11392 | 329 | out_commit: |
02dc1af4 | 330 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 331 | out: |
60b11392 | 332 | |
ccd979bd MF |
333 | mlog_exit(status); |
334 | return status; | |
335 | } | |
336 | ||
337 | static int ocfs2_truncate_file(struct inode *inode, | |
338 | struct buffer_head *di_bh, | |
339 | u64 new_i_size) | |
340 | { | |
341 | int status = 0; | |
342 | struct ocfs2_dinode *fe = NULL; | |
343 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
344 | struct ocfs2_truncate_context *tc = NULL; | |
345 | ||
b0697053 MF |
346 | mlog_entry("(inode = %llu, new_i_size = %llu\n", |
347 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
348 | (unsigned long long)new_i_size); | |
ccd979bd | 349 | |
ccd979bd MF |
350 | fe = (struct ocfs2_dinode *) di_bh->b_data; |
351 | if (!OCFS2_IS_VALID_DINODE(fe)) { | |
352 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); | |
353 | status = -EIO; | |
354 | goto bail; | |
355 | } | |
356 | ||
357 | mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode), | |
b0697053 MF |
358 | "Inode %llu, inode i_size = %lld != di " |
359 | "i_size = %llu, i_flags = 0x%x\n", | |
360 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
ccd979bd | 361 | i_size_read(inode), |
b0697053 MF |
362 | (unsigned long long)le64_to_cpu(fe->i_size), |
363 | le32_to_cpu(fe->i_flags)); | |
ccd979bd MF |
364 | |
365 | if (new_i_size > le64_to_cpu(fe->i_size)) { | |
b0697053 MF |
366 | mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n", |
367 | (unsigned long long)le64_to_cpu(fe->i_size), | |
368 | (unsigned long long)new_i_size); | |
ccd979bd MF |
369 | status = -EINVAL; |
370 | mlog_errno(status); | |
371 | goto bail; | |
372 | } | |
373 | ||
b0697053 MF |
374 | mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n", |
375 | (unsigned long long)le64_to_cpu(fe->i_blkno), | |
376 | (unsigned long long)le64_to_cpu(fe->i_size), | |
377 | (unsigned long long)new_i_size); | |
ccd979bd MF |
378 | |
379 | /* lets handle the simple truncate cases before doing any more | |
380 | * cluster locking. */ | |
381 | if (new_i_size == le64_to_cpu(fe->i_size)) | |
382 | goto bail; | |
383 | ||
2e89b2e4 MF |
384 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
385 | ||
ab0920ce MF |
386 | /* This forces other nodes to sync and drop their pages. Do |
387 | * this even if we have a truncate without allocation change - | |
388 | * ocfs2 cluster sizes can be much greater than page size, so | |
389 | * we have to truncate them anyway. */ | |
390 | status = ocfs2_data_lock(inode, 1); | |
391 | if (status < 0) { | |
2e89b2e4 MF |
392 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
393 | ||
ab0920ce MF |
394 | mlog_errno(status); |
395 | goto bail; | |
396 | } | |
ab0920ce | 397 | |
2e89b2e4 MF |
398 | unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1); |
399 | truncate_inode_pages(inode->i_mapping, new_i_size); | |
400 | ||
ccd979bd MF |
401 | /* alright, we're going to need to do a full blown alloc size |
402 | * change. Orphan the inode so that recovery can complete the | |
403 | * truncate if necessary. This does the task of marking | |
404 | * i_size. */ | |
405 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); | |
406 | if (status < 0) { | |
407 | mlog_errno(status); | |
60b11392 | 408 | goto bail_unlock_data; |
ccd979bd MF |
409 | } |
410 | ||
411 | status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); | |
412 | if (status < 0) { | |
413 | mlog_errno(status); | |
60b11392 | 414 | goto bail_unlock_data; |
ccd979bd MF |
415 | } |
416 | ||
417 | status = ocfs2_commit_truncate(osb, inode, di_bh, tc); | |
418 | if (status < 0) { | |
419 | mlog_errno(status); | |
60b11392 | 420 | goto bail_unlock_data; |
ccd979bd MF |
421 | } |
422 | ||
423 | /* TODO: orphan dir cleanup here. */ | |
60b11392 MF |
424 | bail_unlock_data: |
425 | ocfs2_data_unlock(inode, 1); | |
426 | ||
2e89b2e4 MF |
427 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
428 | ||
ccd979bd MF |
429 | bail: |
430 | ||
431 | mlog_exit(status); | |
432 | return status; | |
433 | } | |
434 | ||
435 | /* | |
436 | * extend allocation only here. | |
437 | * we'll update all the disk stuff, and oip->alloc_size | |
438 | * | |
439 | * expect stuff to be locked, a transaction started and enough data / | |
440 | * metadata reservations in the contexts. | |
441 | * | |
442 | * Will return -EAGAIN, and a reason if a restart is needed. | |
443 | * If passed in, *reason will always be set, even in error. | |
444 | */ | |
445 | int ocfs2_do_extend_allocation(struct ocfs2_super *osb, | |
446 | struct inode *inode, | |
dcd0538f | 447 | u32 *logical_offset, |
ccd979bd | 448 | u32 clusters_to_add, |
2ae99a60 | 449 | int mark_unwritten, |
ccd979bd | 450 | struct buffer_head *fe_bh, |
1fabe148 | 451 | handle_t *handle, |
ccd979bd MF |
452 | struct ocfs2_alloc_context *data_ac, |
453 | struct ocfs2_alloc_context *meta_ac, | |
454 | enum ocfs2_alloc_restarted *reason_ret) | |
455 | { | |
456 | int status = 0; | |
457 | int free_extents; | |
458 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; | |
459 | enum ocfs2_alloc_restarted reason = RESTART_NONE; | |
460 | u32 bit_off, num_bits; | |
461 | u64 block; | |
2ae99a60 | 462 | u8 flags = 0; |
ccd979bd MF |
463 | |
464 | BUG_ON(!clusters_to_add); | |
465 | ||
2ae99a60 MF |
466 | if (mark_unwritten) |
467 | flags = OCFS2_EXT_UNWRITTEN; | |
468 | ||
ccd979bd MF |
469 | free_extents = ocfs2_num_free_extents(osb, inode, fe); |
470 | if (free_extents < 0) { | |
471 | status = free_extents; | |
472 | mlog_errno(status); | |
473 | goto leave; | |
474 | } | |
475 | ||
476 | /* there are two cases which could cause us to EAGAIN in the | |
477 | * we-need-more-metadata case: | |
478 | * 1) we haven't reserved *any* | |
479 | * 2) we are so fragmented, we've needed to add metadata too | |
480 | * many times. */ | |
481 | if (!free_extents && !meta_ac) { | |
482 | mlog(0, "we haven't reserved any metadata!\n"); | |
483 | status = -EAGAIN; | |
484 | reason = RESTART_META; | |
485 | goto leave; | |
486 | } else if ((!free_extents) | |
487 | && (ocfs2_alloc_context_bits_left(meta_ac) | |
488 | < ocfs2_extend_meta_needed(fe))) { | |
489 | mlog(0, "filesystem is really fragmented...\n"); | |
490 | status = -EAGAIN; | |
491 | reason = RESTART_META; | |
492 | goto leave; | |
493 | } | |
494 | ||
495 | status = ocfs2_claim_clusters(osb, handle, data_ac, 1, | |
496 | &bit_off, &num_bits); | |
497 | if (status < 0) { | |
498 | if (status != -ENOSPC) | |
499 | mlog_errno(status); | |
500 | goto leave; | |
501 | } | |
502 | ||
503 | BUG_ON(num_bits > clusters_to_add); | |
504 | ||
505 | /* reserve our write early -- insert_extent may update the inode */ | |
506 | status = ocfs2_journal_access(handle, inode, fe_bh, | |
507 | OCFS2_JOURNAL_ACCESS_WRITE); | |
508 | if (status < 0) { | |
509 | mlog_errno(status); | |
510 | goto leave; | |
511 | } | |
512 | ||
513 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); | |
b0697053 MF |
514 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
515 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
dcd0538f MF |
516 | status = ocfs2_insert_extent(osb, handle, inode, fe_bh, |
517 | *logical_offset, block, num_bits, | |
2ae99a60 | 518 | flags, meta_ac); |
ccd979bd MF |
519 | if (status < 0) { |
520 | mlog_errno(status); | |
521 | goto leave; | |
522 | } | |
523 | ||
ccd979bd MF |
524 | status = ocfs2_journal_dirty(handle, fe_bh); |
525 | if (status < 0) { | |
526 | mlog_errno(status); | |
527 | goto leave; | |
528 | } | |
529 | ||
530 | clusters_to_add -= num_bits; | |
dcd0538f | 531 | *logical_offset += num_bits; |
ccd979bd MF |
532 | |
533 | if (clusters_to_add) { | |
534 | mlog(0, "need to alloc once more, clusters = %u, wanted = " | |
535 | "%u\n", fe->i_clusters, clusters_to_add); | |
536 | status = -EAGAIN; | |
537 | reason = RESTART_TRANS; | |
538 | } | |
539 | ||
540 | leave: | |
541 | mlog_exit(status); | |
542 | if (reason_ret) | |
543 | *reason_ret = reason; | |
544 | return status; | |
545 | } | |
546 | ||
abf8b156 MF |
547 | /* |
548 | * For a given allocation, determine which allocators will need to be | |
549 | * accessed, and lock them, reserving the appropriate number of bits. | |
550 | * | |
2ae99a60 MF |
551 | * Sparse file systems call this from ocfs2_write_begin_nolock() |
552 | * and ocfs2_allocate_unwritten_extents(). | |
553 | * | |
554 | * File systems which don't support holes call this from | |
555 | * ocfs2_extend_allocation(). | |
abf8b156 | 556 | */ |
9517bac6 | 557 | int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, |
b27b7cbc | 558 | u32 clusters_to_add, u32 extents_to_split, |
9517bac6 MF |
559 | struct ocfs2_alloc_context **data_ac, |
560 | struct ocfs2_alloc_context **meta_ac) | |
abf8b156 | 561 | { |
063c4561 | 562 | int ret = 0, num_free_extents; |
b27b7cbc | 563 | unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; |
abf8b156 MF |
564 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
565 | ||
566 | *meta_ac = NULL; | |
063c4561 MF |
567 | if (data_ac) |
568 | *data_ac = NULL; | |
569 | ||
570 | BUG_ON(clusters_to_add != 0 && data_ac == NULL); | |
abf8b156 MF |
571 | |
572 | mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " | |
b27b7cbc | 573 | "clusters_to_add = %u, extents_to_split = %u\n", |
abf8b156 | 574 | (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode), |
b27b7cbc | 575 | le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split); |
abf8b156 MF |
576 | |
577 | num_free_extents = ocfs2_num_free_extents(osb, inode, di); | |
578 | if (num_free_extents < 0) { | |
579 | ret = num_free_extents; | |
580 | mlog_errno(ret); | |
581 | goto out; | |
582 | } | |
583 | ||
584 | /* | |
585 | * Sparse allocation file systems need to be more conservative | |
586 | * with reserving room for expansion - the actual allocation | |
587 | * happens while we've got a journal handle open so re-taking | |
588 | * a cluster lock (because we ran out of room for another | |
589 | * extent) will violate ordering rules. | |
590 | * | |
9517bac6 | 591 | * Most of the time we'll only be seeing this 1 cluster at a time |
abf8b156 | 592 | * anyway. |
b27b7cbc MF |
593 | * |
594 | * Always lock for any unwritten extents - we might want to | |
595 | * add blocks during a split. | |
abf8b156 MF |
596 | */ |
597 | if (!num_free_extents || | |
b27b7cbc | 598 | (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { |
abf8b156 MF |
599 | ret = ocfs2_reserve_new_metadata(osb, di, meta_ac); |
600 | if (ret < 0) { | |
601 | if (ret != -ENOSPC) | |
602 | mlog_errno(ret); | |
603 | goto out; | |
604 | } | |
605 | } | |
606 | ||
063c4561 MF |
607 | if (clusters_to_add == 0) |
608 | goto out; | |
609 | ||
abf8b156 MF |
610 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); |
611 | if (ret < 0) { | |
612 | if (ret != -ENOSPC) | |
613 | mlog_errno(ret); | |
614 | goto out; | |
615 | } | |
616 | ||
617 | out: | |
618 | if (ret) { | |
619 | if (*meta_ac) { | |
620 | ocfs2_free_alloc_context(*meta_ac); | |
621 | *meta_ac = NULL; | |
622 | } | |
623 | ||
624 | /* | |
625 | * We cannot have an error and a non null *data_ac. | |
626 | */ | |
627 | } | |
628 | ||
629 | return ret; | |
630 | } | |
631 | ||
2ae99a60 MF |
632 | static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, |
633 | u32 clusters_to_add, int mark_unwritten) | |
ccd979bd MF |
634 | { |
635 | int status = 0; | |
636 | int restart_func = 0; | |
abf8b156 | 637 | int credits; |
2ae99a60 | 638 | u32 prev_clusters; |
ccd979bd MF |
639 | struct buffer_head *bh = NULL; |
640 | struct ocfs2_dinode *fe = NULL; | |
1fabe148 | 641 | handle_t *handle = NULL; |
ccd979bd MF |
642 | struct ocfs2_alloc_context *data_ac = NULL; |
643 | struct ocfs2_alloc_context *meta_ac = NULL; | |
644 | enum ocfs2_alloc_restarted why; | |
645 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
646 | ||
647 | mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); | |
648 | ||
dcd0538f MF |
649 | /* |
650 | * This function only exists for file systems which don't | |
651 | * support holes. | |
652 | */ | |
2ae99a60 | 653 | BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb)); |
dcd0538f | 654 | |
ccd979bd MF |
655 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, |
656 | OCFS2_BH_CACHED, inode); | |
657 | if (status < 0) { | |
658 | mlog_errno(status); | |
659 | goto leave; | |
660 | } | |
661 | ||
662 | fe = (struct ocfs2_dinode *) bh->b_data; | |
663 | if (!OCFS2_IS_VALID_DINODE(fe)) { | |
664 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); | |
665 | status = -EIO; | |
666 | goto leave; | |
667 | } | |
668 | ||
669 | restart_all: | |
670 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); | |
671 | ||
b27b7cbc | 672 | status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac, |
9517bac6 MF |
673 | &meta_ac); |
674 | if (status) { | |
675 | mlog_errno(status); | |
676 | goto leave; | |
677 | } | |
678 | ||
ccd979bd | 679 | credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); |
65eff9cc | 680 | handle = ocfs2_start_trans(osb, credits); |
ccd979bd MF |
681 | if (IS_ERR(handle)) { |
682 | status = PTR_ERR(handle); | |
683 | handle = NULL; | |
684 | mlog_errno(status); | |
685 | goto leave; | |
686 | } | |
687 | ||
688 | restarted_transaction: | |
689 | /* reserve a write to the file entry early on - that we if we | |
690 | * run out of credits in the allocation path, we can still | |
691 | * update i_size. */ | |
692 | status = ocfs2_journal_access(handle, inode, bh, | |
693 | OCFS2_JOURNAL_ACCESS_WRITE); | |
694 | if (status < 0) { | |
695 | mlog_errno(status); | |
696 | goto leave; | |
697 | } | |
698 | ||
699 | prev_clusters = OCFS2_I(inode)->ip_clusters; | |
700 | ||
701 | status = ocfs2_do_extend_allocation(osb, | |
702 | inode, | |
dcd0538f | 703 | &logical_start, |
ccd979bd | 704 | clusters_to_add, |
2ae99a60 | 705 | mark_unwritten, |
ccd979bd MF |
706 | bh, |
707 | handle, | |
708 | data_ac, | |
709 | meta_ac, | |
710 | &why); | |
711 | if ((status < 0) && (status != -EAGAIN)) { | |
712 | if (status != -ENOSPC) | |
713 | mlog_errno(status); | |
714 | goto leave; | |
715 | } | |
716 | ||
717 | status = ocfs2_journal_dirty(handle, bh); | |
718 | if (status < 0) { | |
719 | mlog_errno(status); | |
720 | goto leave; | |
721 | } | |
722 | ||
723 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
724 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); | |
725 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
726 | ||
727 | if (why != RESTART_NONE && clusters_to_add) { | |
728 | if (why == RESTART_META) { | |
729 | mlog(0, "restarting function.\n"); | |
730 | restart_func = 1; | |
731 | } else { | |
732 | BUG_ON(why != RESTART_TRANS); | |
733 | ||
734 | mlog(0, "restarting transaction.\n"); | |
735 | /* TODO: This can be more intelligent. */ | |
736 | credits = ocfs2_calc_extend_credits(osb->sb, | |
737 | fe, | |
738 | clusters_to_add); | |
1fabe148 | 739 | status = ocfs2_extend_trans(handle, credits); |
ccd979bd MF |
740 | if (status < 0) { |
741 | /* handle still has to be committed at | |
742 | * this point. */ | |
743 | status = -ENOMEM; | |
744 | mlog_errno(status); | |
745 | goto leave; | |
746 | } | |
747 | goto restarted_transaction; | |
748 | } | |
749 | } | |
750 | ||
b0697053 | 751 | mlog(0, "fe: i_clusters = %u, i_size=%llu\n", |
1ca1a111 MF |
752 | le32_to_cpu(fe->i_clusters), |
753 | (unsigned long long)le64_to_cpu(fe->i_size)); | |
ccd979bd MF |
754 | mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", |
755 | OCFS2_I(inode)->ip_clusters, i_size_read(inode)); | |
756 | ||
757 | leave: | |
ccd979bd | 758 | if (handle) { |
02dc1af4 | 759 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
760 | handle = NULL; |
761 | } | |
762 | if (data_ac) { | |
763 | ocfs2_free_alloc_context(data_ac); | |
764 | data_ac = NULL; | |
765 | } | |
766 | if (meta_ac) { | |
767 | ocfs2_free_alloc_context(meta_ac); | |
768 | meta_ac = NULL; | |
769 | } | |
770 | if ((!status) && restart_func) { | |
771 | restart_func = 0; | |
772 | goto restart_all; | |
773 | } | |
774 | if (bh) { | |
775 | brelse(bh); | |
776 | bh = NULL; | |
777 | } | |
778 | ||
779 | mlog_exit(status); | |
780 | return status; | |
781 | } | |
782 | ||
2ae99a60 MF |
783 | static int ocfs2_extend_allocation(struct inode *inode, u32 logical_start, |
784 | u32 clusters_to_add, int mark_unwritten) | |
785 | { | |
786 | int ret; | |
787 | ||
788 | /* | |
789 | * The alloc sem blocks peope in read/write from reading our | |
790 | * allocation until we're done changing it. We depend on | |
791 | * i_mutex to block other extend/truncate calls while we're | |
792 | * here. | |
793 | */ | |
794 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | |
795 | ret = __ocfs2_extend_allocation(inode, logical_start, clusters_to_add, | |
796 | mark_unwritten); | |
797 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | |
798 | ||
799 | return ret; | |
800 | } | |
801 | ||
ccd979bd MF |
802 | /* Some parts of this taken from generic_cont_expand, which turned out |
803 | * to be too fragile to do exactly what we need without us having to | |
53013cba MF |
804 | * worry about recursive locking in ->prepare_write() and |
805 | * ->commit_write(). */ | |
ccd979bd MF |
806 | static int ocfs2_write_zero_page(struct inode *inode, |
807 | u64 size) | |
808 | { | |
809 | struct address_space *mapping = inode->i_mapping; | |
810 | struct page *page; | |
811 | unsigned long index; | |
812 | unsigned int offset; | |
1fabe148 | 813 | handle_t *handle = NULL; |
ccd979bd MF |
814 | int ret; |
815 | ||
816 | offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */ | |
817 | /* ugh. in prepare/commit_write, if from==to==start of block, we | |
818 | ** skip the prepare. make sure we never send an offset for the start | |
819 | ** of a block | |
820 | */ | |
821 | if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { | |
822 | offset++; | |
823 | } | |
824 | index = size >> PAGE_CACHE_SHIFT; | |
825 | ||
826 | page = grab_cache_page(mapping, index); | |
827 | if (!page) { | |
828 | ret = -ENOMEM; | |
829 | mlog_errno(ret); | |
830 | goto out; | |
831 | } | |
832 | ||
53013cba | 833 | ret = ocfs2_prepare_write_nolock(inode, page, offset, offset); |
ccd979bd MF |
834 | if (ret < 0) { |
835 | mlog_errno(ret); | |
836 | goto out_unlock; | |
837 | } | |
838 | ||
839 | if (ocfs2_should_order_data(inode)) { | |
840 | handle = ocfs2_start_walk_page_trans(inode, page, offset, | |
841 | offset); | |
842 | if (IS_ERR(handle)) { | |
843 | ret = PTR_ERR(handle); | |
844 | handle = NULL; | |
845 | goto out_unlock; | |
846 | } | |
847 | } | |
848 | ||
849 | /* must not update i_size! */ | |
850 | ret = block_commit_write(page, offset, offset); | |
851 | if (ret < 0) | |
852 | mlog_errno(ret); | |
853 | else | |
854 | ret = 0; | |
855 | ||
856 | if (handle) | |
02dc1af4 | 857 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
ccd979bd MF |
858 | out_unlock: |
859 | unlock_page(page); | |
860 | page_cache_release(page); | |
861 | out: | |
862 | return ret; | |
863 | } | |
864 | ||
865 | static int ocfs2_zero_extend(struct inode *inode, | |
866 | u64 zero_to_size) | |
867 | { | |
868 | int ret = 0; | |
869 | u64 start_off; | |
870 | struct super_block *sb = inode->i_sb; | |
871 | ||
872 | start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); | |
873 | while (start_off < zero_to_size) { | |
874 | ret = ocfs2_write_zero_page(inode, start_off); | |
875 | if (ret < 0) { | |
876 | mlog_errno(ret); | |
877 | goto out; | |
878 | } | |
879 | ||
880 | start_off += sb->s_blocksize; | |
e2057c5a MF |
881 | |
882 | /* | |
883 | * Very large extends have the potential to lock up | |
884 | * the cpu for extended periods of time. | |
885 | */ | |
886 | cond_resched(); | |
ccd979bd MF |
887 | } |
888 | ||
889 | out: | |
890 | return ret; | |
891 | } | |
892 | ||
53013cba MF |
893 | /* |
894 | * A tail_to_skip value > 0 indicates that we're being called from | |
895 | * ocfs2_file_aio_write(). This has the following implications: | |
896 | * | |
897 | * - we don't want to update i_size | |
898 | * - di_bh will be NULL, which is fine because it's only used in the | |
899 | * case where we want to update i_size. | |
900 | * - ocfs2_zero_extend() will then only be filling the hole created | |
901 | * between i_size and the start of the write. | |
902 | */ | |
ccd979bd MF |
903 | static int ocfs2_extend_file(struct inode *inode, |
904 | struct buffer_head *di_bh, | |
53013cba MF |
905 | u64 new_i_size, |
906 | size_t tail_to_skip) | |
ccd979bd MF |
907 | { |
908 | int ret = 0; | |
3a0782d0 | 909 | u32 clusters_to_add = 0; |
ccd979bd | 910 | |
53013cba MF |
911 | BUG_ON(!tail_to_skip && !di_bh); |
912 | ||
ccd979bd MF |
913 | /* setattr sometimes calls us like this. */ |
914 | if (new_i_size == 0) | |
915 | goto out; | |
916 | ||
917 | if (i_size_read(inode) == new_i_size) | |
918 | goto out; | |
919 | BUG_ON(new_i_size < i_size_read(inode)); | |
920 | ||
3a0782d0 MF |
921 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
922 | BUG_ON(tail_to_skip != 0); | |
923 | goto out_update_size; | |
924 | } | |
925 | ||
ccd979bd MF |
926 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - |
927 | OCFS2_I(inode)->ip_clusters; | |
928 | ||
0effef77 MF |
929 | /* |
930 | * protect the pages that ocfs2_zero_extend is going to be | |
931 | * pulling into the page cache.. we do this before the | |
932 | * metadata extend so that we don't get into the situation | |
933 | * where we've extended the metadata but can't get the data | |
934 | * lock to zero. | |
935 | */ | |
936 | ret = ocfs2_data_lock(inode, 1); | |
937 | if (ret < 0) { | |
938 | mlog_errno(ret); | |
939 | goto out; | |
940 | } | |
ccd979bd | 941 | |
0effef77 | 942 | if (clusters_to_add) { |
2ae99a60 MF |
943 | ret = ocfs2_extend_allocation(inode, |
944 | OCFS2_I(inode)->ip_clusters, | |
945 | clusters_to_add, 0); | |
ccd979bd MF |
946 | if (ret < 0) { |
947 | mlog_errno(ret); | |
53013cba | 948 | goto out_unlock; |
ccd979bd | 949 | } |
0effef77 | 950 | } |
ccd979bd | 951 | |
0effef77 MF |
952 | /* |
953 | * Call this even if we don't add any clusters to the tree. We | |
954 | * still need to zero the area between the old i_size and the | |
955 | * new i_size. | |
956 | */ | |
957 | ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip); | |
958 | if (ret < 0) { | |
959 | mlog_errno(ret); | |
960 | goto out_unlock; | |
53013cba MF |
961 | } |
962 | ||
3a0782d0 | 963 | out_update_size: |
53013cba MF |
964 | if (!tail_to_skip) { |
965 | /* We're being called from ocfs2_setattr() which wants | |
966 | * us to update i_size */ | |
967 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); | |
968 | if (ret < 0) | |
969 | mlog_errno(ret); | |
ccd979bd MF |
970 | } |
971 | ||
53013cba | 972 | out_unlock: |
3a0782d0 MF |
973 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) |
974 | ocfs2_data_unlock(inode, 1); | |
53013cba | 975 | |
ccd979bd MF |
976 | out: |
977 | return ret; | |
978 | } | |
979 | ||
980 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) | |
981 | { | |
982 | int status = 0, size_change; | |
983 | struct inode *inode = dentry->d_inode; | |
984 | struct super_block *sb = inode->i_sb; | |
985 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
986 | struct buffer_head *bh = NULL; | |
1fabe148 | 987 | handle_t *handle = NULL; |
ccd979bd MF |
988 | |
989 | mlog_entry("(0x%p, '%.*s')\n", dentry, | |
990 | dentry->d_name.len, dentry->d_name.name); | |
991 | ||
992 | if (attr->ia_valid & ATTR_MODE) | |
993 | mlog(0, "mode change: %d\n", attr->ia_mode); | |
994 | if (attr->ia_valid & ATTR_UID) | |
995 | mlog(0, "uid change: %d\n", attr->ia_uid); | |
996 | if (attr->ia_valid & ATTR_GID) | |
997 | mlog(0, "gid change: %d\n", attr->ia_gid); | |
998 | if (attr->ia_valid & ATTR_SIZE) | |
999 | mlog(0, "size change...\n"); | |
1000 | if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) | |
1001 | mlog(0, "time change...\n"); | |
1002 | ||
1003 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ | |
1004 | | ATTR_GID | ATTR_UID | ATTR_MODE) | |
1005 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) { | |
1006 | mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid); | |
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | status = inode_change_ok(inode, attr); | |
1011 | if (status) | |
1012 | return status; | |
1013 | ||
1014 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; | |
1015 | if (size_change) { | |
1016 | status = ocfs2_rw_lock(inode, 1); | |
1017 | if (status < 0) { | |
1018 | mlog_errno(status); | |
1019 | goto bail; | |
1020 | } | |
1021 | } | |
1022 | ||
4bcec184 | 1023 | status = ocfs2_meta_lock(inode, &bh, 1); |
ccd979bd MF |
1024 | if (status < 0) { |
1025 | if (status != -ENOENT) | |
1026 | mlog_errno(status); | |
1027 | goto bail_unlock_rw; | |
1028 | } | |
1029 | ||
1030 | if (size_change && attr->ia_size != i_size_read(inode)) { | |
1031 | if (i_size_read(inode) > attr->ia_size) | |
1032 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); | |
1033 | else | |
53013cba | 1034 | status = ocfs2_extend_file(inode, bh, attr->ia_size, 0); |
ccd979bd MF |
1035 | if (status < 0) { |
1036 | if (status != -ENOSPC) | |
1037 | mlog_errno(status); | |
1038 | status = -ENOSPC; | |
1039 | goto bail_unlock; | |
1040 | } | |
1041 | } | |
1042 | ||
65eff9cc | 1043 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
1044 | if (IS_ERR(handle)) { |
1045 | status = PTR_ERR(handle); | |
1046 | mlog_errno(status); | |
1047 | goto bail_unlock; | |
1048 | } | |
1049 | ||
7307de80 MF |
1050 | /* |
1051 | * This will intentionally not wind up calling vmtruncate(), | |
1052 | * since all the work for a size change has been done above. | |
1053 | * Otherwise, we could get into problems with truncate as | |
1054 | * ip_alloc_sem is used there to protect against i_size | |
1055 | * changes. | |
1056 | */ | |
ccd979bd MF |
1057 | status = inode_setattr(inode, attr); |
1058 | if (status < 0) { | |
1059 | mlog_errno(status); | |
1060 | goto bail_commit; | |
1061 | } | |
1062 | ||
1063 | status = ocfs2_mark_inode_dirty(handle, inode, bh); | |
1064 | if (status < 0) | |
1065 | mlog_errno(status); | |
1066 | ||
1067 | bail_commit: | |
02dc1af4 | 1068 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
1069 | bail_unlock: |
1070 | ocfs2_meta_unlock(inode, 1); | |
1071 | bail_unlock_rw: | |
1072 | if (size_change) | |
1073 | ocfs2_rw_unlock(inode, 1); | |
1074 | bail: | |
1075 | if (bh) | |
1076 | brelse(bh); | |
1077 | ||
1078 | mlog_exit(status); | |
1079 | return status; | |
1080 | } | |
1081 | ||
1082 | int ocfs2_getattr(struct vfsmount *mnt, | |
1083 | struct dentry *dentry, | |
1084 | struct kstat *stat) | |
1085 | { | |
1086 | struct inode *inode = dentry->d_inode; | |
1087 | struct super_block *sb = dentry->d_inode->i_sb; | |
1088 | struct ocfs2_super *osb = sb->s_fs_info; | |
1089 | int err; | |
1090 | ||
1091 | mlog_entry_void(); | |
1092 | ||
1093 | err = ocfs2_inode_revalidate(dentry); | |
1094 | if (err) { | |
1095 | if (err != -ENOENT) | |
1096 | mlog_errno(err); | |
1097 | goto bail; | |
1098 | } | |
1099 | ||
1100 | generic_fillattr(inode, stat); | |
1101 | ||
1102 | /* We set the blksize from the cluster size for performance */ | |
1103 | stat->blksize = osb->s_clustersize; | |
1104 | ||
1105 | bail: | |
1106 | mlog_exit(err); | |
1107 | ||
1108 | return err; | |
1109 | } | |
1110 | ||
d38eb8db TY |
1111 | int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd) |
1112 | { | |
1113 | int ret; | |
1114 | ||
1115 | mlog_entry_void(); | |
1116 | ||
1117 | ret = ocfs2_meta_lock(inode, NULL, 0); | |
1118 | if (ret) { | |
a9f5f707 MF |
1119 | if (ret != -ENOENT) |
1120 | mlog_errno(ret); | |
d38eb8db TY |
1121 | goto out; |
1122 | } | |
1123 | ||
1124 | ret = generic_permission(inode, mask, NULL); | |
d38eb8db TY |
1125 | |
1126 | ocfs2_meta_unlock(inode, 0); | |
1127 | out: | |
1128 | mlog_exit(ret); | |
1129 | return ret; | |
1130 | } | |
1131 | ||
b2580103 MF |
1132 | static int __ocfs2_write_remove_suid(struct inode *inode, |
1133 | struct buffer_head *bh) | |
ccd979bd MF |
1134 | { |
1135 | int ret; | |
1fabe148 | 1136 | handle_t *handle; |
ccd979bd MF |
1137 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
1138 | struct ocfs2_dinode *di; | |
1139 | ||
b0697053 | 1140 | mlog_entry("(Inode %llu, mode 0%o)\n", |
b2580103 | 1141 | (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode); |
ccd979bd | 1142 | |
65eff9cc | 1143 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
1144 | if (handle == NULL) { |
1145 | ret = -ENOMEM; | |
1146 | mlog_errno(ret); | |
1147 | goto out; | |
1148 | } | |
1149 | ||
ccd979bd MF |
1150 | ret = ocfs2_journal_access(handle, inode, bh, |
1151 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1152 | if (ret < 0) { | |
1153 | mlog_errno(ret); | |
b2580103 | 1154 | goto out_trans; |
ccd979bd MF |
1155 | } |
1156 | ||
1157 | inode->i_mode &= ~S_ISUID; | |
1158 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) | |
1159 | inode->i_mode &= ~S_ISGID; | |
1160 | ||
1161 | di = (struct ocfs2_dinode *) bh->b_data; | |
1162 | di->i_mode = cpu_to_le16(inode->i_mode); | |
1163 | ||
1164 | ret = ocfs2_journal_dirty(handle, bh); | |
1165 | if (ret < 0) | |
1166 | mlog_errno(ret); | |
b2580103 | 1167 | |
ccd979bd | 1168 | out_trans: |
02dc1af4 | 1169 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
1170 | out: |
1171 | mlog_exit(ret); | |
1172 | return ret; | |
1173 | } | |
1174 | ||
9517bac6 MF |
1175 | /* |
1176 | * Will look for holes and unwritten extents in the range starting at | |
1177 | * pos for count bytes (inclusive). | |
1178 | */ | |
1179 | static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos, | |
1180 | size_t count) | |
1181 | { | |
1182 | int ret = 0; | |
49cb8d2d | 1183 | unsigned int extent_flags; |
9517bac6 MF |
1184 | u32 cpos, clusters, extent_len, phys_cpos; |
1185 | struct super_block *sb = inode->i_sb; | |
1186 | ||
1187 | cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits; | |
1188 | clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos; | |
1189 | ||
1190 | while (clusters) { | |
49cb8d2d MF |
1191 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len, |
1192 | &extent_flags); | |
9517bac6 MF |
1193 | if (ret < 0) { |
1194 | mlog_errno(ret); | |
1195 | goto out; | |
1196 | } | |
1197 | ||
49cb8d2d | 1198 | if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) { |
9517bac6 MF |
1199 | ret = 1; |
1200 | break; | |
1201 | } | |
1202 | ||
1203 | if (extent_len > clusters) | |
1204 | extent_len = clusters; | |
1205 | ||
1206 | clusters -= extent_len; | |
1207 | cpos += extent_len; | |
1208 | } | |
1209 | out: | |
1210 | return ret; | |
1211 | } | |
1212 | ||
b2580103 MF |
1213 | static int ocfs2_write_remove_suid(struct inode *inode) |
1214 | { | |
1215 | int ret; | |
1216 | struct buffer_head *bh = NULL; | |
1217 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1218 | ||
1219 | ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), | |
1220 | oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode); | |
1221 | if (ret < 0) { | |
1222 | mlog_errno(ret); | |
1223 | goto out; | |
1224 | } | |
1225 | ||
1226 | ret = __ocfs2_write_remove_suid(inode, bh); | |
1227 | out: | |
1228 | brelse(bh); | |
1229 | return ret; | |
1230 | } | |
1231 | ||
2ae99a60 MF |
1232 | /* |
1233 | * Allocate enough extents to cover the region starting at byte offset | |
1234 | * start for len bytes. Existing extents are skipped, any extents | |
1235 | * added are marked as "unwritten". | |
1236 | */ | |
1237 | static int ocfs2_allocate_unwritten_extents(struct inode *inode, | |
1238 | u64 start, u64 len) | |
1239 | { | |
1240 | int ret; | |
1241 | u32 cpos, phys_cpos, clusters, alloc_size; | |
1242 | ||
1243 | /* | |
1244 | * We consider both start and len to be inclusive. | |
1245 | */ | |
1246 | cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits; | |
1247 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len); | |
1248 | clusters -= cpos; | |
1249 | ||
1250 | while (clusters) { | |
1251 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, | |
1252 | &alloc_size, NULL); | |
1253 | if (ret) { | |
1254 | mlog_errno(ret); | |
1255 | goto out; | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * Hole or existing extent len can be arbitrary, so | |
1260 | * cap it to our own allocation request. | |
1261 | */ | |
1262 | if (alloc_size > clusters) | |
1263 | alloc_size = clusters; | |
1264 | ||
1265 | if (phys_cpos) { | |
1266 | /* | |
1267 | * We already have an allocation at this | |
1268 | * region so we can safely skip it. | |
1269 | */ | |
1270 | goto next; | |
1271 | } | |
1272 | ||
1273 | ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1); | |
1274 | if (ret) { | |
1275 | if (ret != -ENOSPC) | |
1276 | mlog_errno(ret); | |
1277 | goto out; | |
1278 | } | |
1279 | ||
1280 | next: | |
1281 | cpos += alloc_size; | |
1282 | clusters -= alloc_size; | |
1283 | } | |
1284 | ||
1285 | ret = 0; | |
1286 | out: | |
1287 | return ret; | |
1288 | } | |
1289 | ||
063c4561 MF |
1290 | static int __ocfs2_remove_inode_range(struct inode *inode, |
1291 | struct buffer_head *di_bh, | |
1292 | u32 cpos, u32 phys_cpos, u32 len, | |
1293 | struct ocfs2_cached_dealloc_ctxt *dealloc) | |
1294 | { | |
1295 | int ret; | |
1296 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); | |
1297 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1298 | struct inode *tl_inode = osb->osb_tl_inode; | |
1299 | handle_t *handle; | |
1300 | struct ocfs2_alloc_context *meta_ac = NULL; | |
1301 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
1302 | ||
1303 | ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac); | |
1304 | if (ret) { | |
1305 | mlog_errno(ret); | |
1306 | return ret; | |
1307 | } | |
1308 | ||
1309 | mutex_lock(&tl_inode->i_mutex); | |
1310 | ||
1311 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
1312 | ret = __ocfs2_flush_truncate_log(osb); | |
1313 | if (ret < 0) { | |
1314 | mlog_errno(ret); | |
1315 | goto out; | |
1316 | } | |
1317 | } | |
1318 | ||
1319 | handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); | |
1320 | if (handle == NULL) { | |
1321 | ret = -ENOMEM; | |
1322 | mlog_errno(ret); | |
1323 | goto out; | |
1324 | } | |
1325 | ||
1326 | ret = ocfs2_journal_access(handle, inode, di_bh, | |
1327 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1328 | if (ret) { | |
1329 | mlog_errno(ret); | |
1330 | goto out; | |
1331 | } | |
1332 | ||
1333 | ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac, | |
1334 | dealloc); | |
1335 | if (ret) { | |
1336 | mlog_errno(ret); | |
1337 | goto out_commit; | |
1338 | } | |
1339 | ||
1340 | OCFS2_I(inode)->ip_clusters -= len; | |
1341 | di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); | |
1342 | ||
1343 | ret = ocfs2_journal_dirty(handle, di_bh); | |
1344 | if (ret) { | |
1345 | mlog_errno(ret); | |
1346 | goto out_commit; | |
1347 | } | |
1348 | ||
1349 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); | |
1350 | if (ret) | |
1351 | mlog_errno(ret); | |
1352 | ||
1353 | out_commit: | |
1354 | ocfs2_commit_trans(osb, handle); | |
1355 | out: | |
1356 | mutex_unlock(&tl_inode->i_mutex); | |
1357 | ||
1358 | if (meta_ac) | |
1359 | ocfs2_free_alloc_context(meta_ac); | |
1360 | ||
1361 | return ret; | |
1362 | } | |
1363 | ||
1364 | /* | |
1365 | * Truncate a byte range, avoiding pages within partial clusters. This | |
1366 | * preserves those pages for the zeroing code to write to. | |
1367 | */ | |
1368 | static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start, | |
1369 | u64 byte_len) | |
1370 | { | |
1371 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1372 | loff_t start, end; | |
1373 | struct address_space *mapping = inode->i_mapping; | |
1374 | ||
1375 | start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start); | |
1376 | end = byte_start + byte_len; | |
1377 | end = end & ~(osb->s_clustersize - 1); | |
1378 | ||
1379 | if (start < end) { | |
1380 | unmap_mapping_range(mapping, start, end - start, 0); | |
1381 | truncate_inode_pages_range(mapping, start, end - 1); | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | static int ocfs2_zero_partial_clusters(struct inode *inode, | |
1386 | u64 start, u64 len) | |
1387 | { | |
1388 | int ret = 0; | |
1389 | u64 tmpend, end = start + len; | |
1390 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1391 | unsigned int csize = osb->s_clustersize; | |
1392 | handle_t *handle; | |
1393 | ||
1394 | /* | |
1395 | * The "start" and "end" values are NOT necessarily part of | |
1396 | * the range whose allocation is being deleted. Rather, this | |
1397 | * is what the user passed in with the request. We must zero | |
1398 | * partial clusters here. There's no need to worry about | |
1399 | * physical allocation - the zeroing code knows to skip holes. | |
1400 | */ | |
1401 | mlog(0, "byte start: %llu, end: %llu\n", | |
1402 | (unsigned long long)start, (unsigned long long)end); | |
1403 | ||
1404 | /* | |
1405 | * If both edges are on a cluster boundary then there's no | |
1406 | * zeroing required as the region is part of the allocation to | |
1407 | * be truncated. | |
1408 | */ | |
1409 | if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0) | |
1410 | goto out; | |
1411 | ||
1412 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
1413 | if (handle == NULL) { | |
1414 | ret = -ENOMEM; | |
1415 | mlog_errno(ret); | |
1416 | goto out; | |
1417 | } | |
1418 | ||
1419 | /* | |
1420 | * We want to get the byte offset of the end of the 1st cluster. | |
1421 | */ | |
1422 | tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1)); | |
1423 | if (tmpend > end) | |
1424 | tmpend = end; | |
1425 | ||
1426 | mlog(0, "1st range: start: %llu, tmpend: %llu\n", | |
1427 | (unsigned long long)start, (unsigned long long)tmpend); | |
1428 | ||
1429 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend); | |
1430 | if (ret) | |
1431 | mlog_errno(ret); | |
1432 | ||
1433 | if (tmpend < end) { | |
1434 | /* | |
1435 | * This may make start and end equal, but the zeroing | |
1436 | * code will skip any work in that case so there's no | |
1437 | * need to catch it up here. | |
1438 | */ | |
1439 | start = end & ~(osb->s_clustersize - 1); | |
1440 | ||
1441 | mlog(0, "2nd range: start: %llu, end: %llu\n", | |
1442 | (unsigned long long)start, (unsigned long long)end); | |
1443 | ||
1444 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, end); | |
1445 | if (ret) | |
1446 | mlog_errno(ret); | |
1447 | } | |
1448 | ||
1449 | ocfs2_commit_trans(osb, handle); | |
1450 | out: | |
1451 | return ret; | |
1452 | } | |
1453 | ||
1454 | static int ocfs2_remove_inode_range(struct inode *inode, | |
1455 | struct buffer_head *di_bh, u64 byte_start, | |
1456 | u64 byte_len) | |
1457 | { | |
1458 | int ret = 0; | |
1459 | u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size; | |
1460 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1461 | struct ocfs2_cached_dealloc_ctxt dealloc; | |
1462 | ||
1463 | ocfs2_init_dealloc_ctxt(&dealloc); | |
1464 | ||
1465 | if (byte_len == 0) | |
1466 | return 0; | |
1467 | ||
1468 | trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start); | |
1469 | trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits; | |
1470 | if (trunc_len >= trunc_start) | |
1471 | trunc_len -= trunc_start; | |
1472 | else | |
1473 | trunc_len = 0; | |
1474 | ||
1475 | mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n", | |
1476 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1477 | (unsigned long long)byte_start, | |
1478 | (unsigned long long)byte_len, trunc_start, trunc_len); | |
1479 | ||
1480 | ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len); | |
1481 | if (ret) { | |
1482 | mlog_errno(ret); | |
1483 | goto out; | |
1484 | } | |
1485 | ||
1486 | cpos = trunc_start; | |
1487 | while (trunc_len) { | |
1488 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, | |
1489 | &alloc_size, NULL); | |
1490 | if (ret) { | |
1491 | mlog_errno(ret); | |
1492 | goto out; | |
1493 | } | |
1494 | ||
1495 | if (alloc_size > trunc_len) | |
1496 | alloc_size = trunc_len; | |
1497 | ||
1498 | /* Only do work for non-holes */ | |
1499 | if (phys_cpos != 0) { | |
1500 | ret = __ocfs2_remove_inode_range(inode, di_bh, cpos, | |
1501 | phys_cpos, alloc_size, | |
1502 | &dealloc); | |
1503 | if (ret) { | |
1504 | mlog_errno(ret); | |
1505 | goto out; | |
1506 | } | |
1507 | } | |
1508 | ||
1509 | cpos += alloc_size; | |
1510 | trunc_len -= alloc_size; | |
1511 | } | |
1512 | ||
1513 | ocfs2_truncate_cluster_pages(inode, byte_start, byte_len); | |
1514 | ||
1515 | out: | |
1516 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
1517 | ocfs2_run_deallocs(osb, &dealloc); | |
1518 | ||
1519 | return ret; | |
1520 | } | |
1521 | ||
b2580103 MF |
1522 | /* |
1523 | * Parts of this function taken from xfs_change_file_space() | |
1524 | */ | |
385820a3 MF |
1525 | static int __ocfs2_change_file_space(struct file *file, struct inode *inode, |
1526 | loff_t f_pos, unsigned int cmd, | |
1527 | struct ocfs2_space_resv *sr, | |
1528 | int change_size) | |
b2580103 MF |
1529 | { |
1530 | int ret; | |
1531 | s64 llen; | |
385820a3 | 1532 | loff_t size; |
b2580103 MF |
1533 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
1534 | struct buffer_head *di_bh = NULL; | |
1535 | handle_t *handle; | |
a00cce35 | 1536 | unsigned long long max_off = inode->i_sb->s_maxbytes; |
b2580103 | 1537 | |
b2580103 MF |
1538 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
1539 | return -EROFS; | |
1540 | ||
1541 | mutex_lock(&inode->i_mutex); | |
1542 | ||
1543 | /* | |
1544 | * This prevents concurrent writes on other nodes | |
1545 | */ | |
1546 | ret = ocfs2_rw_lock(inode, 1); | |
1547 | if (ret) { | |
1548 | mlog_errno(ret); | |
1549 | goto out; | |
1550 | } | |
1551 | ||
1552 | ret = ocfs2_meta_lock(inode, &di_bh, 1); | |
1553 | if (ret) { | |
1554 | mlog_errno(ret); | |
1555 | goto out_rw_unlock; | |
1556 | } | |
1557 | ||
1558 | if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) { | |
1559 | ret = -EPERM; | |
1560 | goto out_meta_unlock; | |
1561 | } | |
1562 | ||
1563 | switch (sr->l_whence) { | |
1564 | case 0: /*SEEK_SET*/ | |
1565 | break; | |
1566 | case 1: /*SEEK_CUR*/ | |
385820a3 | 1567 | sr->l_start += f_pos; |
b2580103 MF |
1568 | break; |
1569 | case 2: /*SEEK_END*/ | |
1570 | sr->l_start += i_size_read(inode); | |
1571 | break; | |
1572 | default: | |
1573 | ret = -EINVAL; | |
1574 | goto out_meta_unlock; | |
1575 | } | |
1576 | sr->l_whence = 0; | |
1577 | ||
1578 | llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len; | |
1579 | ||
1580 | if (sr->l_start < 0 | |
1581 | || sr->l_start > max_off | |
1582 | || (sr->l_start + llen) < 0 | |
1583 | || (sr->l_start + llen) > max_off) { | |
1584 | ret = -EINVAL; | |
1585 | goto out_meta_unlock; | |
1586 | } | |
385820a3 | 1587 | size = sr->l_start + sr->l_len; |
b2580103 MF |
1588 | |
1589 | if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) { | |
1590 | if (sr->l_len <= 0) { | |
1591 | ret = -EINVAL; | |
1592 | goto out_meta_unlock; | |
1593 | } | |
1594 | } | |
1595 | ||
385820a3 | 1596 | if (file && should_remove_suid(file->f_path.dentry)) { |
b2580103 MF |
1597 | ret = __ocfs2_write_remove_suid(inode, di_bh); |
1598 | if (ret) { | |
1599 | mlog_errno(ret); | |
1600 | goto out_meta_unlock; | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1605 | switch (cmd) { | |
1606 | case OCFS2_IOC_RESVSP: | |
1607 | case OCFS2_IOC_RESVSP64: | |
1608 | /* | |
1609 | * This takes unsigned offsets, but the signed ones we | |
1610 | * pass have been checked against overflow above. | |
1611 | */ | |
1612 | ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start, | |
1613 | sr->l_len); | |
1614 | break; | |
1615 | case OCFS2_IOC_UNRESVSP: | |
1616 | case OCFS2_IOC_UNRESVSP64: | |
1617 | ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start, | |
1618 | sr->l_len); | |
1619 | break; | |
1620 | default: | |
1621 | ret = -EINVAL; | |
1622 | } | |
1623 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1624 | if (ret) { | |
1625 | mlog_errno(ret); | |
1626 | goto out_meta_unlock; | |
1627 | } | |
1628 | ||
1629 | /* | |
1630 | * We update c/mtime for these changes | |
1631 | */ | |
1632 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
1633 | if (IS_ERR(handle)) { | |
1634 | ret = PTR_ERR(handle); | |
1635 | mlog_errno(ret); | |
1636 | goto out_meta_unlock; | |
1637 | } | |
1638 | ||
385820a3 MF |
1639 | if (change_size && i_size_read(inode) < size) |
1640 | i_size_write(inode, size); | |
1641 | ||
b2580103 MF |
1642 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
1643 | ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); | |
1644 | if (ret < 0) | |
1645 | mlog_errno(ret); | |
1646 | ||
1647 | ocfs2_commit_trans(osb, handle); | |
1648 | ||
1649 | out_meta_unlock: | |
1650 | brelse(di_bh); | |
1651 | ocfs2_meta_unlock(inode, 1); | |
1652 | out_rw_unlock: | |
1653 | ocfs2_rw_unlock(inode, 1); | |
1654 | ||
1655 | mutex_unlock(&inode->i_mutex); | |
1656 | out: | |
1657 | return ret; | |
1658 | } | |
1659 | ||
385820a3 MF |
1660 | int ocfs2_change_file_space(struct file *file, unsigned int cmd, |
1661 | struct ocfs2_space_resv *sr) | |
1662 | { | |
1663 | struct inode *inode = file->f_path.dentry->d_inode; | |
1664 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);; | |
1665 | ||
1666 | if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) && | |
1667 | !ocfs2_writes_unwritten_extents(osb)) | |
1668 | return -ENOTTY; | |
1669 | else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) && | |
1670 | !ocfs2_sparse_alloc(osb)) | |
1671 | return -ENOTTY; | |
1672 | ||
1673 | if (!S_ISREG(inode->i_mode)) | |
1674 | return -EINVAL; | |
1675 | ||
1676 | if (!(file->f_mode & FMODE_WRITE)) | |
1677 | return -EBADF; | |
1678 | ||
1679 | return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0); | |
1680 | } | |
1681 | ||
1682 | static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset, | |
1683 | loff_t len) | |
1684 | { | |
1685 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1686 | struct ocfs2_space_resv sr; | |
1687 | int change_size = 1; | |
1688 | ||
1689 | if (!ocfs2_writes_unwritten_extents(osb)) | |
1690 | return -EOPNOTSUPP; | |
1691 | ||
1692 | if (S_ISDIR(inode->i_mode)) | |
1693 | return -ENODEV; | |
1694 | ||
1695 | if (mode & FALLOC_FL_KEEP_SIZE) | |
1696 | change_size = 0; | |
1697 | ||
1698 | sr.l_whence = 0; | |
1699 | sr.l_start = (s64)offset; | |
1700 | sr.l_len = (s64)len; | |
1701 | ||
1702 | return __ocfs2_change_file_space(NULL, inode, offset, | |
1703 | OCFS2_IOC_RESVSP64, &sr, change_size); | |
1704 | } | |
1705 | ||
8659ac25 TY |
1706 | static int ocfs2_prepare_inode_for_write(struct dentry *dentry, |
1707 | loff_t *ppos, | |
1708 | size_t count, | |
9517bac6 MF |
1709 | int appending, |
1710 | int *direct_io) | |
ccd979bd | 1711 | { |
8659ac25 TY |
1712 | int ret = 0, meta_level = appending; |
1713 | struct inode *inode = dentry->d_inode; | |
ccd979bd | 1714 | u32 clusters; |
ccd979bd | 1715 | loff_t newsize, saved_pos; |
ccd979bd | 1716 | |
ccd979bd MF |
1717 | /* |
1718 | * We sample i_size under a read level meta lock to see if our write | |
1719 | * is extending the file, if it is we back off and get a write level | |
1720 | * meta lock. | |
1721 | */ | |
ccd979bd | 1722 | for(;;) { |
4bcec184 | 1723 | ret = ocfs2_meta_lock(inode, NULL, meta_level); |
ccd979bd MF |
1724 | if (ret < 0) { |
1725 | meta_level = -1; | |
1726 | mlog_errno(ret); | |
1727 | goto out; | |
1728 | } | |
1729 | ||
1730 | /* Clear suid / sgid if necessary. We do this here | |
1731 | * instead of later in the write path because | |
1732 | * remove_suid() calls ->setattr without any hint that | |
1733 | * we may have already done our cluster locking. Since | |
1734 | * ocfs2_setattr() *must* take cluster locks to | |
1735 | * proceeed, this will lead us to recursively lock the | |
1736 | * inode. There's also the dinode i_size state which | |
1737 | * can be lost via setattr during extending writes (we | |
1738 | * set inode->i_size at the end of a write. */ | |
8659ac25 | 1739 | if (should_remove_suid(dentry)) { |
ccd979bd MF |
1740 | if (meta_level == 0) { |
1741 | ocfs2_meta_unlock(inode, meta_level); | |
1742 | meta_level = 1; | |
1743 | continue; | |
1744 | } | |
1745 | ||
1746 | ret = ocfs2_write_remove_suid(inode); | |
1747 | if (ret < 0) { | |
1748 | mlog_errno(ret); | |
8659ac25 | 1749 | goto out_unlock; |
ccd979bd MF |
1750 | } |
1751 | } | |
1752 | ||
1753 | /* work on a copy of ppos until we're sure that we won't have | |
1754 | * to recalculate it due to relocking. */ | |
8659ac25 | 1755 | if (appending) { |
ccd979bd MF |
1756 | saved_pos = i_size_read(inode); |
1757 | mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos); | |
1758 | } else { | |
8659ac25 | 1759 | saved_pos = *ppos; |
ccd979bd | 1760 | } |
3a0782d0 | 1761 | |
9517bac6 MF |
1762 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
1763 | loff_t end = saved_pos + count; | |
1764 | ||
1765 | /* | |
1766 | * Skip the O_DIRECT checks if we don't need | |
1767 | * them. | |
1768 | */ | |
1769 | if (!direct_io || !(*direct_io)) | |
1770 | break; | |
1771 | ||
1772 | /* | |
1773 | * Allowing concurrent direct writes means | |
1774 | * i_size changes wouldn't be synchronized, so | |
1775 | * one node could wind up truncating another | |
1776 | * nodes writes. | |
1777 | */ | |
1778 | if (end > i_size_read(inode)) { | |
1779 | *direct_io = 0; | |
1780 | break; | |
1781 | } | |
1782 | ||
1783 | /* | |
1784 | * We don't fill holes during direct io, so | |
1785 | * check for them here. If any are found, the | |
1786 | * caller will have to retake some cluster | |
1787 | * locks and initiate the io as buffered. | |
1788 | */ | |
1789 | ret = ocfs2_check_range_for_holes(inode, saved_pos, | |
1790 | count); | |
1791 | if (ret == 1) { | |
1792 | *direct_io = 0; | |
1793 | ret = 0; | |
1794 | } else if (ret < 0) | |
1795 | mlog_errno(ret); | |
1796 | break; | |
1797 | } | |
1798 | ||
3a0782d0 MF |
1799 | /* |
1800 | * The rest of this loop is concerned with legacy file | |
1801 | * systems which don't support sparse files. | |
1802 | */ | |
3a0782d0 | 1803 | |
8659ac25 | 1804 | newsize = count + saved_pos; |
ccd979bd | 1805 | |
215c7f9f MF |
1806 | mlog(0, "pos=%lld newsize=%lld cursize=%lld\n", |
1807 | (long long) saved_pos, (long long) newsize, | |
1808 | (long long) i_size_read(inode)); | |
ccd979bd MF |
1809 | |
1810 | /* No need for a higher level metadata lock if we're | |
1811 | * never going past i_size. */ | |
1812 | if (newsize <= i_size_read(inode)) | |
1813 | break; | |
1814 | ||
1815 | if (meta_level == 0) { | |
1816 | ocfs2_meta_unlock(inode, meta_level); | |
1817 | meta_level = 1; | |
1818 | continue; | |
1819 | } | |
1820 | ||
1821 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
1822 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) - | |
1823 | OCFS2_I(inode)->ip_clusters; | |
1824 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1825 | ||
1826 | mlog(0, "Writing at EOF, may need more allocation: " | |
215c7f9f MF |
1827 | "i_size = %lld, newsize = %lld, need %u clusters\n", |
1828 | (long long) i_size_read(inode), (long long) newsize, | |
1829 | clusters); | |
ccd979bd MF |
1830 | |
1831 | /* We only want to continue the rest of this loop if | |
1832 | * our extend will actually require more | |
1833 | * allocation. */ | |
1834 | if (!clusters) | |
1835 | break; | |
1836 | ||
8659ac25 | 1837 | ret = ocfs2_extend_file(inode, NULL, newsize, count); |
ccd979bd MF |
1838 | if (ret < 0) { |
1839 | if (ret != -ENOSPC) | |
1840 | mlog_errno(ret); | |
8659ac25 | 1841 | goto out_unlock; |
ccd979bd | 1842 | } |
ccd979bd MF |
1843 | break; |
1844 | } | |
1845 | ||
8659ac25 TY |
1846 | if (appending) |
1847 | *ppos = saved_pos; | |
1848 | ||
1849 | out_unlock: | |
ccd979bd | 1850 | ocfs2_meta_unlock(inode, meta_level); |
8659ac25 TY |
1851 | |
1852 | out: | |
1853 | return ret; | |
1854 | } | |
1855 | ||
9517bac6 MF |
1856 | static inline void |
1857 | ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes) | |
1858 | { | |
1859 | const struct iovec *iov = *iovp; | |
1860 | size_t base = *basep; | |
1861 | ||
1862 | do { | |
1863 | int copy = min(bytes, iov->iov_len - base); | |
1864 | ||
1865 | bytes -= copy; | |
1866 | base += copy; | |
1867 | if (iov->iov_len == base) { | |
1868 | iov++; | |
1869 | base = 0; | |
1870 | } | |
1871 | } while (bytes); | |
1872 | *iovp = iov; | |
1873 | *basep = base; | |
1874 | } | |
1875 | ||
3a307ffc | 1876 | static struct page * ocfs2_get_write_source(char **ret_src_buf, |
9517bac6 MF |
1877 | const struct iovec *cur_iov, |
1878 | size_t iov_offset) | |
1879 | { | |
1880 | int ret; | |
3a307ffc | 1881 | char *buf = cur_iov->iov_base + iov_offset; |
9517bac6 | 1882 | struct page *src_page = NULL; |
3a307ffc | 1883 | unsigned long off; |
9517bac6 | 1884 | |
3a307ffc | 1885 | off = (unsigned long)(buf) & ~PAGE_CACHE_MASK; |
9517bac6 MF |
1886 | |
1887 | if (!segment_eq(get_fs(), KERNEL_DS)) { | |
1888 | /* | |
1889 | * Pull in the user page. We want to do this outside | |
1890 | * of the meta data locks in order to preserve locking | |
1891 | * order in case of page fault. | |
1892 | */ | |
1893 | ret = get_user_pages(current, current->mm, | |
1894 | (unsigned long)buf & PAGE_CACHE_MASK, 1, | |
1895 | 0, 0, &src_page, NULL); | |
1896 | if (ret == 1) | |
3a307ffc | 1897 | *ret_src_buf = kmap(src_page) + off; |
9517bac6 MF |
1898 | else |
1899 | src_page = ERR_PTR(-EFAULT); | |
1900 | } else { | |
3a307ffc | 1901 | *ret_src_buf = buf; |
9517bac6 MF |
1902 | } |
1903 | ||
1904 | return src_page; | |
1905 | } | |
1906 | ||
3a307ffc | 1907 | static void ocfs2_put_write_source(struct page *page) |
9517bac6 MF |
1908 | { |
1909 | if (page) { | |
1910 | kunmap(page); | |
1911 | page_cache_release(page); | |
1912 | } | |
1913 | } | |
1914 | ||
1915 | static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos, | |
1916 | const struct iovec *iov, | |
1917 | unsigned long nr_segs, | |
1918 | size_t count, | |
1919 | ssize_t o_direct_written) | |
1920 | { | |
1921 | int ret = 0; | |
1922 | ssize_t copied, total = 0; | |
3a307ffc MF |
1923 | size_t iov_offset = 0, bytes; |
1924 | loff_t pos; | |
9517bac6 | 1925 | const struct iovec *cur_iov = iov; |
3a307ffc | 1926 | struct page *user_page, *page; |
8e1c091c JG |
1927 | char * uninitialized_var(buf); |
1928 | char *dst; | |
3a307ffc | 1929 | void *fsdata; |
9517bac6 MF |
1930 | |
1931 | /* | |
1932 | * handle partial DIO write. Adjust cur_iov if needed. | |
1933 | */ | |
1934 | ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written); | |
1935 | ||
1936 | do { | |
3a307ffc | 1937 | pos = *ppos; |
9517bac6 | 1938 | |
3a307ffc MF |
1939 | user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset); |
1940 | if (IS_ERR(user_page)) { | |
1941 | ret = PTR_ERR(user_page); | |
9517bac6 MF |
1942 | goto out; |
1943 | } | |
1944 | ||
3a307ffc MF |
1945 | /* Stay within our page boundaries */ |
1946 | bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)), | |
1947 | (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK))); | |
1948 | /* Stay within the vector boundary */ | |
1949 | bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset); | |
1950 | /* Stay within count */ | |
1951 | bytes = min(bytes, count); | |
1952 | ||
1953 | page = NULL; | |
1954 | ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0, | |
1955 | &page, &fsdata); | |
1956 | if (ret) { | |
1957 | mlog_errno(ret); | |
1958 | goto out; | |
1959 | } | |
9517bac6 | 1960 | |
3a307ffc | 1961 | dst = kmap_atomic(page, KM_USER0); |
7c08d70c | 1962 | memcpy(dst + (pos & (loff_t)(PAGE_CACHE_SIZE - 1)), buf, bytes); |
3a307ffc MF |
1963 | kunmap_atomic(dst, KM_USER0); |
1964 | flush_dcache_page(page); | |
1965 | ocfs2_put_write_source(user_page); | |
9517bac6 | 1966 | |
3a307ffc MF |
1967 | copied = ocfs2_write_end(file, file->f_mapping, pos, bytes, |
1968 | bytes, page, fsdata); | |
9517bac6 MF |
1969 | if (copied < 0) { |
1970 | mlog_errno(copied); | |
1971 | ret = copied; | |
1972 | goto out; | |
1973 | } | |
1974 | ||
1975 | total += copied; | |
3a307ffc | 1976 | *ppos = pos + copied; |
9517bac6 MF |
1977 | count -= copied; |
1978 | ||
1979 | ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied); | |
1980 | } while(count); | |
1981 | ||
1982 | out: | |
1983 | return total ? total : ret; | |
1984 | } | |
1985 | ||
8659ac25 TY |
1986 | static ssize_t ocfs2_file_aio_write(struct kiocb *iocb, |
1987 | const struct iovec *iov, | |
1988 | unsigned long nr_segs, | |
1989 | loff_t pos) | |
1990 | { | |
9517bac6 MF |
1991 | int ret, direct_io, appending, rw_level, have_alloc_sem = 0; |
1992 | int can_do_direct, sync = 0; | |
1993 | ssize_t written = 0; | |
1994 | size_t ocount; /* original count */ | |
1995 | size_t count; /* after file limit checks */ | |
1996 | loff_t *ppos = &iocb->ki_pos; | |
1997 | struct file *file = iocb->ki_filp; | |
1998 | struct inode *inode = file->f_path.dentry->d_inode; | |
1999 | ||
2000 | mlog_entry("(0x%p, %u, '%.*s')\n", file, | |
8659ac25 | 2001 | (unsigned int)nr_segs, |
9517bac6 MF |
2002 | file->f_path.dentry->d_name.len, |
2003 | file->f_path.dentry->d_name.name); | |
8659ac25 | 2004 | |
8659ac25 TY |
2005 | if (iocb->ki_left == 0) |
2006 | return 0; | |
2007 | ||
d9b08b9e | 2008 | ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
9517bac6 MF |
2009 | if (ret) |
2010 | return ret; | |
2011 | ||
2012 | count = ocount; | |
2013 | ||
2014 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
2015 | ||
2016 | appending = file->f_flags & O_APPEND ? 1 : 0; | |
2017 | direct_io = file->f_flags & O_DIRECT ? 1 : 0; | |
2018 | ||
8659ac25 | 2019 | mutex_lock(&inode->i_mutex); |
9517bac6 MF |
2020 | |
2021 | relock: | |
8659ac25 | 2022 | /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */ |
9517bac6 | 2023 | if (direct_io) { |
8659ac25 | 2024 | down_read(&inode->i_alloc_sem); |
9517bac6 | 2025 | have_alloc_sem = 1; |
8659ac25 TY |
2026 | } |
2027 | ||
2028 | /* concurrent O_DIRECT writes are allowed */ | |
9517bac6 | 2029 | rw_level = !direct_io; |
8659ac25 TY |
2030 | ret = ocfs2_rw_lock(inode, rw_level); |
2031 | if (ret < 0) { | |
8659ac25 | 2032 | mlog_errno(ret); |
9517bac6 | 2033 | goto out_sems; |
8659ac25 TY |
2034 | } |
2035 | ||
9517bac6 MF |
2036 | can_do_direct = direct_io; |
2037 | ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos, | |
2038 | iocb->ki_left, appending, | |
2039 | &can_do_direct); | |
8659ac25 TY |
2040 | if (ret < 0) { |
2041 | mlog_errno(ret); | |
2042 | goto out; | |
2043 | } | |
ccd979bd | 2044 | |
9517bac6 MF |
2045 | /* |
2046 | * We can't complete the direct I/O as requested, fall back to | |
2047 | * buffered I/O. | |
2048 | */ | |
2049 | if (direct_io && !can_do_direct) { | |
2050 | ocfs2_rw_unlock(inode, rw_level); | |
2051 | up_read(&inode->i_alloc_sem); | |
2052 | ||
2053 | have_alloc_sem = 0; | |
2054 | rw_level = -1; | |
2055 | ||
2056 | direct_io = 0; | |
2057 | sync = 1; | |
2058 | goto relock; | |
2059 | } | |
2060 | ||
2061 | if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) | |
2062 | sync = 1; | |
2063 | ||
2064 | /* | |
2065 | * XXX: Is it ok to execute these checks a second time? | |
2066 | */ | |
2067 | ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode)); | |
2068 | if (ret) | |
2069 | goto out; | |
2070 | ||
2071 | /* | |
2072 | * Set pos so that sync_page_range_nolock() below understands | |
2073 | * where to start from. We might've moved it around via the | |
2074 | * calls above. The range we want to actually sync starts from | |
2075 | * *ppos here. | |
2076 | * | |
2077 | */ | |
2078 | pos = *ppos; | |
2079 | ||
ccd979bd | 2080 | /* communicate with ocfs2_dio_end_io */ |
7cdfc3a1 | 2081 | ocfs2_iocb_set_rw_locked(iocb, rw_level); |
ccd979bd | 2082 | |
9517bac6 MF |
2083 | if (direct_io) { |
2084 | written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos, | |
2085 | ppos, count, ocount); | |
2086 | if (written < 0) { | |
2087 | ret = written; | |
2088 | goto out_dio; | |
2089 | } | |
2090 | } else { | |
2091 | written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs, | |
2092 | count, written); | |
2093 | if (written < 0) { | |
2094 | ret = written; | |
2095 | if (ret != -EFAULT || ret != -ENOSPC) | |
2096 | mlog_errno(ret); | |
2097 | goto out; | |
2098 | } | |
2099 | } | |
ccd979bd | 2100 | |
9517bac6 | 2101 | out_dio: |
ccd979bd | 2102 | /* buffered aio wouldn't have proper lock coverage today */ |
9517bac6 | 2103 | BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT)); |
ccd979bd MF |
2104 | |
2105 | /* | |
2106 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io | |
2107 | * function pointer which is called when o_direct io completes so that | |
2108 | * it can unlock our rw lock. (it's the clustered equivalent of | |
2109 | * i_alloc_sem; protects truncate from racing with pending ios). | |
2110 | * Unfortunately there are error cases which call end_io and others | |
2111 | * that don't. so we don't have to unlock the rw_lock if either an | |
2112 | * async dio is going to do it in the future or an end_io after an | |
2113 | * error has already done it. | |
2114 | */ | |
2115 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { | |
2116 | rw_level = -1; | |
2117 | have_alloc_sem = 0; | |
2118 | } | |
2119 | ||
2120 | out: | |
9517bac6 MF |
2121 | if (rw_level != -1) |
2122 | ocfs2_rw_unlock(inode, rw_level); | |
2123 | ||
2124 | out_sems: | |
ccd979bd MF |
2125 | if (have_alloc_sem) |
2126 | up_read(&inode->i_alloc_sem); | |
9517bac6 MF |
2127 | |
2128 | if (written > 0 && sync) { | |
2129 | ssize_t err; | |
2130 | ||
2131 | err = sync_page_range_nolock(inode, file->f_mapping, pos, count); | |
2132 | if (err < 0) | |
2133 | written = err; | |
2134 | } | |
2135 | ||
1b1dcc1b | 2136 | mutex_unlock(&inode->i_mutex); |
ccd979bd MF |
2137 | |
2138 | mlog_exit(ret); | |
9517bac6 | 2139 | return written ? written : ret; |
ccd979bd MF |
2140 | } |
2141 | ||
6af67d82 MF |
2142 | static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe, |
2143 | struct pipe_buffer *buf, | |
2144 | struct splice_desc *sd) | |
2145 | { | |
3a307ffc | 2146 | int ret, count; |
6af67d82 | 2147 | ssize_t copied = 0; |
3a307ffc MF |
2148 | struct file *file = sd->u.file; |
2149 | unsigned int offset; | |
2150 | struct page *page = NULL; | |
2151 | void *fsdata; | |
2152 | char *src, *dst; | |
6af67d82 | 2153 | |
cac36bb0 | 2154 | ret = buf->ops->confirm(pipe, buf); |
6af67d82 MF |
2155 | if (ret) |
2156 | goto out; | |
2157 | ||
3a307ffc | 2158 | offset = sd->pos & ~PAGE_CACHE_MASK; |
6af67d82 | 2159 | count = sd->len; |
3a307ffc MF |
2160 | if (count + offset > PAGE_CACHE_SIZE) |
2161 | count = PAGE_CACHE_SIZE - offset; | |
6af67d82 | 2162 | |
3a307ffc MF |
2163 | ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0, |
2164 | &page, &fsdata); | |
2165 | if (ret) { | |
2166 | mlog_errno(ret); | |
2167 | goto out; | |
2168 | } | |
6af67d82 | 2169 | |
3a307ffc MF |
2170 | src = buf->ops->map(pipe, buf, 1); |
2171 | dst = kmap_atomic(page, KM_USER1); | |
2172 | memcpy(dst + offset, src + buf->offset, count); | |
3836df6b | 2173 | kunmap_atomic(dst, KM_USER1); |
3a307ffc | 2174 | buf->ops->unmap(pipe, buf, src); |
6af67d82 | 2175 | |
3a307ffc MF |
2176 | copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count, |
2177 | page, fsdata); | |
2178 | if (copied < 0) { | |
2179 | mlog_errno(copied); | |
2180 | ret = copied; | |
2181 | goto out; | |
2182 | } | |
6af67d82 MF |
2183 | out: |
2184 | ||
3a307ffc | 2185 | return copied ? copied : ret; |
6af67d82 MF |
2186 | } |
2187 | ||
2188 | static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe, | |
2189 | struct file *out, | |
2190 | loff_t *ppos, | |
2191 | size_t len, | |
2192 | unsigned int flags) | |
2193 | { | |
2194 | int ret, err; | |
2195 | struct address_space *mapping = out->f_mapping; | |
2196 | struct inode *inode = mapping->host; | |
c66ab6fa JA |
2197 | struct splice_desc sd = { |
2198 | .total_len = len, | |
2199 | .flags = flags, | |
2200 | .pos = *ppos, | |
6a14b90b | 2201 | .u.file = out, |
c66ab6fa JA |
2202 | }; |
2203 | ||
2204 | ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor); | |
6af67d82 MF |
2205 | if (ret > 0) { |
2206 | *ppos += ret; | |
2207 | ||
2208 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { | |
2209 | err = generic_osync_inode(inode, mapping, | |
2210 | OSYNC_METADATA|OSYNC_DATA); | |
2211 | if (err) | |
2212 | ret = err; | |
2213 | } | |
2214 | } | |
2215 | ||
2216 | return ret; | |
2217 | } | |
2218 | ||
8659ac25 TY |
2219 | static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe, |
2220 | struct file *out, | |
2221 | loff_t *ppos, | |
2222 | size_t len, | |
2223 | unsigned int flags) | |
2224 | { | |
2225 | int ret; | |
d28c9174 | 2226 | struct inode *inode = out->f_path.dentry->d_inode; |
8659ac25 TY |
2227 | |
2228 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe, | |
2229 | (unsigned int)len, | |
d28c9174 JS |
2230 | out->f_path.dentry->d_name.len, |
2231 | out->f_path.dentry->d_name.name); | |
8659ac25 TY |
2232 | |
2233 | inode_double_lock(inode, pipe->inode); | |
2234 | ||
2235 | ret = ocfs2_rw_lock(inode, 1); | |
2236 | if (ret < 0) { | |
2237 | mlog_errno(ret); | |
2238 | goto out; | |
2239 | } | |
2240 | ||
9517bac6 MF |
2241 | ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0, |
2242 | NULL); | |
8659ac25 TY |
2243 | if (ret < 0) { |
2244 | mlog_errno(ret); | |
2245 | goto out_unlock; | |
2246 | } | |
2247 | ||
2248 | /* ok, we're done with i_size and alloc work */ | |
6af67d82 | 2249 | ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags); |
8659ac25 TY |
2250 | |
2251 | out_unlock: | |
2252 | ocfs2_rw_unlock(inode, 1); | |
2253 | out: | |
2254 | inode_double_unlock(inode, pipe->inode); | |
2255 | ||
2256 | mlog_exit(ret); | |
2257 | return ret; | |
2258 | } | |
2259 | ||
2260 | static ssize_t ocfs2_file_splice_read(struct file *in, | |
2261 | loff_t *ppos, | |
2262 | struct pipe_inode_info *pipe, | |
2263 | size_t len, | |
2264 | unsigned int flags) | |
2265 | { | |
2266 | int ret = 0; | |
d28c9174 | 2267 | struct inode *inode = in->f_path.dentry->d_inode; |
8659ac25 TY |
2268 | |
2269 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe, | |
2270 | (unsigned int)len, | |
d28c9174 JS |
2271 | in->f_path.dentry->d_name.len, |
2272 | in->f_path.dentry->d_name.name); | |
8659ac25 TY |
2273 | |
2274 | /* | |
2275 | * See the comment in ocfs2_file_aio_read() | |
2276 | */ | |
2277 | ret = ocfs2_meta_lock(inode, NULL, 0); | |
2278 | if (ret < 0) { | |
2279 | mlog_errno(ret); | |
2280 | goto bail; | |
2281 | } | |
2282 | ocfs2_meta_unlock(inode, 0); | |
2283 | ||
2284 | ret = generic_file_splice_read(in, ppos, pipe, len, flags); | |
2285 | ||
2286 | bail: | |
2287 | mlog_exit(ret); | |
2288 | return ret; | |
2289 | } | |
2290 | ||
ccd979bd | 2291 | static ssize_t ocfs2_file_aio_read(struct kiocb *iocb, |
027445c3 BP |
2292 | const struct iovec *iov, |
2293 | unsigned long nr_segs, | |
ccd979bd MF |
2294 | loff_t pos) |
2295 | { | |
25899dee | 2296 | int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0; |
ccd979bd | 2297 | struct file *filp = iocb->ki_filp; |
d28c9174 | 2298 | struct inode *inode = filp->f_path.dentry->d_inode; |
ccd979bd | 2299 | |
027445c3 BP |
2300 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, |
2301 | (unsigned int)nr_segs, | |
d28c9174 JS |
2302 | filp->f_path.dentry->d_name.len, |
2303 | filp->f_path.dentry->d_name.name); | |
ccd979bd MF |
2304 | |
2305 | if (!inode) { | |
2306 | ret = -EINVAL; | |
2307 | mlog_errno(ret); | |
2308 | goto bail; | |
2309 | } | |
2310 | ||
ccd979bd MF |
2311 | /* |
2312 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads | |
2313 | * need locks to protect pending reads from racing with truncate. | |
2314 | */ | |
2315 | if (filp->f_flags & O_DIRECT) { | |
2316 | down_read(&inode->i_alloc_sem); | |
2317 | have_alloc_sem = 1; | |
2318 | ||
2319 | ret = ocfs2_rw_lock(inode, 0); | |
2320 | if (ret < 0) { | |
2321 | mlog_errno(ret); | |
2322 | goto bail; | |
2323 | } | |
2324 | rw_level = 0; | |
2325 | /* communicate with ocfs2_dio_end_io */ | |
7cdfc3a1 | 2326 | ocfs2_iocb_set_rw_locked(iocb, rw_level); |
ccd979bd MF |
2327 | } |
2328 | ||
c4374f8a MF |
2329 | /* |
2330 | * We're fine letting folks race truncates and extending | |
2331 | * writes with read across the cluster, just like they can | |
2332 | * locally. Hence no rw_lock during read. | |
2333 | * | |
2334 | * Take and drop the meta data lock to update inode fields | |
2335 | * like i_size. This allows the checks down below | |
2336 | * generic_file_aio_read() a chance of actually working. | |
2337 | */ | |
25899dee | 2338 | ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
c4374f8a MF |
2339 | if (ret < 0) { |
2340 | mlog_errno(ret); | |
2341 | goto bail; | |
2342 | } | |
25899dee | 2343 | ocfs2_meta_unlock(inode, lock_level); |
c4374f8a | 2344 | |
027445c3 | 2345 | ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos); |
ccd979bd MF |
2346 | if (ret == -EINVAL) |
2347 | mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n"); | |
2348 | ||
2349 | /* buffered aio wouldn't have proper lock coverage today */ | |
2350 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); | |
2351 | ||
2352 | /* see ocfs2_file_aio_write */ | |
2353 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { | |
2354 | rw_level = -1; | |
2355 | have_alloc_sem = 0; | |
2356 | } | |
2357 | ||
2358 | bail: | |
2359 | if (have_alloc_sem) | |
2360 | up_read(&inode->i_alloc_sem); | |
2361 | if (rw_level != -1) | |
2362 | ocfs2_rw_unlock(inode, rw_level); | |
2363 | mlog_exit(ret); | |
2364 | ||
2365 | return ret; | |
2366 | } | |
2367 | ||
92e1d5be | 2368 | const struct inode_operations ocfs2_file_iops = { |
ccd979bd MF |
2369 | .setattr = ocfs2_setattr, |
2370 | .getattr = ocfs2_getattr, | |
d38eb8db | 2371 | .permission = ocfs2_permission, |
385820a3 | 2372 | .fallocate = ocfs2_fallocate, |
ccd979bd MF |
2373 | }; |
2374 | ||
92e1d5be | 2375 | const struct inode_operations ocfs2_special_file_iops = { |
ccd979bd MF |
2376 | .setattr = ocfs2_setattr, |
2377 | .getattr = ocfs2_getattr, | |
d38eb8db | 2378 | .permission = ocfs2_permission, |
ccd979bd MF |
2379 | }; |
2380 | ||
4b6f5d20 | 2381 | const struct file_operations ocfs2_fops = { |
ccd979bd MF |
2382 | .read = do_sync_read, |
2383 | .write = do_sync_write, | |
ccd979bd MF |
2384 | .mmap = ocfs2_mmap, |
2385 | .fsync = ocfs2_sync_file, | |
2386 | .release = ocfs2_file_release, | |
2387 | .open = ocfs2_file_open, | |
2388 | .aio_read = ocfs2_file_aio_read, | |
2389 | .aio_write = ocfs2_file_aio_write, | |
ca4d147e | 2390 | .ioctl = ocfs2_ioctl, |
586d232b MF |
2391 | #ifdef CONFIG_COMPAT |
2392 | .compat_ioctl = ocfs2_compat_ioctl, | |
2393 | #endif | |
8659ac25 TY |
2394 | .splice_read = ocfs2_file_splice_read, |
2395 | .splice_write = ocfs2_file_splice_write, | |
ccd979bd MF |
2396 | }; |
2397 | ||
4b6f5d20 | 2398 | const struct file_operations ocfs2_dops = { |
ccd979bd MF |
2399 | .read = generic_read_dir, |
2400 | .readdir = ocfs2_readdir, | |
2401 | .fsync = ocfs2_sync_file, | |
ca4d147e | 2402 | .ioctl = ocfs2_ioctl, |
586d232b MF |
2403 | #ifdef CONFIG_COMPAT |
2404 | .compat_ioctl = ocfs2_compat_ioctl, | |
2405 | #endif | |
ccd979bd | 2406 | }; |