inode.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  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 "fuse_dev_i.h"
  9. #include "dev_uring_i.h"
  10. #include <linux/dax.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/slab.h>
  13. #include <linux/file.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/fs_context.h>
  19. #include <linux/fs_parser.h>
  20. #include <linux/statfs.h>
  21. #include <linux/random.h>
  22. #include <linux/sched.h>
  23. #include <linux/exportfs.h>
  24. #include <linux/posix_acl.h>
  25. #include <linux/pid_namespace.h>
  26. #include <uapi/linux/magic.h>
  27. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  28. MODULE_DESCRIPTION("Filesystem in Userspace");
  29. MODULE_LICENSE("GPL");
  30. static struct kmem_cache *fuse_inode_cachep;
  31. struct list_head fuse_conn_list;
  32. DEFINE_MUTEX(fuse_mutex);
  33. DECLARE_WAIT_QUEUE_HEAD(fuse_dev_waitq);
  34. static int set_global_limit(const char *val, const struct kernel_param *kp);
  35. unsigned int fuse_max_pages_limit = 256;
  36. /* default is no timeout */
  37. unsigned int fuse_default_req_timeout;
  38. unsigned int fuse_max_req_timeout;
  39. unsigned int max_user_bgreq;
  40. module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
  41. &max_user_bgreq, 0644);
  42. __MODULE_PARM_TYPE(max_user_bgreq, "uint");
  43. MODULE_PARM_DESC(max_user_bgreq,
  44. "Global limit for the maximum number of backgrounded requests an "
  45. "unprivileged user can set");
  46. unsigned int max_user_congthresh;
  47. module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
  48. &max_user_congthresh, 0644);
  49. __MODULE_PARM_TYPE(max_user_congthresh, "uint");
  50. MODULE_PARM_DESC(max_user_congthresh,
  51. "Global limit for the maximum congestion threshold an "
  52. "unprivileged user can set");
  53. #define FUSE_DEFAULT_BLKSIZE 512
  54. /** Maximum number of outstanding background requests */
  55. #define FUSE_DEFAULT_MAX_BACKGROUND 12
  56. /** Congestion starts at 75% of maximum */
  57. #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
  58. #ifdef CONFIG_BLOCK
  59. static struct file_system_type fuseblk_fs_type;
  60. #endif
  61. struct fuse_forget_link *fuse_alloc_forget(void)
  62. {
  63. return kzalloc_obj(struct fuse_forget_link, GFP_KERNEL_ACCOUNT);
  64. }
  65. static struct fuse_submount_lookup *fuse_alloc_submount_lookup(void)
  66. {
  67. struct fuse_submount_lookup *sl;
  68. sl = kzalloc_obj(struct fuse_submount_lookup, GFP_KERNEL_ACCOUNT);
  69. if (!sl)
  70. return NULL;
  71. sl->forget = fuse_alloc_forget();
  72. if (!sl->forget)
  73. goto out_free;
  74. return sl;
  75. out_free:
  76. kfree(sl);
  77. return NULL;
  78. }
  79. static struct inode *fuse_alloc_inode(struct super_block *sb)
  80. {
  81. struct fuse_inode *fi;
  82. fi = alloc_inode_sb(sb, fuse_inode_cachep, GFP_KERNEL);
  83. if (!fi)
  84. return NULL;
  85. /* Initialize private data (i.e. everything except fi->inode) */
  86. BUILD_BUG_ON(offsetof(struct fuse_inode, inode) != 0);
  87. memset((void *) fi + sizeof(fi->inode), 0, sizeof(*fi) - sizeof(fi->inode));
  88. fi->inval_mask = ~0;
  89. mutex_init(&fi->mutex);
  90. spin_lock_init(&fi->lock);
  91. fi->forget = fuse_alloc_forget();
  92. if (!fi->forget)
  93. goto out_free;
  94. if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi))
  95. goto out_free_forget;
  96. if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  97. fuse_inode_backing_set(fi, NULL);
  98. return &fi->inode;
  99. out_free_forget:
  100. kfree(fi->forget);
  101. out_free:
  102. kmem_cache_free(fuse_inode_cachep, fi);
  103. return NULL;
  104. }
  105. static void fuse_free_inode(struct inode *inode)
  106. {
  107. struct fuse_inode *fi = get_fuse_inode(inode);
  108. mutex_destroy(&fi->mutex);
  109. kfree(fi->forget);
  110. #ifdef CONFIG_FUSE_DAX
  111. kfree(fi->dax);
  112. #endif
  113. if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  114. fuse_backing_put(fuse_inode_backing(fi));
  115. kmem_cache_free(fuse_inode_cachep, fi);
  116. }
  117. static void fuse_cleanup_submount_lookup(struct fuse_conn *fc,
  118. struct fuse_submount_lookup *sl)
  119. {
  120. if (!refcount_dec_and_test(&sl->count))
  121. return;
  122. fuse_queue_forget(fc, sl->forget, sl->nodeid, 1);
  123. sl->forget = NULL;
  124. kfree(sl);
  125. }
  126. static void fuse_evict_inode(struct inode *inode)
  127. {
  128. struct fuse_inode *fi = get_fuse_inode(inode);
  129. /* Will write inode on close/munmap and in all other dirtiers */
  130. WARN_ON(inode_state_read_once(inode) & I_DIRTY_INODE);
  131. if (FUSE_IS_DAX(inode))
  132. dax_break_layout_final(inode);
  133. truncate_inode_pages_final(&inode->i_data);
  134. clear_inode(inode);
  135. if (inode->i_sb->s_flags & SB_ACTIVE) {
  136. struct fuse_conn *fc = get_fuse_conn(inode);
  137. if (FUSE_IS_DAX(inode))
  138. fuse_dax_inode_cleanup(inode);
  139. if (fi->nlookup) {
  140. fuse_queue_forget(fc, fi->forget, fi->nodeid,
  141. fi->nlookup);
  142. fi->forget = NULL;
  143. }
  144. if (fi->submount_lookup) {
  145. fuse_cleanup_submount_lookup(fc, fi->submount_lookup);
  146. fi->submount_lookup = NULL;
  147. }
  148. /*
  149. * Evict of non-deleted inode may race with outstanding
  150. * LOOKUP/READDIRPLUS requests and result in inconsistency when
  151. * the request finishes. Deal with that here by bumping a
  152. * counter that can be compared to the starting value.
  153. */
  154. if (inode->i_nlink > 0)
  155. atomic64_inc(&fc->evict_ctr);
  156. }
  157. if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
  158. WARN_ON(fi->iocachectr != 0);
  159. WARN_ON(!list_empty(&fi->write_files));
  160. WARN_ON(!list_empty(&fi->queued_writes));
  161. }
  162. }
  163. static int fuse_reconfigure(struct fs_context *fsc)
  164. {
  165. struct super_block *sb = fsc->root->d_sb;
  166. sync_filesystem(sb);
  167. if (fsc->sb_flags & SB_MANDLOCK)
  168. return -EINVAL;
  169. return 0;
  170. }
  171. /*
  172. * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
  173. * so that it will fit.
  174. */
  175. static ino_t fuse_squash_ino(u64 ino64)
  176. {
  177. ino_t ino = (ino_t) ino64;
  178. if (sizeof(ino_t) < sizeof(u64))
  179. ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
  180. return ino;
  181. }
  182. void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
  183. struct fuse_statx *sx,
  184. u64 attr_valid, u32 cache_mask,
  185. u64 evict_ctr)
  186. {
  187. struct fuse_conn *fc = get_fuse_conn(inode);
  188. struct fuse_inode *fi = get_fuse_inode(inode);
  189. lockdep_assert_held(&fi->lock);
  190. /*
  191. * Clear basic stats from invalid mask.
  192. *
  193. * Don't do this if this is coming from a fuse_iget() call and there
  194. * might have been a racing evict which would've invalidated the result
  195. * if the attr_version would've been preserved.
  196. *
  197. * !evict_ctr -> this is create
  198. * fi->attr_version != 0 -> this is not a new inode
  199. * evict_ctr == fuse_get_evict_ctr() -> no evicts while during request
  200. */
  201. if (!evict_ctr || fi->attr_version || evict_ctr == fuse_get_evict_ctr(fc))
  202. set_mask_bits(&fi->inval_mask, STATX_BASIC_STATS, 0);
  203. fi->attr_version = atomic64_inc_return(&fc->attr_version);
  204. fi->i_time = attr_valid;
  205. inode->i_ino = fuse_squash_ino(attr->ino);
  206. inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
  207. set_nlink(inode, attr->nlink);
  208. inode->i_uid = make_kuid(fc->user_ns, attr->uid);
  209. inode->i_gid = make_kgid(fc->user_ns, attr->gid);
  210. inode->i_blocks = attr->blocks;
  211. /* Sanitize nsecs */
  212. attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
  213. attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
  214. attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
  215. inode_set_atime(inode, attr->atime, attr->atimensec);
  216. /* mtime from server may be stale due to local buffered write */
  217. if (!(cache_mask & STATX_MTIME)) {
  218. inode_set_mtime(inode, attr->mtime, attr->mtimensec);
  219. }
  220. if (!(cache_mask & STATX_CTIME)) {
  221. inode_set_ctime(inode, attr->ctime, attr->ctimensec);
  222. }
  223. if (sx) {
  224. /* Sanitize nsecs */
  225. sx->btime.tv_nsec =
  226. min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1);
  227. /*
  228. * Btime has been queried, cache is valid (whether or not btime
  229. * is available or not) so clear STATX_BTIME from inval_mask.
  230. *
  231. * Availability of the btime attribute is indicated in
  232. * FUSE_I_BTIME
  233. */
  234. set_mask_bits(&fi->inval_mask, STATX_BTIME, 0);
  235. if (sx->mask & STATX_BTIME) {
  236. set_bit(FUSE_I_BTIME, &fi->state);
  237. fi->i_btime.tv_sec = sx->btime.tv_sec;
  238. fi->i_btime.tv_nsec = sx->btime.tv_nsec;
  239. }
  240. }
  241. if (attr->blksize)
  242. fi->cached_i_blkbits = ilog2(attr->blksize);
  243. else
  244. fi->cached_i_blkbits = inode->i_sb->s_blocksize_bits;
  245. /*
  246. * Don't set the sticky bit in i_mode, unless we want the VFS
  247. * to check permissions. This prevents failures due to the
  248. * check in may_delete().
  249. */
  250. fi->orig_i_mode = inode->i_mode;
  251. if (!fc->default_permissions)
  252. inode->i_mode &= ~S_ISVTX;
  253. fi->orig_ino = attr->ino;
  254. /*
  255. * We are refreshing inode data and it is possible that another
  256. * client set suid/sgid or security.capability xattr. So clear
  257. * S_NOSEC. Ideally, we could have cleared it only if suid/sgid
  258. * was set or if security.capability xattr was set. But we don't
  259. * know if security.capability has been set or not. So clear it
  260. * anyway. Its less efficient but should be safe.
  261. */
  262. inode->i_flags &= ~S_NOSEC;
  263. }
  264. u32 fuse_get_cache_mask(struct inode *inode)
  265. {
  266. struct fuse_conn *fc = get_fuse_conn(inode);
  267. if (!fc->writeback_cache || !S_ISREG(inode->i_mode))
  268. return 0;
  269. return STATX_MTIME | STATX_CTIME | STATX_SIZE;
  270. }
  271. static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr,
  272. struct fuse_statx *sx, u64 attr_valid,
  273. u64 attr_version, u64 evict_ctr)
  274. {
  275. struct fuse_conn *fc = get_fuse_conn(inode);
  276. struct fuse_inode *fi = get_fuse_inode(inode);
  277. u32 cache_mask;
  278. loff_t oldsize;
  279. struct timespec64 old_mtime;
  280. spin_lock(&fi->lock);
  281. /*
  282. * In case of writeback_cache enabled, writes update mtime, ctime and
  283. * may update i_size. In these cases trust the cached value in the
  284. * inode.
  285. */
  286. cache_mask = fuse_get_cache_mask(inode);
  287. if (cache_mask & STATX_SIZE)
  288. attr->size = i_size_read(inode);
  289. if (cache_mask & STATX_MTIME) {
  290. attr->mtime = inode_get_mtime_sec(inode);
  291. attr->mtimensec = inode_get_mtime_nsec(inode);
  292. }
  293. if (cache_mask & STATX_CTIME) {
  294. attr->ctime = inode_get_ctime_sec(inode);
  295. attr->ctimensec = inode_get_ctime_nsec(inode);
  296. }
  297. if ((attr_version != 0 && fi->attr_version > attr_version) ||
  298. test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
  299. spin_unlock(&fi->lock);
  300. return;
  301. }
  302. old_mtime = inode_get_mtime(inode);
  303. fuse_change_attributes_common(inode, attr, sx, attr_valid, cache_mask,
  304. evict_ctr);
  305. oldsize = inode->i_size;
  306. /*
  307. * In case of writeback_cache enabled, the cached writes beyond EOF
  308. * extend local i_size without keeping userspace server in sync. So,
  309. * attr->size coming from server can be stale. We cannot trust it.
  310. */
  311. if (!(cache_mask & STATX_SIZE))
  312. i_size_write(inode, attr->size);
  313. spin_unlock(&fi->lock);
  314. if (!cache_mask && S_ISREG(inode->i_mode)) {
  315. bool inval = false;
  316. if (oldsize != attr->size) {
  317. truncate_pagecache(inode, attr->size);
  318. if (!fc->explicit_inval_data)
  319. inval = true;
  320. } else if (fc->auto_inval_data) {
  321. struct timespec64 new_mtime = {
  322. .tv_sec = attr->mtime,
  323. .tv_nsec = attr->mtimensec,
  324. };
  325. /*
  326. * Auto inval mode also checks and invalidates if mtime
  327. * has changed.
  328. */
  329. if (!timespec64_equal(&old_mtime, &new_mtime))
  330. inval = true;
  331. }
  332. if (inval)
  333. invalidate_inode_pages2(inode->i_mapping);
  334. }
  335. if (IS_ENABLED(CONFIG_FUSE_DAX))
  336. fuse_dax_dontcache(inode, attr->flags);
  337. }
  338. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  339. struct fuse_statx *sx, u64 attr_valid,
  340. u64 attr_version)
  341. {
  342. fuse_change_attributes_i(inode, attr, sx, attr_valid, attr_version, 0);
  343. }
  344. static void fuse_init_submount_lookup(struct fuse_submount_lookup *sl,
  345. u64 nodeid)
  346. {
  347. sl->nodeid = nodeid;
  348. refcount_set(&sl->count, 1);
  349. }
  350. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
  351. struct fuse_conn *fc)
  352. {
  353. inode->i_mode = attr->mode & S_IFMT;
  354. inode->i_size = attr->size;
  355. inode_set_mtime(inode, attr->mtime, attr->mtimensec);
  356. inode_set_ctime(inode, attr->ctime, attr->ctimensec);
  357. if (S_ISREG(inode->i_mode)) {
  358. fuse_init_common(inode);
  359. fuse_init_file_inode(inode, attr->flags);
  360. } else if (S_ISDIR(inode->i_mode))
  361. fuse_init_dir(inode);
  362. else if (S_ISLNK(inode->i_mode))
  363. fuse_init_symlink(inode);
  364. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  365. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  366. fuse_init_common(inode);
  367. init_special_inode(inode, inode->i_mode,
  368. new_decode_dev(attr->rdev));
  369. } else
  370. BUG();
  371. /*
  372. * Ensure that we don't cache acls for daemons without FUSE_POSIX_ACL
  373. * so they see the exact same behavior as before.
  374. */
  375. if (!fc->posix_acl)
  376. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  377. }
  378. static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  379. {
  380. u64 nodeid = *(u64 *) _nodeidp;
  381. if (get_node_id(inode) == nodeid)
  382. return 1;
  383. else
  384. return 0;
  385. }
  386. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  387. {
  388. u64 nodeid = *(u64 *) _nodeidp;
  389. get_fuse_inode(inode)->nodeid = nodeid;
  390. return 0;
  391. }
  392. struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
  393. int generation, struct fuse_attr *attr,
  394. u64 attr_valid, u64 attr_version,
  395. u64 evict_ctr)
  396. {
  397. struct inode *inode;
  398. struct fuse_inode *fi;
  399. struct fuse_conn *fc = get_fuse_conn_super(sb);
  400. /*
  401. * Auto mount points get their node id from the submount root, which is
  402. * not a unique identifier within this filesystem.
  403. *
  404. * To avoid conflicts, do not place submount points into the inode hash
  405. * table.
  406. */
  407. if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) &&
  408. S_ISDIR(attr->mode)) {
  409. struct fuse_inode *fi;
  410. inode = new_inode(sb);
  411. if (!inode)
  412. return NULL;
  413. fuse_init_inode(inode, attr, fc);
  414. fi = get_fuse_inode(inode);
  415. fi->nodeid = nodeid;
  416. fi->submount_lookup = fuse_alloc_submount_lookup();
  417. if (!fi->submount_lookup) {
  418. iput(inode);
  419. return NULL;
  420. }
  421. /* Sets nlookup = 1 on fi->submount_lookup->nlookup */
  422. fuse_init_submount_lookup(fi->submount_lookup, nodeid);
  423. inode->i_flags |= S_AUTOMOUNT;
  424. goto done;
  425. }
  426. retry:
  427. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  428. if (!inode)
  429. return NULL;
  430. if ((inode_state_read_once(inode) & I_NEW)) {
  431. inode->i_flags |= S_NOATIME;
  432. if (!fc->writeback_cache || !S_ISREG(attr->mode))
  433. inode->i_flags |= S_NOCMTIME;
  434. inode->i_generation = generation;
  435. fuse_init_inode(inode, attr, fc);
  436. unlock_new_inode(inode);
  437. } else if (fuse_stale_inode(inode, generation, attr)) {
  438. /* nodeid was reused, any I/O on the old inode should fail */
  439. fuse_make_bad(inode);
  440. if (inode != d_inode(sb->s_root)) {
  441. remove_inode_hash(inode);
  442. iput(inode);
  443. goto retry;
  444. }
  445. }
  446. fi = get_fuse_inode(inode);
  447. spin_lock(&fi->lock);
  448. fi->nlookup++;
  449. spin_unlock(&fi->lock);
  450. done:
  451. fuse_change_attributes_i(inode, attr, NULL, attr_valid, attr_version,
  452. evict_ctr);
  453. return inode;
  454. }
  455. struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
  456. struct fuse_mount **fm)
  457. {
  458. struct fuse_mount *fm_iter;
  459. struct inode *inode;
  460. WARN_ON(!rwsem_is_locked(&fc->killsb));
  461. list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
  462. if (!fm_iter->sb)
  463. continue;
  464. inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid);
  465. if (inode) {
  466. if (fm)
  467. *fm = fm_iter;
  468. return inode;
  469. }
  470. }
  471. return NULL;
  472. }
  473. int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
  474. loff_t offset, loff_t len)
  475. {
  476. struct fuse_inode *fi;
  477. struct inode *inode;
  478. pgoff_t pg_start;
  479. pgoff_t pg_end;
  480. inode = fuse_ilookup(fc, nodeid, NULL);
  481. if (!inode)
  482. return -ENOENT;
  483. fi = get_fuse_inode(inode);
  484. spin_lock(&fi->lock);
  485. fi->attr_version = atomic64_inc_return(&fc->attr_version);
  486. spin_unlock(&fi->lock);
  487. fuse_invalidate_attr(inode);
  488. forget_all_cached_acls(inode);
  489. if (offset >= 0) {
  490. pg_start = offset >> PAGE_SHIFT;
  491. if (len <= 0)
  492. pg_end = -1;
  493. else
  494. pg_end = (offset + len - 1) >> PAGE_SHIFT;
  495. invalidate_inode_pages2_range(inode->i_mapping,
  496. pg_start, pg_end);
  497. }
  498. iput(inode);
  499. return 0;
  500. }
  501. void fuse_try_prune_one_inode(struct fuse_conn *fc, u64 nodeid)
  502. {
  503. struct inode *inode;
  504. inode = fuse_ilookup(fc, nodeid, NULL);
  505. if (!inode)
  506. return;
  507. d_prune_aliases(inode);
  508. iput(inode);
  509. }
  510. bool fuse_lock_inode(struct inode *inode)
  511. {
  512. bool locked = false;
  513. if (!get_fuse_conn(inode)->parallel_dirops) {
  514. mutex_lock(&get_fuse_inode(inode)->mutex);
  515. locked = true;
  516. }
  517. return locked;
  518. }
  519. void fuse_unlock_inode(struct inode *inode, bool locked)
  520. {
  521. if (locked)
  522. mutex_unlock(&get_fuse_inode(inode)->mutex);
  523. }
  524. static void fuse_umount_begin(struct super_block *sb)
  525. {
  526. struct fuse_conn *fc = get_fuse_conn_super(sb);
  527. if (fc->no_force_umount)
  528. return;
  529. fuse_abort_conn(fc);
  530. // Only retire block-device-based superblocks.
  531. if (sb->s_bdev != NULL)
  532. retire_super(sb);
  533. }
  534. static void fuse_send_destroy(struct fuse_mount *fm)
  535. {
  536. if (fm->fc->conn_init) {
  537. FUSE_ARGS(args);
  538. args.opcode = FUSE_DESTROY;
  539. args.force = true;
  540. args.nocreds = true;
  541. fuse_simple_request(fm, &args);
  542. }
  543. }
  544. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  545. {
  546. stbuf->f_type = FUSE_SUPER_MAGIC;
  547. stbuf->f_bsize = attr->bsize;
  548. stbuf->f_frsize = attr->frsize;
  549. stbuf->f_blocks = attr->blocks;
  550. stbuf->f_bfree = attr->bfree;
  551. stbuf->f_bavail = attr->bavail;
  552. stbuf->f_files = attr->files;
  553. stbuf->f_ffree = attr->ffree;
  554. stbuf->f_namelen = attr->namelen;
  555. /* fsid is left zero */
  556. }
  557. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  558. {
  559. struct super_block *sb = dentry->d_sb;
  560. struct fuse_mount *fm = get_fuse_mount_super(sb);
  561. FUSE_ARGS(args);
  562. struct fuse_statfs_out outarg;
  563. int err;
  564. if (!fuse_allow_current_process(fm->fc)) {
  565. buf->f_type = FUSE_SUPER_MAGIC;
  566. return 0;
  567. }
  568. memset(&outarg, 0, sizeof(outarg));
  569. args.in_numargs = 0;
  570. args.opcode = FUSE_STATFS;
  571. args.nodeid = get_node_id(d_inode(dentry));
  572. args.out_numargs = 1;
  573. args.out_args[0].size = sizeof(outarg);
  574. args.out_args[0].value = &outarg;
  575. err = fuse_simple_request(fm, &args);
  576. if (!err)
  577. convert_fuse_statfs(buf, &outarg.st);
  578. return err;
  579. }
  580. static struct fuse_sync_bucket *fuse_sync_bucket_alloc(void)
  581. {
  582. struct fuse_sync_bucket *bucket;
  583. bucket = kzalloc_obj(*bucket, GFP_KERNEL | __GFP_NOFAIL);
  584. if (bucket) {
  585. init_waitqueue_head(&bucket->waitq);
  586. /* Initial active count */
  587. atomic_set(&bucket->count, 1);
  588. }
  589. return bucket;
  590. }
  591. static void fuse_sync_fs_writes(struct fuse_conn *fc)
  592. {
  593. struct fuse_sync_bucket *bucket, *new_bucket;
  594. int count;
  595. new_bucket = fuse_sync_bucket_alloc();
  596. spin_lock(&fc->lock);
  597. bucket = rcu_dereference_protected(fc->curr_bucket, 1);
  598. count = atomic_read(&bucket->count);
  599. WARN_ON(count < 1);
  600. /* No outstanding writes? */
  601. if (count == 1) {
  602. spin_unlock(&fc->lock);
  603. kfree(new_bucket);
  604. return;
  605. }
  606. /*
  607. * Completion of new bucket depends on completion of this bucket, so add
  608. * one more count.
  609. */
  610. atomic_inc(&new_bucket->count);
  611. rcu_assign_pointer(fc->curr_bucket, new_bucket);
  612. spin_unlock(&fc->lock);
  613. /*
  614. * Drop initial active count. At this point if all writes in this and
  615. * ancestor buckets complete, the count will go to zero and this task
  616. * will be woken up.
  617. */
  618. atomic_dec(&bucket->count);
  619. wait_event(bucket->waitq, atomic_read(&bucket->count) == 0);
  620. /* Drop temp count on descendant bucket */
  621. fuse_sync_bucket_dec(new_bucket);
  622. kfree_rcu(bucket, rcu);
  623. }
  624. static int fuse_sync_fs(struct super_block *sb, int wait)
  625. {
  626. struct fuse_mount *fm = get_fuse_mount_super(sb);
  627. struct fuse_conn *fc = fm->fc;
  628. struct fuse_syncfs_in inarg;
  629. FUSE_ARGS(args);
  630. int err;
  631. /*
  632. * Userspace cannot handle the wait == 0 case. Avoid a
  633. * gratuitous roundtrip.
  634. */
  635. if (!wait)
  636. return 0;
  637. /* The filesystem is being unmounted. Nothing to do. */
  638. if (!sb->s_root)
  639. return 0;
  640. if (!fc->sync_fs)
  641. return 0;
  642. fuse_sync_fs_writes(fc);
  643. memset(&inarg, 0, sizeof(inarg));
  644. args.in_numargs = 1;
  645. args.in_args[0].size = sizeof(inarg);
  646. args.in_args[0].value = &inarg;
  647. args.opcode = FUSE_SYNCFS;
  648. args.nodeid = get_node_id(sb->s_root->d_inode);
  649. args.out_numargs = 0;
  650. err = fuse_simple_request(fm, &args);
  651. if (err == -ENOSYS) {
  652. fc->sync_fs = 0;
  653. err = 0;
  654. }
  655. return err;
  656. }
  657. enum {
  658. OPT_SOURCE,
  659. OPT_SUBTYPE,
  660. OPT_FD,
  661. OPT_ROOTMODE,
  662. OPT_USER_ID,
  663. OPT_GROUP_ID,
  664. OPT_DEFAULT_PERMISSIONS,
  665. OPT_ALLOW_OTHER,
  666. OPT_MAX_READ,
  667. OPT_BLKSIZE,
  668. OPT_ERR
  669. };
  670. static const struct fs_parameter_spec fuse_fs_parameters[] = {
  671. fsparam_string ("source", OPT_SOURCE),
  672. fsparam_u32 ("fd", OPT_FD),
  673. fsparam_u32oct ("rootmode", OPT_ROOTMODE),
  674. fsparam_uid ("user_id", OPT_USER_ID),
  675. fsparam_gid ("group_id", OPT_GROUP_ID),
  676. fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
  677. fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
  678. fsparam_u32 ("max_read", OPT_MAX_READ),
  679. fsparam_u32 ("blksize", OPT_BLKSIZE),
  680. fsparam_string ("subtype", OPT_SUBTYPE),
  681. {}
  682. };
  683. static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
  684. {
  685. struct fs_parse_result result;
  686. struct fuse_fs_context *ctx = fsc->fs_private;
  687. int opt;
  688. kuid_t kuid;
  689. kgid_t kgid;
  690. if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  691. /*
  692. * Ignore options coming from mount(MS_REMOUNT) for backward
  693. * compatibility.
  694. */
  695. if (fsc->oldapi)
  696. return 0;
  697. return invalfc(fsc, "No changes allowed in reconfigure");
  698. }
  699. opt = fs_parse(fsc, fuse_fs_parameters, param, &result);
  700. if (opt < 0)
  701. return opt;
  702. switch (opt) {
  703. case OPT_SOURCE:
  704. if (fsc->source)
  705. return invalfc(fsc, "Multiple sources specified");
  706. fsc->source = param->string;
  707. param->string = NULL;
  708. break;
  709. case OPT_SUBTYPE:
  710. if (ctx->subtype)
  711. return invalfc(fsc, "Multiple subtypes specified");
  712. ctx->subtype = param->string;
  713. param->string = NULL;
  714. return 0;
  715. case OPT_FD:
  716. ctx->fd = result.uint_32;
  717. ctx->fd_present = true;
  718. break;
  719. case OPT_ROOTMODE:
  720. if (!fuse_valid_type(result.uint_32))
  721. return invalfc(fsc, "Invalid rootmode");
  722. ctx->rootmode = result.uint_32;
  723. ctx->rootmode_present = true;
  724. break;
  725. case OPT_USER_ID:
  726. kuid = result.uid;
  727. /*
  728. * The requested uid must be representable in the
  729. * filesystem's idmapping.
  730. */
  731. if (!kuid_has_mapping(fsc->user_ns, kuid))
  732. return invalfc(fsc, "Invalid user_id");
  733. ctx->user_id = kuid;
  734. ctx->user_id_present = true;
  735. break;
  736. case OPT_GROUP_ID:
  737. kgid = result.gid;
  738. /*
  739. * The requested gid must be representable in the
  740. * filesystem's idmapping.
  741. */
  742. if (!kgid_has_mapping(fsc->user_ns, kgid))
  743. return invalfc(fsc, "Invalid group_id");
  744. ctx->group_id = kgid;
  745. ctx->group_id_present = true;
  746. break;
  747. case OPT_DEFAULT_PERMISSIONS:
  748. ctx->default_permissions = true;
  749. break;
  750. case OPT_ALLOW_OTHER:
  751. ctx->allow_other = true;
  752. break;
  753. case OPT_MAX_READ:
  754. ctx->max_read = result.uint_32;
  755. break;
  756. case OPT_BLKSIZE:
  757. if (!ctx->is_bdev)
  758. return invalfc(fsc, "blksize only supported for fuseblk");
  759. ctx->blksize = result.uint_32;
  760. break;
  761. default:
  762. return -EINVAL;
  763. }
  764. return 0;
  765. }
  766. static void fuse_free_fsc(struct fs_context *fsc)
  767. {
  768. struct fuse_fs_context *ctx = fsc->fs_private;
  769. if (ctx) {
  770. kfree(ctx->subtype);
  771. kfree(ctx);
  772. }
  773. }
  774. static int fuse_show_options(struct seq_file *m, struct dentry *root)
  775. {
  776. struct super_block *sb = root->d_sb;
  777. struct fuse_conn *fc = get_fuse_conn_super(sb);
  778. if (fc->legacy_opts_show) {
  779. seq_printf(m, ",user_id=%u",
  780. from_kuid_munged(fc->user_ns, fc->user_id));
  781. seq_printf(m, ",group_id=%u",
  782. from_kgid_munged(fc->user_ns, fc->group_id));
  783. if (fc->default_permissions)
  784. seq_puts(m, ",default_permissions");
  785. if (fc->allow_other)
  786. seq_puts(m, ",allow_other");
  787. if (fc->max_read != ~0)
  788. seq_printf(m, ",max_read=%u", fc->max_read);
  789. if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
  790. seq_printf(m, ",blksize=%lu", sb->s_blocksize);
  791. }
  792. #ifdef CONFIG_FUSE_DAX
  793. if (fc->dax_mode == FUSE_DAX_ALWAYS)
  794. seq_puts(m, ",dax=always");
  795. else if (fc->dax_mode == FUSE_DAX_NEVER)
  796. seq_puts(m, ",dax=never");
  797. else if (fc->dax_mode == FUSE_DAX_INODE_USER)
  798. seq_puts(m, ",dax=inode");
  799. #endif
  800. return 0;
  801. }
  802. static void fuse_iqueue_init(struct fuse_iqueue *fiq,
  803. const struct fuse_iqueue_ops *ops,
  804. void *priv)
  805. {
  806. memset(fiq, 0, sizeof(struct fuse_iqueue));
  807. spin_lock_init(&fiq->lock);
  808. init_waitqueue_head(&fiq->waitq);
  809. INIT_LIST_HEAD(&fiq->pending);
  810. INIT_LIST_HEAD(&fiq->interrupts);
  811. fiq->forget_list_tail = &fiq->forget_list_head;
  812. fiq->connected = 1;
  813. fiq->ops = ops;
  814. fiq->priv = priv;
  815. }
  816. void fuse_pqueue_init(struct fuse_pqueue *fpq)
  817. {
  818. unsigned int i;
  819. spin_lock_init(&fpq->lock);
  820. for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
  821. INIT_LIST_HEAD(&fpq->processing[i]);
  822. INIT_LIST_HEAD(&fpq->io);
  823. fpq->connected = 1;
  824. }
  825. void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
  826. struct user_namespace *user_ns,
  827. const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
  828. {
  829. memset(fc, 0, sizeof(*fc));
  830. spin_lock_init(&fc->lock);
  831. spin_lock_init(&fc->bg_lock);
  832. init_rwsem(&fc->killsb);
  833. refcount_set(&fc->count, 1);
  834. atomic_set(&fc->dev_count, 1);
  835. atomic_set(&fc->epoch, 1);
  836. INIT_WORK(&fc->epoch_work, fuse_epoch_work);
  837. init_waitqueue_head(&fc->blocked_waitq);
  838. fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
  839. INIT_LIST_HEAD(&fc->bg_queue);
  840. INIT_LIST_HEAD(&fc->entry);
  841. INIT_LIST_HEAD(&fc->devices);
  842. atomic_set(&fc->num_waiting, 0);
  843. fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
  844. fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
  845. atomic64_set(&fc->khctr, 0);
  846. fc->polled_files = RB_ROOT;
  847. fc->blocked = 0;
  848. fc->initialized = 0;
  849. fc->connected = 1;
  850. atomic64_set(&fc->attr_version, 1);
  851. atomic64_set(&fc->evict_ctr, 1);
  852. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  853. fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
  854. fc->user_ns = get_user_ns(user_ns);
  855. fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
  856. fc->max_pages_limit = fuse_max_pages_limit;
  857. fc->name_max = FUSE_NAME_LOW_MAX;
  858. fc->timeout.req_timeout = 0;
  859. if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  860. fuse_backing_files_init(fc);
  861. INIT_LIST_HEAD(&fc->mounts);
  862. list_add(&fm->fc_entry, &fc->mounts);
  863. fm->fc = fc;
  864. }
  865. EXPORT_SYMBOL_GPL(fuse_conn_init);
  866. static void delayed_release(struct rcu_head *p)
  867. {
  868. struct fuse_conn *fc = container_of(p, struct fuse_conn, rcu);
  869. fuse_uring_destruct(fc);
  870. put_user_ns(fc->user_ns);
  871. fc->release(fc);
  872. }
  873. void fuse_conn_put(struct fuse_conn *fc)
  874. {
  875. struct fuse_iqueue *fiq = &fc->iq;
  876. struct fuse_sync_bucket *bucket;
  877. if (!refcount_dec_and_test(&fc->count))
  878. return;
  879. if (IS_ENABLED(CONFIG_FUSE_DAX))
  880. fuse_dax_conn_free(fc);
  881. if (fc->timeout.req_timeout)
  882. cancel_delayed_work_sync(&fc->timeout.work);
  883. cancel_work_sync(&fc->epoch_work);
  884. if (fiq->ops->release)
  885. fiq->ops->release(fiq);
  886. put_pid_ns(fc->pid_ns);
  887. bucket = rcu_dereference_protected(fc->curr_bucket, 1);
  888. if (bucket) {
  889. WARN_ON(atomic_read(&bucket->count) != 1);
  890. kfree(bucket);
  891. }
  892. if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  893. fuse_backing_files_free(fc);
  894. call_rcu(&fc->rcu, delayed_release);
  895. }
  896. EXPORT_SYMBOL_GPL(fuse_conn_put);
  897. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  898. {
  899. refcount_inc(&fc->count);
  900. return fc;
  901. }
  902. EXPORT_SYMBOL_GPL(fuse_conn_get);
  903. static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned int mode)
  904. {
  905. struct fuse_attr attr;
  906. memset(&attr, 0, sizeof(attr));
  907. attr.mode = mode;
  908. attr.ino = FUSE_ROOT_ID;
  909. attr.nlink = 1;
  910. return fuse_iget(sb, FUSE_ROOT_ID, 0, &attr, 0, 0, 0);
  911. }
  912. struct fuse_inode_handle {
  913. u64 nodeid;
  914. u32 generation;
  915. };
  916. static struct dentry *fuse_get_dentry(struct super_block *sb,
  917. struct fuse_inode_handle *handle)
  918. {
  919. struct fuse_conn *fc = get_fuse_conn_super(sb);
  920. struct inode *inode;
  921. struct dentry *entry;
  922. int err = -ESTALE;
  923. if (handle->nodeid == 0)
  924. goto out_err;
  925. inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
  926. if (!inode) {
  927. struct fuse_entry_out outarg;
  928. const struct qstr name = QSTR_INIT(".", 1);
  929. if (!fc->export_support)
  930. goto out_err;
  931. err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
  932. &inode);
  933. if (err && err != -ENOENT)
  934. goto out_err;
  935. if (err || !inode) {
  936. err = -ESTALE;
  937. goto out_err;
  938. }
  939. err = -EIO;
  940. if (get_node_id(inode) != handle->nodeid)
  941. goto out_iput;
  942. }
  943. err = -ESTALE;
  944. if (inode->i_generation != handle->generation)
  945. goto out_iput;
  946. entry = d_obtain_alias(inode);
  947. if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
  948. fuse_invalidate_entry_cache(entry);
  949. return entry;
  950. out_iput:
  951. iput(inode);
  952. out_err:
  953. return ERR_PTR(err);
  954. }
  955. static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
  956. struct inode *parent)
  957. {
  958. int len = parent ? 6 : 3;
  959. u64 nodeid;
  960. u32 generation;
  961. if (*max_len < len) {
  962. *max_len = len;
  963. return FILEID_INVALID;
  964. }
  965. nodeid = get_fuse_inode(inode)->nodeid;
  966. generation = inode->i_generation;
  967. fh[0] = (u32)(nodeid >> 32);
  968. fh[1] = (u32)(nodeid & 0xffffffff);
  969. fh[2] = generation;
  970. if (parent) {
  971. nodeid = get_fuse_inode(parent)->nodeid;
  972. generation = parent->i_generation;
  973. fh[3] = (u32)(nodeid >> 32);
  974. fh[4] = (u32)(nodeid & 0xffffffff);
  975. fh[5] = generation;
  976. }
  977. *max_len = len;
  978. return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN;
  979. }
  980. static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
  981. struct fid *fid, int fh_len, int fh_type)
  982. {
  983. struct fuse_inode_handle handle;
  984. if ((fh_type != FILEID_INO64_GEN &&
  985. fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3)
  986. return NULL;
  987. handle.nodeid = (u64) fid->raw[0] << 32;
  988. handle.nodeid |= (u64) fid->raw[1];
  989. handle.generation = fid->raw[2];
  990. return fuse_get_dentry(sb, &handle);
  991. }
  992. static struct dentry *fuse_fh_to_parent(struct super_block *sb,
  993. struct fid *fid, int fh_len, int fh_type)
  994. {
  995. struct fuse_inode_handle parent;
  996. if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6)
  997. return NULL;
  998. parent.nodeid = (u64) fid->raw[3] << 32;
  999. parent.nodeid |= (u64) fid->raw[4];
  1000. parent.generation = fid->raw[5];
  1001. return fuse_get_dentry(sb, &parent);
  1002. }
  1003. static struct dentry *fuse_get_parent(struct dentry *child)
  1004. {
  1005. struct inode *child_inode = d_inode(child);
  1006. struct fuse_conn *fc = get_fuse_conn(child_inode);
  1007. struct inode *inode;
  1008. struct dentry *parent;
  1009. struct fuse_entry_out outarg;
  1010. int err;
  1011. if (!fc->export_support)
  1012. return ERR_PTR(-ESTALE);
  1013. err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
  1014. &dotdot_name, &outarg, &inode);
  1015. if (err) {
  1016. if (err == -ENOENT)
  1017. return ERR_PTR(-ESTALE);
  1018. return ERR_PTR(err);
  1019. }
  1020. parent = d_obtain_alias(inode);
  1021. if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
  1022. fuse_invalidate_entry_cache(parent);
  1023. return parent;
  1024. }
  1025. /* only for fid encoding; no support for file handle */
  1026. static const struct export_operations fuse_export_fid_operations = {
  1027. .encode_fh = fuse_encode_fh,
  1028. };
  1029. static const struct export_operations fuse_export_operations = {
  1030. .fh_to_dentry = fuse_fh_to_dentry,
  1031. .fh_to_parent = fuse_fh_to_parent,
  1032. .encode_fh = fuse_encode_fh,
  1033. .get_parent = fuse_get_parent,
  1034. };
  1035. static const struct super_operations fuse_super_operations = {
  1036. .alloc_inode = fuse_alloc_inode,
  1037. .free_inode = fuse_free_inode,
  1038. .evict_inode = fuse_evict_inode,
  1039. .write_inode = fuse_write_inode,
  1040. .drop_inode = inode_just_drop,
  1041. .umount_begin = fuse_umount_begin,
  1042. .statfs = fuse_statfs,
  1043. .sync_fs = fuse_sync_fs,
  1044. .show_options = fuse_show_options,
  1045. };
  1046. static void sanitize_global_limit(unsigned int *limit)
  1047. {
  1048. /*
  1049. * The default maximum number of async requests is calculated to consume
  1050. * 1/2^13 of the total memory, assuming 392 bytes per request.
  1051. */
  1052. if (*limit == 0)
  1053. *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
  1054. if (*limit >= 1 << 16)
  1055. *limit = (1 << 16) - 1;
  1056. }
  1057. static int set_global_limit(const char *val, const struct kernel_param *kp)
  1058. {
  1059. int rv;
  1060. rv = param_set_uint(val, kp);
  1061. if (rv)
  1062. return rv;
  1063. sanitize_global_limit((unsigned int *)kp->arg);
  1064. return 0;
  1065. }
  1066. static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
  1067. {
  1068. int cap_sys_admin = capable(CAP_SYS_ADMIN);
  1069. if (arg->minor < 13)
  1070. return;
  1071. sanitize_global_limit(&max_user_bgreq);
  1072. sanitize_global_limit(&max_user_congthresh);
  1073. spin_lock(&fc->bg_lock);
  1074. if (arg->max_background) {
  1075. fc->max_background = arg->max_background;
  1076. if (!cap_sys_admin && fc->max_background > max_user_bgreq)
  1077. fc->max_background = max_user_bgreq;
  1078. }
  1079. if (arg->congestion_threshold) {
  1080. fc->congestion_threshold = arg->congestion_threshold;
  1081. if (!cap_sys_admin &&
  1082. fc->congestion_threshold > max_user_congthresh)
  1083. fc->congestion_threshold = max_user_congthresh;
  1084. }
  1085. spin_unlock(&fc->bg_lock);
  1086. }
  1087. static void set_request_timeout(struct fuse_conn *fc, unsigned int timeout)
  1088. {
  1089. fc->timeout.req_timeout = secs_to_jiffies(timeout);
  1090. INIT_DELAYED_WORK(&fc->timeout.work, fuse_check_timeout);
  1091. queue_delayed_work(system_percpu_wq, &fc->timeout.work,
  1092. fuse_timeout_timer_freq);
  1093. }
  1094. static void init_server_timeout(struct fuse_conn *fc, unsigned int timeout)
  1095. {
  1096. if (!timeout && !fuse_max_req_timeout && !fuse_default_req_timeout)
  1097. return;
  1098. if (!timeout)
  1099. timeout = fuse_default_req_timeout;
  1100. if (fuse_max_req_timeout) {
  1101. if (timeout)
  1102. timeout = min(fuse_max_req_timeout, timeout);
  1103. else
  1104. timeout = fuse_max_req_timeout;
  1105. }
  1106. timeout = max(FUSE_TIMEOUT_TIMER_FREQ, timeout);
  1107. set_request_timeout(fc, timeout);
  1108. }
  1109. struct fuse_init_args {
  1110. struct fuse_args args;
  1111. struct fuse_init_in in;
  1112. struct fuse_init_out out;
  1113. };
  1114. static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
  1115. int error)
  1116. {
  1117. struct fuse_conn *fc = fm->fc;
  1118. struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
  1119. struct fuse_init_out *arg = &ia->out;
  1120. bool ok = true;
  1121. if (error || arg->major != FUSE_KERNEL_VERSION)
  1122. ok = false;
  1123. else {
  1124. unsigned long ra_pages;
  1125. unsigned int timeout = 0;
  1126. process_init_limits(fc, arg);
  1127. if (arg->minor >= 6) {
  1128. u64 flags = arg->flags;
  1129. if (flags & FUSE_INIT_EXT)
  1130. flags |= (u64) arg->flags2 << 32;
  1131. ra_pages = arg->max_readahead / PAGE_SIZE;
  1132. if (flags & FUSE_ASYNC_READ)
  1133. fc->async_read = 1;
  1134. if (!(flags & FUSE_POSIX_LOCKS))
  1135. fc->no_lock = 1;
  1136. if (arg->minor >= 17) {
  1137. if (!(flags & FUSE_FLOCK_LOCKS))
  1138. fc->no_flock = 1;
  1139. } else {
  1140. if (!(flags & FUSE_POSIX_LOCKS))
  1141. fc->no_flock = 1;
  1142. }
  1143. if (flags & FUSE_ATOMIC_O_TRUNC)
  1144. fc->atomic_o_trunc = 1;
  1145. if (arg->minor >= 9) {
  1146. /* LOOKUP has dependency on proto version */
  1147. if (flags & FUSE_EXPORT_SUPPORT)
  1148. fc->export_support = 1;
  1149. }
  1150. if (flags & FUSE_BIG_WRITES)
  1151. fc->big_writes = 1;
  1152. if (flags & FUSE_DONT_MASK)
  1153. fc->dont_mask = 1;
  1154. if (flags & FUSE_AUTO_INVAL_DATA)
  1155. fc->auto_inval_data = 1;
  1156. else if (flags & FUSE_EXPLICIT_INVAL_DATA)
  1157. fc->explicit_inval_data = 1;
  1158. if (flags & FUSE_DO_READDIRPLUS) {
  1159. fc->do_readdirplus = 1;
  1160. if (flags & FUSE_READDIRPLUS_AUTO)
  1161. fc->readdirplus_auto = 1;
  1162. }
  1163. if (flags & FUSE_ASYNC_DIO)
  1164. fc->async_dio = 1;
  1165. if (flags & FUSE_WRITEBACK_CACHE)
  1166. fc->writeback_cache = 1;
  1167. if (flags & FUSE_PARALLEL_DIROPS)
  1168. fc->parallel_dirops = 1;
  1169. if (flags & FUSE_HANDLE_KILLPRIV)
  1170. fc->handle_killpriv = 1;
  1171. if (arg->time_gran && arg->time_gran <= 1000000000)
  1172. fm->sb->s_time_gran = arg->time_gran;
  1173. if ((flags & FUSE_POSIX_ACL)) {
  1174. fc->default_permissions = 1;
  1175. fc->posix_acl = 1;
  1176. }
  1177. if (flags & FUSE_CACHE_SYMLINKS)
  1178. fc->cache_symlinks = 1;
  1179. if (flags & FUSE_ABORT_ERROR)
  1180. fc->abort_err = 1;
  1181. if (flags & FUSE_MAX_PAGES) {
  1182. fc->max_pages =
  1183. min_t(unsigned int, fc->max_pages_limit,
  1184. max_t(unsigned int, arg->max_pages, 1));
  1185. /*
  1186. * PATH_MAX file names might need two pages for
  1187. * ops like rename
  1188. */
  1189. if (fc->max_pages > 1)
  1190. fc->name_max = FUSE_NAME_MAX;
  1191. }
  1192. if (IS_ENABLED(CONFIG_FUSE_DAX)) {
  1193. if (flags & FUSE_MAP_ALIGNMENT &&
  1194. !fuse_dax_check_alignment(fc, arg->map_alignment)) {
  1195. ok = false;
  1196. }
  1197. if (flags & FUSE_HAS_INODE_DAX)
  1198. fc->inode_dax = 1;
  1199. }
  1200. if (flags & FUSE_HANDLE_KILLPRIV_V2) {
  1201. fc->handle_killpriv_v2 = 1;
  1202. fm->sb->s_flags |= SB_NOSEC;
  1203. }
  1204. if (flags & FUSE_SETXATTR_EXT)
  1205. fc->setxattr_ext = 1;
  1206. if (flags & FUSE_SECURITY_CTX)
  1207. fc->init_security = 1;
  1208. if (flags & FUSE_CREATE_SUPP_GROUP)
  1209. fc->create_supp_group = 1;
  1210. if (flags & FUSE_DIRECT_IO_ALLOW_MMAP)
  1211. fc->direct_io_allow_mmap = 1;
  1212. /*
  1213. * max_stack_depth is the max stack depth of FUSE fs,
  1214. * so it has to be at least 1 to support passthrough
  1215. * to backing files.
  1216. *
  1217. * with max_stack_depth > 1, the backing files can be
  1218. * on a stacked fs (e.g. overlayfs) themselves and with
  1219. * max_stack_depth == 1, FUSE fs can be stacked as the
  1220. * underlying fs of a stacked fs (e.g. overlayfs).
  1221. *
  1222. * Also don't allow the combination of FUSE_PASSTHROUGH
  1223. * and FUSE_WRITEBACK_CACHE, current design doesn't handle
  1224. * them together.
  1225. */
  1226. if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) &&
  1227. (flags & FUSE_PASSTHROUGH) &&
  1228. arg->max_stack_depth > 0 &&
  1229. arg->max_stack_depth <= FILESYSTEM_MAX_STACK_DEPTH &&
  1230. !(flags & FUSE_WRITEBACK_CACHE)) {
  1231. fc->passthrough = 1;
  1232. fc->max_stack_depth = arg->max_stack_depth;
  1233. fm->sb->s_stack_depth = arg->max_stack_depth;
  1234. }
  1235. if (flags & FUSE_NO_EXPORT_SUPPORT)
  1236. fm->sb->s_export_op = &fuse_export_fid_operations;
  1237. if (flags & FUSE_ALLOW_IDMAP) {
  1238. if (fc->default_permissions)
  1239. fm->sb->s_iflags &= ~SB_I_NOIDMAP;
  1240. else
  1241. ok = false;
  1242. }
  1243. if (flags & FUSE_OVER_IO_URING && fuse_uring_enabled())
  1244. fc->io_uring = 1;
  1245. if (flags & FUSE_REQUEST_TIMEOUT)
  1246. timeout = arg->request_timeout;
  1247. } else {
  1248. ra_pages = fc->max_read / PAGE_SIZE;
  1249. fc->no_lock = 1;
  1250. fc->no_flock = 1;
  1251. }
  1252. init_server_timeout(fc, timeout);
  1253. fm->sb->s_bdi->ra_pages =
  1254. min(fm->sb->s_bdi->ra_pages, ra_pages);
  1255. fc->minor = arg->minor;
  1256. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  1257. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  1258. fc->conn_init = 1;
  1259. }
  1260. kfree(ia);
  1261. if (!ok) {
  1262. fc->conn_init = 0;
  1263. fc->conn_error = 1;
  1264. }
  1265. fuse_set_initialized(fc);
  1266. wake_up_all(&fc->blocked_waitq);
  1267. }
  1268. static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
  1269. {
  1270. struct fuse_init_args *ia;
  1271. u64 flags;
  1272. ia = kzalloc_obj(*ia, GFP_KERNEL | __GFP_NOFAIL);
  1273. ia->in.major = FUSE_KERNEL_VERSION;
  1274. ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
  1275. ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
  1276. flags =
  1277. FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  1278. FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
  1279. FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
  1280. FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
  1281. FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
  1282. FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
  1283. FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
  1284. FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
  1285. FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
  1286. FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
  1287. FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
  1288. FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP |
  1289. FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP |
  1290. FUSE_REQUEST_TIMEOUT;
  1291. #ifdef CONFIG_FUSE_DAX
  1292. if (fm->fc->dax)
  1293. flags |= FUSE_MAP_ALIGNMENT;
  1294. if (fuse_is_inode_dax_mode(fm->fc->dax_mode))
  1295. flags |= FUSE_HAS_INODE_DAX;
  1296. #endif
  1297. if (fm->fc->auto_submounts)
  1298. flags |= FUSE_SUBMOUNTS;
  1299. if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  1300. flags |= FUSE_PASSTHROUGH;
  1301. /*
  1302. * This is just an information flag for fuse server. No need to check
  1303. * the reply - server is either sending IORING_OP_URING_CMD or not.
  1304. */
  1305. if (fuse_uring_enabled())
  1306. flags |= FUSE_OVER_IO_URING;
  1307. ia->in.flags = flags;
  1308. ia->in.flags2 = flags >> 32;
  1309. ia->args.opcode = FUSE_INIT;
  1310. ia->args.in_numargs = 1;
  1311. ia->args.in_args[0].size = sizeof(ia->in);
  1312. ia->args.in_args[0].value = &ia->in;
  1313. ia->args.out_numargs = 1;
  1314. /* Variable length argument used for backward compatibility
  1315. with interface version < 7.5. Rest of init_out is zeroed
  1316. by do_get_request(), so a short reply is not a problem */
  1317. ia->args.out_argvar = true;
  1318. ia->args.out_args[0].size = sizeof(ia->out);
  1319. ia->args.out_args[0].value = &ia->out;
  1320. ia->args.force = true;
  1321. ia->args.nocreds = true;
  1322. return ia;
  1323. }
  1324. int fuse_send_init(struct fuse_mount *fm)
  1325. {
  1326. struct fuse_init_args *ia = fuse_new_init(fm);
  1327. int err;
  1328. if (fm->fc->sync_init) {
  1329. err = fuse_simple_request(fm, &ia->args);
  1330. /* Ignore size of init reply */
  1331. if (err > 0)
  1332. err = 0;
  1333. } else {
  1334. ia->args.end = process_init_reply;
  1335. err = fuse_simple_background(fm, &ia->args, GFP_KERNEL);
  1336. if (!err)
  1337. return 0;
  1338. }
  1339. process_init_reply(fm, &ia->args, err);
  1340. if (fm->fc->conn_error)
  1341. return -ENOTCONN;
  1342. return 0;
  1343. }
  1344. EXPORT_SYMBOL_GPL(fuse_send_init);
  1345. void fuse_free_conn(struct fuse_conn *fc)
  1346. {
  1347. WARN_ON(!list_empty(&fc->devices));
  1348. kfree(fc);
  1349. }
  1350. EXPORT_SYMBOL_GPL(fuse_free_conn);
  1351. static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
  1352. {
  1353. int err;
  1354. char *suffix = "";
  1355. if (sb->s_bdev) {
  1356. suffix = "-fuseblk";
  1357. /*
  1358. * sb->s_bdi points to blkdev's bdi however we want to redirect
  1359. * it to our private bdi...
  1360. */
  1361. bdi_put(sb->s_bdi);
  1362. sb->s_bdi = &noop_backing_dev_info;
  1363. }
  1364. err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
  1365. MINOR(fc->dev), suffix);
  1366. if (err)
  1367. return err;
  1368. sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
  1369. /*
  1370. * For a single fuse filesystem use max 1% of dirty +
  1371. * writeback threshold.
  1372. *
  1373. * This gives about 1M of write buffer for memory maps on a
  1374. * machine with 1G and 10% dirty_ratio, which should be more
  1375. * than enough.
  1376. *
  1377. * Privileged users can raise it by writing to
  1378. *
  1379. * /sys/class/bdi/<bdi>/max_ratio
  1380. */
  1381. bdi_set_max_ratio(sb->s_bdi, 1);
  1382. return 0;
  1383. }
  1384. struct fuse_dev *fuse_dev_alloc(void)
  1385. {
  1386. struct fuse_dev *fud;
  1387. struct list_head *pq;
  1388. fud = kzalloc_obj(struct fuse_dev);
  1389. if (!fud)
  1390. return NULL;
  1391. pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
  1392. if (!pq) {
  1393. kfree(fud);
  1394. return NULL;
  1395. }
  1396. fud->pq.processing = pq;
  1397. fuse_pqueue_init(&fud->pq);
  1398. return fud;
  1399. }
  1400. EXPORT_SYMBOL_GPL(fuse_dev_alloc);
  1401. void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
  1402. {
  1403. fud->fc = fuse_conn_get(fc);
  1404. spin_lock(&fc->lock);
  1405. list_add_tail(&fud->entry, &fc->devices);
  1406. spin_unlock(&fc->lock);
  1407. }
  1408. EXPORT_SYMBOL_GPL(fuse_dev_install);
  1409. struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
  1410. {
  1411. struct fuse_dev *fud;
  1412. fud = fuse_dev_alloc();
  1413. if (!fud)
  1414. return NULL;
  1415. fuse_dev_install(fud, fc);
  1416. return fud;
  1417. }
  1418. EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
  1419. void fuse_dev_free(struct fuse_dev *fud)
  1420. {
  1421. struct fuse_conn *fc = fud->fc;
  1422. if (fc) {
  1423. spin_lock(&fc->lock);
  1424. list_del(&fud->entry);
  1425. spin_unlock(&fc->lock);
  1426. fuse_conn_put(fc);
  1427. }
  1428. kfree(fud->pq.processing);
  1429. kfree(fud);
  1430. }
  1431. EXPORT_SYMBOL_GPL(fuse_dev_free);
  1432. static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
  1433. const struct fuse_inode *fi)
  1434. {
  1435. struct timespec64 atime = inode_get_atime(&fi->inode);
  1436. struct timespec64 mtime = inode_get_mtime(&fi->inode);
  1437. struct timespec64 ctime = inode_get_ctime(&fi->inode);
  1438. *attr = (struct fuse_attr){
  1439. .ino = fi->inode.i_ino,
  1440. .size = fi->inode.i_size,
  1441. .blocks = fi->inode.i_blocks,
  1442. .atime = atime.tv_sec,
  1443. .mtime = mtime.tv_sec,
  1444. .ctime = ctime.tv_sec,
  1445. .atimensec = atime.tv_nsec,
  1446. .mtimensec = mtime.tv_nsec,
  1447. .ctimensec = ctime.tv_nsec,
  1448. .mode = fi->inode.i_mode,
  1449. .nlink = fi->inode.i_nlink,
  1450. .uid = __kuid_val(fi->inode.i_uid),
  1451. .gid = __kgid_val(fi->inode.i_gid),
  1452. .rdev = fi->inode.i_rdev,
  1453. .blksize = 1u << fi->inode.i_blkbits,
  1454. };
  1455. }
  1456. static void fuse_sb_defaults(struct super_block *sb)
  1457. {
  1458. sb->s_magic = FUSE_SUPER_MAGIC;
  1459. sb->s_op = &fuse_super_operations;
  1460. sb->s_xattr = fuse_xattr_handlers;
  1461. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1462. sb->s_time_gran = 1;
  1463. sb->s_export_op = &fuse_export_operations;
  1464. sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
  1465. sb->s_iflags |= SB_I_NOIDMAP;
  1466. sb->s_iflags |= SB_I_NO_DATA_INTEGRITY;
  1467. if (sb->s_user_ns != &init_user_ns)
  1468. sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
  1469. sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
  1470. }
  1471. static int fuse_fill_super_submount(struct super_block *sb,
  1472. struct fuse_inode *parent_fi)
  1473. {
  1474. struct fuse_mount *fm = get_fuse_mount_super(sb);
  1475. struct super_block *parent_sb = parent_fi->inode.i_sb;
  1476. struct fuse_attr root_attr;
  1477. struct inode *root;
  1478. struct fuse_submount_lookup *sl;
  1479. struct fuse_inode *fi;
  1480. fuse_sb_defaults(sb);
  1481. fm->sb = sb;
  1482. WARN_ON(sb->s_bdi != &noop_backing_dev_info);
  1483. sb->s_bdi = bdi_get(parent_sb->s_bdi);
  1484. sb->s_xattr = parent_sb->s_xattr;
  1485. sb->s_export_op = parent_sb->s_export_op;
  1486. sb->s_time_gran = parent_sb->s_time_gran;
  1487. sb->s_blocksize = parent_sb->s_blocksize;
  1488. sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
  1489. sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
  1490. if (parent_sb->s_subtype && !sb->s_subtype)
  1491. return -ENOMEM;
  1492. fuse_fill_attr_from_inode(&root_attr, parent_fi);
  1493. root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0,
  1494. fuse_get_evict_ctr(fm->fc));
  1495. /*
  1496. * This inode is just a duplicate, so it is not looked up and
  1497. * its nlookup should not be incremented. fuse_iget() does
  1498. * that, though, so undo it here.
  1499. */
  1500. fi = get_fuse_inode(root);
  1501. fi->nlookup--;
  1502. set_default_d_op(sb, &fuse_dentry_operations);
  1503. sb->s_root = d_make_root(root);
  1504. if (!sb->s_root)
  1505. return -ENOMEM;
  1506. /*
  1507. * Grab the parent's submount_lookup pointer and take a
  1508. * reference on the shared nlookup from the parent. This is to
  1509. * prevent the last forget for this nodeid from getting
  1510. * triggered until all users have finished with it.
  1511. */
  1512. sl = parent_fi->submount_lookup;
  1513. WARN_ON(!sl);
  1514. if (sl) {
  1515. refcount_inc(&sl->count);
  1516. fi->submount_lookup = sl;
  1517. }
  1518. return 0;
  1519. }
  1520. /* Filesystem context private data holds the FUSE inode of the mount point */
  1521. static int fuse_get_tree_submount(struct fs_context *fsc)
  1522. {
  1523. struct fuse_mount *fm;
  1524. struct fuse_inode *mp_fi = fsc->fs_private;
  1525. struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
  1526. struct super_block *sb;
  1527. int err;
  1528. fm = kzalloc_obj(struct fuse_mount);
  1529. if (!fm)
  1530. return -ENOMEM;
  1531. fm->fc = fuse_conn_get(fc);
  1532. fsc->s_fs_info = fm;
  1533. sb = sget_fc(fsc, NULL, set_anon_super_fc);
  1534. if (fsc->s_fs_info)
  1535. fuse_mount_destroy(fm);
  1536. if (IS_ERR(sb))
  1537. return PTR_ERR(sb);
  1538. /* Initialize superblock, making @mp_fi its root */
  1539. err = fuse_fill_super_submount(sb, mp_fi);
  1540. if (err) {
  1541. deactivate_locked_super(sb);
  1542. return err;
  1543. }
  1544. down_write(&fc->killsb);
  1545. list_add_tail(&fm->fc_entry, &fc->mounts);
  1546. up_write(&fc->killsb);
  1547. sb->s_flags |= SB_ACTIVE;
  1548. fsc->root = dget(sb->s_root);
  1549. return 0;
  1550. }
  1551. static const struct fs_context_operations fuse_context_submount_ops = {
  1552. .get_tree = fuse_get_tree_submount,
  1553. };
  1554. int fuse_init_fs_context_submount(struct fs_context *fsc)
  1555. {
  1556. fsc->ops = &fuse_context_submount_ops;
  1557. return 0;
  1558. }
  1559. EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
  1560. int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
  1561. {
  1562. struct fuse_dev *fud = NULL;
  1563. struct fuse_mount *fm = get_fuse_mount_super(sb);
  1564. struct fuse_conn *fc = fm->fc;
  1565. struct inode *root;
  1566. struct dentry *root_dentry;
  1567. int err;
  1568. err = -EINVAL;
  1569. if (sb->s_flags & SB_MANDLOCK)
  1570. goto err;
  1571. rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
  1572. fuse_sb_defaults(sb);
  1573. if (ctx->is_bdev) {
  1574. #ifdef CONFIG_BLOCK
  1575. err = -EINVAL;
  1576. if (!sb_set_blocksize(sb, ctx->blksize))
  1577. goto err;
  1578. #endif
  1579. fc->sync_fs = 1;
  1580. } else {
  1581. sb->s_blocksize = PAGE_SIZE;
  1582. sb->s_blocksize_bits = PAGE_SHIFT;
  1583. }
  1584. sb->s_subtype = ctx->subtype;
  1585. ctx->subtype = NULL;
  1586. if (IS_ENABLED(CONFIG_FUSE_DAX)) {
  1587. err = fuse_dax_conn_alloc(fc, ctx->dax_mode, ctx->dax_dev);
  1588. if (err)
  1589. goto err;
  1590. }
  1591. if (ctx->fudptr) {
  1592. err = -ENOMEM;
  1593. fud = fuse_dev_alloc_install(fc);
  1594. if (!fud)
  1595. goto err_free_dax;
  1596. }
  1597. fc->dev = sb->s_dev;
  1598. fm->sb = sb;
  1599. err = fuse_bdi_init(fc, sb);
  1600. if (err)
  1601. goto err_dev_free;
  1602. /* Handle umasking inside the fuse code */
  1603. if (sb->s_flags & SB_POSIXACL)
  1604. fc->dont_mask = 1;
  1605. sb->s_flags |= SB_POSIXACL;
  1606. fc->default_permissions = ctx->default_permissions;
  1607. fc->allow_other = ctx->allow_other;
  1608. fc->user_id = ctx->user_id;
  1609. fc->group_id = ctx->group_id;
  1610. fc->legacy_opts_show = ctx->legacy_opts_show;
  1611. fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
  1612. fc->destroy = ctx->destroy;
  1613. fc->no_control = ctx->no_control;
  1614. fc->no_force_umount = ctx->no_force_umount;
  1615. err = -ENOMEM;
  1616. root = fuse_get_root_inode(sb, ctx->rootmode);
  1617. set_default_d_op(sb, &fuse_dentry_operations);
  1618. root_dentry = d_make_root(root);
  1619. if (!root_dentry)
  1620. goto err_dev_free;
  1621. mutex_lock(&fuse_mutex);
  1622. err = -EINVAL;
  1623. if (ctx->fudptr && *ctx->fudptr) {
  1624. if (*ctx->fudptr == FUSE_DEV_SYNC_INIT)
  1625. fc->sync_init = 1;
  1626. else
  1627. goto err_unlock;
  1628. }
  1629. err = fuse_ctl_add_conn(fc);
  1630. if (err)
  1631. goto err_unlock;
  1632. list_add_tail(&fc->entry, &fuse_conn_list);
  1633. sb->s_root = root_dentry;
  1634. if (ctx->fudptr) {
  1635. *ctx->fudptr = fud;
  1636. wake_up_all(&fuse_dev_waitq);
  1637. }
  1638. mutex_unlock(&fuse_mutex);
  1639. return 0;
  1640. err_unlock:
  1641. mutex_unlock(&fuse_mutex);
  1642. dput(root_dentry);
  1643. err_dev_free:
  1644. if (fud)
  1645. fuse_dev_free(fud);
  1646. err_free_dax:
  1647. if (IS_ENABLED(CONFIG_FUSE_DAX))
  1648. fuse_dax_conn_free(fc);
  1649. err:
  1650. return err;
  1651. }
  1652. EXPORT_SYMBOL_GPL(fuse_fill_super_common);
  1653. static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
  1654. {
  1655. struct fuse_fs_context *ctx = fsc->fs_private;
  1656. struct fuse_mount *fm;
  1657. int err;
  1658. if (!ctx->file || !ctx->rootmode_present ||
  1659. !ctx->user_id_present || !ctx->group_id_present)
  1660. return -EINVAL;
  1661. /*
  1662. * Require mount to happen from the same user namespace which
  1663. * opened /dev/fuse to prevent potential attacks.
  1664. */
  1665. if ((ctx->file->f_op != &fuse_dev_operations) ||
  1666. (ctx->file->f_cred->user_ns != sb->s_user_ns))
  1667. return -EINVAL;
  1668. ctx->fudptr = &ctx->file->private_data;
  1669. err = fuse_fill_super_common(sb, ctx);
  1670. if (err)
  1671. return err;
  1672. /* file->private_data shall be visible on all CPUs after this */
  1673. smp_mb();
  1674. fm = get_fuse_mount_super(sb);
  1675. return fuse_send_init(fm);
  1676. }
  1677. /*
  1678. * This is the path where user supplied an already initialized fuse dev. In
  1679. * this case never create a new super if the old one is gone.
  1680. */
  1681. static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
  1682. {
  1683. return -ENOTCONN;
  1684. }
  1685. static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
  1686. {
  1687. return fsc->sget_key == get_fuse_conn_super(sb);
  1688. }
  1689. static int fuse_get_tree(struct fs_context *fsc)
  1690. {
  1691. struct fuse_fs_context *ctx = fsc->fs_private;
  1692. struct fuse_dev *fud;
  1693. struct fuse_conn *fc;
  1694. struct fuse_mount *fm;
  1695. struct super_block *sb;
  1696. int err;
  1697. fc = kmalloc_obj(*fc);
  1698. if (!fc)
  1699. return -ENOMEM;
  1700. fm = kzalloc_obj(*fm);
  1701. if (!fm) {
  1702. kfree(fc);
  1703. return -ENOMEM;
  1704. }
  1705. fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
  1706. fc->release = fuse_free_conn;
  1707. fsc->s_fs_info = fm;
  1708. if (ctx->fd_present)
  1709. ctx->file = fget(ctx->fd);
  1710. if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
  1711. err = get_tree_bdev(fsc, fuse_fill_super);
  1712. goto out;
  1713. }
  1714. /*
  1715. * While block dev mount can be initialized with a dummy device fd
  1716. * (found by device name), normal fuse mounts can't
  1717. */
  1718. err = -EINVAL;
  1719. if (!ctx->file)
  1720. goto out;
  1721. /*
  1722. * Allow creating a fuse mount with an already initialized fuse
  1723. * connection
  1724. */
  1725. fud = __fuse_get_dev(ctx->file);
  1726. if (ctx->file->f_op == &fuse_dev_operations && fud) {
  1727. fsc->sget_key = fud->fc;
  1728. sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
  1729. err = PTR_ERR_OR_ZERO(sb);
  1730. if (!IS_ERR(sb))
  1731. fsc->root = dget(sb->s_root);
  1732. } else {
  1733. err = get_tree_nodev(fsc, fuse_fill_super);
  1734. }
  1735. out:
  1736. if (fsc->s_fs_info)
  1737. fuse_mount_destroy(fm);
  1738. if (ctx->file)
  1739. fput(ctx->file);
  1740. return err;
  1741. }
  1742. static const struct fs_context_operations fuse_context_ops = {
  1743. .free = fuse_free_fsc,
  1744. .parse_param = fuse_parse_param,
  1745. .reconfigure = fuse_reconfigure,
  1746. .get_tree = fuse_get_tree,
  1747. };
  1748. /*
  1749. * Set up the filesystem mount context.
  1750. */
  1751. static int fuse_init_fs_context(struct fs_context *fsc)
  1752. {
  1753. struct fuse_fs_context *ctx;
  1754. ctx = kzalloc_obj(struct fuse_fs_context);
  1755. if (!ctx)
  1756. return -ENOMEM;
  1757. ctx->max_read = ~0;
  1758. ctx->blksize = FUSE_DEFAULT_BLKSIZE;
  1759. ctx->legacy_opts_show = true;
  1760. #ifdef CONFIG_BLOCK
  1761. if (fsc->fs_type == &fuseblk_fs_type) {
  1762. ctx->is_bdev = true;
  1763. ctx->destroy = true;
  1764. }
  1765. #endif
  1766. fsc->fs_private = ctx;
  1767. fsc->ops = &fuse_context_ops;
  1768. return 0;
  1769. }
  1770. bool fuse_mount_remove(struct fuse_mount *fm)
  1771. {
  1772. struct fuse_conn *fc = fm->fc;
  1773. bool last = false;
  1774. down_write(&fc->killsb);
  1775. list_del_init(&fm->fc_entry);
  1776. if (list_empty(&fc->mounts))
  1777. last = true;
  1778. up_write(&fc->killsb);
  1779. return last;
  1780. }
  1781. EXPORT_SYMBOL_GPL(fuse_mount_remove);
  1782. void fuse_conn_destroy(struct fuse_mount *fm)
  1783. {
  1784. struct fuse_conn *fc = fm->fc;
  1785. if (fc->destroy)
  1786. fuse_send_destroy(fm);
  1787. fuse_abort_conn(fc);
  1788. fuse_wait_aborted(fc);
  1789. if (!list_empty(&fc->entry)) {
  1790. mutex_lock(&fuse_mutex);
  1791. list_del(&fc->entry);
  1792. fuse_ctl_remove_conn(fc);
  1793. mutex_unlock(&fuse_mutex);
  1794. }
  1795. }
  1796. EXPORT_SYMBOL_GPL(fuse_conn_destroy);
  1797. static void fuse_sb_destroy(struct super_block *sb)
  1798. {
  1799. struct fuse_mount *fm = get_fuse_mount_super(sb);
  1800. bool last;
  1801. if (sb->s_root) {
  1802. last = fuse_mount_remove(fm);
  1803. if (last)
  1804. fuse_conn_destroy(fm);
  1805. }
  1806. }
  1807. void fuse_mount_destroy(struct fuse_mount *fm)
  1808. {
  1809. fuse_conn_put(fm->fc);
  1810. kfree_rcu(fm, rcu);
  1811. }
  1812. EXPORT_SYMBOL(fuse_mount_destroy);
  1813. static void fuse_kill_sb_anon(struct super_block *sb)
  1814. {
  1815. fuse_sb_destroy(sb);
  1816. kill_anon_super(sb);
  1817. fuse_mount_destroy(get_fuse_mount_super(sb));
  1818. }
  1819. static struct file_system_type fuse_fs_type = {
  1820. .owner = THIS_MODULE,
  1821. .name = "fuse",
  1822. .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT | FS_ALLOW_IDMAP,
  1823. .init_fs_context = fuse_init_fs_context,
  1824. .parameters = fuse_fs_parameters,
  1825. .kill_sb = fuse_kill_sb_anon,
  1826. };
  1827. MODULE_ALIAS_FS("fuse");
  1828. #ifdef CONFIG_BLOCK
  1829. static void fuse_kill_sb_blk(struct super_block *sb)
  1830. {
  1831. fuse_sb_destroy(sb);
  1832. kill_block_super(sb);
  1833. fuse_mount_destroy(get_fuse_mount_super(sb));
  1834. }
  1835. static struct file_system_type fuseblk_fs_type = {
  1836. .owner = THIS_MODULE,
  1837. .name = "fuseblk",
  1838. .init_fs_context = fuse_init_fs_context,
  1839. .parameters = fuse_fs_parameters,
  1840. .kill_sb = fuse_kill_sb_blk,
  1841. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE | FS_ALLOW_IDMAP,
  1842. };
  1843. MODULE_ALIAS_FS("fuseblk");
  1844. static inline int register_fuseblk(void)
  1845. {
  1846. return register_filesystem(&fuseblk_fs_type);
  1847. }
  1848. static inline void unregister_fuseblk(void)
  1849. {
  1850. unregister_filesystem(&fuseblk_fs_type);
  1851. }
  1852. #else
  1853. static inline int register_fuseblk(void)
  1854. {
  1855. return 0;
  1856. }
  1857. static inline void unregister_fuseblk(void)
  1858. {
  1859. }
  1860. #endif
  1861. static void fuse_inode_init_once(void *foo)
  1862. {
  1863. struct inode *inode = foo;
  1864. inode_init_once(inode);
  1865. }
  1866. static int __init fuse_fs_init(void)
  1867. {
  1868. int err;
  1869. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  1870. sizeof(struct fuse_inode), 0,
  1871. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
  1872. fuse_inode_init_once);
  1873. err = -ENOMEM;
  1874. if (!fuse_inode_cachep)
  1875. goto out;
  1876. err = register_fuseblk();
  1877. if (err)
  1878. goto out2;
  1879. err = register_filesystem(&fuse_fs_type);
  1880. if (err)
  1881. goto out3;
  1882. err = fuse_sysctl_register();
  1883. if (err)
  1884. goto out4;
  1885. return 0;
  1886. out4:
  1887. unregister_filesystem(&fuse_fs_type);
  1888. out3:
  1889. unregister_fuseblk();
  1890. out2:
  1891. kmem_cache_destroy(fuse_inode_cachep);
  1892. out:
  1893. return err;
  1894. }
  1895. static void fuse_fs_cleanup(void)
  1896. {
  1897. fuse_sysctl_unregister();
  1898. unregister_filesystem(&fuse_fs_type);
  1899. unregister_fuseblk();
  1900. /*
  1901. * Make sure all delayed rcu free inodes are flushed before we
  1902. * destroy cache.
  1903. */
  1904. rcu_barrier();
  1905. kmem_cache_destroy(fuse_inode_cachep);
  1906. }
  1907. static struct kobject *fuse_kobj;
  1908. static int fuse_sysfs_init(void)
  1909. {
  1910. int err;
  1911. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  1912. if (!fuse_kobj) {
  1913. err = -ENOMEM;
  1914. goto out_err;
  1915. }
  1916. err = sysfs_create_mount_point(fuse_kobj, "connections");
  1917. if (err)
  1918. goto out_fuse_unregister;
  1919. return 0;
  1920. out_fuse_unregister:
  1921. kobject_put(fuse_kobj);
  1922. out_err:
  1923. return err;
  1924. }
  1925. static void fuse_sysfs_cleanup(void)
  1926. {
  1927. sysfs_remove_mount_point(fuse_kobj, "connections");
  1928. kobject_put(fuse_kobj);
  1929. }
  1930. static int __init fuse_init(void)
  1931. {
  1932. int res;
  1933. pr_info("init (API version %i.%i)\n",
  1934. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  1935. INIT_LIST_HEAD(&fuse_conn_list);
  1936. res = fuse_fs_init();
  1937. if (res)
  1938. goto err;
  1939. res = fuse_dev_init();
  1940. if (res)
  1941. goto err_fs_cleanup;
  1942. res = fuse_sysfs_init();
  1943. if (res)
  1944. goto err_dev_cleanup;
  1945. res = fuse_ctl_init();
  1946. if (res)
  1947. goto err_sysfs_cleanup;
  1948. fuse_dentry_tree_init();
  1949. sanitize_global_limit(&max_user_bgreq);
  1950. sanitize_global_limit(&max_user_congthresh);
  1951. return 0;
  1952. err_sysfs_cleanup:
  1953. fuse_sysfs_cleanup();
  1954. err_dev_cleanup:
  1955. fuse_dev_cleanup();
  1956. err_fs_cleanup:
  1957. fuse_fs_cleanup();
  1958. err:
  1959. return res;
  1960. }
  1961. static void __exit fuse_exit(void)
  1962. {
  1963. pr_debug("exit\n");
  1964. fuse_dentry_tree_cleanup();
  1965. fuse_ctl_cleanup();
  1966. fuse_sysfs_cleanup();
  1967. fuse_fs_cleanup();
  1968. fuse_dev_cleanup();
  1969. }
  1970. module_init(fuse_init);
  1971. module_exit(fuse_exit);