/** * ntfs_file_fsync - sync a file to disk * @filp: file to be synced * @dentry: dentry describing the file to sync * @datasync: if non-zero only flush user data and not metadata * * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync * system calls. This function is inspired by fs/buffer.c::file_fsync(). * * If @datasync is false, write the mft record and all associated extent mft * records as well as the $DATA attribute and then sync the block device. * * If @datasync is true and the attribute is non-resident, we skip the writing * of the mft record and all associated extent mft records (this might still * happen due to the write_inode_now() call). * * Also, if @datasync is true, we do not wait on the inode to be written out * but we always wait on the page cache pages to be written out. * * Note: In the past @filp could be NULL so we ignore it as we don't need it * anyway. * * Locking: Caller must hold i_sem on the inode. * * TODO: We should probably also write all attribute/index inodes associated * with this inode but since we have no simple way of getting to them we ignore * this problem for now. */ static int ntfs_file_fsync(struct file *filp, struct dentry *dentry, int datasync) { struct inode *vi = dentry->d_inode; int err, ret = 0; ntfs_debug("Entering for inode 0x%lx.", vi->i_ino); BUG_ON(S_ISDIR(vi->i_mode)); if (!datasync || !NInoNonResident(NTFS_I(vi))) ret = ntfs_write_inode(vi, 1); write_inode_now(vi, !datasync); /* * NOTE: If we were to use mapping->private_list (see ext2 and * fs/buffer.c) for dirty blocks then we could optimize the below to be * sync_mapping_buffers(vi->i_mapping). */ err = sync_blockdev(vi->i_sb->s_bdev); if (unlikely(err && !ret)) ret = err; if (likely(!ret)) ntfs_debug("Done."); else ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error " "%u.", datasync ? "data" : "", vi->i_ino, -ret); return ret; }
static int fat_cont_expand(struct inode *inode, loff_t size) { struct address_space *mapping = inode->i_mapping; loff_t start = inode->i_size, count = size - inode->i_size; int err; err = generic_cont_expand_simple(inode, size); if (err) goto out; inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; mark_inode_dirty(inode); if (IS_SYNC(inode)) { int err2; /* * Opencode syncing since we don't have a file open to use * standard fsync path. */ err = filemap_fdatawrite_range(mapping, start, start + count - 1); err2 = sync_mapping_buffers(mapping); if (!err) err = err2; err2 = write_inode_now(inode, 1); if (!err) err = err2; if (!err) { err = filemap_fdatawait_range(mapping, start, start + count - 1); } } out: return err; }
/* Support function for ocfs2_delete_inode. Will help us keep the * inode data in a consistent state for clear_inode. Always truncates * pages, optionally sync's them first. */ static void ocfs2_cleanup_delete_inode(struct inode *inode, int sync_data) { mlog(0, "Cleanup inode %llu, sync = %d\n", (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data); if (sync_data) write_inode_now(inode, 1); truncate_inode_pages(&inode->i_data, 0); }
/* Called under inode_lock, with no more references on the * struct inode, so it's safe here to check the flags field * and to manipulate i_nlink without any other locks. */ int ocfs2_drop_inode(struct inode *inode) { struct ocfs2_inode_info *oi = OCFS2_I(inode); trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags); assert_spin_locked(&inode->i_lock); inode->i_state |= I_WILL_FREE; spin_unlock(&inode->i_lock); write_inode_now(inode, 1); spin_lock(&inode->i_lock); WARN_ON(inode->i_state & I_NEW); inode->i_state &= ~I_WILL_FREE; return 1; }
/* * Generic function to fsync a file. */ int file_fsync(struct file *filp, int datasync) { struct inode *inode = filp->f_mapping->host; struct super_block * sb; int ret, err; /* sync the inode to buffers */ ret = write_inode_now(inode, 0); /* sync the superblock to buffers */ sb = inode->i_sb; if (sb->s_dirt && sb->s_op->write_super) sb->s_op->write_super(sb); /* .. finally sync the buffers to disk */ err = sync_blockdev(sb->s_bdev); if (!ret) ret = err; return ret; }
/** * ntfs_file_fsync - sync a file to disk * @filp: file to be synced * @dentry: dentry describing the file to sync * @datasync: if non-zero only flush user data and not metadata * * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync * system calls. This function is inspired by fs/buffer.c::file_fsync(). * * If @datasync is false, write the mft record and all associated extent mft * records as well as the $DATA attribute and then sync the block device. * * If @datasync is true and the attribute is non-resident, we skip the writing * of the mft record and all associated extent mft records (this might still * happen due to the write_inode_now() call). * * Also, if @datasync is true, we do not wait on the inode to be written out * but we always wait on the page cache pages to be written out. * * Note: In the past @filp could be NULL so we ignore it as we don't need it * anyway. * * Locking: Caller must hold i_sem on the inode. * * TODO: We should probably also write all attribute/index inodes associated * with this inode but since we have no simple way of getting to them we ignore * this problem for now. */ static int ntfs_file_fsync(struct file *filp, struct dentry *dentry, int datasync) { struct inode *vi = dentry->d_inode; int err, ret = 0; ntfs_debug("Entering for inode 0x%lx.", vi->i_ino); BUG_ON(S_ISDIR(vi->i_mode)); if (!datasync || !NInoNonResident(NTFS_I(vi))) ret = ntfs_write_inode(vi, 1); write_inode_now(vi, !datasync); err = sync_blockdev(vi->i_sb->s_bdev); if (unlikely(err && !ret)) ret = err; if (likely(!ret)) ntfs_debug("Done."); else ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error " "%u.", datasync ? "data" : "", vi->i_ino, -ret); return ret; }
static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync) { struct inode *inode = dentry->d_inode; int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC); int ret = 0; if (gfs2_is_jdata(GFS2_I(inode))) { gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl); return 0; } if (sync_state != 0) { if (!datasync) ret = write_inode_now(inode, 0); if (gfs2_is_stuffed(GFS2_I(inode))) gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl); } return ret; }
static int exofs_file_fsync(struct file *filp, int datasync) { int ret; struct address_space *mapping = filp->f_mapping; struct inode *inode = mapping->host; struct super_block *sb; ret = filemap_write_and_wait(mapping); if (ret) return ret; /* sync the inode attributes */ ret = write_inode_now(inode, 1); /* This is a good place to write the sb */ /* TODO: Sechedule an sb-sync on create */ sb = inode->i_sb; if (sb->s_dirt) exofs_sync_fs(sb, 1); return ret; }
/* * Generic function to fsync a file. * * filp may be NULL if called via the msync of a vma. */ int file_fsync(struct file *filp, struct dentry *dentry, int datasync) { struct inode * inode = dentry->d_inode; struct super_block * sb; int ret, err; /* sync the inode to buffers */ ret = write_inode_now(inode, 0); /* sync the superblock to buffers */ sb = inode->i_sb; lock_super(sb); if (is_sb_dirty(sb) && sb->s_op->write_super) sb->s_op->write_super(sb); unlock_super(sb); /* .. finally sync the buffers to disk */ err = sync_blockdev(sb->s_bdev); if (!ret) ret = err; return ret; }
ssize_t GSFS_file_write(struct file *filp, const char __user *buf, size_t len, loff_t *off){ struct inode *inode=filp->f_mapping->host; struct address_space *mapping=filp->f_mapping; sector_t sec_start, sec_end, sec_len; unsigned long bufstart, bytes_in_first_buf_page, bytes_in_last_buf_page, pagestartbyte, pageendbyte, pagelen; size_t rlen; struct page *res[2], *page, **pages; unsigned int i, j, pages_count, start_read, end_read; gwf(printk("<0>" "File write with inode :%lu len:%lu offset:%llu , filepos:%llu, buf:%lx inode_size:%llu, pid:%u\n",inode->i_ino,len,*off,filp->f_pos,(unsigned long)buf,inode->i_size,current->pid)); if(unlikely(!access_ok(VERIFY_READ,buf,len))) return -1; mutex_lock(&inode->i_mutex); if((*off+len)>inode->i_size){ if((*off+len)>inode->i_sb->s_maxbytes) return -1; inode->i_size=(*off+len); GSFS_truncate(inode); } if(filp->f_flags & O_APPEND) *off=inode->i_size; current->backing_dev_info=mapping->backing_dev_info; file_remove_suid(filp); file_update_time(filp); //inode_inc_iversion(inode); sec_start=(*off)>>Block_Size_Bits; sec_end=(*off+len-1)>>Block_Size_Bits; sec_len=sec_end-sec_start+1; pages=kzalloc(sizeof(struct page*) * sec_len, GFP_KERNEL); bytes_in_first_buf_page=Block_Size-((*off)&((unsigned long)Block_Size-1)); bytes_in_last_buf_page=((*off+len)&((unsigned long)Block_Size-1)); if(bytes_in_last_buf_page==0) bytes_in_last_buf_page=Block_Size; start_read=(bytes_in_first_buf_page!=Block_Size)?1:0; end_read=(bytes_in_last_buf_page!=Block_Size && inode->i_size>(*off+len))?1:0; gwf(printk("<0>" "GSFS write bytes_in_first_buf_page:%lu, bytes_in_last_buf_page:%lu\n",bytes_in_first_buf_page,bytes_in_last_buf_page)); gwf(printk("<0>" "GSFS write start_read:%u, end_read:%u, sec_start:%lu, sec_end:%lu\n",start_read,end_read,sec_start,sec_end)); if(sec_start==sec_end){ if(start_read || end_read){ res[0]=GSFS_get_data_page_of_inode_with_read(inode, sec_start); gwf(printk("<0>" "sec_start==sec_end, start_read || end_read , res[0]=%lx",(unsigned long)res[0])); } else{ res[0]=GSFS_get_locked_data_page_of_inode_without_read(inode,sec_start); if(likely(res[0])) unlock_page(res[0]); gwf(printk("<0>" "sec_start==sec_end, !(start_read || end_read) , res[0]=%lx",(unsigned long)res[0])); } res[1]=0; if(unlikely(!res[0])){ gwf(printk("<0>" "GSFS write len:-1\n")); mutex_unlock(&inode->i_mutex); printk("<1>" "GSFS write len:-1\n"); kfree(pages); return len; } } else{ if(start_read){ res[0]=GSFS_get_data_page_of_inode_with_read(inode, sec_start); gwf(printk("<0>" "sec_start!=sec_end, start_read, res[0]=%lx",(unsigned long)res[0])); } else{ res[0]=GSFS_get_locked_data_page_of_inode_without_read(inode,sec_start); if(likely(res[0])) unlock_page(res[0]); gwf(printk("<0>" "sec_start!=sec_end, !start_read, res[0]=%lx",(unsigned long)res[0])); } } pages_count=0; if(sec_len>1) for(i=sec_start+1;i<=sec_end-1;i++) pages[pages_count++]=GSFS_get_locked_data_page_of_inode_without_read(inode,i); if(sec_start != sec_end){ if(end_read){ res[1]=GSFS_get_data_page_of_inode_with_read(inode,sec_end); gwf(printk("<0>" "sec_start!=sec_end, end_read, res[1]=%lx",(unsigned long)res[1])); } else{ res[1]=GSFS_get_locked_data_page_of_inode_without_read(inode,sec_end); if(likely(res[1])) unlock_page(res[1]); gwf(printk("<0>" "sec_start!=sec_end, !end_read, res[1]=%lx",(unsigned long)res[1])); } if(unlikely(!res[0] || !res[1])){ gwf(printk("<0>" "GSFS write len:-1\n")); printk("<1>" "GSFS write len:-1\n"); mutex_unlock(&inode->i_mutex); kfree(pages); return len; } } rlen=0; bufstart=(unsigned long)buf+bytes_in_first_buf_page; pagelen=Block_Size; //100% expected complete pages that should be copied pages_count=0; if(sec_len>1) for(i=sec_start+1;i<=sec_end-1;i++){ gwf(printk("<0>" "write page complete pages, i:%u, bufstart:%lx, rlen=%lu\n",i,bufstart,rlen)); page=pages[pages_count++]; if(unlikely(!page)) goto buf_cont; j=__copy_from_user_inatomic(page_address(page),(void*)bufstart,pagelen); rlen+=(Block_Size-j); mark_page_accessed(page); set_page_dirty(page); put_page(page); unlock_page(page); buf_cont: bufstart+=pagelen; } //first and last page that are not surely complete for(i=0;i<2 && res[i];i++){ page=res[i]; wait_on_page_locked(page); lock_page(page); if(page->index==sec_start){ bufstart=(unsigned long)buf; pagestartbyte=Block_Size-bytes_in_first_buf_page; if(sec_start==sec_end) pageendbyte=pagestartbyte+len-1; else pageendbyte=Block_Size-1; } else{ bufstart=(unsigned long)buf+bytes_in_first_buf_page+((sec_len-2)<<Block_Size_Bits); pageendbyte=bytes_in_last_buf_page-1; pagestartbyte=0; } gwf(printk("<0>" "gsfs_write for first and last page, i=%u, page:%lx, bufstart:%lx, pagestartbyte:%lu, pageendbyte:%lu\n", i,(unsigned long)page,bufstart,pagestartbyte,pageendbyte)); pagelen=pageendbyte-pagestartbyte+1; j=__copy_from_user_inatomic(page_address(page)+pagestartbyte,(void*)bufstart,pagelen); rlen+=(pagelen-j); mark_page_accessed(page); set_page_dirty(page); put_page(page); unlock_page(page); } mutex_unlock(&inode->i_mutex); (*off)+=rlen; gwf(printk("<0>" "GSFS write rlen:%lu\n",rlen)); kfree(pages); if(filp->f_flags & O_SYNC){ write_inode_now(inode,1); } return rlen; }
/* * Set various file attributes. * N.B. After this call fhp needs an fh_put */ __be32 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, int check_guard, time_t guardtime) { struct dentry *dentry; struct inode *inode; int accmode = NFSD_MAY_SATTR; int ftype = 0; __be32 err; int host_err; int size_change = 0; if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE)) accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE; if (iap->ia_valid & ATTR_SIZE) ftype = S_IFREG; /* Get inode */ err = fh_verify(rqstp, fhp, ftype, accmode); if (err) goto out; dentry = fhp->fh_dentry; inode = dentry->d_inode; /* Ignore any mode updates on symlinks */ if (S_ISLNK(inode->i_mode)) iap->ia_valid &= ~ATTR_MODE; if (!iap->ia_valid) goto out; /* * NFSv2 does not differentiate between "set-[ac]time-to-now" * which only requires access, and "set-[ac]time-to-X" which * requires ownership. * So if it looks like it might be "set both to the same time which * is close to now", and if inode_change_ok fails, then we * convert to "set to now" instead of "set to explicit time" * * We only call inode_change_ok as the last test as technically * it is not an interface that we should be using. It is only * valid if the filesystem does not define it's own i_op->setattr. */ #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET) #define MAX_TOUCH_TIME_ERROR (30*60) if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) { /* * Looks probable. * * Now just make sure time is in the right ballpark. * Solaris, at least, doesn't seem to care what the time * request is. We require it be within 30 minutes of now. */ time_t delta = iap->ia_atime.tv_sec - get_seconds(); if (delta < 0) delta = -delta; if (delta < MAX_TOUCH_TIME_ERROR && inode_change_ok(inode, iap) != 0) { /* * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME. * This will cause notify_change to set these times * to "now" */ iap->ia_valid &= ~BOTH_TIME_SET; } } /* * The size case is special. * It changes the file as well as the attributes. */ if (iap->ia_valid & ATTR_SIZE) { if (iap->ia_size < inode->i_size) { err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE); if (err) goto out; } /* * If we are changing the size of the file, then * we need to break all leases. */ host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK); if (host_err == -EWOULDBLOCK) host_err = -ETIMEDOUT; if (host_err) /* ENOMEM or EWOULDBLOCK */ goto out_nfserr; host_err = get_write_access(inode); if (host_err) goto out_nfserr; size_change = 1; host_err = locks_verify_truncate(inode, NULL, iap->ia_size); if (host_err) { put_write_access(inode); goto out_nfserr; } DQUOT_INIT(inode); } /* sanitize the mode change */ if (iap->ia_valid & ATTR_MODE) { iap->ia_mode &= S_IALLUGO; iap->ia_mode |= (inode->i_mode & ~S_IALLUGO); } /* Revoke setuid/setgid on chown */ if (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) || ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)) { iap->ia_valid |= ATTR_KILL_PRIV; if (iap->ia_valid & ATTR_MODE) { /* we're setting mode too, just clear the s*id bits */ iap->ia_mode &= ~S_ISUID; if (iap->ia_mode & S_IXGRP) iap->ia_mode &= ~S_ISGID; } else { /* set ATTR_KILL_* bits and let VFS handle it */ iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID); } } /* Change the attributes. */ iap->ia_valid |= ATTR_CTIME; err = nfserr_notsync; if (!check_guard || guardtime == inode->i_ctime.tv_sec) { fh_lock(fhp); host_err = notify_change(dentry, iap); err = nfserrno(host_err); fh_unlock(fhp); } if (size_change) put_write_access(inode); if (!err) if (EX_ISSYNC(fhp->fh_export)) write_inode_now(inode, 1); out: return err; out_nfserr: err = nfserrno(host_err); goto out; }
static int setflags(struct inode *inode, int flags) { int oldflags, err, release; struct ubifs_inode *ui = ubifs_inode(inode); struct ubifs_info *c = inode->i_sb->s_fs_info; struct ubifs_budget_req req = { .dirtied_ino = 1, .dirtied_ino_d = ui->data_len }; err = ubifs_budget_space(c, &req); if (err) return err; /* * The IMMUTABLE and APPEND_ONLY flags can only be changed by * the relevant capability. */ mutex_lock(&ui->ui_mutex); oldflags = ubifs2ioctl(ui->flags); if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { if (!capable(CAP_LINUX_IMMUTABLE)) { err = -EPERM; goto out_unlock; } } ui->flags = ioctl2ubifs(flags); ubifs_set_inode_flags(inode); inode->i_ctime = ubifs_current_time(inode); release = ui->dirty; mark_inode_dirty_sync(inode); mutex_unlock(&ui->ui_mutex); if (release) ubifs_release_budget(c, &req); if (IS_SYNC(inode)) err = write_inode_now(inode, 1); return err; out_unlock: ubifs_err("can't modify inode %lu attributes", inode->i_ino); mutex_unlock(&ui->ui_mutex); ubifs_release_budget(c, &req); return err; } long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { int flags, err; struct inode *inode = file->f_path.dentry->d_inode; switch (cmd) { case FS_IOC_GETFLAGS: flags = ubifs2ioctl(ubifs_inode(inode)->flags); dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags); return put_user(flags, (int __user *) arg); case FS_IOC_SETFLAGS: { if (IS_RDONLY(inode)) return -EROFS; if (!inode_owner_or_capable(inode)) return -EACCES; if (get_user(flags, (int __user *) arg)) return -EFAULT; if (!S_ISDIR(inode->i_mode)) flags &= ~FS_DIRSYNC_FL; /* * Make sure the file-system is read-write and make sure it * will not become read-only while we are changing the flags. */ err = mnt_want_write(file->f_path.mnt); if (err) return err; dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags); err = setflags(inode, flags); mnt_drop_write(file->f_path.mnt); return err; } default: return -ENOTTY; } }
/* * Set various file attributes. * N.B. After this call fhp needs an fh_put */ int nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, int check_guard, time_t guardtime) { struct dentry *dentry; struct inode *inode; int accmode = MAY_SATTR; int ftype = 0; int imode; int err; int size_change = 0; if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE)) accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE; if (iap->ia_valid & ATTR_SIZE) ftype = S_IFREG; /* Get inode */ err = fh_verify(rqstp, fhp, ftype, accmode); if (err || !iap->ia_valid) goto out; dentry = fhp->fh_dentry; inode = dentry->d_inode; /* NFSv2 does not differentiate between "set-[ac]time-to-now" * which only requires access, and "set-[ac]time-to-X" which * requires ownership. * So if it looks like it might be "set both to the same time which * is close to now", and if inode_change_ok fails, then we * convert to "set to now" instead of "set to explicit time" * * We only call inode_change_ok as the last test as technically * it is not an interface that we should be using. It is only * valid if the filesystem does not define it's own i_op->setattr. */ #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET) #define MAX_TOUCH_TIME_ERROR (30*60) if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET && iap->ia_mtime == iap->ia_atime ) { /* Looks probable. Now just make sure time is in the right ballpark. * Solaris, at least, doesn't seem to care what the time request is. * We require it be within 30 minutes of now. */ time_t delta = iap->ia_atime - CURRENT_TIME; if (delta<0) delta = -delta; if (delta < MAX_TOUCH_TIME_ERROR && inode_change_ok(inode, iap) != 0) { /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME * this will cause notify_change to set these times to "now" */ iap->ia_valid &= ~BOTH_TIME_SET; } } /* The size case is special. It changes the file as well as the attributes. */ if (iap->ia_valid & ATTR_SIZE) { if (iap->ia_size < inode->i_size) { err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE); if (err) goto out; } /* * If we are changing the size of the file, then * we need to break all leases. */ err = get_lease(inode, FMODE_WRITE); if (err) goto out_nfserr; err = get_write_access(inode); if (err) goto out_nfserr; err = locks_verify_truncate(inode, NULL, iap->ia_size); if (err) { put_write_access(inode); goto out_nfserr; } DQUOT_INIT(inode); } imode = inode->i_mode; if (iap->ia_valid & ATTR_MODE) { iap->ia_mode &= S_IALLUGO; imode = iap->ia_mode |= (imode & ~S_IALLUGO); } /* Revoke setuid/setgid bit on chown/chgrp */ if ((iap->ia_valid & ATTR_UID) && (imode & S_ISUID) && iap->ia_uid != inode->i_uid) { iap->ia_valid |= ATTR_MODE; iap->ia_mode = imode &= ~S_ISUID; } if ((iap->ia_valid & ATTR_GID) && (imode & S_ISGID) && iap->ia_gid != inode->i_gid) { iap->ia_valid |= ATTR_MODE; iap->ia_mode = imode &= ~S_ISGID; } /* Change the attributes. */ iap->ia_valid |= ATTR_CTIME; if (iap->ia_valid & ATTR_SIZE) { fh_lock(fhp); size_change = 1; } err = nfserr_notsync; if (!check_guard || guardtime == inode->i_ctime) { err = notify_change(dentry, iap); err = nfserrno(err); } if (size_change) { fh_unlock(fhp); put_write_access(inode); } if (!err) if (EX_ISSYNC(fhp->fh_export)) write_inode_now(inode, 1); out: return err; out_nfserr: err = nfserrno(err); goto out; }
/* * Rename a file * N.B. After this call _both_ ffhp and tfhp need an fh_put */ int nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, struct svc_fh *tfhp, char *tname, int tlen) { struct dentry *fdentry, *tdentry, *odentry, *ndentry; struct inode *fdir, *tdir; int err; err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE); if (err) goto out; err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE); if (err) goto out; fdentry = ffhp->fh_dentry; fdir = fdentry->d_inode; tdentry = tfhp->fh_dentry; tdir = tdentry->d_inode; /* N.B. We shouldn't need this ... dentry layer handles it */ err = nfserr_perm; if (!flen || (fname[0] == '.' && (flen == 1 || (flen == 2 && fname[1] == '.'))) || !tlen || (tname[0] == '.' && (tlen == 1 || (tlen == 2 && tname[1] == '.')))) goto out; odentry = lookup_dentry(fname, dget(fdentry), 0); err = PTR_ERR(odentry); if (IS_ERR(odentry)) goto out_nfserr; err = -ENOENT; if (!odentry->d_inode) goto out_dput_old; ndentry = lookup_dentry(tname, dget(tdentry), 0); err = PTR_ERR(ndentry); if (IS_ERR(ndentry)) goto out_dput_old; /* * Lock the parent directories. */ nfsd_double_down(&tdir->i_sem, &fdir->i_sem); err = -ENOENT; /* GAM3 check for parent changes after locking. */ if (check_parent(fdir, odentry) && check_parent(tdir, ndentry)) { err = vfs_rename(fdir, odentry, tdir, ndentry); if (!err && EX_ISSYNC(tfhp->fh_export)) { write_inode_now(fdir); write_inode_now(tdir); } } else dprintk("nfsd: Caught race in nfsd_rename"); DQUOT_DROP(fdir); DQUOT_DROP(tdir); nfsd_double_up(&tdir->i_sem, &fdir->i_sem); dput(ndentry); out_dput_old: dput(odentry); if (err) goto out_nfserr; out: return err; out_nfserr: err = nfserrno(-err); goto out; }
/* * Unlink a file or directory * N.B. After this call fhp needs an fh_put */ int nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, char *fname, int flen) { struct dentry *dentry, *rdentry; struct inode *dirp; int err; /* N.B. We shouldn't need this test ... handled by dentry layer */ err = nfserr_acces; if (!flen || isdotent(fname, flen)) goto out; err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE); if (err) goto out; dentry = fhp->fh_dentry; dirp = dentry->d_inode; rdentry = lookup_dentry(fname, dget(dentry), 0); err = PTR_ERR(rdentry); if (IS_ERR(rdentry)) goto out_nfserr; if (!rdentry->d_inode) { dput(rdentry); err = nfserr_noent; goto out; } if (type != S_IFDIR) { /* It's UNLINK */ err = fh_lock_parent(fhp, rdentry); if (err) goto out; err = vfs_unlink(dirp, rdentry); DQUOT_DROP(dirp); fh_unlock(fhp); dput(rdentry); } else { /* It's RMDIR */ /* See comments in fs/namei.c:do_rmdir */ rdentry->d_count++; nfsd_double_down(&dirp->i_sem, &rdentry->d_inode->i_sem); if (!fhp->fh_pre_mtime) fhp->fh_pre_mtime = dirp->i_mtime; fhp->fh_locked = 1; err = -ENOENT; if (check_parent(dirp, rdentry)) err = vfs_rmdir(dirp, rdentry); rdentry->d_count--; DQUOT_DROP(dirp); if (!fhp->fh_post_version) fhp->fh_post_version = dirp->i_version; fhp->fh_locked = 0; nfsd_double_up(&dirp->i_sem, &rdentry->d_inode->i_sem); dput(rdentry); } if (err) goto out_nfserr; if (EX_ISSYNC(fhp->fh_export)) write_inode_now(dirp); out: return err; out_nfserr: err = nfserrno(-err); goto out; }
/* * Set various file attributes. * N.B. After this call fhp needs an fh_put */ int nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap) { struct dentry *dentry; struct inode *inode; int accmode = MAY_SATTR; int ftype = 0; int imode; int err; if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE)) accmode |= MAY_WRITE; if (iap->ia_valid & ATTR_SIZE) ftype = S_IFREG; /* Get inode */ err = fh_verify(rqstp, fhp, ftype, accmode); if (err) goto out; dentry = fhp->fh_dentry; inode = dentry->d_inode; err = inode_change_ok(inode, iap); if (err) goto out_nfserr; /* The size case is special... */ if (iap->ia_valid & ATTR_SIZE) { if (!S_ISREG(inode->i_mode)) printk("nfsd_setattr: size change??\n"); if (iap->ia_size < inode->i_size) { err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC); if (err) goto out; } err = get_write_access(inode); if (err) goto out_nfserr; /* N.B. Should we update the inode cache here? */ inode->i_size = iap->ia_size; if (inode->i_op && inode->i_op->truncate) inode->i_op->truncate(inode); mark_inode_dirty(inode); put_write_access(inode); iap->ia_valid &= ~ATTR_SIZE; iap->ia_valid |= ATTR_MTIME; iap->ia_mtime = CURRENT_TIME; } imode = inode->i_mode; if (iap->ia_valid & ATTR_MODE) { iap->ia_mode &= S_IALLUGO; imode = iap->ia_mode |= (imode & ~S_IALLUGO); } /* Revoke setuid/setgid bit on chown/chgrp */ if ((iap->ia_valid & ATTR_UID) && (imode & S_ISUID) && iap->ia_uid != inode->i_uid) { iap->ia_valid |= ATTR_MODE; iap->ia_mode = imode &= ~S_ISUID; } if ((iap->ia_valid & ATTR_GID) && (imode & S_ISGID) && iap->ia_gid != inode->i_gid) { iap->ia_valid |= ATTR_MODE; iap->ia_mode = imode &= ~S_ISGID; } /* Change the attributes. */ if (iap->ia_valid) { kernel_cap_t saved_cap = 0; iap->ia_valid |= ATTR_CTIME; iap->ia_ctime = CURRENT_TIME; if (current->fsuid != 0) { saved_cap = current->cap_effective; cap_clear(current->cap_effective); } err = notify_change(dentry, iap); if (current->fsuid != 0) current->cap_effective = saved_cap; if (err) goto out_nfserr; if (EX_ISSYNC(fhp->fh_export)) write_inode_now(inode); } err = 0; out: return err; out_nfserr: err = nfserrno(-err); goto out; }
/* * Create a hardlink * N.B. After this call _both_ ffhp and tfhp need an fh_put */ int nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int len, struct svc_fh *tfhp) { struct dentry *ddir, *dnew, *dold; struct inode *dirp, *dest; int err; err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE); if (err) goto out; err = fh_verify(rqstp, tfhp, S_IFREG, MAY_NOP); if (err) goto out; err = nfserr_perm; if (!len) goto out; ddir = ffhp->fh_dentry; dirp = ddir->d_inode; dnew = lookup_dentry(fname, dget(ddir), 0); err = PTR_ERR(dnew); if (IS_ERR(dnew)) goto out_nfserr; /* * Lock the parent before checking for existence */ err = fh_lock_parent(ffhp, dnew); if (err) goto out_dput; err = nfserr_exist; if (dnew->d_inode) goto out_unlock; dold = tfhp->fh_dentry; dest = dold->d_inode; err = nfserr_acces; if (nfsd_iscovered(ddir, ffhp->fh_export)) goto out_unlock; /* FIXME: nxdev for NFSv3 */ if (dirp->i_dev != dest->i_dev) goto out_unlock; err = nfserr_perm; if (IS_IMMUTABLE(dest) /* || IS_APPEND(dest) */ ) goto out_unlock; if (!dirp->i_op || !dirp->i_op->link) goto out_unlock; DQUOT_INIT(dirp); err = dirp->i_op->link(dold, dirp, dnew); DQUOT_DROP(dirp); if (!err) { if (EX_ISSYNC(ffhp->fh_export)) { write_inode_now(dirp); write_inode_now(dest); } } else err = nfserrno(-err); out_unlock: fh_unlock(ffhp); out_dput: dput(dnew); out: return err; out_nfserr: err = nfserrno(-err); goto out; }
/* * Create a symlink and look up its inode * N.B. After this call _both_ fhp and resfhp need an fh_put */ int nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *fname, int flen, char *path, int plen, struct svc_fh *resfhp) { struct dentry *dentry, *dnew; struct inode *dirp; int err; err = nfserr_noent; if (!flen || !plen) goto out; err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE); if (err) goto out; dentry = fhp->fh_dentry; err = nfserr_perm; if (nfsd_iscovered(dentry, fhp->fh_export)) goto out; dirp = dentry->d_inode; if (!dirp->i_op || !dirp->i_op->symlink) goto out; dnew = lookup_dentry(fname, dget(dentry), 0); err = PTR_ERR(dnew); if (IS_ERR(dnew)) goto out_nfserr; /* * Lock the parent before checking for existence */ err = fh_lock_parent(fhp, dnew); if (err) goto out_compose; err = nfserr_exist; if (!dnew->d_inode) { DQUOT_INIT(dirp); err = dirp->i_op->symlink(dirp, dnew, path); DQUOT_DROP(dirp); if (!err) { if (EX_ISSYNC(fhp->fh_export)) write_inode_now(dirp); } else err = nfserrno(-err); } fh_unlock(fhp); /* Compose the fh so the dentry will be freed ... */ out_compose: fh_compose(resfhp, fhp->fh_export, dnew); out: return err; out_nfserr: err = nfserrno(-err); goto out; }
/* * Create a file (regular, directory, device, fifo); UNIX sockets * not yet implemented. * If the response fh has been verified, the parent directory should * already be locked. Note that the parent directory is left locked. * * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp */ int nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, char *fname, int flen, struct iattr *iap, int type, dev_t rdev, struct svc_fh *resfhp) { struct dentry *dentry, *dchild; struct inode *dirp; nfsd_dirop_t opfunc = NULL; int err; err = nfserr_perm; if (!flen) goto out; err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE); if (err) goto out; dentry = fhp->fh_dentry; dirp = dentry->d_inode; err = nfserr_notdir; if(!dirp->i_op || !dirp->i_op->lookup) goto out; /* * Check whether the response file handle has been verified yet. * If it has, the parent directory should already be locked. */ if (!resfhp->fh_dverified) { dchild = lookup_dentry(fname, dget(dentry), 0); err = PTR_ERR(dchild); if (IS_ERR(dchild)) goto out_nfserr; fh_compose(resfhp, fhp->fh_export, dchild); /* Lock the parent and check for errors ... */ err = fh_lock_parent(fhp, dchild); if (err) goto out; } else { dchild = resfhp->fh_dentry; if (!fhp->fh_locked) printk(KERN_ERR "nfsd_create: parent %s/%s not locked!\n", dentry->d_parent->d_name.name, dentry->d_name.name); } /* * Make sure the child dentry is still negative ... */ err = nfserr_exist; if (dchild->d_inode) { printk(KERN_WARNING "nfsd_create: dentry %s/%s not negative!\n", dentry->d_name.name, dchild->d_name.name); goto out; } /* * Get the dir op function pointer. */ err = nfserr_perm; switch (type) { case S_IFREG: opfunc = (nfsd_dirop_t) dirp->i_op->create; break; case S_IFDIR: opfunc = (nfsd_dirop_t) dirp->i_op->mkdir; break; case S_IFCHR: case S_IFBLK: /* The client is _NOT_ required to do security enforcement */ if(!capable(CAP_SYS_ADMIN)) { err = -EPERM; goto out; } case S_IFIFO: case S_IFSOCK: opfunc = dirp->i_op->mknod; break; } if (!opfunc) goto out; if (!(iap->ia_valid & ATTR_MODE)) iap->ia_mode = 0; /* * Call the dir op function to create the object. */ DQUOT_INIT(dirp); err = opfunc(dirp, dchild, iap->ia_mode, rdev); DQUOT_DROP(dirp); if (err < 0) goto out_nfserr; if (EX_ISSYNC(fhp->fh_export)) write_inode_now(dirp); /* * Update the file handle to get the new inode info. */ fh_update(resfhp); /* Set file attributes. Mode has already been set and * setting uid/gid works only for root. Irix appears to * send along the gid when it tries to implement setgid * directories via NFS. */ err = 0; if ((iap->ia_valid &= (ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) err = nfsd_setattr(rqstp, resfhp, iap); out: return err; out_nfserr: err = nfserrno(-err); goto out; }
/* * Write data to a file. * The stable flag requests synchronous writes. * N.B. After this call fhp needs an fh_put */ int nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, char *buf, unsigned long cnt, int stable) { struct svc_export *exp; struct file file; struct dentry *dentry; struct inode *inode; mm_segment_t oldfs; int err = 0; #ifdef CONFIG_QUOTA uid_t saved_euid; #endif if (!cnt) goto out; err = nfsd_open(rqstp, fhp, S_IFREG, OPEN_WRITE, &file); if (err) goto out; err = nfserr_perm; if (!file.f_op->write) goto out_close; dentry = file.f_dentry; inode = dentry->d_inode; exp = fhp->fh_export; /* * Request sync writes if * - the sync export option has been set, or * - the client requested O_SYNC behavior (NFSv3 feature). * When gathered writes have been configured for this volume, * flushing the data to disk is handled separately below. */ if ((stable || (stable = EX_ISSYNC(exp))) && !EX_WGATHER(exp)) file.f_flags |= O_SYNC; fh_lock(fhp); /* lock inode */ file.f_pos = offset; /* set write offset */ /* Write the data. */ oldfs = get_fs(); set_fs(KERNEL_DS); #ifdef CONFIG_QUOTA /* This is for disk quota. */ saved_euid = current->euid; current->euid = current->fsuid; err = file.f_op->write(&file, buf, cnt, &file.f_pos); current->euid = saved_euid; #else err = file.f_op->write(&file, buf, cnt, &file.f_pos); #endif set_fs(oldfs); /* clear setuid/setgid flag after write */ if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) { struct iattr ia; kernel_cap_t saved_cap; ia.ia_valid = ATTR_MODE; ia.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID); if (current->fsuid != 0) { saved_cap = current->cap_effective; cap_clear(current->cap_effective); } notify_change(dentry, &ia); if (current->fsuid != 0) current->cap_effective = saved_cap; } fh_unlock(fhp); /* unlock inode */ if (err >= 0 && stable) { static unsigned long last_ino = 0; static kdev_t last_dev = NODEV; /* * Gathered writes: If another process is currently * writing to the file, there's a high chance * this is another nfsd (triggered by a bulk write * from a client's biod). Rather than syncing the * file with each write request, we sleep for 10 msec. * * I don't know if this roughly approximates * C. Juszak's idea of gathered writes, but it's a * nice and simple solution (IMHO), and it seems to * work:-) */ if (EX_WGATHER(exp) && (inode->i_writecount > 1 || (last_ino == inode->i_ino && last_dev == inode->i_dev))) { #if 0 interruptible_sleep_on_timeout(&inode->i_wait, 10 * HZ / 1000); #else dprintk("nfsd: write defer %d\n", current->pid); schedule_timeout((HZ+99)/100); dprintk("nfsd: write resume %d\n", current->pid); #endif } if (inode->i_state & I_DIRTY) { dprintk("nfsd: write sync %d\n", current->pid); nfsd_sync(inode, &file); write_inode_now(inode); } wake_up(&inode->i_wait); last_ino = inode->i_ino; last_dev = inode->i_dev; } dprintk("nfsd: write complete\n"); if (err >= 0) err = 0; else err = nfserrno(-err); out_close: nfsd_close(&file); out: return err; }