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