457c899653991 (Thomas Gleixner 2019-05-19 13:08:55 +0100 1) // SPDX-License-Identifier: GPL-2.0-only
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * fs/libfs.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) * Library for filesystems writers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 7) #include <linux/blkdev.h>
630d9c47274aa (Paul Gortmaker 2011-11-16 23:57:37 -0500 8) #include <linux/export.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) #include <linux/pagemap.h>
5a0e3ad6af866 (Tejun Heo 2010-03-24 17:04:11 +0900 10) #include <linux/slab.h>
5b825c3af1d8a (Ingo Molnar 2017-02-02 17:54:15 +0100 11) #include <linux/cred.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) #include <linux/mount.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) #include <linux/vfs.h>
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 14) #include <linux/quotaops.h>
7cf34c761db88 (Ingo Molnar 2006-03-23 03:00:36 -0800 15) #include <linux/mutex.h>
87dc800be2499 (Al Viro 2013-09-16 10:30:04 -0400 16) #include <linux/namei.h>
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 17) #include <linux/exportfs.h>
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 18) #include <linux/writeback.h>
ff01bb4832651 (Al Viro 2011-09-16 02:31:11 -0400 19) #include <linux/buffer_head.h> /* sync_mapping_buffers */
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 20) #include <linux/fs_context.h>
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 21) #include <linux/pseudo_fs.h>
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 22) #include <linux/fsnotify.h>
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 23) #include <linux/unicode.h>
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 24) #include <linux/fscrypt.h>
7cf34c761db88 (Ingo Molnar 2006-03-23 03:00:36 -0800 25)
7c0f6ba682b9c (Linus Torvalds 2016-12-24 11:46:01 -0800 26) #include <linux/uaccess.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27)
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 28) #include "internal.h"
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 29)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 30) int simple_getattr(struct user_namespace *mnt_userns, const struct path *path,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 31) struct kstat *stat, u32 request_mask,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 32) unsigned int query_flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33) {
a528d35e8bfcc (David Howells 2017-01-31 16:46:22 +0000 34) struct inode *inode = d_inode(path->dentry);
0d56a4518d5ea (Christian Brauner 2021-01-21 14:19:30 +0100 35) generic_fillattr(&init_user_ns, inode, stat);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 36) stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 38) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 39) EXPORT_SYMBOL(simple_getattr);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 40)
726c334223180 (David Howells 2006-06-23 02:02:58 -0700 41) int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 42) {
726c334223180 (David Howells 2006-06-23 02:02:58 -0700 43) buf->f_type = dentry->d_sb->s_magic;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 44) buf->f_bsize = PAGE_SIZE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 45) buf->f_namelen = NAME_MAX;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 46) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 47) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 48) EXPORT_SYMBOL(simple_statfs);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 49)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 50) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 51) * Retaining negative dentries for an in-memory filesystem just wastes
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 52) * memory and lookup time: arrange for them to be deleted immediately.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 53) */
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 54) int always_delete_dentry(const struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 55) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 56) return 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57) }
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 58) EXPORT_SYMBOL(always_delete_dentry);
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 59)
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 60) const struct dentry_operations simple_dentry_operations = {
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 61) .d_delete = always_delete_dentry,
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 62) };
b26d4cd385fc5 (Al Viro 2013-10-25 18:47:37 -0400 63) EXPORT_SYMBOL(simple_dentry_operations);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 65) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 66) * Lookup the data. This is trivial - if the dentry didn't already
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 67) * exist, we know it is negative. Set d_op to delete negative dentries.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) */
00cd8dd3bf95f (Al Viro 2012-06-10 17:13:09 -0400 69) struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71) if (dentry->d_name.len > NAME_MAX)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72) return ERR_PTR(-ENAMETOOLONG);
74931da7a6d34 (Al Viro 2013-07-14 17:43:25 +0400 73) if (!dentry->d_sb->s_d_op)
74931da7a6d34 (Al Viro 2013-07-14 17:43:25 +0400 74) d_set_d_op(dentry, &simple_dentry_operations);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 75) d_add(dentry, NULL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 77) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 78) EXPORT_SYMBOL(simple_lookup);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 79)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 80) int dcache_dir_open(struct inode *inode, struct file *file)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 81) {
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 82) file->private_data = d_alloc_cursor(file->f_path.dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 83)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 84) return file->private_data ? 0 : -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 85) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 86) EXPORT_SYMBOL(dcache_dir_open);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 87)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 88) int dcache_dir_close(struct inode *inode, struct file *file)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 89) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 90) dput(file->private_data);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 91) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 92) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 93) EXPORT_SYMBOL(dcache_dir_close);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 94)
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 95) /* parent is locked at least shared */
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 96) /*
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 97) * Returns an element of siblings' list.
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 98) * We are looking for <count>th positive after <p>; if
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 99) * found, dentry is grabbed and returned to caller.
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 100) * If no such element exists, NULL is returned.
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 101) */
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 102) static struct dentry *scan_positives(struct dentry *cursor,
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 103) struct list_head *p,
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 104) loff_t count,
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 105) struct dentry *last)
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 106) {
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 107) struct dentry *dentry = cursor->d_parent, *found = NULL;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 108)
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 109) spin_lock(&dentry->d_lock);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 110) while ((p = p->next) != &dentry->d_subdirs) {
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 111) struct dentry *d = list_entry(p, struct dentry, d_child);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 112) // we must at least skip cursors, to avoid livelocks
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 113) if (d->d_flags & DCACHE_DENTRY_CURSOR)
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 114) continue;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 115) if (simple_positive(d) && !--count) {
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 116) spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 117) if (simple_positive(d))
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 118) found = dget_dlock(d);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 119) spin_unlock(&d->d_lock);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 120) if (likely(found))
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 121) break;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 122) count = 1;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 123) }
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 124) if (need_resched()) {
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 125) list_move(&cursor->d_child, p);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 126) p = &cursor->d_child;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 127) spin_unlock(&dentry->d_lock);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 128) cond_resched();
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 129) spin_lock(&dentry->d_lock);
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 130) }
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 131) }
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 132) spin_unlock(&dentry->d_lock);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 133) dput(last);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 134) return found;
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 135) }
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 136)
965c8e59cfcf8 (Andrew Morton 2012-12-17 15:59:39 -0800 137) loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 138) {
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 139) struct dentry *dentry = file->f_path.dentry;
965c8e59cfcf8 (Andrew Morton 2012-12-17 15:59:39 -0800 140) switch (whence) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 141) case 1:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 142) offset += file->f_pos;
df561f6688fef (Gustavo A. R. Silva 2020-08-23 17:36:59 -0500 143) fallthrough;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 144) case 0:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 145) if (offset >= 0)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 146) break;
df561f6688fef (Gustavo A. R. Silva 2020-08-23 17:36:59 -0500 147) fallthrough;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 148) default:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 149) return -EINVAL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 150) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 151) if (offset != file->f_pos) {
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 152) struct dentry *cursor = file->private_data;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 153) struct dentry *to = NULL;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 154)
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 155) inode_lock_shared(dentry->d_inode);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 156)
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 157) if (offset > 2)
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 158) to = scan_positives(cursor, &dentry->d_subdirs,
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 159) offset - 2, NULL);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 160) spin_lock(&dentry->d_lock);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 161) if (to)
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 162) list_move(&cursor->d_child, &to->d_child);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 163) else
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 164) list_del_init(&cursor->d_child);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 165) spin_unlock(&dentry->d_lock);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 166) dput(to);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 167)
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 168) file->f_pos = offset;
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 169)
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 170) inode_unlock_shared(dentry->d_inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 171) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 172) return offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 173) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 174) EXPORT_SYMBOL(dcache_dir_lseek);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 175)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 176) /* Relationship between i_mode and the DT_xxx types */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 177) static inline unsigned char dt_type(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 178) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 179) return (inode->i_mode >> 12) & 15;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 180) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 181)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 182) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 183) * Directory is locked and all positive dentries in it are safe, since
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 184) * for ramfs-type trees they can't go away without unlink() or rmdir(),
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 185) * both impossible due to the lock on directory.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 186) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 187)
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 188) int dcache_readdir(struct file *file, struct dir_context *ctx)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 189) {
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 190) struct dentry *dentry = file->f_path.dentry;
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 191) struct dentry *cursor = file->private_data;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 192) struct list_head *anchor = &dentry->d_subdirs;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 193) struct dentry *next = NULL;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 194) struct list_head *p;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 195)
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 196) if (!dir_emit_dots(file, ctx))
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 197) return 0;
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 198)
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 199) if (ctx->pos == 2)
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 200) p = anchor;
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 201) else if (!list_empty(&cursor->d_child))
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 202) p = &cursor->d_child;
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 203) else
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 204) return 0;
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 205)
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 206) while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 207) if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 208) d_inode(next)->i_ino, dt_type(d_inode(next))))
4f42c1b5b9c27 (Al Viro 2016-06-06 19:37:13 -0400 209) break;
5f99f4e79abc6 (Al Viro 2013-05-15 20:23:06 -0400 210) ctx->pos++;
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 211) p = &next->d_child;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 212) }
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 213) spin_lock(&dentry->d_lock);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 214) if (next)
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 215) list_move_tail(&cursor->d_child, &next->d_child);
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 216) else
26b6c98433847 (Al Viro 2019-09-20 16:32:42 -0400 217) list_del_init(&cursor->d_child);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 218) spin_unlock(&dentry->d_lock);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 219) dput(next);
d4f4de5e5ef8e (Al Viro 2019-09-15 12:12:39 -0400 220)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 221) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 222) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 223) EXPORT_SYMBOL(dcache_readdir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 224)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 225) ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 226) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 227) return -EISDIR;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 228) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 229) EXPORT_SYMBOL(generic_read_dir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 230)
4b6f5d20b04dc (Arjan van de Ven 2006-03-28 01:56:42 -0800 231) const struct file_operations simple_dir_operations = {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 232) .open = dcache_dir_open,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 233) .release = dcache_dir_close,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 234) .llseek = dcache_dir_lseek,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 235) .read = generic_read_dir,
4e82901cd6d1a (Al Viro 2016-04-20 19:52:15 -0400 236) .iterate_shared = dcache_readdir,
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 237) .fsync = noop_fsync,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 238) };
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 239) EXPORT_SYMBOL(simple_dir_operations);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 240)
92e1d5be91a0e (Arjan van de Ven 2007-02-12 00:55:39 -0800 241) const struct inode_operations simple_dir_inode_operations = {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 242) .lookup = simple_lookup,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 243) };
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 244) EXPORT_SYMBOL(simple_dir_inode_operations);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 245)
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 246) static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 247) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 248) struct dentry *child = NULL;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 249) struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 250)
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 251) spin_lock(&parent->d_lock);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 252) while ((p = p->next) != &parent->d_subdirs) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 253) struct dentry *d = container_of(p, struct dentry, d_child);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 254) if (simple_positive(d)) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 255) spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 256) if (simple_positive(d))
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 257) child = dget_dlock(d);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 258) spin_unlock(&d->d_lock);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 259) if (likely(child))
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 260) break;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 261) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 262) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 263) spin_unlock(&parent->d_lock);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 264) dput(prev);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 265) return child;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 266) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 267)
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 268) void simple_recursive_removal(struct dentry *dentry,
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 269) void (*callback)(struct dentry *))
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 270) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 271) struct dentry *this = dget(dentry);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 272) while (true) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 273) struct dentry *victim = NULL, *child;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 274) struct inode *inode = this->d_inode;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 275)
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 276) inode_lock(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 277) if (d_is_dir(this))
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 278) inode->i_flags |= S_DEAD;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 279) while ((child = find_next_child(this, victim)) == NULL) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 280) // kill and ascend
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 281) // update metadata while it's still locked
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 282) inode->i_ctime = current_time(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 283) clear_nlink(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 284) inode_unlock(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 285) victim = this;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 286) this = this->d_parent;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 287) inode = this->d_inode;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 288) inode_lock(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 289) if (simple_positive(victim)) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 290) d_invalidate(victim); // avoid lost mounts
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 291) if (d_is_dir(victim))
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 292) fsnotify_rmdir(inode, victim);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 293) else
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 294) fsnotify_unlink(inode, victim);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 295) if (callback)
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 296) callback(victim);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 297) dput(victim); // unpin it
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 298) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 299) if (victim == dentry) {
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 300) inode->i_ctime = inode->i_mtime =
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 301) current_time(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 302) if (d_is_dir(dentry))
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 303) drop_nlink(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 304) inode_unlock(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 305) dput(dentry);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 306) return;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 307) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 308) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 309) inode_unlock(inode);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 310) this = child;
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 311) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 312) }
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 313) EXPORT_SYMBOL(simple_recursive_removal);
a3d1e7eb5abe3 (Al Viro 2019-11-18 09:43:10 -0500 314)
759b9775c25f5 (Hugh Dickins 2007-03-05 00:30:28 -0800 315) static const struct super_operations simple_super_operations = {
759b9775c25f5 (Hugh Dickins 2007-03-05 00:30:28 -0800 316) .statfs = simple_statfs,
759b9775c25f5 (Hugh Dickins 2007-03-05 00:30:28 -0800 317) };
759b9775c25f5 (Hugh Dickins 2007-03-05 00:30:28 -0800 318)
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 319) static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 320) {
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 321) struct pseudo_fs_context *ctx = fc->fs_private;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 322) struct inode *root;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 323)
89a4eb4b66e8f (Jeff Layton 2009-08-18 14:11:08 -0700 324) s->s_maxbytes = MAX_LFS_FILESIZE;
3971e1a917548 (Alex Nixon 2008-07-29 22:33:03 -0700 325) s->s_blocksize = PAGE_SIZE;
3971e1a917548 (Alex Nixon 2008-07-29 22:33:03 -0700 326) s->s_blocksize_bits = PAGE_SHIFT;
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 327) s->s_magic = ctx->magic;
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 328) s->s_op = ctx->ops ?: &simple_super_operations;
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 329) s->s_xattr = ctx->xattr;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 330) s->s_time_gran = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 331) root = new_inode(s);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 332) if (!root)
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 333) return -ENOMEM;
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 334)
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 335) /*
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 336) * since this is the first inode, make it number 1. New inodes created
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 337) * after this must take care not to collide with it (by passing
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 338) * max_reserved of 1 to iunique).
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 339) */
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 340) root->i_ino = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 341) root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 342) root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 343) s->s_root = d_make_root(root);
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 344) if (!s->s_root)
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 345) return -ENOMEM;
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 346) s->s_d_op = ctx->dops;
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 347) return 0;
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 348) }
8d9e46d80777b (Al Viro 2019-05-11 11:43:59 -0400 349)
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 350) static int pseudo_fs_get_tree(struct fs_context *fc)
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 351) {
2ac295d4f0c09 (Al Viro 2019-06-01 20:48:55 -0400 352) return get_tree_nodev(fc, pseudo_fs_fill_super);
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 353) }
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 354)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 355) static void pseudo_fs_free(struct fs_context *fc)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 356) {
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 357) kfree(fc->fs_private);
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 358) }
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 359)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 360) static const struct fs_context_operations pseudo_fs_context_ops = {
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 361) .free = pseudo_fs_free,
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 362) .get_tree = pseudo_fs_get_tree,
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 363) };
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 364)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 365) /*
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 366) * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 367) * will never be mountable)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 368) */
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 369) struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 370) unsigned long magic)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 371) {
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 372) struct pseudo_fs_context *ctx;
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 373)
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 374) ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 375) if (likely(ctx)) {
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 376) ctx->magic = magic;
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 377) fc->fs_private = ctx;
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 378) fc->ops = &pseudo_fs_context_ops;
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 379) fc->sb_flags |= SB_NOUSER;
db2c246a091ef (David Howells 2019-03-25 16:38:26 +0000 380) fc->global = true;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 381) }
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 382) return ctx;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 383) }
31d6d5ce53400 (David Howells 2019-03-25 16:38:23 +0000 384) EXPORT_SYMBOL(init_pseudo);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 385)
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 386) int simple_open(struct inode *inode, struct file *file)
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 387) {
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 388) if (inode->i_private)
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 389) file->private_data = inode->i_private;
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 390) return 0;
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 391) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 392) EXPORT_SYMBOL(simple_open);
20955e891d828 (Stephen Boyd 2012-04-05 14:25:09 -0700 393)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 394) int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 395) {
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 396) struct inode *inode = d_inode(old_dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 397)
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 398) inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
d8c76e6f45c11 (Dave Hansen 2006-09-30 23:29:04 -0700 399) inc_nlink(inode);
7de9c6ee3ecff (Al Viro 2010-10-23 11:11:40 -0400 400) ihold(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 401) dget(dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 402) d_instantiate(dentry, inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 403) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 404) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 405) EXPORT_SYMBOL(simple_link);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 406)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 407) int simple_empty(struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 408) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 409) struct dentry *child;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 410) int ret = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 411)
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 412) spin_lock(&dentry->d_lock);
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 413) list_for_each_entry(child, &dentry->d_subdirs, d_child) {
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 414) spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 415) if (simple_positive(child)) {
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 416) spin_unlock(&child->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 417) goto out;
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 418) }
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 419) spin_unlock(&child->d_lock);
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 420) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 421) ret = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 422) out:
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 423) spin_unlock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 424) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 425) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 426) EXPORT_SYMBOL(simple_empty);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 427)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 428) int simple_unlink(struct inode *dir, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 429) {
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 430) struct inode *inode = d_inode(dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 431)
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 432) inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
9a53c3a783c2f (Dave Hansen 2006-09-30 23:29:03 -0700 433) drop_nlink(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 434) dput(dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 435) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 436) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 437) EXPORT_SYMBOL(simple_unlink);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 438)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 439) int simple_rmdir(struct inode *dir, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 440) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 441) if (!simple_empty(dentry))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 442) return -ENOTEMPTY;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 443)
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 444) drop_nlink(d_inode(dentry));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 445) simple_unlink(dir, dentry);
9a53c3a783c2f (Dave Hansen 2006-09-30 23:29:03 -0700 446) drop_nlink(dir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 447) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 448) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 449) EXPORT_SYMBOL(simple_rmdir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 450)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 451) int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 452) struct dentry *old_dentry, struct inode *new_dir,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 453) struct dentry *new_dentry, unsigned int flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 454) {
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 455) struct inode *inode = d_inode(old_dentry);
e36cb0b89ce20 (David Howells 2015-01-29 12:02:35 +0000 456) int they_are_dirs = d_is_dir(old_dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 457)
e0e0be8a83552 (Miklos Szeredi 2016-09-27 11:03:57 +0200 458) if (flags & ~RENAME_NOREPLACE)
e0e0be8a83552 (Miklos Szeredi 2016-09-27 11:03:57 +0200 459) return -EINVAL;
e0e0be8a83552 (Miklos Szeredi 2016-09-27 11:03:57 +0200 460)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 461) if (!simple_empty(new_dentry))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 462) return -ENOTEMPTY;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 463)
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 464) if (d_really_is_positive(new_dentry)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 465) simple_unlink(new_dir, new_dentry);
841590ce16c19 (Al Viro 2011-07-21 15:49:09 -0400 466) if (they_are_dirs) {
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 467) drop_nlink(d_inode(new_dentry));
9a53c3a783c2f (Dave Hansen 2006-09-30 23:29:03 -0700 468) drop_nlink(old_dir);
841590ce16c19 (Al Viro 2011-07-21 15:49:09 -0400 469) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 470) } else if (they_are_dirs) {
9a53c3a783c2f (Dave Hansen 2006-09-30 23:29:03 -0700 471) drop_nlink(old_dir);
d8c76e6f45c11 (Dave Hansen 2006-09-30 23:29:04 -0700 472) inc_nlink(new_dir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 473) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 474)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 475) old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 476) new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 477)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 478) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 479) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 480) EXPORT_SYMBOL(simple_rename);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 481)
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 482) /**
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 483) * simple_setattr - setattr for simple filesystem
59347d9982411 (Randy Dunlap 2021-02-15 20:29:27 -0800 484) * @mnt_userns: user namespace of the target mount
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 485) * @dentry: dentry
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 486) * @iattr: iattr structure
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 487) *
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 488) * Returns 0 on success, -error on failure.
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 489) *
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 490) * simple_setattr is a simple ->setattr implementation without a proper
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 491) * implementation of size changes.
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 492) *
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 493) * It can either be used for in-memory filesystems or special files
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 494) * on simple regular filesystems. Anything that needs to change on-disk
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 495) * or wire state on size changes needs its own setattr method.
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 496) */
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 497) int simple_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 498) struct iattr *iattr)
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 499) {
dea655c28a32a (David Howells 2015-03-17 22:26:15 +0000 500) struct inode *inode = d_inode(dentry);
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 501) int error;
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 502)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 503) error = setattr_prepare(mnt_userns, dentry, iattr);
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 504) if (error)
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 505) return error;
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 506)
2c27c65ed0696 (Christoph Hellwig 2010-06-04 11:30:04 +0200 507) if (iattr->ia_valid & ATTR_SIZE)
2c27c65ed0696 (Christoph Hellwig 2010-06-04 11:30:04 +0200 508) truncate_setsize(inode, iattr->ia_size);
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 509) setattr_copy(mnt_userns, inode, iattr);
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 510) mark_inode_dirty(inode);
eef2380c18789 (Christoph Hellwig 2010-06-04 11:30:01 +0200 511) return 0;
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 512) }
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 513) EXPORT_SYMBOL(simple_setattr);
7bb46a6734a7e (Nicholas Piggin 2010-05-27 01:05:33 +1000 514)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 515) int simple_readpage(struct file *file, struct page *page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 516) {
c0d92cbc5849f (Pekka J Enberg 2006-09-29 01:59:09 -0700 517) clear_highpage(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 518) flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 519) SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 520) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 521) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 522) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 523) EXPORT_SYMBOL(simple_readpage);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 524)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 525) int simple_write_begin(struct file *file, struct address_space *mapping,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 526) loff_t pos, unsigned len, unsigned flags,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 527) struct page **pagep, void **fsdata)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 528) {
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 529) struct page *page;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 530) pgoff_t index;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 531)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 532) index = pos >> PAGE_SHIFT;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 533)
54566b2c1594c (Nicholas Piggin 2009-01-04 12:00:53 -0800 534) page = grab_cache_page_write_begin(mapping, index, flags);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 535) if (!page)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 536) return -ENOMEM;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 537)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 538) *pagep = page;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 539)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 540) if (!PageUptodate(page) && (len != PAGE_SIZE)) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 541) unsigned from = pos & (PAGE_SIZE - 1);
193cf4b99113a (Boaz Harrosh 2010-01-12 16:18:08 +0200 542)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 543) zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
193cf4b99113a (Boaz Harrosh 2010-01-12 16:18:08 +0200 544) }
193cf4b99113a (Boaz Harrosh 2010-01-12 16:18:08 +0200 545) return 0;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 546) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 547) EXPORT_SYMBOL(simple_write_begin);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 548)
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 549) /**
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 550) * simple_write_end - .write_end helper for non-block-device FSes
8e88bfba77eec (Randy Dunlap 2019-10-14 14:12:14 -0700 551) * @file: See .write_end of address_space_operations
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 552) * @mapping: "
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 553) * @pos: "
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 554) * @len: "
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 555) * @copied: "
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 556) * @page: "
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 557) * @fsdata: "
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 558) *
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 559) * simple_write_end does the minimum needed for updating a page after writing is
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 560) * done. It has the same API signature as the .write_end of
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 561) * address_space_operations vector. So it can just be set onto .write_end for
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 562) * FSes that don't need any other processing. i_mutex is assumed to be held.
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 563) * Block based filesystems should use generic_write_end().
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 564) * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 565) * is not called, so a filesystem that actually does store data in .write_inode
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 566) * should extend on what's done here with a call to mark_inode_dirty() in the
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 567) * case that i_size has changed.
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 568) *
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 569) * Use *ONLY* with simple_readpage()
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 570) */
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 571) int simple_write_end(struct file *file, struct address_space *mapping,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 572) loff_t pos, unsigned len, unsigned copied,
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 573) struct page *page, void *fsdata)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 574) {
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 575) struct inode *inode = page->mapping->host;
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 576) loff_t last_pos = pos + copied;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 577)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 578) /* zero the stale part of the page if we did a short copy */
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 579) if (!PageUptodate(page)) {
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 580) if (copied < len) {
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 581) unsigned from = pos & (PAGE_SIZE - 1);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 582)
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 583) zero_user(page, from + copied, len - copied);
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 584) }
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 585) SetPageUptodate(page);
04fff6416cb78 (Al Viro 2016-08-29 22:39:56 -0400 586) }
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 587) /*
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 588) * No need to use i_size_read() here, the i_size
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 589) * cannot change under us because we hold the i_mutex.
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 590) */
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 591) if (last_pos > inode->i_size)
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 592) i_size_write(inode, last_pos);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 593)
ad2a722f196d2 (Boaz Harrosh 2010-01-12 15:13:47 +0200 594) set_page_dirty(page);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 595) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 596) put_page(page);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 597)
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 598) return copied;
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 599) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 600) EXPORT_SYMBOL(simple_write_end);
afddba49d18f3 (Nicholas Piggin 2007-10-16 01:25:01 -0700 601)
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 602) /*
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 603) * the inodes created here are not hashed. If you use iunique to generate
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 604) * unique inode values later for this filesystem, then you must take care
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 605) * to pass it an appropriate max_reserved value to avoid collisions.
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 606) */
7d683a09990ff (Roberto Sassu 2010-06-03 11:58:28 +0200 607) int simple_fill_super(struct super_block *s, unsigned long magic,
cda37124f4e95 (Eric Biggers 2017-03-25 21:15:37 -0700 608) const struct tree_descr *files)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 609) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 610) struct inode *inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 611) struct dentry *root;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 612) struct dentry *dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 613) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 614)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 615) s->s_blocksize = PAGE_SIZE;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 616) s->s_blocksize_bits = PAGE_SHIFT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 617) s->s_magic = magic;
759b9775c25f5 (Hugh Dickins 2007-03-05 00:30:28 -0800 618) s->s_op = &simple_super_operations;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 619) s->s_time_gran = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 620)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 621) inode = new_inode(s);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 622) if (!inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 623) return -ENOMEM;
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 624) /*
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 625) * because the root inode is 1, the files array must not contain an
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 626) * entry at index 1
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 627) */
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 628) inode->i_ino = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 629) inode->i_mode = S_IFDIR | 0755;
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 630) inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 631) inode->i_op = &simple_dir_inode_operations;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 632) inode->i_fop = &simple_dir_operations;
bfe8684869601 (Miklos Szeredi 2011-10-28 14:13:29 +0200 633) set_nlink(inode, 2);
48fde701aff66 (Al Viro 2012-01-08 22:15:13 -0500 634) root = d_make_root(inode);
48fde701aff66 (Al Viro 2012-01-08 22:15:13 -0500 635) if (!root)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 636) return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 637) for (i = 0; !files->name || files->name[0]; i++, files++) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 638) if (!files->name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 639) continue;
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 640)
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 641) /* warn if it tries to conflict with the root inode */
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 642) if (unlikely(i == 1))
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 643) printk(KERN_WARNING "%s: %s passed in a files array"
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 644) "with an index of 1!\n", __func__,
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 645) s->s_type->name);
1a1c9bb433af2 (Jeff Layton 2007-05-08 00:32:31 -0700 646)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 647) dentry = d_alloc_name(root, files->name);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 648) if (!dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 649) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 650) inode = new_inode(s);
32096ea1aac14 (Konstantin Khlebnikov 2011-11-01 16:12:33 +0300 651) if (!inode) {
32096ea1aac14 (Konstantin Khlebnikov 2011-11-01 16:12:33 +0300 652) dput(dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 653) goto out;
32096ea1aac14 (Konstantin Khlebnikov 2011-11-01 16:12:33 +0300 654) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 655) inode->i_mode = S_IFREG | files->mode;
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 656) inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 657) inode->i_fop = files->ops;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 658) inode->i_ino = i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 659) d_add(dentry, inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 660) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 661) s->s_root = root;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 662) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 663) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 664) d_genocide(root);
640946f20390e (Al Viro 2012-04-02 19:22:25 -0400 665) shrink_dcache_parent(root);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 666) dput(root);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 667) return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 668) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 669) EXPORT_SYMBOL(simple_fill_super);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 670)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 671) static DEFINE_SPINLOCK(pin_fs_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 672)
1f5ce9e93aa96 (Trond Myklebust 2006-06-09 09:34:16 -0400 673) int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 674) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 675) struct vfsmount *mnt = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 676) spin_lock(&pin_fs_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 677) if (unlikely(!*mount)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 678) spin_unlock(&pin_fs_lock);
1751e8a6cb935 (Linus Torvalds 2017-11-27 13:05:09 -0800 679) mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 680) if (IS_ERR(mnt))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 681) return PTR_ERR(mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 682) spin_lock(&pin_fs_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 683) if (!*mount)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 684) *mount = mnt;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 685) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 686) mntget(*mount);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 687) ++*count;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 688) spin_unlock(&pin_fs_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 689) mntput(mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 690) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 691) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 692) EXPORT_SYMBOL(simple_pin_fs);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 693)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 694) void simple_release_fs(struct vfsmount **mount, int *count)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 695) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 696) struct vfsmount *mnt;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 697) spin_lock(&pin_fs_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 698) mnt = *mount;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 699) if (!--*count)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 700) *mount = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 701) spin_unlock(&pin_fs_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 702) mntput(mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 703) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 704) EXPORT_SYMBOL(simple_release_fs);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 705)
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 706) /**
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 707) * simple_read_from_buffer - copy data from the buffer to user space
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 708) * @to: the user space buffer to read to
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 709) * @count: the maximum number of bytes to read
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 710) * @ppos: the current position in the buffer
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 711) * @from: the buffer to read from
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 712) * @available: the size of the buffer
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 713) *
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 714) * The simple_read_from_buffer() function reads up to @count bytes from the
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 715) * buffer @from at offset @ppos into the user space address starting at @to.
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 716) *
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 717) * On success, the number of bytes read is returned and the offset @ppos is
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 718) * advanced by this number, or negative value is returned on error.
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 719) **/
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 720) ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 721) const void *from, size_t available)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 722) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 723) loff_t pos = *ppos;
14be27460e472 (Steven Rostedt 2009-09-18 13:05:42 -0700 724) size_t ret;
14be27460e472 (Steven Rostedt 2009-09-18 13:05:42 -0700 725)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 726) if (pos < 0)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 727) return -EINVAL;
14be27460e472 (Steven Rostedt 2009-09-18 13:05:42 -0700 728) if (pos >= available || !count)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 729) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 730) if (count > available - pos)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 731) count = available - pos;
14be27460e472 (Steven Rostedt 2009-09-18 13:05:42 -0700 732) ret = copy_to_user(to, from + pos, count);
14be27460e472 (Steven Rostedt 2009-09-18 13:05:42 -0700 733) if (ret == count)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 734) return -EFAULT;
14be27460e472 (Steven Rostedt 2009-09-18 13:05:42 -0700 735) count -= ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 736) *ppos = pos + count;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 737) return count;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 738) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 739) EXPORT_SYMBOL(simple_read_from_buffer);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 740)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 741) /**
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 742) * simple_write_to_buffer - copy data from user space to the buffer
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 743) * @to: the buffer to write to
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 744) * @available: the size of the buffer
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 745) * @ppos: the current position in the buffer
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 746) * @from: the user space buffer to read from
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 747) * @count: the maximum number of bytes to read
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 748) *
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 749) * The simple_write_to_buffer() function reads up to @count bytes from the user
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 750) * space address starting at @from into the buffer @to at offset @ppos.
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 751) *
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 752) * On success, the number of bytes written is returned and the offset @ppos is
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 753) * advanced by this number, or negative value is returned on error.
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 754) **/
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 755) ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 756) const void __user *from, size_t count)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 757) {
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 758) loff_t pos = *ppos;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 759) size_t res;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 760)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 761) if (pos < 0)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 762) return -EINVAL;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 763) if (pos >= available || !count)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 764) return 0;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 765) if (count > available - pos)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 766) count = available - pos;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 767) res = copy_from_user(to + pos, from, count);
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 768) if (res == count)
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 769) return -EFAULT;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 770) count -= res;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 771) *ppos = pos + count;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 772) return count;
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 773) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 774) EXPORT_SYMBOL(simple_write_to_buffer);
6a727b43be8b0 (Jiri Slaby 2010-05-01 23:51:22 +0200 775)
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 776) /**
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 777) * memory_read_from_buffer - copy data from the buffer
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 778) * @to: the kernel space buffer to read to
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 779) * @count: the maximum number of bytes to read
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 780) * @ppos: the current position in the buffer
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 781) * @from: the buffer to read from
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 782) * @available: the size of the buffer
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 783) *
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 784) * The memory_read_from_buffer() function reads up to @count bytes from the
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 785) * buffer @from at offset @ppos into the kernel space address starting at @to.
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 786) *
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 787) * On success, the number of bytes read is returned and the offset @ppos is
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 788) * advanced by this number, or negative value is returned on error.
6d1029b56329b (Akinobu Mita 2008-07-04 09:59:51 -0700 789) **/
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 790) ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 791) const void *from, size_t available)
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 792) {
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 793) loff_t pos = *ppos;
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 794)
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 795) if (pos < 0)
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 796) return -EINVAL;
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 797) if (pos >= available)
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 798) return 0;
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 799) if (count > available - pos)
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 800) count = available - pos;
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 801) memcpy(to, from + pos, count);
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 802) *ppos = pos + count;
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 803)
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 804) return count;
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 805) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 806) EXPORT_SYMBOL(memory_read_from_buffer);
93b071139a956 (Akinobu Mita 2008-06-05 22:46:21 -0700 807)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 808) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 809) * Transaction based IO.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 810) * The file expects a single write which triggers the transaction, and then
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 811) * possibly a read which collects the result - which is stored in a
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 812) * file-local buffer.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 813) */
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 814)
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 815) void simple_transaction_set(struct file *file, size_t n)
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 816) {
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 817) struct simple_transaction_argresp *ar = file->private_data;
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 818)
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 819) BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 820)
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 821) /*
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 822) * The barrier ensures that ar->size will really remain zero until
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 823) * ar->data is ready for reading.
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 824) */
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 825) smp_mb();
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 826) ar->size = n;
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 827) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 828) EXPORT_SYMBOL(simple_transaction_set);
76791ab2d5e00 (Ingo Molnar 2009-03-25 16:48:35 +0100 829)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 830) char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 831) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 832) struct simple_transaction_argresp *ar;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 833) static DEFINE_SPINLOCK(simple_transaction_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 834)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 835) if (size > SIMPLE_TRANSACTION_LIMIT - 1)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 836) return ERR_PTR(-EFBIG);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 837)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 838) ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 839) if (!ar)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 840) return ERR_PTR(-ENOMEM);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 841)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 842) spin_lock(&simple_transaction_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 843)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 844) /* only one write allowed per open */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 845) if (file->private_data) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 846) spin_unlock(&simple_transaction_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 847) free_page((unsigned long)ar);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 848) return ERR_PTR(-EBUSY);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 849) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 850)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 851) file->private_data = ar;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 852)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 853) spin_unlock(&simple_transaction_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 854)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 855) if (copy_from_user(ar->data, buf, size))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 856) return ERR_PTR(-EFAULT);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 857)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 858) return ar->data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 859) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 860) EXPORT_SYMBOL(simple_transaction_get);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 861)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 862) ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 863) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 864) struct simple_transaction_argresp *ar = file->private_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 865)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 866) if (!ar)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 867) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 868) return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 869) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 870) EXPORT_SYMBOL(simple_transaction_read);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 871)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 872) int simple_transaction_release(struct inode *inode, struct file *file)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 873) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 874) free_page((unsigned long)file->private_data);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 875) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 876) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 877) EXPORT_SYMBOL(simple_transaction_release);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 878)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 879) /* Simple attribute files */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 880)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 881) struct simple_attr {
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 882) int (*get)(void *, u64 *);
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 883) int (*set)(void *, u64);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 884) char get_buf[24]; /* enough to store a u64 and "\n\0" */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 885) char set_buf[24];
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 886) void *data;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 887) const char *fmt; /* format for read operation */
7cf34c761db88 (Ingo Molnar 2006-03-23 03:00:36 -0800 888) struct mutex mutex; /* protects access to these buffers */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 889) };
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 890)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 891) /* simple_attr_open is called by an actual attribute open file operation
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 892) * to set the attribute specific access operations. */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 893) int simple_attr_open(struct inode *inode, struct file *file,
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 894) int (*get)(void *, u64 *), int (*set)(void *, u64),
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 895) const char *fmt)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 896) {
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 897) struct simple_attr *attr;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 898)
a65cab7d7f05c (Eric Biggers 2020-03-07 18:38:49 -0800 899) attr = kzalloc(sizeof(*attr), GFP_KERNEL);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 900) if (!attr)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 901) return -ENOMEM;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 902)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 903) attr->get = get;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 904) attr->set = set;
8e18e2941c534 (Theodore Ts'o 2006-09-27 01:50:46 -0700 905) attr->data = inode->i_private;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 906) attr->fmt = fmt;
7cf34c761db88 (Ingo Molnar 2006-03-23 03:00:36 -0800 907) mutex_init(&attr->mutex);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 908)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 909) file->private_data = attr;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 910)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 911) return nonseekable_open(inode, file);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 912) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 913) EXPORT_SYMBOL_GPL(simple_attr_open);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 914)
74bedc4d56211 (Christoph Hellwig 2008-02-08 04:20:28 -0800 915) int simple_attr_release(struct inode *inode, struct file *file)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 916) {
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 917) kfree(file->private_data);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 918) return 0;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 919) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 920) EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 921)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 922) /* read from the buffer that is filled with the get function */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 923) ssize_t simple_attr_read(struct file *file, char __user *buf,
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 924) size_t len, loff_t *ppos)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 925) {
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 926) struct simple_attr *attr;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 927) size_t size;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 928) ssize_t ret;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 929)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 930) attr = file->private_data;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 931)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 932) if (!attr->get)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 933) return -EACCES;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 934)
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 935) ret = mutex_lock_interruptible(&attr->mutex);
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 936) if (ret)
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 937) return ret;
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 938)
a65cab7d7f05c (Eric Biggers 2020-03-07 18:38:49 -0800 939) if (*ppos && attr->get_buf[0]) {
a65cab7d7f05c (Eric Biggers 2020-03-07 18:38:49 -0800 940) /* continued read */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 941) size = strlen(attr->get_buf);
a65cab7d7f05c (Eric Biggers 2020-03-07 18:38:49 -0800 942) } else {
a65cab7d7f05c (Eric Biggers 2020-03-07 18:38:49 -0800 943) /* first read */
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 944) u64 val;
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 945) ret = attr->get(attr->data, &val);
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 946) if (ret)
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 947) goto out;
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 948)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 949) size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 950) attr->fmt, (unsigned long long)val);
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 951) }
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 952)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 953) ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
8b88b0998e35d (Christoph Hellwig 2008-02-08 04:20:26 -0800 954) out:
7cf34c761db88 (Ingo Molnar 2006-03-23 03:00:36 -0800 955) mutex_unlock(&attr->mutex);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 956) return ret;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 957) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 958) EXPORT_SYMBOL_GPL(simple_attr_read);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 959)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 960) /* interpret the buffer as a number to call the set function with */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 961) ssize_t simple_attr_write(struct file *file, const char __user *buf,
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 962) size_t len, loff_t *ppos)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 963) {
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 964) struct simple_attr *attr;
488dac0c92376 (Yicong Yang 2020-11-21 22:17:19 -0800 965) unsigned long long val;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 966) size_t size;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 967) ssize_t ret;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 968)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 969) attr = file->private_data;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 970) if (!attr->set)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 971) return -EACCES;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 972)
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 973) ret = mutex_lock_interruptible(&attr->mutex);
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 974) if (ret)
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 975) return ret;
9261303ab7589 (Christoph Hellwig 2008-02-08 04:20:27 -0800 976)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 977) ret = -EFAULT;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 978) size = min(sizeof(attr->set_buf) - 1, len);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 979) if (copy_from_user(attr->set_buf, buf, size))
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 980) goto out;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 981)
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 982) attr->set_buf[size] = '\0';
488dac0c92376 (Yicong Yang 2020-11-21 22:17:19 -0800 983) ret = kstrtoull(attr->set_buf, 0, &val);
488dac0c92376 (Yicong Yang 2020-11-21 22:17:19 -0800 984) if (ret)
488dac0c92376 (Yicong Yang 2020-11-21 22:17:19 -0800 985) goto out;
05cc0cee6948f (Wu Fengguang 2009-09-18 13:06:03 -0700 986) ret = attr->set(attr->data, val);
05cc0cee6948f (Wu Fengguang 2009-09-18 13:06:03 -0700 987) if (ret == 0)
05cc0cee6948f (Wu Fengguang 2009-09-18 13:06:03 -0700 988) ret = len; /* on success, claim we got the whole input */
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 989) out:
7cf34c761db88 (Ingo Molnar 2006-03-23 03:00:36 -0800 990) mutex_unlock(&attr->mutex);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 991) return ret;
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 992) }
12f3887222454 (Al Viro 2013-09-15 21:20:49 -0400 993) EXPORT_SYMBOL_GPL(simple_attr_write);
acaefc25d21f8 (Arnd Bergmann 2005-05-18 14:40:59 +0200 994)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 995) /**
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 996) * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 997) * @sb: filesystem to do the file handle conversion on
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 998) * @fid: file handle to convert
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 999) * @fh_len: length of the file handle in bytes
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1000) * @fh_type: type of file handle
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1001) * @get_inode: filesystem callback to retrieve inode
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1002) *
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1003) * This function decodes @fid as long as it has one of the well-known
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1004) * Linux filehandle types and calls @get_inode on it to retrieve the
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1005) * inode for the object specified in the file handle.
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1006) */
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1007) struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1008) int fh_len, int fh_type, struct inode *(*get_inode)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1009) (struct super_block *sb, u64 ino, u32 gen))
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1010) {
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1011) struct inode *inode = NULL;
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1012)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1013) if (fh_len < 2)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1014) return NULL;
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1015)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1016) switch (fh_type) {
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1017) case FILEID_INO32_GEN:
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1018) case FILEID_INO32_GEN_PARENT:
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1019) inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1020) break;
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1021) }
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1022)
4ea3ada2955e4 (Christoph Hellwig 2008-08-11 15:48:57 +0200 1023) return d_obtain_alias(inode);
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1024) }
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1025) EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1026)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1027) /**
ca1868309be96 (Yanchuan Nian 2012-09-05 16:31:29 +0800 1028) * generic_fh_to_parent - generic helper for the fh_to_parent export operation
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1029) * @sb: filesystem to do the file handle conversion on
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1030) * @fid: file handle to convert
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1031) * @fh_len: length of the file handle in bytes
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1032) * @fh_type: type of file handle
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1033) * @get_inode: filesystem callback to retrieve inode
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1034) *
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1035) * This function decodes @fid as long as it has one of the well-known
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1036) * Linux filehandle types and calls @get_inode on it to retrieve the
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1037) * inode for the _parent_ object specified in the file handle if it
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1038) * is specified in the file handle, or NULL otherwise.
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1039) */
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1040) struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1041) int fh_len, int fh_type, struct inode *(*get_inode)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1042) (struct super_block *sb, u64 ino, u32 gen))
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1043) {
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1044) struct inode *inode = NULL;
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1045)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1046) if (fh_len <= 2)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1047) return NULL;
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1048)
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1049) switch (fh_type) {
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1050) case FILEID_INO32_GEN_PARENT:
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1051) inode = get_inode(sb, fid->i32.parent_ino,
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1052) (fh_len > 3 ? fid->i32.parent_gen : 0));
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1053) break;
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1054) }
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1055)
4ea3ada2955e4 (Christoph Hellwig 2008-08-11 15:48:57 +0200 1056) return d_obtain_alias(inode);
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1057) }
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1058) EXPORT_SYMBOL_GPL(generic_fh_to_parent);
2596110a39945 (Christoph Hellwig 2007-10-21 16:42:05 -0700 1059)
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1060) /**
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1061) * __generic_file_fsync - generic fsync implementation for simple filesystems
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1062) *
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1063) * @file: file to synchronize
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1064) * @start: start offset in bytes
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1065) * @end: end offset in bytes (inclusive)
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1066) * @datasync: only synchronize essential metadata if true
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1067) *
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1068) * This is a generic implementation of the fsync method for simple
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1069) * filesystems which track all non-inode metadata in the buffers list
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1070) * hanging off the address_space structure.
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1071) */
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1072) int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1073) int datasync)
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1074) {
7ea8085910ef3 (Christoph Hellwig 2010-05-26 17:53:25 +0200 1075) struct inode *inode = file->f_mapping->host;
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1076) int err;
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1077) int ret;
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1078)
383aa543c2f46 (Jeff Layton 2017-07-06 07:02:29 -0400 1079) err = file_write_and_wait_range(file, start, end);
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1080) if (err)
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1081) return err;
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1082)
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 1083) inode_lock(inode);
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1084) ret = sync_mapping_buffers(inode->i_mapping);
0ae45f63d4ef8 (Theodore Ts'o 2015-02-02 00:37:00 -0500 1085) if (!(inode->i_state & I_DIRTY_ALL))
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1086) goto out;
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1087) if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1088) goto out;
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1089)
c37650161a53c (Christoph Hellwig 2010-10-06 10:48:20 +0200 1090) err = sync_inode_metadata(inode, 1);
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1091) if (ret == 0)
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1092) ret = err;
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1093)
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1094) out:
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 1095) inode_unlock(inode);
383aa543c2f46 (Jeff Layton 2017-07-06 07:02:29 -0400 1096) /* check and advance again to catch errors after syncing out buffers */
383aa543c2f46 (Jeff Layton 2017-07-06 07:02:29 -0400 1097) err = file_check_and_advance_wb_err(file);
383aa543c2f46 (Jeff Layton 2017-07-06 07:02:29 -0400 1098) if (ret == 0)
383aa543c2f46 (Jeff Layton 2017-07-06 07:02:29 -0400 1099) ret = err;
383aa543c2f46 (Jeff Layton 2017-07-06 07:02:29 -0400 1100) return ret;
d5aacad548db1 (Al Viro 2009-06-07 14:56:44 -0400 1101) }
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1102) EXPORT_SYMBOL(__generic_file_fsync);
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1103)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1104) /**
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1105) * generic_file_fsync - generic fsync implementation for simple filesystems
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1106) * with flush
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1107) * @file: file to synchronize
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1108) * @start: start offset in bytes
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1109) * @end: end offset in bytes (inclusive)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1110) * @datasync: only synchronize essential metadata if true
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1111) *
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1112) */
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1113)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1114) int generic_file_fsync(struct file *file, loff_t start, loff_t end,
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1115) int datasync)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1116) {
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1117) struct inode *inode = file->f_mapping->host;
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1118) int err;
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1119)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1120) err = __generic_file_fsync(file, start, end, datasync);
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1121) if (err)
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1122) return err;
c6bf3f0e25f4c (Christoph Hellwig 2021-01-26 15:52:35 +0100 1123) return blkdev_issue_flush(inode->i_sb->s_bdev);
ac13a829f6adb (Fabian Frederick 2014-06-04 16:06:27 -0700 1124) }
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1125) EXPORT_SYMBOL(generic_file_fsync);
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1126)
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1127) /**
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1128) * generic_check_addressable - Check addressability of file system
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1129) * @blocksize_bits: log of file system block size
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1130) * @num_blocks: number of blocks in file system
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1131) *
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1132) * Determine whether a file system with @num_blocks blocks (and a
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1133) * block size of 2**@blocksize_bits) is addressable by the sector_t
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1134) * and page cache of the system. Return 0 if so and -EFBIG otherwise.
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1135) */
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1136) int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1137) {
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1138) u64 last_fs_block = num_blocks - 1;
a33f13efe0519 (Joel Becker 2010-08-16 12:10:17 -0700 1139) u64 last_fs_page =
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1140) last_fs_block >> (PAGE_SHIFT - blocksize_bits);
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1141)
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1142) if (unlikely(num_blocks == 0))
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1143) return 0;
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1144)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1145) if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1146) return -EINVAL;
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1147)
a33f13efe0519 (Joel Becker 2010-08-16 12:10:17 -0700 1148) if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
a33f13efe0519 (Joel Becker 2010-08-16 12:10:17 -0700 1149) (last_fs_page > (pgoff_t)(~0ULL))) {
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1150) return -EFBIG;
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1151) }
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1152) return 0;
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1153) }
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1154) EXPORT_SYMBOL(generic_check_addressable);
30ca22c70e3ef (Patrick J. LoPresti 2010-07-22 15:03:41 -0700 1155)
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1156) /*
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1157) * No-op implementation of ->fsync for in-memory filesystems.
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1158) */
02c24a82187d5 (Josef Bacik 2011-07-16 20:44:56 -0400 1159) int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1160) {
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1161) return 0;
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1162) }
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 1163) EXPORT_SYMBOL(noop_fsync);
87dc800be2499 (Al Viro 2013-09-16 10:30:04 -0400 1164)
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1165) int noop_set_page_dirty(struct page *page)
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1166) {
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1167) /*
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1168) * Unlike __set_page_dirty_no_writeback that handles dirty page
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1169) * tracking in the page object, dax does all dirty tracking in
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1170) * the inode address_space in response to mkwrite faults. In the
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1171) * dax case we only need to worry about potentially dirty CPU
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1172) * caches, not dirty page cache pages to write back.
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1173) *
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1174) * This callback is defined to prevent fallback to
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1175) * __set_page_dirty_buffers() in set_page_dirty().
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1176) */
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1177) return 0;
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1178) }
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1179) EXPORT_SYMBOL_GPL(noop_set_page_dirty);
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1180)
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1181) void noop_invalidatepage(struct page *page, unsigned int offset,
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1182) unsigned int length)
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1183) {
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1184) /*
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1185) * There is no page cache to invalidate in the dax case, however
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1186) * we need this callback defined to prevent falling back to
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1187) * block_invalidatepage() in do_invalidatepage().
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1188) */
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1189) }
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1190) EXPORT_SYMBOL_GPL(noop_invalidatepage);
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1191)
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1192) ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1193) {
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1194) /*
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1195) * iomap based filesystems support direct I/O without need for
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1196) * this callback. However, it still needs to be set in
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1197) * inode->a_ops so that open/fcntl know that direct I/O is
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1198) * generally supported.
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1199) */
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1200) return -EINVAL;
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1201) }
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1202) EXPORT_SYMBOL_GPL(noop_direct_IO);
f44c77630d26c (Dan Williams 2018-03-07 15:26:44 -0800 1203)
fceef393a5381 (Al Viro 2015-12-29 15:58:39 -0500 1204) /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
fceef393a5381 (Al Viro 2015-12-29 15:58:39 -0500 1205) void kfree_link(void *p)
87dc800be2499 (Al Viro 2013-09-16 10:30:04 -0400 1206) {
fceef393a5381 (Al Viro 2015-12-29 15:58:39 -0500 1207) kfree(p);
87dc800be2499 (Al Viro 2013-09-16 10:30:04 -0400 1208) }
fceef393a5381 (Al Viro 2015-12-29 15:58:39 -0500 1209) EXPORT_SYMBOL(kfree_link);
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1210)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1211) /*
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1212) * nop .set_page_dirty method so that people can use .page_mkwrite on
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1213) * anon inodes.
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1214) */
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1215) static int anon_set_page_dirty(struct page *page)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1216) {
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1217) return 0;
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1218) };
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1219)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1220) struct inode *alloc_anon_inode(struct super_block *s)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1221) {
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1222) static const struct address_space_operations anon_aops = {
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1223) .set_page_dirty = anon_set_page_dirty,
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1224) };
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1225) struct inode *inode = new_inode_pseudo(s);
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1226)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1227) if (!inode)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1228) return ERR_PTR(-ENOMEM);
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1229)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1230) inode->i_ino = get_next_ino();
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1231) inode->i_mapping->a_ops = &anon_aops;
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1232)
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1233) /*
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1234) * Mark the inode dirty from the very beginning,
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1235) * that way it will never be moved to the dirty
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1236) * list because mark_inode_dirty() will think
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1237) * that it already _is_ on the dirty list.
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1238) */
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1239) inode->i_state = I_DIRTY;
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1240) inode->i_mode = S_IRUSR | S_IWUSR;
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1241) inode->i_uid = current_fsuid();
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1242) inode->i_gid = current_fsgid();
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1243) inode->i_flags |= S_PRIVATE;
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 1244) inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1245) return inode;
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1246) }
6987843ff7e83 (Al Viro 2013-10-02 22:35:11 -0400 1247) EXPORT_SYMBOL(alloc_anon_inode);
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1248)
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1249) /**
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1250) * simple_nosetlease - generic helper for prohibiting leases
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1251) * @filp: file pointer
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1252) * @arg: type of lease to obtain
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1253) * @flp: new lease supplied for insertion
e6f5c78930e40 (Jeff Layton 2014-08-22 10:40:25 -0400 1254) * @priv: private data for lm_setup operation
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1255) *
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1256) * Generic helper for filesystems that do not wish to allow leases to be set.
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1257) * All arguments are ignored and it just returns -EINVAL.
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1258) */
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1259) int
e6f5c78930e40 (Jeff Layton 2014-08-22 10:40:25 -0400 1260) simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
e6f5c78930e40 (Jeff Layton 2014-08-22 10:40:25 -0400 1261) void **priv)
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1262) {
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1263) return -EINVAL;
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1264) }
1c994a0909a55 (Jeff Layton 2014-08-27 06:49:41 -0400 1265) EXPORT_SYMBOL(simple_nosetlease);
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1266)
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1267) /**
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1268) * simple_get_link - generic helper to get the target of "fast" symlinks
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1269) * @dentry: not used here
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1270) * @inode: the symlink inode
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1271) * @done: not used here
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1272) *
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1273) * Generic helper for filesystems to use for symlink inodes where a pointer to
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1274) * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1275) * since as an optimization the path lookup code uses any non-NULL ->i_link
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1276) * directly, without calling ->get_link(). But ->get_link() still must be set,
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1277) * to mark the inode_operations as being for a symlink.
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1278) *
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1279) * Return: the symlink target
6ee9706aa22e0 (Eric Biggers 2019-04-11 16:16:30 -0700 1280) */
6b2553918d8b4 (Al Viro 2015-11-17 10:20:54 -0500 1281) const char *simple_get_link(struct dentry *dentry, struct inode *inode,
fceef393a5381 (Al Viro 2015-12-29 15:58:39 -0500 1282) struct delayed_call *done)
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1283) {
6b2553918d8b4 (Al Viro 2015-11-17 10:20:54 -0500 1284) return inode->i_link;
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1285) }
6b2553918d8b4 (Al Viro 2015-11-17 10:20:54 -0500 1286) EXPORT_SYMBOL(simple_get_link);
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1287)
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1288) const struct inode_operations simple_symlink_inode_operations = {
6b2553918d8b4 (Al Viro 2015-11-17 10:20:54 -0500 1289) .get_link = simple_get_link,
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1290) };
61ba64fc07688 (Al Viro 2015-05-02 09:54:06 -0400 1291) EXPORT_SYMBOL(simple_symlink_inode_operations);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1292)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1293) /*
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1294) * Operations for a permanently empty directory.
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1295) */
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1296) static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1297) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1298) return ERR_PTR(-ENOENT);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1299) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1300)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 1301) static int empty_dir_getattr(struct user_namespace *mnt_userns,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 1302) const struct path *path, struct kstat *stat,
a528d35e8bfcc (David Howells 2017-01-31 16:46:22 +0000 1303) u32 request_mask, unsigned int query_flags)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1304) {
a528d35e8bfcc (David Howells 2017-01-31 16:46:22 +0000 1305) struct inode *inode = d_inode(path->dentry);
0d56a4518d5ea (Christian Brauner 2021-01-21 14:19:30 +0100 1306) generic_fillattr(&init_user_ns, inode, stat);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1307) return 0;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1308) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1309)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 1310) static int empty_dir_setattr(struct user_namespace *mnt_userns,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 1311) struct dentry *dentry, struct iattr *attr)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1312) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1313) return -EPERM;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1314) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1315)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1316) static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1317) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1318) return -EOPNOTSUPP;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1319) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1320)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1321) static const struct inode_operations empty_dir_inode_operations = {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1322) .lookup = empty_dir_lookup,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1323) .permission = generic_permission,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1324) .setattr = empty_dir_setattr,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1325) .getattr = empty_dir_getattr,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1326) .listxattr = empty_dir_listxattr,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1327) };
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1328)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1329) static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1330) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1331) /* An empty directory has two entries . and .. at offsets 0 and 1 */
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1332) return generic_file_llseek_size(file, offset, whence, 2, 2);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1333) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1334)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1335) static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1336) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1337) dir_emit_dots(file, ctx);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1338) return 0;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1339) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1340)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1341) static const struct file_operations empty_dir_operations = {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1342) .llseek = empty_dir_llseek,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1343) .read = generic_read_dir,
c51da20c48b76 (Al Viro 2016-04-30 22:37:34 -0400 1344) .iterate_shared = empty_dir_readdir,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1345) .fsync = noop_fsync,
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1346) };
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1347)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1348)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1349) void make_empty_dir_inode(struct inode *inode)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1350) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1351) set_nlink(inode, 2);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1352) inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1353) inode->i_uid = GLOBAL_ROOT_UID;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1354) inode->i_gid = GLOBAL_ROOT_GID;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1355) inode->i_rdev = 0;
4b75de8615050 (Eric W. Biederman 2015-08-12 15:00:12 -0500 1356) inode->i_size = 0;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1357) inode->i_blkbits = PAGE_SHIFT;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1358) inode->i_blocks = 0;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1359)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1360) inode->i_op = &empty_dir_inode_operations;
f5c244383725a (Andreas Gruenbacher 2016-09-29 17:48:41 +0200 1361) inode->i_opflags &= ~IOP_XATTR;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1362) inode->i_fop = &empty_dir_operations;
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1363) }
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1364)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1365) bool is_empty_dir_inode(struct inode *inode)
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1366) {
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1367) return (inode->i_fop == &empty_dir_operations) &&
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1368) (inode->i_op == &empty_dir_inode_operations);
fbabfd0f4ee2e (Eric W. Biederman 2015-05-09 15:54:49 -0500 1369) }
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1370)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1371) #ifdef CONFIG_UNICODE
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1372) /*
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1373) * Determine if the name of a dentry should be casefolded.
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1374) *
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1375) * Return: if names will need casefolding
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1376) */
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1377) static bool needs_casefold(const struct inode *dir)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1378) {
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1379) return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1380) }
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1381)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1382) /**
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1383) * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1384) * @dentry: dentry whose name we are checking against
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1385) * @len: len of name of dentry
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1386) * @str: str pointer to name of dentry
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1387) * @name: Name to compare against
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1388) *
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1389) * Return: 0 if names match, 1 if mismatch, or -ERRNO
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1390) */
794c43f716845 (Eric Biggers 2020-12-28 15:25:29 -0800 1391) static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
794c43f716845 (Eric Biggers 2020-12-28 15:25:29 -0800 1392) const char *str, const struct qstr *name)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1393) {
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1394) const struct dentry *parent = READ_ONCE(dentry->d_parent);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1395) const struct inode *dir = READ_ONCE(parent->d_inode);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1396) const struct super_block *sb = dentry->d_sb;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1397) const struct unicode_map *um = sb->s_encoding;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1398) struct qstr qstr = QSTR_INIT(str, len);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1399) char strbuf[DNAME_INLINE_LEN];
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1400) int ret;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1401)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1402) if (!dir || !needs_casefold(dir))
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1403) goto fallback;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1404) /*
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1405) * If the dentry name is stored in-line, then it may be concurrently
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1406) * modified by a rename. If this happens, the VFS will eventually retry
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1407) * the lookup, so it doesn't matter what ->d_compare() returns.
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1408) * However, it's unsafe to call utf8_strncasecmp() with an unstable
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1409) * string. Therefore, we have to copy the name into a temporary buffer.
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1410) */
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1411) if (len <= DNAME_INLINE_LEN - 1) {
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1412) memcpy(strbuf, str, len);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1413) strbuf[len] = 0;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1414) qstr.name = strbuf;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1415) /* prevent compiler from optimizing out the temporary buffer */
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1416) barrier();
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1417) }
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1418) ret = utf8_strncasecmp(um, name, &qstr);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1419) if (ret >= 0)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1420) return ret;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1421)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1422) if (sb_has_strict_encoding(sb))
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1423) return -EINVAL;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1424) fallback:
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1425) if (len != name->len)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1426) return 1;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1427) return !!memcmp(str, name->name, len);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1428) }
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1429)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1430) /**
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1431) * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1432) * @dentry: dentry of the parent directory
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1433) * @str: qstr of name whose hash we should fill in
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1434) *
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1435) * Return: 0 if hash was successful or unchanged, and -EINVAL on error
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1436) */
794c43f716845 (Eric Biggers 2020-12-28 15:25:29 -0800 1437) static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1438) {
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1439) const struct inode *dir = READ_ONCE(dentry->d_inode);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1440) struct super_block *sb = dentry->d_sb;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1441) const struct unicode_map *um = sb->s_encoding;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1442) int ret = 0;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1443)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1444) if (!dir || !needs_casefold(dir))
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1445) return 0;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1446)
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1447) ret = utf8_casefold_hash(um, dentry, str);
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1448) if (ret < 0 && sb_has_strict_encoding(sb))
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1449) return -EINVAL;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1450) return 0;
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1451) }
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1452)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1453) static const struct dentry_operations generic_ci_dentry_ops = {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1454) .d_hash = generic_ci_d_hash,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1455) .d_compare = generic_ci_d_compare,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1456) };
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1457) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1458)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1459) #ifdef CONFIG_FS_ENCRYPTION
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1460) static const struct dentry_operations generic_encrypted_dentry_ops = {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1461) .d_revalidate = fscrypt_d_revalidate,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1462) };
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1463) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1464)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1465) #if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1466) static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1467) .d_hash = generic_ci_d_hash,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1468) .d_compare = generic_ci_d_compare,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1469) .d_revalidate = fscrypt_d_revalidate,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1470) };
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1471) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1472)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1473) /**
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1474) * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1475) * @dentry: dentry to set ops on
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1476) *
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1477) * Casefolded directories need d_hash and d_compare set, so that the dentries
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1478) * contained in them are handled case-insensitively. Note that these operations
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1479) * are needed on the parent directory rather than on the dentries in it, and
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1480) * while the casefolding flag can be toggled on and off on an empty directory,
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1481) * dentry_operations can't be changed later. As a result, if the filesystem has
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1482) * casefolding support enabled at all, we have to give all dentries the
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1483) * casefolding operations even if their inode doesn't have the casefolding flag
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1484) * currently (and thus the casefolding ops would be no-ops for now).
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1485) *
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1486) * Encryption works differently in that the only dentry operation it needs is
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1487) * d_revalidate, which it only needs on dentries that have the no-key name flag.
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1488) * The no-key flag can't be set "later", so we don't have to worry about that.
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1489) *
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1490) * Finally, to maximize compatibility with overlayfs (which isn't compatible
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1491) * with certain dentry operations) and to avoid taking an unnecessary
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1492) * performance hit, we use custom dentry_operations for each possible
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1493) * combination rather than always installing all operations.
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1494) */
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1495) void generic_set_encrypted_ci_d_ops(struct dentry *dentry)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1496) {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1497) #ifdef CONFIG_FS_ENCRYPTION
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1498) bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME;
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1499) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1500) #ifdef CONFIG_UNICODE
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1501) bool needs_ci_ops = dentry->d_sb->s_encoding;
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1502) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1503) #if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1504) if (needs_encrypt_ops && needs_ci_ops) {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1505) d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1506) return;
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1507) }
c843843e714c8 (Daniel Rosenberg 2020-07-08 02:12:35 -0700 1508) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1509) #ifdef CONFIG_FS_ENCRYPTION
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1510) if (needs_encrypt_ops) {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1511) d_set_d_op(dentry, &generic_encrypted_dentry_ops);
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1512) return;
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1513) }
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1514) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1515) #ifdef CONFIG_UNICODE
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1516) if (needs_ci_ops) {
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1517) d_set_d_op(dentry, &generic_ci_dentry_ops);
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1518) return;
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1519) }
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1520) #endif
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1521) }
608af703519a5 (Daniel Rosenberg 2020-11-19 06:09:02 +0000 1522) EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);