xref: /openbmc/linux/fs/nfs/direct.c (revision 40859d7ee64ed6bfad8a4e93f9bb5c1074afadff)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * There are important applications whose performance or correctness
91da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
101da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
111da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
121da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
131da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
141da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
151da177e4SLinus Torvalds  * need to cache the contents of a file.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
181da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
191da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
201da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
211da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
221da177e4SLinus Torvalds  * an application.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
251da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
261da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
271da177e4SLinus Torvalds  * an undocumented mount option.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
301da177e4SLinus Torvalds  * help from Andrew Morton.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
331da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
341da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
351da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
361da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <linux/config.h>
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/sched.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/smp_lock.h>
451da177e4SLinus Torvalds #include <linux/file.h>
461da177e4SLinus Torvalds #include <linux/pagemap.h>
471da177e4SLinus Torvalds #include <linux/kref.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <linux/nfs_fs.h>
501da177e4SLinus Torvalds #include <linux/nfs_page.h>
511da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <asm/system.h>
541da177e4SLinus Torvalds #include <asm/uaccess.h>
551da177e4SLinus Torvalds #include <asm/atomic.h>
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
581da177e4SLinus Torvalds #define MAX_DIRECTIO_SIZE	(4096UL << PAGE_SHIFT)
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static kmem_cache_t *nfs_direct_cachep;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds struct nfs_direct_req {
661da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
671da177e4SLinus Torvalds 	struct list_head	list;		/* nfs_read_data structs */
681da177e4SLinus Torvalds 	wait_queue_head_t	wait;		/* wait for i/o completion */
691da177e4SLinus Torvalds 	struct page **		pages;		/* pages in our buffer */
701da177e4SLinus Torvalds 	unsigned int		npages;		/* count of pages */
711da177e4SLinus Torvalds 	atomic_t		complete,	/* i/os we're waiting for */
721da177e4SLinus Torvalds 				count,		/* bytes actually processed */
731da177e4SLinus Torvalds 				error;		/* any reported error */
741da177e4SLinus Torvalds };
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /**
781da177e4SLinus Torvalds  * nfs_get_user_pages - find and set up pages underlying user's buffer
791da177e4SLinus Torvalds  * rw: direction (read or write)
801da177e4SLinus Torvalds  * user_addr: starting address of this segment of user's buffer
811da177e4SLinus Torvalds  * count: size of this segment
821da177e4SLinus Torvalds  * @pages: returned array of page struct pointers underlying user's buffer
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds static inline int
851da177e4SLinus Torvalds nfs_get_user_pages(int rw, unsigned long user_addr, size_t size,
861da177e4SLinus Torvalds 		struct page ***pages)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	int result = -ENOMEM;
891da177e4SLinus Torvalds 	unsigned long page_count;
901da177e4SLinus Torvalds 	size_t array_size;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	/* set an arbitrary limit to prevent type overflow */
931da177e4SLinus Torvalds 	/* XXX: this can probably be as large as INT_MAX */
941da177e4SLinus Torvalds 	if (size > MAX_DIRECTIO_SIZE) {
951da177e4SLinus Torvalds 		*pages = NULL;
961da177e4SLinus Torvalds 		return -EFBIG;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1001da177e4SLinus Torvalds 	page_count -= user_addr >> PAGE_SHIFT;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	array_size = (page_count * sizeof(struct page *));
1031da177e4SLinus Torvalds 	*pages = kmalloc(array_size, GFP_KERNEL);
1041da177e4SLinus Torvalds 	if (*pages) {
1051da177e4SLinus Torvalds 		down_read(&current->mm->mmap_sem);
1061da177e4SLinus Torvalds 		result = get_user_pages(current, current->mm, user_addr,
1071da177e4SLinus Torvalds 					page_count, (rw == READ), 0,
1081da177e4SLinus Torvalds 					*pages, NULL);
1091da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 	return result;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /**
1151da177e4SLinus Torvalds  * nfs_free_user_pages - tear down page struct array
1161da177e4SLinus Torvalds  * @pages: array of page struct pointers underlying target buffer
1171da177e4SLinus Torvalds  * @npages: number of pages in the array
1181da177e4SLinus Torvalds  * @do_dirty: dirty the pages as we release them
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds static void
1211da177e4SLinus Torvalds nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	int i;
1241da177e4SLinus Torvalds 	for (i = 0; i < npages; i++) {
1251da177e4SLinus Torvalds 		if (do_dirty)
1261da177e4SLinus Torvalds 			set_page_dirty_lock(pages[i]);
1271da177e4SLinus Torvalds 		page_cache_release(pages[i]);
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 	kfree(pages);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /**
1331da177e4SLinus Torvalds  * nfs_direct_req_release - release  nfs_direct_req structure for direct read
1341da177e4SLinus Torvalds  * @kref: kref object embedded in an nfs_direct_req structure
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
1401da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds /**
1441da177e4SLinus Torvalds  * nfs_direct_read_alloc - allocate nfs_read_data structures for direct read
1451da177e4SLinus Torvalds  * @count: count of bytes for the read request
1461da177e4SLinus Torvalds  * @rsize: local rsize setting
1471da177e4SLinus Torvalds  *
1481da177e4SLinus Torvalds  * Note we also set the number of requests we have in the dreq when we are
1491da177e4SLinus Torvalds  * done.  This prevents races with I/O completion so we will always wait
1501da177e4SLinus Torvalds  * until all requests have been dispatched and completed.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, unsigned int rsize)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct list_head *list;
1551da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1561da177e4SLinus Torvalds 	unsigned int reads = 0;
157*40859d7eSChuck Lever 	unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
1601da177e4SLinus Torvalds 	if (!dreq)
1611da177e4SLinus Torvalds 		return NULL;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	kref_init(&dreq->kref);
1641da177e4SLinus Torvalds 	init_waitqueue_head(&dreq->wait);
1651da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dreq->list);
1661da177e4SLinus Torvalds 	atomic_set(&dreq->count, 0);
1671da177e4SLinus Torvalds 	atomic_set(&dreq->error, 0);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	list = &dreq->list;
1701da177e4SLinus Torvalds 	for(;;) {
171*40859d7eSChuck Lever 		struct nfs_read_data *data = nfs_readdata_alloc(rpages);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 		if (unlikely(!data)) {
1741da177e4SLinus Torvalds 			while (!list_empty(list)) {
1751da177e4SLinus Torvalds 				data = list_entry(list->next,
1761da177e4SLinus Torvalds 						  struct nfs_read_data, pages);
1771da177e4SLinus Torvalds 				list_del(&data->pages);
1781da177e4SLinus Torvalds 				nfs_readdata_free(data);
1791da177e4SLinus Torvalds 			}
1801da177e4SLinus Torvalds 			kref_put(&dreq->kref, nfs_direct_req_release);
1811da177e4SLinus Torvalds 			return NULL;
1821da177e4SLinus Torvalds 		}
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
1851da177e4SLinus Torvalds 		list_add(&data->pages, list);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 		data->req = (struct nfs_page *) dreq;
1881da177e4SLinus Torvalds 		reads++;
1891da177e4SLinus Torvalds 		if (nbytes <= rsize)
1901da177e4SLinus Torvalds 			break;
1911da177e4SLinus Torvalds 		nbytes -= rsize;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	kref_get(&dreq->kref);
1941da177e4SLinus Torvalds 	atomic_set(&dreq->complete, reads);
1951da177e4SLinus Torvalds 	return dreq;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /**
1991da177e4SLinus Torvalds  * nfs_direct_read_result - handle a read reply for a direct read request
2001da177e4SLinus Torvalds  * @data: address of NFS READ operation control block
2011da177e4SLinus Torvalds  * @status: status of this NFS READ operation
2021da177e4SLinus Torvalds  *
2031da177e4SLinus Torvalds  * We must hold a reference to all the pages in this direct read request
2041da177e4SLinus Torvalds  * until the RPCs complete.  This could be long *after* we are woken up in
2051da177e4SLinus Torvalds  * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server).
2061da177e4SLinus Torvalds  */
2071da177e4SLinus Torvalds static void nfs_direct_read_result(struct nfs_read_data *data, int status)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (likely(status >= 0))
2121da177e4SLinus Torvalds 		atomic_add(data->res.count, &dreq->count);
2131da177e4SLinus Torvalds 	else
2141da177e4SLinus Torvalds 		atomic_set(&dreq->error, status);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	if (unlikely(atomic_dec_and_test(&dreq->complete))) {
2171da177e4SLinus Torvalds 		nfs_free_user_pages(dreq->pages, dreq->npages, 1);
2181da177e4SLinus Torvalds 		wake_up(&dreq->wait);
2191da177e4SLinus Torvalds 		kref_put(&dreq->kref, nfs_direct_req_release);
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /**
2241da177e4SLinus Torvalds  * nfs_direct_read_schedule - dispatch NFS READ operations for a direct read
2251da177e4SLinus Torvalds  * @dreq: address of nfs_direct_req struct for this request
2261da177e4SLinus Torvalds  * @inode: target inode
2271da177e4SLinus Torvalds  * @ctx: target file open context
2281da177e4SLinus Torvalds  * @user_addr: starting address of this segment of user's buffer
2291da177e4SLinus Torvalds  * @count: size of this segment
2301da177e4SLinus Torvalds  * @file_offset: offset in file to begin the operation
2311da177e4SLinus Torvalds  *
2321da177e4SLinus Torvalds  * For each nfs_read_data struct that was allocated on the list, dispatch
2331da177e4SLinus Torvalds  * an NFS READ operation
2341da177e4SLinus Torvalds  */
2351da177e4SLinus Torvalds static void nfs_direct_read_schedule(struct nfs_direct_req *dreq,
2361da177e4SLinus Torvalds 		struct inode *inode, struct nfs_open_context *ctx,
2371da177e4SLinus Torvalds 		unsigned long user_addr, size_t count, loff_t file_offset)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	struct list_head *list = &dreq->list;
2401da177e4SLinus Torvalds 	struct page **pages = dreq->pages;
2411da177e4SLinus Torvalds 	unsigned int curpage, pgbase;
2421da177e4SLinus Torvalds 	unsigned int rsize = NFS_SERVER(inode)->rsize;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	curpage = 0;
2451da177e4SLinus Torvalds 	pgbase = user_addr & ~PAGE_MASK;
2461da177e4SLinus Torvalds 	do {
2471da177e4SLinus Torvalds 		struct nfs_read_data *data;
2481da177e4SLinus Torvalds 		unsigned int bytes;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 		bytes = rsize;
2511da177e4SLinus Torvalds 		if (count < rsize)
2521da177e4SLinus Torvalds 			bytes = count;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 		data = list_entry(list->next, struct nfs_read_data, pages);
2551da177e4SLinus Torvalds 		list_del_init(&data->pages);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		data->inode = inode;
2581da177e4SLinus Torvalds 		data->cred = ctx->cred;
2591da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
2601da177e4SLinus Torvalds 		data->args.context = ctx;
2611da177e4SLinus Torvalds 		data->args.offset = file_offset;
2621da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
2631da177e4SLinus Torvalds 		data->args.pages = &pages[curpage];
2641da177e4SLinus Torvalds 		data->args.count = bytes;
2651da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
2661da177e4SLinus Torvalds 		data->res.eof = 0;
2671da177e4SLinus Torvalds 		data->res.count = bytes;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
2721da177e4SLinus Torvalds 		data->complete = nfs_direct_read_result;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 		lock_kernel();
2751da177e4SLinus Torvalds 		rpc_execute(&data->task);
2761da177e4SLinus Torvalds 		unlock_kernel();
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
2791da177e4SLinus Torvalds 				data->task.tk_pid,
2801da177e4SLinus Torvalds 				inode->i_sb->s_id,
2811da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
2821da177e4SLinus Torvalds 				bytes,
2831da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 		file_offset += bytes;
2861da177e4SLinus Torvalds 		pgbase += bytes;
2871da177e4SLinus Torvalds 		curpage += pgbase >> PAGE_SHIFT;
2881da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		count -= bytes;
2911da177e4SLinus Torvalds 	} while (count != 0);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /**
2951da177e4SLinus Torvalds  * nfs_direct_read_wait - wait for I/O completion for direct reads
2961da177e4SLinus Torvalds  * @dreq: request on which we are to wait
2971da177e4SLinus Torvalds  * @intr: whether or not this wait can be interrupted
2981da177e4SLinus Torvalds  *
2991da177e4SLinus Torvalds  * Collects and returns the final error value/byte-count.
3001da177e4SLinus Torvalds  */
3011da177e4SLinus Torvalds static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	int result = 0;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	if (intr) {
3061da177e4SLinus Torvalds 		result = wait_event_interruptible(dreq->wait,
3071da177e4SLinus Torvalds 					(atomic_read(&dreq->complete) == 0));
3081da177e4SLinus Torvalds 	} else {
3091da177e4SLinus Torvalds 		wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0));
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	if (!result)
3131da177e4SLinus Torvalds 		result = atomic_read(&dreq->error);
3141da177e4SLinus Torvalds 	if (!result)
3151da177e4SLinus Torvalds 		result = atomic_read(&dreq->count);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	kref_put(&dreq->kref, nfs_direct_req_release);
3181da177e4SLinus Torvalds 	return (ssize_t) result;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds /**
3221da177e4SLinus Torvalds  * nfs_direct_read_seg - Read in one iov segment.  Generate separate
3231da177e4SLinus Torvalds  *                        read RPCs for each "rsize" bytes.
3241da177e4SLinus Torvalds  * @inode: target inode
3251da177e4SLinus Torvalds  * @ctx: target file open context
3261da177e4SLinus Torvalds  * @user_addr: starting address of this segment of user's buffer
3271da177e4SLinus Torvalds  * @count: size of this segment
3281da177e4SLinus Torvalds  * @file_offset: offset in file to begin the operation
3291da177e4SLinus Torvalds  * @pages: array of addresses of page structs defining user's buffer
3301da177e4SLinus Torvalds  * @nr_pages: number of pages in the array
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  */
3331da177e4SLinus Torvalds static ssize_t nfs_direct_read_seg(struct inode *inode,
3341da177e4SLinus Torvalds 		struct nfs_open_context *ctx, unsigned long user_addr,
3351da177e4SLinus Torvalds 		size_t count, loff_t file_offset, struct page **pages,
3361da177e4SLinus Torvalds 		unsigned int nr_pages)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	ssize_t result;
3391da177e4SLinus Torvalds 	sigset_t oldset;
3401da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
3411da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
3441da177e4SLinus Torvalds 	if (!dreq)
3451da177e4SLinus Torvalds 		return -ENOMEM;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	dreq->pages = pages;
3481da177e4SLinus Torvalds 	dreq->npages = nr_pages;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
3511da177e4SLinus Torvalds 	nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count,
3521da177e4SLinus Torvalds 				 file_offset);
3531da177e4SLinus Torvalds 	result = nfs_direct_read_wait(dreq, clnt->cl_intr);
3541da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	return result;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /**
3601da177e4SLinus Torvalds  * nfs_direct_read - For each iov segment, map the user's buffer
3611da177e4SLinus Torvalds  *                   then generate read RPCs.
3621da177e4SLinus Torvalds  * @inode: target inode
3631da177e4SLinus Torvalds  * @ctx: target file open context
3641da177e4SLinus Torvalds  * @iov: array of vectors that define I/O buffer
3651da177e4SLinus Torvalds  * file_offset: offset in file to begin the operation
3661da177e4SLinus Torvalds  * nr_segs: size of iovec array
3671da177e4SLinus Torvalds  *
3681da177e4SLinus Torvalds  * We've already pushed out any non-direct writes so that this read
3691da177e4SLinus Torvalds  * will see them when we read from the server.
3701da177e4SLinus Torvalds  */
3711da177e4SLinus Torvalds static ssize_t
3721da177e4SLinus Torvalds nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx,
3731da177e4SLinus Torvalds 		const struct iovec *iov, loff_t file_offset,
3741da177e4SLinus Torvalds 		unsigned long nr_segs)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	ssize_t tot_bytes = 0;
3771da177e4SLinus Torvalds 	unsigned long seg = 0;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	while ((seg < nr_segs) && (tot_bytes >= 0)) {
3801da177e4SLinus Torvalds 		ssize_t result;
3811da177e4SLinus Torvalds 		int page_count;
3821da177e4SLinus Torvalds 		struct page **pages;
3831da177e4SLinus Torvalds 		const struct iovec *vec = &iov[seg++];
3841da177e4SLinus Torvalds 		unsigned long user_addr = (unsigned long) vec->iov_base;
3851da177e4SLinus Torvalds 		size_t size = vec->iov_len;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds                 page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
3881da177e4SLinus Torvalds                 if (page_count < 0) {
3891da177e4SLinus Torvalds                         nfs_free_user_pages(pages, 0, 0);
3901da177e4SLinus Torvalds 			if (tot_bytes > 0)
3911da177e4SLinus Torvalds 				break;
3921da177e4SLinus Torvalds                         return page_count;
3931da177e4SLinus Torvalds                 }
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 		result = nfs_direct_read_seg(inode, ctx, user_addr, size,
3961da177e4SLinus Torvalds 				file_offset, pages, page_count);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		if (result <= 0) {
3991da177e4SLinus Torvalds 			if (tot_bytes > 0)
4001da177e4SLinus Torvalds 				break;
4011da177e4SLinus Torvalds 			return result;
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 		tot_bytes += result;
4041da177e4SLinus Torvalds 		file_offset += result;
4051da177e4SLinus Torvalds 		if (result < size)
4061da177e4SLinus Torvalds 			break;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	return tot_bytes;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /**
4131da177e4SLinus Torvalds  * nfs_direct_write_seg - Write out one iov segment.  Generate separate
4141da177e4SLinus Torvalds  *                        write RPCs for each "wsize" bytes, then commit.
4151da177e4SLinus Torvalds  * @inode: target inode
4161da177e4SLinus Torvalds  * @ctx: target file open context
4171da177e4SLinus Torvalds  * user_addr: starting address of this segment of user's buffer
4181da177e4SLinus Torvalds  * count: size of this segment
4191da177e4SLinus Torvalds  * file_offset: offset in file to begin the operation
4201da177e4SLinus Torvalds  * @pages: array of addresses of page structs defining user's buffer
4211da177e4SLinus Torvalds  * nr_pages: size of pages array
4221da177e4SLinus Torvalds  */
4231da177e4SLinus Torvalds static ssize_t nfs_direct_write_seg(struct inode *inode,
4241da177e4SLinus Torvalds 		struct nfs_open_context *ctx, unsigned long user_addr,
4251da177e4SLinus Torvalds 		size_t count, loff_t file_offset, struct page **pages,
4261da177e4SLinus Torvalds 		int nr_pages)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	const unsigned int wsize = NFS_SERVER(inode)->wsize;
4291da177e4SLinus Torvalds 	size_t request;
4301da177e4SLinus Torvalds 	int curpage, need_commit;
4311da177e4SLinus Torvalds 	ssize_t result, tot_bytes;
4321da177e4SLinus Torvalds 	struct nfs_writeverf first_verf;
4331da177e4SLinus Torvalds 	struct nfs_write_data *wdata;
4341da177e4SLinus Torvalds 
435*40859d7eSChuck Lever 	wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
4361da177e4SLinus Torvalds 	if (!wdata)
4371da177e4SLinus Torvalds 		return -ENOMEM;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	wdata->inode = inode;
4401da177e4SLinus Torvalds 	wdata->cred = ctx->cred;
4411da177e4SLinus Torvalds 	wdata->args.fh = NFS_FH(inode);
4421da177e4SLinus Torvalds 	wdata->args.context = ctx;
4431da177e4SLinus Torvalds 	wdata->args.stable = NFS_UNSTABLE;
4441da177e4SLinus Torvalds 	if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
4451da177e4SLinus Torvalds 		wdata->args.stable = NFS_FILE_SYNC;
4461da177e4SLinus Torvalds 	wdata->res.fattr = &wdata->fattr;
4471da177e4SLinus Torvalds 	wdata->res.verf = &wdata->verf;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	nfs_begin_data_update(inode);
4501da177e4SLinus Torvalds retry:
4511da177e4SLinus Torvalds 	need_commit = 0;
4521da177e4SLinus Torvalds 	tot_bytes = 0;
4531da177e4SLinus Torvalds 	curpage = 0;
4541da177e4SLinus Torvalds 	request = count;
4551da177e4SLinus Torvalds 	wdata->args.pgbase = user_addr & ~PAGE_MASK;
4561da177e4SLinus Torvalds 	wdata->args.offset = file_offset;
4571da177e4SLinus Torvalds 	do {
4581da177e4SLinus Torvalds 		wdata->args.count = request;
4591da177e4SLinus Torvalds 		if (wdata->args.count > wsize)
4601da177e4SLinus Torvalds 			wdata->args.count = wsize;
4611da177e4SLinus Torvalds 		wdata->args.pages = &pages[curpage];
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
4641da177e4SLinus Torvalds 			wdata->args.count, (long long) wdata->args.offset,
4651da177e4SLinus Torvalds 			user_addr + tot_bytes, wdata->args.pgbase, curpage);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 		lock_kernel();
4681da177e4SLinus Torvalds 		result = NFS_PROTO(inode)->write(wdata);
4691da177e4SLinus Torvalds 		unlock_kernel();
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 		if (result <= 0) {
4721da177e4SLinus Torvalds 			if (tot_bytes > 0)
4731da177e4SLinus Torvalds 				break;
4741da177e4SLinus Torvalds 			goto out;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		if (tot_bytes == 0)
4781da177e4SLinus Torvalds 			memcpy(&first_verf.verifier, &wdata->verf.verifier,
4791da177e4SLinus Torvalds 						sizeof(first_verf.verifier));
4801da177e4SLinus Torvalds 		if (wdata->verf.committed != NFS_FILE_SYNC) {
4811da177e4SLinus Torvalds 			need_commit = 1;
4821da177e4SLinus Torvalds 			if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
4831da177e4SLinus Torvalds 					sizeof(first_verf.verifier)));
4841da177e4SLinus Torvalds 				goto sync_retry;
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 		tot_bytes += result;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 		/* in case of a short write: stop now, let the app recover */
4901da177e4SLinus Torvalds 		if (result < wdata->args.count)
4911da177e4SLinus Torvalds 			break;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 		wdata->args.offset += result;
4941da177e4SLinus Torvalds 		wdata->args.pgbase += result;
4951da177e4SLinus Torvalds 		curpage += wdata->args.pgbase >> PAGE_SHIFT;
4961da177e4SLinus Torvalds 		wdata->args.pgbase &= ~PAGE_MASK;
4971da177e4SLinus Torvalds 		request -= result;
4981da177e4SLinus Torvalds 	} while (request != 0);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/*
5011da177e4SLinus Torvalds 	 * Commit data written so far, even in the event of an error
5021da177e4SLinus Torvalds 	 */
5031da177e4SLinus Torvalds 	if (need_commit) {
5041da177e4SLinus Torvalds 		wdata->args.count = tot_bytes;
5051da177e4SLinus Torvalds 		wdata->args.offset = file_offset;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 		lock_kernel();
5081da177e4SLinus Torvalds 		result = NFS_PROTO(inode)->commit(wdata);
5091da177e4SLinus Torvalds 		unlock_kernel();
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		if (result < 0 || memcmp(&first_verf.verifier,
5121da177e4SLinus Torvalds 					 &wdata->verf.verifier,
5131da177e4SLinus Torvalds 					 sizeof(first_verf.verifier)) != 0)
5141da177e4SLinus Torvalds 			goto sync_retry;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 	result = tot_bytes;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds out:
519951a143bSTrond Myklebust 	nfs_end_data_update(inode);
5201da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
5211da177e4SLinus Torvalds 	return result;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds sync_retry:
5241da177e4SLinus Torvalds 	wdata->args.stable = NFS_FILE_SYNC;
5251da177e4SLinus Torvalds 	goto retry;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /**
5291da177e4SLinus Torvalds  * nfs_direct_write - For each iov segment, map the user's buffer
5301da177e4SLinus Torvalds  *                    then generate write and commit RPCs.
5311da177e4SLinus Torvalds  * @inode: target inode
5321da177e4SLinus Torvalds  * @ctx: target file open context
5331da177e4SLinus Torvalds  * @iov: array of vectors that define I/O buffer
5341da177e4SLinus Torvalds  * file_offset: offset in file to begin the operation
5351da177e4SLinus Torvalds  * nr_segs: size of iovec array
5361da177e4SLinus Torvalds  *
5371da177e4SLinus Torvalds  * Upon return, generic_file_direct_IO invalidates any cached pages
5381da177e4SLinus Torvalds  * that non-direct readers might access, so they will pick up these
5391da177e4SLinus Torvalds  * writes immediately.
5401da177e4SLinus Torvalds  */
5411da177e4SLinus Torvalds static ssize_t nfs_direct_write(struct inode *inode,
5421da177e4SLinus Torvalds 		struct nfs_open_context *ctx, const struct iovec *iov,
5431da177e4SLinus Torvalds 		loff_t file_offset, unsigned long nr_segs)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	ssize_t tot_bytes = 0;
5461da177e4SLinus Torvalds 	unsigned long seg = 0;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	while ((seg < nr_segs) && (tot_bytes >= 0)) {
5491da177e4SLinus Torvalds 		ssize_t result;
5501da177e4SLinus Torvalds 		int page_count;
5511da177e4SLinus Torvalds 		struct page **pages;
5521da177e4SLinus Torvalds 		const struct iovec *vec = &iov[seg++];
5531da177e4SLinus Torvalds 		unsigned long user_addr = (unsigned long) vec->iov_base;
5541da177e4SLinus Torvalds 		size_t size = vec->iov_len;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds                 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
5571da177e4SLinus Torvalds                 if (page_count < 0) {
5581da177e4SLinus Torvalds                         nfs_free_user_pages(pages, 0, 0);
5591da177e4SLinus Torvalds 			if (tot_bytes > 0)
5601da177e4SLinus Torvalds 				break;
5611da177e4SLinus Torvalds                         return page_count;
5621da177e4SLinus Torvalds                 }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 		result = nfs_direct_write_seg(inode, ctx, user_addr, size,
5651da177e4SLinus Torvalds 				file_offset, pages, page_count);
5661da177e4SLinus Torvalds 		nfs_free_user_pages(pages, page_count, 0);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		if (result <= 0) {
5691da177e4SLinus Torvalds 			if (tot_bytes > 0)
5701da177e4SLinus Torvalds 				break;
5711da177e4SLinus Torvalds 			return result;
5721da177e4SLinus Torvalds 		}
5731da177e4SLinus Torvalds 		tot_bytes += result;
5741da177e4SLinus Torvalds 		file_offset += result;
5751da177e4SLinus Torvalds 		if (result < size)
5761da177e4SLinus Torvalds 			break;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 	return tot_bytes;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /**
5821da177e4SLinus Torvalds  * nfs_direct_IO - NFS address space operation for direct I/O
5831da177e4SLinus Torvalds  * rw: direction (read or write)
5841da177e4SLinus Torvalds  * @iocb: target I/O control block
5851da177e4SLinus Torvalds  * @iov: array of vectors that define I/O buffer
5861da177e4SLinus Torvalds  * file_offset: offset in file to begin the operation
5871da177e4SLinus Torvalds  * nr_segs: size of iovec array
5881da177e4SLinus Torvalds  *
5891da177e4SLinus Torvalds  */
5901da177e4SLinus Torvalds ssize_t
5911da177e4SLinus Torvalds nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
5921da177e4SLinus Torvalds 		loff_t file_offset, unsigned long nr_segs)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	ssize_t result = -EINVAL;
5951da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
5961da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5971da177e4SLinus Torvalds 	struct dentry *dentry = file->f_dentry;
5981da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/*
6011da177e4SLinus Torvalds 	 * No support for async yet
6021da177e4SLinus Torvalds 	 */
6031da177e4SLinus Torvalds 	if (!is_sync_kiocb(iocb))
6041da177e4SLinus Torvalds 		return result;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	ctx = (struct nfs_open_context *)file->private_data;
6071da177e4SLinus Torvalds 	switch (rw) {
6081da177e4SLinus Torvalds 	case READ:
6091da177e4SLinus Torvalds 		dprintk("NFS: direct_IO(read) (%s) off/no(%Lu/%lu)\n",
6101da177e4SLinus Torvalds 				dentry->d_name.name, file_offset, nr_segs);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 		result = nfs_direct_read(inode, ctx, iov,
6131da177e4SLinus Torvalds 						file_offset, nr_segs);
6141da177e4SLinus Torvalds 		break;
6151da177e4SLinus Torvalds 	case WRITE:
6161da177e4SLinus Torvalds 		dprintk("NFS: direct_IO(write) (%s) off/no(%Lu/%lu)\n",
6171da177e4SLinus Torvalds 				dentry->d_name.name, file_offset, nr_segs);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		result = nfs_direct_write(inode, ctx, iov,
6201da177e4SLinus Torvalds 						file_offset, nr_segs);
6211da177e4SLinus Torvalds 		break;
6221da177e4SLinus Torvalds 	default:
6231da177e4SLinus Torvalds 		break;
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 	return result;
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds /**
6291da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
6301da177e4SLinus Torvalds  * @iocb: target I/O control block
6311da177e4SLinus Torvalds  * @buf: user's buffer into which to read data
6321da177e4SLinus Torvalds  * count: number of bytes to read
6331da177e4SLinus Torvalds  * pos: byte offset in file where reading starts
6341da177e4SLinus Torvalds  *
6351da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
6361da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
6371da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
6381da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
6391da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
6401da177e4SLinus Torvalds  * READ where the file size could change.  So our preference is simply
6411da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
6421da177e4SLinus Torvalds  * care of managing the end of file boundary.
6431da177e4SLinus Torvalds  *
6441da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
6451da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
6461da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
6471da177e4SLinus Torvalds  * cache.
6481da177e4SLinus Torvalds  */
6491da177e4SLinus Torvalds ssize_t
6501da177e4SLinus Torvalds nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
6531da177e4SLinus Torvalds 	loff_t *ppos = &iocb->ki_pos;
6541da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
6551da177e4SLinus Torvalds 	struct nfs_open_context *ctx =
6561da177e4SLinus Torvalds 			(struct nfs_open_context *) file->private_data;
6571da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
6581da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
6591da177e4SLinus Torvalds 	struct iovec iov = {
6601da177e4SLinus Torvalds 		.iov_base = buf,
6611da177e4SLinus Torvalds 		.iov_len = count,
6621da177e4SLinus Torvalds 	};
6631da177e4SLinus Torvalds 
664ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
6650bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
6660bbacc40SChuck Lever 		file->f_dentry->d_name.name,
667ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (!is_sync_kiocb(iocb))
6701da177e4SLinus Torvalds 		goto out;
6711da177e4SLinus Torvalds 	if (count < 0)
6721da177e4SLinus Torvalds 		goto out;
6731da177e4SLinus Torvalds 	retval = -EFAULT;
6741da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
6751da177e4SLinus Torvalds 		goto out;
6761da177e4SLinus Torvalds 	retval = 0;
6771da177e4SLinus Torvalds 	if (!count)
6781da177e4SLinus Torvalds 		goto out;
6791da177e4SLinus Torvalds 
68029884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
6811da177e4SLinus Torvalds 	if (retval)
6821da177e4SLinus Torvalds 		goto out;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
6851da177e4SLinus Torvalds 	if (retval > 0)
6861da177e4SLinus Torvalds 		*ppos = pos + retval;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds out:
6891da177e4SLinus Torvalds 	return retval;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /**
6931da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
6941da177e4SLinus Torvalds  * @iocb: target I/O control block
6951da177e4SLinus Torvalds  * @buf: user's buffer from which to write data
6961da177e4SLinus Torvalds  * count: number of bytes to write
6971da177e4SLinus Torvalds  * pos: byte offset in file where writing starts
6981da177e4SLinus Torvalds  *
6991da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
7001da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
7011da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
7021da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
7031da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
7041da177e4SLinus Torvalds  * parameter checking and report problems.
7051da177e4SLinus Torvalds  *
7061da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
7071da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
7081da177e4SLinus Torvalds  *
7091da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
7101da177e4SLinus Torvalds  *
7111da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
7121da177e4SLinus Torvalds  * readers of this file.
7131da177e4SLinus Torvalds  *
7141da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
7151da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
7161da177e4SLinus Torvalds  */
7171da177e4SLinus Torvalds ssize_t
7181da177e4SLinus Torvalds nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
7191da177e4SLinus Torvalds {
720ce1a8e67SChuck Lever 	ssize_t retval;
7211da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
7221da177e4SLinus Torvalds 	struct nfs_open_context *ctx =
7231da177e4SLinus Torvalds 			(struct nfs_open_context *) file->private_data;
7241da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
7251da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
7261da177e4SLinus Torvalds 	struct iovec iov = {
7271da177e4SLinus Torvalds 		.iov_base = (char __user *)buf,
7281da177e4SLinus Torvalds 	};
7291da177e4SLinus Torvalds 
730ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
7310bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
732ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
733ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
7341da177e4SLinus Torvalds 
735ce1a8e67SChuck Lever 	retval = -EINVAL;
7361da177e4SLinus Torvalds 	if (!is_sync_kiocb(iocb))
7371da177e4SLinus Torvalds 		goto out;
738ce1a8e67SChuck Lever 
739ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
740ce1a8e67SChuck Lever 	if (retval)
7411da177e4SLinus Torvalds 		goto out;
742ce1a8e67SChuck Lever 
743ce1a8e67SChuck Lever 	retval = -EINVAL;
744ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
7451da177e4SLinus Torvalds 		goto out;
7461da177e4SLinus Torvalds 	retval = 0;
7471da177e4SLinus Torvalds 	if (!count)
7481da177e4SLinus Torvalds 		goto out;
749ce1a8e67SChuck Lever 	iov.iov_len = count,
750ce1a8e67SChuck Lever 
751ce1a8e67SChuck Lever 	retval = -EFAULT;
752ce1a8e67SChuck Lever 	if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
753ce1a8e67SChuck Lever 		goto out;
7541da177e4SLinus Torvalds 
75529884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
7561da177e4SLinus Torvalds 	if (retval)
7571da177e4SLinus Torvalds 		goto out;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
7601da177e4SLinus Torvalds 	if (mapping->nrpages)
7611da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
7621da177e4SLinus Torvalds 	if (retval > 0)
763ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds out:
7661da177e4SLinus Torvalds 	return retval;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds int nfs_init_directcache(void)
7701da177e4SLinus Torvalds {
7711da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
7721da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
7731da177e4SLinus Torvalds 						0, SLAB_RECLAIM_ACCOUNT,
7741da177e4SLinus Torvalds 						NULL, NULL);
7751da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
7761da177e4SLinus Torvalds 		return -ENOMEM;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	return 0;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds void nfs_destroy_directcache(void)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_direct_cachep))
7841da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
7851da177e4SLinus Torvalds }
786