ocfs2: pass ocfs2_super * into ocfs2_commit_trans()
[linux-2.6/mini2440.git] / fs / ocfs2 / file.c
blob9eb60f21968dbf2b357ba882315fa12a3c3bbbc2
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>
35 #define MLOG_MASK_PREFIX ML_INODE
36 #include <cluster/masklog.h>
38 #include "ocfs2.h"
40 #include "alloc.h"
41 #include "aops.h"
42 #include "dir.h"
43 #include "dlmglue.h"
44 #include "extent_map.h"
45 #include "file.h"
46 #include "sysfile.h"
47 #include "inode.h"
48 #include "ioctl.h"
49 #include "journal.h"
50 #include "mmap.h"
51 #include "suballoc.h"
52 #include "super.h"
54 #include "buffer_head_io.h"
56 static int ocfs2_sync_inode(struct inode *inode)
58 filemap_fdatawrite(inode->i_mapping);
59 return sync_mapping_buffers(inode->i_mapping);
62 static int ocfs2_file_open(struct inode *inode, struct file *file)
64 int status;
65 int mode = file->f_flags;
66 struct ocfs2_inode_info *oi = OCFS2_I(inode);
68 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
69 file->f_dentry->d_name.len, file->f_dentry->d_name.name);
71 spin_lock(&oi->ip_lock);
73 /* Check that the inode hasn't been wiped from disk by another
74 * node. If it hasn't then we're safe as long as we hold the
75 * spin lock until our increment of open count. */
76 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
77 spin_unlock(&oi->ip_lock);
79 status = -ENOENT;
80 goto leave;
83 if (mode & O_DIRECT)
84 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
86 oi->ip_open_count++;
87 spin_unlock(&oi->ip_lock);
88 status = 0;
89 leave:
90 mlog_exit(status);
91 return status;
94 static int ocfs2_file_release(struct inode *inode, struct file *file)
96 struct ocfs2_inode_info *oi = OCFS2_I(inode);
98 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
99 file->f_dentry->d_name.len,
100 file->f_dentry->d_name.name);
102 spin_lock(&oi->ip_lock);
103 if (!--oi->ip_open_count)
104 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
105 spin_unlock(&oi->ip_lock);
107 mlog_exit(0);
109 return 0;
112 static int ocfs2_sync_file(struct file *file,
113 struct dentry *dentry,
114 int datasync)
116 int err = 0;
117 journal_t *journal;
118 struct inode *inode = dentry->d_inode;
119 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
121 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
122 dentry->d_name.len, dentry->d_name.name);
124 err = ocfs2_sync_inode(dentry->d_inode);
125 if (err)
126 goto bail;
128 journal = osb->journal->j_journal;
129 err = journal_force_commit(journal);
131 bail:
132 mlog_exit(err);
134 return (err < 0) ? -EIO : 0;
137 int ocfs2_set_inode_size(struct ocfs2_journal_handle *handle,
138 struct inode *inode,
139 struct buffer_head *fe_bh,
140 u64 new_i_size)
142 int status;
144 mlog_entry_void();
145 i_size_write(inode, new_i_size);
146 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
147 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
149 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
150 if (status < 0) {
151 mlog_errno(status);
152 goto bail;
155 bail:
156 mlog_exit(status);
157 return status;
160 static int ocfs2_simple_size_update(struct inode *inode,
161 struct buffer_head *di_bh,
162 u64 new_i_size)
164 int ret;
165 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
166 struct ocfs2_journal_handle *handle = NULL;
168 handle = ocfs2_start_trans(osb, NULL,
169 OCFS2_INODE_UPDATE_CREDITS);
170 if (handle == NULL) {
171 ret = -ENOMEM;
172 mlog_errno(ret);
173 goto out;
176 ret = ocfs2_set_inode_size(handle, inode, di_bh,
177 new_i_size);
178 if (ret < 0)
179 mlog_errno(ret);
181 ocfs2_commit_trans(osb, handle);
182 out:
183 return ret;
186 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
187 struct inode *inode,
188 struct buffer_head *fe_bh,
189 u64 new_i_size)
191 int status;
192 struct ocfs2_journal_handle *handle;
194 mlog_entry_void();
196 /* TODO: This needs to actually orphan the inode in this
197 * transaction. */
199 handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
200 if (IS_ERR(handle)) {
201 status = PTR_ERR(handle);
202 mlog_errno(status);
203 goto out;
206 status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
207 if (status < 0)
208 mlog_errno(status);
210 ocfs2_commit_trans(osb, handle);
211 out:
212 mlog_exit(status);
213 return status;
216 static int ocfs2_truncate_file(struct inode *inode,
217 struct buffer_head *di_bh,
218 u64 new_i_size)
220 int status = 0;
221 struct ocfs2_dinode *fe = NULL;
222 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
223 struct ocfs2_truncate_context *tc = NULL;
225 mlog_entry("(inode = %llu, new_i_size = %llu\n",
226 (unsigned long long)OCFS2_I(inode)->ip_blkno,
227 (unsigned long long)new_i_size);
229 truncate_inode_pages(inode->i_mapping, new_i_size);
231 fe = (struct ocfs2_dinode *) di_bh->b_data;
232 if (!OCFS2_IS_VALID_DINODE(fe)) {
233 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
234 status = -EIO;
235 goto bail;
238 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
239 "Inode %llu, inode i_size = %lld != di "
240 "i_size = %llu, i_flags = 0x%x\n",
241 (unsigned long long)OCFS2_I(inode)->ip_blkno,
242 i_size_read(inode),
243 (unsigned long long)le64_to_cpu(fe->i_size),
244 le32_to_cpu(fe->i_flags));
246 if (new_i_size > le64_to_cpu(fe->i_size)) {
247 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
248 (unsigned long long)le64_to_cpu(fe->i_size),
249 (unsigned long long)new_i_size);
250 status = -EINVAL;
251 mlog_errno(status);
252 goto bail;
255 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
256 (unsigned long long)le64_to_cpu(fe->i_blkno),
257 (unsigned long long)le64_to_cpu(fe->i_size),
258 (unsigned long long)new_i_size);
260 /* lets handle the simple truncate cases before doing any more
261 * cluster locking. */
262 if (new_i_size == le64_to_cpu(fe->i_size))
263 goto bail;
265 /* This forces other nodes to sync and drop their pages. Do
266 * this even if we have a truncate without allocation change -
267 * ocfs2 cluster sizes can be much greater than page size, so
268 * we have to truncate them anyway. */
269 status = ocfs2_data_lock(inode, 1);
270 if (status < 0) {
271 mlog_errno(status);
272 goto bail;
274 ocfs2_data_unlock(inode, 1);
276 if (le32_to_cpu(fe->i_clusters) ==
277 ocfs2_clusters_for_bytes(osb->sb, new_i_size)) {
278 mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
279 fe->i_clusters);
280 /* No allocation change is required, so lets fast path
281 * this truncate. */
282 status = ocfs2_simple_size_update(inode, di_bh, new_i_size);
283 if (status < 0)
284 mlog_errno(status);
285 goto bail;
288 /* alright, we're going to need to do a full blown alloc size
289 * change. Orphan the inode so that recovery can complete the
290 * truncate if necessary. This does the task of marking
291 * i_size. */
292 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
293 if (status < 0) {
294 mlog_errno(status);
295 goto bail;
298 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
299 if (status < 0) {
300 mlog_errno(status);
301 goto bail;
304 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
305 if (status < 0) {
306 mlog_errno(status);
307 goto bail;
310 /* TODO: orphan dir cleanup here. */
311 bail:
313 mlog_exit(status);
314 return status;
318 * extend allocation only here.
319 * we'll update all the disk stuff, and oip->alloc_size
321 * expect stuff to be locked, a transaction started and enough data /
322 * metadata reservations in the contexts.
324 * Will return -EAGAIN, and a reason if a restart is needed.
325 * If passed in, *reason will always be set, even in error.
327 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
328 struct inode *inode,
329 u32 clusters_to_add,
330 struct buffer_head *fe_bh,
331 struct ocfs2_journal_handle *handle,
332 struct ocfs2_alloc_context *data_ac,
333 struct ocfs2_alloc_context *meta_ac,
334 enum ocfs2_alloc_restarted *reason_ret)
336 int status = 0;
337 int free_extents;
338 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
339 enum ocfs2_alloc_restarted reason = RESTART_NONE;
340 u32 bit_off, num_bits;
341 u64 block;
343 BUG_ON(!clusters_to_add);
345 free_extents = ocfs2_num_free_extents(osb, inode, fe);
346 if (free_extents < 0) {
347 status = free_extents;
348 mlog_errno(status);
349 goto leave;
352 /* there are two cases which could cause us to EAGAIN in the
353 * we-need-more-metadata case:
354 * 1) we haven't reserved *any*
355 * 2) we are so fragmented, we've needed to add metadata too
356 * many times. */
357 if (!free_extents && !meta_ac) {
358 mlog(0, "we haven't reserved any metadata!\n");
359 status = -EAGAIN;
360 reason = RESTART_META;
361 goto leave;
362 } else if ((!free_extents)
363 && (ocfs2_alloc_context_bits_left(meta_ac)
364 < ocfs2_extend_meta_needed(fe))) {
365 mlog(0, "filesystem is really fragmented...\n");
366 status = -EAGAIN;
367 reason = RESTART_META;
368 goto leave;
371 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
372 &bit_off, &num_bits);
373 if (status < 0) {
374 if (status != -ENOSPC)
375 mlog_errno(status);
376 goto leave;
379 BUG_ON(num_bits > clusters_to_add);
381 /* reserve our write early -- insert_extent may update the inode */
382 status = ocfs2_journal_access(handle, inode, fe_bh,
383 OCFS2_JOURNAL_ACCESS_WRITE);
384 if (status < 0) {
385 mlog_errno(status);
386 goto leave;
389 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
390 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
391 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
392 status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block,
393 num_bits, meta_ac);
394 if (status < 0) {
395 mlog_errno(status);
396 goto leave;
399 le32_add_cpu(&fe->i_clusters, num_bits);
400 spin_lock(&OCFS2_I(inode)->ip_lock);
401 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
402 spin_unlock(&OCFS2_I(inode)->ip_lock);
404 status = ocfs2_journal_dirty(handle, fe_bh);
405 if (status < 0) {
406 mlog_errno(status);
407 goto leave;
410 clusters_to_add -= num_bits;
412 if (clusters_to_add) {
413 mlog(0, "need to alloc once more, clusters = %u, wanted = "
414 "%u\n", fe->i_clusters, clusters_to_add);
415 status = -EAGAIN;
416 reason = RESTART_TRANS;
419 leave:
420 mlog_exit(status);
421 if (reason_ret)
422 *reason_ret = reason;
423 return status;
426 static int ocfs2_extend_allocation(struct inode *inode,
427 u32 clusters_to_add)
429 int status = 0;
430 int restart_func = 0;
431 int drop_alloc_sem = 0;
432 int credits, num_free_extents;
433 u32 prev_clusters;
434 struct buffer_head *bh = NULL;
435 struct ocfs2_dinode *fe = NULL;
436 struct ocfs2_journal_handle *handle = NULL;
437 struct ocfs2_alloc_context *data_ac = NULL;
438 struct ocfs2_alloc_context *meta_ac = NULL;
439 enum ocfs2_alloc_restarted why;
440 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
442 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
444 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
445 OCFS2_BH_CACHED, inode);
446 if (status < 0) {
447 mlog_errno(status);
448 goto leave;
451 fe = (struct ocfs2_dinode *) bh->b_data;
452 if (!OCFS2_IS_VALID_DINODE(fe)) {
453 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
454 status = -EIO;
455 goto leave;
458 restart_all:
459 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
461 mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
462 "clusters_to_add = %u\n",
463 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
464 fe->i_clusters, clusters_to_add);
466 num_free_extents = ocfs2_num_free_extents(osb,
467 inode,
468 fe);
469 if (num_free_extents < 0) {
470 status = num_free_extents;
471 mlog_errno(status);
472 goto leave;
475 if (!num_free_extents) {
476 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
477 if (status < 0) {
478 if (status != -ENOSPC)
479 mlog_errno(status);
480 goto leave;
484 status = ocfs2_reserve_clusters(osb, clusters_to_add, &data_ac);
485 if (status < 0) {
486 if (status != -ENOSPC)
487 mlog_errno(status);
488 goto leave;
491 /* blocks peope in read/write from reading our allocation
492 * until we're done changing it. We depend on i_mutex to block
493 * other extend/truncate calls while we're here. Ordering wrt
494 * start_trans is important here -- always do it before! */
495 down_write(&OCFS2_I(inode)->ip_alloc_sem);
496 drop_alloc_sem = 1;
498 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
499 handle = ocfs2_start_trans(osb, NULL, credits);
500 if (IS_ERR(handle)) {
501 status = PTR_ERR(handle);
502 handle = NULL;
503 mlog_errno(status);
504 goto leave;
507 restarted_transaction:
508 /* reserve a write to the file entry early on - that we if we
509 * run out of credits in the allocation path, we can still
510 * update i_size. */
511 status = ocfs2_journal_access(handle, inode, bh,
512 OCFS2_JOURNAL_ACCESS_WRITE);
513 if (status < 0) {
514 mlog_errno(status);
515 goto leave;
518 prev_clusters = OCFS2_I(inode)->ip_clusters;
520 status = ocfs2_do_extend_allocation(osb,
521 inode,
522 clusters_to_add,
524 handle,
525 data_ac,
526 meta_ac,
527 &why);
528 if ((status < 0) && (status != -EAGAIN)) {
529 if (status != -ENOSPC)
530 mlog_errno(status);
531 goto leave;
534 status = ocfs2_journal_dirty(handle, bh);
535 if (status < 0) {
536 mlog_errno(status);
537 goto leave;
540 spin_lock(&OCFS2_I(inode)->ip_lock);
541 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
542 spin_unlock(&OCFS2_I(inode)->ip_lock);
544 if (why != RESTART_NONE && clusters_to_add) {
545 if (why == RESTART_META) {
546 mlog(0, "restarting function.\n");
547 restart_func = 1;
548 } else {
549 BUG_ON(why != RESTART_TRANS);
551 mlog(0, "restarting transaction.\n");
552 /* TODO: This can be more intelligent. */
553 credits = ocfs2_calc_extend_credits(osb->sb,
555 clusters_to_add);
556 status = ocfs2_extend_trans(handle->k_handle, credits);
557 if (status < 0) {
558 /* handle still has to be committed at
559 * this point. */
560 status = -ENOMEM;
561 mlog_errno(status);
562 goto leave;
564 goto restarted_transaction;
568 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
569 fe->i_clusters, (unsigned long long)fe->i_size);
570 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
571 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
573 leave:
574 if (drop_alloc_sem) {
575 up_write(&OCFS2_I(inode)->ip_alloc_sem);
576 drop_alloc_sem = 0;
578 if (handle) {
579 ocfs2_commit_trans(osb, handle);
580 handle = NULL;
582 if (data_ac) {
583 ocfs2_free_alloc_context(data_ac);
584 data_ac = NULL;
586 if (meta_ac) {
587 ocfs2_free_alloc_context(meta_ac);
588 meta_ac = NULL;
590 if ((!status) && restart_func) {
591 restart_func = 0;
592 goto restart_all;
594 if (bh) {
595 brelse(bh);
596 bh = NULL;
599 mlog_exit(status);
600 return status;
603 /* Some parts of this taken from generic_cont_expand, which turned out
604 * to be too fragile to do exactly what we need without us having to
605 * worry about recursive locking in ->prepare_write() and
606 * ->commit_write(). */
607 static int ocfs2_write_zero_page(struct inode *inode,
608 u64 size)
610 struct address_space *mapping = inode->i_mapping;
611 struct page *page;
612 unsigned long index;
613 unsigned int offset;
614 struct ocfs2_journal_handle *handle = NULL;
615 int ret;
617 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
618 /* ugh. in prepare/commit_write, if from==to==start of block, we
619 ** skip the prepare. make sure we never send an offset for the start
620 ** of a block
622 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
623 offset++;
625 index = size >> PAGE_CACHE_SHIFT;
627 page = grab_cache_page(mapping, index);
628 if (!page) {
629 ret = -ENOMEM;
630 mlog_errno(ret);
631 goto out;
634 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
635 if (ret < 0) {
636 mlog_errno(ret);
637 goto out_unlock;
640 if (ocfs2_should_order_data(inode)) {
641 handle = ocfs2_start_walk_page_trans(inode, page, offset,
642 offset);
643 if (IS_ERR(handle)) {
644 ret = PTR_ERR(handle);
645 handle = NULL;
646 goto out_unlock;
650 /* must not update i_size! */
651 ret = block_commit_write(page, offset, offset);
652 if (ret < 0)
653 mlog_errno(ret);
654 else
655 ret = 0;
657 if (handle)
658 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
659 out_unlock:
660 unlock_page(page);
661 page_cache_release(page);
662 out:
663 return ret;
666 static int ocfs2_zero_extend(struct inode *inode,
667 u64 zero_to_size)
669 int ret = 0;
670 u64 start_off;
671 struct super_block *sb = inode->i_sb;
673 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
674 while (start_off < zero_to_size) {
675 ret = ocfs2_write_zero_page(inode, start_off);
676 if (ret < 0) {
677 mlog_errno(ret);
678 goto out;
681 start_off += sb->s_blocksize;
684 * Very large extends have the potential to lock up
685 * the cpu for extended periods of time.
687 cond_resched();
690 out:
691 return ret;
695 * A tail_to_skip value > 0 indicates that we're being called from
696 * ocfs2_file_aio_write(). This has the following implications:
698 * - we don't want to update i_size
699 * - di_bh will be NULL, which is fine because it's only used in the
700 * case where we want to update i_size.
701 * - ocfs2_zero_extend() will then only be filling the hole created
702 * between i_size and the start of the write.
704 static int ocfs2_extend_file(struct inode *inode,
705 struct buffer_head *di_bh,
706 u64 new_i_size,
707 size_t tail_to_skip)
709 int ret = 0;
710 u32 clusters_to_add;
712 BUG_ON(!tail_to_skip && !di_bh);
714 /* setattr sometimes calls us like this. */
715 if (new_i_size == 0)
716 goto out;
718 if (i_size_read(inode) == new_i_size)
719 goto out;
720 BUG_ON(new_i_size < i_size_read(inode));
722 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
723 OCFS2_I(inode)->ip_clusters;
726 * protect the pages that ocfs2_zero_extend is going to be
727 * pulling into the page cache.. we do this before the
728 * metadata extend so that we don't get into the situation
729 * where we've extended the metadata but can't get the data
730 * lock to zero.
732 ret = ocfs2_data_lock(inode, 1);
733 if (ret < 0) {
734 mlog_errno(ret);
735 goto out;
738 if (clusters_to_add) {
739 ret = ocfs2_extend_allocation(inode, clusters_to_add);
740 if (ret < 0) {
741 mlog_errno(ret);
742 goto out_unlock;
747 * Call this even if we don't add any clusters to the tree. We
748 * still need to zero the area between the old i_size and the
749 * new i_size.
751 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
752 if (ret < 0) {
753 mlog_errno(ret);
754 goto out_unlock;
757 if (!tail_to_skip) {
758 /* We're being called from ocfs2_setattr() which wants
759 * us to update i_size */
760 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
761 if (ret < 0)
762 mlog_errno(ret);
765 out_unlock:
766 ocfs2_data_unlock(inode, 1);
768 out:
769 return ret;
772 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
774 int status = 0, size_change;
775 struct inode *inode = dentry->d_inode;
776 struct super_block *sb = inode->i_sb;
777 struct ocfs2_super *osb = OCFS2_SB(sb);
778 struct buffer_head *bh = NULL;
779 struct ocfs2_journal_handle *handle = NULL;
781 mlog_entry("(0x%p, '%.*s')\n", dentry,
782 dentry->d_name.len, dentry->d_name.name);
784 if (attr->ia_valid & ATTR_MODE)
785 mlog(0, "mode change: %d\n", attr->ia_mode);
786 if (attr->ia_valid & ATTR_UID)
787 mlog(0, "uid change: %d\n", attr->ia_uid);
788 if (attr->ia_valid & ATTR_GID)
789 mlog(0, "gid change: %d\n", attr->ia_gid);
790 if (attr->ia_valid & ATTR_SIZE)
791 mlog(0, "size change...\n");
792 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
793 mlog(0, "time change...\n");
795 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
796 | ATTR_GID | ATTR_UID | ATTR_MODE)
797 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
798 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
799 return 0;
802 status = inode_change_ok(inode, attr);
803 if (status)
804 return status;
806 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
807 if (size_change) {
808 status = ocfs2_rw_lock(inode, 1);
809 if (status < 0) {
810 mlog_errno(status);
811 goto bail;
815 status = ocfs2_meta_lock(inode, &bh, 1);
816 if (status < 0) {
817 if (status != -ENOENT)
818 mlog_errno(status);
819 goto bail_unlock_rw;
822 if (size_change && attr->ia_size != i_size_read(inode)) {
823 if (i_size_read(inode) > attr->ia_size)
824 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
825 else
826 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
827 if (status < 0) {
828 if (status != -ENOSPC)
829 mlog_errno(status);
830 status = -ENOSPC;
831 goto bail_unlock;
835 handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
836 if (IS_ERR(handle)) {
837 status = PTR_ERR(handle);
838 mlog_errno(status);
839 goto bail_unlock;
842 status = inode_setattr(inode, attr);
843 if (status < 0) {
844 mlog_errno(status);
845 goto bail_commit;
848 status = ocfs2_mark_inode_dirty(handle, inode, bh);
849 if (status < 0)
850 mlog_errno(status);
852 bail_commit:
853 ocfs2_commit_trans(osb, handle);
854 bail_unlock:
855 ocfs2_meta_unlock(inode, 1);
856 bail_unlock_rw:
857 if (size_change)
858 ocfs2_rw_unlock(inode, 1);
859 bail:
860 if (bh)
861 brelse(bh);
863 mlog_exit(status);
864 return status;
867 int ocfs2_getattr(struct vfsmount *mnt,
868 struct dentry *dentry,
869 struct kstat *stat)
871 struct inode *inode = dentry->d_inode;
872 struct super_block *sb = dentry->d_inode->i_sb;
873 struct ocfs2_super *osb = sb->s_fs_info;
874 int err;
876 mlog_entry_void();
878 err = ocfs2_inode_revalidate(dentry);
879 if (err) {
880 if (err != -ENOENT)
881 mlog_errno(err);
882 goto bail;
885 generic_fillattr(inode, stat);
887 /* We set the blksize from the cluster size for performance */
888 stat->blksize = osb->s_clustersize;
890 bail:
891 mlog_exit(err);
893 return err;
896 static int ocfs2_write_remove_suid(struct inode *inode)
898 int ret;
899 struct buffer_head *bh = NULL;
900 struct ocfs2_inode_info *oi = OCFS2_I(inode);
901 struct ocfs2_journal_handle *handle;
902 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
903 struct ocfs2_dinode *di;
905 mlog_entry("(Inode %llu, mode 0%o)\n",
906 (unsigned long long)oi->ip_blkno, inode->i_mode);
908 handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
909 if (handle == NULL) {
910 ret = -ENOMEM;
911 mlog_errno(ret);
912 goto out;
915 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
916 if (ret < 0) {
917 mlog_errno(ret);
918 goto out_trans;
921 ret = ocfs2_journal_access(handle, inode, bh,
922 OCFS2_JOURNAL_ACCESS_WRITE);
923 if (ret < 0) {
924 mlog_errno(ret);
925 goto out_bh;
928 inode->i_mode &= ~S_ISUID;
929 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
930 inode->i_mode &= ~S_ISGID;
932 di = (struct ocfs2_dinode *) bh->b_data;
933 di->i_mode = cpu_to_le16(inode->i_mode);
935 ret = ocfs2_journal_dirty(handle, bh);
936 if (ret < 0)
937 mlog_errno(ret);
938 out_bh:
939 brelse(bh);
940 out_trans:
941 ocfs2_commit_trans(osb, handle);
942 out:
943 mlog_exit(ret);
944 return ret;
947 static inline int ocfs2_write_should_remove_suid(struct inode *inode)
949 mode_t mode = inode->i_mode;
951 if (!capable(CAP_FSETID)) {
952 if (unlikely(mode & S_ISUID))
953 return 1;
955 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
956 return 1;
958 return 0;
961 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
962 const struct iovec *iov,
963 unsigned long nr_segs,
964 loff_t pos)
966 int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0;
967 u32 clusters;
968 struct file *filp = iocb->ki_filp;
969 struct inode *inode = filp->f_dentry->d_inode;
970 loff_t newsize, saved_pos;
972 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
973 (unsigned int)nr_segs,
974 filp->f_dentry->d_name.len,
975 filp->f_dentry->d_name.name);
977 /* happy write of zero bytes */
978 if (iocb->ki_left == 0)
979 return 0;
981 if (!inode) {
982 mlog(0, "bad inode\n");
983 return -EIO;
986 mutex_lock(&inode->i_mutex);
987 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
988 if (filp->f_flags & O_DIRECT) {
989 have_alloc_sem = 1;
990 down_read(&inode->i_alloc_sem);
993 /* concurrent O_DIRECT writes are allowed */
994 rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
995 ret = ocfs2_rw_lock(inode, rw_level);
996 if (ret < 0) {
997 rw_level = -1;
998 mlog_errno(ret);
999 goto out;
1003 * We sample i_size under a read level meta lock to see if our write
1004 * is extending the file, if it is we back off and get a write level
1005 * meta lock.
1007 meta_level = (filp->f_flags & O_APPEND) ? 1 : 0;
1008 for(;;) {
1009 ret = ocfs2_meta_lock(inode, NULL, meta_level);
1010 if (ret < 0) {
1011 meta_level = -1;
1012 mlog_errno(ret);
1013 goto out;
1016 /* Clear suid / sgid if necessary. We do this here
1017 * instead of later in the write path because
1018 * remove_suid() calls ->setattr without any hint that
1019 * we may have already done our cluster locking. Since
1020 * ocfs2_setattr() *must* take cluster locks to
1021 * proceeed, this will lead us to recursively lock the
1022 * inode. There's also the dinode i_size state which
1023 * can be lost via setattr during extending writes (we
1024 * set inode->i_size at the end of a write. */
1025 if (ocfs2_write_should_remove_suid(inode)) {
1026 if (meta_level == 0) {
1027 ocfs2_meta_unlock(inode, meta_level);
1028 meta_level = 1;
1029 continue;
1032 ret = ocfs2_write_remove_suid(inode);
1033 if (ret < 0) {
1034 mlog_errno(ret);
1035 goto out;
1039 /* work on a copy of ppos until we're sure that we won't have
1040 * to recalculate it due to relocking. */
1041 if (filp->f_flags & O_APPEND) {
1042 saved_pos = i_size_read(inode);
1043 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1044 } else {
1045 saved_pos = iocb->ki_pos;
1047 newsize = iocb->ki_left + saved_pos;
1049 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1050 (long long) saved_pos, (long long) newsize,
1051 (long long) i_size_read(inode));
1053 /* No need for a higher level metadata lock if we're
1054 * never going past i_size. */
1055 if (newsize <= i_size_read(inode))
1056 break;
1058 if (meta_level == 0) {
1059 ocfs2_meta_unlock(inode, meta_level);
1060 meta_level = 1;
1061 continue;
1064 spin_lock(&OCFS2_I(inode)->ip_lock);
1065 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1066 OCFS2_I(inode)->ip_clusters;
1067 spin_unlock(&OCFS2_I(inode)->ip_lock);
1069 mlog(0, "Writing at EOF, may need more allocation: "
1070 "i_size = %lld, newsize = %lld, need %u clusters\n",
1071 (long long) i_size_read(inode), (long long) newsize,
1072 clusters);
1074 /* We only want to continue the rest of this loop if
1075 * our extend will actually require more
1076 * allocation. */
1077 if (!clusters)
1078 break;
1080 ret = ocfs2_extend_file(inode, NULL, newsize, iocb->ki_left);
1081 if (ret < 0) {
1082 if (ret != -ENOSPC)
1083 mlog_errno(ret);
1084 goto out;
1086 break;
1089 /* ok, we're done with i_size and alloc work */
1090 iocb->ki_pos = saved_pos;
1091 ocfs2_meta_unlock(inode, meta_level);
1092 meta_level = -1;
1094 /* communicate with ocfs2_dio_end_io */
1095 ocfs2_iocb_set_rw_locked(iocb);
1097 ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
1099 /* buffered aio wouldn't have proper lock coverage today */
1100 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1103 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1104 * function pointer which is called when o_direct io completes so that
1105 * it can unlock our rw lock. (it's the clustered equivalent of
1106 * i_alloc_sem; protects truncate from racing with pending ios).
1107 * Unfortunately there are error cases which call end_io and others
1108 * that don't. so we don't have to unlock the rw_lock if either an
1109 * async dio is going to do it in the future or an end_io after an
1110 * error has already done it.
1112 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1113 rw_level = -1;
1114 have_alloc_sem = 0;
1117 out:
1118 if (meta_level != -1)
1119 ocfs2_meta_unlock(inode, meta_level);
1120 if (have_alloc_sem)
1121 up_read(&inode->i_alloc_sem);
1122 if (rw_level != -1)
1123 ocfs2_rw_unlock(inode, rw_level);
1124 mutex_unlock(&inode->i_mutex);
1126 mlog_exit(ret);
1127 return ret;
1130 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
1131 const struct iovec *iov,
1132 unsigned long nr_segs,
1133 loff_t pos)
1135 int ret = 0, rw_level = -1, have_alloc_sem = 0;
1136 struct file *filp = iocb->ki_filp;
1137 struct inode *inode = filp->f_dentry->d_inode;
1139 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1140 (unsigned int)nr_segs,
1141 filp->f_dentry->d_name.len,
1142 filp->f_dentry->d_name.name);
1144 if (!inode) {
1145 ret = -EINVAL;
1146 mlog_errno(ret);
1147 goto bail;
1151 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1152 * need locks to protect pending reads from racing with truncate.
1154 if (filp->f_flags & O_DIRECT) {
1155 down_read(&inode->i_alloc_sem);
1156 have_alloc_sem = 1;
1158 ret = ocfs2_rw_lock(inode, 0);
1159 if (ret < 0) {
1160 mlog_errno(ret);
1161 goto bail;
1163 rw_level = 0;
1164 /* communicate with ocfs2_dio_end_io */
1165 ocfs2_iocb_set_rw_locked(iocb);
1169 * We're fine letting folks race truncates and extending
1170 * writes with read across the cluster, just like they can
1171 * locally. Hence no rw_lock during read.
1173 * Take and drop the meta data lock to update inode fields
1174 * like i_size. This allows the checks down below
1175 * generic_file_aio_read() a chance of actually working.
1177 ret = ocfs2_meta_lock(inode, NULL, 0);
1178 if (ret < 0) {
1179 mlog_errno(ret);
1180 goto bail;
1182 ocfs2_meta_unlock(inode, 0);
1184 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
1185 if (ret == -EINVAL)
1186 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1188 /* buffered aio wouldn't have proper lock coverage today */
1189 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1191 /* see ocfs2_file_aio_write */
1192 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1193 rw_level = -1;
1194 have_alloc_sem = 0;
1197 bail:
1198 if (have_alloc_sem)
1199 up_read(&inode->i_alloc_sem);
1200 if (rw_level != -1)
1201 ocfs2_rw_unlock(inode, rw_level);
1202 mlog_exit(ret);
1204 return ret;
1207 struct inode_operations ocfs2_file_iops = {
1208 .setattr = ocfs2_setattr,
1209 .getattr = ocfs2_getattr,
1212 struct inode_operations ocfs2_special_file_iops = {
1213 .setattr = ocfs2_setattr,
1214 .getattr = ocfs2_getattr,
1217 const struct file_operations ocfs2_fops = {
1218 .read = do_sync_read,
1219 .write = do_sync_write,
1220 .sendfile = generic_file_sendfile,
1221 .mmap = ocfs2_mmap,
1222 .fsync = ocfs2_sync_file,
1223 .release = ocfs2_file_release,
1224 .open = ocfs2_file_open,
1225 .aio_read = ocfs2_file_aio_read,
1226 .aio_write = ocfs2_file_aio_write,
1227 .ioctl = ocfs2_ioctl,
1230 const struct file_operations ocfs2_dops = {
1231 .read = generic_read_dir,
1232 .readdir = ocfs2_readdir,
1233 .fsync = ocfs2_sync_file,
1234 .ioctl = ocfs2_ioctl,