ops_vm.c 4.2 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
D
David Teigland 已提交
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
D
David Teigland 已提交
8 9 10 11 12 13 14 15 16
 */

#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
17
#include <linux/gfs2_ondisk.h>
18
#include <linux/lm_interface.h>
D
David Teigland 已提交
19 20

#include "gfs2.h"
21
#include "incore.h"
D
David Teigland 已提交
22 23 24 25 26 27 28
#include "bmap.h"
#include "glock.h"
#include "inode.h"
#include "ops_vm.h"
#include "quota.h"
#include "rgrp.h"
#include "trans.h"
29
#include "util.h"
D
David Teigland 已提交
30 31 32 33 34 35 36 37 38 39

static void pfault_be_greedy(struct gfs2_inode *ip)
{
	unsigned int time;

	spin_lock(&ip->i_spin);
	time = ip->i_greedy;
	ip->i_last_pfault = jiffies;
	spin_unlock(&ip->i_spin);

40
	igrab(&ip->i_inode);
D
David Teigland 已提交
41
	if (gfs2_glock_be_greedy(ip->i_gl, time))
42
		iput(&ip->i_inode);
D
David Teigland 已提交
43 44 45 46 47
}

static struct page *gfs2_private_nopage(struct vm_area_struct *area,
					unsigned long address, int *type)
{
48
	struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host);
D
David Teigland 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
	struct page *result;

	set_bit(GIF_PAGED, &ip->i_flags);

	result = filemap_nopage(area, address, type);

	if (result && result != NOPAGE_OOM)
		pfault_be_greedy(ip);

	return result;
}

static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
{
63
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
64
	unsigned long index = page->index;
65
	u64 lblock = index << (PAGE_CACHE_SHIFT -
66
				    sdp->sd_sb.sb_bsize_shift);
D
David Teigland 已提交
67 68 69 70 71 72 73 74 75 76 77 78
	unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
	struct gfs2_alloc *al;
	unsigned int data_blocks, ind_blocks;
	unsigned int x;
	int error;

	al = gfs2_alloc_get(ip);

	error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
	if (error)
		goto out;

79
	error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
D
David Teigland 已提交
80 81 82
	if (error)
		goto out_gunlock_q;

S
Steven Whitehouse 已提交
83
	gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
D
David Teigland 已提交
84 85 86 87 88 89 90

	al->al_requested = data_blocks + ind_blocks;

	error = gfs2_inplace_reserve(ip);
	if (error)
		goto out_gunlock_q;

S
Steven Whitehouse 已提交
91
	error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length +
D
David Teigland 已提交
92 93 94 95 96 97
				 ind_blocks + RES_DINODE +
				 RES_STATFS + RES_QUOTA, 0);
	if (error)
		goto out_ipres;

	if (gfs2_is_stuffed(ip)) {
98
		error = gfs2_unstuff_dinode(ip, NULL);
D
David Teigland 已提交
99 100 101 102 103
		if (error)
			goto out_trans;
	}

	for (x = 0; x < blocks; ) {
104
		u64 dblock;
D
David Teigland 已提交
105 106 107
		unsigned int extlen;
		int new = 1;

108
		error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
D
David Teigland 已提交
109 110 111 112 113 114 115 116 117
		if (error)
			goto out_trans;

		lblock += extlen;
		x += extlen;
	}

	gfs2_assert_warn(sdp, al->al_alloced);

118
out_trans:
D
David Teigland 已提交
119
	gfs2_trans_end(sdp);
120
out_ipres:
D
David Teigland 已提交
121
	gfs2_inplace_release(ip);
122
out_gunlock_q:
D
David Teigland 已提交
123
	gfs2_quota_unlock(ip);
124
out:
D
David Teigland 已提交
125 126 127 128 129 130 131
	gfs2_alloc_put(ip);
	return error;
}

static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area,
					   unsigned long address, int *type)
{
132 133 134
	struct file *file = area->vm_file;
	struct gfs2_file *gf = file->private_data;
	struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
D
David Teigland 已提交
135 136
	struct gfs2_holder i_gh;
	struct page *result = NULL;
137 138
	unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) +
			      area->vm_pgoff;
D
David Teigland 已提交
139 140 141 142 143 144 145 146 147 148
	int alloc_required;
	int error;

	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
	if (error)
		return NULL;

	set_bit(GIF_PAGED, &ip->i_flags);
	set_bit(GIF_SW_PAGED, &ip->i_flags);

149
	error = gfs2_write_alloc_required(ip, (u64)index << PAGE_CACHE_SHIFT,
D
David Teigland 已提交
150 151 152 153
					  PAGE_CACHE_SIZE, &alloc_required);
	if (error)
		goto out;

154
	set_bit(GFF_EXLOCK, &gf->f_flags);
D
David Teigland 已提交
155
	result = filemap_nopage(area, address, type);
156
	clear_bit(GFF_EXLOCK, &gf->f_flags);
D
David Teigland 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
	if (!result || result == NOPAGE_OOM)
		goto out;

	if (alloc_required) {
		error = alloc_page_backing(ip, result);
		if (error) {
			page_cache_release(result);
			result = NULL;
			goto out;
		}
		set_page_dirty(result);
	}

	pfault_be_greedy(ip);
171
out:
D
David Teigland 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184
	gfs2_glock_dq_uninit(&i_gh);

	return result;
}

struct vm_operations_struct gfs2_vm_ops_private = {
	.nopage = gfs2_private_nopage,
};

struct vm_operations_struct gfs2_vm_ops_sharewrite = {
	.nopage = gfs2_sharewrite_nopage,
};