inode.c 19 KB

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