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