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