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.
24 #include <linux/config.h>
25 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
37 #include <linux/buffer_head.h>
38 #include <linux/mpage.h>
39 #include <linux/writeback.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
44 #include <linux/pagemap.h>
45 #include <linux/smp_lock.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <lustre_mdc.h>
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 static int ll_invalidatepage(struct page *page, unsigned long offset)
66 if (PagePrivate(page))
71 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
73 if (PagePrivate(page))
78 static int ll_set_page_dirty(struct page *page)
80 struct ll_async_page *llap;
83 llap = llap_from_page(page, LLAP_ORIGIN_UNKNOWN);
85 RETURN(PTR_ERR(llap));
87 llap_write_pending(page->mapping->host, llap);
88 RETURN(__set_page_dirty_nobuffers(page));
91 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
93 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
94 size_t size, struct page ***pages)
97 unsigned long page_count;
99 /* set an arbitrary limit to prevent arithmetic overflow */
100 if (size > MAX_DIRECTIO_SIZE) {
105 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
106 page_count -= user_addr >> PAGE_SHIFT;
108 OBD_ALLOC_GFP(*pages, page_count * sizeof(**pages), GFP_KERNEL);
110 down_read(¤t->mm->mmap_sem);
111 result = get_user_pages(current, current->mm, user_addr,
112 page_count, (rw == READ), 0, *pages,
114 up_read(¤t->mm->mmap_sem);
120 /* ll_free_user_pages - tear down page struct array
121 * @pages: array of page struct pointers underlying target buffer */
122 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
126 for (i = 0; i < npages; i++) {
128 set_page_dirty_lock(pages[i]);
129 page_cache_release(pages[i]);
132 OBD_FREE(pages, npages * sizeof(*pages));
135 static ssize_t ll_direct_IO_26_seg(int rw, struct file *file,
136 struct address_space *mapping,
138 struct lov_stripe_md *lsm,
139 unsigned long user_addr, size_t size,
140 loff_t file_offset, struct page **pages,
143 struct brw_page *pga;
147 struct obd_capa *ocapa;
150 OBD_ALLOC(pga, sizeof(*pga) * page_count);
154 for (i = 0, length = size; length > 0;
155 length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
156 pga[i].pg = pages[i];
157 pga[i].off = file_offset;
158 /* To the end of the page, or the length, whatever is less */
159 pga[i].count = min_t(int, PAGE_SIZE -(file_offset & ~PAGE_MASK),
163 POISON_PAGE(pages[i], 0x0d);
166 ll_inode_fill_obdo(inode, rw, &oa);
169 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
170 LPROC_LL_DIRECT_WRITE, size);
171 opc = CAPA_OPC_OSS_WRITE;
172 llap_write_pending(inode, NULL);
174 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
175 LPROC_LL_DIRECT_READ, size);
176 opc = CAPA_OPC_OSS_READ | CAPA_OPC_OSS_WRITE;
178 ocapa = ll_osscapa_get(inode, opc);
179 rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
180 ll_i2dtexp(inode), &oa, lsm, page_count, pga, NULL,
186 lov_stripe_lock(lsm);
187 obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
188 lov_stripe_unlock(lsm);
192 OBD_FREE(pga, sizeof(*pga) * page_count);
196 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
197 const struct iovec *iov, loff_t file_offset,
198 unsigned long nr_segs)
200 struct file *file = iocb->ki_filp;
201 ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
202 struct ll_inode_info *lli = ll_i2info(file->f_mapping->host);
203 unsigned long seg = 0;
206 if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
209 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
210 if ((file_offset & (PAGE_SIZE - 1)) || (count & (PAGE_SIZE - 1)))
213 while ((seg < nr_segs) && (tot_bytes >= 0)) {
214 const struct iovec *vec = &iov[seg++];
215 unsigned long user_addr = (unsigned long)vec->iov_base;
216 size_t size = vec->iov_len;
221 page_count = ll_get_user_pages(rw, user_addr, size, &pages);
222 if (page_count < 0) {
223 ll_free_user_pages(pages, 0, 0);
229 result = ll_direct_IO_26_seg(rw, file, file->f_mapping,
230 file->f_mapping->host,
231 lli->lli_smd, user_addr, size,
232 file_offset, pages, page_count);
233 ll_free_user_pages(pages, page_count, rw == READ);
242 file_offset += result;
249 struct address_space_operations ll_aops = {
250 .readpage = ll_readpage,
251 // .readpages = ll_readpages,
252 .direct_IO = ll_direct_IO_26,
253 .writepage = ll_writepage_26,
254 .writepages = generic_writepages,
255 .set_page_dirty = ll_set_page_dirty,
257 .prepare_write = ll_prepare_write,
258 .commit_write = ll_commit_write,
259 .invalidatepage = ll_invalidatepage,
260 .releasepage = ll_releasepage,