1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/config.h>
26 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/locks.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
39 #include <linux/stat.h>
40 #include <asm/uaccess.h>
41 #include <asm/segment.h>
43 #include <linux/pagemap.h>
44 #include <linux/smp_lock.h>
46 #define DEBUG_SUBSYSTEM S_OST
48 #include <linux/obd_class.h>
49 #include <linux/lustre_net.h>
50 #include <linux/lustre_lib.h>
51 #include <linux/lustre_ha.h>
53 static int sync_io_timeout(void *data)
55 struct io_cb_data *cbd = data;
56 struct ptlrpc_bulk_desc *desc = cbd->desc;
59 desc->b_connection->c_level = LUSTRE_CONN_RECOVD;
60 desc->b_flags |= PTL_RPC_FL_TIMEOUT;
61 if (desc->b_client && desc->b_client->cli_recovd) {
62 /* XXXshaver Do we need a resend strategy, or do we just
63 * XXXshaver return -ERESTARTSYS and punt it?
65 CERROR("signalling failure of client %p\n", desc->b_client);
66 class_signal_client_failure(desc->b_client);
69 /* We go back to sleep, until we're resumed or interrupted. */
73 static int sync_io_intr(void *data)
75 struct io_cb_data *cbd = data;
76 struct ptlrpc_bulk_desc *desc = cbd->desc;
79 desc->b_flags |= PTL_RPC_FL_INTR;
80 RETURN(1); /* ignored, as of this writing */
83 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
88 if (phase == CB_PHASE_START) {
89 #warning shaver hardcoded timeout (/proc/sys/lustre/timeout)
90 struct l_wait_info lwi;
91 lwi = LWI_TIMEOUT_INTR(100 * HZ, sync_io_timeout,
92 SIGTERM | SIGKILL | SIGINT, sync_io_intr,
94 ret = l_wait_event(data->waitq, data->complete, &lwi);
95 if (atomic_dec_and_test(&data->refcount))
96 OBD_FREE(data, sizeof(*data));
97 if (ret == -ERESTARTSYS)
99 } else if (phase == CB_PHASE_FINISH) {
102 wake_up(&data->waitq);
103 if (atomic_dec_and_test(&data->refcount))
104 OBD_FREE(data, sizeof(*data));
112 struct io_cb_data *ll_init_cb(void)
114 struct io_cb_data *d;
117 OBD_ALLOC(d, sizeof(*d));
119 init_waitqueue_head(&d->waitq);
120 atomic_set(&d->refcount, 2);
126 * Remove page from dirty list
128 static void __set_page_clean(struct page *page)
130 struct address_space *mapping = page->mapping;
136 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,9))
137 spin_lock(&pagecache_lock);
140 list_del(&page->list);
141 list_add(&page->list, &mapping->clean_pages);
143 inode = mapping->host;
144 if (list_empty(&mapping->dirty_pages)) {
145 CDEBUG(D_INODE, "inode clean\n");
146 inode->i_state &= ~I_DIRTY_PAGES;
148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,10))
149 spin_unlock(&pagecache_lock);
154 inline void set_page_clean(struct page *page)
156 if (PageDirty(page)) {
157 ClearPageDirty(page);
158 __set_page_clean(page);
162 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10))
164 * Add a page to the dirty page list.
166 void __set_page_dirty(struct page *page)
168 struct address_space *mapping;
171 pg_lock = PAGECACHE_LOCK(page);
174 mapping = page->mapping;
175 spin_lock(&mapping->page_lock);
177 list_del(&page->list);
178 list_add(&page->list, &mapping->dirty_pages);
180 spin_unlock(&mapping->page_lock);
181 spin_unlock(pg_lock);
184 mark_inode_dirty_pages(mapping->host);
188 * Add a page to the dirty page list.
190 void set_page_dirty(struct page *page)
192 if (!test_and_set_bit(PG_dirty, &page->flags)) {
193 struct address_space *mapping = page->mapping;
196 spin_lock(&pagecache_lock);
197 list_del(&page->list);
198 list_add(&page->list, &mapping->dirty_pages);
199 spin_unlock(&pagecache_lock);
202 mark_inode_dirty_pages(mapping->host);
208 inline void lustre_put_page(struct page *page)
211 page_cache_release(page);
214 struct page *lustre_get_page_read(struct inode *inode, unsigned long index)
216 struct address_space *mapping = inode->i_mapping;
220 page = read_cache_page(mapping, index,
221 (filler_t*)mapping->a_ops->readpage, NULL);
225 if (!Page_Uptodate(page)) {
226 CERROR("page index %lu not uptodate\n", index);
227 GOTO(err_page, rc = -EIO);
229 if (PageError(page)) {
230 CERROR("page index %lu has error\n", index);
231 GOTO(err_page, rc = -EIO);
237 lustre_put_page(page);
241 struct page *lustre_get_page_write(struct inode *inode, unsigned long index)
243 struct address_space *mapping = inode->i_mapping;
247 page = grab_cache_page(mapping, index); /* locked page */
251 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
252 * a no-op for most filesystems, because we write the whole
253 * page. For partial-page I/O this will read in the page.
255 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
257 CERROR("page index %lu, rc = %d\n", index, rc);
260 GOTO(err_unlock, rc);
262 /* XXX not sure if we need this if we are overwriting page */
263 if (PageError(page)) {
264 CERROR("error on page index %lu, rc = %d\n", index, rc);
266 GOTO(err_unlock, rc = -EIO);
273 lustre_put_page(page);
277 int lustre_commit_write(struct page *page, unsigned from, unsigned to)
279 struct inode *inode = page->mapping->host;
282 SetPageUptodate(page);
283 set_page_clean(page);
285 page->mapping->a_ops->commit_write(NULL, page, from, to);
287 err = waitfor_one_page(page);
289 lustre_put_page(page);