]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/nfs/direct.c
Merge remote-tracking branch 'ubifs/linux-next'
[karo-tx-linux.git] / fs / nfs / direct.c
1 /*
2  * linux/fs/nfs/direct.c
3  *
4  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
5  *
6  * High-performance uncached I/O for the Linux NFS client
7  *
8  * There are important applications whose performance or correctness
9  * depends on uncached access to file data.  Database clusters
10  * (multiple copies of the same instance running on separate hosts)
11  * implement their own cache coherency protocol that subsumes file
12  * system cache protocols.  Applications that process datasets
13  * considerably larger than the client's memory do not always benefit
14  * from a local cache.  A streaming video server, for instance, has no
15  * need to cache the contents of a file.
16  *
17  * When an application requests uncached I/O, all read and write requests
18  * are made directly to the server; data stored or fetched via these
19  * requests is not cached in the Linux page cache.  The client does not
20  * correct unaligned requests from applications.  All requested bytes are
21  * held on permanent storage before a direct write system call returns to
22  * an application.
23  *
24  * Solaris implements an uncached I/O facility called directio() that
25  * is used for backups and sequential I/O to very large files.  Solaris
26  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
27  * an undocumented mount option.
28  *
29  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30  * help from Andrew Morton.
31  *
32  * 18 Dec 2001  Initial implementation for 2.4  --cel
33  * 08 Jul 2002  Version for 2.4.19, with bug fixes --trondmy
34  * 08 Jun 2003  Port to 2.5 APIs  --cel
35  * 31 Mar 2004  Handle direct I/O without VFS support  --cel
36  * 15 Sep 2004  Parallel async reads  --cel
37  * 04 May 2005  support O_DIRECT with aio  --cel
38  *
39  */
40
41 #include <linux/errno.h>
42 #include <linux/sched.h>
43 #include <linux/kernel.h>
44 #include <linux/file.h>
45 #include <linux/pagemap.h>
46 #include <linux/kref.h>
47 #include <linux/slab.h>
48 #include <linux/task_io_accounting_ops.h>
49 #include <linux/module.h>
50
51 #include <linux/nfs_fs.h>
52 #include <linux/nfs_page.h>
53 #include <linux/sunrpc/clnt.h>
54
55 #include <asm/uaccess.h>
56 #include <linux/atomic.h>
57
58 #include "internal.h"
59 #include "iostat.h"
60 #include "pnfs.h"
61
62 #define NFSDBG_FACILITY         NFSDBG_VFS
63
64 static struct kmem_cache *nfs_direct_cachep;
65
66 /*
67  * This represents a set of asynchronous requests that we're waiting on
68  */
69 struct nfs_direct_req {
70         struct kref             kref;           /* release manager */
71
72         /* I/O parameters */
73         struct nfs_open_context *ctx;           /* file open context info */
74         struct nfs_lock_context *l_ctx;         /* Lock context info */
75         struct kiocb *          iocb;           /* controlling i/o request */
76         struct inode *          inode;          /* target file of i/o */
77
78         /* completion state */
79         atomic_t                io_count;       /* i/os we're waiting for */
80         spinlock_t              lock;           /* protect completion state */
81         ssize_t                 count,          /* bytes actually processed */
82                                 bytes_left,     /* bytes left to be sent */
83                                 error;          /* any reported error */
84         struct completion       completion;     /* wait for i/o completion */
85
86         /* commit state */
87         struct nfs_mds_commit_info mds_cinfo;   /* Storage for cinfo */
88         struct pnfs_ds_commit_info ds_cinfo;    /* Storage for cinfo */
89         struct work_struct      work;
90         int                     flags;
91 #define NFS_ODIRECT_DO_COMMIT           (1)     /* an unstable reply was received */
92 #define NFS_ODIRECT_RESCHED_WRITES      (2)     /* write verification failed */
93 #define NFS_ODIRECT_MARK_DIRTY          (4)     /* mark read pages dirty */
94         struct nfs_writeverf    verf;           /* unstable write verifier */
95 };
96
97 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
98 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
99 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
100 static void nfs_direct_write_schedule_work(struct work_struct *work);
101
102 static inline void get_dreq(struct nfs_direct_req *dreq)
103 {
104         atomic_inc(&dreq->io_count);
105 }
106
107 static inline int put_dreq(struct nfs_direct_req *dreq)
108 {
109         return atomic_dec_and_test(&dreq->io_count);
110 }
111
112 /**
113  * nfs_direct_IO - NFS address space operation for direct I/O
114  * @rw: direction (read or write)
115  * @iocb: target I/O control block
116  * @iter: array of vectors that define I/O buffer
117  * @pos: offset in file to begin the operation
118  * @nr_segs: size of iovec array
119  *
120  * The presence of this routine in the address space ops vector means
121  * the NFS client supports direct I/O. However, we shunt off direct
122  * read and write requests before the VFS gets them, so this method
123  * should never be called.
124  */
125 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
126                       loff_t pos)
127 {
128         dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
129                         iocb->ki_filp->f_path.dentry->d_name.name,
130                         (long long) pos, iter->nr_segs);
131
132         return -EINVAL;
133 }
134
135 static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
136 {
137         unsigned int i;
138         for (i = 0; i < npages; i++)
139                 page_cache_release(pages[i]);
140 }
141
142 void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
143                               struct nfs_direct_req *dreq)
144 {
145         cinfo->lock = &dreq->lock;
146         cinfo->mds = &dreq->mds_cinfo;
147         cinfo->ds = &dreq->ds_cinfo;
148         cinfo->dreq = dreq;
149         cinfo->completion_ops = &nfs_direct_commit_completion_ops;
150 }
151
152 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
153 {
154         struct nfs_direct_req *dreq;
155
156         dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
157         if (!dreq)
158                 return NULL;
159
160         kref_init(&dreq->kref);
161         kref_get(&dreq->kref);
162         init_completion(&dreq->completion);
163         INIT_LIST_HEAD(&dreq->mds_cinfo.list);
164         INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
165         spin_lock_init(&dreq->lock);
166
167         return dreq;
168 }
169
170 static void nfs_direct_req_free(struct kref *kref)
171 {
172         struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
173
174         if (dreq->l_ctx != NULL)
175                 nfs_put_lock_context(dreq->l_ctx);
176         if (dreq->ctx != NULL)
177                 put_nfs_open_context(dreq->ctx);
178         kmem_cache_free(nfs_direct_cachep, dreq);
179 }
180
181 static void nfs_direct_req_release(struct nfs_direct_req *dreq)
182 {
183         kref_put(&dreq->kref, nfs_direct_req_free);
184 }
185
186 ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
187 {
188         return dreq->bytes_left;
189 }
190 EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
191
192 /*
193  * Collects and returns the final error value/byte-count.
194  */
195 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
196 {
197         ssize_t result = -EIOCBQUEUED;
198
199         /* Async requests don't wait here */
200         if (dreq->iocb)
201                 goto out;
202
203         result = wait_for_completion_killable(&dreq->completion);
204
205         if (!result)
206                 result = dreq->error;
207         if (!result)
208                 result = dreq->count;
209
210 out:
211         return (ssize_t) result;
212 }
213
214 /*
215  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
216  * the iocb is still valid here if this is a synchronous request.
217  */
218 static void nfs_direct_complete(struct nfs_direct_req *dreq)
219 {
220         if (dreq->iocb) {
221                 long res = (long) dreq->error;
222                 if (!res)
223                         res = (long) dreq->count;
224                 aio_complete(dreq->iocb, res, 0);
225         }
226         complete_all(&dreq->completion);
227
228         nfs_direct_req_release(dreq);
229 }
230
231 static void nfs_direct_readpage_release(struct nfs_page *req)
232 {
233         dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
234                 req->wb_context->dentry->d_inode->i_sb->s_id,
235                 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
236                 req->wb_bytes,
237                 (long long)req_offset(req));
238         nfs_release_request(req);
239 }
240
241 static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
242 {
243         unsigned long bytes = 0;
244         struct nfs_direct_req *dreq = hdr->dreq;
245
246         if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
247                 goto out_put;
248
249         spin_lock(&dreq->lock);
250         if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
251                 dreq->error = hdr->error;
252         else
253                 dreq->count += hdr->good_bytes;
254         spin_unlock(&dreq->lock);
255
256         while (!list_empty(&hdr->pages)) {
257                 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
258                 struct page *page = req->wb_page;
259
260                 if ((dreq->flags & NFS_ODIRECT_MARK_DIRTY) &&
261                     !PageCompound(page) && bytes < hdr->good_bytes)
262                         set_page_dirty(page);
263                 bytes += req->wb_bytes;
264                 nfs_list_remove_request(req);
265                 nfs_direct_readpage_release(req);
266         }
267 out_put:
268         if (put_dreq(dreq))
269                 nfs_direct_complete(dreq);
270         hdr->release(hdr);
271 }
272
273 static void nfs_read_sync_pgio_error(struct list_head *head)
274 {
275         struct nfs_page *req;
276
277         while (!list_empty(head)) {
278                 req = nfs_list_entry(head->next);
279                 nfs_list_remove_request(req);
280                 nfs_release_request(req);
281         }
282 }
283
284 static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
285 {
286         get_dreq(hdr->dreq);
287 }
288
289 static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
290         .error_cleanup = nfs_read_sync_pgio_error,
291         .init_hdr = nfs_direct_pgio_init,
292         .completion = nfs_direct_read_completion,
293 };
294
295 /*
296  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
297  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
298  * bail and stop sending more reads.  Read length accounting is
299  * handled automatically by nfs_direct_read_result().  Otherwise, if
300  * no requests have been sent, just return an error.
301  */
302 static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
303                                                 const struct iovec *iov,
304                                                 loff_t pos)
305 {
306         struct nfs_direct_req *dreq = desc->pg_dreq;
307         struct nfs_open_context *ctx = dreq->ctx;
308         struct inode *inode = ctx->dentry->d_inode;
309         unsigned long user_addr = (unsigned long)iov->iov_base;
310         size_t count = iov->iov_len;
311         size_t rsize = NFS_SERVER(inode)->rsize;
312         unsigned int pgbase;
313         int result;
314         ssize_t started = 0;
315         struct page **pagevec = NULL;
316         unsigned int npages;
317
318         do {
319                 size_t bytes;
320                 int i;
321
322                 pgbase = user_addr & ~PAGE_MASK;
323                 bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
324
325                 result = -ENOMEM;
326                 npages = nfs_page_array_len(pgbase, bytes);
327                 if (!pagevec)
328                         pagevec = kmalloc(npages * sizeof(struct page *),
329                                           GFP_KERNEL);
330                 if (!pagevec)
331                         break;
332                 down_read(&current->mm->mmap_sem);
333                 result = get_user_pages(current, current->mm, user_addr,
334                                         npages, 1, 0, pagevec, NULL);
335                 up_read(&current->mm->mmap_sem);
336                 if (result < 0)
337                         break;
338                 if ((unsigned)result < npages) {
339                         bytes = result * PAGE_SIZE;
340                         if (bytes <= pgbase) {
341                                 nfs_direct_release_pages(pagevec, result);
342                                 break;
343                         }
344                         bytes -= pgbase;
345                         npages = result;
346                 }
347
348                 for (i = 0; i < npages; i++) {
349                         struct nfs_page *req;
350                         unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
351                         /* XXX do we need to do the eof zeroing found in async_filler? */
352                         req = nfs_create_request(dreq->ctx, dreq->inode,
353                                                  pagevec[i],
354                                                  pgbase, req_len);
355                         if (IS_ERR(req)) {
356                                 result = PTR_ERR(req);
357                                 break;
358                         }
359                         req->wb_index = pos >> PAGE_SHIFT;
360                         req->wb_offset = pos & ~PAGE_MASK;
361                         if (!nfs_pageio_add_request(desc, req)) {
362                                 result = desc->pg_error;
363                                 nfs_release_request(req);
364                                 break;
365                         }
366                         pgbase = 0;
367                         bytes -= req_len;
368                         started += req_len;
369                         user_addr += req_len;
370                         pos += req_len;
371                         count -= req_len;
372                         dreq->bytes_left -= req_len;
373                 }
374                 /* The nfs_page now hold references to these pages */
375                 nfs_direct_release_pages(pagevec, npages);
376         } while (count != 0 && result >= 0);
377
378         kfree(pagevec);
379
380         if (started)
381                 return started;
382         return result < 0 ? (ssize_t) result : -EFAULT;
383 }
384
385 static ssize_t nfs_direct_do_schedule_read_iovec(
386                 struct nfs_pageio_descriptor *desc, const struct iovec *iov,
387                 unsigned long nr_segs, loff_t pos)
388 {
389         ssize_t result = -EINVAL;
390         size_t requested_bytes = 0;
391         unsigned long seg;
392
393         for (seg = 0; seg < nr_segs; seg++) {
394                 const struct iovec *vec = &iov[seg];
395                 result = nfs_direct_read_schedule_segment(desc, vec, pos);
396                 if (result < 0)
397                         break;
398                 requested_bytes += result;
399                 if ((size_t)result < vec->iov_len)
400                         break;
401                 pos += vec->iov_len;
402         }
403         if (requested_bytes)
404                 return requested_bytes;
405
406         return result < 0 ? result : -EIO;
407 }
408
409 #ifdef CONFIG_BLOCK
410 static ssize_t nfs_direct_do_schedule_read_bvec(
411                 struct nfs_pageio_descriptor *desc,
412                 struct bio_vec *bvec, unsigned long nr_segs, loff_t pos)
413 {
414         struct nfs_direct_req *dreq = desc->pg_dreq;
415         struct nfs_open_context *ctx = dreq->ctx;
416         struct inode *inode = ctx->dentry->d_inode;
417         ssize_t result = -EINVAL;
418         size_t requested_bytes = 0;
419         unsigned long seg;
420         struct nfs_page *req;
421         unsigned int req_len;
422
423         for (seg = 0; seg < nr_segs; seg++) {
424                 result = -EIO;
425                 req_len = bvec[seg].bv_len;
426                 req = nfs_create_request(ctx, inode,
427                                          bvec[seg].bv_page,
428                                          bvec[seg].bv_offset, req_len);
429                 if (IS_ERR(req)) {
430                         result = PTR_ERR(req);
431                         break;
432                 }
433                 req->wb_index = pos >> PAGE_SHIFT;
434                 req->wb_offset = pos & ~PAGE_MASK;
435                 if (!nfs_pageio_add_request(desc, req)) {
436                         result = desc->pg_error;
437                         nfs_release_request(req);
438                         break;
439                 }
440                 requested_bytes += req_len;
441                 pos += req_len;
442         }
443
444         if (requested_bytes)
445                 return requested_bytes;
446
447         return result < 0 ? result : -EIO;
448 }
449 #endif /* CONFIG_BLOCK */
450
451 static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq,
452                                         struct iov_iter *iter, loff_t pos)
453 {
454         struct nfs_pageio_descriptor desc;
455         ssize_t result;
456
457         NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
458                              &nfs_direct_read_completion_ops);
459         get_dreq(dreq);
460         desc.pg_dreq = dreq;
461
462         if (iov_iter_has_iovec(iter)) {
463                 result = nfs_direct_do_schedule_read_iovec(&desc,
464                                 iov_iter_iovec(iter), iter->nr_segs, pos);
465 #ifdef CONFIG_BLOCK
466         } else if (iov_iter_has_bvec(iter)) {
467                 result = nfs_direct_do_schedule_read_bvec(&desc,
468                                 iov_iter_bvec(iter), iter->nr_segs, pos);
469 #endif
470         } else
471                 BUG();
472
473         nfs_pageio_complete(&desc);
474
475         /*
476          * If no bytes were started, return the error, and let the
477          * generic layer handle the completion.
478          */
479         if (result < 0) {
480                 nfs_direct_req_release(dreq);
481                 return result;
482         }
483
484         if (put_dreq(dreq))
485                 nfs_direct_complete(dreq);
486         return 0;
487 }
488
489 static ssize_t nfs_direct_read(struct kiocb *iocb, struct iov_iter *iter,
490                                loff_t pos)
491 {
492         ssize_t result = -ENOMEM;
493         struct inode *inode = iocb->ki_filp->f_mapping->host;
494         struct nfs_direct_req *dreq;
495         struct nfs_lock_context *l_ctx;
496
497         dreq = nfs_direct_req_alloc();
498         if (dreq == NULL)
499                 goto out;
500
501         dreq->inode = inode;
502         dreq->bytes_left = iov_iter_count(iter);
503         dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
504         l_ctx = nfs_get_lock_context(dreq->ctx);
505         if (IS_ERR(l_ctx)) {
506                 result = PTR_ERR(l_ctx);
507                 goto out_release;
508         }
509         dreq->l_ctx = l_ctx;
510         if (!is_sync_kiocb(iocb))
511                 dreq->iocb = iocb;
512
513         NFS_I(inode)->read_io += iov_iter_count(iter);
514         result = nfs_direct_read_schedule(dreq, iter, pos);
515         if (!result)
516                 result = nfs_direct_wait(dreq);
517 out_release:
518         nfs_direct_req_release(dreq);
519 out:
520         return result;
521 }
522
523 static void nfs_inode_dio_write_done(struct inode *inode)
524 {
525         nfs_zap_mapping(inode, inode->i_mapping);
526         inode_dio_done(inode);
527 }
528
529 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
530 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
531 {
532         struct nfs_pageio_descriptor desc;
533         struct nfs_page *req, *tmp;
534         LIST_HEAD(reqs);
535         struct nfs_commit_info cinfo;
536         LIST_HEAD(failed);
537
538         nfs_init_cinfo_from_dreq(&cinfo, dreq);
539         pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
540         spin_lock(cinfo.lock);
541         nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
542         spin_unlock(cinfo.lock);
543
544         dreq->count = 0;
545         get_dreq(dreq);
546
547         NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
548                               &nfs_direct_write_completion_ops);
549         desc.pg_dreq = dreq;
550
551         list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
552                 if (!nfs_pageio_add_request(&desc, req)) {
553                         nfs_list_remove_request(req);
554                         nfs_list_add_request(req, &failed);
555                         spin_lock(cinfo.lock);
556                         dreq->flags = 0;
557                         dreq->error = -EIO;
558                         spin_unlock(cinfo.lock);
559                 }
560                 nfs_release_request(req);
561         }
562         nfs_pageio_complete(&desc);
563
564         while (!list_empty(&failed)) {
565                 req = nfs_list_entry(failed.next);
566                 nfs_list_remove_request(req);
567                 nfs_unlock_and_release_request(req);
568         }
569
570         if (put_dreq(dreq))
571                 nfs_direct_write_complete(dreq, dreq->inode);
572 }
573
574 static void nfs_direct_commit_complete(struct nfs_commit_data *data)
575 {
576         struct nfs_direct_req *dreq = data->dreq;
577         struct nfs_commit_info cinfo;
578         struct nfs_page *req;
579         int status = data->task.tk_status;
580
581         nfs_init_cinfo_from_dreq(&cinfo, dreq);
582         if (status < 0) {
583                 dprintk("NFS: %5u commit failed with error %d.\n",
584                         data->task.tk_pid, status);
585                 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
586         } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
587                 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
588                 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
589         }
590
591         dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
592         while (!list_empty(&data->pages)) {
593                 req = nfs_list_entry(data->pages.next);
594                 nfs_list_remove_request(req);
595                 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
596                         /* Note the rewrite will go through mds */
597                         nfs_mark_request_commit(req, NULL, &cinfo);
598                 } else
599                         nfs_release_request(req);
600                 nfs_unlock_and_release_request(req);
601         }
602
603         if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
604                 nfs_direct_write_complete(dreq, data->inode);
605 }
606
607 static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
608 {
609         /* There is no lock to clear */
610 }
611
612 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
613         .completion = nfs_direct_commit_complete,
614         .error_cleanup = nfs_direct_error_cleanup,
615 };
616
617 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
618 {
619         int res;
620         struct nfs_commit_info cinfo;
621         LIST_HEAD(mds_list);
622
623         nfs_init_cinfo_from_dreq(&cinfo, dreq);
624         nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
625         res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
626         if (res < 0) /* res == -ENOMEM */
627                 nfs_direct_write_reschedule(dreq);
628 }
629
630 static void nfs_direct_write_schedule_work(struct work_struct *work)
631 {
632         struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
633         int flags = dreq->flags;
634
635         dreq->flags = 0;
636         switch (flags) {
637                 case NFS_ODIRECT_DO_COMMIT:
638                         nfs_direct_commit_schedule(dreq);
639                         break;
640                 case NFS_ODIRECT_RESCHED_WRITES:
641                         nfs_direct_write_reschedule(dreq);
642                         break;
643                 default:
644                         nfs_inode_dio_write_done(dreq->inode);
645                         nfs_direct_complete(dreq);
646         }
647 }
648
649 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
650 {
651         schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
652 }
653
654 #else
655 static void nfs_direct_write_schedule_work(struct work_struct *work)
656 {
657 }
658
659 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
660 {
661         nfs_inode_dio_write_done(inode);
662         nfs_direct_complete(dreq);
663 }
664 #endif
665
666 /*
667  * NB: Return the value of the first error return code.  Subsequent
668  *     errors after the first one are ignored.
669  */
670 /*
671  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
672  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
673  * bail and stop sending more writes.  Write length accounting is
674  * handled automatically by nfs_direct_write_result().  Otherwise, if
675  * no requests have been sent, just return an error.
676  */
677 static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
678                                                  const struct iovec *iov,
679                                                  loff_t pos)
680 {
681         struct nfs_direct_req *dreq = desc->pg_dreq;
682         struct nfs_open_context *ctx = dreq->ctx;
683         struct inode *inode = ctx->dentry->d_inode;
684         unsigned long user_addr = (unsigned long)iov->iov_base;
685         size_t count = iov->iov_len;
686         size_t wsize = NFS_SERVER(inode)->wsize;
687         unsigned int pgbase;
688         int result;
689         ssize_t started = 0;
690         struct page **pagevec = NULL;
691         unsigned int npages;
692
693         do {
694                 size_t bytes;
695                 int i;
696
697                 pgbase = user_addr & ~PAGE_MASK;
698                 bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
699
700                 result = -ENOMEM;
701                 npages = nfs_page_array_len(pgbase, bytes);
702                 if (!pagevec)
703                         pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
704                 if (!pagevec)
705                         break;
706
707                 down_read(&current->mm->mmap_sem);
708                 result = get_user_pages(current, current->mm, user_addr,
709                                         npages, 0, 0, pagevec, NULL);
710                 up_read(&current->mm->mmap_sem);
711                 if (result < 0)
712                         break;
713
714                 if ((unsigned)result < npages) {
715                         bytes = result * PAGE_SIZE;
716                         if (bytes <= pgbase) {
717                                 nfs_direct_release_pages(pagevec, result);
718                                 break;
719                         }
720                         bytes -= pgbase;
721                         npages = result;
722                 }
723
724                 for (i = 0; i < npages; i++) {
725                         struct nfs_page *req;
726                         unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
727
728                         req = nfs_create_request(dreq->ctx, dreq->inode,
729                                                  pagevec[i],
730                                                  pgbase, req_len);
731                         if (IS_ERR(req)) {
732                                 result = PTR_ERR(req);
733                                 break;
734                         }
735                         nfs_lock_request(req);
736                         req->wb_index = pos >> PAGE_SHIFT;
737                         req->wb_offset = pos & ~PAGE_MASK;
738                         if (!nfs_pageio_add_request(desc, req)) {
739                                 result = desc->pg_error;
740                                 nfs_unlock_and_release_request(req);
741                                 break;
742                         }
743                         pgbase = 0;
744                         bytes -= req_len;
745                         started += req_len;
746                         user_addr += req_len;
747                         pos += req_len;
748                         count -= req_len;
749                         dreq->bytes_left -= req_len;
750                 }
751                 /* The nfs_page now hold references to these pages */
752                 nfs_direct_release_pages(pagevec, npages);
753         } while (count != 0 && result >= 0);
754
755         kfree(pagevec);
756
757         if (started)
758                 return started;
759         return result < 0 ? (ssize_t) result : -EFAULT;
760 }
761
762 static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
763 {
764         struct nfs_direct_req *dreq = hdr->dreq;
765         struct nfs_commit_info cinfo;
766         int bit = -1;
767         struct nfs_page *req = nfs_list_entry(hdr->pages.next);
768
769         if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
770                 goto out_put;
771
772         nfs_init_cinfo_from_dreq(&cinfo, dreq);
773
774         spin_lock(&dreq->lock);
775
776         if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
777                 dreq->flags = 0;
778                 dreq->error = hdr->error;
779         }
780         if (dreq->error != 0)
781                 bit = NFS_IOHDR_ERROR;
782         else {
783                 dreq->count += hdr->good_bytes;
784                 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
785                         dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
786                         bit = NFS_IOHDR_NEED_RESCHED;
787                 } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
788                         if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
789                                 bit = NFS_IOHDR_NEED_RESCHED;
790                         else if (dreq->flags == 0) {
791                                 memcpy(&dreq->verf, hdr->verf,
792                                        sizeof(dreq->verf));
793                                 bit = NFS_IOHDR_NEED_COMMIT;
794                                 dreq->flags = NFS_ODIRECT_DO_COMMIT;
795                         } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
796                                 if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
797                                         dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
798                                         bit = NFS_IOHDR_NEED_RESCHED;
799                                 } else
800                                         bit = NFS_IOHDR_NEED_COMMIT;
801                         }
802                 }
803         }
804         spin_unlock(&dreq->lock);
805
806         while (!list_empty(&hdr->pages)) {
807                 req = nfs_list_entry(hdr->pages.next);
808                 nfs_list_remove_request(req);
809                 switch (bit) {
810                 case NFS_IOHDR_NEED_RESCHED:
811                 case NFS_IOHDR_NEED_COMMIT:
812                         kref_get(&req->wb_kref);
813                         nfs_mark_request_commit(req, hdr->lseg, &cinfo);
814                 }
815                 nfs_unlock_and_release_request(req);
816         }
817
818 out_put:
819         if (put_dreq(dreq))
820                 nfs_direct_write_complete(dreq, hdr->inode);
821         hdr->release(hdr);
822 }
823
824 static void nfs_write_sync_pgio_error(struct list_head *head)
825 {
826         struct nfs_page *req;
827
828         while (!list_empty(head)) {
829                 req = nfs_list_entry(head->next);
830                 nfs_list_remove_request(req);
831                 nfs_unlock_and_release_request(req);
832         }
833 }
834
835 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
836         .error_cleanup = nfs_write_sync_pgio_error,
837         .init_hdr = nfs_direct_pgio_init,
838         .completion = nfs_direct_write_completion,
839 };
840
841 static ssize_t nfs_direct_do_schedule_write_iovec(
842                 struct nfs_pageio_descriptor *desc, const struct iovec *iov,
843                 unsigned long nr_segs, loff_t pos)
844 {
845         ssize_t result = -EINVAL;
846         size_t requested_bytes = 0;
847         unsigned long seg;
848
849         for (seg = 0; seg < nr_segs; seg++) {
850                 const struct iovec *vec = &iov[seg];
851                 result = nfs_direct_write_schedule_segment(desc, vec,
852                                                            pos);
853                 if (result < 0)
854                         break;
855                 requested_bytes += result;
856                 if ((size_t)result < vec->iov_len)
857                         break;
858                 pos += vec->iov_len;
859         }
860
861         if (requested_bytes)
862                 return requested_bytes;
863
864         return result < 0 ? result : -EIO;
865 }
866
867 #ifdef CONFIG_BLOCK
868 static ssize_t nfs_direct_do_schedule_write_bvec(
869                 struct nfs_pageio_descriptor *desc,
870                 struct bio_vec *bvec, unsigned long nr_segs, loff_t pos)
871 {
872         struct nfs_direct_req *dreq = desc->pg_dreq;
873         struct nfs_open_context *ctx = dreq->ctx;
874         struct inode *inode = dreq->inode;
875         ssize_t result = 0;
876         size_t requested_bytes = 0;
877         unsigned long seg;
878         struct nfs_page *req;
879         unsigned int req_len;
880
881         for (seg = 0; seg < nr_segs; seg++) {
882                 req_len = bvec[seg].bv_len;
883
884                 req = nfs_create_request(ctx, inode, bvec[seg].bv_page,
885                                          bvec[seg].bv_offset, req_len);
886                 if (IS_ERR(req)) {
887                         result = PTR_ERR(req);
888                         break;
889                 }
890                 nfs_lock_request(req);
891                 req->wb_index = pos >> PAGE_SHIFT;
892                 req->wb_offset = pos & ~PAGE_MASK;
893                 if (!nfs_pageio_add_request(desc, req)) {
894                         result = desc->pg_error;
895                         nfs_unlock_and_release_request(req);
896                         break;
897                 }
898                 requested_bytes += req_len;
899                 pos += req_len;
900         }
901
902         if (requested_bytes)
903                 return requested_bytes;
904
905         return result < 0 ? result : -EIO;
906 }
907 #endif /* CONFIG_BLOCK */
908
909 static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq,
910                                          struct iov_iter *iter, loff_t pos)
911 {
912         struct nfs_pageio_descriptor desc;
913         struct inode *inode = dreq->inode;
914         ssize_t result = 0;
915
916         NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
917                               &nfs_direct_write_completion_ops);
918         desc.pg_dreq = dreq;
919         get_dreq(dreq);
920         atomic_inc(&inode->i_dio_count);
921
922         NFS_I(dreq->inode)->write_io += iov_iter_count(iter);
923
924         if (iov_iter_has_iovec(iter)) {
925                 result = nfs_direct_do_schedule_write_iovec(&desc,
926                                 iov_iter_iovec(iter), iter->nr_segs, pos);
927 #ifdef CONFIG_BLOCK
928         } else if (iov_iter_has_bvec(iter)) {
929                 result = nfs_direct_do_schedule_write_bvec(&desc,
930                                 iov_iter_bvec(iter), iter->nr_segs, pos);
931 #endif
932         } else
933                 BUG();
934
935         nfs_pageio_complete(&desc);
936
937         /*
938          * If no bytes were started, return the error, and let the
939          * generic layer handle the completion.
940          */
941         if (result < 0) {
942                 inode_dio_done(inode);
943                 nfs_direct_req_release(dreq);
944                 return result;
945         }
946
947         if (put_dreq(dreq))
948                 nfs_direct_write_complete(dreq, dreq->inode);
949         return 0;
950 }
951
952 static ssize_t nfs_direct_write(struct kiocb *iocb, struct iov_iter *iter,
953                                 loff_t pos)
954 {
955         ssize_t result = -ENOMEM;
956         struct inode *inode = iocb->ki_filp->f_mapping->host;
957         struct nfs_direct_req *dreq;
958         struct nfs_lock_context *l_ctx;
959
960         dreq = nfs_direct_req_alloc();
961         if (!dreq)
962                 goto out;
963
964         dreq->inode = inode;
965         dreq->bytes_left = iov_iter_count(iter);
966         dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
967         l_ctx = nfs_get_lock_context(dreq->ctx);
968         if (IS_ERR(l_ctx)) {
969                 result = PTR_ERR(l_ctx);
970                 goto out_release;
971         }
972         dreq->l_ctx = l_ctx;
973         if (!is_sync_kiocb(iocb))
974                 dreq->iocb = iocb;
975
976         result = nfs_direct_write_schedule(dreq, iter, pos);
977         if (!result)
978                 result = nfs_direct_wait(dreq);
979 out_release:
980         nfs_direct_req_release(dreq);
981 out:
982         return result;
983 }
984
985 /**
986  * nfs_file_direct_read - file direct read operation for NFS files
987  * @iocb: target I/O control block
988  * @iter: vector of buffers into which to read data
989  * @pos: byte offset in file where reading starts
990  *
991  * We use this function for direct reads instead of calling
992  * generic_file_read_iter() in order to avoid gfar's check to see if
993  * the request starts before the end of the file.  For that check
994  * to work, we must generate a GETATTR before each direct read, and
995  * even then there is a window between the GETATTR and the subsequent
996  * READ where the file size could change.  Our preference is simply
997  * to do all reads the application wants, and the server will take
998  * care of managing the end of file boundary.
999  *
1000  * This function also eliminates unnecessarily updating the file's
1001  * atime locally, as the NFS server sets the file's atime, and this
1002  * client must read the updated atime from the server back into its
1003  * cache.
1004  */
1005 ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
1006                              loff_t pos)
1007 {
1008         ssize_t retval = -EINVAL;
1009         struct file *file = iocb->ki_filp;
1010         struct address_space *mapping = file->f_mapping;
1011         size_t count;
1012
1013         count = iov_iter_count(iter);
1014         nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
1015
1016         dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
1017                 file->f_path.dentry->d_parent->d_name.name,
1018                 file->f_path.dentry->d_name.name,
1019                 count, (long long) pos);
1020
1021         retval = 0;
1022         if (!count)
1023                 goto out;
1024
1025         retval = nfs_sync_mapping(mapping);
1026         if (retval)
1027                 goto out;
1028
1029         task_io_account_read(count);
1030
1031         retval = nfs_direct_read(iocb, iter, pos);
1032         if (retval > 0)
1033                 iocb->ki_pos = pos + retval;
1034
1035 out:
1036         return retval;
1037 }
1038
1039 /**
1040  * nfs_file_direct_write - file direct write operation for NFS files
1041  * @iocb: target I/O control block
1042  * @iter: vector of buffers from which to write data
1043  * @pos: byte offset in file where writing starts
1044  *
1045  * We use this function for direct writes instead of calling
1046  * generic_file_write_iter() in order to avoid taking the inode
1047  * semaphore and updating the i_size.  The NFS server will set
1048  * the new i_size and this client must read the updated size
1049  * back into its cache.  We let the server do generic write
1050  * parameter checking and report problems.
1051  *
1052  * We eliminate local atime updates, see direct read above.
1053  *
1054  * We avoid unnecessary page cache invalidations for normal cached
1055  * readers of this file.
1056  *
1057  * Note that O_APPEND is not supported for NFS direct writes, as there
1058  * is no atomic O_APPEND write facility in the NFS protocol.
1059  */
1060 ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
1061                               loff_t pos)
1062 {
1063         ssize_t retval = -EINVAL;
1064         struct file *file = iocb->ki_filp;
1065         struct address_space *mapping = file->f_mapping;
1066         size_t count;
1067
1068         count = iov_iter_count(iter);
1069         nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
1070
1071         dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
1072                 file->f_path.dentry->d_parent->d_name.name,
1073                 file->f_path.dentry->d_name.name,
1074                 count, (long long) pos);
1075
1076         retval = generic_write_checks(file, &pos, &count, 0);
1077         if (retval)
1078                 goto out;
1079
1080         retval = -EINVAL;
1081         if ((ssize_t) count < 0)
1082                 goto out;
1083         retval = 0;
1084         if (!count)
1085                 goto out;
1086
1087         retval = nfs_sync_mapping(mapping);
1088         if (retval)
1089                 goto out;
1090
1091         task_io_account_write(count);
1092
1093         retval = nfs_direct_write(iocb, iter, pos);
1094         if (retval > 0) {
1095                 struct inode *inode = mapping->host;
1096
1097                 iocb->ki_pos = pos + retval;
1098                 spin_lock(&inode->i_lock);
1099                 if (i_size_read(inode) < iocb->ki_pos)
1100                         i_size_write(inode, iocb->ki_pos);
1101                 spin_unlock(&inode->i_lock);
1102         }
1103 out:
1104         return retval;
1105 }
1106
1107 /**
1108  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
1109  *
1110  */
1111 int __init nfs_init_directcache(void)
1112 {
1113         nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
1114                                                 sizeof(struct nfs_direct_req),
1115                                                 0, (SLAB_RECLAIM_ACCOUNT|
1116                                                         SLAB_MEM_SPREAD),
1117                                                 NULL);
1118         if (nfs_direct_cachep == NULL)
1119                 return -ENOMEM;
1120
1121         return 0;
1122 }
1123
1124 /**
1125  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
1126  *
1127  */
1128 void nfs_destroy_directcache(void)
1129 {
1130         kmem_cache_destroy(nfs_direct_cachep);
1131 }