Whamcloud - gitweb
LU-13798 llite: parallelize direct i/o issuance
[fs/lustre-release.git] / lustre / llite / rw26.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lustre/llite/rw26.c
32  *
33  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
34  */
35
36 #include <linux/buffer_head.h>
37 #include <linux/errno.h>
38 #include <linux/fs.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/mpage.h>
42 #include <linux/pagemap.h>
43 #include <linux/string.h>
44 #include <linux/unistd.h>
45 #include <linux/writeback.h>
46 #include <linux/migrate.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include "llite_internal.h"
51 #include <lustre_compat.h>
52
53 /**
54  * Implements Linux VM address_space::invalidatepage() method. This method is
55  * called when the page is truncate from a file, either as a result of
56  * explicit truncate, or when inode is removed from memory (as a result of
57  * final iput(), umount, or memory pressure induced icache shrinking).
58  *
59  * [0, offset] bytes of the page remain valid (this is for a case of not-page
60  * aligned truncate). Lustre leaves partially truncated page in the cache,
61  * relying on struct inode::i_size to limit further accesses.
62  */
63 static void ll_invalidatepage(struct page *vmpage,
64 #ifdef HAVE_INVALIDATE_RANGE
65                                 unsigned int offset, unsigned int length
66 #else
67                                 unsigned long offset
68 #endif
69                              )
70 {
71         struct inode     *inode;
72         struct lu_env    *env;
73         struct cl_page   *page;
74         struct cl_object *obj;
75
76         LASSERT(PageLocked(vmpage));
77         LASSERT(!PageWriteback(vmpage));
78
79         /*
80          * It is safe to not check anything in invalidatepage/releasepage
81          * below because they are run with page locked and all our io is
82          * happening with locked page too
83          */
84 #ifdef HAVE_INVALIDATE_RANGE
85         if (offset == 0 && length == PAGE_SIZE) {
86 #else
87         if (offset == 0) {
88 #endif
89                 /* See the comment in ll_releasepage() */
90                 env = cl_env_percpu_get();
91                 LASSERT(!IS_ERR(env));
92
93                 inode = vmpage->mapping->host;
94                 obj = ll_i2info(inode)->lli_clob;
95                 if (obj != NULL) {
96                         page = cl_vmpage_page(vmpage, obj);
97                         if (page != NULL) {
98                                 cl_page_delete(env, page);
99                                 cl_page_put(env, page);
100                         }
101                 } else
102                         LASSERT(vmpage->private == 0);
103
104                 cl_env_percpu_put(env);
105         }
106 }
107
108 #ifdef HAVE_RELEASEPAGE_WITH_INT
109 #define RELEASEPAGE_ARG_TYPE int
110 #else
111 #define RELEASEPAGE_ARG_TYPE gfp_t
112 #endif
113 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
114 {
115         struct lu_env           *env;
116         struct cl_object        *obj;
117         struct cl_page          *page;
118         struct address_space    *mapping;
119         int result = 0;
120
121         LASSERT(PageLocked(vmpage));
122         if (PageWriteback(vmpage) || PageDirty(vmpage))
123                 return 0;
124
125         mapping = vmpage->mapping;
126         if (mapping == NULL)
127                 return 1;
128
129         obj = ll_i2info(mapping->host)->lli_clob;
130         if (obj == NULL)
131                 return 1;
132
133         page = cl_vmpage_page(vmpage, obj);
134         if (page == NULL)
135                 return 1;
136
137         env = cl_env_percpu_get();
138         LASSERT(!IS_ERR(env));
139
140         if (!cl_page_in_use(page)) {
141                 result = 1;
142                 cl_page_delete(env, page);
143         }
144
145         /* To use percpu env array, the call path can not be rescheduled;
146          * otherwise percpu array will be messed if ll_releaspage() called
147          * again on the same CPU.
148          *
149          * If this page holds the last refc of cl_object, the following
150          * call path may cause reschedule:
151          *   cl_page_put -> cl_page_free -> cl_object_put ->
152          *     lu_object_put -> lu_object_free -> lov_delete_raid0.
153          *
154          * However, the kernel can't get rid of this inode until all pages have
155          * been cleaned up. Now that we hold page lock here, it's pretty safe
156          * that we won't get into object delete path.
157          */
158         LASSERT(cl_object_refc(obj) > 1);
159         cl_page_put(env, page);
160
161         cl_env_percpu_put(env);
162         return result;
163 }
164
165 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW) || \
166         defined(HAVE_DIRECTIO_2ARGS)
167 #define HAVE_DIO_ITER 1
168 #endif
169
170 /*
171  * ll_free_user_pages - tear down page struct array
172  * @pages: array of page struct pointers underlying target buffer
173  */
174 static void ll_free_user_pages(struct page **pages, int npages)
175 {
176         int i;
177
178         for (i = 0; i < npages; i++) {
179                 if (!pages[i])
180                         break;
181                 put_page(pages[i]);
182         }
183
184 #if defined(HAVE_DIO_ITER)
185         kvfree(pages);
186 #else
187         OBD_FREE_PTR_ARRAY_LARGE(pages, npages);
188 #endif
189 }
190
191 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
192                                 struct page ***pages, ssize_t *npages,
193                                 size_t maxsize)
194 {
195 #if defined(HAVE_DIO_ITER)
196         size_t start;
197         size_t result;
198
199         /*
200          * iov_iter_get_pages_alloc() is introduced in 3.16 similar
201          * to HAVE_DIO_ITER.
202          */
203         result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
204         if (result > 0)
205                 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
206
207         return result;
208 #else
209         unsigned long addr;
210         size_t page_count;
211         size_t size;
212         long result;
213
214         if (!maxsize)
215                 return 0;
216
217         if (!iter->nr_segs)
218                 return 0;
219
220         addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
221         if (addr & ~PAGE_MASK)
222                 return -EINVAL;
223
224         size = min_t(size_t, maxsize, iter->iov->iov_len);
225         page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
226         OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
227         if (*pages == NULL)
228                 return -ENOMEM;
229
230         mmap_read_lock(current->mm);
231         result = get_user_pages(current, current->mm, addr, page_count,
232                                 rw == READ, 0, *pages, NULL);
233         mmap_read_unlock(current->mm);
234
235         if (unlikely(result != page_count)) {
236                 ll_free_user_pages(*pages, page_count);
237                 *pages = NULL;
238
239                 if (result >= 0)
240                         return -EFAULT;
241
242                 return result;
243         }
244         *npages = page_count;
245
246         return size;
247 #endif
248 }
249
250 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
251 #if defined(HAVE_DIO_ITER)
252 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
253 {
254         return iov_iter_alignment(i);
255 }
256 #else /* copied from alignment_iovec() */
257 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
258 {
259         const struct iovec *iov = i->iov;
260         unsigned long res;
261         size_t size = i->count;
262         size_t n;
263
264         if (!size)
265                 return 0;
266
267         res = (unsigned long)iov->iov_base + i->iov_offset;
268         n = iov->iov_len - i->iov_offset;
269         if (n >= size)
270                 return res | size;
271
272         size -= n;
273         res |= n;
274         while (size > (++iov)->iov_len) {
275                 res |= (unsigned long)iov->iov_base | iov->iov_len;
276                 size -= iov->iov_len;
277         }
278         res |= (unsigned long)iov->iov_base | size;
279
280         return res;
281 }
282 #endif
283
284 /*
285  * Lustre could relax a bit for alignment, io count is not
286  * necessary page alignment.
287  */
288 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
289 {
290         size_t orig_size = i->count;
291         size_t count = orig_size & ~PAGE_MASK;
292         unsigned long res;
293
294         if (!count)
295                 return iov_iter_alignment_vfs(i);
296
297         if (orig_size > PAGE_SIZE) {
298                 iov_iter_truncate(i, orig_size - count);
299                 res = iov_iter_alignment_vfs(i);
300                 iov_iter_reexpand(i, orig_size);
301
302                 return res;
303         }
304
305         res = iov_iter_alignment_vfs(i);
306         /* start address is page aligned */
307         if ((res & ~PAGE_MASK) == orig_size)
308                 return PAGE_SIZE;
309
310         return res;
311 }
312
313 /** direct IO pages */
314 struct ll_dio_pages {
315         struct cl_dio_aio       *ldp_aio;
316         /*
317          * page array to be written. we don't support
318          * partial pages except the last one.
319          */
320         struct page             **ldp_pages;
321         /** # of pages in the array. */
322         size_t                  ldp_count;
323         /* the file offset of the first page. */
324         loff_t                  ldp_file_offset;
325 };
326
327 static int
328 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
329                    int rw, struct inode *inode, struct ll_dio_pages *pv)
330 {
331         struct cl_page    *page;
332         struct cl_2queue  *queue = &io->ci_queue;
333         struct cl_object  *obj = io->ci_obj;
334         struct cl_sync_io *anchor = &pv->ldp_aio->cda_sync;
335         loff_t offset   = pv->ldp_file_offset;
336         int io_pages    = 0;
337         size_t page_size = cl_page_size(obj);
338         int i;
339         ssize_t rc = 0;
340
341         ENTRY;
342
343         cl_2queue_init(queue);
344         for (i = 0; i < pv->ldp_count; i++) {
345                 LASSERT(!(offset & (PAGE_SIZE - 1)));
346                 page = cl_page_find(env, obj, cl_index(obj, offset),
347                                     pv->ldp_pages[i], CPT_TRANSIENT);
348                 if (IS_ERR(page)) {
349                         rc = PTR_ERR(page);
350                         break;
351                 }
352                 LASSERT(page->cp_type == CPT_TRANSIENT);
353                 rc = cl_page_own(env, io, page);
354                 if (rc) {
355                         cl_page_put(env, page);
356                         break;
357                 }
358
359                 page->cp_sync_io = anchor;
360                 if (inode && IS_ENCRYPTED(inode)) {
361                         /* In case of Direct IO on encrypted file, we need to
362                          * add a reference to the inode on the cl_page.
363                          * This info is required by llcrypt to proceed
364                          * to encryption/decryption.
365                          * This is safe because we know these pages are private
366                          * to the thread doing the Direct IO.
367                          */
368                         page->cp_inode = inode;
369                 }
370                 cl_2queue_add(queue, page);
371                 /*
372                  * Set page clip to tell transfer formation engine
373                  * that page has to be sent even if it is beyond KMS.
374                  */
375                 cl_page_clip(env, page, 0, min(size, page_size));
376                 ++io_pages;
377
378                 /* drop the reference count for cl_page_find */
379                 cl_page_put(env, page);
380                 offset += page_size;
381                 size -= page_size;
382         }
383         if (rc == 0 && io_pages > 0) {
384                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
385
386                 atomic_add(io_pages, &anchor->csi_sync_nr);
387                 /*
388                  * Avoid out-of-order execution of adding inflight
389                  * modifications count and io submit.
390                  */
391                 smp_mb();
392                 rc = cl_io_submit_rw(env, io, iot, queue);
393                 if (rc == 0) {
394                         cl_page_list_splice(&queue->c2_qout,
395                                         &pv->ldp_aio->cda_pages);
396                 } else {
397                         atomic_add(-queue->c2_qin.pl_nr,
398                                    &anchor->csi_sync_nr);
399                         cl_page_list_for_each(page, &queue->c2_qin)
400                                 page->cp_sync_io = NULL;
401                 }
402                 /* handle partially submitted reqs */
403                 if (queue->c2_qin.pl_nr > 0) {
404                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
405                                PFID(lu_object_fid(&obj->co_lu)),
406                                queue->c2_qin.pl_nr, rc);
407                         if (rc == 0)
408                                 rc = -EIO;
409                 }
410         }
411
412         cl_2queue_discard(env, io, queue);
413         cl_2queue_disown(env, io, queue);
414         cl_2queue_fini(env, queue);
415         RETURN(rc);
416 }
417
418 #ifdef KMALLOC_MAX_SIZE
419 #define MAX_MALLOC KMALLOC_MAX_SIZE
420 #else
421 #define MAX_MALLOC (128 * 1024)
422 #endif
423
424 /* This is the maximum size of a single O_DIRECT request, based on the
425  * kmalloc limit.  We need to fit all of the brw_page structs, each one
426  * representing PAGE_SIZE worth of user data, into a single buffer, and
427  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
428  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
429 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
430                       ~((size_t)DT_MAX_BRW_SIZE - 1))
431
432 static ssize_t
433 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
434 {
435         struct ll_cl_context *lcc;
436         const struct lu_env *env;
437         struct cl_io *io;
438         struct file *file = iocb->ki_filp;
439         struct inode *inode = file->f_mapping->host;
440         struct cl_dio_aio *aio;
441         size_t count = iov_iter_count(iter);
442         ssize_t tot_bytes = 0, result = 0;
443         loff_t file_offset = iocb->ki_pos;
444         struct vvp_io *vio;
445
446         /* Check EOF by ourselves */
447         if (rw == READ && file_offset >= i_size_read(inode))
448                 return 0;
449
450         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
451         if (file_offset & ~PAGE_MASK)
452                 RETURN(-EINVAL);
453
454         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
455                "offset=%lld=%llx, pages %zd (max %lu)\n",
456                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
457                file_offset, file_offset, count >> PAGE_SHIFT,
458                MAX_DIO_SIZE >> PAGE_SHIFT);
459
460         /* Check that all user buffers are aligned as well */
461         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
462                 RETURN(-EINVAL);
463
464         lcc = ll_cl_find(file);
465         if (lcc == NULL)
466                 RETURN(-EIO);
467
468         env = lcc->lcc_env;
469         LASSERT(!IS_ERR(env));
470         vio = vvp_env_io(env);
471         io = lcc->lcc_io;
472         LASSERT(io != NULL);
473
474         aio = io->ci_aio;
475         LASSERT(aio);
476         LASSERT(aio->cda_iocb == iocb);
477
478         while (iov_iter_count(iter)) {
479                 struct ll_dio_pages pvec = { .ldp_aio = aio };
480                 struct page **pages;
481
482                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
483                 if (rw == READ) {
484                         if (file_offset >= i_size_read(inode))
485                                 break;
486
487                         if (file_offset + count > i_size_read(inode))
488                                 count = i_size_read(inode) - file_offset;
489                 }
490
491                 result = ll_get_user_pages(rw, iter, &pages,
492                                            &pvec.ldp_count, count);
493                 if (unlikely(result <= 0))
494                         GOTO(out, result);
495
496                 count = result;
497                 pvec.ldp_file_offset = file_offset;
498                 pvec.ldp_pages = pages;
499
500                 result = ll_direct_rw_pages(env, io, count,
501                                             rw, inode, &pvec);
502                 ll_free_user_pages(pages, pvec.ldp_count);
503
504                 if (unlikely(result < 0))
505                         GOTO(out, result);
506
507                 iov_iter_advance(iter, count);
508                 tot_bytes += count;
509                 file_offset += count;
510         }
511
512 out:
513         aio->cda_bytes += tot_bytes;
514
515         if (rw == WRITE)
516                 vio->u.readwrite.vui_written += tot_bytes;
517         else
518                 vio->u.readwrite.vui_read += tot_bytes;
519
520         /* If async dio submission is not allowed, we must wait here. */
521         if (is_sync_kiocb(iocb) && !io->ci_parallel_dio) {
522                 ssize_t rc2;
523
524                 rc2 = cl_sync_io_wait_recycle(env, &aio->cda_sync, 0, 0);
525                 if (result == 0 && rc2)
526                         result = rc2;
527
528                 if (result == 0)
529                         result = tot_bytes;
530         } else if (result == 0) {
531                 result = -EIOCBQUEUED;
532         }
533
534         return result;
535 }
536
537 #if defined(HAVE_DIO_ITER)
538 static ssize_t ll_direct_IO(
539 #ifndef HAVE_IOV_ITER_RW
540              int rw,
541 #endif
542              struct kiocb *iocb, struct iov_iter *iter
543 #ifndef HAVE_DIRECTIO_2ARGS
544              , loff_t file_offset
545 #endif
546              )
547 {
548         int nrw;
549
550 #ifndef HAVE_IOV_ITER_RW
551         nrw = rw;
552 #else
553         nrw = iov_iter_rw(iter);
554 #endif
555
556         return ll_direct_IO_impl(iocb, iter, nrw);
557 }
558
559 #else /* !defined(HAVE_DIO_ITER) */
560
561 static ssize_t
562 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
563              loff_t file_offset, unsigned long nr_segs)
564 {
565         struct iov_iter iter;
566
567         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
568         return ll_direct_IO_impl(iocb, &iter, rw);
569 }
570
571 #endif /* !defined(HAVE_DIO_ITER) */
572
573 /**
574  * Prepare partially written-to page for a write.
575  * @pg is owned when passed in and disowned when it returns non-zero result to
576  * the caller.
577  */
578 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
579                                    struct cl_page *pg, struct file *file)
580 {
581         struct cl_attr *attr   = vvp_env_thread_attr(env);
582         struct cl_object *obj  = io->ci_obj;
583         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
584         loff_t          offset = cl_offset(obj, vvp_index(vpg));
585         int             result;
586         ENTRY;
587
588         cl_object_attr_lock(obj);
589         result = cl_object_attr_get(env, obj, attr);
590         cl_object_attr_unlock(obj);
591         if (result) {
592                 cl_page_disown(env, io, pg);
593                 GOTO(out, result);
594         }
595
596         /*
597          * If are writing to a new page, no need to read old data.
598          * The extent locking will have updated the KMS, and for our
599          * purposes here we can treat it like i_size.
600          */
601         if (attr->cat_kms <= offset) {
602                 char *kaddr = kmap_atomic(vpg->vpg_page);
603
604                 memset(kaddr, 0, cl_page_size(obj));
605                 kunmap_atomic(kaddr);
606                 GOTO(out, result = 0);
607         }
608
609         if (vpg->vpg_defer_uptodate) {
610                 vpg->vpg_ra_used = 1;
611                 GOTO(out, result = 0);
612         }
613
614         result = ll_io_read_page(env, io, pg, file);
615         if (result)
616                 GOTO(out, result);
617
618         /* ll_io_read_page() disowns the page */
619         result = cl_page_own(env, io, pg);
620         if (!result) {
621                 if (!PageUptodate(cl_page_vmpage(pg))) {
622                         cl_page_disown(env, io, pg);
623                         result = -EIO;
624                 }
625         } else if (result == -ENOENT) {
626                 /* page was truncated */
627                 result = -EAGAIN;
628         }
629         EXIT;
630
631 out:
632         return result;
633 }
634
635 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
636 {
637         /* Page must be present, up to date, dirty, and not in writeback. */
638         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
639             PageWriteback(vmpage) || vmpage->mapping != mapping)
640                 return -ENODATA;
641
642         return 0;
643 }
644
645 static int ll_write_begin(struct file *file, struct address_space *mapping,
646                           loff_t pos, unsigned len, unsigned flags,
647                           struct page **pagep, void **fsdata)
648 {
649         struct ll_cl_context *lcc = NULL;
650         const struct lu_env  *env = NULL;
651         struct cl_io   *io = NULL;
652         struct cl_page *page = NULL;
653
654         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
655         pgoff_t index = pos >> PAGE_SHIFT;
656         struct page *vmpage = NULL;
657         unsigned from = pos & (PAGE_SIZE - 1);
658         unsigned to = from + len;
659         int result = 0;
660         ENTRY;
661
662         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
663
664         lcc = ll_cl_find(file);
665         if (lcc == NULL) {
666                 vmpage = grab_cache_page_nowait(mapping, index);
667                 result = ll_tiny_write_begin(vmpage, mapping);
668                 GOTO(out, result);
669         }
670
671         env = lcc->lcc_env;
672         io  = lcc->lcc_io;
673
674         if (file->f_flags & O_DIRECT) {
675                 /* direct IO failed because it couldn't clean up cached pages,
676                  * this causes a problem for mirror write because the cached
677                  * page may belong to another mirror, which will result in
678                  * problem submitting the I/O. */
679                 if (io->ci_designated_mirror > 0)
680                         GOTO(out, result = -EBUSY);
681
682                 /**
683                  * Direct write can fall back to buffered read, but DIO is done
684                  * with lockless i/o, and buffered requires LDLM locking, so
685                  * in this case we must restart without lockless.
686                  */
687                 if (!io->ci_dio_lock) {
688                         io->ci_dio_lock = 1;
689                         io->ci_need_restart = 1;
690                         GOTO(out, result = -ENOLCK);
691                 }
692         }
693 again:
694         /* To avoid deadlock, try to lock page first. */
695         vmpage = grab_cache_page_nowait(mapping, index);
696
697         if (unlikely(vmpage == NULL ||
698                      PageDirty(vmpage) || PageWriteback(vmpage))) {
699                 struct vvp_io *vio = vvp_env_io(env);
700                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
701
702                 /* if the page is already in dirty cache, we have to commit
703                  * the pages right now; otherwise, it may cause deadlock
704                  * because it holds page lock of a dirty page and request for
705                  * more grants. It's okay for the dirty page to be the first
706                  * one in commit page list, though. */
707                 if (vmpage != NULL && plist->pl_nr > 0) {
708                         unlock_page(vmpage);
709                         put_page(vmpage);
710                         vmpage = NULL;
711                 }
712
713                 /* commit pages and then wait for page lock */
714                 result = vvp_io_write_commit(env, io);
715                 if (result < 0)
716                         GOTO(out, result);
717
718                 if (vmpage == NULL) {
719                         vmpage = grab_cache_page_write_begin(mapping, index,
720                                                              flags);
721                         if (vmpage == NULL)
722                                 GOTO(out, result = -ENOMEM);
723                 }
724         }
725
726         /* page was truncated */
727         if (mapping != vmpage->mapping) {
728                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
729                 unlock_page(vmpage);
730                 put_page(vmpage);
731                 vmpage = NULL;
732                 goto again;
733         }
734
735         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
736         if (IS_ERR(page))
737                 GOTO(out, result = PTR_ERR(page));
738
739         lcc->lcc_page = page;
740         lu_ref_add(&page->cp_reference, "cl_io", io);
741
742         cl_page_assume(env, io, page);
743         if (!PageUptodate(vmpage)) {
744                 /*
745                  * We're completely overwriting an existing page,
746                  * so _don't_ set it up to date until commit_write
747                  */
748                 if (from == 0 && to == PAGE_SIZE) {
749                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
750                         POISON_PAGE(vmpage, 0x11);
751                 } else {
752                         /* TODO: can be optimized at OSC layer to check if it
753                          * is a lockless IO. In that case, it's not necessary
754                          * to read the data. */
755                         result = ll_prepare_partial_page(env, io, page, file);
756                         if (result) {
757                                 /* vmpage should have been unlocked */
758                                 put_page(vmpage);
759                                 vmpage = NULL;
760
761                                 if (result == -EAGAIN)
762                                         goto again;
763                                 GOTO(out, result);
764                         }
765                 }
766         }
767         EXIT;
768 out:
769         if (result < 0) {
770                 if (vmpage != NULL) {
771                         unlock_page(vmpage);
772                         put_page(vmpage);
773                 }
774                 /* On tiny_write failure, page and io are always null. */
775                 if (!IS_ERR_OR_NULL(page)) {
776                         lu_ref_del(&page->cp_reference, "cl_io", io);
777                         cl_page_put(env, page);
778                 }
779                 if (io)
780                         io->ci_result = result;
781         } else {
782                 *pagep = vmpage;
783                 *fsdata = lcc;
784         }
785         RETURN(result);
786 }
787
788 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
789                              loff_t pos, unsigned int len, unsigned int copied,
790                              struct page *vmpage)
791 {
792         struct cl_page *clpage = (struct cl_page *) vmpage->private;
793         loff_t kms = pos+copied;
794         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
795         __u16 refcheck;
796         struct lu_env *env = cl_env_get(&refcheck);
797         int rc = 0;
798
799         ENTRY;
800
801         if (IS_ERR(env)) {
802                 rc = PTR_ERR(env);
803                 goto out;
804         }
805
806         /* This page is dirty in cache, so it should have a cl_page pointer
807          * set in vmpage->private.
808          */
809         LASSERT(clpage != NULL);
810
811         if (copied == 0)
812                 goto out_env;
813
814         /* Update the underlying size information in the OSC/LOV objects this
815          * page is part of.
816          */
817         cl_page_touch(env, clpage, to);
818
819 out_env:
820         cl_env_put(env, &refcheck);
821
822 out:
823         /* Must return page unlocked. */
824         unlock_page(vmpage);
825
826         RETURN(rc);
827 }
828
829 static int ll_write_end(struct file *file, struct address_space *mapping,
830                         loff_t pos, unsigned len, unsigned copied,
831                         struct page *vmpage, void *fsdata)
832 {
833         struct ll_cl_context *lcc = fsdata;
834         const struct lu_env *env;
835         struct cl_io *io;
836         struct vvp_io *vio;
837         struct cl_page *page;
838         unsigned from = pos & (PAGE_SIZE - 1);
839         bool unplug = false;
840         int result = 0;
841         ENTRY;
842
843         put_page(vmpage);
844
845         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
846
847         if (lcc == NULL) {
848                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
849                                            vmpage);
850                 GOTO(out, result);
851         }
852
853         LASSERT(lcc != NULL);
854         env  = lcc->lcc_env;
855         page = lcc->lcc_page;
856         io   = lcc->lcc_io;
857         vio  = vvp_env_io(env);
858
859         LASSERT(cl_page_is_owned(page, io));
860         if (copied > 0) {
861                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
862
863                 lcc->lcc_page = NULL; /* page will be queued */
864
865                 /* Add it into write queue */
866                 cl_page_list_add(plist, page);
867                 if (plist->pl_nr == 1) /* first page */
868                         vio->u.readwrite.vui_from = from;
869                 else
870                         LASSERT(from == 0);
871                 vio->u.readwrite.vui_to = from + copied;
872
873                 /* To address the deadlock in balance_dirty_pages() where
874                  * this dirty page may be written back in the same thread. */
875                 if (PageDirty(vmpage))
876                         unplug = true;
877
878                 /* We may have one full RPC, commit it soon */
879                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
880                         unplug = true;
881
882                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
883                               "queued page: %d.\n", plist->pl_nr);
884         } else {
885                 cl_page_disown(env, io, page);
886
887                 lcc->lcc_page = NULL;
888                 lu_ref_del(&page->cp_reference, "cl_io", io);
889                 cl_page_put(env, page);
890
891                 /* page list is not contiguous now, commit it now */
892                 unplug = true;
893         }
894         if (unplug || io->u.ci_wr.wr_sync)
895                 result = vvp_io_write_commit(env, io);
896
897         if (result < 0)
898                 io->ci_result = result;
899
900
901 out:
902         RETURN(result >= 0 ? copied : result);
903 }
904
905 #ifdef CONFIG_MIGRATION
906 static int ll_migratepage(struct address_space *mapping,
907                           struct page *newpage, struct page *page,
908                           enum migrate_mode mode)
909 {
910         /* Always fail page migration until we have a proper implementation */
911         return -EIO;
912 }
913 #endif
914
915 const struct address_space_operations ll_aops = {
916         .readpage       = ll_readpage,
917         .direct_IO      = ll_direct_IO,
918         .writepage      = ll_writepage,
919         .writepages     = ll_writepages,
920         .set_page_dirty = __set_page_dirty_nobuffers,
921         .write_begin    = ll_write_begin,
922         .write_end      = ll_write_end,
923         .invalidatepage = ll_invalidatepage,
924         .releasepage    = (void *)ll_releasepage,
925 #ifdef CONFIG_MIGRATION
926         .migratepage    = ll_migratepage,
927 #endif
928 };