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)
43 char *buf = (char *)page_address(page);
44 obd_size size = PAGE_SIZE;
45 obd_size *count = &size;
46 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
47 obd_flag flags = create ? OBD_BRW_CREATE : 0;
51 oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
56 obdfs_from_inode(oa, inode);
58 err = IOPS(inode, brw)(rw, IID(inode), &num_io, &oa, &buf, &count,
62 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
70 /* returns the page unlocked, but with a reference */
71 int obdfs_readpage(struct dentry *dentry, struct page *page)
73 struct inode *inode = dentry->d_inode;
78 rc = obdfs_brw(READ, inode, page, 0);
80 SetPageUptodate(page);
86 } /* obdfs_readpage */
88 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
90 int obdfs_init_pgrqcache(void)
93 if (obdfs_pgrq_cachep == NULL) {
94 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
95 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
96 sizeof(struct obdfs_pgrq),
97 0, SLAB_HWCACHE_ALIGN,
99 if (obdfs_pgrq_cachep == NULL) {
103 CDEBUG(D_INODE, "allocated cache at %p\n",
107 CDEBUG(D_INODE, "using existing cache at %p\n",
112 } /* obdfs_init_wreqcache */
114 void obdfs_cleanup_pgrqcache(void)
117 if (obdfs_pgrq_cachep != NULL) {
118 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
120 if (kmem_cache_destroy(obdfs_pgrq_cachep))
121 printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
123 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
126 } /* obdfs_cleanup_wreqcache */
130 * Find a specific page in the page cache. If it is found, we return
131 * the write request struct associated with it, if not found return NULL.
133 static struct obdfs_pgrq *
134 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
136 struct list_head *page_list = &OBDFS_LIST(inode);
137 struct list_head *tmp;
138 struct obdfs_pgrq *pgrq;
141 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
142 if (list_empty(page_list)) {
143 CDEBUG(D_INODE, "empty list\n");
148 while ( (tmp = tmp->next) != page_list ) {
149 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_list);
150 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
151 if (pgrq->rq_page == page) {
152 CDEBUG(D_INODE, "found page %p in list\n", page);
160 } /* obdfs_find_in_page_cache */
164 * Remove a writeback request from a list
167 obdfs_remove_from_page_cache(struct obdfs_pgrq *pgrq)
169 struct inode *inode = pgrq->rq_inode;
170 struct page *page = pgrq->rq_page;
174 CDEBUG(D_INODE, "writing inode %ld page %p, pgrq: %p\n",
175 inode->i_ino, page, pgrq);
177 PDEBUG(page, "REM_CACHE");
178 err = obdfs_brw(WRITE, inode, page, 1);
179 /* XXX probably should handle error here somehow. I think that
180 * ext2 also does the same thing - discard write even if error?
183 list_del(&pgrq->rq_list);
184 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
189 } /* obdfs_remove_from_page_cache */
192 * Add a page to the write request cache list for later writing
193 * ASYNCHRONOUS write method.
195 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
197 struct obdfs_pgrq *pgrq;
200 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
201 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
202 inode->i_ino, page, pgrq);
207 memset(pgrq, 0, sizeof(*pgrq));
209 pgrq->rq_page = page;
210 pgrq->rq_inode = inode;
212 get_page(pgrq->rq_page);
213 list_add(&pgrq->rq_list, &OBDFS_LIST(inode));
215 /* For testing purposes, we write out the page here.
216 * In the future, a flush daemon will write out the page.
219 pgrq = obdfs_find_in_page_cache(inode, page);
221 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
226 return obdfs_remove_from_page_cache(pgrq);
227 } /* obdfs_add_to_page_cache */
230 /* select between SYNC and ASYNC I/O methods */
231 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
236 PDEBUG(page, "WRITEPAGE");
238 err = obdfs_brw(WRITE, inode, page, 1);
240 err = obdfs_add_to_page_cache(inode, page);
243 SetPageUptodate(page);
244 PDEBUG(page,"WRITEPAGE");
246 } /* obdfs_do_writepage */
248 /* returns the page unlocked, but with a reference */
249 int obdfs_writepage(struct dentry *dentry, struct page *page)
251 return obdfs_do_writepage(dentry->d_inode, page, 0);
255 * This does the "real" work of the write. The generic routine has
256 * allocated the page, locked it, done all the page alignment stuff
257 * calculations etc. Now we should just copy the data from user
258 * space and write it back to the real medium..
260 * If the writer ends up delaying the write, the writer needs to
261 * increment the page use counts until he is done with the page.
263 * Return value is the number of bytes written.
265 int obdfs_write_one_page(struct file *file, struct page *page,
266 unsigned long offset, unsigned long bytes,
269 struct inode *inode = file->f_dentry->d_inode;
273 if ( !Page_Uptodate(page) ) {
274 err = obdfs_brw(READ, inode, page, 1);
276 SetPageUptodate(page);
281 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
285 err = obdfs_writepage(file->f_dentry, page);
288 return (err < 0 ? err : bytes);
289 } /* obdfs_write_one_page */
292 return an up to date page:
293 - if locked is true then is returned locked
294 - if create is true the corresponding disk blocks are created
295 - page is held, i.e. caller must release the page
299 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
301 struct page *page_cache;
308 offset = offset & PAGE_CACHE_MASK;
309 CDEBUG(D_INODE, "\n");
312 page_cache = page_cache_alloc();
313 if ( ! page_cache ) {
317 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
319 hash = page_hash(&inode->i_data, offset);
320 page = grab_cache_page(&inode->i_data, offset);
324 printk("grab_cache_page says no dice ...\n");
329 PDEBUG(page, "GETPAGE: got page - before reading\n");
330 /* now check if the data in the page is up to date */
331 if ( Page_Uptodate(page)) {
338 err = obdfs_brw(READ, inode, page, create);
349 SetPageUptodate(page);
350 PDEBUG(page,"GETPAGE - after reading");
353 } /* obdfs_getpage */