inode.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/pagemap.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/mount.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/parser.h>
  17. #include <linux/statfs.h>
  18. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  19. MODULE_DESCRIPTION("Filesystem in Userspace");
  20. MODULE_LICENSE("GPL");
  21. spinlock_t fuse_lock;
  22. static kmem_cache_t *fuse_inode_cachep;
  23. static int mount_count;
  24. static int mount_max = 1000;
  25. module_param(mount_max, int, 0644);
  26. MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)");
  27. #define FUSE_SUPER_MAGIC 0x65735546
  28. struct fuse_mount_data {
  29. int fd;
  30. unsigned rootmode;
  31. unsigned user_id;
  32. };
  33. static struct inode *fuse_alloc_inode(struct super_block *sb)
  34. {
  35. struct inode *inode;
  36. struct fuse_inode *fi;
  37. inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
  38. if (!inode)
  39. return NULL;
  40. fi = get_fuse_inode(inode);
  41. fi->i_time = jiffies - 1;
  42. fi->nodeid = 0;
  43. return inode;
  44. }
  45. static void fuse_destroy_inode(struct inode *inode)
  46. {
  47. kmem_cache_free(fuse_inode_cachep, inode);
  48. }
  49. static void fuse_read_inode(struct inode *inode)
  50. {
  51. /* No op */
  52. }
  53. static void fuse_clear_inode(struct inode *inode)
  54. {
  55. }
  56. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
  57. {
  58. if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
  59. invalidate_inode_pages(inode->i_mapping);
  60. inode->i_ino = attr->ino;
  61. inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
  62. inode->i_nlink = attr->nlink;
  63. inode->i_uid = attr->uid;
  64. inode->i_gid = attr->gid;
  65. i_size_write(inode, attr->size);
  66. inode->i_blksize = PAGE_CACHE_SIZE;
  67. inode->i_blocks = attr->blocks;
  68. inode->i_atime.tv_sec = attr->atime;
  69. inode->i_atime.tv_nsec = attr->atimensec;
  70. inode->i_mtime.tv_sec = attr->mtime;
  71. inode->i_mtime.tv_nsec = attr->mtimensec;
  72. inode->i_ctime.tv_sec = attr->ctime;
  73. inode->i_ctime.tv_nsec = attr->ctimensec;
  74. }
  75. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
  76. {
  77. inode->i_mode = attr->mode & S_IFMT;
  78. i_size_write(inode, attr->size);
  79. }
  80. static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  81. {
  82. unsigned long nodeid = *(unsigned long *) _nodeidp;
  83. if (get_node_id(inode) == nodeid)
  84. return 1;
  85. else
  86. return 0;
  87. }
  88. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  89. {
  90. unsigned long nodeid = *(unsigned long *) _nodeidp;
  91. get_fuse_inode(inode)->nodeid = nodeid;
  92. return 0;
  93. }
  94. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  95. int generation, struct fuse_attr *attr, int version)
  96. {
  97. struct inode *inode;
  98. struct fuse_conn *fc = get_fuse_conn_super(sb);
  99. int retried = 0;
  100. retry:
  101. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  102. if (!inode)
  103. return NULL;
  104. if ((inode->i_state & I_NEW)) {
  105. inode->i_generation = generation;
  106. inode->i_data.backing_dev_info = &fc->bdi;
  107. fuse_init_inode(inode, attr);
  108. unlock_new_inode(inode);
  109. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  110. BUG_ON(retried);
  111. /* Inode has changed type, any I/O on the old should fail */
  112. make_bad_inode(inode);
  113. iput(inode);
  114. retried = 1;
  115. goto retry;
  116. }
  117. fuse_change_attributes(inode, attr);
  118. inode->i_version = version;
  119. return inode;
  120. }
  121. static void fuse_put_super(struct super_block *sb)
  122. {
  123. struct fuse_conn *fc = get_fuse_conn_super(sb);
  124. spin_lock(&fuse_lock);
  125. mount_count --;
  126. fc->sb = NULL;
  127. fc->user_id = 0;
  128. fuse_release_conn(fc);
  129. *get_fuse_conn_super_p(sb) = NULL;
  130. spin_unlock(&fuse_lock);
  131. }
  132. enum {
  133. OPT_FD,
  134. OPT_ROOTMODE,
  135. OPT_USER_ID,
  136. OPT_DEFAULT_PERMISSIONS,
  137. OPT_ALLOW_OTHER,
  138. OPT_ALLOW_ROOT,
  139. OPT_KERNEL_CACHE,
  140. OPT_ERR
  141. };
  142. static match_table_t tokens = {
  143. {OPT_FD, "fd=%u"},
  144. {OPT_ROOTMODE, "rootmode=%o"},
  145. {OPT_USER_ID, "user_id=%u"},
  146. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  147. {OPT_ALLOW_OTHER, "allow_other"},
  148. {OPT_ALLOW_ROOT, "allow_root"},
  149. {OPT_KERNEL_CACHE, "kernel_cache"},
  150. {OPT_ERR, NULL}
  151. };
  152. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
  153. {
  154. char *p;
  155. memset(d, 0, sizeof(struct fuse_mount_data));
  156. d->fd = -1;
  157. while ((p = strsep(&opt, ",")) != NULL) {
  158. int token;
  159. int value;
  160. substring_t args[MAX_OPT_ARGS];
  161. if (!*p)
  162. continue;
  163. token = match_token(p, tokens, args);
  164. switch (token) {
  165. case OPT_FD:
  166. if (match_int(&args[0], &value))
  167. return 0;
  168. d->fd = value;
  169. break;
  170. case OPT_ROOTMODE:
  171. if (match_octal(&args[0], &value))
  172. return 0;
  173. d->rootmode = value;
  174. break;
  175. case OPT_USER_ID:
  176. if (match_int(&args[0], &value))
  177. return 0;
  178. d->user_id = value;
  179. break;
  180. default:
  181. return 0;
  182. }
  183. }
  184. if (d->fd == -1)
  185. return 0;
  186. return 1;
  187. }
  188. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  189. {
  190. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  191. seq_printf(m, ",user_id=%u", fc->user_id);
  192. return 0;
  193. }
  194. void fuse_release_conn(struct fuse_conn *fc)
  195. {
  196. kfree(fc);
  197. }
  198. static struct fuse_conn *new_conn(void)
  199. {
  200. struct fuse_conn *fc;
  201. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  202. if (fc != NULL) {
  203. memset(fc, 0, sizeof(*fc));
  204. fc->sb = NULL;
  205. fc->user_id = 0;
  206. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  207. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  208. }
  209. return fc;
  210. }
  211. static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
  212. {
  213. struct fuse_conn *fc;
  214. fc = new_conn();
  215. if (fc == NULL)
  216. return NULL;
  217. spin_lock(&fuse_lock);
  218. fc->sb = sb;
  219. spin_unlock(&fuse_lock);
  220. return fc;
  221. }
  222. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  223. {
  224. struct fuse_attr attr;
  225. memset(&attr, 0, sizeof(attr));
  226. attr.mode = mode;
  227. attr.ino = FUSE_ROOT_ID;
  228. return fuse_iget(sb, 1, 0, &attr, 0);
  229. }
  230. static struct super_operations fuse_super_operations = {
  231. .alloc_inode = fuse_alloc_inode,
  232. .destroy_inode = fuse_destroy_inode,
  233. .read_inode = fuse_read_inode,
  234. .clear_inode = fuse_clear_inode,
  235. .put_super = fuse_put_super,
  236. .show_options = fuse_show_options,
  237. };
  238. static int inc_mount_count(void)
  239. {
  240. int success = 0;
  241. spin_lock(&fuse_lock);
  242. mount_count ++;
  243. if (mount_max == -1 || mount_count <= mount_max)
  244. success = 1;
  245. spin_unlock(&fuse_lock);
  246. return success;
  247. }
  248. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  249. {
  250. struct fuse_conn *fc;
  251. struct inode *root;
  252. struct fuse_mount_data d;
  253. struct file *file;
  254. int err;
  255. if (!parse_fuse_opt((char *) data, &d))
  256. return -EINVAL;
  257. sb->s_blocksize = PAGE_CACHE_SIZE;
  258. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  259. sb->s_magic = FUSE_SUPER_MAGIC;
  260. sb->s_op = &fuse_super_operations;
  261. sb->s_maxbytes = MAX_LFS_FILESIZE;
  262. file = fget(d.fd);
  263. if (!file)
  264. return -EINVAL;
  265. fc = get_conn(file, sb);
  266. fput(file);
  267. if (fc == NULL)
  268. return -EINVAL;
  269. fc->user_id = d.user_id;
  270. *get_fuse_conn_super_p(sb) = fc;
  271. err = -ENFILE;
  272. if (!inc_mount_count() && current->uid != 0)
  273. goto err;
  274. err = -ENOMEM;
  275. root = get_root_inode(sb, d.rootmode);
  276. if (root == NULL)
  277. goto err;
  278. sb->s_root = d_alloc_root(root);
  279. if (!sb->s_root) {
  280. iput(root);
  281. goto err;
  282. }
  283. return 0;
  284. err:
  285. spin_lock(&fuse_lock);
  286. mount_count --;
  287. fc->sb = NULL;
  288. fuse_release_conn(fc);
  289. spin_unlock(&fuse_lock);
  290. *get_fuse_conn_super_p(sb) = NULL;
  291. return err;
  292. }
  293. static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
  294. int flags, const char *dev_name,
  295. void *raw_data)
  296. {
  297. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
  298. }
  299. static struct file_system_type fuse_fs_type = {
  300. .owner = THIS_MODULE,
  301. .name = "fuse",
  302. .get_sb = fuse_get_sb,
  303. .kill_sb = kill_anon_super,
  304. };
  305. static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
  306. unsigned long flags)
  307. {
  308. struct inode * inode = foo;
  309. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  310. SLAB_CTOR_CONSTRUCTOR)
  311. inode_init_once(inode);
  312. }
  313. static int __init fuse_fs_init(void)
  314. {
  315. int err;
  316. err = register_filesystem(&fuse_fs_type);
  317. if (err)
  318. printk("fuse: failed to register filesystem\n");
  319. else {
  320. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  321. sizeof(struct fuse_inode),
  322. 0, SLAB_HWCACHE_ALIGN,
  323. fuse_inode_init_once, NULL);
  324. if (!fuse_inode_cachep) {
  325. unregister_filesystem(&fuse_fs_type);
  326. err = -ENOMEM;
  327. }
  328. }
  329. return err;
  330. }
  331. static void fuse_fs_cleanup(void)
  332. {
  333. unregister_filesystem(&fuse_fs_type);
  334. kmem_cache_destroy(fuse_inode_cachep);
  335. }
  336. static int __init fuse_init(void)
  337. {
  338. int res;
  339. printk("fuse init (API version %i.%i)\n",
  340. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  341. spin_lock_init(&fuse_lock);
  342. res = fuse_fs_init();
  343. if (res)
  344. goto err;
  345. return 0;
  346. err:
  347. return res;
  348. }
  349. static void __exit fuse_exit(void)
  350. {
  351. printk(KERN_DEBUG "fuse exit\n");
  352. fuse_fs_cleanup();
  353. }
  354. module_init(fuse_init);
  355. module_exit(fuse_exit);