ops_vm.c 3.7 KB

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