blob: 7bd971bf7cdd489e1f24b9f1aae80bc5e1b1aa8a [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
David Teiglandb3b94fa2006-01-16 16:50:04 +000046/* For regular, non-NFS */
47struct filldir_reg {
48 struct gfs2_sbd *fdr_sbd;
49 int fdr_prefetch;
50
51 filldir_t fdr_filldir;
52 void *fdr_opaque;
53};
54
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000055/*
56 * Most fields left uninitialised to catch anybody who tries to
57 * use them. f_flags set to prevent file_accessed() from touching
58 * any other part of this. Its use is purely as a flag so that we
59 * know (in readpage()) whether or not do to locking.
60 */
Steven Whitehousedd538c82006-09-04 14:53:30 -040061struct file gfs2_internal_file_sentinel = {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000062 .f_flags = O_NOATIME|O_RDONLY,
63};
64
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000065static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
66 unsigned long offset, unsigned long size)
67{
68 char *kaddr;
69 unsigned long count = desc->count;
70
71 if (size > count)
72 size = count;
73
74 kaddr = kmap(page);
Al Viro9c9ab3d2006-10-13 23:49:23 -040075 memcpy(desc->arg.data, kaddr + offset, size);
Steven Whitehouse26c1a572006-09-04 15:32:10 -040076 kunmap(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000077
Steven Whitehouse26c1a572006-09-04 15:32:10 -040078 desc->count = count - size;
79 desc->written += size;
80 desc->arg.buf += size;
81 return size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000082}
83
84int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
85 char *buf, loff_t *pos, unsigned size)
86{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040087 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000088 read_descriptor_t desc;
89 desc.written = 0;
Al Viro9c9ab3d2006-10-13 23:49:23 -040090 desc.arg.data = buf;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000091 desc.count = size;
92 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000093 do_generic_mapping_read(inode->i_mapping, ra_state,
Steven Whitehousedd538c82006-09-04 14:53:30 -040094 &gfs2_internal_file_sentinel, pos, &desc,
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000095 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000096 return desc.written ? desc.written : desc.error;
97}
David Teiglandb3b94fa2006-01-16 16:50:04 +000098
99/**
100 * gfs2_llseek - seek to a location in a file
101 * @file: the file
102 * @offset: the offset
103 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
104 *
105 * SEEK_END requires the glock for the file because it references the
106 * file's size.
107 *
108 * Returns: The new offset, or errno
109 */
110
111static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
112{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400113 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114 struct gfs2_holder i_gh;
115 loff_t error;
116
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 if (origin == 2) {
118 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
119 &i_gh);
120 if (!error) {
121 error = remote_llseek(file, offset, origin);
122 gfs2_glock_dq_uninit(&i_gh);
123 }
124 } else
125 error = remote_llseek(file, offset, origin);
126
127 return error;
128}
129
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400131 * filldir_func - Report a directory entry to the caller of gfs2_dir_read()
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132 * @opaque: opaque data used by the function
133 * @name: the name of the directory entry
134 * @length: the length of the name
135 * @offset: the entry's offset in the directory
136 * @inum: the inode number the entry points to
137 * @type: the type of inode the entry points to
138 *
139 * Returns: 0 on success, 1 if buffer full
140 */
141
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400142static int filldir_func(void *opaque, const char *name, unsigned int length,
Al Viro629a21e2006-10-13 22:51:24 -0400143 u64 offset, struct gfs2_inum_host *inum,
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400144 unsigned int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145{
146 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
147 struct gfs2_sbd *sdp = fdr->fdr_sbd;
148 int error;
149
150 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400151 inum->no_addr, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 if (error)
153 return 1;
154
155 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400156 gfs2_glock_prefetch_num(sdp, inum->no_addr, &gfs2_inode_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400158 gfs2_glock_prefetch_num(sdp, inum->no_addr, &gfs2_iopen_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 LM_ST_SHARED, LM_FLAG_TRY);
160 }
161
162 return 0;
163}
164
165/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400166 * gfs2_readdir - Read directory entries from a directory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167 * @file: The directory to read from
168 * @dirent: Buffer for dirents
169 * @filldir: Function used to do the copying
170 *
171 * Returns: errno
172 */
173
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400174static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500176 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400177 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 struct filldir_reg fdr;
179 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400180 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 int error;
182
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400183 fdr.fdr_sbd = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 fdr.fdr_prefetch = 1;
185 fdr.fdr_filldir = filldir;
186 fdr.fdr_opaque = dirent;
187
188 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
189 error = gfs2_glock_nq_atime(&d_gh);
190 if (error) {
191 gfs2_holder_uninit(&d_gh);
192 return error;
193 }
194
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400195 error = gfs2_dir_read(dir, &offset, &fdr, filldir_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196
197 gfs2_glock_dq_uninit(&d_gh);
198
199 file->f_pos = offset;
200
201 return error;
202}
203
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400204/**
205 * fsflags_cvt
206 * @table: A table of 32 u32 flags
207 * @val: a 32 bit value to convert
208 *
209 * This function can be used to convert between fsflags values and
210 * GFS2's own flags values.
211 *
212 * Returns: the converted flags
213 */
214static u32 fsflags_cvt(const u32 *table, u32 val)
215{
216 u32 res = 0;
217 while(val) {
218 if (val & 1)
219 res |= *table;
220 table++;
221 val >>= 1;
222 }
223 return res;
224}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400226static const u32 fsflags_to_gfs2[32] = {
227 [3] = GFS2_DIF_SYNC,
228 [4] = GFS2_DIF_IMMUTABLE,
229 [5] = GFS2_DIF_APPENDONLY,
230 [7] = GFS2_DIF_NOATIME,
231 [12] = GFS2_DIF_EXHASH,
232 [14] = GFS2_DIF_JDATA,
233 [20] = GFS2_DIF_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500234};
235
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400236static const u32 gfs2_to_fsflags[32] = {
237 [gfs2fl_Sync] = FS_SYNC_FL,
238 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
239 [gfs2fl_AppendOnly] = FS_APPEND_FL,
240 [gfs2fl_NoAtime] = FS_NOATIME_FL,
241 [gfs2fl_ExHash] = FS_INDEX_FL,
242 [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
243 [gfs2fl_Directio] = FS_DIRECTIO_FL,
244 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
245 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500246};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500247
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400248static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500249{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400250 struct inode *inode = filp->f_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400251 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500252 struct gfs2_holder gh;
253 int error;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400254 u32 fsflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500255
256 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500257 error = gfs2_glock_nq_atime(&gh);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500258 if (error)
259 return error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400260
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400261 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
262 if (put_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500263 error = -EFAULT;
264
265 gfs2_glock_dq_m(1, &gh);
266 gfs2_holder_uninit(&gh);
267 return error;
268}
269
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500270void gfs2_set_inode_flags(struct inode *inode)
271{
272 struct gfs2_inode *ip = GFS2_I(inode);
273 struct gfs2_dinode_host *di = &ip->i_di;
274 unsigned int flags = inode->i_flags;
275
276 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
277 if (di->di_flags & GFS2_DIF_IMMUTABLE)
278 flags |= S_IMMUTABLE;
279 if (di->di_flags & GFS2_DIF_APPENDONLY)
280 flags |= S_APPEND;
281 if (di->di_flags & GFS2_DIF_NOATIME)
282 flags |= S_NOATIME;
283 if (di->di_flags & GFS2_DIF_SYNC)
284 flags |= S_SYNC;
285 inode->i_flags = flags;
286}
287
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500288/* Flags that can be set by user space */
289#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
290 GFS2_DIF_DIRECTIO| \
291 GFS2_DIF_IMMUTABLE| \
292 GFS2_DIF_APPENDONLY| \
293 GFS2_DIF_NOATIME| \
294 GFS2_DIF_SYNC| \
295 GFS2_DIF_SYSTEM| \
296 GFS2_DIF_INHERIT_DIRECTIO| \
297 GFS2_DIF_INHERIT_JDATA)
298
299/**
300 * gfs2_set_flags - set flags on an inode
301 * @inode: The inode
302 * @flags: The flags to set
303 * @mask: Indicates which flags are valid
304 *
305 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400306static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500307{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400308 struct inode *inode = filp->f_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400309 struct gfs2_inode *ip = GFS2_I(inode);
310 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500311 struct buffer_head *bh;
312 struct gfs2_holder gh;
313 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400314 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500315
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500316 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
Abhijith Das52f341c2006-07-21 02:03:21 -0400317 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500318 return error;
319
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400320 flags = ip->i_di.di_flags;
321 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500322 if ((new_flags ^ flags) == 0)
323 goto out;
324
Steven Whitehouse4bcf7092006-04-25 13:20:27 -0400325 if (S_ISDIR(inode->i_mode)) {
326 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
327 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
328 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
329 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
330 }
331
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500332 error = -EINVAL;
333 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
334 goto out;
335
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500336 error = -EPERM;
337 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
338 goto out;
339 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
340 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400341 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400342 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500343 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400344 if (!IS_IMMUTABLE(inode)) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400345 error = permission(inode, MAY_WRITE, NULL);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400346 if (error)
347 goto out;
348 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500349
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400350 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500351 if (error)
352 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400353 error = gfs2_meta_inode_buffer(ip, &bh);
354 if (error)
355 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500356 gfs2_trans_add_bh(ip->i_gl, bh, 1);
357 ip->i_di.di_flags = new_flags;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500358 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500359 brelse(bh);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500360 gfs2_set_inode_flags(inode);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400361out_trans_end:
362 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500363out:
364 gfs2_glock_dq_uninit(&gh);
365 return error;
366}
367
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400368static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500369{
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400370 u32 fsflags, gfsflags;
371 if (get_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500372 return -EFAULT;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400373 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400374 return do_gfs2_set_flags(filp, gfsflags, ~0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500375}
376
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400377static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500378{
379 switch(cmd) {
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400380 case FS_IOC_GETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400381 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400382 case FS_IOC_SETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400383 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500384 }
385 return -ENOTTY;
386}
387
388
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389/**
390 * gfs2_mmap -
391 * @file: The file to map
392 * @vma: The VMA which described the mapping
393 *
394 * Returns: 0 or error code
395 */
396
397static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
398{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400399 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 struct gfs2_holder i_gh;
401 int error;
402
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
404 error = gfs2_glock_nq_atime(&i_gh);
405 if (error) {
406 gfs2_holder_uninit(&i_gh);
407 return error;
408 }
409
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000410 /* This is VM_MAYWRITE instead of VM_WRITE because a call
411 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000413 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
414 (VM_MAYSHARE | VM_MAYWRITE))
415 vma->vm_ops = &gfs2_vm_ops_sharewrite;
416 else
417 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418
419 gfs2_glock_dq_uninit(&i_gh);
420
421 return error;
422}
423
424/**
425 * gfs2_open - open a file
426 * @inode: the inode to open
427 * @file: the struct file for this opening
428 *
429 * Returns: errno
430 */
431
432static int gfs2_open(struct inode *inode, struct file *file)
433{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400434 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435 struct gfs2_holder i_gh;
436 struct gfs2_file *fp;
437 int error;
438
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
440 if (!fp)
441 return -ENOMEM;
442
Steven Whitehousef55ab262006-02-21 12:51:39 +0000443 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400445 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500446 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500448 if (S_ISREG(ip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
450 &i_gh);
451 if (error)
452 goto fail;
453
454 if (!(file->f_flags & O_LARGEFILE) &&
455 ip->i_di.di_size > MAX_NON_LFS) {
456 error = -EFBIG;
457 goto fail_gunlock;
458 }
459
460 /* Listen to the Direct I/O flag */
461
462 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
463 file->f_flags |= O_DIRECT;
464
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465 gfs2_glock_dq_uninit(&i_gh);
466 }
467
468 return 0;
469
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400470fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400472fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500473 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 return error;
476}
477
478/**
479 * gfs2_close - called to close a struct file
480 * @inode: the inode the struct file belongs to
481 * @file: the struct file being closed
482 *
483 * Returns: errno
484 */
485
486static int gfs2_close(struct inode *inode, struct file *file)
487{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500488 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 struct gfs2_file *fp;
490
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500491 fp = file->private_data;
492 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493
494 if (gfs2_assert_warn(sdp, fp))
495 return -EIO;
496
497 kfree(fp);
498
499 return 0;
500}
501
502/**
503 * gfs2_fsync - sync the dirty data for a file (across the cluster)
504 * @file: the file that points to the dentry (we ignore this)
505 * @dentry: the dentry that points to the inode to sync
506 *
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500507 * The VFS will flush "normal" data for us. We only need to worry
508 * about metadata here. For journaled data, we just do a log flush
509 * as we can't avoid it. Otherwise we can just bale out if datasync
510 * is set. For stuffed inodes we must flush the log in order to
511 * ensure that all data is on disk.
512 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 * Returns: errno
514 */
515
516static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
517{
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500518 struct inode *inode = dentry->d_inode;
519 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
520 int ret = 0;
521 struct writeback_control wbc = {
522 .sync_mode = WB_SYNC_ALL,
523 .nr_to_write = 0,
524 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500526 if (gfs2_is_jdata(GFS2_I(inode))) {
527 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
528 return 0;
529 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
Steven Whitehouse33c3de32006-11-30 10:14:32 -0500531 if (sync_state != 0) {
532 if (!datasync)
533 ret = sync_inode(inode, &wbc);
534
535 if (gfs2_is_stuffed(GFS2_I(inode)))
536 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
537 }
538
539 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540}
541
542/**
543 * gfs2_lock - acquire/release a posix lock on a file
544 * @file: the file pointer
545 * @cmd: either modify or retrieve lock state, possibly wait
546 * @fl: type and range of lock
547 *
548 * Returns: errno
549 */
550
551static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
552{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400553 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
554 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 struct lm_lockname name =
556 { .ln_number = ip->i_num.no_addr,
557 .ln_type = LM_TYPE_PLOCK };
558
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 if (!(fl->fl_flags & FL_POSIX))
560 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500561 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 return -ENOLCK;
563
564 if (sdp->sd_args.ar_localflocks) {
565 if (IS_GETLK(cmd)) {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500566 struct file_lock tmp;
567 int ret;
568 ret = posix_test_lock(file, fl, &tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 fl->fl_type = F_UNLCK;
Steven Whitehouse8628de02006-03-31 16:48:41 -0500570 if (ret)
571 memcpy(fl, &tmp, sizeof(struct file_lock));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 return 0;
573 } else {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500574 return posix_lock_file_wait(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 }
576 }
577
578 if (IS_GETLK(cmd))
579 return gfs2_lm_plock_get(sdp, &name, file, fl);
580 else if (fl->fl_type == F_UNLCK)
581 return gfs2_lm_punlock(sdp, &name, file, fl);
582 else
583 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
584}
585
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586static int do_flock(struct file *file, int cmd, 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;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400590 struct gfs2_inode *ip = GFS2_I(file->f_dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 struct gfs2_glock *gl;
592 unsigned int state;
593 int flags;
594 int error = 0;
595
596 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400597 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598
Steven Whitehousef55ab262006-02-21 12:51:39 +0000599 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600
601 gl = fl_gh->gh_gl;
602 if (gl) {
603 if (fl_gh->gh_state == state)
604 goto out;
605 gfs2_glock_hold(gl);
606 flock_lock_file_wait(file,
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400607 &(struct file_lock){.fl_type = F_UNLCK});
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608 gfs2_glock_dq_uninit(fl_gh);
609 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400610 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611 ip->i_num.no_addr, &gfs2_flock_glops,
612 CREATE, &gl);
613 if (error)
614 goto out;
615 }
616
617 gfs2_holder_init(gl, state, flags, fl_gh);
618 gfs2_glock_put(gl);
619
620 error = gfs2_glock_nq(fl_gh);
621 if (error) {
622 gfs2_holder_uninit(fl_gh);
623 if (error == GLR_TRYFAILED)
624 error = -EAGAIN;
625 } else {
626 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400627 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 }
629
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400630out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000631 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 return error;
633}
634
635static void do_unflock(struct file *file, struct file_lock *fl)
636{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500637 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
639
Steven Whitehousef55ab262006-02-21 12:51:39 +0000640 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 flock_lock_file_wait(file, fl);
642 if (fl_gh->gh_gl)
643 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000644 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645}
646
647/**
648 * gfs2_flock - acquire/release a flock lock on a file
649 * @file: the file pointer
650 * @cmd: either modify or retrieve lock state, possibly wait
651 * @fl: type and range of lock
652 *
653 * Returns: errno
654 */
655
656static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
657{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400658 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
659 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 if (!(fl->fl_flags & FL_FLOCK))
662 return -ENOLCK;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500663 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 return -ENOLCK;
665
666 if (sdp->sd_args.ar_localflocks)
667 return flock_lock_file_wait(file, fl);
668
669 if (fl->fl_type == F_UNLCK) {
670 do_unflock(file, fl);
671 return 0;
Steven Whitehoused00223f2006-10-02 10:28:05 -0400672 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 return do_flock(file, cmd, fl);
Steven Whitehoused00223f2006-10-02 10:28:05 -0400674 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675}
676
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400677const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400678 .llseek = gfs2_llseek,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400679 .read = do_sync_read,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400680 .aio_read = generic_file_aio_read,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400681 .write = do_sync_write,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400682 .aio_write = generic_file_aio_write,
683 .unlocked_ioctl = gfs2_ioctl,
684 .mmap = gfs2_mmap,
685 .open = gfs2_open,
686 .release = gfs2_close,
687 .fsync = gfs2_fsync,
688 .lock = gfs2_lock,
689 .sendfile = generic_file_sendfile,
690 .flock = gfs2_flock,
691 .splice_read = generic_file_splice_read,
692 .splice_write = generic_file_splice_write,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693};
694
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400695const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400696 .readdir = gfs2_readdir,
697 .unlocked_ioctl = gfs2_ioctl,
698 .open = gfs2_open,
699 .release = gfs2_close,
700 .fsync = gfs2_fsync,
701 .lock = gfs2_lock,
702 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703};
704