super.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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) &&
  443. (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)))
  444. return -EROFS;
  445. return 0;
  446. }
  447. static const struct super_operations ovl_super_operations = {
  448. .put_super = ovl_put_super,
  449. .statfs = ovl_statfs,
  450. .show_options = ovl_show_options,
  451. .remount_fs = ovl_remount,
  452. };
  453. enum {
  454. OPT_LOWERDIR,
  455. OPT_UPPERDIR,
  456. OPT_WORKDIR,
  457. OPT_ERR,
  458. };
  459. static const match_table_t ovl_tokens = {
  460. {OPT_LOWERDIR, "lowerdir=%s"},
  461. {OPT_UPPERDIR, "upperdir=%s"},
  462. {OPT_WORKDIR, "workdir=%s"},
  463. {OPT_ERR, NULL}
  464. };
  465. static char *ovl_next_opt(char **s)
  466. {
  467. char *sbegin = *s;
  468. char *p;
  469. if (sbegin == NULL)
  470. return NULL;
  471. for (p = sbegin; *p; p++) {
  472. if (*p == '\\') {
  473. p++;
  474. if (!*p)
  475. break;
  476. } else if (*p == ',') {
  477. *p = '\0';
  478. *s = p + 1;
  479. return sbegin;
  480. }
  481. }
  482. *s = NULL;
  483. return sbegin;
  484. }
  485. static int ovl_parse_opt(char *opt, struct ovl_config *config)
  486. {
  487. char *p;
  488. while ((p = ovl_next_opt(&opt)) != NULL) {
  489. int token;
  490. substring_t args[MAX_OPT_ARGS];
  491. if (!*p)
  492. continue;
  493. token = match_token(p, ovl_tokens, args);
  494. switch (token) {
  495. case OPT_UPPERDIR:
  496. kfree(config->upperdir);
  497. config->upperdir = match_strdup(&args[0]);
  498. if (!config->upperdir)
  499. return -ENOMEM;
  500. break;
  501. case OPT_LOWERDIR:
  502. kfree(config->lowerdir);
  503. config->lowerdir = match_strdup(&args[0]);
  504. if (!config->lowerdir)
  505. return -ENOMEM;
  506. break;
  507. case OPT_WORKDIR:
  508. kfree(config->workdir);
  509. config->workdir = match_strdup(&args[0]);
  510. if (!config->workdir)
  511. return -ENOMEM;
  512. break;
  513. default:
  514. return -EINVAL;
  515. }
  516. }
  517. return 0;
  518. }
  519. #define OVL_WORKDIR_NAME "work"
  520. static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
  521. struct dentry *dentry)
  522. {
  523. struct inode *dir = dentry->d_inode;
  524. struct dentry *work;
  525. int err;
  526. bool retried = false;
  527. err = mnt_want_write(mnt);
  528. if (err)
  529. return ERR_PTR(err);
  530. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  531. retry:
  532. work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
  533. strlen(OVL_WORKDIR_NAME));
  534. if (!IS_ERR(work)) {
  535. struct kstat stat = {
  536. .mode = S_IFDIR | 0,
  537. };
  538. if (work->d_inode) {
  539. err = -EEXIST;
  540. if (retried)
  541. goto out_dput;
  542. retried = true;
  543. ovl_cleanup(dir, work);
  544. dput(work);
  545. goto retry;
  546. }
  547. err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
  548. if (err)
  549. goto out_dput;
  550. }
  551. out_unlock:
  552. mutex_unlock(&dir->i_mutex);
  553. mnt_drop_write(mnt);
  554. return work;
  555. out_dput:
  556. dput(work);
  557. work = ERR_PTR(err);
  558. goto out_unlock;
  559. }
  560. static void ovl_unescape(char *s)
  561. {
  562. char *d = s;
  563. for (;; s++, d++) {
  564. if (*s == '\\')
  565. s++;
  566. *d = *s;
  567. if (!*s)
  568. break;
  569. }
  570. }
  571. static bool ovl_is_allowed_fs_type(struct dentry *root)
  572. {
  573. const struct dentry_operations *dop = root->d_op;
  574. /*
  575. * We don't support:
  576. * - automount filesystems
  577. * - filesystems with revalidate (FIXME for lower layer)
  578. * - filesystems with case insensitive names
  579. */
  580. if (dop &&
  581. (dop->d_manage || dop->d_automount ||
  582. dop->d_revalidate || dop->d_weak_revalidate ||
  583. dop->d_compare || dop->d_hash)) {
  584. return false;
  585. }
  586. return true;
  587. }
  588. static int ovl_mount_dir_noesc(const char *name, struct path *path)
  589. {
  590. int err = -EINVAL;
  591. if (!*name) {
  592. pr_err("overlayfs: empty lowerdir\n");
  593. goto out;
  594. }
  595. err = kern_path(name, LOOKUP_FOLLOW, path);
  596. if (err) {
  597. pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
  598. goto out;
  599. }
  600. err = -EINVAL;
  601. if (!ovl_is_allowed_fs_type(path->dentry)) {
  602. pr_err("overlayfs: filesystem on '%s' not supported\n", name);
  603. goto out_put;
  604. }
  605. if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
  606. pr_err("overlayfs: '%s' not a directory\n", name);
  607. goto out_put;
  608. }
  609. return 0;
  610. out_put:
  611. path_put(path);
  612. out:
  613. return err;
  614. }
  615. static int ovl_mount_dir(const char *name, struct path *path)
  616. {
  617. int err = -ENOMEM;
  618. char *tmp = kstrdup(name, GFP_KERNEL);
  619. if (tmp) {
  620. ovl_unescape(tmp);
  621. err = ovl_mount_dir_noesc(tmp, path);
  622. kfree(tmp);
  623. }
  624. return err;
  625. }
  626. static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
  627. int *stack_depth)
  628. {
  629. int err;
  630. struct kstatfs statfs;
  631. err = ovl_mount_dir_noesc(name, path);
  632. if (err)
  633. goto out;
  634. err = vfs_statfs(path, &statfs);
  635. if (err) {
  636. pr_err("overlayfs: statfs failed on '%s'\n", name);
  637. goto out_put;
  638. }
  639. *namelen = max(*namelen, statfs.f_namelen);
  640. *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
  641. return 0;
  642. out_put:
  643. path_put(path);
  644. out:
  645. return err;
  646. }
  647. /* Workdir should not be subdir of upperdir and vice versa */
  648. static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
  649. {
  650. bool ok = false;
  651. if (workdir != upperdir) {
  652. ok = (lock_rename(workdir, upperdir) == NULL);
  653. unlock_rename(workdir, upperdir);
  654. }
  655. return ok;
  656. }
  657. static unsigned int ovl_split_lowerdirs(char *str)
  658. {
  659. unsigned int ctr = 1;
  660. char *s, *d;
  661. for (s = d = str;; s++, d++) {
  662. if (*s == '\\') {
  663. s++;
  664. } else if (*s == ':') {
  665. *d = '\0';
  666. ctr++;
  667. continue;
  668. }
  669. *d = *s;
  670. if (!*s)
  671. break;
  672. }
  673. return ctr;
  674. }
  675. static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  676. {
  677. struct path upperpath = { NULL, NULL };
  678. struct path workpath = { NULL, NULL };
  679. struct dentry *root_dentry;
  680. struct ovl_entry *oe;
  681. struct ovl_fs *ufs;
  682. struct path *stack = NULL;
  683. char *lowertmp;
  684. char *lower;
  685. unsigned int numlower;
  686. unsigned int stacklen = 0;
  687. unsigned int i;
  688. int err;
  689. err = -ENOMEM;
  690. ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  691. if (!ufs)
  692. goto out;
  693. err = ovl_parse_opt((char *) data, &ufs->config);
  694. if (err)
  695. goto out_free_config;
  696. err = -EINVAL;
  697. if (!ufs->config.lowerdir) {
  698. pr_err("overlayfs: missing 'lowerdir'\n");
  699. goto out_free_config;
  700. }
  701. sb->s_stack_depth = 0;
  702. if (ufs->config.upperdir) {
  703. /* FIXME: workdir is not needed for a R/O mount */
  704. if (!ufs->config.workdir) {
  705. pr_err("overlayfs: missing 'workdir'\n");
  706. goto out_free_config;
  707. }
  708. err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
  709. if (err)
  710. goto out_free_config;
  711. err = ovl_mount_dir(ufs->config.workdir, &workpath);
  712. if (err)
  713. goto out_put_upperpath;
  714. err = -EINVAL;
  715. if (upperpath.mnt != workpath.mnt) {
  716. pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
  717. goto out_put_workpath;
  718. }
  719. if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
  720. pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
  721. goto out_put_workpath;
  722. }
  723. sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
  724. }
  725. err = -ENOMEM;
  726. lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
  727. if (!lowertmp)
  728. goto out_put_workpath;
  729. err = -EINVAL;
  730. stacklen = ovl_split_lowerdirs(lowertmp);
  731. if (stacklen > OVL_MAX_STACK)
  732. goto out_free_lowertmp;
  733. stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
  734. if (!stack)
  735. goto out_free_lowertmp;
  736. lower = lowertmp;
  737. for (numlower = 0; numlower < stacklen; numlower++) {
  738. err = ovl_lower_dir(lower, &stack[numlower],
  739. &ufs->lower_namelen, &sb->s_stack_depth);
  740. if (err)
  741. goto out_put_lowerpath;
  742. lower = strchr(lower, '\0') + 1;
  743. }
  744. err = -EINVAL;
  745. sb->s_stack_depth++;
  746. if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  747. pr_err("overlayfs: maximum fs stacking depth exceeded\n");
  748. goto out_put_lowerpath;
  749. }
  750. if (ufs->config.upperdir) {
  751. ufs->upper_mnt = clone_private_mount(&upperpath);
  752. err = PTR_ERR(ufs->upper_mnt);
  753. if (IS_ERR(ufs->upper_mnt)) {
  754. pr_err("overlayfs: failed to clone upperpath\n");
  755. goto out_put_lowerpath;
  756. }
  757. ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
  758. err = PTR_ERR(ufs->workdir);
  759. if (IS_ERR(ufs->workdir)) {
  760. pr_err("overlayfs: failed to create directory %s/%s\n",
  761. ufs->config.workdir, OVL_WORKDIR_NAME);
  762. goto out_put_upper_mnt;
  763. }
  764. }
  765. err = -ENOMEM;
  766. ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
  767. if (ufs->lower_mnt == NULL)
  768. goto out_put_workdir;
  769. for (i = 0; i < numlower; i++) {
  770. struct vfsmount *mnt = clone_private_mount(&stack[i]);
  771. err = PTR_ERR(mnt);
  772. if (IS_ERR(mnt)) {
  773. pr_err("overlayfs: failed to clone lowerpath\n");
  774. goto out_put_lower_mnt;
  775. }
  776. /*
  777. * Make lower_mnt R/O. That way fchmod/fchown on lower file
  778. * will fail instead of modifying lower fs.
  779. */
  780. mnt->mnt_flags |= MNT_READONLY;
  781. ufs->lower_mnt[ufs->numlower] = mnt;
  782. ufs->numlower++;
  783. }
  784. /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
  785. if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
  786. sb->s_flags |= MS_RDONLY;
  787. sb->s_d_op = &ovl_dentry_operations;
  788. err = -ENOMEM;
  789. oe = ovl_alloc_entry(numlower);
  790. if (!oe)
  791. goto out_put_lower_mnt;
  792. root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
  793. if (!root_dentry)
  794. goto out_free_oe;
  795. mntput(upperpath.mnt);
  796. for (i = 0; i < numlower; i++)
  797. mntput(stack[i].mnt);
  798. path_put(&workpath);
  799. kfree(lowertmp);
  800. oe->__upperdentry = upperpath.dentry;
  801. for (i = 0; i < numlower; i++) {
  802. oe->lowerstack[i].dentry = stack[i].dentry;
  803. oe->lowerstack[i].mnt = ufs->lower_mnt[i];
  804. }
  805. root_dentry->d_fsdata = oe;
  806. sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  807. sb->s_op = &ovl_super_operations;
  808. sb->s_root = root_dentry;
  809. sb->s_fs_info = ufs;
  810. return 0;
  811. out_free_oe:
  812. kfree(oe);
  813. out_put_lower_mnt:
  814. for (i = 0; i < ufs->numlower; i++)
  815. mntput(ufs->lower_mnt[i]);
  816. kfree(ufs->lower_mnt);
  817. out_put_workdir:
  818. dput(ufs->workdir);
  819. out_put_upper_mnt:
  820. mntput(ufs->upper_mnt);
  821. out_put_lowerpath:
  822. for (i = 0; i < numlower; i++)
  823. path_put(&stack[i]);
  824. kfree(stack);
  825. out_free_lowertmp:
  826. kfree(lowertmp);
  827. out_put_workpath:
  828. path_put(&workpath);
  829. out_put_upperpath:
  830. path_put(&upperpath);
  831. out_free_config:
  832. kfree(ufs->config.lowerdir);
  833. kfree(ufs->config.upperdir);
  834. kfree(ufs->config.workdir);
  835. kfree(ufs);
  836. out:
  837. return err;
  838. }
  839. static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  840. const char *dev_name, void *raw_data)
  841. {
  842. return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  843. }
  844. static struct file_system_type ovl_fs_type = {
  845. .owner = THIS_MODULE,
  846. .name = "overlay",
  847. .mount = ovl_mount,
  848. .kill_sb = kill_anon_super,
  849. };
  850. MODULE_ALIAS_FS("overlay");
  851. static int __init ovl_init(void)
  852. {
  853. return register_filesystem(&ovl_fs_type);
  854. }
  855. static void __exit ovl_exit(void)
  856. {
  857. unregister_filesystem(&ovl_fs_type);
  858. }
  859. module_init(ovl_init);
  860. module_exit(ovl_exit);