Whamcloud - gitweb
This commit contains probably 92% of the striping infrastructure
[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
52 /*
53  * Remove page from dirty list
54  */
55 static void __set_page_clean(struct page *page)
56 {
57         struct address_space *mapping = page->mapping;
58         struct inode *inode;
59         
60         if (!mapping)
61                 return;
62
63 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,9))
64         spin_lock(&pagecache_lock);
65 #endif
66
67         list_del(&page->list);
68         list_add(&page->list, &mapping->clean_pages);
69
70         inode = mapping->host;
71         if (list_empty(&mapping->dirty_pages)) { 
72                 CDEBUG(D_INODE, "inode clean\n");
73                 inode->i_state &= ~I_DIRTY_PAGES;
74         }
75 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,10))
76         spin_unlock(&pagecache_lock);
77 #endif
78         EXIT;
79 }
80
81 inline void set_page_clean(struct page *page)
82 {
83         if (PageDirty(page)) { 
84                 ClearPageDirty(page);
85                 __set_page_clean(page);
86         }
87 }
88
89 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10))
90 /*
91  * Add a page to the dirty page list.
92  */
93 void __set_page_dirty(struct page *page)
94 {
95         struct address_space *mapping;
96         spinlock_t *pg_lock;
97
98         pg_lock = PAGECACHE_LOCK(page);
99         spin_lock(pg_lock);
100
101         mapping = page->mapping;
102         spin_lock(&mapping->page_lock);
103
104         list_del(&page->list);
105         list_add(&page->list, &mapping->dirty_pages);
106
107         spin_unlock(&mapping->page_lock);
108         spin_unlock(pg_lock);
109
110         if (mapping->host)
111                 mark_inode_dirty_pages(mapping->host);
112 }
113 #else
114 /*
115  * Add a page to the dirty page list.
116  */
117 void set_page_dirty(struct page *page)
118 {
119         if (!test_and_set_bit(PG_dirty, &page->flags)) {
120                 struct address_space *mapping = page->mapping;
121
122                 if (mapping) {
123                         spin_lock(&pagecache_lock);
124                         list_del(&page->list);
125                         list_add(&page->list, &mapping->dirty_pages);
126                         spin_unlock(&pagecache_lock);
127
128                         if (mapping->host)
129                                 mark_inode_dirty_pages(mapping->host);
130                 }
131         }
132 }
133 #endif
134
135 inline void lustre_put_page(struct page *page)
136 {
137         kunmap(page);
138         page_cache_release(page);
139 }
140
141 struct page *lustre_get_page_read(struct inode *inode, unsigned long index)
142 {
143         struct address_space *mapping = inode->i_mapping;
144         struct page *page;
145         int rc;
146
147         page = read_cache_page(mapping, index,
148                                (filler_t*)mapping->a_ops->readpage, NULL);
149         if (!IS_ERR(page)) {
150                 wait_on_page(page);
151                 kmap(page);
152                 if (!Page_Uptodate(page)) {
153                         CERROR("page index %lu not uptodate\n", index);
154                         GOTO(err_page, rc = -EIO);
155                 }
156                 if (PageError(page)) {
157                         CERROR("page index %lu has error\n", index);
158                         GOTO(err_page, rc = -EIO);
159                 }
160         }
161         return page;
162
163 err_page:
164         lustre_put_page(page);
165         return ERR_PTR(rc);
166 }
167
168 struct page *lustre_get_page_write(struct inode *inode, unsigned long index)
169 {
170         struct address_space *mapping = inode->i_mapping;
171         struct page *page;
172         int rc;
173
174         page = grab_cache_page(mapping, index); /* locked page */
175
176         if (!IS_ERR(page)) {
177                 kmap(page);
178                 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
179                  * a no-op for most filesystems, because we write the whole
180                  * page.  For partial-page I/O this will read in the page.
181                  */
182                 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
183                 if (rc) {
184                         CERROR("page index %lu, rc = %d\n", index, rc);
185                         if (rc != -ENOSPC)
186                                 LBUG();
187                         GOTO(err_unlock, rc);
188                 }
189                 /* XXX not sure if we need this if we are overwriting page */
190                 if (PageError(page)) {
191                         CERROR("error on page index %lu, rc = %d\n", index, rc);
192                         LBUG();
193                         GOTO(err_unlock, rc = -EIO);
194                 }
195         }
196         return page;
197
198 err_unlock:
199         unlock_page(page);
200         lustre_put_page(page);
201         return ERR_PTR(rc);
202 }
203
204 int lustre_commit_write(struct page *page, unsigned from, unsigned to)
205 {
206         struct inode *inode = page->mapping->host;
207         int err = 0;
208
209         SetPageUptodate(page);
210         set_page_clean(page);
211
212         page->mapping->a_ops->commit_write(NULL, page, from, to);
213         if (IS_SYNC(inode))
214                 err = waitfor_one_page(page);
215         UnlockPage(page);
216         lustre_put_page(page);
217         return err;
218 }