inode.c 33 KB

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