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 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
174 ll_direct_IO_seg(const struct lu_env *env, struct cl_io *io, int rw,
175 struct inode *inode, size_t size, loff_t file_offset,
176 struct page **pages, int page_count)
179 struct cl_2queue *queue;
180 struct cl_object *obj = io->ci_obj;
183 size_t page_size = cl_page_size(obj);
184 size_t orig_size = size;
189 queue = &io->ci_queue;
190 cl_2queue_init(queue);
191 for (i = 0; i < page_count; i++) {
192 LASSERT(!(file_offset & (page_size - 1)));
193 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
194 pages[i], CPT_TRANSIENT);
200 rc = cl_page_own(env, io, clp);
202 LASSERT(clp->cp_state == CPS_FREEING);
203 cl_page_put(env, clp);
209 /* check the page type: if the page is a host page, then do
212 if (clp->cp_type == CPT_CACHEABLE) {
213 struct page *vmpage = cl_page_vmpage(clp);
214 struct page *src_page;
215 struct page *dst_page;
219 src_page = (rw == WRITE) ? pages[i] : vmpage;
220 dst_page = (rw == WRITE) ? vmpage : pages[i];
222 src = ll_kmap_atomic(src_page, KM_USER0);
223 dst = ll_kmap_atomic(dst_page, KM_USER1);
224 memcpy(dst, src, min(page_size, size));
225 ll_kunmap_atomic(dst, KM_USER1);
226 ll_kunmap_atomic(src, KM_USER0);
228 /* make sure page will be added to the transfer by
229 * cl_io_submit()->...->vvp_page_prep_write().
232 set_page_dirty(vmpage);
235 /* do not issue the page for read, since it
236 * may reread a ra page which has NOT uptodate
239 cl_page_disown(env, io, clp);
245 cl_2queue_add(queue, clp);
248 * Set page clip to tell transfer formation engine
249 * that page has to be sent even if it is beyond KMS.
251 cl_page_clip(env, clp, 0, min(size, page_size));
256 /* drop the reference count for cl_page_find */
257 cl_page_put(env, clp);
259 file_offset += page_size;
262 if (rc == 0 && io_pages) {
263 rc = cl_io_submit_sync(env, io,
264 rw == READ ? CRT_READ : CRT_WRITE,
270 cl_2queue_discard(env, io, queue);
271 cl_2queue_disown(env, io, queue);
272 cl_2queue_fini(env, queue);
276 /* ll_free_user_pages - tear down page struct array
277 * @pages: array of page struct pointers underlying target buffer */
278 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
282 for (i = 0; i < npages; i++) {
283 if (pages[i] == NULL)
286 set_page_dirty_lock(pages[i]);
290 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
293 OBD_FREE_LARGE(pages, npages * sizeof(*pages));
297 #ifdef KMALLOC_MAX_SIZE
298 #define MAX_MALLOC KMALLOC_MAX_SIZE
300 #define MAX_MALLOC (128 * 1024)
303 /* This is the maximum size of a single O_DIRECT request, based on the
304 * kmalloc limit. We need to fit all of the brw_page structs, each one
305 * representing PAGE_SIZE worth of user data, into a single buffer, and
306 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
307 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
308 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
309 ~(DT_MAX_BRW_SIZE - 1))
311 #ifndef HAVE_IOV_ITER_RW
312 # define iov_iter_rw(iter) rw
315 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
318 # ifndef HAVE_IOV_ITER_RW
321 struct kiocb *iocb, struct iov_iter *iter
322 # ifndef HAVE_DIRECTIO_2ARGS
327 #ifdef HAVE_DIRECTIO_2ARGS
328 loff_t file_offset = iocb->ki_pos;
330 struct ll_cl_context *lcc;
331 const struct lu_env *env;
333 struct file *file = iocb->ki_filp;
334 struct inode *inode = file->f_mapping->host;
335 ssize_t count = iov_iter_count(iter);
336 ssize_t tot_bytes = 0, result = 0;
337 size_t size = MAX_DIO_SIZE;
339 /* Check EOF by ourselves */
340 if (iov_iter_rw(iter) == READ && file_offset >= i_size_read(inode))
342 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
343 if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
346 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
347 "offset=%lld=%llx, pages %zd (max %lu)\n",
348 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
349 file_offset, file_offset, count >> PAGE_SHIFT,
350 MAX_DIO_SIZE >> PAGE_SHIFT);
352 /* Check that all user buffers are aligned as well */
353 if (iov_iter_alignment(iter) & ~PAGE_MASK)
356 lcc = ll_cl_find(file);
361 LASSERT(!IS_ERR(env));
365 /* 0. Need locking between buffered and direct access. and race with
366 * size changing by concurrent truncates and writes.
367 * 1. Need inode mutex to operate transient pages.
369 if (iov_iter_rw(iter) == READ)
372 while (iov_iter_count(iter)) {
376 count = min_t(size_t, iov_iter_count(iter), size);
377 if (iov_iter_rw(iter) == READ) {
378 if (file_offset >= i_size_read(inode))
381 if (file_offset + count > i_size_read(inode))
382 count = i_size_read(inode) - file_offset;
385 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
386 if (likely(result > 0)) {
387 int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
389 result = ll_direct_IO_seg(env, io, iov_iter_rw(iter),
390 inode, result, file_offset,
392 ll_free_user_pages(pages, n,
393 iov_iter_rw(iter) == READ);
396 if (unlikely(result <= 0)) {
397 /* If we can't allocate a large enough buffer
398 * for the request, shrink it to a smaller
399 * PAGE_SIZE multiple and try again.
400 * We should always be able to kmalloc for a
401 * page worth of page pointers = 4MB on i386. */
402 if (result == -ENOMEM &&
403 size > (PAGE_SIZE / sizeof(*pages)) *
405 size = ((((size / 2) - 1) |
406 ~PAGE_MASK) + 1) & PAGE_MASK;
407 CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
415 iov_iter_advance(iter, result);
417 file_offset += result;
420 if (iov_iter_rw(iter) == READ)
424 struct vvp_io *vio = vvp_env_io(env);
426 /* no commit async for direct IO */
427 vio->u.write.vui_written += tot_bytes;
430 return tot_bytes ? : result;
432 #else /* !HAVE_DIRECTIO_ITER && !HAVE_IOV_ITER_RW */
434 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
435 size_t size, struct page ***pages,
438 int result = -ENOMEM;
440 /* set an arbitrary limit to prevent arithmetic overflow */
441 if (size > MAX_DIRECTIO_SIZE) {
446 *max_pages = (user_addr + size + PAGE_SIZE - 1) >>
448 *max_pages -= user_addr >> PAGE_SHIFT;
450 OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
452 down_read(¤t->mm->mmap_sem);
453 result = get_user_pages(current, current->mm, user_addr,
454 *max_pages, (rw == READ), 0, *pages,
456 up_read(¤t->mm->mmap_sem);
457 if (unlikely(result <= 0))
458 OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
465 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
466 loff_t file_offset, unsigned long nr_segs)
468 struct ll_cl_context *lcc;
469 const struct lu_env *env;
471 struct file *file = iocb->ki_filp;
472 struct inode *inode = file->f_mapping->host;
473 ssize_t count = iov_length(iov, nr_segs);
474 ssize_t tot_bytes = 0, result = 0;
475 unsigned long seg = 0;
476 size_t size = MAX_DIO_SIZE;
479 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
480 if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
483 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
484 "offset=%lld=%llx, pages %zd (max %lu)\n",
485 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
486 file_offset, file_offset, count >> PAGE_SHIFT,
487 MAX_DIO_SIZE >> PAGE_SHIFT);
489 /* Check that all user buffers are aligned as well */
490 for (seg = 0; seg < nr_segs; seg++) {
491 if (((unsigned long)iov[seg].iov_base & ~PAGE_MASK) ||
492 (iov[seg].iov_len & ~PAGE_MASK))
496 lcc = ll_cl_find(file);
501 LASSERT(!IS_ERR(env));
505 for (seg = 0; seg < nr_segs; seg++) {
506 size_t iov_left = iov[seg].iov_len;
507 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
510 if (file_offset >= i_size_read(inode))
512 if (file_offset + iov_left > i_size_read(inode))
513 iov_left = i_size_read(inode) - file_offset;
516 while (iov_left > 0) {
518 int page_count, max_pages = 0;
521 bytes = min(size, iov_left);
522 page_count = ll_get_user_pages(rw, user_addr, bytes,
524 if (likely(page_count > 0)) {
525 if (unlikely(page_count < max_pages))
526 bytes = page_count << PAGE_SHIFT;
527 result = ll_direct_IO_seg(env, io, rw, inode,
530 ll_free_user_pages(pages, max_pages, rw==READ);
531 } else if (page_count == 0) {
532 GOTO(out, result = -EFAULT);
536 if (unlikely(result <= 0)) {
537 /* If we can't allocate a large enough buffer
538 * for the request, shrink it to a smaller
539 * PAGE_SIZE multiple and try again.
540 * We should always be able to kmalloc for a
541 * page worth of page pointers = 4MB on i386. */
542 if (result == -ENOMEM &&
543 size > (PAGE_SIZE / sizeof(*pages)) *
545 size = ((((size / 2) - 1) |
548 CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
557 file_offset += result;
564 struct vvp_io *vio = vvp_env_io(env);
566 /* no commit async for direct IO */
567 vio->u.write.vui_written += tot_bytes;
570 RETURN(tot_bytes ? tot_bytes : result);
572 #endif /* HAVE_DIRECTIO_ITER || HAVE_IOV_ITER_RW */
575 * Prepare partially written-to page for a write.
576 * @pg is owned when passed in and disowned when it returns non-zero result to
579 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
580 struct cl_page *pg, struct file *file)
582 struct cl_attr *attr = vvp_env_thread_attr(env);
583 struct cl_object *obj = io->ci_obj;
584 struct vvp_page *vpg = cl_object_page_slice(obj, pg);
585 loff_t offset = cl_offset(obj, vvp_index(vpg));
589 cl_object_attr_lock(obj);
590 result = cl_object_attr_get(env, obj, attr);
591 cl_object_attr_unlock(obj);
593 cl_page_disown(env, io, pg);
598 * If are writing to a new page, no need to read old data.
599 * The extent locking will have updated the KMS, and for our
600 * purposes here we can treat it like i_size.
602 if (attr->cat_kms <= offset) {
603 char *kaddr = ll_kmap_atomic(vpg->vpg_page, KM_USER0);
605 memset(kaddr, 0, cl_page_size(obj));
606 ll_kunmap_atomic(kaddr, KM_USER0);
607 GOTO(out, result = 0);
610 if (vpg->vpg_defer_uptodate) {
611 vpg->vpg_ra_used = 1;
612 GOTO(out, result = 0);
615 result = ll_io_read_page(env, io, pg, file);
619 /* ll_io_read_page() disowns the page */
620 result = cl_page_own(env, io, pg);
622 if (!PageUptodate(cl_page_vmpage(pg))) {
623 cl_page_disown(env, io, pg);
626 } else if (result == -ENOENT) {
627 /* page was truncated */
636 static int ll_tiny_write_begin(struct page *vmpage)
638 /* Page must be present, up to date, dirty, and not in writeback. */
639 if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
640 PageWriteback(vmpage))
646 static int ll_write_begin(struct file *file, struct address_space *mapping,
647 loff_t pos, unsigned len, unsigned flags,
648 struct page **pagep, void **fsdata)
650 struct ll_cl_context *lcc = NULL;
651 const struct lu_env *env = NULL;
652 struct cl_io *io = NULL;
653 struct cl_page *page = NULL;
655 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
656 pgoff_t index = pos >> PAGE_SHIFT;
657 struct page *vmpage = NULL;
658 unsigned from = pos & (PAGE_SIZE - 1);
659 unsigned to = from + len;
663 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
665 lcc = ll_cl_find(file);
667 vmpage = grab_cache_page_nowait(mapping, index);
668 result = ll_tiny_write_begin(vmpage);
675 if (file->f_flags & O_DIRECT && io->ci_designated_mirror > 0) {
676 /* direct IO failed because it couldn't clean up cached pages,
677 * this causes a problem for mirror write because the cached
678 * page may belong to another mirror, which will result in
679 * problem submitting the I/O. */
680 GOTO(out, result = -EBUSY);
684 /* To avoid deadlock, try to lock page first. */
685 vmpage = grab_cache_page_nowait(mapping, index);
687 if (unlikely(vmpage == NULL ||
688 PageDirty(vmpage) || PageWriteback(vmpage))) {
689 struct vvp_io *vio = vvp_env_io(env);
690 struct cl_page_list *plist = &vio->u.write.vui_queue;
692 /* if the page is already in dirty cache, we have to commit
693 * the pages right now; otherwise, it may cause deadlock
694 * because it holds page lock of a dirty page and request for
695 * more grants. It's okay for the dirty page to be the first
696 * one in commit page list, though. */
697 if (vmpage != NULL && plist->pl_nr > 0) {
703 /* commit pages and then wait for page lock */
704 result = vvp_io_write_commit(env, io);
708 if (vmpage == NULL) {
709 vmpage = grab_cache_page_write_begin(mapping, index,
712 GOTO(out, result = -ENOMEM);
716 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
718 GOTO(out, result = PTR_ERR(page));
720 lcc->lcc_page = page;
721 lu_ref_add(&page->cp_reference, "cl_io", io);
723 cl_page_assume(env, io, page);
724 if (!PageUptodate(vmpage)) {
726 * We're completely overwriting an existing page,
727 * so _don't_ set it up to date until commit_write
729 if (from == 0 && to == PAGE_SIZE) {
730 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
731 POISON_PAGE(vmpage, 0x11);
733 /* TODO: can be optimized at OSC layer to check if it
734 * is a lockless IO. In that case, it's not necessary
735 * to read the data. */
736 result = ll_prepare_partial_page(env, io, page, file);
738 /* vmpage should have been unlocked */
742 if (result == -EAGAIN)
751 if (vmpage != NULL) {
755 /* On tiny_write failure, page and io are always null. */
756 if (!IS_ERR_OR_NULL(page)) {
757 lu_ref_del(&page->cp_reference, "cl_io", io);
758 cl_page_put(env, page);
761 io->ci_result = result;
769 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
770 loff_t pos, unsigned int len, unsigned int copied,
773 struct cl_page *clpage = (struct cl_page *) vmpage->private;
774 loff_t kms = pos+copied;
775 loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
777 struct lu_env *env = cl_env_get(&refcheck);
787 /* This page is dirty in cache, so it should have a cl_page pointer
788 * set in vmpage->private.
790 LASSERT(clpage != NULL);
795 /* Update the underlying size information in the OSC/LOV objects this
798 cl_page_touch(env, clpage, to);
801 cl_env_put(env, &refcheck);
804 /* Must return page unlocked. */
810 static int ll_write_end(struct file *file, struct address_space *mapping,
811 loff_t pos, unsigned len, unsigned copied,
812 struct page *vmpage, void *fsdata)
814 struct ll_cl_context *lcc = fsdata;
815 const struct lu_env *env;
818 struct cl_page *page;
819 unsigned from = pos & (PAGE_SIZE - 1);
826 CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
829 result = ll_tiny_write_end(file, mapping, pos, len, copied,
834 LASSERT(lcc != NULL);
836 page = lcc->lcc_page;
838 vio = vvp_env_io(env);
840 LASSERT(cl_page_is_owned(page, io));
842 struct cl_page_list *plist = &vio->u.write.vui_queue;
844 lcc->lcc_page = NULL; /* page will be queued */
846 /* Add it into write queue */
847 cl_page_list_add(plist, page);
848 if (plist->pl_nr == 1) /* first page */
849 vio->u.write.vui_from = from;
852 vio->u.write.vui_to = from + copied;
854 /* To address the deadlock in balance_dirty_pages() where
855 * this dirty page may be written back in the same thread. */
856 if (PageDirty(vmpage))
859 /* We may have one full RPC, commit it soon */
860 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
863 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
864 "queued page: %d.\n", plist->pl_nr);
866 cl_page_disown(env, io, page);
868 lcc->lcc_page = NULL;
869 lu_ref_del(&page->cp_reference, "cl_io", io);
870 cl_page_put(env, page);
872 /* page list is not contiguous now, commit it now */
875 if (unplug || io->u.ci_wr.wr_sync)
876 result = vvp_io_write_commit(env, io);
879 io->ci_result = result;
883 RETURN(result >= 0 ? copied : result);
886 #ifdef CONFIG_MIGRATION
887 static int ll_migratepage(struct address_space *mapping,
888 struct page *newpage, struct page *page
889 #ifdef HAVE_MIGRATEPAGE_4ARGS
890 , enum migrate_mode mode
894 /* Always fail page migration until we have a proper implementation */
899 const struct address_space_operations ll_aops = {
900 .readpage = ll_readpage,
901 .direct_IO = ll_direct_IO,
902 .writepage = ll_writepage,
903 .writepages = ll_writepages,
904 .set_page_dirty = __set_page_dirty_nobuffers,
905 .write_begin = ll_write_begin,
906 .write_end = ll_write_end,
907 .invalidatepage = ll_invalidatepage,
908 .releasepage = (void *)ll_releasepage,
909 #ifdef CONFIG_MIGRATION
910 .migratepage = ll_migratepage,