1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * File open, close, extend, truncate
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
26 #include <linux/capability.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>
33 #include <linux/sched.h>
34 #include <linux/splice.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
37 #include <linux/falloc.h>
39 #define MLOG_MASK_PREFIX ML_INODE
40 #include <cluster/masklog.h>
48 #include "extent_map.h"
58 #include "buffer_head_io.h"
60 static int ocfs2_sync_inode(struct inode *inode)
62 filemap_fdatawrite(inode->i_mapping);
63 return sync_mapping_buffers(inode->i_mapping);
66 static int ocfs2_file_open(struct inode *inode, struct file *file)
69 int mode = file->f_flags;
70 struct ocfs2_inode_info *oi = OCFS2_I(inode);
72 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
73 file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
75 spin_lock(&oi->ip_lock);
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);
88 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
91 spin_unlock(&oi->ip_lock);
98 static int ocfs2_file_release(struct inode *inode, struct file *file)
100 struct ocfs2_inode_info *oi = OCFS2_I(inode);
102 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
103 file->f_path.dentry->d_name.len,
104 file->f_path.dentry->d_name.name);
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);
116 static int ocfs2_sync_file(struct file *file,
117 struct dentry *dentry,
122 struct inode *inode = dentry->d_inode;
123 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
125 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
126 dentry->d_name.len, dentry->d_name.name);
128 err = ocfs2_sync_inode(dentry->d_inode);
132 journal = osb->journal->j_journal;
133 err = journal_force_commit(journal);
138 return (err < 0) ? -EIO : 0;
141 int ocfs2_should_update_atime(struct inode *inode,
142 struct vfsmount *vfsmnt)
145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
147 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
150 if ((inode->i_flags & S_NOATIME) ||
151 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
155 * We can be called with no vfsmnt structure - NFSD will
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.
165 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
166 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
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))
178 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
184 int ocfs2_update_inode_atime(struct inode *inode,
185 struct buffer_head *bh)
188 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
190 struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
194 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
195 if (handle == NULL) {
201 ret = ocfs2_journal_access(handle, inode, bh,
202 OCFS2_JOURNAL_ACCESS_WRITE);
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
213 inode->i_atime = CURRENT_TIME;
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);
217 ret = ocfs2_journal_dirty(handle, bh);
222 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
228 static int ocfs2_set_inode_size(handle_t *handle,
230 struct buffer_head *fe_bh,
236 i_size_write(inode, new_i_size);
237 inode->i_blocks = ocfs2_inode_sector_count(inode);
238 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
240 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
251 static int ocfs2_simple_size_update(struct inode *inode,
252 struct buffer_head *di_bh,
256 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
257 handle_t *handle = NULL;
259 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
260 if (handle == NULL) {
266 ret = ocfs2_set_inode_size(handle, inode, di_bh,
271 ocfs2_commit_trans(osb, handle);
276 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
278 struct buffer_head *fe_bh,
283 struct ocfs2_dinode *di;
288 /* TODO: This needs to actually orphan the inode in this
291 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
292 if (IS_ERR(handle)) {
293 status = PTR_ERR(handle);
298 status = ocfs2_journal_access(handle, inode, fe_bh,
299 OCFS2_JOURNAL_ACCESS_WRITE);
306 * Do this before setting i_size.
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,
316 i_size_write(inode, new_i_size);
317 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
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);
324 status = ocfs2_journal_dirty(handle, fe_bh);
329 ocfs2_commit_trans(osb, handle);
336 static int ocfs2_truncate_file(struct inode *inode,
337 struct buffer_head *di_bh,
341 struct ocfs2_dinode *fe = NULL;
342 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
343 struct ocfs2_truncate_context *tc = NULL;
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);
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);
356 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
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,
361 (unsigned long long)le64_to_cpu(fe->i_size),
362 le32_to_cpu(fe->i_flags));
364 if (new_i_size > le64_to_cpu(fe->i_size)) {
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);
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);
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))
383 down_write(&OCFS2_I(inode)->ip_alloc_sem);
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
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);
395 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
396 status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
397 i_size_read(inode), 1);
401 goto bail_unlock_sem;
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
408 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
411 goto bail_unlock_sem;
414 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
417 goto bail_unlock_sem;
420 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
423 goto bail_unlock_sem;
426 /* TODO: orphan dir cleanup here. */
428 up_write(&OCFS2_I(inode)->ip_alloc_sem);
437 * extend allocation only here.
438 * we'll update all the disk stuff, and oip->alloc_size
440 * expect stuff to be locked, a transaction started and enough data /
441 * metadata reservations in the contexts.
443 * Will return -EAGAIN, and a reason if a restart is needed.
444 * If passed in, *reason will always be set, even in error.
446 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
451 struct buffer_head *fe_bh,
453 struct ocfs2_alloc_context *data_ac,
454 struct ocfs2_alloc_context *meta_ac,
455 enum ocfs2_alloc_restarted *reason_ret)
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;
465 BUG_ON(!clusters_to_add);
468 flags = OCFS2_EXT_UNWRITTEN;
470 free_extents = ocfs2_num_free_extents(osb, inode, fe);
471 if (free_extents < 0) {
472 status = free_extents;
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
482 if (!free_extents && !meta_ac) {
483 mlog(0, "we haven't reserved any metadata!\n");
485 reason = RESTART_META;
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");
492 reason = RESTART_META;
496 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
497 clusters_to_add, &bit_off, &num_bits);
499 if (status != -ENOSPC)
504 BUG_ON(num_bits > clusters_to_add);
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);
514 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
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);
517 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
518 *logical_offset, block, num_bits,
525 status = ocfs2_journal_dirty(handle, fe_bh);
531 clusters_to_add -= num_bits;
532 *logical_offset += num_bits;
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);
538 reason = RESTART_TRANS;
544 *reason_ret = reason;
549 * For a given allocation, determine which allocators will need to be
550 * accessed, and lock them, reserving the appropriate number of bits.
552 * Sparse file systems call this from ocfs2_write_begin_nolock()
553 * and ocfs2_allocate_unwritten_extents().
555 * File systems which don't support holes call this from
556 * ocfs2_extend_allocation().
558 int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
559 u32 clusters_to_add, u32 extents_to_split,
560 struct ocfs2_alloc_context **data_ac,
561 struct ocfs2_alloc_context **meta_ac)
563 int ret = 0, num_free_extents;
564 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
565 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
571 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
573 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
574 "clusters_to_add = %u, extents_to_split = %u\n",
575 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
576 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
578 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
579 if (num_free_extents < 0) {
580 ret = num_free_extents;
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.
592 * Most of the time we'll only be seeing this 1 cluster at a time
595 * Always lock for any unwritten extents - we might want to
596 * add blocks during a split.
598 if (!num_free_extents ||
599 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
600 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
608 if (clusters_to_add == 0)
611 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
621 ocfs2_free_alloc_context(*meta_ac);
626 * We cannot have an error and a non null *data_ac.
633 static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
634 u32 clusters_to_add, int mark_unwritten)
637 int restart_func = 0;
640 struct buffer_head *bh = NULL;
641 struct ocfs2_dinode *fe = NULL;
642 handle_t *handle = NULL;
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);
648 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
651 * This function only exists for file systems which don't
654 BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
656 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
657 OCFS2_BH_CACHED, inode);
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);
671 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
673 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
680 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
681 handle = ocfs2_start_trans(osb, credits);
682 if (IS_ERR(handle)) {
683 status = PTR_ERR(handle);
689 restarted_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
693 status = ocfs2_journal_access(handle, inode, bh,
694 OCFS2_JOURNAL_ACCESS_WRITE);
700 prev_clusters = OCFS2_I(inode)->ip_clusters;
702 status = ocfs2_do_extend_allocation(osb,
712 if ((status < 0) && (status != -EAGAIN)) {
713 if (status != -ENOSPC)
718 status = ocfs2_journal_dirty(handle, bh);
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);
728 if (why != RESTART_NONE && clusters_to_add) {
729 if (why == RESTART_META) {
730 mlog(0, "restarting function.\n");
733 BUG_ON(why != RESTART_TRANS);
735 mlog(0, "restarting transaction.\n");
736 /* TODO: This can be more intelligent. */
737 credits = ocfs2_calc_extend_credits(osb->sb,
740 status = ocfs2_extend_trans(handle, credits);
742 /* handle still has to be committed at
748 goto restarted_transaction;
752 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
753 le32_to_cpu(fe->i_clusters),
754 (unsigned long long)le64_to_cpu(fe->i_size));
755 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
756 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
760 ocfs2_commit_trans(osb, handle);
764 ocfs2_free_alloc_context(data_ac);
768 ocfs2_free_alloc_context(meta_ac);
771 if ((!status) && restart_func) {
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
786 * worry about recursive locking in ->prepare_write() and
787 * ->commit_write(). */
788 static int ocfs2_write_zero_page(struct inode *inode,
791 struct address_space *mapping = inode->i_mapping;
795 handle_t *handle = NULL;
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
803 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
806 index = size >> PAGE_CACHE_SHIFT;
808 page = grab_cache_page(mapping, index);
815 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
821 if (ocfs2_should_order_data(inode)) {
822 handle = ocfs2_start_walk_page_trans(inode, page, offset,
824 if (IS_ERR(handle)) {
825 ret = PTR_ERR(handle);
831 /* must not update i_size! */
832 ret = block_commit_write(page, offset, offset);
839 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
842 page_cache_release(page);
847 static int ocfs2_zero_extend(struct inode *inode,
852 struct super_block *sb = inode->i_sb;
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);
862 start_off += sb->s_blocksize;
865 * Very large extends have the potential to lock up
866 * the cpu for extended periods of time.
875 int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
879 struct ocfs2_inode_info *oi = OCFS2_I(inode);
881 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
882 if (clusters_to_add < oi->ip_clusters)
885 clusters_to_add -= oi->ip_clusters;
887 if (clusters_to_add) {
888 ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
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
901 ret = ocfs2_zero_extend(inode, zero_to);
909 static int ocfs2_extend_file(struct inode *inode,
910 struct buffer_head *di_bh,
914 struct ocfs2_inode_info *oi = OCFS2_I(inode);
918 /* setattr sometimes calls us like this. */
922 if (i_size_read(inode) == new_i_size)
924 BUG_ON(new_i_size < i_size_read(inode));
927 * Fall through for converting inline data, even if the fs
928 * supports sparse files.
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.
935 if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
936 && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
937 goto out_update_size;
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
945 down_write(&oi->ip_alloc_sem);
947 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
949 * We can optimize small extends by keeping the inodes
952 if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
953 up_write(&oi->ip_alloc_sem);
954 goto out_update_size;
957 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
959 up_write(&oi->ip_alloc_sem);
966 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
967 ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
969 up_write(&oi->ip_alloc_sem);
977 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
985 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
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;
992 handle_t *handle = NULL;
994 mlog_entry("(0x%p, '%.*s')\n", dentry,
995 dentry->d_name.len, dentry->d_name.name);
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");
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);
1015 status = inode_change_ok(inode, attr);
1019 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1021 status = ocfs2_rw_lock(inode, 1);
1028 status = ocfs2_meta_lock(inode, &bh, 1);
1030 if (status != -ENOENT)
1032 goto bail_unlock_rw;
1035 if (size_change && attr->ia_size != i_size_read(inode)) {
1036 if (attr->ia_size > sb->s_maxbytes) {
1041 if (i_size_read(inode) > attr->ia_size)
1042 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1044 status = ocfs2_extend_file(inode, bh, attr->ia_size);
1046 if (status != -ENOSPC)
1053 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1054 if (IS_ERR(handle)) {
1055 status = PTR_ERR(handle);
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
1067 status = inode_setattr(inode, attr);
1073 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1078 ocfs2_commit_trans(osb, handle);
1080 ocfs2_meta_unlock(inode, 1);
1083 ocfs2_rw_unlock(inode, 1);
1092 int ocfs2_getattr(struct vfsmount *mnt,
1093 struct dentry *dentry,
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;
1103 err = ocfs2_inode_revalidate(dentry);
1110 generic_fillattr(inode, stat);
1112 /* We set the blksize from the cluster size for performance */
1113 stat->blksize = osb->s_clustersize;
1121 int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1127 ret = ocfs2_meta_lock(inode, NULL, 0);
1134 ret = generic_permission(inode, mask, NULL);
1136 ocfs2_meta_unlock(inode, 0);
1142 static int __ocfs2_write_remove_suid(struct inode *inode,
1143 struct buffer_head *bh)
1147 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1148 struct ocfs2_dinode *di;
1150 mlog_entry("(Inode %llu, mode 0%o)\n",
1151 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
1153 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1154 if (handle == NULL) {
1160 ret = ocfs2_journal_access(handle, inode, bh,
1161 OCFS2_JOURNAL_ACCESS_WRITE);
1167 inode->i_mode &= ~S_ISUID;
1168 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1169 inode->i_mode &= ~S_ISGID;
1171 di = (struct ocfs2_dinode *) bh->b_data;
1172 di->i_mode = cpu_to_le16(inode->i_mode);
1174 ret = ocfs2_journal_dirty(handle, bh);
1179 ocfs2_commit_trans(osb, handle);
1186 * Will look for holes and unwritten extents in the range starting at
1187 * pos for count bytes (inclusive).
1189 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1193 unsigned int extent_flags;
1194 u32 cpos, clusters, extent_len, phys_cpos;
1195 struct super_block *sb = inode->i_sb;
1197 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1198 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1201 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1208 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
1213 if (extent_len > clusters)
1214 extent_len = clusters;
1216 clusters -= extent_len;
1223 static int ocfs2_write_remove_suid(struct inode *inode)
1226 struct buffer_head *bh = NULL;
1227 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1229 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1230 oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1236 ret = __ocfs2_write_remove_suid(inode, bh);
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".
1247 static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1251 u32 cpos, phys_cpos, clusters, alloc_size;
1252 u64 end = start + len;
1253 struct buffer_head *di_bh = NULL;
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);
1265 * Nothing to do if the requested reservation range
1266 * fits within the inode.
1268 if (ocfs2_size_fits_inline_data(di_bh, end))
1271 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1279 * We consider both start and len to be inclusive.
1281 cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1282 clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1286 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1294 * Hole or existing extent len can be arbitrary, so
1295 * cap it to our own allocation request.
1297 if (alloc_size > clusters)
1298 alloc_size = clusters;
1302 * We already have an allocation at this
1303 * region so we can safely skip it.
1308 ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1317 clusters -= alloc_size;
1327 static 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)
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;
1337 struct ocfs2_alloc_context *meta_ac = NULL;
1338 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1340 ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
1346 mutex_lock(&tl_inode->i_mutex);
1348 if (ocfs2_truncate_log_needs_flush(osb)) {
1349 ret = __ocfs2_flush_truncate_log(osb);
1356 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1357 if (handle == NULL) {
1363 ret = ocfs2_journal_access(handle, inode, di_bh,
1364 OCFS2_JOURNAL_ACCESS_WRITE);
1370 ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
1377 OCFS2_I(inode)->ip_clusters -= len;
1378 di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1380 ret = ocfs2_journal_dirty(handle, di_bh);
1386 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
1391 ocfs2_commit_trans(osb, handle);
1393 mutex_unlock(&tl_inode->i_mutex);
1396 ocfs2_free_alloc_context(meta_ac);
1402 * Truncate a byte range, avoiding pages within partial clusters. This
1403 * preserves those pages for the zeroing code to write to.
1405 static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1408 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1410 struct address_space *mapping = inode->i_mapping;
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);
1417 unmap_mapping_range(mapping, start, end - start, 0);
1418 truncate_inode_pages_range(mapping, start, end - 1);
1422 static int ocfs2_zero_partial_clusters(struct inode *inode,
1426 u64 tmpend, end = start + len;
1427 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1428 unsigned int csize = osb->s_clustersize;
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.
1438 mlog(0, "byte start: %llu, end: %llu\n",
1439 (unsigned long long)start, (unsigned long long)end);
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
1446 if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1449 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1450 if (handle == NULL) {
1457 * We want to get the byte offset of the end of the 1st cluster.
1459 tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
1463 mlog(0, "1st range: start: %llu, tmpend: %llu\n",
1464 (unsigned long long)start, (unsigned long long)tmpend);
1466 ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
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.
1476 start = end & ~(osb->s_clustersize - 1);
1478 mlog(0, "2nd range: start: %llu, end: %llu\n",
1479 (unsigned long long)start, (unsigned long long)end);
1481 ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1486 ocfs2_commit_trans(osb, handle);
1491 static int ocfs2_remove_inode_range(struct inode *inode,
1492 struct buffer_head *di_bh, u64 byte_start,
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;
1499 struct address_space *mapping = inode->i_mapping;
1501 ocfs2_init_dealloc_ctxt(&dealloc);
1506 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1507 ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
1508 byte_start + byte_len, 0);
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.
1519 unmap_mapping_range(mapping, 0, 0, 0);
1520 truncate_inode_pages(mapping, 0);
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;
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);
1536 ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1544 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1551 if (alloc_size > trunc_len)
1552 alloc_size = trunc_len;
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,
1566 trunc_len -= alloc_size;
1569 ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1572 ocfs2_schedule_truncate_log_flush(osb, 1);
1573 ocfs2_run_deallocs(osb, &dealloc);
1579 * Parts of this function taken from xfs_change_file_space()
1581 static 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,
1589 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1590 struct buffer_head *di_bh = NULL;
1592 unsigned long long max_off = inode->i_sb->s_maxbytes;
1594 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
1597 mutex_lock(&inode->i_mutex);
1600 * This prevents concurrent writes on other nodes
1602 ret = ocfs2_rw_lock(inode, 1);
1608 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1614 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1616 goto out_meta_unlock;
1619 switch (sr->l_whence) {
1620 case 0: /*SEEK_SET*/
1622 case 1: /*SEEK_CUR*/
1623 sr->l_start += f_pos;
1625 case 2: /*SEEK_END*/
1626 sr->l_start += i_size_read(inode);
1630 goto out_meta_unlock;
1634 llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
1637 || sr->l_start > max_off
1638 || (sr->l_start + llen) < 0
1639 || (sr->l_start + llen) > max_off) {
1641 goto out_meta_unlock;
1643 size = sr->l_start + sr->l_len;
1645 if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
1646 if (sr->l_len <= 0) {
1648 goto out_meta_unlock;
1652 if (file && should_remove_suid(file->f_path.dentry)) {
1653 ret = __ocfs2_write_remove_suid(inode, di_bh);
1656 goto out_meta_unlock;
1660 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1662 case OCFS2_IOC_RESVSP:
1663 case OCFS2_IOC_RESVSP64:
1665 * This takes unsigned offsets, but the signed ones we
1666 * pass have been checked against overflow above.
1668 ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
1671 case OCFS2_IOC_UNRESVSP:
1672 case OCFS2_IOC_UNRESVSP64:
1673 ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
1679 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1682 goto out_meta_unlock;
1686 * We update c/mtime for these changes
1688 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1689 if (IS_ERR(handle)) {
1690 ret = PTR_ERR(handle);
1692 goto out_meta_unlock;
1695 if (change_size && i_size_read(inode) < size)
1696 i_size_write(inode, size);
1698 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1699 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1703 ocfs2_commit_trans(osb, handle);
1707 ocfs2_meta_unlock(inode, 1);
1709 ocfs2_rw_unlock(inode, 1);
1711 mutex_unlock(&inode->i_mutex);
1716 int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1717 struct ocfs2_space_resv *sr)
1719 struct inode *inode = file->f_path.dentry->d_inode;
1720 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
1722 if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
1723 !ocfs2_writes_unwritten_extents(osb))
1725 else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
1726 !ocfs2_sparse_alloc(osb))
1729 if (!S_ISREG(inode->i_mode))
1732 if (!(file->f_mode & FMODE_WRITE))
1735 return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
1738 static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1741 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1742 struct ocfs2_space_resv sr;
1743 int change_size = 1;
1745 if (!ocfs2_writes_unwritten_extents(osb))
1748 if (S_ISDIR(inode->i_mode))
1751 if (mode & FALLOC_FL_KEEP_SIZE)
1755 sr.l_start = (s64)offset;
1756 sr.l_len = (s64)len;
1758 return __ocfs2_change_file_space(NULL, inode, offset,
1759 OCFS2_IOC_RESVSP64, &sr, change_size);
1762 static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1768 int ret = 0, meta_level = 0;
1769 struct inode *inode = dentry->d_inode;
1770 loff_t saved_pos, end;
1773 * We start with a read level meta lock and only jump to an ex
1774 * if we need to make modifications here.
1777 ret = ocfs2_meta_lock(inode, NULL, meta_level);
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. */
1793 if (should_remove_suid(dentry)) {
1794 if (meta_level == 0) {
1795 ocfs2_meta_unlock(inode, meta_level);
1800 ret = ocfs2_write_remove_suid(inode);
1807 /* work on a copy of ppos until we're sure that we won't have
1808 * to recalculate it due to relocking. */
1810 saved_pos = i_size_read(inode);
1811 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1816 end = saved_pos + count;
1819 * Skip the O_DIRECT checks if we don't need
1822 if (!direct_io || !(*direct_io))
1826 * There's no sane way to do direct writes to an inode
1829 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1835 * Allowing concurrent direct writes means
1836 * i_size changes wouldn't be synchronized, so
1837 * one node could wind up truncating another
1840 if (end > i_size_read(inode)) {
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.
1851 ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
1864 ocfs2_meta_unlock(inode, meta_level);
1870 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1871 const struct iovec *iov,
1872 unsigned long nr_segs,
1875 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
1877 ssize_t written = 0;
1878 size_t ocount; /* original count */
1879 size_t count; /* after file limit checks */
1880 loff_t old_size, *ppos = &iocb->ki_pos;
1882 struct file *file = iocb->ki_filp;
1883 struct inode *inode = file->f_path.dentry->d_inode;
1884 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1886 mlog_entry("(0x%p, %u, '%.*s')\n", file,
1887 (unsigned int)nr_segs,
1888 file->f_path.dentry->d_name.len,
1889 file->f_path.dentry->d_name.name);
1891 if (iocb->ki_left == 0)
1894 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1896 appending = file->f_flags & O_APPEND ? 1 : 0;
1897 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1899 mutex_lock(&inode->i_mutex);
1902 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1904 down_read(&inode->i_alloc_sem);
1908 /* concurrent O_DIRECT writes are allowed */
1909 rw_level = !direct_io;
1910 ret = ocfs2_rw_lock(inode, rw_level);
1916 can_do_direct = direct_io;
1917 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1918 iocb->ki_left, appending,
1926 * We can't complete the direct I/O as requested, fall back to
1929 if (direct_io && !can_do_direct) {
1930 ocfs2_rw_unlock(inode, rw_level);
1931 up_read(&inode->i_alloc_sem);
1941 * To later detect whether a journal commit for sync writes is
1942 * necessary, we sample i_size, and cluster count here.
1944 old_size = i_size_read(inode);
1945 old_clusters = OCFS2_I(inode)->ip_clusters;
1947 /* communicate with ocfs2_dio_end_io */
1948 ocfs2_iocb_set_rw_locked(iocb, rw_level);
1951 ret = generic_segment_checks(iov, &nr_segs, &ocount,
1956 ret = generic_write_checks(file, ppos, &count,
1957 S_ISBLK(inode->i_mode));
1961 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1962 ppos, count, ocount);
1968 written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
1973 /* buffered aio wouldn't have proper lock coverage today */
1974 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
1976 if ((file->f_flags & O_SYNC && !direct_io) || IS_SYNC(inode)) {
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
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);
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.
2001 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2008 ocfs2_rw_unlock(inode, rw_level);
2012 up_read(&inode->i_alloc_sem);
2014 mutex_unlock(&inode->i_mutex);
2017 return written ? written : ret;
2020 static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2027 struct inode *inode = out->f_path.dentry->d_inode;
2029 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
2031 out->f_path.dentry->d_name.len,
2032 out->f_path.dentry->d_name.name);
2034 inode_double_lock(inode, pipe->inode);
2036 ret = ocfs2_rw_lock(inode, 1);
2042 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
2049 ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
2052 ocfs2_rw_unlock(inode, 1);
2054 inode_double_unlock(inode, pipe->inode);
2060 static ssize_t ocfs2_file_splice_read(struct file *in,
2062 struct pipe_inode_info *pipe,
2067 struct inode *inode = in->f_path.dentry->d_inode;
2069 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
2071 in->f_path.dentry->d_name.len,
2072 in->f_path.dentry->d_name.name);
2075 * See the comment in ocfs2_file_aio_read()
2077 ret = ocfs2_meta_lock(inode, NULL, 0);
2082 ocfs2_meta_unlock(inode, 0);
2084 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2091 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
2092 const struct iovec *iov,
2093 unsigned long nr_segs,
2096 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
2097 struct file *filp = iocb->ki_filp;
2098 struct inode *inode = filp->f_path.dentry->d_inode;
2100 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
2101 (unsigned int)nr_segs,
2102 filp->f_path.dentry->d_name.len,
2103 filp->f_path.dentry->d_name.name);
2112 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
2113 * need locks to protect pending reads from racing with truncate.
2115 if (filp->f_flags & O_DIRECT) {
2116 down_read(&inode->i_alloc_sem);
2119 ret = ocfs2_rw_lock(inode, 0);
2125 /* communicate with ocfs2_dio_end_io */
2126 ocfs2_iocb_set_rw_locked(iocb, rw_level);
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.
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.
2138 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
2143 ocfs2_meta_unlock(inode, lock_level);
2145 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
2147 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
2149 /* buffered aio wouldn't have proper lock coverage today */
2150 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
2152 /* see ocfs2_file_aio_write */
2153 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2160 up_read(&inode->i_alloc_sem);
2162 ocfs2_rw_unlock(inode, rw_level);
2168 const struct inode_operations ocfs2_file_iops = {
2169 .setattr = ocfs2_setattr,
2170 .getattr = ocfs2_getattr,
2171 .permission = ocfs2_permission,
2172 .fallocate = ocfs2_fallocate,
2175 const struct inode_operations ocfs2_special_file_iops = {
2176 .setattr = ocfs2_setattr,
2177 .getattr = ocfs2_getattr,
2178 .permission = ocfs2_permission,
2181 const struct file_operations ocfs2_fops = {
2182 .read = do_sync_read,
2183 .write = do_sync_write,
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,
2190 .ioctl = ocfs2_ioctl,
2191 #ifdef CONFIG_COMPAT
2192 .compat_ioctl = ocfs2_compat_ioctl,
2194 .splice_read = ocfs2_file_splice_read,
2195 .splice_write = ocfs2_file_splice_write,
2198 const struct file_operations ocfs2_dops = {
2199 .read = generic_read_dir,
2200 .readdir = ocfs2_readdir,
2201 .fsync = ocfs2_sync_file,
2202 .ioctl = ocfs2_ioctl,
2203 #ifdef CONFIG_COMPAT
2204 .compat_ioctl = ocfs2_compat_ioctl,