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