super.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2011 Novell Inc.
  5. */
  6. #include <uapi/linux/magic.h>
  7. #include <linux/fs.h>
  8. #include <linux/namei.h>
  9. #include <linux/xattr.h>
  10. #include <linux/mount.h>
  11. #include <linux/parser.h>
  12. #include <linux/module.h>
  13. #include <linux/statfs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/posix_acl_xattr.h>
  16. #include <linux/exportfs.h>
  17. #include <linux/file.h>
  18. #include "overlayfs.h"
  19. #ifdef CONFIG_KDP_NS
  20. #include <linux/kdp.h>
  21. #endif
  22. MODULE_AUTHOR("Miklos Szeredi <[email protected]>");
  23. MODULE_DESCRIPTION("Overlay filesystem");
  24. MODULE_LICENSE("GPL");
  25. struct ovl_dir_cache;
  26. #define OVL_MAX_STACK 500
  27. static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
  28. module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
  29. MODULE_PARM_DESC(redirect_dir,
  30. "Default to on or off for the redirect_dir feature");
  31. static bool ovl_redirect_always_follow =
  32. IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
  33. module_param_named(redirect_always_follow, ovl_redirect_always_follow,
  34. bool, 0644);
  35. MODULE_PARM_DESC(redirect_always_follow,
  36. "Follow redirects even if redirect_dir feature is turned off");
  37. static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
  38. module_param_named(index, ovl_index_def, bool, 0644);
  39. MODULE_PARM_DESC(index,
  40. "Default to on or off for the inodes index feature");
  41. static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
  42. module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
  43. MODULE_PARM_DESC(nfs_export,
  44. "Default to on or off for the NFS export feature");
  45. static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
  46. module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
  47. MODULE_PARM_DESC(xino_auto,
  48. "Auto enable xino feature");
  49. static bool __read_mostly ovl_override_creds_def = true;
  50. module_param_named(override_creds, ovl_override_creds_def, bool, 0644);
  51. MODULE_PARM_DESC(ovl_override_creds_def,
  52. "Use mounter's credentials for accesses");
  53. static void ovl_entry_stack_free(struct ovl_entry *oe)
  54. {
  55. unsigned int i;
  56. for (i = 0; i < oe->numlower; i++)
  57. dput(oe->lowerstack[i].dentry);
  58. }
  59. static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
  60. module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
  61. MODULE_PARM_DESC(metacopy,
  62. "Default to on or off for the metadata only copy up feature");
  63. static void ovl_dentry_release(struct dentry *dentry)
  64. {
  65. struct ovl_entry *oe = dentry->d_fsdata;
  66. if (oe) {
  67. ovl_entry_stack_free(oe);
  68. kfree_rcu(oe, rcu);
  69. }
  70. }
  71. static struct dentry *ovl_d_real(struct dentry *dentry,
  72. const struct inode *inode)
  73. {
  74. struct dentry *real = NULL, *lower;
  75. /* It's an overlay file */
  76. if (inode && d_inode(dentry) == inode)
  77. return dentry;
  78. if (!d_is_reg(dentry)) {
  79. if (!inode || inode == d_inode(dentry))
  80. return dentry;
  81. goto bug;
  82. }
  83. real = ovl_dentry_upper(dentry);
  84. if (real && (inode == d_inode(real)))
  85. return real;
  86. if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
  87. return real;
  88. lower = ovl_dentry_lowerdata(dentry);
  89. if (!lower)
  90. goto bug;
  91. real = lower;
  92. /* Handle recursion */
  93. real = d_real(real, inode);
  94. if (!inode || inode == d_inode(real))
  95. return real;
  96. bug:
  97. WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
  98. __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
  99. inode ? inode->i_ino : 0, real,
  100. real && d_inode(real) ? d_inode(real)->i_ino : 0);
  101. return dentry;
  102. }
  103. static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
  104. {
  105. int ret = 1;
  106. if (weak) {
  107. if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
  108. ret = d->d_op->d_weak_revalidate(d, flags);
  109. } else if (d->d_flags & DCACHE_OP_REVALIDATE) {
  110. ret = d->d_op->d_revalidate(d, flags);
  111. if (!ret) {
  112. if (!(flags & LOOKUP_RCU))
  113. d_invalidate(d);
  114. ret = -ESTALE;
  115. }
  116. }
  117. return ret;
  118. }
  119. static int ovl_dentry_revalidate_common(struct dentry *dentry,
  120. unsigned int flags, bool weak)
  121. {
  122. struct ovl_entry *oe = dentry->d_fsdata;
  123. struct inode *inode = d_inode_rcu(dentry);
  124. struct dentry *upper;
  125. unsigned int i;
  126. int ret = 1;
  127. /* Careful in RCU mode */
  128. if (!inode)
  129. return -ECHILD;
  130. upper = ovl_i_dentry_upper(inode);
  131. if (upper)
  132. ret = ovl_revalidate_real(upper, flags, weak);
  133. for (i = 0; ret > 0 && i < oe->numlower; i++) {
  134. ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
  135. weak);
  136. }
  137. return ret;
  138. }
  139. static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
  140. {
  141. return ovl_dentry_revalidate_common(dentry, flags, false);
  142. }
  143. static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
  144. {
  145. return ovl_dentry_revalidate_common(dentry, flags, true);
  146. }
  147. static const struct dentry_operations ovl_dentry_operations = {
  148. .d_release = ovl_dentry_release,
  149. .d_real = ovl_d_real,
  150. .d_revalidate = ovl_dentry_revalidate,
  151. .d_weak_revalidate = ovl_dentry_weak_revalidate,
  152. };
  153. static struct kmem_cache *ovl_inode_cachep;
  154. static struct inode *ovl_alloc_inode(struct super_block *sb)
  155. {
  156. struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL);
  157. if (!oi)
  158. return NULL;
  159. oi->cache = NULL;
  160. oi->redirect = NULL;
  161. oi->version = 0;
  162. oi->flags = 0;
  163. oi->__upperdentry = NULL;
  164. oi->lowerpath.dentry = NULL;
  165. oi->lowerpath.layer = NULL;
  166. oi->lowerdata = NULL;
  167. mutex_init(&oi->lock);
  168. return &oi->vfs_inode;
  169. }
  170. static void ovl_free_inode(struct inode *inode)
  171. {
  172. struct ovl_inode *oi = OVL_I(inode);
  173. kfree(oi->redirect);
  174. mutex_destroy(&oi->lock);
  175. kmem_cache_free(ovl_inode_cachep, oi);
  176. }
  177. static void ovl_destroy_inode(struct inode *inode)
  178. {
  179. struct ovl_inode *oi = OVL_I(inode);
  180. dput(oi->__upperdentry);
  181. dput(oi->lowerpath.dentry);
  182. if (S_ISDIR(inode->i_mode))
  183. ovl_dir_cache_free(inode);
  184. else
  185. iput(oi->lowerdata);
  186. }
  187. static void ovl_free_fs(struct ovl_fs *ofs)
  188. {
  189. struct vfsmount **mounts;
  190. unsigned i;
  191. iput(ofs->workbasedir_trap);
  192. iput(ofs->indexdir_trap);
  193. iput(ofs->workdir_trap);
  194. dput(ofs->whiteout);
  195. dput(ofs->indexdir);
  196. dput(ofs->workdir);
  197. if (ofs->workdir_locked)
  198. ovl_inuse_unlock(ofs->workbasedir);
  199. dput(ofs->workbasedir);
  200. if (ofs->upperdir_locked)
  201. ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
  202. /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */
  203. mounts = (struct vfsmount **) ofs->layers;
  204. for (i = 0; i < ofs->numlayer; i++) {
  205. iput(ofs->layers[i].trap);
  206. mounts[i] = ofs->layers[i].mnt;
  207. }
  208. kern_unmount_array(mounts, ofs->numlayer);
  209. kfree(ofs->layers);
  210. for (i = 0; i < ofs->numfs; i++)
  211. free_anon_bdev(ofs->fs[i].pseudo_dev);
  212. kfree(ofs->fs);
  213. kfree(ofs->config.lowerdir);
  214. kfree(ofs->config.upperdir);
  215. kfree(ofs->config.workdir);
  216. kfree(ofs->config.redirect_mode);
  217. if (ofs->creator_cred)
  218. put_cred(ofs->creator_cred);
  219. kfree(ofs);
  220. }
  221. static void ovl_put_super(struct super_block *sb)
  222. {
  223. struct ovl_fs *ofs = sb->s_fs_info;
  224. ovl_free_fs(ofs);
  225. }
  226. /* Sync real dirty inodes in upper filesystem (if it exists) */
  227. static int ovl_sync_fs(struct super_block *sb, int wait)
  228. {
  229. struct ovl_fs *ofs = sb->s_fs_info;
  230. struct super_block *upper_sb;
  231. int ret;
  232. ret = ovl_sync_status(ofs);
  233. /*
  234. * We have to always set the err, because the return value isn't
  235. * checked in syncfs, and instead indirectly return an error via
  236. * the sb's writeback errseq, which VFS inspects after this call.
  237. */
  238. if (ret < 0) {
  239. errseq_set(&sb->s_wb_err, -EIO);
  240. return -EIO;
  241. }
  242. if (!ret)
  243. return ret;
  244. /*
  245. * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
  246. * All the super blocks will be iterated, including upper_sb.
  247. *
  248. * If this is a syncfs(2) call, then we do need to call
  249. * sync_filesystem() on upper_sb, but enough if we do it when being
  250. * called with wait == 1.
  251. */
  252. if (!wait)
  253. return 0;
  254. upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
  255. down_read(&upper_sb->s_umount);
  256. ret = sync_filesystem(upper_sb);
  257. up_read(&upper_sb->s_umount);
  258. return ret;
  259. }
  260. /**
  261. * ovl_statfs
  262. * @dentry: The dentry to query
  263. * @buf: The struct kstatfs to fill in with stats
  264. *
  265. * Get the filesystem statistics. As writes always target the upper layer
  266. * filesystem pass the statfs to the upper filesystem (if it exists)
  267. */
  268. static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  269. {
  270. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  271. struct dentry *root_dentry = dentry->d_sb->s_root;
  272. struct path path;
  273. int err;
  274. #ifdef CONFIG_KSU_SUSFS_SUS_OVERLAYFS
  275. ovl_path_lowerdata(root_dentry, &path);
  276. if (likely(path.mnt && path.dentry)) {
  277. err = vfs_statfs(&path, buf);
  278. if (!err) {
  279. buf->f_namelen = 255; // 255 for erofs, ext2/4, f2fs
  280. buf->f_type = path.dentry->d_sb->s_magic;
  281. }
  282. return err;
  283. }
  284. #endif
  285. ovl_path_real(root_dentry, &path);
  286. err = vfs_statfs(&path, buf);
  287. if (!err) {
  288. buf->f_namelen = ofs->namelen;
  289. buf->f_type = OVERLAYFS_SUPER_MAGIC;
  290. }
  291. return err;
  292. }
  293. /* Will this overlay be forced to mount/remount ro? */
  294. static bool ovl_force_readonly(struct ovl_fs *ofs)
  295. {
  296. return (!ovl_upper_mnt(ofs) || !ofs->workdir);
  297. }
  298. static const char *ovl_redirect_mode_def(void)
  299. {
  300. return ovl_redirect_dir_def ? "on" : "off";
  301. }
  302. static const char * const ovl_xino_str[] = {
  303. "off",
  304. "auto",
  305. "on",
  306. };
  307. static inline int ovl_xino_def(void)
  308. {
  309. return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
  310. }
  311. /**
  312. * ovl_show_options
  313. * @m: the seq_file handle
  314. * @dentry: The dentry to query
  315. *
  316. * Prints the mount options for a given superblock.
  317. * Returns zero; does not fail.
  318. */
  319. static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  320. {
  321. struct super_block *sb = dentry->d_sb;
  322. struct ovl_fs *ofs = sb->s_fs_info;
  323. seq_show_option(m, "lowerdir", ofs->config.lowerdir);
  324. if (ofs->config.upperdir) {
  325. seq_show_option(m, "upperdir", ofs->config.upperdir);
  326. seq_show_option(m, "workdir", ofs->config.workdir);
  327. }
  328. if (ofs->config.default_permissions)
  329. seq_puts(m, ",default_permissions");
  330. if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
  331. seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
  332. if (ofs->config.index != ovl_index_def)
  333. seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
  334. if (!ofs->config.uuid)
  335. seq_puts(m, ",uuid=off");
  336. if (ofs->config.nfs_export != ovl_nfs_export_def)
  337. seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
  338. "on" : "off");
  339. if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
  340. seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
  341. if (ofs->config.metacopy != ovl_metacopy_def)
  342. seq_printf(m, ",metacopy=%s",
  343. ofs->config.metacopy ? "on" : "off");
  344. if (ofs->config.ovl_volatile)
  345. seq_puts(m, ",volatile");
  346. if (ofs->config.userxattr)
  347. seq_puts(m, ",userxattr");
  348. if (ofs->config.override_creds != ovl_override_creds_def)
  349. seq_show_option(m, "override_creds",
  350. ofs->config.override_creds ? "on" : "off");
  351. return 0;
  352. }
  353. static int ovl_remount(struct super_block *sb, int *flags, char *data)
  354. {
  355. struct ovl_fs *ofs = sb->s_fs_info;
  356. struct super_block *upper_sb;
  357. int ret = 0;
  358. if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
  359. return -EROFS;
  360. if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
  361. upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
  362. if (ovl_should_sync(ofs)) {
  363. down_read(&upper_sb->s_umount);
  364. ret = sync_filesystem(upper_sb);
  365. up_read(&upper_sb->s_umount);
  366. }
  367. }
  368. return ret;
  369. }
  370. static const struct super_operations ovl_super_operations = {
  371. .alloc_inode = ovl_alloc_inode,
  372. .free_inode = ovl_free_inode,
  373. .destroy_inode = ovl_destroy_inode,
  374. .drop_inode = generic_delete_inode,
  375. .put_super = ovl_put_super,
  376. .sync_fs = ovl_sync_fs,
  377. .statfs = ovl_statfs,
  378. .show_options = ovl_show_options,
  379. .remount_fs = ovl_remount,
  380. };
  381. enum {
  382. OPT_LOWERDIR,
  383. OPT_UPPERDIR,
  384. OPT_WORKDIR,
  385. OPT_DEFAULT_PERMISSIONS,
  386. OPT_REDIRECT_DIR,
  387. OPT_INDEX_ON,
  388. OPT_INDEX_OFF,
  389. OPT_UUID_ON,
  390. OPT_UUID_OFF,
  391. OPT_NFS_EXPORT_ON,
  392. OPT_USERXATTR,
  393. OPT_NFS_EXPORT_OFF,
  394. OPT_XINO_ON,
  395. OPT_XINO_OFF,
  396. OPT_XINO_AUTO,
  397. OPT_METACOPY_ON,
  398. OPT_METACOPY_OFF,
  399. OPT_VOLATILE,
  400. OPT_OVERRIDE_CREDS_ON,
  401. OPT_OVERRIDE_CREDS_OFF,
  402. OPT_ERR,
  403. };
  404. static const match_table_t ovl_tokens = {
  405. {OPT_LOWERDIR, "lowerdir=%s"},
  406. {OPT_UPPERDIR, "upperdir=%s"},
  407. {OPT_WORKDIR, "workdir=%s"},
  408. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  409. {OPT_REDIRECT_DIR, "redirect_dir=%s"},
  410. {OPT_INDEX_ON, "index=on"},
  411. {OPT_INDEX_OFF, "index=off"},
  412. {OPT_USERXATTR, "userxattr"},
  413. {OPT_UUID_ON, "uuid=on"},
  414. {OPT_UUID_OFF, "uuid=off"},
  415. {OPT_NFS_EXPORT_ON, "nfs_export=on"},
  416. {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
  417. {OPT_XINO_ON, "xino=on"},
  418. {OPT_XINO_OFF, "xino=off"},
  419. {OPT_XINO_AUTO, "xino=auto"},
  420. {OPT_METACOPY_ON, "metacopy=on"},
  421. {OPT_METACOPY_OFF, "metacopy=off"},
  422. {OPT_VOLATILE, "volatile"},
  423. {OPT_OVERRIDE_CREDS_ON, "override_creds=on"},
  424. {OPT_OVERRIDE_CREDS_OFF, "override_creds=off"},
  425. {OPT_ERR, NULL}
  426. };
  427. static char *ovl_next_opt(char **s)
  428. {
  429. char *sbegin = *s;
  430. char *p;
  431. if (sbegin == NULL)
  432. return NULL;
  433. for (p = sbegin; *p; p++) {
  434. if (*p == '\\') {
  435. p++;
  436. if (!*p)
  437. break;
  438. } else if (*p == ',') {
  439. *p = '\0';
  440. *s = p + 1;
  441. return sbegin;
  442. }
  443. }
  444. *s = NULL;
  445. return sbegin;
  446. }
  447. static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
  448. {
  449. if (strcmp(mode, "on") == 0) {
  450. config->redirect_dir = true;
  451. /*
  452. * Does not make sense to have redirect creation without
  453. * redirect following.
  454. */
  455. config->redirect_follow = true;
  456. } else if (strcmp(mode, "follow") == 0) {
  457. config->redirect_follow = true;
  458. } else if (strcmp(mode, "off") == 0) {
  459. if (ovl_redirect_always_follow)
  460. config->redirect_follow = true;
  461. } else if (strcmp(mode, "nofollow") != 0) {
  462. pr_err("bad mount option \"redirect_dir=%s\"\n",
  463. mode);
  464. return -EINVAL;
  465. }
  466. return 0;
  467. }
  468. static int ovl_parse_opt(char *opt, struct ovl_config *config)
  469. {
  470. char *p;
  471. int err;
  472. bool metacopy_opt = false, redirect_opt = false;
  473. bool nfs_export_opt = false, index_opt = false;
  474. config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
  475. if (!config->redirect_mode)
  476. return -ENOMEM;
  477. config->override_creds = ovl_override_creds_def;
  478. while ((p = ovl_next_opt(&opt)) != NULL) {
  479. int token;
  480. substring_t args[MAX_OPT_ARGS];
  481. if (!*p)
  482. continue;
  483. token = match_token(p, ovl_tokens, args);
  484. switch (token) {
  485. case OPT_UPPERDIR:
  486. kfree(config->upperdir);
  487. config->upperdir = match_strdup(&args[0]);
  488. if (!config->upperdir)
  489. return -ENOMEM;
  490. break;
  491. case OPT_LOWERDIR:
  492. kfree(config->lowerdir);
  493. config->lowerdir = match_strdup(&args[0]);
  494. if (!config->lowerdir)
  495. return -ENOMEM;
  496. break;
  497. case OPT_WORKDIR:
  498. kfree(config->workdir);
  499. config->workdir = match_strdup(&args[0]);
  500. if (!config->workdir)
  501. return -ENOMEM;
  502. break;
  503. case OPT_DEFAULT_PERMISSIONS:
  504. config->default_permissions = true;
  505. break;
  506. case OPT_REDIRECT_DIR:
  507. kfree(config->redirect_mode);
  508. config->redirect_mode = match_strdup(&args[0]);
  509. if (!config->redirect_mode)
  510. return -ENOMEM;
  511. redirect_opt = true;
  512. break;
  513. case OPT_INDEX_ON:
  514. config->index = true;
  515. index_opt = true;
  516. break;
  517. case OPT_INDEX_OFF:
  518. config->index = false;
  519. index_opt = true;
  520. break;
  521. case OPT_UUID_ON:
  522. config->uuid = true;
  523. break;
  524. case OPT_UUID_OFF:
  525. config->uuid = false;
  526. break;
  527. case OPT_NFS_EXPORT_ON:
  528. config->nfs_export = true;
  529. nfs_export_opt = true;
  530. break;
  531. case OPT_NFS_EXPORT_OFF:
  532. config->nfs_export = false;
  533. nfs_export_opt = true;
  534. break;
  535. case OPT_XINO_ON:
  536. config->xino = OVL_XINO_ON;
  537. break;
  538. case OPT_XINO_OFF:
  539. config->xino = OVL_XINO_OFF;
  540. break;
  541. case OPT_XINO_AUTO:
  542. config->xino = OVL_XINO_AUTO;
  543. break;
  544. case OPT_METACOPY_ON:
  545. config->metacopy = true;
  546. metacopy_opt = true;
  547. break;
  548. case OPT_METACOPY_OFF:
  549. config->metacopy = false;
  550. metacopy_opt = true;
  551. break;
  552. case OPT_VOLATILE:
  553. config->ovl_volatile = true;
  554. break;
  555. case OPT_USERXATTR:
  556. config->userxattr = true;
  557. break;
  558. case OPT_OVERRIDE_CREDS_ON:
  559. config->override_creds = true;
  560. break;
  561. case OPT_OVERRIDE_CREDS_OFF:
  562. config->override_creds = false;
  563. break;
  564. default:
  565. pr_err("unrecognized mount option \"%s\" or missing value\n",
  566. p);
  567. return -EINVAL;
  568. }
  569. }
  570. /* Workdir/index are useless in non-upper mount */
  571. if (!config->upperdir) {
  572. if (config->workdir) {
  573. pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
  574. config->workdir);
  575. kfree(config->workdir);
  576. config->workdir = NULL;
  577. }
  578. if (config->index && index_opt) {
  579. pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
  580. index_opt = false;
  581. }
  582. config->index = false;
  583. }
  584. if (!config->upperdir && config->ovl_volatile) {
  585. pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
  586. config->ovl_volatile = false;
  587. }
  588. err = ovl_parse_redirect_mode(config, config->redirect_mode);
  589. if (err)
  590. return err;
  591. /*
  592. * This is to make the logic below simpler. It doesn't make any other
  593. * difference, since config->redirect_dir is only used for upper.
  594. */
  595. if (!config->upperdir && config->redirect_follow)
  596. config->redirect_dir = true;
  597. /* Resolve metacopy -> redirect_dir dependency */
  598. if (config->metacopy && !config->redirect_dir) {
  599. if (metacopy_opt && redirect_opt) {
  600. pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
  601. config->redirect_mode);
  602. return -EINVAL;
  603. }
  604. if (redirect_opt) {
  605. /*
  606. * There was an explicit redirect_dir=... that resulted
  607. * in this conflict.
  608. */
  609. pr_info("disabling metacopy due to redirect_dir=%s\n",
  610. config->redirect_mode);
  611. config->metacopy = false;
  612. } else {
  613. /* Automatically enable redirect otherwise. */
  614. config->redirect_follow = config->redirect_dir = true;
  615. }
  616. }
  617. /* Resolve nfs_export -> index dependency */
  618. if (config->nfs_export && !config->index) {
  619. if (!config->upperdir && config->redirect_follow) {
  620. pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
  621. config->nfs_export = false;
  622. } else if (nfs_export_opt && index_opt) {
  623. pr_err("conflicting options: nfs_export=on,index=off\n");
  624. return -EINVAL;
  625. } else if (index_opt) {
  626. /*
  627. * There was an explicit index=off that resulted
  628. * in this conflict.
  629. */
  630. pr_info("disabling nfs_export due to index=off\n");
  631. config->nfs_export = false;
  632. } else {
  633. /* Automatically enable index otherwise. */
  634. config->index = true;
  635. }
  636. }
  637. /* Resolve nfs_export -> !metacopy dependency */
  638. if (config->nfs_export && config->metacopy) {
  639. if (nfs_export_opt && metacopy_opt) {
  640. pr_err("conflicting options: nfs_export=on,metacopy=on\n");
  641. return -EINVAL;
  642. }
  643. if (metacopy_opt) {
  644. /*
  645. * There was an explicit metacopy=on that resulted
  646. * in this conflict.
  647. */
  648. pr_info("disabling nfs_export due to metacopy=on\n");
  649. config->nfs_export = false;
  650. } else {
  651. /*
  652. * There was an explicit nfs_export=on that resulted
  653. * in this conflict.
  654. */
  655. pr_info("disabling metacopy due to nfs_export=on\n");
  656. config->metacopy = false;
  657. }
  658. }
  659. /* Resolve userxattr -> !redirect && !metacopy dependency */
  660. if (config->userxattr) {
  661. if (config->redirect_follow && redirect_opt) {
  662. pr_err("conflicting options: userxattr,redirect_dir=%s\n",
  663. config->redirect_mode);
  664. return -EINVAL;
  665. }
  666. if (config->metacopy && metacopy_opt) {
  667. pr_err("conflicting options: userxattr,metacopy=on\n");
  668. return -EINVAL;
  669. }
  670. /*
  671. * Silently disable default setting of redirect and metacopy.
  672. * This shall be the default in the future as well: these
  673. * options must be explicitly enabled if used together with
  674. * userxattr.
  675. */
  676. config->redirect_dir = config->redirect_follow = false;
  677. config->metacopy = false;
  678. }
  679. return 0;
  680. }
  681. #define OVL_WORKDIR_NAME "work"
  682. #define OVL_INDEXDIR_NAME "index"
  683. static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
  684. const char *name, bool persist)
  685. {
  686. struct inode *dir = ofs->workbasedir->d_inode;
  687. struct vfsmount *mnt = ovl_upper_mnt(ofs);
  688. struct dentry *work;
  689. int err;
  690. bool retried = false;
  691. inode_lock_nested(dir, I_MUTEX_PARENT);
  692. retry:
  693. work = ovl_lookup_upper(ofs, name, ofs->workbasedir, strlen(name));
  694. if (!IS_ERR(work)) {
  695. struct iattr attr = {
  696. .ia_valid = ATTR_MODE,
  697. .ia_mode = S_IFDIR | 0,
  698. };
  699. if (work->d_inode) {
  700. err = -EEXIST;
  701. if (retried)
  702. goto out_dput;
  703. if (persist)
  704. goto out_unlock;
  705. retried = true;
  706. err = ovl_workdir_cleanup(ofs, dir, mnt, work, 0);
  707. dput(work);
  708. if (err == -EINVAL) {
  709. work = ERR_PTR(err);
  710. goto out_unlock;
  711. }
  712. goto retry;
  713. }
  714. err = ovl_mkdir_real(ofs, dir, &work, attr.ia_mode);
  715. if (err)
  716. goto out_dput;
  717. /* Weird filesystem returning with hashed negative (kernfs)? */
  718. err = -EINVAL;
  719. if (d_really_is_negative(work))
  720. goto out_dput;
  721. /*
  722. * Try to remove POSIX ACL xattrs from workdir. We are good if:
  723. *
  724. * a) success (there was a POSIX ACL xattr and was removed)
  725. * b) -ENODATA (there was no POSIX ACL xattr)
  726. * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
  727. *
  728. * There are various other error values that could effectively
  729. * mean that the xattr doesn't exist (e.g. -ERANGE is returned
  730. * if the xattr name is too long), but the set of filesystems
  731. * allowed as upper are limited to "normal" ones, where checking
  732. * for the above two errors is sufficient.
  733. */
  734. err = ovl_do_removexattr(ofs, work,
  735. XATTR_NAME_POSIX_ACL_DEFAULT);
  736. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  737. goto out_dput;
  738. err = ovl_do_removexattr(ofs, work,
  739. XATTR_NAME_POSIX_ACL_ACCESS);
  740. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  741. goto out_dput;
  742. /* Clear any inherited mode bits */
  743. inode_lock(work->d_inode);
  744. err = ovl_do_notify_change(ofs, work, &attr);
  745. inode_unlock(work->d_inode);
  746. if (err)
  747. goto out_dput;
  748. } else {
  749. err = PTR_ERR(work);
  750. goto out_err;
  751. }
  752. out_unlock:
  753. inode_unlock(dir);
  754. return work;
  755. out_dput:
  756. dput(work);
  757. out_err:
  758. pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
  759. ofs->config.workdir, name, -err);
  760. work = NULL;
  761. goto out_unlock;
  762. }
  763. static void ovl_unescape(char *s)
  764. {
  765. char *d = s;
  766. for (;; s++, d++) {
  767. if (*s == '\\')
  768. s++;
  769. *d = *s;
  770. if (!*s)
  771. break;
  772. }
  773. }
  774. static int ovl_mount_dir_noesc(const char *name, struct path *path)
  775. {
  776. int err = -EINVAL;
  777. if (!*name) {
  778. pr_err("empty lowerdir\n");
  779. goto out;
  780. }
  781. err = kern_path(name, LOOKUP_FOLLOW, path);
  782. if (err) {
  783. pr_err("failed to resolve '%s': %i\n", name, err);
  784. goto out;
  785. }
  786. err = -EINVAL;
  787. if (ovl_dentry_weird(path->dentry)) {
  788. pr_err("filesystem on '%s' not supported\n", name);
  789. goto out_put;
  790. }
  791. if (!d_is_dir(path->dentry)) {
  792. pr_err("'%s' not a directory\n", name);
  793. goto out_put;
  794. }
  795. return 0;
  796. out_put:
  797. path_put_init(path);
  798. out:
  799. return err;
  800. }
  801. static int ovl_mount_dir(const char *name, struct path *path)
  802. {
  803. int err = -ENOMEM;
  804. char *tmp = kstrdup(name, GFP_KERNEL);
  805. if (tmp) {
  806. ovl_unescape(tmp);
  807. err = ovl_mount_dir_noesc(tmp, path);
  808. if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
  809. pr_err("filesystem on '%s' not supported as upperdir\n",
  810. tmp);
  811. path_put_init(path);
  812. err = -EINVAL;
  813. }
  814. kfree(tmp);
  815. }
  816. return err;
  817. }
  818. static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs,
  819. const char *name)
  820. {
  821. struct kstatfs statfs;
  822. int err = vfs_statfs(path, &statfs);
  823. if (err)
  824. pr_err("statfs failed on '%s'\n", name);
  825. else
  826. ofs->namelen = max(ofs->namelen, statfs.f_namelen);
  827. return err;
  828. }
  829. static int ovl_lower_dir(const char *name, struct path *path,
  830. struct ovl_fs *ofs, int *stack_depth)
  831. {
  832. int fh_type;
  833. int err;
  834. err = ovl_mount_dir_noesc(name, path);
  835. if (err)
  836. return err;
  837. err = ovl_check_namelen(path, ofs, name);
  838. if (err)
  839. return err;
  840. *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
  841. /*
  842. * The inodes index feature and NFS export need to encode and decode
  843. * file handles, so they require that all layers support them.
  844. */
  845. fh_type = ovl_can_decode_fh(path->dentry->d_sb);
  846. if ((ofs->config.nfs_export ||
  847. (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
  848. ofs->config.index = false;
  849. ofs->config.nfs_export = false;
  850. pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
  851. name);
  852. }
  853. /*
  854. * Decoding origin file handle is required for persistent st_ino.
  855. * Without persistent st_ino, xino=auto falls back to xino=off.
  856. */
  857. if (ofs->config.xino == OVL_XINO_AUTO &&
  858. ofs->config.upperdir && !fh_type) {
  859. ofs->config.xino = OVL_XINO_OFF;
  860. pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n",
  861. name);
  862. }
  863. /* Check if lower fs has 32bit inode numbers */
  864. if (fh_type != FILEID_INO32_GEN)
  865. ofs->xino_mode = -1;
  866. return 0;
  867. }
  868. /* Workdir should not be subdir of upperdir and vice versa */
  869. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  870. {
  871. bool ok = false;
  872. if (workdir != upperdir) {
  873. ok = (lock_rename(workdir, upperdir) == NULL);
  874. unlock_rename(workdir, upperdir);
  875. }
  876. return ok;
  877. }
  878. static unsigned int ovl_split_lowerdirs(char *str)
  879. {
  880. unsigned int ctr = 1;
  881. char *s, *d;
  882. for (s = d = str;; s++, d++) {
  883. if (*s == '\\') {
  884. s++;
  885. } else if (*s == ':') {
  886. *d = '\0';
  887. ctr++;
  888. continue;
  889. }
  890. *d = *s;
  891. if (!*s)
  892. break;
  893. }
  894. return ctr;
  895. }
  896. static int __maybe_unused
  897. ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
  898. struct dentry *dentry, struct inode *inode,
  899. const char *name, void *buffer, size_t size)
  900. {
  901. return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
  902. }
  903. static int __maybe_unused
  904. ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
  905. struct user_namespace *mnt_userns,
  906. struct dentry *dentry, struct inode *inode,
  907. const char *name, const void *value,
  908. size_t size, int flags)
  909. {
  910. struct dentry *workdir = ovl_workdir(dentry);
  911. struct inode *realinode = ovl_inode_real(inode);
  912. struct posix_acl *acl = NULL;
  913. int err;
  914. /* Check that everything is OK before copy-up */
  915. if (value) {
  916. /* The above comment can be understood in two ways:
  917. *
  918. * 1. We just want to check whether the basic POSIX ACL format
  919. * is ok. For example, if the header is correct and the size
  920. * is sane.
  921. * 2. We want to know whether the ACL_{GROUP,USER} entries can
  922. * be mapped according to the underlying filesystem.
  923. *
  924. * Currently, we only check 1. If we wanted to check 2. we
  925. * would need to pass the mnt_userns and the fs_userns of the
  926. * underlying filesystem. But frankly, I think checking 1. is
  927. * enough to start the copy-up.
  928. */
  929. acl = vfs_set_acl_prepare(&init_user_ns, &init_user_ns, value, size);
  930. if (IS_ERR(acl))
  931. return PTR_ERR(acl);
  932. }
  933. err = -EOPNOTSUPP;
  934. if (!IS_POSIXACL(d_inode(workdir)))
  935. goto out_acl_release;
  936. if (!realinode->i_op->set_acl)
  937. goto out_acl_release;
  938. if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
  939. err = acl ? -EACCES : 0;
  940. goto out_acl_release;
  941. }
  942. err = -EPERM;
  943. if (!inode_owner_or_capable(&init_user_ns, inode))
  944. goto out_acl_release;
  945. posix_acl_release(acl);
  946. /*
  947. * Check if sgid bit needs to be cleared (actual setacl operation will
  948. * be done with mounter's capabilities and so that won't do it for us).
  949. */
  950. if (unlikely(inode->i_mode & S_ISGID) &&
  951. handler->flags == ACL_TYPE_ACCESS &&
  952. !in_group_p(inode->i_gid) &&
  953. !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID)) {
  954. struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
  955. err = ovl_setattr(&init_user_ns, dentry, &iattr);
  956. if (err)
  957. return err;
  958. }
  959. err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
  960. return err;
  961. out_acl_release:
  962. posix_acl_release(acl);
  963. return err;
  964. }
  965. static int ovl_own_xattr_get(const struct xattr_handler *handler,
  966. struct dentry *dentry, struct inode *inode,
  967. const char *name, void *buffer, size_t size)
  968. {
  969. return -EOPNOTSUPP;
  970. }
  971. static int ovl_own_xattr_set(const struct xattr_handler *handler,
  972. struct user_namespace *mnt_userns,
  973. struct dentry *dentry, struct inode *inode,
  974. const char *name, const void *value,
  975. size_t size, int flags)
  976. {
  977. return -EOPNOTSUPP;
  978. }
  979. static int ovl_other_xattr_get(const struct xattr_handler *handler,
  980. struct dentry *dentry, struct inode *inode,
  981. const char *name, void *buffer, size_t size)
  982. {
  983. return ovl_xattr_get(dentry, inode, name, buffer, size);
  984. }
  985. static int ovl_other_xattr_set(const struct xattr_handler *handler,
  986. struct user_namespace *mnt_userns,
  987. struct dentry *dentry, struct inode *inode,
  988. const char *name, const void *value,
  989. size_t size, int flags)
  990. {
  991. return ovl_xattr_set(dentry, inode, name, value, size, flags);
  992. }
  993. static const struct xattr_handler __maybe_unused
  994. ovl_posix_acl_access_xattr_handler = {
  995. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  996. .flags = ACL_TYPE_ACCESS,
  997. .get = ovl_posix_acl_xattr_get,
  998. .set = ovl_posix_acl_xattr_set,
  999. };
  1000. static const struct xattr_handler __maybe_unused
  1001. ovl_posix_acl_default_xattr_handler = {
  1002. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  1003. .flags = ACL_TYPE_DEFAULT,
  1004. .get = ovl_posix_acl_xattr_get,
  1005. .set = ovl_posix_acl_xattr_set,
  1006. };
  1007. static const struct xattr_handler ovl_own_trusted_xattr_handler = {
  1008. .prefix = OVL_XATTR_TRUSTED_PREFIX,
  1009. .get = ovl_own_xattr_get,
  1010. .set = ovl_own_xattr_set,
  1011. };
  1012. static const struct xattr_handler ovl_own_user_xattr_handler = {
  1013. .prefix = OVL_XATTR_USER_PREFIX,
  1014. .get = ovl_own_xattr_get,
  1015. .set = ovl_own_xattr_set,
  1016. };
  1017. static const struct xattr_handler ovl_other_xattr_handler = {
  1018. .prefix = "", /* catch all */
  1019. .get = ovl_other_xattr_get,
  1020. .set = ovl_other_xattr_set,
  1021. };
  1022. static const struct xattr_handler *ovl_trusted_xattr_handlers[] = {
  1023. #ifdef CONFIG_FS_POSIX_ACL
  1024. &ovl_posix_acl_access_xattr_handler,
  1025. &ovl_posix_acl_default_xattr_handler,
  1026. #endif
  1027. &ovl_own_trusted_xattr_handler,
  1028. &ovl_other_xattr_handler,
  1029. NULL
  1030. };
  1031. static const struct xattr_handler *ovl_user_xattr_handlers[] = {
  1032. #ifdef CONFIG_FS_POSIX_ACL
  1033. &ovl_posix_acl_access_xattr_handler,
  1034. &ovl_posix_acl_default_xattr_handler,
  1035. #endif
  1036. &ovl_own_user_xattr_handler,
  1037. &ovl_other_xattr_handler,
  1038. NULL
  1039. };
  1040. static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
  1041. struct inode **ptrap, const char *name)
  1042. {
  1043. struct inode *trap;
  1044. int err;
  1045. trap = ovl_get_trap_inode(sb, dir);
  1046. err = PTR_ERR_OR_ZERO(trap);
  1047. if (err) {
  1048. if (err == -ELOOP)
  1049. pr_err("conflicting %s path\n", name);
  1050. return err;
  1051. }
  1052. *ptrap = trap;
  1053. return 0;
  1054. }
  1055. /*
  1056. * Determine how we treat concurrent use of upperdir/workdir based on the
  1057. * index feature. This is papering over mount leaks of container runtimes,
  1058. * for example, an old overlay mount is leaked and now its upperdir is
  1059. * attempted to be used as a lower layer in a new overlay mount.
  1060. */
  1061. static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
  1062. {
  1063. if (ofs->config.index) {
  1064. pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
  1065. name);
  1066. return -EBUSY;
  1067. } else {
  1068. pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
  1069. name);
  1070. return 0;
  1071. }
  1072. }
  1073. static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
  1074. struct ovl_layer *upper_layer, struct path *upperpath)
  1075. {
  1076. struct vfsmount *upper_mnt;
  1077. int err;
  1078. err = ovl_mount_dir(ofs->config.upperdir, upperpath);
  1079. if (err)
  1080. goto out;
  1081. /* Upperdir path should not be r/o */
  1082. if (__mnt_is_readonly(upperpath->mnt)) {
  1083. pr_err("upper fs is r/o, try multi-lower layers mount\n");
  1084. err = -EINVAL;
  1085. goto out;
  1086. }
  1087. err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
  1088. if (err)
  1089. goto out;
  1090. err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
  1091. "upperdir");
  1092. if (err)
  1093. goto out;
  1094. upper_mnt = clone_private_mount(upperpath);
  1095. err = PTR_ERR(upper_mnt);
  1096. if (IS_ERR(upper_mnt)) {
  1097. pr_err("failed to clone upperpath\n");
  1098. goto out;
  1099. }
  1100. /* Don't inherit atime flags */
  1101. #ifdef CONFIG_KDP_NS
  1102. kdp_clear_mnt_flags(upper_mnt, (MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME));
  1103. #else
  1104. upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
  1105. #endif
  1106. upper_layer->mnt = upper_mnt;
  1107. upper_layer->idx = 0;
  1108. upper_layer->fsid = 0;
  1109. /*
  1110. * Inherit SB_NOSEC flag from upperdir.
  1111. *
  1112. * This optimization changes behavior when a security related attribute
  1113. * (suid/sgid/security.*) is changed on an underlying layer. This is
  1114. * okay because we don't yet have guarantees in that case, but it will
  1115. * need careful treatment once we want to honour changes to underlying
  1116. * filesystems.
  1117. */
  1118. if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
  1119. sb->s_flags |= SB_NOSEC;
  1120. if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
  1121. ofs->upperdir_locked = true;
  1122. } else {
  1123. err = ovl_report_in_use(ofs, "upperdir");
  1124. if (err)
  1125. goto out;
  1126. }
  1127. err = 0;
  1128. out:
  1129. return err;
  1130. }
  1131. /*
  1132. * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
  1133. * negative values if error is encountered.
  1134. */
  1135. static int ovl_check_rename_whiteout(struct ovl_fs *ofs)
  1136. {
  1137. struct dentry *workdir = ofs->workdir;
  1138. struct inode *dir = d_inode(workdir);
  1139. struct dentry *temp;
  1140. struct dentry *dest;
  1141. struct dentry *whiteout;
  1142. struct name_snapshot name;
  1143. int err;
  1144. inode_lock_nested(dir, I_MUTEX_PARENT);
  1145. temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0));
  1146. err = PTR_ERR(temp);
  1147. if (IS_ERR(temp))
  1148. goto out_unlock;
  1149. dest = ovl_lookup_temp(ofs, workdir);
  1150. err = PTR_ERR(dest);
  1151. if (IS_ERR(dest)) {
  1152. dput(temp);
  1153. goto out_unlock;
  1154. }
  1155. /* Name is inline and stable - using snapshot as a copy helper */
  1156. take_dentry_name_snapshot(&name, temp);
  1157. err = ovl_do_rename(ofs, dir, temp, dir, dest, RENAME_WHITEOUT);
  1158. if (err) {
  1159. if (err == -EINVAL)
  1160. err = 0;
  1161. goto cleanup_temp;
  1162. }
  1163. whiteout = ovl_lookup_upper(ofs, name.name.name, workdir, name.name.len);
  1164. err = PTR_ERR(whiteout);
  1165. if (IS_ERR(whiteout))
  1166. goto cleanup_temp;
  1167. err = ovl_is_whiteout(whiteout);
  1168. /* Best effort cleanup of whiteout and temp file */
  1169. if (err)
  1170. ovl_cleanup(ofs, dir, whiteout);
  1171. dput(whiteout);
  1172. cleanup_temp:
  1173. ovl_cleanup(ofs, dir, temp);
  1174. release_dentry_name_snapshot(&name);
  1175. dput(temp);
  1176. dput(dest);
  1177. out_unlock:
  1178. inode_unlock(dir);
  1179. return err;
  1180. }
  1181. static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
  1182. struct dentry *parent,
  1183. const char *name, umode_t mode)
  1184. {
  1185. size_t len = strlen(name);
  1186. struct dentry *child;
  1187. inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
  1188. child = ovl_lookup_upper(ofs, name, parent, len);
  1189. if (!IS_ERR(child) && !child->d_inode)
  1190. child = ovl_create_real(ofs, parent->d_inode, child,
  1191. OVL_CATTR(mode));
  1192. inode_unlock(parent->d_inode);
  1193. dput(parent);
  1194. return child;
  1195. }
  1196. /*
  1197. * Creates $workdir/work/incompat/volatile/dirty file if it is not already
  1198. * present.
  1199. */
  1200. static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
  1201. {
  1202. unsigned int ctr;
  1203. struct dentry *d = dget(ofs->workbasedir);
  1204. static const char *const volatile_path[] = {
  1205. OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
  1206. };
  1207. const char *const *name = volatile_path;
  1208. for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
  1209. d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
  1210. if (IS_ERR(d))
  1211. return PTR_ERR(d);
  1212. }
  1213. dput(d);
  1214. return 0;
  1215. }
  1216. static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
  1217. const struct path *workpath)
  1218. {
  1219. struct vfsmount *mnt = ovl_upper_mnt(ofs);
  1220. struct dentry *workdir;
  1221. struct file *tmpfile;
  1222. bool rename_whiteout;
  1223. bool d_type;
  1224. int fh_type;
  1225. int err;
  1226. err = mnt_want_write(mnt);
  1227. if (err)
  1228. return err;
  1229. workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
  1230. err = PTR_ERR(workdir);
  1231. if (IS_ERR_OR_NULL(workdir))
  1232. goto out;
  1233. ofs->workdir = workdir;
  1234. err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
  1235. if (err)
  1236. goto out;
  1237. /*
  1238. * Upper should support d_type, else whiteouts are visible. Given
  1239. * workdir and upper are on same fs, we can do iterate_dir() on
  1240. * workdir. This check requires successful creation of workdir in
  1241. * previous step.
  1242. */
  1243. err = ovl_check_d_type_supported(workpath);
  1244. if (err < 0)
  1245. goto out;
  1246. d_type = err;
  1247. if (!d_type)
  1248. pr_warn("upper fs needs to support d_type.\n");
  1249. /* Check if upper/work fs supports O_TMPFILE */
  1250. tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0);
  1251. ofs->tmpfile = !IS_ERR(tmpfile);
  1252. if (ofs->tmpfile)
  1253. fput(tmpfile);
  1254. else
  1255. pr_warn("upper fs does not support tmpfile.\n");
  1256. /* Check if upper/work fs supports RENAME_WHITEOUT */
  1257. err = ovl_check_rename_whiteout(ofs);
  1258. if (err < 0)
  1259. goto out;
  1260. rename_whiteout = err;
  1261. if (!rename_whiteout)
  1262. pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
  1263. /*
  1264. * Check if upper/work fs supports (trusted|user).overlay.* xattr
  1265. */
  1266. err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
  1267. if (err) {
  1268. pr_warn("failed to set xattr on upper\n");
  1269. ofs->noxattr = true;
  1270. if (ofs->config.index || ofs->config.metacopy) {
  1271. ofs->config.index = false;
  1272. ofs->config.metacopy = false;
  1273. pr_warn("...falling back to index=off,metacopy=off.\n");
  1274. }
  1275. /*
  1276. * xattr support is required for persistent st_ino.
  1277. * Without persistent st_ino, xino=auto falls back to xino=off.
  1278. */
  1279. if (ofs->config.xino == OVL_XINO_AUTO) {
  1280. ofs->config.xino = OVL_XINO_OFF;
  1281. pr_warn("...falling back to xino=off.\n");
  1282. }
  1283. if (err == -EPERM && !ofs->config.userxattr)
  1284. pr_info("try mounting with 'userxattr' option\n");
  1285. err = 0;
  1286. } else {
  1287. ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
  1288. }
  1289. /*
  1290. * We allowed sub-optimal upper fs configuration and don't want to break
  1291. * users over kernel upgrade, but we never allowed remote upper fs, so
  1292. * we can enforce strict requirements for remote upper fs.
  1293. */
  1294. if (ovl_dentry_remote(ofs->workdir) &&
  1295. (!d_type || !rename_whiteout || ofs->noxattr)) {
  1296. pr_err("upper fs missing required features.\n");
  1297. err = -EINVAL;
  1298. goto out;
  1299. }
  1300. /*
  1301. * For volatile mount, create a incompat/volatile/dirty file to keep
  1302. * track of it.
  1303. */
  1304. if (ofs->config.ovl_volatile) {
  1305. err = ovl_create_volatile_dirty(ofs);
  1306. if (err < 0) {
  1307. pr_err("Failed to create volatile/dirty file.\n");
  1308. goto out;
  1309. }
  1310. }
  1311. /* Check if upper/work fs supports file handles */
  1312. fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
  1313. if (ofs->config.index && !fh_type) {
  1314. ofs->config.index = false;
  1315. pr_warn("upper fs does not support file handles, falling back to index=off.\n");
  1316. }
  1317. /* Check if upper fs has 32bit inode numbers */
  1318. if (fh_type != FILEID_INO32_GEN)
  1319. ofs->xino_mode = -1;
  1320. /* NFS export of r/w mount depends on index */
  1321. if (ofs->config.nfs_export && !ofs->config.index) {
  1322. pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
  1323. ofs->config.nfs_export = false;
  1324. }
  1325. out:
  1326. mnt_drop_write(mnt);
  1327. return err;
  1328. }
  1329. static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
  1330. const struct path *upperpath)
  1331. {
  1332. int err;
  1333. struct path workpath = { };
  1334. err = ovl_mount_dir(ofs->config.workdir, &workpath);
  1335. if (err)
  1336. goto out;
  1337. err = -EINVAL;
  1338. if (upperpath->mnt != workpath.mnt) {
  1339. pr_err("workdir and upperdir must reside under the same mount\n");
  1340. goto out;
  1341. }
  1342. if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
  1343. pr_err("workdir and upperdir must be separate subtrees\n");
  1344. goto out;
  1345. }
  1346. ofs->workbasedir = dget(workpath.dentry);
  1347. if (ovl_inuse_trylock(ofs->workbasedir)) {
  1348. ofs->workdir_locked = true;
  1349. } else {
  1350. err = ovl_report_in_use(ofs, "workdir");
  1351. if (err)
  1352. goto out;
  1353. }
  1354. err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
  1355. "workdir");
  1356. if (err)
  1357. goto out;
  1358. err = ovl_make_workdir(sb, ofs, &workpath);
  1359. out:
  1360. path_put(&workpath);
  1361. return err;
  1362. }
  1363. static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
  1364. struct ovl_entry *oe, const struct path *upperpath)
  1365. {
  1366. struct vfsmount *mnt = ovl_upper_mnt(ofs);
  1367. struct dentry *indexdir;
  1368. int err;
  1369. err = mnt_want_write(mnt);
  1370. if (err)
  1371. return err;
  1372. /* Verify lower root is upper root origin */
  1373. err = ovl_verify_origin(ofs, upperpath->dentry,
  1374. oe->lowerstack[0].dentry, true);
  1375. if (err) {
  1376. pr_err("failed to verify upper root origin\n");
  1377. goto out;
  1378. }
  1379. /* index dir will act also as workdir */
  1380. iput(ofs->workdir_trap);
  1381. ofs->workdir_trap = NULL;
  1382. dput(ofs->workdir);
  1383. ofs->workdir = NULL;
  1384. indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
  1385. if (IS_ERR(indexdir)) {
  1386. err = PTR_ERR(indexdir);
  1387. } else if (indexdir) {
  1388. ofs->indexdir = indexdir;
  1389. ofs->workdir = dget(indexdir);
  1390. err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
  1391. "indexdir");
  1392. if (err)
  1393. goto out;
  1394. /*
  1395. * Verify upper root is exclusively associated with index dir.
  1396. * Older kernels stored upper fh in ".overlay.origin"
  1397. * xattr. If that xattr exists, verify that it is a match to
  1398. * upper dir file handle. In any case, verify or set xattr
  1399. * ".overlay.upper" to indicate that index may have
  1400. * directory entries.
  1401. */
  1402. if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
  1403. err = ovl_verify_set_fh(ofs, ofs->indexdir,
  1404. OVL_XATTR_ORIGIN,
  1405. upperpath->dentry, true, false);
  1406. if (err)
  1407. pr_err("failed to verify index dir 'origin' xattr\n");
  1408. }
  1409. err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
  1410. true);
  1411. if (err)
  1412. pr_err("failed to verify index dir 'upper' xattr\n");
  1413. /* Cleanup bad/stale/orphan index entries */
  1414. if (!err)
  1415. err = ovl_indexdir_cleanup(ofs);
  1416. }
  1417. if (err || !ofs->indexdir)
  1418. pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
  1419. out:
  1420. mnt_drop_write(mnt);
  1421. return err;
  1422. }
  1423. static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
  1424. {
  1425. unsigned int i;
  1426. if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
  1427. return true;
  1428. /*
  1429. * We allow using single lower with null uuid for index and nfs_export
  1430. * for example to support those features with single lower squashfs.
  1431. * To avoid regressions in setups of overlay with re-formatted lower
  1432. * squashfs, do not allow decoding origin with lower null uuid unless
  1433. * user opted-in to one of the new features that require following the
  1434. * lower inode of non-dir upper.
  1435. */
  1436. if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid))
  1437. return false;
  1438. for (i = 0; i < ofs->numfs; i++) {
  1439. /*
  1440. * We use uuid to associate an overlay lower file handle with a
  1441. * lower layer, so we can accept lower fs with null uuid as long
  1442. * as all lower layers with null uuid are on the same fs.
  1443. * if we detect multiple lower fs with the same uuid, we
  1444. * disable lower file handle decoding on all of them.
  1445. */
  1446. if (ofs->fs[i].is_lower &&
  1447. uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
  1448. ofs->fs[i].bad_uuid = true;
  1449. return false;
  1450. }
  1451. }
  1452. return true;
  1453. }
  1454. /* Get a unique fsid for the layer */
  1455. static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
  1456. {
  1457. struct super_block *sb = path->mnt->mnt_sb;
  1458. unsigned int i;
  1459. dev_t dev;
  1460. int err;
  1461. bool bad_uuid = false;
  1462. bool warn = false;
  1463. for (i = 0; i < ofs->numfs; i++) {
  1464. if (ofs->fs[i].sb == sb)
  1465. return i;
  1466. }
  1467. if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
  1468. bad_uuid = true;
  1469. if (ofs->config.xino == OVL_XINO_AUTO) {
  1470. ofs->config.xino = OVL_XINO_OFF;
  1471. warn = true;
  1472. }
  1473. if (ofs->config.index || ofs->config.nfs_export) {
  1474. ofs->config.index = false;
  1475. ofs->config.nfs_export = false;
  1476. warn = true;
  1477. }
  1478. if (warn) {
  1479. pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n",
  1480. uuid_is_null(&sb->s_uuid) ? "null" :
  1481. "conflicting",
  1482. path->dentry, ovl_xino_str[ofs->config.xino]);
  1483. }
  1484. }
  1485. err = get_anon_bdev(&dev);
  1486. if (err) {
  1487. pr_err("failed to get anonymous bdev for lowerpath\n");
  1488. return err;
  1489. }
  1490. ofs->fs[ofs->numfs].sb = sb;
  1491. ofs->fs[ofs->numfs].pseudo_dev = dev;
  1492. ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
  1493. return ofs->numfs++;
  1494. }
  1495. static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
  1496. struct path *stack, unsigned int numlower,
  1497. struct ovl_layer *layers)
  1498. {
  1499. int err;
  1500. unsigned int i;
  1501. err = -ENOMEM;
  1502. ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
  1503. if (ofs->fs == NULL)
  1504. goto out;
  1505. /* idx/fsid 0 are reserved for upper fs even with lower only overlay */
  1506. ofs->numfs++;
  1507. /*
  1508. * All lower layers that share the same fs as upper layer, use the same
  1509. * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
  1510. * only overlay to simplify ovl_fs_free().
  1511. * is_lower will be set if upper fs is shared with a lower layer.
  1512. */
  1513. err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
  1514. if (err) {
  1515. pr_err("failed to get anonymous bdev for upper fs\n");
  1516. goto out;
  1517. }
  1518. if (ovl_upper_mnt(ofs)) {
  1519. ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
  1520. ofs->fs[0].is_lower = false;
  1521. }
  1522. for (i = 0; i < numlower; i++) {
  1523. struct vfsmount *mnt;
  1524. struct inode *trap;
  1525. int fsid;
  1526. err = fsid = ovl_get_fsid(ofs, &stack[i]);
  1527. if (err < 0)
  1528. goto out;
  1529. /*
  1530. * Check if lower root conflicts with this overlay layers before
  1531. * checking if it is in-use as upperdir/workdir of "another"
  1532. * mount, because we do not bother to check in ovl_is_inuse() if
  1533. * the upperdir/workdir is in fact in-use by our
  1534. * upperdir/workdir.
  1535. */
  1536. err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
  1537. if (err)
  1538. goto out;
  1539. if (ovl_is_inuse(stack[i].dentry)) {
  1540. err = ovl_report_in_use(ofs, "lowerdir");
  1541. if (err) {
  1542. iput(trap);
  1543. goto out;
  1544. }
  1545. }
  1546. mnt = clone_private_mount(&stack[i]);
  1547. err = PTR_ERR(mnt);
  1548. if (IS_ERR(mnt)) {
  1549. pr_err("failed to clone lowerpath\n");
  1550. iput(trap);
  1551. goto out;
  1552. }
  1553. /*
  1554. * Make lower layers R/O. That way fchmod/fchown on lower file
  1555. * will fail instead of modifying lower fs.
  1556. */
  1557. #ifdef CONFIG_KDP_NS
  1558. kdp_set_mnt_flags(mnt, MNT_READONLY|MNT_NOATIME);
  1559. #else
  1560. mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
  1561. #endif
  1562. layers[ofs->numlayer].trap = trap;
  1563. layers[ofs->numlayer].mnt = mnt;
  1564. layers[ofs->numlayer].idx = ofs->numlayer;
  1565. layers[ofs->numlayer].fsid = fsid;
  1566. layers[ofs->numlayer].fs = &ofs->fs[fsid];
  1567. ofs->numlayer++;
  1568. ofs->fs[fsid].is_lower = true;
  1569. }
  1570. /*
  1571. * When all layers on same fs, overlay can use real inode numbers.
  1572. * With mount option "xino=<on|auto>", mounter declares that there are
  1573. * enough free high bits in underlying fs to hold the unique fsid.
  1574. * If overlayfs does encounter underlying inodes using the high xino
  1575. * bits reserved for fsid, it emits a warning and uses the original
  1576. * inode number or a non persistent inode number allocated from a
  1577. * dedicated range.
  1578. */
  1579. if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
  1580. if (ofs->config.xino == OVL_XINO_ON)
  1581. pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
  1582. ofs->xino_mode = 0;
  1583. } else if (ofs->config.xino == OVL_XINO_OFF) {
  1584. ofs->xino_mode = -1;
  1585. } else if (ofs->xino_mode < 0) {
  1586. /*
  1587. * This is a roundup of number of bits needed for encoding
  1588. * fsid, where fsid 0 is reserved for upper fs (even with
  1589. * lower only overlay) +1 extra bit is reserved for the non
  1590. * persistent inode number range that is used for resolving
  1591. * xino lower bits overflow.
  1592. */
  1593. BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
  1594. ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
  1595. }
  1596. if (ofs->xino_mode > 0) {
  1597. pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
  1598. ofs->xino_mode);
  1599. }
  1600. err = 0;
  1601. out:
  1602. return err;
  1603. }
  1604. static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
  1605. const char *lower, unsigned int numlower,
  1606. struct ovl_fs *ofs, struct ovl_layer *layers)
  1607. {
  1608. int err;
  1609. struct path *stack = NULL;
  1610. unsigned int i;
  1611. struct ovl_entry *oe;
  1612. if (!ofs->config.upperdir && numlower == 1) {
  1613. pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
  1614. return ERR_PTR(-EINVAL);
  1615. }
  1616. stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
  1617. if (!stack)
  1618. return ERR_PTR(-ENOMEM);
  1619. err = -EINVAL;
  1620. for (i = 0; i < numlower; i++) {
  1621. err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
  1622. if (err)
  1623. goto out_err;
  1624. lower = strchr(lower, '\0') + 1;
  1625. }
  1626. err = -EINVAL;
  1627. sb->s_stack_depth++;
  1628. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  1629. pr_err("maximum fs stacking depth exceeded\n");
  1630. goto out_err;
  1631. }
  1632. err = ovl_get_layers(sb, ofs, stack, numlower, layers);
  1633. if (err)
  1634. goto out_err;
  1635. err = -ENOMEM;
  1636. oe = ovl_alloc_entry(numlower);
  1637. if (!oe)
  1638. goto out_err;
  1639. for (i = 0; i < numlower; i++) {
  1640. oe->lowerstack[i].dentry = dget(stack[i].dentry);
  1641. oe->lowerstack[i].layer = &ofs->layers[i+1];
  1642. }
  1643. out:
  1644. for (i = 0; i < numlower; i++)
  1645. path_put(&stack[i]);
  1646. kfree(stack);
  1647. return oe;
  1648. out_err:
  1649. oe = ERR_PTR(err);
  1650. goto out;
  1651. }
  1652. /*
  1653. * Check if this layer root is a descendant of:
  1654. * - another layer of this overlayfs instance
  1655. * - upper/work dir of any overlayfs instance
  1656. */
  1657. static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
  1658. struct dentry *dentry, const char *name,
  1659. bool is_lower)
  1660. {
  1661. struct dentry *next = dentry, *parent;
  1662. int err = 0;
  1663. if (!dentry)
  1664. return 0;
  1665. parent = dget_parent(next);
  1666. /* Walk back ancestors to root (inclusive) looking for traps */
  1667. while (!err && parent != next) {
  1668. if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
  1669. err = -ELOOP;
  1670. pr_err("overlapping %s path\n", name);
  1671. } else if (ovl_is_inuse(parent)) {
  1672. err = ovl_report_in_use(ofs, name);
  1673. }
  1674. next = parent;
  1675. parent = dget_parent(next);
  1676. dput(next);
  1677. }
  1678. dput(parent);
  1679. return err;
  1680. }
  1681. /*
  1682. * Check if any of the layers or work dirs overlap.
  1683. */
  1684. static int ovl_check_overlapping_layers(struct super_block *sb,
  1685. struct ovl_fs *ofs)
  1686. {
  1687. int i, err;
  1688. if (ovl_upper_mnt(ofs)) {
  1689. err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
  1690. "upperdir", false);
  1691. if (err)
  1692. return err;
  1693. /*
  1694. * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
  1695. * this instance and covers overlapping work and index dirs,
  1696. * unless work or index dir have been moved since created inside
  1697. * workbasedir. In that case, we already have their traps in
  1698. * inode cache and we will catch that case on lookup.
  1699. */
  1700. err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
  1701. false);
  1702. if (err)
  1703. return err;
  1704. }
  1705. for (i = 1; i < ofs->numlayer; i++) {
  1706. err = ovl_check_layer(sb, ofs,
  1707. ofs->layers[i].mnt->mnt_root,
  1708. "lowerdir", true);
  1709. if (err)
  1710. return err;
  1711. }
  1712. return 0;
  1713. }
  1714. static struct dentry *ovl_get_root(struct super_block *sb,
  1715. struct dentry *upperdentry,
  1716. struct ovl_entry *oe)
  1717. {
  1718. struct dentry *root;
  1719. struct ovl_path *lowerpath = &oe->lowerstack[0];
  1720. unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
  1721. int fsid = lowerpath->layer->fsid;
  1722. struct ovl_inode_params oip = {
  1723. .upperdentry = upperdentry,
  1724. .lowerpath = lowerpath,
  1725. };
  1726. root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
  1727. if (!root)
  1728. return NULL;
  1729. root->d_fsdata = oe;
  1730. if (upperdentry) {
  1731. /* Root inode uses upper st_ino/i_ino */
  1732. ino = d_inode(upperdentry)->i_ino;
  1733. fsid = 0;
  1734. ovl_dentry_set_upper_alias(root);
  1735. if (ovl_is_impuredir(sb, upperdentry))
  1736. ovl_set_flag(OVL_IMPURE, d_inode(root));
  1737. }
  1738. /* Root is always merge -> can have whiteouts */
  1739. ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
  1740. ovl_dentry_set_flag(OVL_E_CONNECTED, root);
  1741. ovl_set_upperdata(d_inode(root));
  1742. ovl_inode_init(d_inode(root), &oip, ino, fsid);
  1743. ovl_dentry_init_flags(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
  1744. return root;
  1745. }
  1746. static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  1747. {
  1748. struct path upperpath = { };
  1749. struct dentry *root_dentry;
  1750. struct ovl_entry *oe;
  1751. struct ovl_fs *ofs;
  1752. struct ovl_layer *layers;
  1753. struct cred *cred;
  1754. char *splitlower = NULL;
  1755. unsigned int numlower;
  1756. int err;
  1757. err = -EIO;
  1758. if (WARN_ON(sb->s_user_ns != current_user_ns()))
  1759. goto out;
  1760. sb->s_d_op = &ovl_dentry_operations;
  1761. err = -ENOMEM;
  1762. ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  1763. if (!ofs)
  1764. goto out;
  1765. err = -ENOMEM;
  1766. ofs->creator_cred = cred = prepare_creds();
  1767. if (!cred)
  1768. goto out_err;
  1769. /* Is there a reason anyone would want not to share whiteouts? */
  1770. ofs->share_whiteout = true;
  1771. ofs->config.index = ovl_index_def;
  1772. ofs->config.uuid = true;
  1773. ofs->config.nfs_export = ovl_nfs_export_def;
  1774. ofs->config.xino = ovl_xino_def();
  1775. ofs->config.metacopy = ovl_metacopy_def;
  1776. err = ovl_parse_opt((char *) data, &ofs->config);
  1777. if (err)
  1778. goto out_err;
  1779. err = -EINVAL;
  1780. if (!ofs->config.lowerdir) {
  1781. if (!silent)
  1782. pr_err("missing 'lowerdir'\n");
  1783. goto out_err;
  1784. }
  1785. err = -ENOMEM;
  1786. splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
  1787. if (!splitlower)
  1788. goto out_err;
  1789. err = -EINVAL;
  1790. numlower = ovl_split_lowerdirs(splitlower);
  1791. if (numlower > OVL_MAX_STACK) {
  1792. pr_err("too many lower directories, limit is %d\n",
  1793. OVL_MAX_STACK);
  1794. goto out_err;
  1795. }
  1796. err = -ENOMEM;
  1797. layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
  1798. if (!layers)
  1799. goto out_err;
  1800. ofs->layers = layers;
  1801. /* Layer 0 is reserved for upper even if there's no upper */
  1802. ofs->numlayer = 1;
  1803. sb->s_stack_depth = 0;
  1804. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1805. atomic_long_set(&ofs->last_ino, 1);
  1806. /* Assume underlying fs uses 32bit inodes unless proven otherwise */
  1807. if (ofs->config.xino != OVL_XINO_OFF) {
  1808. ofs->xino_mode = BITS_PER_LONG - 32;
  1809. if (!ofs->xino_mode) {
  1810. pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
  1811. ofs->config.xino = OVL_XINO_OFF;
  1812. }
  1813. }
  1814. /* alloc/destroy_inode needed for setting up traps in inode cache */
  1815. sb->s_op = &ovl_super_operations;
  1816. if (ofs->config.upperdir) {
  1817. struct super_block *upper_sb;
  1818. err = -EINVAL;
  1819. if (!ofs->config.workdir) {
  1820. pr_err("missing 'workdir'\n");
  1821. goto out_err;
  1822. }
  1823. err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
  1824. if (err)
  1825. goto out_err;
  1826. upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
  1827. if (!ovl_should_sync(ofs)) {
  1828. ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
  1829. if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
  1830. err = -EIO;
  1831. pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
  1832. goto out_err;
  1833. }
  1834. }
  1835. err = ovl_get_workdir(sb, ofs, &upperpath);
  1836. if (err)
  1837. goto out_err;
  1838. if (!ofs->workdir)
  1839. sb->s_flags |= SB_RDONLY;
  1840. sb->s_stack_depth = upper_sb->s_stack_depth;
  1841. sb->s_time_gran = upper_sb->s_time_gran;
  1842. }
  1843. oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
  1844. err = PTR_ERR(oe);
  1845. if (IS_ERR(oe))
  1846. goto out_err;
  1847. /* If the upper fs is nonexistent, we mark overlayfs r/o too */
  1848. if (!ovl_upper_mnt(ofs))
  1849. sb->s_flags |= SB_RDONLY;
  1850. if (!ofs->config.uuid && ofs->numfs > 1) {
  1851. pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n");
  1852. ofs->config.uuid = true;
  1853. }
  1854. if (!ovl_force_readonly(ofs) && ofs->config.index) {
  1855. err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
  1856. if (err)
  1857. goto out_free_oe;
  1858. /* Force r/o mount with no index dir */
  1859. if (!ofs->indexdir)
  1860. sb->s_flags |= SB_RDONLY;
  1861. }
  1862. err = ovl_check_overlapping_layers(sb, ofs);
  1863. if (err)
  1864. goto out_free_oe;
  1865. /* Show index=off in /proc/mounts for forced r/o mount */
  1866. if (!ofs->indexdir) {
  1867. ofs->config.index = false;
  1868. if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
  1869. pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
  1870. ofs->config.nfs_export = false;
  1871. }
  1872. }
  1873. if (ofs->config.metacopy && ofs->config.nfs_export) {
  1874. pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
  1875. ofs->config.nfs_export = false;
  1876. }
  1877. if (ofs->config.nfs_export)
  1878. sb->s_export_op = &ovl_export_operations;
  1879. /* Never override disk quota limits or use reserved space */
  1880. cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
  1881. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  1882. sb->s_xattr = ofs->config.userxattr ? ovl_user_xattr_handlers :
  1883. ovl_trusted_xattr_handlers;
  1884. sb->s_fs_info = ofs;
  1885. sb->s_flags |= SB_POSIXACL;
  1886. sb->s_iflags |= SB_I_SKIP_SYNC;
  1887. err = -ENOMEM;
  1888. root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
  1889. if (!root_dentry)
  1890. goto out_free_oe;
  1891. mntput(upperpath.mnt);
  1892. kfree(splitlower);
  1893. sb->s_root = root_dentry;
  1894. return 0;
  1895. out_free_oe:
  1896. ovl_entry_stack_free(oe);
  1897. kfree(oe);
  1898. out_err:
  1899. kfree(splitlower);
  1900. path_put(&upperpath);
  1901. ovl_free_fs(ofs);
  1902. out:
  1903. return err;
  1904. }
  1905. static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  1906. const char *dev_name, void *raw_data)
  1907. {
  1908. return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  1909. }
  1910. static struct file_system_type ovl_fs_type = {
  1911. .owner = THIS_MODULE,
  1912. .name = "overlay",
  1913. .fs_flags = FS_USERNS_MOUNT,
  1914. .mount = ovl_mount,
  1915. .kill_sb = kill_anon_super,
  1916. };
  1917. MODULE_ALIAS_FS("overlay");
  1918. static void ovl_inode_init_once(void *foo)
  1919. {
  1920. struct ovl_inode *oi = foo;
  1921. inode_init_once(&oi->vfs_inode);
  1922. }
  1923. static int __init ovl_init(void)
  1924. {
  1925. int err;
  1926. ovl_inode_cachep = kmem_cache_create("ovl_inode",
  1927. sizeof(struct ovl_inode), 0,
  1928. (SLAB_RECLAIM_ACCOUNT|
  1929. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1930. ovl_inode_init_once);
  1931. if (ovl_inode_cachep == NULL)
  1932. return -ENOMEM;
  1933. err = ovl_aio_request_cache_init();
  1934. if (!err) {
  1935. err = register_filesystem(&ovl_fs_type);
  1936. if (!err)
  1937. return 0;
  1938. ovl_aio_request_cache_destroy();
  1939. }
  1940. kmem_cache_destroy(ovl_inode_cachep);
  1941. return err;
  1942. }
  1943. static void __exit ovl_exit(void)
  1944. {
  1945. unregister_filesystem(&ovl_fs_type);
  1946. /*
  1947. * Make sure all delayed rcu free inodes are flushed before we
  1948. * destroy cache.
  1949. */
  1950. rcu_barrier();
  1951. kmem_cache_destroy(ovl_inode_cachep);
  1952. ovl_aio_request_cache_destroy();
  1953. }
  1954. module_init(ovl_init);
  1955. module_exit(ovl_exit);