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 <linux/uaccess.h>
49 #include <linux/migrate.h>
51 #include <linux/buffer_head.h>
52 #include <linux/mpage.h>
53 #include <linux/writeback.h>
54 #include <linux/pagemap.h>
56 #define DEBUG_SUBSYSTEM S_LLITE
58 #include "../include/lustre_lite.h"
59 #include "llite_internal.h"
60 #include "../include/linux/lustre_compat25.h"
63 * Implements Linux VM address_space::invalidatepage() method. This method is
64 * called when the page is truncate from a file, either as a result of
65 * explicit truncate, or when inode is removed from memory (as a result of
66 * final iput(), umount, or memory pressure induced icache shrinking).
68 * [0, offset] bytes of the page remain valid (this is for a case of not-page
69 * aligned truncate). Lustre leaves partially truncated page in the cache,
70 * relying on struct inode::i_size to limit further accesses.
72 static void ll_invalidatepage(struct page *vmpage, unsigned int offset,
78 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 if (offset == 0 && length == PAGE_CACHE_SIZE) {
91 env = cl_env_get(&refcheck);
93 inode = vmpage->mapping->host;
94 obj = ll_i2info(inode)->lli_clob;
96 page = cl_vmpage_page(vmpage, obj);
98 lu_ref_add(&page->cp_reference,
100 cl_page_delete(env, page);
101 lu_ref_del(&page->cp_reference,
103 cl_page_put(env, page);
106 LASSERT(vmpage->private == 0);
107 cl_env_put(env, &refcheck);
112 static int ll_releasepage(struct page *vmpage, gfp_t gfp_mask)
114 struct cl_env_nest nest;
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 /* 1 for page allocator, 1 for cl_page and 1 for page cache */
134 if (page_count(vmpage) > 3)
137 /* TODO: determine what gfp should be used by @gfp_mask. */
138 env = cl_env_nested_get(&nest);
140 /* If we can't allocate an env we won't call cl_page_put()
141 * later on which further means it's impossible to drop
142 * page refcount by cl_page, so ask kernel to not free
147 page = cl_vmpage_page(vmpage, obj);
150 if (!cl_page_in_use(page)) {
152 cl_page_delete(env, page);
154 cl_page_put(env, page);
156 cl_env_nested_put(&nest, env);
160 static int ll_set_page_dirty(struct page *vmpage)
163 struct cl_page *page = vvp_vmpage_page_transient(vmpage);
164 struct vvp_object *obj = cl_inode2vvp(vmpage->mapping->host);
165 struct vvp_page *cpg;
168 * XXX should page method be called here?
170 LASSERT(&obj->co_cl == page->cp_obj);
171 cpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
173 * XXX cannot do much here, because page is possibly not locked:
175 * ->unmap_page_range()->zap_pte_range()->set_page_dirty().
177 vvp_write_pending(obj, cpg);
179 return __set_page_dirty_nobuffers(vmpage);
182 #define MAX_DIRECTIO_SIZE (2*1024*1024*1024UL)
184 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
185 size_t size, struct page ***pages,
188 int result = -ENOMEM;
190 /* set an arbitrary limit to prevent arithmetic overflow */
191 if (size > MAX_DIRECTIO_SIZE) {
196 *max_pages = (user_addr + size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
197 *max_pages -= user_addr >> PAGE_CACHE_SHIFT;
199 *pages = libcfs_kvzalloc(*max_pages * sizeof(**pages), GFP_NOFS);
201 result = get_user_pages_fast(user_addr, *max_pages,
202 (rw == READ), *pages);
203 if (unlikely(result <= 0))
210 /* ll_free_user_pages - tear down page struct array
211 * @pages: array of page struct pointers underlying target buffer
213 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
217 for (i = 0; i < npages; i++) {
219 set_page_dirty_lock(pages[i]);
220 page_cache_release(pages[i]);
225 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
226 int rw, struct inode *inode,
227 struct ll_dio_pages *pv)
230 struct cl_2queue *queue;
231 struct cl_object *obj = io->ci_obj;
234 loff_t file_offset = pv->ldp_start_offset;
235 long size = pv->ldp_size;
236 int page_count = pv->ldp_nr;
237 struct page **pages = pv->ldp_pages;
238 long page_size = cl_page_size(obj);
242 queue = &io->ci_queue;
243 cl_2queue_init(queue);
244 for (i = 0; i < page_count; i++) {
246 file_offset = pv->ldp_offsets[i];
248 LASSERT(!(file_offset & (page_size - 1)));
249 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
250 pv->ldp_pages[i], CPT_TRANSIENT);
256 rc = cl_page_own(env, io, clp);
258 LASSERT(clp->cp_state == CPS_FREEING);
259 cl_page_put(env, clp);
265 /* check the page type: if the page is a host page, then do
268 if (clp->cp_type == CPT_CACHEABLE) {
269 struct page *vmpage = cl_page_vmpage(env, clp);
270 struct page *src_page;
271 struct page *dst_page;
275 src_page = (rw == WRITE) ? pages[i] : vmpage;
276 dst_page = (rw == WRITE) ? vmpage : pages[i];
278 src = kmap_atomic(src_page);
279 dst = kmap_atomic(dst_page);
280 memcpy(dst, src, min(page_size, size));
284 /* make sure page will be added to the transfer by
285 * cl_io_submit()->...->vvp_page_prep_write().
288 set_page_dirty(vmpage);
291 /* do not issue the page for read, since it
292 * may reread a ra page which has NOT uptodate
295 cl_page_disown(env, io, clp);
302 * Add a page to the incoming page list of 2-queue.
304 cl_page_list_add(&queue->c2_qin, clp);
307 * Set page clip to tell transfer formation engine
308 * that page has to be sent even if it is beyond KMS.
310 cl_page_clip(env, clp, 0, min(size, page_size));
315 /* drop the reference count for cl_page_find */
316 cl_page_put(env, clp);
318 file_offset += page_size;
321 if (rc == 0 && io_pages) {
322 rc = cl_io_submit_sync(env, io,
323 rw == READ ? CRT_READ : CRT_WRITE,
329 cl_2queue_discard(env, io, queue);
330 cl_2queue_disown(env, io, queue);
331 cl_2queue_fini(env, queue);
334 EXPORT_SYMBOL(ll_direct_rw_pages);
336 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
337 int rw, struct inode *inode,
338 struct address_space *mapping,
339 size_t size, loff_t file_offset,
340 struct page **pages, int page_count)
342 struct ll_dio_pages pvec = {
344 .ldp_nr = page_count,
347 .ldp_start_offset = file_offset
350 return ll_direct_rw_pages(env, io, rw, inode, &pvec);
353 /* This is the maximum size of a single O_DIRECT request, based on the
354 * kmalloc limit. We need to fit all of the brw_page structs, each one
355 * representing PAGE_SIZE worth of user data, into a single buffer, and
356 * then truncate this to be a full-sized RPC. For 4kB PAGE_SIZE this is
357 * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc.
359 #define MAX_DIO_SIZE ((KMALLOC_MAX_SIZE / sizeof(struct brw_page) * \
360 PAGE_CACHE_SIZE) & ~(DT_MAX_BRW_SIZE - 1))
361 static ssize_t ll_direct_IO_26(struct kiocb *iocb, struct iov_iter *iter,
366 struct file *file = iocb->ki_filp;
367 struct inode *inode = file->f_mapping->host;
368 struct ccc_object *obj = cl_inode2ccc(inode);
369 ssize_t count = iov_iter_count(iter);
370 ssize_t tot_bytes = 0, result = 0;
371 struct ll_inode_info *lli = ll_i2info(inode);
372 long size = MAX_DIO_SIZE;
375 if (!lli->lli_has_smd)
378 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
379 if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
383 "VFS Op:inode=%lu/%u(%p), size=%zd (max %lu), offset=%lld=%llx, pages %zd (max %lu)\n",
384 inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
385 file_offset, file_offset, count >> PAGE_CACHE_SHIFT,
386 MAX_DIO_SIZE >> PAGE_CACHE_SHIFT);
388 /* Check that all user buffers are aligned as well */
389 if (iov_iter_alignment(iter) & ~CFS_PAGE_MASK)
392 env = cl_env_get(&refcheck);
393 LASSERT(!IS_ERR(env));
394 io = ccc_env_io(env)->cui_cl.cis_io;
397 /* 0. Need locking between buffered and direct access. and race with
398 * size changing by concurrent truncates and writes.
399 * 1. Need inode mutex to operate transient pages.
401 if (iov_iter_rw(iter) == READ)
404 LASSERT(obj->cob_transient_pages == 0);
405 while (iov_iter_count(iter)) {
409 count = min_t(size_t, iov_iter_count(iter), size);
410 if (iov_iter_rw(iter) == READ) {
411 if (file_offset >= i_size_read(inode))
413 if (file_offset + count > i_size_read(inode))
414 count = i_size_read(inode) - file_offset;
417 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
418 if (likely(result > 0)) {
419 int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
421 result = ll_direct_IO_26_seg(env, io, iov_iter_rw(iter),
422 inode, file->f_mapping,
423 result, file_offset, pages,
425 ll_free_user_pages(pages, n, iov_iter_rw(iter) == READ);
427 if (unlikely(result <= 0)) {
428 /* If we can't allocate a large enough buffer
429 * for the request, shrink it to a smaller
430 * PAGE_SIZE multiple and try again.
431 * We should always be able to kmalloc for a
432 * page worth of page pointers = 4MB on i386.
434 if (result == -ENOMEM &&
435 size > (PAGE_CACHE_SIZE / sizeof(*pages)) *
437 size = ((((size / 2) - 1) |
438 ~CFS_PAGE_MASK) + 1) &
440 CDEBUG(D_VFSTRACE, "DIO size now %lu\n",
447 iov_iter_advance(iter, result);
449 file_offset += result;
452 LASSERT(obj->cob_transient_pages == 0);
453 if (iov_iter_rw(iter) == READ)
457 if (iov_iter_rw(iter) == WRITE) {
458 struct lov_stripe_md *lsm;
460 lsm = ccc_inode_lsm_get(inode);
462 lov_stripe_lock(lsm);
463 obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
464 lov_stripe_unlock(lsm);
465 ccc_inode_lsm_put(inode, lsm);
469 cl_env_put(env, &refcheck);
470 return tot_bytes ? : result;
473 static int ll_write_begin(struct file *file, struct address_space *mapping,
474 loff_t pos, unsigned len, unsigned flags,
475 struct page **pagep, void **fsdata)
477 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
480 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
482 page = grab_cache_page_write_begin(mapping, index, flags);
488 rc = ll_prepare_write(file, page, from, from + len);
491 page_cache_release(page);
496 static int ll_write_end(struct file *file, struct address_space *mapping,
497 loff_t pos, unsigned len, unsigned copied,
498 struct page *page, void *fsdata)
500 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
503 rc = ll_commit_write(file, page, from, from + copied);
505 page_cache_release(page);
510 #ifdef CONFIG_MIGRATION
511 static int ll_migratepage(struct address_space *mapping,
512 struct page *newpage, struct page *page,
513 enum migrate_mode mode
516 /* Always fail page migration until we have a proper implementation */
521 const struct address_space_operations ll_aops = {
522 .readpage = ll_readpage,
523 .direct_IO = ll_direct_IO_26,
524 .writepage = ll_writepage,
525 .writepages = ll_writepages,
526 .set_page_dirty = ll_set_page_dirty,
527 .write_begin = ll_write_begin,
528 .write_end = ll_write_end,
529 .invalidatepage = ll_invalidatepage,
530 .releasepage = (void *)ll_releasepage,
531 #ifdef CONFIG_MIGRATION
532 .migratepage = ll_migratepage,