super.c 39 KB

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