inode.c 20 KB

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