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>
54 * Implements Linux VM address_space::invalidatepage() method. This method is
55 * called when the page is truncate from a file, either as a result of
56 * explicit truncate, or when inode is removed from memory (as a result of
57 * final iput(), umount, or memory pressure induced icache shrinking).
59 * [0, offset] bytes of the page remain valid (this is for a case of not-page
60 * aligned truncate). Lustre leaves partially truncated page in the cache,
61 * relying on struct inode::i_size to limit further accesses.
63 static void ll_invalidatepage(struct page *vmpage,
64 #ifdef HAVE_INVALIDATE_RANGE
65 unsigned int offset, unsigned int length
74 struct cl_object *obj;
76 LASSERT(PageLocked(vmpage));
77 LASSERT(!PageWriteback(vmpage));
80 * It is safe to not check anything in invalidatepage/releasepage
81 * below because they are run with page locked and all our io is
82 * happening with locked page too
84 #ifdef HAVE_INVALIDATE_RANGE
85 if (offset == 0 && length == PAGE_SIZE) {
89 /* See the comment in ll_releasepage() */
90 env = cl_env_percpu_get();
91 LASSERT(!IS_ERR(env));
93 inode = vmpage->mapping->host;
94 obj = ll_i2info(inode)->lli_clob;
96 page = cl_vmpage_page(vmpage, obj);
98 cl_page_delete(env, page);
99 cl_page_put(env, page);
102 LASSERT(vmpage->private == 0);
104 cl_env_percpu_put(env);
108 #ifdef HAVE_RELEASEPAGE_WITH_INT
109 #define RELEASEPAGE_ARG_TYPE int
111 #define RELEASEPAGE_ARG_TYPE gfp_t
113 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
116 struct cl_object *obj;
117 struct cl_page *page;
118 struct address_space *mapping;
121 LASSERT(PageLocked(vmpage));
122 if (PageWriteback(vmpage) || PageDirty(vmpage))
125 mapping = vmpage->mapping;
129 obj = ll_i2info(mapping->host)->lli_clob;
133 page = cl_vmpage_page(vmpage, obj);
137 env = cl_env_percpu_get();
138 LASSERT(!IS_ERR(env));
140 if (!cl_page_in_use(page)) {
142 cl_page_delete(env, page);
145 /* To use percpu env array, the call path can not be rescheduled;
146 * otherwise percpu array will be messed if ll_releaspage() called
147 * again on the same CPU.
149 * If this page holds the last refc of cl_object, the following
150 * call path may cause reschedule:
151 * cl_page_put -> cl_page_free -> cl_object_put ->
152 * lu_object_put -> lu_object_free -> lov_delete_raid0.
154 * However, the kernel can't get rid of this inode until all pages have
155 * been cleaned up. Now that we hold page lock here, it's pretty safe
156 * that we won't get into object delete path.
158 LASSERT(cl_object_refc(obj) > 1);
159 cl_page_put(env, page);
161 cl_env_percpu_put(env);
165 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW) || \
166 defined(HAVE_DIRECTIO_2ARGS)
167 #define HAVE_DIO_ITER 1
171 * ll_free_user_pages - tear down page struct array
172 * @pages: array of page struct pointers underlying target buffer
174 static void ll_free_user_pages(struct page **pages, int npages)
178 for (i = 0; i < npages; i++) {
184 #if defined(HAVE_DIO_ITER)
187 OBD_FREE_PTR_ARRAY_LARGE(pages, npages);
191 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
192 struct page ***pages, ssize_t *npages,
195 #if defined(HAVE_DIO_ITER)
200 * iov_iter_get_pages_alloc() is introduced in 3.16 similar
203 result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
205 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
220 addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
221 if (addr & ~PAGE_MASK)
224 size = min_t(size_t, maxsize, iter->iov->iov_len);
225 page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
226 OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
230 mmap_read_lock(current->mm);
231 result = get_user_pages(current, current->mm, addr, page_count,
232 rw == READ, 0, *pages, NULL);
233 mmap_read_unlock(current->mm);
235 if (unlikely(result != page_count)) {
236 ll_free_user_pages(*pages, page_count);
244 *npages = page_count;
250 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
251 #if defined(HAVE_DIO_ITER)
252 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
254 return iov_iter_alignment(i);
256 #else /* copied from alignment_iovec() */
257 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
259 const struct iovec *iov = i->iov;
261 size_t size = i->count;
267 res = (unsigned long)iov->iov_base + i->iov_offset;
268 n = iov->iov_len - i->iov_offset;
274 while (size > (++iov)->iov_len) {
275 res |= (unsigned long)iov->iov_base | iov->iov_len;
276 size -= iov->iov_len;
278 res |= (unsigned long)iov->iov_base | size;
285 * Lustre could relax a bit for alignment, io count is not
286 * necessary page alignment.
288 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
290 size_t orig_size = i->count;
291 size_t count = orig_size & ~PAGE_MASK;
295 return iov_iter_alignment_vfs(i);
297 if (orig_size > PAGE_SIZE) {
298 iov_iter_truncate(i, orig_size - count);
299 res = iov_iter_alignment_vfs(i);
300 iov_iter_reexpand(i, orig_size);
305 res = iov_iter_alignment_vfs(i);
306 /* start address is page aligned */
307 if ((res & ~PAGE_MASK) == orig_size)
313 /** direct IO pages */
314 struct ll_dio_pages {
315 struct cl_dio_aio *ldp_aio;
317 * page array to be written. we don't support
318 * partial pages except the last one.
320 struct page **ldp_pages;
321 /** # of pages in the array. */
323 /* the file offset of the first page. */
324 loff_t ldp_file_offset;
328 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
329 int rw, struct inode *inode, struct ll_dio_pages *pv)
331 struct cl_page *page;
332 struct cl_2queue *queue = &io->ci_queue;
333 struct cl_object *obj = io->ci_obj;
334 struct cl_sync_io *anchor = &pv->ldp_aio->cda_sync;
335 loff_t offset = pv->ldp_file_offset;
337 size_t page_size = cl_page_size(obj);
343 cl_2queue_init(queue);
344 for (i = 0; i < pv->ldp_count; i++) {
345 LASSERT(!(offset & (PAGE_SIZE - 1)));
346 page = cl_page_find(env, obj, cl_index(obj, offset),
347 pv->ldp_pages[i], CPT_TRANSIENT);
352 LASSERT(page->cp_type == CPT_TRANSIENT);
353 rc = cl_page_own(env, io, page);
355 cl_page_put(env, page);
359 page->cp_sync_io = anchor;
360 if (inode && IS_ENCRYPTED(inode)) {
361 /* In case of Direct IO on encrypted file, we need to
362 * add a reference to the inode on the cl_page.
363 * This info is required by llcrypt to proceed
364 * to encryption/decryption.
365 * This is safe because we know these pages are private
366 * to the thread doing the Direct IO.
368 page->cp_inode = inode;
370 /* We keep the refcount from cl_page_find, so we don't need
373 cl_2queue_add(queue, page, false);
375 * Set page clip to tell transfer formation engine
376 * that page has to be sent even if it is beyond KMS.
378 if (size < page_size)
379 cl_page_clip(env, page, 0, size);
385 if (rc == 0 && io_pages > 0) {
386 int iot = rw == READ ? CRT_READ : CRT_WRITE;
388 atomic_add(io_pages, &anchor->csi_sync_nr);
390 * Avoid out-of-order execution of adding inflight
391 * modifications count and io submit.
394 rc = cl_io_submit_rw(env, io, iot, queue);
396 cl_page_list_splice(&queue->c2_qout,
397 &pv->ldp_aio->cda_pages);
399 atomic_add(-queue->c2_qin.pl_nr,
400 &anchor->csi_sync_nr);
401 cl_page_list_for_each(page, &queue->c2_qin)
402 page->cp_sync_io = NULL;
404 /* handle partially submitted reqs */
405 if (queue->c2_qin.pl_nr > 0) {
406 CERROR(DFID " failed to submit %d dio pages: %zd\n",
407 PFID(lu_object_fid(&obj->co_lu)),
408 queue->c2_qin.pl_nr, rc);
414 cl_2queue_discard(env, io, queue);
415 cl_2queue_disown(env, io, queue);
416 cl_2queue_fini(env, queue);
420 #ifdef KMALLOC_MAX_SIZE
421 #define MAX_MALLOC KMALLOC_MAX_SIZE
423 #define MAX_MALLOC (128 * 1024)
426 /* This is the maximum size of a single O_DIRECT request, based on the
427 * kmalloc limit. We need to fit all of the brw_page structs, each one
428 * representing PAGE_SIZE worth of user data, into a single buffer, and
429 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
430 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
431 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
432 ~((size_t)DT_MAX_BRW_SIZE - 1))
435 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
437 struct ll_cl_context *lcc;
438 const struct lu_env *env;
440 struct file *file = iocb->ki_filp;
441 struct inode *inode = file->f_mapping->host;
442 struct cl_dio_aio *aio;
443 size_t count = iov_iter_count(iter);
444 ssize_t tot_bytes = 0, result = 0;
445 loff_t file_offset = iocb->ki_pos;
448 /* Check EOF by ourselves */
449 if (rw == READ && file_offset >= i_size_read(inode))
452 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
453 if (file_offset & ~PAGE_MASK)
456 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
457 "offset=%lld=%llx, pages %zd (max %lu)\n",
458 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
459 file_offset, file_offset, count >> PAGE_SHIFT,
460 MAX_DIO_SIZE >> PAGE_SHIFT);
462 /* Check that all user buffers are aligned as well */
463 if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
466 lcc = ll_cl_find(file);
471 LASSERT(!IS_ERR(env));
472 vio = vvp_env_io(env);
478 LASSERT(aio->cda_iocb == iocb);
480 while (iov_iter_count(iter)) {
481 struct ll_dio_pages pvec = { .ldp_aio = aio };
484 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
486 if (file_offset >= i_size_read(inode))
489 if (file_offset + count > i_size_read(inode))
490 count = i_size_read(inode) - file_offset;
493 result = ll_get_user_pages(rw, iter, &pages,
494 &pvec.ldp_count, count);
495 if (unlikely(result <= 0))
499 pvec.ldp_file_offset = file_offset;
500 pvec.ldp_pages = pages;
502 result = ll_direct_rw_pages(env, io, count,
504 ll_free_user_pages(pages, pvec.ldp_count);
506 if (unlikely(result < 0))
509 iov_iter_advance(iter, count);
511 file_offset += count;
515 aio->cda_bytes += tot_bytes;
518 vio->u.readwrite.vui_written += tot_bytes;
520 vio->u.readwrite.vui_read += tot_bytes;
522 /* We cannot do async submission - for AIO or regular DIO - unless
523 * lockless because it causes us to release the lock early.
525 * There are also several circumstances in which we must disable
526 * parallel DIO, so we check if it is enabled.
528 * The check for "is_sync_kiocb" excludes AIO, which does not need to
529 * be disabled in these situations.
531 if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio)) {
534 /* Wait here rather than doing async submission */
535 rc2 = cl_sync_io_wait_recycle(env, &aio->cda_sync, 0, 0);
536 if (result == 0 && rc2)
541 } else if (result == 0) {
542 result = -EIOCBQUEUED;
548 #if defined(HAVE_DIO_ITER)
549 static ssize_t ll_direct_IO(
550 #ifndef HAVE_IOV_ITER_RW
553 struct kiocb *iocb, struct iov_iter *iter
554 #ifndef HAVE_DIRECTIO_2ARGS
561 #ifndef HAVE_IOV_ITER_RW
564 nrw = iov_iter_rw(iter);
567 return ll_direct_IO_impl(iocb, iter, nrw);
570 #else /* !defined(HAVE_DIO_ITER) */
573 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
574 loff_t file_offset, unsigned long nr_segs)
576 struct iov_iter iter;
578 iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
579 return ll_direct_IO_impl(iocb, &iter, rw);
582 #endif /* !defined(HAVE_DIO_ITER) */
585 * Prepare partially written-to page for a write.
586 * @pg is owned when passed in and disowned when it returns non-zero result to
589 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
590 struct cl_page *pg, struct file *file)
592 struct cl_attr *attr = vvp_env_thread_attr(env);
593 struct cl_object *obj = io->ci_obj;
594 struct vvp_page *vpg = cl_object_page_slice(obj, pg);
595 loff_t offset = cl_offset(obj, vvp_index(vpg));
599 cl_object_attr_lock(obj);
600 result = cl_object_attr_get(env, obj, attr);
601 cl_object_attr_unlock(obj);
603 cl_page_disown(env, io, pg);
608 * If are writing to a new page, no need to read old data.
609 * The extent locking will have updated the KMS, and for our
610 * purposes here we can treat it like i_size.
612 if (attr->cat_kms <= offset) {
613 char *kaddr = kmap_atomic(vpg->vpg_page);
615 memset(kaddr, 0, cl_page_size(obj));
616 kunmap_atomic(kaddr);
617 GOTO(out, result = 0);
620 if (vpg->vpg_defer_uptodate) {
621 vpg->vpg_ra_used = 1;
622 GOTO(out, result = 0);
625 result = ll_io_read_page(env, io, pg, file);
629 /* ll_io_read_page() disowns the page */
630 result = cl_page_own(env, io, pg);
632 if (!PageUptodate(cl_page_vmpage(pg))) {
633 cl_page_disown(env, io, pg);
636 } else if (result == -ENOENT) {
637 /* page was truncated */
646 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
648 /* Page must be present, up to date, dirty, and not in writeback. */
649 if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
650 PageWriteback(vmpage) || vmpage->mapping != mapping)
656 static int ll_write_begin(struct file *file, struct address_space *mapping,
657 loff_t pos, unsigned len, unsigned flags,
658 struct page **pagep, void **fsdata)
660 struct ll_cl_context *lcc = NULL;
661 const struct lu_env *env = NULL;
662 struct cl_io *io = NULL;
663 struct cl_page *page = NULL;
665 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
666 pgoff_t index = pos >> PAGE_SHIFT;
667 struct page *vmpage = NULL;
668 unsigned from = pos & (PAGE_SIZE - 1);
669 unsigned to = from + len;
673 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
675 lcc = ll_cl_find(file);
677 vmpage = grab_cache_page_nowait(mapping, index);
678 result = ll_tiny_write_begin(vmpage, mapping);
685 if (file->f_flags & O_DIRECT) {
686 /* direct IO failed because it couldn't clean up cached pages,
687 * this causes a problem for mirror write because the cached
688 * page may belong to another mirror, which will result in
689 * problem submitting the I/O. */
690 if (io->ci_designated_mirror > 0)
691 GOTO(out, result = -EBUSY);
694 * Direct write can fall back to buffered read, but DIO is done
695 * with lockless i/o, and buffered requires LDLM locking, so
696 * in this case we must restart without lockless.
698 if (!io->ci_dio_lock) {
700 io->ci_need_restart = 1;
701 GOTO(out, result = -ENOLCK);
705 /* To avoid deadlock, try to lock page first. */
706 vmpage = grab_cache_page_nowait(mapping, index);
708 if (unlikely(vmpage == NULL ||
709 PageDirty(vmpage) || PageWriteback(vmpage))) {
710 struct vvp_io *vio = vvp_env_io(env);
711 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
713 /* if the page is already in dirty cache, we have to commit
714 * the pages right now; otherwise, it may cause deadlock
715 * because it holds page lock of a dirty page and request for
716 * more grants. It's okay for the dirty page to be the first
717 * one in commit page list, though. */
718 if (vmpage != NULL && plist->pl_nr > 0) {
724 /* commit pages and then wait for page lock */
725 result = vvp_io_write_commit(env, io);
729 if (vmpage == NULL) {
730 vmpage = grab_cache_page_write_begin(mapping, index,
733 GOTO(out, result = -ENOMEM);
737 /* page was truncated */
738 if (mapping != vmpage->mapping) {
739 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
746 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
748 GOTO(out, result = PTR_ERR(page));
750 lcc->lcc_page = page;
751 lu_ref_add(&page->cp_reference, "cl_io", io);
753 cl_page_assume(env, io, page);
754 if (!PageUptodate(vmpage)) {
756 * We're completely overwriting an existing page,
757 * so _don't_ set it up to date until commit_write
759 if (from == 0 && to == PAGE_SIZE) {
760 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
761 POISON_PAGE(vmpage, 0x11);
763 /* TODO: can be optimized at OSC layer to check if it
764 * is a lockless IO. In that case, it's not necessary
765 * to read the data. */
766 result = ll_prepare_partial_page(env, io, page, file);
768 /* vmpage should have been unlocked */
772 if (result == -EAGAIN)
781 if (vmpage != NULL) {
785 /* On tiny_write failure, page and io are always null. */
786 if (!IS_ERR_OR_NULL(page)) {
787 lu_ref_del(&page->cp_reference, "cl_io", io);
788 cl_page_put(env, page);
791 io->ci_result = result;
799 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
800 loff_t pos, unsigned int len, unsigned int copied,
803 struct cl_page *clpage = (struct cl_page *) vmpage->private;
804 loff_t kms = pos+copied;
805 loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
807 struct lu_env *env = cl_env_get(&refcheck);
817 /* This page is dirty in cache, so it should have a cl_page pointer
818 * set in vmpage->private.
820 LASSERT(clpage != NULL);
825 /* Update the underlying size information in the OSC/LOV objects this
828 cl_page_touch(env, clpage, to);
831 cl_env_put(env, &refcheck);
834 /* Must return page unlocked. */
840 static int ll_write_end(struct file *file, struct address_space *mapping,
841 loff_t pos, unsigned len, unsigned copied,
842 struct page *vmpage, void *fsdata)
844 struct ll_cl_context *lcc = fsdata;
845 const struct lu_env *env;
848 struct cl_page *page;
849 unsigned from = pos & (PAGE_SIZE - 1);
856 CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
859 result = ll_tiny_write_end(file, mapping, pos, len, copied,
864 LASSERT(lcc != NULL);
866 page = lcc->lcc_page;
868 vio = vvp_env_io(env);
870 LASSERT(cl_page_is_owned(page, io));
872 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
874 lcc->lcc_page = NULL; /* page will be queued */
876 /* Add it into write queue */
877 cl_page_list_add(plist, page, true);
878 if (plist->pl_nr == 1) /* first page */
879 vio->u.readwrite.vui_from = from;
882 vio->u.readwrite.vui_to = from + copied;
884 /* To address the deadlock in balance_dirty_pages() where
885 * this dirty page may be written back in the same thread. */
886 if (PageDirty(vmpage))
889 /* We may have one full RPC, commit it soon */
890 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
893 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
894 "queued page: %d.\n", plist->pl_nr);
896 cl_page_disown(env, io, page);
898 lcc->lcc_page = NULL;
899 lu_ref_del(&page->cp_reference, "cl_io", io);
900 cl_page_put(env, page);
902 /* page list is not contiguous now, commit it now */
905 if (unplug || io->u.ci_wr.wr_sync)
906 result = vvp_io_write_commit(env, io);
909 io->ci_result = result;
913 RETURN(result >= 0 ? copied : result);
916 #ifdef CONFIG_MIGRATION
917 static int ll_migratepage(struct address_space *mapping,
918 struct page *newpage, struct page *page,
919 enum migrate_mode mode)
921 /* Always fail page migration until we have a proper implementation */
926 const struct address_space_operations ll_aops = {
927 .readpage = ll_readpage,
928 .direct_IO = ll_direct_IO,
929 .writepage = ll_writepage,
930 .writepages = ll_writepages,
931 .set_page_dirty = __set_page_dirty_nobuffers,
932 .write_begin = ll_write_begin,
933 .write_end = ll_write_end,
934 .invalidatepage = ll_invalidatepage,
935 .releasepage = (void *)ll_releasepage,
936 #ifdef CONFIG_MIGRATION
937 .migratepage = ll_migratepage,