super.c 32 KB

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