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 */
183 /* returns the page unlocked, but with a reference */
184 int obdfs_readpage(struct file *file, struct page *page)
186 struct inode *inode = page->mapping->host;
191 if ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT)
193 memset(kmap(page), 0, PAGE_CACHE_SIZE);
198 if (Page_Uptodate(page)) {
203 rc = obdfs_brw(READ, inode, page, 0);
208 /* PDEBUG(page, "READ"); */
211 SetPageUptodate(page);
212 obd_unlock_page(page);
215 } /* obdfs_readpage */
217 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
219 struct inode *inode = page->mapping->host;
220 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
225 if (Page_Uptodate(page)) {
230 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
235 rc = obdfs_brw(READ, inode, page, 0);
237 SetPageUptodate(page);
241 set_page_dirty(page);
242 //SetPageDirty(page);
252 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
254 int obdfs_init_pgrqcache(void)
257 if (obdfs_pgrq_cachep == NULL) {
258 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
259 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
260 sizeof(struct obdfs_pgrq),
261 0, SLAB_HWCACHE_ALIGN,
263 if (obdfs_pgrq_cachep == NULL) {
267 CDEBUG(D_CACHE, "allocated cache at %p\n",
271 CDEBUG(D_CACHE, "using existing cache at %p\n",
276 } /* obdfs_init_wreqcache */
278 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
281 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
282 pgrq->rq_page, obdfs_cache_count);
283 list_del(&pgrq->rq_plist);
284 OBDClearCachePage(pgrq->rq_page);
285 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
288 void obdfs_cleanup_pgrqcache(void)
291 if (obdfs_pgrq_cachep != NULL) {
292 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
293 obdfs_pgrq_cachep, obdfs_cache_count);
294 if (kmem_cache_destroy(obdfs_pgrq_cachep))
295 printk(KERN_INFO __FUNCTION__
296 ": unable to free all of cache\n");
297 obdfs_pgrq_cachep = NULL;
299 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
302 } /* obdfs_cleanup_wreqcache */
305 /* called with the list lock held */
306 static struct page *obdfs_find_page_index(struct inode *inode,
309 struct list_head *page_list = obdfs_iplist(inode);
310 struct list_head *tmp;
315 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
316 inode->i_ino, index);
319 if (list_empty(page_list)) {
324 while ( (tmp = tmp->next) != page_list ) {
325 struct obdfs_pgrq *pgrq;
327 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
328 page = pgrq->rq_page;
329 if (index == page->index) {
331 "INDEX SEARCH found page %p, index %ld\n",
340 } /* obdfs_find_page_index */
343 /* call and free pages from Linux page cache: called with io lock on inodes */
344 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
345 obd_count num_obdos, struct obdo **obdos,
346 obd_count *oa_bufs, struct page **pages, char **bufs,
347 obd_size *counts, obd_off *offsets, obd_flag *flags)
352 if (IOPS(inodes[0], brw) == NULL) {
353 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
358 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
360 if (obd_debug_level & D_INFO) { /* DEBUGGING */
363 for (i = 0; i < num_obdos; i++)
364 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
367 for (i = 0; i < num_io; i++)
368 printk("0x%p ", pages[i]);
372 err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
373 oa_bufs, pages, counts, offsets, flags);
375 CDEBUG(D_INFO, "BRW done\n");
376 /* release the pages from the page cache */
377 while ( num_io > 0 ) {
379 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
380 pages[num_io], pages[num_io]->index);
381 /* PDEBUG(pages[num_io], "do_vec_wr"); */
382 put_page(pages[num_io]);
383 /* PDEBUG(pages[num_io], "do_vec_wr"); */
385 CDEBUG(D_INFO, "put_page done\n");
387 while ( num_obdos > 0) {
389 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
390 /* copy o_blocks to i_blocks */
391 obdfs_set_size (inodes[num_obdos], obdos[num_obdos]->o_size);
392 //obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
393 obdo_free(obdos[num_obdos]);
395 CDEBUG(D_INFO, "obdo_free done\n");
402 * Add a page to the write request cache list for later writing.
403 * ASYNCHRONOUS write method.
405 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
410 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
411 * is written, so at worst we will write the page out twice.
413 * If the page has the PG_obdcache bit set, then the inode MUST be
414 * on the superblock dirty list so we don't need to check this.
415 * Dirty inodes are removed from the superblock list ONLY when they
416 * don't have any more cached pages. It is possible to have an inode
417 * with no dirty pages on the superblock list, but not possible to
418 * have an inode with dirty pages NOT on the superblock dirty list.
420 if (!OBDAddCachePage(page)) {
421 struct obdfs_pgrq *pgrq;
422 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
424 OBDClearCachePage(page);
428 /* not really necessary since we set all pgrq fields here
429 memset(pgrq, 0, sizeof(*pgrq));
432 pgrq->rq_page = page;
433 pgrq->rq_jiffies = jiffies;
434 get_page(pgrq->rq_page);
436 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
437 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
439 //printk("-- count %d\n", obdfs_cache_count);
441 /* If inode isn't already on superblock inodes list, add it.
443 * We increment the reference count on the inode to keep it
444 * from being freed from memory. This _should_ be an iget()
445 * with an iput() in both flush_reqs() and put_inode(), but
446 * since put_inode() is called from iput() we can't call iput()
447 * again there. Instead we just increment/decrement i_count,
448 * which is mostly what iget/iput do for an inode in memory.
450 if ( list_empty(obdfs_islist(inode)) ) {
451 atomic_inc(&inode->i_count);
453 "adding inode %ld to superblock list %p\n",
454 inode->i_ino, obdfs_slist(inode));
455 list_add(obdfs_islist(inode), obdfs_slist(inode));
457 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
461 /* XXX For testing purposes, we can write out the page here.
462 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
467 } /* obdfs_add_page_to_cache */
471 if (obdfs_cache_count > 60000) {
472 printk("-- count %ld\n", obdfs_cache_count);
473 //obdfs_flush_dirty_pages(~0UL);
474 printk("-- count %ld\n", obdfs_cache_count);
478 /* select between SYNC and ASYNC I/O methods */
479 int obdfs_do_writepage(struct page *page, int sync)
481 struct inode *inode = page->mapping->host;
485 /* PDEBUG(page, "WRITEPAGE"); */
487 err = obdfs_brw(WRITE, inode, page, 1);
489 err = obdfs_add_page_to_cache(inode, page);
490 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
491 inode->i_ino, page, err, Page_Uptodate(page));
495 SetPageUptodate(page);
496 set_page_clean(page);
498 /* PDEBUG(page,"WRITEPAGE"); */
501 } /* obdfs_do_writepage */
505 /* returns the page unlocked, but with a reference */
506 int obdfs_writepage(struct page *page)
509 struct inode *inode = page->mapping->host;
511 printk("---> writepage called ino %ld!\n", inode->i_ino);
513 rc = obdfs_do_writepage(page, 1);
515 set_page_clean(page);
517 CDEBUG(D_INODE, "--> GRR %d\n", rc);
523 void write_inode_pages(struct inode *inode)
525 struct list_head *tmp = &inode->i_mapping->dirty_pages;
527 while ( (tmp = tmp->next) != &inode->i_mapping->dirty_pages) {
529 page = list_entry(tmp, struct page, list);
530 obdfs_writepage(page);
535 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
537 struct inode *inode = page->mapping->host;
539 loff_t len = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
541 CDEBUG(D_INODE, "commit write ino %ld (end at %Ld) from %d to %d ,ind %ld\n",
542 inode->i_ino, len, from, to, page->index);
545 if (cache_writes == 0) {
546 rc = obdfs_commit_page(page, 1, from, to);
549 if (len > inode->i_size) {
550 obdfs_set_size(inode, len);
560 * This does the "real" work of the write. The generic routine has
561 * allocated the page, locked it, done all the page alignment stuff
562 * calculations etc. Now we should just copy the data from user
563 * space and write it back to the real medium..
565 * If the writer ends up delaying the write, the writer needs to
566 * increment the page use counts until he is done with the page.
568 * Return value is the number of bytes written.
570 int obdfs_write_one_page(struct file *file, struct page *page,
571 unsigned long offset, unsigned long bytes,
574 struct inode *inode = file->f_dentry->d_inode;
578 /* We check for complete page writes here, as we then don't have to
579 * get the page before writing over everything anyways.
581 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
582 err = obdfs_brw(READ, inode, page, 0);
585 SetPageUptodate(page);
588 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
592 err = obdfs_writepage(page);
595 return (err < 0 ? err : bytes);
596 } /* obdfs_write_one_page */
599 * return an up to date page:
600 * - if locked is true then is returned locked
601 * - if create is true the corresponding disk blocks are created
602 * - page is held, i.e. caller must release the page
604 * modeled on NFS code.
606 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
607 int create, int locked)
615 offset = offset & PAGE_CACHE_MASK;
616 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
617 inode->i_ino, offset, create, locked);
618 index = offset >> PAGE_CACHE_SHIFT;
620 page = grab_cache_page(&inode->i_data, index);
624 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
629 /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
630 /* now check if the data in the page is up to date */
631 if ( Page_Uptodate(page)) {
633 if (PageLocked(page))
634 obd_unlock_page(page);
636 printk("file %s, line %d: expecting locked page\n",
644 #ifdef EXT2_OBD_DEBUG
645 if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
646 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
651 err = obdfs_brw(READ, inode, page, create);
655 obd_unlock_page(page);
661 obd_unlock_page(page);
662 SetPageUptodate(page);
663 /* PDEBUG(page,"GETPAGE - after reading"); */
666 } /* obdfs_getpage */
669 void obdfs_truncate(struct inode *inode)
675 //obdfs_dequeue_pages(inode);
677 if (IOPS(inode, punch) == NULL) {
678 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
685 /* XXX This would give an inconsistent FS, so deal with it as
686 * best we can for now - an obdo on the stack is not pretty.
690 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
692 obdo.o_valid = OBD_MD_FLNOTOBD;
693 obdfs_from_inode(&obdo, inode);
695 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
697 oa->o_valid = OBD_MD_FLNOTOBD;
698 obdfs_from_inode(oa, inode);
700 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
701 (long)oa->o_id, oa->o_size);
702 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
708 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
713 } /* obdfs_truncate */
715 struct address_space_operations obdfs_aops = {
716 readpage: obdfs_readpage,
717 writepage: obdfs_writepage,
718 sync_page: block_sync_page,
719 prepare_write: obdfs_prepare_write,
720 commit_write: obdfs_commit_write,