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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lustre/llite/rw26.c
38 * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
41 #include <linux/kernel.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/unistd.h>
47 #include <asm/uaccess.h>
50 #include <linux/migrate.h>
51 #elif defined(HAVE_MIGRATE_MODE_H)
52 #include <linux/migrate_mode.h>
55 #include <linux/buffer_head.h>
56 #include <linux/mpage.h>
57 #include <linux/writeback.h>
58 #include <linux/stat.h>
59 #include <asm/uaccess.h>
61 #include <linux/pagemap.h>
63 #define DEBUG_SUBSYSTEM S_LLITE
65 #include "llite_internal.h"
66 #include <linux/lustre_compat25.h>
69 * Implements Linux VM address_space::invalidatepage() method. This method is
70 * called when the page is truncate from a file, either as a result of
71 * explicit truncate, or when inode is removed from memory (as a result of
72 * final iput(), umount, or memory pressure induced icache shrinking).
74 * [0, offset] bytes of the page remain valid (this is for a case of not-page
75 * aligned truncate). Lustre leaves partially truncated page in the cache,
76 * relying on struct inode::i_size to limit further accesses.
78 static void ll_invalidatepage(struct page *vmpage,
79 #ifdef HAVE_INVALIDATE_RANGE
80 unsigned int offset, unsigned int length
89 struct cl_object *obj;
93 LASSERT(PageLocked(vmpage));
94 LASSERT(!PageWriteback(vmpage));
97 * It is safe to not check anything in invalidatepage/releasepage
98 * below because they are run with page locked and all our io is
99 * happening with locked page too
101 #ifdef HAVE_INVALIDATE_RANGE
102 if (offset == 0 && length == PAGE_CACHE_SIZE) {
106 env = cl_env_get(&refcheck);
108 inode = vmpage->mapping->host;
109 obj = ll_i2info(inode)->lli_clob;
111 page = cl_vmpage_page(vmpage, obj);
113 cl_page_delete(env, page);
114 cl_page_put(env, page);
117 LASSERT(vmpage->private == 0);
118 cl_env_put(env, &refcheck);
123 #ifdef HAVE_RELEASEPAGE_WITH_INT
124 #define RELEASEPAGE_ARG_TYPE int
126 #define RELEASEPAGE_ARG_TYPE gfp_t
128 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
132 struct cl_object *obj;
133 struct cl_page *page;
134 struct address_space *mapping;
137 LASSERT(PageLocked(vmpage));
138 if (PageWriteback(vmpage) || PageDirty(vmpage))
141 mapping = vmpage->mapping;
145 obj = ll_i2info(mapping->host)->lli_clob;
149 /* 1 for caller, 1 for cl_page and 1 for page cache */
150 if (page_count(vmpage) > 3)
153 page = cl_vmpage_page(vmpage, obj);
157 cookie = cl_env_reenter();
158 env = cl_env_percpu_get();
159 LASSERT(!IS_ERR(env));
161 if (!cl_page_in_use(page)) {
163 cl_page_delete(env, page);
166 /* To use percpu env array, the call path can not be rescheduled;
167 * otherwise percpu array will be messed if ll_releaspage() called
168 * again on the same CPU.
170 * If this page holds the last refc of cl_object, the following
171 * call path may cause reschedule:
172 * cl_page_put -> cl_page_free -> cl_object_put ->
173 * lu_object_put -> lu_object_free -> lov_delete_raid0 ->
176 * However, the kernel can't get rid of this inode until all pages have
177 * been cleaned up. Now that we hold page lock here, it's pretty safe
178 * that we won't get into object delete path.
180 LASSERT(cl_object_refc(obj) > 1);
181 cl_page_put(env, page);
183 cl_env_percpu_put(env);
184 cl_env_reexit(cookie);
188 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
190 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
191 size_t size, struct page ***pages,
194 int result = -ENOMEM;
196 /* set an arbitrary limit to prevent arithmetic overflow */
197 if (size > MAX_DIRECTIO_SIZE) {
202 *max_pages = (user_addr + size + PAGE_CACHE_SIZE - 1) >>
204 *max_pages -= user_addr >> PAGE_CACHE_SHIFT;
206 OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
208 down_read(¤t->mm->mmap_sem);
209 result = get_user_pages(current, current->mm, user_addr,
210 *max_pages, (rw == READ), 0, *pages,
212 up_read(¤t->mm->mmap_sem);
213 if (unlikely(result <= 0))
214 OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
220 /* ll_free_user_pages - tear down page struct array
221 * @pages: array of page struct pointers underlying target buffer */
222 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
226 for (i = 0; i < npages; i++) {
227 if (pages[i] == NULL)
230 set_page_dirty_lock(pages[i]);
231 page_cache_release(pages[i]);
234 OBD_FREE_LARGE(pages, npages * sizeof(*pages));
237 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
238 int rw, struct inode *inode,
239 struct ll_dio_pages *pv)
242 struct cl_2queue *queue;
243 struct cl_object *obj = io->ci_obj;
246 loff_t file_offset = pv->ldp_start_offset;
247 long size = pv->ldp_size;
248 int page_count = pv->ldp_nr;
249 struct page **pages = pv->ldp_pages;
250 long page_size = cl_page_size(obj);
255 queue = &io->ci_queue;
256 cl_2queue_init(queue);
257 for (i = 0; i < page_count; i++) {
259 file_offset = pv->ldp_offsets[i];
261 LASSERT(!(file_offset & (page_size - 1)));
262 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
263 pv->ldp_pages[i], CPT_TRANSIENT);
269 rc = cl_page_own(env, io, clp);
271 LASSERT(clp->cp_state == CPS_FREEING);
272 cl_page_put(env, clp);
278 /* check the page type: if the page is a host page, then do
280 if (clp->cp_type == CPT_CACHEABLE) {
281 struct page *vmpage = cl_page_vmpage(clp);
282 struct page *src_page;
283 struct page *dst_page;
287 src_page = (rw == WRITE) ? pages[i] : vmpage;
288 dst_page = (rw == WRITE) ? vmpage : pages[i];
290 src = ll_kmap_atomic(src_page, KM_USER0);
291 dst = ll_kmap_atomic(dst_page, KM_USER1);
292 memcpy(dst, src, min(page_size, size));
293 ll_kunmap_atomic(dst, KM_USER1);
294 ll_kunmap_atomic(src, KM_USER0);
296 /* make sure page will be added to the transfer by
297 * cl_io_submit()->...->vvp_page_prep_write(). */
299 set_page_dirty(vmpage);
302 /* do not issue the page for read, since it
303 * may reread a ra page which has NOT uptodate
305 cl_page_disown(env, io, clp);
311 cl_2queue_add(queue, clp);
314 * Set page clip to tell transfer formation engine
315 * that page has to be sent even if it is beyond KMS.
317 cl_page_clip(env, clp, 0, min(size, page_size));
322 /* drop the reference count for cl_page_find */
323 cl_page_put(env, clp);
325 file_offset += page_size;
328 if (rc == 0 && io_pages) {
329 rc = cl_io_submit_sync(env, io,
330 rw == READ ? CRT_READ : CRT_WRITE,
336 cl_2queue_discard(env, io, queue);
337 cl_2queue_disown(env, io, queue);
338 cl_2queue_fini(env, queue);
341 EXPORT_SYMBOL(ll_direct_rw_pages);
343 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
344 int rw, struct inode *inode,
345 struct address_space *mapping,
346 size_t size, loff_t file_offset,
347 struct page **pages, int page_count)
349 struct ll_dio_pages pvec = { .ldp_pages = pages,
350 .ldp_nr = page_count,
353 .ldp_start_offset = file_offset
356 return ll_direct_rw_pages(env, io, rw, inode, &pvec);
359 #ifdef KMALLOC_MAX_SIZE
360 #define MAX_MALLOC KMALLOC_MAX_SIZE
362 #define MAX_MALLOC (128 * 1024)
365 /* This is the maximum size of a single O_DIRECT request, based on the
366 * kmalloc limit. We need to fit all of the brw_page structs, each one
367 * representing PAGE_SIZE worth of user data, into a single buffer, and
368 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
369 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
370 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_CACHE_SIZE) & \
371 ~(DT_MAX_BRW_SIZE - 1))
372 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
373 const struct iovec *iov, loff_t file_offset,
374 unsigned long nr_segs)
378 struct file *file = iocb->ki_filp;
379 struct inode *inode = file->f_mapping->host;
380 struct ccc_object *obj = cl_inode2ccc(inode);
381 long count = iov_length(iov, nr_segs);
382 long tot_bytes = 0, result = 0;
383 struct ll_inode_info *lli = ll_i2info(inode);
384 unsigned long seg = 0;
385 long size = MAX_DIO_SIZE;
389 if (!lli->lli_has_smd)
392 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
393 if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
396 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%lu (max %lu), "
397 "offset=%lld=%llx, pages %lu (max %lu)\n",
398 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
399 file_offset, file_offset, count >> PAGE_CACHE_SHIFT,
400 MAX_DIO_SIZE >> PAGE_CACHE_SHIFT);
402 /* Check that all user buffers are aligned as well */
403 for (seg = 0; seg < nr_segs; seg++) {
404 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
405 (iov[seg].iov_len & ~CFS_PAGE_MASK))
409 env = cl_env_get(&refcheck);
410 LASSERT(!IS_ERR(env));
411 io = ccc_env_io(env)->cui_cl.cis_io;
414 LASSERT(obj->cob_transient_pages == 0);
415 for (seg = 0; seg < nr_segs; seg++) {
416 long iov_left = iov[seg].iov_len;
417 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
420 if (file_offset >= i_size_read(inode))
422 if (file_offset + iov_left > i_size_read(inode))
423 iov_left = i_size_read(inode) - file_offset;
426 while (iov_left > 0) {
428 int page_count, max_pages = 0;
431 bytes = min(size, iov_left);
432 page_count = ll_get_user_pages(rw, user_addr, bytes,
434 if (likely(page_count > 0)) {
435 if (unlikely(page_count < max_pages))
436 bytes = page_count << PAGE_CACHE_SHIFT;
437 result = ll_direct_IO_26_seg(env, io, rw, inode,
441 ll_free_user_pages(pages, max_pages, rw==READ);
442 } else if (page_count == 0) {
443 GOTO(out, result = -EFAULT);
447 if (unlikely(result <= 0)) {
448 /* If we can't allocate a large enough buffer
449 * for the request, shrink it to a smaller
450 * PAGE_SIZE multiple and try again.
451 * We should always be able to kmalloc for a
452 * page worth of page pointers = 4MB on i386. */
453 if (result == -ENOMEM &&
454 size > (PAGE_CACHE_SIZE / sizeof(*pages)) *
456 size = ((((size / 2) - 1) |
457 ~CFS_PAGE_MASK) + 1) &
459 CDEBUG(D_VFSTRACE,"DIO size now %lu\n",
468 file_offset += result;
474 LASSERT(obj->cob_transient_pages == 0);
477 struct ccc_io *cio = ccc_env_io(env);
479 /* no commit async for direct IO */
480 cio->u.write.cui_written += tot_bytes;
483 cl_env_put(env, &refcheck);
484 RETURN(tot_bytes ? tot_bytes : result);
488 * Prepare partially written-to page for a write.
490 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
493 struct cl_attr *attr = ccc_env_thread_attr(env);
494 struct cl_object *obj = io->ci_obj;
495 struct ccc_page *cp = cl_object_page_slice(obj, pg);
496 loff_t offset = cl_offset(obj, ccc_index(cp));
499 cl_object_attr_lock(obj);
500 result = cl_object_attr_get(env, obj, attr);
501 cl_object_attr_unlock(obj);
504 * If are writing to a new page, no need to read old data.
505 * The extent locking will have updated the KMS, and for our
506 * purposes here we can treat it like i_size.
508 if (attr->cat_kms <= offset) {
509 char *kaddr = ll_kmap_atomic(cp->cpg_page, KM_USER0);
511 memset(kaddr, 0, cl_page_size(obj));
512 ll_kunmap_atomic(kaddr, KM_USER0);
513 } else if (cp->cpg_defer_uptodate)
516 result = ll_page_sync_io(env, io, pg, CRT_READ);
521 static int ll_write_begin(struct file *file, struct address_space *mapping,
522 loff_t pos, unsigned len, unsigned flags,
523 struct page **pagep, void **fsdata)
525 struct ll_cl_context *lcc;
526 const struct lu_env *env;
528 struct cl_page *page;
530 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
531 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
532 struct page *vmpage = NULL;
533 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
534 unsigned to = from + len;
538 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
540 lcc = ll_cl_find(file);
542 GOTO(out, result = -EIO);
547 /* To avoid deadlock, try to lock page first. */
548 vmpage = grab_cache_page_nowait(mapping, index);
549 if (unlikely(vmpage == NULL ||
550 PageDirty(vmpage) || PageWriteback(vmpage))) {
551 struct ccc_io *cio = ccc_env_io(env);
552 struct cl_page_list *plist = &cio->u.write.cui_queue;
554 /* if the page is already in dirty cache, we have to commit
555 * the pages right now; otherwise, it may cause deadlock
556 * because it holds page lock of a dirty page and request for
557 * more grants. It's okay for the dirty page to be the first
558 * one in commit page list, though. */
559 if (vmpage != NULL && plist->pl_nr > 0) {
561 page_cache_release(vmpage);
565 /* commit pages and then wait for page lock */
566 result = vvp_io_write_commit(env, io);
570 if (vmpage == NULL) {
571 vmpage = grab_cache_page_write_begin(mapping, index,
574 GOTO(out, result = -ENOMEM);
578 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
580 GOTO(out, result = PTR_ERR(page));
582 lcc->lcc_page = page;
583 lu_ref_add(&page->cp_reference, "cl_io", io);
585 cl_page_assume(env, io, page);
586 if (!PageUptodate(vmpage)) {
588 * We're completely overwriting an existing page,
589 * so _don't_ set it up to date until commit_write
591 if (from == 0 && to == PAGE_SIZE) {
592 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
593 POISON_PAGE(vmpage, 0x11);
595 /* TODO: can be optimized at OSC layer to check if it
596 * is a lockless IO. In that case, it's not necessary
597 * to read the data. */
598 result = ll_prepare_partial_page(env, io, page);
600 SetPageUptodate(vmpage);
604 cl_page_unassume(env, io, page);
608 if (vmpage != NULL) {
610 page_cache_release(vmpage);
619 static int ll_write_end(struct file *file, struct address_space *mapping,
620 loff_t pos, unsigned len, unsigned copied,
621 struct page *vmpage, void *fsdata)
623 struct ll_cl_context *lcc = fsdata;
624 const struct lu_env *env;
627 struct cl_page *page;
628 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
633 page_cache_release(vmpage);
635 LASSERT(lcc != NULL);
637 page = lcc->lcc_page;
639 cio = ccc_env_io(env);
641 LASSERT(cl_page_is_owned(page, io));
643 struct cl_page_list *plist = &cio->u.write.cui_queue;
645 lcc->lcc_page = NULL; /* page will be queued */
647 /* Add it into write queue */
648 cl_page_list_add(plist, page);
649 if (plist->pl_nr == 1) /* first page */
650 cio->u.write.cui_from = from;
653 cio->u.write.cui_to = from + copied;
655 /* To address the deadlock in balance_dirty_pages() where
656 * this dirty page may be written back in the same thread. */
657 if (PageDirty(vmpage))
660 /* We may have one full RPC, commit it soon */
661 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
664 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
665 "queued page: %d.\n", plist->pl_nr);
667 cl_page_disown(env, io, page);
669 lcc->lcc_page = NULL;
670 lu_ref_del(&page->cp_reference, "cl_io", io);
671 cl_page_put(env, page);
673 /* page list is not contiguous now, commit it now */
677 file->f_flags & O_SYNC || IS_SYNC(file->f_dentry->d_inode))
678 result = vvp_io_write_commit(env, io);
680 RETURN(result >= 0 ? copied : result);
683 #ifdef CONFIG_MIGRATION
684 static int ll_migratepage(struct address_space *mapping,
685 struct page *newpage, struct page *page
686 #ifdef HAVE_MIGRATEPAGE_4ARGS
687 , enum migrate_mode mode
691 /* Always fail page migration until we have a proper implementation */
696 #ifndef MS_HAS_NEW_AOPS
697 const struct address_space_operations ll_aops = {
698 .readpage = ll_readpage,
699 .direct_IO = ll_direct_IO_26,
700 .writepage = ll_writepage,
701 .writepages = ll_writepages,
702 .set_page_dirty = __set_page_dirty_nobuffers,
703 .write_begin = ll_write_begin,
704 .write_end = ll_write_end,
705 .invalidatepage = ll_invalidatepage,
706 .releasepage = (void *)ll_releasepage,
707 #ifdef CONFIG_MIGRATION
708 .migratepage = ll_migratepage,
712 const struct address_space_operations_ext ll_aops = {
713 .orig_aops.readpage = ll_readpage,
714 .orig_aops.direct_IO = ll_direct_IO_26,
715 .orig_aops.writepage = ll_writepage,
716 .orig_aops.writepages = ll_writepages,
717 .orig_aops.set_page_dirty = __set_page_dirty_nobuffers,
718 .orig_aops.invalidatepage = ll_invalidatepage,
719 .orig_aops.releasepage = ll_releasepage,
720 #ifdef CONFIG_MIGRATION
721 .orig_aops.migratepage = ll_migratepage,
723 .write_begin = ll_write_begin,
724 .write_end = ll_write_end