inode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 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/seq_file.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/parser.h>
  15. #include <linux/statfs.h>
  16. #include <linux/random.h>
  17. #include <linux/sched.h>
  18. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  19. MODULE_DESCRIPTION("Filesystem in Userspace");
  20. MODULE_LICENSE("GPL");
  21. static struct kmem_cache *fuse_inode_cachep;
  22. struct list_head fuse_conn_list;
  23. DEFINE_MUTEX(fuse_mutex);
  24. #define FUSE_SUPER_MAGIC 0x65735546
  25. struct fuse_mount_data {
  26. int fd;
  27. unsigned rootmode;
  28. unsigned user_id;
  29. unsigned group_id;
  30. unsigned fd_present : 1;
  31. unsigned rootmode_present : 1;
  32. unsigned user_id_present : 1;
  33. unsigned group_id_present : 1;
  34. unsigned flags;
  35. unsigned max_read;
  36. unsigned blksize;
  37. };
  38. static struct inode *fuse_alloc_inode(struct super_block *sb)
  39. {
  40. struct inode *inode;
  41. struct fuse_inode *fi;
  42. inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
  43. if (!inode)
  44. return NULL;
  45. fi = get_fuse_inode(inode);
  46. fi->i_time = 0;
  47. fi->nodeid = 0;
  48. fi->nlookup = 0;
  49. fi->forget_req = fuse_request_alloc();
  50. if (!fi->forget_req) {
  51. kmem_cache_free(fuse_inode_cachep, inode);
  52. return NULL;
  53. }
  54. return inode;
  55. }
  56. static void fuse_destroy_inode(struct inode *inode)
  57. {
  58. struct fuse_inode *fi = get_fuse_inode(inode);
  59. if (fi->forget_req)
  60. fuse_request_free(fi->forget_req);
  61. kmem_cache_free(fuse_inode_cachep, inode);
  62. }
  63. static void fuse_read_inode(struct inode *inode)
  64. {
  65. /* No op */
  66. }
  67. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  68. unsigned long nodeid, u64 nlookup)
  69. {
  70. struct fuse_forget_in *inarg = &req->misc.forget_in;
  71. inarg->nlookup = nlookup;
  72. req->in.h.opcode = FUSE_FORGET;
  73. req->in.h.nodeid = nodeid;
  74. req->in.numargs = 1;
  75. req->in.args[0].size = sizeof(struct fuse_forget_in);
  76. req->in.args[0].value = inarg;
  77. request_send_noreply(fc, req);
  78. }
  79. static void fuse_clear_inode(struct inode *inode)
  80. {
  81. if (inode->i_sb->s_flags & MS_ACTIVE) {
  82. struct fuse_conn *fc = get_fuse_conn(inode);
  83. struct fuse_inode *fi = get_fuse_inode(inode);
  84. fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
  85. fi->forget_req = NULL;
  86. }
  87. }
  88. static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
  89. {
  90. if (*flags & MS_MANDLOCK)
  91. return -EINVAL;
  92. return 0;
  93. }
  94. static void fuse_truncate(struct address_space *mapping, loff_t offset)
  95. {
  96. /* See vmtruncate() */
  97. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  98. truncate_inode_pages(mapping, offset);
  99. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  100. }
  101. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
  102. {
  103. struct fuse_conn *fc = get_fuse_conn(inode);
  104. loff_t oldsize;
  105. inode->i_ino = attr->ino;
  106. inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
  107. inode->i_nlink = attr->nlink;
  108. inode->i_uid = attr->uid;
  109. inode->i_gid = attr->gid;
  110. inode->i_blocks = attr->blocks;
  111. inode->i_atime.tv_sec = attr->atime;
  112. inode->i_atime.tv_nsec = attr->atimensec;
  113. inode->i_mtime.tv_sec = attr->mtime;
  114. inode->i_mtime.tv_nsec = attr->mtimensec;
  115. inode->i_ctime.tv_sec = attr->ctime;
  116. inode->i_ctime.tv_nsec = attr->ctimensec;
  117. spin_lock(&fc->lock);
  118. oldsize = inode->i_size;
  119. i_size_write(inode, attr->size);
  120. spin_unlock(&fc->lock);
  121. if (S_ISREG(inode->i_mode) && oldsize != attr->size) {
  122. if (attr->size < oldsize)
  123. fuse_truncate(inode->i_mapping, attr->size);
  124. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  125. }
  126. }
  127. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
  128. {
  129. inode->i_mode = attr->mode & S_IFMT;
  130. inode->i_size = attr->size;
  131. if (S_ISREG(inode->i_mode)) {
  132. fuse_init_common(inode);
  133. fuse_init_file_inode(inode);
  134. } else if (S_ISDIR(inode->i_mode))
  135. fuse_init_dir(inode);
  136. else if (S_ISLNK(inode->i_mode))
  137. fuse_init_symlink(inode);
  138. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  139. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  140. fuse_init_common(inode);
  141. init_special_inode(inode, inode->i_mode,
  142. new_decode_dev(attr->rdev));
  143. } else
  144. BUG();
  145. }
  146. static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  147. {
  148. unsigned long nodeid = *(unsigned long *) _nodeidp;
  149. if (get_node_id(inode) == nodeid)
  150. return 1;
  151. else
  152. return 0;
  153. }
  154. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  155. {
  156. unsigned long nodeid = *(unsigned long *) _nodeidp;
  157. get_fuse_inode(inode)->nodeid = nodeid;
  158. return 0;
  159. }
  160. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  161. int generation, struct fuse_attr *attr)
  162. {
  163. struct inode *inode;
  164. struct fuse_inode *fi;
  165. struct fuse_conn *fc = get_fuse_conn_super(sb);
  166. retry:
  167. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  168. if (!inode)
  169. return NULL;
  170. if ((inode->i_state & I_NEW)) {
  171. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  172. inode->i_generation = generation;
  173. inode->i_data.backing_dev_info = &fc->bdi;
  174. fuse_init_inode(inode, attr);
  175. unlock_new_inode(inode);
  176. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  177. /* Inode has changed type, any I/O on the old should fail */
  178. make_bad_inode(inode);
  179. iput(inode);
  180. goto retry;
  181. }
  182. fi = get_fuse_inode(inode);
  183. spin_lock(&fc->lock);
  184. fi->nlookup ++;
  185. spin_unlock(&fc->lock);
  186. fuse_change_attributes(inode, attr);
  187. return inode;
  188. }
  189. static void fuse_umount_begin(struct vfsmount *vfsmnt, int flags)
  190. {
  191. if (flags & MNT_FORCE)
  192. fuse_abort_conn(get_fuse_conn_super(vfsmnt->mnt_sb));
  193. }
  194. static void fuse_send_destroy(struct fuse_conn *fc)
  195. {
  196. struct fuse_req *req = fc->destroy_req;
  197. if (req && fc->conn_init) {
  198. fc->destroy_req = NULL;
  199. req->in.h.opcode = FUSE_DESTROY;
  200. req->force = 1;
  201. request_send(fc, req);
  202. fuse_put_request(fc, req);
  203. }
  204. }
  205. static void fuse_put_super(struct super_block *sb)
  206. {
  207. struct fuse_conn *fc = get_fuse_conn_super(sb);
  208. fuse_send_destroy(fc);
  209. spin_lock(&fc->lock);
  210. fc->connected = 0;
  211. fc->blocked = 0;
  212. spin_unlock(&fc->lock);
  213. /* Flush all readers on this fs */
  214. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  215. wake_up_all(&fc->waitq);
  216. wake_up_all(&fc->blocked_waitq);
  217. wake_up_all(&fc->reserved_req_waitq);
  218. mutex_lock(&fuse_mutex);
  219. list_del(&fc->entry);
  220. fuse_ctl_remove_conn(fc);
  221. mutex_unlock(&fuse_mutex);
  222. fuse_conn_put(fc);
  223. }
  224. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  225. {
  226. stbuf->f_type = FUSE_SUPER_MAGIC;
  227. stbuf->f_bsize = attr->bsize;
  228. stbuf->f_frsize = attr->frsize;
  229. stbuf->f_blocks = attr->blocks;
  230. stbuf->f_bfree = attr->bfree;
  231. stbuf->f_bavail = attr->bavail;
  232. stbuf->f_files = attr->files;
  233. stbuf->f_ffree = attr->ffree;
  234. stbuf->f_namelen = attr->namelen;
  235. /* fsid is left zero */
  236. }
  237. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  238. {
  239. struct super_block *sb = dentry->d_sb;
  240. struct fuse_conn *fc = get_fuse_conn_super(sb);
  241. struct fuse_req *req;
  242. struct fuse_statfs_out outarg;
  243. int err;
  244. req = fuse_get_req(fc);
  245. if (IS_ERR(req))
  246. return PTR_ERR(req);
  247. memset(&outarg, 0, sizeof(outarg));
  248. req->in.numargs = 0;
  249. req->in.h.opcode = FUSE_STATFS;
  250. req->in.h.nodeid = get_node_id(dentry->d_inode);
  251. req->out.numargs = 1;
  252. req->out.args[0].size =
  253. fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
  254. req->out.args[0].value = &outarg;
  255. request_send(fc, req);
  256. err = req->out.h.error;
  257. if (!err)
  258. convert_fuse_statfs(buf, &outarg.st);
  259. fuse_put_request(fc, req);
  260. return err;
  261. }
  262. enum {
  263. OPT_FD,
  264. OPT_ROOTMODE,
  265. OPT_USER_ID,
  266. OPT_GROUP_ID,
  267. OPT_DEFAULT_PERMISSIONS,
  268. OPT_ALLOW_OTHER,
  269. OPT_MAX_READ,
  270. OPT_BLKSIZE,
  271. OPT_ERR
  272. };
  273. static match_table_t tokens = {
  274. {OPT_FD, "fd=%u"},
  275. {OPT_ROOTMODE, "rootmode=%o"},
  276. {OPT_USER_ID, "user_id=%u"},
  277. {OPT_GROUP_ID, "group_id=%u"},
  278. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  279. {OPT_ALLOW_OTHER, "allow_other"},
  280. {OPT_MAX_READ, "max_read=%u"},
  281. {OPT_BLKSIZE, "blksize=%u"},
  282. {OPT_ERR, NULL}
  283. };
  284. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
  285. {
  286. char *p;
  287. memset(d, 0, sizeof(struct fuse_mount_data));
  288. d->max_read = ~0;
  289. d->blksize = 512;
  290. while ((p = strsep(&opt, ",")) != NULL) {
  291. int token;
  292. int value;
  293. substring_t args[MAX_OPT_ARGS];
  294. if (!*p)
  295. continue;
  296. token = match_token(p, tokens, args);
  297. switch (token) {
  298. case OPT_FD:
  299. if (match_int(&args[0], &value))
  300. return 0;
  301. d->fd = value;
  302. d->fd_present = 1;
  303. break;
  304. case OPT_ROOTMODE:
  305. if (match_octal(&args[0], &value))
  306. return 0;
  307. if (!fuse_valid_type(value))
  308. return 0;
  309. d->rootmode = value;
  310. d->rootmode_present = 1;
  311. break;
  312. case OPT_USER_ID:
  313. if (match_int(&args[0], &value))
  314. return 0;
  315. d->user_id = value;
  316. d->user_id_present = 1;
  317. break;
  318. case OPT_GROUP_ID:
  319. if (match_int(&args[0], &value))
  320. return 0;
  321. d->group_id = value;
  322. d->group_id_present = 1;
  323. break;
  324. case OPT_DEFAULT_PERMISSIONS:
  325. d->flags |= FUSE_DEFAULT_PERMISSIONS;
  326. break;
  327. case OPT_ALLOW_OTHER:
  328. d->flags |= FUSE_ALLOW_OTHER;
  329. break;
  330. case OPT_MAX_READ:
  331. if (match_int(&args[0], &value))
  332. return 0;
  333. d->max_read = value;
  334. break;
  335. case OPT_BLKSIZE:
  336. if (!is_bdev || match_int(&args[0], &value))
  337. return 0;
  338. d->blksize = value;
  339. break;
  340. default:
  341. return 0;
  342. }
  343. }
  344. if (!d->fd_present || !d->rootmode_present ||
  345. !d->user_id_present || !d->group_id_present)
  346. return 0;
  347. return 1;
  348. }
  349. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  350. {
  351. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  352. seq_printf(m, ",user_id=%u", fc->user_id);
  353. seq_printf(m, ",group_id=%u", fc->group_id);
  354. if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
  355. seq_puts(m, ",default_permissions");
  356. if (fc->flags & FUSE_ALLOW_OTHER)
  357. seq_puts(m, ",allow_other");
  358. if (fc->max_read != ~0)
  359. seq_printf(m, ",max_read=%u", fc->max_read);
  360. return 0;
  361. }
  362. static struct fuse_conn *new_conn(void)
  363. {
  364. struct fuse_conn *fc;
  365. int err;
  366. fc = kzalloc(sizeof(*fc), GFP_KERNEL);
  367. if (fc) {
  368. spin_lock_init(&fc->lock);
  369. mutex_init(&fc->inst_mutex);
  370. atomic_set(&fc->count, 1);
  371. init_waitqueue_head(&fc->waitq);
  372. init_waitqueue_head(&fc->blocked_waitq);
  373. init_waitqueue_head(&fc->reserved_req_waitq);
  374. INIT_LIST_HEAD(&fc->pending);
  375. INIT_LIST_HEAD(&fc->processing);
  376. INIT_LIST_HEAD(&fc->io);
  377. INIT_LIST_HEAD(&fc->interrupts);
  378. atomic_set(&fc->num_waiting, 0);
  379. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  380. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  381. err = bdi_init(&fc->bdi);
  382. if (err) {
  383. kfree(fc);
  384. fc = NULL;
  385. goto out;
  386. }
  387. fc->reqctr = 0;
  388. fc->blocked = 1;
  389. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  390. }
  391. out:
  392. return fc;
  393. }
  394. void fuse_conn_put(struct fuse_conn *fc)
  395. {
  396. if (atomic_dec_and_test(&fc->count)) {
  397. if (fc->destroy_req)
  398. fuse_request_free(fc->destroy_req);
  399. mutex_destroy(&fc->inst_mutex);
  400. bdi_destroy(&fc->bdi);
  401. kfree(fc);
  402. }
  403. }
  404. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  405. {
  406. atomic_inc(&fc->count);
  407. return fc;
  408. }
  409. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  410. {
  411. struct fuse_attr attr;
  412. memset(&attr, 0, sizeof(attr));
  413. attr.mode = mode;
  414. attr.ino = FUSE_ROOT_ID;
  415. return fuse_iget(sb, 1, 0, &attr);
  416. }
  417. static const struct super_operations fuse_super_operations = {
  418. .alloc_inode = fuse_alloc_inode,
  419. .destroy_inode = fuse_destroy_inode,
  420. .read_inode = fuse_read_inode,
  421. .clear_inode = fuse_clear_inode,
  422. .drop_inode = generic_delete_inode,
  423. .remount_fs = fuse_remount_fs,
  424. .put_super = fuse_put_super,
  425. .umount_begin = fuse_umount_begin,
  426. .statfs = fuse_statfs,
  427. .show_options = fuse_show_options,
  428. };
  429. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  430. {
  431. struct fuse_init_out *arg = &req->misc.init_out;
  432. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  433. fc->conn_error = 1;
  434. else {
  435. unsigned long ra_pages;
  436. if (arg->minor >= 6) {
  437. ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
  438. if (arg->flags & FUSE_ASYNC_READ)
  439. fc->async_read = 1;
  440. if (!(arg->flags & FUSE_POSIX_LOCKS))
  441. fc->no_lock = 1;
  442. } else {
  443. ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  444. fc->no_lock = 1;
  445. }
  446. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  447. fc->minor = arg->minor;
  448. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  449. fc->conn_init = 1;
  450. }
  451. fuse_put_request(fc, req);
  452. fc->blocked = 0;
  453. wake_up_all(&fc->blocked_waitq);
  454. }
  455. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  456. {
  457. struct fuse_init_in *arg = &req->misc.init_in;
  458. arg->major = FUSE_KERNEL_VERSION;
  459. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  460. arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
  461. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS;
  462. req->in.h.opcode = FUSE_INIT;
  463. req->in.numargs = 1;
  464. req->in.args[0].size = sizeof(*arg);
  465. req->in.args[0].value = arg;
  466. req->out.numargs = 1;
  467. /* Variable length arguement used for backward compatibility
  468. with interface version < 7.5. Rest of init_out is zeroed
  469. by do_get_request(), so a short reply is not a problem */
  470. req->out.argvar = 1;
  471. req->out.args[0].size = sizeof(struct fuse_init_out);
  472. req->out.args[0].value = &req->misc.init_out;
  473. req->end = process_init_reply;
  474. request_send_background(fc, req);
  475. }
  476. static u64 conn_id(void)
  477. {
  478. static u64 ctr = 1;
  479. return ctr++;
  480. }
  481. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  482. {
  483. struct fuse_conn *fc;
  484. struct inode *root;
  485. struct fuse_mount_data d;
  486. struct file *file;
  487. struct dentry *root_dentry;
  488. struct fuse_req *init_req;
  489. int err;
  490. int is_bdev = sb->s_bdev != NULL;
  491. if (sb->s_flags & MS_MANDLOCK)
  492. return -EINVAL;
  493. if (!parse_fuse_opt((char *) data, &d, is_bdev))
  494. return -EINVAL;
  495. if (is_bdev) {
  496. #ifdef CONFIG_BLOCK
  497. if (!sb_set_blocksize(sb, d.blksize))
  498. return -EINVAL;
  499. #endif
  500. } else {
  501. sb->s_blocksize = PAGE_CACHE_SIZE;
  502. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  503. }
  504. sb->s_magic = FUSE_SUPER_MAGIC;
  505. sb->s_op = &fuse_super_operations;
  506. sb->s_maxbytes = MAX_LFS_FILESIZE;
  507. file = fget(d.fd);
  508. if (!file)
  509. return -EINVAL;
  510. if (file->f_op != &fuse_dev_operations)
  511. return -EINVAL;
  512. fc = new_conn();
  513. if (!fc)
  514. return -ENOMEM;
  515. fc->flags = d.flags;
  516. fc->user_id = d.user_id;
  517. fc->group_id = d.group_id;
  518. fc->max_read = d.max_read;
  519. /* Used by get_root_inode() */
  520. sb->s_fs_info = fc;
  521. err = -ENOMEM;
  522. root = get_root_inode(sb, d.rootmode);
  523. if (!root)
  524. goto err;
  525. root_dentry = d_alloc_root(root);
  526. if (!root_dentry) {
  527. iput(root);
  528. goto err;
  529. }
  530. init_req = fuse_request_alloc();
  531. if (!init_req)
  532. goto err_put_root;
  533. if (is_bdev) {
  534. fc->destroy_req = fuse_request_alloc();
  535. if (!fc->destroy_req)
  536. goto err_put_root;
  537. }
  538. mutex_lock(&fuse_mutex);
  539. err = -EINVAL;
  540. if (file->private_data)
  541. goto err_unlock;
  542. fc->id = conn_id();
  543. err = fuse_ctl_add_conn(fc);
  544. if (err)
  545. goto err_unlock;
  546. list_add_tail(&fc->entry, &fuse_conn_list);
  547. sb->s_root = root_dentry;
  548. fc->connected = 1;
  549. file->private_data = fuse_conn_get(fc);
  550. mutex_unlock(&fuse_mutex);
  551. /*
  552. * atomic_dec_and_test() in fput() provides the necessary
  553. * memory barrier for file->private_data to be visible on all
  554. * CPUs after this
  555. */
  556. fput(file);
  557. fuse_send_init(fc, init_req);
  558. return 0;
  559. err_unlock:
  560. mutex_unlock(&fuse_mutex);
  561. fuse_request_free(init_req);
  562. err_put_root:
  563. dput(root_dentry);
  564. err:
  565. fput(file);
  566. fuse_conn_put(fc);
  567. return err;
  568. }
  569. static int fuse_get_sb(struct file_system_type *fs_type,
  570. int flags, const char *dev_name,
  571. void *raw_data, struct vfsmount *mnt)
  572. {
  573. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
  574. }
  575. static struct file_system_type fuse_fs_type = {
  576. .owner = THIS_MODULE,
  577. .name = "fuse",
  578. .fs_flags = FS_HAS_SUBTYPE,
  579. .get_sb = fuse_get_sb,
  580. .kill_sb = kill_anon_super,
  581. };
  582. #ifdef CONFIG_BLOCK
  583. static int fuse_get_sb_blk(struct file_system_type *fs_type,
  584. int flags, const char *dev_name,
  585. void *raw_data, struct vfsmount *mnt)
  586. {
  587. return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
  588. mnt);
  589. }
  590. static struct file_system_type fuseblk_fs_type = {
  591. .owner = THIS_MODULE,
  592. .name = "fuseblk",
  593. .get_sb = fuse_get_sb_blk,
  594. .kill_sb = kill_block_super,
  595. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  596. };
  597. static inline int register_fuseblk(void)
  598. {
  599. return register_filesystem(&fuseblk_fs_type);
  600. }
  601. static inline void unregister_fuseblk(void)
  602. {
  603. unregister_filesystem(&fuseblk_fs_type);
  604. }
  605. #else
  606. static inline int register_fuseblk(void)
  607. {
  608. return 0;
  609. }
  610. static inline void unregister_fuseblk(void)
  611. {
  612. }
  613. #endif
  614. static decl_subsys(fuse, NULL, NULL);
  615. static decl_subsys(connections, NULL, NULL);
  616. static void fuse_inode_init_once(struct kmem_cache *cachep, void *foo)
  617. {
  618. struct inode * inode = foo;
  619. inode_init_once(inode);
  620. }
  621. static int __init fuse_fs_init(void)
  622. {
  623. int err;
  624. err = register_filesystem(&fuse_fs_type);
  625. if (err)
  626. goto out;
  627. err = register_fuseblk();
  628. if (err)
  629. goto out_unreg;
  630. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  631. sizeof(struct fuse_inode),
  632. 0, SLAB_HWCACHE_ALIGN,
  633. fuse_inode_init_once);
  634. err = -ENOMEM;
  635. if (!fuse_inode_cachep)
  636. goto out_unreg2;
  637. return 0;
  638. out_unreg2:
  639. unregister_fuseblk();
  640. out_unreg:
  641. unregister_filesystem(&fuse_fs_type);
  642. out:
  643. return err;
  644. }
  645. static void fuse_fs_cleanup(void)
  646. {
  647. unregister_filesystem(&fuse_fs_type);
  648. unregister_fuseblk();
  649. kmem_cache_destroy(fuse_inode_cachep);
  650. }
  651. static int fuse_sysfs_init(void)
  652. {
  653. int err;
  654. kobj_set_kset_s(&fuse_subsys, fs_subsys);
  655. err = subsystem_register(&fuse_subsys);
  656. if (err)
  657. goto out_err;
  658. kobj_set_kset_s(&connections_subsys, fuse_subsys);
  659. err = subsystem_register(&connections_subsys);
  660. if (err)
  661. goto out_fuse_unregister;
  662. return 0;
  663. out_fuse_unregister:
  664. subsystem_unregister(&fuse_subsys);
  665. out_err:
  666. return err;
  667. }
  668. static void fuse_sysfs_cleanup(void)
  669. {
  670. subsystem_unregister(&connections_subsys);
  671. subsystem_unregister(&fuse_subsys);
  672. }
  673. static int __init fuse_init(void)
  674. {
  675. int res;
  676. printk("fuse init (API version %i.%i)\n",
  677. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  678. INIT_LIST_HEAD(&fuse_conn_list);
  679. res = fuse_fs_init();
  680. if (res)
  681. goto err;
  682. res = fuse_dev_init();
  683. if (res)
  684. goto err_fs_cleanup;
  685. res = fuse_sysfs_init();
  686. if (res)
  687. goto err_dev_cleanup;
  688. res = fuse_ctl_init();
  689. if (res)
  690. goto err_sysfs_cleanup;
  691. return 0;
  692. err_sysfs_cleanup:
  693. fuse_sysfs_cleanup();
  694. err_dev_cleanup:
  695. fuse_dev_cleanup();
  696. err_fs_cleanup:
  697. fuse_fs_cleanup();
  698. err:
  699. return res;
  700. }
  701. static void __exit fuse_exit(void)
  702. {
  703. printk(KERN_DEBUG "fuse exit\n");
  704. fuse_ctl_cleanup();
  705. fuse_sysfs_cleanup();
  706. fuse_fs_cleanup();
  707. fuse_dev_cleanup();
  708. }
  709. module_init(fuse_init);
  710. module_exit(fuse_exit);