David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 17 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 18 | |
| 19 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 20 | #include "lm_interface.h" |
| 21 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include "bmap.h" |
| 23 | #include "glock.h" |
| 24 | #include "inode.h" |
| 25 | #include "ops_vm.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 26 | #include "quota.h" |
| 27 | #include "rgrp.h" |
| 28 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 29 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | |
| 31 | static 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 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 40 | igrab(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | if (gfs2_glock_be_greedy(ip->i_gl, time)) |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 42 | iput(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static struct page *gfs2_private_nopage(struct vm_area_struct *area, |
| 46 | unsigned long address, int *type) |
| 47 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 48 | struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 49 | struct page *result; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 58 | return result; |
| 59 | } |
| 60 | |
| 61 | static int alloc_page_backing(struct gfs2_inode *ip, struct page *page) |
| 62 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 63 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 64 | unsigned long index = page->index; |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 65 | uint64_t lblock = index << (PAGE_CACHE_SHIFT - |
| 66 | sdp->sd_sb.sb_bsize_shift); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 83 | gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 91 | error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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)) { |
Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 98 | error = gfs2_unstuff_dinode(ip, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 108 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame^] | 118 | out_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 119 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame^] | 120 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame^] | 122 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 123 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame^] | 124 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 125 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 126 | return error; |
| 127 | } |
| 128 | |
| 129 | static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area, |
| 130 | unsigned long address, int *type) |
| 131 | { |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 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); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 135 | struct gfs2_holder i_gh; |
| 136 | struct page *result = NULL; |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 137 | unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + |
| 138 | area->vm_pgoff; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 139 | int alloc_required; |
| 140 | int error; |
| 141 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 142 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 143 | if (error) |
| 144 | return NULL; |
| 145 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 146 | set_bit(GIF_PAGED, &ip->i_flags); |
| 147 | set_bit(GIF_SW_PAGED, &ip->i_flags); |
| 148 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 149 | error = gfs2_write_alloc_required(ip, (u64)index << PAGE_CACHE_SHIFT, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | PAGE_CACHE_SIZE, &alloc_required); |
| 151 | if (error) |
| 152 | goto out; |
| 153 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 154 | set_bit(GFF_EXLOCK, &gf->f_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 155 | result = filemap_nopage(area, address, type); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 156 | clear_bit(GFF_EXLOCK, &gf->f_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 157 | if (!result || result == NOPAGE_OOM) |
| 158 | goto out; |
| 159 | |
| 160 | if (alloc_required) { |
| 161 | error = alloc_page_backing(ip, result); |
| 162 | if (error) { |
| 163 | page_cache_release(result); |
| 164 | result = NULL; |
| 165 | goto out; |
| 166 | } |
| 167 | set_page_dirty(result); |
| 168 | } |
| 169 | |
| 170 | pfault_be_greedy(ip); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 171 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 172 | gfs2_glock_dq_uninit(&i_gh); |
| 173 | |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | struct vm_operations_struct gfs2_vm_ops_private = { |
| 178 | .nopage = gfs2_private_nopage, |
| 179 | }; |
| 180 | |
| 181 | struct vm_operations_struct gfs2_vm_ops_sharewrite = { |
| 182 | .nopage = gfs2_sharewrite_nopage, |
| 183 | }; |
| 184 | |