VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / libfs.c
1 /*
2  *      fs/libfs.c
3  *      Library for filesystems writers.
4  */
5
6 #include <linux/module.h>
7 #include <linux/pagemap.h>
8 #include <linux/mount.h>
9 #include <linux/vfs.h>
10 #include <asm/uaccess.h>
11
12 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
13                    struct kstat *stat)
14 {
15         struct inode *inode = dentry->d_inode;
16         generic_fillattr(inode, stat);
17         stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
18         return 0;
19 }
20
21 int simple_statfs(struct super_block *sb, struct kstatfs *buf)
22 {
23         buf->f_type = sb->s_magic;
24         buf->f_bsize = PAGE_CACHE_SIZE;
25         buf->f_namelen = NAME_MAX;
26         return 0;
27 }
28
29 /*
30  * Lookup the data. This is trivial - if the dentry didn't already
31  * exist, we know it is negative.
32  */
33
34 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
35 {
36         if (dentry->d_name.len > NAME_MAX)
37                 return ERR_PTR(-ENAMETOOLONG);
38         d_add(dentry, NULL);
39         return NULL;
40 }
41
42 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
43 {
44         return 0;
45 }
46  
47 int dcache_dir_open(struct inode *inode, struct file *file)
48 {
49         static struct qstr cursor_name = {.len = 1, .name = "."};
50
51         file->private_data = d_alloc(file->f_dentry, &cursor_name);
52
53         return file->private_data ? 0 : -ENOMEM;
54 }
55
56 int dcache_dir_close(struct inode *inode, struct file *file)
57 {
58         dput(file->private_data);
59         return 0;
60 }
61
62 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
63 {
64         down(&file->f_dentry->d_inode->i_sem);
65         switch (origin) {
66                 case 1:
67                         offset += file->f_pos;
68                 case 0:
69                         if (offset >= 0)
70                                 break;
71                 default:
72                         up(&file->f_dentry->d_inode->i_sem);
73                         return -EINVAL;
74         }
75         if (offset != file->f_pos) {
76                 file->f_pos = offset;
77                 if (file->f_pos >= 2) {
78                         struct list_head *p;
79                         struct dentry *cursor = file->private_data;
80                         loff_t n = file->f_pos - 2;
81
82                         spin_lock(&dcache_lock);
83                         list_del(&cursor->d_child);
84                         p = file->f_dentry->d_subdirs.next;
85                         while (n && p != &file->f_dentry->d_subdirs) {
86                                 struct dentry *next;
87                                 next = list_entry(p, struct dentry, d_child);
88                                 if (!d_unhashed(next) && next->d_inode)
89                                         n--;
90                                 p = p->next;
91                         }
92                         list_add_tail(&cursor->d_child, p);
93                         spin_unlock(&dcache_lock);
94                 }
95         }
96         up(&file->f_dentry->d_inode->i_sem);
97         return offset;
98 }
99
100 /* Relationship between i_mode and the DT_xxx types */
101 static inline unsigned char dt_type(struct inode *inode)
102 {
103         return (inode->i_mode >> 12) & 15;
104 }
105
106 /*
107  * Directory is locked and all positive dentries in it are safe, since
108  * for ramfs-type trees they can't go away without unlink() or rmdir(),
109  * both impossible due to the lock on directory.
110  */
111
112 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
113 {
114         struct dentry *dentry = filp->f_dentry;
115         struct dentry *cursor = filp->private_data;
116         struct list_head *p, *q = &cursor->d_child;
117         ino_t ino;
118         int i = filp->f_pos;
119
120         switch (i) {
121                 case 0:
122                         ino = dentry->d_inode->i_ino;
123                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
124                                 break;
125                         filp->f_pos++;
126                         i++;
127                         /* fallthrough */
128                 case 1:
129                         ino = parent_ino(dentry);
130                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
131                                 break;
132                         filp->f_pos++;
133                         i++;
134                         /* fallthrough */
135                 default:
136                         spin_lock(&dcache_lock);
137                         if (filp->f_pos == 2) {
138                                 list_del(q);
139                                 list_add(q, &dentry->d_subdirs);
140                         }
141                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
142                                 struct dentry *next;
143                                 next = list_entry(p, struct dentry, d_child);
144                                 if (d_unhashed(next) || !next->d_inode)
145                                         continue;
146
147                                 spin_unlock(&dcache_lock);
148                                 if (filldir(dirent, next->d_name.name, next->d_name.len, filp->f_pos, next->d_inode->i_ino, dt_type(next->d_inode)) < 0)
149                                         return 0;
150                                 spin_lock(&dcache_lock);
151                                 /* next is still alive */
152                                 list_del(q);
153                                 list_add(q, p);
154                                 p = q;
155                                 filp->f_pos++;
156                         }
157                         spin_unlock(&dcache_lock);
158         }
159         return 0;
160 }
161
162 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
163 {
164         return -EISDIR;
165 }
166
167 struct file_operations simple_dir_operations = {
168         .open           = dcache_dir_open,
169         .release        = dcache_dir_close,
170         .llseek         = dcache_dir_lseek,
171         .read           = generic_read_dir,
172         .readdir        = dcache_readdir,
173 };
174
175 struct inode_operations simple_dir_inode_operations = {
176         .lookup         = simple_lookup,
177 };
178
179 /*
180  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
181  * will never be mountable)
182  */
183 struct super_block *
184 get_sb_pseudo(struct file_system_type *fs_type, char *name,
185         struct super_operations *ops, unsigned long magic)
186 {
187         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
188         static struct super_operations default_ops = {.statfs = simple_statfs};
189         struct dentry *dentry;
190         struct inode *root;
191         struct qstr d_name = {.name = name, .len = strlen(name)};
192
193         if (IS_ERR(s))
194                 return s;
195
196         s->s_flags = MS_NOUSER;
197         s->s_maxbytes = ~0ULL;
198         s->s_blocksize = 1024;
199         s->s_blocksize_bits = 10;
200         s->s_magic = magic;
201         s->s_op = ops ? ops : &default_ops;
202         root = new_inode(s);
203         if (!root)
204                 goto Enomem;
205         root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
206         root->i_uid = root->i_gid = 0;
207         root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
208         dentry = d_alloc(NULL, &d_name);
209         if (!dentry) {
210                 iput(root);
211                 goto Enomem;
212         }
213         dentry->d_sb = s;
214         dentry->d_parent = dentry;
215         d_instantiate(dentry, root);
216         s->s_root = dentry;
217         s->s_flags |= MS_ACTIVE;
218         return s;
219
220 Enomem:
221         up_write(&s->s_umount);
222         deactivate_super(s);
223         return ERR_PTR(-ENOMEM);
224 }
225
226 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
227 {
228         struct inode *inode = old_dentry->d_inode;
229
230         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
231         inode->i_nlink++;
232         atomic_inc(&inode->i_count);
233         dget(dentry);
234         d_instantiate(dentry, inode);
235         return 0;
236 }
237
238 static inline int simple_positive(struct dentry *dentry)
239 {
240         return dentry->d_inode && !d_unhashed(dentry);
241 }
242
243 int simple_empty(struct dentry *dentry)
244 {
245         struct dentry *child;
246         int ret = 0;
247
248         spin_lock(&dcache_lock);
249         list_for_each_entry(child, &dentry->d_subdirs, d_child)
250                 if (simple_positive(child))
251                         goto out;
252         ret = 1;
253 out:
254         spin_unlock(&dcache_lock);
255         return ret;
256 }
257
258 int simple_unlink(struct inode *dir, struct dentry *dentry)
259 {
260         struct inode *inode = dentry->d_inode;
261
262         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
263         inode->i_nlink--;
264         dput(dentry);
265         return 0;
266 }
267
268 int simple_rmdir(struct inode *dir, struct dentry *dentry)
269 {
270         if (!simple_empty(dentry))
271                 return -ENOTEMPTY;
272
273         dentry->d_inode->i_nlink--;
274         simple_unlink(dir, dentry);
275         dir->i_nlink--;
276         return 0;
277 }
278
279 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
280                 struct inode *new_dir, struct dentry *new_dentry)
281 {
282         struct inode *inode = old_dentry->d_inode;
283         int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
284
285         if (!simple_empty(new_dentry))
286                 return -ENOTEMPTY;
287
288         if (new_dentry->d_inode) {
289                 simple_unlink(new_dir, new_dentry);
290                 if (they_are_dirs)
291                         old_dir->i_nlink--;
292         } else if (they_are_dirs) {
293                 old_dir->i_nlink--;
294                 new_dir->i_nlink++;
295         }
296
297         old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
298                 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
299
300         return 0;
301 }
302
303 int simple_readpage(struct file *file, struct page *page)
304 {
305         void *kaddr;
306
307         if (PageUptodate(page))
308                 goto out;
309
310         kaddr = kmap_atomic(page, KM_USER0);
311         memset(kaddr, 0, PAGE_CACHE_SIZE);
312         kunmap_atomic(kaddr, KM_USER0);
313         flush_dcache_page(page);
314         SetPageUptodate(page);
315 out:
316         unlock_page(page);
317         return 0;
318 }
319
320 int simple_prepare_write(struct file *file, struct page *page,
321                         unsigned from, unsigned to)
322 {
323         if (!PageUptodate(page)) {
324                 if (to - from != PAGE_CACHE_SIZE) {
325                         void *kaddr = kmap_atomic(page, KM_USER0);
326                         memset(kaddr, 0, from);
327                         memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
328                         flush_dcache_page(page);
329                         kunmap_atomic(kaddr, KM_USER0);
330                 }
331                 SetPageUptodate(page);
332         }
333         return 0;
334 }
335
336 int simple_commit_write(struct file *file, struct page *page,
337                         unsigned offset, unsigned to)
338 {
339         struct inode *inode = page->mapping->host;
340         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
341
342         /*
343          * No need to use i_size_read() here, the i_size
344          * cannot change under us because we hold the i_sem.
345          */
346         if (pos > inode->i_size)
347                 i_size_write(inode, pos);
348         set_page_dirty(page);
349         return 0;
350 }
351
352 int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
353 {
354         static struct super_operations s_ops = {.statfs = simple_statfs};
355         struct inode *inode;
356         struct dentry *root;
357         struct dentry *dentry;
358         int i;
359
360         s->s_blocksize = PAGE_CACHE_SIZE;
361         s->s_blocksize_bits = PAGE_CACHE_SHIFT;
362         s->s_magic = magic;
363         s->s_op = &s_ops;
364
365         inode = new_inode(s);
366         if (!inode)
367                 return -ENOMEM;
368         inode->i_mode = S_IFDIR | 0755;
369         inode->i_uid = inode->i_gid = 0;
370         inode->i_blksize = PAGE_CACHE_SIZE;
371         inode->i_blocks = 0;
372         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
373         inode->i_op = &simple_dir_inode_operations;
374         inode->i_fop = &simple_dir_operations;
375         root = d_alloc_root(inode);
376         if (!root) {
377                 iput(inode);
378                 return -ENOMEM;
379         }
380         for (i = 0; !files->name || files->name[0]; i++, files++) {
381                 struct qstr name;
382                 if (!files->name)
383                         continue;
384                 name.name = files->name;
385                 name.len = strlen(name.name);
386                 name.hash = full_name_hash(name.name, name.len);
387                 dentry = d_alloc(root, &name);
388                 if (!dentry)
389                         goto out;
390                 inode = new_inode(s);
391                 if (!inode)
392                         goto out;
393                 inode->i_mode = S_IFREG | files->mode;
394                 inode->i_uid = inode->i_gid = 0;
395                 inode->i_blksize = PAGE_CACHE_SIZE;
396                 inode->i_blocks = 0;
397                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
398                 inode->i_fop = files->ops;
399                 inode->i_ino = i;
400                 d_add(dentry, inode);
401         }
402         s->s_root = root;
403         return 0;
404 out:
405         d_genocide(root);
406         dput(root);
407         return -ENOMEM;
408 }
409
410 static spinlock_t pin_fs_lock = SPIN_LOCK_UNLOCKED;
411
412 int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
413 {
414         struct vfsmount *mnt = NULL;
415         spin_lock(&pin_fs_lock);
416         if (unlikely(!*mount)) {
417                 spin_unlock(&pin_fs_lock);
418                 mnt = do_kern_mount(name, 0, name, NULL);
419                 if (IS_ERR(mnt))
420                         return PTR_ERR(mnt);
421                 spin_lock(&pin_fs_lock);
422                 if (!*mount)
423                         *mount = mnt;
424         }
425         mntget(*mount);
426         ++*count;
427         spin_unlock(&pin_fs_lock);
428         mntput(mnt);
429         return 0;
430 }
431
432 void simple_release_fs(struct vfsmount **mount, int *count)
433 {
434         struct vfsmount *mnt;
435         spin_lock(&pin_fs_lock);
436         mnt = *mount;
437         if (!--*count)
438                 *mount = NULL;
439         spin_unlock(&pin_fs_lock);
440         mntput(mnt);
441 }
442
443 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
444                                 const void *from, size_t available)
445 {
446         loff_t pos = *ppos;
447         if (pos < 0)
448                 return -EINVAL;
449         if (pos >= available)
450                 return 0;
451         if (count > available - pos)
452                 count = available - pos;
453         if (copy_to_user(to, from + pos, count))
454                 return -EFAULT;
455         *ppos = pos + count;
456         return count;
457 }
458
459 EXPORT_SYMBOL(dcache_dir_close);
460 EXPORT_SYMBOL(dcache_dir_lseek);
461 EXPORT_SYMBOL(dcache_dir_open);
462 EXPORT_SYMBOL(dcache_readdir);
463 EXPORT_SYMBOL(generic_read_dir);
464 EXPORT_SYMBOL(simple_commit_write);
465 EXPORT_SYMBOL(simple_dir_inode_operations);
466 EXPORT_SYMBOL(simple_dir_operations);
467 EXPORT_SYMBOL(simple_empty);
468 EXPORT_SYMBOL(simple_fill_super);
469 EXPORT_SYMBOL(simple_getattr);
470 EXPORT_SYMBOL(simple_link);
471 EXPORT_SYMBOL(simple_lookup);
472 EXPORT_SYMBOL(simple_pin_fs);
473 EXPORT_SYMBOL(simple_prepare_write);
474 EXPORT_SYMBOL(simple_readpage);
475 EXPORT_SYMBOL(simple_release_fs);
476 EXPORT_SYMBOL(simple_rename);
477 EXPORT_SYMBOL(simple_rmdir);
478 EXPORT_SYMBOL(simple_statfs);
479 EXPORT_SYMBOL(simple_sync_file);
480 EXPORT_SYMBOL(simple_unlink);
481 EXPORT_SYMBOL(simple_read_from_buffer);