util.c 38 KB

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