blob: c9b2850c0f7ce622443a960c2a1dbb9f03177c01 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * fs/libfs.c
4 * Library for filesystems writers.
5 */
6
Fabian Frederickac13a822014-06-04 16:06:27 -07007#include <linux/blkdev.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010011#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mount.h>
13#include <linux/vfs.h>
npiggin@suse.de7bb46a62010-05-27 01:05:33 +100014#include <linux/quotaops.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080015#include <linux/mutex.h>
Al Viro87dc8002013-09-16 10:30:04 -040016#include <linux/namei.h>
Christoph Hellwig25961102007-10-21 16:42:05 -070017#include <linux/exportfs.h>
Al Virod5aacad2009-06-07 14:56:44 -040018#include <linux/writeback.h>
Al Viroff01bb42011-09-16 02:31:11 -040019#include <linux/buffer_head.h> /* sync_mapping_buffers */
David Howells31d6d5c2019-03-25 16:38:23 +000020#include <linux/fs_context.h>
21#include <linux/pseudo_fs.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080022
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Al Viroa4464db2011-07-07 15:03:58 -040025#include "internal.h"
26
David Howellsa528d352017-01-31 16:46:22 +000027int simple_getattr(const struct path *path, struct kstat *stat,
28 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
David Howellsa528d352017-01-31 16:46:22 +000030 struct inode *inode = d_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 generic_fillattr(inode, stat);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030032 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return 0;
34}
Al Viro12f38872013-09-15 21:20:49 -040035EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
David Howells726c3342006-06-23 02:02:58 -070037int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
David Howells726c3342006-06-23 02:02:58 -070039 buf->f_type = dentry->d_sb->s_magic;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030040 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 buf->f_namelen = NAME_MAX;
42 return 0;
43}
Al Viro12f38872013-09-15 21:20:49 -040044EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * Retaining negative dentries for an in-memory filesystem just wastes
48 * memory and lookup time: arrange for them to be deleted immediately.
49 */
Al Virob26d4cd2013-10-25 18:47:37 -040050int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 return 1;
53}
Al Virob26d4cd2013-10-25 18:47:37 -040054EXPORT_SYMBOL(always_delete_dentry);
55
56const struct dentry_operations simple_dentry_operations = {
57 .d_delete = always_delete_dentry,
58};
59EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * Lookup the data. This is trivial - if the dentry didn't already
63 * exist, we know it is negative. Set d_op to delete negative dentries.
64 */
Al Viro00cd8dd2012-06-10 17:13:09 -040065struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (dentry->d_name.len > NAME_MAX)
68 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040069 if (!dentry->d_sb->s_d_op)
70 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 d_add(dentry, NULL);
72 return NULL;
73}
Al Viro12f38872013-09-15 21:20:49 -040074EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076int dcache_dir_open(struct inode *inode, struct file *file)
77{
Al Viroba65dc52016-06-10 11:32:47 -040078 file->private_data = d_alloc_cursor(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 return file->private_data ? 0 : -ENOMEM;
81}
Al Viro12f38872013-09-15 21:20:49 -040082EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84int dcache_dir_close(struct inode *inode, struct file *file)
85{
86 dput(file->private_data);
87 return 0;
88}
Al Viro12f38872013-09-15 21:20:49 -040089EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Al Viro4f42c1b2016-06-06 19:37:13 -040091/* parent is locked at least shared */
92static struct dentry *next_positive(struct dentry *parent,
93 struct list_head *from,
94 int count)
95{
Al Viroebaaa802016-06-06 20:55:34 -040096 unsigned *seq = &parent->d_inode->i_dir_seq, n;
97 struct dentry *res;
Al Viro4f42c1b2016-06-06 19:37:13 -040098 struct list_head *p;
Al Viroebaaa802016-06-06 20:55:34 -040099 bool skipped;
100 int i;
Al Viro4f42c1b2016-06-06 19:37:13 -0400101
Al Viroebaaa802016-06-06 20:55:34 -0400102retry:
103 i = count;
104 skipped = false;
105 n = smp_load_acquire(seq) & ~1;
106 res = NULL;
107 rcu_read_lock();
Al Viro4f42c1b2016-06-06 19:37:13 -0400108 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
109 struct dentry *d = list_entry(p, struct dentry, d_child);
Al Viroebaaa802016-06-06 20:55:34 -0400110 if (!simple_positive(d)) {
111 skipped = true;
112 } else if (!--i) {
Al Viro4f42c1b2016-06-06 19:37:13 -0400113 res = d;
114 break;
115 }
116 }
Al Viroebaaa802016-06-06 20:55:34 -0400117 rcu_read_unlock();
118 if (skipped) {
119 smp_rmb();
120 if (unlikely(*seq != n))
121 goto retry;
122 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400123 return res;
124}
125
126static void move_cursor(struct dentry *cursor, struct list_head *after)
127{
128 struct dentry *parent = cursor->d_parent;
Al Viroebaaa802016-06-06 20:55:34 -0400129 unsigned n, *seq = &parent->d_inode->i_dir_seq;
Al Viro4f42c1b2016-06-06 19:37:13 -0400130 spin_lock(&parent->d_lock);
Al Viroebaaa802016-06-06 20:55:34 -0400131 for (;;) {
132 n = *seq;
133 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
134 break;
135 cpu_relax();
136 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400137 __list_del(cursor->d_child.prev, cursor->d_child.next);
138 if (after)
139 list_add(&cursor->d_child, after);
140 else
141 list_add_tail(&cursor->d_child, &parent->d_subdirs);
Al Viroebaaa802016-06-06 20:55:34 -0400142 smp_store_release(seq, n + 2);
Al Viro4f42c1b2016-06-06 19:37:13 -0400143 spin_unlock(&parent->d_lock);
144}
145
Andrew Morton965c8e52012-12-17 15:59:39 -0800146loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100148 struct dentry *dentry = file->f_path.dentry;
Andrew Morton965c8e52012-12-17 15:59:39 -0800149 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 case 1:
151 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600152 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 case 0:
154 if (offset >= 0)
155 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600156 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
159 }
160 if (offset != file->f_pos) {
161 file->f_pos = offset;
162 if (file->f_pos >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400164 struct dentry *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 loff_t n = file->f_pos - 2;
166
Al Viro274f5b02016-06-06 18:55:57 -0400167 inode_lock_shared(dentry->d_inode);
Al Viro4f42c1b2016-06-06 19:37:13 -0400168 to = next_positive(dentry, &dentry->d_subdirs, n);
169 move_cursor(cursor, to ? &to->d_child : NULL);
Al Viro274f5b02016-06-06 18:55:57 -0400170 inode_unlock_shared(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return offset;
174}
Al Viro12f38872013-09-15 21:20:49 -0400175EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Relationship between i_mode and the DT_xxx types */
178static inline unsigned char dt_type(struct inode *inode)
179{
180 return (inode->i_mode >> 12) & 15;
181}
182
183/*
184 * Directory is locked and all positive dentries in it are safe, since
185 * for ramfs-type trees they can't go away without unlink() or rmdir(),
186 * both impossible due to the lock on directory.
187 */
188
Al Viro5f99f4e2013-05-15 20:23:06 -0400189int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Al Viro5f99f4e2013-05-15 20:23:06 -0400191 struct dentry *dentry = file->f_path.dentry;
192 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400193 struct list_head *p = &cursor->d_child;
194 struct dentry *next;
195 bool moved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Al Viro5f99f4e2013-05-15 20:23:06 -0400197 if (!dir_emit_dots(file, ctx))
198 return 0;
Al Viro4f42c1b2016-06-06 19:37:13 -0400199
Al Viro5f99f4e2013-05-15 20:23:06 -0400200 if (ctx->pos == 2)
Al Viro4f42c1b2016-06-06 19:37:13 -0400201 p = &dentry->d_subdirs;
202 while ((next = next_positive(dentry, p, 1)) != NULL) {
Al Viro5f99f4e2013-05-15 20:23:06 -0400203 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000204 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro4f42c1b2016-06-06 19:37:13 -0400205 break;
206 moved = true;
207 p = &next->d_child;
Al Viro5f99f4e2013-05-15 20:23:06 -0400208 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400210 if (moved)
211 move_cursor(cursor, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213}
Al Viro12f38872013-09-15 21:20:49 -0400214EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
217{
218 return -EISDIR;
219}
Al Viro12f38872013-09-15 21:20:49 -0400220EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800222const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .open = dcache_dir_open,
224 .release = dcache_dir_close,
225 .llseek = dcache_dir_lseek,
226 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400227 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200228 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
Al Viro12f38872013-09-15 21:20:49 -0400230EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800232const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .lookup = simple_lookup,
234};
Al Viro12f38872013-09-15 21:20:49 -0400235EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Hugh Dickins759b9772007-03-05 00:30:28 -0800237static const struct super_operations simple_super_operations = {
238 .statfs = simple_statfs,
239};
240
David Howellsdb2c2462019-03-25 16:38:26 +0000241static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
David Howells31d6d5c2019-03-25 16:38:23 +0000243 struct pseudo_fs_context *ctx = fc->fs_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct inode *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Jeff Layton89a4eb42009-08-18 14:11:08 -0700246 s->s_maxbytes = MAX_LFS_FILESIZE;
Alex Nixon3971e1a2008-07-29 22:33:03 -0700247 s->s_blocksize = PAGE_SIZE;
248 s->s_blocksize_bits = PAGE_SHIFT;
Al Viro8d9e46d2019-05-11 11:43:59 -0400249 s->s_magic = ctx->magic;
250 s->s_op = ctx->ops ?: &simple_super_operations;
251 s->s_xattr = ctx->xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 s->s_time_gran = 1;
253 root = new_inode(s);
254 if (!root)
David Howellsdb2c2462019-03-25 16:38:26 +0000255 return -ENOMEM;
256
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700257 /*
258 * since this is the first inode, make it number 1. New inodes created
259 * after this must take care not to collide with it (by passing
260 * max_reserved of 1 to iunique).
261 */
262 root->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700264 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
Al Viro8d9e46d2019-05-11 11:43:59 -0400265 s->s_root = d_make_root(root);
266 if (!s->s_root)
David Howellsdb2c2462019-03-25 16:38:26 +0000267 return -ENOMEM;
Al Viro8d9e46d2019-05-11 11:43:59 -0400268 s->s_d_op = ctx->dops;
David Howells31d6d5c2019-03-25 16:38:23 +0000269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Al Viro8d9e46d2019-05-11 11:43:59 -0400271
David Howellsdb2c2462019-03-25 16:38:26 +0000272static int pseudo_fs_get_tree(struct fs_context *fc)
273{
Al Viro2ac295d2019-06-01 20:48:55 -0400274 return get_tree_nodev(fc, pseudo_fs_fill_super);
David Howells31d6d5c2019-03-25 16:38:23 +0000275}
276
277static void pseudo_fs_free(struct fs_context *fc)
278{
279 kfree(fc->fs_private);
280}
281
282static const struct fs_context_operations pseudo_fs_context_ops = {
283 .free = pseudo_fs_free,
284 .get_tree = pseudo_fs_get_tree,
285};
286
287/*
288 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
289 * will never be mountable)
290 */
291struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
292 unsigned long magic)
293{
294 struct pseudo_fs_context *ctx;
295
296 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
297 if (likely(ctx)) {
298 ctx->magic = magic;
299 fc->fs_private = ctx;
300 fc->ops = &pseudo_fs_context_ops;
David Howellsdb2c2462019-03-25 16:38:26 +0000301 fc->sb_flags |= SB_NOUSER;
302 fc->global = true;
David Howells31d6d5c2019-03-25 16:38:23 +0000303 }
304 return ctx;
305}
306EXPORT_SYMBOL(init_pseudo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Stephen Boyd20955e82012-04-05 14:25:09 -0700308int simple_open(struct inode *inode, struct file *file)
309{
310 if (inode->i_private)
311 file->private_data = inode->i_private;
312 return 0;
313}
Al Viro12f38872013-09-15 21:20:49 -0400314EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
317{
David Howellsdea655c2015-03-17 22:26:15 +0000318 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Deepa Dinamani078cd822016-09-14 07:48:04 -0700320 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700321 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400322 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 dget(dentry);
324 d_instantiate(dentry, inode);
325 return 0;
326}
Al Viro12f38872013-09-15 21:20:49 -0400327EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329int simple_empty(struct dentry *dentry)
330{
331 struct dentry *child;
332 int ret = 0;
333
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100334 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400335 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100336 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
337 if (simple_positive(child)) {
338 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100340 }
341 spin_unlock(&child->d_lock);
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ret = 1;
344out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100345 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return ret;
347}
Al Viro12f38872013-09-15 21:20:49 -0400348EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350int simple_unlink(struct inode *dir, struct dentry *dentry)
351{
David Howellsdea655c2015-03-17 22:26:15 +0000352 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Deepa Dinamani078cd822016-09-14 07:48:04 -0700354 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700355 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dput(dentry);
357 return 0;
358}
Al Viro12f38872013-09-15 21:20:49 -0400359EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361int simple_rmdir(struct inode *dir, struct dentry *dentry)
362{
363 if (!simple_empty(dentry))
364 return -ENOTEMPTY;
365
David Howellsdea655c2015-03-17 22:26:15 +0000366 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700368 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
Al Viro12f38872013-09-15 21:20:49 -0400371EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200374 struct inode *new_dir, struct dentry *new_dentry,
375 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
David Howellsdea655c2015-03-17 22:26:15 +0000377 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000378 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200380 if (flags & ~RENAME_NOREPLACE)
381 return -EINVAL;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!simple_empty(new_dentry))
384 return -ENOTEMPTY;
385
David Howellsdea655c2015-03-17 22:26:15 +0000386 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400388 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000389 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700390 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700393 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700394 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -0700398 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 return 0;
401}
Al Viro12f38872013-09-15 21:20:49 -0400402EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000404/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200405 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000406 * @dentry: dentry
407 * @iattr: iattr structure
408 *
409 * Returns 0 on success, -error on failure.
410 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200411 * simple_setattr is a simple ->setattr implementation without a proper
412 * implementation of size changes.
413 *
414 * It can either be used for in-memory filesystems or special files
415 * on simple regular filesystems. Anything that needs to change on-disk
416 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000417 */
418int simple_setattr(struct dentry *dentry, struct iattr *iattr)
419{
David Howellsdea655c2015-03-17 22:26:15 +0000420 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000421 int error;
422
Jan Kara31051c82016-05-26 16:55:18 +0200423 error = setattr_prepare(dentry, iattr);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000424 if (error)
425 return error;
426
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200427 if (iattr->ia_valid & ATTR_SIZE)
428 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200429 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200430 mark_inode_dirty(inode);
431 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000432}
433EXPORT_SYMBOL(simple_setattr);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435int simple_readpage(struct file *file, struct page *page)
436{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700437 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 flush_dcache_page(page);
439 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unlock_page(page);
441 return 0;
442}
Al Viro12f38872013-09-15 21:20:49 -0400443EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Nick Pigginafddba42007-10-16 01:25:01 -0700445int simple_write_begin(struct file *file, struct address_space *mapping,
446 loff_t pos, unsigned len, unsigned flags,
447 struct page **pagep, void **fsdata)
448{
449 struct page *page;
450 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700451
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300452 index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700453
Nick Piggin54566b22009-01-04 12:00:53 -0800454 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700455 if (!page)
456 return -ENOMEM;
457
458 *pagep = page;
459
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300460 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
461 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200462
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300463 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200464 }
465 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700466}
Al Viro12f38872013-09-15 21:20:49 -0400467EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700468
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200469/**
470 * simple_write_end - .write_end helper for non-block-device FSes
471 * @available: See .write_end of address_space_operations
472 * @file: "
473 * @mapping: "
474 * @pos: "
475 * @len: "
476 * @copied: "
477 * @page: "
478 * @fsdata: "
479 *
480 * simple_write_end does the minimum needed for updating a page after writing is
481 * done. It has the same API signature as the .write_end of
482 * address_space_operations vector. So it can just be set onto .write_end for
483 * FSes that don't need any other processing. i_mutex is assumed to be held.
484 * Block based filesystems should use generic_write_end().
485 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
486 * is not called, so a filesystem that actually does store data in .write_inode
487 * should extend on what's done here with a call to mark_inode_dirty() in the
488 * case that i_size has changed.
Al Viro04fff642016-08-29 22:39:56 -0400489 *
490 * Use *ONLY* with simple_readpage()
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200491 */
492int simple_write_end(struct file *file, struct address_space *mapping,
493 loff_t pos, unsigned len, unsigned copied,
494 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200497 loff_t last_pos = pos + copied;
498
499 /* zero the stale part of the page if we did a short copy */
Al Viro04fff642016-08-29 22:39:56 -0400500 if (!PageUptodate(page)) {
501 if (copied < len) {
502 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200503
Al Viro04fff642016-08-29 22:39:56 -0400504 zero_user(page, from + copied, len - copied);
505 }
Nick Piggin955eff52007-02-20 13:58:08 -0800506 SetPageUptodate(page);
Al Viro04fff642016-08-29 22:39:56 -0400507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /*
509 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800510 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200512 if (last_pos > inode->i_size)
513 i_size_write(inode, last_pos);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700516 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 put_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700518
519 return copied;
520}
Al Viro12f38872013-09-15 21:20:49 -0400521EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700522
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700523/*
524 * the inodes created here are not hashed. If you use iunique to generate
525 * unique inode values later for this filesystem, then you must take care
526 * to pass it an appropriate max_reserved value to avoid collisions.
527 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200528int simple_fill_super(struct super_block *s, unsigned long magic,
Eric Biggerscda37122017-03-25 21:15:37 -0700529 const struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct inode *inode;
532 struct dentry *root;
533 struct dentry *dentry;
534 int i;
535
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300536 s->s_blocksize = PAGE_SIZE;
537 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800539 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 s->s_time_gran = 1;
541
542 inode = new_inode(s);
543 if (!inode)
544 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700545 /*
546 * because the root inode is 1, the files array must not contain an
547 * entry at index 1
548 */
549 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 inode->i_mode = S_IFDIR | 0755;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700551 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 inode->i_op = &simple_dir_inode_operations;
553 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200554 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500555 root = d_make_root(inode);
556 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 for (i = 0; !files->name || files->name[0]; i++, files++) {
559 if (!files->name)
560 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700561
562 /* warn if it tries to conflict with the root inode */
563 if (unlikely(i == 1))
564 printk(KERN_WARNING "%s: %s passed in a files array"
565 "with an index of 1!\n", __func__,
566 s->s_type->name);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 dentry = d_alloc_name(root, files->name);
569 if (!dentry)
570 goto out;
571 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300572 if (!inode) {
573 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 inode->i_mode = S_IFREG | files->mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700577 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 inode->i_fop = files->ops;
579 inode->i_ino = i;
580 d_add(dentry, inode);
581 }
582 s->s_root = root;
583 return 0;
584out:
585 d_genocide(root);
Al Viro640946f2012-04-02 19:22:25 -0400586 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 dput(root);
588 return -ENOMEM;
589}
Al Viro12f38872013-09-15 21:20:49 -0400590EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592static DEFINE_SPINLOCK(pin_fs_lock);
593
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400594int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct vfsmount *mnt = NULL;
597 spin_lock(&pin_fs_lock);
598 if (unlikely(!*mount)) {
599 spin_unlock(&pin_fs_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800600 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (IS_ERR(mnt))
602 return PTR_ERR(mnt);
603 spin_lock(&pin_fs_lock);
604 if (!*mount)
605 *mount = mnt;
606 }
607 mntget(*mount);
608 ++*count;
609 spin_unlock(&pin_fs_lock);
610 mntput(mnt);
611 return 0;
612}
Al Viro12f38872013-09-15 21:20:49 -0400613EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615void simple_release_fs(struct vfsmount **mount, int *count)
616{
617 struct vfsmount *mnt;
618 spin_lock(&pin_fs_lock);
619 mnt = *mount;
620 if (!--*count)
621 *mount = NULL;
622 spin_unlock(&pin_fs_lock);
623 mntput(mnt);
624}
Al Viro12f38872013-09-15 21:20:49 -0400625EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700627/**
628 * simple_read_from_buffer - copy data from the buffer to user space
629 * @to: the user space buffer to read to
630 * @count: the maximum number of bytes to read
631 * @ppos: the current position in the buffer
632 * @from: the buffer to read from
633 * @available: the size of the buffer
634 *
635 * The simple_read_from_buffer() function reads up to @count bytes from the
636 * buffer @from at offset @ppos into the user space address starting at @to.
637 *
638 * On success, the number of bytes read is returned and the offset @ppos is
639 * advanced by this number, or negative value is returned on error.
640 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
642 const void *from, size_t available)
643{
644 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700645 size_t ret;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (pos < 0)
648 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700649 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651 if (count > available - pos)
652 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700653 ret = copy_to_user(to, from + pos, count);
654 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700656 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 *ppos = pos + count;
658 return count;
659}
Al Viro12f38872013-09-15 21:20:49 -0400660EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700662/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200663 * simple_write_to_buffer - copy data from user space to the buffer
664 * @to: the buffer to write to
665 * @available: the size of the buffer
666 * @ppos: the current position in the buffer
667 * @from: the user space buffer to read from
668 * @count: the maximum number of bytes to read
669 *
670 * The simple_write_to_buffer() function reads up to @count bytes from the user
671 * space address starting at @from into the buffer @to at offset @ppos.
672 *
673 * On success, the number of bytes written is returned and the offset @ppos is
674 * advanced by this number, or negative value is returned on error.
675 **/
676ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
677 const void __user *from, size_t count)
678{
679 loff_t pos = *ppos;
680 size_t res;
681
682 if (pos < 0)
683 return -EINVAL;
684 if (pos >= available || !count)
685 return 0;
686 if (count > available - pos)
687 count = available - pos;
688 res = copy_from_user(to + pos, from, count);
689 if (res == count)
690 return -EFAULT;
691 count -= res;
692 *ppos = pos + count;
693 return count;
694}
Al Viro12f38872013-09-15 21:20:49 -0400695EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200696
697/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700698 * memory_read_from_buffer - copy data from the buffer
699 * @to: the kernel space buffer to read to
700 * @count: the maximum number of bytes to read
701 * @ppos: the current position in the buffer
702 * @from: the buffer to read from
703 * @available: the size of the buffer
704 *
705 * The memory_read_from_buffer() function reads up to @count bytes from the
706 * buffer @from at offset @ppos into the kernel space address starting at @to.
707 *
708 * On success, the number of bytes read is returned and the offset @ppos is
709 * advanced by this number, or negative value is returned on error.
710 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700711ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
712 const void *from, size_t available)
713{
714 loff_t pos = *ppos;
715
716 if (pos < 0)
717 return -EINVAL;
718 if (pos >= available)
719 return 0;
720 if (count > available - pos)
721 count = available - pos;
722 memcpy(to, from + pos, count);
723 *ppos = pos + count;
724
725 return count;
726}
Al Viro12f38872013-09-15 21:20:49 -0400727EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * Transaction based IO.
731 * The file expects a single write which triggers the transaction, and then
732 * possibly a read which collects the result - which is stored in a
733 * file-local buffer.
734 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100735
736void simple_transaction_set(struct file *file, size_t n)
737{
738 struct simple_transaction_argresp *ar = file->private_data;
739
740 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
741
742 /*
743 * The barrier ensures that ar->size will really remain zero until
744 * ar->data is ready for reading.
745 */
746 smp_mb();
747 ar->size = n;
748}
Al Viro12f38872013-09-15 21:20:49 -0400749EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
752{
753 struct simple_transaction_argresp *ar;
754 static DEFINE_SPINLOCK(simple_transaction_lock);
755
756 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
757 return ERR_PTR(-EFBIG);
758
759 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
760 if (!ar)
761 return ERR_PTR(-ENOMEM);
762
763 spin_lock(&simple_transaction_lock);
764
765 /* only one write allowed per open */
766 if (file->private_data) {
767 spin_unlock(&simple_transaction_lock);
768 free_page((unsigned long)ar);
769 return ERR_PTR(-EBUSY);
770 }
771
772 file->private_data = ar;
773
774 spin_unlock(&simple_transaction_lock);
775
776 if (copy_from_user(ar->data, buf, size))
777 return ERR_PTR(-EFAULT);
778
779 return ar->data;
780}
Al Viro12f38872013-09-15 21:20:49 -0400781EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
784{
785 struct simple_transaction_argresp *ar = file->private_data;
786
787 if (!ar)
788 return 0;
789 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
790}
Al Viro12f38872013-09-15 21:20:49 -0400791EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793int simple_transaction_release(struct inode *inode, struct file *file)
794{
795 free_page((unsigned long)file->private_data);
796 return 0;
797}
Al Viro12f38872013-09-15 21:20:49 -0400798EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200800/* Simple attribute files */
801
802struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800803 int (*get)(void *, u64 *);
804 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200805 char get_buf[24]; /* enough to store a u64 and "\n\0" */
806 char set_buf[24];
807 void *data;
808 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800809 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200810};
811
812/* simple_attr_open is called by an actual attribute open file operation
813 * to set the attribute specific access operations. */
814int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800815 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200816 const char *fmt)
817{
818 struct simple_attr *attr;
819
820 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
821 if (!attr)
822 return -ENOMEM;
823
824 attr->get = get;
825 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700826 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200827 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800828 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200829
830 file->private_data = attr;
831
832 return nonseekable_open(inode, file);
833}
Al Viro12f38872013-09-15 21:20:49 -0400834EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200835
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800836int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200837{
838 kfree(file->private_data);
839 return 0;
840}
Al Viro12f38872013-09-15 21:20:49 -0400841EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200842
843/* read from the buffer that is filled with the get function */
844ssize_t simple_attr_read(struct file *file, char __user *buf,
845 size_t len, loff_t *ppos)
846{
847 struct simple_attr *attr;
848 size_t size;
849 ssize_t ret;
850
851 attr = file->private_data;
852
853 if (!attr->get)
854 return -EACCES;
855
Christoph Hellwig92613032008-02-08 04:20:27 -0800856 ret = mutex_lock_interruptible(&attr->mutex);
857 if (ret)
858 return ret;
859
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800860 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200861 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800862 } else { /* first read */
863 u64 val;
864 ret = attr->get(attr->data, &val);
865 if (ret)
866 goto out;
867
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200868 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800869 attr->fmt, (unsigned long long)val);
870 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200871
872 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800873out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800874 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200875 return ret;
876}
Al Viro12f38872013-09-15 21:20:49 -0400877EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200878
879/* interpret the buffer as a number to call the set function with */
880ssize_t simple_attr_write(struct file *file, const char __user *buf,
881 size_t len, loff_t *ppos)
882{
883 struct simple_attr *attr;
884 u64 val;
885 size_t size;
886 ssize_t ret;
887
888 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200889 if (!attr->set)
890 return -EACCES;
891
Christoph Hellwig92613032008-02-08 04:20:27 -0800892 ret = mutex_lock_interruptible(&attr->mutex);
893 if (ret)
894 return ret;
895
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200896 ret = -EFAULT;
897 size = min(sizeof(attr->set_buf) - 1, len);
898 if (copy_from_user(attr->set_buf, buf, size))
899 goto out;
900
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200901 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700902 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700903 ret = attr->set(attr->data, val);
904 if (ret == 0)
905 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200906out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800907 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200908 return ret;
909}
Al Viro12f38872013-09-15 21:20:49 -0400910EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200911
Christoph Hellwig25961102007-10-21 16:42:05 -0700912/**
913 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
914 * @sb: filesystem to do the file handle conversion on
915 * @fid: file handle to convert
916 * @fh_len: length of the file handle in bytes
917 * @fh_type: type of file handle
918 * @get_inode: filesystem callback to retrieve inode
919 *
920 * This function decodes @fid as long as it has one of the well-known
921 * Linux filehandle types and calls @get_inode on it to retrieve the
922 * inode for the object specified in the file handle.
923 */
924struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
925 int fh_len, int fh_type, struct inode *(*get_inode)
926 (struct super_block *sb, u64 ino, u32 gen))
927{
928 struct inode *inode = NULL;
929
930 if (fh_len < 2)
931 return NULL;
932
933 switch (fh_type) {
934 case FILEID_INO32_GEN:
935 case FILEID_INO32_GEN_PARENT:
936 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
937 break;
938 }
939
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200940 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700941}
942EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
943
944/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800945 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700946 * @sb: filesystem to do the file handle conversion on
947 * @fid: file handle to convert
948 * @fh_len: length of the file handle in bytes
949 * @fh_type: type of file handle
950 * @get_inode: filesystem callback to retrieve inode
951 *
952 * This function decodes @fid as long as it has one of the well-known
953 * Linux filehandle types and calls @get_inode on it to retrieve the
954 * inode for the _parent_ object specified in the file handle if it
955 * is specified in the file handle, or NULL otherwise.
956 */
957struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
958 int fh_len, int fh_type, struct inode *(*get_inode)
959 (struct super_block *sb, u64 ino, u32 gen))
960{
961 struct inode *inode = NULL;
962
963 if (fh_len <= 2)
964 return NULL;
965
966 switch (fh_type) {
967 case FILEID_INO32_GEN_PARENT:
968 inode = get_inode(sb, fid->i32.parent_ino,
969 (fh_len > 3 ? fid->i32.parent_gen : 0));
970 break;
971 }
972
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200973 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700974}
975EXPORT_SYMBOL_GPL(generic_fh_to_parent);
976
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200977/**
Fabian Frederickac13a822014-06-04 16:06:27 -0700978 * __generic_file_fsync - generic fsync implementation for simple filesystems
979 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200980 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -0700981 * @start: start offset in bytes
982 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200983 * @datasync: only synchronize essential metadata if true
984 *
985 * This is a generic implementation of the fsync method for simple
986 * filesystems which track all non-inode metadata in the buffers list
987 * hanging off the address_space structure.
988 */
Fabian Frederickac13a822014-06-04 16:06:27 -0700989int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
990 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -0400991{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200992 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -0400993 int err;
994 int ret;
995
Jeff Layton383aa542017-07-06 07:02:29 -0400996 err = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -0400997 if (err)
998 return err;
999
Al Viro59551022016-01-22 15:40:57 -05001000 inode_lock(inode);
Al Virod5aacad2009-06-07 14:56:44 -04001001 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001002 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -04001003 goto out;
Al Virod5aacad2009-06-07 14:56:44 -04001004 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -04001005 goto out;
Al Virod5aacad2009-06-07 14:56:44 -04001006
Christoph Hellwigc37650162010-10-06 10:48:20 +02001007 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -04001008 if (ret == 0)
1009 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -07001010
Josef Bacik02c24a82011-07-16 20:44:56 -04001011out:
Al Viro59551022016-01-22 15:40:57 -05001012 inode_unlock(inode);
Jeff Layton383aa542017-07-06 07:02:29 -04001013 /* check and advance again to catch errors after syncing out buffers */
1014 err = file_check_and_advance_wb_err(file);
1015 if (ret == 0)
1016 ret = err;
1017 return ret;
Al Virod5aacad2009-06-07 14:56:44 -04001018}
Fabian Frederickac13a822014-06-04 16:06:27 -07001019EXPORT_SYMBOL(__generic_file_fsync);
1020
1021/**
1022 * generic_file_fsync - generic fsync implementation for simple filesystems
1023 * with flush
1024 * @file: file to synchronize
1025 * @start: start offset in bytes
1026 * @end: end offset in bytes (inclusive)
1027 * @datasync: only synchronize essential metadata if true
1028 *
1029 */
1030
1031int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1032 int datasync)
1033{
1034 struct inode *inode = file->f_mapping->host;
1035 int err;
1036
1037 err = __generic_file_fsync(file, start, end, datasync);
1038 if (err)
1039 return err;
1040 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1041}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001042EXPORT_SYMBOL(generic_file_fsync);
1043
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001044/**
1045 * generic_check_addressable - Check addressability of file system
1046 * @blocksize_bits: log of file system block size
1047 * @num_blocks: number of blocks in file system
1048 *
1049 * Determine whether a file system with @num_blocks blocks (and a
1050 * block size of 2**@blocksize_bits) is addressable by the sector_t
1051 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1052 */
1053int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1054{
1055 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -07001056 u64 last_fs_page =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001057 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001058
1059 if (unlikely(num_blocks == 0))
1060 return 0;
1061
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001062 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001063 return -EINVAL;
1064
Joel Beckera33f13e2010-08-16 12:10:17 -07001065 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1066 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001067 return -EFBIG;
1068 }
1069 return 0;
1070}
1071EXPORT_SYMBOL(generic_check_addressable);
1072
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001073/*
1074 * No-op implementation of ->fsync for in-memory filesystems.
1075 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001076int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001077{
1078 return 0;
1079}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001080EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001081
Dan Williamsf44c7762018-03-07 15:26:44 -08001082int noop_set_page_dirty(struct page *page)
1083{
1084 /*
1085 * Unlike __set_page_dirty_no_writeback that handles dirty page
1086 * tracking in the page object, dax does all dirty tracking in
1087 * the inode address_space in response to mkwrite faults. In the
1088 * dax case we only need to worry about potentially dirty CPU
1089 * caches, not dirty page cache pages to write back.
1090 *
1091 * This callback is defined to prevent fallback to
1092 * __set_page_dirty_buffers() in set_page_dirty().
1093 */
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1097
1098void noop_invalidatepage(struct page *page, unsigned int offset,
1099 unsigned int length)
1100{
1101 /*
1102 * There is no page cache to invalidate in the dax case, however
1103 * we need this callback defined to prevent falling back to
1104 * block_invalidatepage() in do_invalidatepage().
1105 */
1106}
1107EXPORT_SYMBOL_GPL(noop_invalidatepage);
1108
1109ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1110{
1111 /*
1112 * iomap based filesystems support direct I/O without need for
1113 * this callback. However, it still needs to be set in
1114 * inode->a_ops so that open/fcntl know that direct I/O is
1115 * generally supported.
1116 */
1117 return -EINVAL;
1118}
1119EXPORT_SYMBOL_GPL(noop_direct_IO);
1120
Al Virofceef392015-12-29 15:58:39 -05001121/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1122void kfree_link(void *p)
Al Viro87dc8002013-09-16 10:30:04 -04001123{
Al Virofceef392015-12-29 15:58:39 -05001124 kfree(p);
Al Viro87dc8002013-09-16 10:30:04 -04001125}
Al Virofceef392015-12-29 15:58:39 -05001126EXPORT_SYMBOL(kfree_link);
Al Viro69878432013-10-02 22:35:11 -04001127
1128/*
1129 * nop .set_page_dirty method so that people can use .page_mkwrite on
1130 * anon inodes.
1131 */
1132static int anon_set_page_dirty(struct page *page)
1133{
1134 return 0;
1135};
1136
1137/*
1138 * A single inode exists for all anon_inode files. Contrary to pipes,
1139 * anon_inode inodes have no associated per-instance data, so we need
1140 * only allocate one of them.
1141 */
1142struct inode *alloc_anon_inode(struct super_block *s)
1143{
1144 static const struct address_space_operations anon_aops = {
1145 .set_page_dirty = anon_set_page_dirty,
1146 };
1147 struct inode *inode = new_inode_pseudo(s);
1148
1149 if (!inode)
1150 return ERR_PTR(-ENOMEM);
1151
1152 inode->i_ino = get_next_ino();
1153 inode->i_mapping->a_ops = &anon_aops;
1154
1155 /*
1156 * Mark the inode dirty from the very beginning,
1157 * that way it will never be moved to the dirty
1158 * list because mark_inode_dirty() will think
1159 * that it already _is_ on the dirty list.
1160 */
1161 inode->i_state = I_DIRTY;
1162 inode->i_mode = S_IRUSR | S_IWUSR;
1163 inode->i_uid = current_fsuid();
1164 inode->i_gid = current_fsgid();
1165 inode->i_flags |= S_PRIVATE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001166 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro69878432013-10-02 22:35:11 -04001167 return inode;
1168}
1169EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001170
1171/**
1172 * simple_nosetlease - generic helper for prohibiting leases
1173 * @filp: file pointer
1174 * @arg: type of lease to obtain
1175 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001176 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001177 *
1178 * Generic helper for filesystems that do not wish to allow leases to be set.
1179 * All arguments are ignored and it just returns -EINVAL.
1180 */
1181int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001182simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1183 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001184{
1185 return -EINVAL;
1186}
1187EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001188
Eric Biggers6ee97062019-04-11 16:16:30 -07001189/**
1190 * simple_get_link - generic helper to get the target of "fast" symlinks
1191 * @dentry: not used here
1192 * @inode: the symlink inode
1193 * @done: not used here
1194 *
1195 * Generic helper for filesystems to use for symlink inodes where a pointer to
1196 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1197 * since as an optimization the path lookup code uses any non-NULL ->i_link
1198 * directly, without calling ->get_link(). But ->get_link() still must be set,
1199 * to mark the inode_operations as being for a symlink.
1200 *
1201 * Return: the symlink target
1202 */
Al Viro6b255392015-11-17 10:20:54 -05001203const char *simple_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05001204 struct delayed_call *done)
Al Viro61ba64f2015-05-02 09:54:06 -04001205{
Al Viro6b255392015-11-17 10:20:54 -05001206 return inode->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001207}
Al Viro6b255392015-11-17 10:20:54 -05001208EXPORT_SYMBOL(simple_get_link);
Al Viro61ba64f2015-05-02 09:54:06 -04001209
1210const struct inode_operations simple_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001211 .get_link = simple_get_link,
Al Viro61ba64f2015-05-02 09:54:06 -04001212};
1213EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001214
1215/*
1216 * Operations for a permanently empty directory.
1217 */
1218static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1219{
1220 return ERR_PTR(-ENOENT);
1221}
1222
David Howellsa528d352017-01-31 16:46:22 +00001223static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1224 u32 request_mask, unsigned int query_flags)
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001225{
David Howellsa528d352017-01-31 16:46:22 +00001226 struct inode *inode = d_inode(path->dentry);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001227 generic_fillattr(inode, stat);
1228 return 0;
1229}
1230
1231static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1232{
1233 return -EPERM;
1234}
1235
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001236static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1237{
1238 return -EOPNOTSUPP;
1239}
1240
1241static const struct inode_operations empty_dir_inode_operations = {
1242 .lookup = empty_dir_lookup,
1243 .permission = generic_permission,
1244 .setattr = empty_dir_setattr,
1245 .getattr = empty_dir_getattr,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001246 .listxattr = empty_dir_listxattr,
1247};
1248
1249static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1250{
1251 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1252 return generic_file_llseek_size(file, offset, whence, 2, 2);
1253}
1254
1255static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1256{
1257 dir_emit_dots(file, ctx);
1258 return 0;
1259}
1260
1261static const struct file_operations empty_dir_operations = {
1262 .llseek = empty_dir_llseek,
1263 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001264 .iterate_shared = empty_dir_readdir,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001265 .fsync = noop_fsync,
1266};
1267
1268
1269void make_empty_dir_inode(struct inode *inode)
1270{
1271 set_nlink(inode, 2);
1272 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1273 inode->i_uid = GLOBAL_ROOT_UID;
1274 inode->i_gid = GLOBAL_ROOT_GID;
1275 inode->i_rdev = 0;
Eric W. Biederman4b75de862015-08-12 15:00:12 -05001276 inode->i_size = 0;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001277 inode->i_blkbits = PAGE_SHIFT;
1278 inode->i_blocks = 0;
1279
1280 inode->i_op = &empty_dir_inode_operations;
Andreas Gruenbacherf5c24432016-09-29 17:48:41 +02001281 inode->i_opflags &= ~IOP_XATTR;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001282 inode->i_fop = &empty_dir_operations;
1283}
1284
1285bool is_empty_dir_inode(struct inode *inode)
1286{
1287 return (inode->i_fop == &empty_dir_operations) &&
1288 (inode->i_op == &empty_dir_inode_operations);
1289}