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