]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/gfs2/ops_vm.c
[GFS2] Fix lock ordering bug in page fault path
[mirror_ubuntu-bionic-kernel.git] / fs / gfs2 / ops_vm.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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 v.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>
5c676f6d 17#include <linux/gfs2_ondisk.h>
b3b94faa
DT
18
19#include "gfs2.h"
5c676f6d
SW
20#include "lm_interface.h"
21#include "incore.h"
b3b94faa
DT
22#include "bmap.h"
23#include "glock.h"
24#include "inode.h"
25#include "ops_vm.h"
b3b94faa
DT
26#include "quota.h"
27#include "rgrp.h"
28#include "trans.h"
5c676f6d 29#include "util.h"
b3b94faa
DT
30
31static void pfault_be_greedy(struct gfs2_inode *ip)
32{
33 unsigned int time;
34
35 spin_lock(&ip->i_spin);
36 time = ip->i_greedy;
37 ip->i_last_pfault = jiffies;
38 spin_unlock(&ip->i_spin);
39
feaa7bba 40 igrab(&ip->i_inode);
b3b94faa 41 if (gfs2_glock_be_greedy(ip->i_gl, time))
feaa7bba 42 iput(&ip->i_inode);
b3b94faa
DT
43}
44
45static struct page *gfs2_private_nopage(struct vm_area_struct *area,
46 unsigned long address, int *type)
47{
feaa7bba 48 struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host);
b3b94faa 49 struct page *result;
b3b94faa
DT
50
51 set_bit(GIF_PAGED, &ip->i_flags);
52
53 result = filemap_nopage(area, address, type);
54
55 if (result && result != NOPAGE_OOM)
56 pfault_be_greedy(ip);
57
b3b94faa
DT
58 return result;
59}
60
61static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
62{
feaa7bba 63 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa 64 unsigned long index = page->index;
568f4c96
SW
65 uint64_t lblock = index << (PAGE_CACHE_SHIFT -
66 sdp->sd_sb.sb_bsize_shift);
b3b94faa
DT
67 unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
68 struct gfs2_alloc *al;
69 unsigned int data_blocks, ind_blocks;
70 unsigned int x;
71 int error;
72
73 al = gfs2_alloc_get(ip);
74
75 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
76 if (error)
77 goto out;
78
79 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
80 if (error)
81 goto out_gunlock_q;
82
fd88de56 83 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
b3b94faa
DT
84
85 al->al_requested = data_blocks + ind_blocks;
86
87 error = gfs2_inplace_reserve(ip);
88 if (error)
89 goto out_gunlock_q;
90
fd88de56 91 error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length +
b3b94faa
DT
92 ind_blocks + RES_DINODE +
93 RES_STATFS + RES_QUOTA, 0);
94 if (error)
95 goto out_ipres;
96
97 if (gfs2_is_stuffed(ip)) {
f25ef0c1 98 error = gfs2_unstuff_dinode(ip, NULL);
b3b94faa
DT
99 if (error)
100 goto out_trans;
101 }
102
103 for (x = 0; x < blocks; ) {
104 uint64_t dblock;
105 unsigned int extlen;
106 int new = 1;
107
feaa7bba 108 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
b3b94faa
DT
109 if (error)
110 goto out_trans;
111
112 lblock += extlen;
113 x += extlen;
114 }
115
116 gfs2_assert_warn(sdp, al->al_alloced);
117
118 out_trans:
119 gfs2_trans_end(sdp);
120
121 out_ipres:
122 gfs2_inplace_release(ip);
123
124 out_gunlock_q:
125 gfs2_quota_unlock(ip);
126
127 out:
128 gfs2_alloc_put(ip);
129
130 return error;
131}
132
133static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area,
134 unsigned long address, int *type)
135{
59a1cc6b
SW
136 struct file *file = area->vm_file;
137 struct gfs2_file *gf = file->private_data;
138 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
b3b94faa
DT
139 struct gfs2_holder i_gh;
140 struct page *result = NULL;
568f4c96
SW
141 unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) +
142 area->vm_pgoff;
b3b94faa
DT
143 int alloc_required;
144 int error;
145
b3b94faa
DT
146 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
147 if (error)
148 return NULL;
149
b3b94faa
DT
150 set_bit(GIF_PAGED, &ip->i_flags);
151 set_bit(GIF_SW_PAGED, &ip->i_flags);
152
59a1cc6b 153 error = gfs2_write_alloc_required(ip, (u64)index << PAGE_CACHE_SHIFT,
b3b94faa
DT
154 PAGE_CACHE_SIZE, &alloc_required);
155 if (error)
156 goto out;
157
59a1cc6b 158 set_bit(GFF_EXLOCK, &gf->f_flags);
b3b94faa 159 result = filemap_nopage(area, address, type);
59a1cc6b 160 clear_bit(GFF_EXLOCK, &gf->f_flags);
b3b94faa
DT
161 if (!result || result == NOPAGE_OOM)
162 goto out;
163
164 if (alloc_required) {
165 error = alloc_page_backing(ip, result);
166 if (error) {
167 page_cache_release(result);
168 result = NULL;
169 goto out;
170 }
171 set_page_dirty(result);
172 }
173
174 pfault_be_greedy(ip);
59a1cc6b 175out:
b3b94faa
DT
176 gfs2_glock_dq_uninit(&i_gh);
177
178 return result;
179}
180
181struct vm_operations_struct gfs2_vm_ops_private = {
182 .nopage = gfs2_private_nopage,
183};
184
185struct vm_operations_struct gfs2_vm_ops_sharewrite = {
186 .nopage = gfs2_sharewrite_nopage,
187};
188