inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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_blocks = attr->blocks;
  194. stbuf->f_bfree = attr->bfree;
  195. stbuf->f_bavail = attr->bavail;
  196. stbuf->f_files = attr->files;
  197. stbuf->f_ffree = attr->ffree;
  198. stbuf->f_namelen = attr->namelen;
  199. /* fsid is left zero */
  200. }
  201. static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
  202. {
  203. struct fuse_conn *fc = get_fuse_conn_super(sb);
  204. struct fuse_req *req;
  205. struct fuse_statfs_out outarg;
  206. int err;
  207. req = fuse_get_request(fc);
  208. if (!req)
  209. return -EINTR;
  210. req->in.numargs = 0;
  211. req->in.h.opcode = FUSE_STATFS;
  212. req->out.numargs = 1;
  213. req->out.args[0].size = sizeof(outarg);
  214. req->out.args[0].value = &outarg;
  215. request_send(fc, req);
  216. err = req->out.h.error;
  217. if (!err)
  218. convert_fuse_statfs(buf, &outarg.st);
  219. fuse_put_request(fc, req);
  220. return err;
  221. }
  222. enum {
  223. OPT_FD,
  224. OPT_ROOTMODE,
  225. OPT_USER_ID,
  226. OPT_GROUP_ID,
  227. OPT_DEFAULT_PERMISSIONS,
  228. OPT_ALLOW_OTHER,
  229. OPT_MAX_READ,
  230. OPT_ERR
  231. };
  232. static match_table_t tokens = {
  233. {OPT_FD, "fd=%u"},
  234. {OPT_ROOTMODE, "rootmode=%o"},
  235. {OPT_USER_ID, "user_id=%u"},
  236. {OPT_GROUP_ID, "group_id=%u"},
  237. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  238. {OPT_ALLOW_OTHER, "allow_other"},
  239. {OPT_MAX_READ, "max_read=%u"},
  240. {OPT_ERR, NULL}
  241. };
  242. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
  243. {
  244. char *p;
  245. memset(d, 0, sizeof(struct fuse_mount_data));
  246. d->max_read = ~0;
  247. while ((p = strsep(&opt, ",")) != NULL) {
  248. int token;
  249. int value;
  250. substring_t args[MAX_OPT_ARGS];
  251. if (!*p)
  252. continue;
  253. token = match_token(p, tokens, args);
  254. switch (token) {
  255. case OPT_FD:
  256. if (match_int(&args[0], &value))
  257. return 0;
  258. d->fd = value;
  259. d->fd_present = 1;
  260. break;
  261. case OPT_ROOTMODE:
  262. if (match_octal(&args[0], &value))
  263. return 0;
  264. d->rootmode = value;
  265. d->rootmode_present = 1;
  266. break;
  267. case OPT_USER_ID:
  268. if (match_int(&args[0], &value))
  269. return 0;
  270. d->user_id = value;
  271. d->user_id_present = 1;
  272. break;
  273. case OPT_GROUP_ID:
  274. if (match_int(&args[0], &value))
  275. return 0;
  276. d->group_id = value;
  277. d->group_id_present = 1;
  278. break;
  279. case OPT_DEFAULT_PERMISSIONS:
  280. d->flags |= FUSE_DEFAULT_PERMISSIONS;
  281. break;
  282. case OPT_ALLOW_OTHER:
  283. d->flags |= FUSE_ALLOW_OTHER;
  284. break;
  285. case OPT_MAX_READ:
  286. if (match_int(&args[0], &value))
  287. return 0;
  288. d->max_read = value;
  289. break;
  290. default:
  291. return 0;
  292. }
  293. }
  294. if (!d->fd_present || !d->rootmode_present ||
  295. !d->user_id_present || !d->group_id_present)
  296. return 0;
  297. return 1;
  298. }
  299. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  300. {
  301. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  302. seq_printf(m, ",user_id=%u", fc->user_id);
  303. seq_printf(m, ",group_id=%u", fc->group_id);
  304. if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
  305. seq_puts(m, ",default_permissions");
  306. if (fc->flags & FUSE_ALLOW_OTHER)
  307. seq_puts(m, ",allow_other");
  308. if (fc->max_read != ~0)
  309. seq_printf(m, ",max_read=%u", fc->max_read);
  310. return 0;
  311. }
  312. static void free_conn(struct fuse_conn *fc)
  313. {
  314. while (!list_empty(&fc->unused_list)) {
  315. struct fuse_req *req;
  316. req = list_entry(fc->unused_list.next, struct fuse_req, list);
  317. list_del(&req->list);
  318. fuse_request_free(req);
  319. }
  320. kfree(fc);
  321. }
  322. /* Must be called with the fuse lock held */
  323. void fuse_release_conn(struct fuse_conn *fc)
  324. {
  325. fc->count--;
  326. if (!fc->count)
  327. free_conn(fc);
  328. }
  329. static struct fuse_conn *new_conn(void)
  330. {
  331. struct fuse_conn *fc;
  332. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  333. if (fc != NULL) {
  334. int i;
  335. memset(fc, 0, sizeof(*fc));
  336. init_waitqueue_head(&fc->waitq);
  337. INIT_LIST_HEAD(&fc->pending);
  338. INIT_LIST_HEAD(&fc->processing);
  339. INIT_LIST_HEAD(&fc->unused_list);
  340. INIT_LIST_HEAD(&fc->background);
  341. sema_init(&fc->outstanding_sem, 0);
  342. init_rwsem(&fc->sbput_sem);
  343. for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
  344. struct fuse_req *req = fuse_request_alloc();
  345. if (!req) {
  346. free_conn(fc);
  347. return NULL;
  348. }
  349. list_add(&req->list, &fc->unused_list);
  350. }
  351. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  352. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  353. fc->reqctr = 0;
  354. }
  355. return fc;
  356. }
  357. static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
  358. {
  359. struct fuse_conn *fc;
  360. if (file->f_op != &fuse_dev_operations)
  361. return ERR_PTR(-EINVAL);
  362. fc = new_conn();
  363. if (fc == NULL)
  364. return ERR_PTR(-ENOMEM);
  365. spin_lock(&fuse_lock);
  366. if (file->private_data) {
  367. free_conn(fc);
  368. fc = ERR_PTR(-EINVAL);
  369. } else {
  370. file->private_data = fc;
  371. *get_fuse_conn_super_p(sb) = fc;
  372. fc->mounted = 1;
  373. fc->connected = 1;
  374. fc->count = 2;
  375. }
  376. spin_unlock(&fuse_lock);
  377. return fc;
  378. }
  379. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  380. {
  381. struct fuse_attr attr;
  382. memset(&attr, 0, sizeof(attr));
  383. attr.mode = mode;
  384. attr.ino = FUSE_ROOT_ID;
  385. return fuse_iget(sb, 1, 0, &attr);
  386. }
  387. static struct super_operations fuse_super_operations = {
  388. .alloc_inode = fuse_alloc_inode,
  389. .destroy_inode = fuse_destroy_inode,
  390. .read_inode = fuse_read_inode,
  391. .clear_inode = fuse_clear_inode,
  392. .put_super = fuse_put_super,
  393. .statfs = fuse_statfs,
  394. .show_options = fuse_show_options,
  395. };
  396. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  397. {
  398. struct fuse_conn *fc;
  399. struct inode *root;
  400. struct fuse_mount_data d;
  401. struct file *file;
  402. int err;
  403. if (!parse_fuse_opt((char *) data, &d))
  404. return -EINVAL;
  405. sb->s_blocksize = PAGE_CACHE_SIZE;
  406. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  407. sb->s_magic = FUSE_SUPER_MAGIC;
  408. sb->s_op = &fuse_super_operations;
  409. sb->s_maxbytes = MAX_LFS_FILESIZE;
  410. file = fget(d.fd);
  411. if (!file)
  412. return -EINVAL;
  413. fc = get_conn(file, sb);
  414. fput(file);
  415. if (IS_ERR(fc))
  416. return PTR_ERR(fc);
  417. fc->flags = d.flags;
  418. fc->user_id = d.user_id;
  419. fc->group_id = d.group_id;
  420. fc->max_read = d.max_read;
  421. if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
  422. fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  423. fc->max_write = FUSE_MAX_IN / 2;
  424. err = -ENOMEM;
  425. root = get_root_inode(sb, d.rootmode);
  426. if (root == NULL)
  427. goto err;
  428. sb->s_root = d_alloc_root(root);
  429. if (!sb->s_root) {
  430. iput(root);
  431. goto err;
  432. }
  433. fuse_send_init(fc);
  434. return 0;
  435. err:
  436. spin_lock(&fuse_lock);
  437. fuse_release_conn(fc);
  438. spin_unlock(&fuse_lock);
  439. return err;
  440. }
  441. static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
  442. int flags, const char *dev_name,
  443. void *raw_data)
  444. {
  445. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
  446. }
  447. static struct file_system_type fuse_fs_type = {
  448. .owner = THIS_MODULE,
  449. .name = "fuse",
  450. .get_sb = fuse_get_sb,
  451. .kill_sb = kill_anon_super,
  452. };
  453. static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
  454. unsigned long flags)
  455. {
  456. struct inode * inode = foo;
  457. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  458. SLAB_CTOR_CONSTRUCTOR)
  459. inode_init_once(inode);
  460. }
  461. static int __init fuse_fs_init(void)
  462. {
  463. int err;
  464. err = register_filesystem(&fuse_fs_type);
  465. if (err)
  466. printk("fuse: failed to register filesystem\n");
  467. else {
  468. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  469. sizeof(struct fuse_inode),
  470. 0, SLAB_HWCACHE_ALIGN,
  471. fuse_inode_init_once, NULL);
  472. if (!fuse_inode_cachep) {
  473. unregister_filesystem(&fuse_fs_type);
  474. err = -ENOMEM;
  475. }
  476. }
  477. return err;
  478. }
  479. static void fuse_fs_cleanup(void)
  480. {
  481. unregister_filesystem(&fuse_fs_type);
  482. kmem_cache_destroy(fuse_inode_cachep);
  483. }
  484. static int __init fuse_init(void)
  485. {
  486. int res;
  487. printk("fuse init (API version %i.%i)\n",
  488. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  489. spin_lock_init(&fuse_lock);
  490. res = fuse_fs_init();
  491. if (res)
  492. goto err;
  493. res = fuse_dev_init();
  494. if (res)
  495. goto err_fs_cleanup;
  496. return 0;
  497. err_fs_cleanup:
  498. fuse_fs_cleanup();
  499. err:
  500. return res;
  501. }
  502. static void __exit fuse_exit(void)
  503. {
  504. printk(KERN_DEBUG "fuse exit\n");
  505. fuse_fs_cleanup();
  506. fuse_dev_cleanup();
  507. }
  508. module_init(fuse_init);
  509. module_exit(fuse_exit);