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-2003 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 CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
111 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
114 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
116 inode->i_ino, inode, inode->i_size, page->mapping->host,
117 page->mapping->host->i_size, page->index, pg.off);
120 pg.flag = create ? OBD_BRW_CREATE : 0;
122 set->brw_callback = ll_brw_sync_wait;
123 rc = obd_brw(cmd, ll_i2obdconn(inode), lsm, 1, &pg, set, NULL);
126 CERROR("error from obd_brw: rc = %d\n", rc);
128 rc = ll_brw_sync_wait(set, CB_PHASE_START);
130 CERROR("error from callback: rc = %d\n", rc);
132 obd_brw_set_free(set);
137 /* returns the page unlocked, but with a reference */
138 static int ll_readpage(struct file *file, struct page *page)
140 struct inode *inode = page->mapping->host;
141 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
145 if (!PageLocked(page))
148 if (inode->i_size <= offset) {
149 memset(kmap(page), 0, PAGE_SIZE);
151 GOTO(readpage_out, rc);
154 if (PageUptodate(page)) {
155 CERROR("Explain this please?\n");
156 GOTO(readpage_out, rc);
159 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
164 SetPageUptodate(page);
169 void ll_truncate(struct inode *inode)
171 struct obdo oa = {0};
172 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
173 struct lustre_handle lockh = { 0, 0 };
178 /* object not yet allocated */
179 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
183 oa.o_id = lsm->lsm_object_id;
184 oa.o_mode = inode->i_mode;
185 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
187 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
188 oa.o_id, inode->i_size);
190 err = ll_size_lock(inode, lsm, inode->i_size, LCK_PW, &lockh);
192 CERROR("ll_size_lock failed: %d\n", err);
196 /* truncate == punch from new size to absolute end of file */
197 err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
198 OBD_OBJECT_EOF, NULL);
200 CERROR("obd_truncate fails (%d) ino %lu\n", err, inode->i_ino);
202 obdo_to_inode(inode, &oa, oa.o_valid);
204 err = ll_size_unlock(inode, lsm, LCK_PW, &lockh);
206 CERROR("ll_size_unlock failed: %d\n", err);
212 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
214 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
217 struct inode *inode = page->mapping->host;
218 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
224 if (!PageLocked(page))
227 if (Page_Uptodate(page))
228 GOTO(prepare_done, rc);
230 /* We're completely overwriting an existing page, so _don't_ set it up
231 * to date until commit_write */
232 if (from == 0 && to == PAGE_SIZE)
235 /* If are writing to a new page, no need to read old data. If we
236 * haven't already gotten the file size in ll_file_write() since
237 * we got our extent lock, we need to verify it here before we
238 * overwrite some other node's write (bug 445).
240 if (inode->i_size <= offset) {
241 if (!S_ISBLK(inode->i_mode) && !(file->f_flags & O_APPEND)) {
242 struct ll_file_data *fd = file->private_data;
243 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
245 rc = ll_file_size(inode, lsm, &fd->fd_osthandle);
247 GOTO(prepare_done, rc);
249 if (inode->i_size <= offset) {
250 memset(addr, 0, PAGE_SIZE);
251 GOTO(prepare_done, rc=0);
255 rc = ll_brw(OBD_BRW_READ, inode, page, 0);
260 SetPageUptodate(page);
267 /* Write a page from kupdated or kswapd.
269 * We unlock the page even in the face of an error, otherwise dirty
270 * pages could OOM the system if they cannot be written. Also, there
271 * is nobody to return an error code to from here - the application
272 * may not even be running anymore.
274 * Returns the page unlocked, but with a reference.
276 static int ll_writepage(struct page *page) {
277 struct inode *inode = page->mapping->host; int err; ENTRY;
279 LASSERT(PageLocked(page));
281 /* XXX need to make sure we have LDLM lock on this page */
282 err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
284 CERROR("ll_brw failure %d\n", err);
286 set_page_clean(page);
293 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
295 static int ll_commit_write(struct file *file, struct page *page,
296 unsigned from, unsigned to)
298 struct inode *inode = page->mapping->host;
299 struct ll_inode_info *lli = ll_i2info(inode);
300 struct lov_stripe_md *md = lli->lli_smd;
302 struct obd_brw_set *set;
309 /* XXX make the starting offset "from" */
310 pg.off = (((obd_off)page->index) << PAGE_SHIFT);
311 pg.flag = create ? OBD_BRW_CREATE : 0;
313 set = obd_brw_set_new();
317 SetPageUptodate(page);
319 if (!PageLocked(page))
322 CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count %d\n",
325 set->brw_callback = ll_brw_sync_wait;
326 rc = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md, 1, &pg, set, NULL);
328 CERROR("error from obd_brw: rc = %d\n", rc);
330 rc = ll_brw_sync_wait(set, CB_PHASE_START);
332 CERROR("error from callback: rc = %d\n", rc);
334 obd_brw_set_free(set);
337 size = pg.off + pg.count;
338 /* do NOT truncate when writing in the middle of a file */
339 if (size > inode->i_size)
340 inode->i_size = size;
343 } /* ll_commit_write */
346 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
347 unsigned long blocknr, int blocksize)
349 obd_count bufs_per_obdo = iobuf->nr_pages;
350 struct ll_inode_info *lli = ll_i2info(inode);
351 struct lov_stripe_md *lsm = lli->lli_smd;
352 struct brw_page *pga;
353 struct obd_brw_set *set;
357 if (!lsm || !lsm->lsm_object_id)
360 if (blocksize != PAGE_SIZE) {
361 CERROR("direct_IO blocksize != PAGE_SIZE\n");
365 set = obd_brw_set_new();
369 OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
371 obd_brw_set_free(set);
375 /* NB: we can't use iobuf->maplist[i]->index for the offset
376 * instead of "blocknr" because ->index contains garbage.
378 for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
379 pga[i].pg = iobuf->maplist[i];
380 pga[i].count = PAGE_SIZE;
381 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
382 pga[i].flag = OBD_BRW_CREATE;
385 set->brw_callback = ll_brw_sync_wait;
386 rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
387 ll_i2obdconn(inode), lsm, bufs_per_obdo, pga, set, NULL);
389 CERROR("error from obd_brw: rc = %d\n", rc);
391 rc = ll_brw_sync_wait(set, CB_PHASE_START);
393 CERROR("error from callback: rc = %d\n", rc);
395 obd_brw_set_free(set);
397 rc = bufs_per_obdo * PAGE_SIZE;
399 OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
403 int ll_flush_inode_pages(struct inode * inode)
405 obd_count bufs_per_obdo = 0;
406 obd_size *count = NULL;
407 obd_off *offset = NULL;
408 obd_flag *flags = NULL;
413 spin_lock(&pagecache_lock);
415 spin_unlock(&pagecache_lock);
418 OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
419 OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
420 OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
421 if (!count || !offset || !flags)
422 GOTO(out, err=-ENOMEM);
425 for (i = 0 ; i < bufs_per_obdo ; i++) {
426 count[i] = PAGE_SIZE;
427 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
428 flags[i] = OBD_BRW_CREATE;
431 err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
432 ll_i2info(inode)->lli_smd, bufs_per_obdo,
433 iobuf->maplist, count, offset, flags, NULL, NULL);
435 err = bufs_per_obdo * 4096;
438 OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
439 OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
440 OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
447 struct address_space_operations ll_aops = {
448 readpage: ll_readpage,
449 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
450 direct_IO: ll_direct_IO,
451 writepage: ll_writepage,
452 sync_page: block_sync_page,
453 prepare_write: ll_prepare_write,
454 commit_write: ll_commit_write,