super.c 42 KB

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