Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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, 2014, 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 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
183                            int rw, struct inode *inode,
184                            struct ll_dio_pages *pv)
185 {
186         struct cl_page    *clp;
187         struct cl_2queue  *queue;
188         struct cl_object  *obj = io->ci_obj;
189         int i;
190         ssize_t rc = 0;
191         loff_t file_offset  = pv->ldp_start_offset;
192         size_t size         = pv->ldp_size;
193         int page_count      = pv->ldp_nr;
194         struct page **pages = pv->ldp_pages;
195         size_t page_size    = cl_page_size(obj);
196         bool do_io;
197         int  io_pages       = 0;
198         ENTRY;
199
200         queue = &io->ci_queue;
201         cl_2queue_init(queue);
202         for (i = 0; i < page_count; i++) {
203                 if (pv->ldp_offsets)
204                     file_offset = pv->ldp_offsets[i];
205
206                 LASSERT(!(file_offset & (page_size - 1)));
207                 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
208                                    pv->ldp_pages[i], CPT_TRANSIENT);
209                 if (IS_ERR(clp)) {
210                         rc = PTR_ERR(clp);
211                         break;
212                 }
213
214                 rc = cl_page_own(env, io, clp);
215                 if (rc) {
216                         LASSERT(clp->cp_state == CPS_FREEING);
217                         cl_page_put(env, clp);
218                         break;
219                 }
220
221                 do_io = true;
222
223                 /* check the page type: if the page is a host page, then do
224                  * write directly */
225                 if (clp->cp_type == CPT_CACHEABLE) {
226                         struct page *vmpage = cl_page_vmpage(clp);
227                         struct page *src_page;
228                         struct page *dst_page;
229                         void       *src;
230                         void       *dst;
231
232                         src_page = (rw == WRITE) ? pages[i] : vmpage;
233                         dst_page = (rw == WRITE) ? vmpage : pages[i];
234
235                         src = ll_kmap_atomic(src_page, KM_USER0);
236                         dst = ll_kmap_atomic(dst_page, KM_USER1);
237                         memcpy(dst, src, min(page_size, size));
238                         ll_kunmap_atomic(dst, KM_USER1);
239                         ll_kunmap_atomic(src, KM_USER0);
240
241                         /* make sure page will be added to the transfer by
242                          * cl_io_submit()->...->vvp_page_prep_write(). */
243                         if (rw == WRITE)
244                                 set_page_dirty(vmpage);
245
246                         if (rw == READ) {
247                                 /* do not issue the page for read, since it
248                                  * may reread a ra page which has NOT uptodate
249                                  * bit set. */
250                                 cl_page_disown(env, io, clp);
251                                 do_io = false;
252                         }
253                 }
254
255                 if (likely(do_io)) {
256                         cl_2queue_add(queue, clp);
257
258                         /*
259                          * Set page clip to tell transfer formation engine
260                          * that page has to be sent even if it is beyond KMS.
261                          */
262                         cl_page_clip(env, clp, 0, min(size, page_size));
263
264                         ++io_pages;
265                 }
266
267                 /* drop the reference count for cl_page_find */
268                 cl_page_put(env, clp);
269                 size -= page_size;
270                 file_offset += page_size;
271         }
272
273         if (rc == 0 && io_pages) {
274                 rc = cl_io_submit_sync(env, io,
275                                        rw == READ ? CRT_READ : CRT_WRITE,
276                                        queue, 0);
277         }
278         if (rc == 0)
279                 rc = pv->ldp_size;
280
281         cl_2queue_discard(env, io, queue);
282         cl_2queue_disown(env, io, queue);
283         cl_2queue_fini(env, queue);
284         RETURN(rc);
285 }
286 EXPORT_SYMBOL(ll_direct_rw_pages);
287
288 static ssize_t
289 ll_direct_IO_seg(const struct lu_env *env, struct cl_io *io, int rw,
290                  struct inode *inode, size_t size, loff_t file_offset,
291                  struct page **pages, int page_count)
292 {
293         struct ll_dio_pages pvec = { .ldp_pages         = pages,
294                                      .ldp_nr            = page_count,
295                                      .ldp_size          = size,
296                                      .ldp_offsets       = NULL,
297                                      .ldp_start_offset  = file_offset
298                                    };
299
300         return ll_direct_rw_pages(env, io, rw, inode, &pvec);
301 }
302
303 /*  ll_free_user_pages - tear down page struct array
304  *  @pages: array of page struct pointers underlying target buffer */
305 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
306 {
307         int i;
308
309         for (i = 0; i < npages; i++) {
310                 if (pages[i] == NULL)
311                         break;
312                 if (do_dirty)
313                         set_page_dirty_lock(pages[i]);
314                 put_page(pages[i]);
315         }
316
317 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
318         kvfree(pages);
319 #else
320         OBD_FREE_LARGE(pages, npages * sizeof(*pages));
321 #endif
322 }
323
324 #ifdef KMALLOC_MAX_SIZE
325 #define MAX_MALLOC KMALLOC_MAX_SIZE
326 #else
327 #define MAX_MALLOC (128 * 1024)
328 #endif
329
330 /* This is the maximum size of a single O_DIRECT request, based on the
331  * kmalloc limit.  We need to fit all of the brw_page structs, each one
332  * representing PAGE_SIZE worth of user data, into a single buffer, and
333  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
334  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
335 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
336                       ~(DT_MAX_BRW_SIZE - 1))
337
338 #ifndef HAVE_IOV_ITER_RW
339 # define iov_iter_rw(iter)      rw
340 #endif
341
342 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
343 static ssize_t
344 ll_direct_IO(
345 # ifndef HAVE_IOV_ITER_RW
346              int rw,
347 # endif
348              struct kiocb *iocb, struct iov_iter *iter,
349              loff_t file_offset)
350 {
351         struct ll_cl_context *lcc;
352         const struct lu_env *env;
353         struct cl_io *io;
354         struct file *file = iocb->ki_filp;
355         struct inode *inode = file->f_mapping->host;
356         ssize_t count = iov_iter_count(iter);
357         ssize_t tot_bytes = 0, result = 0;
358         size_t size = MAX_DIO_SIZE;
359
360         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
361         if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
362                 return -EINVAL;
363
364         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
365                "offset=%lld=%llx, pages %zd (max %lu)\n",
366                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
367                file_offset, file_offset, count >> PAGE_SHIFT,
368                MAX_DIO_SIZE >> PAGE_SHIFT);
369
370         /* Check that all user buffers are aligned as well */
371         if (iov_iter_alignment(iter) & ~PAGE_MASK)
372                 return -EINVAL;
373
374         lcc = ll_cl_find(file);
375         if (lcc == NULL)
376                 RETURN(-EIO);
377
378         env = lcc->lcc_env;
379         LASSERT(!IS_ERR(env));
380         io = lcc->lcc_io;
381         LASSERT(io != NULL);
382
383         /* 0. Need locking between buffered and direct access. and race with
384          *    size changing by concurrent truncates and writes.
385          * 1. Need inode mutex to operate transient pages.
386          */
387         if (iov_iter_rw(iter) == READ)
388                 inode_lock(inode);
389
390         while (iov_iter_count(iter)) {
391                 struct page **pages;
392                 size_t offs;
393
394                 count = min_t(size_t, iov_iter_count(iter), size);
395                 if (iov_iter_rw(iter) == READ) {
396                         if (file_offset >= i_size_read(inode))
397                                 break;
398
399                         if (file_offset + count > i_size_read(inode))
400                                 count = i_size_read(inode) - file_offset;
401                 }
402
403                 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
404                 if (likely(result > 0)) {
405                         int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
406
407                         result = ll_direct_IO_seg(env, io, iov_iter_rw(iter),
408                                                   inode, result, file_offset,
409                                                   pages, n);
410                         ll_free_user_pages(pages, n,
411                                            iov_iter_rw(iter) == READ);
412
413                 }
414                 if (unlikely(result <= 0)) {
415                         /* If we can't allocate a large enough buffer
416                          * for the request, shrink it to a smaller
417                          * PAGE_SIZE multiple and try again.
418                          * We should always be able to kmalloc for a
419                          * page worth of page pointers = 4MB on i386. */
420                         if (result == -ENOMEM &&
421                             size > (PAGE_SIZE / sizeof(*pages)) *
422                                     PAGE_SIZE) {
423                                 size = ((((size / 2) - 1) |
424                                         ~PAGE_MASK) + 1) & PAGE_MASK;
425                                 CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
426                                        size);
427                                 continue;
428                         }
429
430                         GOTO(out, result);
431                 }
432
433                 iov_iter_advance(iter, result);
434                 tot_bytes += result;
435                 file_offset += result;
436         }
437 out:
438         if (iov_iter_rw(iter) == READ)
439                 inode_unlock(inode);
440
441         if (tot_bytes > 0) {
442                 struct vvp_io *vio = vvp_env_io(env);
443
444                 /* no commit async for direct IO */
445                 vio->u.write.vui_written += tot_bytes;
446         }
447
448         return tot_bytes ? : result;
449 }
450 #else /* !HAVE_DIRECTIO_ITER && !HAVE_IOV_ITER_RW */
451
452 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
453                                     size_t size, struct page ***pages,
454                                     int *max_pages)
455 {
456         int result = -ENOMEM;
457
458         /* set an arbitrary limit to prevent arithmetic overflow */
459         if (size > MAX_DIRECTIO_SIZE) {
460                 *pages = NULL;
461                 return -EFBIG;
462         }
463
464         *max_pages = (user_addr + size + PAGE_SIZE - 1) >>
465                       PAGE_SHIFT;
466         *max_pages -= user_addr >> PAGE_SHIFT;
467
468         OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
469         if (*pages) {
470                 down_read(&current->mm->mmap_sem);
471                 result = get_user_pages(current, current->mm, user_addr,
472                                         *max_pages, (rw == READ), 0, *pages,
473                                         NULL);
474                 up_read(&current->mm->mmap_sem);
475                 if (unlikely(result <= 0))
476                         OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
477         }
478
479         return result;
480 }
481
482 static ssize_t
483 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
484              loff_t file_offset, unsigned long nr_segs)
485 {
486         struct ll_cl_context *lcc;
487         const struct lu_env *env;
488         struct cl_io *io;
489         struct file *file = iocb->ki_filp;
490         struct inode *inode = file->f_mapping->host;
491         ssize_t count = iov_length(iov, nr_segs);
492         ssize_t tot_bytes = 0, result = 0;
493         unsigned long seg = 0;
494         size_t size = MAX_DIO_SIZE;
495         ENTRY;
496
497         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
498         if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
499                 RETURN(-EINVAL);
500
501         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
502                "offset=%lld=%llx, pages %zd (max %lu)\n",
503                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
504                file_offset, file_offset, count >> PAGE_SHIFT,
505                MAX_DIO_SIZE >> PAGE_SHIFT);
506
507         /* Check that all user buffers are aligned as well */
508         for (seg = 0; seg < nr_segs; seg++) {
509                 if (((unsigned long)iov[seg].iov_base & ~PAGE_MASK) ||
510                     (iov[seg].iov_len & ~PAGE_MASK))
511                         RETURN(-EINVAL);
512         }
513
514         lcc = ll_cl_find(file);
515         if (lcc == NULL)
516                 RETURN(-EIO);
517
518         env = lcc->lcc_env;
519         LASSERT(!IS_ERR(env));
520         io = lcc->lcc_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_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_SIZE / sizeof(*pages)) *
562                                            PAGE_SIZE) {
563                                         size = ((((size / 2) - 1) |
564                                                  ~PAGE_MASK) + 1) &
565                                                 PAGE_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         RETURN(tot_bytes ? tot_bytes : result);
589 }
590 #endif /* HAVE_DIRECTIO_ITER || HAVE_IOV_ITER_RW */
591
592 /**
593  * Prepare partially written-to page for a write.
594  */
595 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
596                                    struct cl_page *pg)
597 {
598         struct cl_attr *attr   = vvp_env_thread_attr(env);
599         struct cl_object *obj  = io->ci_obj;
600         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
601         loff_t          offset = cl_offset(obj, vvp_index(vpg));
602         int             result;
603
604         cl_object_attr_lock(obj);
605         result = cl_object_attr_get(env, obj, attr);
606         cl_object_attr_unlock(obj);
607         if (result == 0) {
608                 /*
609                  * If are writing to a new page, no need to read old data.
610                  * The extent locking will have updated the KMS, and for our
611                  * purposes here we can treat it like i_size.
612                  */
613                 if (attr->cat_kms <= offset) {
614                         char *kaddr = ll_kmap_atomic(vpg->vpg_page, KM_USER0);
615
616                         memset(kaddr, 0, cl_page_size(obj));
617                         ll_kunmap_atomic(kaddr, KM_USER0);
618                 } else if (vpg->vpg_defer_uptodate)
619                         vpg->vpg_ra_used = 1;
620                 else
621                         result = ll_page_sync_io(env, io, pg, CRT_READ);
622         }
623         return result;
624 }
625
626 static int ll_write_begin(struct file *file, struct address_space *mapping,
627                           loff_t pos, unsigned len, unsigned flags,
628                           struct page **pagep, void **fsdata)
629 {
630         struct ll_cl_context *lcc;
631         const struct lu_env  *env = NULL;
632         struct cl_io   *io;
633         struct cl_page *page = NULL;
634
635         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
636         pgoff_t index = pos >> PAGE_SHIFT;
637         struct page *vmpage = NULL;
638         unsigned from = pos & (PAGE_SIZE - 1);
639         unsigned to = from + len;
640         int result = 0;
641         ENTRY;
642
643         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
644
645         lcc = ll_cl_find(file);
646         if (lcc == NULL) {
647                 io = NULL;
648                 GOTO(out, result = -EIO);
649         }
650
651         env = lcc->lcc_env;
652         io  = lcc->lcc_io;
653
654         /* To avoid deadlock, try to lock page first. */
655         vmpage = grab_cache_page_nowait(mapping, index);
656
657         if (unlikely(vmpage == NULL ||
658                      PageDirty(vmpage) || PageWriteback(vmpage))) {
659                 struct vvp_io *vio = vvp_env_io(env);
660                 struct cl_page_list *plist = &vio->u.write.vui_queue;
661
662                 /* if the page is already in dirty cache, we have to commit
663                  * the pages right now; otherwise, it may cause deadlock
664                  * because it holds page lock of a dirty page and request for
665                  * more grants. It's okay for the dirty page to be the first
666                  * one in commit page list, though. */
667                 if (vmpage != NULL && plist->pl_nr > 0) {
668                         unlock_page(vmpage);
669                         put_page(vmpage);
670                         vmpage = NULL;
671                 }
672
673                 /* commit pages and then wait for page lock */
674                 result = vvp_io_write_commit(env, io);
675                 if (result < 0)
676                         GOTO(out, result);
677
678                 if (vmpage == NULL) {
679                         vmpage = grab_cache_page_write_begin(mapping, index,
680                                                              flags);
681                         if (vmpage == NULL)
682                                 GOTO(out, result = -ENOMEM);
683                 }
684         }
685
686         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
687         if (IS_ERR(page))
688                 GOTO(out, result = PTR_ERR(page));
689
690         lcc->lcc_page = page;
691         lu_ref_add(&page->cp_reference, "cl_io", io);
692
693         cl_page_assume(env, io, page);
694         if (!PageUptodate(vmpage)) {
695                 /*
696                  * We're completely overwriting an existing page,
697                  * so _don't_ set it up to date until commit_write
698                  */
699                 if (from == 0 && to == PAGE_SIZE) {
700                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
701                         POISON_PAGE(vmpage, 0x11);
702                 } else {
703                         /* TODO: can be optimized at OSC layer to check if it
704                          * is a lockless IO. In that case, it's not necessary
705                          * to read the data. */
706                         result = ll_prepare_partial_page(env, io, page);
707                         if (result == 0)
708                                 SetPageUptodate(vmpage);
709                 }
710         }
711         if (result < 0)
712                 cl_page_unassume(env, io, page);
713         EXIT;
714 out:
715         if (result < 0) {
716                 if (vmpage != NULL) {
717                         unlock_page(vmpage);
718                         put_page(vmpage);
719                 }
720                 if (!IS_ERR_OR_NULL(page)) {
721                         lu_ref_del(&page->cp_reference, "cl_io", io);
722                         cl_page_put(env, page);
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         put_page(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 };