super.c 30 KB

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