ops_vm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 version 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 <linux/lm_interface.h>
  18. #include "gfs2.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 page *result;
  44. set_bit(GIF_PAGED, &ip->i_flags);
  45. result = filemap_nopage(area, address, type);
  46. if (result && result != NOPAGE_OOM)
  47. pfault_be_greedy(ip);
  48. return result;
  49. }
  50. static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
  51. {
  52. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  53. unsigned long index = page->index;
  54. u64 lblock = index << (PAGE_CACHE_SHIFT -
  55. sdp->sd_sb.sb_bsize_shift);
  56. unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
  57. struct gfs2_alloc *al;
  58. unsigned int data_blocks, ind_blocks;
  59. unsigned int x;
  60. int error;
  61. al = gfs2_alloc_get(ip);
  62. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  63. if (error)
  64. goto out;
  65. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  66. if (error)
  67. goto out_gunlock_q;
  68. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  69. al->al_requested = data_blocks + ind_blocks;
  70. error = gfs2_inplace_reserve(ip);
  71. if (error)
  72. goto out_gunlock_q;
  73. error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length +
  74. ind_blocks + RES_DINODE +
  75. RES_STATFS + RES_QUOTA, 0);
  76. if (error)
  77. goto out_ipres;
  78. if (gfs2_is_stuffed(ip)) {
  79. error = gfs2_unstuff_dinode(ip, NULL);
  80. if (error)
  81. goto out_trans;
  82. }
  83. for (x = 0; x < blocks; ) {
  84. u64 dblock;
  85. unsigned int extlen;
  86. int new = 1;
  87. error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
  88. if (error)
  89. goto out_trans;
  90. lblock += extlen;
  91. x += extlen;
  92. }
  93. gfs2_assert_warn(sdp, al->al_alloced);
  94. out_trans:
  95. gfs2_trans_end(sdp);
  96. out_ipres:
  97. gfs2_inplace_release(ip);
  98. out_gunlock_q:
  99. gfs2_quota_unlock(ip);
  100. out:
  101. gfs2_alloc_put(ip);
  102. return error;
  103. }
  104. static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area,
  105. unsigned long address, int *type)
  106. {
  107. struct file *file = area->vm_file;
  108. struct gfs2_file *gf = file->private_data;
  109. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  110. struct gfs2_holder i_gh;
  111. struct page *result = NULL;
  112. unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) +
  113. area->vm_pgoff;
  114. int alloc_required;
  115. int error;
  116. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  117. if (error)
  118. return NULL;
  119. set_bit(GIF_PAGED, &ip->i_flags);
  120. set_bit(GIF_SW_PAGED, &ip->i_flags);
  121. error = gfs2_write_alloc_required(ip, (u64)index << PAGE_CACHE_SHIFT,
  122. PAGE_CACHE_SIZE, &alloc_required);
  123. if (error)
  124. goto out;
  125. set_bit(GFF_EXLOCK, &gf->f_flags);
  126. result = filemap_nopage(area, address, type);
  127. clear_bit(GFF_EXLOCK, &gf->f_flags);
  128. if (!result || result == NOPAGE_OOM)
  129. goto out;
  130. if (alloc_required) {
  131. error = alloc_page_backing(ip, result);
  132. if (error) {
  133. page_cache_release(result);
  134. result = NULL;
  135. goto out;
  136. }
  137. set_page_dirty(result);
  138. }
  139. pfault_be_greedy(ip);
  140. out:
  141. gfs2_glock_dq_uninit(&i_gh);
  142. return result;
  143. }
  144. struct vm_operations_struct gfs2_vm_ops_private = {
  145. .nopage = gfs2_private_nopage,
  146. };
  147. struct vm_operations_struct gfs2_vm_ops_sharewrite = {
  148. .nopage = gfs2_sharewrite_nopage,
  149. };