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 if (iocb->ki_filp->f_flags & O_DIRECT) 238027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n", 2411da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2421da177e4SLinus Torvalds (unsigned long) count, (unsigned long) pos); 2431da177e4SLinus Torvalds 24444b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 24591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count); 2461da177e4SLinus Torvalds if (!result) 247027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2481da177e4SLinus Torvalds return result; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds static ssize_t 252f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 253f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 254f0930fffSJens Axboe unsigned int flags) 2551da177e4SLinus Torvalds { 25601cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 2571da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2581da177e4SLinus Torvalds ssize_t res; 2591da177e4SLinus Torvalds 260f0930fffSJens Axboe dfprintk(VFS, "nfs: splice_read(%s/%s, %lu@%Lu)\n", 2611da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2621da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2631da177e4SLinus Torvalds 26444b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 2651da177e4SLinus Torvalds if (!res) 266f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 2671da177e4SLinus Torvalds return res; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds static int 2711da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2721da177e4SLinus Torvalds { 27301cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 2741da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2751da177e4SLinus Torvalds int status; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds dfprintk(VFS, "nfs: mmap(%s/%s)\n", 2781da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2791da177e4SLinus Torvalds 28044b11874STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 28194387fb1STrond Myklebust if (!status) { 28294387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 28394387fb1STrond Myklebust vma->vm_flags |= VM_CAN_NONLINEAR; 28494387fb1STrond Myklebust file_accessed(file); 28594387fb1STrond Myklebust } 2861da177e4SLinus Torvalds return status; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds /* 2901da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2911da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2921da177e4SLinus Torvalds * whether any write errors occurred for this process. 2931da177e4SLinus Torvalds */ 2941da177e4SLinus Torvalds static int 2951da177e4SLinus Torvalds nfs_fsync(struct file *file, struct dentry *dentry, int datasync) 2961da177e4SLinus Torvalds { 297cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 2981da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 3011da177e4SLinus Torvalds 30291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 3037b159fc1STrond Myklebust return nfs_do_fsync(ctx, inode); 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* 3074899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3084899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3094899f9c8SNick Piggin * data from user space. 3101da177e4SLinus Torvalds * 3111da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3121da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3131da177e4SLinus Torvalds */ 3144899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3154899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3164899f9c8SNick Piggin struct page **pagep, void **fsdata) 3171da177e4SLinus Torvalds { 3184899f9c8SNick Piggin int ret; 3194899f9c8SNick Piggin pgoff_t index; 3204899f9c8SNick Piggin struct page *page; 3214899f9c8SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 3224899f9c8SNick Piggin 3234899f9c8SNick Piggin page = __grab_cache_page(mapping, index); 3244899f9c8SNick Piggin if (!page) 3254899f9c8SNick Piggin return -ENOMEM; 3264899f9c8SNick Piggin *pagep = page; 3274899f9c8SNick Piggin 3284899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3294899f9c8SNick Piggin if (ret) { 3304899f9c8SNick Piggin unlock_page(page); 3314899f9c8SNick Piggin page_cache_release(page); 3324899f9c8SNick Piggin } 3334899f9c8SNick Piggin return ret; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3364899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3374899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3384899f9c8SNick Piggin struct page *page, void *fsdata) 3391da177e4SLinus Torvalds { 3404899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 3414899f9c8SNick Piggin int status; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds lock_kernel(); 3444899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 3451da177e4SLinus Torvalds unlock_kernel(); 3464899f9c8SNick Piggin 3474899f9c8SNick Piggin unlock_page(page); 3484899f9c8SNick Piggin page_cache_release(page); 3494899f9c8SNick Piggin 3503d509e54SChuck Lever if (status < 0) 3513d509e54SChuck Lever return status; 3523d509e54SChuck Lever return 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 .direct_IO = nfs_direct_IO, 385e3db7691STrond Myklebust .launder_page = nfs_launder_page, 3861da177e4SLinus Torvalds }; 3871da177e4SLinus Torvalds 38894387fb1STrond Myklebust static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) 38994387fb1STrond Myklebust { 39094387fb1STrond Myklebust struct file *filp = vma->vm_file; 39194387fb1STrond Myklebust unsigned pagelen; 39294387fb1STrond Myklebust int ret = -EINVAL; 3934899f9c8SNick Piggin struct address_space *mapping; 39494387fb1STrond Myklebust 39594387fb1STrond Myklebust lock_page(page); 3964899f9c8SNick Piggin mapping = page->mapping; 3978b1f9ee5STrond Myklebust if (mapping != vma->vm_file->f_path.dentry->d_inode->i_mapping) 3988b1f9ee5STrond Myklebust goto out_unlock; 3998b1f9ee5STrond Myklebust 4008b1f9ee5STrond Myklebust ret = 0; 4014899f9c8SNick Piggin pagelen = nfs_page_length(page); 4028b1f9ee5STrond Myklebust if (pagelen == 0) 4038b1f9ee5STrond Myklebust goto out_unlock; 4048b1f9ee5STrond Myklebust 4058b1f9ee5STrond Myklebust ret = nfs_flush_incompatible(filp, page); 4068b1f9ee5STrond Myklebust if (ret != 0) 4078b1f9ee5STrond Myklebust goto out_unlock; 4088b1f9ee5STrond Myklebust 4098b1f9ee5STrond Myklebust ret = nfs_updatepage(filp, page, 0, pagelen); 4108b1f9ee5STrond Myklebust if (ret == 0) 4118b1f9ee5STrond Myklebust ret = pagelen; 4128b1f9ee5STrond Myklebust out_unlock: 4134899f9c8SNick Piggin unlock_page(page); 41494387fb1STrond Myklebust return ret; 41594387fb1STrond Myklebust } 41694387fb1STrond Myklebust 41794387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = { 41894387fb1STrond Myklebust .fault = filemap_fault, 41994387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 42094387fb1STrond Myklebust }; 42194387fb1STrond Myklebust 4227b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 4237b159fc1STrond Myklebust { 4247b159fc1STrond Myklebust struct nfs_open_context *ctx; 4257b159fc1STrond Myklebust 4267b159fc1STrond Myklebust if (IS_SYNC(inode) || (filp->f_flags & O_SYNC)) 4277b159fc1STrond Myklebust return 1; 428cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 4297b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 4307b159fc1STrond Myklebust return 1; 4317b159fc1STrond Myklebust return 0; 4327b159fc1STrond Myklebust } 4337b159fc1STrond Myklebust 434027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 435027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 4361da177e4SLinus Torvalds { 43701cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 4381da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 4391da177e4SLinus Torvalds ssize_t result; 440027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 443027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 4441da177e4SLinus Torvalds 445027445c3SBadari Pulavarty dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n", 4461da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 447027445c3SBadari Pulavarty inode->i_ino, (unsigned long) count, (long long) pos); 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds result = -EBUSY; 4501da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 4511da177e4SLinus Torvalds goto out_swapfile; 4527d52e862STrond Myklebust /* 4537d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 4547d52e862STrond Myklebust */ 4557d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 4567d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 4571da177e4SLinus Torvalds if (result) 4581da177e4SLinus Torvalds goto out; 459fe51beecSTrond Myklebust } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds result = count; 4621da177e4SLinus Torvalds if (!count) 4631da177e4SLinus Torvalds goto out; 4641da177e4SLinus Torvalds 46591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count); 466027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 467200baa21STrond Myklebust /* Return error values for O_SYNC and IS_SYNC() */ 4687b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 469cd3758e3STrond Myklebust int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode); 470200baa21STrond Myklebust if (err < 0) 471200baa21STrond Myklebust result = err; 472200baa21STrond Myklebust } 4731da177e4SLinus Torvalds out: 4741da177e4SLinus Torvalds return result; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds out_swapfile: 4771da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 4781da177e4SLinus Torvalds goto out; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 4841da177e4SLinus Torvalds int status = 0; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds lock_kernel(); 487039c4d7aSTrond Myklebust /* Try local locking first */ 4886d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 4896d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 4906d34ac19SJ. Bruce Fields /* found a conflict */ 491039c4d7aSTrond Myklebust goto out; 4921da177e4SLinus Torvalds } 493039c4d7aSTrond Myklebust 494039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 495039c4d7aSTrond Myklebust goto out_noconflict; 496039c4d7aSTrond Myklebust 497039c4d7aSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) 498039c4d7aSTrond Myklebust goto out_noconflict; 499039c4d7aSTrond Myklebust 500039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 501039c4d7aSTrond Myklebust out: 5021da177e4SLinus Torvalds unlock_kernel(); 5031da177e4SLinus Torvalds return status; 504039c4d7aSTrond Myklebust out_noconflict: 505039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 506039c4d7aSTrond Myklebust goto out; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 5101da177e4SLinus Torvalds { 5111da177e4SLinus Torvalds int res = 0; 5121da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 5131da177e4SLinus Torvalds case FL_POSIX: 5141da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 5151da177e4SLinus Torvalds break; 5161da177e4SLinus Torvalds case FL_FLOCK: 5171da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 5181da177e4SLinus Torvalds break; 5191da177e4SLinus Torvalds default: 5201da177e4SLinus Torvalds BUG(); 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds if (res < 0) 52346bae1a9SNeil Brown dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager" 52446bae1a9SNeil Brown " - error %d!\n", 52546bae1a9SNeil Brown __FUNCTION__, res); 5261da177e4SLinus Torvalds return res; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl) 5301da177e4SLinus Torvalds { 5311da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 5321da177e4SLinus Torvalds int status; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds /* 5351da177e4SLinus Torvalds * Flush all pending writes before doing anything 5361da177e4SLinus Torvalds * with locks.. 5371da177e4SLinus Torvalds */ 53829884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* NOTE: special case 5411da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 5421da177e4SLinus Torvalds * still need to complete the unlock. 5431da177e4SLinus Torvalds */ 5441da177e4SLinus Torvalds lock_kernel(); 5451da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 5461da177e4SLinus Torvalds if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 5471da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 5481da177e4SLinus Torvalds else 5491da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 5501da177e4SLinus Torvalds unlock_kernel(); 5511da177e4SLinus Torvalds return status; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 5571da177e4SLinus Torvalds int status; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds /* 5601da177e4SLinus Torvalds * Flush all pending writes before doing anything 5611da177e4SLinus Torvalds * with locks.. 5621da177e4SLinus Torvalds */ 56329884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 56429884df0STrond Myklebust if (status != 0) 5651da177e4SLinus Torvalds goto out; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds lock_kernel(); 5681da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 569*c4d7c402STrond Myklebust if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 5701da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 571*c4d7c402STrond Myklebust else 5721da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 5731da177e4SLinus Torvalds unlock_kernel(); 5741da177e4SLinus Torvalds if (status < 0) 5751da177e4SLinus Torvalds goto out; 5761da177e4SLinus Torvalds /* 5771da177e4SLinus Torvalds * Make sure we clear the cache whenever we try to get the lock. 5781da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 5791da177e4SLinus Torvalds */ 58029884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 5811da177e4SLinus Torvalds nfs_zap_caches(inode); 5821da177e4SLinus Torvalds out: 5831da177e4SLinus Torvalds return status; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* 5871da177e4SLinus Torvalds * Lock a (portion of) a file 5881da177e4SLinus Torvalds */ 5891da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 5901da177e4SLinus Torvalds { 5911da177e4SLinus Torvalds struct inode * inode = filp->f_mapping->host; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n", 5941da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino, 5951da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 5961da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 59791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* No mandatory locks over NFS */ 600dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 6011da177e4SLinus Torvalds return -ENOLCK; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds if (IS_GETLK(cmd)) 6041da177e4SLinus Torvalds return do_getlk(filp, cmd, fl); 6051da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 6061da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 6071da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /* 6111da177e4SLinus Torvalds * Lock a (portion of) a file 6121da177e4SLinus Torvalds */ 6131da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n", 61601cce933SJosef "Jeff" Sipek filp->f_path.dentry->d_inode->i_sb->s_id, 61701cce933SJosef "Jeff" Sipek filp->f_path.dentry->d_inode->i_ino, 6181da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds /* 6211da177e4SLinus Torvalds * No BSD flocks over NFS allowed. 6221da177e4SLinus Torvalds * Note: we could try to fake a POSIX lock request here by 6231da177e4SLinus Torvalds * using ((u32) filp | 0x80000000) or some such as the pid. 6241da177e4SLinus Torvalds * Not sure whether that would be unique, though, or whether 6251da177e4SLinus Torvalds * that would break in other places. 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 6281da177e4SLinus Torvalds return -ENOLCK; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 6311da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 6321da177e4SLinus Torvalds fl->fl_start = 0; 6331da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 6361da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 6371da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 6381da177e4SLinus Torvalds } 639370f6599SJ. Bruce Fields 640370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 641370f6599SJ. Bruce Fields { 642370f6599SJ. Bruce Fields /* 643370f6599SJ. Bruce Fields * There is no protocol support for leases, so we have no way 644370f6599SJ. Bruce Fields * to implement them correctly in the face of opens by other 645370f6599SJ. Bruce Fields * clients. 646370f6599SJ. Bruce Fields */ 647370f6599SJ. Bruce Fields return -EINVAL; 648370f6599SJ. Bruce Fields } 649