^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) FUSE: Filesystem in Userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) This program can be distributed under the terms of the GNU GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) See the file COPYING.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "fuse_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define FUSE_CTL_SUPER_MAGIC 0x65735543
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * This is non-NULL when the single instance of the control filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * exists. Protected by fuse_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct super_block *fuse_control_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct fuse_conn *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) mutex_lock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) fc = file_inode(file)->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) fc = fuse_conn_get(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) mutex_unlock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (fc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (fc->abort_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) fc->aborted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) fuse_abort_conn(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) char tmp[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!*ppos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) value = atomic_read(&fc->num_waiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) file->private_data = (void *)value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) size = sprintf(tmp, "%ld\n", (long)file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return simple_read_from_buffer(buf, len, ppos, tmp, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) size_t len, loff_t *ppos, unsigned val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) char tmp[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) size_t size = sprintf(tmp, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return simple_read_from_buffer(buf, len, ppos, tmp, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) size_t count, loff_t *ppos, unsigned *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned global_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned long t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned limit = (1 << 16) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (*ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = kstrtoul_from_user(buf, count, 0, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) limit = min(limit, global_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (t > limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *val = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static ssize_t fuse_conn_max_background_read(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) char __user *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct fuse_conn *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fc = fuse_ctl_file_conn_get(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) val = READ_ONCE(fc->max_background);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return fuse_conn_limit_read(file, buf, len, ppos, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static ssize_t fuse_conn_max_background_write(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) max_user_bgreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (fc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) spin_lock(&fc->bg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) fc->max_background = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) fc->blocked = fc->num_background >= fc->max_background;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!fc->blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) wake_up(&fc->blocked_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_unlock(&fc->bg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) char __user *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct fuse_conn *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) unsigned val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fc = fuse_ctl_file_conn_get(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) val = READ_ONCE(fc->congestion_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return fuse_conn_limit_read(file, buf, len, ppos, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct fuse_conn *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct fuse_mount *fm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) max_user_congthresh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) fc = fuse_ctl_file_conn_get(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (!fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) down_read(&fc->killsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) spin_lock(&fc->bg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) fc->congestion_threshold = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * Get any fuse_mount belonging to this fuse_conn; s_bdi is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * shared between all of them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!list_empty(&fc->mounts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) fm = list_first_entry(&fc->mounts, struct fuse_mount, fc_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (fc->num_background < fc->congestion_threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) clear_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) clear_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) set_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) set_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) spin_unlock(&fc->bg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) up_read(&fc->killsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static const struct file_operations fuse_ctl_abort_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .open = nonseekable_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .write = fuse_conn_abort_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static const struct file_operations fuse_ctl_waiting_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .open = nonseekable_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .read = fuse_conn_waiting_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static const struct file_operations fuse_conn_max_background_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .open = nonseekable_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .read = fuse_conn_max_background_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .write = fuse_conn_max_background_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static const struct file_operations fuse_conn_congestion_threshold_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) .open = nonseekable_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .read = fuse_conn_congestion_threshold_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) .write = fuse_conn_congestion_threshold_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct fuse_conn *fc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int mode, int nlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) const struct inode_operations *iop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) const struct file_operations *fop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) dentry = d_alloc_name(parent, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) inode = new_inode(fuse_control_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) inode->i_ino = get_next_ino();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) inode->i_uid = fc->user_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) inode->i_gid = fc->group_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* setting ->i_op to NULL is not allowed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (iop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) inode->i_op = iop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) inode->i_fop = fop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) set_nlink(inode, nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) inode->i_private = fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) d_add(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) fc->ctl_dentry[fc->ctl_ndents++] = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * Add a connection to the control filesystem (if it exists). Caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * must hold fuse_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int fuse_ctl_add_conn(struct fuse_conn *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) char name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!fuse_control_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) parent = fuse_control_sb->s_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) inc_nlink(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) sprintf(name, "%u", fc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) &simple_dir_inode_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) &simple_dir_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) NULL, &fuse_ctl_waiting_ops) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) NULL, &fuse_ctl_abort_ops) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 1, NULL, &fuse_conn_max_background_ops) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) S_IFREG | 0600, 1, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) &fuse_conn_congestion_threshold_ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) fuse_ctl_remove_conn(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * Remove a connection from the control filesystem (if it exists).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Caller must hold fuse_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) void fuse_ctl_remove_conn(struct fuse_conn *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!fuse_control_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) for (i = fc->ctl_ndents - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct dentry *dentry = fc->ctl_dentry[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) d_inode(dentry)->i_private = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* Get rid of submounts: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) d_invalidate(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) drop_nlink(d_inode(fuse_control_sb->s_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static const struct tree_descr empty_descr = {""};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct fuse_conn *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) mutex_lock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) BUG_ON(fuse_control_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) fuse_control_sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) list_for_each_entry(fc, &fuse_conn_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) err = fuse_ctl_add_conn(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) fuse_control_sb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) mutex_unlock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) mutex_unlock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static int fuse_ctl_get_tree(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return get_tree_single(fc, fuse_ctl_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static const struct fs_context_operations fuse_ctl_context_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) .get_tree = fuse_ctl_get_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int fuse_ctl_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) fc->ops = &fuse_ctl_context_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static void fuse_ctl_kill_sb(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct fuse_conn *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) mutex_lock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) fuse_control_sb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) list_for_each_entry(fc, &fuse_conn_list, entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) fc->ctl_ndents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) mutex_unlock(&fuse_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) kill_litter_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static struct file_system_type fuse_ctl_fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) .name = "fusectl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .init_fs_context = fuse_ctl_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .kill_sb = fuse_ctl_kill_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) MODULE_ALIAS_FS("fusectl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) int __init fuse_ctl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return register_filesystem(&fuse_ctl_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) void __exit fuse_ctl_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) unregister_filesystem(&fuse_ctl_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }