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