Whamcloud - gitweb
obdfs/flushd.c: added constant for nr_free_buffer_pages() function call as
[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 /* SYNCHRONOUS I/O for an inode */
38 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
39 {
40         obd_count        num_obdo = 1;
41         obd_count        bufs_per_obdo = 1;
42         struct obdo     *oa;
43         char            *buf = (char *)page_address(page);
44         obd_size         count = PAGE_SIZE;
45         obd_off          offset = ((obd_off)page->index) << PAGE_SHIFT;
46         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
47         int              err;
48
49         ENTRY;
50         if (IOPS(inode, brw) == NULL) {
51                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
52                 EXIT;
53                 return -EIO;
54         }
55
56         oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
57         if ( IS_ERR(oa) ) {
58                 EXIT;
59                 return PTR_ERR(oa);
60         }
61         obdfs_from_inode(oa, inode);
62
63         err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
64                                &buf, &count, &offset, &flags);
65
66         if ( !err )
67                 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
68
69         obdo_free(oa);
70         
71         EXIT;
72         return err;
73 } /* obdfs_brw */
74
75 /* returns the page unlocked, but with a reference */
76 int obdfs_readpage(struct dentry *dentry, struct page *page)
77 {
78         struct inode *inode = dentry->d_inode;
79         int rc;
80
81         ENTRY;
82         /* PDEBUG(page, "READ"); */
83         rc = obdfs_brw(READ, inode, page, 0);
84         if ( !rc ) {
85                 SetPageUptodate(page);
86                 UnlockPage(page);
87         } 
88         /* PDEBUG(page, "READ"); */
89         EXIT;
90         return rc;
91 } /* obdfs_readpage */
92
93 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
94
95 int obdfs_init_pgrqcache(void)
96 {
97         ENTRY;
98         if (obdfs_pgrq_cachep == NULL) {
99                 CDEBUG(D_CACHE, "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_CACHE, "allocated cache at %p\n",
109                                obdfs_pgrq_cachep);
110                 }
111         } else {
112                 CDEBUG(D_CACHE, "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_INFO, "deleting page %p from list [count %ld]\n",
123                pgrq->rq_page, obdfs_cache_count);
124         OBDClearCachePage(pgrq->rq_page);
125         list_del(&pgrq->rq_plist);
126         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
127 }
128
129 void obdfs_cleanup_pgrqcache(void)
130 {
131         ENTRY;
132         if (obdfs_pgrq_cachep != NULL) {
133                 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
134                        obdfs_pgrq_cachep, obdfs_cache_count);
135                 if (kmem_cache_destroy(obdfs_pgrq_cachep))
136                         printk(KERN_INFO __FUNCTION__
137                                ": unable to free all of cache\n");
138                 obdfs_pgrq_cachep = NULL;
139         } else
140                 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
141
142         EXIT;
143 } /* obdfs_cleanup_wreqcache */
144
145
146 /*
147  * See whether a specific page in the page cache.
148  * Called with the list lock held.
149  */
150 #ifdef PG_obdcache
151 #define obdfs_find_in_page_list(inode, page) OBDAddCachePage(page)
152 #else
153 static int obdfs_find_in_page_list(struct inode *inode, struct page *page)
154 {
155         struct list_head *page_list = obdfs_iplist(inode);
156         struct list_head *tmp;
157
158         ENTRY;
159
160         CDEBUG(D_INFO, "looking for inode %ld page %p\n", inode->i_ino, page);
161         OIDEBUG(inode);
162
163         if (list_empty(page_list)) {
164                 CDEBUG(D_INFO, "empty list\n");
165                 EXIT;
166                 return 0;
167         }
168         tmp = page_list;
169         while ( (tmp = tmp->next) != page_list ) {
170                 struct obdfs_pgrq *pgrq;
171
172                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
173                 if (pgrq->rq_page == page) {
174                         CDEBUG(D_INFO, "found page %p in list\n", page);
175                         EXIT;
176                         return 1;
177                 }
178         } 
179
180         EXIT;
181         return 0;
182 } /* obdfs_find_in_page_list */
183 #endif
184
185
186 /* called with the list lock held */
187 static struct page *obdfs_find_page_index(struct inode *inode,
188                                           unsigned long index)
189 {
190         struct list_head *page_list = obdfs_iplist(inode);
191         struct list_head *tmp;
192         struct page *page;
193
194         ENTRY;
195
196         CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
197                inode->i_ino, index);
198         OIDEBUG(inode);
199
200         if (list_empty(page_list)) {
201                 EXIT;
202                 return NULL;
203         }
204         tmp = page_list;
205         while ( (tmp = tmp->next) != page_list ) {
206                 struct obdfs_pgrq *pgrq;
207
208                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
209                 page = pgrq->rq_page;
210                 if (index == page->index) {
211                         CDEBUG(D_INFO,
212                                "INDEX SEARCH found page %p, index %ld\n",
213                                page, index);
214                         EXIT;
215                         return page;
216                 }
217         } 
218
219         EXIT;
220         return NULL;
221 } /* obdfs_find_page_index */
222
223
224 /* call and free pages from Linux page cache: called with io lock on inodes */
225 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
226                     obd_count num_obdos, struct obdo **obdos,
227                     obd_count *oa_bufs, struct page **pages, char **bufs,
228                     obd_size *counts, obd_off *offsets, obd_flag *flags)
229 {
230         int err;
231
232         ENTRY;
233         if (IOPS(inodes[0], brw) == NULL) {
234                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
235                 EXIT;
236                 return -EIO;
237         }
238
239         CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
240                num_io, num_obdos);
241         { /* DEBUGGING */
242                 int i;
243                 printk("OBDOS: ");
244                 for (i = 0; i < num_obdos; i++)
245                         printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
246
247                 printk("\nPAGES: ");
248                 for (i = 0; i < num_io; i++)
249                         printk("0x%p ", pages[i]);
250                 printk("\n");
251         }
252
253         err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
254                                   oa_bufs, bufs, counts, offsets, flags);
255
256         CDEBUG(D_CACHE, "BRW done\n");
257         /* release the pages from the page cache */
258         while ( num_io > 0 ) {
259                 --num_io;
260                 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
261                        pages[num_io], pages[num_io]->index);
262                 /* PDEBUG(pages[num_io], "do_vec_wr"); */
263                 put_page(pages[num_io]);
264                 /* PDEBUG(pages[num_io], "do_vec_wr"); */
265         }
266         CDEBUG(D_CACHE, "put_page done\n");
267
268         while ( num_obdos > 0) {
269                 --num_obdos;
270                 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
271                 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
272                 obdo_free(obdos[num_obdos]);
273         }
274         CDEBUG(D_CACHE, "obdo_free done\n");
275         EXIT;
276         return err;
277 }
278
279
280 /*
281  * Add a page to the write request cache list for later writing
282  * ASYNCHRONOUS write method.
283  */
284 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
285 {
286         int res = 0;
287
288         ENTRY;
289
290         /* If this page isn't already in the inode page list, add it */
291         obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
292         if ( !obdfs_find_in_page_list(inode, page) ) {
293                 struct obdfs_pgrq *pgrq;
294                 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
295                 if (!pgrq) {
296                         EXIT;
297                         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
298                         return -ENOMEM;
299                 }
300                 memset(pgrq, 0, sizeof(*pgrq)); 
301                 
302                 pgrq->rq_page = page;
303                 pgrq->rq_jiffies = jiffies;
304                 get_page(pgrq->rq_page);
305                 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
306                 obdfs_cache_count++;
307                 CDEBUG(D_INFO,
308                        "added inode %ld page %p, pgrq: %p, cache count [%ld]\n",
309                        inode->i_ino, page, pgrq, obdfs_cache_count);
310         }
311
312         /* If inode isn't already on the superblock inodes list, add it,
313          * and increase ref count on inode so it doesn't disappear on us.
314          *
315          * We increment the reference count on the inode to keep it from
316          * being freed from memory.  This _should_ be an iget() with an
317          * iput() in both flush_reqs() and put_inode(), but since put_inode()
318          * is called from iput() we can't call iput() again there.  Instead
319          * we just increment/decrement i_count, which is essentially what
320          * iget/iput do for an inode already in memory.
321          */
322         if ( list_empty(obdfs_islist(inode)) ) {
323                 inode->i_count++;
324                 CDEBUG(D_INFO, "adding inode %ld to superblock list %p\n",
325                        inode->i_ino, obdfs_slist(inode));
326                 list_add(obdfs_islist(inode), obdfs_slist(inode));
327         }
328         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
329
330         /* XXX For testing purposes, we write out the page here.
331          *     In the future, a flush daemon will write out the page.
332         res = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
333          */
334
335         EXIT;
336         return res;
337 } /* obdfs_add_page_to_cache */
338
339
340 /* select between SYNC and ASYNC I/O methods */
341 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
342 {
343         int err;
344
345         ENTRY;
346         /* PDEBUG(page, "WRITEPAGE"); */
347         if ( sync )
348                 err = obdfs_brw(WRITE, inode, page, 1);
349         else {
350                 err = obdfs_add_page_to_cache(inode, page);
351                 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
352                        inode->i_ino, page, err, Page_Uptodate(page));
353         }
354                 
355         if ( !err )
356                 SetPageUptodate(page);
357         /* PDEBUG(page,"WRITEPAGE"); */
358         EXIT;
359         return err;
360 } /* obdfs_do_writepage */
361
362 /* returns the page unlocked, but with a reference */
363 int obdfs_writepage(struct dentry *dentry, struct page *page)
364 {
365         return obdfs_do_writepage(dentry->d_inode, page, 0);
366 }
367
368 /*
369  * This does the "real" work of the write. The generic routine has
370  * allocated the page, locked it, done all the page alignment stuff
371  * calculations etc. Now we should just copy the data from user
372  * space and write it back to the real medium..
373  *
374  * If the writer ends up delaying the write, the writer needs to
375  * increment the page use counts until he is done with the page.
376  *
377  * Return value is the number of bytes written.
378  */
379 int obdfs_write_one_page(struct file *file, struct page *page,
380                          unsigned long offset, unsigned long bytes,
381                          const char * buf)
382 {
383         struct inode *inode = file->f_dentry->d_inode;
384         int err;
385
386         ENTRY;
387         /* We check for complete page writes here, as we then don't have to
388          * get the page before writing over everything anyways.
389          */
390         if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
391                 err = obdfs_brw(READ, inode, page, 0);
392                 if ( err )
393                         return err;
394                 SetPageUptodate(page);
395         }
396
397         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
398                 return -EFAULT;
399
400         lock_kernel();
401         err = obdfs_writepage(file->f_dentry, page);
402         unlock_kernel();
403
404         return (err < 0 ? err : bytes);
405 } /* obdfs_write_one_page */
406
407 /* 
408  * return an up to date page:
409  *  - if locked is true then is returned locked
410  *  - if create is true the corresponding disk blocks are created 
411  *  - page is held, i.e. caller must release the page
412  *
413  * modeled on NFS code.
414  */
415 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
416                            int create, int locked)
417 {
418         struct page * page;
419         int index;
420         int err;
421
422         ENTRY;
423
424         offset = offset & PAGE_CACHE_MASK;
425         CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
426                inode->i_ino, offset, create, locked);
427         index = offset >> PAGE_CACHE_SHIFT;
428
429         page = grab_cache_page(&inode->i_data, index);
430
431         /* Yuck, no page */
432         if (! page) {
433             printk(KERN_WARNING " grab_cache_page says no dice ...\n");
434             EXIT;
435             return NULL;
436         }
437
438         /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
439         /* now check if the data in the page is up to date */
440         if ( Page_Uptodate(page)) { 
441                 if (!locked)
442                         UnlockPage(page);
443                 EXIT;
444                 return page;
445         } 
446
447
448 #ifdef EXT2_OBD_DEBUG
449         if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
450                 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
451                        page, page->index);
452         }
453 #endif
454
455         err = obdfs_brw(READ, inode, page, create);
456
457         if ( err ) {
458                 SetPageError(page);
459                 UnlockPage(page);
460                 EXIT;
461                 return page;
462         }
463
464         if ( !locked )
465                 UnlockPage(page);
466         SetPageUptodate(page);
467         /* PDEBUG(page,"GETPAGE - after reading"); */
468         EXIT;
469         return page;
470 } /* obdfs_getpage */
471
472
473 void obdfs_truncate(struct inode *inode)
474 {
475         struct obdo *oa;
476         int err;
477         ENTRY;
478
479         obdfs_dequeue_pages(inode);
480
481         if (IOPS(inode, punch) == NULL) {
482                 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
483                 EXIT;
484                 return;
485         }
486         oa = obdo_alloc();
487         if ( !oa ) {
488                 /* XXX This would give an inconsistent FS, so deal with it as
489                  * best we can for now - an obdo on the stack is not pretty.
490                  */
491                 struct obdo obdo;
492
493                 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
494
495                 obdo.o_valid = OBD_MD_FLNOTOBD;
496                 obdfs_from_inode(&obdo, inode);
497
498                 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
499         } else {
500                 oa->o_valid = OBD_MD_FLNOTOBD;
501                 obdfs_from_inode(oa, inode);
502
503                 CDEBUG(D_PUNCH, "calling punch for %ld (%Lu bytes at 0)\n",
504                        (long)oa->o_id, oa->o_size);
505                 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
506
507                 obdo_free(oa);
508         }
509
510         if (err) {
511                 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
512                 EXIT;
513                 return;
514         }
515         EXIT;
516 }