Whamcloud - gitweb
Updated parameters for obdfs_writepage() to use struct *dentry instead of
[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 <linux/obd_support.h>
32 #include <linux/obd_sim.h>
33 #include <linux/obdfs.h>
34
35 int console_loglevel;
36
37 /* VFS super_block ops */
38
39 #if 0
40 int obdfs_brw(struct inode *dir, int rw, struct page *page, int create)
41 {
42         return iops(dir)->o_brw(rw, iid(dir), dir, page, create);
43 }
44 #endif
45
46 /* returns the page unlocked, but with a reference */
47 int obdfs_readpage(struct dentry *dentry, struct page *page)
48 {
49         struct inode *inode = dentry->d_inode;
50         int rc;
51
52         ENTRY;
53         /* XXX flush stuff */
54         PDEBUG(page, "READ");
55         rc =  iops(inode)->o_brw(READ, iid(inode),inode, page, 0);
56         if (rc == PAGE_SIZE ) {
57                 SetPageUptodate(page);
58                 UnlockPage(page);
59         } 
60         PDEBUG(page, "READ");
61         if ( rc == PAGE_SIZE ) 
62                 rc = 0;
63         return rc;
64
65 }
66
67 /* returns the page unlocked, but with a reference */
68 static int obdfs_writepage(struct dentry *dentry, struct page *page)
69 {
70         struct inode *inode = dentry->d_inode;
71         int rc;
72
73         ENTRY;
74         PDEBUG(page, "WRITEPAGE");
75         /* XXX flush stuff */
76
77         rc = iops(inode)->o_brw(WRITE, iid(inode), inode, page, 1);
78
79         SetPageUptodate(page);
80         PDEBUG(page,"WRITEPAGE");
81         return rc;
82 }
83
84 /*
85  * This does the "real" work of the write. The generic routine has
86  * allocated the page, locked it, done all the page alignment stuff
87  * calculations etc. Now we should just copy the data from user
88  * space and write it back to the real medium..
89  *
90  * If the writer ends up delaying the write, the writer needs to
91  * increment the page use counts until he is done with the page.
92  */
93 int obdfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
94 {
95         long status;
96         struct inode *inode = file->f_dentry->d_inode;
97
98         ENTRY;
99         if ( !Page_Uptodate(page) ) {
100                 status =  iops(inode)->o_brw(READ, iid(inode), inode, page, 1);
101                 if (status == PAGE_SIZE ) {
102                         SetPageUptodate(page);
103                 } else { 
104                         return status;
105                 }
106         }
107         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
108         status = -EFAULT;
109
110         if (bytes) {
111
112                 lock_kernel();
113                 status = obdfs_writepage(file->f_dentry, page);
114                 unlock_kernel();
115         }
116         EXIT;
117         if ( status != PAGE_SIZE ) 
118                 return status;
119         else
120                 return bytes;
121 }
122
123
124
125
126
127 void report_inode(struct page * page) {
128         struct inode *inode = (struct inode *)0;
129         int offset = (int)&inode->i_data;
130         inode = (struct inode *)( (char *)page->mapping - offset);
131         if ( inode->i_sb->s_magic == 0x4711 )
132                 printk("----> ino %ld , dev %d\n", inode->i_ino, inode->i_dev);
133 }
134
135 /* 
136    return an up to date page:
137     - if locked is true then is returned locked
138     - if create is true the corresponding disk blocks are created 
139     - page is held, i.e. caller must release the page
140
141    modeled on NFS code.
142 */
143 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
144 {
145         struct page *page_cache;
146         struct page ** hash;
147         struct page * page;
148         int rc;
149
150         ENTRY;
151
152         offset = offset & PAGE_CACHE_MASK;
153         CDEBUG(D_INODE, "\n");
154         
155         page = NULL;
156         page_cache = page_cache_alloc();
157         if ( ! page_cache ) 
158                 return NULL;
159         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
160
161         hash = page_hash(&inode->i_data, offset);
162  repeat:
163         CDEBUG(D_INODE, "Finding page\n");
164         IDEBUG(inode);
165
166         page = __find_lock_page(&inode->i_data, offset, hash); 
167         if ( page ) {
168                 CDEBUG(D_INODE, "Page found freeing\n");
169                 page_cache_free(page_cache);
170         } else {
171                 page = page_cache;
172                 if ( page->buffers ) {
173                         PDEBUG(page, "GETPAGE: buffers bug\n");
174                         UnlockPage(page);
175                         return NULL;
176                 }
177                 if (add_to_page_cache_unique(page, &inode->i_data, offset, hash)) {
178                         page_cache_release(page);
179                         CDEBUG(D_INODE, "Someone raced: try again\n");
180                         goto repeat;
181                 }
182         }
183
184         PDEBUG(page, "GETPAGE: got page - before reading\n");
185         /* now check if the data in the page is up to date */
186         if ( Page_Uptodate(page)) { 
187                 if (!locked)
188                         UnlockPage(page);
189                 EXIT;
190                 return page;
191         } 
192
193         /* it's not: read it */
194         if (! page) {
195             printk("get_page_map says no dice ...\n");
196             return 0;
197             }
198
199
200
201         rc = iops(inode)->o_brw(READ, iid(inode), inode, page, create);
202         if ( rc != PAGE_SIZE ) {
203                 SetPageError(page);
204                 UnlockPage(page);
205                 return page;
206         }
207
208         if ( !locked )
209                 UnlockPage(page);
210         SetPageUptodate(page);
211         PDEBUG(page,"GETPAGE - after reading");
212         EXIT;
213         return page;
214 }
215
216
217 struct file_operations obdfs_file_ops = {
218         NULL,                   /* lseek - default */
219         generic_file_read,      /* read */
220         obdfs_file_write,       /* write - bad */
221         obdfs_readdir,          /* readdir */
222         NULL,                   /* poll - default */
223         NULL,                   /* ioctl */
224         NULL,                   /* mmap */
225         NULL,                   /* no special open code */
226         NULL,                   /* flush */
227         NULL,                   /* no special release code */
228         NULL,                   /* fsync */
229         NULL,                   /* fasync */
230         NULL,                   /* check_media_change */
231         NULL                    /* revalidate */
232 };
233
234 struct inode_operations obdfs_inode_ops = {
235         &obdfs_file_ops,        /* default directory file-ops */
236         obdfs_create,   /* create */
237         obdfs_lookup,   /* lookup */
238         obdfs_link,     /* link */
239         obdfs_unlink,   /* unlink */
240         obdfs_symlink,  /* symlink */
241         obdfs_mkdir,    /* mkdir */
242         obdfs_rmdir,    /* rmdir */
243         obdfs_mknod,    /* mknod */
244         obdfs_rename,   /* rename */
245         NULL,           /* readlink */
246         NULL,           /* follow_link */
247         NULL,           /* get_block */
248         obdfs_readpage, /* readpage */
249         obdfs_writepage, /* writepage */
250         NULL,           /* flushpage */
251         NULL,           /* truncate */
252         NULL,           /* permission */
253         NULL,           /* smap */
254         NULL            /* revalidate */
255 };