Commit | Line | Data |
---|---|---|
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 | |
7 | * of the GNU General Public License v.2. | |
8 | */ | |
9 | ||
10 | #include <linux/sched.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/completion.h> | |
14 | #include <linux/buffer_head.h> | |
15 | #include <linux/mm.h> | |
16 | #include <linux/pagemap.h> | |
5c676f6d | 17 | #include <linux/gfs2_ondisk.h> |
b3b94faa DT |
18 | |
19 | #include "gfs2.h" | |
5c676f6d SW |
20 | #include "lm_interface.h" |
21 | #include "incore.h" | |
b3b94faa DT |
22 | #include "bmap.h" |
23 | #include "glock.h" | |
24 | #include "inode.h" | |
25 | #include "ops_vm.h" | |
b3b94faa DT |
26 | #include "quota.h" |
27 | #include "rgrp.h" | |
28 | #include "trans.h" | |
5c676f6d | 29 | #include "util.h" |
b3b94faa DT |
30 | |
31 | static void pfault_be_greedy(struct gfs2_inode *ip) | |
32 | { | |
33 | unsigned int time; | |
34 | ||
35 | spin_lock(&ip->i_spin); | |
36 | time = ip->i_greedy; | |
37 | ip->i_last_pfault = jiffies; | |
38 | spin_unlock(&ip->i_spin); | |
39 | ||
feaa7bba | 40 | igrab(&ip->i_inode); |
b3b94faa | 41 | if (gfs2_glock_be_greedy(ip->i_gl, time)) |
feaa7bba | 42 | iput(&ip->i_inode); |
b3b94faa DT |
43 | } |
44 | ||
45 | static struct page *gfs2_private_nopage(struct vm_area_struct *area, | |
46 | unsigned long address, int *type) | |
47 | { | |
feaa7bba | 48 | struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host); |
b3b94faa DT |
49 | struct gfs2_holder i_gh; |
50 | struct page *result; | |
51 | int error; | |
52 | ||
b3b94faa DT |
53 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); |
54 | if (error) | |
55 | return NULL; | |
56 | ||
57 | set_bit(GIF_PAGED, &ip->i_flags); | |
58 | ||
59 | result = filemap_nopage(area, address, type); | |
60 | ||
61 | if (result && result != NOPAGE_OOM) | |
62 | pfault_be_greedy(ip); | |
63 | ||
64 | gfs2_glock_dq_uninit(&i_gh); | |
65 | ||
66 | return result; | |
67 | } | |
68 | ||
69 | static int alloc_page_backing(struct gfs2_inode *ip, struct page *page) | |
70 | { | |
feaa7bba | 71 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 72 | unsigned long index = page->index; |
568f4c96 SW |
73 | uint64_t lblock = index << (PAGE_CACHE_SHIFT - |
74 | sdp->sd_sb.sb_bsize_shift); | |
b3b94faa DT |
75 | unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift; |
76 | struct gfs2_alloc *al; | |
77 | unsigned int data_blocks, ind_blocks; | |
78 | unsigned int x; | |
79 | int error; | |
80 | ||
81 | al = gfs2_alloc_get(ip); | |
82 | ||
83 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
84 | if (error) | |
85 | goto out; | |
86 | ||
87 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); | |
88 | if (error) | |
89 | goto out_gunlock_q; | |
90 | ||
fd88de56 | 91 | gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks); |
b3b94faa DT |
92 | |
93 | al->al_requested = data_blocks + ind_blocks; | |
94 | ||
95 | error = gfs2_inplace_reserve(ip); | |
96 | if (error) | |
97 | goto out_gunlock_q; | |
98 | ||
fd88de56 | 99 | error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length + |
b3b94faa DT |
100 | ind_blocks + RES_DINODE + |
101 | RES_STATFS + RES_QUOTA, 0); | |
102 | if (error) | |
103 | goto out_ipres; | |
104 | ||
105 | if (gfs2_is_stuffed(ip)) { | |
f25ef0c1 | 106 | error = gfs2_unstuff_dinode(ip, NULL); |
b3b94faa DT |
107 | if (error) |
108 | goto out_trans; | |
109 | } | |
110 | ||
111 | for (x = 0; x < blocks; ) { | |
112 | uint64_t dblock; | |
113 | unsigned int extlen; | |
114 | int new = 1; | |
115 | ||
feaa7bba | 116 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen); |
b3b94faa DT |
117 | if (error) |
118 | goto out_trans; | |
119 | ||
120 | lblock += extlen; | |
121 | x += extlen; | |
122 | } | |
123 | ||
124 | gfs2_assert_warn(sdp, al->al_alloced); | |
125 | ||
126 | out_trans: | |
127 | gfs2_trans_end(sdp); | |
128 | ||
129 | out_ipres: | |
130 | gfs2_inplace_release(ip); | |
131 | ||
132 | out_gunlock_q: | |
133 | gfs2_quota_unlock(ip); | |
134 | ||
135 | out: | |
136 | gfs2_alloc_put(ip); | |
137 | ||
138 | return error; | |
139 | } | |
140 | ||
141 | static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area, | |
142 | unsigned long address, int *type) | |
143 | { | |
feaa7bba | 144 | struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host); |
b3b94faa DT |
145 | struct gfs2_holder i_gh; |
146 | struct page *result = NULL; | |
568f4c96 SW |
147 | unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + |
148 | area->vm_pgoff; | |
b3b94faa DT |
149 | int alloc_required; |
150 | int error; | |
151 | ||
b3b94faa DT |
152 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
153 | if (error) | |
154 | return NULL; | |
155 | ||
b3b94faa DT |
156 | set_bit(GIF_PAGED, &ip->i_flags); |
157 | set_bit(GIF_SW_PAGED, &ip->i_flags); | |
158 | ||
159 | error = gfs2_write_alloc_required(ip, | |
160 | (uint64_t)index << PAGE_CACHE_SHIFT, | |
161 | PAGE_CACHE_SIZE, &alloc_required); | |
162 | if (error) | |
163 | goto out; | |
164 | ||
165 | result = filemap_nopage(area, address, type); | |
166 | if (!result || result == NOPAGE_OOM) | |
167 | goto out; | |
168 | ||
169 | if (alloc_required) { | |
170 | error = alloc_page_backing(ip, result); | |
171 | if (error) { | |
172 | page_cache_release(result); | |
173 | result = NULL; | |
174 | goto out; | |
175 | } | |
176 | set_page_dirty(result); | |
177 | } | |
178 | ||
179 | pfault_be_greedy(ip); | |
180 | ||
181 | out: | |
182 | gfs2_glock_dq_uninit(&i_gh); | |
183 | ||
184 | return result; | |
185 | } | |
186 | ||
187 | struct vm_operations_struct gfs2_vm_ops_private = { | |
188 | .nopage = gfs2_private_nopage, | |
189 | }; | |
190 | ||
191 | struct vm_operations_struct gfs2_vm_ops_sharewrite = { | |
192 | .nopage = gfs2_sharewrite_nopage, | |
193 | }; | |
194 |