allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / ocfs2 / file.c
blob96aaf7455431c4b030fe92c9b6ce106132740252
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * file.c
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>
27 #include <linux/fs.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/pagemap.h>
32 #include <linux/uio.h>
33 #include <linux/sched.h>
34 #include <linux/splice.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
38 #define MLOG_MASK_PREFIX ML_INODE
39 #include <cluster/masklog.h>
41 #include "ocfs2.h"
43 #include "alloc.h"
44 #include "aops.h"
45 #include "dir.h"
46 #include "dlmglue.h"
47 #include "extent_map.h"
48 #include "file.h"
49 #include "sysfile.h"
50 #include "inode.h"
51 #include "ioctl.h"
52 #include "journal.h"
53 #include "mmap.h"
54 #include "suballoc.h"
55 #include "super.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)
67 int status;
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);
82 status = -ENOENT;
83 goto leave;
86 if (mode & O_DIRECT)
87 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
89 oi->ip_open_count++;
90 spin_unlock(&oi->ip_lock);
91 status = 0;
92 leave:
93 mlog_exit(status);
94 return status;
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);
110 mlog_exit(0);
112 return 0;
115 static int ocfs2_sync_file(struct file *file,
116 struct dentry *dentry,
117 int datasync)
119 int err = 0;
120 journal_t *journal;
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);
128 if (err)
129 goto bail;
131 journal = osb->journal->j_journal;
132 err = journal_force_commit(journal);
134 bail:
135 mlog_exit(err);
137 return (err < 0) ? -EIO : 0;
140 int ocfs2_should_update_atime(struct inode *inode,
141 struct vfsmount *vfsmnt)
143 struct timespec now;
144 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
146 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
147 return 0;
149 if ((inode->i_flags & S_NOATIME) ||
150 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
151 return 0;
154 * We can be called with no vfsmnt structure - NFSD will
155 * sometimes do this.
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.
161 if (vfsmnt == NULL)
162 return 0;
164 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
165 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
166 return 0;
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))
171 return 1;
173 return 0;
176 now = CURRENT_TIME;
177 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
178 return 0;
179 else
180 return 1;
183 int ocfs2_update_inode_atime(struct inode *inode,
184 struct buffer_head *bh)
186 int ret;
187 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
188 handle_t *handle;
190 mlog_entry_void();
192 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
193 if (handle == NULL) {
194 ret = -ENOMEM;
195 mlog_errno(ret);
196 goto out;
199 inode->i_atime = CURRENT_TIME;
200 ret = ocfs2_mark_inode_dirty(handle, inode, bh);
201 if (ret < 0)
202 mlog_errno(ret);
204 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
205 out:
206 mlog_exit(ret);
207 return ret;
210 static int ocfs2_set_inode_size(handle_t *handle,
211 struct inode *inode,
212 struct buffer_head *fe_bh,
213 u64 new_i_size)
215 int status;
217 mlog_entry_void();
218 i_size_write(inode, new_i_size);
219 inode->i_blocks = ocfs2_inode_sector_count(inode);
220 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
222 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
223 if (status < 0) {
224 mlog_errno(status);
225 goto bail;
228 bail:
229 mlog_exit(status);
230 return status;
233 static int ocfs2_simple_size_update(struct inode *inode,
234 struct buffer_head *di_bh,
235 u64 new_i_size)
237 int ret;
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) {
243 ret = -ENOMEM;
244 mlog_errno(ret);
245 goto out;
248 ret = ocfs2_set_inode_size(handle, inode, di_bh,
249 new_i_size);
250 if (ret < 0)
251 mlog_errno(ret);
253 ocfs2_commit_trans(osb, handle);
254 out:
255 return ret;
258 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
259 struct inode *inode,
260 struct buffer_head *fe_bh,
261 u64 new_i_size)
263 int status;
264 handle_t *handle;
265 struct ocfs2_dinode *di;
267 mlog_entry_void();
269 /* TODO: This needs to actually orphan the inode in this
270 * transaction. */
272 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
273 if (IS_ERR(handle)) {
274 status = PTR_ERR(handle);
275 mlog_errno(status);
276 goto out;
279 status = ocfs2_journal_access(handle, inode, fe_bh,
280 OCFS2_JOURNAL_ACCESS_WRITE);
281 if (status < 0) {
282 mlog_errno(status);
283 goto out_commit;
287 * Do this before setting i_size.
289 status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
290 if (status) {
291 mlog_errno(status);
292 goto out_commit;
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);
305 if (status < 0)
306 mlog_errno(status);
308 out_commit:
309 ocfs2_commit_trans(osb, handle);
310 out:
312 mlog_exit(status);
313 return status;
316 static int ocfs2_truncate_file(struct inode *inode,
317 struct buffer_head *di_bh,
318 u64 new_i_size)
320 int status = 0;
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 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
330 truncate_inode_pages(inode->i_mapping, new_i_size);
332 fe = (struct ocfs2_dinode *) di_bh->b_data;
333 if (!OCFS2_IS_VALID_DINODE(fe)) {
334 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
335 status = -EIO;
336 goto bail;
339 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
340 "Inode %llu, inode i_size = %lld != di "
341 "i_size = %llu, i_flags = 0x%x\n",
342 (unsigned long long)OCFS2_I(inode)->ip_blkno,
343 i_size_read(inode),
344 (unsigned long long)le64_to_cpu(fe->i_size),
345 le32_to_cpu(fe->i_flags));
347 if (new_i_size > le64_to_cpu(fe->i_size)) {
348 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
349 (unsigned long long)le64_to_cpu(fe->i_size),
350 (unsigned long long)new_i_size);
351 status = -EINVAL;
352 mlog_errno(status);
353 goto bail;
356 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
357 (unsigned long long)le64_to_cpu(fe->i_blkno),
358 (unsigned long long)le64_to_cpu(fe->i_size),
359 (unsigned long long)new_i_size);
361 /* lets handle the simple truncate cases before doing any more
362 * cluster locking. */
363 if (new_i_size == le64_to_cpu(fe->i_size))
364 goto bail;
366 /* This forces other nodes to sync and drop their pages. Do
367 * this even if we have a truncate without allocation change -
368 * ocfs2 cluster sizes can be much greater than page size, so
369 * we have to truncate them anyway. */
370 status = ocfs2_data_lock(inode, 1);
371 if (status < 0) {
372 mlog_errno(status);
373 goto bail;
376 /* alright, we're going to need to do a full blown alloc size
377 * change. Orphan the inode so that recovery can complete the
378 * truncate if necessary. This does the task of marking
379 * i_size. */
380 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
381 if (status < 0) {
382 mlog_errno(status);
383 goto bail_unlock_data;
386 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
387 if (status < 0) {
388 mlog_errno(status);
389 goto bail_unlock_data;
392 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
393 if (status < 0) {
394 mlog_errno(status);
395 goto bail_unlock_data;
398 /* TODO: orphan dir cleanup here. */
399 bail_unlock_data:
400 ocfs2_data_unlock(inode, 1);
402 bail:
404 mlog_exit(status);
405 return status;
409 * extend allocation only here.
410 * we'll update all the disk stuff, and oip->alloc_size
412 * expect stuff to be locked, a transaction started and enough data /
413 * metadata reservations in the contexts.
415 * Will return -EAGAIN, and a reason if a restart is needed.
416 * If passed in, *reason will always be set, even in error.
418 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
419 struct inode *inode,
420 u32 *logical_offset,
421 u32 clusters_to_add,
422 struct buffer_head *fe_bh,
423 handle_t *handle,
424 struct ocfs2_alloc_context *data_ac,
425 struct ocfs2_alloc_context *meta_ac,
426 enum ocfs2_alloc_restarted *reason_ret)
428 int status = 0;
429 int free_extents;
430 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
431 enum ocfs2_alloc_restarted reason = RESTART_NONE;
432 u32 bit_off, num_bits;
433 u64 block;
435 BUG_ON(!clusters_to_add);
437 free_extents = ocfs2_num_free_extents(osb, inode, fe);
438 if (free_extents < 0) {
439 status = free_extents;
440 mlog_errno(status);
441 goto leave;
444 /* there are two cases which could cause us to EAGAIN in the
445 * we-need-more-metadata case:
446 * 1) we haven't reserved *any*
447 * 2) we are so fragmented, we've needed to add metadata too
448 * many times. */
449 if (!free_extents && !meta_ac) {
450 mlog(0, "we haven't reserved any metadata!\n");
451 status = -EAGAIN;
452 reason = RESTART_META;
453 goto leave;
454 } else if ((!free_extents)
455 && (ocfs2_alloc_context_bits_left(meta_ac)
456 < ocfs2_extend_meta_needed(fe))) {
457 mlog(0, "filesystem is really fragmented...\n");
458 status = -EAGAIN;
459 reason = RESTART_META;
460 goto leave;
463 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
464 &bit_off, &num_bits);
465 if (status < 0) {
466 if (status != -ENOSPC)
467 mlog_errno(status);
468 goto leave;
471 BUG_ON(num_bits > clusters_to_add);
473 /* reserve our write early -- insert_extent may update the inode */
474 status = ocfs2_journal_access(handle, inode, fe_bh,
475 OCFS2_JOURNAL_ACCESS_WRITE);
476 if (status < 0) {
477 mlog_errno(status);
478 goto leave;
481 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
482 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
483 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
484 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
485 *logical_offset, block, num_bits,
486 meta_ac);
487 if (status < 0) {
488 mlog_errno(status);
489 goto leave;
492 status = ocfs2_journal_dirty(handle, fe_bh);
493 if (status < 0) {
494 mlog_errno(status);
495 goto leave;
498 clusters_to_add -= num_bits;
499 *logical_offset += num_bits;
501 if (clusters_to_add) {
502 mlog(0, "need to alloc once more, clusters = %u, wanted = "
503 "%u\n", fe->i_clusters, clusters_to_add);
504 status = -EAGAIN;
505 reason = RESTART_TRANS;
508 leave:
509 mlog_exit(status);
510 if (reason_ret)
511 *reason_ret = reason;
512 return status;
516 * For a given allocation, determine which allocators will need to be
517 * accessed, and lock them, reserving the appropriate number of bits.
519 * Called from ocfs2_extend_allocation() for file systems which don't
520 * support holes, and from ocfs2_write() for file systems which
521 * understand sparse inodes.
523 int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
524 u32 clusters_to_add,
525 struct ocfs2_alloc_context **data_ac,
526 struct ocfs2_alloc_context **meta_ac)
528 int ret, num_free_extents;
529 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
531 *meta_ac = NULL;
532 *data_ac = NULL;
534 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
535 "clusters_to_add = %u\n",
536 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
537 le32_to_cpu(di->i_clusters), clusters_to_add);
539 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
540 if (num_free_extents < 0) {
541 ret = num_free_extents;
542 mlog_errno(ret);
543 goto out;
547 * Sparse allocation file systems need to be more conservative
548 * with reserving room for expansion - the actual allocation
549 * happens while we've got a journal handle open so re-taking
550 * a cluster lock (because we ran out of room for another
551 * extent) will violate ordering rules.
553 * Most of the time we'll only be seeing this 1 cluster at a time
554 * anyway.
556 if (!num_free_extents ||
557 (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
558 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
559 if (ret < 0) {
560 if (ret != -ENOSPC)
561 mlog_errno(ret);
562 goto out;
566 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
567 if (ret < 0) {
568 if (ret != -ENOSPC)
569 mlog_errno(ret);
570 goto out;
573 out:
574 if (ret) {
575 if (*meta_ac) {
576 ocfs2_free_alloc_context(*meta_ac);
577 *meta_ac = NULL;
581 * We cannot have an error and a non null *data_ac.
585 return ret;
588 static int ocfs2_extend_allocation(struct inode *inode,
589 u32 clusters_to_add)
591 int status = 0;
592 int restart_func = 0;
593 int drop_alloc_sem = 0;
594 int credits;
595 u32 prev_clusters, logical_start;
596 struct buffer_head *bh = NULL;
597 struct ocfs2_dinode *fe = NULL;
598 handle_t *handle = NULL;
599 struct ocfs2_alloc_context *data_ac = NULL;
600 struct ocfs2_alloc_context *meta_ac = NULL;
601 enum ocfs2_alloc_restarted why;
602 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
604 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
607 * This function only exists for file systems which don't
608 * support holes.
610 BUG_ON(ocfs2_sparse_alloc(osb));
612 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
613 OCFS2_BH_CACHED, inode);
614 if (status < 0) {
615 mlog_errno(status);
616 goto leave;
619 fe = (struct ocfs2_dinode *) bh->b_data;
620 if (!OCFS2_IS_VALID_DINODE(fe)) {
621 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
622 status = -EIO;
623 goto leave;
626 logical_start = OCFS2_I(inode)->ip_clusters;
628 restart_all:
629 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
631 /* blocks peope in read/write from reading our allocation
632 * until we're done changing it. We depend on i_mutex to block
633 * other extend/truncate calls while we're here. Ordering wrt
634 * start_trans is important here -- always do it before! */
635 down_write(&OCFS2_I(inode)->ip_alloc_sem);
636 drop_alloc_sem = 1;
638 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
639 &meta_ac);
640 if (status) {
641 mlog_errno(status);
642 goto leave;
645 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
646 handle = ocfs2_start_trans(osb, credits);
647 if (IS_ERR(handle)) {
648 status = PTR_ERR(handle);
649 handle = NULL;
650 mlog_errno(status);
651 goto leave;
654 restarted_transaction:
655 /* reserve a write to the file entry early on - that we if we
656 * run out of credits in the allocation path, we can still
657 * update i_size. */
658 status = ocfs2_journal_access(handle, inode, bh,
659 OCFS2_JOURNAL_ACCESS_WRITE);
660 if (status < 0) {
661 mlog_errno(status);
662 goto leave;
665 prev_clusters = OCFS2_I(inode)->ip_clusters;
667 status = ocfs2_do_extend_allocation(osb,
668 inode,
669 &logical_start,
670 clusters_to_add,
672 handle,
673 data_ac,
674 meta_ac,
675 &why);
676 if ((status < 0) && (status != -EAGAIN)) {
677 if (status != -ENOSPC)
678 mlog_errno(status);
679 goto leave;
682 status = ocfs2_journal_dirty(handle, bh);
683 if (status < 0) {
684 mlog_errno(status);
685 goto leave;
688 spin_lock(&OCFS2_I(inode)->ip_lock);
689 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
690 spin_unlock(&OCFS2_I(inode)->ip_lock);
692 if (why != RESTART_NONE && clusters_to_add) {
693 if (why == RESTART_META) {
694 mlog(0, "restarting function.\n");
695 restart_func = 1;
696 } else {
697 BUG_ON(why != RESTART_TRANS);
699 mlog(0, "restarting transaction.\n");
700 /* TODO: This can be more intelligent. */
701 credits = ocfs2_calc_extend_credits(osb->sb,
703 clusters_to_add);
704 status = ocfs2_extend_trans(handle, credits);
705 if (status < 0) {
706 /* handle still has to be committed at
707 * this point. */
708 status = -ENOMEM;
709 mlog_errno(status);
710 goto leave;
712 goto restarted_transaction;
716 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
717 le32_to_cpu(fe->i_clusters),
718 (unsigned long long)le64_to_cpu(fe->i_size));
719 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
720 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
722 leave:
723 if (drop_alloc_sem) {
724 up_write(&OCFS2_I(inode)->ip_alloc_sem);
725 drop_alloc_sem = 0;
727 if (handle) {
728 ocfs2_commit_trans(osb, handle);
729 handle = NULL;
731 if (data_ac) {
732 ocfs2_free_alloc_context(data_ac);
733 data_ac = NULL;
735 if (meta_ac) {
736 ocfs2_free_alloc_context(meta_ac);
737 meta_ac = NULL;
739 if ((!status) && restart_func) {
740 restart_func = 0;
741 goto restart_all;
743 if (bh) {
744 brelse(bh);
745 bh = NULL;
748 mlog_exit(status);
749 return status;
752 /* Some parts of this taken from generic_cont_expand, which turned out
753 * to be too fragile to do exactly what we need without us having to
754 * worry about recursive locking in ->prepare_write() and
755 * ->commit_write(). */
756 static int ocfs2_write_zero_page(struct inode *inode,
757 u64 size)
759 struct address_space *mapping = inode->i_mapping;
760 struct page *page;
761 unsigned long index;
762 unsigned int offset;
763 handle_t *handle = NULL;
764 int ret;
766 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
767 /* ugh. in prepare/commit_write, if from==to==start of block, we
768 ** skip the prepare. make sure we never send an offset for the start
769 ** of a block
771 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
772 offset++;
774 index = size >> PAGE_CACHE_SHIFT;
776 page = grab_cache_page(mapping, index);
777 if (!page) {
778 ret = -ENOMEM;
779 mlog_errno(ret);
780 goto out;
783 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
784 if (ret < 0) {
785 mlog_errno(ret);
786 goto out_unlock;
789 if (ocfs2_should_order_data(inode)) {
790 handle = ocfs2_start_walk_page_trans(inode, page, offset,
791 offset);
792 if (IS_ERR(handle)) {
793 ret = PTR_ERR(handle);
794 handle = NULL;
795 goto out_unlock;
799 /* must not update i_size! */
800 ret = block_commit_write(page, offset, offset);
801 if (ret < 0)
802 mlog_errno(ret);
803 else
804 ret = 0;
806 if (handle)
807 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
808 out_unlock:
809 unlock_page(page);
810 page_cache_release(page);
811 out:
812 return ret;
815 static int ocfs2_zero_extend(struct inode *inode,
816 u64 zero_to_size)
818 int ret = 0;
819 u64 start_off;
820 struct super_block *sb = inode->i_sb;
822 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
823 while (start_off < zero_to_size) {
824 ret = ocfs2_write_zero_page(inode, start_off);
825 if (ret < 0) {
826 mlog_errno(ret);
827 goto out;
830 start_off += sb->s_blocksize;
833 * Very large extends have the potential to lock up
834 * the cpu for extended periods of time.
836 cond_resched();
839 out:
840 return ret;
844 * A tail_to_skip value > 0 indicates that we're being called from
845 * ocfs2_file_aio_write(). This has the following implications:
847 * - we don't want to update i_size
848 * - di_bh will be NULL, which is fine because it's only used in the
849 * case where we want to update i_size.
850 * - ocfs2_zero_extend() will then only be filling the hole created
851 * between i_size and the start of the write.
853 static int ocfs2_extend_file(struct inode *inode,
854 struct buffer_head *di_bh,
855 u64 new_i_size,
856 size_t tail_to_skip)
858 int ret = 0;
859 u32 clusters_to_add = 0;
861 BUG_ON(!tail_to_skip && !di_bh);
863 /* setattr sometimes calls us like this. */
864 if (new_i_size == 0)
865 goto out;
867 if (i_size_read(inode) == new_i_size)
868 goto out;
869 BUG_ON(new_i_size < i_size_read(inode));
871 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
872 BUG_ON(tail_to_skip != 0);
873 goto out_update_size;
876 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
877 OCFS2_I(inode)->ip_clusters;
880 * protect the pages that ocfs2_zero_extend is going to be
881 * pulling into the page cache.. we do this before the
882 * metadata extend so that we don't get into the situation
883 * where we've extended the metadata but can't get the data
884 * lock to zero.
886 ret = ocfs2_data_lock(inode, 1);
887 if (ret < 0) {
888 mlog_errno(ret);
889 goto out;
892 if (clusters_to_add) {
893 ret = ocfs2_extend_allocation(inode, clusters_to_add);
894 if (ret < 0) {
895 mlog_errno(ret);
896 goto out_unlock;
901 * Call this even if we don't add any clusters to the tree. We
902 * still need to zero the area between the old i_size and the
903 * new i_size.
905 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
906 if (ret < 0) {
907 mlog_errno(ret);
908 goto out_unlock;
911 out_update_size:
912 if (!tail_to_skip) {
913 /* We're being called from ocfs2_setattr() which wants
914 * us to update i_size */
915 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
916 if (ret < 0)
917 mlog_errno(ret);
920 out_unlock:
921 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
922 ocfs2_data_unlock(inode, 1);
924 out:
925 return ret;
928 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
930 int status = 0, size_change;
931 struct inode *inode = dentry->d_inode;
932 struct super_block *sb = inode->i_sb;
933 struct ocfs2_super *osb = OCFS2_SB(sb);
934 struct buffer_head *bh = NULL;
935 handle_t *handle = NULL;
937 mlog_entry("(0x%p, '%.*s')\n", dentry,
938 dentry->d_name.len, dentry->d_name.name);
940 if (attr->ia_valid & ATTR_MODE)
941 mlog(0, "mode change: %d\n", attr->ia_mode);
942 if (attr->ia_valid & ATTR_UID)
943 mlog(0, "uid change: %d\n", attr->ia_uid);
944 if (attr->ia_valid & ATTR_GID)
945 mlog(0, "gid change: %d\n", attr->ia_gid);
946 if (attr->ia_valid & ATTR_SIZE)
947 mlog(0, "size change...\n");
948 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
949 mlog(0, "time change...\n");
951 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
952 | ATTR_GID | ATTR_UID | ATTR_MODE)
953 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
954 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
955 return 0;
958 status = inode_change_ok(inode, attr);
959 if (status)
960 return status;
962 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
963 if (size_change) {
964 status = ocfs2_rw_lock(inode, 1);
965 if (status < 0) {
966 mlog_errno(status);
967 goto bail;
971 status = ocfs2_meta_lock(inode, &bh, 1);
972 if (status < 0) {
973 if (status != -ENOENT)
974 mlog_errno(status);
975 goto bail_unlock_rw;
978 if (size_change && attr->ia_size != i_size_read(inode)) {
979 if (i_size_read(inode) > attr->ia_size)
980 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
981 else
982 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
983 if (status < 0) {
984 if (status != -ENOSPC)
985 mlog_errno(status);
986 status = -ENOSPC;
987 goto bail_unlock;
991 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
992 if (IS_ERR(handle)) {
993 status = PTR_ERR(handle);
994 mlog_errno(status);
995 goto bail_unlock;
998 status = inode_setattr(inode, attr);
999 if (status < 0) {
1000 mlog_errno(status);
1001 goto bail_commit;
1004 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1005 if (status < 0)
1006 mlog_errno(status);
1008 bail_commit:
1009 ocfs2_commit_trans(osb, handle);
1010 bail_unlock:
1011 ocfs2_meta_unlock(inode, 1);
1012 bail_unlock_rw:
1013 if (size_change)
1014 ocfs2_rw_unlock(inode, 1);
1015 bail:
1016 if (bh)
1017 brelse(bh);
1019 mlog_exit(status);
1020 return status;
1023 int ocfs2_getattr(struct vfsmount *mnt,
1024 struct dentry *dentry,
1025 struct kstat *stat)
1027 struct inode *inode = dentry->d_inode;
1028 struct super_block *sb = dentry->d_inode->i_sb;
1029 struct ocfs2_super *osb = sb->s_fs_info;
1030 int err;
1032 mlog_entry_void();
1034 err = ocfs2_inode_revalidate(dentry);
1035 if (err) {
1036 if (err != -ENOENT)
1037 mlog_errno(err);
1038 goto bail;
1041 generic_fillattr(inode, stat);
1043 /* We set the blksize from the cluster size for performance */
1044 stat->blksize = osb->s_clustersize;
1046 bail:
1047 mlog_exit(err);
1049 return err;
1052 int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1054 int ret;
1056 mlog_entry_void();
1058 ret = ocfs2_meta_lock(inode, NULL, 0);
1059 if (ret) {
1060 if (ret != -ENOENT)
1061 mlog_errno(ret);
1062 goto out;
1065 ret = generic_permission(inode, mask, NULL);
1067 ocfs2_meta_unlock(inode, 0);
1068 out:
1069 mlog_exit(ret);
1070 return ret;
1073 static int ocfs2_write_remove_suid(struct inode *inode)
1075 int ret;
1076 struct buffer_head *bh = NULL;
1077 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1078 handle_t *handle;
1079 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1080 struct ocfs2_dinode *di;
1082 mlog_entry("(Inode %llu, mode 0%o)\n",
1083 (unsigned long long)oi->ip_blkno, inode->i_mode);
1085 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1086 if (handle == NULL) {
1087 ret = -ENOMEM;
1088 mlog_errno(ret);
1089 goto out;
1092 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1093 if (ret < 0) {
1094 mlog_errno(ret);
1095 goto out_trans;
1098 ret = ocfs2_journal_access(handle, inode, bh,
1099 OCFS2_JOURNAL_ACCESS_WRITE);
1100 if (ret < 0) {
1101 mlog_errno(ret);
1102 goto out_bh;
1105 inode->i_mode &= ~S_ISUID;
1106 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1107 inode->i_mode &= ~S_ISGID;
1109 di = (struct ocfs2_dinode *) bh->b_data;
1110 di->i_mode = cpu_to_le16(inode->i_mode);
1112 ret = ocfs2_journal_dirty(handle, bh);
1113 if (ret < 0)
1114 mlog_errno(ret);
1115 out_bh:
1116 brelse(bh);
1117 out_trans:
1118 ocfs2_commit_trans(osb, handle);
1119 out:
1120 mlog_exit(ret);
1121 return ret;
1125 * Will look for holes and unwritten extents in the range starting at
1126 * pos for count bytes (inclusive).
1128 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1129 size_t count)
1131 int ret = 0;
1132 unsigned int extent_flags;
1133 u32 cpos, clusters, extent_len, phys_cpos;
1134 struct super_block *sb = inode->i_sb;
1136 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1137 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1139 while (clusters) {
1140 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1141 &extent_flags);
1142 if (ret < 0) {
1143 mlog_errno(ret);
1144 goto out;
1147 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
1148 ret = 1;
1149 break;
1152 if (extent_len > clusters)
1153 extent_len = clusters;
1155 clusters -= extent_len;
1156 cpos += extent_len;
1158 out:
1159 return ret;
1162 static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1163 loff_t *ppos,
1164 size_t count,
1165 int appending,
1166 int *direct_io)
1168 int ret = 0, meta_level = appending;
1169 struct inode *inode = dentry->d_inode;
1170 u32 clusters;
1171 loff_t newsize, saved_pos;
1174 * We sample i_size under a read level meta lock to see if our write
1175 * is extending the file, if it is we back off and get a write level
1176 * meta lock.
1178 for(;;) {
1179 ret = ocfs2_meta_lock(inode, NULL, meta_level);
1180 if (ret < 0) {
1181 meta_level = -1;
1182 mlog_errno(ret);
1183 goto out;
1186 /* Clear suid / sgid if necessary. We do this here
1187 * instead of later in the write path because
1188 * remove_suid() calls ->setattr without any hint that
1189 * we may have already done our cluster locking. Since
1190 * ocfs2_setattr() *must* take cluster locks to
1191 * proceeed, this will lead us to recursively lock the
1192 * inode. There's also the dinode i_size state which
1193 * can be lost via setattr during extending writes (we
1194 * set inode->i_size at the end of a write. */
1195 if (should_remove_suid(dentry)) {
1196 if (meta_level == 0) {
1197 ocfs2_meta_unlock(inode, meta_level);
1198 meta_level = 1;
1199 continue;
1202 ret = ocfs2_write_remove_suid(inode);
1203 if (ret < 0) {
1204 mlog_errno(ret);
1205 goto out_unlock;
1209 /* work on a copy of ppos until we're sure that we won't have
1210 * to recalculate it due to relocking. */
1211 if (appending) {
1212 saved_pos = i_size_read(inode);
1213 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1214 } else {
1215 saved_pos = *ppos;
1218 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1219 loff_t end = saved_pos + count;
1222 * Skip the O_DIRECT checks if we don't need
1223 * them.
1225 if (!direct_io || !(*direct_io))
1226 break;
1229 * Allowing concurrent direct writes means
1230 * i_size changes wouldn't be synchronized, so
1231 * one node could wind up truncating another
1232 * nodes writes.
1234 if (end > i_size_read(inode)) {
1235 *direct_io = 0;
1236 break;
1240 * We don't fill holes during direct io, so
1241 * check for them here. If any are found, the
1242 * caller will have to retake some cluster
1243 * locks and initiate the io as buffered.
1245 ret = ocfs2_check_range_for_holes(inode, saved_pos,
1246 count);
1247 if (ret == 1) {
1248 *direct_io = 0;
1249 ret = 0;
1250 } else if (ret < 0)
1251 mlog_errno(ret);
1252 break;
1256 * The rest of this loop is concerned with legacy file
1257 * systems which don't support sparse files.
1260 newsize = count + saved_pos;
1262 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1263 (long long) saved_pos, (long long) newsize,
1264 (long long) i_size_read(inode));
1266 /* No need for a higher level metadata lock if we're
1267 * never going past i_size. */
1268 if (newsize <= i_size_read(inode))
1269 break;
1271 if (meta_level == 0) {
1272 ocfs2_meta_unlock(inode, meta_level);
1273 meta_level = 1;
1274 continue;
1277 spin_lock(&OCFS2_I(inode)->ip_lock);
1278 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1279 OCFS2_I(inode)->ip_clusters;
1280 spin_unlock(&OCFS2_I(inode)->ip_lock);
1282 mlog(0, "Writing at EOF, may need more allocation: "
1283 "i_size = %lld, newsize = %lld, need %u clusters\n",
1284 (long long) i_size_read(inode), (long long) newsize,
1285 clusters);
1287 /* We only want to continue the rest of this loop if
1288 * our extend will actually require more
1289 * allocation. */
1290 if (!clusters)
1291 break;
1293 ret = ocfs2_extend_file(inode, NULL, newsize, count);
1294 if (ret < 0) {
1295 if (ret != -ENOSPC)
1296 mlog_errno(ret);
1297 goto out_unlock;
1299 break;
1302 if (appending)
1303 *ppos = saved_pos;
1305 out_unlock:
1306 ocfs2_meta_unlock(inode, meta_level);
1308 out:
1309 return ret;
1312 static inline void
1313 ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1315 const struct iovec *iov = *iovp;
1316 size_t base = *basep;
1318 do {
1319 int copy = min(bytes, iov->iov_len - base);
1321 bytes -= copy;
1322 base += copy;
1323 if (iov->iov_len == base) {
1324 iov++;
1325 base = 0;
1327 } while (bytes);
1328 *iovp = iov;
1329 *basep = base;
1332 static struct page * ocfs2_get_write_source(struct ocfs2_buffered_write_priv *bp,
1333 const struct iovec *cur_iov,
1334 size_t iov_offset)
1336 int ret;
1337 char *buf;
1338 struct page *src_page = NULL;
1340 buf = cur_iov->iov_base + iov_offset;
1342 if (!segment_eq(get_fs(), KERNEL_DS)) {
1344 * Pull in the user page. We want to do this outside
1345 * of the meta data locks in order to preserve locking
1346 * order in case of page fault.
1348 ret = get_user_pages(current, current->mm,
1349 (unsigned long)buf & PAGE_CACHE_MASK, 1,
1350 0, 0, &src_page, NULL);
1351 if (ret == 1)
1352 bp->b_src_buf = kmap(src_page);
1353 else
1354 src_page = ERR_PTR(-EFAULT);
1355 } else {
1356 bp->b_src_buf = (char *)((unsigned long)buf & PAGE_CACHE_MASK);
1359 return src_page;
1362 static void ocfs2_put_write_source(struct ocfs2_buffered_write_priv *bp,
1363 struct page *page)
1365 if (page) {
1366 kunmap(page);
1367 page_cache_release(page);
1371 static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1372 const struct iovec *iov,
1373 unsigned long nr_segs,
1374 size_t count,
1375 ssize_t o_direct_written)
1377 int ret = 0;
1378 ssize_t copied, total = 0;
1379 size_t iov_offset = 0;
1380 const struct iovec *cur_iov = iov;
1381 struct ocfs2_buffered_write_priv bp;
1382 struct page *page;
1385 * handle partial DIO write. Adjust cur_iov if needed.
1387 ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1389 do {
1390 bp.b_cur_off = iov_offset;
1391 bp.b_cur_iov = cur_iov;
1393 page = ocfs2_get_write_source(&bp, cur_iov, iov_offset);
1394 if (IS_ERR(page)) {
1395 ret = PTR_ERR(page);
1396 goto out;
1399 copied = ocfs2_buffered_write_cluster(file, *ppos, count,
1400 ocfs2_map_and_write_user_data,
1401 &bp);
1403 ocfs2_put_write_source(&bp, page);
1405 if (copied < 0) {
1406 mlog_errno(copied);
1407 ret = copied;
1408 goto out;
1411 total += copied;
1412 *ppos = *ppos + copied;
1413 count -= copied;
1415 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1416 } while(count);
1418 out:
1419 return total ? total : ret;
1422 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1423 const struct iovec *iov,
1424 unsigned long nr_segs,
1425 loff_t pos)
1427 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
1428 int can_do_direct, sync = 0;
1429 ssize_t written = 0;
1430 size_t ocount; /* original count */
1431 size_t count; /* after file limit checks */
1432 loff_t *ppos = &iocb->ki_pos;
1433 struct file *file = iocb->ki_filp;
1434 struct inode *inode = file->f_path.dentry->d_inode;
1436 mlog_entry("(0x%p, %u, '%.*s')\n", file,
1437 (unsigned int)nr_segs,
1438 file->f_path.dentry->d_name.len,
1439 file->f_path.dentry->d_name.name);
1441 if (iocb->ki_left == 0)
1442 return 0;
1444 ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1445 if (ret)
1446 return ret;
1448 count = ocount;
1450 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1452 appending = file->f_flags & O_APPEND ? 1 : 0;
1453 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1455 mutex_lock(&inode->i_mutex);
1457 relock:
1458 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1459 if (direct_io) {
1460 down_read(&inode->i_alloc_sem);
1461 have_alloc_sem = 1;
1464 /* concurrent O_DIRECT writes are allowed */
1465 rw_level = !direct_io;
1466 ret = ocfs2_rw_lock(inode, rw_level);
1467 if (ret < 0) {
1468 mlog_errno(ret);
1469 goto out_sems;
1472 can_do_direct = direct_io;
1473 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1474 iocb->ki_left, appending,
1475 &can_do_direct);
1476 if (ret < 0) {
1477 mlog_errno(ret);
1478 goto out;
1482 * We can't complete the direct I/O as requested, fall back to
1483 * buffered I/O.
1485 if (direct_io && !can_do_direct) {
1486 ocfs2_rw_unlock(inode, rw_level);
1487 up_read(&inode->i_alloc_sem);
1489 have_alloc_sem = 0;
1490 rw_level = -1;
1492 direct_io = 0;
1493 sync = 1;
1494 goto relock;
1497 if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
1498 sync = 1;
1501 * XXX: Is it ok to execute these checks a second time?
1503 ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
1504 if (ret)
1505 goto out;
1508 * Set pos so that sync_page_range_nolock() below understands
1509 * where to start from. We might've moved it around via the
1510 * calls above. The range we want to actually sync starts from
1511 * *ppos here.
1514 pos = *ppos;
1516 /* communicate with ocfs2_dio_end_io */
1517 ocfs2_iocb_set_rw_locked(iocb, rw_level);
1519 if (direct_io) {
1520 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1521 ppos, count, ocount);
1522 if (written < 0) {
1523 ret = written;
1524 goto out_dio;
1526 } else {
1527 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
1528 count, written);
1529 if (written < 0) {
1530 ret = written;
1531 if (ret != -EFAULT || ret != -ENOSPC)
1532 mlog_errno(ret);
1533 goto out;
1537 out_dio:
1538 /* buffered aio wouldn't have proper lock coverage today */
1539 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
1542 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1543 * function pointer which is called when o_direct io completes so that
1544 * it can unlock our rw lock. (it's the clustered equivalent of
1545 * i_alloc_sem; protects truncate from racing with pending ios).
1546 * Unfortunately there are error cases which call end_io and others
1547 * that don't. so we don't have to unlock the rw_lock if either an
1548 * async dio is going to do it in the future or an end_io after an
1549 * error has already done it.
1551 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1552 rw_level = -1;
1553 have_alloc_sem = 0;
1556 out:
1557 if (rw_level != -1)
1558 ocfs2_rw_unlock(inode, rw_level);
1560 out_sems:
1561 if (have_alloc_sem)
1562 up_read(&inode->i_alloc_sem);
1564 if (written > 0 && sync) {
1565 ssize_t err;
1567 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
1568 if (err < 0)
1569 written = err;
1572 mutex_unlock(&inode->i_mutex);
1574 mlog_exit(ret);
1575 return written ? written : ret;
1578 static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
1579 struct pipe_buffer *buf,
1580 struct splice_desc *sd)
1582 int ret, count, total = 0;
1583 ssize_t copied = 0;
1584 struct ocfs2_splice_write_priv sp;
1586 ret = buf->ops->confirm(pipe, buf);
1587 if (ret)
1588 goto out;
1590 sp.s_sd = sd;
1591 sp.s_buf = buf;
1592 sp.s_pipe = pipe;
1593 sp.s_offset = sd->pos & ~PAGE_CACHE_MASK;
1594 sp.s_buf_offset = buf->offset;
1596 count = sd->len;
1597 if (count + sp.s_offset > PAGE_CACHE_SIZE)
1598 count = PAGE_CACHE_SIZE - sp.s_offset;
1600 do {
1602 * splice wants us to copy up to one page at a
1603 * time. For pagesize > cluster size, this means we
1604 * might enter ocfs2_buffered_write_cluster() more
1605 * than once, so keep track of our progress here.
1607 copied = ocfs2_buffered_write_cluster(sd->u.file,
1608 (loff_t)sd->pos + total,
1609 count,
1610 ocfs2_map_and_write_splice_data,
1611 &sp);
1612 if (copied < 0) {
1613 mlog_errno(copied);
1614 ret = copied;
1615 goto out;
1618 count -= copied;
1619 sp.s_offset += copied;
1620 sp.s_buf_offset += copied;
1621 total += copied;
1622 } while (count);
1624 ret = 0;
1625 out:
1627 return total ? total : ret;
1630 static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1631 struct file *out,
1632 loff_t *ppos,
1633 size_t len,
1634 unsigned int flags)
1636 int ret, err;
1637 struct address_space *mapping = out->f_mapping;
1638 struct inode *inode = mapping->host;
1639 struct splice_desc sd = {
1640 .total_len = len,
1641 .flags = flags,
1642 .pos = *ppos,
1643 .u.file = out,
1646 ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
1647 if (ret > 0) {
1648 *ppos += ret;
1650 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
1651 err = generic_osync_inode(inode, mapping,
1652 OSYNC_METADATA|OSYNC_DATA);
1653 if (err)
1654 ret = err;
1658 return ret;
1661 static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1662 struct file *out,
1663 loff_t *ppos,
1664 size_t len,
1665 unsigned int flags)
1667 int ret;
1668 struct inode *inode = out->f_path.dentry->d_inode;
1670 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1671 (unsigned int)len,
1672 out->f_path.dentry->d_name.len,
1673 out->f_path.dentry->d_name.name);
1675 inode_double_lock(inode, pipe->inode);
1677 ret = ocfs2_rw_lock(inode, 1);
1678 if (ret < 0) {
1679 mlog_errno(ret);
1680 goto out;
1683 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
1684 NULL);
1685 if (ret < 0) {
1686 mlog_errno(ret);
1687 goto out_unlock;
1690 /* ok, we're done with i_size and alloc work */
1691 ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
1693 out_unlock:
1694 ocfs2_rw_unlock(inode, 1);
1695 out:
1696 inode_double_unlock(inode, pipe->inode);
1698 mlog_exit(ret);
1699 return ret;
1702 static ssize_t ocfs2_file_splice_read(struct file *in,
1703 loff_t *ppos,
1704 struct pipe_inode_info *pipe,
1705 size_t len,
1706 unsigned int flags)
1708 int ret = 0;
1709 struct inode *inode = in->f_path.dentry->d_inode;
1711 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1712 (unsigned int)len,
1713 in->f_path.dentry->d_name.len,
1714 in->f_path.dentry->d_name.name);
1717 * See the comment in ocfs2_file_aio_read()
1719 ret = ocfs2_meta_lock(inode, NULL, 0);
1720 if (ret < 0) {
1721 mlog_errno(ret);
1722 goto bail;
1724 ocfs2_meta_unlock(inode, 0);
1726 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1728 bail:
1729 mlog_exit(ret);
1730 return ret;
1733 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
1734 const struct iovec *iov,
1735 unsigned long nr_segs,
1736 loff_t pos)
1738 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
1739 struct file *filp = iocb->ki_filp;
1740 struct inode *inode = filp->f_path.dentry->d_inode;
1742 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1743 (unsigned int)nr_segs,
1744 filp->f_path.dentry->d_name.len,
1745 filp->f_path.dentry->d_name.name);
1747 if (!inode) {
1748 ret = -EINVAL;
1749 mlog_errno(ret);
1750 goto bail;
1754 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1755 * need locks to protect pending reads from racing with truncate.
1757 if (filp->f_flags & O_DIRECT) {
1758 down_read(&inode->i_alloc_sem);
1759 have_alloc_sem = 1;
1761 ret = ocfs2_rw_lock(inode, 0);
1762 if (ret < 0) {
1763 mlog_errno(ret);
1764 goto bail;
1766 rw_level = 0;
1767 /* communicate with ocfs2_dio_end_io */
1768 ocfs2_iocb_set_rw_locked(iocb, rw_level);
1772 * We're fine letting folks race truncates and extending
1773 * writes with read across the cluster, just like they can
1774 * locally. Hence no rw_lock during read.
1776 * Take and drop the meta data lock to update inode fields
1777 * like i_size. This allows the checks down below
1778 * generic_file_aio_read() a chance of actually working.
1780 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1781 if (ret < 0) {
1782 mlog_errno(ret);
1783 goto bail;
1785 ocfs2_meta_unlock(inode, lock_level);
1787 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
1788 if (ret == -EINVAL)
1789 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1791 /* buffered aio wouldn't have proper lock coverage today */
1792 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1794 /* see ocfs2_file_aio_write */
1795 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1796 rw_level = -1;
1797 have_alloc_sem = 0;
1800 bail:
1801 if (have_alloc_sem)
1802 up_read(&inode->i_alloc_sem);
1803 if (rw_level != -1)
1804 ocfs2_rw_unlock(inode, rw_level);
1805 mlog_exit(ret);
1807 return ret;
1810 const struct inode_operations ocfs2_file_iops = {
1811 .setattr = ocfs2_setattr,
1812 .getattr = ocfs2_getattr,
1813 .permission = ocfs2_permission,
1816 const struct inode_operations ocfs2_special_file_iops = {
1817 .setattr = ocfs2_setattr,
1818 .getattr = ocfs2_getattr,
1819 .permission = ocfs2_permission,
1822 const struct file_operations ocfs2_fops = {
1823 .read = do_sync_read,
1824 .write = do_sync_write,
1825 .mmap = ocfs2_mmap,
1826 .fsync = ocfs2_sync_file,
1827 .release = ocfs2_file_release,
1828 .open = ocfs2_file_open,
1829 .aio_read = ocfs2_file_aio_read,
1830 .aio_write = ocfs2_file_aio_write,
1831 .ioctl = ocfs2_ioctl,
1832 #ifdef CONFIG_COMPAT
1833 .compat_ioctl = ocfs2_compat_ioctl,
1834 #endif
1835 .splice_read = ocfs2_file_splice_read,
1836 .splice_write = ocfs2_file_splice_write,
1839 const struct file_operations ocfs2_dops = {
1840 .read = generic_read_dir,
1841 .readdir = ocfs2_readdir,
1842 .fsync = ocfs2_sync_file,
1843 .ioctl = ocfs2_ioctl,
1844 #ifdef CONFIG_COMPAT
1845 .compat_ioctl = ocfs2_compat_ioctl,
1846 #endif