inode.c 23 KB

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