inode.c 26 KB

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