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 int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
50 obdo->o_id = inode->i_ino;
52 res = IOPS(inode, brw)(rw, IID(inode), obdo,
53 (char *)page_address(page),
55 (page->index) >> PAGE_SHIFT,
58 obdo_to_inode(inode, obdo); /* copy o_blocks to i_blocks */
61 if ( res == PAGE_SIZE )
66 /* returns the page unlocked, but with a reference */
67 int obdfs_readpage(struct dentry *dentry, struct page *page)
69 struct inode *inode = dentry->d_inode;
74 rc = obdfs_brw(READ, inode, page, 0);
76 SetPageUptodate(page);
84 static kmem_cache_t *obdfs_wreq_cachep;
86 int obdfs_init_wreqcache(void)
88 /* XXX need to free this somewhere? */
90 obdfs_wreq_cachep = kmem_cache_create("obdfs_wreq",
91 sizeof(struct obdfs_wreq),
92 0, SLAB_HWCACHE_ALIGN,
94 if (obdfs_wreq_cachep == NULL) {
102 void obdfs_cleanup_wreqcache(void)
104 if (obdfs_wreq_cachep != NULL)
105 kmem_cache_destroy(obdfs_wreq_cachep);
107 obdfs_wreq_cachep = NULL;
112 * Find a specific page in the page cache. If it is found, we return
113 * the write request struct associated with it, if not found return NULL.
115 static struct obdfs_wreq *
116 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
118 struct list_head *list_head = &OBD_LIST(inode);
119 struct obdfs_wreq *head, *wreq;
122 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
123 if (list_empty(list_head)) {
124 CDEBUG(D_INODE, "empty list\n");
128 wreq = head = WREQ(list_head->next);
130 CDEBUG(D_INODE, "checking page %p\n", wreq->wb_page);
131 if (wreq->wb_page == page) {
132 CDEBUG(D_INODE, "found page %p in list\n", page);
136 } while ((wreq = WB_NEXT(wreq)) != head);
144 * Remove a writeback request from a list
147 obdfs_remove_from_page_cache(struct obdfs_wreq *wreq)
149 struct inode *inode = wreq->wb_inode;
150 struct page *page = wreq->wb_page;
154 CDEBUG(D_INODE, "removing inode %ld page %p, wreq: %p\n",
155 inode->i_ino, page, wreq);
156 rc = obdfs_brw(WRITE, inode, page, 1);
157 /* XXX probably should handle error here somehow. I think that
158 * ext2 also does the same thing - discard write even if error?
161 list_del(&wreq->wb_list);
162 kmem_cache_free(obdfs_wreq_cachep, wreq);
169 * Add a page to the write request cache list for later writing
172 obdfs_add_to_page_cache(struct inode *inode, struct page *page)
174 struct obdfs_wreq *wreq;
177 wreq = kmem_cache_alloc(obdfs_wreq_cachep, SLAB_KERNEL);
178 CDEBUG(D_INODE, "adding inode %ld page %p, wreq: %p\n",
179 inode->i_ino, page, wreq);
184 memset(wreq, 0, sizeof(*wreq));
186 wreq->wb_page = page;
187 wreq->wb_inode = inode;
189 CDEBUG(D_INODE, "getting page %p\n", wreq->wb_page);
190 get_page(wreq->wb_page);
191 CDEBUG(D_INODE, "adding wreq %p to inode %p\n", wreq, inode);
192 { struct obdfs_inode_info *oinfo = OBD_INFO(inode);
193 CDEBUG(D_INODE, "generic is %p\n", inode->u.generic_ip);
194 CDEBUG(D_INODE, "oinfo is %p\n", oinfo);
196 CDEBUG(D_INODE, "wreq_list %p\n", &wreq->wb_list);
198 CDEBUG(D_INODE, "inode_list: next %p, prev %p\n", OBD_LIST(inode).next,
199 OBD_LIST(inode).prev);
200 CDEBUG(D_INODE, "inode_list_addr: %p\n", &OBD_LIST(inode));
202 list_add(&wreq->wb_list, &OBD_LIST(inode));
204 /* For testing purposes, we write out the page here.
205 * In the future, a flush daemon will write out the page.
207 printk(KERN_INFO "finding page in cache for write\n");
208 wreq = obdfs_find_in_page_cache(inode, page);
210 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
216 return obdfs_remove_from_page_cache(wreq);
220 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
225 PDEBUG(page, "WRITEPAGE");
227 rc = obdfs_brw(WRITE, inode, page, 1);
229 /* XXX flush stuff */
230 rc = obdfs_add_to_page_cache(inode, page);
234 SetPageUptodate(page);
235 PDEBUG(page,"WRITEPAGE");
239 /* returns the page unlocked, but with a reference */
240 int obdfs_writepage(struct dentry *dentry, struct page *page)
242 return obdfs_do_writepage(dentry->d_inode, page, 0);
246 * This does the "real" work of the write. The generic routine has
247 * allocated the page, locked it, done all the page alignment stuff
248 * calculations etc. Now we should just copy the data from user
249 * space and write it back to the real medium..
251 * If the writer ends up delaying the write, the writer needs to
252 * increment the page use counts until he is done with the page.
254 int obdfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
257 struct inode *inode = file->f_dentry->d_inode;
260 if ( !Page_Uptodate(page) ) {
261 status = obdfs_brw(READ, inode, page, 1);
263 SetPageUptodate(page);
268 bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
273 status = obdfs_writepage(file->f_dentry, page);
277 if ( status != PAGE_SIZE )
284 return an up to date page:
285 - if locked is true then is returned locked
286 - if create is true the corresponding disk blocks are created
287 - page is held, i.e. caller must release the page
291 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
293 struct page *page_cache;
300 offset = offset & PAGE_CACHE_MASK;
301 CDEBUG(D_INODE, "\n");
304 page_cache = page_cache_alloc();
307 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
309 hash = page_hash(&inode->i_data, offset);
310 page = grab_cache_page(&inode->i_data, offset);
314 printk("grab_cache_page says no dice ...\n");
318 PDEBUG(page, "GETPAGE: got page - before reading\n");
319 /* now check if the data in the page is up to date */
320 if ( Page_Uptodate(page)) {
327 rc = obdfs_brw(READ, inode, page, create);
329 if ( rc != PAGE_SIZE ) {
337 SetPageUptodate(page);
338 PDEBUG(page,"GETPAGE - after reading");