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>
37 int obdfs_flush_reqs(struct list_head *page_list,
38 int flush_inode, int check_time);
41 /* SYNCHRONOUS I/O for an inode */
42 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
45 obd_count oa_bufs = 1;
47 char *buf = (char *)page_address(page);
48 obd_size count = PAGE_SIZE;
49 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
50 obd_flag flags = create ? OBD_BRW_CREATE : 0;
54 oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
59 obdfs_from_inode(oa, inode);
61 err = IOPS(inode, brw)(rw, IID(inode), num_oa, &oa, &oa_bufs, &buf,
62 &count, &offset, &flags);
65 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
73 /* returns the page unlocked, but with a reference */
74 int obdfs_readpage(struct dentry *dentry, struct page *page)
76 struct inode *inode = dentry->d_inode;
81 rc = obdfs_brw(READ, inode, page, 0);
83 SetPageUptodate(page);
89 } /* obdfs_readpage */
91 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
93 int obdfs_init_pgrqcache(void)
96 if (obdfs_pgrq_cachep == NULL) {
97 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
98 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
99 sizeof(struct obdfs_pgrq),
100 0, SLAB_HWCACHE_ALIGN,
102 if (obdfs_pgrq_cachep == NULL) {
106 CDEBUG(D_INODE, "allocated cache at %p\n",
110 CDEBUG(D_INODE, "using existing cache at %p\n",
115 } /* obdfs_init_wreqcache */
117 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
119 CDEBUG(D_INODE, "deleting page %p from list\n", pgrq->rq_page);
120 list_del(&pgrq->rq_plist);
121 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
124 void obdfs_cleanup_pgrqcache(void)
127 if (obdfs_pgrq_cachep != NULL) {
128 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
130 if (kmem_cache_destroy(obdfs_pgrq_cachep))
131 printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
133 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
136 } /* obdfs_cleanup_wreqcache */
140 * Find a specific page in the page cache. If it is found, we return
141 * the write request struct associated with it, if not found return NULL.
143 static struct obdfs_pgrq *
144 obdfs_find_in_page_list(struct inode *inode, struct page *page)
146 struct list_head *page_list = obdfs_iplist(inode);
147 struct list_head *tmp;
151 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
154 if (list_empty(page_list)) {
159 while ( (tmp = tmp->next) != page_list ) {
160 struct obdfs_pgrq *pgrq;
162 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
163 if (pgrq->rq_page == page) {
164 CDEBUG(D_INODE, "found page %p in list\n", page);
172 } /* obdfs_find_in_page_list */
175 /* call and free pages from Linux page cache */
176 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
177 obd_count num_obdos, struct obdo **obdos,
178 obd_count *oa_bufs, struct page **pages, char **bufs,
179 obd_size *counts, obd_off *offsets, obd_flag *flags)
181 struct super_block *sb = inodes[0]->i_sb;
182 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
186 CDEBUG(D_INODE, "writing %d page(s), %d obdo(s) in vector\n",
188 err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
189 bufs, counts, offsets, flags);
191 /* release the pages from the page cache */
192 while ( num_io > 0 ) {
194 CDEBUG(D_INODE, "calling put_page for %p\n", pages[num_io]);
195 put_page(pages[num_io]);
198 while ( num_obdos > 0) {
200 CDEBUG(D_INODE, "copy/free obdo %ld\n",
201 (long)obdos[num_obdos]->o_id);
202 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
203 obdo_free(obdos[num_obdos]);
211 * Add a page to the write request cache list for later writing
212 * ASYNCHRONOUS write method.
214 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
216 struct obdfs_pgrq *pgrq;
219 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
220 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
221 inode->i_ino, page, pgrq);
226 memset(pgrq, 0, sizeof(*pgrq));
228 pgrq->rq_page = page;
230 get_page(pgrq->rq_page);
232 /* If this page isn't already in the inode page list, add it */
233 if ( !obdfs_find_in_page_list(inode, page) ) {
234 CDEBUG(D_INODE, "adding page %p to inode list %p\n", page,
235 obdfs_iplist(inode));
236 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
239 /* If inode isn't already on the superblock inodes list, add it */
240 if ( list_empty(obdfs_islist(inode)) ) {
241 CDEBUG(D_INODE, "adding inode %p to superblock list %p\n",
242 obdfs_islist(inode), obdfs_slist(inode));
243 list_add(obdfs_islist(inode), obdfs_slist(inode));
248 /* XXX For testing purposes, we write out the page here.
249 * In the future, a flush daemon will write out the page.
250 return obdfs_flush_reqs(obdfs_slist(inode), 0, 0);
253 } /* obdfs_add_page_to_cache */
256 /* select between SYNC and ASYNC I/O methods */
257 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
262 PDEBUG(page, "WRITEPAGE");
264 err = obdfs_brw(WRITE, inode, page, 1);
266 err = obdfs_add_page_to_cache(inode, page);
269 SetPageUptodate(page);
270 PDEBUG(page,"WRITEPAGE");
273 } /* obdfs_do_writepage */
275 /* returns the page unlocked, but with a reference */
276 int obdfs_writepage(struct dentry *dentry, struct page *page)
278 return obdfs_do_writepage(dentry->d_inode, page, 0);
282 * This does the "real" work of the write. The generic routine has
283 * allocated the page, locked it, done all the page alignment stuff
284 * calculations etc. Now we should just copy the data from user
285 * space and write it back to the real medium..
287 * If the writer ends up delaying the write, the writer needs to
288 * increment the page use counts until he is done with the page.
290 * Return value is the number of bytes written.
292 int obdfs_write_one_page(struct file *file, struct page *page,
293 unsigned long offset, unsigned long bytes,
296 struct inode *inode = file->f_dentry->d_inode;
300 if ( !Page_Uptodate(page) ) {
301 err = obdfs_brw(READ, inode, page, 1);
303 SetPageUptodate(page);
308 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
312 err = obdfs_writepage(file->f_dentry, page);
315 return (err < 0 ? err : bytes);
316 } /* obdfs_write_one_page */
319 return an up to date page:
320 - if locked is true then is returned locked
321 - if create is true the corresponding disk blocks are created
322 - page is held, i.e. caller must release the page
326 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
328 struct page *page_cache;
335 offset = offset & PAGE_CACHE_MASK;
336 CDEBUG(D_INODE, "\n");
339 page_cache = page_cache_alloc();
340 if ( ! page_cache ) {
344 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
346 hash = page_hash(&inode->i_data, offset);
347 page = grab_cache_page(&inode->i_data, offset);
351 printk("grab_cache_page says no dice ...\n");
356 PDEBUG(page, "GETPAGE: got page - before reading\n");
357 /* now check if the data in the page is up to date */
358 if ( Page_Uptodate(page)) {
365 err = obdfs_brw(READ, inode, page, create);
376 SetPageUptodate(page);
377 PDEBUG(page,"GETPAGE - after reading");
380 } /* obdfs_getpage */