2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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.
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>
16 #include <linux/pagemap.h>
17 #include <asm/semaphore.h>
29 static void pfault_be_greedy(struct gfs2_inode *ip)
33 spin_lock(&ip->i_spin);
35 ip->i_last_pfault = jiffies;
36 spin_unlock(&ip->i_spin);
39 if (gfs2_glock_be_greedy(ip->i_gl, time))
43 static struct page *gfs2_private_nopage(struct vm_area_struct *area,
44 unsigned long address, int *type)
46 struct gfs2_inode *ip = get_v2ip(area->vm_file->f_mapping->host);
47 struct gfs2_holder i_gh;
51 atomic_inc(&ip->i_sbd->sd_ops_vm);
53 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
57 set_bit(GIF_PAGED, &ip->i_flags);
59 result = filemap_nopage(area, address, type);
61 if (result && result != NOPAGE_OOM)
64 gfs2_glock_dq_uninit(&i_gh);
69 static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
71 struct gfs2_sbd *sdp = ip->i_sbd;
72 unsigned long index = page->index;
73 uint64_t lblock = index << (PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift);
74 unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
75 struct gfs2_alloc *al;
76 unsigned int data_blocks, ind_blocks;
80 al = gfs2_alloc_get(ip);
82 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
86 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
90 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE,
91 &data_blocks, &ind_blocks);
93 al->al_requested = data_blocks + ind_blocks;
95 error = gfs2_inplace_reserve(ip);
99 error = gfs2_trans_begin(sdp,
100 al->al_rgd->rd_ri.ri_length +
101 ind_blocks + RES_DINODE +
102 RES_STATFS + RES_QUOTA, 0);
106 if (gfs2_is_stuffed(ip)) {
107 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, NULL);
112 for (x = 0; x < blocks; ) {
117 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
125 gfs2_assert_warn(sdp, al->al_alloced);
131 gfs2_inplace_release(ip);
134 gfs2_quota_unlock(ip);
142 static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area,
143 unsigned long address, int *type)
145 struct gfs2_inode *ip = get_v2ip(area->vm_file->f_mapping->host);
146 struct gfs2_holder i_gh;
147 struct page *result = NULL;
148 unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
152 atomic_inc(&ip->i_sbd->sd_ops_vm);
154 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
158 if (gfs2_is_jdata(ip))
161 set_bit(GIF_PAGED, &ip->i_flags);
162 set_bit(GIF_SW_PAGED, &ip->i_flags);
164 error = gfs2_write_alloc_required(ip,
165 (uint64_t)index << PAGE_CACHE_SHIFT,
166 PAGE_CACHE_SIZE, &alloc_required);
170 result = filemap_nopage(area, address, type);
171 if (!result || result == NOPAGE_OOM)
174 if (alloc_required) {
175 error = alloc_page_backing(ip, result);
177 page_cache_release(result);
181 set_page_dirty(result);
184 pfault_be_greedy(ip);
187 gfs2_glock_dq_uninit(&i_gh);
192 struct vm_operations_struct gfs2_vm_ops_private = {
193 .nopage = gfs2_private_nopage,
196 struct vm_operations_struct gfs2_vm_ops_sharewrite = {
197 .nopage = gfs2_sharewrite_nopage,