http://prdownloads.sourceforge.net/lufs/lufs-0.9.6.tar.gz?download
[lufs.git] / kernel / Linux / 2.6 / file.c
1 /*
2  * file.c
3  * Copyright (C) 2002 Florin Malita <mali@go.ro>
4  *
5  * This file is part of LUFS, a free userspace filesystem implementation.
6  * See http://lufs.sourceforge.net/ for updates.
7  *
8  * LUFS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * LUFS is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/pagemap.h>
28 #include <linux/socket.h>
29
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32
33 #include <linux/smp_lock.h>
34
35 #include "lufs.h"
36 #include "proc.h"
37
38 extern int lufs_notify_change(struct dentry*, struct iattr*);
39 extern int lu_revalidate_inode(struct dentry*);
40
41 static int lu_file_open(struct inode *inode, struct file *file)
42 {
43     int res, gres;
44     struct server_slot *slot;
45     struct iovec iov[2];
46     unsigned flags;
47
48     TRACE("in\n");
49
50     if((gres = generic_file_open(inode, file)) < 0)
51         return gres;
52
53     TRACE("f_mode: %u, i_mode: %u\n", file->f_mode, inode->i_mode);
54     TRACE("f_flags: %u, i_flags: %u\n", file->f_flags, inode->i_flags);
55
56     if((slot = lu_getslot(GET_INFO(file->f_dentry->d_sb))) == NULL)
57         return gres;
58
59     if((res = lu_getname(file->f_dentry, slot->s_buf, LU_MAXDATA)) < 0){
60         WARN("lu_getname failed!\n");
61         goto out;
62     }
63
64     flags = file->f_flags & O_ACCMODE;
65     iov[0].iov_base = &flags;
66     iov[0].iov_len = sizeof(flags);
67     iov[1].iov_base = slot->s_buf;
68     iov[1].iov_len = strlen(slot->s_buf) + 1;
69
70     lu_execute(GET_INFO(file->f_dentry->d_sb), slot, PTYPE_OPEN, iov, 2, NULL, 0);
71
72 out:
73     lu_putslot(slot);
74
75     TRACE("out\n");
76     return gres;
77 }
78
79 static int lu_file_release(struct inode *inode, struct file *file)
80 {
81     int res;
82     struct server_slot *slot;
83     struct iovec iov;
84
85     TRACE("in\n");
86
87     if((slot = lu_getslot(GET_INFO(file->f_dentry->d_sb))) == NULL)
88         return -ERESTARTSYS;
89
90     if((res = lu_getname(file->f_dentry, slot->s_buf, LU_MAXPATHLEN)) < 0){
91         WARN("lu_getname failed!\n");
92         goto out;
93     }
94     
95     iov.iov_base = slot->s_buf;
96     iov.iov_len = strlen(slot->s_buf) + 1;
97
98     if((res = lu_execute(GET_INFO(file->f_dentry->d_sb), slot, PTYPE_RELEASE, &iov, 1, NULL, 0)) < 0)
99         goto out;
100
101     if(PIS_ERROR(res)){
102         TRACE("release failed\n");
103         res = PERROR(res);
104         goto out;
105     }
106     
107     res = 0;
108
109 out:
110     lu_putslot(slot);
111
112     TRACE("out\n");
113     return res;
114 }
115
116 static int lu_file_readpage(struct file *f, struct page *p)
117 {
118     int res;
119     struct iovec siov[3], riov;
120     long long offset;
121     unsigned long count;
122     struct server_slot *slot;
123
124     TRACE("in\n");
125
126     if((slot = lu_getslot(GET_INFO(f->f_dentry->d_sb))) == NULL)
127         return -ERESTARTSYS;
128
129     get_page(p);
130
131     if((res = lu_getname(f->f_dentry, slot->s_buf, LU_MAXDATA)) < 0){
132         WARN("lu_getname failed!\n");
133         goto out;
134     }
135
136     offset = p->index << PAGE_CACHE_SHIFT;
137     count = PAGE_SIZE;
138
139     siov[0].iov_base = &offset;
140     siov[0].iov_len = sizeof(offset);
141     siov[1].iov_base = &count;
142     siov[1].iov_len = sizeof(count);
143     siov[2].iov_base = slot->s_buf;
144     siov[2].iov_len = strlen(slot->s_buf) + 1;
145
146     riov.iov_base = page_address(p);
147     riov.iov_len = count;
148
149     if((res = lu_execute(GET_INFO(f->f_dentry->d_sb), slot, PTYPE_READ, siov, 3, &riov, 1)) < 0)
150         goto out;
151
152     if(PIS_ERROR(res)){
153         TRACE("read failed\n");
154         res = PERROR(res);
155         goto out;
156     }
157     
158     flush_dcache_page(p);
159     SetPageUptodate(p);
160     res = 0;
161     
162   out:
163     lu_putslot(slot);
164     unlock_page(p);
165     put_page(p);
166         
167     TRACE("out\n");
168     return res;
169 }
170
171 static int lu_file_writepage(struct page *p, struct writeback_control *wbc)
172 {
173     TRACE("in\n");
174
175     TRACE("out\n");
176     return -1;
177 }
178
179 static int lu_file_preparewrite(struct file *f, struct page *p, unsigned offset, unsigned to)
180 {
181     TRACE("in\n");
182
183     TRACE("out\n");
184
185     return 0;
186 }
187
188 static int lu_file_commitwrite(struct file *f, struct page *p, unsigned offset, unsigned to)
189 {
190     int res;
191     struct server_slot *slot;
192     struct iovec iov[4];
193     char *buf;
194     long long off;
195     unsigned long cnt;
196
197     TRACE("in\n");
198
199     if((slot = lu_getslot(GET_INFO(f->f_dentry->d_sb))) == NULL)
200         return -ERESTARTSYS;
201
202     if((res = lu_getname(f->f_dentry, slot->s_buf, LU_MAXDATA)) < 0){
203         WARN("lu_getname failed!\n");
204         goto out2;
205     }
206
207     lock_kernel();
208
209     buf = kmap(p) + offset;
210     cnt = to - offset;
211     off = offset + (((long long)p->index) << PAGE_CACHE_SHIFT);
212
213     iov[0].iov_base = &off;
214     iov[0].iov_len = sizeof(off);
215     iov[1].iov_base = &cnt;
216     iov[1].iov_len = sizeof(cnt);
217     iov[2].iov_base = slot->s_buf;
218     iov[2].iov_len = strlen(slot->s_buf) + 1;
219     iov[3].iov_base = buf;
220     iov[3].iov_len = cnt;
221
222     TRACE("write %s, offset %Ld, count %d\n", slot->s_buf, off, (int)cnt);
223
224     if((res = lu_execute(GET_INFO(f->f_dentry->d_sb), slot, PTYPE_WRITE, iov, 4, NULL, 0)) < 0)
225         goto out1;
226
227
228     if(PIS_ERROR(res)){
229         TRACE("write failed\n");
230         res = PERROR(res);
231         goto out1;
232     }
233
234     f->f_dentry->d_inode->i_mtime = f->f_dentry->d_inode->i_atime = CURRENT_TIME;
235     if(off + cnt > f->f_dentry->d_inode->i_size)
236         f->f_dentry->d_inode->i_size = off + cnt;
237
238     res = cnt;
239
240   out1:
241     kunmap(p);
242     unlock_kernel();
243   out2:
244     lu_putslot(slot);
245     TRACE("out\n");
246     return res;
247 }
248
249 static int lu_file_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
250 {
251     struct dentry *dentry = filp->f_dentry;
252     int res;
253
254     TRACE("in\n");
255
256     if(!(res = lu_revalidate_inode(dentry)))
257         res = generic_file_read(filp, buf, count, ppos);
258
259     TRACE("out\n");
260     
261     return res;
262 }
263
264 static int lu_file_mmap(struct file *filp, struct vm_area_struct *vma)
265 {
266     struct dentry *dentry = filp->f_dentry;
267     int res;
268
269     TRACE("in\n");
270
271     if(!(res = lu_revalidate_inode(dentry)))
272         res = generic_file_mmap(filp, vma);
273
274     TRACE("out\n");
275
276     return res;
277 }
278
279 static ssize_t lu_file_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
280 {
281     struct dentry *dentry = filp->f_dentry;
282     ssize_t res;
283
284     TRACE("in\n");
285
286     if(!(res = lu_revalidate_inode(dentry)) && (count > 0))
287         res = generic_file_write(filp, buf, count, ppos);
288
289     TRACE("out\n");
290
291     return res;
292 }
293
294 static int lu_file_fsync(struct file *filp, struct dentry *dentryp, int datasync)
295 {    
296     return 0;
297 }
298
299 struct file_operations lu_file_operations = {
300     .llseek     = generic_file_llseek,
301     .read       = lu_file_read,
302     .write      = lu_file_write,
303     .mmap       = lu_file_mmap,
304     .open       = lu_file_open,
305     .release    = lu_file_release,
306     .fsync      = lu_file_fsync,
307 };
308
309 struct inode_operations lu_file_inode_operations = {
310     .setattr    = lufs_notify_change,
311 };
312
313 struct address_space_operations lu_file_aops = {
314     .readpage           = lu_file_readpage,
315     .writepage          = lu_file_writepage,
316     .prepare_write      = lu_file_preparewrite,
317     .commit_write       = lu_file_commitwrite,
318 };
319
320
321