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 oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
51 obdfs_from_inode(oa, inode);
53 err = IOPS(inode, brw)(rw, IID(inode), oa, (char *)page_address(page),
54 &count, (page->index) >> PAGE_SHIFT, create);
57 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
65 /* returns the page unlocked, but with a reference */
66 int obdfs_readpage(struct dentry *dentry, struct page *page)
68 struct inode *inode = dentry->d_inode;
73 rc = obdfs_brw(READ, inode, page, 0);
75 SetPageUptodate(page);
81 } /* obdfs_readpage */
83 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
85 int obdfs_init_pgrqcache(void)
88 if (obdfs_pgrq_cachep == NULL) {
89 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
90 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
91 sizeof(struct obdfs_pgrq),
92 0, SLAB_HWCACHE_ALIGN,
94 if (obdfs_pgrq_cachep == NULL) {
98 CDEBUG(D_INODE, "allocated cache at %p\n",
102 CDEBUG(D_INODE, "using existing cache at %p\n",
107 } /* obdfs_init_wreqcache */
109 void obdfs_cleanup_pgrqcache(void)
112 if (obdfs_pgrq_cachep != NULL) {
113 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
115 if (kmem_cache_destroy(obdfs_pgrq_cachep))
116 printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
118 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
121 } /* obdfs_cleanup_wreqcache */
125 * Find a specific page in the page cache. If it is found, we return
126 * the write request struct associated with it, if not found return NULL.
128 static struct obdfs_pgrq *
129 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
131 struct list_head *page_list = &OBDFS_LIST(inode);
132 struct list_head *tmp;
133 struct obdfs_pgrq *pgrq;
136 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
137 if (list_empty(page_list)) {
138 CDEBUG(D_INODE, "empty list\n");
143 while ( (tmp = tmp->next) != page_list ) {
144 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_list);
145 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
146 if (pgrq->rq_page == page) {
147 CDEBUG(D_INODE, "found page %p in list\n", page);
155 } /* obdfs_find_in_page_cache */
159 * Remove a writeback request from a list
162 obdfs_remove_from_page_cache(struct obdfs_pgrq *pgrq)
164 struct inode *inode = pgrq->rq_inode;
165 struct page *page = pgrq->rq_page;
169 CDEBUG(D_INODE, "writing inode %ld page %p, pgrq: %p\n",
170 inode->i_ino, page, pgrq);
172 PDEBUG(page, "REM_CACHE");
173 err = obdfs_brw(WRITE, inode, page, 1);
174 /* XXX probably should handle error here somehow. I think that
175 * ext2 also does the same thing - discard write even if error?
178 list_del(&pgrq->rq_list);
179 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
184 } /* obdfs_remove_from_page_cache */
187 * Add a page to the write request cache list for later writing
188 * ASYNCHRONOUS write method.
190 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
192 struct obdfs_pgrq *pgrq;
195 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
196 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
197 inode->i_ino, page, pgrq);
202 memset(pgrq, 0, sizeof(*pgrq));
204 pgrq->rq_page = page;
205 pgrq->rq_inode = inode;
207 get_page(pgrq->rq_page);
208 list_add(&pgrq->rq_list, &OBDFS_LIST(inode));
210 /* For testing purposes, we write out the page here.
211 * In the future, a flush daemon will write out the page.
214 pgrq = obdfs_find_in_page_cache(inode, page);
216 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
221 return obdfs_remove_from_page_cache(pgrq);
222 } /* obdfs_add_to_page_cache */
225 /* select between SYNC and ASYNC I/O methods */
226 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
231 PDEBUG(page, "WRITEPAGE");
233 err = obdfs_brw(WRITE, inode, page, 1);
235 err = obdfs_add_to_page_cache(inode, page);
238 SetPageUptodate(page);
239 PDEBUG(page,"WRITEPAGE");
241 } /* obdfs_do_writepage */
243 /* returns the page unlocked, but with a reference */
244 int obdfs_writepage(struct dentry *dentry, struct page *page)
246 return obdfs_do_writepage(dentry->d_inode, page, 0);
250 * This does the "real" work of the write. The generic routine has
251 * allocated the page, locked it, done all the page alignment stuff
252 * calculations etc. Now we should just copy the data from user
253 * space and write it back to the real medium..
255 * If the writer ends up delaying the write, the writer needs to
256 * increment the page use counts until he is done with the page.
258 * Return value is the number of bytes written.
260 int obdfs_write_one_page(struct file *file, struct page *page,
261 unsigned long offset, unsigned long bytes,
264 struct inode *inode = file->f_dentry->d_inode;
268 if ( !Page_Uptodate(page) ) {
269 err = obdfs_brw(READ, inode, page, 1);
271 SetPageUptodate(page);
276 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
280 err = obdfs_writepage(file->f_dentry, page);
283 return (err < 0 ? err : bytes);
284 } /* obdfs_write_one_page */
287 return an up to date page:
288 - if locked is true then is returned locked
289 - if create is true the corresponding disk blocks are created
290 - page is held, i.e. caller must release the page
294 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
296 struct page *page_cache;
303 offset = offset & PAGE_CACHE_MASK;
304 CDEBUG(D_INODE, "\n");
307 page_cache = page_cache_alloc();
308 if ( ! page_cache ) {
312 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
314 hash = page_hash(&inode->i_data, offset);
315 page = grab_cache_page(&inode->i_data, offset);
319 printk("grab_cache_page says no dice ...\n");
324 PDEBUG(page, "GETPAGE: got page - before reading\n");
325 /* now check if the data in the page is up to date */
326 if ( Page_Uptodate(page)) {
333 err = obdfs_brw(READ, inode, page, create);
344 SetPageUptodate(page);
345 PDEBUG(page,"GETPAGE - after reading");
348 } /* obdfs_getpage */