inode.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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. sb->s_fs_info = NULL;
  1013. err_fput:
  1014. fput(file);
  1015. err:
  1016. return err;
  1017. }
  1018. static struct dentry *fuse_mount(struct file_system_type *fs_type,
  1019. int flags, const char *dev_name,
  1020. void *raw_data)
  1021. {
  1022. return mount_nodev(fs_type, flags, raw_data, fuse_fill_super);
  1023. }
  1024. static void fuse_kill_sb_anon(struct super_block *sb)
  1025. {
  1026. struct fuse_conn *fc = get_fuse_conn_super(sb);
  1027. if (fc) {
  1028. down_write(&fc->killsb);
  1029. fc->sb = NULL;
  1030. up_write(&fc->killsb);
  1031. }
  1032. kill_anon_super(sb);
  1033. }
  1034. static struct file_system_type fuse_fs_type = {
  1035. .owner = THIS_MODULE,
  1036. .name = "fuse",
  1037. .fs_flags = FS_HAS_SUBTYPE,
  1038. .mount = fuse_mount,
  1039. .kill_sb = fuse_kill_sb_anon,
  1040. };
  1041. MODULE_ALIAS_FS("fuse");
  1042. #ifdef CONFIG_BLOCK
  1043. static struct dentry *fuse_mount_blk(struct file_system_type *fs_type,
  1044. int flags, const char *dev_name,
  1045. void *raw_data)
  1046. {
  1047. return mount_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super);
  1048. }
  1049. static void fuse_kill_sb_blk(struct super_block *sb)
  1050. {
  1051. struct fuse_conn *fc = get_fuse_conn_super(sb);
  1052. if (fc) {
  1053. down_write(&fc->killsb);
  1054. fc->sb = NULL;
  1055. up_write(&fc->killsb);
  1056. }
  1057. kill_block_super(sb);
  1058. }
  1059. static struct file_system_type fuseblk_fs_type = {
  1060. .owner = THIS_MODULE,
  1061. .name = "fuseblk",
  1062. .mount = fuse_mount_blk,
  1063. .kill_sb = fuse_kill_sb_blk,
  1064. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  1065. };
  1066. MODULE_ALIAS_FS("fuseblk");
  1067. static inline int register_fuseblk(void)
  1068. {
  1069. return register_filesystem(&fuseblk_fs_type);
  1070. }
  1071. static inline void unregister_fuseblk(void)
  1072. {
  1073. unregister_filesystem(&fuseblk_fs_type);
  1074. }
  1075. #else
  1076. static inline int register_fuseblk(void)
  1077. {
  1078. return 0;
  1079. }
  1080. static inline void unregister_fuseblk(void)
  1081. {
  1082. }
  1083. #endif
  1084. static void fuse_inode_init_once(void *foo)
  1085. {
  1086. struct inode *inode = foo;
  1087. inode_init_once(inode);
  1088. }
  1089. static int __init fuse_fs_init(void)
  1090. {
  1091. int err;
  1092. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  1093. sizeof(struct fuse_inode), 0,
  1094. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
  1095. fuse_inode_init_once);
  1096. err = -ENOMEM;
  1097. if (!fuse_inode_cachep)
  1098. goto out;
  1099. err = register_fuseblk();
  1100. if (err)
  1101. goto out2;
  1102. err = register_filesystem(&fuse_fs_type);
  1103. if (err)
  1104. goto out3;
  1105. return 0;
  1106. out3:
  1107. unregister_fuseblk();
  1108. out2:
  1109. kmem_cache_destroy(fuse_inode_cachep);
  1110. out:
  1111. return err;
  1112. }
  1113. static void fuse_fs_cleanup(void)
  1114. {
  1115. unregister_filesystem(&fuse_fs_type);
  1116. unregister_fuseblk();
  1117. /*
  1118. * Make sure all delayed rcu free inodes are flushed before we
  1119. * destroy cache.
  1120. */
  1121. rcu_barrier();
  1122. kmem_cache_destroy(fuse_inode_cachep);
  1123. }
  1124. static struct kobject *fuse_kobj;
  1125. static int fuse_sysfs_init(void)
  1126. {
  1127. int err;
  1128. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  1129. if (!fuse_kobj) {
  1130. err = -ENOMEM;
  1131. goto out_err;
  1132. }
  1133. err = sysfs_create_mount_point(fuse_kobj, "connections");
  1134. if (err)
  1135. goto out_fuse_unregister;
  1136. return 0;
  1137. out_fuse_unregister:
  1138. kobject_put(fuse_kobj);
  1139. out_err:
  1140. return err;
  1141. }
  1142. static void fuse_sysfs_cleanup(void)
  1143. {
  1144. sysfs_remove_mount_point(fuse_kobj, "connections");
  1145. kobject_put(fuse_kobj);
  1146. }
  1147. static int __init fuse_init(void)
  1148. {
  1149. int res;
  1150. printk(KERN_INFO "fuse init (API version %i.%i)\n",
  1151. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  1152. INIT_LIST_HEAD(&fuse_conn_list);
  1153. res = fuse_fs_init();
  1154. if (res)
  1155. goto err;
  1156. res = fuse_dev_init();
  1157. if (res)
  1158. goto err_fs_cleanup;
  1159. res = fuse_sysfs_init();
  1160. if (res)
  1161. goto err_dev_cleanup;
  1162. res = fuse_ctl_init();
  1163. if (res)
  1164. goto err_sysfs_cleanup;
  1165. sanitize_global_limit(&max_user_bgreq);
  1166. sanitize_global_limit(&max_user_congthresh);
  1167. return 0;
  1168. err_sysfs_cleanup:
  1169. fuse_sysfs_cleanup();
  1170. err_dev_cleanup:
  1171. fuse_dev_cleanup();
  1172. err_fs_cleanup:
  1173. fuse_fs_cleanup();
  1174. err:
  1175. return res;
  1176. }
  1177. static void __exit fuse_exit(void)
  1178. {
  1179. printk(KERN_DEBUG "fuse exit\n");
  1180. fuse_ctl_cleanup();
  1181. fuse_sysfs_cleanup();
  1182. fuse_fs_cleanup();
  1183. fuse_dev_cleanup();
  1184. }
  1185. module_init(fuse_init);
  1186. module_exit(fuse_exit);