Whamcloud - gitweb
558cfedf6ee3661592171e7558e065154fc62c60
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copryright (C) 1999 Stelias Computing Inc, 
5  *                (author Peter J. Braam <braam@stelias.com>)
6  * Copryright (C) 1999 Seagate Technology Inc.
7  */
8
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/errno.h>
16 #include <linux/locks.h>
17 #include <linux/unistd.h>
18
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <asm/uaccess.h>
25 #include <linux/vmalloc.h>
26 #include <asm/segment.h>
27 #include <linux/mm.h>
28 #include <linux/pagemap.h>
29 #include <linux/smp_lock.h>
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_ext2.h>
33 #include <linux/obdfs.h>
34
35 int console_loglevel;
36
37 /* VFS super_block ops */
38
39 #if 0
40 int obdfs_brw(struct inode *dir, int rw, struct page *page, int create)
41 {
42         return iops(dir)->o_brw(rw, iid(dir), dir, page, create);
43 }
44 #endif
45
46 /* returns the page unlocked, but with a reference */
47 int obdfs_readpage(struct dentry *dentry, struct page *page)
48 {
49         struct inode *inode = dentry->d_inode;
50         struct obdfs_wreq *wreq;
51         int rc = 0;
52
53         ENTRY;
54         PDEBUG(page, "READ");
55         rc =  iops(inode)->o_brw(READ, iid(inode),inode, page, 0);
56         if (rc == PAGE_SIZE ) {
57                 SetPageUptodate(page);
58                 UnlockPage(page);
59         } 
60         PDEBUG(page, "READ");
61         if ( rc == PAGE_SIZE ) 
62                 rc = 0;
63         } 
64         EXIT;
65         return rc;
66 }
67
68 static kmem_cache_t *obdfs_wreq_cachep;
69
70 int obdfs_init_wreqcache(void)
71 {
72         /* XXX need to free this somewhere? */
73         ENTRY;
74         obdfs_wreq_cachep = kmem_cache_create("obdfs_wreq",
75                                               sizeof(struct obdfs_wreq),
76                                               0, SLAB_HWCACHE_ALIGN,
77                                               NULL, NULL);
78         if (obdfs_wreq_cachep == NULL) {
79                 EXIT;
80                 return -ENOMEM;
81         }
82         EXIT;
83         return 0;
84 }
85
86 /*
87  * Find a specific page in the page cache.  If it is found, we return
88  * the write request struct associated with it, if not found return NULL.
89  */
90 static struct obdfs_wreq *
91 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
92 {
93         struct list_head *list_head = &OBD_LIST(inode);
94         struct obdfs_wreq *head, *wreq;
95
96         ENTRY;
97         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
98         if (list_empty(list_head)) {
99                 CDEBUG(D_INODE, "empty list\n");
100                 EXIT;
101                 return NULL;
102         }
103         wreq = head = WREQ(list_head->next);
104         do {
105                 CDEBUG(D_INODE, "checking page %p\n", wreq->wb_page);
106                 if (wreq->wb_page == page) {
107                         CDEBUG(D_INODE, "found page %p in list\n", page);
108                         EXIT;
109                         return wreq;
110                 }
111         } while ((wreq = WB_NEXT(wreq)) != head);
112
113         EXIT;
114         return NULL;
115 }
116
117
118 /*
119  * Remove a writeback request from a list
120  */
121 static inline int
122 obdfs_remove_from_page_cache(struct obdfs_wreq *wreq)
123 {
124         struct inode *inode = wreq->wb_inode;
125         struct page *page = wreq->wb_page;
126         int rc;
127
128         ENTRY;
129         CDEBUG(D_INODE, "removing inode %ld page %p, wreq: %p\n",
130                inode->i_ino, page, wreq);
131         rc = iops(inode)->o_brw(WRITE, iid(inode), inode, page, 1);
132         /* XXX probably should handle error here somehow.  I think that
133          *     ext2 also does the same thing - discard write even if error?
134          */
135         put_page(page);
136         list_del(&wreq->wb_list);
137         kmem_cache_free(obdfs_wreq_cachep, wreq);
138
139         EXIT;
140         return rc;
141 }
142
143 /*
144  * Add a page to the write request cache list for later writing
145  */
146 static int
147 obdfs_add_to_page_cache(struct inode *inode, struct page *page)
148 {
149         struct obdfs_wreq *wreq;
150
151         ENTRY;
152         wreq = kmem_cache_alloc(obdfs_wreq_cachep, SLAB_KERNEL);
153         CDEBUG(D_INODE, "adding inode %ld page %p, wreq: %p\n",
154                inode->i_ino, page, wreq);
155         if (!wreq) {
156                 EXIT;
157                 return -ENOMEM;
158         }
159         memset(wreq, 0, sizeof(*wreq)); 
160
161         wreq->wb_page = page;
162         wreq->wb_inode = inode;
163
164         get_page(wreq->wb_page);
165         list_add(&wreq->wb_list, &OBD_LIST(inode));
166
167         /* For testing purposes, we write out the page here.
168          * In the future, a flush daemon will write out the page.
169          */
170         wreq = obdfs_find_in_page_cache(inode, page);
171         if (!wreq) {
172                 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
173                 return -EINVAL;
174         } else
175                 return obdfs_remove_from_page_cache(wreq);
176
177         return 0;
178 }
179
180
181 /* returns the page unlocked, but with a reference */
182 int obdfs_writepage(struct dentry *dentry, struct page *page)
183 {
184         struct inode *inode = dentry->d_inode;
185         int rc;
186
187         ENTRY;
188         PDEBUG(page, "WRITEPAGE");
189         /* XXX flush stuff */
190         rc = obdfs_add_to_page_cache(inode, page);
191
192         if (!rc)
193                 SetPageUptodate(page);
194         PDEBUG(page,"WRITEPAGE");
195         return rc;
196 }
197
198 /*
199  * This does the "real" work of the write. The generic routine has
200  * allocated the page, locked it, done all the page alignment stuff
201  * calculations etc. Now we should just copy the data from user
202  * space and write it back to the real medium..
203  *
204  * If the writer ends up delaying the write, the writer needs to
205  * increment the page use counts until he is done with the page.
206  */
207 int obdfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
208 {
209         long status;
210         struct inode *inode = file->f_dentry->d_inode;
211
212         ENTRY;
213         if ( !Page_Uptodate(page) ) {
214                 status =  iops(inode)->o_brw(READ, iid(inode), inode, page, 1);
215                 if (status == PAGE_SIZE ) {
216                         SetPageUptodate(page);
217                 } else { 
218                         return status;
219                 }
220         }
221         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
222         status = -EFAULT;
223
224         if (bytes) {
225                 lock_kernel();
226                 status = obdfs_writepage(file->f_dentry, page);
227                 unlock_kernel();
228         }
229         EXIT;
230         if ( status != PAGE_SIZE ) 
231                 return status;
232         else
233                 return bytes;
234 }
235
236
237
238
239
240 void report_inode(struct page * page) {
241         struct inode *inode = (struct inode *)0;
242         int offset = (int)&inode->i_data;
243         inode = (struct inode *)( (char *)page->mapping - offset);
244         if ( inode->i_sb->s_magic == 0x4711 )
245                 printk("----> ino %ld , dev %d\n", inode->i_ino, inode->i_dev);
246 }
247
248 /* 
249    return an up to date page:
250     - if locked is true then is returned locked
251     - if create is true the corresponding disk blocks are created 
252     - page is held, i.e. caller must release the page
253
254    modeled on NFS code.
255 */
256 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
257 {
258         struct page *page_cache;
259         struct page ** hash;
260         struct page * page;
261         int rc;
262
263         ENTRY;
264
265         offset = offset & PAGE_CACHE_MASK;
266         CDEBUG(D_INODE, "\n");
267         
268         page = NULL;
269         page_cache = page_cache_alloc();
270         if ( ! page_cache ) 
271                 return NULL;
272         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
273
274         hash = page_hash(&inode->i_data, offset);
275         page = grab_cache_page(&inode->i_data, offset);
276
277         PDEBUG(page, "GETPAGE: got page - before reading\n");
278         /* now check if the data in the page is up to date */
279         if ( Page_Uptodate(page)) { 
280                 if (!locked)
281                         UnlockPage(page);
282                 EXIT;
283                 return page;
284         } 
285
286         /* it's not: read it */
287         if (! page) {
288             printk("get_page_map says no dice ...\n");
289             return 0;
290         }
291
292         rc = iops(inode)->o_brw(READ, iid(inode), inode, page, create);
293         if ( rc != PAGE_SIZE ) {
294                 SetPageError(page);
295                 UnlockPage(page);
296                 return page;
297         }
298
299         if ( !locked )
300                 UnlockPage(page);
301         SetPageUptodate(page);
302         PDEBUG(page,"GETPAGE - after reading");
303         EXIT;
304         return page;
305 }
306
307