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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/lustre/llite/rw26.c
34 * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
37 #include <linux/buffer_head.h>
38 #include <linux/errno.h>
40 #include <linux/kernel.h>
42 #include <linux/mpage.h>
43 #include <linux/pagemap.h>
44 #include <linux/string.h>
45 #include <linux/unistd.h>
46 #include <linux/writeback.h>
49 #include <linux/migrate.h>
50 #elif defined(HAVE_MIGRATE_MODE_H)
51 #include <linux/migrate_mode.h>
54 #define DEBUG_SUBSYSTEM S_LLITE
56 #include "llite_internal.h"
57 #include <lustre_compat.h>
60 * Implements Linux VM address_space::invalidatepage() method. This method is
61 * called when the page is truncate from a file, either as a result of
62 * explicit truncate, or when inode is removed from memory (as a result of
63 * final iput(), umount, or memory pressure induced icache shrinking).
65 * [0, offset] bytes of the page remain valid (this is for a case of not-page
66 * aligned truncate). Lustre leaves partially truncated page in the cache,
67 * relying on struct inode::i_size to limit further accesses.
69 static void ll_invalidatepage(struct page *vmpage,
70 #ifdef HAVE_INVALIDATE_RANGE
71 unsigned int offset, unsigned int length
80 struct cl_object *obj;
82 LASSERT(PageLocked(vmpage));
83 LASSERT(!PageWriteback(vmpage));
86 * It is safe to not check anything in invalidatepage/releasepage
87 * below because they are run with page locked and all our io is
88 * happening with locked page too
90 #ifdef HAVE_INVALIDATE_RANGE
91 if (offset == 0 && length == PAGE_SIZE) {
95 /* See the comment in ll_releasepage() */
96 env = cl_env_percpu_get();
97 LASSERT(!IS_ERR(env));
99 inode = vmpage->mapping->host;
100 obj = ll_i2info(inode)->lli_clob;
102 page = cl_vmpage_page(vmpage, obj);
104 cl_page_delete(env, page);
105 cl_page_put(env, page);
108 LASSERT(vmpage->private == 0);
110 cl_env_percpu_put(env);
114 #ifdef HAVE_RELEASEPAGE_WITH_INT
115 #define RELEASEPAGE_ARG_TYPE int
117 #define RELEASEPAGE_ARG_TYPE gfp_t
119 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
122 struct cl_object *obj;
123 struct cl_page *page;
124 struct address_space *mapping;
127 LASSERT(PageLocked(vmpage));
128 if (PageWriteback(vmpage) || PageDirty(vmpage))
131 mapping = vmpage->mapping;
135 obj = ll_i2info(mapping->host)->lli_clob;
139 page = cl_vmpage_page(vmpage, obj);
143 env = cl_env_percpu_get();
144 LASSERT(!IS_ERR(env));
146 if (!cl_page_in_use(page)) {
148 cl_page_delete(env, page);
151 /* To use percpu env array, the call path can not be rescheduled;
152 * otherwise percpu array will be messed if ll_releaspage() called
153 * again on the same CPU.
155 * If this page holds the last refc of cl_object, the following
156 * call path may cause reschedule:
157 * cl_page_put -> cl_page_free -> cl_object_put ->
158 * lu_object_put -> lu_object_free -> lov_delete_raid0.
160 * However, the kernel can't get rid of this inode until all pages have
161 * been cleaned up. Now that we hold page lock here, it's pretty safe
162 * that we won't get into object delete path.
164 LASSERT(cl_object_refc(obj) > 1);
165 cl_page_put(env, page);
167 cl_env_percpu_put(env);
171 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW) || \
172 defined(HAVE_DIRECTIO_2ARGS)
173 #define HAVE_DIO_ITER 1
177 * ll_free_user_pages - tear down page struct array
178 * @pages: array of page struct pointers underlying target buffer
180 static void ll_free_user_pages(struct page **pages, int npages)
184 for (i = 0; i < npages; i++) {
190 #if defined(HAVE_DIO_ITER)
193 OBD_FREE_LARGE(pages, npages * sizeof(*pages));
197 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
198 struct page ***pages, ssize_t *npages,
201 #if defined(HAVE_DIO_ITER)
206 * iov_iter_get_pages_alloc() is introduced in 3.16 similar
209 result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
211 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
226 addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
227 if (addr & ~PAGE_MASK)
230 size = min_t(size_t, maxsize, iter->iov->iov_len);
231 page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
232 OBD_ALLOC_LARGE(*pages, page_count * sizeof(**pages));
236 down_read(¤t->mm->mmap_sem);
237 result = get_user_pages(current, current->mm, addr, page_count,
238 rw == READ, 0, *pages, NULL);
239 up_read(¤t->mm->mmap_sem);
241 if (unlikely(result != page_count)) {
242 ll_free_user_pages(*pages, page_count);
250 *npages = page_count;
256 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
257 #if defined(HAVE_DIO_ITER)
258 static unsigned long ll_iov_iter_alignment(const struct iov_iter *i)
260 return iov_iter_alignment(i);
262 #else /* copied from alignment_iovec() */
263 static unsigned long ll_iov_iter_alignment(const struct iov_iter *i)
265 const struct iovec *iov = i->iov;
267 size_t size = i->count;
273 res = (unsigned long)iov->iov_base + i->iov_offset;
274 n = iov->iov_len - i->iov_offset;
280 while (size > (++iov)->iov_len) {
281 res |= (unsigned long)iov->iov_base | iov->iov_len;
282 size -= iov->iov_len;
284 res |= (unsigned long)iov->iov_base | size;
290 #ifndef HAVE_AIO_COMPLETE
291 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
293 if (iocb->ki_complete)
294 iocb->ki_complete(iocb, res, res2);
298 /** direct IO pages */
299 struct ll_dio_pages {
300 struct cl_dio_aio *ldp_aio;
302 * page array to be written. we don't support
303 * partial pages except the last one.
305 struct page **ldp_pages;
306 /** # of pages in the array. */
308 /* the file offset of the first page. */
309 loff_t ldp_file_offset;
312 static void ll_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
314 struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
315 ssize_t ret = anchor->csi_sync_rc;
320 while (aio->cda_pages.pl_nr > 0) {
321 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
324 cl_page_list_del(env, &aio->cda_pages, page);
325 cl_page_delete(env, page);
326 cl_page_put(env, page);
329 if (!is_sync_kiocb(aio->cda_iocb))
330 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
335 static struct cl_dio_aio *ll_aio_alloc(struct kiocb *iocb)
337 struct cl_dio_aio *aio;
342 * Hold one ref so that it won't be released until
343 * every pages is added.
345 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
346 NULL : aio, ll_aio_end);
347 cl_page_list_init(&aio->cda_pages);
348 aio->cda_iocb = iocb;
354 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
355 int rw, struct inode *inode, struct ll_dio_pages *pv)
357 struct cl_page *page;
358 struct cl_2queue *queue = &io->ci_queue;
359 struct cl_object *obj = io->ci_obj;
360 struct cl_sync_io *anchor = &pv->ldp_aio->cda_sync;
361 loff_t offset = pv->ldp_file_offset;
363 size_t page_size = cl_page_size(obj);
369 cl_2queue_init(queue);
370 for (i = 0; i < pv->ldp_count; i++) {
371 LASSERT(!(offset & (PAGE_SIZE - 1)));
372 page = cl_page_find(env, obj, cl_index(obj, offset),
373 pv->ldp_pages[i], CPT_TRANSIENT);
378 LASSERT(page->cp_type == CPT_TRANSIENT);
379 rc = cl_page_own(env, io, page);
381 cl_page_put(env, page);
385 page->cp_sync_io = anchor;
386 cl_2queue_add(queue, page);
388 * Set page clip to tell transfer formation engine
389 * that page has to be sent even if it is beyond KMS.
391 cl_page_clip(env, page, 0, min(size, page_size));
394 /* drop the reference count for cl_page_find */
395 cl_page_put(env, page);
399 if (rc == 0 && io_pages > 0) {
400 int iot = rw == READ ? CRT_READ : CRT_WRITE;
402 atomic_add(io_pages, &anchor->csi_sync_nr);
403 rc = cl_io_submit_rw(env, io, iot, queue);
405 cl_page_list_splice(&queue->c2_qout,
406 &pv->ldp_aio->cda_pages);
408 atomic_add(-queue->c2_qin.pl_nr,
409 &anchor->csi_sync_nr);
410 cl_page_list_for_each(page, &queue->c2_qin)
411 page->cp_sync_io = NULL;
413 /* handle partially submitted reqs */
414 if (queue->c2_qin.pl_nr > 0) {
415 CERROR(DFID " failed to submit %d dio pages: %zd\n",
416 PFID(lu_object_fid(&obj->co_lu)),
417 queue->c2_qin.pl_nr, rc);
423 cl_2queue_discard(env, io, queue);
424 cl_2queue_disown(env, io, queue);
425 cl_2queue_fini(env, queue);
429 #ifdef KMALLOC_MAX_SIZE
430 #define MAX_MALLOC KMALLOC_MAX_SIZE
432 #define MAX_MALLOC (128 * 1024)
435 /* This is the maximum size of a single O_DIRECT request, based on the
436 * kmalloc limit. We need to fit all of the brw_page structs, each one
437 * representing PAGE_SIZE worth of user data, into a single buffer, and
438 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
439 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
440 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
441 ~(DT_MAX_BRW_SIZE - 1))
444 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
446 struct ll_cl_context *lcc;
447 const struct lu_env *env;
449 struct file *file = iocb->ki_filp;
450 struct inode *inode = file->f_mapping->host;
451 struct cl_dio_aio *aio;
452 size_t count = iov_iter_count(iter);
453 ssize_t tot_bytes = 0, result = 0;
454 loff_t file_offset = iocb->ki_pos;
456 /* Check EOF by ourselves */
457 if (rw == READ && file_offset >= i_size_read(inode))
460 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
461 if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
464 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
465 "offset=%lld=%llx, pages %zd (max %lu)\n",
466 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
467 file_offset, file_offset, count >> PAGE_SHIFT,
468 MAX_DIO_SIZE >> PAGE_SHIFT);
470 /* Check that all user buffers are aligned as well */
471 if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
474 lcc = ll_cl_find(file);
479 LASSERT(!IS_ERR(env));
483 aio = ll_aio_alloc(iocb);
487 /* 0. Need locking between buffered and direct access. and race with
488 * size changing by concurrent truncates and writes.
489 * 1. Need inode mutex to operate transient pages.
494 while (iov_iter_count(iter)) {
495 struct ll_dio_pages pvec = { .ldp_aio = aio };
498 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
500 if (file_offset >= i_size_read(inode))
503 if (file_offset + count > i_size_read(inode))
504 count = i_size_read(inode) - file_offset;
507 result = ll_get_user_pages(rw, iter, &pages,
508 &pvec.ldp_count, count);
509 if (unlikely(result <= 0))
513 pvec.ldp_file_offset = file_offset;
514 pvec.ldp_pages = pages;
516 result = ll_direct_rw_pages(env, io, count,
518 ll_free_user_pages(pages, pvec.ldp_count);
520 if (unlikely(result < 0))
523 iov_iter_advance(iter, count);
525 file_offset += count;
529 aio->cda_bytes = tot_bytes;
530 cl_sync_io_note(env, &aio->cda_sync, result);
532 if (is_sync_kiocb(iocb)) {
535 rc2 = cl_sync_io_wait(env, &aio->cda_sync, 0);
536 if (result == 0 && rc2)
540 struct vvp_io *vio = vvp_env_io(env);
541 /* no commit async for direct IO */
542 vio->u.write.vui_written += tot_bytes;
548 result = -EIOCBQUEUED;
557 #if defined(HAVE_DIO_ITER)
558 static ssize_t ll_direct_IO(
559 #ifndef HAVE_IOV_ITER_RW
562 struct kiocb *iocb, struct iov_iter *iter
563 #ifndef HAVE_DIRECTIO_2ARGS
570 #ifndef HAVE_IOV_ITER_RW
573 nrw = iov_iter_rw(iter);
576 return ll_direct_IO_impl(iocb, iter, nrw);
579 #else /* !defined(HAVE_DIO_ITER) */
582 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
583 loff_t file_offset, unsigned long nr_segs)
585 struct iov_iter iter;
587 iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
588 return ll_direct_IO_impl(iocb, &iter, rw);
591 #endif /* !defined(HAVE_DIO_ITER) */
594 * Prepare partially written-to page for a write.
595 * @pg is owned when passed in and disowned when it returns non-zero result to
598 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
599 struct cl_page *pg, struct file *file)
601 struct cl_attr *attr = vvp_env_thread_attr(env);
602 struct cl_object *obj = io->ci_obj;
603 struct vvp_page *vpg = cl_object_page_slice(obj, pg);
604 loff_t offset = cl_offset(obj, vvp_index(vpg));
608 cl_object_attr_lock(obj);
609 result = cl_object_attr_get(env, obj, attr);
610 cl_object_attr_unlock(obj);
612 cl_page_disown(env, io, pg);
617 * If are writing to a new page, no need to read old data.
618 * The extent locking will have updated the KMS, and for our
619 * purposes here we can treat it like i_size.
621 if (attr->cat_kms <= offset) {
622 char *kaddr = ll_kmap_atomic(vpg->vpg_page, KM_USER0);
624 memset(kaddr, 0, cl_page_size(obj));
625 ll_kunmap_atomic(kaddr, KM_USER0);
626 GOTO(out, result = 0);
629 if (vpg->vpg_defer_uptodate) {
630 vpg->vpg_ra_used = 1;
631 GOTO(out, result = 0);
634 result = ll_io_read_page(env, io, pg, file);
638 /* ll_io_read_page() disowns the page */
639 result = cl_page_own(env, io, pg);
641 if (!PageUptodate(cl_page_vmpage(pg))) {
642 cl_page_disown(env, io, pg);
645 } else if (result == -ENOENT) {
646 /* page was truncated */
655 static int ll_tiny_write_begin(struct page *vmpage)
657 /* Page must be present, up to date, dirty, and not in writeback. */
658 if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
659 PageWriteback(vmpage))
665 static int ll_write_begin(struct file *file, struct address_space *mapping,
666 loff_t pos, unsigned len, unsigned flags,
667 struct page **pagep, void **fsdata)
669 struct ll_cl_context *lcc = NULL;
670 const struct lu_env *env = NULL;
671 struct cl_io *io = NULL;
672 struct cl_page *page = NULL;
674 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
675 pgoff_t index = pos >> PAGE_SHIFT;
676 struct page *vmpage = NULL;
677 unsigned from = pos & (PAGE_SIZE - 1);
678 unsigned to = from + len;
682 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
684 lcc = ll_cl_find(file);
686 vmpage = grab_cache_page_nowait(mapping, index);
687 result = ll_tiny_write_begin(vmpage);
694 if (file->f_flags & O_DIRECT) {
695 /* direct IO failed because it couldn't clean up cached pages,
696 * this causes a problem for mirror write because the cached
697 * page may belong to another mirror, which will result in
698 * problem submitting the I/O. */
699 if (io->ci_designated_mirror > 0)
700 GOTO(out, result = -EBUSY);
703 * Direct read can fall back to buffered read, but DIO is done
704 * with lockless i/o, and buffered requires LDLM locking, so
705 * in this case we must restart without lockless.
707 if (!io->ci_ignore_lockless) {
708 io->ci_ignore_lockless = 1;
709 io->ci_need_restart = 1;
710 GOTO(out, result = -ENOLCK);
714 /* To avoid deadlock, try to lock page first. */
715 vmpage = grab_cache_page_nowait(mapping, index);
717 if (unlikely(vmpage == NULL ||
718 PageDirty(vmpage) || PageWriteback(vmpage))) {
719 struct vvp_io *vio = vvp_env_io(env);
720 struct cl_page_list *plist = &vio->u.write.vui_queue;
722 /* if the page is already in dirty cache, we have to commit
723 * the pages right now; otherwise, it may cause deadlock
724 * because it holds page lock of a dirty page and request for
725 * more grants. It's okay for the dirty page to be the first
726 * one in commit page list, though. */
727 if (vmpage != NULL && plist->pl_nr > 0) {
733 /* commit pages and then wait for page lock */
734 result = vvp_io_write_commit(env, io);
738 if (vmpage == NULL) {
739 vmpage = grab_cache_page_write_begin(mapping, index,
742 GOTO(out, result = -ENOMEM);
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.write.vui_queue;
874 lcc->lcc_page = NULL; /* page will be queued */
876 /* Add it into write queue */
877 cl_page_list_add(plist, page);
878 if (plist->pl_nr == 1) /* first page */
879 vio->u.write.vui_from = from;
882 vio->u.write.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 #ifdef HAVE_MIGRATEPAGE_4ARGS
920 , enum migrate_mode mode
924 /* Always fail page migration until we have a proper implementation */
929 const struct address_space_operations ll_aops = {
930 .readpage = ll_readpage,
931 .direct_IO = ll_direct_IO,
932 .writepage = ll_writepage,
933 .writepages = ll_writepages,
934 .set_page_dirty = __set_page_dirty_nobuffers,
935 .write_begin = ll_write_begin,
936 .write_end = ll_write_end,
937 .invalidatepage = ll_invalidatepage,
938 .releasepage = (void *)ll_releasepage,
939 #ifdef CONFIG_MIGRATION
940 .migratepage = ll_migratepage,