Whamcloud - gitweb
- Fixed some 64bit warnings
[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 inline void lustre_put_page(struct page *page)
89 {
90         kunmap(page);
91         page_cache_release(page);
92 }
93
94 struct page *lustre_get_page_read(struct inode *inode, unsigned long index)
95 {
96         struct address_space *mapping = inode->i_mapping;
97         struct page *page;
98         int rc;
99
100         page = read_cache_page(mapping, index,
101                                (filler_t*)mapping->a_ops->readpage, NULL);
102         if (!IS_ERR(page)) {
103                 wait_on_page(page);
104                 kmap(page);
105                 if (!Page_Uptodate(page)) {
106                         CERROR("page index %lu not uptodate\n", index);
107                         GOTO(err_page, rc = -EIO);
108                 }
109                 if (PageError(page)) {
110                         CERROR("page index %lu has error\n", index);
111                         GOTO(err_page, rc = -EIO);
112                 }
113         }
114         return page;
115
116 err_page:
117         lustre_put_page(page);
118         return ERR_PTR(rc);
119 }
120
121 struct page *lustre_get_page_write(struct inode *inode, unsigned long index)
122 {
123         struct address_space *mapping = inode->i_mapping;
124         struct page *page;
125         int rc;
126
127         page = grab_cache_page(mapping, index); /* locked page */
128
129         if (!IS_ERR(page)) {
130                 kmap(page);
131
132                 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
133                  * a no-op for most filesystems, because we write the whole
134                  * page.  For partial-page I/O this will read in the page.
135                  */
136                 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
137                 if (rc) {
138                         CERROR("page index %lu, rc = %d\n", index, rc);
139                         if (rc != -ENOSPC)
140                                 LBUG();
141                         GOTO(err_unlock, rc);
142                 }
143                 /* XXX not sure if we need this if we are overwriting page */
144                 if (PageError(page)) {
145                         CERROR("error on page index %lu, rc = %d\n", index, rc);
146                         LBUG();
147                         GOTO(err_unlock, rc = -EIO);
148                 }
149         }
150         return page;
151
152 err_unlock:
153         UnlockPage(page);
154         lustre_put_page(page);
155         return ERR_PTR(rc);
156 }
157
158 int lustre_commit_page(struct page *page, unsigned from, unsigned to)
159 {
160         struct inode *inode = page->mapping->host;
161         int err = 0;
162
163         SetPageUptodate(page);
164         set_page_clean(page);
165
166         page->mapping->a_ops->commit_write(NULL, page, from, to);
167         if (IS_SYNC(inode))
168                 err = waitfor_one_page(page);
169         UnlockPage(page);
170         lustre_put_page(page);
171         return err;
172 }