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" 39e5e94017SBenny Halevy #include "pnfs.h" 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static int nfs_file_open(struct inode *, struct file *); 441da177e4SLinus Torvalds static int nfs_file_release(struct inode *, struct file *); 45980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin); 461da177e4SLinus Torvalds static int nfs_file_mmap(struct file *, struct vm_area_struct *); 47f0930fffSJens Axboe static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos, 48f0930fffSJens Axboe struct pipe_inode_info *pipe, 49f0930fffSJens Axboe size_t count, unsigned int flags); 50027445c3SBadari Pulavarty static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov, 51027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 52bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 53bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 54bf40d343SSuresh Jayaraman size_t count, unsigned int flags); 55027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov, 56027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 5775e1fcc0SMiklos Szeredi static int nfs_file_flush(struct file *, fl_owner_t id); 5802c24a82SJosef Bacik static int nfs_file_fsync(struct file *, loff_t, loff_t, int datasync); 591da177e4SLinus Torvalds static int nfs_check_flags(int flags); 601da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl); 611da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl); 62370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl); 631da177e4SLinus Torvalds 64f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops; 6594387fb1STrond Myklebust 664b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = { 67980802e3STrond Myklebust .llseek = nfs_file_llseek, 681da177e4SLinus Torvalds .read = do_sync_read, 691da177e4SLinus Torvalds .write = do_sync_write, 701da177e4SLinus Torvalds .aio_read = nfs_file_read, 711da177e4SLinus Torvalds .aio_write = nfs_file_write, 721da177e4SLinus Torvalds .mmap = nfs_file_mmap, 731da177e4SLinus Torvalds .open = nfs_file_open, 741da177e4SLinus Torvalds .flush = nfs_file_flush, 751da177e4SLinus Torvalds .release = nfs_file_release, 7654917786SChuck Lever .fsync = nfs_file_fsync, 771da177e4SLinus Torvalds .lock = nfs_lock, 781da177e4SLinus Torvalds .flock = nfs_flock, 79f0930fffSJens Axboe .splice_read = nfs_file_splice_read, 80bf40d343SSuresh Jayaraman .splice_write = nfs_file_splice_write, 811da177e4SLinus Torvalds .check_flags = nfs_check_flags, 82370f6599SJ. Bruce Fields .setlease = nfs_setlease, 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 8592e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = { 861da177e4SLinus Torvalds .permission = nfs_permission, 871da177e4SLinus Torvalds .getattr = nfs_getattr, 881da177e4SLinus Torvalds .setattr = nfs_setattr, 891da177e4SLinus Torvalds }; 901da177e4SLinus Torvalds 91b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 9292e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = { 93b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 94b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 95b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 96b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 97b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 98b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 99b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 100b7fa0554SAndreas Gruenbacher }; 101b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_v3 */ 102b7fa0554SAndreas Gruenbacher 1031da177e4SLinus Torvalds /* Hack for future NFS swap support */ 1041da177e4SLinus Torvalds #ifndef IS_SWAPFILE 1051da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 1061da177e4SLinus Torvalds #endif 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds static int nfs_check_flags(int flags) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 1111da177e4SLinus Torvalds return -EINVAL; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds return 0; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * Open file 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds static int 1201da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds int res; 1231da177e4SLinus Torvalds 1246da24bc9SChuck Lever dprintk("NFS: open file(%s/%s)\n", 125cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 126cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 127cc0dd2d1SChuck Lever 128c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1291da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 1301da177e4SLinus Torvalds if (res) 1311da177e4SLinus Torvalds return res; 1321da177e4SLinus Torvalds 13346cb650cSTrond Myklebust res = nfs_open(inode, filp); 1341da177e4SLinus Torvalds return res; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static int 1381da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 1391da177e4SLinus Torvalds { 1406da24bc9SChuck Lever struct dentry *dentry = filp->f_path.dentry; 1416da24bc9SChuck Lever 1426da24bc9SChuck Lever dprintk("NFS: release(%s/%s)\n", 1436da24bc9SChuck Lever dentry->d_parent->d_name.name, 1446da24bc9SChuck Lever dentry->d_name.name); 1456da24bc9SChuck Lever 14691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 14746cb650cSTrond Myklebust return nfs_release(inode, filp); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 150980802e3STrond Myklebust /** 151980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 152980802e3STrond Myklebust * @inode - pointer to inode struct 153980802e3STrond Myklebust * @file - pointer to struct file 154980802e3STrond Myklebust * 155980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 156980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 157980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 158980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 159980802e3STrond Myklebust * shouldn't trust the cache). 160980802e3STrond Myklebust */ 161980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 162980802e3STrond Myklebust { 163980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 164980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 165980802e3STrond Myklebust 166d7cf8dd0STrond Myklebust if (nfs_have_delegated_attributes(inode)) 167d7cf8dd0STrond Myklebust goto out_noreval; 168d7cf8dd0STrond Myklebust 169980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 170980802e3STrond Myklebust goto force_reval; 171d7cf8dd0STrond Myklebust if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 172d7cf8dd0STrond Myklebust goto force_reval; 173d7cf8dd0STrond Myklebust if (nfs_attribute_timeout(inode)) 174d7cf8dd0STrond Myklebust goto force_reval; 175d7cf8dd0STrond Myklebust out_noreval: 176fe51beecSTrond Myklebust return 0; 177980802e3STrond Myklebust force_reval: 178980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 179980802e3STrond Myklebust } 180980802e3STrond Myklebust 181980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 182980802e3STrond 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 18806222e49SJosef Bacik /* 18906222e49SJosef Bacik * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 19006222e49SJosef Bacik * the cached file length 19106222e49SJosef Bacik */ 19206222e49SJosef Bacik if (origin != SEEK_SET || origin != SEEK_CUR) { 193980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 194d5e66348STrond Myklebust 195980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 196980802e3STrond Myklebust if (retval < 0) 197980802e3STrond Myklebust return (loff_t)retval; 198*79835a71SAndi Kleen } 199d5e66348STrond Myklebust 200*79835a71SAndi Kleen return generic_file_llseek(filp, offset, origin); 201980802e3STrond Myklebust } 202980802e3STrond Myklebust 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds static int 20775e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 2081da177e4SLinus Torvalds { 2096da24bc9SChuck Lever struct dentry *dentry = file->f_path.dentry; 2106da24bc9SChuck Lever struct inode *inode = dentry->d_inode; 2111da177e4SLinus Torvalds 2126da24bc9SChuck Lever dprintk("NFS: flush(%s/%s)\n", 2136da24bc9SChuck Lever dentry->d_parent->d_name.name, 2146da24bc9SChuck Lever dentry->d_name.name); 2151da177e4SLinus Torvalds 216c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 2171da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 2181da177e4SLinus Torvalds return 0; 2197b159fc1STrond Myklebust 2207fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 221af7fa165STrond Myklebust return vfs_fsync(file, 0); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds static ssize_t 225027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 226027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 2271da177e4SLinus Torvalds { 22801cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 2291da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 2301da177e4SLinus Torvalds ssize_t result; 231027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 234027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 2351da177e4SLinus Torvalds 2366da24bc9SChuck Lever dprintk("NFS: read(%s/%s, %lu@%lu)\n", 2371da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2381da177e4SLinus Torvalds (unsigned long) count, (unsigned long) pos); 2391da177e4SLinus Torvalds 24044b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 2414184dcf2SChuck Lever if (!result) { 242027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2434184dcf2SChuck Lever if (result > 0) 2444184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 2454184dcf2SChuck Lever } 2461da177e4SLinus Torvalds return result; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds static ssize_t 250f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 251f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 252f0930fffSJens Axboe unsigned int flags) 2531da177e4SLinus Torvalds { 25401cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 2551da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2561da177e4SLinus Torvalds ssize_t res; 2571da177e4SLinus Torvalds 2586da24bc9SChuck Lever dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n", 2591da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2601da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2611da177e4SLinus Torvalds 26244b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 263aa2f1ef1SChuck Lever if (!res) { 264f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 265aa2f1ef1SChuck Lever if (res > 0) 266aa2f1ef1SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res); 267aa2f1ef1SChuck Lever } 2681da177e4SLinus Torvalds return res; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static int 2721da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2731da177e4SLinus Torvalds { 27401cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 2751da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2761da177e4SLinus Torvalds int status; 2771da177e4SLinus Torvalds 2786da24bc9SChuck Lever dprintk("NFS: mmap(%s/%s)\n", 2791da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2801da177e4SLinus Torvalds 281e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 282e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 283e1ebfd33STrond Myklebust */ 284e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 28594387fb1STrond Myklebust if (!status) { 28694387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 287e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 28894387fb1STrond Myklebust } 2891da177e4SLinus Torvalds return status; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* 2931da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2941da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2951da177e4SLinus Torvalds * whether any write errors occurred for this process. 296af7fa165STrond Myklebust * 297af7fa165STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 298af7fa165STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 299af7fa165STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 300af7fa165STrond Myklebust * This is because the former is used to notify the _next_ call to 30125985edcSLucas De Marchi * nfs_file_write() that a write error occurred, and hence cause it to 302af7fa165STrond Myklebust * fall back to doing a synchronous write. 3031da177e4SLinus Torvalds */ 3041da177e4SLinus Torvalds static int 30502c24a82SJosef Bacik nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 3061da177e4SLinus Torvalds { 3077ea80859SChristoph Hellwig struct dentry *dentry = file->f_path.dentry; 308cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 3091da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 310af7fa165STrond Myklebust int have_error, status; 311af7fa165STrond Myklebust int ret = 0; 312af7fa165STrond Myklebust 3136da24bc9SChuck Lever dprintk("NFS: fsync file(%s/%s) datasync %d\n", 31454917786SChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 31554917786SChuck Lever datasync); 3161da177e4SLinus Torvalds 31702c24a82SJosef Bacik ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 31802c24a82SJosef Bacik if (ret) 31902c24a82SJosef Bacik return ret; 32002c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 32102c24a82SJosef Bacik 32291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 323af7fa165STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 324af7fa165STrond Myklebust status = nfs_commit_inode(inode, FLUSH_SYNC); 325af7fa165STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 326af7fa165STrond Myklebust if (have_error) 327af7fa165STrond Myklebust ret = xchg(&ctx->error, 0); 3280702099bSJ. R. Okajima if (!ret && status < 0) 329af7fa165STrond Myklebust ret = status; 330863a3c6cSAndy Adamson if (!ret && !datasync) 331863a3c6cSAndy Adamson /* application has asked for meta-data sync */ 332ef311537SAndy Adamson ret = pnfs_layoutcommit_inode(inode, true); 33302c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 334af7fa165STrond Myklebust return ret; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* 33838c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 33938c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 34038c73044SPeter Staubach * page cache. 34138c73044SPeter Staubach * 34238c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 34338c73044SPeter Staubach * being completely filled by the writer. In this situation, the 34438c73044SPeter Staubach * page must be completely written to stable storage on the server 34538c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 34638c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 34738c73044SPeter Staubach * done. 34838c73044SPeter Staubach * 34938c73044SPeter Staubach * It may be more efficient to read the page first if the file is 35038c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 35138c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 35238c73044SPeter Staubach * indicating that it was previously allocated and then modified, 35338c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 35438c73044SPeter Staubach * and that the new data won't completely replace the old data in 35538c73044SPeter Staubach * that range of the file. 35638c73044SPeter Staubach */ 35738c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page, 35838c73044SPeter Staubach loff_t pos, unsigned len) 35938c73044SPeter Staubach { 36038c73044SPeter Staubach unsigned int pglen = nfs_page_length(page); 36138c73044SPeter Staubach unsigned int offset = pos & (PAGE_CACHE_SIZE - 1); 36238c73044SPeter Staubach unsigned int end = offset + len; 36338c73044SPeter Staubach 36438c73044SPeter Staubach if ((file->f_mode & FMODE_READ) && /* open for read? */ 36538c73044SPeter Staubach !PageUptodate(page) && /* Uptodate? */ 36638c73044SPeter Staubach !PagePrivate(page) && /* i/o request already? */ 36738c73044SPeter Staubach pglen && /* valid bytes of file? */ 36838c73044SPeter Staubach (end < pglen || offset)) /* replace all valid bytes? */ 36938c73044SPeter Staubach return 1; 37038c73044SPeter Staubach return 0; 37138c73044SPeter Staubach } 37238c73044SPeter Staubach 37338c73044SPeter Staubach /* 3744899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3754899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3764899f9c8SNick Piggin * data from user space. 3771da177e4SLinus Torvalds * 3781da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3791da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3801da177e4SLinus Torvalds */ 3814899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3824899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3834899f9c8SNick Piggin struct page **pagep, void **fsdata) 3841da177e4SLinus Torvalds { 3854899f9c8SNick Piggin int ret; 38638c73044SPeter Staubach pgoff_t index = pos >> PAGE_CACHE_SHIFT; 3874899f9c8SNick Piggin struct page *page; 38838c73044SPeter Staubach int once_thru = 0; 3894899f9c8SNick Piggin 390b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n", 391b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 392b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 393b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 394b7eaefaaSChuck Lever 39538c73044SPeter Staubach start: 39672cb77f4STrond Myklebust /* 39772cb77f4STrond Myklebust * Prevent starvation issues if someone is doing a consistency 39872cb77f4STrond Myklebust * sync-to-disk 39972cb77f4STrond Myklebust */ 40072cb77f4STrond Myklebust ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING, 40172cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 40272cb77f4STrond Myklebust if (ret) 40372cb77f4STrond Myklebust return ret; 40472cb77f4STrond Myklebust 40554566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 4064899f9c8SNick Piggin if (!page) 4074899f9c8SNick Piggin return -ENOMEM; 4084899f9c8SNick Piggin *pagep = page; 4094899f9c8SNick Piggin 4104899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 4114899f9c8SNick Piggin if (ret) { 4124899f9c8SNick Piggin unlock_page(page); 4134899f9c8SNick Piggin page_cache_release(page); 41438c73044SPeter Staubach } else if (!once_thru && 41538c73044SPeter Staubach nfs_want_read_modify_write(file, page, pos, len)) { 41638c73044SPeter Staubach once_thru = 1; 41738c73044SPeter Staubach ret = nfs_readpage(file, page); 41838c73044SPeter Staubach page_cache_release(page); 41938c73044SPeter Staubach if (!ret) 42038c73044SPeter Staubach goto start; 4214899f9c8SNick Piggin } 4224899f9c8SNick Piggin return ret; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4254899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 4264899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 4274899f9c8SNick Piggin struct page *page, void *fsdata) 4281da177e4SLinus Torvalds { 4294899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 4304899f9c8SNick Piggin int status; 4311da177e4SLinus Torvalds 432b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n", 433b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 434b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 435b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 436b7eaefaaSChuck Lever 437efc91ed0STrond Myklebust /* 438efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 439efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 440efc91ed0STrond Myklebust */ 441efc91ed0STrond Myklebust if (!PageUptodate(page)) { 442efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 443efc91ed0STrond Myklebust unsigned end = offset + len; 444efc91ed0STrond Myklebust 445efc91ed0STrond Myklebust if (pglen == 0) { 446efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 447efc91ed0STrond Myklebust end, PAGE_CACHE_SIZE); 448efc91ed0STrond Myklebust SetPageUptodate(page); 449efc91ed0STrond Myklebust } else if (end >= pglen) { 450efc91ed0STrond Myklebust zero_user_segment(page, end, PAGE_CACHE_SIZE); 451efc91ed0STrond Myklebust if (offset == 0) 452efc91ed0STrond Myklebust SetPageUptodate(page); 453efc91ed0STrond Myklebust } else 454efc91ed0STrond Myklebust zero_user_segment(page, pglen, PAGE_CACHE_SIZE); 455efc91ed0STrond Myklebust } 456efc91ed0STrond Myklebust 4574899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4584899f9c8SNick Piggin 4594899f9c8SNick Piggin unlock_page(page); 4604899f9c8SNick Piggin page_cache_release(page); 4614899f9c8SNick Piggin 4623d509e54SChuck Lever if (status < 0) 4633d509e54SChuck Lever return status; 4643d509e54SChuck Lever return copied; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4676b9b3514SDavid Howells /* 4686b9b3514SDavid Howells * Partially or wholly invalidate a page 4696b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4706b9b3514SDavid Howells * page invalidation 471545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4726b9b3514SDavid Howells * - Caller holds page lock 4736b9b3514SDavid Howells */ 4742ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset) 475cd52ed35STrond Myklebust { 476b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset); 477b7eaefaaSChuck Lever 4781c75950bSTrond Myklebust if (offset != 0) 4791c75950bSTrond Myklebust return; 480d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 4811b3b4a1aSTrond Myklebust nfs_wb_page_cancel(page->mapping->host, page); 482545db45fSDavid Howells 483545db45fSDavid Howells nfs_fscache_invalidate_page(page, page->mapping->host); 484cd52ed35STrond Myklebust } 485cd52ed35STrond Myklebust 4866b9b3514SDavid Howells /* 4876b9b3514SDavid Howells * Attempt to release the private state associated with a page 488545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4896b9b3514SDavid Howells * - Caller holds page lock 4906b9b3514SDavid Howells * - Return true (may release page) or false (may not) 4916b9b3514SDavid Howells */ 492cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 493cd52ed35STrond Myklebust { 494b608b283STrond Myklebust struct address_space *mapping = page->mapping; 495b608b283STrond Myklebust 496b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 497b7eaefaaSChuck Lever 498d812e575STrond Myklebust /* Only do I/O if gfp is a superset of GFP_KERNEL */ 499b608b283STrond Myklebust if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) { 500b608b283STrond Myklebust int how = FLUSH_SYNC; 501b608b283STrond Myklebust 502b608b283STrond Myklebust /* Don't let kswapd deadlock waiting for OOM RPC calls */ 503b608b283STrond Myklebust if (current_is_kswapd()) 504b608b283STrond Myklebust how = 0; 505b608b283STrond Myklebust nfs_commit_inode(mapping->host, how); 506b608b283STrond Myklebust } 507e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 508545db45fSDavid Howells if (PagePrivate(page)) 509ddeff520SNikita Danilov return 0; 510545db45fSDavid Howells return nfs_fscache_release_page(page, gfp); 511e3db7691STrond Myklebust } 512e3db7691STrond Myklebust 5136b9b3514SDavid Howells /* 5146b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 5156b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 5166b9b3514SDavid Howells * destroyed 517545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 5186b9b3514SDavid Howells * - Caller holds page lock 5196b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 5206b9b3514SDavid Howells */ 521e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 522e3db7691STrond Myklebust { 523b7eaefaaSChuck Lever struct inode *inode = page->mapping->host; 524545db45fSDavid Howells struct nfs_inode *nfsi = NFS_I(inode); 525b7eaefaaSChuck Lever 526b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 527b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 528b7eaefaaSChuck Lever 529545db45fSDavid Howells nfs_fscache_wait_on_page_write(nfsi, page); 530b7eaefaaSChuck Lever return nfs_wb_page(inode, page); 531cd52ed35STrond Myklebust } 532cd52ed35STrond Myklebust 533f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 5341da177e4SLinus Torvalds .readpage = nfs_readpage, 5351da177e4SLinus Torvalds .readpages = nfs_readpages, 5369cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 5371da177e4SLinus Torvalds .writepage = nfs_writepage, 5381da177e4SLinus Torvalds .writepages = nfs_writepages, 5394899f9c8SNick Piggin .write_begin = nfs_write_begin, 5404899f9c8SNick Piggin .write_end = nfs_write_end, 541cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 542cd52ed35STrond Myklebust .releasepage = nfs_release_page, 5431da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 544074cc1deSTrond Myklebust .migratepage = nfs_migrate_page, 545e3db7691STrond Myklebust .launder_page = nfs_launder_page, 546f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 5471da177e4SLinus Torvalds }; 5481da177e4SLinus Torvalds 5496b9b3514SDavid Howells /* 5506b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5516b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5526b9b3514SDavid Howells * shared-writable mapping 5536b9b3514SDavid Howells */ 554c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 55594387fb1STrond Myklebust { 556c2ec175cSNick Piggin struct page *page = vmf->page; 55794387fb1STrond Myklebust struct file *filp = vma->vm_file; 558b7eaefaaSChuck Lever struct dentry *dentry = filp->f_path.dentry; 55994387fb1STrond Myklebust unsigned pagelen; 560bc4866b6STrond Myklebust int ret = VM_FAULT_NOPAGE; 5614899f9c8SNick Piggin struct address_space *mapping; 56294387fb1STrond Myklebust 563b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n", 564b7eaefaaSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 565b7eaefaaSChuck Lever filp->f_mapping->host->i_ino, 566b7eaefaaSChuck Lever (long long)page_offset(page)); 567b7eaefaaSChuck Lever 568545db45fSDavid Howells /* make sure the cache has finished storing the page */ 569545db45fSDavid Howells nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page); 570545db45fSDavid Howells 57194387fb1STrond Myklebust lock_page(page); 5724899f9c8SNick Piggin mapping = page->mapping; 573b7eaefaaSChuck Lever if (mapping != dentry->d_inode->i_mapping) 5748b1f9ee5STrond Myklebust goto out_unlock; 5758b1f9ee5STrond Myklebust 5764899f9c8SNick Piggin pagelen = nfs_page_length(page); 5778b1f9ee5STrond Myklebust if (pagelen == 0) 5788b1f9ee5STrond Myklebust goto out_unlock; 5798b1f9ee5STrond Myklebust 580bc4866b6STrond Myklebust ret = VM_FAULT_LOCKED; 581bc4866b6STrond Myklebust if (nfs_flush_incompatible(filp, page) == 0 && 582bc4866b6STrond Myklebust nfs_updatepage(filp, page, 0, pagelen) == 0) 583bc4866b6STrond Myklebust goto out; 5848b1f9ee5STrond Myklebust 585bc4866b6STrond Myklebust ret = VM_FAULT_SIGBUS; 5868b1f9ee5STrond Myklebust out_unlock: 5874899f9c8SNick Piggin unlock_page(page); 588bc4866b6STrond Myklebust out: 589bc4866b6STrond Myklebust return ret; 59094387fb1STrond Myklebust } 59194387fb1STrond Myklebust 592f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 59394387fb1STrond Myklebust .fault = filemap_fault, 59494387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 59594387fb1STrond Myklebust }; 59694387fb1STrond Myklebust 5977b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 5987b159fc1STrond Myklebust { 5997b159fc1STrond Myklebust struct nfs_open_context *ctx; 6007b159fc1STrond Myklebust 6016b2f3d1fSChristoph Hellwig if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC)) 6027b159fc1STrond Myklebust return 1; 603cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 6047b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 6057b159fc1STrond Myklebust return 1; 6067b159fc1STrond Myklebust return 0; 6077b159fc1STrond Myklebust } 6087b159fc1STrond Myklebust 609027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 610027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 6111da177e4SLinus Torvalds { 61201cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 6131da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 6147e381172SChuck Lever unsigned long written = 0; 6151da177e4SLinus Torvalds ssize_t result; 616027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 619027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 6201da177e4SLinus Torvalds 6216da24bc9SChuck Lever dprintk("NFS: write(%s/%s, %lu@%Ld)\n", 6221da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 6236da24bc9SChuck Lever (unsigned long) count, (long long) pos); 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds result = -EBUSY; 6261da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 6271da177e4SLinus Torvalds goto out_swapfile; 6287d52e862STrond Myklebust /* 6297d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 6307d52e862STrond Myklebust */ 6317d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 6327d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 6331da177e4SLinus Torvalds if (result) 6341da177e4SLinus Torvalds goto out; 635fe51beecSTrond Myklebust } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds result = count; 6381da177e4SLinus Torvalds if (!count) 6391da177e4SLinus Torvalds goto out; 6401da177e4SLinus Torvalds 641027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 6427e381172SChuck Lever if (result > 0) 6437e381172SChuck Lever written = result; 6447e381172SChuck Lever 6456b2f3d1fSChristoph Hellwig /* Return error values for O_DSYNC and IS_SYNC() */ 6467b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 647af7fa165STrond Myklebust int err = vfs_fsync(iocb->ki_filp, 0); 648200baa21STrond Myklebust if (err < 0) 649200baa21STrond Myklebust result = err; 650200baa21STrond Myklebust } 6517e381172SChuck Lever if (result > 0) 6527e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 6531da177e4SLinus Torvalds out: 6541da177e4SLinus Torvalds return result; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds out_swapfile: 6571da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 6581da177e4SLinus Torvalds goto out; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 661bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 662bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 663bf40d343SSuresh Jayaraman size_t count, unsigned int flags) 664bf40d343SSuresh Jayaraman { 665bf40d343SSuresh Jayaraman struct dentry *dentry = filp->f_path.dentry; 666bf40d343SSuresh Jayaraman struct inode *inode = dentry->d_inode; 6677e381172SChuck Lever unsigned long written = 0; 668bf40d343SSuresh Jayaraman ssize_t ret; 669bf40d343SSuresh Jayaraman 670bf40d343SSuresh Jayaraman dprintk("NFS splice_write(%s/%s, %lu@%llu)\n", 671bf40d343SSuresh Jayaraman dentry->d_parent->d_name.name, dentry->d_name.name, 672bf40d343SSuresh Jayaraman (unsigned long) count, (unsigned long long) *ppos); 673bf40d343SSuresh Jayaraman 674bf40d343SSuresh Jayaraman /* 675bf40d343SSuresh Jayaraman * The combination of splice and an O_APPEND destination is disallowed. 676bf40d343SSuresh Jayaraman */ 677bf40d343SSuresh Jayaraman 678bf40d343SSuresh Jayaraman ret = generic_file_splice_write(pipe, filp, ppos, count, flags); 6797e381172SChuck Lever if (ret > 0) 6807e381172SChuck Lever written = ret; 6817e381172SChuck Lever 682bf40d343SSuresh Jayaraman if (ret >= 0 && nfs_need_sync_write(filp, inode)) { 683af7fa165STrond Myklebust int err = vfs_fsync(filp, 0); 684bf40d343SSuresh Jayaraman if (err < 0) 685bf40d343SSuresh Jayaraman ret = err; 686bf40d343SSuresh Jayaraman } 6877e381172SChuck Lever if (ret > 0) 6887e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 689bf40d343SSuresh Jayaraman return ret; 690bf40d343SSuresh Jayaraman } 691bf40d343SSuresh Jayaraman 6925eebde23SSuresh Jayaraman static int 6935eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6961da177e4SLinus Torvalds int status = 0; 69721ac19d4SSergey Vlasov unsigned int saved_type = fl->fl_type; 6981da177e4SLinus Torvalds 699039c4d7aSTrond Myklebust /* Try local locking first */ 7006d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 7016d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 7026d34ac19SJ. Bruce Fields /* found a conflict */ 703039c4d7aSTrond Myklebust goto out; 7041da177e4SLinus Torvalds } 70521ac19d4SSergey Vlasov fl->fl_type = saved_type; 706039c4d7aSTrond Myklebust 707039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 708039c4d7aSTrond Myklebust goto out_noconflict; 709039c4d7aSTrond Myklebust 7105eebde23SSuresh Jayaraman if (is_local) 711039c4d7aSTrond Myklebust goto out_noconflict; 712039c4d7aSTrond Myklebust 713039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 714039c4d7aSTrond Myklebust out: 7151da177e4SLinus Torvalds return status; 716039c4d7aSTrond Myklebust out_noconflict: 717039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 718039c4d7aSTrond Myklebust goto out; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds int res = 0; 7241da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 7251da177e4SLinus Torvalds case FL_POSIX: 7261da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 7271da177e4SLinus Torvalds break; 7281da177e4SLinus Torvalds case FL_FLOCK: 7291da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 7301da177e4SLinus Torvalds break; 7311da177e4SLinus Torvalds default: 7321da177e4SLinus Torvalds BUG(); 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds return res; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7375eebde23SSuresh Jayaraman static int 7385eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7391da177e4SLinus Torvalds { 7401da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7411da177e4SLinus Torvalds int status; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds /* 7441da177e4SLinus Torvalds * Flush all pending writes before doing anything 7451da177e4SLinus Torvalds * with locks.. 7461da177e4SLinus Torvalds */ 74729884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* NOTE: special case 7501da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 7511da177e4SLinus Torvalds * still need to complete the unlock. 7521da177e4SLinus Torvalds */ 7535eebde23SSuresh Jayaraman /* 7545eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7555eebde23SSuresh Jayaraman * "-olocal_lock=" 7565eebde23SSuresh Jayaraman */ 7575eebde23SSuresh Jayaraman if (!is_local) 7581da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 7591da177e4SLinus Torvalds else 7601da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7611da177e4SLinus Torvalds return status; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7645eebde23SSuresh Jayaraman static int 7656b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) { 7666b96724eSRicardo Labiaga return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000)); 7676b96724eSRicardo Labiaga } 7686b96724eSRicardo Labiaga 7696b96724eSRicardo Labiaga static int 7705eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7711da177e4SLinus Torvalds { 7721da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7731da177e4SLinus Torvalds int status; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * Flush all pending writes before doing anything 7771da177e4SLinus Torvalds * with locks.. 7781da177e4SLinus Torvalds */ 77929884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 78029884df0STrond Myklebust if (status != 0) 7811da177e4SLinus Torvalds goto out; 7821da177e4SLinus Torvalds 7835eebde23SSuresh Jayaraman /* 7845eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7855eebde23SSuresh Jayaraman * "-olocal_lock=" 7865eebde23SSuresh Jayaraman */ 7875eebde23SSuresh Jayaraman if (!is_local) 7881da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 789c4d7c402STrond Myklebust else 7901da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7911da177e4SLinus Torvalds if (status < 0) 7921da177e4SLinus Torvalds goto out; 7936b96724eSRicardo Labiaga 7941da177e4SLinus Torvalds /* 7956b96724eSRicardo Labiaga * Revalidate the cache if the server has time stamps granular 7966b96724eSRicardo Labiaga * enough to detect subsecond changes. Otherwise, clear the 7976b96724eSRicardo Labiaga * cache to prevent missing any changes. 7986b96724eSRicardo Labiaga * 7991da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 8001da177e4SLinus Torvalds */ 80129884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 8026b96724eSRicardo Labiaga if (!nfs_have_delegation(inode, FMODE_READ)) { 8036b96724eSRicardo Labiaga if (is_time_granular(&NFS_SERVER(inode)->time_delta)) 8046b96724eSRicardo Labiaga __nfs_revalidate_inode(NFS_SERVER(inode), inode); 8056b96724eSRicardo Labiaga else 8061da177e4SLinus Torvalds nfs_zap_caches(inode); 8076b96724eSRicardo Labiaga } 8081da177e4SLinus Torvalds out: 8091da177e4SLinus Torvalds return status; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * Lock a (portion of) a file 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 8161da177e4SLinus Torvalds { 8171da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8182116271aSTrond Myklebust int ret = -ENOLCK; 8195eebde23SSuresh Jayaraman int is_local = 0; 8201da177e4SLinus Torvalds 8216da24bc9SChuck Lever dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n", 8226da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 8236da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 8241da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 8251da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 8266da24bc9SChuck Lever 82791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* No mandatory locks over NFS */ 830dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 8312116271aSTrond Myklebust goto out_err; 8322116271aSTrond Myklebust 8335eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 8345eebde23SSuresh Jayaraman is_local = 1; 8355eebde23SSuresh Jayaraman 8362116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 8372116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 8382116271aSTrond Myklebust if (ret < 0) 8392116271aSTrond Myklebust goto out_err; 8402116271aSTrond Myklebust } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds if (IS_GETLK(cmd)) 8435eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8442116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 8455eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8462116271aSTrond Myklebust else 8475eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 8482116271aSTrond Myklebust out_err: 8492116271aSTrond Myklebust return ret; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* 8531da177e4SLinus Torvalds * Lock a (portion of) a file 8541da177e4SLinus Torvalds */ 8551da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 8561da177e4SLinus Torvalds { 8575eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 8585eebde23SSuresh Jayaraman int is_local = 0; 8595eebde23SSuresh Jayaraman 8606da24bc9SChuck Lever dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n", 8616da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 8626da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 8631da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 8661da177e4SLinus Torvalds return -ENOLCK; 8671da177e4SLinus Torvalds 8685eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 8695eebde23SSuresh Jayaraman is_local = 1; 8705eebde23SSuresh Jayaraman 8711da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 8721da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 8731da177e4SLinus Torvalds fl->fl_start = 0; 8741da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 8775eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 8785eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 8791da177e4SLinus Torvalds } 880370f6599SJ. Bruce Fields 8816da24bc9SChuck Lever /* 8826da24bc9SChuck Lever * There is no protocol support for leases, so we have no way to implement 8836da24bc9SChuck Lever * them correctly in the face of opens by other clients. 8846da24bc9SChuck Lever */ 885370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 886370f6599SJ. Bruce Fields { 8876da24bc9SChuck Lever dprintk("NFS: setlease(%s/%s, arg=%ld)\n", 8886da24bc9SChuck Lever file->f_path.dentry->d_parent->d_name.name, 8896da24bc9SChuck Lever file->f_path.dentry->d_name.name, arg); 890370f6599SJ. Bruce Fields return -EINVAL; 891370f6599SJ. Bruce Fields } 892