[GFS2] Get superblock a different way
[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 int gfs2_private_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
31 {
32         struct gfs2_inode *ip = GFS2_I(vma->vm_file->f_mapping->host);
33
34         set_bit(GIF_PAGED, &ip->i_flags);
35         return filemap_fault(vma, vmf);
36 }
37
38 static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
39 {
40         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
41         unsigned long index = page->index;
42         u64 lblock = index << (PAGE_CACHE_SHIFT -
43                                     sdp->sd_sb.sb_bsize_shift);
44         unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
45         struct gfs2_alloc *al;
46         unsigned int data_blocks, ind_blocks;
47         unsigned int x;
48         int error;
49
50         al = gfs2_alloc_get(ip);
51
52         error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
53         if (error)
54                 goto out;
55
56         error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
57         if (error)
58                 goto out_gunlock_q;
59
60         gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
61
62         al->al_requested = data_blocks + ind_blocks;
63
64         error = gfs2_inplace_reserve(ip);
65         if (error)
66                 goto out_gunlock_q;
67
68         error = gfs2_trans_begin(sdp, al->al_rgd->rd_length +
69                                  ind_blocks + RES_DINODE +
70                                  RES_STATFS + RES_QUOTA, 0);
71         if (error)
72                 goto out_ipres;
73
74         if (gfs2_is_stuffed(ip)) {
75                 error = gfs2_unstuff_dinode(ip, NULL);
76                 if (error)
77                         goto out_trans;
78         }
79
80         for (x = 0; x < blocks; ) {
81                 u64 dblock;
82                 unsigned int extlen;
83                 int new = 1;
84
85                 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
86                 if (error)
87                         goto out_trans;
88
89                 lblock += extlen;
90                 x += extlen;
91         }
92
93         gfs2_assert_warn(sdp, al->al_alloced);
94
95 out_trans:
96         gfs2_trans_end(sdp);
97 out_ipres:
98         gfs2_inplace_release(ip);
99 out_gunlock_q:
100         gfs2_quota_unlock(ip);
101 out:
102         gfs2_alloc_put(ip);
103         return error;
104 }
105
106 static int gfs2_sharewrite_fault(struct vm_area_struct *vma,
107                                                 struct vm_fault *vmf)
108 {
109         struct file *file = vma->vm_file;
110         struct gfs2_file *gf = file->private_data;
111         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
112         struct gfs2_holder i_gh;
113         int alloc_required;
114         int error;
115         int ret = 0;
116
117         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
118         if (error)
119                 goto out;
120
121         set_bit(GIF_PAGED, &ip->i_flags);
122         set_bit(GIF_SW_PAGED, &ip->i_flags);
123
124         error = gfs2_write_alloc_required(ip,
125                                         (u64)vmf->pgoff << PAGE_CACHE_SHIFT,
126                                         PAGE_CACHE_SIZE, &alloc_required);
127         if (error) {
128                 ret = VM_FAULT_OOM; /* XXX: are these right? */
129                 goto out_unlock;
130         }
131
132         set_bit(GFF_EXLOCK, &gf->f_flags);
133         ret = filemap_fault(vma, vmf);
134         clear_bit(GFF_EXLOCK, &gf->f_flags);
135         if (ret & VM_FAULT_ERROR)
136                 goto out_unlock;
137
138         if (alloc_required) {
139                 /* XXX: do we need to drop page lock around alloc_page_backing?*/
140                 error = alloc_page_backing(ip, vmf->page);
141                 if (error) {
142                         /*
143                          * VM_FAULT_LOCKED should always be the case for
144                          * filemap_fault, but it may not be in a future
145                          * implementation.
146                          */
147                         if (ret & VM_FAULT_LOCKED)
148                                 unlock_page(vmf->page);
149                         page_cache_release(vmf->page);
150                         ret = VM_FAULT_OOM;
151                         goto out_unlock;
152                 }
153                 set_page_dirty(vmf->page);
154         }
155
156 out_unlock:
157         gfs2_glock_dq_uninit(&i_gh);
158 out:
159         return ret;
160 }
161
162 struct vm_operations_struct gfs2_vm_ops_private = {
163         .fault = gfs2_private_fault,
164 };
165
166 struct vm_operations_struct gfs2_vm_ops_sharewrite = {
167         .fault = gfs2_sharewrite_fault,
168 };
169