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 /* SYNCHRONOUS I/O to object storage for an inode */
38 static int ll_brw(int rw, struct inode *inode, struct page *page, int create)
40 struct ll_inode_info *lli = ll_i2info(inode);
41 struct lov_stripe_md *md = lli->lli_smd;
44 struct io_cb_data *cbd = ll_init_cb();
52 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
53 pg.flag = create ? OBD_BRW_CREATE : 0;
55 err = obd_brw(rw, ll_i2obdconn(inode), md, 1, &pg, ll_sync_io_cb, cbd);
60 /* returns the page unlocked, but with a reference */
61 static int ll_readpage(struct file *file, struct page *page)
63 struct inode *inode = page->mapping->host;
64 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
68 if (!PageLocked(page))
71 if (inode->i_size <= offset) {
72 memset(kmap(page), 0, PAGE_SIZE);
74 GOTO(readpage_out, rc);
77 if (Page_Uptodate(page)) {
78 CERROR("Explain this please?\n");
79 GOTO(readpage_out, rc);
82 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
87 SetPageUptodate(page);
93 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
96 struct inode *inode = page->mapping->host;
97 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
103 if (!PageLocked(page))
106 if (Page_Uptodate(page))
107 GOTO(prepare_done, rc);
109 /* We're completely overwriting an existing page, so _don't_ set it up
110 * to date until commit_write */
111 if (from == 0 && to == PAGE_SIZE)
114 /* We are writing to a new page, no need to read old data */
115 if (inode->i_size <= offset) {
116 memset(addr, 0, PAGE_SIZE);
120 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
125 SetPageUptodate(page);
130 /* returns the page unlocked, but with a reference */
131 static int ll_writepage(struct page *page)
133 struct inode *inode = page->mapping->host;
137 if (!PageLocked(page))
140 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
142 //SetPageUptodate(page);
143 set_page_clean(page);
145 CERROR("ll_brw failure %d\n", err);
152 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
154 static int ll_commit_write(struct file *file, struct page *page,
155 unsigned from, unsigned to)
158 struct inode *inode = page->mapping->host;
159 struct ll_inode_info *lli = ll_i2info(inode);
160 struct lov_stripe_md *md = lli->lli_smd;
164 struct io_cb_data *cbd = ll_init_cb();
168 pg.off = (((obd_off)page->index) << PAGE_SHIFT);
169 pg.flag = create ? OBD_BRW_CREATE : 0;
175 SetPageUptodate(page);
177 if (!PageLocked(page))
180 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
181 from, to, (unsigned long long)pg.count);
183 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
184 1, &pg, ll_sync_io_cb, cbd);
187 size = pg.off + pg.count;
188 /* do NOT truncate when writing in the middle of a file */
189 if (size > inode->i_size)
190 inode->i_size = size;
193 } /* ll_commit_write */
195 void ll_truncate(struct inode *inode)
197 struct obdo oa = {0};
198 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
199 struct lustre_handle *lockhs = NULL;
204 /* object not yet allocated */
205 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
209 oa.o_id = md->lmd_object_id;
210 oa.o_size = inode->i_size;
212 CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
213 (long)oa.o_id, (unsigned long long)oa.o_size);
215 err = ll_size_lock(inode, md, oa.o_size, LCK_PW, &lockhs);
217 CERROR("ll_size_lock failed: %d\n", err);
218 /* FIXME: What to do here? It's too late to back out... */
222 oa.o_valid = OBD_MD_FLID;
223 /* truncate == punch to/from start from/to end:
224 set end to -1 for that. */
225 err = obd_punch(ll_i2obdconn(inode), &oa, md, inode->i_size,
228 CERROR("obd_truncate fails (%d)\n", err);
230 obdo_to_inode(inode, &oa, oa.o_valid);
232 err = ll_size_unlock(inode, md, LCK_PW, lockhs);
234 CERROR("ll_size_unlock failed: %d\n", err);
240 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
241 unsigned long blocknr, int blocksize)
243 obd_count bufs_per_obdo = iobuf->nr_pages;
244 struct ll_inode_info *lli = ll_i2info(inode);
245 struct lov_stripe_md *md = lli->lli_smd;
246 struct brw_page *pga;
249 struct io_cb_data *cbd = ll_init_cb();
255 if (blocksize != PAGE_SIZE) {
256 CERROR("direct_IO blocksize != PAGE_SIZE\n");
260 OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
262 GOTO(out, rc = -ENOMEM);
264 /* NB: we can't use iobuf->maplist[i]->index for the offset
265 * instead of "blocknr" because ->index contains garbage.
267 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
268 pga[i].pg = iobuf->maplist[i];
269 pga[i].count = PAGE_SIZE;
270 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
271 pga[i].flag = OBD_BRW_CREATE;
274 if (!md || !md->lmd_object_id)
275 GOTO(out, rc = -ENOMEM);
277 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
278 ll_i2obdconn(inode), md, bufs_per_obdo, pga,
281 rc = bufs_per_obdo * PAGE_SIZE;
284 OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
289 int ll_flush_inode_pages(struct inode * inode)
291 obd_count bufs_per_obdo = 0;
292 obd_size *count = NULL;
293 obd_off *offset = NULL;
294 obd_flag *flags = NULL;
299 spin_lock(&pagecache_lock);
301 spin_unlock(&pagecache_lock);
304 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
305 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
306 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
307 if (!count || !offset || !flags)
308 GOTO(out, err=-ENOMEM);
311 for (i = 0 ; i < bufs_per_obdo ; i++) {
312 count[i] = PAGE_SIZE;
313 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
314 flags[i] = OBD_BRW_CREATE;
317 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
318 ll_i2info(inode)->lli_smd, bufs_per_obdo,
319 iobuf->maplist, count, offset, flags, NULL, NULL);
321 err = bufs_per_obdo * 4096;
324 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
325 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
326 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
332 struct address_space_operations ll_aops = {
333 readpage: ll_readpage,
334 writepage: ll_writepage,
335 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
336 direct_IO: ll_direct_IO,
338 sync_page: block_sync_page,
339 prepare_write: ll_prepare_write,
340 commit_write: ll_commit_write,