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> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 18 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 19 | |
| 20 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 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 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 31 | static struct page *gfs2_private_nopage(struct vm_area_struct *area, |
| 32 | unsigned long address, int *type) |
| 33 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 34 | struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | |
| 36 | set_bit(GIF_PAGED, &ip->i_flags); |
Steven Whitehouse | e5dab55 | 2007-01-18 17:44:20 +0000 | [diff] [blame] | 37 | return filemap_nopage(area, address, type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static int alloc_page_backing(struct gfs2_inode *ip, struct page *page) |
| 41 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 42 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 43 | unsigned long index = page->index; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 44 | u64 lblock = index << (PAGE_CACHE_SHIFT - |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 45 | sdp->sd_sb.sb_bsize_shift); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 58 | error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 59 | if (error) |
| 60 | goto out_gunlock_q; |
| 61 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 62 | gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 70 | error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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)) { |
Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 77 | error = gfs2_unstuff_dinode(ip, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 78 | if (error) |
| 79 | goto out_trans; |
| 80 | } |
| 81 | |
| 82 | for (x = 0; x < blocks; ) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 83 | u64 dblock; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 84 | unsigned int extlen; |
| 85 | int new = 1; |
| 86 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 87 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 97 | out_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 98 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 99 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 100 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 101 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 102 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 103 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 104 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 105 | return error; |
| 106 | } |
| 107 | |
| 108 | static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area, |
| 109 | unsigned long address, int *type) |
| 110 | { |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 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); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 114 | struct gfs2_holder i_gh; |
| 115 | struct page *result = NULL; |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 116 | unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + |
| 117 | area->vm_pgoff; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 118 | int alloc_required; |
| 119 | int error; |
| 120 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 122 | if (error) |
| 123 | return NULL; |
| 124 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 125 | set_bit(GIF_PAGED, &ip->i_flags); |
| 126 | set_bit(GIF_SW_PAGED, &ip->i_flags); |
| 127 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 128 | error = gfs2_write_alloc_required(ip, (u64)index << PAGE_CACHE_SHIFT, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 129 | PAGE_CACHE_SIZE, &alloc_required); |
| 130 | if (error) |
| 131 | goto out; |
| 132 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 133 | set_bit(GFF_EXLOCK, &gf->f_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 134 | result = filemap_nopage(area, address, type); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 135 | clear_bit(GFF_EXLOCK, &gf->f_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 149 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |