inode.c 17 KB

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