util.c 22 KB

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