super.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 <linux/fs.h>
  10. #include <linux/namei.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/xattr.h>
  13. #include <linux/security.h>
  14. #include <linux/mount.h>
  15. #include <linux/slab.h>
  16. #include <linux/parser.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/statfs.h>
  20. #include <linux/seq_file.h>
  21. #include "overlayfs.h"
  22. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  23. MODULE_DESCRIPTION("Overlay filesystem");
  24. MODULE_LICENSE("GPL");
  25. #define OVERLAYFS_SUPER_MAGIC 0x794c7630
  26. struct ovl_config {
  27. char *lowerdir;
  28. char *upperdir;
  29. char *workdir;
  30. };
  31. /* private information held for overlayfs's superblock */
  32. struct ovl_fs {
  33. struct vfsmount *upper_mnt;
  34. unsigned numlower;
  35. struct vfsmount **lower_mnt;
  36. struct dentry *workdir;
  37. long lower_namelen;
  38. /* pathnames of lower and upper dirs, for show_options */
  39. struct ovl_config config;
  40. /* creds of process who forced instantiation of super block */
  41. const struct cred *creator_cred;
  42. };
  43. struct ovl_dir_cache;
  44. /* private information held for every overlayfs dentry */
  45. struct ovl_entry {
  46. struct dentry *__upperdentry;
  47. struct ovl_dir_cache *cache;
  48. union {
  49. struct {
  50. u64 version;
  51. bool opaque;
  52. };
  53. struct rcu_head rcu;
  54. };
  55. unsigned numlower;
  56. struct path lowerstack[];
  57. };
  58. #define OVL_MAX_STACK 500
  59. static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
  60. {
  61. return oe->numlower ? oe->lowerstack[0].dentry : NULL;
  62. }
  63. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  64. {
  65. struct ovl_entry *oe = dentry->d_fsdata;
  66. enum ovl_path_type type = 0;
  67. if (oe->__upperdentry) {
  68. type = __OVL_PATH_UPPER;
  69. /*
  70. * Non-dir dentry can hold lower dentry from previous
  71. * location. Its purity depends only on opaque flag.
  72. */
  73. if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode))
  74. type |= __OVL_PATH_MERGE;
  75. else if (!oe->opaque)
  76. type |= __OVL_PATH_PURE;
  77. } else {
  78. if (oe->numlower > 1)
  79. type |= __OVL_PATH_MERGE;
  80. }
  81. return type;
  82. }
  83. static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
  84. {
  85. return lockless_dereference(oe->__upperdentry);
  86. }
  87. void ovl_path_upper(struct dentry *dentry, struct path *path)
  88. {
  89. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  90. struct ovl_entry *oe = dentry->d_fsdata;
  91. path->mnt = ofs->upper_mnt;
  92. path->dentry = ovl_upperdentry_dereference(oe);
  93. }
  94. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  95. {
  96. enum ovl_path_type type = ovl_path_type(dentry);
  97. if (!OVL_TYPE_UPPER(type))
  98. ovl_path_lower(dentry, path);
  99. else
  100. ovl_path_upper(dentry, path);
  101. return type;
  102. }
  103. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  104. {
  105. struct ovl_entry *oe = dentry->d_fsdata;
  106. return ovl_upperdentry_dereference(oe);
  107. }
  108. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  109. {
  110. struct ovl_entry *oe = dentry->d_fsdata;
  111. return __ovl_dentry_lower(oe);
  112. }
  113. struct dentry *ovl_dentry_real(struct dentry *dentry)
  114. {
  115. struct ovl_entry *oe = dentry->d_fsdata;
  116. struct dentry *realdentry;
  117. realdentry = ovl_upperdentry_dereference(oe);
  118. if (!realdentry)
  119. realdentry = __ovl_dentry_lower(oe);
  120. return realdentry;
  121. }
  122. struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
  123. {
  124. struct dentry *realdentry;
  125. realdentry = ovl_upperdentry_dereference(oe);
  126. if (realdentry) {
  127. *is_upper = true;
  128. } else {
  129. realdentry = __ovl_dentry_lower(oe);
  130. *is_upper = false;
  131. }
  132. return realdentry;
  133. }
  134. struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
  135. {
  136. struct ovl_entry *oe = dentry->d_fsdata;
  137. return oe->cache;
  138. }
  139. void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
  140. {
  141. struct ovl_entry *oe = dentry->d_fsdata;
  142. oe->cache = cache;
  143. }
  144. void ovl_path_lower(struct dentry *dentry, struct path *path)
  145. {
  146. struct ovl_entry *oe = dentry->d_fsdata;
  147. *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
  148. }
  149. int ovl_want_write(struct dentry *dentry)
  150. {
  151. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  152. return mnt_want_write(ofs->upper_mnt);
  153. }
  154. void ovl_drop_write(struct dentry *dentry)
  155. {
  156. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  157. mnt_drop_write(ofs->upper_mnt);
  158. }
  159. struct dentry *ovl_workdir(struct dentry *dentry)
  160. {
  161. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  162. return ofs->workdir;
  163. }
  164. bool ovl_dentry_is_opaque(struct dentry *dentry)
  165. {
  166. struct ovl_entry *oe = dentry->d_fsdata;
  167. return oe->opaque;
  168. }
  169. void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
  170. {
  171. struct ovl_entry *oe = dentry->d_fsdata;
  172. oe->opaque = opaque;
  173. }
  174. void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
  175. {
  176. struct ovl_entry *oe = dentry->d_fsdata;
  177. WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
  178. WARN_ON(oe->__upperdentry);
  179. BUG_ON(!upperdentry->d_inode);
  180. /*
  181. * Make sure upperdentry is consistent before making it visible to
  182. * ovl_upperdentry_dereference().
  183. */
  184. smp_wmb();
  185. oe->__upperdentry = upperdentry;
  186. }
  187. void ovl_dentry_version_inc(struct dentry *dentry)
  188. {
  189. struct ovl_entry *oe = dentry->d_fsdata;
  190. WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  191. oe->version++;
  192. }
  193. u64 ovl_dentry_version_get(struct dentry *dentry)
  194. {
  195. struct ovl_entry *oe = dentry->d_fsdata;
  196. WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  197. return oe->version;
  198. }
  199. bool ovl_is_whiteout(struct dentry *dentry)
  200. {
  201. struct inode *inode = dentry->d_inode;
  202. return inode && IS_WHITEOUT(inode);
  203. }
  204. const struct cred *ovl_override_creds(struct super_block *sb)
  205. {
  206. struct ovl_fs *ofs = sb->s_fs_info;
  207. return override_creds(ofs->creator_cred);
  208. }
  209. static bool ovl_is_opaquedir(struct dentry *dentry)
  210. {
  211. int res;
  212. char val;
  213. struct inode *inode = dentry->d_inode;
  214. if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
  215. return false;
  216. res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
  217. if (res == 1 && val == 'y')
  218. return true;
  219. return false;
  220. }
  221. static void ovl_dentry_release(struct dentry *dentry)
  222. {
  223. struct ovl_entry *oe = dentry->d_fsdata;
  224. if (oe) {
  225. unsigned int i;
  226. dput(oe->__upperdentry);
  227. for (i = 0; i < oe->numlower; i++)
  228. dput(oe->lowerstack[i].dentry);
  229. kfree_rcu(oe, rcu);
  230. }
  231. }
  232. static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
  233. {
  234. struct dentry *real;
  235. if (d_is_dir(dentry)) {
  236. if (!inode || inode == d_inode(dentry))
  237. return dentry;
  238. goto bug;
  239. }
  240. real = ovl_dentry_upper(dentry);
  241. if (real && (!inode || inode == d_inode(real)))
  242. return real;
  243. real = ovl_dentry_lower(dentry);
  244. if (!real)
  245. goto bug;
  246. if (!inode || inode == d_inode(real))
  247. return real;
  248. /* Handle recursion */
  249. if (real->d_flags & DCACHE_OP_REAL)
  250. return real->d_op->d_real(real, inode);
  251. bug:
  252. WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
  253. inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
  254. return dentry;
  255. }
  256. static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
  257. {
  258. struct ovl_entry *oe = dentry->d_fsdata;
  259. unsigned int i;
  260. int ret = 1;
  261. for (i = 0; i < oe->numlower; i++) {
  262. struct dentry *d = oe->lowerstack[i].dentry;
  263. if (d->d_flags & DCACHE_OP_REVALIDATE) {
  264. ret = d->d_op->d_revalidate(d, flags);
  265. if (ret < 0)
  266. return ret;
  267. if (!ret) {
  268. if (!(flags & LOOKUP_RCU))
  269. d_invalidate(d);
  270. return -ESTALE;
  271. }
  272. }
  273. }
  274. return 1;
  275. }
  276. static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
  277. {
  278. struct ovl_entry *oe = dentry->d_fsdata;
  279. unsigned int i;
  280. int ret = 1;
  281. for (i = 0; i < oe->numlower; i++) {
  282. struct dentry *d = oe->lowerstack[i].dentry;
  283. if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
  284. ret = d->d_op->d_weak_revalidate(d, flags);
  285. if (ret <= 0)
  286. break;
  287. }
  288. }
  289. return ret;
  290. }
  291. static const struct dentry_operations ovl_dentry_operations = {
  292. .d_release = ovl_dentry_release,
  293. .d_select_inode = ovl_d_select_inode,
  294. .d_real = ovl_d_real,
  295. };
  296. static const struct dentry_operations ovl_reval_dentry_operations = {
  297. .d_release = ovl_dentry_release,
  298. .d_select_inode = ovl_d_select_inode,
  299. .d_real = ovl_d_real,
  300. .d_revalidate = ovl_dentry_revalidate,
  301. .d_weak_revalidate = ovl_dentry_weak_revalidate,
  302. };
  303. static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  304. {
  305. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  306. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  307. if (oe)
  308. oe->numlower = numlower;
  309. return oe;
  310. }
  311. static bool ovl_dentry_remote(struct dentry *dentry)
  312. {
  313. return dentry->d_flags &
  314. (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
  315. DCACHE_OP_REAL);
  316. }
  317. static bool ovl_dentry_weird(struct dentry *dentry)
  318. {
  319. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  320. DCACHE_MANAGE_TRANSIT |
  321. DCACHE_OP_HASH |
  322. DCACHE_OP_COMPARE);
  323. }
  324. static inline struct dentry *ovl_lookup_real(struct dentry *dir,
  325. struct qstr *name)
  326. {
  327. struct dentry *dentry;
  328. mutex_lock(&dir->d_inode->i_mutex);
  329. dentry = lookup_one_len(name->name, dir, name->len);
  330. mutex_unlock(&dir->d_inode->i_mutex);
  331. if (IS_ERR(dentry)) {
  332. if (PTR_ERR(dentry) == -ENOENT)
  333. dentry = NULL;
  334. } else if (!dentry->d_inode) {
  335. dput(dentry);
  336. dentry = NULL;
  337. } else if (ovl_dentry_weird(dentry)) {
  338. dput(dentry);
  339. /* Don't support traversing automounts and other weirdness */
  340. dentry = ERR_PTR(-EREMOTE);
  341. }
  342. return dentry;
  343. }
  344. /*
  345. * Returns next layer in stack starting from top.
  346. * Returns -1 if this is the last layer.
  347. */
  348. int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
  349. {
  350. struct ovl_entry *oe = dentry->d_fsdata;
  351. BUG_ON(idx < 0);
  352. if (idx == 0) {
  353. ovl_path_upper(dentry, path);
  354. if (path->dentry)
  355. return oe->numlower ? 1 : -1;
  356. idx++;
  357. }
  358. BUG_ON(idx > oe->numlower);
  359. *path = oe->lowerstack[idx - 1];
  360. return (idx < oe->numlower) ? idx + 1 : -1;
  361. }
  362. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  363. unsigned int flags)
  364. {
  365. struct ovl_entry *oe;
  366. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  367. struct path *stack = NULL;
  368. struct dentry *upperdir, *upperdentry = NULL;
  369. unsigned int ctr = 0;
  370. struct inode *inode = NULL;
  371. bool upperopaque = false;
  372. struct dentry *this, *prev = NULL;
  373. unsigned int i;
  374. int err;
  375. upperdir = ovl_upperdentry_dereference(poe);
  376. if (upperdir) {
  377. this = ovl_lookup_real(upperdir, &dentry->d_name);
  378. err = PTR_ERR(this);
  379. if (IS_ERR(this))
  380. goto out;
  381. if (this) {
  382. if (unlikely(ovl_dentry_remote(this))) {
  383. dput(this);
  384. err = -EREMOTE;
  385. goto out;
  386. }
  387. if (ovl_is_whiteout(this)) {
  388. dput(this);
  389. this = NULL;
  390. upperopaque = true;
  391. } else if (poe->numlower && ovl_is_opaquedir(this)) {
  392. upperopaque = true;
  393. }
  394. }
  395. upperdentry = prev = this;
  396. }
  397. if (!upperopaque && poe->numlower) {
  398. err = -ENOMEM;
  399. stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
  400. if (!stack)
  401. goto out_put_upper;
  402. }
  403. for (i = 0; !upperopaque && i < poe->numlower; i++) {
  404. bool opaque = false;
  405. struct path lowerpath = poe->lowerstack[i];
  406. this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
  407. err = PTR_ERR(this);
  408. if (IS_ERR(this)) {
  409. /*
  410. * If it's positive, then treat ENAMETOOLONG as ENOENT.
  411. */
  412. if (err == -ENAMETOOLONG && (upperdentry || ctr))
  413. continue;
  414. goto out_put;
  415. }
  416. if (!this)
  417. continue;
  418. if (ovl_is_whiteout(this)) {
  419. dput(this);
  420. break;
  421. }
  422. /*
  423. * Only makes sense to check opaque dir if this is not the
  424. * lowermost layer.
  425. */
  426. if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
  427. opaque = true;
  428. if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
  429. !S_ISDIR(this->d_inode->i_mode))) {
  430. /*
  431. * FIXME: check for upper-opaqueness maybe better done
  432. * in remove code.
  433. */
  434. if (prev == upperdentry)
  435. upperopaque = true;
  436. dput(this);
  437. break;
  438. }
  439. /*
  440. * If this is a non-directory then stop here.
  441. */
  442. if (!S_ISDIR(this->d_inode->i_mode))
  443. opaque = true;
  444. stack[ctr].dentry = this;
  445. stack[ctr].mnt = lowerpath.mnt;
  446. ctr++;
  447. prev = this;
  448. if (opaque)
  449. break;
  450. }
  451. oe = ovl_alloc_entry(ctr);
  452. err = -ENOMEM;
  453. if (!oe)
  454. goto out_put;
  455. if (upperdentry || ctr) {
  456. struct dentry *realdentry;
  457. realdentry = upperdentry ? upperdentry : stack[0].dentry;
  458. err = -ENOMEM;
  459. inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
  460. oe);
  461. if (!inode)
  462. goto out_free_oe;
  463. ovl_copyattr(realdentry->d_inode, inode);
  464. }
  465. oe->opaque = upperopaque;
  466. oe->__upperdentry = upperdentry;
  467. memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
  468. kfree(stack);
  469. dentry->d_fsdata = oe;
  470. d_add(dentry, inode);
  471. return NULL;
  472. out_free_oe:
  473. kfree(oe);
  474. out_put:
  475. for (i = 0; i < ctr; i++)
  476. dput(stack[i].dentry);
  477. kfree(stack);
  478. out_put_upper:
  479. dput(upperdentry);
  480. out:
  481. return ERR_PTR(err);
  482. }
  483. struct file *ovl_path_open(struct path *path, int flags)
  484. {
  485. return dentry_open(path, flags, current_cred());
  486. }
  487. static void ovl_put_super(struct super_block *sb)
  488. {
  489. struct ovl_fs *ufs = sb->s_fs_info;
  490. unsigned i;
  491. dput(ufs->workdir);
  492. mntput(ufs->upper_mnt);
  493. for (i = 0; i < ufs->numlower; i++)
  494. mntput(ufs->lower_mnt[i]);
  495. kfree(ufs->lower_mnt);
  496. kfree(ufs->config.lowerdir);
  497. kfree(ufs->config.upperdir);
  498. kfree(ufs->config.workdir);
  499. put_cred(ufs->creator_cred);
  500. kfree(ufs);
  501. }
  502. /**
  503. * ovl_statfs
  504. * @sb: The overlayfs super block
  505. * @buf: The struct kstatfs to fill in with stats
  506. *
  507. * Get the filesystem statistics. As writes always target the upper layer
  508. * filesystem pass the statfs to the upper filesystem (if it exists)
  509. */
  510. static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  511. {
  512. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  513. struct dentry *root_dentry = dentry->d_sb->s_root;
  514. struct path path;
  515. int err;
  516. ovl_path_real(root_dentry, &path);
  517. err = vfs_statfs(&path, buf);
  518. if (!err) {
  519. buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
  520. buf->f_type = OVERLAYFS_SUPER_MAGIC;
  521. }
  522. return err;
  523. }
  524. /**
  525. * ovl_show_options
  526. *
  527. * Prints the mount options for a given superblock.
  528. * Returns zero; does not fail.
  529. */
  530. static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  531. {
  532. struct super_block *sb = dentry->d_sb;
  533. struct ovl_fs *ufs = sb->s_fs_info;
  534. seq_show_option(m, "lowerdir", ufs->config.lowerdir);
  535. if (ufs->config.upperdir) {
  536. seq_show_option(m, "upperdir", ufs->config.upperdir);
  537. seq_show_option(m, "workdir", ufs->config.workdir);
  538. }
  539. return 0;
  540. }
  541. static int ovl_remount(struct super_block *sb, int *flags, char *data)
  542. {
  543. struct ovl_fs *ufs = sb->s_fs_info;
  544. if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
  545. return -EROFS;
  546. return 0;
  547. }
  548. static const struct super_operations ovl_super_operations = {
  549. .put_super = ovl_put_super,
  550. .statfs = ovl_statfs,
  551. .show_options = ovl_show_options,
  552. .remount_fs = ovl_remount,
  553. };
  554. enum {
  555. OPT_LOWERDIR,
  556. OPT_UPPERDIR,
  557. OPT_WORKDIR,
  558. OPT_ERR,
  559. };
  560. static const match_table_t ovl_tokens = {
  561. {OPT_LOWERDIR, "lowerdir=%s"},
  562. {OPT_UPPERDIR, "upperdir=%s"},
  563. {OPT_WORKDIR, "workdir=%s"},
  564. {OPT_ERR, NULL}
  565. };
  566. static char *ovl_next_opt(char **s)
  567. {
  568. char *sbegin = *s;
  569. char *p;
  570. if (sbegin == NULL)
  571. return NULL;
  572. for (p = sbegin; *p; p++) {
  573. if (*p == '\\') {
  574. p++;
  575. if (!*p)
  576. break;
  577. } else if (*p == ',') {
  578. *p = '\0';
  579. *s = p + 1;
  580. return sbegin;
  581. }
  582. }
  583. *s = NULL;
  584. return sbegin;
  585. }
  586. static int ovl_parse_opt(char *opt, struct ovl_config *config)
  587. {
  588. char *p;
  589. while ((p = ovl_next_opt(&opt)) != NULL) {
  590. int token;
  591. substring_t args[MAX_OPT_ARGS];
  592. if (!*p)
  593. continue;
  594. token = match_token(p, ovl_tokens, args);
  595. switch (token) {
  596. case OPT_UPPERDIR:
  597. kfree(config->upperdir);
  598. config->upperdir = match_strdup(&args[0]);
  599. if (!config->upperdir)
  600. return -ENOMEM;
  601. break;
  602. case OPT_LOWERDIR:
  603. kfree(config->lowerdir);
  604. config->lowerdir = match_strdup(&args[0]);
  605. if (!config->lowerdir)
  606. return -ENOMEM;
  607. break;
  608. case OPT_WORKDIR:
  609. kfree(config->workdir);
  610. config->workdir = match_strdup(&args[0]);
  611. if (!config->workdir)
  612. return -ENOMEM;
  613. break;
  614. default:
  615. pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
  616. return -EINVAL;
  617. }
  618. }
  619. /* Workdir is useless in non-upper mount */
  620. if (!config->upperdir && config->workdir) {
  621. pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
  622. config->workdir);
  623. kfree(config->workdir);
  624. config->workdir = NULL;
  625. }
  626. return 0;
  627. }
  628. #define OVL_WORKDIR_NAME "work"
  629. static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
  630. struct dentry *dentry)
  631. {
  632. struct inode *dir = dentry->d_inode;
  633. struct dentry *work;
  634. int err;
  635. bool retried = false;
  636. err = mnt_want_write(mnt);
  637. if (err)
  638. return ERR_PTR(err);
  639. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  640. retry:
  641. work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
  642. strlen(OVL_WORKDIR_NAME));
  643. if (!IS_ERR(work)) {
  644. struct kstat stat = {
  645. .mode = S_IFDIR | 0,
  646. };
  647. struct iattr attr = {
  648. .ia_valid = ATTR_MODE,
  649. .ia_mode = stat.mode,
  650. };
  651. if (work->d_inode) {
  652. err = -EEXIST;
  653. if (retried)
  654. goto out_dput;
  655. retried = true;
  656. ovl_workdir_cleanup(dir, mnt, work, 0);
  657. dput(work);
  658. goto retry;
  659. }
  660. err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
  661. if (err)
  662. goto out_dput;
  663. err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
  664. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  665. goto out_dput;
  666. err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
  667. if (err && err != -ENODATA && err != -EOPNOTSUPP)
  668. goto out_dput;
  669. /* Clear any inherited mode bits */
  670. inode_lock(work->d_inode);
  671. err = notify_change(work, &attr, NULL);
  672. inode_unlock(work->d_inode);
  673. if (err)
  674. goto out_dput;
  675. }
  676. out_unlock:
  677. mutex_unlock(&dir->i_mutex);
  678. mnt_drop_write(mnt);
  679. return work;
  680. out_dput:
  681. dput(work);
  682. work = ERR_PTR(err);
  683. goto out_unlock;
  684. }
  685. static void ovl_unescape(char *s)
  686. {
  687. char *d = s;
  688. for (;; s++, d++) {
  689. if (*s == '\\')
  690. s++;
  691. *d = *s;
  692. if (!*s)
  693. break;
  694. }
  695. }
  696. static int ovl_mount_dir_noesc(const char *name, struct path *path)
  697. {
  698. int err = -EINVAL;
  699. if (!*name) {
  700. pr_err("overlayfs: empty lowerdir\n");
  701. goto out;
  702. }
  703. err = kern_path(name, LOOKUP_FOLLOW, path);
  704. if (err) {
  705. pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
  706. goto out;
  707. }
  708. err = -EINVAL;
  709. if (ovl_dentry_weird(path->dentry)) {
  710. pr_err("overlayfs: filesystem on '%s' not supported\n", name);
  711. goto out_put;
  712. }
  713. if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
  714. pr_err("overlayfs: '%s' not a directory\n", name);
  715. goto out_put;
  716. }
  717. return 0;
  718. out_put:
  719. path_put(path);
  720. out:
  721. return err;
  722. }
  723. static int ovl_mount_dir(const char *name, struct path *path)
  724. {
  725. int err = -ENOMEM;
  726. char *tmp = kstrdup(name, GFP_KERNEL);
  727. if (tmp) {
  728. ovl_unescape(tmp);
  729. err = ovl_mount_dir_noesc(tmp, path);
  730. if (!err)
  731. if (ovl_dentry_remote(path->dentry)) {
  732. pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
  733. tmp);
  734. path_put(path);
  735. err = -EINVAL;
  736. }
  737. kfree(tmp);
  738. }
  739. return err;
  740. }
  741. static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
  742. int *stack_depth, bool *remote)
  743. {
  744. int err;
  745. struct kstatfs statfs;
  746. err = ovl_mount_dir_noesc(name, path);
  747. if (err)
  748. goto out;
  749. err = vfs_statfs(path, &statfs);
  750. if (err) {
  751. pr_err("overlayfs: statfs failed on '%s'\n", name);
  752. goto out_put;
  753. }
  754. *namelen = max(*namelen, statfs.f_namelen);
  755. *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
  756. if (ovl_dentry_remote(path->dentry))
  757. *remote = true;
  758. return 0;
  759. out_put:
  760. path_put(path);
  761. out:
  762. return err;
  763. }
  764. /* Workdir should not be subdir of upperdir and vice versa */
  765. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  766. {
  767. bool ok = false;
  768. if (workdir != upperdir) {
  769. ok = (lock_rename(workdir, upperdir) == NULL);
  770. unlock_rename(workdir, upperdir);
  771. }
  772. return ok;
  773. }
  774. static unsigned int ovl_split_lowerdirs(char *str)
  775. {
  776. unsigned int ctr = 1;
  777. char *s, *d;
  778. for (s = d = str;; s++, d++) {
  779. if (*s == '\\') {
  780. s++;
  781. } else if (*s == ':') {
  782. *d = '\0';
  783. ctr++;
  784. continue;
  785. }
  786. *d = *s;
  787. if (!*s)
  788. break;
  789. }
  790. return ctr;
  791. }
  792. static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  793. {
  794. struct path upperpath = { NULL, NULL };
  795. struct path workpath = { NULL, NULL };
  796. struct dentry *root_dentry;
  797. struct ovl_entry *oe;
  798. struct ovl_fs *ufs;
  799. struct path *stack = NULL;
  800. char *lowertmp;
  801. char *lower;
  802. unsigned int numlower;
  803. unsigned int stacklen = 0;
  804. unsigned int i;
  805. bool remote = false;
  806. int err;
  807. err = -ENOMEM;
  808. ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  809. if (!ufs)
  810. goto out;
  811. err = ovl_parse_opt((char *) data, &ufs->config);
  812. if (err)
  813. goto out_free_config;
  814. err = -EINVAL;
  815. if (!ufs->config.lowerdir) {
  816. pr_err("overlayfs: missing 'lowerdir'\n");
  817. goto out_free_config;
  818. }
  819. sb->s_stack_depth = 0;
  820. sb->s_maxbytes = MAX_LFS_FILESIZE;
  821. if (ufs->config.upperdir) {
  822. if (!ufs->config.workdir) {
  823. pr_err("overlayfs: missing 'workdir'\n");
  824. goto out_free_config;
  825. }
  826. err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
  827. if (err)
  828. goto out_free_config;
  829. /* Upper fs should not be r/o */
  830. if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
  831. pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
  832. err = -EINVAL;
  833. goto out_put_upperpath;
  834. }
  835. err = ovl_mount_dir(ufs->config.workdir, &workpath);
  836. if (err)
  837. goto out_put_upperpath;
  838. err = -EINVAL;
  839. if (upperpath.mnt != workpath.mnt) {
  840. pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
  841. goto out_put_workpath;
  842. }
  843. if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
  844. pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
  845. goto out_put_workpath;
  846. }
  847. sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
  848. }
  849. err = -ENOMEM;
  850. lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
  851. if (!lowertmp)
  852. goto out_put_workpath;
  853. err = -EINVAL;
  854. stacklen = ovl_split_lowerdirs(lowertmp);
  855. if (stacklen > OVL_MAX_STACK) {
  856. pr_err("overlayfs: too many lower directries, limit is %d\n",
  857. OVL_MAX_STACK);
  858. goto out_free_lowertmp;
  859. } else if (!ufs->config.upperdir && stacklen == 1) {
  860. pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
  861. goto out_free_lowertmp;
  862. }
  863. stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
  864. if (!stack)
  865. goto out_free_lowertmp;
  866. lower = lowertmp;
  867. for (numlower = 0; numlower < stacklen; numlower++) {
  868. err = ovl_lower_dir(lower, &stack[numlower],
  869. &ufs->lower_namelen, &sb->s_stack_depth,
  870. &remote);
  871. if (err)
  872. goto out_put_lowerpath;
  873. lower = strchr(lower, '\0') + 1;
  874. }
  875. err = -EINVAL;
  876. sb->s_stack_depth++;
  877. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  878. pr_err("overlayfs: maximum fs stacking depth exceeded\n");
  879. goto out_put_lowerpath;
  880. }
  881. if (ufs->config.upperdir) {
  882. ufs->upper_mnt = clone_private_mount(&upperpath);
  883. err = PTR_ERR(ufs->upper_mnt);
  884. if (IS_ERR(ufs->upper_mnt)) {
  885. pr_err("overlayfs: failed to clone upperpath\n");
  886. goto out_put_lowerpath;
  887. }
  888. ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
  889. err = PTR_ERR(ufs->workdir);
  890. if (IS_ERR(ufs->workdir)) {
  891. pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
  892. ufs->config.workdir, OVL_WORKDIR_NAME, -err);
  893. sb->s_flags |= MS_RDONLY;
  894. ufs->workdir = NULL;
  895. }
  896. /*
  897. * Upper should support d_type, else whiteouts are visible.
  898. * Given workdir and upper are on same fs, we can do
  899. * iterate_dir() on workdir. This check requires successful
  900. * creation of workdir in previous step.
  901. */
  902. if (ufs->workdir) {
  903. err = ovl_check_d_type_supported(&workpath);
  904. if (err < 0)
  905. goto out_put_workdir;
  906. /*
  907. * We allowed this configuration and don't want to
  908. * break users over kernel upgrade. So warn instead
  909. * of erroring out.
  910. */
  911. if (!err)
  912. pr_warn("overlayfs: upper fs needs to support d_type.\n");
  913. }
  914. }
  915. err = -ENOMEM;
  916. ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
  917. if (ufs->lower_mnt == NULL)
  918. goto out_put_workdir;
  919. for (i = 0; i < numlower; i++) {
  920. struct vfsmount *mnt = clone_private_mount(&stack[i]);
  921. err = PTR_ERR(mnt);
  922. if (IS_ERR(mnt)) {
  923. pr_err("overlayfs: failed to clone lowerpath\n");
  924. goto out_put_lower_mnt;
  925. }
  926. /*
  927. * Make lower_mnt R/O. That way fchmod/fchown on lower file
  928. * will fail instead of modifying lower fs.
  929. */
  930. mnt->mnt_flags |= MNT_READONLY;
  931. ufs->lower_mnt[ufs->numlower] = mnt;
  932. ufs->numlower++;
  933. }
  934. /* If the upper fs is nonexistent, we mark overlayfs r/o too */
  935. if (!ufs->upper_mnt)
  936. sb->s_flags |= MS_RDONLY;
  937. if (remote)
  938. sb->s_d_op = &ovl_reval_dentry_operations;
  939. else
  940. sb->s_d_op = &ovl_dentry_operations;
  941. ufs->creator_cred = prepare_creds();
  942. if (!ufs->creator_cred)
  943. goto out_put_lower_mnt;
  944. err = -ENOMEM;
  945. oe = ovl_alloc_entry(numlower);
  946. if (!oe)
  947. goto out_put_cred;
  948. root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
  949. if (!root_dentry)
  950. goto out_free_oe;
  951. mntput(upperpath.mnt);
  952. for (i = 0; i < numlower; i++)
  953. mntput(stack[i].mnt);
  954. path_put(&workpath);
  955. kfree(lowertmp);
  956. oe->__upperdentry = upperpath.dentry;
  957. for (i = 0; i < numlower; i++) {
  958. oe->lowerstack[i].dentry = stack[i].dentry;
  959. oe->lowerstack[i].mnt = ufs->lower_mnt[i];
  960. }
  961. kfree(stack);
  962. root_dentry->d_fsdata = oe;
  963. ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
  964. root_dentry->d_inode);
  965. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  966. sb->s_op = &ovl_super_operations;
  967. sb->s_root = root_dentry;
  968. sb->s_fs_info = ufs;
  969. return 0;
  970. out_free_oe:
  971. kfree(oe);
  972. out_put_cred:
  973. put_cred(ufs->creator_cred);
  974. out_put_lower_mnt:
  975. for (i = 0; i < ufs->numlower; i++)
  976. mntput(ufs->lower_mnt[i]);
  977. kfree(ufs->lower_mnt);
  978. out_put_workdir:
  979. dput(ufs->workdir);
  980. mntput(ufs->upper_mnt);
  981. out_put_lowerpath:
  982. for (i = 0; i < numlower; i++)
  983. path_put(&stack[i]);
  984. kfree(stack);
  985. out_free_lowertmp:
  986. kfree(lowertmp);
  987. out_put_workpath:
  988. path_put(&workpath);
  989. out_put_upperpath:
  990. path_put(&upperpath);
  991. out_free_config:
  992. kfree(ufs->config.lowerdir);
  993. kfree(ufs->config.upperdir);
  994. kfree(ufs->config.workdir);
  995. kfree(ufs);
  996. out:
  997. return err;
  998. }
  999. static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  1000. const char *dev_name, void *raw_data)
  1001. {
  1002. return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  1003. }
  1004. static struct file_system_type ovl_fs_type = {
  1005. .owner = THIS_MODULE,
  1006. .name = "overlay",
  1007. .mount = ovl_mount,
  1008. .kill_sb = kill_anon_super,
  1009. };
  1010. MODULE_ALIAS_FS("overlay");
  1011. static int __init ovl_init(void)
  1012. {
  1013. return register_filesystem(&ovl_fs_type);
  1014. }
  1015. static void __exit ovl_exit(void)
  1016. {
  1017. unregister_filesystem(&ovl_fs_type);
  1018. }
  1019. module_init(ovl_init);
  1020. module_exit(ovl_exit);