ops_vm.c 3.8 KB

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