Whamcloud - gitweb
obdfs/flushd.c: send writes in FIFO order (use ->prev from list)
[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_CACHE, "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_CACHE, "allocated cache at %p\n",
106                                obdfs_pgrq_cachep);
107                 }
108         } else {
109                 CDEBUG(D_CACHE, "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_INFO, "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_CACHE, "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 __FUNCTION__
133                                ": unable to free all of cache\n");
134                 obdfs_pgrq_cachep = NULL;
135         } else
136                 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
137
138         EXIT;
139 } /* obdfs_cleanup_wreqcache */
140
141
142 /*
143  * Find a specific page in the page cache.  If it is found, we return
144  * the write request struct associated with it, if not found return NULL.
145  * Called with the list lock held.
146  */
147 static struct obdfs_pgrq *
148 obdfs_find_in_page_list(struct inode *inode, struct page *page)
149 {
150         struct list_head *page_list = obdfs_iplist(inode);
151         struct list_head *tmp;
152
153         ENTRY;
154
155         CDEBUG(D_INFO, "looking for inode %ld page %p\n", inode->i_ino, page);
156         OIDEBUG(inode);
157
158         if (list_empty(page_list)) {
159                 CDEBUG(D_INFO, "empty list\n");
160                 EXIT;
161                 return NULL;
162         }
163         tmp = page_list;
164         while ( (tmp = tmp->next) != page_list ) {
165                 struct obdfs_pgrq *pgrq;
166
167                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
168                 if (pgrq->rq_page == page) {
169                         CDEBUG(D_INFO, "found page %p in list\n", page);
170                         EXIT;
171                         return pgrq;
172                 }
173         } 
174
175         EXIT;
176         return NULL;
177 } /* obdfs_find_in_page_list */
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         struct super_block *sb = inodes[0]->i_sb;
225         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)&sb->u.generic_sbp;
226         int err;
227
228         ENTRY;
229         CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
230                num_io, num_obdos);
231         err = OPS(sb, brw)(WRITE, &sbi->osi_conn, num_obdos, obdos, oa_bufs,
232                                 bufs, counts, offsets, flags);
233
234         /* release the pages from the page cache */
235         while ( num_io > 0 ) {
236                 num_io--;
237                 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
238                        pages[num_io], pages[num_io]->index);
239                 put_page(pages[num_io]);
240         }
241
242         while ( num_obdos > 0) {
243                 num_obdos--;
244                 CDEBUG(D_INFO, "copy/free obdo %ld\n",
245                        (long)obdos[num_obdos]->o_id);
246                 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
247                 obdo_free(obdos[num_obdos]);
248         }
249         EXIT;
250         return err;
251 }
252
253
254 /*
255  * Add a page to the write request cache list for later writing
256  * ASYNCHRONOUS write method.
257  */
258 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
259 {
260         int res = 0;
261
262         ENTRY;
263
264         /* If this page isn't already in the inode page list, add it */
265         obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
266         if ( !obdfs_find_in_page_list(inode, page) ) {
267                 struct obdfs_pgrq *pgrq;
268                 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
269                 CDEBUG(D_INFO,
270                        "adding inode %ld page %p, pgrq: %p, cache count [%d]\n",
271                        inode->i_ino, page, pgrq, obdfs_cache_count + 1);
272                 if (!pgrq) {
273                         EXIT;
274                         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
275                         return -ENOMEM;
276                 }
277                 memset(pgrq, 0, sizeof(*pgrq)); 
278                 
279                 pgrq->rq_page = page;
280                 pgrq->rq_jiffies = jiffies;
281                 get_page(pgrq->rq_page);
282                 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
283                 obdfs_cache_count++;
284         }
285
286         /* If inode isn't already on the superblock inodes list, add it,
287          * and increase ref count on inode so it doesn't disappear on us.
288          *
289          * We increment the reference count on the inode to keep it from
290          * being freed from memory.  This _should_ be an iget() with an
291          * iput() in both flush_reqs() and put_inode(), but since ut_inode()
292          * is called from iput() we can't call iput() again there.  Instead
293          * we just increment/decrement i_count, which is essentially what
294          * iget/iput do for an inode already in memory.
295          */
296         if ( list_empty(obdfs_islist(inode)) ) {
297                 inode->i_count++;
298                 CDEBUG(D_INFO, "adding inode %ld to superblock list %p\n",
299                        inode->i_ino, obdfs_slist(inode));
300                 list_add(obdfs_islist(inode), obdfs_slist(inode));
301         }
302
303         /* XXX For testing purposes, we write out the page here.
304          *     In the future, a flush daemon will write out the page.
305         res = obdfs_flush_reqs(obdfs_slist(inode), 0);
306         obdfs_flush_dirty_pages(1);
307          */
308         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
309
310         EXIT;
311         return res;
312 } /* obdfs_add_page_to_cache */
313
314
315 /* select between SYNC and ASYNC I/O methods */
316 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
317 {
318         int err;
319
320         ENTRY;
321         /* PDEBUG(page, "WRITEPAGE"); */
322         if ( sync )
323                 err = obdfs_brw(WRITE, inode, page, 1);
324         else {
325                 err = obdfs_add_page_to_cache(inode, page);
326                 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
327                        inode->i_ino, page, err, Page_Uptodate(page));
328         }
329                 
330         if ( !err )
331                 SetPageUptodate(page);
332         /* PDEBUG(page,"WRITEPAGE"); */
333         EXIT;
334         return err;
335 } /* obdfs_do_writepage */
336
337 /* returns the page unlocked, but with a reference */
338 int obdfs_writepage(struct dentry *dentry, struct page *page)
339 {
340         return obdfs_do_writepage(dentry->d_inode, page, 0);
341 }
342
343 /*
344  * This does the "real" work of the write. The generic routine has
345  * allocated the page, locked it, done all the page alignment stuff
346  * calculations etc. Now we should just copy the data from user
347  * space and write it back to the real medium..
348  *
349  * If the writer ends up delaying the write, the writer needs to
350  * increment the page use counts until he is done with the page.
351  *
352  * Return value is the number of bytes written.
353  */
354 int obdfs_write_one_page(struct file *file, struct page *page,
355                           unsigned long offset, unsigned long bytes,
356                           const char * buf)
357 {
358         struct inode *inode = file->f_dentry->d_inode;
359         int err;
360
361         ENTRY;
362         if ( !Page_Uptodate(page) ) {
363                 err = obdfs_brw(READ, inode, page, 1);
364                 if ( !err )
365                         SetPageUptodate(page);
366                 else
367                         return err;
368         }
369
370         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
371                 return -EFAULT;
372
373         lock_kernel();
374         err = obdfs_writepage(file->f_dentry, page);
375         unlock_kernel();
376
377         return (err < 0 ? err : bytes);
378 } /* obdfs_write_one_page */
379
380 /* 
381  * return an up to date page:
382  *  - if locked is true then is returned locked
383  *  - if create is true the corresponding disk blocks are created 
384  *  - page is held, i.e. caller must release the page
385  *
386  * modeled on NFS code.
387  */
388 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
389                            int create, int locked)
390 {
391         struct page *page_cache;
392         int index;
393         struct page ** hash;
394         struct page * page;
395         int err;
396
397         ENTRY;
398
399         offset = offset & PAGE_CACHE_MASK;
400         CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
401                inode->i_ino, offset, create, locked);
402         index = offset >> PAGE_CACHE_SHIFT;
403
404
405         page = NULL;
406         page_cache = page_cache_alloc();
407         if ( ! page_cache ) {
408                 EXIT;
409                 return NULL;
410         }
411         CDEBUG(D_INFO, "page_cache %p\n", page_cache);
412
413         hash = page_hash(&inode->i_data, index);
414         page = grab_cache_page(&inode->i_data, index);
415
416         /* Yuck, no page */
417         if (! page) {
418             printk(KERN_WARNING " grab_cache_page says no dice ...\n");
419             EXIT;
420             return 0;
421         }
422
423         PDEBUG(page, "GETPAGE: got page - before reading\n");
424         /* now check if the data in the page is up to date */
425         if ( Page_Uptodate(page)) { 
426                 if (!locked)
427                         UnlockPage(page);
428                 EXIT;
429                 return page;
430         } 
431
432
433         if ( obdfs_find_page_index(inode, index) ) {
434                 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
435                        page, page->index);
436         }
437
438         err = obdfs_brw(READ, inode, page, create);
439
440         if ( err ) {
441                 SetPageError(page);
442                 UnlockPage(page);
443                 EXIT;
444                 return page;
445         }
446
447         if ( !locked )
448                 UnlockPage(page);
449         SetPageUptodate(page);
450         PDEBUG(page,"GETPAGE - after reading");
451         EXIT;
452         return page;
453 } /* obdfs_getpage */
454
455