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