inode.c 28 KB

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