blob: 217896ca4fae67cf862794f487e4a19e13dc1a85 [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>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mount.h>
12#include <linux/vfs.h>
npiggin@suse.de7bb46a62010-05-27 01:05:33 +100013#include <linux/quotaops.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080014#include <linux/mutex.h>
Al Viro87dc8002013-09-16 10:30:04 -040015#include <linux/namei.h>
Christoph Hellwig25961102007-10-21 16:42:05 -070016#include <linux/exportfs.h>
Al Virod5aacad2009-06-07 14:56:44 -040017#include <linux/writeback.h>
Al Viroff01bb42011-09-16 02:31:11 -040018#include <linux/buffer_head.h> /* sync_mapping_buffers */
Ingo Molnar7cf34c72006-03-23 03:00:36 -080019
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Al Viroa4464db2011-07-07 15:03:58 -040022#include "internal.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
25 struct kstat *stat)
26{
David Howellsdea655c2015-03-17 22:26:15 +000027 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 generic_fillattr(inode, stat);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030029 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 0;
31}
Al Viro12f38872013-09-15 21:20:49 -040032EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
David Howells726c3342006-06-23 02:02:58 -070034int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
David Howells726c3342006-06-23 02:02:58 -070036 buf->f_type = dentry->d_sb->s_magic;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030037 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 buf->f_namelen = NAME_MAX;
39 return 0;
40}
Al Viro12f38872013-09-15 21:20:49 -040041EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Retaining negative dentries for an in-memory filesystem just wastes
45 * memory and lookup time: arrange for them to be deleted immediately.
46 */
Al Virob26d4cd2013-10-25 18:47:37 -040047int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 return 1;
50}
Al Virob26d4cd2013-10-25 18:47:37 -040051EXPORT_SYMBOL(always_delete_dentry);
52
53const struct dentry_operations simple_dentry_operations = {
54 .d_delete = always_delete_dentry,
55};
56EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Lookup the data. This is trivial - if the dentry didn't already
60 * exist, we know it is negative. Set d_op to delete negative dentries.
61 */
Al Viro00cd8dd2012-06-10 17:13:09 -040062struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (dentry->d_name.len > NAME_MAX)
65 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040066 if (!dentry->d_sb->s_d_op)
67 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 d_add(dentry, NULL);
69 return NULL;
70}
Al Viro12f38872013-09-15 21:20:49 -040071EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073int dcache_dir_open(struct inode *inode, struct file *file)
74{
Al Viroba65dc52016-06-10 11:32:47 -040075 file->private_data = d_alloc_cursor(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 return file->private_data ? 0 : -ENOMEM;
78}
Al Viro12f38872013-09-15 21:20:49 -040079EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81int dcache_dir_close(struct inode *inode, struct file *file)
82{
83 dput(file->private_data);
84 return 0;
85}
Al Viro12f38872013-09-15 21:20:49 -040086EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Al Viro4f42c1b2016-06-06 19:37:13 -040088/* parent is locked at least shared */
89static struct dentry *next_positive(struct dentry *parent,
90 struct list_head *from,
91 int count)
92{
Al Viroebaaa802016-06-06 20:55:34 -040093 unsigned *seq = &parent->d_inode->i_dir_seq, n;
94 struct dentry *res;
Al Viro4f42c1b2016-06-06 19:37:13 -040095 struct list_head *p;
Al Viroebaaa802016-06-06 20:55:34 -040096 bool skipped;
97 int i;
Al Viro4f42c1b2016-06-06 19:37:13 -040098
Al Viroebaaa802016-06-06 20:55:34 -040099retry:
100 i = count;
101 skipped = false;
102 n = smp_load_acquire(seq) & ~1;
103 res = NULL;
104 rcu_read_lock();
Al Viro4f42c1b2016-06-06 19:37:13 -0400105 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
106 struct dentry *d = list_entry(p, struct dentry, d_child);
Al Viroebaaa802016-06-06 20:55:34 -0400107 if (!simple_positive(d)) {
108 skipped = true;
109 } else if (!--i) {
Al Viro4f42c1b2016-06-06 19:37:13 -0400110 res = d;
111 break;
112 }
113 }
Al Viroebaaa802016-06-06 20:55:34 -0400114 rcu_read_unlock();
115 if (skipped) {
116 smp_rmb();
117 if (unlikely(*seq != n))
118 goto retry;
119 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400120 return res;
121}
122
123static void move_cursor(struct dentry *cursor, struct list_head *after)
124{
125 struct dentry *parent = cursor->d_parent;
Al Viroebaaa802016-06-06 20:55:34 -0400126 unsigned n, *seq = &parent->d_inode->i_dir_seq;
Al Viro4f42c1b2016-06-06 19:37:13 -0400127 spin_lock(&parent->d_lock);
Al Viroebaaa802016-06-06 20:55:34 -0400128 for (;;) {
129 n = *seq;
130 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
131 break;
132 cpu_relax();
133 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400134 __list_del(cursor->d_child.prev, cursor->d_child.next);
135 if (after)
136 list_add(&cursor->d_child, after);
137 else
138 list_add_tail(&cursor->d_child, &parent->d_subdirs);
Al Viroebaaa802016-06-06 20:55:34 -0400139 smp_store_release(seq, n + 2);
Al Viro4f42c1b2016-06-06 19:37:13 -0400140 spin_unlock(&parent->d_lock);
141}
142
Andrew Morton965c8e52012-12-17 15:59:39 -0800143loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100145 struct dentry *dentry = file->f_path.dentry;
Andrew Morton965c8e52012-12-17 15:59:39 -0800146 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case 1:
148 offset += file->f_pos;
149 case 0:
150 if (offset >= 0)
151 break;
152 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -EINVAL;
154 }
155 if (offset != file->f_pos) {
156 file->f_pos = offset;
157 if (file->f_pos >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400159 struct dentry *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 loff_t n = file->f_pos - 2;
161
Al Viro274f5b02016-06-06 18:55:57 -0400162 inode_lock_shared(dentry->d_inode);
Al Viro4f42c1b2016-06-06 19:37:13 -0400163 to = next_positive(dentry, &dentry->d_subdirs, n);
164 move_cursor(cursor, to ? &to->d_child : NULL);
Al Viro274f5b02016-06-06 18:55:57 -0400165 inode_unlock_shared(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return offset;
169}
Al Viro12f38872013-09-15 21:20:49 -0400170EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* Relationship between i_mode and the DT_xxx types */
173static inline unsigned char dt_type(struct inode *inode)
174{
175 return (inode->i_mode >> 12) & 15;
176}
177
178/*
179 * Directory is locked and all positive dentries in it are safe, since
180 * for ramfs-type trees they can't go away without unlink() or rmdir(),
181 * both impossible due to the lock on directory.
182 */
183
Al Viro5f99f4e2013-05-15 20:23:06 -0400184int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Al Viro5f99f4e2013-05-15 20:23:06 -0400186 struct dentry *dentry = file->f_path.dentry;
187 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400188 struct list_head *p = &cursor->d_child;
189 struct dentry *next;
190 bool moved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Al Viro5f99f4e2013-05-15 20:23:06 -0400192 if (!dir_emit_dots(file, ctx))
193 return 0;
Al Viro4f42c1b2016-06-06 19:37:13 -0400194
Al Viro5f99f4e2013-05-15 20:23:06 -0400195 if (ctx->pos == 2)
Al Viro4f42c1b2016-06-06 19:37:13 -0400196 p = &dentry->d_subdirs;
197 while ((next = next_positive(dentry, p, 1)) != NULL) {
Al Viro5f99f4e2013-05-15 20:23:06 -0400198 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000199 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro4f42c1b2016-06-06 19:37:13 -0400200 break;
201 moved = true;
202 p = &next->d_child;
Al Viro5f99f4e2013-05-15 20:23:06 -0400203 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400205 if (moved)
206 move_cursor(cursor, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208}
Al Viro12f38872013-09-15 21:20:49 -0400209EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
212{
213 return -EISDIR;
214}
Al Viro12f38872013-09-15 21:20:49 -0400215EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800217const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .open = dcache_dir_open,
219 .release = dcache_dir_close,
220 .llseek = dcache_dir_lseek,
221 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400222 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200223 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
Al Viro12f38872013-09-15 21:20:49 -0400225EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800227const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .lookup = simple_lookup,
229};
Al Viro12f38872013-09-15 21:20:49 -0400230EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Hugh Dickins759b9772007-03-05 00:30:28 -0800232static const struct super_operations simple_super_operations = {
233 .statfs = simple_statfs,
234};
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
238 * will never be mountable)
239 */
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200240struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type, char *name,
241 const struct super_operations *ops, const struct xattr_handler **xattr,
Al Viroc74a1cb2011-01-12 16:59:34 -0500242 const struct dentry_operations *dops, unsigned long magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
David Howells9249e172012-06-25 12:55:37 +0100244 struct super_block *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct dentry *dentry;
246 struct inode *root;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700247 struct qstr d_name = QSTR_INIT(name, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Eric W. Biederman75422722017-01-04 17:37:27 +1300249 s = sget_userns(fs_type, NULL, set_anon_super, MS_KERNMOUNT|MS_NOUSER,
250 &init_user_ns, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (IS_ERR(s))
Al Viro51139ad2010-07-25 23:47:46 +0400252 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jeff Layton89a4eb42009-08-18 14:11:08 -0700254 s->s_maxbytes = MAX_LFS_FILESIZE;
Alex Nixon3971e1a2008-07-29 22:33:03 -0700255 s->s_blocksize = PAGE_SIZE;
256 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800258 s->s_op = ops ? ops : &simple_super_operations;
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200259 s->s_xattr = xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 s->s_time_gran = 1;
261 root = new_inode(s);
262 if (!root)
263 goto Enomem;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700264 /*
265 * since this is the first inode, make it number 1. New inodes created
266 * after this must take care not to collide with it (by passing
267 * max_reserved of 1 to iunique).
268 */
269 root->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700271 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
Al Viroa4464db2011-07-07 15:03:58 -0400272 dentry = __d_alloc(s, &d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!dentry) {
274 iput(root);
275 goto Enomem;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 d_instantiate(dentry, root);
278 s->s_root = dentry;
Al Viroc74a1cb2011-01-12 16:59:34 -0500279 s->s_d_op = dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 s->s_flags |= MS_ACTIVE;
Al Viro51139ad2010-07-25 23:47:46 +0400281 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283Enomem:
Al Viro6f5bbff2009-05-06 01:34:22 -0400284 deactivate_locked_super(s);
Al Viro51139ad2010-07-25 23:47:46 +0400285 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200287EXPORT_SYMBOL(mount_pseudo_xattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Stephen Boyd20955e82012-04-05 14:25:09 -0700289int simple_open(struct inode *inode, struct file *file)
290{
291 if (inode->i_private)
292 file->private_data = inode->i_private;
293 return 0;
294}
Al Viro12f38872013-09-15 21:20:49 -0400295EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
298{
David Howellsdea655c2015-03-17 22:26:15 +0000299 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Deepa Dinamani078cd822016-09-14 07:48:04 -0700301 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700302 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400303 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dget(dentry);
305 d_instantiate(dentry, inode);
306 return 0;
307}
Al Viro12f38872013-09-15 21:20:49 -0400308EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310int simple_empty(struct dentry *dentry)
311{
312 struct dentry *child;
313 int ret = 0;
314
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100315 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400316 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100317 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
318 if (simple_positive(child)) {
319 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100321 }
322 spin_unlock(&child->d_lock);
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ret = 1;
325out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100326 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return ret;
328}
Al Viro12f38872013-09-15 21:20:49 -0400329EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331int simple_unlink(struct inode *dir, struct dentry *dentry)
332{
David Howellsdea655c2015-03-17 22:26:15 +0000333 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Deepa Dinamani078cd822016-09-14 07:48:04 -0700335 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700336 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dput(dentry);
338 return 0;
339}
Al Viro12f38872013-09-15 21:20:49 -0400340EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342int simple_rmdir(struct inode *dir, struct dentry *dentry)
343{
344 if (!simple_empty(dentry))
345 return -ENOTEMPTY;
346
David Howellsdea655c2015-03-17 22:26:15 +0000347 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700349 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
Al Viro12f38872013-09-15 21:20:49 -0400352EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200355 struct inode *new_dir, struct dentry *new_dentry,
356 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David Howellsdea655c2015-03-17 22:26:15 +0000358 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000359 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200361 if (flags & ~RENAME_NOREPLACE)
362 return -EINVAL;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!simple_empty(new_dentry))
365 return -ENOTEMPTY;
366
David Howellsdea655c2015-03-17 22:26:15 +0000367 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400369 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000370 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700371 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700374 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700375 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -0700379 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return 0;
382}
Al Viro12f38872013-09-15 21:20:49 -0400383EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000385/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200386 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000387 * @dentry: dentry
388 * @iattr: iattr structure
389 *
390 * Returns 0 on success, -error on failure.
391 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200392 * simple_setattr is a simple ->setattr implementation without a proper
393 * implementation of size changes.
394 *
395 * It can either be used for in-memory filesystems or special files
396 * on simple regular filesystems. Anything that needs to change on-disk
397 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000398 */
399int simple_setattr(struct dentry *dentry, struct iattr *iattr)
400{
David Howellsdea655c2015-03-17 22:26:15 +0000401 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000402 int error;
403
Jan Kara31051c82016-05-26 16:55:18 +0200404 error = setattr_prepare(dentry, iattr);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000405 if (error)
406 return error;
407
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200408 if (iattr->ia_valid & ATTR_SIZE)
409 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200410 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200411 mark_inode_dirty(inode);
412 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000413}
414EXPORT_SYMBOL(simple_setattr);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416int simple_readpage(struct file *file, struct page *page)
417{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700418 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 flush_dcache_page(page);
420 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unlock_page(page);
422 return 0;
423}
Al Viro12f38872013-09-15 21:20:49 -0400424EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Nick Pigginafddba42007-10-16 01:25:01 -0700426int simple_write_begin(struct file *file, struct address_space *mapping,
427 loff_t pos, unsigned len, unsigned flags,
428 struct page **pagep, void **fsdata)
429{
430 struct page *page;
431 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700432
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300433 index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700434
Nick Piggin54566b22009-01-04 12:00:53 -0800435 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700436 if (!page)
437 return -ENOMEM;
438
439 *pagep = page;
440
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300441 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
442 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200443
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300444 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200445 }
446 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700447}
Al Viro12f38872013-09-15 21:20:49 -0400448EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700449
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200450/**
451 * simple_write_end - .write_end helper for non-block-device FSes
452 * @available: See .write_end of address_space_operations
453 * @file: "
454 * @mapping: "
455 * @pos: "
456 * @len: "
457 * @copied: "
458 * @page: "
459 * @fsdata: "
460 *
461 * simple_write_end does the minimum needed for updating a page after writing is
462 * done. It has the same API signature as the .write_end of
463 * address_space_operations vector. So it can just be set onto .write_end for
464 * FSes that don't need any other processing. i_mutex is assumed to be held.
465 * Block based filesystems should use generic_write_end().
466 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
467 * is not called, so a filesystem that actually does store data in .write_inode
468 * should extend on what's done here with a call to mark_inode_dirty() in the
469 * case that i_size has changed.
Al Viro04fff642016-08-29 22:39:56 -0400470 *
471 * Use *ONLY* with simple_readpage()
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200472 */
473int simple_write_end(struct file *file, struct address_space *mapping,
474 loff_t pos, unsigned len, unsigned copied,
475 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200478 loff_t last_pos = pos + copied;
479
480 /* zero the stale part of the page if we did a short copy */
Al Viro04fff642016-08-29 22:39:56 -0400481 if (!PageUptodate(page)) {
482 if (copied < len) {
483 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200484
Al Viro04fff642016-08-29 22:39:56 -0400485 zero_user(page, from + copied, len - copied);
486 }
Nick Piggin955eff52007-02-20 13:58:08 -0800487 SetPageUptodate(page);
Al Viro04fff642016-08-29 22:39:56 -0400488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800491 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200493 if (last_pos > inode->i_size)
494 i_size_write(inode, last_pos);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700497 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300498 put_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700499
500 return copied;
501}
Al Viro12f38872013-09-15 21:20:49 -0400502EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700503
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700504/*
505 * the inodes created here are not hashed. If you use iunique to generate
506 * unique inode values later for this filesystem, then you must take care
507 * to pass it an appropriate max_reserved value to avoid collisions.
508 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200509int simple_fill_super(struct super_block *s, unsigned long magic,
510 struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct inode *inode;
513 struct dentry *root;
514 struct dentry *dentry;
515 int i;
516
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 s->s_blocksize = PAGE_SIZE;
518 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800520 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 s->s_time_gran = 1;
522
523 inode = new_inode(s);
524 if (!inode)
525 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700526 /*
527 * because the root inode is 1, the files array must not contain an
528 * entry at index 1
529 */
530 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 inode->i_mode = S_IFDIR | 0755;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700532 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 inode->i_op = &simple_dir_inode_operations;
534 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200535 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500536 root = d_make_root(inode);
537 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 for (i = 0; !files->name || files->name[0]; i++, files++) {
540 if (!files->name)
541 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700542
543 /* warn if it tries to conflict with the root inode */
544 if (unlikely(i == 1))
545 printk(KERN_WARNING "%s: %s passed in a files array"
546 "with an index of 1!\n", __func__,
547 s->s_type->name);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dentry = d_alloc_name(root, files->name);
550 if (!dentry)
551 goto out;
552 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300553 if (!inode) {
554 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 inode->i_mode = S_IFREG | files->mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700558 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 inode->i_fop = files->ops;
560 inode->i_ino = i;
561 d_add(dentry, inode);
562 }
563 s->s_root = root;
564 return 0;
565out:
566 d_genocide(root);
Al Viro640946f2012-04-02 19:22:25 -0400567 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 dput(root);
569 return -ENOMEM;
570}
Al Viro12f38872013-09-15 21:20:49 -0400571EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573static DEFINE_SPINLOCK(pin_fs_lock);
574
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400575int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct vfsmount *mnt = NULL;
578 spin_lock(&pin_fs_lock);
579 if (unlikely(!*mount)) {
580 spin_unlock(&pin_fs_lock);
Al Viro24529922012-03-17 02:13:52 -0400581 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (IS_ERR(mnt))
583 return PTR_ERR(mnt);
584 spin_lock(&pin_fs_lock);
585 if (!*mount)
586 *mount = mnt;
587 }
588 mntget(*mount);
589 ++*count;
590 spin_unlock(&pin_fs_lock);
591 mntput(mnt);
592 return 0;
593}
Al Viro12f38872013-09-15 21:20:49 -0400594EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596void simple_release_fs(struct vfsmount **mount, int *count)
597{
598 struct vfsmount *mnt;
599 spin_lock(&pin_fs_lock);
600 mnt = *mount;
601 if (!--*count)
602 *mount = NULL;
603 spin_unlock(&pin_fs_lock);
604 mntput(mnt);
605}
Al Viro12f38872013-09-15 21:20:49 -0400606EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700608/**
609 * simple_read_from_buffer - copy data from the buffer to user space
610 * @to: the user space buffer to read to
611 * @count: the maximum number of bytes to read
612 * @ppos: the current position in the buffer
613 * @from: the buffer to read from
614 * @available: the size of the buffer
615 *
616 * The simple_read_from_buffer() function reads up to @count bytes from the
617 * buffer @from at offset @ppos into the user space address starting at @to.
618 *
619 * On success, the number of bytes read is returned and the offset @ppos is
620 * advanced by this number, or negative value is returned on error.
621 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
623 const void *from, size_t available)
624{
625 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700626 size_t ret;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (pos < 0)
629 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700630 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
632 if (count > available - pos)
633 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700634 ret = copy_to_user(to, from + pos, count);
635 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700637 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *ppos = pos + count;
639 return count;
640}
Al Viro12f38872013-09-15 21:20:49 -0400641EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700643/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200644 * simple_write_to_buffer - copy data from user space to the buffer
645 * @to: the buffer to write to
646 * @available: the size of the buffer
647 * @ppos: the current position in the buffer
648 * @from: the user space buffer to read from
649 * @count: the maximum number of bytes to read
650 *
651 * The simple_write_to_buffer() function reads up to @count bytes from the user
652 * space address starting at @from into the buffer @to at offset @ppos.
653 *
654 * On success, the number of bytes written is returned and the offset @ppos is
655 * advanced by this number, or negative value is returned on error.
656 **/
657ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
658 const void __user *from, size_t count)
659{
660 loff_t pos = *ppos;
661 size_t res;
662
663 if (pos < 0)
664 return -EINVAL;
665 if (pos >= available || !count)
666 return 0;
667 if (count > available - pos)
668 count = available - pos;
669 res = copy_from_user(to + pos, from, count);
670 if (res == count)
671 return -EFAULT;
672 count -= res;
673 *ppos = pos + count;
674 return count;
675}
Al Viro12f38872013-09-15 21:20:49 -0400676EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200677
678/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700679 * memory_read_from_buffer - copy data from the buffer
680 * @to: the kernel space buffer to read to
681 * @count: the maximum number of bytes to read
682 * @ppos: the current position in the buffer
683 * @from: the buffer to read from
684 * @available: the size of the buffer
685 *
686 * The memory_read_from_buffer() function reads up to @count bytes from the
687 * buffer @from at offset @ppos into the kernel space address starting at @to.
688 *
689 * On success, the number of bytes read is returned and the offset @ppos is
690 * advanced by this number, or negative value is returned on error.
691 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700692ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
693 const void *from, size_t available)
694{
695 loff_t pos = *ppos;
696
697 if (pos < 0)
698 return -EINVAL;
699 if (pos >= available)
700 return 0;
701 if (count > available - pos)
702 count = available - pos;
703 memcpy(to, from + pos, count);
704 *ppos = pos + count;
705
706 return count;
707}
Al Viro12f38872013-09-15 21:20:49 -0400708EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * Transaction based IO.
712 * The file expects a single write which triggers the transaction, and then
713 * possibly a read which collects the result - which is stored in a
714 * file-local buffer.
715 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100716
717void simple_transaction_set(struct file *file, size_t n)
718{
719 struct simple_transaction_argresp *ar = file->private_data;
720
721 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
722
723 /*
724 * The barrier ensures that ar->size will really remain zero until
725 * ar->data is ready for reading.
726 */
727 smp_mb();
728 ar->size = n;
729}
Al Viro12f38872013-09-15 21:20:49 -0400730EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
733{
734 struct simple_transaction_argresp *ar;
735 static DEFINE_SPINLOCK(simple_transaction_lock);
736
737 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
738 return ERR_PTR(-EFBIG);
739
740 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
741 if (!ar)
742 return ERR_PTR(-ENOMEM);
743
744 spin_lock(&simple_transaction_lock);
745
746 /* only one write allowed per open */
747 if (file->private_data) {
748 spin_unlock(&simple_transaction_lock);
749 free_page((unsigned long)ar);
750 return ERR_PTR(-EBUSY);
751 }
752
753 file->private_data = ar;
754
755 spin_unlock(&simple_transaction_lock);
756
757 if (copy_from_user(ar->data, buf, size))
758 return ERR_PTR(-EFAULT);
759
760 return ar->data;
761}
Al Viro12f38872013-09-15 21:20:49 -0400762EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
765{
766 struct simple_transaction_argresp *ar = file->private_data;
767
768 if (!ar)
769 return 0;
770 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
771}
Al Viro12f38872013-09-15 21:20:49 -0400772EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774int simple_transaction_release(struct inode *inode, struct file *file)
775{
776 free_page((unsigned long)file->private_data);
777 return 0;
778}
Al Viro12f38872013-09-15 21:20:49 -0400779EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200781/* Simple attribute files */
782
783struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800784 int (*get)(void *, u64 *);
785 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200786 char get_buf[24]; /* enough to store a u64 and "\n\0" */
787 char set_buf[24];
788 void *data;
789 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800790 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200791};
792
793/* simple_attr_open is called by an actual attribute open file operation
794 * to set the attribute specific access operations. */
795int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800796 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200797 const char *fmt)
798{
799 struct simple_attr *attr;
800
801 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
802 if (!attr)
803 return -ENOMEM;
804
805 attr->get = get;
806 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700807 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200808 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800809 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200810
811 file->private_data = attr;
812
813 return nonseekable_open(inode, file);
814}
Al Viro12f38872013-09-15 21:20:49 -0400815EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200816
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800817int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200818{
819 kfree(file->private_data);
820 return 0;
821}
Al Viro12f38872013-09-15 21:20:49 -0400822EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200823
824/* read from the buffer that is filled with the get function */
825ssize_t simple_attr_read(struct file *file, char __user *buf,
826 size_t len, loff_t *ppos)
827{
828 struct simple_attr *attr;
829 size_t size;
830 ssize_t ret;
831
832 attr = file->private_data;
833
834 if (!attr->get)
835 return -EACCES;
836
Christoph Hellwig92613032008-02-08 04:20:27 -0800837 ret = mutex_lock_interruptible(&attr->mutex);
838 if (ret)
839 return ret;
840
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800841 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200842 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800843 } else { /* first read */
844 u64 val;
845 ret = attr->get(attr->data, &val);
846 if (ret)
847 goto out;
848
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200849 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800850 attr->fmt, (unsigned long long)val);
851 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200852
853 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800854out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800855 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200856 return ret;
857}
Al Viro12f38872013-09-15 21:20:49 -0400858EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200859
860/* interpret the buffer as a number to call the set function with */
861ssize_t simple_attr_write(struct file *file, const char __user *buf,
862 size_t len, loff_t *ppos)
863{
864 struct simple_attr *attr;
865 u64 val;
866 size_t size;
867 ssize_t ret;
868
869 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200870 if (!attr->set)
871 return -EACCES;
872
Christoph Hellwig92613032008-02-08 04:20:27 -0800873 ret = mutex_lock_interruptible(&attr->mutex);
874 if (ret)
875 return ret;
876
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200877 ret = -EFAULT;
878 size = min(sizeof(attr->set_buf) - 1, len);
879 if (copy_from_user(attr->set_buf, buf, size))
880 goto out;
881
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200882 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700883 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700884 ret = attr->set(attr->data, val);
885 if (ret == 0)
886 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200887out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800888 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200889 return ret;
890}
Al Viro12f38872013-09-15 21:20:49 -0400891EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200892
Christoph Hellwig25961102007-10-21 16:42:05 -0700893/**
894 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
895 * @sb: filesystem to do the file handle conversion on
896 * @fid: file handle to convert
897 * @fh_len: length of the file handle in bytes
898 * @fh_type: type of file handle
899 * @get_inode: filesystem callback to retrieve inode
900 *
901 * This function decodes @fid as long as it has one of the well-known
902 * Linux filehandle types and calls @get_inode on it to retrieve the
903 * inode for the object specified in the file handle.
904 */
905struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
906 int fh_len, int fh_type, struct inode *(*get_inode)
907 (struct super_block *sb, u64 ino, u32 gen))
908{
909 struct inode *inode = NULL;
910
911 if (fh_len < 2)
912 return NULL;
913
914 switch (fh_type) {
915 case FILEID_INO32_GEN:
916 case FILEID_INO32_GEN_PARENT:
917 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
918 break;
919 }
920
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200921 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700922}
923EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
924
925/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800926 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700927 * @sb: filesystem to do the file handle conversion on
928 * @fid: file handle to convert
929 * @fh_len: length of the file handle in bytes
930 * @fh_type: type of file handle
931 * @get_inode: filesystem callback to retrieve inode
932 *
933 * This function decodes @fid as long as it has one of the well-known
934 * Linux filehandle types and calls @get_inode on it to retrieve the
935 * inode for the _parent_ object specified in the file handle if it
936 * is specified in the file handle, or NULL otherwise.
937 */
938struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
939 int fh_len, int fh_type, struct inode *(*get_inode)
940 (struct super_block *sb, u64 ino, u32 gen))
941{
942 struct inode *inode = NULL;
943
944 if (fh_len <= 2)
945 return NULL;
946
947 switch (fh_type) {
948 case FILEID_INO32_GEN_PARENT:
949 inode = get_inode(sb, fid->i32.parent_ino,
950 (fh_len > 3 ? fid->i32.parent_gen : 0));
951 break;
952 }
953
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200954 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700955}
956EXPORT_SYMBOL_GPL(generic_fh_to_parent);
957
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200958/**
Fabian Frederickac13a822014-06-04 16:06:27 -0700959 * __generic_file_fsync - generic fsync implementation for simple filesystems
960 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200961 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -0700962 * @start: start offset in bytes
963 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200964 * @datasync: only synchronize essential metadata if true
965 *
966 * This is a generic implementation of the fsync method for simple
967 * filesystems which track all non-inode metadata in the buffers list
968 * hanging off the address_space structure.
969 */
Fabian Frederickac13a822014-06-04 16:06:27 -0700970int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
971 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -0400972{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200973 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -0400974 int err;
975 int ret;
976
Josef Bacik02c24a82011-07-16 20:44:56 -0400977 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
978 if (err)
979 return err;
980
Al Viro59551022016-01-22 15:40:57 -0500981 inode_lock(inode);
Al Virod5aacad2009-06-07 14:56:44 -0400982 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500983 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -0400984 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400985 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -0400986 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400987
Christoph Hellwigc37650162010-10-06 10:48:20 +0200988 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -0400989 if (ret == 0)
990 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -0700991
Josef Bacik02c24a82011-07-16 20:44:56 -0400992out:
Al Viro59551022016-01-22 15:40:57 -0500993 inode_unlock(inode);
Al Virod5aacad2009-06-07 14:56:44 -0400994 return ret;
995}
Fabian Frederickac13a822014-06-04 16:06:27 -0700996EXPORT_SYMBOL(__generic_file_fsync);
997
998/**
999 * generic_file_fsync - generic fsync implementation for simple filesystems
1000 * with flush
1001 * @file: file to synchronize
1002 * @start: start offset in bytes
1003 * @end: end offset in bytes (inclusive)
1004 * @datasync: only synchronize essential metadata if true
1005 *
1006 */
1007
1008int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1009 int datasync)
1010{
1011 struct inode *inode = file->f_mapping->host;
1012 int err;
1013
1014 err = __generic_file_fsync(file, start, end, datasync);
1015 if (err)
1016 return err;
1017 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1018}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001019EXPORT_SYMBOL(generic_file_fsync);
1020
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001021/**
1022 * generic_check_addressable - Check addressability of file system
1023 * @blocksize_bits: log of file system block size
1024 * @num_blocks: number of blocks in file system
1025 *
1026 * Determine whether a file system with @num_blocks blocks (and a
1027 * block size of 2**@blocksize_bits) is addressable by the sector_t
1028 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1029 */
1030int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1031{
1032 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -07001033 u64 last_fs_page =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001034 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001035
1036 if (unlikely(num_blocks == 0))
1037 return 0;
1038
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001039 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001040 return -EINVAL;
1041
Joel Beckera33f13e2010-08-16 12:10:17 -07001042 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1043 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001044 return -EFBIG;
1045 }
1046 return 0;
1047}
1048EXPORT_SYMBOL(generic_check_addressable);
1049
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001050/*
1051 * No-op implementation of ->fsync for in-memory filesystems.
1052 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001053int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001054{
1055 return 0;
1056}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001057EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001058
Al Virofceef392015-12-29 15:58:39 -05001059/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1060void kfree_link(void *p)
Al Viro87dc8002013-09-16 10:30:04 -04001061{
Al Virofceef392015-12-29 15:58:39 -05001062 kfree(p);
Al Viro87dc8002013-09-16 10:30:04 -04001063}
Al Virofceef392015-12-29 15:58:39 -05001064EXPORT_SYMBOL(kfree_link);
Al Viro69878432013-10-02 22:35:11 -04001065
1066/*
1067 * nop .set_page_dirty method so that people can use .page_mkwrite on
1068 * anon inodes.
1069 */
1070static int anon_set_page_dirty(struct page *page)
1071{
1072 return 0;
1073};
1074
1075/*
1076 * A single inode exists for all anon_inode files. Contrary to pipes,
1077 * anon_inode inodes have no associated per-instance data, so we need
1078 * only allocate one of them.
1079 */
1080struct inode *alloc_anon_inode(struct super_block *s)
1081{
1082 static const struct address_space_operations anon_aops = {
1083 .set_page_dirty = anon_set_page_dirty,
1084 };
1085 struct inode *inode = new_inode_pseudo(s);
1086
1087 if (!inode)
1088 return ERR_PTR(-ENOMEM);
1089
1090 inode->i_ino = get_next_ino();
1091 inode->i_mapping->a_ops = &anon_aops;
1092
1093 /*
1094 * Mark the inode dirty from the very beginning,
1095 * that way it will never be moved to the dirty
1096 * list because mark_inode_dirty() will think
1097 * that it already _is_ on the dirty list.
1098 */
1099 inode->i_state = I_DIRTY;
1100 inode->i_mode = S_IRUSR | S_IWUSR;
1101 inode->i_uid = current_fsuid();
1102 inode->i_gid = current_fsgid();
1103 inode->i_flags |= S_PRIVATE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001104 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro69878432013-10-02 22:35:11 -04001105 return inode;
1106}
1107EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001108
1109/**
1110 * simple_nosetlease - generic helper for prohibiting leases
1111 * @filp: file pointer
1112 * @arg: type of lease to obtain
1113 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001114 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001115 *
1116 * Generic helper for filesystems that do not wish to allow leases to be set.
1117 * All arguments are ignored and it just returns -EINVAL.
1118 */
1119int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001120simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1121 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001122{
1123 return -EINVAL;
1124}
1125EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001126
Al Viro6b255392015-11-17 10:20:54 -05001127const char *simple_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05001128 struct delayed_call *done)
Al Viro61ba64f2015-05-02 09:54:06 -04001129{
Al Viro6b255392015-11-17 10:20:54 -05001130 return inode->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001131}
Al Viro6b255392015-11-17 10:20:54 -05001132EXPORT_SYMBOL(simple_get_link);
Al Viro61ba64f2015-05-02 09:54:06 -04001133
1134const struct inode_operations simple_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001135 .get_link = simple_get_link,
Al Viro61ba64f2015-05-02 09:54:06 -04001136};
1137EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001138
1139/*
1140 * Operations for a permanently empty directory.
1141 */
1142static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1143{
1144 return ERR_PTR(-ENOENT);
1145}
1146
1147static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1148 struct kstat *stat)
1149{
1150 struct inode *inode = d_inode(dentry);
1151 generic_fillattr(inode, stat);
1152 return 0;
1153}
1154
1155static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1156{
1157 return -EPERM;
1158}
1159
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001160static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1161{
1162 return -EOPNOTSUPP;
1163}
1164
1165static const struct inode_operations empty_dir_inode_operations = {
1166 .lookup = empty_dir_lookup,
1167 .permission = generic_permission,
1168 .setattr = empty_dir_setattr,
1169 .getattr = empty_dir_getattr,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001170 .listxattr = empty_dir_listxattr,
1171};
1172
1173static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1174{
1175 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1176 return generic_file_llseek_size(file, offset, whence, 2, 2);
1177}
1178
1179static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1180{
1181 dir_emit_dots(file, ctx);
1182 return 0;
1183}
1184
1185static const struct file_operations empty_dir_operations = {
1186 .llseek = empty_dir_llseek,
1187 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001188 .iterate_shared = empty_dir_readdir,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001189 .fsync = noop_fsync,
1190};
1191
1192
1193void make_empty_dir_inode(struct inode *inode)
1194{
1195 set_nlink(inode, 2);
1196 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1197 inode->i_uid = GLOBAL_ROOT_UID;
1198 inode->i_gid = GLOBAL_ROOT_GID;
1199 inode->i_rdev = 0;
Eric W. Biederman4b75de862015-08-12 15:00:12 -05001200 inode->i_size = 0;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001201 inode->i_blkbits = PAGE_SHIFT;
1202 inode->i_blocks = 0;
1203
1204 inode->i_op = &empty_dir_inode_operations;
Andreas Gruenbacherf5c24432016-09-29 17:48:41 +02001205 inode->i_opflags &= ~IOP_XATTR;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001206 inode->i_fop = &empty_dir_operations;
1207}
1208
1209bool is_empty_dir_inode(struct inode *inode)
1210{
1211 return (inode->i_fop == &empty_dir_operations) &&
1212 (inode->i_op == &empty_dir_inode_operations);
1213}