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);
58 obdo_to_inode(inode, obdo); /* copy o_blocks to i_blocks */
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);
82 } /* obdfs_readpage */
84 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
86 int obdfs_init_pgrqcache(void)
89 if (obdfs_pgrq_cachep == NULL) {
90 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
91 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
92 sizeof(struct obdfs_pgrq),
93 0, SLAB_HWCACHE_ALIGN,
95 if (obdfs_pgrq_cachep == NULL) {
99 CDEBUG(D_INODE, "allocated cache at %p\n",
103 CDEBUG(D_INODE, "using existing cache at %p\n",
108 } /* obdfs_init_wreqcache */
110 void obdfs_cleanup_pgrqcache(void)
113 if (obdfs_pgrq_cachep != NULL) {
114 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
116 if (kmem_cache_destroy(obdfs_pgrq_cachep))
117 printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
119 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
122 } /* obdfs_cleanup_wreqcache */
126 * Find a specific page in the page cache. If it is found, we return
127 * the write request struct associated with it, if not found return NULL.
129 static struct obdfs_pgrq *
130 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
132 struct list_head *page_list = &OBDFS_LIST(inode);
133 struct list_head *tmp;
134 struct obdfs_pgrq *pgrq;
137 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
138 if (list_empty(page_list)) {
139 CDEBUG(D_INODE, "empty list\n");
144 while ( (tmp = tmp->next) != page_list ) {
145 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_list);
146 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
147 if (pgrq->rq_page == page) {
148 CDEBUG(D_INODE, "found page %p in list\n", page);
156 } /* obdfs_find_in_page_cache */
160 * Remove a writeback request from a list
163 obdfs_remove_from_page_cache(struct obdfs_pgrq *pgrq)
165 struct inode *inode = pgrq->rq_inode;
166 struct page *page = pgrq->rq_page;
170 CDEBUG(D_INODE, "removing inode %ld page %p, pgrq: %p\n",
171 inode->i_ino, page, pgrq);
173 PDEBUG(page, "REM_CACHE");
174 err = obdfs_brw(WRITE, inode, page, 1);
175 /* XXX probably should handle error here somehow. I think that
176 * ext2 also does the same thing - discard write even if error?
179 list_del(&pgrq->rq_list);
180 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
185 } /* obdfs_remove_from_page_cache */
188 * Add a page to the write request cache list for later writing
189 * ASYNCHRONOUS write method.
191 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
193 struct obdfs_pgrq *pgrq;
196 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
197 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
198 inode->i_ino, page, pgrq);
203 memset(pgrq, 0, sizeof(*pgrq));
205 pgrq->rq_page = page;
206 pgrq->rq_inode = inode;
208 get_page(pgrq->rq_page);
209 list_add(&pgrq->rq_list, &OBDFS_LIST(inode));
211 /* For testing purposes, we write out the page here.
212 * In the future, a flush daemon will write out the page.
215 pgrq = obdfs_find_in_page_cache(inode, page);
217 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
222 return obdfs_remove_from_page_cache(pgrq);
223 } /* obdfs_add_to_page_cache */
226 /* select between SYNC and ASYNC I/O methods */
227 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
232 PDEBUG(page, "WRITEPAGE");
234 err = obdfs_brw(WRITE, inode, page, 1);
236 err = obdfs_add_to_page_cache(inode, page);
239 SetPageUptodate(page);
240 PDEBUG(page,"WRITEPAGE");
242 } /* obdfs_do_writepage */
244 /* returns the page unlocked, but with a reference */
245 int obdfs_writepage(struct dentry *dentry, struct page *page)
247 return obdfs_do_writepage(dentry->d_inode, page, 0);
251 * This does the "real" work of the write. The generic routine has
252 * allocated the page, locked it, done all the page alignment stuff
253 * calculations etc. Now we should just copy the data from user
254 * space and write it back to the real medium..
256 * If the writer ends up delaying the write, the writer needs to
257 * increment the page use counts until he is done with the page.
259 * Return value is the number of bytes written.
261 int obdfs_write_one_page(struct file *file, struct page *page,
262 unsigned long offset, unsigned long bytes,
265 struct inode *inode = file->f_dentry->d_inode;
269 if ( !Page_Uptodate(page) ) {
270 err = obdfs_brw(READ, inode, page, 1);
272 SetPageUptodate(page);
277 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
281 err = obdfs_writepage(file->f_dentry, page);
284 return (err < 0 ? err : bytes);
285 } /* obdfs_write_one_page */
288 return an up to date page:
289 - if locked is true then is returned locked
290 - if create is true the corresponding disk blocks are created
291 - page is held, i.e. caller must release the page
295 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
297 struct page *page_cache;
304 offset = offset & PAGE_CACHE_MASK;
305 CDEBUG(D_INODE, "\n");
308 page_cache = page_cache_alloc();
309 if ( ! page_cache ) {
313 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
315 hash = page_hash(&inode->i_data, offset);
316 page = grab_cache_page(&inode->i_data, offset);
320 printk("grab_cache_page says no dice ...\n");
325 PDEBUG(page, "GETPAGE: got page - before reading\n");
326 /* now check if the data in the page is up to date */
327 if ( Page_Uptodate(page)) {
334 err = obdfs_brw(READ, inode, page, create);
345 SetPageUptodate(page);
346 PDEBUG(page,"GETPAGE - after reading");
349 } /* obdfs_getpage */