3 * Library for filesystems writers.
6 #include <linux/module.h>
7 #include <linux/pagemap.h>
8 #include <linux/mount.h>
11 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
14 struct inode *inode = dentry->d_inode;
15 generic_fillattr(inode, stat);
16 stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
20 int simple_statfs(struct super_block *sb, struct kstatfs *buf)
22 buf->f_type = sb->s_magic;
23 buf->f_bsize = PAGE_CACHE_SIZE;
24 buf->f_namelen = NAME_MAX;
29 * Lookup the data. This is trivial - if the dentry didn't already
30 * exist, we know it is negative.
33 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
35 if (dentry->d_name.len > NAME_MAX)
36 return ERR_PTR(-ENAMETOOLONG);
41 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
46 int dcache_dir_open(struct inode *inode, struct file *file)
48 static struct qstr cursor_name = {.len = 1, .name = "."};
50 file->private_data = d_alloc(file->f_dentry, &cursor_name);
52 return file->private_data ? 0 : -ENOMEM;
55 int dcache_dir_close(struct inode *inode, struct file *file)
57 dput(file->private_data);
61 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
63 down(&file->f_dentry->d_inode->i_sem);
66 offset += file->f_pos;
71 up(&file->f_dentry->d_inode->i_sem);
74 if (offset != file->f_pos) {
76 if (file->f_pos >= 2) {
78 struct dentry *cursor = file->private_data;
79 loff_t n = file->f_pos - 2;
81 spin_lock(&dcache_lock);
82 list_del(&cursor->d_child);
83 p = file->f_dentry->d_subdirs.next;
84 while (n && p != &file->f_dentry->d_subdirs) {
86 next = list_entry(p, struct dentry, d_child);
87 if (!d_unhashed(next) && next->d_inode)
91 list_add_tail(&cursor->d_child, p);
92 spin_unlock(&dcache_lock);
95 up(&file->f_dentry->d_inode->i_sem);
99 /* Relationship between i_mode and the DT_xxx types */
100 static inline unsigned char dt_type(struct inode *inode)
102 return (inode->i_mode >> 12) & 15;
106 * Directory is locked and all positive dentries in it are safe, since
107 * for ramfs-type trees they can't go away without unlink() or rmdir(),
108 * both impossible due to the lock on directory.
111 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
113 struct dentry *dentry = filp->f_dentry;
114 struct dentry *cursor = filp->private_data;
115 struct list_head *p, *q = &cursor->d_child;
121 ino = dentry->d_inode->i_ino;
122 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
128 ino = parent_ino(dentry);
129 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
135 spin_lock(&dcache_lock);
136 if (filp->f_pos == 2) {
138 list_add(q, &dentry->d_subdirs);
140 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
142 next = list_entry(p, struct dentry, d_child);
143 if (d_unhashed(next) || !next->d_inode)
146 spin_unlock(&dcache_lock);
147 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 spin_lock(&dcache_lock);
150 /* next is still alive */
156 spin_unlock(&dcache_lock);
161 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
166 struct file_operations simple_dir_operations = {
167 .open = dcache_dir_open,
168 .release = dcache_dir_close,
169 .llseek = dcache_dir_lseek,
170 .read = generic_read_dir,
171 .readdir = dcache_readdir,
174 struct inode_operations simple_dir_inode_operations = {
175 .lookup = simple_lookup,
179 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
180 * will never be mountable)
183 get_sb_pseudo(struct file_system_type *fs_type, char *name,
184 struct super_operations *ops, unsigned long magic)
186 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
187 static struct super_operations default_ops = {.statfs = simple_statfs};
188 struct dentry *dentry;
190 struct qstr d_name = {.name = name, .len = strlen(name)};
195 s->s_flags = MS_NOUSER;
196 s->s_maxbytes = ~0ULL;
197 s->s_blocksize = 1024;
198 s->s_blocksize_bits = 10;
200 s->s_op = ops ? ops : &default_ops;
204 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
205 root->i_uid = root->i_gid = 0;
206 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
207 dentry = d_alloc(NULL, &d_name);
213 dentry->d_parent = dentry;
214 d_instantiate(dentry, root);
216 s->s_flags |= MS_ACTIVE;
220 up_write(&s->s_umount);
222 return ERR_PTR(-ENOMEM);
225 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
227 struct inode *inode = old_dentry->d_inode;
229 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
231 atomic_inc(&inode->i_count);
233 d_instantiate(dentry, inode);
237 static inline int simple_positive(struct dentry *dentry)
239 return dentry->d_inode && !d_unhashed(dentry);
242 int simple_empty(struct dentry *dentry)
244 struct dentry *child;
247 spin_lock(&dcache_lock);
248 list_for_each_entry(child, &dentry->d_subdirs, d_child)
249 if (simple_positive(child))
253 spin_unlock(&dcache_lock);
257 int simple_unlink(struct inode *dir, struct dentry *dentry)
259 struct inode *inode = dentry->d_inode;
261 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
267 int simple_rmdir(struct inode *dir, struct dentry *dentry)
269 if (!simple_empty(dentry))
272 dentry->d_inode->i_nlink--;
273 simple_unlink(dir, dentry);
278 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
279 struct inode *new_dir, struct dentry *new_dentry)
281 struct inode *inode = old_dentry->d_inode;
282 int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
284 if (!simple_empty(new_dentry))
287 if (new_dentry->d_inode) {
288 simple_unlink(new_dir, new_dentry);
291 } else if (they_are_dirs) {
296 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
297 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
302 int simple_readpage(struct file *file, struct page *page)
306 if (PageUptodate(page))
309 kaddr = kmap_atomic(page, KM_USER0);
310 memset(kaddr, 0, PAGE_CACHE_SIZE);
311 kunmap_atomic(kaddr, KM_USER0);
312 flush_dcache_page(page);
313 SetPageUptodate(page);
319 int simple_prepare_write(struct file *file, struct page *page,
320 unsigned from, unsigned to)
322 if (!PageUptodate(page)) {
323 if (to - from != PAGE_CACHE_SIZE) {
324 void *kaddr = kmap_atomic(page, KM_USER0);
325 memset(kaddr, 0, from);
326 memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
327 flush_dcache_page(page);
328 kunmap_atomic(kaddr, KM_USER0);
330 SetPageUptodate(page);
335 int simple_commit_write(struct file *file, struct page *page,
336 unsigned offset, unsigned to)
338 struct inode *inode = page->mapping->host;
339 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
342 * No need to use i_size_read() here, the i_size
343 * cannot change under us because we hold the i_sem.
345 if (pos > inode->i_size)
346 i_size_write(inode, pos);
347 set_page_dirty(page);
351 int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
353 static struct super_operations s_ops = {.statfs = simple_statfs};
356 struct dentry *dentry;
359 s->s_blocksize = PAGE_CACHE_SIZE;
360 s->s_blocksize_bits = PAGE_CACHE_SHIFT;
364 inode = new_inode(s);
367 inode->i_mode = S_IFDIR | 0755;
368 inode->i_uid = inode->i_gid = 0;
369 inode->i_blksize = PAGE_CACHE_SIZE;
371 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
372 inode->i_op = &simple_dir_inode_operations;
373 inode->i_fop = &simple_dir_operations;
374 root = d_alloc_root(inode);
379 for (i = 0; !files->name || files->name[0]; i++, files++) {
383 name.name = files->name;
384 name.len = strlen(name.name);
385 name.hash = full_name_hash(name.name, name.len);
386 dentry = d_alloc(root, &name);
389 inode = new_inode(s);
392 inode->i_mode = S_IFREG | files->mode;
393 inode->i_uid = inode->i_gid = 0;
394 inode->i_blksize = PAGE_CACHE_SIZE;
396 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
397 inode->i_fop = files->ops;
399 d_add(dentry, inode);
409 static spinlock_t pin_fs_lock = SPIN_LOCK_UNLOCKED;
411 int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
413 struct vfsmount *mnt = NULL;
414 spin_lock(&pin_fs_lock);
415 if (unlikely(!*mount)) {
416 spin_unlock(&pin_fs_lock);
417 mnt = do_kern_mount(name, 0, name, NULL);
420 spin_lock(&pin_fs_lock);
426 spin_unlock(&pin_fs_lock);
431 void simple_release_fs(struct vfsmount **mount, int *count)
433 struct vfsmount *mnt;
434 spin_lock(&pin_fs_lock);
438 spin_unlock(&pin_fs_lock);
442 EXPORT_SYMBOL(dcache_dir_close);
443 EXPORT_SYMBOL(dcache_dir_lseek);
444 EXPORT_SYMBOL(dcache_dir_open);
445 EXPORT_SYMBOL(dcache_readdir);
446 EXPORT_SYMBOL(generic_read_dir);
447 EXPORT_SYMBOL(simple_commit_write);
448 EXPORT_SYMBOL(simple_dir_inode_operations);
449 EXPORT_SYMBOL(simple_dir_operations);
450 EXPORT_SYMBOL(simple_empty);
451 EXPORT_SYMBOL(simple_fill_super);
452 EXPORT_SYMBOL(simple_getattr);
453 EXPORT_SYMBOL(simple_link);
454 EXPORT_SYMBOL(simple_lookup);
455 EXPORT_SYMBOL(simple_pin_fs);
456 EXPORT_SYMBOL(simple_prepare_write);
457 EXPORT_SYMBOL(simple_readpage);
458 EXPORT_SYMBOL(simple_release_fs);
459 EXPORT_SYMBOL(simple_rename);
460 EXPORT_SYMBOL(simple_rmdir);
461 EXPORT_SYMBOL(simple_statfs);
462 EXPORT_SYMBOL(simple_sync_file);
463 EXPORT_SYMBOL(simple_unlink);