2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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/pagevec.h>
17 #include <linux/mpage.h>
19 #include <linux/gfs2_ondisk.h>
22 #include "lm_interface.h"
29 #include "ops_address.h"
39 * gfs2_get_block - Fills in a buffer head with details about a block
41 * @lblock: The block number to look up
42 * @bh_result: The buffer head to return the result in
43 * @create: Non-zero if we may add block to the file
48 int gfs2_get_block(struct inode *inode, sector_t lblock,
49 struct buffer_head *bh_result, int create)
56 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
63 map_bh(bh_result, inode->i_sb, dblock);
65 set_buffer_new(bh_result);
67 set_buffer_boundary(bh_result);
73 * get_block_noalloc - Fills in a buffer head with details about a block
75 * @lblock: The block number to look up
76 * @bh_result: The buffer head to return the result in
77 * @create: Non-zero if we may add block to the file
82 static int get_block_noalloc(struct inode *inode, sector_t lblock,
83 struct buffer_head *bh_result, int create)
90 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
95 map_bh(bh_result, inode->i_sb, dblock);
96 else if (gfs2_assert_withdraw(GFS2_SB(inode), !create))
99 set_buffer_boundary(bh_result);
105 * gfs2_writepage - Write complete page
106 * @page: Page to write
110 * Some of this is copied from block_write_full_page() although we still
111 * call it to do most of the work.
114 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
116 struct inode *inode = page->mapping->host;
117 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
118 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
119 loff_t i_size = i_size_read(inode);
120 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
125 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
129 if (current->journal_info)
132 /* Is the page fully outside i_size? (truncate in progress) */
133 offset = i_size & (PAGE_CACHE_SIZE-1);
134 if (page->index > end_index || (page->index == end_index && !offset)) {
135 page->mapping->a_ops->invalidatepage(page, 0);
137 return 0; /* don't care */
140 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
141 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
144 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
147 error = block_write_full_page(page, get_block_noalloc, wbc);
150 gfs2_meta_cache_flush(ip);
154 redirty_page_for_writepage(wbc, page);
159 static int zero_readpage(struct page *page)
163 kaddr = kmap_atomic(page, KM_USER0);
164 memset(kaddr, 0, PAGE_CACHE_SIZE);
165 kunmap_atomic(page, KM_USER0);
167 SetPageUptodate(page);
173 * stuffed_readpage - Fill in a Linux page with stuffed file data
180 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
182 struct buffer_head *dibh;
186 /* Only the first page of a stuffed file might contain data */
187 if (unlikely(page->index))
188 return zero_readpage(page);
190 error = gfs2_meta_inode_buffer(ip, &dibh);
194 kaddr = kmap_atomic(page, KM_USER0);
195 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
197 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
198 kunmap_atomic(page, KM_USER0);
202 SetPageUptodate(page);
209 * gfs2_readpage - readpage with locking
210 * @file: The file to read a page for. N.B. This may be NULL if we are
211 * reading an internal file.
212 * @page: The page to read
217 static int gfs2_readpage(struct file *file, struct page *page)
219 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
220 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
221 struct gfs2_holder gh;
224 if (likely(file != &gfs2_internal_file_sentinal)) {
225 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
226 error = gfs2_glock_nq_m_atime(1, &gh);
231 if (gfs2_is_stuffed(ip)) {
232 error = stuffed_readpage(ip, page);
235 error = mpage_readpage(page, gfs2_get_block);
237 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
240 if (file != &gfs2_internal_file_sentinal) {
241 gfs2_glock_dq_m(1, &gh);
242 gfs2_holder_uninit(&gh);
248 if (file != &gfs2_internal_file_sentinal)
249 gfs2_holder_uninit(&gh);
254 * gfs2_readpages - Read a bunch of pages at once
257 * 1. This is only for readahead, so we can simply ignore any things
258 * which are slightly inconvenient (such as locking conflicts between
259 * the page lock and the glock) and return having done no I/O. Its
260 * obviously not something we'd want to do on too regular a basis.
261 * Any I/O we ignore at this time will be done via readpage later.
262 * 2. We have to handle stuffed files here too.
263 * 3. mpage_readpages() does most of the heavy lifting in the common case.
264 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
265 * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
266 * well as read-ahead.
268 static int gfs2_readpages(struct file *file, struct address_space *mapping,
269 struct list_head *pages, unsigned nr_pages)
271 struct inode *inode = mapping->host;
272 struct gfs2_inode *ip = GFS2_I(inode);
273 struct gfs2_sbd *sdp = GFS2_SB(inode);
274 struct gfs2_holder gh;
278 if (likely(file != &gfs2_internal_file_sentinal)) {
279 gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
280 LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh);
281 ret = gfs2_glock_nq_m_atime(1, &gh);
282 if (ret == GLR_TRYFAILED)
288 if (gfs2_is_stuffed(ip)) {
289 struct pagevec lru_pvec;
290 pagevec_init(&lru_pvec, 0);
291 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
292 struct page *page = list_entry(pages->prev, struct page, lru);
293 prefetchw(&page->flags);
294 list_del(&page->lru);
295 if (!add_to_page_cache(page, mapping,
296 page->index, GFP_KERNEL)) {
297 ret = stuffed_readpage(ip, page);
299 if (!pagevec_add(&lru_pvec, page))
300 __pagevec_lru_add(&lru_pvec);
302 page_cache_release(page);
305 pagevec_lru_add(&lru_pvec);
308 /* What we really want to do .... */
309 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
312 if (likely(file != &gfs2_internal_file_sentinal)) {
313 gfs2_glock_dq_m(1, &gh);
314 gfs2_holder_uninit(&gh);
317 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
323 /* unlock all pages, we can't do any I/O right now */
324 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
325 struct page *page = list_entry(pages->prev, struct page, lru);
326 list_del(&page->lru);
328 page_cache_release(page);
330 if (likely(file != &gfs2_internal_file_sentinal))
331 gfs2_holder_uninit(&gh);
336 * gfs2_prepare_write - Prepare to write a page to a file
337 * @file: The file to write to
338 * @page: The page which is to be prepared for writing
339 * @from: From (byte range within page)
340 * @to: To (byte range within page)
345 static int gfs2_prepare_write(struct file *file, struct page *page,
346 unsigned from, unsigned to)
348 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
349 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
350 unsigned int data_blocks, ind_blocks, rblocks;
353 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
354 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
355 struct gfs2_alloc *al;
357 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
358 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
362 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
364 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
369 if (alloc_required) {
370 al = gfs2_alloc_get(ip);
372 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
376 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
380 al->al_requested = data_blocks + ind_blocks;
381 error = gfs2_inplace_reserve(ip);
386 rblocks = RES_DINODE + ind_blocks;
387 if (gfs2_is_jdata(ip))
388 rblocks += data_blocks ? data_blocks : 1;
389 if (ind_blocks || data_blocks)
390 rblocks += RES_STATFS + RES_QUOTA;
392 error = gfs2_trans_begin(sdp, rblocks, 0);
396 if (gfs2_is_stuffed(ip)) {
397 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
398 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
402 } else if (!PageUptodate(page))
403 error = stuffed_readpage(ip, page);
408 error = block_prepare_write(page, from, to, gfs2_get_block);
413 if (alloc_required) {
414 gfs2_inplace_release(ip);
416 gfs2_quota_unlock(ip);
421 gfs2_glock_dq_m(1, &ip->i_gh);
423 gfs2_holder_uninit(&ip->i_gh);
430 * gfs2_commit_write - Commit write to a file
431 * @file: The file to write to
432 * @page: The page containing the data
433 * @from: From (byte range within page)
434 * @to: To (byte range within page)
439 static int gfs2_commit_write(struct file *file, struct page *page,
440 unsigned from, unsigned to)
442 struct inode *inode = page->mapping->host;
443 struct gfs2_inode *ip = GFS2_I(inode);
444 struct gfs2_sbd *sdp = GFS2_SB(inode);
445 int error = -EOPNOTSUPP;
446 struct buffer_head *dibh;
447 struct gfs2_alloc *al = &ip->i_alloc;;
449 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
452 error = gfs2_meta_inode_buffer(ip, &dibh);
456 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
458 if (gfs2_is_stuffed(ip)) {
462 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
464 kaddr = kmap_atomic(page, KM_USER0);
465 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
466 (char *)kaddr + from, to - from);
467 kunmap_atomic(page, KM_USER0);
469 SetPageUptodate(page);
471 if (inode->i_size < file_size)
472 i_size_write(inode, file_size);
474 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
476 gfs2_page_add_databufs(ip, page, from, to);
477 error = generic_commit_write(file, page, from, to);
482 if (ip->i_di.di_size < inode->i_size)
483 ip->i_di.di_size = inode->i_size;
485 gfs2_dinode_out(&ip->i_di, dibh->b_data);
488 if (al->al_requested) {
489 gfs2_inplace_release(ip);
490 gfs2_quota_unlock(ip);
493 gfs2_glock_dq_m(1, &ip->i_gh);
494 gfs2_holder_uninit(&ip->i_gh);
501 if (al->al_requested) {
502 gfs2_inplace_release(ip);
503 gfs2_quota_unlock(ip);
506 gfs2_glock_dq_m(1, &ip->i_gh);
507 gfs2_holder_uninit(&ip->i_gh);
509 ClearPageUptodate(page);
514 * gfs2_bmap - Block map function
515 * @mapping: Address space info
516 * @lblock: The block to map
518 * Returns: The disk address for the block or 0 on hole or error
521 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
523 struct gfs2_inode *ip = GFS2_I(mapping->host);
524 struct gfs2_holder i_gh;
528 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
532 if (!gfs2_is_stuffed(ip))
533 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
535 gfs2_glock_dq_uninit(&i_gh);
540 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
542 struct gfs2_bufdata *bd;
548 bh->b_private = NULL;
549 gfs2_log_unlock(sdp);
552 gfs2_log_unlock(sdp);
555 clear_buffer_dirty(bh);
557 clear_buffer_mapped(bh);
558 clear_buffer_req(bh);
559 clear_buffer_new(bh);
560 clear_buffer_delay(bh);
564 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
566 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
567 struct buffer_head *head, *bh, *next;
568 unsigned int curr_off = 0;
570 BUG_ON(!PageLocked(page));
571 if (!page_has_buffers(page))
574 bh = head = page_buffers(page);
576 unsigned int next_off = curr_off + bh->b_size;
577 next = bh->b_this_page;
579 if (offset <= curr_off)
580 discard_buffer(sdp, bh);
584 } while (bh != head);
587 try_to_release_page(page, 0);
592 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
593 loff_t offset, unsigned long nr_segs)
595 struct file *file = iocb->ki_filp;
596 struct inode *inode = file->f_mapping->host;
597 struct gfs2_inode *ip = GFS2_I(inode);
598 struct gfs2_holder gh;
602 * Shared lock, even though its write, since we do no allocation
603 * on this path. All we need change is atime.
605 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
606 rv = gfs2_glock_nq_m_atime(1, &gh);
611 * Should we return an error here? I can't see that O_DIRECT for
612 * a journaled file makes any sense. For now we'll silently fall
613 * back to buffered I/O, likewise we do the same for stuffed
614 * files since they are (a) small and (b) unaligned.
616 if (gfs2_is_jdata(ip))
619 if (gfs2_is_stuffed(ip))
622 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
623 iov, offset, nr_segs, gfs2_get_block,
624 NULL, DIO_OWN_LOCKING);
626 gfs2_glock_dq_m(1, &gh);
627 gfs2_holder_uninit(&gh);
635 * This is called with a shared lock already held for the read path.
636 * Currently, no locks are held when the write path is called.
638 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
639 const struct iovec *iov, loff_t offset,
640 unsigned long nr_segs)
642 struct file *file = iocb->ki_filp;
643 struct inode *inode = file->f_mapping->host;
644 struct gfs2_inode *ip = GFS2_I(inode);
645 struct gfs2_sbd *sdp = GFS2_SB(inode);
649 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
651 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
652 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
655 mutex_lock(&inode->i_mutex);
656 ret = __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
657 offset, nr_segs, gfs2_get_block, NULL,
659 mutex_unlock(&inode->i_mutex);
664 * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
665 * @bh: the buffer we're stuck on
669 static void stuck_releasepage(struct buffer_head *bh)
671 struct inode *inode = bh->b_page->mapping->host;
672 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
673 struct gfs2_bufdata *bd = bh->b_private;
674 struct gfs2_glock *gl;
676 fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
677 fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
678 (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
679 fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
680 fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
687 fs_warn(sdp, "gl = (%u, %llu)\n",
688 gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
690 fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
691 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
692 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
694 if (gl->gl_ops == &gfs2_inode_glops) {
695 struct gfs2_inode *ip = gl->gl_object;
701 fs_warn(sdp, "ip = %llu %llu\n",
702 (unsigned long long)ip->i_num.no_formal_ino,
703 (unsigned long long)ip->i_num.no_addr);
705 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
706 fs_warn(sdp, "ip->i_cache[%u] = %s\n",
707 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
712 * gfs2_aspace_releasepage - free the metadata associated with a page
713 * @page: the page that's being released
714 * @gfp_mask: passed from Linux VFS, ignored by us
716 * Call try_to_free_buffers() if the buffers in this page can be
722 int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
724 struct inode *aspace = page->mapping->host;
725 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
726 struct buffer_head *bh, *head;
727 struct gfs2_bufdata *bd;
730 if (!page_has_buffers(page))
733 head = bh = page_buffers(page);
737 while (atomic_read(&bh->b_count)) {
738 if (atomic_read(&aspace->i_writecount)) {
739 if (time_after_eq(jiffies, t +
740 gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
741 stuck_releasepage(bh);
752 gfs2_assert_warn(sdp, !buffer_pinned(bh));
756 gfs2_assert_warn(sdp, bd->bd_bh == bh);
757 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
758 gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
759 gfs2_assert_warn(sdp, !bd->bd_ail);
760 kmem_cache_free(gfs2_bufdata_cachep, bd);
761 bh->b_private = NULL;
764 bh = bh->b_this_page;
769 return try_to_free_buffers(page);
772 const struct address_space_operations gfs2_file_aops = {
773 .writepage = gfs2_writepage,
774 .readpage = gfs2_readpage,
775 .readpages = gfs2_readpages,
776 .sync_page = block_sync_page,
777 .prepare_write = gfs2_prepare_write,
778 .commit_write = gfs2_commit_write,
780 .invalidatepage = gfs2_invalidatepage,
781 .releasepage = gfs2_releasepage,
782 .direct_IO = gfs2_direct_IO,