inode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell 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/slab.h>
  11. #include <linux/cred.h>
  12. #include <linux/xattr.h>
  13. #include <linux/posix_acl.h>
  14. #include <linux/ratelimit.h>
  15. #include "overlayfs.h"
  16. static dev_t ovl_get_pseudo_dev(struct dentry *dentry)
  17. {
  18. struct ovl_entry *oe = dentry->d_fsdata;
  19. return oe->lowerstack[0].layer->pseudo_dev;
  20. }
  21. int ovl_setattr(struct dentry *dentry, struct iattr *attr)
  22. {
  23. int err;
  24. struct dentry *upperdentry;
  25. const struct cred *old_cred;
  26. /*
  27. * Check for permissions before trying to copy-up. This is redundant
  28. * since it will be rechecked later by ->setattr() on upper dentry. But
  29. * without this, copy-up can be triggered by just about anybody.
  30. *
  31. * We don't initialize inode->size, which just means that
  32. * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
  33. * check for a swapfile (which this won't be anyway).
  34. */
  35. err = setattr_prepare(dentry, attr);
  36. if (err)
  37. return err;
  38. err = ovl_want_write(dentry);
  39. if (err)
  40. goto out;
  41. err = ovl_copy_up(dentry);
  42. if (!err) {
  43. upperdentry = ovl_dentry_upper(dentry);
  44. if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  45. attr->ia_valid &= ~ATTR_MODE;
  46. inode_lock(upperdentry->d_inode);
  47. old_cred = ovl_override_creds(dentry->d_sb);
  48. err = notify_change(upperdentry, attr, NULL);
  49. revert_creds(old_cred);
  50. if (!err)
  51. ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
  52. inode_unlock(upperdentry->d_inode);
  53. }
  54. ovl_drop_write(dentry);
  55. out:
  56. return err;
  57. }
  58. int ovl_getattr(const struct path *path, struct kstat *stat,
  59. u32 request_mask, unsigned int flags)
  60. {
  61. struct dentry *dentry = path->dentry;
  62. enum ovl_path_type type;
  63. struct path realpath;
  64. const struct cred *old_cred;
  65. bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
  66. bool samefs = ovl_same_sb(dentry->d_sb);
  67. int err;
  68. type = ovl_path_real(dentry, &realpath);
  69. old_cred = ovl_override_creds(dentry->d_sb);
  70. err = vfs_getattr(&realpath, stat, request_mask, flags);
  71. if (err)
  72. goto out;
  73. /*
  74. * For non-dir or same fs, we use st_ino of the copy up origin, if we
  75. * know it. This guaranties constant st_dev/st_ino across copy up.
  76. *
  77. * If filesystem supports NFS export ops, this also guaranties
  78. * persistent st_ino across mount cycle.
  79. */
  80. if (!is_dir || samefs) {
  81. if (OVL_TYPE_ORIGIN(type)) {
  82. struct kstat lowerstat;
  83. u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
  84. ovl_path_lower(dentry, &realpath);
  85. err = vfs_getattr(&realpath, &lowerstat,
  86. lowermask, flags);
  87. if (err)
  88. goto out;
  89. /*
  90. * Lower hardlinks may be broken on copy up to different
  91. * upper files, so we cannot use the lower origin st_ino
  92. * for those different files, even for the same fs case.
  93. *
  94. * Similarly, several redirected dirs can point to the
  95. * same dir on a lower layer. With the "verify_lower"
  96. * feature, we do not use the lower origin st_ino, if
  97. * we haven't verified that this redirect is unique.
  98. *
  99. * With inodes index enabled, it is safe to use st_ino
  100. * of an indexed origin. The index validates that the
  101. * upper hardlink is not broken and that a redirected
  102. * dir is the only redirect to that origin.
  103. */
  104. if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
  105. (!ovl_verify_lower(dentry->d_sb) &&
  106. (is_dir || lowerstat.nlink == 1)))
  107. stat->ino = lowerstat.ino;
  108. if (samefs)
  109. WARN_ON_ONCE(stat->dev != lowerstat.dev);
  110. else
  111. stat->dev = ovl_get_pseudo_dev(dentry);
  112. }
  113. if (samefs) {
  114. /*
  115. * When all layers are on the same fs, all real inode
  116. * number are unique, so we use the overlay st_dev,
  117. * which is friendly to du -x.
  118. */
  119. stat->dev = dentry->d_sb->s_dev;
  120. } else if (!OVL_TYPE_UPPER(type)) {
  121. /*
  122. * For non-samefs setup, to make sure that st_dev/st_ino
  123. * pair is unique across the system, we use a unique
  124. * anonymous st_dev for lower layer inode.
  125. */
  126. stat->dev = ovl_get_pseudo_dev(dentry);
  127. }
  128. } else {
  129. /*
  130. * Always use the overlay st_dev for directories, so 'find
  131. * -xdev' will scan the entire overlay mount and won't cross the
  132. * overlay mount boundaries.
  133. *
  134. * If not all layers are on the same fs the pair {real st_ino;
  135. * overlay st_dev} is not unique, so use the non persistent
  136. * overlay st_ino for directories.
  137. */
  138. stat->dev = dentry->d_sb->s_dev;
  139. stat->ino = dentry->d_inode->i_ino;
  140. }
  141. /*
  142. * It's probably not worth it to count subdirs to get the
  143. * correct link count. nlink=1 seems to pacify 'find' and
  144. * other utilities.
  145. */
  146. if (is_dir && OVL_TYPE_MERGE(type))
  147. stat->nlink = 1;
  148. /*
  149. * Return the overlay inode nlinks for indexed upper inodes.
  150. * Overlay inode nlink counts the union of the upper hardlinks
  151. * and non-covered lower hardlinks. It does not include the upper
  152. * index hardlink.
  153. */
  154. if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  155. stat->nlink = dentry->d_inode->i_nlink;
  156. out:
  157. revert_creds(old_cred);
  158. return err;
  159. }
  160. int ovl_permission(struct inode *inode, int mask)
  161. {
  162. struct inode *upperinode = ovl_inode_upper(inode);
  163. struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
  164. const struct cred *old_cred;
  165. int err;
  166. /* Careful in RCU walk mode */
  167. if (!realinode) {
  168. WARN_ON(!(mask & MAY_NOT_BLOCK));
  169. return -ECHILD;
  170. }
  171. /*
  172. * Check overlay inode with the creds of task and underlying inode
  173. * with creds of mounter
  174. */
  175. err = generic_permission(inode, mask);
  176. if (err)
  177. return err;
  178. old_cred = ovl_override_creds(inode->i_sb);
  179. if (!upperinode &&
  180. !special_file(realinode->i_mode) && mask & MAY_WRITE) {
  181. mask &= ~(MAY_WRITE | MAY_APPEND);
  182. /* Make sure mounter can read file for copy up later */
  183. mask |= MAY_READ;
  184. }
  185. err = inode_permission(realinode, mask);
  186. revert_creds(old_cred);
  187. return err;
  188. }
  189. static const char *ovl_get_link(struct dentry *dentry,
  190. struct inode *inode,
  191. struct delayed_call *done)
  192. {
  193. const struct cred *old_cred;
  194. const char *p;
  195. if (!dentry)
  196. return ERR_PTR(-ECHILD);
  197. old_cred = ovl_override_creds(dentry->d_sb);
  198. p = vfs_get_link(ovl_dentry_real(dentry), done);
  199. revert_creds(old_cred);
  200. return p;
  201. }
  202. bool ovl_is_private_xattr(const char *name)
  203. {
  204. return strncmp(name, OVL_XATTR_PREFIX,
  205. sizeof(OVL_XATTR_PREFIX) - 1) == 0;
  206. }
  207. int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
  208. const void *value, size_t size, int flags)
  209. {
  210. int err;
  211. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  212. struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
  213. const struct cred *old_cred;
  214. err = ovl_want_write(dentry);
  215. if (err)
  216. goto out;
  217. if (!value && !upperdentry) {
  218. err = vfs_getxattr(realdentry, name, NULL, 0);
  219. if (err < 0)
  220. goto out_drop_write;
  221. }
  222. if (!upperdentry) {
  223. err = ovl_copy_up(dentry);
  224. if (err)
  225. goto out_drop_write;
  226. realdentry = ovl_dentry_upper(dentry);
  227. }
  228. old_cred = ovl_override_creds(dentry->d_sb);
  229. if (value)
  230. err = vfs_setxattr(realdentry, name, value, size, flags);
  231. else {
  232. WARN_ON(flags != XATTR_REPLACE);
  233. err = vfs_removexattr(realdentry, name);
  234. }
  235. revert_creds(old_cred);
  236. out_drop_write:
  237. ovl_drop_write(dentry);
  238. out:
  239. return err;
  240. }
  241. int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
  242. void *value, size_t size)
  243. {
  244. ssize_t res;
  245. const struct cred *old_cred;
  246. struct dentry *realdentry =
  247. ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
  248. old_cred = ovl_override_creds(dentry->d_sb);
  249. res = vfs_getxattr(realdentry, name, value, size);
  250. revert_creds(old_cred);
  251. return res;
  252. }
  253. static bool ovl_can_list(const char *s)
  254. {
  255. /* List all non-trusted xatts */
  256. if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
  257. return true;
  258. /* Never list trusted.overlay, list other trusted for superuser only */
  259. return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
  260. }
  261. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  262. {
  263. struct dentry *realdentry = ovl_dentry_real(dentry);
  264. ssize_t res;
  265. size_t len;
  266. char *s;
  267. const struct cred *old_cred;
  268. old_cred = ovl_override_creds(dentry->d_sb);
  269. res = vfs_listxattr(realdentry, list, size);
  270. revert_creds(old_cred);
  271. if (res <= 0 || size == 0)
  272. return res;
  273. /* filter out private xattrs */
  274. for (s = list, len = res; len;) {
  275. size_t slen = strnlen(s, len) + 1;
  276. /* underlying fs providing us with an broken xattr list? */
  277. if (WARN_ON(slen > len))
  278. return -EIO;
  279. len -= slen;
  280. if (!ovl_can_list(s)) {
  281. res -= slen;
  282. memmove(s, s + slen, len);
  283. } else {
  284. s += slen;
  285. }
  286. }
  287. return res;
  288. }
  289. struct posix_acl *ovl_get_acl(struct inode *inode, int type)
  290. {
  291. struct inode *realinode = ovl_inode_real(inode);
  292. const struct cred *old_cred;
  293. struct posix_acl *acl;
  294. if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
  295. return NULL;
  296. old_cred = ovl_override_creds(inode->i_sb);
  297. acl = get_acl(realinode, type);
  298. revert_creds(old_cred);
  299. return acl;
  300. }
  301. static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
  302. {
  303. /* Copy up of disconnected dentry does not set upper alias */
  304. if (ovl_dentry_upper(dentry) &&
  305. (ovl_dentry_has_upper_alias(dentry) ||
  306. (dentry->d_flags & DCACHE_DISCONNECTED)))
  307. return false;
  308. if (special_file(d_inode(dentry)->i_mode))
  309. return false;
  310. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  311. return false;
  312. return true;
  313. }
  314. int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
  315. {
  316. int err = 0;
  317. if (ovl_open_need_copy_up(dentry, file_flags)) {
  318. err = ovl_want_write(dentry);
  319. if (!err) {
  320. err = ovl_copy_up_flags(dentry, file_flags);
  321. ovl_drop_write(dentry);
  322. }
  323. }
  324. return err;
  325. }
  326. int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
  327. {
  328. struct dentry *alias;
  329. struct path upperpath;
  330. if (!(flags & S_ATIME))
  331. return 0;
  332. alias = d_find_any_alias(inode);
  333. if (!alias)
  334. return 0;
  335. ovl_path_upper(alias, &upperpath);
  336. if (upperpath.dentry) {
  337. touch_atime(&upperpath);
  338. inode->i_atime = d_inode(upperpath.dentry)->i_atime;
  339. }
  340. dput(alias);
  341. return 0;
  342. }
  343. static const struct inode_operations ovl_file_inode_operations = {
  344. .setattr = ovl_setattr,
  345. .permission = ovl_permission,
  346. .getattr = ovl_getattr,
  347. .listxattr = ovl_listxattr,
  348. .get_acl = ovl_get_acl,
  349. .update_time = ovl_update_time,
  350. };
  351. static const struct inode_operations ovl_symlink_inode_operations = {
  352. .setattr = ovl_setattr,
  353. .get_link = ovl_get_link,
  354. .getattr = ovl_getattr,
  355. .listxattr = ovl_listxattr,
  356. .update_time = ovl_update_time,
  357. };
  358. /*
  359. * It is possible to stack overlayfs instance on top of another
  360. * overlayfs instance as lower layer. We need to annonate the
  361. * stackable i_mutex locks according to stack level of the super
  362. * block instance. An overlayfs instance can never be in stack
  363. * depth 0 (there is always a real fs below it). An overlayfs
  364. * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
  365. *
  366. * For example, here is a snip from /proc/lockdep_chains after
  367. * dir_iterate of nested overlayfs:
  368. *
  369. * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
  370. * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
  371. * [...] &type->i_mutex_dir_key (stack_depth=0)
  372. */
  373. #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
  374. static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
  375. {
  376. #ifdef CONFIG_LOCKDEP
  377. static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
  378. static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
  379. static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
  380. int depth = inode->i_sb->s_stack_depth - 1;
  381. if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
  382. depth = 0;
  383. if (S_ISDIR(inode->i_mode))
  384. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
  385. else
  386. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
  387. lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
  388. #endif
  389. }
  390. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
  391. {
  392. inode->i_ino = get_next_ino();
  393. inode->i_mode = mode;
  394. inode->i_flags |= S_NOCMTIME;
  395. #ifdef CONFIG_FS_POSIX_ACL
  396. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  397. #endif
  398. ovl_lockdep_annotate_inode_mutex_key(inode);
  399. switch (mode & S_IFMT) {
  400. case S_IFREG:
  401. inode->i_op = &ovl_file_inode_operations;
  402. break;
  403. case S_IFDIR:
  404. inode->i_op = &ovl_dir_inode_operations;
  405. inode->i_fop = &ovl_dir_operations;
  406. break;
  407. case S_IFLNK:
  408. inode->i_op = &ovl_symlink_inode_operations;
  409. break;
  410. default:
  411. inode->i_op = &ovl_file_inode_operations;
  412. init_special_inode(inode, mode, rdev);
  413. break;
  414. }
  415. }
  416. /*
  417. * With inodes index enabled, an overlay inode nlink counts the union of upper
  418. * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
  419. * upper inode, the following nlink modifying operations can happen:
  420. *
  421. * 1. Lower hardlink copy up
  422. * 2. Upper hardlink created, unlinked or renamed over
  423. * 3. Lower hardlink whiteout or renamed over
  424. *
  425. * For the first, copy up case, the union nlink does not change, whether the
  426. * operation succeeds or fails, but the upper inode nlink may change.
  427. * Therefore, before copy up, we store the union nlink value relative to the
  428. * lower inode nlink in the index inode xattr trusted.overlay.nlink.
  429. *
  430. * For the second, upper hardlink case, the union nlink should be incremented
  431. * or decremented IFF the operation succeeds, aligned with nlink change of the
  432. * upper inode. Therefore, before link/unlink/rename, we store the union nlink
  433. * value relative to the upper inode nlink in the index inode.
  434. *
  435. * For the last, lower cover up case, we simplify things by preceding the
  436. * whiteout or cover up with copy up. This makes sure that there is an index
  437. * upper inode where the nlink xattr can be stored before the copied up upper
  438. * entry is unlink.
  439. */
  440. #define OVL_NLINK_ADD_UPPER (1 << 0)
  441. /*
  442. * On-disk format for indexed nlink:
  443. *
  444. * nlink relative to the upper inode - "U[+-]NUM"
  445. * nlink relative to the lower inode - "L[+-]NUM"
  446. */
  447. static int ovl_set_nlink_common(struct dentry *dentry,
  448. struct dentry *realdentry, const char *format)
  449. {
  450. struct inode *inode = d_inode(dentry);
  451. struct inode *realinode = d_inode(realdentry);
  452. char buf[13];
  453. int len;
  454. len = snprintf(buf, sizeof(buf), format,
  455. (int) (inode->i_nlink - realinode->i_nlink));
  456. if (WARN_ON(len >= sizeof(buf)))
  457. return -EIO;
  458. return ovl_do_setxattr(ovl_dentry_upper(dentry),
  459. OVL_XATTR_NLINK, buf, len, 0);
  460. }
  461. int ovl_set_nlink_upper(struct dentry *dentry)
  462. {
  463. return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
  464. }
  465. int ovl_set_nlink_lower(struct dentry *dentry)
  466. {
  467. return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
  468. }
  469. unsigned int ovl_get_nlink(struct dentry *lowerdentry,
  470. struct dentry *upperdentry,
  471. unsigned int fallback)
  472. {
  473. int nlink_diff;
  474. int nlink;
  475. char buf[13];
  476. int err;
  477. if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
  478. return fallback;
  479. err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
  480. if (err < 0)
  481. goto fail;
  482. buf[err] = '\0';
  483. if ((buf[0] != 'L' && buf[0] != 'U') ||
  484. (buf[1] != '+' && buf[1] != '-'))
  485. goto fail;
  486. err = kstrtoint(buf + 1, 10, &nlink_diff);
  487. if (err < 0)
  488. goto fail;
  489. nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
  490. nlink += nlink_diff;
  491. if (nlink <= 0)
  492. goto fail;
  493. return nlink;
  494. fail:
  495. pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
  496. upperdentry, err);
  497. return fallback;
  498. }
  499. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  500. {
  501. struct inode *inode;
  502. inode = new_inode(sb);
  503. if (inode)
  504. ovl_fill_inode(inode, mode, rdev);
  505. return inode;
  506. }
  507. static int ovl_inode_test(struct inode *inode, void *data)
  508. {
  509. return inode->i_private == data;
  510. }
  511. static int ovl_inode_set(struct inode *inode, void *data)
  512. {
  513. inode->i_private = data;
  514. return 0;
  515. }
  516. static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
  517. struct dentry *upperdentry, bool strict)
  518. {
  519. /*
  520. * For directories, @strict verify from lookup path performs consistency
  521. * checks, so NULL lower/upper in dentry must match NULL lower/upper in
  522. * inode. Non @strict verify from NFS handle decode path passes NULL for
  523. * 'unknown' lower/upper.
  524. */
  525. if (S_ISDIR(inode->i_mode) && strict) {
  526. /* Real lower dir moved to upper layer under us? */
  527. if (!lowerdentry && ovl_inode_lower(inode))
  528. return false;
  529. /* Lookup of an uncovered redirect origin? */
  530. if (!upperdentry && ovl_inode_upper(inode))
  531. return false;
  532. }
  533. /*
  534. * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
  535. * This happens when finding a copied up overlay inode for a renamed
  536. * or hardlinked overlay dentry and lower dentry cannot be followed
  537. * by origin because lower fs does not support file handles.
  538. */
  539. if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
  540. return false;
  541. /*
  542. * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
  543. * This happens when finding a lower alias for a copied up hard link.
  544. */
  545. if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
  546. return false;
  547. return true;
  548. }
  549. struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
  550. bool is_upper)
  551. {
  552. struct inode *inode, *key = d_inode(real);
  553. inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
  554. if (!inode)
  555. return NULL;
  556. if (!ovl_verify_inode(inode, is_upper ? NULL : real,
  557. is_upper ? real : NULL, false)) {
  558. iput(inode);
  559. return ERR_PTR(-ESTALE);
  560. }
  561. return inode;
  562. }
  563. /*
  564. * Does overlay inode need to be hashed by lower inode?
  565. */
  566. static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
  567. struct dentry *lower, struct dentry *index)
  568. {
  569. struct ovl_fs *ofs = sb->s_fs_info;
  570. /* No, if pure upper */
  571. if (!lower)
  572. return false;
  573. /* Yes, if already indexed */
  574. if (index)
  575. return true;
  576. /* Yes, if won't be copied up */
  577. if (!ofs->upper_mnt)
  578. return true;
  579. /* No, if lower hardlink is or will be broken on copy up */
  580. if ((upper || !ovl_indexdir(sb)) &&
  581. !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  582. return false;
  583. /* No, if non-indexed upper with NFS export */
  584. if (sb->s_export_op && upper)
  585. return false;
  586. /* Otherwise, hash by lower inode for fsnotify */
  587. return true;
  588. }
  589. struct inode *ovl_get_inode(struct super_block *sb, struct dentry *upperdentry,
  590. struct dentry *lowerdentry, struct dentry *index,
  591. unsigned int numlower)
  592. {
  593. struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
  594. struct inode *inode;
  595. bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index);
  596. bool is_dir;
  597. if (!realinode)
  598. realinode = d_inode(lowerdentry);
  599. /*
  600. * Copy up origin (lower) may exist for non-indexed upper, but we must
  601. * not use lower as hash key if this is a broken hardlink.
  602. */
  603. is_dir = S_ISDIR(realinode->i_mode);
  604. if (upperdentry || bylower) {
  605. struct inode *key = d_inode(bylower ? lowerdentry :
  606. upperdentry);
  607. unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
  608. inode = iget5_locked(sb, (unsigned long) key,
  609. ovl_inode_test, ovl_inode_set, key);
  610. if (!inode)
  611. goto out_nomem;
  612. if (!(inode->i_state & I_NEW)) {
  613. /*
  614. * Verify that the underlying files stored in the inode
  615. * match those in the dentry.
  616. */
  617. if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
  618. true)) {
  619. iput(inode);
  620. inode = ERR_PTR(-ESTALE);
  621. goto out;
  622. }
  623. dput(upperdentry);
  624. goto out;
  625. }
  626. /* Recalculate nlink for non-dir due to indexing */
  627. if (!is_dir)
  628. nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
  629. set_nlink(inode, nlink);
  630. } else {
  631. /* Lower hardlink that will be broken on copy up */
  632. inode = new_inode(sb);
  633. if (!inode)
  634. goto out_nomem;
  635. }
  636. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
  637. ovl_inode_init(inode, upperdentry, lowerdentry);
  638. if (upperdentry && ovl_is_impuredir(upperdentry))
  639. ovl_set_flag(OVL_IMPURE, inode);
  640. /* Check for non-merge dir that may have whiteouts */
  641. if (is_dir) {
  642. if (((upperdentry && lowerdentry) || numlower > 1) ||
  643. ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
  644. ovl_set_flag(OVL_WHITEOUTS, inode);
  645. }
  646. }
  647. if (inode->i_state & I_NEW)
  648. unlock_new_inode(inode);
  649. out:
  650. return inode;
  651. out_nomem:
  652. inode = ERR_PTR(-ENOMEM);
  653. goto out;
  654. }