blob: 94d76ace0b95b840427552c33636f3c0bab46fd3 [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>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000018#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050020#include <linux/ext2_fs.h>
21#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020022#include <linux/lm_interface.h>
Steven Whitehouse33c3de32006-11-30 10:14:32 -050023#include <linux/writeback.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include <asm/uaccess.h>
25
26#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "bmap.h"
29#include "dir.h"
30#include "glock.h"
31#include "glops.h"
32#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "lm.h"
34#include "log.h"
35#include "meta_io.h"
36#include "ops_file.h"
37#include "ops_vm.h"
38#include "quota.h"
39#include "rgrp.h"
40#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050041#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050042#include "eaops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000043
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000044/*
45 * Most fields left uninitialised to catch anybody who tries to
46 * use them. f_flags set to prevent file_accessed() from touching
47 * any other part of this. Its use is purely as a flag so that we
48 * know (in readpage()) whether or not do to locking.
49 */
Steven Whitehousedd538c82006-09-04 14:53:30 -040050struct file gfs2_internal_file_sentinel = {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000051 .f_flags = O_NOATIME|O_RDONLY,
52};
53
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000054static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
55 unsigned long offset, unsigned long size)
56{
57 char *kaddr;
58 unsigned long count = desc->count;
59
60 if (size > count)
61 size = count;
62
63 kaddr = kmap(page);
Al Viro9c9ab3d2006-10-13 23:49:23 -040064 memcpy(desc->arg.data, kaddr + offset, size);
Steven Whitehouse26c1a572006-09-04 15:32:10 -040065 kunmap(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000066
Steven Whitehouse26c1a572006-09-04 15:32:10 -040067 desc->count = count - size;
68 desc->written += size;
69 desc->arg.buf += size;
70 return size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000071}
72
73int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
74 char *buf, loff_t *pos, unsigned size)
75{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040076 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000077 read_descriptor_t desc;
78 desc.written = 0;
Al Viro9c9ab3d2006-10-13 23:49:23 -040079 desc.arg.data = buf;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000080 desc.count = size;
81 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000082 do_generic_mapping_read(inode->i_mapping, ra_state,
Steven Whitehousedd538c82006-09-04 14:53:30 -040083 &gfs2_internal_file_sentinel, pos, &desc,
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000084 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000085 return desc.written ? desc.written : desc.error;
86}
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
88/**
89 * gfs2_llseek - seek to a location in a file
90 * @file: the file
91 * @offset: the offset
92 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
93 *
94 * SEEK_END requires the glock for the file because it references the
95 * file's size.
96 *
97 * Returns: The new offset, or errno
98 */
99
100static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
101{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400102 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 struct gfs2_holder i_gh;
104 loff_t error;
105
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106 if (origin == 2) {
107 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
108 &i_gh);
109 if (!error) {
110 error = remote_llseek(file, offset, origin);
111 gfs2_glock_dq_uninit(&i_gh);
112 }
113 } else
114 error = remote_llseek(file, offset, origin);
115
116 return error;
117}
118
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400120 * gfs2_readdir - Read directory entries from a directory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121 * @file: The directory to read from
122 * @dirent: Buffer for dirents
123 * @filldir: Function used to do the copying
124 *
125 * Returns: errno
126 */
127
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400128static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500130 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400131 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400133 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 int error;
135
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
137 error = gfs2_glock_nq_atime(&d_gh);
138 if (error) {
139 gfs2_holder_uninit(&d_gh);
140 return error;
141 }
142
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000143 error = gfs2_dir_read(dir, &offset, dirent, filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144
145 gfs2_glock_dq_uninit(&d_gh);
146
147 file->f_pos = offset;
148
149 return error;
150}
151
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400152/**
153 * fsflags_cvt
154 * @table: A table of 32 u32 flags
155 * @val: a 32 bit value to convert
156 *
157 * This function can be used to convert between fsflags values and
158 * GFS2's own flags values.
159 *
160 * Returns: the converted flags
161 */
162static u32 fsflags_cvt(const u32 *table, u32 val)
163{
164 u32 res = 0;
165 while(val) {
166 if (val & 1)
167 res |= *table;
168 table++;
169 val >>= 1;
170 }
171 return res;
172}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400174static const u32 fsflags_to_gfs2[32] = {
175 [3] = GFS2_DIF_SYNC,
176 [4] = GFS2_DIF_IMMUTABLE,
177 [5] = GFS2_DIF_APPENDONLY,
178 [7] = GFS2_DIF_NOATIME,
179 [12] = GFS2_DIF_EXHASH,
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100180 [14] = GFS2_DIF_INHERIT_JDATA,
181 [20] = GFS2_DIF_INHERIT_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500182};
183
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400184static const u32 gfs2_to_fsflags[32] = {
185 [gfs2fl_Sync] = FS_SYNC_FL,
186 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
187 [gfs2fl_AppendOnly] = FS_APPEND_FL,
188 [gfs2fl_NoAtime] = FS_NOATIME_FL,
189 [gfs2fl_ExHash] = FS_INDEX_FL,
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400190 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
191 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500192};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500193
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400194static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500195{
Josef Sipek81454092006-12-08 02:37:03 -0800196 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400197 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500198 struct gfs2_holder gh;
199 int error;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400200 u32 fsflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500201
202 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500203 error = gfs2_glock_nq_atime(&gh);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500204 if (error)
205 return error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400206
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400207 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100208 if (!S_ISDIR(inode->i_mode)) {
209 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
210 fsflags |= FS_JOURNAL_DATA_FL;
211 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
212 fsflags |= FS_DIRECTIO_FL;
213 }
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400214 if (put_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500215 error = -EFAULT;
216
217 gfs2_glock_dq_m(1, &gh);
218 gfs2_holder_uninit(&gh);
219 return error;
220}
221
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500222void gfs2_set_inode_flags(struct inode *inode)
223{
224 struct gfs2_inode *ip = GFS2_I(inode);
225 struct gfs2_dinode_host *di = &ip->i_di;
226 unsigned int flags = inode->i_flags;
227
228 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
229 if (di->di_flags & GFS2_DIF_IMMUTABLE)
230 flags |= S_IMMUTABLE;
231 if (di->di_flags & GFS2_DIF_APPENDONLY)
232 flags |= S_APPEND;
233 if (di->di_flags & GFS2_DIF_NOATIME)
234 flags |= S_NOATIME;
235 if (di->di_flags & GFS2_DIF_SYNC)
236 flags |= S_SYNC;
237 inode->i_flags = flags;
238}
239
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500240/* Flags that can be set by user space */
241#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
242 GFS2_DIF_DIRECTIO| \
243 GFS2_DIF_IMMUTABLE| \
244 GFS2_DIF_APPENDONLY| \
245 GFS2_DIF_NOATIME| \
246 GFS2_DIF_SYNC| \
247 GFS2_DIF_SYSTEM| \
248 GFS2_DIF_INHERIT_DIRECTIO| \
249 GFS2_DIF_INHERIT_JDATA)
250
251/**
252 * gfs2_set_flags - set flags on an inode
253 * @inode: The inode
254 * @flags: The flags to set
255 * @mask: Indicates which flags are valid
256 *
257 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400258static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500259{
Josef Sipek81454092006-12-08 02:37:03 -0800260 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400261 struct gfs2_inode *ip = GFS2_I(inode);
262 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500263 struct buffer_head *bh;
264 struct gfs2_holder gh;
265 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400266 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500267
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500268 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
Abhijith Das52f341c2006-07-21 02:03:21 -0400269 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500270 return error;
271
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400272 flags = ip->i_di.di_flags;
273 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500274 if ((new_flags ^ flags) == 0)
275 goto out;
276
277 error = -EINVAL;
278 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
279 goto out;
280
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500281 error = -EPERM;
282 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
283 goto out;
284 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
285 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400286 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400287 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500288 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400289 if (!IS_IMMUTABLE(inode)) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400290 error = permission(inode, MAY_WRITE, NULL);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400291 if (error)
292 goto out;
293 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500294
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400295 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500296 if (error)
297 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400298 error = gfs2_meta_inode_buffer(ip, &bh);
299 if (error)
300 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500301 gfs2_trans_add_bh(ip->i_gl, bh, 1);
302 ip->i_di.di_flags = new_flags;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500303 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500304 brelse(bh);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500305 gfs2_set_inode_flags(inode);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400306out_trans_end:
307 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500308out:
309 gfs2_glock_dq_uninit(&gh);
310 return error;
311}
312
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400313static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500314{
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100315 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400316 u32 fsflags, gfsflags;
317 if (get_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500318 return -EFAULT;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400319 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
Steven Whitehouseb9af7ca2007-07-18 11:40:06 +0100320 if (!S_ISDIR(inode->i_mode)) {
321 if (gfsflags & GFS2_DIF_INHERIT_JDATA)
322 gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
323 if (gfsflags & GFS2_DIF_INHERIT_DIRECTIO)
324 gfsflags ^= (GFS2_DIF_DIRECTIO | GFS2_DIF_INHERIT_DIRECTIO);
325 return do_gfs2_set_flags(filp, gfsflags, ~0);
326 }
327 return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500328}
329
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400330static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500331{
332 switch(cmd) {
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400333 case FS_IOC_GETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400334 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400335 case FS_IOC_SETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400336 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500337 }
338 return -ENOTTY;
339}
340
341
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342/**
343 * gfs2_mmap -
344 * @file: The file to map
345 * @vma: The VMA which described the mapping
346 *
347 * Returns: 0 or error code
348 */
349
350static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
351{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400352 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 struct gfs2_holder i_gh;
354 int error;
355
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
357 error = gfs2_glock_nq_atime(&i_gh);
358 if (error) {
359 gfs2_holder_uninit(&i_gh);
360 return error;
361 }
362
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000363 /* This is VM_MAYWRITE instead of VM_WRITE because a call
364 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000366 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
367 (VM_MAYSHARE | VM_MAYWRITE))
368 vma->vm_ops = &gfs2_vm_ops_sharewrite;
369 else
370 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371
372 gfs2_glock_dq_uninit(&i_gh);
373
374 return error;
375}
376
377/**
378 * gfs2_open - open a file
379 * @inode: the inode to open
380 * @file: the struct file for this opening
381 *
382 * Returns: errno
383 */
384
385static int gfs2_open(struct inode *inode, struct file *file)
386{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400387 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 struct gfs2_holder i_gh;
389 struct gfs2_file *fp;
390 int error;
391
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
393 if (!fp)
394 return -ENOMEM;
395
Steven Whitehousef55ab262006-02-21 12:51:39 +0000396 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400398 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500399 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500401 if (S_ISREG(ip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
403 &i_gh);
404 if (error)
405 goto fail;
406
407 if (!(file->f_flags & O_LARGEFILE) &&
408 ip->i_di.di_size > MAX_NON_LFS) {
409 error = -EFBIG;
410 goto fail_gunlock;
411 }
412
413 /* Listen to the Direct I/O flag */
414
415 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
416 file->f_flags |= O_DIRECT;
417
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 gfs2_glock_dq_uninit(&i_gh);
419 }
420
421 return 0;
422
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400423fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400425fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500426 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428 return error;
429}
430
431/**
432 * gfs2_close - called to close a struct file
433 * @inode: the inode the struct file belongs to
434 * @file: the struct file being closed
435 *
436 * Returns: errno
437 */
438
439static int gfs2_close(struct inode *inode, struct file *file)
440{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500441 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 struct gfs2_file *fp;
443
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500444 fp = file->private_data;
445 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446
447 if (gfs2_assert_warn(sdp, fp))
448 return -EIO;
449
450 kfree(fp);
451
452 return 0;
453}
454
455/**
456 * gfs2_fsync - sync the dirty data for a file (across the cluster)
457 * @file: the file that points to the dentry (we ignore this)
458 * @dentry: the dentry that points to the inode to sync
459 *
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500460 * The VFS will flush "normal" data for us. We only need to worry
461 * about metadata here. For journaled data, we just do a log flush
462 * as we can't avoid it. Otherwise we can just bale out if datasync
463 * is set. For stuffed inodes we must flush the log in order to
464 * ensure that all data is on disk.
465 *
Steven Whitehouse34126f92006-12-07 09:13:14 -0500466 * The call to write_inode_now() is there to write back metadata and
467 * the inode itself. It does also try and write the data, but thats
468 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
469 * for us.
470 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 * Returns: errno
472 */
473
474static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
475{
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500476 struct inode *inode = dentry->d_inode;
477 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
478 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500480 if (gfs2_is_jdata(GFS2_I(inode))) {
481 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
482 return 0;
483 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500485 if (sync_state != 0) {
486 if (!datasync)
Steven Whitehouse34126f92006-12-07 09:13:14 -0500487 ret = write_inode_now(inode, 0);
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500488
489 if (gfs2_is_stuffed(GFS2_I(inode)))
490 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
491 }
492
493 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494}
495
496/**
Marc Eshel60446062007-01-15 18:33:36 -0500497 * gfs2_setlease - acquire/release a file lease
498 * @file: the file pointer
499 * @arg: lease type
500 * @fl: file lock
501 *
502 * Returns: errno
503 */
504
505static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
506{
507 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
508
509 /*
510 * We don't currently have a way to enforce a lease across the whole
511 * cluster; until we do, disable leases (by just returning -EINVAL),
512 * unless the administrator has requested purely local locking.
513 */
514 if (!sdp->sd_args.ar_localflocks)
515 return -EINVAL;
Christoph Hellwig0af1a452007-07-31 00:39:22 -0700516 return generic_setlease(file, arg, fl);
Marc Eshel60446062007-01-15 18:33:36 -0500517}
518
519/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 * gfs2_lock - acquire/release a posix lock on a file
521 * @file: the file pointer
522 * @cmd: either modify or retrieve lock state, possibly wait
523 * @fl: type and range of lock
524 *
525 * Returns: errno
526 */
527
528static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
529{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400530 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
531 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 struct lm_lockname name =
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100533 { .ln_number = ip->i_no_addr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 .ln_type = LM_TYPE_PLOCK };
535
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 if (!(fl->fl_flags & FL_POSIX))
537 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500538 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 return -ENOLCK;
540
541 if (sdp->sd_args.ar_localflocks) {
542 if (IS_GETLK(cmd)) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500543 posix_test_lock(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 return 0;
545 } else {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500546 return posix_lock_file_wait(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 }
548 }
549
Marc Eshel586759f2006-11-14 16:37:25 -0500550 if (cmd == F_CANCELLK) {
551 /* Hack: */
552 cmd = F_SETLK;
553 fl->fl_type = F_UNLCK;
554 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 if (IS_GETLK(cmd))
556 return gfs2_lm_plock_get(sdp, &name, file, fl);
557 else if (fl->fl_type == F_UNLCK)
558 return gfs2_lm_punlock(sdp, &name, file, fl);
559 else
560 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
561}
562
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563static int do_flock(struct file *file, int cmd, struct file_lock *fl)
564{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500565 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
Josef Sipek81454092006-12-08 02:37:03 -0800567 struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 struct gfs2_glock *gl;
569 unsigned int state;
570 int flags;
571 int error = 0;
572
573 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400574 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575
Steven Whitehousef55ab262006-02-21 12:51:39 +0000576 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577
578 gl = fl_gh->gh_gl;
579 if (gl) {
580 if (fl_gh->gh_state == state)
581 goto out;
582 gfs2_glock_hold(gl);
583 flock_lock_file_wait(file,
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400584 &(struct file_lock){.fl_type = F_UNLCK});
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 gfs2_glock_dq_uninit(fl_gh);
586 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400587 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100588 ip->i_no_addr, &gfs2_flock_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 CREATE, &gl);
590 if (error)
591 goto out;
592 }
593
594 gfs2_holder_init(gl, state, flags, fl_gh);
595 gfs2_glock_put(gl);
596
597 error = gfs2_glock_nq(fl_gh);
598 if (error) {
599 gfs2_holder_uninit(fl_gh);
600 if (error == GLR_TRYFAILED)
601 error = -EAGAIN;
602 } else {
603 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400604 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 }
606
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400607out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000608 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 return error;
610}
611
612static void do_unflock(struct file *file, struct file_lock *fl)
613{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500614 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
616
Steven Whitehousef55ab262006-02-21 12:51:39 +0000617 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 flock_lock_file_wait(file, fl);
619 if (fl_gh->gh_gl)
620 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000621 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622}
623
624/**
625 * gfs2_flock - acquire/release a flock lock on a file
626 * @file: the file pointer
627 * @cmd: either modify or retrieve lock state, possibly wait
628 * @fl: type and range of lock
629 *
630 * Returns: errno
631 */
632
633static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
634{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400635 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
636 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 if (!(fl->fl_flags & FL_FLOCK))
639 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500640 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 return -ENOLCK;
642
643 if (sdp->sd_args.ar_localflocks)
644 return flock_lock_file_wait(file, fl);
645
646 if (fl->fl_type == F_UNLCK) {
647 do_unflock(file, fl);
648 return 0;
Steven Whitehoused00223f2006-10-02 10:28:05 -0400649 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 return do_flock(file, cmd, fl);
Steven Whitehoused00223f2006-10-02 10:28:05 -0400651 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652}
653
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400654const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400655 .llseek = gfs2_llseek,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400656 .read = do_sync_read,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400657 .aio_read = generic_file_aio_read,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400658 .write = do_sync_write,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400659 .aio_write = generic_file_aio_write,
660 .unlocked_ioctl = gfs2_ioctl,
661 .mmap = gfs2_mmap,
662 .open = gfs2_open,
663 .release = gfs2_close,
664 .fsync = gfs2_fsync,
665 .lock = gfs2_lock,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400666 .flock = gfs2_flock,
667 .splice_read = generic_file_splice_read,
668 .splice_write = generic_file_splice_write,
Marc Eshel60446062007-01-15 18:33:36 -0500669 .setlease = gfs2_setlease,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670};
671
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400672const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400673 .readdir = gfs2_readdir,
674 .unlocked_ioctl = gfs2_ioctl,
675 .open = gfs2_open,
676 .release = gfs2_close,
677 .fsync = gfs2_fsync,
678 .lock = gfs2_lock,
679 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680};
681