inode.c 13 KB

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