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/pagemap.h> 28e8edc6e0SAlexey Dobriyan #include <linux/aio.h> 295a0e3ad6STejun Heo #include <linux/gfp.h> 30b608b283STrond Myklebust #include <linux/swap.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" 38545db45fSDavid Howells #include "fscache.h" 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static int nfs_file_open(struct inode *, struct file *); 431da177e4SLinus Torvalds static int nfs_file_release(struct inode *, struct file *); 44980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin); 451da177e4SLinus Torvalds static int nfs_file_mmap(struct file *, struct vm_area_struct *); 46f0930fffSJens Axboe static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos, 47f0930fffSJens Axboe struct pipe_inode_info *pipe, 48f0930fffSJens Axboe size_t count, unsigned int flags); 49027445c3SBadari Pulavarty static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov, 50027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 51bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 52bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 53bf40d343SSuresh Jayaraman size_t count, unsigned int flags); 54027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov, 55027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 5675e1fcc0SMiklos Szeredi static int nfs_file_flush(struct file *, fl_owner_t id); 577ea80859SChristoph Hellwig static int nfs_file_fsync(struct file *, int datasync); 581da177e4SLinus Torvalds static int nfs_check_flags(int flags); 591da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl); 601da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl); 61370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl); 621da177e4SLinus Torvalds 63f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops; 6494387fb1STrond Myklebust 654b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = { 66980802e3STrond Myklebust .llseek = nfs_file_llseek, 671da177e4SLinus Torvalds .read = do_sync_read, 681da177e4SLinus Torvalds .write = do_sync_write, 691da177e4SLinus Torvalds .aio_read = nfs_file_read, 701da177e4SLinus Torvalds .aio_write = nfs_file_write, 711da177e4SLinus Torvalds .mmap = nfs_file_mmap, 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, 79bf40d343SSuresh Jayaraman .splice_write = nfs_file_splice_write, 801da177e4SLinus Torvalds .check_flags = nfs_check_flags, 81370f6599SJ. Bruce Fields .setlease = nfs_setlease, 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 8492e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = { 851da177e4SLinus Torvalds .permission = nfs_permission, 861da177e4SLinus Torvalds .getattr = nfs_getattr, 871da177e4SLinus Torvalds .setattr = nfs_setattr, 881da177e4SLinus Torvalds }; 891da177e4SLinus Torvalds 90b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 9192e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = { 92b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 93b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 94b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 95b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 96b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 97b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 98b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 99b7fa0554SAndreas Gruenbacher }; 100b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_v3 */ 101b7fa0554SAndreas Gruenbacher 1021da177e4SLinus Torvalds /* Hack for future NFS swap support */ 1031da177e4SLinus Torvalds #ifndef IS_SWAPFILE 1041da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 1051da177e4SLinus Torvalds #endif 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static int nfs_check_flags(int flags) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 1101da177e4SLinus Torvalds return -EINVAL; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds return 0; 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds /* 1161da177e4SLinus Torvalds * Open file 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds static int 1191da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds int res; 1221da177e4SLinus Torvalds 1236da24bc9SChuck Lever dprintk("NFS: open file(%s/%s)\n", 124cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 125cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 126cc0dd2d1SChuck Lever 127c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1281da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 1291da177e4SLinus Torvalds if (res) 1301da177e4SLinus Torvalds return res; 1311da177e4SLinus Torvalds 13246cb650cSTrond Myklebust res = nfs_open(inode, filp); 1331da177e4SLinus Torvalds return res; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds static int 1371da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 1381da177e4SLinus Torvalds { 1396da24bc9SChuck Lever struct dentry *dentry = filp->f_path.dentry; 1406da24bc9SChuck Lever 1416da24bc9SChuck Lever dprintk("NFS: release(%s/%s)\n", 1426da24bc9SChuck Lever dentry->d_parent->d_name.name, 1436da24bc9SChuck Lever dentry->d_name.name); 1446da24bc9SChuck Lever 14591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 14646cb650cSTrond Myklebust return nfs_release(inode, filp); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 149980802e3STrond Myklebust /** 150980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 151980802e3STrond Myklebust * @inode - pointer to inode struct 152980802e3STrond Myklebust * @file - pointer to struct file 153980802e3STrond Myklebust * 154980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 155980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 156980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 157980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 158980802e3STrond Myklebust * shouldn't trust the cache). 159980802e3STrond Myklebust */ 160980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 161980802e3STrond Myklebust { 162980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 163980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 164980802e3STrond Myklebust 165d7cf8dd0STrond Myklebust if (nfs_have_delegated_attributes(inode)) 166d7cf8dd0STrond Myklebust goto out_noreval; 167d7cf8dd0STrond Myklebust 168980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 169980802e3STrond Myklebust goto force_reval; 170d7cf8dd0STrond Myklebust if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 171d7cf8dd0STrond Myklebust goto force_reval; 172d7cf8dd0STrond Myklebust if (nfs_attribute_timeout(inode)) 173d7cf8dd0STrond Myklebust goto force_reval; 174d7cf8dd0STrond Myklebust out_noreval: 175fe51beecSTrond Myklebust return 0; 176980802e3STrond Myklebust force_reval: 177980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 178980802e3STrond Myklebust } 179980802e3STrond Myklebust 180980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 181980802e3STrond Myklebust { 1829465efc9SAndi Kleen loff_t loff; 183e89e896dSTrond Myklebust 1846da24bc9SChuck Lever dprintk("NFS: llseek file(%s/%s, %lld, %d)\n", 185b84e06c5SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 186b84e06c5SChuck Lever filp->f_path.dentry->d_name.name, 187b84e06c5SChuck Lever offset, origin); 188b84e06c5SChuck Lever 189980802e3STrond Myklebust /* origin == SEEK_END => we must revalidate the cached file length */ 190aec5e175SJosef 'Jeff' Sipek if (origin == SEEK_END) { 191980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 192d5e66348STrond Myklebust 193980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 194980802e3STrond Myklebust if (retval < 0) 195980802e3STrond Myklebust return (loff_t)retval; 196d5e66348STrond Myklebust 197d5e66348STrond Myklebust spin_lock(&inode->i_lock); 1989465efc9SAndi Kleen loff = generic_file_llseek_unlocked(filp, offset, origin); 199d5e66348STrond Myklebust spin_unlock(&inode->i_lock); 200d5e66348STrond Myklebust } else 201d5e66348STrond Myklebust loff = generic_file_llseek_unlocked(filp, offset, origin); 2029465efc9SAndi Kleen return loff; 203980802e3STrond Myklebust } 204980802e3STrond Myklebust 2051da177e4SLinus Torvalds /* 2061da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds static int 20975e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 2101da177e4SLinus Torvalds { 2116da24bc9SChuck Lever struct dentry *dentry = file->f_path.dentry; 2126da24bc9SChuck Lever struct inode *inode = dentry->d_inode; 2131da177e4SLinus Torvalds 2146da24bc9SChuck Lever dprintk("NFS: flush(%s/%s)\n", 2156da24bc9SChuck Lever dentry->d_parent->d_name.name, 2166da24bc9SChuck Lever dentry->d_name.name); 2171da177e4SLinus Torvalds 218c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 2191da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 2201da177e4SLinus Torvalds return 0; 2217b159fc1STrond Myklebust 2227fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 223af7fa165STrond Myklebust return vfs_fsync(file, 0); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static ssize_t 227027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 228027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 2291da177e4SLinus Torvalds { 23001cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 2311da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 2321da177e4SLinus Torvalds ssize_t result; 233027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 236027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 2371da177e4SLinus Torvalds 2386da24bc9SChuck Lever dprintk("NFS: read(%s/%s, %lu@%lu)\n", 2391da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2401da177e4SLinus Torvalds (unsigned long) count, (unsigned long) pos); 2411da177e4SLinus Torvalds 24244b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 2434184dcf2SChuck Lever if (!result) { 244027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2454184dcf2SChuck Lever if (result > 0) 2464184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 2474184dcf2SChuck Lever } 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 2606da24bc9SChuck Lever dprintk("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); 265aa2f1ef1SChuck Lever if (!res) { 266f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 267aa2f1ef1SChuck Lever if (res > 0) 268aa2f1ef1SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res); 269aa2f1ef1SChuck Lever } 2701da177e4SLinus Torvalds return res; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds static int 2741da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2751da177e4SLinus Torvalds { 27601cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 2771da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2781da177e4SLinus Torvalds int status; 2791da177e4SLinus Torvalds 2806da24bc9SChuck Lever dprintk("NFS: mmap(%s/%s)\n", 2811da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2821da177e4SLinus Torvalds 283e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 284e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 285e1ebfd33STrond Myklebust */ 286e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 28794387fb1STrond Myklebust if (!status) { 28894387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 289e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 29094387fb1STrond Myklebust } 2911da177e4SLinus Torvalds return status; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /* 2951da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2961da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2971da177e4SLinus Torvalds * whether any write errors occurred for this process. 298af7fa165STrond Myklebust * 299af7fa165STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 300af7fa165STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 301af7fa165STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 302af7fa165STrond Myklebust * This is because the former is used to notify the _next_ call to 303af7fa165STrond Myklebust * nfs_file_write() that a write error occured, and hence cause it to 304af7fa165STrond Myklebust * fall back to doing a synchronous write. 3051da177e4SLinus Torvalds */ 3061da177e4SLinus Torvalds static int 3077ea80859SChristoph Hellwig nfs_file_fsync(struct file *file, int datasync) 3081da177e4SLinus Torvalds { 3097ea80859SChristoph Hellwig struct dentry *dentry = file->f_path.dentry; 310cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 3111da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 312af7fa165STrond Myklebust int have_error, status; 313af7fa165STrond Myklebust int ret = 0; 314af7fa165STrond Myklebust 3151da177e4SLinus Torvalds 3166da24bc9SChuck Lever dprintk("NFS: fsync file(%s/%s) datasync %d\n", 31754917786SChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 31854917786SChuck Lever datasync); 3191da177e4SLinus Torvalds 32091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 321af7fa165STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 322af7fa165STrond Myklebust status = nfs_commit_inode(inode, FLUSH_SYNC); 323af7fa165STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 324af7fa165STrond Myklebust if (have_error) 325af7fa165STrond Myklebust ret = xchg(&ctx->error, 0); 3260702099bSJ. R. Okajima if (!ret && status < 0) 327af7fa165STrond Myklebust ret = status; 328af7fa165STrond Myklebust return ret; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* 33238c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 33338c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 33438c73044SPeter Staubach * page cache. 33538c73044SPeter Staubach * 33638c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 33738c73044SPeter Staubach * being completely filled by the writer. In this situation, the 33838c73044SPeter Staubach * page must be completely written to stable storage on the server 33938c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 34038c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 34138c73044SPeter Staubach * done. 34238c73044SPeter Staubach * 34338c73044SPeter Staubach * It may be more efficient to read the page first if the file is 34438c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 34538c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 34638c73044SPeter Staubach * indicating that it was previously allocated and then modified, 34738c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 34838c73044SPeter Staubach * and that the new data won't completely replace the old data in 34938c73044SPeter Staubach * that range of the file. 35038c73044SPeter Staubach */ 35138c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page, 35238c73044SPeter Staubach loff_t pos, unsigned len) 35338c73044SPeter Staubach { 35438c73044SPeter Staubach unsigned int pglen = nfs_page_length(page); 35538c73044SPeter Staubach unsigned int offset = pos & (PAGE_CACHE_SIZE - 1); 35638c73044SPeter Staubach unsigned int end = offset + len; 35738c73044SPeter Staubach 35838c73044SPeter Staubach if ((file->f_mode & FMODE_READ) && /* open for read? */ 35938c73044SPeter Staubach !PageUptodate(page) && /* Uptodate? */ 36038c73044SPeter Staubach !PagePrivate(page) && /* i/o request already? */ 36138c73044SPeter Staubach pglen && /* valid bytes of file? */ 36238c73044SPeter Staubach (end < pglen || offset)) /* replace all valid bytes? */ 36338c73044SPeter Staubach return 1; 36438c73044SPeter Staubach return 0; 36538c73044SPeter Staubach } 36638c73044SPeter Staubach 36738c73044SPeter Staubach /* 3684899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3694899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3704899f9c8SNick Piggin * data from user space. 3711da177e4SLinus Torvalds * 3721da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3731da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3741da177e4SLinus Torvalds */ 3754899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3764899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3774899f9c8SNick Piggin struct page **pagep, void **fsdata) 3781da177e4SLinus Torvalds { 3794899f9c8SNick Piggin int ret; 38038c73044SPeter Staubach pgoff_t index = pos >> PAGE_CACHE_SHIFT; 3814899f9c8SNick Piggin struct page *page; 38238c73044SPeter Staubach int once_thru = 0; 3834899f9c8SNick Piggin 384b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n", 385b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 386b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 387b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 388b7eaefaaSChuck Lever 38938c73044SPeter Staubach start: 39072cb77f4STrond Myklebust /* 39172cb77f4STrond Myklebust * Prevent starvation issues if someone is doing a consistency 39272cb77f4STrond Myklebust * sync-to-disk 39372cb77f4STrond Myklebust */ 39472cb77f4STrond Myklebust ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING, 39572cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 39672cb77f4STrond Myklebust if (ret) 39772cb77f4STrond Myklebust return ret; 39872cb77f4STrond Myklebust 39954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 4004899f9c8SNick Piggin if (!page) 4014899f9c8SNick Piggin return -ENOMEM; 4024899f9c8SNick Piggin *pagep = page; 4034899f9c8SNick Piggin 4044899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 4054899f9c8SNick Piggin if (ret) { 4064899f9c8SNick Piggin unlock_page(page); 4074899f9c8SNick Piggin page_cache_release(page); 40838c73044SPeter Staubach } else if (!once_thru && 40938c73044SPeter Staubach nfs_want_read_modify_write(file, page, pos, len)) { 41038c73044SPeter Staubach once_thru = 1; 41138c73044SPeter Staubach ret = nfs_readpage(file, page); 41238c73044SPeter Staubach page_cache_release(page); 41338c73044SPeter Staubach if (!ret) 41438c73044SPeter Staubach goto start; 4154899f9c8SNick Piggin } 4164899f9c8SNick Piggin return ret; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4194899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 4204899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 4214899f9c8SNick Piggin struct page *page, void *fsdata) 4221da177e4SLinus Torvalds { 4234899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 4244899f9c8SNick Piggin int status; 4251da177e4SLinus Torvalds 426b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n", 427b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 428b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 429b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 430b7eaefaaSChuck Lever 431efc91ed0STrond Myklebust /* 432efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 433efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 434efc91ed0STrond Myklebust */ 435efc91ed0STrond Myklebust if (!PageUptodate(page)) { 436efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 437efc91ed0STrond Myklebust unsigned end = offset + len; 438efc91ed0STrond Myklebust 439efc91ed0STrond Myklebust if (pglen == 0) { 440efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 441efc91ed0STrond Myklebust end, PAGE_CACHE_SIZE); 442efc91ed0STrond Myklebust SetPageUptodate(page); 443efc91ed0STrond Myklebust } else if (end >= pglen) { 444efc91ed0STrond Myklebust zero_user_segment(page, end, PAGE_CACHE_SIZE); 445efc91ed0STrond Myklebust if (offset == 0) 446efc91ed0STrond Myklebust SetPageUptodate(page); 447efc91ed0STrond Myklebust } else 448efc91ed0STrond Myklebust zero_user_segment(page, pglen, PAGE_CACHE_SIZE); 449efc91ed0STrond Myklebust } 450efc91ed0STrond Myklebust 4514899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4524899f9c8SNick Piggin 4534899f9c8SNick Piggin unlock_page(page); 4544899f9c8SNick Piggin page_cache_release(page); 4554899f9c8SNick Piggin 4563d509e54SChuck Lever if (status < 0) 4573d509e54SChuck Lever return status; 4583d509e54SChuck Lever return copied; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4616b9b3514SDavid Howells /* 4626b9b3514SDavid Howells * Partially or wholly invalidate a page 4636b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4646b9b3514SDavid Howells * page invalidation 465545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4666b9b3514SDavid Howells * - Caller holds page lock 4676b9b3514SDavid Howells */ 4682ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset) 469cd52ed35STrond Myklebust { 470b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset); 471b7eaefaaSChuck Lever 4721c75950bSTrond Myklebust if (offset != 0) 4731c75950bSTrond Myklebust return; 474d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 4751b3b4a1aSTrond Myklebust nfs_wb_page_cancel(page->mapping->host, page); 476545db45fSDavid Howells 477545db45fSDavid Howells nfs_fscache_invalidate_page(page, page->mapping->host); 478cd52ed35STrond Myklebust } 479cd52ed35STrond Myklebust 4806b9b3514SDavid Howells /* 4816b9b3514SDavid Howells * Attempt to release the private state associated with a page 482545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4836b9b3514SDavid Howells * - Caller holds page lock 4846b9b3514SDavid Howells * - Return true (may release page) or false (may not) 4856b9b3514SDavid Howells */ 486cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 487cd52ed35STrond Myklebust { 488b608b283STrond Myklebust struct address_space *mapping = page->mapping; 489b608b283STrond Myklebust 490b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 491b7eaefaaSChuck Lever 492d812e575STrond Myklebust /* Only do I/O if gfp is a superset of GFP_KERNEL */ 493b608b283STrond Myklebust if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) { 494b608b283STrond Myklebust int how = FLUSH_SYNC; 495b608b283STrond Myklebust 496b608b283STrond Myklebust /* Don't let kswapd deadlock waiting for OOM RPC calls */ 497b608b283STrond Myklebust if (current_is_kswapd()) 498b608b283STrond Myklebust how = 0; 499b608b283STrond Myklebust nfs_commit_inode(mapping->host, how); 500b608b283STrond Myklebust } 501e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 502545db45fSDavid Howells if (PagePrivate(page)) 503ddeff520SNikita Danilov return 0; 504545db45fSDavid Howells return nfs_fscache_release_page(page, gfp); 505e3db7691STrond Myklebust } 506e3db7691STrond Myklebust 5076b9b3514SDavid Howells /* 5086b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 5096b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 5106b9b3514SDavid Howells * destroyed 511545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 5126b9b3514SDavid Howells * - Caller holds page lock 5136b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 5146b9b3514SDavid Howells */ 515e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 516e3db7691STrond Myklebust { 517b7eaefaaSChuck Lever struct inode *inode = page->mapping->host; 518545db45fSDavid Howells struct nfs_inode *nfsi = NFS_I(inode); 519b7eaefaaSChuck Lever 520b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 521b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 522b7eaefaaSChuck Lever 523545db45fSDavid Howells nfs_fscache_wait_on_page_write(nfsi, page); 524b7eaefaaSChuck Lever return nfs_wb_page(inode, page); 525cd52ed35STrond Myklebust } 526cd52ed35STrond Myklebust 527f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 5281da177e4SLinus Torvalds .readpage = nfs_readpage, 5291da177e4SLinus Torvalds .readpages = nfs_readpages, 5309cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 5311da177e4SLinus Torvalds .writepage = nfs_writepage, 5321da177e4SLinus Torvalds .writepages = nfs_writepages, 5334899f9c8SNick Piggin .write_begin = nfs_write_begin, 5344899f9c8SNick Piggin .write_end = nfs_write_end, 535cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 536cd52ed35STrond Myklebust .releasepage = nfs_release_page, 5371da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 538074cc1deSTrond Myklebust .migratepage = nfs_migrate_page, 539e3db7691STrond Myklebust .launder_page = nfs_launder_page, 540f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 5411da177e4SLinus Torvalds }; 5421da177e4SLinus Torvalds 5436b9b3514SDavid Howells /* 5446b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5456b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5466b9b3514SDavid Howells * shared-writable mapping 5476b9b3514SDavid Howells */ 548c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 54994387fb1STrond Myklebust { 550c2ec175cSNick Piggin struct page *page = vmf->page; 55194387fb1STrond Myklebust struct file *filp = vma->vm_file; 552b7eaefaaSChuck Lever struct dentry *dentry = filp->f_path.dentry; 55394387fb1STrond Myklebust unsigned pagelen; 55494387fb1STrond Myklebust int ret = -EINVAL; 5554899f9c8SNick Piggin struct address_space *mapping; 55694387fb1STrond Myklebust 557b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n", 558b7eaefaaSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 559b7eaefaaSChuck Lever filp->f_mapping->host->i_ino, 560b7eaefaaSChuck Lever (long long)page_offset(page)); 561b7eaefaaSChuck Lever 562545db45fSDavid Howells /* make sure the cache has finished storing the page */ 563545db45fSDavid Howells nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page); 564545db45fSDavid Howells 56594387fb1STrond Myklebust lock_page(page); 5664899f9c8SNick Piggin mapping = page->mapping; 567b7eaefaaSChuck Lever if (mapping != dentry->d_inode->i_mapping) 5688b1f9ee5STrond Myklebust goto out_unlock; 5698b1f9ee5STrond Myklebust 5708b1f9ee5STrond Myklebust ret = 0; 5714899f9c8SNick Piggin pagelen = nfs_page_length(page); 5728b1f9ee5STrond Myklebust if (pagelen == 0) 5738b1f9ee5STrond Myklebust goto out_unlock; 5748b1f9ee5STrond Myklebust 5758b1f9ee5STrond Myklebust ret = nfs_flush_incompatible(filp, page); 5768b1f9ee5STrond Myklebust if (ret != 0) 5778b1f9ee5STrond Myklebust goto out_unlock; 5788b1f9ee5STrond Myklebust 5798b1f9ee5STrond Myklebust ret = nfs_updatepage(filp, page, 0, pagelen); 5808b1f9ee5STrond Myklebust out_unlock: 5817fdf5230STrond Myklebust if (!ret) 5827fdf5230STrond Myklebust return VM_FAULT_LOCKED; 5834899f9c8SNick Piggin unlock_page(page); 5847fdf5230STrond Myklebust return VM_FAULT_SIGBUS; 58594387fb1STrond Myklebust } 58694387fb1STrond Myklebust 587f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 58894387fb1STrond Myklebust .fault = filemap_fault, 58994387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 59094387fb1STrond Myklebust }; 59194387fb1STrond Myklebust 5927b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 5937b159fc1STrond Myklebust { 5947b159fc1STrond Myklebust struct nfs_open_context *ctx; 5957b159fc1STrond Myklebust 5966b2f3d1fSChristoph Hellwig if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC)) 5977b159fc1STrond Myklebust return 1; 598cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 5997b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 6007b159fc1STrond Myklebust return 1; 6017b159fc1STrond Myklebust return 0; 6027b159fc1STrond Myklebust } 6037b159fc1STrond Myklebust 604027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 605027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 6061da177e4SLinus Torvalds { 60701cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 6081da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 6097e381172SChuck Lever unsigned long written = 0; 6101da177e4SLinus Torvalds ssize_t result; 611027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 614027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 6151da177e4SLinus Torvalds 6166da24bc9SChuck Lever dprintk("NFS: write(%s/%s, %lu@%Ld)\n", 6171da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 6186da24bc9SChuck Lever (unsigned long) count, (long long) pos); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds result = -EBUSY; 6211da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 6221da177e4SLinus Torvalds goto out_swapfile; 6237d52e862STrond Myklebust /* 6247d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 6257d52e862STrond Myklebust */ 6267d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 6277d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 6281da177e4SLinus Torvalds if (result) 6291da177e4SLinus Torvalds goto out; 630fe51beecSTrond Myklebust } 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds result = count; 6331da177e4SLinus Torvalds if (!count) 6341da177e4SLinus Torvalds goto out; 6351da177e4SLinus Torvalds 636027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 6377e381172SChuck Lever if (result > 0) 6387e381172SChuck Lever written = result; 6397e381172SChuck Lever 6406b2f3d1fSChristoph Hellwig /* Return error values for O_DSYNC and IS_SYNC() */ 6417b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 642af7fa165STrond Myklebust int err = vfs_fsync(iocb->ki_filp, 0); 643200baa21STrond Myklebust if (err < 0) 644200baa21STrond Myklebust result = err; 645200baa21STrond Myklebust } 6467e381172SChuck Lever if (result > 0) 6477e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 6481da177e4SLinus Torvalds out: 6491da177e4SLinus Torvalds return result; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds out_swapfile: 6521da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 6531da177e4SLinus Torvalds goto out; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 656bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 657bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 658bf40d343SSuresh Jayaraman size_t count, unsigned int flags) 659bf40d343SSuresh Jayaraman { 660bf40d343SSuresh Jayaraman struct dentry *dentry = filp->f_path.dentry; 661bf40d343SSuresh Jayaraman struct inode *inode = dentry->d_inode; 6627e381172SChuck Lever unsigned long written = 0; 663bf40d343SSuresh Jayaraman ssize_t ret; 664bf40d343SSuresh Jayaraman 665bf40d343SSuresh Jayaraman dprintk("NFS splice_write(%s/%s, %lu@%llu)\n", 666bf40d343SSuresh Jayaraman dentry->d_parent->d_name.name, dentry->d_name.name, 667bf40d343SSuresh Jayaraman (unsigned long) count, (unsigned long long) *ppos); 668bf40d343SSuresh Jayaraman 669bf40d343SSuresh Jayaraman /* 670bf40d343SSuresh Jayaraman * The combination of splice and an O_APPEND destination is disallowed. 671bf40d343SSuresh Jayaraman */ 672bf40d343SSuresh Jayaraman 673bf40d343SSuresh Jayaraman ret = generic_file_splice_write(pipe, filp, ppos, count, flags); 6747e381172SChuck Lever if (ret > 0) 6757e381172SChuck Lever written = ret; 6767e381172SChuck Lever 677bf40d343SSuresh Jayaraman if (ret >= 0 && nfs_need_sync_write(filp, inode)) { 678af7fa165STrond Myklebust int err = vfs_fsync(filp, 0); 679bf40d343SSuresh Jayaraman if (err < 0) 680bf40d343SSuresh Jayaraman ret = err; 681bf40d343SSuresh Jayaraman } 6827e381172SChuck Lever if (ret > 0) 6837e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 684bf40d343SSuresh Jayaraman return ret; 685bf40d343SSuresh Jayaraman } 686bf40d343SSuresh Jayaraman 687*5eebde23SSuresh Jayaraman static int 688*5eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6911da177e4SLinus Torvalds int status = 0; 6921da177e4SLinus Torvalds 693039c4d7aSTrond Myklebust /* Try local locking first */ 6946d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 6956d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 6966d34ac19SJ. Bruce Fields /* found a conflict */ 697039c4d7aSTrond Myklebust goto out; 6981da177e4SLinus Torvalds } 699039c4d7aSTrond Myklebust 700039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 701039c4d7aSTrond Myklebust goto out_noconflict; 702039c4d7aSTrond Myklebust 703*5eebde23SSuresh Jayaraman if (is_local) 704039c4d7aSTrond Myklebust goto out_noconflict; 705039c4d7aSTrond Myklebust 706039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 707039c4d7aSTrond Myklebust out: 7081da177e4SLinus Torvalds return status; 709039c4d7aSTrond Myklebust out_noconflict: 710039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 711039c4d7aSTrond Myklebust goto out; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds int res = 0; 7171da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 7181da177e4SLinus Torvalds case FL_POSIX: 7191da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 7201da177e4SLinus Torvalds break; 7211da177e4SLinus Torvalds case FL_FLOCK: 7221da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 7231da177e4SLinus Torvalds break; 7241da177e4SLinus Torvalds default: 7251da177e4SLinus Torvalds BUG(); 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds if (res < 0) 72846bae1a9SNeil Brown dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager" 72946bae1a9SNeil Brown " - error %d!\n", 7303110ff80SHarvey Harrison __func__, res); 7311da177e4SLinus Torvalds return res; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 734*5eebde23SSuresh Jayaraman static int 735*5eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7361da177e4SLinus Torvalds { 7371da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7381da177e4SLinus Torvalds int status; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds /* 7411da177e4SLinus Torvalds * Flush all pending writes before doing anything 7421da177e4SLinus Torvalds * with locks.. 7431da177e4SLinus Torvalds */ 74429884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* NOTE: special case 7471da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 7481da177e4SLinus Torvalds * still need to complete the unlock. 7491da177e4SLinus Torvalds */ 750*5eebde23SSuresh Jayaraman /* 751*5eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 752*5eebde23SSuresh Jayaraman * "-olocal_lock=" 753*5eebde23SSuresh Jayaraman */ 754*5eebde23SSuresh Jayaraman if (!is_local) 7551da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 7561da177e4SLinus Torvalds else 7571da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7581da177e4SLinus Torvalds return status; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 761*5eebde23SSuresh Jayaraman static int 762*5eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7651da177e4SLinus Torvalds int status; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* 7681da177e4SLinus Torvalds * Flush all pending writes before doing anything 7691da177e4SLinus Torvalds * with locks.. 7701da177e4SLinus Torvalds */ 77129884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 77229884df0STrond Myklebust if (status != 0) 7731da177e4SLinus Torvalds goto out; 7741da177e4SLinus Torvalds 775*5eebde23SSuresh Jayaraman /* 776*5eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 777*5eebde23SSuresh Jayaraman * "-olocal_lock=" 778*5eebde23SSuresh Jayaraman */ 779*5eebde23SSuresh Jayaraman if (!is_local) 7801da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 781c4d7c402STrond Myklebust else 7821da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7831da177e4SLinus Torvalds if (status < 0) 7841da177e4SLinus Torvalds goto out; 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * Make sure we clear the cache whenever we try to get the lock. 7871da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 7881da177e4SLinus Torvalds */ 78929884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 790b5418383STrond Myklebust if (!nfs_have_delegation(inode, FMODE_READ)) 7911da177e4SLinus Torvalds nfs_zap_caches(inode); 7921da177e4SLinus Torvalds out: 7931da177e4SLinus Torvalds return status; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * Lock a (portion of) a file 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8022116271aSTrond Myklebust int ret = -ENOLCK; 803*5eebde23SSuresh Jayaraman int is_local = 0; 8041da177e4SLinus Torvalds 8056da24bc9SChuck Lever dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n", 8066da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 8076da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 8081da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 8091da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 8106da24bc9SChuck Lever 81191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* No mandatory locks over NFS */ 814dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 8152116271aSTrond Myklebust goto out_err; 8162116271aSTrond Myklebust 817*5eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 818*5eebde23SSuresh Jayaraman is_local = 1; 819*5eebde23SSuresh Jayaraman 8202116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 8212116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 8222116271aSTrond Myklebust if (ret < 0) 8232116271aSTrond Myklebust goto out_err; 8242116271aSTrond Myklebust } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds if (IS_GETLK(cmd)) 827*5eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8282116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 829*5eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8302116271aSTrond Myklebust else 831*5eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 8322116271aSTrond Myklebust out_err: 8332116271aSTrond Myklebust return ret; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* 8371da177e4SLinus Torvalds * Lock a (portion of) a file 8381da177e4SLinus Torvalds */ 8391da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 8401da177e4SLinus Torvalds { 841*5eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 842*5eebde23SSuresh Jayaraman int is_local = 0; 843*5eebde23SSuresh Jayaraman 8446da24bc9SChuck Lever dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n", 8456da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 8466da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 8471da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 8501da177e4SLinus Torvalds return -ENOLCK; 8511da177e4SLinus Torvalds 852*5eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 853*5eebde23SSuresh Jayaraman is_local = 1; 854*5eebde23SSuresh Jayaraman 8551da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 8561da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 8571da177e4SLinus Torvalds fl->fl_start = 0; 8581da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 861*5eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 862*5eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 8631da177e4SLinus Torvalds } 864370f6599SJ. Bruce Fields 8656da24bc9SChuck Lever /* 8666da24bc9SChuck Lever * There is no protocol support for leases, so we have no way to implement 8676da24bc9SChuck Lever * them correctly in the face of opens by other clients. 8686da24bc9SChuck Lever */ 869370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 870370f6599SJ. Bruce Fields { 8716da24bc9SChuck Lever dprintk("NFS: setlease(%s/%s, arg=%ld)\n", 8726da24bc9SChuck Lever file->f_path.dentry->d_parent->d_name.name, 8736da24bc9SChuck Lever file->f_path.dentry->d_name.name, arg); 8746da24bc9SChuck Lever 875370f6599SJ. Bruce Fields return -EINVAL; 876370f6599SJ. Bruce Fields } 877