blob: edef70d3543868ac95a739818129231ac7c42a58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/libfs.c
3 * Library for filesystems writers.
4 */
5
Fabian Frederickac13a822014-06-04 16:06:27 -07006#include <linux/blkdev.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mount.h>
12#include <linux/vfs.h>
npiggin@suse.de7bb46a62010-05-27 01:05:33 +100013#include <linux/quotaops.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080014#include <linux/mutex.h>
Al Viro87dc8002013-09-16 10:30:04 -040015#include <linux/namei.h>
Christoph Hellwig25961102007-10-21 16:42:05 -070016#include <linux/exportfs.h>
Al Virod5aacad2009-06-07 14:56:44 -040017#include <linux/writeback.h>
Al Viroff01bb42011-09-16 02:31:11 -040018#include <linux/buffer_head.h> /* sync_mapping_buffers */
David Howells31d6d5c2019-03-25 16:38:23 +000019#include <linux/fs_context.h>
20#include <linux/pseudo_fs.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080021
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Al Viroa4464db2011-07-07 15:03:58 -040024#include "internal.h"
25
David Howellsa528d352017-01-31 16:46:22 +000026int simple_getattr(const struct path *path, struct kstat *stat,
27 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
David Howellsa528d352017-01-31 16:46:22 +000029 struct inode *inode = d_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 generic_fillattr(inode, stat);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030031 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return 0;
33}
Al Viro12f38872013-09-15 21:20:49 -040034EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David Howells726c3342006-06-23 02:02:58 -070036int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
David Howells726c3342006-06-23 02:02:58 -070038 buf->f_type = dentry->d_sb->s_magic;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030039 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 buf->f_namelen = NAME_MAX;
41 return 0;
42}
Al Viro12f38872013-09-15 21:20:49 -040043EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * Retaining negative dentries for an in-memory filesystem just wastes
47 * memory and lookup time: arrange for them to be deleted immediately.
48 */
Al Virob26d4cd2013-10-25 18:47:37 -040049int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 return 1;
52}
Al Virob26d4cd2013-10-25 18:47:37 -040053EXPORT_SYMBOL(always_delete_dentry);
54
55const struct dentry_operations simple_dentry_operations = {
56 .d_delete = always_delete_dentry,
57};
58EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * Lookup the data. This is trivial - if the dentry didn't already
62 * exist, we know it is negative. Set d_op to delete negative dentries.
63 */
Al Viro00cd8dd2012-06-10 17:13:09 -040064struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (dentry->d_name.len > NAME_MAX)
67 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040068 if (!dentry->d_sb->s_d_op)
69 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 d_add(dentry, NULL);
71 return NULL;
72}
Al Viro12f38872013-09-15 21:20:49 -040073EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075int dcache_dir_open(struct inode *inode, struct file *file)
76{
Al Viroba65dc52016-06-10 11:32:47 -040077 file->private_data = d_alloc_cursor(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 return file->private_data ? 0 : -ENOMEM;
80}
Al Viro12f38872013-09-15 21:20:49 -040081EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83int dcache_dir_close(struct inode *inode, struct file *file)
84{
85 dput(file->private_data);
86 return 0;
87}
Al Viro12f38872013-09-15 21:20:49 -040088EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Al Viro4f42c1b2016-06-06 19:37:13 -040090/* parent is locked at least shared */
91static struct dentry *next_positive(struct dentry *parent,
92 struct list_head *from,
93 int count)
94{
Al Viroebaaa802016-06-06 20:55:34 -040095 unsigned *seq = &parent->d_inode->i_dir_seq, n;
96 struct dentry *res;
Al Viro4f42c1b2016-06-06 19:37:13 -040097 struct list_head *p;
Al Viroebaaa802016-06-06 20:55:34 -040098 bool skipped;
99 int i;
Al Viro4f42c1b2016-06-06 19:37:13 -0400100
Al Viroebaaa802016-06-06 20:55:34 -0400101retry:
102 i = count;
103 skipped = false;
104 n = smp_load_acquire(seq) & ~1;
105 res = NULL;
106 rcu_read_lock();
Al Viro4f42c1b2016-06-06 19:37:13 -0400107 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
108 struct dentry *d = list_entry(p, struct dentry, d_child);
Al Viroebaaa802016-06-06 20:55:34 -0400109 if (!simple_positive(d)) {
110 skipped = true;
111 } else if (!--i) {
Al Viro4f42c1b2016-06-06 19:37:13 -0400112 res = d;
113 break;
114 }
115 }
Al Viroebaaa802016-06-06 20:55:34 -0400116 rcu_read_unlock();
117 if (skipped) {
118 smp_rmb();
119 if (unlikely(*seq != n))
120 goto retry;
121 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400122 return res;
123}
124
125static void move_cursor(struct dentry *cursor, struct list_head *after)
126{
127 struct dentry *parent = cursor->d_parent;
Al Viroebaaa802016-06-06 20:55:34 -0400128 unsigned n, *seq = &parent->d_inode->i_dir_seq;
Al Viro4f42c1b2016-06-06 19:37:13 -0400129 spin_lock(&parent->d_lock);
Al Viroebaaa802016-06-06 20:55:34 -0400130 for (;;) {
131 n = *seq;
132 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
133 break;
134 cpu_relax();
135 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400136 __list_del(cursor->d_child.prev, cursor->d_child.next);
137 if (after)
138 list_add(&cursor->d_child, after);
139 else
140 list_add_tail(&cursor->d_child, &parent->d_subdirs);
Al Viroebaaa802016-06-06 20:55:34 -0400141 smp_store_release(seq, n + 2);
Al Viro4f42c1b2016-06-06 19:37:13 -0400142 spin_unlock(&parent->d_lock);
143}
144
Andrew Morton965c8e52012-12-17 15:59:39 -0800145loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100147 struct dentry *dentry = file->f_path.dentry;
Andrew Morton965c8e52012-12-17 15:59:39 -0800148 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 case 1:
150 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600151 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case 0:
153 if (offset >= 0)
154 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600155 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EINVAL;
158 }
159 if (offset != file->f_pos) {
160 file->f_pos = offset;
161 if (file->f_pos >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400163 struct dentry *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 loff_t n = file->f_pos - 2;
165
Al Viro274f5b02016-06-06 18:55:57 -0400166 inode_lock_shared(dentry->d_inode);
Al Viro4f42c1b2016-06-06 19:37:13 -0400167 to = next_positive(dentry, &dentry->d_subdirs, n);
168 move_cursor(cursor, to ? &to->d_child : NULL);
Al Viro274f5b02016-06-06 18:55:57 -0400169 inode_unlock_shared(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return offset;
173}
Al Viro12f38872013-09-15 21:20:49 -0400174EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* Relationship between i_mode and the DT_xxx types */
177static inline unsigned char dt_type(struct inode *inode)
178{
179 return (inode->i_mode >> 12) & 15;
180}
181
182/*
183 * Directory is locked and all positive dentries in it are safe, since
184 * for ramfs-type trees they can't go away without unlink() or rmdir(),
185 * both impossible due to the lock on directory.
186 */
187
Al Viro5f99f4e2013-05-15 20:23:06 -0400188int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Al Viro5f99f4e2013-05-15 20:23:06 -0400190 struct dentry *dentry = file->f_path.dentry;
191 struct dentry *cursor = file->private_data;
Al Viro4f42c1b2016-06-06 19:37:13 -0400192 struct list_head *p = &cursor->d_child;
193 struct dentry *next;
194 bool moved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Al Viro5f99f4e2013-05-15 20:23:06 -0400196 if (!dir_emit_dots(file, ctx))
197 return 0;
Al Viro4f42c1b2016-06-06 19:37:13 -0400198
Al Viro5f99f4e2013-05-15 20:23:06 -0400199 if (ctx->pos == 2)
Al Viro4f42c1b2016-06-06 19:37:13 -0400200 p = &dentry->d_subdirs;
201 while ((next = next_positive(dentry, p, 1)) != NULL) {
Al Viro5f99f4e2013-05-15 20:23:06 -0400202 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000203 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro4f42c1b2016-06-06 19:37:13 -0400204 break;
205 moved = true;
206 p = &next->d_child;
Al Viro5f99f4e2013-05-15 20:23:06 -0400207 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Al Viro4f42c1b2016-06-06 19:37:13 -0400209 if (moved)
210 move_cursor(cursor, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
Al Viro12f38872013-09-15 21:20:49 -0400213EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
216{
217 return -EISDIR;
218}
Al Viro12f38872013-09-15 21:20:49 -0400219EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800221const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .open = dcache_dir_open,
223 .release = dcache_dir_close,
224 .llseek = dcache_dir_lseek,
225 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400226 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200227 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
Al Viro12f38872013-09-15 21:20:49 -0400229EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800231const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .lookup = simple_lookup,
233};
Al Viro12f38872013-09-15 21:20:49 -0400234EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Hugh Dickins759b9772007-03-05 00:30:28 -0800236static const struct super_operations simple_super_operations = {
237 .statfs = simple_statfs,
238};
239
David Howells31d6d5c2019-03-25 16:38:23 +0000240static int pseudo_fs_get_tree(struct fs_context *fc)
241{
242 struct pseudo_fs_context *ctx = fc->fs_private;
243 struct dentry *root;
244
245 root = mount_pseudo_xattr(fc->fs_type,
246 ctx->ops, ctx->xattr,
247 ctx->dops, ctx->magic);
248 if (IS_ERR(root))
249 return PTR_ERR(root);
250
251 fc->root = root;
252 return 0;
253}
254
255static void pseudo_fs_free(struct fs_context *fc)
256{
257 kfree(fc->fs_private);
258}
259
260static const struct fs_context_operations pseudo_fs_context_ops = {
261 .free = pseudo_fs_free,
262 .get_tree = pseudo_fs_get_tree,
263};
264
265/*
266 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
267 * will never be mountable)
268 */
269struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
270 unsigned long magic)
271{
272 struct pseudo_fs_context *ctx;
273
274 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
275 if (likely(ctx)) {
276 ctx->magic = magic;
277 fc->fs_private = ctx;
278 fc->ops = &pseudo_fs_context_ops;
279 }
280 return ctx;
281}
282EXPORT_SYMBOL(init_pseudo);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
286 * will never be mountable)
287 */
Al Viro1f58bb12019-05-20 13:44:57 +0100288struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200289 const struct super_operations *ops, const struct xattr_handler **xattr,
Al Viroc74a1cb2011-01-12 16:59:34 -0500290 const struct dentry_operations *dops, unsigned long magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
David Howells9249e172012-06-25 12:55:37 +0100292 struct super_block *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct inode *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800295 s = sget_userns(fs_type, NULL, set_anon_super, SB_KERNMOUNT|SB_NOUSER,
Eric W. Biederman75422722017-01-04 17:37:27 +1300296 &init_user_ns, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (IS_ERR(s))
Al Viro51139ad2010-07-25 23:47:46 +0400298 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Jeff Layton89a4eb42009-08-18 14:11:08 -0700300 s->s_maxbytes = MAX_LFS_FILESIZE;
Alex Nixon3971e1a2008-07-29 22:33:03 -0700301 s->s_blocksize = PAGE_SIZE;
302 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800304 s->s_op = ops ? ops : &simple_super_operations;
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200305 s->s_xattr = xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 s->s_time_gran = 1;
307 root = new_inode(s);
308 if (!root)
309 goto Enomem;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700310 /*
311 * since this is the first inode, make it number 1. New inodes created
312 * after this must take care not to collide with it (by passing
313 * max_reserved of 1 to iunique).
314 */
315 root->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700317 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
Al Viro1f58bb12019-05-20 13:44:57 +0100318 s->s_root = d_make_root(root);
319 if (!s->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto Enomem;
Al Viroc74a1cb2011-01-12 16:59:34 -0500321 s->s_d_op = dops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800322 s->s_flags |= SB_ACTIVE;
Al Viro51139ad2010-07-25 23:47:46 +0400323 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325Enomem:
Al Viro6f5bbff2009-05-06 01:34:22 -0400326 deactivate_locked_super(s);
Al Viro51139ad2010-07-25 23:47:46 +0400327 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200329EXPORT_SYMBOL(mount_pseudo_xattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Stephen Boyd20955e82012-04-05 14:25:09 -0700331int simple_open(struct inode *inode, struct file *file)
332{
333 if (inode->i_private)
334 file->private_data = inode->i_private;
335 return 0;
336}
Al Viro12f38872013-09-15 21:20:49 -0400337EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
340{
David Howellsdea655c2015-03-17 22:26:15 +0000341 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Deepa Dinamani078cd822016-09-14 07:48:04 -0700343 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700344 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400345 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dget(dentry);
347 d_instantiate(dentry, inode);
348 return 0;
349}
Al Viro12f38872013-09-15 21:20:49 -0400350EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352int simple_empty(struct dentry *dentry)
353{
354 struct dentry *child;
355 int ret = 0;
356
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100357 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400358 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100359 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
360 if (simple_positive(child)) {
361 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100363 }
364 spin_unlock(&child->d_lock);
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ret = 1;
367out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100368 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return ret;
370}
Al Viro12f38872013-09-15 21:20:49 -0400371EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373int simple_unlink(struct inode *dir, struct dentry *dentry)
374{
David Howellsdea655c2015-03-17 22:26:15 +0000375 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Deepa Dinamani078cd822016-09-14 07:48:04 -0700377 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700378 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dput(dentry);
380 return 0;
381}
Al Viro12f38872013-09-15 21:20:49 -0400382EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384int simple_rmdir(struct inode *dir, struct dentry *dentry)
385{
386 if (!simple_empty(dentry))
387 return -ENOTEMPTY;
388
David Howellsdea655c2015-03-17 22:26:15 +0000389 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700391 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
393}
Al Viro12f38872013-09-15 21:20:49 -0400394EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200397 struct inode *new_dir, struct dentry *new_dentry,
398 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
David Howellsdea655c2015-03-17 22:26:15 +0000400 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000401 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Miklos Szeredie0e0be82016-09-27 11:03:57 +0200403 if (flags & ~RENAME_NOREPLACE)
404 return -EINVAL;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!simple_empty(new_dentry))
407 return -ENOTEMPTY;
408
David Howellsdea655c2015-03-17 22:26:15 +0000409 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400411 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000412 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700413 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700416 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700417 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -0700421 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return 0;
424}
Al Viro12f38872013-09-15 21:20:49 -0400425EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000427/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200428 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000429 * @dentry: dentry
430 * @iattr: iattr structure
431 *
432 * Returns 0 on success, -error on failure.
433 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200434 * simple_setattr is a simple ->setattr implementation without a proper
435 * implementation of size changes.
436 *
437 * It can either be used for in-memory filesystems or special files
438 * on simple regular filesystems. Anything that needs to change on-disk
439 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000440 */
441int simple_setattr(struct dentry *dentry, struct iattr *iattr)
442{
David Howellsdea655c2015-03-17 22:26:15 +0000443 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000444 int error;
445
Jan Kara31051c82016-05-26 16:55:18 +0200446 error = setattr_prepare(dentry, iattr);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000447 if (error)
448 return error;
449
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200450 if (iattr->ia_valid & ATTR_SIZE)
451 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200452 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200453 mark_inode_dirty(inode);
454 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000455}
456EXPORT_SYMBOL(simple_setattr);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458int simple_readpage(struct file *file, struct page *page)
459{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700460 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 flush_dcache_page(page);
462 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unlock_page(page);
464 return 0;
465}
Al Viro12f38872013-09-15 21:20:49 -0400466EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Nick Pigginafddba42007-10-16 01:25:01 -0700468int simple_write_begin(struct file *file, struct address_space *mapping,
469 loff_t pos, unsigned len, unsigned flags,
470 struct page **pagep, void **fsdata)
471{
472 struct page *page;
473 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700474
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300475 index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700476
Nick Piggin54566b22009-01-04 12:00:53 -0800477 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700478 if (!page)
479 return -ENOMEM;
480
481 *pagep = page;
482
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300483 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
484 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200485
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300486 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200487 }
488 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700489}
Al Viro12f38872013-09-15 21:20:49 -0400490EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700491
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200492/**
493 * simple_write_end - .write_end helper for non-block-device FSes
494 * @available: See .write_end of address_space_operations
495 * @file: "
496 * @mapping: "
497 * @pos: "
498 * @len: "
499 * @copied: "
500 * @page: "
501 * @fsdata: "
502 *
503 * simple_write_end does the minimum needed for updating a page after writing is
504 * done. It has the same API signature as the .write_end of
505 * address_space_operations vector. So it can just be set onto .write_end for
506 * FSes that don't need any other processing. i_mutex is assumed to be held.
507 * Block based filesystems should use generic_write_end().
508 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
509 * is not called, so a filesystem that actually does store data in .write_inode
510 * should extend on what's done here with a call to mark_inode_dirty() in the
511 * case that i_size has changed.
Al Viro04fff642016-08-29 22:39:56 -0400512 *
513 * Use *ONLY* with simple_readpage()
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200514 */
515int simple_write_end(struct file *file, struct address_space *mapping,
516 loff_t pos, unsigned len, unsigned copied,
517 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200520 loff_t last_pos = pos + copied;
521
522 /* zero the stale part of the page if we did a short copy */
Al Viro04fff642016-08-29 22:39:56 -0400523 if (!PageUptodate(page)) {
524 if (copied < len) {
525 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200526
Al Viro04fff642016-08-29 22:39:56 -0400527 zero_user(page, from + copied, len - copied);
528 }
Nick Piggin955eff52007-02-20 13:58:08 -0800529 SetPageUptodate(page);
Al Viro04fff642016-08-29 22:39:56 -0400530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800533 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200535 if (last_pos > inode->i_size)
536 i_size_write(inode, last_pos);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700539 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300540 put_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700541
542 return copied;
543}
Al Viro12f38872013-09-15 21:20:49 -0400544EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700545
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700546/*
547 * the inodes created here are not hashed. If you use iunique to generate
548 * unique inode values later for this filesystem, then you must take care
549 * to pass it an appropriate max_reserved value to avoid collisions.
550 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200551int simple_fill_super(struct super_block *s, unsigned long magic,
Eric Biggerscda37122017-03-25 21:15:37 -0700552 const struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct inode *inode;
555 struct dentry *root;
556 struct dentry *dentry;
557 int i;
558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 s->s_blocksize = PAGE_SIZE;
560 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800562 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 s->s_time_gran = 1;
564
565 inode = new_inode(s);
566 if (!inode)
567 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700568 /*
569 * because the root inode is 1, the files array must not contain an
570 * entry at index 1
571 */
572 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 inode->i_mode = S_IFDIR | 0755;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700574 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 inode->i_op = &simple_dir_inode_operations;
576 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200577 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500578 root = d_make_root(inode);
579 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 for (i = 0; !files->name || files->name[0]; i++, files++) {
582 if (!files->name)
583 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700584
585 /* warn if it tries to conflict with the root inode */
586 if (unlikely(i == 1))
587 printk(KERN_WARNING "%s: %s passed in a files array"
588 "with an index of 1!\n", __func__,
589 s->s_type->name);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 dentry = d_alloc_name(root, files->name);
592 if (!dentry)
593 goto out;
594 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300595 if (!inode) {
596 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 inode->i_mode = S_IFREG | files->mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700600 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 inode->i_fop = files->ops;
602 inode->i_ino = i;
603 d_add(dentry, inode);
604 }
605 s->s_root = root;
606 return 0;
607out:
608 d_genocide(root);
Al Viro640946f22012-04-02 19:22:25 -0400609 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 dput(root);
611 return -ENOMEM;
612}
Al Viro12f38872013-09-15 21:20:49 -0400613EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615static DEFINE_SPINLOCK(pin_fs_lock);
616
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400617int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct vfsmount *mnt = NULL;
620 spin_lock(&pin_fs_lock);
621 if (unlikely(!*mount)) {
622 spin_unlock(&pin_fs_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800623 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (IS_ERR(mnt))
625 return PTR_ERR(mnt);
626 spin_lock(&pin_fs_lock);
627 if (!*mount)
628 *mount = mnt;
629 }
630 mntget(*mount);
631 ++*count;
632 spin_unlock(&pin_fs_lock);
633 mntput(mnt);
634 return 0;
635}
Al Viro12f38872013-09-15 21:20:49 -0400636EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638void simple_release_fs(struct vfsmount **mount, int *count)
639{
640 struct vfsmount *mnt;
641 spin_lock(&pin_fs_lock);
642 mnt = *mount;
643 if (!--*count)
644 *mount = NULL;
645 spin_unlock(&pin_fs_lock);
646 mntput(mnt);
647}
Al Viro12f38872013-09-15 21:20:49 -0400648EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700650/**
651 * simple_read_from_buffer - copy data from the buffer to user space
652 * @to: the user space buffer to read to
653 * @count: the maximum number of bytes to read
654 * @ppos: the current position in the buffer
655 * @from: the buffer to read from
656 * @available: the size of the buffer
657 *
658 * The simple_read_from_buffer() function reads up to @count bytes from the
659 * buffer @from at offset @ppos into the user space address starting at @to.
660 *
661 * On success, the number of bytes read is returned and the offset @ppos is
662 * advanced by this number, or negative value is returned on error.
663 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
665 const void *from, size_t available)
666{
667 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700668 size_t ret;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (pos < 0)
671 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700672 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
674 if (count > available - pos)
675 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700676 ret = copy_to_user(to, from + pos, count);
677 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700679 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *ppos = pos + count;
681 return count;
682}
Al Viro12f38872013-09-15 21:20:49 -0400683EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700685/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200686 * simple_write_to_buffer - copy data from user space to the buffer
687 * @to: the buffer to write to
688 * @available: the size of the buffer
689 * @ppos: the current position in the buffer
690 * @from: the user space buffer to read from
691 * @count: the maximum number of bytes to read
692 *
693 * The simple_write_to_buffer() function reads up to @count bytes from the user
694 * space address starting at @from into the buffer @to at offset @ppos.
695 *
696 * On success, the number of bytes written is returned and the offset @ppos is
697 * advanced by this number, or negative value is returned on error.
698 **/
699ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
700 const void __user *from, size_t count)
701{
702 loff_t pos = *ppos;
703 size_t res;
704
705 if (pos < 0)
706 return -EINVAL;
707 if (pos >= available || !count)
708 return 0;
709 if (count > available - pos)
710 count = available - pos;
711 res = copy_from_user(to + pos, from, count);
712 if (res == count)
713 return -EFAULT;
714 count -= res;
715 *ppos = pos + count;
716 return count;
717}
Al Viro12f38872013-09-15 21:20:49 -0400718EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200719
720/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700721 * memory_read_from_buffer - copy data from the buffer
722 * @to: the kernel space buffer to read to
723 * @count: the maximum number of bytes to read
724 * @ppos: the current position in the buffer
725 * @from: the buffer to read from
726 * @available: the size of the buffer
727 *
728 * The memory_read_from_buffer() function reads up to @count bytes from the
729 * buffer @from at offset @ppos into the kernel space address starting at @to.
730 *
731 * On success, the number of bytes read is returned and the offset @ppos is
732 * advanced by this number, or negative value is returned on error.
733 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700734ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
735 const void *from, size_t available)
736{
737 loff_t pos = *ppos;
738
739 if (pos < 0)
740 return -EINVAL;
741 if (pos >= available)
742 return 0;
743 if (count > available - pos)
744 count = available - pos;
745 memcpy(to, from + pos, count);
746 *ppos = pos + count;
747
748 return count;
749}
Al Viro12f38872013-09-15 21:20:49 -0400750EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * Transaction based IO.
754 * The file expects a single write which triggers the transaction, and then
755 * possibly a read which collects the result - which is stored in a
756 * file-local buffer.
757 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100758
759void simple_transaction_set(struct file *file, size_t n)
760{
761 struct simple_transaction_argresp *ar = file->private_data;
762
763 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
764
765 /*
766 * The barrier ensures that ar->size will really remain zero until
767 * ar->data is ready for reading.
768 */
769 smp_mb();
770 ar->size = n;
771}
Al Viro12f38872013-09-15 21:20:49 -0400772EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
775{
776 struct simple_transaction_argresp *ar;
777 static DEFINE_SPINLOCK(simple_transaction_lock);
778
779 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
780 return ERR_PTR(-EFBIG);
781
782 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
783 if (!ar)
784 return ERR_PTR(-ENOMEM);
785
786 spin_lock(&simple_transaction_lock);
787
788 /* only one write allowed per open */
789 if (file->private_data) {
790 spin_unlock(&simple_transaction_lock);
791 free_page((unsigned long)ar);
792 return ERR_PTR(-EBUSY);
793 }
794
795 file->private_data = ar;
796
797 spin_unlock(&simple_transaction_lock);
798
799 if (copy_from_user(ar->data, buf, size))
800 return ERR_PTR(-EFAULT);
801
802 return ar->data;
803}
Al Viro12f38872013-09-15 21:20:49 -0400804EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
807{
808 struct simple_transaction_argresp *ar = file->private_data;
809
810 if (!ar)
811 return 0;
812 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
813}
Al Viro12f38872013-09-15 21:20:49 -0400814EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816int simple_transaction_release(struct inode *inode, struct file *file)
817{
818 free_page((unsigned long)file->private_data);
819 return 0;
820}
Al Viro12f38872013-09-15 21:20:49 -0400821EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200823/* Simple attribute files */
824
825struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800826 int (*get)(void *, u64 *);
827 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200828 char get_buf[24]; /* enough to store a u64 and "\n\0" */
829 char set_buf[24];
830 void *data;
831 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800832 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200833};
834
835/* simple_attr_open is called by an actual attribute open file operation
836 * to set the attribute specific access operations. */
837int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800838 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200839 const char *fmt)
840{
841 struct simple_attr *attr;
842
843 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
844 if (!attr)
845 return -ENOMEM;
846
847 attr->get = get;
848 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700849 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200850 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800851 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200852
853 file->private_data = attr;
854
855 return nonseekable_open(inode, file);
856}
Al Viro12f38872013-09-15 21:20:49 -0400857EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200858
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800859int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200860{
861 kfree(file->private_data);
862 return 0;
863}
Al Viro12f38872013-09-15 21:20:49 -0400864EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200865
866/* read from the buffer that is filled with the get function */
867ssize_t simple_attr_read(struct file *file, char __user *buf,
868 size_t len, loff_t *ppos)
869{
870 struct simple_attr *attr;
871 size_t size;
872 ssize_t ret;
873
874 attr = file->private_data;
875
876 if (!attr->get)
877 return -EACCES;
878
Christoph Hellwig92613032008-02-08 04:20:27 -0800879 ret = mutex_lock_interruptible(&attr->mutex);
880 if (ret)
881 return ret;
882
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800883 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200884 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800885 } else { /* first read */
886 u64 val;
887 ret = attr->get(attr->data, &val);
888 if (ret)
889 goto out;
890
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200891 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800892 attr->fmt, (unsigned long long)val);
893 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200894
895 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800896out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800897 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200898 return ret;
899}
Al Viro12f38872013-09-15 21:20:49 -0400900EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200901
902/* interpret the buffer as a number to call the set function with */
903ssize_t simple_attr_write(struct file *file, const char __user *buf,
904 size_t len, loff_t *ppos)
905{
906 struct simple_attr *attr;
907 u64 val;
908 size_t size;
909 ssize_t ret;
910
911 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200912 if (!attr->set)
913 return -EACCES;
914
Christoph Hellwig92613032008-02-08 04:20:27 -0800915 ret = mutex_lock_interruptible(&attr->mutex);
916 if (ret)
917 return ret;
918
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200919 ret = -EFAULT;
920 size = min(sizeof(attr->set_buf) - 1, len);
921 if (copy_from_user(attr->set_buf, buf, size))
922 goto out;
923
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200924 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700925 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700926 ret = attr->set(attr->data, val);
927 if (ret == 0)
928 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200929out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800930 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200931 return ret;
932}
Al Viro12f38872013-09-15 21:20:49 -0400933EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200934
Christoph Hellwig25961102007-10-21 16:42:05 -0700935/**
936 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
937 * @sb: filesystem to do the file handle conversion on
938 * @fid: file handle to convert
939 * @fh_len: length of the file handle in bytes
940 * @fh_type: type of file handle
941 * @get_inode: filesystem callback to retrieve inode
942 *
943 * This function decodes @fid as long as it has one of the well-known
944 * Linux filehandle types and calls @get_inode on it to retrieve the
945 * inode for the object specified in the file handle.
946 */
947struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
948 int fh_len, int fh_type, struct inode *(*get_inode)
949 (struct super_block *sb, u64 ino, u32 gen))
950{
951 struct inode *inode = NULL;
952
953 if (fh_len < 2)
954 return NULL;
955
956 switch (fh_type) {
957 case FILEID_INO32_GEN:
958 case FILEID_INO32_GEN_PARENT:
959 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
960 break;
961 }
962
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200963 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700964}
965EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
966
967/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800968 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700969 * @sb: filesystem to do the file handle conversion on
970 * @fid: file handle to convert
971 * @fh_len: length of the file handle in bytes
972 * @fh_type: type of file handle
973 * @get_inode: filesystem callback to retrieve inode
974 *
975 * This function decodes @fid as long as it has one of the well-known
976 * Linux filehandle types and calls @get_inode on it to retrieve the
977 * inode for the _parent_ object specified in the file handle if it
978 * is specified in the file handle, or NULL otherwise.
979 */
980struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
981 int fh_len, int fh_type, struct inode *(*get_inode)
982 (struct super_block *sb, u64 ino, u32 gen))
983{
984 struct inode *inode = NULL;
985
986 if (fh_len <= 2)
987 return NULL;
988
989 switch (fh_type) {
990 case FILEID_INO32_GEN_PARENT:
991 inode = get_inode(sb, fid->i32.parent_ino,
992 (fh_len > 3 ? fid->i32.parent_gen : 0));
993 break;
994 }
995
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200996 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700997}
998EXPORT_SYMBOL_GPL(generic_fh_to_parent);
999
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001000/**
Fabian Frederickac13a822014-06-04 16:06:27 -07001001 * __generic_file_fsync - generic fsync implementation for simple filesystems
1002 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001003 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -07001004 * @start: start offset in bytes
1005 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001006 * @datasync: only synchronize essential metadata if true
1007 *
1008 * This is a generic implementation of the fsync method for simple
1009 * filesystems which track all non-inode metadata in the buffers list
1010 * hanging off the address_space structure.
1011 */
Fabian Frederickac13a822014-06-04 16:06:27 -07001012int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
1013 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -04001014{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001015 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -04001016 int err;
1017 int ret;
1018
Jeff Layton383aa542017-07-06 07:02:29 -04001019 err = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -04001020 if (err)
1021 return err;
1022
Al Viro59551022016-01-22 15:40:57 -05001023 inode_lock(inode);
Al Virod5aacad2009-06-07 14:56:44 -04001024 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001025 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -04001026 goto out;
Al Virod5aacad2009-06-07 14:56:44 -04001027 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -04001028 goto out;
Al Virod5aacad2009-06-07 14:56:44 -04001029
Christoph Hellwigc37650162010-10-06 10:48:20 +02001030 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -04001031 if (ret == 0)
1032 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -07001033
Josef Bacik02c24a82011-07-16 20:44:56 -04001034out:
Al Viro59551022016-01-22 15:40:57 -05001035 inode_unlock(inode);
Jeff Layton383aa542017-07-06 07:02:29 -04001036 /* check and advance again to catch errors after syncing out buffers */
1037 err = file_check_and_advance_wb_err(file);
1038 if (ret == 0)
1039 ret = err;
1040 return ret;
Al Virod5aacad2009-06-07 14:56:44 -04001041}
Fabian Frederickac13a822014-06-04 16:06:27 -07001042EXPORT_SYMBOL(__generic_file_fsync);
1043
1044/**
1045 * generic_file_fsync - generic fsync implementation for simple filesystems
1046 * with flush
1047 * @file: file to synchronize
1048 * @start: start offset in bytes
1049 * @end: end offset in bytes (inclusive)
1050 * @datasync: only synchronize essential metadata if true
1051 *
1052 */
1053
1054int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1055 int datasync)
1056{
1057 struct inode *inode = file->f_mapping->host;
1058 int err;
1059
1060 err = __generic_file_fsync(file, start, end, datasync);
1061 if (err)
1062 return err;
1063 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1064}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001065EXPORT_SYMBOL(generic_file_fsync);
1066
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001067/**
1068 * generic_check_addressable - Check addressability of file system
1069 * @blocksize_bits: log of file system block size
1070 * @num_blocks: number of blocks in file system
1071 *
1072 * Determine whether a file system with @num_blocks blocks (and a
1073 * block size of 2**@blocksize_bits) is addressable by the sector_t
1074 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1075 */
1076int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1077{
1078 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -07001079 u64 last_fs_page =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001080 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001081
1082 if (unlikely(num_blocks == 0))
1083 return 0;
1084
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001086 return -EINVAL;
1087
Joel Beckera33f13e2010-08-16 12:10:17 -07001088 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1089 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07001090 return -EFBIG;
1091 }
1092 return 0;
1093}
1094EXPORT_SYMBOL(generic_check_addressable);
1095
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001096/*
1097 * No-op implementation of ->fsync for in-memory filesystems.
1098 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001099int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001100{
1101 return 0;
1102}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001103EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001104
Dan Williamsf44c7762018-03-07 15:26:44 -08001105int noop_set_page_dirty(struct page *page)
1106{
1107 /*
1108 * Unlike __set_page_dirty_no_writeback that handles dirty page
1109 * tracking in the page object, dax does all dirty tracking in
1110 * the inode address_space in response to mkwrite faults. In the
1111 * dax case we only need to worry about potentially dirty CPU
1112 * caches, not dirty page cache pages to write back.
1113 *
1114 * This callback is defined to prevent fallback to
1115 * __set_page_dirty_buffers() in set_page_dirty().
1116 */
1117 return 0;
1118}
1119EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1120
1121void noop_invalidatepage(struct page *page, unsigned int offset,
1122 unsigned int length)
1123{
1124 /*
1125 * There is no page cache to invalidate in the dax case, however
1126 * we need this callback defined to prevent falling back to
1127 * block_invalidatepage() in do_invalidatepage().
1128 */
1129}
1130EXPORT_SYMBOL_GPL(noop_invalidatepage);
1131
1132ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1133{
1134 /*
1135 * iomap based filesystems support direct I/O without need for
1136 * this callback. However, it still needs to be set in
1137 * inode->a_ops so that open/fcntl know that direct I/O is
1138 * generally supported.
1139 */
1140 return -EINVAL;
1141}
1142EXPORT_SYMBOL_GPL(noop_direct_IO);
1143
Al Virofceef392015-12-29 15:58:39 -05001144/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1145void kfree_link(void *p)
Al Viro87dc8002013-09-16 10:30:04 -04001146{
Al Virofceef392015-12-29 15:58:39 -05001147 kfree(p);
Al Viro87dc8002013-09-16 10:30:04 -04001148}
Al Virofceef392015-12-29 15:58:39 -05001149EXPORT_SYMBOL(kfree_link);
Al Viro69878432013-10-02 22:35:11 -04001150
1151/*
1152 * nop .set_page_dirty method so that people can use .page_mkwrite on
1153 * anon inodes.
1154 */
1155static int anon_set_page_dirty(struct page *page)
1156{
1157 return 0;
1158};
1159
1160/*
1161 * A single inode exists for all anon_inode files. Contrary to pipes,
1162 * anon_inode inodes have no associated per-instance data, so we need
1163 * only allocate one of them.
1164 */
1165struct inode *alloc_anon_inode(struct super_block *s)
1166{
1167 static const struct address_space_operations anon_aops = {
1168 .set_page_dirty = anon_set_page_dirty,
1169 };
1170 struct inode *inode = new_inode_pseudo(s);
1171
1172 if (!inode)
1173 return ERR_PTR(-ENOMEM);
1174
1175 inode->i_ino = get_next_ino();
1176 inode->i_mapping->a_ops = &anon_aops;
1177
1178 /*
1179 * Mark the inode dirty from the very beginning,
1180 * that way it will never be moved to the dirty
1181 * list because mark_inode_dirty() will think
1182 * that it already _is_ on the dirty list.
1183 */
1184 inode->i_state = I_DIRTY;
1185 inode->i_mode = S_IRUSR | S_IWUSR;
1186 inode->i_uid = current_fsuid();
1187 inode->i_gid = current_fsgid();
1188 inode->i_flags |= S_PRIVATE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001189 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro69878432013-10-02 22:35:11 -04001190 return inode;
1191}
1192EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001193
1194/**
1195 * simple_nosetlease - generic helper for prohibiting leases
1196 * @filp: file pointer
1197 * @arg: type of lease to obtain
1198 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001199 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001200 *
1201 * Generic helper for filesystems that do not wish to allow leases to be set.
1202 * All arguments are ignored and it just returns -EINVAL.
1203 */
1204int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001205simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1206 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001207{
1208 return -EINVAL;
1209}
1210EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001211
Eric Biggers6ee97062019-04-11 16:16:30 -07001212/**
1213 * simple_get_link - generic helper to get the target of "fast" symlinks
1214 * @dentry: not used here
1215 * @inode: the symlink inode
1216 * @done: not used here
1217 *
1218 * Generic helper for filesystems to use for symlink inodes where a pointer to
1219 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1220 * since as an optimization the path lookup code uses any non-NULL ->i_link
1221 * directly, without calling ->get_link(). But ->get_link() still must be set,
1222 * to mark the inode_operations as being for a symlink.
1223 *
1224 * Return: the symlink target
1225 */
Al Viro6b255392015-11-17 10:20:54 -05001226const char *simple_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05001227 struct delayed_call *done)
Al Viro61ba64f2015-05-02 09:54:06 -04001228{
Al Viro6b255392015-11-17 10:20:54 -05001229 return inode->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001230}
Al Viro6b255392015-11-17 10:20:54 -05001231EXPORT_SYMBOL(simple_get_link);
Al Viro61ba64f2015-05-02 09:54:06 -04001232
1233const struct inode_operations simple_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001234 .get_link = simple_get_link,
Al Viro61ba64f2015-05-02 09:54:06 -04001235};
1236EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001237
1238/*
1239 * Operations for a permanently empty directory.
1240 */
1241static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1242{
1243 return ERR_PTR(-ENOENT);
1244}
1245
David Howellsa528d352017-01-31 16:46:22 +00001246static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1247 u32 request_mask, unsigned int query_flags)
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001248{
David Howellsa528d352017-01-31 16:46:22 +00001249 struct inode *inode = d_inode(path->dentry);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001250 generic_fillattr(inode, stat);
1251 return 0;
1252}
1253
1254static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1255{
1256 return -EPERM;
1257}
1258
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001259static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1260{
1261 return -EOPNOTSUPP;
1262}
1263
1264static const struct inode_operations empty_dir_inode_operations = {
1265 .lookup = empty_dir_lookup,
1266 .permission = generic_permission,
1267 .setattr = empty_dir_setattr,
1268 .getattr = empty_dir_getattr,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001269 .listxattr = empty_dir_listxattr,
1270};
1271
1272static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1273{
1274 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1275 return generic_file_llseek_size(file, offset, whence, 2, 2);
1276}
1277
1278static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1279{
1280 dir_emit_dots(file, ctx);
1281 return 0;
1282}
1283
1284static const struct file_operations empty_dir_operations = {
1285 .llseek = empty_dir_llseek,
1286 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001287 .iterate_shared = empty_dir_readdir,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001288 .fsync = noop_fsync,
1289};
1290
1291
1292void make_empty_dir_inode(struct inode *inode)
1293{
1294 set_nlink(inode, 2);
1295 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1296 inode->i_uid = GLOBAL_ROOT_UID;
1297 inode->i_gid = GLOBAL_ROOT_GID;
1298 inode->i_rdev = 0;
Eric W. Biederman4b75de862015-08-12 15:00:12 -05001299 inode->i_size = 0;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001300 inode->i_blkbits = PAGE_SHIFT;
1301 inode->i_blocks = 0;
1302
1303 inode->i_op = &empty_dir_inode_operations;
Andreas Gruenbacherf5c24432016-09-29 17:48:41 +02001304 inode->i_opflags &= ~IOP_XATTR;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001305 inode->i_fop = &empty_dir_operations;
1306}
1307
1308bool is_empty_dir_inode(struct inode *inode)
1309{
1310 return (inode->i_fop == &empty_dir_operations) &&
1311 (inode->i_op == &empty_dir_inode_operations);
1312}