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, 2014, 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/kernel.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <asm/uaccess.h>
46 #include <linux/migrate.h>
47 #elif defined(HAVE_MIGRATE_MODE_H)
48 #include <linux/migrate_mode.h>
51 #include <linux/buffer_head.h>
52 #include <linux/mpage.h>
53 #include <linux/writeback.h>
54 #include <linux/stat.h>
55 #include <asm/uaccess.h>
57 #include <linux/pagemap.h>
59 #define DEBUG_SUBSYSTEM S_LLITE
61 #include "llite_internal.h"
62 #include <lustre_compat.h>
65 * Implements Linux VM address_space::invalidatepage() method. This method is
66 * called when the page is truncate from a file, either as a result of
67 * explicit truncate, or when inode is removed from memory (as a result of
68 * final iput(), umount, or memory pressure induced icache shrinking).
70 * [0, offset] bytes of the page remain valid (this is for a case of not-page
71 * aligned truncate). Lustre leaves partially truncated page in the cache,
72 * relying on struct inode::i_size to limit further accesses.
74 static void ll_invalidatepage(struct page *vmpage,
75 #ifdef HAVE_INVALIDATE_RANGE
76 unsigned int offset, unsigned int length
85 struct cl_object *obj;
87 LASSERT(PageLocked(vmpage));
88 LASSERT(!PageWriteback(vmpage));
91 * It is safe to not check anything in invalidatepage/releasepage
92 * below because they are run with page locked and all our io is
93 * happening with locked page too
95 #ifdef HAVE_INVALIDATE_RANGE
96 if (offset == 0 && length == PAGE_SIZE) {
100 /* See the comment in ll_releasepage() */
101 env = cl_env_percpu_get();
102 LASSERT(!IS_ERR(env));
104 inode = vmpage->mapping->host;
105 obj = ll_i2info(inode)->lli_clob;
107 page = cl_vmpage_page(vmpage, obj);
109 cl_page_delete(env, page);
110 cl_page_put(env, page);
113 LASSERT(vmpage->private == 0);
115 cl_env_percpu_put(env);
119 #ifdef HAVE_RELEASEPAGE_WITH_INT
120 #define RELEASEPAGE_ARG_TYPE int
122 #define RELEASEPAGE_ARG_TYPE gfp_t
124 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
127 struct cl_object *obj;
128 struct cl_page *page;
129 struct address_space *mapping;
132 LASSERT(PageLocked(vmpage));
133 if (PageWriteback(vmpage) || PageDirty(vmpage))
136 mapping = vmpage->mapping;
140 obj = ll_i2info(mapping->host)->lli_clob;
144 /* 1 for caller, 1 for cl_page and 1 for page cache */
145 if (page_count(vmpage) > 3)
148 page = cl_vmpage_page(vmpage, obj);
152 env = cl_env_percpu_get();
153 LASSERT(!IS_ERR(env));
155 if (!cl_page_in_use(page)) {
157 cl_page_delete(env, page);
160 /* To use percpu env array, the call path can not be rescheduled;
161 * otherwise percpu array will be messed if ll_releaspage() called
162 * again on the same CPU.
164 * If this page holds the last refc of cl_object, the following
165 * call path may cause reschedule:
166 * cl_page_put -> cl_page_free -> cl_object_put ->
167 * lu_object_put -> lu_object_free -> lov_delete_raid0.
169 * However, the kernel can't get rid of this inode until all pages have
170 * been cleaned up. Now that we hold page lock here, it's pretty safe
171 * that we won't get into object delete path.
173 LASSERT(cl_object_refc(obj) > 1);
174 cl_page_put(env, page);
176 cl_env_percpu_put(env);
180 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
182 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
183 int rw, struct inode *inode,
184 struct ll_dio_pages *pv)
187 struct cl_2queue *queue;
188 struct cl_object *obj = io->ci_obj;
191 loff_t file_offset = pv->ldp_start_offset;
192 size_t size = pv->ldp_size;
193 int page_count = pv->ldp_nr;
194 struct page **pages = pv->ldp_pages;
195 size_t page_size = cl_page_size(obj);
200 queue = &io->ci_queue;
201 cl_2queue_init(queue);
202 for (i = 0; i < page_count; i++) {
204 file_offset = pv->ldp_offsets[i];
206 LASSERT(!(file_offset & (page_size - 1)));
207 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
208 pv->ldp_pages[i], CPT_TRANSIENT);
214 rc = cl_page_own(env, io, clp);
216 LASSERT(clp->cp_state == CPS_FREEING);
217 cl_page_put(env, clp);
223 /* check the page type: if the page is a host page, then do
225 if (clp->cp_type == CPT_CACHEABLE) {
226 struct page *vmpage = cl_page_vmpage(clp);
227 struct page *src_page;
228 struct page *dst_page;
232 src_page = (rw == WRITE) ? pages[i] : vmpage;
233 dst_page = (rw == WRITE) ? vmpage : pages[i];
235 src = ll_kmap_atomic(src_page, KM_USER0);
236 dst = ll_kmap_atomic(dst_page, KM_USER1);
237 memcpy(dst, src, min(page_size, size));
238 ll_kunmap_atomic(dst, KM_USER1);
239 ll_kunmap_atomic(src, KM_USER0);
241 /* make sure page will be added to the transfer by
242 * cl_io_submit()->...->vvp_page_prep_write(). */
244 set_page_dirty(vmpage);
247 /* do not issue the page for read, since it
248 * may reread a ra page which has NOT uptodate
250 cl_page_disown(env, io, clp);
256 cl_2queue_add(queue, clp);
259 * Set page clip to tell transfer formation engine
260 * that page has to be sent even if it is beyond KMS.
262 cl_page_clip(env, clp, 0, min(size, page_size));
267 /* drop the reference count for cl_page_find */
268 cl_page_put(env, clp);
270 file_offset += page_size;
273 if (rc == 0 && io_pages) {
274 rc = cl_io_submit_sync(env, io,
275 rw == READ ? CRT_READ : CRT_WRITE,
281 cl_2queue_discard(env, io, queue);
282 cl_2queue_disown(env, io, queue);
283 cl_2queue_fini(env, queue);
286 EXPORT_SYMBOL(ll_direct_rw_pages);
289 ll_direct_IO_seg(const struct lu_env *env, struct cl_io *io, int rw,
290 struct inode *inode, size_t size, loff_t file_offset,
291 struct page **pages, int page_count)
293 struct ll_dio_pages pvec = { .ldp_pages = pages,
294 .ldp_nr = page_count,
297 .ldp_start_offset = file_offset
300 return ll_direct_rw_pages(env, io, rw, inode, &pvec);
303 /* ll_free_user_pages - tear down page struct array
304 * @pages: array of page struct pointers underlying target buffer */
305 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
309 for (i = 0; i < npages; i++) {
310 if (pages[i] == NULL)
313 set_page_dirty_lock(pages[i]);
317 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
320 OBD_FREE_LARGE(pages, npages * sizeof(*pages));
324 #ifdef KMALLOC_MAX_SIZE
325 #define MAX_MALLOC KMALLOC_MAX_SIZE
327 #define MAX_MALLOC (128 * 1024)
330 /* This is the maximum size of a single O_DIRECT request, based on the
331 * kmalloc limit. We need to fit all of the brw_page structs, each one
332 * representing PAGE_SIZE worth of user data, into a single buffer, and
333 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
334 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
335 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
336 ~(DT_MAX_BRW_SIZE - 1))
338 #ifndef HAVE_IOV_ITER_RW
339 # define iov_iter_rw(iter) rw
342 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW)
345 # ifndef HAVE_IOV_ITER_RW
348 struct kiocb *iocb, struct iov_iter *iter,
351 struct ll_cl_context *lcc;
352 const struct lu_env *env;
354 struct file *file = iocb->ki_filp;
355 struct inode *inode = file->f_mapping->host;
356 ssize_t count = iov_iter_count(iter);
357 ssize_t tot_bytes = 0, result = 0;
358 size_t size = MAX_DIO_SIZE;
360 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
361 if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
364 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
365 "offset=%lld=%llx, pages %zd (max %lu)\n",
366 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
367 file_offset, file_offset, count >> PAGE_SHIFT,
368 MAX_DIO_SIZE >> PAGE_SHIFT);
370 /* Check that all user buffers are aligned as well */
371 if (iov_iter_alignment(iter) & ~PAGE_MASK)
374 lcc = ll_cl_find(file);
379 LASSERT(!IS_ERR(env));
383 /* 0. Need locking between buffered and direct access. and race with
384 * size changing by concurrent truncates and writes.
385 * 1. Need inode mutex to operate transient pages.
387 if (iov_iter_rw(iter) == READ)
390 while (iov_iter_count(iter)) {
394 count = min_t(size_t, iov_iter_count(iter), size);
395 if (iov_iter_rw(iter) == READ) {
396 if (file_offset >= i_size_read(inode))
399 if (file_offset + count > i_size_read(inode))
400 count = i_size_read(inode) - file_offset;
403 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
404 if (likely(result > 0)) {
405 int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
407 result = ll_direct_IO_seg(env, io, iov_iter_rw(iter),
408 inode, result, file_offset,
410 ll_free_user_pages(pages, n,
411 iov_iter_rw(iter) == READ);
414 if (unlikely(result <= 0)) {
415 /* If we can't allocate a large enough buffer
416 * for the request, shrink it to a smaller
417 * PAGE_SIZE multiple and try again.
418 * We should always be able to kmalloc for a
419 * page worth of page pointers = 4MB on i386. */
420 if (result == -ENOMEM &&
421 size > (PAGE_SIZE / sizeof(*pages)) *
423 size = ((((size / 2) - 1) |
424 ~PAGE_MASK) + 1) & PAGE_MASK;
425 CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
433 iov_iter_advance(iter, result);
435 file_offset += result;
438 if (iov_iter_rw(iter) == READ)
442 struct vvp_io *vio = vvp_env_io(env);
444 /* no commit async for direct IO */
445 vio->u.write.vui_written += tot_bytes;
448 return tot_bytes ? : result;
450 #else /* !HAVE_DIRECTIO_ITER && !HAVE_IOV_ITER_RW */
452 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
453 size_t size, struct page ***pages,
456 int result = -ENOMEM;
458 /* set an arbitrary limit to prevent arithmetic overflow */
459 if (size > MAX_DIRECTIO_SIZE) {
464 *max_pages = (user_addr + size + PAGE_SIZE - 1) >>
466 *max_pages -= user_addr >> PAGE_SHIFT;
468 OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
470 down_read(¤t->mm->mmap_sem);
471 result = get_user_pages(current, current->mm, user_addr,
472 *max_pages, (rw == READ), 0, *pages,
474 up_read(¤t->mm->mmap_sem);
475 if (unlikely(result <= 0))
476 OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
483 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
484 loff_t file_offset, unsigned long nr_segs)
486 struct ll_cl_context *lcc;
487 const struct lu_env *env;
489 struct file *file = iocb->ki_filp;
490 struct inode *inode = file->f_mapping->host;
491 ssize_t count = iov_length(iov, nr_segs);
492 ssize_t tot_bytes = 0, result = 0;
493 unsigned long seg = 0;
494 size_t size = MAX_DIO_SIZE;
497 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
498 if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
501 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
502 "offset=%lld=%llx, pages %zd (max %lu)\n",
503 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
504 file_offset, file_offset, count >> PAGE_SHIFT,
505 MAX_DIO_SIZE >> PAGE_SHIFT);
507 /* Check that all user buffers are aligned as well */
508 for (seg = 0; seg < nr_segs; seg++) {
509 if (((unsigned long)iov[seg].iov_base & ~PAGE_MASK) ||
510 (iov[seg].iov_len & ~PAGE_MASK))
514 lcc = ll_cl_find(file);
519 LASSERT(!IS_ERR(env));
523 for (seg = 0; seg < nr_segs; seg++) {
524 size_t iov_left = iov[seg].iov_len;
525 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
528 if (file_offset >= i_size_read(inode))
530 if (file_offset + iov_left > i_size_read(inode))
531 iov_left = i_size_read(inode) - file_offset;
534 while (iov_left > 0) {
536 int page_count, max_pages = 0;
539 bytes = min(size, iov_left);
540 page_count = ll_get_user_pages(rw, user_addr, bytes,
542 if (likely(page_count > 0)) {
543 if (unlikely(page_count < max_pages))
544 bytes = page_count << PAGE_SHIFT;
545 result = ll_direct_IO_seg(env, io, rw, inode,
548 ll_free_user_pages(pages, max_pages, rw==READ);
549 } else if (page_count == 0) {
550 GOTO(out, result = -EFAULT);
554 if (unlikely(result <= 0)) {
555 /* If we can't allocate a large enough buffer
556 * for the request, shrink it to a smaller
557 * PAGE_SIZE multiple and try again.
558 * We should always be able to kmalloc for a
559 * page worth of page pointers = 4MB on i386. */
560 if (result == -ENOMEM &&
561 size > (PAGE_SIZE / sizeof(*pages)) *
563 size = ((((size / 2) - 1) |
566 CDEBUG(D_VFSTRACE, "DIO size now %zu\n",
575 file_offset += result;
582 struct vvp_io *vio = vvp_env_io(env);
584 /* no commit async for direct IO */
585 vio->u.write.vui_written += tot_bytes;
588 RETURN(tot_bytes ? tot_bytes : result);
590 #endif /* HAVE_DIRECTIO_ITER || HAVE_IOV_ITER_RW */
593 * Prepare partially written-to page for a write.
595 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
598 struct cl_attr *attr = vvp_env_thread_attr(env);
599 struct cl_object *obj = io->ci_obj;
600 struct vvp_page *vpg = cl_object_page_slice(obj, pg);
601 loff_t offset = cl_offset(obj, vvp_index(vpg));
604 cl_object_attr_lock(obj);
605 result = cl_object_attr_get(env, obj, attr);
606 cl_object_attr_unlock(obj);
609 * If are writing to a new page, no need to read old data.
610 * The extent locking will have updated the KMS, and for our
611 * purposes here we can treat it like i_size.
613 if (attr->cat_kms <= offset) {
614 char *kaddr = ll_kmap_atomic(vpg->vpg_page, KM_USER0);
616 memset(kaddr, 0, cl_page_size(obj));
617 ll_kunmap_atomic(kaddr, KM_USER0);
618 } else if (vpg->vpg_defer_uptodate)
619 vpg->vpg_ra_used = 1;
621 result = ll_page_sync_io(env, io, pg, CRT_READ);
626 static int ll_write_begin(struct file *file, struct address_space *mapping,
627 loff_t pos, unsigned len, unsigned flags,
628 struct page **pagep, void **fsdata)
630 struct ll_cl_context *lcc;
631 const struct lu_env *env = NULL;
633 struct cl_page *page = NULL;
635 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
636 pgoff_t index = pos >> PAGE_SHIFT;
637 struct page *vmpage = NULL;
638 unsigned from = pos & (PAGE_SIZE - 1);
639 unsigned to = from + len;
643 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
645 lcc = ll_cl_find(file);
648 GOTO(out, result = -EIO);
654 /* To avoid deadlock, try to lock page first. */
655 vmpage = grab_cache_page_nowait(mapping, index);
657 if (unlikely(vmpage == NULL ||
658 PageDirty(vmpage) || PageWriteback(vmpage))) {
659 struct vvp_io *vio = vvp_env_io(env);
660 struct cl_page_list *plist = &vio->u.write.vui_queue;
662 /* if the page is already in dirty cache, we have to commit
663 * the pages right now; otherwise, it may cause deadlock
664 * because it holds page lock of a dirty page and request for
665 * more grants. It's okay for the dirty page to be the first
666 * one in commit page list, though. */
667 if (vmpage != NULL && plist->pl_nr > 0) {
673 /* commit pages and then wait for page lock */
674 result = vvp_io_write_commit(env, io);
678 if (vmpage == NULL) {
679 vmpage = grab_cache_page_write_begin(mapping, index,
682 GOTO(out, result = -ENOMEM);
686 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
688 GOTO(out, result = PTR_ERR(page));
690 lcc->lcc_page = page;
691 lu_ref_add(&page->cp_reference, "cl_io", io);
693 cl_page_assume(env, io, page);
694 if (!PageUptodate(vmpage)) {
696 * We're completely overwriting an existing page,
697 * so _don't_ set it up to date until commit_write
699 if (from == 0 && to == PAGE_SIZE) {
700 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
701 POISON_PAGE(vmpage, 0x11);
703 /* TODO: can be optimized at OSC layer to check if it
704 * is a lockless IO. In that case, it's not necessary
705 * to read the data. */
706 result = ll_prepare_partial_page(env, io, page);
708 SetPageUptodate(vmpage);
712 cl_page_unassume(env, io, page);
716 if (vmpage != NULL) {
720 if (!IS_ERR_OR_NULL(page)) {
721 lu_ref_del(&page->cp_reference, "cl_io", io);
722 cl_page_put(env, page);
725 io->ci_result = result;
733 static int ll_write_end(struct file *file, struct address_space *mapping,
734 loff_t pos, unsigned len, unsigned copied,
735 struct page *vmpage, void *fsdata)
737 struct ll_cl_context *lcc = fsdata;
738 const struct lu_env *env;
741 struct cl_page *page;
742 unsigned from = pos & (PAGE_SIZE - 1);
749 LASSERT(lcc != NULL);
751 page = lcc->lcc_page;
753 vio = vvp_env_io(env);
755 LASSERT(cl_page_is_owned(page, io));
757 struct cl_page_list *plist = &vio->u.write.vui_queue;
759 lcc->lcc_page = NULL; /* page will be queued */
761 /* Add it into write queue */
762 cl_page_list_add(plist, page);
763 if (plist->pl_nr == 1) /* first page */
764 vio->u.write.vui_from = from;
767 vio->u.write.vui_to = from + copied;
769 /* To address the deadlock in balance_dirty_pages() where
770 * this dirty page may be written back in the same thread. */
771 if (PageDirty(vmpage))
774 /* We may have one full RPC, commit it soon */
775 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
778 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
779 "queued page: %d.\n", plist->pl_nr);
781 cl_page_disown(env, io, page);
783 lcc->lcc_page = NULL;
784 lu_ref_del(&page->cp_reference, "cl_io", io);
785 cl_page_put(env, page);
787 /* page list is not contiguous now, commit it now */
791 file->f_flags & O_SYNC || IS_SYNC(file_inode(file)))
792 result = vvp_io_write_commit(env, io);
795 io->ci_result = result;
796 RETURN(result >= 0 ? copied : result);
799 #ifdef CONFIG_MIGRATION
800 static int ll_migratepage(struct address_space *mapping,
801 struct page *newpage, struct page *page
802 #ifdef HAVE_MIGRATEPAGE_4ARGS
803 , enum migrate_mode mode
807 /* Always fail page migration until we have a proper implementation */
812 const struct address_space_operations ll_aops = {
813 .readpage = ll_readpage,
814 .direct_IO = ll_direct_IO,
815 .writepage = ll_writepage,
816 .writepages = ll_writepages,
817 .set_page_dirty = __set_page_dirty_nobuffers,
818 .write_begin = ll_write_begin,
819 .write_end = ll_write_end,
820 .invalidatepage = ll_invalidatepage,
821 .releasepage = (void *)ll_releasepage,
822 #ifdef CONFIG_MIGRATION
823 .migratepage = ll_migratepage,