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 list_del(&pgrq->rq_plist);
120 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
123 void obdfs_cleanup_pgrqcache(void)
126 if (obdfs_pgrq_cachep != NULL) {
127 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
129 if (kmem_cache_destroy(obdfs_pgrq_cachep))
130 printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
132 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
135 } /* obdfs_cleanup_wreqcache */
139 * Find a specific page in the page cache. If it is found, we return
140 * the write request struct associated with it, if not found return NULL.
142 static struct obdfs_pgrq *
143 obdfs_find_in_page_list(struct inode *inode, struct page *page)
145 struct list_head *page_list = obdfs_iplist(inode);
146 struct list_head *tmp;
149 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
150 if (list_empty(page_list)) {
151 CDEBUG(D_INODE, "empty list\n");
156 while ( (tmp = tmp->next) != page_list ) {
157 struct obdfs_pgrq *pgrq;
159 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
160 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
161 if (pgrq->rq_page == page) {
162 CDEBUG(D_INODE, "found page %p in list\n", page);
170 } /* obdfs_find_in_page_list */
173 /* call and free pages from Linux page cache */
174 int obdfs_do_vec_wr(struct super_block *sb, obd_count num_io,
175 obd_count num_obdos, struct obdo **obdos,
176 obd_count *oa_bufs, struct page **pages, char **bufs,
177 obd_size *counts, obd_off *offsets, obd_flag *flags)
179 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
183 CDEBUG(D_INODE, "writing %d pages, %d obdos in vector\n",
185 err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
186 bufs, counts, offsets, flags);
188 /* release the pages from the page cache */
189 while ( num_io >= 0 ) {
191 put_page(pages[num_io]);
200 * Add a page to the write request cache list for later writing
201 * ASYNCHRONOUS write method.
203 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
205 struct obdfs_pgrq *pgrq;
208 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
209 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
210 inode->i_ino, page, pgrq);
215 memset(pgrq, 0, sizeof(*pgrq));
217 pgrq->rq_page = page;
219 get_page(pgrq->rq_page);
221 /* If this page isn't already in the inode page list, add it */
222 if ( !obdfs_find_in_page_list(inode, page) ) {
223 CDEBUG(D_INODE, "adding page %p to inode list %p\n", page,
224 obdfs_iplist(inode));
225 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
228 /* If inode isn't already on the superblock inodes list, add it */
229 if ( list_empty(obdfs_islist(inode)) ) {
230 CDEBUG(D_INODE, "adding inode %p to superblock list %p\n",
231 obdfs_islist(inode), obdfs_islist(inode));
232 list_add(obdfs_islist(inode), obdfs_slist(inode));
237 /* XXX For testing purposes, we write out the page here.
238 * In the future, a flush daemon will write out the page.
241 return obdfs_flush_reqs(obdfs_slist(inode), 0, 0);
242 } /* obdfs_add_page_to_cache */
245 /* select between SYNC and ASYNC I/O methods */
246 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
251 PDEBUG(page, "WRITEPAGE");
253 err = obdfs_brw(WRITE, inode, page, 1);
255 err = obdfs_add_page_to_cache(inode, page);
258 SetPageUptodate(page);
259 PDEBUG(page,"WRITEPAGE");
262 } /* obdfs_do_writepage */
264 /* returns the page unlocked, but with a reference */
265 int obdfs_writepage(struct dentry *dentry, struct page *page)
267 return obdfs_do_writepage(dentry->d_inode, page, 0);
271 * This does the "real" work of the write. The generic routine has
272 * allocated the page, locked it, done all the page alignment stuff
273 * calculations etc. Now we should just copy the data from user
274 * space and write it back to the real medium..
276 * If the writer ends up delaying the write, the writer needs to
277 * increment the page use counts until he is done with the page.
279 * Return value is the number of bytes written.
281 int obdfs_write_one_page(struct file *file, struct page *page,
282 unsigned long offset, unsigned long bytes,
285 struct inode *inode = file->f_dentry->d_inode;
289 if ( !Page_Uptodate(page) ) {
290 err = obdfs_brw(READ, inode, page, 1);
292 SetPageUptodate(page);
297 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
301 err = obdfs_writepage(file->f_dentry, page);
304 return (err < 0 ? err : bytes);
305 } /* obdfs_write_one_page */
308 return an up to date page:
309 - if locked is true then is returned locked
310 - if create is true the corresponding disk blocks are created
311 - page is held, i.e. caller must release the page
315 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
317 struct page *page_cache;
324 offset = offset & PAGE_CACHE_MASK;
325 CDEBUG(D_INODE, "\n");
328 page_cache = page_cache_alloc();
329 if ( ! page_cache ) {
333 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
335 hash = page_hash(&inode->i_data, offset);
336 page = grab_cache_page(&inode->i_data, offset);
340 printk("grab_cache_page says no dice ...\n");
345 PDEBUG(page, "GETPAGE: got page - before reading\n");
346 /* now check if the data in the page is up to date */
347 if ( Page_Uptodate(page)) {
354 err = obdfs_brw(READ, inode, page, create);
365 SetPageUptodate(page);
366 PDEBUG(page,"GETPAGE - after reading");
369 } /* obdfs_getpage */