blob: 7e6811ba4edd39279989c1eaa83e41bd54f7ca46 [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 */
David Howells31d6d5c2019-03-25 16:38:23 +000019#include <linux/fs_context.h>
20#include <linux/pseudo_fs.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080021
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Al Viroa4464db2011-07-07 15:03:58 -040024#include "internal.h"
25
David Howellsa528d352017-01-31 16:46:22 +000026int simple_getattr(const struct path *path, struct kstat *stat,
27 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
David Howellsa528d352017-01-31 16:46:22 +000029 struct inode *inode = d_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 generic_fillattr(inode, stat);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030031 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return 0;
33}
Al Viro12f38872013-09-15 21:20:49 -040034EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David Howells726c3342006-06-23 02:02:58 -070036int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
David Howells726c3342006-06-23 02:02:58 -070038 buf->f_type = dentry->d_sb->s_magic;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030039 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 buf->f_namelen = NAME_MAX;
41 return 0;
42}
Al Viro12f38872013-09-15 21:20:49 -040043EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * Retaining negative dentries for an in-memory filesystem just wastes
47 * memory and lookup time: arrange for them to be deleted immediately.
48 */
Al Virob26d4cd2013-10-25 18:47:37 -040049int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 return 1;
52}
Al Virob26d4cd2013-10-25 18:47:37 -040053EXPORT_SYMBOL(always_delete_dentry);
54
55const struct dentry_operations simple_dentry_operations = {
56 .d_delete = always_delete_dentry,
57};
58EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * Lookup the data. This is trivial - if the dentry didn't already
62 * exist, we know it is negative. Set d_op to delete negative dentries.
63 */
Al Viro00cd8dd2012-06-10 17:13:09 -040064struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (dentry->d_name.len > NAME_MAX)
67 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040068 if (!dentry->d_sb->s_d_op)
69 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 d_add(dentry, NULL);
71 return NULL;
72}
Al Viro12f38872013-09-15 21:20:49 -040073EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075int dcache_dir_open(struct inode *inode, struct file *file)
76{
Al Viroba65dc52016-06-10 11:32:47 -040077 file->private_data = d_alloc_cursor(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 return file->private_data ? 0 : -ENOMEM;
80}
Al Viro12f38872013-09-15 21:20:49 -040081EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83int dcache_dir_close(struct inode *inode, struct file *file)
84{
85 dput(file->private_data);
86 return 0;
87}
Al Viro12f38872013-09-15 21:20:49 -040088EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Al Viro4f42c1b2016-06-06 19:37:13 -040090/* parent is locked at least shared */
91static struct dentry *next_positive(struct dentry *parent,
92 struct list_head *from,
93 int count)
94{
Al Viroebaaa802016-06-06 20:55:34 -040095 unsigned *seq = &parent->d_inode->i_dir_seq, n;
96 struct dentry *res;
Al Viro4f42c1b2016-06-06 19:37:13 -040097 struct list_head *p;
Al Viroebaaa802016-06-06 20:55:34 -040098 bool skipped;
99 int i;
Al Viro4f42c1b2016-06-06 19:37:13 -0400100
Al Viroebaaa802016-06-06 20:55:34 -0400101retry:
102 i = count;
103 skipped = false;
104 n = smp_load_acquire(seq) & ~1;
105 res = NULL;
106 rcu_read_lock();
Al Viro4f42c1b2016-06-06 19:37:13 -0400107 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
108 struct dentry *d = list_entry(p, struct dentry, d_child);
Al Viroebaaa802016-06-06 20:55:34 -0400109 if (!simple_positive(d)) {
110 skipped = true;
111 } else if (!--i) {
Al Viro4f42c1b2016-06-06 19:37:13 -0400112 res = d;
113 break;
114 }
115 }
Al Viroebaaa802016-06-06 20:55:34 -0400116 rcu_read_unlock();
117 if (skipped) {
118 smp_rmb();
119 if (unlikely(*seq != n))
120 goto retry;
121 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400122 return res;
123}
124
125static void move_cursor(struct dentry *cursor, struct list_head *after)
126{
127 struct dentry *parent = cursor->d_parent;
Al Viroebaaa802016-06-06 20:55:34 -0400128 unsigned n, *seq = &parent->d_inode->i_dir_seq;
Al Viro4f42c1b2016-06-06 19:37:13 -0400129 spin_lock(&parent->d_lock);
Al Viroebaaa802016-06-06 20:55:34 -0400130 for (;;) {
131 n = *seq;
132 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
133 break;
134 cpu_relax();
135 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400136 __list_del(cursor->d_child.prev, cursor->d_child.next);
137 if (after)
138 list_add(&cursor->d_child, after);
139 else
140 list_add_tail(&cursor->d_child, &parent->d_subdirs);
Al Viroebaaa802016-06-06 20:55:34 -0400141 smp_store_release(seq, n + 2);
Al Viro4f42c1b2016-06-06 19:37:13 -0400142 spin_unlock(&parent->d_lock);
143}
144
Andrew Morton965c8e52012-12-17 15:59:39 -0800145loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100147 struct dentry *dentry = file->f_path.dentry;
Andrew Morton965c8e52012-12-17 15:59:39 -0800148 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 case 1:
150 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600151 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case 0:
153 if (offset >= 0)
154 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600155 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EINVAL;
158 }
159 if (offset != file->f_pos) {
160 file->f_pos = offset;
161 if (file->f_pos >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400163 struct dentry *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 loff_t n = file->f_pos - 2;
165
Al Viro274f5b02016-06-06 18:55:57 -0400166 inode_lock_shared(dentry->d_inode);
Al Viro4f42c1b2016-06-06 19:37:13 -0400167 to = next_positive(dentry, &dentry->d_subdirs, n);
168 move_cursor(cursor, to ? &to->d_child : NULL);
Al Viro274f5b02016-06-06 18:55:57 -0400169 inode_unlock_shared(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return offset;
173}
Al Viro12f38872013-09-15 21:20:49 -0400174EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* Relationship between i_mode and the DT_xxx types */
177static inline unsigned char dt_type(struct inode *inode)
178{
179 return (inode->i_mode >> 12) & 15;
180}
181
182/*
183 * Directory is locked and all positive dentries in it are safe, since
184 * for ramfs-type trees they can't go away without unlink() or rmdir(),
185 * both impossible due to the lock on directory.
186 */
187
Al Viro5f99f4e2013-05-15 20:23:06 -0400188int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Al Viro5f99f4e2013-05-15 20:23:06 -0400190 struct dentry *dentry = file->f_path.dentry;
191 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400192 struct list_head *p = &cursor->d_child;
193 struct dentry *next;
194 bool moved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Al Viro5f99f4e2013-05-15 20:23:06 -0400196 if (!dir_emit_dots(file, ctx))
197 return 0;
Al Viro4f42c1b2016-06-06 19:37:13 -0400198
Al Viro5f99f4e2013-05-15 20:23:06 -0400199 if (ctx->pos == 2)
Al Viro4f42c1b2016-06-06 19:37:13 -0400200 p = &dentry->d_subdirs;
201 while ((next = next_positive(dentry, p, 1)) != NULL) {
Al Viro5f99f4e2013-05-15 20:23:06 -0400202 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000203 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro4f42c1b2016-06-06 19:37:13 -0400204 break;
205 moved = true;
206 p = &next->d_child;
Al Viro5f99f4e2013-05-15 20:23:06 -0400207 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400209 if (moved)
210 move_cursor(cursor, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
Al Viro12f38872013-09-15 21:20:49 -0400213EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
216{
217 return -EISDIR;
218}
Al Viro12f38872013-09-15 21:20:49 -0400219EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800221const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .open = dcache_dir_open,
223 .release = dcache_dir_close,
224 .llseek = dcache_dir_lseek,
225 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400226 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200227 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
Al Viro12f38872013-09-15 21:20:49 -0400229EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800231const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .lookup = simple_lookup,
233};
Al Viro12f38872013-09-15 21:20:49 -0400234EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Hugh Dickins759b9772007-03-05 00:30:28 -0800236static const struct super_operations simple_super_operations = {
237 .statfs = simple_statfs,
238};
239
David Howellsdb2c2462019-03-25 16:38:26 +0000240static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
David Howells31d6d5c2019-03-25 16:38:23 +0000241{
242 struct pseudo_fs_context *ctx = fc->fs_private;
Al Viro8d9e46d2019-05-11 11:43:59 -0400243 struct inode *root;
David Howells31d6d5c2019-03-25 16:38:23 +0000244
Al Viro8d9e46d2019-05-11 11:43:59 -0400245 s->s_maxbytes = MAX_LFS_FILESIZE;
246 s->s_blocksize = PAGE_SIZE;
247 s->s_blocksize_bits = PAGE_SHIFT;
248 s->s_magic = ctx->magic;
249 s->s_op = ctx->ops ?: &simple_super_operations;
250 s->s_xattr = ctx->xattr;
251 s->s_time_gran = 1;
252 root = new_inode(s);
253 if (!root)
David Howellsdb2c2462019-03-25 16:38:26 +0000254 return -ENOMEM;
255
Al Viro8d9e46d2019-05-11 11:43:59 -0400256 /*
257 * since this is the first inode, make it number 1. New inodes created
258 * after this must take care not to collide with it (by passing
259 * max_reserved of 1 to iunique).
260 */
261 root->i_ino = 1;
262 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
263 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
264 s->s_root = d_make_root(root);
265 if (!s->s_root)
David Howellsdb2c2462019-03-25 16:38:26 +0000266 return -ENOMEM;
Al Viro8d9e46d2019-05-11 11:43:59 -0400267 s->s_d_op = ctx->dops;
David Howells31d6d5c2019-03-25 16:38:23 +0000268 return 0;
David Howellsdb2c2462019-03-25 16:38:26 +0000269}
Al Viro8d9e46d2019-05-11 11:43:59 -0400270
David Howellsdb2c2462019-03-25 16:38:26 +0000271static int pseudo_fs_get_tree(struct fs_context *fc)
272{
273 return vfs_get_super(fc, vfs_get_independent_super, pseudo_fs_fill_super);
David Howells31d6d5c2019-03-25 16:38:23 +0000274}
275
276static void pseudo_fs_free(struct fs_context *fc)
277{
278 kfree(fc->fs_private);
279}
280
281static const struct fs_context_operations pseudo_fs_context_ops = {
282 .free = pseudo_fs_free,
283 .get_tree = pseudo_fs_get_tree,
284};
285
286/*
287 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
288 * will never be mountable)
289 */
290struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
291 unsigned long magic)
292{
293 struct pseudo_fs_context *ctx;
294
295 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
296 if (likely(ctx)) {
297 ctx->magic = magic;
298 fc->fs_private = ctx;
299 fc->ops = &pseudo_fs_context_ops;
David Howellsdb2c2462019-03-25 16:38:26 +0000300 fc->sb_flags |= SB_NOUSER;
301 fc->global = true;
David Howells31d6d5c2019-03-25 16:38:23 +0000302 }
303 return ctx;
304}
305EXPORT_SYMBOL(init_pseudo);
306
Stephen Boyd20955e82012-04-05 14:25:09 -0700307int simple_open(struct inode *inode, struct file *file)
308{
309 if (inode->i_private)
310 file->private_data = inode->i_private;
311 return 0;
312}
Al Viro12f38872013-09-15 21:20:49 -0400313EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
316{
David Howellsdea655c2015-03-17 22:26:15 +0000317 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Deepa Dinamani078cd822016-09-14 07:48:04 -0700319 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700320 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400321 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dget(dentry);
323 d_instantiate(dentry, inode);
324 return 0;
325}
Al Viro12f38872013-09-15 21:20:49 -0400326EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328int simple_empty(struct dentry *dentry)
329{
330 struct dentry *child;
331 int ret = 0;
332
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100333 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400334 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100335 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
336 if (simple_positive(child)) {
337 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100339 }
340 spin_unlock(&child->d_lock);
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 ret = 1;
343out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100344 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return ret;
346}
Al Viro12f38872013-09-15 21:20:49 -0400347EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349int simple_unlink(struct inode *dir, struct dentry *dentry)
350{
David Howellsdea655c2015-03-17 22:26:15 +0000351 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Deepa Dinamani078cd822016-09-14 07:48:04 -0700353 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700354 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dput(dentry);
356 return 0;
357}
Al Viro12f38872013-09-15 21:20:49 -0400358EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360int simple_rmdir(struct inode *dir, struct dentry *dentry)
361{
362 if (!simple_empty(dentry))
363 return -ENOTEMPTY;
364
David Howellsdea655c2015-03-17 22:26:15 +0000365 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700367 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
Al Viro12f38872013-09-15 21:20:49 -0400370EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200373 struct inode *new_dir, struct dentry *new_dentry,
374 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
David Howellsdea655c2015-03-17 22:26:15 +0000376 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000377 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200379 if (flags & ~RENAME_NOREPLACE)
380 return -EINVAL;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!simple_empty(new_dentry))
383 return -ENOTEMPTY;
384
David Howellsdea655c2015-03-17 22:26:15 +0000385 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400387 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000388 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700389 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700392 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700393 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -0700397 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
Al Viro12f38872013-09-15 21:20:49 -0400401EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000403/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200404 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000405 * @dentry: dentry
406 * @iattr: iattr structure
407 *
408 * Returns 0 on success, -error on failure.
409 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200410 * simple_setattr is a simple ->setattr implementation without a proper
411 * implementation of size changes.
412 *
413 * It can either be used for in-memory filesystems or special files
414 * on simple regular filesystems. Anything that needs to change on-disk
415 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000416 */
417int simple_setattr(struct dentry *dentry, struct iattr *iattr)
418{
David Howellsdea655c2015-03-17 22:26:15 +0000419 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000420 int error;
421
Jan Kara31051c82016-05-26 16:55:18 +0200422 error = setattr_prepare(dentry, iattr);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000423 if (error)
424 return error;
425
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200426 if (iattr->ia_valid & ATTR_SIZE)
427 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200428 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200429 mark_inode_dirty(inode);
430 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000431}
432EXPORT_SYMBOL(simple_setattr);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434int simple_readpage(struct file *file, struct page *page)
435{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700436 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 flush_dcache_page(page);
438 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unlock_page(page);
440 return 0;
441}
Al Viro12f38872013-09-15 21:20:49 -0400442EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Nick Pigginafddba42007-10-16 01:25:01 -0700444int simple_write_begin(struct file *file, struct address_space *mapping,
445 loff_t pos, unsigned len, unsigned flags,
446 struct page **pagep, void **fsdata)
447{
448 struct page *page;
449 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700450
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300451 index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700452
Nick Piggin54566b22009-01-04 12:00:53 -0800453 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700454 if (!page)
455 return -ENOMEM;
456
457 *pagep = page;
458
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300459 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
460 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200461
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300462 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200463 }
464 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700465}
Al Viro12f38872013-09-15 21:20:49 -0400466EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700467
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200468/**
469 * simple_write_end - .write_end helper for non-block-device FSes
470 * @available: See .write_end of address_space_operations
471 * @file: "
472 * @mapping: "
473 * @pos: "
474 * @len: "
475 * @copied: "
476 * @page: "
477 * @fsdata: "
478 *
479 * simple_write_end does the minimum needed for updating a page after writing is
480 * done. It has the same API signature as the .write_end of
481 * address_space_operations vector. So it can just be set onto .write_end for
482 * FSes that don't need any other processing. i_mutex is assumed to be held.
483 * Block based filesystems should use generic_write_end().
484 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
485 * is not called, so a filesystem that actually does store data in .write_inode
486 * should extend on what's done here with a call to mark_inode_dirty() in the
487 * case that i_size has changed.
Al Viro04fff642016-08-29 22:39:56 -0400488 *
489 * Use *ONLY* with simple_readpage()
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200490 */
491int simple_write_end(struct file *file, struct address_space *mapping,
492 loff_t pos, unsigned len, unsigned copied,
493 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200496 loff_t last_pos = pos + copied;
497
498 /* zero the stale part of the page if we did a short copy */
Al Viro04fff642016-08-29 22:39:56 -0400499 if (!PageUptodate(page)) {
500 if (copied < len) {
501 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200502
Al Viro04fff642016-08-29 22:39:56 -0400503 zero_user(page, from + copied, len - copied);
504 }
Nick Piggin955eff52007-02-20 13:58:08 -0800505 SetPageUptodate(page);
Al Viro04fff642016-08-29 22:39:56 -0400506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800509 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200511 if (last_pos > inode->i_size)
512 i_size_write(inode, last_pos);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700515 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300516 put_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700517
518 return copied;
519}
Al Viro12f38872013-09-15 21:20:49 -0400520EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700521
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700522/*
523 * the inodes created here are not hashed. If you use iunique to generate
524 * unique inode values later for this filesystem, then you must take care
525 * to pass it an appropriate max_reserved value to avoid collisions.
526 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200527int simple_fill_super(struct super_block *s, unsigned long magic,
Eric Biggerscda37122017-03-25 21:15:37 -0700528 const struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct inode *inode;
531 struct dentry *root;
532 struct dentry *dentry;
533 int i;
534
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300535 s->s_blocksize = PAGE_SIZE;
536 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800538 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 s->s_time_gran = 1;
540
541 inode = new_inode(s);
542 if (!inode)
543 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700544 /*
545 * because the root inode is 1, the files array must not contain an
546 * entry at index 1
547 */
548 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 inode->i_mode = S_IFDIR | 0755;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700550 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 inode->i_op = &simple_dir_inode_operations;
552 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200553 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500554 root = d_make_root(inode);
555 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 for (i = 0; !files->name || files->name[0]; i++, files++) {
558 if (!files->name)
559 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700560
561 /* warn if it tries to conflict with the root inode */
562 if (unlikely(i == 1))
563 printk(KERN_WARNING "%s: %s passed in a files array"
564 "with an index of 1!\n", __func__,
565 s->s_type->name);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dentry = d_alloc_name(root, files->name);
568 if (!dentry)
569 goto out;
570 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300571 if (!inode) {
572 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 inode->i_mode = S_IFREG | files->mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700576 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 inode->i_fop = files->ops;
578 inode->i_ino = i;
579 d_add(dentry, inode);
580 }
581 s->s_root = root;
582 return 0;
583out:
584 d_genocide(root);
Al Viro640946f2012-04-02 19:22:25 -0400585 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 dput(root);
587 return -ENOMEM;
588}
Al Viro12f38872013-09-15 21:20:49 -0400589EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591static DEFINE_SPINLOCK(pin_fs_lock);
592
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400593int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct vfsmount *mnt = NULL;
596 spin_lock(&pin_fs_lock);
597 if (unlikely(!*mount)) {
598 spin_unlock(&pin_fs_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800599 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (IS_ERR(mnt))
601 return PTR_ERR(mnt);
602 spin_lock(&pin_fs_lock);
603 if (!*mount)
604 *mount = mnt;
605 }
606 mntget(*mount);
607 ++*count;
608 spin_unlock(&pin_fs_lock);
609 mntput(mnt);
610 return 0;
611}
Al Viro12f38872013-09-15 21:20:49 -0400612EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614void simple_release_fs(struct vfsmount **mount, int *count)
615{
616 struct vfsmount *mnt;
617 spin_lock(&pin_fs_lock);
618 mnt = *mount;
619 if (!--*count)
620 *mount = NULL;
621 spin_unlock(&pin_fs_lock);
622 mntput(mnt);
623}
Al Viro12f38872013-09-15 21:20:49 -0400624EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700626/**
627 * simple_read_from_buffer - copy data from the buffer to user space
628 * @to: the user space buffer to read to
629 * @count: the maximum number of bytes to read
630 * @ppos: the current position in the buffer
631 * @from: the buffer to read from
632 * @available: the size of the buffer
633 *
634 * The simple_read_from_buffer() function reads up to @count bytes from the
635 * buffer @from at offset @ppos into the user space address starting at @to.
636 *
637 * On success, the number of bytes read is returned and the offset @ppos is
638 * advanced by this number, or negative value is returned on error.
639 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
641 const void *from, size_t available)
642{
643 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700644 size_t ret;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (pos < 0)
647 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700648 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650 if (count > available - pos)
651 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700652 ret = copy_to_user(to, from + pos, count);
653 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700655 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *ppos = pos + count;
657 return count;
658}
Al Viro12f38872013-09-15 21:20:49 -0400659EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700661/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200662 * simple_write_to_buffer - copy data from user space to the buffer
663 * @to: the buffer to write to
664 * @available: the size of the buffer
665 * @ppos: the current position in the buffer
666 * @from: the user space buffer to read from
667 * @count: the maximum number of bytes to read
668 *
669 * The simple_write_to_buffer() function reads up to @count bytes from the user
670 * space address starting at @from into the buffer @to at offset @ppos.
671 *
672 * On success, the number of bytes written is returned and the offset @ppos is
673 * advanced by this number, or negative value is returned on error.
674 **/
675ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
676 const void __user *from, size_t count)
677{
678 loff_t pos = *ppos;
679 size_t res;
680
681 if (pos < 0)
682 return -EINVAL;
683 if (pos >= available || !count)
684 return 0;
685 if (count > available - pos)
686 count = available - pos;
687 res = copy_from_user(to + pos, from, count);
688 if (res == count)
689 return -EFAULT;
690 count -= res;
691 *ppos = pos + count;
692 return count;
693}
Al Viro12f38872013-09-15 21:20:49 -0400694EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200695
696/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700697 * memory_read_from_buffer - copy data from the buffer
698 * @to: the kernel space buffer to read to
699 * @count: the maximum number of bytes to read
700 * @ppos: the current position in the buffer
701 * @from: the buffer to read from
702 * @available: the size of the buffer
703 *
704 * The memory_read_from_buffer() function reads up to @count bytes from the
705 * buffer @from at offset @ppos into the kernel space address starting at @to.
706 *
707 * On success, the number of bytes read is returned and the offset @ppos is
708 * advanced by this number, or negative value is returned on error.
709 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700710ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
711 const void *from, size_t available)
712{
713 loff_t pos = *ppos;
714
715 if (pos < 0)
716 return -EINVAL;
717 if (pos >= available)
718 return 0;
719 if (count > available - pos)
720 count = available - pos;
721 memcpy(to, from + pos, count);
722 *ppos = pos + count;
723
724 return count;
725}
Al Viro12f38872013-09-15 21:20:49 -0400726EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
729 * Transaction based IO.
730 * The file expects a single write which triggers the transaction, and then
731 * possibly a read which collects the result - which is stored in a
732 * file-local buffer.
733 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100734
735void simple_transaction_set(struct file *file, size_t n)
736{
737 struct simple_transaction_argresp *ar = file->private_data;
738
739 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
740
741 /*
742 * The barrier ensures that ar->size will really remain zero until
743 * ar->data is ready for reading.
744 */
745 smp_mb();
746 ar->size = n;
747}
Al Viro12f38872013-09-15 21:20:49 -0400748EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
751{
752 struct simple_transaction_argresp *ar;
753 static DEFINE_SPINLOCK(simple_transaction_lock);
754
755 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
756 return ERR_PTR(-EFBIG);
757
758 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
759 if (!ar)
760 return ERR_PTR(-ENOMEM);
761
762 spin_lock(&simple_transaction_lock);
763
764 /* only one write allowed per open */
765 if (file->private_data) {
766 spin_unlock(&simple_transaction_lock);
767 free_page((unsigned long)ar);
768 return ERR_PTR(-EBUSY);
769 }
770
771 file->private_data = ar;
772
773 spin_unlock(&simple_transaction_lock);
774
775 if (copy_from_user(ar->data, buf, size))
776 return ERR_PTR(-EFAULT);
777
778 return ar->data;
779}
Al Viro12f38872013-09-15 21:20:49 -0400780EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
783{
784 struct simple_transaction_argresp *ar = file->private_data;
785
786 if (!ar)
787 return 0;
788 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
789}
Al Viro12f38872013-09-15 21:20:49 -0400790EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792int simple_transaction_release(struct inode *inode, struct file *file)
793{
794 free_page((unsigned long)file->private_data);
795 return 0;
796}
Al Viro12f38872013-09-15 21:20:49 -0400797EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200799/* Simple attribute files */
800
801struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800802 int (*get)(void *, u64 *);
803 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200804 char get_buf[24]; /* enough to store a u64 and "\n\0" */
805 char set_buf[24];
806 void *data;
807 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800808 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200809};
810
811/* simple_attr_open is called by an actual attribute open file operation
812 * to set the attribute specific access operations. */
813int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800814 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200815 const char *fmt)
816{
817 struct simple_attr *attr;
818
819 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
820 if (!attr)
821 return -ENOMEM;
822
823 attr->get = get;
824 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700825 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200826 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800827 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200828
829 file->private_data = attr;
830
831 return nonseekable_open(inode, file);
832}
Al Viro12f38872013-09-15 21:20:49 -0400833EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200834
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800835int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200836{
837 kfree(file->private_data);
838 return 0;
839}
Al Viro12f38872013-09-15 21:20:49 -0400840EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200841
842/* read from the buffer that is filled with the get function */
843ssize_t simple_attr_read(struct file *file, char __user *buf,
844 size_t len, loff_t *ppos)
845{
846 struct simple_attr *attr;
847 size_t size;
848 ssize_t ret;
849
850 attr = file->private_data;
851
852 if (!attr->get)
853 return -EACCES;
854
Christoph Hellwig92613032008-02-08 04:20:27 -0800855 ret = mutex_lock_interruptible(&attr->mutex);
856 if (ret)
857 return ret;
858
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800859 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200860 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800861 } else { /* first read */
862 u64 val;
863 ret = attr->get(attr->data, &val);
864 if (ret)
865 goto out;
866
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200867 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800868 attr->fmt, (unsigned long long)val);
869 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200870
871 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800872out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800873 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200874 return ret;
875}
Al Viro12f38872013-09-15 21:20:49 -0400876EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200877
878/* interpret the buffer as a number to call the set function with */
879ssize_t simple_attr_write(struct file *file, const char __user *buf,
880 size_t len, loff_t *ppos)
881{
882 struct simple_attr *attr;
883 u64 val;
884 size_t size;
885 ssize_t ret;
886
887 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200888 if (!attr->set)
889 return -EACCES;
890
Christoph Hellwig92613032008-02-08 04:20:27 -0800891 ret = mutex_lock_interruptible(&attr->mutex);
892 if (ret)
893 return ret;
894
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200895 ret = -EFAULT;
896 size = min(sizeof(attr->set_buf) - 1, len);
897 if (copy_from_user(attr->set_buf, buf, size))
898 goto out;
899
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200900 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700901 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700902 ret = attr->set(attr->data, val);
903 if (ret == 0)
904 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200905out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800906 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200907 return ret;
908}
Al Viro12f38872013-09-15 21:20:49 -0400909EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200910
Christoph Hellwig25961102007-10-21 16:42:05 -0700911/**
912 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
913 * @sb: filesystem to do the file handle conversion on
914 * @fid: file handle to convert
915 * @fh_len: length of the file handle in bytes
916 * @fh_type: type of file handle
917 * @get_inode: filesystem callback to retrieve inode
918 *
919 * This function decodes @fid as long as it has one of the well-known
920 * Linux filehandle types and calls @get_inode on it to retrieve the
921 * inode for the object specified in the file handle.
922 */
923struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
924 int fh_len, int fh_type, struct inode *(*get_inode)
925 (struct super_block *sb, u64 ino, u32 gen))
926{
927 struct inode *inode = NULL;
928
929 if (fh_len < 2)
930 return NULL;
931
932 switch (fh_type) {
933 case FILEID_INO32_GEN:
934 case FILEID_INO32_GEN_PARENT:
935 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
936 break;
937 }
938
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200939 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700940}
941EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
942
943/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800944 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700945 * @sb: filesystem to do the file handle conversion on
946 * @fid: file handle to convert
947 * @fh_len: length of the file handle in bytes
948 * @fh_type: type of file handle
949 * @get_inode: filesystem callback to retrieve inode
950 *
951 * This function decodes @fid as long as it has one of the well-known
952 * Linux filehandle types and calls @get_inode on it to retrieve the
953 * inode for the _parent_ object specified in the file handle if it
954 * is specified in the file handle, or NULL otherwise.
955 */
956struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
957 int fh_len, int fh_type, struct inode *(*get_inode)
958 (struct super_block *sb, u64 ino, u32 gen))
959{
960 struct inode *inode = NULL;
961
962 if (fh_len <= 2)
963 return NULL;
964
965 switch (fh_type) {
966 case FILEID_INO32_GEN_PARENT:
967 inode = get_inode(sb, fid->i32.parent_ino,
968 (fh_len > 3 ? fid->i32.parent_gen : 0));
969 break;
970 }
971
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200972 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700973}
974EXPORT_SYMBOL_GPL(generic_fh_to_parent);
975
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200976/**
Fabian Frederickac13a822014-06-04 16:06:27 -0700977 * __generic_file_fsync - generic fsync implementation for simple filesystems
978 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200979 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -0700980 * @start: start offset in bytes
981 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200982 * @datasync: only synchronize essential metadata if true
983 *
984 * This is a generic implementation of the fsync method for simple
985 * filesystems which track all non-inode metadata in the buffers list
986 * hanging off the address_space structure.
987 */
Fabian Frederickac13a822014-06-04 16:06:27 -0700988int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
989 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -0400990{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200991 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -0400992 int err;
993 int ret;
994
Jeff Layton383aa542017-07-06 07:02:29 -0400995 err = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -0400996 if (err)
997 return err;
998
Al Viro59551022016-01-22 15:40:57 -0500999 inode_lock(inode);
Al Virod5aacad2009-06-07 14:56:44 -04001000 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001001 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -04001002 goto out;
Al Virod5aacad2009-06-07 14:56:44 -04001003 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -04001004 goto out;
Al Virod5aacad2009-06-07 14:56:44 -04001005
Christoph Hellwigc37650162010-10-06 10:48:20 +02001006 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -04001007 if (ret == 0)
1008 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -07001009
Josef Bacik02c24a82011-07-16 20:44:56 -04001010out:
Al Viro59551022016-01-22 15:40:57 -05001011 inode_unlock(inode);
Jeff Layton383aa542017-07-06 07:02:29 -04001012 /* check and advance again to catch errors after syncing out buffers */
1013 err = file_check_and_advance_wb_err(file);
1014 if (ret == 0)
1015 ret = err;
1016 return ret;
Al Virod5aacad2009-06-07 14:56:44 -04001017}
Fabian Frederickac13a822014-06-04 16:06:27 -07001018EXPORT_SYMBOL(__generic_file_fsync);
1019
1020/**
1021 * generic_file_fsync - generic fsync implementation for simple filesystems
1022 * with flush
1023 * @file: file to synchronize
1024 * @start: start offset in bytes
1025 * @end: end offset in bytes (inclusive)
1026 * @datasync: only synchronize essential metadata if true
1027 *
1028 */
1029
1030int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1031 int datasync)
1032{
1033 struct inode *inode = file->f_mapping->host;
1034 int err;
1035
1036 err = __generic_file_fsync(file, start, end, datasync);
1037 if (err)
1038 return err;
1039 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1040}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001041EXPORT_SYMBOL(generic_file_fsync);
1042
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001043/**
1044 * generic_check_addressable - Check addressability of file system
1045 * @blocksize_bits: log of file system block size
1046 * @num_blocks: number of blocks in file system
1047 *
1048 * Determine whether a file system with @num_blocks blocks (and a
1049 * block size of 2**@blocksize_bits) is addressable by the sector_t
1050 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1051 */
1052int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1053{
1054 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -07001055 u64 last_fs_page =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001056 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001057
1058 if (unlikely(num_blocks == 0))
1059 return 0;
1060
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001061 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001062 return -EINVAL;
1063
Joel Beckera33f13e2010-08-16 12:10:17 -07001064 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1065 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001066 return -EFBIG;
1067 }
1068 return 0;
1069}
1070EXPORT_SYMBOL(generic_check_addressable);
1071
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001072/*
1073 * No-op implementation of ->fsync for in-memory filesystems.
1074 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001075int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001076{
1077 return 0;
1078}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001079EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001080
Dan Williamsf44c7762018-03-07 15:26:44 -08001081int noop_set_page_dirty(struct page *page)
1082{
1083 /*
1084 * Unlike __set_page_dirty_no_writeback that handles dirty page
1085 * tracking in the page object, dax does all dirty tracking in
1086 * the inode address_space in response to mkwrite faults. In the
1087 * dax case we only need to worry about potentially dirty CPU
1088 * caches, not dirty page cache pages to write back.
1089 *
1090 * This callback is defined to prevent fallback to
1091 * __set_page_dirty_buffers() in set_page_dirty().
1092 */
1093 return 0;
1094}
1095EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1096
1097void noop_invalidatepage(struct page *page, unsigned int offset,
1098 unsigned int length)
1099{
1100 /*
1101 * There is no page cache to invalidate in the dax case, however
1102 * we need this callback defined to prevent falling back to
1103 * block_invalidatepage() in do_invalidatepage().
1104 */
1105}
1106EXPORT_SYMBOL_GPL(noop_invalidatepage);
1107
1108ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1109{
1110 /*
1111 * iomap based filesystems support direct I/O without need for
1112 * this callback. However, it still needs to be set in
1113 * inode->a_ops so that open/fcntl know that direct I/O is
1114 * generally supported.
1115 */
1116 return -EINVAL;
1117}
1118EXPORT_SYMBOL_GPL(noop_direct_IO);
1119
Al Virofceef392015-12-29 15:58:39 -05001120/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1121void kfree_link(void *p)
Al Viro87dc8002013-09-16 10:30:04 -04001122{
Al Virofceef392015-12-29 15:58:39 -05001123 kfree(p);
Al Viro87dc8002013-09-16 10:30:04 -04001124}
Al Virofceef392015-12-29 15:58:39 -05001125EXPORT_SYMBOL(kfree_link);
Al Viro69878432013-10-02 22:35:11 -04001126
1127/*
1128 * nop .set_page_dirty method so that people can use .page_mkwrite on
1129 * anon inodes.
1130 */
1131static int anon_set_page_dirty(struct page *page)
1132{
1133 return 0;
1134};
1135
1136/*
1137 * A single inode exists for all anon_inode files. Contrary to pipes,
1138 * anon_inode inodes have no associated per-instance data, so we need
1139 * only allocate one of them.
1140 */
1141struct inode *alloc_anon_inode(struct super_block *s)
1142{
1143 static const struct address_space_operations anon_aops = {
1144 .set_page_dirty = anon_set_page_dirty,
1145 };
1146 struct inode *inode = new_inode_pseudo(s);
1147
1148 if (!inode)
1149 return ERR_PTR(-ENOMEM);
1150
1151 inode->i_ino = get_next_ino();
1152 inode->i_mapping->a_ops = &anon_aops;
1153
1154 /*
1155 * Mark the inode dirty from the very beginning,
1156 * that way it will never be moved to the dirty
1157 * list because mark_inode_dirty() will think
1158 * that it already _is_ on the dirty list.
1159 */
1160 inode->i_state = I_DIRTY;
1161 inode->i_mode = S_IRUSR | S_IWUSR;
1162 inode->i_uid = current_fsuid();
1163 inode->i_gid = current_fsgid();
1164 inode->i_flags |= S_PRIVATE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001165 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro69878432013-10-02 22:35:11 -04001166 return inode;
1167}
1168EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001169
1170/**
1171 * simple_nosetlease - generic helper for prohibiting leases
1172 * @filp: file pointer
1173 * @arg: type of lease to obtain
1174 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001175 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001176 *
1177 * Generic helper for filesystems that do not wish to allow leases to be set.
1178 * All arguments are ignored and it just returns -EINVAL.
1179 */
1180int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001181simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1182 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001183{
1184 return -EINVAL;
1185}
1186EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001187
Eric Biggers6ee97062019-04-11 16:16:30 -07001188/**
1189 * simple_get_link - generic helper to get the target of "fast" symlinks
1190 * @dentry: not used here
1191 * @inode: the symlink inode
1192 * @done: not used here
1193 *
1194 * Generic helper for filesystems to use for symlink inodes where a pointer to
1195 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1196 * since as an optimization the path lookup code uses any non-NULL ->i_link
1197 * directly, without calling ->get_link(). But ->get_link() still must be set,
1198 * to mark the inode_operations as being for a symlink.
1199 *
1200 * Return: the symlink target
1201 */
Al Viro6b255392015-11-17 10:20:54 -05001202const char *simple_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05001203 struct delayed_call *done)
Al Viro61ba64f2015-05-02 09:54:06 -04001204{
Al Viro6b255392015-11-17 10:20:54 -05001205 return inode->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001206}
Al Viro6b255392015-11-17 10:20:54 -05001207EXPORT_SYMBOL(simple_get_link);
Al Viro61ba64f2015-05-02 09:54:06 -04001208
1209const struct inode_operations simple_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001210 .get_link = simple_get_link,
Al Viro61ba64f2015-05-02 09:54:06 -04001211};
1212EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001213
1214/*
1215 * Operations for a permanently empty directory.
1216 */
1217static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1218{
1219 return ERR_PTR(-ENOENT);
1220}
1221
David Howellsa528d352017-01-31 16:46:22 +00001222static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1223 u32 request_mask, unsigned int query_flags)
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001224{
David Howellsa528d352017-01-31 16:46:22 +00001225 struct inode *inode = d_inode(path->dentry);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001226 generic_fillattr(inode, stat);
1227 return 0;
1228}
1229
1230static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1231{
1232 return -EPERM;
1233}
1234
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001235static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1236{
1237 return -EOPNOTSUPP;
1238}
1239
1240static const struct inode_operations empty_dir_inode_operations = {
1241 .lookup = empty_dir_lookup,
1242 .permission = generic_permission,
1243 .setattr = empty_dir_setattr,
1244 .getattr = empty_dir_getattr,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001245 .listxattr = empty_dir_listxattr,
1246};
1247
1248static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1249{
1250 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1251 return generic_file_llseek_size(file, offset, whence, 2, 2);
1252}
1253
1254static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1255{
1256 dir_emit_dots(file, ctx);
1257 return 0;
1258}
1259
1260static const struct file_operations empty_dir_operations = {
1261 .llseek = empty_dir_llseek,
1262 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001263 .iterate_shared = empty_dir_readdir,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001264 .fsync = noop_fsync,
1265};
1266
1267
1268void make_empty_dir_inode(struct inode *inode)
1269{
1270 set_nlink(inode, 2);
1271 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1272 inode->i_uid = GLOBAL_ROOT_UID;
1273 inode->i_gid = GLOBAL_ROOT_GID;
1274 inode->i_rdev = 0;
Eric W. Biederman4b75de862015-08-12 15:00:12 -05001275 inode->i_size = 0;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001276 inode->i_blkbits = PAGE_SHIFT;
1277 inode->i_blocks = 0;
1278
1279 inode->i_op = &empty_dir_inode_operations;
Andreas Gruenbacherf5c24432016-09-29 17:48:41 +02001280 inode->i_opflags &= ~IOP_XATTR;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001281 inode->i_fop = &empty_dir_operations;
1282}
1283
1284bool is_empty_dir_inode(struct inode *inode)
1285{
1286 return (inode->i_fop == &empty_dir_operations) &&
1287 (inode->i_op == &empty_dir_inode_operations);
1288}