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