blob: 48b248d7c8235b7991e6cbdbf7914f3959eb12a0 [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>
18#include <linux/smp_lock.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 "lm.h"
35#include "log.h"
36#include "meta_io.h"
37#include "ops_file.h"
38#include "ops_vm.h"
39#include "quota.h"
40#include "rgrp.h"
41#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050042#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050043#include "eaops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000044
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000045/*
46 * Most fields left uninitialised to catch anybody who tries to
47 * use them. f_flags set to prevent file_accessed() from touching
48 * any other part of this. Its use is purely as a flag so that we
49 * know (in readpage()) whether or not do to locking.
50 */
Steven Whitehousedd538c82006-09-04 14:53:30 -040051struct file gfs2_internal_file_sentinel = {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000052 .f_flags = O_NOATIME|O_RDONLY,
53};
54
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000055static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
56 unsigned long offset, unsigned long size)
57{
58 char *kaddr;
59 unsigned long count = desc->count;
60
61 if (size > count)
62 size = count;
63
64 kaddr = kmap(page);
Al Viro9c9ab3d2006-10-13 23:49:23 -040065 memcpy(desc->arg.data, kaddr + offset, size);
Steven Whitehouse26c1a572006-09-04 15:32:10 -040066 kunmap(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000067
Steven Whitehouse26c1a572006-09-04 15:32:10 -040068 desc->count = count - size;
69 desc->written += size;
70 desc->arg.buf += size;
71 return size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000072}
73
74int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
75 char *buf, loff_t *pos, unsigned size)
76{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040077 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000078 read_descriptor_t desc;
79 desc.written = 0;
Al Viro9c9ab3d2006-10-13 23:49:23 -040080 desc.arg.data = buf;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000081 desc.count = size;
82 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000083 do_generic_mapping_read(inode->i_mapping, ra_state,
Steven Whitehousedd538c82006-09-04 14:53:30 -040084 &gfs2_internal_file_sentinel, pos, &desc,
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000085 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000086 return desc.written ? desc.written : desc.error;
87}
David Teiglandb3b94fa2006-01-16 16:50:04 +000088
89/**
90 * gfs2_llseek - seek to a location in a file
91 * @file: the file
92 * @offset: the offset
93 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
94 *
95 * SEEK_END requires the glock for the file because it references the
96 * file's size.
97 *
98 * Returns: The new offset, or errno
99 */
100
101static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
102{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400103 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104 struct gfs2_holder i_gh;
105 loff_t error;
106
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107 if (origin == 2) {
108 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
109 &i_gh);
110 if (!error) {
111 error = remote_llseek(file, offset, origin);
112 gfs2_glock_dq_uninit(&i_gh);
113 }
114 } else
115 error = remote_llseek(file, offset, origin);
116
117 return error;
118}
119
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400121 * gfs2_readdir - Read directory entries from a directory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 * @file: The directory to read from
123 * @dirent: Buffer for dirents
124 * @filldir: Function used to do the copying
125 *
126 * Returns: errno
127 */
128
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400129static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500131 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400132 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400134 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 int error;
136
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
138 error = gfs2_glock_nq_atime(&d_gh);
139 if (error) {
140 gfs2_holder_uninit(&d_gh);
141 return error;
142 }
143
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000144 error = gfs2_dir_read(dir, &offset, dirent, filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145
146 gfs2_glock_dq_uninit(&d_gh);
147
148 file->f_pos = offset;
149
150 return error;
151}
152
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400153/**
154 * fsflags_cvt
155 * @table: A table of 32 u32 flags
156 * @val: a 32 bit value to convert
157 *
158 * This function can be used to convert between fsflags values and
159 * GFS2's own flags values.
160 *
161 * Returns: the converted flags
162 */
163static u32 fsflags_cvt(const u32 *table, u32 val)
164{
165 u32 res = 0;
166 while(val) {
167 if (val & 1)
168 res |= *table;
169 table++;
170 val >>= 1;
171 }
172 return res;
173}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400175static const u32 fsflags_to_gfs2[32] = {
176 [3] = GFS2_DIF_SYNC,
177 [4] = GFS2_DIF_IMMUTABLE,
178 [5] = GFS2_DIF_APPENDONLY,
179 [7] = GFS2_DIF_NOATIME,
180 [12] = GFS2_DIF_EXHASH,
181 [14] = GFS2_DIF_JDATA,
182 [20] = GFS2_DIF_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500183};
184
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400185static const u32 gfs2_to_fsflags[32] = {
186 [gfs2fl_Sync] = FS_SYNC_FL,
187 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
188 [gfs2fl_AppendOnly] = FS_APPEND_FL,
189 [gfs2fl_NoAtime] = FS_NOATIME_FL,
190 [gfs2fl_ExHash] = FS_INDEX_FL,
191 [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
192 [gfs2fl_Directio] = FS_DIRECTIO_FL,
193 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
194 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500195};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500196
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400197static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500198{
Josef Sipek81454092006-12-08 02:37:03 -0800199 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400200 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500201 struct gfs2_holder gh;
202 int error;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400203 u32 fsflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500204
205 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500206 error = gfs2_glock_nq_atime(&gh);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500207 if (error)
208 return error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400209
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400210 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
211 if (put_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500212 error = -EFAULT;
213
214 gfs2_glock_dq_m(1, &gh);
215 gfs2_holder_uninit(&gh);
216 return error;
217}
218
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500219void gfs2_set_inode_flags(struct inode *inode)
220{
221 struct gfs2_inode *ip = GFS2_I(inode);
222 struct gfs2_dinode_host *di = &ip->i_di;
223 unsigned int flags = inode->i_flags;
224
225 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
226 if (di->di_flags & GFS2_DIF_IMMUTABLE)
227 flags |= S_IMMUTABLE;
228 if (di->di_flags & GFS2_DIF_APPENDONLY)
229 flags |= S_APPEND;
230 if (di->di_flags & GFS2_DIF_NOATIME)
231 flags |= S_NOATIME;
232 if (di->di_flags & GFS2_DIF_SYNC)
233 flags |= S_SYNC;
234 inode->i_flags = flags;
235}
236
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500237/* Flags that can be set by user space */
238#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
239 GFS2_DIF_DIRECTIO| \
240 GFS2_DIF_IMMUTABLE| \
241 GFS2_DIF_APPENDONLY| \
242 GFS2_DIF_NOATIME| \
243 GFS2_DIF_SYNC| \
244 GFS2_DIF_SYSTEM| \
245 GFS2_DIF_INHERIT_DIRECTIO| \
246 GFS2_DIF_INHERIT_JDATA)
247
248/**
249 * gfs2_set_flags - set flags on an inode
250 * @inode: The inode
251 * @flags: The flags to set
252 * @mask: Indicates which flags are valid
253 *
254 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400255static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500256{
Josef Sipek81454092006-12-08 02:37:03 -0800257 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400258 struct gfs2_inode *ip = GFS2_I(inode);
259 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500260 struct buffer_head *bh;
261 struct gfs2_holder gh;
262 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400263 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500264
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500265 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
Abhijith Das52f341c2006-07-21 02:03:21 -0400266 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500267 return error;
268
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400269 flags = ip->i_di.di_flags;
270 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500271 if ((new_flags ^ flags) == 0)
272 goto out;
273
Steven Whitehouse4bcf7092006-04-25 13:20:27 -0400274 if (S_ISDIR(inode->i_mode)) {
275 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
276 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
277 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
278 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
279 }
280
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500281 error = -EINVAL;
282 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
283 goto out;
284
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500285 error = -EPERM;
286 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
287 goto out;
288 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
289 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400290 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400291 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500292 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400293 if (!IS_IMMUTABLE(inode)) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400294 error = permission(inode, MAY_WRITE, NULL);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400295 if (error)
296 goto out;
297 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500298
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400299 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500300 if (error)
301 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400302 error = gfs2_meta_inode_buffer(ip, &bh);
303 if (error)
304 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500305 gfs2_trans_add_bh(ip->i_gl, bh, 1);
306 ip->i_di.di_flags = new_flags;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500307 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500308 brelse(bh);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500309 gfs2_set_inode_flags(inode);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400310out_trans_end:
311 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500312out:
313 gfs2_glock_dq_uninit(&gh);
314 return error;
315}
316
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400317static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500318{
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400319 u32 fsflags, gfsflags;
320 if (get_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500321 return -EFAULT;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400322 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400323 return do_gfs2_set_flags(filp, gfsflags, ~0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500324}
325
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400326static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500327{
328 switch(cmd) {
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400329 case FS_IOC_GETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400330 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400331 case FS_IOC_SETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400332 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500333 }
334 return -ENOTTY;
335}
336
337
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338/**
339 * gfs2_mmap -
340 * @file: The file to map
341 * @vma: The VMA which described the mapping
342 *
343 * Returns: 0 or error code
344 */
345
346static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
347{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400348 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349 struct gfs2_holder i_gh;
350 int error;
351
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
353 error = gfs2_glock_nq_atime(&i_gh);
354 if (error) {
355 gfs2_holder_uninit(&i_gh);
356 return error;
357 }
358
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000359 /* This is VM_MAYWRITE instead of VM_WRITE because a call
360 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000362 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
363 (VM_MAYSHARE | VM_MAYWRITE))
364 vma->vm_ops = &gfs2_vm_ops_sharewrite;
365 else
366 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367
368 gfs2_glock_dq_uninit(&i_gh);
369
370 return error;
371}
372
373/**
374 * gfs2_open - open a file
375 * @inode: the inode to open
376 * @file: the struct file for this opening
377 *
378 * Returns: errno
379 */
380
381static int gfs2_open(struct inode *inode, struct file *file)
382{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400383 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 struct gfs2_holder i_gh;
385 struct gfs2_file *fp;
386 int error;
387
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
389 if (!fp)
390 return -ENOMEM;
391
Steven Whitehousef55ab262006-02-21 12:51:39 +0000392 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400394 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500395 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500397 if (S_ISREG(ip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
399 &i_gh);
400 if (error)
401 goto fail;
402
403 if (!(file->f_flags & O_LARGEFILE) &&
404 ip->i_di.di_size > MAX_NON_LFS) {
405 error = -EFBIG;
406 goto fail_gunlock;
407 }
408
409 /* Listen to the Direct I/O flag */
410
411 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
412 file->f_flags |= O_DIRECT;
413
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 gfs2_glock_dq_uninit(&i_gh);
415 }
416
417 return 0;
418
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400419fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400421fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500422 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 return error;
425}
426
427/**
428 * gfs2_close - called to close a struct file
429 * @inode: the inode the struct file belongs to
430 * @file: the struct file being closed
431 *
432 * Returns: errno
433 */
434
435static int gfs2_close(struct inode *inode, struct file *file)
436{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500437 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438 struct gfs2_file *fp;
439
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500440 fp = file->private_data;
441 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442
443 if (gfs2_assert_warn(sdp, fp))
444 return -EIO;
445
446 kfree(fp);
447
448 return 0;
449}
450
451/**
452 * gfs2_fsync - sync the dirty data for a file (across the cluster)
453 * @file: the file that points to the dentry (we ignore this)
454 * @dentry: the dentry that points to the inode to sync
455 *
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500456 * The VFS will flush "normal" data for us. We only need to worry
457 * about metadata here. For journaled data, we just do a log flush
458 * as we can't avoid it. Otherwise we can just bale out if datasync
459 * is set. For stuffed inodes we must flush the log in order to
460 * ensure that all data is on disk.
461 *
Steven Whitehouse34126f92006-12-07 09:13:14 -0500462 * The call to write_inode_now() is there to write back metadata and
463 * the inode itself. It does also try and write the data, but thats
464 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
465 * for us.
466 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 * Returns: errno
468 */
469
470static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
471{
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500472 struct inode *inode = dentry->d_inode;
473 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
474 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500476 if (gfs2_is_jdata(GFS2_I(inode))) {
477 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
478 return 0;
479 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500481 if (sync_state != 0) {
482 if (!datasync)
Steven Whitehouse34126f92006-12-07 09:13:14 -0500483 ret = write_inode_now(inode, 0);
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500484
485 if (gfs2_is_stuffed(GFS2_I(inode)))
486 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
487 }
488
489 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490}
491
492/**
493 * gfs2_lock - acquire/release a posix lock on a file
494 * @file: the file pointer
495 * @cmd: either modify or retrieve lock state, possibly wait
496 * @fl: type and range of lock
497 *
498 * Returns: errno
499 */
500
501static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
502{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400503 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
504 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 struct lm_lockname name =
506 { .ln_number = ip->i_num.no_addr,
507 .ln_type = LM_TYPE_PLOCK };
508
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 if (!(fl->fl_flags & FL_POSIX))
510 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500511 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 return -ENOLCK;
513
514 if (sdp->sd_args.ar_localflocks) {
515 if (IS_GETLK(cmd)) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500516 posix_test_lock(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517 return 0;
518 } else {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500519 return posix_lock_file_wait(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 }
521 }
522
523 if (IS_GETLK(cmd))
524 return gfs2_lm_plock_get(sdp, &name, file, fl);
525 else if (fl->fl_type == F_UNLCK)
526 return gfs2_lm_punlock(sdp, &name, file, fl);
527 else
528 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
529}
530
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531static int do_flock(struct file *file, int cmd, struct file_lock *fl)
532{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500533 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
Josef Sipek81454092006-12-08 02:37:03 -0800535 struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 struct gfs2_glock *gl;
537 unsigned int state;
538 int flags;
539 int error = 0;
540
541 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400542 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543
Steven Whitehousef55ab262006-02-21 12:51:39 +0000544 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545
546 gl = fl_gh->gh_gl;
547 if (gl) {
548 if (fl_gh->gh_state == state)
549 goto out;
550 gfs2_glock_hold(gl);
551 flock_lock_file_wait(file,
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400552 &(struct file_lock){.fl_type = F_UNLCK});
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 gfs2_glock_dq_uninit(fl_gh);
554 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400555 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 ip->i_num.no_addr, &gfs2_flock_glops,
557 CREATE, &gl);
558 if (error)
559 goto out;
560 }
561
562 gfs2_holder_init(gl, state, flags, fl_gh);
563 gfs2_glock_put(gl);
564
565 error = gfs2_glock_nq(fl_gh);
566 if (error) {
567 gfs2_holder_uninit(fl_gh);
568 if (error == GLR_TRYFAILED)
569 error = -EAGAIN;
570 } else {
571 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400572 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 }
574
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400575out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000576 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 return error;
578}
579
580static void do_unflock(struct file *file, struct file_lock *fl)
581{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500582 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
584
Steven Whitehousef55ab262006-02-21 12:51:39 +0000585 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 flock_lock_file_wait(file, fl);
587 if (fl_gh->gh_gl)
588 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000589 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590}
591
592/**
593 * gfs2_flock - acquire/release a flock lock on a file
594 * @file: the file pointer
595 * @cmd: either modify or retrieve lock state, possibly wait
596 * @fl: type and range of lock
597 *
598 * Returns: errno
599 */
600
601static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
602{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400603 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
604 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 if (!(fl->fl_flags & FL_FLOCK))
607 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500608 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 return -ENOLCK;
610
611 if (sdp->sd_args.ar_localflocks)
612 return flock_lock_file_wait(file, fl);
613
614 if (fl->fl_type == F_UNLCK) {
615 do_unflock(file, fl);
616 return 0;
Steven Whitehoused00223f2006-10-02 10:28:05 -0400617 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 return do_flock(file, cmd, fl);
Steven Whitehoused00223f2006-10-02 10:28:05 -0400619 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620}
621
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400622const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400623 .llseek = gfs2_llseek,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400624 .read = do_sync_read,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400625 .aio_read = generic_file_aio_read,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400626 .write = do_sync_write,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400627 .aio_write = generic_file_aio_write,
628 .unlocked_ioctl = gfs2_ioctl,
629 .mmap = gfs2_mmap,
630 .open = gfs2_open,
631 .release = gfs2_close,
632 .fsync = gfs2_fsync,
633 .lock = gfs2_lock,
634 .sendfile = generic_file_sendfile,
635 .flock = gfs2_flock,
636 .splice_read = generic_file_splice_read,
637 .splice_write = generic_file_splice_write,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638};
639
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400640const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400641 .readdir = gfs2_readdir,
642 .unlocked_ioctl = gfs2_ioctl,
643 .open = gfs2_open,
644 .release = gfs2_close,
645 .fsync = gfs2_fsync,
646 .lock = gfs2_lock,
647 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648};
649