Whamcloud - gitweb
Several bugfixes. Most notably ext2obd_brw is still totally broken,
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
5  * Copryright (C) 1999 Stelias Computing Inc, 
6  *                (author Peter J. Braam <braam@stelias.com>)
7  * Copryright (C) 1999 Seagate Technology Inc.
8  */
9
10
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/locks.h>
18 #include <linux/unistd.h>
19
20 #include <asm/system.h>
21 #include <asm/uaccess.h>
22
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <asm/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <asm/segment.h>
28 #include <linux/mm.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp_lock.h>
31
32 #include <linux/obd_support.h>
33 #include <linux/obd_ext2.h>
34 #include <linux/obdfs.h>
35
36
37 int obdfs_flush_reqs(struct list_head *page_list, 
38                      int flush_inode, int check_time);
39
40 void obdfs_flush_dirty_pages(int check_time);
41
42 /* SYNCHRONOUS I/O for an inode */
43 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
44 {
45         obd_count        num_oa = 1;
46         obd_count        oa_bufs = 1;
47         struct obdo     *oa;
48         char            *buf = (char *)page_address(page);
49         obd_size         count = PAGE_SIZE;
50         obd_off          offset = ((obd_off)page->index) << PAGE_SHIFT;
51         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
52         int              err;
53
54         ENTRY;
55         oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
56         if ( IS_ERR(oa) ) {
57                 EXIT;
58                 return PTR_ERR(oa);
59         }
60         obdfs_from_inode(oa, inode);
61
62         err = IOPS(inode, brw)(rw, IID(inode), num_oa, &oa, &oa_bufs, &buf,
63                                &count, &offset, &flags);
64
65         if ( !err )
66                 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
67
68         obdo_free(oa);
69         
70         EXIT;
71         return err;
72 } /* obdfs_brw */
73
74 /* returns the page unlocked, but with a reference */
75 int obdfs_readpage(struct dentry *dentry, struct page *page)
76 {
77         struct inode *inode = dentry->d_inode;
78         int rc;
79
80         ENTRY;
81         PDEBUG(page, "READ");
82         rc = obdfs_brw(READ, inode, page, 0);
83         if ( !rc ) {
84                 SetPageUptodate(page);
85                 UnlockPage(page);
86         } 
87         PDEBUG(page, "READ");
88         EXIT;
89         return rc;
90 } /* obdfs_readpage */
91
92 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
93 static int obdfs_cache_count = 0;
94
95 int obdfs_init_pgrqcache(void)
96 {
97         ENTRY;
98         if (obdfs_pgrq_cachep == NULL) {
99                 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
100                 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
101                                                       sizeof(struct obdfs_pgrq),
102                                                       0, SLAB_HWCACHE_ALIGN,
103                                                       NULL, NULL);
104                 if (obdfs_pgrq_cachep == NULL) {
105                         EXIT;
106                         return -ENOMEM;
107                 } else {
108                         CDEBUG(D_INODE, "allocated cache at %p\n",
109                                obdfs_pgrq_cachep);
110                 }
111         } else {
112                 CDEBUG(D_INODE, "using existing cache at %p\n",
113                        obdfs_pgrq_cachep);
114         }
115         EXIT;
116         return 0;
117 } /* obdfs_init_wreqcache */
118
119 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
120 {
121         obdfs_cache_count--;
122         CDEBUG(D_INODE, "deleting page %p from list [count %d]\n", pgrq->rq_page, obdfs_cache_count);
123         list_del(&pgrq->rq_plist);
124         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
125 }
126
127 void obdfs_cleanup_pgrqcache(void)
128 {
129         ENTRY;
130         if (obdfs_pgrq_cachep != NULL) {
131                 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p, count %d\n",
132                        obdfs_pgrq_cachep, obdfs_cache_count);
133                 if (kmem_cache_destroy(obdfs_pgrq_cachep))
134                         printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
135         } else
136                 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
137
138         EXIT;
139 } /* obdfs_cleanup_wreqcache */
140
141
142 /*
143  * Find a specific page in the page cache.  If it is found, we return
144  * the write request struct associated with it, if not found return NULL.
145  */
146 static struct obdfs_pgrq *
147 obdfs_find_in_page_list(struct inode *inode, struct page *page)
148 {
149         struct list_head *page_list = obdfs_iplist(inode);
150         struct list_head *tmp;
151
152         ENTRY;
153
154         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
155         OIDEBUG(inode);
156
157         if (list_empty(page_list)) {
158                 EXIT;
159                 return NULL;
160         }
161         tmp = page_list;
162         while ( (tmp = tmp->next) != page_list ) {
163                 struct obdfs_pgrq *pgrq;
164
165                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
166                 if (pgrq->rq_page == page) {
167                         CDEBUG(D_INODE, "found page %p in list\n", page);
168                         EXIT;
169                         return pgrq;
170                 }
171         } 
172
173         EXIT;
174         return NULL;
175 } /* obdfs_find_in_page_list */
176
177
178 static struct page*
179 obdfs_find_page_index(struct inode *inode, unsigned long index)
180 {
181         struct list_head *page_list = obdfs_iplist(inode);
182         struct list_head *tmp;
183         struct page *page;
184
185         ENTRY;
186
187         CDEBUG(D_INODE, "looking for inode %ld pageindex %ld\n", inode->i_ino, index);
188         OIDEBUG(inode);
189
190         if (list_empty(page_list)) {
191                 EXIT;
192                 return NULL;
193         }
194         tmp = page_list;
195         while ( (tmp = tmp->next) != page_list ) {
196                 struct obdfs_pgrq *pgrq;
197
198                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
199                 page = pgrq->rq_page;
200                 if (index == page->index) {
201                         CDEBUG(D_INODE, "INDEX SEARC found page %p in list, index %ld\n", page, index);
202                         EXIT;
203                         return page;
204                 }
205         } 
206
207         EXIT;
208         return NULL;
209 } /* obdfs_find_in_page_list */
210
211
212 /* call and free pages from Linux page cache */
213 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
214                     obd_count num_obdos, struct obdo **obdos,
215                     obd_count *oa_bufs, struct page **pages, char **bufs,
216                     obd_size *counts, obd_off *offsets, obd_flag *flags)
217 {
218         struct super_block *sb = inodes[0]->i_sb;
219         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
220         int err;
221
222         ENTRY;
223         CDEBUG(D_INODE, "writing %d page(s), %d obdo(s) in vector\n",
224                num_io, num_obdos);
225         err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
226                                 bufs, counts, offsets, flags);
227
228         /* release the pages from the page cache */
229         while ( num_io > 0 ) {
230                 num_io--;
231                 CDEBUG(D_INODE, "calling put_page for %p, index %ld\n", pages[num_io], pages[num_io]->index);
232                 /*
233                 UnlockPage(pages[num_io]);
234                 */
235                 put_page(pages[num_io]);
236         }
237
238         while ( num_obdos > 0) {
239                 num_obdos--;
240                 CDEBUG(D_INODE, "copy/free obdo %ld\n",
241                        (long)obdos[num_obdos]->o_id);
242                 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
243                 obdo_free(obdos[num_obdos]);
244         }
245         EXIT;
246         return err;
247 }
248
249
250 /*
251  * Add a page to the write request cache list for later writing
252  * ASYNCHRONOUS write method.
253  */
254 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
255 {
256         int res = 0;
257
258         ENTRY;
259
260         /* If this page isn't already in the inode page list, add it */
261         if ( !obdfs_find_in_page_list(inode, page) ) {
262                 struct obdfs_pgrq *pgrq;
263                 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
264                 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p, cache count [%d]\n",
265                        inode->i_ino, page, pgrq, obdfs_cache_count);
266                 if (!pgrq) {
267                         EXIT;
268                         return -ENOMEM;
269                 }
270                 memset(pgrq, 0, sizeof(*pgrq)); 
271                 
272                 pgrq->rq_page = page;
273                 get_page(pgrq->rq_page);
274                 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
275         }
276
277         /* If inode isn't already on the superblock inodes list, add it */
278         if ( list_empty(obdfs_islist(inode)) ) {
279                 CDEBUG(D_INODE, "adding inode %ld to superblock list %p\n",
280                        inode->i_ino, obdfs_slist(inode));
281                 list_add(obdfs_islist(inode), obdfs_slist(inode));
282         }
283
284
285         /* XXX For testing purposes, we write out the page here.
286          *     In the future, a flush daemon will write out the page.
287         res = obdfs_flush_reqs(obdfs_slist(inode), 0);
288         obdfs_flush_dirty_pages(1);
289          */
290
291         EXIT;
292         return res;
293         /*return 0;*/
294 } /* obdfs_add_page_to_cache */
295
296
297 /* select between SYNC and ASYNC I/O methods */
298 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
299 {
300         int err;
301
302         ENTRY;
303         /* PDEBUG(page, "WRITEPAGE"); */
304         if ( sync )
305                 err = obdfs_brw(WRITE, inode, page, 1);
306         else {
307                 err = obdfs_add_page_to_cache(inode, page);
308                 CDEBUG(D_IOCTL, "DO_WR ino: %ld, page %p, err %d, uptodata %d\n", inode->i_ino, page, err, Page_Uptodate(page));
309         }
310                 
311         if ( !err )
312                 SetPageUptodate(page);
313         /* PDEBUG(page,"WRITEPAGE"); */
314         EXIT;
315         return err;
316 } /* obdfs_do_writepage */
317
318 /* returns the page unlocked, but with a reference */
319 int obdfs_writepage(struct dentry *dentry, struct page *page)
320 {
321         return obdfs_do_writepage(dentry->d_inode, page, 0);
322 }
323
324 /*
325  * This does the "real" work of the write. The generic routine has
326  * allocated the page, locked it, done all the page alignment stuff
327  * calculations etc. Now we should just copy the data from user
328  * space and write it back to the real medium..
329  *
330  * If the writer ends up delaying the write, the writer needs to
331  * increment the page use counts until he is done with the page.
332  *
333  * Return value is the number of bytes written.
334  */
335 int obdfs_write_one_page(struct file *file, struct page *page,
336                           unsigned long offset, unsigned long bytes,
337                           const char * buf)
338 {
339         struct inode *inode = file->f_dentry->d_inode;
340         int err;
341
342         ENTRY;
343         if ( !Page_Uptodate(page) ) {
344                 err = obdfs_brw(READ, inode, page, 1);
345                 if ( !err )
346                         SetPageUptodate(page);
347                 else
348                         return err;
349         }
350
351         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
352                 return -EFAULT;
353
354         lock_kernel();
355         err = obdfs_writepage(file->f_dentry, page);
356         unlock_kernel();
357
358         return (err < 0 ? err : bytes);
359 } /* obdfs_write_one_page */
360
361 /* 
362    return an up to date page:
363     - if locked is true then is returned locked
364     - if create is true the corresponding disk blocks are created 
365     - page is held, i.e. caller must release the page
366
367    modeled on NFS code.
368 */
369 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
370 {
371         struct page *page_cache;
372         struct page ** hash;
373         struct page * page;
374         int err;
375
376         ENTRY;
377
378         offset = offset & PAGE_CACHE_MASK;
379         CDEBUG(D_INODE, "\n");
380         
381         page = NULL;
382         page_cache = page_cache_alloc();
383         if ( ! page_cache ) {
384                 EXIT;
385                 return NULL;
386         }
387         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
388
389         hash = page_hash(&inode->i_data, offset);
390         page = grab_cache_page(&inode->i_data, offset);
391
392         /* Yuck, no page */
393         if (! page) {
394             printk("grab_cache_page says no dice ...\n");
395             EXIT;
396             return 0;
397         }
398
399         PDEBUG(page, "GETPAGE: got page - before reading\n");
400         /* now check if the data in the page is up to date */
401         if ( Page_Uptodate(page)) { 
402                 if (!locked)
403                         UnlockPage(page);
404                 EXIT;
405                 return page;
406         } 
407
408
409         if ( obdfs_find_page_index(inode, offset) ) {
410                 printk("OVERWRITE: found dirty page %p, index %ld\n", page, page->index);
411         }
412
413         err = obdfs_brw(READ, inode, page, create);
414
415         if ( err ) {
416                 SetPageError(page);
417                 UnlockPage(page);
418                 EXIT;
419                 return page;
420         }
421
422         if ( !locked )
423                 UnlockPage(page);
424         SetPageUptodate(page);
425         PDEBUG(page,"GETPAGE - after reading");
426         EXIT;
427         return page;
428 } /* obdfs_getpage */
429
430