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>
37 * Remove page from dirty list
39 static void __set_page_clean(struct page *page)
41 struct address_space *mapping = page->mapping;
47 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,9))
48 spin_lock(&pagecache_lock);
51 list_del(&page->list);
52 list_add(&page->list, &mapping->clean_pages);
54 inode = mapping->host;
55 if (list_empty(&mapping->dirty_pages)) {
56 CDEBUG(D_INODE, "inode clean\n");
57 inode->i_state &= ~I_DIRTY_PAGES;
59 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,10))
60 spin_unlock(&pagecache_lock);
65 inline void set_page_clean(struct page *page)
67 if (PageDirty(page)) {
69 __set_page_clean(page);
73 /* SYNCHRONOUS I/O to object storage for an inode */
74 static int ll_brw(int cmd, struct inode *inode, struct page *page, int create)
76 struct ll_inode_info *lli = ll_i2info(inode);
77 struct lov_stripe_md *md = lli->lli_smd;
80 struct io_cb_data *cbd = ll_init_cb();
88 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
89 pg.flag = create ? OBD_BRW_CREATE : 0;
91 err = obd_brw(cmd, ll_i2obdconn(inode), md, 1, &pg, ll_sync_io_cb, cbd);
96 /* returns the page unlocked, but with a reference */
97 static int ll_readpage(struct file *file, struct page *page)
99 struct inode *inode = page->mapping->host;
100 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
104 if (!PageLocked(page))
107 if (inode->i_size <= offset) {
108 memset(kmap(page), 0, PAGE_SIZE);
110 GOTO(readpage_out, rc);
113 if (Page_Uptodate(page)) {
114 CERROR("Explain this please?\n");
115 GOTO(readpage_out, rc);
118 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
123 SetPageUptodate(page);
129 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
132 struct inode *inode = page->mapping->host;
133 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
139 if (!PageLocked(page))
142 if (Page_Uptodate(page))
143 GOTO(prepare_done, rc);
145 /* We're completely overwriting an existing page, so _don't_ set it up
146 * to date until commit_write */
147 if (from == 0 && to == PAGE_SIZE)
150 /* We are writing to a new page, no need to read old data */
151 if (inode->i_size <= offset) {
152 memset(addr, 0, PAGE_SIZE);
156 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
161 SetPageUptodate(page);
166 /* returns the page unlocked, but with a reference */
167 static int ll_writepage(struct page *page)
169 struct inode *inode = page->mapping->host;
173 if (!PageLocked(page))
176 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
178 //SetPageUptodate(page);
179 set_page_clean(page);
181 CERROR("ll_brw failure %d\n", err);
188 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
190 static int ll_commit_write(struct file *file, struct page *page,
191 unsigned from, unsigned to)
194 struct inode *inode = page->mapping->host;
195 struct ll_inode_info *lli = ll_i2info(inode);
196 struct lov_stripe_md *md = lli->lli_smd;
200 struct io_cb_data *cbd = ll_init_cb();
205 pg.off = (((obd_off)page->index) << PAGE_SHIFT);
206 pg.flag = create ? OBD_BRW_CREATE : 0;
211 SetPageUptodate(page);
213 if (!PageLocked(page))
216 CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count %ld\n",
219 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
220 1, &pg, ll_sync_io_cb, cbd);
223 size = pg.off + pg.count;
224 /* do NOT truncate when writing in the middle of a file */
225 if (size > inode->i_size)
226 inode->i_size = size;
229 } /* ll_commit_write */
231 void ll_truncate(struct inode *inode)
233 struct obdo oa = {0};
234 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
235 struct lustre_handle *lockhs = NULL;
240 /* object not yet allocated */
241 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
245 oa.o_id = md->lmd_object_id;
246 oa.o_size = inode->i_size;
248 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after "LPD64")\n",
251 err = ll_size_lock(inode, md, oa.o_size, LCK_PW, &lockhs);
253 CERROR("ll_size_lock failed: %d\n", err);
254 /* FIXME: What to do here? It's too late to back out... */
258 oa.o_valid = OBD_MD_FLID;
259 /* truncate == punch to/from start from/to end:
260 set end to -1 for that. */
261 err = obd_punch(ll_i2obdconn(inode), &oa, md, inode->i_size,
264 CERROR("obd_truncate fails (%d)\n", err);
266 obdo_to_inode(inode, &oa, oa.o_valid);
268 err = ll_size_unlock(inode, md, LCK_PW, lockhs);
270 CERROR("ll_size_unlock failed: %d\n", err);
276 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
277 unsigned long blocknr, int blocksize)
279 obd_count bufs_per_obdo = iobuf->nr_pages;
280 struct ll_inode_info *lli = ll_i2info(inode);
281 struct lov_stripe_md *md = lli->lli_smd;
282 struct brw_page *pga;
284 struct io_cb_data *cbd;
287 if (!md || !md->lmd_object_id)
290 if (blocksize != PAGE_SIZE) {
291 CERROR("direct_IO blocksize != PAGE_SIZE\n");
299 OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
301 OBD_FREE(cbd, sizeof(*cbd));
305 /* NB: we can't use iobuf->maplist[i]->index for the offset
306 * instead of "blocknr" because ->index contains garbage.
308 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
309 pga[i].pg = iobuf->maplist[i];
310 pga[i].count = PAGE_SIZE;
311 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
312 pga[i].flag = OBD_BRW_CREATE;
315 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
316 ll_i2obdconn(inode), md, bufs_per_obdo, pga,
319 rc = bufs_per_obdo * PAGE_SIZE;
321 OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
325 int ll_flush_inode_pages(struct inode * inode)
327 obd_count bufs_per_obdo = 0;
328 obd_size *count = NULL;
329 obd_off *offset = NULL;
330 obd_flag *flags = NULL;
335 spin_lock(&pagecache_lock);
337 spin_unlock(&pagecache_lock);
340 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
341 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
342 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
343 if (!count || !offset || !flags)
344 GOTO(out, err=-ENOMEM);
347 for (i = 0 ; i < bufs_per_obdo ; i++) {
348 count[i] = PAGE_SIZE;
349 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
350 flags[i] = OBD_BRW_CREATE;
353 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
354 ll_i2info(inode)->lli_smd, bufs_per_obdo,
355 iobuf->maplist, count, offset, flags, NULL, NULL);
357 err = bufs_per_obdo * 4096;
360 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
361 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
362 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
366 struct address_space_operations ll_aops = {
367 readpage: ll_readpage,
368 writepage: ll_writepage,
369 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
370 direct_IO: ll_direct_IO,
372 sync_page: block_sync_page,
373 prepare_write: ll_prepare_write,
374 commit_write: ll_commit_write,