]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ocfs2/file.c
ocfs2: Add splice support
[net-next-2.6.git] / fs / ocfs2 / file.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * file.c
5 *
6 * File open, close, extend, truncate
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
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.
14 *
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.
19 *
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.
24 */
25
16f7e0fe 26#include <linux/capability.h>
ccd979bd
MF
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>
e2057c5a 33#include <linux/sched.h>
8659ac25 34#include <linux/pipe_fs_i.h>
ccd979bd
MF
35
36#define MLOG_MASK_PREFIX ML_INODE
37#include <cluster/masklog.h>
38
39#include "ocfs2.h"
40
41#include "alloc.h"
42#include "aops.h"
43#include "dir.h"
44#include "dlmglue.h"
45#include "extent_map.h"
46#include "file.h"
47#include "sysfile.h"
48#include "inode.h"
ca4d147e 49#include "ioctl.h"
ccd979bd
MF
50#include "journal.h"
51#include "mmap.h"
52#include "suballoc.h"
53#include "super.h"
54
55#include "buffer_head_io.h"
56
57static int ocfs2_sync_inode(struct inode *inode)
58{
59 filemap_fdatawrite(inode->i_mapping);
60 return sync_mapping_buffers(inode->i_mapping);
61}
62
63static int ocfs2_file_open(struct inode *inode, struct file *file)
64{
65 int status;
66 int mode = file->f_flags;
67 struct ocfs2_inode_info *oi = OCFS2_I(inode);
68
69 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
70 file->f_dentry->d_name.len, file->f_dentry->d_name.name);
71
72 spin_lock(&oi->ip_lock);
73
74 /* Check that the inode hasn't been wiped from disk by another
75 * node. If it hasn't then we're safe as long as we hold the
76 * spin lock until our increment of open count. */
77 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
78 spin_unlock(&oi->ip_lock);
79
80 status = -ENOENT;
81 goto leave;
82 }
83
84 if (mode & O_DIRECT)
85 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
86
87 oi->ip_open_count++;
88 spin_unlock(&oi->ip_lock);
89 status = 0;
90leave:
91 mlog_exit(status);
92 return status;
93}
94
95static int ocfs2_file_release(struct inode *inode, struct file *file)
96{
97 struct ocfs2_inode_info *oi = OCFS2_I(inode);
98
99 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
100 file->f_dentry->d_name.len,
101 file->f_dentry->d_name.name);
102
103 spin_lock(&oi->ip_lock);
104 if (!--oi->ip_open_count)
105 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
106 spin_unlock(&oi->ip_lock);
107
108 mlog_exit(0);
109
110 return 0;
111}
112
113static int ocfs2_sync_file(struct file *file,
114 struct dentry *dentry,
115 int datasync)
116{
117 int err = 0;
118 journal_t *journal;
119 struct inode *inode = dentry->d_inode;
120 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
121
122 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
123 dentry->d_name.len, dentry->d_name.name);
124
125 err = ocfs2_sync_inode(dentry->d_inode);
126 if (err)
127 goto bail;
128
129 journal = osb->journal->j_journal;
130 err = journal_force_commit(journal);
131
132bail:
133 mlog_exit(err);
134
135 return (err < 0) ? -EIO : 0;
136}
137
1fabe148 138int ocfs2_set_inode_size(handle_t *handle,
ccd979bd
MF
139 struct inode *inode,
140 struct buffer_head *fe_bh,
141 u64 new_i_size)
142{
143 int status;
144
145 mlog_entry_void();
146 i_size_write(inode, new_i_size);
147 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
148 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
149
150 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
151 if (status < 0) {
152 mlog_errno(status);
153 goto bail;
154 }
155
156bail:
157 mlog_exit(status);
158 return status;
159}
160
161static int ocfs2_simple_size_update(struct inode *inode,
162 struct buffer_head *di_bh,
163 u64 new_i_size)
164{
165 int ret;
166 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1fabe148 167 handle_t *handle = NULL;
ccd979bd 168
65eff9cc 169 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
170 if (handle == NULL) {
171 ret = -ENOMEM;
172 mlog_errno(ret);
173 goto out;
174 }
175
176 ret = ocfs2_set_inode_size(handle, inode, di_bh,
177 new_i_size);
178 if (ret < 0)
179 mlog_errno(ret);
180
02dc1af4 181 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
182out:
183 return ret;
184}
185
186static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
187 struct inode *inode,
188 struct buffer_head *fe_bh,
189 u64 new_i_size)
190{
191 int status;
1fabe148 192 handle_t *handle;
ccd979bd
MF
193
194 mlog_entry_void();
195
196 /* TODO: This needs to actually orphan the inode in this
197 * transaction. */
198
65eff9cc 199 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
200 if (IS_ERR(handle)) {
201 status = PTR_ERR(handle);
202 mlog_errno(status);
203 goto out;
204 }
205
206 status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
207 if (status < 0)
208 mlog_errno(status);
209
02dc1af4 210 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
211out:
212 mlog_exit(status);
213 return status;
214}
215
216static int ocfs2_truncate_file(struct inode *inode,
217 struct buffer_head *di_bh,
218 u64 new_i_size)
219{
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;
224
b0697053
MF
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);
ccd979bd
MF
228
229 truncate_inode_pages(inode->i_mapping, new_i_size);
230
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;
236 }
237
238 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
b0697053
MF
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,
ccd979bd 242 i_size_read(inode),
b0697053
MF
243 (unsigned long long)le64_to_cpu(fe->i_size),
244 le32_to_cpu(fe->i_flags));
ccd979bd
MF
245
246 if (new_i_size > le64_to_cpu(fe->i_size)) {
b0697053
MF
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);
ccd979bd
MF
250 status = -EINVAL;
251 mlog_errno(status);
252 goto bail;
253 }
254
b0697053
MF
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);
ccd979bd
MF
259
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;
264
ab0920ce
MF
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;
273 }
274 ocfs2_data_unlock(inode, 1);
275
ccd979bd
MF
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;
286 }
287
ccd979bd
MF
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;
296 }
297
298 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
299 if (status < 0) {
300 mlog_errno(status);
301 goto bail;
302 }
303
304 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
305 if (status < 0) {
306 mlog_errno(status);
307 goto bail;
308 }
309
310 /* TODO: orphan dir cleanup here. */
311bail:
312
313 mlog_exit(status);
314 return status;
315}
316
317/*
318 * extend allocation only here.
319 * we'll update all the disk stuff, and oip->alloc_size
320 *
321 * expect stuff to be locked, a transaction started and enough data /
322 * metadata reservations in the contexts.
323 *
324 * Will return -EAGAIN, and a reason if a restart is needed.
325 * If passed in, *reason will always be set, even in error.
326 */
327int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
328 struct inode *inode,
329 u32 clusters_to_add,
330 struct buffer_head *fe_bh,
1fabe148 331 handle_t *handle,
ccd979bd
MF
332 struct ocfs2_alloc_context *data_ac,
333 struct ocfs2_alloc_context *meta_ac,
334 enum ocfs2_alloc_restarted *reason_ret)
335{
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;
342
343 BUG_ON(!clusters_to_add);
344
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;
350 }
351
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;
369 }
370
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;
377 }
378
379 BUG_ON(num_bits > clusters_to_add);
380
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;
387 }
388
389 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
b0697053
MF
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);
ccd979bd
MF
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;
397 }
398
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);
403
404 status = ocfs2_journal_dirty(handle, fe_bh);
405 if (status < 0) {
406 mlog_errno(status);
407 goto leave;
408 }
409
410 clusters_to_add -= num_bits;
411
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;
417 }
418
419leave:
420 mlog_exit(status);
421 if (reason_ret)
422 *reason_ret = reason;
423 return status;
424}
425
426static int ocfs2_extend_allocation(struct inode *inode,
427 u32 clusters_to_add)
428{
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;
1fabe148 436 handle_t *handle = NULL;
ccd979bd
MF
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);
441
442 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
443
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;
449 }
450
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;
456 }
457
458restart_all:
459 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
460
b0697053 461 mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
ccd979bd 462 "clusters_to_add = %u\n",
b0697053 463 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
ccd979bd
MF
464 fe->i_clusters, clusters_to_add);
465
ccd979bd
MF
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;
473 }
474
475 if (!num_free_extents) {
da5cbf2f 476 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
ccd979bd
MF
477 if (status < 0) {
478 if (status != -ENOSPC)
479 mlog_errno(status);
480 goto leave;
481 }
482 }
483
da5cbf2f 484 status = ocfs2_reserve_clusters(osb, clusters_to_add, &data_ac);
ccd979bd
MF
485 if (status < 0) {
486 if (status != -ENOSPC)
487 mlog_errno(status);
488 goto leave;
489 }
490
491 /* blocks peope in read/write from reading our allocation
1b1dcc1b 492 * until we're done changing it. We depend on i_mutex to block
ccd979bd
MF
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;
497
498 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
65eff9cc 499 handle = ocfs2_start_trans(osb, credits);
ccd979bd
MF
500 if (IS_ERR(handle)) {
501 status = PTR_ERR(handle);
502 handle = NULL;
503 mlog_errno(status);
504 goto leave;
505 }
506
507restarted_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;
516 }
517
518 prev_clusters = OCFS2_I(inode)->ip_clusters;
519
520 status = ocfs2_do_extend_allocation(osb,
521 inode,
522 clusters_to_add,
523 bh,
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;
532 }
533
534 status = ocfs2_journal_dirty(handle, bh);
535 if (status < 0) {
536 mlog_errno(status);
537 goto leave;
538 }
539
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);
543
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);
550
551 mlog(0, "restarting transaction.\n");
552 /* TODO: This can be more intelligent. */
553 credits = ocfs2_calc_extend_credits(osb->sb,
554 fe,
555 clusters_to_add);
1fabe148 556 status = ocfs2_extend_trans(handle, credits);
ccd979bd
MF
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;
563 }
564 goto restarted_transaction;
565 }
566 }
567
b0697053
MF
568 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
569 fe->i_clusters, (unsigned long long)fe->i_size);
ccd979bd
MF
570 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
571 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
572
573leave:
574 if (drop_alloc_sem) {
575 up_write(&OCFS2_I(inode)->ip_alloc_sem);
576 drop_alloc_sem = 0;
577 }
578 if (handle) {
02dc1af4 579 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
580 handle = NULL;
581 }
582 if (data_ac) {
583 ocfs2_free_alloc_context(data_ac);
584 data_ac = NULL;
585 }
586 if (meta_ac) {
587 ocfs2_free_alloc_context(meta_ac);
588 meta_ac = NULL;
589 }
590 if ((!status) && restart_func) {
591 restart_func = 0;
592 goto restart_all;
593 }
594 if (bh) {
595 brelse(bh);
596 bh = NULL;
597 }
598
599 mlog_exit(status);
600 return status;
601}
602
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
53013cba
MF
605 * worry about recursive locking in ->prepare_write() and
606 * ->commit_write(). */
ccd979bd
MF
607static int ocfs2_write_zero_page(struct inode *inode,
608 u64 size)
609{
610 struct address_space *mapping = inode->i_mapping;
611 struct page *page;
612 unsigned long index;
613 unsigned int offset;
1fabe148 614 handle_t *handle = NULL;
ccd979bd
MF
615 int ret;
616
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
621 */
622 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
623 offset++;
624 }
625 index = size >> PAGE_CACHE_SHIFT;
626
627 page = grab_cache_page(mapping, index);
628 if (!page) {
629 ret = -ENOMEM;
630 mlog_errno(ret);
631 goto out;
632 }
633
53013cba 634 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
ccd979bd
MF
635 if (ret < 0) {
636 mlog_errno(ret);
637 goto out_unlock;
638 }
639
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;
647 }
648 }
649
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;
656
657 if (handle)
02dc1af4 658 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
ccd979bd
MF
659out_unlock:
660 unlock_page(page);
661 page_cache_release(page);
662out:
663 return ret;
664}
665
666static int ocfs2_zero_extend(struct inode *inode,
667 u64 zero_to_size)
668{
669 int ret = 0;
670 u64 start_off;
671 struct super_block *sb = inode->i_sb;
672
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;
679 }
680
681 start_off += sb->s_blocksize;
e2057c5a
MF
682
683 /*
684 * Very large extends have the potential to lock up
685 * the cpu for extended periods of time.
686 */
687 cond_resched();
ccd979bd
MF
688 }
689
690out:
691 return ret;
692}
693
53013cba
MF
694/*
695 * A tail_to_skip value > 0 indicates that we're being called from
696 * ocfs2_file_aio_write(). This has the following implications:
697 *
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.
703 */
ccd979bd
MF
704static int ocfs2_extend_file(struct inode *inode,
705 struct buffer_head *di_bh,
53013cba
MF
706 u64 new_i_size,
707 size_t tail_to_skip)
ccd979bd
MF
708{
709 int ret = 0;
710 u32 clusters_to_add;
711
53013cba
MF
712 BUG_ON(!tail_to_skip && !di_bh);
713
ccd979bd
MF
714 /* setattr sometimes calls us like this. */
715 if (new_i_size == 0)
716 goto out;
717
718 if (i_size_read(inode) == new_i_size)
719 goto out;
720 BUG_ON(new_i_size < i_size_read(inode));
721
722 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
723 OCFS2_I(inode)->ip_clusters;
724
0effef77
MF
725 /*
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.
731 */
732 ret = ocfs2_data_lock(inode, 1);
733 if (ret < 0) {
734 mlog_errno(ret);
735 goto out;
736 }
ccd979bd 737
0effef77 738 if (clusters_to_add) {
53013cba 739 ret = ocfs2_extend_allocation(inode, clusters_to_add);
ccd979bd
MF
740 if (ret < 0) {
741 mlog_errno(ret);
53013cba 742 goto out_unlock;
ccd979bd 743 }
0effef77 744 }
ccd979bd 745
0effef77
MF
746 /*
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.
750 */
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;
53013cba
MF
755 }
756
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);
ccd979bd
MF
763 }
764
53013cba 765out_unlock:
0effef77 766 ocfs2_data_unlock(inode, 1);
53013cba 767
ccd979bd
MF
768out:
769 return ret;
770}
771
772int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
773{
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;
1fabe148 779 handle_t *handle = NULL;
ccd979bd
MF
780
781 mlog_entry("(0x%p, '%.*s')\n", dentry,
782 dentry->d_name.len, dentry->d_name.name);
783
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");
794
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;
800 }
801
802 status = inode_change_ok(inode, attr);
803 if (status)
804 return status;
805
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;
812 }
813 }
814
4bcec184 815 status = ocfs2_meta_lock(inode, &bh, 1);
ccd979bd
MF
816 if (status < 0) {
817 if (status != -ENOENT)
818 mlog_errno(status);
819 goto bail_unlock_rw;
820 }
821
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
53013cba 826 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
ccd979bd
MF
827 if (status < 0) {
828 if (status != -ENOSPC)
829 mlog_errno(status);
830 status = -ENOSPC;
831 goto bail_unlock;
832 }
833 }
834
65eff9cc 835 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
836 if (IS_ERR(handle)) {
837 status = PTR_ERR(handle);
838 mlog_errno(status);
839 goto bail_unlock;
840 }
841
842 status = inode_setattr(inode, attr);
843 if (status < 0) {
844 mlog_errno(status);
845 goto bail_commit;
846 }
847
848 status = ocfs2_mark_inode_dirty(handle, inode, bh);
849 if (status < 0)
850 mlog_errno(status);
851
852bail_commit:
02dc1af4 853 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
854bail_unlock:
855 ocfs2_meta_unlock(inode, 1);
856bail_unlock_rw:
857 if (size_change)
858 ocfs2_rw_unlock(inode, 1);
859bail:
860 if (bh)
861 brelse(bh);
862
863 mlog_exit(status);
864 return status;
865}
866
867int ocfs2_getattr(struct vfsmount *mnt,
868 struct dentry *dentry,
869 struct kstat *stat)
870{
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;
875
876 mlog_entry_void();
877
878 err = ocfs2_inode_revalidate(dentry);
879 if (err) {
880 if (err != -ENOENT)
881 mlog_errno(err);
882 goto bail;
883 }
884
885 generic_fillattr(inode, stat);
886
887 /* We set the blksize from the cluster size for performance */
888 stat->blksize = osb->s_clustersize;
889
890bail:
891 mlog_exit(err);
892
893 return err;
894}
895
896static int ocfs2_write_remove_suid(struct inode *inode)
897{
898 int ret;
899 struct buffer_head *bh = NULL;
900 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1fabe148 901 handle_t *handle;
ccd979bd
MF
902 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
903 struct ocfs2_dinode *di;
904
b0697053
MF
905 mlog_entry("(Inode %llu, mode 0%o)\n",
906 (unsigned long long)oi->ip_blkno, inode->i_mode);
ccd979bd 907
65eff9cc 908 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
909 if (handle == NULL) {
910 ret = -ENOMEM;
911 mlog_errno(ret);
912 goto out;
913 }
914
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;
919 }
920
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;
926 }
927
928 inode->i_mode &= ~S_ISUID;
929 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
930 inode->i_mode &= ~S_ISGID;
931
932 di = (struct ocfs2_dinode *) bh->b_data;
933 di->i_mode = cpu_to_le16(inode->i_mode);
934
935 ret = ocfs2_journal_dirty(handle, bh);
936 if (ret < 0)
937 mlog_errno(ret);
938out_bh:
939 brelse(bh);
940out_trans:
02dc1af4 941 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
942out:
943 mlog_exit(ret);
944 return ret;
945}
946
8659ac25
TY
947static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
948 loff_t *ppos,
949 size_t count,
950 int appending)
ccd979bd 951{
8659ac25
TY
952 int ret = 0, meta_level = appending;
953 struct inode *inode = dentry->d_inode;
ccd979bd 954 u32 clusters;
ccd979bd 955 loff_t newsize, saved_pos;
ccd979bd 956
ccd979bd
MF
957 /*
958 * We sample i_size under a read level meta lock to see if our write
959 * is extending the file, if it is we back off and get a write level
960 * meta lock.
961 */
ccd979bd 962 for(;;) {
4bcec184 963 ret = ocfs2_meta_lock(inode, NULL, meta_level);
ccd979bd
MF
964 if (ret < 0) {
965 meta_level = -1;
966 mlog_errno(ret);
967 goto out;
968 }
969
970 /* Clear suid / sgid if necessary. We do this here
971 * instead of later in the write path because
972 * remove_suid() calls ->setattr without any hint that
973 * we may have already done our cluster locking. Since
974 * ocfs2_setattr() *must* take cluster locks to
975 * proceeed, this will lead us to recursively lock the
976 * inode. There's also the dinode i_size state which
977 * can be lost via setattr during extending writes (we
978 * set inode->i_size at the end of a write. */
8659ac25 979 if (should_remove_suid(dentry)) {
ccd979bd
MF
980 if (meta_level == 0) {
981 ocfs2_meta_unlock(inode, meta_level);
982 meta_level = 1;
983 continue;
984 }
985
986 ret = ocfs2_write_remove_suid(inode);
987 if (ret < 0) {
988 mlog_errno(ret);
8659ac25 989 goto out_unlock;
ccd979bd
MF
990 }
991 }
992
993 /* work on a copy of ppos until we're sure that we won't have
994 * to recalculate it due to relocking. */
8659ac25 995 if (appending) {
ccd979bd
MF
996 saved_pos = i_size_read(inode);
997 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
998 } else {
8659ac25 999 saved_pos = *ppos;
ccd979bd 1000 }
8659ac25 1001 newsize = count + saved_pos;
ccd979bd 1002
215c7f9f
MF
1003 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1004 (long long) saved_pos, (long long) newsize,
1005 (long long) i_size_read(inode));
ccd979bd
MF
1006
1007 /* No need for a higher level metadata lock if we're
1008 * never going past i_size. */
1009 if (newsize <= i_size_read(inode))
1010 break;
1011
1012 if (meta_level == 0) {
1013 ocfs2_meta_unlock(inode, meta_level);
1014 meta_level = 1;
1015 continue;
1016 }
1017
1018 spin_lock(&OCFS2_I(inode)->ip_lock);
1019 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1020 OCFS2_I(inode)->ip_clusters;
1021 spin_unlock(&OCFS2_I(inode)->ip_lock);
1022
1023 mlog(0, "Writing at EOF, may need more allocation: "
215c7f9f
MF
1024 "i_size = %lld, newsize = %lld, need %u clusters\n",
1025 (long long) i_size_read(inode), (long long) newsize,
1026 clusters);
ccd979bd
MF
1027
1028 /* We only want to continue the rest of this loop if
1029 * our extend will actually require more
1030 * allocation. */
1031 if (!clusters)
1032 break;
1033
8659ac25 1034 ret = ocfs2_extend_file(inode, NULL, newsize, count);
ccd979bd
MF
1035 if (ret < 0) {
1036 if (ret != -ENOSPC)
1037 mlog_errno(ret);
8659ac25 1038 goto out_unlock;
ccd979bd 1039 }
ccd979bd
MF
1040 break;
1041 }
1042
8659ac25
TY
1043 if (appending)
1044 *ppos = saved_pos;
1045
1046out_unlock:
ccd979bd 1047 ocfs2_meta_unlock(inode, meta_level);
8659ac25
TY
1048
1049out:
1050 return ret;
1051}
1052
1053static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1054 const struct iovec *iov,
1055 unsigned long nr_segs,
1056 loff_t pos)
1057{
1058 int ret, rw_level, have_alloc_sem = 0;
1059 struct file *filp = iocb->ki_filp;
1060 struct inode *inode = filp->f_dentry->d_inode;
1061 int appending = filp->f_flags & O_APPEND ? 1 : 0;
1062
1063 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1064 (unsigned int)nr_segs,
1065 filp->f_dentry->d_name.len,
1066 filp->f_dentry->d_name.name);
1067
1068 /* happy write of zero bytes */
1069 if (iocb->ki_left == 0)
1070 return 0;
1071
1072 mutex_lock(&inode->i_mutex);
1073 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1074 if (filp->f_flags & O_DIRECT) {
1075 have_alloc_sem = 1;
1076 down_read(&inode->i_alloc_sem);
1077 }
1078
1079 /* concurrent O_DIRECT writes are allowed */
1080 rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
1081 ret = ocfs2_rw_lock(inode, rw_level);
1082 if (ret < 0) {
1083 rw_level = -1;
1084 mlog_errno(ret);
1085 goto out;
1086 }
1087
1088 ret = ocfs2_prepare_inode_for_write(filp->f_dentry, &iocb->ki_pos,
1089 iocb->ki_left, appending);
1090 if (ret < 0) {
1091 mlog_errno(ret);
1092 goto out;
1093 }
ccd979bd
MF
1094
1095 /* communicate with ocfs2_dio_end_io */
1096 ocfs2_iocb_set_rw_locked(iocb);
1097
027445c3 1098 ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
ccd979bd
MF
1099
1100 /* buffered aio wouldn't have proper lock coverage today */
1101 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1102
1103 /*
1104 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1105 * function pointer which is called when o_direct io completes so that
1106 * it can unlock our rw lock. (it's the clustered equivalent of
1107 * i_alloc_sem; protects truncate from racing with pending ios).
1108 * Unfortunately there are error cases which call end_io and others
1109 * that don't. so we don't have to unlock the rw_lock if either an
1110 * async dio is going to do it in the future or an end_io after an
1111 * error has already done it.
1112 */
1113 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1114 rw_level = -1;
1115 have_alloc_sem = 0;
1116 }
1117
1118out:
ccd979bd
MF
1119 if (have_alloc_sem)
1120 up_read(&inode->i_alloc_sem);
1121 if (rw_level != -1)
1122 ocfs2_rw_unlock(inode, rw_level);
1b1dcc1b 1123 mutex_unlock(&inode->i_mutex);
ccd979bd
MF
1124
1125 mlog_exit(ret);
1126 return ret;
1127}
1128
8659ac25
TY
1129static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1130 struct file *out,
1131 loff_t *ppos,
1132 size_t len,
1133 unsigned int flags)
1134{
1135 int ret;
1136 struct inode *inode = out->f_dentry->d_inode;
1137
1138 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1139 (unsigned int)len,
1140 out->f_dentry->d_name.len,
1141 out->f_dentry->d_name.name);
1142
1143 inode_double_lock(inode, pipe->inode);
1144
1145 ret = ocfs2_rw_lock(inode, 1);
1146 if (ret < 0) {
1147 mlog_errno(ret);
1148 goto out;
1149 }
1150
1151 ret = ocfs2_prepare_inode_for_write(out->f_dentry, ppos, len, 0);
1152 if (ret < 0) {
1153 mlog_errno(ret);
1154 goto out_unlock;
1155 }
1156
1157 /* ok, we're done with i_size and alloc work */
1158 ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
1159
1160out_unlock:
1161 ocfs2_rw_unlock(inode, 1);
1162out:
1163 inode_double_unlock(inode, pipe->inode);
1164
1165 mlog_exit(ret);
1166 return ret;
1167}
1168
1169static ssize_t ocfs2_file_splice_read(struct file *in,
1170 loff_t *ppos,
1171 struct pipe_inode_info *pipe,
1172 size_t len,
1173 unsigned int flags)
1174{
1175 int ret = 0;
1176 struct inode *inode = in->f_dentry->d_inode;
1177
1178 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1179 (unsigned int)len,
1180 in->f_dentry->d_name.len,
1181 in->f_dentry->d_name.name);
1182
1183 /*
1184 * See the comment in ocfs2_file_aio_read()
1185 */
1186 ret = ocfs2_meta_lock(inode, NULL, 0);
1187 if (ret < 0) {
1188 mlog_errno(ret);
1189 goto bail;
1190 }
1191 ocfs2_meta_unlock(inode, 0);
1192
1193 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1194
1195bail:
1196 mlog_exit(ret);
1197 return ret;
1198}
1199
ccd979bd 1200static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
027445c3
BP
1201 const struct iovec *iov,
1202 unsigned long nr_segs,
ccd979bd
MF
1203 loff_t pos)
1204{
1205 int ret = 0, rw_level = -1, have_alloc_sem = 0;
1206 struct file *filp = iocb->ki_filp;
1207 struct inode *inode = filp->f_dentry->d_inode;
ccd979bd 1208
027445c3
BP
1209 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1210 (unsigned int)nr_segs,
ccd979bd
MF
1211 filp->f_dentry->d_name.len,
1212 filp->f_dentry->d_name.name);
1213
1214 if (!inode) {
1215 ret = -EINVAL;
1216 mlog_errno(ret);
1217 goto bail;
1218 }
1219
ccd979bd
MF
1220 /*
1221 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1222 * need locks to protect pending reads from racing with truncate.
1223 */
1224 if (filp->f_flags & O_DIRECT) {
1225 down_read(&inode->i_alloc_sem);
1226 have_alloc_sem = 1;
1227
1228 ret = ocfs2_rw_lock(inode, 0);
1229 if (ret < 0) {
1230 mlog_errno(ret);
1231 goto bail;
1232 }
1233 rw_level = 0;
1234 /* communicate with ocfs2_dio_end_io */
1235 ocfs2_iocb_set_rw_locked(iocb);
1236 }
1237
c4374f8a
MF
1238 /*
1239 * We're fine letting folks race truncates and extending
1240 * writes with read across the cluster, just like they can
1241 * locally. Hence no rw_lock during read.
1242 *
1243 * Take and drop the meta data lock to update inode fields
1244 * like i_size. This allows the checks down below
1245 * generic_file_aio_read() a chance of actually working.
1246 */
4bcec184 1247 ret = ocfs2_meta_lock(inode, NULL, 0);
c4374f8a
MF
1248 if (ret < 0) {
1249 mlog_errno(ret);
1250 goto bail;
1251 }
1252 ocfs2_meta_unlock(inode, 0);
1253
027445c3 1254 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
ccd979bd
MF
1255 if (ret == -EINVAL)
1256 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1257
1258 /* buffered aio wouldn't have proper lock coverage today */
1259 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1260
1261 /* see ocfs2_file_aio_write */
1262 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1263 rw_level = -1;
1264 have_alloc_sem = 0;
1265 }
1266
1267bail:
1268 if (have_alloc_sem)
1269 up_read(&inode->i_alloc_sem);
1270 if (rw_level != -1)
1271 ocfs2_rw_unlock(inode, rw_level);
1272 mlog_exit(ret);
1273
1274 return ret;
1275}
1276
1277struct inode_operations ocfs2_file_iops = {
1278 .setattr = ocfs2_setattr,
1279 .getattr = ocfs2_getattr,
1280};
1281
1282struct inode_operations ocfs2_special_file_iops = {
1283 .setattr = ocfs2_setattr,
1284 .getattr = ocfs2_getattr,
1285};
1286
4b6f5d20 1287const struct file_operations ocfs2_fops = {
ccd979bd
MF
1288 .read = do_sync_read,
1289 .write = do_sync_write,
1290 .sendfile = generic_file_sendfile,
1291 .mmap = ocfs2_mmap,
1292 .fsync = ocfs2_sync_file,
1293 .release = ocfs2_file_release,
1294 .open = ocfs2_file_open,
1295 .aio_read = ocfs2_file_aio_read,
1296 .aio_write = ocfs2_file_aio_write,
ca4d147e 1297 .ioctl = ocfs2_ioctl,
8659ac25
TY
1298 .splice_read = ocfs2_file_splice_read,
1299 .splice_write = ocfs2_file_splice_write,
ccd979bd
MF
1300};
1301
4b6f5d20 1302const struct file_operations ocfs2_dops = {
ccd979bd
MF
1303 .read = generic_read_dir,
1304 .readdir = ocfs2_readdir,
1305 .fsync = ocfs2_sync_file,
ca4d147e 1306 .ioctl = ocfs2_ioctl,
ccd979bd 1307};