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