Whamcloud - gitweb
obdfs/super.c: add call to flush pages when unmounting filesystem
[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         oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
51         if ( IS_ERR(oa) ) {
52                 EXIT;
53                 return PTR_ERR(oa);
54         }
55         obdfs_from_inode(oa, inode);
56
57         err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
58                                &buf, &count, &offset, &flags);
59
60         if ( !err )
61                 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
62
63         obdo_free(oa);
64         
65         EXIT;
66         return err;
67 } /* obdfs_brw */
68
69 /* returns the page unlocked, but with a reference */
70 int obdfs_readpage(struct dentry *dentry, struct page *page)
71 {
72         struct inode *inode = dentry->d_inode;
73         int rc;
74
75         ENTRY;
76         PDEBUG(page, "READ");
77         rc = obdfs_brw(READ, inode, page, 0);
78         if ( !rc ) {
79                 SetPageUptodate(page);
80                 UnlockPage(page);
81         } 
82         PDEBUG(page, "READ");
83         EXIT;
84         return rc;
85 } /* obdfs_readpage */
86
87 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
88
89 /* XXX should probably have one of these per superblock */
90 static int obdfs_cache_count = 0;
91
92 int obdfs_init_pgrqcache(void)
93 {
94         ENTRY;
95         if (obdfs_pgrq_cachep == NULL) {
96                 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
97                 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
98                                                       sizeof(struct obdfs_pgrq),
99                                                       0, SLAB_HWCACHE_ALIGN,
100                                                       NULL, NULL);
101                 if (obdfs_pgrq_cachep == NULL) {
102                         EXIT;
103                         return -ENOMEM;
104                 } else {
105                         CDEBUG(D_INODE, "allocated cache at %p\n",
106                                obdfs_pgrq_cachep);
107                 }
108         } else {
109                 CDEBUG(D_INODE, "using existing cache at %p\n",
110                        obdfs_pgrq_cachep);
111         }
112         EXIT;
113         return 0;
114 } /* obdfs_init_wreqcache */
115
116 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
117 {
118         obdfs_cache_count--;
119         CDEBUG(D_INODE, "deleting page %p from list [count %d]\n",
120                pgrq->rq_page, obdfs_cache_count);
121         list_del(&pgrq->rq_plist);
122         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
123 }
124
125 void obdfs_cleanup_pgrqcache(void)
126 {
127         ENTRY;
128         if (obdfs_pgrq_cachep != NULL) {
129                 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p, count %d\n",
130                        obdfs_pgrq_cachep, obdfs_cache_count);
131                 if (kmem_cache_destroy(obdfs_pgrq_cachep))
132                         printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
133         } else
134                 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
135
136         EXIT;
137 } /* obdfs_cleanup_wreqcache */
138
139
140 /*
141  * Find a specific page in the page cache.  If it is found, we return
142  * the write request struct associated with it, if not found return NULL.
143  * Called with the list lock held.
144  */
145 static struct obdfs_pgrq *
146 obdfs_find_in_page_list(struct inode *inode, struct page *page)
147 {
148         struct list_head *page_list = obdfs_iplist(inode);
149         struct list_head *tmp;
150
151         ENTRY;
152
153         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
154         OIDEBUG(inode);
155
156         if (list_empty(page_list)) {
157                 CDEBUG(D_INODE, "empty list\n");
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 /* called with the list lock held */
179 static struct page* obdfs_find_page_index(struct inode *inode,
180                                           unsigned long index)
181 {
182         struct list_head *page_list = obdfs_iplist(inode);
183         struct list_head *tmp;
184         struct page *page;
185
186         ENTRY;
187
188         CDEBUG(D_INODE, "looking for inode %ld pageindex %ld\n",
189                inode->i_ino, index);
190         OIDEBUG(inode);
191
192         if (list_empty(page_list)) {
193                 EXIT;
194                 return NULL;
195         }
196         tmp = page_list;
197         while ( (tmp = tmp->next) != page_list ) {
198                 struct obdfs_pgrq *pgrq;
199
200                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
201                 page = pgrq->rq_page;
202                 if (index == page->index) {
203                         CDEBUG(D_INODE,
204                                "INDEX SEARCH found page %p, index %ld\n",
205                                page, index);
206                         EXIT;
207                         return page;
208                 }
209         } 
210
211         EXIT;
212         return NULL;
213 } /* obdfs_find_page_index */
214
215
216 /* call and free pages from Linux page cache: called with io lock on inodes */
217 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
218                     obd_count num_obdos, struct obdo **obdos,
219                     obd_count *oa_bufs, struct page **pages, char **bufs,
220                     obd_size *counts, obd_off *offsets, obd_flag *flags)
221 {
222         struct super_block *sb = inodes[0]->i_sb;
223         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
224         int err;
225
226         ENTRY;
227         CDEBUG(D_INODE, "writing %d page(s), %d obdo(s) in vector\n",
228                num_io, num_obdos);
229         err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
230                                 bufs, counts, offsets, flags);
231
232         /* release the pages from the page cache */
233         while ( num_io > 0 ) {
234                 num_io--;
235                 CDEBUG(D_INODE, "calling put_page for %p, index %ld\n",
236                        pages[num_io], pages[num_io]->index);
237                 put_page(pages[num_io]);
238         }
239
240         while ( num_obdos > 0) {
241                 num_obdos--;
242                 CDEBUG(D_INODE, "copy/free obdo %ld\n",
243                        (long)obdos[num_obdos]->o_id);
244                 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
245                 obdo_free(obdos[num_obdos]);
246         }
247         EXIT;
248         return err;
249 }
250
251
252 /*
253  * Add a page to the write request cache list for later writing
254  * ASYNCHRONOUS write method.
255  */
256 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
257 {
258         int res = 0;
259
260         ENTRY;
261
262         /* If this page isn't already in the inode page list, add it */
263         obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
264         if ( !obdfs_find_in_page_list(inode, page) ) {
265                 struct obdfs_pgrq *pgrq;
266                 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
267                 CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p, cache count [%d]\n",
268                        inode->i_ino, page, pgrq, obdfs_cache_count);
269                 if (!pgrq) {
270                         EXIT;
271                         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
272                         return -ENOMEM;
273                 }
274                 memset(pgrq, 0, sizeof(*pgrq)); 
275                 
276                 pgrq->rq_page = page;
277                 get_page(pgrq->rq_page);
278                 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
279                 obdfs_cache_count++;
280         }
281
282         /* If inode isn't already on the superblock inodes list, add it,
283          * and increase ref count on inode so it doesn't disappear on us.
284          */
285         if ( list_empty(obdfs_islist(inode)) ) {
286                 iget(inode->i_sb, inode->i_ino);
287                 CDEBUG(D_INODE, "adding inode %ld to superblock list %p\n",
288                        inode->i_ino, obdfs_slist(inode));
289                 list_add(obdfs_islist(inode), obdfs_slist(inode));
290         }
291
292         /* XXX For testing purposes, we write out the page here.
293          *     In the future, a flush daemon will write out the page.
294         res = obdfs_flush_reqs(obdfs_slist(inode), 0);
295         obdfs_flush_dirty_pages(1);
296          */
297         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
298
299         EXIT;
300         return res;
301 } /* obdfs_add_page_to_cache */
302
303
304 /* select between SYNC and ASYNC I/O methods */
305 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
306 {
307         int err;
308
309         ENTRY;
310         /* PDEBUG(page, "WRITEPAGE"); */
311         if ( sync )
312                 err = obdfs_brw(WRITE, inode, page, 1);
313         else {
314                 err = obdfs_add_page_to_cache(inode, page);
315                 CDEBUG(D_IOCTL, "DO_WR ino: %ld, page %p, err %d, uptodata %d\n", inode->i_ino, page, err, Page_Uptodate(page));
316         }
317                 
318         if ( !err )
319                 SetPageUptodate(page);
320         /* PDEBUG(page,"WRITEPAGE"); */
321         EXIT;
322         return err;
323 } /* obdfs_do_writepage */
324
325 /* returns the page unlocked, but with a reference */
326 int obdfs_writepage(struct dentry *dentry, struct page *page)
327 {
328         return obdfs_do_writepage(dentry->d_inode, page, 0);
329 }
330
331 /*
332  * This does the "real" work of the write. The generic routine has
333  * allocated the page, locked it, done all the page alignment stuff
334  * calculations etc. Now we should just copy the data from user
335  * space and write it back to the real medium..
336  *
337  * If the writer ends up delaying the write, the writer needs to
338  * increment the page use counts until he is done with the page.
339  *
340  * Return value is the number of bytes written.
341  */
342 int obdfs_write_one_page(struct file *file, struct page *page,
343                           unsigned long offset, unsigned long bytes,
344                           const char * buf)
345 {
346         struct inode *inode = file->f_dentry->d_inode;
347         int err;
348
349         ENTRY;
350         if ( !Page_Uptodate(page) ) {
351                 err = obdfs_brw(READ, inode, page, 1);
352                 if ( !err )
353                         SetPageUptodate(page);
354                 else
355                         return err;
356         }
357
358         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
359                 return -EFAULT;
360
361         lock_kernel();
362         err = obdfs_writepage(file->f_dentry, page);
363         unlock_kernel();
364
365         return (err < 0 ? err : bytes);
366 } /* obdfs_write_one_page */
367
368 /* 
369  * return an up to date page:
370  *  - if locked is true then is returned locked
371  *  - if create is true the corresponding disk blocks are created 
372  *  - page is held, i.e. caller must release the page
373  *
374  * modeled on NFS code.
375  */
376 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
377                            int create, int locked)
378 {
379         struct page *page_cache;
380         int index;
381         struct page ** hash;
382         struct page * page;
383         int err;
384
385         ENTRY;
386
387         offset = offset & PAGE_CACHE_MASK;
388         CDEBUG(D_INODE, "ino: %ld, offset %ld, create %d, locked %d\n",
389                inode->i_ino, offset, create, locked);
390         index = offset >> PAGE_CACHE_SHIFT;
391
392
393         page = NULL;
394         page_cache = page_cache_alloc();
395         if ( ! page_cache ) {
396                 EXIT;
397                 return NULL;
398         }
399         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
400
401         hash = page_hash(&inode->i_data, index);
402         page = grab_cache_page(&inode->i_data, index);
403
404         /* Yuck, no page */
405         if (! page) {
406             printk("grab_cache_page says no dice ...\n");
407             EXIT;
408             return 0;
409         }
410
411         PDEBUG(page, "GETPAGE: got page - before reading\n");
412         /* now check if the data in the page is up to date */
413         if ( Page_Uptodate(page)) { 
414                 if (!locked)
415                         UnlockPage(page);
416                 EXIT;
417                 return page;
418         } 
419
420
421         if ( obdfs_find_page_index(inode, index) ) {
422                 CDEBUG(D_INODE, "OVERWRITE: found dirty page %p, index %ld\n",
423                        page, page->index);
424         }
425
426         err = obdfs_brw(READ, inode, page, create);
427
428         if ( err ) {
429                 SetPageError(page);
430                 UnlockPage(page);
431                 EXIT;
432                 return page;
433         }
434
435         if ( !locked )
436                 UnlockPage(page);
437         SetPageUptodate(page);
438         PDEBUG(page,"GETPAGE - after reading");
439         EXIT;
440         return page;
441 } /* obdfs_getpage */
442
443