Whamcloud - gitweb
aa18c729603fd7f1554ef162e59ded65a04985c7
[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_ext2.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, 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         struct obdo *oa;
51         int err;
52
53         ENTRY;
54         PDEBUG(page, "READ");
55         oa = obdo_alloc();
56         if (!oa) {
57                 printk("obdfs_readpage: obdo_alloc failure\n");
58                 EXIT;
59                 return -ENOMEM;
60         }
61
62         oa->o_id = inode->i_ino;
63         err = IOPS(inode, brw)(READ, IID(inode), oa, (char *)page_address(page),
64                                PAGE_SIZE, (page->index) << PAGE_SHIFT, 0);
65         obdo_to_inode(inode, oa); /* copy o_blocks to i_blocks */
66         obdo_free(oa);
67
68         if (err == PAGE_SIZE ) {
69                 SetPageUptodate(page);
70                 UnlockPage(page);
71         } 
72         PDEBUG(page, "READ");
73         if ( err == PAGE_SIZE ) 
74                 err = 0;
75         EXIT;
76         return err;
77 }
78
79 static kmem_cache_t *obdfs_wreq_cachep;
80
81 int obdfs_init_wreqcache(void)
82 {
83         /* XXX need to free this somewhere? */
84         ENTRY;
85         obdfs_wreq_cachep = kmem_cache_create("obdfs_wreq",
86                                               sizeof(struct obdfs_wreq),
87                                               0, SLAB_HWCACHE_ALIGN,
88                                               NULL, NULL);
89         if (obdfs_wreq_cachep == NULL) {
90                 EXIT;
91                 return -ENOMEM;
92         }
93         EXIT;
94         return 0;
95 }
96
97 /*
98  * Find a specific page in the page cache.  If it is found, we return
99  * the write request struct associated with it, if not found return NULL.
100  */
101 static struct obdfs_wreq *
102 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
103 {
104         struct list_head *list_head = &OBD_LIST(inode);
105         struct obdfs_wreq *head, *wreq;
106
107         ENTRY;
108         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
109         if (list_empty(list_head)) {
110                 CDEBUG(D_INODE, "empty list\n");
111                 EXIT;
112                 return NULL;
113         }
114         wreq = head = WREQ(list_head->next);
115         do {
116                 CDEBUG(D_INODE, "checking page %p\n", wreq->wb_page);
117                 if (wreq->wb_page == page) {
118                         CDEBUG(D_INODE, "found page %p in list\n", page);
119                         EXIT;
120                         return wreq;
121                 }
122         } while ((wreq = WB_NEXT(wreq)) != head);
123
124         EXIT;
125         return NULL;
126 }
127
128
129 /*
130  * Remove a writeback request from a list
131  */
132 static inline int
133 obdfs_remove_from_page_cache(struct obdfs_wreq *wreq)
134 {
135         struct inode *inode = wreq->wb_inode;
136         struct page *page = wreq->wb_page;
137         struct obdo *oa;
138         int err;
139
140         ENTRY;
141         CDEBUG(D_INODE, "removing inode %ld page %p, wreq: %p\n",
142                inode->i_ino, page, wreq);
143         oa = obdo_alloc();
144         if (!oa) {
145                 printk("obdfs_remove_from_page_cache: obdo_alloc failure\n");
146                 EXIT;
147                 return -ENOMEM;
148         }
149         oa->o_id = inode->i_ino;
150         err = IOPS(inode, brw)(WRITE, IID(inode), oa,(char *)page_address(page),
151                                PAGE_SIZE, (page->index) << PAGE_SHIFT, 1);
152         obdo_to_inode(inode, oa); /* copy o_blocks to i_blocks */
153         /* XXX probably should handle error here somehow.  I think that
154          *     ext2 also does the same thing - discard write even if error?
155          */
156         put_page(page);
157         list_del(&wreq->wb_list);
158         kmem_cache_free(obdfs_wreq_cachep, wreq);
159
160         EXIT;
161         return err;
162 }
163
164 /*
165  * Add a page to the write request cache list for later writing
166  */
167 static int
168 obdfs_add_to_page_cache(struct inode *inode, struct page *page)
169 {
170         struct obdfs_wreq *wreq;
171
172         ENTRY;
173         wreq = kmem_cache_alloc(obdfs_wreq_cachep, SLAB_KERNEL);
174         CDEBUG(D_INODE, "adding inode %ld page %p, wreq: %p\n",
175                inode->i_ino, page, wreq);
176         if (!wreq) {
177                 EXIT;
178                 return -ENOMEM;
179         }
180         memset(wreq, 0, sizeof(*wreq)); 
181
182         wreq->wb_page = page;
183         wreq->wb_inode = inode;
184
185         get_page(wreq->wb_page);
186         list_add(&wreq->wb_list, &OBD_LIST(inode));
187
188         /* For testing purposes, we write out the page here.
189          * In the future, a flush daemon will write out the page.
190          */
191         wreq = obdfs_find_in_page_cache(inode, page);
192         if (!wreq) {
193                 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
194                 return -EINVAL;
195         } else
196                 return obdfs_remove_from_page_cache(wreq);
197
198         return 0;
199 }
200
201
202 /* returns the page unlocked, but with a reference */
203 int obdfs_writepage(struct dentry *dentry, struct page *page)
204 {
205         struct inode *inode = dentry->d_inode;
206         int err;
207
208         ENTRY;
209         PDEBUG(page, "WRITEPAGE");
210         /* XXX flush stuff */
211         err = obdfs_add_to_page_cache(inode, page);
212
213         if (!err)
214                 SetPageUptodate(page);
215         PDEBUG(page,"WRITEPAGE");
216         return err;
217 }
218
219 /*
220  * This does the "real" work of the write. The generic routine has
221  * allocated the page, locked it, done all the page alignment stuff
222  * calculations etc. Now we should just copy the data from user
223  * space and write it back to the real medium..
224  *
225  * If the writer ends up delaying the write, the writer needs to
226  * increment the page use counts until he is done with the page.
227  */
228 int obdfs_write_one_page(struct file *file, struct page *page,
229                          unsigned long offset, unsigned long bytes,
230                          const char * buf)
231 {
232         long status;
233         struct inode *inode = file->f_dentry->d_inode;
234
235         ENTRY;
236         if ( !Page_Uptodate(page) ) {
237                 struct obdo *oa;
238                 oa = obdo_alloc();
239                 if (!oa) {
240                         printk("obdfs_write_one_page: obdo_alloc failure\n");
241                         EXIT;
242                         return -ENOMEM;
243                 }
244                 oa->o_id = inode->i_ino;
245                 status = IOPS(inode, brw)(READ, IID(inode), oa,
246                                           (char *)page_address(page), PAGE_SIZE,                                          (page->index) << PAGE_SHIFT, 1);
247                 obdo_to_inode(inode, oa); /* copy o_blocks to i_blocks */
248                 obdo_free(oa);
249
250                 if (status == PAGE_SIZE ) {
251                         SetPageUptodate(page);
252                 } else { 
253                         return status;
254                 }
255         }
256         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
257         status = -EFAULT;
258
259         if (bytes) {
260                 lock_kernel();
261                 status = obdfs_writepage(file->f_dentry, page);
262                 unlock_kernel();
263         }
264         EXIT;
265         if ( status != PAGE_SIZE ) 
266                 return status;
267         else
268                 return bytes;
269 }
270
271
272
273
274
275 void report_inode(struct page * page) {
276         struct inode *inode = (struct inode *)0;
277         int offset = (int)&inode->i_data;
278         inode = (struct inode *)( (char *)page->mapping - offset);
279         if ( inode->i_sb->s_magic == 0x4711 )
280                 printk("----> ino %ld , dev %d\n", inode->i_ino, inode->i_dev);
281 }
282
283 /* 
284    return an up to date page:
285     - if locked is true then is returned locked
286     - if create is true the corresponding disk blocks are created 
287     - page is held, i.e. caller must release the page
288
289    modeled on NFS code.
290 */
291 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
292                            int create, int locked)
293 {
294         struct page *page_cache;
295         struct page ** hash;
296         struct page * page;
297         struct obdo *oa;
298         int err;
299
300         ENTRY;
301
302         offset = offset & PAGE_CACHE_MASK;
303         CDEBUG(D_INODE, "\n");
304         
305         page = NULL;
306         page_cache = page_cache_alloc();
307         if ( ! page_cache ) 
308                 return NULL;
309         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
310
311         hash = page_hash(&inode->i_data, offset);
312         page = grab_cache_page(&inode->i_data, offset);
313
314         PDEBUG(page, "obdfs_getpage: got page - before reading\n");
315         /* now check if the data in the page is up to date */
316         if ( Page_Uptodate(page)) { 
317                 if (!locked)
318                         UnlockPage(page);
319                 EXIT;
320                 return page;
321         } 
322
323         /* it's not: read it */
324         if (! page) {
325                 printk("obdfs_getpage: says no dice ...\n");
326                 return NULL;
327         }
328
329         oa = obdo_alloc();
330         if (!oa) {
331                 printk("obdfs_getpage: obdo_alloc failure\n");
332                 EXIT;
333                 return NULL;
334         }
335         oa->o_id = inode->i_ino;
336         err = IOPS(inode, brw)(READ, IID(inode), oa, (char *)page_address(page),
337                                PAGE_SIZE, (page->index) << PAGE_SHIFT, create);
338
339         obdo_to_inode(inode, oa); /* XXX to copy o_blocks */
340         obdo_free(oa);
341
342         if ( err != PAGE_SIZE ) {
343                 SetPageError(page);
344                 UnlockPage(page);
345                 return page;
346         }
347
348         if ( !locked )
349                 UnlockPage(page);
350         SetPageUptodate(page);
351         PDEBUG(page,"GETPAGE - after reading");
352         EXIT;
353         return page;
354 }
355
356