inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. BUG();
  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_flags |= S_NOATIME|S_NOCMTIME;
  150. inode->i_generation = generation;
  151. inode->i_data.backing_dev_info = &fc->bdi;
  152. fuse_init_inode(inode, attr);
  153. unlock_new_inode(inode);
  154. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  155. BUG_ON(retried);
  156. /* Inode has changed type, any I/O on the old should fail */
  157. make_bad_inode(inode);
  158. iput(inode);
  159. retried = 1;
  160. goto retry;
  161. }
  162. fi = get_fuse_inode(inode);
  163. fi->nlookup ++;
  164. fuse_change_attributes(inode, attr);
  165. return inode;
  166. }
  167. static void fuse_put_super(struct super_block *sb)
  168. {
  169. struct fuse_conn *fc = get_fuse_conn_super(sb);
  170. down_write(&fc->sbput_sem);
  171. while (!list_empty(&fc->background))
  172. fuse_release_background(list_entry(fc->background.next,
  173. struct fuse_req, bg_entry));
  174. spin_lock(&fuse_lock);
  175. fc->mounted = 0;
  176. /* Flush all readers on this fs */
  177. wake_up_all(&fc->waitq);
  178. up_write(&fc->sbput_sem);
  179. fuse_release_conn(fc);
  180. spin_unlock(&fuse_lock);
  181. }
  182. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  183. {
  184. stbuf->f_type = FUSE_SUPER_MAGIC;
  185. stbuf->f_bsize = attr->bsize;
  186. stbuf->f_frsize = attr->frsize;
  187. stbuf->f_blocks = attr->blocks;
  188. stbuf->f_bfree = attr->bfree;
  189. stbuf->f_bavail = attr->bavail;
  190. stbuf->f_files = attr->files;
  191. stbuf->f_ffree = attr->ffree;
  192. stbuf->f_namelen = attr->namelen;
  193. /* fsid is left zero */
  194. }
  195. static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
  196. {
  197. struct fuse_conn *fc = get_fuse_conn_super(sb);
  198. struct fuse_req *req;
  199. struct fuse_statfs_out outarg;
  200. int err;
  201. req = fuse_get_request(fc);
  202. if (!req)
  203. return -EINTR;
  204. memset(&outarg, 0, sizeof(outarg));
  205. req->in.numargs = 0;
  206. req->in.h.opcode = FUSE_STATFS;
  207. req->out.numargs = 1;
  208. req->out.args[0].size =
  209. fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
  210. req->out.args[0].value = &outarg;
  211. request_send(fc, req);
  212. err = req->out.h.error;
  213. if (!err)
  214. convert_fuse_statfs(buf, &outarg.st);
  215. fuse_put_request(fc, req);
  216. return err;
  217. }
  218. enum {
  219. OPT_FD,
  220. OPT_ROOTMODE,
  221. OPT_USER_ID,
  222. OPT_GROUP_ID,
  223. OPT_DEFAULT_PERMISSIONS,
  224. OPT_ALLOW_OTHER,
  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_MAX_READ, "max_read=%u"},
  236. {OPT_ERR, NULL}
  237. };
  238. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
  239. {
  240. char *p;
  241. memset(d, 0, sizeof(struct fuse_mount_data));
  242. d->max_read = ~0;
  243. while ((p = strsep(&opt, ",")) != NULL) {
  244. int token;
  245. int value;
  246. substring_t args[MAX_OPT_ARGS];
  247. if (!*p)
  248. continue;
  249. token = match_token(p, tokens, args);
  250. switch (token) {
  251. case OPT_FD:
  252. if (match_int(&args[0], &value))
  253. return 0;
  254. d->fd = value;
  255. d->fd_present = 1;
  256. break;
  257. case OPT_ROOTMODE:
  258. if (match_octal(&args[0], &value))
  259. return 0;
  260. d->rootmode = value;
  261. d->rootmode_present = 1;
  262. break;
  263. case OPT_USER_ID:
  264. if (match_int(&args[0], &value))
  265. return 0;
  266. d->user_id = value;
  267. d->user_id_present = 1;
  268. break;
  269. case OPT_GROUP_ID:
  270. if (match_int(&args[0], &value))
  271. return 0;
  272. d->group_id = value;
  273. d->group_id_present = 1;
  274. break;
  275. case OPT_DEFAULT_PERMISSIONS:
  276. d->flags |= FUSE_DEFAULT_PERMISSIONS;
  277. break;
  278. case OPT_ALLOW_OTHER:
  279. d->flags |= FUSE_ALLOW_OTHER;
  280. break;
  281. case OPT_MAX_READ:
  282. if (match_int(&args[0], &value))
  283. return 0;
  284. d->max_read = value;
  285. break;
  286. default:
  287. return 0;
  288. }
  289. }
  290. if (!d->fd_present || !d->rootmode_present ||
  291. !d->user_id_present || !d->group_id_present)
  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->max_read != ~0)
  305. seq_printf(m, ",max_read=%u", fc->max_read);
  306. return 0;
  307. }
  308. static void free_conn(struct fuse_conn *fc)
  309. {
  310. while (!list_empty(&fc->unused_list)) {
  311. struct fuse_req *req;
  312. req = list_entry(fc->unused_list.next, struct fuse_req, list);
  313. list_del(&req->list);
  314. fuse_request_free(req);
  315. }
  316. kfree(fc);
  317. }
  318. /* Must be called with the fuse lock held */
  319. void fuse_release_conn(struct fuse_conn *fc)
  320. {
  321. fc->count--;
  322. if (!fc->count)
  323. free_conn(fc);
  324. }
  325. static struct fuse_conn *new_conn(void)
  326. {
  327. struct fuse_conn *fc;
  328. fc = kzalloc(sizeof(*fc), GFP_KERNEL);
  329. if (fc != NULL) {
  330. int i;
  331. init_waitqueue_head(&fc->waitq);
  332. INIT_LIST_HEAD(&fc->pending);
  333. INIT_LIST_HEAD(&fc->processing);
  334. INIT_LIST_HEAD(&fc->io);
  335. INIT_LIST_HEAD(&fc->unused_list);
  336. INIT_LIST_HEAD(&fc->background);
  337. sema_init(&fc->outstanding_sem, 1); /* One for INIT */
  338. init_rwsem(&fc->sbput_sem);
  339. for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
  340. struct fuse_req *req = fuse_request_alloc();
  341. if (!req) {
  342. free_conn(fc);
  343. return NULL;
  344. }
  345. list_add(&req->list, &fc->unused_list);
  346. }
  347. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  348. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  349. fc->reqctr = 0;
  350. }
  351. return fc;
  352. }
  353. static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
  354. {
  355. struct fuse_conn *fc;
  356. if (file->f_op != &fuse_dev_operations)
  357. return ERR_PTR(-EINVAL);
  358. fc = new_conn();
  359. if (fc == NULL)
  360. return ERR_PTR(-ENOMEM);
  361. spin_lock(&fuse_lock);
  362. if (file->private_data) {
  363. free_conn(fc);
  364. fc = ERR_PTR(-EINVAL);
  365. } else {
  366. file->private_data = fc;
  367. sb->s_fs_info = fc;
  368. fc->mounted = 1;
  369. fc->connected = 1;
  370. fc->count = 2;
  371. }
  372. spin_unlock(&fuse_lock);
  373. return fc;
  374. }
  375. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  376. {
  377. struct fuse_attr attr;
  378. memset(&attr, 0, sizeof(attr));
  379. attr.mode = mode;
  380. attr.ino = FUSE_ROOT_ID;
  381. return fuse_iget(sb, 1, 0, &attr);
  382. }
  383. static struct super_operations fuse_super_operations = {
  384. .alloc_inode = fuse_alloc_inode,
  385. .destroy_inode = fuse_destroy_inode,
  386. .read_inode = fuse_read_inode,
  387. .clear_inode = fuse_clear_inode,
  388. .put_super = fuse_put_super,
  389. .statfs = fuse_statfs,
  390. .show_options = fuse_show_options,
  391. };
  392. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  393. {
  394. struct fuse_conn *fc;
  395. struct inode *root;
  396. struct fuse_mount_data d;
  397. struct file *file;
  398. int err;
  399. if (!parse_fuse_opt((char *) data, &d))
  400. return -EINVAL;
  401. sb->s_blocksize = PAGE_CACHE_SIZE;
  402. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  403. sb->s_magic = FUSE_SUPER_MAGIC;
  404. sb->s_op = &fuse_super_operations;
  405. sb->s_maxbytes = MAX_LFS_FILESIZE;
  406. file = fget(d.fd);
  407. if (!file)
  408. return -EINVAL;
  409. fc = get_conn(file, sb);
  410. fput(file);
  411. if (IS_ERR(fc))
  412. return PTR_ERR(fc);
  413. fc->flags = d.flags;
  414. fc->user_id = d.user_id;
  415. fc->group_id = d.group_id;
  416. fc->max_read = d.max_read;
  417. if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
  418. fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  419. err = -ENOMEM;
  420. root = get_root_inode(sb, d.rootmode);
  421. if (root == NULL)
  422. goto err;
  423. sb->s_root = d_alloc_root(root);
  424. if (!sb->s_root) {
  425. iput(root);
  426. goto err;
  427. }
  428. fuse_send_init(fc);
  429. return 0;
  430. err:
  431. spin_lock(&fuse_lock);
  432. fuse_release_conn(fc);
  433. spin_unlock(&fuse_lock);
  434. return err;
  435. }
  436. static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
  437. int flags, const char *dev_name,
  438. void *raw_data)
  439. {
  440. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
  441. }
  442. static struct file_system_type fuse_fs_type = {
  443. .owner = THIS_MODULE,
  444. .name = "fuse",
  445. .get_sb = fuse_get_sb,
  446. .kill_sb = kill_anon_super,
  447. };
  448. static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
  449. unsigned long flags)
  450. {
  451. struct inode * inode = foo;
  452. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  453. SLAB_CTOR_CONSTRUCTOR)
  454. inode_init_once(inode);
  455. }
  456. static int __init fuse_fs_init(void)
  457. {
  458. int err;
  459. err = register_filesystem(&fuse_fs_type);
  460. if (err)
  461. printk("fuse: failed to register filesystem\n");
  462. else {
  463. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  464. sizeof(struct fuse_inode),
  465. 0, SLAB_HWCACHE_ALIGN,
  466. fuse_inode_init_once, NULL);
  467. if (!fuse_inode_cachep) {
  468. unregister_filesystem(&fuse_fs_type);
  469. err = -ENOMEM;
  470. }
  471. }
  472. return err;
  473. }
  474. static void fuse_fs_cleanup(void)
  475. {
  476. unregister_filesystem(&fuse_fs_type);
  477. kmem_cache_destroy(fuse_inode_cachep);
  478. }
  479. static int __init fuse_init(void)
  480. {
  481. int res;
  482. printk("fuse init (API version %i.%i)\n",
  483. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  484. spin_lock_init(&fuse_lock);
  485. res = fuse_fs_init();
  486. if (res)
  487. goto err;
  488. res = fuse_dev_init();
  489. if (res)
  490. goto err_fs_cleanup;
  491. return 0;
  492. err_fs_cleanup:
  493. fuse_fs_cleanup();
  494. err:
  495. return res;
  496. }
  497. static void __exit fuse_exit(void)
  498. {
  499. printk(KERN_DEBUG "fuse exit\n");
  500. fuse_fs_cleanup();
  501. fuse_dev_cleanup();
  502. }
  503. module_init(fuse_init);
  504. module_exit(fuse_exit);