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.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/pagemap.h>
31 #include <linux/uio.h>
33 #define MLOG_MASK_PREFIX ML_INODE
34 #include <cluster/masklog.h>
42 #include "extent_map.h"
51 #include "buffer_head_io.h"
53 static int ocfs2_sync_inode(struct inode *inode)
55 filemap_fdatawrite(inode->i_mapping);
56 return sync_mapping_buffers(inode->i_mapping);
59 static int ocfs2_file_open(struct inode *inode, struct file *file)
62 int mode = file->f_flags;
63 struct ocfs2_inode_info *oi = OCFS2_I(inode);
65 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
66 file->f_dentry->d_name.len, file->f_dentry->d_name.name);
68 spin_lock(&oi->ip_lock);
70 /* Check that the inode hasn't been wiped from disk by another
71 * node. If it hasn't then we're safe as long as we hold the
72 * spin lock until our increment of open count. */
73 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
74 spin_unlock(&oi->ip_lock);
81 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
84 spin_unlock(&oi->ip_lock);
91 static int ocfs2_file_release(struct inode *inode, struct file *file)
93 struct ocfs2_inode_info *oi = OCFS2_I(inode);
95 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
96 file->f_dentry->d_name.len,
97 file->f_dentry->d_name.name);
99 spin_lock(&oi->ip_lock);
100 if (!--oi->ip_open_count)
101 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
102 spin_unlock(&oi->ip_lock);
109 static int ocfs2_sync_file(struct file *file,
110 struct dentry *dentry,
115 struct inode *inode = dentry->d_inode;
116 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
118 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
119 dentry->d_name.len, dentry->d_name.name);
121 err = ocfs2_sync_inode(dentry->d_inode);
125 journal = osb->journal->j_journal;
126 err = journal_force_commit(journal);
131 return (err < 0) ? -EIO : 0;
134 int ocfs2_set_inode_size(struct ocfs2_journal_handle *handle,
136 struct buffer_head *fe_bh,
142 i_size_write(inode, new_i_size);
143 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
144 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
146 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
157 static int ocfs2_simple_size_update(struct inode *inode,
158 struct buffer_head *di_bh,
162 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
163 struct ocfs2_journal_handle *handle = NULL;
165 handle = ocfs2_start_trans(osb, NULL,
166 OCFS2_INODE_UPDATE_CREDITS);
167 if (handle == NULL) {
173 ret = ocfs2_set_inode_size(handle, inode, di_bh,
178 ocfs2_commit_trans(handle);
183 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
185 struct buffer_head *fe_bh,
189 struct ocfs2_journal_handle *handle;
193 /* TODO: This needs to actually orphan the inode in this
196 handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
197 if (IS_ERR(handle)) {
198 status = PTR_ERR(handle);
203 status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
207 ocfs2_commit_trans(handle);
213 static int ocfs2_truncate_file(struct inode *inode,
214 struct buffer_head *di_bh,
218 struct ocfs2_dinode *fe = NULL;
219 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
220 struct ocfs2_truncate_context *tc = NULL;
222 mlog_entry("(inode = %"MLFu64", new_i_size = %"MLFu64"\n",
223 OCFS2_I(inode)->ip_blkno, new_i_size);
225 truncate_inode_pages(inode->i_mapping, new_i_size);
227 fe = (struct ocfs2_dinode *) di_bh->b_data;
228 if (!OCFS2_IS_VALID_DINODE(fe)) {
229 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
234 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
235 "Inode %"MLFu64", inode i_size = %lld != di "
236 "i_size = %"MLFu64", i_flags = 0x%x\n",
237 OCFS2_I(inode)->ip_blkno,
239 le64_to_cpu(fe->i_size), le32_to_cpu(fe->i_flags));
241 if (new_i_size > le64_to_cpu(fe->i_size)) {
242 mlog(0, "asked to truncate file with size (%"MLFu64") "
243 "to size (%"MLFu64")!\n",
244 le64_to_cpu(fe->i_size), new_i_size);
250 mlog(0, "inode %"MLFu64", i_size = %"MLFu64", new_i_size = %"MLFu64"\n",
251 le64_to_cpu(fe->i_blkno), le64_to_cpu(fe->i_size), new_i_size);
253 /* lets handle the simple truncate cases before doing any more
254 * cluster locking. */
255 if (new_i_size == le64_to_cpu(fe->i_size))
258 if (le32_to_cpu(fe->i_clusters) ==
259 ocfs2_clusters_for_bytes(osb->sb, new_i_size)) {
260 mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
262 /* No allocation change is required, so lets fast path
264 status = ocfs2_simple_size_update(inode, di_bh, new_i_size);
270 /* This forces other nodes to sync and drop their pages */
271 status = ocfs2_data_lock(inode, 1);
276 ocfs2_data_unlock(inode, 1);
278 /* alright, we're going to need to do a full blown alloc size
279 * change. Orphan the inode so that recovery can complete the
280 * truncate if necessary. This does the task of marking
282 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
288 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
294 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
300 /* TODO: orphan dir cleanup here. */
308 * extend allocation only here.
309 * we'll update all the disk stuff, and oip->alloc_size
311 * expect stuff to be locked, a transaction started and enough data /
312 * metadata reservations in the contexts.
314 * Will return -EAGAIN, and a reason if a restart is needed.
315 * If passed in, *reason will always be set, even in error.
317 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
320 struct buffer_head *fe_bh,
321 struct ocfs2_journal_handle *handle,
322 struct ocfs2_alloc_context *data_ac,
323 struct ocfs2_alloc_context *meta_ac,
324 enum ocfs2_alloc_restarted *reason_ret)
328 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
329 enum ocfs2_alloc_restarted reason = RESTART_NONE;
330 u32 bit_off, num_bits;
333 BUG_ON(!clusters_to_add);
335 free_extents = ocfs2_num_free_extents(osb, inode, fe);
336 if (free_extents < 0) {
337 status = free_extents;
342 /* there are two cases which could cause us to EAGAIN in the
343 * we-need-more-metadata case:
344 * 1) we haven't reserved *any*
345 * 2) we are so fragmented, we've needed to add metadata too
347 if (!free_extents && !meta_ac) {
348 mlog(0, "we haven't reserved any metadata!\n");
350 reason = RESTART_META;
352 } else if ((!free_extents)
353 && (ocfs2_alloc_context_bits_left(meta_ac)
354 < ocfs2_extend_meta_needed(fe))) {
355 mlog(0, "filesystem is really fragmented...\n");
357 reason = RESTART_META;
361 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
362 &bit_off, &num_bits);
364 if (status != -ENOSPC)
369 BUG_ON(num_bits > clusters_to_add);
371 /* reserve our write early -- insert_extent may update the inode */
372 status = ocfs2_journal_access(handle, inode, fe_bh,
373 OCFS2_JOURNAL_ACCESS_WRITE);
379 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
380 mlog(0, "Allocating %u clusters at block %u for inode %"MLFu64"\n",
381 num_bits, bit_off, OCFS2_I(inode)->ip_blkno);
382 status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block,
389 le32_add_cpu(&fe->i_clusters, num_bits);
390 spin_lock(&OCFS2_I(inode)->ip_lock);
391 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
392 spin_unlock(&OCFS2_I(inode)->ip_lock);
394 status = ocfs2_journal_dirty(handle, fe_bh);
400 clusters_to_add -= num_bits;
402 if (clusters_to_add) {
403 mlog(0, "need to alloc once more, clusters = %u, wanted = "
404 "%u\n", fe->i_clusters, clusters_to_add);
406 reason = RESTART_TRANS;
412 *reason_ret = reason;
416 static int ocfs2_extend_allocation(struct inode *inode,
420 int restart_func = 0;
421 int drop_alloc_sem = 0;
422 int credits, num_free_extents;
424 struct buffer_head *bh = NULL;
425 struct ocfs2_dinode *fe = NULL;
426 struct ocfs2_journal_handle *handle = NULL;
427 struct ocfs2_alloc_context *data_ac = NULL;
428 struct ocfs2_alloc_context *meta_ac = NULL;
429 enum ocfs2_alloc_restarted why;
430 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
432 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
434 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
435 OCFS2_BH_CACHED, inode);
441 fe = (struct ocfs2_dinode *) bh->b_data;
442 if (!OCFS2_IS_VALID_DINODE(fe)) {
443 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
449 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
451 mlog(0, "extend inode %"MLFu64", i_size = %lld, fe->i_clusters = %u, "
452 "clusters_to_add = %u\n",
453 OCFS2_I(inode)->ip_blkno, i_size_read(inode),
454 fe->i_clusters, clusters_to_add);
456 handle = ocfs2_alloc_handle(osb);
457 if (handle == NULL) {
463 num_free_extents = ocfs2_num_free_extents(osb,
466 if (num_free_extents < 0) {
467 status = num_free_extents;
472 if (!num_free_extents) {
473 status = ocfs2_reserve_new_metadata(osb,
478 if (status != -ENOSPC)
484 status = ocfs2_reserve_clusters(osb,
489 if (status != -ENOSPC)
494 /* blocks peope in read/write from reading our allocation
495 * until we're done changing it. We depend on i_sem to block
496 * other extend/truncate calls while we're here. Ordering wrt
497 * start_trans is important here -- always do it before! */
498 down_write(&OCFS2_I(inode)->ip_alloc_sem);
501 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
502 handle = ocfs2_start_trans(osb, handle, credits);
503 if (IS_ERR(handle)) {
504 status = PTR_ERR(handle);
510 restarted_transaction:
511 /* reserve a write to the file entry early on - that we if we
512 * run out of credits in the allocation path, we can still
514 status = ocfs2_journal_access(handle, inode, bh,
515 OCFS2_JOURNAL_ACCESS_WRITE);
521 prev_clusters = OCFS2_I(inode)->ip_clusters;
523 status = ocfs2_do_extend_allocation(osb,
531 if ((status < 0) && (status != -EAGAIN)) {
532 if (status != -ENOSPC)
537 status = ocfs2_journal_dirty(handle, bh);
543 spin_lock(&OCFS2_I(inode)->ip_lock);
544 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
545 spin_unlock(&OCFS2_I(inode)->ip_lock);
547 if (why != RESTART_NONE && clusters_to_add) {
548 if (why == RESTART_META) {
549 mlog(0, "restarting function.\n");
552 BUG_ON(why != RESTART_TRANS);
554 mlog(0, "restarting transaction.\n");
555 /* TODO: This can be more intelligent. */
556 credits = ocfs2_calc_extend_credits(osb->sb,
559 status = ocfs2_extend_trans(handle, credits);
561 /* handle still has to be committed at
567 goto restarted_transaction;
571 mlog(0, "fe: i_clusters = %u, i_size=%"MLFu64"\n",
572 fe->i_clusters, fe->i_size);
573 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
574 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
577 if (drop_alloc_sem) {
578 up_write(&OCFS2_I(inode)->ip_alloc_sem);
582 ocfs2_commit_trans(handle);
586 ocfs2_free_alloc_context(data_ac);
590 ocfs2_free_alloc_context(meta_ac);
593 if ((!status) && restart_func) {
606 /* Some parts of this taken from generic_cont_expand, which turned out
607 * to be too fragile to do exactly what we need without us having to
608 * worry about recursive locking in ->commit_write(). */
609 static int ocfs2_write_zero_page(struct inode *inode,
612 struct address_space *mapping = inode->i_mapping;
616 struct ocfs2_journal_handle *handle = NULL;
619 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
620 /* ugh. in prepare/commit_write, if from==to==start of block, we
621 ** skip the prepare. make sure we never send an offset for the start
624 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
627 index = size >> PAGE_CACHE_SHIFT;
629 page = grab_cache_page(mapping, index);
636 ret = ocfs2_prepare_write(NULL, page, offset, offset);
642 if (ocfs2_should_order_data(inode)) {
643 handle = ocfs2_start_walk_page_trans(inode, page, offset,
645 if (IS_ERR(handle)) {
646 ret = PTR_ERR(handle);
652 /* must not update i_size! */
653 ret = block_commit_write(page, offset, offset);
660 ocfs2_commit_trans(handle);
663 page_cache_release(page);
668 static int ocfs2_zero_extend(struct inode *inode,
673 struct super_block *sb = inode->i_sb;
675 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
676 while (start_off < zero_to_size) {
677 ret = ocfs2_write_zero_page(inode, start_off);
683 start_off += sb->s_blocksize;
690 static int ocfs2_extend_file(struct inode *inode,
691 struct buffer_head *di_bh,
697 /* setattr sometimes calls us like this. */
701 if (i_size_read(inode) == new_i_size)
703 BUG_ON(new_i_size < i_size_read(inode));
705 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
706 OCFS2_I(inode)->ip_clusters;
708 if (clusters_to_add) {
709 ret = ocfs2_extend_allocation(inode, clusters_to_add);
715 ret = ocfs2_zero_extend(inode, new_i_size);
722 /* No allocation required, we just use this helper to
723 * do a trivial update of i_size. */
724 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
734 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
736 int status = 0, size_change;
737 struct inode *inode = dentry->d_inode;
738 struct super_block *sb = inode->i_sb;
739 struct ocfs2_super *osb = OCFS2_SB(sb);
740 struct buffer_head *bh = NULL;
741 struct ocfs2_journal_handle *handle = NULL;
743 mlog_entry("(0x%p, '%.*s')\n", dentry,
744 dentry->d_name.len, dentry->d_name.name);
746 if (attr->ia_valid & ATTR_MODE)
747 mlog(0, "mode change: %d\n", attr->ia_mode);
748 if (attr->ia_valid & ATTR_UID)
749 mlog(0, "uid change: %d\n", attr->ia_uid);
750 if (attr->ia_valid & ATTR_GID)
751 mlog(0, "gid change: %d\n", attr->ia_gid);
752 if (attr->ia_valid & ATTR_SIZE)
753 mlog(0, "size change...\n");
754 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
755 mlog(0, "time change...\n");
757 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
758 | ATTR_GID | ATTR_UID | ATTR_MODE)
759 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
760 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
764 status = inode_change_ok(inode, attr);
768 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
770 status = ocfs2_rw_lock(inode, 1);
777 status = ocfs2_meta_lock(inode, NULL, &bh, 1);
779 if (status != -ENOENT)
784 if (size_change && attr->ia_size != i_size_read(inode)) {
785 if (i_size_read(inode) > attr->ia_size)
786 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
788 status = ocfs2_extend_file(inode, bh, attr->ia_size);
790 if (status != -ENOSPC)
797 handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
798 if (IS_ERR(handle)) {
799 status = PTR_ERR(handle);
804 status = inode_setattr(inode, attr);
810 status = ocfs2_mark_inode_dirty(handle, inode, bh);
815 ocfs2_commit_trans(handle);
817 ocfs2_meta_unlock(inode, 1);
820 ocfs2_rw_unlock(inode, 1);
829 int ocfs2_getattr(struct vfsmount *mnt,
830 struct dentry *dentry,
833 struct inode *inode = dentry->d_inode;
834 struct super_block *sb = dentry->d_inode->i_sb;
835 struct ocfs2_super *osb = sb->s_fs_info;
840 err = ocfs2_inode_revalidate(dentry);
847 generic_fillattr(inode, stat);
849 /* We set the blksize from the cluster size for performance */
850 stat->blksize = osb->s_clustersize;
858 static int ocfs2_write_remove_suid(struct inode *inode)
861 struct buffer_head *bh = NULL;
862 struct ocfs2_inode_info *oi = OCFS2_I(inode);
863 struct ocfs2_journal_handle *handle;
864 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
865 struct ocfs2_dinode *di;
867 mlog_entry("(Inode %"MLFu64", mode 0%o)\n", oi->ip_blkno,
870 handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
871 if (handle == NULL) {
877 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
883 ret = ocfs2_journal_access(handle, inode, bh,
884 OCFS2_JOURNAL_ACCESS_WRITE);
890 inode->i_mode &= ~S_ISUID;
891 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
892 inode->i_mode &= ~S_ISGID;
894 di = (struct ocfs2_dinode *) bh->b_data;
895 di->i_mode = cpu_to_le16(inode->i_mode);
897 ret = ocfs2_journal_dirty(handle, bh);
903 ocfs2_commit_trans(handle);
909 static inline int ocfs2_write_should_remove_suid(struct inode *inode)
911 mode_t mode = inode->i_mode;
913 if (!capable(CAP_FSETID)) {
914 if (unlikely(mode & S_ISUID))
917 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
923 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
924 const char __user *buf,
928 struct iovec local_iov = { .iov_base = (void __user *)buf,
930 int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0;
932 struct file *filp = iocb->ki_filp;
933 struct inode *inode = filp->f_dentry->d_inode;
934 loff_t newsize, saved_pos;
935 #ifdef OCFS2_ORACORE_WORKAROUNDS
936 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
939 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf,
941 filp->f_dentry->d_name.len,
942 filp->f_dentry->d_name.name);
944 /* happy write of zero bytes */
949 mlog(0, "bad inode\n");
953 #ifdef OCFS2_ORACORE_WORKAROUNDS
954 /* ugh, work around some applications which open everything O_DIRECT +
955 * O_APPEND and really don't mean to use O_DIRECT. */
956 if (osb->s_mount_opt & OCFS2_MOUNT_COMPAT_OCFS &&
957 (filp->f_flags & O_APPEND) && (filp->f_flags & O_DIRECT))
958 filp->f_flags &= ~O_DIRECT;
962 /* to match setattr's i_sem -> i_alloc_sem -> rw_lock ordering */
963 if (filp->f_flags & O_DIRECT) {
965 down_read(&inode->i_alloc_sem);
968 /* concurrent O_DIRECT writes are allowed */
969 rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
970 ret = ocfs2_rw_lock(inode, rw_level);
978 * We sample i_size under a read level meta lock to see if our write
979 * is extending the file, if it is we back off and get a write level
982 meta_level = (filp->f_flags & O_APPEND) ? 1 : 0;
984 ret = ocfs2_meta_lock(inode, NULL, NULL, meta_level);
991 /* Clear suid / sgid if necessary. We do this here
992 * instead of later in the write path because
993 * remove_suid() calls ->setattr without any hint that
994 * we may have already done our cluster locking. Since
995 * ocfs2_setattr() *must* take cluster locks to
996 * proceeed, this will lead us to recursively lock the
997 * inode. There's also the dinode i_size state which
998 * can be lost via setattr during extending writes (we
999 * set inode->i_size at the end of a write. */
1000 if (ocfs2_write_should_remove_suid(inode)) {
1001 if (meta_level == 0) {
1002 ocfs2_meta_unlock(inode, meta_level);
1007 ret = ocfs2_write_remove_suid(inode);
1014 /* work on a copy of ppos until we're sure that we won't have
1015 * to recalculate it due to relocking. */
1016 if (filp->f_flags & O_APPEND) {
1017 saved_pos = i_size_read(inode);
1018 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1020 saved_pos = iocb->ki_pos;
1022 newsize = count + saved_pos;
1024 mlog(0, "pos=%lld newsize=%"MLFu64" cursize=%lld\n",
1025 saved_pos, newsize, i_size_read(inode));
1027 /* No need for a higher level metadata lock if we're
1028 * never going past i_size. */
1029 if (newsize <= i_size_read(inode))
1032 if (meta_level == 0) {
1033 ocfs2_meta_unlock(inode, meta_level);
1038 spin_lock(&OCFS2_I(inode)->ip_lock);
1039 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1040 OCFS2_I(inode)->ip_clusters;
1041 spin_unlock(&OCFS2_I(inode)->ip_lock);
1043 mlog(0, "Writing at EOF, may need more allocation: "
1044 "i_size = %lld, newsize = %"MLFu64", need %u clusters\n",
1045 i_size_read(inode), newsize, clusters);
1047 /* We only want to continue the rest of this loop if
1048 * our extend will actually require more
1053 ret = ocfs2_extend_allocation(inode, clusters);
1060 /* Fill any holes which would've been created by this
1061 * write. If we're O_APPEND, this will wind up
1062 * (correctly) being a noop. */
1063 ret = ocfs2_zero_extend(inode, (u64) newsize - count);
1071 /* ok, we're done with i_size and alloc work */
1072 iocb->ki_pos = saved_pos;
1073 ocfs2_meta_unlock(inode, meta_level);
1076 /* communicate with ocfs2_dio_end_io */
1077 ocfs2_iocb_set_rw_locked(iocb);
1079 #ifdef OCFS2_ORACORE_WORKAROUNDS
1080 if (osb->s_mount_opt & OCFS2_MOUNT_COMPAT_OCFS &&
1081 filp->f_flags & O_DIRECT) {
1082 unsigned int saved_flags = filp->f_flags;
1083 int sector_size = 1 << osb->s_sectsize_bits;
1085 if ((saved_pos & (sector_size - 1)) ||
1086 (count & (sector_size - 1)) ||
1087 ((unsigned long)buf & (sector_size - 1))) {
1088 filp->f_flags |= O_SYNC;
1089 filp->f_flags &= ~O_DIRECT;
1092 ret = generic_file_aio_write_nolock(iocb, &local_iov, 1,
1095 filp->f_flags = saved_flags;
1098 ret = generic_file_aio_write_nolock(iocb, &local_iov, 1,
1101 /* buffered aio wouldn't have proper lock coverage today */
1102 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1105 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1106 * function pointer which is called when o_direct io completes so that
1107 * it can unlock our rw lock. (it's the clustered equivalent of
1108 * i_alloc_sem; protects truncate from racing with pending ios).
1109 * Unfortunately there are error cases which call end_io and others
1110 * that don't. so we don't have to unlock the rw_lock if either an
1111 * async dio is going to do it in the future or an end_io after an
1112 * error has already done it.
1114 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1120 if (meta_level != -1)
1121 ocfs2_meta_unlock(inode, meta_level);
1123 up_read(&inode->i_alloc_sem);
1125 ocfs2_rw_unlock(inode, rw_level);
1132 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
1137 int ret = 0, rw_level = -1, have_alloc_sem = 0;
1138 struct file *filp = iocb->ki_filp;
1139 struct inode *inode = filp->f_dentry->d_inode;
1140 #ifdef OCFS2_ORACORE_WORKAROUNDS
1141 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1144 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf,
1145 (unsigned int)count,
1146 filp->f_dentry->d_name.len,
1147 filp->f_dentry->d_name.name);
1155 #ifdef OCFS2_ORACORE_WORKAROUNDS
1156 if (osb->s_mount_opt & OCFS2_MOUNT_COMPAT_OCFS) {
1157 if (filp->f_flags & O_DIRECT) {
1158 int sector_size = 1 << osb->s_sectsize_bits;
1160 if ((pos & (sector_size - 1)) ||
1161 (count & (sector_size - 1)) ||
1162 ((unsigned long)buf & (sector_size - 1)) ||
1163 (i_size_read(inode) & (sector_size -1))) {
1164 filp->f_flags &= ~O_DIRECT;
1171 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1172 * need locks to protect pending reads from racing with truncate.
1174 if (filp->f_flags & O_DIRECT) {
1175 down_read(&inode->i_alloc_sem);
1178 ret = ocfs2_rw_lock(inode, 0);
1184 /* communicate with ocfs2_dio_end_io */
1185 ocfs2_iocb_set_rw_locked(iocb);
1188 ret = generic_file_aio_read(iocb, buf, count, iocb->ki_pos);
1190 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1192 /* buffered aio wouldn't have proper lock coverage today */
1193 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1195 /* see ocfs2_file_aio_write */
1196 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1203 up_read(&inode->i_alloc_sem);
1205 ocfs2_rw_unlock(inode, rw_level);
1211 struct inode_operations ocfs2_file_iops = {
1212 .setattr = ocfs2_setattr,
1213 .getattr = ocfs2_getattr,
1216 struct inode_operations ocfs2_special_file_iops = {
1217 .setattr = ocfs2_setattr,
1218 .getattr = ocfs2_getattr,
1221 struct file_operations ocfs2_fops = {
1222 .read = do_sync_read,
1223 .write = do_sync_write,
1224 .sendfile = generic_file_sendfile,
1226 .fsync = ocfs2_sync_file,
1227 .release = ocfs2_file_release,
1228 .open = ocfs2_file_open,
1229 .aio_read = ocfs2_file_aio_read,
1230 .aio_write = ocfs2_file_aio_write,
1233 struct file_operations ocfs2_dops = {
1234 .read = generic_read_dir,
1235 .readdir = ocfs2_readdir,
1236 .fsync = ocfs2_sync_file,