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