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