super.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 const struct dentry_operations ovl_dentry_operations = {
  223. .d_release = ovl_dentry_release,
  224. };
  225. static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  226. {
  227. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  228. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  229. if (oe)
  230. oe->numlower = numlower;
  231. return oe;
  232. }
  233. static inline struct dentry *ovl_lookup_real(struct dentry *dir,
  234. struct qstr *name)
  235. {
  236. struct dentry *dentry;
  237. mutex_lock(&dir->d_inode->i_mutex);
  238. dentry = lookup_one_len(name->name, dir, name->len);
  239. mutex_unlock(&dir->d_inode->i_mutex);
  240. if (IS_ERR(dentry)) {
  241. if (PTR_ERR(dentry) == -ENOENT)
  242. dentry = NULL;
  243. } else if (!dentry->d_inode) {
  244. dput(dentry);
  245. dentry = NULL;
  246. }
  247. return dentry;
  248. }
  249. /*
  250. * Returns next layer in stack starting from top.
  251. * Returns -1 if this is the last layer.
  252. */
  253. int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
  254. {
  255. struct ovl_entry *oe = dentry->d_fsdata;
  256. BUG_ON(idx < 0);
  257. if (idx == 0) {
  258. ovl_path_upper(dentry, path);
  259. if (path->dentry)
  260. return oe->numlower ? 1 : -1;
  261. idx++;
  262. }
  263. BUG_ON(idx > oe->numlower);
  264. *path = oe->lowerstack[idx - 1];
  265. return (idx < oe->numlower) ? idx + 1 : -1;
  266. }
  267. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  268. unsigned int flags)
  269. {
  270. struct ovl_entry *oe;
  271. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  272. struct path *stack = NULL;
  273. struct dentry *upperdir, *upperdentry = NULL;
  274. unsigned int ctr = 0;
  275. struct inode *inode = NULL;
  276. bool upperopaque = false;
  277. struct dentry *this, *prev = NULL;
  278. unsigned int i;
  279. int err;
  280. upperdir = ovl_upperdentry_dereference(poe);
  281. if (upperdir) {
  282. this = ovl_lookup_real(upperdir, &dentry->d_name);
  283. err = PTR_ERR(this);
  284. if (IS_ERR(this))
  285. goto out;
  286. if (this) {
  287. if (ovl_is_whiteout(this)) {
  288. dput(this);
  289. this = NULL;
  290. upperopaque = true;
  291. } else if (poe->numlower && ovl_is_opaquedir(this)) {
  292. upperopaque = true;
  293. }
  294. }
  295. upperdentry = prev = this;
  296. }
  297. if (!upperopaque && poe->numlower) {
  298. err = -ENOMEM;
  299. stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
  300. if (!stack)
  301. goto out_put_upper;
  302. }
  303. for (i = 0; !upperopaque && i < poe->numlower; i++) {
  304. bool opaque = false;
  305. struct path lowerpath = poe->lowerstack[i];
  306. opaque = false;
  307. this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
  308. err = PTR_ERR(this);
  309. if (IS_ERR(this)) {
  310. /*
  311. * If it's positive, then treat ENAMETOOLONG as ENOENT.
  312. */
  313. if (err == -ENAMETOOLONG && (upperdentry || ctr))
  314. continue;
  315. goto out_put;
  316. }
  317. if (!this)
  318. continue;
  319. if (ovl_is_whiteout(this)) {
  320. dput(this);
  321. break;
  322. }
  323. /*
  324. * Only makes sense to check opaque dir if this is not the
  325. * lowermost layer.
  326. */
  327. if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
  328. opaque = true;
  329. /*
  330. * If this is a non-directory then stop here.
  331. *
  332. * FIXME: check for opaqueness maybe better done in remove code.
  333. */
  334. if (!S_ISDIR(this->d_inode->i_mode)) {
  335. opaque = true;
  336. } else if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
  337. !S_ISDIR(this->d_inode->i_mode))) {
  338. if (prev == upperdentry)
  339. upperopaque = true;
  340. dput(this);
  341. break;
  342. }
  343. stack[ctr].dentry = this;
  344. stack[ctr].mnt = lowerpath.mnt;
  345. ctr++;
  346. prev = this;
  347. if (opaque)
  348. break;
  349. }
  350. oe = ovl_alloc_entry(ctr);
  351. err = -ENOMEM;
  352. if (!oe)
  353. goto out_put;
  354. if (upperdentry || ctr) {
  355. struct dentry *realdentry;
  356. realdentry = upperdentry ? upperdentry : stack[0].dentry;
  357. err = -ENOMEM;
  358. inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
  359. oe);
  360. if (!inode)
  361. goto out_free_oe;
  362. ovl_copyattr(realdentry->d_inode, inode);
  363. }
  364. oe->opaque = upperopaque;
  365. oe->__upperdentry = upperdentry;
  366. memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
  367. kfree(stack);
  368. dentry->d_fsdata = oe;
  369. d_add(dentry, inode);
  370. return NULL;
  371. out_free_oe:
  372. kfree(oe);
  373. out_put:
  374. for (i = 0; i < ctr; i++)
  375. dput(stack[i].dentry);
  376. kfree(stack);
  377. out_put_upper:
  378. dput(upperdentry);
  379. out:
  380. return ERR_PTR(err);
  381. }
  382. struct file *ovl_path_open(struct path *path, int flags)
  383. {
  384. return dentry_open(path, flags, current_cred());
  385. }
  386. static void ovl_put_super(struct super_block *sb)
  387. {
  388. struct ovl_fs *ufs = sb->s_fs_info;
  389. unsigned i;
  390. dput(ufs->workdir);
  391. mntput(ufs->upper_mnt);
  392. for (i = 0; i < ufs->numlower; i++)
  393. mntput(ufs->lower_mnt[i]);
  394. kfree(ufs->config.lowerdir);
  395. kfree(ufs->config.upperdir);
  396. kfree(ufs->config.workdir);
  397. kfree(ufs);
  398. }
  399. /**
  400. * ovl_statfs
  401. * @sb: The overlayfs super block
  402. * @buf: The struct kstatfs to fill in with stats
  403. *
  404. * Get the filesystem statistics. As writes always target the upper layer
  405. * filesystem pass the statfs to the upper filesystem (if it exists)
  406. */
  407. static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  408. {
  409. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  410. struct dentry *root_dentry = dentry->d_sb->s_root;
  411. struct path path;
  412. int err;
  413. ovl_path_real(root_dentry, &path);
  414. err = vfs_statfs(&path, buf);
  415. if (!err) {
  416. buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
  417. buf->f_type = OVERLAYFS_SUPER_MAGIC;
  418. }
  419. return err;
  420. }
  421. /**
  422. * ovl_show_options
  423. *
  424. * Prints the mount options for a given superblock.
  425. * Returns zero; does not fail.
  426. */
  427. static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  428. {
  429. struct super_block *sb = dentry->d_sb;
  430. struct ovl_fs *ufs = sb->s_fs_info;
  431. seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
  432. if (ufs->config.upperdir) {
  433. seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
  434. seq_printf(m, ",workdir=%s", ufs->config.workdir);
  435. }
  436. return 0;
  437. }
  438. static const struct super_operations ovl_super_operations = {
  439. .put_super = ovl_put_super,
  440. .statfs = ovl_statfs,
  441. .show_options = ovl_show_options,
  442. };
  443. enum {
  444. OPT_LOWERDIR,
  445. OPT_UPPERDIR,
  446. OPT_WORKDIR,
  447. OPT_ERR,
  448. };
  449. static const match_table_t ovl_tokens = {
  450. {OPT_LOWERDIR, "lowerdir=%s"},
  451. {OPT_UPPERDIR, "upperdir=%s"},
  452. {OPT_WORKDIR, "workdir=%s"},
  453. {OPT_ERR, NULL}
  454. };
  455. static char *ovl_next_opt(char **s)
  456. {
  457. char *sbegin = *s;
  458. char *p;
  459. if (sbegin == NULL)
  460. return NULL;
  461. for (p = sbegin; *p; p++) {
  462. if (*p == '\\') {
  463. p++;
  464. if (!*p)
  465. break;
  466. } else if (*p == ',') {
  467. *p = '\0';
  468. *s = p + 1;
  469. return sbegin;
  470. }
  471. }
  472. *s = NULL;
  473. return sbegin;
  474. }
  475. static int ovl_parse_opt(char *opt, struct ovl_config *config)
  476. {
  477. char *p;
  478. while ((p = ovl_next_opt(&opt)) != NULL) {
  479. int token;
  480. substring_t args[MAX_OPT_ARGS];
  481. if (!*p)
  482. continue;
  483. token = match_token(p, ovl_tokens, args);
  484. switch (token) {
  485. case OPT_UPPERDIR:
  486. kfree(config->upperdir);
  487. config->upperdir = match_strdup(&args[0]);
  488. if (!config->upperdir)
  489. return -ENOMEM;
  490. break;
  491. case OPT_LOWERDIR:
  492. kfree(config->lowerdir);
  493. config->lowerdir = match_strdup(&args[0]);
  494. if (!config->lowerdir)
  495. return -ENOMEM;
  496. break;
  497. case OPT_WORKDIR:
  498. kfree(config->workdir);
  499. config->workdir = match_strdup(&args[0]);
  500. if (!config->workdir)
  501. return -ENOMEM;
  502. break;
  503. default:
  504. return -EINVAL;
  505. }
  506. }
  507. return 0;
  508. }
  509. #define OVL_WORKDIR_NAME "work"
  510. static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
  511. struct dentry *dentry)
  512. {
  513. struct inode *dir = dentry->d_inode;
  514. struct dentry *work;
  515. int err;
  516. bool retried = false;
  517. err = mnt_want_write(mnt);
  518. if (err)
  519. return ERR_PTR(err);
  520. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  521. retry:
  522. work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
  523. strlen(OVL_WORKDIR_NAME));
  524. if (!IS_ERR(work)) {
  525. struct kstat stat = {
  526. .mode = S_IFDIR | 0,
  527. };
  528. if (work->d_inode) {
  529. err = -EEXIST;
  530. if (retried)
  531. goto out_dput;
  532. retried = true;
  533. ovl_cleanup(dir, work);
  534. dput(work);
  535. goto retry;
  536. }
  537. err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
  538. if (err)
  539. goto out_dput;
  540. }
  541. out_unlock:
  542. mutex_unlock(&dir->i_mutex);
  543. mnt_drop_write(mnt);
  544. return work;
  545. out_dput:
  546. dput(work);
  547. work = ERR_PTR(err);
  548. goto out_unlock;
  549. }
  550. static void ovl_unescape(char *s)
  551. {
  552. char *d = s;
  553. for (;; s++, d++) {
  554. if (*s == '\\')
  555. s++;
  556. *d = *s;
  557. if (!*s)
  558. break;
  559. }
  560. }
  561. static bool ovl_is_allowed_fs_type(struct dentry *root)
  562. {
  563. const struct dentry_operations *dop = root->d_op;
  564. /*
  565. * We don't support:
  566. * - automount filesystems
  567. * - filesystems with revalidate (FIXME for lower layer)
  568. * - filesystems with case insensitive names
  569. */
  570. if (dop &&
  571. (dop->d_manage || dop->d_automount ||
  572. dop->d_revalidate || dop->d_weak_revalidate ||
  573. dop->d_compare || dop->d_hash)) {
  574. return false;
  575. }
  576. return true;
  577. }
  578. static int ovl_mount_dir_noesc(const char *name, struct path *path)
  579. {
  580. int err = -EINVAL;
  581. if (!*name) {
  582. pr_err("overlayfs: empty lowerdir\n");
  583. goto out;
  584. }
  585. err = kern_path(name, LOOKUP_FOLLOW, path);
  586. if (err) {
  587. pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
  588. goto out;
  589. }
  590. err = -EINVAL;
  591. if (!ovl_is_allowed_fs_type(path->dentry)) {
  592. pr_err("overlayfs: filesystem on '%s' not supported\n", name);
  593. goto out_put;
  594. }
  595. if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
  596. pr_err("overlayfs: '%s' not a directory\n", name);
  597. goto out_put;
  598. }
  599. return 0;
  600. out_put:
  601. path_put(path);
  602. out:
  603. return err;
  604. }
  605. static int ovl_mount_dir(const char *name, struct path *path)
  606. {
  607. int err = -ENOMEM;
  608. char *tmp = kstrdup(name, GFP_KERNEL);
  609. if (tmp) {
  610. ovl_unescape(tmp);
  611. err = ovl_mount_dir_noesc(tmp, path);
  612. kfree(tmp);
  613. }
  614. return err;
  615. }
  616. static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
  617. int *stack_depth)
  618. {
  619. int err;
  620. struct kstatfs statfs;
  621. err = ovl_mount_dir_noesc(name, path);
  622. if (err)
  623. goto out;
  624. err = vfs_statfs(path, &statfs);
  625. if (err) {
  626. pr_err("overlayfs: statfs failed on '%s'\n", name);
  627. goto out_put;
  628. }
  629. *namelen = max(*namelen, statfs.f_namelen);
  630. *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
  631. return 0;
  632. out_put:
  633. path_put(path);
  634. out:
  635. return err;
  636. }
  637. /* Workdir should not be subdir of upperdir and vice versa */
  638. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  639. {
  640. bool ok = false;
  641. if (workdir != upperdir) {
  642. ok = (lock_rename(workdir, upperdir) == NULL);
  643. unlock_rename(workdir, upperdir);
  644. }
  645. return ok;
  646. }
  647. static unsigned int ovl_split_lowerdirs(char *str)
  648. {
  649. unsigned int ctr = 1;
  650. char *s, *d;
  651. for (s = d = str;; s++, d++) {
  652. if (*s == '\\') {
  653. s++;
  654. } else if (*s == ':') {
  655. *d = '\0';
  656. ctr++;
  657. continue;
  658. }
  659. *d = *s;
  660. if (!*s)
  661. break;
  662. }
  663. return ctr;
  664. }
  665. static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  666. {
  667. struct path upperpath = { NULL, NULL };
  668. struct path workpath = { NULL, NULL };
  669. struct dentry *root_dentry;
  670. struct ovl_entry *oe;
  671. struct ovl_fs *ufs;
  672. struct path *stack = NULL;
  673. char *lowertmp;
  674. char *lower;
  675. unsigned int numlower;
  676. unsigned int stacklen = 0;
  677. unsigned int i;
  678. int err;
  679. err = -ENOMEM;
  680. ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  681. if (!ufs)
  682. goto out;
  683. err = ovl_parse_opt((char *) data, &ufs->config);
  684. if (err)
  685. goto out_free_config;
  686. err = -EINVAL;
  687. if (!ufs->config.lowerdir) {
  688. pr_err("overlayfs: missing 'lowerdir'\n");
  689. goto out_free_config;
  690. }
  691. sb->s_stack_depth = 0;
  692. if (ufs->config.upperdir) {
  693. /* FIXME: workdir is not needed for a R/O mount */
  694. if (!ufs->config.workdir) {
  695. pr_err("overlayfs: missing 'workdir'\n");
  696. goto out_free_config;
  697. }
  698. err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
  699. if (err)
  700. goto out_free_config;
  701. err = ovl_mount_dir(ufs->config.workdir, &workpath);
  702. if (err)
  703. goto out_put_upperpath;
  704. if (upperpath.mnt != workpath.mnt) {
  705. pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
  706. goto out_put_workpath;
  707. }
  708. if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
  709. pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
  710. goto out_put_workpath;
  711. }
  712. sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
  713. }
  714. err = -ENOMEM;
  715. lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
  716. if (!lowertmp)
  717. goto out_put_workpath;
  718. err = -EINVAL;
  719. stacklen = ovl_split_lowerdirs(lowertmp);
  720. if (stacklen > OVL_MAX_STACK)
  721. goto out_free_lowertmp;
  722. stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
  723. if (!stack)
  724. goto out_free_lowertmp;
  725. lower = lowertmp;
  726. for (numlower = 0; numlower < stacklen; numlower++) {
  727. err = ovl_lower_dir(lower, &stack[numlower],
  728. &ufs->lower_namelen, &sb->s_stack_depth);
  729. if (err)
  730. goto out_put_lowerpath;
  731. lower = strchr(lower, '\0') + 1;
  732. }
  733. err = -EINVAL;
  734. sb->s_stack_depth++;
  735. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  736. pr_err("overlayfs: maximum fs stacking depth exceeded\n");
  737. goto out_put_lowerpath;
  738. }
  739. if (ufs->config.upperdir) {
  740. ufs->upper_mnt = clone_private_mount(&upperpath);
  741. err = PTR_ERR(ufs->upper_mnt);
  742. if (IS_ERR(ufs->upper_mnt)) {
  743. pr_err("overlayfs: failed to clone upperpath\n");
  744. goto out_put_lowerpath;
  745. }
  746. ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
  747. err = PTR_ERR(ufs->workdir);
  748. if (IS_ERR(ufs->workdir)) {
  749. pr_err("overlayfs: failed to create directory %s/%s\n",
  750. ufs->config.workdir, OVL_WORKDIR_NAME);
  751. goto out_put_upper_mnt;
  752. }
  753. }
  754. ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
  755. if (ufs->lower_mnt == NULL)
  756. goto out_put_workdir;
  757. for (i = 0; i < numlower; i++) {
  758. struct vfsmount *mnt = clone_private_mount(&stack[i]);
  759. if (IS_ERR(mnt)) {
  760. pr_err("overlayfs: failed to clone lowerpath\n");
  761. goto out_put_lower_mnt;
  762. }
  763. /*
  764. * Make lower_mnt R/O. That way fchmod/fchown on lower file
  765. * will fail instead of modifying lower fs.
  766. */
  767. mnt->mnt_flags |= MNT_READONLY;
  768. ufs->lower_mnt[ufs->numlower] = mnt;
  769. ufs->numlower++;
  770. }
  771. /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
  772. if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
  773. sb->s_flags |= MS_RDONLY;
  774. sb->s_d_op = &ovl_dentry_operations;
  775. err = -ENOMEM;
  776. oe = ovl_alloc_entry(numlower);
  777. if (!oe)
  778. goto out_put_lower_mnt;
  779. root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
  780. if (!root_dentry)
  781. goto out_free_oe;
  782. mntput(upperpath.mnt);
  783. for (i = 0; i < numlower; i++)
  784. mntput(stack[i].mnt);
  785. path_put(&workpath);
  786. kfree(lowertmp);
  787. oe->__upperdentry = upperpath.dentry;
  788. for (i = 0; i < numlower; i++) {
  789. oe->lowerstack[i].dentry = stack[i].dentry;
  790. oe->lowerstack[i].mnt = ufs->lower_mnt[i];
  791. }
  792. root_dentry->d_fsdata = oe;
  793. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  794. sb->s_op = &ovl_super_operations;
  795. sb->s_root = root_dentry;
  796. sb->s_fs_info = ufs;
  797. return 0;
  798. out_free_oe:
  799. kfree(oe);
  800. out_put_lower_mnt:
  801. for (i = 0; i < ufs->numlower; i++)
  802. mntput(ufs->lower_mnt[i]);
  803. kfree(ufs->lower_mnt);
  804. out_put_workdir:
  805. dput(ufs->workdir);
  806. out_put_upper_mnt:
  807. mntput(ufs->upper_mnt);
  808. out_put_lowerpath:
  809. for (i = 0; i < numlower; i++)
  810. path_put(&stack[i]);
  811. kfree(stack);
  812. out_free_lowertmp:
  813. kfree(lowertmp);
  814. out_put_workpath:
  815. path_put(&workpath);
  816. out_put_upperpath:
  817. path_put(&upperpath);
  818. out_free_config:
  819. kfree(ufs->config.lowerdir);
  820. kfree(ufs->config.upperdir);
  821. kfree(ufs->config.workdir);
  822. kfree(ufs);
  823. out:
  824. return err;
  825. }
  826. static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  827. const char *dev_name, void *raw_data)
  828. {
  829. return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  830. }
  831. static struct file_system_type ovl_fs_type = {
  832. .owner = THIS_MODULE,
  833. .name = "overlay",
  834. .mount = ovl_mount,
  835. .kill_sb = kill_anon_super,
  836. };
  837. MODULE_ALIAS_FS("overlay");
  838. static int __init ovl_init(void)
  839. {
  840. return register_filesystem(&ovl_fs_type);
  841. }
  842. static void __exit ovl_exit(void)
  843. {
  844. unregister_filesystem(&ovl_fs_type);
  845. }
  846. module_init(ovl_init);
  847. module_exit(ovl_exit);