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/pipe_fs_i.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
38 #define MLOG_MASK_PREFIX ML_INODE
39 #include <cluster/masklog.h>
47 #include "extent_map.h"
57 #include "buffer_head_io.h"
59 static int ocfs2_sync_inode(struct inode *inode)
61 filemap_fdatawrite(inode->i_mapping);
62 return sync_mapping_buffers(inode->i_mapping);
65 static int ocfs2_file_open(struct inode *inode, struct file *file)
68 int mode = file->f_flags;
69 struct ocfs2_inode_info *oi = OCFS2_I(inode);
71 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
72 file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
74 spin_lock(&oi->ip_lock);
76 /* Check that the inode hasn't been wiped from disk by another
77 * node. If it hasn't then we're safe as long as we hold the
78 * spin lock until our increment of open count. */
79 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
80 spin_unlock(&oi->ip_lock);
87 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
90 spin_unlock(&oi->ip_lock);
97 static int ocfs2_file_release(struct inode *inode, struct file *file)
99 struct ocfs2_inode_info *oi = OCFS2_I(inode);
101 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
102 file->f_path.dentry->d_name.len,
103 file->f_path.dentry->d_name.name);
105 spin_lock(&oi->ip_lock);
106 if (!--oi->ip_open_count)
107 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
108 spin_unlock(&oi->ip_lock);
115 static int ocfs2_sync_file(struct file *file,
116 struct dentry *dentry,
121 struct inode *inode = dentry->d_inode;
122 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
124 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
125 dentry->d_name.len, dentry->d_name.name);
127 err = ocfs2_sync_inode(dentry->d_inode);
131 journal = osb->journal->j_journal;
132 err = journal_force_commit(journal);
137 return (err < 0) ? -EIO : 0;
140 int ocfs2_should_update_atime(struct inode *inode,
141 struct vfsmount *vfsmnt)
144 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
146 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
149 if ((inode->i_flags & S_NOATIME) ||
150 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
154 * We can be called with no vfsmnt structure - NFSD will
157 * Note that our action here is different than touch_atime() -
158 * if we can't tell whether this is a noatime mount, then we
159 * don't know whether to trust the value of s_atime_quantum.
164 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
165 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
168 if (vfsmnt->mnt_flags & MNT_RELATIME) {
169 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
170 (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
177 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
183 int ocfs2_update_inode_atime(struct inode *inode,
184 struct buffer_head *bh)
187 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
192 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
193 if (handle == NULL) {
199 inode->i_atime = CURRENT_TIME;
200 ret = ocfs2_mark_inode_dirty(handle, inode, bh);
204 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
210 int ocfs2_set_inode_size(handle_t *handle,
212 struct buffer_head *fe_bh,
218 i_size_write(inode, new_i_size);
219 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
220 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
222 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
233 static int ocfs2_simple_size_update(struct inode *inode,
234 struct buffer_head *di_bh,
238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
239 handle_t *handle = NULL;
241 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
242 if (handle == NULL) {
248 ret = ocfs2_set_inode_size(handle, inode, di_bh,
253 ocfs2_commit_trans(osb, handle);
258 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
260 struct buffer_head *fe_bh,
265 struct ocfs2_dinode *di;
269 /* TODO: This needs to actually orphan the inode in this
272 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
273 if (IS_ERR(handle)) {
274 status = PTR_ERR(handle);
279 status = ocfs2_journal_access(handle, inode, fe_bh,
280 OCFS2_JOURNAL_ACCESS_WRITE);
287 * Do this before setting i_size.
289 status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
295 i_size_write(inode, new_i_size);
296 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
297 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
299 di = (struct ocfs2_dinode *) fe_bh->b_data;
300 di->i_size = cpu_to_le64(new_i_size);
301 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
302 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
304 status = ocfs2_journal_dirty(handle, fe_bh);
309 ocfs2_commit_trans(osb, handle);
316 static int ocfs2_truncate_file(struct inode *inode,
317 struct buffer_head *di_bh,
321 struct ocfs2_dinode *fe = NULL;
322 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
323 struct ocfs2_truncate_context *tc = NULL;
325 mlog_entry("(inode = %llu, new_i_size = %llu\n",
326 (unsigned long long)OCFS2_I(inode)->ip_blkno,
327 (unsigned long long)new_i_size);
329 truncate_inode_pages(inode->i_mapping, new_i_size);
331 fe = (struct ocfs2_dinode *) di_bh->b_data;
332 if (!OCFS2_IS_VALID_DINODE(fe)) {
333 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
338 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
339 "Inode %llu, inode i_size = %lld != di "
340 "i_size = %llu, i_flags = 0x%x\n",
341 (unsigned long long)OCFS2_I(inode)->ip_blkno,
343 (unsigned long long)le64_to_cpu(fe->i_size),
344 le32_to_cpu(fe->i_flags));
346 if (new_i_size > le64_to_cpu(fe->i_size)) {
347 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
348 (unsigned long long)le64_to_cpu(fe->i_size),
349 (unsigned long long)new_i_size);
355 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
356 (unsigned long long)le64_to_cpu(fe->i_blkno),
357 (unsigned long long)le64_to_cpu(fe->i_size),
358 (unsigned long long)new_i_size);
360 /* lets handle the simple truncate cases before doing any more
361 * cluster locking. */
362 if (new_i_size == le64_to_cpu(fe->i_size))
365 /* This forces other nodes to sync and drop their pages. Do
366 * this even if we have a truncate without allocation change -
367 * ocfs2 cluster sizes can be much greater than page size, so
368 * we have to truncate them anyway. */
369 status = ocfs2_data_lock(inode, 1);
375 /* alright, we're going to need to do a full blown alloc size
376 * change. Orphan the inode so that recovery can complete the
377 * truncate if necessary. This does the task of marking
379 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
382 goto bail_unlock_data;
385 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
388 goto bail_unlock_data;
391 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
394 goto bail_unlock_data;
397 /* TODO: orphan dir cleanup here. */
399 ocfs2_data_unlock(inode, 1);
408 * extend allocation only here.
409 * we'll update all the disk stuff, and oip->alloc_size
411 * expect stuff to be locked, a transaction started and enough data /
412 * metadata reservations in the contexts.
414 * Will return -EAGAIN, and a reason if a restart is needed.
415 * If passed in, *reason will always be set, even in error.
417 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
421 struct buffer_head *fe_bh,
423 struct ocfs2_alloc_context *data_ac,
424 struct ocfs2_alloc_context *meta_ac,
425 enum ocfs2_alloc_restarted *reason_ret)
429 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
430 enum ocfs2_alloc_restarted reason = RESTART_NONE;
431 u32 bit_off, num_bits;
434 BUG_ON(!clusters_to_add);
436 free_extents = ocfs2_num_free_extents(osb, inode, fe);
437 if (free_extents < 0) {
438 status = free_extents;
443 /* there are two cases which could cause us to EAGAIN in the
444 * we-need-more-metadata case:
445 * 1) we haven't reserved *any*
446 * 2) we are so fragmented, we've needed to add metadata too
448 if (!free_extents && !meta_ac) {
449 mlog(0, "we haven't reserved any metadata!\n");
451 reason = RESTART_META;
453 } else if ((!free_extents)
454 && (ocfs2_alloc_context_bits_left(meta_ac)
455 < ocfs2_extend_meta_needed(fe))) {
456 mlog(0, "filesystem is really fragmented...\n");
458 reason = RESTART_META;
462 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
463 &bit_off, &num_bits);
465 if (status != -ENOSPC)
470 BUG_ON(num_bits > clusters_to_add);
472 /* reserve our write early -- insert_extent may update the inode */
473 status = ocfs2_journal_access(handle, inode, fe_bh,
474 OCFS2_JOURNAL_ACCESS_WRITE);
480 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
481 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
482 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
483 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
484 *logical_offset, block, num_bits,
491 status = ocfs2_journal_dirty(handle, fe_bh);
497 clusters_to_add -= num_bits;
498 *logical_offset += num_bits;
500 if (clusters_to_add) {
501 mlog(0, "need to alloc once more, clusters = %u, wanted = "
502 "%u\n", fe->i_clusters, clusters_to_add);
504 reason = RESTART_TRANS;
510 *reason_ret = reason;
515 * For a given allocation, determine which allocators will need to be
516 * accessed, and lock them, reserving the appropriate number of bits.
518 * Called from ocfs2_extend_allocation() for file systems which don't
519 * support holes, and from ocfs2_write() for file systems which
520 * understand sparse inodes.
522 int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
524 struct ocfs2_alloc_context **data_ac,
525 struct ocfs2_alloc_context **meta_ac)
527 int ret, num_free_extents;
528 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
533 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
534 "clusters_to_add = %u\n",
535 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
536 le32_to_cpu(di->i_clusters), clusters_to_add);
538 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
539 if (num_free_extents < 0) {
540 ret = num_free_extents;
546 * Sparse allocation file systems need to be more conservative
547 * with reserving room for expansion - the actual allocation
548 * happens while we've got a journal handle open so re-taking
549 * a cluster lock (because we ran out of room for another
550 * extent) will violate ordering rules.
552 * Most of the time we'll only be seeing this 1 cluster at a time
555 if (!num_free_extents ||
556 (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
557 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
565 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
575 ocfs2_free_alloc_context(*meta_ac);
580 * We cannot have an error and a non null *data_ac.
587 static int ocfs2_extend_allocation(struct inode *inode,
591 int restart_func = 0;
592 int drop_alloc_sem = 0;
594 u32 prev_clusters, logical_start;
595 struct buffer_head *bh = NULL;
596 struct ocfs2_dinode *fe = NULL;
597 handle_t *handle = NULL;
598 struct ocfs2_alloc_context *data_ac = NULL;
599 struct ocfs2_alloc_context *meta_ac = NULL;
600 enum ocfs2_alloc_restarted why;
601 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
603 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
606 * This function only exists for file systems which don't
609 BUG_ON(ocfs2_sparse_alloc(osb));
611 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
612 OCFS2_BH_CACHED, inode);
618 fe = (struct ocfs2_dinode *) bh->b_data;
619 if (!OCFS2_IS_VALID_DINODE(fe)) {
620 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
625 logical_start = OCFS2_I(inode)->ip_clusters;
628 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
630 /* blocks peope in read/write from reading our allocation
631 * until we're done changing it. We depend on i_mutex to block
632 * other extend/truncate calls while we're here. Ordering wrt
633 * start_trans is important here -- always do it before! */
634 down_write(&OCFS2_I(inode)->ip_alloc_sem);
637 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
644 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
645 handle = ocfs2_start_trans(osb, credits);
646 if (IS_ERR(handle)) {
647 status = PTR_ERR(handle);
653 restarted_transaction:
654 /* reserve a write to the file entry early on - that we if we
655 * run out of credits in the allocation path, we can still
657 status = ocfs2_journal_access(handle, inode, bh,
658 OCFS2_JOURNAL_ACCESS_WRITE);
664 prev_clusters = OCFS2_I(inode)->ip_clusters;
666 status = ocfs2_do_extend_allocation(osb,
675 if ((status < 0) && (status != -EAGAIN)) {
676 if (status != -ENOSPC)
681 status = ocfs2_journal_dirty(handle, bh);
687 spin_lock(&OCFS2_I(inode)->ip_lock);
688 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
689 spin_unlock(&OCFS2_I(inode)->ip_lock);
691 if (why != RESTART_NONE && clusters_to_add) {
692 if (why == RESTART_META) {
693 mlog(0, "restarting function.\n");
696 BUG_ON(why != RESTART_TRANS);
698 mlog(0, "restarting transaction.\n");
699 /* TODO: This can be more intelligent. */
700 credits = ocfs2_calc_extend_credits(osb->sb,
703 status = ocfs2_extend_trans(handle, credits);
705 /* handle still has to be committed at
711 goto restarted_transaction;
715 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
716 fe->i_clusters, (unsigned long long)fe->i_size);
717 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
718 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
721 if (drop_alloc_sem) {
722 up_write(&OCFS2_I(inode)->ip_alloc_sem);
726 ocfs2_commit_trans(osb, handle);
730 ocfs2_free_alloc_context(data_ac);
734 ocfs2_free_alloc_context(meta_ac);
737 if ((!status) && restart_func) {
750 /* Some parts of this taken from generic_cont_expand, which turned out
751 * to be too fragile to do exactly what we need without us having to
752 * worry about recursive locking in ->prepare_write() and
753 * ->commit_write(). */
754 static int ocfs2_write_zero_page(struct inode *inode,
757 struct address_space *mapping = inode->i_mapping;
761 handle_t *handle = NULL;
764 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
765 /* ugh. in prepare/commit_write, if from==to==start of block, we
766 ** skip the prepare. make sure we never send an offset for the start
769 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
772 index = size >> PAGE_CACHE_SHIFT;
774 page = grab_cache_page(mapping, index);
781 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
787 if (ocfs2_should_order_data(inode)) {
788 handle = ocfs2_start_walk_page_trans(inode, page, offset,
790 if (IS_ERR(handle)) {
791 ret = PTR_ERR(handle);
797 /* must not update i_size! */
798 ret = block_commit_write(page, offset, offset);
805 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
808 page_cache_release(page);
813 static int ocfs2_zero_extend(struct inode *inode,
818 struct super_block *sb = inode->i_sb;
820 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
821 while (start_off < zero_to_size) {
822 ret = ocfs2_write_zero_page(inode, start_off);
828 start_off += sb->s_blocksize;
831 * Very large extends have the potential to lock up
832 * the cpu for extended periods of time.
842 * A tail_to_skip value > 0 indicates that we're being called from
843 * ocfs2_file_aio_write(). This has the following implications:
845 * - we don't want to update i_size
846 * - di_bh will be NULL, which is fine because it's only used in the
847 * case where we want to update i_size.
848 * - ocfs2_zero_extend() will then only be filling the hole created
849 * between i_size and the start of the write.
851 static int ocfs2_extend_file(struct inode *inode,
852 struct buffer_head *di_bh,
857 u32 clusters_to_add = 0;
859 BUG_ON(!tail_to_skip && !di_bh);
861 /* setattr sometimes calls us like this. */
865 if (i_size_read(inode) == new_i_size)
867 BUG_ON(new_i_size < i_size_read(inode));
869 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
870 BUG_ON(tail_to_skip != 0);
871 goto out_update_size;
874 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
875 OCFS2_I(inode)->ip_clusters;
878 * protect the pages that ocfs2_zero_extend is going to be
879 * pulling into the page cache.. we do this before the
880 * metadata extend so that we don't get into the situation
881 * where we've extended the metadata but can't get the data
884 ret = ocfs2_data_lock(inode, 1);
890 if (clusters_to_add) {
891 ret = ocfs2_extend_allocation(inode, clusters_to_add);
899 * Call this even if we don't add any clusters to the tree. We
900 * still need to zero the area between the old i_size and the
903 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
911 /* We're being called from ocfs2_setattr() which wants
912 * us to update i_size */
913 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
919 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
920 ocfs2_data_unlock(inode, 1);
926 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
928 int status = 0, size_change;
929 struct inode *inode = dentry->d_inode;
930 struct super_block *sb = inode->i_sb;
931 struct ocfs2_super *osb = OCFS2_SB(sb);
932 struct buffer_head *bh = NULL;
933 handle_t *handle = NULL;
935 mlog_entry("(0x%p, '%.*s')\n", dentry,
936 dentry->d_name.len, dentry->d_name.name);
938 if (attr->ia_valid & ATTR_MODE)
939 mlog(0, "mode change: %d\n", attr->ia_mode);
940 if (attr->ia_valid & ATTR_UID)
941 mlog(0, "uid change: %d\n", attr->ia_uid);
942 if (attr->ia_valid & ATTR_GID)
943 mlog(0, "gid change: %d\n", attr->ia_gid);
944 if (attr->ia_valid & ATTR_SIZE)
945 mlog(0, "size change...\n");
946 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
947 mlog(0, "time change...\n");
949 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
950 | ATTR_GID | ATTR_UID | ATTR_MODE)
951 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
952 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
956 status = inode_change_ok(inode, attr);
960 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
962 status = ocfs2_rw_lock(inode, 1);
969 status = ocfs2_meta_lock(inode, &bh, 1);
971 if (status != -ENOENT)
976 if (size_change && attr->ia_size != i_size_read(inode)) {
977 if (i_size_read(inode) > attr->ia_size)
978 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
980 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
982 if (status != -ENOSPC)
989 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
990 if (IS_ERR(handle)) {
991 status = PTR_ERR(handle);
996 status = inode_setattr(inode, attr);
1002 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1007 ocfs2_commit_trans(osb, handle);
1009 ocfs2_meta_unlock(inode, 1);
1012 ocfs2_rw_unlock(inode, 1);
1021 int ocfs2_getattr(struct vfsmount *mnt,
1022 struct dentry *dentry,
1025 struct inode *inode = dentry->d_inode;
1026 struct super_block *sb = dentry->d_inode->i_sb;
1027 struct ocfs2_super *osb = sb->s_fs_info;
1032 err = ocfs2_inode_revalidate(dentry);
1039 generic_fillattr(inode, stat);
1041 /* We set the blksize from the cluster size for performance */
1042 stat->blksize = osb->s_clustersize;
1050 int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1056 ret = ocfs2_meta_lock(inode, NULL, 0);
1063 ret = generic_permission(inode, mask, NULL);
1065 ocfs2_meta_unlock(inode, 0);
1071 static int ocfs2_write_remove_suid(struct inode *inode)
1074 struct buffer_head *bh = NULL;
1075 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1077 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1078 struct ocfs2_dinode *di;
1080 mlog_entry("(Inode %llu, mode 0%o)\n",
1081 (unsigned long long)oi->ip_blkno, inode->i_mode);
1083 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1084 if (handle == NULL) {
1090 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1096 ret = ocfs2_journal_access(handle, inode, bh,
1097 OCFS2_JOURNAL_ACCESS_WRITE);
1103 inode->i_mode &= ~S_ISUID;
1104 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1105 inode->i_mode &= ~S_ISGID;
1107 di = (struct ocfs2_dinode *) bh->b_data;
1108 di->i_mode = cpu_to_le16(inode->i_mode);
1110 ret = ocfs2_journal_dirty(handle, bh);
1116 ocfs2_commit_trans(osb, handle);
1123 * Will look for holes and unwritten extents in the range starting at
1124 * pos for count bytes (inclusive).
1126 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1130 u32 cpos, clusters, extent_len, phys_cpos;
1131 struct super_block *sb = inode->i_sb;
1133 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1134 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1137 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len);
1143 if (phys_cpos == 0) {
1148 if (extent_len > clusters)
1149 extent_len = clusters;
1151 clusters -= extent_len;
1158 static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1164 int ret = 0, meta_level = appending;
1165 struct inode *inode = dentry->d_inode;
1167 loff_t newsize, saved_pos;
1170 * We sample i_size under a read level meta lock to see if our write
1171 * is extending the file, if it is we back off and get a write level
1175 ret = ocfs2_meta_lock(inode, NULL, meta_level);
1182 /* Clear suid / sgid if necessary. We do this here
1183 * instead of later in the write path because
1184 * remove_suid() calls ->setattr without any hint that
1185 * we may have already done our cluster locking. Since
1186 * ocfs2_setattr() *must* take cluster locks to
1187 * proceeed, this will lead us to recursively lock the
1188 * inode. There's also the dinode i_size state which
1189 * can be lost via setattr during extending writes (we
1190 * set inode->i_size at the end of a write. */
1191 if (should_remove_suid(dentry)) {
1192 if (meta_level == 0) {
1193 ocfs2_meta_unlock(inode, meta_level);
1198 ret = ocfs2_write_remove_suid(inode);
1205 /* work on a copy of ppos until we're sure that we won't have
1206 * to recalculate it due to relocking. */
1208 saved_pos = i_size_read(inode);
1209 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1214 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1215 loff_t end = saved_pos + count;
1218 * Skip the O_DIRECT checks if we don't need
1221 if (!direct_io || !(*direct_io))
1225 * Allowing concurrent direct writes means
1226 * i_size changes wouldn't be synchronized, so
1227 * one node could wind up truncating another
1230 if (end > i_size_read(inode)) {
1236 * We don't fill holes during direct io, so
1237 * check for them here. If any are found, the
1238 * caller will have to retake some cluster
1239 * locks and initiate the io as buffered.
1241 ret = ocfs2_check_range_for_holes(inode, saved_pos,
1252 * The rest of this loop is concerned with legacy file
1253 * systems which don't support sparse files.
1256 newsize = count + saved_pos;
1258 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1259 (long long) saved_pos, (long long) newsize,
1260 (long long) i_size_read(inode));
1262 /* No need for a higher level metadata lock if we're
1263 * never going past i_size. */
1264 if (newsize <= i_size_read(inode))
1267 if (meta_level == 0) {
1268 ocfs2_meta_unlock(inode, meta_level);
1273 spin_lock(&OCFS2_I(inode)->ip_lock);
1274 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1275 OCFS2_I(inode)->ip_clusters;
1276 spin_unlock(&OCFS2_I(inode)->ip_lock);
1278 mlog(0, "Writing at EOF, may need more allocation: "
1279 "i_size = %lld, newsize = %lld, need %u clusters\n",
1280 (long long) i_size_read(inode), (long long) newsize,
1283 /* We only want to continue the rest of this loop if
1284 * our extend will actually require more
1289 ret = ocfs2_extend_file(inode, NULL, newsize, count);
1302 ocfs2_meta_unlock(inode, meta_level);
1309 ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1311 const struct iovec *iov = *iovp;
1312 size_t base = *basep;
1315 int copy = min(bytes, iov->iov_len - base);
1319 if (iov->iov_len == base) {
1328 static struct page * ocfs2_get_write_source(struct ocfs2_buffered_write_priv *bp,
1329 const struct iovec *cur_iov,
1334 struct page *src_page = NULL;
1336 buf = cur_iov->iov_base + iov_offset;
1338 if (!segment_eq(get_fs(), KERNEL_DS)) {
1340 * Pull in the user page. We want to do this outside
1341 * of the meta data locks in order to preserve locking
1342 * order in case of page fault.
1344 ret = get_user_pages(current, current->mm,
1345 (unsigned long)buf & PAGE_CACHE_MASK, 1,
1346 0, 0, &src_page, NULL);
1348 bp->b_src_buf = kmap(src_page);
1350 src_page = ERR_PTR(-EFAULT);
1352 bp->b_src_buf = buf;
1358 static void ocfs2_put_write_source(struct ocfs2_buffered_write_priv *bp,
1363 page_cache_release(page);
1367 static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1368 const struct iovec *iov,
1369 unsigned long nr_segs,
1371 ssize_t o_direct_written)
1374 ssize_t copied, total = 0;
1375 size_t iov_offset = 0;
1376 const struct iovec *cur_iov = iov;
1377 struct ocfs2_buffered_write_priv bp;
1381 * handle partial DIO write. Adjust cur_iov if needed.
1383 ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1386 bp.b_cur_off = iov_offset;
1387 bp.b_cur_iov = cur_iov;
1389 page = ocfs2_get_write_source(&bp, cur_iov, iov_offset);
1391 ret = PTR_ERR(page);
1395 copied = ocfs2_buffered_write_cluster(file, *ppos, count,
1396 ocfs2_map_and_write_user_data,
1399 ocfs2_put_write_source(&bp, page);
1408 *ppos = *ppos + copied;
1411 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1415 return total ? total : ret;
1418 static int ocfs2_check_iovec(const struct iovec *iov, size_t *counted,
1419 unsigned long *nr_segs)
1421 size_t ocount; /* original count */
1425 for (seg = 0; seg < *nr_segs; seg++) {
1426 const struct iovec *iv = &iov[seg];
1429 * If any segment has a negative length, or the cumulative
1430 * length ever wraps negative then return -EINVAL.
1432 ocount += iv->iov_len;
1433 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
1435 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1440 ocount -= iv->iov_len; /* This segment is no good */
1448 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1449 const struct iovec *iov,
1450 unsigned long nr_segs,
1453 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
1454 int can_do_direct, sync = 0;
1455 ssize_t written = 0;
1456 size_t ocount; /* original count */
1457 size_t count; /* after file limit checks */
1458 loff_t *ppos = &iocb->ki_pos;
1459 struct file *file = iocb->ki_filp;
1460 struct inode *inode = file->f_path.dentry->d_inode;
1462 mlog_entry("(0x%p, %u, '%.*s')\n", file,
1463 (unsigned int)nr_segs,
1464 file->f_path.dentry->d_name.len,
1465 file->f_path.dentry->d_name.name);
1467 if (iocb->ki_left == 0)
1470 ret = ocfs2_check_iovec(iov, &ocount, &nr_segs);
1476 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1478 appending = file->f_flags & O_APPEND ? 1 : 0;
1479 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1481 mutex_lock(&inode->i_mutex);
1484 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1486 down_read(&inode->i_alloc_sem);
1490 /* concurrent O_DIRECT writes are allowed */
1491 rw_level = !direct_io;
1492 ret = ocfs2_rw_lock(inode, rw_level);
1498 can_do_direct = direct_io;
1499 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1500 iocb->ki_left, appending,
1508 * We can't complete the direct I/O as requested, fall back to
1511 if (direct_io && !can_do_direct) {
1512 ocfs2_rw_unlock(inode, rw_level);
1513 up_read(&inode->i_alloc_sem);
1523 if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
1527 * XXX: Is it ok to execute these checks a second time?
1529 ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
1534 * Set pos so that sync_page_range_nolock() below understands
1535 * where to start from. We might've moved it around via the
1536 * calls above. The range we want to actually sync starts from
1542 /* communicate with ocfs2_dio_end_io */
1543 ocfs2_iocb_set_rw_locked(iocb);
1546 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1547 ppos, count, ocount);
1553 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
1557 if (ret != -EFAULT || ret != -ENOSPC)
1564 /* buffered aio wouldn't have proper lock coverage today */
1565 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
1568 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1569 * function pointer which is called when o_direct io completes so that
1570 * it can unlock our rw lock. (it's the clustered equivalent of
1571 * i_alloc_sem; protects truncate from racing with pending ios).
1572 * Unfortunately there are error cases which call end_io and others
1573 * that don't. so we don't have to unlock the rw_lock if either an
1574 * async dio is going to do it in the future or an end_io after an
1575 * error has already done it.
1577 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1584 ocfs2_rw_unlock(inode, rw_level);
1588 up_read(&inode->i_alloc_sem);
1590 if (written > 0 && sync) {
1593 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
1598 mutex_unlock(&inode->i_mutex);
1601 return written ? written : ret;
1604 static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
1605 struct pipe_buffer *buf,
1606 struct splice_desc *sd)
1608 int ret, count, total = 0;
1610 struct ocfs2_splice_write_priv sp;
1612 ret = buf->ops->pin(pipe, buf);
1619 sp.s_offset = sd->pos & ~PAGE_CACHE_MASK;
1620 sp.s_buf_offset = buf->offset;
1623 if (count + sp.s_offset > PAGE_CACHE_SIZE)
1624 count = PAGE_CACHE_SIZE - sp.s_offset;
1628 * splice wants us to copy up to one page at a
1629 * time. For pagesize > cluster size, this means we
1630 * might enter ocfs2_buffered_write_cluster() more
1631 * than once, so keep track of our progress here.
1633 copied = ocfs2_buffered_write_cluster(sd->file,
1634 (loff_t)sd->pos + total,
1636 ocfs2_map_and_write_splice_data,
1645 sp.s_offset += copied;
1646 sp.s_buf_offset += copied;
1653 return total ? total : ret;
1656 static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1663 struct address_space *mapping = out->f_mapping;
1664 struct inode *inode = mapping->host;
1666 ret = __splice_from_pipe(pipe, out, ppos, len, flags,
1667 ocfs2_splice_write_actor);
1671 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
1672 err = generic_osync_inode(inode, mapping,
1673 OSYNC_METADATA|OSYNC_DATA);
1682 static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1689 struct inode *inode = out->f_path.dentry->d_inode;
1691 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1693 out->f_path.dentry->d_name.len,
1694 out->f_path.dentry->d_name.name);
1696 inode_double_lock(inode, pipe->inode);
1698 ret = ocfs2_rw_lock(inode, 1);
1704 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
1711 /* ok, we're done with i_size and alloc work */
1712 ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
1715 ocfs2_rw_unlock(inode, 1);
1717 inode_double_unlock(inode, pipe->inode);
1723 static ssize_t ocfs2_file_splice_read(struct file *in,
1725 struct pipe_inode_info *pipe,
1730 struct inode *inode = in->f_path.dentry->d_inode;
1732 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1734 in->f_path.dentry->d_name.len,
1735 in->f_path.dentry->d_name.name);
1738 * See the comment in ocfs2_file_aio_read()
1740 ret = ocfs2_meta_lock(inode, NULL, 0);
1745 ocfs2_meta_unlock(inode, 0);
1747 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1754 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
1755 const struct iovec *iov,
1756 unsigned long nr_segs,
1759 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
1760 struct file *filp = iocb->ki_filp;
1761 struct inode *inode = filp->f_path.dentry->d_inode;
1763 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1764 (unsigned int)nr_segs,
1765 filp->f_path.dentry->d_name.len,
1766 filp->f_path.dentry->d_name.name);
1775 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1776 * need locks to protect pending reads from racing with truncate.
1778 if (filp->f_flags & O_DIRECT) {
1779 down_read(&inode->i_alloc_sem);
1782 ret = ocfs2_rw_lock(inode, 0);
1788 /* communicate with ocfs2_dio_end_io */
1789 ocfs2_iocb_set_rw_locked(iocb);
1793 * We're fine letting folks race truncates and extending
1794 * writes with read across the cluster, just like they can
1795 * locally. Hence no rw_lock during read.
1797 * Take and drop the meta data lock to update inode fields
1798 * like i_size. This allows the checks down below
1799 * generic_file_aio_read() a chance of actually working.
1801 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1806 ocfs2_meta_unlock(inode, lock_level);
1808 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
1810 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1812 /* buffered aio wouldn't have proper lock coverage today */
1813 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1815 /* see ocfs2_file_aio_write */
1816 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1823 up_read(&inode->i_alloc_sem);
1825 ocfs2_rw_unlock(inode, rw_level);
1831 const struct inode_operations ocfs2_file_iops = {
1832 .setattr = ocfs2_setattr,
1833 .getattr = ocfs2_getattr,
1834 .permission = ocfs2_permission,
1837 const struct inode_operations ocfs2_special_file_iops = {
1838 .setattr = ocfs2_setattr,
1839 .getattr = ocfs2_getattr,
1840 .permission = ocfs2_permission,
1843 const struct file_operations ocfs2_fops = {
1844 .read = do_sync_read,
1845 .write = do_sync_write,
1846 .sendfile = generic_file_sendfile,
1848 .fsync = ocfs2_sync_file,
1849 .release = ocfs2_file_release,
1850 .open = ocfs2_file_open,
1851 .aio_read = ocfs2_file_aio_read,
1852 .aio_write = ocfs2_file_aio_write,
1853 .ioctl = ocfs2_ioctl,
1854 .splice_read = ocfs2_file_splice_read,
1855 .splice_write = ocfs2_file_splice_write,
1858 const struct file_operations ocfs2_dops = {
1859 .read = generic_read_dir,
1860 .readdir = ocfs2_readdir,
1861 .fsync = ocfs2_sync_file,
1862 .ioctl = ocfs2_ioctl,