Whamcloud - gitweb
Added code for obdfs to do writes to files and reads of directories and
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copryright (C) 1999 Stelias Computing Inc, 
5  *                (author Peter J. Braam <braam@stelias.com>)
6  * Copryright (C) 1999 Seagate Technology Inc.
7  */
8
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/errno.h>
16 #include <linux/locks.h>
17 #include <linux/unistd.h>
18
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <asm/uaccess.h>
25 #include <linux/vmalloc.h>
26 #include <asm/segment.h>
27 #include <linux/mm.h>
28 #include <linux/pagemap.h>
29 #include <linux/smp_lock.h>
30
31 #include <../obd/linux/obd_support.h>
32 #include <../obd/linux/obd_sim.h>
33 #include <obdfs.h>
34
35 /* VFS super_block ops */
36
37 /* returns the page unlocked, but with a reference */
38 int obdfs_readpage(struct file *file, struct page *page)
39 {
40         struct obdfs_sb_info *sbi;
41         struct super_block *sb = file->f_dentry->d_inode->i_sb;
42         int rc;
43
44         ENTRY;
45
46         /* XXX flush stuff */
47         sbi = sb->u.generic_sbp;
48         PDEBUG(page, READ);
49         rc =  sbi->osi_ops->o_brw(READ, sbi->osi_conn_info.conn_id, 
50                       file->f_dentry->d_inode->i_ino, page);
51         if (rc == PAGE_SIZE ) {
52                 SetPageUptodate(page);
53                 UnlockPage(page);
54         } 
55         PDEBUG(page, READ);
56         if ( rc == PAGE_SIZE ) 
57                 rc = 0;
58         return rc;
59
60 }
61
62 /*
63  * This does the "real" work of the write. The generic routine has
64  * allocated the page, locked it, done all the page alignment stuff
65  * calculations etc. Now we should just copy the data from user
66  * space and write it back to the real medium..
67  *
68  * If the writer ends up delaying the write, the writer needs to
69  * increment the page use counts until he is done with the page.
70  */
71 int obdfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
72 {
73         long status;
74
75         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
76         status = -EFAULT;
77         CDEBUG(D_INODE, "page offset %ld, bytes %ld, offset %ld, page addr %lx, writing: %s, beg of page %s\n", page->offset, bytes, offset, page_address(page), ((char *) page_address(page)) + offset, (char *)page_address(page));
78         if (bytes) {
79                 lock_kernel();
80                 status = obdfs_writepage(file, page);
81                 unlock_kernel();
82         }
83         if ( status != PAGE_SIZE ) 
84                 return status;
85         else
86                 return bytes;
87 }
88
89
90
91
92 /* returns the page unlocked, but with a reference */
93 int obdfs_writepage(struct file *file, struct page *page)
94 {
95         struct obdfs_sb_info *sbi;
96         struct super_block *sb = file->f_dentry->d_inode->i_sb;
97         int rc;
98
99         ENTRY;
100         PDEBUG(page,WRITE);
101         /* XXX flush stuff */
102         sbi = sb->u.generic_sbp;
103
104         rc = sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
105                       file->f_dentry->d_inode->i_ino, page);
106         SetPageUptodate(page);
107         PDEBUG(page,WRITE);
108         return rc;
109 }
110
111
112 /* 
113    page is returned unlocked, with the up to date flag set, 
114    and held, i.e. caller must do a page_put
115 */
116 struct page *obdfs_getpage(struct inode *inode, unsigned long offset)
117 {
118         unsigned long new_page;
119         struct page ** hash;
120         struct page * page; 
121         struct obdfs_sb_info *sbi;
122         struct super_block *sb = inode->i_sb;
123
124         ENTRY;
125
126         sbi = sb->u.generic_sbp;
127         
128         page = find_get_page(inode, offset); 
129         if (page && Page_Uptodate(page)) { 
130                 PDEBUG(page,READ);
131                 return page;
132         } 
133                 
134         if (page && !Page_Uptodate(page) ) {
135                 CDEBUG(D_INODE, "Page found but not up to date\n");
136         }
137
138         /* page_cache_alloc returns address of page */
139         new_page = page_cache_alloc();
140         if (!new_page)
141                 return NULL;
142         
143         /* corresponding struct page in the mmap */
144         hash = page_hash(inode, offset);
145         page = page_cache_entry(new_page);
146         if (!add_to_page_cache_unique(page, inode, offset, hash)) {
147                 CDEBUG(D_INODE, "Page not found. Reading it.\n");
148                 PDEBUG(page,READ);
149                 sbi->osi_ops->o_brw(READ, sbi->osi_conn_info.conn_id, 
150                                     inode->i_ino, page);
151                 UnlockPage(page);
152                 SetPageUptodate(page);
153                 return page;
154         }
155         /*
156          * We arrive here in the unlikely event that someone 
157          * raced with us and added our page to the cache first.
158          */
159         CDEBUG(D_INODE, "Page not found. Someone raced us.\n");
160         PDEBUG(page,READ);
161         return page;
162 }
163
164
165
166 struct file_operations obdfs_file_operations = {
167         NULL,                   /* lseek - default */
168         generic_file_read,      /* read */
169         obdfs_file_write,     /* write - bad */
170         obdfs_readdir,          /* readdir */
171         NULL,                   /* poll - default */
172         NULL,                   /* ioctl */
173         NULL,                   /* mmap */
174         NULL,                   /* no special open code */
175         NULL,                   /* flush */
176         NULL,                   /* no special release code */
177         NULL,                   /* fsync */
178         NULL,                   /* fasync */
179         NULL,                   /* check_media_change */
180         NULL                    /* revalidate */
181 };
182
183 struct inode_operations obdfs_inode_ops = {
184         &obdfs_file_operations, /* default directory file-ops */
185         NULL,           /* create */
186         obdfs_lookup,   /* lookup */
187         NULL,           /* link */
188         NULL,           /* unlink */
189         NULL,           /* symlink */
190         NULL,           /* mkdir */
191         NULL,           /* rmdir */
192         NULL,           /* mknod */
193         NULL,           /* rename */
194         NULL,           /* readlink */
195         NULL,           /* follow_link */
196         NULL,           /* get_block */
197         obdfs_readpage, /* readpage */
198         obdfs_writepage, /* writepage */
199         NULL,           /* flushpage */
200         NULL,           /* truncate */
201         NULL,           /* permission */
202         NULL,           /* smap */
203         NULL            /* revalidate */
204 };