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/smp_lock.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 *lsm = lli->lli_smd;
78 struct io_cb_data *cbd = ll_init_cb();
83 CHECK_MOUNT_EPOCH(inode);
90 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
91 pg.flag = create ? OBD_BRW_CREATE : 0;
93 err = obd_brw(cmd, ll_i2obdconn(inode),lsm, 1, &pg, ll_sync_io_cb, cbd);
98 /* returns the page unlocked, but with a reference */
99 static int ll_readpage(struct file *file, struct page *page)
101 struct inode *inode = page->mapping->host;
102 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
106 if (!PageLocked(page))
109 if (inode->i_size <= offset) {
110 memset(kmap(page), 0, PAGE_SIZE);
112 GOTO(readpage_out, rc);
115 if (Page_Uptodate(page)) {
116 CERROR("Explain this please?\n");
117 GOTO(readpage_out, rc);
120 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
125 SetPageUptodate(page);
131 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
134 struct inode *inode = page->mapping->host;
135 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
141 if (!PageLocked(page))
144 if (Page_Uptodate(page))
145 GOTO(prepare_done, rc);
147 /* We're completely overwriting an existing page, so _don't_ set it up
148 * to date until commit_write */
149 if (from == 0 && to == PAGE_SIZE)
152 /* We are writing to a new page, no need to read old data */
153 if (inode->i_size <= offset) {
154 memset(addr, 0, PAGE_SIZE);
158 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
163 SetPageUptodate(page);
168 /* returns the page unlocked, but with a reference */
169 static int ll_writepage(struct page *page)
171 struct inode *inode = page->mapping->host;
175 if (!PageLocked(page))
178 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
180 //SetPageUptodate(page);
181 set_page_clean(page);
183 CERROR("ll_brw failure %d\n", err);
190 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
192 static int ll_commit_write(struct file *file, struct page *page,
193 unsigned from, unsigned to)
196 struct inode *inode = page->mapping->host;
197 struct ll_inode_info *lli = ll_i2info(inode);
198 struct lov_stripe_md *md = lli->lli_smd;
202 struct io_cb_data *cbd = ll_init_cb();
205 CHECK_MOUNT_EPOCH(inode);
209 pg.off = (((obd_off)page->index) << PAGE_SHIFT);
210 pg.flag = create ? OBD_BRW_CREATE : 0;
215 SetPageUptodate(page);
217 if (!PageLocked(page))
220 CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count "LPD64"\n",
223 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
224 1, &pg, ll_sync_io_cb, cbd);
227 size = pg.off + pg.count;
228 /* do NOT truncate when writing in the middle of a file */
229 if (size > inode->i_size)
230 inode->i_size = size;
233 } /* ll_commit_write */
235 void ll_truncate(struct inode *inode)
237 struct obdo oa = {0};
238 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
239 struct lustre_handle *lockhs = NULL;
244 /* object not yet allocated */
245 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
249 oa.o_id = lsm->lsm_object_id;
250 oa.o_mode = inode->i_mode;
251 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
253 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after "LPD64")\n",
254 oa.o_id, inode->i_size);
256 err = ll_size_lock(inode, lsm, inode->i_size, LCK_PW, &lockhs);
258 CERROR("ll_size_lock failed: %d\n", err);
259 /* FIXME: What to do here? It's too late to back out... */
263 /* truncate == punch from new size to absolute end of file */
264 err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
267 CERROR("obd_truncate fails (%d)\n", err);
269 obdo_to_inode(inode, &oa, oa.o_valid);
271 err = ll_size_unlock(inode, lsm, LCK_PW, lockhs);
273 CERROR("ll_size_unlock failed: %d\n", err);
279 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
280 unsigned long blocknr, int blocksize)
282 obd_count bufs_per_obdo = iobuf->nr_pages;
283 struct ll_inode_info *lli = ll_i2info(inode);
284 struct lov_stripe_md *lsm = lli->lli_smd;
285 struct brw_page *pga;
287 struct io_cb_data *cbd;
289 CHECK_MOUNT_EPOCH(inode);
292 if (!lsm || !lsm->lsm_object_id)
295 if (blocksize != PAGE_SIZE) {
296 CERROR("direct_IO blocksize != PAGE_SIZE\n");
304 OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
306 OBD_FREE(cbd, sizeof(*cbd));
310 /* NB: we can't use iobuf->maplist[i]->index for the offset
311 * instead of "blocknr" because ->index contains garbage.
313 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
314 pga[i].pg = iobuf->maplist[i];
315 pga[i].count = PAGE_SIZE;
316 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
317 pga[i].flag = OBD_BRW_CREATE;
320 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
321 ll_i2obdconn(inode), lsm, bufs_per_obdo, pga,
324 rc = bufs_per_obdo * PAGE_SIZE;
326 OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
330 int ll_flush_inode_pages(struct inode * inode)
332 obd_count bufs_per_obdo = 0;
333 obd_size *count = NULL;
334 obd_off *offset = NULL;
335 obd_flag *flags = NULL;
340 spin_lock(&pagecache_lock);
342 spin_unlock(&pagecache_lock);
345 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
346 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
347 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
348 if (!count || !offset || !flags)
349 GOTO(out, err=-ENOMEM);
352 for (i = 0 ; i < bufs_per_obdo ; i++) {
353 count[i] = PAGE_SIZE;
354 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
355 flags[i] = OBD_BRW_CREATE;
358 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
359 ll_i2info(inode)->lli_smd, bufs_per_obdo,
360 iobuf->maplist, count, offset, flags, NULL, NULL);
362 err = bufs_per_obdo * 4096;
365 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
366 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
367 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
371 struct address_space_operations ll_aops = {
372 readpage: ll_readpage,
373 writepage: ll_writepage,
374 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
375 direct_IO: ll_direct_IO,
377 sync_page: block_sync_page,
378 prepare_write: ll_prepare_write,
379 commit_write: ll_commit_write,