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_sync_io *anchor = &sdio->csd_sync;
370 struct cl_2queue *queue = &io->ci_queue;
371 struct cl_object *obj = io->ci_obj;
372 struct cl_page *page;
373 int iot = rw == READ ? CRT_READ : CRT_WRITE;
374 loff_t offset = pv->ldp_file_offset;
381 cl_2queue_init(queue);
383 size_t from = offset & ~PAGE_MASK;
384 size_t to = min(from + size, PAGE_SIZE);
386 page = cl_page_find(env, obj, offset >> PAGE_SHIFT,
387 pv->ldp_pages[i], CPT_TRANSIENT);
389 GOTO(out, rc = PTR_ERR(page));
391 LASSERT(page->cp_type == CPT_TRANSIENT);
392 rc = cl_page_own(env, io, page);
394 cl_page_put(env, page);
398 page->cp_sync_io = anchor;
399 if (inode && IS_ENCRYPTED(inode)) {
400 /* In case of Direct IO on encrypted file, we need to
401 * add a reference to the inode on the cl_page.
402 * This info is required by llcrypt to proceed
403 * to encryption/decryption.
404 * This is safe because we know these pages are private
405 * to the thread doing the Direct IO.
407 page->cp_inode = inode;
409 /* We keep the refcount from cl_page_find, so we don't need
412 cl_page_list_add(&queue->c2_qin, page, false);
414 * Call page clip for incomplete pages, to set range of bytes
415 * in the page and to tell transfer formation engine to send
416 * the page even if it is beyond KMS (ie, don't trim IO to KMS)
418 if (from != 0 || to != PAGE_SIZE)
419 cl_page_clip(env, page, from, to);
426 /* on success, we should hit every page in the pvec and have no bytes
429 LASSERT(i == pv->ldp_count);
432 atomic_add(io_pages, &anchor->csi_sync_nr);
434 * Avoid out-of-order execution of adding inflight
435 * modifications count and io submit.
438 rc = cl_io_submit_rw(env, io, iot, queue);
440 cl_page_list_splice(&queue->c2_qout, &sdio->csd_pages);
442 atomic_add(-queue->c2_qin.pl_nr,
443 &anchor->csi_sync_nr);
444 cl_page_list_for_each(page, &queue->c2_qin)
445 page->cp_sync_io = NULL;
447 /* handle partially submitted reqs */
448 if (queue->c2_qin.pl_nr > 0) {
449 CERROR(DFID " failed to submit %d dio pages: %zd\n",
450 PFID(lu_object_fid(&obj->co_lu)),
451 queue->c2_qin.pl_nr, rc);
457 cl_2queue_discard(env, io, queue);
458 cl_2queue_disown(env, queue);
459 cl_2queue_fini(env, queue);
463 #ifdef KMALLOC_MAX_SIZE
464 #define MAX_MALLOC KMALLOC_MAX_SIZE
466 #define MAX_MALLOC (128 * 1024)
469 /* This is the maximum size of a single O_DIRECT request, based on the
470 * kmalloc limit. We need to fit all of the brw_page structs, each one
471 * representing PAGE_SIZE worth of user data, into a single buffer, and
472 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
473 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
474 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
475 ~((size_t)DT_MAX_BRW_SIZE - 1))
478 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
480 struct ll_cl_context *lcc;
481 const struct lu_env *env;
483 struct file *file = iocb->ki_filp;
484 struct inode *inode = file->f_mapping->host;
485 struct cl_dio_aio *ll_dio_aio;
486 struct cl_sub_dio *sdio;
487 size_t count = iov_iter_count(iter);
488 ssize_t tot_bytes = 0, result = 0;
489 loff_t file_offset = iocb->ki_pos;
490 bool sync_submit = false;
491 bool unaligned = false;
497 if (file_offset & ~PAGE_MASK)
500 if (count & ~PAGE_MASK)
503 /* Check that all user buffers are aligned as well */
504 if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
507 lcc = ll_cl_find(inode);
512 LASSERT(!IS_ERR(env));
513 vio = vvp_env_io(env);
518 "VFS Op:inode="DFID"(%p), size=%zd (max %lu), offset=%lld=%llx, pages %zd (max %lu)%s%s%s%s\n",
519 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
520 file_offset, file_offset,
521 (count >> PAGE_SHIFT) + !!(count & ~PAGE_MASK),
522 MAX_DIO_SIZE >> PAGE_SHIFT,
523 io->ci_dio_lock ? ", locked" : ", lockless",
524 io->ci_parallel_dio ? ", parallel" : "",
525 unaligned ? ", unaligned" : "",
526 io->ci_hybrid_switched ? ", hybrid" : "");
528 /* Check EOF by ourselves */
529 if (rw == READ && file_offset >= i_size_read(inode))
532 /* unaligned DIO support can be turned off, so is it on? */
533 if (unaligned && !ll_sbi_has_unaligned_dio(ll_i2sbi(inode)))
536 /* the requirement to not return EIOCBQUEUED for pipes (see bottom of
537 * this function) plays havoc with the unaligned I/O lifecycle, so
538 * don't allow unaligned I/O on pipes
540 if (unaligned && iov_iter_is_pipe(iter))
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(iter)->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;
807 struct cl_io *io = NULL;
808 struct cl_page *page = NULL;
809 struct inode *inode = file_inode(file);
810 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
811 pgoff_t index = pos >> PAGE_SHIFT;
812 struct page *vmpage = NULL;
813 unsigned from = pos & (PAGE_SIZE - 1);
814 unsigned to = from + len;
819 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
821 lcc = ll_cl_find(inode);
823 /* do not allocate a page, only find & lock */
824 vmpage = find_lock_page(mapping, index);
825 result = ll_tiny_write_begin(vmpage, mapping);
831 vio = vvp_env_io(env);
833 iocb_flags = iocb_ki_flags_get(file, vio->vui_iocb);
834 if (iocb_ki_flags_check(iocb_flags, DIRECT)) {
835 /* direct IO failed because it couldn't clean up cached pages,
836 * this causes a problem for mirror write because the cached
837 * page may belong to another mirror, which will result in
838 * problem submitting the I/O. */
839 if (io->ci_designated_mirror > 0)
840 GOTO(out, result = -EBUSY);
843 * Direct write can fall back to buffered read, but DIO is done
844 * with lockless i/o, and buffered requires LDLM locking, so
845 * in this case we must restart without lockless.
847 if (!io->ci_dio_lock) {
849 io->ci_need_restart = 1;
850 GOTO(out, result = -ENOLCK);
854 /* To avoid deadlock, try to lock page first. */
855 vmpage = grab_cache_page_nowait(mapping, index);
857 if (unlikely(vmpage == NULL ||
858 PageDirty(vmpage) || PageWriteback(vmpage))) {
859 struct vvp_io *vio = vvp_env_io(env);
860 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
862 /* if the page is already in dirty cache, we have to commit
863 * the pages right now; otherwise, it may cause deadlock
864 * because it holds page lock of a dirty page and request for
865 * more grants. It's okay for the dirty page to be the first
866 * one in commit page list, though. */
867 if (vmpage != NULL && plist->pl_nr > 0) {
873 /* commit pages and then wait for page lock */
874 result = vvp_io_write_commit(env, io);
878 if (vmpage == NULL) {
879 vmpage = grab_cache_page_write_begin(mapping, index
880 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
885 GOTO(out, result = -ENOMEM);
889 /* page was truncated */
890 if (mapping != vmpage->mapping) {
891 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
898 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
900 GOTO(out, result = PTR_ERR(page));
902 lcc->lcc_page = page;
903 lu_ref_add(&page->cp_reference, "cl_io", io);
905 cl_page_assume(env, io, page);
906 if (!PageUptodate(vmpage)) {
908 * We're completely overwriting an existing page,
909 * so _don't_ set it up to date until commit_write
911 if (from == 0 && to == PAGE_SIZE) {
912 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
913 POISON_PAGE(vmpage, 0x11);
915 /* TODO: can be optimized at OSC layer to check if it
916 * is a lockless IO. In that case, it's not necessary
917 * to read the data. */
918 result = ll_prepare_partial_page(env, io, page, file);
920 /* vmpage should have been unlocked */
924 if (result == -EAGAIN)
933 if (vmpage != NULL) {
937 /* On tiny_write failure, page and io are always null. */
938 if (!IS_ERR_OR_NULL(page)) {
939 lu_ref_del(&page->cp_reference, "cl_io", io);
940 cl_page_put(env, page);
943 io->ci_result = result;
951 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
952 loff_t pos, unsigned int len, unsigned int copied,
955 struct cl_page *clpage = (struct cl_page *) vmpage->private;
956 loff_t kms = pos+copied;
957 loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
959 struct lu_env *env = cl_env_get(&refcheck);
969 /* This page is dirty in cache, so it should have a cl_page pointer
970 * set in vmpage->private.
972 LASSERT(clpage != NULL);
977 /* Update the underlying size information in the OSC/LOV objects this
980 cl_page_touch(env, clpage, to);
983 cl_env_put(env, &refcheck);
986 /* Must return page unlocked. */
992 static int ll_write_end(struct file *file, struct address_space *mapping,
993 loff_t pos, unsigned len, unsigned copied,
994 struct page *vmpage, void *fsdata)
996 struct ll_cl_context *lcc = fsdata;
997 const struct lu_env *env;
1000 struct cl_page *page;
1001 unsigned from = pos & (PAGE_SIZE - 1);
1002 bool unplug = false;
1008 CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
1011 result = ll_tiny_write_end(file, mapping, pos, len, copied,
1016 LASSERT(lcc != NULL);
1018 page = lcc->lcc_page;
1020 vio = vvp_env_io(env);
1022 LASSERT(cl_page_is_owned(page, io));
1024 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
1026 lcc->lcc_page = NULL; /* page will be queued */
1028 /* Add it into write queue */
1029 cl_page_list_add(plist, page, true);
1030 if (plist->pl_nr == 1) /* first page */
1031 vio->u.readwrite.vui_from = from;
1034 vio->u.readwrite.vui_to = from + copied;
1036 /* To address the deadlock in balance_dirty_pages() where
1037 * this dirty page may be written back in the same thread. */
1038 if (PageDirty(vmpage))
1041 /* We may have one full RPC, commit it soon */
1042 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
1045 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
1046 "queued page: %d.\n", plist->pl_nr);
1048 cl_page_disown(env, io, page);
1050 lcc->lcc_page = NULL;
1051 lu_ref_del(&page->cp_reference, "cl_io", io);
1052 cl_page_put(env, page);
1054 /* page list is not contiguous now, commit it now */
1057 if (unplug || io->u.ci_wr.wr_sync)
1058 result = vvp_io_write_commit(env, io);
1061 io->ci_result = result;
1065 RETURN(result >= 0 ? copied : result);
1068 #ifdef CONFIG_MIGRATION
1069 static int ll_migrate_folio(struct address_space *mapping,
1070 struct folio_migr *newpage, struct folio_migr *page,
1071 enum migrate_mode mode)
1073 /* Always fail page migration until we have a proper implementation */
1078 const struct address_space_operations ll_aops = {
1079 #ifdef HAVE_DIRTY_FOLIO
1080 .dirty_folio = filemap_dirty_folio,
1082 .set_page_dirty = __set_page_dirty_nobuffers,
1084 #ifdef HAVE_INVALIDATE_FOLIO
1085 .invalidate_folio = ll_invalidate_folio,
1087 .invalidatepage = ll_invalidatepage,
1089 #ifdef HAVE_AOPS_READ_FOLIO
1090 .read_folio = ll_read_folio,
1092 .readpage = ll_readpage,
1094 #ifdef HAVE_AOPS_RELEASE_FOLIO
1095 .release_folio = ll_release_folio,
1097 .releasepage = (void *)ll_releasepage,
1099 .direct_IO = ll_direct_IO,
1100 .writepage = ll_writepage,
1101 .writepages = ll_writepages,
1102 .write_begin = ll_write_begin,
1103 .write_end = ll_write_end,
1104 #ifdef CONFIG_MIGRATION
1105 .migrate_folio = ll_migrate_folio,