util.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Novell Inc.
  4. * Copyright (C) 2016 Red Hat, Inc.
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/mount.h>
  8. #include <linux/slab.h>
  9. #include <linux/cred.h>
  10. #include <linux/xattr.h>
  11. #include <linux/exportfs.h>
  12. #include <linux/file.h>
  13. #include <linux/fileattr.h>
  14. #include <linux/uuid.h>
  15. #include <linux/namei.h>
  16. #include <linux/ratelimit.h>
  17. #include "overlayfs.h"
  18. /* Get write access to upper mnt - may fail if upper sb was remounted ro */
  19. int ovl_get_write_access(struct dentry *dentry)
  20. {
  21. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  22. return mnt_get_write_access(ovl_upper_mnt(ofs));
  23. }
  24. /* Get write access to upper sb - may block if upper sb is frozen */
  25. void ovl_start_write(struct dentry *dentry)
  26. {
  27. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  28. sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
  29. }
  30. int ovl_want_write(struct dentry *dentry)
  31. {
  32. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  33. return mnt_want_write(ovl_upper_mnt(ofs));
  34. }
  35. void ovl_put_write_access(struct dentry *dentry)
  36. {
  37. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  38. mnt_put_write_access(ovl_upper_mnt(ofs));
  39. }
  40. void ovl_end_write(struct dentry *dentry)
  41. {
  42. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  43. sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
  44. }
  45. void ovl_drop_write(struct dentry *dentry)
  46. {
  47. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  48. mnt_drop_write(ovl_upper_mnt(ofs));
  49. }
  50. struct dentry *ovl_workdir(struct dentry *dentry)
  51. {
  52. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  53. return ofs->workdir;
  54. }
  55. const struct cred *ovl_override_creds(struct super_block *sb)
  56. {
  57. struct ovl_fs *ofs = OVL_FS(sb);
  58. return override_creds(ofs->creator_cred);
  59. }
  60. /*
  61. * Check if underlying fs supports file handles and try to determine encoding
  62. * type, in order to deduce maximum inode number used by fs.
  63. *
  64. * Return 0 if file handles are not supported.
  65. * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
  66. * Return -1 if fs uses a non default encoding with unknown inode size.
  67. */
  68. int ovl_can_decode_fh(struct super_block *sb)
  69. {
  70. if (!capable(CAP_DAC_READ_SEARCH))
  71. return 0;
  72. if (!exportfs_can_decode_fh(sb->s_export_op))
  73. return 0;
  74. return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
  75. }
  76. struct dentry *ovl_indexdir(struct super_block *sb)
  77. {
  78. struct ovl_fs *ofs = OVL_FS(sb);
  79. return ofs->config.index ? ofs->workdir : NULL;
  80. }
  81. /* Index all files on copy up. For now only enabled for NFS export */
  82. bool ovl_index_all(struct super_block *sb)
  83. {
  84. struct ovl_fs *ofs = OVL_FS(sb);
  85. return ofs->config.nfs_export && ofs->config.index;
  86. }
  87. /* Verify lower origin on lookup. For now only enabled for NFS export */
  88. bool ovl_verify_lower(struct super_block *sb)
  89. {
  90. struct ovl_fs *ofs = OVL_FS(sb);
  91. return ofs->config.nfs_export && ofs->config.index;
  92. }
  93. struct ovl_path *ovl_stack_alloc(unsigned int n)
  94. {
  95. return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
  96. }
  97. void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
  98. {
  99. unsigned int i;
  100. memcpy(dst, src, sizeof(struct ovl_path) * n);
  101. for (i = 0; i < n; i++)
  102. dget(src[i].dentry);
  103. }
  104. void ovl_stack_put(struct ovl_path *stack, unsigned int n)
  105. {
  106. unsigned int i;
  107. for (i = 0; stack && i < n; i++)
  108. dput(stack[i].dentry);
  109. }
  110. void ovl_stack_free(struct ovl_path *stack, unsigned int n)
  111. {
  112. ovl_stack_put(stack, n);
  113. kfree(stack);
  114. }
  115. struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  116. {
  117. size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]);
  118. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  119. if (oe)
  120. oe->__numlower = numlower;
  121. return oe;
  122. }
  123. void ovl_free_entry(struct ovl_entry *oe)
  124. {
  125. ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
  126. kfree(oe);
  127. }
  128. #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
  129. bool ovl_dentry_remote(struct dentry *dentry)
  130. {
  131. return dentry->d_flags & OVL_D_REVALIDATE;
  132. }
  133. void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
  134. {
  135. if (!ovl_dentry_remote(realdentry))
  136. return;
  137. spin_lock(&dentry->d_lock);
  138. dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
  139. spin_unlock(&dentry->d_lock);
  140. }
  141. void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
  142. struct ovl_entry *oe)
  143. {
  144. return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
  145. }
  146. void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
  147. struct ovl_entry *oe, unsigned int mask)
  148. {
  149. struct ovl_path *lowerstack = ovl_lowerstack(oe);
  150. unsigned int i, flags = 0;
  151. if (upperdentry)
  152. flags |= upperdentry->d_flags;
  153. for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
  154. flags |= lowerstack[i].dentry->d_flags;
  155. spin_lock(&dentry->d_lock);
  156. dentry->d_flags &= ~mask;
  157. dentry->d_flags |= flags & mask;
  158. spin_unlock(&dentry->d_lock);
  159. }
  160. bool ovl_dentry_weird(struct dentry *dentry)
  161. {
  162. if (!d_can_lookup(dentry) && !d_is_file(dentry) && !d_is_symlink(dentry))
  163. return true;
  164. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  165. DCACHE_MANAGE_TRANSIT |
  166. DCACHE_OP_HASH |
  167. DCACHE_OP_COMPARE);
  168. }
  169. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  170. {
  171. struct ovl_entry *oe = OVL_E(dentry);
  172. enum ovl_path_type type = 0;
  173. if (ovl_dentry_upper(dentry)) {
  174. type = __OVL_PATH_UPPER;
  175. /*
  176. * Non-dir dentry can hold lower dentry of its copy up origin.
  177. */
  178. if (ovl_numlower(oe)) {
  179. if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
  180. type |= __OVL_PATH_ORIGIN;
  181. if (d_is_dir(dentry) ||
  182. !ovl_has_upperdata(d_inode(dentry)))
  183. type |= __OVL_PATH_MERGE;
  184. }
  185. } else {
  186. if (ovl_numlower(oe) > 1)
  187. type |= __OVL_PATH_MERGE;
  188. }
  189. return type;
  190. }
  191. void ovl_path_upper(struct dentry *dentry, struct path *path)
  192. {
  193. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  194. path->mnt = ovl_upper_mnt(ofs);
  195. path->dentry = ovl_dentry_upper(dentry);
  196. }
  197. void ovl_path_lower(struct dentry *dentry, struct path *path)
  198. {
  199. struct ovl_entry *oe = OVL_E(dentry);
  200. struct ovl_path *lowerpath = ovl_lowerstack(oe);
  201. if (ovl_numlower(oe)) {
  202. path->mnt = lowerpath->layer->mnt;
  203. path->dentry = lowerpath->dentry;
  204. } else {
  205. *path = (struct path) { };
  206. }
  207. }
  208. void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
  209. {
  210. struct ovl_entry *oe = OVL_E(dentry);
  211. struct ovl_path *lowerdata = ovl_lowerdata(oe);
  212. struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
  213. if (lowerdata_dentry) {
  214. path->dentry = lowerdata_dentry;
  215. /*
  216. * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
  217. * Make sure that if lowerdata->dentry is visible, then
  218. * datapath->layer is visible as well.
  219. */
  220. smp_rmb();
  221. path->mnt = READ_ONCE(lowerdata->layer)->mnt;
  222. } else {
  223. *path = (struct path) { };
  224. }
  225. }
  226. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  227. {
  228. enum ovl_path_type type = ovl_path_type(dentry);
  229. if (!OVL_TYPE_UPPER(type))
  230. ovl_path_lower(dentry, path);
  231. else
  232. ovl_path_upper(dentry, path);
  233. return type;
  234. }
  235. enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
  236. {
  237. enum ovl_path_type type = ovl_path_type(dentry);
  238. WARN_ON_ONCE(d_is_dir(dentry));
  239. if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
  240. ovl_path_lowerdata(dentry, path);
  241. else
  242. ovl_path_upper(dentry, path);
  243. return type;
  244. }
  245. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  246. {
  247. struct inode *inode = d_inode(dentry);
  248. return inode ? ovl_upperdentry_dereference(OVL_I(inode)) : NULL;
  249. }
  250. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  251. {
  252. struct ovl_entry *oe = OVL_E(dentry);
  253. return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
  254. }
  255. const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
  256. {
  257. struct ovl_entry *oe = OVL_E(dentry);
  258. return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
  259. }
  260. /*
  261. * ovl_dentry_lower() could return either a data dentry or metacopy dentry
  262. * depending on what is stored in lowerstack[0]. At times we need to find
  263. * lower dentry which has data (and not metacopy dentry). This helper
  264. * returns the lower data dentry.
  265. */
  266. struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
  267. {
  268. return ovl_lowerdata_dentry(OVL_E(dentry));
  269. }
  270. int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
  271. {
  272. struct ovl_entry *oe = OVL_E(dentry);
  273. struct ovl_path *lowerdata = ovl_lowerdata(oe);
  274. struct dentry *datadentry = datapath->dentry;
  275. if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
  276. return -EIO;
  277. WRITE_ONCE(lowerdata->layer, datapath->layer);
  278. /*
  279. * Pairs with smp_rmb() in ovl_path_lowerdata().
  280. * Make sure that if lowerdata->dentry is visible, then
  281. * lowerdata->layer is visible as well.
  282. */
  283. smp_wmb();
  284. WRITE_ONCE(lowerdata->dentry, dget(datadentry));
  285. ovl_dentry_update_reval(dentry, datadentry);
  286. return 0;
  287. }
  288. struct dentry *ovl_dentry_real(struct dentry *dentry)
  289. {
  290. return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
  291. }
  292. struct dentry *ovl_i_dentry_upper(struct inode *inode)
  293. {
  294. return ovl_upperdentry_dereference(OVL_I(inode));
  295. }
  296. struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
  297. {
  298. struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
  299. path->dentry = ovl_i_dentry_upper(inode);
  300. if (!path->dentry) {
  301. path->dentry = lowerpath->dentry;
  302. path->mnt = lowerpath->layer->mnt;
  303. } else {
  304. path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
  305. }
  306. return path->dentry ? d_inode_rcu(path->dentry) : NULL;
  307. }
  308. struct inode *ovl_inode_upper(struct inode *inode)
  309. {
  310. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  311. return upperdentry ? d_inode(upperdentry) : NULL;
  312. }
  313. struct inode *ovl_inode_lower(struct inode *inode)
  314. {
  315. struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
  316. return lowerpath ? d_inode(lowerpath->dentry) : NULL;
  317. }
  318. struct inode *ovl_inode_real(struct inode *inode)
  319. {
  320. return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
  321. }
  322. /* Return inode which contains lower data. Do not return metacopy */
  323. struct inode *ovl_inode_lowerdata(struct inode *inode)
  324. {
  325. struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
  326. if (WARN_ON(!S_ISREG(inode->i_mode)))
  327. return NULL;
  328. return lowerdata ? d_inode(lowerdata) : NULL;
  329. }
  330. /* Return real inode which contains data. Does not return metacopy inode */
  331. struct inode *ovl_inode_realdata(struct inode *inode)
  332. {
  333. struct inode *upperinode;
  334. upperinode = ovl_inode_upper(inode);
  335. if (upperinode && ovl_has_upperdata(inode))
  336. return upperinode;
  337. return ovl_inode_lowerdata(inode);
  338. }
  339. const char *ovl_lowerdata_redirect(struct inode *inode)
  340. {
  341. return inode && S_ISREG(inode->i_mode) ?
  342. OVL_I(inode)->lowerdata_redirect : NULL;
  343. }
  344. struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
  345. {
  346. return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
  347. }
  348. void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
  349. {
  350. OVL_I(inode)->cache = cache;
  351. }
  352. void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
  353. {
  354. set_bit(flag, OVL_E_FLAGS(dentry));
  355. }
  356. void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
  357. {
  358. clear_bit(flag, OVL_E_FLAGS(dentry));
  359. }
  360. bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
  361. {
  362. return test_bit(flag, OVL_E_FLAGS(dentry));
  363. }
  364. bool ovl_dentry_is_opaque(struct dentry *dentry)
  365. {
  366. return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
  367. }
  368. bool ovl_dentry_is_whiteout(struct dentry *dentry)
  369. {
  370. return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
  371. }
  372. void ovl_dentry_set_opaque(struct dentry *dentry)
  373. {
  374. ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
  375. }
  376. bool ovl_dentry_has_xwhiteouts(struct dentry *dentry)
  377. {
  378. return ovl_dentry_test_flag(OVL_E_XWHITEOUTS, dentry);
  379. }
  380. void ovl_dentry_set_xwhiteouts(struct dentry *dentry)
  381. {
  382. ovl_dentry_set_flag(OVL_E_XWHITEOUTS, dentry);
  383. }
  384. /*
  385. * ovl_layer_set_xwhiteouts() is called before adding the overlay dir
  386. * dentry to dcache, while readdir of that same directory happens after
  387. * the overlay dir dentry is in dcache, so if some cpu observes that
  388. * ovl_dentry_is_xwhiteouts(), it will also observe layer->has_xwhiteouts
  389. * for the layers where xwhiteouts marker was found in that merge dir.
  390. */
  391. void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs,
  392. const struct ovl_layer *layer)
  393. {
  394. if (layer->has_xwhiteouts)
  395. return;
  396. /* Write once to read-mostly layer properties */
  397. ofs->layers[layer->idx].has_xwhiteouts = true;
  398. }
  399. /*
  400. * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
  401. * to return positive, while there's no actual upper alias for the inode.
  402. * Copy up code needs to know about the existence of the upper alias, so it
  403. * can't use ovl_dentry_upper().
  404. */
  405. bool ovl_dentry_has_upper_alias(struct dentry *dentry)
  406. {
  407. return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
  408. }
  409. void ovl_dentry_set_upper_alias(struct dentry *dentry)
  410. {
  411. ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
  412. }
  413. static bool ovl_should_check_upperdata(struct inode *inode)
  414. {
  415. if (!S_ISREG(inode->i_mode))
  416. return false;
  417. if (!ovl_inode_lower(inode))
  418. return false;
  419. return true;
  420. }
  421. bool ovl_has_upperdata(struct inode *inode)
  422. {
  423. if (!ovl_should_check_upperdata(inode))
  424. return true;
  425. if (!ovl_test_flag(OVL_UPPERDATA, inode))
  426. return false;
  427. /*
  428. * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
  429. * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
  430. * if setting of OVL_UPPERDATA is visible, then effects of writes
  431. * before that are visible too.
  432. */
  433. smp_rmb();
  434. return true;
  435. }
  436. void ovl_set_upperdata(struct inode *inode)
  437. {
  438. /*
  439. * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
  440. * if OVL_UPPERDATA flag is visible, then effects of write operations
  441. * before it are visible as well.
  442. */
  443. smp_wmb();
  444. ovl_set_flag(OVL_UPPERDATA, inode);
  445. }
  446. /* Caller should hold ovl_inode->lock */
  447. bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
  448. {
  449. if (!ovl_open_flags_need_copy_up(flags))
  450. return false;
  451. return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
  452. }
  453. bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
  454. {
  455. if (!ovl_open_flags_need_copy_up(flags))
  456. return false;
  457. return !ovl_has_upperdata(d_inode(dentry));
  458. }
  459. const char *ovl_dentry_get_redirect(struct dentry *dentry)
  460. {
  461. return OVL_I(d_inode(dentry))->redirect;
  462. }
  463. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
  464. {
  465. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  466. kfree(oi->redirect);
  467. oi->redirect = redirect;
  468. }
  469. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
  470. {
  471. struct inode *upperinode = d_inode(upperdentry);
  472. WARN_ON(OVL_I(inode)->__upperdentry);
  473. /*
  474. * Make sure upperdentry is consistent before making it visible
  475. */
  476. smp_wmb();
  477. OVL_I(inode)->__upperdentry = upperdentry;
  478. if (inode_unhashed(inode)) {
  479. inode->i_private = upperinode;
  480. __insert_inode_hash(inode, (unsigned long) upperinode);
  481. }
  482. }
  483. static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
  484. {
  485. struct inode *inode = d_inode(dentry);
  486. WARN_ON(!inode_is_locked(inode));
  487. WARN_ON(!d_is_dir(dentry));
  488. /*
  489. * Version is used by readdir code to keep cache consistent.
  490. * For merge dirs (or dirs with origin) all changes need to be noted.
  491. * For non-merge dirs, cache contains only impure entries (i.e. ones
  492. * which have been copied up and have origins), so only need to note
  493. * changes to impure entries.
  494. */
  495. if (!ovl_dir_is_real(inode) || impurity)
  496. OVL_I(inode)->version++;
  497. }
  498. void ovl_dir_modified(struct dentry *dentry, bool impurity)
  499. {
  500. /* Copy mtime/ctime */
  501. ovl_copyattr(d_inode(dentry));
  502. ovl_dir_version_inc(dentry, impurity);
  503. }
  504. u64 ovl_inode_version_get(struct inode *inode)
  505. {
  506. WARN_ON(!inode_is_locked(inode));
  507. return OVL_I(inode)->version;
  508. }
  509. bool ovl_is_whiteout(struct dentry *dentry)
  510. {
  511. struct inode *inode = dentry->d_inode;
  512. return inode && IS_WHITEOUT(inode);
  513. }
  514. /*
  515. * Use this over ovl_is_whiteout for upper and lower files, as it also
  516. * handles overlay.whiteout xattr whiteout files.
  517. */
  518. bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path)
  519. {
  520. return ovl_is_whiteout(path->dentry) ||
  521. ovl_path_check_xwhiteout_xattr(ofs, path);
  522. }
  523. struct file *ovl_path_open(const struct path *path, int flags)
  524. {
  525. struct inode *inode = d_inode(path->dentry);
  526. struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
  527. int err, acc_mode;
  528. if (flags & ~(O_ACCMODE | O_LARGEFILE))
  529. BUG();
  530. switch (flags & O_ACCMODE) {
  531. case O_RDONLY:
  532. acc_mode = MAY_READ;
  533. break;
  534. case O_WRONLY:
  535. acc_mode = MAY_WRITE;
  536. break;
  537. default:
  538. BUG();
  539. }
  540. err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
  541. if (err)
  542. return ERR_PTR(err);
  543. /* O_NOATIME is an optimization, don't fail if not permitted */
  544. if (inode_owner_or_capable(real_idmap, inode))
  545. flags |= O_NOATIME;
  546. return dentry_open(path, flags, current_cred());
  547. }
  548. /* Caller should hold ovl_inode->lock */
  549. static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
  550. {
  551. bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
  552. if (ovl_dentry_upper(dentry) &&
  553. (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
  554. !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
  555. return true;
  556. return false;
  557. }
  558. bool ovl_already_copied_up(struct dentry *dentry, int flags)
  559. {
  560. bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
  561. /*
  562. * Check if copy-up has happened as well as for upper alias (in
  563. * case of hard links) is there.
  564. *
  565. * Both checks are lockless:
  566. * - false negatives: will recheck under oi->lock
  567. * - false positives:
  568. * + ovl_dentry_upper() uses memory barriers to ensure the
  569. * upper dentry is up-to-date
  570. * + ovl_dentry_has_upper_alias() relies on locking of
  571. * upper parent i_rwsem to prevent reordering copy-up
  572. * with rename.
  573. */
  574. if (ovl_dentry_upper(dentry) &&
  575. (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
  576. !ovl_dentry_needs_data_copy_up(dentry, flags))
  577. return true;
  578. return false;
  579. }
  580. /*
  581. * The copy up "transaction" keeps an elevated mnt write count on upper mnt,
  582. * but leaves taking freeze protection on upper sb to lower level helpers.
  583. */
  584. int ovl_copy_up_start(struct dentry *dentry, int flags)
  585. {
  586. struct inode *inode = d_inode(dentry);
  587. int err;
  588. err = ovl_inode_lock_interruptible(inode);
  589. if (err)
  590. return err;
  591. if (ovl_already_copied_up_locked(dentry, flags))
  592. err = 1; /* Already copied up */
  593. else
  594. err = ovl_get_write_access(dentry);
  595. if (err)
  596. goto out_unlock;
  597. return 0;
  598. out_unlock:
  599. ovl_inode_unlock(inode);
  600. return err;
  601. }
  602. void ovl_copy_up_end(struct dentry *dentry)
  603. {
  604. ovl_put_write_access(dentry);
  605. ovl_inode_unlock(d_inode(dentry));
  606. }
  607. bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
  608. {
  609. int res;
  610. res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
  611. /* Zero size value means "copied up but origin unknown" */
  612. if (res >= 0)
  613. return true;
  614. return false;
  615. }
  616. bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path)
  617. {
  618. struct dentry *dentry = path->dentry;
  619. int res;
  620. /* xattr.whiteout must be a zero size regular file */
  621. if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0)
  622. return false;
  623. res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0);
  624. return res >= 0;
  625. }
  626. /*
  627. * Load persistent uuid from xattr into s_uuid if found, or store a new
  628. * random generated value in s_uuid and in xattr.
  629. */
  630. bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
  631. const struct path *upperpath)
  632. {
  633. bool set = false;
  634. uuid_t uuid;
  635. int res;
  636. /* Try to load existing persistent uuid */
  637. res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, uuid.b,
  638. UUID_SIZE);
  639. if (res == UUID_SIZE)
  640. goto set_uuid;
  641. if (res != -ENODATA)
  642. goto fail;
  643. /*
  644. * With uuid=auto, if uuid xattr is found, it will be used.
  645. * If uuid xattrs is not found, generate a persistent uuid only on mount
  646. * of new overlays where upper root dir is not yet marked as impure.
  647. * An upper dir is marked as impure on copy up or lookup of its subdirs.
  648. */
  649. if (ofs->config.uuid == OVL_UUID_AUTO) {
  650. res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
  651. 0);
  652. if (res > 0) {
  653. /* Any mount of old overlay - downgrade to uuid=null */
  654. ofs->config.uuid = OVL_UUID_NULL;
  655. return true;
  656. } else if (res == -ENODATA) {
  657. /* First mount of new overlay - upgrade to uuid=on */
  658. ofs->config.uuid = OVL_UUID_ON;
  659. } else if (res < 0) {
  660. goto fail;
  661. }
  662. }
  663. /* Generate overlay instance uuid */
  664. uuid_gen(&uuid);
  665. /* Try to store persistent uuid */
  666. set = true;
  667. res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, uuid.b,
  668. UUID_SIZE);
  669. if (res)
  670. goto fail;
  671. set_uuid:
  672. super_set_uuid(sb, uuid.b, sizeof(uuid));
  673. return true;
  674. fail:
  675. ofs->config.uuid = OVL_UUID_NULL;
  676. pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
  677. set ? "set" : "get", upperpath->dentry, res);
  678. return false;
  679. }
  680. char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path,
  681. enum ovl_xattr ox)
  682. {
  683. int res;
  684. char val;
  685. if (!d_is_dir(path->dentry))
  686. return 0;
  687. res = ovl_path_getxattr(ofs, path, ox, &val, 1);
  688. return res == 1 ? val : 0;
  689. }
  690. #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
  691. #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
  692. #define OVL_XATTR_ORIGIN_POSTFIX "origin"
  693. #define OVL_XATTR_IMPURE_POSTFIX "impure"
  694. #define OVL_XATTR_NLINK_POSTFIX "nlink"
  695. #define OVL_XATTR_UPPER_POSTFIX "upper"
  696. #define OVL_XATTR_UUID_POSTFIX "uuid"
  697. #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
  698. #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
  699. #define OVL_XATTR_XWHITEOUT_POSTFIX "whiteout"
  700. #define OVL_XATTR_TAB_ENTRY(x) \
  701. [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
  702. [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
  703. const char *const ovl_xattr_table[][2] = {
  704. OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
  705. OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
  706. OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
  707. OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
  708. OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
  709. OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
  710. OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
  711. OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
  712. OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
  713. OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT),
  714. };
  715. int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
  716. enum ovl_xattr ox, const void *value, size_t size,
  717. int xerr)
  718. {
  719. int err;
  720. if (ofs->noxattr)
  721. return xerr;
  722. err = ovl_setxattr(ofs, upperdentry, ox, value, size);
  723. if (err == -EOPNOTSUPP) {
  724. pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
  725. ofs->noxattr = true;
  726. return xerr;
  727. }
  728. return err;
  729. }
  730. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
  731. {
  732. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  733. int err;
  734. if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
  735. return 0;
  736. /*
  737. * Do not fail when upper doesn't support xattrs.
  738. * Upper inodes won't have origin nor redirect xattr anyway.
  739. */
  740. err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
  741. if (!err)
  742. ovl_set_flag(OVL_IMPURE, d_inode(dentry));
  743. return err;
  744. }
  745. #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
  746. void ovl_check_protattr(struct inode *inode, struct dentry *upper)
  747. {
  748. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  749. u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
  750. char buf[OVL_PROTATTR_MAX+1];
  751. int res, n;
  752. res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
  753. OVL_PROTATTR_MAX);
  754. if (res < 0)
  755. return;
  756. /*
  757. * Initialize inode flags from overlay.protattr xattr and upper inode
  758. * flags. If upper inode has those fileattr flags set (i.e. from old
  759. * kernel), we do not clear them on ovl_get_inode(), but we will clear
  760. * them on next fileattr_set().
  761. */
  762. for (n = 0; n < res; n++) {
  763. if (buf[n] == 'a')
  764. iflags |= S_APPEND;
  765. else if (buf[n] == 'i')
  766. iflags |= S_IMMUTABLE;
  767. else
  768. break;
  769. }
  770. if (!res || n < res) {
  771. pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
  772. upper, res);
  773. } else {
  774. inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
  775. }
  776. }
  777. int ovl_set_protattr(struct inode *inode, struct dentry *upper,
  778. struct fileattr *fa)
  779. {
  780. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  781. char buf[OVL_PROTATTR_MAX];
  782. int len = 0, err = 0;
  783. u32 iflags = 0;
  784. BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
  785. if (fa->flags & FS_APPEND_FL) {
  786. buf[len++] = 'a';
  787. iflags |= S_APPEND;
  788. }
  789. if (fa->flags & FS_IMMUTABLE_FL) {
  790. buf[len++] = 'i';
  791. iflags |= S_IMMUTABLE;
  792. }
  793. /*
  794. * Do not allow to set protection flags when upper doesn't support
  795. * xattrs, because we do not set those fileattr flags on upper inode.
  796. * Remove xattr if it exist and all protection flags are cleared.
  797. */
  798. if (len) {
  799. err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
  800. buf, len, -EPERM);
  801. } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
  802. err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
  803. if (err == -EOPNOTSUPP || err == -ENODATA)
  804. err = 0;
  805. }
  806. if (err)
  807. return err;
  808. inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
  809. /* Mask out the fileattr flags that should not be set in upper inode */
  810. fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
  811. fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
  812. return 0;
  813. }
  814. /*
  815. * Caller must hold a reference to inode to prevent it from being freed while
  816. * it is marked inuse.
  817. */
  818. bool ovl_inuse_trylock(struct dentry *dentry)
  819. {
  820. struct inode *inode = d_inode(dentry);
  821. bool locked = false;
  822. spin_lock(&inode->i_lock);
  823. if (!(inode->i_state & I_OVL_INUSE)) {
  824. inode->i_state |= I_OVL_INUSE;
  825. locked = true;
  826. }
  827. spin_unlock(&inode->i_lock);
  828. return locked;
  829. }
  830. void ovl_inuse_unlock(struct dentry *dentry)
  831. {
  832. if (dentry) {
  833. struct inode *inode = d_inode(dentry);
  834. spin_lock(&inode->i_lock);
  835. WARN_ON(!(inode->i_state & I_OVL_INUSE));
  836. inode->i_state &= ~I_OVL_INUSE;
  837. spin_unlock(&inode->i_lock);
  838. }
  839. }
  840. bool ovl_is_inuse(struct dentry *dentry)
  841. {
  842. struct inode *inode = d_inode(dentry);
  843. bool inuse;
  844. spin_lock(&inode->i_lock);
  845. inuse = (inode->i_state & I_OVL_INUSE);
  846. spin_unlock(&inode->i_lock);
  847. return inuse;
  848. }
  849. /*
  850. * Does this overlay dentry need to be indexed on copy up?
  851. */
  852. bool ovl_need_index(struct dentry *dentry)
  853. {
  854. struct dentry *lower = ovl_dentry_lower(dentry);
  855. if (!lower || !ovl_indexdir(dentry->d_sb))
  856. return false;
  857. /* Index all files for NFS export and consistency verification */
  858. if (ovl_index_all(dentry->d_sb))
  859. return true;
  860. /* Index only lower hardlinks on copy up */
  861. if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  862. return true;
  863. return false;
  864. }
  865. /* Caller must hold OVL_I(inode)->lock */
  866. static void ovl_cleanup_index(struct dentry *dentry)
  867. {
  868. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  869. struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
  870. struct inode *dir = indexdir->d_inode;
  871. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  872. struct dentry *upperdentry = ovl_dentry_upper(dentry);
  873. struct dentry *index = NULL;
  874. struct inode *inode;
  875. struct qstr name = { };
  876. bool got_write = false;
  877. int err;
  878. err = ovl_get_index_name(ofs, lowerdentry, &name);
  879. if (err)
  880. goto fail;
  881. err = ovl_want_write(dentry);
  882. if (err)
  883. goto fail;
  884. got_write = true;
  885. inode = d_inode(upperdentry);
  886. if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
  887. pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
  888. upperdentry, inode->i_ino, inode->i_nlink);
  889. /*
  890. * We either have a bug with persistent union nlink or a lower
  891. * hardlink was added while overlay is mounted. Adding a lower
  892. * hardlink and then unlinking all overlay hardlinks would drop
  893. * overlay nlink to zero before all upper inodes are unlinked.
  894. * As a safety measure, when that situation is detected, set
  895. * the overlay nlink to the index inode nlink minus one for the
  896. * index entry itself.
  897. */
  898. set_nlink(d_inode(dentry), inode->i_nlink - 1);
  899. ovl_set_nlink_upper(dentry);
  900. goto out;
  901. }
  902. inode_lock_nested(dir, I_MUTEX_PARENT);
  903. index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
  904. err = PTR_ERR(index);
  905. if (IS_ERR(index)) {
  906. index = NULL;
  907. } else if (ovl_index_all(dentry->d_sb)) {
  908. /* Whiteout orphan index to block future open by handle */
  909. err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
  910. dir, index);
  911. } else {
  912. /* Cleanup orphan index entries */
  913. err = ovl_cleanup(ofs, dir, index);
  914. }
  915. inode_unlock(dir);
  916. if (err)
  917. goto fail;
  918. out:
  919. if (got_write)
  920. ovl_drop_write(dentry);
  921. kfree(name.name);
  922. dput(index);
  923. return;
  924. fail:
  925. pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
  926. goto out;
  927. }
  928. /*
  929. * Operations that change overlay inode and upper inode nlink need to be
  930. * synchronized with copy up for persistent nlink accounting.
  931. */
  932. int ovl_nlink_start(struct dentry *dentry)
  933. {
  934. struct inode *inode = d_inode(dentry);
  935. const struct cred *old_cred;
  936. int err;
  937. if (WARN_ON(!inode))
  938. return -ENOENT;
  939. /*
  940. * With inodes index is enabled, we store the union overlay nlink
  941. * in an xattr on the index inode. When whiting out an indexed lower,
  942. * we need to decrement the overlay persistent nlink, but before the
  943. * first copy up, we have no upper index inode to store the xattr.
  944. *
  945. * As a workaround, before whiteout/rename over an indexed lower,
  946. * copy up to create the upper index. Creating the upper index will
  947. * initialize the overlay nlink, so it could be dropped if unlink
  948. * or rename succeeds.
  949. *
  950. * TODO: implement metadata only index copy up when called with
  951. * ovl_copy_up_flags(dentry, O_PATH).
  952. */
  953. if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
  954. err = ovl_copy_up(dentry);
  955. if (err)
  956. return err;
  957. }
  958. err = ovl_inode_lock_interruptible(inode);
  959. if (err)
  960. return err;
  961. err = ovl_want_write(dentry);
  962. if (err)
  963. goto out_unlock;
  964. if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
  965. return 0;
  966. old_cred = ovl_override_creds(dentry->d_sb);
  967. /*
  968. * The overlay inode nlink should be incremented/decremented IFF the
  969. * upper operation succeeds, along with nlink change of upper inode.
  970. * Therefore, before link/unlink/rename, we store the union nlink
  971. * value relative to the upper inode nlink in an upper inode xattr.
  972. */
  973. err = ovl_set_nlink_upper(dentry);
  974. revert_creds(old_cred);
  975. if (err)
  976. goto out_drop_write;
  977. return 0;
  978. out_drop_write:
  979. ovl_drop_write(dentry);
  980. out_unlock:
  981. ovl_inode_unlock(inode);
  982. return err;
  983. }
  984. void ovl_nlink_end(struct dentry *dentry)
  985. {
  986. struct inode *inode = d_inode(dentry);
  987. ovl_drop_write(dentry);
  988. if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
  989. const struct cred *old_cred;
  990. old_cred = ovl_override_creds(dentry->d_sb);
  991. ovl_cleanup_index(dentry);
  992. revert_creds(old_cred);
  993. }
  994. ovl_inode_unlock(inode);
  995. }
  996. int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
  997. {
  998. struct dentry *trap;
  999. /* Workdir should not be the same as upperdir */
  1000. if (workdir == upperdir)
  1001. goto err;
  1002. /* Workdir should not be subdir of upperdir and vice versa */
  1003. trap = lock_rename(workdir, upperdir);
  1004. if (IS_ERR(trap))
  1005. goto err;
  1006. if (trap)
  1007. goto err_unlock;
  1008. return 0;
  1009. err_unlock:
  1010. unlock_rename(workdir, upperdir);
  1011. err:
  1012. pr_err("failed to lock workdir+upperdir\n");
  1013. return -EIO;
  1014. }
  1015. /*
  1016. * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
  1017. * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
  1018. */
  1019. int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
  1020. struct ovl_metacopy *data)
  1021. {
  1022. int res;
  1023. /* Only regular files can have metacopy xattr */
  1024. if (!S_ISREG(d_inode(path->dentry)->i_mode))
  1025. return 0;
  1026. res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
  1027. data, data ? OVL_METACOPY_MAX_SIZE : 0);
  1028. if (res < 0) {
  1029. if (res == -ENODATA || res == -EOPNOTSUPP)
  1030. return 0;
  1031. /*
  1032. * getxattr on user.* may fail with EACCES in case there's no
  1033. * read permission on the inode. Not much we can do, other than
  1034. * tell the caller that this is not a metacopy inode.
  1035. */
  1036. if (ofs->config.userxattr && res == -EACCES)
  1037. return 0;
  1038. goto out;
  1039. }
  1040. if (res == 0) {
  1041. /* Emulate empty data for zero size metacopy xattr */
  1042. res = OVL_METACOPY_MIN_SIZE;
  1043. if (data) {
  1044. memset(data, 0, res);
  1045. data->len = res;
  1046. }
  1047. } else if (res < OVL_METACOPY_MIN_SIZE) {
  1048. pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
  1049. path->dentry);
  1050. return -EIO;
  1051. } else if (data) {
  1052. if (data->version != 0) {
  1053. pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
  1054. path->dentry);
  1055. return -EIO;
  1056. }
  1057. if (res != data->len) {
  1058. pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
  1059. path->dentry);
  1060. return -EIO;
  1061. }
  1062. }
  1063. return res;
  1064. out:
  1065. pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
  1066. return res;
  1067. }
  1068. int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
  1069. {
  1070. size_t len = metacopy->len;
  1071. /* If no flags or digest fall back to empty metacopy file */
  1072. if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
  1073. len = 0;
  1074. return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
  1075. metacopy, len, -EOPNOTSUPP);
  1076. }
  1077. bool ovl_is_metacopy_dentry(struct dentry *dentry)
  1078. {
  1079. struct ovl_entry *oe = OVL_E(dentry);
  1080. if (!d_is_reg(dentry))
  1081. return false;
  1082. if (ovl_dentry_upper(dentry)) {
  1083. if (!ovl_has_upperdata(d_inode(dentry)))
  1084. return true;
  1085. return false;
  1086. }
  1087. return (ovl_numlower(oe) > 1);
  1088. }
  1089. char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
  1090. {
  1091. int res;
  1092. char *s, *next, *buf = NULL;
  1093. res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
  1094. if (res == -ENODATA || res == -EOPNOTSUPP)
  1095. return NULL;
  1096. if (res < 0)
  1097. goto fail;
  1098. if (res == 0)
  1099. goto invalid;
  1100. buf = kzalloc(res + padding + 1, GFP_KERNEL);
  1101. if (!buf)
  1102. return ERR_PTR(-ENOMEM);
  1103. res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
  1104. if (res < 0)
  1105. goto fail;
  1106. if (res == 0)
  1107. goto invalid;
  1108. if (buf[0] == '/') {
  1109. for (s = buf; *s++ == '/'; s = next) {
  1110. next = strchrnul(s, '/');
  1111. if (s == next)
  1112. goto invalid;
  1113. }
  1114. } else {
  1115. if (strchr(buf, '/') != NULL)
  1116. goto invalid;
  1117. }
  1118. return buf;
  1119. invalid:
  1120. pr_warn_ratelimited("invalid redirect (%s)\n", buf);
  1121. res = -EINVAL;
  1122. goto err_free;
  1123. fail:
  1124. pr_warn_ratelimited("failed to get redirect (%i)\n", res);
  1125. err_free:
  1126. kfree(buf);
  1127. return ERR_PTR(res);
  1128. }
  1129. /* Call with mounter creds as it may open the file */
  1130. int ovl_ensure_verity_loaded(struct path *datapath)
  1131. {
  1132. struct inode *inode = d_inode(datapath->dentry);
  1133. struct file *filp;
  1134. if (!fsverity_active(inode) && IS_VERITY(inode)) {
  1135. /*
  1136. * If this inode was not yet opened, the verity info hasn't been
  1137. * loaded yet, so we need to do that here to force it into memory.
  1138. */
  1139. filp = kernel_file_open(datapath, O_RDONLY, current_cred());
  1140. if (IS_ERR(filp))
  1141. return PTR_ERR(filp);
  1142. fput(filp);
  1143. }
  1144. return 0;
  1145. }
  1146. int ovl_validate_verity(struct ovl_fs *ofs,
  1147. struct path *metapath,
  1148. struct path *datapath)
  1149. {
  1150. struct ovl_metacopy metacopy_data;
  1151. u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
  1152. int xattr_digest_size, digest_size;
  1153. int xattr_size, err;
  1154. u8 verity_algo;
  1155. if (!ofs->config.verity_mode ||
  1156. /* Verity only works on regular files */
  1157. !S_ISREG(d_inode(metapath->dentry)->i_mode))
  1158. return 0;
  1159. xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
  1160. if (xattr_size < 0)
  1161. return xattr_size;
  1162. if (!xattr_size || !metacopy_data.digest_algo) {
  1163. if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
  1164. pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
  1165. metapath->dentry);
  1166. return -EIO;
  1167. }
  1168. return 0;
  1169. }
  1170. xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
  1171. err = ovl_ensure_verity_loaded(datapath);
  1172. if (err < 0) {
  1173. pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
  1174. datapath->dentry);
  1175. return -EIO;
  1176. }
  1177. digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
  1178. &verity_algo, NULL);
  1179. if (digest_size == 0) {
  1180. pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
  1181. return -EIO;
  1182. }
  1183. if (xattr_digest_size != digest_size ||
  1184. metacopy_data.digest_algo != verity_algo ||
  1185. memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
  1186. pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
  1187. datapath->dentry);
  1188. return -EIO;
  1189. }
  1190. return 0;
  1191. }
  1192. int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
  1193. struct ovl_metacopy *metacopy)
  1194. {
  1195. int err, digest_size;
  1196. if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
  1197. return 0;
  1198. err = ovl_ensure_verity_loaded(src);
  1199. if (err < 0) {
  1200. pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
  1201. src->dentry);
  1202. return -EIO;
  1203. }
  1204. digest_size = fsverity_get_digest(d_inode(src->dentry),
  1205. metacopy->digest, &metacopy->digest_algo, NULL);
  1206. if (digest_size == 0 ||
  1207. WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
  1208. if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
  1209. pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
  1210. src->dentry);
  1211. return -EIO;
  1212. }
  1213. return 0;
  1214. }
  1215. metacopy->len += digest_size;
  1216. return 0;
  1217. }
  1218. /*
  1219. * ovl_sync_status() - Check fs sync status for volatile mounts
  1220. *
  1221. * Returns 1 if this is not a volatile mount and a real sync is required.
  1222. *
  1223. * Returns 0 if syncing can be skipped because mount is volatile, and no errors
  1224. * have occurred on the upperdir since the mount.
  1225. *
  1226. * Returns -errno if it is a volatile mount, and the error that occurred since
  1227. * the last mount. If the error code changes, it'll return the latest error
  1228. * code.
  1229. */
  1230. int ovl_sync_status(struct ovl_fs *ofs)
  1231. {
  1232. struct vfsmount *mnt;
  1233. if (ovl_should_sync(ofs))
  1234. return 1;
  1235. mnt = ovl_upper_mnt(ofs);
  1236. if (!mnt)
  1237. return 0;
  1238. return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
  1239. }
  1240. /*
  1241. * ovl_copyattr() - copy inode attributes from layer to ovl inode
  1242. *
  1243. * When overlay copies inode information from an upper or lower layer to the
  1244. * relevant overlay inode it will apply the idmapping of the upper or lower
  1245. * layer when doing so ensuring that the ovl inode ownership will correctly
  1246. * reflect the ownership of the idmapped upper or lower layer. For example, an
  1247. * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
  1248. * map any lower or upper inode owned by id 1001 to id 1000. These mapping
  1249. * helpers are nops when the relevant layer isn't idmapped.
  1250. */
  1251. void ovl_copyattr(struct inode *inode)
  1252. {
  1253. struct path realpath;
  1254. struct inode *realinode;
  1255. struct mnt_idmap *real_idmap;
  1256. vfsuid_t vfsuid;
  1257. vfsgid_t vfsgid;
  1258. realinode = ovl_i_path_real(inode, &realpath);
  1259. real_idmap = mnt_idmap(realpath.mnt);
  1260. spin_lock(&inode->i_lock);
  1261. vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
  1262. vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
  1263. inode->i_uid = vfsuid_into_kuid(vfsuid);
  1264. inode->i_gid = vfsgid_into_kgid(vfsgid);
  1265. inode->i_mode = realinode->i_mode;
  1266. inode_set_atime_to_ts(inode, inode_get_atime(realinode));
  1267. inode_set_mtime_to_ts(inode, inode_get_mtime(realinode));
  1268. inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
  1269. i_size_write(inode, i_size_read(realinode));
  1270. spin_unlock(&inode->i_lock);
  1271. }