Whamcloud - gitweb
- obd filter works over ext2, but bonnie++ is buggie
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
7  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
8  * Copryright (C) 1999 Stelias Computing Inc, 
9  *                (author Peter J. Braam <braam@stelias.com>)
10  * Copryright (C) 1999 Seagate Technology Inc.
11 */
12
13
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/string.h>
18 #include <linux/stat.h>
19 #include <linux/errno.h>
20 #include <linux/locks.h>
21 #include <linux/unistd.h>
22
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
25
26 #include <linux/fs.h>
27 #include <linux/stat.h>
28 #include <asm/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <asm/segment.h>
31 #include <linux/mm.h>
32 #include <linux/pagemap.h>
33 #include <linux/smp_lock.h>
34
35 #include <linux/obd_support.h>
36 #include <linux/obd_ext2.h>
37 #include <linux/obdfs.h>
38
39 void obdfs_change_inode(struct inode *inode);
40
41 static int cache_writes = 0;
42
43
44 /* page cache support stuff */ 
45
46
47 /*
48  * Add a page to the dirty page list.
49  */
50 void set_page_dirty(struct page *page)
51 {
52         if (!test_and_set_bit(PG_dirty, &page->flags)) {
53                 struct address_space *mapping = page->mapping;
54
55                 if (mapping) {
56                         spin_lock(&pagecache_lock);
57                         list_del(&page->list);
58                         list_add(&page->list, &mapping->dirty_pages);
59                         spin_unlock(&pagecache_lock);
60
61                         if (mapping->host)
62                                 mark_inode_dirty_pages(mapping->host);
63                 }
64         }
65 }
66
67 /*
68  * Remove page from dirty list
69  */
70 void __set_page_clean(struct page *page)
71 {
72         struct address_space *mapping = page->mapping;
73         struct inode *inode;
74         
75         if (!mapping)
76                 return;
77
78         spin_lock(&pagecache_lock);
79         list_del(&page->list);
80         list_add(&page->list, &mapping->clean_pages);
81
82         inode = mapping->host;
83         if (list_empty(&mapping->dirty_pages)) { 
84                 CDEBUG(D_INODE, "inode clean\n");
85                 inode->i_state &= ~I_DIRTY_PAGES;
86         }
87         spin_unlock(&pagecache_lock);
88         EXIT;
89 }
90
91 inline void set_page_clean(struct page *page)
92 {
93         if (PageDirty(page)) { 
94                 ClearPageDirty(page);
95                 __set_page_clean(page);
96         }
97 }
98
99 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
100 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
101 {
102         obd_count        num_obdo = 1;
103         obd_count        bufs_per_obdo = 1;
104         struct obdo     *oa;
105         obd_size         count = PAGE_SIZE;
106         obd_off          offset = ((obd_off)page->index) << PAGE_SHIFT;
107         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
108         int              err;
109
110         ENTRY;
111         if (IOPS(inode, brw) == NULL) {
112                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
113                 EXIT;
114                 return -EIO;
115         }
116
117         oa = obdo_alloc();
118         if ( !oa ) {
119                 EXIT;
120                 return -ENOMEM;
121         }
122         oa->o_valid = OBD_MD_FLNOTOBD;
123         obdfs_from_inode(oa, inode);
124
125         err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
126                                &page, &count, &offset, &flags);
127         //if ( !err )
128         //      obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
129
130         obdo_free(oa);
131         EXIT;
132         return err;
133 } /* obdfs_brw */
134
135 extern void set_page_clean(struct page *);
136
137 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
138 static int obdfs_commit_page(struct page *page, int create, int from, int to)
139 {
140         struct inode *inode = page->mapping->host;
141         obd_count        num_obdo = 1;
142         obd_count        bufs_per_obdo = 1;
143         struct obdo     *oa;
144         obd_size         count = to;
145         obd_off          offset = (((obd_off)page->index) << PAGE_SHIFT);
146         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
147         int              err;
148
149         ENTRY;
150         if (IOPS(inode, brw) == NULL) {
151                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
152                 EXIT;
153                 return -EIO;
154         }
155
156         oa = obdo_alloc();
157         if ( !oa ) {
158                 EXIT;
159                 return -ENOMEM;
160         }
161         oa->o_valid = OBD_MD_FLNOTOBD;
162         obdfs_from_inode(oa, inode);
163
164         CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n", 
165                from, to, count);
166
167         err = IOPS(inode, brw)(WRITE, IID(inode), num_obdo, &oa, &bufs_per_obdo,
168                                &page, &count, &offset, &flags);
169         if ( !err ) {
170                 SetPageUptodate(page);
171                 set_page_clean(page);
172         }
173
174         //if ( !err )
175         //      obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
176
177         obdo_free(oa);
178         EXIT;
179         return err;
180 } /* obdfs_brw */
181
182
183 /* returns the page unlocked, but with a reference */
184 int obdfs_readpage(struct file *file, struct page *page)
185 {
186         struct inode *inode = page->mapping->host;
187         int rc;
188
189         ENTRY;
190
191         if ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT) 
192              <= page->index) {
193                 memset(kmap(page), 0, PAGE_CACHE_SIZE);
194                 kunmap(page);
195                 goto readpage_out;
196         }
197
198         if (Page_Uptodate(page)) {
199                 EXIT;
200                 goto readpage_out;
201         }
202
203         rc = obdfs_brw(READ, inode, page, 0);
204         if ( rc ) {
205                 EXIT; 
206                 return rc;
207         } 
208         /* PDEBUG(page, "READ"); */
209
210  readpage_out:
211         SetPageUptodate(page);
212         obd_unlock_page(page);
213         EXIT;
214         return 0;
215 } /* obdfs_readpage */
216
217 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
218 {
219         struct inode *inode = page->mapping->host;
220         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
221         int rc = 0;
222         ENTRY; 
223         
224         kmap(page);
225         if (Page_Uptodate(page)) { 
226                 EXIT;
227                 goto prepare_done;
228         }
229
230         if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
231                 EXIT;
232                 return 0;
233         }
234         
235         rc = obdfs_brw(READ, inode, page, 0);
236         if ( !rc ) {
237                 SetPageUptodate(page);
238         } 
239
240  prepare_done:
241         set_page_dirty(page);
242         //SetPageDirty(page);
243         EXIT;
244         return rc;
245 }
246
247
248
249
250
251
252 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
253
254 int obdfs_init_pgrqcache(void)
255 {
256         ENTRY;
257         if (obdfs_pgrq_cachep == NULL) {
258                 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
259                 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
260                                                       sizeof(struct obdfs_pgrq),
261                                                       0, SLAB_HWCACHE_ALIGN,
262                                                       NULL, NULL);
263                 if (obdfs_pgrq_cachep == NULL) {
264                         EXIT;
265                         return -ENOMEM;
266                 } else {
267                         CDEBUG(D_CACHE, "allocated cache at %p\n",
268                                obdfs_pgrq_cachep);
269                 }
270         } else {
271                 CDEBUG(D_CACHE, "using existing cache at %p\n",
272                        obdfs_pgrq_cachep);
273         }
274         EXIT;
275         return 0;
276 } /* obdfs_init_wreqcache */
277
278 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
279 {
280         --obdfs_cache_count;
281         CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
282                pgrq->rq_page, obdfs_cache_count);
283         list_del(&pgrq->rq_plist);
284         OBDClearCachePage(pgrq->rq_page);
285         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
286 }
287
288 void obdfs_cleanup_pgrqcache(void)
289 {
290         ENTRY;
291         if (obdfs_pgrq_cachep != NULL) {
292                 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
293                        obdfs_pgrq_cachep, obdfs_cache_count);
294                 if (kmem_cache_destroy(obdfs_pgrq_cachep))
295                         printk(KERN_INFO __FUNCTION__
296                                ": unable to free all of cache\n");
297                 obdfs_pgrq_cachep = NULL;
298         } else
299                 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
300
301         EXIT;
302 } /* obdfs_cleanup_wreqcache */
303
304
305 /* called with the list lock held */
306 static struct page *obdfs_find_page_index(struct inode *inode,
307                                           unsigned long index)
308 {
309         struct list_head *page_list = obdfs_iplist(inode);
310         struct list_head *tmp;
311         struct page *page;
312
313         ENTRY;
314
315         CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
316                inode->i_ino, index);
317         OIDEBUG(inode);
318
319         if (list_empty(page_list)) {
320                 EXIT;
321                 return NULL;
322         }
323         tmp = page_list;
324         while ( (tmp = tmp->next) != page_list ) {
325                 struct obdfs_pgrq *pgrq;
326
327                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
328                 page = pgrq->rq_page;
329                 if (index == page->index) {
330                         CDEBUG(D_INFO,
331                                "INDEX SEARCH found page %p, index %ld\n",
332                                page, index);
333                         EXIT;
334                         return page;
335                 }
336         } 
337
338         EXIT;
339         return NULL;
340 } /* obdfs_find_page_index */
341
342
343 /* call and free pages from Linux page cache: called with io lock on inodes */
344 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
345                     obd_count num_obdos, struct obdo **obdos,
346                     obd_count *oa_bufs, struct page **pages, char **bufs,
347                     obd_size *counts, obd_off *offsets, obd_flag *flags)
348 {
349         int err;
350
351         ENTRY;
352         if (IOPS(inodes[0], brw) == NULL) {
353                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
354                 EXIT;
355                 return -EIO;
356         }
357
358         CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
359                num_io, num_obdos);
360         if (obd_debug_level & D_INFO) { /* DEBUGGING */
361                 int i;
362                 printk("OBDOS: ");
363                 for (i = 0; i < num_obdos; i++)
364                         printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
365
366                 printk("\nPAGES: ");
367                 for (i = 0; i < num_io; i++)
368                         printk("0x%p ", pages[i]);
369                 printk("\n");
370         }
371
372         err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
373                                   oa_bufs, pages, counts, offsets, flags);
374
375         CDEBUG(D_INFO, "BRW done\n");
376         /* release the pages from the page cache */
377         while ( num_io > 0 ) {
378                 --num_io;
379                 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
380                        pages[num_io], pages[num_io]->index);
381                 /* PDEBUG(pages[num_io], "do_vec_wr"); */
382                 put_page(pages[num_io]);
383                 /* PDEBUG(pages[num_io], "do_vec_wr"); */
384         }
385         CDEBUG(D_INFO, "put_page done\n");
386
387         while ( num_obdos > 0) {
388                 --num_obdos;
389                 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
390                 /* copy o_blocks to i_blocks */
391                 obdfs_set_size (inodes[num_obdos], obdos[num_obdos]->o_size);
392                 //obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
393                 obdo_free(obdos[num_obdos]);
394         }
395         CDEBUG(D_INFO, "obdo_free done\n");
396         EXIT;
397         return err;
398 }
399
400
401 /*
402  * Add a page to the write request cache list for later writing.
403  * ASYNCHRONOUS write method.
404  */
405 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
406 {
407         int err = 0;
408         ENTRY;
409
410         /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
411          * is written, so at worst we will write the page out twice.
412          *
413          * If the page has the PG_obdcache bit set, then the inode MUST be
414          * on the superblock dirty list so we don't need to check this.
415          * Dirty inodes are removed from the superblock list ONLY when they
416          * don't have any more cached pages.  It is possible to have an inode
417          * with no dirty pages on the superblock list, but not possible to
418          * have an inode with dirty pages NOT on the superblock dirty list.
419          */
420         if (!OBDAddCachePage(page)) {
421                 struct obdfs_pgrq *pgrq;
422                 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
423                 if (!pgrq) {
424                         OBDClearCachePage(page);
425                         EXIT;
426                         return -ENOMEM;
427                 }
428                 /* not really necessary since we set all pgrq fields here
429                 memset(pgrq, 0, sizeof(*pgrq)); 
430                 */
431                 
432                 pgrq->rq_page = page;
433                 pgrq->rq_jiffies = jiffies;
434                 get_page(pgrq->rq_page);
435
436                 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
437                 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
438                 obdfs_cache_count++;
439                 //printk("-- count %d\n", obdfs_cache_count);
440
441                 /* If inode isn't already on superblock inodes list, add it.
442                  *
443                  * We increment the reference count on the inode to keep it
444                  * from being freed from memory.  This _should_ be an iget()
445                  * with an iput() in both flush_reqs() and put_inode(), but
446                  * since put_inode() is called from iput() we can't call iput()
447                  * again there.  Instead we just increment/decrement i_count,
448                  * which is mostly what iget/iput do for an inode in memory.
449                  */
450                 if ( list_empty(obdfs_islist(inode)) ) {
451                         atomic_inc(&inode->i_count);
452                         CDEBUG(D_INFO,
453                                "adding inode %ld to superblock list %p\n",
454                                inode->i_ino, obdfs_slist(inode));
455                         list_add(obdfs_islist(inode), obdfs_slist(inode));
456                 }
457                 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
458
459         }
460
461         /* XXX For testing purposes, we can write out the page here.
462         err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
463          */
464
465         EXIT;
466         return err;
467 } /* obdfs_add_page_to_cache */
468
469 void rebalance(void)
470 {
471         if (obdfs_cache_count > 60000) {
472                 printk("-- count %ld\n", obdfs_cache_count);
473                 //obdfs_flush_dirty_pages(~0UL);
474                 printk("-- count %ld\n", obdfs_cache_count);
475         }
476 }
477
478 /* select between SYNC and ASYNC I/O methods */
479 int obdfs_do_writepage(struct page *page, int sync)
480 {
481         struct inode *inode = page->mapping->host;
482         int err;
483
484         ENTRY;
485         /* PDEBUG(page, "WRITEPAGE"); */
486         if ( sync )
487                 err = obdfs_brw(WRITE, inode, page, 1);
488         else {
489                 err = obdfs_add_page_to_cache(inode, page);
490                 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
491                        inode->i_ino, page, err, Page_Uptodate(page));
492         }
493                 
494         if ( !err ) {
495                 SetPageUptodate(page);
496                 set_page_clean(page);
497         }
498         /* PDEBUG(page,"WRITEPAGE"); */
499         EXIT;
500         return err;
501 } /* obdfs_do_writepage */
502
503
504
505 /* returns the page unlocked, but with a reference */
506 int obdfs_writepage(struct page *page)
507 {
508         int rc;
509         struct inode *inode = page->mapping->host;
510         ENTRY;
511         printk("---> writepage called ino %ld!\n", inode->i_ino);
512         BUG();
513         rc = obdfs_do_writepage(page, 1);
514         if ( !rc ) {
515                 set_page_clean(page);
516         } else {
517                 CDEBUG(D_INODE, "--> GRR %d\n", rc);
518         }
519         EXIT;
520         return rc;
521 }
522
523 void write_inode_pages(struct inode *inode)
524 {
525         struct list_head *tmp = &inode->i_mapping->dirty_pages;
526         
527         while ( (tmp = tmp->next) != &inode->i_mapping->dirty_pages) { 
528                 struct page *page;
529                 page = list_entry(tmp, struct page, list);
530                 obdfs_writepage(page);
531         }
532 }
533
534
535 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
536 {
537         struct inode *inode = page->mapping->host;
538         int rc = 0;
539         loff_t len = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
540         ENTRY;
541         CDEBUG(D_INODE, "commit write ino %ld (end at %Ld) from %d to %d ,ind %ld\n",
542                inode->i_ino, len, from, to, page->index);
543
544
545         if (cache_writes == 0) { 
546                 rc = obdfs_commit_page(page, 1, from, to);
547         }
548
549         if (len > inode->i_size) {
550                 obdfs_set_size(inode, len);
551         }
552
553         kunmap(page);
554         EXIT;
555         return rc;
556 }
557
558
559 /*
560  * This does the "real" work of the write. The generic routine has
561  * allocated the page, locked it, done all the page alignment stuff
562  * calculations etc. Now we should just copy the data from user
563  * space and write it back to the real medium..
564  *
565  * If the writer ends up delaying the write, the writer needs to
566  * increment the page use counts until he is done with the page.
567  *
568  * Return value is the number of bytes written.
569  */
570 int obdfs_write_one_page(struct file *file, struct page *page,
571                          unsigned long offset, unsigned long bytes,
572                          const char * buf)
573 {
574         struct inode *inode = file->f_dentry->d_inode;
575         int err;
576
577         ENTRY;
578         /* We check for complete page writes here, as we then don't have to
579          * get the page before writing over everything anyways.
580          */
581         if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
582                 err = obdfs_brw(READ, inode, page, 0);
583                 if ( err )
584                         return err;
585                 SetPageUptodate(page);
586         }
587
588         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
589                 return -EFAULT;
590
591         lock_kernel();
592         err = obdfs_writepage(page);
593         unlock_kernel();
594
595         return (err < 0 ? err : bytes);
596 } /* obdfs_write_one_page */
597
598 /* 
599  * return an up to date page:
600  *  - if locked is true then is returned locked
601  *  - if create is true the corresponding disk blocks are created 
602  *  - page is held, i.e. caller must release the page
603  *
604  * modeled on NFS code.
605  */
606 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
607                            int create, int locked)
608 {
609         struct page * page;
610         int index;
611         int err;
612
613         ENTRY;
614
615         offset = offset & PAGE_CACHE_MASK;
616         CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
617                inode->i_ino, offset, create, locked);
618         index = offset >> PAGE_CACHE_SHIFT;
619
620         page = grab_cache_page(&inode->i_data, index);
621
622         /* Yuck, no page */
623         if (! page) {
624             printk(KERN_WARNING " grab_cache_page says no dice ...\n");
625             EXIT;
626             return NULL;
627         }
628
629         /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
630         /* now check if the data in the page is up to date */
631         if ( Page_Uptodate(page)) { 
632                 if (!locked) {
633                         if (PageLocked(page))
634                                 obd_unlock_page(page);
635                 } else {
636                         printk("file %s, line %d: expecting locked page\n",
637                                __FILE__, __LINE__); 
638                 }
639                 EXIT;
640                 return page;
641         } 
642
643
644 #ifdef EXT2_OBD_DEBUG
645         if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
646                 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
647                        page, page->index);
648         }
649 #endif
650
651         err = obdfs_brw(READ, inode, page, create);
652
653         if ( err ) {
654                 SetPageError(page);
655                 obd_unlock_page(page);
656                 EXIT;
657                 return page;
658         }
659
660         if ( !locked )
661                 obd_unlock_page(page);
662         SetPageUptodate(page);
663         /* PDEBUG(page,"GETPAGE - after reading"); */
664         EXIT;
665         return page;
666 } /* obdfs_getpage */
667
668
669 void obdfs_truncate(struct inode *inode)
670 {
671         struct obdo *oa;
672         int err;
673         ENTRY;
674
675         //obdfs_dequeue_pages(inode);
676
677         if (IOPS(inode, punch) == NULL) {
678                 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
679                 EXIT;
680                 return;
681         }
682
683         oa = obdo_alloc();
684         if ( !oa ) {
685                 /* XXX This would give an inconsistent FS, so deal with it as
686                  * best we can for now - an obdo on the stack is not pretty.
687                  */
688                 struct obdo obdo;
689
690                 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
691
692                 obdo.o_valid = OBD_MD_FLNOTOBD;
693                 obdfs_from_inode(&obdo, inode);
694
695                 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
696         } else {
697                 oa->o_valid = OBD_MD_FLNOTOBD;
698                 obdfs_from_inode(oa, inode);
699
700                 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
701                        (long)oa->o_id, oa->o_size);
702                 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
703
704                 obdo_free(oa);
705         }
706
707         if (err) {
708                 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
709                 EXIT;
710                 return;
711         }
712         EXIT;
713 } /* obdfs_truncate */
714
715 struct address_space_operations obdfs_aops = {
716         readpage: obdfs_readpage,
717         writepage: obdfs_writepage,
718         sync_page: block_sync_page,
719         prepare_write: obdfs_prepare_write, 
720         commit_write: obdfs_commit_write,
721         bmap: NULL
722 };