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 struct ll_inode_info *lii = ll_i2info(inode);
40 struct lov_stripe_md *md = lii->lli_smd;
41 obd_size count = PAGE_SIZE;
42 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
43 obd_flag flags = create ? OBD_BRW_CREATE : 0;
47 err = obd_brw(rw, ll_i2obdconn(inode), md, 1,
48 &page, &count, &offset, &flags, NULL, NULL);
52 /* returns the page unlocked, but with a reference */
53 static int ll_readpage(struct file *file, struct page *page)
55 struct inode *inode = page->mapping->host;
59 if (!PageLocked(page))
62 if (((inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) <= page->index) {
63 memset(kmap(page), 0, PAGE_SIZE);
65 GOTO(readpage_out, rc);
68 if (Page_Uptodate(page)) {
69 CERROR("Explain this please?\n");
70 GOTO(readpage_out, rc);
73 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
78 SetPageUptodate(page);
84 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
87 struct inode *inode = page->mapping->host;
88 //obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
94 if (!PageLocked(page))
97 if (Page_Uptodate(page))
98 GOTO(prepare_done, rc);
100 memset(addr, 0, PAGE_SIZE);
102 /* We're completely overwriting an existing page, so _don't_ set it up
103 * to date until commit_write */
104 if (from == 0 && to == PAGE_SIZE)
107 /* prepare write should not read what lies beyond the end of
111 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
116 SetPageUptodate(page);
121 /* returns the page unlocked, but with a reference */
122 static int ll_writepage(struct page *page)
124 struct inode *inode = page->mapping->host;
128 if (!PageLocked(page))
131 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
133 //SetPageUptodate(page);
134 set_page_clean(page);
136 CERROR("ll_brw failure %d\n", err);
142 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
144 static int ll_commit_write(struct file *file, struct page *page,
145 unsigned from, unsigned to)
148 struct inode *inode = page->mapping->host;
149 struct ll_inode_info *lii = ll_i2info(inode);
150 struct lov_stripe_md *md = lii->lli_smd;
152 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
153 obd_flag flags = create ? OBD_BRW_CREATE : 0;
159 SetPageUptodate(page);
161 if (!PageLocked(page))
164 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
165 from, to, (unsigned long long)count);
167 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
168 1, &page, &count, &offset, &flags, NULL, NULL);
171 iattr.ia_size = offset + to;
172 if (iattr.ia_size > inode->i_size) {
173 /* do NOT truncate when writing in the middle of a file */
174 inode->i_size = iattr.ia_size;
175 iattr.ia_valid = ATTR_SIZE;
177 err = ll_inode_setattr(inode, &iattr, 0);
179 CERROR("failed - %d.\n", err);
186 } /* ll_commit_write */
188 void ll_truncate(struct inode *inode)
190 struct obdo oa = {0};
191 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
196 /* object not yet allocated */
197 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
201 CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
202 (long)oa.o_id, (unsigned long long)oa.o_size);
204 oa.o_id = md->lmd_object_id;
205 oa.o_valid = OBD_MD_FLID;
206 /* truncate == punch to/from start from/to end:
207 set end to -1 for that. */
208 err = obd_punch(ll_i2obdconn(inode), &oa, md, inode->i_size,
211 CERROR("obd_truncate fails (%d)\n", err);
213 /* This is done for us at the OST and MDS, but the
214 * updated timestamps are not sent back to us.
217 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
223 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
224 unsigned long blocknr, int blocksize)
226 obd_count bufs_per_obdo = iobuf->nr_pages;
227 struct ll_inode_info *lii = ll_i2info(inode);
228 struct lov_stripe_md *md = lii->lli_smd;
229 obd_size *count = NULL;
230 obd_off *offset = NULL;
231 obd_flag *flags = NULL;
237 if (blocksize != PAGE_SIZE) {
238 CERROR("direct_IO blocksize != PAGE_SIZE\n");
242 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
243 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
244 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
245 if (!count || !offset || !flags)
246 GOTO(out, rc = -ENOMEM);
248 /* NB: we can't use iobuf->maplist[i]->index for the offset
249 * instead of "blocknr" because ->index contains garbage.
251 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
252 count[i] = PAGE_SIZE;
253 offset[i] = (obd_off)blocknr << PAGE_SHIFT;
254 flags[i] = OBD_BRW_CREATE;
257 if (!md || !md->lmd_object_id)
258 GOTO(out, rc = -ENOMEM);
260 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
261 ll_i2obdconn(inode), md, bufs_per_obdo,
262 iobuf->maplist, count, offset, flags, NULL, NULL);
264 rc = bufs_per_obdo * PAGE_SIZE;
267 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo);
268 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo);
269 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo);
274 int ll_flush_inode_pages(struct inode * inode)
276 obd_count bufs_per_obdo = 0;
277 obd_size *count = NULL;
278 obd_off *offset = NULL;
279 obd_flag *flags = NULL;
284 spin_lock(&pagecache_lock);
286 spin_unlock(&pagecache_lock);
289 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
290 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
291 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
292 if (!count || !offset || !flags)
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 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
303 ll_i2info(inode)->lli_smd, bufs_per_obdo,
304 iobuf->maplist, count, offset, flags, NULL, NULL);
306 err = bufs_per_obdo * 4096;
309 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
310 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
311 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
317 struct address_space_operations ll_aops = {
318 readpage: ll_readpage,
319 writepage: ll_writepage,
320 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
321 direct_IO: ll_direct_IO,
323 sync_page: block_sync_page,
324 prepare_write: ll_prepare_write,
325 commit_write: ll_commit_write,