util.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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/fileattr.h>
  13. #include <linux/uuid.h>
  14. #include <linux/namei.h>
  15. #include <linux/ratelimit.h>
  16. #include "overlayfs.h"
  17. int ovl_want_write(struct dentry *dentry)
  18. {
  19. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  20. return mnt_want_write(ovl_upper_mnt(ofs));
  21. }
  22. void ovl_drop_write(struct dentry *dentry)
  23. {
  24. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  25. mnt_drop_write(ovl_upper_mnt(ofs));
  26. }
  27. struct dentry *ovl_workdir(struct dentry *dentry)
  28. {
  29. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  30. return ofs->workdir;
  31. }
  32. const struct cred *ovl_override_creds(struct super_block *sb)
  33. {
  34. struct ovl_fs *ofs = sb->s_fs_info;
  35. if (!ofs->config.override_creds)
  36. return NULL;
  37. return override_creds(ofs->creator_cred);
  38. }
  39. void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
  40. {
  41. if (old_cred)
  42. revert_creds(old_cred);
  43. }
  44. /*
  45. * Check if underlying fs supports file handles and try to determine encoding
  46. * type, in order to deduce maximum inode number used by fs.
  47. *
  48. * Return 0 if file handles are not supported.
  49. * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
  50. * Return -1 if fs uses a non default encoding with unknown inode size.
  51. */
  52. int ovl_can_decode_fh(struct super_block *sb)
  53. {
  54. if (!capable(CAP_DAC_READ_SEARCH))
  55. return 0;
  56. if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
  57. return 0;
  58. return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
  59. }
  60. struct dentry *ovl_indexdir(struct super_block *sb)
  61. {
  62. struct ovl_fs *ofs = sb->s_fs_info;
  63. return ofs->indexdir;
  64. }
  65. /* Index all files on copy up. For now only enabled for NFS export */
  66. bool ovl_index_all(struct super_block *sb)
  67. {
  68. struct ovl_fs *ofs = sb->s_fs_info;
  69. return ofs->config.nfs_export && ofs->config.index;
  70. }
  71. /* Verify lower origin on lookup. For now only enabled for NFS export */
  72. bool ovl_verify_lower(struct super_block *sb)
  73. {
  74. struct ovl_fs *ofs = sb->s_fs_info;
  75. return ofs->config.nfs_export && ofs->config.index;
  76. }
  77. struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  78. {
  79. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  80. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  81. if (oe)
  82. oe->numlower = numlower;
  83. return oe;
  84. }
  85. #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
  86. bool ovl_dentry_remote(struct dentry *dentry)
  87. {
  88. return dentry->d_flags & OVL_D_REVALIDATE;
  89. }
  90. void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
  91. {
  92. if (!ovl_dentry_remote(realdentry))
  93. return;
  94. spin_lock(&dentry->d_lock);
  95. dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
  96. spin_unlock(&dentry->d_lock);
  97. }
  98. void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry)
  99. {
  100. return ovl_dentry_init_flags(dentry, upperdentry, OVL_D_REVALIDATE);
  101. }
  102. void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
  103. unsigned int mask)
  104. {
  105. struct ovl_entry *oe = OVL_E(dentry);
  106. unsigned int i, flags = 0;
  107. if (upperdentry)
  108. flags |= upperdentry->d_flags;
  109. for (i = 0; i < oe->numlower; i++)
  110. flags |= oe->lowerstack[i].dentry->d_flags;
  111. spin_lock(&dentry->d_lock);
  112. dentry->d_flags &= ~mask;
  113. dentry->d_flags |= flags & mask;
  114. spin_unlock(&dentry->d_lock);
  115. }
  116. bool ovl_dentry_weird(struct dentry *dentry)
  117. {
  118. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  119. DCACHE_MANAGE_TRANSIT |
  120. DCACHE_OP_HASH |
  121. DCACHE_OP_COMPARE);
  122. }
  123. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  124. {
  125. struct ovl_entry *oe = dentry->d_fsdata;
  126. enum ovl_path_type type = 0;
  127. if (ovl_dentry_upper(dentry)) {
  128. type = __OVL_PATH_UPPER;
  129. /*
  130. * Non-dir dentry can hold lower dentry of its copy up origin.
  131. */
  132. if (oe->numlower) {
  133. if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
  134. type |= __OVL_PATH_ORIGIN;
  135. if (d_is_dir(dentry) ||
  136. !ovl_has_upperdata(d_inode(dentry)))
  137. type |= __OVL_PATH_MERGE;
  138. }
  139. } else {
  140. if (oe->numlower > 1)
  141. type |= __OVL_PATH_MERGE;
  142. }
  143. return type;
  144. }
  145. void ovl_path_upper(struct dentry *dentry, struct path *path)
  146. {
  147. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  148. path->mnt = ovl_upper_mnt(ofs);
  149. path->dentry = ovl_dentry_upper(dentry);
  150. }
  151. void ovl_path_lower(struct dentry *dentry, struct path *path)
  152. {
  153. struct ovl_entry *oe = dentry->d_fsdata;
  154. if (oe->numlower) {
  155. path->mnt = oe->lowerstack[0].layer->mnt;
  156. path->dentry = oe->lowerstack[0].dentry;
  157. } else {
  158. *path = (struct path) { };
  159. }
  160. }
  161. void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
  162. {
  163. struct ovl_entry *oe = dentry->d_fsdata;
  164. if (oe->numlower) {
  165. path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
  166. path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
  167. } else {
  168. *path = (struct path) { };
  169. }
  170. }
  171. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  172. {
  173. enum ovl_path_type type = ovl_path_type(dentry);
  174. if (!OVL_TYPE_UPPER(type))
  175. ovl_path_lower(dentry, path);
  176. else
  177. ovl_path_upper(dentry, path);
  178. return type;
  179. }
  180. enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
  181. {
  182. enum ovl_path_type type = ovl_path_type(dentry);
  183. WARN_ON_ONCE(d_is_dir(dentry));
  184. if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
  185. ovl_path_lowerdata(dentry, path);
  186. else
  187. ovl_path_upper(dentry, path);
  188. return type;
  189. }
  190. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  191. {
  192. return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
  193. }
  194. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  195. {
  196. struct ovl_entry *oe = dentry->d_fsdata;
  197. return oe->numlower ? oe->lowerstack[0].dentry : NULL;
  198. }
  199. const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
  200. {
  201. struct ovl_entry *oe = dentry->d_fsdata;
  202. return oe->numlower ? oe->lowerstack[0].layer : NULL;
  203. }
  204. /*
  205. * ovl_dentry_lower() could return either a data dentry or metacopy dentry
  206. * depending on what is stored in lowerstack[0]. At times we need to find
  207. * lower dentry which has data (and not metacopy dentry). This helper
  208. * returns the lower data dentry.
  209. */
  210. struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
  211. {
  212. struct ovl_entry *oe = dentry->d_fsdata;
  213. return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
  214. }
  215. struct dentry *ovl_dentry_real(struct dentry *dentry)
  216. {
  217. return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
  218. }
  219. struct dentry *ovl_i_dentry_upper(struct inode *inode)
  220. {
  221. return ovl_upperdentry_dereference(OVL_I(inode));
  222. }
  223. struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
  224. {
  225. path->dentry = ovl_i_dentry_upper(inode);
  226. if (!path->dentry) {
  227. path->dentry = OVL_I(inode)->lowerpath.dentry;
  228. path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
  229. } else {
  230. path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
  231. }
  232. return path->dentry ? d_inode_rcu(path->dentry) : NULL;
  233. }
  234. struct inode *ovl_inode_upper(struct inode *inode)
  235. {
  236. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  237. return upperdentry ? d_inode(upperdentry) : NULL;
  238. }
  239. struct inode *ovl_inode_lower(struct inode *inode)
  240. {
  241. struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
  242. return lowerdentry ? d_inode(lowerdentry) : NULL;
  243. }
  244. struct inode *ovl_inode_real(struct inode *inode)
  245. {
  246. return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
  247. }
  248. /* Return inode which contains lower data. Do not return metacopy */
  249. struct inode *ovl_inode_lowerdata(struct inode *inode)
  250. {
  251. if (WARN_ON(!S_ISREG(inode->i_mode)))
  252. return NULL;
  253. return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
  254. }
  255. /* Return real inode which contains data. Does not return metacopy inode */
  256. struct inode *ovl_inode_realdata(struct inode *inode)
  257. {
  258. struct inode *upperinode;
  259. upperinode = ovl_inode_upper(inode);
  260. if (upperinode && ovl_has_upperdata(inode))
  261. return upperinode;
  262. return ovl_inode_lowerdata(inode);
  263. }
  264. struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
  265. {
  266. return OVL_I(inode)->cache;
  267. }
  268. void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
  269. {
  270. OVL_I(inode)->cache = cache;
  271. }
  272. void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
  273. {
  274. set_bit(flag, &OVL_E(dentry)->flags);
  275. }
  276. void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
  277. {
  278. clear_bit(flag, &OVL_E(dentry)->flags);
  279. }
  280. bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
  281. {
  282. return test_bit(flag, &OVL_E(dentry)->flags);
  283. }
  284. bool ovl_dentry_is_opaque(struct dentry *dentry)
  285. {
  286. return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
  287. }
  288. bool ovl_dentry_is_whiteout(struct dentry *dentry)
  289. {
  290. return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
  291. }
  292. void ovl_dentry_set_opaque(struct dentry *dentry)
  293. {
  294. ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
  295. }
  296. /*
  297. * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
  298. * to return positive, while there's no actual upper alias for the inode.
  299. * Copy up code needs to know about the existence of the upper alias, so it
  300. * can't use ovl_dentry_upper().
  301. */
  302. bool ovl_dentry_has_upper_alias(struct dentry *dentry)
  303. {
  304. return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
  305. }
  306. void ovl_dentry_set_upper_alias(struct dentry *dentry)
  307. {
  308. ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
  309. }
  310. static bool ovl_should_check_upperdata(struct inode *inode)
  311. {
  312. if (!S_ISREG(inode->i_mode))
  313. return false;
  314. if (!ovl_inode_lower(inode))
  315. return false;
  316. return true;
  317. }
  318. bool ovl_has_upperdata(struct inode *inode)
  319. {
  320. if (!ovl_should_check_upperdata(inode))
  321. return true;
  322. if (!ovl_test_flag(OVL_UPPERDATA, inode))
  323. return false;
  324. /*
  325. * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
  326. * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
  327. * if setting of OVL_UPPERDATA is visible, then effects of writes
  328. * before that are visible too.
  329. */
  330. smp_rmb();
  331. return true;
  332. }
  333. void ovl_set_upperdata(struct inode *inode)
  334. {
  335. /*
  336. * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
  337. * if OVL_UPPERDATA flag is visible, then effects of write operations
  338. * before it are visible as well.
  339. */
  340. smp_wmb();
  341. ovl_set_flag(OVL_UPPERDATA, inode);
  342. }
  343. /* Caller should hold ovl_inode->lock */
  344. bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
  345. {
  346. if (!ovl_open_flags_need_copy_up(flags))
  347. return false;
  348. return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
  349. }
  350. bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
  351. {
  352. if (!ovl_open_flags_need_copy_up(flags))
  353. return false;
  354. return !ovl_has_upperdata(d_inode(dentry));
  355. }
  356. bool ovl_redirect_dir(struct super_block *sb)
  357. {
  358. struct ovl_fs *ofs = sb->s_fs_info;
  359. return ofs->config.redirect_dir && !ofs->noxattr;
  360. }
  361. const char *ovl_dentry_get_redirect(struct dentry *dentry)
  362. {
  363. return OVL_I(d_inode(dentry))->redirect;
  364. }
  365. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
  366. {
  367. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  368. kfree(oi->redirect);
  369. oi->redirect = redirect;
  370. }
  371. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
  372. {
  373. struct inode *upperinode = d_inode(upperdentry);
  374. WARN_ON(OVL_I(inode)->__upperdentry);
  375. /*
  376. * Make sure upperdentry is consistent before making it visible
  377. */
  378. smp_wmb();
  379. OVL_I(inode)->__upperdentry = upperdentry;
  380. if (inode_unhashed(inode)) {
  381. inode->i_private = upperinode;
  382. __insert_inode_hash(inode, (unsigned long) upperinode);
  383. }
  384. }
  385. static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
  386. {
  387. struct inode *inode = d_inode(dentry);
  388. WARN_ON(!inode_is_locked(inode));
  389. WARN_ON(!d_is_dir(dentry));
  390. /*
  391. * Version is used by readdir code to keep cache consistent.
  392. * For merge dirs (or dirs with origin) all changes need to be noted.
  393. * For non-merge dirs, cache contains only impure entries (i.e. ones
  394. * which have been copied up and have origins), so only need to note
  395. * changes to impure entries.
  396. */
  397. if (!ovl_dir_is_real(dentry) || impurity)
  398. OVL_I(inode)->version++;
  399. }
  400. void ovl_dir_modified(struct dentry *dentry, bool impurity)
  401. {
  402. /* Copy mtime/ctime */
  403. ovl_copyattr(d_inode(dentry));
  404. ovl_dir_version_inc(dentry, impurity);
  405. }
  406. u64 ovl_dentry_version_get(struct dentry *dentry)
  407. {
  408. struct inode *inode = d_inode(dentry);
  409. WARN_ON(!inode_is_locked(inode));
  410. return OVL_I(inode)->version;
  411. }
  412. bool ovl_is_whiteout(struct dentry *dentry)
  413. {
  414. struct inode *inode = dentry->d_inode;
  415. return inode && IS_WHITEOUT(inode);
  416. }
  417. struct file *ovl_path_open(const struct path *path, int flags)
  418. {
  419. struct inode *inode = d_inode(path->dentry);
  420. struct user_namespace *real_mnt_userns = mnt_user_ns(path->mnt);
  421. int err, acc_mode;
  422. if (flags & ~(O_ACCMODE | O_LARGEFILE))
  423. BUG();
  424. switch (flags & O_ACCMODE) {
  425. case O_RDONLY:
  426. acc_mode = MAY_READ;
  427. break;
  428. case O_WRONLY:
  429. acc_mode = MAY_WRITE;
  430. break;
  431. default:
  432. BUG();
  433. }
  434. err = inode_permission(real_mnt_userns, inode, acc_mode | MAY_OPEN);
  435. if (err)
  436. return ERR_PTR(err);
  437. /* O_NOATIME is an optimization, don't fail if not permitted */
  438. if (inode_owner_or_capable(real_mnt_userns, inode))
  439. flags |= O_NOATIME;
  440. return dentry_open(path, flags, current_cred());
  441. }
  442. /* Caller should hold ovl_inode->lock */
  443. static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
  444. {
  445. bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
  446. if (ovl_dentry_upper(dentry) &&
  447. (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
  448. !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
  449. return true;
  450. return false;
  451. }
  452. bool ovl_already_copied_up(struct dentry *dentry, int flags)
  453. {
  454. bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
  455. /*
  456. * Check if copy-up has happened as well as for upper alias (in
  457. * case of hard links) is there.
  458. *
  459. * Both checks are lockless:
  460. * - false negatives: will recheck under oi->lock
  461. * - false positives:
  462. * + ovl_dentry_upper() uses memory barriers to ensure the
  463. * upper dentry is up-to-date
  464. * + ovl_dentry_has_upper_alias() relies on locking of
  465. * upper parent i_rwsem to prevent reordering copy-up
  466. * with rename.
  467. */
  468. if (ovl_dentry_upper(dentry) &&
  469. (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
  470. !ovl_dentry_needs_data_copy_up(dentry, flags))
  471. return true;
  472. return false;
  473. }
  474. int ovl_copy_up_start(struct dentry *dentry, int flags)
  475. {
  476. struct inode *inode = d_inode(dentry);
  477. int err;
  478. err = ovl_inode_lock_interruptible(inode);
  479. if (!err && ovl_already_copied_up_locked(dentry, flags)) {
  480. err = 1; /* Already copied up */
  481. ovl_inode_unlock(inode);
  482. }
  483. return err;
  484. }
  485. void ovl_copy_up_end(struct dentry *dentry)
  486. {
  487. ovl_inode_unlock(d_inode(dentry));
  488. }
  489. bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
  490. {
  491. int res;
  492. res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
  493. /* Zero size value means "copied up but origin unknown" */
  494. if (res >= 0)
  495. return true;
  496. return false;
  497. }
  498. bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
  499. enum ovl_xattr ox)
  500. {
  501. int res;
  502. char val;
  503. if (!d_is_dir(path->dentry))
  504. return false;
  505. res = ovl_path_getxattr(ofs, path, ox, &val, 1);
  506. if (res == 1 && val == 'y')
  507. return true;
  508. return false;
  509. }
  510. #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
  511. #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
  512. #define OVL_XATTR_ORIGIN_POSTFIX "origin"
  513. #define OVL_XATTR_IMPURE_POSTFIX "impure"
  514. #define OVL_XATTR_NLINK_POSTFIX "nlink"
  515. #define OVL_XATTR_UPPER_POSTFIX "upper"
  516. #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
  517. #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
  518. #define OVL_XATTR_TAB_ENTRY(x) \
  519. [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
  520. [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
  521. const char *const ovl_xattr_table[][2] = {
  522. OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
  523. OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
  524. OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
  525. OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
  526. OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
  527. OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
  528. OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
  529. OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
  530. };
  531. int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
  532. enum ovl_xattr ox, const void *value, size_t size,
  533. int xerr)
  534. {
  535. int err;
  536. if (ofs->noxattr)
  537. return xerr;
  538. err = ovl_setxattr(ofs, upperdentry, ox, value, size);
  539. if (err == -EOPNOTSUPP) {
  540. pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
  541. ofs->noxattr = true;
  542. return xerr;
  543. }
  544. return err;
  545. }
  546. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
  547. {
  548. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  549. int err;
  550. if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
  551. return 0;
  552. /*
  553. * Do not fail when upper doesn't support xattrs.
  554. * Upper inodes won't have origin nor redirect xattr anyway.
  555. */
  556. err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
  557. if (!err)
  558. ovl_set_flag(OVL_IMPURE, d_inode(dentry));
  559. return err;
  560. }
  561. #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
  562. void ovl_check_protattr(struct inode *inode, struct dentry *upper)
  563. {
  564. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  565. u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
  566. char buf[OVL_PROTATTR_MAX+1];
  567. int res, n;
  568. res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
  569. OVL_PROTATTR_MAX);
  570. if (res < 0)
  571. return;
  572. /*
  573. * Initialize inode flags from overlay.protattr xattr and upper inode
  574. * flags. If upper inode has those fileattr flags set (i.e. from old
  575. * kernel), we do not clear them on ovl_get_inode(), but we will clear
  576. * them on next fileattr_set().
  577. */
  578. for (n = 0; n < res; n++) {
  579. if (buf[n] == 'a')
  580. iflags |= S_APPEND;
  581. else if (buf[n] == 'i')
  582. iflags |= S_IMMUTABLE;
  583. else
  584. break;
  585. }
  586. if (!res || n < res) {
  587. pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
  588. upper, res);
  589. } else {
  590. inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
  591. }
  592. }
  593. int ovl_set_protattr(struct inode *inode, struct dentry *upper,
  594. struct fileattr *fa)
  595. {
  596. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  597. char buf[OVL_PROTATTR_MAX];
  598. int len = 0, err = 0;
  599. u32 iflags = 0;
  600. BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
  601. if (fa->flags & FS_APPEND_FL) {
  602. buf[len++] = 'a';
  603. iflags |= S_APPEND;
  604. }
  605. if (fa->flags & FS_IMMUTABLE_FL) {
  606. buf[len++] = 'i';
  607. iflags |= S_IMMUTABLE;
  608. }
  609. /*
  610. * Do not allow to set protection flags when upper doesn't support
  611. * xattrs, because we do not set those fileattr flags on upper inode.
  612. * Remove xattr if it exist and all protection flags are cleared.
  613. */
  614. if (len) {
  615. err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
  616. buf, len, -EPERM);
  617. } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
  618. err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
  619. if (err == -EOPNOTSUPP || err == -ENODATA)
  620. err = 0;
  621. }
  622. if (err)
  623. return err;
  624. inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
  625. /* Mask out the fileattr flags that should not be set in upper inode */
  626. fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
  627. fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
  628. return 0;
  629. }
  630. /**
  631. * Caller must hold a reference to inode to prevent it from being freed while
  632. * it is marked inuse.
  633. */
  634. bool ovl_inuse_trylock(struct dentry *dentry)
  635. {
  636. struct inode *inode = d_inode(dentry);
  637. bool locked = false;
  638. spin_lock(&inode->i_lock);
  639. if (!(inode->i_state & I_OVL_INUSE)) {
  640. inode->i_state |= I_OVL_INUSE;
  641. locked = true;
  642. }
  643. spin_unlock(&inode->i_lock);
  644. return locked;
  645. }
  646. void ovl_inuse_unlock(struct dentry *dentry)
  647. {
  648. if (dentry) {
  649. struct inode *inode = d_inode(dentry);
  650. spin_lock(&inode->i_lock);
  651. WARN_ON(!(inode->i_state & I_OVL_INUSE));
  652. inode->i_state &= ~I_OVL_INUSE;
  653. spin_unlock(&inode->i_lock);
  654. }
  655. }
  656. bool ovl_is_inuse(struct dentry *dentry)
  657. {
  658. struct inode *inode = d_inode(dentry);
  659. bool inuse;
  660. spin_lock(&inode->i_lock);
  661. inuse = (inode->i_state & I_OVL_INUSE);
  662. spin_unlock(&inode->i_lock);
  663. return inuse;
  664. }
  665. /*
  666. * Does this overlay dentry need to be indexed on copy up?
  667. */
  668. bool ovl_need_index(struct dentry *dentry)
  669. {
  670. struct dentry *lower = ovl_dentry_lower(dentry);
  671. if (!lower || !ovl_indexdir(dentry->d_sb))
  672. return false;
  673. /* Index all files for NFS export and consistency verification */
  674. if (ovl_index_all(dentry->d_sb))
  675. return true;
  676. /* Index only lower hardlinks on copy up */
  677. if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  678. return true;
  679. return false;
  680. }
  681. /* Caller must hold OVL_I(inode)->lock */
  682. static void ovl_cleanup_index(struct dentry *dentry)
  683. {
  684. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  685. struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
  686. struct inode *dir = indexdir->d_inode;
  687. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  688. struct dentry *upperdentry = ovl_dentry_upper(dentry);
  689. struct dentry *index = NULL;
  690. struct inode *inode;
  691. struct qstr name = { };
  692. int err;
  693. err = ovl_get_index_name(ofs, lowerdentry, &name);
  694. if (err)
  695. goto fail;
  696. inode = d_inode(upperdentry);
  697. if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
  698. pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
  699. upperdentry, inode->i_ino, inode->i_nlink);
  700. /*
  701. * We either have a bug with persistent union nlink or a lower
  702. * hardlink was added while overlay is mounted. Adding a lower
  703. * hardlink and then unlinking all overlay hardlinks would drop
  704. * overlay nlink to zero before all upper inodes are unlinked.
  705. * As a safety measure, when that situation is detected, set
  706. * the overlay nlink to the index inode nlink minus one for the
  707. * index entry itself.
  708. */
  709. set_nlink(d_inode(dentry), inode->i_nlink - 1);
  710. ovl_set_nlink_upper(dentry);
  711. goto out;
  712. }
  713. inode_lock_nested(dir, I_MUTEX_PARENT);
  714. index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
  715. err = PTR_ERR(index);
  716. if (IS_ERR(index)) {
  717. index = NULL;
  718. } else if (ovl_index_all(dentry->d_sb)) {
  719. /* Whiteout orphan index to block future open by handle */
  720. err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
  721. dir, index);
  722. } else {
  723. /* Cleanup orphan index entries */
  724. err = ovl_cleanup(ofs, dir, index);
  725. }
  726. inode_unlock(dir);
  727. if (err)
  728. goto fail;
  729. out:
  730. kfree(name.name);
  731. dput(index);
  732. return;
  733. fail:
  734. pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
  735. goto out;
  736. }
  737. /*
  738. * Operations that change overlay inode and upper inode nlink need to be
  739. * synchronized with copy up for persistent nlink accounting.
  740. */
  741. int ovl_nlink_start(struct dentry *dentry)
  742. {
  743. struct inode *inode = d_inode(dentry);
  744. const struct cred *old_cred;
  745. int err;
  746. if (WARN_ON(!inode))
  747. return -ENOENT;
  748. /*
  749. * With inodes index is enabled, we store the union overlay nlink
  750. * in an xattr on the index inode. When whiting out an indexed lower,
  751. * we need to decrement the overlay persistent nlink, but before the
  752. * first copy up, we have no upper index inode to store the xattr.
  753. *
  754. * As a workaround, before whiteout/rename over an indexed lower,
  755. * copy up to create the upper index. Creating the upper index will
  756. * initialize the overlay nlink, so it could be dropped if unlink
  757. * or rename succeeds.
  758. *
  759. * TODO: implement metadata only index copy up when called with
  760. * ovl_copy_up_flags(dentry, O_PATH).
  761. */
  762. if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
  763. err = ovl_copy_up(dentry);
  764. if (err)
  765. return err;
  766. }
  767. err = ovl_inode_lock_interruptible(inode);
  768. if (err)
  769. return err;
  770. if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
  771. goto out;
  772. old_cred = ovl_override_creds(dentry->d_sb);
  773. /*
  774. * The overlay inode nlink should be incremented/decremented IFF the
  775. * upper operation succeeds, along with nlink change of upper inode.
  776. * Therefore, before link/unlink/rename, we store the union nlink
  777. * value relative to the upper inode nlink in an upper inode xattr.
  778. */
  779. err = ovl_set_nlink_upper(dentry);
  780. ovl_revert_creds(dentry->d_sb, old_cred);
  781. out:
  782. if (err)
  783. ovl_inode_unlock(inode);
  784. return err;
  785. }
  786. void ovl_nlink_end(struct dentry *dentry)
  787. {
  788. struct inode *inode = d_inode(dentry);
  789. if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
  790. const struct cred *old_cred;
  791. old_cred = ovl_override_creds(dentry->d_sb);
  792. ovl_cleanup_index(dentry);
  793. ovl_revert_creds(dentry->d_sb, old_cred);
  794. }
  795. ovl_inode_unlock(inode);
  796. }
  797. int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
  798. {
  799. /* Workdir should not be the same as upperdir */
  800. if (workdir == upperdir)
  801. goto err;
  802. /* Workdir should not be subdir of upperdir and vice versa */
  803. if (lock_rename(workdir, upperdir) != NULL)
  804. goto err_unlock;
  805. return 0;
  806. err_unlock:
  807. unlock_rename(workdir, upperdir);
  808. err:
  809. pr_err("failed to lock workdir+upperdir\n");
  810. return -EIO;
  811. }
  812. /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
  813. int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path)
  814. {
  815. int res;
  816. /* Only regular files can have metacopy xattr */
  817. if (!S_ISREG(d_inode(path->dentry)->i_mode))
  818. return 0;
  819. res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
  820. if (res < 0) {
  821. if (res == -ENODATA || res == -EOPNOTSUPP)
  822. return 0;
  823. /*
  824. * getxattr on user.* may fail with EACCES in case there's no
  825. * read permission on the inode. Not much we can do, other than
  826. * tell the caller that this is not a metacopy inode.
  827. */
  828. if (ofs->config.userxattr && res == -EACCES)
  829. return 0;
  830. goto out;
  831. }
  832. return 1;
  833. out:
  834. pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
  835. return res;
  836. }
  837. bool ovl_is_metacopy_dentry(struct dentry *dentry)
  838. {
  839. struct ovl_entry *oe = dentry->d_fsdata;
  840. if (!d_is_reg(dentry))
  841. return false;
  842. if (ovl_dentry_upper(dentry)) {
  843. if (!ovl_has_upperdata(d_inode(dentry)))
  844. return true;
  845. return false;
  846. }
  847. return (oe->numlower > 1);
  848. }
  849. char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
  850. {
  851. int res;
  852. char *s, *next, *buf = NULL;
  853. res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
  854. if (res == -ENODATA || res == -EOPNOTSUPP)
  855. return NULL;
  856. if (res < 0)
  857. goto fail;
  858. if (res == 0)
  859. goto invalid;
  860. buf = kzalloc(res + padding + 1, GFP_KERNEL);
  861. if (!buf)
  862. return ERR_PTR(-ENOMEM);
  863. res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
  864. if (res < 0)
  865. goto fail;
  866. if (res == 0)
  867. goto invalid;
  868. if (buf[0] == '/') {
  869. for (s = buf; *s++ == '/'; s = next) {
  870. next = strchrnul(s, '/');
  871. if (s == next)
  872. goto invalid;
  873. }
  874. } else {
  875. if (strchr(buf, '/') != NULL)
  876. goto invalid;
  877. }
  878. return buf;
  879. invalid:
  880. pr_warn_ratelimited("invalid redirect (%s)\n", buf);
  881. res = -EINVAL;
  882. goto err_free;
  883. fail:
  884. pr_warn_ratelimited("failed to get redirect (%i)\n", res);
  885. err_free:
  886. kfree(buf);
  887. return ERR_PTR(res);
  888. }
  889. /*
  890. * ovl_sync_status() - Check fs sync status for volatile mounts
  891. *
  892. * Returns 1 if this is not a volatile mount and a real sync is required.
  893. *
  894. * Returns 0 if syncing can be skipped because mount is volatile, and no errors
  895. * have occurred on the upperdir since the mount.
  896. *
  897. * Returns -errno if it is a volatile mount, and the error that occurred since
  898. * the last mount. If the error code changes, it'll return the latest error
  899. * code.
  900. */
  901. int ovl_sync_status(struct ovl_fs *ofs)
  902. {
  903. struct vfsmount *mnt;
  904. if (ovl_should_sync(ofs))
  905. return 1;
  906. mnt = ovl_upper_mnt(ofs);
  907. if (!mnt)
  908. return 0;
  909. return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
  910. }
  911. /*
  912. * ovl_copyattr() - copy inode attributes from layer to ovl inode
  913. *
  914. * When overlay copies inode information from an upper or lower layer to the
  915. * relevant overlay inode it will apply the idmapping of the upper or lower
  916. * layer when doing so ensuring that the ovl inode ownership will correctly
  917. * reflect the ownership of the idmapped upper or lower layer. For example, an
  918. * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
  919. * map any lower or upper inode owned by id 1001 to id 1000. These mapping
  920. * helpers are nops when the relevant layer isn't idmapped.
  921. */
  922. void ovl_copyattr(struct inode *inode)
  923. {
  924. struct path realpath;
  925. struct inode *realinode;
  926. struct user_namespace *real_mnt_userns;
  927. realinode = ovl_i_path_real(inode, &realpath);
  928. real_mnt_userns = mnt_user_ns(realpath.mnt);
  929. inode->i_uid = i_uid_into_mnt(real_mnt_userns, realinode);
  930. inode->i_gid = i_gid_into_mnt(real_mnt_userns, realinode);
  931. inode->i_mode = realinode->i_mode;
  932. inode->i_atime = realinode->i_atime;
  933. inode->i_mtime = realinode->i_mtime;
  934. inode->i_ctime = realinode->i_ctime;
  935. i_size_write(inode, i_size_read(realinode));
  936. }