super.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2011 Novell Inc.
  5. */
  6. #include <uapi/linux/magic.h>
  7. #include <linux/fs.h>
  8. #include <linux/namei.h>
  9. #include <linux/xattr.h>
  10. #include <linux/mount.h>
  11. #include <linux/parser.h>
  12. #include <linux/module.h>
  13. #include <linux/statfs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/posix_acl_xattr.h>
  16. #include <linux/exportfs.h>
  17. #include <linux/file.h>
  18. #include <linux/fs_context.h>
  19. #include <linux/fs_parser.h>
  20. #include "overlayfs.h"
  21. #include "params.h"
  22. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  23. MODULE_DESCRIPTION("Overlay filesystem");
  24. MODULE_LICENSE("GPL");
  25. struct ovl_dir_cache;
  26. static struct dentry *ovl_d_real(struct dentry *dentry, enum d_real_type type)
  27. {
  28. struct dentry *upper, *lower;
  29. int err;
  30. switch (type) {
  31. case D_REAL_DATA:
  32. case D_REAL_METADATA:
  33. break;
  34. default:
  35. goto bug;
  36. }
  37. if (!d_is_reg(dentry)) {
  38. /* d_real_inode() is only relevant for regular files */
  39. return dentry;
  40. }
  41. upper = ovl_dentry_upper(dentry);
  42. if (upper && (type == D_REAL_METADATA ||
  43. ovl_has_upperdata(d_inode(dentry))))
  44. return upper;
  45. if (type == D_REAL_METADATA) {
  46. lower = ovl_dentry_lower(dentry);
  47. goto real_lower;
  48. }
  49. /*
  50. * Best effort lazy lookup of lowerdata for D_REAL_DATA case to return
  51. * the real lowerdata dentry. The only current caller of d_real() with
  52. * D_REAL_DATA is d_real_inode() from trace_uprobe and this caller is
  53. * likely going to be followed reading from the file, before placing
  54. * uprobes on offset within the file, so lowerdata should be available
  55. * when setting the uprobe.
  56. */
  57. err = ovl_verify_lowerdata(dentry);
  58. if (err)
  59. goto bug;
  60. lower = ovl_dentry_lowerdata(dentry);
  61. if (!lower)
  62. goto bug;
  63. real_lower:
  64. /* Handle recursion into stacked lower fs */
  65. return d_real(lower, type);
  66. bug:
  67. WARN(1, "%s(%pd4, %d): real dentry not found\n", __func__, dentry, type);
  68. return dentry;
  69. }
  70. static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
  71. {
  72. int ret = 1;
  73. if (!d)
  74. return 1;
  75. if (weak) {
  76. if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
  77. ret = d->d_op->d_weak_revalidate(d, flags);
  78. } else if (d->d_flags & DCACHE_OP_REVALIDATE) {
  79. ret = d->d_op->d_revalidate(d, flags);
  80. if (!ret) {
  81. if (!(flags & LOOKUP_RCU))
  82. d_invalidate(d);
  83. ret = -ESTALE;
  84. }
  85. }
  86. return ret;
  87. }
  88. static int ovl_dentry_revalidate_common(struct dentry *dentry,
  89. unsigned int flags, bool weak)
  90. {
  91. struct ovl_entry *oe;
  92. struct ovl_path *lowerstack;
  93. struct inode *inode = d_inode_rcu(dentry);
  94. struct dentry *upper;
  95. unsigned int i;
  96. int ret = 1;
  97. /* Careful in RCU mode */
  98. if (!inode)
  99. return -ECHILD;
  100. oe = OVL_I_E(inode);
  101. lowerstack = ovl_lowerstack(oe);
  102. upper = ovl_i_dentry_upper(inode);
  103. if (upper)
  104. ret = ovl_revalidate_real(upper, flags, weak);
  105. for (i = 0; ret > 0 && i < ovl_numlower(oe); i++)
  106. ret = ovl_revalidate_real(lowerstack[i].dentry, flags, weak);
  107. return ret;
  108. }
  109. static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
  110. {
  111. return ovl_dentry_revalidate_common(dentry, flags, false);
  112. }
  113. static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
  114. {
  115. return ovl_dentry_revalidate_common(dentry, flags, true);
  116. }
  117. static const struct dentry_operations ovl_dentry_operations = {
  118. .d_real = ovl_d_real,
  119. .d_revalidate = ovl_dentry_revalidate,
  120. .d_weak_revalidate = ovl_dentry_weak_revalidate,
  121. };
  122. static struct kmem_cache *ovl_inode_cachep;
  123. static struct inode *ovl_alloc_inode(struct super_block *sb)
  124. {
  125. struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL);
  126. if (!oi)
  127. return NULL;
  128. oi->cache = NULL;
  129. oi->redirect = NULL;
  130. oi->version = 0;
  131. oi->flags = 0;
  132. oi->__upperdentry = NULL;
  133. oi->lowerdata_redirect = NULL;
  134. oi->oe = NULL;
  135. mutex_init(&oi->lock);
  136. return &oi->vfs_inode;
  137. }
  138. static void ovl_free_inode(struct inode *inode)
  139. {
  140. struct ovl_inode *oi = OVL_I(inode);
  141. kfree(oi->redirect);
  142. kfree(oi->oe);
  143. mutex_destroy(&oi->lock);
  144. kmem_cache_free(ovl_inode_cachep, oi);
  145. }
  146. static void ovl_destroy_inode(struct inode *inode)
  147. {
  148. struct ovl_inode *oi = OVL_I(inode);
  149. dput(oi->__upperdentry);
  150. ovl_stack_put(ovl_lowerstack(oi->oe), ovl_numlower(oi->oe));
  151. if (S_ISDIR(inode->i_mode))
  152. ovl_dir_cache_free(inode);
  153. else
  154. kfree(oi->lowerdata_redirect);
  155. }
  156. static void ovl_put_super(struct super_block *sb)
  157. {
  158. struct ovl_fs *ofs = OVL_FS(sb);
  159. if (ofs)
  160. ovl_free_fs(ofs);
  161. }
  162. /* Sync real dirty inodes in upper filesystem (if it exists) */
  163. static int ovl_sync_fs(struct super_block *sb, int wait)
  164. {
  165. struct ovl_fs *ofs = OVL_FS(sb);
  166. struct super_block *upper_sb;
  167. int ret;
  168. ret = ovl_sync_status(ofs);
  169. if (ret < 0)
  170. return -EIO;
  171. if (!ret)
  172. return ret;
  173. /*
  174. * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
  175. * All the super blocks will be iterated, including upper_sb.
  176. *
  177. * If this is a syncfs(2) call, then we do need to call
  178. * sync_filesystem() on upper_sb, but enough if we do it when being
  179. * called with wait == 1.
  180. */
  181. if (!wait)
  182. return 0;
  183. upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
  184. down_read(&upper_sb->s_umount);
  185. ret = sync_filesystem(upper_sb);
  186. up_read(&upper_sb->s_umount);
  187. return ret;
  188. }
  189. /**
  190. * ovl_statfs
  191. * @dentry: The dentry to query
  192. * @buf: The struct kstatfs to fill in with stats
  193. *
  194. * Get the filesystem statistics. As writes always target the upper layer
  195. * filesystem pass the statfs to the upper filesystem (if it exists)
  196. */
  197. static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  198. {
  199. struct super_block *sb = dentry->d_sb;
  200. struct ovl_fs *ofs = OVL_FS(sb);
  201. struct dentry *root_dentry = sb->s_root;
  202. struct path path;
  203. int err;
  204. ovl_path_real(root_dentry, &path);
  205. err = vfs_statfs(&path, buf);
  206. if (!err) {
  207. buf->f_namelen = ofs->namelen;
  208. buf->f_type = OVERLAYFS_SUPER_MAGIC;
  209. if (ovl_has_fsid(ofs))
  210. buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
  211. }
  212. return err;
  213. }
  214. static const struct super_operations ovl_super_operations = {
  215. .alloc_inode = ovl_alloc_inode,
  216. .free_inode = ovl_free_inode,
  217. .destroy_inode = ovl_destroy_inode,
  218. .drop_inode = generic_delete_inode,
  219. .put_super = ovl_put_super,
  220. .sync_fs = ovl_sync_fs,
  221. .statfs = ovl_statfs,
  222. .show_options = ovl_show_options,
  223. };
  224. #define OVL_WORKDIR_NAME "work"
  225. #define OVL_INDEXDIR_NAME "index"
  226. static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
  227. const char *name, bool persist)
  228. {
  229. struct inode *dir = ofs->workbasedir->d_inode;
  230. struct vfsmount *mnt = ovl_upper_mnt(ofs);
  231. struct dentry *work;
  232. int err;
  233. bool retried = false;
  234. inode_lock_nested(dir, I_MUTEX_PARENT);
  235. retry:
  236. work = ovl_lookup_upper(ofs, name, ofs->workbasedir, strlen(name));
  237. if (!IS_ERR(work)) {
  238. struct iattr attr = {
  239. .ia_valid = ATTR_MODE,
  240. .ia_mode = S_IFDIR | 0,
  241. };
  242. if (work->d_inode) {
  243. err = -EEXIST;
  244. if (retried)
  245. goto out_dput;
  246. if (persist)
  247. goto out_unlock;
  248. retried = true;
  249. err = ovl_workdir_cleanup(ofs, dir, mnt, work, 0);
  250. dput(work);
  251. if (err == -EINVAL) {
  252. work = ERR_PTR(err);
  253. goto out_unlock;
  254. }
  255. goto retry;
  256. }
  257. err = ovl_mkdir_real(ofs, dir, &work, attr.ia_mode);
  258. if (err)
  259. goto out_dput;
  260. /* Weird filesystem returning with hashed negative (kernfs)? */
  261. err = -EINVAL;
  262. if (d_really_is_negative(work))
  263. goto out_dput;
  264. /*
  265. * Try to remove POSIX ACL xattrs from workdir. We are good if:
  266. *
  267. * a) success (there was a POSIX ACL xattr and was removed)
  268. * b) -ENODATA (there was no POSIX ACL xattr)
  269. * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
  270. *
  271. * There are various other error values that could effectively
  272. * mean that the xattr doesn't exist (e.g. -ERANGE is returned
  273. * if the xattr name is too long), but the set of filesystems
  274. * allowed as upper are limited to "normal" ones, where checking
  275. * for the above two errors is sufficient.
  276. */
  277. err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_DEFAULT);
  278. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  279. goto out_dput;
  280. err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_ACCESS);
  281. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  282. goto out_dput;
  283. /* Clear any inherited mode bits */
  284. inode_lock(work->d_inode);
  285. err = ovl_do_notify_change(ofs, work, &attr);
  286. inode_unlock(work->d_inode);
  287. if (err)
  288. goto out_dput;
  289. } else {
  290. err = PTR_ERR(work);
  291. goto out_err;
  292. }
  293. out_unlock:
  294. inode_unlock(dir);
  295. return work;
  296. out_dput:
  297. dput(work);
  298. out_err:
  299. pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
  300. ofs->config.workdir, name, -err);
  301. work = NULL;
  302. goto out_unlock;
  303. }
  304. static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs,
  305. const char *name)
  306. {
  307. struct kstatfs statfs;
  308. int err = vfs_statfs(path, &statfs);
  309. if (err)
  310. pr_err("statfs failed on '%s'\n", name);
  311. else
  312. ofs->namelen = max(ofs->namelen, statfs.f_namelen);
  313. return err;
  314. }
  315. static int ovl_lower_dir(const char *name, struct path *path,
  316. struct ovl_fs *ofs, int *stack_depth)
  317. {
  318. int fh_type;
  319. int err;
  320. err = ovl_check_namelen(path, ofs, name);
  321. if (err)
  322. return err;
  323. *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
  324. /*
  325. * The inodes index feature and NFS export need to encode and decode
  326. * file handles, so they require that all layers support them.
  327. */
  328. fh_type = ovl_can_decode_fh(path->dentry->d_sb);
  329. if ((ofs->config.nfs_export ||
  330. (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
  331. ofs->config.index = false;
  332. ofs->config.nfs_export = false;
  333. pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
  334. name);
  335. }
  336. ofs->nofh |= !fh_type;
  337. /*
  338. * Decoding origin file handle is required for persistent st_ino.
  339. * Without persistent st_ino, xino=auto falls back to xino=off.
  340. */
  341. if (ofs->config.xino == OVL_XINO_AUTO &&
  342. ofs->config.upperdir && !fh_type) {
  343. ofs->config.xino = OVL_XINO_OFF;
  344. pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n",
  345. name);
  346. }
  347. /* Check if lower fs has 32bit inode numbers */
  348. if (fh_type != FILEID_INO32_GEN)
  349. ofs->xino_mode = -1;
  350. return 0;
  351. }
  352. /* Workdir should not be subdir of upperdir and vice versa */
  353. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  354. {
  355. bool ok = false;
  356. if (workdir != upperdir) {
  357. struct dentry *trap = lock_rename(workdir, upperdir);
  358. if (!IS_ERR(trap))
  359. unlock_rename(workdir, upperdir);
  360. ok = (trap == NULL);
  361. }
  362. return ok;
  363. }
  364. static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
  365. struct inode **ptrap, const char *name)
  366. {
  367. struct inode *trap;
  368. int err;
  369. trap = ovl_get_trap_inode(sb, dir);
  370. err = PTR_ERR_OR_ZERO(trap);
  371. if (err) {
  372. if (err == -ELOOP)
  373. pr_err("conflicting %s path\n", name);
  374. return err;
  375. }
  376. *ptrap = trap;
  377. return 0;
  378. }
  379. /*
  380. * Determine how we treat concurrent use of upperdir/workdir based on the
  381. * index feature. This is papering over mount leaks of container runtimes,
  382. * for example, an old overlay mount is leaked and now its upperdir is
  383. * attempted to be used as a lower layer in a new overlay mount.
  384. */
  385. static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
  386. {
  387. if (ofs->config.index) {
  388. pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
  389. name);
  390. return -EBUSY;
  391. } else {
  392. pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
  393. name);
  394. return 0;
  395. }
  396. }
  397. static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
  398. struct ovl_layer *upper_layer,
  399. const struct path *upperpath)
  400. {
  401. struct vfsmount *upper_mnt;
  402. int err;
  403. /* Upperdir path should not be r/o */
  404. if (__mnt_is_readonly(upperpath->mnt)) {
  405. pr_err("upper fs is r/o, try multi-lower layers mount\n");
  406. err = -EINVAL;
  407. goto out;
  408. }
  409. err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
  410. if (err)
  411. goto out;
  412. err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
  413. "upperdir");
  414. if (err)
  415. goto out;
  416. upper_mnt = clone_private_mount(upperpath);
  417. err = PTR_ERR(upper_mnt);
  418. if (IS_ERR(upper_mnt)) {
  419. pr_err("failed to clone upperpath\n");
  420. goto out;
  421. }
  422. /* Don't inherit atime flags */
  423. upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
  424. upper_layer->mnt = upper_mnt;
  425. upper_layer->idx = 0;
  426. upper_layer->fsid = 0;
  427. /*
  428. * Inherit SB_NOSEC flag from upperdir.
  429. *
  430. * This optimization changes behavior when a security related attribute
  431. * (suid/sgid/security.*) is changed on an underlying layer. This is
  432. * okay because we don't yet have guarantees in that case, but it will
  433. * need careful treatment once we want to honour changes to underlying
  434. * filesystems.
  435. */
  436. if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
  437. sb->s_flags |= SB_NOSEC;
  438. if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
  439. ofs->upperdir_locked = true;
  440. } else {
  441. err = ovl_report_in_use(ofs, "upperdir");
  442. if (err)
  443. goto out;
  444. }
  445. err = 0;
  446. out:
  447. return err;
  448. }
  449. /*
  450. * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
  451. * negative values if error is encountered.
  452. */
  453. static int ovl_check_rename_whiteout(struct ovl_fs *ofs)
  454. {
  455. struct dentry *workdir = ofs->workdir;
  456. struct inode *dir = d_inode(workdir);
  457. struct dentry *temp;
  458. struct dentry *dest;
  459. struct dentry *whiteout;
  460. struct name_snapshot name;
  461. int err;
  462. inode_lock_nested(dir, I_MUTEX_PARENT);
  463. temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0));
  464. err = PTR_ERR(temp);
  465. if (IS_ERR(temp))
  466. goto out_unlock;
  467. dest = ovl_lookup_temp(ofs, workdir);
  468. err = PTR_ERR(dest);
  469. if (IS_ERR(dest)) {
  470. dput(temp);
  471. goto out_unlock;
  472. }
  473. /* Name is inline and stable - using snapshot as a copy helper */
  474. take_dentry_name_snapshot(&name, temp);
  475. err = ovl_do_rename(ofs, dir, temp, dir, dest, RENAME_WHITEOUT);
  476. if (err) {
  477. if (err == -EINVAL)
  478. err = 0;
  479. goto cleanup_temp;
  480. }
  481. whiteout = ovl_lookup_upper(ofs, name.name.name, workdir, name.name.len);
  482. err = PTR_ERR(whiteout);
  483. if (IS_ERR(whiteout))
  484. goto cleanup_temp;
  485. err = ovl_upper_is_whiteout(ofs, whiteout);
  486. /* Best effort cleanup of whiteout and temp file */
  487. if (err)
  488. ovl_cleanup(ofs, dir, whiteout);
  489. dput(whiteout);
  490. cleanup_temp:
  491. ovl_cleanup(ofs, dir, temp);
  492. release_dentry_name_snapshot(&name);
  493. dput(temp);
  494. dput(dest);
  495. out_unlock:
  496. inode_unlock(dir);
  497. return err;
  498. }
  499. static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
  500. struct dentry *parent,
  501. const char *name, umode_t mode)
  502. {
  503. size_t len = strlen(name);
  504. struct dentry *child;
  505. inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
  506. child = ovl_lookup_upper(ofs, name, parent, len);
  507. if (!IS_ERR(child) && !child->d_inode)
  508. child = ovl_create_real(ofs, parent->d_inode, child,
  509. OVL_CATTR(mode));
  510. inode_unlock(parent->d_inode);
  511. dput(parent);
  512. return child;
  513. }
  514. /*
  515. * Creates $workdir/work/incompat/volatile/dirty file if it is not already
  516. * present.
  517. */
  518. static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
  519. {
  520. unsigned int ctr;
  521. struct dentry *d = dget(ofs->workbasedir);
  522. static const char *const volatile_path[] = {
  523. OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
  524. };
  525. const char *const *name = volatile_path;
  526. for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
  527. d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
  528. if (IS_ERR(d))
  529. return PTR_ERR(d);
  530. }
  531. dput(d);
  532. return 0;
  533. }
  534. static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
  535. const struct path *workpath)
  536. {
  537. struct vfsmount *mnt = ovl_upper_mnt(ofs);
  538. struct dentry *workdir;
  539. struct file *tmpfile;
  540. bool rename_whiteout;
  541. bool d_type;
  542. int fh_type;
  543. int err;
  544. err = mnt_want_write(mnt);
  545. if (err)
  546. return err;
  547. workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
  548. err = PTR_ERR(workdir);
  549. if (IS_ERR_OR_NULL(workdir))
  550. goto out;
  551. ofs->workdir = workdir;
  552. err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
  553. if (err)
  554. goto out;
  555. /*
  556. * Upper should support d_type, else whiteouts are visible. Given
  557. * workdir and upper are on same fs, we can do iterate_dir() on
  558. * workdir. This check requires successful creation of workdir in
  559. * previous step.
  560. */
  561. err = ovl_check_d_type_supported(workpath);
  562. if (err < 0)
  563. goto out;
  564. d_type = err;
  565. if (!d_type)
  566. pr_warn("upper fs needs to support d_type.\n");
  567. /* Check if upper/work fs supports O_TMPFILE */
  568. tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0);
  569. ofs->tmpfile = !IS_ERR(tmpfile);
  570. if (ofs->tmpfile)
  571. fput(tmpfile);
  572. else
  573. pr_warn("upper fs does not support tmpfile.\n");
  574. /* Check if upper/work fs supports RENAME_WHITEOUT */
  575. err = ovl_check_rename_whiteout(ofs);
  576. if (err < 0)
  577. goto out;
  578. rename_whiteout = err;
  579. if (!rename_whiteout)
  580. pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
  581. /*
  582. * Check if upper/work fs supports (trusted|user).overlay.* xattr
  583. */
  584. err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
  585. if (err) {
  586. pr_warn("failed to set xattr on upper\n");
  587. ofs->noxattr = true;
  588. if (ovl_redirect_follow(ofs)) {
  589. ofs->config.redirect_mode = OVL_REDIRECT_NOFOLLOW;
  590. pr_warn("...falling back to redirect_dir=nofollow.\n");
  591. }
  592. if (ofs->config.metacopy) {
  593. ofs->config.metacopy = false;
  594. pr_warn("...falling back to metacopy=off.\n");
  595. }
  596. if (ofs->config.index) {
  597. ofs->config.index = false;
  598. pr_warn("...falling back to index=off.\n");
  599. }
  600. if (ovl_has_fsid(ofs)) {
  601. ofs->config.uuid = OVL_UUID_NULL;
  602. pr_warn("...falling back to uuid=null.\n");
  603. }
  604. /*
  605. * xattr support is required for persistent st_ino.
  606. * Without persistent st_ino, xino=auto falls back to xino=off.
  607. */
  608. if (ofs->config.xino == OVL_XINO_AUTO) {
  609. ofs->config.xino = OVL_XINO_OFF;
  610. pr_warn("...falling back to xino=off.\n");
  611. }
  612. if (err == -EPERM && !ofs->config.userxattr)
  613. pr_info("try mounting with 'userxattr' option\n");
  614. err = 0;
  615. } else {
  616. ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
  617. }
  618. /*
  619. * We allowed sub-optimal upper fs configuration and don't want to break
  620. * users over kernel upgrade, but we never allowed remote upper fs, so
  621. * we can enforce strict requirements for remote upper fs.
  622. */
  623. if (ovl_dentry_remote(ofs->workdir) &&
  624. (!d_type || !rename_whiteout || ofs->noxattr)) {
  625. pr_err("upper fs missing required features.\n");
  626. err = -EINVAL;
  627. goto out;
  628. }
  629. /*
  630. * For volatile mount, create a incompat/volatile/dirty file to keep
  631. * track of it.
  632. */
  633. if (ofs->config.ovl_volatile) {
  634. err = ovl_create_volatile_dirty(ofs);
  635. if (err < 0) {
  636. pr_err("Failed to create volatile/dirty file.\n");
  637. goto out;
  638. }
  639. }
  640. /* Check if upper/work fs supports file handles */
  641. fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
  642. if (ofs->config.index && !fh_type) {
  643. ofs->config.index = false;
  644. pr_warn("upper fs does not support file handles, falling back to index=off.\n");
  645. }
  646. ofs->nofh |= !fh_type;
  647. /* Check if upper fs has 32bit inode numbers */
  648. if (fh_type != FILEID_INO32_GEN)
  649. ofs->xino_mode = -1;
  650. /* NFS export of r/w mount depends on index */
  651. if (ofs->config.nfs_export && !ofs->config.index) {
  652. pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
  653. ofs->config.nfs_export = false;
  654. }
  655. out:
  656. mnt_drop_write(mnt);
  657. return err;
  658. }
  659. static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
  660. const struct path *upperpath,
  661. const struct path *workpath)
  662. {
  663. int err;
  664. err = -EINVAL;
  665. if (upperpath->mnt != workpath->mnt) {
  666. pr_err("workdir and upperdir must reside under the same mount\n");
  667. return err;
  668. }
  669. if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) {
  670. pr_err("workdir and upperdir must be separate subtrees\n");
  671. return err;
  672. }
  673. ofs->workbasedir = dget(workpath->dentry);
  674. if (ovl_inuse_trylock(ofs->workbasedir)) {
  675. ofs->workdir_locked = true;
  676. } else {
  677. err = ovl_report_in_use(ofs, "workdir");
  678. if (err)
  679. return err;
  680. }
  681. err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
  682. "workdir");
  683. if (err)
  684. return err;
  685. return ovl_make_workdir(sb, ofs, workpath);
  686. }
  687. static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
  688. struct ovl_entry *oe, const struct path *upperpath)
  689. {
  690. struct vfsmount *mnt = ovl_upper_mnt(ofs);
  691. struct dentry *indexdir;
  692. struct dentry *origin = ovl_lowerstack(oe)->dentry;
  693. const struct ovl_fh *fh;
  694. int err;
  695. fh = ovl_get_origin_fh(ofs, origin);
  696. if (IS_ERR(fh))
  697. return PTR_ERR(fh);
  698. err = mnt_want_write(mnt);
  699. if (err)
  700. goto out_free_fh;
  701. /* Verify lower root is upper root origin */
  702. err = ovl_verify_origin_fh(ofs, upperpath->dentry, fh, true);
  703. if (err) {
  704. pr_err("failed to verify upper root origin\n");
  705. goto out;
  706. }
  707. /* index dir will act also as workdir */
  708. iput(ofs->workdir_trap);
  709. ofs->workdir_trap = NULL;
  710. dput(ofs->workdir);
  711. ofs->workdir = NULL;
  712. indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
  713. if (IS_ERR(indexdir)) {
  714. err = PTR_ERR(indexdir);
  715. } else if (indexdir) {
  716. ofs->workdir = indexdir;
  717. err = ovl_setup_trap(sb, indexdir, &ofs->workdir_trap,
  718. "indexdir");
  719. if (err)
  720. goto out;
  721. /*
  722. * Verify upper root is exclusively associated with index dir.
  723. * Older kernels stored upper fh in ".overlay.origin"
  724. * xattr. If that xattr exists, verify that it is a match to
  725. * upper dir file handle. In any case, verify or set xattr
  726. * ".overlay.upper" to indicate that index may have
  727. * directory entries.
  728. */
  729. if (ovl_check_origin_xattr(ofs, indexdir)) {
  730. err = ovl_verify_origin_xattr(ofs, indexdir,
  731. OVL_XATTR_ORIGIN,
  732. upperpath->dentry, true,
  733. false);
  734. if (err)
  735. pr_err("failed to verify index dir 'origin' xattr\n");
  736. }
  737. err = ovl_verify_upper(ofs, indexdir, upperpath->dentry, true);
  738. if (err)
  739. pr_err("failed to verify index dir 'upper' xattr\n");
  740. /* Cleanup bad/stale/orphan index entries */
  741. if (!err)
  742. err = ovl_indexdir_cleanup(ofs);
  743. }
  744. if (err || !indexdir)
  745. pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
  746. out:
  747. mnt_drop_write(mnt);
  748. out_free_fh:
  749. kfree(fh);
  750. return err;
  751. }
  752. static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
  753. {
  754. unsigned int i;
  755. if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
  756. return true;
  757. /*
  758. * We allow using single lower with null uuid for index and nfs_export
  759. * for example to support those features with single lower squashfs.
  760. * To avoid regressions in setups of overlay with re-formatted lower
  761. * squashfs, do not allow decoding origin with lower null uuid unless
  762. * user opted-in to one of the new features that require following the
  763. * lower inode of non-dir upper.
  764. */
  765. if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid))
  766. return false;
  767. for (i = 0; i < ofs->numfs; i++) {
  768. /*
  769. * We use uuid to associate an overlay lower file handle with a
  770. * lower layer, so we can accept lower fs with null uuid as long
  771. * as all lower layers with null uuid are on the same fs.
  772. * if we detect multiple lower fs with the same uuid, we
  773. * disable lower file handle decoding on all of them.
  774. */
  775. if (ofs->fs[i].is_lower &&
  776. uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
  777. ofs->fs[i].bad_uuid = true;
  778. return false;
  779. }
  780. }
  781. return true;
  782. }
  783. /* Get a unique fsid for the layer */
  784. static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
  785. {
  786. struct super_block *sb = path->mnt->mnt_sb;
  787. unsigned int i;
  788. dev_t dev;
  789. int err;
  790. bool bad_uuid = false;
  791. bool warn = false;
  792. for (i = 0; i < ofs->numfs; i++) {
  793. if (ofs->fs[i].sb == sb)
  794. return i;
  795. }
  796. if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
  797. bad_uuid = true;
  798. if (ofs->config.xino == OVL_XINO_AUTO) {
  799. ofs->config.xino = OVL_XINO_OFF;
  800. warn = true;
  801. }
  802. if (ofs->config.index || ofs->config.nfs_export) {
  803. ofs->config.index = false;
  804. ofs->config.nfs_export = false;
  805. warn = true;
  806. }
  807. if (warn) {
  808. pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n",
  809. uuid_is_null(&sb->s_uuid) ? "null" :
  810. "conflicting",
  811. path->dentry, ovl_xino_mode(&ofs->config));
  812. }
  813. }
  814. err = get_anon_bdev(&dev);
  815. if (err) {
  816. pr_err("failed to get anonymous bdev for lowerpath\n");
  817. return err;
  818. }
  819. ofs->fs[ofs->numfs].sb = sb;
  820. ofs->fs[ofs->numfs].pseudo_dev = dev;
  821. ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
  822. return ofs->numfs++;
  823. }
  824. /*
  825. * The fsid after the last lower fsid is used for the data layers.
  826. * It is a "null fs" with a null sb, null uuid, and no pseudo dev.
  827. */
  828. static int ovl_get_data_fsid(struct ovl_fs *ofs)
  829. {
  830. return ofs->numfs;
  831. }
  832. static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
  833. struct ovl_fs_context *ctx, struct ovl_layer *layers)
  834. {
  835. int err;
  836. unsigned int i;
  837. size_t nr_merged_lower;
  838. ofs->fs = kcalloc(ctx->nr + 2, sizeof(struct ovl_sb), GFP_KERNEL);
  839. if (ofs->fs == NULL)
  840. return -ENOMEM;
  841. /*
  842. * idx/fsid 0 are reserved for upper fs even with lower only overlay
  843. * and the last fsid is reserved for "null fs" of the data layers.
  844. */
  845. ofs->numfs++;
  846. /*
  847. * All lower layers that share the same fs as upper layer, use the same
  848. * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
  849. * only overlay to simplify ovl_fs_free().
  850. * is_lower will be set if upper fs is shared with a lower layer.
  851. */
  852. err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
  853. if (err) {
  854. pr_err("failed to get anonymous bdev for upper fs\n");
  855. return err;
  856. }
  857. if (ovl_upper_mnt(ofs)) {
  858. ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
  859. ofs->fs[0].is_lower = false;
  860. }
  861. nr_merged_lower = ctx->nr - ctx->nr_data;
  862. for (i = 0; i < ctx->nr; i++) {
  863. struct ovl_fs_context_layer *l = &ctx->lower[i];
  864. struct vfsmount *mnt;
  865. struct inode *trap;
  866. int fsid;
  867. if (i < nr_merged_lower)
  868. fsid = ovl_get_fsid(ofs, &l->path);
  869. else
  870. fsid = ovl_get_data_fsid(ofs);
  871. if (fsid < 0)
  872. return fsid;
  873. /*
  874. * Check if lower root conflicts with this overlay layers before
  875. * checking if it is in-use as upperdir/workdir of "another"
  876. * mount, because we do not bother to check in ovl_is_inuse() if
  877. * the upperdir/workdir is in fact in-use by our
  878. * upperdir/workdir.
  879. */
  880. err = ovl_setup_trap(sb, l->path.dentry, &trap, "lowerdir");
  881. if (err)
  882. return err;
  883. if (ovl_is_inuse(l->path.dentry)) {
  884. err = ovl_report_in_use(ofs, "lowerdir");
  885. if (err) {
  886. iput(trap);
  887. return err;
  888. }
  889. }
  890. mnt = clone_private_mount(&l->path);
  891. err = PTR_ERR(mnt);
  892. if (IS_ERR(mnt)) {
  893. pr_err("failed to clone lowerpath\n");
  894. iput(trap);
  895. return err;
  896. }
  897. /*
  898. * Make lower layers R/O. That way fchmod/fchown on lower file
  899. * will fail instead of modifying lower fs.
  900. */
  901. mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
  902. layers[ofs->numlayer].trap = trap;
  903. layers[ofs->numlayer].mnt = mnt;
  904. layers[ofs->numlayer].idx = ofs->numlayer;
  905. layers[ofs->numlayer].fsid = fsid;
  906. layers[ofs->numlayer].fs = &ofs->fs[fsid];
  907. /* Store for printing lowerdir=... in ovl_show_options() */
  908. ofs->config.lowerdirs[ofs->numlayer] = l->name;
  909. l->name = NULL;
  910. ofs->numlayer++;
  911. ofs->fs[fsid].is_lower = true;
  912. }
  913. /*
  914. * When all layers on same fs, overlay can use real inode numbers.
  915. * With mount option "xino=<on|auto>", mounter declares that there are
  916. * enough free high bits in underlying fs to hold the unique fsid.
  917. * If overlayfs does encounter underlying inodes using the high xino
  918. * bits reserved for fsid, it emits a warning and uses the original
  919. * inode number or a non persistent inode number allocated from a
  920. * dedicated range.
  921. */
  922. if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
  923. if (ofs->config.xino == OVL_XINO_ON)
  924. pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
  925. ofs->xino_mode = 0;
  926. } else if (ofs->config.xino == OVL_XINO_OFF) {
  927. ofs->xino_mode = -1;
  928. } else if (ofs->xino_mode < 0) {
  929. /*
  930. * This is a roundup of number of bits needed for encoding
  931. * fsid, where fsid 0 is reserved for upper fs (even with
  932. * lower only overlay) +1 extra bit is reserved for the non
  933. * persistent inode number range that is used for resolving
  934. * xino lower bits overflow.
  935. */
  936. BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
  937. ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
  938. }
  939. if (ofs->xino_mode > 0) {
  940. pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
  941. ofs->xino_mode);
  942. }
  943. return 0;
  944. }
  945. static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
  946. struct ovl_fs_context *ctx,
  947. struct ovl_fs *ofs,
  948. struct ovl_layer *layers)
  949. {
  950. int err;
  951. unsigned int i;
  952. size_t nr_merged_lower;
  953. struct ovl_entry *oe;
  954. struct ovl_path *lowerstack;
  955. struct ovl_fs_context_layer *l;
  956. if (!ofs->config.upperdir && ctx->nr == 1) {
  957. pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
  958. return ERR_PTR(-EINVAL);
  959. }
  960. if (ctx->nr == ctx->nr_data) {
  961. pr_err("at least one non-data lowerdir is required\n");
  962. return ERR_PTR(-EINVAL);
  963. }
  964. err = -EINVAL;
  965. for (i = 0; i < ctx->nr; i++) {
  966. l = &ctx->lower[i];
  967. err = ovl_lower_dir(l->name, &l->path, ofs, &sb->s_stack_depth);
  968. if (err)
  969. return ERR_PTR(err);
  970. }
  971. err = -EINVAL;
  972. sb->s_stack_depth++;
  973. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  974. pr_err("maximum fs stacking depth exceeded\n");
  975. return ERR_PTR(err);
  976. }
  977. err = ovl_get_layers(sb, ofs, ctx, layers);
  978. if (err)
  979. return ERR_PTR(err);
  980. err = -ENOMEM;
  981. /* Data-only layers are not merged in root directory */
  982. nr_merged_lower = ctx->nr - ctx->nr_data;
  983. oe = ovl_alloc_entry(nr_merged_lower);
  984. if (!oe)
  985. return ERR_PTR(err);
  986. lowerstack = ovl_lowerstack(oe);
  987. for (i = 0; i < nr_merged_lower; i++) {
  988. l = &ctx->lower[i];
  989. lowerstack[i].dentry = dget(l->path.dentry);
  990. lowerstack[i].layer = &ofs->layers[i + 1];
  991. }
  992. ofs->numdatalayer = ctx->nr_data;
  993. return oe;
  994. }
  995. /*
  996. * Check if this layer root is a descendant of:
  997. * - another layer of this overlayfs instance
  998. * - upper/work dir of any overlayfs instance
  999. */
  1000. static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
  1001. struct dentry *dentry, const char *name,
  1002. bool is_lower)
  1003. {
  1004. struct dentry *next = dentry, *parent;
  1005. int err = 0;
  1006. if (!dentry)
  1007. return 0;
  1008. parent = dget_parent(next);
  1009. /* Walk back ancestors to root (inclusive) looking for traps */
  1010. while (!err && parent != next) {
  1011. if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
  1012. err = -ELOOP;
  1013. pr_err("overlapping %s path\n", name);
  1014. } else if (ovl_is_inuse(parent)) {
  1015. err = ovl_report_in_use(ofs, name);
  1016. }
  1017. next = parent;
  1018. parent = dget_parent(next);
  1019. dput(next);
  1020. }
  1021. dput(parent);
  1022. return err;
  1023. }
  1024. /*
  1025. * Check if any of the layers or work dirs overlap.
  1026. */
  1027. static int ovl_check_overlapping_layers(struct super_block *sb,
  1028. struct ovl_fs *ofs)
  1029. {
  1030. int i, err;
  1031. if (ovl_upper_mnt(ofs)) {
  1032. err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
  1033. "upperdir", false);
  1034. if (err)
  1035. return err;
  1036. /*
  1037. * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
  1038. * this instance and covers overlapping work and index dirs,
  1039. * unless work or index dir have been moved since created inside
  1040. * workbasedir. In that case, we already have their traps in
  1041. * inode cache and we will catch that case on lookup.
  1042. */
  1043. err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
  1044. false);
  1045. if (err)
  1046. return err;
  1047. }
  1048. for (i = 1; i < ofs->numlayer; i++) {
  1049. err = ovl_check_layer(sb, ofs,
  1050. ofs->layers[i].mnt->mnt_root,
  1051. "lowerdir", true);
  1052. if (err)
  1053. return err;
  1054. }
  1055. return 0;
  1056. }
  1057. static struct dentry *ovl_get_root(struct super_block *sb,
  1058. struct dentry *upperdentry,
  1059. struct ovl_entry *oe)
  1060. {
  1061. struct dentry *root;
  1062. struct ovl_fs *ofs = OVL_FS(sb);
  1063. struct ovl_path *lowerpath = ovl_lowerstack(oe);
  1064. unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
  1065. int fsid = lowerpath->layer->fsid;
  1066. struct ovl_inode_params oip = {
  1067. .upperdentry = upperdentry,
  1068. .oe = oe,
  1069. };
  1070. root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
  1071. if (!root)
  1072. return NULL;
  1073. if (upperdentry) {
  1074. /* Root inode uses upper st_ino/i_ino */
  1075. ino = d_inode(upperdentry)->i_ino;
  1076. fsid = 0;
  1077. ovl_dentry_set_upper_alias(root);
  1078. if (ovl_is_impuredir(sb, upperdentry))
  1079. ovl_set_flag(OVL_IMPURE, d_inode(root));
  1080. }
  1081. /* Look for xwhiteouts marker except in the lowermost layer */
  1082. for (int i = 0; i < ovl_numlower(oe) - 1; i++, lowerpath++) {
  1083. struct path path = {
  1084. .mnt = lowerpath->layer->mnt,
  1085. .dentry = lowerpath->dentry,
  1086. };
  1087. /* overlay.opaque=x means xwhiteouts directory */
  1088. if (ovl_get_opaquedir_val(ofs, &path) == 'x') {
  1089. ovl_layer_set_xwhiteouts(ofs, lowerpath->layer);
  1090. ovl_dentry_set_xwhiteouts(root);
  1091. }
  1092. }
  1093. /* Root is always merge -> can have whiteouts */
  1094. ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
  1095. ovl_dentry_set_flag(OVL_E_CONNECTED, root);
  1096. ovl_set_upperdata(d_inode(root));
  1097. ovl_inode_init(d_inode(root), &oip, ino, fsid);
  1098. ovl_dentry_init_flags(root, upperdentry, oe, DCACHE_OP_WEAK_REVALIDATE);
  1099. /* root keeps a reference of upperdentry */
  1100. dget(upperdentry);
  1101. return root;
  1102. }
  1103. int ovl_fill_super(struct super_block *sb, struct fs_context *fc)
  1104. {
  1105. struct ovl_fs *ofs = sb->s_fs_info;
  1106. struct ovl_fs_context *ctx = fc->fs_private;
  1107. struct dentry *root_dentry;
  1108. struct ovl_entry *oe;
  1109. struct ovl_layer *layers;
  1110. struct cred *cred;
  1111. int err;
  1112. err = -EIO;
  1113. if (WARN_ON(fc->user_ns != current_user_ns()))
  1114. goto out_err;
  1115. sb->s_d_op = &ovl_dentry_operations;
  1116. err = -ENOMEM;
  1117. ofs->creator_cred = cred = prepare_creds();
  1118. if (!cred)
  1119. goto out_err;
  1120. err = ovl_fs_params_verify(ctx, &ofs->config);
  1121. if (err)
  1122. goto out_err;
  1123. err = -EINVAL;
  1124. if (ctx->nr == 0) {
  1125. if (!(fc->sb_flags & SB_SILENT))
  1126. pr_err("missing 'lowerdir'\n");
  1127. goto out_err;
  1128. }
  1129. err = -ENOMEM;
  1130. layers = kcalloc(ctx->nr + 1, sizeof(struct ovl_layer), GFP_KERNEL);
  1131. if (!layers)
  1132. goto out_err;
  1133. ofs->config.lowerdirs = kcalloc(ctx->nr + 1, sizeof(char *), GFP_KERNEL);
  1134. if (!ofs->config.lowerdirs) {
  1135. kfree(layers);
  1136. goto out_err;
  1137. }
  1138. ofs->layers = layers;
  1139. /*
  1140. * Layer 0 is reserved for upper even if there's no upper.
  1141. * config.lowerdirs[0] is used for storing the user provided colon
  1142. * separated lowerdir string.
  1143. */
  1144. ofs->config.lowerdirs[0] = ctx->lowerdir_all;
  1145. ctx->lowerdir_all = NULL;
  1146. ofs->numlayer = 1;
  1147. sb->s_stack_depth = 0;
  1148. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1149. atomic_long_set(&ofs->last_ino, 1);
  1150. /* Assume underlying fs uses 32bit inodes unless proven otherwise */
  1151. if (ofs->config.xino != OVL_XINO_OFF) {
  1152. ofs->xino_mode = BITS_PER_LONG - 32;
  1153. if (!ofs->xino_mode) {
  1154. pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
  1155. ofs->config.xino = OVL_XINO_OFF;
  1156. }
  1157. }
  1158. /* alloc/destroy_inode needed for setting up traps in inode cache */
  1159. sb->s_op = &ovl_super_operations;
  1160. if (ofs->config.upperdir) {
  1161. struct super_block *upper_sb;
  1162. err = -EINVAL;
  1163. if (!ofs->config.workdir) {
  1164. pr_err("missing 'workdir'\n");
  1165. goto out_err;
  1166. }
  1167. err = ovl_get_upper(sb, ofs, &layers[0], &ctx->upper);
  1168. if (err)
  1169. goto out_err;
  1170. upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
  1171. if (!ovl_should_sync(ofs)) {
  1172. ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
  1173. if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
  1174. err = -EIO;
  1175. pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
  1176. goto out_err;
  1177. }
  1178. }
  1179. err = ovl_get_workdir(sb, ofs, &ctx->upper, &ctx->work);
  1180. if (err)
  1181. goto out_err;
  1182. if (!ofs->workdir)
  1183. sb->s_flags |= SB_RDONLY;
  1184. sb->s_stack_depth = upper_sb->s_stack_depth;
  1185. sb->s_time_gran = upper_sb->s_time_gran;
  1186. }
  1187. oe = ovl_get_lowerstack(sb, ctx, ofs, layers);
  1188. err = PTR_ERR(oe);
  1189. if (IS_ERR(oe))
  1190. goto out_err;
  1191. /* If the upper fs is nonexistent, we mark overlayfs r/o too */
  1192. if (!ovl_upper_mnt(ofs))
  1193. sb->s_flags |= SB_RDONLY;
  1194. if (!ovl_origin_uuid(ofs) && ofs->numfs > 1) {
  1195. pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=null.\n");
  1196. ofs->config.uuid = OVL_UUID_NULL;
  1197. } else if (ovl_has_fsid(ofs) && ovl_upper_mnt(ofs)) {
  1198. /* Use per instance persistent uuid/fsid */
  1199. ovl_init_uuid_xattr(sb, ofs, &ctx->upper);
  1200. }
  1201. if (!ovl_force_readonly(ofs) && ofs->config.index) {
  1202. err = ovl_get_indexdir(sb, ofs, oe, &ctx->upper);
  1203. if (err)
  1204. goto out_free_oe;
  1205. /* Force r/o mount with no index dir */
  1206. if (!ofs->workdir)
  1207. sb->s_flags |= SB_RDONLY;
  1208. }
  1209. err = ovl_check_overlapping_layers(sb, ofs);
  1210. if (err)
  1211. goto out_free_oe;
  1212. /* Show index=off in /proc/mounts for forced r/o mount */
  1213. if (!ofs->workdir) {
  1214. ofs->config.index = false;
  1215. if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
  1216. pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
  1217. ofs->config.nfs_export = false;
  1218. }
  1219. }
  1220. if (ofs->config.metacopy && ofs->config.nfs_export) {
  1221. pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
  1222. ofs->config.nfs_export = false;
  1223. }
  1224. /*
  1225. * Support encoding decodable file handles with nfs_export=on
  1226. * and encoding non-decodable file handles with nfs_export=off
  1227. * if all layers support file handles.
  1228. */
  1229. if (ofs->config.nfs_export)
  1230. sb->s_export_op = &ovl_export_operations;
  1231. else if (!ofs->nofh)
  1232. sb->s_export_op = &ovl_export_fid_operations;
  1233. /* Never override disk quota limits or use reserved space */
  1234. cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
  1235. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  1236. sb->s_xattr = ovl_xattr_handlers(ofs);
  1237. sb->s_fs_info = ofs;
  1238. #ifdef CONFIG_FS_POSIX_ACL
  1239. sb->s_flags |= SB_POSIXACL;
  1240. #endif
  1241. sb->s_iflags |= SB_I_SKIP_SYNC;
  1242. /*
  1243. * Ensure that umask handling is done by the filesystems used
  1244. * for the the upper layer instead of overlayfs as that would
  1245. * lead to unexpected results.
  1246. */
  1247. sb->s_iflags |= SB_I_NOUMASK;
  1248. sb->s_iflags |= SB_I_EVM_HMAC_UNSUPPORTED;
  1249. err = -ENOMEM;
  1250. root_dentry = ovl_get_root(sb, ctx->upper.dentry, oe);
  1251. if (!root_dentry)
  1252. goto out_free_oe;
  1253. sb->s_root = root_dentry;
  1254. return 0;
  1255. out_free_oe:
  1256. ovl_free_entry(oe);
  1257. out_err:
  1258. ovl_free_fs(ofs);
  1259. sb->s_fs_info = NULL;
  1260. return err;
  1261. }
  1262. struct file_system_type ovl_fs_type = {
  1263. .owner = THIS_MODULE,
  1264. .name = "overlay",
  1265. .init_fs_context = ovl_init_fs_context,
  1266. .parameters = ovl_parameter_spec,
  1267. .fs_flags = FS_USERNS_MOUNT,
  1268. .kill_sb = kill_anon_super,
  1269. };
  1270. MODULE_ALIAS_FS("overlay");
  1271. static void ovl_inode_init_once(void *foo)
  1272. {
  1273. struct ovl_inode *oi = foo;
  1274. inode_init_once(&oi->vfs_inode);
  1275. }
  1276. static int __init ovl_init(void)
  1277. {
  1278. int err;
  1279. ovl_inode_cachep = kmem_cache_create("ovl_inode",
  1280. sizeof(struct ovl_inode), 0,
  1281. (SLAB_RECLAIM_ACCOUNT|
  1282. SLAB_ACCOUNT),
  1283. ovl_inode_init_once);
  1284. if (ovl_inode_cachep == NULL)
  1285. return -ENOMEM;
  1286. err = register_filesystem(&ovl_fs_type);
  1287. if (!err)
  1288. return 0;
  1289. kmem_cache_destroy(ovl_inode_cachep);
  1290. return err;
  1291. }
  1292. static void __exit ovl_exit(void)
  1293. {
  1294. unregister_filesystem(&ovl_fs_type);
  1295. /*
  1296. * Make sure all delayed rcu free inodes are flushed before we
  1297. * destroy cache.
  1298. */
  1299. rcu_barrier();
  1300. kmem_cache_destroy(ovl_inode_cachep);
  1301. }
  1302. module_init(ovl_init);
  1303. module_exit(ovl_exit);