Whamcloud - gitweb
LU-14651 llite: extend inode methods with user namespace arg
[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                 /* We keep the refcount from cl_page_find, so we don't need
371                  * another one here
372                  */
373                 cl_2queue_add(queue, page, false);
374                 /*
375                  * Set page clip to tell transfer formation engine
376                  * that page has to be sent even if it is beyond KMS.
377                  */
378                 if (size < page_size)
379                         cl_page_clip(env, page, 0, size);
380                 ++io_pages;
381
382                 offset += page_size;
383                 size -= page_size;
384         }
385         if (rc == 0 && io_pages > 0) {
386                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
387
388                 atomic_add(io_pages, &anchor->csi_sync_nr);
389                 /*
390                  * Avoid out-of-order execution of adding inflight
391                  * modifications count and io submit.
392                  */
393                 smp_mb();
394                 rc = cl_io_submit_rw(env, io, iot, queue);
395                 if (rc == 0) {
396                         cl_page_list_splice(&queue->c2_qout,
397                                         &pv->ldp_aio->cda_pages);
398                 } else {
399                         atomic_add(-queue->c2_qin.pl_nr,
400                                    &anchor->csi_sync_nr);
401                         cl_page_list_for_each(page, &queue->c2_qin)
402                                 page->cp_sync_io = NULL;
403                 }
404                 /* handle partially submitted reqs */
405                 if (queue->c2_qin.pl_nr > 0) {
406                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
407                                PFID(lu_object_fid(&obj->co_lu)),
408                                queue->c2_qin.pl_nr, rc);
409                         if (rc == 0)
410                                 rc = -EIO;
411                 }
412         }
413
414         cl_2queue_discard(env, io, queue);
415         cl_2queue_disown(env, io, queue);
416         cl_2queue_fini(env, queue);
417         RETURN(rc);
418 }
419
420 #ifdef KMALLOC_MAX_SIZE
421 #define MAX_MALLOC KMALLOC_MAX_SIZE
422 #else
423 #define MAX_MALLOC (128 * 1024)
424 #endif
425
426 /* This is the maximum size of a single O_DIRECT request, based on the
427  * kmalloc limit.  We need to fit all of the brw_page structs, each one
428  * representing PAGE_SIZE worth of user data, into a single buffer, and
429  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
430  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
431 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
432                       ~((size_t)DT_MAX_BRW_SIZE - 1))
433
434 static ssize_t
435 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
436 {
437         struct ll_cl_context *lcc;
438         const struct lu_env *env;
439         struct cl_io *io;
440         struct file *file = iocb->ki_filp;
441         struct inode *inode = file->f_mapping->host;
442         struct cl_dio_aio *ll_aio;
443         struct cl_dio_aio *ldp_aio;
444         size_t count = iov_iter_count(iter);
445         ssize_t tot_bytes = 0, result = 0;
446         loff_t file_offset = iocb->ki_pos;
447         struct vvp_io *vio;
448
449         /* Check EOF by ourselves */
450         if (rw == READ && file_offset >= i_size_read(inode))
451                 return 0;
452
453         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
454         if (file_offset & ~PAGE_MASK)
455                 RETURN(-EINVAL);
456
457         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
458                "offset=%lld=%llx, pages %zd (max %lu)\n",
459                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
460                file_offset, file_offset, count >> PAGE_SHIFT,
461                MAX_DIO_SIZE >> PAGE_SHIFT);
462
463         /* Check that all user buffers are aligned as well */
464         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
465                 RETURN(-EINVAL);
466
467         lcc = ll_cl_find(inode);
468         if (lcc == NULL)
469                 RETURN(-EIO);
470
471         env = lcc->lcc_env;
472         LASSERT(!IS_ERR(env));
473         vio = vvp_env_io(env);
474         io = lcc->lcc_io;
475         LASSERT(io != NULL);
476
477         ll_aio = io->ci_aio;
478         LASSERT(ll_aio);
479         LASSERT(ll_aio->cda_iocb == iocb);
480
481         while (iov_iter_count(iter)) {
482                 struct ll_dio_pages pvec = {};
483                 struct page **pages;
484
485                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
486                 if (rw == READ) {
487                         if (file_offset >= i_size_read(inode))
488                                 break;
489
490                         if (file_offset + count > i_size_read(inode))
491                                 count = i_size_read(inode) - file_offset;
492                 }
493
494                 /* this aio is freed on completion from cl_sync_io_note, so we
495                  * do not need to directly free the memory here
496                  */
497                 ldp_aio = cl_aio_alloc(iocb, ll_i2info(inode)->lli_clob, ll_aio);
498                 if (!ldp_aio)
499                         GOTO(out, result = -ENOMEM);
500                 pvec.ldp_aio = ldp_aio;
501
502                 result = ll_get_user_pages(rw, iter, &pages,
503                                            &pvec.ldp_count, count);
504                 if (unlikely(result <= 0)) {
505                         cl_sync_io_note(env, &ldp_aio->cda_sync, result);
506                         GOTO(out, result);
507                 }
508
509                 count = result;
510                 pvec.ldp_file_offset = file_offset;
511                 pvec.ldp_pages = pages;
512
513                 result = ll_direct_rw_pages(env, io, count,
514                                             rw, inode, &pvec);
515                 /* We've submitted pages and can now remove the extra
516                  * reference for that
517                  */
518                 cl_sync_io_note(env, &ldp_aio->cda_sync, result);
519                 ll_free_user_pages(pages, pvec.ldp_count);
520
521                 if (unlikely(result < 0))
522                         GOTO(out, result);
523
524                 iov_iter_advance(iter, count);
525                 tot_bytes += count;
526                 file_offset += count;
527         }
528
529 out:
530         ll_aio->cda_bytes += tot_bytes;
531
532         if (rw == WRITE)
533                 vio->u.readwrite.vui_written += tot_bytes;
534         else
535                 vio->u.readwrite.vui_read += tot_bytes;
536
537         /* We cannot do async submission - for AIO or regular DIO - unless
538          * lockless because it causes us to release the lock early.
539          *
540          * There are also several circumstances in which we must disable
541          * parallel DIO, so we check if it is enabled.
542          *
543          * The check for "is_sync_kiocb" excludes AIO, which does not need to
544          * be disabled in these situations.
545          */
546         if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio)) {
547                 ssize_t rc2;
548
549                 /* Wait here rather than doing async submission */
550                 rc2 = cl_sync_io_wait_recycle(env, &ll_aio->cda_sync, 0, 0);
551                 if (result == 0 && rc2)
552                         result = rc2;
553
554                 if (result == 0)
555                         result = tot_bytes;
556         } else if (result == 0) {
557                 result = -EIOCBQUEUED;
558         }
559
560         return result;
561 }
562
563 #if defined(HAVE_DIO_ITER)
564 static ssize_t ll_direct_IO(
565 #ifndef HAVE_IOV_ITER_RW
566              int rw,
567 #endif
568              struct kiocb *iocb, struct iov_iter *iter
569 #ifndef HAVE_DIRECTIO_2ARGS
570              , loff_t file_offset
571 #endif
572              )
573 {
574         int nrw;
575
576 #ifndef HAVE_IOV_ITER_RW
577         nrw = rw;
578 #else
579         nrw = iov_iter_rw(iter);
580 #endif
581
582         return ll_direct_IO_impl(iocb, iter, nrw);
583 }
584
585 #else /* !defined(HAVE_DIO_ITER) */
586
587 static ssize_t
588 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
589              loff_t file_offset, unsigned long nr_segs)
590 {
591         struct iov_iter iter;
592
593         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
594         return ll_direct_IO_impl(iocb, &iter, rw);
595 }
596
597 #endif /* !defined(HAVE_DIO_ITER) */
598
599 /**
600  * Prepare partially written-to page for a write.
601  * @pg is owned when passed in and disowned when it returns non-zero result to
602  * the caller.
603  */
604 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
605                                    struct cl_page *pg, struct file *file)
606 {
607         struct cl_attr *attr   = vvp_env_thread_attr(env);
608         struct cl_object *obj  = io->ci_obj;
609         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
610         loff_t          offset = cl_offset(obj, vvp_index(vpg));
611         int             result;
612         ENTRY;
613
614         cl_object_attr_lock(obj);
615         result = cl_object_attr_get(env, obj, attr);
616         cl_object_attr_unlock(obj);
617         if (result) {
618                 cl_page_disown(env, io, pg);
619                 GOTO(out, result);
620         }
621
622         /*
623          * If are writing to a new page, no need to read old data.
624          * The extent locking will have updated the KMS, and for our
625          * purposes here we can treat it like i_size.
626          */
627         if (attr->cat_kms <= offset) {
628                 char *kaddr = kmap_atomic(vpg->vpg_page);
629
630                 memset(kaddr, 0, cl_page_size(obj));
631                 kunmap_atomic(kaddr);
632                 GOTO(out, result = 0);
633         }
634
635         if (vpg->vpg_defer_uptodate) {
636                 vpg->vpg_ra_used = 1;
637                 GOTO(out, result = 0);
638         }
639
640         result = ll_io_read_page(env, io, pg, file);
641         if (result)
642                 GOTO(out, result);
643
644         /* ll_io_read_page() disowns the page */
645         result = cl_page_own(env, io, pg);
646         if (!result) {
647                 if (!PageUptodate(cl_page_vmpage(pg))) {
648                         cl_page_disown(env, io, pg);
649                         result = -EIO;
650                 }
651         } else if (result == -ENOENT) {
652                 /* page was truncated */
653                 result = -EAGAIN;
654         }
655         EXIT;
656
657 out:
658         return result;
659 }
660
661 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
662 {
663         /* Page must be present, up to date, dirty, and not in writeback. */
664         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
665             PageWriteback(vmpage) || vmpage->mapping != mapping)
666                 return -ENODATA;
667
668         return 0;
669 }
670
671 static int ll_write_begin(struct file *file, struct address_space *mapping,
672                           loff_t pos, unsigned len, unsigned flags,
673                           struct page **pagep, void **fsdata)
674 {
675         struct ll_cl_context *lcc = NULL;
676         const struct lu_env  *env = NULL;
677         struct cl_io   *io = NULL;
678         struct cl_page *page = NULL;
679         struct inode *inode = file_inode(file);
680         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
681         pgoff_t index = pos >> PAGE_SHIFT;
682         struct page *vmpage = NULL;
683         unsigned from = pos & (PAGE_SIZE - 1);
684         unsigned to = from + len;
685         int result = 0;
686         ENTRY;
687
688         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
689
690         lcc = ll_cl_find(inode);
691         if (lcc == NULL) {
692                 vmpage = grab_cache_page_nowait(mapping, index);
693                 result = ll_tiny_write_begin(vmpage, mapping);
694                 GOTO(out, result);
695         }
696
697         env = lcc->lcc_env;
698         io  = lcc->lcc_io;
699
700         if (file->f_flags & O_DIRECT) {
701                 /* direct IO failed because it couldn't clean up cached pages,
702                  * this causes a problem for mirror write because the cached
703                  * page may belong to another mirror, which will result in
704                  * problem submitting the I/O. */
705                 if (io->ci_designated_mirror > 0)
706                         GOTO(out, result = -EBUSY);
707
708                 /**
709                  * Direct write can fall back to buffered read, but DIO is done
710                  * with lockless i/o, and buffered requires LDLM locking, so
711                  * in this case we must restart without lockless.
712                  */
713                 if (!io->ci_dio_lock) {
714                         io->ci_dio_lock = 1;
715                         io->ci_need_restart = 1;
716                         GOTO(out, result = -ENOLCK);
717                 }
718         }
719 again:
720         /* To avoid deadlock, try to lock page first. */
721         vmpage = grab_cache_page_nowait(mapping, index);
722
723         if (unlikely(vmpage == NULL ||
724                      PageDirty(vmpage) || PageWriteback(vmpage))) {
725                 struct vvp_io *vio = vvp_env_io(env);
726                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
727
728                 /* if the page is already in dirty cache, we have to commit
729                  * the pages right now; otherwise, it may cause deadlock
730                  * because it holds page lock of a dirty page and request for
731                  * more grants. It's okay for the dirty page to be the first
732                  * one in commit page list, though. */
733                 if (vmpage != NULL && plist->pl_nr > 0) {
734                         unlock_page(vmpage);
735                         put_page(vmpage);
736                         vmpage = NULL;
737                 }
738
739                 /* commit pages and then wait for page lock */
740                 result = vvp_io_write_commit(env, io);
741                 if (result < 0)
742                         GOTO(out, result);
743
744                 if (vmpage == NULL) {
745                         vmpage = grab_cache_page_write_begin(mapping, index,
746                                                              flags);
747                         if (vmpage == NULL)
748                                 GOTO(out, result = -ENOMEM);
749                 }
750         }
751
752         /* page was truncated */
753         if (mapping != vmpage->mapping) {
754                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
755                 unlock_page(vmpage);
756                 put_page(vmpage);
757                 vmpage = NULL;
758                 goto again;
759         }
760
761         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
762         if (IS_ERR(page))
763                 GOTO(out, result = PTR_ERR(page));
764
765         lcc->lcc_page = page;
766         lu_ref_add(&page->cp_reference, "cl_io", io);
767
768         cl_page_assume(env, io, page);
769         if (!PageUptodate(vmpage)) {
770                 /*
771                  * We're completely overwriting an existing page,
772                  * so _don't_ set it up to date until commit_write
773                  */
774                 if (from == 0 && to == PAGE_SIZE) {
775                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
776                         POISON_PAGE(vmpage, 0x11);
777                 } else {
778                         /* TODO: can be optimized at OSC layer to check if it
779                          * is a lockless IO. In that case, it's not necessary
780                          * to read the data. */
781                         result = ll_prepare_partial_page(env, io, page, file);
782                         if (result) {
783                                 /* vmpage should have been unlocked */
784                                 put_page(vmpage);
785                                 vmpage = NULL;
786
787                                 if (result == -EAGAIN)
788                                         goto again;
789                                 GOTO(out, result);
790                         }
791                 }
792         }
793         EXIT;
794 out:
795         if (result < 0) {
796                 if (vmpage != NULL) {
797                         unlock_page(vmpage);
798                         put_page(vmpage);
799                 }
800                 /* On tiny_write failure, page and io are always null. */
801                 if (!IS_ERR_OR_NULL(page)) {
802                         lu_ref_del(&page->cp_reference, "cl_io", io);
803                         cl_page_put(env, page);
804                 }
805                 if (io)
806                         io->ci_result = result;
807         } else {
808                 *pagep = vmpage;
809                 *fsdata = lcc;
810         }
811         RETURN(result);
812 }
813
814 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
815                              loff_t pos, unsigned int len, unsigned int copied,
816                              struct page *vmpage)
817 {
818         struct cl_page *clpage = (struct cl_page *) vmpage->private;
819         loff_t kms = pos+copied;
820         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
821         __u16 refcheck;
822         struct lu_env *env = cl_env_get(&refcheck);
823         int rc = 0;
824
825         ENTRY;
826
827         if (IS_ERR(env)) {
828                 rc = PTR_ERR(env);
829                 goto out;
830         }
831
832         /* This page is dirty in cache, so it should have a cl_page pointer
833          * set in vmpage->private.
834          */
835         LASSERT(clpage != NULL);
836
837         if (copied == 0)
838                 goto out_env;
839
840         /* Update the underlying size information in the OSC/LOV objects this
841          * page is part of.
842          */
843         cl_page_touch(env, clpage, to);
844
845 out_env:
846         cl_env_put(env, &refcheck);
847
848 out:
849         /* Must return page unlocked. */
850         unlock_page(vmpage);
851
852         RETURN(rc);
853 }
854
855 static int ll_write_end(struct file *file, struct address_space *mapping,
856                         loff_t pos, unsigned len, unsigned copied,
857                         struct page *vmpage, void *fsdata)
858 {
859         struct ll_cl_context *lcc = fsdata;
860         const struct lu_env *env;
861         struct cl_io *io;
862         struct vvp_io *vio;
863         struct cl_page *page;
864         unsigned from = pos & (PAGE_SIZE - 1);
865         bool unplug = false;
866         int result = 0;
867         ENTRY;
868
869         put_page(vmpage);
870
871         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
872
873         if (lcc == NULL) {
874                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
875                                            vmpage);
876                 GOTO(out, result);
877         }
878
879         LASSERT(lcc != NULL);
880         env  = lcc->lcc_env;
881         page = lcc->lcc_page;
882         io   = lcc->lcc_io;
883         vio  = vvp_env_io(env);
884
885         LASSERT(cl_page_is_owned(page, io));
886         if (copied > 0) {
887                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
888
889                 lcc->lcc_page = NULL; /* page will be queued */
890
891                 /* Add it into write queue */
892                 cl_page_list_add(plist, page, true);
893                 if (plist->pl_nr == 1) /* first page */
894                         vio->u.readwrite.vui_from = from;
895                 else
896                         LASSERT(from == 0);
897                 vio->u.readwrite.vui_to = from + copied;
898
899                 /* To address the deadlock in balance_dirty_pages() where
900                  * this dirty page may be written back in the same thread. */
901                 if (PageDirty(vmpage))
902                         unplug = true;
903
904                 /* We may have one full RPC, commit it soon */
905                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
906                         unplug = true;
907
908                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
909                               "queued page: %d.\n", plist->pl_nr);
910         } else {
911                 cl_page_disown(env, io, page);
912
913                 lcc->lcc_page = NULL;
914                 lu_ref_del(&page->cp_reference, "cl_io", io);
915                 cl_page_put(env, page);
916
917                 /* page list is not contiguous now, commit it now */
918                 unplug = true;
919         }
920         if (unplug || io->u.ci_wr.wr_sync)
921                 result = vvp_io_write_commit(env, io);
922
923         if (result < 0)
924                 io->ci_result = result;
925
926
927 out:
928         RETURN(result >= 0 ? copied : result);
929 }
930
931 #ifdef CONFIG_MIGRATION
932 static int ll_migratepage(struct address_space *mapping,
933                           struct page *newpage, struct page *page,
934                           enum migrate_mode mode)
935 {
936         /* Always fail page migration until we have a proper implementation */
937         return -EIO;
938 }
939 #endif
940
941 const struct address_space_operations ll_aops = {
942         .readpage       = ll_readpage,
943         .direct_IO      = ll_direct_IO,
944         .writepage      = ll_writepage,
945         .writepages     = ll_writepages,
946         .set_page_dirty = __set_page_dirty_nobuffers,
947         .write_begin    = ll_write_begin,
948         .write_end      = ll_write_end,
949         .invalidatepage = ll_invalidatepage,
950         .releasepage    = (void *)ll_releasepage,
951 #ifdef CONFIG_MIGRATION
952         .migratepage    = ll_migratepage,
953 #endif
954 };