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;
46 obdo = obdo_fromid(IID(inode), inode->i_ino);
52 err = IOPS(inode, brw)(rw, IID(inode), obdo, (char *)page_address(page),
53 &count, (page->index) >> PAGE_SHIFT, create);
56 obdfs_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_pgrq_cachep = NULL;
84 int obdfs_init_pgrqcache(void)
87 if (obdfs_pgrq_cachep == NULL) {
88 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
89 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
90 sizeof(struct obdfs_pgrq),
91 0, SLAB_HWCACHE_ALIGN,
93 if (obdfs_pgrq_cachep == NULL) {
97 CDEBUG(D_INODE, "allocated cache at %p\n",
101 CDEBUG(D_INODE, "using existing cache at %p\n",
106 } /* obdfs_init_wreqcache */
108 void obdfs_cleanup_pgrqcache(void)
111 if (obdfs_pgrq_cachep != NULL) {
112 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
114 if (kmem_cache_destroy(obdfs_pgrq_cachep))
115 printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
117 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
120 } /* obdfs_cleanup_wreqcache */
124 * Find a specific page in the page cache. If it is found, we return
125 * the write request struct associated with it, if not found return NULL.
127 static struct obdfs_pgrq *
128 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
130 struct list_head *page_list = &OBDFS_LIST(inode);
131 struct list_head *tmp;
132 struct obdfs_pgrq *pgrq;
135 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
136 if (list_empty(page_list)) {
137 CDEBUG(D_INODE, "empty list\n");
142 while ( (tmp = tmp->next) != page_list ) {
143 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_list);
144 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
145 if (pgrq->rq_page == page) {
146 CDEBUG(D_INODE, "found page %p in list\n", page);
154 } /* obdfs_find_in_page_cache */
158 * Remove a writeback request from a list
161 obdfs_remove_from_page_cache(struct obdfs_pgrq *pgrq)
163 struct inode *inode = pgrq->rq_inode;
164 struct page *page = pgrq->rq_page;
168 CDEBUG(D_INODE, "writing inode %ld page %p, pgrq: %p\n",
169 inode->i_ino, page, pgrq);
171 PDEBUG(page, "REM_CACHE");
172 err = obdfs_brw(WRITE, inode, page, 1);
173 /* XXX probably should handle error here somehow. I think that
174 * ext2 also does the same thing - discard write even if error?
177 list_del(&pgrq->rq_list);
178 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
183 } /* obdfs_remove_from_page_cache */
186 * Add a page to the write request cache list for later writing
187 * ASYNCHRONOUS write method.
189 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
191 struct obdfs_pgrq *pgrq;
194 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
195 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
196 inode->i_ino, page, pgrq);
201 memset(pgrq, 0, sizeof(*pgrq));
203 pgrq->rq_page = page;
204 pgrq->rq_inode = inode;
206 get_page(pgrq->rq_page);
207 list_add(&pgrq->rq_list, &OBDFS_LIST(inode));
209 /* For testing purposes, we write out the page here.
210 * In the future, a flush daemon will write out the page.
213 pgrq = obdfs_find_in_page_cache(inode, page);
215 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
220 return obdfs_remove_from_page_cache(pgrq);
221 } /* obdfs_add_to_page_cache */
224 /* select between SYNC and ASYNC I/O methods */
225 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
230 PDEBUG(page, "WRITEPAGE");
232 err = obdfs_brw(WRITE, inode, page, 1);
234 err = obdfs_add_to_page_cache(inode, page);
237 SetPageUptodate(page);
238 PDEBUG(page,"WRITEPAGE");
240 } /* obdfs_do_writepage */
242 /* returns the page unlocked, but with a reference */
243 int obdfs_writepage(struct dentry *dentry, struct page *page)
245 return obdfs_do_writepage(dentry->d_inode, page, 0);
249 * This does the "real" work of the write. The generic routine has
250 * allocated the page, locked it, done all the page alignment stuff
251 * calculations etc. Now we should just copy the data from user
252 * space and write it back to the real medium..
254 * If the writer ends up delaying the write, the writer needs to
255 * increment the page use counts until he is done with the page.
257 * Return value is the number of bytes written.
259 int obdfs_write_one_page(struct file *file, struct page *page,
260 unsigned long offset, unsigned long bytes,
263 struct inode *inode = file->f_dentry->d_inode;
267 if ( !Page_Uptodate(page) ) {
268 err = obdfs_brw(READ, inode, page, 1);
270 SetPageUptodate(page);
275 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
279 err = obdfs_writepage(file->f_dentry, page);
282 return (err < 0 ? err : bytes);
283 } /* obdfs_write_one_page */
286 return an up to date page:
287 - if locked is true then is returned locked
288 - if create is true the corresponding disk blocks are created
289 - page is held, i.e. caller must release the page
293 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
295 struct page *page_cache;
302 offset = offset & PAGE_CACHE_MASK;
303 CDEBUG(D_INODE, "\n");
306 page_cache = page_cache_alloc();
307 if ( ! page_cache ) {
311 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
313 hash = page_hash(&inode->i_data, offset);
314 page = grab_cache_page(&inode->i_data, offset);
318 printk("grab_cache_page says no dice ...\n");
323 PDEBUG(page, "GETPAGE: got page - before reading\n");
324 /* now check if the data in the page is up to date */
325 if ( Page_Uptodate(page)) {
332 err = obdfs_brw(READ, inode, page, create);
343 SetPageUptodate(page);
344 PDEBUG(page,"GETPAGE - after reading");
347 } /* obdfs_getpage */