1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 #ifndef AUTOCONF_INCLUDED
42 #include <linux/config.h>
44 #include <linux/kernel.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/smp_lock.h>
50 #include <linux/unistd.h>
51 #include <linux/version.h>
52 #include <asm/system.h>
53 #include <asm/uaccess.h>
56 #include <linux/buffer_head.h>
57 #include <linux/writeback.h>
58 #include <linux/stat.h>
59 #include <asm/uaccess.h>
61 #include <linux/pagemap.h>
62 #include <linux/smp_lock.h>
64 #define DEBUG_SUBSYSTEM S_LLITE
66 //#include <lustre_mdc.h>
67 #include <lustre_lite.h>
68 #include "llite_internal.h"
69 #include <linux/lustre_compat25.h>
72 * Implements Linux VM address_space::invalidatepage() method. This method is
73 * called when the page is truncate from a file, either as a result of
74 * explicit truncate, or when inode is removed from memory (as a result of
75 * final iput(), umount, or memory pressure induced icache shrinking).
77 * [0, offset] bytes of the page remain valid (this is for a case of not-page
78 * aligned truncate). Lustre leaves partially truncated page in the cache,
79 * relying on struct inode::i_size to limit further accesses.
81 static int cl_invalidatepage(struct page *vmpage, unsigned long offset)
86 struct cl_object *obj;
91 LASSERT(PageLocked(vmpage));
92 LASSERT(!PageWriteback(vmpage));
95 * It is safe to not check anything in invalidatepage/releasepage
96 * below because they are run with page locked and all our io is
97 * happening with locked page too
101 env = cl_env_get(&refcheck);
103 inode = vmpage->mapping->host;
104 obj = ll_i2info(inode)->lli_clob;
106 page = cl_vmpage_page(vmpage, obj);
108 lu_ref_add(&page->cp_reference,
110 cl_page_delete(env, page);
112 lu_ref_del(&page->cp_reference,
114 cl_page_put(env, page);
117 LASSERT(vmpage->private == 0);
118 cl_env_put(env, &refcheck);
124 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
125 static int ll_invalidatepage(struct page *page, unsigned long offset)
127 return cl_invalidatepage(page, offset);
129 #else /* !HAVE_INVALIDATEPAGE_RETURN_INT */
130 static void ll_invalidatepage(struct page *page, unsigned long offset)
132 cl_invalidatepage(page, offset);
136 #ifdef HAVE_RELEASEPAGE_WITH_INT
137 #define RELEASEPAGE_ARG_TYPE int
139 #define RELEASEPAGE_ARG_TYPE gfp_t
141 static int ll_releasepage(struct page *page, RELEASEPAGE_ARG_TYPE gfp_mask)
145 cookie = cl_env_reenter();
146 ll_invalidatepage(page, 0);
147 cl_env_reexit(cookie);
151 static int ll_set_page_dirty(struct page *vmpage)
154 struct cl_page *page = vvp_vmpage_page_transient(vmpage);
155 struct vvp_object *obj = cl_inode2vvp(vmpage->mapping->host);
156 struct vvp_page *cpg;
159 * XXX should page method be called here?
161 LASSERT(&obj->co_cl == page->cp_obj);
162 cpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
164 * XXX cannot do much here, because page is possibly not locked:
166 * ->unmap_page_range()->zap_pte_range()->set_page_dirty().
168 vvp_write_pending(obj, cpg);
170 RETURN(__set_page_dirty_nobuffers(vmpage));
173 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
175 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
176 size_t size, struct page ***pages)
178 int result = -ENOMEM;
181 /* set an arbitrary limit to prevent arithmetic overflow */
182 if (size > MAX_DIRECTIO_SIZE) {
187 page_count = (user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
188 page_count -= user_addr >> CFS_PAGE_SHIFT;
190 OBD_ALLOC_WAIT(*pages, page_count * sizeof(**pages));
192 down_read(¤t->mm->mmap_sem);
193 result = get_user_pages(current, current->mm, user_addr,
194 page_count, (rw == READ), 0, *pages,
196 up_read(¤t->mm->mmap_sem);
198 OBD_FREE(*pages, page_count * sizeof(**pages));
204 /* ll_free_user_pages - tear down page struct array
205 * @pages: array of page struct pointers underlying target buffer */
206 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
210 for (i = 0; i < npages; i++) {
212 set_page_dirty_lock(pages[i]);
213 page_cache_release(pages[i]);
216 OBD_FREE(pages, npages * sizeof(*pages));
219 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
220 int rw, struct inode *inode,
221 struct ll_dio_pages *pv)
224 struct ccc_page *clup;
225 struct cl_2queue *queue;
226 struct cl_object *obj = io->ci_obj;
227 struct cl_sync_io *anchor = &ccc_env_info(env)->cti_sync_io;
230 loff_t file_offset = pv->ldp_start_offset;
231 size_t size = pv->ldp_size;
232 int page_count = pv->ldp_nr;
233 struct page **pages = pv->ldp_pages;
234 size_t page_size = cl_page_size(obj);
237 cl_sync_io_init(anchor, page_count);
239 queue = &io->ci_queue;
240 cl_2queue_init(queue);
241 for (i = 0; i < page_count; i++) {
243 file_offset = pv->ldp_offsets[i];
244 LASSERT(!(file_offset & (page_size - 1)));
245 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
246 pv->ldp_pages[i], CPT_TRANSIENT);
252 /* check the page type: if the page is a host page, then do
255 * Very rare case that the host pages can be found for
256 * directIO case, since linux kernel truncated all covered
257 * pages before getting here. So, to make the OST happy(to
258 * write a contiguous region), all pages are issued
260 if (clp->cp_type == CPT_CACHEABLE) {
261 cfs_page_t *vmpage = cl_page_vmpage(env, clp);
262 cfs_page_t *src_page;
263 cfs_page_t *dst_page;
267 src_page = (rw == WRITE) ? pages[i] : vmpage;
268 dst_page = (rw == WRITE) ? vmpage : pages[i];
270 src = kmap_atomic(src_page, KM_USER0);
271 dst = kmap_atomic(dst_page, KM_USER1);
272 memcpy(dst, (const void *)src, min(page_size, size));
273 kunmap_atomic(dst, KM_USER1);
274 kunmap_atomic(src, KM_USER0);
276 /* make sure page will be added to the transfer by
277 * cl_io_submit()->...->vvp_page_prep_write(). */
279 set_page_dirty(vmpage);
281 * If direct-io read finds up-to-date page in the
282 * cache, just copy it to the user space. Page will be
283 * filtered out by vvp_page_prep_read(). This
284 * preserves an invariant, that page is read at most
285 * once, see cl_page_flags::CPF_READ_COMPLETED.
289 rc = cl_page_own(env, io, clp);
291 LASSERT(clp->cp_state == CPS_FREEING);
292 cl_page_put(env, clp);
296 clup = cl2ccc_page(cl_page_at(clp, &vvp_device_type));
297 clup->cpg_sync_io = anchor;
298 cl_2queue_add(queue, clp);
300 /* drop the reference count for cl_page_find, so that the page
301 * will be freed in cl_2queue_fini. */
302 cl_page_put(env, clp);
304 * Set page clip to tell transfer formation engine that page
305 * has to be sent even if it is beyond KMS.
307 cl_page_clip(env, clp, 0, min(size, page_size));
309 file_offset += page_size;
313 rc = cl_io_submit_rw(env, io, rw == READ ? CRT_READ : CRT_WRITE,
317 * If some pages weren't sent for any reason (e.g.,
318 * direct-io read found up-to-date pages in the
319 * cache), count them as completed to avoid infinite
322 cl_page_list_for_each(clp, &queue->c2_qin)
323 cl_sync_io_note(anchor, +1);
324 /* wait for the IO to be finished. */
325 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
326 anchor) ?: pv->ldp_size;
330 cl_2queue_discard(env, io, queue);
331 cl_2queue_disown(env, io, queue);
332 cl_2queue_fini(env, queue);
335 EXPORT_SYMBOL(ll_direct_rw_pages);
337 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
338 int rw, struct inode *inode,
339 struct address_space *mapping,
340 size_t size, loff_t file_offset,
341 struct page **pages, int page_count)
343 struct ll_dio_pages pvec = { .ldp_pages = pages,
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 a 128kB
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. This is 22MB for 4kB pages. */
357 #define MAX_DIO_SIZE ((128 * 1024 / sizeof(struct brw_page) * CFS_PAGE_SIZE) & \
358 ~(PTLRPC_MAX_BRW_SIZE - 1))
359 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
360 const struct iovec *iov, loff_t file_offset,
361 unsigned long nr_segs)
365 struct file *file = iocb->ki_filp;
366 struct inode *inode = file->f_mapping->host;
367 struct ccc_object *obj = cl_inode2ccc(inode);
368 ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
369 struct ll_inode_info *lli = ll_i2info(inode);
370 struct lov_stripe_md *lsm = lli->lli_smd;
371 unsigned long seg = 0;
372 size_t size = MAX_DIO_SIZE;
376 if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
379 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
380 if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
383 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max %lu), "
384 "offset=%lld=%llx, pages "LPSZ" (max %lu)\n",
385 inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
386 file_offset, file_offset, count >> CFS_PAGE_SHIFT,
387 MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
389 /* Check that all user buffers are aligned as well */
390 for (seg = 0; seg < nr_segs; seg++) {
391 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
392 (iov[seg].iov_len & ~CFS_PAGE_MASK))
396 env = cl_env_get(&refcheck);
397 LASSERT(!IS_ERR(env));
398 io = ccc_env_io(env)->cui_cl.cis_io;
401 /* 0. Need locking between buffered and direct access. and race with
402 *size changing by concurrent truncates and writes.
403 * 1. Need inode sem to operate transient pages. */
405 LOCK_INODE_MUTEX(inode);
407 LASSERT(obj->cob_transient_pages == 0);
408 for (seg = 0; seg < nr_segs; seg++) {
409 size_t iov_left = iov[seg].iov_len;
410 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
413 if (file_offset >= inode->i_size)
415 if (file_offset + iov_left > inode->i_size)
416 iov_left = inode->i_size - file_offset;
419 while (iov_left > 0) {
424 page_count = ll_get_user_pages(rw, user_addr,
427 LASSERT(page_count != 0);
428 if (page_count > 0) {
429 result = ll_direct_IO_26_seg(env, io, rw, inode,
434 ll_free_user_pages(pages, page_count, rw==READ);
438 if (page_count < 0 || result <= 0) {
439 /* If we can't allocate a large enough buffer
440 * for the request, shrink it to a smaller
441 * PAGE_SIZE multiple and try again.
442 * We should always be able to kmalloc for a
443 * page worth of page pointers = 4MB on i386. */
444 if ((page_count == -ENOMEM||result == -ENOMEM)&&
445 size > (CFS_PAGE_SIZE / sizeof(*pages)) *
447 size = ((((size / 2) - 1) |
448 ~CFS_PAGE_MASK) + 1) &
450 CDEBUG(D_VFSTRACE, "DIO size now %u\n",
456 tot_bytes = page_count < 0 ? page_count : result;
457 GOTO(out, tot_bytes);
461 file_offset += result;
467 LASSERT(obj->cob_transient_pages == 0);
469 UNLOCK_INODE_MUTEX(inode);
473 lov_stripe_lock(lsm);
474 obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
475 lov_stripe_unlock(lsm);
479 cl_env_put(env, &refcheck);
483 struct address_space_operations ll_aops = {
484 .readpage = ll_readpage,
485 // .readpages = ll_readpages,
486 .direct_IO = ll_direct_IO_26,
487 .writepage = ll_writepage,
488 .writepages = generic_writepages,
489 .set_page_dirty = ll_set_page_dirty,
491 .prepare_write = ll_prepare_write,
492 .commit_write = ll_commit_write,
493 .invalidatepage = ll_invalidatepage,
494 .releasepage = (void *)ll_releasepage,