page.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  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.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mm.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include "gfs2.h"
  18. #include "lm_interface.h"
  19. #include "incore.h"
  20. #include "bmap.h"
  21. #include "inode.h"
  22. #include "page.h"
  23. #include "trans.h"
  24. #include "ops_address.h"
  25. #include "util.h"
  26. /**
  27. * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
  28. * @gl: the glock
  29. *
  30. */
  31. void gfs2_pte_inval(struct gfs2_glock *gl)
  32. {
  33. struct gfs2_inode *ip;
  34. struct inode *inode;
  35. ip = gl->gl_object;
  36. inode = &ip->i_inode;
  37. if (!ip || !S_ISREG(ip->i_di.di_mode))
  38. return;
  39. if (!test_bit(GIF_PAGED, &ip->i_flags))
  40. return;
  41. unmap_shared_mapping_range(inode->i_mapping, 0, 0);
  42. if (test_bit(GIF_SW_PAGED, &ip->i_flags))
  43. set_bit(GLF_DIRTY, &gl->gl_flags);
  44. clear_bit(GIF_SW_PAGED, &ip->i_flags);
  45. }
  46. /**
  47. * gfs2_page_inval - Invalidate all pages associated with a glock
  48. * @gl: the glock
  49. *
  50. */
  51. void gfs2_page_inval(struct gfs2_glock *gl)
  52. {
  53. struct gfs2_inode *ip;
  54. struct inode *inode;
  55. ip = gl->gl_object;
  56. inode = &ip->i_inode;
  57. if (!ip || !S_ISREG(ip->i_di.di_mode))
  58. return;
  59. truncate_inode_pages(inode->i_mapping, 0);
  60. gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), !inode->i_mapping->nrpages);
  61. clear_bit(GIF_PAGED, &ip->i_flags);
  62. }
  63. /**
  64. * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
  65. * @gl: the glock
  66. * @flags: DIO_START | DIO_WAIT
  67. *
  68. * Syncs data (not metadata) for a regular file.
  69. * No-op for all other types.
  70. */
  71. void gfs2_page_sync(struct gfs2_glock *gl, int flags)
  72. {
  73. struct gfs2_inode *ip;
  74. struct inode *inode;
  75. struct address_space *mapping;
  76. int error = 0;
  77. ip = gl->gl_object;
  78. inode = &ip->i_inode;
  79. if (!ip || !S_ISREG(ip->i_di.di_mode))
  80. return;
  81. mapping = inode->i_mapping;
  82. if (flags & DIO_START)
  83. filemap_fdatawrite(mapping);
  84. if (!error && (flags & DIO_WAIT))
  85. error = filemap_fdatawait(mapping);
  86. /* Put back any errors cleared by filemap_fdatawait()
  87. so they can be caught by someone who can pass them
  88. up to user space. */
  89. if (error == -ENOSPC)
  90. set_bit(AS_ENOSPC, &mapping->flags);
  91. else if (error)
  92. set_bit(AS_EIO, &mapping->flags);
  93. }
  94. /**
  95. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  96. *
  97. * This is partly borrowed from ext3.
  98. */
  99. int gfs2_block_truncate_page(struct address_space *mapping)
  100. {
  101. struct inode *inode = mapping->host;
  102. struct gfs2_inode *ip = GFS2_I(inode);
  103. struct gfs2_sbd *sdp = GFS2_SB(inode);
  104. loff_t from = inode->i_size;
  105. unsigned long index = from >> PAGE_CACHE_SHIFT;
  106. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  107. unsigned blocksize, iblock, length, pos;
  108. struct buffer_head *bh;
  109. struct page *page;
  110. void *kaddr;
  111. int err;
  112. page = grab_cache_page(mapping, index);
  113. if (!page)
  114. return 0;
  115. blocksize = inode->i_sb->s_blocksize;
  116. length = blocksize - (offset & (blocksize - 1));
  117. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  118. if (!page_has_buffers(page))
  119. create_empty_buffers(page, blocksize, 0);
  120. /* Find the buffer that contains "offset" */
  121. bh = page_buffers(page);
  122. pos = blocksize;
  123. while (offset >= pos) {
  124. bh = bh->b_this_page;
  125. iblock++;
  126. pos += blocksize;
  127. }
  128. err = 0;
  129. if (!buffer_mapped(bh)) {
  130. gfs2_get_block(inode, iblock, bh, 0);
  131. /* unmapped? It's a hole - nothing to do */
  132. if (!buffer_mapped(bh))
  133. goto unlock;
  134. }
  135. /* Ok, it's mapped. Make sure it's up-to-date */
  136. if (PageUptodate(page))
  137. set_buffer_uptodate(bh);
  138. if (!buffer_uptodate(bh)) {
  139. err = -EIO;
  140. ll_rw_block(READ, 1, &bh);
  141. wait_on_buffer(bh);
  142. /* Uhhuh. Read error. Complain and punt. */
  143. if (!buffer_uptodate(bh))
  144. goto unlock;
  145. }
  146. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  147. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  148. kaddr = kmap_atomic(page, KM_USER0);
  149. memset(kaddr + offset, 0, length);
  150. flush_dcache_page(page);
  151. kunmap_atomic(kaddr, KM_USER0);
  152. unlock:
  153. unlock_page(page);
  154. page_cache_release(page);
  155. return err;
  156. }
  157. void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  158. unsigned int from, unsigned int to)
  159. {
  160. struct buffer_head *head = page_buffers(page);
  161. unsigned int bsize = head->b_size;
  162. struct buffer_head *bh;
  163. unsigned int start, end;
  164. for (bh = head, start = 0; bh != head || !start;
  165. bh = bh->b_this_page, start = end) {
  166. end = start + bsize;
  167. if (end <= from || start >= to)
  168. continue;
  169. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  170. }
  171. }