inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. fi->forget_req = fuse_request_alloc();
  44. if (!fi->forget_req) {
  45. kmem_cache_free(fuse_inode_cachep, inode);
  46. return NULL;
  47. }
  48. return inode;
  49. }
  50. static void fuse_destroy_inode(struct inode *inode)
  51. {
  52. struct fuse_inode *fi = get_fuse_inode(inode);
  53. if (fi->forget_req)
  54. fuse_request_free(fi->forget_req);
  55. kmem_cache_free(fuse_inode_cachep, inode);
  56. }
  57. static void fuse_read_inode(struct inode *inode)
  58. {
  59. /* No op */
  60. }
  61. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  62. unsigned long nodeid, int version)
  63. {
  64. struct fuse_forget_in *inarg = &req->misc.forget_in;
  65. inarg->version = version;
  66. req->in.h.opcode = FUSE_FORGET;
  67. req->in.h.nodeid = nodeid;
  68. req->in.numargs = 1;
  69. req->in.args[0].size = sizeof(struct fuse_forget_in);
  70. req->in.args[0].value = inarg;
  71. request_send_noreply(fc, req);
  72. }
  73. static void fuse_clear_inode(struct inode *inode)
  74. {
  75. struct fuse_conn *fc = get_fuse_conn(inode);
  76. if (fc) {
  77. struct fuse_inode *fi = get_fuse_inode(inode);
  78. fuse_send_forget(fc, fi->forget_req, fi->nodeid, inode->i_version);
  79. fi->forget_req = NULL;
  80. }
  81. }
  82. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
  83. {
  84. if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
  85. invalidate_inode_pages(inode->i_mapping);
  86. inode->i_ino = attr->ino;
  87. inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
  88. inode->i_nlink = attr->nlink;
  89. inode->i_uid = attr->uid;
  90. inode->i_gid = attr->gid;
  91. i_size_write(inode, attr->size);
  92. inode->i_blksize = PAGE_CACHE_SIZE;
  93. inode->i_blocks = attr->blocks;
  94. inode->i_atime.tv_sec = attr->atime;
  95. inode->i_atime.tv_nsec = attr->atimensec;
  96. inode->i_mtime.tv_sec = attr->mtime;
  97. inode->i_mtime.tv_nsec = attr->mtimensec;
  98. inode->i_ctime.tv_sec = attr->ctime;
  99. inode->i_ctime.tv_nsec = attr->ctimensec;
  100. }
  101. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
  102. {
  103. inode->i_mode = attr->mode & S_IFMT;
  104. i_size_write(inode, attr->size);
  105. if (S_ISREG(inode->i_mode)) {
  106. fuse_init_common(inode);
  107. } else if (S_ISDIR(inode->i_mode))
  108. fuse_init_dir(inode);
  109. else if (S_ISLNK(inode->i_mode))
  110. fuse_init_symlink(inode);
  111. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  112. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  113. fuse_init_common(inode);
  114. init_special_inode(inode, inode->i_mode,
  115. new_decode_dev(attr->rdev));
  116. } else {
  117. /* Don't let user create weird files */
  118. inode->i_mode = S_IFREG;
  119. fuse_init_common(inode);
  120. }
  121. }
  122. static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  123. {
  124. unsigned long nodeid = *(unsigned long *) _nodeidp;
  125. if (get_node_id(inode) == nodeid)
  126. return 1;
  127. else
  128. return 0;
  129. }
  130. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  131. {
  132. unsigned long nodeid = *(unsigned long *) _nodeidp;
  133. get_fuse_inode(inode)->nodeid = nodeid;
  134. return 0;
  135. }
  136. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  137. int generation, struct fuse_attr *attr, int version)
  138. {
  139. struct inode *inode;
  140. struct fuse_conn *fc = get_fuse_conn_super(sb);
  141. int retried = 0;
  142. retry:
  143. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  144. if (!inode)
  145. return NULL;
  146. if ((inode->i_state & I_NEW)) {
  147. inode->i_generation = generation;
  148. inode->i_data.backing_dev_info = &fc->bdi;
  149. fuse_init_inode(inode, attr);
  150. unlock_new_inode(inode);
  151. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  152. BUG_ON(retried);
  153. /* Inode has changed type, any I/O on the old should fail */
  154. make_bad_inode(inode);
  155. iput(inode);
  156. retried = 1;
  157. goto retry;
  158. }
  159. fuse_change_attributes(inode, attr);
  160. inode->i_version = version;
  161. return inode;
  162. }
  163. static void fuse_put_super(struct super_block *sb)
  164. {
  165. struct fuse_conn *fc = get_fuse_conn_super(sb);
  166. spin_lock(&fuse_lock);
  167. mount_count --;
  168. fc->sb = NULL;
  169. fc->user_id = 0;
  170. /* Flush all readers on this fs */
  171. wake_up_all(&fc->waitq);
  172. fuse_release_conn(fc);
  173. *get_fuse_conn_super_p(sb) = NULL;
  174. spin_unlock(&fuse_lock);
  175. }
  176. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  177. {
  178. stbuf->f_type = FUSE_SUPER_MAGIC;
  179. stbuf->f_bsize = attr->bsize;
  180. stbuf->f_blocks = attr->blocks;
  181. stbuf->f_bfree = attr->bfree;
  182. stbuf->f_bavail = attr->bavail;
  183. stbuf->f_files = attr->files;
  184. stbuf->f_ffree = attr->ffree;
  185. stbuf->f_namelen = attr->namelen;
  186. /* fsid is left zero */
  187. }
  188. static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
  189. {
  190. struct fuse_conn *fc = get_fuse_conn_super(sb);
  191. struct fuse_req *req;
  192. struct fuse_statfs_out outarg;
  193. int err;
  194. req = fuse_get_request(fc);
  195. if (!req)
  196. return -ERESTARTSYS;
  197. req->in.numargs = 0;
  198. req->in.h.opcode = FUSE_STATFS;
  199. req->out.numargs = 1;
  200. req->out.args[0].size = sizeof(outarg);
  201. req->out.args[0].value = &outarg;
  202. request_send(fc, req);
  203. err = req->out.h.error;
  204. if (!err)
  205. convert_fuse_statfs(buf, &outarg.st);
  206. fuse_put_request(fc, req);
  207. return err;
  208. }
  209. enum {
  210. OPT_FD,
  211. OPT_ROOTMODE,
  212. OPT_USER_ID,
  213. OPT_DEFAULT_PERMISSIONS,
  214. OPT_ALLOW_OTHER,
  215. OPT_ALLOW_ROOT,
  216. OPT_KERNEL_CACHE,
  217. OPT_ERR
  218. };
  219. static match_table_t tokens = {
  220. {OPT_FD, "fd=%u"},
  221. {OPT_ROOTMODE, "rootmode=%o"},
  222. {OPT_USER_ID, "user_id=%u"},
  223. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  224. {OPT_ALLOW_OTHER, "allow_other"},
  225. {OPT_ALLOW_ROOT, "allow_root"},
  226. {OPT_KERNEL_CACHE, "kernel_cache"},
  227. {OPT_ERR, NULL}
  228. };
  229. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
  230. {
  231. char *p;
  232. memset(d, 0, sizeof(struct fuse_mount_data));
  233. d->fd = -1;
  234. while ((p = strsep(&opt, ",")) != NULL) {
  235. int token;
  236. int value;
  237. substring_t args[MAX_OPT_ARGS];
  238. if (!*p)
  239. continue;
  240. token = match_token(p, tokens, args);
  241. switch (token) {
  242. case OPT_FD:
  243. if (match_int(&args[0], &value))
  244. return 0;
  245. d->fd = value;
  246. break;
  247. case OPT_ROOTMODE:
  248. if (match_octal(&args[0], &value))
  249. return 0;
  250. d->rootmode = value;
  251. break;
  252. case OPT_USER_ID:
  253. if (match_int(&args[0], &value))
  254. return 0;
  255. d->user_id = value;
  256. break;
  257. default:
  258. return 0;
  259. }
  260. }
  261. if (d->fd == -1)
  262. return 0;
  263. return 1;
  264. }
  265. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  266. {
  267. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  268. seq_printf(m, ",user_id=%u", fc->user_id);
  269. return 0;
  270. }
  271. static void free_conn(struct fuse_conn *fc)
  272. {
  273. while (!list_empty(&fc->unused_list)) {
  274. struct fuse_req *req;
  275. req = list_entry(fc->unused_list.next, struct fuse_req, list);
  276. list_del(&req->list);
  277. fuse_request_free(req);
  278. }
  279. kfree(fc);
  280. }
  281. /* Must be called with the fuse lock held */
  282. void fuse_release_conn(struct fuse_conn *fc)
  283. {
  284. if (!fc->sb && !fc->file)
  285. free_conn(fc);
  286. }
  287. static struct fuse_conn *new_conn(void)
  288. {
  289. struct fuse_conn *fc;
  290. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  291. if (fc != NULL) {
  292. int i;
  293. memset(fc, 0, sizeof(*fc));
  294. fc->sb = NULL;
  295. fc->file = NULL;
  296. fc->user_id = 0;
  297. init_waitqueue_head(&fc->waitq);
  298. INIT_LIST_HEAD(&fc->pending);
  299. INIT_LIST_HEAD(&fc->processing);
  300. INIT_LIST_HEAD(&fc->unused_list);
  301. sema_init(&fc->outstanding_sem, 0);
  302. for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
  303. struct fuse_req *req = fuse_request_alloc();
  304. if (!req) {
  305. free_conn(fc);
  306. return NULL;
  307. }
  308. list_add(&req->list, &fc->unused_list);
  309. }
  310. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  311. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  312. fc->reqctr = 0;
  313. }
  314. return fc;
  315. }
  316. static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
  317. {
  318. struct fuse_conn *fc;
  319. if (file->f_op != &fuse_dev_operations)
  320. return ERR_PTR(-EINVAL);
  321. fc = new_conn();
  322. if (fc == NULL)
  323. return ERR_PTR(-ENOMEM);
  324. spin_lock(&fuse_lock);
  325. if (file->private_data) {
  326. free_conn(fc);
  327. fc = ERR_PTR(-EINVAL);
  328. } else {
  329. file->private_data = fc;
  330. fc->sb = sb;
  331. fc->file = file;
  332. }
  333. spin_unlock(&fuse_lock);
  334. return fc;
  335. }
  336. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  337. {
  338. struct fuse_attr attr;
  339. memset(&attr, 0, sizeof(attr));
  340. attr.mode = mode;
  341. attr.ino = FUSE_ROOT_ID;
  342. return fuse_iget(sb, 1, 0, &attr, 0);
  343. }
  344. static struct super_operations fuse_super_operations = {
  345. .alloc_inode = fuse_alloc_inode,
  346. .destroy_inode = fuse_destroy_inode,
  347. .read_inode = fuse_read_inode,
  348. .clear_inode = fuse_clear_inode,
  349. .put_super = fuse_put_super,
  350. .statfs = fuse_statfs,
  351. .show_options = fuse_show_options,
  352. };
  353. static int inc_mount_count(void)
  354. {
  355. int success = 0;
  356. spin_lock(&fuse_lock);
  357. mount_count ++;
  358. if (mount_max == -1 || mount_count <= mount_max)
  359. success = 1;
  360. spin_unlock(&fuse_lock);
  361. return success;
  362. }
  363. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  364. {
  365. struct fuse_conn *fc;
  366. struct inode *root;
  367. struct fuse_mount_data d;
  368. struct file *file;
  369. int err;
  370. if (!parse_fuse_opt((char *) data, &d))
  371. return -EINVAL;
  372. sb->s_blocksize = PAGE_CACHE_SIZE;
  373. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  374. sb->s_magic = FUSE_SUPER_MAGIC;
  375. sb->s_op = &fuse_super_operations;
  376. sb->s_maxbytes = MAX_LFS_FILESIZE;
  377. file = fget(d.fd);
  378. if (!file)
  379. return -EINVAL;
  380. fc = get_conn(file, sb);
  381. fput(file);
  382. if (IS_ERR(fc))
  383. return PTR_ERR(fc);
  384. fc->user_id = d.user_id;
  385. *get_fuse_conn_super_p(sb) = fc;
  386. err = -ENFILE;
  387. if (!inc_mount_count() && current->uid != 0)
  388. goto err;
  389. err = -ENOMEM;
  390. root = get_root_inode(sb, d.rootmode);
  391. if (root == NULL)
  392. goto err;
  393. sb->s_root = d_alloc_root(root);
  394. if (!sb->s_root) {
  395. iput(root);
  396. goto err;
  397. }
  398. fuse_send_init(fc);
  399. return 0;
  400. err:
  401. spin_lock(&fuse_lock);
  402. mount_count --;
  403. fc->sb = NULL;
  404. fuse_release_conn(fc);
  405. spin_unlock(&fuse_lock);
  406. *get_fuse_conn_super_p(sb) = NULL;
  407. return err;
  408. }
  409. static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
  410. int flags, const char *dev_name,
  411. void *raw_data)
  412. {
  413. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
  414. }
  415. static struct file_system_type fuse_fs_type = {
  416. .owner = THIS_MODULE,
  417. .name = "fuse",
  418. .get_sb = fuse_get_sb,
  419. .kill_sb = kill_anon_super,
  420. };
  421. static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
  422. unsigned long flags)
  423. {
  424. struct inode * inode = foo;
  425. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  426. SLAB_CTOR_CONSTRUCTOR)
  427. inode_init_once(inode);
  428. }
  429. static int __init fuse_fs_init(void)
  430. {
  431. int err;
  432. err = register_filesystem(&fuse_fs_type);
  433. if (err)
  434. printk("fuse: failed to register filesystem\n");
  435. else {
  436. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  437. sizeof(struct fuse_inode),
  438. 0, SLAB_HWCACHE_ALIGN,
  439. fuse_inode_init_once, NULL);
  440. if (!fuse_inode_cachep) {
  441. unregister_filesystem(&fuse_fs_type);
  442. err = -ENOMEM;
  443. }
  444. }
  445. return err;
  446. }
  447. static void fuse_fs_cleanup(void)
  448. {
  449. unregister_filesystem(&fuse_fs_type);
  450. kmem_cache_destroy(fuse_inode_cachep);
  451. }
  452. static int __init fuse_init(void)
  453. {
  454. int res;
  455. printk("fuse init (API version %i.%i)\n",
  456. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  457. spin_lock_init(&fuse_lock);
  458. res = fuse_fs_init();
  459. if (res)
  460. goto err;
  461. res = fuse_dev_init();
  462. if (res)
  463. goto err_fs_cleanup;
  464. return 0;
  465. err_fs_cleanup:
  466. fuse_fs_cleanup();
  467. err:
  468. return res;
  469. }
  470. static void __exit fuse_exit(void)
  471. {
  472. printk(KERN_DEBUG "fuse exit\n");
  473. fuse_fs_cleanup();
  474. fuse_dev_cleanup();
  475. }
  476. module_init(fuse_init);
  477. module_exit(fuse_exit);