inode.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 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/moduleparam.h>
  15. #include <linux/parser.h>
  16. #include <linux/statfs.h>
  17. #include <linux/random.h>
  18. #include <linux/sched.h>
  19. #include <linux/exportfs.h>
  20. #include <linux/posix_acl.h>
  21. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  22. MODULE_DESCRIPTION("Filesystem in Userspace");
  23. MODULE_LICENSE("GPL");
  24. static struct kmem_cache *fuse_inode_cachep;
  25. struct list_head fuse_conn_list;
  26. DEFINE_MUTEX(fuse_mutex);
  27. static int set_global_limit(const char *val, struct kernel_param *kp);
  28. unsigned max_user_bgreq;
  29. module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
  30. &max_user_bgreq, 0644);
  31. __MODULE_PARM_TYPE(max_user_bgreq, "uint");
  32. MODULE_PARM_DESC(max_user_bgreq,
  33. "Global limit for the maximum number of backgrounded requests an "
  34. "unprivileged user can set");
  35. unsigned max_user_congthresh;
  36. module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
  37. &max_user_congthresh, 0644);
  38. __MODULE_PARM_TYPE(max_user_congthresh, "uint");
  39. MODULE_PARM_DESC(max_user_congthresh,
  40. "Global limit for the maximum congestion threshold an "
  41. "unprivileged user can set");
  42. #define FUSE_SUPER_MAGIC 0x65735546
  43. #define FUSE_DEFAULT_BLKSIZE 512
  44. /** Maximum number of outstanding background requests */
  45. #define FUSE_DEFAULT_MAX_BACKGROUND 12
  46. /** Congestion starts at 75% of maximum */
  47. #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
  48. struct fuse_mount_data {
  49. int fd;
  50. unsigned rootmode;
  51. kuid_t user_id;
  52. kgid_t group_id;
  53. unsigned fd_present:1;
  54. unsigned rootmode_present:1;
  55. unsigned user_id_present:1;
  56. unsigned group_id_present:1;
  57. unsigned default_permissions:1;
  58. unsigned allow_other:1;
  59. unsigned max_read;
  60. unsigned blksize;
  61. };
  62. struct fuse_forget_link *fuse_alloc_forget(void)
  63. {
  64. return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL);
  65. }
  66. static struct inode *fuse_alloc_inode(struct super_block *sb)
  67. {
  68. struct inode *inode;
  69. struct fuse_inode *fi;
  70. inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
  71. if (!inode)
  72. return NULL;
  73. fi = get_fuse_inode(inode);
  74. fi->i_time = 0;
  75. fi->nodeid = 0;
  76. fi->nlookup = 0;
  77. fi->attr_version = 0;
  78. fi->writectr = 0;
  79. fi->orig_ino = 0;
  80. fi->state = 0;
  81. INIT_LIST_HEAD(&fi->write_files);
  82. INIT_LIST_HEAD(&fi->queued_writes);
  83. INIT_LIST_HEAD(&fi->writepages);
  84. init_waitqueue_head(&fi->page_waitq);
  85. mutex_init(&fi->mutex);
  86. fi->forget = fuse_alloc_forget();
  87. if (!fi->forget) {
  88. kmem_cache_free(fuse_inode_cachep, inode);
  89. return NULL;
  90. }
  91. return inode;
  92. }
  93. static void fuse_i_callback(struct rcu_head *head)
  94. {
  95. struct inode *inode = container_of(head, struct inode, i_rcu);
  96. kmem_cache_free(fuse_inode_cachep, inode);
  97. }
  98. static void fuse_destroy_inode(struct inode *inode)
  99. {
  100. struct fuse_inode *fi = get_fuse_inode(inode);
  101. BUG_ON(!list_empty(&fi->write_files));
  102. BUG_ON(!list_empty(&fi->queued_writes));
  103. mutex_destroy(&fi->mutex);
  104. kfree(fi->forget);
  105. call_rcu(&inode->i_rcu, fuse_i_callback);
  106. }
  107. static void fuse_evict_inode(struct inode *inode)
  108. {
  109. truncate_inode_pages_final(&inode->i_data);
  110. clear_inode(inode);
  111. if (inode->i_sb->s_flags & MS_ACTIVE) {
  112. struct fuse_conn *fc = get_fuse_conn(inode);
  113. struct fuse_inode *fi = get_fuse_inode(inode);
  114. fuse_queue_forget(fc, fi->forget, fi->nodeid, fi->nlookup);
  115. fi->forget = NULL;
  116. }
  117. }
  118. static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
  119. {
  120. sync_filesystem(sb);
  121. if (*flags & MS_MANDLOCK)
  122. return -EINVAL;
  123. return 0;
  124. }
  125. /*
  126. * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
  127. * so that it will fit.
  128. */
  129. static ino_t fuse_squash_ino(u64 ino64)
  130. {
  131. ino_t ino = (ino_t) ino64;
  132. if (sizeof(ino_t) < sizeof(u64))
  133. ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
  134. return ino;
  135. }
  136. void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
  137. u64 attr_valid)
  138. {
  139. struct fuse_conn *fc = get_fuse_conn(inode);
  140. struct fuse_inode *fi = get_fuse_inode(inode);
  141. fi->attr_version = ++fc->attr_version;
  142. fi->i_time = attr_valid;
  143. inode->i_ino = fuse_squash_ino(attr->ino);
  144. inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
  145. set_nlink(inode, attr->nlink);
  146. inode->i_uid = make_kuid(&init_user_ns, attr->uid);
  147. inode->i_gid = make_kgid(&init_user_ns, attr->gid);
  148. inode->i_blocks = attr->blocks;
  149. inode->i_atime.tv_sec = attr->atime;
  150. inode->i_atime.tv_nsec = attr->atimensec;
  151. /* mtime from server may be stale due to local buffered write */
  152. if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) {
  153. inode->i_mtime.tv_sec = attr->mtime;
  154. inode->i_mtime.tv_nsec = attr->mtimensec;
  155. inode->i_ctime.tv_sec = attr->ctime;
  156. inode->i_ctime.tv_nsec = attr->ctimensec;
  157. }
  158. if (attr->blksize != 0)
  159. inode->i_blkbits = ilog2(attr->blksize);
  160. else
  161. inode->i_blkbits = inode->i_sb->s_blocksize_bits;
  162. /*
  163. * Don't set the sticky bit in i_mode, unless we want the VFS
  164. * to check permissions. This prevents failures due to the
  165. * check in may_delete().
  166. */
  167. fi->orig_i_mode = inode->i_mode;
  168. if (!fc->default_permissions)
  169. inode->i_mode &= ~S_ISVTX;
  170. fi->orig_ino = attr->ino;
  171. }
  172. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  173. u64 attr_valid, u64 attr_version)
  174. {
  175. struct fuse_conn *fc = get_fuse_conn(inode);
  176. struct fuse_inode *fi = get_fuse_inode(inode);
  177. bool is_wb = fc->writeback_cache;
  178. loff_t oldsize;
  179. struct timespec old_mtime;
  180. spin_lock(&fc->lock);
  181. if ((attr_version != 0 && fi->attr_version > attr_version) ||
  182. test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
  183. spin_unlock(&fc->lock);
  184. return;
  185. }
  186. old_mtime = inode->i_mtime;
  187. fuse_change_attributes_common(inode, attr, attr_valid);
  188. oldsize = inode->i_size;
  189. /*
  190. * In case of writeback_cache enabled, the cached writes beyond EOF
  191. * extend local i_size without keeping userspace server in sync. So,
  192. * attr->size coming from server can be stale. We cannot trust it.
  193. */
  194. if (!is_wb || !S_ISREG(inode->i_mode))
  195. i_size_write(inode, attr->size);
  196. spin_unlock(&fc->lock);
  197. if (!is_wb && S_ISREG(inode->i_mode)) {
  198. bool inval = false;
  199. if (oldsize != attr->size) {
  200. truncate_pagecache(inode, attr->size);
  201. inval = true;
  202. } else if (fc->auto_inval_data) {
  203. struct timespec new_mtime = {
  204. .tv_sec = attr->mtime,
  205. .tv_nsec = attr->mtimensec,
  206. };
  207. /*
  208. * Auto inval mode also checks and invalidates if mtime
  209. * has changed.
  210. */
  211. if (!timespec_equal(&old_mtime, &new_mtime))
  212. inval = true;
  213. }
  214. if (inval)
  215. invalidate_inode_pages2(inode->i_mapping);
  216. }
  217. }
  218. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
  219. {
  220. inode->i_mode = attr->mode & S_IFMT;
  221. inode->i_size = attr->size;
  222. inode->i_mtime.tv_sec = attr->mtime;
  223. inode->i_mtime.tv_nsec = attr->mtimensec;
  224. inode->i_ctime.tv_sec = attr->ctime;
  225. inode->i_ctime.tv_nsec = attr->ctimensec;
  226. if (S_ISREG(inode->i_mode)) {
  227. fuse_init_common(inode);
  228. fuse_init_file_inode(inode);
  229. } else if (S_ISDIR(inode->i_mode))
  230. fuse_init_dir(inode);
  231. else if (S_ISLNK(inode->i_mode))
  232. fuse_init_symlink(inode);
  233. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  234. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  235. fuse_init_common(inode);
  236. init_special_inode(inode, inode->i_mode,
  237. new_decode_dev(attr->rdev));
  238. } else
  239. BUG();
  240. }
  241. int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  242. {
  243. u64 nodeid = *(u64 *) _nodeidp;
  244. if (get_node_id(inode) == nodeid)
  245. return 1;
  246. else
  247. return 0;
  248. }
  249. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  250. {
  251. u64 nodeid = *(u64 *) _nodeidp;
  252. get_fuse_inode(inode)->nodeid = nodeid;
  253. return 0;
  254. }
  255. struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
  256. int generation, struct fuse_attr *attr,
  257. u64 attr_valid, u64 attr_version)
  258. {
  259. struct inode *inode;
  260. struct fuse_inode *fi;
  261. struct fuse_conn *fc = get_fuse_conn_super(sb);
  262. retry:
  263. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  264. if (!inode)
  265. return NULL;
  266. if ((inode->i_state & I_NEW)) {
  267. inode->i_flags |= S_NOATIME;
  268. if (!fc->writeback_cache || !S_ISREG(attr->mode))
  269. inode->i_flags |= S_NOCMTIME;
  270. inode->i_generation = generation;
  271. fuse_init_inode(inode, attr);
  272. unlock_new_inode(inode);
  273. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  274. /* Inode has changed type, any I/O on the old should fail */
  275. make_bad_inode(inode);
  276. iput(inode);
  277. goto retry;
  278. }
  279. fi = get_fuse_inode(inode);
  280. spin_lock(&fc->lock);
  281. fi->nlookup++;
  282. spin_unlock(&fc->lock);
  283. fuse_change_attributes(inode, attr, attr_valid, attr_version);
  284. return inode;
  285. }
  286. int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
  287. loff_t offset, loff_t len)
  288. {
  289. struct inode *inode;
  290. pgoff_t pg_start;
  291. pgoff_t pg_end;
  292. inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
  293. if (!inode)
  294. return -ENOENT;
  295. fuse_invalidate_attr(inode);
  296. forget_all_cached_acls(inode);
  297. if (offset >= 0) {
  298. pg_start = offset >> PAGE_SHIFT;
  299. if (len <= 0)
  300. pg_end = -1;
  301. else
  302. pg_end = (offset + len - 1) >> PAGE_SHIFT;
  303. invalidate_inode_pages2_range(inode->i_mapping,
  304. pg_start, pg_end);
  305. }
  306. iput(inode);
  307. return 0;
  308. }
  309. void fuse_lock_inode(struct inode *inode)
  310. {
  311. if (!get_fuse_conn(inode)->parallel_dirops)
  312. mutex_lock(&get_fuse_inode(inode)->mutex);
  313. }
  314. void fuse_unlock_inode(struct inode *inode)
  315. {
  316. if (!get_fuse_conn(inode)->parallel_dirops)
  317. mutex_unlock(&get_fuse_inode(inode)->mutex);
  318. }
  319. static void fuse_umount_begin(struct super_block *sb)
  320. {
  321. fuse_abort_conn(get_fuse_conn_super(sb));
  322. }
  323. static void fuse_send_destroy(struct fuse_conn *fc)
  324. {
  325. struct fuse_req *req = fc->destroy_req;
  326. if (req && fc->conn_init) {
  327. fc->destroy_req = NULL;
  328. req->in.h.opcode = FUSE_DESTROY;
  329. __set_bit(FR_FORCE, &req->flags);
  330. __clear_bit(FR_BACKGROUND, &req->flags);
  331. fuse_request_send(fc, req);
  332. fuse_put_request(fc, req);
  333. }
  334. }
  335. static void fuse_bdi_destroy(struct fuse_conn *fc)
  336. {
  337. if (fc->bdi_initialized)
  338. bdi_destroy(&fc->bdi);
  339. }
  340. static void fuse_put_super(struct super_block *sb)
  341. {
  342. struct fuse_conn *fc = get_fuse_conn_super(sb);
  343. fuse_send_destroy(fc);
  344. fuse_abort_conn(fc);
  345. mutex_lock(&fuse_mutex);
  346. list_del(&fc->entry);
  347. fuse_ctl_remove_conn(fc);
  348. mutex_unlock(&fuse_mutex);
  349. fuse_bdi_destroy(fc);
  350. fuse_conn_put(fc);
  351. }
  352. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  353. {
  354. stbuf->f_type = FUSE_SUPER_MAGIC;
  355. stbuf->f_bsize = attr->bsize;
  356. stbuf->f_frsize = attr->frsize;
  357. stbuf->f_blocks = attr->blocks;
  358. stbuf->f_bfree = attr->bfree;
  359. stbuf->f_bavail = attr->bavail;
  360. stbuf->f_files = attr->files;
  361. stbuf->f_ffree = attr->ffree;
  362. stbuf->f_namelen = attr->namelen;
  363. /* fsid is left zero */
  364. }
  365. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  366. {
  367. struct super_block *sb = dentry->d_sb;
  368. struct fuse_conn *fc = get_fuse_conn_super(sb);
  369. FUSE_ARGS(args);
  370. struct fuse_statfs_out outarg;
  371. int err;
  372. if (!fuse_allow_current_process(fc)) {
  373. buf->f_type = FUSE_SUPER_MAGIC;
  374. return 0;
  375. }
  376. memset(&outarg, 0, sizeof(outarg));
  377. args.in.numargs = 0;
  378. args.in.h.opcode = FUSE_STATFS;
  379. args.in.h.nodeid = get_node_id(d_inode(dentry));
  380. args.out.numargs = 1;
  381. args.out.args[0].size = sizeof(outarg);
  382. args.out.args[0].value = &outarg;
  383. err = fuse_simple_request(fc, &args);
  384. if (!err)
  385. convert_fuse_statfs(buf, &outarg.st);
  386. return err;
  387. }
  388. enum {
  389. OPT_FD,
  390. OPT_ROOTMODE,
  391. OPT_USER_ID,
  392. OPT_GROUP_ID,
  393. OPT_DEFAULT_PERMISSIONS,
  394. OPT_ALLOW_OTHER,
  395. OPT_MAX_READ,
  396. OPT_BLKSIZE,
  397. OPT_ERR
  398. };
  399. static const match_table_t tokens = {
  400. {OPT_FD, "fd=%u"},
  401. {OPT_ROOTMODE, "rootmode=%o"},
  402. {OPT_USER_ID, "user_id=%u"},
  403. {OPT_GROUP_ID, "group_id=%u"},
  404. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  405. {OPT_ALLOW_OTHER, "allow_other"},
  406. {OPT_MAX_READ, "max_read=%u"},
  407. {OPT_BLKSIZE, "blksize=%u"},
  408. {OPT_ERR, NULL}
  409. };
  410. static int fuse_match_uint(substring_t *s, unsigned int *res)
  411. {
  412. int err = -ENOMEM;
  413. char *buf = match_strdup(s);
  414. if (buf) {
  415. err = kstrtouint(buf, 10, res);
  416. kfree(buf);
  417. }
  418. return err;
  419. }
  420. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
  421. {
  422. char *p;
  423. memset(d, 0, sizeof(struct fuse_mount_data));
  424. d->max_read = ~0;
  425. d->blksize = FUSE_DEFAULT_BLKSIZE;
  426. while ((p = strsep(&opt, ",")) != NULL) {
  427. int token;
  428. int value;
  429. unsigned uv;
  430. substring_t args[MAX_OPT_ARGS];
  431. if (!*p)
  432. continue;
  433. token = match_token(p, tokens, args);
  434. switch (token) {
  435. case OPT_FD:
  436. if (match_int(&args[0], &value))
  437. return 0;
  438. d->fd = value;
  439. d->fd_present = 1;
  440. break;
  441. case OPT_ROOTMODE:
  442. if (match_octal(&args[0], &value))
  443. return 0;
  444. if (!fuse_valid_type(value))
  445. return 0;
  446. d->rootmode = value;
  447. d->rootmode_present = 1;
  448. break;
  449. case OPT_USER_ID:
  450. if (fuse_match_uint(&args[0], &uv))
  451. return 0;
  452. d->user_id = make_kuid(current_user_ns(), uv);
  453. if (!uid_valid(d->user_id))
  454. return 0;
  455. d->user_id_present = 1;
  456. break;
  457. case OPT_GROUP_ID:
  458. if (fuse_match_uint(&args[0], &uv))
  459. return 0;
  460. d->group_id = make_kgid(current_user_ns(), uv);
  461. if (!gid_valid(d->group_id))
  462. return 0;
  463. d->group_id_present = 1;
  464. break;
  465. case OPT_DEFAULT_PERMISSIONS:
  466. d->default_permissions = 1;
  467. break;
  468. case OPT_ALLOW_OTHER:
  469. d->allow_other = 1;
  470. break;
  471. case OPT_MAX_READ:
  472. if (match_int(&args[0], &value))
  473. return 0;
  474. d->max_read = value;
  475. break;
  476. case OPT_BLKSIZE:
  477. if (!is_bdev || match_int(&args[0], &value))
  478. return 0;
  479. d->blksize = value;
  480. break;
  481. default:
  482. return 0;
  483. }
  484. }
  485. if (!d->fd_present || !d->rootmode_present ||
  486. !d->user_id_present || !d->group_id_present)
  487. return 0;
  488. return 1;
  489. }
  490. static int fuse_show_options(struct seq_file *m, struct dentry *root)
  491. {
  492. struct super_block *sb = root->d_sb;
  493. struct fuse_conn *fc = get_fuse_conn_super(sb);
  494. seq_printf(m, ",user_id=%u", from_kuid_munged(&init_user_ns, fc->user_id));
  495. seq_printf(m, ",group_id=%u", from_kgid_munged(&init_user_ns, fc->group_id));
  496. if (fc->default_permissions)
  497. seq_puts(m, ",default_permissions");
  498. if (fc->allow_other)
  499. seq_puts(m, ",allow_other");
  500. if (fc->max_read != ~0)
  501. seq_printf(m, ",max_read=%u", fc->max_read);
  502. if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
  503. seq_printf(m, ",blksize=%lu", sb->s_blocksize);
  504. return 0;
  505. }
  506. static void fuse_iqueue_init(struct fuse_iqueue *fiq)
  507. {
  508. memset(fiq, 0, sizeof(struct fuse_iqueue));
  509. init_waitqueue_head(&fiq->waitq);
  510. INIT_LIST_HEAD(&fiq->pending);
  511. INIT_LIST_HEAD(&fiq->interrupts);
  512. fiq->forget_list_tail = &fiq->forget_list_head;
  513. fiq->connected = 1;
  514. }
  515. static void fuse_pqueue_init(struct fuse_pqueue *fpq)
  516. {
  517. memset(fpq, 0, sizeof(struct fuse_pqueue));
  518. spin_lock_init(&fpq->lock);
  519. INIT_LIST_HEAD(&fpq->processing);
  520. INIT_LIST_HEAD(&fpq->io);
  521. fpq->connected = 1;
  522. }
  523. void fuse_conn_init(struct fuse_conn *fc)
  524. {
  525. memset(fc, 0, sizeof(*fc));
  526. spin_lock_init(&fc->lock);
  527. init_rwsem(&fc->killsb);
  528. atomic_set(&fc->count, 1);
  529. atomic_set(&fc->dev_count, 1);
  530. init_waitqueue_head(&fc->blocked_waitq);
  531. init_waitqueue_head(&fc->reserved_req_waitq);
  532. fuse_iqueue_init(&fc->iq);
  533. INIT_LIST_HEAD(&fc->bg_queue);
  534. INIT_LIST_HEAD(&fc->entry);
  535. INIT_LIST_HEAD(&fc->devices);
  536. atomic_set(&fc->num_waiting, 0);
  537. fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
  538. fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
  539. fc->khctr = 0;
  540. fc->polled_files = RB_ROOT;
  541. fc->blocked = 0;
  542. fc->initialized = 0;
  543. fc->connected = 1;
  544. fc->attr_version = 1;
  545. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  546. }
  547. EXPORT_SYMBOL_GPL(fuse_conn_init);
  548. void fuse_conn_put(struct fuse_conn *fc)
  549. {
  550. if (atomic_dec_and_test(&fc->count)) {
  551. if (fc->destroy_req)
  552. fuse_request_free(fc->destroy_req);
  553. fc->release(fc);
  554. }
  555. }
  556. EXPORT_SYMBOL_GPL(fuse_conn_put);
  557. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  558. {
  559. atomic_inc(&fc->count);
  560. return fc;
  561. }
  562. EXPORT_SYMBOL_GPL(fuse_conn_get);
  563. static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
  564. {
  565. struct fuse_attr attr;
  566. memset(&attr, 0, sizeof(attr));
  567. attr.mode = mode;
  568. attr.ino = FUSE_ROOT_ID;
  569. attr.nlink = 1;
  570. return fuse_iget(sb, 1, 0, &attr, 0, 0);
  571. }
  572. struct fuse_inode_handle {
  573. u64 nodeid;
  574. u32 generation;
  575. };
  576. static struct dentry *fuse_get_dentry(struct super_block *sb,
  577. struct fuse_inode_handle *handle)
  578. {
  579. struct fuse_conn *fc = get_fuse_conn_super(sb);
  580. struct inode *inode;
  581. struct dentry *entry;
  582. int err = -ESTALE;
  583. if (handle->nodeid == 0)
  584. goto out_err;
  585. inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
  586. if (!inode) {
  587. struct fuse_entry_out outarg;
  588. const struct qstr name = QSTR_INIT(".", 1);
  589. if (!fc->export_support)
  590. goto out_err;
  591. err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
  592. &inode);
  593. if (err && err != -ENOENT)
  594. goto out_err;
  595. if (err || !inode) {
  596. err = -ESTALE;
  597. goto out_err;
  598. }
  599. err = -EIO;
  600. if (get_node_id(inode) != handle->nodeid)
  601. goto out_iput;
  602. }
  603. err = -ESTALE;
  604. if (inode->i_generation != handle->generation)
  605. goto out_iput;
  606. entry = d_obtain_alias(inode);
  607. if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
  608. fuse_invalidate_entry_cache(entry);
  609. return entry;
  610. out_iput:
  611. iput(inode);
  612. out_err:
  613. return ERR_PTR(err);
  614. }
  615. static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
  616. struct inode *parent)
  617. {
  618. int len = parent ? 6 : 3;
  619. u64 nodeid;
  620. u32 generation;
  621. if (*max_len < len) {
  622. *max_len = len;
  623. return FILEID_INVALID;
  624. }
  625. nodeid = get_fuse_inode(inode)->nodeid;
  626. generation = inode->i_generation;
  627. fh[0] = (u32)(nodeid >> 32);
  628. fh[1] = (u32)(nodeid & 0xffffffff);
  629. fh[2] = generation;
  630. if (parent) {
  631. nodeid = get_fuse_inode(parent)->nodeid;
  632. generation = parent->i_generation;
  633. fh[3] = (u32)(nodeid >> 32);
  634. fh[4] = (u32)(nodeid & 0xffffffff);
  635. fh[5] = generation;
  636. }
  637. *max_len = len;
  638. return parent ? 0x82 : 0x81;
  639. }
  640. static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
  641. struct fid *fid, int fh_len, int fh_type)
  642. {
  643. struct fuse_inode_handle handle;
  644. if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
  645. return NULL;
  646. handle.nodeid = (u64) fid->raw[0] << 32;
  647. handle.nodeid |= (u64) fid->raw[1];
  648. handle.generation = fid->raw[2];
  649. return fuse_get_dentry(sb, &handle);
  650. }
  651. static struct dentry *fuse_fh_to_parent(struct super_block *sb,
  652. struct fid *fid, int fh_len, int fh_type)
  653. {
  654. struct fuse_inode_handle parent;
  655. if (fh_type != 0x82 || fh_len < 6)
  656. return NULL;
  657. parent.nodeid = (u64) fid->raw[3] << 32;
  658. parent.nodeid |= (u64) fid->raw[4];
  659. parent.generation = fid->raw[5];
  660. return fuse_get_dentry(sb, &parent);
  661. }
  662. static struct dentry *fuse_get_parent(struct dentry *child)
  663. {
  664. struct inode *child_inode = d_inode(child);
  665. struct fuse_conn *fc = get_fuse_conn(child_inode);
  666. struct inode *inode;
  667. struct dentry *parent;
  668. struct fuse_entry_out outarg;
  669. const struct qstr name = QSTR_INIT("..", 2);
  670. int err;
  671. if (!fc->export_support)
  672. return ERR_PTR(-ESTALE);
  673. err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
  674. &name, &outarg, &inode);
  675. if (err) {
  676. if (err == -ENOENT)
  677. return ERR_PTR(-ESTALE);
  678. return ERR_PTR(err);
  679. }
  680. parent = d_obtain_alias(inode);
  681. if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
  682. fuse_invalidate_entry_cache(parent);
  683. return parent;
  684. }
  685. static const struct export_operations fuse_export_operations = {
  686. .fh_to_dentry = fuse_fh_to_dentry,
  687. .fh_to_parent = fuse_fh_to_parent,
  688. .encode_fh = fuse_encode_fh,
  689. .get_parent = fuse_get_parent,
  690. };
  691. static const struct super_operations fuse_super_operations = {
  692. .alloc_inode = fuse_alloc_inode,
  693. .destroy_inode = fuse_destroy_inode,
  694. .evict_inode = fuse_evict_inode,
  695. .write_inode = fuse_write_inode,
  696. .drop_inode = generic_delete_inode,
  697. .remount_fs = fuse_remount_fs,
  698. .put_super = fuse_put_super,
  699. .umount_begin = fuse_umount_begin,
  700. .statfs = fuse_statfs,
  701. .show_options = fuse_show_options,
  702. };
  703. static void sanitize_global_limit(unsigned *limit)
  704. {
  705. if (*limit == 0)
  706. *limit = ((totalram_pages << PAGE_SHIFT) >> 13) /
  707. sizeof(struct fuse_req);
  708. if (*limit >= 1 << 16)
  709. *limit = (1 << 16) - 1;
  710. }
  711. static int set_global_limit(const char *val, struct kernel_param *kp)
  712. {
  713. int rv;
  714. rv = param_set_uint(val, kp);
  715. if (rv)
  716. return rv;
  717. sanitize_global_limit((unsigned *)kp->arg);
  718. return 0;
  719. }
  720. static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
  721. {
  722. int cap_sys_admin = capable(CAP_SYS_ADMIN);
  723. if (arg->minor < 13)
  724. return;
  725. sanitize_global_limit(&max_user_bgreq);
  726. sanitize_global_limit(&max_user_congthresh);
  727. if (arg->max_background) {
  728. fc->max_background = arg->max_background;
  729. if (!cap_sys_admin && fc->max_background > max_user_bgreq)
  730. fc->max_background = max_user_bgreq;
  731. }
  732. if (arg->congestion_threshold) {
  733. fc->congestion_threshold = arg->congestion_threshold;
  734. if (!cap_sys_admin &&
  735. fc->congestion_threshold > max_user_congthresh)
  736. fc->congestion_threshold = max_user_congthresh;
  737. }
  738. }
  739. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  740. {
  741. struct fuse_init_out *arg = &req->misc.init_out;
  742. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  743. fc->conn_error = 1;
  744. else {
  745. unsigned long ra_pages;
  746. process_init_limits(fc, arg);
  747. if (arg->minor >= 6) {
  748. ra_pages = arg->max_readahead / PAGE_SIZE;
  749. if (arg->flags & FUSE_ASYNC_READ)
  750. fc->async_read = 1;
  751. if (!(arg->flags & FUSE_POSIX_LOCKS))
  752. fc->no_lock = 1;
  753. if (arg->minor >= 17) {
  754. if (!(arg->flags & FUSE_FLOCK_LOCKS))
  755. fc->no_flock = 1;
  756. } else {
  757. if (!(arg->flags & FUSE_POSIX_LOCKS))
  758. fc->no_flock = 1;
  759. }
  760. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  761. fc->atomic_o_trunc = 1;
  762. if (arg->minor >= 9) {
  763. /* LOOKUP has dependency on proto version */
  764. if (arg->flags & FUSE_EXPORT_SUPPORT)
  765. fc->export_support = 1;
  766. }
  767. if (arg->flags & FUSE_BIG_WRITES)
  768. fc->big_writes = 1;
  769. if (arg->flags & FUSE_DONT_MASK)
  770. fc->dont_mask = 1;
  771. if (arg->flags & FUSE_AUTO_INVAL_DATA)
  772. fc->auto_inval_data = 1;
  773. if (arg->flags & FUSE_DO_READDIRPLUS) {
  774. fc->do_readdirplus = 1;
  775. if (arg->flags & FUSE_READDIRPLUS_AUTO)
  776. fc->readdirplus_auto = 1;
  777. }
  778. if (arg->flags & FUSE_ASYNC_DIO)
  779. fc->async_dio = 1;
  780. if (arg->flags & FUSE_WRITEBACK_CACHE)
  781. fc->writeback_cache = 1;
  782. if (arg->flags & FUSE_PARALLEL_DIROPS)
  783. fc->parallel_dirops = 1;
  784. if (arg->flags & FUSE_HANDLE_KILLPRIV)
  785. fc->handle_killpriv = 1;
  786. if (arg->time_gran && arg->time_gran <= 1000000000)
  787. fc->sb->s_time_gran = arg->time_gran;
  788. if ((arg->flags & FUSE_POSIX_ACL)) {
  789. fc->default_permissions = 1;
  790. fc->posix_acl = 1;
  791. fc->sb->s_xattr = fuse_acl_xattr_handlers;
  792. }
  793. } else {
  794. ra_pages = fc->max_read / PAGE_SIZE;
  795. fc->no_lock = 1;
  796. fc->no_flock = 1;
  797. }
  798. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  799. fc->minor = arg->minor;
  800. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  801. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  802. fc->conn_init = 1;
  803. }
  804. fuse_set_initialized(fc);
  805. wake_up_all(&fc->blocked_waitq);
  806. }
  807. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  808. {
  809. struct fuse_init_in *arg = &req->misc.init_in;
  810. arg->major = FUSE_KERNEL_VERSION;
  811. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  812. arg->max_readahead = fc->bdi.ra_pages * PAGE_SIZE;
  813. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  814. FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
  815. FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
  816. FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
  817. FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
  818. FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
  819. FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL;
  820. req->in.h.opcode = FUSE_INIT;
  821. req->in.numargs = 1;
  822. req->in.args[0].size = sizeof(*arg);
  823. req->in.args[0].value = arg;
  824. req->out.numargs = 1;
  825. /* Variable length argument used for backward compatibility
  826. with interface version < 7.5. Rest of init_out is zeroed
  827. by do_get_request(), so a short reply is not a problem */
  828. req->out.argvar = 1;
  829. req->out.args[0].size = sizeof(struct fuse_init_out);
  830. req->out.args[0].value = &req->misc.init_out;
  831. req->end = process_init_reply;
  832. fuse_request_send_background(fc, req);
  833. }
  834. static void fuse_free_conn(struct fuse_conn *fc)
  835. {
  836. WARN_ON(!list_empty(&fc->devices));
  837. kfree_rcu(fc, rcu);
  838. }
  839. static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
  840. {
  841. int err;
  842. fc->bdi.name = "fuse";
  843. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
  844. /* fuse does it's own writeback accounting */
  845. fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB | BDI_CAP_STRICTLIMIT;
  846. err = bdi_init(&fc->bdi);
  847. if (err)
  848. return err;
  849. fc->bdi_initialized = 1;
  850. if (sb->s_bdev) {
  851. err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
  852. MAJOR(fc->dev), MINOR(fc->dev));
  853. } else {
  854. err = bdi_register_dev(&fc->bdi, fc->dev);
  855. }
  856. if (err)
  857. return err;
  858. /*
  859. * For a single fuse filesystem use max 1% of dirty +
  860. * writeback threshold.
  861. *
  862. * This gives about 1M of write buffer for memory maps on a
  863. * machine with 1G and 10% dirty_ratio, which should be more
  864. * than enough.
  865. *
  866. * Privileged users can raise it by writing to
  867. *
  868. * /sys/class/bdi/<bdi>/max_ratio
  869. */
  870. bdi_set_max_ratio(&fc->bdi, 1);
  871. return 0;
  872. }
  873. struct fuse_dev *fuse_dev_alloc(struct fuse_conn *fc)
  874. {
  875. struct fuse_dev *fud;
  876. fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
  877. if (fud) {
  878. fud->fc = fuse_conn_get(fc);
  879. fuse_pqueue_init(&fud->pq);
  880. spin_lock(&fc->lock);
  881. list_add_tail(&fud->entry, &fc->devices);
  882. spin_unlock(&fc->lock);
  883. }
  884. return fud;
  885. }
  886. EXPORT_SYMBOL_GPL(fuse_dev_alloc);
  887. void fuse_dev_free(struct fuse_dev *fud)
  888. {
  889. struct fuse_conn *fc = fud->fc;
  890. if (fc) {
  891. spin_lock(&fc->lock);
  892. list_del(&fud->entry);
  893. spin_unlock(&fc->lock);
  894. fuse_conn_put(fc);
  895. }
  896. kfree(fud);
  897. }
  898. EXPORT_SYMBOL_GPL(fuse_dev_free);
  899. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  900. {
  901. struct fuse_dev *fud;
  902. struct fuse_conn *fc;
  903. struct inode *root;
  904. struct fuse_mount_data d;
  905. struct file *file;
  906. struct dentry *root_dentry;
  907. struct fuse_req *init_req;
  908. int err;
  909. int is_bdev = sb->s_bdev != NULL;
  910. err = -EINVAL;
  911. if (sb->s_flags & MS_MANDLOCK)
  912. goto err;
  913. sb->s_flags &= ~(MS_NOSEC | MS_I_VERSION);
  914. if (!parse_fuse_opt(data, &d, is_bdev))
  915. goto err;
  916. if (is_bdev) {
  917. #ifdef CONFIG_BLOCK
  918. err = -EINVAL;
  919. if (!sb_set_blocksize(sb, d.blksize))
  920. goto err;
  921. #endif
  922. } else {
  923. sb->s_blocksize = PAGE_SIZE;
  924. sb->s_blocksize_bits = PAGE_SHIFT;
  925. }
  926. sb->s_magic = FUSE_SUPER_MAGIC;
  927. sb->s_op = &fuse_super_operations;
  928. sb->s_xattr = fuse_xattr_handlers;
  929. sb->s_maxbytes = MAX_LFS_FILESIZE;
  930. sb->s_time_gran = 1;
  931. sb->s_export_op = &fuse_export_operations;
  932. file = fget(d.fd);
  933. err = -EINVAL;
  934. if (!file)
  935. goto err;
  936. if ((file->f_op != &fuse_dev_operations) ||
  937. (file->f_cred->user_ns != &init_user_ns))
  938. goto err_fput;
  939. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  940. err = -ENOMEM;
  941. if (!fc)
  942. goto err_fput;
  943. fuse_conn_init(fc);
  944. fc->release = fuse_free_conn;
  945. fud = fuse_dev_alloc(fc);
  946. if (!fud)
  947. goto err_put_conn;
  948. fc->dev = sb->s_dev;
  949. fc->sb = sb;
  950. err = fuse_bdi_init(fc, sb);
  951. if (err)
  952. goto err_dev_free;
  953. sb->s_bdi = &fc->bdi;
  954. /* Handle umasking inside the fuse code */
  955. if (sb->s_flags & MS_POSIXACL)
  956. fc->dont_mask = 1;
  957. sb->s_flags |= MS_POSIXACL;
  958. fc->default_permissions = d.default_permissions;
  959. fc->allow_other = d.allow_other;
  960. fc->user_id = d.user_id;
  961. fc->group_id = d.group_id;
  962. fc->max_read = max_t(unsigned, 4096, d.max_read);
  963. /* Used by get_root_inode() */
  964. sb->s_fs_info = fc;
  965. err = -ENOMEM;
  966. root = fuse_get_root_inode(sb, d.rootmode);
  967. sb->s_d_op = &fuse_root_dentry_operations;
  968. root_dentry = d_make_root(root);
  969. if (!root_dentry)
  970. goto err_dev_free;
  971. /* Root dentry doesn't have .d_revalidate */
  972. sb->s_d_op = &fuse_dentry_operations;
  973. init_req = fuse_request_alloc(0);
  974. if (!init_req)
  975. goto err_put_root;
  976. __set_bit(FR_BACKGROUND, &init_req->flags);
  977. if (is_bdev) {
  978. fc->destroy_req = fuse_request_alloc(0);
  979. if (!fc->destroy_req)
  980. goto err_free_init_req;
  981. }
  982. mutex_lock(&fuse_mutex);
  983. err = -EINVAL;
  984. if (file->private_data)
  985. goto err_unlock;
  986. err = fuse_ctl_add_conn(fc);
  987. if (err)
  988. goto err_unlock;
  989. list_add_tail(&fc->entry, &fuse_conn_list);
  990. sb->s_root = root_dentry;
  991. file->private_data = fud;
  992. mutex_unlock(&fuse_mutex);
  993. /*
  994. * atomic_dec_and_test() in fput() provides the necessary
  995. * memory barrier for file->private_data to be visible on all
  996. * CPUs after this
  997. */
  998. fput(file);
  999. fuse_send_init(fc, init_req);
  1000. return 0;
  1001. err_unlock:
  1002. mutex_unlock(&fuse_mutex);
  1003. err_free_init_req:
  1004. fuse_request_free(init_req);
  1005. err_put_root:
  1006. dput(root_dentry);
  1007. err_dev_free:
  1008. fuse_dev_free(fud);
  1009. err_put_conn:
  1010. fuse_bdi_destroy(fc);
  1011. fuse_conn_put(fc);
  1012. err_fput:
  1013. fput(file);
  1014. err:
  1015. return err;
  1016. }
  1017. static struct dentry *fuse_mount(struct file_system_type *fs_type,
  1018. int flags, const char *dev_name,
  1019. void *raw_data)
  1020. {
  1021. return mount_nodev(fs_type, flags, raw_data, fuse_fill_super);
  1022. }
  1023. static void fuse_kill_sb_anon(struct super_block *sb)
  1024. {
  1025. struct fuse_conn *fc = get_fuse_conn_super(sb);
  1026. if (fc) {
  1027. down_write(&fc->killsb);
  1028. fc->sb = NULL;
  1029. up_write(&fc->killsb);
  1030. }
  1031. kill_anon_super(sb);
  1032. }
  1033. static struct file_system_type fuse_fs_type = {
  1034. .owner = THIS_MODULE,
  1035. .name = "fuse",
  1036. .fs_flags = FS_HAS_SUBTYPE,
  1037. .mount = fuse_mount,
  1038. .kill_sb = fuse_kill_sb_anon,
  1039. };
  1040. MODULE_ALIAS_FS("fuse");
  1041. #ifdef CONFIG_BLOCK
  1042. static struct dentry *fuse_mount_blk(struct file_system_type *fs_type,
  1043. int flags, const char *dev_name,
  1044. void *raw_data)
  1045. {
  1046. return mount_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super);
  1047. }
  1048. static void fuse_kill_sb_blk(struct super_block *sb)
  1049. {
  1050. struct fuse_conn *fc = get_fuse_conn_super(sb);
  1051. if (fc) {
  1052. down_write(&fc->killsb);
  1053. fc->sb = NULL;
  1054. up_write(&fc->killsb);
  1055. }
  1056. kill_block_super(sb);
  1057. }
  1058. static struct file_system_type fuseblk_fs_type = {
  1059. .owner = THIS_MODULE,
  1060. .name = "fuseblk",
  1061. .mount = fuse_mount_blk,
  1062. .kill_sb = fuse_kill_sb_blk,
  1063. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  1064. };
  1065. MODULE_ALIAS_FS("fuseblk");
  1066. static inline int register_fuseblk(void)
  1067. {
  1068. return register_filesystem(&fuseblk_fs_type);
  1069. }
  1070. static inline void unregister_fuseblk(void)
  1071. {
  1072. unregister_filesystem(&fuseblk_fs_type);
  1073. }
  1074. #else
  1075. static inline int register_fuseblk(void)
  1076. {
  1077. return 0;
  1078. }
  1079. static inline void unregister_fuseblk(void)
  1080. {
  1081. }
  1082. #endif
  1083. static void fuse_inode_init_once(void *foo)
  1084. {
  1085. struct inode *inode = foo;
  1086. inode_init_once(inode);
  1087. }
  1088. static int __init fuse_fs_init(void)
  1089. {
  1090. int err;
  1091. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  1092. sizeof(struct fuse_inode), 0,
  1093. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
  1094. fuse_inode_init_once);
  1095. err = -ENOMEM;
  1096. if (!fuse_inode_cachep)
  1097. goto out;
  1098. err = register_fuseblk();
  1099. if (err)
  1100. goto out2;
  1101. err = register_filesystem(&fuse_fs_type);
  1102. if (err)
  1103. goto out3;
  1104. return 0;
  1105. out3:
  1106. unregister_fuseblk();
  1107. out2:
  1108. kmem_cache_destroy(fuse_inode_cachep);
  1109. out:
  1110. return err;
  1111. }
  1112. static void fuse_fs_cleanup(void)
  1113. {
  1114. unregister_filesystem(&fuse_fs_type);
  1115. unregister_fuseblk();
  1116. /*
  1117. * Make sure all delayed rcu free inodes are flushed before we
  1118. * destroy cache.
  1119. */
  1120. rcu_barrier();
  1121. kmem_cache_destroy(fuse_inode_cachep);
  1122. }
  1123. static struct kobject *fuse_kobj;
  1124. static int fuse_sysfs_init(void)
  1125. {
  1126. int err;
  1127. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  1128. if (!fuse_kobj) {
  1129. err = -ENOMEM;
  1130. goto out_err;
  1131. }
  1132. err = sysfs_create_mount_point(fuse_kobj, "connections");
  1133. if (err)
  1134. goto out_fuse_unregister;
  1135. return 0;
  1136. out_fuse_unregister:
  1137. kobject_put(fuse_kobj);
  1138. out_err:
  1139. return err;
  1140. }
  1141. static void fuse_sysfs_cleanup(void)
  1142. {
  1143. sysfs_remove_mount_point(fuse_kobj, "connections");
  1144. kobject_put(fuse_kobj);
  1145. }
  1146. static int __init fuse_init(void)
  1147. {
  1148. int res;
  1149. printk(KERN_INFO "fuse init (API version %i.%i)\n",
  1150. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  1151. INIT_LIST_HEAD(&fuse_conn_list);
  1152. res = fuse_fs_init();
  1153. if (res)
  1154. goto err;
  1155. res = fuse_dev_init();
  1156. if (res)
  1157. goto err_fs_cleanup;
  1158. res = fuse_sysfs_init();
  1159. if (res)
  1160. goto err_dev_cleanup;
  1161. res = fuse_ctl_init();
  1162. if (res)
  1163. goto err_sysfs_cleanup;
  1164. sanitize_global_limit(&max_user_bgreq);
  1165. sanitize_global_limit(&max_user_congthresh);
  1166. return 0;
  1167. err_sysfs_cleanup:
  1168. fuse_sysfs_cleanup();
  1169. err_dev_cleanup:
  1170. fuse_dev_cleanup();
  1171. err_fs_cleanup:
  1172. fuse_fs_cleanup();
  1173. err:
  1174. return res;
  1175. }
  1176. static void __exit fuse_exit(void)
  1177. {
  1178. printk(KERN_DEBUG "fuse exit\n");
  1179. fuse_ctl_cleanup();
  1180. fuse_sysfs_cleanup();
  1181. fuse_fs_cleanup();
  1182. fuse_dev_cleanup();
  1183. }
  1184. module_init(fuse_init);
  1185. module_exit(fuse_exit);