super.c 52 KB

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