2 * OBDFS Super operations
4 * This code is issued under the GNU General Public License.
5 * See the file COPYING in this distribution
7 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
8 * Copryright (C) 1999 Stelias Computing Inc,
9 * (author Peter J. Braam <braam@stelias.com>)
10 * Copryright (C) 1999 Seagate Technology Inc.
14 #include <linux/config.h>
15 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/stat.h>
19 #include <linux/errno.h>
20 #include <linux/locks.h>
21 #include <linux/unistd.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
27 #include <linux/stat.h>
28 #include <asm/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <asm/segment.h>
32 #include <linux/pagemap.h>
33 #include <linux/smp_lock.h>
35 #include <linux/obd_support.h>
36 #include <linux/obd_ext2.h>
37 #include <linux/obdfs.h>
39 void obdfs_change_inode(struct inode *inode);
41 static int cache_writes = 0;
44 /* page cache support stuff */
47 * Add a page to the dirty page list.
49 void __set_page_dirty(struct page *page)
51 struct address_space *mapping;
54 pg_lock = PAGECACHE_LOCK(page);
57 mapping = page->mapping;
58 spin_lock(&mapping->page_lock);
60 list_del(&page->list);
61 list_add(&page->list, &mapping->dirty_pages);
63 spin_unlock(&mapping->page_lock);
67 mark_inode_dirty_pages(mapping->host);
71 * Add a page to the dirty page list.
73 void __set_page_clean(struct page *page)
76 struct address_space *mapping;
79 pg_lock = PAGECACHE_LOCK(page);
82 mapping = page->mapping;
83 spin_lock(&mapping->page_lock);
85 list_del(&page->list);
86 list_add(&page->list, &mapping->clean_pages);
88 spin_unlock(&mapping->page_lock);
91 inode = mapping->host;
92 if (list_empty(&mapping->dirty_pages)) {
93 CDEBUG(D_INODE, "inode clean\n");
94 inode->i_state &= ~I_DIRTY_PAGES;
99 inline void set_page_clean(struct page *page)
101 if (PageDirty(page)) {
102 ClearPageDirty(page);
103 __set_page_clean(page);
107 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
108 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
110 obd_count num_obdo = 1;
111 obd_count bufs_per_obdo = 1;
113 obd_size count = PAGE_SIZE;
114 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
115 obd_flag flags = create ? OBD_BRW_CREATE : 0;
119 if (IOPS(inode, brw) == NULL) {
120 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
130 oa->o_valid = OBD_MD_FLNOTOBD;
131 obdfs_from_inode(oa, inode);
133 err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
134 &page, &count, &offset, &flags);
136 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
143 extern void set_page_clean(struct page *);
145 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
146 static int obdfs_commit_page(struct page *page, int create, int from, int to)
148 struct inode *inode = page->mapping->host;
149 obd_count num_obdo = 1;
150 obd_count bufs_per_obdo = 1;
153 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
154 obd_flag flags = create ? OBD_BRW_CREATE : 0;
158 if (IOPS(inode, brw) == NULL) {
159 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
169 oa->o_valid = OBD_MD_FLNOTOBD;
170 obdfs_from_inode(oa, inode);
172 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
175 err = IOPS(inode, brw)(WRITE, IID(inode), num_obdo, &oa, &bufs_per_obdo,
176 &page, &count, &offset, &flags);
178 SetPageUptodate(page);
179 set_page_clean(page);
183 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
191 /* returns the page unlocked, but with a reference */
192 int obdfs_readpage(struct file *file, struct page *page)
194 struct inode *inode = page->mapping->host;
199 if ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT)
201 memset(kmap(page), 0, PAGE_CACHE_SIZE);
206 if (Page_Uptodate(page)) {
211 rc = obdfs_brw(READ, inode, page, 0);
216 /* PDEBUG(page, "READ"); */
219 SetPageUptodate(page);
220 obd_unlock_page(page);
223 } /* obdfs_readpage */
225 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
227 struct inode *inode = page->mapping->host;
228 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
233 if (Page_Uptodate(page)) {
238 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
243 rc = obdfs_brw(READ, inode, page, 0);
245 SetPageUptodate(page);
249 set_page_dirty(page);
250 //SetPageDirty(page);
260 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
262 int obdfs_init_pgrqcache(void)
265 if (obdfs_pgrq_cachep == NULL) {
266 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
267 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
268 sizeof(struct obdfs_pgrq),
269 0, SLAB_HWCACHE_ALIGN,
271 if (obdfs_pgrq_cachep == NULL) {
275 CDEBUG(D_CACHE, "allocated cache at %p\n",
279 CDEBUG(D_CACHE, "using existing cache at %p\n",
284 } /* obdfs_init_wreqcache */
286 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
289 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
290 pgrq->rq_page, obdfs_cache_count);
291 list_del(&pgrq->rq_plist);
292 OBDClearCachePage(pgrq->rq_page);
293 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
296 void obdfs_cleanup_pgrqcache(void)
299 if (obdfs_pgrq_cachep != NULL) {
300 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
301 obdfs_pgrq_cachep, obdfs_cache_count);
302 if (kmem_cache_destroy(obdfs_pgrq_cachep))
303 printk(KERN_INFO __FUNCTION__
304 ": unable to free all of cache\n");
305 obdfs_pgrq_cachep = NULL;
307 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
310 } /* obdfs_cleanup_wreqcache */
313 /* called with the list lock held */
314 static struct page *obdfs_find_page_index(struct inode *inode,
317 struct list_head *page_list = obdfs_iplist(inode);
318 struct list_head *tmp;
323 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
324 inode->i_ino, index);
327 if (list_empty(page_list)) {
332 while ( (tmp = tmp->next) != page_list ) {
333 struct obdfs_pgrq *pgrq;
335 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
336 page = pgrq->rq_page;
337 if (index == page->index) {
339 "INDEX SEARCH found page %p, index %ld\n",
348 } /* obdfs_find_page_index */
351 /* call and free pages from Linux page cache: called with io lock on inodes */
352 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
353 obd_count num_obdos, struct obdo **obdos,
354 obd_count *oa_bufs, struct page **pages, char **bufs,
355 obd_size *counts, obd_off *offsets, obd_flag *flags)
360 if (IOPS(inodes[0], brw) == NULL) {
361 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
366 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
368 if (obd_debug_level & D_INFO) { /* DEBUGGING */
371 for (i = 0; i < num_obdos; i++)
372 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
375 for (i = 0; i < num_io; i++)
376 printk("0x%p ", pages[i]);
380 err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
381 oa_bufs, pages, counts, offsets, flags);
383 CDEBUG(D_INFO, "BRW done\n");
384 /* release the pages from the page cache */
385 while ( num_io > 0 ) {
387 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
388 pages[num_io], pages[num_io]->index);
389 /* PDEBUG(pages[num_io], "do_vec_wr"); */
390 put_page(pages[num_io]);
391 /* PDEBUG(pages[num_io], "do_vec_wr"); */
393 CDEBUG(D_INFO, "put_page done\n");
395 while ( num_obdos > 0) {
397 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
398 /* copy o_blocks to i_blocks */
399 obdfs_set_size (inodes[num_obdos], obdos[num_obdos]->o_size);
400 //obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
401 obdo_free(obdos[num_obdos]);
403 CDEBUG(D_INFO, "obdo_free done\n");
410 * Add a page to the write request cache list for later writing.
411 * ASYNCHRONOUS write method.
413 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
418 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
419 * is written, so at worst we will write the page out twice.
421 * If the page has the PG_obdcache bit set, then the inode MUST be
422 * on the superblock dirty list so we don't need to check this.
423 * Dirty inodes are removed from the superblock list ONLY when they
424 * don't have any more cached pages. It is possible to have an inode
425 * with no dirty pages on the superblock list, but not possible to
426 * have an inode with dirty pages NOT on the superblock dirty list.
428 if (!OBDAddCachePage(page)) {
429 struct obdfs_pgrq *pgrq;
430 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
432 OBDClearCachePage(page);
436 /* not really necessary since we set all pgrq fields here
437 memset(pgrq, 0, sizeof(*pgrq));
440 pgrq->rq_page = page;
441 pgrq->rq_jiffies = jiffies;
442 get_page(pgrq->rq_page);
444 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
445 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
447 //printk("-- count %d\n", obdfs_cache_count);
449 /* If inode isn't already on superblock inodes list, add it.
451 * We increment the reference count on the inode to keep it
452 * from being freed from memory. This _should_ be an iget()
453 * with an iput() in both flush_reqs() and put_inode(), but
454 * since put_inode() is called from iput() we can't call iput()
455 * again there. Instead we just increment/decrement i_count,
456 * which is mostly what iget/iput do for an inode in memory.
458 if ( list_empty(obdfs_islist(inode)) ) {
459 atomic_inc(&inode->i_count);
461 "adding inode %ld to superblock list %p\n",
462 inode->i_ino, obdfs_slist(inode));
463 list_add(obdfs_islist(inode), obdfs_slist(inode));
465 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
469 /* XXX For testing purposes, we can write out the page here.
470 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
475 } /* obdfs_add_page_to_cache */
479 if (obdfs_cache_count > 60000) {
480 printk("-- count %ld\n", obdfs_cache_count);
481 //obdfs_flush_dirty_pages(~0UL);
482 printk("-- count %ld\n", obdfs_cache_count);
486 /* select between SYNC and ASYNC I/O methods */
487 int obdfs_do_writepage(struct page *page, int sync)
489 struct inode *inode = page->mapping->host;
493 /* PDEBUG(page, "WRITEPAGE"); */
495 err = obdfs_brw(WRITE, inode, page, 1);
497 err = obdfs_add_page_to_cache(inode, page);
498 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
499 inode->i_ino, page, err, Page_Uptodate(page));
503 SetPageUptodate(page);
504 set_page_clean(page);
506 /* PDEBUG(page,"WRITEPAGE"); */
509 } /* obdfs_do_writepage */
513 /* returns the page unlocked, but with a reference */
514 int obdfs_writepage(struct page *page)
517 struct inode *inode = page->mapping->host;
519 printk("---> writepage called ino %ld!\n", inode->i_ino);
521 rc = obdfs_do_writepage(page, 1);
523 set_page_clean(page);
525 CDEBUG(D_INODE, "--> GRR %d\n", rc);
531 void write_inode_pages(struct inode *inode)
533 struct list_head *tmp = &inode->i_mapping->dirty_pages;
535 while ( (tmp = tmp->next) != &inode->i_mapping->dirty_pages) {
537 page = list_entry(tmp, struct page, list);
538 obdfs_writepage(page);
543 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
545 struct inode *inode = page->mapping->host;
547 loff_t len = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
549 CDEBUG(D_INODE, "commit write ino %ld (end at %Ld) from %d to %d ,ind %ld\n",
550 inode->i_ino, len, from, to, page->index);
553 if (cache_writes == 0) {
554 rc = obdfs_commit_page(page, 1, from, to);
557 if (len > inode->i_size) {
558 obdfs_set_size(inode, len);
568 * This does the "real" work of the write. The generic routine has
569 * allocated the page, locked it, done all the page alignment stuff
570 * calculations etc. Now we should just copy the data from user
571 * space and write it back to the real medium..
573 * If the writer ends up delaying the write, the writer needs to
574 * increment the page use counts until he is done with the page.
576 * Return value is the number of bytes written.
578 int obdfs_write_one_page(struct file *file, struct page *page,
579 unsigned long offset, unsigned long bytes,
582 struct inode *inode = file->f_dentry->d_inode;
586 /* We check for complete page writes here, as we then don't have to
587 * get the page before writing over everything anyways.
589 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
590 err = obdfs_brw(READ, inode, page, 0);
593 SetPageUptodate(page);
596 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
600 err = obdfs_writepage(page);
603 return (err < 0 ? err : bytes);
604 } /* obdfs_write_one_page */
607 * return an up to date page:
608 * - if locked is true then is returned locked
609 * - if create is true the corresponding disk blocks are created
610 * - page is held, i.e. caller must release the page
612 * modeled on NFS code.
614 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
615 int create, int locked)
623 offset = offset & PAGE_CACHE_MASK;
624 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
625 inode->i_ino, offset, create, locked);
626 index = offset >> PAGE_CACHE_SHIFT;
628 page = grab_cache_page(&inode->i_data, index);
632 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
637 /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
638 /* now check if the data in the page is up to date */
639 if ( Page_Uptodate(page)) {
641 if (PageLocked(page))
642 obd_unlock_page(page);
644 printk("file %s, line %d: expecting locked page\n",
652 #ifdef EXT2_OBD_DEBUG
653 if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
654 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
659 err = obdfs_brw(READ, inode, page, create);
663 obd_unlock_page(page);
669 obd_unlock_page(page);
670 SetPageUptodate(page);
671 /* PDEBUG(page,"GETPAGE - after reading"); */
674 } /* obdfs_getpage */
677 void obdfs_truncate(struct inode *inode)
683 //obdfs_dequeue_pages(inode);
685 if (IOPS(inode, punch) == NULL) {
686 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
693 /* XXX This would give an inconsistent FS, so deal with it as
694 * best we can for now - an obdo on the stack is not pretty.
698 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
700 obdo.o_valid = OBD_MD_FLNOTOBD;
701 obdfs_from_inode(&obdo, inode);
703 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
705 oa->o_valid = OBD_MD_FLNOTOBD;
706 obdfs_from_inode(oa, inode);
708 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
709 (long)oa->o_id, oa->o_size);
710 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
716 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
721 } /* obdfs_truncate */
723 struct address_space_operations obdfs_aops = {
724 readpage: obdfs_readpage,
725 writepage: obdfs_writepage,
726 sync_page: block_sync_page,
727 prepare_write: obdfs_prepare_write,
728 commit_write: obdfs_commit_write,