util.c 21 KB

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