super.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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/xattr.h>
  12. #include <linux/security.h>
  13. #include <linux/mount.h>
  14. #include <linux/slab.h>
  15. #include <linux/parser.h>
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/statfs.h>
  19. #include <linux/seq_file.h>
  20. #include "overlayfs.h"
  21. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  22. MODULE_DESCRIPTION("Overlay filesystem");
  23. MODULE_LICENSE("GPL");
  24. #define OVERLAYFS_SUPER_MAGIC 0x794c7630
  25. struct ovl_config {
  26. char *lowerdir;
  27. char *upperdir;
  28. char *workdir;
  29. };
  30. /* private information held for overlayfs's superblock */
  31. struct ovl_fs {
  32. struct vfsmount *upper_mnt;
  33. struct vfsmount *lower_mnt;
  34. struct dentry *workdir;
  35. long lower_namelen;
  36. /* pathnames of lower and upper dirs, for show_options */
  37. struct ovl_config config;
  38. };
  39. struct ovl_dir_cache;
  40. /* private information held for every overlayfs dentry */
  41. struct ovl_entry {
  42. struct dentry *__upperdentry;
  43. struct dentry *lowerdentry;
  44. struct ovl_dir_cache *cache;
  45. union {
  46. struct {
  47. u64 version;
  48. bool opaque;
  49. };
  50. struct rcu_head rcu;
  51. };
  52. };
  53. const char *ovl_opaque_xattr = "trusted.overlay.opaque";
  54. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  55. {
  56. struct ovl_entry *oe = dentry->d_fsdata;
  57. if (oe->__upperdentry) {
  58. if (oe->lowerdentry) {
  59. if (S_ISDIR(dentry->d_inode->i_mode))
  60. return OVL_PATH_MERGE;
  61. else
  62. return OVL_PATH_UPPER;
  63. } else {
  64. if (oe->opaque)
  65. return OVL_PATH_UPPER;
  66. else
  67. return OVL_PATH_PURE_UPPER;
  68. }
  69. } else {
  70. return OVL_PATH_LOWER;
  71. }
  72. }
  73. static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
  74. {
  75. return lockless_dereference(oe->__upperdentry);
  76. }
  77. void ovl_path_upper(struct dentry *dentry, struct path *path)
  78. {
  79. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  80. struct ovl_entry *oe = dentry->d_fsdata;
  81. path->mnt = ofs->upper_mnt;
  82. path->dentry = ovl_upperdentry_dereference(oe);
  83. }
  84. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  85. {
  86. enum ovl_path_type type = ovl_path_type(dentry);
  87. if (type == OVL_PATH_LOWER)
  88. ovl_path_lower(dentry, path);
  89. else
  90. ovl_path_upper(dentry, path);
  91. return type;
  92. }
  93. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  94. {
  95. struct ovl_entry *oe = dentry->d_fsdata;
  96. return ovl_upperdentry_dereference(oe);
  97. }
  98. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  99. {
  100. struct ovl_entry *oe = dentry->d_fsdata;
  101. return oe->lowerdentry;
  102. }
  103. struct dentry *ovl_dentry_real(struct dentry *dentry)
  104. {
  105. struct ovl_entry *oe = dentry->d_fsdata;
  106. struct dentry *realdentry;
  107. realdentry = ovl_upperdentry_dereference(oe);
  108. if (!realdentry)
  109. realdentry = oe->lowerdentry;
  110. return realdentry;
  111. }
  112. struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
  113. {
  114. struct dentry *realdentry;
  115. realdentry = ovl_upperdentry_dereference(oe);
  116. if (realdentry) {
  117. *is_upper = true;
  118. } else {
  119. realdentry = oe->lowerdentry;
  120. *is_upper = false;
  121. }
  122. return realdentry;
  123. }
  124. struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
  125. {
  126. struct ovl_entry *oe = dentry->d_fsdata;
  127. return oe->cache;
  128. }
  129. void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
  130. {
  131. struct ovl_entry *oe = dentry->d_fsdata;
  132. oe->cache = cache;
  133. }
  134. void ovl_path_lower(struct dentry *dentry, struct path *path)
  135. {
  136. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  137. struct ovl_entry *oe = dentry->d_fsdata;
  138. path->mnt = ofs->lower_mnt;
  139. path->dentry = oe->lowerdentry;
  140. }
  141. int ovl_want_write(struct dentry *dentry)
  142. {
  143. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  144. return mnt_want_write(ofs->upper_mnt);
  145. }
  146. void ovl_drop_write(struct dentry *dentry)
  147. {
  148. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  149. mnt_drop_write(ofs->upper_mnt);
  150. }
  151. struct dentry *ovl_workdir(struct dentry *dentry)
  152. {
  153. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  154. return ofs->workdir;
  155. }
  156. bool ovl_dentry_is_opaque(struct dentry *dentry)
  157. {
  158. struct ovl_entry *oe = dentry->d_fsdata;
  159. return oe->opaque;
  160. }
  161. void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
  162. {
  163. struct ovl_entry *oe = dentry->d_fsdata;
  164. oe->opaque = opaque;
  165. }
  166. void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
  167. {
  168. struct ovl_entry *oe = dentry->d_fsdata;
  169. WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
  170. WARN_ON(oe->__upperdentry);
  171. BUG_ON(!upperdentry->d_inode);
  172. /*
  173. * Make sure upperdentry is consistent before making it visible to
  174. * ovl_upperdentry_dereference().
  175. */
  176. smp_wmb();
  177. oe->__upperdentry = upperdentry;
  178. }
  179. void ovl_dentry_version_inc(struct dentry *dentry)
  180. {
  181. struct ovl_entry *oe = dentry->d_fsdata;
  182. WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  183. oe->version++;
  184. }
  185. u64 ovl_dentry_version_get(struct dentry *dentry)
  186. {
  187. struct ovl_entry *oe = dentry->d_fsdata;
  188. WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  189. return oe->version;
  190. }
  191. bool ovl_is_whiteout(struct dentry *dentry)
  192. {
  193. struct inode *inode = dentry->d_inode;
  194. return inode && IS_WHITEOUT(inode);
  195. }
  196. static bool ovl_is_opaquedir(struct dentry *dentry)
  197. {
  198. int res;
  199. char val;
  200. struct inode *inode = dentry->d_inode;
  201. if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
  202. return false;
  203. res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
  204. if (res == 1 && val == 'y')
  205. return true;
  206. return false;
  207. }
  208. static void ovl_dentry_release(struct dentry *dentry)
  209. {
  210. struct ovl_entry *oe = dentry->d_fsdata;
  211. if (oe) {
  212. dput(oe->__upperdentry);
  213. dput(oe->lowerdentry);
  214. kfree_rcu(oe, rcu);
  215. }
  216. }
  217. static const struct dentry_operations ovl_dentry_operations = {
  218. .d_release = ovl_dentry_release,
  219. };
  220. static struct ovl_entry *ovl_alloc_entry(void)
  221. {
  222. return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
  223. }
  224. static inline struct dentry *ovl_lookup_real(struct dentry *dir,
  225. struct qstr *name)
  226. {
  227. struct dentry *dentry;
  228. mutex_lock(&dir->d_inode->i_mutex);
  229. dentry = lookup_one_len(name->name, dir, name->len);
  230. mutex_unlock(&dir->d_inode->i_mutex);
  231. if (IS_ERR(dentry)) {
  232. if (PTR_ERR(dentry) == -ENOENT)
  233. dentry = NULL;
  234. } else if (!dentry->d_inode) {
  235. dput(dentry);
  236. dentry = NULL;
  237. }
  238. return dentry;
  239. }
  240. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  241. unsigned int flags)
  242. {
  243. struct ovl_entry *oe;
  244. struct dentry *upperdir;
  245. struct dentry *lowerdir;
  246. struct dentry *upperdentry = NULL;
  247. struct dentry *lowerdentry = NULL;
  248. struct inode *inode = NULL;
  249. int err;
  250. err = -ENOMEM;
  251. oe = ovl_alloc_entry();
  252. if (!oe)
  253. goto out;
  254. upperdir = ovl_dentry_upper(dentry->d_parent);
  255. lowerdir = ovl_dentry_lower(dentry->d_parent);
  256. if (upperdir) {
  257. upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
  258. err = PTR_ERR(upperdentry);
  259. if (IS_ERR(upperdentry))
  260. goto out_put_dir;
  261. if (lowerdir && upperdentry) {
  262. if (ovl_is_whiteout(upperdentry)) {
  263. dput(upperdentry);
  264. upperdentry = NULL;
  265. oe->opaque = true;
  266. } else if (ovl_is_opaquedir(upperdentry)) {
  267. oe->opaque = true;
  268. }
  269. }
  270. }
  271. if (lowerdir && !oe->opaque) {
  272. lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
  273. err = PTR_ERR(lowerdentry);
  274. if (IS_ERR(lowerdentry))
  275. goto out_dput_upper;
  276. }
  277. if (lowerdentry && upperdentry &&
  278. (!S_ISDIR(upperdentry->d_inode->i_mode) ||
  279. !S_ISDIR(lowerdentry->d_inode->i_mode))) {
  280. dput(lowerdentry);
  281. lowerdentry = NULL;
  282. oe->opaque = true;
  283. }
  284. if (lowerdentry || upperdentry) {
  285. struct dentry *realdentry;
  286. realdentry = upperdentry ? upperdentry : lowerdentry;
  287. err = -ENOMEM;
  288. inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
  289. oe);
  290. if (!inode)
  291. goto out_dput;
  292. ovl_copyattr(realdentry->d_inode, inode);
  293. }
  294. oe->__upperdentry = upperdentry;
  295. oe->lowerdentry = lowerdentry;
  296. dentry->d_fsdata = oe;
  297. d_add(dentry, inode);
  298. return NULL;
  299. out_dput:
  300. dput(lowerdentry);
  301. out_dput_upper:
  302. dput(upperdentry);
  303. out_put_dir:
  304. kfree(oe);
  305. out:
  306. return ERR_PTR(err);
  307. }
  308. struct file *ovl_path_open(struct path *path, int flags)
  309. {
  310. return dentry_open(path, flags, current_cred());
  311. }
  312. static void ovl_put_super(struct super_block *sb)
  313. {
  314. struct ovl_fs *ufs = sb->s_fs_info;
  315. dput(ufs->workdir);
  316. mntput(ufs->upper_mnt);
  317. mntput(ufs->lower_mnt);
  318. kfree(ufs->config.lowerdir);
  319. kfree(ufs->config.upperdir);
  320. kfree(ufs->config.workdir);
  321. kfree(ufs);
  322. }
  323. /**
  324. * ovl_statfs
  325. * @sb: The overlayfs super block
  326. * @buf: The struct kstatfs to fill in with stats
  327. *
  328. * Get the filesystem statistics. As writes always target the upper layer
  329. * filesystem pass the statfs to the same filesystem.
  330. */
  331. static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  332. {
  333. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  334. struct dentry *root_dentry = dentry->d_sb->s_root;
  335. struct path path;
  336. int err;
  337. ovl_path_upper(root_dentry, &path);
  338. err = vfs_statfs(&path, buf);
  339. if (!err) {
  340. buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
  341. buf->f_type = OVERLAYFS_SUPER_MAGIC;
  342. }
  343. return err;
  344. }
  345. /**
  346. * ovl_show_options
  347. *
  348. * Prints the mount options for a given superblock.
  349. * Returns zero; does not fail.
  350. */
  351. static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  352. {
  353. struct super_block *sb = dentry->d_sb;
  354. struct ovl_fs *ufs = sb->s_fs_info;
  355. seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
  356. seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
  357. seq_printf(m, ",workdir=%s", ufs->config.workdir);
  358. return 0;
  359. }
  360. static const struct super_operations ovl_super_operations = {
  361. .put_super = ovl_put_super,
  362. .statfs = ovl_statfs,
  363. .show_options = ovl_show_options,
  364. };
  365. enum {
  366. OPT_LOWERDIR,
  367. OPT_UPPERDIR,
  368. OPT_WORKDIR,
  369. OPT_ERR,
  370. };
  371. static const match_table_t ovl_tokens = {
  372. {OPT_LOWERDIR, "lowerdir=%s"},
  373. {OPT_UPPERDIR, "upperdir=%s"},
  374. {OPT_WORKDIR, "workdir=%s"},
  375. {OPT_ERR, NULL}
  376. };
  377. static char *ovl_next_opt(char **s)
  378. {
  379. char *sbegin = *s;
  380. char *p;
  381. if (sbegin == NULL)
  382. return NULL;
  383. for (p = sbegin; *p; p++) {
  384. if (*p == '\\') {
  385. p++;
  386. if (!*p)
  387. break;
  388. } else if (*p == ',') {
  389. *p = '\0';
  390. *s = p + 1;
  391. return sbegin;
  392. }
  393. }
  394. *s = NULL;
  395. return sbegin;
  396. }
  397. static int ovl_parse_opt(char *opt, struct ovl_config *config)
  398. {
  399. char *p;
  400. while ((p = ovl_next_opt(&opt)) != NULL) {
  401. int token;
  402. substring_t args[MAX_OPT_ARGS];
  403. if (!*p)
  404. continue;
  405. token = match_token(p, ovl_tokens, args);
  406. switch (token) {
  407. case OPT_UPPERDIR:
  408. kfree(config->upperdir);
  409. config->upperdir = match_strdup(&args[0]);
  410. if (!config->upperdir)
  411. return -ENOMEM;
  412. break;
  413. case OPT_LOWERDIR:
  414. kfree(config->lowerdir);
  415. config->lowerdir = match_strdup(&args[0]);
  416. if (!config->lowerdir)
  417. return -ENOMEM;
  418. break;
  419. case OPT_WORKDIR:
  420. kfree(config->workdir);
  421. config->workdir = match_strdup(&args[0]);
  422. if (!config->workdir)
  423. return -ENOMEM;
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. }
  429. return 0;
  430. }
  431. #define OVL_WORKDIR_NAME "work"
  432. static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
  433. struct dentry *dentry)
  434. {
  435. struct inode *dir = dentry->d_inode;
  436. struct dentry *work;
  437. int err;
  438. bool retried = false;
  439. err = mnt_want_write(mnt);
  440. if (err)
  441. return ERR_PTR(err);
  442. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  443. retry:
  444. work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
  445. strlen(OVL_WORKDIR_NAME));
  446. if (!IS_ERR(work)) {
  447. struct kstat stat = {
  448. .mode = S_IFDIR | 0,
  449. };
  450. if (work->d_inode) {
  451. err = -EEXIST;
  452. if (retried)
  453. goto out_dput;
  454. retried = true;
  455. ovl_cleanup(dir, work);
  456. dput(work);
  457. goto retry;
  458. }
  459. err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
  460. if (err)
  461. goto out_dput;
  462. }
  463. out_unlock:
  464. mutex_unlock(&dir->i_mutex);
  465. mnt_drop_write(mnt);
  466. return work;
  467. out_dput:
  468. dput(work);
  469. work = ERR_PTR(err);
  470. goto out_unlock;
  471. }
  472. static void ovl_unescape(char *s)
  473. {
  474. char *d = s;
  475. for (;; s++, d++) {
  476. if (*s == '\\')
  477. s++;
  478. *d = *s;
  479. if (!*s)
  480. break;
  481. }
  482. }
  483. static int ovl_mount_dir(const char *name, struct path *path)
  484. {
  485. int err;
  486. char *tmp = kstrdup(name, GFP_KERNEL);
  487. if (!tmp)
  488. return -ENOMEM;
  489. ovl_unescape(tmp);
  490. err = kern_path(tmp, LOOKUP_FOLLOW, path);
  491. if (err) {
  492. pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
  493. err = -EINVAL;
  494. }
  495. kfree(tmp);
  496. return err;
  497. }
  498. static bool ovl_is_allowed_fs_type(struct dentry *root)
  499. {
  500. const struct dentry_operations *dop = root->d_op;
  501. /*
  502. * We don't support:
  503. * - automount filesystems
  504. * - filesystems with revalidate (FIXME for lower layer)
  505. * - filesystems with case insensitive names
  506. */
  507. if (dop &&
  508. (dop->d_manage || dop->d_automount ||
  509. dop->d_revalidate || dop->d_weak_revalidate ||
  510. dop->d_compare || dop->d_hash)) {
  511. return false;
  512. }
  513. return true;
  514. }
  515. /* Workdir should not be subdir of upperdir and vice versa */
  516. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  517. {
  518. bool ok = false;
  519. if (workdir != upperdir) {
  520. ok = (lock_rename(workdir, upperdir) == NULL);
  521. unlock_rename(workdir, upperdir);
  522. }
  523. return ok;
  524. }
  525. static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  526. {
  527. struct path lowerpath;
  528. struct path upperpath;
  529. struct path workpath;
  530. struct inode *root_inode;
  531. struct dentry *root_dentry;
  532. struct ovl_entry *oe;
  533. struct ovl_fs *ufs;
  534. struct kstatfs statfs;
  535. int err;
  536. err = -ENOMEM;
  537. ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  538. if (!ufs)
  539. goto out;
  540. err = ovl_parse_opt((char *) data, &ufs->config);
  541. if (err)
  542. goto out_free_config;
  543. /* FIXME: workdir is not needed for a R/O mount */
  544. err = -EINVAL;
  545. if (!ufs->config.upperdir || !ufs->config.lowerdir ||
  546. !ufs->config.workdir) {
  547. pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
  548. goto out_free_config;
  549. }
  550. err = -ENOMEM;
  551. oe = ovl_alloc_entry();
  552. if (oe == NULL)
  553. goto out_free_config;
  554. err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
  555. if (err)
  556. goto out_free_oe;
  557. err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
  558. if (err)
  559. goto out_put_upperpath;
  560. err = ovl_mount_dir(ufs->config.workdir, &workpath);
  561. if (err)
  562. goto out_put_lowerpath;
  563. err = -EINVAL;
  564. if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
  565. !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
  566. !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
  567. pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
  568. goto out_put_workpath;
  569. }
  570. if (upperpath.mnt != workpath.mnt) {
  571. pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
  572. goto out_put_workpath;
  573. }
  574. if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
  575. pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
  576. goto out_put_workpath;
  577. }
  578. if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
  579. pr_err("overlayfs: filesystem of upperdir is not supported\n");
  580. goto out_put_workpath;
  581. }
  582. if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
  583. pr_err("overlayfs: filesystem of lowerdir is not supported\n");
  584. goto out_put_workpath;
  585. }
  586. err = vfs_statfs(&lowerpath, &statfs);
  587. if (err) {
  588. pr_err("overlayfs: statfs failed on lowerpath\n");
  589. goto out_put_workpath;
  590. }
  591. ufs->lower_namelen = statfs.f_namelen;
  592. sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
  593. lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
  594. err = -EINVAL;
  595. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  596. pr_err("overlayfs: maximum fs stacking depth exceeded\n");
  597. goto out_put_workpath;
  598. }
  599. ufs->upper_mnt = clone_private_mount(&upperpath);
  600. err = PTR_ERR(ufs->upper_mnt);
  601. if (IS_ERR(ufs->upper_mnt)) {
  602. pr_err("overlayfs: failed to clone upperpath\n");
  603. goto out_put_workpath;
  604. }
  605. ufs->lower_mnt = clone_private_mount(&lowerpath);
  606. err = PTR_ERR(ufs->lower_mnt);
  607. if (IS_ERR(ufs->lower_mnt)) {
  608. pr_err("overlayfs: failed to clone lowerpath\n");
  609. goto out_put_upper_mnt;
  610. }
  611. ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
  612. err = PTR_ERR(ufs->workdir);
  613. if (IS_ERR(ufs->workdir)) {
  614. pr_err("overlayfs: failed to create directory %s/%s\n",
  615. ufs->config.workdir, OVL_WORKDIR_NAME);
  616. goto out_put_lower_mnt;
  617. }
  618. /*
  619. * Make lower_mnt R/O. That way fchmod/fchown on lower file
  620. * will fail instead of modifying lower fs.
  621. */
  622. ufs->lower_mnt->mnt_flags |= MNT_READONLY;
  623. /* If the upper fs is r/o, we mark overlayfs r/o too */
  624. if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
  625. sb->s_flags |= MS_RDONLY;
  626. sb->s_d_op = &ovl_dentry_operations;
  627. err = -ENOMEM;
  628. root_inode = ovl_new_inode(sb, S_IFDIR, oe);
  629. if (!root_inode)
  630. goto out_put_workdir;
  631. root_dentry = d_make_root(root_inode);
  632. if (!root_dentry)
  633. goto out_put_workdir;
  634. mntput(upperpath.mnt);
  635. mntput(lowerpath.mnt);
  636. path_put(&workpath);
  637. oe->__upperdentry = upperpath.dentry;
  638. oe->lowerdentry = lowerpath.dentry;
  639. root_dentry->d_fsdata = oe;
  640. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  641. sb->s_op = &ovl_super_operations;
  642. sb->s_root = root_dentry;
  643. sb->s_fs_info = ufs;
  644. return 0;
  645. out_put_workdir:
  646. dput(ufs->workdir);
  647. out_put_lower_mnt:
  648. mntput(ufs->lower_mnt);
  649. out_put_upper_mnt:
  650. mntput(ufs->upper_mnt);
  651. out_put_workpath:
  652. path_put(&workpath);
  653. out_put_lowerpath:
  654. path_put(&lowerpath);
  655. out_put_upperpath:
  656. path_put(&upperpath);
  657. out_free_oe:
  658. kfree(oe);
  659. out_free_config:
  660. kfree(ufs->config.lowerdir);
  661. kfree(ufs->config.upperdir);
  662. kfree(ufs->config.workdir);
  663. kfree(ufs);
  664. out:
  665. return err;
  666. }
  667. static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  668. const char *dev_name, void *raw_data)
  669. {
  670. return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  671. }
  672. static struct file_system_type ovl_fs_type = {
  673. .owner = THIS_MODULE,
  674. .name = "overlay",
  675. .mount = ovl_mount,
  676. .kill_sb = kill_anon_super,
  677. };
  678. MODULE_ALIAS_FS("overlay");
  679. static int __init ovl_init(void)
  680. {
  681. return register_filesystem(&ovl_fs_type);
  682. }
  683. static void __exit ovl_exit(void)
  684. {
  685. unregister_filesystem(&ovl_fs_type);
  686. }
  687. module_init(ovl_init);
  688. module_exit(ovl_exit);