inode.c 19 KB

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