Whamcloud - gitweb
Fixing brw page lock issue.
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
5  * Copryright (C) 1999 Stelias Computing Inc, 
6  *                (author Peter J. Braam <braam@stelias.com>)
7  * Copryright (C) 1999 Seagate Technology Inc.
8  */
9
10
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/locks.h>
18 #include <linux/unistd.h>
19
20 #include <asm/system.h>
21 #include <asm/uaccess.h>
22
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <asm/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <asm/segment.h>
28 #include <linux/mm.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp_lock.h>
31
32 #include <linux/obd_support.h>
33 #include <linux/obd_ext2.h>
34 #include <linux/obdfs.h>
35
36 int console_loglevel;
37
38 /* SYNCHRONOUS I/O for an inode */
39 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
40 {
41         struct obdo *obdo;
42         obd_size count = PAGE_SIZE;
43         int err;
44
45         ENTRY;
46         obdo = obdo_alloc();
47         if ( ! obdo ) {
48                 EXIT;
49                 return -ENOMEM;
50         }
51
52         obdo->o_id = inode->i_ino;
53
54         err = IOPS(inode, brw)(rw, IID(inode), obdo, (char *)page_address(page),
55                                &count, (page->index) >> PAGE_SHIFT, create);
56
57         obdo_to_inode(inode, obdo); /* copy o_blocks to i_blocks */
58         obdo_free(obdo);
59         
60         EXIT;
61         return err;
62 } /* obdfs_brw */
63
64 /* returns the page unlocked, but with a reference */
65 int obdfs_readpage(struct dentry *dentry, struct page *page)
66 {
67         struct inode *inode = dentry->d_inode;
68         int rc;
69
70         ENTRY;
71         PDEBUG(page, "READ");
72         rc = obdfs_brw(READ, inode, page, 0);
73         if ( !rc ) {
74                 SetPageUptodate(page);
75                 UnlockPage(page);
76         } 
77         PDEBUG(page, "READ");
78         EXIT;
79         return rc;
80 } /* obdfs_readpage */
81
82 static kmem_cache_t *obdfs_wreq_cachep = NULL;
83
84 int obdfs_init_wreqcache(void)
85 {
86         ENTRY;
87
88         if (obdfs_wreq_cachep == NULL) {
89                 obdfs_wreq_cachep = kmem_cache_create("obdfs_wreq",
90                                                       sizeof(struct obdfs_wreq),
91                                                       0, SLAB_HWCACHE_ALIGN,
92                                                       NULL, NULL);
93                 if (obdfs_wreq_cachep == NULL) {
94                         EXIT;
95                         return -ENOMEM;
96                 }
97         }
98         EXIT;
99         return 0;
100 } /* obdfs_init_wreqcache */
101
102 void obdfs_cleanup_wreqcache(void)
103 {
104         ENTRY;
105         if (obdfs_wreq_cachep != NULL) {
106                 if (kmem_cache_shrink(obdfs_wreq_cachep))
107                         printk(KERN_INFO "obdfs_cleanup_wreqcache: unable to free all of cache\n");
108         } else
109                 printk(KERN_ERR "obdfs_cleanup_wreqcache: called with NULL cache pointer\n");
110         
111         EXIT;
112 } /* obdfs_cleanup_wreqcache */
113
114
115 /*
116  * Find a specific page in the page cache.  If it is found, we return
117  * the write request struct associated with it, if not found return NULL.
118  */
119 static struct obdfs_wreq *obdfs_find_in_page_cache(struct inode *inode,
120                                                    struct page *page)
121 {
122         struct list_head *list_head = &OBD_LIST(inode);
123         struct obdfs_wreq *head, *wreq;
124
125         ENTRY;
126         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
127         if (list_empty(list_head)) {
128                 CDEBUG(D_INODE, "empty list\n");
129                 EXIT;
130                 return NULL;
131         }
132         wreq = head = WREQ(list_head->next);
133         do {
134                 CDEBUG(D_INODE, "checking page %p\n", wreq->wb_page);
135                 if (wreq->wb_page == page) {
136                         CDEBUG(D_INODE, "found page %p in list\n", page);
137                         EXIT;
138                         return wreq;
139                 }
140         } while ((wreq = WB_NEXT(wreq)) != head);
141
142         EXIT;
143         return NULL;
144 } /* obdfs_find_in_page_cache */
145
146
147 /*
148  * Remove a writeback request from a list
149  */
150 static inline int obdfs_remove_from_page_cache(struct obdfs_wreq *wreq)
151 {
152         struct inode *inode = wreq->wb_inode;
153         struct page *page = wreq->wb_page;
154         int rc;
155
156         ENTRY;
157         CDEBUG(D_INODE, "removing inode %ld, wreq: %p\n",
158                inode->i_ino, wreq);
159         PDEBUG(page, "REM_CACHE");
160         rc = obdfs_brw(WRITE, inode, page, 1);
161         /* XXX probably should handle error here somehow.  I think that
162          *     ext2 also does the same thing - discard write even if error?
163          */
164         put_page(page);
165         list_del(&wreq->wb_list);
166         kmem_cache_free(obdfs_wreq_cachep, wreq);
167
168         EXIT;
169         return rc;
170 } /* obdfs_remove_from_page_cache */
171
172 /*
173  * Add a page to the write request cache list for later writing
174  */
175 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
176 {
177         struct obdfs_wreq *wreq;
178
179         ENTRY;
180         wreq = kmem_cache_alloc(obdfs_wreq_cachep, SLAB_KERNEL);
181         CDEBUG(D_INODE, "adding inode %ld page %p, wreq: %p\n",
182                inode->i_ino, page, wreq);
183         if (!wreq) {
184                 EXIT;
185                 return -ENOMEM;
186         }
187         memset(wreq, 0, sizeof(*wreq)); 
188
189         wreq->wb_page = page;
190         wreq->wb_inode = inode;
191
192         get_page(wreq->wb_page);
193         list_add(&wreq->wb_list, &OBD_LIST(inode));
194
195         /* For testing purposes, we write out the page here.
196          * In the future, a flush daemon will write out the page.
197         return 0;
198          */
199         printk(KERN_INFO "finding page in cache for write\n");
200         wreq = obdfs_find_in_page_cache(inode, page);
201         if (!wreq) {
202                 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
203                 EXIT;
204                 return -EINVAL;
205         }
206
207         EXIT;
208         return obdfs_remove_from_page_cache(wreq);
209 } /* obdfs_add_to_page_cache */
210
211
212 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
213 {
214         int err;
215
216         ENTRY;
217         PDEBUG(page, "WRITEPAGE");
218         if ( sync )
219                 err = obdfs_brw(WRITE, inode, page, 1);
220         else
221                 err = obdfs_add_to_page_cache(inode, page);
222                 
223         if ( !err )
224                 SetPageUptodate(page);
225         PDEBUG(page,"WRITEPAGE");
226         return err;
227 } /* obdfs_do_writepage */
228
229 /* returns the page unlocked, but with a reference */
230 int obdfs_writepage(struct dentry *dentry, struct page *page)
231 {
232         return obdfs_do_writepage(dentry->d_inode, page, 0);
233 }
234
235 /*
236  * This does the "real" work of the write. The generic routine has
237  * allocated the page, locked it, done all the page alignment stuff
238  * calculations etc. Now we should just copy the data from user
239  * space and write it back to the real medium..
240  *
241  * If the writer ends up delaying the write, the writer needs to
242  * increment the page use counts until he is done with the page.
243  */
244 int obdfs_write_one_page(struct file *file, struct page *page,
245                          unsigned long offset, unsigned long bytes,
246                          const char * buf)
247 {
248         struct inode *inode = file->f_dentry->d_inode;
249         int err;
250
251         ENTRY;
252         if ( !Page_Uptodate(page) ) {
253                 err = obdfs_brw(READ, inode, page, 1);
254                 if ( !err )
255                         SetPageUptodate(page);
256                 else
257                         return err;
258         }
259         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
260         err = -EFAULT;
261
262         if (bytes) {
263                 lock_kernel();
264                 err = obdfs_writepage(file->f_dentry, page);
265                 unlock_kernel();
266         }
267
268         return err;
269 } /* obdfs_write_one_page */
270
271 /* 
272    return an up to date page:
273     - if locked is true then is returned locked
274     - if create is true the corresponding disk blocks are created 
275     - page is held, i.e. caller must release the page
276
277    modeled on NFS code.
278 */
279 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
280 {
281         struct page *page_cache;
282         struct page ** hash;
283         struct page * page;
284         int err;
285
286         ENTRY;
287
288         offset = offset & PAGE_CACHE_MASK;
289         CDEBUG(D_INODE, "\n");
290         
291         page = NULL;
292         page_cache = page_cache_alloc();
293         if ( ! page_cache ) {
294                 EXIT;
295                 return NULL;
296         }
297         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
298
299         hash = page_hash(&inode->i_data, offset);
300         page = grab_cache_page(&inode->i_data, offset);
301
302         /* Yuck, no page */
303         if (! page) {
304             printk("grab_cache_page says no dice ...\n");
305             EXIT;
306             return 0;
307         }
308
309         PDEBUG(page, "GETPAGE: got page - before reading\n");
310         /* now check if the data in the page is up to date */
311         if ( Page_Uptodate(page)) { 
312                 if (!locked)
313                         UnlockPage(page);
314                 EXIT;
315                 return page;
316         } 
317
318         err = obdfs_brw(READ, inode, page, create);
319
320         if ( err ) {
321                 SetPageError(page);
322                 UnlockPage(page);
323                 EXIT;
324                 return page;
325         }
326
327         if ( !locked )
328                 UnlockPage(page);
329         SetPageUptodate(page);
330         PDEBUG(page,"GETPAGE - after reading");
331         EXIT;
332         return page;
333 } /* obdfs_getpage */
334
335