ops_vm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/mm.h>
  15. #include <linux/pagemap.h>
  16. #include <asm/semaphore.h>
  17. #include "gfs2.h"
  18. #include "bmap.h"
  19. #include "glock.h"
  20. #include "inode.h"
  21. #include "ops_vm.h"
  22. #include "page.h"
  23. #include "quota.h"
  24. #include "rgrp.h"
  25. #include "trans.h"
  26. static void pfault_be_greedy(struct gfs2_inode *ip)
  27. {
  28. unsigned int time;
  29. spin_lock(&ip->i_spin);
  30. time = ip->i_greedy;
  31. ip->i_last_pfault = jiffies;
  32. spin_unlock(&ip->i_spin);
  33. gfs2_inode_hold(ip);
  34. if (gfs2_glock_be_greedy(ip->i_gl, time))
  35. gfs2_inode_put(ip);
  36. }
  37. static struct page *gfs2_private_nopage(struct vm_area_struct *area,
  38. unsigned long address, int *type)
  39. {
  40. struct gfs2_inode *ip = get_v2ip(area->vm_file->f_mapping->host);
  41. struct gfs2_holder i_gh;
  42. struct page *result;
  43. int error;
  44. atomic_inc(&ip->i_sbd->sd_ops_vm);
  45. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  46. if (error)
  47. return NULL;
  48. set_bit(GIF_PAGED, &ip->i_flags);
  49. result = filemap_nopage(area, address, type);
  50. if (result && result != NOPAGE_OOM)
  51. pfault_be_greedy(ip);
  52. gfs2_glock_dq_uninit(&i_gh);
  53. return result;
  54. }
  55. static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
  56. {
  57. struct gfs2_sbd *sdp = ip->i_sbd;
  58. unsigned long index = page->index;
  59. uint64_t lblock = index << (PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift);
  60. unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
  61. struct gfs2_alloc *al;
  62. unsigned int data_blocks, ind_blocks;
  63. unsigned int x;
  64. int error;
  65. al = gfs2_alloc_get(ip);
  66. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  67. if (error)
  68. goto out;
  69. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  70. if (error)
  71. goto out_gunlock_q;
  72. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE,
  73. &data_blocks, &ind_blocks);
  74. al->al_requested = data_blocks + ind_blocks;
  75. error = gfs2_inplace_reserve(ip);
  76. if (error)
  77. goto out_gunlock_q;
  78. error = gfs2_trans_begin(sdp,
  79. 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, gfs2_unstuffer_page, 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_block_map(ip, 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 = get_v2ip(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) + area->vm_pgoff;
  117. int alloc_required;
  118. int error;
  119. atomic_inc(&ip->i_sbd->sd_ops_vm);
  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. };