inode.c 19 KB

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