inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. if (sb->s_bdev) {
  428. err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
  429. MAJOR(fc->dev), MINOR(fc->dev));
  430. } else {
  431. err = bdi_register_dev(&fc->bdi, fc->dev);
  432. }
  433. if (err)
  434. goto error_bdi_destroy;
  435. /*
  436. * For a single fuse filesystem use max 1% of dirty +
  437. * writeback threshold.
  438. *
  439. * This gives about 1M of write buffer for memory maps on a
  440. * machine with 1G and 10% dirty_ratio, which should be more
  441. * than enough.
  442. *
  443. * Privileged users can raise it by writing to
  444. *
  445. * /sys/class/bdi/<bdi>/max_ratio
  446. */
  447. bdi_set_max_ratio(&fc->bdi, 1);
  448. fc->reqctr = 0;
  449. fc->blocked = 1;
  450. fc->attr_version = 1;
  451. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  452. }
  453. return fc;
  454. error_bdi_destroy:
  455. bdi_destroy(&fc->bdi);
  456. error_kfree:
  457. mutex_destroy(&fc->inst_mutex);
  458. kfree(fc);
  459. return NULL;
  460. }
  461. void fuse_conn_put(struct fuse_conn *fc)
  462. {
  463. if (atomic_dec_and_test(&fc->count)) {
  464. if (fc->destroy_req)
  465. fuse_request_free(fc->destroy_req);
  466. mutex_destroy(&fc->inst_mutex);
  467. bdi_destroy(&fc->bdi);
  468. kfree(fc);
  469. }
  470. }
  471. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  472. {
  473. atomic_inc(&fc->count);
  474. return fc;
  475. }
  476. static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
  477. {
  478. struct fuse_attr attr;
  479. memset(&attr, 0, sizeof(attr));
  480. attr.mode = mode;
  481. attr.ino = FUSE_ROOT_ID;
  482. attr.nlink = 1;
  483. return fuse_iget(sb, 1, 0, &attr, 0, 0);
  484. }
  485. static const struct super_operations fuse_super_operations = {
  486. .alloc_inode = fuse_alloc_inode,
  487. .destroy_inode = fuse_destroy_inode,
  488. .clear_inode = fuse_clear_inode,
  489. .drop_inode = generic_delete_inode,
  490. .remount_fs = fuse_remount_fs,
  491. .put_super = fuse_put_super,
  492. .umount_begin = fuse_umount_begin,
  493. .statfs = fuse_statfs,
  494. .show_options = fuse_show_options,
  495. };
  496. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  497. {
  498. struct fuse_init_out *arg = &req->misc.init_out;
  499. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  500. fc->conn_error = 1;
  501. else {
  502. unsigned long ra_pages;
  503. if (arg->minor >= 6) {
  504. ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
  505. if (arg->flags & FUSE_ASYNC_READ)
  506. fc->async_read = 1;
  507. if (!(arg->flags & FUSE_POSIX_LOCKS))
  508. fc->no_lock = 1;
  509. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  510. fc->atomic_o_trunc = 1;
  511. if (arg->flags & FUSE_BIG_WRITES)
  512. fc->big_writes = 1;
  513. } else {
  514. ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  515. fc->no_lock = 1;
  516. }
  517. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  518. fc->minor = arg->minor;
  519. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  520. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  521. fc->conn_init = 1;
  522. }
  523. fuse_put_request(fc, req);
  524. fc->blocked = 0;
  525. wake_up_all(&fc->blocked_waitq);
  526. }
  527. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  528. {
  529. struct fuse_init_in *arg = &req->misc.init_in;
  530. arg->major = FUSE_KERNEL_VERSION;
  531. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  532. arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
  533. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  534. FUSE_BIG_WRITES;
  535. req->in.h.opcode = FUSE_INIT;
  536. req->in.numargs = 1;
  537. req->in.args[0].size = sizeof(*arg);
  538. req->in.args[0].value = arg;
  539. req->out.numargs = 1;
  540. /* Variable length arguement used for backward compatibility
  541. with interface version < 7.5. Rest of init_out is zeroed
  542. by do_get_request(), so a short reply is not a problem */
  543. req->out.argvar = 1;
  544. req->out.args[0].size = sizeof(struct fuse_init_out);
  545. req->out.args[0].value = &req->misc.init_out;
  546. req->end = process_init_reply;
  547. request_send_background(fc, req);
  548. }
  549. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  550. {
  551. struct fuse_conn *fc;
  552. struct inode *root;
  553. struct fuse_mount_data d;
  554. struct file *file;
  555. struct dentry *root_dentry;
  556. struct fuse_req *init_req;
  557. int err;
  558. int is_bdev = sb->s_bdev != NULL;
  559. if (sb->s_flags & MS_MANDLOCK)
  560. return -EINVAL;
  561. if (!parse_fuse_opt((char *) data, &d, is_bdev))
  562. return -EINVAL;
  563. if (is_bdev) {
  564. #ifdef CONFIG_BLOCK
  565. if (!sb_set_blocksize(sb, d.blksize))
  566. return -EINVAL;
  567. #endif
  568. } else {
  569. sb->s_blocksize = PAGE_CACHE_SIZE;
  570. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  571. }
  572. sb->s_magic = FUSE_SUPER_MAGIC;
  573. sb->s_op = &fuse_super_operations;
  574. sb->s_maxbytes = MAX_LFS_FILESIZE;
  575. file = fget(d.fd);
  576. if (!file)
  577. return -EINVAL;
  578. if (file->f_op != &fuse_dev_operations)
  579. return -EINVAL;
  580. fc = new_conn(sb);
  581. if (!fc)
  582. return -ENOMEM;
  583. fc->flags = d.flags;
  584. fc->user_id = d.user_id;
  585. fc->group_id = d.group_id;
  586. fc->max_read = max_t(unsigned, 4096, d.max_read);
  587. /* Used by get_root_inode() */
  588. sb->s_fs_info = fc;
  589. err = -ENOMEM;
  590. root = get_root_inode(sb, d.rootmode);
  591. if (!root)
  592. goto err;
  593. root_dentry = d_alloc_root(root);
  594. if (!root_dentry) {
  595. iput(root);
  596. goto err;
  597. }
  598. init_req = fuse_request_alloc();
  599. if (!init_req)
  600. goto err_put_root;
  601. if (is_bdev) {
  602. fc->destroy_req = fuse_request_alloc();
  603. if (!fc->destroy_req)
  604. goto err_put_root;
  605. }
  606. mutex_lock(&fuse_mutex);
  607. err = -EINVAL;
  608. if (file->private_data)
  609. goto err_unlock;
  610. err = fuse_ctl_add_conn(fc);
  611. if (err)
  612. goto err_unlock;
  613. list_add_tail(&fc->entry, &fuse_conn_list);
  614. sb->s_root = root_dentry;
  615. fc->connected = 1;
  616. file->private_data = fuse_conn_get(fc);
  617. mutex_unlock(&fuse_mutex);
  618. /*
  619. * atomic_dec_and_test() in fput() provides the necessary
  620. * memory barrier for file->private_data to be visible on all
  621. * CPUs after this
  622. */
  623. fput(file);
  624. fuse_send_init(fc, init_req);
  625. return 0;
  626. err_unlock:
  627. mutex_unlock(&fuse_mutex);
  628. fuse_request_free(init_req);
  629. err_put_root:
  630. dput(root_dentry);
  631. err:
  632. fput(file);
  633. fuse_conn_put(fc);
  634. return err;
  635. }
  636. static int fuse_get_sb(struct file_system_type *fs_type,
  637. int flags, const char *dev_name,
  638. void *raw_data, struct vfsmount *mnt)
  639. {
  640. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
  641. }
  642. static struct file_system_type fuse_fs_type = {
  643. .owner = THIS_MODULE,
  644. .name = "fuse",
  645. .fs_flags = FS_HAS_SUBTYPE,
  646. .get_sb = fuse_get_sb,
  647. .kill_sb = kill_anon_super,
  648. };
  649. #ifdef CONFIG_BLOCK
  650. static int fuse_get_sb_blk(struct file_system_type *fs_type,
  651. int flags, const char *dev_name,
  652. void *raw_data, struct vfsmount *mnt)
  653. {
  654. return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
  655. mnt);
  656. }
  657. static struct file_system_type fuseblk_fs_type = {
  658. .owner = THIS_MODULE,
  659. .name = "fuseblk",
  660. .get_sb = fuse_get_sb_blk,
  661. .kill_sb = kill_block_super,
  662. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  663. };
  664. static inline int register_fuseblk(void)
  665. {
  666. return register_filesystem(&fuseblk_fs_type);
  667. }
  668. static inline void unregister_fuseblk(void)
  669. {
  670. unregister_filesystem(&fuseblk_fs_type);
  671. }
  672. #else
  673. static inline int register_fuseblk(void)
  674. {
  675. return 0;
  676. }
  677. static inline void unregister_fuseblk(void)
  678. {
  679. }
  680. #endif
  681. static void fuse_inode_init_once(struct kmem_cache *cachep, void *foo)
  682. {
  683. struct inode * inode = foo;
  684. inode_init_once(inode);
  685. }
  686. static int __init fuse_fs_init(void)
  687. {
  688. int err;
  689. err = register_filesystem(&fuse_fs_type);
  690. if (err)
  691. goto out;
  692. err = register_fuseblk();
  693. if (err)
  694. goto out_unreg;
  695. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  696. sizeof(struct fuse_inode),
  697. 0, SLAB_HWCACHE_ALIGN,
  698. fuse_inode_init_once);
  699. err = -ENOMEM;
  700. if (!fuse_inode_cachep)
  701. goto out_unreg2;
  702. return 0;
  703. out_unreg2:
  704. unregister_fuseblk();
  705. out_unreg:
  706. unregister_filesystem(&fuse_fs_type);
  707. out:
  708. return err;
  709. }
  710. static void fuse_fs_cleanup(void)
  711. {
  712. unregister_filesystem(&fuse_fs_type);
  713. unregister_fuseblk();
  714. kmem_cache_destroy(fuse_inode_cachep);
  715. }
  716. static struct kobject *fuse_kobj;
  717. static struct kobject *connections_kobj;
  718. static int fuse_sysfs_init(void)
  719. {
  720. int err;
  721. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  722. if (!fuse_kobj) {
  723. err = -ENOMEM;
  724. goto out_err;
  725. }
  726. connections_kobj = kobject_create_and_add("connections", fuse_kobj);
  727. if (!connections_kobj) {
  728. err = -ENOMEM;
  729. goto out_fuse_unregister;
  730. }
  731. return 0;
  732. out_fuse_unregister:
  733. kobject_put(fuse_kobj);
  734. out_err:
  735. return err;
  736. }
  737. static void fuse_sysfs_cleanup(void)
  738. {
  739. kobject_put(connections_kobj);
  740. kobject_put(fuse_kobj);
  741. }
  742. static int __init fuse_init(void)
  743. {
  744. int res;
  745. printk("fuse init (API version %i.%i)\n",
  746. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  747. INIT_LIST_HEAD(&fuse_conn_list);
  748. res = fuse_fs_init();
  749. if (res)
  750. goto err;
  751. res = fuse_dev_init();
  752. if (res)
  753. goto err_fs_cleanup;
  754. res = fuse_sysfs_init();
  755. if (res)
  756. goto err_dev_cleanup;
  757. res = fuse_ctl_init();
  758. if (res)
  759. goto err_sysfs_cleanup;
  760. return 0;
  761. err_sysfs_cleanup:
  762. fuse_sysfs_cleanup();
  763. err_dev_cleanup:
  764. fuse_dev_cleanup();
  765. err_fs_cleanup:
  766. fuse_fs_cleanup();
  767. err:
  768. return res;
  769. }
  770. static void __exit fuse_exit(void)
  771. {
  772. printk(KERN_DEBUG "fuse exit\n");
  773. fuse_ctl_cleanup();
  774. fuse_sysfs_cleanup();
  775. fuse_fs_cleanup();
  776. fuse_dev_cleanup();
  777. }
  778. module_init(fuse_init);
  779. module_exit(fuse_exit);