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 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
166 struct page ***pages, ssize_t *npages,
169 #if defined(HAVE_DIO_ITER)
174 * iov_iter_get_pages_alloc() is introduced in 3.16 similar
177 result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
179 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
194 addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
195 if (addr & ~PAGE_MASK)
198 size = min_t(size_t, maxsize, iter->iov->iov_len);
199 page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
200 OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
204 mmap_read_lock(current->mm);
205 result = get_user_pages(current, current->mm, addr, page_count,
206 rw == READ, 0, *pages, NULL);
207 mmap_read_unlock(current->mm);
209 if (unlikely(result != page_count)) {
210 ll_release_user_pages(*pages, page_count);
218 *npages = page_count;
224 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
225 #if defined(HAVE_DIO_ITER)
226 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
228 return iov_iter_alignment(i);
230 #else /* copied from alignment_iovec() */
231 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
233 const struct iovec *iov = i->iov;
235 size_t size = i->count;
241 res = (unsigned long)iov->iov_base + i->iov_offset;
242 n = iov->iov_len - i->iov_offset;
248 while (size > (++iov)->iov_len) {
249 res |= (unsigned long)iov->iov_base | iov->iov_len;
250 size -= iov->iov_len;
252 res |= (unsigned long)iov->iov_base | size;
259 * Lustre could relax a bit for alignment, io count is not
260 * necessary page alignment.
262 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
264 size_t orig_size = i->count;
265 size_t count = orig_size & ~PAGE_MASK;
269 return iov_iter_alignment_vfs(i);
271 if (orig_size > PAGE_SIZE) {
272 iov_iter_truncate(i, orig_size - count);
273 res = iov_iter_alignment_vfs(i);
274 iov_iter_reexpand(i, orig_size);
279 res = iov_iter_alignment_vfs(i);
280 /* start address is page aligned */
281 if ((res & ~PAGE_MASK) == orig_size)
288 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
289 int rw, struct inode *inode, struct cl_dio_aio *aio)
291 struct ll_dio_pages *pv = &aio->cda_dio_pages;
292 struct cl_page *page;
293 struct cl_2queue *queue = &io->ci_queue;
294 struct cl_object *obj = io->ci_obj;
295 struct cl_sync_io *anchor = &aio->cda_sync;
296 loff_t offset = pv->ldp_file_offset;
298 size_t page_size = cl_page_size(obj);
304 cl_2queue_init(queue);
305 for (i = 0; i < pv->ldp_count; i++) {
306 LASSERT(!(offset & (PAGE_SIZE - 1)));
307 page = cl_page_find(env, obj, cl_index(obj, offset),
308 pv->ldp_pages[i], CPT_TRANSIENT);
313 LASSERT(page->cp_type == CPT_TRANSIENT);
314 rc = cl_page_own(env, io, page);
316 cl_page_put(env, page);
320 page->cp_sync_io = anchor;
321 if (inode && IS_ENCRYPTED(inode)) {
322 /* In case of Direct IO on encrypted file, we need to
323 * add a reference to the inode on the cl_page.
324 * This info is required by llcrypt to proceed
325 * to encryption/decryption.
326 * This is safe because we know these pages are private
327 * to the thread doing the Direct IO.
329 page->cp_inode = inode;
331 /* We keep the refcount from cl_page_find, so we don't need
334 cl_2queue_add(queue, page, false);
336 * Set page clip to tell transfer formation engine
337 * that page has to be sent even if it is beyond KMS.
339 if (size < page_size)
340 cl_page_clip(env, page, 0, size);
346 if (rc == 0 && io_pages > 0) {
347 int iot = rw == READ ? CRT_READ : CRT_WRITE;
349 atomic_add(io_pages, &anchor->csi_sync_nr);
351 * Avoid out-of-order execution of adding inflight
352 * modifications count and io submit.
355 rc = cl_io_submit_rw(env, io, iot, queue);
357 cl_page_list_splice(&queue->c2_qout, &aio->cda_pages);
359 atomic_add(-queue->c2_qin.pl_nr,
360 &anchor->csi_sync_nr);
361 cl_page_list_for_each(page, &queue->c2_qin)
362 page->cp_sync_io = NULL;
364 /* handle partially submitted reqs */
365 if (queue->c2_qin.pl_nr > 0) {
366 CERROR(DFID " failed to submit %d dio pages: %zd\n",
367 PFID(lu_object_fid(&obj->co_lu)),
368 queue->c2_qin.pl_nr, rc);
374 cl_2queue_discard(env, io, queue);
375 cl_2queue_disown(env, io, queue);
376 cl_2queue_fini(env, queue);
380 #ifdef KMALLOC_MAX_SIZE
381 #define MAX_MALLOC KMALLOC_MAX_SIZE
383 #define MAX_MALLOC (128 * 1024)
386 /* This is the maximum size of a single O_DIRECT request, based on the
387 * kmalloc limit. We need to fit all of the brw_page structs, each one
388 * representing PAGE_SIZE worth of user data, into a single buffer, and
389 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
390 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
391 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
392 ~((size_t)DT_MAX_BRW_SIZE - 1))
395 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
397 struct ll_cl_context *lcc;
398 const struct lu_env *env;
400 struct file *file = iocb->ki_filp;
401 struct inode *inode = file->f_mapping->host;
402 struct cl_dio_aio *ll_aio;
403 struct cl_dio_aio *ldp_aio;
404 size_t count = iov_iter_count(iter);
405 ssize_t tot_bytes = 0, result = 0;
406 loff_t file_offset = iocb->ki_pos;
409 /* Check EOF by ourselves */
410 if (rw == READ && file_offset >= i_size_read(inode))
413 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
414 if (file_offset & ~PAGE_MASK)
417 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
418 "offset=%lld=%llx, pages %zd (max %lu)\n",
419 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
420 file_offset, file_offset, count >> PAGE_SHIFT,
421 MAX_DIO_SIZE >> PAGE_SHIFT);
423 /* Check that all user buffers are aligned as well */
424 if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
427 lcc = ll_cl_find(inode);
432 LASSERT(!IS_ERR(env));
433 vio = vvp_env_io(env);
439 LASSERT(ll_aio->cda_iocb == iocb);
441 while (iov_iter_count(iter)) {
442 struct ll_dio_pages *pvec;
445 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
447 if (file_offset >= i_size_read(inode))
450 if (file_offset + count > i_size_read(inode))
451 count = i_size_read(inode) - file_offset;
454 /* this aio is freed on completion from cl_sync_io_note, so we
455 * do not need to directly free the memory here
457 ldp_aio = cl_aio_alloc(iocb, ll_i2info(inode)->lli_clob, ll_aio);
459 GOTO(out, result = -ENOMEM);
461 pvec = &ldp_aio->cda_dio_pages;
463 result = ll_get_user_pages(rw, iter, &pages,
464 &pvec->ldp_count, count);
465 if (unlikely(result <= 0)) {
466 cl_sync_io_note(env, &ldp_aio->cda_sync, result);
471 pvec->ldp_file_offset = file_offset;
472 pvec->ldp_pages = pages;
474 result = ll_direct_rw_pages(env, io, count,
476 /* We've submitted pages and can now remove the extra
479 cl_sync_io_note(env, &ldp_aio->cda_sync, result);
481 if (unlikely(result < 0))
484 iov_iter_advance(iter, count);
486 file_offset += count;
490 ll_aio->cda_bytes += tot_bytes;
493 vio->u.readwrite.vui_written += tot_bytes;
495 vio->u.readwrite.vui_read += tot_bytes;
497 /* We cannot do async submission - for AIO or regular DIO - unless
498 * lockless because it causes us to release the lock early.
500 * There are also several circumstances in which we must disable
501 * parallel DIO, so we check if it is enabled.
503 * The check for "is_sync_kiocb" excludes AIO, which does not need to
504 * be disabled in these situations.
506 if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio)) {
509 /* Wait here rather than doing async submission */
510 rc2 = cl_sync_io_wait_recycle(env, &ll_aio->cda_sync, 0, 0);
511 if (result == 0 && rc2)
516 } else if (result == 0) {
517 result = -EIOCBQUEUED;
523 #if defined(HAVE_DIO_ITER)
524 static ssize_t ll_direct_IO(
525 #ifndef HAVE_IOV_ITER_RW
528 struct kiocb *iocb, struct iov_iter *iter
529 #ifndef HAVE_DIRECTIO_2ARGS
536 #ifndef HAVE_IOV_ITER_RW
539 nrw = iov_iter_rw(iter);
542 return ll_direct_IO_impl(iocb, iter, nrw);
545 #else /* !defined(HAVE_DIO_ITER) */
548 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
549 loff_t file_offset, unsigned long nr_segs)
551 struct iov_iter iter;
553 iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
554 return ll_direct_IO_impl(iocb, &iter, rw);
557 #endif /* !defined(HAVE_DIO_ITER) */
560 * Prepare partially written-to page for a write.
561 * @pg is owned when passed in and disowned when it returns non-zero result to
564 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
565 struct cl_page *pg, struct file *file)
567 struct cl_attr *attr = vvp_env_thread_attr(env);
568 struct cl_object *obj = io->ci_obj;
569 struct vvp_page *vpg = cl_object_page_slice(obj, pg);
570 loff_t offset = cl_offset(obj, vvp_index(vpg));
574 cl_object_attr_lock(obj);
575 result = cl_object_attr_get(env, obj, attr);
576 cl_object_attr_unlock(obj);
578 cl_page_disown(env, io, pg);
583 * If are writing to a new page, no need to read old data.
584 * The extent locking will have updated the KMS, and for our
585 * purposes here we can treat it like i_size.
587 if (attr->cat_kms <= offset) {
588 char *kaddr = kmap_atomic(vpg->vpg_page);
590 memset(kaddr, 0, cl_page_size(obj));
591 kunmap_atomic(kaddr);
592 GOTO(out, result = 0);
595 if (vpg->vpg_defer_uptodate) {
596 vpg->vpg_ra_used = 1;
597 GOTO(out, result = 0);
600 result = ll_io_read_page(env, io, pg, file);
604 /* ll_io_read_page() disowns the page */
605 result = cl_page_own(env, io, pg);
607 if (!PageUptodate(cl_page_vmpage(pg))) {
608 cl_page_disown(env, io, pg);
611 } else if (result == -ENOENT) {
612 /* page was truncated */
621 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
623 /* Page must be present, up to date, dirty, and not in writeback. */
624 if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
625 PageWriteback(vmpage) || vmpage->mapping != mapping)
631 static int ll_write_begin(struct file *file, struct address_space *mapping,
632 loff_t pos, unsigned len, unsigned flags,
633 struct page **pagep, void **fsdata)
635 struct ll_cl_context *lcc = NULL;
636 const struct lu_env *env = NULL;
637 struct cl_io *io = NULL;
638 struct cl_page *page = NULL;
639 struct inode *inode = file_inode(file);
640 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
641 pgoff_t index = pos >> PAGE_SHIFT;
642 struct page *vmpage = NULL;
643 unsigned from = pos & (PAGE_SIZE - 1);
644 unsigned to = from + len;
648 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
650 lcc = ll_cl_find(inode);
652 vmpage = grab_cache_page_nowait(mapping, index);
653 result = ll_tiny_write_begin(vmpage, mapping);
660 if (file->f_flags & O_DIRECT) {
661 /* direct IO failed because it couldn't clean up cached pages,
662 * this causes a problem for mirror write because the cached
663 * page may belong to another mirror, which will result in
664 * problem submitting the I/O. */
665 if (io->ci_designated_mirror > 0)
666 GOTO(out, result = -EBUSY);
669 * Direct write can fall back to buffered read, but DIO is done
670 * with lockless i/o, and buffered requires LDLM locking, so
671 * in this case we must restart without lockless.
673 if (!io->ci_dio_lock) {
675 io->ci_need_restart = 1;
676 GOTO(out, result = -ENOLCK);
680 /* To avoid deadlock, try to lock page first. */
681 vmpage = grab_cache_page_nowait(mapping, index);
683 if (unlikely(vmpage == NULL ||
684 PageDirty(vmpage) || PageWriteback(vmpage))) {
685 struct vvp_io *vio = vvp_env_io(env);
686 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
688 /* if the page is already in dirty cache, we have to commit
689 * the pages right now; otherwise, it may cause deadlock
690 * because it holds page lock of a dirty page and request for
691 * more grants. It's okay for the dirty page to be the first
692 * one in commit page list, though. */
693 if (vmpage != NULL && plist->pl_nr > 0) {
699 /* commit pages and then wait for page lock */
700 result = vvp_io_write_commit(env, io);
704 if (vmpage == NULL) {
705 vmpage = grab_cache_page_write_begin(mapping, index,
708 GOTO(out, result = -ENOMEM);
712 /* page was truncated */
713 if (mapping != vmpage->mapping) {
714 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
721 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
723 GOTO(out, result = PTR_ERR(page));
725 lcc->lcc_page = page;
726 lu_ref_add(&page->cp_reference, "cl_io", io);
728 cl_page_assume(env, io, page);
729 if (!PageUptodate(vmpage)) {
731 * We're completely overwriting an existing page,
732 * so _don't_ set it up to date until commit_write
734 if (from == 0 && to == PAGE_SIZE) {
735 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
736 POISON_PAGE(vmpage, 0x11);
738 /* TODO: can be optimized at OSC layer to check if it
739 * is a lockless IO. In that case, it's not necessary
740 * to read the data. */
741 result = ll_prepare_partial_page(env, io, page, file);
743 /* vmpage should have been unlocked */
747 if (result == -EAGAIN)
756 if (vmpage != NULL) {
760 /* On tiny_write failure, page and io are always null. */
761 if (!IS_ERR_OR_NULL(page)) {
762 lu_ref_del(&page->cp_reference, "cl_io", io);
763 cl_page_put(env, page);
766 io->ci_result = result;
774 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
775 loff_t pos, unsigned int len, unsigned int copied,
778 struct cl_page *clpage = (struct cl_page *) vmpage->private;
779 loff_t kms = pos+copied;
780 loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
782 struct lu_env *env = cl_env_get(&refcheck);
792 /* This page is dirty in cache, so it should have a cl_page pointer
793 * set in vmpage->private.
795 LASSERT(clpage != NULL);
800 /* Update the underlying size information in the OSC/LOV objects this
803 cl_page_touch(env, clpage, to);
806 cl_env_put(env, &refcheck);
809 /* Must return page unlocked. */
815 static int ll_write_end(struct file *file, struct address_space *mapping,
816 loff_t pos, unsigned len, unsigned copied,
817 struct page *vmpage, void *fsdata)
819 struct ll_cl_context *lcc = fsdata;
820 const struct lu_env *env;
823 struct cl_page *page;
824 unsigned from = pos & (PAGE_SIZE - 1);
831 CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
834 result = ll_tiny_write_end(file, mapping, pos, len, copied,
839 LASSERT(lcc != NULL);
841 page = lcc->lcc_page;
843 vio = vvp_env_io(env);
845 LASSERT(cl_page_is_owned(page, io));
847 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
849 lcc->lcc_page = NULL; /* page will be queued */
851 /* Add it into write queue */
852 cl_page_list_add(plist, page, true);
853 if (plist->pl_nr == 1) /* first page */
854 vio->u.readwrite.vui_from = from;
857 vio->u.readwrite.vui_to = from + copied;
859 /* To address the deadlock in balance_dirty_pages() where
860 * this dirty page may be written back in the same thread. */
861 if (PageDirty(vmpage))
864 /* We may have one full RPC, commit it soon */
865 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
868 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
869 "queued page: %d.\n", plist->pl_nr);
871 cl_page_disown(env, io, page);
873 lcc->lcc_page = NULL;
874 lu_ref_del(&page->cp_reference, "cl_io", io);
875 cl_page_put(env, page);
877 /* page list is not contiguous now, commit it now */
880 if (unplug || io->u.ci_wr.wr_sync)
881 result = vvp_io_write_commit(env, io);
884 io->ci_result = result;
888 RETURN(result >= 0 ? copied : result);
891 #ifdef CONFIG_MIGRATION
892 static int ll_migratepage(struct address_space *mapping,
893 struct page *newpage, struct page *page,
894 enum migrate_mode mode)
896 /* Always fail page migration until we have a proper implementation */
901 const struct address_space_operations ll_aops = {
902 .readpage = ll_readpage,
903 .direct_IO = ll_direct_IO,
904 .writepage = ll_writepage,
905 .writepages = ll_writepages,
906 .set_page_dirty = __set_page_dirty_nobuffers,
907 .write_begin = ll_write_begin,
908 .write_end = ll_write_end,
909 .invalidatepage = ll_invalidatepage,
910 .releasepage = (void *)ll_releasepage,
911 #ifdef CONFIG_MIGRATION
912 .migratepage = ll_migratepage,