GFS2: File and inode operations
PATCH 5/16 GFS2: File and inode operations
[PATCH 05/16] GFS2: File and inode operations
File operations, superblock operations and inode operations code for GFS2.
This also includes a new header file, iflags.h which is designed to abstract the (originally ext2 only, but now used by many different fs) get/set flags ioctl by having one central place in which to register filesystem flags. Given favourable reviews, I'll submit some patches to update the other fileststems to define their flags in terms of those in iflags.h. Note that this doesn't change the interface of the other filesystems since the values of the flags are identical to those previously defined.
To the best of my knowledge, GFS2 is the only filesystem which requires the addition of flags above and beyond those defined by ext2/3 so if there are others and we've clashed with them, please let me know.
fs/gfs2/ops_file.c | 812 ++++++++++++++++++++++++++++++++++ fs/gfs2/ops_file.h | 20 fs/gfs2/ops_fstype.c | 980 +++++++++++++++++++++++++++++++++++++++++ fs/gfs2/ops_fstype.h | 16 fs/gfs2/ops_inode.c | 1165 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/gfs2/ops_inode.h | 18 include/linux/iflags.h | 102 ++++ 7 files changed, 3113 insertions(+)
fs/gfs2/ops_file.c new file +812 -0
@@ -0,0 +1,812 @@ +/* +* Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. +* Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. +* +* This copyrighted material is made available to anyone wishing to use, +* modify, copy, or redistribute it subject to the terms and conditions +* of the GNU General Public License v.2. +*/ + +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/completion.h> +#include <linux/buffer_head.h> +#include <linux/pagemap.h> +#include <linux/uio.h> +#include <linux/blkdev.h> +#include <linux/mm.h> +#include <linux/smp_lock.h> +#include <linux/fs.h> +#include <linux/gfs2_ondisk.h> +#include <linux/ext2_fs.h> +#include <linux/crc32.h> +#include <linux/iflags.h> +#include <asm/uaccess.h> + +#include "gfs2.h" +#include "lm_interface.h" +#include "incore.h" +#include "bmap.h" +#include "dir.h" +#include "glock.h" +#include "glops.h" +#include "inode.h" +#include "lm.h" +#include "log.h" +#include "meta_io.h" +#include "ops_file.h" +#include "ops_vm.h" +#include "quota.h" +#include "rgrp.h" +#include "trans.h" +#include "util.h" +#include "eaops.h" + +/* "bad" is for NFS support */ +struct filldir_bad_entry { +char *fbe_name; +unsigned int fbe_length; +uint64_t fbe_offset; +struct gfs2_inum fbe_inum; +unsigned int fbe_type; +}; + +struct filldir_bad { +struct gfs2_sbd *fdb_sbd; + +struct filldir_bad_entry *fdb_entry; +unsigned int fdb_entry_num; +unsigned int fdb_entry_off; + +char *fdb_name; +unsigned int fdb_name_size; +unsigned int fdb_name_off; +}; + +/* For regular, non-NFS */ +struct filldir_reg { +struct gfs2_sbd *fdr_sbd; +int fdr_prefetch; + +filldir_t fdr_filldir; +void *fdr_opaque; +}; + +/* +* Most fields left uninitialised to catch anybody who tries to +* use them. f_flags set to prevent file_accessed() from touching +* any other part of this. Its use is purely as a flag so that we +* know (in readpage()) whether or not do to locking. +*/ +struct file gfs2_internal_file_sentinal = { + .f_flags =O_NOATIME |O_RDONLY , +}; + +static int gfs2_read_actor (read_descriptor_t *desc,struct page *page, +unsigned long offset,unsigned long size) +{ +char *kaddr; +unsigned long count = desc->count; + +if (size > count) + size = count; + + kaddr =kmap (page); +memcpy (desc->arg.buf, kaddr + offset, size); +kunmap (page); + + desc->count = count - size; + desc->written += size; + desc->arg.buf += size; +return size; +} + +int gfs2_internal_read (struct gfs2_inode *ip,struct file_ra_state *ra_state, +char *buf,loff_t *pos,unsigned size) +{ +struct inode *inode = &ip->i_inode; +read_descriptor_t desc; + desc.written =0 ; + desc.arg.buf = buf; + desc.count = size; + desc.error =0 ; +do_generic_mapping_read (inode->i_mapping, ra_state, + &gfs2_internal_file_sentinal, pos, &desc, + gfs2_read_actor); +return desc.written ? desc.written : desc.error; +} + +/** +* gfs2_llseek - seek to a location in a file +* @file: the file +* @offset: the offset +* @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END) +* +* SEEK_END requires the glock for the file because it references the +* file's size. +* +* Returns: The new offset, or errno +*/ + +static loff_t gfs2_llseek (struct file *file,loff_t offset,int origin) +{ +struct gfs2_inode *ip =GFS2_I (file->f_mapping->host); +struct gfs2_holder i_gh; +loff_t error; + +if (origin ==2 ) { + error =gfs2_glock_nq_init (ip->i_gl,LM_ST_SHARED ,LM_FLAG_ANY , + &i_gh); +if (!error) { + error =remote_llseek (file, offset, origin); +gfs2_glock_dq_uninit (&i_gh); + } + }else + error =remote_llseek (file, offset, origin); + +return error; +} + +/** +* filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read() +* @opaque: opaque data used by the function +* @name: the name of the directory entry +* @length: the length of the name +* @offset: the entry's offset in the directory +* @inum: the inode number the entry points to +* @type: the type of inode the entry points to +* +* Returns: 0 on success, 1 if buffer full +*/ + +static int filldir_reg_func (void *opaque,const char *name,unsigned int length, +uint64_t offset,struct gfs2_inum *inum, +unsigned int type) +{ +struct filldir_reg *fdr = (struct filldir_reg *)opaque; +struct gfs2_sbd *sdp = fdr->fdr_sbd; +int error; + + error = fdr->fdr_filldir (fdr->fdr_opaque, name, length, offset, + inum->no_addr, type); +if (error) +return 1 ; + +if (fdr->fdr_prefetch && !(length ==1 && *name =='.' )) { +gfs2_glock_prefetch_num (sdp, + inum->no_addr, &gfs2_inode_glops, +LM_ST_SHARED ,LM_FLAG_TRY |LM_FLAG_ANY ); +gfs2_glock_prefetch_num (sdp, + inum->no_addr, &gfs2_iopen_glops, +LM_ST_SHARED ,LM_FLAG_TRY ); + } + +return 0 ; +} + +/** +* readdir_reg - Read directory entries from a directory +* @file: The directory to read from +* @dirent: Buffer for dirents +* @filldir: Function used to do the copying +* +* Returns: errno +*/ + +static int readdir_reg (struct file *file,void *dirent,filldir_t filldir) +{ +struct inode *dir = file->f_mapping->host; +struct gfs2_inode *dip =GFS2_I (dir); +struct filldir_reg fdr; +struct gfs2_holder d_gh; +uint64_t offset = file->f_pos; +int error; + + fdr.fdr_sbd =GFS2_SB (dir); + fdr.fdr_prefetch =1 ; + fdr.fdr_filldir = filldir; + fdr.fdr_opaque = dirent; + +gfs2_holder_init (dip->i_gl,LM_ST_SHARED ,GL_ATIME , &d_gh); + error =gfs2_glock_nq_atime (&d_gh); +if (error) { +gfs2_holder_uninit (&d_gh); +return error; + } + + error =gfs2_dir_read (dir, &offset, &fdr, filldir_reg_func); + +gfs2_glock_dq_uninit (&d_gh); + + file->f_pos = offset; + +return error; +} + +/** +* filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read() +* @opaque: opaque data used by the function +* @name: the name of the directory entry +* @length: the length of the name +* @offset: the entry's offset in the directory +* @inum: the inode number the entry points to +* @type: the type of inode the entry points to +* +* For supporting NFS. +* +* Returns: 0 on success, 1 if buffer full +*/ + +static int filldir_bad_func (void *opaque,const char *name,unsigned int length, +uint64_t offset,struct gfs2_inum *inum, +unsigned int type) +{ +struct filldir_bad *fdb = (struct filldir_bad *)opaque; +struct gfs2_sbd *sdp = fdb->fdb_sbd; +struct filldir_bad_entry *fbe; + +if (fdb->fdb_entry_off == fdb->fdb_entry_num || + fdb->fdb_name_off + length > fdb->fdb_name_size) +return 1 ; + + fbe = &fdb->fdb_entry[fdb->fdb_entry_off]; + fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off; +memcpy (fbe->fbe_name, name, length); + fbe->fbe_length = length; + fbe->fbe_offset = offset; + fbe->fbe_inum = *inum; + fbe->fbe_type = type; + + fdb->fdb_entry_off++; + fdb->fdb_name_off += length; + +if (!(length ==1 && *name =='.' )) { +gfs2_glock_prefetch_num (sdp, + inum->no_addr, &gfs2_inode_glops, +LM_ST_SHARED ,LM_FLAG_TRY |LM_FLAG_ANY ); +gfs2_glock_prefetch_num (sdp, + inum->no_addr, &gfs2_iopen_glops, +LM_ST_SHARED ,LM_FLAG_TRY ); + } + +return 0 ; +} + +/** +* readdir_bad - Read directory entries from a directory +* @file: The directory to read from +* @dirent: Buffer for dirents +* @filldir: Function used to do the copying +* +* For supporting NFS. +* +* Returns: errno +*/ + +static int readdir_bad (struct file *file,void *dirent,filldir_t filldir) +{ +struct inode *dir = file->f_mapping->host; +struct gfs2_inode *dip =GFS2_I (dir); +struct gfs2_sbd *sdp =GFS2_SB (dir); +struct filldir_reg fdr; +unsigned int entries, size; +struct filldir_bad *fdb; +struct gfs2_holder d_gh; +uint64_t offset = file->f_pos; +unsigned int x; +struct filldir_bad_entry *fbe; +int error; + + entries =gfs2_tune_get (sdp, gt_entries_per_readdir); + size =sizeof (struct filldir_bad ) + + entries * (sizeof (struct filldir_bad_entry ) +GFS2_FAST_NAME_SIZE ); + + fdb =kzalloc (size,GFP_KERNEL ); +if (!fdb) +return -ENOMEM ; + + fdb->fdb_sbd = sdp; + fdb->fdb_entry = (struct filldir_bad_entry *)(fdb +1 ); + fdb->fdb_entry_num = entries; + fdb->fdb_name = ((char *)fdb) +sizeof (struct filldir_bad ) + + entries *sizeof (struct filldir_bad_entry ); + fdb->fdb_name_size = entries *GFS2_FAST_NAME_SIZE ; + +gfs2_holder_init (dip->i_gl,LM_ST_SHARED ,GL_ATIME , &d_gh); + error =gfs2_glock_nq_atime (&d_gh); +if (error) { +gfs2_holder_uninit (&d_gh); + goto out; + } + + error =gfs2_dir_read (dir, &offset, fdb, filldir_bad_func); + +gfs2_glock_dq_uninit (&d_gh); + + fdr.fdr_sbd = sdp; + fdr.fdr_prefetch =0 ; + fdr.fdr_filldir = filldir; + fdr.fdr_opaque = dirent; + +for (x =0 ; x < fdb->fdb_entry_off; x++) { + fbe = &fdb->fdb_entry[x]; + + error =filldir_reg_func (&fdr, + fbe->fbe_name, fbe->fbe_length, + fbe->fbe_offset, + &fbe->fbe_inum, fbe->fbe_type); +if (error) { + file->f_pos = fbe->fbe_offset; + error =0 ; + goto out; + } + } + + file->f_pos = offset; + + out: +kfree (fdb); + +return error; +} + +/** +* gfs2_readdir - Read directory entries from a directory +* @file: The directory to read from +* @dirent: Buffer for dirents +* @filldir: Function used to do the copying +* +* Returns: errno +*/ + +static int gfs2_readdir (struct file *file,void *dirent,filldir_t filldir) +{ +int error; + +if (strcmp (current->comm,"nfsd" ) !=0 ) + error =readdir_reg (file, dirent, filldir); +else + error =readdir_bad (file, dirent, filldir); + +return error; +} + +static const u32 iflags_to_gfs2[32 ] = { + [iflag_Sync] =GFS2_DIF_SYNC , + [iflag_Immutable] =GFS2_DIF_IMMUTABLE , + [iflag_Append] =GFS2_DIF_APPENDONLY , + [iflag_NoAtime] =GFS2_DIF_NOATIME , + [iflag_Index] =GFS2_DIF_EXHASH , + [iflag_JournalData] =GFS2_DIF_JDATA , + [iflag_DirectIO] =GFS2_DIF_DIRECTIO , +}; + +static const u32 gfs2_to_iflags[32 ] = { + [gfs2fl_Sync] =IFLAG_SYNC , + [gfs2fl_Immutable] =IFLAG_IMMUTABLE , + [gfs2fl_AppendOnly] =IFLAG_APPEND , + [gfs2fl_NoAtime] =IFLAG_NOATIME , + [gfs2fl_ExHash] =IFLAG_INDEX , + [gfs2fl_Jdata] =IFLAG_JOURNAL_DATA , + [gfs2fl_Directio] =IFLAG_DIRECTIO , + [gfs2fl_InheritDirectio] =IFLAG_DIRECTIO , + [gfs2fl_InheritJdata] =IFLAG_JOURNAL_DATA , +}; + +static int gfs2_get_flags (struct file *filp,u32 __user *ptr) +{ +struct inode *inode = filp->f_dentry->d_inode; +struct gfs2_inode *ip =GFS2_I (inode); +struct gfs2_holder gh; +int error; +u32 iflags; + +gfs2_holder_init (ip->i_gl,LM_ST_SHARED ,GL_ATIME , &gh); + error =gfs2_glock_nq_m_atime (1 , &gh); +if (error) +return error; + + iflags =iflags_cvt (gfs2_to_iflags, ip->i_di.di_flags); +if (put_user (iflags, ptr)) + error = -EFAULT ; + +gfs2_glock_dq_m (1 , &gh); +gfs2_holder_uninit (&gh); +return error; +} + +/* Flags that can be set by user space */ +#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \ + GFS2_DIF_DIRECTIO| \ + GFS2_DIF_IMMUTABLE| \ + GFS2_DIF_APPENDONLY| \ + GFS2_DIF_NOATIME| \ + GFS2_DIF_SYNC| \ + GFS2_DIF_SYSTEM| \ + GFS2_DIF_INHERIT_DIRECTIO| \ + GFS2_DIF_INHERIT_JDATA) + +/** +* gfs2_set_flags - set flags on an inode +* @inode: The inode +* @flags: The flags to set +* @mask: Indicates which flags are valid +* +*/ +static int do_gfs2_set_flags (struct file *filp,u32 reqflags,u32 mask) +{ +struct inode *inode = filp->f_dentry->d_inode; +struct gfs2_inode *ip =GFS2_I (inode); +struct gfs2_sbd *sdp =GFS2_SB (inode); +struct buffer_head *bh; +struct gfs2_holder gh; +int error; +u32 new_flags, flags; + + error =gfs2_glock_nq_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &gh); +if (error) +return error; + + flags = ip->i_di.di_flags; + new_flags = (flags & ~mask) | (reqflags & mask); +if ((new_flags ^ flags) ==0 ) + goto out; + +if (S_ISDIR (inode->i_mode)) { +if ((new_flags ^ flags) &GFS2_DIF_JDATA ) + new_flags ^= (GFS2_DIF_JDATA |GFS2_DIF_INHERIT_JDATA ); +if ((new_flags ^ flags) &GFS2_DIF_DIRECTIO ) + new_flags ^= (GFS2_DIF_DIRECTIO |GFS2_DIF_INHERIT_DIRECTIO ); + } + + error = -EINVAL ; +if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET ) + goto out; + + error = -EPERM ; +if (IS_IMMUTABLE (inode) && (new_flags &GFS2_DIF_IMMUTABLE )) + goto out; +if (IS_APPEND (inode) && (new_flags &GFS2_DIF_APPENDONLY )) + goto out; +if (((new_flags ^ flags) &GFS2_DIF_IMMUTABLE ) && + !capable (CAP_LINUX_IMMUTABLE )) + goto out; +if (!IS_IMMUTABLE (inode)) { + error =permission (inode,MAY_WRITE ,NULL ); +if (error) + goto out; + } + + error =gfs2_trans_begin (sdp,RES_DINODE ,0 ); +if (error) + goto out; + error =gfs2_meta_inode_buffer (ip, &bh); +if (error) + goto out_trans_end; +gfs2_trans_add_bh (ip->i_gl, bh,1 ); + ip->i_di.di_flags = new_flags; +gfs2_dinode_out (&ip->i_di, bh->b_data); +brelse (bh); +out_trans_end: +gfs2_trans_end (sdp); +out: +gfs2_glock_dq_uninit (&gh); +return error; +} + +static int gfs2_set_flags (struct file *filp,u32 __user *ptr) +{ +u32 iflags, gfsflags; +if (get_user (iflags, ptr)) +return -EFAULT ; + gfsflags =iflags_cvt (iflags_to_gfs2, iflags); +return do_gfs2_set_flags (filp, gfsflags, ~0 ); +} + +static long gfs2_ioctl (struct file *filp,unsigned int cmd,unsigned long arg) +{ +switch (cmd) { +case IFLAGS_GET_IOC : +return gfs2_get_flags (filp, (u32 __user *)arg); +case IFLAGS_SET_IOC : +return gfs2_set_flags (filp, (u32 __user *)arg); + } +return -ENOTTY ; +} + + +/** +* gfs2_mmap - +* @file: The file to map +* @vma: The VMA which described the mapping +* +* Returns: 0 or error code +*/ + +static int gfs2_mmap (struct file *file,struct vm_area_struct *vma) +{ +struct gfs2_inode *ip =GFS2_I (file->f_mapping->host); +struct gfs2_holder i_gh; +int error; + +gfs2_holder_init (ip->i_gl,LM_ST_SHARED ,GL_ATIME , &i_gh); + error =gfs2_glock_nq_atime (&i_gh); +if (error) { +gfs2_holder_uninit (&i_gh); +return error; + } + +/* This is VM_MAYWRITE instead of VM_WRITE because a call +to mprotect() can turn on VM_WRITE later. */ + +if ((vma->vm_flags & (VM_MAYSHARE |VM_MAYWRITE )) == + (VM_MAYSHARE |VM_MAYWRITE )) + vma->vm_ops = &gfs2_vm_ops_sharewrite; +else + vma->vm_ops = &gfs2_vm_ops_private; + +gfs2_glock_dq_uninit (&i_gh); + +return error; +} + +/** +* gfs2_open - open a file +* @inode: the inode to open +* @file: the struct file for this opening +* +* Returns: errno +*/ + +static int gfs2_open (struct inode *inode,struct file *file) +{ +struct gfs2_inode *ip =GFS2_I (inode); +struct gfs2_holder i_gh; +struct gfs2_file *fp; +int error; + + fp =kzalloc (sizeof (struct gfs2_file ),GFP_KERNEL ); +if (!fp) +return -ENOMEM ; + +mutex_init (&fp->f_fl_mutex); + +gfs2_assert_warn (GFS2_SB (inode), !file->private_data); + file->private_data = fp; + +if (S_ISREG (ip->i_di.di_mode)) { + error =gfs2_glock_nq_init (ip->i_gl,LM_ST_SHARED ,LM_FLAG_ANY , + &i_gh); +if (error) + goto fail; + +if (!(file->f_flags &O_LARGEFILE ) && + ip->i_di.di_size >MAX_NON_LFS ) { + error = -EFBIG ; + goto fail_gunlock; + } + +/* Listen to the Direct I/O flag */ + +if (ip->i_di.di_flags &GFS2_DIF_DIRECTIO ) + file->f_flags |=O_DIRECT ; + +gfs2_glock_dq_uninit (&i_gh); + } + +return 0 ; + +fail_gunlock: +gfs2_glock_dq_uninit (&i_gh); +fail: + file->private_data =NULL ; +kfree (fp); +return error; +} + +/** +* gfs2_close - called to close a struct file +* @inode: the inode the struct file belongs to +* @file: the struct file being closed +* +* Returns: errno +*/ + +static int gfs2_close (struct inode *inode,struct file *file) +{ +struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; +struct gfs2_file *fp; + + fp = file->private_data; + file->private_data =NULL ; + +if (gfs2_assert_warn (sdp, fp)) +return -EIO ; + +kfree (fp); + +return 0 ; +} + +/** +* gfs2_fsync - sync the dirty data for a file (across the cluster) +* @file: the file that points to the dentry (we ignore this) +* @dentry: the dentry that points to the inode to sync +* +* Returns: errno +*/ + +static int gfs2_fsync (struct file *file,struct dentry *dentry,int datasync) +{ +struct gfs2_inode *ip =GFS2_I (dentry->d_inode); + +gfs2_log_flush (ip->i_gl->gl_sbd, ip->i_gl); + +return 0 ; +} + +/** +* gfs2_lock - acquire/release a posix lock on a file +* @file: the file pointer +* @cmd: either modify or retrieve lock state, possibly wait +* @fl: type and range of lock +* +* Returns: errno +*/ + +static int gfs2_lock (struct file *file,int cmd,struct file_lock *fl) +{ +struct gfs2_inode *ip =GFS2_I (file->f_mapping->host); +struct gfs2_sbd *sdp =GFS2_SB (file->f_mapping->host); +struct lm_lockname name = + { .ln_number = ip->i_num.no_addr, + .ln_type =LM_TYPE_PLOCK }; + +if (!(fl->fl_flags &FL_POSIX )) +return -ENOLCK ; +if ((ip->i_di.di_mode & (S_ISGID |S_IXGRP )) ==S_ISGID ) +return -ENOLCK ; + +if (sdp->sd_args.ar_localflocks) { +if (IS_GETLK (cmd)) { +struct file_lock tmp; +int ret; + ret =posix_test_lock (file, fl, &tmp); + fl->fl_type =F_UNLCK ; +if (ret) +memcpy (fl, &tmp,sizeof (struct file_lock )); +return 0 ; + }else { +return posix_lock_file_wait (file, fl); + } + } + +if (IS_GETLK (cmd)) +return gfs2_lm_plock_get (sdp, &name, file, fl); +else if (fl->fl_type ==F_UNLCK ) +return gfs2_lm_punlock (sdp, &name, file, fl); +else +return gfs2_lm_plock (sdp, &name, file, cmd, fl); +} + +static int do_flock (struct file *file,int cmd,struct file_lock *fl) +{ +struct gfs2_file *fp = file->private_data; +struct gfs2_holder *fl_gh = &fp->f_fl_gh; +struct gfs2_inode *ip =GFS2_I (file->f_dentry->d_inode); +struct gfs2_glock *gl; +unsigned int state; +int flags; +int error =0 ; + + state = (fl->fl_type ==F_WRLCK ) ?LM_ST_EXCLUSIVE :LM_ST_SHARED ; + flags = ((IS_SETLKW (cmd)) ?0 :LM_FLAG_TRY ) |GL_EXACT |GL_NOCACHE ; + +mutex_lock (&fp->f_fl_mutex); + + gl = fl_gh->gh_gl; +if (gl) { +if (fl_gh->gh_state == state) + goto out; +gfs2_glock_hold (gl); +flock_lock_file_wait (file, + &(struct file_lock ){.fl_type =F_UNLCK }); +gfs2_glock_dq_uninit (fl_gh); + }else { + error =gfs2_glock_get (GFS2_SB (&ip->i_inode), + ip->i_num.no_addr, &gfs2_flock_glops, +CREATE , &gl); +if (error) + goto out; + } + +gfs2_holder_init (gl, state, flags, fl_gh); +gfs2_glock_put (gl); + + error =gfs2_glock_nq (fl_gh); +if (error) { +gfs2_holder_uninit (fl_gh); +if (error ==GLR_TRYFAILED ) + error = -EAGAIN ; + }else { + error =flock_lock_file_wait (file, fl); +gfs2_assert_warn (GFS2_SB (&ip->i_inode), !error); + } + +out: +mutex_unlock (&fp->f_fl_mutex); +return error; +} + +static void do_unflock (struct file *file,struct file_lock *fl) +{ +struct gfs2_file *fp = file->private_data; +struct gfs2_holder *fl_gh = &fp->f_fl_gh; + +mutex_lock (&fp->f_fl_mutex); +flock_lock_file_wait (file, fl); +if (fl_gh->gh_gl) +gfs2_glock_dq_uninit (fl_gh); +mutex_unlock (&fp->f_fl_mutex); +} + +/** +* gfs2_flock - acquire/release a flock lock on a file +* @file: the file pointer +* @cmd: either modify or retrieve lock state, possibly wait +* @fl: type and range of lock +* +* Returns: errno +*/ + +static int gfs2_flock (struct file *file,int cmd,struct file_lock *fl) +{ +struct gfs2_inode *ip =GFS2_I (file->f_mapping->host); +struct gfs2_sbd *sdp =GFS2_SB (file->f_mapping->host); + +if (!(fl->fl_flags &FL_FLOCK )) +return -ENOLCK ; +if ((ip->i_di.di_mode & (S_ISGID |S_IXGRP )) ==S_ISGID ) +return -ENOLCK ; + +if (sdp->sd_args.ar_localflocks) +return flock_lock_file_wait (file, fl); + +if (fl->fl_type ==F_UNLCK ) { +do_unflock (file, fl); +return 0 ; + }else +return do_flock (file, cmd, fl); +} + +const struct file_operations gfs2_file_fops = { + .llseek = gfs2_llseek, + .read = generic_file_read, + .readv = generic_file_readv, + .aio_read = generic_file_aio_read, + .write = generic_file_write, + .writev = generic_file_writev, + .aio_write = generic_file_aio_write, + .unlocked_ioctl = gfs2_ioctl, + .mmap = gfs2_mmap, + .open = gfs2_open, + .release = gfs2_close, + .fsync = gfs2_fsync, + .lock = gfs2_lock, + .sendfile = generic_file_sendfile, + .flock = gfs2_flock, + .splice_read = generic_file_splice_read, + .splice_write = generic_file_splice_write, +}; + +const struct file_operations gfs2_dir_fops = { + .readdir = gfs2_readdir, + .unlocked_ioctl = gfs2_ioctl, + .open = gfs2_open, + .release = gfs2_close, + .fsync = gfs2_fsync, + .lock = gfs2_lock, + .flock = gfs2_flock, +}; +
include/linux/iflags.h new file +102 -0
102 lines not on this page · raw message
fs/gfs2/ops_file.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. +* Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. +* +* This copyrighted material is made available to anyone wishing to use, +* modify, copy, or redistribute it subject to the terms and conditions +* of the GNU General Public License v.2. +*/ + +#ifndef __OPS_FILE_DOT_H__ +#define __OPS_FILE_DOT_H__ +extern struct file gfs2_internal_file_sentinal; +extern int gfs2_internal_read (struct gfs2_inode *ip, +struct file_ra_state *ra_state, +char *buf,loff_t *pos,unsigned size); + +extern const struct file_operations gfs2_file_fops; +extern const struct file_operations gfs2_dir_fops; + +#endif /* __OPS_FILE_DOT_H__ */
fs/gfs2/ops_fstype.c new file +980 -0
980 lines not on this page · raw message
fs/gfs2/ops_fstype.h new file +16 -0
@@ -0,0 +1,16 @@ +/* +* Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. +* Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. +* +* This copyrighted material is made available to anyone wishing to use, +* modify, copy, or redistribute it subject to the terms and conditions +* of the GNU General Public License v.2. +*/ + +#ifndef __OPS_FSTYPE_DOT_H__ +#define __OPS_FSTYPE_DOT_H__ + +extern struct file_system_type gfs2_fs_type; +extern struct file_system_type gfs2meta_fs_type; + +#endif /* __OPS_FSTYPE_DOT_H__ */
fs/gfs2/ops_inode.c new file +1165 -0
1,070 lines not on this page · raw message
fs/gfs2/ops_inode.h new file +18 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
Quoted 13 lines
fs/gfs2/ops_file.c new file +12
-0+struct file gfs2_internal_file_sentinal = { + .f_flags =O_NOATIME |O_RDONLY , +}; + +static int gfs2_read_actor (read_descriptor_t *desc,struct page *page, +unsigned long offset,unsigned long size) +{ +char *kaddr; +unsigned long count = desc->count; + +if (size > count) + size = count;
fs/gfs2/ops_file.c line 103
Quoted 10 lines
fs/gfs2/ops_file.c new file +10
-0+ + kaddr =kmap (page); +memcpy (desc->arg.buf, kaddr + offset, size); +kunmap (page); + + desc->count = count - size; + desc->written += size; + desc->arg.buf += size; +return size; +}
Indent mismatch.
fs/gfs2/ops_file.c line 367
Quoted 5 lines
fs/gfs2/ops_file.c new file +5
-0+static int gfs2_readdir (struct file *file,void *dirent,filldir_t filldir) +{ +int error; + +if (strcmp (current->comm,"nfsd" ) !=0 )
Is not there a better way to do this? Note that there is also a "nfsd4" process running. Do you really want to do a 'costly'
strcmp(current->comm, "nfsd") != 0 && strcmp(current->comm, "nfsd4") != 0
every time someone does a readdir? What if I run a userspace nfs daemon? What if that userspace daemon is called differently?
jengelh@linux01:7 08:28:14 ~ > ps -e | grep nfs
5580 ? 00:00:08 rpc.nfsd
jengelh@linux01:7 08:28:30 ~ > rpm -qf /usr/sbin/rpc.nfsd nfs-server-2.2beta51-209.2 (a userspace nfsd)
fs/gfs2/ops_file.c line 704
Quoted 12 lines
fs/gfs2/ops_file.c new file +12
-0+static int do_flock (struct file *file,int cmd,struct file_lock *fl) +{ +struct gfs2_file *fp = file->private_data; +struct gfs2_holder *fl_gh = &fp->f_fl_gh; +struct gfs2_inode *ip =GFS2_I (file->f_dentry->d_inode); +struct gfs2_glock *gl; +unsigned int state; +int flags; +int error =0 ; + + state = (fl->fl_type ==F_WRLCK ) ?LM_ST_EXCLUSIVE :LM_ST_SHARED ; + flags = ((IS_SETLKW (cmd)) ?0 :LM_FLAG_TRY ) |GL_EXACT |GL_NOCACHE ;
^ ^
fs/gfs2/ops_file.c line 811
Quoted 9 lines
fs/gfs2/ops_file.c new file +9
-0+const struct file_operations gfs2_dir_fops = { + .readdir = gfs2_readdir, + .unlocked_ioctl = gfs2_ioctl, + .open = gfs2_open, + .release = gfs2_close, + .fsync = gfs2_fsync, + .lock = gfs2_lock, + .flock = gfs2_flock, +};
Maybe have the structs' members lined up.
include/linux/iflags.h line 28
Quoted 6 lines
include/linux/iflags.h new file +6
-0+/* +* These values are provided for use as indices of an array +* for use with the iflags_cvt function below +*/ +enum { + iflag_SecureRm =0 ,/* Secure deletion */
If these values are not used on-disk or on-network, the =N parts could go away.
fs/gfs2/ops_fstype.c line 806
Quoted 2 lines
+
fail_sys:fs/gfs2/ops_fstype.c new file +1
-0+gfs2_sys_fs_del (sdp);
Some labels in your code have a leading space, others do not. Make it consistent.
fs/gfs2/ops_fstype.c line 824
Quoted 10 lines
fs/gfs2/ops_fstype.c new file +10
-0+static int gfs2_get_sb (struct file_system_type *fs_type,int flags, +const char *dev_name,void *data,struct vfsmount *mnt) +{ +struct super_block *sb; +struct gfs2_sbd *sdp; +int error =get_sb_bdev (fs_type, flags, dev_name, data, fill_super, mnt); +if (error) + goto out; + sb = mnt->mnt_sb; + sdp = (struct gfs2_sbd *)sb->s_fs_info;
Nocast.
fs/gfs2/ops_fstype.c line 865
Quoted 4 lines
fs/gfs2/ops_fstype.c new file +4
-0+static int test_bdev_super (struct super_block *s,void *data) +{ +return (void *)s->s_bdev == data; +}
Nocast.
fs/gfs2/ops_fstype.c line 917
Quoted 4 lines
fs/gfs2/ops_fstype.c new file +4
-0+static int gfs2_get_sb_meta (struct file_system_type *fs_type,int flags, +const char *dev_name,void *data,struct vfsmount *mnt) +{ + sdp = (struct gfs2_sbd *) sb->s_fs_info;
Nocast.
fs/gfs2/ops_inode.c line 429
Quoted 1 line
fs/gfs2/ops_inode.c new file +1
-0+ error =gfs2_change_nlink (dip,+1 );
Interesting annotation of direction (+1) :-)
fs/gfs2/ops_inode.c line 602
Quoted 1 line
fs/gfs2/ops_inode.c new file +1
-0+/* Make sure we aren't trying to move a dirctory into it's subdir */
Should not the VFS handle this? (Or at least, the VFS should have some generic_*_check_move_subdir() or so, in case a fs does not have its own.)
Quoted 3 lines
+/**
+ *gfs2_getattr- Read out an inode's attributes
+ * @mnt: ?
Might complete the doc.
Review
Quoted 1 line
(quoted diff) +1
-0+ struct gfs2_ea_request er;
Just for info, EA stands for what?
Jan Engelhardt
Signature · 2 lines
-- VGER BF report: U 0.5
Hi,
Quoted 16 lines · Jan Engelhardt
Quoted 16 lines
fs/gfs2/ops_file.c new file +16
-0+struct file gfs2_internal_file_sentinal = { + .f_flags =O_NOATIME |O_RDONLY , +}; + +static int gfs2_read_actor (read_descriptor_t *desc,struct page *page, +unsigned long offset,unsigned long size) +{ +char *kaddr; +unsigned long count = desc->count; + +if (size > count) + size = count; + + kaddr =kmap (page); +memcpy (desc->arg.buf, kaddr + offset, size); +kunmap (page);
Quoted 9 lines
fs/gfs2/ops_file.c line 103
Quoted 6 lines
fs/gfs2/ops_file.c new file +6
-0+ + desc->count = count - size; + desc->written += size; + desc->arg.buf += size; +return size; +}Indent mismatch.
Now fixed.
Quoted 11 lines
fs/gfs2/ops_file.c line 367
Quoted 5 lines
fs/gfs2/ops_file.c new file +5
-0+static int gfs2_readdir (struct file *file,void *dirent,filldir_t filldir) +{ +int error; + +if (strcmp (current->comm,"nfsd" ) !=0 )Is not there a better way to do this? Note that there is also a "nfsd4" process running. Do you really want to do a 'costly'
strcmp(current->comm, "nfsd") != 0 && strcmp(current->comm, "nfsd4") != 0
Quoted 8 lines
every time someone does a readdir? What if I run a userspace nfs daemon? What if that userspace daemon is called differently?
jengelh@linux01:7 08:28:14 ~ > ps -e | grep nfs
5580 ? 00:00:08 rpc.nfsd
jengelh@linux01:7 08:28:30 ~ > rpm -qf/usr/sbin/rpc.nfsdnfs-server-2.2beta51-209.2 (a userspace nfsd)
Ah, now this is a very tricky one to solve. Its on my todo list to look at this area again. You are right that the test is bogus in that it should only respond to the in kernel NFS server and the reason for its existence is due to locking issues with the way that NFS calls through the VFS layer.
It means the in the NFS case we have to constantly check that the file position within the directory hasn't changed. This is, of course rather slow and hence has been special cased.
The test you've mentioned here is (aside from slow) also bogus for the reason that any userspace NFS daemon will also get caught by this code despite the fact that it could perfectly well use the "normal" readdir function which is much faster.
It may be that the correct place to make the change is in NFS rather than GFS2, but I haven't had a chance to look at this in detail yet.
Red Hat bugzilla #201015 is tracking this issue - I just realised that it wasn't publicly viewable, so I've fixed that.
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+static int do_flock(struct file *file, int cmd, struct file_lock *fl) +{
[function lines snipped]
Quoted 4 lines
fs/gfs2/ops_file.c line 704
Quoted 2 lines
fs/gfs2/ops_file.c new file +2
-0+ state = (fl->fl_type ==F_WRLCK ) ?LM_ST_EXCLUSIVE :LM_ST_SHARED ; + flags = ((IS_SETLKW (cmd)) ?0 :LM_FLAG_TRY ) |GL_EXACT |GL_NOCACHE ;^ ^
ok.
Quoted 12 lines
fs/gfs2/ops_file.c line 811
Quoted 9 lines
fs/gfs2/ops_file.c new file +9
-0+const struct file_operations gfs2_dir_fops = { + .readdir = gfs2_readdir, + .unlocked_ioctl = gfs2_ioctl, + .open = gfs2_open, + .release = gfs2_close, + .fsync = gfs2_fsync, + .lock = gfs2_lock, + .flock = gfs2_flock, +};Maybe have the structs' members lined up.
now done.
Quoted 9 lines
include/linux/iflags.h line 28
Quoted 6 lines
include/linux/iflags.h new file +6
-0+/* +* These values are provided for use as indices of an array +* for use with the iflags_cvt function below +*/ +enum { + iflag_SecureRm =0 ,/* Secure deletion */ If these values are not used on-disk or on-network, the =N parts could go away.
These have to be equal to the values of the constants as originally defined by ext2/3 and also used/extended by other fs. This is generic infrastructure originally requested by Christoph. I didn't want to carry patches to all the other filesystems in the GFS2 git tree, but they can all use this as well and I'll send some patches once this has hit Linus kernel tree.
Quoted 6 lines
fs/gfs2/ops_fstype.c line 806
Quoted 2 lines
+
fail_sys:fs/gfs2/ops_fstype.c new file +1
-0+gfs2_sys_fs_del (sdp);Some labels in your code have a leading space, others do not. Make it consistent.
Fixed in an earlier commit.
Quoted 1 line
Quoted 1 line
+static int gfs2_get_sb(struct
file_system_type*fs_type, int flags,
[lines snipped]
Quoted 5 lines
sb = mnt->mnt_sb;fs/gfs2/ops_fstype.c line 824
Quoted 1 line
fs/gfs2/ops_fstype.c new file +1
-0+ sdp = (struct gfs2_sbd *)sb->s_fs_info;Nocast.
ok.
Quoted 7 lines
fs/gfs2/ops_fstype.c line 865
Quoted 4 lines
fs/gfs2/ops_fstype.c new file +4
-0+static int test_bdev_super (struct super_block *s,void *data) +{ +return (void *)s->s_bdev == data; +}Nocast.
ok
Quoted 7 lines
fs/gfs2/ops_fstype.c line 917
Quoted 4 lines
fs/gfs2/ops_fstype.c new file +4
-0+static int gfs2_get_sb_meta (struct file_system_type *fs_type,int flags, +const char *dev_name,void *data,struct vfsmount *mnt) +{ + sdp = (struct gfs2_sbd *) sb->s_fs_info;Nocast.
ok.
Quoted 4 lines
fs/gfs2/ops_inode.c line 429
Quoted 1 line
fs/gfs2/ops_inode.c new file +1
-0+ error =gfs2_change_nlink (dip,+1 );Interesting annotation of direction (+1) :-)
Well it seemed to make sense here, so I left it alone.
Quoted 5 lines
fs/gfs2/ops_inode.c line 602
Quoted 1 line
fs/gfs2/ops_inode.c new file +1
-0+/* Make sure we aren't trying to move a dirctory into it's subdir */ Should not the VFS handle this? (Or at least, the VFS should have some generic_*_check_move_subdir() or so, in case a fs does not have its own.)
It can't handle this since the VFS doesn't know about locking across the cluster. GFS2 has to redo the checks which the VFS does once it has the glocks required for the operation.
Quoted 6 lines
Quoted 3 lines
+/**
+ *gfs2_getattr- Read out an inode's attributes
+ * @mnt: ?Might complete the doc.
fixed.
Quoted 4 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ struct gfs2_ea_request er;Just for info, EA stands for what?
Extended Attribute. I don't see anywhere convenient to add a note to the code though... I guessed that it was reasonably obvious since it occurred in a function relating to extended attributes. The patch for this email is:
Steve.
Quoted 9 lines
Quoted 9 lines
Is not there a better way to do this? Note that there is also a "nfsd4" process running. Do you really want to do a 'costly'
strcmp(current->comm, "nfsd") != 0 && strcmp(current->comm, "nfsd4") != 0every time someone does a readdir? What if I run a userspace nfs daemon? What if that userspace daemon is called differently?
Quoted 8 lines
Quoted 4 lines
jengelh@linux01:7 08:28:14 ~ > ps -e | grep nfs
5580 ? 00:00:08 rpc.nfsd
jengelh@linux01:7 08:28:30 ~ > rpm -qf/usr/sbin/rpc.nfsdnfs-server-2.2beta51-209.2 (a userspace nfsd)Ah, now this is a very tricky one to solve. Its on my todo list to look at this area again. You are right that the test is bogus in that it should only respond to the in kernel NFS server and the reason for its
In that case, you could use the fact that ... (I'll express it in C code):
is_kernel_task = !current->mm;
Quoted 2 lines
existence is due to locking issues with the way that NFS calls through the VFS layer.
Jan Engelhardt
Quoted 4 lines
fs/gfs2/ops_file.c line 84
Quoted 4 lines
fs/gfs2/ops_file.c new file +3
-0+struct file gfs2_internal_file_sentinal = { + .f_flags =O_NOATIME |O_RDONLY , +};
Statically allocation a struct file is not allowed. Where do you use gfs2_internal_read? It really shouldn't be needed.
Quoted 7 lines
fs/gfs2/ops_file.c line 367
Quoted 5 lines
fs/gfs2/ops_file.c new file +5
-0+static int gfs2_readdir (struct file *file,void *dirent,filldir_t filldir) +{ +int error; + +if (strcmp (current->comm,"nfsd" ) !=0 )Is not there a better way to do this? Note that there is also a "nfsd4" process
This is in fact not allowed at at all. Please fix you readdir code not to need special cases for nfsd.
Hi,
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines
fs/gfs2/ops_file.c line 84
Quoted 4 lines
fs/gfs2/ops_file.c new file +3
-0+struct file gfs2_internal_file_sentinal = { + .f_flags =O_NOATIME |O_RDONLY , +};Statically allocation a struct file is not allowed. Where do you use
gfs2_internal_read? It really shouldn't be needed.
The sentinel is used as a flag value only. Its passed through to the underlying address space operations of readpage(s) to indicate that the glock for the inode is already held and therefore locking isn't required at the address space operations level. The glock is used to ensure consistancy when reading certain "internal" files.
For example, the resource index contains a list of all the resource groups on disk and is written through the gfs2meta filesystem when the filesystem is expanded. We need a way to read the resource index in, in such a way as to ensure its not being updated in the middle of the read. So we use gfs2_internal_read to do that, holding the glock for the entire operation.
It has a similar use for some of the other internal files as well.
Quoted 11 lines
Quoted 7 lines
fs/gfs2/ops_file.c line 367
Quoted 5 lines
fs/gfs2/ops_file.c new file +5
-0+static int gfs2_readdir (struct file *file,void *dirent,filldir_t filldir) +{ +int error; + +if (strcmp (current->comm,"nfsd" ) !=0 )Is not there a better way to do this? Note that there is also a "nfsd4" process
This is in fact not allowed at at all. Please fix you readdir code not to need special cases for nfsd.
Perhaps you can suggest a solution to the problem.... We have a bz #201015 (in bugzilla.redhat.com) tracking this.
The problem is caused by the way in which nfsd does a stat from readdir's filldir routine. We have to acquire and drop the directory glock in filldir otherwise it may deadlock with the glocks which are taken by stat. As a result of this, we then have to revalidate the position in the directory on each call to filldir, rather than locking the whole readdir call as the "normal" version does, so the current nfs variant of readdir is much slower than the "normal" one even though it could otherwise potentially be used for both cases.
I will try and have a look at this problem in more detail some time this week,
Steve.
Hi,
Quoted 9 lines · Christoph Hellwig
Quoted 8 lines
fs/gfs2/ops_file.c line 367
Quoted 6 lines
fs/gfs2/ops_file.c new file +5
-0+static int gfs2_readdir (struct file *file,void *dirent,filldir_t filldir) +{ +int error; + +if (strcmp (current->comm,"nfsd" ) !=0 )Is not there a better way to do this? Note that there is also a "nfsd4" process
Quoted 3 lines
This is in fact not allowed at at all. Please fix you readdir code not to need special cases for nfsd.
This has now been done in the git tree, and I believe that the resulting lock ordering will be deadlock free,
Steve.