Whamcloud - gitweb
ddcd5de6535534f2514277dae56e95a00c203326
[fs/lustre-release.git] / lustre / lib / page.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23
24
25 #include <linux/config.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.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>
34
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37
38 #include <linux/fs.h>
39 #include <linux/stat.h>
40 #include <asm/uaccess.h>
41 #include <asm/segment.h>
42 #include <linux/mm.h>
43 #include <linux/pagemap.h>
44 #include <linux/smp_lock.h>
45
46 #define DEBUG_SUBSYSTEM S_OST
47
48 #include <linux/obd_class.h>
49 #include <linux/lustre_net.h>
50 #include <linux/lustre_lib.h>
51 #include <linux/lustre_ha.h>
52
53 static int sync_io_timeout(void *data)
54 {
55         struct io_cb_data *cbd = data;
56         struct ptlrpc_bulk_desc *desc = cbd->desc;
57
58         ENTRY;
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?
64                  */
65                 CERROR("signalling failure of client %p\n", desc->b_client);
66                 class_signal_client_failure(desc->b_client);
67         }
68
69         /* We go back to sleep, until we're resumed or interrupted. */
70         RETURN(0);
71 }
72
73 static int sync_io_intr(void *data)
74 {
75         struct io_cb_data *cbd = data;
76         struct ptlrpc_bulk_desc *desc = cbd->desc;
77
78         ENTRY;
79         desc->b_flags |= PTL_RPC_FL_INTR;
80         RETURN(1); /* ignored, as of this writing */
81 }
82
83 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
84 {
85         int ret;
86         ENTRY; 
87
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,
93                                        data);
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)
98                         return ret;
99         } else if (phase == CB_PHASE_FINISH) { 
100                 data->err = err;
101                 data->complete = 1;
102                 wake_up(&data->waitq); 
103                 if (atomic_dec_and_test(&data->refcount))
104                         OBD_FREE(data, sizeof(*data));
105                 return err;
106         } else 
107                 LBUG();
108         EXIT;
109         return 0;
110 }
111
112 struct io_cb_data *ll_init_cb(void)
113 {
114         struct io_cb_data *d;
115
116
117         OBD_ALLOC(d, sizeof(*d));
118         if (d) { 
119                 init_waitqueue_head(&d->waitq);
120                 atomic_set(&d->refcount, 2);
121         }
122         RETURN(d); 
123 }
124
125 /*
126  * Remove page from dirty list
127  */
128 static void __set_page_clean(struct page *page)
129 {
130         struct address_space *mapping = page->mapping;
131         struct inode *inode;
132         
133         if (!mapping)
134                 return;
135
136 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,9))
137         spin_lock(&pagecache_lock);
138 #endif
139
140         list_del(&page->list);
141         list_add(&page->list, &mapping->clean_pages);
142
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;
147         }
148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,10))
149         spin_unlock(&pagecache_lock);
150 #endif
151         EXIT;
152 }
153
154 inline void set_page_clean(struct page *page)
155 {
156         if (PageDirty(page)) { 
157                 ClearPageDirty(page);
158                 __set_page_clean(page);
159         }
160 }
161
162 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10))
163 /*
164  * Add a page to the dirty page list.
165  */
166 void __set_page_dirty(struct page *page)
167 {
168         struct address_space *mapping;
169         spinlock_t *pg_lock;
170
171         pg_lock = PAGECACHE_LOCK(page);
172         spin_lock(pg_lock);
173
174         mapping = page->mapping;
175         spin_lock(&mapping->page_lock);
176
177         list_del(&page->list);
178         list_add(&page->list, &mapping->dirty_pages);
179
180         spin_unlock(&mapping->page_lock);
181         spin_unlock(pg_lock);
182
183         if (mapping->host)
184                 mark_inode_dirty_pages(mapping->host);
185 }
186 #else
187 /*
188  * Add a page to the dirty page list.
189  */
190 void set_page_dirty(struct page *page)
191 {
192         if (!test_and_set_bit(PG_dirty, &page->flags)) {
193                 struct address_space *mapping = page->mapping;
194
195                 if (mapping) {
196                         spin_lock(&pagecache_lock);
197                         list_del(&page->list);
198                         list_add(&page->list, &mapping->dirty_pages);
199                         spin_unlock(&pagecache_lock);
200
201                         if (mapping->host)
202                                 mark_inode_dirty_pages(mapping->host);
203                 }
204         }
205 }
206 #endif
207
208 inline void lustre_put_page(struct page *page)
209 {
210         kunmap(page);
211         page_cache_release(page);
212 }
213
214 struct page *lustre_get_page_read(struct inode *inode, unsigned long index)
215 {
216         struct address_space *mapping = inode->i_mapping;
217         struct page *page;
218         int rc;
219
220         page = read_cache_page(mapping, index,
221                                (filler_t*)mapping->a_ops->readpage, NULL);
222         if (!IS_ERR(page)) {
223                 wait_on_page(page);
224                 kmap(page);
225                 if (!Page_Uptodate(page)) {
226                         CERROR("page index %lu not uptodate\n", index);
227                         GOTO(err_page, rc = -EIO);
228                 }
229                 if (PageError(page)) {
230                         CERROR("page index %lu has error\n", index);
231                         GOTO(err_page, rc = -EIO);
232                 }
233         }
234         return page;
235
236 err_page:
237         lustre_put_page(page);
238         return ERR_PTR(rc);
239 }
240
241 struct page *lustre_get_page_write(struct inode *inode, unsigned long index)
242 {
243         struct address_space *mapping = inode->i_mapping;
244         struct page *page;
245         int rc;
246
247         page = grab_cache_page(mapping, index); /* locked page */
248
249         if (!IS_ERR(page)) {
250                 kmap(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.
254                  */
255                 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
256                 if (rc) {
257                         CERROR("page index %lu, rc = %d\n", index, rc);
258                         if (rc != -ENOSPC)
259                                 LBUG();
260                         GOTO(err_unlock, rc);
261                 }
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);
265                         LBUG();
266                         GOTO(err_unlock, rc = -EIO);
267                 }
268         }
269         return page;
270
271 err_unlock:
272         unlock_page(page);
273         lustre_put_page(page);
274         return ERR_PTR(rc);
275 }
276
277 int lustre_commit_write(struct page *page, unsigned from, unsigned to)
278 {
279         struct inode *inode = page->mapping->host;
280         int err = 0;
281
282         SetPageUptodate(page);
283         set_page_clean(page);
284
285         page->mapping->a_ops->commit_write(NULL, page, from, to);
286         if (IS_SYNC(inode))
287                 err = waitfor_one_page(page);
288         UnlockPage(page);
289         lustre_put_page(page);
290         return err;
291 }