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