super.c 56 KB

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