util.c 38 KB

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