inode.c 19 KB

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