inode.c 13 KB

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