]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/gfs2/ops_vm.c
mm: fault feedback #1
[net-next-2.6.git] / fs / gfs2 / ops_vm.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
b3b94faa
DT
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>
5c676f6d 16#include <linux/gfs2_ondisk.h>
7d308590 17#include <linux/lm_interface.h>
b3b94faa
DT
18
19#include "gfs2.h"
5c676f6d 20#include "incore.h"
b3b94faa
DT
21#include "bmap.h"
22#include "glock.h"
23#include "inode.h"
24#include "ops_vm.h"
b3b94faa
DT
25#include "quota.h"
26#include "rgrp.h"
27#include "trans.h"
5c676f6d 28#include "util.h"
b3b94faa 29
d0217ac0 30static int gfs2_private_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
b3b94faa 31{
54cb8821 32 struct gfs2_inode *ip = GFS2_I(vma->vm_file->f_mapping->host);
b3b94faa
DT
33
34 set_bit(GIF_PAGED, &ip->i_flags);
d0217ac0 35 return filemap_fault(vma, vmf);
b3b94faa
DT
36}
37
38static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
39{
feaa7bba 40 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa 41 unsigned long index = page->index;
cd915493 42 u64 lblock = index << (PAGE_CACHE_SHIFT -
568f4c96 43 sdp->sd_sb.sb_bsize_shift);
b3b94faa
DT
44 unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
45 struct gfs2_alloc *al;
46 unsigned int data_blocks, ind_blocks;
47 unsigned int x;
48 int error;
49
50 al = gfs2_alloc_get(ip);
51
52 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
53 if (error)
54 goto out;
55
2933f925 56 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
b3b94faa
DT
57 if (error)
58 goto out_gunlock_q;
59
fd88de56 60 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
b3b94faa
DT
61
62 al->al_requested = data_blocks + ind_blocks;
63
64 error = gfs2_inplace_reserve(ip);
65 if (error)
66 goto out_gunlock_q;
67
bb8d8a6f 68 error = gfs2_trans_begin(sdp, al->al_rgd->rd_length +
b3b94faa
DT
69 ind_blocks + RES_DINODE +
70 RES_STATFS + RES_QUOTA, 0);
71 if (error)
72 goto out_ipres;
73
74 if (gfs2_is_stuffed(ip)) {
f25ef0c1 75 error = gfs2_unstuff_dinode(ip, NULL);
b3b94faa
DT
76 if (error)
77 goto out_trans;
78 }
79
80 for (x = 0; x < blocks; ) {
cd915493 81 u64 dblock;
b3b94faa
DT
82 unsigned int extlen;
83 int new = 1;
84
feaa7bba 85 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
b3b94faa
DT
86 if (error)
87 goto out_trans;
88
89 lblock += extlen;
90 x += extlen;
91 }
92
93 gfs2_assert_warn(sdp, al->al_alloced);
94
a91ea69f 95out_trans:
b3b94faa 96 gfs2_trans_end(sdp);
a91ea69f 97out_ipres:
b3b94faa 98 gfs2_inplace_release(ip);
a91ea69f 99out_gunlock_q:
b3b94faa 100 gfs2_quota_unlock(ip);
a91ea69f 101out:
b3b94faa 102 gfs2_alloc_put(ip);
b3b94faa
DT
103 return error;
104}
105
d0217ac0
NP
106static int gfs2_sharewrite_fault(struct vm_area_struct *vma,
107 struct vm_fault *vmf)
b3b94faa 108{
54cb8821 109 struct file *file = vma->vm_file;
59a1cc6b
SW
110 struct gfs2_file *gf = file->private_data;
111 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
b3b94faa 112 struct gfs2_holder i_gh;
b3b94faa
DT
113 int alloc_required;
114 int error;
d0217ac0 115 int ret = VM_FAULT_MINOR;
b3b94faa 116
b3b94faa
DT
117 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
118 if (error)
d0217ac0 119 goto out;
b3b94faa 120
b3b94faa
DT
121 set_bit(GIF_PAGED, &ip->i_flags);
122 set_bit(GIF_SW_PAGED, &ip->i_flags);
123
54cb8821 124 error = gfs2_write_alloc_required(ip,
d0217ac0 125 (u64)vmf->pgoff << PAGE_CACHE_SHIFT,
54cb8821
NP
126 PAGE_CACHE_SIZE, &alloc_required);
127 if (error) {
d0217ac0
NP
128 ret = VM_FAULT_OOM; /* XXX: are these right? */
129 goto out_unlock;
54cb8821 130 }
b3b94faa 131
59a1cc6b 132 set_bit(GFF_EXLOCK, &gf->f_flags);
d0217ac0 133 ret = filemap_fault(vma, vmf);
59a1cc6b 134 clear_bit(GFF_EXLOCK, &gf->f_flags);
d0217ac0
NP
135 if (ret & (VM_FAULT_ERROR | FAULT_RET_NOPAGE))
136 goto out_unlock;
b3b94faa
DT
137
138 if (alloc_required) {
d0217ac0
NP
139 /* XXX: do we need to drop page lock around alloc_page_backing?*/
140 error = alloc_page_backing(ip, vmf->page);
b3b94faa 141 if (error) {
d0217ac0
NP
142 if (ret & FAULT_RET_LOCKED)
143 unlock_page(vmf->page);
144 page_cache_release(vmf->page);
145 ret = VM_FAULT_OOM;
146 goto out_unlock;
b3b94faa 147 }
d0217ac0 148 set_page_dirty(vmf->page);
b3b94faa
DT
149 }
150
d0217ac0 151out_unlock:
b3b94faa 152 gfs2_glock_dq_uninit(&i_gh);
d0217ac0
NP
153out:
154 return ret;
b3b94faa
DT
155}
156
157struct vm_operations_struct gfs2_vm_ops_private = {
54cb8821 158 .fault = gfs2_private_fault,
b3b94faa
DT
159};
160
161struct vm_operations_struct gfs2_vm_ops_sharewrite = {
54cb8821 162 .fault = gfs2_sharewrite_fault,
b3b94faa
DT
163};
164