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>
37 #define MLOG_MASK_PREFIX ML_INODE
38 #include <cluster/masklog.h>
46 #include "extent_map.h"
56 #include "buffer_head_io.h"
58 static int ocfs2_sync_inode(struct inode
*inode
)
60 filemap_fdatawrite(inode
->i_mapping
);
61 return sync_mapping_buffers(inode
->i_mapping
);
64 static int ocfs2_file_open(struct inode
*inode
, struct file
*file
)
67 int mode
= file
->f_flags
;
68 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
70 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode
, file
,
71 file
->f_path
.dentry
->d_name
.len
, file
->f_path
.dentry
->d_name
.name
);
73 spin_lock(&oi
->ip_lock
);
75 /* Check that the inode hasn't been wiped from disk by another
76 * node. If it hasn't then we're safe as long as we hold the
77 * spin lock until our increment of open count. */
78 if (OCFS2_I(inode
)->ip_flags
& OCFS2_INODE_DELETED
) {
79 spin_unlock(&oi
->ip_lock
);
86 oi
->ip_flags
|= OCFS2_INODE_OPEN_DIRECT
;
89 spin_unlock(&oi
->ip_lock
);
96 static int ocfs2_file_release(struct inode
*inode
, struct file
*file
)
98 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
100 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode
, file
,
101 file
->f_path
.dentry
->d_name
.len
,
102 file
->f_path
.dentry
->d_name
.name
);
104 spin_lock(&oi
->ip_lock
);
105 if (!--oi
->ip_open_count
)
106 oi
->ip_flags
&= ~OCFS2_INODE_OPEN_DIRECT
;
107 spin_unlock(&oi
->ip_lock
);
114 static int ocfs2_sync_file(struct file
*file
,
115 struct dentry
*dentry
,
120 struct inode
*inode
= dentry
->d_inode
;
121 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
123 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file
, dentry
, datasync
,
124 dentry
->d_name
.len
, dentry
->d_name
.name
);
126 err
= ocfs2_sync_inode(dentry
->d_inode
);
130 journal
= osb
->journal
->j_journal
;
131 err
= journal_force_commit(journal
);
136 return (err
< 0) ? -EIO
: 0;
139 int ocfs2_should_update_atime(struct inode
*inode
,
140 struct vfsmount
*vfsmnt
)
143 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
145 if (ocfs2_is_hard_readonly(osb
) || ocfs2_is_soft_readonly(osb
))
148 if ((inode
->i_flags
& S_NOATIME
) ||
149 ((inode
->i_sb
->s_flags
& MS_NODIRATIME
) && S_ISDIR(inode
->i_mode
)))
153 * We can be called with no vfsmnt structure - NFSD will
156 * Note that our action here is different than touch_atime() -
157 * if we can't tell whether this is a noatime mount, then we
158 * don't know whether to trust the value of s_atime_quantum.
163 if ((vfsmnt
->mnt_flags
& MNT_NOATIME
) ||
164 ((vfsmnt
->mnt_flags
& MNT_NODIRATIME
) && S_ISDIR(inode
->i_mode
)))
167 if (vfsmnt
->mnt_flags
& MNT_RELATIME
) {
168 if ((timespec_compare(&inode
->i_atime
, &inode
->i_mtime
) <= 0) ||
169 (timespec_compare(&inode
->i_atime
, &inode
->i_ctime
) <= 0))
176 if ((now
.tv_sec
- inode
->i_atime
.tv_sec
<= osb
->s_atime_quantum
))
182 int ocfs2_update_inode_atime(struct inode
*inode
,
183 struct buffer_head
*bh
)
186 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
191 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
192 if (handle
== NULL
) {
198 inode
->i_atime
= CURRENT_TIME
;
199 ret
= ocfs2_mark_inode_dirty(handle
, inode
, bh
);
203 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
209 int ocfs2_set_inode_size(handle_t
*handle
,
211 struct buffer_head
*fe_bh
,
217 i_size_write(inode
, new_i_size
);
218 inode
->i_blocks
= ocfs2_align_bytes_to_sectors(new_i_size
);
219 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
221 status
= ocfs2_mark_inode_dirty(handle
, inode
, fe_bh
);
232 static int ocfs2_simple_size_update(struct inode
*inode
,
233 struct buffer_head
*di_bh
,
237 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
238 handle_t
*handle
= NULL
;
240 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
241 if (handle
== NULL
) {
247 ret
= ocfs2_set_inode_size(handle
, inode
, di_bh
,
252 ocfs2_commit_trans(osb
, handle
);
257 static int ocfs2_orphan_for_truncate(struct ocfs2_super
*osb
,
259 struct buffer_head
*fe_bh
,
267 /* TODO: This needs to actually orphan the inode in this
270 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
271 if (IS_ERR(handle
)) {
272 status
= PTR_ERR(handle
);
277 status
= ocfs2_set_inode_size(handle
, inode
, fe_bh
, new_i_size
);
281 ocfs2_commit_trans(osb
, handle
);
287 static int ocfs2_truncate_file(struct inode
*inode
,
288 struct buffer_head
*di_bh
,
292 struct ocfs2_dinode
*fe
= NULL
;
293 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
294 struct ocfs2_truncate_context
*tc
= NULL
;
296 mlog_entry("(inode = %llu, new_i_size = %llu\n",
297 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
298 (unsigned long long)new_i_size
);
300 truncate_inode_pages(inode
->i_mapping
, new_i_size
);
302 fe
= (struct ocfs2_dinode
*) di_bh
->b_data
;
303 if (!OCFS2_IS_VALID_DINODE(fe
)) {
304 OCFS2_RO_ON_INVALID_DINODE(inode
->i_sb
, fe
);
309 mlog_bug_on_msg(le64_to_cpu(fe
->i_size
) != i_size_read(inode
),
310 "Inode %llu, inode i_size = %lld != di "
311 "i_size = %llu, i_flags = 0x%x\n",
312 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
314 (unsigned long long)le64_to_cpu(fe
->i_size
),
315 le32_to_cpu(fe
->i_flags
));
317 if (new_i_size
> le64_to_cpu(fe
->i_size
)) {
318 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
319 (unsigned long long)le64_to_cpu(fe
->i_size
),
320 (unsigned long long)new_i_size
);
326 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
327 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
328 (unsigned long long)le64_to_cpu(fe
->i_size
),
329 (unsigned long long)new_i_size
);
331 /* lets handle the simple truncate cases before doing any more
332 * cluster locking. */
333 if (new_i_size
== le64_to_cpu(fe
->i_size
))
336 /* This forces other nodes to sync and drop their pages. Do
337 * this even if we have a truncate without allocation change -
338 * ocfs2 cluster sizes can be much greater than page size, so
339 * we have to truncate them anyway. */
340 status
= ocfs2_data_lock(inode
, 1);
345 ocfs2_data_unlock(inode
, 1);
347 if (le32_to_cpu(fe
->i_clusters
) ==
348 ocfs2_clusters_for_bytes(osb
->sb
, new_i_size
)) {
349 mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
351 /* No allocation change is required, so lets fast path
353 status
= ocfs2_simple_size_update(inode
, di_bh
, new_i_size
);
359 /* alright, we're going to need to do a full blown alloc size
360 * change. Orphan the inode so that recovery can complete the
361 * truncate if necessary. This does the task of marking
363 status
= ocfs2_orphan_for_truncate(osb
, inode
, di_bh
, new_i_size
);
369 status
= ocfs2_prepare_truncate(osb
, inode
, di_bh
, &tc
);
375 status
= ocfs2_commit_truncate(osb
, inode
, di_bh
, tc
);
381 /* TODO: orphan dir cleanup here. */
389 * extend allocation only here.
390 * we'll update all the disk stuff, and oip->alloc_size
392 * expect stuff to be locked, a transaction started and enough data /
393 * metadata reservations in the contexts.
395 * Will return -EAGAIN, and a reason if a restart is needed.
396 * If passed in, *reason will always be set, even in error.
398 int ocfs2_do_extend_allocation(struct ocfs2_super
*osb
,
401 struct buffer_head
*fe_bh
,
403 struct ocfs2_alloc_context
*data_ac
,
404 struct ocfs2_alloc_context
*meta_ac
,
405 enum ocfs2_alloc_restarted
*reason_ret
)
409 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
410 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
411 u32 bit_off
, num_bits
;
414 BUG_ON(!clusters_to_add
);
416 free_extents
= ocfs2_num_free_extents(osb
, inode
, fe
);
417 if (free_extents
< 0) {
418 status
= free_extents
;
423 /* there are two cases which could cause us to EAGAIN in the
424 * we-need-more-metadata case:
425 * 1) we haven't reserved *any*
426 * 2) we are so fragmented, we've needed to add metadata too
428 if (!free_extents
&& !meta_ac
) {
429 mlog(0, "we haven't reserved any metadata!\n");
431 reason
= RESTART_META
;
433 } else if ((!free_extents
)
434 && (ocfs2_alloc_context_bits_left(meta_ac
)
435 < ocfs2_extend_meta_needed(fe
))) {
436 mlog(0, "filesystem is really fragmented...\n");
438 reason
= RESTART_META
;
442 status
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
443 &bit_off
, &num_bits
);
445 if (status
!= -ENOSPC
)
450 BUG_ON(num_bits
> clusters_to_add
);
452 /* reserve our write early -- insert_extent may update the inode */
453 status
= ocfs2_journal_access(handle
, inode
, fe_bh
,
454 OCFS2_JOURNAL_ACCESS_WRITE
);
460 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
461 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
462 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
463 status
= ocfs2_insert_extent(osb
, handle
, inode
, fe_bh
, block
,
470 le32_add_cpu(&fe
->i_clusters
, num_bits
);
471 spin_lock(&OCFS2_I(inode
)->ip_lock
);
472 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
473 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
475 status
= ocfs2_journal_dirty(handle
, fe_bh
);
481 clusters_to_add
-= num_bits
;
483 if (clusters_to_add
) {
484 mlog(0, "need to alloc once more, clusters = %u, wanted = "
485 "%u\n", fe
->i_clusters
, clusters_to_add
);
487 reason
= RESTART_TRANS
;
493 *reason_ret
= reason
;
497 static int ocfs2_extend_allocation(struct inode
*inode
,
501 int restart_func
= 0;
502 int drop_alloc_sem
= 0;
503 int credits
, num_free_extents
;
505 struct buffer_head
*bh
= NULL
;
506 struct ocfs2_dinode
*fe
= NULL
;
507 handle_t
*handle
= NULL
;
508 struct ocfs2_alloc_context
*data_ac
= NULL
;
509 struct ocfs2_alloc_context
*meta_ac
= NULL
;
510 enum ocfs2_alloc_restarted why
;
511 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
513 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add
);
515 status
= ocfs2_read_block(osb
, OCFS2_I(inode
)->ip_blkno
, &bh
,
516 OCFS2_BH_CACHED
, inode
);
522 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
523 if (!OCFS2_IS_VALID_DINODE(fe
)) {
524 OCFS2_RO_ON_INVALID_DINODE(inode
->i_sb
, fe
);
530 BUG_ON(le32_to_cpu(fe
->i_clusters
) != OCFS2_I(inode
)->ip_clusters
);
532 mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
533 "clusters_to_add = %u\n",
534 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i_size_read(inode
),
535 fe
->i_clusters
, clusters_to_add
);
537 num_free_extents
= ocfs2_num_free_extents(osb
,
540 if (num_free_extents
< 0) {
541 status
= num_free_extents
;
546 if (!num_free_extents
) {
547 status
= ocfs2_reserve_new_metadata(osb
, fe
, &meta_ac
);
549 if (status
!= -ENOSPC
)
555 status
= ocfs2_reserve_clusters(osb
, clusters_to_add
, &data_ac
);
557 if (status
!= -ENOSPC
)
562 /* blocks peope in read/write from reading our allocation
563 * until we're done changing it. We depend on i_mutex to block
564 * other extend/truncate calls while we're here. Ordering wrt
565 * start_trans is important here -- always do it before! */
566 down_write(&OCFS2_I(inode
)->ip_alloc_sem
);
569 credits
= ocfs2_calc_extend_credits(osb
->sb
, fe
, clusters_to_add
);
570 handle
= ocfs2_start_trans(osb
, credits
);
571 if (IS_ERR(handle
)) {
572 status
= PTR_ERR(handle
);
578 restarted_transaction
:
579 /* reserve a write to the file entry early on - that we if we
580 * run out of credits in the allocation path, we can still
582 status
= ocfs2_journal_access(handle
, inode
, bh
,
583 OCFS2_JOURNAL_ACCESS_WRITE
);
589 prev_clusters
= OCFS2_I(inode
)->ip_clusters
;
591 status
= ocfs2_do_extend_allocation(osb
,
599 if ((status
< 0) && (status
!= -EAGAIN
)) {
600 if (status
!= -ENOSPC
)
605 status
= ocfs2_journal_dirty(handle
, bh
);
611 spin_lock(&OCFS2_I(inode
)->ip_lock
);
612 clusters_to_add
-= (OCFS2_I(inode
)->ip_clusters
- prev_clusters
);
613 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
615 if (why
!= RESTART_NONE
&& clusters_to_add
) {
616 if (why
== RESTART_META
) {
617 mlog(0, "restarting function.\n");
620 BUG_ON(why
!= RESTART_TRANS
);
622 mlog(0, "restarting transaction.\n");
623 /* TODO: This can be more intelligent. */
624 credits
= ocfs2_calc_extend_credits(osb
->sb
,
627 status
= ocfs2_extend_trans(handle
, credits
);
629 /* handle still has to be committed at
635 goto restarted_transaction
;
639 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
640 fe
->i_clusters
, (unsigned long long)fe
->i_size
);
641 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
642 OCFS2_I(inode
)->ip_clusters
, i_size_read(inode
));
645 if (drop_alloc_sem
) {
646 up_write(&OCFS2_I(inode
)->ip_alloc_sem
);
650 ocfs2_commit_trans(osb
, handle
);
654 ocfs2_free_alloc_context(data_ac
);
658 ocfs2_free_alloc_context(meta_ac
);
661 if ((!status
) && restart_func
) {
674 /* Some parts of this taken from generic_cont_expand, which turned out
675 * to be too fragile to do exactly what we need without us having to
676 * worry about recursive locking in ->prepare_write() and
677 * ->commit_write(). */
678 static int ocfs2_write_zero_page(struct inode
*inode
,
681 struct address_space
*mapping
= inode
->i_mapping
;
685 handle_t
*handle
= NULL
;
688 offset
= (size
& (PAGE_CACHE_SIZE
-1)); /* Within page */
689 /* ugh. in prepare/commit_write, if from==to==start of block, we
690 ** skip the prepare. make sure we never send an offset for the start
693 if ((offset
& (inode
->i_sb
->s_blocksize
- 1)) == 0) {
696 index
= size
>> PAGE_CACHE_SHIFT
;
698 page
= grab_cache_page(mapping
, index
);
705 ret
= ocfs2_prepare_write_nolock(inode
, page
, offset
, offset
);
711 if (ocfs2_should_order_data(inode
)) {
712 handle
= ocfs2_start_walk_page_trans(inode
, page
, offset
,
714 if (IS_ERR(handle
)) {
715 ret
= PTR_ERR(handle
);
721 /* must not update i_size! */
722 ret
= block_commit_write(page
, offset
, offset
);
729 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
732 page_cache_release(page
);
737 static int ocfs2_zero_extend(struct inode
*inode
,
742 struct super_block
*sb
= inode
->i_sb
;
744 start_off
= ocfs2_align_bytes_to_blocks(sb
, i_size_read(inode
));
745 while (start_off
< zero_to_size
) {
746 ret
= ocfs2_write_zero_page(inode
, start_off
);
752 start_off
+= sb
->s_blocksize
;
755 * Very large extends have the potential to lock up
756 * the cpu for extended periods of time.
766 * A tail_to_skip value > 0 indicates that we're being called from
767 * ocfs2_file_aio_write(). This has the following implications:
769 * - we don't want to update i_size
770 * - di_bh will be NULL, which is fine because it's only used in the
771 * case where we want to update i_size.
772 * - ocfs2_zero_extend() will then only be filling the hole created
773 * between i_size and the start of the write.
775 static int ocfs2_extend_file(struct inode
*inode
,
776 struct buffer_head
*di_bh
,
783 BUG_ON(!tail_to_skip
&& !di_bh
);
785 /* setattr sometimes calls us like this. */
789 if (i_size_read(inode
) == new_i_size
)
791 BUG_ON(new_i_size
< i_size_read(inode
));
793 clusters_to_add
= ocfs2_clusters_for_bytes(inode
->i_sb
, new_i_size
) -
794 OCFS2_I(inode
)->ip_clusters
;
797 * protect the pages that ocfs2_zero_extend is going to be
798 * pulling into the page cache.. we do this before the
799 * metadata extend so that we don't get into the situation
800 * where we've extended the metadata but can't get the data
803 ret
= ocfs2_data_lock(inode
, 1);
809 if (clusters_to_add
) {
810 ret
= ocfs2_extend_allocation(inode
, clusters_to_add
);
818 * Call this even if we don't add any clusters to the tree. We
819 * still need to zero the area between the old i_size and the
822 ret
= ocfs2_zero_extend(inode
, (u64
)new_i_size
- tail_to_skip
);
829 /* We're being called from ocfs2_setattr() which wants
830 * us to update i_size */
831 ret
= ocfs2_simple_size_update(inode
, di_bh
, new_i_size
);
837 ocfs2_data_unlock(inode
, 1);
843 int ocfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
845 int status
= 0, size_change
;
846 struct inode
*inode
= dentry
->d_inode
;
847 struct super_block
*sb
= inode
->i_sb
;
848 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
849 struct buffer_head
*bh
= NULL
;
850 handle_t
*handle
= NULL
;
852 mlog_entry("(0x%p, '%.*s')\n", dentry
,
853 dentry
->d_name
.len
, dentry
->d_name
.name
);
855 if (attr
->ia_valid
& ATTR_MODE
)
856 mlog(0, "mode change: %d\n", attr
->ia_mode
);
857 if (attr
->ia_valid
& ATTR_UID
)
858 mlog(0, "uid change: %d\n", attr
->ia_uid
);
859 if (attr
->ia_valid
& ATTR_GID
)
860 mlog(0, "gid change: %d\n", attr
->ia_gid
);
861 if (attr
->ia_valid
& ATTR_SIZE
)
862 mlog(0, "size change...\n");
863 if (attr
->ia_valid
& (ATTR_ATIME
| ATTR_MTIME
| ATTR_CTIME
))
864 mlog(0, "time change...\n");
866 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
867 | ATTR_GID | ATTR_UID | ATTR_MODE)
868 if (!(attr
->ia_valid
& OCFS2_VALID_ATTRS
)) {
869 mlog(0, "can't handle attrs: 0x%x\n", attr
->ia_valid
);
873 status
= inode_change_ok(inode
, attr
);
877 size_change
= S_ISREG(inode
->i_mode
) && attr
->ia_valid
& ATTR_SIZE
;
879 status
= ocfs2_rw_lock(inode
, 1);
886 status
= ocfs2_meta_lock(inode
, &bh
, 1);
888 if (status
!= -ENOENT
)
893 if (size_change
&& attr
->ia_size
!= i_size_read(inode
)) {
894 if (i_size_read(inode
) > attr
->ia_size
)
895 status
= ocfs2_truncate_file(inode
, bh
, attr
->ia_size
);
897 status
= ocfs2_extend_file(inode
, bh
, attr
->ia_size
, 0);
899 if (status
!= -ENOSPC
)
906 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
907 if (IS_ERR(handle
)) {
908 status
= PTR_ERR(handle
);
913 status
= inode_setattr(inode
, attr
);
919 status
= ocfs2_mark_inode_dirty(handle
, inode
, bh
);
924 ocfs2_commit_trans(osb
, handle
);
926 ocfs2_meta_unlock(inode
, 1);
929 ocfs2_rw_unlock(inode
, 1);
938 int ocfs2_getattr(struct vfsmount
*mnt
,
939 struct dentry
*dentry
,
942 struct inode
*inode
= dentry
->d_inode
;
943 struct super_block
*sb
= dentry
->d_inode
->i_sb
;
944 struct ocfs2_super
*osb
= sb
->s_fs_info
;
949 err
= ocfs2_inode_revalidate(dentry
);
956 generic_fillattr(inode
, stat
);
958 /* We set the blksize from the cluster size for performance */
959 stat
->blksize
= osb
->s_clustersize
;
967 int ocfs2_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
973 ret
= ocfs2_meta_lock(inode
, NULL
, 0);
979 ret
= generic_permission(inode
, mask
, NULL
);
981 ocfs2_meta_unlock(inode
, 0);
987 static int ocfs2_write_remove_suid(struct inode
*inode
)
990 struct buffer_head
*bh
= NULL
;
991 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
993 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
994 struct ocfs2_dinode
*di
;
996 mlog_entry("(Inode %llu, mode 0%o)\n",
997 (unsigned long long)oi
->ip_blkno
, inode
->i_mode
);
999 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
1000 if (handle
== NULL
) {
1006 ret
= ocfs2_read_block(osb
, oi
->ip_blkno
, &bh
, OCFS2_BH_CACHED
, inode
);
1012 ret
= ocfs2_journal_access(handle
, inode
, bh
,
1013 OCFS2_JOURNAL_ACCESS_WRITE
);
1019 inode
->i_mode
&= ~S_ISUID
;
1020 if ((inode
->i_mode
& S_ISGID
) && (inode
->i_mode
& S_IXGRP
))
1021 inode
->i_mode
&= ~S_ISGID
;
1023 di
= (struct ocfs2_dinode
*) bh
->b_data
;
1024 di
->i_mode
= cpu_to_le16(inode
->i_mode
);
1026 ret
= ocfs2_journal_dirty(handle
, bh
);
1032 ocfs2_commit_trans(osb
, handle
);
1038 static int ocfs2_prepare_inode_for_write(struct dentry
*dentry
,
1043 int ret
= 0, meta_level
= appending
;
1044 struct inode
*inode
= dentry
->d_inode
;
1046 loff_t newsize
, saved_pos
;
1049 * We sample i_size under a read level meta lock to see if our write
1050 * is extending the file, if it is we back off and get a write level
1054 ret
= ocfs2_meta_lock(inode
, NULL
, meta_level
);
1061 /* Clear suid / sgid if necessary. We do this here
1062 * instead of later in the write path because
1063 * remove_suid() calls ->setattr without any hint that
1064 * we may have already done our cluster locking. Since
1065 * ocfs2_setattr() *must* take cluster locks to
1066 * proceeed, this will lead us to recursively lock the
1067 * inode. There's also the dinode i_size state which
1068 * can be lost via setattr during extending writes (we
1069 * set inode->i_size at the end of a write. */
1070 if (should_remove_suid(dentry
)) {
1071 if (meta_level
== 0) {
1072 ocfs2_meta_unlock(inode
, meta_level
);
1077 ret
= ocfs2_write_remove_suid(inode
);
1084 /* work on a copy of ppos until we're sure that we won't have
1085 * to recalculate it due to relocking. */
1087 saved_pos
= i_size_read(inode
);
1088 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos
);
1092 newsize
= count
+ saved_pos
;
1094 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1095 (long long) saved_pos
, (long long) newsize
,
1096 (long long) i_size_read(inode
));
1098 /* No need for a higher level metadata lock if we're
1099 * never going past i_size. */
1100 if (newsize
<= i_size_read(inode
))
1103 if (meta_level
== 0) {
1104 ocfs2_meta_unlock(inode
, meta_level
);
1109 spin_lock(&OCFS2_I(inode
)->ip_lock
);
1110 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, newsize
) -
1111 OCFS2_I(inode
)->ip_clusters
;
1112 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
1114 mlog(0, "Writing at EOF, may need more allocation: "
1115 "i_size = %lld, newsize = %lld, need %u clusters\n",
1116 (long long) i_size_read(inode
), (long long) newsize
,
1119 /* We only want to continue the rest of this loop if
1120 * our extend will actually require more
1125 ret
= ocfs2_extend_file(inode
, NULL
, newsize
, count
);
1138 ocfs2_meta_unlock(inode
, meta_level
);
1144 static ssize_t
ocfs2_file_aio_write(struct kiocb
*iocb
,
1145 const struct iovec
*iov
,
1146 unsigned long nr_segs
,
1149 int ret
, rw_level
, have_alloc_sem
= 0;
1150 struct file
*filp
= iocb
->ki_filp
;
1151 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1152 int appending
= filp
->f_flags
& O_APPEND
? 1 : 0;
1154 mlog_entry("(0x%p, %u, '%.*s')\n", filp
,
1155 (unsigned int)nr_segs
,
1156 filp
->f_path
.dentry
->d_name
.len
,
1157 filp
->f_path
.dentry
->d_name
.name
);
1159 /* happy write of zero bytes */
1160 if (iocb
->ki_left
== 0)
1163 mutex_lock(&inode
->i_mutex
);
1164 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1165 if (filp
->f_flags
& O_DIRECT
) {
1167 down_read(&inode
->i_alloc_sem
);
1170 /* concurrent O_DIRECT writes are allowed */
1171 rw_level
= (filp
->f_flags
& O_DIRECT
) ? 0 : 1;
1172 ret
= ocfs2_rw_lock(inode
, rw_level
);
1179 ret
= ocfs2_prepare_inode_for_write(filp
->f_path
.dentry
, &iocb
->ki_pos
,
1180 iocb
->ki_left
, appending
);
1186 /* communicate with ocfs2_dio_end_io */
1187 ocfs2_iocb_set_rw_locked(iocb
);
1189 ret
= generic_file_aio_write_nolock(iocb
, iov
, nr_segs
, iocb
->ki_pos
);
1191 /* buffered aio wouldn't have proper lock coverage today */
1192 BUG_ON(ret
== -EIOCBQUEUED
&& !(filp
->f_flags
& O_DIRECT
));
1195 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1196 * function pointer which is called when o_direct io completes so that
1197 * it can unlock our rw lock. (it's the clustered equivalent of
1198 * i_alloc_sem; protects truncate from racing with pending ios).
1199 * Unfortunately there are error cases which call end_io and others
1200 * that don't. so we don't have to unlock the rw_lock if either an
1201 * async dio is going to do it in the future or an end_io after an
1202 * error has already done it.
1204 if (ret
== -EIOCBQUEUED
|| !ocfs2_iocb_is_rw_locked(iocb
)) {
1211 up_read(&inode
->i_alloc_sem
);
1213 ocfs2_rw_unlock(inode
, rw_level
);
1214 mutex_unlock(&inode
->i_mutex
);
1220 static ssize_t
ocfs2_file_splice_write(struct pipe_inode_info
*pipe
,
1227 struct inode
*inode
= out
->f_path
.dentry
->d_inode
;
1229 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out
, pipe
,
1231 out
->f_path
.dentry
->d_name
.len
,
1232 out
->f_path
.dentry
->d_name
.name
);
1234 inode_double_lock(inode
, pipe
->inode
);
1236 ret
= ocfs2_rw_lock(inode
, 1);
1242 ret
= ocfs2_prepare_inode_for_write(out
->f_path
.dentry
, ppos
, len
, 0);
1248 /* ok, we're done with i_size and alloc work */
1249 ret
= generic_file_splice_write_nolock(pipe
, out
, ppos
, len
, flags
);
1252 ocfs2_rw_unlock(inode
, 1);
1254 inode_double_unlock(inode
, pipe
->inode
);
1260 static ssize_t
ocfs2_file_splice_read(struct file
*in
,
1262 struct pipe_inode_info
*pipe
,
1267 struct inode
*inode
= in
->f_path
.dentry
->d_inode
;
1269 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in
, pipe
,
1271 in
->f_path
.dentry
->d_name
.len
,
1272 in
->f_path
.dentry
->d_name
.name
);
1275 * See the comment in ocfs2_file_aio_read()
1277 ret
= ocfs2_meta_lock(inode
, NULL
, 0);
1282 ocfs2_meta_unlock(inode
, 0);
1284 ret
= generic_file_splice_read(in
, ppos
, pipe
, len
, flags
);
1291 static ssize_t
ocfs2_file_aio_read(struct kiocb
*iocb
,
1292 const struct iovec
*iov
,
1293 unsigned long nr_segs
,
1296 int ret
= 0, rw_level
= -1, have_alloc_sem
= 0, lock_level
= 0;
1297 struct file
*filp
= iocb
->ki_filp
;
1298 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1300 mlog_entry("(0x%p, %u, '%.*s')\n", filp
,
1301 (unsigned int)nr_segs
,
1302 filp
->f_path
.dentry
->d_name
.len
,
1303 filp
->f_path
.dentry
->d_name
.name
);
1312 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1313 * need locks to protect pending reads from racing with truncate.
1315 if (filp
->f_flags
& O_DIRECT
) {
1316 down_read(&inode
->i_alloc_sem
);
1319 ret
= ocfs2_rw_lock(inode
, 0);
1325 /* communicate with ocfs2_dio_end_io */
1326 ocfs2_iocb_set_rw_locked(iocb
);
1330 * We're fine letting folks race truncates and extending
1331 * writes with read across the cluster, just like they can
1332 * locally. Hence no rw_lock during read.
1334 * Take and drop the meta data lock to update inode fields
1335 * like i_size. This allows the checks down below
1336 * generic_file_aio_read() a chance of actually working.
1338 ret
= ocfs2_meta_lock_atime(inode
, filp
->f_vfsmnt
, &lock_level
);
1343 ocfs2_meta_unlock(inode
, lock_level
);
1345 ret
= generic_file_aio_read(iocb
, iov
, nr_segs
, iocb
->ki_pos
);
1347 mlog(ML_ERROR
, "generic_file_aio_read returned -EINVAL\n");
1349 /* buffered aio wouldn't have proper lock coverage today */
1350 BUG_ON(ret
== -EIOCBQUEUED
&& !(filp
->f_flags
& O_DIRECT
));
1352 /* see ocfs2_file_aio_write */
1353 if (ret
== -EIOCBQUEUED
|| !ocfs2_iocb_is_rw_locked(iocb
)) {
1360 up_read(&inode
->i_alloc_sem
);
1362 ocfs2_rw_unlock(inode
, rw_level
);
1368 struct inode_operations ocfs2_file_iops
= {
1369 .setattr
= ocfs2_setattr
,
1370 .getattr
= ocfs2_getattr
,
1371 .permission
= ocfs2_permission
,
1374 struct inode_operations ocfs2_special_file_iops
= {
1375 .setattr
= ocfs2_setattr
,
1376 .getattr
= ocfs2_getattr
,
1377 .permission
= ocfs2_permission
,
1380 const struct file_operations ocfs2_fops
= {
1381 .read
= do_sync_read
,
1382 .write
= do_sync_write
,
1383 .sendfile
= generic_file_sendfile
,
1385 .fsync
= ocfs2_sync_file
,
1386 .release
= ocfs2_file_release
,
1387 .open
= ocfs2_file_open
,
1388 .aio_read
= ocfs2_file_aio_read
,
1389 .aio_write
= ocfs2_file_aio_write
,
1390 .ioctl
= ocfs2_ioctl
,
1391 .splice_read
= ocfs2_file_splice_read
,
1392 .splice_write
= ocfs2_file_splice_write
,
1395 const struct file_operations ocfs2_dops
= {
1396 .read
= generic_read_dir
,
1397 .readdir
= ocfs2_readdir
,
1398 .fsync
= ocfs2_sync_file
,
1399 .ioctl
= ocfs2_ioctl
,