| .. | .. |
|---|
| 4 | 4 | */ |
|---|
| 5 | 5 | |
|---|
| 6 | 6 | #include <linux/blkdev.h> |
|---|
| 7 | | -#include <linux/cred.h> |
|---|
| 8 | | -#include <linux/eventpoll.h> |
|---|
| 7 | +#include <linux/compat.h> |
|---|
| 8 | +#include <linux/delay.h> |
|---|
| 9 | 9 | #include <linux/file.h> |
|---|
| 10 | 10 | #include <linux/fs.h> |
|---|
| 11 | 11 | #include <linux/fs_stack.h> |
|---|
| 12 | +#include <linux/fsnotify.h> |
|---|
| 13 | +#include <linux/fsverity.h> |
|---|
| 14 | +#include <linux/mmap_lock.h> |
|---|
| 12 | 15 | #include <linux/namei.h> |
|---|
| 13 | 16 | #include <linux/parser.h> |
|---|
| 14 | | -#include <linux/poll.h> |
|---|
| 15 | 17 | #include <linux/seq_file.h> |
|---|
| 16 | | -#include <linux/syscalls.h> |
|---|
| 17 | | -#include <linux/xattr.h> |
|---|
| 18 | 18 | |
|---|
| 19 | 19 | #include <uapi/linux/incrementalfs.h> |
|---|
| 20 | 20 | |
|---|
| 21 | 21 | #include "vfs.h" |
|---|
| 22 | + |
|---|
| 22 | 23 | #include "data_mgmt.h" |
|---|
| 23 | 24 | #include "format.h" |
|---|
| 24 | | -#include "integrity.h" |
|---|
| 25 | 25 | #include "internal.h" |
|---|
| 26 | | - |
|---|
| 27 | | -#define INCFS_PENDING_READS_INODE 2 |
|---|
| 28 | | -#define INCFS_LOG_INODE 3 |
|---|
| 29 | | -#define INCFS_START_INO_RANGE 10 |
|---|
| 30 | | -#define READ_FILE_MODE 0444 |
|---|
| 31 | | -#define READ_EXEC_FILE_MODE 0555 |
|---|
| 32 | | -#define READ_WRITE_FILE_MODE 0666 |
|---|
| 26 | +#include "pseudo_files.h" |
|---|
| 27 | +#include "sysfs.h" |
|---|
| 28 | +#include "verity.h" |
|---|
| 33 | 29 | |
|---|
| 34 | 30 | static int incfs_remount_fs(struct super_block *sb, int *flags, char *data); |
|---|
| 35 | 31 | |
|---|
| .. | .. |
|---|
| 52 | 48 | static int read_single_page(struct file *f, struct page *page); |
|---|
| 53 | 49 | static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg); |
|---|
| 54 | 50 | |
|---|
| 55 | | -static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len, |
|---|
| 56 | | - loff_t *ppos); |
|---|
| 57 | | -static __poll_t pending_reads_poll(struct file *file, poll_table *wait); |
|---|
| 58 | | -static int pending_reads_open(struct inode *inode, struct file *file); |
|---|
| 59 | | -static int pending_reads_release(struct inode *, struct file *); |
|---|
| 60 | | - |
|---|
| 61 | | -static ssize_t log_read(struct file *f, char __user *buf, size_t len, |
|---|
| 62 | | - loff_t *ppos); |
|---|
| 63 | | -static __poll_t log_poll(struct file *file, poll_table *wait); |
|---|
| 64 | | -static int log_open(struct inode *inode, struct file *file); |
|---|
| 65 | | -static int log_release(struct inode *, struct file *); |
|---|
| 51 | +#ifdef CONFIG_COMPAT |
|---|
| 52 | +static long incfs_compat_ioctl(struct file *file, unsigned int cmd, |
|---|
| 53 | + unsigned long arg); |
|---|
| 54 | +#endif |
|---|
| 66 | 55 | |
|---|
| 67 | 56 | static struct inode *alloc_inode(struct super_block *sb); |
|---|
| 68 | 57 | static void free_inode(struct inode *inode); |
|---|
| 69 | 58 | static void evict_inode(struct inode *inode); |
|---|
| 70 | 59 | |
|---|
| 71 | 60 | static int incfs_setattr(struct dentry *dentry, struct iattr *ia); |
|---|
| 61 | +static int incfs_getattr(const struct path *path, |
|---|
| 62 | + struct kstat *stat, u32 request_mask, |
|---|
| 63 | + unsigned int query_flags); |
|---|
| 72 | 64 | static ssize_t incfs_getxattr(struct dentry *d, const char *name, |
|---|
| 73 | 65 | void *value, size_t size); |
|---|
| 74 | 66 | static ssize_t incfs_setxattr(struct dentry *d, const char *name, |
|---|
| .. | .. |
|---|
| 109 | 101 | .iterate = iterate_incfs_dir, |
|---|
| 110 | 102 | .open = file_open, |
|---|
| 111 | 103 | .release = file_release, |
|---|
| 112 | | - .unlocked_ioctl = dispatch_ioctl, |
|---|
| 113 | | - .compat_ioctl = dispatch_ioctl |
|---|
| 114 | 104 | }; |
|---|
| 115 | 105 | |
|---|
| 116 | 106 | static const struct dentry_operations incfs_dentry_ops = { |
|---|
| .. | .. |
|---|
| 123 | 113 | /* .readpages = readpages */ |
|---|
| 124 | 114 | }; |
|---|
| 125 | 115 | |
|---|
| 126 | | -static const struct file_operations incfs_file_ops = { |
|---|
| 116 | +static vm_fault_t incfs_fault(struct vm_fault *vmf) |
|---|
| 117 | +{ |
|---|
| 118 | + vmf->flags &= ~FAULT_FLAG_ALLOW_RETRY; |
|---|
| 119 | + return filemap_fault(vmf); |
|---|
| 120 | +} |
|---|
| 121 | + |
|---|
| 122 | +static const struct vm_operations_struct incfs_file_vm_ops = { |
|---|
| 123 | + .fault = incfs_fault, |
|---|
| 124 | + .map_pages = filemap_map_pages, |
|---|
| 125 | + .page_mkwrite = filemap_page_mkwrite, |
|---|
| 126 | +}; |
|---|
| 127 | + |
|---|
| 128 | +/* This is used for a general mmap of a disk file */ |
|---|
| 129 | + |
|---|
| 130 | +static int incfs_file_mmap(struct file *file, struct vm_area_struct *vma) |
|---|
| 131 | +{ |
|---|
| 132 | + struct address_space *mapping = file->f_mapping; |
|---|
| 133 | + |
|---|
| 134 | + if (!mapping->a_ops->readpage) |
|---|
| 135 | + return -ENOEXEC; |
|---|
| 136 | + file_accessed(file); |
|---|
| 137 | + vma->vm_ops = &incfs_file_vm_ops; |
|---|
| 138 | + return 0; |
|---|
| 139 | +} |
|---|
| 140 | + |
|---|
| 141 | +const struct file_operations incfs_file_ops = { |
|---|
| 127 | 142 | .open = file_open, |
|---|
| 128 | 143 | .release = file_release, |
|---|
| 129 | 144 | .read_iter = generic_file_read_iter, |
|---|
| 130 | | - .mmap = generic_file_mmap, |
|---|
| 145 | + .mmap = incfs_file_mmap, |
|---|
| 131 | 146 | .splice_read = generic_file_splice_read, |
|---|
| 132 | 147 | .llseek = generic_file_llseek, |
|---|
| 133 | 148 | .unlocked_ioctl = dispatch_ioctl, |
|---|
| 134 | | - .compat_ioctl = dispatch_ioctl |
|---|
| 149 | +#ifdef CONFIG_COMPAT |
|---|
| 150 | + .compat_ioctl = incfs_compat_ioctl, |
|---|
| 151 | +#endif |
|---|
| 135 | 152 | }; |
|---|
| 136 | 153 | |
|---|
| 137 | | -enum FILL_PERMISSION { |
|---|
| 138 | | - CANT_FILL = 0, |
|---|
| 139 | | - CAN_FILL = 1, |
|---|
| 140 | | -}; |
|---|
| 141 | | - |
|---|
| 142 | | -static const struct file_operations incfs_pending_read_file_ops = { |
|---|
| 143 | | - .read = pending_reads_read, |
|---|
| 144 | | - .poll = pending_reads_poll, |
|---|
| 145 | | - .open = pending_reads_open, |
|---|
| 146 | | - .release = pending_reads_release, |
|---|
| 147 | | - .llseek = noop_llseek, |
|---|
| 148 | | - .unlocked_ioctl = dispatch_ioctl, |
|---|
| 149 | | - .compat_ioctl = dispatch_ioctl |
|---|
| 150 | | -}; |
|---|
| 151 | | - |
|---|
| 152 | | -static const struct file_operations incfs_log_file_ops = { |
|---|
| 153 | | - .read = log_read, |
|---|
| 154 | | - .poll = log_poll, |
|---|
| 155 | | - .open = log_open, |
|---|
| 156 | | - .release = log_release, |
|---|
| 157 | | - .llseek = noop_llseek, |
|---|
| 158 | | - .unlocked_ioctl = dispatch_ioctl, |
|---|
| 159 | | - .compat_ioctl = dispatch_ioctl |
|---|
| 160 | | -}; |
|---|
| 161 | | - |
|---|
| 162 | | -static const struct inode_operations incfs_file_inode_ops = { |
|---|
| 154 | +const struct inode_operations incfs_file_inode_ops = { |
|---|
| 163 | 155 | .setattr = incfs_setattr, |
|---|
| 164 | | - .getattr = simple_getattr, |
|---|
| 156 | + .getattr = incfs_getattr, |
|---|
| 165 | 157 | .listxattr = incfs_listxattr |
|---|
| 166 | 158 | }; |
|---|
| 167 | 159 | |
|---|
| 168 | 160 | static int incfs_handler_getxattr(const struct xattr_handler *xh, |
|---|
| 169 | 161 | struct dentry *d, struct inode *inode, |
|---|
| 170 | | - const char *name, void *buffer, size_t size) |
|---|
| 162 | + const char *name, void *buffer, size_t size, |
|---|
| 163 | + int flags) |
|---|
| 171 | 164 | { |
|---|
| 172 | 165 | return incfs_getxattr(d, name, buffer, size); |
|---|
| 173 | 166 | } |
|---|
| .. | .. |
|---|
| 191 | 184 | NULL, |
|---|
| 192 | 185 | }; |
|---|
| 193 | 186 | |
|---|
| 194 | | -/* State of an open .pending_reads file, unique for each file descriptor. */ |
|---|
| 195 | | -struct pending_reads_state { |
|---|
| 196 | | - /* A serial number of the last pending read obtained from this file. */ |
|---|
| 197 | | - int last_pending_read_sn; |
|---|
| 198 | | -}; |
|---|
| 199 | | - |
|---|
| 200 | | -/* State of an open .log file, unique for each file descriptor. */ |
|---|
| 201 | | -struct log_file_state { |
|---|
| 202 | | - struct read_log_state state; |
|---|
| 203 | | -}; |
|---|
| 204 | | - |
|---|
| 205 | 187 | struct inode_search { |
|---|
| 206 | 188 | unsigned long ino; |
|---|
| 207 | 189 | |
|---|
| 208 | 190 | struct dentry *backing_dentry; |
|---|
| 209 | 191 | |
|---|
| 210 | 192 | size_t size; |
|---|
| 193 | + |
|---|
| 194 | + bool verity; |
|---|
| 211 | 195 | }; |
|---|
| 212 | 196 | |
|---|
| 213 | 197 | enum parse_parameter { |
|---|
| 214 | 198 | Opt_read_timeout, |
|---|
| 215 | 199 | Opt_readahead_pages, |
|---|
| 216 | | - Opt_no_backing_file_cache, |
|---|
| 217 | | - Opt_no_backing_file_readahead, |
|---|
| 218 | 200 | Opt_rlog_pages, |
|---|
| 219 | 201 | Opt_rlog_wakeup_cnt, |
|---|
| 202 | + Opt_report_uid, |
|---|
| 203 | + Opt_sysfs_name, |
|---|
| 220 | 204 | Opt_err |
|---|
| 221 | | -}; |
|---|
| 222 | | - |
|---|
| 223 | | -static const char pending_reads_file_name[] = INCFS_PENDING_READS_FILENAME; |
|---|
| 224 | | -static struct mem_range pending_reads_file_name_range = { |
|---|
| 225 | | - .data = (u8 *)pending_reads_file_name, |
|---|
| 226 | | - .len = ARRAY_SIZE(pending_reads_file_name) - 1 |
|---|
| 227 | | -}; |
|---|
| 228 | | - |
|---|
| 229 | | -static const char log_file_name[] = INCFS_LOG_FILENAME; |
|---|
| 230 | | -static struct mem_range log_file_name_range = { |
|---|
| 231 | | - .data = (u8 *)log_file_name, |
|---|
| 232 | | - .len = ARRAY_SIZE(log_file_name) - 1 |
|---|
| 233 | 205 | }; |
|---|
| 234 | 206 | |
|---|
| 235 | 207 | static const match_table_t option_tokens = { |
|---|
| 236 | 208 | { Opt_read_timeout, "read_timeout_ms=%u" }, |
|---|
| 237 | 209 | { Opt_readahead_pages, "readahead=%u" }, |
|---|
| 238 | | - { Opt_no_backing_file_cache, "no_bf_cache=%u" }, |
|---|
| 239 | | - { Opt_no_backing_file_readahead, "no_bf_readahead=%u" }, |
|---|
| 240 | 210 | { Opt_rlog_pages, "rlog_pages=%u" }, |
|---|
| 241 | 211 | { Opt_rlog_wakeup_cnt, "rlog_wakeup_cnt=%u" }, |
|---|
| 212 | + { Opt_report_uid, "report_uid" }, |
|---|
| 213 | + { Opt_sysfs_name, "sysfs_name=%s" }, |
|---|
| 242 | 214 | { Opt_err, NULL } |
|---|
| 243 | 215 | }; |
|---|
| 216 | + |
|---|
| 217 | +static void free_options(struct mount_options *opts) |
|---|
| 218 | +{ |
|---|
| 219 | + kfree(opts->sysfs_name); |
|---|
| 220 | + opts->sysfs_name = NULL; |
|---|
| 221 | +} |
|---|
| 244 | 222 | |
|---|
| 245 | 223 | static int parse_options(struct mount_options *opts, char *str) |
|---|
| 246 | 224 | { |
|---|
| .. | .. |
|---|
| 251 | 229 | if (opts == NULL) |
|---|
| 252 | 230 | return -EFAULT; |
|---|
| 253 | 231 | |
|---|
| 254 | | - opts->read_timeout_ms = 1000; /* Default: 1s */ |
|---|
| 255 | | - opts->readahead_pages = 10; |
|---|
| 256 | | - opts->read_log_pages = 2; |
|---|
| 257 | | - opts->read_log_wakeup_count = 10; |
|---|
| 258 | | - opts->no_backing_file_cache = false; |
|---|
| 259 | | - opts->no_backing_file_readahead = false; |
|---|
| 232 | + *opts = (struct mount_options) { |
|---|
| 233 | + .read_timeout_ms = 1000, /* Default: 1s */ |
|---|
| 234 | + .readahead_pages = 10, |
|---|
| 235 | + .read_log_pages = 2, |
|---|
| 236 | + .read_log_wakeup_count = 10, |
|---|
| 237 | + }; |
|---|
| 238 | + |
|---|
| 260 | 239 | if (str == NULL || *str == 0) |
|---|
| 261 | 240 | return 0; |
|---|
| 262 | 241 | |
|---|
| .. | .. |
|---|
| 272 | 251 | case Opt_read_timeout: |
|---|
| 273 | 252 | if (match_int(&args[0], &value)) |
|---|
| 274 | 253 | return -EINVAL; |
|---|
| 254 | + if (value > 3600000) |
|---|
| 255 | + return -EINVAL; |
|---|
| 275 | 256 | opts->read_timeout_ms = value; |
|---|
| 276 | 257 | break; |
|---|
| 277 | 258 | case Opt_readahead_pages: |
|---|
| 278 | 259 | if (match_int(&args[0], &value)) |
|---|
| 279 | 260 | return -EINVAL; |
|---|
| 280 | 261 | opts->readahead_pages = value; |
|---|
| 281 | | - break; |
|---|
| 282 | | - case Opt_no_backing_file_cache: |
|---|
| 283 | | - if (match_int(&args[0], &value)) |
|---|
| 284 | | - return -EINVAL; |
|---|
| 285 | | - opts->no_backing_file_cache = (value != 0); |
|---|
| 286 | | - break; |
|---|
| 287 | | - case Opt_no_backing_file_readahead: |
|---|
| 288 | | - if (match_int(&args[0], &value)) |
|---|
| 289 | | - return -EINVAL; |
|---|
| 290 | | - opts->no_backing_file_readahead = (value != 0); |
|---|
| 291 | 262 | break; |
|---|
| 292 | 263 | case Opt_rlog_pages: |
|---|
| 293 | 264 | if (match_int(&args[0], &value)) |
|---|
| .. | .. |
|---|
| 299 | 270 | return -EINVAL; |
|---|
| 300 | 271 | opts->read_log_wakeup_count = value; |
|---|
| 301 | 272 | break; |
|---|
| 273 | + case Opt_report_uid: |
|---|
| 274 | + opts->report_uid = true; |
|---|
| 275 | + break; |
|---|
| 276 | + case Opt_sysfs_name: |
|---|
| 277 | + opts->sysfs_name = match_strdup(&args[0]); |
|---|
| 278 | + break; |
|---|
| 302 | 279 | default: |
|---|
| 280 | + free_options(opts); |
|---|
| 303 | 281 | return -EINVAL; |
|---|
| 304 | 282 | } |
|---|
| 305 | 283 | } |
|---|
| 306 | 284 | |
|---|
| 307 | 285 | return 0; |
|---|
| 308 | | -} |
|---|
| 309 | | - |
|---|
| 310 | | -static struct super_block *file_superblock(struct file *f) |
|---|
| 311 | | -{ |
|---|
| 312 | | - struct inode *inode = file_inode(f); |
|---|
| 313 | | - |
|---|
| 314 | | - return inode->i_sb; |
|---|
| 315 | | -} |
|---|
| 316 | | - |
|---|
| 317 | | -static struct mount_info *get_mount_info(struct super_block *sb) |
|---|
| 318 | | -{ |
|---|
| 319 | | - struct mount_info *result = sb->s_fs_info; |
|---|
| 320 | | - |
|---|
| 321 | | - return result; |
|---|
| 322 | 286 | } |
|---|
| 323 | 287 | |
|---|
| 324 | 288 | /* Read file size from the attribute. Quicker than reading the header */ |
|---|
| .. | .. |
|---|
| 336 | 300 | return le64_to_cpu(attr_value); |
|---|
| 337 | 301 | } |
|---|
| 338 | 302 | |
|---|
| 303 | +/* Read verity flag from the attribute. Quicker than reading the header */ |
|---|
| 304 | +static bool read_verity_attr(struct dentry *backing_dentry) |
|---|
| 305 | +{ |
|---|
| 306 | + return vfs_getxattr(backing_dentry, INCFS_XATTR_VERITY_NAME, NULL, 0) |
|---|
| 307 | + >= 0; |
|---|
| 308 | +} |
|---|
| 309 | + |
|---|
| 339 | 310 | static int inode_test(struct inode *inode, void *opaque) |
|---|
| 340 | 311 | { |
|---|
| 341 | 312 | struct inode_search *search = opaque; |
|---|
| 342 | 313 | struct inode_info *node = get_incfs_node(inode); |
|---|
| 314 | + struct inode *backing_inode = d_inode(search->backing_dentry); |
|---|
| 343 | 315 | |
|---|
| 344 | 316 | if (!node) |
|---|
| 345 | 317 | return 0; |
|---|
| 346 | 318 | |
|---|
| 347 | | - if (search->backing_dentry) { |
|---|
| 348 | | - struct inode *backing_inode = d_inode(search->backing_dentry); |
|---|
| 349 | | - |
|---|
| 350 | | - return (node->n_backing_inode == backing_inode) && |
|---|
| 351 | | - inode->i_ino == search->ino; |
|---|
| 352 | | - } else |
|---|
| 353 | | - return inode->i_ino == search->ino; |
|---|
| 319 | + return node->n_backing_inode == backing_inode && |
|---|
| 320 | + inode->i_ino == search->ino; |
|---|
| 354 | 321 | } |
|---|
| 355 | 322 | |
|---|
| 356 | 323 | static int inode_set(struct inode *inode, void *opaque) |
|---|
| 357 | 324 | { |
|---|
| 358 | 325 | struct inode_search *search = opaque; |
|---|
| 359 | 326 | struct inode_info *node = get_incfs_node(inode); |
|---|
| 327 | + struct dentry *backing_dentry = search->backing_dentry; |
|---|
| 328 | + struct inode *backing_inode = d_inode(backing_dentry); |
|---|
| 360 | 329 | |
|---|
| 361 | | - if (search->backing_dentry) { |
|---|
| 362 | | - /* It's a regular inode that has corresponding backing inode */ |
|---|
| 363 | | - struct dentry *backing_dentry = search->backing_dentry; |
|---|
| 364 | | - struct inode *backing_inode = d_inode(backing_dentry); |
|---|
| 330 | + fsstack_copy_attr_all(inode, backing_inode); |
|---|
| 331 | + if (S_ISREG(inode->i_mode)) { |
|---|
| 332 | + u64 size = search->size; |
|---|
| 365 | 333 | |
|---|
| 366 | | - fsstack_copy_attr_all(inode, backing_inode); |
|---|
| 367 | | - if (S_ISREG(inode->i_mode)) { |
|---|
| 368 | | - u64 size = search->size; |
|---|
| 369 | | - |
|---|
| 370 | | - inode->i_size = size; |
|---|
| 371 | | - inode->i_blocks = get_blocks_count_for_size(size); |
|---|
| 372 | | - inode->i_mapping->a_ops = &incfs_address_space_ops; |
|---|
| 373 | | - inode->i_op = &incfs_file_inode_ops; |
|---|
| 374 | | - inode->i_fop = &incfs_file_ops; |
|---|
| 375 | | - inode->i_mode &= ~0222; |
|---|
| 376 | | - } else if (S_ISDIR(inode->i_mode)) { |
|---|
| 377 | | - inode->i_size = 0; |
|---|
| 378 | | - inode->i_blocks = 1; |
|---|
| 379 | | - inode->i_mapping->a_ops = &incfs_address_space_ops; |
|---|
| 380 | | - inode->i_op = &incfs_dir_inode_ops; |
|---|
| 381 | | - inode->i_fop = &incfs_dir_fops; |
|---|
| 382 | | - } else { |
|---|
| 383 | | - pr_warn_once("incfs: Unexpected inode type\n"); |
|---|
| 384 | | - return -EBADF; |
|---|
| 385 | | - } |
|---|
| 386 | | - |
|---|
| 387 | | - ihold(backing_inode); |
|---|
| 388 | | - node->n_backing_inode = backing_inode; |
|---|
| 389 | | - node->n_mount_info = get_mount_info(inode->i_sb); |
|---|
| 390 | | - inode->i_ctime = backing_inode->i_ctime; |
|---|
| 391 | | - inode->i_mtime = backing_inode->i_mtime; |
|---|
| 392 | | - inode->i_atime = backing_inode->i_atime; |
|---|
| 393 | | - inode->i_ino = backing_inode->i_ino; |
|---|
| 394 | | - if (backing_inode->i_ino < INCFS_START_INO_RANGE) { |
|---|
| 395 | | - pr_warn("incfs: ino conflict with backing FS %ld\n", |
|---|
| 396 | | - backing_inode->i_ino); |
|---|
| 397 | | - } |
|---|
| 398 | | - |
|---|
| 399 | | - return 0; |
|---|
| 400 | | - } else if (search->ino == INCFS_PENDING_READS_INODE) { |
|---|
| 401 | | - /* It's an inode for .pending_reads pseudo file. */ |
|---|
| 402 | | - |
|---|
| 403 | | - inode->i_ctime = (struct timespec64){}; |
|---|
| 404 | | - inode->i_mtime = inode->i_ctime; |
|---|
| 405 | | - inode->i_atime = inode->i_ctime; |
|---|
| 406 | | - inode->i_size = 0; |
|---|
| 407 | | - inode->i_ino = INCFS_PENDING_READS_INODE; |
|---|
| 408 | | - inode->i_private = NULL; |
|---|
| 409 | | - |
|---|
| 410 | | - inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE); |
|---|
| 411 | | - |
|---|
| 334 | + inode->i_size = size; |
|---|
| 335 | + inode->i_blocks = get_blocks_count_for_size(size); |
|---|
| 336 | + inode->i_mapping->a_ops = &incfs_address_space_ops; |
|---|
| 412 | 337 | inode->i_op = &incfs_file_inode_ops; |
|---|
| 413 | | - inode->i_fop = &incfs_pending_read_file_ops; |
|---|
| 414 | | - |
|---|
| 415 | | - } else if (search->ino == INCFS_LOG_INODE) { |
|---|
| 416 | | - /* It's an inode for .log pseudo file. */ |
|---|
| 417 | | - |
|---|
| 418 | | - inode->i_ctime = (struct timespec64){}; |
|---|
| 419 | | - inode->i_mtime = inode->i_ctime; |
|---|
| 420 | | - inode->i_atime = inode->i_ctime; |
|---|
| 338 | + inode->i_fop = &incfs_file_ops; |
|---|
| 339 | + inode->i_mode &= ~0222; |
|---|
| 340 | + if (search->verity) |
|---|
| 341 | + inode_set_flags(inode, S_VERITY, S_VERITY); |
|---|
| 342 | + } else if (S_ISDIR(inode->i_mode)) { |
|---|
| 421 | 343 | inode->i_size = 0; |
|---|
| 422 | | - inode->i_ino = INCFS_LOG_INODE; |
|---|
| 423 | | - inode->i_private = NULL; |
|---|
| 424 | | - |
|---|
| 425 | | - inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE); |
|---|
| 426 | | - |
|---|
| 427 | | - inode->i_op = &incfs_file_inode_ops; |
|---|
| 428 | | - inode->i_fop = &incfs_log_file_ops; |
|---|
| 429 | | - |
|---|
| 344 | + inode->i_blocks = 1; |
|---|
| 345 | + inode->i_mapping->a_ops = &incfs_address_space_ops; |
|---|
| 346 | + inode->i_op = &incfs_dir_inode_ops; |
|---|
| 347 | + inode->i_fop = &incfs_dir_fops; |
|---|
| 430 | 348 | } else { |
|---|
| 431 | | - /* Unknown inode requested. */ |
|---|
| 432 | | - return -EINVAL; |
|---|
| 349 | + pr_warn_once("incfs: Unexpected inode type\n"); |
|---|
| 350 | + return -EBADF; |
|---|
| 351 | + } |
|---|
| 352 | + |
|---|
| 353 | + ihold(backing_inode); |
|---|
| 354 | + node->n_backing_inode = backing_inode; |
|---|
| 355 | + node->n_mount_info = get_mount_info(inode->i_sb); |
|---|
| 356 | + inode->i_ctime = backing_inode->i_ctime; |
|---|
| 357 | + inode->i_mtime = backing_inode->i_mtime; |
|---|
| 358 | + inode->i_atime = backing_inode->i_atime; |
|---|
| 359 | + inode->i_ino = backing_inode->i_ino; |
|---|
| 360 | + if (backing_inode->i_ino < INCFS_START_INO_RANGE) { |
|---|
| 361 | + pr_warn("incfs: ino conflict with backing FS %ld\n", |
|---|
| 362 | + backing_inode->i_ino); |
|---|
| 433 | 363 | } |
|---|
| 434 | 364 | |
|---|
| 435 | 365 | return 0; |
|---|
| .. | .. |
|---|
| 443 | 373 | .ino = backing_inode->i_ino, |
|---|
| 444 | 374 | .backing_dentry = backing_dentry, |
|---|
| 445 | 375 | .size = read_size_attr(backing_dentry), |
|---|
| 446 | | - }; |
|---|
| 447 | | - struct inode *inode = iget5_locked(sb, search.ino, inode_test, |
|---|
| 448 | | - inode_set, &search); |
|---|
| 449 | | - |
|---|
| 450 | | - if (!inode) |
|---|
| 451 | | - return ERR_PTR(-ENOMEM); |
|---|
| 452 | | - |
|---|
| 453 | | - if (inode->i_state & I_NEW) |
|---|
| 454 | | - unlock_new_inode(inode); |
|---|
| 455 | | - |
|---|
| 456 | | - return inode; |
|---|
| 457 | | -} |
|---|
| 458 | | - |
|---|
| 459 | | -static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len, |
|---|
| 460 | | - loff_t *ppos) |
|---|
| 461 | | -{ |
|---|
| 462 | | - struct pending_reads_state *pr_state = f->private_data; |
|---|
| 463 | | - struct mount_info *mi = get_mount_info(file_superblock(f)); |
|---|
| 464 | | - struct incfs_pending_read_info *reads_buf = NULL; |
|---|
| 465 | | - size_t reads_to_collect = len / sizeof(*reads_buf); |
|---|
| 466 | | - int last_known_read_sn = READ_ONCE(pr_state->last_pending_read_sn); |
|---|
| 467 | | - int new_max_sn = last_known_read_sn; |
|---|
| 468 | | - int reads_collected = 0; |
|---|
| 469 | | - ssize_t result = 0; |
|---|
| 470 | | - int i = 0; |
|---|
| 471 | | - |
|---|
| 472 | | - if (!incfs_fresh_pending_reads_exist(mi, last_known_read_sn)) |
|---|
| 473 | | - return 0; |
|---|
| 474 | | - |
|---|
| 475 | | - reads_buf = (struct incfs_pending_read_info *)get_zeroed_page(GFP_NOFS); |
|---|
| 476 | | - if (!reads_buf) |
|---|
| 477 | | - return -ENOMEM; |
|---|
| 478 | | - |
|---|
| 479 | | - reads_to_collect = |
|---|
| 480 | | - min_t(size_t, PAGE_SIZE / sizeof(*reads_buf), reads_to_collect); |
|---|
| 481 | | - |
|---|
| 482 | | - reads_collected = incfs_collect_pending_reads( |
|---|
| 483 | | - mi, last_known_read_sn, reads_buf, reads_to_collect); |
|---|
| 484 | | - if (reads_collected < 0) { |
|---|
| 485 | | - result = reads_collected; |
|---|
| 486 | | - goto out; |
|---|
| 487 | | - } |
|---|
| 488 | | - |
|---|
| 489 | | - for (i = 0; i < reads_collected; i++) |
|---|
| 490 | | - if (reads_buf[i].serial_number > new_max_sn) |
|---|
| 491 | | - new_max_sn = reads_buf[i].serial_number; |
|---|
| 492 | | - |
|---|
| 493 | | - /* |
|---|
| 494 | | - * Just to make sure that we don't accidentally copy more data |
|---|
| 495 | | - * to reads buffer than userspace can handle. |
|---|
| 496 | | - */ |
|---|
| 497 | | - reads_collected = min_t(size_t, reads_collected, reads_to_collect); |
|---|
| 498 | | - result = reads_collected * sizeof(*reads_buf); |
|---|
| 499 | | - |
|---|
| 500 | | - /* Copy reads info to the userspace buffer */ |
|---|
| 501 | | - if (copy_to_user(buf, reads_buf, result)) { |
|---|
| 502 | | - result = -EFAULT; |
|---|
| 503 | | - goto out; |
|---|
| 504 | | - } |
|---|
| 505 | | - |
|---|
| 506 | | - WRITE_ONCE(pr_state->last_pending_read_sn, new_max_sn); |
|---|
| 507 | | - *ppos = 0; |
|---|
| 508 | | -out: |
|---|
| 509 | | - if (reads_buf) |
|---|
| 510 | | - free_page((unsigned long)reads_buf); |
|---|
| 511 | | - return result; |
|---|
| 512 | | -} |
|---|
| 513 | | - |
|---|
| 514 | | - |
|---|
| 515 | | -static __poll_t pending_reads_poll(struct file *file, poll_table *wait) |
|---|
| 516 | | -{ |
|---|
| 517 | | - struct pending_reads_state *state = file->private_data; |
|---|
| 518 | | - struct mount_info *mi = get_mount_info(file_superblock(file)); |
|---|
| 519 | | - __poll_t ret = 0; |
|---|
| 520 | | - |
|---|
| 521 | | - poll_wait(file, &mi->mi_pending_reads_notif_wq, wait); |
|---|
| 522 | | - if (incfs_fresh_pending_reads_exist(mi, |
|---|
| 523 | | - state->last_pending_read_sn)) |
|---|
| 524 | | - ret = EPOLLIN | EPOLLRDNORM; |
|---|
| 525 | | - |
|---|
| 526 | | - return ret; |
|---|
| 527 | | -} |
|---|
| 528 | | - |
|---|
| 529 | | -static int pending_reads_open(struct inode *inode, struct file *file) |
|---|
| 530 | | -{ |
|---|
| 531 | | - struct pending_reads_state *state = NULL; |
|---|
| 532 | | - |
|---|
| 533 | | - state = kzalloc(sizeof(*state), GFP_NOFS); |
|---|
| 534 | | - if (!state) |
|---|
| 535 | | - return -ENOMEM; |
|---|
| 536 | | - |
|---|
| 537 | | - file->private_data = state; |
|---|
| 538 | | - return 0; |
|---|
| 539 | | -} |
|---|
| 540 | | - |
|---|
| 541 | | -static int pending_reads_release(struct inode *inode, struct file *file) |
|---|
| 542 | | -{ |
|---|
| 543 | | - kfree(file->private_data); |
|---|
| 544 | | - return 0; |
|---|
| 545 | | -} |
|---|
| 546 | | - |
|---|
| 547 | | -static struct inode *fetch_pending_reads_inode(struct super_block *sb) |
|---|
| 548 | | -{ |
|---|
| 549 | | - struct inode_search search = { |
|---|
| 550 | | - .ino = INCFS_PENDING_READS_INODE |
|---|
| 551 | | - }; |
|---|
| 552 | | - struct inode *inode = iget5_locked(sb, search.ino, inode_test, |
|---|
| 553 | | - inode_set, &search); |
|---|
| 554 | | - |
|---|
| 555 | | - if (!inode) |
|---|
| 556 | | - return ERR_PTR(-ENOMEM); |
|---|
| 557 | | - |
|---|
| 558 | | - if (inode->i_state & I_NEW) |
|---|
| 559 | | - unlock_new_inode(inode); |
|---|
| 560 | | - |
|---|
| 561 | | - return inode; |
|---|
| 562 | | -} |
|---|
| 563 | | - |
|---|
| 564 | | -static int log_open(struct inode *inode, struct file *file) |
|---|
| 565 | | -{ |
|---|
| 566 | | - struct log_file_state *log_state = NULL; |
|---|
| 567 | | - struct mount_info *mi = get_mount_info(file_superblock(file)); |
|---|
| 568 | | - |
|---|
| 569 | | - log_state = kzalloc(sizeof(*log_state), GFP_NOFS); |
|---|
| 570 | | - if (!log_state) |
|---|
| 571 | | - return -ENOMEM; |
|---|
| 572 | | - |
|---|
| 573 | | - log_state->state = incfs_get_log_state(mi); |
|---|
| 574 | | - file->private_data = log_state; |
|---|
| 575 | | - return 0; |
|---|
| 576 | | -} |
|---|
| 577 | | - |
|---|
| 578 | | -static int log_release(struct inode *inode, struct file *file) |
|---|
| 579 | | -{ |
|---|
| 580 | | - kfree(file->private_data); |
|---|
| 581 | | - return 0; |
|---|
| 582 | | -} |
|---|
| 583 | | - |
|---|
| 584 | | -static ssize_t log_read(struct file *f, char __user *buf, size_t len, |
|---|
| 585 | | - loff_t *ppos) |
|---|
| 586 | | -{ |
|---|
| 587 | | - struct log_file_state *log_state = f->private_data; |
|---|
| 588 | | - struct mount_info *mi = get_mount_info(file_superblock(f)); |
|---|
| 589 | | - int total_reads_collected = 0; |
|---|
| 590 | | - int rl_size; |
|---|
| 591 | | - ssize_t result = 0; |
|---|
| 592 | | - struct incfs_pending_read_info *reads_buf; |
|---|
| 593 | | - ssize_t reads_to_collect = len / sizeof(*reads_buf); |
|---|
| 594 | | - ssize_t reads_per_page = PAGE_SIZE / sizeof(*reads_buf); |
|---|
| 595 | | - |
|---|
| 596 | | - rl_size = READ_ONCE(mi->mi_log.rl_size); |
|---|
| 597 | | - if (rl_size == 0) |
|---|
| 598 | | - return 0; |
|---|
| 599 | | - |
|---|
| 600 | | - reads_buf = (struct incfs_pending_read_info *)__get_free_page(GFP_NOFS); |
|---|
| 601 | | - if (!reads_buf) |
|---|
| 602 | | - return -ENOMEM; |
|---|
| 603 | | - |
|---|
| 604 | | - reads_to_collect = min_t(ssize_t, rl_size, reads_to_collect); |
|---|
| 605 | | - while (reads_to_collect > 0) { |
|---|
| 606 | | - struct read_log_state next_state; |
|---|
| 607 | | - int reads_collected; |
|---|
| 608 | | - |
|---|
| 609 | | - memcpy(&next_state, &log_state->state, sizeof(next_state)); |
|---|
| 610 | | - reads_collected = incfs_collect_logged_reads( |
|---|
| 611 | | - mi, &next_state, reads_buf, |
|---|
| 612 | | - min_t(ssize_t, reads_to_collect, reads_per_page)); |
|---|
| 613 | | - if (reads_collected <= 0) { |
|---|
| 614 | | - result = total_reads_collected ? |
|---|
| 615 | | - total_reads_collected * |
|---|
| 616 | | - sizeof(*reads_buf) : |
|---|
| 617 | | - reads_collected; |
|---|
| 618 | | - goto out; |
|---|
| 619 | | - } |
|---|
| 620 | | - if (copy_to_user(buf, reads_buf, |
|---|
| 621 | | - reads_collected * sizeof(*reads_buf))) { |
|---|
| 622 | | - result = total_reads_collected ? |
|---|
| 623 | | - total_reads_collected * |
|---|
| 624 | | - sizeof(*reads_buf) : |
|---|
| 625 | | - -EFAULT; |
|---|
| 626 | | - goto out; |
|---|
| 627 | | - } |
|---|
| 628 | | - |
|---|
| 629 | | - memcpy(&log_state->state, &next_state, sizeof(next_state)); |
|---|
| 630 | | - total_reads_collected += reads_collected; |
|---|
| 631 | | - buf += reads_collected * sizeof(*reads_buf); |
|---|
| 632 | | - reads_to_collect -= reads_collected; |
|---|
| 633 | | - } |
|---|
| 634 | | - |
|---|
| 635 | | - result = total_reads_collected * sizeof(*reads_buf); |
|---|
| 636 | | - *ppos = 0; |
|---|
| 637 | | -out: |
|---|
| 638 | | - if (reads_buf) |
|---|
| 639 | | - free_page((unsigned long)reads_buf); |
|---|
| 640 | | - return result; |
|---|
| 641 | | -} |
|---|
| 642 | | - |
|---|
| 643 | | -static __poll_t log_poll(struct file *file, poll_table *wait) |
|---|
| 644 | | -{ |
|---|
| 645 | | - struct log_file_state *log_state = file->private_data; |
|---|
| 646 | | - struct mount_info *mi = get_mount_info(file_superblock(file)); |
|---|
| 647 | | - int count; |
|---|
| 648 | | - __poll_t ret = 0; |
|---|
| 649 | | - |
|---|
| 650 | | - poll_wait(file, &mi->mi_log.ml_notif_wq, wait); |
|---|
| 651 | | - count = incfs_get_uncollected_logs_count(mi, &log_state->state); |
|---|
| 652 | | - if (count >= mi->mi_options.read_log_wakeup_count) |
|---|
| 653 | | - ret = EPOLLIN | EPOLLRDNORM; |
|---|
| 654 | | - |
|---|
| 655 | | - return ret; |
|---|
| 656 | | -} |
|---|
| 657 | | - |
|---|
| 658 | | -static struct inode *fetch_log_inode(struct super_block *sb) |
|---|
| 659 | | -{ |
|---|
| 660 | | - struct inode_search search = { |
|---|
| 661 | | - .ino = INCFS_LOG_INODE |
|---|
| 376 | + .verity = read_verity_attr(backing_dentry), |
|---|
| 662 | 377 | }; |
|---|
| 663 | 378 | struct inode *inode = iget5_locked(sb, search.ino, inode_test, |
|---|
| 664 | 379 | inode_set, &search); |
|---|
| .. | .. |
|---|
| 679 | 394 | struct mount_info *mi = get_mount_info(file_superblock(file)); |
|---|
| 680 | 395 | bool root; |
|---|
| 681 | 396 | |
|---|
| 682 | | - if (!dir || !mi) { |
|---|
| 397 | + if (!dir) { |
|---|
| 683 | 398 | error = -EBADF; |
|---|
| 684 | 399 | goto out; |
|---|
| 685 | 400 | } |
|---|
| .. | .. |
|---|
| 687 | 402 | root = dir->backing_dir->f_inode |
|---|
| 688 | 403 | == d_inode(mi->mi_backing_dir_path.dentry); |
|---|
| 689 | 404 | |
|---|
| 690 | | - if (root && ctx->pos == 0) { |
|---|
| 691 | | - if (!dir_emit(ctx, pending_reads_file_name, |
|---|
| 692 | | - ARRAY_SIZE(pending_reads_file_name) - 1, |
|---|
| 693 | | - INCFS_PENDING_READS_INODE, DT_REG)) { |
|---|
| 694 | | - error = -EINVAL; |
|---|
| 405 | + if (root) { |
|---|
| 406 | + error = emit_pseudo_files(ctx); |
|---|
| 407 | + if (error) |
|---|
| 695 | 408 | goto out; |
|---|
| 696 | | - } |
|---|
| 697 | | - ctx->pos++; |
|---|
| 698 | 409 | } |
|---|
| 699 | 410 | |
|---|
| 700 | | - if (root && ctx->pos == 1) { |
|---|
| 701 | | - if (!dir_emit(ctx, log_file_name, |
|---|
| 702 | | - ARRAY_SIZE(log_file_name) - 1, |
|---|
| 703 | | - INCFS_LOG_INODE, DT_REG)) { |
|---|
| 704 | | - error = -EINVAL; |
|---|
| 705 | | - goto out; |
|---|
| 706 | | - } |
|---|
| 707 | | - ctx->pos++; |
|---|
| 708 | | - } |
|---|
| 709 | | - |
|---|
| 710 | | - ctx->pos -= 2; |
|---|
| 411 | + ctx->pos -= PSEUDO_FILE_COUNT; |
|---|
| 711 | 412 | error = iterate_dir(dir->backing_dir, ctx); |
|---|
| 712 | | - ctx->pos += 2; |
|---|
| 413 | + ctx->pos += PSEUDO_FILE_COUNT; |
|---|
| 713 | 414 | file->f_pos = dir->backing_dir->f_pos; |
|---|
| 714 | 415 | out: |
|---|
| 715 | 416 | if (error) |
|---|
| .. | .. |
|---|
| 736 | 437 | return 0; |
|---|
| 737 | 438 | } |
|---|
| 738 | 439 | |
|---|
| 739 | | -static struct dentry *incfs_lookup_dentry(struct dentry *parent, |
|---|
| 740 | | - const char *name) |
|---|
| 440 | +static struct dentry *open_or_create_special_dir(struct dentry *backing_dir, |
|---|
| 441 | + const char *name, |
|---|
| 442 | + bool *created) |
|---|
| 741 | 443 | { |
|---|
| 742 | | - struct inode *inode; |
|---|
| 743 | | - struct dentry *result = NULL; |
|---|
| 744 | | - |
|---|
| 745 | | - if (!parent) |
|---|
| 746 | | - return ERR_PTR(-EFAULT); |
|---|
| 747 | | - |
|---|
| 748 | | - inode = d_inode(parent); |
|---|
| 749 | | - inode_lock_nested(inode, I_MUTEX_PARENT); |
|---|
| 750 | | - result = lookup_one_len(name, parent, strlen(name)); |
|---|
| 751 | | - inode_unlock(inode); |
|---|
| 752 | | - |
|---|
| 753 | | - if (IS_ERR(result)) |
|---|
| 754 | | - pr_warn("%s err:%ld\n", __func__, PTR_ERR(result)); |
|---|
| 755 | | - |
|---|
| 756 | | - return result; |
|---|
| 757 | | -} |
|---|
| 758 | | - |
|---|
| 759 | | -static struct dentry *open_or_create_index_dir(struct dentry *backing_dir) |
|---|
| 760 | | -{ |
|---|
| 761 | | - static const char name[] = ".index"; |
|---|
| 762 | 444 | struct dentry *index_dentry; |
|---|
| 763 | 445 | struct inode *backing_inode = d_inode(backing_dir); |
|---|
| 764 | 446 | int err = 0; |
|---|
| .. | .. |
|---|
| 770 | 452 | return index_dentry; |
|---|
| 771 | 453 | } else if (d_really_is_positive(index_dentry)) { |
|---|
| 772 | 454 | /* Index already exists. */ |
|---|
| 455 | + *created = false; |
|---|
| 773 | 456 | return index_dentry; |
|---|
| 774 | 457 | } |
|---|
| 775 | 458 | |
|---|
| .. | .. |
|---|
| 783 | 466 | return ERR_PTR(err); |
|---|
| 784 | 467 | } |
|---|
| 785 | 468 | |
|---|
| 786 | | - if (!d_really_is_positive(index_dentry)) { |
|---|
| 469 | + if (!d_really_is_positive(index_dentry) || |
|---|
| 470 | + unlikely(d_unhashed(index_dentry))) { |
|---|
| 787 | 471 | dput(index_dentry); |
|---|
| 788 | 472 | return ERR_PTR(-EINVAL); |
|---|
| 789 | 473 | } |
|---|
| 790 | 474 | |
|---|
| 475 | + *created = true; |
|---|
| 791 | 476 | return index_dentry; |
|---|
| 477 | +} |
|---|
| 478 | + |
|---|
| 479 | +static int read_single_page_timeouts(struct data_file *df, struct file *f, |
|---|
| 480 | + int block_index, struct mem_range range, |
|---|
| 481 | + struct mem_range tmp, |
|---|
| 482 | + unsigned int *delayed_min_us) |
|---|
| 483 | +{ |
|---|
| 484 | + struct mount_info *mi = df->df_mount_info; |
|---|
| 485 | + struct incfs_read_data_file_timeouts timeouts = { |
|---|
| 486 | + .max_pending_time_us = U32_MAX, |
|---|
| 487 | + }; |
|---|
| 488 | + int uid = current_uid().val; |
|---|
| 489 | + int i; |
|---|
| 490 | + |
|---|
| 491 | + spin_lock(&mi->mi_per_uid_read_timeouts_lock); |
|---|
| 492 | + for (i = 0; i < mi->mi_per_uid_read_timeouts_size / |
|---|
| 493 | + sizeof(*mi->mi_per_uid_read_timeouts); ++i) { |
|---|
| 494 | + struct incfs_per_uid_read_timeouts *t = |
|---|
| 495 | + &mi->mi_per_uid_read_timeouts[i]; |
|---|
| 496 | + |
|---|
| 497 | + if(t->uid == uid) { |
|---|
| 498 | + timeouts.min_time_us = t->min_time_us; |
|---|
| 499 | + timeouts.min_pending_time_us = t->min_pending_time_us; |
|---|
| 500 | + timeouts.max_pending_time_us = t->max_pending_time_us; |
|---|
| 501 | + break; |
|---|
| 502 | + } |
|---|
| 503 | + } |
|---|
| 504 | + spin_unlock(&mi->mi_per_uid_read_timeouts_lock); |
|---|
| 505 | + if (timeouts.max_pending_time_us == U32_MAX) { |
|---|
| 506 | + u64 read_timeout_us = (u64)mi->mi_options.read_timeout_ms * |
|---|
| 507 | + 1000; |
|---|
| 508 | + |
|---|
| 509 | + timeouts.max_pending_time_us = read_timeout_us <= U32_MAX ? |
|---|
| 510 | + read_timeout_us : U32_MAX; |
|---|
| 511 | + } |
|---|
| 512 | + |
|---|
| 513 | + return incfs_read_data_file_block(range, f, block_index, tmp, |
|---|
| 514 | + &timeouts, delayed_min_us); |
|---|
| 515 | +} |
|---|
| 516 | + |
|---|
| 517 | +static int usleep_interruptible(u32 us) |
|---|
| 518 | +{ |
|---|
| 519 | + /* See: |
|---|
| 520 | + * https://www.kernel.org/doc/Documentation/timers/timers-howto.txt |
|---|
| 521 | + * for explanation |
|---|
| 522 | + */ |
|---|
| 523 | + if (us < 10) { |
|---|
| 524 | + udelay(us); |
|---|
| 525 | + return 0; |
|---|
| 526 | + } else if (us < 20000) { |
|---|
| 527 | + usleep_range(us, us + us / 10); |
|---|
| 528 | + return 0; |
|---|
| 529 | + } else |
|---|
| 530 | + return msleep_interruptible(us / 1000); |
|---|
| 792 | 531 | } |
|---|
| 793 | 532 | |
|---|
| 794 | 533 | static int read_single_page(struct file *f, struct page *page) |
|---|
| .. | .. |
|---|
| 799 | 538 | ssize_t read_result = 0; |
|---|
| 800 | 539 | struct data_file *df = get_incfs_data_file(f); |
|---|
| 801 | 540 | int result = 0; |
|---|
| 802 | | - void *page_start = kmap(page); |
|---|
| 541 | + void *page_start; |
|---|
| 803 | 542 | int block_index; |
|---|
| 804 | | - int timeout_ms; |
|---|
| 543 | + unsigned int delayed_min_us = 0; |
|---|
| 805 | 544 | |
|---|
| 806 | | - if (!df) |
|---|
| 545 | + if (!df) { |
|---|
| 546 | + SetPageError(page); |
|---|
| 547 | + unlock_page(page); |
|---|
| 807 | 548 | return -EBADF; |
|---|
| 549 | + } |
|---|
| 808 | 550 | |
|---|
| 551 | + page_start = kmap(page); |
|---|
| 809 | 552 | offset = page_offset(page); |
|---|
| 810 | | - block_index = offset / INCFS_DATA_FILE_BLOCK_SIZE; |
|---|
| 553 | + block_index = (offset + df->df_mapped_offset) / |
|---|
| 554 | + INCFS_DATA_FILE_BLOCK_SIZE; |
|---|
| 811 | 555 | size = df->df_size; |
|---|
| 812 | | - timeout_ms = df->df_mount_info->mi_options.read_timeout_ms; |
|---|
| 813 | 556 | |
|---|
| 814 | 557 | if (offset < size) { |
|---|
| 815 | 558 | struct mem_range tmp = { |
|---|
| 816 | 559 | .len = 2 * INCFS_DATA_FILE_BLOCK_SIZE |
|---|
| 817 | 560 | }; |
|---|
| 818 | | - |
|---|
| 819 | 561 | tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len)); |
|---|
| 562 | + if (!tmp.data) { |
|---|
| 563 | + read_result = -ENOMEM; |
|---|
| 564 | + goto err; |
|---|
| 565 | + } |
|---|
| 820 | 566 | bytes_to_read = min_t(loff_t, size - offset, PAGE_SIZE); |
|---|
| 821 | | - read_result = incfs_read_data_file_block( |
|---|
| 822 | | - range(page_start, bytes_to_read), f, block_index, |
|---|
| 823 | | - timeout_ms, tmp); |
|---|
| 567 | + |
|---|
| 568 | + read_result = read_single_page_timeouts(df, f, block_index, |
|---|
| 569 | + range(page_start, bytes_to_read), tmp, |
|---|
| 570 | + &delayed_min_us); |
|---|
| 824 | 571 | |
|---|
| 825 | 572 | free_pages((unsigned long)tmp.data, get_order(tmp.len)); |
|---|
| 826 | 573 | } else { |
|---|
| .. | .. |
|---|
| 828 | 575 | read_result = 0; |
|---|
| 829 | 576 | } |
|---|
| 830 | 577 | |
|---|
| 578 | +err: |
|---|
| 831 | 579 | if (read_result < 0) |
|---|
| 832 | 580 | result = read_result; |
|---|
| 833 | 581 | else if (read_result < PAGE_SIZE) |
|---|
| .. | .. |
|---|
| 841 | 589 | flush_dcache_page(page); |
|---|
| 842 | 590 | kunmap(page); |
|---|
| 843 | 591 | unlock_page(page); |
|---|
| 592 | + if (delayed_min_us) |
|---|
| 593 | + usleep_interruptible(delayed_min_us); |
|---|
| 844 | 594 | return result; |
|---|
| 845 | 595 | } |
|---|
| 846 | 596 | |
|---|
| 847 | | -static char *file_id_to_str(incfs_uuid_t id) |
|---|
| 848 | | -{ |
|---|
| 849 | | - char *result = kmalloc(1 + sizeof(id.bytes) * 2, GFP_NOFS); |
|---|
| 850 | | - char *end; |
|---|
| 851 | | - |
|---|
| 852 | | - if (!result) |
|---|
| 853 | | - return NULL; |
|---|
| 854 | | - |
|---|
| 855 | | - end = bin2hex(result, id.bytes, sizeof(id.bytes)); |
|---|
| 856 | | - *end = 0; |
|---|
| 857 | | - return result; |
|---|
| 858 | | -} |
|---|
| 859 | | - |
|---|
| 860 | | -static struct mem_range incfs_copy_signature_info_from_user(u8 __user *original, |
|---|
| 861 | | - u64 size) |
|---|
| 862 | | -{ |
|---|
| 863 | | - u8 *result; |
|---|
| 864 | | - |
|---|
| 865 | | - if (!original) |
|---|
| 866 | | - return range(NULL, 0); |
|---|
| 867 | | - |
|---|
| 868 | | - if (size > INCFS_MAX_SIGNATURE_SIZE) |
|---|
| 869 | | - return range(ERR_PTR(-EFAULT), 0); |
|---|
| 870 | | - |
|---|
| 871 | | - result = kzalloc(size, GFP_NOFS | __GFP_COMP); |
|---|
| 872 | | - if (!result) |
|---|
| 873 | | - return range(ERR_PTR(-ENOMEM), 0); |
|---|
| 874 | | - |
|---|
| 875 | | - if (copy_from_user(result, original, size)) { |
|---|
| 876 | | - kfree(result); |
|---|
| 877 | | - return range(ERR_PTR(-EFAULT), 0); |
|---|
| 878 | | - } |
|---|
| 879 | | - |
|---|
| 880 | | - return range(result, size); |
|---|
| 881 | | -} |
|---|
| 882 | | - |
|---|
| 883 | | -static int init_new_file(struct mount_info *mi, struct dentry *dentry, |
|---|
| 884 | | - incfs_uuid_t *uuid, u64 size, struct mem_range attr, |
|---|
| 885 | | - u8 __user *user_signature_info, u64 signature_size) |
|---|
| 886 | | -{ |
|---|
| 887 | | - struct path path = {}; |
|---|
| 888 | | - struct file *new_file; |
|---|
| 889 | | - int error = 0; |
|---|
| 890 | | - struct backing_file_context *bfc = NULL; |
|---|
| 891 | | - u32 block_count; |
|---|
| 892 | | - struct mem_range raw_signature = { NULL }; |
|---|
| 893 | | - struct mtree *hash_tree = NULL; |
|---|
| 894 | | - |
|---|
| 895 | | - if (!mi || !dentry || !uuid) |
|---|
| 896 | | - return -EFAULT; |
|---|
| 897 | | - |
|---|
| 898 | | - /* Resize newly created file to its true size. */ |
|---|
| 899 | | - path = (struct path) { |
|---|
| 900 | | - .mnt = mi->mi_backing_dir_path.mnt, |
|---|
| 901 | | - .dentry = dentry |
|---|
| 902 | | - }; |
|---|
| 903 | | - new_file = dentry_open(&path, O_RDWR | O_NOATIME | O_LARGEFILE, |
|---|
| 904 | | - current_cred()); |
|---|
| 905 | | - |
|---|
| 906 | | - if (IS_ERR(new_file)) { |
|---|
| 907 | | - error = PTR_ERR(new_file); |
|---|
| 908 | | - goto out; |
|---|
| 909 | | - } |
|---|
| 910 | | - |
|---|
| 911 | | - bfc = incfs_alloc_bfc(mi, new_file); |
|---|
| 912 | | - fput(new_file); |
|---|
| 913 | | - if (IS_ERR(bfc)) { |
|---|
| 914 | | - error = PTR_ERR(bfc); |
|---|
| 915 | | - bfc = NULL; |
|---|
| 916 | | - goto out; |
|---|
| 917 | | - } |
|---|
| 918 | | - |
|---|
| 919 | | - mutex_lock(&bfc->bc_mutex); |
|---|
| 920 | | - error = incfs_write_fh_to_backing_file(bfc, uuid, size); |
|---|
| 921 | | - if (error) |
|---|
| 922 | | - goto out; |
|---|
| 923 | | - |
|---|
| 924 | | - if (attr.data && attr.len) { |
|---|
| 925 | | - error = incfs_write_file_attr_to_backing_file(bfc, |
|---|
| 926 | | - attr, NULL); |
|---|
| 927 | | - if (error) |
|---|
| 928 | | - goto out; |
|---|
| 929 | | - } |
|---|
| 930 | | - |
|---|
| 931 | | - block_count = (u32)get_blocks_count_for_size(size); |
|---|
| 932 | | - |
|---|
| 933 | | - if (user_signature_info) { |
|---|
| 934 | | - raw_signature = incfs_copy_signature_info_from_user( |
|---|
| 935 | | - user_signature_info, signature_size); |
|---|
| 936 | | - |
|---|
| 937 | | - if (IS_ERR(raw_signature.data)) { |
|---|
| 938 | | - error = PTR_ERR(raw_signature.data); |
|---|
| 939 | | - raw_signature.data = NULL; |
|---|
| 940 | | - goto out; |
|---|
| 941 | | - } |
|---|
| 942 | | - |
|---|
| 943 | | - hash_tree = incfs_alloc_mtree(raw_signature, block_count); |
|---|
| 944 | | - if (IS_ERR(hash_tree)) { |
|---|
| 945 | | - error = PTR_ERR(hash_tree); |
|---|
| 946 | | - hash_tree = NULL; |
|---|
| 947 | | - goto out; |
|---|
| 948 | | - } |
|---|
| 949 | | - |
|---|
| 950 | | - error = incfs_write_signature_to_backing_file( |
|---|
| 951 | | - bfc, raw_signature, hash_tree->hash_tree_area_size); |
|---|
| 952 | | - if (error) |
|---|
| 953 | | - goto out; |
|---|
| 954 | | - |
|---|
| 955 | | - block_count += get_blocks_count_for_size( |
|---|
| 956 | | - hash_tree->hash_tree_area_size); |
|---|
| 957 | | - } |
|---|
| 958 | | - |
|---|
| 959 | | - if (block_count) |
|---|
| 960 | | - error = incfs_write_blockmap_to_backing_file(bfc, block_count); |
|---|
| 961 | | - |
|---|
| 962 | | - if (error) |
|---|
| 963 | | - goto out; |
|---|
| 964 | | -out: |
|---|
| 965 | | - if (bfc) { |
|---|
| 966 | | - mutex_unlock(&bfc->bc_mutex); |
|---|
| 967 | | - incfs_free_bfc(bfc); |
|---|
| 968 | | - } |
|---|
| 969 | | - incfs_free_mtree(hash_tree); |
|---|
| 970 | | - kfree(raw_signature.data); |
|---|
| 971 | | - |
|---|
| 972 | | - if (error) |
|---|
| 973 | | - pr_debug("incfs: %s error: %d\n", __func__, error); |
|---|
| 974 | | - return error; |
|---|
| 975 | | -} |
|---|
| 976 | | - |
|---|
| 977 | | -static int incfs_link(struct dentry *what, struct dentry *where) |
|---|
| 597 | +int incfs_link(struct dentry *what, struct dentry *where) |
|---|
| 978 | 598 | { |
|---|
| 979 | 599 | struct dentry *parent_dentry = dget_parent(where); |
|---|
| 980 | 600 | struct inode *pinode = d_inode(parent_dentry); |
|---|
| .. | .. |
|---|
| 988 | 608 | return error; |
|---|
| 989 | 609 | } |
|---|
| 990 | 610 | |
|---|
| 991 | | -static int incfs_unlink(struct dentry *dentry) |
|---|
| 611 | +int incfs_unlink(struct dentry *dentry) |
|---|
| 992 | 612 | { |
|---|
| 993 | 613 | struct dentry *parent_dentry = dget_parent(dentry); |
|---|
| 994 | 614 | struct inode *pinode = d_inode(parent_dentry); |
|---|
| .. | .. |
|---|
| 1016 | 636 | return error; |
|---|
| 1017 | 637 | } |
|---|
| 1018 | 638 | |
|---|
| 1019 | | -static int dir_relative_path_resolve( |
|---|
| 1020 | | - struct mount_info *mi, |
|---|
| 1021 | | - const char __user *relative_path, |
|---|
| 1022 | | - struct path *result_path) |
|---|
| 639 | +static void notify_unlink(struct dentry *dentry, const char *file_id_str, |
|---|
| 640 | + const char *special_directory) |
|---|
| 1023 | 641 | { |
|---|
| 1024 | | - struct path *base_path = &mi->mi_backing_dir_path; |
|---|
| 1025 | | - int dir_fd = get_unused_fd_flags(0); |
|---|
| 1026 | | - struct file *dir_f = NULL; |
|---|
| 642 | + struct dentry *root = dentry; |
|---|
| 643 | + struct dentry *file = NULL; |
|---|
| 644 | + struct dentry *dir = NULL; |
|---|
| 1027 | 645 | int error = 0; |
|---|
| 646 | + bool take_lock = root->d_parent != root->d_parent->d_parent; |
|---|
| 1028 | 647 | |
|---|
| 1029 | | - if (dir_fd < 0) |
|---|
| 1030 | | - return dir_fd; |
|---|
| 648 | + while (root != root->d_parent) |
|---|
| 649 | + root = root->d_parent; |
|---|
| 1031 | 650 | |
|---|
| 1032 | | - dir_f = dentry_open(base_path, O_RDONLY | O_NOATIME, current_cred()); |
|---|
| 651 | + if (take_lock) |
|---|
| 652 | + dir = incfs_lookup_dentry(root, special_directory); |
|---|
| 653 | + else |
|---|
| 654 | + dir = lookup_one_len(special_directory, root, |
|---|
| 655 | + strlen(special_directory)); |
|---|
| 1033 | 656 | |
|---|
| 1034 | | - if (IS_ERR(dir_f)) { |
|---|
| 1035 | | - error = PTR_ERR(dir_f); |
|---|
| 657 | + if (IS_ERR(dir)) { |
|---|
| 658 | + error = PTR_ERR(dir); |
|---|
| 1036 | 659 | goto out; |
|---|
| 1037 | 660 | } |
|---|
| 1038 | | - fd_install(dir_fd, dir_f); |
|---|
| 1039 | | - |
|---|
| 1040 | | - if (!relative_path) { |
|---|
| 1041 | | - /* No relative path given, just return the base dir. */ |
|---|
| 1042 | | - *result_path = *base_path; |
|---|
| 1043 | | - path_get(result_path); |
|---|
| 661 | + if (d_is_negative(dir)) { |
|---|
| 662 | + error = -ENOENT; |
|---|
| 1044 | 663 | goto out; |
|---|
| 1045 | 664 | } |
|---|
| 1046 | 665 | |
|---|
| 1047 | | - error = user_path_at_empty(dir_fd, relative_path, |
|---|
| 1048 | | - LOOKUP_FOLLOW | LOOKUP_DIRECTORY, result_path, NULL); |
|---|
| 666 | + file = incfs_lookup_dentry(dir, file_id_str); |
|---|
| 667 | + if (IS_ERR(file)) { |
|---|
| 668 | + error = PTR_ERR(file); |
|---|
| 669 | + goto out; |
|---|
| 670 | + } |
|---|
| 671 | + if (d_is_negative(file)) { |
|---|
| 672 | + error = -ENOENT; |
|---|
| 673 | + goto out; |
|---|
| 674 | + } |
|---|
| 675 | + |
|---|
| 676 | + fsnotify_unlink(d_inode(dir), file); |
|---|
| 677 | + d_delete(file); |
|---|
| 1049 | 678 | |
|---|
| 1050 | 679 | out: |
|---|
| 1051 | | - ksys_close(dir_fd); |
|---|
| 1052 | 680 | if (error) |
|---|
| 1053 | | - pr_debug("incfs: %s %d\n", __func__, error); |
|---|
| 1054 | | - return error; |
|---|
| 681 | + pr_warn("%s failed with error %d\n", __func__, error); |
|---|
| 682 | + |
|---|
| 683 | + dput(dir); |
|---|
| 684 | + dput(file); |
|---|
| 1055 | 685 | } |
|---|
| 1056 | 686 | |
|---|
| 1057 | | -static int validate_name(char *file_name) |
|---|
| 687 | +static void handle_file_completed(struct file *f, struct data_file *df) |
|---|
| 1058 | 688 | { |
|---|
| 1059 | | - struct mem_range name = range(file_name, strlen(file_name)); |
|---|
| 1060 | | - int i = 0; |
|---|
| 1061 | | - |
|---|
| 1062 | | - if (name.len > INCFS_MAX_NAME_LEN) |
|---|
| 1063 | | - return -ENAMETOOLONG; |
|---|
| 1064 | | - |
|---|
| 1065 | | - if (incfs_equal_ranges(pending_reads_file_name_range, name)) |
|---|
| 1066 | | - return -EINVAL; |
|---|
| 1067 | | - |
|---|
| 1068 | | - for (i = 0; i < name.len; i++) |
|---|
| 1069 | | - if (name.data[i] == '/') |
|---|
| 1070 | | - return -EINVAL; |
|---|
| 1071 | | - |
|---|
| 1072 | | - return 0; |
|---|
| 1073 | | -} |
|---|
| 1074 | | - |
|---|
| 1075 | | -static int chmod(struct dentry *dentry, umode_t mode) |
|---|
| 1076 | | -{ |
|---|
| 1077 | | - struct inode *inode = dentry->d_inode; |
|---|
| 1078 | | - struct inode *delegated_inode = NULL; |
|---|
| 1079 | | - struct iattr newattrs; |
|---|
| 689 | + struct backing_file_context *bfc; |
|---|
| 690 | + struct mount_info *mi = df->df_mount_info; |
|---|
| 691 | + char *file_id_str = NULL; |
|---|
| 692 | + struct dentry *incomplete_file_dentry = NULL; |
|---|
| 693 | + const struct cred *old_cred = override_creds(mi->mi_owner); |
|---|
| 1080 | 694 | int error; |
|---|
| 1081 | 695 | |
|---|
| 1082 | | -retry_deleg: |
|---|
| 1083 | | - inode_lock(inode); |
|---|
| 1084 | | - newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
|---|
| 1085 | | - newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
|---|
| 1086 | | - error = notify_change(dentry, &newattrs, &delegated_inode); |
|---|
| 1087 | | - inode_unlock(inode); |
|---|
| 1088 | | - if (delegated_inode) { |
|---|
| 1089 | | - error = break_deleg_wait(&delegated_inode); |
|---|
| 1090 | | - if (!error) |
|---|
| 1091 | | - goto retry_deleg; |
|---|
| 1092 | | - } |
|---|
| 1093 | | - return error; |
|---|
| 1094 | | -} |
|---|
| 696 | + /* Truncate file to remove any preallocated space */ |
|---|
| 697 | + bfc = df->df_backing_file_context; |
|---|
| 698 | + if (bfc) { |
|---|
| 699 | + struct file *f = bfc->bc_file; |
|---|
| 1095 | 700 | |
|---|
| 1096 | | -static long ioctl_create_file(struct mount_info *mi, |
|---|
| 1097 | | - struct incfs_new_file_args __user *usr_args) |
|---|
| 1098 | | -{ |
|---|
| 1099 | | - struct incfs_new_file_args args; |
|---|
| 1100 | | - char *file_id_str = NULL; |
|---|
| 1101 | | - struct dentry *index_file_dentry = NULL; |
|---|
| 1102 | | - struct dentry *named_file_dentry = NULL; |
|---|
| 1103 | | - struct path parent_dir_path = {}; |
|---|
| 1104 | | - struct inode *index_dir_inode = NULL; |
|---|
| 1105 | | - __le64 size_attr_value = 0; |
|---|
| 1106 | | - char *file_name = NULL; |
|---|
| 1107 | | - char *attr_value = NULL; |
|---|
| 1108 | | - int error = 0; |
|---|
| 1109 | | - bool locked = false; |
|---|
| 701 | + if (f) { |
|---|
| 702 | + loff_t size = i_size_read(file_inode(f)); |
|---|
| 1110 | 703 | |
|---|
| 1111 | | - if (!mi || !mi->mi_index_dir) { |
|---|
| 1112 | | - error = -EFAULT; |
|---|
| 1113 | | - goto out; |
|---|
| 1114 | | - } |
|---|
| 1115 | | - |
|---|
| 1116 | | - if (copy_from_user(&args, usr_args, sizeof(args)) > 0) { |
|---|
| 1117 | | - error = -EFAULT; |
|---|
| 1118 | | - goto out; |
|---|
| 1119 | | - } |
|---|
| 1120 | | - |
|---|
| 1121 | | - file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX); |
|---|
| 1122 | | - if (IS_ERR(file_name)) { |
|---|
| 1123 | | - error = PTR_ERR(file_name); |
|---|
| 1124 | | - file_name = NULL; |
|---|
| 1125 | | - goto out; |
|---|
| 1126 | | - } |
|---|
| 1127 | | - |
|---|
| 1128 | | - error = validate_name(file_name); |
|---|
| 1129 | | - if (error) |
|---|
| 1130 | | - goto out; |
|---|
| 1131 | | - |
|---|
| 1132 | | - file_id_str = file_id_to_str(args.file_id); |
|---|
| 1133 | | - if (!file_id_str) { |
|---|
| 1134 | | - error = -ENOMEM; |
|---|
| 1135 | | - goto out; |
|---|
| 1136 | | - } |
|---|
| 1137 | | - |
|---|
| 1138 | | - error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex); |
|---|
| 1139 | | - if (error) |
|---|
| 1140 | | - goto out; |
|---|
| 1141 | | - locked = true; |
|---|
| 1142 | | - |
|---|
| 1143 | | - /* Find a directory to put the file into. */ |
|---|
| 1144 | | - error = dir_relative_path_resolve(mi, |
|---|
| 1145 | | - u64_to_user_ptr(args.directory_path), |
|---|
| 1146 | | - &parent_dir_path); |
|---|
| 1147 | | - if (error) |
|---|
| 1148 | | - goto out; |
|---|
| 1149 | | - |
|---|
| 1150 | | - if (parent_dir_path.dentry == mi->mi_index_dir) { |
|---|
| 1151 | | - /* Can't create a file directly inside .index */ |
|---|
| 1152 | | - error = -EBUSY; |
|---|
| 1153 | | - goto out; |
|---|
| 1154 | | - } |
|---|
| 1155 | | - |
|---|
| 1156 | | - /* Look up a dentry in the parent dir. It should be negative. */ |
|---|
| 1157 | | - named_file_dentry = incfs_lookup_dentry(parent_dir_path.dentry, |
|---|
| 1158 | | - file_name); |
|---|
| 1159 | | - if (!named_file_dentry) { |
|---|
| 1160 | | - error = -EFAULT; |
|---|
| 1161 | | - goto out; |
|---|
| 1162 | | - } |
|---|
| 1163 | | - if (IS_ERR(named_file_dentry)) { |
|---|
| 1164 | | - error = PTR_ERR(named_file_dentry); |
|---|
| 1165 | | - named_file_dentry = NULL; |
|---|
| 1166 | | - goto out; |
|---|
| 1167 | | - } |
|---|
| 1168 | | - if (d_really_is_positive(named_file_dentry)) { |
|---|
| 1169 | | - /* File with this path already exists. */ |
|---|
| 1170 | | - error = -EEXIST; |
|---|
| 1171 | | - goto out; |
|---|
| 1172 | | - } |
|---|
| 1173 | | - /* Look up a dentry in the .index dir. It should be negative. */ |
|---|
| 1174 | | - index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str); |
|---|
| 1175 | | - if (!index_file_dentry) { |
|---|
| 1176 | | - error = -EFAULT; |
|---|
| 1177 | | - goto out; |
|---|
| 1178 | | - } |
|---|
| 1179 | | - if (IS_ERR(index_file_dentry)) { |
|---|
| 1180 | | - error = PTR_ERR(index_file_dentry); |
|---|
| 1181 | | - index_file_dentry = NULL; |
|---|
| 1182 | | - goto out; |
|---|
| 1183 | | - } |
|---|
| 1184 | | - if (d_really_is_positive(index_file_dentry)) { |
|---|
| 1185 | | - /* File with this ID already exists in index. */ |
|---|
| 1186 | | - error = -EEXIST; |
|---|
| 1187 | | - goto out; |
|---|
| 1188 | | - } |
|---|
| 1189 | | - |
|---|
| 1190 | | - /* Creating a file in the .index dir. */ |
|---|
| 1191 | | - index_dir_inode = d_inode(mi->mi_index_dir); |
|---|
| 1192 | | - inode_lock_nested(index_dir_inode, I_MUTEX_PARENT); |
|---|
| 1193 | | - error = vfs_create(index_dir_inode, index_file_dentry, args.mode | 0222, |
|---|
| 1194 | | - true); |
|---|
| 1195 | | - inode_unlock(index_dir_inode); |
|---|
| 1196 | | - |
|---|
| 1197 | | - if (error) |
|---|
| 1198 | | - goto out; |
|---|
| 1199 | | - if (!d_really_is_positive(index_file_dentry)) { |
|---|
| 1200 | | - error = -EINVAL; |
|---|
| 1201 | | - goto out; |
|---|
| 1202 | | - } |
|---|
| 1203 | | - |
|---|
| 1204 | | - error = chmod(index_file_dentry, args.mode | 0222); |
|---|
| 1205 | | - if (error) { |
|---|
| 1206 | | - pr_debug("incfs: chmod err: %d\n", error); |
|---|
| 1207 | | - goto delete_index_file; |
|---|
| 1208 | | - } |
|---|
| 1209 | | - |
|---|
| 1210 | | - /* Save the file's ID as an xattr for easy fetching in future. */ |
|---|
| 1211 | | - error = vfs_setxattr(index_file_dentry, INCFS_XATTR_ID_NAME, |
|---|
| 1212 | | - file_id_str, strlen(file_id_str), XATTR_CREATE); |
|---|
| 1213 | | - if (error) { |
|---|
| 1214 | | - pr_debug("incfs: vfs_setxattr err:%d\n", error); |
|---|
| 1215 | | - goto delete_index_file; |
|---|
| 1216 | | - } |
|---|
| 1217 | | - |
|---|
| 1218 | | - /* Save the file's size as an xattr for easy fetching in future. */ |
|---|
| 1219 | | - size_attr_value = cpu_to_le64(args.size); |
|---|
| 1220 | | - error = vfs_setxattr(index_file_dentry, INCFS_XATTR_SIZE_NAME, |
|---|
| 1221 | | - (char *)&size_attr_value, sizeof(size_attr_value), |
|---|
| 1222 | | - XATTR_CREATE); |
|---|
| 1223 | | - if (error) { |
|---|
| 1224 | | - pr_debug("incfs: vfs_setxattr err:%d\n", error); |
|---|
| 1225 | | - goto delete_index_file; |
|---|
| 1226 | | - } |
|---|
| 1227 | | - |
|---|
| 1228 | | - /* Save the file's attribute as an xattr */ |
|---|
| 1229 | | - if (args.file_attr_len && args.file_attr) { |
|---|
| 1230 | | - if (args.file_attr_len > INCFS_MAX_FILE_ATTR_SIZE) { |
|---|
| 1231 | | - error = -E2BIG; |
|---|
| 1232 | | - goto delete_index_file; |
|---|
| 704 | + error = vfs_truncate(&f->f_path, size); |
|---|
| 705 | + if (error) |
|---|
| 706 | + /* No useful action on failure */ |
|---|
| 707 | + pr_warn("incfs: Failed to truncate complete file: %d\n", |
|---|
| 708 | + error); |
|---|
| 1233 | 709 | } |
|---|
| 1234 | | - |
|---|
| 1235 | | - attr_value = kmalloc(args.file_attr_len, GFP_NOFS); |
|---|
| 1236 | | - if (!attr_value) { |
|---|
| 1237 | | - error = -ENOMEM; |
|---|
| 1238 | | - goto delete_index_file; |
|---|
| 1239 | | - } |
|---|
| 1240 | | - |
|---|
| 1241 | | - if (copy_from_user(attr_value, |
|---|
| 1242 | | - u64_to_user_ptr(args.file_attr), |
|---|
| 1243 | | - args.file_attr_len) > 0) { |
|---|
| 1244 | | - error = -EFAULT; |
|---|
| 1245 | | - goto delete_index_file; |
|---|
| 1246 | | - } |
|---|
| 1247 | | - |
|---|
| 1248 | | - error = vfs_setxattr(index_file_dentry, |
|---|
| 1249 | | - INCFS_XATTR_METADATA_NAME, |
|---|
| 1250 | | - attr_value, args.file_attr_len, |
|---|
| 1251 | | - XATTR_CREATE); |
|---|
| 1252 | | - |
|---|
| 1253 | | - if (error) |
|---|
| 1254 | | - goto delete_index_file; |
|---|
| 1255 | 710 | } |
|---|
| 1256 | 711 | |
|---|
| 1257 | | - /* Initializing a newly created file. */ |
|---|
| 1258 | | - error = init_new_file(mi, index_file_dentry, &args.file_id, args.size, |
|---|
| 1259 | | - range(attr_value, args.file_attr_len), |
|---|
| 1260 | | - (u8 __user *)args.signature_info, |
|---|
| 1261 | | - args.signature_size); |
|---|
| 1262 | | - if (error) |
|---|
| 1263 | | - goto delete_index_file; |
|---|
| 1264 | | - |
|---|
| 1265 | | - /* Linking a file with it's real name from the requested dir. */ |
|---|
| 1266 | | - error = incfs_link(index_file_dentry, named_file_dentry); |
|---|
| 1267 | | - |
|---|
| 1268 | | - if (!error) |
|---|
| 712 | + /* This is best effort - there is no useful action to take on failure */ |
|---|
| 713 | + file_id_str = file_id_to_str(df->df_id); |
|---|
| 714 | + if (!file_id_str) |
|---|
| 1269 | 715 | goto out; |
|---|
| 1270 | 716 | |
|---|
| 1271 | | -delete_index_file: |
|---|
| 1272 | | - incfs_unlink(index_file_dentry); |
|---|
| 717 | + incomplete_file_dentry = incfs_lookup_dentry( |
|---|
| 718 | + df->df_mount_info->mi_incomplete_dir, |
|---|
| 719 | + file_id_str); |
|---|
| 720 | + if (!incomplete_file_dentry || IS_ERR(incomplete_file_dentry)) { |
|---|
| 721 | + incomplete_file_dentry = NULL; |
|---|
| 722 | + goto out; |
|---|
| 723 | + } |
|---|
| 724 | + |
|---|
| 725 | + if (!d_really_is_positive(incomplete_file_dentry)) |
|---|
| 726 | + goto out; |
|---|
| 727 | + |
|---|
| 728 | + vfs_fsync(df->df_backing_file_context->bc_file, 0); |
|---|
| 729 | + error = incfs_unlink(incomplete_file_dentry); |
|---|
| 730 | + if (error) { |
|---|
| 731 | + pr_warn("incfs: Deleting incomplete file failed: %d\n", error); |
|---|
| 732 | + goto out; |
|---|
| 733 | + } |
|---|
| 734 | + |
|---|
| 735 | + notify_unlink(f->f_path.dentry, file_id_str, INCFS_INCOMPLETE_NAME); |
|---|
| 1273 | 736 | |
|---|
| 1274 | 737 | out: |
|---|
| 1275 | | - if (error) |
|---|
| 1276 | | - pr_debug("incfs: %s err:%d\n", __func__, error); |
|---|
| 1277 | | - |
|---|
| 738 | + dput(incomplete_file_dentry); |
|---|
| 1278 | 739 | kfree(file_id_str); |
|---|
| 1279 | | - kfree(file_name); |
|---|
| 1280 | | - kfree(attr_value); |
|---|
| 1281 | | - dput(named_file_dentry); |
|---|
| 1282 | | - dput(index_file_dentry); |
|---|
| 1283 | | - path_put(&parent_dir_path); |
|---|
| 1284 | | - if (locked) |
|---|
| 1285 | | - mutex_unlock(&mi->mi_dir_struct_mutex); |
|---|
| 1286 | | - return error; |
|---|
| 740 | + revert_creds(old_cred); |
|---|
| 1287 | 741 | } |
|---|
| 1288 | 742 | |
|---|
| 1289 | 743 | static long ioctl_fill_blocks(struct file *f, void __user *arg) |
|---|
| .. | .. |
|---|
| 1292 | 746 | struct incfs_fill_blocks fill_blocks; |
|---|
| 1293 | 747 | struct incfs_fill_block __user *usr_fill_block_array; |
|---|
| 1294 | 748 | struct data_file *df = get_incfs_data_file(f); |
|---|
| 749 | + struct incfs_file_data *fd = f->private_data; |
|---|
| 1295 | 750 | const ssize_t data_buf_size = 2 * INCFS_DATA_FILE_BLOCK_SIZE; |
|---|
| 1296 | 751 | u8 *data_buf = NULL; |
|---|
| 1297 | 752 | ssize_t error = 0; |
|---|
| 1298 | 753 | int i = 0; |
|---|
| 754 | + bool complete = false; |
|---|
| 1299 | 755 | |
|---|
| 1300 | 756 | if (!df) |
|---|
| 1301 | 757 | return -EBADF; |
|---|
| 1302 | 758 | |
|---|
| 1303 | | - if ((uintptr_t)f->private_data != CAN_FILL) |
|---|
| 759 | + if (!fd || fd->fd_fill_permission != CAN_FILL) |
|---|
| 1304 | 760 | return -EPERM; |
|---|
| 1305 | 761 | |
|---|
| 1306 | 762 | if (copy_from_user(&fill_blocks, usr_fill_blocks, sizeof(fill_blocks))) |
|---|
| .. | .. |
|---|
| 1337 | 793 | data_buf); |
|---|
| 1338 | 794 | } else { |
|---|
| 1339 | 795 | error = incfs_process_new_data_block(df, &fill_block, |
|---|
| 1340 | | - data_buf); |
|---|
| 796 | + data_buf, &complete); |
|---|
| 1341 | 797 | } |
|---|
| 1342 | 798 | if (error) |
|---|
| 1343 | 799 | break; |
|---|
| .. | .. |
|---|
| 1345 | 801 | |
|---|
| 1346 | 802 | if (data_buf) |
|---|
| 1347 | 803 | free_pages((unsigned long)data_buf, get_order(data_buf_size)); |
|---|
| 804 | + |
|---|
| 805 | + if (complete) |
|---|
| 806 | + handle_file_completed(f, df); |
|---|
| 1348 | 807 | |
|---|
| 1349 | 808 | /* |
|---|
| 1350 | 809 | * Only report the error if no records were processed, otherwise |
|---|
| .. | .. |
|---|
| 1354 | 813 | return error; |
|---|
| 1355 | 814 | |
|---|
| 1356 | 815 | return i; |
|---|
| 1357 | | -} |
|---|
| 1358 | | - |
|---|
| 1359 | | -static long ioctl_permit_fill(struct file *f, void __user *arg) |
|---|
| 1360 | | -{ |
|---|
| 1361 | | - struct incfs_permit_fill __user *usr_permit_fill = arg; |
|---|
| 1362 | | - struct incfs_permit_fill permit_fill; |
|---|
| 1363 | | - long error = 0; |
|---|
| 1364 | | - struct file *file = NULL; |
|---|
| 1365 | | - |
|---|
| 1366 | | - if (f->f_op != &incfs_pending_read_file_ops) |
|---|
| 1367 | | - return -EPERM; |
|---|
| 1368 | | - |
|---|
| 1369 | | - if (copy_from_user(&permit_fill, usr_permit_fill, sizeof(permit_fill))) |
|---|
| 1370 | | - return -EFAULT; |
|---|
| 1371 | | - |
|---|
| 1372 | | - file = fget(permit_fill.file_descriptor); |
|---|
| 1373 | | - if (IS_ERR(file)) |
|---|
| 1374 | | - return PTR_ERR(file); |
|---|
| 1375 | | - |
|---|
| 1376 | | - if (file->f_op != &incfs_file_ops) { |
|---|
| 1377 | | - error = -EPERM; |
|---|
| 1378 | | - goto out; |
|---|
| 1379 | | - } |
|---|
| 1380 | | - |
|---|
| 1381 | | - if (file->f_inode->i_sb != f->f_inode->i_sb) { |
|---|
| 1382 | | - error = -EPERM; |
|---|
| 1383 | | - goto out; |
|---|
| 1384 | | - } |
|---|
| 1385 | | - |
|---|
| 1386 | | - switch ((uintptr_t)file->private_data) { |
|---|
| 1387 | | - case CANT_FILL: |
|---|
| 1388 | | - file->private_data = (void *)CAN_FILL; |
|---|
| 1389 | | - break; |
|---|
| 1390 | | - |
|---|
| 1391 | | - case CAN_FILL: |
|---|
| 1392 | | - pr_debug("CAN_FILL already set"); |
|---|
| 1393 | | - break; |
|---|
| 1394 | | - |
|---|
| 1395 | | - default: |
|---|
| 1396 | | - pr_warn("Invalid file private data"); |
|---|
| 1397 | | - error = -EFAULT; |
|---|
| 1398 | | - goto out; |
|---|
| 1399 | | - } |
|---|
| 1400 | | - |
|---|
| 1401 | | -out: |
|---|
| 1402 | | - fput(file); |
|---|
| 1403 | | - return error; |
|---|
| 1404 | 816 | } |
|---|
| 1405 | 817 | |
|---|
| 1406 | 818 | static long ioctl_read_file_signature(struct file *f, void __user *arg) |
|---|
| .. | .. |
|---|
| 1456 | 868 | struct incfs_get_filled_blocks_args __user *args_usr_ptr = arg; |
|---|
| 1457 | 869 | struct incfs_get_filled_blocks_args args = {}; |
|---|
| 1458 | 870 | struct data_file *df = get_incfs_data_file(f); |
|---|
| 871 | + struct incfs_file_data *fd = f->private_data; |
|---|
| 1459 | 872 | int error; |
|---|
| 1460 | 873 | |
|---|
| 1461 | | - if (!df) |
|---|
| 874 | + if (!df || !fd) |
|---|
| 1462 | 875 | return -EINVAL; |
|---|
| 1463 | 876 | |
|---|
| 1464 | | - if ((uintptr_t)f->private_data != CAN_FILL) |
|---|
| 877 | + if (fd->fd_fill_permission != CAN_FILL) |
|---|
| 1465 | 878 | return -EPERM; |
|---|
| 1466 | 879 | |
|---|
| 1467 | 880 | if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0) |
|---|
| 1468 | 881 | return -EINVAL; |
|---|
| 1469 | 882 | |
|---|
| 1470 | | - error = incfs_get_filled_blocks(df, &args); |
|---|
| 883 | + error = incfs_get_filled_blocks(df, fd, &args); |
|---|
| 1471 | 884 | |
|---|
| 1472 | 885 | if (copy_to_user(args_usr_ptr, &args, sizeof(args))) |
|---|
| 1473 | 886 | return -EFAULT; |
|---|
| .. | .. |
|---|
| 1475 | 888 | return error; |
|---|
| 1476 | 889 | } |
|---|
| 1477 | 890 | |
|---|
| 891 | +static long ioctl_get_block_count(struct file *f, void __user *arg) |
|---|
| 892 | +{ |
|---|
| 893 | + struct incfs_get_block_count_args __user *args_usr_ptr = arg; |
|---|
| 894 | + struct incfs_get_block_count_args args = {}; |
|---|
| 895 | + struct data_file *df = get_incfs_data_file(f); |
|---|
| 896 | + |
|---|
| 897 | + if (!df) |
|---|
| 898 | + return -EINVAL; |
|---|
| 899 | + |
|---|
| 900 | + args.total_data_blocks_out = df->df_data_block_count; |
|---|
| 901 | + args.filled_data_blocks_out = atomic_read(&df->df_data_blocks_written); |
|---|
| 902 | + args.total_hash_blocks_out = df->df_total_block_count - |
|---|
| 903 | + df->df_data_block_count; |
|---|
| 904 | + args.filled_hash_blocks_out = atomic_read(&df->df_hash_blocks_written); |
|---|
| 905 | + |
|---|
| 906 | + if (copy_to_user(args_usr_ptr, &args, sizeof(args))) |
|---|
| 907 | + return -EFAULT; |
|---|
| 908 | + |
|---|
| 909 | + return 0; |
|---|
| 910 | +} |
|---|
| 911 | + |
|---|
| 912 | +static int incfs_ioctl_get_flags(struct file *f, void __user *arg) |
|---|
| 913 | +{ |
|---|
| 914 | + u32 flags = IS_VERITY(file_inode(f)) ? FS_VERITY_FL : 0; |
|---|
| 915 | + |
|---|
| 916 | + return put_user(flags, (int __user *) arg); |
|---|
| 917 | +} |
|---|
| 918 | + |
|---|
| 1478 | 919 | static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg) |
|---|
| 1479 | 920 | { |
|---|
| 1480 | | - struct mount_info *mi = get_mount_info(file_superblock(f)); |
|---|
| 1481 | | - |
|---|
| 1482 | 921 | switch (req) { |
|---|
| 1483 | | - case INCFS_IOC_CREATE_FILE: |
|---|
| 1484 | | - return ioctl_create_file(mi, (void __user *)arg); |
|---|
| 1485 | 922 | case INCFS_IOC_FILL_BLOCKS: |
|---|
| 1486 | 923 | return ioctl_fill_blocks(f, (void __user *)arg); |
|---|
| 1487 | | - case INCFS_IOC_PERMIT_FILL: |
|---|
| 1488 | | - return ioctl_permit_fill(f, (void __user *)arg); |
|---|
| 1489 | 924 | case INCFS_IOC_READ_FILE_SIGNATURE: |
|---|
| 1490 | 925 | return ioctl_read_file_signature(f, (void __user *)arg); |
|---|
| 1491 | 926 | case INCFS_IOC_GET_FILLED_BLOCKS: |
|---|
| 1492 | 927 | return ioctl_get_filled_blocks(f, (void __user *)arg); |
|---|
| 928 | + case INCFS_IOC_GET_BLOCK_COUNT: |
|---|
| 929 | + return ioctl_get_block_count(f, (void __user *)arg); |
|---|
| 930 | + case FS_IOC_ENABLE_VERITY: |
|---|
| 931 | + return incfs_ioctl_enable_verity(f, (const void __user *)arg); |
|---|
| 932 | + case FS_IOC_GETFLAGS: |
|---|
| 933 | + return incfs_ioctl_get_flags(f, (void __user *) arg); |
|---|
| 934 | + case FS_IOC_MEASURE_VERITY: |
|---|
| 935 | + return incfs_ioctl_measure_verity(f, (void __user *)arg); |
|---|
| 936 | + case FS_IOC_READ_VERITY_METADATA: |
|---|
| 937 | + return incfs_ioctl_read_verity_metadata(f, (void __user *)arg); |
|---|
| 1493 | 938 | default: |
|---|
| 1494 | 939 | return -EINVAL; |
|---|
| 1495 | 940 | } |
|---|
| 1496 | 941 | } |
|---|
| 942 | + |
|---|
| 943 | +#ifdef CONFIG_COMPAT |
|---|
| 944 | +static long incfs_compat_ioctl(struct file *file, unsigned int cmd, |
|---|
| 945 | + unsigned long arg) |
|---|
| 946 | +{ |
|---|
| 947 | + switch (cmd) { |
|---|
| 948 | + case FS_IOC32_GETFLAGS: |
|---|
| 949 | + cmd = FS_IOC_GETFLAGS; |
|---|
| 950 | + break; |
|---|
| 951 | + case INCFS_IOC_FILL_BLOCKS: |
|---|
| 952 | + case INCFS_IOC_READ_FILE_SIGNATURE: |
|---|
| 953 | + case INCFS_IOC_GET_FILLED_BLOCKS: |
|---|
| 954 | + case INCFS_IOC_GET_BLOCK_COUNT: |
|---|
| 955 | + case FS_IOC_ENABLE_VERITY: |
|---|
| 956 | + case FS_IOC_MEASURE_VERITY: |
|---|
| 957 | + case FS_IOC_READ_VERITY_METADATA: |
|---|
| 958 | + break; |
|---|
| 959 | + default: |
|---|
| 960 | + return -ENOIOCTLCMD; |
|---|
| 961 | + } |
|---|
| 962 | + return dispatch_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
|---|
| 963 | +} |
|---|
| 964 | +#endif |
|---|
| 1497 | 965 | |
|---|
| 1498 | 966 | static struct dentry *dir_lookup(struct inode *dir_inode, struct dentry *dentry, |
|---|
| 1499 | 967 | unsigned int flags) |
|---|
| .. | .. |
|---|
| 1503 | 971 | struct dentry *backing_dentry = NULL; |
|---|
| 1504 | 972 | struct path dir_backing_path = {}; |
|---|
| 1505 | 973 | struct inode_info *dir_info = get_incfs_node(dir_inode); |
|---|
| 1506 | | - struct mem_range name_range = |
|---|
| 1507 | | - range((u8 *)dentry->d_name.name, dentry->d_name.len); |
|---|
| 1508 | 974 | int err = 0; |
|---|
| 975 | + |
|---|
| 976 | + if (!mi || !dir_info || !dir_info->n_backing_inode) |
|---|
| 977 | + return ERR_PTR(-EBADF); |
|---|
| 1509 | 978 | |
|---|
| 1510 | 979 | if (d_inode(mi->mi_backing_dir_path.dentry) == |
|---|
| 1511 | 980 | dir_info->n_backing_inode) { |
|---|
| 1512 | 981 | /* We do lookup in the FS root. Show pseudo files. */ |
|---|
| 1513 | | - |
|---|
| 1514 | | - if (incfs_equal_ranges(pending_reads_file_name_range, |
|---|
| 1515 | | - name_range)) { |
|---|
| 1516 | | - struct inode *inode = fetch_pending_reads_inode( |
|---|
| 1517 | | - dir_inode->i_sb); |
|---|
| 1518 | | - |
|---|
| 1519 | | - if (IS_ERR(inode)) { |
|---|
| 1520 | | - err = PTR_ERR(inode); |
|---|
| 1521 | | - goto out; |
|---|
| 1522 | | - } |
|---|
| 1523 | | - |
|---|
| 1524 | | - d_add(dentry, inode); |
|---|
| 982 | + err = dir_lookup_pseudo_files(dir_inode->i_sb, dentry); |
|---|
| 983 | + if (err != -ENOENT) |
|---|
| 1525 | 984 | goto out; |
|---|
| 1526 | | - } |
|---|
| 1527 | | - |
|---|
| 1528 | | - if (incfs_equal_ranges(log_file_name_range, name_range)) { |
|---|
| 1529 | | - struct inode *inode = fetch_log_inode( |
|---|
| 1530 | | - dir_inode->i_sb); |
|---|
| 1531 | | - |
|---|
| 1532 | | - if (IS_ERR(inode)) { |
|---|
| 1533 | | - err = PTR_ERR(inode); |
|---|
| 1534 | | - goto out; |
|---|
| 1535 | | - } |
|---|
| 1536 | | - |
|---|
| 1537 | | - d_add(dentry, inode); |
|---|
| 1538 | | - goto out; |
|---|
| 1539 | | - } |
|---|
| 985 | + err = 0; |
|---|
| 1540 | 986 | } |
|---|
| 1541 | 987 | |
|---|
| 1542 | 988 | dir_dentry = dget_parent(dentry); |
|---|
| .. | .. |
|---|
| 1622 | 1068 | |
|---|
| 1623 | 1069 | if (!backing_dentry) { |
|---|
| 1624 | 1070 | err = -EBADF; |
|---|
| 1625 | | - goto out; |
|---|
| 1071 | + goto path_err; |
|---|
| 1626 | 1072 | } |
|---|
| 1627 | 1073 | |
|---|
| 1628 | 1074 | if (backing_dentry->d_parent == mi->mi_index_dir) { |
|---|
| .. | .. |
|---|
| 1631 | 1077 | goto out; |
|---|
| 1632 | 1078 | } |
|---|
| 1633 | 1079 | |
|---|
| 1080 | + if (backing_dentry->d_parent == mi->mi_incomplete_dir) { |
|---|
| 1081 | + /* Can't create a subdir inside .incomplete */ |
|---|
| 1082 | + err = -EBUSY; |
|---|
| 1083 | + goto out; |
|---|
| 1084 | + } |
|---|
| 1634 | 1085 | inode_lock_nested(dir_node->n_backing_inode, I_MUTEX_PARENT); |
|---|
| 1635 | 1086 | err = vfs_mkdir(dir_node->n_backing_inode, backing_dentry, mode | 0222); |
|---|
| 1636 | 1087 | inode_unlock(dir_node->n_backing_inode); |
|---|
| 1637 | 1088 | if (!err) { |
|---|
| 1638 | 1089 | struct inode *inode = NULL; |
|---|
| 1639 | 1090 | |
|---|
| 1640 | | - if (d_really_is_negative(backing_dentry)) { |
|---|
| 1091 | + if (d_really_is_negative(backing_dentry) || |
|---|
| 1092 | + unlikely(d_unhashed(backing_dentry))) { |
|---|
| 1641 | 1093 | err = -EINVAL; |
|---|
| 1642 | 1094 | goto out; |
|---|
| 1643 | 1095 | } |
|---|
| .. | .. |
|---|
| 1654 | 1106 | if (d_really_is_negative(dentry)) |
|---|
| 1655 | 1107 | d_drop(dentry); |
|---|
| 1656 | 1108 | path_put(&backing_path); |
|---|
| 1109 | + |
|---|
| 1110 | +path_err: |
|---|
| 1657 | 1111 | mutex_unlock(&mi->mi_dir_struct_mutex); |
|---|
| 1658 | 1112 | if (err) |
|---|
| 1659 | 1113 | pr_debug("incfs: %s err:%d\n", __func__, err); |
|---|
| 1660 | 1114 | return err; |
|---|
| 1661 | 1115 | } |
|---|
| 1662 | 1116 | |
|---|
| 1663 | | -/* Delete file referenced by backing_dentry and also its hardlink from .index */ |
|---|
| 1664 | | -static int final_file_delete(struct mount_info *mi, |
|---|
| 1665 | | - struct dentry *backing_dentry) |
|---|
| 1117 | +/* |
|---|
| 1118 | + * Delete file referenced by backing_dentry and if appropriate its hardlink |
|---|
| 1119 | + * from .index and .incomplete |
|---|
| 1120 | + */ |
|---|
| 1121 | +static int file_delete(struct mount_info *mi, struct dentry *dentry, |
|---|
| 1122 | + struct dentry *backing_dentry, int nlink) |
|---|
| 1666 | 1123 | { |
|---|
| 1667 | 1124 | struct dentry *index_file_dentry = NULL; |
|---|
| 1125 | + struct dentry *incomplete_file_dentry = NULL; |
|---|
| 1668 | 1126 | /* 2 chars per byte of file ID + 1 char for \0 */ |
|---|
| 1669 | 1127 | char file_id_str[2 * sizeof(incfs_uuid_t) + 1] = {0}; |
|---|
| 1670 | 1128 | ssize_t uuid_size = 0; |
|---|
| 1671 | 1129 | int error = 0; |
|---|
| 1672 | 1130 | |
|---|
| 1673 | 1131 | WARN_ON(!mutex_is_locked(&mi->mi_dir_struct_mutex)); |
|---|
| 1132 | + |
|---|
| 1133 | + if (nlink > 3) |
|---|
| 1134 | + goto just_unlink; |
|---|
| 1135 | + |
|---|
| 1674 | 1136 | uuid_size = vfs_getxattr(backing_dentry, INCFS_XATTR_ID_NAME, |
|---|
| 1675 | 1137 | file_id_str, 2 * sizeof(incfs_uuid_t)); |
|---|
| 1676 | 1138 | if (uuid_size < 0) { |
|---|
| .. | .. |
|---|
| 1686 | 1148 | index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str); |
|---|
| 1687 | 1149 | if (IS_ERR(index_file_dentry)) { |
|---|
| 1688 | 1150 | error = PTR_ERR(index_file_dentry); |
|---|
| 1151 | + index_file_dentry = NULL; |
|---|
| 1689 | 1152 | goto out; |
|---|
| 1690 | 1153 | } |
|---|
| 1691 | 1154 | |
|---|
| 1692 | | - error = incfs_unlink(backing_dentry); |
|---|
| 1693 | | - if (error) |
|---|
| 1694 | | - goto out; |
|---|
| 1155 | + if (d_really_is_positive(index_file_dentry) && nlink > 0) |
|---|
| 1156 | + nlink--; |
|---|
| 1695 | 1157 | |
|---|
| 1696 | | - if (d_really_is_positive(index_file_dentry)) |
|---|
| 1158 | + if (nlink > 2) |
|---|
| 1159 | + goto just_unlink; |
|---|
| 1160 | + |
|---|
| 1161 | + incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir, |
|---|
| 1162 | + file_id_str); |
|---|
| 1163 | + if (IS_ERR(incomplete_file_dentry)) { |
|---|
| 1164 | + error = PTR_ERR(incomplete_file_dentry); |
|---|
| 1165 | + incomplete_file_dentry = NULL; |
|---|
| 1166 | + goto out; |
|---|
| 1167 | + } |
|---|
| 1168 | + |
|---|
| 1169 | + if (d_really_is_positive(incomplete_file_dentry) && nlink > 0) |
|---|
| 1170 | + nlink--; |
|---|
| 1171 | + |
|---|
| 1172 | + if (nlink > 1) |
|---|
| 1173 | + goto just_unlink; |
|---|
| 1174 | + |
|---|
| 1175 | + if (d_really_is_positive(index_file_dentry)) { |
|---|
| 1697 | 1176 | error = incfs_unlink(index_file_dentry); |
|---|
| 1177 | + if (error) |
|---|
| 1178 | + goto out; |
|---|
| 1179 | + notify_unlink(dentry, file_id_str, INCFS_INDEX_NAME); |
|---|
| 1180 | + } |
|---|
| 1181 | + |
|---|
| 1182 | + if (d_really_is_positive(incomplete_file_dentry)) { |
|---|
| 1183 | + error = incfs_unlink(incomplete_file_dentry); |
|---|
| 1184 | + if (error) |
|---|
| 1185 | + goto out; |
|---|
| 1186 | + notify_unlink(dentry, file_id_str, INCFS_INCOMPLETE_NAME); |
|---|
| 1187 | + } |
|---|
| 1188 | + |
|---|
| 1189 | +just_unlink: |
|---|
| 1190 | + error = incfs_unlink(backing_dentry); |
|---|
| 1191 | + |
|---|
| 1698 | 1192 | out: |
|---|
| 1699 | 1193 | dput(index_file_dentry); |
|---|
| 1194 | + dput(incomplete_file_dentry); |
|---|
| 1700 | 1195 | if (error) |
|---|
| 1701 | 1196 | pr_debug("incfs: delete_file_from_index err:%d\n", error); |
|---|
| 1702 | 1197 | return error; |
|---|
| .. | .. |
|---|
| 1709 | 1204 | struct kstat stat; |
|---|
| 1710 | 1205 | int err = 0; |
|---|
| 1711 | 1206 | |
|---|
| 1207 | + if (!mi) |
|---|
| 1208 | + return -EBADF; |
|---|
| 1209 | + |
|---|
| 1712 | 1210 | err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex); |
|---|
| 1713 | 1211 | if (err) |
|---|
| 1714 | 1212 | return err; |
|---|
| .. | .. |
|---|
| 1716 | 1214 | get_incfs_backing_path(dentry, &backing_path); |
|---|
| 1717 | 1215 | if (!backing_path.dentry) { |
|---|
| 1718 | 1216 | err = -EBADF; |
|---|
| 1719 | | - goto out; |
|---|
| 1217 | + goto path_err; |
|---|
| 1720 | 1218 | } |
|---|
| 1721 | 1219 | |
|---|
| 1722 | 1220 | if (backing_path.dentry->d_parent == mi->mi_index_dir) { |
|---|
| 1723 | 1221 | /* Direct unlink from .index are not allowed. */ |
|---|
| 1222 | + err = -EBUSY; |
|---|
| 1223 | + goto out; |
|---|
| 1224 | + } |
|---|
| 1225 | + |
|---|
| 1226 | + if (backing_path.dentry->d_parent == mi->mi_incomplete_dir) { |
|---|
| 1227 | + /* Direct unlink from .incomplete are not allowed. */ |
|---|
| 1724 | 1228 | err = -EBUSY; |
|---|
| 1725 | 1229 | goto out; |
|---|
| 1726 | 1230 | } |
|---|
| .. | .. |
|---|
| 1730 | 1234 | if (err) |
|---|
| 1731 | 1235 | goto out; |
|---|
| 1732 | 1236 | |
|---|
| 1733 | | - if (stat.nlink == 2) { |
|---|
| 1734 | | - /* |
|---|
| 1735 | | - * This is the last named link to this file. The only one left |
|---|
| 1736 | | - * is in .index. Remove them both now. |
|---|
| 1737 | | - */ |
|---|
| 1738 | | - err = final_file_delete(mi, backing_path.dentry); |
|---|
| 1739 | | - } else { |
|---|
| 1740 | | - /* There are other links to this file. Remove just this one. */ |
|---|
| 1741 | | - err = incfs_unlink(backing_path.dentry); |
|---|
| 1742 | | - } |
|---|
| 1237 | + err = file_delete(mi, dentry, backing_path.dentry, stat.nlink); |
|---|
| 1743 | 1238 | |
|---|
| 1744 | 1239 | d_drop(dentry); |
|---|
| 1745 | 1240 | out: |
|---|
| 1746 | 1241 | path_put(&backing_path); |
|---|
| 1242 | +path_err: |
|---|
| 1747 | 1243 | if (err) |
|---|
| 1748 | 1244 | pr_debug("incfs: %s err:%d\n", __func__, err); |
|---|
| 1749 | 1245 | mutex_unlock(&mi->mi_dir_struct_mutex); |
|---|
| .. | .. |
|---|
| 1758 | 1254 | struct path backing_new_path = {}; |
|---|
| 1759 | 1255 | int error = 0; |
|---|
| 1760 | 1256 | |
|---|
| 1257 | + if (!mi) |
|---|
| 1258 | + return -EBADF; |
|---|
| 1259 | + |
|---|
| 1761 | 1260 | error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex); |
|---|
| 1762 | 1261 | if (error) |
|---|
| 1763 | 1262 | return error; |
|---|
| .. | .. |
|---|
| 1767 | 1266 | |
|---|
| 1768 | 1267 | if (backing_new_path.dentry->d_parent == mi->mi_index_dir) { |
|---|
| 1769 | 1268 | /* Can't link to .index */ |
|---|
| 1269 | + error = -EBUSY; |
|---|
| 1270 | + goto out; |
|---|
| 1271 | + } |
|---|
| 1272 | + |
|---|
| 1273 | + if (backing_new_path.dentry->d_parent == mi->mi_incomplete_dir) { |
|---|
| 1274 | + /* Can't link to .incomplete */ |
|---|
| 1770 | 1275 | error = -EBUSY; |
|---|
| 1771 | 1276 | goto out; |
|---|
| 1772 | 1277 | } |
|---|
| .. | .. |
|---|
| 1804 | 1309 | struct path backing_path = {}; |
|---|
| 1805 | 1310 | int err = 0; |
|---|
| 1806 | 1311 | |
|---|
| 1312 | + if (!mi) |
|---|
| 1313 | + return -EBADF; |
|---|
| 1314 | + |
|---|
| 1807 | 1315 | err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex); |
|---|
| 1808 | 1316 | if (err) |
|---|
| 1809 | 1317 | return err; |
|---|
| .. | .. |
|---|
| 1811 | 1319 | get_incfs_backing_path(dentry, &backing_path); |
|---|
| 1812 | 1320 | if (!backing_path.dentry) { |
|---|
| 1813 | 1321 | err = -EBADF; |
|---|
| 1814 | | - goto out; |
|---|
| 1322 | + goto path_err; |
|---|
| 1815 | 1323 | } |
|---|
| 1816 | 1324 | |
|---|
| 1817 | 1325 | if (backing_path.dentry == mi->mi_index_dir) { |
|---|
| 1818 | 1326 | /* Can't delete .index */ |
|---|
| 1327 | + err = -EBUSY; |
|---|
| 1328 | + goto out; |
|---|
| 1329 | + } |
|---|
| 1330 | + |
|---|
| 1331 | + if (backing_path.dentry == mi->mi_incomplete_dir) { |
|---|
| 1332 | + /* Can't delete .incomplete */ |
|---|
| 1819 | 1333 | err = -EBUSY; |
|---|
| 1820 | 1334 | goto out; |
|---|
| 1821 | 1335 | } |
|---|
| .. | .. |
|---|
| 1825 | 1339 | d_drop(dentry); |
|---|
| 1826 | 1340 | out: |
|---|
| 1827 | 1341 | path_put(&backing_path); |
|---|
| 1342 | + |
|---|
| 1343 | +path_err: |
|---|
| 1828 | 1344 | if (err) |
|---|
| 1829 | 1345 | pr_debug("incfs: %s err:%d\n", __func__, err); |
|---|
| 1830 | 1346 | mutex_unlock(&mi->mi_dir_struct_mutex); |
|---|
| .. | .. |
|---|
| 1843 | 1359 | struct dentry *trap; |
|---|
| 1844 | 1360 | int error = 0; |
|---|
| 1845 | 1361 | |
|---|
| 1846 | | - if (!mi) |
|---|
| 1847 | | - return -EBADF; |
|---|
| 1848 | | - |
|---|
| 1849 | 1362 | error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex); |
|---|
| 1850 | 1363 | if (error) |
|---|
| 1851 | 1364 | return error; |
|---|
| 1852 | 1365 | |
|---|
| 1853 | 1366 | backing_old_dentry = get_incfs_dentry(old_dentry)->backing_path.dentry; |
|---|
| 1367 | + |
|---|
| 1368 | + if (!backing_old_dentry || backing_old_dentry == mi->mi_index_dir || |
|---|
| 1369 | + backing_old_dentry == mi->mi_incomplete_dir) { |
|---|
| 1370 | + /* Renaming .index or .incomplete not allowed */ |
|---|
| 1371 | + error = -EBUSY; |
|---|
| 1372 | + goto exit; |
|---|
| 1373 | + } |
|---|
| 1374 | + |
|---|
| 1854 | 1375 | backing_new_dentry = get_incfs_dentry(new_dentry)->backing_path.dentry; |
|---|
| 1855 | 1376 | dget(backing_old_dentry); |
|---|
| 1856 | 1377 | dget(backing_new_dentry); |
|---|
| .. | .. |
|---|
| 1859 | 1380 | backing_new_dir_dentry = dget_parent(backing_new_dentry); |
|---|
| 1860 | 1381 | target_inode = d_inode(new_dentry); |
|---|
| 1861 | 1382 | |
|---|
| 1862 | | - if (backing_old_dir_dentry == mi->mi_index_dir) { |
|---|
| 1863 | | - /* Direct moves from .index are not allowed. */ |
|---|
| 1383 | + if (backing_old_dir_dentry == mi->mi_index_dir || |
|---|
| 1384 | + backing_old_dir_dentry == mi->mi_incomplete_dir) { |
|---|
| 1385 | + /* Direct moves from .index or .incomplete are not allowed. */ |
|---|
| 1864 | 1386 | error = -EBUSY; |
|---|
| 1865 | 1387 | goto out; |
|---|
| 1866 | 1388 | } |
|---|
| .. | .. |
|---|
| 1897 | 1419 | dput(backing_new_dentry); |
|---|
| 1898 | 1420 | dput(backing_old_dentry); |
|---|
| 1899 | 1421 | |
|---|
| 1422 | +exit: |
|---|
| 1900 | 1423 | mutex_unlock(&mi->mi_dir_struct_mutex); |
|---|
| 1901 | 1424 | if (error) |
|---|
| 1902 | 1425 | pr_debug("incfs: %s err:%d\n", __func__, error); |
|---|
| .. | .. |
|---|
| 1910 | 1433 | struct file *backing_file = NULL; |
|---|
| 1911 | 1434 | struct path backing_path = {}; |
|---|
| 1912 | 1435 | int err = 0; |
|---|
| 1436 | + int flags = O_NOATIME | O_LARGEFILE | |
|---|
| 1437 | + (S_ISDIR(inode->i_mode) ? O_RDONLY : O_RDWR); |
|---|
| 1913 | 1438 | const struct cred *old_cred; |
|---|
| 1914 | 1439 | |
|---|
| 1440 | + WARN_ON(file->private_data); |
|---|
| 1441 | + |
|---|
| 1442 | + if (!mi) |
|---|
| 1443 | + return -EBADF; |
|---|
| 1444 | + |
|---|
| 1915 | 1445 | get_incfs_backing_path(file->f_path.dentry, &backing_path); |
|---|
| 1446 | + if (!backing_path.dentry) |
|---|
| 1447 | + return -EBADF; |
|---|
| 1448 | + |
|---|
| 1916 | 1449 | old_cred = override_creds(mi->mi_owner); |
|---|
| 1917 | | - backing_file = dentry_open(&backing_path, |
|---|
| 1918 | | - O_RDWR | O_NOATIME | O_LARGEFILE, current_cred()); |
|---|
| 1450 | + backing_file = dentry_open(&backing_path, flags, current_cred()); |
|---|
| 1919 | 1451 | revert_creds(old_cred); |
|---|
| 1920 | 1452 | path_put(&backing_path); |
|---|
| 1921 | 1453 | |
|---|
| .. | .. |
|---|
| 1926 | 1458 | } |
|---|
| 1927 | 1459 | |
|---|
| 1928 | 1460 | if (S_ISREG(inode->i_mode)) { |
|---|
| 1461 | + struct incfs_file_data *fd = kzalloc(sizeof(*fd), GFP_NOFS); |
|---|
| 1462 | + |
|---|
| 1463 | + if (!fd) { |
|---|
| 1464 | + err = -ENOMEM; |
|---|
| 1465 | + goto out; |
|---|
| 1466 | + } |
|---|
| 1467 | + |
|---|
| 1468 | + *fd = (struct incfs_file_data) { |
|---|
| 1469 | + .fd_fill_permission = CANT_FILL, |
|---|
| 1470 | + }; |
|---|
| 1471 | + file->private_data = fd; |
|---|
| 1472 | + |
|---|
| 1929 | 1473 | err = make_inode_ready_for_data_ops(mi, inode, backing_file); |
|---|
| 1930 | | - file->private_data = (void *)CANT_FILL; |
|---|
| 1474 | + if (err) |
|---|
| 1475 | + goto out; |
|---|
| 1476 | + |
|---|
| 1477 | + err = incfs_fsverity_file_open(inode, file); |
|---|
| 1478 | + if (err) |
|---|
| 1479 | + goto out; |
|---|
| 1931 | 1480 | } else if (S_ISDIR(inode->i_mode)) { |
|---|
| 1932 | 1481 | struct dir_file *dir = NULL; |
|---|
| 1933 | 1482 | |
|---|
| .. | .. |
|---|
| 1940 | 1489 | err = -EBADF; |
|---|
| 1941 | 1490 | |
|---|
| 1942 | 1491 | out: |
|---|
| 1943 | | - if (err) |
|---|
| 1944 | | - pr_debug("incfs: %s name:%s err: %d\n", __func__, |
|---|
| 1945 | | - file->f_path.dentry->d_name.name, err); |
|---|
| 1492 | + if (err) { |
|---|
| 1493 | + pr_debug("name:%s err: %d\n", |
|---|
| 1494 | + file->f_path.dentry->d_name.name, err); |
|---|
| 1495 | + if (S_ISREG(inode->i_mode)) |
|---|
| 1496 | + kfree(file->private_data); |
|---|
| 1497 | + else if (S_ISDIR(inode->i_mode)) |
|---|
| 1498 | + incfs_free_dir_file(file->private_data); |
|---|
| 1499 | + |
|---|
| 1500 | + file->private_data = NULL; |
|---|
| 1501 | + } |
|---|
| 1502 | + |
|---|
| 1946 | 1503 | if (backing_file) |
|---|
| 1947 | 1504 | fput(backing_file); |
|---|
| 1948 | 1505 | return err; |
|---|
| .. | .. |
|---|
| 1951 | 1508 | static int file_release(struct inode *inode, struct file *file) |
|---|
| 1952 | 1509 | { |
|---|
| 1953 | 1510 | if (S_ISREG(inode->i_mode)) { |
|---|
| 1954 | | - /* Do nothing. |
|---|
| 1955 | | - * data_file is released only by inode eviction. |
|---|
| 1956 | | - */ |
|---|
| 1511 | + kfree(file->private_data); |
|---|
| 1512 | + file->private_data = NULL; |
|---|
| 1957 | 1513 | } else if (S_ISDIR(inode->i_mode)) { |
|---|
| 1958 | 1514 | struct dir_file *dir = get_incfs_dir_file(file); |
|---|
| 1959 | 1515 | |
|---|
| .. | .. |
|---|
| 2056 | 1612 | if (ia->ia_valid & ATTR_SIZE) |
|---|
| 2057 | 1613 | return -EINVAL; |
|---|
| 2058 | 1614 | |
|---|
| 1615 | + if ((ia->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && |
|---|
| 1616 | + (ia->ia_valid & ATTR_MODE)) |
|---|
| 1617 | + return -EINVAL; |
|---|
| 1618 | + |
|---|
| 2059 | 1619 | if (!di) |
|---|
| 2060 | 1620 | return -EINVAL; |
|---|
| 2061 | 1621 | backing_dentry = di->backing_path.dentry; |
|---|
| .. | .. |
|---|
| 2085 | 1645 | return simple_setattr(dentry, ia); |
|---|
| 2086 | 1646 | } |
|---|
| 2087 | 1647 | |
|---|
| 1648 | + |
|---|
| 1649 | +static int incfs_getattr(const struct path *path, |
|---|
| 1650 | + struct kstat *stat, u32 request_mask, |
|---|
| 1651 | + unsigned int query_flags) |
|---|
| 1652 | +{ |
|---|
| 1653 | + struct inode *inode = d_inode(path->dentry); |
|---|
| 1654 | + |
|---|
| 1655 | + generic_fillattr(inode, stat); |
|---|
| 1656 | + |
|---|
| 1657 | + if (inode->i_ino < INCFS_START_INO_RANGE) |
|---|
| 1658 | + return 0; |
|---|
| 1659 | + |
|---|
| 1660 | + stat->attributes &= ~STATX_ATTR_VERITY; |
|---|
| 1661 | + if (IS_VERITY(inode)) |
|---|
| 1662 | + stat->attributes |= STATX_ATTR_VERITY; |
|---|
| 1663 | + stat->attributes_mask |= STATX_ATTR_VERITY; |
|---|
| 1664 | + |
|---|
| 1665 | + if (request_mask & STATX_BLOCKS) { |
|---|
| 1666 | + struct kstat backing_kstat; |
|---|
| 1667 | + struct dentry_info *di = get_incfs_dentry(path->dentry); |
|---|
| 1668 | + int error = 0; |
|---|
| 1669 | + struct path *backing_path; |
|---|
| 1670 | + |
|---|
| 1671 | + if (!di) |
|---|
| 1672 | + return -EFSCORRUPTED; |
|---|
| 1673 | + backing_path = &di->backing_path; |
|---|
| 1674 | + error = vfs_getattr(backing_path, &backing_kstat, STATX_BLOCKS, |
|---|
| 1675 | + AT_STATX_SYNC_AS_STAT); |
|---|
| 1676 | + if (error) |
|---|
| 1677 | + return error; |
|---|
| 1678 | + |
|---|
| 1679 | + stat->blocks = backing_kstat.blocks; |
|---|
| 1680 | + } |
|---|
| 1681 | + |
|---|
| 1682 | + return 0; |
|---|
| 1683 | +} |
|---|
| 1684 | + |
|---|
| 2088 | 1685 | static ssize_t incfs_getxattr(struct dentry *d, const char *name, |
|---|
| 2089 | 1686 | void *value, size_t size) |
|---|
| 2090 | 1687 | { |
|---|
| .. | .. |
|---|
| 2092 | 1689 | struct mount_info *mi = get_mount_info(d->d_sb); |
|---|
| 2093 | 1690 | char *stored_value; |
|---|
| 2094 | 1691 | size_t stored_size; |
|---|
| 2095 | | - |
|---|
| 2096 | | - if (!mi) |
|---|
| 2097 | | - return -EBADF; |
|---|
| 1692 | + int i; |
|---|
| 2098 | 1693 | |
|---|
| 2099 | 1694 | if (di && di->backing_path.dentry) |
|---|
| 2100 | 1695 | return vfs_getxattr(di->backing_path.dentry, name, value, size); |
|---|
| .. | .. |
|---|
| 2102 | 1697 | if (strcmp(name, "security.selinux")) |
|---|
| 2103 | 1698 | return -ENODATA; |
|---|
| 2104 | 1699 | |
|---|
| 2105 | | - if (!strcmp(d->d_iname, INCFS_PENDING_READS_FILENAME)) { |
|---|
| 2106 | | - stored_value = mi->pending_read_xattr; |
|---|
| 2107 | | - stored_size = mi->pending_read_xattr_size; |
|---|
| 2108 | | - } else if (!strcmp(d->d_iname, INCFS_LOG_FILENAME)) { |
|---|
| 2109 | | - stored_value = mi->log_xattr; |
|---|
| 2110 | | - stored_size = mi->log_xattr_size; |
|---|
| 2111 | | - } else { |
|---|
| 1700 | + for (i = 0; i < PSEUDO_FILE_COUNT; ++i) |
|---|
| 1701 | + if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data)) |
|---|
| 1702 | + break; |
|---|
| 1703 | + if (i == PSEUDO_FILE_COUNT) |
|---|
| 2112 | 1704 | return -ENODATA; |
|---|
| 2113 | | - } |
|---|
| 2114 | 1705 | |
|---|
| 1706 | + stored_value = mi->pseudo_file_xattr[i].data; |
|---|
| 1707 | + stored_size = mi->pseudo_file_xattr[i].len; |
|---|
| 2115 | 1708 | if (!stored_value) |
|---|
| 2116 | 1709 | return -ENODATA; |
|---|
| 2117 | 1710 | |
|---|
| .. | .. |
|---|
| 2120 | 1713 | |
|---|
| 2121 | 1714 | memcpy(value, stored_value, stored_size); |
|---|
| 2122 | 1715 | return stored_size; |
|---|
| 2123 | | - |
|---|
| 2124 | 1716 | } |
|---|
| 2125 | 1717 | |
|---|
| 2126 | 1718 | |
|---|
| .. | .. |
|---|
| 2129 | 1721 | { |
|---|
| 2130 | 1722 | struct dentry_info *di = get_incfs_dentry(d); |
|---|
| 2131 | 1723 | struct mount_info *mi = get_mount_info(d->d_sb); |
|---|
| 2132 | | - void **stored_value; |
|---|
| 1724 | + u8 **stored_value; |
|---|
| 2133 | 1725 | size_t *stored_size; |
|---|
| 2134 | | - |
|---|
| 2135 | | - if (!mi) |
|---|
| 2136 | | - return -EBADF; |
|---|
| 1726 | + int i; |
|---|
| 2137 | 1727 | |
|---|
| 2138 | 1728 | if (di && di->backing_path.dentry) |
|---|
| 2139 | 1729 | return vfs_setxattr(di->backing_path.dentry, name, value, size, |
|---|
| .. | .. |
|---|
| 2145 | 1735 | if (size > INCFS_MAX_FILE_ATTR_SIZE) |
|---|
| 2146 | 1736 | return -E2BIG; |
|---|
| 2147 | 1737 | |
|---|
| 2148 | | - if (!strcmp(d->d_iname, INCFS_PENDING_READS_FILENAME)) { |
|---|
| 2149 | | - stored_value = &mi->pending_read_xattr; |
|---|
| 2150 | | - stored_size = &mi->pending_read_xattr_size; |
|---|
| 2151 | | - } else if (!strcmp(d->d_iname, INCFS_LOG_FILENAME)) { |
|---|
| 2152 | | - stored_value = &mi->log_xattr; |
|---|
| 2153 | | - stored_size = &mi->log_xattr_size; |
|---|
| 2154 | | - } else { |
|---|
| 1738 | + for (i = 0; i < PSEUDO_FILE_COUNT; ++i) |
|---|
| 1739 | + if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data)) |
|---|
| 1740 | + break; |
|---|
| 1741 | + if (i == PSEUDO_FILE_COUNT) |
|---|
| 2155 | 1742 | return -ENODATA; |
|---|
| 2156 | | - } |
|---|
| 2157 | 1743 | |
|---|
| 1744 | + stored_value = &mi->pseudo_file_xattr[i].data; |
|---|
| 1745 | + stored_size = &mi->pseudo_file_xattr[i].len; |
|---|
| 2158 | 1746 | kfree (*stored_value); |
|---|
| 2159 | 1747 | *stored_value = kzalloc(size, GFP_NOFS); |
|---|
| 2160 | 1748 | if (!*stored_value) |
|---|
| .. | .. |
|---|
| 2175 | 1763 | return vfs_listxattr(di->backing_path.dentry, list, size); |
|---|
| 2176 | 1764 | } |
|---|
| 2177 | 1765 | |
|---|
| 2178 | | -static int incfs_test_super(struct super_block *s, void *p) |
|---|
| 2179 | | -{ |
|---|
| 2180 | | - return s->s_fs_info != NULL; |
|---|
| 2181 | | -} |
|---|
| 2182 | | - |
|---|
| 2183 | 1766 | struct dentry *incfs_mount_fs(struct file_system_type *type, int flags, |
|---|
| 2184 | 1767 | const char *dev_name, void *data) |
|---|
| 2185 | 1768 | { |
|---|
| 2186 | 1769 | struct mount_options options = {}; |
|---|
| 2187 | 1770 | struct mount_info *mi = NULL; |
|---|
| 2188 | 1771 | struct path backing_dir_path = {}; |
|---|
| 2189 | | - struct dentry *index_dir; |
|---|
| 1772 | + struct dentry *index_dir = NULL; |
|---|
| 1773 | + struct dentry *incomplete_dir = NULL; |
|---|
| 2190 | 1774 | struct super_block *src_fs_sb = NULL; |
|---|
| 2191 | 1775 | struct inode *root_inode = NULL; |
|---|
| 2192 | | - struct super_block *sb = sget(type, incfs_test_super, set_anon_super, |
|---|
| 2193 | | - flags, NULL); |
|---|
| 1776 | + struct super_block *sb = sget(type, NULL, set_anon_super, flags, NULL); |
|---|
| 1777 | + bool dir_created = false; |
|---|
| 2194 | 1778 | int error = 0; |
|---|
| 2195 | 1779 | |
|---|
| 2196 | 1780 | if (IS_ERR(sb)) |
|---|
| .. | .. |
|---|
| 2199 | 1783 | sb->s_op = &incfs_super_ops; |
|---|
| 2200 | 1784 | sb->s_d_op = &incfs_dentry_ops; |
|---|
| 2201 | 1785 | sb->s_flags |= S_NOATIME; |
|---|
| 2202 | | - sb->s_magic = (long)INCFS_MAGIC_NUMBER; |
|---|
| 1786 | + sb->s_magic = INCFS_MAGIC_NUMBER; |
|---|
| 2203 | 1787 | sb->s_time_gran = 1; |
|---|
| 2204 | 1788 | sb->s_blocksize = INCFS_DATA_FILE_BLOCK_SIZE; |
|---|
| 2205 | 1789 | sb->s_blocksize_bits = blksize_bits(sb->s_blocksize); |
|---|
| .. | .. |
|---|
| 2207 | 1791 | |
|---|
| 2208 | 1792 | BUILD_BUG_ON(PAGE_SIZE != INCFS_DATA_FILE_BLOCK_SIZE); |
|---|
| 2209 | 1793 | |
|---|
| 1794 | + if (!dev_name) { |
|---|
| 1795 | + pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n"); |
|---|
| 1796 | + error = -ENOENT; |
|---|
| 1797 | + goto err_deactivate; |
|---|
| 1798 | + } |
|---|
| 1799 | + |
|---|
| 2210 | 1800 | error = parse_options(&options, (char *)data); |
|---|
| 2211 | 1801 | if (error != 0) { |
|---|
| 2212 | 1802 | pr_err("incfs: Options parsing error. %d\n", error); |
|---|
| 2213 | | - goto err; |
|---|
| 1803 | + goto err_deactivate; |
|---|
| 2214 | 1804 | } |
|---|
| 2215 | 1805 | |
|---|
| 2216 | 1806 | sb->s_bdi->ra_pages = options.readahead_pages; |
|---|
| 2217 | 1807 | if (!dev_name) { |
|---|
| 2218 | 1808 | pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n"); |
|---|
| 2219 | 1809 | error = -ENOENT; |
|---|
| 2220 | | - goto err; |
|---|
| 1810 | + goto err_free_opts; |
|---|
| 2221 | 1811 | } |
|---|
| 2222 | 1812 | |
|---|
| 2223 | 1813 | error = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, |
|---|
| .. | .. |
|---|
| 2226 | 1816 | !d_really_is_positive(backing_dir_path.dentry)) { |
|---|
| 2227 | 1817 | pr_err("incfs: Error accessing: %s.\n", |
|---|
| 2228 | 1818 | dev_name); |
|---|
| 2229 | | - goto err; |
|---|
| 1819 | + goto err_free_opts; |
|---|
| 2230 | 1820 | } |
|---|
| 2231 | 1821 | src_fs_sb = backing_dir_path.dentry->d_sb; |
|---|
| 2232 | 1822 | sb->s_maxbytes = src_fs_sb->s_maxbytes; |
|---|
| 1823 | + sb->s_stack_depth = src_fs_sb->s_stack_depth + 1; |
|---|
| 2233 | 1824 | |
|---|
| 2234 | | - if (!sb->s_fs_info) { |
|---|
| 2235 | | - mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path); |
|---|
| 2236 | | - |
|---|
| 2237 | | - if (IS_ERR_OR_NULL(mi)) { |
|---|
| 2238 | | - error = PTR_ERR(mi); |
|---|
| 2239 | | - pr_err("incfs: Error allocating mount info. %d\n", |
|---|
| 2240 | | - error); |
|---|
| 2241 | | - mi = NULL; |
|---|
| 2242 | | - goto err; |
|---|
| 2243 | | - } |
|---|
| 2244 | | - sb->s_fs_info = mi; |
|---|
| 2245 | | - } else { |
|---|
| 2246 | | - mi = sb->s_fs_info; |
|---|
| 1825 | + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
|---|
| 1826 | + error = -EINVAL; |
|---|
| 1827 | + goto err_put_path; |
|---|
| 2247 | 1828 | } |
|---|
| 2248 | 1829 | |
|---|
| 2249 | | - index_dir = open_or_create_index_dir(backing_dir_path.dentry); |
|---|
| 1830 | + mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path); |
|---|
| 1831 | + if (IS_ERR_OR_NULL(mi)) { |
|---|
| 1832 | + error = PTR_ERR(mi); |
|---|
| 1833 | + pr_err("incfs: Error allocating mount info. %d\n", error); |
|---|
| 1834 | + goto err_put_path; |
|---|
| 1835 | + } |
|---|
| 1836 | + |
|---|
| 1837 | + sb->s_fs_info = mi; |
|---|
| 1838 | + mi->mi_backing_dir_path = backing_dir_path; |
|---|
| 1839 | + index_dir = open_or_create_special_dir(backing_dir_path.dentry, |
|---|
| 1840 | + INCFS_INDEX_NAME, &dir_created); |
|---|
| 2250 | 1841 | if (IS_ERR_OR_NULL(index_dir)) { |
|---|
| 2251 | 1842 | error = PTR_ERR(index_dir); |
|---|
| 2252 | 1843 | pr_err("incfs: Can't find or create .index dir in %s\n", |
|---|
| 2253 | 1844 | dev_name); |
|---|
| 2254 | | - goto err; |
|---|
| 1845 | + /* No need to null index_dir since we don't put it */ |
|---|
| 1846 | + goto err_put_path; |
|---|
| 2255 | 1847 | } |
|---|
| 1848 | + |
|---|
| 2256 | 1849 | mi->mi_index_dir = index_dir; |
|---|
| 1850 | + mi->mi_index_free = dir_created; |
|---|
| 1851 | + |
|---|
| 1852 | + incomplete_dir = open_or_create_special_dir(backing_dir_path.dentry, |
|---|
| 1853 | + INCFS_INCOMPLETE_NAME, |
|---|
| 1854 | + &dir_created); |
|---|
| 1855 | + if (IS_ERR_OR_NULL(incomplete_dir)) { |
|---|
| 1856 | + error = PTR_ERR(incomplete_dir); |
|---|
| 1857 | + pr_err("incfs: Can't find or create .incomplete dir in %s\n", |
|---|
| 1858 | + dev_name); |
|---|
| 1859 | + /* No need to null incomplete_dir since we don't put it */ |
|---|
| 1860 | + goto err_put_path; |
|---|
| 1861 | + } |
|---|
| 1862 | + mi->mi_incomplete_dir = incomplete_dir; |
|---|
| 1863 | + mi->mi_incomplete_free = dir_created; |
|---|
| 2257 | 1864 | |
|---|
| 2258 | 1865 | root_inode = fetch_regular_inode(sb, backing_dir_path.dentry); |
|---|
| 2259 | 1866 | if (IS_ERR(root_inode)) { |
|---|
| 2260 | 1867 | error = PTR_ERR(root_inode); |
|---|
| 2261 | | - goto err; |
|---|
| 1868 | + goto err_put_path; |
|---|
| 2262 | 1869 | } |
|---|
| 2263 | 1870 | |
|---|
| 1871 | + sb->s_root = d_make_root(root_inode); |
|---|
| 2264 | 1872 | if (!sb->s_root) { |
|---|
| 2265 | | - sb->s_root = d_make_root(root_inode); |
|---|
| 2266 | | - if (!sb->s_root) { |
|---|
| 2267 | | - error = -ENOMEM; |
|---|
| 2268 | | - goto err; |
|---|
| 2269 | | - } |
|---|
| 2270 | | - error = incfs_init_dentry(sb->s_root, &backing_dir_path); |
|---|
| 2271 | | - if (error) |
|---|
| 2272 | | - goto err; |
|---|
| 1873 | + error = -ENOMEM; |
|---|
| 1874 | + goto err_put_path; |
|---|
| 2273 | 1875 | } |
|---|
| 1876 | + error = incfs_init_dentry(sb->s_root, &backing_dir_path); |
|---|
| 1877 | + if (error) |
|---|
| 1878 | + goto err_put_path; |
|---|
| 2274 | 1879 | |
|---|
| 2275 | | - mi->mi_backing_dir_path = backing_dir_path; |
|---|
| 1880 | + path_put(&backing_dir_path); |
|---|
| 2276 | 1881 | sb->s_flags |= SB_ACTIVE; |
|---|
| 2277 | 1882 | |
|---|
| 2278 | 1883 | pr_debug("incfs: mount\n"); |
|---|
| 2279 | 1884 | return dget(sb->s_root); |
|---|
| 2280 | | -err: |
|---|
| 2281 | | - sb->s_fs_info = NULL; |
|---|
| 1885 | + |
|---|
| 1886 | +err_put_path: |
|---|
| 2282 | 1887 | path_put(&backing_dir_path); |
|---|
| 2283 | | - incfs_free_mount_info(mi); |
|---|
| 1888 | +err_free_opts: |
|---|
| 1889 | + free_options(&options); |
|---|
| 1890 | +err_deactivate: |
|---|
| 2284 | 1891 | deactivate_locked_super(sb); |
|---|
| 1892 | + pr_err("incfs: mount failed %d\n", error); |
|---|
| 2285 | 1893 | return ERR_PTR(error); |
|---|
| 2286 | 1894 | } |
|---|
| 2287 | 1895 | |
|---|
| .. | .. |
|---|
| 2291 | 1899 | struct mount_info *mi = get_mount_info(sb); |
|---|
| 2292 | 1900 | int err = 0; |
|---|
| 2293 | 1901 | |
|---|
| 2294 | | - if (!mi) |
|---|
| 2295 | | - return err; |
|---|
| 2296 | | - |
|---|
| 2297 | 1902 | sync_filesystem(sb); |
|---|
| 2298 | 1903 | err = parse_options(&options, (char *)data); |
|---|
| 2299 | 1904 | if (err) |
|---|
| 2300 | 1905 | return err; |
|---|
| 2301 | 1906 | |
|---|
| 1907 | + if (options.report_uid != mi->mi_options.report_uid) { |
|---|
| 1908 | + pr_err("incfs: Can't change report_uid mount option on remount\n"); |
|---|
| 1909 | + err = -EOPNOTSUPP; |
|---|
| 1910 | + goto out; |
|---|
| 1911 | + } |
|---|
| 1912 | + |
|---|
| 2302 | 1913 | err = incfs_realloc_mount_info(mi, &options); |
|---|
| 2303 | 1914 | if (err) |
|---|
| 2304 | | - return err; |
|---|
| 1915 | + goto out; |
|---|
| 2305 | 1916 | |
|---|
| 2306 | 1917 | pr_debug("incfs: remount\n"); |
|---|
| 2307 | | - return 0; |
|---|
| 1918 | + |
|---|
| 1919 | +out: |
|---|
| 1920 | + free_options(&options); |
|---|
| 1921 | + return err; |
|---|
| 2308 | 1922 | } |
|---|
| 2309 | 1923 | |
|---|
| 2310 | 1924 | void incfs_kill_sb(struct super_block *sb) |
|---|
| 2311 | 1925 | { |
|---|
| 2312 | 1926 | struct mount_info *mi = sb->s_fs_info; |
|---|
| 1927 | + struct inode *dinode = NULL; |
|---|
| 2313 | 1928 | |
|---|
| 2314 | 1929 | pr_debug("incfs: unmount\n"); |
|---|
| 2315 | | - vfs_rmdir(d_inode(mi->mi_backing_dir_path.dentry), mi->mi_index_dir); |
|---|
| 1930 | + |
|---|
| 1931 | + /* |
|---|
| 1932 | + * We must kill the super before freeing mi, since killing the super |
|---|
| 1933 | + * triggers inode eviction, which triggers the final update of the |
|---|
| 1934 | + * backing file, which uses certain information for mi |
|---|
| 1935 | + */ |
|---|
| 2316 | 1936 | kill_anon_super(sb); |
|---|
| 2317 | | - incfs_free_mount_info(mi); |
|---|
| 2318 | | - sb->s_fs_info = NULL; |
|---|
| 1937 | + |
|---|
| 1938 | + if (mi) { |
|---|
| 1939 | + if (mi->mi_backing_dir_path.dentry) |
|---|
| 1940 | + dinode = d_inode(mi->mi_backing_dir_path.dentry); |
|---|
| 1941 | + |
|---|
| 1942 | + if (dinode) { |
|---|
| 1943 | + if (mi->mi_index_dir && mi->mi_index_free) |
|---|
| 1944 | + vfs_rmdir(dinode, mi->mi_index_dir); |
|---|
| 1945 | + |
|---|
| 1946 | + if (mi->mi_incomplete_dir && mi->mi_incomplete_free) |
|---|
| 1947 | + vfs_rmdir(dinode, mi->mi_incomplete_dir); |
|---|
| 1948 | + } |
|---|
| 1949 | + |
|---|
| 1950 | + incfs_free_mount_info(mi); |
|---|
| 1951 | + sb->s_fs_info = NULL; |
|---|
| 1952 | + } |
|---|
| 2319 | 1953 | } |
|---|
| 2320 | 1954 | |
|---|
| 2321 | 1955 | static int show_options(struct seq_file *m, struct dentry *root) |
|---|
| 2322 | 1956 | { |
|---|
| 2323 | 1957 | struct mount_info *mi = get_mount_info(root->d_sb); |
|---|
| 2324 | | - |
|---|
| 2325 | | - if (!mi) |
|---|
| 2326 | | - return -EBADF; |
|---|
| 2327 | 1958 | |
|---|
| 2328 | 1959 | seq_printf(m, ",read_timeout_ms=%u", mi->mi_options.read_timeout_ms); |
|---|
| 2329 | 1960 | seq_printf(m, ",readahead=%u", mi->mi_options.readahead_pages); |
|---|
| .. | .. |
|---|
| 2332 | 1963 | seq_printf(m, ",rlog_wakeup_cnt=%u", |
|---|
| 2333 | 1964 | mi->mi_options.read_log_wakeup_count); |
|---|
| 2334 | 1965 | } |
|---|
| 2335 | | - if (mi->mi_options.no_backing_file_cache) |
|---|
| 2336 | | - seq_puts(m, ",no_bf_cache"); |
|---|
| 2337 | | - if (mi->mi_options.no_backing_file_readahead) |
|---|
| 2338 | | - seq_puts(m, ",no_bf_readahead"); |
|---|
| 1966 | + if (mi->mi_options.report_uid) |
|---|
| 1967 | + seq_puts(m, ",report_uid"); |
|---|
| 1968 | + |
|---|
| 1969 | + if (mi->mi_sysfs_node) |
|---|
| 1970 | + seq_printf(m, ",sysfs_name=%s", |
|---|
| 1971 | + kobject_name(&mi->mi_sysfs_node->isn_sysfs_node)); |
|---|
| 2339 | 1972 | return 0; |
|---|
| 2340 | 1973 | } |
|---|