ops_vm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/mm.h>
  15. #include <linux/pagemap.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 "glock.h"
  22. #include "inode.h"
  23. #include "ops_vm.h"
  24. #include "quota.h"
  25. #include "rgrp.h"
  26. #include "trans.h"
  27. #include "util.h"
  28. static void pfault_be_greedy(struct gfs2_inode *ip)
  29. {
  30. unsigned int time;
  31. spin_lock(&ip->i_spin);
  32. time = ip->i_greedy;
  33. ip->i_last_pfault = jiffies;
  34. spin_unlock(&ip->i_spin);
  35. igrab(&ip->i_inode);
  36. if (gfs2_glock_be_greedy(ip->i_gl, time))
  37. iput(&ip->i_inode);
  38. }
  39. static struct page *gfs2_private_nopage(struct vm_area_struct *area,
  40. unsigned long address, int *type)
  41. {
  42. struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host);
  43. struct gfs2_holder i_gh;
  44. struct page *result;
  45. int error;
  46. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  47. if (error)
  48. return NULL;
  49. set_bit(GIF_PAGED, &ip->i_flags);
  50. result = filemap_nopage(area, address, type);
  51. if (result && result != NOPAGE_OOM)
  52. pfault_be_greedy(ip);
  53. gfs2_glock_dq_uninit(&i_gh);
  54. return result;
  55. }
  56. static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
  57. {
  58. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  59. unsigned long index = page->index;
  60. uint64_t lblock = index << (PAGE_CACHE_SHIFT -
  61. sdp->sd_sb.sb_bsize_shift);
  62. unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
  63. struct gfs2_alloc *al;
  64. unsigned int data_blocks, ind_blocks;
  65. unsigned int x;
  66. int error;
  67. al = gfs2_alloc_get(ip);
  68. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  69. if (error)
  70. goto out;
  71. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  72. if (error)
  73. goto out_gunlock_q;
  74. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  75. al->al_requested = data_blocks + ind_blocks;
  76. error = gfs2_inplace_reserve(ip);
  77. if (error)
  78. goto out_gunlock_q;
  79. error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length +
  80. ind_blocks + RES_DINODE +
  81. RES_STATFS + RES_QUOTA, 0);
  82. if (error)
  83. goto out_ipres;
  84. if (gfs2_is_stuffed(ip)) {
  85. error = gfs2_unstuff_dinode(ip, NULL);
  86. if (error)
  87. goto out_trans;
  88. }
  89. for (x = 0; x < blocks; ) {
  90. uint64_t dblock;
  91. unsigned int extlen;
  92. int new = 1;
  93. error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
  94. if (error)
  95. goto out_trans;
  96. lblock += extlen;
  97. x += extlen;
  98. }
  99. gfs2_assert_warn(sdp, al->al_alloced);
  100. out_trans:
  101. gfs2_trans_end(sdp);
  102. out_ipres:
  103. gfs2_inplace_release(ip);
  104. out_gunlock_q:
  105. gfs2_quota_unlock(ip);
  106. out:
  107. gfs2_alloc_put(ip);
  108. return error;
  109. }
  110. static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area,
  111. unsigned long address, int *type)
  112. {
  113. struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host);
  114. struct gfs2_holder i_gh;
  115. struct page *result = NULL;
  116. unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) +
  117. area->vm_pgoff;
  118. int alloc_required;
  119. int error;
  120. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  121. if (error)
  122. return NULL;
  123. set_bit(GIF_PAGED, &ip->i_flags);
  124. set_bit(GIF_SW_PAGED, &ip->i_flags);
  125. error = gfs2_write_alloc_required(ip,
  126. (uint64_t)index << PAGE_CACHE_SHIFT,
  127. PAGE_CACHE_SIZE, &alloc_required);
  128. if (error)
  129. goto out;
  130. result = filemap_nopage(area, address, type);
  131. if (!result || result == NOPAGE_OOM)
  132. goto out;
  133. if (alloc_required) {
  134. error = alloc_page_backing(ip, result);
  135. if (error) {
  136. page_cache_release(result);
  137. result = NULL;
  138. goto out;
  139. }
  140. set_page_dirty(result);
  141. }
  142. pfault_be_greedy(ip);
  143. out:
  144. gfs2_glock_dq_uninit(&i_gh);
  145. return result;
  146. }
  147. struct vm_operations_struct gfs2_vm_ops_private = {
  148. .nopage = gfs2_private_nopage,
  149. };
  150. struct vm_operations_struct gfs2_vm_ops_sharewrite = {
  151. .nopage = gfs2_sharewrite_nopage,
  152. };