super.c 24 KB

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