super.c 26 KB

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