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