blob: 7e52e77692ec5114f3669e20733952310c3e6533 [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 */
Ingo Molnar7cf34c72006-03-23 03:00:36 -080020
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Al Viroa4464db2011-07-07 15:03:58 -040023#include "internal.h"
24
David Howellsa528d352017-01-31 16:46:22 +000025int simple_getattr(const struct path *path, struct kstat *stat,
26 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
David Howellsa528d352017-01-31 16:46:22 +000028 struct inode *inode = d_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 generic_fillattr(inode, stat);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030030 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 0;
32}
Al Viro12f38872013-09-15 21:20:49 -040033EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David Howells726c3342006-06-23 02:02:58 -070035int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
David Howells726c3342006-06-23 02:02:58 -070037 buf->f_type = dentry->d_sb->s_magic;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030038 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 buf->f_namelen = NAME_MAX;
40 return 0;
41}
Al Viro12f38872013-09-15 21:20:49 -040042EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Retaining negative dentries for an in-memory filesystem just wastes
46 * memory and lookup time: arrange for them to be deleted immediately.
47 */
Al Virob26d4cd2013-10-25 18:47:37 -040048int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 return 1;
51}
Al Virob26d4cd2013-10-25 18:47:37 -040052EXPORT_SYMBOL(always_delete_dentry);
53
54const struct dentry_operations simple_dentry_operations = {
55 .d_delete = always_delete_dentry,
56};
57EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * Lookup the data. This is trivial - if the dentry didn't already
61 * exist, we know it is negative. Set d_op to delete negative dentries.
62 */
Al Viro00cd8dd2012-06-10 17:13:09 -040063struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (dentry->d_name.len > NAME_MAX)
66 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040067 if (!dentry->d_sb->s_d_op)
68 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 d_add(dentry, NULL);
70 return NULL;
71}
Al Viro12f38872013-09-15 21:20:49 -040072EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074int dcache_dir_open(struct inode *inode, struct file *file)
75{
Al Viroba65dc52016-06-10 11:32:47 -040076 file->private_data = d_alloc_cursor(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return file->private_data ? 0 : -ENOMEM;
79}
Al Viro12f38872013-09-15 21:20:49 -040080EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82int dcache_dir_close(struct inode *inode, struct file *file)
83{
84 dput(file->private_data);
85 return 0;
86}
Al Viro12f38872013-09-15 21:20:49 -040087EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Al Viro4f42c1b2016-06-06 19:37:13 -040089/* parent is locked at least shared */
90static struct dentry *next_positive(struct dentry *parent,
91 struct list_head *from,
92 int count)
93{
Al Viroebaaa802016-06-06 20:55:34 -040094 unsigned *seq = &parent->d_inode->i_dir_seq, n;
95 struct dentry *res;
Al Viro4f42c1b2016-06-06 19:37:13 -040096 struct list_head *p;
Al Viroebaaa802016-06-06 20:55:34 -040097 bool skipped;
98 int i;
Al Viro4f42c1b2016-06-06 19:37:13 -040099
Al Viroebaaa802016-06-06 20:55:34 -0400100retry:
101 i = count;
102 skipped = false;
103 n = smp_load_acquire(seq) & ~1;
104 res = NULL;
105 rcu_read_lock();
Al Viro4f42c1b2016-06-06 19:37:13 -0400106 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
107 struct dentry *d = list_entry(p, struct dentry, d_child);
Al Viroebaaa802016-06-06 20:55:34 -0400108 if (!simple_positive(d)) {
109 skipped = true;
110 } else if (!--i) {
Al Viro4f42c1b2016-06-06 19:37:13 -0400111 res = d;
112 break;
113 }
114 }
Al Viroebaaa802016-06-06 20:55:34 -0400115 rcu_read_unlock();
116 if (skipped) {
117 smp_rmb();
118 if (unlikely(*seq != n))
119 goto retry;
120 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400121 return res;
122}
123
124static void move_cursor(struct dentry *cursor, struct list_head *after)
125{
126 struct dentry *parent = cursor->d_parent;
Al Viroebaaa802016-06-06 20:55:34 -0400127 unsigned n, *seq = &parent->d_inode->i_dir_seq;
Al Viro4f42c1b2016-06-06 19:37:13 -0400128 spin_lock(&parent->d_lock);
Al Viroebaaa802016-06-06 20:55:34 -0400129 for (;;) {
130 n = *seq;
131 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
132 break;
133 cpu_relax();
134 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400135 __list_del(cursor->d_child.prev, cursor->d_child.next);
136 if (after)
137 list_add(&cursor->d_child, after);
138 else
139 list_add_tail(&cursor->d_child, &parent->d_subdirs);
Al Viroebaaa802016-06-06 20:55:34 -0400140 smp_store_release(seq, n + 2);
Al Viro4f42c1b2016-06-06 19:37:13 -0400141 spin_unlock(&parent->d_lock);
142}
143
Andrew Morton965c8e52012-12-17 15:59:39 -0800144loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100146 struct dentry *dentry = file->f_path.dentry;
Andrew Morton965c8e52012-12-17 15:59:39 -0800147 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 case 1:
149 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600150 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case 0:
152 if (offset >= 0)
153 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600154 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EINVAL;
157 }
158 if (offset != file->f_pos) {
159 file->f_pos = offset;
160 if (file->f_pos >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400162 struct dentry *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 loff_t n = file->f_pos - 2;
164
Al Viro274f5b02016-06-06 18:55:57 -0400165 inode_lock_shared(dentry->d_inode);
Al Viro4f42c1b2016-06-06 19:37:13 -0400166 to = next_positive(dentry, &dentry->d_subdirs, n);
167 move_cursor(cursor, to ? &to->d_child : NULL);
Al Viro274f5b02016-06-06 18:55:57 -0400168 inode_unlock_shared(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return offset;
172}
Al Viro12f38872013-09-15 21:20:49 -0400173EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* Relationship between i_mode and the DT_xxx types */
176static inline unsigned char dt_type(struct inode *inode)
177{
178 return (inode->i_mode >> 12) & 15;
179}
180
181/*
182 * Directory is locked and all positive dentries in it are safe, since
183 * for ramfs-type trees they can't go away without unlink() or rmdir(),
184 * both impossible due to the lock on directory.
185 */
186
Al Viro5f99f4e2013-05-15 20:23:06 -0400187int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Al Viro5f99f4e2013-05-15 20:23:06 -0400189 struct dentry *dentry = file->f_path.dentry;
190 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400191 struct list_head *p = &cursor->d_child;
192 struct dentry *next;
193 bool moved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Al Viro5f99f4e2013-05-15 20:23:06 -0400195 if (!dir_emit_dots(file, ctx))
196 return 0;
Al Viro4f42c1b2016-06-06 19:37:13 -0400197
Al Viro5f99f4e2013-05-15 20:23:06 -0400198 if (ctx->pos == 2)
Al Viro4f42c1b2016-06-06 19:37:13 -0400199 p = &dentry->d_subdirs;
200 while ((next = next_positive(dentry, p, 1)) != NULL) {
Al Viro5f99f4e2013-05-15 20:23:06 -0400201 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000202 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro4f42c1b2016-06-06 19:37:13 -0400203 break;
204 moved = true;
205 p = &next->d_child;
Al Viro5f99f4e2013-05-15 20:23:06 -0400206 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400208 if (moved)
209 move_cursor(cursor, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
Al Viro12f38872013-09-15 21:20:49 -0400212EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
215{
216 return -EISDIR;
217}
Al Viro12f38872013-09-15 21:20:49 -0400218EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800220const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .open = dcache_dir_open,
222 .release = dcache_dir_close,
223 .llseek = dcache_dir_lseek,
224 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400225 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200226 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
Al Viro12f38872013-09-15 21:20:49 -0400228EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800230const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .lookup = simple_lookup,
232};
Al Viro12f38872013-09-15 21:20:49 -0400233EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Hugh Dickins759b9772007-03-05 00:30:28 -0800235static const struct super_operations simple_super_operations = {
236 .statfs = simple_statfs,
237};
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
241 * will never be mountable)
242 */
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200243struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type, char *name,
244 const struct super_operations *ops, const struct xattr_handler **xattr,
Al Viroc74a1cb2011-01-12 16:59:34 -0500245 const struct dentry_operations *dops, unsigned long magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
David Howells9249e172012-06-25 12:55:37 +0100247 struct super_block *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct dentry *dentry;
249 struct inode *root;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700250 struct qstr d_name = QSTR_INIT(name, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800252 s = sget_userns(fs_type, NULL, set_anon_super, SB_KERNMOUNT|SB_NOUSER,
Eric W. Biederman75422722017-01-04 17:37:27 +1300253 &init_user_ns, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (IS_ERR(s))
Al Viro51139ad2010-07-25 23:47:46 +0400255 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Jeff Layton89a4eb42009-08-18 14:11:08 -0700257 s->s_maxbytes = MAX_LFS_FILESIZE;
Alex Nixon3971e1a2008-07-29 22:33:03 -0700258 s->s_blocksize = PAGE_SIZE;
259 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800261 s->s_op = ops ? ops : &simple_super_operations;
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200262 s->s_xattr = xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 s->s_time_gran = 1;
264 root = new_inode(s);
265 if (!root)
266 goto Enomem;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700267 /*
268 * since this is the first inode, make it number 1. New inodes created
269 * after this must take care not to collide with it (by passing
270 * max_reserved of 1 to iunique).
271 */
272 root->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700274 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
Al Viroa4464db2011-07-07 15:03:58 -0400275 dentry = __d_alloc(s, &d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!dentry) {
277 iput(root);
278 goto Enomem;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 d_instantiate(dentry, root);
281 s->s_root = dentry;
Al Viroc74a1cb2011-01-12 16:59:34 -0500282 s->s_d_op = dops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800283 s->s_flags |= SB_ACTIVE;
Al Viro51139ad2010-07-25 23:47:46 +0400284 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286Enomem:
Al Viro6f5bbff2009-05-06 01:34:22 -0400287 deactivate_locked_super(s);
Al Viro51139ad2010-07-25 23:47:46 +0400288 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200290EXPORT_SYMBOL(mount_pseudo_xattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Stephen Boyd20955e82012-04-05 14:25:09 -0700292int simple_open(struct inode *inode, struct file *file)
293{
294 if (inode->i_private)
295 file->private_data = inode->i_private;
296 return 0;
297}
Al Viro12f38872013-09-15 21:20:49 -0400298EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
301{
David Howellsdea655c2015-03-17 22:26:15 +0000302 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Deepa Dinamani078cd822016-09-14 07:48:04 -0700304 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700305 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400306 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 dget(dentry);
308 d_instantiate(dentry, inode);
309 return 0;
310}
Al Viro12f38872013-09-15 21:20:49 -0400311EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313int simple_empty(struct dentry *dentry)
314{
315 struct dentry *child;
316 int ret = 0;
317
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100318 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400319 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100320 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
321 if (simple_positive(child)) {
322 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100324 }
325 spin_unlock(&child->d_lock);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ret = 1;
328out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100329 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return ret;
331}
Al Viro12f38872013-09-15 21:20:49 -0400332EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334int simple_unlink(struct inode *dir, struct dentry *dentry)
335{
David Howellsdea655c2015-03-17 22:26:15 +0000336 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Deepa Dinamani078cd822016-09-14 07:48:04 -0700338 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700339 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dput(dentry);
341 return 0;
342}
Al Viro12f38872013-09-15 21:20:49 -0400343EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345int simple_rmdir(struct inode *dir, struct dentry *dentry)
346{
347 if (!simple_empty(dentry))
348 return -ENOTEMPTY;
349
David Howellsdea655c2015-03-17 22:26:15 +0000350 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700352 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
Al Viro12f38872013-09-15 21:20:49 -0400355EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200358 struct inode *new_dir, struct dentry *new_dentry,
359 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
David Howellsdea655c2015-03-17 22:26:15 +0000361 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000362 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200364 if (flags & ~RENAME_NOREPLACE)
365 return -EINVAL;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (!simple_empty(new_dentry))
368 return -ENOTEMPTY;
369
David Howellsdea655c2015-03-17 22:26:15 +0000370 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400372 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000373 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700374 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700377 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700378 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -0700382 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return 0;
385}
Al Viro12f38872013-09-15 21:20:49 -0400386EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000388/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200389 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000390 * @dentry: dentry
391 * @iattr: iattr structure
392 *
393 * Returns 0 on success, -error on failure.
394 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200395 * simple_setattr is a simple ->setattr implementation without a proper
396 * implementation of size changes.
397 *
398 * It can either be used for in-memory filesystems or special files
399 * on simple regular filesystems. Anything that needs to change on-disk
400 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000401 */
402int simple_setattr(struct dentry *dentry, struct iattr *iattr)
403{
David Howellsdea655c2015-03-17 22:26:15 +0000404 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000405 int error;
406
Jan Kara31051c82016-05-26 16:55:18 +0200407 error = setattr_prepare(dentry, iattr);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000408 if (error)
409 return error;
410
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200411 if (iattr->ia_valid & ATTR_SIZE)
412 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200413 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200414 mark_inode_dirty(inode);
415 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000416}
417EXPORT_SYMBOL(simple_setattr);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419int simple_readpage(struct file *file, struct page *page)
420{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700421 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 flush_dcache_page(page);
423 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unlock_page(page);
425 return 0;
426}
Al Viro12f38872013-09-15 21:20:49 -0400427EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Nick Pigginafddba42007-10-16 01:25:01 -0700429int simple_write_begin(struct file *file, struct address_space *mapping,
430 loff_t pos, unsigned len, unsigned flags,
431 struct page **pagep, void **fsdata)
432{
433 struct page *page;
434 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700435
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300436 index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700437
Nick Piggin54566b22009-01-04 12:00:53 -0800438 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700439 if (!page)
440 return -ENOMEM;
441
442 *pagep = page;
443
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300444 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
445 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200446
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300447 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200448 }
449 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700450}
Al Viro12f38872013-09-15 21:20:49 -0400451EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700452
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200453/**
454 * simple_write_end - .write_end helper for non-block-device FSes
455 * @available: See .write_end of address_space_operations
456 * @file: "
457 * @mapping: "
458 * @pos: "
459 * @len: "
460 * @copied: "
461 * @page: "
462 * @fsdata: "
463 *
464 * simple_write_end does the minimum needed for updating a page after writing is
465 * done. It has the same API signature as the .write_end of
466 * address_space_operations vector. So it can just be set onto .write_end for
467 * FSes that don't need any other processing. i_mutex is assumed to be held.
468 * Block based filesystems should use generic_write_end().
469 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
470 * is not called, so a filesystem that actually does store data in .write_inode
471 * should extend on what's done here with a call to mark_inode_dirty() in the
472 * case that i_size has changed.
Al Viro04fff642016-08-29 22:39:56 -0400473 *
474 * Use *ONLY* with simple_readpage()
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200475 */
476int simple_write_end(struct file *file, struct address_space *mapping,
477 loff_t pos, unsigned len, unsigned copied,
478 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200481 loff_t last_pos = pos + copied;
482
483 /* zero the stale part of the page if we did a short copy */
Al Viro04fff642016-08-29 22:39:56 -0400484 if (!PageUptodate(page)) {
485 if (copied < len) {
486 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200487
Al Viro04fff642016-08-29 22:39:56 -0400488 zero_user(page, from + copied, len - copied);
489 }
Nick Piggin955eff52007-02-20 13:58:08 -0800490 SetPageUptodate(page);
Al Viro04fff642016-08-29 22:39:56 -0400491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800494 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200496 if (last_pos > inode->i_size)
497 i_size_write(inode, last_pos);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700500 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300501 put_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700502
503 return copied;
504}
Al Viro12f38872013-09-15 21:20:49 -0400505EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700506
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700507/*
508 * the inodes created here are not hashed. If you use iunique to generate
509 * unique inode values later for this filesystem, then you must take care
510 * to pass it an appropriate max_reserved value to avoid collisions.
511 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200512int simple_fill_super(struct super_block *s, unsigned long magic,
Eric Biggerscda37122017-03-25 21:15:37 -0700513 const struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct inode *inode;
516 struct dentry *root;
517 struct dentry *dentry;
518 int i;
519
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 s->s_blocksize = PAGE_SIZE;
521 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800523 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 s->s_time_gran = 1;
525
526 inode = new_inode(s);
527 if (!inode)
528 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700529 /*
530 * because the root inode is 1, the files array must not contain an
531 * entry at index 1
532 */
533 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 inode->i_mode = S_IFDIR | 0755;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700535 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 inode->i_op = &simple_dir_inode_operations;
537 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200538 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500539 root = d_make_root(inode);
540 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 for (i = 0; !files->name || files->name[0]; i++, files++) {
543 if (!files->name)
544 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700545
546 /* warn if it tries to conflict with the root inode */
547 if (unlikely(i == 1))
548 printk(KERN_WARNING "%s: %s passed in a files array"
549 "with an index of 1!\n", __func__,
550 s->s_type->name);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 dentry = d_alloc_name(root, files->name);
553 if (!dentry)
554 goto out;
555 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300556 if (!inode) {
557 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 inode->i_mode = S_IFREG | files->mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700561 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 inode->i_fop = files->ops;
563 inode->i_ino = i;
564 d_add(dentry, inode);
565 }
566 s->s_root = root;
567 return 0;
568out:
569 d_genocide(root);
Al Viro640946f22012-04-02 19:22:25 -0400570 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 dput(root);
572 return -ENOMEM;
573}
Al Viro12f38872013-09-15 21:20:49 -0400574EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576static DEFINE_SPINLOCK(pin_fs_lock);
577
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400578int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct vfsmount *mnt = NULL;
581 spin_lock(&pin_fs_lock);
582 if (unlikely(!*mount)) {
583 spin_unlock(&pin_fs_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800584 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (IS_ERR(mnt))
586 return PTR_ERR(mnt);
587 spin_lock(&pin_fs_lock);
588 if (!*mount)
589 *mount = mnt;
590 }
591 mntget(*mount);
592 ++*count;
593 spin_unlock(&pin_fs_lock);
594 mntput(mnt);
595 return 0;
596}
Al Viro12f38872013-09-15 21:20:49 -0400597EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599void simple_release_fs(struct vfsmount **mount, int *count)
600{
601 struct vfsmount *mnt;
602 spin_lock(&pin_fs_lock);
603 mnt = *mount;
604 if (!--*count)
605 *mount = NULL;
606 spin_unlock(&pin_fs_lock);
607 mntput(mnt);
608}
Al Viro12f38872013-09-15 21:20:49 -0400609EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700611/**
612 * simple_read_from_buffer - copy data from the buffer to user space
613 * @to: the user space buffer to read to
614 * @count: the maximum number of bytes to read
615 * @ppos: the current position in the buffer
616 * @from: the buffer to read from
617 * @available: the size of the buffer
618 *
619 * The simple_read_from_buffer() function reads up to @count bytes from the
620 * buffer @from at offset @ppos into the user space address starting at @to.
621 *
622 * On success, the number of bytes read is returned and the offset @ppos is
623 * advanced by this number, or negative value is returned on error.
624 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
626 const void *from, size_t available)
627{
628 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700629 size_t ret;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (pos < 0)
632 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700633 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635 if (count > available - pos)
636 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700637 ret = copy_to_user(to, from + pos, count);
638 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700640 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 *ppos = pos + count;
642 return count;
643}
Al Viro12f38872013-09-15 21:20:49 -0400644EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700646/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200647 * simple_write_to_buffer - copy data from user space to the buffer
648 * @to: the buffer to write to
649 * @available: the size of the buffer
650 * @ppos: the current position in the buffer
651 * @from: the user space buffer to read from
652 * @count: the maximum number of bytes to read
653 *
654 * The simple_write_to_buffer() function reads up to @count bytes from the user
655 * space address starting at @from into the buffer @to at offset @ppos.
656 *
657 * On success, the number of bytes written is returned and the offset @ppos is
658 * advanced by this number, or negative value is returned on error.
659 **/
660ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
661 const void __user *from, size_t count)
662{
663 loff_t pos = *ppos;
664 size_t res;
665
666 if (pos < 0)
667 return -EINVAL;
668 if (pos >= available || !count)
669 return 0;
670 if (count > available - pos)
671 count = available - pos;
672 res = copy_from_user(to + pos, from, count);
673 if (res == count)
674 return -EFAULT;
675 count -= res;
676 *ppos = pos + count;
677 return count;
678}
Al Viro12f38872013-09-15 21:20:49 -0400679EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200680
681/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700682 * memory_read_from_buffer - copy data from the buffer
683 * @to: the kernel space buffer to read to
684 * @count: the maximum number of bytes to read
685 * @ppos: the current position in the buffer
686 * @from: the buffer to read from
687 * @available: the size of the buffer
688 *
689 * The memory_read_from_buffer() function reads up to @count bytes from the
690 * buffer @from at offset @ppos into the kernel space address starting at @to.
691 *
692 * On success, the number of bytes read is returned and the offset @ppos is
693 * advanced by this number, or negative value is returned on error.
694 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700695ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
696 const void *from, size_t available)
697{
698 loff_t pos = *ppos;
699
700 if (pos < 0)
701 return -EINVAL;
702 if (pos >= available)
703 return 0;
704 if (count > available - pos)
705 count = available - pos;
706 memcpy(to, from + pos, count);
707 *ppos = pos + count;
708
709 return count;
710}
Al Viro12f38872013-09-15 21:20:49 -0400711EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Transaction based IO.
715 * The file expects a single write which triggers the transaction, and then
716 * possibly a read which collects the result - which is stored in a
717 * file-local buffer.
718 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100719
720void simple_transaction_set(struct file *file, size_t n)
721{
722 struct simple_transaction_argresp *ar = file->private_data;
723
724 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
725
726 /*
727 * The barrier ensures that ar->size will really remain zero until
728 * ar->data is ready for reading.
729 */
730 smp_mb();
731 ar->size = n;
732}
Al Viro12f38872013-09-15 21:20:49 -0400733EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
736{
737 struct simple_transaction_argresp *ar;
738 static DEFINE_SPINLOCK(simple_transaction_lock);
739
740 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
741 return ERR_PTR(-EFBIG);
742
743 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
744 if (!ar)
745 return ERR_PTR(-ENOMEM);
746
747 spin_lock(&simple_transaction_lock);
748
749 /* only one write allowed per open */
750 if (file->private_data) {
751 spin_unlock(&simple_transaction_lock);
752 free_page((unsigned long)ar);
753 return ERR_PTR(-EBUSY);
754 }
755
756 file->private_data = ar;
757
758 spin_unlock(&simple_transaction_lock);
759
760 if (copy_from_user(ar->data, buf, size))
761 return ERR_PTR(-EFAULT);
762
763 return ar->data;
764}
Al Viro12f38872013-09-15 21:20:49 -0400765EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
768{
769 struct simple_transaction_argresp *ar = file->private_data;
770
771 if (!ar)
772 return 0;
773 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
774}
Al Viro12f38872013-09-15 21:20:49 -0400775EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777int simple_transaction_release(struct inode *inode, struct file *file)
778{
779 free_page((unsigned long)file->private_data);
780 return 0;
781}
Al Viro12f38872013-09-15 21:20:49 -0400782EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200784/* Simple attribute files */
785
786struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800787 int (*get)(void *, u64 *);
788 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200789 char get_buf[24]; /* enough to store a u64 and "\n\0" */
790 char set_buf[24];
791 void *data;
792 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800793 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200794};
795
796/* simple_attr_open is called by an actual attribute open file operation
797 * to set the attribute specific access operations. */
798int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800799 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200800 const char *fmt)
801{
802 struct simple_attr *attr;
803
804 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
805 if (!attr)
806 return -ENOMEM;
807
808 attr->get = get;
809 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700810 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200811 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800812 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200813
814 file->private_data = attr;
815
816 return nonseekable_open(inode, file);
817}
Al Viro12f38872013-09-15 21:20:49 -0400818EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200819
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800820int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200821{
822 kfree(file->private_data);
823 return 0;
824}
Al Viro12f38872013-09-15 21:20:49 -0400825EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200826
827/* read from the buffer that is filled with the get function */
828ssize_t simple_attr_read(struct file *file, char __user *buf,
829 size_t len, loff_t *ppos)
830{
831 struct simple_attr *attr;
832 size_t size;
833 ssize_t ret;
834
835 attr = file->private_data;
836
837 if (!attr->get)
838 return -EACCES;
839
Christoph Hellwig92613032008-02-08 04:20:27 -0800840 ret = mutex_lock_interruptible(&attr->mutex);
841 if (ret)
842 return ret;
843
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800844 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200845 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800846 } else { /* first read */
847 u64 val;
848 ret = attr->get(attr->data, &val);
849 if (ret)
850 goto out;
851
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200852 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800853 attr->fmt, (unsigned long long)val);
854 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200855
856 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800857out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800858 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200859 return ret;
860}
Al Viro12f38872013-09-15 21:20:49 -0400861EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200862
863/* interpret the buffer as a number to call the set function with */
864ssize_t simple_attr_write(struct file *file, const char __user *buf,
865 size_t len, loff_t *ppos)
866{
867 struct simple_attr *attr;
868 u64 val;
869 size_t size;
870 ssize_t ret;
871
872 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200873 if (!attr->set)
874 return -EACCES;
875
Christoph Hellwig92613032008-02-08 04:20:27 -0800876 ret = mutex_lock_interruptible(&attr->mutex);
877 if (ret)
878 return ret;
879
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200880 ret = -EFAULT;
881 size = min(sizeof(attr->set_buf) - 1, len);
882 if (copy_from_user(attr->set_buf, buf, size))
883 goto out;
884
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200885 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700886 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700887 ret = attr->set(attr->data, val);
888 if (ret == 0)
889 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200890out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800891 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200892 return ret;
893}
Al Viro12f38872013-09-15 21:20:49 -0400894EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200895
Christoph Hellwig25961102007-10-21 16:42:05 -0700896/**
897 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
898 * @sb: filesystem to do the file handle conversion on
899 * @fid: file handle to convert
900 * @fh_len: length of the file handle in bytes
901 * @fh_type: type of file handle
902 * @get_inode: filesystem callback to retrieve inode
903 *
904 * This function decodes @fid as long as it has one of the well-known
905 * Linux filehandle types and calls @get_inode on it to retrieve the
906 * inode for the object specified in the file handle.
907 */
908struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
909 int fh_len, int fh_type, struct inode *(*get_inode)
910 (struct super_block *sb, u64 ino, u32 gen))
911{
912 struct inode *inode = NULL;
913
914 if (fh_len < 2)
915 return NULL;
916
917 switch (fh_type) {
918 case FILEID_INO32_GEN:
919 case FILEID_INO32_GEN_PARENT:
920 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
921 break;
922 }
923
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200924 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700925}
926EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
927
928/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800929 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700930 * @sb: filesystem to do the file handle conversion on
931 * @fid: file handle to convert
932 * @fh_len: length of the file handle in bytes
933 * @fh_type: type of file handle
934 * @get_inode: filesystem callback to retrieve inode
935 *
936 * This function decodes @fid as long as it has one of the well-known
937 * Linux filehandle types and calls @get_inode on it to retrieve the
938 * inode for the _parent_ object specified in the file handle if it
939 * is specified in the file handle, or NULL otherwise.
940 */
941struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
942 int fh_len, int fh_type, struct inode *(*get_inode)
943 (struct super_block *sb, u64 ino, u32 gen))
944{
945 struct inode *inode = NULL;
946
947 if (fh_len <= 2)
948 return NULL;
949
950 switch (fh_type) {
951 case FILEID_INO32_GEN_PARENT:
952 inode = get_inode(sb, fid->i32.parent_ino,
953 (fh_len > 3 ? fid->i32.parent_gen : 0));
954 break;
955 }
956
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200957 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700958}
959EXPORT_SYMBOL_GPL(generic_fh_to_parent);
960
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200961/**
Fabian Frederickac13a822014-06-04 16:06:27 -0700962 * __generic_file_fsync - generic fsync implementation for simple filesystems
963 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200964 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -0700965 * @start: start offset in bytes
966 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200967 * @datasync: only synchronize essential metadata if true
968 *
969 * This is a generic implementation of the fsync method for simple
970 * filesystems which track all non-inode metadata in the buffers list
971 * hanging off the address_space structure.
972 */
Fabian Frederickac13a822014-06-04 16:06:27 -0700973int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
974 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -0400975{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200976 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -0400977 int err;
978 int ret;
979
Jeff Layton383aa542017-07-06 07:02:29 -0400980 err = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -0400981 if (err)
982 return err;
983
Al Viro59551022016-01-22 15:40:57 -0500984 inode_lock(inode);
Al Virod5aacad2009-06-07 14:56:44 -0400985 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500986 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -0400987 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400988 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -0400989 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400990
Christoph Hellwigc37650162010-10-06 10:48:20 +0200991 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -0400992 if (ret == 0)
993 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -0700994
Josef Bacik02c24a82011-07-16 20:44:56 -0400995out:
Al Viro59551022016-01-22 15:40:57 -0500996 inode_unlock(inode);
Jeff Layton383aa542017-07-06 07:02:29 -0400997 /* check and advance again to catch errors after syncing out buffers */
998 err = file_check_and_advance_wb_err(file);
999 if (ret == 0)
1000 ret = err;
1001 return ret;
Al Virod5aacad2009-06-07 14:56:44 -04001002}
Fabian Frederickac13a822014-06-04 16:06:27 -07001003EXPORT_SYMBOL(__generic_file_fsync);
1004
1005/**
1006 * generic_file_fsync - generic fsync implementation for simple filesystems
1007 * with flush
1008 * @file: file to synchronize
1009 * @start: start offset in bytes
1010 * @end: end offset in bytes (inclusive)
1011 * @datasync: only synchronize essential metadata if true
1012 *
1013 */
1014
1015int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1016 int datasync)
1017{
1018 struct inode *inode = file->f_mapping->host;
1019 int err;
1020
1021 err = __generic_file_fsync(file, start, end, datasync);
1022 if (err)
1023 return err;
1024 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1025}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001026EXPORT_SYMBOL(generic_file_fsync);
1027
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001028/**
1029 * generic_check_addressable - Check addressability of file system
1030 * @blocksize_bits: log of file system block size
1031 * @num_blocks: number of blocks in file system
1032 *
1033 * Determine whether a file system with @num_blocks blocks (and a
1034 * block size of 2**@blocksize_bits) is addressable by the sector_t
1035 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1036 */
1037int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1038{
1039 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -07001040 u64 last_fs_page =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001042
1043 if (unlikely(num_blocks == 0))
1044 return 0;
1045
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001046 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001047 return -EINVAL;
1048
Joel Beckera33f13e2010-08-16 12:10:17 -07001049 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1050 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001051 return -EFBIG;
1052 }
1053 return 0;
1054}
1055EXPORT_SYMBOL(generic_check_addressable);
1056
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001057/*
1058 * No-op implementation of ->fsync for in-memory filesystems.
1059 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001060int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001061{
1062 return 0;
1063}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001064EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001065
Dan Williamsf44c7762018-03-07 15:26:44 -08001066int noop_set_page_dirty(struct page *page)
1067{
1068 /*
1069 * Unlike __set_page_dirty_no_writeback that handles dirty page
1070 * tracking in the page object, dax does all dirty tracking in
1071 * the inode address_space in response to mkwrite faults. In the
1072 * dax case we only need to worry about potentially dirty CPU
1073 * caches, not dirty page cache pages to write back.
1074 *
1075 * This callback is defined to prevent fallback to
1076 * __set_page_dirty_buffers() in set_page_dirty().
1077 */
1078 return 0;
1079}
1080EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1081
1082void noop_invalidatepage(struct page *page, unsigned int offset,
1083 unsigned int length)
1084{
1085 /*
1086 * There is no page cache to invalidate in the dax case, however
1087 * we need this callback defined to prevent falling back to
1088 * block_invalidatepage() in do_invalidatepage().
1089 */
1090}
1091EXPORT_SYMBOL_GPL(noop_invalidatepage);
1092
1093ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1094{
1095 /*
1096 * iomap based filesystems support direct I/O without need for
1097 * this callback. However, it still needs to be set in
1098 * inode->a_ops so that open/fcntl know that direct I/O is
1099 * generally supported.
1100 */
1101 return -EINVAL;
1102}
1103EXPORT_SYMBOL_GPL(noop_direct_IO);
1104
Al Virofceef392015-12-29 15:58:39 -05001105/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1106void kfree_link(void *p)
Al Viro87dc8002013-09-16 10:30:04 -04001107{
Al Virofceef392015-12-29 15:58:39 -05001108 kfree(p);
Al Viro87dc8002013-09-16 10:30:04 -04001109}
Al Virofceef392015-12-29 15:58:39 -05001110EXPORT_SYMBOL(kfree_link);
Al Viro69878432013-10-02 22:35:11 -04001111
1112/*
1113 * nop .set_page_dirty method so that people can use .page_mkwrite on
1114 * anon inodes.
1115 */
1116static int anon_set_page_dirty(struct page *page)
1117{
1118 return 0;
1119};
1120
1121/*
1122 * A single inode exists for all anon_inode files. Contrary to pipes,
1123 * anon_inode inodes have no associated per-instance data, so we need
1124 * only allocate one of them.
1125 */
1126struct inode *alloc_anon_inode(struct super_block *s)
1127{
1128 static const struct address_space_operations anon_aops = {
1129 .set_page_dirty = anon_set_page_dirty,
1130 };
1131 struct inode *inode = new_inode_pseudo(s);
1132
1133 if (!inode)
1134 return ERR_PTR(-ENOMEM);
1135
1136 inode->i_ino = get_next_ino();
1137 inode->i_mapping->a_ops = &anon_aops;
1138
1139 /*
1140 * Mark the inode dirty from the very beginning,
1141 * that way it will never be moved to the dirty
1142 * list because mark_inode_dirty() will think
1143 * that it already _is_ on the dirty list.
1144 */
1145 inode->i_state = I_DIRTY;
1146 inode->i_mode = S_IRUSR | S_IWUSR;
1147 inode->i_uid = current_fsuid();
1148 inode->i_gid = current_fsgid();
1149 inode->i_flags |= S_PRIVATE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001150 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro69878432013-10-02 22:35:11 -04001151 return inode;
1152}
1153EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001154
1155/**
1156 * simple_nosetlease - generic helper for prohibiting leases
1157 * @filp: file pointer
1158 * @arg: type of lease to obtain
1159 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001160 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001161 *
1162 * Generic helper for filesystems that do not wish to allow leases to be set.
1163 * All arguments are ignored and it just returns -EINVAL.
1164 */
1165int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001166simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1167 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001168{
1169 return -EINVAL;
1170}
1171EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001172
Eric Biggers6ee97062019-04-11 16:16:30 -07001173/**
1174 * simple_get_link - generic helper to get the target of "fast" symlinks
1175 * @dentry: not used here
1176 * @inode: the symlink inode
1177 * @done: not used here
1178 *
1179 * Generic helper for filesystems to use for symlink inodes where a pointer to
1180 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1181 * since as an optimization the path lookup code uses any non-NULL ->i_link
1182 * directly, without calling ->get_link(). But ->get_link() still must be set,
1183 * to mark the inode_operations as being for a symlink.
1184 *
1185 * Return: the symlink target
1186 */
Al Viro6b255392015-11-17 10:20:54 -05001187const char *simple_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05001188 struct delayed_call *done)
Al Viro61ba64f2015-05-02 09:54:06 -04001189{
Al Viro6b255392015-11-17 10:20:54 -05001190 return inode->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001191}
Al Viro6b255392015-11-17 10:20:54 -05001192EXPORT_SYMBOL(simple_get_link);
Al Viro61ba64f2015-05-02 09:54:06 -04001193
1194const struct inode_operations simple_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001195 .get_link = simple_get_link,
Al Viro61ba64f2015-05-02 09:54:06 -04001196};
1197EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001198
1199/*
1200 * Operations for a permanently empty directory.
1201 */
1202static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1203{
1204 return ERR_PTR(-ENOENT);
1205}
1206
David Howellsa528d352017-01-31 16:46:22 +00001207static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1208 u32 request_mask, unsigned int query_flags)
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001209{
David Howellsa528d352017-01-31 16:46:22 +00001210 struct inode *inode = d_inode(path->dentry);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001211 generic_fillattr(inode, stat);
1212 return 0;
1213}
1214
1215static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1216{
1217 return -EPERM;
1218}
1219
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001220static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1221{
1222 return -EOPNOTSUPP;
1223}
1224
1225static const struct inode_operations empty_dir_inode_operations = {
1226 .lookup = empty_dir_lookup,
1227 .permission = generic_permission,
1228 .setattr = empty_dir_setattr,
1229 .getattr = empty_dir_getattr,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001230 .listxattr = empty_dir_listxattr,
1231};
1232
1233static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1234{
1235 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1236 return generic_file_llseek_size(file, offset, whence, 2, 2);
1237}
1238
1239static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1240{
1241 dir_emit_dots(file, ctx);
1242 return 0;
1243}
1244
1245static const struct file_operations empty_dir_operations = {
1246 .llseek = empty_dir_llseek,
1247 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001248 .iterate_shared = empty_dir_readdir,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001249 .fsync = noop_fsync,
1250};
1251
1252
1253void make_empty_dir_inode(struct inode *inode)
1254{
1255 set_nlink(inode, 2);
1256 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1257 inode->i_uid = GLOBAL_ROOT_UID;
1258 inode->i_gid = GLOBAL_ROOT_GID;
1259 inode->i_rdev = 0;
Eric W. Biederman4b75de862015-08-12 15:00:12 -05001260 inode->i_size = 0;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001261 inode->i_blkbits = PAGE_SHIFT;
1262 inode->i_blocks = 0;
1263
1264 inode->i_op = &empty_dir_inode_operations;
Andreas Gruenbacherf5c24432016-09-29 17:48:41 +02001265 inode->i_opflags &= ~IOP_XATTR;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001266 inode->i_fop = &empty_dir_operations;
1267}
1268
1269bool is_empty_dir_inode(struct inode *inode)
1270{
1271 return (inode->i_fop == &empty_dir_operations) &&
1272 (inode->i_op == &empty_dir_inode_operations);
1273}