Whamcloud - gitweb
LU-8958 llite: remove llite_loop left overs
[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, 2016, 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/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <asm/uaccess.h>
44
45 #ifdef HAVE_MIGRATE_H
46 #include <linux/migrate.h>
47 #elif defined(HAVE_MIGRATE_MODE_H)
48 #include <linux/migrate_mode.h>
49 #endif
50 #include <linux/fs.h>
51 #include <linux/buffer_head.h>
52 #include <linux/mpage.h>
53 #include <linux/writeback.h>
54 #include <linux/stat.h>
55 #include <asm/uaccess.h>
56 #include <linux/mm.h>
57 #include <linux/pagemap.h>
58
59 #define DEBUG_SUBSYSTEM S_LLITE
60
61 #include "llite_internal.h"
62 #include <lustre_compat.h>
63
64 /**
65  * Implements Linux VM address_space::invalidatepage() method. This method is
66  * called when the page is truncate from a file, either as a result of
67  * explicit truncate, or when inode is removed from memory (as a result of
68  * final iput(), umount, or memory pressure induced icache shrinking).
69  *
70  * [0, offset] bytes of the page remain valid (this is for a case of not-page
71  * aligned truncate). Lustre leaves partially truncated page in the cache,
72  * relying on struct inode::i_size to limit further accesses.
73  */
74 static void ll_invalidatepage(struct page *vmpage,
75 #ifdef HAVE_INVALIDATE_RANGE
76                                 unsigned int offset, unsigned int length
77 #else
78                                 unsigned long offset
79 #endif
80                              )
81 {
82         struct inode     *inode;
83         struct lu_env    *env;
84         struct cl_page   *page;
85         struct cl_object *obj;
86
87         LASSERT(PageLocked(vmpage));
88         LASSERT(!PageWriteback(vmpage));
89
90         /*
91          * It is safe to not check anything in invalidatepage/releasepage
92          * below because they are run with page locked and all our io is
93          * happening with locked page too
94          */
95 #ifdef HAVE_INVALIDATE_RANGE
96         if (offset == 0 && length == PAGE_SIZE) {
97 #else
98         if (offset == 0) {
99 #endif
100                 /* See the comment in ll_releasepage() */
101                 env = cl_env_percpu_get();
102                 LASSERT(!IS_ERR(env));
103
104                 inode = vmpage->mapping->host;
105                 obj = ll_i2info(inode)->lli_clob;
106                 if (obj != NULL) {
107                         page = cl_vmpage_page(vmpage, obj);
108                         if (page != NULL) {
109                                 cl_page_delete(env, page);
110                                 cl_page_put(env, page);
111                         }
112                 } else
113                         LASSERT(vmpage->private == 0);
114
115                 cl_env_percpu_put(env);
116         }
117 }
118
119 #ifdef HAVE_RELEASEPAGE_WITH_INT
120 #define RELEASEPAGE_ARG_TYPE int
121 #else
122 #define RELEASEPAGE_ARG_TYPE gfp_t
123 #endif
124 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
125 {
126         struct lu_env           *env;
127         struct cl_object        *obj;
128         struct cl_page          *page;
129         struct address_space    *mapping;
130         int result = 0;
131
132         LASSERT(PageLocked(vmpage));
133         if (PageWriteback(vmpage) || PageDirty(vmpage))
134                 return 0;
135
136         mapping = vmpage->mapping;
137         if (mapping == NULL)
138                 return 1;
139
140         obj = ll_i2info(mapping->host)->lli_clob;
141         if (obj == NULL)
142                 return 1;
143
144         /* 1 for caller, 1 for cl_page and 1 for page cache */
145         if (page_count(vmpage) > 3)
146                 return 0;
147
148         page = cl_vmpage_page(vmpage, obj);
149         if (page == NULL)
150                 return 1;
151
152         env = cl_env_percpu_get();
153         LASSERT(!IS_ERR(env));
154
155         if (!cl_page_in_use(page)) {
156                 result = 1;
157                 cl_page_delete(env, page);
158         }
159
160         /* To use percpu env array, the call path can not be rescheduled;
161          * otherwise percpu array will be messed if ll_releaspage() called
162          * again on the same CPU.
163          *
164          * If this page holds the last refc of cl_object, the following
165          * call path may cause reschedule:
166          *   cl_page_put -> cl_page_free -> cl_object_put ->
167          *     lu_object_put -> lu_object_free -> lov_delete_raid0.
168          *
169          * However, the kernel can't get rid of this inode until all pages have
170          * been cleaned up. Now that we hold page lock here, it's pretty safe
171          * that we won't get into object delete path.
172          */
173         LASSERT(cl_object_refc(obj) > 1);
174         cl_page_put(env, page);
175
176         cl_env_percpu_put(env);
177         return result;
178 }
179
180 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
181
182 static ssize_t
183 ll_direct_IO_seg(const struct lu_env *env, struct cl_io *io, int rw,
184                  struct inode *inode, size_t size, loff_t file_offset,
185                  struct page **pages, int page_count)
186 {
187         struct cl_page *clp;
188         struct cl_2queue *queue;
189         struct cl_object *obj = io->ci_obj;
190         int i;
191         ssize_t rc = 0;
192         size_t page_size = cl_page_size(obj);
193         size_t orig_size = size;
194         bool do_io;
195         int io_pages = 0;
196
197         ENTRY;
198         queue = &io->ci_queue;
199         cl_2queue_init(queue);
200         for (i = 0; i < page_count; i++) {
201                 LASSERT(!(file_offset & (page_size - 1)));
202                 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
203                                    pages[i], CPT_TRANSIENT);
204                 if (IS_ERR(clp)) {
205                         rc = PTR_ERR(clp);
206                         break;
207                 }
208
209                 rc = cl_page_own(env, io, clp);
210                 if (rc) {
211                         LASSERT(clp->cp_state == CPS_FREEING);
212                         cl_page_put(env, clp);
213                         break;
214                 }
215
216                 do_io = true;
217
218                 /* check the page type: if the page is a host page, then do
219                  * write directly
220                  */
221                 if (clp->cp_type == CPT_CACHEABLE) {
222                         struct page *vmpage = cl_page_vmpage(clp);
223                         struct page *src_page;
224                         struct page *dst_page;
225                         void *src;
226                         void *dst;
227
228                         src_page = (rw == WRITE) ? pages[i] : vmpage;
229                         dst_page = (rw == WRITE) ? vmpage : pages[i];
230
231                         src = ll_kmap_atomic(src_page, KM_USER0);
232                         dst = ll_kmap_atomic(dst_page, KM_USER1);
233                         memcpy(dst, src, min(page_size, size));
234                         ll_kunmap_atomic(dst, KM_USER1);
235                         ll_kunmap_atomic(src, KM_USER0);
236
237                         /* make sure page will be added to the transfer by
238                          * cl_io_submit()->...->vvp_page_prep_write().
239                          */
240                         if (rw == WRITE)
241                                 set_page_dirty(vmpage);
242
243                         if (rw == READ) {
244                                 /* do not issue the page for read, since it
245                                  * may reread a ra page which has NOT uptodate
246                                  * bit set.
247                                  */
248                                 cl_page_disown(env, io, clp);
249                                 do_io = false;
250                         }
251                 }
252
253                 if (likely(do_io)) {
254                         cl_2queue_add(queue, clp);
255
256                         /*
257                          * Set page clip to tell transfer formation engine
258                          * that page has to be sent even if it is beyond KMS.
259                          */
260                         cl_page_clip(env, clp, 0, min(size, page_size));
261
262                         ++io_pages;
263                 }
264
265                 /* drop the reference count for cl_page_find */
266                 cl_page_put(env, clp);
267                 size -= page_size;
268                 file_offset += page_size;
269         }
270
271         if (rc == 0 && io_pages) {
272                 rc = cl_io_submit_sync(env, io,
273                                        rw == READ ? CRT_READ : CRT_WRITE,
274                                        queue, 0);
275         }
276         if (rc == 0)
277                 rc = orig_size;
278
279         cl_2queue_discard(env, io, queue);
280         cl_2queue_disown(env, io, queue);
281         cl_2queue_fini(env, queue);
282         RETURN(rc);
283 }
284
285 /*  ll_free_user_pages - tear down page struct array
286  *  @pages: array of page struct pointers underlying target buffer */
287 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
288 {
289         int i;
290
291         for (i = 0; i < npages; i++) {
292                 if (pages[i] == NULL)
293                         break;
294                 if (do_dirty)
295                         set_page_dirty_lock(pages[i]);
296                 put_page(pages[i]);
297         }
298
299 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
300         kvfree(pages);
301 #else
302         OBD_FREE_LARGE(pages, npages * sizeof(*pages));
303 #endif
304 }
305
306 #ifdef KMALLOC_MAX_SIZE
307 #define MAX_MALLOC KMALLOC_MAX_SIZE
308 #else
309 #define MAX_MALLOC (128 * 1024)
310 #endif
311
312 /* This is the maximum size of a single O_DIRECT request, based on the
313  * kmalloc limit.  We need to fit all of the brw_page structs, each one
314  * representing PAGE_SIZE worth of user data, into a single buffer, and
315  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
316  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
317 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
318                       ~(DT_MAX_BRW_SIZE - 1))
319
320 #ifndef HAVE_IOV_ITER_RW
321 # define iov_iter_rw(iter)      rw
322 #endif
323
324 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
325 static ssize_t
326 ll_direct_IO(
327 # ifndef HAVE_IOV_ITER_RW
328              int rw,
329 # endif
330              struct kiocb *iocb, struct iov_iter *iter
331 # ifndef HAVE_DIRECTIO_2ARGS
332              , loff_t file_offset
333 # endif
334              )
335 {
336 #ifdef HAVE_DIRECTIO_2ARGS
337         loff_t file_offset = iocb->ki_pos;
338 #endif
339         struct ll_cl_context *lcc;
340         const struct lu_env *env;
341         struct cl_io *io;
342         struct file *file = iocb->ki_filp;
343         struct inode *inode = file->f_mapping->host;
344         ssize_t count = iov_iter_count(iter);
345         ssize_t tot_bytes = 0, result = 0;
346         size_t size = MAX_DIO_SIZE;
347
348         /* Check EOF by ourselves */
349         if (iov_iter_rw(iter) == READ && file_offset >= i_size_read(inode))
350                 return 0;
351         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
352         if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
353                 return -EINVAL;
354
355         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
356                "offset=%lld=%llx, pages %zd (max %lu)\n",
357                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
358                file_offset, file_offset, count >> PAGE_SHIFT,
359                MAX_DIO_SIZE >> PAGE_SHIFT);
360
361         /* Check that all user buffers are aligned as well */
362         if (iov_iter_alignment(iter) & ~PAGE_MASK)
363                 return -EINVAL;
364
365         lcc = ll_cl_find(file);
366         if (lcc == NULL)
367                 RETURN(-EIO);
368
369         env = lcc->lcc_env;
370         LASSERT(!IS_ERR(env));
371         io = lcc->lcc_io;
372         LASSERT(io != NULL);
373
374         /* 0. Need locking between buffered and direct access. and race with
375          *    size changing by concurrent truncates and writes.
376          * 1. Need inode mutex to operate transient pages.
377          */
378         if (iov_iter_rw(iter) == READ)
379                 inode_lock(inode);
380
381         while (iov_iter_count(iter)) {
382                 struct page **pages;
383                 size_t offs;
384
385                 count = min_t(size_t, iov_iter_count(iter), size);
386                 if (iov_iter_rw(iter) == READ) {
387                         if (file_offset >= i_size_read(inode))
388                                 break;
389
390                         if (file_offset + count > i_size_read(inode))
391                                 count = i_size_read(inode) - file_offset;
392                 }
393
394                 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
395                 if (likely(result > 0)) {
396                         int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
397
398                         result = ll_direct_IO_seg(env, io, iov_iter_rw(iter),
399                                                   inode, result, file_offset,
400                                                   pages, n);
401                         ll_free_user_pages(pages, n,
402                                            iov_iter_rw(iter) == READ);
403
404                 }
405                 if (unlikely(result <= 0)) {
406                         /* If we can't allocate a large enough buffer
407                          * for the request, shrink it to a smaller
408                          * PAGE_SIZE multiple and try again.
409                          * We should always be able to kmalloc for a
410                          * page worth of page pointers = 4MB on i386. */
411                         if (result == -ENOMEM &&
412                             size > (PAGE_SIZE / sizeof(*pages)) *
413                                     PAGE_SIZE) {
414                                 size = ((((size / 2) - 1) |
415                                         ~PAGE_MASK) + 1) & PAGE_MASK;
416                                 CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
417                                        size);
418                                 continue;
419                         }
420
421                         GOTO(out, result);
422                 }
423
424                 iov_iter_advance(iter, result);
425                 tot_bytes += result;
426                 file_offset += result;
427         }
428 out:
429         if (iov_iter_rw(iter) == READ)
430                 inode_unlock(inode);
431
432         if (tot_bytes > 0) {
433                 struct vvp_io *vio = vvp_env_io(env);
434
435                 /* no commit async for direct IO */
436                 vio->u.write.vui_written += tot_bytes;
437         }
438
439         return tot_bytes ? : result;
440 }
441 #else /* !HAVE_DIRECTIO_ITER && !HAVE_IOV_ITER_RW */
442
443 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
444                                     size_t size, struct page ***pages,
445                                     int *max_pages)
446 {
447         int result = -ENOMEM;
448
449         /* set an arbitrary limit to prevent arithmetic overflow */
450         if (size > MAX_DIRECTIO_SIZE) {
451                 *pages = NULL;
452                 return -EFBIG;
453         }
454
455         *max_pages = (user_addr + size + PAGE_SIZE - 1) >>
456                       PAGE_SHIFT;
457         *max_pages -= user_addr >> PAGE_SHIFT;
458
459         OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
460         if (*pages) {
461                 down_read(&current->mm->mmap_sem);
462                 result = get_user_pages(current, current->mm, user_addr,
463                                         *max_pages, (rw == READ), 0, *pages,
464                                         NULL);
465                 up_read(&current->mm->mmap_sem);
466                 if (unlikely(result <= 0))
467                         OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
468         }
469
470         return result;
471 }
472
473 static ssize_t
474 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
475              loff_t file_offset, unsigned long nr_segs)
476 {
477         struct ll_cl_context *lcc;
478         const struct lu_env *env;
479         struct cl_io *io;
480         struct file *file = iocb->ki_filp;
481         struct inode *inode = file->f_mapping->host;
482         ssize_t count = iov_length(iov, nr_segs);
483         ssize_t tot_bytes = 0, result = 0;
484         unsigned long seg = 0;
485         size_t size = MAX_DIO_SIZE;
486         ENTRY;
487
488         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
489         if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
490                 RETURN(-EINVAL);
491
492         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
493                "offset=%lld=%llx, pages %zd (max %lu)\n",
494                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
495                file_offset, file_offset, count >> PAGE_SHIFT,
496                MAX_DIO_SIZE >> PAGE_SHIFT);
497
498         /* Check that all user buffers are aligned as well */
499         for (seg = 0; seg < nr_segs; seg++) {
500                 if (((unsigned long)iov[seg].iov_base & ~PAGE_MASK) ||
501                     (iov[seg].iov_len & ~PAGE_MASK))
502                         RETURN(-EINVAL);
503         }
504
505         lcc = ll_cl_find(file);
506         if (lcc == NULL)
507                 RETURN(-EIO);
508
509         env = lcc->lcc_env;
510         LASSERT(!IS_ERR(env));
511         io = lcc->lcc_io;
512         LASSERT(io != NULL);
513
514         for (seg = 0; seg < nr_segs; seg++) {
515                 size_t iov_left = iov[seg].iov_len;
516                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
517
518                 if (rw == READ) {
519                         if (file_offset >= i_size_read(inode))
520                                 break;
521                         if (file_offset + iov_left > i_size_read(inode))
522                                 iov_left = i_size_read(inode) - file_offset;
523                 }
524
525                 while (iov_left > 0) {
526                         struct page **pages;
527                         int page_count, max_pages = 0;
528                         size_t bytes;
529
530                         bytes = min(size, iov_left);
531                         page_count = ll_get_user_pages(rw, user_addr, bytes,
532                                                        &pages, &max_pages);
533                         if (likely(page_count > 0)) {
534                                 if (unlikely(page_count <  max_pages))
535                                         bytes = page_count << PAGE_SHIFT;
536                                 result = ll_direct_IO_seg(env, io, rw, inode,
537                                                           bytes, file_offset,
538                                                           pages, page_count);
539                                 ll_free_user_pages(pages, max_pages, rw==READ);
540                         } else if (page_count == 0) {
541                                 GOTO(out, result = -EFAULT);
542                         } else {
543                                 result = page_count;
544                         }
545                         if (unlikely(result <= 0)) {
546                                 /* If we can't allocate a large enough buffer
547                                  * for the request, shrink it to a smaller
548                                  * PAGE_SIZE multiple and try again.
549                                  * We should always be able to kmalloc for a
550                                  * page worth of page pointers = 4MB on i386. */
551                                 if (result == -ENOMEM &&
552                                     size > (PAGE_SIZE / sizeof(*pages)) *
553                                            PAGE_SIZE) {
554                                         size = ((((size / 2) - 1) |
555                                                  ~PAGE_MASK) + 1) &
556                                                 PAGE_MASK;
557                                         CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
558                                                size);
559                                         continue;
560                                 }
561
562                                 GOTO(out, result);
563                         }
564
565                         tot_bytes += result;
566                         file_offset += result;
567                         iov_left -= result;
568                         user_addr += result;
569                 }
570         }
571 out:
572         if (tot_bytes > 0) {
573                 struct vvp_io *vio = vvp_env_io(env);
574
575                 /* no commit async for direct IO */
576                 vio->u.write.vui_written += tot_bytes;
577         }
578
579         RETURN(tot_bytes ? tot_bytes : result);
580 }
581 #endif /* HAVE_DIRECTIO_ITER || HAVE_IOV_ITER_RW */
582
583 /**
584  * Prepare partially written-to page for a write.
585  */
586 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
587                                    struct cl_page *pg)
588 {
589         struct cl_attr *attr   = vvp_env_thread_attr(env);
590         struct cl_object *obj  = io->ci_obj;
591         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
592         loff_t          offset = cl_offset(obj, vvp_index(vpg));
593         int             result;
594
595         cl_object_attr_lock(obj);
596         result = cl_object_attr_get(env, obj, attr);
597         cl_object_attr_unlock(obj);
598         if (result == 0) {
599                 /*
600                  * If are writing to a new page, no need to read old data.
601                  * The extent locking will have updated the KMS, and for our
602                  * purposes here we can treat it like i_size.
603                  */
604                 if (attr->cat_kms <= offset) {
605                         char *kaddr = ll_kmap_atomic(vpg->vpg_page, KM_USER0);
606
607                         memset(kaddr, 0, cl_page_size(obj));
608                         ll_kunmap_atomic(kaddr, KM_USER0);
609                 } else if (vpg->vpg_defer_uptodate)
610                         vpg->vpg_ra_used = 1;
611                 else
612                         result = ll_page_sync_io(env, io, pg, CRT_READ);
613         }
614         return result;
615 }
616
617 static int ll_write_begin(struct file *file, struct address_space *mapping,
618                           loff_t pos, unsigned len, unsigned flags,
619                           struct page **pagep, void **fsdata)
620 {
621         struct ll_cl_context *lcc;
622         const struct lu_env  *env = NULL;
623         struct cl_io   *io;
624         struct cl_page *page = NULL;
625
626         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
627         pgoff_t index = pos >> PAGE_SHIFT;
628         struct page *vmpage = NULL;
629         unsigned from = pos & (PAGE_SIZE - 1);
630         unsigned to = from + len;
631         int result = 0;
632         ENTRY;
633
634         CDEBUG(D_PAGE, "Writing %lu of %d to %d bytes\n", index, from, len);
635
636         lcc = ll_cl_find(file);
637         if (lcc == NULL) {
638                 io = NULL;
639                 GOTO(out, result = -EIO);
640         }
641
642         env = lcc->lcc_env;
643         io  = lcc->lcc_io;
644
645         /* To avoid deadlock, try to lock page first. */
646         vmpage = grab_cache_page_nowait(mapping, index);
647
648         if (unlikely(vmpage == NULL ||
649                      PageDirty(vmpage) || PageWriteback(vmpage))) {
650                 struct vvp_io *vio = vvp_env_io(env);
651                 struct cl_page_list *plist = &vio->u.write.vui_queue;
652
653                 /* if the page is already in dirty cache, we have to commit
654                  * the pages right now; otherwise, it may cause deadlock
655                  * because it holds page lock of a dirty page and request for
656                  * more grants. It's okay for the dirty page to be the first
657                  * one in commit page list, though. */
658                 if (vmpage != NULL && plist->pl_nr > 0) {
659                         unlock_page(vmpage);
660                         put_page(vmpage);
661                         vmpage = NULL;
662                 }
663
664                 /* commit pages and then wait for page lock */
665                 result = vvp_io_write_commit(env, io);
666                 if (result < 0)
667                         GOTO(out, result);
668
669                 if (vmpage == NULL) {
670                         vmpage = grab_cache_page_write_begin(mapping, index,
671                                                              flags);
672                         if (vmpage == NULL)
673                                 GOTO(out, result = -ENOMEM);
674                 }
675         }
676
677         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
678         if (IS_ERR(page))
679                 GOTO(out, result = PTR_ERR(page));
680
681         lcc->lcc_page = page;
682         lu_ref_add(&page->cp_reference, "cl_io", io);
683
684         cl_page_assume(env, io, page);
685         if (!PageUptodate(vmpage)) {
686                 /*
687                  * We're completely overwriting an existing page,
688                  * so _don't_ set it up to date until commit_write
689                  */
690                 if (from == 0 && to == PAGE_SIZE) {
691                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
692                         POISON_PAGE(vmpage, 0x11);
693                 } else {
694                         /* TODO: can be optimized at OSC layer to check if it
695                          * is a lockless IO. In that case, it's not necessary
696                          * to read the data. */
697                         result = ll_prepare_partial_page(env, io, page);
698                         if (result == 0)
699                                 SetPageUptodate(vmpage);
700                 }
701         }
702         if (result < 0)
703                 cl_page_unassume(env, io, page);
704         EXIT;
705 out:
706         if (result < 0) {
707                 if (vmpage != NULL) {
708                         unlock_page(vmpage);
709                         put_page(vmpage);
710                 }
711                 if (!IS_ERR_OR_NULL(page)) {
712                         lu_ref_del(&page->cp_reference, "cl_io", io);
713                         cl_page_put(env, page);
714                 }
715                 if (io)
716                         io->ci_result = result;
717         } else {
718                 *pagep = vmpage;
719                 *fsdata = lcc;
720         }
721         RETURN(result);
722 }
723
724 static int ll_write_end(struct file *file, struct address_space *mapping,
725                         loff_t pos, unsigned len, unsigned copied,
726                         struct page *vmpage, void *fsdata)
727 {
728         struct ll_cl_context *lcc = fsdata;
729         const struct lu_env *env;
730         struct cl_io *io;
731         struct vvp_io *vio;
732         struct cl_page *page;
733         unsigned from = pos & (PAGE_SIZE - 1);
734         bool unplug = false;
735         int result = 0;
736         ENTRY;
737
738         put_page(vmpage);
739
740         LASSERT(lcc != NULL);
741         env  = lcc->lcc_env;
742         page = lcc->lcc_page;
743         io   = lcc->lcc_io;
744         vio  = vvp_env_io(env);
745
746         LASSERT(cl_page_is_owned(page, io));
747         if (copied > 0) {
748                 struct cl_page_list *plist = &vio->u.write.vui_queue;
749
750                 lcc->lcc_page = NULL; /* page will be queued */
751
752                 /* Add it into write queue */
753                 cl_page_list_add(plist, page);
754                 if (plist->pl_nr == 1) /* first page */
755                         vio->u.write.vui_from = from;
756                 else
757                         LASSERT(from == 0);
758                 vio->u.write.vui_to = from + copied;
759
760                 /* To address the deadlock in balance_dirty_pages() where
761                  * this dirty page may be written back in the same thread. */
762                 if (PageDirty(vmpage))
763                         unplug = true;
764
765                 /* We may have one full RPC, commit it soon */
766                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
767                         unplug = true;
768
769                 CL_PAGE_DEBUG(D_PAGE, env, page,
770                               "queued page: %d.\n", plist->pl_nr);
771         } else {
772                 cl_page_disown(env, io, page);
773
774                 lcc->lcc_page = NULL;
775                 lu_ref_del(&page->cp_reference, "cl_io", io);
776                 cl_page_put(env, page);
777
778                 /* page list is not contiguous now, commit it now */
779                 unplug = true;
780         }
781         if (unplug ||
782             file->f_flags & O_SYNC || IS_SYNC(file_inode(file)))
783                 result = vvp_io_write_commit(env, io);
784
785         if (result < 0)
786                 io->ci_result = result;
787         RETURN(result >= 0 ? copied : result);
788 }
789
790 #ifdef CONFIG_MIGRATION
791 static int ll_migratepage(struct address_space *mapping,
792                           struct page *newpage, struct page *page
793 #ifdef HAVE_MIGRATEPAGE_4ARGS
794                           , enum migrate_mode mode
795 #endif
796         )
797 {
798         /* Always fail page migration until we have a proper implementation */
799         return -EIO;
800 }
801 #endif
802
803 const struct address_space_operations ll_aops = {
804         .readpage       = ll_readpage,
805         .direct_IO      = ll_direct_IO,
806         .writepage      = ll_writepage,
807         .writepages     = ll_writepages,
808         .set_page_dirty = __set_page_dirty_nobuffers,
809         .write_begin    = ll_write_begin,
810         .write_end      = ll_write_end,
811         .invalidatepage = ll_invalidatepage,
812         .releasepage    = (void *)ll_releasepage,
813 #ifdef CONFIG_MIGRATION
814         .migratepage    = ll_migratepage,
815 #endif
816 };