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