blob: 3064f133bf3c23380bbb6ee2bc5e824fdfd6dfa9 [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>
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
David Teiglandb3b94fa2006-01-16 16:50:04 +000045/* For regular, non-NFS */
46struct filldir_reg {
47 struct gfs2_sbd *fdr_sbd;
48 int fdr_prefetch;
49
50 filldir_t fdr_filldir;
51 void *fdr_opaque;
52};
53
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000054/*
55 * Most fields left uninitialised to catch anybody who tries to
56 * use them. f_flags set to prevent file_accessed() from touching
57 * any other part of this. Its use is purely as a flag so that we
58 * know (in readpage()) whether or not do to locking.
59 */
Steven Whitehousedd538c82006-09-04 14:53:30 -040060struct file gfs2_internal_file_sentinel = {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000061 .f_flags = O_NOATIME|O_RDONLY,
62};
63
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000064static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
65 unsigned long offset, unsigned long size)
66{
67 char *kaddr;
68 unsigned long count = desc->count;
69
70 if (size > count)
71 size = count;
72
73 kaddr = kmap(page);
74 memcpy(desc->arg.buf, kaddr + offset, size);
Steven Whitehouse26c1a572006-09-04 15:32:10 -040075 kunmap(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000076
Steven Whitehouse26c1a572006-09-04 15:32:10 -040077 desc->count = count - size;
78 desc->written += size;
79 desc->arg.buf += size;
80 return size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000081}
82
83int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
84 char *buf, loff_t *pos, unsigned size)
85{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040086 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000087 read_descriptor_t desc;
88 desc.written = 0;
89 desc.arg.buf = buf;
90 desc.count = size;
91 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000092 do_generic_mapping_read(inode->i_mapping, ra_state,
Steven Whitehousedd538c82006-09-04 14:53:30 -040093 &gfs2_internal_file_sentinel, pos, &desc,
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000094 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000095 return desc.written ? desc.written : desc.error;
96}
David Teiglandb3b94fa2006-01-16 16:50:04 +000097
98/**
99 * gfs2_llseek - seek to a location in a file
100 * @file: the file
101 * @offset: the offset
102 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
103 *
104 * SEEK_END requires the glock for the file because it references the
105 * file's size.
106 *
107 * Returns: The new offset, or errno
108 */
109
110static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
111{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400112 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 struct gfs2_holder i_gh;
114 loff_t error;
115
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116 if (origin == 2) {
117 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
118 &i_gh);
119 if (!error) {
120 error = remote_llseek(file, offset, origin);
121 gfs2_glock_dq_uninit(&i_gh);
122 }
123 } else
124 error = remote_llseek(file, offset, origin);
125
126 return error;
127}
128
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400130 * filldir_func - Report a directory entry to the caller of gfs2_dir_read()
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 * @opaque: opaque data used by the function
132 * @name: the name of the directory entry
133 * @length: the length of the name
134 * @offset: the entry's offset in the directory
135 * @inum: the inode number the entry points to
136 * @type: the type of inode the entry points to
137 *
138 * Returns: 0 on success, 1 if buffer full
139 */
140
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400141static int filldir_func(void *opaque, const char *name, unsigned int length,
142 u64 offset, struct gfs2_inum *inum,
143 unsigned int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144{
145 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
146 struct gfs2_sbd *sdp = fdr->fdr_sbd;
147 int error;
148
149 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400150 inum->no_addr, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 if (error)
152 return 1;
153
154 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400155 gfs2_glock_prefetch_num(sdp, inum->no_addr, &gfs2_inode_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400157 gfs2_glock_prefetch_num(sdp, inum->no_addr, &gfs2_iopen_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 LM_ST_SHARED, LM_FLAG_TRY);
159 }
160
161 return 0;
162}
163
164/**
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400165 * gfs2_readdir - Read directory entries from a directory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 * @file: The directory to read from
167 * @dirent: Buffer for dirents
168 * @filldir: Function used to do the copying
169 *
170 * Returns: errno
171 */
172
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400173static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500175 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400176 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 struct filldir_reg fdr;
178 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400179 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 int error;
181
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400182 fdr.fdr_sbd = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 fdr.fdr_prefetch = 1;
184 fdr.fdr_filldir = filldir;
185 fdr.fdr_opaque = dirent;
186
187 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
188 error = gfs2_glock_nq_atime(&d_gh);
189 if (error) {
190 gfs2_holder_uninit(&d_gh);
191 return error;
192 }
193
Steven Whitehousef0e522a2006-09-19 16:41:11 -0400194 error = gfs2_dir_read(dir, &offset, &fdr, filldir_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195
196 gfs2_glock_dq_uninit(&d_gh);
197
198 file->f_pos = offset;
199
200 return error;
201}
202
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400203/**
204 * fsflags_cvt
205 * @table: A table of 32 u32 flags
206 * @val: a 32 bit value to convert
207 *
208 * This function can be used to convert between fsflags values and
209 * GFS2's own flags values.
210 *
211 * Returns: the converted flags
212 */
213static u32 fsflags_cvt(const u32 *table, u32 val)
214{
215 u32 res = 0;
216 while(val) {
217 if (val & 1)
218 res |= *table;
219 table++;
220 val >>= 1;
221 }
222 return res;
223}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400225static const u32 fsflags_to_gfs2[32] = {
226 [3] = GFS2_DIF_SYNC,
227 [4] = GFS2_DIF_IMMUTABLE,
228 [5] = GFS2_DIF_APPENDONLY,
229 [7] = GFS2_DIF_NOATIME,
230 [12] = GFS2_DIF_EXHASH,
231 [14] = GFS2_DIF_JDATA,
232 [20] = GFS2_DIF_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500233};
234
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400235static const u32 gfs2_to_fsflags[32] = {
236 [gfs2fl_Sync] = FS_SYNC_FL,
237 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
238 [gfs2fl_AppendOnly] = FS_APPEND_FL,
239 [gfs2fl_NoAtime] = FS_NOATIME_FL,
240 [gfs2fl_ExHash] = FS_INDEX_FL,
241 [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
242 [gfs2fl_Directio] = FS_DIRECTIO_FL,
243 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
244 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500245};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500246
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400247static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500248{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400249 struct inode *inode = filp->f_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500251 struct gfs2_holder gh;
252 int error;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400253 u32 fsflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500254
255 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
256 error = gfs2_glock_nq_m_atime(1, &gh);
257 if (error)
258 return error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400259
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400260 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
261 if (put_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500262 error = -EFAULT;
263
264 gfs2_glock_dq_m(1, &gh);
265 gfs2_holder_uninit(&gh);
266 return error;
267}
268
269/* Flags that can be set by user space */
270#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
271 GFS2_DIF_DIRECTIO| \
272 GFS2_DIF_IMMUTABLE| \
273 GFS2_DIF_APPENDONLY| \
274 GFS2_DIF_NOATIME| \
275 GFS2_DIF_SYNC| \
276 GFS2_DIF_SYSTEM| \
277 GFS2_DIF_INHERIT_DIRECTIO| \
278 GFS2_DIF_INHERIT_JDATA)
279
280/**
281 * gfs2_set_flags - set flags on an inode
282 * @inode: The inode
283 * @flags: The flags to set
284 * @mask: Indicates which flags are valid
285 *
286 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400287static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500288{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400289 struct inode *inode = filp->f_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290 struct gfs2_inode *ip = GFS2_I(inode);
291 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500292 struct buffer_head *bh;
293 struct gfs2_holder gh;
294 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400295 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500296
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500297 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
Abhijith Das52f341c2006-07-21 02:03:21 -0400298 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500299 return error;
300
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400301 flags = ip->i_di.di_flags;
302 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500303 if ((new_flags ^ flags) == 0)
304 goto out;
305
Steven Whitehouse4bcf7092006-04-25 13:20:27 -0400306 if (S_ISDIR(inode->i_mode)) {
307 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
308 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
309 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
310 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
311 }
312
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500313 error = -EINVAL;
314 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
315 goto out;
316
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500317 error = -EPERM;
318 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
319 goto out;
320 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
321 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400322 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400323 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500324 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400325 if (!IS_IMMUTABLE(inode)) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400326 error = permission(inode, MAY_WRITE, NULL);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400327 if (error)
328 goto out;
329 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500330
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400331 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500332 if (error)
333 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400334 error = gfs2_meta_inode_buffer(ip, &bh);
335 if (error)
336 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500337 gfs2_trans_add_bh(ip->i_gl, bh, 1);
338 ip->i_di.di_flags = new_flags;
339 gfs2_dinode_out(&ip->i_di, bh->b_data);
340 brelse(bh);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400341out_trans_end:
342 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500343out:
344 gfs2_glock_dq_uninit(&gh);
345 return error;
346}
347
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400348static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500349{
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400350 u32 fsflags, gfsflags;
351 if (get_user(fsflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500352 return -EFAULT;
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400353 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400354 return do_gfs2_set_flags(filp, gfsflags, ~0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500355}
356
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400357static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500358{
359 switch(cmd) {
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400360 case FS_IOC_GETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400361 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse128e5eb2006-10-02 11:24:43 -0400362 case FS_IOC_SETFLAGS:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400363 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500364 }
365 return -ENOTTY;
366}
367
368
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369/**
370 * gfs2_mmap -
371 * @file: The file to map
372 * @vma: The VMA which described the mapping
373 *
374 * Returns: 0 or error code
375 */
376
377static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
378{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400379 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 struct gfs2_holder i_gh;
381 int error;
382
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
384 error = gfs2_glock_nq_atime(&i_gh);
385 if (error) {
386 gfs2_holder_uninit(&i_gh);
387 return error;
388 }
389
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000390 /* This is VM_MAYWRITE instead of VM_WRITE because a call
391 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000393 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
394 (VM_MAYSHARE | VM_MAYWRITE))
395 vma->vm_ops = &gfs2_vm_ops_sharewrite;
396 else
397 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398
399 gfs2_glock_dq_uninit(&i_gh);
400
401 return error;
402}
403
404/**
405 * gfs2_open - open a file
406 * @inode: the inode to open
407 * @file: the struct file for this opening
408 *
409 * Returns: errno
410 */
411
412static int gfs2_open(struct inode *inode, struct file *file)
413{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400414 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 struct gfs2_holder i_gh;
416 struct gfs2_file *fp;
417 int error;
418
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
420 if (!fp)
421 return -ENOMEM;
422
Steven Whitehousef55ab262006-02-21 12:51:39 +0000423 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400425 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500426 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427
428 if (S_ISREG(ip->i_di.di_mode)) {
429 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
430 &i_gh);
431 if (error)
432 goto fail;
433
434 if (!(file->f_flags & O_LARGEFILE) &&
435 ip->i_di.di_size > MAX_NON_LFS) {
436 error = -EFBIG;
437 goto fail_gunlock;
438 }
439
440 /* Listen to the Direct I/O flag */
441
442 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
443 file->f_flags |= O_DIRECT;
444
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 gfs2_glock_dq_uninit(&i_gh);
446 }
447
448 return 0;
449
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400450fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400452fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500453 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455 return error;
456}
457
458/**
459 * gfs2_close - called to close a struct file
460 * @inode: the inode the struct file belongs to
461 * @file: the struct file being closed
462 *
463 * Returns: errno
464 */
465
466static int gfs2_close(struct inode *inode, struct file *file)
467{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500468 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 struct gfs2_file *fp;
470
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500471 fp = file->private_data;
472 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473
474 if (gfs2_assert_warn(sdp, fp))
475 return -EIO;
476
477 kfree(fp);
478
479 return 0;
480}
481
482/**
483 * gfs2_fsync - sync the dirty data for a file (across the cluster)
484 * @file: the file that points to the dentry (we ignore this)
485 * @dentry: the dentry that points to the inode to sync
486 *
487 * Returns: errno
488 */
489
490static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
491{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400492 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400494 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495
496 return 0;
497}
498
499/**
500 * gfs2_lock - acquire/release a posix lock on a file
501 * @file: the file pointer
502 * @cmd: either modify or retrieve lock state, possibly wait
503 * @fl: type and range of lock
504 *
505 * Returns: errno
506 */
507
508static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
509{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400510 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
511 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 struct lm_lockname name =
513 { .ln_number = ip->i_num.no_addr,
514 .ln_type = LM_TYPE_PLOCK };
515
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 if (!(fl->fl_flags & FL_POSIX))
517 return -ENOLCK;
518 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
519 return -ENOLCK;
520
521 if (sdp->sd_args.ar_localflocks) {
522 if (IS_GETLK(cmd)) {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500523 struct file_lock tmp;
524 int ret;
525 ret = posix_test_lock(file, fl, &tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 fl->fl_type = F_UNLCK;
Steven Whitehouse8628de02006-03-31 16:48:41 -0500527 if (ret)
528 memcpy(fl, &tmp, sizeof(struct file_lock));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 return 0;
530 } else {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500531 return posix_lock_file_wait(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 }
533 }
534
535 if (IS_GETLK(cmd))
536 return gfs2_lm_plock_get(sdp, &name, file, fl);
537 else if (fl->fl_type == F_UNLCK)
538 return gfs2_lm_punlock(sdp, &name, file, fl);
539 else
540 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
541}
542
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543static int do_flock(struct file *file, int cmd, struct file_lock *fl)
544{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500545 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400547 struct gfs2_inode *ip = GFS2_I(file->f_dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 struct gfs2_glock *gl;
549 unsigned int state;
550 int flags;
551 int error = 0;
552
553 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400554 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555
Steven Whitehousef55ab262006-02-21 12:51:39 +0000556 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557
558 gl = fl_gh->gh_gl;
559 if (gl) {
560 if (fl_gh->gh_state == state)
561 goto out;
562 gfs2_glock_hold(gl);
563 flock_lock_file_wait(file,
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400564 &(struct file_lock){.fl_type = F_UNLCK});
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 gfs2_glock_dq_uninit(fl_gh);
566 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400567 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 ip->i_num.no_addr, &gfs2_flock_glops,
569 CREATE, &gl);
570 if (error)
571 goto out;
572 }
573
574 gfs2_holder_init(gl, state, flags, fl_gh);
575 gfs2_glock_put(gl);
576
577 error = gfs2_glock_nq(fl_gh);
578 if (error) {
579 gfs2_holder_uninit(fl_gh);
580 if (error == GLR_TRYFAILED)
581 error = -EAGAIN;
582 } else {
583 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400584 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 }
586
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400587out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000588 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 return error;
590}
591
592static void do_unflock(struct file *file, struct file_lock *fl)
593{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500594 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
596
Steven Whitehousef55ab262006-02-21 12:51:39 +0000597 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 flock_lock_file_wait(file, fl);
599 if (fl_gh->gh_gl)
600 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000601 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602}
603
604/**
605 * gfs2_flock - acquire/release a flock lock on a file
606 * @file: the file pointer
607 * @cmd: either modify or retrieve lock state, possibly wait
608 * @fl: type and range of lock
609 *
610 * Returns: errno
611 */
612
613static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
614{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400615 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
616 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 if (!(fl->fl_flags & FL_FLOCK))
619 return -ENOLCK;
620 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
621 return -ENOLCK;
622
623 if (sdp->sd_args.ar_localflocks)
624 return flock_lock_file_wait(file, fl);
625
626 if (fl->fl_type == F_UNLCK) {
627 do_unflock(file, fl);
628 return 0;
Steven Whitehoused00223f2006-10-02 10:28:05 -0400629 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 return do_flock(file, cmd, fl);
Steven Whitehoused00223f2006-10-02 10:28:05 -0400631 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632}
633
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400634const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400635 .llseek = gfs2_llseek,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400636 .read = do_sync_read,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400637 .aio_read = generic_file_aio_read,
Steven Whitehoused00223f2006-10-02 10:28:05 -0400638 .write = do_sync_write,
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400639 .aio_write = generic_file_aio_write,
640 .unlocked_ioctl = gfs2_ioctl,
641 .mmap = gfs2_mmap,
642 .open = gfs2_open,
643 .release = gfs2_close,
644 .fsync = gfs2_fsync,
645 .lock = gfs2_lock,
646 .sendfile = generic_file_sendfile,
647 .flock = gfs2_flock,
648 .splice_read = generic_file_splice_read,
649 .splice_write = generic_file_splice_write,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650};
651
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400652const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400653 .readdir = gfs2_readdir,
654 .unlocked_ioctl = gfs2_ioctl,
655 .open = gfs2_open,
656 .release = gfs2_close,
657 .fsync = gfs2_fsync,
658 .lock = gfs2_lock,
659 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660};
661