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); 5354917786SChuck Lever static int nfs_file_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, 67240ee831SBryan Wu #ifdef CONFIG_MMU 681da177e4SLinus Torvalds .mmap = nfs_file_mmap, 69240ee831SBryan Wu #else 70240ee831SBryan Wu .mmap = generic_file_mmap, 71240ee831SBryan Wu #endif 721da177e4SLinus Torvalds .open = nfs_file_open, 731da177e4SLinus Torvalds .flush = nfs_file_flush, 741da177e4SLinus Torvalds .release = nfs_file_release, 7554917786SChuck Lever .fsync = nfs_file_fsync, 761da177e4SLinus Torvalds .lock = nfs_lock, 771da177e4SLinus Torvalds .flock = nfs_flock, 78f0930fffSJens Axboe .splice_read = nfs_file_splice_read, 791da177e4SLinus Torvalds .check_flags = nfs_check_flags, 80370f6599SJ. Bruce Fields .setlease = nfs_setlease, 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 8392e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = { 841da177e4SLinus Torvalds .permission = nfs_permission, 851da177e4SLinus Torvalds .getattr = nfs_getattr, 861da177e4SLinus Torvalds .setattr = nfs_setattr, 871da177e4SLinus Torvalds }; 881da177e4SLinus Torvalds 89b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 9092e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = { 91b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 92b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 93b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 94b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 95b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 96b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 97b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 98b7fa0554SAndreas Gruenbacher }; 99b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_v3 */ 100b7fa0554SAndreas Gruenbacher 1011da177e4SLinus Torvalds /* Hack for future NFS swap support */ 1021da177e4SLinus Torvalds #ifndef IS_SWAPFILE 1031da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static int nfs_check_flags(int flags) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 1091da177e4SLinus Torvalds return -EINVAL; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds return 0; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* 1151da177e4SLinus Torvalds * Open file 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds static int 1181da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int res; 1211da177e4SLinus Torvalds 1226da24bc9SChuck Lever dprintk("NFS: open file(%s/%s)\n", 123cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 124cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 125cc0dd2d1SChuck Lever 1261da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 1271da177e4SLinus Torvalds if (res) 1281da177e4SLinus Torvalds return res; 1291da177e4SLinus Torvalds 13091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 13146cb650cSTrond Myklebust res = nfs_open(inode, filp); 1321da177e4SLinus Torvalds return res; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static int 1361da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 1371da177e4SLinus Torvalds { 1386da24bc9SChuck Lever struct dentry *dentry = filp->f_path.dentry; 1396da24bc9SChuck Lever 1406da24bc9SChuck Lever dprintk("NFS: release(%s/%s)\n", 1416da24bc9SChuck Lever dentry->d_parent->d_name.name, 1426da24bc9SChuck Lever dentry->d_name.name); 1436da24bc9SChuck Lever 1441da177e4SLinus Torvalds /* Ensure that dirty pages are flushed out with the right creds */ 1451da177e4SLinus Torvalds if (filp->f_mode & FMODE_WRITE) 1466da24bc9SChuck Lever nfs_wb_all(dentry->d_inode); 14791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 14846cb650cSTrond Myklebust return nfs_release(inode, filp); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 151980802e3STrond Myklebust /** 152980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 153980802e3STrond Myklebust * @inode - pointer to inode struct 154980802e3STrond Myklebust * @file - pointer to struct file 155980802e3STrond Myklebust * 156980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 157980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 158980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 159980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 160980802e3STrond Myklebust * shouldn't trust the cache). 161980802e3STrond Myklebust */ 162980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 163980802e3STrond Myklebust { 164980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 165980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 166980802e3STrond Myklebust 167980802e3STrond Myklebust if (server->flags & NFS_MOUNT_NOAC) 168980802e3STrond Myklebust goto force_reval; 169980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 170980802e3STrond Myklebust goto force_reval; 171980802e3STrond Myklebust if (nfsi->npages != 0) 172980802e3STrond Myklebust return 0; 17355296809SChuck Lever if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode)) 174fe51beecSTrond Myklebust return 0; 175980802e3STrond Myklebust force_reval: 176980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 177980802e3STrond Myklebust } 178980802e3STrond Myklebust 179980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 180980802e3STrond Myklebust { 1819465efc9SAndi Kleen loff_t loff; 182e89e896dSTrond Myklebust 1836da24bc9SChuck Lever dprintk("NFS: llseek file(%s/%s, %lld, %d)\n", 184b84e06c5SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 185b84e06c5SChuck Lever filp->f_path.dentry->d_name.name, 186b84e06c5SChuck Lever offset, origin); 187b84e06c5SChuck Lever 188980802e3STrond Myklebust /* origin == SEEK_END => we must revalidate the cached file length */ 189aec5e175SJosef 'Jeff' Sipek if (origin == SEEK_END) { 190980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 191d5e66348STrond Myklebust 192980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 193980802e3STrond Myklebust if (retval < 0) 194980802e3STrond Myklebust return (loff_t)retval; 195d5e66348STrond Myklebust 196d5e66348STrond Myklebust spin_lock(&inode->i_lock); 1979465efc9SAndi Kleen loff = generic_file_llseek_unlocked(filp, offset, origin); 198d5e66348STrond Myklebust spin_unlock(&inode->i_lock); 199d5e66348STrond Myklebust } else 200d5e66348STrond Myklebust loff = generic_file_llseek_unlocked(filp, offset, origin); 2019465efc9SAndi Kleen return loff; 202980802e3STrond Myklebust } 203980802e3STrond Myklebust 2041da177e4SLinus Torvalds /* 20554917786SChuck Lever * Helper for nfs_file_flush() and nfs_file_fsync() 2067b159fc1STrond Myklebust * 2077b159fc1STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 2087b159fc1STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 2097b159fc1STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 2107b159fc1STrond Myklebust * This is because the former is used to notify the _next_ call to 2117b159fc1STrond Myklebust * nfs_file_write() that a write error occured, and hence cause it to 2127b159fc1STrond Myklebust * fall back to doing a synchronous write. 2137b159fc1STrond Myklebust */ 2147b159fc1STrond Myklebust static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode) 2157b159fc1STrond Myklebust { 2167b159fc1STrond Myklebust int have_error, status; 2177b159fc1STrond Myklebust int ret = 0; 2187b159fc1STrond Myklebust 2197b159fc1STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 2207b159fc1STrond Myklebust status = nfs_wb_all(inode); 2217b159fc1STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 2227b159fc1STrond Myklebust if (have_error) 2237b159fc1STrond Myklebust ret = xchg(&ctx->error, 0); 2247b159fc1STrond Myklebust if (!ret) 2257b159fc1STrond Myklebust ret = status; 2267b159fc1STrond Myklebust return ret; 2277b159fc1STrond Myklebust } 2287b159fc1STrond Myklebust 2297b159fc1STrond Myklebust /* 2301da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 2311da177e4SLinus Torvalds */ 2321da177e4SLinus Torvalds static int 23375e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 2341da177e4SLinus Torvalds { 235cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 2366da24bc9SChuck Lever struct dentry *dentry = file->f_path.dentry; 2376da24bc9SChuck Lever struct inode *inode = dentry->d_inode; 2381da177e4SLinus Torvalds int status; 2391da177e4SLinus Torvalds 2406da24bc9SChuck Lever dprintk("NFS: flush(%s/%s)\n", 2416da24bc9SChuck Lever dentry->d_parent->d_name.name, 2426da24bc9SChuck Lever dentry->d_name.name); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 2451da177e4SLinus Torvalds return 0; 24691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 2477b159fc1STrond Myklebust 2481da177e4SLinus Torvalds /* Ensure that data+attribute caches are up to date after close() */ 2497b159fc1STrond Myklebust status = nfs_do_fsync(ctx, inode); 2503338c143STrond Myklebust if (!status) 2513338c143STrond Myklebust nfs_revalidate_inode(NFS_SERVER(inode), inode); 2521da177e4SLinus Torvalds return status; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static ssize_t 256027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 257027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 2581da177e4SLinus Torvalds { 25901cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 2601da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 2611da177e4SLinus Torvalds ssize_t result; 262027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 265027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 2661da177e4SLinus Torvalds 2676da24bc9SChuck Lever dprintk("NFS: read(%s/%s, %lu@%lu)\n", 2681da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2691da177e4SLinus Torvalds (unsigned long) count, (unsigned long) pos); 2701da177e4SLinus Torvalds 27144b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 27291d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count); 2731da177e4SLinus Torvalds if (!result) 274027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2751da177e4SLinus Torvalds return result; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds static ssize_t 279f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 280f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 281f0930fffSJens Axboe unsigned int flags) 2821da177e4SLinus Torvalds { 28301cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 2841da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2851da177e4SLinus Torvalds ssize_t res; 2861da177e4SLinus Torvalds 2876da24bc9SChuck Lever dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n", 2881da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2891da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2901da177e4SLinus Torvalds 29144b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 2921da177e4SLinus Torvalds if (!res) 293f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 2941da177e4SLinus Torvalds return res; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds static int 2981da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2991da177e4SLinus Torvalds { 30001cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 3011da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3021da177e4SLinus Torvalds int status; 3031da177e4SLinus Torvalds 3046da24bc9SChuck Lever dprintk("NFS: mmap(%s/%s)\n", 3051da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 3061da177e4SLinus Torvalds 30744b11874STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 30894387fb1STrond Myklebust if (!status) { 30994387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 31094387fb1STrond Myklebust vma->vm_flags |= VM_CAN_NONLINEAR; 31194387fb1STrond Myklebust file_accessed(file); 31294387fb1STrond Myklebust } 3131da177e4SLinus Torvalds return status; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* 3171da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 3181da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 3191da177e4SLinus Torvalds * whether any write errors occurred for this process. 3201da177e4SLinus Torvalds */ 3211da177e4SLinus Torvalds static int 32254917786SChuck Lever nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync) 3231da177e4SLinus Torvalds { 324cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 3251da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3261da177e4SLinus Torvalds 3276da24bc9SChuck Lever dprintk("NFS: fsync file(%s/%s) datasync %d\n", 32854917786SChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 32954917786SChuck Lever datasync); 3301da177e4SLinus Torvalds 33191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 3327b159fc1STrond Myklebust return nfs_do_fsync(ctx, inode); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* 3364899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3374899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3384899f9c8SNick Piggin * data from user space. 3391da177e4SLinus Torvalds * 3401da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3411da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3421da177e4SLinus Torvalds */ 3434899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3444899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3454899f9c8SNick Piggin struct page **pagep, void **fsdata) 3461da177e4SLinus Torvalds { 3474899f9c8SNick Piggin int ret; 3484899f9c8SNick Piggin pgoff_t index; 3494899f9c8SNick Piggin struct page *page; 3504899f9c8SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 3514899f9c8SNick Piggin 352b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n", 353b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 354b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 355b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 356b7eaefaaSChuck Lever 35772cb77f4STrond Myklebust /* 35872cb77f4STrond Myklebust * Prevent starvation issues if someone is doing a consistency 35972cb77f4STrond Myklebust * sync-to-disk 36072cb77f4STrond Myklebust */ 36172cb77f4STrond Myklebust ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING, 36272cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 36372cb77f4STrond Myklebust if (ret) 36472cb77f4STrond Myklebust return ret; 36572cb77f4STrond Myklebust 36654566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3674899f9c8SNick Piggin if (!page) 3684899f9c8SNick Piggin return -ENOMEM; 3694899f9c8SNick Piggin *pagep = page; 3704899f9c8SNick Piggin 3714899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3724899f9c8SNick Piggin if (ret) { 3734899f9c8SNick Piggin unlock_page(page); 3744899f9c8SNick Piggin page_cache_release(page); 3754899f9c8SNick Piggin } 3764899f9c8SNick Piggin return ret; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3794899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3804899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3814899f9c8SNick Piggin struct page *page, void *fsdata) 3821da177e4SLinus Torvalds { 3834899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 3844899f9c8SNick Piggin int status; 3851da177e4SLinus Torvalds 386b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n", 387b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 388b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 389b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 390b7eaefaaSChuck Lever 391efc91ed0STrond Myklebust /* 392efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 393efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 394efc91ed0STrond Myklebust */ 395efc91ed0STrond Myklebust if (!PageUptodate(page)) { 396efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 397efc91ed0STrond Myklebust unsigned end = offset + len; 398efc91ed0STrond Myklebust 399efc91ed0STrond Myklebust if (pglen == 0) { 400efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 401efc91ed0STrond Myklebust end, PAGE_CACHE_SIZE); 402efc91ed0STrond Myklebust SetPageUptodate(page); 403efc91ed0STrond Myklebust } else if (end >= pglen) { 404efc91ed0STrond Myklebust zero_user_segment(page, end, PAGE_CACHE_SIZE); 405efc91ed0STrond Myklebust if (offset == 0) 406efc91ed0STrond Myklebust SetPageUptodate(page); 407efc91ed0STrond Myklebust } else 408efc91ed0STrond Myklebust zero_user_segment(page, pglen, PAGE_CACHE_SIZE); 409efc91ed0STrond Myklebust } 410efc91ed0STrond Myklebust 4114899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4124899f9c8SNick Piggin 4134899f9c8SNick Piggin unlock_page(page); 4144899f9c8SNick Piggin page_cache_release(page); 4154899f9c8SNick Piggin 4163d509e54SChuck Lever if (status < 0) 4173d509e54SChuck Lever return status; 4183d509e54SChuck Lever return copied; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4212ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset) 422cd52ed35STrond Myklebust { 423b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset); 424b7eaefaaSChuck Lever 4251c75950bSTrond Myklebust if (offset != 0) 4261c75950bSTrond Myklebust return; 427d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 4281b3b4a1aSTrond Myklebust nfs_wb_page_cancel(page->mapping->host, page); 429cd52ed35STrond Myklebust } 430cd52ed35STrond Myklebust 431cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 432cd52ed35STrond Myklebust { 433b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 434b7eaefaaSChuck Lever 435e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 436ddeff520SNikita Danilov return 0; 437e3db7691STrond Myklebust } 438e3db7691STrond Myklebust 439e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 440e3db7691STrond Myklebust { 441b7eaefaaSChuck Lever struct inode *inode = page->mapping->host; 442b7eaefaaSChuck Lever 443b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 444b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 445b7eaefaaSChuck Lever 446b7eaefaaSChuck Lever return nfs_wb_page(inode, page); 447cd52ed35STrond Myklebust } 448cd52ed35STrond Myklebust 449f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 4501da177e4SLinus Torvalds .readpage = nfs_readpage, 4511da177e4SLinus Torvalds .readpages = nfs_readpages, 4529cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 4531da177e4SLinus Torvalds .writepage = nfs_writepage, 4541da177e4SLinus Torvalds .writepages = nfs_writepages, 4554899f9c8SNick Piggin .write_begin = nfs_write_begin, 4564899f9c8SNick Piggin .write_end = nfs_write_end, 457cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 458cd52ed35STrond Myklebust .releasepage = nfs_release_page, 4591da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 460e3db7691STrond Myklebust .launder_page = nfs_launder_page, 4611da177e4SLinus Torvalds }; 4621da177e4SLinus Torvalds 46394387fb1STrond Myklebust static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) 46494387fb1STrond Myklebust { 46594387fb1STrond Myklebust struct file *filp = vma->vm_file; 466b7eaefaaSChuck Lever struct dentry *dentry = filp->f_path.dentry; 46794387fb1STrond Myklebust unsigned pagelen; 46894387fb1STrond Myklebust int ret = -EINVAL; 4694899f9c8SNick Piggin struct address_space *mapping; 47094387fb1STrond Myklebust 471b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n", 472b7eaefaaSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 473b7eaefaaSChuck Lever filp->f_mapping->host->i_ino, 474b7eaefaaSChuck Lever (long long)page_offset(page)); 475b7eaefaaSChuck Lever 47694387fb1STrond Myklebust lock_page(page); 4774899f9c8SNick Piggin mapping = page->mapping; 478b7eaefaaSChuck Lever if (mapping != dentry->d_inode->i_mapping) 4798b1f9ee5STrond Myklebust goto out_unlock; 4808b1f9ee5STrond Myklebust 4818b1f9ee5STrond Myklebust ret = 0; 4824899f9c8SNick Piggin pagelen = nfs_page_length(page); 4838b1f9ee5STrond Myklebust if (pagelen == 0) 4848b1f9ee5STrond Myklebust goto out_unlock; 4858b1f9ee5STrond Myklebust 4868b1f9ee5STrond Myklebust ret = nfs_flush_incompatible(filp, page); 4878b1f9ee5STrond Myklebust if (ret != 0) 4888b1f9ee5STrond Myklebust goto out_unlock; 4898b1f9ee5STrond Myklebust 4908b1f9ee5STrond Myklebust ret = nfs_updatepage(filp, page, 0, pagelen); 4918b1f9ee5STrond Myklebust if (ret == 0) 4928b1f9ee5STrond Myklebust ret = pagelen; 4938b1f9ee5STrond Myklebust out_unlock: 4944899f9c8SNick Piggin unlock_page(page); 49594387fb1STrond Myklebust return ret; 49694387fb1STrond Myklebust } 49794387fb1STrond Myklebust 49894387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = { 49994387fb1STrond Myklebust .fault = filemap_fault, 50094387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 50194387fb1STrond Myklebust }; 50294387fb1STrond Myklebust 5037b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 5047b159fc1STrond Myklebust { 5057b159fc1STrond Myklebust struct nfs_open_context *ctx; 5067b159fc1STrond Myklebust 5077b159fc1STrond Myklebust if (IS_SYNC(inode) || (filp->f_flags & O_SYNC)) 5087b159fc1STrond Myklebust return 1; 509cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 5107b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 5117b159fc1STrond Myklebust return 1; 5127b159fc1STrond Myklebust return 0; 5137b159fc1STrond Myklebust } 5147b159fc1STrond Myklebust 515027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 516027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 5171da177e4SLinus Torvalds { 51801cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 5191da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 5201da177e4SLinus Torvalds ssize_t result; 521027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 524027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 5251da177e4SLinus Torvalds 5266da24bc9SChuck Lever dprintk("NFS: write(%s/%s, %lu@%Ld)\n", 5271da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 5286da24bc9SChuck Lever (unsigned long) count, (long long) pos); 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds result = -EBUSY; 5311da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 5321da177e4SLinus Torvalds goto out_swapfile; 5337d52e862STrond Myklebust /* 5347d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 5357d52e862STrond Myklebust */ 5367d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 5377d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 5381da177e4SLinus Torvalds if (result) 5391da177e4SLinus Torvalds goto out; 540fe51beecSTrond Myklebust } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds result = count; 5431da177e4SLinus Torvalds if (!count) 5441da177e4SLinus Torvalds goto out; 5451da177e4SLinus Torvalds 54691d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count); 547027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 548200baa21STrond Myklebust /* Return error values for O_SYNC and IS_SYNC() */ 5497b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 550cd3758e3STrond Myklebust int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode); 551200baa21STrond Myklebust if (err < 0) 552200baa21STrond Myklebust result = err; 553200baa21STrond Myklebust } 5541da177e4SLinus Torvalds out: 5551da177e4SLinus Torvalds return result; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds out_swapfile: 5581da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 5591da177e4SLinus Torvalds goto out; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 5651da177e4SLinus Torvalds int status = 0; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds lock_kernel(); 568039c4d7aSTrond Myklebust /* Try local locking first */ 5696d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 5706d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 5716d34ac19SJ. Bruce Fields /* found a conflict */ 572039c4d7aSTrond Myklebust goto out; 5731da177e4SLinus Torvalds } 574039c4d7aSTrond Myklebust 575039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 576039c4d7aSTrond Myklebust goto out_noconflict; 577039c4d7aSTrond Myklebust 578039c4d7aSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) 579039c4d7aSTrond Myklebust goto out_noconflict; 580039c4d7aSTrond Myklebust 581039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 582039c4d7aSTrond Myklebust out: 5831da177e4SLinus Torvalds unlock_kernel(); 5841da177e4SLinus Torvalds return status; 585039c4d7aSTrond Myklebust out_noconflict: 586039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 587039c4d7aSTrond Myklebust goto out; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds int res = 0; 5931da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 5941da177e4SLinus Torvalds case FL_POSIX: 5951da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 5961da177e4SLinus Torvalds break; 5971da177e4SLinus Torvalds case FL_FLOCK: 5981da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 5991da177e4SLinus Torvalds break; 6001da177e4SLinus Torvalds default: 6011da177e4SLinus Torvalds BUG(); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds if (res < 0) 60446bae1a9SNeil Brown dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager" 60546bae1a9SNeil Brown " - error %d!\n", 6063110ff80SHarvey Harrison __func__, res); 6071da177e4SLinus Torvalds return res; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6131da177e4SLinus Torvalds int status; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds /* 6161da177e4SLinus Torvalds * Flush all pending writes before doing anything 6171da177e4SLinus Torvalds * with locks.. 6181da177e4SLinus Torvalds */ 61929884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* NOTE: special case 6221da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 6231da177e4SLinus Torvalds * still need to complete the unlock. 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds lock_kernel(); 6261da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 6271da177e4SLinus Torvalds if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 6281da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 6291da177e4SLinus Torvalds else 6301da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 6311da177e4SLinus Torvalds unlock_kernel(); 6321da177e4SLinus Torvalds return status; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6381da177e4SLinus Torvalds int status; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* 6411da177e4SLinus Torvalds * Flush all pending writes before doing anything 6421da177e4SLinus Torvalds * with locks.. 6431da177e4SLinus Torvalds */ 64429884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 64529884df0STrond Myklebust if (status != 0) 6461da177e4SLinus Torvalds goto out; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds lock_kernel(); 6491da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 650c4d7c402STrond Myklebust if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 6511da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 652c4d7c402STrond Myklebust else 6531da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 6541da177e4SLinus Torvalds unlock_kernel(); 6551da177e4SLinus Torvalds if (status < 0) 6561da177e4SLinus Torvalds goto out; 6571da177e4SLinus Torvalds /* 6581da177e4SLinus Torvalds * Make sure we clear the cache whenever we try to get the lock. 6591da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 6601da177e4SLinus Torvalds */ 66129884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 662b5418383STrond Myklebust if (!nfs_have_delegation(inode, FMODE_READ)) 6631da177e4SLinus Torvalds nfs_zap_caches(inode); 6641da177e4SLinus Torvalds out: 6651da177e4SLinus Torvalds return status; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* 6691da177e4SLinus Torvalds * Lock a (portion of) a file 6701da177e4SLinus Torvalds */ 6711da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6742116271aSTrond Myklebust int ret = -ENOLCK; 6751da177e4SLinus Torvalds 6766da24bc9SChuck Lever dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n", 6776da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 6786da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 6791da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 6801da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 6816da24bc9SChuck Lever 68291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /* No mandatory locks over NFS */ 685dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 6862116271aSTrond Myklebust goto out_err; 6872116271aSTrond Myklebust 6882116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 6892116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 6902116271aSTrond Myklebust if (ret < 0) 6912116271aSTrond Myklebust goto out_err; 6922116271aSTrond Myklebust } 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds if (IS_GETLK(cmd)) 6952116271aSTrond Myklebust ret = do_getlk(filp, cmd, fl); 6962116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 6972116271aSTrond Myklebust ret = do_unlk(filp, cmd, fl); 6982116271aSTrond Myklebust else 6992116271aSTrond Myklebust ret = do_setlk(filp, cmd, fl); 7002116271aSTrond Myklebust out_err: 7012116271aSTrond Myklebust return ret; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* 7051da177e4SLinus Torvalds * Lock a (portion of) a file 7061da177e4SLinus Torvalds */ 7071da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 7081da177e4SLinus Torvalds { 7096da24bc9SChuck Lever dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n", 7106da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 7116da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 7121da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 7151da177e4SLinus Torvalds return -ENOLCK; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 7181da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 7191da177e4SLinus Torvalds fl->fl_start = 0; 7201da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 7231da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 7241da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 7251da177e4SLinus Torvalds } 726370f6599SJ. Bruce Fields 7276da24bc9SChuck Lever /* 7286da24bc9SChuck Lever * There is no protocol support for leases, so we have no way to implement 7296da24bc9SChuck Lever * them correctly in the face of opens by other clients. 7306da24bc9SChuck Lever */ 731370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 732370f6599SJ. Bruce Fields { 7336da24bc9SChuck Lever dprintk("NFS: setlease(%s/%s, arg=%ld)\n", 7346da24bc9SChuck Lever file->f_path.dentry->d_parent->d_name.name, 7356da24bc9SChuck Lever file->f_path.dentry->d_name.name, arg); 7366da24bc9SChuck Lever 737370f6599SJ. Bruce Fields return -EINVAL; 738370f6599SJ. Bruce Fields } 739