Whamcloud - gitweb
LU-16327 llite: read_folio, release_folio, filler_t
[fs/lustre-release.git] / lustre / llite / rw26.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lustre/llite/rw26.c
32  *
33  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
34  */
35
36 #include <linux/buffer_head.h>
37 #include <linux/errno.h>
38 #include <linux/fs.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/mpage.h>
42 #include <linux/pagemap.h>
43 #include <linux/string.h>
44 #include <linux/unistd.h>
45 #include <linux/writeback.h>
46 #include <linux/migrate.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include "llite_internal.h"
51 #include <lustre_compat.h>
52
53 #ifdef HAVE_INVALIDATE_FOLIO
54 /**
55  * Implements Linux VM address_space::invalidate_folio() method. This method is
56  * called when the folio is truncated from a file, either as a result of
57  * explicit truncate, or when inode is removed from memory (as a result of
58  * final iput(), umount, or memory pressure induced icache shrinking).
59  *
60  * [0, off] bytes of the folio remain valid (this is for a case of non-page
61  * aligned truncate). Lustre leaves partially truncated folios in the cache,
62  * relying on struct inode::i_size to limit further accesses.
63  */
64 static void ll_invalidate_folio(struct folio *folio, size_t offset, size_t len)
65 {
66         struct inode *inode;
67         struct lu_env *env;
68         struct cl_page *page;
69         struct cl_object *obj;
70
71         LASSERT(!folio_test_writeback(folio));
72         LASSERT(folio_test_locked(folio));
73
74         if (!(offset == 0 && len == folio_size(folio)) &&
75             !folio_test_large(folio))
76                 return;
77
78         /* Drop the pages from the folio */
79         env = cl_env_percpu_get();
80         LASSERT(!IS_ERR(env));
81
82         inode = folio_inode(folio);
83         obj = ll_i2info(inode)->lli_clob;
84         if (obj != NULL) {
85                 int n, npgs = folio_nr_pages(folio);
86
87                 for (n = 0; n < npgs; n++) {
88                         struct page *vmpage = folio_page(folio, n);
89
90                         LASSERT(PageLocked(vmpage));
91                         LASSERT(!PageWriteback(vmpage));
92
93                         page = cl_vmpage_page(vmpage, obj);
94                         if (page != NULL) {
95                                 cl_page_delete(env, page);
96                                 cl_page_put(env, page);
97                         }
98                 }
99         } else {
100                 LASSERT(!folio_get_private(folio));
101         }
102         cl_env_percpu_put(env);
103 }
104 #else
105
106 /**
107  * Implements Linux VM address_space::invalidatepage() method. This method is
108  * called when the page is truncate from a file, either as a result of
109  * explicit truncate, or when inode is removed from memory (as a result of
110  * final iput(), umount, or memory pressure induced icache shrinking).
111  *
112  * [0, offset] bytes of the page remain valid (this is for a case of not-page
113  * aligned truncate). Lustre leaves partially truncated page in the cache,
114  * relying on struct inode::i_size to limit further accesses.
115  */
116 static void ll_invalidatepage(struct page *vmpage,
117 #ifdef HAVE_INVALIDATE_RANGE
118                                 unsigned int offset, unsigned int length
119 #else
120                                 unsigned long offset
121 #endif
122                              )
123 {
124         struct inode     *inode;
125         struct lu_env    *env;
126         struct cl_page   *page;
127         struct cl_object *obj;
128
129         LASSERT(PageLocked(vmpage));
130         LASSERT(!PageWriteback(vmpage));
131
132         /*
133          * It is safe to not check anything in invalidatepage/releasepage
134          * below because they are run with page locked and all our io is
135          * happening with locked page too
136          */
137 #ifdef HAVE_INVALIDATE_RANGE
138         if (offset == 0 && length == PAGE_SIZE) {
139 #else
140         if (offset == 0) {
141 #endif
142                 /* See the comment in ll_releasepage() */
143                 env = cl_env_percpu_get();
144                 LASSERT(!IS_ERR(env));
145
146                 inode = vmpage->mapping->host;
147                 obj = ll_i2info(inode)->lli_clob;
148                 if (obj != NULL) {
149                         page = cl_vmpage_page(vmpage, obj);
150                         if (page != NULL) {
151                                 cl_page_delete(env, page);
152                                 cl_page_put(env, page);
153                         }
154                 } else
155                         LASSERT(vmpage->private == 0);
156
157                 cl_env_percpu_put(env);
158         }
159
160         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE)) {
161                 unlock_page(vmpage);
162                 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE,
163                                  cfs_fail_val);
164                 lock_page(vmpage);
165         }
166 }
167 #endif
168
169 static bool do_release_page(struct page *vmpage, gfp_t wait)
170 {
171         struct lu_env *env;
172         struct cl_object *obj;
173         struct cl_page *clpage;
174         struct address_space *mapping;
175         int result = 0;
176
177         LASSERT(PageLocked(vmpage));
178         if (PageWriteback(vmpage) || PageDirty(vmpage))
179                 return 0;
180
181         mapping = vmpage->mapping;
182         if (mapping == NULL)
183                 return 1;
184
185         obj = ll_i2info(mapping->host)->lli_clob;
186         if (obj == NULL)
187                 return 1;
188
189         clpage = cl_vmpage_page(vmpage, obj);
190         if (clpage == NULL)
191                 return 1;
192
193         env = cl_env_percpu_get();
194         LASSERT(!IS_ERR(env));
195
196         /* we must not delete the cl_page if the vmpage is in use, otherwise we
197          * disconnect the vmpage from Lustre while it's still alive(!), which
198          * means we won't find it to discard on lock cancellation.
199          *
200          * References here are: caller + cl_page + page cache.
201          * Any other references are potentially transient and must be ignored.
202          */
203         if (!cl_page_in_use(clpage) && !vmpage_in_use(vmpage, 1)) {
204                 result = 1;
205                 cl_page_delete(env, clpage);
206         }
207
208         /* To use percpu env array, the call path can not be rescheduled;
209          * otherwise percpu array will be messed if ll_releaspage() called
210          * again on the same CPU.
211          *
212          * If this page holds the last refc of cl_object, the following
213          * call path may cause reschedule:
214          *   cl_page_put -> cl_page_free -> cl_object_put ->
215          *     lu_object_put -> lu_object_free -> lov_delete_raid0.
216          *
217          * However, the kernel can't get rid of this inode until all pages have
218          * been cleaned up. Now that we hold page lock here, it's pretty safe
219          * that we won't get into object delete path.
220          */
221         LASSERT(cl_object_refc(obj) > 1);
222         cl_page_put(env, clpage);
223
224         cl_env_percpu_put(env);
225         return result;
226 }
227
228 #ifdef HAVE_AOPS_RELEASE_FOLIO
229 static bool ll_release_folio(struct folio *folio, gfp_t wait)
230 {
231         struct page *vmpage = folio_page(folio, 0);
232
233         /* folio_nr_pages(folio) == 1 is fixed with grab_cache_page* */
234         BUG_ON(folio_nr_pages(folio) != 1);
235
236         return do_release_page(vmpage, wait);
237 }
238 #else /* !HAVE_AOPS_RELEASE_FOLIO */
239 #ifdef HAVE_RELEASEPAGE_WITH_INT
240 #define RELEASEPAGE_ARG_TYPE int
241 #else
242 #define RELEASEPAGE_ARG_TYPE gfp_t
243 #endif
244 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
245 {
246         return do_release_page(vmpage, gfp_mask);
247 }
248 #endif /* HAVE_AOPS_RELEASE_FOLIO */
249
250 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
251                                 struct page ***pages, ssize_t *npages,
252                                 size_t maxsize)
253 {
254 #if defined(HAVE_DIO_ITER)
255         size_t start;
256         size_t result;
257
258         /*
259          * iov_iter_get_pages_alloc() is introduced in 3.16 similar
260          * to HAVE_DIO_ITER.
261          */
262         result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
263         if (result > 0)
264                 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
265
266         return result;
267 #else
268         unsigned long addr;
269         size_t page_count;
270         size_t size;
271         long result;
272
273         if (!maxsize)
274                 return 0;
275
276         if (!iter->nr_segs)
277                 return 0;
278
279         addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
280         if (addr & ~PAGE_MASK)
281                 return -EINVAL;
282
283         size = min_t(size_t, maxsize, iter->iov->iov_len);
284         page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
285         OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
286         if (*pages == NULL)
287                 return -ENOMEM;
288
289         mmap_read_lock(current->mm);
290         result = get_user_pages(current, current->mm, addr, page_count,
291                                 rw == READ, 0, *pages, NULL);
292         mmap_read_unlock(current->mm);
293
294         if (unlikely(result != page_count)) {
295                 ll_release_user_pages(*pages, page_count);
296                 *pages = NULL;
297
298                 if (result >= 0)
299                         return -EFAULT;
300
301                 return result;
302         }
303         *npages = page_count;
304
305         return size;
306 #endif
307 }
308
309 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
310 #if defined(HAVE_DIO_ITER)
311 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
312 {
313         return iov_iter_alignment(i);
314 }
315 #else /* copied from alignment_iovec() */
316 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
317 {
318         const struct iovec *iov = i->iov;
319         unsigned long res;
320         size_t size = i->count;
321         size_t n;
322
323         if (!size)
324                 return 0;
325
326         res = (unsigned long)iov->iov_base + i->iov_offset;
327         n = iov->iov_len - i->iov_offset;
328         if (n >= size)
329                 return res | size;
330
331         size -= n;
332         res |= n;
333         while (size > (++iov)->iov_len) {
334                 res |= (unsigned long)iov->iov_base | iov->iov_len;
335                 size -= iov->iov_len;
336         }
337         res |= (unsigned long)iov->iov_base | size;
338
339         return res;
340 }
341 #endif
342
343 /*
344  * Lustre could relax a bit for alignment, io count is not
345  * necessary page alignment.
346  */
347 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
348 {
349         size_t orig_size = i->count;
350         size_t count = orig_size & ~PAGE_MASK;
351         unsigned long res;
352
353         if (!count)
354                 return iov_iter_alignment_vfs(i);
355
356         if (orig_size > PAGE_SIZE) {
357                 iov_iter_truncate(i, orig_size - count);
358                 res = iov_iter_alignment_vfs(i);
359                 iov_iter_reexpand(i, orig_size);
360
361                 return res;
362         }
363
364         res = iov_iter_alignment_vfs(i);
365         /* start address is page aligned */
366         if ((res & ~PAGE_MASK) == orig_size)
367                 return PAGE_SIZE;
368
369         return res;
370 }
371
372 static int
373 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
374                    int rw, struct inode *inode, struct cl_sub_dio *sdio)
375 {
376         struct ll_dio_pages *pv = &sdio->csd_dio_pages;
377         struct cl_page    *page;
378         struct cl_2queue  *queue = &io->ci_queue;
379         struct cl_object  *obj = io->ci_obj;
380         struct cl_sync_io *anchor = &sdio->csd_sync;
381         loff_t offset   = pv->ldp_file_offset;
382         int io_pages    = 0;
383         int i;
384         ssize_t rc = 0;
385
386         ENTRY;
387
388         cl_2queue_init(queue);
389         for (i = 0; i < pv->ldp_count; i++) {
390                 LASSERT(!(offset & (PAGE_SIZE - 1)));
391                 page = cl_page_find(env, obj, cl_index(obj, offset),
392                                     pv->ldp_pages[i], CPT_TRANSIENT);
393                 if (IS_ERR(page)) {
394                         rc = PTR_ERR(page);
395                         break;
396                 }
397                 LASSERT(page->cp_type == CPT_TRANSIENT);
398                 rc = cl_page_own(env, io, page);
399                 if (rc) {
400                         cl_page_put(env, page);
401                         break;
402                 }
403
404                 page->cp_sync_io = anchor;
405                 if (inode && IS_ENCRYPTED(inode)) {
406                         /* In case of Direct IO on encrypted file, we need to
407                          * add a reference to the inode on the cl_page.
408                          * This info is required by llcrypt to proceed
409                          * to encryption/decryption.
410                          * This is safe because we know these pages are private
411                          * to the thread doing the Direct IO.
412                          */
413                         page->cp_inode = inode;
414                 }
415                 /* We keep the refcount from cl_page_find, so we don't need
416                  * another one here
417                  */
418                 cl_page_list_add(&queue->c2_qin, page, false);
419                 /*
420                  * Set page clip to tell transfer formation engine
421                  * that page has to be sent even if it is beyond KMS.
422                  */
423                 if (size < PAGE_SIZE)
424                         cl_page_clip(env, page, 0, size);
425                 ++io_pages;
426
427                 offset += PAGE_SIZE;
428                 size -= PAGE_SIZE;
429         }
430         if (rc == 0 && io_pages > 0) {
431                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
432
433                 atomic_add(io_pages, &anchor->csi_sync_nr);
434                 /*
435                  * Avoid out-of-order execution of adding inflight
436                  * modifications count and io submit.
437                  */
438                 smp_mb();
439                 rc = cl_io_submit_rw(env, io, iot, queue);
440                 if (rc == 0) {
441                         cl_page_list_splice(&queue->c2_qout, &sdio->csd_pages);
442                 } else {
443                         atomic_add(-queue->c2_qin.pl_nr,
444                                    &anchor->csi_sync_nr);
445                         cl_page_list_for_each(page, &queue->c2_qin)
446                                 page->cp_sync_io = NULL;
447                 }
448                 /* handle partially submitted reqs */
449                 if (queue->c2_qin.pl_nr > 0) {
450                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
451                                PFID(lu_object_fid(&obj->co_lu)),
452                                queue->c2_qin.pl_nr, rc);
453                         if (rc == 0)
454                                 rc = -EIO;
455                 }
456         }
457
458         cl_2queue_discard(env, io, queue);
459         cl_2queue_disown(env, queue);
460         cl_2queue_fini(env, queue);
461         RETURN(rc);
462 }
463
464 #ifdef KMALLOC_MAX_SIZE
465 #define MAX_MALLOC KMALLOC_MAX_SIZE
466 #else
467 #define MAX_MALLOC (128 * 1024)
468 #endif
469
470 /* This is the maximum size of a single O_DIRECT request, based on the
471  * kmalloc limit.  We need to fit all of the brw_page structs, each one
472  * representing PAGE_SIZE worth of user data, into a single buffer, and
473  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
474  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
475 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
476                       ~((size_t)DT_MAX_BRW_SIZE - 1))
477
478 static ssize_t
479 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
480 {
481         struct ll_cl_context *lcc;
482         const struct lu_env *env;
483         struct cl_io *io;
484         struct file *file = iocb->ki_filp;
485         struct inode *inode = file->f_mapping->host;
486         struct cl_dio_aio *ll_dio_aio;
487         struct cl_sub_dio *ldp_aio;
488         size_t count = iov_iter_count(iter);
489         ssize_t tot_bytes = 0, result = 0;
490         loff_t file_offset = iocb->ki_pos;
491         bool sync_submit = false;
492         struct vvp_io *vio;
493         ssize_t rc2;
494
495         /* Check EOF by ourselves */
496         if (rw == READ && file_offset >= i_size_read(inode))
497                 return 0;
498
499         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
500         if (file_offset & ~PAGE_MASK)
501                 RETURN(-EINVAL);
502
503         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
504                "offset=%lld=%llx, pages %zd (max %lu)\n",
505                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
506                file_offset, file_offset, count >> PAGE_SHIFT,
507                MAX_DIO_SIZE >> PAGE_SHIFT);
508
509         /* Check that all user buffers are aligned as well */
510         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
511                 RETURN(-EINVAL);
512
513         lcc = ll_cl_find(inode);
514         if (lcc == NULL)
515                 RETURN(-EIO);
516
517         env = lcc->lcc_env;
518         LASSERT(!IS_ERR(env));
519         vio = vvp_env_io(env);
520         io = lcc->lcc_io;
521         LASSERT(io != NULL);
522
523         ll_dio_aio = io->ci_dio_aio;
524         LASSERT(ll_dio_aio);
525         LASSERT(ll_dio_aio->cda_iocb == iocb);
526
527         /* We cannot do parallel submission of sub-I/Os - for AIO or regular
528          * DIO - unless lockless because it causes us to release the lock
529          * early.
530          *
531          * There are also several circumstances in which we must disable
532          * parallel DIO, so we check if it is enabled.
533          *
534          * The check for "is_sync_kiocb" excludes AIO, which does not need to
535          * be disabled in these situations.
536          */
537         if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio))
538                 sync_submit = true;
539
540         while (iov_iter_count(iter)) {
541                 struct ll_dio_pages *pvec;
542                 struct page **pages;
543
544                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
545                 if (rw == READ) {
546                         if (file_offset >= i_size_read(inode))
547                                 break;
548
549                         if (file_offset + count > i_size_read(inode))
550                                 count = i_size_read(inode) - file_offset;
551                 }
552
553                 /* if we are doing sync_submit, then we free this below,
554                  * otherwise it is freed on the final call to cl_sync_io_note
555                  * (either in this function or from a ptlrpcd daemon)
556                  */
557                 ldp_aio = cl_sub_dio_alloc(ll_dio_aio, sync_submit);
558                 if (!ldp_aio)
559                         GOTO(out, result = -ENOMEM);
560
561                 pvec = &ldp_aio->csd_dio_pages;
562
563                 result = ll_get_user_pages(rw, iter, &pages,
564                                            &pvec->ldp_count, count);
565                 if (unlikely(result <= 0)) {
566                         cl_sync_io_note(env, &ldp_aio->csd_sync, result);
567                         if (sync_submit) {
568                                 LASSERT(ldp_aio->csd_creator_free);
569                                 cl_sub_dio_free(ldp_aio);
570                         }
571                         GOTO(out, result);
572                 }
573
574                 count = result;
575                 pvec->ldp_file_offset = file_offset;
576                 pvec->ldp_pages = pages;
577
578                 result = ll_direct_rw_pages(env, io, count,
579                                             rw, inode, ldp_aio);
580                 /* We've submitted pages and can now remove the extra
581                  * reference for that
582                  */
583                 cl_sync_io_note(env, &ldp_aio->csd_sync, result);
584
585                 if (sync_submit) {
586                         rc2 = cl_sync_io_wait(env, &ldp_aio->csd_sync,
587                                              0);
588                         if (result == 0 && rc2)
589                                 result = rc2;
590                         LASSERT(ldp_aio->csd_creator_free);
591                         cl_sub_dio_free(ldp_aio);
592                 }
593                 if (unlikely(result < 0))
594                         GOTO(out, result);
595
596                 iov_iter_advance(iter, count);
597                 tot_bytes += count;
598                 file_offset += count;
599         }
600
601 out:
602         ll_dio_aio->cda_bytes += tot_bytes;
603
604         if (rw == WRITE)
605                 vio->u.readwrite.vui_written += tot_bytes;
606         else
607                 vio->u.readwrite.vui_read += tot_bytes;
608
609         /* AIO is not supported on pipes, so we cannot return EIOCBQEUED like
610          * we normally would for both DIO and AIO here
611          */
612         if (result == 0 && !iov_iter_is_pipe(iter))
613                 result = -EIOCBQUEUED;
614
615         return result;
616 }
617
618 #if defined(HAVE_DIO_ITER)
619 static ssize_t ll_direct_IO(
620 #ifndef HAVE_IOV_ITER_RW
621              int rw,
622 #endif
623              struct kiocb *iocb, struct iov_iter *iter
624 #ifndef HAVE_DIRECTIO_2ARGS
625              , loff_t file_offset
626 #endif
627              )
628 {
629         int nrw;
630
631 #ifndef HAVE_IOV_ITER_RW
632         nrw = rw;
633 #else
634         nrw = iov_iter_rw(iter);
635 #endif
636
637         return ll_direct_IO_impl(iocb, iter, nrw);
638 }
639
640 #else /* !defined(HAVE_DIO_ITER) */
641
642 static ssize_t
643 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
644              loff_t file_offset, unsigned long nr_segs)
645 {
646         struct iov_iter iter;
647
648         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
649         return ll_direct_IO_impl(iocb, &iter, rw);
650 }
651
652 #endif /* !defined(HAVE_DIO_ITER) */
653
654 /**
655  * Prepare partially written-to page for a write.
656  * @pg is owned when passed in and disowned when it returns non-zero result to
657  * the caller.
658  */
659 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
660                                    struct cl_page *pg, struct file *file)
661 {
662         struct cl_attr *attr   = vvp_env_thread_attr(env);
663         struct cl_object *obj  = io->ci_obj;
664         loff_t          offset = cl_offset(obj, cl_page_index(pg));
665         int             result;
666         ENTRY;
667
668         cl_object_attr_lock(obj);
669         result = cl_object_attr_get(env, obj, attr);
670         cl_object_attr_unlock(obj);
671         if (result) {
672                 cl_page_disown(env, io, pg);
673                 GOTO(out, result);
674         }
675
676         /*
677          * If are writing to a new page, no need to read old data.
678          * The extent locking will have updated the KMS, and for our
679          * purposes here we can treat it like i_size.
680          */
681         if (attr->cat_kms <= offset) {
682                 char *kaddr = kmap_atomic(pg->cp_vmpage);
683
684                 memset(kaddr, 0, PAGE_SIZE);
685                 kunmap_atomic(kaddr);
686                 GOTO(out, result = 0);
687         }
688
689         if (pg->cp_defer_uptodate) {
690                 pg->cp_ra_used = 1;
691                 GOTO(out, result = 0);
692         }
693
694         result = ll_io_read_page(env, io, pg, file);
695         if (result)
696                 GOTO(out, result);
697
698         /* ll_io_read_page() disowns the page */
699         result = cl_page_own(env, io, pg);
700         if (!result) {
701                 if (!PageUptodate(cl_page_vmpage(pg))) {
702                         cl_page_disown(env, io, pg);
703                         result = -EIO;
704                 }
705         } else if (result == -ENOENT) {
706                 /* page was truncated */
707                 result = -EAGAIN;
708         }
709         EXIT;
710
711 out:
712         return result;
713 }
714
715 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
716 {
717         /* Page must be present, up to date, dirty, and not in writeback. */
718         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
719             PageWriteback(vmpage) || vmpage->mapping != mapping)
720                 return -ENODATA;
721
722         return 0;
723 }
724
725 static int ll_write_begin(struct file *file, struct address_space *mapping,
726                           loff_t pos, unsigned int len,
727 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
728                           unsigned int flags,
729 #endif
730                           struct page **pagep, void **fsdata)
731 {
732         struct ll_cl_context *lcc = NULL;
733         const struct lu_env  *env = NULL;
734         struct cl_io   *io = NULL;
735         struct cl_page *page = NULL;
736         struct inode *inode = file_inode(file);
737         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
738         pgoff_t index = pos >> PAGE_SHIFT;
739         struct page *vmpage = NULL;
740         unsigned from = pos & (PAGE_SIZE - 1);
741         unsigned to = from + len;
742         int result = 0;
743         ENTRY;
744
745         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
746
747         lcc = ll_cl_find(inode);
748         if (lcc == NULL) {
749                 vmpage = grab_cache_page_nowait(mapping, index);
750                 result = ll_tiny_write_begin(vmpage, mapping);
751                 GOTO(out, result);
752         }
753
754         env = lcc->lcc_env;
755         io  = lcc->lcc_io;
756
757         if (file->f_flags & O_DIRECT) {
758                 /* direct IO failed because it couldn't clean up cached pages,
759                  * this causes a problem for mirror write because the cached
760                  * page may belong to another mirror, which will result in
761                  * problem submitting the I/O. */
762                 if (io->ci_designated_mirror > 0)
763                         GOTO(out, result = -EBUSY);
764
765                 /**
766                  * Direct write can fall back to buffered read, but DIO is done
767                  * with lockless i/o, and buffered requires LDLM locking, so
768                  * in this case we must restart without lockless.
769                  */
770                 if (!io->ci_dio_lock) {
771                         io->ci_dio_lock = 1;
772                         io->ci_need_restart = 1;
773                         GOTO(out, result = -ENOLCK);
774                 }
775         }
776 again:
777         /* To avoid deadlock, try to lock page first. */
778         vmpage = grab_cache_page_nowait(mapping, index);
779
780         if (unlikely(vmpage == NULL ||
781                      PageDirty(vmpage) || PageWriteback(vmpage))) {
782                 struct vvp_io *vio = vvp_env_io(env);
783                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
784
785                 /* if the page is already in dirty cache, we have to commit
786                  * the pages right now; otherwise, it may cause deadlock
787                  * because it holds page lock of a dirty page and request for
788                  * more grants. It's okay for the dirty page to be the first
789                  * one in commit page list, though. */
790                 if (vmpage != NULL && plist->pl_nr > 0) {
791                         unlock_page(vmpage);
792                         put_page(vmpage);
793                         vmpage = NULL;
794                 }
795
796                 /* commit pages and then wait for page lock */
797                 result = vvp_io_write_commit(env, io);
798                 if (result < 0)
799                         GOTO(out, result);
800
801                 if (vmpage == NULL) {
802                         vmpage = grab_cache_page_write_begin(mapping, index
803 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
804                                                              , flags
805 #endif
806                                                              );
807                         if (vmpage == NULL)
808                                 GOTO(out, result = -ENOMEM);
809                 }
810         }
811
812         /* page was truncated */
813         if (mapping != vmpage->mapping) {
814                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
815                 unlock_page(vmpage);
816                 put_page(vmpage);
817                 vmpage = NULL;
818                 goto again;
819         }
820
821         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
822         if (IS_ERR(page))
823                 GOTO(out, result = PTR_ERR(page));
824
825         lcc->lcc_page = page;
826         lu_ref_add(&page->cp_reference, "cl_io", io);
827
828         cl_page_assume(env, io, page);
829         if (!PageUptodate(vmpage)) {
830                 /*
831                  * We're completely overwriting an existing page,
832                  * so _don't_ set it up to date until commit_write
833                  */
834                 if (from == 0 && to == PAGE_SIZE) {
835                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
836                         POISON_PAGE(vmpage, 0x11);
837                 } else {
838                         /* TODO: can be optimized at OSC layer to check if it
839                          * is a lockless IO. In that case, it's not necessary
840                          * to read the data. */
841                         result = ll_prepare_partial_page(env, io, page, file);
842                         if (result) {
843                                 /* vmpage should have been unlocked */
844                                 put_page(vmpage);
845                                 vmpage = NULL;
846
847                                 if (result == -EAGAIN)
848                                         goto again;
849                                 GOTO(out, result);
850                         }
851                 }
852         }
853         EXIT;
854 out:
855         if (result < 0) {
856                 if (vmpage != NULL) {
857                         unlock_page(vmpage);
858                         put_page(vmpage);
859                 }
860                 /* On tiny_write failure, page and io are always null. */
861                 if (!IS_ERR_OR_NULL(page)) {
862                         lu_ref_del(&page->cp_reference, "cl_io", io);
863                         cl_page_put(env, page);
864                 }
865                 if (io)
866                         io->ci_result = result;
867         } else {
868                 *pagep = vmpage;
869                 *fsdata = lcc;
870         }
871         RETURN(result);
872 }
873
874 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
875                              loff_t pos, unsigned int len, unsigned int copied,
876                              struct page *vmpage)
877 {
878         struct cl_page *clpage = (struct cl_page *) vmpage->private;
879         loff_t kms = pos+copied;
880         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
881         __u16 refcheck;
882         struct lu_env *env = cl_env_get(&refcheck);
883         int rc = 0;
884
885         ENTRY;
886
887         if (IS_ERR(env)) {
888                 rc = PTR_ERR(env);
889                 goto out;
890         }
891
892         /* This page is dirty in cache, so it should have a cl_page pointer
893          * set in vmpage->private.
894          */
895         LASSERT(clpage != NULL);
896
897         if (copied == 0)
898                 goto out_env;
899
900         /* Update the underlying size information in the OSC/LOV objects this
901          * page is part of.
902          */
903         cl_page_touch(env, clpage, to);
904
905 out_env:
906         cl_env_put(env, &refcheck);
907
908 out:
909         /* Must return page unlocked. */
910         unlock_page(vmpage);
911
912         RETURN(rc);
913 }
914
915 static int ll_write_end(struct file *file, struct address_space *mapping,
916                         loff_t pos, unsigned len, unsigned copied,
917                         struct page *vmpage, void *fsdata)
918 {
919         struct ll_cl_context *lcc = fsdata;
920         const struct lu_env *env;
921         struct cl_io *io;
922         struct vvp_io *vio;
923         struct cl_page *page;
924         unsigned from = pos & (PAGE_SIZE - 1);
925         bool unplug = false;
926         int result = 0;
927         ENTRY;
928
929         put_page(vmpage);
930
931         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
932
933         if (lcc == NULL) {
934                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
935                                            vmpage);
936                 GOTO(out, result);
937         }
938
939         LASSERT(lcc != NULL);
940         env  = lcc->lcc_env;
941         page = lcc->lcc_page;
942         io   = lcc->lcc_io;
943         vio  = vvp_env_io(env);
944
945         LASSERT(cl_page_is_owned(page, io));
946         if (copied > 0) {
947                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
948
949                 lcc->lcc_page = NULL; /* page will be queued */
950
951                 /* Add it into write queue */
952                 cl_page_list_add(plist, page, true);
953                 if (plist->pl_nr == 1) /* first page */
954                         vio->u.readwrite.vui_from = from;
955                 else
956                         LASSERT(from == 0);
957                 vio->u.readwrite.vui_to = from + copied;
958
959                 /* To address the deadlock in balance_dirty_pages() where
960                  * this dirty page may be written back in the same thread. */
961                 if (PageDirty(vmpage))
962                         unplug = true;
963
964                 /* We may have one full RPC, commit it soon */
965                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
966                         unplug = true;
967
968                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
969                               "queued page: %d.\n", plist->pl_nr);
970         } else {
971                 cl_page_disown(env, io, page);
972
973                 lcc->lcc_page = NULL;
974                 lu_ref_del(&page->cp_reference, "cl_io", io);
975                 cl_page_put(env, page);
976
977                 /* page list is not contiguous now, commit it now */
978                 unplug = true;
979         }
980         if (unplug || io->u.ci_wr.wr_sync)
981                 result = vvp_io_write_commit(env, io);
982
983         if (result < 0)
984                 io->ci_result = result;
985
986
987 out:
988         RETURN(result >= 0 ? copied : result);
989 }
990
991 #ifdef CONFIG_MIGRATION
992 static int ll_migratepage(struct address_space *mapping,
993                           struct page *newpage, struct page *page,
994                           enum migrate_mode mode)
995 {
996         /* Always fail page migration until we have a proper implementation */
997         return -EIO;
998 }
999 #endif
1000
1001 const struct address_space_operations ll_aops = {
1002 #ifdef HAVE_DIRTY_FOLIO
1003         .dirty_folio            = filemap_dirty_folio,
1004 #else
1005         .set_page_dirty         = __set_page_dirty_nobuffers,
1006 #endif
1007 #ifdef HAVE_INVALIDATE_FOLIO
1008         .invalidate_folio       = ll_invalidate_folio,
1009 #else
1010         .invalidatepage         = ll_invalidatepage,
1011 #endif
1012 #ifdef HAVE_AOPS_READ_FOLIO
1013         .read_folio             = ll_read_folio,
1014 #else
1015         .readpage               = ll_readpage,
1016 #endif
1017 #ifdef HAVE_AOPS_RELEASE_FOLIO
1018         .release_folio          = ll_release_folio,
1019 #else
1020         .releasepage            = (void *)ll_releasepage,
1021 #endif
1022         .direct_IO              = ll_direct_IO,
1023         .writepage              = ll_writepage,
1024         .writepages             = ll_writepages,
1025         .write_begin            = ll_write_begin,
1026         .write_end              = ll_write_end,
1027 #ifdef CONFIG_MIGRATION
1028         .migratepage            = ll_migratepage,
1029 #endif
1030 };