blob: c996aa739a0515f5c11229591a83805a4abb6f99 [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
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/pagemap.h>
16#include <linux/uio.h>
17#include <linux/blkdev.h>
18#include <linux/mm.h>
19#include <linux/smp_lock.h>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000020#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050022#include <linux/ext2_fs.h>
23#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020024#include <linux/lm_interface.h>
Steven Whitehouse33c3de32006-11-30 10:14:32 -050025#include <linux/writeback.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include <asm/uaccess.h>
27
28#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030#include "bmap.h"
31#include "dir.h"
32#include "glock.h"
33#include "glops.h"
34#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035#include "lm.h"
36#include "log.h"
37#include "meta_io.h"
38#include "ops_file.h"
39#include "ops_vm.h"
40#include "quota.h"
41#include "rgrp.h"
42#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050043#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050044#include "eaops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000045
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000046/*
47 * Most fields left uninitialised to catch anybody who tries to
48 * use them. f_flags set to prevent file_accessed() from touching
49 * any other part of this. Its use is purely as a flag so that we
50 * know (in readpage()) whether or not do to locking.
51 */
Steven Whitehousedd538c82006-09-04 14:53:30 -040052struct file gfs2_internal_file_sentinel = {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000053 .f_flags = O_NOATIME|O_RDONLY,
54};
55
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000056static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
57 unsigned long offset, unsigned long size)
58{
59 char *kaddr;
60 unsigned long count = desc->count;
61
62 if (size > count)
63 size = count;
64
65 kaddr = kmap(page);
Al Viro9c9ab3d2006-10-13 23:49:23 -040066 memcpy(desc->arg.data, kaddr + offset, size);
Steven Whitehouse26c1a572006-09-04 15:32:10 -040067 kunmap(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000068
Steven Whitehouse26c1a572006-09-04 15:32:10 -040069 desc->count = count - size;
70 desc->written += size;
71 desc->arg.buf += size;
72 return size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000073}
74
75int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
76 char *buf, loff_t *pos, unsigned size)
77{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040078 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000079 read_descriptor_t desc;
80 desc.written = 0;
Al Viro9c9ab3d2006-10-13 23:49:23 -040081 desc.arg.data = buf;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000082 desc.count = size;
83 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000084 do_generic_mapping_read(inode->i_mapping, ra_state,
Steven Whitehousedd538c82006-09-04 14:53:30 -040085 &gfs2_internal_file_sentinel, pos, &desc,
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000086 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000087 return desc.written ? desc.written : desc.error;
88}
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
90/**
91 * gfs2_llseek - seek to a location in a file
92 * @file: the file
93 * @offset: the offset
94 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
95 *
96 * SEEK_END requires the glock for the file because it references the
97 * file's size.
98 *
99 * Returns: The new offset, or errno
100 */
101
102static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
103{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400104 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105 struct gfs2_holder i_gh;
106 loff_t error;
107
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 if (origin == 2) {
109 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
110 &i_gh);
111 if (!error) {
112 error = remote_llseek(file, offset, origin);
113 gfs2_glock_dq_uninit(&i_gh);
114 }
115 } else
116 error = remote_llseek(file, offset, origin);
117
118 return error;
119}
120
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400122 * gfs2_readdir - Read directory entries from a directory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 * @file: The directory to read from
124 * @dirent: Buffer for dirents
125 * @filldir: Function used to do the copying
126 *
127 * Returns: errno
128 */
129
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400130static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500132 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400133 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400135 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 int error;
137
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
139 error = gfs2_glock_nq_atime(&d_gh);
140 if (error) {
141 gfs2_holder_uninit(&d_gh);
142 return error;
143 }
144
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000145 error = gfs2_dir_read(dir, &offset, dirent, filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146
147 gfs2_glock_dq_uninit(&d_gh);
148
149 file->f_pos = offset;
150
151 return error;
152}
153
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400154/**
155 * fsflags_cvt
156 * @table: A table of 32 u32 flags
157 * @val: a 32 bit value to convert
158 *
159 * This function can be used to convert between fsflags values and
160 * GFS2's own flags values.
161 *
162 * Returns: the converted flags
163 */
164static u32 fsflags_cvt(const u32 *table, u32 val)
165{
166 u32 res = 0;
167 while(val) {
168 if (val & 1)
169 res |= *table;
170 table++;
171 val >>= 1;
172 }
173 return res;
174}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400176static const u32 fsflags_to_gfs2[32] = {
177 [3] = GFS2_DIF_SYNC,
178 [4] = GFS2_DIF_IMMUTABLE,
179 [5] = GFS2_DIF_APPENDONLY,
180 [7] = GFS2_DIF_NOATIME,
181 [12] = GFS2_DIF_EXHASH,
182 [14] = GFS2_DIF_JDATA,
183 [20] = GFS2_DIF_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500184};
185
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400186static const u32 gfs2_to_fsflags[32] = {
187 [gfs2fl_Sync] = FS_SYNC_FL,
188 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
189 [gfs2fl_AppendOnly] = FS_APPEND_FL,
190 [gfs2fl_NoAtime] = FS_NOATIME_FL,
191 [gfs2fl_ExHash] = FS_INDEX_FL,
192 [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
193 [gfs2fl_Directio] = FS_DIRECTIO_FL,
194 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
195 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500196};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500197
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400198static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500199{
Josef Sipek81454092006-12-08 02:37:03 -0800200 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400201 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500202 struct gfs2_holder gh;
203 int error;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400204 u32 fsflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500205
206 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500207 error = gfs2_glock_nq_atime(&gh);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500208 if (error)
209 return error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400210
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400211 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
212 if (put_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500213 error = -EFAULT;
214
215 gfs2_glock_dq_m(1, &gh);
216 gfs2_holder_uninit(&gh);
217 return error;
218}
219
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500220void gfs2_set_inode_flags(struct inode *inode)
221{
222 struct gfs2_inode *ip = GFS2_I(inode);
223 struct gfs2_dinode_host *di = &ip->i_di;
224 unsigned int flags = inode->i_flags;
225
226 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
227 if (di->di_flags & GFS2_DIF_IMMUTABLE)
228 flags |= S_IMMUTABLE;
229 if (di->di_flags & GFS2_DIF_APPENDONLY)
230 flags |= S_APPEND;
231 if (di->di_flags & GFS2_DIF_NOATIME)
232 flags |= S_NOATIME;
233 if (di->di_flags & GFS2_DIF_SYNC)
234 flags |= S_SYNC;
235 inode->i_flags = flags;
236}
237
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500238/* Flags that can be set by user space */
239#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
240 GFS2_DIF_DIRECTIO| \
241 GFS2_DIF_IMMUTABLE| \
242 GFS2_DIF_APPENDONLY| \
243 GFS2_DIF_NOATIME| \
244 GFS2_DIF_SYNC| \
245 GFS2_DIF_SYSTEM| \
246 GFS2_DIF_INHERIT_DIRECTIO| \
247 GFS2_DIF_INHERIT_JDATA)
248
249/**
250 * gfs2_set_flags - set flags on an inode
251 * @inode: The inode
252 * @flags: The flags to set
253 * @mask: Indicates which flags are valid
254 *
255 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400256static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500257{
Josef Sipek81454092006-12-08 02:37:03 -0800258 struct inode *inode = filp->f_path.dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400259 struct gfs2_inode *ip = GFS2_I(inode);
260 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500261 struct buffer_head *bh;
262 struct gfs2_holder gh;
263 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400264 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500265
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500266 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
Abhijith Das52f341c2006-07-21 02:03:21 -0400267 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500268 return error;
269
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400270 flags = ip->i_di.di_flags;
271 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500272 if ((new_flags ^ flags) == 0)
273 goto out;
274
Steven Whitehouse4bcf7092006-04-25 13:20:27 -0400275 if (S_ISDIR(inode->i_mode)) {
276 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
277 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
278 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
279 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
280 }
281
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500282 error = -EINVAL;
283 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
284 goto out;
285
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500286 error = -EPERM;
287 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
288 goto out;
289 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
290 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400291 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400292 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500293 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400294 if (!IS_IMMUTABLE(inode)) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400295 error = permission(inode, MAY_WRITE, NULL);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400296 if (error)
297 goto out;
298 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500299
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400300 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500301 if (error)
302 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400303 error = gfs2_meta_inode_buffer(ip, &bh);
304 if (error)
305 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500306 gfs2_trans_add_bh(ip->i_gl, bh, 1);
307 ip->i_di.di_flags = new_flags;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500308 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500309 brelse(bh);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500310 gfs2_set_inode_flags(inode);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400311out_trans_end:
312 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500313out:
314 gfs2_glock_dq_uninit(&gh);
315 return error;
316}
317
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400318static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500319{
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400320 u32 fsflags, gfsflags;
321 if (get_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500322 return -EFAULT;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400323 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400324 return do_gfs2_set_flags(filp, gfsflags, ~0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500325}
326
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400327static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500328{
329 switch(cmd) {
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400330 case FS_IOC_GETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400331 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400332 case FS_IOC_SETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400333 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500334 }
335 return -ENOTTY;
336}
337
338
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339/**
340 * gfs2_mmap -
341 * @file: The file to map
342 * @vma: The VMA which described the mapping
343 *
344 * Returns: 0 or error code
345 */
346
347static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
348{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400349 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 struct gfs2_holder i_gh;
351 int error;
352
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
354 error = gfs2_glock_nq_atime(&i_gh);
355 if (error) {
356 gfs2_holder_uninit(&i_gh);
357 return error;
358 }
359
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000360 /* This is VM_MAYWRITE instead of VM_WRITE because a call
361 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000363 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
364 (VM_MAYSHARE | VM_MAYWRITE))
365 vma->vm_ops = &gfs2_vm_ops_sharewrite;
366 else
367 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368
369 gfs2_glock_dq_uninit(&i_gh);
370
371 return error;
372}
373
374/**
375 * gfs2_open - open a file
376 * @inode: the inode to open
377 * @file: the struct file for this opening
378 *
379 * Returns: errno
380 */
381
382static int gfs2_open(struct inode *inode, struct file *file)
383{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400384 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 struct gfs2_holder i_gh;
386 struct gfs2_file *fp;
387 int error;
388
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
390 if (!fp)
391 return -ENOMEM;
392
Steven Whitehousef55ab262006-02-21 12:51:39 +0000393 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400395 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500396 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500398 if (S_ISREG(ip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
400 &i_gh);
401 if (error)
402 goto fail;
403
404 if (!(file->f_flags & O_LARGEFILE) &&
405 ip->i_di.di_size > MAX_NON_LFS) {
406 error = -EFBIG;
407 goto fail_gunlock;
408 }
409
410 /* Listen to the Direct I/O flag */
411
412 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
413 file->f_flags |= O_DIRECT;
414
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 gfs2_glock_dq_uninit(&i_gh);
416 }
417
418 return 0;
419
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400420fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400422fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500423 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 return error;
426}
427
428/**
429 * gfs2_close - called to close a struct file
430 * @inode: the inode the struct file belongs to
431 * @file: the struct file being closed
432 *
433 * Returns: errno
434 */
435
436static int gfs2_close(struct inode *inode, struct file *file)
437{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500438 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 struct gfs2_file *fp;
440
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500441 fp = file->private_data;
442 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443
444 if (gfs2_assert_warn(sdp, fp))
445 return -EIO;
446
447 kfree(fp);
448
449 return 0;
450}
451
452/**
453 * gfs2_fsync - sync the dirty data for a file (across the cluster)
454 * @file: the file that points to the dentry (we ignore this)
455 * @dentry: the dentry that points to the inode to sync
456 *
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500457 * The VFS will flush "normal" data for us. We only need to worry
458 * about metadata here. For journaled data, we just do a log flush
459 * as we can't avoid it. Otherwise we can just bale out if datasync
460 * is set. For stuffed inodes we must flush the log in order to
461 * ensure that all data is on disk.
462 *
Steven Whitehouse34126f92006-12-07 09:13:14 -0500463 * The call to write_inode_now() is there to write back metadata and
464 * the inode itself. It does also try and write the data, but thats
465 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
466 * for us.
467 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 * Returns: errno
469 */
470
471static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
472{
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500473 struct inode *inode = dentry->d_inode;
474 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
475 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500477 if (gfs2_is_jdata(GFS2_I(inode))) {
478 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
479 return 0;
480 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500482 if (sync_state != 0) {
483 if (!datasync)
Steven Whitehouse34126f92006-12-07 09:13:14 -0500484 ret = write_inode_now(inode, 0);
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500485
486 if (gfs2_is_stuffed(GFS2_I(inode)))
487 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
488 }
489
490 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491}
492
493/**
494 * gfs2_lock - acquire/release a posix lock on a file
495 * @file: the file pointer
496 * @cmd: either modify or retrieve lock state, possibly wait
497 * @fl: type and range of lock
498 *
499 * Returns: errno
500 */
501
502static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
503{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400504 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
505 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506 struct lm_lockname name =
507 { .ln_number = ip->i_num.no_addr,
508 .ln_type = LM_TYPE_PLOCK };
509
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 if (!(fl->fl_flags & FL_POSIX))
511 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500512 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 return -ENOLCK;
514
515 if (sdp->sd_args.ar_localflocks) {
516 if (IS_GETLK(cmd)) {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500517 struct file_lock tmp;
518 int ret;
519 ret = posix_test_lock(file, fl, &tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 fl->fl_type = F_UNLCK;
Steven Whitehouse8628de02006-03-31 16:48:41 -0500521 if (ret)
522 memcpy(fl, &tmp, sizeof(struct file_lock));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 return 0;
524 } else {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500525 return posix_lock_file_wait(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 }
527 }
528
529 if (IS_GETLK(cmd))
530 return gfs2_lm_plock_get(sdp, &name, file, fl);
531 else if (fl->fl_type == F_UNLCK)
532 return gfs2_lm_punlock(sdp, &name, file, fl);
533 else
534 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
535}
536
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537static int do_flock(struct file *file, int cmd, struct file_lock *fl)
538{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500539 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
Josef Sipek81454092006-12-08 02:37:03 -0800541 struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542 struct gfs2_glock *gl;
543 unsigned int state;
544 int flags;
545 int error = 0;
546
547 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400548 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549
Steven Whitehousef55ab262006-02-21 12:51:39 +0000550 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551
552 gl = fl_gh->gh_gl;
553 if (gl) {
554 if (fl_gh->gh_state == state)
555 goto out;
556 gfs2_glock_hold(gl);
557 flock_lock_file_wait(file,
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400558 &(struct file_lock){.fl_type = F_UNLCK});
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 gfs2_glock_dq_uninit(fl_gh);
560 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400561 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 ip->i_num.no_addr, &gfs2_flock_glops,
563 CREATE, &gl);
564 if (error)
565 goto out;
566 }
567
568 gfs2_holder_init(gl, state, flags, fl_gh);
569 gfs2_glock_put(gl);
570
571 error = gfs2_glock_nq(fl_gh);
572 if (error) {
573 gfs2_holder_uninit(fl_gh);
574 if (error == GLR_TRYFAILED)
575 error = -EAGAIN;
576 } else {
577 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400578 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 }
580
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400581out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000582 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583 return error;
584}
585
586static void do_unflock(struct file *file, struct file_lock *fl)
587{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500588 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
590
Steven Whitehousef55ab262006-02-21 12:51:39 +0000591 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 flock_lock_file_wait(file, fl);
593 if (fl_gh->gh_gl)
594 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000595 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596}
597
598/**
599 * gfs2_flock - acquire/release a flock lock on a file
600 * @file: the file pointer
601 * @cmd: either modify or retrieve lock state, possibly wait
602 * @fl: type and range of lock
603 *
604 * Returns: errno
605 */
606
607static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
608{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400609 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
610 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 if (!(fl->fl_flags & FL_FLOCK))
613 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500614 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 return -ENOLCK;
616
617 if (sdp->sd_args.ar_localflocks)
618 return flock_lock_file_wait(file, fl);
619
620 if (fl->fl_type == F_UNLCK) {
621 do_unflock(file, fl);
622 return 0;
Steven Whitehoused00223f2006-10-02 10:28:05 -0400623 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624 return do_flock(file, cmd, fl);
Steven Whitehoused00223f2006-10-02 10:28:05 -0400625 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626}
627
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400628const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400629 .llseek = gfs2_llseek,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400630 .read = do_sync_read,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400631 .aio_read = generic_file_aio_read,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400632 .write = do_sync_write,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400633 .aio_write = generic_file_aio_write,
634 .unlocked_ioctl = gfs2_ioctl,
635 .mmap = gfs2_mmap,
636 .open = gfs2_open,
637 .release = gfs2_close,
638 .fsync = gfs2_fsync,
639 .lock = gfs2_lock,
640 .sendfile = generic_file_sendfile,
641 .flock = gfs2_flock,
642 .splice_read = generic_file_splice_read,
643 .splice_write = generic_file_splice_write,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644};
645
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400646const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400647 .readdir = gfs2_readdir,
648 .unlocked_ioctl = gfs2_ioctl,
649 .open = gfs2_open,
650 .release = gfs2_close,
651 .fsync = gfs2_fsync,
652 .lock = gfs2_lock,
653 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654};
655