11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/file.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Changes Copyright (C) 1994 by Florian La Roche 71da177e4SLinus Torvalds * - Do not copy data too often around in the kernel. 81da177e4SLinus Torvalds * - In nfs_file_read the return value of kmalloc wasn't checked. 91da177e4SLinus Torvalds * - Put in a better version of read look-ahead buffering. Original idea 101da177e4SLinus Torvalds * and implementation by Wai S Kok elekokws@ee.nus.sg. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Expire cache on write to a file by Wai S Kok (Oct 1994). 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Total rewrite of read side for new NFS buffer cache.. Linus. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * nfs regular file handling functions 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/time.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/errno.h> 221da177e4SLinus Torvalds #include <linux/fcntl.h> 231da177e4SLinus Torvalds #include <linux/stat.h> 241da177e4SLinus Torvalds #include <linux/nfs_fs.h> 251da177e4SLinus Torvalds #include <linux/nfs_mount.h> 261da177e4SLinus Torvalds #include <linux/mm.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/pagemap.h> 291da177e4SLinus Torvalds #include <linux/smp_lock.h> 30e8edc6e0SAlexey Dobriyan #include <linux/aio.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <asm/uaccess.h> 331da177e4SLinus Torvalds #include <asm/system.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include "delegation.h" 3694387fb1STrond Myklebust #include "internal.h" 3791d5b470SChuck Lever #include "iostat.h" 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static int nfs_file_open(struct inode *, struct file *); 421da177e4SLinus Torvalds static int nfs_file_release(struct inode *, struct file *); 43980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin); 441da177e4SLinus Torvalds static int nfs_file_mmap(struct file *, struct vm_area_struct *); 45f0930fffSJens Axboe static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos, 46f0930fffSJens Axboe struct pipe_inode_info *pipe, 47f0930fffSJens Axboe size_t count, unsigned int flags); 48027445c3SBadari Pulavarty static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov, 49027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 50027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov, 51027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 5275e1fcc0SMiklos Szeredi static int nfs_file_flush(struct file *, fl_owner_t id); 531da177e4SLinus Torvalds static int nfs_fsync(struct file *, struct dentry *dentry, int datasync); 541da177e4SLinus Torvalds static int nfs_check_flags(int flags); 551da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl); 561da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl); 57370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl); 581da177e4SLinus Torvalds 5994387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops; 6094387fb1STrond Myklebust 614b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = { 62980802e3STrond Myklebust .llseek = nfs_file_llseek, 631da177e4SLinus Torvalds .read = do_sync_read, 641da177e4SLinus Torvalds .write = do_sync_write, 651da177e4SLinus Torvalds .aio_read = nfs_file_read, 661da177e4SLinus Torvalds .aio_write = nfs_file_write, 671da177e4SLinus Torvalds .mmap = nfs_file_mmap, 681da177e4SLinus Torvalds .open = nfs_file_open, 691da177e4SLinus Torvalds .flush = nfs_file_flush, 701da177e4SLinus Torvalds .release = nfs_file_release, 711da177e4SLinus Torvalds .fsync = nfs_fsync, 721da177e4SLinus Torvalds .lock = nfs_lock, 731da177e4SLinus Torvalds .flock = nfs_flock, 74f0930fffSJens Axboe .splice_read = nfs_file_splice_read, 751da177e4SLinus Torvalds .check_flags = nfs_check_flags, 76370f6599SJ. Bruce Fields .setlease = nfs_setlease, 771da177e4SLinus Torvalds }; 781da177e4SLinus Torvalds 7992e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = { 801da177e4SLinus Torvalds .permission = nfs_permission, 811da177e4SLinus Torvalds .getattr = nfs_getattr, 821da177e4SLinus Torvalds .setattr = nfs_setattr, 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 85b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 8692e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = { 87b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 88b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 89b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 90b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 91b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 92b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 93b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 94b7fa0554SAndreas Gruenbacher }; 95b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_v3 */ 96b7fa0554SAndreas Gruenbacher 971da177e4SLinus Torvalds /* Hack for future NFS swap support */ 981da177e4SLinus Torvalds #ifndef IS_SWAPFILE 991da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 1001da177e4SLinus Torvalds #endif 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static int nfs_check_flags(int flags) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 1051da177e4SLinus Torvalds return -EINVAL; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds return 0; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* 1111da177e4SLinus Torvalds * Open file 1121da177e4SLinus Torvalds */ 1131da177e4SLinus Torvalds static int 1141da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds int res; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 1191da177e4SLinus Torvalds if (res) 1201da177e4SLinus Torvalds return res; 1211da177e4SLinus Torvalds 12291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1231da177e4SLinus Torvalds lock_kernel(); 1241f163415SDavid Howells res = NFS_PROTO(inode)->file_open(inode, filp); 1251da177e4SLinus Torvalds unlock_kernel(); 1261da177e4SLinus Torvalds return res; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static int 1301da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds /* Ensure that dirty pages are flushed out with the right creds */ 1331da177e4SLinus Torvalds if (filp->f_mode & FMODE_WRITE) 134a49c3c77STrond Myklebust nfs_wb_all(filp->f_path.dentry->d_inode); 13591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 1361da177e4SLinus Torvalds return NFS_PROTO(inode)->file_release(inode, filp); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 139980802e3STrond Myklebust /** 140980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 141980802e3STrond Myklebust * @inode - pointer to inode struct 142980802e3STrond Myklebust * @file - pointer to struct file 143980802e3STrond Myklebust * 144980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 145980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 146980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 147980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 148980802e3STrond Myklebust * shouldn't trust the cache). 149980802e3STrond Myklebust */ 150980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 151980802e3STrond Myklebust { 152980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 153980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 154980802e3STrond Myklebust 155980802e3STrond Myklebust if (server->flags & NFS_MOUNT_NOAC) 156980802e3STrond Myklebust goto force_reval; 157980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 158980802e3STrond Myklebust goto force_reval; 159980802e3STrond Myklebust if (nfsi->npages != 0) 160980802e3STrond Myklebust return 0; 16155296809SChuck Lever if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode)) 162fe51beecSTrond Myklebust return 0; 163980802e3STrond Myklebust force_reval: 164980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 165980802e3STrond Myklebust } 166980802e3STrond Myklebust 167980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 168980802e3STrond Myklebust { 169980802e3STrond Myklebust /* origin == SEEK_END => we must revalidate the cached file length */ 170aec5e175SJosef 'Jeff' Sipek if (origin == SEEK_END) { 171980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 172980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 173980802e3STrond Myklebust if (retval < 0) 174980802e3STrond Myklebust return (loff_t)retval; 175980802e3STrond Myklebust } 176980802e3STrond Myklebust return remote_llseek(filp, offset, origin); 177980802e3STrond Myklebust } 178980802e3STrond Myklebust 1791da177e4SLinus Torvalds /* 1807b159fc1STrond Myklebust * Helper for nfs_file_flush() and nfs_fsync() 1817b159fc1STrond Myklebust * 1827b159fc1STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 1837b159fc1STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 1847b159fc1STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 1857b159fc1STrond Myklebust * This is because the former is used to notify the _next_ call to 1867b159fc1STrond Myklebust * nfs_file_write() that a write error occured, and hence cause it to 1877b159fc1STrond Myklebust * fall back to doing a synchronous write. 1887b159fc1STrond Myklebust */ 1897b159fc1STrond Myklebust static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode) 1907b159fc1STrond Myklebust { 1917b159fc1STrond Myklebust int have_error, status; 1927b159fc1STrond Myklebust int ret = 0; 1937b159fc1STrond Myklebust 1947b159fc1STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1957b159fc1STrond Myklebust status = nfs_wb_all(inode); 1967b159fc1STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1977b159fc1STrond Myklebust if (have_error) 1987b159fc1STrond Myklebust ret = xchg(&ctx->error, 0); 1997b159fc1STrond Myklebust if (!ret) 2007b159fc1STrond Myklebust ret = status; 2017b159fc1STrond Myklebust return ret; 2027b159fc1STrond Myklebust } 2037b159fc1STrond Myklebust 2047b159fc1STrond Myklebust /* 2051da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 2061da177e4SLinus Torvalds * 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds static int 20975e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 2101da177e4SLinus Torvalds { 211cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 21201cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 2131da177e4SLinus Torvalds int status; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 2181da177e4SLinus Torvalds return 0; 21991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 2207b159fc1STrond Myklebust 2211da177e4SLinus Torvalds /* Ensure that data+attribute caches are up to date after close() */ 2227b159fc1STrond Myklebust status = nfs_do_fsync(ctx, inode); 2233338c143STrond Myklebust if (!status) 2243338c143STrond Myklebust nfs_revalidate_inode(NFS_SERVER(inode), inode); 2251da177e4SLinus Torvalds return status; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static ssize_t 229027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 230027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 2311da177e4SLinus Torvalds { 23201cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 2331da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 2341da177e4SLinus Torvalds ssize_t result; 235027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO 2381da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 239027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 2401da177e4SLinus Torvalds #endif 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n", 2431da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2441da177e4SLinus Torvalds (unsigned long) count, (unsigned long) pos); 2451da177e4SLinus Torvalds 24644b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 24791d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count); 2481da177e4SLinus Torvalds if (!result) 249027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2501da177e4SLinus Torvalds return result; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds static ssize_t 254f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 255f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 256f0930fffSJens Axboe unsigned int flags) 2571da177e4SLinus Torvalds { 25801cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 2591da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2601da177e4SLinus Torvalds ssize_t res; 2611da177e4SLinus Torvalds 262f0930fffSJens Axboe dfprintk(VFS, "nfs: splice_read(%s/%s, %lu@%Lu)\n", 2631da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2641da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2651da177e4SLinus Torvalds 26644b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 2671da177e4SLinus Torvalds if (!res) 268f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 2691da177e4SLinus Torvalds return res; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds static int 2731da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2741da177e4SLinus Torvalds { 27501cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 2761da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2771da177e4SLinus Torvalds int status; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds dfprintk(VFS, "nfs: mmap(%s/%s)\n", 2801da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2811da177e4SLinus Torvalds 28244b11874STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 28394387fb1STrond Myklebust if (!status) { 28494387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 28594387fb1STrond Myklebust vma->vm_flags |= VM_CAN_NONLINEAR; 28694387fb1STrond Myklebust file_accessed(file); 28794387fb1STrond Myklebust } 2881da177e4SLinus Torvalds return status; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 2921da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2931da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2941da177e4SLinus Torvalds * whether any write errors occurred for this process. 2951da177e4SLinus Torvalds */ 2961da177e4SLinus Torvalds static int 2971da177e4SLinus Torvalds nfs_fsync(struct file *file, struct dentry *dentry, int datasync) 2981da177e4SLinus Torvalds { 299cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 3001da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 3031da177e4SLinus Torvalds 30491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 3057b159fc1STrond Myklebust return nfs_do_fsync(ctx, inode); 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* 3094899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3104899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3114899f9c8SNick Piggin * data from user space. 3121da177e4SLinus Torvalds * 3131da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3141da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3151da177e4SLinus Torvalds */ 3164899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3174899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3184899f9c8SNick Piggin struct page **pagep, void **fsdata) 3191da177e4SLinus Torvalds { 3204899f9c8SNick Piggin int ret; 3214899f9c8SNick Piggin pgoff_t index; 3224899f9c8SNick Piggin struct page *page; 3234899f9c8SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 3244899f9c8SNick Piggin 3254899f9c8SNick Piggin page = __grab_cache_page(mapping, index); 3264899f9c8SNick Piggin if (!page) 3274899f9c8SNick Piggin return -ENOMEM; 3284899f9c8SNick Piggin *pagep = page; 3294899f9c8SNick Piggin 3304899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3314899f9c8SNick Piggin if (ret) { 3324899f9c8SNick Piggin unlock_page(page); 3334899f9c8SNick Piggin page_cache_release(page); 3344899f9c8SNick Piggin } 3354899f9c8SNick Piggin return ret; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3384899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3394899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3404899f9c8SNick Piggin struct page *page, void *fsdata) 3411da177e4SLinus Torvalds { 3424899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 3434899f9c8SNick Piggin int status; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds lock_kernel(); 3464899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 3471da177e4SLinus Torvalds unlock_kernel(); 3484899f9c8SNick Piggin 3494899f9c8SNick Piggin unlock_page(page); 3504899f9c8SNick Piggin page_cache_release(page); 3514899f9c8SNick Piggin 3524899f9c8SNick Piggin return status < 0 ? status : copied; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3552ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset) 356cd52ed35STrond Myklebust { 3571c75950bSTrond Myklebust if (offset != 0) 3581c75950bSTrond Myklebust return; 359d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 3601b3b4a1aSTrond Myklebust nfs_wb_page_cancel(page->mapping->host, page); 361cd52ed35STrond Myklebust } 362cd52ed35STrond Myklebust 363cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 364cd52ed35STrond Myklebust { 365e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 366ddeff520SNikita Danilov return 0; 367e3db7691STrond Myklebust } 368e3db7691STrond Myklebust 369e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 370e3db7691STrond Myklebust { 371e3db7691STrond Myklebust return nfs_wb_page(page->mapping->host, page); 372cd52ed35STrond Myklebust } 373cd52ed35STrond Myklebust 374f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 3751da177e4SLinus Torvalds .readpage = nfs_readpage, 3761da177e4SLinus Torvalds .readpages = nfs_readpages, 3779cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 3781da177e4SLinus Torvalds .writepage = nfs_writepage, 3791da177e4SLinus Torvalds .writepages = nfs_writepages, 3804899f9c8SNick Piggin .write_begin = nfs_write_begin, 3814899f9c8SNick Piggin .write_end = nfs_write_end, 382cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 383cd52ed35STrond Myklebust .releasepage = nfs_release_page, 3841da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO 3851da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 3861da177e4SLinus Torvalds #endif 387e3db7691STrond Myklebust .launder_page = nfs_launder_page, 3881da177e4SLinus Torvalds }; 3891da177e4SLinus Torvalds 39094387fb1STrond Myklebust static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) 39194387fb1STrond Myklebust { 39294387fb1STrond Myklebust struct file *filp = vma->vm_file; 39394387fb1STrond Myklebust unsigned pagelen; 39494387fb1STrond Myklebust int ret = -EINVAL; 3954899f9c8SNick Piggin struct address_space *mapping; 39694387fb1STrond Myklebust 39794387fb1STrond Myklebust lock_page(page); 3984899f9c8SNick Piggin mapping = page->mapping; 399*8b1f9ee5STrond Myklebust if (mapping != vma->vm_file->f_path.dentry->d_inode->i_mapping) 400*8b1f9ee5STrond Myklebust goto out_unlock; 401*8b1f9ee5STrond Myklebust 402*8b1f9ee5STrond Myklebust ret = 0; 4034899f9c8SNick Piggin pagelen = nfs_page_length(page); 404*8b1f9ee5STrond Myklebust if (pagelen == 0) 405*8b1f9ee5STrond Myklebust goto out_unlock; 406*8b1f9ee5STrond Myklebust 407*8b1f9ee5STrond Myklebust ret = nfs_flush_incompatible(filp, page); 408*8b1f9ee5STrond Myklebust if (ret != 0) 409*8b1f9ee5STrond Myklebust goto out_unlock; 410*8b1f9ee5STrond Myklebust 411*8b1f9ee5STrond Myklebust ret = nfs_updatepage(filp, page, 0, pagelen); 412*8b1f9ee5STrond Myklebust if (ret == 0) 413*8b1f9ee5STrond Myklebust ret = pagelen; 414*8b1f9ee5STrond Myklebust out_unlock: 4154899f9c8SNick Piggin unlock_page(page); 41694387fb1STrond Myklebust return ret; 41794387fb1STrond Myklebust } 41894387fb1STrond Myklebust 41994387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = { 42094387fb1STrond Myklebust .fault = filemap_fault, 42194387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 42294387fb1STrond Myklebust }; 42394387fb1STrond Myklebust 4247b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 4257b159fc1STrond Myklebust { 4267b159fc1STrond Myklebust struct nfs_open_context *ctx; 4277b159fc1STrond Myklebust 4287b159fc1STrond Myklebust if (IS_SYNC(inode) || (filp->f_flags & O_SYNC)) 4297b159fc1STrond Myklebust return 1; 430cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 4317b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 4327b159fc1STrond Myklebust return 1; 4337b159fc1STrond Myklebust return 0; 4347b159fc1STrond Myklebust } 4357b159fc1STrond Myklebust 436027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 437027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 4381da177e4SLinus Torvalds { 43901cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 4401da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 4411da177e4SLinus Torvalds ssize_t result; 442027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO 4451da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 446027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 4471da177e4SLinus Torvalds #endif 4481da177e4SLinus Torvalds 449027445c3SBadari Pulavarty dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n", 4501da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 451027445c3SBadari Pulavarty inode->i_ino, (unsigned long) count, (long long) pos); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds result = -EBUSY; 4541da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 4551da177e4SLinus Torvalds goto out_swapfile; 4567d52e862STrond Myklebust /* 4577d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 4587d52e862STrond Myklebust */ 4597d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 4607d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 4611da177e4SLinus Torvalds if (result) 4621da177e4SLinus Torvalds goto out; 463fe51beecSTrond Myklebust } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds result = count; 4661da177e4SLinus Torvalds if (!count) 4671da177e4SLinus Torvalds goto out; 4681da177e4SLinus Torvalds 46991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count); 470027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 471200baa21STrond Myklebust /* Return error values for O_SYNC and IS_SYNC() */ 4727b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 473cd3758e3STrond Myklebust int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode); 474200baa21STrond Myklebust if (err < 0) 475200baa21STrond Myklebust result = err; 476200baa21STrond Myklebust } 4771da177e4SLinus Torvalds out: 4781da177e4SLinus Torvalds return result; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds out_swapfile: 4811da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 4821da177e4SLinus Torvalds goto out; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 4881da177e4SLinus Torvalds int status = 0; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds lock_kernel(); 491039c4d7aSTrond Myklebust /* Try local locking first */ 4926d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 4936d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 4946d34ac19SJ. Bruce Fields /* found a conflict */ 495039c4d7aSTrond Myklebust goto out; 4961da177e4SLinus Torvalds } 497039c4d7aSTrond Myklebust 498039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 499039c4d7aSTrond Myklebust goto out_noconflict; 500039c4d7aSTrond Myklebust 501039c4d7aSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) 502039c4d7aSTrond Myklebust goto out_noconflict; 503039c4d7aSTrond Myklebust 504039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 505039c4d7aSTrond Myklebust out: 5061da177e4SLinus Torvalds unlock_kernel(); 5071da177e4SLinus Torvalds return status; 508039c4d7aSTrond Myklebust out_noconflict: 509039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 510039c4d7aSTrond Myklebust goto out; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds int res = 0; 5161da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 5171da177e4SLinus Torvalds case FL_POSIX: 5181da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 5191da177e4SLinus Torvalds break; 5201da177e4SLinus Torvalds case FL_FLOCK: 5211da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 5221da177e4SLinus Torvalds break; 5231da177e4SLinus Torvalds default: 5241da177e4SLinus Torvalds BUG(); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds if (res < 0) 52746bae1a9SNeil Brown dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager" 52846bae1a9SNeil Brown " - error %d!\n", 52946bae1a9SNeil Brown __FUNCTION__, res); 5301da177e4SLinus Torvalds return res; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 5361da177e4SLinus Torvalds int status; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* 5391da177e4SLinus Torvalds * Flush all pending writes before doing anything 5401da177e4SLinus Torvalds * with locks.. 5411da177e4SLinus Torvalds */ 54229884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* NOTE: special case 5451da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 5461da177e4SLinus Torvalds * still need to complete the unlock. 5471da177e4SLinus Torvalds */ 5481da177e4SLinus Torvalds lock_kernel(); 5491da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 5501da177e4SLinus Torvalds if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 5511da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 5521da177e4SLinus Torvalds else 5531da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 5541da177e4SLinus Torvalds unlock_kernel(); 5551da177e4SLinus Torvalds return status; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl) 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 5611da177e4SLinus Torvalds int status; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds /* 5641da177e4SLinus Torvalds * Flush all pending writes before doing anything 5651da177e4SLinus Torvalds * with locks.. 5661da177e4SLinus Torvalds */ 56729884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 56829884df0STrond Myklebust if (status != 0) 5691da177e4SLinus Torvalds goto out; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds lock_kernel(); 5721da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 5731da177e4SLinus Torvalds if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) { 5741da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 5751da177e4SLinus Torvalds /* If we were signalled we still need to ensure that 5761da177e4SLinus Torvalds * we clean up any state on the server. We therefore 5771da177e4SLinus Torvalds * record the lock call as having succeeded in order to 5781da177e4SLinus Torvalds * ensure that locks_remove_posix() cleans it out when 5791da177e4SLinus Torvalds * the process exits. 5801da177e4SLinus Torvalds */ 5811da177e4SLinus Torvalds if (status == -EINTR || status == -ERESTARTSYS) 5821da177e4SLinus Torvalds do_vfs_lock(filp, fl); 5831da177e4SLinus Torvalds } else 5841da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 5851da177e4SLinus Torvalds unlock_kernel(); 5861da177e4SLinus Torvalds if (status < 0) 5871da177e4SLinus Torvalds goto out; 5881da177e4SLinus Torvalds /* 5891da177e4SLinus Torvalds * Make sure we clear the cache whenever we try to get the lock. 5901da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 5911da177e4SLinus Torvalds */ 59229884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 5931da177e4SLinus Torvalds nfs_zap_caches(inode); 5941da177e4SLinus Torvalds out: 5951da177e4SLinus Torvalds return status; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* 5991da177e4SLinus Torvalds * Lock a (portion of) a file 6001da177e4SLinus Torvalds */ 6011da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds struct inode * inode = filp->f_mapping->host; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n", 6061da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino, 6071da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 6081da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 60991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* No mandatory locks over NFS */ 612dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 6131da177e4SLinus Torvalds return -ENOLCK; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds if (IS_GETLK(cmd)) 6161da177e4SLinus Torvalds return do_getlk(filp, cmd, fl); 6171da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 6181da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 6191da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /* 6231da177e4SLinus Torvalds * Lock a (portion of) a file 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 6261da177e4SLinus Torvalds { 6271da177e4SLinus Torvalds dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n", 62801cce933SJosef "Jeff" Sipek filp->f_path.dentry->d_inode->i_sb->s_id, 62901cce933SJosef "Jeff" Sipek filp->f_path.dentry->d_inode->i_ino, 6301da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /* 6331da177e4SLinus Torvalds * No BSD flocks over NFS allowed. 6341da177e4SLinus Torvalds * Note: we could try to fake a POSIX lock request here by 6351da177e4SLinus Torvalds * using ((u32) filp | 0x80000000) or some such as the pid. 6361da177e4SLinus Torvalds * Not sure whether that would be unique, though, or whether 6371da177e4SLinus Torvalds * that would break in other places. 6381da177e4SLinus Torvalds */ 6391da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 6401da177e4SLinus Torvalds return -ENOLCK; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 6431da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 6441da177e4SLinus Torvalds fl->fl_start = 0; 6451da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 6481da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 6491da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 6501da177e4SLinus Torvalds } 651370f6599SJ. Bruce Fields 652370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 653370f6599SJ. Bruce Fields { 654370f6599SJ. Bruce Fields /* 655370f6599SJ. Bruce Fields * There is no protocol support for leases, so we have no way 656370f6599SJ. Bruce Fields * to implement them correctly in the face of opens by other 657370f6599SJ. Bruce Fields * clients. 658370f6599SJ. Bruce Fields */ 659370f6599SJ. Bruce Fields return -EINVAL; 660370f6599SJ. Bruce Fields } 661