inode.c 35 KB

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