blob: d67248cae9b9adca0f0de25cb2c3b6f55c6498a6 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
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
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050017#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018#include <asm/semaphore.h>
19
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "lm_interface.h"
22#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "bmap.h"
24#include "glock.h"
25#include "inode.h"
26#include "ops_vm.h"
27#include "page.h"
28#include "quota.h"
29#include "rgrp.h"
30#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050031#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
33static void pfault_be_greedy(struct gfs2_inode *ip)
34{
35 unsigned int time;
36
37 spin_lock(&ip->i_spin);
38 time = ip->i_greedy;
39 ip->i_last_pfault = jiffies;
40 spin_unlock(&ip->i_spin);
41
42 gfs2_inode_hold(ip);
43 if (gfs2_glock_be_greedy(ip->i_gl, time))
44 gfs2_inode_put(ip);
45}
46
47static struct page *gfs2_private_nopage(struct vm_area_struct *area,
48 unsigned long address, int *type)
49{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050050 struct gfs2_inode *ip = area->vm_file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 struct gfs2_holder i_gh;
52 struct page *result;
53 int error;
54
David Teiglandb3b94fa2006-01-16 16:50:04 +000055 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
56 if (error)
57 return NULL;
58
59 set_bit(GIF_PAGED, &ip->i_flags);
60
61 result = filemap_nopage(area, address, type);
62
63 if (result && result != NOPAGE_OOM)
64 pfault_be_greedy(ip);
65
66 gfs2_glock_dq_uninit(&i_gh);
67
68 return result;
69}
70
71static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
72{
73 struct gfs2_sbd *sdp = ip->i_sbd;
74 unsigned long index = page->index;
Steven Whitehouse568f4c92006-02-27 12:00:42 -050075 uint64_t lblock = index << (PAGE_CACHE_SHIFT -
76 sdp->sd_sb.sb_bsize_shift);
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
78 struct gfs2_alloc *al;
79 unsigned int data_blocks, ind_blocks;
80 unsigned int x;
81 int error;
82
83 al = gfs2_alloc_get(ip);
84
85 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
86 if (error)
87 goto out;
88
89 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
90 if (error)
91 goto out_gunlock_q;
92
Steven Whitehousefd88de562006-05-05 16:59:11 -040093 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +000094
95 al->al_requested = data_blocks + ind_blocks;
96
97 error = gfs2_inplace_reserve(ip);
98 if (error)
99 goto out_gunlock_q;
100
Steven Whitehousefd88de562006-05-05 16:59:11 -0400101 error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102 ind_blocks + RES_DINODE +
103 RES_STATFS + RES_QUOTA, 0);
104 if (error)
105 goto out_ipres;
106
107 if (gfs2_is_stuffed(ip)) {
108 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, NULL);
109 if (error)
110 goto out_trans;
111 }
112
113 for (x = 0; x < blocks; ) {
114 uint64_t dblock;
115 unsigned int extlen;
116 int new = 1;
117
Steven Whitehousefd88de562006-05-05 16:59:11 -0400118 error = gfs2_extent_map(ip->i_vnode, lblock, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 if (error)
120 goto out_trans;
121
122 lblock += extlen;
123 x += extlen;
124 }
125
126 gfs2_assert_warn(sdp, al->al_alloced);
127
128 out_trans:
129 gfs2_trans_end(sdp);
130
131 out_ipres:
132 gfs2_inplace_release(ip);
133
134 out_gunlock_q:
135 gfs2_quota_unlock(ip);
136
137 out:
138 gfs2_alloc_put(ip);
139
140 return error;
141}
142
143static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area,
144 unsigned long address, int *type)
145{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500146 struct gfs2_inode *ip = area->vm_file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 struct gfs2_holder i_gh;
148 struct page *result = NULL;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500149 unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) +
150 area->vm_pgoff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 int alloc_required;
152 int error;
153
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
155 if (error)
156 return NULL;
157
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 set_bit(GIF_PAGED, &ip->i_flags);
159 set_bit(GIF_SW_PAGED, &ip->i_flags);
160
161 error = gfs2_write_alloc_required(ip,
162 (uint64_t)index << PAGE_CACHE_SHIFT,
163 PAGE_CACHE_SIZE, &alloc_required);
164 if (error)
165 goto out;
166
167 result = filemap_nopage(area, address, type);
168 if (!result || result == NOPAGE_OOM)
169 goto out;
170
171 if (alloc_required) {
172 error = alloc_page_backing(ip, result);
173 if (error) {
174 page_cache_release(result);
175 result = NULL;
176 goto out;
177 }
178 set_page_dirty(result);
179 }
180
181 pfault_be_greedy(ip);
182
183 out:
184 gfs2_glock_dq_uninit(&i_gh);
185
186 return result;
187}
188
189struct vm_operations_struct gfs2_vm_ops_private = {
190 .nopage = gfs2_private_nopage,
191};
192
193struct vm_operations_struct gfs2_vm_ops_sharewrite = {
194 .nopage = gfs2_sharewrite_nopage,
195};
196