1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifdef HAVE_KERNEL_CONFIG_H
24 #include <linux/config.h>
26 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
38 #include <linux/buffer_head.h>
39 #include <linux/mpage.h>
40 #include <linux/writeback.h>
41 #include <linux/stat.h>
42 #include <asm/uaccess.h>
43 #include <asm/segment.h>
45 #include <linux/pagemap.h>
46 #include <linux/smp_lock.h>
48 #define DEBUG_SUBSYSTEM S_LLITE
50 #include <lustre_lite.h>
51 #include "llite_internal.h"
52 #include <linux/lustre_compat25.h>
54 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
56 return ll_writepage(page);
59 /* It is safe to not check anything in invalidatepage/releasepage below
60 because they are run with page locked and all our io is happening with
62 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
63 static int ll_invalidatepage(struct page *page, unsigned long offset)
67 if (PagePrivate(page))
72 static void ll_invalidatepage(struct page *page, unsigned long offset)
76 if (PagePrivate(page))
81 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
83 if (PagePrivate(page))
88 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
90 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
91 size_t size, struct page ***pages)
94 unsigned long page_count;
96 /* set an arbitrary limit to prevent arithmetic overflow */
97 if (size > MAX_DIRECTIO_SIZE) {
102 page_count = (user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
103 page_count -= user_addr >> CFS_PAGE_SHIFT;
105 OBD_ALLOC_GFP(*pages, page_count * sizeof(**pages), GFP_KERNEL);
107 down_read(¤t->mm->mmap_sem);
108 result = get_user_pages(current, current->mm, user_addr,
109 page_count, (rw == READ), 0, *pages,
111 up_read(¤t->mm->mmap_sem);
117 /* ll_free_user_pages - tear down page struct array
118 * @pages: array of page struct pointers underlying target buffer */
119 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
123 for (i = 0; i < npages; i++) {
125 set_page_dirty_lock(pages[i]);
126 page_cache_release(pages[i]);
129 OBD_FREE(pages, npages * sizeof(*pages));
132 static ssize_t ll_direct_IO_26_seg(int rw, struct file *file,
133 struct address_space *mapping,
135 struct lov_stripe_md *lsm,
136 unsigned long user_addr, size_t size,
137 loff_t file_offset, struct page **pages,
140 struct brw_page *pga;
146 OBD_ALLOC(pga, sizeof(*pga) * page_count);
150 for (i = 0, length = size; length > 0;
151 length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
152 pga[i].pg = pages[i];
153 pga[i].off = file_offset;
154 /* To the end of the page, or the length, whatever is less */
155 pga[i].count = min_t(int, CFS_PAGE_SIZE -(file_offset & ~CFS_PAGE_MASK),
159 POISON_PAGE(pages[i], 0x0d);
162 ll_inode_fill_obdo(inode, rw, &oa);
165 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
166 LPROC_LL_DIRECT_WRITE, size);
168 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
169 LPROC_LL_DIRECT_READ, size);
170 rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
171 ll_i2obdexp(inode), &oa, lsm, page_count, pga, NULL);
175 lov_stripe_lock(lsm);
176 obd_adjust_kms(ll_i2obdexp(inode), lsm, file_offset, 0);
177 lov_stripe_unlock(lsm);
181 OBD_FREE(pga, sizeof(*pga) * page_count);
185 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
186 const struct iovec *iov, loff_t file_offset,
187 unsigned long nr_segs)
189 struct file *file = iocb->ki_filp;
190 ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
191 struct ll_inode_info *lli = ll_i2info(file->f_mapping->host);
195 if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
198 /* FIXME: io smaller than CFS_PAGE_SIZE is broken on ia64 ??? */
199 if ((file_offset & (~CFS_PAGE_MASK)) || (count & ~CFS_PAGE_MASK))
202 /* Check that all user buffers are aligned as well */
203 for (seg = 0; seg < nr_segs; seg++) {
204 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
205 size_t size = iov[seg].iov_len;
206 if ((user_addr & ~CFS_PAGE_MASK) || (size & ~CFS_PAGE_MASK))
211 while ((seg < nr_segs) && (tot_bytes >= 0)) {
212 const struct iovec *vec = &iov[seg++];
213 unsigned long user_addr = (unsigned long)vec->iov_base;
214 size_t size = vec->iov_len;
219 page_count = ll_get_user_pages(rw, user_addr, size, &pages);
220 if (page_count < 0) {
221 ll_free_user_pages(pages, 0, 0);
227 result = ll_direct_IO_26_seg(rw, file, file->f_mapping,
228 file->f_mapping->host,
229 lli->lli_smd, user_addr, size,
230 file_offset, pages, page_count);
231 ll_free_user_pages(pages, page_count, rw == READ);
240 file_offset += result;
247 struct address_space_operations ll_aops = {
248 .readpage = ll_readpage,
249 // .readpages = ll_readpages,
250 .direct_IO = ll_direct_IO_26,
251 .writepage = ll_writepage_26,
252 .writepages = generic_writepages,
253 .set_page_dirty = __set_page_dirty_nobuffers,
255 .prepare_write = ll_prepare_write,
256 .commit_write = ll_commit_write,
257 .invalidatepage = ll_invalidatepage,
258 .releasepage = ll_releasepage,
262 static int wait_on_page_locked_range(struct address_space *mapping,
263 pgoff_t start, pgoff_t end)
272 for(index = start; index < end; index++) {
273 page = find_get_page(mapping, index);
277 wait_on_page_locked(page);
283 /* Check for outstanding write errors */
284 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
286 if (test_and_clear_bit(AS_EIO, &mapping->flags))
292 int ll_sync_page_range(struct inode *inode, struct address_space *mapping,
293 loff_t pos, size_t count)
295 pgoff_t start = pos >> CFS_PAGE_SHIFT;
296 pgoff_t end = (pos + count - 1) >> CFS_PAGE_SHIFT;
297 struct writeback_control wbc;
300 wbc.sync_mode = WB_SYNC_ALL;
301 wbc.nr_to_write = mapping->nrpages * 2;
304 ret = generic_writepages(mapping, &wbc);
307 ret = wait_on_page_locked_range(mapping, start, end);