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