backing.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FUSE passthrough to backing file.
  4. *
  5. * Copyright (c) 2023 CTERA Networks.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/file.h>
  9. struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
  10. {
  11. if (fb && refcount_inc_not_zero(&fb->count))
  12. return fb;
  13. return NULL;
  14. }
  15. static void fuse_backing_free(struct fuse_backing *fb)
  16. {
  17. pr_debug("%s: fb=0x%p\n", __func__, fb);
  18. if (fb->file)
  19. fput(fb->file);
  20. put_cred(fb->cred);
  21. kfree_rcu(fb, rcu);
  22. }
  23. void fuse_backing_put(struct fuse_backing *fb)
  24. {
  25. if (fb && refcount_dec_and_test(&fb->count))
  26. fuse_backing_free(fb);
  27. }
  28. void fuse_backing_files_init(struct fuse_conn *fc)
  29. {
  30. idr_init(&fc->backing_files_map);
  31. }
  32. static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
  33. {
  34. int id;
  35. idr_preload(GFP_KERNEL);
  36. spin_lock(&fc->lock);
  37. /* FIXME: xarray might be space inefficient */
  38. id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
  39. spin_unlock(&fc->lock);
  40. idr_preload_end();
  41. WARN_ON_ONCE(id == 0);
  42. return id;
  43. }
  44. static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
  45. int id)
  46. {
  47. struct fuse_backing *fb;
  48. spin_lock(&fc->lock);
  49. fb = idr_remove(&fc->backing_files_map, id);
  50. spin_unlock(&fc->lock);
  51. return fb;
  52. }
  53. static int fuse_backing_id_free(int id, void *p, void *data)
  54. {
  55. struct fuse_backing *fb = p;
  56. WARN_ON_ONCE(refcount_read(&fb->count) != 1);
  57. fuse_backing_free(fb);
  58. return 0;
  59. }
  60. void fuse_backing_files_free(struct fuse_conn *fc)
  61. {
  62. idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
  63. idr_destroy(&fc->backing_files_map);
  64. }
  65. int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
  66. {
  67. struct file *file;
  68. struct super_block *backing_sb;
  69. struct fuse_backing *fb = NULL;
  70. int res;
  71. pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
  72. /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
  73. res = -EPERM;
  74. if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
  75. goto out;
  76. res = -EINVAL;
  77. if (map->flags || map->padding)
  78. goto out;
  79. file = fget_raw(map->fd);
  80. res = -EBADF;
  81. if (!file)
  82. goto out;
  83. /* read/write/splice/mmap passthrough only relevant for regular files */
  84. res = d_is_dir(file->f_path.dentry) ? -EISDIR : -EINVAL;
  85. if (!d_is_reg(file->f_path.dentry))
  86. goto out_fput;
  87. backing_sb = file_inode(file)->i_sb;
  88. res = -ELOOP;
  89. if (backing_sb->s_stack_depth >= fc->max_stack_depth)
  90. goto out_fput;
  91. fb = kmalloc_obj(struct fuse_backing);
  92. res = -ENOMEM;
  93. if (!fb)
  94. goto out_fput;
  95. fb->file = file;
  96. fb->cred = prepare_creds();
  97. refcount_set(&fb->count, 1);
  98. res = fuse_backing_id_alloc(fc, fb);
  99. if (res < 0) {
  100. fuse_backing_free(fb);
  101. fb = NULL;
  102. }
  103. out:
  104. pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
  105. return res;
  106. out_fput:
  107. fput(file);
  108. goto out;
  109. }
  110. int fuse_backing_close(struct fuse_conn *fc, int backing_id)
  111. {
  112. struct fuse_backing *fb = NULL;
  113. int err;
  114. pr_debug("%s: backing_id=%d\n", __func__, backing_id);
  115. /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
  116. err = -EPERM;
  117. if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
  118. goto out;
  119. err = -EINVAL;
  120. if (backing_id <= 0)
  121. goto out;
  122. err = -ENOENT;
  123. fb = fuse_backing_id_remove(fc, backing_id);
  124. if (!fb)
  125. goto out;
  126. fuse_backing_put(fb);
  127. err = 0;
  128. out:
  129. pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
  130. return err;
  131. }
  132. struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id)
  133. {
  134. struct fuse_backing *fb;
  135. rcu_read_lock();
  136. fb = idr_find(&fc->backing_files_map, backing_id);
  137. fb = fuse_backing_get(fb);
  138. rcu_read_unlock();
  139. return fb;
  140. }