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