super.c 24 KB

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