1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
23 #include <linux/slab.h>
24 #include <linux/highmem.h>
25 #include <linux/pagemap.h>
26 #include <asm/byteorder.h>
28 #define MLOG_MASK_PREFIX ML_FILE_IO
29 #include <cluster/masklog.h>
36 #include "extent_map.h"
43 #include "buffer_head_io.h"
45 static int ocfs2_symlink_get_block(struct inode
*inode
, sector_t iblock
,
46 struct buffer_head
*bh_result
, int create
)
50 struct ocfs2_dinode
*fe
= NULL
;
51 struct buffer_head
*bh
= NULL
;
52 struct buffer_head
*buffer_cache_bh
= NULL
;
53 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
56 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode
,
57 (unsigned long long)iblock
, bh_result
, create
);
59 BUG_ON(ocfs2_inode_is_fast_symlink(inode
));
61 if ((iblock
<< inode
->i_sb
->s_blocksize_bits
) > PATH_MAX
+ 1) {
62 mlog(ML_ERROR
, "block offset > PATH_MAX: %llu",
63 (unsigned long long)iblock
);
67 status
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
68 OCFS2_I(inode
)->ip_blkno
,
69 &bh
, OCFS2_BH_CACHED
, inode
);
74 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
76 if (!OCFS2_IS_VALID_DINODE(fe
)) {
77 mlog(ML_ERROR
, "Invalid dinode #%llu: signature = %.*s\n",
78 (unsigned long long)fe
->i_blkno
, 7, fe
->i_signature
);
82 if ((u64
)iblock
>= ocfs2_clusters_to_blocks(inode
->i_sb
,
83 le32_to_cpu(fe
->i_clusters
))) {
84 mlog(ML_ERROR
, "block offset is outside the allocated size: "
85 "%llu\n", (unsigned long long)iblock
);
89 /* We don't use the page cache to create symlink data, so if
90 * need be, copy it over from the buffer cache. */
91 if (!buffer_uptodate(bh_result
) && ocfs2_inode_is_new(inode
)) {
92 u64 blkno
= le64_to_cpu(fe
->id2
.i_list
.l_recs
[0].e_blkno
) +
94 buffer_cache_bh
= sb_getblk(osb
->sb
, blkno
);
95 if (!buffer_cache_bh
) {
96 mlog(ML_ERROR
, "couldn't getblock for symlink!\n");
100 /* we haven't locked out transactions, so a commit
101 * could've happened. Since we've got a reference on
102 * the bh, even if it commits while we're doing the
103 * copy, the data is still good. */
104 if (buffer_jbd(buffer_cache_bh
)
105 && ocfs2_inode_is_new(inode
)) {
106 kaddr
= kmap_atomic(bh_result
->b_page
, KM_USER0
);
108 mlog(ML_ERROR
, "couldn't kmap!\n");
111 memcpy(kaddr
+ (bh_result
->b_size
* iblock
),
112 buffer_cache_bh
->b_data
,
114 kunmap_atomic(kaddr
, KM_USER0
);
115 set_buffer_uptodate(bh_result
);
117 brelse(buffer_cache_bh
);
120 map_bh(bh_result
, inode
->i_sb
,
121 le64_to_cpu(fe
->id2
.i_list
.l_recs
[0].e_blkno
) + iblock
);
133 static int ocfs2_get_block(struct inode
*inode
, sector_t iblock
,
134 struct buffer_head
*bh_result
, int create
)
137 u64 p_blkno
, past_eof
;
139 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode
,
140 (unsigned long long)iblock
, bh_result
, create
);
142 if (OCFS2_I(inode
)->ip_flags
& OCFS2_INODE_SYSTEM_FILE
)
143 mlog(ML_NOTICE
, "get_block on system inode 0x%p (%lu)\n",
144 inode
, inode
->i_ino
);
146 if (S_ISLNK(inode
->i_mode
)) {
147 /* this always does I/O for some reason. */
148 err
= ocfs2_symlink_get_block(inode
, iblock
, bh_result
, create
);
152 /* this can happen if another node truncs after our extend! */
153 spin_lock(&OCFS2_I(inode
)->ip_lock
);
154 if (iblock
>= ocfs2_clusters_to_blocks(inode
->i_sb
,
155 OCFS2_I(inode
)->ip_clusters
))
157 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
161 err
= ocfs2_extent_map_get_blocks(inode
, iblock
, 1, &p_blkno
,
164 mlog(ML_ERROR
, "Error %d from get_blocks(0x%p, %llu, 1, "
165 "%llu, NULL)\n", err
, inode
, (unsigned long long)iblock
,
166 (unsigned long long)p_blkno
);
170 map_bh(bh_result
, inode
->i_sb
, p_blkno
);
172 if (bh_result
->b_blocknr
== 0) {
174 mlog(ML_ERROR
, "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
175 (unsigned long long)iblock
,
176 (unsigned long long)p_blkno
,
177 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
180 past_eof
= ocfs2_blocks_for_bytes(inode
->i_sb
, i_size_read(inode
));
181 mlog(0, "Inode %lu, past_eof = %llu\n", inode
->i_ino
,
182 (unsigned long long)past_eof
);
184 if (create
&& (iblock
>= past_eof
))
185 set_buffer_new(bh_result
);
195 static int ocfs2_readpage(struct file
*file
, struct page
*page
)
197 struct inode
*inode
= page
->mapping
->host
;
198 loff_t start
= (loff_t
)page
->index
<< PAGE_CACHE_SHIFT
;
201 mlog_entry("(0x%p, %lu)\n", file
, (page
? page
->index
: 0));
203 ret
= ocfs2_meta_lock_with_page(inode
, NULL
, NULL
, 0, page
);
205 if (ret
== AOP_TRUNCATED_PAGE
)
211 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
214 * i_size might have just been updated as we grabed the meta lock. We
215 * might now be discovering a truncate that hit on another node.
216 * block_read_full_page->get_block freaks out if it is asked to read
217 * beyond the end of a file, so we check here. Callers
218 * (generic_file_read, fault->nopage) are clever enough to check i_size
219 * and notice that the page they just read isn't needed.
221 * XXX sys_readahead() seems to get that wrong?
223 if (start
>= i_size_read(inode
)) {
224 char *addr
= kmap(page
);
225 memset(addr
, 0, PAGE_SIZE
);
226 flush_dcache_page(page
);
228 SetPageUptodate(page
);
233 ret
= ocfs2_data_lock_with_page(inode
, 0, page
);
235 if (ret
== AOP_TRUNCATED_PAGE
)
241 ret
= block_read_full_page(page
, ocfs2_get_block
);
244 ocfs2_data_unlock(inode
, 0);
246 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
247 ocfs2_meta_unlock(inode
, 0);
255 /* Note: Because we don't support holes, our allocation has
256 * already happened (allocation writes zeros to the file data)
257 * so we don't have to worry about ordered writes in
260 * ->writepage is called during the process of invalidating the page cache
261 * during blocked lock processing. It can't block on any cluster locks
262 * to during block mapping. It's relying on the fact that the block
263 * mapping can't have disappeared under the dirty pages that it is
264 * being asked to write back.
266 static int ocfs2_writepage(struct page
*page
, struct writeback_control
*wbc
)
270 mlog_entry("(0x%p)\n", page
);
272 ret
= block_write_full_page(page
, ocfs2_get_block
, wbc
);
280 * ocfs2_prepare_write() can be an outer-most ocfs2 call when it is called
281 * from loopback. It must be able to perform its own locking around
284 int ocfs2_prepare_write(struct file
*file
, struct page
*page
,
285 unsigned from
, unsigned to
)
287 struct inode
*inode
= page
->mapping
->host
;
290 mlog_entry("(0x%p, 0x%p, %u, %u)\n", file
, page
, from
, to
);
292 ret
= ocfs2_meta_lock_with_page(inode
, NULL
, NULL
, 0, page
);
298 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
300 ret
= block_prepare_write(page
, from
, to
, ocfs2_get_block
);
302 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
304 ocfs2_meta_unlock(inode
, 0);
310 /* Taken from ext3. We don't necessarily need the full blown
311 * functionality yet, but IMHO it's better to cut and paste the whole
312 * thing so we can avoid introducing our own bugs (and easily pick up
313 * their fixes when they happen) --Mark */
314 static int walk_page_buffers( handle_t
*handle
,
315 struct buffer_head
*head
,
319 int (*fn
)( handle_t
*handle
,
320 struct buffer_head
*bh
))
322 struct buffer_head
*bh
;
323 unsigned block_start
, block_end
;
324 unsigned blocksize
= head
->b_size
;
326 struct buffer_head
*next
;
328 for ( bh
= head
, block_start
= 0;
329 ret
== 0 && (bh
!= head
|| !block_start
);
330 block_start
= block_end
, bh
= next
)
332 next
= bh
->b_this_page
;
333 block_end
= block_start
+ blocksize
;
334 if (block_end
<= from
|| block_start
>= to
) {
335 if (partial
&& !buffer_uptodate(bh
))
339 err
= (*fn
)(handle
, bh
);
346 struct ocfs2_journal_handle
*ocfs2_start_walk_page_trans(struct inode
*inode
,
351 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
352 struct ocfs2_journal_handle
*handle
= NULL
;
355 handle
= ocfs2_start_trans(osb
, NULL
, OCFS2_INODE_UPDATE_CREDITS
);
362 if (ocfs2_should_order_data(inode
)) {
363 ret
= walk_page_buffers(handle
->k_handle
,
366 ocfs2_journal_dirty_data
);
373 ocfs2_commit_trans(handle
);
374 handle
= ERR_PTR(ret
);
379 static int ocfs2_commit_write(struct file
*file
, struct page
*page
,
380 unsigned from
, unsigned to
)
382 int ret
, extending
= 0, locklevel
= 0;
384 struct buffer_head
*di_bh
= NULL
;
385 struct inode
*inode
= page
->mapping
->host
;
386 struct ocfs2_journal_handle
*handle
= NULL
;
388 mlog_entry("(0x%p, 0x%p, %u, %u)\n", file
, page
, from
, to
);
390 /* NOTE: ocfs2_file_aio_write has ensured that it's safe for
391 * us to sample inode->i_size here without the metadata lock:
393 * 1) We're currently holding the inode alloc lock, so no
394 * nodes can change it underneath us.
396 * 2) We've had to take the metadata lock at least once
397 * already to check for extending writes, hence insuring
398 * that our current copy is also up to date.
400 new_i_size
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
401 if (new_i_size
> i_size_read(inode
)) {
406 ret
= ocfs2_meta_lock_with_page(inode
, NULL
, &di_bh
, locklevel
, page
);
412 ret
= ocfs2_data_lock_with_page(inode
, 1, page
);
415 goto out_unlock_meta
;
419 handle
= ocfs2_start_walk_page_trans(inode
, page
, from
, to
);
420 if (IS_ERR(handle
)) {
421 ret
= PTR_ERR(handle
);
423 goto out_unlock_data
;
426 /* Mark our buffer early. We'd rather catch this error up here
427 * as opposed to after a successful commit_write which would
428 * require us to set back inode->i_size. */
429 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
430 OCFS2_JOURNAL_ACCESS_WRITE
);
437 /* might update i_size */
438 ret
= generic_commit_write(file
, page
, from
, to
);
445 loff_t size
= (u64
) i_size_read(inode
);
446 struct ocfs2_dinode
*di
=
447 (struct ocfs2_dinode
*)di_bh
->b_data
;
449 /* ocfs2_mark_inode_dirty is too heavy to use here. */
450 inode
->i_blocks
= ocfs2_align_bytes_to_sectors(size
);
451 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
453 di
->i_size
= cpu_to_le64(size
);
454 di
->i_ctime
= di
->i_mtime
=
455 cpu_to_le64(inode
->i_mtime
.tv_sec
);
456 di
->i_ctime_nsec
= di
->i_mtime_nsec
=
457 cpu_to_le32(inode
->i_mtime
.tv_nsec
);
459 ret
= ocfs2_journal_dirty(handle
, di_bh
);
466 BUG_ON(extending
&& (i_size_read(inode
) != new_i_size
));
470 ocfs2_commit_trans(handle
);
472 ocfs2_data_unlock(inode
, 1);
474 ocfs2_meta_unlock(inode
, locklevel
);
483 static sector_t
ocfs2_bmap(struct address_space
*mapping
, sector_t block
)
488 struct inode
*inode
= mapping
->host
;
490 mlog_entry("(block = %llu)\n", (unsigned long long)block
);
492 /* We don't need to lock journal system files, since they aren't
493 * accessed concurrently from multiple nodes.
495 if (!INODE_JOURNAL(inode
)) {
496 err
= ocfs2_meta_lock(inode
, NULL
, NULL
, 0);
502 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
505 err
= ocfs2_extent_map_get_blocks(inode
, block
, 1, &p_blkno
,
508 if (!INODE_JOURNAL(inode
)) {
509 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
510 ocfs2_meta_unlock(inode
, 0);
514 mlog(ML_ERROR
, "get_blocks() failed, block = %llu\n",
515 (unsigned long long)block
);
522 status
= err
? 0 : p_blkno
;
524 mlog_exit((int)status
);
530 * TODO: Make this into a generic get_blocks function.
532 * From do_direct_io in direct-io.c:
533 * "So what we do is to permit the ->get_blocks function to populate
534 * bh.b_size with the size of IO which is permitted at this offset and
537 * This function is called directly from get_more_blocks in direct-io.c.
539 * called like this: dio->get_blocks(dio->inode, fs_startblk,
540 * fs_count, map_bh, dio->rw == WRITE);
542 static int ocfs2_direct_IO_get_blocks(struct inode
*inode
, sector_t iblock
,
543 struct buffer_head
*bh_result
, int create
)
546 u64 vbo_max
; /* file offset, max_blocks from iblock */
549 unsigned char blocksize_bits
;
550 unsigned long max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
552 if (!inode
|| !bh_result
) {
553 mlog(ML_ERROR
, "inode or bh_result is null\n");
557 blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
559 /* This function won't even be called if the request isn't all
560 * nicely aligned and of the right size, so there's no need
561 * for us to check any of that. */
563 vbo_max
= ((u64
)iblock
+ max_blocks
) << blocksize_bits
;
565 spin_lock(&OCFS2_I(inode
)->ip_lock
);
566 if ((iblock
+ max_blocks
) >
567 ocfs2_clusters_to_blocks(inode
->i_sb
,
568 OCFS2_I(inode
)->ip_clusters
)) {
569 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
573 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
575 /* This figures out the size of the next contiguous block, and
576 * our logical offset */
577 ret
= ocfs2_extent_map_get_blocks(inode
, iblock
, 1, &p_blkno
,
580 mlog(ML_ERROR
, "get_blocks() failed iblock=%llu\n",
581 (unsigned long long)iblock
);
586 map_bh(bh_result
, inode
->i_sb
, p_blkno
);
588 /* make sure we don't map more than max_blocks blocks here as
589 that's all the kernel will handle at this point. */
590 if (max_blocks
< contig_blocks
)
591 contig_blocks
= max_blocks
;
592 bh_result
->b_size
= contig_blocks
<< blocksize_bits
;
598 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
599 * particularly interested in the aio/dio case. Like the core uses
600 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
601 * truncation on another.
603 static void ocfs2_dio_end_io(struct kiocb
*iocb
,
608 struct inode
*inode
= iocb
->ki_filp
->f_dentry
->d_inode
;
610 /* this io's submitter should not have unlocked this before we could */
611 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb
));
612 ocfs2_iocb_clear_rw_locked(iocb
);
613 up_read(&inode
->i_alloc_sem
);
614 ocfs2_rw_unlock(inode
, 0);
617 static ssize_t
ocfs2_direct_IO(int rw
,
619 const struct iovec
*iov
,
621 unsigned long nr_segs
)
623 struct file
*file
= iocb
->ki_filp
;
624 struct inode
*inode
= file
->f_dentry
->d_inode
->i_mapping
->host
;
628 ret
= blockdev_direct_IO_no_locking(rw
, iocb
, inode
,
629 inode
->i_sb
->s_bdev
, iov
, offset
,
631 ocfs2_direct_IO_get_blocks
,
637 struct address_space_operations ocfs2_aops
= {
638 .readpage
= ocfs2_readpage
,
639 .writepage
= ocfs2_writepage
,
640 .prepare_write
= ocfs2_prepare_write
,
641 .commit_write
= ocfs2_commit_write
,
643 .sync_page
= block_sync_page
,
644 .direct_IO
= ocfs2_direct_IO