inode.c 13 KB

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