2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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 v.2.
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>
19 #include <linux/smp_lock.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/crc32.h>
24 #include <linux/iflags.h>
25 #include <asm/semaphore.h>
26 #include <asm/uaccess.h>
29 #include "lm_interface.h"
47 /* "bad" is for NFS support */
48 struct filldir_bad_entry {
50 unsigned int fbe_length;
52 struct gfs2_inum fbe_inum;
53 unsigned int fbe_type;
57 struct gfs2_sbd *fdb_sbd;
59 struct filldir_bad_entry *fdb_entry;
60 unsigned int fdb_entry_num;
61 unsigned int fdb_entry_off;
64 unsigned int fdb_name_size;
65 unsigned int fdb_name_off;
68 /* For regular, non-NFS */
70 struct gfs2_sbd *fdr_sbd;
73 filldir_t fdr_filldir;
78 * Most fields left uninitialised to catch anybody who tries to
79 * use them. f_flags set to prevent file_accessed() from touching
80 * any other part of this. Its use is purely as a flag so that we
81 * know (in readpage()) whether or not do to locking.
83 struct file gfs2_internal_file_sentinal = {
84 .f_flags = O_NOATIME|O_RDONLY,
87 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
88 unsigned long offset, unsigned long size)
91 unsigned long count = desc->count;
97 memcpy(desc->arg.buf, kaddr + offset, size);
100 desc->count = count - size;
101 desc->written += size;
102 desc->arg.buf += size;
106 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
107 char *buf, loff_t *pos, unsigned size)
109 struct inode *inode = ip->i_vnode;
110 read_descriptor_t desc;
115 do_generic_mapping_read(inode->i_mapping, ra_state,
116 &gfs2_internal_file_sentinal, pos, &desc,
118 return desc.written ? desc.written : desc.error;
122 * gfs2_llseek - seek to a location in a file
124 * @offset: the offset
125 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
127 * SEEK_END requires the glock for the file because it references the
130 * Returns: The new offset, or errno
133 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
135 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
136 struct gfs2_holder i_gh;
140 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
143 error = remote_llseek(file, offset, origin);
144 gfs2_glock_dq_uninit(&i_gh);
147 error = remote_llseek(file, offset, origin);
153 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
154 loff_t offset, unsigned long nr_segs)
156 struct file *file = iocb->ki_filp;
157 struct address_space *mapping = file->f_mapping;
160 retval = filemap_write_and_wait(mapping);
162 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
169 * __gfs2_file_aio_read - The main GFS2 read function
171 * N.B. This is almost, but not quite the same as __generic_file_aio_read()
172 * the important subtle different being that inode->i_size isn't valid
173 * unless we are holding a lock, and we do this _only_ on the O_DIRECT
174 * path since otherwise locking is done entirely at the page cache
177 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
178 const struct iovec *iov,
179 unsigned long nr_segs, loff_t *ppos)
181 struct file *filp = iocb->ki_filp;
182 struct gfs2_inode *ip = filp->f_mapping->host->u.generic_ip;
183 struct gfs2_holder gh;
189 for (seg = 0; seg < nr_segs; seg++) {
190 const struct iovec *iv = &iov[seg];
193 * If any segment has a negative length, or the cumulative
194 * length ever wraps negative then return -EINVAL.
196 count += iv->iov_len;
197 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
199 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
204 count -= iv->iov_len; /* This segment is no good */
208 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
209 if (filp->f_flags & O_DIRECT) {
210 loff_t pos = *ppos, size;
211 struct address_space *mapping;
214 mapping = filp->f_mapping;
215 inode = mapping->host;
218 goto out; /* skip atime */
220 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
221 retval = gfs2_glock_nq_m_atime(1, &gh);
224 if (gfs2_is_stuffed(ip)) {
225 gfs2_glock_dq_m(1, &gh);
226 gfs2_holder_uninit(&gh);
227 goto fallback_to_normal;
229 size = i_size_read(inode);
231 retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
232 if (retval > 0 && !is_sync_kiocb(iocb))
233 retval = -EIOCBQUEUED;
235 *ppos = pos + retval;
238 gfs2_glock_dq_m(1, &gh);
239 gfs2_holder_uninit(&gh);
246 for (seg = 0; seg < nr_segs; seg++) {
247 read_descriptor_t desc;
250 desc.arg.buf = iov[seg].iov_base;
251 desc.count = iov[seg].iov_len;
255 do_generic_file_read(filp,ppos,&desc,file_read_actor);
256 retval += desc.written;
258 retval = retval ?: desc.error;
268 * gfs2_read - Read bytes from a file
269 * @file: The file to read from
270 * @buf: The buffer to copy into
271 * @size: The amount of data requested
272 * @offset: The current file offset
274 * Outputs: Offset - updated according to number of bytes read
276 * Returns: The number of bytes read, errno on failure
279 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
282 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
286 init_sync_kiocb(&kiocb, filp);
287 ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
288 if (-EIOCBQUEUED == ret)
289 ret = wait_on_sync_kiocb(&kiocb);
293 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
294 unsigned long nr_segs, loff_t *ppos)
299 init_sync_kiocb(&kiocb, filp);
300 ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
301 if (-EIOCBQUEUED == ret)
302 ret = wait_on_sync_kiocb(&kiocb);
306 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
307 size_t count, loff_t pos)
309 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
311 BUG_ON(iocb->ki_pos != pos);
312 return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
317 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
318 * @opaque: opaque data used by the function
319 * @name: the name of the directory entry
320 * @length: the length of the name
321 * @offset: the entry's offset in the directory
322 * @inum: the inode number the entry points to
323 * @type: the type of inode the entry points to
325 * Returns: 0 on success, 1 if buffer full
328 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
329 uint64_t offset, struct gfs2_inum *inum,
332 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
333 struct gfs2_sbd *sdp = fdr->fdr_sbd;
336 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
337 inum->no_formal_ino, type);
341 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
342 gfs2_glock_prefetch_num(sdp,
343 inum->no_addr, &gfs2_inode_glops,
344 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
345 gfs2_glock_prefetch_num(sdp,
346 inum->no_addr, &gfs2_iopen_glops,
347 LM_ST_SHARED, LM_FLAG_TRY);
354 * readdir_reg - Read directory entries from a directory
355 * @file: The directory to read from
356 * @dirent: Buffer for dirents
357 * @filldir: Function used to do the copying
362 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
364 struct inode *dir = file->f_mapping->host;
365 struct gfs2_inode *dip = dir->u.generic_ip;
366 struct filldir_reg fdr;
367 struct gfs2_holder d_gh;
368 uint64_t offset = file->f_pos;
371 fdr.fdr_sbd = dip->i_sbd;
372 fdr.fdr_prefetch = 1;
373 fdr.fdr_filldir = filldir;
374 fdr.fdr_opaque = dirent;
376 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
377 error = gfs2_glock_nq_atime(&d_gh);
379 gfs2_holder_uninit(&d_gh);
383 error = gfs2_dir_read(dir, &offset, &fdr, filldir_reg_func);
385 gfs2_glock_dq_uninit(&d_gh);
387 file->f_pos = offset;
393 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
394 * @opaque: opaque data used by the function
395 * @name: the name of the directory entry
396 * @length: the length of the name
397 * @offset: the entry's offset in the directory
398 * @inum: the inode number the entry points to
399 * @type: the type of inode the entry points to
401 * For supporting NFS.
403 * Returns: 0 on success, 1 if buffer full
406 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
407 uint64_t offset, struct gfs2_inum *inum,
410 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
411 struct gfs2_sbd *sdp = fdb->fdb_sbd;
412 struct filldir_bad_entry *fbe;
414 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
415 fdb->fdb_name_off + length > fdb->fdb_name_size)
418 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
419 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
420 memcpy(fbe->fbe_name, name, length);
421 fbe->fbe_length = length;
422 fbe->fbe_offset = offset;
423 fbe->fbe_inum = *inum;
424 fbe->fbe_type = type;
426 fdb->fdb_entry_off++;
427 fdb->fdb_name_off += length;
429 if (!(length == 1 && *name == '.')) {
430 gfs2_glock_prefetch_num(sdp,
431 inum->no_addr, &gfs2_inode_glops,
432 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
433 gfs2_glock_prefetch_num(sdp,
434 inum->no_addr, &gfs2_iopen_glops,
435 LM_ST_SHARED, LM_FLAG_TRY);
442 * readdir_bad - Read directory entries from a directory
443 * @file: The directory to read from
444 * @dirent: Buffer for dirents
445 * @filldir: Function used to do the copying
447 * For supporting NFS.
452 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
454 struct inode *dir = file->f_mapping->host;
455 struct gfs2_inode *dip = dir->u.generic_ip;
456 struct gfs2_sbd *sdp = dip->i_sbd;
457 struct filldir_reg fdr;
458 unsigned int entries, size;
459 struct filldir_bad *fdb;
460 struct gfs2_holder d_gh;
461 uint64_t offset = file->f_pos;
463 struct filldir_bad_entry *fbe;
466 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
467 size = sizeof(struct filldir_bad) +
468 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
470 fdb = kzalloc(size, GFP_KERNEL);
475 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
476 fdb->fdb_entry_num = entries;
477 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
478 entries * sizeof(struct filldir_bad_entry);
479 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
481 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
482 error = gfs2_glock_nq_atime(&d_gh);
484 gfs2_holder_uninit(&d_gh);
488 error = gfs2_dir_read(dir, &offset, fdb, filldir_bad_func);
490 gfs2_glock_dq_uninit(&d_gh);
493 fdr.fdr_prefetch = 0;
494 fdr.fdr_filldir = filldir;
495 fdr.fdr_opaque = dirent;
497 for (x = 0; x < fdb->fdb_entry_off; x++) {
498 fbe = &fdb->fdb_entry[x];
500 error = filldir_reg_func(&fdr,
501 fbe->fbe_name, fbe->fbe_length,
503 &fbe->fbe_inum, fbe->fbe_type);
505 file->f_pos = fbe->fbe_offset;
511 file->f_pos = offset;
520 * gfs2_readdir - Read directory entries from a directory
521 * @file: The directory to read from
522 * @dirent: Buffer for dirents
523 * @filldir: Function used to do the copying
528 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
532 if (strcmp(current->comm, "nfsd") != 0)
533 error = readdir_reg(file, dirent, filldir);
535 error = readdir_bad(file, dirent, filldir);
540 static const u32 iflags_to_gfs2[32] = {
541 [iflag_Sync] = GFS2_DIF_SYNC,
542 [iflag_Immutable] = GFS2_DIF_IMMUTABLE,
543 [iflag_Append] = GFS2_DIF_APPENDONLY,
544 [iflag_NoAtime] = GFS2_DIF_NOATIME,
545 [iflag_Index] = GFS2_DIF_EXHASH,
546 [iflag_JournalData] = GFS2_DIF_JDATA,
547 [iflag_DirectIO] = GFS2_DIF_DIRECTIO,
548 [iflag_InheritDirectIO] = GFS2_DIF_INHERIT_DIRECTIO,
549 [iflag_InheritJdata] = GFS2_DIF_INHERIT_JDATA,
552 static const u32 gfs2_to_iflags[32] = {
553 [gfs2fl_Sync] = IFLAG_SYNC,
554 [gfs2fl_Immutable] = IFLAG_IMMUTABLE,
555 [gfs2fl_AppendOnly] = IFLAG_APPEND,
556 [gfs2fl_NoAtime] = IFLAG_NOATIME,
557 [gfs2fl_ExHash] = IFLAG_INDEX,
558 [gfs2fl_Jdata] = IFLAG_JOURNAL_DATA,
559 [gfs2fl_Directio] = IFLAG_DIRECTIO,
560 [gfs2fl_InheritDirectio] = IFLAG_INHERITDIRECTIO,
561 [gfs2fl_InheritJdata] = IFLAG_INHERITJDATA,
564 static int gfs2_get_flags(struct inode *inode, u32 __user *ptr)
566 struct gfs2_inode *ip = inode->u.generic_ip;
567 struct gfs2_holder gh;
571 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
572 error = gfs2_glock_nq_m_atime(1, &gh);
576 iflags = iflags_cvt(gfs2_to_iflags, ip->i_di.di_flags);
577 if (put_user(iflags, ptr))
580 gfs2_glock_dq_m(1, &gh);
581 gfs2_holder_uninit(&gh);
585 /* Flags that can be set by user space */
586 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
588 GFS2_DIF_IMMUTABLE| \
589 GFS2_DIF_APPENDONLY| \
593 GFS2_DIF_INHERIT_DIRECTIO| \
594 GFS2_DIF_INHERIT_JDATA)
597 * gfs2_set_flags - set flags on an inode
599 * @flags: The flags to set
600 * @mask: Indicates which flags are valid
603 static int do_gfs2_set_flags(struct inode *inode, u32 reqflags, u32 mask)
605 struct gfs2_inode *ip = inode->u.generic_ip;
606 struct gfs2_sbd *sdp = ip->i_sbd;
607 struct buffer_head *bh;
608 struct gfs2_holder gh;
610 u32 new_flags, flags;
612 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
613 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
617 flags = ip->i_di.di_flags;
618 new_flags = (flags & ~mask) | (reqflags & mask);
619 if ((new_flags ^ flags) == 0)
623 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
626 if (S_ISDIR(inode->i_mode)) {
627 if ((new_flags ^ flags) & (GFS2_DIF_JDATA | GFS2_DIF_DIRECTIO))
629 } else if (S_ISREG(inode->i_mode)) {
630 if ((new_flags ^ flags) & (GFS2_DIF_INHERIT_DIRECTIO|
631 GFS2_DIF_INHERIT_JDATA))
637 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
639 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
641 error = gfs2_repermission(inode, MAY_WRITE, NULL);
645 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
648 error = gfs2_meta_inode_buffer(ip, &bh);
651 gfs2_trans_add_bh(ip->i_gl, bh, 1);
652 ip->i_di.di_flags = new_flags;
653 gfs2_dinode_out(&ip->i_di, bh->b_data);
658 gfs2_glock_dq_uninit(&gh);
662 static int gfs2_set_flags(struct inode *inode, u32 __user *ptr)
664 u32 iflags, gfsflags;
665 if (get_user(iflags, ptr))
667 gfsflags = iflags_cvt(iflags_to_gfs2, iflags);
668 return do_gfs2_set_flags(inode, gfsflags, ~0);
671 int gfs2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
676 return gfs2_get_flags(inode, (u32 __user *)arg);
678 return gfs2_set_flags(inode, (u32 __user *)arg);
686 * @file: The file to map
687 * @vma: The VMA which described the mapping
689 * Returns: 0 or error code
692 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
694 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
695 struct gfs2_holder i_gh;
698 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
699 error = gfs2_glock_nq_atime(&i_gh);
701 gfs2_holder_uninit(&i_gh);
705 /* This is VM_MAYWRITE instead of VM_WRITE because a call
706 to mprotect() can turn on VM_WRITE later. */
708 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
709 (VM_MAYSHARE | VM_MAYWRITE))
710 vma->vm_ops = &gfs2_vm_ops_sharewrite;
712 vma->vm_ops = &gfs2_vm_ops_private;
714 gfs2_glock_dq_uninit(&i_gh);
720 * gfs2_open - open a file
721 * @inode: the inode to open
722 * @file: the struct file for this opening
727 static int gfs2_open(struct inode *inode, struct file *file)
729 struct gfs2_inode *ip = inode->u.generic_ip;
730 struct gfs2_holder i_gh;
731 struct gfs2_file *fp;
734 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
738 mutex_init(&fp->f_fl_mutex);
740 gfs2_assert_warn(ip->i_sbd, !file->private_data);
741 file->private_data = fp;
743 if (S_ISREG(ip->i_di.di_mode)) {
744 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
749 if (!(file->f_flags & O_LARGEFILE) &&
750 ip->i_di.di_size > MAX_NON_LFS) {
755 /* Listen to the Direct I/O flag */
757 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
758 file->f_flags |= O_DIRECT;
760 gfs2_glock_dq_uninit(&i_gh);
766 gfs2_glock_dq_uninit(&i_gh);
769 file->private_data = NULL;
776 * gfs2_close - called to close a struct file
777 * @inode: the inode the struct file belongs to
778 * @file: the struct file being closed
783 static int gfs2_close(struct inode *inode, struct file *file)
785 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
786 struct gfs2_file *fp;
788 fp = file->private_data;
789 file->private_data = NULL;
791 if (gfs2_assert_warn(sdp, fp))
800 * gfs2_fsync - sync the dirty data for a file (across the cluster)
801 * @file: the file that points to the dentry (we ignore this)
802 * @dentry: the dentry that points to the inode to sync
807 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
809 struct gfs2_inode *ip = dentry->d_inode->u.generic_ip;
811 gfs2_log_flush_glock(ip->i_gl);
817 * gfs2_lock - acquire/release a posix lock on a file
818 * @file: the file pointer
819 * @cmd: either modify or retrieve lock state, possibly wait
820 * @fl: type and range of lock
825 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
827 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
828 struct gfs2_sbd *sdp = ip->i_sbd;
829 struct lm_lockname name =
830 { .ln_number = ip->i_num.no_addr,
831 .ln_type = LM_TYPE_PLOCK };
833 if (!(fl->fl_flags & FL_POSIX))
835 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
838 if (sdp->sd_args.ar_localflocks) {
840 struct file_lock tmp;
842 ret = posix_test_lock(file, fl, &tmp);
843 fl->fl_type = F_UNLCK;
845 memcpy(fl, &tmp, sizeof(struct file_lock));
848 return posix_lock_file_wait(file, fl);
853 return gfs2_lm_plock_get(sdp, &name, file, fl);
854 else if (fl->fl_type == F_UNLCK)
855 return gfs2_lm_punlock(sdp, &name, file, fl);
857 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
861 * gfs2_sendfile - Send bytes to a file or socket
862 * @in_file: The file to read from
863 * @out_file: The file to write to
864 * @count: The amount of data
865 * @offset: The beginning file offset
867 * Outputs: offset - updated according to number of bytes read
869 * Returns: The number of bytes sent, errno on failure
872 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
873 read_actor_t actor, void *target)
875 return generic_file_sendfile(in_file, offset, count, actor, target);
878 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
880 struct gfs2_file *fp = file->private_data;
881 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
882 struct gfs2_inode *ip = file->f_dentry->d_inode->u.generic_ip;
883 struct gfs2_glock *gl;
888 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
889 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
891 mutex_lock(&fp->f_fl_mutex);
895 if (fl_gh->gh_state == state)
898 flock_lock_file_wait(file,
899 &(struct file_lock){.fl_type = F_UNLCK});
900 gfs2_glock_dq_uninit(fl_gh);
902 error = gfs2_glock_get(ip->i_sbd,
903 ip->i_num.no_addr, &gfs2_flock_glops,
909 gfs2_holder_init(gl, state, flags, fl_gh);
912 error = gfs2_glock_nq(fl_gh);
914 gfs2_holder_uninit(fl_gh);
915 if (error == GLR_TRYFAILED)
918 error = flock_lock_file_wait(file, fl);
919 gfs2_assert_warn(ip->i_sbd, !error);
923 mutex_unlock(&fp->f_fl_mutex);
928 static void do_unflock(struct file *file, struct file_lock *fl)
930 struct gfs2_file *fp = file->private_data;
931 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
933 mutex_lock(&fp->f_fl_mutex);
934 flock_lock_file_wait(file, fl);
936 gfs2_glock_dq_uninit(fl_gh);
937 mutex_unlock(&fp->f_fl_mutex);
941 * gfs2_flock - acquire/release a flock lock on a file
942 * @file: the file pointer
943 * @cmd: either modify or retrieve lock state, possibly wait
944 * @fl: type and range of lock
949 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
951 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
952 struct gfs2_sbd *sdp = ip->i_sbd;
954 if (!(fl->fl_flags & FL_FLOCK))
956 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
959 if (sdp->sd_args.ar_localflocks)
960 return flock_lock_file_wait(file, fl);
962 if (fl->fl_type == F_UNLCK) {
963 do_unflock(file, fl);
966 return do_flock(file, cmd, fl);
969 struct file_operations gfs2_file_fops = {
970 .llseek = gfs2_llseek,
972 .readv = gfs2_file_readv,
973 .aio_read = gfs2_file_aio_read,
974 .write = generic_file_write,
975 .writev = generic_file_writev,
976 .aio_write = generic_file_aio_write,
980 .release = gfs2_close,
983 .sendfile = gfs2_sendfile,
985 .splice_read = generic_file_splice_read,
986 .splice_write = generic_file_splice_write,
989 struct file_operations gfs2_dir_fops = {
990 .readdir = gfs2_readdir,
993 .release = gfs2_close,