util.c 23 KB

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