Whamcloud - gitweb
LU-13799 osc: Simplify clipping for transient pages
[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 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW) || \
166         defined(HAVE_DIRECTIO_2ARGS)
167 #define HAVE_DIO_ITER 1
168 #endif
169
170 /*
171  * ll_free_user_pages - tear down page struct array
172  * @pages: array of page struct pointers underlying target buffer
173  */
174 static void ll_free_user_pages(struct page **pages, int npages)
175 {
176         int i;
177
178         for (i = 0; i < npages; i++) {
179                 if (!pages[i])
180                         break;
181                 put_page(pages[i]);
182         }
183
184 #if defined(HAVE_DIO_ITER)
185         kvfree(pages);
186 #else
187         OBD_FREE_PTR_ARRAY_LARGE(pages, npages);
188 #endif
189 }
190
191 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
192                                 struct page ***pages, ssize_t *npages,
193                                 size_t maxsize)
194 {
195 #if defined(HAVE_DIO_ITER)
196         size_t start;
197         size_t result;
198
199         /*
200          * iov_iter_get_pages_alloc() is introduced in 3.16 similar
201          * to HAVE_DIO_ITER.
202          */
203         result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
204         if (result > 0)
205                 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
206
207         return result;
208 #else
209         unsigned long addr;
210         size_t page_count;
211         size_t size;
212         long result;
213
214         if (!maxsize)
215                 return 0;
216
217         if (!iter->nr_segs)
218                 return 0;
219
220         addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
221         if (addr & ~PAGE_MASK)
222                 return -EINVAL;
223
224         size = min_t(size_t, maxsize, iter->iov->iov_len);
225         page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
226         OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
227         if (*pages == NULL)
228                 return -ENOMEM;
229
230         mmap_read_lock(current->mm);
231         result = get_user_pages(current, current->mm, addr, page_count,
232                                 rw == READ, 0, *pages, NULL);
233         mmap_read_unlock(current->mm);
234
235         if (unlikely(result != page_count)) {
236                 ll_free_user_pages(*pages, page_count);
237                 *pages = NULL;
238
239                 if (result >= 0)
240                         return -EFAULT;
241
242                 return result;
243         }
244         *npages = page_count;
245
246         return size;
247 #endif
248 }
249
250 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
251 #if defined(HAVE_DIO_ITER)
252 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
253 {
254         return iov_iter_alignment(i);
255 }
256 #else /* copied from alignment_iovec() */
257 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
258 {
259         const struct iovec *iov = i->iov;
260         unsigned long res;
261         size_t size = i->count;
262         size_t n;
263
264         if (!size)
265                 return 0;
266
267         res = (unsigned long)iov->iov_base + i->iov_offset;
268         n = iov->iov_len - i->iov_offset;
269         if (n >= size)
270                 return res | size;
271
272         size -= n;
273         res |= n;
274         while (size > (++iov)->iov_len) {
275                 res |= (unsigned long)iov->iov_base | iov->iov_len;
276                 size -= iov->iov_len;
277         }
278         res |= (unsigned long)iov->iov_base | size;
279
280         return res;
281 }
282 #endif
283
284 /*
285  * Lustre could relax a bit for alignment, io count is not
286  * necessary page alignment.
287  */
288 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
289 {
290         size_t orig_size = i->count;
291         size_t count = orig_size & ~PAGE_MASK;
292         unsigned long res;
293
294         if (!count)
295                 return iov_iter_alignment_vfs(i);
296
297         if (orig_size > PAGE_SIZE) {
298                 iov_iter_truncate(i, orig_size - count);
299                 res = iov_iter_alignment_vfs(i);
300                 iov_iter_reexpand(i, orig_size);
301
302                 return res;
303         }
304
305         res = iov_iter_alignment_vfs(i);
306         /* start address is page aligned */
307         if ((res & ~PAGE_MASK) == orig_size)
308                 return PAGE_SIZE;
309
310         return res;
311 }
312
313 /** direct IO pages */
314 struct ll_dio_pages {
315         struct cl_dio_aio       *ldp_aio;
316         /*
317          * page array to be written. we don't support
318          * partial pages except the last one.
319          */
320         struct page             **ldp_pages;
321         /** # of pages in the array. */
322         size_t                  ldp_count;
323         /* the file offset of the first page. */
324         loff_t                  ldp_file_offset;
325 };
326
327 static int
328 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
329                    int rw, struct inode *inode, struct ll_dio_pages *pv)
330 {
331         struct cl_page    *page;
332         struct cl_2queue  *queue = &io->ci_queue;
333         struct cl_object  *obj = io->ci_obj;
334         struct cl_sync_io *anchor = &pv->ldp_aio->cda_sync;
335         loff_t offset   = pv->ldp_file_offset;
336         int io_pages    = 0;
337         size_t page_size = cl_page_size(obj);
338         int i;
339         ssize_t rc = 0;
340
341         ENTRY;
342
343         cl_2queue_init(queue);
344         for (i = 0; i < pv->ldp_count; i++) {
345                 LASSERT(!(offset & (PAGE_SIZE - 1)));
346                 page = cl_page_find(env, obj, cl_index(obj, offset),
347                                     pv->ldp_pages[i], CPT_TRANSIENT);
348                 if (IS_ERR(page)) {
349                         rc = PTR_ERR(page);
350                         break;
351                 }
352                 LASSERT(page->cp_type == CPT_TRANSIENT);
353                 rc = cl_page_own(env, io, page);
354                 if (rc) {
355                         cl_page_put(env, page);
356                         break;
357                 }
358
359                 page->cp_sync_io = anchor;
360                 if (inode && IS_ENCRYPTED(inode)) {
361                         /* In case of Direct IO on encrypted file, we need to
362                          * add a reference to the inode on the cl_page.
363                          * This info is required by llcrypt to proceed
364                          * to encryption/decryption.
365                          * This is safe because we know these pages are private
366                          * to the thread doing the Direct IO.
367                          */
368                         page->cp_inode = inode;
369                 }
370                 cl_2queue_add(queue, page);
371                 /*
372                  * Set page clip to tell transfer formation engine
373                  * that page has to be sent even if it is beyond KMS.
374                  */
375                 if (size < page_size)
376                         cl_page_clip(env, page, 0, size);
377                 ++io_pages;
378
379                 /* drop the reference count for cl_page_find */
380                 cl_page_put(env, page);
381                 offset += page_size;
382                 size -= page_size;
383         }
384         if (rc == 0 && io_pages > 0) {
385                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
386
387                 atomic_add(io_pages, &anchor->csi_sync_nr);
388                 /*
389                  * Avoid out-of-order execution of adding inflight
390                  * modifications count and io submit.
391                  */
392                 smp_mb();
393                 rc = cl_io_submit_rw(env, io, iot, queue);
394                 if (rc == 0) {
395                         cl_page_list_splice(&queue->c2_qout,
396                                         &pv->ldp_aio->cda_pages);
397                 } else {
398                         atomic_add(-queue->c2_qin.pl_nr,
399                                    &anchor->csi_sync_nr);
400                         cl_page_list_for_each(page, &queue->c2_qin)
401                                 page->cp_sync_io = NULL;
402                 }
403                 /* handle partially submitted reqs */
404                 if (queue->c2_qin.pl_nr > 0) {
405                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
406                                PFID(lu_object_fid(&obj->co_lu)),
407                                queue->c2_qin.pl_nr, rc);
408                         if (rc == 0)
409                                 rc = -EIO;
410                 }
411         }
412
413         cl_2queue_discard(env, io, queue);
414         cl_2queue_disown(env, io, queue);
415         cl_2queue_fini(env, queue);
416         RETURN(rc);
417 }
418
419 #ifdef KMALLOC_MAX_SIZE
420 #define MAX_MALLOC KMALLOC_MAX_SIZE
421 #else
422 #define MAX_MALLOC (128 * 1024)
423 #endif
424
425 /* This is the maximum size of a single O_DIRECT request, based on the
426  * kmalloc limit.  We need to fit all of the brw_page structs, each one
427  * representing PAGE_SIZE worth of user data, into a single buffer, and
428  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
429  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
430 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
431                       ~((size_t)DT_MAX_BRW_SIZE - 1))
432
433 static ssize_t
434 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
435 {
436         struct ll_cl_context *lcc;
437         const struct lu_env *env;
438         struct cl_io *io;
439         struct file *file = iocb->ki_filp;
440         struct inode *inode = file->f_mapping->host;
441         struct cl_dio_aio *aio;
442         size_t count = iov_iter_count(iter);
443         ssize_t tot_bytes = 0, result = 0;
444         loff_t file_offset = iocb->ki_pos;
445         struct vvp_io *vio;
446
447         /* Check EOF by ourselves */
448         if (rw == READ && file_offset >= i_size_read(inode))
449                 return 0;
450
451         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
452         if (file_offset & ~PAGE_MASK)
453                 RETURN(-EINVAL);
454
455         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
456                "offset=%lld=%llx, pages %zd (max %lu)\n",
457                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
458                file_offset, file_offset, count >> PAGE_SHIFT,
459                MAX_DIO_SIZE >> PAGE_SHIFT);
460
461         /* Check that all user buffers are aligned as well */
462         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
463                 RETURN(-EINVAL);
464
465         lcc = ll_cl_find(file);
466         if (lcc == NULL)
467                 RETURN(-EIO);
468
469         env = lcc->lcc_env;
470         LASSERT(!IS_ERR(env));
471         vio = vvp_env_io(env);
472         io = lcc->lcc_io;
473         LASSERT(io != NULL);
474
475         aio = io->ci_aio;
476         LASSERT(aio);
477         LASSERT(aio->cda_iocb == iocb);
478
479         while (iov_iter_count(iter)) {
480                 struct ll_dio_pages pvec = { .ldp_aio = aio };
481                 struct page **pages;
482
483                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
484                 if (rw == READ) {
485                         if (file_offset >= i_size_read(inode))
486                                 break;
487
488                         if (file_offset + count > i_size_read(inode))
489                                 count = i_size_read(inode) - file_offset;
490                 }
491
492                 result = ll_get_user_pages(rw, iter, &pages,
493                                            &pvec.ldp_count, count);
494                 if (unlikely(result <= 0))
495                         GOTO(out, result);
496
497                 count = result;
498                 pvec.ldp_file_offset = file_offset;
499                 pvec.ldp_pages = pages;
500
501                 result = ll_direct_rw_pages(env, io, count,
502                                             rw, inode, &pvec);
503                 ll_free_user_pages(pages, pvec.ldp_count);
504
505                 if (unlikely(result < 0))
506                         GOTO(out, result);
507
508                 iov_iter_advance(iter, count);
509                 tot_bytes += count;
510                 file_offset += count;
511         }
512
513 out:
514         aio->cda_bytes += tot_bytes;
515
516         if (rw == WRITE)
517                 vio->u.readwrite.vui_written += tot_bytes;
518         else
519                 vio->u.readwrite.vui_read += tot_bytes;
520
521         /* If async dio submission is not allowed, we must wait here. */
522         if (is_sync_kiocb(iocb) && !io->ci_parallel_dio) {
523                 ssize_t rc2;
524
525                 rc2 = cl_sync_io_wait_recycle(env, &aio->cda_sync, 0, 0);
526                 if (result == 0 && rc2)
527                         result = rc2;
528
529                 if (result == 0)
530                         result = tot_bytes;
531         } else if (result == 0) {
532                 result = -EIOCBQUEUED;
533         }
534
535         return result;
536 }
537
538 #if defined(HAVE_DIO_ITER)
539 static ssize_t ll_direct_IO(
540 #ifndef HAVE_IOV_ITER_RW
541              int rw,
542 #endif
543              struct kiocb *iocb, struct iov_iter *iter
544 #ifndef HAVE_DIRECTIO_2ARGS
545              , loff_t file_offset
546 #endif
547              )
548 {
549         int nrw;
550
551 #ifndef HAVE_IOV_ITER_RW
552         nrw = rw;
553 #else
554         nrw = iov_iter_rw(iter);
555 #endif
556
557         return ll_direct_IO_impl(iocb, iter, nrw);
558 }
559
560 #else /* !defined(HAVE_DIO_ITER) */
561
562 static ssize_t
563 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
564              loff_t file_offset, unsigned long nr_segs)
565 {
566         struct iov_iter iter;
567
568         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
569         return ll_direct_IO_impl(iocb, &iter, rw);
570 }
571
572 #endif /* !defined(HAVE_DIO_ITER) */
573
574 /**
575  * Prepare partially written-to page for a write.
576  * @pg is owned when passed in and disowned when it returns non-zero result to
577  * the caller.
578  */
579 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
580                                    struct cl_page *pg, struct file *file)
581 {
582         struct cl_attr *attr   = vvp_env_thread_attr(env);
583         struct cl_object *obj  = io->ci_obj;
584         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
585         loff_t          offset = cl_offset(obj, vvp_index(vpg));
586         int             result;
587         ENTRY;
588
589         cl_object_attr_lock(obj);
590         result = cl_object_attr_get(env, obj, attr);
591         cl_object_attr_unlock(obj);
592         if (result) {
593                 cl_page_disown(env, io, pg);
594                 GOTO(out, result);
595         }
596
597         /*
598          * If are writing to a new page, no need to read old data.
599          * The extent locking will have updated the KMS, and for our
600          * purposes here we can treat it like i_size.
601          */
602         if (attr->cat_kms <= offset) {
603                 char *kaddr = kmap_atomic(vpg->vpg_page);
604
605                 memset(kaddr, 0, cl_page_size(obj));
606                 kunmap_atomic(kaddr);
607                 GOTO(out, result = 0);
608         }
609
610         if (vpg->vpg_defer_uptodate) {
611                 vpg->vpg_ra_used = 1;
612                 GOTO(out, result = 0);
613         }
614
615         result = ll_io_read_page(env, io, pg, file);
616         if (result)
617                 GOTO(out, result);
618
619         /* ll_io_read_page() disowns the page */
620         result = cl_page_own(env, io, pg);
621         if (!result) {
622                 if (!PageUptodate(cl_page_vmpage(pg))) {
623                         cl_page_disown(env, io, pg);
624                         result = -EIO;
625                 }
626         } else if (result == -ENOENT) {
627                 /* page was truncated */
628                 result = -EAGAIN;
629         }
630         EXIT;
631
632 out:
633         return result;
634 }
635
636 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
637 {
638         /* Page must be present, up to date, dirty, and not in writeback. */
639         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
640             PageWriteback(vmpage) || vmpage->mapping != mapping)
641                 return -ENODATA;
642
643         return 0;
644 }
645
646 static int ll_write_begin(struct file *file, struct address_space *mapping,
647                           loff_t pos, unsigned len, unsigned flags,
648                           struct page **pagep, void **fsdata)
649 {
650         struct ll_cl_context *lcc = NULL;
651         const struct lu_env  *env = NULL;
652         struct cl_io   *io = NULL;
653         struct cl_page *page = NULL;
654
655         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
656         pgoff_t index = pos >> PAGE_SHIFT;
657         struct page *vmpage = NULL;
658         unsigned from = pos & (PAGE_SIZE - 1);
659         unsigned to = from + len;
660         int result = 0;
661         ENTRY;
662
663         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
664
665         lcc = ll_cl_find(file);
666         if (lcc == NULL) {
667                 vmpage = grab_cache_page_nowait(mapping, index);
668                 result = ll_tiny_write_begin(vmpage, mapping);
669                 GOTO(out, result);
670         }
671
672         env = lcc->lcc_env;
673         io  = lcc->lcc_io;
674
675         if (file->f_flags & O_DIRECT) {
676                 /* direct IO failed because it couldn't clean up cached pages,
677                  * this causes a problem for mirror write because the cached
678                  * page may belong to another mirror, which will result in
679                  * problem submitting the I/O. */
680                 if (io->ci_designated_mirror > 0)
681                         GOTO(out, result = -EBUSY);
682
683                 /**
684                  * Direct write can fall back to buffered read, but DIO is done
685                  * with lockless i/o, and buffered requires LDLM locking, so
686                  * in this case we must restart without lockless.
687                  */
688                 if (!io->ci_dio_lock) {
689                         io->ci_dio_lock = 1;
690                         io->ci_need_restart = 1;
691                         GOTO(out, result = -ENOLCK);
692                 }
693         }
694 again:
695         /* To avoid deadlock, try to lock page first. */
696         vmpage = grab_cache_page_nowait(mapping, index);
697
698         if (unlikely(vmpage == NULL ||
699                      PageDirty(vmpage) || PageWriteback(vmpage))) {
700                 struct vvp_io *vio = vvp_env_io(env);
701                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
702
703                 /* if the page is already in dirty cache, we have to commit
704                  * the pages right now; otherwise, it may cause deadlock
705                  * because it holds page lock of a dirty page and request for
706                  * more grants. It's okay for the dirty page to be the first
707                  * one in commit page list, though. */
708                 if (vmpage != NULL && plist->pl_nr > 0) {
709                         unlock_page(vmpage);
710                         put_page(vmpage);
711                         vmpage = NULL;
712                 }
713
714                 /* commit pages and then wait for page lock */
715                 result = vvp_io_write_commit(env, io);
716                 if (result < 0)
717                         GOTO(out, result);
718
719                 if (vmpage == NULL) {
720                         vmpage = grab_cache_page_write_begin(mapping, index,
721                                                              flags);
722                         if (vmpage == NULL)
723                                 GOTO(out, result = -ENOMEM);
724                 }
725         }
726
727         /* page was truncated */
728         if (mapping != vmpage->mapping) {
729                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
730                 unlock_page(vmpage);
731                 put_page(vmpage);
732                 vmpage = NULL;
733                 goto again;
734         }
735
736         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
737         if (IS_ERR(page))
738                 GOTO(out, result = PTR_ERR(page));
739
740         lcc->lcc_page = page;
741         lu_ref_add(&page->cp_reference, "cl_io", io);
742
743         cl_page_assume(env, io, page);
744         if (!PageUptodate(vmpage)) {
745                 /*
746                  * We're completely overwriting an existing page,
747                  * so _don't_ set it up to date until commit_write
748                  */
749                 if (from == 0 && to == PAGE_SIZE) {
750                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
751                         POISON_PAGE(vmpage, 0x11);
752                 } else {
753                         /* TODO: can be optimized at OSC layer to check if it
754                          * is a lockless IO. In that case, it's not necessary
755                          * to read the data. */
756                         result = ll_prepare_partial_page(env, io, page, file);
757                         if (result) {
758                                 /* vmpage should have been unlocked */
759                                 put_page(vmpage);
760                                 vmpage = NULL;
761
762                                 if (result == -EAGAIN)
763                                         goto again;
764                                 GOTO(out, result);
765                         }
766                 }
767         }
768         EXIT;
769 out:
770         if (result < 0) {
771                 if (vmpage != NULL) {
772                         unlock_page(vmpage);
773                         put_page(vmpage);
774                 }
775                 /* On tiny_write failure, page and io are always null. */
776                 if (!IS_ERR_OR_NULL(page)) {
777                         lu_ref_del(&page->cp_reference, "cl_io", io);
778                         cl_page_put(env, page);
779                 }
780                 if (io)
781                         io->ci_result = result;
782         } else {
783                 *pagep = vmpage;
784                 *fsdata = lcc;
785         }
786         RETURN(result);
787 }
788
789 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
790                              loff_t pos, unsigned int len, unsigned int copied,
791                              struct page *vmpage)
792 {
793         struct cl_page *clpage = (struct cl_page *) vmpage->private;
794         loff_t kms = pos+copied;
795         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
796         __u16 refcheck;
797         struct lu_env *env = cl_env_get(&refcheck);
798         int rc = 0;
799
800         ENTRY;
801
802         if (IS_ERR(env)) {
803                 rc = PTR_ERR(env);
804                 goto out;
805         }
806
807         /* This page is dirty in cache, so it should have a cl_page pointer
808          * set in vmpage->private.
809          */
810         LASSERT(clpage != NULL);
811
812         if (copied == 0)
813                 goto out_env;
814
815         /* Update the underlying size information in the OSC/LOV objects this
816          * page is part of.
817          */
818         cl_page_touch(env, clpage, to);
819
820 out_env:
821         cl_env_put(env, &refcheck);
822
823 out:
824         /* Must return page unlocked. */
825         unlock_page(vmpage);
826
827         RETURN(rc);
828 }
829
830 static int ll_write_end(struct file *file, struct address_space *mapping,
831                         loff_t pos, unsigned len, unsigned copied,
832                         struct page *vmpage, void *fsdata)
833 {
834         struct ll_cl_context *lcc = fsdata;
835         const struct lu_env *env;
836         struct cl_io *io;
837         struct vvp_io *vio;
838         struct cl_page *page;
839         unsigned from = pos & (PAGE_SIZE - 1);
840         bool unplug = false;
841         int result = 0;
842         ENTRY;
843
844         put_page(vmpage);
845
846         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
847
848         if (lcc == NULL) {
849                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
850                                            vmpage);
851                 GOTO(out, result);
852         }
853
854         LASSERT(lcc != NULL);
855         env  = lcc->lcc_env;
856         page = lcc->lcc_page;
857         io   = lcc->lcc_io;
858         vio  = vvp_env_io(env);
859
860         LASSERT(cl_page_is_owned(page, io));
861         if (copied > 0) {
862                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
863
864                 lcc->lcc_page = NULL; /* page will be queued */
865
866                 /* Add it into write queue */
867                 cl_page_list_add(plist, page);
868                 if (plist->pl_nr == 1) /* first page */
869                         vio->u.readwrite.vui_from = from;
870                 else
871                         LASSERT(from == 0);
872                 vio->u.readwrite.vui_to = from + copied;
873
874                 /* To address the deadlock in balance_dirty_pages() where
875                  * this dirty page may be written back in the same thread. */
876                 if (PageDirty(vmpage))
877                         unplug = true;
878
879                 /* We may have one full RPC, commit it soon */
880                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
881                         unplug = true;
882
883                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
884                               "queued page: %d.\n", plist->pl_nr);
885         } else {
886                 cl_page_disown(env, io, page);
887
888                 lcc->lcc_page = NULL;
889                 lu_ref_del(&page->cp_reference, "cl_io", io);
890                 cl_page_put(env, page);
891
892                 /* page list is not contiguous now, commit it now */
893                 unplug = true;
894         }
895         if (unplug || io->u.ci_wr.wr_sync)
896                 result = vvp_io_write_commit(env, io);
897
898         if (result < 0)
899                 io->ci_result = result;
900
901
902 out:
903         RETURN(result >= 0 ? copied : result);
904 }
905
906 #ifdef CONFIG_MIGRATION
907 static int ll_migratepage(struct address_space *mapping,
908                           struct page *newpage, struct page *page,
909                           enum migrate_mode mode)
910 {
911         /* Always fail page migration until we have a proper implementation */
912         return -EIO;
913 }
914 #endif
915
916 const struct address_space_operations ll_aops = {
917         .readpage       = ll_readpage,
918         .direct_IO      = ll_direct_IO,
919         .writepage      = ll_writepage,
920         .writepages     = ll_writepages,
921         .set_page_dirty = __set_page_dirty_nobuffers,
922         .write_begin    = ll_write_begin,
923         .write_end      = ll_write_end,
924         .invalidatepage = ll_invalidatepage,
925         .releasepage    = (void *)ll_releasepage,
926 #ifdef CONFIG_MIGRATION
927         .migratepage    = ll_migratepage,
928 #endif
929 };