inode.c 18 KB

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