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
6 * Copyright (C) 2002 Cluster File Systems, Inc.
9 #include <linux/config.h>
10 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/iobuf.h>
15 #include <linux/errno.h>
16 #include <linux/locks.h>
17 #include <linux/unistd.h>
18 #include <linux/version.h>
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
23 #include <linux/stat.h>
24 #include <asm/uaccess.h>
25 #include <asm/segment.h>
27 #include <linux/pagemap.h>
28 #include <linux/smp_lock.h>
30 #define DEBUG_SUBSYSTEM S_LLITE
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_lite.h>
34 #include <linux/lustre_lib.h>
36 /* SYNCHRONOUS I/O to object storage for an inode */
37 static int ll_brw(int rw, struct inode *inode, struct page *page, int create)
39 obd_count num_obdo = 1;
40 obd_count bufs_per_obdo = 1;
42 obd_size count = PAGE_SIZE;
43 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
44 obd_flag flags = create ? OBD_BRW_CREATE : 0;
48 oa = ll_i2info(inode)->lli_obdo;
49 err = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
50 &page, &count, &offset, &flags, NULL);
54 /* returns the page unlocked, but with a reference */
55 static int ll_readpage(struct file *file, struct page *page)
57 struct inode *inode = page->mapping->host;
61 if (!PageLocked(page))
64 if (((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT) <= page->index) {
65 memset(kmap(page), 0, PAGE_CACHE_SIZE);
67 GOTO(readpage_out, rc);
70 if (Page_Uptodate(page)) {
71 CERROR("Explain this please?\n");
72 GOTO(readpage_out, rc);
75 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
80 SetPageUptodate(page);
86 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
89 struct inode *inode = page->mapping->host;
90 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
96 if (!PageLocked(page))
99 if (Page_Uptodate(page))
100 GOTO(prepare_done, rc);
102 if (offset + from >= inode->i_size) {
103 memset(addr, 0, PAGE_SIZE);
104 GOTO(prepare_done, rc);
107 /* We're completely overwriting an existing page, so _don't_ set it up
108 * to date until commit_write */
109 if (from == 0 && to == PAGE_SIZE) {
110 memset(addr, 0, PAGE_SIZE);
114 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
119 SetPageUptodate(page);
124 /* returns the page unlocked, but with a reference */
125 static int ll_writepage(struct page *page)
127 struct inode *inode = page->mapping->host;
131 if (!PageLocked(page))
134 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
136 //SetPageUptodate(page);
137 set_page_clean(page);
139 CERROR("ll_brw failure %d\n", err);
145 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
147 static int ll_commit_write(struct file *file, struct page *page,
148 unsigned from, unsigned to)
151 struct inode *inode = page->mapping->host;
152 obd_count num_obdo = 1;
153 obd_count bufs_per_obdo = 1;
156 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
157 obd_flag flags = create ? OBD_BRW_CREATE : 0;
162 oa = ll_i2info(inode)->lli_obdo;
164 SetPageUptodate(page);
166 if (!PageLocked(page))
169 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
170 from, to, (unsigned long long)count);
172 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), num_obdo, &oa,
173 &bufs_per_obdo, &page, &count, &offset, &flags, NULL);
176 iattr.ia_size = offset + to;
177 if (iattr.ia_size > inode->i_size) {
178 /* do NOT truncate when writing in the middle of a file */
179 inode->i_size = iattr.ia_size;
180 iattr.ia_valid = ATTR_SIZE;
182 err = ll_inode_setattr(inode, &iattr, 0);
184 CERROR("failed - %d.\n", err);
191 } /* ll_commit_write */
193 void ll_truncate(struct inode *inode)
199 oa = ll_i2info(inode)->lli_obdo;
201 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
202 (long)oa->o_id, (unsigned long long)oa->o_size);
203 err = obd_punch(ll_i2obdconn(inode), oa, oa->o_size, 0);
206 CERROR("obd_truncate fails (%d)\n", err);
212 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
213 unsigned long blocknr, int blocksize)
216 obd_count num_obdo = 1;
217 obd_count bufs_per_obdo = iobuf->nr_pages;
218 struct obdo *oa = NULL;
219 obd_size *count = NULL;
220 obd_off *offset = NULL;
221 obd_flag *flags = NULL;
226 if (blocksize != PAGE_SIZE) {
227 CERROR("direct_IO blocksize != PAGE_SIZE, what to do?\n");
231 OBD_ALLOC(count, sizeof(obd_size) * bufs_per_obdo);
232 OBD_ALLOC(offset, sizeof(obd_off) * bufs_per_obdo);
233 OBD_ALLOC(flags, sizeof(obd_flag) * bufs_per_obdo);
234 if (!count || !offset || !flags)
235 GOTO(out, rc = -ENOMEM);
237 /* NB: we can't use iobuf->maplist[i]->index for the offset
238 * instead of "blocknr" because ->index contains garbage.
240 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
241 count[i] = PAGE_SIZE;
242 offset[i] = (obd_off)blocknr << PAGE_SHIFT;
243 flags[i] = OBD_BRW_CREATE;
246 oa = ll_i2info(inode)->lli_obdo;
248 GOTO(out, rc = -ENOMEM);
249 rc = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
250 iobuf->maplist, count, offset, flags, NULL);
252 rc = bufs_per_obdo * PAGE_SIZE;
256 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo);
258 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo);
260 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo);
265 int ll_flush_inode_pages(struct inode * inode)
268 // obd_count num_obdo = 1;
269 obd_count bufs_per_obdo = 0;
270 struct obdo *oa = NULL;
271 obd_size *count = NULL;
272 obd_off *offset = NULL;
273 obd_flag *flags = NULL;
278 spin_lock(&pagecache_lock);
280 spin_unlock(&pagecache_lock);
283 OBD_ALLOC(count, sizeof(obd_size) * bufs_per_obdo);
285 GOTO(out, err=-ENOMEM);
287 OBD_ALLOC(offset, sizeof(obd_off) * bufs_per_obdo);
289 GOTO(out, err=-ENOMEM);
291 OBD_ALLOC(flags, sizeof(obd_flag) * bufs_per_obdo);
293 GOTO(out, err=-ENOMEM);
296 for (i = 0 ; i < bufs_per_obdo ; i++) {
297 count[i] = PAGE_SIZE;
298 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
299 flags[i] = OBD_BRW_CREATE;
302 oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
306 err = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
307 iobuf->maplist, count, offset, flags);
309 err = bufs_per_obdo * 4096;
315 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo);
317 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo);
319 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo);
325 struct address_space_operations ll_aops = {
326 readpage: ll_readpage,
327 writepage: ll_writepage,
328 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
329 direct_IO: ll_direct_IO,
331 sync_page: block_sync_page,
332 prepare_write: ll_prepare_write,
333 commit_write: ll_commit_write,