super.c 24 KB

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