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