]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/gfs2/ops_file.c
[GFS2] Fix incorrect error path in prepare_write()
[net-next-2.6.git] / fs / gfs2 / ops_file.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
b3b94faa
DT
10#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>
18ec7d5c 18#include <linux/fs.h>
5c676f6d 19#include <linux/gfs2_ondisk.h>
71b86f56
SW
20#include <linux/ext2_fs.h>
21#include <linux/crc32.h>
7d308590 22#include <linux/lm_interface.h>
33c3de32 23#include <linux/writeback.h>
b3b94faa
DT
24#include <asm/uaccess.h>
25
26#include "gfs2.h"
5c676f6d 27#include "incore.h"
b3b94faa
DT
28#include "bmap.h"
29#include "dir.h"
30#include "glock.h"
31#include "glops.h"
32#include "inode.h"
b3b94faa
DT
33#include "lm.h"
34#include "log.h"
35#include "meta_io.h"
36#include "ops_file.h"
37#include "ops_vm.h"
38#include "quota.h"
39#include "rgrp.h"
40#include "trans.h"
5c676f6d 41#include "util.h"
71b86f56 42#include "eaops.h"
b3b94faa 43
61a30dcb
SW
44/*
45 * Most fields left uninitialised to catch anybody who tries to
46 * use them. f_flags set to prevent file_accessed() from touching
47 * any other part of this. Its use is purely as a flag so that we
48 * know (in readpage()) whether or not do to locking.
49 */
dd538c83 50struct file gfs2_internal_file_sentinel = {
61a30dcb
SW
51 .f_flags = O_NOATIME|O_RDONLY,
52};
53
18ec7d5c
SW
54static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
55 unsigned long offset, unsigned long size)
56{
57 char *kaddr;
58 unsigned long count = desc->count;
59
60 if (size > count)
61 size = count;
62
63 kaddr = kmap(page);
9c9ab3d5 64 memcpy(desc->arg.data, kaddr + offset, size);
26c1a574 65 kunmap(page);
18ec7d5c 66
26c1a574
SW
67 desc->count = count - size;
68 desc->written += size;
69 desc->arg.buf += size;
70 return size;
18ec7d5c
SW
71}
72
73int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
74 char *buf, loff_t *pos, unsigned size)
75{
feaa7bba 76 struct inode *inode = &ip->i_inode;
18ec7d5c
SW
77 read_descriptor_t desc;
78 desc.written = 0;
9c9ab3d5 79 desc.arg.data = buf;
18ec7d5c
SW
80 desc.count = size;
81 desc.error = 0;
61a30dcb 82 do_generic_mapping_read(inode->i_mapping, ra_state,
dd538c83 83 &gfs2_internal_file_sentinel, pos, &desc,
61a30dcb 84 gfs2_read_actor);
18ec7d5c
SW
85 return desc.written ? desc.written : desc.error;
86}
b3b94faa
DT
87
88/**
89 * gfs2_llseek - seek to a location in a file
90 * @file: the file
91 * @offset: the offset
92 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
93 *
94 * SEEK_END requires the glock for the file because it references the
95 * file's size.
96 *
97 * Returns: The new offset, or errno
98 */
99
100static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
101{
feaa7bba 102 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
b3b94faa
DT
103 struct gfs2_holder i_gh;
104 loff_t error;
105
b3b94faa
DT
106 if (origin == 2) {
107 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
108 &i_gh);
109 if (!error) {
110 error = remote_llseek(file, offset, origin);
111 gfs2_glock_dq_uninit(&i_gh);
112 }
113 } else
114 error = remote_llseek(file, offset, origin);
115
116 return error;
117}
118
b3b94faa 119/**
f0e522a9 120 * gfs2_readdir - Read directory entries from a directory
b3b94faa
DT
121 * @file: The directory to read from
122 * @dirent: Buffer for dirents
123 * @filldir: Function used to do the copying
124 *
125 * Returns: errno
126 */
127
f0e522a9 128static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
b3b94faa 129{
71b86f56 130 struct inode *dir = file->f_mapping->host;
feaa7bba 131 struct gfs2_inode *dip = GFS2_I(dir);
b3b94faa 132 struct gfs2_holder d_gh;
cd915493 133 u64 offset = file->f_pos;
b3b94faa
DT
134 int error;
135
b3b94faa
DT
136 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
137 error = gfs2_glock_nq_atime(&d_gh);
138 if (error) {
139 gfs2_holder_uninit(&d_gh);
140 return error;
141 }
142
3699e3a4 143 error = gfs2_dir_read(dir, &offset, dirent, filldir);
b3b94faa
DT
144
145 gfs2_glock_dq_uninit(&d_gh);
146
b3b94faa
DT
147 file->f_pos = offset;
148
b3b94faa
DT
149 return error;
150}
151
128e5eba
SW
152/**
153 * fsflags_cvt
154 * @table: A table of 32 u32 flags
155 * @val: a 32 bit value to convert
156 *
157 * This function can be used to convert between fsflags values and
158 * GFS2's own flags values.
159 *
160 * Returns: the converted flags
161 */
162static u32 fsflags_cvt(const u32 *table, u32 val)
163{
164 u32 res = 0;
165 while(val) {
166 if (val & 1)
167 res |= *table;
168 table++;
169 val >>= 1;
170 }
171 return res;
172}
b3b94faa 173
128e5eba
SW
174static const u32 fsflags_to_gfs2[32] = {
175 [3] = GFS2_DIF_SYNC,
176 [4] = GFS2_DIF_IMMUTABLE,
177 [5] = GFS2_DIF_APPENDONLY,
178 [7] = GFS2_DIF_NOATIME,
179 [12] = GFS2_DIF_EXHASH,
180 [14] = GFS2_DIF_JDATA,
181 [20] = GFS2_DIF_DIRECTIO,
71b86f56
SW
182};
183
128e5eba
SW
184static const u32 gfs2_to_fsflags[32] = {
185 [gfs2fl_Sync] = FS_SYNC_FL,
186 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
187 [gfs2fl_AppendOnly] = FS_APPEND_FL,
188 [gfs2fl_NoAtime] = FS_NOATIME_FL,
189 [gfs2fl_ExHash] = FS_INDEX_FL,
190 [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
191 [gfs2fl_Directio] = FS_DIRECTIO_FL,
192 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
193 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
7ea9ea83 194};
71b86f56 195
b09e593d 196static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
71b86f56 197{
81454098 198 struct inode *inode = filp->f_path.dentry->d_inode;
feaa7bba 199 struct gfs2_inode *ip = GFS2_I(inode);
71b86f56
SW
200 struct gfs2_holder gh;
201 int error;
128e5eba 202 u32 fsflags;
71b86f56
SW
203
204 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
dcd24799 205 error = gfs2_glock_nq_atime(&gh);
71b86f56
SW
206 if (error)
207 return error;
907b9bce 208
128e5eba
SW
209 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
210 if (put_user(fsflags, ptr))
71b86f56
SW
211 error = -EFAULT;
212
213 gfs2_glock_dq_m(1, &gh);
214 gfs2_holder_uninit(&gh);
215 return error;
216}
217
6b124d8d
SW
218void gfs2_set_inode_flags(struct inode *inode)
219{
220 struct gfs2_inode *ip = GFS2_I(inode);
221 struct gfs2_dinode_host *di = &ip->i_di;
222 unsigned int flags = inode->i_flags;
223
224 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
225 if (di->di_flags & GFS2_DIF_IMMUTABLE)
226 flags |= S_IMMUTABLE;
227 if (di->di_flags & GFS2_DIF_APPENDONLY)
228 flags |= S_APPEND;
229 if (di->di_flags & GFS2_DIF_NOATIME)
230 flags |= S_NOATIME;
231 if (di->di_flags & GFS2_DIF_SYNC)
232 flags |= S_SYNC;
233 inode->i_flags = flags;
234}
235
71b86f56
SW
236/* Flags that can be set by user space */
237#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
238 GFS2_DIF_DIRECTIO| \
239 GFS2_DIF_IMMUTABLE| \
240 GFS2_DIF_APPENDONLY| \
241 GFS2_DIF_NOATIME| \
242 GFS2_DIF_SYNC| \
243 GFS2_DIF_SYSTEM| \
244 GFS2_DIF_INHERIT_DIRECTIO| \
245 GFS2_DIF_INHERIT_JDATA)
246
247/**
248 * gfs2_set_flags - set flags on an inode
249 * @inode: The inode
250 * @flags: The flags to set
251 * @mask: Indicates which flags are valid
252 *
253 */
b09e593d 254static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
71b86f56 255{
81454098 256 struct inode *inode = filp->f_path.dentry->d_inode;
feaa7bba
SW
257 struct gfs2_inode *ip = GFS2_I(inode);
258 struct gfs2_sbd *sdp = GFS2_SB(inode);
71b86f56
SW
259 struct buffer_head *bh;
260 struct gfs2_holder gh;
261 int error;
55eccc6d 262 u32 new_flags, flags;
71b86f56 263
71b86f56 264 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
52f341cf 265 if (error)
71b86f56
SW
266 return error;
267
55eccc6d
SW
268 flags = ip->i_di.di_flags;
269 new_flags = (flags & ~mask) | (reqflags & mask);
71b86f56
SW
270 if ((new_flags ^ flags) == 0)
271 goto out;
272
4bcf7091
SW
273 if (S_ISDIR(inode->i_mode)) {
274 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
275 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
276 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
277 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
278 }
279
71b86f56
SW
280 error = -EINVAL;
281 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
282 goto out;
283
71b86f56
SW
284 error = -EPERM;
285 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
286 goto out;
287 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
288 goto out;
907b9bce 289 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
b9cb9813 290 !capable(CAP_LINUX_IMMUTABLE))
71b86f56 291 goto out;
b9cb9813 292 if (!IS_IMMUTABLE(inode)) {
faf450ef 293 error = permission(inode, MAY_WRITE, NULL);
b9cb9813
SW
294 if (error)
295 goto out;
296 }
71b86f56 297
55eccc6d 298 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
71b86f56
SW
299 if (error)
300 goto out;
55eccc6d
SW
301 error = gfs2_meta_inode_buffer(ip, &bh);
302 if (error)
303 goto out_trans_end;
71b86f56
SW
304 gfs2_trans_add_bh(ip->i_gl, bh, 1);
305 ip->i_di.di_flags = new_flags;
539e5d6b 306 gfs2_dinode_out(ip, bh->b_data);
71b86f56 307 brelse(bh);
6b124d8d 308 gfs2_set_inode_flags(inode);
55eccc6d
SW
309out_trans_end:
310 gfs2_trans_end(sdp);
71b86f56
SW
311out:
312 gfs2_glock_dq_uninit(&gh);
313 return error;
314}
315
b09e593d 316static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
71b86f56 317{
128e5eba
SW
318 u32 fsflags, gfsflags;
319 if (get_user(fsflags, ptr))
71b86f56 320 return -EFAULT;
128e5eba 321 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
b09e593d 322 return do_gfs2_set_flags(filp, gfsflags, ~0);
71b86f56
SW
323}
324
b09e593d 325static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
71b86f56
SW
326{
327 switch(cmd) {
128e5eba 328 case FS_IOC_GETFLAGS:
b09e593d 329 return gfs2_get_flags(filp, (u32 __user *)arg);
128e5eba 330 case FS_IOC_SETFLAGS:
b09e593d 331 return gfs2_set_flags(filp, (u32 __user *)arg);
71b86f56
SW
332 }
333 return -ENOTTY;
334}
335
336
b3b94faa
DT
337/**
338 * gfs2_mmap -
339 * @file: The file to map
340 * @vma: The VMA which described the mapping
341 *
342 * Returns: 0 or error code
343 */
344
345static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
346{
feaa7bba 347 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
b3b94faa
DT
348 struct gfs2_holder i_gh;
349 int error;
350
b3b94faa
DT
351 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
352 error = gfs2_glock_nq_atime(&i_gh);
353 if (error) {
354 gfs2_holder_uninit(&i_gh);
355 return error;
356 }
357
18ec7d5c
SW
358 /* This is VM_MAYWRITE instead of VM_WRITE because a call
359 to mprotect() can turn on VM_WRITE later. */
360
361 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
362 (VM_MAYSHARE | VM_MAYWRITE))
363 vma->vm_ops = &gfs2_vm_ops_sharewrite;
364 else
365 vma->vm_ops = &gfs2_vm_ops_private;
b3b94faa
DT
366
367 gfs2_glock_dq_uninit(&i_gh);
368
369 return error;
370}
371
372/**
373 * gfs2_open - open a file
374 * @inode: the inode to open
375 * @file: the struct file for this opening
376 *
377 * Returns: errno
378 */
379
380static int gfs2_open(struct inode *inode, struct file *file)
381{
feaa7bba 382 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
383 struct gfs2_holder i_gh;
384 struct gfs2_file *fp;
385 int error;
386
b3b94faa
DT
387 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
388 if (!fp)
389 return -ENOMEM;
390
f55ab26a 391 mutex_init(&fp->f_fl_mutex);
b3b94faa 392
feaa7bba 393 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
5c676f6d 394 file->private_data = fp;
b3b94faa 395
b60623c2 396 if (S_ISREG(ip->i_inode.i_mode)) {
b3b94faa
DT
397 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
398 &i_gh);
399 if (error)
400 goto fail;
401
402 if (!(file->f_flags & O_LARGEFILE) &&
403 ip->i_di.di_size > MAX_NON_LFS) {
404 error = -EFBIG;
405 goto fail_gunlock;
406 }
407
408 /* Listen to the Direct I/O flag */
409
410 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
411 file->f_flags |= O_DIRECT;
412
b3b94faa
DT
413 gfs2_glock_dq_uninit(&i_gh);
414 }
415
416 return 0;
417
420b9e5e 418fail_gunlock:
b3b94faa 419 gfs2_glock_dq_uninit(&i_gh);
420b9e5e 420fail:
5c676f6d 421 file->private_data = NULL;
b3b94faa 422 kfree(fp);
b3b94faa
DT
423 return error;
424}
425
426/**
427 * gfs2_close - called to close a struct file
428 * @inode: the inode the struct file belongs to
429 * @file: the struct file being closed
430 *
431 * Returns: errno
432 */
433
434static int gfs2_close(struct inode *inode, struct file *file)
435{
5c676f6d 436 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
b3b94faa
DT
437 struct gfs2_file *fp;
438
5c676f6d
SW
439 fp = file->private_data;
440 file->private_data = NULL;
b3b94faa
DT
441
442 if (gfs2_assert_warn(sdp, fp))
443 return -EIO;
444
445 kfree(fp);
446
447 return 0;
448}
449
450/**
451 * gfs2_fsync - sync the dirty data for a file (across the cluster)
452 * @file: the file that points to the dentry (we ignore this)
453 * @dentry: the dentry that points to the inode to sync
454 *
33c3de32
SW
455 * The VFS will flush "normal" data for us. We only need to worry
456 * about metadata here. For journaled data, we just do a log flush
457 * as we can't avoid it. Otherwise we can just bale out if datasync
458 * is set. For stuffed inodes we must flush the log in order to
459 * ensure that all data is on disk.
460 *
34126f9f
SW
461 * The call to write_inode_now() is there to write back metadata and
462 * the inode itself. It does also try and write the data, but thats
463 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
464 * for us.
465 *
b3b94faa
DT
466 * Returns: errno
467 */
468
469static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
470{
33c3de32
SW
471 struct inode *inode = dentry->d_inode;
472 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
473 int ret = 0;
33c3de32
SW
474
475 if (gfs2_is_jdata(GFS2_I(inode))) {
476 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
477 return 0;
478 }
b3b94faa 479
33c3de32
SW
480 if (sync_state != 0) {
481 if (!datasync)
34126f9f 482 ret = write_inode_now(inode, 0);
b3b94faa 483
33c3de32
SW
484 if (gfs2_is_stuffed(GFS2_I(inode)))
485 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
486 }
487
488 return ret;
b3b94faa
DT
489}
490
60446067
ME
491/**
492 * gfs2_setlease - acquire/release a file lease
493 * @file: the file pointer
494 * @arg: lease type
495 * @fl: file lock
496 *
497 * Returns: errno
498 */
499
500static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
501{
502 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
503
504 /*
505 * We don't currently have a way to enforce a lease across the whole
506 * cluster; until we do, disable leases (by just returning -EINVAL),
507 * unless the administrator has requested purely local locking.
508 */
509 if (!sdp->sd_args.ar_localflocks)
510 return -EINVAL;
0af1a450 511 return generic_setlease(file, arg, fl);
60446067
ME
512}
513
b3b94faa
DT
514/**
515 * gfs2_lock - acquire/release a posix lock on a file
516 * @file: the file pointer
517 * @cmd: either modify or retrieve lock state, possibly wait
518 * @fl: type and range of lock
519 *
520 * Returns: errno
521 */
522
523static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
524{
feaa7bba
SW
525 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
526 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
b3b94faa 527 struct lm_lockname name =
dbb7cae2 528 { .ln_number = ip->i_no_addr,
b3b94faa
DT
529 .ln_type = LM_TYPE_PLOCK };
530
b3b94faa
DT
531 if (!(fl->fl_flags & FL_POSIX))
532 return -ENOLCK;
b60623c2 533 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
b3b94faa
DT
534 return -ENOLCK;
535
536 if (sdp->sd_args.ar_localflocks) {
537 if (IS_GETLK(cmd)) {
9d6a8c5c 538 posix_test_lock(file, fl);
b3b94faa
DT
539 return 0;
540 } else {
8628de05 541 return posix_lock_file_wait(file, fl);
b3b94faa
DT
542 }
543 }
544
586759f0
ME
545 if (cmd == F_CANCELLK) {
546 /* Hack: */
547 cmd = F_SETLK;
548 fl->fl_type = F_UNLCK;
549 }
b3b94faa
DT
550 if (IS_GETLK(cmd))
551 return gfs2_lm_plock_get(sdp, &name, file, fl);
552 else if (fl->fl_type == F_UNLCK)
553 return gfs2_lm_punlock(sdp, &name, file, fl);
554 else
555 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
556}
557
b3b94faa
DT
558static int do_flock(struct file *file, int cmd, struct file_lock *fl)
559{
5c676f6d 560 struct gfs2_file *fp = file->private_data;
b3b94faa 561 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
81454098 562 struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
b3b94faa
DT
563 struct gfs2_glock *gl;
564 unsigned int state;
565 int flags;
566 int error = 0;
567
568 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
26c1a574 569 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
b3b94faa 570
f55ab26a 571 mutex_lock(&fp->f_fl_mutex);
b3b94faa
DT
572
573 gl = fl_gh->gh_gl;
574 if (gl) {
575 if (fl_gh->gh_state == state)
576 goto out;
577 gfs2_glock_hold(gl);
578 flock_lock_file_wait(file,
907b9bce 579 &(struct file_lock){.fl_type = F_UNLCK});
b3b94faa
DT
580 gfs2_glock_dq_uninit(fl_gh);
581 } else {
feaa7bba 582 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
dbb7cae2 583 ip->i_no_addr, &gfs2_flock_glops,
b3b94faa
DT
584 CREATE, &gl);
585 if (error)
586 goto out;
587 }
588
589 gfs2_holder_init(gl, state, flags, fl_gh);
590 gfs2_glock_put(gl);
591
592 error = gfs2_glock_nq(fl_gh);
593 if (error) {
594 gfs2_holder_uninit(fl_gh);
595 if (error == GLR_TRYFAILED)
596 error = -EAGAIN;
597 } else {
598 error = flock_lock_file_wait(file, fl);
feaa7bba 599 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
b3b94faa
DT
600 }
601
420b9e5e 602out:
f55ab26a 603 mutex_unlock(&fp->f_fl_mutex);
b3b94faa
DT
604 return error;
605}
606
607static void do_unflock(struct file *file, struct file_lock *fl)
608{
5c676f6d 609 struct gfs2_file *fp = file->private_data;
b3b94faa
DT
610 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
611
f55ab26a 612 mutex_lock(&fp->f_fl_mutex);
b3b94faa
DT
613 flock_lock_file_wait(file, fl);
614 if (fl_gh->gh_gl)
615 gfs2_glock_dq_uninit(fl_gh);
f55ab26a 616 mutex_unlock(&fp->f_fl_mutex);
b3b94faa
DT
617}
618
619/**
620 * gfs2_flock - acquire/release a flock lock on a file
621 * @file: the file pointer
622 * @cmd: either modify or retrieve lock state, possibly wait
623 * @fl: type and range of lock
624 *
625 * Returns: errno
626 */
627
628static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
629{
feaa7bba
SW
630 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
631 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
b3b94faa 632
b3b94faa
DT
633 if (!(fl->fl_flags & FL_FLOCK))
634 return -ENOLCK;
b60623c2 635 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
b3b94faa
DT
636 return -ENOLCK;
637
638 if (sdp->sd_args.ar_localflocks)
639 return flock_lock_file_wait(file, fl);
640
641 if (fl->fl_type == F_UNLCK) {
642 do_unflock(file, fl);
643 return 0;
d00223f1 644 } else {
b3b94faa 645 return do_flock(file, cmd, fl);
d00223f1 646 }
b3b94faa
DT
647}
648
b0dd9308 649const struct file_operations gfs2_file_fops = {
26c1a574 650 .llseek = gfs2_llseek,
d00223f1 651 .read = do_sync_read,
26c1a574 652 .aio_read = generic_file_aio_read,
d00223f1 653 .write = do_sync_write,
26c1a574
SW
654 .aio_write = generic_file_aio_write,
655 .unlocked_ioctl = gfs2_ioctl,
656 .mmap = gfs2_mmap,
657 .open = gfs2_open,
658 .release = gfs2_close,
659 .fsync = gfs2_fsync,
660 .lock = gfs2_lock,
26c1a574
SW
661 .flock = gfs2_flock,
662 .splice_read = generic_file_splice_read,
663 .splice_write = generic_file_splice_write,
60446067 664 .setlease = gfs2_setlease,
b3b94faa
DT
665};
666
b0dd9308 667const struct file_operations gfs2_dir_fops = {
26c1a574
SW
668 .readdir = gfs2_readdir,
669 .unlocked_ioctl = gfs2_ioctl,
670 .open = gfs2_open,
671 .release = gfs2_close,
672 .fsync = gfs2_fsync,
673 .lock = gfs2_lock,
674 .flock = gfs2_flock,
b3b94faa
DT
675};
676