blob: f56acb1e5fbcbc97463a1a9b728e70a09d2e8eea [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mount.h>
11#include <linux/vfs.h>
npiggin@suse.de7bb46a62010-05-27 01:05:33 +100012#include <linux/quotaops.h>
Ingo Molnar7cf34c72006-03-23 03:00:36 -080013#include <linux/mutex.h>
Al Viro87dc8002013-09-16 10:30:04 -040014#include <linux/namei.h>
Christoph Hellwig25961102007-10-21 16:42:05 -070015#include <linux/exportfs.h>
Al Virod5aacad2009-06-07 14:56:44 -040016#include <linux/writeback.h>
Al Viroff01bb42011-09-16 02:31:11 -040017#include <linux/buffer_head.h> /* sync_mapping_buffers */
Ingo Molnar7cf34c72006-03-23 03:00:36 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20
Al Viroa4464db2011-07-07 15:03:58 -040021#include "internal.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
24 struct kstat *stat)
25{
David Howellsdea655c2015-03-17 22:26:15 +000026 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 generic_fillattr(inode, stat);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030028 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return 0;
30}
Al Viro12f38872013-09-15 21:20:49 -040031EXPORT_SYMBOL(simple_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Howells726c3342006-06-23 02:02:58 -070033int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
David Howells726c3342006-06-23 02:02:58 -070035 buf->f_type = dentry->d_sb->s_magic;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030036 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 buf->f_namelen = NAME_MAX;
38 return 0;
39}
Al Viro12f38872013-09-15 21:20:49 -040040EXPORT_SYMBOL(simple_statfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Retaining negative dentries for an in-memory filesystem just wastes
44 * memory and lookup time: arrange for them to be deleted immediately.
45 */
Al Virob26d4cd2013-10-25 18:47:37 -040046int always_delete_dentry(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 return 1;
49}
Al Virob26d4cd2013-10-25 18:47:37 -040050EXPORT_SYMBOL(always_delete_dentry);
51
52const struct dentry_operations simple_dentry_operations = {
53 .d_delete = always_delete_dentry,
54};
55EXPORT_SYMBOL(simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * Lookup the data. This is trivial - if the dentry didn't already
59 * exist, we know it is negative. Set d_op to delete negative dentries.
60 */
Al Viro00cd8dd2012-06-10 17:13:09 -040061struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (dentry->d_name.len > NAME_MAX)
64 return ERR_PTR(-ENAMETOOLONG);
Al Viro74931da2013-07-14 17:43:25 +040065 if (!dentry->d_sb->s_d_op)
66 d_set_d_op(dentry, &simple_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 d_add(dentry, NULL);
68 return NULL;
69}
Al Viro12f38872013-09-15 21:20:49 -040070EXPORT_SYMBOL(simple_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072int dcache_dir_open(struct inode *inode, struct file *file)
73{
Al Viroba65dc52016-06-10 11:32:47 -040074 file->private_data = d_alloc_cursor(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return file->private_data ? 0 : -ENOMEM;
77}
Al Viro12f38872013-09-15 21:20:49 -040078EXPORT_SYMBOL(dcache_dir_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80int dcache_dir_close(struct inode *inode, struct file *file)
81{
82 dput(file->private_data);
83 return 0;
84}
Al Viro12f38872013-09-15 21:20:49 -040085EXPORT_SYMBOL(dcache_dir_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Andrew Morton965c8e52012-12-17 15:59:39 -080087loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110089 struct dentry *dentry = file->f_path.dentry;
Andrew Morton965c8e52012-12-17 15:59:39 -080090 switch (whence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 case 1:
92 offset += file->f_pos;
93 case 0:
94 if (offset >= 0)
95 break;
96 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EINVAL;
98 }
99 if (offset != file->f_pos) {
100 file->f_pos = offset;
101 if (file->f_pos >= 2) {
102 struct list_head *p;
103 struct dentry *cursor = file->private_data;
104 loff_t n = file->f_pos - 2;
105
Al Viro274f5b02016-06-06 18:55:57 -0400106 inode_lock_shared(dentry->d_inode);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100107 spin_lock(&dentry->d_lock);
108 /* d_lock not required for cursor */
Al Viro946e51f2014-10-26 19:19:16 -0400109 list_del(&cursor->d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100110 p = dentry->d_subdirs.next;
111 while (n && p != &dentry->d_subdirs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct dentry *next;
Al Viro946e51f2014-10-26 19:19:16 -0400113 next = list_entry(p, struct dentry, d_child);
Nick Pigginda502952011-01-07 17:49:33 +1100114 if (simple_positive(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 n--;
116 p = p->next;
117 }
Al Viro946e51f2014-10-26 19:19:16 -0400118 list_add_tail(&cursor->d_child, p);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100119 spin_unlock(&dentry->d_lock);
Al Viro274f5b02016-06-06 18:55:57 -0400120 inode_unlock_shared(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return offset;
124}
Al Viro12f38872013-09-15 21:20:49 -0400125EXPORT_SYMBOL(dcache_dir_lseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* Relationship between i_mode and the DT_xxx types */
128static inline unsigned char dt_type(struct inode *inode)
129{
130 return (inode->i_mode >> 12) & 15;
131}
132
133/*
134 * Directory is locked and all positive dentries in it are safe, since
135 * for ramfs-type trees they can't go away without unlink() or rmdir(),
136 * both impossible due to the lock on directory.
137 */
138
Al Viro5f99f4e2013-05-15 20:23:06 -0400139int dcache_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Al Viro5f99f4e2013-05-15 20:23:06 -0400141 struct dentry *dentry = file->f_path.dentry;
142 struct dentry *cursor = file->private_data;
Al Viro946e51f2014-10-26 19:19:16 -0400143 struct list_head *p, *q = &cursor->d_child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Al Viro5f99f4e2013-05-15 20:23:06 -0400145 if (!dir_emit_dots(file, ctx))
146 return 0;
147 spin_lock(&dentry->d_lock);
148 if (ctx->pos == 2)
149 list_move(q, &dentry->d_subdirs);
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700150
Al Viro5f99f4e2013-05-15 20:23:06 -0400151 for (p = q->next; p != &dentry->d_subdirs; p = p->next) {
Al Viro946e51f2014-10-26 19:19:16 -0400152 struct dentry *next = list_entry(p, struct dentry, d_child);
Al Viro274f5b02016-06-06 18:55:57 -0400153 if (!simple_positive(next))
Al Viro5f99f4e2013-05-15 20:23:06 -0400154 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Al Viro5f99f4e2013-05-15 20:23:06 -0400156 spin_unlock(&dentry->d_lock);
157 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
David Howellsdea655c2015-03-17 22:26:15 +0000158 d_inode(next)->i_ino, dt_type(d_inode(next))))
Al Viro5f99f4e2013-05-15 20:23:06 -0400159 return 0;
160 spin_lock(&dentry->d_lock);
Al Viro5f99f4e2013-05-15 20:23:06 -0400161 /* next is still alive */
162 list_move(q, p);
Al Viro5f99f4e2013-05-15 20:23:06 -0400163 p = q;
164 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Al Viro5f99f4e2013-05-15 20:23:06 -0400166 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
Al Viro12f38872013-09-15 21:20:49 -0400169EXPORT_SYMBOL(dcache_readdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
172{
173 return -EISDIR;
174}
Al Viro12f38872013-09-15 21:20:49 -0400175EXPORT_SYMBOL(generic_read_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800177const struct file_operations simple_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .open = dcache_dir_open,
179 .release = dcache_dir_close,
180 .llseek = dcache_dir_lseek,
181 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400182 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200183 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
Al Viro12f38872013-09-15 21:20:49 -0400185EXPORT_SYMBOL(simple_dir_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800187const struct inode_operations simple_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .lookup = simple_lookup,
189};
Al Viro12f38872013-09-15 21:20:49 -0400190EXPORT_SYMBOL(simple_dir_inode_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Hugh Dickins759b9772007-03-05 00:30:28 -0800192static const struct super_operations simple_super_operations = {
193 .statfs = simple_statfs,
194};
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
197 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
198 * will never be mountable)
199 */
Al Viro51139ad2010-07-25 23:47:46 +0400200struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
Al Viroc74a1cb2011-01-12 16:59:34 -0500201 const struct super_operations *ops,
202 const struct dentry_operations *dops, unsigned long magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
David Howells9249e172012-06-25 12:55:37 +0100204 struct super_block *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct dentry *dentry;
206 struct inode *root;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700207 struct qstr d_name = QSTR_INIT(name, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Howells9249e172012-06-25 12:55:37 +0100209 s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (IS_ERR(s))
Al Viro51139ad2010-07-25 23:47:46 +0400211 return ERR_CAST(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jeff Layton89a4eb42009-08-18 14:11:08 -0700213 s->s_maxbytes = MAX_LFS_FILESIZE;
Alex Nixon3971e1a2008-07-29 22:33:03 -0700214 s->s_blocksize = PAGE_SIZE;
215 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800217 s->s_op = ops ? ops : &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 s->s_time_gran = 1;
219 root = new_inode(s);
220 if (!root)
221 goto Enomem;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700222 /*
223 * since this is the first inode, make it number 1. New inodes created
224 * after this must take care not to collide with it (by passing
225 * max_reserved of 1 to iunique).
226 */
227 root->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
Al Viroa4464db2011-07-07 15:03:58 -0400230 dentry = __d_alloc(s, &d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!dentry) {
232 iput(root);
233 goto Enomem;
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 d_instantiate(dentry, root);
236 s->s_root = dentry;
Al Viroc74a1cb2011-01-12 16:59:34 -0500237 s->s_d_op = dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 s->s_flags |= MS_ACTIVE;
Al Viro51139ad2010-07-25 23:47:46 +0400239 return dget(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241Enomem:
Al Viro6f5bbff2009-05-06 01:34:22 -0400242 deactivate_locked_super(s);
Al Viro51139ad2010-07-25 23:47:46 +0400243 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
Al Viro12f38872013-09-15 21:20:49 -0400245EXPORT_SYMBOL(mount_pseudo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Stephen Boyd20955e82012-04-05 14:25:09 -0700247int simple_open(struct inode *inode, struct file *file)
248{
249 if (inode->i_private)
250 file->private_data = inode->i_private;
251 return 0;
252}
Al Viro12f38872013-09-15 21:20:49 -0400253EXPORT_SYMBOL(simple_open);
Stephen Boyd20955e82012-04-05 14:25:09 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
256{
David Howellsdea655c2015-03-17 22:26:15 +0000257 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -0700260 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400261 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dget(dentry);
263 d_instantiate(dentry, inode);
264 return 0;
265}
Al Viro12f38872013-09-15 21:20:49 -0400266EXPORT_SYMBOL(simple_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268int simple_empty(struct dentry *dentry)
269{
270 struct dentry *child;
271 int ret = 0;
272
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100273 spin_lock(&dentry->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400274 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Nick Pigginda502952011-01-07 17:49:33 +1100275 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
276 if (simple_positive(child)) {
277 spin_unlock(&child->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto out;
Nick Pigginda502952011-01-07 17:49:33 +1100279 }
280 spin_unlock(&child->d_lock);
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 ret = 1;
283out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100284 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return ret;
286}
Al Viro12f38872013-09-15 21:20:49 -0400287EXPORT_SYMBOL(simple_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289int simple_unlink(struct inode *dir, struct dentry *dentry)
290{
David Howellsdea655c2015-03-17 22:26:15 +0000291 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700294 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 dput(dentry);
296 return 0;
297}
Al Viro12f38872013-09-15 21:20:49 -0400298EXPORT_SYMBOL(simple_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300int simple_rmdir(struct inode *dir, struct dentry *dentry)
301{
302 if (!simple_empty(dentry))
303 return -ENOTEMPTY;
304
David Howellsdea655c2015-03-17 22:26:15 +0000305 drop_nlink(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 simple_unlink(dir, dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700307 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309}
Al Viro12f38872013-09-15 21:20:49 -0400310EXPORT_SYMBOL(simple_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
313 struct inode *new_dir, struct dentry *new_dentry)
314{
David Howellsdea655c2015-03-17 22:26:15 +0000315 struct inode *inode = d_inode(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +0000316 int they_are_dirs = d_is_dir(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if (!simple_empty(new_dentry))
319 return -ENOTEMPTY;
320
David Howellsdea655c2015-03-17 22:26:15 +0000321 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 simple_unlink(new_dir, new_dentry);
Al Viro841590c2011-07-21 15:49:09 -0400323 if (they_are_dirs) {
David Howellsdea655c2015-03-17 22:26:15 +0000324 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700325 drop_nlink(old_dir);
Al Viro841590c2011-07-21 15:49:09 -0400326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700328 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -0700329 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
333 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
334
335 return 0;
336}
Al Viro12f38872013-09-15 21:20:49 -0400337EXPORT_SYMBOL(simple_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000339/**
Christoph Hellwigeef23802010-06-04 11:30:01 +0200340 * simple_setattr - setattr for simple filesystem
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000341 * @dentry: dentry
342 * @iattr: iattr structure
343 *
344 * Returns 0 on success, -error on failure.
345 *
Christoph Hellwigeef23802010-06-04 11:30:01 +0200346 * simple_setattr is a simple ->setattr implementation without a proper
347 * implementation of size changes.
348 *
349 * It can either be used for in-memory filesystems or special files
350 * on simple regular filesystems. Anything that needs to change on-disk
351 * or wire state on size changes needs its own setattr method.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000352 */
353int simple_setattr(struct dentry *dentry, struct iattr *iattr)
354{
David Howellsdea655c2015-03-17 22:26:15 +0000355 struct inode *inode = d_inode(dentry);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000356 int error;
357
358 error = inode_change_ok(inode, iattr);
359 if (error)
360 return error;
361
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200362 if (iattr->ia_valid & ATTR_SIZE)
363 truncate_setsize(inode, iattr->ia_size);
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200364 setattr_copy(inode, iattr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200365 mark_inode_dirty(inode);
366 return 0;
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000367}
368EXPORT_SYMBOL(simple_setattr);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370int simple_readpage(struct file *file, struct page *page)
371{
Pekka J Enbergc0d92cb2006-09-29 01:59:09 -0700372 clear_highpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 flush_dcache_page(page);
374 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unlock_page(page);
376 return 0;
377}
Al Viro12f38872013-09-15 21:20:49 -0400378EXPORT_SYMBOL(simple_readpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Nick Pigginafddba42007-10-16 01:25:01 -0700380int simple_write_begin(struct file *file, struct address_space *mapping,
381 loff_t pos, unsigned len, unsigned flags,
382 struct page **pagep, void **fsdata)
383{
384 struct page *page;
385 pgoff_t index;
Nick Pigginafddba42007-10-16 01:25:01 -0700386
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300387 index = pos >> PAGE_SHIFT;
Nick Pigginafddba42007-10-16 01:25:01 -0700388
Nick Piggin54566b22009-01-04 12:00:53 -0800389 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginafddba42007-10-16 01:25:01 -0700390 if (!page)
391 return -ENOMEM;
392
393 *pagep = page;
394
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300395 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
396 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200397
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300398 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
Boaz Harrosh193cf4b2010-01-12 16:18:08 +0200399 }
400 return 0;
Nick Pigginafddba42007-10-16 01:25:01 -0700401}
Al Viro12f38872013-09-15 21:20:49 -0400402EXPORT_SYMBOL(simple_write_begin);
Nick Pigginafddba42007-10-16 01:25:01 -0700403
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200404/**
405 * simple_write_end - .write_end helper for non-block-device FSes
406 * @available: See .write_end of address_space_operations
407 * @file: "
408 * @mapping: "
409 * @pos: "
410 * @len: "
411 * @copied: "
412 * @page: "
413 * @fsdata: "
414 *
415 * simple_write_end does the minimum needed for updating a page after writing is
416 * done. It has the same API signature as the .write_end of
417 * address_space_operations vector. So it can just be set onto .write_end for
418 * FSes that don't need any other processing. i_mutex is assumed to be held.
419 * Block based filesystems should use generic_write_end().
420 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
421 * is not called, so a filesystem that actually does store data in .write_inode
422 * should extend on what's done here with a call to mark_inode_dirty() in the
423 * case that i_size has changed.
424 */
425int simple_write_end(struct file *file, struct address_space *mapping,
426 loff_t pos, unsigned len, unsigned copied,
427 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct inode *inode = page->mapping->host;
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200430 loff_t last_pos = pos + copied;
431
432 /* zero the stale part of the page if we did a short copy */
433 if (copied < len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300434 unsigned from = pos & (PAGE_SIZE - 1);
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200435
436 zero_user(page, from + copied, len - copied);
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Nick Piggin955eff52007-02-20 13:58:08 -0800439 if (!PageUptodate(page))
440 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * No need to use i_size_read() here, the i_size
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800443 * cannot change under us because we hold the i_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Boaz Harroshad2a722f2010-01-12 15:13:47 +0200445 if (last_pos > inode->i_size)
446 i_size_write(inode, last_pos);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 set_page_dirty(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700449 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300450 put_page(page);
Nick Pigginafddba42007-10-16 01:25:01 -0700451
452 return copied;
453}
Al Viro12f38872013-09-15 21:20:49 -0400454EXPORT_SYMBOL(simple_write_end);
Nick Pigginafddba42007-10-16 01:25:01 -0700455
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700456/*
457 * the inodes created here are not hashed. If you use iunique to generate
458 * unique inode values later for this filesystem, then you must take care
459 * to pass it an appropriate max_reserved value to avoid collisions.
460 */
Roberto Sassu7d683a02010-06-03 11:58:28 +0200461int simple_fill_super(struct super_block *s, unsigned long magic,
462 struct tree_descr *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct inode *inode;
465 struct dentry *root;
466 struct dentry *dentry;
467 int i;
468
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300469 s->s_blocksize = PAGE_SIZE;
470 s->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 s->s_magic = magic;
Hugh Dickins759b9772007-03-05 00:30:28 -0800472 s->s_op = &simple_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 s->s_time_gran = 1;
474
475 inode = new_inode(s);
476 if (!inode)
477 return -ENOMEM;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700478 /*
479 * because the root inode is 1, the files array must not contain an
480 * entry at index 1
481 */
482 inode->i_ino = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 inode->i_mode = S_IFDIR | 0755;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
485 inode->i_op = &simple_dir_inode_operations;
486 inode->i_fop = &simple_dir_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200487 set_nlink(inode, 2);
Al Viro48fde702012-01-08 22:15:13 -0500488 root = d_make_root(inode);
489 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 for (i = 0; !files->name || files->name[0]; i++, files++) {
492 if (!files->name)
493 continue;
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700494
495 /* warn if it tries to conflict with the root inode */
496 if (unlikely(i == 1))
497 printk(KERN_WARNING "%s: %s passed in a files array"
498 "with an index of 1!\n", __func__,
499 s->s_type->name);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dentry = d_alloc_name(root, files->name);
502 if (!dentry)
503 goto out;
504 inode = new_inode(s);
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300505 if (!inode) {
506 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto out;
Konstantin Khlebnikov32096ea2011-11-01 16:12:33 +0300508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 inode->i_mode = S_IFREG | files->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
511 inode->i_fop = files->ops;
512 inode->i_ino = i;
513 d_add(dentry, inode);
514 }
515 s->s_root = root;
516 return 0;
517out:
518 d_genocide(root);
Al Viro640946f22012-04-02 19:22:25 -0400519 shrink_dcache_parent(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dput(root);
521 return -ENOMEM;
522}
Al Viro12f38872013-09-15 21:20:49 -0400523EXPORT_SYMBOL(simple_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525static DEFINE_SPINLOCK(pin_fs_lock);
526
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400527int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct vfsmount *mnt = NULL;
530 spin_lock(&pin_fs_lock);
531 if (unlikely(!*mount)) {
532 spin_unlock(&pin_fs_lock);
Al Viro24529922012-03-17 02:13:52 -0400533 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (IS_ERR(mnt))
535 return PTR_ERR(mnt);
536 spin_lock(&pin_fs_lock);
537 if (!*mount)
538 *mount = mnt;
539 }
540 mntget(*mount);
541 ++*count;
542 spin_unlock(&pin_fs_lock);
543 mntput(mnt);
544 return 0;
545}
Al Viro12f38872013-09-15 21:20:49 -0400546EXPORT_SYMBOL(simple_pin_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548void simple_release_fs(struct vfsmount **mount, int *count)
549{
550 struct vfsmount *mnt;
551 spin_lock(&pin_fs_lock);
552 mnt = *mount;
553 if (!--*count)
554 *mount = NULL;
555 spin_unlock(&pin_fs_lock);
556 mntput(mnt);
557}
Al Viro12f38872013-09-15 21:20:49 -0400558EXPORT_SYMBOL(simple_release_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700560/**
561 * simple_read_from_buffer - copy data from the buffer to user space
562 * @to: the user space buffer to read to
563 * @count: the maximum number of bytes to read
564 * @ppos: the current position in the buffer
565 * @from: the buffer to read from
566 * @available: the size of the buffer
567 *
568 * The simple_read_from_buffer() function reads up to @count bytes from the
569 * buffer @from at offset @ppos into the user space address starting at @to.
570 *
571 * On success, the number of bytes read is returned and the offset @ppos is
572 * advanced by this number, or negative value is returned on error.
573 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
575 const void *from, size_t available)
576{
577 loff_t pos = *ppos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700578 size_t ret;
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (pos < 0)
581 return -EINVAL;
Steven Rostedt14be2742009-09-18 13:05:42 -0700582 if (pos >= available || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584 if (count > available - pos)
585 count = available - pos;
Steven Rostedt14be2742009-09-18 13:05:42 -0700586 ret = copy_to_user(to, from + pos, count);
587 if (ret == count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -EFAULT;
Steven Rostedt14be2742009-09-18 13:05:42 -0700589 count -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *ppos = pos + count;
591 return count;
592}
Al Viro12f38872013-09-15 21:20:49 -0400593EXPORT_SYMBOL(simple_read_from_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700595/**
Jiri Slaby6a727b42010-05-01 23:51:22 +0200596 * simple_write_to_buffer - copy data from user space to the buffer
597 * @to: the buffer to write to
598 * @available: the size of the buffer
599 * @ppos: the current position in the buffer
600 * @from: the user space buffer to read from
601 * @count: the maximum number of bytes to read
602 *
603 * The simple_write_to_buffer() function reads up to @count bytes from the user
604 * space address starting at @from into the buffer @to at offset @ppos.
605 *
606 * On success, the number of bytes written is returned and the offset @ppos is
607 * advanced by this number, or negative value is returned on error.
608 **/
609ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
610 const void __user *from, size_t count)
611{
612 loff_t pos = *ppos;
613 size_t res;
614
615 if (pos < 0)
616 return -EINVAL;
617 if (pos >= available || !count)
618 return 0;
619 if (count > available - pos)
620 count = available - pos;
621 res = copy_from_user(to + pos, from, count);
622 if (res == count)
623 return -EFAULT;
624 count -= res;
625 *ppos = pos + count;
626 return count;
627}
Al Viro12f38872013-09-15 21:20:49 -0400628EXPORT_SYMBOL(simple_write_to_buffer);
Jiri Slaby6a727b42010-05-01 23:51:22 +0200629
630/**
Akinobu Mita6d1029b2008-07-04 09:59:51 -0700631 * memory_read_from_buffer - copy data from the buffer
632 * @to: the kernel space buffer to read to
633 * @count: the maximum number of bytes to read
634 * @ppos: the current position in the buffer
635 * @from: the buffer to read from
636 * @available: the size of the buffer
637 *
638 * The memory_read_from_buffer() function reads up to @count bytes from the
639 * buffer @from at offset @ppos into the kernel space address starting at @to.
640 *
641 * On success, the number of bytes read is returned and the offset @ppos is
642 * advanced by this number, or negative value is returned on error.
643 **/
Akinobu Mita93b07112008-06-05 22:46:21 -0700644ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
645 const void *from, size_t available)
646{
647 loff_t pos = *ppos;
648
649 if (pos < 0)
650 return -EINVAL;
651 if (pos >= available)
652 return 0;
653 if (count > available - pos)
654 count = available - pos;
655 memcpy(to, from + pos, count);
656 *ppos = pos + count;
657
658 return count;
659}
Al Viro12f38872013-09-15 21:20:49 -0400660EXPORT_SYMBOL(memory_read_from_buffer);
Akinobu Mita93b07112008-06-05 22:46:21 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*
663 * Transaction based IO.
664 * The file expects a single write which triggers the transaction, and then
665 * possibly a read which collects the result - which is stored in a
666 * file-local buffer.
667 */
Ingo Molnar76791ab2009-03-25 16:48:35 +0100668
669void simple_transaction_set(struct file *file, size_t n)
670{
671 struct simple_transaction_argresp *ar = file->private_data;
672
673 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
674
675 /*
676 * The barrier ensures that ar->size will really remain zero until
677 * ar->data is ready for reading.
678 */
679 smp_mb();
680 ar->size = n;
681}
Al Viro12f38872013-09-15 21:20:49 -0400682EXPORT_SYMBOL(simple_transaction_set);
Ingo Molnar76791ab2009-03-25 16:48:35 +0100683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
685{
686 struct simple_transaction_argresp *ar;
687 static DEFINE_SPINLOCK(simple_transaction_lock);
688
689 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
690 return ERR_PTR(-EFBIG);
691
692 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
693 if (!ar)
694 return ERR_PTR(-ENOMEM);
695
696 spin_lock(&simple_transaction_lock);
697
698 /* only one write allowed per open */
699 if (file->private_data) {
700 spin_unlock(&simple_transaction_lock);
701 free_page((unsigned long)ar);
702 return ERR_PTR(-EBUSY);
703 }
704
705 file->private_data = ar;
706
707 spin_unlock(&simple_transaction_lock);
708
709 if (copy_from_user(ar->data, buf, size))
710 return ERR_PTR(-EFAULT);
711
712 return ar->data;
713}
Al Viro12f38872013-09-15 21:20:49 -0400714EXPORT_SYMBOL(simple_transaction_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
717{
718 struct simple_transaction_argresp *ar = file->private_data;
719
720 if (!ar)
721 return 0;
722 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
723}
Al Viro12f38872013-09-15 21:20:49 -0400724EXPORT_SYMBOL(simple_transaction_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726int simple_transaction_release(struct inode *inode, struct file *file)
727{
728 free_page((unsigned long)file->private_data);
729 return 0;
730}
Al Viro12f38872013-09-15 21:20:49 -0400731EXPORT_SYMBOL(simple_transaction_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200733/* Simple attribute files */
734
735struct simple_attr {
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800736 int (*get)(void *, u64 *);
737 int (*set)(void *, u64);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200738 char get_buf[24]; /* enough to store a u64 and "\n\0" */
739 char set_buf[24];
740 void *data;
741 const char *fmt; /* format for read operation */
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800742 struct mutex mutex; /* protects access to these buffers */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200743};
744
745/* simple_attr_open is called by an actual attribute open file operation
746 * to set the attribute specific access operations. */
747int simple_attr_open(struct inode *inode, struct file *file,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800748 int (*get)(void *, u64 *), int (*set)(void *, u64),
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200749 const char *fmt)
750{
751 struct simple_attr *attr;
752
753 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
754 if (!attr)
755 return -ENOMEM;
756
757 attr->get = get;
758 attr->set = set;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700759 attr->data = inode->i_private;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200760 attr->fmt = fmt;
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800761 mutex_init(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200762
763 file->private_data = attr;
764
765 return nonseekable_open(inode, file);
766}
Al Viro12f38872013-09-15 21:20:49 -0400767EXPORT_SYMBOL_GPL(simple_attr_open);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200768
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800769int simple_attr_release(struct inode *inode, struct file *file)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200770{
771 kfree(file->private_data);
772 return 0;
773}
Al Viro12f38872013-09-15 21:20:49 -0400774EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200775
776/* read from the buffer that is filled with the get function */
777ssize_t simple_attr_read(struct file *file, char __user *buf,
778 size_t len, loff_t *ppos)
779{
780 struct simple_attr *attr;
781 size_t size;
782 ssize_t ret;
783
784 attr = file->private_data;
785
786 if (!attr->get)
787 return -EACCES;
788
Christoph Hellwig92613032008-02-08 04:20:27 -0800789 ret = mutex_lock_interruptible(&attr->mutex);
790 if (ret)
791 return ret;
792
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800793 if (*ppos) { /* continued read */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200794 size = strlen(attr->get_buf);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800795 } else { /* first read */
796 u64 val;
797 ret = attr->get(attr->data, &val);
798 if (ret)
799 goto out;
800
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200801 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800802 attr->fmt, (unsigned long long)val);
803 }
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200804
805 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800806out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800807 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200808 return ret;
809}
Al Viro12f38872013-09-15 21:20:49 -0400810EXPORT_SYMBOL_GPL(simple_attr_read);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200811
812/* interpret the buffer as a number to call the set function with */
813ssize_t simple_attr_write(struct file *file, const char __user *buf,
814 size_t len, loff_t *ppos)
815{
816 struct simple_attr *attr;
817 u64 val;
818 size_t size;
819 ssize_t ret;
820
821 attr = file->private_data;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200822 if (!attr->set)
823 return -EACCES;
824
Christoph Hellwig92613032008-02-08 04:20:27 -0800825 ret = mutex_lock_interruptible(&attr->mutex);
826 if (ret)
827 return ret;
828
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200829 ret = -EFAULT;
830 size = min(sizeof(attr->set_buf) - 1, len);
831 if (copy_from_user(attr->set_buf, buf, size))
832 goto out;
833
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200834 attr->set_buf[size] = '\0';
Akinobu Mitaf7b88632011-07-19 08:49:25 -0700835 val = simple_strtoll(attr->set_buf, NULL, 0);
Wu Fengguang05cc0ce2009-09-18 13:06:03 -0700836 ret = attr->set(attr->data, val);
837 if (ret == 0)
838 ret = len; /* on success, claim we got the whole input */
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200839out:
Ingo Molnar7cf34c72006-03-23 03:00:36 -0800840 mutex_unlock(&attr->mutex);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200841 return ret;
842}
Al Viro12f38872013-09-15 21:20:49 -0400843EXPORT_SYMBOL_GPL(simple_attr_write);
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200844
Christoph Hellwig25961102007-10-21 16:42:05 -0700845/**
846 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
847 * @sb: filesystem to do the file handle conversion on
848 * @fid: file handle to convert
849 * @fh_len: length of the file handle in bytes
850 * @fh_type: type of file handle
851 * @get_inode: filesystem callback to retrieve inode
852 *
853 * This function decodes @fid as long as it has one of the well-known
854 * Linux filehandle types and calls @get_inode on it to retrieve the
855 * inode for the object specified in the file handle.
856 */
857struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
858 int fh_len, int fh_type, struct inode *(*get_inode)
859 (struct super_block *sb, u64 ino, u32 gen))
860{
861 struct inode *inode = NULL;
862
863 if (fh_len < 2)
864 return NULL;
865
866 switch (fh_type) {
867 case FILEID_INO32_GEN:
868 case FILEID_INO32_GEN_PARENT:
869 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
870 break;
871 }
872
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200873 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700874}
875EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
876
877/**
Yanchuan Nianca186832012-09-05 16:31:29 +0800878 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
Christoph Hellwig25961102007-10-21 16:42:05 -0700879 * @sb: filesystem to do the file handle conversion on
880 * @fid: file handle to convert
881 * @fh_len: length of the file handle in bytes
882 * @fh_type: type of file handle
883 * @get_inode: filesystem callback to retrieve inode
884 *
885 * This function decodes @fid as long as it has one of the well-known
886 * Linux filehandle types and calls @get_inode on it to retrieve the
887 * inode for the _parent_ object specified in the file handle if it
888 * is specified in the file handle, or NULL otherwise.
889 */
890struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
891 int fh_len, int fh_type, struct inode *(*get_inode)
892 (struct super_block *sb, u64 ino, u32 gen))
893{
894 struct inode *inode = NULL;
895
896 if (fh_len <= 2)
897 return NULL;
898
899 switch (fh_type) {
900 case FILEID_INO32_GEN_PARENT:
901 inode = get_inode(sb, fid->i32.parent_ino,
902 (fh_len > 3 ? fid->i32.parent_gen : 0));
903 break;
904 }
905
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +0200906 return d_obtain_alias(inode);
Christoph Hellwig25961102007-10-21 16:42:05 -0700907}
908EXPORT_SYMBOL_GPL(generic_fh_to_parent);
909
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200910/**
Fabian Frederickac13a822014-06-04 16:06:27 -0700911 * __generic_file_fsync - generic fsync implementation for simple filesystems
912 *
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200913 * @file: file to synchronize
Fabian Frederickac13a822014-06-04 16:06:27 -0700914 * @start: start offset in bytes
915 * @end: end offset in bytes (inclusive)
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200916 * @datasync: only synchronize essential metadata if true
917 *
918 * This is a generic implementation of the fsync method for simple
919 * filesystems which track all non-inode metadata in the buffers list
920 * hanging off the address_space structure.
921 */
Fabian Frederickac13a822014-06-04 16:06:27 -0700922int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
923 int datasync)
Al Virod5aacad2009-06-07 14:56:44 -0400924{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200925 struct inode *inode = file->f_mapping->host;
Al Virod5aacad2009-06-07 14:56:44 -0400926 int err;
927 int ret;
928
Josef Bacik02c24a82011-07-16 20:44:56 -0400929 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
930 if (err)
931 return err;
932
Al Viro59551022016-01-22 15:40:57 -0500933 inode_lock(inode);
Al Virod5aacad2009-06-07 14:56:44 -0400934 ret = sync_mapping_buffers(inode->i_mapping);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500935 if (!(inode->i_state & I_DIRTY_ALL))
Josef Bacik02c24a82011-07-16 20:44:56 -0400936 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400937 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
Josef Bacik02c24a82011-07-16 20:44:56 -0400938 goto out;
Al Virod5aacad2009-06-07 14:56:44 -0400939
Christoph Hellwigc37650162010-10-06 10:48:20 +0200940 err = sync_inode_metadata(inode, 1);
Al Virod5aacad2009-06-07 14:56:44 -0400941 if (ret == 0)
942 ret = err;
Fabian Frederickac13a822014-06-04 16:06:27 -0700943
Josef Bacik02c24a82011-07-16 20:44:56 -0400944out:
Al Viro59551022016-01-22 15:40:57 -0500945 inode_unlock(inode);
Al Virod5aacad2009-06-07 14:56:44 -0400946 return ret;
947}
Fabian Frederickac13a822014-06-04 16:06:27 -0700948EXPORT_SYMBOL(__generic_file_fsync);
949
950/**
951 * generic_file_fsync - generic fsync implementation for simple filesystems
952 * with flush
953 * @file: file to synchronize
954 * @start: start offset in bytes
955 * @end: end offset in bytes (inclusive)
956 * @datasync: only synchronize essential metadata if true
957 *
958 */
959
960int generic_file_fsync(struct file *file, loff_t start, loff_t end,
961 int datasync)
962{
963 struct inode *inode = file->f_mapping->host;
964 int err;
965
966 err = __generic_file_fsync(file, start, end, datasync);
967 if (err)
968 return err;
969 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
970}
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200971EXPORT_SYMBOL(generic_file_fsync);
972
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -0700973/**
974 * generic_check_addressable - Check addressability of file system
975 * @blocksize_bits: log of file system block size
976 * @num_blocks: number of blocks in file system
977 *
978 * Determine whether a file system with @num_blocks blocks (and a
979 * block size of 2**@blocksize_bits) is addressable by the sector_t
980 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
981 */
982int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
983{
984 u64 last_fs_block = num_blocks - 1;
Joel Beckera33f13e2010-08-16 12:10:17 -0700985 u64 last_fs_page =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300986 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -0700987
988 if (unlikely(num_blocks == 0))
989 return 0;
990
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300991 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -0700992 return -EINVAL;
993
Joel Beckera33f13e2010-08-16 12:10:17 -0700994 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
995 (last_fs_page > (pgoff_t)(~0ULL))) {
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -0700996 return -EFBIG;
997 }
998 return 0;
999}
1000EXPORT_SYMBOL(generic_check_addressable);
1001
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001002/*
1003 * No-op implementation of ->fsync for in-memory filesystems.
1004 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001005int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001006{
1007 return 0;
1008}
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001009EXPORT_SYMBOL(noop_fsync);
Al Viro87dc8002013-09-16 10:30:04 -04001010
Al Virofceef392015-12-29 15:58:39 -05001011/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1012void kfree_link(void *p)
Al Viro87dc8002013-09-16 10:30:04 -04001013{
Al Virofceef392015-12-29 15:58:39 -05001014 kfree(p);
Al Viro87dc8002013-09-16 10:30:04 -04001015}
Al Virofceef392015-12-29 15:58:39 -05001016EXPORT_SYMBOL(kfree_link);
Al Viro69878432013-10-02 22:35:11 -04001017
1018/*
1019 * nop .set_page_dirty method so that people can use .page_mkwrite on
1020 * anon inodes.
1021 */
1022static int anon_set_page_dirty(struct page *page)
1023{
1024 return 0;
1025};
1026
1027/*
1028 * A single inode exists for all anon_inode files. Contrary to pipes,
1029 * anon_inode inodes have no associated per-instance data, so we need
1030 * only allocate one of them.
1031 */
1032struct inode *alloc_anon_inode(struct super_block *s)
1033{
1034 static const struct address_space_operations anon_aops = {
1035 .set_page_dirty = anon_set_page_dirty,
1036 };
1037 struct inode *inode = new_inode_pseudo(s);
1038
1039 if (!inode)
1040 return ERR_PTR(-ENOMEM);
1041
1042 inode->i_ino = get_next_ino();
1043 inode->i_mapping->a_ops = &anon_aops;
1044
1045 /*
1046 * Mark the inode dirty from the very beginning,
1047 * that way it will never be moved to the dirty
1048 * list because mark_inode_dirty() will think
1049 * that it already _is_ on the dirty list.
1050 */
1051 inode->i_state = I_DIRTY;
1052 inode->i_mode = S_IRUSR | S_IWUSR;
1053 inode->i_uid = current_fsuid();
1054 inode->i_gid = current_fsgid();
1055 inode->i_flags |= S_PRIVATE;
1056 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1057 return inode;
1058}
1059EXPORT_SYMBOL(alloc_anon_inode);
Jeff Layton1c994a02014-08-27 06:49:41 -04001060
1061/**
1062 * simple_nosetlease - generic helper for prohibiting leases
1063 * @filp: file pointer
1064 * @arg: type of lease to obtain
1065 * @flp: new lease supplied for insertion
Jeff Laytone6f5c782014-08-22 10:40:25 -04001066 * @priv: private data for lm_setup operation
Jeff Layton1c994a02014-08-27 06:49:41 -04001067 *
1068 * Generic helper for filesystems that do not wish to allow leases to be set.
1069 * All arguments are ignored and it just returns -EINVAL.
1070 */
1071int
Jeff Laytone6f5c782014-08-22 10:40:25 -04001072simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1073 void **priv)
Jeff Layton1c994a02014-08-27 06:49:41 -04001074{
1075 return -EINVAL;
1076}
1077EXPORT_SYMBOL(simple_nosetlease);
Al Viro61ba64f2015-05-02 09:54:06 -04001078
Al Viro6b255392015-11-17 10:20:54 -05001079const char *simple_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05001080 struct delayed_call *done)
Al Viro61ba64f2015-05-02 09:54:06 -04001081{
Al Viro6b255392015-11-17 10:20:54 -05001082 return inode->i_link;
Al Viro61ba64f2015-05-02 09:54:06 -04001083}
Al Viro6b255392015-11-17 10:20:54 -05001084EXPORT_SYMBOL(simple_get_link);
Al Viro61ba64f2015-05-02 09:54:06 -04001085
1086const struct inode_operations simple_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001087 .get_link = simple_get_link,
Al Viro61ba64f2015-05-02 09:54:06 -04001088 .readlink = generic_readlink
1089};
1090EXPORT_SYMBOL(simple_symlink_inode_operations);
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001091
1092/*
1093 * Operations for a permanently empty directory.
1094 */
1095static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1096{
1097 return ERR_PTR(-ENOENT);
1098}
1099
1100static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1101 struct kstat *stat)
1102{
1103 struct inode *inode = d_inode(dentry);
1104 generic_fillattr(inode, stat);
1105 return 0;
1106}
1107
1108static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1109{
1110 return -EPERM;
1111}
1112
Al Viro3767e252016-05-27 11:06:05 -04001113static int empty_dir_setxattr(struct dentry *dentry, struct inode *inode,
1114 const char *name, const void *value,
1115 size_t size, int flags)
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001116{
1117 return -EOPNOTSUPP;
1118}
1119
Al Viroce23e642016-04-11 00:48:00 -04001120static ssize_t empty_dir_getxattr(struct dentry *dentry, struct inode *inode,
1121 const char *name, void *value, size_t size)
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001122{
1123 return -EOPNOTSUPP;
1124}
1125
1126static int empty_dir_removexattr(struct dentry *dentry, const char *name)
1127{
1128 return -EOPNOTSUPP;
1129}
1130
1131static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1132{
1133 return -EOPNOTSUPP;
1134}
1135
1136static const struct inode_operations empty_dir_inode_operations = {
1137 .lookup = empty_dir_lookup,
1138 .permission = generic_permission,
1139 .setattr = empty_dir_setattr,
1140 .getattr = empty_dir_getattr,
1141 .setxattr = empty_dir_setxattr,
1142 .getxattr = empty_dir_getxattr,
1143 .removexattr = empty_dir_removexattr,
1144 .listxattr = empty_dir_listxattr,
1145};
1146
1147static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1148{
1149 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1150 return generic_file_llseek_size(file, offset, whence, 2, 2);
1151}
1152
1153static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1154{
1155 dir_emit_dots(file, ctx);
1156 return 0;
1157}
1158
1159static const struct file_operations empty_dir_operations = {
1160 .llseek = empty_dir_llseek,
1161 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001162 .iterate_shared = empty_dir_readdir,
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001163 .fsync = noop_fsync,
1164};
1165
1166
1167void make_empty_dir_inode(struct inode *inode)
1168{
1169 set_nlink(inode, 2);
1170 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1171 inode->i_uid = GLOBAL_ROOT_UID;
1172 inode->i_gid = GLOBAL_ROOT_GID;
1173 inode->i_rdev = 0;
Eric W. Biederman4b75de862015-08-12 15:00:12 -05001174 inode->i_size = 0;
Eric W. Biedermanfbabfd02015-05-09 15:54:49 -05001175 inode->i_blkbits = PAGE_SHIFT;
1176 inode->i_blocks = 0;
1177
1178 inode->i_op = &empty_dir_inode_operations;
1179 inode->i_fop = &empty_dir_operations;
1180}
1181
1182bool is_empty_dir_inode(struct inode *inode)
1183{
1184 return (inode->i_fop == &empty_dir_operations) &&
1185 (inode->i_op == &empty_dir_inode_operations);
1186}