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;
117 oa->o_valid = OBD_MD_FLNOTOBD;
118 obdfs_from_inode(oa, inode);
120 err = obd_brw(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
121 &page, &count, &offset, &flags);
123 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
130 extern void set_page_clean(struct page *);
132 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
133 static int obdfs_commit_page(struct page *page, int create, int from, int to)
135 struct inode *inode = page->mapping->host;
136 obd_count num_obdo = 1;
137 obd_count bufs_per_obdo = 1;
140 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
141 obd_flag flags = create ? OBD_BRW_CREATE : 0;
150 oa->o_valid = OBD_MD_FLNOTOBD;
151 obdfs_from_inode(oa, inode);
153 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
156 err = obd_brw(WRITE, IID(inode), num_obdo, &oa, &bufs_per_obdo,
157 &page, &count, &offset, &flags);
159 SetPageUptodate(page);
160 set_page_clean(page);
164 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
171 /* returns the page unlocked, but with a reference */
172 int obdfs_writepage(struct page *page)
175 struct inode *inode = page->mapping->host;
177 printk("---> writepage called ino %ld!\n", inode->i_ino);
179 rc = obdfs_brw(WRITE, inode, page, 1);
181 set_page_clean(page);
183 CDEBUG(D_INODE, "--> GRR %d\n", rc);
190 void write_inode_pages(struct inode *inode)
192 struct list_head *tmp = &inode->i_mapping->dirty_pages;
194 while ( (tmp = tmp->next) != &inode->i_mapping->dirty_pages) {
196 page = list_entry(tmp, struct page, list);
197 obdfs_writepage(page);
202 /* returns the page unlocked, but with a reference */
203 int obdfs_readpage(struct file *file, struct page *page)
205 struct inode *inode = page->mapping->host;
210 if ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT)
212 memset(kmap(page), 0, PAGE_CACHE_SIZE);
217 if (Page_Uptodate(page)) {
222 rc = obdfs_brw(READ, inode, page, 0);
229 SetPageUptodate(page);
230 obd_unlock_page(page);
233 } /* obdfs_readpage */
235 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
237 struct inode *inode = page->mapping->host;
238 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
243 if (Page_Uptodate(page)) {
248 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
253 rc = obdfs_brw(READ, inode, page, 0);
255 SetPageUptodate(page);
259 set_page_dirty(page);
260 //SetPageDirty(page);
270 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
272 int obdfs_init_pgrqcache(void)
275 if (obdfs_pgrq_cachep == NULL) {
276 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
277 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
278 sizeof(struct obdfs_pgrq),
279 0, SLAB_HWCACHE_ALIGN,
281 if (obdfs_pgrq_cachep == NULL) {
285 CDEBUG(D_CACHE, "allocated cache at %p\n",
289 CDEBUG(D_CACHE, "using existing cache at %p\n",
294 } /* obdfs_init_wreqcache */
296 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
299 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
300 pgrq->rq_page, obdfs_cache_count);
301 list_del(&pgrq->rq_plist);
302 OBDClearCachePage(pgrq->rq_page);
303 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
306 void obdfs_cleanup_pgrqcache(void)
309 if (obdfs_pgrq_cachep != NULL) {
310 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
311 obdfs_pgrq_cachep, obdfs_cache_count);
312 if (kmem_cache_destroy(obdfs_pgrq_cachep))
313 printk(KERN_INFO __FUNCTION__
314 ": unable to free all of cache\n");
315 obdfs_pgrq_cachep = NULL;
317 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
320 } /* obdfs_cleanup_wreqcache */
323 /* called with the list lock held */
324 static struct page *obdfs_find_page_index(struct inode *inode,
327 struct list_head *page_list = obdfs_iplist(inode);
328 struct list_head *tmp;
333 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
334 inode->i_ino, index);
337 if (list_empty(page_list)) {
342 while ( (tmp = tmp->next) != page_list ) {
343 struct obdfs_pgrq *pgrq;
345 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
346 page = pgrq->rq_page;
347 if (index == page->index) {
349 "INDEX SEARCH found page %p, index %ld\n",
358 } /* obdfs_find_page_index */
361 /* call and free pages from Linux page cache: called with io lock on inodes */
362 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
363 obd_count num_obdos, struct obdo **obdos,
364 obd_count *oa_bufs, struct page **pages, char **bufs,
365 obd_size *counts, obd_off *offsets, obd_flag *flags)
370 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
372 if (obd_debug_level & D_INFO) { /* DEBUGGING */
375 for (i = 0; i < num_obdos; i++)
376 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
379 for (i = 0; i < num_io; i++)
380 printk("0x%p ", pages[i]);
384 err = obd_brw(WRITE, IID(inodes[0]), num_obdos, obdos,
385 oa_bufs, pages, counts, offsets, flags);
387 CDEBUG(D_INFO, "BRW done\n");
388 /* release the pages from the page cache */
389 while ( num_io > 0 ) {
391 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
392 pages[num_io], pages[num_io]->index);
393 /* PDEBUG(pages[num_io], "do_vec_wr"); */
394 put_page(pages[num_io]);
395 /* PDEBUG(pages[num_io], "do_vec_wr"); */
397 CDEBUG(D_INFO, "put_page done\n");
399 while ( num_obdos > 0) {
401 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
402 /* copy o_blocks to i_blocks */
403 obdfs_set_size (inodes[num_obdos], obdos[num_obdos]->o_size);
404 //obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
405 obdo_free(obdos[num_obdos]);
407 CDEBUG(D_INFO, "obdo_free done\n");
414 * Add a page to the write request cache list for later writing.
415 * ASYNCHRONOUS write method.
417 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
422 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
423 * is written, so at worst we will write the page out twice.
425 * If the page has the PG_obdcache bit set, then the inode MUST be
426 * on the superblock dirty list so we don't need to check this.
427 * Dirty inodes are removed from the superblock list ONLY when they
428 * don't have any more cached pages. It is possible to have an inode
429 * with no dirty pages on the superblock list, but not possible to
430 * have an inode with dirty pages NOT on the superblock dirty list.
432 if (!OBDAddCachePage(page)) {
433 struct obdfs_pgrq *pgrq;
434 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
436 OBDClearCachePage(page);
440 /* not really necessary since we set all pgrq fields here
441 memset(pgrq, 0, sizeof(*pgrq));
444 pgrq->rq_page = page;
445 pgrq->rq_jiffies = jiffies;
446 get_page(pgrq->rq_page);
448 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
449 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
451 //printk("-- count %d\n", obdfs_cache_count);
453 /* If inode isn't already on superblock inodes list, add it.
455 * We increment the reference count on the inode to keep it
456 * from being freed from memory. This _should_ be an iget()
457 * with an iput() in both flush_reqs() and put_inode(), but
458 * since put_inode() is called from iput() we can't call iput()
459 * again there. Instead we just increment/decrement i_count,
460 * which is mostly what iget/iput do for an inode in memory.
462 if ( list_empty(obdfs_islist(inode)) ) {
463 atomic_inc(&inode->i_count);
465 "adding inode %ld to superblock list %p\n",
466 inode->i_ino, obdfs_slist(inode));
467 list_add(obdfs_islist(inode), obdfs_slist(inode));
469 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
473 /* XXX For testing purposes, we can write out the page here.
474 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
479 } /* obdfs_add_page_to_cache */
483 if (obdfs_cache_count > 60000) {
484 printk("-- count %ld\n", obdfs_cache_count);
485 //obdfs_flush_dirty_pages(~0UL);
486 printk("-- count %ld\n", obdfs_cache_count);
492 /* select between SYNC and ASYNC I/O methods */
493 int obdfs_do_writepage(struct page *page, int sync)
495 struct inode *inode = page->mapping->host;
499 /* PDEBUG(page, "WRITEPAGE"); */
501 err = obdfs_brw(WRITE, inode, page, 1);
503 err = obdfs_add_page_to_cache(inode, page);
504 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
505 inode->i_ino, page, err, Page_Uptodate(page));
509 SetPageUptodate(page);
510 set_page_clean(page);
512 /* PDEBUG(page,"WRITEPAGE"); */
515 } /* obdfs_do_writepage */
522 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
524 struct inode *inode = page->mapping->host;
526 loff_t len = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
528 CDEBUG(D_INODE, "commit write ino %ld (end at %Ld) from %d to %d ,ind %ld\n",
529 inode->i_ino, len, from, to, page->index);
532 if (cache_writes == 0) {
533 rc = obdfs_commit_page(page, 1, from, to);
536 if (len > inode->i_size) {
537 obdfs_set_size(inode, len);
547 * This does the "real" work of the write. The generic routine has
548 * allocated the page, locked it, done all the page alignment stuff
549 * calculations etc. Now we should just copy the data from user
550 * space and write it back to the real medium..
552 * If the writer ends up delaying the write, the writer needs to
553 * increment the page use counts until he is done with the page.
555 * Return value is the number of bytes written.
557 int obdfs_write_one_page(struct file *file, struct page *page,
558 unsigned long offset, unsigned long bytes,
561 struct inode *inode = file->f_dentry->d_inode;
565 /* We check for complete page writes here, as we then don't have to
566 * get the page before writing over everything anyways.
568 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
569 err = obdfs_brw(READ, inode, page, 0);
572 SetPageUptodate(page);
575 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
579 err = obdfs_writepage(page);
582 return (err < 0 ? err : bytes);
583 } /* obdfs_write_one_page */
586 * return an up to date page:
587 * - if locked is true then is returned locked
588 * - if create is true the corresponding disk blocks are created
589 * - page is held, i.e. caller must release the page
591 * modeled on NFS code.
593 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
594 int create, int locked)
602 offset = offset & PAGE_CACHE_MASK;
603 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
604 inode->i_ino, offset, create, locked);
605 index = offset >> PAGE_CACHE_SHIFT;
607 page = grab_cache_page(&inode->i_data, index);
611 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
616 /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
617 /* now check if the data in the page is up to date */
618 if ( Page_Uptodate(page)) {
620 if (PageLocked(page))
621 obd_unlock_page(page);
623 printk("file %s, line %d: expecting locked page\n",
630 err = obdfs_brw(READ, inode, page, create);
634 obd_unlock_page(page);
640 obd_unlock_page(page);
641 SetPageUptodate(page);
642 /* PDEBUG(page,"GETPAGE - after reading"); */
645 } /* obdfs_getpage */
648 void obdfs_truncate(struct inode *inode)
654 //obdfs_dequeue_pages(inode);
658 printk(__FUNCTION__ ": obdo_alloc failed!\n");
660 oa->o_valid = OBD_MD_FLNOTOBD;
661 obdfs_from_inode(oa, inode);
663 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
664 (long)oa->o_id, oa->o_size);
665 err = obd_punch(IID(inode), oa, oa->o_size, 0);
671 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
676 } /* obdfs_truncate */
678 struct address_space_operations obdfs_aops = {
679 readpage: obdfs_readpage,
680 writepage: obdfs_writepage,
681 sync_page: block_sync_page,
682 prepare_write: obdfs_prepare_write,
683 commit_write: obdfs_commit_write,