inode.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 Miklos Szeredi <[email protected]>
  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 <[email protected]>");
  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, const 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. bool fuse_lock_inode(struct inode *inode)
  310. {
  311. bool locked = false;
  312. if (!get_fuse_conn(inode)->parallel_dirops) {
  313. mutex_lock(&get_fuse_inode(inode)->mutex);
  314. locked = true;
  315. }
  316. return locked;
  317. }
  318. void fuse_unlock_inode(struct inode *inode, bool locked)
  319. {
  320. if (locked)
  321. mutex_unlock(&get_fuse_inode(inode)->mutex);
  322. }
  323. static void fuse_umount_begin(struct super_block *sb)
  324. {
  325. fuse_abort_conn(get_fuse_conn_super(sb));
  326. }
  327. static void fuse_send_destroy(struct fuse_conn *fc)
  328. {
  329. struct fuse_req *req = fc->destroy_req;
  330. if (req && fc->conn_init) {
  331. fc->destroy_req = NULL;
  332. req->in.h.opcode = FUSE_DESTROY;
  333. __set_bit(FR_FORCE, &req->flags);
  334. __clear_bit(FR_BACKGROUND, &req->flags);
  335. fuse_request_send(fc, req);
  336. fuse_put_request(fc, req);
  337. }
  338. }
  339. static void fuse_bdi_destroy(struct fuse_conn *fc)
  340. {
  341. if (fc->bdi_initialized)
  342. bdi_destroy(&fc->bdi);
  343. }
  344. static void fuse_put_super(struct super_block *sb)
  345. {
  346. struct fuse_conn *fc = get_fuse_conn_super(sb);
  347. mutex_lock(&fuse_mutex);
  348. list_del(&fc->entry);
  349. fuse_ctl_remove_conn(fc);
  350. mutex_unlock(&fuse_mutex);
  351. fuse_bdi_destroy(fc);
  352. fuse_conn_put(fc);
  353. }
  354. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  355. {
  356. stbuf->f_type = FUSE_SUPER_MAGIC;
  357. stbuf->f_bsize = attr->bsize;
  358. stbuf->f_frsize = attr->frsize;
  359. stbuf->f_blocks = attr->blocks;
  360. stbuf->f_bfree = attr->bfree;
  361. stbuf->f_bavail = attr->bavail;
  362. stbuf->f_files = attr->files;
  363. stbuf->f_ffree = attr->ffree;
  364. stbuf->f_namelen = attr->namelen;
  365. /* fsid is left zero */
  366. }
  367. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  368. {
  369. struct super_block *sb = dentry->d_sb;
  370. struct fuse_conn *fc = get_fuse_conn_super(sb);
  371. FUSE_ARGS(args);
  372. struct fuse_statfs_out outarg;
  373. int err;
  374. if (!fuse_allow_current_process(fc)) {
  375. buf->f_type = FUSE_SUPER_MAGIC;
  376. return 0;
  377. }
  378. memset(&outarg, 0, sizeof(outarg));
  379. args.in.numargs = 0;
  380. args.in.h.opcode = FUSE_STATFS;
  381. args.in.h.nodeid = get_node_id(d_inode(dentry));
  382. args.out.numargs = 1;
  383. args.out.args[0].size = sizeof(outarg);
  384. args.out.args[0].value = &outarg;
  385. err = fuse_simple_request(fc, &args);
  386. if (!err)
  387. convert_fuse_statfs(buf, &outarg.st);
  388. return err;
  389. }
  390. enum {
  391. OPT_FD,
  392. OPT_ROOTMODE,
  393. OPT_USER_ID,
  394. OPT_GROUP_ID,
  395. OPT_DEFAULT_PERMISSIONS,
  396. OPT_ALLOW_OTHER,
  397. OPT_MAX_READ,
  398. OPT_BLKSIZE,
  399. OPT_ERR
  400. };
  401. static const match_table_t tokens = {
  402. {OPT_FD, "fd=%u"},
  403. {OPT_ROOTMODE, "rootmode=%o"},
  404. {OPT_USER_ID, "user_id=%u"},
  405. {OPT_GROUP_ID, "group_id=%u"},
  406. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  407. {OPT_ALLOW_OTHER, "allow_other"},
  408. {OPT_MAX_READ, "max_read=%u"},
  409. {OPT_BLKSIZE, "blksize=%u"},
  410. {OPT_ERR, NULL}
  411. };
  412. static int fuse_match_uint(substring_t *s, unsigned int *res)
  413. {
  414. int err = -ENOMEM;
  415. char *buf = match_strdup(s);
  416. if (buf) {
  417. err = kstrtouint(buf, 10, res);
  418. kfree(buf);
  419. }
  420. return err;
  421. }
  422. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
  423. {
  424. char *p;
  425. memset(d, 0, sizeof(struct fuse_mount_data));
  426. d->max_read = ~0;
  427. d->blksize = FUSE_DEFAULT_BLKSIZE;
  428. while ((p = strsep(&opt, ",")) != NULL) {
  429. int token;
  430. int value;
  431. unsigned uv;
  432. substring_t args[MAX_OPT_ARGS];
  433. if (!*p)
  434. continue;
  435. token = match_token(p, tokens, args);
  436. switch (token) {
  437. case OPT_FD:
  438. if (match_int(&args[0], &value))
  439. return 0;
  440. d->fd = value;
  441. d->fd_present = 1;
  442. break;
  443. case OPT_ROOTMODE:
  444. if (match_octal(&args[0], &value))
  445. return 0;
  446. if (!fuse_valid_type(value))
  447. return 0;
  448. d->rootmode = value;
  449. d->rootmode_present = 1;
  450. break;
  451. case OPT_USER_ID:
  452. if (fuse_match_uint(&args[0], &uv))
  453. return 0;
  454. d->user_id = make_kuid(current_user_ns(), uv);
  455. if (!uid_valid(d->user_id))
  456. return 0;
  457. d->user_id_present = 1;
  458. break;
  459. case OPT_GROUP_ID:
  460. if (fuse_match_uint(&args[0], &uv))
  461. return 0;
  462. d->group_id = make_kgid(current_user_ns(), uv);
  463. if (!gid_valid(d->group_id))
  464. return 0;
  465. d->group_id_present = 1;
  466. break;
  467. case OPT_DEFAULT_PERMISSIONS:
  468. d->default_permissions = 1;
  469. break;
  470. case OPT_ALLOW_OTHER:
  471. d->allow_other = 1;
  472. break;
  473. case OPT_MAX_READ:
  474. if (match_int(&args[0], &value))
  475. return 0;
  476. d->max_read = value;
  477. break;
  478. case OPT_BLKSIZE:
  479. if (!is_bdev || match_int(&args[0], &value))
  480. return 0;
  481. d->blksize = value;
  482. break;
  483. default:
  484. return 0;
  485. }
  486. }
  487. if (!d->fd_present || !d->rootmode_present ||
  488. !d->user_id_present || !d->group_id_present)
  489. return 0;
  490. return 1;
  491. }
  492. static int fuse_show_options(struct seq_file *m, struct dentry *root)
  493. {
  494. struct super_block *sb = root->d_sb;
  495. struct fuse_conn *fc = get_fuse_conn_super(sb);
  496. seq_printf(m, ",user_id=%u", from_kuid_munged(&init_user_ns, fc->user_id));
  497. seq_printf(m, ",group_id=%u", from_kgid_munged(&init_user_ns, fc->group_id));
  498. if (fc->default_permissions)
  499. seq_puts(m, ",default_permissions");
  500. if (fc->allow_other)
  501. seq_puts(m, ",allow_other");
  502. if (fc->max_read != ~0)
  503. seq_printf(m, ",max_read=%u", fc->max_read);
  504. if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
  505. seq_printf(m, ",blksize=%lu", sb->s_blocksize);
  506. return 0;
  507. }
  508. static void fuse_iqueue_init(struct fuse_iqueue *fiq)
  509. {
  510. memset(fiq, 0, sizeof(struct fuse_iqueue));
  511. init_waitqueue_head(&fiq->waitq);
  512. INIT_LIST_HEAD(&fiq->pending);
  513. INIT_LIST_HEAD(&fiq->interrupts);
  514. fiq->forget_list_tail = &fiq->forget_list_head;
  515. fiq->connected = 1;
  516. }
  517. static void fuse_pqueue_init(struct fuse_pqueue *fpq)
  518. {
  519. memset(fpq, 0, sizeof(struct fuse_pqueue));
  520. spin_lock_init(&fpq->lock);
  521. INIT_LIST_HEAD(&fpq->processing);
  522. INIT_LIST_HEAD(&fpq->io);
  523. fpq->connected = 1;
  524. }
  525. void fuse_conn_init(struct fuse_conn *fc)
  526. {
  527. memset(fc, 0, sizeof(*fc));
  528. spin_lock_init(&fc->lock);
  529. init_rwsem(&fc->killsb);
  530. atomic_set(&fc->count, 1);
  531. atomic_set(&fc->dev_count, 1);
  532. init_waitqueue_head(&fc->blocked_waitq);
  533. init_waitqueue_head(&fc->reserved_req_waitq);
  534. fuse_iqueue_init(&fc->iq);
  535. INIT_LIST_HEAD(&fc->bg_queue);
  536. INIT_LIST_HEAD(&fc->entry);
  537. INIT_LIST_HEAD(&fc->devices);
  538. atomic_set(&fc->num_waiting, 0);
  539. fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
  540. fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
  541. fc->khctr = 0;
  542. fc->polled_files = RB_ROOT;
  543. fc->blocked = 0;
  544. fc->initialized = 0;
  545. fc->connected = 1;
  546. fc->attr_version = 1;
  547. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  548. }
  549. EXPORT_SYMBOL_GPL(fuse_conn_init);
  550. void fuse_conn_put(struct fuse_conn *fc)
  551. {
  552. if (atomic_dec_and_test(&fc->count)) {
  553. if (fc->destroy_req)
  554. fuse_request_free(fc->destroy_req);
  555. fc->release(fc);
  556. }
  557. }
  558. EXPORT_SYMBOL_GPL(fuse_conn_put);
  559. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  560. {
  561. atomic_inc(&fc->count);
  562. return fc;
  563. }
  564. EXPORT_SYMBOL_GPL(fuse_conn_get);
  565. static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
  566. {
  567. struct fuse_attr attr;
  568. memset(&attr, 0, sizeof(attr));
  569. attr.mode = mode;
  570. attr.ino = FUSE_ROOT_ID;
  571. attr.nlink = 1;
  572. return fuse_iget(sb, 1, 0, &attr, 0, 0);
  573. }
  574. struct fuse_inode_handle {
  575. u64 nodeid;
  576. u32 generation;
  577. };
  578. static struct dentry *fuse_get_dentry(struct super_block *sb,
  579. struct fuse_inode_handle *handle)
  580. {
  581. struct fuse_conn *fc = get_fuse_conn_super(sb);
  582. struct inode *inode;
  583. struct dentry *entry;
  584. int err = -ESTALE;
  585. if (handle->nodeid == 0)
  586. goto out_err;
  587. inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
  588. if (!inode) {
  589. struct fuse_entry_out outarg;
  590. const struct qstr name = QSTR_INIT(".", 1);
  591. if (!fc->export_support)
  592. goto out_err;
  593. err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
  594. &inode);
  595. if (err && err != -ENOENT)
  596. goto out_err;
  597. if (err || !inode) {
  598. err = -ESTALE;
  599. goto out_err;
  600. }
  601. err = -EIO;
  602. if (get_node_id(inode) != handle->nodeid)
  603. goto out_iput;
  604. }
  605. err = -ESTALE;
  606. if (inode->i_generation != handle->generation)
  607. goto out_iput;
  608. entry = d_obtain_alias(inode);
  609. if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
  610. fuse_invalidate_entry_cache(entry);
  611. return entry;
  612. out_iput:
  613. iput(inode);
  614. out_err:
  615. return ERR_PTR(err);
  616. }
  617. static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
  618. struct inode *parent)
  619. {
  620. int len = parent ? 6 : 3;
  621. u64 nodeid;
  622. u32 generation;
  623. if (*max_len < len) {
  624. *max_len = len;
  625. return FILEID_INVALID;
  626. }
  627. nodeid = get_fuse_inode(inode)->nodeid;
  628. generation = inode->i_generation;
  629. fh[0] = (u32)(nodeid >> 32);
  630. fh[1] = (u32)(nodeid & 0xffffffff);
  631. fh[2] = generation;
  632. if (parent) {
  633. nodeid = get_fuse_inode(parent)->nodeid;
  634. generation = parent->i_generation;
  635. fh[3] = (u32)(nodeid >> 32);
  636. fh[4] = (u32)(nodeid & 0xffffffff);
  637. fh[5] = generation;
  638. }
  639. *max_len = len;
  640. return parent ? 0x82 : 0x81;
  641. }
  642. static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
  643. struct fid *fid, int fh_len, int fh_type)
  644. {
  645. struct fuse_inode_handle handle;
  646. if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
  647. return NULL;
  648. handle.nodeid = (u64) fid->raw[0] << 32;
  649. handle.nodeid |= (u64) fid->raw[1];
  650. handle.generation = fid->raw[2];
  651. return fuse_get_dentry(sb, &handle);
  652. }
  653. static struct dentry *fuse_fh_to_parent(struct super_block *sb,
  654. struct fid *fid, int fh_len, int fh_type)
  655. {
  656. struct fuse_inode_handle parent;
  657. if (fh_type != 0x82 || fh_len < 6)
  658. return NULL;
  659. parent.nodeid = (u64) fid->raw[3] << 32;
  660. parent.nodeid |= (u64) fid->raw[4];
  661. parent.generation = fid->raw[5];
  662. return fuse_get_dentry(sb, &parent);
  663. }
  664. static struct dentry *fuse_get_parent(struct dentry *child)
  665. {
  666. struct inode *child_inode = d_inode(child);
  667. struct fuse_conn *fc = get_fuse_conn(child_inode);
  668. struct inode *inode;
  669. struct dentry *parent;
  670. struct fuse_entry_out outarg;
  671. const struct qstr name = QSTR_INIT("..", 2);
  672. int err;
  673. if (!fc->export_support)
  674. return ERR_PTR(-ESTALE);
  675. err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
  676. &name, &outarg, &inode);
  677. if (err) {
  678. if (err == -ENOENT)
  679. return ERR_PTR(-ESTALE);
  680. return ERR_PTR(err);
  681. }
  682. parent = d_obtain_alias(inode);
  683. if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
  684. fuse_invalidate_entry_cache(parent);
  685. return parent;
  686. }
  687. static const struct export_operations fuse_export_operations = {
  688. .fh_to_dentry = fuse_fh_to_dentry,
  689. .fh_to_parent = fuse_fh_to_parent,
  690. .encode_fh = fuse_encode_fh,
  691. .get_parent = fuse_get_parent,
  692. };
  693. static const struct super_operations fuse_super_operations = {
  694. .alloc_inode = fuse_alloc_inode,
  695. .destroy_inode = fuse_destroy_inode,
  696. .evict_inode = fuse_evict_inode,
  697. .write_inode = fuse_write_inode,
  698. .drop_inode = generic_delete_inode,
  699. .remount_fs = fuse_remount_fs,
  700. .put_super = fuse_put_super,
  701. .umount_begin = fuse_umount_begin,
  702. .statfs = fuse_statfs,
  703. .show_options = fuse_show_options,
  704. };
  705. static void sanitize_global_limit(unsigned *limit)
  706. {
  707. if (*limit == 0)
  708. *limit = ((totalram_pages << PAGE_SHIFT) >> 13) /
  709. sizeof(struct fuse_req);
  710. if (*limit >= 1 << 16)
  711. *limit = (1 << 16) - 1;
  712. }
  713. static int set_global_limit(const char *val, const struct kernel_param *kp)
  714. {
  715. int rv;
  716. rv = param_set_uint(val, kp);
  717. if (rv)
  718. return rv;
  719. sanitize_global_limit((unsigned *)kp->arg);
  720. return 0;
  721. }
  722. static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
  723. {
  724. int cap_sys_admin = capable(CAP_SYS_ADMIN);
  725. if (arg->minor < 13)
  726. return;
  727. sanitize_global_limit(&max_user_bgreq);
  728. sanitize_global_limit(&max_user_congthresh);
  729. if (arg->max_background) {
  730. fc->max_background = arg->max_background;
  731. if (!cap_sys_admin && fc->max_background > max_user_bgreq)
  732. fc->max_background = max_user_bgreq;
  733. }
  734. if (arg->congestion_threshold) {
  735. fc->congestion_threshold = arg->congestion_threshold;
  736. if (!cap_sys_admin &&
  737. fc->congestion_threshold > max_user_congthresh)
  738. fc->congestion_threshold = max_user_congthresh;
  739. }
  740. }
  741. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  742. {
  743. struct fuse_init_out *arg = &req->misc.init_out;
  744. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  745. fc->conn_error = 1;
  746. else {
  747. unsigned long ra_pages;
  748. process_init_limits(fc, arg);
  749. if (arg->minor >= 6) {
  750. ra_pages = arg->max_readahead / PAGE_SIZE;
  751. if (arg->flags & FUSE_ASYNC_READ)
  752. fc->async_read = 1;
  753. if (!(arg->flags & FUSE_POSIX_LOCKS))
  754. fc->no_lock = 1;
  755. if (arg->minor >= 17) {
  756. if (!(arg->flags & FUSE_FLOCK_LOCKS))
  757. fc->no_flock = 1;
  758. } else {
  759. if (!(arg->flags & FUSE_POSIX_LOCKS))
  760. fc->no_flock = 1;
  761. }
  762. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  763. fc->atomic_o_trunc = 1;
  764. if (arg->minor >= 9) {
  765. /* LOOKUP has dependency on proto version */
  766. if (arg->flags & FUSE_EXPORT_SUPPORT)
  767. fc->export_support = 1;
  768. }
  769. if (arg->flags & FUSE_BIG_WRITES)
  770. fc->big_writes = 1;
  771. if (arg->flags & FUSE_DONT_MASK)
  772. fc->dont_mask = 1;
  773. if (arg->flags & FUSE_AUTO_INVAL_DATA)
  774. fc->auto_inval_data = 1;
  775. if (arg->flags & FUSE_DO_READDIRPLUS) {
  776. fc->do_readdirplus = 1;
  777. if (arg->flags & FUSE_READDIRPLUS_AUTO)
  778. fc->readdirplus_auto = 1;
  779. }
  780. if (arg->flags & FUSE_ASYNC_DIO)
  781. fc->async_dio = 1;
  782. if (arg->flags & FUSE_WRITEBACK_CACHE)
  783. fc->writeback_cache = 1;
  784. if (arg->flags & FUSE_PARALLEL_DIROPS)
  785. fc->parallel_dirops = 1;
  786. if (arg->flags & FUSE_HANDLE_KILLPRIV)
  787. fc->handle_killpriv = 1;
  788. if (arg->flags & FUSE_PASSTHROUGH) {
  789. fc->passthrough = 1;
  790. /* Prevent further stacking */
  791. fc->sb->s_stack_depth =
  792. FILESYSTEM_MAX_STACK_DEPTH;
  793. }
  794. if (arg->time_gran && arg->time_gran <= 1000000000)
  795. fc->sb->s_time_gran = arg->time_gran;
  796. if ((arg->flags & FUSE_POSIX_ACL)) {
  797. fc->default_permissions = 1;
  798. fc->posix_acl = 1;
  799. fc->sb->s_xattr = fuse_acl_xattr_handlers;
  800. }
  801. } else {
  802. ra_pages = fc->max_read / PAGE_SIZE;
  803. fc->no_lock = 1;
  804. fc->no_flock = 1;
  805. }
  806. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  807. fc->minor = arg->minor;
  808. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  809. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  810. fc->conn_init = 1;
  811. }
  812. fuse_set_initialized(fc);
  813. wake_up_all(&fc->blocked_waitq);
  814. }
  815. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  816. {
  817. struct fuse_init_in *arg = &req->misc.init_in;
  818. arg->major = FUSE_KERNEL_VERSION;
  819. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  820. arg->max_readahead = fc->bdi.ra_pages * PAGE_SIZE;
  821. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  822. FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
  823. FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
  824. FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
  825. FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
  826. FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
  827. FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL;
  828. req->in.h.opcode = FUSE_INIT;
  829. req->in.numargs = 1;
  830. req->in.args[0].size = sizeof(*arg);
  831. req->in.args[0].value = arg;
  832. req->out.numargs = 1;
  833. /* Variable length argument used for backward compatibility
  834. with interface version < 7.5. Rest of init_out is zeroed
  835. by do_get_request(), so a short reply is not a problem */
  836. req->out.argvar = 1;
  837. req->out.args[0].size = sizeof(struct fuse_init_out);
  838. req->out.args[0].value = &req->misc.init_out;
  839. req->end = process_init_reply;
  840. fuse_request_send_background(fc, req);
  841. }
  842. static void fuse_free_conn(struct fuse_conn *fc)
  843. {
  844. WARN_ON(!list_empty(&fc->devices));
  845. kfree_rcu(fc, rcu);
  846. }
  847. static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
  848. {
  849. int err;
  850. fc->bdi.name = "fuse";
  851. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
  852. /* fuse does it's own writeback accounting */
  853. fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB | BDI_CAP_STRICTLIMIT;
  854. err = bdi_init(&fc->bdi);
  855. if (err)
  856. return err;
  857. fc->bdi_initialized = 1;
  858. if (sb->s_bdev) {
  859. err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
  860. MAJOR(fc->dev), MINOR(fc->dev));
  861. } else {
  862. err = bdi_register_dev(&fc->bdi, fc->dev);
  863. }
  864. if (err)
  865. return err;
  866. /*
  867. * For a single fuse filesystem use max 1% of dirty +
  868. * writeback threshold.
  869. *
  870. * This gives about 1M of write buffer for memory maps on a
  871. * machine with 1G and 10% dirty_ratio, which should be more
  872. * than enough.
  873. *
  874. * Privileged users can raise it by writing to
  875. *
  876. * /sys/class/bdi/<bdi>/max_ratio
  877. */
  878. bdi_set_max_ratio(&fc->bdi, 1);
  879. return 0;
  880. }
  881. struct fuse_dev *fuse_dev_alloc(struct fuse_conn *fc)
  882. {
  883. struct fuse_dev *fud;
  884. fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
  885. if (fud) {
  886. fud->fc = fuse_conn_get(fc);
  887. fuse_pqueue_init(&fud->pq);
  888. spin_lock(&fc->lock);
  889. list_add_tail(&fud->entry, &fc->devices);
  890. spin_unlock(&fc->lock);
  891. }
  892. return fud;
  893. }
  894. EXPORT_SYMBOL_GPL(fuse_dev_alloc);
  895. void fuse_dev_free(struct fuse_dev *fud)
  896. {
  897. struct fuse_conn *fc = fud->fc;
  898. if (fc) {
  899. spin_lock(&fc->lock);
  900. list_del(&fud->entry);
  901. spin_unlock(&fc->lock);
  902. fuse_conn_put(fc);
  903. }
  904. kfree(fud);
  905. }
  906. EXPORT_SYMBOL_GPL(fuse_dev_free);
  907. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  908. {
  909. struct fuse_dev *fud;
  910. struct fuse_conn *fc;
  911. struct inode *root;
  912. struct fuse_mount_data d;
  913. struct file *file;
  914. struct dentry *root_dentry;
  915. struct fuse_req *init_req;
  916. int err;
  917. int is_bdev = sb->s_bdev != NULL;
  918. err = -EINVAL;
  919. if (sb->s_flags & MS_MANDLOCK)
  920. goto err;
  921. sb->s_flags &= ~(MS_NOSEC | MS_I_VERSION);
  922. if (!parse_fuse_opt(data, &d, is_bdev))
  923. goto err;
  924. if (is_bdev) {
  925. #ifdef CONFIG_BLOCK
  926. err = -EINVAL;
  927. if (!sb_set_blocksize(sb, d.blksize))
  928. goto err;
  929. #endif
  930. } else {
  931. sb->s_blocksize = PAGE_SIZE;
  932. sb->s_blocksize_bits = PAGE_SHIFT;
  933. }
  934. sb->s_magic = FUSE_SUPER_MAGIC;
  935. sb->s_op = &fuse_super_operations;
  936. sb->s_xattr = fuse_xattr_handlers;
  937. sb->s_maxbytes = MAX_LFS_FILESIZE;
  938. sb->s_time_gran = 1;
  939. sb->s_export_op = &fuse_export_operations;
  940. file = fget(d.fd);
  941. err = -EINVAL;
  942. if (!file)
  943. goto err;
  944. if ((file->f_op != &fuse_dev_operations) ||
  945. (file->f_cred->user_ns != &init_user_ns))
  946. goto err_fput;
  947. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  948. err = -ENOMEM;
  949. if (!fc)
  950. goto err_fput;
  951. fuse_conn_init(fc);
  952. fc->release = fuse_free_conn;
  953. fud = fuse_dev_alloc(fc);
  954. if (!fud)
  955. goto err_put_conn;
  956. fc->dev = sb->s_dev;
  957. fc->sb = sb;
  958. err = fuse_bdi_init(fc, sb);
  959. if (err)
  960. goto err_dev_free;
  961. sb->s_bdi = &fc->bdi;
  962. /* Handle umasking inside the fuse code */
  963. if (sb->s_flags & MS_POSIXACL)
  964. fc->dont_mask = 1;
  965. sb->s_flags |= MS_POSIXACL;
  966. fc->default_permissions = d.default_permissions;
  967. fc->allow_other = d.allow_other;
  968. fc->user_id = d.user_id;
  969. fc->group_id = d.group_id;
  970. fc->max_read = max_t(unsigned, 4096, d.max_read);
  971. /* Used by get_root_inode() */
  972. sb->s_fs_info = fc;
  973. err = -ENOMEM;
  974. root = fuse_get_root_inode(sb, d.rootmode);
  975. sb->s_d_op = &fuse_root_dentry_operations;
  976. root_dentry = d_make_root(root);
  977. if (!root_dentry)
  978. goto err_dev_free;
  979. /* Root dentry doesn't have .d_revalidate */
  980. sb->s_d_op = &fuse_dentry_operations;
  981. init_req = fuse_request_alloc(0);
  982. if (!init_req)
  983. goto err_put_root;
  984. __set_bit(FR_BACKGROUND, &init_req->flags);
  985. if (is_bdev) {
  986. fc->destroy_req = fuse_request_alloc(0);
  987. if (!fc->destroy_req)
  988. goto err_free_init_req;
  989. }
  990. mutex_lock(&fuse_mutex);
  991. err = -EINVAL;
  992. if (file->private_data)
  993. goto err_unlock;
  994. err = fuse_ctl_add_conn(fc);
  995. if (err)
  996. goto err_unlock;
  997. list_add_tail(&fc->entry, &fuse_conn_list);
  998. sb->s_root = root_dentry;
  999. file->private_data = fud;
  1000. mutex_unlock(&fuse_mutex);
  1001. /*
  1002. * atomic_dec_and_test() in fput() provides the necessary
  1003. * memory barrier for file->private_data to be visible on all
  1004. * CPUs after this
  1005. */
  1006. fput(file);
  1007. fuse_send_init(fc, init_req);
  1008. return 0;
  1009. err_unlock:
  1010. mutex_unlock(&fuse_mutex);
  1011. err_free_init_req:
  1012. fuse_request_free(init_req);
  1013. err_put_root:
  1014. dput(root_dentry);
  1015. err_dev_free:
  1016. fuse_dev_free(fud);
  1017. err_put_conn:
  1018. fuse_bdi_destroy(fc);
  1019. fuse_conn_put(fc);
  1020. sb->s_fs_info = NULL;
  1021. err_fput:
  1022. fput(file);
  1023. err:
  1024. return err;
  1025. }
  1026. static struct dentry *fuse_mount(struct file_system_type *fs_type,
  1027. int flags, const char *dev_name,
  1028. void *raw_data)
  1029. {
  1030. return mount_nodev(fs_type, flags, raw_data, fuse_fill_super);
  1031. }
  1032. static void fuse_sb_destroy(struct super_block *sb)
  1033. {
  1034. struct fuse_conn *fc = get_fuse_conn_super(sb);
  1035. if (fc) {
  1036. fuse_send_destroy(fc);
  1037. fuse_abort_conn(fc);
  1038. fuse_wait_aborted(fc);
  1039. down_write(&fc->killsb);
  1040. fc->sb = NULL;
  1041. up_write(&fc->killsb);
  1042. }
  1043. }
  1044. static void fuse_kill_sb_anon(struct super_block *sb)
  1045. {
  1046. fuse_sb_destroy(sb);
  1047. kill_anon_super(sb);
  1048. }
  1049. static struct file_system_type fuse_fs_type = {
  1050. .owner = THIS_MODULE,
  1051. .name = "fuse",
  1052. .fs_flags = FS_HAS_SUBTYPE,
  1053. .mount = fuse_mount,
  1054. .kill_sb = fuse_kill_sb_anon,
  1055. };
  1056. MODULE_ALIAS_FS("fuse");
  1057. #ifdef CONFIG_BLOCK
  1058. static struct dentry *fuse_mount_blk(struct file_system_type *fs_type,
  1059. int flags, const char *dev_name,
  1060. void *raw_data)
  1061. {
  1062. return mount_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super);
  1063. }
  1064. static void fuse_kill_sb_blk(struct super_block *sb)
  1065. {
  1066. fuse_sb_destroy(sb);
  1067. kill_block_super(sb);
  1068. }
  1069. static struct file_system_type fuseblk_fs_type = {
  1070. .owner = THIS_MODULE,
  1071. .name = "fuseblk",
  1072. .mount = fuse_mount_blk,
  1073. .kill_sb = fuse_kill_sb_blk,
  1074. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  1075. };
  1076. MODULE_ALIAS_FS("fuseblk");
  1077. static inline int register_fuseblk(void)
  1078. {
  1079. return register_filesystem(&fuseblk_fs_type);
  1080. }
  1081. static inline void unregister_fuseblk(void)
  1082. {
  1083. unregister_filesystem(&fuseblk_fs_type);
  1084. }
  1085. #else
  1086. static inline int register_fuseblk(void)
  1087. {
  1088. return 0;
  1089. }
  1090. static inline void unregister_fuseblk(void)
  1091. {
  1092. }
  1093. #endif
  1094. static void fuse_inode_init_once(void *foo)
  1095. {
  1096. struct inode *inode = foo;
  1097. inode_init_once(inode);
  1098. }
  1099. static int __init fuse_fs_init(void)
  1100. {
  1101. int err;
  1102. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  1103. sizeof(struct fuse_inode), 0,
  1104. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
  1105. fuse_inode_init_once);
  1106. err = -ENOMEM;
  1107. if (!fuse_inode_cachep)
  1108. goto out;
  1109. err = register_fuseblk();
  1110. if (err)
  1111. goto out2;
  1112. err = register_filesystem(&fuse_fs_type);
  1113. if (err)
  1114. goto out3;
  1115. return 0;
  1116. out3:
  1117. unregister_fuseblk();
  1118. out2:
  1119. kmem_cache_destroy(fuse_inode_cachep);
  1120. out:
  1121. return err;
  1122. }
  1123. static void fuse_fs_cleanup(void)
  1124. {
  1125. unregister_filesystem(&fuse_fs_type);
  1126. unregister_fuseblk();
  1127. /*
  1128. * Make sure all delayed rcu free inodes are flushed before we
  1129. * destroy cache.
  1130. */
  1131. rcu_barrier();
  1132. kmem_cache_destroy(fuse_inode_cachep);
  1133. }
  1134. static struct kobject *fuse_kobj;
  1135. static int fuse_sysfs_init(void)
  1136. {
  1137. int err;
  1138. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  1139. if (!fuse_kobj) {
  1140. err = -ENOMEM;
  1141. goto out_err;
  1142. }
  1143. err = sysfs_create_mount_point(fuse_kobj, "connections");
  1144. if (err)
  1145. goto out_fuse_unregister;
  1146. return 0;
  1147. out_fuse_unregister:
  1148. kobject_put(fuse_kobj);
  1149. out_err:
  1150. return err;
  1151. }
  1152. static void fuse_sysfs_cleanup(void)
  1153. {
  1154. sysfs_remove_mount_point(fuse_kobj, "connections");
  1155. kobject_put(fuse_kobj);
  1156. }
  1157. static int __init fuse_init(void)
  1158. {
  1159. int res;
  1160. printk(KERN_INFO "fuse init (API version %i.%i)\n",
  1161. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  1162. INIT_LIST_HEAD(&fuse_conn_list);
  1163. res = fuse_fs_init();
  1164. if (res)
  1165. goto err;
  1166. res = fuse_dev_init();
  1167. if (res)
  1168. goto err_fs_cleanup;
  1169. res = fuse_sysfs_init();
  1170. if (res)
  1171. goto err_dev_cleanup;
  1172. res = fuse_ctl_init();
  1173. if (res)
  1174. goto err_sysfs_cleanup;
  1175. sanitize_global_limit(&max_user_bgreq);
  1176. sanitize_global_limit(&max_user_congthresh);
  1177. return 0;
  1178. err_sysfs_cleanup:
  1179. fuse_sysfs_cleanup();
  1180. err_dev_cleanup:
  1181. fuse_dev_cleanup();
  1182. err_fs_cleanup:
  1183. fuse_fs_cleanup();
  1184. err:
  1185. return res;
  1186. }
  1187. static void __exit fuse_exit(void)
  1188. {
  1189. printk(KERN_DEBUG "fuse exit\n");
  1190. fuse_ctl_cleanup();
  1191. fuse_sysfs_cleanup();
  1192. fuse_fs_cleanup();
  1193. fuse_dev_cleanup();
  1194. }
  1195. module_init(fuse_init);
  1196. module_exit(fuse_exit);