inode.c 18 KB

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