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) 2001, 2002 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/iobuf.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
40 #include <asm/segment.h>
42 #include <linux/pagemap.h>
43 #include <linux/smp_lock.h>
45 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <linux/lustre_mds.h>
48 #include <linux/lustre_lite.h>
49 #include <linux/lustre_lib.h>
52 * Remove page from dirty list
54 static void __set_page_clean(struct page *page)
56 struct address_space *mapping = page->mapping;
62 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
63 spin_lock(&pagecache_lock);
66 list_del(&page->list);
67 list_add(&page->list, &mapping->clean_pages);
69 inode = mapping->host;
70 if (list_empty(&mapping->dirty_pages)) {
71 CDEBUG(D_INODE, "inode clean\n");
72 inode->i_state &= ~I_DIRTY_PAGES;
74 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
75 spin_unlock(&pagecache_lock);
80 inline void set_page_clean(struct page *page)
82 if (PageDirty(page)) {
84 __set_page_clean(page);
88 /* SYNCHRONOUS I/O to object storage for an inode */
89 static int ll_brw(int cmd, struct inode *inode, struct page *page, int create)
91 struct ll_inode_info *lli = ll_i2info(inode);
92 struct lov_stripe_md *lsm = lli->lli_smd;
93 struct obd_brw_set *set;
98 set = obd_brw_set_new();
103 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
105 if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
106 pg.count = inode->i_size % PAGE_SIZE;
108 pg.count = PAGE_SIZE;
110 pg.flag = create ? OBD_BRW_CREATE : 0;
112 set->brw_callback = ll_brw_sync_wait;
113 rc = obd_brw(cmd, ll_i2obdconn(inode), lsm, 1, &pg, set);
115 CERROR("error from obd_brw: rc = %d\n", rc);
117 rc = ll_brw_sync_wait(set, CB_PHASE_START);
119 CERROR("error from callback: rc = %d\n", rc);
121 obd_brw_set_free(set);
126 /* returns the page unlocked, but with a reference */
127 static int ll_readpage(struct file *file, struct page *page)
129 struct inode *inode = page->mapping->host;
130 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
134 if (!PageLocked(page))
137 if (inode->i_size <= offset) {
138 memset(kmap(page), 0, PAGE_SIZE);
140 GOTO(readpage_out, rc);
143 if (PageUptodate(page)) {
144 CERROR("Explain this please?\n");
145 GOTO(readpage_out, rc);
148 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
153 SetPageUptodate(page);
158 void ll_truncate(struct inode *inode)
160 struct obdo oa = {0};
161 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
162 struct lustre_handle *lockhs = NULL;
167 /* object not yet allocated */
168 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
172 oa.o_id = lsm->lsm_object_id;
173 oa.o_mode = inode->i_mode;
174 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
176 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after "LPD64")\n",
177 oa.o_id, inode->i_size);
179 err = ll_size_lock(inode, lsm, inode->i_size, LCK_PW, &lockhs);
181 CERROR("ll_size_lock failed: %d\n", err);
182 /* FIXME: What to do here? It's too late to back out... */
186 /* truncate == punch from new size to absolute end of file */
187 err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
191 CERROR("obd_truncate fails (%d) ino %ld\n", err,
194 obdo_to_inode(inode, &oa, oa.o_valid);
196 err = ll_size_unlock(inode, lsm, LCK_PW, lockhs);
198 CERROR("ll_size_unlock failed: %d\n", err);
204 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
206 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
209 struct inode *inode = page->mapping->host;
210 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
216 if (!PageLocked(page))
219 if (Page_Uptodate(page))
220 GOTO(prepare_done, rc);
222 /* We're completely overwriting an existing page, so _don't_ set it up
223 * to date until commit_write */
224 if (from == 0 && to == PAGE_SIZE)
227 /* We are writing to a new page, no need to read old data */
228 if (inode->i_size <= offset) {
229 memset(addr, 0, PAGE_SIZE);
230 GOTO(prepare_done, rc=0);
233 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
238 SetPageUptodate(page);
243 /* returns the page unlocked, but with a reference */
244 static int ll_writepage(struct page *page)
246 struct inode *inode = page->mapping->host;
250 if (!PageLocked(page))
253 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
255 //SetPageUptodate(page);
256 set_page_clean(page);
258 CERROR("ll_brw failure %d\n", err);
265 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
267 static int ll_commit_write(struct file *file, struct page *page,
268 unsigned from, unsigned to)
270 struct inode *inode = page->mapping->host;
271 struct ll_inode_info *lli = ll_i2info(inode);
272 struct lov_stripe_md *md = lli->lli_smd;
274 struct obd_brw_set *set;
281 pg.off = (((obd_off)page->index) << PAGE_SHIFT);
282 pg.flag = create ? OBD_BRW_CREATE : 0;
284 set = obd_brw_set_new();
288 SetPageUptodate(page);
290 if (!PageLocked(page))
293 CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count "LPD64"\n",
296 set->brw_callback = ll_brw_sync_wait;
297 rc = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md, 1, &pg, set);
299 CERROR("error from obd_brw: rc = %d\n", rc);
301 rc = ll_brw_sync_wait(set, CB_PHASE_START);
303 CERROR("error from callback: rc = %d\n", rc);
305 obd_brw_set_free(set);
308 size = pg.off + pg.count;
309 /* do NOT truncate when writing in the middle of a file */
310 if (size > inode->i_size)
311 inode->i_size = size;
314 } /* ll_commit_write */
317 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
318 unsigned long blocknr, int blocksize)
320 obd_count bufs_per_obdo = iobuf->nr_pages;
321 struct ll_inode_info *lli = ll_i2info(inode);
322 struct lov_stripe_md *lsm = lli->lli_smd;
323 struct brw_page *pga;
324 struct obd_brw_set *set;
328 if (!lsm || !lsm->lsm_object_id)
331 if (blocksize != PAGE_SIZE) {
332 CERROR("direct_IO blocksize != PAGE_SIZE\n");
336 set = obd_brw_set_new();
340 OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
342 obd_brw_set_free(set);
346 /* NB: we can't use iobuf->maplist[i]->index for the offset
347 * instead of "blocknr" because ->index contains garbage.
349 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
350 pga[i].pg = iobuf->maplist[i];
351 pga[i].count = PAGE_SIZE;
352 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
353 pga[i].flag = OBD_BRW_CREATE;
356 set->brw_callback = ll_brw_sync_wait;
357 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
358 ll_i2obdconn(inode), lsm, bufs_per_obdo, pga, set);
360 CERROR("error from obd_brw: rc = %d\n", rc);
362 rc = ll_brw_sync_wait(set, CB_PHASE_START);
364 CERROR("error from callback: rc = %d\n", rc);
366 obd_brw_set_free(set);
368 rc = bufs_per_obdo * PAGE_SIZE;
370 OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
374 int ll_flush_inode_pages(struct inode * inode)
376 obd_count bufs_per_obdo = 0;
377 obd_size *count = NULL;
378 obd_off *offset = NULL;
379 obd_flag *flags = NULL;
384 spin_lock(&pagecache_lock);
386 spin_unlock(&pagecache_lock);
389 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
390 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
391 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
392 if (!count || !offset || !flags)
393 GOTO(out, err=-ENOMEM);
396 for (i = 0 ; i < bufs_per_obdo ; i++) {
397 count[i] = PAGE_SIZE;
398 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
399 flags[i] = OBD_BRW_CREATE;
402 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
403 ll_i2info(inode)->lli_smd, bufs_per_obdo,
404 iobuf->maplist, count, offset, flags, NULL, NULL);
406 err = bufs_per_obdo * 4096;
409 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
410 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
411 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
418 struct address_space_operations ll_aops = {
419 readpage: ll_readpage,
420 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
421 direct_IO: ll_direct_IO,
422 writepage: ll_writepage,
423 sync_page: block_sync_page,
424 prepare_write: ll_prepare_write,
425 commit_write: ll_commit_write,