super.c 22 KB

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