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