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, 2012, 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 <linux/uaccess.h>
45 #include <linux/migrate.h>
47 #include <linux/buffer_head.h>
48 #include <linux/mpage.h>
49 #include <linux/writeback.h>
50 #include <linux/pagemap.h>
52 #define DEBUG_SUBSYSTEM S_LLITE
54 #include "llite_internal.h"
57 * Implements Linux VM address_space::invalidatepage() method. This method is
58 * called when the page is truncate from a file, either as a result of
59 * explicit truncate, or when inode is removed from memory (as a result of
60 * final iput(), umount, or memory pressure induced icache shrinking).
62 * [0, offset] bytes of the page remain valid (this is for a case of not-page
63 * aligned truncate). Lustre leaves partially truncated page in the cache,
64 * relying on struct inode::i_size to limit further accesses.
66 static void ll_invalidatepage(struct page *vmpage, unsigned int offset,
72 struct cl_object *obj;
74 LASSERT(PageLocked(vmpage));
75 LASSERT(!PageWriteback(vmpage));
78 * It is safe to not check anything in invalidatepage/releasepage
79 * below because they are run with page locked and all our io is
80 * happening with locked page too
82 if (offset == 0 && length == PAGE_SIZE) {
83 /* See the comment in ll_releasepage() */
84 env = cl_env_percpu_get();
85 LASSERT(!IS_ERR(env));
86 inode = vmpage->mapping->host;
87 obj = ll_i2info(inode)->lli_clob;
89 page = cl_vmpage_page(vmpage, obj);
91 cl_page_delete(env, page);
92 cl_page_put(env, page);
95 LASSERT(vmpage->private == 0);
97 cl_env_percpu_put(env);
101 static int ll_releasepage(struct page *vmpage, gfp_t gfp_mask)
104 struct cl_object *obj;
105 struct cl_page *page;
106 struct address_space *mapping;
109 LASSERT(PageLocked(vmpage));
110 if (PageWriteback(vmpage) || PageDirty(vmpage))
113 mapping = vmpage->mapping;
117 obj = ll_i2info(mapping->host)->lli_clob;
121 /* 1 for caller, 1 for cl_page and 1 for page cache */
122 if (page_count(vmpage) > 3)
125 page = cl_vmpage_page(vmpage, obj);
129 env = cl_env_percpu_get();
130 LASSERT(!IS_ERR(env));
132 if (!cl_page_in_use(page)) {
134 cl_page_delete(env, page);
137 /* To use percpu env array, the call path can not be rescheduled;
138 * otherwise percpu array will be messed if ll_releaspage() called
139 * again on the same CPU.
141 * If this page holds the last refc of cl_object, the following
142 * call path may cause reschedule:
143 * cl_page_put -> cl_page_free -> cl_object_put ->
144 * lu_object_put -> lu_object_free -> lov_delete_raid0.
146 * However, the kernel can't get rid of this inode until all pages have
147 * been cleaned up. Now that we hold page lock here, it's pretty safe
148 * that we won't get into object delete path.
150 LASSERT(cl_object_refc(obj) > 1);
151 cl_page_put(env, page);
153 cl_env_percpu_put(env);
157 #define MAX_DIRECTIO_SIZE (2 * 1024 * 1024 * 1024UL)
159 /* ll_free_user_pages - tear down page struct array
160 * @pages: array of page struct pointers underlying target buffer
162 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
166 for (i = 0; i < npages; i++) {
168 set_page_dirty_lock(pages[i]);
174 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
175 int rw, struct inode *inode,
176 struct ll_dio_pages *pv)
179 struct cl_2queue *queue;
180 struct cl_object *obj = io->ci_obj;
183 loff_t file_offset = pv->ldp_start_offset;
184 size_t size = pv->ldp_size;
185 int page_count = pv->ldp_nr;
186 struct page **pages = pv->ldp_pages;
187 size_t page_size = cl_page_size(obj);
191 queue = &io->ci_queue;
192 cl_2queue_init(queue);
193 for (i = 0; i < page_count; i++) {
195 file_offset = pv->ldp_offsets[i];
197 LASSERT(!(file_offset & (page_size - 1)));
198 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
199 pv->ldp_pages[i], CPT_TRANSIENT);
205 rc = cl_page_own(env, io, clp);
207 LASSERT(clp->cp_state == CPS_FREEING);
208 cl_page_put(env, clp);
214 /* check the page type: if the page is a host page, then do
217 if (clp->cp_type == CPT_CACHEABLE) {
218 struct page *vmpage = cl_page_vmpage(clp);
219 struct page *src_page;
220 struct page *dst_page;
224 src_page = (rw == WRITE) ? pages[i] : vmpage;
225 dst_page = (rw == WRITE) ? vmpage : pages[i];
227 src = kmap_atomic(src_page);
228 dst = kmap_atomic(dst_page);
229 memcpy(dst, src, min(page_size, size));
233 /* make sure page will be added to the transfer by
234 * cl_io_submit()->...->vvp_page_prep_write().
237 set_page_dirty(vmpage);
240 /* do not issue the page for read, since it
241 * may reread a ra page which has NOT uptodate
244 cl_page_disown(env, io, clp);
251 * Add a page to the incoming page list of 2-queue.
253 cl_page_list_add(&queue->c2_qin, clp);
256 * Set page clip to tell transfer formation engine
257 * that page has to be sent even if it is beyond KMS.
259 cl_page_clip(env, clp, 0, min(size, page_size));
264 /* drop the reference count for cl_page_find */
265 cl_page_put(env, clp);
267 file_offset += page_size;
270 if (rc == 0 && io_pages) {
271 rc = cl_io_submit_sync(env, io,
272 rw == READ ? CRT_READ : CRT_WRITE,
278 cl_2queue_discard(env, io, queue);
279 cl_2queue_disown(env, io, queue);
280 cl_2queue_fini(env, queue);
283 EXPORT_SYMBOL(ll_direct_rw_pages);
285 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
286 int rw, struct inode *inode,
287 struct address_space *mapping,
288 size_t size, loff_t file_offset,
289 struct page **pages, int page_count)
291 struct ll_dio_pages pvec = {
293 .ldp_nr = page_count,
296 .ldp_start_offset = file_offset
299 return ll_direct_rw_pages(env, io, rw, inode, &pvec);
302 /* This is the maximum size of a single O_DIRECT request, based on the
303 * kmalloc limit. We need to fit all of the brw_page structs, each one
304 * representing PAGE_SIZE worth of user data, into a single buffer, and
305 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
306 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc.
308 #define MAX_DIO_SIZE ((KMALLOC_MAX_SIZE / sizeof(struct brw_page) * \
309 PAGE_SIZE) & ~(DT_MAX_BRW_SIZE - 1))
310 static ssize_t ll_direct_IO_26(struct kiocb *iocb, struct iov_iter *iter)
312 struct ll_cl_context *lcc;
313 const struct lu_env *env;
315 struct file *file = iocb->ki_filp;
316 struct inode *inode = file->f_mapping->host;
317 loff_t file_offset = iocb->ki_pos;
318 ssize_t count = iov_iter_count(iter);
319 ssize_t tot_bytes = 0, result = 0;
320 long size = MAX_DIO_SIZE;
322 /* Check EOF by ourselves */
323 if (iov_iter_rw(iter) == READ && file_offset >= i_size_read(inode))
326 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
327 if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
330 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), size=%zd (max %lu), offset=%lld=%llx, pages %zd (max %lu)\n",
331 PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
332 file_offset, file_offset, count >> PAGE_SHIFT,
333 MAX_DIO_SIZE >> PAGE_SHIFT);
335 /* Check that all user buffers are aligned as well */
336 if (iov_iter_alignment(iter) & ~PAGE_MASK)
339 lcc = ll_cl_find(file);
344 LASSERT(!IS_ERR(env));
348 while (iov_iter_count(iter)) {
352 count = min_t(size_t, iov_iter_count(iter), size);
353 if (iov_iter_rw(iter) == READ) {
354 if (file_offset >= i_size_read(inode))
356 if (file_offset + count > i_size_read(inode))
357 count = i_size_read(inode) - file_offset;
360 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
361 if (likely(result > 0)) {
362 int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
364 result = ll_direct_IO_26_seg(env, io, iov_iter_rw(iter),
365 inode, file->f_mapping,
366 result, file_offset, pages,
368 ll_free_user_pages(pages, n, iov_iter_rw(iter) == READ);
370 if (unlikely(result <= 0)) {
371 /* If we can't allocate a large enough buffer
372 * for the request, shrink it to a smaller
373 * PAGE_SIZE multiple and try again.
374 * We should always be able to kmalloc for a
375 * page worth of page pointers = 4MB on i386.
377 if (result == -ENOMEM &&
378 size > (PAGE_SIZE / sizeof(*pages)) *
380 size = ((((size / 2) - 1) |
383 CDEBUG(D_VFSTRACE, "DIO size now %lu\n",
390 iov_iter_advance(iter, result);
392 file_offset += result;
396 struct vvp_io *vio = vvp_env_io(env);
398 /* no commit async for direct IO */
399 vio->u.write.vui_written += tot_bytes;
402 return tot_bytes ? tot_bytes : result;
406 * Prepare partially written-to page for a write.
408 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
411 struct cl_attr *attr = vvp_env_thread_attr(env);
412 struct cl_object *obj = io->ci_obj;
413 struct vvp_page *vpg = cl_object_page_slice(obj, pg);
414 loff_t offset = cl_offset(obj, vvp_index(vpg));
417 cl_object_attr_lock(obj);
418 result = cl_object_attr_get(env, obj, attr);
419 cl_object_attr_unlock(obj);
422 * If are writing to a new page, no need to read old data.
423 * The extent locking will have updated the KMS, and for our
424 * purposes here we can treat it like i_size.
426 if (attr->cat_kms <= offset) {
427 char *kaddr = kmap_atomic(vpg->vpg_page);
429 memset(kaddr, 0, cl_page_size(obj));
430 kunmap_atomic(kaddr);
431 } else if (vpg->vpg_defer_uptodate) {
432 vpg->vpg_ra_used = 1;
434 result = ll_page_sync_io(env, io, pg, CRT_READ);
440 static int ll_write_begin(struct file *file, struct address_space *mapping,
441 loff_t pos, unsigned int len, unsigned int flags,
442 struct page **pagep, void **fsdata)
444 struct ll_cl_context *lcc;
445 const struct lu_env *env = NULL;
447 struct cl_page *page = NULL;
448 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
449 pgoff_t index = pos >> PAGE_SHIFT;
450 struct page *vmpage = NULL;
451 unsigned int from = pos & (PAGE_SIZE - 1);
452 unsigned int to = from + len;
455 CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
457 lcc = ll_cl_find(file);
467 /* To avoid deadlock, try to lock page first. */
468 vmpage = grab_cache_page_nowait(mapping, index);
469 if (unlikely(!vmpage || PageDirty(vmpage) || PageWriteback(vmpage))) {
470 struct vvp_io *vio = vvp_env_io(env);
471 struct cl_page_list *plist = &vio->u.write.vui_queue;
473 /* if the page is already in dirty cache, we have to commit
474 * the pages right now; otherwise, it may cause deadlock
475 * because it holds page lock of a dirty page and request for
476 * more grants. It's okay for the dirty page to be the first
477 * one in commit page list, though.
479 if (vmpage && plist->pl_nr > 0) {
485 /* commit pages and then wait for page lock */
486 result = vvp_io_write_commit(env, io);
491 vmpage = grab_cache_page_write_begin(mapping, index,
500 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
502 result = PTR_ERR(page);
506 lcc->lcc_page = page;
507 lu_ref_add(&page->cp_reference, "cl_io", io);
509 cl_page_assume(env, io, page);
510 if (!PageUptodate(vmpage)) {
512 * We're completely overwriting an existing page,
513 * so _don't_ set it up to date until commit_write
515 if (from == 0 && to == PAGE_SIZE) {
516 CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
517 POISON_PAGE(vmpage, 0x11);
519 /* TODO: can be optimized at OSC layer to check if it
520 * is a lockless IO. In that case, it's not necessary
523 result = ll_prepare_partial_page(env, io, page);
525 SetPageUptodate(vmpage);
529 cl_page_unassume(env, io, page);
536 if (!IS_ERR_OR_NULL(page)) {
537 lu_ref_del(&page->cp_reference, "cl_io", io);
538 cl_page_put(env, page);
541 io->ci_result = result;
549 static int ll_write_end(struct file *file, struct address_space *mapping,
550 loff_t pos, unsigned int len, unsigned int copied,
551 struct page *vmpage, void *fsdata)
553 struct ll_cl_context *lcc = fsdata;
554 const struct lu_env *env;
557 struct cl_page *page;
558 unsigned int from = pos & (PAGE_SIZE - 1);
565 page = lcc->lcc_page;
567 vio = vvp_env_io(env);
569 LASSERT(cl_page_is_owned(page, io));
571 struct cl_page_list *plist = &vio->u.write.vui_queue;
573 lcc->lcc_page = NULL; /* page will be queued */
575 /* Add it into write queue */
576 cl_page_list_add(plist, page);
577 if (plist->pl_nr == 1) /* first page */
578 vio->u.write.vui_from = from;
581 vio->u.write.vui_to = from + copied;
584 * To address the deadlock in balance_dirty_pages() where
585 * this dirty page may be written back in the same thread.
587 if (PageDirty(vmpage))
590 /* We may have one full RPC, commit it soon */
591 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
594 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
595 "queued page: %d.\n", plist->pl_nr);
597 cl_page_disown(env, io, page);
599 lcc->lcc_page = NULL;
600 lu_ref_del(&page->cp_reference, "cl_io", io);
601 cl_page_put(env, page);
603 /* page list is not contiguous now, commit it now */
608 file->f_flags & O_SYNC || IS_SYNC(file_inode(file)))
609 result = vvp_io_write_commit(env, io);
612 io->ci_result = result;
613 return result >= 0 ? copied : result;
616 #ifdef CONFIG_MIGRATION
617 static int ll_migratepage(struct address_space *mapping,
618 struct page *newpage, struct page *page,
619 enum migrate_mode mode
622 /* Always fail page migration until we have a proper implementation */
627 const struct address_space_operations ll_aops = {
628 .readpage = ll_readpage,
629 .direct_IO = ll_direct_IO_26,
630 .writepage = ll_writepage,
631 .writepages = ll_writepages,
632 .set_page_dirty = __set_page_dirty_nobuffers,
633 .write_begin = ll_write_begin,
634 .write_end = ll_write_end,
635 .invalidatepage = ll_invalidatepage,
636 .releasepage = (void *)ll_releasepage,
637 #ifdef CONFIG_MIGRATION
638 .migratepage = ll_migratepage,