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 /* SYNCHRONOUS I/O for an inode */
38 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
40 obd_count num_obdo = 1;
41 obd_count bufs_per_obdo = 1;
43 char *buf = (char *)page_address(page);
44 obd_size count = PAGE_SIZE;
45 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
46 obd_flag flags = create ? OBD_BRW_CREATE : 0;
50 oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
55 obdfs_from_inode(oa, inode);
57 err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
58 &buf, &count, &offset, &flags);
61 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
69 /* returns the page unlocked, but with a reference */
70 int obdfs_readpage(struct dentry *dentry, struct page *page)
72 struct inode *inode = dentry->d_inode;
77 rc = obdfs_brw(READ, inode, page, 0);
79 SetPageUptodate(page);
85 } /* obdfs_readpage */
87 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
89 /* XXX should probably have one of these per superblock */
90 static int obdfs_cache_count = 0;
92 int obdfs_init_pgrqcache(void)
95 if (obdfs_pgrq_cachep == NULL) {
96 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
97 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
98 sizeof(struct obdfs_pgrq),
99 0, SLAB_HWCACHE_ALIGN,
101 if (obdfs_pgrq_cachep == NULL) {
105 CDEBUG(D_INODE, "allocated cache at %p\n",
109 CDEBUG(D_INODE, "using existing cache at %p\n",
114 } /* obdfs_init_wreqcache */
116 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
119 CDEBUG(D_INODE, "deleting page %p from list [count %d]\n",
120 pgrq->rq_page, obdfs_cache_count);
121 list_del(&pgrq->rq_plist);
122 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
125 void obdfs_cleanup_pgrqcache(void)
128 if (obdfs_pgrq_cachep != NULL) {
129 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p, count %d\n",
130 obdfs_pgrq_cachep, obdfs_cache_count);
131 if (kmem_cache_destroy(obdfs_pgrq_cachep))
132 printk(KERN_INFO __FUNCTION__
133 ": unable to free all of cache\n");
134 obdfs_pgrq_cachep = NULL;
136 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
139 } /* obdfs_cleanup_wreqcache */
143 * Find a specific page in the page cache. If it is found, we return
144 * the write request struct associated with it, if not found return NULL.
145 * Called with the list lock held.
147 static struct obdfs_pgrq *
148 obdfs_find_in_page_list(struct inode *inode, struct page *page)
150 struct list_head *page_list = obdfs_iplist(inode);
151 struct list_head *tmp;
155 CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
158 if (list_empty(page_list)) {
159 CDEBUG(D_INODE, "empty list\n");
164 while ( (tmp = tmp->next) != page_list ) {
165 struct obdfs_pgrq *pgrq;
167 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
168 if (pgrq->rq_page == page) {
169 CDEBUG(D_INODE, "found page %p in list\n", page);
177 } /* obdfs_find_in_page_list */
180 /* called with the list lock held */
181 static struct page* obdfs_find_page_index(struct inode *inode,
184 struct list_head *page_list = obdfs_iplist(inode);
185 struct list_head *tmp;
190 CDEBUG(D_INODE, "looking for inode %ld pageindex %ld\n",
191 inode->i_ino, index);
194 if (list_empty(page_list)) {
199 while ( (tmp = tmp->next) != page_list ) {
200 struct obdfs_pgrq *pgrq;
202 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
203 page = pgrq->rq_page;
204 if (index == page->index) {
206 "INDEX SEARCH found page %p, index %ld\n",
215 } /* obdfs_find_page_index */
218 /* call and free pages from Linux page cache: called with io lock on inodes */
219 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
220 obd_count num_obdos, struct obdo **obdos,
221 obd_count *oa_bufs, struct page **pages, char **bufs,
222 obd_size *counts, obd_off *offsets, obd_flag *flags)
224 struct super_block *sb = inodes[0]->i_sb;
225 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
229 CDEBUG(D_INODE, "writing %d page(s), %d obdo(s) in vector\n",
231 err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
232 bufs, counts, offsets, flags);
234 /* release the pages from the page cache */
235 while ( num_io > 0 ) {
237 CDEBUG(D_INODE, "calling put_page for %p, index %ld\n",
238 pages[num_io], pages[num_io]->index);
239 put_page(pages[num_io]);
242 while ( num_obdos > 0) {
244 CDEBUG(D_INODE, "copy/free obdo %ld\n",
245 (long)obdos[num_obdos]->o_id);
246 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
247 obdo_free(obdos[num_obdos]);
255 * Add a page to the write request cache list for later writing
256 * ASYNCHRONOUS write method.
258 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
264 /* If this page isn't already in the inode page list, add it */
265 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
266 if ( !obdfs_find_in_page_list(inode, page) ) {
267 struct obdfs_pgrq *pgrq;
268 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
269 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p, cache count [%d]\n",
270 inode->i_ino, page, pgrq, obdfs_cache_count);
273 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
276 memset(pgrq, 0, sizeof(*pgrq));
278 pgrq->rq_page = page;
279 get_page(pgrq->rq_page);
280 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
284 /* If inode isn't already on the superblock inodes list, add it,
285 * and increase ref count on inode so it doesn't disappear on us.
287 * We increment the reference count on the inode to keep it from
288 * being freed from memory. This _should_ be an iget() with an
289 * iput() in both flush_reqs() and put_inode(), but since ut_inode()
290 * is called from iput() we can't call iput() again there. Instead
291 * we just increment/decrement i_count, which is essentially what
292 * iget/iput do for an inode already in memory.
294 if ( list_empty(obdfs_islist(inode)) ) {
296 CDEBUG(D_INODE, "adding inode %ld to superblock list %p\n",
297 inode->i_ino, obdfs_slist(inode));
298 list_add(obdfs_islist(inode), obdfs_slist(inode));
301 /* XXX For testing purposes, we write out the page here.
302 * In the future, a flush daemon will write out the page.
303 res = obdfs_flush_reqs(obdfs_slist(inode), 0);
304 obdfs_flush_dirty_pages(1);
306 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
310 } /* obdfs_add_page_to_cache */
313 /* select between SYNC and ASYNC I/O methods */
314 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
319 /* PDEBUG(page, "WRITEPAGE"); */
321 err = obdfs_brw(WRITE, inode, page, 1);
323 err = obdfs_add_page_to_cache(inode, page);
324 CDEBUG(D_IOCTL, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n", inode->i_ino, page, err, Page_Uptodate(page));
328 SetPageUptodate(page);
329 /* PDEBUG(page,"WRITEPAGE"); */
332 } /* obdfs_do_writepage */
334 /* returns the page unlocked, but with a reference */
335 int obdfs_writepage(struct dentry *dentry, struct page *page)
337 return obdfs_do_writepage(dentry->d_inode, page, 0);
341 * This does the "real" work of the write. The generic routine has
342 * allocated the page, locked it, done all the page alignment stuff
343 * calculations etc. Now we should just copy the data from user
344 * space and write it back to the real medium..
346 * If the writer ends up delaying the write, the writer needs to
347 * increment the page use counts until he is done with the page.
349 * Return value is the number of bytes written.
351 int obdfs_write_one_page(struct file *file, struct page *page,
352 unsigned long offset, unsigned long bytes,
355 struct inode *inode = file->f_dentry->d_inode;
359 if ( !Page_Uptodate(page) ) {
360 err = obdfs_brw(READ, inode, page, 1);
362 SetPageUptodate(page);
367 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
371 err = obdfs_writepage(file->f_dentry, page);
374 return (err < 0 ? err : bytes);
375 } /* obdfs_write_one_page */
378 * return an up to date page:
379 * - if locked is true then is returned locked
380 * - if create is true the corresponding disk blocks are created
381 * - page is held, i.e. caller must release the page
383 * modeled on NFS code.
385 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
386 int create, int locked)
388 struct page *page_cache;
396 offset = offset & PAGE_CACHE_MASK;
397 CDEBUG(D_INODE, "ino: %ld, offset %ld, create %d, locked %d\n",
398 inode->i_ino, offset, create, locked);
399 index = offset >> PAGE_CACHE_SHIFT;
403 page_cache = page_cache_alloc();
404 if ( ! page_cache ) {
408 CDEBUG(D_INODE, "page_cache %p\n", page_cache);
410 hash = page_hash(&inode->i_data, index);
411 page = grab_cache_page(&inode->i_data, index);
415 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
420 PDEBUG(page, "GETPAGE: got page - before reading\n");
421 /* now check if the data in the page is up to date */
422 if ( Page_Uptodate(page)) {
430 if ( obdfs_find_page_index(inode, index) ) {
431 CDEBUG(D_INODE, "OVERWRITE: found dirty page %p, index %ld\n",
435 err = obdfs_brw(READ, inode, page, create);
446 SetPageUptodate(page);
447 PDEBUG(page,"GETPAGE - after reading");
450 } /* obdfs_getpage */