+/*
+ * Find a specific page in the page cache. If it is found, we return
+ * the write request struct associated with it, if not found return NULL.
+ * Called with the list lock held.
+ */
+static struct obdfs_pgrq *
+obdfs_find_in_page_list(struct inode *inode, struct page *page)
+{
+ struct list_head *page_list = obdfs_iplist(inode);
+ struct list_head *tmp;
+
+ ENTRY;
+
+ CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
+ OIDEBUG(inode);
+
+ if (list_empty(page_list)) {
+ CDEBUG(D_INODE, "empty list\n");
+ EXIT;
+ return NULL;
+ }
+ tmp = page_list;
+ while ( (tmp = tmp->next) != page_list ) {
+ struct obdfs_pgrq *pgrq;
+
+ pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
+ if (pgrq->rq_page == page) {
+ CDEBUG(D_INODE, "found page %p in list\n", page);
+ EXIT;
+ return pgrq;
+ }
+ }
+
+ EXIT;
+ return NULL;
+} /* obdfs_find_in_page_list */
+
+
+/* called with the list lock held */
+static struct page* obdfs_find_page_index(struct inode *inode,
+ unsigned long index)
+{
+ struct list_head *page_list = obdfs_iplist(inode);
+ struct list_head *tmp;
+ struct page *page;
+
+ ENTRY;
+
+ CDEBUG(D_INODE, "looking for inode %ld pageindex %ld\n",
+ inode->i_ino, index);
+ OIDEBUG(inode);
+
+ if (list_empty(page_list)) {
+ EXIT;
+ return NULL;
+ }
+ tmp = page_list;
+ while ( (tmp = tmp->next) != page_list ) {
+ struct obdfs_pgrq *pgrq;
+
+ pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
+ page = pgrq->rq_page;
+ if (index == page->index) {
+ CDEBUG(D_INODE,
+ "INDEX SEARCH found page %p, index %ld\n",
+ page, index);
+ EXIT;
+ return page;
+ }
+ }
+
+ EXIT;
+ return NULL;
+} /* obdfs_find_page_index */
+
+
+/* call and free pages from Linux page cache: called with io lock on inodes */
+int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
+ obd_count num_obdos, struct obdo **obdos,
+ obd_count *oa_bufs, struct page **pages, char **bufs,
+ obd_size *counts, obd_off *offsets, obd_flag *flags)
+{
+ struct super_block *sb = inodes[0]->i_sb;
+ struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
+ int err;
+
+ ENTRY;
+ CDEBUG(D_INODE, "writing %d page(s), %d obdo(s) in vector\n",
+ num_io, num_obdos);
+ err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
+ bufs, counts, offsets, flags);
+
+ /* release the pages from the page cache */
+ while ( num_io > 0 ) {
+ num_io--;
+ CDEBUG(D_INODE, "calling put_page for %p, index %ld\n",
+ pages[num_io], pages[num_io]->index);
+ put_page(pages[num_io]);
+ }
+
+ while ( num_obdos > 0) {
+ num_obdos--;
+ CDEBUG(D_INODE, "copy/free obdo %ld\n",
+ (long)obdos[num_obdos]->o_id);
+ obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
+ obdo_free(obdos[num_obdos]);
+ }
+ EXIT;
+ return err;
+}
+
+
+/*
+ * Add a page to the write request cache list for later writing
+ * ASYNCHRONOUS write method.
+ */
+static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
+{
+ int res = 0;
+
+ ENTRY;
+
+ /* If this page isn't already in the inode page list, add it */
+ obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
+ if ( !obdfs_find_in_page_list(inode, page) ) {
+ struct obdfs_pgrq *pgrq;
+ pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
+ CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p, cache count [%d]\n",
+ inode->i_ino, page, pgrq, obdfs_cache_count);
+ if (!pgrq) {
+ EXIT;
+ obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
+ return -ENOMEM;
+ }
+ memset(pgrq, 0, sizeof(*pgrq));
+
+ pgrq->rq_page = page;
+ get_page(pgrq->rq_page);
+ list_add(&pgrq->rq_plist, obdfs_iplist(inode));
+ obdfs_cache_count++;
+ }
+
+ /* If inode isn't already on the superblock inodes list, add it,
+ * and increase ref count on inode so it doesn't disappear on us.
+ */
+ if ( list_empty(obdfs_islist(inode)) ) {
+ iget(inode->i_sb, inode->i_ino);
+ CDEBUG(D_INODE, "adding inode %ld to superblock list %p\n",
+ inode->i_ino, obdfs_slist(inode));
+ list_add(obdfs_islist(inode), obdfs_slist(inode));
+ }
+
+ /* XXX For testing purposes, we write out the page here.
+ * In the future, a flush daemon will write out the page.
+ res = obdfs_flush_reqs(obdfs_slist(inode), 0);
+ obdfs_flush_dirty_pages(1);
+ */
+ obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
+
+ EXIT;
+ return res;
+} /* obdfs_add_page_to_cache */
+
+
+/* select between SYNC and ASYNC I/O methods */
+int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
+{
+ int err;
+
+ ENTRY;
+ /* PDEBUG(page, "WRITEPAGE"); */
+ if ( sync )
+ err = obdfs_brw(WRITE, inode, page, 1);
+ else {
+ err = obdfs_add_page_to_cache(inode, page);
+ CDEBUG(D_IOCTL, "DO_WR ino: %ld, page %p, err %d, uptodata %d\n", inode->i_ino, page, err, Page_Uptodate(page));
+ }
+
+ if ( !err )
+ SetPageUptodate(page);
+ /* PDEBUG(page,"WRITEPAGE"); */
+ EXIT;
+ return err;
+} /* obdfs_do_writepage */
+
+/* returns the page unlocked, but with a reference */
+int obdfs_writepage(struct dentry *dentry, struct page *page)
+{
+ return obdfs_do_writepage(dentry->d_inode, page, 0);