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>
22 #include <linux/version.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
28 #include <linux/stat.h>
29 #include <asm/uaccess.h>
30 #include <linux/vmalloc.h>
31 #include <asm/segment.h>
33 #include <linux/pagemap.h>
34 #include <linux/smp_lock.h>
36 #define DEBUG_SUBSYSTEM S_OBDFS
38 #include <linux/obd_support.h>
39 #include <linux/obd_ext2.h>
40 #include <linux/obdfs.h>
42 void obdfs_change_inode(struct inode *inode);
44 static int cache_writes = 0;
47 /* page cache support stuff */
49 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10)
51 * Add a page to the dirty page list.
53 void __set_page_dirty(struct page *page)
55 struct address_space *mapping;
58 pg_lock = PAGECACHE_LOCK(page);
61 mapping = page->mapping;
62 spin_lock(&mapping->page_lock);
64 list_del(&page->list);
65 list_add(&page->list, &mapping->dirty_pages);
67 spin_unlock(&mapping->page_lock);
71 mark_inode_dirty_pages(mapping->host);
74 * Remove page from dirty list
76 void __set_page_clean(struct page *page)
78 struct address_space *mapping = page->mapping;
84 list_del(&page->list);
85 list_add(&page->list, &mapping->clean_pages);
87 inode = mapping->host;
88 if (list_empty(&mapping->dirty_pages)) {
89 CDEBUG(D_INODE, "inode clean\n");
90 inode->i_state &= ~I_DIRTY_PAGES;
97 * Add a page to the dirty page list.
99 void set_page_dirty(struct page *page)
101 if (!test_and_set_bit(PG_dirty, &page->flags)) {
102 struct address_space *mapping = page->mapping;
105 spin_lock(&pagecache_lock);
106 list_del(&page->list);
107 list_add(&page->list, &mapping->dirty_pages);
108 spin_unlock(&pagecache_lock);
111 mark_inode_dirty_pages(mapping->host);
116 * Remove page from dirty list
118 void __set_page_clean(struct page *page)
120 struct address_space *mapping = page->mapping;
126 spin_lock(&pagecache_lock);
127 list_del(&page->list);
128 list_add(&page->list, &mapping->clean_pages);
130 inode = mapping->host;
131 if (list_empty(&mapping->dirty_pages)) {
132 CDEBUG(D_INODE, "inode clean\n");
133 inode->i_state &= ~I_DIRTY_PAGES;
135 spin_unlock(&pagecache_lock);
142 inline void set_page_clean(struct page *page)
144 if (PageDirty(page)) {
145 ClearPageDirty(page);
146 __set_page_clean(page);
150 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
151 static int obdfs_brw(int rw, struct inode *inode2,
152 struct page *page, int create)
154 struct inode *inode = page->mapping->host;
155 struct ll_inode_info *lii = ll_i2info(inode);
156 obd_size count = PAGE_SIZE;
157 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
158 obd_flag flags = create ? OBD_BRW_CREATE : 0;
163 CHECK_MOUNT_EPOCH(inode);
164 CHECK_MOUNT_EPOCH(inode2);
166 err = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ, IID(inode),
167 md, 1, &page, &count, &offset,
170 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
177 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
178 static int obdfs_commit_page(struct page *page, int create, int from, int to)
180 struct inode *inode = page->mapping->host;
181 obd_count num_obdo = 1;
182 obd_count bufs_per_obdo = 1;
185 obd_off offset = (((obd_off)page->index) << PAGE_SHIFT);
186 obd_flag flags = create ? OBD_BRW_CREATE : 0;
191 CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
192 from, to, (unsigned long long)count);
194 err = obd_brw(OBD_BRW_WRITE, IID(inode), md, 1,
195 &page, &count, &offset, &flags, NULL);
197 SetPageUptodate(page);
198 set_page_clean(page);
202 // obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
209 /* returns the page unlocked, but with a reference */
210 int obdfs_writepage(struct page *page)
213 struct inode *inode = page->mapping->host;
215 CERROR("---> writepage called ino %ld!\n", inode->i_ino);
217 rc = obdfs_brw(OBD_BRW_WRITE, inode, page, 1);
219 set_page_clean(page);
221 CDEBUG(D_INODE, "--> GRR %d\n", rc);
228 void write_inode_pages(struct inode *inode)
230 struct list_head *tmp = &inode->i_mapping->dirty_pages;
232 while ( (tmp = tmp->next) != &inode->i_mapping->dirty_pages) {
234 page = list_entry(tmp, struct page, list);
235 obdfs_writepage(page);
240 /* returns the page unlocked, but with a reference */
241 int obdfs_readpage(struct file *file, struct page *page)
243 struct inode *inode = page->mapping->host;
248 if ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT)
250 memset(kmap(page), 0, PAGE_CACHE_SIZE);
255 if (Page_Uptodate(page)) {
260 rc = obdfs_brw(OBD_BRW_READ, inode, page, 0);
267 SetPageUptodate(page);
271 } /* obdfs_readpage */
273 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
275 struct inode *inode = page->mapping->host;
276 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
281 if (Page_Uptodate(page)) {
286 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
291 rc = obdfs_brw(OBD_BRW_READ, inode, page, 0);
293 SetPageUptodate(page);
296 set_page_dirty(page);
297 //SetPageDirty(page);
307 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
309 int obdfs_init_pgrqcache(void)
312 if (obdfs_pgrq_cachep == NULL) {
313 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
314 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
315 sizeof(struct obdfs_pgrq),
316 0, SLAB_HWCACHE_ALIGN,
318 if (obdfs_pgrq_cachep == NULL) {
322 CDEBUG(D_CACHE, "allocated cache at %p\n",
326 CDEBUG(D_CACHE, "using existing cache at %p\n",
331 } /* obdfs_init_wreqcache */
333 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
336 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
337 pgrq->rq_page, obdfs_cache_count);
338 list_del(&pgrq->rq_plist);
339 OBDClearCachePage(pgrq->rq_page);
340 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
343 void obdfs_cleanup_pgrqcache(void)
346 if (obdfs_pgrq_cachep != NULL) {
347 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
348 obdfs_pgrq_cachep, obdfs_cache_count);
349 if (kmem_cache_destroy(obdfs_pgrq_cachep))
350 CERROR("unable to free all of cache\n");
351 obdfs_pgrq_cachep = NULL;
353 CERROR("called with NULL pointer\n");
356 } /* obdfs_cleanup_wreqcache */
359 /* called with the list lock held */
360 static struct page *obdfs_find_page_index(struct inode *inode,
363 struct list_head *page_list = obdfs_iplist(inode);
364 struct list_head *tmp;
369 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
370 inode->i_ino, index);
373 if (list_empty(page_list)) {
378 while ( (tmp = tmp->next) != page_list ) {
379 struct obdfs_pgrq *pgrq;
381 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
382 page = pgrq->rq_page;
383 if (index == page->index) {
385 "INDEX SEARCH found page %p, index %ld\n",
394 } /* obdfs_find_page_index */
397 /* call and free pages from Linux page cache: called with io lock on inodes */
398 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
399 obd_count num_obdos, struct obdo **obdos,
400 obd_count *oa_bufs, struct page **pages, char **bufs,
401 obd_size *counts, obd_off *offsets, obd_flag *flags)
406 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
408 if (obd_debug_level & D_INFO) { /* DEBUGGING */
411 for (i = 0; i < num_obdos; i++)
412 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
415 for (i = 0; i < num_io; i++)
416 printk("0x%p ", pages[i]);
420 err = obd_brw(OBD_BRW_WRITE, IID(inodes[0]), num_obdos, obdos,
421 oa_bufs, pages, counts, offsets, flags);
423 CDEBUG(D_INFO, "BRW done\n");
424 /* release the pages from the page cache */
425 while (num_io-- > 0) {
426 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
427 pages[num_io], pages[num_io]->index);
428 put_page(pages[num_io]);
430 CDEBUG(D_INFO, "put_page done\n");
432 while (num_obdos-- > 0) {
433 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
434 /* copy o_blocks to i_blocks */
435 obdfs_set_size (inodes[num_obdos], obdos[num_obdos]->o_size);
436 //obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
437 obdo_free(obdos[num_obdos]);
439 CDEBUG(D_INFO, "obdo_free done\n");
446 * Add a page to the write request cache list for later writing.
447 * ASYNCHRONOUS write method.
449 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
454 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
455 * is written, so at worst we will write the page out twice.
457 * If the page has the PG_obdcache bit set, then the inode MUST be
458 * on the superblock dirty list so we don't need to check this.
459 * Dirty inodes are removed from the superblock list ONLY when they
460 * don't have any more cached pages. It is possible to have an inode
461 * with no dirty pages on the superblock list, but not possible to
462 * have an inode with dirty pages NOT on the superblock dirty list.
464 if (!OBDAddCachePage(page)) {
465 struct obdfs_pgrq *pgrq;
466 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
468 OBDClearCachePage(page);
472 /* not really necessary since we set all pgrq fields here
473 memset(pgrq, 0, sizeof(*pgrq));
476 pgrq->rq_page = page;
477 pgrq->rq_jiffies = jiffies;
478 get_page(pgrq->rq_page);
480 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
481 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
483 //CERROR("-- count %d\n", obdfs_cache_count);
485 /* If inode isn't already on superblock inodes list, add it.
487 * We increment the reference count on the inode to keep it
488 * from being freed from memory. This _should_ be an iget()
489 * with an iput() in both flush_reqs() and put_inode(), but
490 * since put_inode() is called from iput() we can't call iput()
491 * again there. Instead we just increment/decrement i_count,
492 * which is mostly what iget/iput do for an inode in memory.
494 if ( list_empty(obdfs_islist(inode)) ) {
495 atomic_inc(&inode->i_count);
497 "adding inode %ld to superblock list %p\n",
498 inode->i_ino, obdfs_slist(inode));
499 list_add(obdfs_islist(inode), obdfs_slist(inode));
501 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
505 /* XXX For testing purposes, we can write out the page here.
506 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
511 } /* obdfs_add_page_to_cache */
515 if (obdfs_cache_count > 60000) {
516 CERROR("-- count %ld\n", obdfs_cache_count);
517 //obdfs_flush_dirty_pages(~0UL);
518 CERROR("-- count %ld\n", obdfs_cache_count);
524 /* select between SYNC and ASYNC I/O methods */
525 int obdfs_do_writepage(struct page *page, int sync)
527 struct inode *inode = page->mapping->host;
532 err = obdfs_brw(OBD_BRW_WRITE, inode, page, 1);
534 err = obdfs_add_page_to_cache(inode, page);
535 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
536 inode->i_ino, page, err, Page_Uptodate(page));
540 SetPageUptodate(page);
541 set_page_clean(page);
545 } /* obdfs_do_writepage */
552 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
554 struct inode *inode = page->mapping->host;
556 loff_t len = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
558 CDEBUG(D_INODE, "commit write ino %ld (end at %Ld) from %d to %d ,ind %ld\n",
559 inode->i_ino, len, from, to, page->index);
562 if (cache_writes == 0) {
563 rc = obdfs_commit_page(page, 1, from, to);
566 if (len > inode->i_size) {
567 obdfs_set_size(inode, len);
577 * This does the "real" work of the write. The generic routine has
578 * allocated the page, locked it, done all the page alignment stuff
579 * calculations etc. Now we should just copy the data from user
580 * space and write it back to the real medium..
582 * If the writer ends up delaying the write, the writer needs to
583 * increment the page use counts until he is done with the page.
585 * Return value is the number of bytes written.
587 int obdfs_write_one_page(struct file *file, struct page *page,
588 unsigned long offset, unsigned long bytes,
591 struct inode *inode = file->f_dentry->d_inode;
595 /* We check for complete page writes here, as we then don't have to
596 * get the page before writing over everything anyways.
598 if (!Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE)) {
599 err = obdfs_brw(OBD_BRW_READ, inode, page, 0);
602 SetPageUptodate(page);
605 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
609 err = obdfs_writepage(page);
612 return (err < 0 ? err : bytes);
613 } /* obdfs_write_one_page */
616 * return an up to date page:
617 * - if locked is true then is returned locked
618 * - if create is true the corresponding disk blocks are created
619 * - page is held, i.e. caller must release the page
621 * modeled on NFS code.
623 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
624 int create, int locked)
632 offset = offset & PAGE_CACHE_MASK;
633 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
634 inode->i_ino, offset, create, locked);
635 index = offset >> PAGE_CACHE_SHIFT;
637 page = grab_cache_page(&inode->i_data, index);
641 CERROR("grab_cache_page says no dice ...\n");
646 /* now check if the data in the page is up to date */
647 if ( Page_Uptodate(page)) {
649 if (PageLocked(page))
652 CERROR("expecting locked page\n");
658 err = obdfs_brw(OBD_BRW_READ, inode, page, create);
669 SetPageUptodate(page);
672 } /* obdfs_getpage */
675 void obdfs_truncate(struct inode *inode)
681 //obdfs_dequeue_pages(inode);
685 CERROR("obdo_alloc failed!\n");
687 oa->o_valid = OBD_MD_FLNOTOBD;
688 obdfs_from_inode(oa, inode);
690 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
691 (long)oa->o_id, (unsigned long long)oa->o_size);
692 err = obd_punch(IID(inode), oa, oa->o_size, 0);
698 CERROR("obd_truncate fails (%d)\n", err);
703 } /* obdfs_truncate */
705 struct address_space_operations obdfs_aops = {
706 readpage: obdfs_readpage,
707 writepage: obdfs_writepage,
708 sync_page: block_sync_page,
709 prepare_write: obdfs_prepare_write,
710 commit_write: obdfs_commit_write,