3 * Library for filesystems writers.
6 #include <linux/module.h>
7 #include <linux/pagemap.h>
8 #include <linux/mount.h>
10 #include <linux/mutex.h>
12 #include <asm/uaccess.h>
14 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
17 struct inode *inode = dentry->d_inode;
18 generic_fillattr(inode, stat);
19 stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
23 int simple_statfs(struct super_block *sb, struct kstatfs *buf)
25 buf->f_type = sb->s_magic;
26 buf->f_bsize = PAGE_CACHE_SIZE;
27 buf->f_namelen = NAME_MAX;
32 * Retaining negative dentries for an in-memory filesystem just wastes
33 * memory and lookup time: arrange for them to be deleted immediately.
35 static int simple_delete_dentry(struct dentry *dentry)
41 * Lookup the data. This is trivial - if the dentry didn't already
42 * exist, we know it is negative. Set d_op to delete negative dentries.
44 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
46 static struct dentry_operations simple_dentry_operations = {
47 .d_delete = simple_delete_dentry,
50 if (dentry->d_name.len > NAME_MAX)
51 return ERR_PTR(-ENAMETOOLONG);
52 dentry->d_op = &simple_dentry_operations;
57 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
62 int dcache_dir_open(struct inode *inode, struct file *file)
64 static struct qstr cursor_name = {.len = 1, .name = "."};
66 file->private_data = d_alloc(file->f_dentry, &cursor_name);
68 return file->private_data ? 0 : -ENOMEM;
71 int dcache_dir_close(struct inode *inode, struct file *file)
73 dput(file->private_data);
77 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
79 mutex_lock(&file->f_dentry->d_inode->i_mutex);
82 offset += file->f_pos;
87 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
90 if (offset != file->f_pos) {
92 if (file->f_pos >= 2) {
94 struct dentry *cursor = file->private_data;
95 loff_t n = file->f_pos - 2;
97 spin_lock(&dcache_lock);
98 list_del(&cursor->d_u.d_child);
99 p = file->f_dentry->d_subdirs.next;
100 while (n && p != &file->f_dentry->d_subdirs) {
102 next = list_entry(p, struct dentry, d_u.d_child);
103 if (!d_unhashed(next) && next->d_inode)
107 list_add_tail(&cursor->d_u.d_child, p);
108 spin_unlock(&dcache_lock);
111 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
115 /* Relationship between i_mode and the DT_xxx types */
116 static inline unsigned char dt_type(struct inode *inode)
118 return (inode->i_mode >> 12) & 15;
122 * Directory is locked and all positive dentries in it are safe, since
123 * for ramfs-type trees they can't go away without unlink() or rmdir(),
124 * both impossible due to the lock on directory.
127 static inline int do_dcache_readdir_filter(struct file * filp,
128 void * dirent, filldir_t filldir, int (*filter)(struct dentry *dentry))
130 struct dentry *dentry = filp->f_dentry;
131 struct dentry *cursor = filp->private_data;
132 struct list_head *p, *q = &cursor->d_u.d_child;
138 ino = dentry->d_inode->i_ino;
139 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
145 ino = parent_ino(dentry);
146 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
152 spin_lock(&dcache_lock);
153 if (filp->f_pos == 2) {
155 list_add(q, &dentry->d_subdirs);
157 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
159 next = list_entry(p, struct dentry, d_u.d_child);
160 if (d_unhashed(next) || !next->d_inode)
162 if (filter && !filter(next))
165 spin_unlock(&dcache_lock);
166 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)
168 spin_lock(&dcache_lock);
169 /* next is still alive */
175 spin_unlock(&dcache_lock);
180 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
182 return do_dcache_readdir_filter(filp, dirent, filldir, NULL);
185 int dcache_readdir_filter(struct file * filp, void * dirent, filldir_t filldir,
186 int (*filter)(struct dentry *))
188 return do_dcache_readdir_filter(filp, dirent, filldir, filter);
192 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
197 const struct file_operations simple_dir_operations = {
198 .open = dcache_dir_open,
199 .release = dcache_dir_close,
200 .llseek = dcache_dir_lseek,
201 .read = generic_read_dir,
202 .readdir = dcache_readdir,
203 .fsync = simple_sync_file,
206 struct inode_operations simple_dir_inode_operations = {
207 .lookup = simple_lookup,
211 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
212 * will never be mountable)
215 get_sb_pseudo(struct file_system_type *fs_type, char *name,
216 struct super_operations *ops, unsigned long magic)
218 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
219 static struct super_operations default_ops = {.statfs = simple_statfs};
220 struct dentry *dentry;
222 struct qstr d_name = {.name = name, .len = strlen(name)};
227 s->s_flags = MS_NOUSER;
228 s->s_maxbytes = ~0ULL;
229 s->s_blocksize = 1024;
230 s->s_blocksize_bits = 10;
232 s->s_op = ops ? ops : &default_ops;
237 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
238 root->i_uid = root->i_gid = 0;
239 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
240 dentry = d_alloc(NULL, &d_name);
246 dentry->d_parent = dentry;
247 d_instantiate(dentry, root);
249 s->s_flags |= MS_ACTIVE;
253 up_write(&s->s_umount);
255 return ERR_PTR(-ENOMEM);
258 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
260 struct inode *inode = old_dentry->d_inode;
262 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
264 atomic_inc(&inode->i_count);
266 d_instantiate(dentry, inode);
270 static inline int simple_positive(struct dentry *dentry)
272 return dentry->d_inode && !d_unhashed(dentry);
275 int simple_empty(struct dentry *dentry)
277 struct dentry *child;
280 spin_lock(&dcache_lock);
281 list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child)
282 if (simple_positive(child))
286 spin_unlock(&dcache_lock);
290 int simple_unlink(struct inode *dir, struct dentry *dentry)
292 struct inode *inode = dentry->d_inode;
294 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
300 int simple_rmdir(struct inode *dir, struct dentry *dentry)
302 if (!simple_empty(dentry))
305 dentry->d_inode->i_nlink--;
306 simple_unlink(dir, dentry);
311 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
312 struct inode *new_dir, struct dentry *new_dentry)
314 struct inode *inode = old_dentry->d_inode;
315 int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
317 if (!simple_empty(new_dentry))
320 if (new_dentry->d_inode) {
321 simple_unlink(new_dir, new_dentry);
324 } else if (they_are_dirs) {
329 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
330 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
335 int simple_readpage(struct file *file, struct page *page)
339 if (PageUptodate(page))
342 kaddr = kmap_atomic(page, KM_USER0);
343 memset(kaddr, 0, PAGE_CACHE_SIZE);
344 kunmap_atomic(kaddr, KM_USER0);
345 flush_dcache_page(page);
346 SetPageUptodate(page);
352 int simple_prepare_write(struct file *file, struct page *page,
353 unsigned from, unsigned to)
355 if (!PageUptodate(page)) {
356 if (to - from != PAGE_CACHE_SIZE) {
357 void *kaddr = kmap_atomic(page, KM_USER0);
358 memset(kaddr, 0, from);
359 memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
360 flush_dcache_page(page);
361 kunmap_atomic(kaddr, KM_USER0);
363 SetPageUptodate(page);
368 int simple_commit_write(struct file *file, struct page *page,
369 unsigned offset, unsigned to)
371 struct inode *inode = page->mapping->host;
372 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
375 * No need to use i_size_read() here, the i_size
376 * cannot change under us because we hold the i_mutex.
378 if (pos > inode->i_size)
379 i_size_write(inode, pos);
380 set_page_dirty(page);
384 int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
386 static struct super_operations s_ops = {.statfs = simple_statfs};
389 struct dentry *dentry;
392 s->s_blocksize = PAGE_CACHE_SIZE;
393 s->s_blocksize_bits = PAGE_CACHE_SHIFT;
398 inode = new_inode(s);
401 inode->i_mode = S_IFDIR | 0755;
402 inode->i_uid = inode->i_gid = 0;
403 inode->i_blksize = PAGE_CACHE_SIZE;
405 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
406 inode->i_op = &simple_dir_inode_operations;
407 inode->i_fop = &simple_dir_operations;
409 root = d_alloc_root(inode);
414 for (i = 0; !files->name || files->name[0]; i++, files++) {
417 dentry = d_alloc_name(root, files->name);
420 inode = new_inode(s);
423 inode->i_mode = S_IFREG | files->mode;
424 inode->i_uid = inode->i_gid = 0;
425 inode->i_blksize = PAGE_CACHE_SIZE;
427 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
428 inode->i_fop = files->ops;
430 d_add(dentry, inode);
440 static DEFINE_SPINLOCK(pin_fs_lock);
442 int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
444 struct vfsmount *mnt = NULL;
445 spin_lock(&pin_fs_lock);
446 if (unlikely(!*mount)) {
447 spin_unlock(&pin_fs_lock);
448 mnt = do_kern_mount(name, 0, name, NULL);
451 spin_lock(&pin_fs_lock);
457 spin_unlock(&pin_fs_lock);
462 void simple_release_fs(struct vfsmount **mount, int *count)
464 struct vfsmount *mnt;
465 spin_lock(&pin_fs_lock);
469 spin_unlock(&pin_fs_lock);
473 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
474 const void *from, size_t available)
479 if (pos >= available)
481 if (count > available - pos)
482 count = available - pos;
483 if (copy_to_user(to, from + pos, count))
490 * Transaction based IO.
491 * The file expects a single write which triggers the transaction, and then
492 * possibly a read which collects the result - which is stored in a
495 char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
497 struct simple_transaction_argresp *ar;
498 static DEFINE_SPINLOCK(simple_transaction_lock);
500 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
501 return ERR_PTR(-EFBIG);
503 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
505 return ERR_PTR(-ENOMEM);
507 spin_lock(&simple_transaction_lock);
509 /* only one write allowed per open */
510 if (file->private_data) {
511 spin_unlock(&simple_transaction_lock);
512 free_page((unsigned long)ar);
513 return ERR_PTR(-EBUSY);
516 file->private_data = ar;
518 spin_unlock(&simple_transaction_lock);
520 if (copy_from_user(ar->data, buf, size))
521 return ERR_PTR(-EFAULT);
526 ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
528 struct simple_transaction_argresp *ar = file->private_data;
532 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
535 int simple_transaction_release(struct inode *inode, struct file *file)
537 free_page((unsigned long)file->private_data);
541 /* Simple attribute files */
545 void (*set)(void *, u64);
546 char get_buf[24]; /* enough to store a u64 and "\n\0" */
549 const char *fmt; /* format for read operation */
550 struct mutex mutex; /* protects access to these buffers */
553 /* simple_attr_open is called by an actual attribute open file operation
554 * to set the attribute specific access operations. */
555 int simple_attr_open(struct inode *inode, struct file *file,
556 u64 (*get)(void *), void (*set)(void *, u64),
559 struct simple_attr *attr;
561 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
567 attr->data = inode->u.generic_ip;
569 mutex_init(&attr->mutex);
571 file->private_data = attr;
573 return nonseekable_open(inode, file);
576 int simple_attr_close(struct inode *inode, struct file *file)
578 kfree(file->private_data);
582 /* read from the buffer that is filled with the get function */
583 ssize_t simple_attr_read(struct file *file, char __user *buf,
584 size_t len, loff_t *ppos)
586 struct simple_attr *attr;
590 attr = file->private_data;
595 mutex_lock(&attr->mutex);
596 if (*ppos) /* continued read */
597 size = strlen(attr->get_buf);
598 else /* first read */
599 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
601 (unsigned long long)attr->get(attr->data));
603 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
604 mutex_unlock(&attr->mutex);
608 /* interpret the buffer as a number to call the set function with */
609 ssize_t simple_attr_write(struct file *file, const char __user *buf,
610 size_t len, loff_t *ppos)
612 struct simple_attr *attr;
617 attr = file->private_data;
622 mutex_lock(&attr->mutex);
624 size = min(sizeof(attr->set_buf) - 1, len);
625 if (copy_from_user(attr->set_buf, buf, size))
628 ret = len; /* claim we got the whole input */
629 attr->set_buf[size] = '\0';
630 val = simple_strtol(attr->set_buf, NULL, 0);
631 attr->set(attr->data, val);
633 mutex_unlock(&attr->mutex);
637 EXPORT_SYMBOL(dcache_dir_close);
638 EXPORT_SYMBOL(dcache_dir_lseek);
639 EXPORT_SYMBOL(dcache_dir_open);
640 EXPORT_SYMBOL(dcache_readdir);
641 EXPORT_SYMBOL(dcache_readdir_filter);
642 EXPORT_SYMBOL(generic_read_dir);
643 EXPORT_SYMBOL(get_sb_pseudo);
644 EXPORT_SYMBOL(simple_commit_write);
645 EXPORT_SYMBOL(simple_dir_inode_operations);
646 EXPORT_SYMBOL(simple_dir_operations);
647 EXPORT_SYMBOL(simple_empty);
648 EXPORT_SYMBOL(d_alloc_name);
649 EXPORT_SYMBOL(simple_fill_super);
650 EXPORT_SYMBOL(simple_getattr);
651 EXPORT_SYMBOL(simple_link);
652 EXPORT_SYMBOL(simple_lookup);
653 EXPORT_SYMBOL(simple_pin_fs);
654 EXPORT_SYMBOL(simple_prepare_write);
655 EXPORT_SYMBOL(simple_readpage);
656 EXPORT_SYMBOL(simple_release_fs);
657 EXPORT_SYMBOL(simple_rename);
658 EXPORT_SYMBOL(simple_rmdir);
659 EXPORT_SYMBOL(simple_statfs);
660 EXPORT_SYMBOL(simple_sync_file);
661 EXPORT_SYMBOL(simple_unlink);
662 EXPORT_SYMBOL(simple_read_from_buffer);
663 EXPORT_SYMBOL(simple_transaction_get);
664 EXPORT_SYMBOL(simple_transaction_read);
665 EXPORT_SYMBOL(simple_transaction_release);
666 EXPORT_SYMBOL_GPL(simple_attr_open);
667 EXPORT_SYMBOL_GPL(simple_attr_close);
668 EXPORT_SYMBOL_GPL(simple_attr_read);
669 EXPORT_SYMBOL_GPL(simple_attr_write);