Whamcloud - gitweb
LU-6142 llite: Fix style issues for llite_nfs.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_tiny_write_begin(struct page *vmpage)
641 {
642         /* Page must be present, up to date, dirty, and not in writeback. */
643         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
644             PageWriteback(vmpage))
645                 return -ENODATA;
646
647         return 0;
648 }
649
650 static int ll_write_begin(struct file *file, struct address_space *mapping,
651                           loff_t pos, unsigned len, unsigned flags,
652                           struct page **pagep, void **fsdata)
653 {
654         struct ll_cl_context *lcc = NULL;
655         const struct lu_env  *env = NULL;
656         struct cl_io   *io = NULL;
657         struct cl_page *page = NULL;
658
659         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
660         pgoff_t index = pos >> PAGE_SHIFT;
661         struct page *vmpage = NULL;
662         unsigned from = pos & (PAGE_SIZE - 1);
663         unsigned to = from + len;
664         int result = 0;
665         ENTRY;
666
667         CDEBUG(D_PAGE, "Writing %lu of %d to %d bytes\n", index, from, len);
668
669         lcc = ll_cl_find(file);
670         if (lcc == NULL) {
671                 vmpage = grab_cache_page_nowait(mapping, index);
672                 result = ll_tiny_write_begin(vmpage);
673                 GOTO(out, result);
674         }
675
676         env = lcc->lcc_env;
677         io  = lcc->lcc_io;
678
679         if (file->f_flags & O_DIRECT && io->ci_designated_mirror > 0) {
680                 /* direct IO failed because it couldn't clean up cached pages,
681                  * this causes a problem for mirror write because the cached
682                  * page may belong to another mirror, which will result in
683                  * problem submitting the I/O. */
684                 GOTO(out, result = -EBUSY);
685         }
686
687 again:
688         /* To avoid deadlock, try to lock page first. */
689         vmpage = grab_cache_page_nowait(mapping, index);
690
691         if (unlikely(vmpage == NULL ||
692                      PageDirty(vmpage) || PageWriteback(vmpage))) {
693                 struct vvp_io *vio = vvp_env_io(env);
694                 struct cl_page_list *plist = &vio->u.write.vui_queue;
695
696                 /* if the page is already in dirty cache, we have to commit
697                  * the pages right now; otherwise, it may cause deadlock
698                  * because it holds page lock of a dirty page and request for
699                  * more grants. It's okay for the dirty page to be the first
700                  * one in commit page list, though. */
701                 if (vmpage != NULL && plist->pl_nr > 0) {
702                         unlock_page(vmpage);
703                         put_page(vmpage);
704                         vmpage = NULL;
705                 }
706
707                 /* commit pages and then wait for page lock */
708                 result = vvp_io_write_commit(env, io);
709                 if (result < 0)
710                         GOTO(out, result);
711
712                 if (vmpage == NULL) {
713                         vmpage = grab_cache_page_write_begin(mapping, index,
714                                                              flags);
715                         if (vmpage == NULL)
716                                 GOTO(out, result = -ENOMEM);
717                 }
718         }
719
720         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
721         if (IS_ERR(page))
722                 GOTO(out, result = PTR_ERR(page));
723
724         lcc->lcc_page = page;
725         lu_ref_add(&page->cp_reference, "cl_io", io);
726
727         cl_page_assume(env, io, page);
728         if (!PageUptodate(vmpage)) {
729                 /*
730                  * We're completely overwriting an existing page,
731                  * so _don't_ set it up to date until commit_write
732                  */
733                 if (from == 0 && to == PAGE_SIZE) {
734                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
735                         POISON_PAGE(vmpage, 0x11);
736                 } else {
737                         /* TODO: can be optimized at OSC layer to check if it
738                          * is a lockless IO. In that case, it's not necessary
739                          * to read the data. */
740                         result = ll_prepare_partial_page(env, io, page, file);
741                         if (result) {
742                                 /* vmpage should have been unlocked */
743                                 put_page(vmpage);
744                                 vmpage = NULL;
745
746                                 if (result == -EAGAIN)
747                                         goto again;
748                                 GOTO(out, result);
749                         }
750                 }
751         }
752         EXIT;
753 out:
754         if (result < 0) {
755                 if (vmpage != NULL) {
756                         unlock_page(vmpage);
757                         put_page(vmpage);
758                 }
759                 /* On tiny_write failure, page and io are always null. */
760                 if (!IS_ERR_OR_NULL(page)) {
761                         lu_ref_del(&page->cp_reference, "cl_io", io);
762                         cl_page_put(env, page);
763                 }
764                 if (io)
765                         io->ci_result = result;
766         } else {
767                 *pagep = vmpage;
768                 *fsdata = lcc;
769         }
770         RETURN(result);
771 }
772
773 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
774                              loff_t pos, unsigned int len, unsigned int copied,
775                              struct page *vmpage)
776 {
777         struct cl_page *clpage = (struct cl_page *) vmpage->private;
778         loff_t kms = pos+copied;
779         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
780         __u16 refcheck;
781         struct lu_env *env = cl_env_get(&refcheck);
782         int rc = 0;
783
784         ENTRY;
785
786         if (IS_ERR(env)) {
787                 rc = PTR_ERR(env);
788                 goto out;
789         }
790
791         /* This page is dirty in cache, so it should have a cl_page pointer
792          * set in vmpage->private.
793          */
794         LASSERT(clpage != NULL);
795
796         if (copied == 0)
797                 goto out_env;
798
799         /* Update the underlying size information in the OSC/LOV objects this
800          * page is part of.
801          */
802         cl_page_touch(env, clpage, to);
803
804 out_env:
805         cl_env_put(env, &refcheck);
806
807 out:
808         /* Must return page unlocked. */
809         unlock_page(vmpage);
810
811         RETURN(rc);
812 }
813
814 static int ll_write_end(struct file *file, struct address_space *mapping,
815                         loff_t pos, unsigned len, unsigned copied,
816                         struct page *vmpage, void *fsdata)
817 {
818         struct ll_cl_context *lcc = fsdata;
819         const struct lu_env *env;
820         struct cl_io *io;
821         struct vvp_io *vio;
822         struct cl_page *page;
823         unsigned from = pos & (PAGE_SIZE - 1);
824         bool unplug = false;
825         int result = 0;
826         ENTRY;
827
828         put_page(vmpage);
829
830         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
831
832         if (lcc == NULL) {
833                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
834                                            vmpage);
835                 GOTO(out, result);
836         }
837
838         LASSERT(lcc != NULL);
839         env  = lcc->lcc_env;
840         page = lcc->lcc_page;
841         io   = lcc->lcc_io;
842         vio  = vvp_env_io(env);
843
844         LASSERT(cl_page_is_owned(page, io));
845         if (copied > 0) {
846                 struct cl_page_list *plist = &vio->u.write.vui_queue;
847
848                 lcc->lcc_page = NULL; /* page will be queued */
849
850                 /* Add it into write queue */
851                 cl_page_list_add(plist, page);
852                 if (plist->pl_nr == 1) /* first page */
853                         vio->u.write.vui_from = from;
854                 else
855                         LASSERT(from == 0);
856                 vio->u.write.vui_to = from + copied;
857
858                 /* To address the deadlock in balance_dirty_pages() where
859                  * this dirty page may be written back in the same thread. */
860                 if (PageDirty(vmpage))
861                         unplug = true;
862
863                 /* We may have one full RPC, commit it soon */
864                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
865                         unplug = true;
866
867                 CL_PAGE_DEBUG(D_PAGE, env, page,
868                               "queued page: %d.\n", plist->pl_nr);
869         } else {
870                 cl_page_disown(env, io, page);
871
872                 lcc->lcc_page = NULL;
873                 lu_ref_del(&page->cp_reference, "cl_io", io);
874                 cl_page_put(env, page);
875
876                 /* page list is not contiguous now, commit it now */
877                 unplug = true;
878         }
879         if (unplug || io->u.ci_rw.rw_sync)
880                 result = vvp_io_write_commit(env, io);
881
882         if (result < 0)
883                 io->ci_result = result;
884
885
886 out:
887         RETURN(result >= 0 ? copied : result);
888 }
889
890 #ifdef CONFIG_MIGRATION
891 static int ll_migratepage(struct address_space *mapping,
892                           struct page *newpage, struct page *page
893 #ifdef HAVE_MIGRATEPAGE_4ARGS
894                           , enum migrate_mode mode
895 #endif
896         )
897 {
898         /* Always fail page migration until we have a proper implementation */
899         return -EIO;
900 }
901 #endif
902
903 const struct address_space_operations ll_aops = {
904         .readpage       = ll_readpage,
905         .direct_IO      = ll_direct_IO,
906         .writepage      = ll_writepage,
907         .writepages     = ll_writepages,
908         .set_page_dirty = __set_page_dirty_nobuffers,
909         .write_begin    = ll_write_begin,
910         .write_end      = ll_write_end,
911         .invalidatepage = ll_invalidatepage,
912         .releasepage    = (void *)ll_releasepage,
913 #ifdef CONFIG_MIGRATION
914         .migratepage    = ll_migratepage,
915 #endif
916 };