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_SIZE - 1) >> PAGE_SHIFT) <= page->index) {
65 memset(kmap(page), 0, PAGE_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 memset(addr, 0, PAGE_SIZE);
104 /* We're completely overwriting an existing page, so _don't_ set it up
105 * to date until commit_write */
106 if (from == 0 && to == PAGE_SIZE)
109 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
114 SetPageUptodate(page);
119 /* returns the page unlocked, but with a reference */
120 static int ll_writepage(struct page *page)
122 struct inode *inode = page->mapping->host;
126 if (!PageLocked(page))
129 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
131 //SetPageUptodate(page);
132 set_page_clean(page);
134 CERROR("ll_brw failure %d\n", err);
140 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
142 static int ll_commit_write(struct file *file, struct page *page,
143 unsigned from, unsigned to)
146 struct inode *inode = page->mapping->host;
147 obd_count num_obdo = 1;
148 obd_count bufs_per_obdo = 1;
151 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
152 obd_flag flags = create ? OBD_BRW_CREATE : 0;
157 oa = ll_i2info(inode)->lli_obdo;
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), num_obdo, &oa,
168 &bufs_per_obdo, &page, &count, &offset, &flags, 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)
194 oa = ll_i2info(inode)->lli_obdo;
196 /* object not yet allocated */
197 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
200 CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
201 (long)oa->o_id, (unsigned long long)oa->o_size);
202 oa->o_size = inode->i_size;
203 oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLID;
204 /* truncate == punch from i_size onwards */
205 err = obd_punch(ll_i2obdconn(inode), oa, -1 - oa->o_size, oa->o_size);
207 CERROR("obd_truncate fails (%d)\n", err);
209 /* This is done for us at the OST and MDS, but the
210 * updated timestamps are not sent back to us.
213 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
219 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
220 unsigned long blocknr, int blocksize)
222 obd_count num_obdo = 1;
223 obd_count bufs_per_obdo = iobuf->nr_pages;
224 struct obdo *oa = NULL;
225 obd_size *count = NULL;
226 obd_off *offset = NULL;
227 obd_flag *flags = NULL;
233 if (blocksize != PAGE_SIZE) {
234 CERROR("direct_IO blocksize != PAGE_SIZE\n");
238 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
239 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
240 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
241 if (!count || !offset || !flags)
242 GOTO(out, rc = -ENOMEM);
244 /* NB: we can't use iobuf->maplist[i]->index for the offset
245 * instead of "blocknr" because ->index contains garbage.
247 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
248 count[i] = PAGE_SIZE;
249 offset[i] = (obd_off)blocknr << PAGE_SHIFT;
250 flags[i] = OBD_BRW_CREATE;
253 oa = ll_i2info(inode)->lli_obdo;
255 GOTO(out, rc = -ENOMEM);
257 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
258 ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
259 iobuf->maplist, count, offset, flags, NULL);
261 rc = bufs_per_obdo * PAGE_SIZE;
264 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo);
265 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo);
266 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo);
271 int ll_flush_inode_pages(struct inode * inode)
274 // obd_count num_obdo = 1;
275 obd_count bufs_per_obdo = 0;
276 struct obdo *oa = NULL;
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 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;
313 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
314 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
315 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
321 struct address_space_operations ll_aops = {
322 readpage: ll_readpage,
323 writepage: ll_writepage,
324 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
325 direct_IO: ll_direct_IO,
327 sync_page: block_sync_page,
328 prepare_write: ll_prepare_write,
329 commit_write: ll_commit_write,