inode.c 31 KB

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