Whamcloud - gitweb
LU-13799 llite: Adjust dio refcounting
[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                 /* We keep the refcount from cl_page_find, so we don't need
371                  * another one here
372                  */
373                 cl_2queue_add(queue, page, false);
374                 /*
375                  * Set page clip to tell transfer formation engine
376                  * that page has to be sent even if it is beyond KMS.
377                  */
378                 if (size < page_size)
379                         cl_page_clip(env, page, 0, size);
380                 ++io_pages;
381
382                 offset += page_size;
383                 size -= page_size;
384         }
385         if (rc == 0 && io_pages > 0) {
386                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
387
388                 atomic_add(io_pages, &anchor->csi_sync_nr);
389                 /*
390                  * Avoid out-of-order execution of adding inflight
391                  * modifications count and io submit.
392                  */
393                 smp_mb();
394                 rc = cl_io_submit_rw(env, io, iot, queue);
395                 if (rc == 0) {
396                         cl_page_list_splice(&queue->c2_qout,
397                                         &pv->ldp_aio->cda_pages);
398                 } else {
399                         atomic_add(-queue->c2_qin.pl_nr,
400                                    &anchor->csi_sync_nr);
401                         cl_page_list_for_each(page, &queue->c2_qin)
402                                 page->cp_sync_io = NULL;
403                 }
404                 /* handle partially submitted reqs */
405                 if (queue->c2_qin.pl_nr > 0) {
406                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
407                                PFID(lu_object_fid(&obj->co_lu)),
408                                queue->c2_qin.pl_nr, rc);
409                         if (rc == 0)
410                                 rc = -EIO;
411                 }
412         }
413
414         cl_2queue_discard(env, io, queue);
415         cl_2queue_disown(env, io, queue);
416         cl_2queue_fini(env, queue);
417         RETURN(rc);
418 }
419
420 #ifdef KMALLOC_MAX_SIZE
421 #define MAX_MALLOC KMALLOC_MAX_SIZE
422 #else
423 #define MAX_MALLOC (128 * 1024)
424 #endif
425
426 /* This is the maximum size of a single O_DIRECT request, based on the
427  * kmalloc limit.  We need to fit all of the brw_page structs, each one
428  * representing PAGE_SIZE worth of user data, into a single buffer, and
429  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
430  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
431 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
432                       ~((size_t)DT_MAX_BRW_SIZE - 1))
433
434 static ssize_t
435 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
436 {
437         struct ll_cl_context *lcc;
438         const struct lu_env *env;
439         struct cl_io *io;
440         struct file *file = iocb->ki_filp;
441         struct inode *inode = file->f_mapping->host;
442         struct cl_dio_aio *aio;
443         size_t count = iov_iter_count(iter);
444         ssize_t tot_bytes = 0, result = 0;
445         loff_t file_offset = iocb->ki_pos;
446         struct vvp_io *vio;
447
448         /* Check EOF by ourselves */
449         if (rw == READ && file_offset >= i_size_read(inode))
450                 return 0;
451
452         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
453         if (file_offset & ~PAGE_MASK)
454                 RETURN(-EINVAL);
455
456         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
457                "offset=%lld=%llx, pages %zd (max %lu)\n",
458                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
459                file_offset, file_offset, count >> PAGE_SHIFT,
460                MAX_DIO_SIZE >> PAGE_SHIFT);
461
462         /* Check that all user buffers are aligned as well */
463         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
464                 RETURN(-EINVAL);
465
466         lcc = ll_cl_find(file);
467         if (lcc == NULL)
468                 RETURN(-EIO);
469
470         env = lcc->lcc_env;
471         LASSERT(!IS_ERR(env));
472         vio = vvp_env_io(env);
473         io = lcc->lcc_io;
474         LASSERT(io != NULL);
475
476         aio = io->ci_aio;
477         LASSERT(aio);
478         LASSERT(aio->cda_iocb == iocb);
479
480         while (iov_iter_count(iter)) {
481                 struct ll_dio_pages pvec = { .ldp_aio = aio };
482                 struct page **pages;
483
484                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
485                 if (rw == READ) {
486                         if (file_offset >= i_size_read(inode))
487                                 break;
488
489                         if (file_offset + count > i_size_read(inode))
490                                 count = i_size_read(inode) - file_offset;
491                 }
492
493                 result = ll_get_user_pages(rw, iter, &pages,
494                                            &pvec.ldp_count, count);
495                 if (unlikely(result <= 0))
496                         GOTO(out, result);
497
498                 count = result;
499                 pvec.ldp_file_offset = file_offset;
500                 pvec.ldp_pages = pages;
501
502                 result = ll_direct_rw_pages(env, io, count,
503                                             rw, inode, &pvec);
504                 ll_free_user_pages(pages, pvec.ldp_count);
505
506                 if (unlikely(result < 0))
507                         GOTO(out, result);
508
509                 iov_iter_advance(iter, count);
510                 tot_bytes += count;
511                 file_offset += count;
512         }
513
514 out:
515         aio->cda_bytes += tot_bytes;
516
517         if (rw == WRITE)
518                 vio->u.readwrite.vui_written += tot_bytes;
519         else
520                 vio->u.readwrite.vui_read += tot_bytes;
521
522         /* We cannot do async submission - for AIO or regular DIO - unless
523          * lockless because it causes us to release the lock early.
524          *
525          * There are also several circumstances in which we must disable
526          * parallel DIO, so we check if it is enabled.
527          *
528          * The check for "is_sync_kiocb" excludes AIO, which does not need to
529          * be disabled in these situations.
530          */
531         if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio)) {
532                 ssize_t rc2;
533
534                 /* Wait here rather than doing async submission */
535                 rc2 = cl_sync_io_wait_recycle(env, &aio->cda_sync, 0, 0);
536                 if (result == 0 && rc2)
537                         result = rc2;
538
539                 if (result == 0)
540                         result = tot_bytes;
541         } else if (result == 0) {
542                 result = -EIOCBQUEUED;
543         }
544
545         return result;
546 }
547
548 #if defined(HAVE_DIO_ITER)
549 static ssize_t ll_direct_IO(
550 #ifndef HAVE_IOV_ITER_RW
551              int rw,
552 #endif
553              struct kiocb *iocb, struct iov_iter *iter
554 #ifndef HAVE_DIRECTIO_2ARGS
555              , loff_t file_offset
556 #endif
557              )
558 {
559         int nrw;
560
561 #ifndef HAVE_IOV_ITER_RW
562         nrw = rw;
563 #else
564         nrw = iov_iter_rw(iter);
565 #endif
566
567         return ll_direct_IO_impl(iocb, iter, nrw);
568 }
569
570 #else /* !defined(HAVE_DIO_ITER) */
571
572 static ssize_t
573 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
574              loff_t file_offset, unsigned long nr_segs)
575 {
576         struct iov_iter iter;
577
578         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
579         return ll_direct_IO_impl(iocb, &iter, rw);
580 }
581
582 #endif /* !defined(HAVE_DIO_ITER) */
583
584 /**
585  * Prepare partially written-to page for a write.
586  * @pg is owned when passed in and disowned when it returns non-zero result to
587  * the caller.
588  */
589 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
590                                    struct cl_page *pg, struct file *file)
591 {
592         struct cl_attr *attr   = vvp_env_thread_attr(env);
593         struct cl_object *obj  = io->ci_obj;
594         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
595         loff_t          offset = cl_offset(obj, vvp_index(vpg));
596         int             result;
597         ENTRY;
598
599         cl_object_attr_lock(obj);
600         result = cl_object_attr_get(env, obj, attr);
601         cl_object_attr_unlock(obj);
602         if (result) {
603                 cl_page_disown(env, io, pg);
604                 GOTO(out, result);
605         }
606
607         /*
608          * If are writing to a new page, no need to read old data.
609          * The extent locking will have updated the KMS, and for our
610          * purposes here we can treat it like i_size.
611          */
612         if (attr->cat_kms <= offset) {
613                 char *kaddr = kmap_atomic(vpg->vpg_page);
614
615                 memset(kaddr, 0, cl_page_size(obj));
616                 kunmap_atomic(kaddr);
617                 GOTO(out, result = 0);
618         }
619
620         if (vpg->vpg_defer_uptodate) {
621                 vpg->vpg_ra_used = 1;
622                 GOTO(out, result = 0);
623         }
624
625         result = ll_io_read_page(env, io, pg, file);
626         if (result)
627                 GOTO(out, result);
628
629         /* ll_io_read_page() disowns the page */
630         result = cl_page_own(env, io, pg);
631         if (!result) {
632                 if (!PageUptodate(cl_page_vmpage(pg))) {
633                         cl_page_disown(env, io, pg);
634                         result = -EIO;
635                 }
636         } else if (result == -ENOENT) {
637                 /* page was truncated */
638                 result = -EAGAIN;
639         }
640         EXIT;
641
642 out:
643         return result;
644 }
645
646 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
647 {
648         /* Page must be present, up to date, dirty, and not in writeback. */
649         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
650             PageWriteback(vmpage) || vmpage->mapping != mapping)
651                 return -ENODATA;
652
653         return 0;
654 }
655
656 static int ll_write_begin(struct file *file, struct address_space *mapping,
657                           loff_t pos, unsigned len, unsigned flags,
658                           struct page **pagep, void **fsdata)
659 {
660         struct ll_cl_context *lcc = NULL;
661         const struct lu_env  *env = NULL;
662         struct cl_io   *io = NULL;
663         struct cl_page *page = NULL;
664
665         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
666         pgoff_t index = pos >> PAGE_SHIFT;
667         struct page *vmpage = NULL;
668         unsigned from = pos & (PAGE_SIZE - 1);
669         unsigned to = from + len;
670         int result = 0;
671         ENTRY;
672
673         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
674
675         lcc = ll_cl_find(file);
676         if (lcc == NULL) {
677                 vmpage = grab_cache_page_nowait(mapping, index);
678                 result = ll_tiny_write_begin(vmpage, mapping);
679                 GOTO(out, result);
680         }
681
682         env = lcc->lcc_env;
683         io  = lcc->lcc_io;
684
685         if (file->f_flags & O_DIRECT) {
686                 /* direct IO failed because it couldn't clean up cached pages,
687                  * this causes a problem for mirror write because the cached
688                  * page may belong to another mirror, which will result in
689                  * problem submitting the I/O. */
690                 if (io->ci_designated_mirror > 0)
691                         GOTO(out, result = -EBUSY);
692
693                 /**
694                  * Direct write can fall back to buffered read, but DIO is done
695                  * with lockless i/o, and buffered requires LDLM locking, so
696                  * in this case we must restart without lockless.
697                  */
698                 if (!io->ci_dio_lock) {
699                         io->ci_dio_lock = 1;
700                         io->ci_need_restart = 1;
701                         GOTO(out, result = -ENOLCK);
702                 }
703         }
704 again:
705         /* To avoid deadlock, try to lock page first. */
706         vmpage = grab_cache_page_nowait(mapping, index);
707
708         if (unlikely(vmpage == NULL ||
709                      PageDirty(vmpage) || PageWriteback(vmpage))) {
710                 struct vvp_io *vio = vvp_env_io(env);
711                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
712
713                 /* if the page is already in dirty cache, we have to commit
714                  * the pages right now; otherwise, it may cause deadlock
715                  * because it holds page lock of a dirty page and request for
716                  * more grants. It's okay for the dirty page to be the first
717                  * one in commit page list, though. */
718                 if (vmpage != NULL && plist->pl_nr > 0) {
719                         unlock_page(vmpage);
720                         put_page(vmpage);
721                         vmpage = NULL;
722                 }
723
724                 /* commit pages and then wait for page lock */
725                 result = vvp_io_write_commit(env, io);
726                 if (result < 0)
727                         GOTO(out, result);
728
729                 if (vmpage == NULL) {
730                         vmpage = grab_cache_page_write_begin(mapping, index,
731                                                              flags);
732                         if (vmpage == NULL)
733                                 GOTO(out, result = -ENOMEM);
734                 }
735         }
736
737         /* page was truncated */
738         if (mapping != vmpage->mapping) {
739                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
740                 unlock_page(vmpage);
741                 put_page(vmpage);
742                 vmpage = NULL;
743                 goto again;
744         }
745
746         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
747         if (IS_ERR(page))
748                 GOTO(out, result = PTR_ERR(page));
749
750         lcc->lcc_page = page;
751         lu_ref_add(&page->cp_reference, "cl_io", io);
752
753         cl_page_assume(env, io, page);
754         if (!PageUptodate(vmpage)) {
755                 /*
756                  * We're completely overwriting an existing page,
757                  * so _don't_ set it up to date until commit_write
758                  */
759                 if (from == 0 && to == PAGE_SIZE) {
760                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
761                         POISON_PAGE(vmpage, 0x11);
762                 } else {
763                         /* TODO: can be optimized at OSC layer to check if it
764                          * is a lockless IO. In that case, it's not necessary
765                          * to read the data. */
766                         result = ll_prepare_partial_page(env, io, page, file);
767                         if (result) {
768                                 /* vmpage should have been unlocked */
769                                 put_page(vmpage);
770                                 vmpage = NULL;
771
772                                 if (result == -EAGAIN)
773                                         goto again;
774                                 GOTO(out, result);
775                         }
776                 }
777         }
778         EXIT;
779 out:
780         if (result < 0) {
781                 if (vmpage != NULL) {
782                         unlock_page(vmpage);
783                         put_page(vmpage);
784                 }
785                 /* On tiny_write failure, page and io are always null. */
786                 if (!IS_ERR_OR_NULL(page)) {
787                         lu_ref_del(&page->cp_reference, "cl_io", io);
788                         cl_page_put(env, page);
789                 }
790                 if (io)
791                         io->ci_result = result;
792         } else {
793                 *pagep = vmpage;
794                 *fsdata = lcc;
795         }
796         RETURN(result);
797 }
798
799 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
800                              loff_t pos, unsigned int len, unsigned int copied,
801                              struct page *vmpage)
802 {
803         struct cl_page *clpage = (struct cl_page *) vmpage->private;
804         loff_t kms = pos+copied;
805         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
806         __u16 refcheck;
807         struct lu_env *env = cl_env_get(&refcheck);
808         int rc = 0;
809
810         ENTRY;
811
812         if (IS_ERR(env)) {
813                 rc = PTR_ERR(env);
814                 goto out;
815         }
816
817         /* This page is dirty in cache, so it should have a cl_page pointer
818          * set in vmpage->private.
819          */
820         LASSERT(clpage != NULL);
821
822         if (copied == 0)
823                 goto out_env;
824
825         /* Update the underlying size information in the OSC/LOV objects this
826          * page is part of.
827          */
828         cl_page_touch(env, clpage, to);
829
830 out_env:
831         cl_env_put(env, &refcheck);
832
833 out:
834         /* Must return page unlocked. */
835         unlock_page(vmpage);
836
837         RETURN(rc);
838 }
839
840 static int ll_write_end(struct file *file, struct address_space *mapping,
841                         loff_t pos, unsigned len, unsigned copied,
842                         struct page *vmpage, void *fsdata)
843 {
844         struct ll_cl_context *lcc = fsdata;
845         const struct lu_env *env;
846         struct cl_io *io;
847         struct vvp_io *vio;
848         struct cl_page *page;
849         unsigned from = pos & (PAGE_SIZE - 1);
850         bool unplug = false;
851         int result = 0;
852         ENTRY;
853
854         put_page(vmpage);
855
856         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
857
858         if (lcc == NULL) {
859                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
860                                            vmpage);
861                 GOTO(out, result);
862         }
863
864         LASSERT(lcc != NULL);
865         env  = lcc->lcc_env;
866         page = lcc->lcc_page;
867         io   = lcc->lcc_io;
868         vio  = vvp_env_io(env);
869
870         LASSERT(cl_page_is_owned(page, io));
871         if (copied > 0) {
872                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
873
874                 lcc->lcc_page = NULL; /* page will be queued */
875
876                 /* Add it into write queue */
877                 cl_page_list_add(plist, page, true);
878                 if (plist->pl_nr == 1) /* first page */
879                         vio->u.readwrite.vui_from = from;
880                 else
881                         LASSERT(from == 0);
882                 vio->u.readwrite.vui_to = from + copied;
883
884                 /* To address the deadlock in balance_dirty_pages() where
885                  * this dirty page may be written back in the same thread. */
886                 if (PageDirty(vmpage))
887                         unplug = true;
888
889                 /* We may have one full RPC, commit it soon */
890                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
891                         unplug = true;
892
893                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
894                               "queued page: %d.\n", plist->pl_nr);
895         } else {
896                 cl_page_disown(env, io, page);
897
898                 lcc->lcc_page = NULL;
899                 lu_ref_del(&page->cp_reference, "cl_io", io);
900                 cl_page_put(env, page);
901
902                 /* page list is not contiguous now, commit it now */
903                 unplug = true;
904         }
905         if (unplug || io->u.ci_wr.wr_sync)
906                 result = vvp_io_write_commit(env, io);
907
908         if (result < 0)
909                 io->ci_result = result;
910
911
912 out:
913         RETURN(result >= 0 ? copied : result);
914 }
915
916 #ifdef CONFIG_MIGRATION
917 static int ll_migratepage(struct address_space *mapping,
918                           struct page *newpage, struct page *page,
919                           enum migrate_mode mode)
920 {
921         /* Always fail page migration until we have a proper implementation */
922         return -EIO;
923 }
924 #endif
925
926 const struct address_space_operations ll_aops = {
927         .readpage       = ll_readpage,
928         .direct_IO      = ll_direct_IO,
929         .writepage      = ll_writepage,
930         .writepages     = ll_writepages,
931         .set_page_dirty = __set_page_dirty_nobuffers,
932         .write_begin    = ll_write_begin,
933         .write_end      = ll_write_end,
934         .invalidatepage = ll_invalidatepage,
935         .releasepage    = (void *)ll_releasepage,
936 #ifdef CONFIG_MIGRATION
937         .migratepage    = ll_migratepage,
938 #endif
939 };