2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/pagemap.h>
16 #include <linux/mpage.h>
18 #include <asm/semaphore.h>
26 #include "ops_address.h"
34 * gfs2_get_block - Fills in a buffer head with details about a block
36 * @lblock: The block number to look up
37 * @bh_result: The buffer head to return the result in
38 * @create: Non-zero if we may add block to the file
43 int gfs2_get_block(struct inode *inode, sector_t lblock,
44 struct buffer_head *bh_result, int create)
46 struct gfs2_inode *ip = get_v2ip(inode);
51 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
58 map_bh(bh_result, inode->i_sb, dblock);
60 set_buffer_new(bh_result);
66 * get_block_noalloc - Fills in a buffer head with details about a block
68 * @lblock: The block number to look up
69 * @bh_result: The buffer head to return the result in
70 * @create: Non-zero if we may add block to the file
75 static int get_block_noalloc(struct inode *inode, sector_t lblock,
76 struct buffer_head *bh_result, int create)
78 struct gfs2_inode *ip = get_v2ip(inode);
83 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
88 map_bh(bh_result, inode->i_sb, dblock);
89 else if (gfs2_assert_withdraw(ip->i_sbd, !create))
95 static int get_blocks(struct inode *inode, sector_t lblock,
96 unsigned long max_blocks, struct buffer_head *bh_result,
99 struct gfs2_inode *ip = get_v2ip(inode);
105 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
112 map_bh(bh_result, inode->i_sb, dblock);
114 set_buffer_new(bh_result);
116 if (extlen > max_blocks)
118 bh_result->b_size = extlen << inode->i_blkbits;
123 static int get_blocks_noalloc(struct inode *inode, sector_t lblock,
124 unsigned long max_blocks,
125 struct buffer_head *bh_result, int create)
127 struct gfs2_inode *ip = get_v2ip(inode);
133 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
138 map_bh(bh_result, inode->i_sb, dblock);
139 if (extlen > max_blocks)
141 bh_result->b_size = extlen << inode->i_blkbits;
142 } else if (gfs2_assert_withdraw(ip->i_sbd, !create))
149 * gfs2_writepage - Write complete page
150 * @page: Page to write
154 * Some of this is copied from block_write_full_page() although we still
155 * call it to do most of the work.
158 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
160 struct inode *inode = page->mapping->host;
161 struct gfs2_inode *ip = get_v2ip(page->mapping->host);
162 struct gfs2_sbd *sdp = ip->i_sbd;
163 loff_t i_size = i_size_read(inode);
164 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
169 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
176 /* Is the page fully outside i_size? (truncate in progress) */
177 offset = i_size & (PAGE_CACHE_SIZE-1);
178 if (page->index >= end_index+1 || !offset) {
179 page->mapping->a_ops->invalidatepage(page, 0);
181 return 0; /* don't care */
184 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
185 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
188 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
191 error = block_write_full_page(page, get_block_noalloc, wbc);
194 gfs2_meta_cache_flush(ip);
198 redirty_page_for_writepage(wbc, page);
204 * stuffed_readpage - Fill in a Linux page with stuffed file data
211 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
213 struct buffer_head *dibh;
217 error = gfs2_meta_inode_buffer(ip, &dibh);
221 kaddr = kmap_atomic(page, KM_USER0);
222 memcpy((char *)kaddr,
223 dibh->b_data + sizeof(struct gfs2_dinode),
225 memset((char *)kaddr + ip->i_di.di_size,
227 PAGE_CACHE_SIZE - ip->i_di.di_size);
228 kunmap_atomic(page, KM_USER0);
232 SetPageUptodate(page);
237 static int zero_readpage(struct page *page)
241 kaddr = kmap_atomic(page, KM_USER0);
242 memset(kaddr, 0, PAGE_CACHE_SIZE);
243 kunmap_atomic(page, KM_USER0);
245 SetPageUptodate(page);
252 * gfs2_readpage - readpage with locking
253 * @file: The file to read a page for. N.B. This may be NULL if we are
254 * reading an internal file.
255 * @page: The page to read
260 static int gfs2_readpage(struct file *file, struct page *page)
262 struct gfs2_inode *ip = get_v2ip(page->mapping->host);
263 struct gfs2_sbd *sdp = ip->i_sbd;
264 struct gfs2_holder gh;
267 if (file != &gfs2_internal_file_sentinal) {
268 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
269 error = gfs2_glock_nq_m_atime(1, &gh);
274 if (gfs2_is_stuffed(ip)) {
276 error = stuffed_readpage(ip, page);
279 error = zero_readpage(page);
281 error = mpage_readpage(page, gfs2_get_block);
283 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
286 if (file != &gfs2_internal_file_sentinal) {
287 gfs2_glock_dq_m(1, &gh);
288 gfs2_holder_uninit(&gh);
298 * gfs2_prepare_write - Prepare to write a page to a file
299 * @file: The file to write to
300 * @page: The page which is to be prepared for writing
301 * @from: From (byte range within page)
302 * @to: To (byte range within page)
307 static int gfs2_prepare_write(struct file *file, struct page *page,
308 unsigned from, unsigned to)
310 struct gfs2_inode *ip = get_v2ip(page->mapping->host);
311 struct gfs2_sbd *sdp = ip->i_sbd;
312 unsigned int data_blocks, ind_blocks, rblocks;
315 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
316 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
317 struct gfs2_alloc *al;
319 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
320 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
324 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
326 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
331 if (alloc_required) {
332 al = gfs2_alloc_get(ip);
334 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
338 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
342 al->al_requested = data_blocks + ind_blocks;
343 error = gfs2_inplace_reserve(ip);
348 rblocks = RES_DINODE + ind_blocks;
349 if (gfs2_is_jdata(ip))
350 rblocks += data_blocks ? data_blocks : 1;
351 if (ind_blocks || data_blocks)
352 rblocks += RES_STATFS + RES_QUOTA;
354 error = gfs2_trans_begin(sdp, rblocks, 0);
358 if (gfs2_is_stuffed(ip)) {
359 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
360 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, page);
363 } else if (!PageUptodate(page))
364 error = stuffed_readpage(ip, page);
369 error = block_prepare_write(page, from, to, gfs2_get_block);
374 if (alloc_required) {
375 gfs2_inplace_release(ip);
377 gfs2_quota_unlock(ip);
382 gfs2_glock_dq_m(1, &ip->i_gh);
384 gfs2_holder_uninit(&ip->i_gh);
391 * gfs2_commit_write - Commit write to a file
392 * @file: The file to write to
393 * @page: The page containing the data
394 * @from: From (byte range within page)
395 * @to: To (byte range within page)
400 static int gfs2_commit_write(struct file *file, struct page *page,
401 unsigned from, unsigned to)
403 struct inode *inode = page->mapping->host;
404 struct gfs2_inode *ip = get_v2ip(inode);
405 struct gfs2_sbd *sdp = ip->i_sbd;
406 int error = -EOPNOTSUPP;
407 struct buffer_head *dibh;
408 struct gfs2_alloc *al = &ip->i_alloc;;
410 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
413 error = gfs2_meta_inode_buffer(ip, &dibh);
417 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
419 if (gfs2_is_stuffed(ip)) {
423 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
425 kaddr = kmap_atomic(page, KM_USER0);
426 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
427 (char *)kaddr + from, to - from);
428 kunmap_atomic(page, KM_USER0);
430 SetPageUptodate(page);
432 if (inode->i_size < file_size)
433 i_size_write(inode, file_size);
435 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
436 gfs2_page_add_databufs(ip, page, from, to);
437 error = generic_commit_write(file, page, from, to);
442 if (ip->i_di.di_size < inode->i_size)
443 ip->i_di.di_size = inode->i_size;
445 gfs2_dinode_out(&ip->i_di, dibh->b_data);
448 if (al->al_requested) {
449 gfs2_inplace_release(ip);
450 gfs2_quota_unlock(ip);
453 gfs2_glock_dq_m(1, &ip->i_gh);
454 gfs2_holder_uninit(&ip->i_gh);
461 if (al->al_requested) {
462 gfs2_inplace_release(ip);
463 gfs2_quota_unlock(ip);
466 gfs2_glock_dq_m(1, &ip->i_gh);
467 gfs2_holder_uninit(&ip->i_gh);
469 ClearPageUptodate(page);
474 * gfs2_bmap - Block map function
475 * @mapping: Address space info
476 * @lblock: The block to map
478 * Returns: The disk address for the block or 0 on hole or error
481 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
483 struct gfs2_inode *ip = get_v2ip(mapping->host);
484 struct gfs2_holder i_gh;
488 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
492 if (!gfs2_is_stuffed(ip))
493 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
495 gfs2_glock_dq_uninit(&i_gh);
500 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
502 struct gfs2_bufdata *bd;
509 gfs2_log_unlock(sdp);
512 gfs2_log_unlock(sdp);
515 clear_buffer_dirty(bh);
517 clear_buffer_mapped(bh);
518 clear_buffer_req(bh);
519 clear_buffer_new(bh);
520 clear_buffer_delay(bh);
524 static int gfs2_invalidatepage(struct page *page, unsigned long offset)
526 struct gfs2_sbd *sdp = get_v2sdp(page->mapping->host->i_sb);
527 struct buffer_head *head, *bh, *next;
528 unsigned int curr_off = 0;
531 BUG_ON(!PageLocked(page));
532 if (!page_has_buffers(page))
535 bh = head = page_buffers(page);
537 unsigned int next_off = curr_off + bh->b_size;
538 next = bh->b_this_page;
540 if (offset <= curr_off)
541 discard_buffer(sdp, bh);
545 } while (bh != head);
548 ret = try_to_release_page(page, 0);
553 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
554 loff_t offset, unsigned long nr_segs)
556 struct file *file = iocb->ki_filp;
557 struct inode *inode = file->f_mapping->host;
558 struct gfs2_inode *ip = get_v2ip(inode);
559 struct gfs2_holder gh;
563 * Shared lock, even though its write, since we do no allocation
564 * on this path. All we need change is atime.
566 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
567 rv = gfs2_glock_nq_m_atime(1, &gh);
572 * Should we return an error here? I can't see that O_DIRECT for
573 * a journaled file makes any sense. For now we'll silently fall
574 * back to buffered I/O, likewise we do the same for stuffed
575 * files since they are (a) small and (b) unaligned.
577 if (gfs2_is_jdata(ip))
580 if (gfs2_is_stuffed(ip))
583 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
584 iov, offset, nr_segs, get_blocks_noalloc,
585 NULL, DIO_OWN_LOCKING);
587 gfs2_glock_dq_m(1, &gh);
588 gfs2_holder_uninit(&gh);
596 * This is called with a shared lock already held for the read path.
597 * Currently, no locks are held when the write path is called.
599 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
600 const struct iovec *iov, loff_t offset,
601 unsigned long nr_segs)
603 struct file *file = iocb->ki_filp;
604 struct inode *inode = file->f_mapping->host;
605 struct gfs2_inode *ip = get_v2ip(inode);
606 struct gfs2_sbd *sdp = ip->i_sbd;
609 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
611 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
612 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
615 return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
616 offset, nr_segs, get_blocks, NULL,
620 struct address_space_operations gfs2_file_aops = {
621 .writepage = gfs2_writepage,
622 .readpage = gfs2_readpage,
623 .sync_page = block_sync_page,
624 .prepare_write = gfs2_prepare_write,
625 .commit_write = gfs2_commit_write,
627 .invalidatepage = gfs2_invalidatepage,
628 .direct_IO = gfs2_direct_IO,