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 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 16 | #include <linux/gfs2_ondisk.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 17 | #include <linux/lm_interface.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 "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 21 | #include "bmap.h" |
| 22 | #include "glock.h" |
| 23 | #include "inode.h" |
| 24 | #include "ops_vm.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 25 | #include "quota.h" |
| 26 | #include "rgrp.h" |
| 27 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 29 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | static struct page *gfs2_private_nopage(struct vm_area_struct *area, |
| 31 | unsigned long address, int *type) |
| 32 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 33 | struct gfs2_inode *ip = GFS2_I(area->vm_file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 34 | |
| 35 | set_bit(GIF_PAGED, &ip->i_flags); |
Steven Whitehouse | e5dab55 | 2007-01-18 17:44:20 +0000 | [diff] [blame] | 36 | return filemap_nopage(area, address, type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int alloc_page_backing(struct gfs2_inode *ip, struct page *page) |
| 40 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 41 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 42 | unsigned long index = page->index; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 43 | u64 lblock = index << (PAGE_CACHE_SHIFT - |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 44 | sdp->sd_sb.sb_bsize_shift); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 45 | unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift; |
| 46 | struct gfs2_alloc *al; |
| 47 | unsigned int data_blocks, ind_blocks; |
| 48 | unsigned int x; |
| 49 | int error; |
| 50 | |
| 51 | al = gfs2_alloc_get(ip); |
| 52 | |
| 53 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 54 | if (error) |
| 55 | goto out; |
| 56 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 57 | 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] | 58 | if (error) |
| 59 | goto out_gunlock_q; |
| 60 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 61 | gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 62 | |
| 63 | al->al_requested = data_blocks + ind_blocks; |
| 64 | |
| 65 | error = gfs2_inplace_reserve(ip); |
| 66 | if (error) |
| 67 | goto out_gunlock_q; |
| 68 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 69 | error = gfs2_trans_begin(sdp, al->al_rgd->rd_ri.ri_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 70 | ind_blocks + RES_DINODE + |
| 71 | RES_STATFS + RES_QUOTA, 0); |
| 72 | if (error) |
| 73 | goto out_ipres; |
| 74 | |
| 75 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 76 | error = gfs2_unstuff_dinode(ip, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 77 | if (error) |
| 78 | goto out_trans; |
| 79 | } |
| 80 | |
| 81 | for (x = 0; x < blocks; ) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 82 | u64 dblock; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 83 | unsigned int extlen; |
| 84 | int new = 1; |
| 85 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 86 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 | if (error) |
| 88 | goto out_trans; |
| 89 | |
| 90 | lblock += extlen; |
| 91 | x += extlen; |
| 92 | } |
| 93 | |
| 94 | gfs2_assert_warn(sdp, al->al_alloced); |
| 95 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 96 | out_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 97 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 98 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 99 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 100 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 101 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 102 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 103 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 104 | return error; |
| 105 | } |
| 106 | |
| 107 | static struct page *gfs2_sharewrite_nopage(struct vm_area_struct *area, |
| 108 | unsigned long address, int *type) |
| 109 | { |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 110 | struct file *file = area->vm_file; |
| 111 | struct gfs2_file *gf = file->private_data; |
| 112 | struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 113 | struct gfs2_holder i_gh; |
| 114 | struct page *result = NULL; |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 115 | unsigned long index = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + |
| 116 | area->vm_pgoff; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | int alloc_required; |
| 118 | int error; |
| 119 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 120 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 121 | if (error) |
| 122 | return NULL; |
| 123 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | set_bit(GIF_PAGED, &ip->i_flags); |
| 125 | set_bit(GIF_SW_PAGED, &ip->i_flags); |
| 126 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 127 | error = gfs2_write_alloc_required(ip, (u64)index << PAGE_CACHE_SHIFT, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 128 | PAGE_CACHE_SIZE, &alloc_required); |
| 129 | if (error) |
| 130 | goto out; |
| 131 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 132 | set_bit(GFF_EXLOCK, &gf->f_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 133 | result = filemap_nopage(area, address, type); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 134 | clear_bit(GFF_EXLOCK, &gf->f_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 135 | if (!result || result == NOPAGE_OOM) |
| 136 | goto out; |
| 137 | |
| 138 | if (alloc_required) { |
| 139 | error = alloc_page_backing(ip, result); |
| 140 | if (error) { |
| 141 | page_cache_release(result); |
| 142 | result = NULL; |
| 143 | goto out; |
| 144 | } |
| 145 | set_page_dirty(result); |
| 146 | } |
| 147 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 148 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 149 | gfs2_glock_dq_uninit(&i_gh); |
| 150 | |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | struct vm_operations_struct gfs2_vm_ops_private = { |
| 155 | .nopage = gfs2_private_nopage, |
| 156 | }; |
| 157 | |
| 158 | struct vm_operations_struct gfs2_vm_ops_sharewrite = { |
| 159 | .nopage = gfs2_sharewrite_nopage, |
| 160 | }; |
| 161 | |