inode.c 32 KB

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