inode.c 18 KB

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