page.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 <asm/semaphore.h>
  18. #include "gfs2.h"
  19. #include "lm_interface.h"
  20. #include "incore.h"
  21. #include "bmap.h"
  22. #include "inode.h"
  23. #include "page.h"
  24. #include "trans.h"
  25. #include "ops_address.h"
  26. #include "util.h"
  27. /**
  28. * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
  29. * @gl: the glock
  30. *
  31. */
  32. void gfs2_pte_inval(struct gfs2_glock *gl)
  33. {
  34. struct gfs2_inode *ip;
  35. struct inode *inode;
  36. ip = gl->gl_object;
  37. if (!ip || !S_ISREG(ip->i_di.di_mode))
  38. return;
  39. if (!test_bit(GIF_PAGED, &ip->i_flags))
  40. return;
  41. inode = gfs2_ip2v_lookup(ip);
  42. if (inode) {
  43. unmap_shared_mapping_range(inode->i_mapping, 0, 0);
  44. iput(inode);
  45. if (test_bit(GIF_SW_PAGED, &ip->i_flags))
  46. set_bit(GLF_DIRTY, &gl->gl_flags);
  47. }
  48. clear_bit(GIF_SW_PAGED, &ip->i_flags);
  49. }
  50. /**
  51. * gfs2_page_inval - Invalidate all pages associated with a glock
  52. * @gl: the glock
  53. *
  54. */
  55. void gfs2_page_inval(struct gfs2_glock *gl)
  56. {
  57. struct gfs2_inode *ip;
  58. struct inode *inode;
  59. ip = gl->gl_object;
  60. if (!ip || !S_ISREG(ip->i_di.di_mode))
  61. return;
  62. inode = gfs2_ip2v_lookup(ip);
  63. if (inode) {
  64. struct address_space *mapping = inode->i_mapping;
  65. truncate_inode_pages(mapping, 0);
  66. gfs2_assert_withdraw(ip->i_sbd, !mapping->nrpages);
  67. iput(inode);
  68. }
  69. clear_bit(GIF_PAGED, &ip->i_flags);
  70. }
  71. /**
  72. * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
  73. * @gl: the glock
  74. * @flags: DIO_START | DIO_WAIT
  75. *
  76. * Syncs data (not metadata) for a regular file.
  77. * No-op for all other types.
  78. */
  79. void gfs2_page_sync(struct gfs2_glock *gl, int flags)
  80. {
  81. struct gfs2_inode *ip;
  82. struct inode *inode;
  83. ip = gl->gl_object;
  84. if (!ip || !S_ISREG(ip->i_di.di_mode))
  85. return;
  86. inode = gfs2_ip2v_lookup(ip);
  87. if (inode) {
  88. struct address_space *mapping = inode->i_mapping;
  89. int error = 0;
  90. if (flags & DIO_START)
  91. filemap_fdatawrite(mapping);
  92. if (!error && (flags & DIO_WAIT))
  93. error = filemap_fdatawait(mapping);
  94. /* Put back any errors cleared by filemap_fdatawait()
  95. so they can be caught by someone who can pass them
  96. up to user space. */
  97. if (error == -ENOSPC)
  98. set_bit(AS_ENOSPC, &mapping->flags);
  99. else if (error)
  100. set_bit(AS_EIO, &mapping->flags);
  101. iput(inode);
  102. }
  103. }
  104. /**
  105. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  106. * @ip: the inode
  107. * @dibh: the dinode buffer
  108. * @block: the block number that was allocated
  109. * @private: any locked page held by the caller process
  110. *
  111. * Returns: errno
  112. */
  113. int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  114. uint64_t block, void *private)
  115. {
  116. struct gfs2_sbd *sdp = ip->i_sbd;
  117. struct inode *inode = ip->i_vnode;
  118. struct page *page = (struct page *)private;
  119. struct buffer_head *bh;
  120. int release = 0;
  121. if (!page || page->index) {
  122. page = grab_cache_page(inode->i_mapping, 0);
  123. if (!page)
  124. return -ENOMEM;
  125. release = 1;
  126. }
  127. if (!PageUptodate(page)) {
  128. void *kaddr = kmap(page);
  129. memcpy(kaddr,
  130. dibh->b_data + sizeof(struct gfs2_dinode),
  131. ip->i_di.di_size);
  132. memset(kaddr + ip->i_di.di_size,
  133. 0,
  134. PAGE_CACHE_SIZE - ip->i_di.di_size);
  135. kunmap(page);
  136. SetPageUptodate(page);
  137. }
  138. if (!page_has_buffers(page))
  139. create_empty_buffers(page, 1 << inode->i_blkbits,
  140. (1 << BH_Uptodate));
  141. bh = page_buffers(page);
  142. if (!buffer_mapped(bh))
  143. map_bh(bh, inode->i_sb, block);
  144. set_buffer_uptodate(bh);
  145. if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED) || gfs2_is_jdata(ip))
  146. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  147. mark_buffer_dirty(bh);
  148. if (release) {
  149. unlock_page(page);
  150. page_cache_release(page);
  151. }
  152. return 0;
  153. }
  154. /**
  155. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  156. *
  157. * This is partly borrowed from ext3.
  158. */
  159. int gfs2_block_truncate_page(struct address_space *mapping)
  160. {
  161. struct inode *inode = mapping->host;
  162. struct gfs2_inode *ip = inode->u.generic_ip;
  163. struct gfs2_sbd *sdp = ip->i_sbd;
  164. loff_t from = inode->i_size;
  165. unsigned long index = from >> PAGE_CACHE_SHIFT;
  166. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  167. unsigned blocksize, iblock, length, pos;
  168. struct buffer_head *bh;
  169. struct page *page;
  170. void *kaddr;
  171. int err;
  172. page = grab_cache_page(mapping, index);
  173. if (!page)
  174. return 0;
  175. blocksize = inode->i_sb->s_blocksize;
  176. length = blocksize - (offset & (blocksize - 1));
  177. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  178. if (!page_has_buffers(page))
  179. create_empty_buffers(page, blocksize, 0);
  180. /* Find the buffer that contains "offset" */
  181. bh = page_buffers(page);
  182. pos = blocksize;
  183. while (offset >= pos) {
  184. bh = bh->b_this_page;
  185. iblock++;
  186. pos += blocksize;
  187. }
  188. err = 0;
  189. if (!buffer_mapped(bh)) {
  190. gfs2_get_block(inode, iblock, bh, 0);
  191. /* unmapped? It's a hole - nothing to do */
  192. if (!buffer_mapped(bh))
  193. goto unlock;
  194. }
  195. /* Ok, it's mapped. Make sure it's up-to-date */
  196. if (PageUptodate(page))
  197. set_buffer_uptodate(bh);
  198. if (!buffer_uptodate(bh)) {
  199. err = -EIO;
  200. ll_rw_block(READ, 1, &bh);
  201. wait_on_buffer(bh);
  202. /* Uhhuh. Read error. Complain and punt. */
  203. if (!buffer_uptodate(bh))
  204. goto unlock;
  205. }
  206. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  207. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  208. kaddr = kmap_atomic(page, KM_USER0);
  209. memset(kaddr + offset, 0, length);
  210. flush_dcache_page(page);
  211. kunmap_atomic(kaddr, KM_USER0);
  212. unlock:
  213. unlock_page(page);
  214. page_cache_release(page);
  215. return err;
  216. }
  217. void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  218. unsigned int from, unsigned int to)
  219. {
  220. struct buffer_head *head = page_buffers(page);
  221. unsigned int bsize = head->b_size;
  222. struct buffer_head *bh;
  223. unsigned int start, end;
  224. for (bh = head, start = 0;
  225. bh != head || !start;
  226. bh = bh->b_this_page, start = end) {
  227. end = start + bsize;
  228. if (end <= from || start >= to)
  229. continue;
  230. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  231. }
  232. }