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