2 * OBDFS Super operations
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.
11 #include <linux/config.h>
12 #include <linux/kernel.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>
20 #include <asm/system.h>
21 #include <asm/uaccess.h>
24 #include <linux/stat.h>
25 #include <asm/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <asm/segment.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp_lock.h>
32 #include <linux/obd_support.h>
33 #include <linux/obd_ext2.h>
34 #include <linux/obdfs.h>
38 /* SYNCHRONOUS I/O for an inode */
39 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
42 obd_size count = PAGE_SIZE;
52 obdo->o_id = inode->i_ino;
54 err = IOPS(inode, brw)(rw, IID(inode), obdo, (char *)page_address(page),
55 &count, (page->index) >> PAGE_SHIFT, create);
57 obdo_to_inode(inode, obdo); /* copy o_blocks to i_blocks */
64 /* returns the page unlocked, but with a reference */
65 int obdfs_readpage(struct dentry *dentry, struct page *page)
67 struct inode *inode = dentry->d_inode;
72 rc = obdfs_brw(READ, inode, page, 0);
74 SetPageUptodate(page);
80 } /* obdfs_readpage */
82 static kmem_cache_t *obdfs_wreq_cachep = NULL;
84 int obdfs_init_wreqcache(void)
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,
93 if (obdfs_wreq_cachep == NULL) {
100 } /* obdfs_init_wreqcache */
102 void obdfs_cleanup_wreqcache(void)
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");
109 printk(KERN_ERR "obdfs_cleanup_wreqcache: called with NULL cache pointer\n");
112 } /* obdfs_cleanup_wreqcache */
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.
119 static struct obdfs_wreq *obdfs_find_in_page_cache(struct inode *inode,
122 struct list_head *list_head = &OBD_LIST(inode);
123 struct obdfs_wreq *head, *wreq;
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");
132 wreq = head = WREQ(list_head->next);
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);
140 } while ((wreq = WB_NEXT(wreq)) != head);
144 } /* obdfs_find_in_page_cache */
148 * Remove a writeback request from a list
150 static inline int obdfs_remove_from_page_cache(struct obdfs_wreq *wreq)
152 struct inode *inode = wreq->wb_inode;
153 struct page *page = wreq->wb_page;
157 CDEBUG(D_INODE, "removing inode %ld, wreq: %p\n",
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?
165 list_del(&wreq->wb_list);
166 kmem_cache_free(obdfs_wreq_cachep, wreq);
170 } /* obdfs_remove_from_page_cache */
173 * Add a page to the write request cache list for later writing
175 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
177 struct obdfs_wreq *wreq;
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);
187 memset(wreq, 0, sizeof(*wreq));
189 wreq->wb_page = page;
190 wreq->wb_inode = inode;
192 get_page(wreq->wb_page);
193 list_add(&wreq->wb_list, &OBD_LIST(inode));
195 /* For testing purposes, we write out the page here.
196 * In the future, a flush daemon will write out the page.
199 printk(KERN_INFO "finding page in cache for write\n");
200 wreq = obdfs_find_in_page_cache(inode, page);
202 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
208 return obdfs_remove_from_page_cache(wreq);
209 } /* obdfs_add_to_page_cache */
212 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
217 PDEBUG(page, "WRITEPAGE");
219 err = obdfs_brw(WRITE, inode, page, 1);
221 err = obdfs_add_to_page_cache(inode, page);
224 SetPageUptodate(page);
225 PDEBUG(page,"WRITEPAGE");
227 } /* obdfs_do_writepage */
229 /* returns the page unlocked, but with a reference */
230 int obdfs_writepage(struct dentry *dentry, struct page *page)
232 return obdfs_do_writepage(dentry->d_inode, page, 0);
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..
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.
244 int obdfs_write_one_page(struct file *file, struct page *page,
245 unsigned long offset, unsigned long bytes,
248 struct inode *inode = file->f_dentry->d_inode;
252 if ( !Page_Uptodate(page) ) {
253 err = obdfs_brw(READ, inode, page, 1);
255 SetPageUptodate(page);
259 bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
264 err = obdfs_writepage(file->f_dentry, page);
269 } /* obdfs_write_one_page */
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
279 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
281 struct page *page_cache;
288 offset = offset & PAGE_CACHE_MASK;
289 CDEBUG(D_INODE, "\n");
292 page_cache = page_cache_alloc();
293 if ( ! page_cache ) {
297 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
299 hash = page_hash(&inode->i_data, offset);
300 page = grab_cache_page(&inode->i_data, offset);
304 printk("grab_cache_page says no dice ...\n");
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)) {
318 err = obdfs_brw(READ, inode, page, create);
329 SetPageUptodate(page);
330 PDEBUG(page,"GETPAGE - after reading");
333 } /* obdfs_getpage */