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> |
8659ac25 | 34 | #include <linux/pipe_fs_i.h> |
7f1a37e3 | 35 | #include <linux/mount.h> |
ccd979bd MF |
36 | |
37 | #define MLOG_MASK_PREFIX ML_INODE | |
38 | #include <cluster/masklog.h> | |
39 | ||
40 | #include "ocfs2.h" | |
41 | ||
42 | #include "alloc.h" | |
43 | #include "aops.h" | |
44 | #include "dir.h" | |
45 | #include "dlmglue.h" | |
46 | #include "extent_map.h" | |
47 | #include "file.h" | |
48 | #include "sysfile.h" | |
49 | #include "inode.h" | |
ca4d147e | 50 | #include "ioctl.h" |
ccd979bd MF |
51 | #include "journal.h" |
52 | #include "mmap.h" | |
53 | #include "suballoc.h" | |
54 | #include "super.h" | |
55 | ||
56 | #include "buffer_head_io.h" | |
57 | ||
58 | static int ocfs2_sync_inode(struct inode *inode) | |
59 | { | |
60 | filemap_fdatawrite(inode->i_mapping); | |
61 | return sync_mapping_buffers(inode->i_mapping); | |
62 | } | |
63 | ||
64 | static int ocfs2_file_open(struct inode *inode, struct file *file) | |
65 | { | |
66 | int status; | |
67 | int mode = file->f_flags; | |
68 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
69 | ||
70 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, | |
d28c9174 | 71 | file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name); |
ccd979bd MF |
72 | |
73 | spin_lock(&oi->ip_lock); | |
74 | ||
75 | /* Check that the inode hasn't been wiped from disk by another | |
76 | * node. If it hasn't then we're safe as long as we hold the | |
77 | * spin lock until our increment of open count. */ | |
78 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { | |
79 | spin_unlock(&oi->ip_lock); | |
80 | ||
81 | status = -ENOENT; | |
82 | goto leave; | |
83 | } | |
84 | ||
85 | if (mode & O_DIRECT) | |
86 | oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT; | |
87 | ||
88 | oi->ip_open_count++; | |
89 | spin_unlock(&oi->ip_lock); | |
90 | status = 0; | |
91 | leave: | |
92 | mlog_exit(status); | |
93 | return status; | |
94 | } | |
95 | ||
96 | static int ocfs2_file_release(struct inode *inode, struct file *file) | |
97 | { | |
98 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
99 | ||
100 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, | |
d28c9174 JS |
101 | file->f_path.dentry->d_name.len, |
102 | file->f_path.dentry->d_name.name); | |
ccd979bd MF |
103 | |
104 | spin_lock(&oi->ip_lock); | |
105 | if (!--oi->ip_open_count) | |
106 | oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; | |
107 | spin_unlock(&oi->ip_lock); | |
108 | ||
109 | mlog_exit(0); | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
114 | static int ocfs2_sync_file(struct file *file, | |
115 | struct dentry *dentry, | |
116 | int datasync) | |
117 | { | |
118 | int err = 0; | |
119 | journal_t *journal; | |
120 | struct inode *inode = dentry->d_inode; | |
121 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
122 | ||
123 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync, | |
124 | dentry->d_name.len, dentry->d_name.name); | |
125 | ||
126 | err = ocfs2_sync_inode(dentry->d_inode); | |
127 | if (err) | |
128 | goto bail; | |
129 | ||
130 | journal = osb->journal->j_journal; | |
131 | err = journal_force_commit(journal); | |
132 | ||
133 | bail: | |
134 | mlog_exit(err); | |
135 | ||
136 | return (err < 0) ? -EIO : 0; | |
137 | } | |
138 | ||
7f1a37e3 TY |
139 | int ocfs2_should_update_atime(struct inode *inode, |
140 | struct vfsmount *vfsmnt) | |
141 | { | |
142 | struct timespec now; | |
143 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
144 | ||
145 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) | |
146 | return 0; | |
147 | ||
148 | if ((inode->i_flags & S_NOATIME) || | |
149 | ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
150 | return 0; | |
151 | ||
6c2aad05 MF |
152 | /* |
153 | * We can be called with no vfsmnt structure - NFSD will | |
154 | * sometimes do this. | |
155 | * | |
156 | * Note that our action here is different than touch_atime() - | |
157 | * if we can't tell whether this is a noatime mount, then we | |
158 | * don't know whether to trust the value of s_atime_quantum. | |
159 | */ | |
160 | if (vfsmnt == NULL) | |
161 | return 0; | |
162 | ||
7f1a37e3 TY |
163 | if ((vfsmnt->mnt_flags & MNT_NOATIME) || |
164 | ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
165 | return 0; | |
166 | ||
7e913c53 MF |
167 | if (vfsmnt->mnt_flags & MNT_RELATIME) { |
168 | if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) || | |
169 | (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0)) | |
170 | return 1; | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
7f1a37e3 TY |
175 | now = CURRENT_TIME; |
176 | if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum)) | |
177 | return 0; | |
178 | else | |
179 | return 1; | |
180 | } | |
181 | ||
182 | int ocfs2_update_inode_atime(struct inode *inode, | |
183 | struct buffer_head *bh) | |
184 | { | |
185 | int ret; | |
186 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
187 | handle_t *handle; | |
188 | ||
189 | mlog_entry_void(); | |
190 | ||
191 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
192 | if (handle == NULL) { | |
193 | ret = -ENOMEM; | |
194 | mlog_errno(ret); | |
195 | goto out; | |
196 | } | |
197 | ||
198 | inode->i_atime = CURRENT_TIME; | |
199 | ret = ocfs2_mark_inode_dirty(handle, inode, bh); | |
200 | if (ret < 0) | |
201 | mlog_errno(ret); | |
202 | ||
203 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
204 | out: | |
205 | mlog_exit(ret); | |
206 | return ret; | |
207 | } | |
208 | ||
1fabe148 | 209 | int ocfs2_set_inode_size(handle_t *handle, |
ccd979bd MF |
210 | struct inode *inode, |
211 | struct buffer_head *fe_bh, | |
212 | u64 new_i_size) | |
213 | { | |
214 | int status; | |
215 | ||
216 | mlog_entry_void(); | |
217 | i_size_write(inode, new_i_size); | |
218 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); | |
219 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
220 | ||
221 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
222 | if (status < 0) { | |
223 | mlog_errno(status); | |
224 | goto bail; | |
225 | } | |
226 | ||
227 | bail: | |
228 | mlog_exit(status); | |
229 | return status; | |
230 | } | |
231 | ||
232 | static int ocfs2_simple_size_update(struct inode *inode, | |
233 | struct buffer_head *di_bh, | |
234 | u64 new_i_size) | |
235 | { | |
236 | int ret; | |
237 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1fabe148 | 238 | handle_t *handle = NULL; |
ccd979bd | 239 | |
65eff9cc | 240 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
241 | if (handle == NULL) { |
242 | ret = -ENOMEM; | |
243 | mlog_errno(ret); | |
244 | goto out; | |
245 | } | |
246 | ||
247 | ret = ocfs2_set_inode_size(handle, inode, di_bh, | |
248 | new_i_size); | |
249 | if (ret < 0) | |
250 | mlog_errno(ret); | |
251 | ||
02dc1af4 | 252 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
253 | out: |
254 | return ret; | |
255 | } | |
256 | ||
257 | static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb, | |
258 | struct inode *inode, | |
259 | struct buffer_head *fe_bh, | |
260 | u64 new_i_size) | |
261 | { | |
262 | int status; | |
1fabe148 | 263 | handle_t *handle; |
ccd979bd MF |
264 | |
265 | mlog_entry_void(); | |
266 | ||
267 | /* TODO: This needs to actually orphan the inode in this | |
268 | * transaction. */ | |
269 | ||
65eff9cc | 270 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
271 | if (IS_ERR(handle)) { |
272 | status = PTR_ERR(handle); | |
273 | mlog_errno(status); | |
274 | goto out; | |
275 | } | |
276 | ||
277 | status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size); | |
278 | if (status < 0) | |
279 | mlog_errno(status); | |
280 | ||
02dc1af4 | 281 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
282 | out: |
283 | mlog_exit(status); | |
284 | return status; | |
285 | } | |
286 | ||
287 | static int ocfs2_truncate_file(struct inode *inode, | |
288 | struct buffer_head *di_bh, | |
289 | u64 new_i_size) | |
290 | { | |
291 | int status = 0; | |
292 | struct ocfs2_dinode *fe = NULL; | |
293 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
294 | struct ocfs2_truncate_context *tc = NULL; | |
295 | ||
b0697053 MF |
296 | mlog_entry("(inode = %llu, new_i_size = %llu\n", |
297 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
298 | (unsigned long long)new_i_size); | |
ccd979bd MF |
299 | |
300 | truncate_inode_pages(inode->i_mapping, new_i_size); | |
301 | ||
302 | fe = (struct ocfs2_dinode *) di_bh->b_data; | |
303 | if (!OCFS2_IS_VALID_DINODE(fe)) { | |
304 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); | |
305 | status = -EIO; | |
306 | goto bail; | |
307 | } | |
308 | ||
309 | mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode), | |
b0697053 MF |
310 | "Inode %llu, inode i_size = %lld != di " |
311 | "i_size = %llu, i_flags = 0x%x\n", | |
312 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
ccd979bd | 313 | i_size_read(inode), |
b0697053 MF |
314 | (unsigned long long)le64_to_cpu(fe->i_size), |
315 | le32_to_cpu(fe->i_flags)); | |
ccd979bd MF |
316 | |
317 | if (new_i_size > le64_to_cpu(fe->i_size)) { | |
b0697053 MF |
318 | mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n", |
319 | (unsigned long long)le64_to_cpu(fe->i_size), | |
320 | (unsigned long long)new_i_size); | |
ccd979bd MF |
321 | status = -EINVAL; |
322 | mlog_errno(status); | |
323 | goto bail; | |
324 | } | |
325 | ||
b0697053 MF |
326 | mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n", |
327 | (unsigned long long)le64_to_cpu(fe->i_blkno), | |
328 | (unsigned long long)le64_to_cpu(fe->i_size), | |
329 | (unsigned long long)new_i_size); | |
ccd979bd MF |
330 | |
331 | /* lets handle the simple truncate cases before doing any more | |
332 | * cluster locking. */ | |
333 | if (new_i_size == le64_to_cpu(fe->i_size)) | |
334 | goto bail; | |
335 | ||
ab0920ce MF |
336 | /* This forces other nodes to sync and drop their pages. Do |
337 | * this even if we have a truncate without allocation change - | |
338 | * ocfs2 cluster sizes can be much greater than page size, so | |
339 | * we have to truncate them anyway. */ | |
340 | status = ocfs2_data_lock(inode, 1); | |
341 | if (status < 0) { | |
342 | mlog_errno(status); | |
343 | goto bail; | |
344 | } | |
345 | ocfs2_data_unlock(inode, 1); | |
346 | ||
ccd979bd MF |
347 | /* alright, we're going to need to do a full blown alloc size |
348 | * change. Orphan the inode so that recovery can complete the | |
349 | * truncate if necessary. This does the task of marking | |
350 | * i_size. */ | |
351 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); | |
352 | if (status < 0) { | |
353 | mlog_errno(status); | |
354 | goto bail; | |
355 | } | |
356 | ||
357 | status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); | |
358 | if (status < 0) { | |
359 | mlog_errno(status); | |
360 | goto bail; | |
361 | } | |
362 | ||
363 | status = ocfs2_commit_truncate(osb, inode, di_bh, tc); | |
364 | if (status < 0) { | |
365 | mlog_errno(status); | |
366 | goto bail; | |
367 | } | |
368 | ||
369 | /* TODO: orphan dir cleanup here. */ | |
370 | bail: | |
371 | ||
372 | mlog_exit(status); | |
373 | return status; | |
374 | } | |
375 | ||
376 | /* | |
377 | * extend allocation only here. | |
378 | * we'll update all the disk stuff, and oip->alloc_size | |
379 | * | |
380 | * expect stuff to be locked, a transaction started and enough data / | |
381 | * metadata reservations in the contexts. | |
382 | * | |
383 | * Will return -EAGAIN, and a reason if a restart is needed. | |
384 | * If passed in, *reason will always be set, even in error. | |
385 | */ | |
386 | int ocfs2_do_extend_allocation(struct ocfs2_super *osb, | |
387 | struct inode *inode, | |
dcd0538f | 388 | u32 *logical_offset, |
ccd979bd MF |
389 | u32 clusters_to_add, |
390 | struct buffer_head *fe_bh, | |
1fabe148 | 391 | handle_t *handle, |
ccd979bd MF |
392 | struct ocfs2_alloc_context *data_ac, |
393 | struct ocfs2_alloc_context *meta_ac, | |
394 | enum ocfs2_alloc_restarted *reason_ret) | |
395 | { | |
396 | int status = 0; | |
397 | int free_extents; | |
398 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; | |
399 | enum ocfs2_alloc_restarted reason = RESTART_NONE; | |
400 | u32 bit_off, num_bits; | |
401 | u64 block; | |
402 | ||
403 | BUG_ON(!clusters_to_add); | |
404 | ||
405 | free_extents = ocfs2_num_free_extents(osb, inode, fe); | |
406 | if (free_extents < 0) { | |
407 | status = free_extents; | |
408 | mlog_errno(status); | |
409 | goto leave; | |
410 | } | |
411 | ||
412 | /* there are two cases which could cause us to EAGAIN in the | |
413 | * we-need-more-metadata case: | |
414 | * 1) we haven't reserved *any* | |
415 | * 2) we are so fragmented, we've needed to add metadata too | |
416 | * many times. */ | |
417 | if (!free_extents && !meta_ac) { | |
418 | mlog(0, "we haven't reserved any metadata!\n"); | |
419 | status = -EAGAIN; | |
420 | reason = RESTART_META; | |
421 | goto leave; | |
422 | } else if ((!free_extents) | |
423 | && (ocfs2_alloc_context_bits_left(meta_ac) | |
424 | < ocfs2_extend_meta_needed(fe))) { | |
425 | mlog(0, "filesystem is really fragmented...\n"); | |
426 | status = -EAGAIN; | |
427 | reason = RESTART_META; | |
428 | goto leave; | |
429 | } | |
430 | ||
431 | status = ocfs2_claim_clusters(osb, handle, data_ac, 1, | |
432 | &bit_off, &num_bits); | |
433 | if (status < 0) { | |
434 | if (status != -ENOSPC) | |
435 | mlog_errno(status); | |
436 | goto leave; | |
437 | } | |
438 | ||
439 | BUG_ON(num_bits > clusters_to_add); | |
440 | ||
441 | /* reserve our write early -- insert_extent may update the inode */ | |
442 | status = ocfs2_journal_access(handle, inode, fe_bh, | |
443 | OCFS2_JOURNAL_ACCESS_WRITE); | |
444 | if (status < 0) { | |
445 | mlog_errno(status); | |
446 | goto leave; | |
447 | } | |
448 | ||
449 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); | |
b0697053 MF |
450 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
451 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
dcd0538f MF |
452 | status = ocfs2_insert_extent(osb, handle, inode, fe_bh, |
453 | *logical_offset, block, num_bits, | |
454 | meta_ac); | |
ccd979bd MF |
455 | if (status < 0) { |
456 | mlog_errno(status); | |
457 | goto leave; | |
458 | } | |
459 | ||
ccd979bd MF |
460 | status = ocfs2_journal_dirty(handle, fe_bh); |
461 | if (status < 0) { | |
462 | mlog_errno(status); | |
463 | goto leave; | |
464 | } | |
465 | ||
466 | clusters_to_add -= num_bits; | |
dcd0538f | 467 | *logical_offset += num_bits; |
ccd979bd MF |
468 | |
469 | if (clusters_to_add) { | |
470 | mlog(0, "need to alloc once more, clusters = %u, wanted = " | |
471 | "%u\n", fe->i_clusters, clusters_to_add); | |
472 | status = -EAGAIN; | |
473 | reason = RESTART_TRANS; | |
474 | } | |
475 | ||
476 | leave: | |
477 | mlog_exit(status); | |
478 | if (reason_ret) | |
479 | *reason_ret = reason; | |
480 | return status; | |
481 | } | |
482 | ||
abf8b156 MF |
483 | /* |
484 | * For a given allocation, determine which allocators will need to be | |
485 | * accessed, and lock them, reserving the appropriate number of bits. | |
486 | * | |
487 | * Called from ocfs2_extend_allocation() for file systems which don't | |
488 | * support holes, and from ocfs2_prepare_write() for file systems | |
489 | * which understand sparse inodes. | |
490 | */ | |
491 | static int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, | |
492 | u32 clusters_to_add, | |
493 | struct ocfs2_alloc_context **data_ac, | |
494 | struct ocfs2_alloc_context **meta_ac) | |
495 | { | |
496 | int ret, num_free_extents; | |
497 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
498 | ||
499 | *meta_ac = NULL; | |
500 | *data_ac = NULL; | |
501 | ||
502 | mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " | |
503 | "clusters_to_add = %u\n", | |
504 | (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode), | |
505 | le32_to_cpu(di->i_clusters), clusters_to_add); | |
506 | ||
507 | num_free_extents = ocfs2_num_free_extents(osb, inode, di); | |
508 | if (num_free_extents < 0) { | |
509 | ret = num_free_extents; | |
510 | mlog_errno(ret); | |
511 | goto out; | |
512 | } | |
513 | ||
514 | /* | |
515 | * Sparse allocation file systems need to be more conservative | |
516 | * with reserving room for expansion - the actual allocation | |
517 | * happens while we've got a journal handle open so re-taking | |
518 | * a cluster lock (because we ran out of room for another | |
519 | * extent) will violate ordering rules. | |
520 | * | |
521 | * Most of the time we'll only be seeing this 1 page at a time | |
522 | * anyway. | |
523 | */ | |
524 | if (!num_free_extents || | |
525 | (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) { | |
526 | ret = ocfs2_reserve_new_metadata(osb, di, meta_ac); | |
527 | if (ret < 0) { | |
528 | if (ret != -ENOSPC) | |
529 | mlog_errno(ret); | |
530 | goto out; | |
531 | } | |
532 | } | |
533 | ||
534 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); | |
535 | if (ret < 0) { | |
536 | if (ret != -ENOSPC) | |
537 | mlog_errno(ret); | |
538 | goto out; | |
539 | } | |
540 | ||
541 | out: | |
542 | if (ret) { | |
543 | if (*meta_ac) { | |
544 | ocfs2_free_alloc_context(*meta_ac); | |
545 | *meta_ac = NULL; | |
546 | } | |
547 | ||
548 | /* | |
549 | * We cannot have an error and a non null *data_ac. | |
550 | */ | |
551 | } | |
552 | ||
553 | return ret; | |
554 | } | |
555 | ||
ccd979bd MF |
556 | static int ocfs2_extend_allocation(struct inode *inode, |
557 | u32 clusters_to_add) | |
558 | { | |
559 | int status = 0; | |
560 | int restart_func = 0; | |
561 | int drop_alloc_sem = 0; | |
abf8b156 | 562 | int credits; |
dcd0538f | 563 | u32 prev_clusters, logical_start; |
ccd979bd MF |
564 | struct buffer_head *bh = NULL; |
565 | struct ocfs2_dinode *fe = NULL; | |
1fabe148 | 566 | handle_t *handle = NULL; |
ccd979bd MF |
567 | struct ocfs2_alloc_context *data_ac = NULL; |
568 | struct ocfs2_alloc_context *meta_ac = NULL; | |
569 | enum ocfs2_alloc_restarted why; | |
570 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
571 | ||
572 | mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); | |
573 | ||
dcd0538f MF |
574 | /* |
575 | * This function only exists for file systems which don't | |
576 | * support holes. | |
577 | */ | |
578 | BUG_ON(ocfs2_sparse_alloc(osb)); | |
579 | ||
ccd979bd MF |
580 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, |
581 | OCFS2_BH_CACHED, inode); | |
582 | if (status < 0) { | |
583 | mlog_errno(status); | |
584 | goto leave; | |
585 | } | |
586 | ||
587 | fe = (struct ocfs2_dinode *) bh->b_data; | |
588 | if (!OCFS2_IS_VALID_DINODE(fe)) { | |
589 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); | |
590 | status = -EIO; | |
591 | goto leave; | |
592 | } | |
593 | ||
dcd0538f MF |
594 | logical_start = OCFS2_I(inode)->ip_clusters; |
595 | ||
ccd979bd MF |
596 | restart_all: |
597 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); | |
598 | ||
abf8b156 MF |
599 | status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac, |
600 | &meta_ac); | |
601 | if (status) { | |
ccd979bd MF |
602 | mlog_errno(status); |
603 | goto leave; | |
604 | } | |
605 | ||
ccd979bd | 606 | /* blocks peope in read/write from reading our allocation |
1b1dcc1b | 607 | * until we're done changing it. We depend on i_mutex to block |
ccd979bd MF |
608 | * other extend/truncate calls while we're here. Ordering wrt |
609 | * start_trans is important here -- always do it before! */ | |
610 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | |
611 | drop_alloc_sem = 1; | |
612 | ||
613 | credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); | |
65eff9cc | 614 | handle = ocfs2_start_trans(osb, credits); |
ccd979bd MF |
615 | if (IS_ERR(handle)) { |
616 | status = PTR_ERR(handle); | |
617 | handle = NULL; | |
618 | mlog_errno(status); | |
619 | goto leave; | |
620 | } | |
621 | ||
622 | restarted_transaction: | |
623 | /* reserve a write to the file entry early on - that we if we | |
624 | * run out of credits in the allocation path, we can still | |
625 | * update i_size. */ | |
626 | status = ocfs2_journal_access(handle, inode, bh, | |
627 | OCFS2_JOURNAL_ACCESS_WRITE); | |
628 | if (status < 0) { | |
629 | mlog_errno(status); | |
630 | goto leave; | |
631 | } | |
632 | ||
633 | prev_clusters = OCFS2_I(inode)->ip_clusters; | |
634 | ||
635 | status = ocfs2_do_extend_allocation(osb, | |
636 | inode, | |
dcd0538f | 637 | &logical_start, |
ccd979bd MF |
638 | clusters_to_add, |
639 | bh, | |
640 | handle, | |
641 | data_ac, | |
642 | meta_ac, | |
643 | &why); | |
644 | if ((status < 0) && (status != -EAGAIN)) { | |
645 | if (status != -ENOSPC) | |
646 | mlog_errno(status); | |
647 | goto leave; | |
648 | } | |
649 | ||
650 | status = ocfs2_journal_dirty(handle, bh); | |
651 | if (status < 0) { | |
652 | mlog_errno(status); | |
653 | goto leave; | |
654 | } | |
655 | ||
656 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
657 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); | |
658 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
659 | ||
660 | if (why != RESTART_NONE && clusters_to_add) { | |
661 | if (why == RESTART_META) { | |
662 | mlog(0, "restarting function.\n"); | |
663 | restart_func = 1; | |
664 | } else { | |
665 | BUG_ON(why != RESTART_TRANS); | |
666 | ||
667 | mlog(0, "restarting transaction.\n"); | |
668 | /* TODO: This can be more intelligent. */ | |
669 | credits = ocfs2_calc_extend_credits(osb->sb, | |
670 | fe, | |
671 | clusters_to_add); | |
1fabe148 | 672 | status = ocfs2_extend_trans(handle, credits); |
ccd979bd MF |
673 | if (status < 0) { |
674 | /* handle still has to be committed at | |
675 | * this point. */ | |
676 | status = -ENOMEM; | |
677 | mlog_errno(status); | |
678 | goto leave; | |
679 | } | |
680 | goto restarted_transaction; | |
681 | } | |
682 | } | |
683 | ||
b0697053 MF |
684 | mlog(0, "fe: i_clusters = %u, i_size=%llu\n", |
685 | fe->i_clusters, (unsigned long long)fe->i_size); | |
ccd979bd MF |
686 | mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", |
687 | OCFS2_I(inode)->ip_clusters, i_size_read(inode)); | |
688 | ||
689 | leave: | |
690 | if (drop_alloc_sem) { | |
691 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | |
692 | drop_alloc_sem = 0; | |
693 | } | |
694 | if (handle) { | |
02dc1af4 | 695 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
696 | handle = NULL; |
697 | } | |
698 | if (data_ac) { | |
699 | ocfs2_free_alloc_context(data_ac); | |
700 | data_ac = NULL; | |
701 | } | |
702 | if (meta_ac) { | |
703 | ocfs2_free_alloc_context(meta_ac); | |
704 | meta_ac = NULL; | |
705 | } | |
706 | if ((!status) && restart_func) { | |
707 | restart_func = 0; | |
708 | goto restart_all; | |
709 | } | |
710 | if (bh) { | |
711 | brelse(bh); | |
712 | bh = NULL; | |
713 | } | |
714 | ||
715 | mlog_exit(status); | |
716 | return status; | |
717 | } | |
718 | ||
719 | /* Some parts of this taken from generic_cont_expand, which turned out | |
720 | * to be too fragile to do exactly what we need without us having to | |
53013cba MF |
721 | * worry about recursive locking in ->prepare_write() and |
722 | * ->commit_write(). */ | |
ccd979bd MF |
723 | static int ocfs2_write_zero_page(struct inode *inode, |
724 | u64 size) | |
725 | { | |
726 | struct address_space *mapping = inode->i_mapping; | |
727 | struct page *page; | |
728 | unsigned long index; | |
729 | unsigned int offset; | |
1fabe148 | 730 | handle_t *handle = NULL; |
ccd979bd MF |
731 | int ret; |
732 | ||
733 | offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */ | |
734 | /* ugh. in prepare/commit_write, if from==to==start of block, we | |
735 | ** skip the prepare. make sure we never send an offset for the start | |
736 | ** of a block | |
737 | */ | |
738 | if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { | |
739 | offset++; | |
740 | } | |
741 | index = size >> PAGE_CACHE_SHIFT; | |
742 | ||
743 | page = grab_cache_page(mapping, index); | |
744 | if (!page) { | |
745 | ret = -ENOMEM; | |
746 | mlog_errno(ret); | |
747 | goto out; | |
748 | } | |
749 | ||
53013cba | 750 | ret = ocfs2_prepare_write_nolock(inode, page, offset, offset); |
ccd979bd MF |
751 | if (ret < 0) { |
752 | mlog_errno(ret); | |
753 | goto out_unlock; | |
754 | } | |
755 | ||
756 | if (ocfs2_should_order_data(inode)) { | |
757 | handle = ocfs2_start_walk_page_trans(inode, page, offset, | |
758 | offset); | |
759 | if (IS_ERR(handle)) { | |
760 | ret = PTR_ERR(handle); | |
761 | handle = NULL; | |
762 | goto out_unlock; | |
763 | } | |
764 | } | |
765 | ||
766 | /* must not update i_size! */ | |
767 | ret = block_commit_write(page, offset, offset); | |
768 | if (ret < 0) | |
769 | mlog_errno(ret); | |
770 | else | |
771 | ret = 0; | |
772 | ||
773 | if (handle) | |
02dc1af4 | 774 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
ccd979bd MF |
775 | out_unlock: |
776 | unlock_page(page); | |
777 | page_cache_release(page); | |
778 | out: | |
779 | return ret; | |
780 | } | |
781 | ||
782 | static int ocfs2_zero_extend(struct inode *inode, | |
783 | u64 zero_to_size) | |
784 | { | |
785 | int ret = 0; | |
786 | u64 start_off; | |
787 | struct super_block *sb = inode->i_sb; | |
788 | ||
789 | start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); | |
790 | while (start_off < zero_to_size) { | |
791 | ret = ocfs2_write_zero_page(inode, start_off); | |
792 | if (ret < 0) { | |
793 | mlog_errno(ret); | |
794 | goto out; | |
795 | } | |
796 | ||
797 | start_off += sb->s_blocksize; | |
e2057c5a MF |
798 | |
799 | /* | |
800 | * Very large extends have the potential to lock up | |
801 | * the cpu for extended periods of time. | |
802 | */ | |
803 | cond_resched(); | |
ccd979bd MF |
804 | } |
805 | ||
806 | out: | |
807 | return ret; | |
808 | } | |
809 | ||
53013cba MF |
810 | /* |
811 | * A tail_to_skip value > 0 indicates that we're being called from | |
812 | * ocfs2_file_aio_write(). This has the following implications: | |
813 | * | |
814 | * - we don't want to update i_size | |
815 | * - di_bh will be NULL, which is fine because it's only used in the | |
816 | * case where we want to update i_size. | |
817 | * - ocfs2_zero_extend() will then only be filling the hole created | |
818 | * between i_size and the start of the write. | |
819 | */ | |
ccd979bd MF |
820 | static int ocfs2_extend_file(struct inode *inode, |
821 | struct buffer_head *di_bh, | |
53013cba MF |
822 | u64 new_i_size, |
823 | size_t tail_to_skip) | |
ccd979bd MF |
824 | { |
825 | int ret = 0; | |
3a0782d0 | 826 | u32 clusters_to_add = 0; |
ccd979bd | 827 | |
53013cba MF |
828 | BUG_ON(!tail_to_skip && !di_bh); |
829 | ||
ccd979bd MF |
830 | /* setattr sometimes calls us like this. */ |
831 | if (new_i_size == 0) | |
832 | goto out; | |
833 | ||
834 | if (i_size_read(inode) == new_i_size) | |
835 | goto out; | |
836 | BUG_ON(new_i_size < i_size_read(inode)); | |
837 | ||
3a0782d0 MF |
838 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
839 | BUG_ON(tail_to_skip != 0); | |
840 | goto out_update_size; | |
841 | } | |
842 | ||
ccd979bd MF |
843 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - |
844 | OCFS2_I(inode)->ip_clusters; | |
845 | ||
0effef77 MF |
846 | /* |
847 | * protect the pages that ocfs2_zero_extend is going to be | |
848 | * pulling into the page cache.. we do this before the | |
849 | * metadata extend so that we don't get into the situation | |
850 | * where we've extended the metadata but can't get the data | |
851 | * lock to zero. | |
852 | */ | |
853 | ret = ocfs2_data_lock(inode, 1); | |
854 | if (ret < 0) { | |
855 | mlog_errno(ret); | |
856 | goto out; | |
857 | } | |
ccd979bd | 858 | |
0effef77 | 859 | if (clusters_to_add) { |
53013cba | 860 | ret = ocfs2_extend_allocation(inode, clusters_to_add); |
ccd979bd MF |
861 | if (ret < 0) { |
862 | mlog_errno(ret); | |
53013cba | 863 | goto out_unlock; |
ccd979bd | 864 | } |
0effef77 | 865 | } |
ccd979bd | 866 | |
0effef77 MF |
867 | /* |
868 | * Call this even if we don't add any clusters to the tree. We | |
869 | * still need to zero the area between the old i_size and the | |
870 | * new i_size. | |
871 | */ | |
872 | ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip); | |
873 | if (ret < 0) { | |
874 | mlog_errno(ret); | |
875 | goto out_unlock; | |
53013cba MF |
876 | } |
877 | ||
3a0782d0 | 878 | out_update_size: |
53013cba MF |
879 | if (!tail_to_skip) { |
880 | /* We're being called from ocfs2_setattr() which wants | |
881 | * us to update i_size */ | |
882 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); | |
883 | if (ret < 0) | |
884 | mlog_errno(ret); | |
ccd979bd MF |
885 | } |
886 | ||
53013cba | 887 | out_unlock: |
3a0782d0 MF |
888 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) |
889 | ocfs2_data_unlock(inode, 1); | |
53013cba | 890 | |
ccd979bd MF |
891 | out: |
892 | return ret; | |
893 | } | |
894 | ||
895 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) | |
896 | { | |
897 | int status = 0, size_change; | |
898 | struct inode *inode = dentry->d_inode; | |
899 | struct super_block *sb = inode->i_sb; | |
900 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
901 | struct buffer_head *bh = NULL; | |
1fabe148 | 902 | handle_t *handle = NULL; |
ccd979bd MF |
903 | |
904 | mlog_entry("(0x%p, '%.*s')\n", dentry, | |
905 | dentry->d_name.len, dentry->d_name.name); | |
906 | ||
907 | if (attr->ia_valid & ATTR_MODE) | |
908 | mlog(0, "mode change: %d\n", attr->ia_mode); | |
909 | if (attr->ia_valid & ATTR_UID) | |
910 | mlog(0, "uid change: %d\n", attr->ia_uid); | |
911 | if (attr->ia_valid & ATTR_GID) | |
912 | mlog(0, "gid change: %d\n", attr->ia_gid); | |
913 | if (attr->ia_valid & ATTR_SIZE) | |
914 | mlog(0, "size change...\n"); | |
915 | if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) | |
916 | mlog(0, "time change...\n"); | |
917 | ||
918 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ | |
919 | | ATTR_GID | ATTR_UID | ATTR_MODE) | |
920 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) { | |
921 | mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid); | |
922 | return 0; | |
923 | } | |
924 | ||
925 | status = inode_change_ok(inode, attr); | |
926 | if (status) | |
927 | return status; | |
928 | ||
929 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; | |
930 | if (size_change) { | |
931 | status = ocfs2_rw_lock(inode, 1); | |
932 | if (status < 0) { | |
933 | mlog_errno(status); | |
934 | goto bail; | |
935 | } | |
936 | } | |
937 | ||
4bcec184 | 938 | status = ocfs2_meta_lock(inode, &bh, 1); |
ccd979bd MF |
939 | if (status < 0) { |
940 | if (status != -ENOENT) | |
941 | mlog_errno(status); | |
942 | goto bail_unlock_rw; | |
943 | } | |
944 | ||
945 | if (size_change && attr->ia_size != i_size_read(inode)) { | |
946 | if (i_size_read(inode) > attr->ia_size) | |
947 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); | |
948 | else | |
53013cba | 949 | status = ocfs2_extend_file(inode, bh, attr->ia_size, 0); |
ccd979bd MF |
950 | if (status < 0) { |
951 | if (status != -ENOSPC) | |
952 | mlog_errno(status); | |
953 | status = -ENOSPC; | |
954 | goto bail_unlock; | |
955 | } | |
956 | } | |
957 | ||
65eff9cc | 958 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
959 | if (IS_ERR(handle)) { |
960 | status = PTR_ERR(handle); | |
961 | mlog_errno(status); | |
962 | goto bail_unlock; | |
963 | } | |
964 | ||
965 | status = inode_setattr(inode, attr); | |
966 | if (status < 0) { | |
967 | mlog_errno(status); | |
968 | goto bail_commit; | |
969 | } | |
970 | ||
971 | status = ocfs2_mark_inode_dirty(handle, inode, bh); | |
972 | if (status < 0) | |
973 | mlog_errno(status); | |
974 | ||
975 | bail_commit: | |
02dc1af4 | 976 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
977 | bail_unlock: |
978 | ocfs2_meta_unlock(inode, 1); | |
979 | bail_unlock_rw: | |
980 | if (size_change) | |
981 | ocfs2_rw_unlock(inode, 1); | |
982 | bail: | |
983 | if (bh) | |
984 | brelse(bh); | |
985 | ||
986 | mlog_exit(status); | |
987 | return status; | |
988 | } | |
989 | ||
990 | int ocfs2_getattr(struct vfsmount *mnt, | |
991 | struct dentry *dentry, | |
992 | struct kstat *stat) | |
993 | { | |
994 | struct inode *inode = dentry->d_inode; | |
995 | struct super_block *sb = dentry->d_inode->i_sb; | |
996 | struct ocfs2_super *osb = sb->s_fs_info; | |
997 | int err; | |
998 | ||
999 | mlog_entry_void(); | |
1000 | ||
1001 | err = ocfs2_inode_revalidate(dentry); | |
1002 | if (err) { | |
1003 | if (err != -ENOENT) | |
1004 | mlog_errno(err); | |
1005 | goto bail; | |
1006 | } | |
1007 | ||
1008 | generic_fillattr(inode, stat); | |
1009 | ||
1010 | /* We set the blksize from the cluster size for performance */ | |
1011 | stat->blksize = osb->s_clustersize; | |
1012 | ||
1013 | bail: | |
1014 | mlog_exit(err); | |
1015 | ||
1016 | return err; | |
1017 | } | |
1018 | ||
d38eb8db TY |
1019 | int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd) |
1020 | { | |
1021 | int ret; | |
1022 | ||
1023 | mlog_entry_void(); | |
1024 | ||
1025 | ret = ocfs2_meta_lock(inode, NULL, 0); | |
1026 | if (ret) { | |
a9f5f707 MF |
1027 | if (ret != -ENOENT) |
1028 | mlog_errno(ret); | |
d38eb8db TY |
1029 | goto out; |
1030 | } | |
1031 | ||
1032 | ret = generic_permission(inode, mask, NULL); | |
d38eb8db TY |
1033 | |
1034 | ocfs2_meta_unlock(inode, 0); | |
1035 | out: | |
1036 | mlog_exit(ret); | |
1037 | return ret; | |
1038 | } | |
1039 | ||
ccd979bd MF |
1040 | static int ocfs2_write_remove_suid(struct inode *inode) |
1041 | { | |
1042 | int ret; | |
1043 | struct buffer_head *bh = NULL; | |
1044 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1fabe148 | 1045 | handle_t *handle; |
ccd979bd MF |
1046 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
1047 | struct ocfs2_dinode *di; | |
1048 | ||
b0697053 MF |
1049 | mlog_entry("(Inode %llu, mode 0%o)\n", |
1050 | (unsigned long long)oi->ip_blkno, inode->i_mode); | |
ccd979bd | 1051 | |
65eff9cc | 1052 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
1053 | if (handle == NULL) { |
1054 | ret = -ENOMEM; | |
1055 | mlog_errno(ret); | |
1056 | goto out; | |
1057 | } | |
1058 | ||
1059 | ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode); | |
1060 | if (ret < 0) { | |
1061 | mlog_errno(ret); | |
1062 | goto out_trans; | |
1063 | } | |
1064 | ||
1065 | ret = ocfs2_journal_access(handle, inode, bh, | |
1066 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1067 | if (ret < 0) { | |
1068 | mlog_errno(ret); | |
1069 | goto out_bh; | |
1070 | } | |
1071 | ||
1072 | inode->i_mode &= ~S_ISUID; | |
1073 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) | |
1074 | inode->i_mode &= ~S_ISGID; | |
1075 | ||
1076 | di = (struct ocfs2_dinode *) bh->b_data; | |
1077 | di->i_mode = cpu_to_le16(inode->i_mode); | |
1078 | ||
1079 | ret = ocfs2_journal_dirty(handle, bh); | |
1080 | if (ret < 0) | |
1081 | mlog_errno(ret); | |
1082 | out_bh: | |
1083 | brelse(bh); | |
1084 | out_trans: | |
02dc1af4 | 1085 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
1086 | out: |
1087 | mlog_exit(ret); | |
1088 | return ret; | |
1089 | } | |
1090 | ||
8659ac25 TY |
1091 | static int ocfs2_prepare_inode_for_write(struct dentry *dentry, |
1092 | loff_t *ppos, | |
1093 | size_t count, | |
1094 | int appending) | |
ccd979bd | 1095 | { |
8659ac25 TY |
1096 | int ret = 0, meta_level = appending; |
1097 | struct inode *inode = dentry->d_inode; | |
ccd979bd | 1098 | u32 clusters; |
ccd979bd | 1099 | loff_t newsize, saved_pos; |
ccd979bd | 1100 | |
ccd979bd MF |
1101 | /* |
1102 | * We sample i_size under a read level meta lock to see if our write | |
1103 | * is extending the file, if it is we back off and get a write level | |
1104 | * meta lock. | |
1105 | */ | |
ccd979bd | 1106 | for(;;) { |
4bcec184 | 1107 | ret = ocfs2_meta_lock(inode, NULL, meta_level); |
ccd979bd MF |
1108 | if (ret < 0) { |
1109 | meta_level = -1; | |
1110 | mlog_errno(ret); | |
1111 | goto out; | |
1112 | } | |
1113 | ||
1114 | /* Clear suid / sgid if necessary. We do this here | |
1115 | * instead of later in the write path because | |
1116 | * remove_suid() calls ->setattr without any hint that | |
1117 | * we may have already done our cluster locking. Since | |
1118 | * ocfs2_setattr() *must* take cluster locks to | |
1119 | * proceeed, this will lead us to recursively lock the | |
1120 | * inode. There's also the dinode i_size state which | |
1121 | * can be lost via setattr during extending writes (we | |
1122 | * set inode->i_size at the end of a write. */ | |
8659ac25 | 1123 | if (should_remove_suid(dentry)) { |
ccd979bd MF |
1124 | if (meta_level == 0) { |
1125 | ocfs2_meta_unlock(inode, meta_level); | |
1126 | meta_level = 1; | |
1127 | continue; | |
1128 | } | |
1129 | ||
1130 | ret = ocfs2_write_remove_suid(inode); | |
1131 | if (ret < 0) { | |
1132 | mlog_errno(ret); | |
8659ac25 | 1133 | goto out_unlock; |
ccd979bd MF |
1134 | } |
1135 | } | |
1136 | ||
1137 | /* work on a copy of ppos until we're sure that we won't have | |
1138 | * to recalculate it due to relocking. */ | |
8659ac25 | 1139 | if (appending) { |
ccd979bd MF |
1140 | saved_pos = i_size_read(inode); |
1141 | mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos); | |
1142 | } else { | |
8659ac25 | 1143 | saved_pos = *ppos; |
ccd979bd | 1144 | } |
3a0782d0 MF |
1145 | |
1146 | /* | |
1147 | * The rest of this loop is concerned with legacy file | |
1148 | * systems which don't support sparse files. | |
1149 | */ | |
1150 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) | |
1151 | break; | |
1152 | ||
8659ac25 | 1153 | newsize = count + saved_pos; |
ccd979bd | 1154 | |
215c7f9f MF |
1155 | mlog(0, "pos=%lld newsize=%lld cursize=%lld\n", |
1156 | (long long) saved_pos, (long long) newsize, | |
1157 | (long long) i_size_read(inode)); | |
ccd979bd MF |
1158 | |
1159 | /* No need for a higher level metadata lock if we're | |
1160 | * never going past i_size. */ | |
1161 | if (newsize <= i_size_read(inode)) | |
1162 | break; | |
1163 | ||
1164 | if (meta_level == 0) { | |
1165 | ocfs2_meta_unlock(inode, meta_level); | |
1166 | meta_level = 1; | |
1167 | continue; | |
1168 | } | |
1169 | ||
1170 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
1171 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) - | |
1172 | OCFS2_I(inode)->ip_clusters; | |
1173 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1174 | ||
1175 | mlog(0, "Writing at EOF, may need more allocation: " | |
215c7f9f MF |
1176 | "i_size = %lld, newsize = %lld, need %u clusters\n", |
1177 | (long long) i_size_read(inode), (long long) newsize, | |
1178 | clusters); | |
ccd979bd MF |
1179 | |
1180 | /* We only want to continue the rest of this loop if | |
1181 | * our extend will actually require more | |
1182 | * allocation. */ | |
1183 | if (!clusters) | |
1184 | break; | |
1185 | ||
8659ac25 | 1186 | ret = ocfs2_extend_file(inode, NULL, newsize, count); |
ccd979bd MF |
1187 | if (ret < 0) { |
1188 | if (ret != -ENOSPC) | |
1189 | mlog_errno(ret); | |
8659ac25 | 1190 | goto out_unlock; |
ccd979bd | 1191 | } |
ccd979bd MF |
1192 | break; |
1193 | } | |
1194 | ||
8659ac25 TY |
1195 | if (appending) |
1196 | *ppos = saved_pos; | |
1197 | ||
1198 | out_unlock: | |
ccd979bd | 1199 | ocfs2_meta_unlock(inode, meta_level); |
8659ac25 TY |
1200 | |
1201 | out: | |
1202 | return ret; | |
1203 | } | |
1204 | ||
1205 | static ssize_t ocfs2_file_aio_write(struct kiocb *iocb, | |
1206 | const struct iovec *iov, | |
1207 | unsigned long nr_segs, | |
1208 | loff_t pos) | |
1209 | { | |
1210 | int ret, rw_level, have_alloc_sem = 0; | |
1211 | struct file *filp = iocb->ki_filp; | |
d28c9174 | 1212 | struct inode *inode = filp->f_path.dentry->d_inode; |
8659ac25 TY |
1213 | int appending = filp->f_flags & O_APPEND ? 1 : 0; |
1214 | ||
1215 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, | |
1216 | (unsigned int)nr_segs, | |
d28c9174 JS |
1217 | filp->f_path.dentry->d_name.len, |
1218 | filp->f_path.dentry->d_name.name); | |
8659ac25 TY |
1219 | |
1220 | /* happy write of zero bytes */ | |
1221 | if (iocb->ki_left == 0) | |
1222 | return 0; | |
1223 | ||
1224 | mutex_lock(&inode->i_mutex); | |
1225 | /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */ | |
1226 | if (filp->f_flags & O_DIRECT) { | |
1227 | have_alloc_sem = 1; | |
1228 | down_read(&inode->i_alloc_sem); | |
1229 | } | |
1230 | ||
1231 | /* concurrent O_DIRECT writes are allowed */ | |
1232 | rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1; | |
1233 | ret = ocfs2_rw_lock(inode, rw_level); | |
1234 | if (ret < 0) { | |
1235 | rw_level = -1; | |
1236 | mlog_errno(ret); | |
1237 | goto out; | |
1238 | } | |
1239 | ||
d28c9174 | 1240 | ret = ocfs2_prepare_inode_for_write(filp->f_path.dentry, &iocb->ki_pos, |
8659ac25 TY |
1241 | iocb->ki_left, appending); |
1242 | if (ret < 0) { | |
1243 | mlog_errno(ret); | |
1244 | goto out; | |
1245 | } | |
ccd979bd MF |
1246 | |
1247 | /* communicate with ocfs2_dio_end_io */ | |
1248 | ocfs2_iocb_set_rw_locked(iocb); | |
1249 | ||
027445c3 | 1250 | ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos); |
ccd979bd MF |
1251 | |
1252 | /* buffered aio wouldn't have proper lock coverage today */ | |
1253 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); | |
1254 | ||
1255 | /* | |
1256 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io | |
1257 | * function pointer which is called when o_direct io completes so that | |
1258 | * it can unlock our rw lock. (it's the clustered equivalent of | |
1259 | * i_alloc_sem; protects truncate from racing with pending ios). | |
1260 | * Unfortunately there are error cases which call end_io and others | |
1261 | * that don't. so we don't have to unlock the rw_lock if either an | |
1262 | * async dio is going to do it in the future or an end_io after an | |
1263 | * error has already done it. | |
1264 | */ | |
1265 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { | |
1266 | rw_level = -1; | |
1267 | have_alloc_sem = 0; | |
1268 | } | |
1269 | ||
1270 | out: | |
ccd979bd MF |
1271 | if (have_alloc_sem) |
1272 | up_read(&inode->i_alloc_sem); | |
1273 | if (rw_level != -1) | |
1274 | ocfs2_rw_unlock(inode, rw_level); | |
1b1dcc1b | 1275 | mutex_unlock(&inode->i_mutex); |
ccd979bd MF |
1276 | |
1277 | mlog_exit(ret); | |
1278 | return ret; | |
1279 | } | |
1280 | ||
8659ac25 TY |
1281 | static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe, |
1282 | struct file *out, | |
1283 | loff_t *ppos, | |
1284 | size_t len, | |
1285 | unsigned int flags) | |
1286 | { | |
1287 | int ret; | |
d28c9174 | 1288 | struct inode *inode = out->f_path.dentry->d_inode; |
8659ac25 TY |
1289 | |
1290 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe, | |
1291 | (unsigned int)len, | |
d28c9174 JS |
1292 | out->f_path.dentry->d_name.len, |
1293 | out->f_path.dentry->d_name.name); | |
8659ac25 TY |
1294 | |
1295 | inode_double_lock(inode, pipe->inode); | |
1296 | ||
1297 | ret = ocfs2_rw_lock(inode, 1); | |
1298 | if (ret < 0) { | |
1299 | mlog_errno(ret); | |
1300 | goto out; | |
1301 | } | |
1302 | ||
d28c9174 | 1303 | ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0); |
8659ac25 TY |
1304 | if (ret < 0) { |
1305 | mlog_errno(ret); | |
1306 | goto out_unlock; | |
1307 | } | |
1308 | ||
1309 | /* ok, we're done with i_size and alloc work */ | |
1310 | ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags); | |
1311 | ||
1312 | out_unlock: | |
1313 | ocfs2_rw_unlock(inode, 1); | |
1314 | out: | |
1315 | inode_double_unlock(inode, pipe->inode); | |
1316 | ||
1317 | mlog_exit(ret); | |
1318 | return ret; | |
1319 | } | |
1320 | ||
1321 | static ssize_t ocfs2_file_splice_read(struct file *in, | |
1322 | loff_t *ppos, | |
1323 | struct pipe_inode_info *pipe, | |
1324 | size_t len, | |
1325 | unsigned int flags) | |
1326 | { | |
1327 | int ret = 0; | |
d28c9174 | 1328 | struct inode *inode = in->f_path.dentry->d_inode; |
8659ac25 TY |
1329 | |
1330 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe, | |
1331 | (unsigned int)len, | |
d28c9174 JS |
1332 | in->f_path.dentry->d_name.len, |
1333 | in->f_path.dentry->d_name.name); | |
8659ac25 TY |
1334 | |
1335 | /* | |
1336 | * See the comment in ocfs2_file_aio_read() | |
1337 | */ | |
1338 | ret = ocfs2_meta_lock(inode, NULL, 0); | |
1339 | if (ret < 0) { | |
1340 | mlog_errno(ret); | |
1341 | goto bail; | |
1342 | } | |
1343 | ocfs2_meta_unlock(inode, 0); | |
1344 | ||
1345 | ret = generic_file_splice_read(in, ppos, pipe, len, flags); | |
1346 | ||
1347 | bail: | |
1348 | mlog_exit(ret); | |
1349 | return ret; | |
1350 | } | |
1351 | ||
ccd979bd | 1352 | static ssize_t ocfs2_file_aio_read(struct kiocb *iocb, |
027445c3 BP |
1353 | const struct iovec *iov, |
1354 | unsigned long nr_segs, | |
ccd979bd MF |
1355 | loff_t pos) |
1356 | { | |
25899dee | 1357 | int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0; |
ccd979bd | 1358 | struct file *filp = iocb->ki_filp; |
d28c9174 | 1359 | struct inode *inode = filp->f_path.dentry->d_inode; |
ccd979bd | 1360 | |
027445c3 BP |
1361 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, |
1362 | (unsigned int)nr_segs, | |
d28c9174 JS |
1363 | filp->f_path.dentry->d_name.len, |
1364 | filp->f_path.dentry->d_name.name); | |
ccd979bd MF |
1365 | |
1366 | if (!inode) { | |
1367 | ret = -EINVAL; | |
1368 | mlog_errno(ret); | |
1369 | goto bail; | |
1370 | } | |
1371 | ||
ccd979bd MF |
1372 | /* |
1373 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads | |
1374 | * need locks to protect pending reads from racing with truncate. | |
1375 | */ | |
1376 | if (filp->f_flags & O_DIRECT) { | |
1377 | down_read(&inode->i_alloc_sem); | |
1378 | have_alloc_sem = 1; | |
1379 | ||
1380 | ret = ocfs2_rw_lock(inode, 0); | |
1381 | if (ret < 0) { | |
1382 | mlog_errno(ret); | |
1383 | goto bail; | |
1384 | } | |
1385 | rw_level = 0; | |
1386 | /* communicate with ocfs2_dio_end_io */ | |
1387 | ocfs2_iocb_set_rw_locked(iocb); | |
1388 | } | |
1389 | ||
c4374f8a MF |
1390 | /* |
1391 | * We're fine letting folks race truncates and extending | |
1392 | * writes with read across the cluster, just like they can | |
1393 | * locally. Hence no rw_lock during read. | |
1394 | * | |
1395 | * Take and drop the meta data lock to update inode fields | |
1396 | * like i_size. This allows the checks down below | |
1397 | * generic_file_aio_read() a chance of actually working. | |
1398 | */ | |
25899dee | 1399 | ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
c4374f8a MF |
1400 | if (ret < 0) { |
1401 | mlog_errno(ret); | |
1402 | goto bail; | |
1403 | } | |
25899dee | 1404 | ocfs2_meta_unlock(inode, lock_level); |
c4374f8a | 1405 | |
027445c3 | 1406 | ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos); |
ccd979bd MF |
1407 | if (ret == -EINVAL) |
1408 | mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n"); | |
1409 | ||
1410 | /* buffered aio wouldn't have proper lock coverage today */ | |
1411 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); | |
1412 | ||
1413 | /* see ocfs2_file_aio_write */ | |
1414 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { | |
1415 | rw_level = -1; | |
1416 | have_alloc_sem = 0; | |
1417 | } | |
1418 | ||
1419 | bail: | |
1420 | if (have_alloc_sem) | |
1421 | up_read(&inode->i_alloc_sem); | |
1422 | if (rw_level != -1) | |
1423 | ocfs2_rw_unlock(inode, rw_level); | |
1424 | mlog_exit(ret); | |
1425 | ||
1426 | return ret; | |
1427 | } | |
1428 | ||
92e1d5be | 1429 | const struct inode_operations ocfs2_file_iops = { |
ccd979bd MF |
1430 | .setattr = ocfs2_setattr, |
1431 | .getattr = ocfs2_getattr, | |
d38eb8db | 1432 | .permission = ocfs2_permission, |
ccd979bd MF |
1433 | }; |
1434 | ||
92e1d5be | 1435 | const struct inode_operations ocfs2_special_file_iops = { |
ccd979bd MF |
1436 | .setattr = ocfs2_setattr, |
1437 | .getattr = ocfs2_getattr, | |
d38eb8db | 1438 | .permission = ocfs2_permission, |
ccd979bd MF |
1439 | }; |
1440 | ||
4b6f5d20 | 1441 | const struct file_operations ocfs2_fops = { |
ccd979bd MF |
1442 | .read = do_sync_read, |
1443 | .write = do_sync_write, | |
1444 | .sendfile = generic_file_sendfile, | |
1445 | .mmap = ocfs2_mmap, | |
1446 | .fsync = ocfs2_sync_file, | |
1447 | .release = ocfs2_file_release, | |
1448 | .open = ocfs2_file_open, | |
1449 | .aio_read = ocfs2_file_aio_read, | |
1450 | .aio_write = ocfs2_file_aio_write, | |
ca4d147e | 1451 | .ioctl = ocfs2_ioctl, |
8659ac25 TY |
1452 | .splice_read = ocfs2_file_splice_read, |
1453 | .splice_write = ocfs2_file_splice_write, | |
ccd979bd MF |
1454 | }; |
1455 | ||
4b6f5d20 | 1456 | const struct file_operations ocfs2_dops = { |
ccd979bd MF |
1457 | .read = generic_read_dir, |
1458 | .readdir = ocfs2_readdir, | |
1459 | .fsync = ocfs2_sync_file, | |
ca4d147e | 1460 | .ioctl = ocfs2_ioctl, |
ccd979bd | 1461 | }; |