^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2018 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/fs_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fsverity.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mmap_lock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <uapi/linux/incrementalfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "vfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "data_mgmt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "pseudo_files.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "verity.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int incfs_remount_fs(struct super_block *sb, int *flags, char *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int dentry_revalidate(struct dentry *dentry, unsigned int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static void dentry_release(struct dentry *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int iterate_incfs_dir(struct file *file, struct dir_context *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct dentry *dir_lookup(struct inode *dir_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct dentry *dentry, unsigned int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int dir_unlink(struct inode *dir, struct dentry *dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int dir_link(struct dentry *old_dentry, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct dentry *new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int dir_rmdir(struct inode *dir, struct dentry *dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int dir_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct inode *new_dir, struct dentry *new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int file_open(struct inode *inode, struct file *file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int file_release(struct inode *inode, struct file *file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int read_single_page(struct file *f, struct page *page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static struct inode *alloc_inode(struct super_block *sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void free_inode(struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void evict_inode(struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int incfs_setattr(struct dentry *dentry, struct iattr *ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int incfs_getattr(const struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct kstat *stat, u32 request_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int query_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static ssize_t incfs_getxattr(struct dentry *d, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) void *value, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static ssize_t incfs_setxattr(struct dentry *d, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) const void *value, size_t size, int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static ssize_t incfs_listxattr(struct dentry *d, char *list, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int show_options(struct seq_file *, struct dentry *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static const struct super_operations incfs_super_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .statfs = simple_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .remount_fs = incfs_remount_fs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .alloc_inode = alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .destroy_inode = free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .evict_inode = evict_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .show_options = show_options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int dir_rename_wrap(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return dir_rename(old_dir, old_dentry, new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static const struct inode_operations incfs_dir_inode_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .lookup = dir_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .mkdir = dir_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .rename = dir_rename_wrap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .unlink = dir_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .link = dir_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .rmdir = dir_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .setattr = incfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static const struct file_operations incfs_dir_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .iterate = iterate_incfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .open = file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .release = file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct dentry_operations incfs_dentry_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .d_revalidate = dentry_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .d_release = dentry_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static const struct address_space_operations incfs_address_space_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .readpage = read_single_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* .readpages = readpages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static vm_fault_t incfs_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) vmf->flags &= ~FAULT_FLAG_ALLOW_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return filemap_fault(vmf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static const struct vm_operations_struct incfs_file_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .fault = incfs_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .map_pages = filemap_map_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .page_mkwrite = filemap_page_mkwrite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* This is used for a general mmap of a disk file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int incfs_file_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct address_space *mapping = file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!mapping->a_ops->readpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) file_accessed(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) vma->vm_ops = &incfs_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) const struct file_operations incfs_file_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .open = file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .release = file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .read_iter = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .mmap = incfs_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .unlocked_ioctl = dispatch_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .compat_ioctl = incfs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) const struct inode_operations incfs_file_inode_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .setattr = incfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .getattr = incfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .listxattr = incfs_listxattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int incfs_handler_getxattr(const struct xattr_handler *xh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct dentry *d, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) const char *name, void *buffer, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return incfs_getxattr(d, name, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int incfs_handler_setxattr(const struct xattr_handler *xh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct dentry *d, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) const char *name, const void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return incfs_setxattr(d, name, buffer, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static const struct xattr_handler incfs_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .prefix = "", /* AKA all attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .get = incfs_handler_getxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .set = incfs_handler_setxattr,
^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) static const struct xattr_handler *incfs_xattr_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) &incfs_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct inode_search {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct dentry *backing_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) bool verity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) enum parse_parameter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) Opt_read_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) Opt_readahead_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) Opt_rlog_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) Opt_rlog_wakeup_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) Opt_report_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) Opt_sysfs_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) Opt_err
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static const match_table_t option_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) { Opt_read_timeout, "read_timeout_ms=%u" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) { Opt_readahead_pages, "readahead=%u" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) { Opt_rlog_pages, "rlog_pages=%u" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) { Opt_rlog_wakeup_cnt, "rlog_wakeup_cnt=%u" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) { Opt_report_uid, "report_uid" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) { Opt_sysfs_name, "sysfs_name=%s" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) { Opt_err, NULL }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void free_options(struct mount_options *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) kfree(opts->sysfs_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) opts->sysfs_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int parse_options(struct mount_options *opts, char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) char *position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (opts == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) *opts = (struct mount_options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .read_timeout_ms = 1000, /* Default: 1s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .readahead_pages = 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) .read_log_pages = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) .read_log_wakeup_count = 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (str == NULL || *str == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) while ((position = strsep(&str, ",")) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!*position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) token = match_token(position, option_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) switch (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case Opt_read_timeout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (match_int(&args[0], &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (value > 3600000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) opts->read_timeout_ms = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) case Opt_readahead_pages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (match_int(&args[0], &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) opts->readahead_pages = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case Opt_rlog_pages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (match_int(&args[0], &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) opts->read_log_pages = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case Opt_rlog_wakeup_cnt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (match_int(&args[0], &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) opts->read_log_wakeup_count = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) case Opt_report_uid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) opts->report_uid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) case Opt_sysfs_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) opts->sysfs_name = match_strdup(&args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) free_options(opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* Read file size from the attribute. Quicker than reading the header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static u64 read_size_attr(struct dentry *backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) __le64 attr_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ssize_t bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) bytes_read = vfs_getxattr(backing_dentry, INCFS_XATTR_SIZE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) (char *)&attr_value, sizeof(attr_value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (bytes_read != sizeof(attr_value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return le64_to_cpu(attr_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* Read verity flag from the attribute. Quicker than reading the header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static bool read_verity_attr(struct dentry *backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return vfs_getxattr(backing_dentry, INCFS_XATTR_VERITY_NAME, NULL, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int inode_test(struct inode *inode, void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct inode_search *search = opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct inode_info *node = get_incfs_node(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct inode *backing_inode = d_inode(search->backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return node->n_backing_inode == backing_inode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) inode->i_ino == search->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int inode_set(struct inode *inode, void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct inode_search *search = opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct inode_info *node = get_incfs_node(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct dentry *backing_dentry = search->backing_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct inode *backing_inode = d_inode(backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) fsstack_copy_attr_all(inode, backing_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) u64 size = search->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) inode->i_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) inode->i_blocks = get_blocks_count_for_size(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) inode->i_mapping->a_ops = &incfs_address_space_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) inode->i_op = &incfs_file_inode_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) inode->i_fop = &incfs_file_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) inode->i_mode &= ~0222;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (search->verity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) inode_set_flags(inode, S_VERITY, S_VERITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) } else if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) inode->i_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) inode->i_mapping->a_ops = &incfs_address_space_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) inode->i_op = &incfs_dir_inode_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) inode->i_fop = &incfs_dir_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) pr_warn_once("incfs: Unexpected inode type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ihold(backing_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) node->n_backing_inode = backing_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) node->n_mount_info = get_mount_info(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) inode->i_ctime = backing_inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) inode->i_mtime = backing_inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) inode->i_atime = backing_inode->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) inode->i_ino = backing_inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (backing_inode->i_ino < INCFS_START_INO_RANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pr_warn("incfs: ino conflict with backing FS %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) backing_inode->i_ino);
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static struct inode *fetch_regular_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct dentry *backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct inode *backing_inode = d_inode(backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct inode_search search = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) .ino = backing_inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .backing_dentry = backing_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .size = read_size_attr(backing_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) .verity = read_verity_attr(backing_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct inode *inode = iget5_locked(sb, search.ino, inode_test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) inode_set, &search);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (inode->i_state & I_NEW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int iterate_incfs_dir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct dir_file *dir = get_incfs_dir_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct mount_info *mi = get_mount_info(file_superblock(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) bool root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) error = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) root = dir->backing_dir->f_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) == d_inode(mi->mi_backing_dir_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) error = emit_pseudo_files(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ctx->pos -= PSEUDO_FILE_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) error = iterate_dir(dir->backing_dir, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ctx->pos += PSEUDO_FILE_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) file->f_pos = dir->backing_dir->f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) pr_warn("incfs: %s %s %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) file->f_path.dentry->d_name.name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static int incfs_init_dentry(struct dentry *dentry, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct dentry_info *d_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (!dentry || !path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) d_info = kzalloc(sizeof(*d_info), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (!d_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) d_info->backing_path = *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) path_get(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) dentry->d_fsdata = d_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static struct dentry *open_or_create_special_dir(struct dentry *backing_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) bool *created)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct dentry *index_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct inode *backing_inode = d_inode(backing_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) index_dentry = incfs_lookup_dentry(backing_dir, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!index_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) } else if (IS_ERR(index_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return index_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) } else if (d_really_is_positive(index_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /* Index already exists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) *created = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return index_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /* Index needs to be created. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) inode_lock_nested(backing_inode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) err = vfs_mkdir(backing_inode, index_dentry, 0777);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) inode_unlock(backing_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dput(index_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (!d_really_is_positive(index_dentry) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) unlikely(d_unhashed(index_dentry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dput(index_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return ERR_PTR(-EINVAL);
^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) *created = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return index_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int read_single_page_timeouts(struct data_file *df, struct file *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int block_index, struct mem_range range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct mem_range tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct mount_info *mi = df->df_mount_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct incfs_read_data_file_timeouts timeouts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) .max_pending_time_us = U32_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int uid = current_uid().val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) spin_lock(&mi->mi_per_uid_read_timeouts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) for (i = 0; i < mi->mi_per_uid_read_timeouts_size /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) sizeof(*mi->mi_per_uid_read_timeouts); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct incfs_per_uid_read_timeouts *t =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) &mi->mi_per_uid_read_timeouts[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if(t->uid == uid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) timeouts.min_time_us = t->min_time_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) timeouts.min_pending_time_us = t->min_pending_time_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) timeouts.max_pending_time_us = t->max_pending_time_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (timeouts.max_pending_time_us == U32_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) u64 read_timeout_us = (u64)mi->mi_options.read_timeout_ms *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) timeouts.max_pending_time_us = read_timeout_us <= U32_MAX ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) read_timeout_us : U32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return incfs_read_data_file_block(range, f, block_index, tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) &timeouts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static int read_single_page(struct file *f, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) loff_t offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) loff_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) ssize_t bytes_to_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ssize_t read_result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct data_file *df = get_incfs_data_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) void *page_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int block_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (!df) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) page_start = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) offset = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) block_index = (offset + df->df_mapped_offset) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) INCFS_DATA_FILE_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) size = df->df_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (offset < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct mem_range tmp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) .len = 2 * INCFS_DATA_FILE_BLOCK_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (!tmp.data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) read_result = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) bytes_to_read = min_t(loff_t, size - offset, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) read_result = read_single_page_timeouts(df, f, block_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) range(page_start, bytes_to_read), tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) free_pages((unsigned long)tmp.data, get_order(tmp.len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) bytes_to_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) read_result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (read_result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) result = read_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) else if (read_result < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) zero_user(page, read_result, PAGE_SIZE - read_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (result == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int incfs_link(struct dentry *what, struct dentry *where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct dentry *parent_dentry = dget_parent(where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct inode *pinode = d_inode(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) inode_lock_nested(pinode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) error = vfs_link(what, pinode, where, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) inode_unlock(pinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) dput(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) int incfs_unlink(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct dentry *parent_dentry = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct inode *pinode = d_inode(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) inode_lock_nested(pinode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) error = vfs_unlink(pinode, dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) inode_unlock(pinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dput(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static int incfs_rmdir(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct dentry *parent_dentry = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct inode *pinode = d_inode(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) inode_lock_nested(pinode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) error = vfs_rmdir(pinode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) inode_unlock(pinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) dput(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) static void notify_unlink(struct dentry *dentry, const char *file_id_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) const char *special_directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) struct dentry *root = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) struct dentry *file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct dentry *dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) bool take_lock = root->d_parent != root->d_parent->d_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) while (root != root->d_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) root = root->d_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (take_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) dir = incfs_lookup_dentry(root, special_directory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) dir = lookup_one_len(special_directory, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) strlen(special_directory));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (IS_ERR(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) error = PTR_ERR(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (d_is_negative(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) file = incfs_lookup_dentry(dir, file_id_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) error = PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (d_is_negative(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) fsnotify_unlink(d_inode(dir), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) d_delete(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) pr_warn("%s failed with error %d\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) dput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static void maybe_delete_incomplete_file(struct file *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct data_file *df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) struct backing_file_context *bfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct mount_info *mi = df->df_mount_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) char *file_id_str = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct dentry *incomplete_file_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) const struct cred *old_cred = override_creds(mi->mi_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (atomic_read(&df->df_data_blocks_written) < df->df_data_block_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /* Truncate file to remove any preallocated space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) bfc = df->df_backing_file_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (bfc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct file *f = bfc->bc_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) loff_t size = i_size_read(file_inode(f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) error = vfs_truncate(&f->f_path, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) /* No useful action on failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pr_warn("incfs: Failed to truncate complete file: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /* This is best effort - there is no useful action to take on failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) file_id_str = file_id_to_str(df->df_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (!file_id_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) incomplete_file_dentry = incfs_lookup_dentry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) df->df_mount_info->mi_incomplete_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) file_id_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (!incomplete_file_dentry || IS_ERR(incomplete_file_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) incomplete_file_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (!d_really_is_positive(incomplete_file_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) vfs_fsync(df->df_backing_file_context->bc_file, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) error = incfs_unlink(incomplete_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) pr_warn("incfs: Deleting incomplete file failed: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) notify_unlink(f->f_path.dentry, file_id_str, INCFS_INCOMPLETE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) dput(incomplete_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) kfree(file_id_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) static long ioctl_fill_blocks(struct file *f, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct incfs_fill_blocks __user *usr_fill_blocks = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) struct incfs_fill_blocks fill_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) struct incfs_fill_block __user *usr_fill_block_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct data_file *df = get_incfs_data_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct incfs_file_data *fd = f->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) const ssize_t data_buf_size = 2 * INCFS_DATA_FILE_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) u8 *data_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) ssize_t error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (!df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (!fd || fd->fd_fill_permission != CAN_FILL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (copy_from_user(&fill_blocks, usr_fill_blocks, sizeof(fill_blocks)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) usr_fill_block_array = u64_to_user_ptr(fill_blocks.fill_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) data_buf = (u8 *)__get_free_pages(GFP_NOFS | __GFP_COMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) get_order(data_buf_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (!data_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) for (i = 0; i < fill_blocks.count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) struct incfs_fill_block fill_block = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (copy_from_user(&fill_block, &usr_fill_block_array[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) sizeof(fill_block)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (fill_block.data_len > data_buf_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) error = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) if (copy_from_user(data_buf, u64_to_user_ptr(fill_block.data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) fill_block.data_len) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) fill_block.data = 0; /* To make sure nobody uses it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) if (fill_block.flags & INCFS_BLOCK_FLAGS_HASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) error = incfs_process_new_hash_block(df, &fill_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) data_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) error = incfs_process_new_data_block(df, &fill_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) data_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (data_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) free_pages((unsigned long)data_buf, get_order(data_buf_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) maybe_delete_incomplete_file(f, df);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * Only report the error if no records were processed, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * just return how many were processed successfully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) static long ioctl_read_file_signature(struct file *f, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) struct incfs_get_file_sig_args __user *args_usr_ptr = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct incfs_get_file_sig_args args = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) u8 *sig_buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) size_t sig_buf_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) int read_result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) struct data_file *df = get_incfs_data_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (!df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) sig_buf_size = args.file_signature_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (sig_buf_size > INCFS_MAX_SIGNATURE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) sig_buffer = kzalloc(sig_buf_size, GFP_NOFS | __GFP_COMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (!sig_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) read_result = incfs_read_file_signature(df,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) range(sig_buffer, sig_buf_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (read_result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) error = read_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (copy_to_user(u64_to_user_ptr(args.file_signature), sig_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) read_result)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) args.file_signature_len_out = read_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) kfree(sig_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) static long ioctl_get_filled_blocks(struct file *f, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) struct incfs_get_filled_blocks_args __user *args_usr_ptr = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) struct incfs_get_filled_blocks_args args = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) struct data_file *df = get_incfs_data_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct incfs_file_data *fd = f->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (!df || !fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (fd->fd_fill_permission != CAN_FILL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) error = incfs_get_filled_blocks(df, fd, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) static long ioctl_get_block_count(struct file *f, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) struct incfs_get_block_count_args __user *args_usr_ptr = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) struct incfs_get_block_count_args args = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) struct data_file *df = get_incfs_data_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (!df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) args.total_data_blocks_out = df->df_data_block_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) args.filled_data_blocks_out = atomic_read(&df->df_data_blocks_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) args.total_hash_blocks_out = df->df_total_block_count -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) df->df_data_block_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) args.filled_hash_blocks_out = atomic_read(&df->df_hash_blocks_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) static int incfs_ioctl_get_flags(struct file *f, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) u32 flags = IS_VERITY(file_inode(f)) ? FS_VERITY_FL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) return put_user(flags, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) switch (req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) case INCFS_IOC_FILL_BLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) return ioctl_fill_blocks(f, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) case INCFS_IOC_READ_FILE_SIGNATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) return ioctl_read_file_signature(f, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) case INCFS_IOC_GET_FILLED_BLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) return ioctl_get_filled_blocks(f, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) case INCFS_IOC_GET_BLOCK_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) return ioctl_get_block_count(f, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) case FS_IOC_ENABLE_VERITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return incfs_ioctl_enable_verity(f, (const void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) case FS_IOC_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return incfs_ioctl_get_flags(f, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) case FS_IOC_MEASURE_VERITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return incfs_ioctl_measure_verity(f, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) case FS_IOC_READ_VERITY_METADATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return incfs_ioctl_read_verity_metadata(f, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) case FS_IOC32_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) cmd = FS_IOC_GETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) case INCFS_IOC_FILL_BLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) case INCFS_IOC_READ_FILE_SIGNATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) case INCFS_IOC_GET_FILLED_BLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) case INCFS_IOC_GET_BLOCK_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) case FS_IOC_ENABLE_VERITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) case FS_IOC_MEASURE_VERITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) case FS_IOC_READ_VERITY_METADATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) return dispatch_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) static struct dentry *dir_lookup(struct inode *dir_inode, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) struct mount_info *mi = get_mount_info(dir_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) struct dentry *dir_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) struct dentry *backing_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct path dir_backing_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) struct inode_info *dir_info = get_incfs_node(dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (!mi || !dir_info || !dir_info->n_backing_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return ERR_PTR(-EBADF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (d_inode(mi->mi_backing_dir_path.dentry) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) dir_info->n_backing_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) /* We do lookup in the FS root. Show pseudo files. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) err = dir_lookup_pseudo_files(dir_inode->i_sb, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) dir_dentry = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) get_incfs_backing_path(dir_dentry, &dir_backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) backing_dentry = incfs_lookup_dentry(dir_backing_path.dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) dentry->d_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (!backing_dentry || IS_ERR(backing_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) err = IS_ERR(backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) ? PTR_ERR(backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) : -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) backing_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) struct path backing_path = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) .mnt = dir_backing_path.mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) .dentry = backing_dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) err = incfs_init_dentry(dentry, &backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (!d_really_is_positive(backing_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * No such entry found in the backing dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * Create a negative entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) d_add(dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (d_inode(backing_dentry)->i_sb !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) dir_info->n_backing_inode->i_sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * Somehow after the path lookup we ended up in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * different fs mount. If we keep going it's going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) * to end badly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) err = -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) inode = fetch_regular_inode(dir_inode->i_sb, backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) d_add(dentry, inode);
^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) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) dput(dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) dput(backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) path_put(&dir_backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) pr_debug("incfs: %s %s %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) dentry->d_name.name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) static int dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) struct mount_info *mi = get_mount_info(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct inode_info *dir_node = get_incfs_node(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) struct dentry *backing_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) struct path backing_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (!mi || !dir_node || !dir_node->n_backing_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) get_incfs_backing_path(dentry, &backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) backing_dentry = backing_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) if (!backing_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) err = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) goto path_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (backing_dentry->d_parent == mi->mi_index_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) /* Can't create a subdir inside .index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (backing_dentry->d_parent == mi->mi_incomplete_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) /* Can't create a subdir inside .incomplete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) inode_lock_nested(dir_node->n_backing_inode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) err = vfs_mkdir(dir_node->n_backing_inode, backing_dentry, mode | 0222);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) inode_unlock(dir_node->n_backing_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) if (d_really_is_negative(backing_dentry) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) unlikely(d_unhashed(backing_dentry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) inode = fetch_regular_inode(dir->i_sb, backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (d_really_is_negative(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) path_put(&backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) path_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) mutex_unlock(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) pr_debug("incfs: %s err:%d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * Delete file referenced by backing_dentry and if appropriate its hardlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * from .index and .incomplete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) static int file_delete(struct mount_info *mi, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) struct dentry *backing_dentry, int nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct dentry *index_file_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) struct dentry *incomplete_file_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /* 2 chars per byte of file ID + 1 char for \0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) char file_id_str[2 * sizeof(incfs_uuid_t) + 1] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) ssize_t uuid_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) WARN_ON(!mutex_is_locked(&mi->mi_dir_struct_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) if (nlink > 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) goto just_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) uuid_size = vfs_getxattr(backing_dentry, INCFS_XATTR_ID_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) file_id_str, 2 * sizeof(incfs_uuid_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) if (uuid_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) error = uuid_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (uuid_size != 2 * sizeof(incfs_uuid_t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) error = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (IS_ERR(index_file_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) error = PTR_ERR(index_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) index_file_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) if (d_really_is_positive(index_file_dentry) && nlink > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) nlink--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) if (nlink > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) goto just_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) file_id_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (IS_ERR(incomplete_file_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) error = PTR_ERR(incomplete_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) incomplete_file_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (d_really_is_positive(incomplete_file_dentry) && nlink > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) nlink--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) if (nlink > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) goto just_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (d_really_is_positive(index_file_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) error = incfs_unlink(index_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) notify_unlink(dentry, file_id_str, INCFS_INDEX_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) if (d_really_is_positive(incomplete_file_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) error = incfs_unlink(incomplete_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) notify_unlink(dentry, file_id_str, INCFS_INCOMPLETE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) just_unlink:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) error = incfs_unlink(backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) dput(index_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) dput(incomplete_file_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) pr_debug("incfs: delete_file_from_index err:%d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) static int dir_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) struct mount_info *mi = get_mount_info(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) struct path backing_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (!mi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) get_incfs_backing_path(dentry, &backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (!backing_path.dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) err = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) goto path_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (backing_path.dentry->d_parent == mi->mi_index_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) /* Direct unlink from .index are not allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (backing_path.dentry->d_parent == mi->mi_incomplete_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) /* Direct unlink from .incomplete are not allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) err = vfs_getattr(&backing_path, &stat, STATX_NLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) err = file_delete(mi, dentry, backing_path.dentry, stat.nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) path_put(&backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) path_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) pr_debug("incfs: %s err:%d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) mutex_unlock(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) static int dir_link(struct dentry *old_dentry, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) struct mount_info *mi = get_mount_info(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) struct path backing_old_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) struct path backing_new_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) if (!mi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) get_incfs_backing_path(old_dentry, &backing_old_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) get_incfs_backing_path(new_dentry, &backing_new_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (backing_new_path.dentry->d_parent == mi->mi_index_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) /* Can't link to .index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) if (backing_new_path.dentry->d_parent == mi->mi_incomplete_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) /* Can't link to .incomplete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) error = incfs_link(backing_old_path.dentry, backing_new_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) struct dentry *bdentry = backing_new_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (d_really_is_negative(bdentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) inode = fetch_regular_inode(dir->i_sb, bdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) error = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) d_instantiate(new_dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) path_put(&backing_old_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) path_put(&backing_new_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) pr_debug("incfs: %s err:%d\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) mutex_unlock(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static int dir_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) struct mount_info *mi = get_mount_info(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) struct path backing_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (!mi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) get_incfs_backing_path(dentry, &backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (!backing_path.dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) err = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) goto path_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) if (backing_path.dentry == mi->mi_index_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) /* Can't delete .index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) goto out;
^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) if (backing_path.dentry == mi->mi_incomplete_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) /* Can't delete .incomplete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) err = incfs_rmdir(backing_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) path_put(&backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) path_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) pr_debug("incfs: %s err:%d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) mutex_unlock(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) return err;
^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 dir_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) struct inode *new_dir, struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) struct mount_info *mi = get_mount_info(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct dentry *backing_old_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct dentry *backing_new_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) struct dentry *backing_old_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) struct dentry *backing_new_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct inode *target_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) struct dentry *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) backing_old_dentry = get_incfs_dentry(old_dentry)->backing_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) if (!backing_old_dentry || backing_old_dentry == mi->mi_index_dir ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) backing_old_dentry == mi->mi_incomplete_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) /* Renaming .index or .incomplete not allowed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) backing_new_dentry = get_incfs_dentry(new_dentry)->backing_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) dget(backing_old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) dget(backing_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) backing_old_dir_dentry = dget_parent(backing_old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) backing_new_dir_dentry = dget_parent(backing_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) target_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (backing_old_dir_dentry == mi->mi_index_dir ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) backing_old_dir_dentry == mi->mi_incomplete_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) /* Direct moves from .index or .incomplete are not allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) trap = lock_rename(backing_old_dir_dentry, backing_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) if (trap == backing_old_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) if (trap == backing_new_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) error = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) error = vfs_rename(d_inode(backing_old_dir_dentry), backing_old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) d_inode(backing_new_dir_dentry), backing_new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) if (target_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) fsstack_copy_attr_all(target_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) get_incfs_node(target_inode)->n_backing_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) fsstack_copy_attr_all(new_dir, d_inode(backing_new_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) if (new_dir != old_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) fsstack_copy_attr_all(old_dir, d_inode(backing_old_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) unlock_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) unlock_rename(backing_old_dir_dentry, backing_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) dput(backing_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) dput(backing_old_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) dput(backing_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) dput(backing_old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) mutex_unlock(&mi->mi_dir_struct_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) pr_debug("incfs: %s err:%d\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) static int file_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) struct mount_info *mi = get_mount_info(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) struct file *backing_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) struct path backing_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) int flags = O_NOATIME | O_LARGEFILE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) (S_ISDIR(inode->i_mode) ? O_RDONLY : O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) WARN_ON(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (!mi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) get_incfs_backing_path(file->f_path.dentry, &backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) if (!backing_path.dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) old_cred = override_creds(mi->mi_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) backing_file = dentry_open(&backing_path, flags, current_cred());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) path_put(&backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) if (IS_ERR(backing_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) err = PTR_ERR(backing_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) backing_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) struct incfs_file_data *fd = kzalloc(sizeof(*fd), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) if (!fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) goto out;
^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) *fd = (struct incfs_file_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) .fd_fill_permission = CANT_FILL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) file->private_data = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) err = make_inode_ready_for_data_ops(mi, inode, backing_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) err = incfs_fsverity_file_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) } else if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) struct dir_file *dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) dir = incfs_open_dir_file(mi, backing_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (IS_ERR(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) err = PTR_ERR(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) file->private_data = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) err = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) pr_debug("name:%s err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) file->f_path.dentry->d_name.name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) else if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) incfs_free_dir_file(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (backing_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) fput(backing_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static int file_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) } else if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) struct dir_file *dir = get_incfs_dir_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) incfs_free_dir_file(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) static int dentry_revalidate(struct dentry *d, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) struct path backing_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) struct inode_info *info = get_incfs_node(d_inode(d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) struct inode *binode = (info == NULL) ? NULL : info->n_backing_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) struct dentry *backing_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) if (flags & LOOKUP_RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) get_incfs_backing_path(d, &backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) backing_dentry = backing_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) if (!backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (d_inode(backing_dentry) != binode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) * Backing inodes obtained via dentry and inode don't match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) * It indicates that most likely backing dir has changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) * directly bypassing Incremental FS interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) if (backing_dentry->d_flags & DCACHE_OP_REVALIDATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) result = backing_dentry->d_op->d_revalidate(backing_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) result = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) path_put(&backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) static void dentry_release(struct dentry *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) struct dentry_info *di = get_incfs_dentry(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if (di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) path_put(&di->backing_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) kfree(d->d_fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) d->d_fsdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) static struct inode *alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) struct inode_info *node = kzalloc(sizeof(*node), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) /* TODO: add a slab-based cache here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) inode_init_once(&node->n_vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) return &node->n_vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) static void free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) struct inode_info *node = get_incfs_node(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) kfree(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) static void evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) struct inode_info *node = get_incfs_node(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (node->n_backing_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) iput(node->n_backing_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) node->n_backing_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) if (node->n_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) incfs_free_data_file(node->n_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) node->n_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) truncate_inode_pages(&inode->i_data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) static int incfs_setattr(struct dentry *dentry, struct iattr *ia)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) struct dentry_info *di = get_incfs_dentry(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) struct dentry *backing_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) struct inode *backing_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) if (ia->ia_valid & ATTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) if (!di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) backing_dentry = di->backing_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) if (!backing_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) backing_inode = d_inode(backing_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) /* incfs files are readonly, but the backing files must be writeable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) if (S_ISREG(backing_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) if ((ia->ia_valid & ATTR_MODE) && (ia->ia_mode & 0222))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) ia->ia_mode |= 0222;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) inode_lock(d_inode(backing_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) error = notify_change(backing_dentry, ia, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) inode_unlock(d_inode(backing_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) if (S_ISREG(backing_inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) ia->ia_mode &= ~0222;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) return simple_setattr(dentry, ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) static int incfs_getattr(const struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) struct kstat *stat, u32 request_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) unsigned int query_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) generic_fillattr(inode, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (inode->i_ino < INCFS_START_INO_RANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) stat->attributes &= ~STATX_ATTR_VERITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) if (IS_VERITY(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) stat->attributes |= STATX_ATTR_VERITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) stat->attributes_mask |= STATX_ATTR_VERITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) if (request_mask & STATX_BLOCKS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) struct kstat backing_kstat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) struct dentry_info *di = get_incfs_dentry(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) struct path *backing_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) if (!di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) backing_path = &di->backing_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) error = vfs_getattr(backing_path, &backing_kstat, STATX_BLOCKS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) stat->blocks = backing_kstat.blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) static ssize_t incfs_getxattr(struct dentry *d, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) void *value, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct dentry_info *di = get_incfs_dentry(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) struct mount_info *mi = get_mount_info(d->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) char *stored_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) size_t stored_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (di && di->backing_path.dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return vfs_getxattr(di->backing_path.dentry, name, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) if (strcmp(name, "security.selinux"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (i == PSEUDO_FILE_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) stored_value = mi->pseudo_file_xattr[i].data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) stored_size = mi->pseudo_file_xattr[i].len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) if (!stored_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (stored_size > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) memcpy(value, stored_value, stored_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) return stored_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) static ssize_t incfs_setxattr(struct dentry *d, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) const void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) struct dentry_info *di = get_incfs_dentry(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) struct mount_info *mi = get_mount_info(d->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) u8 **stored_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) size_t *stored_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) if (di && di->backing_path.dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) return vfs_setxattr(di->backing_path.dentry, name, value, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) if (strcmp(name, "security.selinux"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) if (size > INCFS_MAX_FILE_ATTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) if (i == PSEUDO_FILE_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) stored_value = &mi->pseudo_file_xattr[i].data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) stored_size = &mi->pseudo_file_xattr[i].len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) kfree (*stored_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) *stored_value = kzalloc(size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) if (!*stored_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) memcpy(*stored_value, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) *stored_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static ssize_t incfs_listxattr(struct dentry *d, char *list, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) struct dentry_info *di = get_incfs_dentry(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (!di || !di->backing_path.dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) return vfs_listxattr(di->backing_path.dentry, list, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) struct dentry *incfs_mount_fs(struct file_system_type *type, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) const char *dev_name, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) struct mount_options options = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) struct mount_info *mi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) struct path backing_dir_path = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) struct dentry *index_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) struct dentry *incomplete_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) struct super_block *src_fs_sb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) struct inode *root_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) struct super_block *sb = sget(type, NULL, set_anon_super, flags, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) bool dir_created = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) if (IS_ERR(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) return ERR_CAST(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) sb->s_op = &incfs_super_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) sb->s_d_op = &incfs_dentry_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) sb->s_flags |= S_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) sb->s_magic = INCFS_MAGIC_NUMBER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) sb->s_time_gran = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) sb->s_blocksize = INCFS_DATA_FILE_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) sb->s_blocksize_bits = blksize_bits(sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) sb->s_xattr = incfs_xattr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) BUILD_BUG_ON(PAGE_SIZE != INCFS_DATA_FILE_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) if (!dev_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) goto err_deactivate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) error = parse_options(&options, (char *)data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) if (error != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) pr_err("incfs: Options parsing error. %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) goto err_deactivate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) sb->s_bdi->ra_pages = options.readahead_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) if (!dev_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) goto err_free_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) error = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) &backing_dir_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) if (error || backing_dir_path.dentry == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) !d_really_is_positive(backing_dir_path.dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) pr_err("incfs: Error accessing: %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) goto err_free_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) src_fs_sb = backing_dir_path.dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) sb->s_maxbytes = src_fs_sb->s_maxbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) sb->s_stack_depth = src_fs_sb->s_stack_depth + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) if (IS_ERR_OR_NULL(mi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) error = PTR_ERR(mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) pr_err("incfs: Error allocating mount info. %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) sb->s_fs_info = mi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) mi->mi_backing_dir_path = backing_dir_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) index_dir = open_or_create_special_dir(backing_dir_path.dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) INCFS_INDEX_NAME, &dir_created);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) if (IS_ERR_OR_NULL(index_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) error = PTR_ERR(index_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) pr_err("incfs: Can't find or create .index dir in %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) /* No need to null index_dir since we don't put it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) mi->mi_index_dir = index_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) mi->mi_index_free = dir_created;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) incomplete_dir = open_or_create_special_dir(backing_dir_path.dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) INCFS_INCOMPLETE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) &dir_created);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) if (IS_ERR_OR_NULL(incomplete_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) error = PTR_ERR(incomplete_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) pr_err("incfs: Can't find or create .incomplete dir in %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) /* No need to null incomplete_dir since we don't put it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) mi->mi_incomplete_dir = incomplete_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) mi->mi_incomplete_free = dir_created;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) root_inode = fetch_regular_inode(sb, backing_dir_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) if (IS_ERR(root_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) error = PTR_ERR(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) sb->s_root = d_make_root(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) if (!sb->s_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) error = incfs_init_dentry(sb->s_root, &backing_dir_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) goto err_put_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) path_put(&backing_dir_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) sb->s_flags |= SB_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) pr_debug("incfs: mount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) return dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) err_put_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) path_put(&backing_dir_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) err_free_opts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) free_options(&options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) err_deactivate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) deactivate_locked_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) pr_err("incfs: mount failed %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) static int incfs_remount_fs(struct super_block *sb, int *flags, char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) struct mount_options options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) struct mount_info *mi = get_mount_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) sync_filesystem(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) err = parse_options(&options, (char *)data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) if (options.report_uid != mi->mi_options.report_uid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) pr_err("incfs: Can't change report_uid mount option on remount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) err = incfs_realloc_mount_info(mi, &options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) pr_debug("incfs: remount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) free_options(&options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) void incfs_kill_sb(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) struct mount_info *mi = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) struct inode *dinode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) pr_debug("incfs: unmount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) if (mi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) if (mi->mi_backing_dir_path.dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) dinode = d_inode(mi->mi_backing_dir_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) if (dinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) if (mi->mi_index_dir && mi->mi_index_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) vfs_rmdir(dinode, mi->mi_index_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (mi->mi_incomplete_dir && mi->mi_incomplete_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) vfs_rmdir(dinode, mi->mi_incomplete_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) incfs_free_mount_info(mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) sb->s_fs_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) kill_anon_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) static int show_options(struct seq_file *m, struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) struct mount_info *mi = get_mount_info(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) seq_printf(m, ",read_timeout_ms=%u", mi->mi_options.read_timeout_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) seq_printf(m, ",readahead=%u", mi->mi_options.readahead_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) if (mi->mi_options.read_log_pages != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) seq_printf(m, ",rlog_pages=%u", mi->mi_options.read_log_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) seq_printf(m, ",rlog_wakeup_cnt=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) mi->mi_options.read_log_wakeup_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) if (mi->mi_options.report_uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) seq_puts(m, ",report_uid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) if (mi->mi_sysfs_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) seq_printf(m, ",sysfs_name=%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) kobject_name(&mi->mi_sysfs_node->isn_sysfs_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) }