inode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. stat->ino = lowerstat.ino;
  182. lower_layer = ovl_layer_lower(dentry);
  183. }
  184. /*
  185. * If we are querying a metacopy dentry and lower
  186. * dentry is data dentry, then use the blocks we
  187. * queried just now. We don't have to do additional
  188. * vfs_getattr(). If lower itself is metacopy, then
  189. * additional vfs_getattr() is unavoidable.
  190. */
  191. if (metacopy_blocks &&
  192. realpath.dentry == ovl_dentry_lowerdata(dentry)) {
  193. stat->blocks = lowerstat.blocks;
  194. metacopy_blocks = false;
  195. }
  196. }
  197. if (metacopy_blocks) {
  198. /*
  199. * If lower is not same as lowerdata or if there was
  200. * no origin on upper, we can end up here.
  201. */
  202. struct kstat lowerdatastat;
  203. u32 lowermask = STATX_BLOCKS;
  204. ovl_path_lowerdata(dentry, &realpath);
  205. err = vfs_getattr(&realpath, &lowerdatastat,
  206. lowermask, flags);
  207. if (err)
  208. goto out;
  209. stat->blocks = lowerdatastat.blocks;
  210. }
  211. }
  212. err = ovl_map_dev_ino(dentry, stat, lower_layer);
  213. if (err)
  214. goto out;
  215. /*
  216. * It's probably not worth it to count subdirs to get the
  217. * correct link count. nlink=1 seems to pacify 'find' and
  218. * other utilities.
  219. */
  220. if (is_dir && OVL_TYPE_MERGE(type))
  221. stat->nlink = 1;
  222. /*
  223. * Return the overlay inode nlinks for indexed upper inodes.
  224. * Overlay inode nlink counts the union of the upper hardlinks
  225. * and non-covered lower hardlinks. It does not include the upper
  226. * index hardlink.
  227. */
  228. if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  229. stat->nlink = dentry->d_inode->i_nlink;
  230. out:
  231. revert_creds(old_cred);
  232. return err;
  233. }
  234. int ovl_permission(struct inode *inode, int mask)
  235. {
  236. struct inode *upperinode = ovl_inode_upper(inode);
  237. struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
  238. const struct cred *old_cred;
  239. int err;
  240. /* Careful in RCU walk mode */
  241. if (!realinode) {
  242. WARN_ON(!(mask & MAY_NOT_BLOCK));
  243. return -ECHILD;
  244. }
  245. /*
  246. * Check overlay inode with the creds of task and underlying inode
  247. * with creds of mounter
  248. */
  249. err = generic_permission(inode, mask);
  250. if (err)
  251. return err;
  252. old_cred = ovl_override_creds(inode->i_sb);
  253. if (!upperinode &&
  254. !special_file(realinode->i_mode) && mask & MAY_WRITE) {
  255. mask &= ~(MAY_WRITE | MAY_APPEND);
  256. /* Make sure mounter can read file for copy up later */
  257. mask |= MAY_READ;
  258. }
  259. err = inode_permission(realinode, mask);
  260. revert_creds(old_cred);
  261. return err;
  262. }
  263. static const char *ovl_get_link(struct dentry *dentry,
  264. struct inode *inode,
  265. struct delayed_call *done)
  266. {
  267. const struct cred *old_cred;
  268. const char *p;
  269. if (!dentry)
  270. return ERR_PTR(-ECHILD);
  271. old_cred = ovl_override_creds(dentry->d_sb);
  272. p = vfs_get_link(ovl_dentry_real(dentry), done);
  273. revert_creds(old_cred);
  274. return p;
  275. }
  276. bool ovl_is_private_xattr(const char *name)
  277. {
  278. return strncmp(name, OVL_XATTR_PREFIX,
  279. sizeof(OVL_XATTR_PREFIX) - 1) == 0;
  280. }
  281. int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
  282. const void *value, size_t size, int flags)
  283. {
  284. int err;
  285. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  286. struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
  287. const struct cred *old_cred;
  288. err = ovl_want_write(dentry);
  289. if (err)
  290. goto out;
  291. if (!value && !upperdentry) {
  292. err = vfs_getxattr(realdentry, name, NULL, 0);
  293. if (err < 0)
  294. goto out_drop_write;
  295. }
  296. if (!upperdentry) {
  297. err = ovl_copy_up(dentry);
  298. if (err)
  299. goto out_drop_write;
  300. realdentry = ovl_dentry_upper(dentry);
  301. }
  302. old_cred = ovl_override_creds(dentry->d_sb);
  303. if (value)
  304. err = vfs_setxattr(realdentry, name, value, size, flags);
  305. else {
  306. WARN_ON(flags != XATTR_REPLACE);
  307. err = vfs_removexattr(realdentry, name);
  308. }
  309. revert_creds(old_cred);
  310. /* copy c/mtime */
  311. ovl_copyattr(d_inode(realdentry), inode);
  312. out_drop_write:
  313. ovl_drop_write(dentry);
  314. out:
  315. return err;
  316. }
  317. int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
  318. void *value, size_t size)
  319. {
  320. ssize_t res;
  321. const struct cred *old_cred;
  322. struct dentry *realdentry =
  323. ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
  324. old_cred = ovl_override_creds(dentry->d_sb);
  325. res = vfs_getxattr(realdentry, name, value, size);
  326. revert_creds(old_cred);
  327. return res;
  328. }
  329. static bool ovl_can_list(const char *s)
  330. {
  331. /* List all non-trusted xatts */
  332. if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
  333. return true;
  334. /* Never list trusted.overlay, list other trusted for superuser only */
  335. return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
  336. }
  337. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  338. {
  339. struct dentry *realdentry = ovl_dentry_real(dentry);
  340. ssize_t res;
  341. size_t len;
  342. char *s;
  343. const struct cred *old_cred;
  344. old_cred = ovl_override_creds(dentry->d_sb);
  345. res = vfs_listxattr(realdentry, list, size);
  346. revert_creds(old_cred);
  347. if (res <= 0 || size == 0)
  348. return res;
  349. /* filter out private xattrs */
  350. for (s = list, len = res; len;) {
  351. size_t slen = strnlen(s, len) + 1;
  352. /* underlying fs providing us with an broken xattr list? */
  353. if (WARN_ON(slen > len))
  354. return -EIO;
  355. len -= slen;
  356. if (!ovl_can_list(s)) {
  357. res -= slen;
  358. memmove(s, s + slen, len);
  359. } else {
  360. s += slen;
  361. }
  362. }
  363. return res;
  364. }
  365. struct posix_acl *ovl_get_acl(struct inode *inode, int type)
  366. {
  367. struct inode *realinode = ovl_inode_real(inode);
  368. const struct cred *old_cred;
  369. struct posix_acl *acl;
  370. if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
  371. return NULL;
  372. old_cred = ovl_override_creds(inode->i_sb);
  373. acl = get_acl(realinode, type);
  374. revert_creds(old_cred);
  375. return acl;
  376. }
  377. int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
  378. {
  379. if (flags & S_ATIME) {
  380. struct ovl_fs *ofs = inode->i_sb->s_fs_info;
  381. struct path upperpath = {
  382. .mnt = ofs->upper_mnt,
  383. .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
  384. };
  385. if (upperpath.dentry) {
  386. touch_atime(&upperpath);
  387. inode->i_atime = d_inode(upperpath.dentry)->i_atime;
  388. }
  389. }
  390. return 0;
  391. }
  392. static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  393. u64 start, u64 len)
  394. {
  395. int err;
  396. struct inode *realinode = ovl_inode_real(inode);
  397. const struct cred *old_cred;
  398. if (!realinode->i_op->fiemap)
  399. return -EOPNOTSUPP;
  400. old_cred = ovl_override_creds(inode->i_sb);
  401. if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
  402. filemap_write_and_wait(realinode->i_mapping);
  403. err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
  404. revert_creds(old_cred);
  405. return err;
  406. }
  407. static const struct inode_operations ovl_file_inode_operations = {
  408. .setattr = ovl_setattr,
  409. .permission = ovl_permission,
  410. .getattr = ovl_getattr,
  411. .listxattr = ovl_listxattr,
  412. .get_acl = ovl_get_acl,
  413. .update_time = ovl_update_time,
  414. .fiemap = ovl_fiemap,
  415. };
  416. static const struct inode_operations ovl_symlink_inode_operations = {
  417. .setattr = ovl_setattr,
  418. .get_link = ovl_get_link,
  419. .getattr = ovl_getattr,
  420. .listxattr = ovl_listxattr,
  421. .update_time = ovl_update_time,
  422. };
  423. static const struct inode_operations ovl_special_inode_operations = {
  424. .setattr = ovl_setattr,
  425. .permission = ovl_permission,
  426. .getattr = ovl_getattr,
  427. .listxattr = ovl_listxattr,
  428. .get_acl = ovl_get_acl,
  429. .update_time = ovl_update_time,
  430. };
  431. static const struct address_space_operations ovl_aops = {
  432. /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
  433. .direct_IO = noop_direct_IO,
  434. };
  435. /*
  436. * It is possible to stack overlayfs instance on top of another
  437. * overlayfs instance as lower layer. We need to annonate the
  438. * stackable i_mutex locks according to stack level of the super
  439. * block instance. An overlayfs instance can never be in stack
  440. * depth 0 (there is always a real fs below it). An overlayfs
  441. * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
  442. *
  443. * For example, here is a snip from /proc/lockdep_chains after
  444. * dir_iterate of nested overlayfs:
  445. *
  446. * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
  447. * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
  448. * [...] &type->i_mutex_dir_key (stack_depth=0)
  449. */
  450. #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
  451. static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
  452. {
  453. #ifdef CONFIG_LOCKDEP
  454. static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
  455. static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
  456. static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
  457. int depth = inode->i_sb->s_stack_depth - 1;
  458. if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
  459. depth = 0;
  460. if (S_ISDIR(inode->i_mode))
  461. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
  462. else
  463. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
  464. lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
  465. #endif
  466. }
  467. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
  468. unsigned long ino, int fsid)
  469. {
  470. int xinobits = ovl_xino_bits(inode->i_sb);
  471. /*
  472. * When NFS export is enabled and d_ino is consistent with st_ino
  473. * (samefs or i_ino has enough bits to encode layer), set the same
  474. * value used for d_ino to i_ino, because nfsd readdirplus compares
  475. * d_ino values to i_ino values of child entries. When called from
  476. * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
  477. * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
  478. */
  479. if (inode->i_sb->s_export_op &&
  480. (ovl_same_sb(inode->i_sb) || xinobits)) {
  481. inode->i_ino = ino;
  482. if (xinobits && fsid && !(ino >> (64 - xinobits)))
  483. inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
  484. } else {
  485. inode->i_ino = get_next_ino();
  486. }
  487. inode->i_mode = mode;
  488. inode->i_flags |= S_NOCMTIME;
  489. #ifdef CONFIG_FS_POSIX_ACL
  490. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  491. #endif
  492. ovl_lockdep_annotate_inode_mutex_key(inode);
  493. switch (mode & S_IFMT) {
  494. case S_IFREG:
  495. inode->i_op = &ovl_file_inode_operations;
  496. inode->i_fop = &ovl_file_operations;
  497. inode->i_mapping->a_ops = &ovl_aops;
  498. break;
  499. case S_IFDIR:
  500. inode->i_op = &ovl_dir_inode_operations;
  501. inode->i_fop = &ovl_dir_operations;
  502. break;
  503. case S_IFLNK:
  504. inode->i_op = &ovl_symlink_inode_operations;
  505. break;
  506. default:
  507. inode->i_op = &ovl_special_inode_operations;
  508. init_special_inode(inode, mode, rdev);
  509. break;
  510. }
  511. }
  512. /*
  513. * With inodes index enabled, an overlay inode nlink counts the union of upper
  514. * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
  515. * upper inode, the following nlink modifying operations can happen:
  516. *
  517. * 1. Lower hardlink copy up
  518. * 2. Upper hardlink created, unlinked or renamed over
  519. * 3. Lower hardlink whiteout or renamed over
  520. *
  521. * For the first, copy up case, the union nlink does not change, whether the
  522. * operation succeeds or fails, but the upper inode nlink may change.
  523. * Therefore, before copy up, we store the union nlink value relative to the
  524. * lower inode nlink in the index inode xattr trusted.overlay.nlink.
  525. *
  526. * For the second, upper hardlink case, the union nlink should be incremented
  527. * or decremented IFF the operation succeeds, aligned with nlink change of the
  528. * upper inode. Therefore, before link/unlink/rename, we store the union nlink
  529. * value relative to the upper inode nlink in the index inode.
  530. *
  531. * For the last, lower cover up case, we simplify things by preceding the
  532. * whiteout or cover up with copy up. This makes sure that there is an index
  533. * upper inode where the nlink xattr can be stored before the copied up upper
  534. * entry is unlink.
  535. */
  536. #define OVL_NLINK_ADD_UPPER (1 << 0)
  537. /*
  538. * On-disk format for indexed nlink:
  539. *
  540. * nlink relative to the upper inode - "U[+-]NUM"
  541. * nlink relative to the lower inode - "L[+-]NUM"
  542. */
  543. static int ovl_set_nlink_common(struct dentry *dentry,
  544. struct dentry *realdentry, const char *format)
  545. {
  546. struct inode *inode = d_inode(dentry);
  547. struct inode *realinode = d_inode(realdentry);
  548. char buf[13];
  549. int len;
  550. len = snprintf(buf, sizeof(buf), format,
  551. (int) (inode->i_nlink - realinode->i_nlink));
  552. if (WARN_ON(len >= sizeof(buf)))
  553. return -EIO;
  554. return ovl_do_setxattr(ovl_dentry_upper(dentry),
  555. OVL_XATTR_NLINK, buf, len, 0);
  556. }
  557. int ovl_set_nlink_upper(struct dentry *dentry)
  558. {
  559. return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
  560. }
  561. int ovl_set_nlink_lower(struct dentry *dentry)
  562. {
  563. return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
  564. }
  565. unsigned int ovl_get_nlink(struct dentry *lowerdentry,
  566. struct dentry *upperdentry,
  567. unsigned int fallback)
  568. {
  569. int nlink_diff;
  570. int nlink;
  571. char buf[13];
  572. int err;
  573. if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
  574. return fallback;
  575. err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
  576. if (err < 0)
  577. goto fail;
  578. buf[err] = '\0';
  579. if ((buf[0] != 'L' && buf[0] != 'U') ||
  580. (buf[1] != '+' && buf[1] != '-'))
  581. goto fail;
  582. err = kstrtoint(buf + 1, 10, &nlink_diff);
  583. if (err < 0)
  584. goto fail;
  585. nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
  586. nlink += nlink_diff;
  587. if (nlink <= 0)
  588. goto fail;
  589. return nlink;
  590. fail:
  591. pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
  592. upperdentry, err);
  593. return fallback;
  594. }
  595. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  596. {
  597. struct inode *inode;
  598. inode = new_inode(sb);
  599. if (inode)
  600. ovl_fill_inode(inode, mode, rdev, 0, 0);
  601. return inode;
  602. }
  603. static int ovl_inode_test(struct inode *inode, void *data)
  604. {
  605. return inode->i_private == data;
  606. }
  607. static int ovl_inode_set(struct inode *inode, void *data)
  608. {
  609. inode->i_private = data;
  610. return 0;
  611. }
  612. static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
  613. struct dentry *upperdentry, bool strict)
  614. {
  615. /*
  616. * For directories, @strict verify from lookup path performs consistency
  617. * checks, so NULL lower/upper in dentry must match NULL lower/upper in
  618. * inode. Non @strict verify from NFS handle decode path passes NULL for
  619. * 'unknown' lower/upper.
  620. */
  621. if (S_ISDIR(inode->i_mode) && strict) {
  622. /* Real lower dir moved to upper layer under us? */
  623. if (!lowerdentry && ovl_inode_lower(inode))
  624. return false;
  625. /* Lookup of an uncovered redirect origin? */
  626. if (!upperdentry && ovl_inode_upper(inode))
  627. return false;
  628. }
  629. /*
  630. * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
  631. * This happens when finding a copied up overlay inode for a renamed
  632. * or hardlinked overlay dentry and lower dentry cannot be followed
  633. * by origin because lower fs does not support file handles.
  634. */
  635. if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
  636. return false;
  637. /*
  638. * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
  639. * This happens when finding a lower alias for a copied up hard link.
  640. */
  641. if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
  642. return false;
  643. return true;
  644. }
  645. struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
  646. bool is_upper)
  647. {
  648. struct inode *inode, *key = d_inode(real);
  649. inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
  650. if (!inode)
  651. return NULL;
  652. if (!ovl_verify_inode(inode, is_upper ? NULL : real,
  653. is_upper ? real : NULL, false)) {
  654. iput(inode);
  655. return ERR_PTR(-ESTALE);
  656. }
  657. return inode;
  658. }
  659. /*
  660. * Does overlay inode need to be hashed by lower inode?
  661. */
  662. static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
  663. struct dentry *lower, struct dentry *index)
  664. {
  665. struct ovl_fs *ofs = sb->s_fs_info;
  666. /* No, if pure upper */
  667. if (!lower)
  668. return false;
  669. /* Yes, if already indexed */
  670. if (index)
  671. return true;
  672. /* Yes, if won't be copied up */
  673. if (!ofs->upper_mnt)
  674. return true;
  675. /* No, if lower hardlink is or will be broken on copy up */
  676. if ((upper || !ovl_indexdir(sb)) &&
  677. !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  678. return false;
  679. /* No, if non-indexed upper with NFS export */
  680. if (sb->s_export_op && upper)
  681. return false;
  682. /* Otherwise, hash by lower inode for fsnotify */
  683. return true;
  684. }
  685. static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
  686. struct inode *key)
  687. {
  688. return newinode ? inode_insert5(newinode, (unsigned long) key,
  689. ovl_inode_test, ovl_inode_set, key) :
  690. iget5_locked(sb, (unsigned long) key,
  691. ovl_inode_test, ovl_inode_set, key);
  692. }
  693. struct inode *ovl_get_inode(struct super_block *sb,
  694. struct ovl_inode_params *oip)
  695. {
  696. struct dentry *upperdentry = oip->upperdentry;
  697. struct ovl_path *lowerpath = oip->lowerpath;
  698. struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
  699. struct inode *inode;
  700. struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
  701. bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
  702. oip->index);
  703. int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
  704. bool is_dir, metacopy = false;
  705. unsigned long ino = 0;
  706. int err = -ENOMEM;
  707. if (!realinode)
  708. realinode = d_inode(lowerdentry);
  709. /*
  710. * Copy up origin (lower) may exist for non-indexed upper, but we must
  711. * not use lower as hash key if this is a broken hardlink.
  712. */
  713. is_dir = S_ISDIR(realinode->i_mode);
  714. if (upperdentry || bylower) {
  715. struct inode *key = d_inode(bylower ? lowerdentry :
  716. upperdentry);
  717. unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
  718. inode = ovl_iget5(sb, oip->newinode, key);
  719. if (!inode)
  720. goto out_err;
  721. if (!(inode->i_state & I_NEW)) {
  722. /*
  723. * Verify that the underlying files stored in the inode
  724. * match those in the dentry.
  725. */
  726. if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
  727. true)) {
  728. iput(inode);
  729. err = -ESTALE;
  730. goto out_err;
  731. }
  732. dput(upperdentry);
  733. kfree(oip->redirect);
  734. goto out;
  735. }
  736. /* Recalculate nlink for non-dir due to indexing */
  737. if (!is_dir)
  738. nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
  739. set_nlink(inode, nlink);
  740. ino = key->i_ino;
  741. } else {
  742. /* Lower hardlink that will be broken on copy up */
  743. inode = new_inode(sb);
  744. if (!inode) {
  745. err = -ENOMEM;
  746. goto out_err;
  747. }
  748. }
  749. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
  750. ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
  751. if (upperdentry && ovl_is_impuredir(upperdentry))
  752. ovl_set_flag(OVL_IMPURE, inode);
  753. if (oip->index)
  754. ovl_set_flag(OVL_INDEX, inode);
  755. if (upperdentry) {
  756. err = ovl_check_metacopy_xattr(upperdentry);
  757. if (err < 0)
  758. goto out_err;
  759. metacopy = err;
  760. if (!metacopy)
  761. ovl_set_flag(OVL_UPPERDATA, inode);
  762. }
  763. OVL_I(inode)->redirect = oip->redirect;
  764. if (bylower)
  765. ovl_set_flag(OVL_CONST_INO, inode);
  766. /* Check for non-merge dir that may have whiteouts */
  767. if (is_dir) {
  768. if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
  769. ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
  770. ovl_set_flag(OVL_WHITEOUTS, inode);
  771. }
  772. }
  773. if (inode->i_state & I_NEW)
  774. unlock_new_inode(inode);
  775. out:
  776. return inode;
  777. out_err:
  778. inode = ERR_PTR(err);
  779. goto out;
  780. }