Whamcloud - gitweb
Unlock page in lustre_prepare_page() if there is an error.
[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 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/string.h>
27 #include <linux/stat.h>
28 #include <linux/errno.h>
29 #include <linux/locks.h>
30 #include <linux/unistd.h>
31
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34
35 #include <linux/fs.h>
36 #include <linux/stat.h>
37 #include <asm/uaccess.h>
38 #include <asm/segment.h>
39 #include <linux/mm.h>
40 #include <linux/pagemap.h>
41 #include <linux/smp_lock.h>
42
43 #define DEBUG_SUBSYSTEM S_OST
44
45 #include <linux/obd_class.h>
46 #include <linux/lustre_lib.h>
47
48 /*
49  * Remove page from dirty list
50  */
51 static void __set_page_clean(struct page *page)
52 {
53         struct address_space *mapping = page->mapping;
54         struct inode *inode;
55         
56         if (!mapping)
57                 return;
58
59         spin_lock(&pagecache_lock);
60         list_del(&page->list);
61         list_add(&page->list, &mapping->clean_pages);
62
63         inode = mapping->host;
64         if (list_empty(&mapping->dirty_pages)) { 
65                 CDEBUG(D_INODE, "inode clean\n");
66                 inode->i_state &= ~I_DIRTY_PAGES;
67         }
68         spin_unlock(&pagecache_lock);
69         EXIT;
70 }
71
72 inline void set_page_clean(struct page *page)
73 {
74         if (PageDirty(page)) { 
75                 ClearPageDirty(page);
76                 __set_page_clean(page);
77         }
78 }
79
80 inline void lustre_put_page(struct page *page)
81 {
82         kunmap(page);
83         page_cache_release(page);
84 }
85
86 struct page * lustre_get_page(struct inode *inode, unsigned long n)
87 {
88         struct address_space *mapping = inode->i_mapping;
89         struct page *page = read_cache_page(mapping, n,
90                                 (filler_t*)mapping->a_ops->readpage, NULL);
91         if (!IS_ERR(page)) {
92                 wait_on_page(page);
93                 kmap(page);
94                 if (!Page_Uptodate(page))
95                         goto fail;
96                 if (PageError(page))
97                         goto fail;
98         }
99         return page;
100
101 fail:
102         lustre_put_page(page);
103         return ERR_PTR(-EIO);
104 }
105
106 int lustre_prepare_page(unsigned from, unsigned to, struct page *page)
107 {
108         int err;
109
110         lock_page(page);
111         err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
112         if (err) {
113                 unlock_page(page);
114                 CERROR("page index %ld from %d to %d err %d\n",
115                                 page->index, from, to, err);
116         }
117         return err;
118 }
119
120 int lustre_commit_page(struct page *page, unsigned from, unsigned to)
121 {
122         struct inode *inode = page->mapping->host;
123         int err = 0;
124
125         SetPageUptodate(page);
126         set_page_clean(page);
127
128         page->mapping->a_ops->commit_write(NULL, page, from, to);
129         if (IS_SYNC(inode))
130                 err = waitfor_one_page(page);
131         UnlockPage(page);
132         lustre_put_page(page);
133         return err;
134 }