inode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2011 Novell Inc.
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/slab.h>
  8. #include <linux/cred.h>
  9. #include <linux/xattr.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/fiemap.h>
  12. #include <linux/fileattr.h>
  13. #include <linux/security.h>
  14. #include <linux/namei.h>
  15. #include <linux/posix_acl.h>
  16. #include <linux/posix_acl_xattr.h>
  17. #include "overlayfs.h"
  18. int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  19. struct iattr *attr)
  20. {
  21. int err;
  22. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  23. bool full_copy_up = false;
  24. struct dentry *upperdentry;
  25. const struct cred *old_cred;
  26. err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
  27. if (err)
  28. return err;
  29. if (attr->ia_valid & ATTR_SIZE) {
  30. /* Truncate should trigger data copy up as well */
  31. full_copy_up = true;
  32. }
  33. if (!full_copy_up)
  34. err = ovl_copy_up(dentry);
  35. else
  36. err = ovl_copy_up_with_data(dentry);
  37. if (!err) {
  38. struct inode *winode = NULL;
  39. upperdentry = ovl_dentry_upper(dentry);
  40. if (attr->ia_valid & ATTR_SIZE) {
  41. winode = d_inode(upperdentry);
  42. err = get_write_access(winode);
  43. if (err)
  44. goto out;
  45. }
  46. if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  47. attr->ia_valid &= ~ATTR_MODE;
  48. /*
  49. * We might have to translate ovl file into real file object
  50. * once use cases emerge. For now, simply don't let underlying
  51. * filesystem rely on attr->ia_file
  52. */
  53. attr->ia_valid &= ~ATTR_FILE;
  54. /*
  55. * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
  56. * set. Overlayfs does not pass O_TRUNC flag to underlying
  57. * filesystem during open -> do not pass ATTR_OPEN. This
  58. * disables optimization in fuse which assumes open(O_TRUNC)
  59. * already set file size to 0. But we never passed O_TRUNC to
  60. * fuse. So by clearing ATTR_OPEN, fuse will be forced to send
  61. * setattr request to server.
  62. */
  63. attr->ia_valid &= ~ATTR_OPEN;
  64. err = ovl_want_write(dentry);
  65. if (err)
  66. goto out_put_write;
  67. inode_lock(upperdentry->d_inode);
  68. old_cred = ovl_override_creds(dentry->d_sb);
  69. err = ovl_do_notify_change(ofs, upperdentry, attr);
  70. revert_creds(old_cred);
  71. if (!err)
  72. ovl_copyattr(dentry->d_inode);
  73. inode_unlock(upperdentry->d_inode);
  74. ovl_drop_write(dentry);
  75. out_put_write:
  76. if (winode)
  77. put_write_access(winode);
  78. }
  79. out:
  80. return err;
  81. }
  82. static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
  83. {
  84. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  85. bool samefs = ovl_same_fs(ofs);
  86. unsigned int xinobits = ovl_xino_bits(ofs);
  87. unsigned int xinoshift = 64 - xinobits;
  88. if (samefs) {
  89. /*
  90. * When all layers are on the same fs, all real inode
  91. * number are unique, so we use the overlay st_dev,
  92. * which is friendly to du -x.
  93. */
  94. stat->dev = dentry->d_sb->s_dev;
  95. return;
  96. } else if (xinobits) {
  97. /*
  98. * All inode numbers of underlying fs should not be using the
  99. * high xinobits, so we use high xinobits to partition the
  100. * overlay st_ino address space. The high bits holds the fsid
  101. * (upper fsid is 0). The lowest xinobit is reserved for mapping
  102. * the non-persistent inode numbers range in case of overflow.
  103. * This way all overlay inode numbers are unique and use the
  104. * overlay st_dev.
  105. */
  106. if (likely(!(stat->ino >> xinoshift))) {
  107. stat->ino |= ((u64)fsid) << (xinoshift + 1);
  108. stat->dev = dentry->d_sb->s_dev;
  109. return;
  110. } else if (ovl_xino_warn(ofs)) {
  111. pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
  112. dentry, stat->ino, xinobits);
  113. }
  114. }
  115. /* The inode could not be mapped to a unified st_ino address space */
  116. if (S_ISDIR(dentry->d_inode->i_mode)) {
  117. /*
  118. * Always use the overlay st_dev for directories, so 'find
  119. * -xdev' will scan the entire overlay mount and won't cross the
  120. * overlay mount boundaries.
  121. *
  122. * If not all layers are on the same fs the pair {real st_ino;
  123. * overlay st_dev} is not unique, so use the non persistent
  124. * overlay st_ino for directories.
  125. */
  126. stat->dev = dentry->d_sb->s_dev;
  127. stat->ino = dentry->d_inode->i_ino;
  128. } else {
  129. /*
  130. * For non-samefs setup, if we cannot map all layers st_ino
  131. * to a unified address space, we need to make sure that st_dev
  132. * is unique per underlying fs, so we use the unique anonymous
  133. * bdev assigned to the underlying fs.
  134. */
  135. stat->dev = ofs->fs[fsid].pseudo_dev;
  136. }
  137. }
  138. int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
  139. struct kstat *stat, u32 request_mask, unsigned int flags)
  140. {
  141. struct dentry *dentry = path->dentry;
  142. enum ovl_path_type type;
  143. struct path realpath;
  144. const struct cred *old_cred;
  145. struct inode *inode = d_inode(dentry);
  146. bool is_dir = S_ISDIR(inode->i_mode);
  147. int fsid = 0;
  148. int err;
  149. bool metacopy_blocks = false;
  150. metacopy_blocks = ovl_is_metacopy_dentry(dentry);
  151. type = ovl_path_real(dentry, &realpath);
  152. old_cred = ovl_override_creds(dentry->d_sb);
  153. err = ovl_do_getattr(&realpath, stat, request_mask, flags);
  154. if (err)
  155. goto out;
  156. /* Report the effective immutable/append-only STATX flags */
  157. generic_fill_statx_attr(inode, stat);
  158. /*
  159. * For non-dir or same fs, we use st_ino of the copy up origin.
  160. * This guaranties constant st_dev/st_ino across copy up.
  161. * With xino feature and non-samefs, we use st_ino of the copy up
  162. * origin masked with high bits that represent the layer id.
  163. *
  164. * If lower filesystem supports NFS file handles, this also guaranties
  165. * persistent st_ino across mount cycle.
  166. */
  167. if (!is_dir || ovl_same_dev(OVL_FS(dentry->d_sb))) {
  168. if (!OVL_TYPE_UPPER(type)) {
  169. fsid = ovl_layer_lower(dentry)->fsid;
  170. } else if (OVL_TYPE_ORIGIN(type)) {
  171. struct kstat lowerstat;
  172. u32 lowermask = STATX_INO | STATX_BLOCKS |
  173. (!is_dir ? STATX_NLINK : 0);
  174. ovl_path_lower(dentry, &realpath);
  175. err = ovl_do_getattr(&realpath, &lowerstat, lowermask,
  176. flags);
  177. if (err)
  178. goto out;
  179. /*
  180. * Lower hardlinks may be broken on copy up to different
  181. * upper files, so we cannot use the lower origin st_ino
  182. * for those different files, even for the same fs case.
  183. *
  184. * Similarly, several redirected dirs can point to the
  185. * same dir on a lower layer. With the "verify_lower"
  186. * feature, we do not use the lower origin st_ino, if
  187. * we haven't verified that this redirect is unique.
  188. *
  189. * With inodes index enabled, it is safe to use st_ino
  190. * of an indexed origin. The index validates that the
  191. * upper hardlink is not broken and that a redirected
  192. * dir is the only redirect to that origin.
  193. */
  194. if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
  195. (!ovl_verify_lower(dentry->d_sb) &&
  196. (is_dir || lowerstat.nlink == 1))) {
  197. fsid = ovl_layer_lower(dentry)->fsid;
  198. stat->ino = lowerstat.ino;
  199. }
  200. /*
  201. * If we are querying a metacopy dentry and lower
  202. * dentry is data dentry, then use the blocks we
  203. * queried just now. We don't have to do additional
  204. * vfs_getattr(). If lower itself is metacopy, then
  205. * additional vfs_getattr() is unavoidable.
  206. */
  207. if (metacopy_blocks &&
  208. realpath.dentry == ovl_dentry_lowerdata(dentry)) {
  209. stat->blocks = lowerstat.blocks;
  210. metacopy_blocks = false;
  211. }
  212. }
  213. if (metacopy_blocks) {
  214. /*
  215. * If lower is not same as lowerdata or if there was
  216. * no origin on upper, we can end up here.
  217. * With lazy lowerdata lookup, guess lowerdata blocks
  218. * from size to avoid lowerdata lookup on stat(2).
  219. */
  220. struct kstat lowerdatastat;
  221. u32 lowermask = STATX_BLOCKS;
  222. ovl_path_lowerdata(dentry, &realpath);
  223. if (realpath.dentry) {
  224. err = ovl_do_getattr(&realpath, &lowerdatastat,
  225. lowermask, flags);
  226. if (err)
  227. goto out;
  228. } else {
  229. lowerdatastat.blocks =
  230. round_up(stat->size, stat->blksize) >> 9;
  231. }
  232. stat->blocks = lowerdatastat.blocks;
  233. }
  234. }
  235. ovl_map_dev_ino(dentry, stat, fsid);
  236. /*
  237. * It's probably not worth it to count subdirs to get the
  238. * correct link count. nlink=1 seems to pacify 'find' and
  239. * other utilities.
  240. */
  241. if (is_dir && OVL_TYPE_MERGE(type))
  242. stat->nlink = 1;
  243. /*
  244. * Return the overlay inode nlinks for indexed upper inodes.
  245. * Overlay inode nlink counts the union of the upper hardlinks
  246. * and non-covered lower hardlinks. It does not include the upper
  247. * index hardlink.
  248. */
  249. if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  250. stat->nlink = dentry->d_inode->i_nlink;
  251. out:
  252. revert_creds(old_cred);
  253. return err;
  254. }
  255. int ovl_permission(struct mnt_idmap *idmap,
  256. struct inode *inode, int mask)
  257. {
  258. struct inode *upperinode = ovl_inode_upper(inode);
  259. struct inode *realinode;
  260. struct path realpath;
  261. const struct cred *old_cred;
  262. int err;
  263. /* Careful in RCU walk mode */
  264. realinode = ovl_i_path_real(inode, &realpath);
  265. if (!realinode) {
  266. WARN_ON(!(mask & MAY_NOT_BLOCK));
  267. return -ECHILD;
  268. }
  269. /*
  270. * Check overlay inode with the creds of task and underlying inode
  271. * with creds of mounter
  272. */
  273. err = generic_permission(&nop_mnt_idmap, inode, mask);
  274. if (err)
  275. return err;
  276. old_cred = ovl_override_creds(inode->i_sb);
  277. if (!upperinode &&
  278. !special_file(realinode->i_mode) && mask & MAY_WRITE) {
  279. mask &= ~(MAY_WRITE | MAY_APPEND);
  280. /* Make sure mounter can read file for copy up later */
  281. mask |= MAY_READ;
  282. }
  283. err = inode_permission(mnt_idmap(realpath.mnt), realinode, mask);
  284. revert_creds(old_cred);
  285. return err;
  286. }
  287. static const char *ovl_get_link(struct dentry *dentry,
  288. struct inode *inode,
  289. struct delayed_call *done)
  290. {
  291. const struct cred *old_cred;
  292. const char *p;
  293. if (!dentry)
  294. return ERR_PTR(-ECHILD);
  295. old_cred = ovl_override_creds(dentry->d_sb);
  296. p = vfs_get_link(ovl_dentry_real(dentry), done);
  297. revert_creds(old_cred);
  298. return p;
  299. }
  300. #ifdef CONFIG_FS_POSIX_ACL
  301. /*
  302. * Apply the idmapping of the layer to POSIX ACLs. The caller must pass a clone
  303. * of the POSIX ACLs retrieved from the lower layer to this function to not
  304. * alter the POSIX ACLs for the underlying filesystem.
  305. */
  306. static void ovl_idmap_posix_acl(const struct inode *realinode,
  307. struct mnt_idmap *idmap,
  308. struct posix_acl *acl)
  309. {
  310. struct user_namespace *fs_userns = i_user_ns(realinode);
  311. for (unsigned int i = 0; i < acl->a_count; i++) {
  312. vfsuid_t vfsuid;
  313. vfsgid_t vfsgid;
  314. struct posix_acl_entry *e = &acl->a_entries[i];
  315. switch (e->e_tag) {
  316. case ACL_USER:
  317. vfsuid = make_vfsuid(idmap, fs_userns, e->e_uid);
  318. e->e_uid = vfsuid_into_kuid(vfsuid);
  319. break;
  320. case ACL_GROUP:
  321. vfsgid = make_vfsgid(idmap, fs_userns, e->e_gid);
  322. e->e_gid = vfsgid_into_kgid(vfsgid);
  323. break;
  324. }
  325. }
  326. }
  327. /*
  328. * The @noperm argument is used to skip permission checking and is a temporary
  329. * measure. Quoting Miklos from an earlier discussion:
  330. *
  331. * > So there are two paths to getting an acl:
  332. * > 1) permission checking and 2) retrieving the value via getxattr(2).
  333. * > This is a similar situation as reading a symlink vs. following it.
  334. * > When following a symlink overlayfs always reads the link on the
  335. * > underlying fs just as if it was a readlink(2) call, calling
  336. * > security_inode_readlink() instead of security_inode_follow_link().
  337. * > This is logical: we are reading the link from the underlying storage,
  338. * > and following it on overlayfs.
  339. * >
  340. * > Applying the same logic to acl: we do need to call the
  341. * > security_inode_getxattr() on the underlying fs, even if just want to
  342. * > check permissions on overlay. This is currently not done, which is an
  343. * > inconsistency.
  344. * >
  345. * > Maybe adding the check to ovl_get_acl() is the right way to go, but
  346. * > I'm a little afraid of a performance regression. Will look into that.
  347. *
  348. * Until we have made a decision allow this helper to take the @noperm
  349. * argument. We should hopefully be able to remove it soon.
  350. */
  351. struct posix_acl *ovl_get_acl_path(const struct path *path,
  352. const char *acl_name, bool noperm)
  353. {
  354. struct posix_acl *real_acl, *clone;
  355. struct mnt_idmap *idmap;
  356. struct inode *realinode = d_inode(path->dentry);
  357. idmap = mnt_idmap(path->mnt);
  358. if (noperm)
  359. real_acl = get_inode_acl(realinode, posix_acl_type(acl_name));
  360. else
  361. real_acl = vfs_get_acl(idmap, path->dentry, acl_name);
  362. if (IS_ERR_OR_NULL(real_acl))
  363. return real_acl;
  364. if (!is_idmapped_mnt(path->mnt))
  365. return real_acl;
  366. /*
  367. * We cannot alter the ACLs returned from the relevant layer as that
  368. * would alter the cached values filesystem wide for the lower
  369. * filesystem. Instead we can clone the ACLs and then apply the
  370. * relevant idmapping of the layer.
  371. */
  372. clone = posix_acl_clone(real_acl, GFP_KERNEL);
  373. posix_acl_release(real_acl); /* release original acl */
  374. if (!clone)
  375. return ERR_PTR(-ENOMEM);
  376. ovl_idmap_posix_acl(realinode, idmap, clone);
  377. return clone;
  378. }
  379. /*
  380. * When the relevant layer is an idmapped mount we need to take the idmapping
  381. * of the layer into account and translate any ACL_{GROUP,USER} values
  382. * according to the idmapped mount.
  383. *
  384. * We cannot alter the ACLs returned from the relevant layer as that would
  385. * alter the cached values filesystem wide for the lower filesystem. Instead we
  386. * can clone the ACLs and then apply the relevant idmapping of the layer.
  387. *
  388. * This is obviously only relevant when idmapped layers are used.
  389. */
  390. struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
  391. struct inode *inode, int type,
  392. bool rcu, bool noperm)
  393. {
  394. struct inode *realinode;
  395. struct posix_acl *acl;
  396. struct path realpath;
  397. /* Careful in RCU walk mode */
  398. realinode = ovl_i_path_real(inode, &realpath);
  399. if (!realinode) {
  400. WARN_ON(!rcu);
  401. return ERR_PTR(-ECHILD);
  402. }
  403. if (!IS_POSIXACL(realinode))
  404. return NULL;
  405. if (rcu) {
  406. /*
  407. * If the layer is idmapped drop out of RCU path walk
  408. * so we can clone the ACLs.
  409. */
  410. if (is_idmapped_mnt(realpath.mnt))
  411. return ERR_PTR(-ECHILD);
  412. acl = get_cached_acl_rcu(realinode, type);
  413. } else {
  414. const struct cred *old_cred;
  415. old_cred = ovl_override_creds(inode->i_sb);
  416. acl = ovl_get_acl_path(&realpath, posix_acl_xattr_name(type), noperm);
  417. revert_creds(old_cred);
  418. }
  419. return acl;
  420. }
  421. static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode,
  422. struct posix_acl *acl, int type)
  423. {
  424. int err;
  425. struct path realpath;
  426. const char *acl_name;
  427. const struct cred *old_cred;
  428. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  429. struct dentry *upperdentry = ovl_dentry_upper(dentry);
  430. struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
  431. /*
  432. * If ACL is to be removed from a lower file, check if it exists in
  433. * the first place before copying it up.
  434. */
  435. acl_name = posix_acl_xattr_name(type);
  436. if (!acl && !upperdentry) {
  437. struct posix_acl *real_acl;
  438. ovl_path_lower(dentry, &realpath);
  439. old_cred = ovl_override_creds(dentry->d_sb);
  440. real_acl = vfs_get_acl(mnt_idmap(realpath.mnt), realdentry,
  441. acl_name);
  442. revert_creds(old_cred);
  443. if (IS_ERR(real_acl)) {
  444. err = PTR_ERR(real_acl);
  445. goto out;
  446. }
  447. posix_acl_release(real_acl);
  448. }
  449. if (!upperdentry) {
  450. err = ovl_copy_up(dentry);
  451. if (err)
  452. goto out;
  453. realdentry = ovl_dentry_upper(dentry);
  454. }
  455. err = ovl_want_write(dentry);
  456. if (err)
  457. goto out;
  458. old_cred = ovl_override_creds(dentry->d_sb);
  459. if (acl)
  460. err = ovl_do_set_acl(ofs, realdentry, acl_name, acl);
  461. else
  462. err = ovl_do_remove_acl(ofs, realdentry, acl_name);
  463. revert_creds(old_cred);
  464. ovl_drop_write(dentry);
  465. /* copy c/mtime */
  466. ovl_copyattr(inode);
  467. out:
  468. return err;
  469. }
  470. int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  471. struct posix_acl *acl, int type)
  472. {
  473. int err;
  474. struct inode *inode = d_inode(dentry);
  475. struct dentry *workdir = ovl_workdir(dentry);
  476. struct inode *realinode = ovl_inode_real(inode);
  477. if (!IS_POSIXACL(d_inode(workdir)))
  478. return -EOPNOTSUPP;
  479. if (!realinode->i_op->set_acl)
  480. return -EOPNOTSUPP;
  481. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  482. return acl ? -EACCES : 0;
  483. if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
  484. return -EPERM;
  485. /*
  486. * Check if sgid bit needs to be cleared (actual setacl operation will
  487. * be done with mounter's capabilities and so that won't do it for us).
  488. */
  489. if (unlikely(inode->i_mode & S_ISGID) && type == ACL_TYPE_ACCESS &&
  490. !in_group_p(inode->i_gid) &&
  491. !capable_wrt_inode_uidgid(&nop_mnt_idmap, inode, CAP_FSETID)) {
  492. struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
  493. err = ovl_setattr(&nop_mnt_idmap, dentry, &iattr);
  494. if (err)
  495. return err;
  496. }
  497. return ovl_set_or_remove_acl(dentry, inode, acl, type);
  498. }
  499. #endif
  500. int ovl_update_time(struct inode *inode, int flags)
  501. {
  502. if (flags & S_ATIME) {
  503. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  504. struct path upperpath = {
  505. .mnt = ovl_upper_mnt(ofs),
  506. .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
  507. };
  508. if (upperpath.dentry) {
  509. touch_atime(&upperpath);
  510. inode_set_atime_to_ts(inode,
  511. inode_get_atime(d_inode(upperpath.dentry)));
  512. }
  513. }
  514. return 0;
  515. }
  516. static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  517. u64 start, u64 len)
  518. {
  519. int err;
  520. struct inode *realinode = ovl_inode_realdata(inode);
  521. const struct cred *old_cred;
  522. if (!realinode)
  523. return -EIO;
  524. if (!realinode->i_op->fiemap)
  525. return -EOPNOTSUPP;
  526. old_cred = ovl_override_creds(inode->i_sb);
  527. err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
  528. revert_creds(old_cred);
  529. return err;
  530. }
  531. /*
  532. * Work around the fact that security_file_ioctl() takes a file argument.
  533. * Introducing security_inode_fileattr_get/set() hooks would solve this issue
  534. * properly.
  535. */
  536. static int ovl_security_fileattr(const struct path *realpath, struct fileattr *fa,
  537. bool set)
  538. {
  539. struct file *file;
  540. unsigned int cmd;
  541. int err;
  542. unsigned int flags;
  543. flags = O_RDONLY;
  544. if (force_o_largefile())
  545. flags |= O_LARGEFILE;
  546. file = dentry_open(realpath, flags, current_cred());
  547. if (IS_ERR(file))
  548. return PTR_ERR(file);
  549. if (set)
  550. cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS;
  551. else
  552. cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS;
  553. err = security_file_ioctl(file, cmd, 0);
  554. fput(file);
  555. return err;
  556. }
  557. int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa)
  558. {
  559. int err;
  560. err = ovl_security_fileattr(realpath, fa, true);
  561. if (err)
  562. return err;
  563. return vfs_fileattr_set(mnt_idmap(realpath->mnt), realpath->dentry, fa);
  564. }
  565. int ovl_fileattr_set(struct mnt_idmap *idmap,
  566. struct dentry *dentry, struct fileattr *fa)
  567. {
  568. struct inode *inode = d_inode(dentry);
  569. struct path upperpath;
  570. const struct cred *old_cred;
  571. unsigned int flags;
  572. int err;
  573. err = ovl_copy_up(dentry);
  574. if (!err) {
  575. ovl_path_real(dentry, &upperpath);
  576. err = ovl_want_write(dentry);
  577. if (err)
  578. goto out;
  579. old_cred = ovl_override_creds(inode->i_sb);
  580. /*
  581. * Store immutable/append-only flags in xattr and clear them
  582. * in upper fileattr (in case they were set by older kernel)
  583. * so children of "ovl-immutable" directories lower aliases of
  584. * "ovl-immutable" hardlinks could be copied up.
  585. * Clear xattr when flags are cleared.
  586. */
  587. err = ovl_set_protattr(inode, upperpath.dentry, fa);
  588. if (!err)
  589. err = ovl_real_fileattr_set(&upperpath, fa);
  590. revert_creds(old_cred);
  591. ovl_drop_write(dentry);
  592. /*
  593. * Merge real inode flags with inode flags read from
  594. * overlay.protattr xattr
  595. */
  596. flags = ovl_inode_real(inode)->i_flags & OVL_COPY_I_FLAGS_MASK;
  597. BUILD_BUG_ON(OVL_PROT_I_FLAGS_MASK & ~OVL_COPY_I_FLAGS_MASK);
  598. flags |= inode->i_flags & OVL_PROT_I_FLAGS_MASK;
  599. inode_set_flags(inode, flags, OVL_COPY_I_FLAGS_MASK);
  600. /* Update ctime */
  601. ovl_copyattr(inode);
  602. }
  603. out:
  604. return err;
  605. }
  606. /* Convert inode protection flags to fileattr flags */
  607. static void ovl_fileattr_prot_flags(struct inode *inode, struct fileattr *fa)
  608. {
  609. BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL);
  610. BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
  611. if (inode->i_flags & S_APPEND) {
  612. fa->flags |= FS_APPEND_FL;
  613. fa->fsx_xflags |= FS_XFLAG_APPEND;
  614. }
  615. if (inode->i_flags & S_IMMUTABLE) {
  616. fa->flags |= FS_IMMUTABLE_FL;
  617. fa->fsx_xflags |= FS_XFLAG_IMMUTABLE;
  618. }
  619. }
  620. int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa)
  621. {
  622. int err;
  623. err = ovl_security_fileattr(realpath, fa, false);
  624. if (err)
  625. return err;
  626. err = vfs_fileattr_get(realpath->dentry, fa);
  627. if (err == -ENOIOCTLCMD)
  628. err = -ENOTTY;
  629. return err;
  630. }
  631. int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  632. {
  633. struct inode *inode = d_inode(dentry);
  634. struct path realpath;
  635. const struct cred *old_cred;
  636. int err;
  637. ovl_path_real(dentry, &realpath);
  638. old_cred = ovl_override_creds(inode->i_sb);
  639. err = ovl_real_fileattr_get(&realpath, fa);
  640. ovl_fileattr_prot_flags(inode, fa);
  641. revert_creds(old_cred);
  642. return err;
  643. }
  644. static const struct inode_operations ovl_file_inode_operations = {
  645. .setattr = ovl_setattr,
  646. .permission = ovl_permission,
  647. .getattr = ovl_getattr,
  648. .listxattr = ovl_listxattr,
  649. .get_inode_acl = ovl_get_inode_acl,
  650. .get_acl = ovl_get_acl,
  651. .set_acl = ovl_set_acl,
  652. .update_time = ovl_update_time,
  653. .fiemap = ovl_fiemap,
  654. .fileattr_get = ovl_fileattr_get,
  655. .fileattr_set = ovl_fileattr_set,
  656. };
  657. static const struct inode_operations ovl_symlink_inode_operations = {
  658. .setattr = ovl_setattr,
  659. .get_link = ovl_get_link,
  660. .getattr = ovl_getattr,
  661. .listxattr = ovl_listxattr,
  662. .update_time = ovl_update_time,
  663. };
  664. static const struct inode_operations ovl_special_inode_operations = {
  665. .setattr = ovl_setattr,
  666. .permission = ovl_permission,
  667. .getattr = ovl_getattr,
  668. .listxattr = ovl_listxattr,
  669. .get_inode_acl = ovl_get_inode_acl,
  670. .get_acl = ovl_get_acl,
  671. .set_acl = ovl_set_acl,
  672. .update_time = ovl_update_time,
  673. };
  674. static const struct address_space_operations ovl_aops = {
  675. /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
  676. .direct_IO = noop_direct_IO,
  677. };
  678. /*
  679. * It is possible to stack overlayfs instance on top of another
  680. * overlayfs instance as lower layer. We need to annotate the
  681. * stackable i_mutex locks according to stack level of the super
  682. * block instance. An overlayfs instance can never be in stack
  683. * depth 0 (there is always a real fs below it). An overlayfs
  684. * inode lock will use the lockdep annotation ovl_i_mutex_key[depth].
  685. *
  686. * For example, here is a snip from /proc/lockdep_chains after
  687. * dir_iterate of nested overlayfs:
  688. *
  689. * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
  690. * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
  691. * [...] &type->i_mutex_dir_key (stack_depth=0)
  692. *
  693. * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
  694. *
  695. * This chain is valid:
  696. * - inode->i_rwsem (inode_lock[2])
  697. * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
  698. * - OVL_I(inode)->lock (ovl_inode_lock[2])
  699. * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
  700. *
  701. * And this chain is valid:
  702. * - inode->i_rwsem (inode_lock[2])
  703. * - OVL_I(inode)->lock (ovl_inode_lock[2])
  704. * - lowerinode->i_rwsem (inode_lock[1])
  705. * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
  706. *
  707. * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
  708. * held, because it is in reverse order of the non-nested case using the same
  709. * upper fs:
  710. * - inode->i_rwsem (inode_lock[1])
  711. * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
  712. * - OVL_I(inode)->lock (ovl_inode_lock[1])
  713. */
  714. #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
  715. static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
  716. {
  717. #ifdef CONFIG_LOCKDEP
  718. static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
  719. static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
  720. static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
  721. int depth = inode->i_sb->s_stack_depth - 1;
  722. if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
  723. depth = 0;
  724. if (S_ISDIR(inode->i_mode))
  725. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
  726. else
  727. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
  728. lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
  729. #endif
  730. }
  731. static void ovl_next_ino(struct inode *inode)
  732. {
  733. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  734. inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
  735. if (unlikely(!inode->i_ino))
  736. inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
  737. }
  738. static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid)
  739. {
  740. struct ovl_fs *ofs = OVL_FS(inode->i_sb);
  741. int xinobits = ovl_xino_bits(ofs);
  742. unsigned int xinoshift = 64 - xinobits;
  743. /*
  744. * When d_ino is consistent with st_ino (samefs or i_ino has enough
  745. * bits to encode layer), set the same value used for st_ino to i_ino,
  746. * so inode number exposed via /proc/locks and a like will be
  747. * consistent with d_ino and st_ino values. An i_ino value inconsistent
  748. * with d_ino also causes nfsd readdirplus to fail.
  749. */
  750. inode->i_ino = ino;
  751. if (ovl_same_fs(ofs)) {
  752. return;
  753. } else if (xinobits && likely(!(ino >> xinoshift))) {
  754. inode->i_ino |= (unsigned long)fsid << (xinoshift + 1);
  755. return;
  756. }
  757. /*
  758. * For directory inodes on non-samefs with xino disabled or xino
  759. * overflow, we allocate a non-persistent inode number, to be used for
  760. * resolving st_ino collisions in ovl_map_dev_ino().
  761. *
  762. * To avoid ino collision with legitimate xino values from upper
  763. * layer (fsid 0), use the lowest xinobit to map the non
  764. * persistent inode numbers to the unified st_ino address space.
  765. */
  766. if (S_ISDIR(inode->i_mode)) {
  767. ovl_next_ino(inode);
  768. if (xinobits) {
  769. inode->i_ino &= ~0UL >> xinobits;
  770. inode->i_ino |= 1UL << xinoshift;
  771. }
  772. }
  773. }
  774. void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
  775. unsigned long ino, int fsid)
  776. {
  777. struct inode *realinode;
  778. struct ovl_inode *oi = OVL_I(inode);
  779. oi->__upperdentry = oip->upperdentry;
  780. oi->oe = oip->oe;
  781. oi->redirect = oip->redirect;
  782. oi->lowerdata_redirect = oip->lowerdata_redirect;
  783. realinode = ovl_inode_real(inode);
  784. ovl_copyattr(inode);
  785. ovl_copyflags(realinode, inode);
  786. ovl_map_ino(inode, ino, fsid);
  787. }
  788. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
  789. {
  790. inode->i_mode = mode;
  791. inode->i_flags |= S_NOCMTIME;
  792. #ifdef CONFIG_FS_POSIX_ACL
  793. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  794. #endif
  795. ovl_lockdep_annotate_inode_mutex_key(inode);
  796. switch (mode & S_IFMT) {
  797. case S_IFREG:
  798. inode->i_op = &ovl_file_inode_operations;
  799. inode->i_fop = &ovl_file_operations;
  800. inode->i_mapping->a_ops = &ovl_aops;
  801. break;
  802. case S_IFDIR:
  803. inode->i_op = &ovl_dir_inode_operations;
  804. inode->i_fop = &ovl_dir_operations;
  805. break;
  806. case S_IFLNK:
  807. inode->i_op = &ovl_symlink_inode_operations;
  808. break;
  809. default:
  810. inode->i_op = &ovl_special_inode_operations;
  811. init_special_inode(inode, mode, rdev);
  812. break;
  813. }
  814. }
  815. /*
  816. * With inodes index enabled, an overlay inode nlink counts the union of upper
  817. * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
  818. * upper inode, the following nlink modifying operations can happen:
  819. *
  820. * 1. Lower hardlink copy up
  821. * 2. Upper hardlink created, unlinked or renamed over
  822. * 3. Lower hardlink whiteout or renamed over
  823. *
  824. * For the first, copy up case, the union nlink does not change, whether the
  825. * operation succeeds or fails, but the upper inode nlink may change.
  826. * Therefore, before copy up, we store the union nlink value relative to the
  827. * lower inode nlink in the index inode xattr .overlay.nlink.
  828. *
  829. * For the second, upper hardlink case, the union nlink should be incremented
  830. * or decremented IFF the operation succeeds, aligned with nlink change of the
  831. * upper inode. Therefore, before link/unlink/rename, we store the union nlink
  832. * value relative to the upper inode nlink in the index inode.
  833. *
  834. * For the last, lower cover up case, we simplify things by preceding the
  835. * whiteout or cover up with copy up. This makes sure that there is an index
  836. * upper inode where the nlink xattr can be stored before the copied up upper
  837. * entry is unlink.
  838. */
  839. #define OVL_NLINK_ADD_UPPER (1 << 0)
  840. /*
  841. * On-disk format for indexed nlink:
  842. *
  843. * nlink relative to the upper inode - "U[+-]NUM"
  844. * nlink relative to the lower inode - "L[+-]NUM"
  845. */
  846. static int ovl_set_nlink_common(struct dentry *dentry,
  847. struct dentry *realdentry, const char *format)
  848. {
  849. struct inode *inode = d_inode(dentry);
  850. struct inode *realinode = d_inode(realdentry);
  851. char buf[13];
  852. int len;
  853. len = snprintf(buf, sizeof(buf), format,
  854. (int) (inode->i_nlink - realinode->i_nlink));
  855. if (WARN_ON(len >= sizeof(buf)))
  856. return -EIO;
  857. return ovl_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry),
  858. OVL_XATTR_NLINK, buf, len);
  859. }
  860. int ovl_set_nlink_upper(struct dentry *dentry)
  861. {
  862. return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
  863. }
  864. int ovl_set_nlink_lower(struct dentry *dentry)
  865. {
  866. return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
  867. }
  868. unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
  869. struct dentry *upperdentry,
  870. unsigned int fallback)
  871. {
  872. int nlink_diff;
  873. int nlink;
  874. char buf[13];
  875. int err;
  876. if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
  877. return fallback;
  878. err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK,
  879. &buf, sizeof(buf) - 1);
  880. if (err < 0)
  881. goto fail;
  882. buf[err] = '\0';
  883. if ((buf[0] != 'L' && buf[0] != 'U') ||
  884. (buf[1] != '+' && buf[1] != '-'))
  885. goto fail;
  886. err = kstrtoint(buf + 1, 10, &nlink_diff);
  887. if (err < 0)
  888. goto fail;
  889. nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
  890. nlink += nlink_diff;
  891. if (nlink <= 0)
  892. goto fail;
  893. return nlink;
  894. fail:
  895. pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
  896. upperdentry, err);
  897. return fallback;
  898. }
  899. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  900. {
  901. struct inode *inode;
  902. inode = new_inode(sb);
  903. if (inode)
  904. ovl_fill_inode(inode, mode, rdev);
  905. return inode;
  906. }
  907. static int ovl_inode_test(struct inode *inode, void *data)
  908. {
  909. return inode->i_private == data;
  910. }
  911. static int ovl_inode_set(struct inode *inode, void *data)
  912. {
  913. inode->i_private = data;
  914. return 0;
  915. }
  916. static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
  917. struct dentry *upperdentry, bool strict)
  918. {
  919. /*
  920. * For directories, @strict verify from lookup path performs consistency
  921. * checks, so NULL lower/upper in dentry must match NULL lower/upper in
  922. * inode. Non @strict verify from NFS handle decode path passes NULL for
  923. * 'unknown' lower/upper.
  924. */
  925. if (S_ISDIR(inode->i_mode) && strict) {
  926. /* Real lower dir moved to upper layer under us? */
  927. if (!lowerdentry && ovl_inode_lower(inode))
  928. return false;
  929. /* Lookup of an uncovered redirect origin? */
  930. if (!upperdentry && ovl_inode_upper(inode))
  931. return false;
  932. }
  933. /*
  934. * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
  935. * This happens when finding a copied up overlay inode for a renamed
  936. * or hardlinked overlay dentry and lower dentry cannot be followed
  937. * by origin because lower fs does not support file handles.
  938. */
  939. if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
  940. return false;
  941. /*
  942. * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
  943. * This happens when finding a lower alias for a copied up hard link.
  944. */
  945. if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
  946. return false;
  947. return true;
  948. }
  949. struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
  950. bool is_upper)
  951. {
  952. struct inode *inode, *key = d_inode(real);
  953. inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
  954. if (!inode)
  955. return NULL;
  956. if (!ovl_verify_inode(inode, is_upper ? NULL : real,
  957. is_upper ? real : NULL, false)) {
  958. iput(inode);
  959. return ERR_PTR(-ESTALE);
  960. }
  961. return inode;
  962. }
  963. bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
  964. {
  965. struct inode *key = d_inode(dir);
  966. struct inode *trap;
  967. bool res;
  968. trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
  969. if (!trap)
  970. return false;
  971. res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
  972. !ovl_inode_lower(trap);
  973. iput(trap);
  974. return res;
  975. }
  976. /*
  977. * Create an inode cache entry for layer root dir, that will intentionally
  978. * fail ovl_verify_inode(), so any lookup that will find some layer root
  979. * will fail.
  980. */
  981. struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
  982. {
  983. struct inode *key = d_inode(dir);
  984. struct inode *trap;
  985. if (!d_is_dir(dir))
  986. return ERR_PTR(-ENOTDIR);
  987. trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
  988. ovl_inode_set, key);
  989. if (!trap)
  990. return ERR_PTR(-ENOMEM);
  991. if (!(trap->i_state & I_NEW)) {
  992. /* Conflicting layer roots? */
  993. iput(trap);
  994. return ERR_PTR(-ELOOP);
  995. }
  996. trap->i_mode = S_IFDIR;
  997. trap->i_flags = S_DEAD;
  998. unlock_new_inode(trap);
  999. return trap;
  1000. }
  1001. /*
  1002. * Does overlay inode need to be hashed by lower inode?
  1003. */
  1004. static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
  1005. struct dentry *lower, bool index)
  1006. {
  1007. struct ovl_fs *ofs = OVL_FS(sb);
  1008. /* No, if pure upper */
  1009. if (!lower)
  1010. return false;
  1011. /* Yes, if already indexed */
  1012. if (index)
  1013. return true;
  1014. /* Yes, if won't be copied up */
  1015. if (!ovl_upper_mnt(ofs))
  1016. return true;
  1017. /* No, if lower hardlink is or will be broken on copy up */
  1018. if ((upper || !ovl_indexdir(sb)) &&
  1019. !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  1020. return false;
  1021. /* No, if non-indexed upper with NFS export */
  1022. if (ofs->config.nfs_export && upper)
  1023. return false;
  1024. /* Otherwise, hash by lower inode for fsnotify */
  1025. return true;
  1026. }
  1027. static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
  1028. struct inode *key)
  1029. {
  1030. return newinode ? inode_insert5(newinode, (unsigned long) key,
  1031. ovl_inode_test, ovl_inode_set, key) :
  1032. iget5_locked(sb, (unsigned long) key,
  1033. ovl_inode_test, ovl_inode_set, key);
  1034. }
  1035. struct inode *ovl_get_inode(struct super_block *sb,
  1036. struct ovl_inode_params *oip)
  1037. {
  1038. struct ovl_fs *ofs = OVL_FS(sb);
  1039. struct dentry *upperdentry = oip->upperdentry;
  1040. struct ovl_path *lowerpath = ovl_lowerpath(oip->oe);
  1041. struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
  1042. struct inode *inode;
  1043. struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
  1044. struct path realpath = {
  1045. .dentry = upperdentry ?: lowerdentry,
  1046. .mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt,
  1047. };
  1048. bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
  1049. oip->index);
  1050. int fsid = bylower ? lowerpath->layer->fsid : 0;
  1051. bool is_dir;
  1052. unsigned long ino = 0;
  1053. int err = oip->newinode ? -EEXIST : -ENOMEM;
  1054. if (!realinode)
  1055. realinode = d_inode(lowerdentry);
  1056. /*
  1057. * Copy up origin (lower) may exist for non-indexed upper, but we must
  1058. * not use lower as hash key if this is a broken hardlink.
  1059. */
  1060. is_dir = S_ISDIR(realinode->i_mode);
  1061. if (upperdentry || bylower) {
  1062. struct inode *key = d_inode(bylower ? lowerdentry :
  1063. upperdentry);
  1064. unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
  1065. inode = ovl_iget5(sb, oip->newinode, key);
  1066. if (!inode)
  1067. goto out_err;
  1068. if (!(inode->i_state & I_NEW)) {
  1069. /*
  1070. * Verify that the underlying files stored in the inode
  1071. * match those in the dentry.
  1072. */
  1073. if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
  1074. true)) {
  1075. iput(inode);
  1076. err = -ESTALE;
  1077. goto out_err;
  1078. }
  1079. dput(upperdentry);
  1080. ovl_free_entry(oip->oe);
  1081. kfree(oip->redirect);
  1082. kfree(oip->lowerdata_redirect);
  1083. goto out;
  1084. }
  1085. /* Recalculate nlink for non-dir due to indexing */
  1086. if (!is_dir)
  1087. nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry,
  1088. nlink);
  1089. set_nlink(inode, nlink);
  1090. ino = key->i_ino;
  1091. } else {
  1092. /* Lower hardlink that will be broken on copy up */
  1093. inode = new_inode(sb);
  1094. if (!inode) {
  1095. err = -ENOMEM;
  1096. goto out_err;
  1097. }
  1098. ino = realinode->i_ino;
  1099. fsid = lowerpath->layer->fsid;
  1100. }
  1101. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
  1102. ovl_inode_init(inode, oip, ino, fsid);
  1103. if (upperdentry && ovl_is_impuredir(sb, upperdentry))
  1104. ovl_set_flag(OVL_IMPURE, inode);
  1105. if (oip->index)
  1106. ovl_set_flag(OVL_INDEX, inode);
  1107. if (bylower)
  1108. ovl_set_flag(OVL_CONST_INO, inode);
  1109. /* Check for non-merge dir that may have whiteouts */
  1110. if (is_dir) {
  1111. if (((upperdentry && lowerdentry) || ovl_numlower(oip->oe) > 1) ||
  1112. ovl_path_check_origin_xattr(ofs, &realpath)) {
  1113. ovl_set_flag(OVL_WHITEOUTS, inode);
  1114. }
  1115. }
  1116. /* Check for immutable/append-only inode flags in xattr */
  1117. if (upperdentry)
  1118. ovl_check_protattr(inode, upperdentry);
  1119. if (inode->i_state & I_NEW)
  1120. unlock_new_inode(inode);
  1121. out:
  1122. return inode;
  1123. out_err:
  1124. pr_warn_ratelimited("failed to get inode (%i)\n", err);
  1125. inode = ERR_PTR(err);
  1126. goto out;
  1127. }