4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/lustre/llite/rw26.c
33 * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
36 #include <linux/buffer_head.h>
37 #include <linux/errno.h>
39 #include <linux/kernel.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>
48 #define DEBUG_SUBSYSTEM S_LLITE
50 #include "llite_internal.h"
51 #include <lustre_compat.h>
53 #ifdef HAVE_INVALIDATE_FOLIO
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).
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.
64 static void ll_invalidate_folio(struct folio *folio, size_t offset, size_t len)
69 struct cl_object *obj;
71 LASSERT(!folio_test_writeback(folio));
72 LASSERT(folio_test_locked(folio));
74 if (!(offset == 0 && len == folio_size(folio)) &&
75 !folio_test_large(folio))
78 /* Drop the pages from the folio */
79 env = cl_env_percpu_get();
80 LASSERT(!IS_ERR(env));
82 inode = folio_inode(folio);
83 obj = ll_i2info(inode)->lli_clob;
85 int n, npgs = folio_nr_pages(folio);
87 for (n = 0; n < npgs; n++) {
88 struct page *vmpage = folio_page(folio, n);
90 LASSERT(PageLocked(vmpage));
91 LASSERT(!PageWriteback(vmpage));
93 page = cl_vmpage_page(vmpage, obj);
95 cl_page_delete(env, page);
96 cl_page_put(env, page);
100 LASSERT(!folio_get_private(folio));
102 cl_env_percpu_put(env);
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).
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.
116 static void ll_invalidatepage(struct page *vmpage,
117 #ifdef HAVE_INVALIDATE_RANGE
118 unsigned int offset, unsigned int length
126 struct cl_page *page;
127 struct cl_object *obj;
129 LASSERT(PageLocked(vmpage));
130 LASSERT(!PageWriteback(vmpage));
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
137 #ifdef HAVE_INVALIDATE_RANGE
138 if (offset == 0 && length == PAGE_SIZE) {
142 /* See the comment in ll_releasepage() */
143 env = cl_env_percpu_get();
144 LASSERT(!IS_ERR(env));
146 inode = vmpage->mapping->host;
147 obj = ll_i2info(inode)->lli_clob;
149 page = cl_vmpage_page(vmpage, obj);
151 cl_page_delete(env, page);
152 cl_page_put(env, page);
155 LASSERT(vmpage->private == 0);
157 cl_env_percpu_put(env);
160 if (CFS_FAIL_PRECHECK(OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE)) {
162 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE,
169 static bool do_release_page(struct page *vmpage, gfp_t wait)
171 struct address_space *mapping;
172 struct cl_object *obj;
173 struct cl_page *page;
179 LASSERT(PageLocked(vmpage));
180 if (PageWriteback(vmpage) || PageDirty(vmpage))
183 mapping = vmpage->mapping;
187 obj = ll_i2info(mapping->host)->lli_clob;
191 page = cl_vmpage_page(vmpage, obj);
195 env = cl_env_percpu_get();
196 LASSERT(!IS_ERR(env));
198 if (!cl_page_in_use(page)) {
200 cl_page_delete(env, page);
203 /* To use percpu env array, the call path can not be rescheduled;
204 * otherwise percpu array will be messed if ll_releaspage() called
205 * again on the same CPU.
207 * If this page holds the last refc of cl_object, the following
208 * call path may cause reschedule:
209 * cl_page_put -> cl_page_free -> cl_object_put ->
210 * lu_object_put -> lu_object_free -> lov_delete_raid0.
212 * However, the kernel can't get rid of this inode until all pages have
213 * been cleaned up. Now that we hold page lock here, it's pretty safe
214 * that we won't get into object delete path.
216 LASSERT(cl_object_refc(obj) > 1);
217 cl_page_put(env, page);
219 cl_env_percpu_put(env);
223 #ifdef HAVE_AOPS_RELEASE_FOLIO
224 static bool ll_release_folio(struct folio *folio, gfp_t wait)
226 struct page *vmpage = folio_page(folio, 0);
228 /* folio_nr_pages(folio) == 1 is fixed with grab_cache_page* */
229 BUG_ON(folio_nr_pages(folio) != 1);
231 return do_release_page(vmpage, wait);
233 #else /* !HAVE_AOPS_RELEASE_FOLIO */
234 #ifdef HAVE_RELEASEPAGE_WITH_INT
235 #define RELEASEPAGE_ARG_TYPE int
237 #define RELEASEPAGE_ARG_TYPE gfp_t
239 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
241 return do_release_page(vmpage, gfp_mask);
243 #endif /* HAVE_AOPS_RELEASE_FOLIO */
245 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
246 struct ll_dio_pages *pvec,
249 #if defined(HAVE_DIO_ITER)
253 result = iov_iter_get_pages_alloc2(iter, &pvec->ldp_pages, maxsize,
256 pvec->ldp_count = DIV_ROUND_UP(result + start, PAGE_SIZE);
271 addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
272 if (addr & ~PAGE_MASK)
275 size = min_t(size_t, maxsize, iter->iov->iov_len);
276 page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
277 OBD_ALLOC_PTR_ARRAY_LARGE(pvec->ldp_pages, page_count);
278 if (pvec->ldp_pages == NULL)
281 mmap_read_lock(current->mm);
282 result = get_user_pages(current, current->mm, addr, page_count,
283 rw == READ, 0, pvec->ldp_pages, NULL);
284 mmap_read_unlock(current->mm);
286 if (unlikely(result != page_count)) {
287 ll_release_user_pages(pvec->ldp_pages, page_count);
288 pvec->ldp_pages = NULL;
295 pvec->ldp_count = page_count;
301 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
302 #if defined(HAVE_DIO_ITER)
303 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
305 return iov_iter_alignment(i);
307 #else /* copied from alignment_iovec() */
308 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
310 const struct iovec *iov = i->iov;
312 size_t size = i->count;
318 res = (unsigned long)iov->iov_base + i->iov_offset;
319 n = iov->iov_len - i->iov_offset;
325 while (size > (++iov)->iov_len) {
326 res |= (unsigned long)iov->iov_base | iov->iov_len;
327 size -= iov->iov_len;
329 res |= (unsigned long)iov->iov_base | size;
336 * Lustre could relax a bit for alignment, io count is not
337 * necessary page alignment.
339 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
341 size_t orig_size = i->count;
342 size_t count = orig_size & ~PAGE_MASK;
346 return iov_iter_alignment_vfs(i);
348 if (orig_size > PAGE_SIZE) {
349 iov_iter_truncate(i, orig_size - count);
350 res = iov_iter_alignment_vfs(i);
351 iov_iter_reexpand(i, orig_size);
356 res = iov_iter_alignment_vfs(i);
357 /* start address is page aligned */
358 if ((res & ~PAGE_MASK) == orig_size)
365 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
366 int rw, struct inode *inode, struct cl_sub_dio *sdio)
368 struct ll_dio_pages *pv = &sdio->csd_dio_pages;
369 struct cl_page *page;
370 struct cl_2queue *queue = &io->ci_queue;
371 struct cl_object *obj = io->ci_obj;
372 struct cl_sync_io *anchor = &sdio->csd_sync;
373 loff_t offset = pv->ldp_file_offset;
380 cl_2queue_init(queue);
382 size_t from = offset & ~PAGE_MASK;
383 size_t to = min(from + size, PAGE_SIZE);
385 page = cl_page_find(env, obj, offset >> PAGE_SHIFT,
386 pv->ldp_pages[i], CPT_TRANSIENT);
392 LASSERT(page->cp_type == CPT_TRANSIENT);
393 rc = cl_page_own(env, io, page);
395 cl_page_put(env, page);
399 page->cp_sync_io = anchor;
400 if (inode && IS_ENCRYPTED(inode)) {
401 /* In case of Direct IO on encrypted file, we need to
402 * add a reference to the inode on the cl_page.
403 * This info is required by llcrypt to proceed
404 * to encryption/decryption.
405 * This is safe because we know these pages are private
406 * to the thread doing the Direct IO.
408 page->cp_inode = inode;
410 /* We keep the refcount from cl_page_find, so we don't need
413 cl_page_list_add(&queue->c2_qin, page, false);
415 * Call page clip for incomplete pages, to set range of bytes
416 * in the page and to tell transfer formation engine to send
417 * the page even if it is beyond KMS (ie, don't trim IO to KMS)
419 if (from != 0 || to != PAGE_SIZE)
420 cl_page_clip(env, page, from, to);
427 /* on success, we should hit every page in the pvec and have no bytes
430 LASSERT(ergo(rc == 0, i == pv->ldp_count));
431 LASSERT(ergo(rc == 0, size == 0));
433 if (rc == 0 && io_pages > 0) {
434 int iot = rw == READ ? CRT_READ : CRT_WRITE;
436 atomic_add(io_pages, &anchor->csi_sync_nr);
438 * Avoid out-of-order execution of adding inflight
439 * modifications count and io submit.
442 rc = cl_io_submit_rw(env, io, iot, queue);
444 cl_page_list_splice(&queue->c2_qout, &sdio->csd_pages);
446 atomic_add(-queue->c2_qin.pl_nr,
447 &anchor->csi_sync_nr);
448 cl_page_list_for_each(page, &queue->c2_qin)
449 page->cp_sync_io = NULL;
451 /* handle partially submitted reqs */
452 if (queue->c2_qin.pl_nr > 0) {
453 CERROR(DFID " failed to submit %d dio pages: %zd\n",
454 PFID(lu_object_fid(&obj->co_lu)),
455 queue->c2_qin.pl_nr, rc);
461 cl_2queue_discard(env, io, queue);
462 cl_2queue_disown(env, queue);
463 cl_2queue_fini(env, queue);
467 #ifdef KMALLOC_MAX_SIZE
468 #define MAX_MALLOC KMALLOC_MAX_SIZE
470 #define MAX_MALLOC (128 * 1024)
473 /* This is the maximum size of a single O_DIRECT request, based on the
474 * kmalloc limit. We need to fit all of the brw_page structs, each one
475 * representing PAGE_SIZE worth of user data, into a single buffer, and
476 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
477 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
478 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
479 ~((size_t)DT_MAX_BRW_SIZE - 1))
482 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
484 struct ll_cl_context *lcc;
485 const struct lu_env *env;
487 struct file *file = iocb->ki_filp;
488 struct inode *inode = file->f_mapping->host;
489 struct cl_dio_aio *ll_dio_aio;
490 struct cl_sub_dio *sdio;
491 size_t count = iov_iter_count(iter);
492 ssize_t tot_bytes = 0, result = 0;
493 loff_t file_offset = iocb->ki_pos;
494 bool sync_submit = false;
495 bool unaligned = false;
501 if (file_offset & ~PAGE_MASK)
504 if (count & ~PAGE_MASK)
507 /* Check that all user buffers are aligned as well */
508 if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
512 "VFS Op:inode="DFID"(%p), size=%zd (max %lu), offset=%lld=%llx, pages %zd (max %lu)%s\n",
513 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
514 file_offset, file_offset,
515 (count >> PAGE_SHIFT) + !!(count & ~PAGE_MASK),
516 MAX_DIO_SIZE >> PAGE_SHIFT, unaligned ? ", unaligned" : "");
518 /* Check EOF by ourselves */
519 if (rw == READ && file_offset >= i_size_read(inode))
522 /* unaligned DIO support can be turned off, so is it on? */
523 if (unaligned && !ll_sbi_has_unaligned_dio(ll_i2sbi(inode)))
526 /* the requirement to not return EIOCBQUEUED for pipes (see bottom of
527 * this function) plays havoc with the unaligned I/O lifecycle, so
528 * don't allow unaligned I/O on pipes
530 if (unaligned && iov_iter_is_pipe(iter))
533 lcc = ll_cl_find(inode);
538 LASSERT(!IS_ERR(env));
539 vio = vvp_env_io(env);
543 /* this means we encountered an old server which can't safely support
544 * unaligned DIO, so we have to disable it
546 if (unaligned && !cl_io_top(io)->ci_allow_unaligned_dio)
549 /* if one part of an I/O is unaligned, just handle all of it that way -
550 * otherwise we create significant complexities with managing the iovec
551 * in different ways, etc, all for very marginal benefits
554 io->ci_unaligned_dio = true;
555 if (io->ci_unaligned_dio)
558 ll_dio_aio = io->ci_dio_aio;
560 LASSERT(ll_dio_aio->cda_iocb == iocb);
562 /* We cannot do parallel submission of sub-I/Os - for AIO or regular
563 * DIO - unless lockless because it causes us to release the lock
566 * There are also several circumstances in which we must disable
567 * parallel DIO, so we check if it is enabled.
569 * The check for "is_sync_kiocb" excludes AIO, which does not need to
570 * be disabled in these situations.
572 if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio))
575 while (iov_iter_count(iter)) {
576 struct ll_dio_pages *pvec;
578 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
580 if (file_offset >= i_size_read(inode))
583 if (file_offset + count > i_size_read(inode))
584 count = i_size_read(inode) - file_offset;
587 /* if we are doing sync_submit, then we free this below,
588 * otherwise it is freed on the final call to cl_sync_io_note
589 * (either in this function or from a ptlrpcd daemon)
591 sdio = cl_sub_dio_alloc(ll_dio_aio, iter, rw == WRITE,
592 unaligned, sync_submit);
594 GOTO(out, result = -ENOMEM);
596 pvec = &sdio->csd_dio_pages;
597 pvec->ldp_file_offset = file_offset;
600 result = ll_get_user_pages(rw, iter, pvec, count);
601 /* ll_get_user_pages returns bytes in the IO or error*/
604 /* same calculation used in ll_get_user_pages */
605 count = min_t(size_t, count, iter->iov->iov_len);
606 result = ll_allocate_dio_buffer(pvec, count);
607 /* allocate_dio_buffer returns number of pages or
608 * error, so do not set count = result
612 /* now we have the actual count, so store it in the sdio */
613 sdio->csd_bytes = count;
615 if (unlikely(result <= 0)) {
616 cl_sync_io_note(env, &sdio->csd_sync, result);
618 LASSERT(sdio->csd_creator_free);
619 cl_sub_dio_free(sdio);
624 if (unaligned && rw == WRITE) {
625 result = ll_dio_user_copy(sdio, iter);
626 if (unlikely(result <= 0)) {
627 cl_sync_io_note(env, &sdio->csd_sync, result);
629 LASSERT(sdio->csd_creator_free);
630 cl_sub_dio_free(sdio);
636 result = ll_direct_rw_pages(env, io, count, rw, inode, sdio);
637 /* if the i/o was unsuccessful, we zero the number of bytes to
638 * copy back. Note that partial I/O completion isn't possible
639 * here - I/O either completes or fails. So there's no need to
640 * handle short I/O here by changing 'count' with the result
641 * from ll_direct_rw_pages.
643 * This must be done before we release the reference
644 * immediately below, because releasing the reference allows
645 * i/o completion (and copyback to userspace, if unaligned) to
650 /* We've submitted pages and can now remove the extra
653 cl_sync_io_note(env, &sdio->csd_sync, result);
656 rc2 = cl_sync_io_wait(env, &sdio->csd_sync,
658 if (result == 0 && rc2)
660 LASSERT(sdio->csd_creator_free);
661 cl_sub_dio_free(sdio);
663 if (unlikely(result < 0))
666 iov_iter_advance(iter, count);
669 file_offset += count;
671 "result %zd tot_bytes %zd count %zd file_offset %lld\n",
672 result, tot_bytes, count, file_offset);
677 vio->u.readwrite.vui_written += tot_bytes;
679 vio->u.readwrite.vui_read += tot_bytes;
681 /* AIO is not supported on pipes, so we cannot return EIOCBQEUED like
682 * we normally would for both DIO and AIO here
684 if (result == 0 && !iov_iter_is_pipe(iter))
685 result = -EIOCBQUEUED;
691 static ssize_t ll_direct_IO(
692 #ifndef HAVE_IOV_ITER_RW
695 struct kiocb *iocb, struct iov_iter *iter
696 #ifndef HAVE_DIRECTIO_2ARGS
703 #ifndef HAVE_IOV_ITER_RW
706 nrw = iov_iter_rw(iter);
709 return ll_direct_IO_impl(iocb, iter, nrw);
712 #else /* !defined(HAVE_DIO_ITER) */
715 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
716 loff_t file_offset, unsigned long nr_segs)
718 struct iov_iter iter;
720 iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
721 return ll_direct_IO_impl(iocb, &iter, rw);
724 #endif /* !defined(HAVE_DIO_ITER) */
727 * Prepare partially written-to page for a write.
728 * @pg is owned when passed in and disowned when it returns non-zero result to
731 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
732 struct cl_page *pg, struct file *file)
734 struct cl_attr *attr = vvp_env_thread_attr(env);
735 struct cl_object *obj = io->ci_obj;
736 loff_t offset = cl_page_index(pg) << PAGE_SHIFT;
740 cl_object_attr_lock(obj);
741 result = cl_object_attr_get(env, obj, attr);
742 cl_object_attr_unlock(obj);
744 cl_page_disown(env, io, pg);
749 * If are writing to a new page, no need to read old data.
750 * The extent locking will have updated the KMS, and for our
751 * purposes here we can treat it like i_size.
753 if (attr->cat_kms <= offset) {
754 char *kaddr = kmap_atomic(pg->cp_vmpage);
756 memset(kaddr, 0, PAGE_SIZE);
757 kunmap_atomic(kaddr);
758 GOTO(out, result = 0);
761 if (pg->cp_defer_uptodate) {
763 GOTO(out, result = 0);
766 result = ll_io_read_page(env, io, pg, file);
770 /* ll_io_read_page() disowns the page */
771 result = cl_page_own(env, io, pg);
773 if (!PageUptodate(cl_page_vmpage(pg))) {
774 cl_page_disown(env, io, pg);
777 } else if (result == -ENOENT) {
778 /* page was truncated */
787 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
789 /* Page must be present, up to date, dirty, and not in writeback. */
790 if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
791 PageWriteback(vmpage) || vmpage->mapping != mapping)
797 static int ll_write_begin(struct file *file, struct address_space *mapping,
798 loff_t pos, unsigned int len,
799 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
802 struct page **pagep, void **fsdata)
804 struct ll_cl_context *lcc = NULL;
805 const struct lu_env *env = NULL;
806 struct cl_io *io = NULL;
807 struct cl_page *page = NULL;
808 struct inode *inode = file_inode(file);
809 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
810 pgoff_t index = pos >> PAGE_SHIFT;
811 struct page *vmpage = NULL;
812 unsigned from = pos & (PAGE_SIZE - 1);
813 unsigned to = from + len;
817 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
819 lcc = ll_cl_find(inode);
821 vmpage = grab_cache_page_nowait(mapping, index);
822 result = ll_tiny_write_begin(vmpage, mapping);
829 if (file->f_flags & O_DIRECT) {
830 /* direct IO failed because it couldn't clean up cached pages,
831 * this causes a problem for mirror write because the cached
832 * page may belong to another mirror, which will result in
833 * problem submitting the I/O. */
834 if (io->ci_designated_mirror > 0)
835 GOTO(out, result = -EBUSY);
838 * Direct write can fall back to buffered read, but DIO is done
839 * with lockless i/o, and buffered requires LDLM locking, so
840 * in this case we must restart without lockless.
842 if (!io->ci_dio_lock) {
844 io->ci_need_restart = 1;
845 GOTO(out, result = -ENOLCK);
849 /* To avoid deadlock, try to lock page first. */
850 vmpage = grab_cache_page_nowait(mapping, index);
852 if (unlikely(vmpage == NULL ||
853 PageDirty(vmpage) || PageWriteback(vmpage))) {
854 struct vvp_io *vio = vvp_env_io(env);
855 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
857 /* if the page is already in dirty cache, we have to commit
858 * the pages right now; otherwise, it may cause deadlock
859 * because it holds page lock of a dirty page and request for
860 * more grants. It's okay for the dirty page to be the first
861 * one in commit page list, though. */
862 if (vmpage != NULL && plist->pl_nr > 0) {
868 /* commit pages and then wait for page lock */
869 result = vvp_io_write_commit(env, io);
873 if (vmpage == NULL) {
874 vmpage = grab_cache_page_write_begin(mapping, index
875 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
880 GOTO(out, result = -ENOMEM);
884 /* page was truncated */
885 if (mapping != vmpage->mapping) {
886 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
893 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
895 GOTO(out, result = PTR_ERR(page));
897 lcc->lcc_page = page;
898 lu_ref_add(&page->cp_reference, "cl_io", io);
900 cl_page_assume(env, io, page);
901 if (!PageUptodate(vmpage)) {
903 * We're completely overwriting an existing page,
904 * so _don't_ set it up to date until commit_write
906 if (from == 0 && to == PAGE_SIZE) {
907 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
908 POISON_PAGE(vmpage, 0x11);
910 /* TODO: can be optimized at OSC layer to check if it
911 * is a lockless IO. In that case, it's not necessary
912 * to read the data. */
913 result = ll_prepare_partial_page(env, io, page, file);
915 /* vmpage should have been unlocked */
919 if (result == -EAGAIN)
928 if (vmpage != NULL) {
932 /* On tiny_write failure, page and io are always null. */
933 if (!IS_ERR_OR_NULL(page)) {
934 lu_ref_del(&page->cp_reference, "cl_io", io);
935 cl_page_put(env, page);
938 io->ci_result = result;
946 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
947 loff_t pos, unsigned int len, unsigned int copied,
950 struct cl_page *clpage = (struct cl_page *) vmpage->private;
951 loff_t kms = pos+copied;
952 loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
954 struct lu_env *env = cl_env_get(&refcheck);
964 /* This page is dirty in cache, so it should have a cl_page pointer
965 * set in vmpage->private.
967 LASSERT(clpage != NULL);
972 /* Update the underlying size information in the OSC/LOV objects this
975 cl_page_touch(env, clpage, to);
978 cl_env_put(env, &refcheck);
981 /* Must return page unlocked. */
987 static int ll_write_end(struct file *file, struct address_space *mapping,
988 loff_t pos, unsigned len, unsigned copied,
989 struct page *vmpage, void *fsdata)
991 struct ll_cl_context *lcc = fsdata;
992 const struct lu_env *env;
995 struct cl_page *page;
996 unsigned from = pos & (PAGE_SIZE - 1);
1003 CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
1006 result = ll_tiny_write_end(file, mapping, pos, len, copied,
1011 LASSERT(lcc != NULL);
1013 page = lcc->lcc_page;
1015 vio = vvp_env_io(env);
1017 LASSERT(cl_page_is_owned(page, io));
1019 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
1021 lcc->lcc_page = NULL; /* page will be queued */
1023 /* Add it into write queue */
1024 cl_page_list_add(plist, page, true);
1025 if (plist->pl_nr == 1) /* first page */
1026 vio->u.readwrite.vui_from = from;
1029 vio->u.readwrite.vui_to = from + copied;
1031 /* To address the deadlock in balance_dirty_pages() where
1032 * this dirty page may be written back in the same thread. */
1033 if (PageDirty(vmpage))
1036 /* We may have one full RPC, commit it soon */
1037 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
1040 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
1041 "queued page: %d.\n", plist->pl_nr);
1043 cl_page_disown(env, io, page);
1045 lcc->lcc_page = NULL;
1046 lu_ref_del(&page->cp_reference, "cl_io", io);
1047 cl_page_put(env, page);
1049 /* page list is not contiguous now, commit it now */
1052 if (unplug || io->u.ci_wr.wr_sync)
1053 result = vvp_io_write_commit(env, io);
1056 io->ci_result = result;
1060 RETURN(result >= 0 ? copied : result);
1063 #ifdef CONFIG_MIGRATION
1064 static int ll_migrate_folio(struct address_space *mapping,
1065 struct folio_migr *newpage, struct folio_migr *page,
1066 enum migrate_mode mode)
1068 /* Always fail page migration until we have a proper implementation */
1073 const struct address_space_operations ll_aops = {
1074 #ifdef HAVE_DIRTY_FOLIO
1075 .dirty_folio = filemap_dirty_folio,
1077 .set_page_dirty = __set_page_dirty_nobuffers,
1079 #ifdef HAVE_INVALIDATE_FOLIO
1080 .invalidate_folio = ll_invalidate_folio,
1082 .invalidatepage = ll_invalidatepage,
1084 #ifdef HAVE_AOPS_READ_FOLIO
1085 .read_folio = ll_read_folio,
1087 .readpage = ll_readpage,
1089 #ifdef HAVE_AOPS_RELEASE_FOLIO
1090 .release_folio = ll_release_folio,
1092 .releasepage = (void *)ll_releasepage,
1094 .direct_IO = ll_direct_IO,
1095 .writepage = ll_writepage,
1096 .writepages = ll_writepages,
1097 .write_begin = ll_write_begin,
1098 .write_end = ll_write_end,
1099 #ifdef CONFIG_MIGRATION
1100 .migrate_folio = ll_migrate_folio,