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