inode.c 31 KB

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