inode.c 31 KB

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