blob: 102edfd39000c15f14594a47b6236ba8edb5ccc8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/libfs.c
3 * Library for filesystems writers.
4 */
5
Fabian Frederickac13a822014-06-04 16:06:27 -07006#include <linux/blkdev.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mount.h>
11#include <linux/vfs.h>
npiggin@suse.de7bb46a62010-05-27 01:05:33 +100012#include <linux/quotaops.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080013#include <linux/mutex.h>
Al Viro87dc8002013-09-16 10:30:04 -040014#include <linux/namei.h>
Christoph Hellwig25961102007-10-21 16:42:05 -070015#include <linux/exportfs.h>
Al Virod5aacad2009-06-07 14:56:44 -040016#include <linux/writeback.h>
Al Viroff01bb42011-09-16 02:31:11 -040017#include <linux/buffer_head.h> /* sync_mapping_buffers */
Ingo Molnar7cf34c72006-03-23 03:00:36 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20
Al Viroa4464db2011-07-07 15:03:58 -040021#include "internal.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
24 struct kstat *stat)
25{
David Howellsdea655c2015-03-17 22:26:15 +000026 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 generic_fillattr(inode, stat);
28 stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
29 return 0;
30}
Al Viro12f38872013-09-15 21:20:49 -040031EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Howells726c3342006-06-23 02:02:58 -070033int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
David Howells726c3342006-06-23 02:02:58 -070035 buf->f_type = dentry->d_sb->s_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 buf->f_bsize = PAGE_CACHE_SIZE;
37 buf->f_namelen = NAME_MAX;
38 return 0;
39}
Al Viro12f38872013-09-15 21:20:49 -040040EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Retaining negative dentries for an in-memory filesystem just wastes
44 * memory and lookup time: arrange for them to be deleted immediately.
45 */
Al Virob26d4cd2013-10-25 18:47:37 -040046int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 return 1;
49}
Al Virob26d4cd2013-10-25 18:47:37 -040050EXPORT_SYMBOL(always_delete_dentry);
51
52const struct dentry_operations simple_dentry_operations = {
53 .d_delete = always_delete_dentry,
54};
55EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * Lookup the data. This is trivial - if the dentry didn't already
59 * exist, we know it is negative. Set d_op to delete negative dentries.
60 */
Al Viro00cd8dd2012-06-10 17:13:09 -040061struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (dentry->d_name.len > NAME_MAX)
64 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040065 if (!dentry->d_sb->s_d_op)
66 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 d_add(dentry, NULL);
68 return NULL;
69}
Al Viro12f38872013-09-15 21:20:49 -040070EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072int dcache_dir_open(struct inode *inode, struct file *file)
73{
Linus Torvalds26fe5752012-05-10 13:14:12 -070074 static struct qstr cursor_name = QSTR_INIT(".", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -080076 file->private_data = d_alloc(file->f_path.dentry, &cursor_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return file->private_data ? 0 : -ENOMEM;
79}
Al Viro12f38872013-09-15 21:20:49 -040080EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82int dcache_dir_close(struct inode *inode, struct file *file)
83{
84 dput(file->private_data);
85 return 0;
86}
Al Viro12f38872013-09-15 21:20:49 -040087EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Andrew Morton965c8e52012-12-17 15:59:39 -080089loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110091 struct dentry *dentry = file->f_path.dentry;
David Howellsdea655c2015-03-17 22:26:15 +000092 mutex_lock(&d_inode(dentry)->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -080093 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 case 1:
95 offset += file->f_pos;
96 case 0:
97 if (offset >= 0)
98 break;
99 default:
David Howellsdea655c2015-03-17 22:26:15 +0000100 mutex_unlock(&d_inode(dentry)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -EINVAL;
102 }
103 if (offset != file->f_pos) {
104 file->f_pos = offset;
105 if (file->f_pos >= 2) {
106 struct list_head *p;
107 struct dentry *cursor = file->private_data;
108 loff_t n = file->f_pos - 2;
109
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100110 spin_lock(&dentry->d_lock);
111 /* d_lock not required for cursor */
Al Viro946e51f2014-10-26 19:19:16 -0400112 list_del(&cursor->d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100113 p = dentry->d_subdirs.next;
114 while (n && p != &dentry->d_subdirs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct dentry *next;
Al Viro946e51f2014-10-26 19:19:16 -0400116 next = list_entry(p, struct dentry, d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100117 spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
Nick Pigginda502952011-01-07 17:49:33 +1100118 if (simple_positive(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 n--;
Nick Pigginda502952011-01-07 17:49:33 +1100120 spin_unlock(&next->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 p = p->next;
122 }
Al Viro946e51f2014-10-26 19:19:16 -0400123 list_add_tail(&cursor->d_child, p);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100124 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
David Howellsdea655c2015-03-17 22:26:15 +0000127 mutex_unlock(&d_inode(dentry)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return offset;
129}
Al Viro12f38872013-09-15 21:20:49 -0400130EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/* Relationship between i_mode and the DT_xxx types */
133static inline unsigned char dt_type(struct inode *inode)
134{
135 return (inode->i_mode >> 12) & 15;
136}
137
138/*
139 * Directory is locked and all positive dentries in it are safe, since
140 * for ramfs-type trees they can't go away without unlink() or rmdir(),
141 * both impossible due to the lock on directory.
142 */
143
Al Viro5f99f4e2013-05-15 20:23:06 -0400144int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Al Viro5f99f4e2013-05-15 20:23:06 -0400146 struct dentry *dentry = file->f_path.dentry;
147 struct dentry *cursor = file->private_data;
Al Viro946e51f2014-10-26 19:19:16 -0400148 struct list_head *p, *q = &cursor->d_child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Al Viro5f99f4e2013-05-15 20:23:06 -0400150 if (!dir_emit_dots(file, ctx))
151 return 0;
152 spin_lock(&dentry->d_lock);
153 if (ctx->pos == 2)
154 list_move(q, &dentry->d_subdirs);
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700155
Al Viro5f99f4e2013-05-15 20:23:06 -0400156 for (p = q->next; p != &dentry->d_subdirs; p = p->next) {
Al Viro946e51f2014-10-26 19:19:16 -0400157 struct dentry *next = list_entry(p, struct dentry, d_child);
Al Viro5f99f4e2013-05-15 20:23:06 -0400158 spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
159 if (!simple_positive(next)) {
160 spin_unlock(&next->d_lock);
161 continue;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Al Viro5f99f4e2013-05-15 20:23:06 -0400164 spin_unlock(&next->d_lock);
165 spin_unlock(&dentry->d_lock);
166 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000167 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro5f99f4e2013-05-15 20:23:06 -0400168 return 0;
169 spin_lock(&dentry->d_lock);
170 spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
171 /* next is still alive */
172 list_move(q, p);
173 spin_unlock(&next->d_lock);
174 p = q;
175 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Al Viro5f99f4e2013-05-15 20:23:06 -0400177 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
179}
Al Viro12f38872013-09-15 21:20:49 -0400180EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
183{
184 return -EISDIR;
185}
Al Viro12f38872013-09-15 21:20:49 -0400186EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800188const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .open = dcache_dir_open,
190 .release = dcache_dir_close,
191 .llseek = dcache_dir_lseek,
192 .read = generic_read_dir,
Al Viro5f99f4e2013-05-15 20:23:06 -0400193 .iterate = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200194 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
Al Viro12f38872013-09-15 21:20:49 -0400196EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800198const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .lookup = simple_lookup,
200};
Al Viro12f38872013-09-15 21:20:49 -0400201EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Hugh Dickins759b9772007-03-05 00:30:28 -0800203static const struct super_operations simple_super_operations = {
204 .statfs = simple_statfs,
205};
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
209 * will never be mountable)
210 */
Al Viro51139ad2010-07-25 23:47:46 +0400211struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
Al Viroc74a1cb2011-01-12 16:59:34 -0500212 const struct super_operations *ops,
213 const struct dentry_operations *dops, unsigned long magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
David Howells9249e172012-06-25 12:55:37 +0100215 struct super_block *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct dentry *dentry;
217 struct inode *root;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700218 struct qstr d_name = QSTR_INIT(name, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
David Howells9249e172012-06-25 12:55:37 +0100220 s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (IS_ERR(s))
Al Viro51139ad2010-07-25 23:47:46 +0400222 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jeff Layton89a4eb42009-08-18 14:11:08 -0700224 s->s_maxbytes = MAX_LFS_FILESIZE;
Alex Nixon3971e1a2008-07-29 22:33:03 -0700225 s->s_blocksize = PAGE_SIZE;
226 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800228 s->s_op = ops ? ops : &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 s->s_time_gran = 1;
230 root = new_inode(s);
231 if (!root)
232 goto Enomem;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700233 /*
234 * since this is the first inode, make it number 1. New inodes created
235 * after this must take care not to collide with it (by passing
236 * max_reserved of 1 to iunique).
237 */
238 root->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
Al Viroa4464db2011-07-07 15:03:58 -0400241 dentry = __d_alloc(s, &d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!dentry) {
243 iput(root);
244 goto Enomem;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 d_instantiate(dentry, root);
247 s->s_root = dentry;
Al Viroc74a1cb2011-01-12 16:59:34 -0500248 s->s_d_op = dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 s->s_flags |= MS_ACTIVE;
Al Viro51139ad2010-07-25 23:47:46 +0400250 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252Enomem:
Al Viro6f5bbff2009-05-06 01:34:22 -0400253 deactivate_locked_super(s);
Al Viro51139ad2010-07-25 23:47:46 +0400254 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
Al Viro12f38872013-09-15 21:20:49 -0400256EXPORT_SYMBOL(mount_pseudo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Stephen Boyd20955e82012-04-05 14:25:09 -0700258int simple_open(struct inode *inode, struct file *file)
259{
260 if (inode->i_private)
261 file->private_data = inode->i_private;
262 return 0;
263}
Al Viro12f38872013-09-15 21:20:49 -0400264EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
267{
David Howellsdea655c2015-03-17 22:26:15 +0000268 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -0700271 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400272 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dget(dentry);
274 d_instantiate(dentry, inode);
275 return 0;
276}
Al Viro12f38872013-09-15 21:20:49 -0400277EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279int simple_empty(struct dentry *dentry)
280{
281 struct dentry *child;
282 int ret = 0;
283
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100284 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400285 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100286 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
287 if (simple_positive(child)) {
288 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100290 }
291 spin_unlock(&child->d_lock);
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ret = 1;
294out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100295 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return ret;
297}
Al Viro12f38872013-09-15 21:20:49 -0400298EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300int simple_unlink(struct inode *dir, struct dentry *dentry)
301{
David Howellsdea655c2015-03-17 22:26:15 +0000302 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700305 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 dput(dentry);
307 return 0;
308}
Al Viro12f38872013-09-15 21:20:49 -0400309EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311int simple_rmdir(struct inode *dir, struct dentry *dentry)
312{
313 if (!simple_empty(dentry))
314 return -ENOTEMPTY;
315
David Howellsdea655c2015-03-17 22:26:15 +0000316 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700318 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
320}
Al Viro12f38872013-09-15 21:20:49 -0400321EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
324 struct inode *new_dir, struct dentry *new_dentry)
325{
David Howellsdea655c2015-03-17 22:26:15 +0000326 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000327 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (!simple_empty(new_dentry))
330 return -ENOTEMPTY;
331
David Howellsdea655c2015-03-17 22:26:15 +0000332 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400334 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000335 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700336 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700339 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700340 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
344 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
345
346 return 0;
347}
Al Viro12f38872013-09-15 21:20:49 -0400348EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000350/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200351 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000352 * @dentry: dentry
353 * @iattr: iattr structure
354 *
355 * Returns 0 on success, -error on failure.
356 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200357 * simple_setattr is a simple ->setattr implementation without a proper
358 * implementation of size changes.
359 *
360 * It can either be used for in-memory filesystems or special files
361 * on simple regular filesystems. Anything that needs to change on-disk
362 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000363 */
364int simple_setattr(struct dentry *dentry, struct iattr *iattr)
365{
David Howellsdea655c2015-03-17 22:26:15 +0000366 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000367 int error;
368
369 error = inode_change_ok(inode, iattr);
370 if (error)
371 return error;
372
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200373 if (iattr->ia_valid & ATTR_SIZE)
374 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200375 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200376 mark_inode_dirty(inode);
377 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000378}
379EXPORT_SYMBOL(simple_setattr);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381int simple_readpage(struct file *file, struct page *page)
382{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700383 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 flush_dcache_page(page);
385 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unlock_page(page);
387 return 0;
388}
Al Viro12f38872013-09-15 21:20:49 -0400389EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Nick Pigginafddba42007-10-16 01:25:01 -0700391int simple_write_begin(struct file *file, struct address_space *mapping,
392 loff_t pos, unsigned len, unsigned flags,
393 struct page **pagep, void **fsdata)
394{
395 struct page *page;
396 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700397
398 index = pos >> PAGE_CACHE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700399
Nick Piggin54566b22009-01-04 12:00:53 -0800400 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700401 if (!page)
402 return -ENOMEM;
403
404 *pagep = page;
405
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200406 if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
407 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
408
409 zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);
410 }
411 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700412}
Al Viro12f38872013-09-15 21:20:49 -0400413EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700414
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200415/**
416 * simple_write_end - .write_end helper for non-block-device FSes
417 * @available: See .write_end of address_space_operations
418 * @file: "
419 * @mapping: "
420 * @pos: "
421 * @len: "
422 * @copied: "
423 * @page: "
424 * @fsdata: "
425 *
426 * simple_write_end does the minimum needed for updating a page after writing is
427 * done. It has the same API signature as the .write_end of
428 * address_space_operations vector. So it can just be set onto .write_end for
429 * FSes that don't need any other processing. i_mutex is assumed to be held.
430 * Block based filesystems should use generic_write_end().
431 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
432 * is not called, so a filesystem that actually does store data in .write_inode
433 * should extend on what's done here with a call to mark_inode_dirty() in the
434 * case that i_size has changed.
435 */
436int simple_write_end(struct file *file, struct address_space *mapping,
437 loff_t pos, unsigned len, unsigned copied,
438 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200441 loff_t last_pos = pos + copied;
442
443 /* zero the stale part of the page if we did a short copy */
444 if (copied < len) {
445 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
446
447 zero_user(page, from + copied, len - copied);
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Nick Piggin955eff52007-02-20 13:58:08 -0800450 if (!PageUptodate(page))
451 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800454 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200456 if (last_pos > inode->i_size)
457 i_size_write(inode, last_pos);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700460 unlock_page(page);
461 page_cache_release(page);
462
463 return copied;
464}
Al Viro12f38872013-09-15 21:20:49 -0400465EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700466
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700467/*
468 * the inodes created here are not hashed. If you use iunique to generate
469 * unique inode values later for this filesystem, then you must take care
470 * to pass it an appropriate max_reserved value to avoid collisions.
471 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200472int simple_fill_super(struct super_block *s, unsigned long magic,
473 struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct inode *inode;
476 struct dentry *root;
477 struct dentry *dentry;
478 int i;
479
480 s->s_blocksize = PAGE_CACHE_SIZE;
481 s->s_blocksize_bits = PAGE_CACHE_SHIFT;
482 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800483 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 s->s_time_gran = 1;
485
486 inode = new_inode(s);
487 if (!inode)
488 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700489 /*
490 * because the root inode is 1, the files array must not contain an
491 * entry at index 1
492 */
493 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 inode->i_mode = S_IFDIR | 0755;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
496 inode->i_op = &simple_dir_inode_operations;
497 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200498 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500499 root = d_make_root(inode);
500 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 for (i = 0; !files->name || files->name[0]; i++, files++) {
503 if (!files->name)
504 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700505
506 /* warn if it tries to conflict with the root inode */
507 if (unlikely(i == 1))
508 printk(KERN_WARNING "%s: %s passed in a files array"
509 "with an index of 1!\n", __func__,
510 s->s_type->name);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dentry = d_alloc_name(root, files->name);
513 if (!dentry)
514 goto out;
515 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300516 if (!inode) {
517 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 inode->i_mode = S_IFREG | files->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
522 inode->i_fop = files->ops;
523 inode->i_ino = i;
524 d_add(dentry, inode);
525 }
526 s->s_root = root;
527 return 0;
528out:
529 d_genocide(root);
Al Viro640946f2012-04-02 19:22:25 -0400530 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dput(root);
532 return -ENOMEM;
533}
Al Viro12f38872013-09-15 21:20:49 -0400534EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536static DEFINE_SPINLOCK(pin_fs_lock);
537
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400538int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct vfsmount *mnt = NULL;
541 spin_lock(&pin_fs_lock);
542 if (unlikely(!*mount)) {
543 spin_unlock(&pin_fs_lock);
Al Viro24529922012-03-17 02:13:52 -0400544 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (IS_ERR(mnt))
546 return PTR_ERR(mnt);
547 spin_lock(&pin_fs_lock);
548 if (!*mount)
549 *mount = mnt;
550 }
551 mntget(*mount);
552 ++*count;
553 spin_unlock(&pin_fs_lock);
554 mntput(mnt);
555 return 0;
556}
Al Viro12f38872013-09-15 21:20:49 -0400557EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559void simple_release_fs(struct vfsmount **mount, int *count)
560{
561 struct vfsmount *mnt;
562 spin_lock(&pin_fs_lock);
563 mnt = *mount;
564 if (!--*count)
565 *mount = NULL;
566 spin_unlock(&pin_fs_lock);
567 mntput(mnt);
568}
Al Viro12f38872013-09-15 21:20:49 -0400569EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700571/**
572 * simple_read_from_buffer - copy data from the buffer to user space
573 * @to: the user space buffer to read to
574 * @count: the maximum number of bytes to read
575 * @ppos: the current position in the buffer
576 * @from: the buffer to read from
577 * @available: the size of the buffer
578 *
579 * The simple_read_from_buffer() function reads up to @count bytes from the
580 * buffer @from at offset @ppos into the user space address starting at @to.
581 *
582 * On success, the number of bytes read is returned and the offset @ppos is
583 * advanced by this number, or negative value is returned on error.
584 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
586 const void *from, size_t available)
587{
588 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700589 size_t ret;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (pos < 0)
592 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700593 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595 if (count > available - pos)
596 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700597 ret = copy_to_user(to, from + pos, count);
598 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700600 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *ppos = pos + count;
602 return count;
603}
Al Viro12f38872013-09-15 21:20:49 -0400604EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700606/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200607 * simple_write_to_buffer - copy data from user space to the buffer
608 * @to: the buffer to write to
609 * @available: the size of the buffer
610 * @ppos: the current position in the buffer
611 * @from: the user space buffer to read from
612 * @count: the maximum number of bytes to read
613 *
614 * The simple_write_to_buffer() function reads up to @count bytes from the user
615 * space address starting at @from into the buffer @to at offset @ppos.
616 *
617 * On success, the number of bytes written is returned and the offset @ppos is
618 * advanced by this number, or negative value is returned on error.
619 **/
620ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
621 const void __user *from, size_t count)
622{
623 loff_t pos = *ppos;
624 size_t res;
625
626 if (pos < 0)
627 return -EINVAL;
628 if (pos >= available || !count)
629 return 0;
630 if (count > available - pos)
631 count = available - pos;
632 res = copy_from_user(to + pos, from, count);
633 if (res == count)
634 return -EFAULT;
635 count -= res;
636 *ppos = pos + count;
637 return count;
638}
Al Viro12f38872013-09-15 21:20:49 -0400639EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200640
641/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700642 * memory_read_from_buffer - copy data from the buffer
643 * @to: the kernel space buffer to read to
644 * @count: the maximum number of bytes to read
645 * @ppos: the current position in the buffer
646 * @from: the buffer to read from
647 * @available: the size of the buffer
648 *
649 * The memory_read_from_buffer() function reads up to @count bytes from the
650 * buffer @from at offset @ppos into the kernel space address starting at @to.
651 *
652 * On success, the number of bytes read is returned and the offset @ppos is
653 * advanced by this number, or negative value is returned on error.
654 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700655ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
656 const void *from, size_t available)
657{
658 loff_t pos = *ppos;
659
660 if (pos < 0)
661 return -EINVAL;
662 if (pos >= available)
663 return 0;
664 if (count > available - pos)
665 count = available - pos;
666 memcpy(to, from + pos, count);
667 *ppos = pos + count;
668
669 return count;
670}
Al Viro12f38872013-09-15 21:20:49 -0400671EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
674 * Transaction based IO.
675 * The file expects a single write which triggers the transaction, and then
676 * possibly a read which collects the result - which is stored in a
677 * file-local buffer.
678 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100679
680void simple_transaction_set(struct file *file, size_t n)
681{
682 struct simple_transaction_argresp *ar = file->private_data;
683
684 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
685
686 /*
687 * The barrier ensures that ar->size will really remain zero until
688 * ar->data is ready for reading.
689 */
690 smp_mb();
691 ar->size = n;
692}
Al Viro12f38872013-09-15 21:20:49 -0400693EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
696{
697 struct simple_transaction_argresp *ar;
698 static DEFINE_SPINLOCK(simple_transaction_lock);
699
700 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
701 return ERR_PTR(-EFBIG);
702
703 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
704 if (!ar)
705 return ERR_PTR(-ENOMEM);
706
707 spin_lock(&simple_transaction_lock);
708
709 /* only one write allowed per open */
710 if (file->private_data) {
711 spin_unlock(&simple_transaction_lock);
712 free_page((unsigned long)ar);
713 return ERR_PTR(-EBUSY);
714 }
715
716 file->private_data = ar;
717
718 spin_unlock(&simple_transaction_lock);
719
720 if (copy_from_user(ar->data, buf, size))
721 return ERR_PTR(-EFAULT);
722
723 return ar->data;
724}
Al Viro12f38872013-09-15 21:20:49 -0400725EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
728{
729 struct simple_transaction_argresp *ar = file->private_data;
730
731 if (!ar)
732 return 0;
733 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
734}
Al Viro12f38872013-09-15 21:20:49 -0400735EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737int simple_transaction_release(struct inode *inode, struct file *file)
738{
739 free_page((unsigned long)file->private_data);
740 return 0;
741}
Al Viro12f38872013-09-15 21:20:49 -0400742EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200744/* Simple attribute files */
745
746struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800747 int (*get)(void *, u64 *);
748 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200749 char get_buf[24]; /* enough to store a u64 and "\n\0" */
750 char set_buf[24];
751 void *data;
752 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800753 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200754};
755
756/* simple_attr_open is called by an actual attribute open file operation
757 * to set the attribute specific access operations. */
758int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800759 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200760 const char *fmt)
761{
762 struct simple_attr *attr;
763
764 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
765 if (!attr)
766 return -ENOMEM;
767
768 attr->get = get;
769 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700770 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200771 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800772 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200773
774 file->private_data = attr;
775
776 return nonseekable_open(inode, file);
777}
Al Viro12f38872013-09-15 21:20:49 -0400778EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200779
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800780int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200781{
782 kfree(file->private_data);
783 return 0;
784}
Al Viro12f38872013-09-15 21:20:49 -0400785EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200786
787/* read from the buffer that is filled with the get function */
788ssize_t simple_attr_read(struct file *file, char __user *buf,
789 size_t len, loff_t *ppos)
790{
791 struct simple_attr *attr;
792 size_t size;
793 ssize_t ret;
794
795 attr = file->private_data;
796
797 if (!attr->get)
798 return -EACCES;
799
Christoph Hellwig92613032008-02-08 04:20:27 -0800800 ret = mutex_lock_interruptible(&attr->mutex);
801 if (ret)
802 return ret;
803
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800804 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200805 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800806 } else { /* first read */
807 u64 val;
808 ret = attr->get(attr->data, &val);
809 if (ret)
810 goto out;
811
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200812 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800813 attr->fmt, (unsigned long long)val);
814 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200815
816 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800817out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800818 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200819 return ret;
820}
Al Viro12f38872013-09-15 21:20:49 -0400821EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200822
823/* interpret the buffer as a number to call the set function with */
824ssize_t simple_attr_write(struct file *file, const char __user *buf,
825 size_t len, loff_t *ppos)
826{
827 struct simple_attr *attr;
828 u64 val;
829 size_t size;
830 ssize_t ret;
831
832 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200833 if (!attr->set)
834 return -EACCES;
835
Christoph Hellwig92613032008-02-08 04:20:27 -0800836 ret = mutex_lock_interruptible(&attr->mutex);
837 if (ret)
838 return ret;
839
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200840 ret = -EFAULT;
841 size = min(sizeof(attr->set_buf) - 1, len);
842 if (copy_from_user(attr->set_buf, buf, size))
843 goto out;
844
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200845 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700846 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700847 ret = attr->set(attr->data, val);
848 if (ret == 0)
849 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200850out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800851 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200852 return ret;
853}
Al Viro12f38872013-09-15 21:20:49 -0400854EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200855
Christoph Hellwig25961102007-10-21 16:42:05 -0700856/**
857 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
858 * @sb: filesystem to do the file handle conversion on
859 * @fid: file handle to convert
860 * @fh_len: length of the file handle in bytes
861 * @fh_type: type of file handle
862 * @get_inode: filesystem callback to retrieve inode
863 *
864 * This function decodes @fid as long as it has one of the well-known
865 * Linux filehandle types and calls @get_inode on it to retrieve the
866 * inode for the object specified in the file handle.
867 */
868struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
869 int fh_len, int fh_type, struct inode *(*get_inode)
870 (struct super_block *sb, u64 ino, u32 gen))
871{
872 struct inode *inode = NULL;
873
874 if (fh_len < 2)
875 return NULL;
876
877 switch (fh_type) {
878 case FILEID_INO32_GEN:
879 case FILEID_INO32_GEN_PARENT:
880 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
881 break;
882 }
883
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200884 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700885}
886EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
887
888/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800889 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700890 * @sb: filesystem to do the file handle conversion on
891 * @fid: file handle to convert
892 * @fh_len: length of the file handle in bytes
893 * @fh_type: type of file handle
894 * @get_inode: filesystem callback to retrieve inode
895 *
896 * This function decodes @fid as long as it has one of the well-known
897 * Linux filehandle types and calls @get_inode on it to retrieve the
898 * inode for the _parent_ object specified in the file handle if it
899 * is specified in the file handle, or NULL otherwise.
900 */
901struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
902 int fh_len, int fh_type, struct inode *(*get_inode)
903 (struct super_block *sb, u64 ino, u32 gen))
904{
905 struct inode *inode = NULL;
906
907 if (fh_len <= 2)
908 return NULL;
909
910 switch (fh_type) {
911 case FILEID_INO32_GEN_PARENT:
912 inode = get_inode(sb, fid->i32.parent_ino,
913 (fh_len > 3 ? fid->i32.parent_gen : 0));
914 break;
915 }
916
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200917 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700918}
919EXPORT_SYMBOL_GPL(generic_fh_to_parent);
920
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200921/**
Fabian Frederickac13a822014-06-04 16:06:27 -0700922 * __generic_file_fsync - generic fsync implementation for simple filesystems
923 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200924 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -0700925 * @start: start offset in bytes
926 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200927 * @datasync: only synchronize essential metadata if true
928 *
929 * This is a generic implementation of the fsync method for simple
930 * filesystems which track all non-inode metadata in the buffers list
931 * hanging off the address_space structure.
932 */
Fabian Frederickac13a822014-06-04 16:06:27 -0700933int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
934 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -0400935{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200936 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -0400937 int err;
938 int ret;
939
Josef Bacik02c24a82011-07-16 20:44:56 -0400940 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
941 if (err)
942 return err;
943
944 mutex_lock(&inode->i_mutex);
Al Virod5aacad2009-06-07 14:56:44 -0400945 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500946 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -0400947 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400948 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -0400949 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400950
Christoph Hellwigc37650162010-10-06 10:48:20 +0200951 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -0400952 if (ret == 0)
953 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -0700954
Josef Bacik02c24a82011-07-16 20:44:56 -0400955out:
956 mutex_unlock(&inode->i_mutex);
Al Virod5aacad2009-06-07 14:56:44 -0400957 return ret;
958}
Fabian Frederickac13a822014-06-04 16:06:27 -0700959EXPORT_SYMBOL(__generic_file_fsync);
960
961/**
962 * generic_file_fsync - generic fsync implementation for simple filesystems
963 * with flush
964 * @file: file to synchronize
965 * @start: start offset in bytes
966 * @end: end offset in bytes (inclusive)
967 * @datasync: only synchronize essential metadata if true
968 *
969 */
970
971int generic_file_fsync(struct file *file, loff_t start, loff_t end,
972 int datasync)
973{
974 struct inode *inode = file->f_mapping->host;
975 int err;
976
977 err = __generic_file_fsync(file, start, end, datasync);
978 if (err)
979 return err;
980 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
981}
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200982EXPORT_SYMBOL(generic_file_fsync);
983
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -0700984/**
985 * generic_check_addressable - Check addressability of file system
986 * @blocksize_bits: log of file system block size
987 * @num_blocks: number of blocks in file system
988 *
989 * Determine whether a file system with @num_blocks blocks (and a
990 * block size of 2**@blocksize_bits) is addressable by the sector_t
991 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
992 */
993int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
994{
995 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -0700996 u64 last_fs_page =
997 last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -0700998
999 if (unlikely(num_blocks == 0))
1000 return 0;
1001
1002 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
1003 return -EINVAL;
1004
Joel Beckera33f13e2010-08-16 12:10:17 -07001005 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1006 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001007 return -EFBIG;
1008 }
1009 return 0;
1010}
1011EXPORT_SYMBOL(generic_check_addressable);
1012
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001013/*
1014 * No-op implementation of ->fsync for in-memory filesystems.
1015 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001016int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001017{
1018 return 0;
1019}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001020EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001021
Al Viro5f2c4172015-05-07 11:14:26 -04001022void kfree_put_link(struct inode *unused, void *cookie)
Al Viro87dc8002013-09-16 10:30:04 -04001023{
Al Viro680baac2015-05-02 13:32:22 -04001024 kfree(cookie);
Al Viro87dc8002013-09-16 10:30:04 -04001025}
1026EXPORT_SYMBOL(kfree_put_link);
Al Viro69878432013-10-02 22:35:11 -04001027
Al Viroecc087f2015-05-07 11:19:14 -04001028void free_page_put_link(struct inode *unused, void *cookie)
1029{
1030 free_page((unsigned long) cookie);
1031}
1032EXPORT_SYMBOL(free_page_put_link);
1033
Al Viro69878432013-10-02 22:35:11 -04001034/*
1035 * nop .set_page_dirty method so that people can use .page_mkwrite on
1036 * anon inodes.
1037 */
1038static int anon_set_page_dirty(struct page *page)
1039{
1040 return 0;
1041};
1042
1043/*
1044 * A single inode exists for all anon_inode files. Contrary to pipes,
1045 * anon_inode inodes have no associated per-instance data, so we need
1046 * only allocate one of them.
1047 */
1048struct inode *alloc_anon_inode(struct super_block *s)
1049{
1050 static const struct address_space_operations anon_aops = {
1051 .set_page_dirty = anon_set_page_dirty,
1052 };
1053 struct inode *inode = new_inode_pseudo(s);
1054
1055 if (!inode)
1056 return ERR_PTR(-ENOMEM);
1057
1058 inode->i_ino = get_next_ino();
1059 inode->i_mapping->a_ops = &anon_aops;
1060
1061 /*
1062 * Mark the inode dirty from the very beginning,
1063 * that way it will never be moved to the dirty
1064 * list because mark_inode_dirty() will think
1065 * that it already _is_ on the dirty list.
1066 */
1067 inode->i_state = I_DIRTY;
1068 inode->i_mode = S_IRUSR | S_IWUSR;
1069 inode->i_uid = current_fsuid();
1070 inode->i_gid = current_fsgid();
1071 inode->i_flags |= S_PRIVATE;
1072 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1073 return inode;
1074}
1075EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001076
1077/**
1078 * simple_nosetlease - generic helper for prohibiting leases
1079 * @filp: file pointer
1080 * @arg: type of lease to obtain
1081 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001082 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001083 *
1084 * Generic helper for filesystems that do not wish to allow leases to be set.
1085 * All arguments are ignored and it just returns -EINVAL.
1086 */
1087int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001088simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1089 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001090{
1091 return -EINVAL;
1092}
1093EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001094
Al Viro6e771372015-05-02 13:37:52 -04001095const char *simple_follow_link(struct dentry *dentry, void **cookie)
Al Viro61ba64f2015-05-02 09:54:06 -04001096{
Al Viro680baac2015-05-02 13:32:22 -04001097 return d_inode(dentry)->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001098}
1099EXPORT_SYMBOL(simple_follow_link);
1100
1101const struct inode_operations simple_symlink_inode_operations = {
1102 .follow_link = simple_follow_link,
1103 .readlink = generic_readlink
1104};
1105EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001106
1107/*
1108 * Operations for a permanently empty directory.
1109 */
1110static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1111{
1112 return ERR_PTR(-ENOENT);
1113}
1114
1115static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1116 struct kstat *stat)
1117{
1118 struct inode *inode = d_inode(dentry);
1119 generic_fillattr(inode, stat);
1120 return 0;
1121}
1122
1123static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1124{
1125 return -EPERM;
1126}
1127
1128static int empty_dir_setxattr(struct dentry *dentry, const char *name,
1129 const void *value, size_t size, int flags)
1130{
1131 return -EOPNOTSUPP;
1132}
1133
1134static ssize_t empty_dir_getxattr(struct dentry *dentry, const char *name,
1135 void *value, size_t size)
1136{
1137 return -EOPNOTSUPP;
1138}
1139
1140static int empty_dir_removexattr(struct dentry *dentry, const char *name)
1141{
1142 return -EOPNOTSUPP;
1143}
1144
1145static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1146{
1147 return -EOPNOTSUPP;
1148}
1149
1150static const struct inode_operations empty_dir_inode_operations = {
1151 .lookup = empty_dir_lookup,
1152 .permission = generic_permission,
1153 .setattr = empty_dir_setattr,
1154 .getattr = empty_dir_getattr,
1155 .setxattr = empty_dir_setxattr,
1156 .getxattr = empty_dir_getxattr,
1157 .removexattr = empty_dir_removexattr,
1158 .listxattr = empty_dir_listxattr,
1159};
1160
1161static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1162{
1163 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1164 return generic_file_llseek_size(file, offset, whence, 2, 2);
1165}
1166
1167static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1168{
1169 dir_emit_dots(file, ctx);
1170 return 0;
1171}
1172
1173static const struct file_operations empty_dir_operations = {
1174 .llseek = empty_dir_llseek,
1175 .read = generic_read_dir,
1176 .iterate = empty_dir_readdir,
1177 .fsync = noop_fsync,
1178};
1179
1180
1181void make_empty_dir_inode(struct inode *inode)
1182{
1183 set_nlink(inode, 2);
1184 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1185 inode->i_uid = GLOBAL_ROOT_UID;
1186 inode->i_gid = GLOBAL_ROOT_GID;
1187 inode->i_rdev = 0;
1188 inode->i_size = 2;
1189 inode->i_blkbits = PAGE_SHIFT;
1190 inode->i_blocks = 0;
1191
1192 inode->i_op = &empty_dir_inode_operations;
1193 inode->i_fop = &empty_dir_operations;
1194}
1195
1196bool is_empty_dir_inode(struct inode *inode)
1197{
1198 return (inode->i_fop == &empty_dir_operations) &&
1199 (inode->i_op == &empty_dir_inode_operations);
1200}