2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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.
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>
16 #include <linux/pagemap.h>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/lm_interface.h>
31 static void pfault_be_greedy(struct gfs2_inode
*ip
)
35 spin_lock(&ip
->i_spin
);
37 ip
->i_last_pfault
= jiffies
;
38 spin_unlock(&ip
->i_spin
);
41 if (gfs2_glock_be_greedy(ip
->i_gl
, time
))
45 static struct page
*gfs2_private_nopage(struct vm_area_struct
*area
,
46 unsigned long address
, int *type
)
48 struct gfs2_inode
*ip
= GFS2_I(area
->vm_file
->f_mapping
->host
);
51 set_bit(GIF_PAGED
, &ip
->i_flags
);
53 result
= filemap_nopage(area
, address
, type
);
55 if (result
&& result
!= NOPAGE_OOM
)
61 static int alloc_page_backing(struct gfs2_inode
*ip
, struct page
*page
)
63 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
64 unsigned long index
= page
->index
;
65 u64 lblock
= index
<< (PAGE_CACHE_SHIFT
-
66 sdp
->sd_sb
.sb_bsize_shift
);
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
;
73 al
= gfs2_alloc_get(ip
);
75 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
79 error
= gfs2_quota_check(ip
, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
83 gfs2_write_calc_reserv(ip
, PAGE_CACHE_SIZE
, &data_blocks
, &ind_blocks
);
85 al
->al_requested
= data_blocks
+ ind_blocks
;
87 error
= gfs2_inplace_reserve(ip
);
91 error
= gfs2_trans_begin(sdp
, al
->al_rgd
->rd_ri
.ri_length
+
92 ind_blocks
+ RES_DINODE
+
93 RES_STATFS
+ RES_QUOTA
, 0);
97 if (gfs2_is_stuffed(ip
)) {
98 error
= gfs2_unstuff_dinode(ip
, NULL
);
103 for (x
= 0; x
< blocks
; ) {
108 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new, &dblock
, &extlen
);
116 gfs2_assert_warn(sdp
, al
->al_alloced
);
121 gfs2_inplace_release(ip
);
123 gfs2_quota_unlock(ip
);
129 static struct page
*gfs2_sharewrite_nopage(struct vm_area_struct
*area
,
130 unsigned long address
, int *type
)
132 struct file
*file
= area
->vm_file
;
133 struct gfs2_file
*gf
= file
->private_data
;
134 struct gfs2_inode
*ip
= GFS2_I(file
->f_mapping
->host
);
135 struct gfs2_holder i_gh
;
136 struct page
*result
= NULL
;
137 unsigned long index
= ((address
- area
->vm_start
) >> PAGE_CACHE_SHIFT
) +
142 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
146 set_bit(GIF_PAGED
, &ip
->i_flags
);
147 set_bit(GIF_SW_PAGED
, &ip
->i_flags
);
149 error
= gfs2_write_alloc_required(ip
, (u64
)index
<< PAGE_CACHE_SHIFT
,
150 PAGE_CACHE_SIZE
, &alloc_required
);
154 set_bit(GFF_EXLOCK
, &gf
->f_flags
);
155 result
= filemap_nopage(area
, address
, type
);
156 clear_bit(GFF_EXLOCK
, &gf
->f_flags
);
157 if (!result
|| result
== NOPAGE_OOM
)
160 if (alloc_required
) {
161 error
= alloc_page_backing(ip
, result
);
163 page_cache_release(result
);
167 set_page_dirty(result
);
170 pfault_be_greedy(ip
);
172 gfs2_glock_dq_uninit(&i_gh
);
177 struct vm_operations_struct gfs2_vm_ops_private
= {
178 .nopage
= gfs2_private_nopage
,
181 struct vm_operations_struct gfs2_vm_ops_sharewrite
= {
182 .nopage
= gfs2_sharewrite_nopage
,