blob: 927d739d468512502225331857c859bf05d826ca [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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 Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#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 Whitehouse5c676f62006-02-27 17:23:27 -050016#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020017#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "bmap.h"
22#include "glock.h"
23#include "inode.h"
24#include "ops_vm.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "quota.h"
26#include "rgrp.h"
27#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
Nick Piggind0217ac2007-07-19 01:47:03 -070030static int gfs2_private_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Teiglandb3b94fa2006-01-16 16:50:04 +000031{
Nick Piggin54cb8822007-07-19 01:46:59 -070032 struct gfs2_inode *ip = GFS2_I(vma->vm_file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +000033
34 set_bit(GIF_PAGED, &ip->i_flags);
Nick Piggind0217ac2007-07-19 01:47:03 -070035 return filemap_fault(vma, vmf);
David Teiglandb3b94fa2006-01-16 16:50:04 +000036}
37
38static int alloc_page_backing(struct gfs2_inode *ip, struct page *page)
39{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040040 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 unsigned long index = page->index;
Steven Whitehousecd915492006-09-04 12:49:07 -040042 u64 lblock = index << (PAGE_CACHE_SHIFT -
Steven Whitehouse568f4c92006-02-27 12:00:42 -050043 sdp->sd_sb.sb_bsize_shift);
David Teiglandb3b94fa2006-01-16 16:50:04 +000044 unsigned int blocks = PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift;
45 struct gfs2_alloc *al;
46 unsigned int data_blocks, ind_blocks;
47 unsigned int x;
48 int error;
49
50 al = gfs2_alloc_get(ip);
51
52 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
53 if (error)
54 goto out;
55
Steven Whitehouse2933f922006-11-01 13:23:29 -050056 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +000057 if (error)
58 goto out_gunlock_q;
59
Steven Whitehousefd88de562006-05-05 16:59:11 -040060 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +000061
62 al->al_requested = data_blocks + ind_blocks;
63
64 error = gfs2_inplace_reserve(ip);
65 if (error)
66 goto out_gunlock_q;
67
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010068 error = gfs2_trans_begin(sdp, al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 ind_blocks + RES_DINODE +
70 RES_STATFS + RES_QUOTA, 0);
71 if (error)
72 goto out_ipres;
73
74 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040075 error = gfs2_unstuff_dinode(ip, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 if (error)
77 goto out_trans;
78 }
79
80 for (x = 0; x < blocks; ) {
Steven Whitehousecd915492006-09-04 12:49:07 -040081 u64 dblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +000082 unsigned int extlen;
83 int new = 1;
84
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040085 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 if (error)
87 goto out_trans;
88
89 lblock += extlen;
90 x += extlen;
91 }
92
93 gfs2_assert_warn(sdp, al->al_alloced);
94
Steven Whitehousea91ea692006-09-04 12:04:26 -040095out_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +000096 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -040097out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +000098 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -040099out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400101out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 return error;
104}
105
Nick Piggind0217ac2007-07-19 01:47:03 -0700106static int gfs2_sharewrite_fault(struct vm_area_struct *vma,
107 struct vm_fault *vmf)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108{
Nick Piggin54cb8822007-07-19 01:46:59 -0700109 struct file *file = vma->vm_file;
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400110 struct gfs2_file *gf = file->private_data;
111 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 struct gfs2_holder i_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 int alloc_required;
114 int error;
Nick Piggin83c54072007-07-19 01:47:05 -0700115 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
118 if (error)
Nick Piggind0217ac2007-07-19 01:47:03 -0700119 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121 set_bit(GIF_PAGED, &ip->i_flags);
122 set_bit(GIF_SW_PAGED, &ip->i_flags);
123
Nick Piggin54cb8822007-07-19 01:46:59 -0700124 error = gfs2_write_alloc_required(ip,
Nick Piggind0217ac2007-07-19 01:47:03 -0700125 (u64)vmf->pgoff << PAGE_CACHE_SHIFT,
Nick Piggin54cb8822007-07-19 01:46:59 -0700126 PAGE_CACHE_SIZE, &alloc_required);
127 if (error) {
Nick Piggind0217ac2007-07-19 01:47:03 -0700128 ret = VM_FAULT_OOM; /* XXX: are these right? */
129 goto out_unlock;
Nick Piggin54cb8822007-07-19 01:46:59 -0700130 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400132 set_bit(GFF_EXLOCK, &gf->f_flags);
Nick Piggind0217ac2007-07-19 01:47:03 -0700133 ret = filemap_fault(vma, vmf);
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400134 clear_bit(GFF_EXLOCK, &gf->f_flags);
Nick Piggin83c54072007-07-19 01:47:05 -0700135 if (ret & VM_FAULT_ERROR)
Nick Piggind0217ac2007-07-19 01:47:03 -0700136 goto out_unlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137
138 if (alloc_required) {
Nick Piggind0217ac2007-07-19 01:47:03 -0700139 /* XXX: do we need to drop page lock around alloc_page_backing?*/
140 error = alloc_page_backing(ip, vmf->page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 if (error) {
Nick Piggin83c54072007-07-19 01:47:05 -0700142 /*
143 * VM_FAULT_LOCKED should always be the case for
144 * filemap_fault, but it may not be in a future
145 * implementation.
146 */
147 if (ret & VM_FAULT_LOCKED)
Nick Piggind0217ac2007-07-19 01:47:03 -0700148 unlock_page(vmf->page);
149 page_cache_release(vmf->page);
150 ret = VM_FAULT_OOM;
151 goto out_unlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 }
Nick Piggind0217ac2007-07-19 01:47:03 -0700153 set_page_dirty(vmf->page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 }
155
Nick Piggind0217ac2007-07-19 01:47:03 -0700156out_unlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 gfs2_glock_dq_uninit(&i_gh);
Nick Piggind0217ac2007-07-19 01:47:03 -0700158out:
159 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160}
161
162struct vm_operations_struct gfs2_vm_ops_private = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700163 .fault = gfs2_private_fault,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164};
165
166struct vm_operations_struct gfs2_vm_ops_sharewrite = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700167 .fault = gfs2_sharewrite_fault,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168};
169