inode.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. /* Flush all readers on this fs */
  129. wake_up_all(&fc->waitq);
  130. fuse_release_conn(fc);
  131. *get_fuse_conn_super_p(sb) = NULL;
  132. spin_unlock(&fuse_lock);
  133. }
  134. enum {
  135. OPT_FD,
  136. OPT_ROOTMODE,
  137. OPT_USER_ID,
  138. OPT_DEFAULT_PERMISSIONS,
  139. OPT_ALLOW_OTHER,
  140. OPT_ALLOW_ROOT,
  141. OPT_KERNEL_CACHE,
  142. OPT_ERR
  143. };
  144. static match_table_t tokens = {
  145. {OPT_FD, "fd=%u"},
  146. {OPT_ROOTMODE, "rootmode=%o"},
  147. {OPT_USER_ID, "user_id=%u"},
  148. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  149. {OPT_ALLOW_OTHER, "allow_other"},
  150. {OPT_ALLOW_ROOT, "allow_root"},
  151. {OPT_KERNEL_CACHE, "kernel_cache"},
  152. {OPT_ERR, NULL}
  153. };
  154. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
  155. {
  156. char *p;
  157. memset(d, 0, sizeof(struct fuse_mount_data));
  158. d->fd = -1;
  159. while ((p = strsep(&opt, ",")) != NULL) {
  160. int token;
  161. int value;
  162. substring_t args[MAX_OPT_ARGS];
  163. if (!*p)
  164. continue;
  165. token = match_token(p, tokens, args);
  166. switch (token) {
  167. case OPT_FD:
  168. if (match_int(&args[0], &value))
  169. return 0;
  170. d->fd = value;
  171. break;
  172. case OPT_ROOTMODE:
  173. if (match_octal(&args[0], &value))
  174. return 0;
  175. d->rootmode = value;
  176. break;
  177. case OPT_USER_ID:
  178. if (match_int(&args[0], &value))
  179. return 0;
  180. d->user_id = value;
  181. break;
  182. default:
  183. return 0;
  184. }
  185. }
  186. if (d->fd == -1)
  187. return 0;
  188. return 1;
  189. }
  190. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  191. {
  192. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  193. seq_printf(m, ",user_id=%u", fc->user_id);
  194. return 0;
  195. }
  196. static void free_conn(struct fuse_conn *fc)
  197. {
  198. while (!list_empty(&fc->unused_list)) {
  199. struct fuse_req *req;
  200. req = list_entry(fc->unused_list.next, struct fuse_req, list);
  201. list_del(&req->list);
  202. fuse_request_free(req);
  203. }
  204. kfree(fc);
  205. }
  206. /* Must be called with the fuse lock held */
  207. void fuse_release_conn(struct fuse_conn *fc)
  208. {
  209. if (!fc->sb && !fc->file)
  210. free_conn(fc);
  211. }
  212. static struct fuse_conn *new_conn(void)
  213. {
  214. struct fuse_conn *fc;
  215. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  216. if (fc != NULL) {
  217. int i;
  218. memset(fc, 0, sizeof(*fc));
  219. fc->sb = NULL;
  220. fc->file = NULL;
  221. fc->user_id = 0;
  222. init_waitqueue_head(&fc->waitq);
  223. INIT_LIST_HEAD(&fc->pending);
  224. INIT_LIST_HEAD(&fc->processing);
  225. INIT_LIST_HEAD(&fc->unused_list);
  226. sema_init(&fc->outstanding_sem, 0);
  227. for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
  228. struct fuse_req *req = fuse_request_alloc();
  229. if (!req) {
  230. free_conn(fc);
  231. return NULL;
  232. }
  233. list_add(&req->list, &fc->unused_list);
  234. }
  235. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  236. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  237. fc->reqctr = 0;
  238. }
  239. return fc;
  240. }
  241. static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
  242. {
  243. struct fuse_conn *fc;
  244. if (file->f_op != &fuse_dev_operations)
  245. return ERR_PTR(-EINVAL);
  246. fc = new_conn();
  247. if (fc == NULL)
  248. return ERR_PTR(-ENOMEM);
  249. spin_lock(&fuse_lock);
  250. if (file->private_data) {
  251. free_conn(fc);
  252. fc = ERR_PTR(-EINVAL);
  253. } else {
  254. file->private_data = fc;
  255. fc->sb = sb;
  256. fc->file = file;
  257. }
  258. spin_unlock(&fuse_lock);
  259. return fc;
  260. }
  261. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  262. {
  263. struct fuse_attr attr;
  264. memset(&attr, 0, sizeof(attr));
  265. attr.mode = mode;
  266. attr.ino = FUSE_ROOT_ID;
  267. return fuse_iget(sb, 1, 0, &attr, 0);
  268. }
  269. static struct super_operations fuse_super_operations = {
  270. .alloc_inode = fuse_alloc_inode,
  271. .destroy_inode = fuse_destroy_inode,
  272. .read_inode = fuse_read_inode,
  273. .clear_inode = fuse_clear_inode,
  274. .put_super = fuse_put_super,
  275. .show_options = fuse_show_options,
  276. };
  277. static int inc_mount_count(void)
  278. {
  279. int success = 0;
  280. spin_lock(&fuse_lock);
  281. mount_count ++;
  282. if (mount_max == -1 || mount_count <= mount_max)
  283. success = 1;
  284. spin_unlock(&fuse_lock);
  285. return success;
  286. }
  287. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  288. {
  289. struct fuse_conn *fc;
  290. struct inode *root;
  291. struct fuse_mount_data d;
  292. struct file *file;
  293. int err;
  294. if (!parse_fuse_opt((char *) data, &d))
  295. return -EINVAL;
  296. sb->s_blocksize = PAGE_CACHE_SIZE;
  297. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  298. sb->s_magic = FUSE_SUPER_MAGIC;
  299. sb->s_op = &fuse_super_operations;
  300. sb->s_maxbytes = MAX_LFS_FILESIZE;
  301. file = fget(d.fd);
  302. if (!file)
  303. return -EINVAL;
  304. fc = get_conn(file, sb);
  305. fput(file);
  306. if (IS_ERR(fc))
  307. return PTR_ERR(fc);
  308. fc->user_id = d.user_id;
  309. *get_fuse_conn_super_p(sb) = fc;
  310. err = -ENFILE;
  311. if (!inc_mount_count() && current->uid != 0)
  312. goto err;
  313. err = -ENOMEM;
  314. root = get_root_inode(sb, d.rootmode);
  315. if (root == NULL)
  316. goto err;
  317. sb->s_root = d_alloc_root(root);
  318. if (!sb->s_root) {
  319. iput(root);
  320. goto err;
  321. }
  322. fuse_send_init(fc);
  323. return 0;
  324. err:
  325. spin_lock(&fuse_lock);
  326. mount_count --;
  327. fc->sb = NULL;
  328. fuse_release_conn(fc);
  329. spin_unlock(&fuse_lock);
  330. *get_fuse_conn_super_p(sb) = NULL;
  331. return err;
  332. }
  333. static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
  334. int flags, const char *dev_name,
  335. void *raw_data)
  336. {
  337. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
  338. }
  339. static struct file_system_type fuse_fs_type = {
  340. .owner = THIS_MODULE,
  341. .name = "fuse",
  342. .get_sb = fuse_get_sb,
  343. .kill_sb = kill_anon_super,
  344. };
  345. static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
  346. unsigned long flags)
  347. {
  348. struct inode * inode = foo;
  349. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  350. SLAB_CTOR_CONSTRUCTOR)
  351. inode_init_once(inode);
  352. }
  353. static int __init fuse_fs_init(void)
  354. {
  355. int err;
  356. err = register_filesystem(&fuse_fs_type);
  357. if (err)
  358. printk("fuse: failed to register filesystem\n");
  359. else {
  360. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  361. sizeof(struct fuse_inode),
  362. 0, SLAB_HWCACHE_ALIGN,
  363. fuse_inode_init_once, NULL);
  364. if (!fuse_inode_cachep) {
  365. unregister_filesystem(&fuse_fs_type);
  366. err = -ENOMEM;
  367. }
  368. }
  369. return err;
  370. }
  371. static void fuse_fs_cleanup(void)
  372. {
  373. unregister_filesystem(&fuse_fs_type);
  374. kmem_cache_destroy(fuse_inode_cachep);
  375. }
  376. static int __init fuse_init(void)
  377. {
  378. int res;
  379. printk("fuse init (API version %i.%i)\n",
  380. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  381. spin_lock_init(&fuse_lock);
  382. res = fuse_fs_init();
  383. if (res)
  384. goto err;
  385. res = fuse_dev_init();
  386. if (res)
  387. goto err_fs_cleanup;
  388. return 0;
  389. err_fs_cleanup:
  390. fuse_fs_cleanup();
  391. err:
  392. return res;
  393. }
  394. static void __exit fuse_exit(void)
  395. {
  396. printk(KERN_DEBUG "fuse exit\n");
  397. fuse_fs_cleanup();
  398. fuse_dev_cleanup();
  399. }
  400. module_init(fuse_init);
  401. module_exit(fuse_exit);