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"
38 static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
39 unsigned int from, unsigned int to)
41 struct buffer_head *head = page_buffers(page);
42 unsigned int bsize = head->b_size;
43 struct buffer_head *bh;
44 unsigned int start, end;
46 for (bh = head, start = 0; bh != head || !start;
47 bh = bh->b_this_page, start = end) {
49 if (end <= from || start >= to)
51 gfs2_trans_add_bh(ip->i_gl, bh, 0);
56 * gfs2_get_block - Fills in a buffer head with details about a block
58 * @lblock: The block number to look up
59 * @bh_result: The buffer head to return the result in
60 * @create: Non-zero if we may add block to the file
65 int gfs2_get_block(struct inode *inode, sector_t lblock,
66 struct buffer_head *bh_result, int create)
73 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
80 map_bh(bh_result, inode->i_sb, dblock);
82 set_buffer_new(bh_result);
84 set_buffer_boundary(bh_result);
90 * get_block_noalloc - Fills in a buffer head with details about a block
92 * @lblock: The block number to look up
93 * @bh_result: The buffer head to return the result in
94 * @create: Non-zero if we may add block to the file
99 static int get_block_noalloc(struct inode *inode, sector_t lblock,
100 struct buffer_head *bh_result, int create)
107 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
112 map_bh(bh_result, inode->i_sb, dblock);
113 else if (gfs2_assert_withdraw(GFS2_SB(inode), !create))
116 set_buffer_boundary(bh_result);
122 * gfs2_writepage - Write complete page
123 * @page: Page to write
127 * Some of this is copied from block_write_full_page() although we still
128 * call it to do most of the work.
131 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
133 struct inode *inode = page->mapping->host;
134 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
135 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
136 loff_t i_size = i_size_read(inode);
137 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
142 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
146 if (current->journal_info)
149 /* Is the page fully outside i_size? (truncate in progress) */
150 offset = i_size & (PAGE_CACHE_SIZE-1);
151 if (page->index > end_index || (page->index == end_index && !offset)) {
152 page->mapping->a_ops->invalidatepage(page, 0);
154 return 0; /* don't care */
157 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
158 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
161 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
164 error = block_write_full_page(page, get_block_noalloc, wbc);
167 gfs2_meta_cache_flush(ip);
171 redirty_page_for_writepage(wbc, page);
176 static int zero_readpage(struct page *page)
180 kaddr = kmap_atomic(page, KM_USER0);
181 memset(kaddr, 0, PAGE_CACHE_SIZE);
182 kunmap_atomic(page, KM_USER0);
184 SetPageUptodate(page);
190 * stuffed_readpage - Fill in a Linux page with stuffed file data
197 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
199 struct buffer_head *dibh;
203 /* Only the first page of a stuffed file might contain data */
204 if (unlikely(page->index))
205 return zero_readpage(page);
207 error = gfs2_meta_inode_buffer(ip, &dibh);
211 kaddr = kmap_atomic(page, KM_USER0);
212 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
214 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
215 kunmap_atomic(page, KM_USER0);
219 SetPageUptodate(page);
226 * gfs2_readpage - readpage with locking
227 * @file: The file to read a page for. N.B. This may be NULL if we are
228 * reading an internal file.
229 * @page: The page to read
234 static int gfs2_readpage(struct file *file, struct page *page)
236 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
237 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
238 struct gfs2_holder gh;
241 if (likely(file != &gfs2_internal_file_sentinal)) {
242 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
243 error = gfs2_glock_nq_m_atime(1, &gh);
248 if (gfs2_is_stuffed(ip)) {
249 error = stuffed_readpage(ip, page);
252 error = mpage_readpage(page, gfs2_get_block);
254 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
257 if (file != &gfs2_internal_file_sentinal) {
258 gfs2_glock_dq_m(1, &gh);
259 gfs2_holder_uninit(&gh);
265 if (file != &gfs2_internal_file_sentinal)
266 gfs2_holder_uninit(&gh);
271 * gfs2_readpages - Read a bunch of pages at once
274 * 1. This is only for readahead, so we can simply ignore any things
275 * which are slightly inconvenient (such as locking conflicts between
276 * the page lock and the glock) and return having done no I/O. Its
277 * obviously not something we'd want to do on too regular a basis.
278 * Any I/O we ignore at this time will be done via readpage later.
279 * 2. We have to handle stuffed files here too.
280 * 3. mpage_readpages() does most of the heavy lifting in the common case.
281 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
282 * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
283 * well as read-ahead.
285 static int gfs2_readpages(struct file *file, struct address_space *mapping,
286 struct list_head *pages, unsigned nr_pages)
288 struct inode *inode = mapping->host;
289 struct gfs2_inode *ip = GFS2_I(inode);
290 struct gfs2_sbd *sdp = GFS2_SB(inode);
291 struct gfs2_holder gh;
295 if (likely(file != &gfs2_internal_file_sentinal)) {
296 gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
297 LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh);
298 ret = gfs2_glock_nq_m_atime(1, &gh);
299 if (ret == GLR_TRYFAILED)
305 if (gfs2_is_stuffed(ip)) {
306 struct pagevec lru_pvec;
307 pagevec_init(&lru_pvec, 0);
308 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
309 struct page *page = list_entry(pages->prev, struct page, lru);
310 prefetchw(&page->flags);
311 list_del(&page->lru);
312 if (!add_to_page_cache(page, mapping,
313 page->index, GFP_KERNEL)) {
314 ret = stuffed_readpage(ip, page);
316 if (!pagevec_add(&lru_pvec, page))
317 __pagevec_lru_add(&lru_pvec);
319 page_cache_release(page);
322 pagevec_lru_add(&lru_pvec);
325 /* What we really want to do .... */
326 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
329 if (likely(file != &gfs2_internal_file_sentinal)) {
330 gfs2_glock_dq_m(1, &gh);
331 gfs2_holder_uninit(&gh);
334 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
340 /* unlock all pages, we can't do any I/O right now */
341 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
342 struct page *page = list_entry(pages->prev, struct page, lru);
343 list_del(&page->lru);
345 page_cache_release(page);
347 if (likely(file != &gfs2_internal_file_sentinal))
348 gfs2_holder_uninit(&gh);
353 * gfs2_prepare_write - Prepare to write a page to a file
354 * @file: The file to write to
355 * @page: The page which is to be prepared for writing
356 * @from: From (byte range within page)
357 * @to: To (byte range within page)
362 static int gfs2_prepare_write(struct file *file, struct page *page,
363 unsigned from, unsigned to)
365 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
366 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
367 unsigned int data_blocks, ind_blocks, rblocks;
370 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
371 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
372 struct gfs2_alloc *al;
374 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
375 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
379 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
381 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
386 if (alloc_required) {
387 al = gfs2_alloc_get(ip);
389 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
393 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
397 al->al_requested = data_blocks + ind_blocks;
398 error = gfs2_inplace_reserve(ip);
403 rblocks = RES_DINODE + ind_blocks;
404 if (gfs2_is_jdata(ip))
405 rblocks += data_blocks ? data_blocks : 1;
406 if (ind_blocks || data_blocks)
407 rblocks += RES_STATFS + RES_QUOTA;
409 error = gfs2_trans_begin(sdp, rblocks, 0);
413 if (gfs2_is_stuffed(ip)) {
414 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
415 error = gfs2_unstuff_dinode(ip, page);
418 } else if (!PageUptodate(page))
419 error = stuffed_readpage(ip, page);
424 error = block_prepare_write(page, from, to, gfs2_get_block);
429 if (alloc_required) {
430 gfs2_inplace_release(ip);
432 gfs2_quota_unlock(ip);
437 gfs2_glock_dq_m(1, &ip->i_gh);
439 gfs2_holder_uninit(&ip->i_gh);
446 * gfs2_commit_write - Commit write to a file
447 * @file: The file to write to
448 * @page: The page containing the data
449 * @from: From (byte range within page)
450 * @to: To (byte range within page)
455 static int gfs2_commit_write(struct file *file, struct page *page,
456 unsigned from, unsigned to)
458 struct inode *inode = page->mapping->host;
459 struct gfs2_inode *ip = GFS2_I(inode);
460 struct gfs2_sbd *sdp = GFS2_SB(inode);
461 int error = -EOPNOTSUPP;
462 struct buffer_head *dibh;
463 struct gfs2_alloc *al = &ip->i_alloc;;
465 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
468 error = gfs2_meta_inode_buffer(ip, &dibh);
472 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
474 if (gfs2_is_stuffed(ip)) {
478 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
480 kaddr = kmap_atomic(page, KM_USER0);
481 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
482 (char *)kaddr + from, to - from);
483 kunmap_atomic(page, KM_USER0);
485 SetPageUptodate(page);
487 if (inode->i_size < file_size)
488 i_size_write(inode, file_size);
490 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
492 gfs2_page_add_databufs(ip, page, from, to);
493 error = generic_commit_write(file, page, from, to);
498 if (ip->i_di.di_size < inode->i_size)
499 ip->i_di.di_size = inode->i_size;
501 gfs2_dinode_out(&ip->i_di, dibh->b_data);
504 if (al->al_requested) {
505 gfs2_inplace_release(ip);
506 gfs2_quota_unlock(ip);
509 gfs2_glock_dq_m(1, &ip->i_gh);
510 gfs2_holder_uninit(&ip->i_gh);
517 if (al->al_requested) {
518 gfs2_inplace_release(ip);
519 gfs2_quota_unlock(ip);
522 gfs2_glock_dq_m(1, &ip->i_gh);
523 gfs2_holder_uninit(&ip->i_gh);
525 ClearPageUptodate(page);
530 * gfs2_bmap - Block map function
531 * @mapping: Address space info
532 * @lblock: The block to map
534 * Returns: The disk address for the block or 0 on hole or error
537 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
539 struct gfs2_inode *ip = GFS2_I(mapping->host);
540 struct gfs2_holder i_gh;
544 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
548 if (!gfs2_is_stuffed(ip))
549 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
551 gfs2_glock_dq_uninit(&i_gh);
556 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
558 struct gfs2_bufdata *bd;
564 bh->b_private = NULL;
565 gfs2_log_unlock(sdp);
568 gfs2_log_unlock(sdp);
571 clear_buffer_dirty(bh);
573 clear_buffer_mapped(bh);
574 clear_buffer_req(bh);
575 clear_buffer_new(bh);
576 clear_buffer_delay(bh);
580 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
582 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
583 struct buffer_head *head, *bh, *next;
584 unsigned int curr_off = 0;
586 BUG_ON(!PageLocked(page));
587 if (!page_has_buffers(page))
590 bh = head = page_buffers(page);
592 unsigned int next_off = curr_off + bh->b_size;
593 next = bh->b_this_page;
595 if (offset <= curr_off)
596 discard_buffer(sdp, bh);
600 } while (bh != head);
603 try_to_release_page(page, 0);
608 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
609 const struct iovec *iov, loff_t offset,
610 unsigned long nr_segs)
612 struct file *file = iocb->ki_filp;
613 struct inode *inode = file->f_mapping->host;
614 struct gfs2_inode *ip = GFS2_I(inode);
615 struct gfs2_holder gh;
619 mutex_lock(&inode->i_mutex);
621 * Shared lock, even if its a write, since we do no allocation
622 * on this path. All we need change is atime.
624 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
625 rv = gfs2_glock_nq_m_atime(1, &gh);
629 if (offset > i_size_read(inode))
633 * Should we return an error here? I can't see that O_DIRECT for
634 * a journaled file makes any sense. For now we'll silently fall
635 * back to buffered I/O, likewise we do the same for stuffed
636 * files since they are (a) small and (b) unaligned.
638 if (gfs2_is_jdata(ip))
641 if (gfs2_is_stuffed(ip))
644 rv = blockdev_direct_IO_own_locking(rw, iocb, inode,
646 iov, offset, nr_segs,
647 gfs2_get_block, NULL);
649 gfs2_glock_dq_m(1, &gh);
650 gfs2_holder_uninit(&gh);
652 mutex_unlock(&inode->i_mutex);
658 * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
659 * @bh: the buffer we're stuck on
663 static void stuck_releasepage(struct buffer_head *bh)
665 struct inode *inode = bh->b_page->mapping->host;
666 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
667 struct gfs2_bufdata *bd = bh->b_private;
668 struct gfs2_glock *gl;
670 fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
671 fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
672 (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
673 fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
674 fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
681 fs_warn(sdp, "gl = (%u, %llu)\n",
682 gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
684 fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
685 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
686 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
688 if (gl->gl_ops == &gfs2_inode_glops) {
689 struct gfs2_inode *ip = gl->gl_object;
695 fs_warn(sdp, "ip = %llu %llu\n",
696 (unsigned long long)ip->i_num.no_formal_ino,
697 (unsigned long long)ip->i_num.no_addr);
699 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
700 fs_warn(sdp, "ip->i_cache[%u] = %s\n",
701 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
706 * gfs2_aspace_releasepage - free the metadata associated with a page
707 * @page: the page that's being released
708 * @gfp_mask: passed from Linux VFS, ignored by us
710 * Call try_to_free_buffers() if the buffers in this page can be
716 int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
718 struct inode *aspace = page->mapping->host;
719 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
720 struct buffer_head *bh, *head;
721 struct gfs2_bufdata *bd;
724 if (!page_has_buffers(page))
727 head = bh = page_buffers(page);
731 while (atomic_read(&bh->b_count)) {
732 if (atomic_read(&aspace->i_writecount)) {
733 if (time_after_eq(jiffies, t +
734 gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
735 stuck_releasepage(bh);
746 gfs2_assert_warn(sdp, !buffer_pinned(bh));
750 gfs2_assert_warn(sdp, bd->bd_bh == bh);
751 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
752 gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
753 gfs2_assert_warn(sdp, !bd->bd_ail);
754 kmem_cache_free(gfs2_bufdata_cachep, bd);
755 bh->b_private = NULL;
758 bh = bh->b_this_page;
763 return try_to_free_buffers(page);
766 const struct address_space_operations gfs2_file_aops = {
767 .writepage = gfs2_writepage,
768 .readpage = gfs2_readpage,
769 .readpages = gfs2_readpages,
770 .sync_page = block_sync_page,
771 .prepare_write = gfs2_prepare_write,
772 .commit_write = gfs2_commit_write,
774 .invalidatepage = gfs2_invalidatepage,
775 .releasepage = gfs2_releasepage,
776 .direct_IO = gfs2_direct_IO,