3 * Library for filesystems writers.
6 #include <linux/module.h>
7 #include <linux/pagemap.h>
8 #include <linux/mount.h>
10 #include <asm/uaccess.h>
12 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
15 struct inode *inode = dentry->d_inode;
16 generic_fillattr(inode, stat);
17 stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
21 int simple_statfs(struct super_block *sb, struct kstatfs *buf)
23 buf->f_type = sb->s_magic;
24 buf->f_bsize = PAGE_CACHE_SIZE;
25 buf->f_namelen = NAME_MAX;
30 * Lookup the data. This is trivial - if the dentry didn't already
31 * exist, we know it is negative.
34 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
36 if (dentry->d_name.len > NAME_MAX)
37 return ERR_PTR(-ENAMETOOLONG);
42 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
47 int dcache_dir_open(struct inode *inode, struct file *file)
49 static struct qstr cursor_name = {.len = 1, .name = "."};
51 file->private_data = d_alloc(file->f_dentry, &cursor_name);
53 return file->private_data ? 0 : -ENOMEM;
56 int dcache_dir_close(struct inode *inode, struct file *file)
58 dput(file->private_data);
62 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
64 down(&file->f_dentry->d_inode->i_sem);
67 offset += file->f_pos;
72 up(&file->f_dentry->d_inode->i_sem);
75 if (offset != file->f_pos) {
77 if (file->f_pos >= 2) {
79 struct dentry *cursor = file->private_data;
80 loff_t n = file->f_pos - 2;
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) {
87 next = list_entry(p, struct dentry, d_child);
88 if (!d_unhashed(next) && next->d_inode)
92 list_add_tail(&cursor->d_child, p);
93 spin_unlock(&dcache_lock);
96 up(&file->f_dentry->d_inode->i_sem);
100 /* Relationship between i_mode and the DT_xxx types */
101 static inline unsigned char dt_type(struct inode *inode)
103 return (inode->i_mode >> 12) & 15;
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.
112 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
114 struct dentry *dentry = filp->f_dentry;
115 struct dentry *cursor = filp->private_data;
116 struct list_head *p, *q = &cursor->d_child;
122 ino = dentry->d_inode->i_ino;
123 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
129 ino = parent_ino(dentry);
130 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
136 spin_lock(&dcache_lock);
137 if (filp->f_pos == 2) {
139 list_add(q, &dentry->d_subdirs);
141 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
143 next = list_entry(p, struct dentry, d_child);
144 if (d_unhashed(next) || !next->d_inode)
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)
150 spin_lock(&dcache_lock);
151 /* next is still alive */
157 spin_unlock(&dcache_lock);
162 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
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,
175 struct inode_operations simple_dir_inode_operations = {
176 .lookup = simple_lookup,
180 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
181 * will never be mountable)
184 get_sb_pseudo(struct file_system_type *fs_type, char *name,
185 struct super_operations *ops, unsigned long magic)
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;
191 struct qstr d_name = {.name = name, .len = strlen(name)};
196 s->s_flags = MS_NOUSER;
197 s->s_maxbytes = ~0ULL;
198 s->s_blocksize = 1024;
199 s->s_blocksize_bits = 10;
201 s->s_op = ops ? ops : &default_ops;
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);
214 dentry->d_parent = dentry;
215 d_instantiate(dentry, root);
217 s->s_flags |= MS_ACTIVE;
221 up_write(&s->s_umount);
223 return ERR_PTR(-ENOMEM);
226 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
228 struct inode *inode = old_dentry->d_inode;
230 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
232 atomic_inc(&inode->i_count);
234 d_instantiate(dentry, inode);
238 static inline int simple_positive(struct dentry *dentry)
240 return dentry->d_inode && !d_unhashed(dentry);
243 int simple_empty(struct dentry *dentry)
245 struct dentry *child;
248 spin_lock(&dcache_lock);
249 list_for_each_entry(child, &dentry->d_subdirs, d_child)
250 if (simple_positive(child))
254 spin_unlock(&dcache_lock);
258 int simple_unlink(struct inode *dir, struct dentry *dentry)
260 struct inode *inode = dentry->d_inode;
262 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
268 int simple_rmdir(struct inode *dir, struct dentry *dentry)
270 if (!simple_empty(dentry))
273 dentry->d_inode->i_nlink--;
274 simple_unlink(dir, dentry);
279 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
280 struct inode *new_dir, struct dentry *new_dentry)
282 struct inode *inode = old_dentry->d_inode;
283 int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
285 if (!simple_empty(new_dentry))
288 if (new_dentry->d_inode) {
289 simple_unlink(new_dir, new_dentry);
292 } else if (they_are_dirs) {
297 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
298 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
303 int simple_readpage(struct file *file, struct page *page)
307 if (PageUptodate(page))
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);
320 int simple_prepare_write(struct file *file, struct page *page,
321 unsigned from, unsigned to)
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);
331 SetPageUptodate(page);
336 int simple_commit_write(struct file *file, struct page *page,
337 unsigned offset, unsigned to)
339 struct inode *inode = page->mapping->host;
340 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
343 * No need to use i_size_read() here, the i_size
344 * cannot change under us because we hold the i_sem.
346 if (pos > inode->i_size)
347 i_size_write(inode, pos);
348 set_page_dirty(page);
352 int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
354 static struct super_operations s_ops = {.statfs = simple_statfs};
357 struct dentry *dentry;
360 s->s_blocksize = PAGE_CACHE_SIZE;
361 s->s_blocksize_bits = PAGE_CACHE_SHIFT;
365 inode = new_inode(s);
368 inode->i_mode = S_IFDIR | 0755;
369 inode->i_uid = inode->i_gid = 0;
370 inode->i_blksize = PAGE_CACHE_SIZE;
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);
380 for (i = 0; !files->name || files->name[0]; i++, files++) {
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);
390 inode = new_inode(s);
393 inode->i_mode = S_IFREG | files->mode;
394 inode->i_uid = inode->i_gid = 0;
395 inode->i_blksize = PAGE_CACHE_SIZE;
397 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
398 inode->i_fop = files->ops;
400 d_add(dentry, inode);
410 static spinlock_t pin_fs_lock = SPIN_LOCK_UNLOCKED;
412 int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
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);
421 spin_lock(&pin_fs_lock);
427 spin_unlock(&pin_fs_lock);
432 void simple_release_fs(struct vfsmount **mount, int *count)
434 struct vfsmount *mnt;
435 spin_lock(&pin_fs_lock);
439 spin_unlock(&pin_fs_lock);
443 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
444 const void *from, size_t available)
449 if (pos >= available)
451 if (count > available - pos)
452 count = available - pos;
453 if (copy_to_user(to, from + pos, count))
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);