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 */
48 * Add a page to the dirty page list.
50 void set_page_dirty(struct page *page)
52 if (!test_and_set_bit(PG_dirty, &page->flags)) {
53 struct address_space *mapping = page->mapping;
56 spin_lock(&pagecache_lock);
57 list_del(&page->list);
58 list_add(&page->list, &mapping->dirty_pages);
59 spin_unlock(&pagecache_lock);
62 mark_inode_dirty_pages(mapping->host);
68 * Remove page from dirty list
70 void __set_page_clean(struct page *page)
72 struct address_space *mapping = page->mapping;
78 spin_lock(&pagecache_lock);
79 list_del(&page->list);
80 list_add(&page->list, &mapping->clean_pages);
82 inode = mapping->host;
83 if (list_empty(&mapping->dirty_pages)) {
84 CDEBUG(D_INODE, "inode clean\n");
85 inode->i_state &= ~I_DIRTY_PAGES;
87 spin_unlock(&pagecache_lock);
91 inline void set_page_clean(struct page *page)
93 if (PageDirty(page)) {
95 __set_page_clean(page);
99 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
100 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
102 obd_count num_obdo = 1;
103 obd_count bufs_per_obdo = 1;
105 obd_size count = PAGE_SIZE;
106 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
107 obd_flag flags = create ? OBD_BRW_CREATE : 0;
111 if (IOPS(inode, brw) == NULL) {
112 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
122 oa->o_valid = OBD_MD_FLNOTOBD;
123 obdfs_from_inode(oa, inode);
125 err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
126 &page, &count, &offset, &flags);
128 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
135 extern void set_page_clean(struct page *);
137 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
138 static int obdfs_commit_page(struct page *page, int create, int from, int to)
140 struct inode *inode = page->mapping->host;
141 obd_count num_obdo = 1;
142 obd_count bufs_per_obdo = 1;
145 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
146 obd_flag flags = create ? OBD_BRW_CREATE : 0;
150 if (IOPS(inode, brw) == NULL) {
151 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
161 oa->o_valid = OBD_MD_FLNOTOBD;
162 obdfs_from_inode(oa, inode);
164 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
167 err = IOPS(inode, brw)(WRITE, IID(inode), num_obdo, &oa, &bufs_per_obdo,
168 &page, &count, &offset, &flags);
170 SetPageUptodate(page);
171 set_page_clean(page);
175 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
182 /* returns the page unlocked, but with a reference */
183 int obdfs_writepage(struct page *page)
186 struct inode *inode = page->mapping->host;
188 printk("---> writepage called ino %ld!\n", inode->i_ino);
190 rc = obdfs_brw(WRITE, inode, page, 1);
192 set_page_clean(page);
194 CDEBUG(D_INODE, "--> GRR %d\n", rc);
201 void write_inode_pages(struct inode *inode)
203 struct list_head *tmp = &inode->i_mapping->dirty_pages;
205 while ( (tmp = tmp->next) != &inode->i_mapping->dirty_pages) {
207 page = list_entry(tmp, struct page, list);
208 obdfs_writepage(page);
213 /* returns the page unlocked, but with a reference */
214 int obdfs_readpage(struct file *file, struct page *page)
216 struct inode *inode = page->mapping->host;
221 if ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT)
223 memset(kmap(page), 0, PAGE_CACHE_SIZE);
228 if (Page_Uptodate(page)) {
233 rc = obdfs_brw(READ, inode, page, 0);
238 /* PDEBUG(page, "READ"); */
241 SetPageUptodate(page);
242 obd_unlock_page(page);
245 } /* obdfs_readpage */
247 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
249 struct inode *inode = page->mapping->host;
250 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
255 if (Page_Uptodate(page)) {
260 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
265 rc = obdfs_brw(READ, inode, page, 0);
267 SetPageUptodate(page);
271 set_page_dirty(page);
272 //SetPageDirty(page);
282 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
284 int obdfs_init_pgrqcache(void)
287 if (obdfs_pgrq_cachep == NULL) {
288 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
289 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
290 sizeof(struct obdfs_pgrq),
291 0, SLAB_HWCACHE_ALIGN,
293 if (obdfs_pgrq_cachep == NULL) {
297 CDEBUG(D_CACHE, "allocated cache at %p\n",
301 CDEBUG(D_CACHE, "using existing cache at %p\n",
306 } /* obdfs_init_wreqcache */
308 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
311 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
312 pgrq->rq_page, obdfs_cache_count);
313 list_del(&pgrq->rq_plist);
314 OBDClearCachePage(pgrq->rq_page);
315 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
318 void obdfs_cleanup_pgrqcache(void)
321 if (obdfs_pgrq_cachep != NULL) {
322 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
323 obdfs_pgrq_cachep, obdfs_cache_count);
324 if (kmem_cache_destroy(obdfs_pgrq_cachep))
325 printk(KERN_INFO __FUNCTION__
326 ": unable to free all of cache\n");
327 obdfs_pgrq_cachep = NULL;
329 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
332 } /* obdfs_cleanup_wreqcache */
335 /* called with the list lock held */
336 static struct page *obdfs_find_page_index(struct inode *inode,
339 struct list_head *page_list = obdfs_iplist(inode);
340 struct list_head *tmp;
345 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
346 inode->i_ino, index);
349 if (list_empty(page_list)) {
354 while ( (tmp = tmp->next) != page_list ) {
355 struct obdfs_pgrq *pgrq;
357 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
358 page = pgrq->rq_page;
359 if (index == page->index) {
361 "INDEX SEARCH found page %p, index %ld\n",
370 } /* obdfs_find_page_index */
373 /* call and free pages from Linux page cache: called with io lock on inodes */
374 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
375 obd_count num_obdos, struct obdo **obdos,
376 obd_count *oa_bufs, struct page **pages, char **bufs,
377 obd_size *counts, obd_off *offsets, obd_flag *flags)
382 if (IOPS(inodes[0], brw) == NULL) {
383 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
388 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
390 if (obd_debug_level & D_INFO) { /* DEBUGGING */
393 for (i = 0; i < num_obdos; i++)
394 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
397 for (i = 0; i < num_io; i++)
398 printk("0x%p ", pages[i]);
402 err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
403 oa_bufs, pages, counts, offsets, flags);
405 CDEBUG(D_INFO, "BRW done\n");
406 /* release the pages from the page cache */
407 while ( num_io > 0 ) {
409 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
410 pages[num_io], pages[num_io]->index);
411 /* PDEBUG(pages[num_io], "do_vec_wr"); */
412 put_page(pages[num_io]);
413 /* PDEBUG(pages[num_io], "do_vec_wr"); */
415 CDEBUG(D_INFO, "put_page done\n");
417 while ( num_obdos > 0) {
419 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
420 /* copy o_blocks to i_blocks */
421 obdfs_set_size (inodes[num_obdos], obdos[num_obdos]->o_size);
422 //obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
423 obdo_free(obdos[num_obdos]);
425 CDEBUG(D_INFO, "obdo_free done\n");
432 * Add a page to the write request cache list for later writing.
433 * ASYNCHRONOUS write method.
435 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
440 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
441 * is written, so at worst we will write the page out twice.
443 * If the page has the PG_obdcache bit set, then the inode MUST be
444 * on the superblock dirty list so we don't need to check this.
445 * Dirty inodes are removed from the superblock list ONLY when they
446 * don't have any more cached pages. It is possible to have an inode
447 * with no dirty pages on the superblock list, but not possible to
448 * have an inode with dirty pages NOT on the superblock dirty list.
450 if (!OBDAddCachePage(page)) {
451 struct obdfs_pgrq *pgrq;
452 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
454 OBDClearCachePage(page);
458 /* not really necessary since we set all pgrq fields here
459 memset(pgrq, 0, sizeof(*pgrq));
462 pgrq->rq_page = page;
463 pgrq->rq_jiffies = jiffies;
464 get_page(pgrq->rq_page);
466 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
467 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
469 //printk("-- count %d\n", obdfs_cache_count);
471 /* If inode isn't already on superblock inodes list, add it.
473 * We increment the reference count on the inode to keep it
474 * from being freed from memory. This _should_ be an iget()
475 * with an iput() in both flush_reqs() and put_inode(), but
476 * since put_inode() is called from iput() we can't call iput()
477 * again there. Instead we just increment/decrement i_count,
478 * which is mostly what iget/iput do for an inode in memory.
480 if ( list_empty(obdfs_islist(inode)) ) {
481 atomic_inc(&inode->i_count);
483 "adding inode %ld to superblock list %p\n",
484 inode->i_ino, obdfs_slist(inode));
485 list_add(obdfs_islist(inode), obdfs_slist(inode));
487 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
491 /* XXX For testing purposes, we can write out the page here.
492 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
497 } /* obdfs_add_page_to_cache */
501 if (obdfs_cache_count > 60000) {
502 printk("-- count %ld\n", obdfs_cache_count);
503 //obdfs_flush_dirty_pages(~0UL);
504 printk("-- count %ld\n", obdfs_cache_count);
510 /* select between SYNC and ASYNC I/O methods */
511 int obdfs_do_writepage(struct page *page, int sync)
513 struct inode *inode = page->mapping->host;
517 /* PDEBUG(page, "WRITEPAGE"); */
519 err = obdfs_brw(WRITE, inode, page, 1);
521 err = obdfs_add_page_to_cache(inode, page);
522 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
523 inode->i_ino, page, err, Page_Uptodate(page));
527 SetPageUptodate(page);
528 set_page_clean(page);
530 /* PDEBUG(page,"WRITEPAGE"); */
533 } /* obdfs_do_writepage */
540 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
542 struct inode *inode = page->mapping->host;
544 loff_t len = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
546 CDEBUG(D_INODE, "commit write ino %ld (end at %Ld) from %d to %d ,ind %ld\n",
547 inode->i_ino, len, from, to, page->index);
550 if (cache_writes == 0) {
551 rc = obdfs_commit_page(page, 1, from, to);
554 if (len > inode->i_size) {
555 obdfs_set_size(inode, len);
565 * This does the "real" work of the write. The generic routine has
566 * allocated the page, locked it, done all the page alignment stuff
567 * calculations etc. Now we should just copy the data from user
568 * space and write it back to the real medium..
570 * If the writer ends up delaying the write, the writer needs to
571 * increment the page use counts until he is done with the page.
573 * Return value is the number of bytes written.
575 int obdfs_write_one_page(struct file *file, struct page *page,
576 unsigned long offset, unsigned long bytes,
579 struct inode *inode = file->f_dentry->d_inode;
583 /* We check for complete page writes here, as we then don't have to
584 * get the page before writing over everything anyways.
586 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
587 err = obdfs_brw(READ, inode, page, 0);
590 SetPageUptodate(page);
593 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
597 err = obdfs_writepage(page);
600 return (err < 0 ? err : bytes);
601 } /* obdfs_write_one_page */
604 * return an up to date page:
605 * - if locked is true then is returned locked
606 * - if create is true the corresponding disk blocks are created
607 * - page is held, i.e. caller must release the page
609 * modeled on NFS code.
611 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
612 int create, int locked)
620 offset = offset & PAGE_CACHE_MASK;
621 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
622 inode->i_ino, offset, create, locked);
623 index = offset >> PAGE_CACHE_SHIFT;
625 page = grab_cache_page(&inode->i_data, index);
629 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
634 /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
635 /* now check if the data in the page is up to date */
636 if ( Page_Uptodate(page)) {
638 if (PageLocked(page))
639 obd_unlock_page(page);
641 printk("file %s, line %d: expecting locked page\n",
648 err = obdfs_brw(READ, inode, page, create);
652 obd_unlock_page(page);
658 obd_unlock_page(page);
659 SetPageUptodate(page);
660 /* PDEBUG(page,"GETPAGE - after reading"); */
663 } /* obdfs_getpage */
666 void obdfs_truncate(struct inode *inode)
672 //obdfs_dequeue_pages(inode);
674 if (IOPS(inode, punch) == NULL) {
675 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
682 /* XXX This would give an inconsistent FS, so deal with it as
683 * best we can for now - an obdo on the stack is not pretty.
687 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
689 obdo.o_valid = OBD_MD_FLNOTOBD;
690 obdfs_from_inode(&obdo, inode);
692 err = IOPS(inode, punch)(IID(inode), &obdo, 0, obdo.o_size);
694 oa->o_valid = OBD_MD_FLNOTOBD;
695 obdfs_from_inode(oa, inode);
697 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
698 (long)oa->o_id, oa->o_size);
699 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
705 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
710 } /* obdfs_truncate */
712 struct address_space_operations obdfs_aops = {
713 readpage: obdfs_readpage,
714 writepage: obdfs_writepage,
715 sync_page: block_sync_page,
716 prepare_write: obdfs_prepare_write,
717 commit_write: obdfs_commit_write,