blob: 1737af98a4209bd7a11b11f6539bcd23fd9853b2 [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/pagemap.h>
15#include <linux/uio.h>
16#include <linux/blkdev.h>
17#include <linux/mm.h>
Miklos Szeredif58ba882008-07-02 21:12:01 +020018#include <linux/mount.h>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000019#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050021#include <linux/ext2_fs.h>
22#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020023#include <linux/lm_interface.h>
Steven Whitehouse33c3de32006-11-30 10:14:32 -050024#include <linux/writeback.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include <asm/uaccess.h>
26
27#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "bmap.h"
30#include "dir.h"
31#include "glock.h"
32#include "glops.h"
33#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034#include "log.h"
35#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036#include "quota.h"
37#include "rgrp.h"
38#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050039#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050040#include "eaops.h"
Steven Whitehouse55610932007-10-17 08:47:38 +010041#include "ops_address.h"
Adrian Bunk8af4c722008-02-17 10:17:12 +020042#include "ops_inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000043
David Teiglandb3b94fa2006-01-16 16:50:04 +000044/**
45 * gfs2_llseek - seek to a location in a file
46 * @file: the file
47 * @offset: the offset
48 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
49 *
50 * SEEK_END requires the glock for the file because it references the
51 * file's size.
52 *
53 * Returns: The new offset, or errno
54 */
55
56static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
57{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040058 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 struct gfs2_holder i_gh;
60 loff_t error;
61
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 if (origin == 2) {
63 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
64 &i_gh);
65 if (!error) {
66 error = remote_llseek(file, offset, origin);
67 gfs2_glock_dq_uninit(&i_gh);
68 }
69 } else
70 error = remote_llseek(file, offset, origin);
71
72 return error;
73}
74
David Teiglandb3b94fa2006-01-16 16:50:04 +000075/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -040076 * gfs2_readdir - Read directory entries from a directory
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 * @file: The directory to read from
78 * @dirent: Buffer for dirents
79 * @filldir: Function used to do the copying
80 *
81 * Returns: errno
82 */
83
Steven Whitehousef0e522a2006-09-19 16:41:11 -040084static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +000085{
Steven Whitehouse71b86f52006-03-28 14:14:04 -050086 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040087 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000088 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -040089 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 int error;
91
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
93 error = gfs2_glock_nq_atime(&d_gh);
94 if (error) {
95 gfs2_holder_uninit(&d_gh);
96 return error;
97 }
98
Steven Whitehouse3699e3a2007-01-17 15:09:20 +000099 error = gfs2_dir_read(dir, &offset, dirent, filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100
101 gfs2_glock_dq_uninit(&d_gh);
102
103 file->f_pos = offset;
104
105 return error;
106}
107
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400108/**
109 * fsflags_cvt
110 * @table: A table of 32 u32 flags
111 * @val: a 32 bit value to convert
112 *
113 * This function can be used to convert between fsflags values and
114 * GFS2's own flags values.
115 *
116 * Returns: the converted flags
117 */
118static u32 fsflags_cvt(const u32 *table, u32 val)
119{
120 u32 res = 0;
121 while(val) {
122 if (val & 1)
123 res |= *table;
124 table++;
125 val >>= 1;
126 }
127 return res;
128}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400130static const u32 fsflags_to_gfs2[32] = {
131 [3] = GFS2_DIF_SYNC,
132 [4] = GFS2_DIF_IMMUTABLE,
133 [5] = GFS2_DIF_APPENDONLY,
134 [7] = GFS2_DIF_NOATIME,
135 [12] = GFS2_DIF_EXHASH,
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100136 [14] = GFS2_DIF_INHERIT_JDATA,
137 [20] = GFS2_DIF_INHERIT_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500138};
139
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400140static const u32 gfs2_to_fsflags[32] = {
141 [gfs2fl_Sync] = FS_SYNC_FL,
142 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
143 [gfs2fl_AppendOnly] = FS_APPEND_FL,
144 [gfs2fl_NoAtime] = FS_NOATIME_FL,
145 [gfs2fl_ExHash] = FS_INDEX_FL,
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400146 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
147 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500148};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500149
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400150static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500151{
Josef Sipek81454092006-12-08 02:37:03 -0800152 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400153 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500154 struct gfs2_holder gh;
155 int error;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400156 u32 fsflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500157
158 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500159 error = gfs2_glock_nq_atime(&gh);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500160 if (error)
161 return error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400162
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400163 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100164 if (!S_ISDIR(inode->i_mode)) {
165 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
166 fsflags |= FS_JOURNAL_DATA_FL;
167 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
168 fsflags |= FS_DIRECTIO_FL;
169 }
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400170 if (put_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500171 error = -EFAULT;
172
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100173 gfs2_glock_dq(&gh);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500174 gfs2_holder_uninit(&gh);
175 return error;
176}
177
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500178void gfs2_set_inode_flags(struct inode *inode)
179{
180 struct gfs2_inode *ip = GFS2_I(inode);
181 struct gfs2_dinode_host *di = &ip->i_di;
182 unsigned int flags = inode->i_flags;
183
184 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
185 if (di->di_flags & GFS2_DIF_IMMUTABLE)
186 flags |= S_IMMUTABLE;
187 if (di->di_flags & GFS2_DIF_APPENDONLY)
188 flags |= S_APPEND;
189 if (di->di_flags & GFS2_DIF_NOATIME)
190 flags |= S_NOATIME;
191 if (di->di_flags & GFS2_DIF_SYNC)
192 flags |= S_SYNC;
193 inode->i_flags = flags;
194}
195
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500196/* Flags that can be set by user space */
197#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
198 GFS2_DIF_DIRECTIO| \
199 GFS2_DIF_IMMUTABLE| \
200 GFS2_DIF_APPENDONLY| \
201 GFS2_DIF_NOATIME| \
202 GFS2_DIF_SYNC| \
203 GFS2_DIF_SYSTEM| \
204 GFS2_DIF_INHERIT_DIRECTIO| \
205 GFS2_DIF_INHERIT_JDATA)
206
207/**
208 * gfs2_set_flags - set flags on an inode
209 * @inode: The inode
210 * @flags: The flags to set
211 * @mask: Indicates which flags are valid
212 *
213 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400214static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500215{
Josef Sipek81454092006-12-08 02:37:03 -0800216 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400217 struct gfs2_inode *ip = GFS2_I(inode);
218 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500219 struct buffer_head *bh;
220 struct gfs2_holder gh;
221 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400222 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500223
Miklos Szeredif58ba882008-07-02 21:12:01 +0200224 error = mnt_want_write(filp->f_path.mnt);
Abhijith Das52f341c2006-07-21 02:03:21 -0400225 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500226 return error;
227
Miklos Szeredif58ba882008-07-02 21:12:01 +0200228 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
229 if (error)
230 goto out_drop_write;
231
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400232 flags = ip->i_di.di_flags;
233 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500234 if ((new_flags ^ flags) == 0)
235 goto out;
236
237 error = -EINVAL;
238 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
239 goto out;
240
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500241 error = -EPERM;
242 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
243 goto out;
244 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
245 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400246 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400247 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500248 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400249 if (!IS_IMMUTABLE(inode)) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200250 error = gfs2_permission(inode, MAY_WRITE);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400251 if (error)
252 goto out;
253 }
Steven Whitehouse55610932007-10-17 08:47:38 +0100254 if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
255 if (flags & GFS2_DIF_JDATA)
256 gfs2_log_flush(sdp, ip->i_gl);
257 error = filemap_fdatawrite(inode->i_mapping);
258 if (error)
259 goto out;
260 error = filemap_fdatawait(inode->i_mapping);
261 if (error)
262 goto out;
263 }
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400264 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500265 if (error)
266 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400267 error = gfs2_meta_inode_buffer(ip, &bh);
268 if (error)
269 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500270 gfs2_trans_add_bh(ip->i_gl, bh, 1);
271 ip->i_di.di_flags = new_flags;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500272 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500273 brelse(bh);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500274 gfs2_set_inode_flags(inode);
Steven Whitehouse55610932007-10-17 08:47:38 +0100275 gfs2_set_aops(inode);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400276out_trans_end:
277 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500278out:
279 gfs2_glock_dq_uninit(&gh);
Miklos Szeredif58ba882008-07-02 21:12:01 +0200280out_drop_write:
281 mnt_drop_write(filp->f_path.mnt);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500282 return error;
283}
284
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400285static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500286{
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100287 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400288 u32 fsflags, gfsflags;
289 if (get_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500290 return -EFAULT;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400291 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100292 if (!S_ISDIR(inode->i_mode)) {
293 if (gfsflags & GFS2_DIF_INHERIT_JDATA)
294 gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
295 if (gfsflags & GFS2_DIF_INHERIT_DIRECTIO)
296 gfsflags ^= (GFS2_DIF_DIRECTIO | GFS2_DIF_INHERIT_DIRECTIO);
297 return do_gfs2_set_flags(filp, gfsflags, ~0);
298 }
299 return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500300}
301
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400302static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500303{
304 switch(cmd) {
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400305 case FS_IOC_GETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400306 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400307 case FS_IOC_SETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400308 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500309 }
310 return -ENOTTY;
311}
312
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100313/**
314 * gfs2_allocate_page_backing - Use bmap to allocate blocks
315 * @page: The (locked) page to allocate backing for
316 *
317 * We try to allocate all the blocks required for the page in
318 * one go. This might fail for various reasons, so we keep
319 * trying until all the blocks to back this page are allocated.
320 * If some of the blocks are already allocated, thats ok too.
321 */
322
323static int gfs2_allocate_page_backing(struct page *page)
324{
325 struct inode *inode = page->mapping->host;
326 struct buffer_head bh;
327 unsigned long size = PAGE_CACHE_SIZE;
328 u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
329
330 do {
331 bh.b_state = 0;
332 bh.b_size = size;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600333 gfs2_block_map(inode, lblock, &bh, 1);
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100334 if (!buffer_mapped(&bh))
335 return -EIO;
336 size -= bh.b_size;
337 lblock += (bh.b_size >> inode->i_blkbits);
338 } while(size > 0);
339 return 0;
340}
341
342/**
343 * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
344 * @vma: The virtual memory area
345 * @page: The page which is about to become writable
346 *
347 * When the page becomes writable, we need to ensure that we have
348 * blocks allocated on disk to back that page.
349 */
350
351static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
352{
353 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
354 struct gfs2_inode *ip = GFS2_I(inode);
355 struct gfs2_sbd *sdp = GFS2_SB(inode);
356 unsigned long last_index;
357 u64 pos = page->index << (PAGE_CACHE_SIZE - inode->i_blkbits);
358 unsigned int data_blocks, ind_blocks, rblocks;
359 int alloc_required = 0;
360 struct gfs2_holder gh;
361 struct gfs2_alloc *al;
362 int ret;
363
364 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &gh);
365 ret = gfs2_glock_nq_atime(&gh);
366 if (ret)
367 goto out;
368
369 set_bit(GIF_SW_PAGED, &ip->i_flags);
370 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
371 ret = gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE, &alloc_required);
372 if (ret || !alloc_required)
373 goto out_unlock;
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000374 ret = -ENOMEM;
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100375 al = gfs2_alloc_get(ip);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000376 if (al == NULL)
377 goto out_unlock;
378
Steven Whitehoused82661d2008-03-10 15:34:50 +0000379 ret = gfs2_quota_lock_check(ip);
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100380 if (ret)
381 goto out_alloc_put;
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100382 al->al_requested = data_blocks + ind_blocks;
383 ret = gfs2_inplace_reserve(ip);
384 if (ret)
385 goto out_quota_unlock;
386
387 rblocks = RES_DINODE + ind_blocks;
388 if (gfs2_is_jdata(ip))
389 rblocks += data_blocks ? data_blocks : 1;
390 if (ind_blocks || data_blocks)
391 rblocks += RES_STATFS + RES_QUOTA;
392 ret = gfs2_trans_begin(sdp, rblocks, 0);
393 if (ret)
394 goto out_trans_fail;
395
396 lock_page(page);
397 ret = -EINVAL;
398 last_index = ip->i_inode.i_size >> PAGE_CACHE_SHIFT;
399 if (page->index > last_index)
400 goto out_unlock_page;
Steven Whitehouseb7fe2e32008-01-17 15:12:03 +0000401 ret = 0;
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100402 if (!PageUptodate(page) || page->mapping != ip->i_inode.i_mapping)
403 goto out_unlock_page;
404 if (gfs2_is_stuffed(ip)) {
405 ret = gfs2_unstuff_dinode(ip, page);
406 if (ret)
407 goto out_unlock_page;
408 }
409 ret = gfs2_allocate_page_backing(page);
410
411out_unlock_page:
412 unlock_page(page);
413 gfs2_trans_end(sdp);
414out_trans_fail:
415 gfs2_inplace_release(ip);
416out_quota_unlock:
417 gfs2_quota_unlock(ip);
418out_alloc_put:
419 gfs2_alloc_put(ip);
420out_unlock:
421 gfs2_glock_dq(&gh);
422out:
423 gfs2_holder_uninit(&gh);
424 return ret;
425}
426
427static struct vm_operations_struct gfs2_vm_ops = {
428 .fault = filemap_fault,
429 .page_mkwrite = gfs2_page_mkwrite,
430};
431
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500432
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433/**
434 * gfs2_mmap -
435 * @file: The file to map
436 * @vma: The VMA which described the mapping
437 *
438 * Returns: 0 or error code
439 */
440
441static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
442{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400443 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 struct gfs2_holder i_gh;
445 int error;
446
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
448 error = gfs2_glock_nq_atime(&i_gh);
449 if (error) {
450 gfs2_holder_uninit(&i_gh);
451 return error;
452 }
453
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100454 vma->vm_ops = &gfs2_vm_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
456 gfs2_glock_dq_uninit(&i_gh);
457
458 return error;
459}
460
461/**
462 * gfs2_open - open a file
463 * @inode: the inode to open
464 * @file: the struct file for this opening
465 *
466 * Returns: errno
467 */
468
469static int gfs2_open(struct inode *inode, struct file *file)
470{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400471 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 struct gfs2_holder i_gh;
473 struct gfs2_file *fp;
474 int error;
475
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
477 if (!fp)
478 return -ENOMEM;
479
Steven Whitehousef55ab262006-02-21 12:51:39 +0000480 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400482 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500483 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500485 if (S_ISREG(ip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
487 &i_gh);
488 if (error)
489 goto fail;
490
491 if (!(file->f_flags & O_LARGEFILE) &&
492 ip->i_di.di_size > MAX_NON_LFS) {
Alan Coxa9c62a12007-10-16 23:30:22 -0700493 error = -EOVERFLOW;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 goto fail_gunlock;
495 }
496
497 /* Listen to the Direct I/O flag */
498
499 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
500 file->f_flags |= O_DIRECT;
501
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502 gfs2_glock_dq_uninit(&i_gh);
503 }
504
505 return 0;
506
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400507fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400509fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500510 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 return error;
513}
514
515/**
516 * gfs2_close - called to close a struct file
517 * @inode: the inode the struct file belongs to
518 * @file: the struct file being closed
519 *
520 * Returns: errno
521 */
522
523static int gfs2_close(struct inode *inode, struct file *file)
524{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500525 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 struct gfs2_file *fp;
527
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500528 fp = file->private_data;
529 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
531 if (gfs2_assert_warn(sdp, fp))
532 return -EIO;
533
534 kfree(fp);
535
536 return 0;
537}
538
539/**
540 * gfs2_fsync - sync the dirty data for a file (across the cluster)
541 * @file: the file that points to the dentry (we ignore this)
542 * @dentry: the dentry that points to the inode to sync
543 *
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500544 * The VFS will flush "normal" data for us. We only need to worry
545 * about metadata here. For journaled data, we just do a log flush
546 * as we can't avoid it. Otherwise we can just bale out if datasync
547 * is set. For stuffed inodes we must flush the log in order to
548 * ensure that all data is on disk.
549 *
Steven Whitehouse34126f92006-12-07 09:13:14 -0500550 * The call to write_inode_now() is there to write back metadata and
551 * the inode itself. It does also try and write the data, but thats
552 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
553 * for us.
554 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 * Returns: errno
556 */
557
558static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
559{
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500560 struct inode *inode = dentry->d_inode;
561 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
562 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500564 if (gfs2_is_jdata(GFS2_I(inode))) {
565 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
566 return 0;
567 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500569 if (sync_state != 0) {
570 if (!datasync)
Steven Whitehouse34126f92006-12-07 09:13:14 -0500571 ret = write_inode_now(inode, 0);
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500572
573 if (gfs2_is_stuffed(GFS2_I(inode)))
574 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
575 }
576
577 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578}
579
580/**
Marc Eshel60446062007-01-15 18:33:36 -0500581 * gfs2_setlease - acquire/release a file lease
582 * @file: the file pointer
583 * @arg: lease type
584 * @fl: file lock
585 *
586 * Returns: errno
587 */
588
589static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
590{
591 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
592
593 /*
594 * We don't currently have a way to enforce a lease across the whole
595 * cluster; until we do, disable leases (by just returning -EINVAL),
596 * unless the administrator has requested purely local locking.
597 */
598 if (!sdp->sd_args.ar_localflocks)
599 return -EINVAL;
Christoph Hellwig0af1a452007-07-31 00:39:22 -0700600 return generic_setlease(file, arg, fl);
Marc Eshel60446062007-01-15 18:33:36 -0500601}
602
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000603static int gfs2_lm_plock_get(struct gfs2_sbd *sdp, struct lm_lockname *name,
604 struct file *file, struct file_lock *fl)
605{
606 int error = -EIO;
607 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
608 error = sdp->sd_lockstruct.ls_ops->lm_plock_get(
609 sdp->sd_lockstruct.ls_lockspace, name, file, fl);
610 return error;
611}
612
613static int gfs2_lm_plock(struct gfs2_sbd *sdp, struct lm_lockname *name,
614 struct file *file, int cmd, struct file_lock *fl)
615{
616 int error = -EIO;
617 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
618 error = sdp->sd_lockstruct.ls_ops->lm_plock(
619 sdp->sd_lockstruct.ls_lockspace, name, file, cmd, fl);
620 return error;
621}
622
623static int gfs2_lm_punlock(struct gfs2_sbd *sdp, struct lm_lockname *name,
624 struct file *file, struct file_lock *fl)
625{
626 int error = -EIO;
627 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
628 error = sdp->sd_lockstruct.ls_ops->lm_punlock(
629 sdp->sd_lockstruct.ls_lockspace, name, file, fl);
630 return error;
631}
632
Marc Eshel60446062007-01-15 18:33:36 -0500633/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 * gfs2_lock - acquire/release a posix lock on a file
635 * @file: the file pointer
636 * @cmd: either modify or retrieve lock state, possibly wait
637 * @fl: type and range of lock
638 *
639 * Returns: errno
640 */
641
642static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
643{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400644 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
645 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 struct lm_lockname name =
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100647 { .ln_number = ip->i_no_addr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648 .ln_type = LM_TYPE_PLOCK };
649
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 if (!(fl->fl_flags & FL_POSIX))
651 return -ENOLCK;
Pavel Emelyanov7afaac62007-10-01 14:41:13 -0700652 if (__mandatory_lock(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653 return -ENOLCK;
654
Marc Eshel586759f2006-11-14 16:37:25 -0500655 if (cmd == F_CANCELLK) {
656 /* Hack: */
657 cmd = F_SETLK;
658 fl->fl_type = F_UNLCK;
659 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 if (IS_GETLK(cmd))
661 return gfs2_lm_plock_get(sdp, &name, file, fl);
662 else if (fl->fl_type == F_UNLCK)
663 return gfs2_lm_punlock(sdp, &name, file, fl);
664 else
665 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
666}
667
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668static int do_flock(struct file *file, int cmd, struct file_lock *fl)
669{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500670 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
Josef Sipek81454092006-12-08 02:37:03 -0800672 struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 struct gfs2_glock *gl;
674 unsigned int state;
675 int flags;
676 int error = 0;
677
678 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100679 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680
Steven Whitehousef55ab262006-02-21 12:51:39 +0000681 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682
683 gl = fl_gh->gh_gl;
684 if (gl) {
685 if (fl_gh->gh_state == state)
686 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 flock_lock_file_wait(file,
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400688 &(struct file_lock){.fl_type = F_UNLCK});
Abhijith Dasb4c20162007-09-13 23:35:27 -0500689 gfs2_glock_dq_wait(fl_gh);
690 gfs2_holder_reinit(state, flags, fl_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 } else {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100692 error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
693 &gfs2_flock_glops, CREATE, &gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 if (error)
695 goto out;
Abhijith Dasb4c20162007-09-13 23:35:27 -0500696 gfs2_holder_init(gl, state, flags, fl_gh);
697 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699 error = gfs2_glock_nq(fl_gh);
700 if (error) {
701 gfs2_holder_uninit(fl_gh);
702 if (error == GLR_TRYFAILED)
703 error = -EAGAIN;
704 } else {
705 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400706 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 }
708
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400709out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000710 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711 return error;
712}
713
714static void do_unflock(struct file *file, struct file_lock *fl)
715{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500716 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
718
Steven Whitehousef55ab262006-02-21 12:51:39 +0000719 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 flock_lock_file_wait(file, fl);
721 if (fl_gh->gh_gl)
722 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000723 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724}
725
726/**
727 * gfs2_flock - acquire/release a flock lock on a file
728 * @file: the file pointer
729 * @cmd: either modify or retrieve lock state, possibly wait
730 * @fl: type and range of lock
731 *
732 * Returns: errno
733 */
734
735static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
736{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400737 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 if (!(fl->fl_flags & FL_FLOCK))
740 return -ENOLCK;
Pavel Emelyanov7afaac62007-10-01 14:41:13 -0700741 if (__mandatory_lock(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 return -ENOLCK;
743
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 if (fl->fl_type == F_UNLCK) {
745 do_unflock(file, fl);
746 return 0;
Steven Whitehoused00223f2006-10-02 10:28:05 -0400747 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 return do_flock(file, cmd, fl);
Steven Whitehoused00223f2006-10-02 10:28:05 -0400749 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750}
751
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400752const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400753 .llseek = gfs2_llseek,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400754 .read = do_sync_read,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400755 .aio_read = generic_file_aio_read,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400756 .write = do_sync_write,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400757 .aio_write = generic_file_aio_write,
758 .unlocked_ioctl = gfs2_ioctl,
759 .mmap = gfs2_mmap,
760 .open = gfs2_open,
761 .release = gfs2_close,
762 .fsync = gfs2_fsync,
763 .lock = gfs2_lock,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400764 .flock = gfs2_flock,
765 .splice_read = generic_file_splice_read,
766 .splice_write = generic_file_splice_write,
Marc Eshel60446062007-01-15 18:33:36 -0500767 .setlease = gfs2_setlease,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768};
769
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400770const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400771 .readdir = gfs2_readdir,
772 .unlocked_ioctl = gfs2_ioctl,
773 .open = gfs2_open,
774 .release = gfs2_close,
775 .fsync = gfs2_fsync,
776 .lock = gfs2_lock,
777 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778};
779
Wendy Chengc97bfe42007-11-29 17:56:51 -0500780const struct file_operations gfs2_file_fops_nolock = {
781 .llseek = gfs2_llseek,
782 .read = do_sync_read,
783 .aio_read = generic_file_aio_read,
784 .write = do_sync_write,
785 .aio_write = generic_file_aio_write,
786 .unlocked_ioctl = gfs2_ioctl,
787 .mmap = gfs2_mmap,
788 .open = gfs2_open,
789 .release = gfs2_close,
790 .fsync = gfs2_fsync,
791 .splice_read = generic_file_splice_read,
792 .splice_write = generic_file_splice_write,
793 .setlease = gfs2_setlease,
794};
795
796const struct file_operations gfs2_dir_fops_nolock = {
797 .readdir = gfs2_readdir,
798 .unlocked_ioctl = gfs2_ioctl,
799 .open = gfs2_open,
800 .release = gfs2_close,
801 .fsync = gfs2_fsync,
802};
803