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