inode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. err = vfs_getxattr(realdentry, name, NULL, 0);
  299. if (err < 0)
  300. goto out_drop_write;
  301. }
  302. if (!upperdentry) {
  303. err = ovl_copy_up(dentry);
  304. if (err)
  305. goto out_drop_write;
  306. realdentry = ovl_dentry_upper(dentry);
  307. }
  308. old_cred = ovl_override_creds(dentry->d_sb);
  309. if (value)
  310. err = vfs_setxattr(realdentry, name, value, size, flags);
  311. else {
  312. WARN_ON(flags != XATTR_REPLACE);
  313. err = vfs_removexattr(realdentry, name);
  314. }
  315. revert_creds(old_cred);
  316. /* copy c/mtime */
  317. ovl_copyattr(d_inode(realdentry), inode);
  318. out_drop_write:
  319. ovl_drop_write(dentry);
  320. out:
  321. return err;
  322. }
  323. int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
  324. void *value, size_t size)
  325. {
  326. ssize_t res;
  327. const struct cred *old_cred;
  328. struct dentry *realdentry =
  329. ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
  330. old_cred = ovl_override_creds(dentry->d_sb);
  331. res = vfs_getxattr(realdentry, name, value, size);
  332. revert_creds(old_cred);
  333. return res;
  334. }
  335. static bool ovl_can_list(const char *s)
  336. {
  337. /* List all non-trusted xatts */
  338. if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
  339. return true;
  340. /* Never list trusted.overlay, list other trusted for superuser only */
  341. return !ovl_is_private_xattr(s) &&
  342. ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
  343. }
  344. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  345. {
  346. struct dentry *realdentry = ovl_dentry_real(dentry);
  347. ssize_t res;
  348. size_t len;
  349. char *s;
  350. const struct cred *old_cred;
  351. old_cred = ovl_override_creds(dentry->d_sb);
  352. res = vfs_listxattr(realdentry, list, size);
  353. revert_creds(old_cred);
  354. if (res <= 0 || size == 0)
  355. return res;
  356. /* filter out private xattrs */
  357. for (s = list, len = res; len;) {
  358. size_t slen = strnlen(s, len) + 1;
  359. /* underlying fs providing us with an broken xattr list? */
  360. if (WARN_ON(slen > len))
  361. return -EIO;
  362. len -= slen;
  363. if (!ovl_can_list(s)) {
  364. res -= slen;
  365. memmove(s, s + slen, len);
  366. } else {
  367. s += slen;
  368. }
  369. }
  370. return res;
  371. }
  372. struct posix_acl *ovl_get_acl(struct inode *inode, int type)
  373. {
  374. struct inode *realinode = ovl_inode_real(inode);
  375. const struct cred *old_cred;
  376. struct posix_acl *acl;
  377. if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
  378. return NULL;
  379. old_cred = ovl_override_creds(inode->i_sb);
  380. acl = get_acl(realinode, type);
  381. revert_creds(old_cred);
  382. return acl;
  383. }
  384. int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
  385. {
  386. if (flags & S_ATIME) {
  387. struct ovl_fs *ofs = inode->i_sb->s_fs_info;
  388. struct path upperpath = {
  389. .mnt = ofs->upper_mnt,
  390. .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
  391. };
  392. if (upperpath.dentry) {
  393. touch_atime(&upperpath);
  394. inode->i_atime = d_inode(upperpath.dentry)->i_atime;
  395. }
  396. }
  397. return 0;
  398. }
  399. static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  400. u64 start, u64 len)
  401. {
  402. int err;
  403. struct inode *realinode = ovl_inode_real(inode);
  404. const struct cred *old_cred;
  405. if (!realinode->i_op->fiemap)
  406. return -EOPNOTSUPP;
  407. old_cred = ovl_override_creds(inode->i_sb);
  408. if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
  409. filemap_write_and_wait(realinode->i_mapping);
  410. err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
  411. revert_creds(old_cred);
  412. return err;
  413. }
  414. static const struct inode_operations ovl_file_inode_operations = {
  415. .setattr = ovl_setattr,
  416. .permission = ovl_permission,
  417. .getattr = ovl_getattr,
  418. .listxattr = ovl_listxattr,
  419. .get_acl = ovl_get_acl,
  420. .update_time = ovl_update_time,
  421. .fiemap = ovl_fiemap,
  422. };
  423. static const struct inode_operations ovl_symlink_inode_operations = {
  424. .setattr = ovl_setattr,
  425. .get_link = ovl_get_link,
  426. .getattr = ovl_getattr,
  427. .listxattr = ovl_listxattr,
  428. .update_time = ovl_update_time,
  429. };
  430. static const struct inode_operations ovl_special_inode_operations = {
  431. .setattr = ovl_setattr,
  432. .permission = ovl_permission,
  433. .getattr = ovl_getattr,
  434. .listxattr = ovl_listxattr,
  435. .get_acl = ovl_get_acl,
  436. .update_time = ovl_update_time,
  437. };
  438. static const struct address_space_operations ovl_aops = {
  439. /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
  440. .direct_IO = noop_direct_IO,
  441. };
  442. /*
  443. * It is possible to stack overlayfs instance on top of another
  444. * overlayfs instance as lower layer. We need to annonate the
  445. * stackable i_mutex locks according to stack level of the super
  446. * block instance. An overlayfs instance can never be in stack
  447. * depth 0 (there is always a real fs below it). An overlayfs
  448. * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
  449. *
  450. * For example, here is a snip from /proc/lockdep_chains after
  451. * dir_iterate of nested overlayfs:
  452. *
  453. * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
  454. * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
  455. * [...] &type->i_mutex_dir_key (stack_depth=0)
  456. */
  457. #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
  458. static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
  459. {
  460. #ifdef CONFIG_LOCKDEP
  461. static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
  462. static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
  463. static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
  464. int depth = inode->i_sb->s_stack_depth - 1;
  465. if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
  466. depth = 0;
  467. if (S_ISDIR(inode->i_mode))
  468. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
  469. else
  470. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
  471. lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
  472. #endif
  473. }
  474. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
  475. unsigned long ino, int fsid)
  476. {
  477. int xinobits = ovl_xino_bits(inode->i_sb);
  478. /*
  479. * When d_ino is consistent with st_ino (samefs or i_ino has enough
  480. * bits to encode layer), set the same value used for st_ino to i_ino,
  481. * so inode number exposed via /proc/locks and a like will be
  482. * consistent with d_ino and st_ino values. An i_ino value inconsistent
  483. * with d_ino also causes nfsd readdirplus to fail. When called from
  484. * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
  485. * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
  486. */
  487. if (ovl_same_sb(inode->i_sb) || xinobits) {
  488. inode->i_ino = ino;
  489. if (xinobits && fsid && !(ino >> (64 - xinobits)))
  490. inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
  491. } else {
  492. inode->i_ino = get_next_ino();
  493. }
  494. inode->i_mode = mode;
  495. inode->i_flags |= S_NOCMTIME;
  496. #ifdef CONFIG_FS_POSIX_ACL
  497. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  498. #endif
  499. ovl_lockdep_annotate_inode_mutex_key(inode);
  500. switch (mode & S_IFMT) {
  501. case S_IFREG:
  502. inode->i_op = &ovl_file_inode_operations;
  503. inode->i_fop = &ovl_file_operations;
  504. inode->i_mapping->a_ops = &ovl_aops;
  505. break;
  506. case S_IFDIR:
  507. inode->i_op = &ovl_dir_inode_operations;
  508. inode->i_fop = &ovl_dir_operations;
  509. break;
  510. case S_IFLNK:
  511. inode->i_op = &ovl_symlink_inode_operations;
  512. break;
  513. default:
  514. inode->i_op = &ovl_special_inode_operations;
  515. init_special_inode(inode, mode, rdev);
  516. break;
  517. }
  518. }
  519. /*
  520. * With inodes index enabled, an overlay inode nlink counts the union of upper
  521. * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
  522. * upper inode, the following nlink modifying operations can happen:
  523. *
  524. * 1. Lower hardlink copy up
  525. * 2. Upper hardlink created, unlinked or renamed over
  526. * 3. Lower hardlink whiteout or renamed over
  527. *
  528. * For the first, copy up case, the union nlink does not change, whether the
  529. * operation succeeds or fails, but the upper inode nlink may change.
  530. * Therefore, before copy up, we store the union nlink value relative to the
  531. * lower inode nlink in the index inode xattr trusted.overlay.nlink.
  532. *
  533. * For the second, upper hardlink case, the union nlink should be incremented
  534. * or decremented IFF the operation succeeds, aligned with nlink change of the
  535. * upper inode. Therefore, before link/unlink/rename, we store the union nlink
  536. * value relative to the upper inode nlink in the index inode.
  537. *
  538. * For the last, lower cover up case, we simplify things by preceding the
  539. * whiteout or cover up with copy up. This makes sure that there is an index
  540. * upper inode where the nlink xattr can be stored before the copied up upper
  541. * entry is unlink.
  542. */
  543. #define OVL_NLINK_ADD_UPPER (1 << 0)
  544. /*
  545. * On-disk format for indexed nlink:
  546. *
  547. * nlink relative to the upper inode - "U[+-]NUM"
  548. * nlink relative to the lower inode - "L[+-]NUM"
  549. */
  550. static int ovl_set_nlink_common(struct dentry *dentry,
  551. struct dentry *realdentry, const char *format)
  552. {
  553. struct inode *inode = d_inode(dentry);
  554. struct inode *realinode = d_inode(realdentry);
  555. char buf[13];
  556. int len;
  557. len = snprintf(buf, sizeof(buf), format,
  558. (int) (inode->i_nlink - realinode->i_nlink));
  559. if (WARN_ON(len >= sizeof(buf)))
  560. return -EIO;
  561. return ovl_do_setxattr(ovl_dentry_upper(dentry),
  562. OVL_XATTR_NLINK, buf, len, 0);
  563. }
  564. int ovl_set_nlink_upper(struct dentry *dentry)
  565. {
  566. return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
  567. }
  568. int ovl_set_nlink_lower(struct dentry *dentry)
  569. {
  570. return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
  571. }
  572. unsigned int ovl_get_nlink(struct dentry *lowerdentry,
  573. struct dentry *upperdentry,
  574. unsigned int fallback)
  575. {
  576. int nlink_diff;
  577. int nlink;
  578. char buf[13];
  579. int err;
  580. if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
  581. return fallback;
  582. err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
  583. if (err < 0)
  584. goto fail;
  585. buf[err] = '\0';
  586. if ((buf[0] != 'L' && buf[0] != 'U') ||
  587. (buf[1] != '+' && buf[1] != '-'))
  588. goto fail;
  589. err = kstrtoint(buf + 1, 10, &nlink_diff);
  590. if (err < 0)
  591. goto fail;
  592. nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
  593. nlink += nlink_diff;
  594. if (nlink <= 0)
  595. goto fail;
  596. return nlink;
  597. fail:
  598. pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
  599. upperdentry, err);
  600. return fallback;
  601. }
  602. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  603. {
  604. struct inode *inode;
  605. inode = new_inode(sb);
  606. if (inode)
  607. ovl_fill_inode(inode, mode, rdev, 0, 0);
  608. return inode;
  609. }
  610. static int ovl_inode_test(struct inode *inode, void *data)
  611. {
  612. return inode->i_private == data;
  613. }
  614. static int ovl_inode_set(struct inode *inode, void *data)
  615. {
  616. inode->i_private = data;
  617. return 0;
  618. }
  619. static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
  620. struct dentry *upperdentry, bool strict)
  621. {
  622. /*
  623. * For directories, @strict verify from lookup path performs consistency
  624. * checks, so NULL lower/upper in dentry must match NULL lower/upper in
  625. * inode. Non @strict verify from NFS handle decode path passes NULL for
  626. * 'unknown' lower/upper.
  627. */
  628. if (S_ISDIR(inode->i_mode) && strict) {
  629. /* Real lower dir moved to upper layer under us? */
  630. if (!lowerdentry && ovl_inode_lower(inode))
  631. return false;
  632. /* Lookup of an uncovered redirect origin? */
  633. if (!upperdentry && ovl_inode_upper(inode))
  634. return false;
  635. }
  636. /*
  637. * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
  638. * This happens when finding a copied up overlay inode for a renamed
  639. * or hardlinked overlay dentry and lower dentry cannot be followed
  640. * by origin because lower fs does not support file handles.
  641. */
  642. if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
  643. return false;
  644. /*
  645. * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
  646. * This happens when finding a lower alias for a copied up hard link.
  647. */
  648. if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
  649. return false;
  650. return true;
  651. }
  652. struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
  653. bool is_upper)
  654. {
  655. struct inode *inode, *key = d_inode(real);
  656. inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
  657. if (!inode)
  658. return NULL;
  659. if (!ovl_verify_inode(inode, is_upper ? NULL : real,
  660. is_upper ? real : NULL, false)) {
  661. iput(inode);
  662. return ERR_PTR(-ESTALE);
  663. }
  664. return inode;
  665. }
  666. bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
  667. {
  668. struct inode *key = d_inode(dir);
  669. struct inode *trap;
  670. bool res;
  671. trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
  672. if (!trap)
  673. return false;
  674. res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
  675. !ovl_inode_lower(trap);
  676. iput(trap);
  677. return res;
  678. }
  679. /*
  680. * Create an inode cache entry for layer root dir, that will intentionally
  681. * fail ovl_verify_inode(), so any lookup that will find some layer root
  682. * will fail.
  683. */
  684. struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
  685. {
  686. struct inode *key = d_inode(dir);
  687. struct inode *trap;
  688. if (!d_is_dir(dir))
  689. return ERR_PTR(-ENOTDIR);
  690. trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
  691. ovl_inode_set, key);
  692. if (!trap)
  693. return ERR_PTR(-ENOMEM);
  694. if (!(trap->i_state & I_NEW)) {
  695. /* Conflicting layer roots? */
  696. iput(trap);
  697. return ERR_PTR(-ELOOP);
  698. }
  699. trap->i_mode = S_IFDIR;
  700. trap->i_flags = S_DEAD;
  701. unlock_new_inode(trap);
  702. return trap;
  703. }
  704. /*
  705. * Does overlay inode need to be hashed by lower inode?
  706. */
  707. static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
  708. struct dentry *lower, struct dentry *index)
  709. {
  710. struct ovl_fs *ofs = sb->s_fs_info;
  711. /* No, if pure upper */
  712. if (!lower)
  713. return false;
  714. /* Yes, if already indexed */
  715. if (index)
  716. return true;
  717. /* Yes, if won't be copied up */
  718. if (!ofs->upper_mnt)
  719. return true;
  720. /* No, if lower hardlink is or will be broken on copy up */
  721. if ((upper || !ovl_indexdir(sb)) &&
  722. !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  723. return false;
  724. /* No, if non-indexed upper with NFS export */
  725. if (sb->s_export_op && upper)
  726. return false;
  727. /* Otherwise, hash by lower inode for fsnotify */
  728. return true;
  729. }
  730. static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
  731. struct inode *key)
  732. {
  733. return newinode ? inode_insert5(newinode, (unsigned long) key,
  734. ovl_inode_test, ovl_inode_set, key) :
  735. iget5_locked(sb, (unsigned long) key,
  736. ovl_inode_test, ovl_inode_set, key);
  737. }
  738. struct inode *ovl_get_inode(struct super_block *sb,
  739. struct ovl_inode_params *oip)
  740. {
  741. struct dentry *upperdentry = oip->upperdentry;
  742. struct ovl_path *lowerpath = oip->lowerpath;
  743. struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
  744. struct inode *inode;
  745. struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
  746. bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
  747. oip->index);
  748. int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
  749. bool is_dir, metacopy = false;
  750. unsigned long ino = 0;
  751. int err = oip->newinode ? -EEXIST : -ENOMEM;
  752. if (!realinode)
  753. realinode = d_inode(lowerdentry);
  754. /*
  755. * Copy up origin (lower) may exist for non-indexed upper, but we must
  756. * not use lower as hash key if this is a broken hardlink.
  757. */
  758. is_dir = S_ISDIR(realinode->i_mode);
  759. if (upperdentry || bylower) {
  760. struct inode *key = d_inode(bylower ? lowerdentry :
  761. upperdentry);
  762. unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
  763. inode = ovl_iget5(sb, oip->newinode, key);
  764. if (!inode)
  765. goto out_err;
  766. if (!(inode->i_state & I_NEW)) {
  767. /*
  768. * Verify that the underlying files stored in the inode
  769. * match those in the dentry.
  770. */
  771. if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
  772. true)) {
  773. iput(inode);
  774. err = -ESTALE;
  775. goto out_err;
  776. }
  777. dput(upperdentry);
  778. kfree(oip->redirect);
  779. goto out;
  780. }
  781. /* Recalculate nlink for non-dir due to indexing */
  782. if (!is_dir)
  783. nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
  784. set_nlink(inode, nlink);
  785. ino = key->i_ino;
  786. } else {
  787. /* Lower hardlink that will be broken on copy up */
  788. inode = new_inode(sb);
  789. if (!inode) {
  790. err = -ENOMEM;
  791. goto out_err;
  792. }
  793. }
  794. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
  795. ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
  796. if (upperdentry && ovl_is_impuredir(upperdentry))
  797. ovl_set_flag(OVL_IMPURE, inode);
  798. if (oip->index)
  799. ovl_set_flag(OVL_INDEX, inode);
  800. if (upperdentry) {
  801. err = ovl_check_metacopy_xattr(upperdentry);
  802. if (err < 0)
  803. goto out_err;
  804. metacopy = err;
  805. if (!metacopy)
  806. ovl_set_flag(OVL_UPPERDATA, inode);
  807. }
  808. OVL_I(inode)->redirect = oip->redirect;
  809. if (bylower)
  810. ovl_set_flag(OVL_CONST_INO, inode);
  811. /* Check for non-merge dir that may have whiteouts */
  812. if (is_dir) {
  813. if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
  814. ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
  815. ovl_set_flag(OVL_WHITEOUTS, inode);
  816. }
  817. }
  818. if (inode->i_state & I_NEW)
  819. unlock_new_inode(inode);
  820. out:
  821. return inode;
  822. out_err:
  823. pr_warn_ratelimited("overlayfs: failed to get inode (%i)\n", err);
  824. inode = ERR_PTR(err);
  825. goto out;
  826. }