[GFS2] don't call permission()
[linux-2.6] / fs / gfs2 / ops_file.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
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
7  * of the GNU General Public License version 2.
8  */
9
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>
18 #include <linux/mount.h>
19 #include <linux/fs.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <linux/ext2_fs.h>
22 #include <linux/crc32.h>
23 #include <linux/lm_interface.h>
24 #include <linux/writeback.h>
25 #include <asm/uaccess.h>
26
27 #include "gfs2.h"
28 #include "incore.h"
29 #include "bmap.h"
30 #include "dir.h"
31 #include "glock.h"
32 #include "glops.h"
33 #include "inode.h"
34 #include "log.h"
35 #include "meta_io.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39 #include "util.h"
40 #include "eaops.h"
41 #include "ops_address.h"
42 #include "ops_inode.h"
43
44 /**
45  * gfs2_llseek - seek to a location in a file
46  * @file: the file
47  * @offset: the offset
48  * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
49  *
50  * SEEK_END requires the glock for the file because it references the
51  * file's size.
52  *
53  * Returns: The new offset, or errno
54  */
55
56 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
57 {
58         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
59         struct gfs2_holder i_gh;
60         loff_t error;
61
62         if (origin == 2) {
63                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
64                                            &i_gh);
65                 if (!error) {
66                         error = remote_llseek(file, offset, origin);
67                         gfs2_glock_dq_uninit(&i_gh);
68                 }
69         } else
70                 error = remote_llseek(file, offset, origin);
71
72         return error;
73 }
74
75 /**
76  * gfs2_readdir - Read directory entries from a directory
77  * @file: The directory to read from
78  * @dirent: Buffer for dirents
79  * @filldir: Function used to do the copying
80  *
81  * Returns: errno
82  */
83
84 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
85 {
86         struct inode *dir = file->f_mapping->host;
87         struct gfs2_inode *dip = GFS2_I(dir);
88         struct gfs2_holder d_gh;
89         u64 offset = file->f_pos;
90         int error;
91
92         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
93         error = gfs2_glock_nq_atime(&d_gh);
94         if (error) {
95                 gfs2_holder_uninit(&d_gh);
96                 return error;
97         }
98
99         error = gfs2_dir_read(dir, &offset, dirent, filldir);
100
101         gfs2_glock_dq_uninit(&d_gh);
102
103         file->f_pos = offset;
104
105         return error;
106 }
107
108 /**
109  * fsflags_cvt
110  * @table: A table of 32 u32 flags
111  * @val: a 32 bit value to convert
112  *
113  * This function can be used to convert between fsflags values and
114  * GFS2's own flags values.
115  *
116  * Returns: the converted flags
117  */
118 static u32 fsflags_cvt(const u32 *table, u32 val)
119 {
120         u32 res = 0;
121         while(val) {
122                 if (val & 1)
123                         res |= *table;
124                 table++;
125                 val >>= 1;
126         }
127         return res;
128 }
129
130 static const u32 fsflags_to_gfs2[32] = {
131         [3] = GFS2_DIF_SYNC,
132         [4] = GFS2_DIF_IMMUTABLE,
133         [5] = GFS2_DIF_APPENDONLY,
134         [7] = GFS2_DIF_NOATIME,
135         [12] = GFS2_DIF_EXHASH,
136         [14] = GFS2_DIF_INHERIT_JDATA,
137         [20] = GFS2_DIF_INHERIT_DIRECTIO,
138 };
139
140 static const u32 gfs2_to_fsflags[32] = {
141         [gfs2fl_Sync] = FS_SYNC_FL,
142         [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
143         [gfs2fl_AppendOnly] = FS_APPEND_FL,
144         [gfs2fl_NoAtime] = FS_NOATIME_FL,
145         [gfs2fl_ExHash] = FS_INDEX_FL,
146         [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
147         [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
148 };
149
150 static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
151 {
152         struct inode *inode = filp->f_path.dentry->d_inode;
153         struct gfs2_inode *ip = GFS2_I(inode);
154         struct gfs2_holder gh;
155         int error;
156         u32 fsflags;
157
158         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
159         error = gfs2_glock_nq_atime(&gh);
160         if (error)
161                 return error;
162
163         fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
164         if (!S_ISDIR(inode->i_mode)) {
165                 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
166                         fsflags |= FS_JOURNAL_DATA_FL;
167                 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
168                         fsflags |= FS_DIRECTIO_FL;
169         }
170         if (put_user(fsflags, ptr))
171                 error = -EFAULT;
172
173         gfs2_glock_dq(&gh);
174         gfs2_holder_uninit(&gh);
175         return error;
176 }
177
178 void gfs2_set_inode_flags(struct inode *inode)
179 {
180         struct gfs2_inode *ip = GFS2_I(inode);
181         struct gfs2_dinode_host *di = &ip->i_di;
182         unsigned int flags = inode->i_flags;
183
184         flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
185         if (di->di_flags & GFS2_DIF_IMMUTABLE)
186                 flags |= S_IMMUTABLE;
187         if (di->di_flags & GFS2_DIF_APPENDONLY)
188                 flags |= S_APPEND;
189         if (di->di_flags & GFS2_DIF_NOATIME)
190                 flags |= S_NOATIME;
191         if (di->di_flags & GFS2_DIF_SYNC)
192                 flags |= S_SYNC;
193         inode->i_flags = flags;
194 }
195
196 /* Flags that can be set by user space */
197 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA|                    \
198                              GFS2_DIF_DIRECTIO|                 \
199                              GFS2_DIF_IMMUTABLE|                \
200                              GFS2_DIF_APPENDONLY|               \
201                              GFS2_DIF_NOATIME|                  \
202                              GFS2_DIF_SYNC|                     \
203                              GFS2_DIF_SYSTEM|                   \
204                              GFS2_DIF_INHERIT_DIRECTIO|         \
205                              GFS2_DIF_INHERIT_JDATA)
206
207 /**
208  * gfs2_set_flags - set flags on an inode
209  * @inode: The inode
210  * @flags: The flags to set
211  * @mask: Indicates which flags are valid
212  *
213  */
214 static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
215 {
216         struct inode *inode = filp->f_path.dentry->d_inode;
217         struct gfs2_inode *ip = GFS2_I(inode);
218         struct gfs2_sbd *sdp = GFS2_SB(inode);
219         struct buffer_head *bh;
220         struct gfs2_holder gh;
221         int error;
222         u32 new_flags, flags;
223
224         error = mnt_want_write(filp->f_path.mnt);
225         if (error)
226                 return error;
227
228         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
229         if (error)
230                 goto out_drop_write;
231
232         flags = ip->i_di.di_flags;
233         new_flags = (flags & ~mask) | (reqflags & mask);
234         if ((new_flags ^ flags) == 0)
235                 goto out;
236
237         error = -EINVAL;
238         if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
239                 goto out;
240
241         error = -EPERM;
242         if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
243                 goto out;
244         if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
245                 goto out;
246         if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
247             !capable(CAP_LINUX_IMMUTABLE))
248                 goto out;
249         if (!IS_IMMUTABLE(inode)) {
250                 error = gfs2_permission(inode, MAY_WRITE);
251                 if (error)
252                         goto out;
253         }
254         if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
255                 if (flags & GFS2_DIF_JDATA)
256                         gfs2_log_flush(sdp, ip->i_gl);
257                 error = filemap_fdatawrite(inode->i_mapping);
258                 if (error)
259                         goto out;
260                 error = filemap_fdatawait(inode->i_mapping);
261                 if (error)
262                         goto out;
263         }
264         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
265         if (error)
266                 goto out;
267         error = gfs2_meta_inode_buffer(ip, &bh);
268         if (error)
269                 goto out_trans_end;
270         gfs2_trans_add_bh(ip->i_gl, bh, 1);
271         ip->i_di.di_flags = new_flags;
272         gfs2_dinode_out(ip, bh->b_data);
273         brelse(bh);
274         gfs2_set_inode_flags(inode);
275         gfs2_set_aops(inode);
276 out_trans_end:
277         gfs2_trans_end(sdp);
278 out:
279         gfs2_glock_dq_uninit(&gh);
280 out_drop_write:
281         mnt_drop_write(filp->f_path.mnt);
282         return error;
283 }
284
285 static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
286 {
287         struct inode *inode = filp->f_path.dentry->d_inode;
288         u32 fsflags, gfsflags;
289         if (get_user(fsflags, ptr))
290                 return -EFAULT;
291         gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
292         if (!S_ISDIR(inode->i_mode)) {
293                 if (gfsflags & GFS2_DIF_INHERIT_JDATA)
294                         gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
295                 if (gfsflags & GFS2_DIF_INHERIT_DIRECTIO)
296                         gfsflags ^= (GFS2_DIF_DIRECTIO | GFS2_DIF_INHERIT_DIRECTIO);
297                 return do_gfs2_set_flags(filp, gfsflags, ~0);
298         }
299         return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
300 }
301
302 static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
303 {
304         switch(cmd) {
305         case FS_IOC_GETFLAGS:
306                 return gfs2_get_flags(filp, (u32 __user *)arg);
307         case FS_IOC_SETFLAGS:
308                 return gfs2_set_flags(filp, (u32 __user *)arg);
309         }
310         return -ENOTTY;
311 }
312
313 /**
314  * gfs2_allocate_page_backing - Use bmap to allocate blocks
315  * @page: The (locked) page to allocate backing for
316  *
317  * We try to allocate all the blocks required for the page in
318  * one go. This might fail for various reasons, so we keep
319  * trying until all the blocks to back this page are allocated.
320  * If some of the blocks are already allocated, thats ok too.
321  */
322
323 static int gfs2_allocate_page_backing(struct page *page)
324 {
325         struct inode *inode = page->mapping->host;
326         struct buffer_head bh;
327         unsigned long size = PAGE_CACHE_SIZE;
328         u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
329
330         do {
331                 bh.b_state = 0;
332                 bh.b_size = size;
333                 gfs2_block_map(inode, lblock, &bh, 1);
334                 if (!buffer_mapped(&bh))
335                         return -EIO;
336                 size -= bh.b_size;
337                 lblock += (bh.b_size >> inode->i_blkbits);
338         } while(size > 0);
339         return 0;
340 }
341
342 /**
343  * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
344  * @vma: The virtual memory area
345  * @page: The page which is about to become writable
346  *
347  * When the page becomes writable, we need to ensure that we have
348  * blocks allocated on disk to back that page.
349  */
350
351 static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
352 {
353         struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
354         struct gfs2_inode *ip = GFS2_I(inode);
355         struct gfs2_sbd *sdp = GFS2_SB(inode);
356         unsigned long last_index;
357         u64 pos = page->index << (PAGE_CACHE_SIZE - inode->i_blkbits);
358         unsigned int data_blocks, ind_blocks, rblocks;
359         int alloc_required = 0;
360         struct gfs2_holder gh;
361         struct gfs2_alloc *al;
362         int ret;
363
364         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &gh);
365         ret = gfs2_glock_nq_atime(&gh);
366         if (ret)
367                 goto out;
368
369         set_bit(GIF_SW_PAGED, &ip->i_flags);
370         gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
371         ret = gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE, &alloc_required);
372         if (ret || !alloc_required)
373                 goto out_unlock;
374         ret = -ENOMEM;
375         al = gfs2_alloc_get(ip);
376         if (al == NULL)
377                 goto out_unlock;
378
379         ret = gfs2_quota_lock_check(ip);
380         if (ret)
381                 goto out_alloc_put;
382         al->al_requested = data_blocks + ind_blocks;
383         ret = gfs2_inplace_reserve(ip);
384         if (ret)
385                 goto out_quota_unlock;
386
387         rblocks = RES_DINODE + ind_blocks;
388         if (gfs2_is_jdata(ip))
389                 rblocks += data_blocks ? data_blocks : 1;
390         if (ind_blocks || data_blocks)
391                 rblocks += RES_STATFS + RES_QUOTA;
392         ret = gfs2_trans_begin(sdp, rblocks, 0);
393         if (ret)
394                 goto out_trans_fail;
395
396         lock_page(page);
397         ret = -EINVAL;
398         last_index = ip->i_inode.i_size >> PAGE_CACHE_SHIFT;
399         if (page->index > last_index)
400                 goto out_unlock_page;
401         ret = 0;
402         if (!PageUptodate(page) || page->mapping != ip->i_inode.i_mapping)
403                 goto out_unlock_page;
404         if (gfs2_is_stuffed(ip)) {
405                 ret = gfs2_unstuff_dinode(ip, page);
406                 if (ret)
407                         goto out_unlock_page;
408         }
409         ret = gfs2_allocate_page_backing(page);
410
411 out_unlock_page:
412         unlock_page(page);
413         gfs2_trans_end(sdp);
414 out_trans_fail:
415         gfs2_inplace_release(ip);
416 out_quota_unlock:
417         gfs2_quota_unlock(ip);
418 out_alloc_put:
419         gfs2_alloc_put(ip);
420 out_unlock:
421         gfs2_glock_dq(&gh);
422 out:
423         gfs2_holder_uninit(&gh);
424         return ret;
425 }
426
427 static struct vm_operations_struct gfs2_vm_ops = {
428         .fault = filemap_fault,
429         .page_mkwrite = gfs2_page_mkwrite,
430 };
431
432
433 /**
434  * gfs2_mmap -
435  * @file: The file to map
436  * @vma: The VMA which described the mapping
437  *
438  * Returns: 0 or error code
439  */
440
441 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
442 {
443         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
444         struct gfs2_holder i_gh;
445         int error;
446
447         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
448         error = gfs2_glock_nq_atime(&i_gh);
449         if (error) {
450                 gfs2_holder_uninit(&i_gh);
451                 return error;
452         }
453
454         vma->vm_ops = &gfs2_vm_ops;
455
456         gfs2_glock_dq_uninit(&i_gh);
457
458         return error;
459 }
460
461 /**
462  * gfs2_open - open a file
463  * @inode: the inode to open
464  * @file: the struct file for this opening
465  *
466  * Returns: errno
467  */
468
469 static int gfs2_open(struct inode *inode, struct file *file)
470 {
471         struct gfs2_inode *ip = GFS2_I(inode);
472         struct gfs2_holder i_gh;
473         struct gfs2_file *fp;
474         int error;
475
476         fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
477         if (!fp)
478                 return -ENOMEM;
479
480         mutex_init(&fp->f_fl_mutex);
481
482         gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
483         file->private_data = fp;
484
485         if (S_ISREG(ip->i_inode.i_mode)) {
486                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
487                                            &i_gh);
488                 if (error)
489                         goto fail;
490
491                 if (!(file->f_flags & O_LARGEFILE) &&
492                     ip->i_di.di_size > MAX_NON_LFS) {
493                         error = -EOVERFLOW;
494                         goto fail_gunlock;
495                 }
496
497                 /* Listen to the Direct I/O flag */
498
499                 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
500                         file->f_flags |= O_DIRECT;
501
502                 gfs2_glock_dq_uninit(&i_gh);
503         }
504
505         return 0;
506
507 fail_gunlock:
508         gfs2_glock_dq_uninit(&i_gh);
509 fail:
510         file->private_data = NULL;
511         kfree(fp);
512         return error;
513 }
514
515 /**
516  * gfs2_close - called to close a struct file
517  * @inode: the inode the struct file belongs to
518  * @file: the struct file being closed
519  *
520  * Returns: errno
521  */
522
523 static int gfs2_close(struct inode *inode, struct file *file)
524 {
525         struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
526         struct gfs2_file *fp;
527
528         fp = file->private_data;
529         file->private_data = NULL;
530
531         if (gfs2_assert_warn(sdp, fp))
532                 return -EIO;
533
534         kfree(fp);
535
536         return 0;
537 }
538
539 /**
540  * gfs2_fsync - sync the dirty data for a file (across the cluster)
541  * @file: the file that points to the dentry (we ignore this)
542  * @dentry: the dentry that points to the inode to sync
543  *
544  * The VFS will flush "normal" data for us. We only need to worry
545  * about metadata here. For journaled data, we just do a log flush
546  * as we can't avoid it. Otherwise we can just bale out if datasync
547  * is set. For stuffed inodes we must flush the log in order to
548  * ensure that all data is on disk.
549  *
550  * The call to write_inode_now() is there to write back metadata and
551  * the inode itself. It does also try and write the data, but thats
552  * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
553  * for us.
554  *
555  * Returns: errno
556  */
557
558 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
559 {
560         struct inode *inode = dentry->d_inode;
561         int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
562         int ret = 0;
563
564         if (gfs2_is_jdata(GFS2_I(inode))) {
565                 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
566                 return 0;
567         }
568
569         if (sync_state != 0) {
570                 if (!datasync)
571                         ret = write_inode_now(inode, 0);
572
573                 if (gfs2_is_stuffed(GFS2_I(inode)))
574                         gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
575         }
576
577         return ret;
578 }
579
580 /**
581  * gfs2_setlease - acquire/release a file lease
582  * @file: the file pointer
583  * @arg: lease type
584  * @fl: file lock
585  *
586  * Returns: errno
587  */
588
589 static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
590 {
591         struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
592
593         /*
594          * We don't currently have a way to enforce a lease across the whole
595          * cluster; until we do, disable leases (by just returning -EINVAL),
596          * unless the administrator has requested purely local locking.
597          */
598         if (!sdp->sd_args.ar_localflocks)
599                 return -EINVAL;
600         return generic_setlease(file, arg, fl);
601 }
602
603 static int gfs2_lm_plock_get(struct gfs2_sbd *sdp, struct lm_lockname *name,
604                       struct file *file, struct file_lock *fl)
605 {
606         int error = -EIO;
607         if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
608                 error = sdp->sd_lockstruct.ls_ops->lm_plock_get(
609                                 sdp->sd_lockstruct.ls_lockspace, name, file, fl);
610         return error;
611 }
612
613 static int gfs2_lm_plock(struct gfs2_sbd *sdp, struct lm_lockname *name,
614                   struct file *file, int cmd, struct file_lock *fl)
615 {
616         int error = -EIO;
617         if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
618                 error = sdp->sd_lockstruct.ls_ops->lm_plock(
619                                 sdp->sd_lockstruct.ls_lockspace, name, file, cmd, fl);
620         return error;
621 }
622
623 static int gfs2_lm_punlock(struct gfs2_sbd *sdp, struct lm_lockname *name,
624                     struct file *file, struct file_lock *fl)
625 {
626         int error = -EIO;
627         if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
628                 error = sdp->sd_lockstruct.ls_ops->lm_punlock(
629                                 sdp->sd_lockstruct.ls_lockspace, name, file, fl);
630         return error;
631 }
632
633 /**
634  * gfs2_lock - acquire/release a posix lock on a file
635  * @file: the file pointer
636  * @cmd: either modify or retrieve lock state, possibly wait
637  * @fl: type and range of lock
638  *
639  * Returns: errno
640  */
641
642 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
643 {
644         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
645         struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
646         struct lm_lockname name =
647                 { .ln_number = ip->i_no_addr,
648                   .ln_type = LM_TYPE_PLOCK };
649
650         if (!(fl->fl_flags & FL_POSIX))
651                 return -ENOLCK;
652         if (__mandatory_lock(&ip->i_inode))
653                 return -ENOLCK;
654
655         if (cmd == F_CANCELLK) {
656                 /* Hack: */
657                 cmd = F_SETLK;
658                 fl->fl_type = F_UNLCK;
659         }
660         if (IS_GETLK(cmd))
661                 return gfs2_lm_plock_get(sdp, &name, file, fl);
662         else if (fl->fl_type == F_UNLCK)
663                 return gfs2_lm_punlock(sdp, &name, file, fl);
664         else
665                 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
666 }
667
668 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
669 {
670         struct gfs2_file *fp = file->private_data;
671         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
672         struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
673         struct gfs2_glock *gl;
674         unsigned int state;
675         int flags;
676         int error = 0;
677
678         state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
679         flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
680
681         mutex_lock(&fp->f_fl_mutex);
682
683         gl = fl_gh->gh_gl;
684         if (gl) {
685                 if (fl_gh->gh_state == state)
686                         goto out;
687                 flock_lock_file_wait(file,
688                                      &(struct file_lock){.fl_type = F_UNLCK});
689                 gfs2_glock_dq_wait(fl_gh);
690                 gfs2_holder_reinit(state, flags, fl_gh);
691         } else {
692                 error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
693                                        &gfs2_flock_glops, CREATE, &gl);
694                 if (error)
695                         goto out;
696                 gfs2_holder_init(gl, state, flags, fl_gh);
697                 gfs2_glock_put(gl);
698         }
699         error = gfs2_glock_nq(fl_gh);
700         if (error) {
701                 gfs2_holder_uninit(fl_gh);
702                 if (error == GLR_TRYFAILED)
703                         error = -EAGAIN;
704         } else {
705                 error = flock_lock_file_wait(file, fl);
706                 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
707         }
708
709 out:
710         mutex_unlock(&fp->f_fl_mutex);
711         return error;
712 }
713
714 static void do_unflock(struct file *file, struct file_lock *fl)
715 {
716         struct gfs2_file *fp = file->private_data;
717         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
718
719         mutex_lock(&fp->f_fl_mutex);
720         flock_lock_file_wait(file, fl);
721         if (fl_gh->gh_gl)
722                 gfs2_glock_dq_uninit(fl_gh);
723         mutex_unlock(&fp->f_fl_mutex);
724 }
725
726 /**
727  * gfs2_flock - acquire/release a flock lock on a file
728  * @file: the file pointer
729  * @cmd: either modify or retrieve lock state, possibly wait
730  * @fl: type and range of lock
731  *
732  * Returns: errno
733  */
734
735 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
736 {
737         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
738
739         if (!(fl->fl_flags & FL_FLOCK))
740                 return -ENOLCK;
741         if (__mandatory_lock(&ip->i_inode))
742                 return -ENOLCK;
743
744         if (fl->fl_type == F_UNLCK) {
745                 do_unflock(file, fl);
746                 return 0;
747         } else {
748                 return do_flock(file, cmd, fl);
749         }
750 }
751
752 const struct file_operations gfs2_file_fops = {
753         .llseek         = gfs2_llseek,
754         .read           = do_sync_read,
755         .aio_read       = generic_file_aio_read,
756         .write          = do_sync_write,
757         .aio_write      = generic_file_aio_write,
758         .unlocked_ioctl = gfs2_ioctl,
759         .mmap           = gfs2_mmap,
760         .open           = gfs2_open,
761         .release        = gfs2_close,
762         .fsync          = gfs2_fsync,
763         .lock           = gfs2_lock,
764         .flock          = gfs2_flock,
765         .splice_read    = generic_file_splice_read,
766         .splice_write   = generic_file_splice_write,
767         .setlease       = gfs2_setlease,
768 };
769
770 const struct file_operations gfs2_dir_fops = {
771         .readdir        = gfs2_readdir,
772         .unlocked_ioctl = gfs2_ioctl,
773         .open           = gfs2_open,
774         .release        = gfs2_close,
775         .fsync          = gfs2_fsync,
776         .lock           = gfs2_lock,
777         .flock          = gfs2_flock,
778 };
779
780 const struct file_operations gfs2_file_fops_nolock = {
781         .llseek         = gfs2_llseek,
782         .read           = do_sync_read,
783         .aio_read       = generic_file_aio_read,
784         .write          = do_sync_write,
785         .aio_write      = generic_file_aio_write,
786         .unlocked_ioctl = gfs2_ioctl,
787         .mmap           = gfs2_mmap,
788         .open           = gfs2_open,
789         .release        = gfs2_close,
790         .fsync          = gfs2_fsync,
791         .splice_read    = generic_file_splice_read,
792         .splice_write   = generic_file_splice_write,
793         .setlease       = gfs2_setlease,
794 };
795
796 const struct file_operations gfs2_dir_fops_nolock = {
797         .readdir        = gfs2_readdir,
798         .unlocked_ioctl = gfs2_ioctl,
799         .open           = gfs2_open,
800         .release        = gfs2_close,
801         .fsync          = gfs2_fsync,
802 };
803