super.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <uapi/linux/magic.h>
  10. #include <linux/fs.h>
  11. #include <linux/namei.h>
  12. #include <linux/xattr.h>
  13. #include <linux/mount.h>
  14. #include <linux/parser.h>
  15. #include <linux/module.h>
  16. #include <linux/statfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/posix_acl_xattr.h>
  19. #include <linux/exportfs.h>
  20. #include "overlayfs.h"
  21. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  22. MODULE_DESCRIPTION("Overlay filesystem");
  23. MODULE_LICENSE("GPL");
  24. struct ovl_dir_cache;
  25. #define OVL_MAX_STACK 500
  26. static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
  27. module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
  28. MODULE_PARM_DESC(ovl_redirect_dir_def,
  29. "Default to on or off for the redirect_dir feature");
  30. static bool ovl_redirect_always_follow =
  31. IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
  32. module_param_named(redirect_always_follow, ovl_redirect_always_follow,
  33. bool, 0644);
  34. MODULE_PARM_DESC(ovl_redirect_always_follow,
  35. "Follow redirects even if redirect_dir feature is turned off");
  36. static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
  37. module_param_named(index, ovl_index_def, bool, 0644);
  38. MODULE_PARM_DESC(ovl_index_def,
  39. "Default to on or off for the inodes index feature");
  40. static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
  41. module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
  42. MODULE_PARM_DESC(ovl_nfs_export_def,
  43. "Default to on or off for the NFS export feature");
  44. static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
  45. module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
  46. MODULE_PARM_DESC(ovl_xino_auto_def,
  47. "Auto enable xino feature");
  48. static bool ovl_permit_mounts_in_userns;
  49. module_param_named_unsafe(permit_mounts_in_userns, ovl_permit_mounts_in_userns,
  50. bool, 0444);
  51. MODULE_PARM_DESC(permit_mounts_in_userns, "Permit mounts in user namespaces");
  52. static void ovl_entry_stack_free(struct ovl_entry *oe)
  53. {
  54. unsigned int i;
  55. for (i = 0; i < oe->numlower; i++)
  56. dput(oe->lowerstack[i].dentry);
  57. }
  58. static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
  59. module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
  60. MODULE_PARM_DESC(ovl_metacopy_def,
  61. "Default to on or off for the metadata only copy up feature");
  62. static void ovl_dentry_release(struct dentry *dentry)
  63. {
  64. struct ovl_entry *oe = dentry->d_fsdata;
  65. if (oe) {
  66. ovl_entry_stack_free(oe);
  67. kfree_rcu(oe, rcu);
  68. }
  69. }
  70. static struct dentry *ovl_d_real(struct dentry *dentry,
  71. const struct inode *inode)
  72. {
  73. struct dentry *real;
  74. /* It's an overlay file */
  75. if (inode && d_inode(dentry) == inode)
  76. return dentry;
  77. if (!d_is_reg(dentry)) {
  78. if (!inode || inode == d_inode(dentry))
  79. return dentry;
  80. goto bug;
  81. }
  82. real = ovl_dentry_upper(dentry);
  83. if (real && (inode == d_inode(real)))
  84. return real;
  85. if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
  86. return real;
  87. real = ovl_dentry_lowerdata(dentry);
  88. if (!real)
  89. goto bug;
  90. /* Handle recursion */
  91. real = d_real(real, inode);
  92. if (!inode || inode == d_inode(real))
  93. return real;
  94. bug:
  95. WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
  96. inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
  97. return dentry;
  98. }
  99. static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
  100. {
  101. struct ovl_entry *oe = dentry->d_fsdata;
  102. unsigned int i;
  103. int ret = 1;
  104. for (i = 0; i < oe->numlower; i++) {
  105. struct dentry *d = oe->lowerstack[i].dentry;
  106. if (d->d_flags & DCACHE_OP_REVALIDATE) {
  107. ret = d->d_op->d_revalidate(d, flags);
  108. if (ret < 0)
  109. return ret;
  110. if (!ret) {
  111. if (!(flags & LOOKUP_RCU))
  112. d_invalidate(d);
  113. return -ESTALE;
  114. }
  115. }
  116. }
  117. return 1;
  118. }
  119. static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
  120. {
  121. struct ovl_entry *oe = dentry->d_fsdata;
  122. unsigned int i;
  123. int ret = 1;
  124. for (i = 0; i < oe->numlower; i++) {
  125. struct dentry *d = oe->lowerstack[i].dentry;
  126. if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
  127. ret = d->d_op->d_weak_revalidate(d, flags);
  128. if (ret <= 0)
  129. break;
  130. }
  131. }
  132. return ret;
  133. }
  134. static const struct dentry_operations ovl_dentry_operations = {
  135. .d_release = ovl_dentry_release,
  136. .d_real = ovl_d_real,
  137. };
  138. static const struct dentry_operations ovl_reval_dentry_operations = {
  139. .d_release = ovl_dentry_release,
  140. .d_real = ovl_d_real,
  141. .d_revalidate = ovl_dentry_revalidate,
  142. .d_weak_revalidate = ovl_dentry_weak_revalidate,
  143. };
  144. static struct kmem_cache *ovl_inode_cachep;
  145. static struct inode *ovl_alloc_inode(struct super_block *sb)
  146. {
  147. struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
  148. if (!oi)
  149. return NULL;
  150. oi->cache = NULL;
  151. oi->redirect = NULL;
  152. oi->version = 0;
  153. oi->flags = 0;
  154. oi->__upperdentry = NULL;
  155. oi->lower = NULL;
  156. oi->lowerdata = NULL;
  157. mutex_init(&oi->lock);
  158. return &oi->vfs_inode;
  159. }
  160. static void ovl_i_callback(struct rcu_head *head)
  161. {
  162. struct inode *inode = container_of(head, struct inode, i_rcu);
  163. kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
  164. }
  165. static void ovl_destroy_inode(struct inode *inode)
  166. {
  167. struct ovl_inode *oi = OVL_I(inode);
  168. dput(oi->__upperdentry);
  169. iput(oi->lower);
  170. if (S_ISDIR(inode->i_mode))
  171. ovl_dir_cache_free(inode);
  172. else
  173. iput(oi->lowerdata);
  174. kfree(oi->redirect);
  175. mutex_destroy(&oi->lock);
  176. call_rcu(&inode->i_rcu, ovl_i_callback);
  177. }
  178. static void ovl_free_fs(struct ovl_fs *ofs)
  179. {
  180. unsigned i;
  181. iput(ofs->workbasedir_trap);
  182. iput(ofs->indexdir_trap);
  183. iput(ofs->workdir_trap);
  184. iput(ofs->upperdir_trap);
  185. dput(ofs->indexdir);
  186. dput(ofs->workdir);
  187. if (ofs->workdir_locked)
  188. ovl_inuse_unlock(ofs->workbasedir);
  189. dput(ofs->workbasedir);
  190. if (ofs->upperdir_locked)
  191. ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
  192. mntput(ofs->upper_mnt);
  193. for (i = 0; i < ofs->numlower; i++) {
  194. iput(ofs->lower_layers[i].trap);
  195. mntput(ofs->lower_layers[i].mnt);
  196. }
  197. for (i = 0; i < ofs->numlowerfs; i++)
  198. free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
  199. kfree(ofs->lower_layers);
  200. kfree(ofs->lower_fs);
  201. kfree(ofs->config.lowerdir);
  202. kfree(ofs->config.upperdir);
  203. kfree(ofs->config.workdir);
  204. kfree(ofs->config.redirect_mode);
  205. if (ofs->creator_cred)
  206. put_cred(ofs->creator_cred);
  207. kfree(ofs);
  208. }
  209. static void ovl_put_super(struct super_block *sb)
  210. {
  211. struct ovl_fs *ofs = sb->s_fs_info;
  212. ovl_free_fs(ofs);
  213. }
  214. /* Sync real dirty inodes in upper filesystem (if it exists) */
  215. static int ovl_sync_fs(struct super_block *sb, int wait)
  216. {
  217. struct ovl_fs *ofs = sb->s_fs_info;
  218. struct super_block *upper_sb;
  219. int ret;
  220. if (!ofs->upper_mnt)
  221. return 0;
  222. /*
  223. * If this is a sync(2) call or an emergency sync, all the super blocks
  224. * will be iterated, including upper_sb, so no need to do anything.
  225. *
  226. * If this is a syncfs(2) call, then we do need to call
  227. * sync_filesystem() on upper_sb, but enough if we do it when being
  228. * called with wait == 1.
  229. */
  230. if (!wait)
  231. return 0;
  232. upper_sb = ofs->upper_mnt->mnt_sb;
  233. down_read(&upper_sb->s_umount);
  234. ret = sync_filesystem(upper_sb);
  235. up_read(&upper_sb->s_umount);
  236. return ret;
  237. }
  238. /**
  239. * ovl_statfs
  240. * @sb: The overlayfs super block
  241. * @buf: The struct kstatfs to fill in with stats
  242. *
  243. * Get the filesystem statistics. As writes always target the upper layer
  244. * filesystem pass the statfs to the upper filesystem (if it exists)
  245. */
  246. static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  247. {
  248. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  249. struct dentry *root_dentry = dentry->d_sb->s_root;
  250. struct path path;
  251. int err;
  252. ovl_path_real(root_dentry, &path);
  253. err = vfs_statfs(&path, buf);
  254. if (!err) {
  255. buf->f_namelen = ofs->namelen;
  256. buf->f_type = OVERLAYFS_SUPER_MAGIC;
  257. }
  258. return err;
  259. }
  260. /* Will this overlay be forced to mount/remount ro? */
  261. static bool ovl_force_readonly(struct ovl_fs *ofs)
  262. {
  263. return (!ofs->upper_mnt || !ofs->workdir);
  264. }
  265. static const char *ovl_redirect_mode_def(void)
  266. {
  267. return ovl_redirect_dir_def ? "on" : "off";
  268. }
  269. enum {
  270. OVL_XINO_OFF,
  271. OVL_XINO_AUTO,
  272. OVL_XINO_ON,
  273. };
  274. static const char * const ovl_xino_str[] = {
  275. "off",
  276. "auto",
  277. "on",
  278. };
  279. static inline int ovl_xino_def(void)
  280. {
  281. return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
  282. }
  283. /**
  284. * ovl_show_options
  285. *
  286. * Prints the mount options for a given superblock.
  287. * Returns zero; does not fail.
  288. */
  289. static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  290. {
  291. struct super_block *sb = dentry->d_sb;
  292. struct ovl_fs *ofs = sb->s_fs_info;
  293. seq_show_option(m, "lowerdir", ofs->config.lowerdir);
  294. if (ofs->config.upperdir) {
  295. seq_show_option(m, "upperdir", ofs->config.upperdir);
  296. seq_show_option(m, "workdir", ofs->config.workdir);
  297. }
  298. if (ofs->config.default_permissions)
  299. seq_puts(m, ",default_permissions");
  300. if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
  301. seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
  302. if (ofs->config.index != ovl_index_def)
  303. seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
  304. if (ofs->config.nfs_export != ovl_nfs_export_def)
  305. seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
  306. "on" : "off");
  307. if (ofs->config.xino != ovl_xino_def())
  308. seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
  309. if (ofs->config.metacopy != ovl_metacopy_def)
  310. seq_printf(m, ",metacopy=%s",
  311. ofs->config.metacopy ? "on" : "off");
  312. return 0;
  313. }
  314. static int ovl_remount(struct super_block *sb, int *flags, char *data)
  315. {
  316. struct ovl_fs *ofs = sb->s_fs_info;
  317. if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
  318. return -EROFS;
  319. return 0;
  320. }
  321. static const struct super_operations ovl_super_operations = {
  322. .alloc_inode = ovl_alloc_inode,
  323. .destroy_inode = ovl_destroy_inode,
  324. .drop_inode = generic_delete_inode,
  325. .put_super = ovl_put_super,
  326. .sync_fs = ovl_sync_fs,
  327. .statfs = ovl_statfs,
  328. .show_options = ovl_show_options,
  329. .remount_fs = ovl_remount,
  330. };
  331. enum {
  332. OPT_LOWERDIR,
  333. OPT_UPPERDIR,
  334. OPT_WORKDIR,
  335. OPT_DEFAULT_PERMISSIONS,
  336. OPT_REDIRECT_DIR,
  337. OPT_INDEX_ON,
  338. OPT_INDEX_OFF,
  339. OPT_NFS_EXPORT_ON,
  340. OPT_NFS_EXPORT_OFF,
  341. OPT_XINO_ON,
  342. OPT_XINO_OFF,
  343. OPT_XINO_AUTO,
  344. OPT_METACOPY_ON,
  345. OPT_METACOPY_OFF,
  346. OPT_ERR,
  347. };
  348. static const match_table_t ovl_tokens = {
  349. {OPT_LOWERDIR, "lowerdir=%s"},
  350. {OPT_UPPERDIR, "upperdir=%s"},
  351. {OPT_WORKDIR, "workdir=%s"},
  352. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  353. {OPT_REDIRECT_DIR, "redirect_dir=%s"},
  354. {OPT_INDEX_ON, "index=on"},
  355. {OPT_INDEX_OFF, "index=off"},
  356. {OPT_NFS_EXPORT_ON, "nfs_export=on"},
  357. {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
  358. {OPT_XINO_ON, "xino=on"},
  359. {OPT_XINO_OFF, "xino=off"},
  360. {OPT_XINO_AUTO, "xino=auto"},
  361. {OPT_METACOPY_ON, "metacopy=on"},
  362. {OPT_METACOPY_OFF, "metacopy=off"},
  363. {OPT_ERR, NULL}
  364. };
  365. static char *ovl_next_opt(char **s)
  366. {
  367. char *sbegin = *s;
  368. char *p;
  369. if (sbegin == NULL)
  370. return NULL;
  371. for (p = sbegin; *p; p++) {
  372. if (*p == '\\') {
  373. p++;
  374. if (!*p)
  375. break;
  376. } else if (*p == ',') {
  377. *p = '\0';
  378. *s = p + 1;
  379. return sbegin;
  380. }
  381. }
  382. *s = NULL;
  383. return sbegin;
  384. }
  385. static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
  386. {
  387. if (strcmp(mode, "on") == 0) {
  388. config->redirect_dir = true;
  389. /*
  390. * Does not make sense to have redirect creation without
  391. * redirect following.
  392. */
  393. config->redirect_follow = true;
  394. } else if (strcmp(mode, "follow") == 0) {
  395. config->redirect_follow = true;
  396. } else if (strcmp(mode, "off") == 0) {
  397. if (ovl_redirect_always_follow)
  398. config->redirect_follow = true;
  399. } else if (strcmp(mode, "nofollow") != 0) {
  400. pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
  401. mode);
  402. return -EINVAL;
  403. }
  404. return 0;
  405. }
  406. static int ovl_parse_opt(char *opt, struct ovl_config *config)
  407. {
  408. char *p;
  409. int err;
  410. bool metacopy_opt = false, redirect_opt = false;
  411. config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
  412. if (!config->redirect_mode)
  413. return -ENOMEM;
  414. while ((p = ovl_next_opt(&opt)) != NULL) {
  415. int token;
  416. substring_t args[MAX_OPT_ARGS];
  417. if (!*p)
  418. continue;
  419. token = match_token(p, ovl_tokens, args);
  420. switch (token) {
  421. case OPT_UPPERDIR:
  422. kfree(config->upperdir);
  423. config->upperdir = match_strdup(&args[0]);
  424. if (!config->upperdir)
  425. return -ENOMEM;
  426. break;
  427. case OPT_LOWERDIR:
  428. kfree(config->lowerdir);
  429. config->lowerdir = match_strdup(&args[0]);
  430. if (!config->lowerdir)
  431. return -ENOMEM;
  432. break;
  433. case OPT_WORKDIR:
  434. kfree(config->workdir);
  435. config->workdir = match_strdup(&args[0]);
  436. if (!config->workdir)
  437. return -ENOMEM;
  438. break;
  439. case OPT_DEFAULT_PERMISSIONS:
  440. config->default_permissions = true;
  441. break;
  442. case OPT_REDIRECT_DIR:
  443. kfree(config->redirect_mode);
  444. config->redirect_mode = match_strdup(&args[0]);
  445. if (!config->redirect_mode)
  446. return -ENOMEM;
  447. redirect_opt = true;
  448. break;
  449. case OPT_INDEX_ON:
  450. config->index = true;
  451. break;
  452. case OPT_INDEX_OFF:
  453. config->index = false;
  454. break;
  455. case OPT_NFS_EXPORT_ON:
  456. config->nfs_export = true;
  457. break;
  458. case OPT_NFS_EXPORT_OFF:
  459. config->nfs_export = false;
  460. break;
  461. case OPT_XINO_ON:
  462. config->xino = OVL_XINO_ON;
  463. break;
  464. case OPT_XINO_OFF:
  465. config->xino = OVL_XINO_OFF;
  466. break;
  467. case OPT_XINO_AUTO:
  468. config->xino = OVL_XINO_AUTO;
  469. break;
  470. case OPT_METACOPY_ON:
  471. config->metacopy = true;
  472. metacopy_opt = true;
  473. break;
  474. case OPT_METACOPY_OFF:
  475. config->metacopy = false;
  476. break;
  477. default:
  478. pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
  479. return -EINVAL;
  480. }
  481. }
  482. /* Workdir is useless in non-upper mount */
  483. if (!config->upperdir && config->workdir) {
  484. pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
  485. config->workdir);
  486. kfree(config->workdir);
  487. config->workdir = NULL;
  488. }
  489. err = ovl_parse_redirect_mode(config, config->redirect_mode);
  490. if (err)
  491. return err;
  492. /*
  493. * This is to make the logic below simpler. It doesn't make any other
  494. * difference, since config->redirect_dir is only used for upper.
  495. */
  496. if (!config->upperdir && config->redirect_follow)
  497. config->redirect_dir = true;
  498. /* Resolve metacopy -> redirect_dir dependency */
  499. if (config->metacopy && !config->redirect_dir) {
  500. if (metacopy_opt && redirect_opt) {
  501. pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
  502. config->redirect_mode);
  503. return -EINVAL;
  504. }
  505. if (redirect_opt) {
  506. /*
  507. * There was an explicit redirect_dir=... that resulted
  508. * in this conflict.
  509. */
  510. pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
  511. config->redirect_mode);
  512. config->metacopy = false;
  513. } else {
  514. /* Automatically enable redirect otherwise. */
  515. config->redirect_follow = config->redirect_dir = true;
  516. }
  517. }
  518. return 0;
  519. }
  520. #define OVL_WORKDIR_NAME "work"
  521. #define OVL_INDEXDIR_NAME "index"
  522. static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
  523. const char *name, bool persist)
  524. {
  525. struct inode *dir = ofs->workbasedir->d_inode;
  526. struct vfsmount *mnt = ofs->upper_mnt;
  527. struct dentry *work;
  528. int err;
  529. bool retried = false;
  530. bool locked = false;
  531. inode_lock_nested(dir, I_MUTEX_PARENT);
  532. locked = true;
  533. retry:
  534. work = lookup_one_len(name, ofs->workbasedir, strlen(name));
  535. if (!IS_ERR(work)) {
  536. struct iattr attr = {
  537. .ia_valid = ATTR_MODE,
  538. .ia_mode = S_IFDIR | 0,
  539. };
  540. if (work->d_inode) {
  541. err = -EEXIST;
  542. if (retried)
  543. goto out_dput;
  544. if (persist)
  545. goto out_unlock;
  546. retried = true;
  547. ovl_workdir_cleanup(dir, mnt, work, 0);
  548. dput(work);
  549. goto retry;
  550. }
  551. work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
  552. err = PTR_ERR(work);
  553. if (IS_ERR(work))
  554. goto out_err;
  555. /*
  556. * Try to remove POSIX ACL xattrs from workdir. We are good if:
  557. *
  558. * a) success (there was a POSIX ACL xattr and was removed)
  559. * b) -ENODATA (there was no POSIX ACL xattr)
  560. * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
  561. *
  562. * There are various other error values that could effectively
  563. * mean that the xattr doesn't exist (e.g. -ERANGE is returned
  564. * if the xattr name is too long), but the set of filesystems
  565. * allowed as upper are limited to "normal" ones, where checking
  566. * for the above two errors is sufficient.
  567. */
  568. err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
  569. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  570. goto out_dput;
  571. err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
  572. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  573. goto out_dput;
  574. /* Clear any inherited mode bits */
  575. inode_lock(work->d_inode);
  576. err = notify_change(work, &attr, NULL);
  577. inode_unlock(work->d_inode);
  578. if (err)
  579. goto out_dput;
  580. } else {
  581. err = PTR_ERR(work);
  582. goto out_err;
  583. }
  584. out_unlock:
  585. if (locked)
  586. inode_unlock(dir);
  587. return work;
  588. out_dput:
  589. dput(work);
  590. out_err:
  591. pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
  592. ofs->config.workdir, name, -err);
  593. work = NULL;
  594. goto out_unlock;
  595. }
  596. static void ovl_unescape(char *s)
  597. {
  598. char *d = s;
  599. for (;; s++, d++) {
  600. if (*s == '\\')
  601. s++;
  602. *d = *s;
  603. if (!*s)
  604. break;
  605. }
  606. }
  607. static int ovl_mount_dir_noesc(const char *name, struct path *path)
  608. {
  609. int err = -EINVAL;
  610. if (!*name) {
  611. pr_err("overlayfs: empty lowerdir\n");
  612. goto out;
  613. }
  614. err = kern_path(name, LOOKUP_FOLLOW, path);
  615. if (err) {
  616. pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
  617. goto out;
  618. }
  619. err = -EINVAL;
  620. if (ovl_dentry_weird(path->dentry)) {
  621. pr_err("overlayfs: filesystem on '%s' not supported\n", name);
  622. goto out_put;
  623. }
  624. if (!d_is_dir(path->dentry)) {
  625. pr_err("overlayfs: '%s' not a directory\n", name);
  626. goto out_put;
  627. }
  628. return 0;
  629. out_put:
  630. path_put_init(path);
  631. out:
  632. return err;
  633. }
  634. static int ovl_mount_dir(const char *name, struct path *path)
  635. {
  636. int err = -ENOMEM;
  637. char *tmp = kstrdup(name, GFP_KERNEL);
  638. if (tmp) {
  639. ovl_unescape(tmp);
  640. err = ovl_mount_dir_noesc(tmp, path);
  641. if (!err)
  642. if (ovl_dentry_remote(path->dentry)) {
  643. pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
  644. tmp);
  645. path_put_init(path);
  646. err = -EINVAL;
  647. }
  648. kfree(tmp);
  649. }
  650. return err;
  651. }
  652. static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
  653. const char *name)
  654. {
  655. struct kstatfs statfs;
  656. int err = vfs_statfs(path, &statfs);
  657. if (err)
  658. pr_err("overlayfs: statfs failed on '%s'\n", name);
  659. else
  660. ofs->namelen = max(ofs->namelen, statfs.f_namelen);
  661. return err;
  662. }
  663. static int ovl_lower_dir(const char *name, struct path *path,
  664. struct ovl_fs *ofs, int *stack_depth, bool *remote)
  665. {
  666. int fh_type;
  667. int err;
  668. err = ovl_mount_dir_noesc(name, path);
  669. if (err)
  670. goto out;
  671. err = ovl_check_namelen(path, ofs, name);
  672. if (err)
  673. goto out_put;
  674. *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
  675. if (ovl_dentry_remote(path->dentry))
  676. *remote = true;
  677. /*
  678. * The inodes index feature and NFS export need to encode and decode
  679. * file handles, so they require that all layers support them.
  680. */
  681. fh_type = ovl_can_decode_fh(path->dentry->d_sb);
  682. if ((ofs->config.nfs_export ||
  683. (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
  684. ofs->config.index = false;
  685. ofs->config.nfs_export = false;
  686. pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
  687. name);
  688. }
  689. /* Check if lower fs has 32bit inode numbers */
  690. if (fh_type != FILEID_INO32_GEN)
  691. ofs->xino_bits = 0;
  692. return 0;
  693. out_put:
  694. path_put_init(path);
  695. out:
  696. return err;
  697. }
  698. /* Workdir should not be subdir of upperdir and vice versa */
  699. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  700. {
  701. bool ok = false;
  702. if (workdir != upperdir) {
  703. ok = (lock_rename(workdir, upperdir) == NULL);
  704. unlock_rename(workdir, upperdir);
  705. }
  706. return ok;
  707. }
  708. static unsigned int ovl_split_lowerdirs(char *str)
  709. {
  710. unsigned int ctr = 1;
  711. char *s, *d;
  712. for (s = d = str;; s++, d++) {
  713. if (*s == '\\') {
  714. s++;
  715. } else if (*s == ':') {
  716. *d = '\0';
  717. ctr++;
  718. continue;
  719. }
  720. *d = *s;
  721. if (!*s)
  722. break;
  723. }
  724. return ctr;
  725. }
  726. static int __maybe_unused
  727. ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
  728. struct dentry *dentry, struct inode *inode,
  729. const char *name, void *buffer, size_t size)
  730. {
  731. return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
  732. }
  733. static int __maybe_unused
  734. ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
  735. struct dentry *dentry, struct inode *inode,
  736. const char *name, const void *value,
  737. size_t size, int flags)
  738. {
  739. struct dentry *workdir = ovl_workdir(dentry);
  740. struct inode *realinode = ovl_inode_real(inode);
  741. struct posix_acl *acl = NULL;
  742. int err;
  743. /* Check that everything is OK before copy-up */
  744. if (value) {
  745. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  746. if (IS_ERR(acl))
  747. return PTR_ERR(acl);
  748. }
  749. err = -EOPNOTSUPP;
  750. if (!IS_POSIXACL(d_inode(workdir)))
  751. goto out_acl_release;
  752. if (!realinode->i_op->set_acl)
  753. goto out_acl_release;
  754. if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
  755. err = acl ? -EACCES : 0;
  756. goto out_acl_release;
  757. }
  758. err = -EPERM;
  759. if (!inode_owner_or_capable(inode))
  760. goto out_acl_release;
  761. posix_acl_release(acl);
  762. /*
  763. * Check if sgid bit needs to be cleared (actual setacl operation will
  764. * be done with mounter's capabilities and so that won't do it for us).
  765. */
  766. if (unlikely(inode->i_mode & S_ISGID) &&
  767. handler->flags == ACL_TYPE_ACCESS &&
  768. !in_group_p(inode->i_gid) &&
  769. !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
  770. struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
  771. err = ovl_setattr(dentry, &iattr);
  772. if (err)
  773. return err;
  774. }
  775. err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
  776. if (!err)
  777. ovl_copyattr(ovl_inode_real(inode), inode);
  778. return err;
  779. out_acl_release:
  780. posix_acl_release(acl);
  781. return err;
  782. }
  783. static int ovl_own_xattr_get(const struct xattr_handler *handler,
  784. struct dentry *dentry, struct inode *inode,
  785. const char *name, void *buffer, size_t size)
  786. {
  787. return -EOPNOTSUPP;
  788. }
  789. static int ovl_own_xattr_set(const struct xattr_handler *handler,
  790. struct dentry *dentry, struct inode *inode,
  791. const char *name, const void *value,
  792. size_t size, int flags)
  793. {
  794. return -EOPNOTSUPP;
  795. }
  796. static int ovl_other_xattr_get(const struct xattr_handler *handler,
  797. struct dentry *dentry, struct inode *inode,
  798. const char *name, void *buffer, size_t size)
  799. {
  800. return ovl_xattr_get(dentry, inode, name, buffer, size);
  801. }
  802. static int ovl_other_xattr_set(const struct xattr_handler *handler,
  803. struct dentry *dentry, struct inode *inode,
  804. const char *name, const void *value,
  805. size_t size, int flags)
  806. {
  807. return ovl_xattr_set(dentry, inode, name, value, size, flags);
  808. }
  809. static const struct xattr_handler __maybe_unused
  810. ovl_posix_acl_access_xattr_handler = {
  811. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  812. .flags = ACL_TYPE_ACCESS,
  813. .get = ovl_posix_acl_xattr_get,
  814. .set = ovl_posix_acl_xattr_set,
  815. };
  816. static const struct xattr_handler __maybe_unused
  817. ovl_posix_acl_default_xattr_handler = {
  818. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  819. .flags = ACL_TYPE_DEFAULT,
  820. .get = ovl_posix_acl_xattr_get,
  821. .set = ovl_posix_acl_xattr_set,
  822. };
  823. static const struct xattr_handler ovl_own_xattr_handler = {
  824. .prefix = OVL_XATTR_PREFIX,
  825. .get = ovl_own_xattr_get,
  826. .set = ovl_own_xattr_set,
  827. };
  828. static const struct xattr_handler ovl_other_xattr_handler = {
  829. .prefix = "", /* catch all */
  830. .get = ovl_other_xattr_get,
  831. .set = ovl_other_xattr_set,
  832. };
  833. static const struct xattr_handler *ovl_xattr_handlers[] = {
  834. #ifdef CONFIG_FS_POSIX_ACL
  835. &ovl_posix_acl_access_xattr_handler,
  836. &ovl_posix_acl_default_xattr_handler,
  837. #endif
  838. &ovl_own_xattr_handler,
  839. &ovl_other_xattr_handler,
  840. NULL
  841. };
  842. static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
  843. struct inode **ptrap, const char *name)
  844. {
  845. struct inode *trap;
  846. int err;
  847. trap = ovl_get_trap_inode(sb, dir);
  848. err = PTR_ERR_OR_ZERO(trap);
  849. if (err) {
  850. if (err == -ELOOP)
  851. pr_err("overlayfs: conflicting %s path\n", name);
  852. return err;
  853. }
  854. *ptrap = trap;
  855. return 0;
  856. }
  857. /*
  858. * Determine how we treat concurrent use of upperdir/workdir based on the
  859. * index feature. This is papering over mount leaks of container runtimes,
  860. * for example, an old overlay mount is leaked and now its upperdir is
  861. * attempted to be used as a lower layer in a new overlay mount.
  862. */
  863. static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
  864. {
  865. if (ofs->config.index) {
  866. pr_err("overlayfs: %s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
  867. name);
  868. return -EBUSY;
  869. } else {
  870. pr_warn("overlayfs: %s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
  871. name);
  872. return 0;
  873. }
  874. }
  875. static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
  876. struct path *upperpath)
  877. {
  878. struct vfsmount *upper_mnt;
  879. int err;
  880. err = ovl_mount_dir(ofs->config.upperdir, upperpath);
  881. if (err)
  882. goto out;
  883. /* Upper fs should not be r/o */
  884. if (sb_rdonly(upperpath->mnt->mnt_sb)) {
  885. pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
  886. err = -EINVAL;
  887. goto out;
  888. }
  889. err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
  890. if (err)
  891. goto out;
  892. err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
  893. "upperdir");
  894. if (err)
  895. goto out;
  896. upper_mnt = clone_private_mount(upperpath);
  897. err = PTR_ERR(upper_mnt);
  898. if (IS_ERR(upper_mnt)) {
  899. pr_err("overlayfs: failed to clone upperpath\n");
  900. goto out;
  901. }
  902. /* Don't inherit atime flags */
  903. upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
  904. ofs->upper_mnt = upper_mnt;
  905. if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
  906. ofs->upperdir_locked = true;
  907. } else {
  908. err = ovl_report_in_use(ofs, "upperdir");
  909. if (err)
  910. goto out;
  911. }
  912. err = 0;
  913. out:
  914. return err;
  915. }
  916. static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
  917. struct path *workpath)
  918. {
  919. struct vfsmount *mnt = ofs->upper_mnt;
  920. struct dentry *temp;
  921. int fh_type;
  922. int err;
  923. err = mnt_want_write(mnt);
  924. if (err)
  925. return err;
  926. ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
  927. if (!ofs->workdir)
  928. goto out;
  929. err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
  930. if (err)
  931. goto out;
  932. /*
  933. * Upper should support d_type, else whiteouts are visible. Given
  934. * workdir and upper are on same fs, we can do iterate_dir() on
  935. * workdir. This check requires successful creation of workdir in
  936. * previous step.
  937. */
  938. err = ovl_check_d_type_supported(workpath);
  939. if (err < 0)
  940. goto out;
  941. /*
  942. * We allowed this configuration and don't want to break users over
  943. * kernel upgrade. So warn instead of erroring out.
  944. */
  945. if (!err)
  946. pr_warn("overlayfs: upper fs needs to support d_type.\n");
  947. /* Check if upper/work fs supports O_TMPFILE */
  948. temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
  949. ofs->tmpfile = !IS_ERR(temp);
  950. if (ofs->tmpfile)
  951. dput(temp);
  952. else
  953. pr_warn("overlayfs: upper fs does not support tmpfile.\n");
  954. /*
  955. * Check if upper/work fs supports trusted.overlay.* xattr
  956. */
  957. err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
  958. if (err) {
  959. ofs->noxattr = true;
  960. ofs->config.index = false;
  961. ofs->config.metacopy = false;
  962. pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
  963. err = 0;
  964. } else {
  965. vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
  966. }
  967. /* Check if upper/work fs supports file handles */
  968. fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
  969. if (ofs->config.index && !fh_type) {
  970. ofs->config.index = false;
  971. pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
  972. }
  973. /* Check if upper fs has 32bit inode numbers */
  974. if (fh_type != FILEID_INO32_GEN)
  975. ofs->xino_bits = 0;
  976. /* NFS export of r/w mount depends on index */
  977. if (ofs->config.nfs_export && !ofs->config.index) {
  978. pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
  979. ofs->config.nfs_export = false;
  980. }
  981. out:
  982. mnt_drop_write(mnt);
  983. return err;
  984. }
  985. static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
  986. struct path *upperpath)
  987. {
  988. int err;
  989. struct path workpath = { };
  990. err = ovl_mount_dir(ofs->config.workdir, &workpath);
  991. if (err)
  992. goto out;
  993. err = -EINVAL;
  994. if (upperpath->mnt != workpath.mnt) {
  995. pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
  996. goto out;
  997. }
  998. if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
  999. pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
  1000. goto out;
  1001. }
  1002. ofs->workbasedir = dget(workpath.dentry);
  1003. if (ovl_inuse_trylock(ofs->workbasedir)) {
  1004. ofs->workdir_locked = true;
  1005. } else {
  1006. err = ovl_report_in_use(ofs, "workdir");
  1007. if (err)
  1008. goto out;
  1009. }
  1010. err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
  1011. "workdir");
  1012. if (err)
  1013. goto out;
  1014. err = ovl_make_workdir(sb, ofs, &workpath);
  1015. out:
  1016. path_put(&workpath);
  1017. return err;
  1018. }
  1019. static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
  1020. struct ovl_entry *oe, struct path *upperpath)
  1021. {
  1022. struct vfsmount *mnt = ofs->upper_mnt;
  1023. int err;
  1024. err = mnt_want_write(mnt);
  1025. if (err)
  1026. return err;
  1027. /* Verify lower root is upper root origin */
  1028. err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
  1029. true);
  1030. if (err) {
  1031. pr_err("overlayfs: failed to verify upper root origin\n");
  1032. goto out;
  1033. }
  1034. ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
  1035. if (ofs->indexdir) {
  1036. err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
  1037. "indexdir");
  1038. if (err)
  1039. goto out;
  1040. /*
  1041. * Verify upper root is exclusively associated with index dir.
  1042. * Older kernels stored upper fh in "trusted.overlay.origin"
  1043. * xattr. If that xattr exists, verify that it is a match to
  1044. * upper dir file handle. In any case, verify or set xattr
  1045. * "trusted.overlay.upper" to indicate that index may have
  1046. * directory entries.
  1047. */
  1048. if (ovl_check_origin_xattr(ofs->indexdir)) {
  1049. err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
  1050. upperpath->dentry, true, false);
  1051. if (err)
  1052. pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
  1053. }
  1054. err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
  1055. if (err)
  1056. pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
  1057. /* Cleanup bad/stale/orphan index entries */
  1058. if (!err)
  1059. err = ovl_indexdir_cleanup(ofs);
  1060. }
  1061. if (err || !ofs->indexdir)
  1062. pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
  1063. out:
  1064. mnt_drop_write(mnt);
  1065. return err;
  1066. }
  1067. /* Get a unique fsid for the layer */
  1068. static int ovl_get_fsid(struct ovl_fs *ofs, struct super_block *sb)
  1069. {
  1070. unsigned int i;
  1071. dev_t dev;
  1072. int err;
  1073. /* fsid 0 is reserved for upper fs even with non upper overlay */
  1074. if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
  1075. return 0;
  1076. for (i = 0; i < ofs->numlowerfs; i++) {
  1077. if (ofs->lower_fs[i].sb == sb)
  1078. return i + 1;
  1079. }
  1080. err = get_anon_bdev(&dev);
  1081. if (err) {
  1082. pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
  1083. return err;
  1084. }
  1085. ofs->lower_fs[ofs->numlowerfs].sb = sb;
  1086. ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
  1087. ofs->numlowerfs++;
  1088. return ofs->numlowerfs;
  1089. }
  1090. static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs,
  1091. struct path *stack, unsigned int numlower)
  1092. {
  1093. int err;
  1094. unsigned int i;
  1095. err = -ENOMEM;
  1096. ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
  1097. GFP_KERNEL);
  1098. if (ofs->lower_layers == NULL)
  1099. goto out;
  1100. ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
  1101. GFP_KERNEL);
  1102. if (ofs->lower_fs == NULL)
  1103. goto out;
  1104. for (i = 0; i < numlower; i++) {
  1105. struct vfsmount *mnt;
  1106. struct inode *trap;
  1107. int fsid;
  1108. err = fsid = ovl_get_fsid(ofs, stack[i].mnt->mnt_sb);
  1109. if (err < 0)
  1110. goto out;
  1111. err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
  1112. if (err)
  1113. goto out;
  1114. if (ovl_is_inuse(stack[i].dentry)) {
  1115. err = ovl_report_in_use(ofs, "lowerdir");
  1116. if (err)
  1117. goto out;
  1118. }
  1119. mnt = clone_private_mount(&stack[i]);
  1120. err = PTR_ERR(mnt);
  1121. if (IS_ERR(mnt)) {
  1122. pr_err("overlayfs: failed to clone lowerpath\n");
  1123. iput(trap);
  1124. goto out;
  1125. }
  1126. /*
  1127. * Make lower layers R/O. That way fchmod/fchown on lower file
  1128. * will fail instead of modifying lower fs.
  1129. */
  1130. mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
  1131. ofs->lower_layers[ofs->numlower].trap = trap;
  1132. ofs->lower_layers[ofs->numlower].mnt = mnt;
  1133. ofs->lower_layers[ofs->numlower].idx = i + 1;
  1134. ofs->lower_layers[ofs->numlower].fsid = fsid;
  1135. if (fsid) {
  1136. ofs->lower_layers[ofs->numlower].fs =
  1137. &ofs->lower_fs[fsid - 1];
  1138. }
  1139. ofs->numlower++;
  1140. }
  1141. /*
  1142. * When all layers on same fs, overlay can use real inode numbers.
  1143. * With mount option "xino=on", mounter declares that there are enough
  1144. * free high bits in underlying fs to hold the unique fsid.
  1145. * If overlayfs does encounter underlying inodes using the high xino
  1146. * bits reserved for fsid, it emits a warning and uses the original
  1147. * inode number.
  1148. */
  1149. if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
  1150. ofs->xino_bits = 0;
  1151. ofs->config.xino = OVL_XINO_OFF;
  1152. } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
  1153. /*
  1154. * This is a roundup of number of bits needed for numlowerfs+1
  1155. * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
  1156. * upper fs even with non upper overlay.
  1157. */
  1158. BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
  1159. ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
  1160. }
  1161. if (ofs->xino_bits) {
  1162. pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
  1163. ofs->xino_bits);
  1164. }
  1165. err = 0;
  1166. out:
  1167. return err;
  1168. }
  1169. static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
  1170. struct ovl_fs *ofs)
  1171. {
  1172. int err;
  1173. char *lowertmp, *lower;
  1174. struct path *stack = NULL;
  1175. unsigned int stacklen, numlower = 0, i;
  1176. bool remote = false;
  1177. struct ovl_entry *oe;
  1178. err = -ENOMEM;
  1179. lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
  1180. if (!lowertmp)
  1181. goto out_err;
  1182. err = -EINVAL;
  1183. stacklen = ovl_split_lowerdirs(lowertmp);
  1184. if (stacklen > OVL_MAX_STACK) {
  1185. pr_err("overlayfs: too many lower directories, limit is %d\n",
  1186. OVL_MAX_STACK);
  1187. goto out_err;
  1188. } else if (!ofs->config.upperdir && stacklen == 1) {
  1189. pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
  1190. goto out_err;
  1191. } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
  1192. ofs->config.redirect_follow) {
  1193. pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
  1194. ofs->config.nfs_export = false;
  1195. }
  1196. err = -ENOMEM;
  1197. stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
  1198. if (!stack)
  1199. goto out_err;
  1200. err = -EINVAL;
  1201. lower = lowertmp;
  1202. for (numlower = 0; numlower < stacklen; numlower++) {
  1203. err = ovl_lower_dir(lower, &stack[numlower], ofs,
  1204. &sb->s_stack_depth, &remote);
  1205. if (err)
  1206. goto out_err;
  1207. lower = strchr(lower, '\0') + 1;
  1208. }
  1209. err = -EINVAL;
  1210. sb->s_stack_depth++;
  1211. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  1212. pr_err("overlayfs: maximum fs stacking depth exceeded\n");
  1213. goto out_err;
  1214. }
  1215. err = ovl_get_lower_layers(sb, ofs, stack, numlower);
  1216. if (err)
  1217. goto out_err;
  1218. err = -ENOMEM;
  1219. oe = ovl_alloc_entry(numlower);
  1220. if (!oe)
  1221. goto out_err;
  1222. for (i = 0; i < numlower; i++) {
  1223. oe->lowerstack[i].dentry = dget(stack[i].dentry);
  1224. oe->lowerstack[i].layer = &ofs->lower_layers[i];
  1225. }
  1226. if (remote)
  1227. sb->s_d_op = &ovl_reval_dentry_operations;
  1228. else
  1229. sb->s_d_op = &ovl_dentry_operations;
  1230. out:
  1231. for (i = 0; i < numlower; i++)
  1232. path_put(&stack[i]);
  1233. kfree(stack);
  1234. kfree(lowertmp);
  1235. return oe;
  1236. out_err:
  1237. oe = ERR_PTR(err);
  1238. goto out;
  1239. }
  1240. /*
  1241. * Check if this layer root is a descendant of:
  1242. * - another layer of this overlayfs instance
  1243. * - upper/work dir of any overlayfs instance
  1244. */
  1245. static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
  1246. struct dentry *dentry, const char *name)
  1247. {
  1248. struct dentry *next = dentry, *parent;
  1249. int err = 0;
  1250. if (!dentry)
  1251. return 0;
  1252. parent = dget_parent(next);
  1253. /* Walk back ancestors to root (inclusive) looking for traps */
  1254. while (!err && parent != next) {
  1255. if (ovl_lookup_trap_inode(sb, parent)) {
  1256. err = -ELOOP;
  1257. pr_err("overlayfs: overlapping %s path\n", name);
  1258. } else if (ovl_is_inuse(parent)) {
  1259. err = ovl_report_in_use(ofs, name);
  1260. }
  1261. next = parent;
  1262. parent = dget_parent(next);
  1263. dput(next);
  1264. }
  1265. dput(parent);
  1266. return err;
  1267. }
  1268. /*
  1269. * Check if any of the layers or work dirs overlap.
  1270. */
  1271. static int ovl_check_overlapping_layers(struct super_block *sb,
  1272. struct ovl_fs *ofs)
  1273. {
  1274. int i, err;
  1275. if (ofs->upper_mnt) {
  1276. err = ovl_check_layer(sb, ofs, ofs->upper_mnt->mnt_root,
  1277. "upperdir");
  1278. if (err)
  1279. return err;
  1280. /*
  1281. * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
  1282. * this instance and covers overlapping work and index dirs,
  1283. * unless work or index dir have been moved since created inside
  1284. * workbasedir. In that case, we already have their traps in
  1285. * inode cache and we will catch that case on lookup.
  1286. */
  1287. err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir");
  1288. if (err)
  1289. return err;
  1290. }
  1291. for (i = 0; i < ofs->numlower; i++) {
  1292. err = ovl_check_layer(sb, ofs,
  1293. ofs->lower_layers[i].mnt->mnt_root,
  1294. "lowerdir");
  1295. if (err)
  1296. return err;
  1297. }
  1298. return 0;
  1299. }
  1300. static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  1301. {
  1302. struct path upperpath = { };
  1303. struct dentry *root_dentry;
  1304. struct ovl_entry *oe;
  1305. struct ovl_fs *ofs;
  1306. struct cred *cred;
  1307. int err;
  1308. err = -ENOMEM;
  1309. ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  1310. if (!ofs)
  1311. goto out;
  1312. ofs->creator_cred = cred = prepare_creds();
  1313. if (!cred)
  1314. goto out_err;
  1315. ofs->config.index = ovl_index_def;
  1316. ofs->config.nfs_export = ovl_nfs_export_def;
  1317. ofs->config.xino = ovl_xino_def();
  1318. ofs->config.metacopy = ovl_metacopy_def;
  1319. err = ovl_parse_opt((char *) data, &ofs->config);
  1320. if (err)
  1321. goto out_err;
  1322. err = -EINVAL;
  1323. if (!ofs->config.lowerdir) {
  1324. if (!silent)
  1325. pr_err("overlayfs: missing 'lowerdir'\n");
  1326. goto out_err;
  1327. }
  1328. sb->s_stack_depth = 0;
  1329. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1330. /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
  1331. if (ofs->config.xino != OVL_XINO_OFF)
  1332. ofs->xino_bits = BITS_PER_LONG - 32;
  1333. /* alloc/destroy_inode needed for setting up traps in inode cache */
  1334. sb->s_op = &ovl_super_operations;
  1335. if (ofs->config.upperdir) {
  1336. if (!ofs->config.workdir) {
  1337. pr_err("overlayfs: missing 'workdir'\n");
  1338. goto out_err;
  1339. }
  1340. err = ovl_get_upper(sb, ofs, &upperpath);
  1341. if (err)
  1342. goto out_err;
  1343. err = ovl_get_workdir(sb, ofs, &upperpath);
  1344. if (err)
  1345. goto out_err;
  1346. if (!ofs->workdir)
  1347. sb->s_flags |= SB_RDONLY;
  1348. sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
  1349. sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
  1350. }
  1351. oe = ovl_get_lowerstack(sb, ofs);
  1352. err = PTR_ERR(oe);
  1353. if (IS_ERR(oe))
  1354. goto out_err;
  1355. /* If the upper fs is nonexistent, we mark overlayfs r/o too */
  1356. if (!ofs->upper_mnt)
  1357. sb->s_flags |= SB_RDONLY;
  1358. if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
  1359. err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
  1360. if (err)
  1361. goto out_free_oe;
  1362. /* Force r/o mount with no index dir */
  1363. if (!ofs->indexdir) {
  1364. dput(ofs->workdir);
  1365. ofs->workdir = NULL;
  1366. sb->s_flags |= SB_RDONLY;
  1367. }
  1368. }
  1369. err = ovl_check_overlapping_layers(sb, ofs);
  1370. if (err)
  1371. goto out_free_oe;
  1372. /* Show index=off in /proc/mounts for forced r/o mount */
  1373. if (!ofs->indexdir) {
  1374. ofs->config.index = false;
  1375. if (ofs->upper_mnt && ofs->config.nfs_export) {
  1376. pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
  1377. ofs->config.nfs_export = false;
  1378. }
  1379. }
  1380. if (ofs->config.metacopy && ofs->config.nfs_export) {
  1381. pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
  1382. ofs->config.nfs_export = false;
  1383. }
  1384. if (ofs->config.nfs_export)
  1385. sb->s_export_op = &ovl_export_operations;
  1386. /* Never override disk quota limits or use reserved space */
  1387. cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
  1388. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  1389. sb->s_xattr = ovl_xattr_handlers;
  1390. sb->s_fs_info = ofs;
  1391. sb->s_flags |= SB_POSIXACL;
  1392. err = -ENOMEM;
  1393. root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
  1394. if (!root_dentry)
  1395. goto out_free_oe;
  1396. root_dentry->d_fsdata = oe;
  1397. mntput(upperpath.mnt);
  1398. if (upperpath.dentry) {
  1399. ovl_dentry_set_upper_alias(root_dentry);
  1400. if (ovl_is_impuredir(upperpath.dentry))
  1401. ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
  1402. }
  1403. /* Root is always merge -> can have whiteouts */
  1404. ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
  1405. ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
  1406. ovl_set_upperdata(d_inode(root_dentry));
  1407. ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
  1408. ovl_dentry_lower(root_dentry), NULL);
  1409. sb->s_root = root_dentry;
  1410. return 0;
  1411. out_free_oe:
  1412. ovl_entry_stack_free(oe);
  1413. kfree(oe);
  1414. out_err:
  1415. path_put(&upperpath);
  1416. ovl_free_fs(ofs);
  1417. out:
  1418. return err;
  1419. }
  1420. static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  1421. const char *dev_name, void *raw_data)
  1422. {
  1423. return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  1424. }
  1425. static struct file_system_type ovl_fs_type = {
  1426. .owner = THIS_MODULE,
  1427. .name = "overlay",
  1428. .mount = ovl_mount,
  1429. .kill_sb = kill_anon_super,
  1430. };
  1431. MODULE_ALIAS_FS("overlay");
  1432. static void ovl_inode_init_once(void *foo)
  1433. {
  1434. struct ovl_inode *oi = foo;
  1435. inode_init_once(&oi->vfs_inode);
  1436. }
  1437. static int __init ovl_init(void)
  1438. {
  1439. int err;
  1440. ovl_inode_cachep = kmem_cache_create("ovl_inode",
  1441. sizeof(struct ovl_inode), 0,
  1442. (SLAB_RECLAIM_ACCOUNT|
  1443. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1444. ovl_inode_init_once);
  1445. if (ovl_inode_cachep == NULL)
  1446. return -ENOMEM;
  1447. if (unlikely(ovl_permit_mounts_in_userns)) {
  1448. pr_warn("overlayfs: Allowing overlay mounts in user namespaces bears security risks\n");
  1449. ovl_fs_type.fs_flags |= FS_USERNS_MOUNT;
  1450. }
  1451. err = register_filesystem(&ovl_fs_type);
  1452. if (err)
  1453. kmem_cache_destroy(ovl_inode_cachep);
  1454. return err;
  1455. }
  1456. static void __exit ovl_exit(void)
  1457. {
  1458. unregister_filesystem(&ovl_fs_type);
  1459. /*
  1460. * Make sure all delayed rcu free inodes are flushed before we
  1461. * destroy cache.
  1462. */
  1463. rcu_barrier();
  1464. kmem_cache_destroy(ovl_inode_cachep);
  1465. }
  1466. module_init(ovl_init);
  1467. module_exit(ovl_exit);