readdir.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/namei.h>
  12. #include <linux/file.h>
  13. #include <linux/xattr.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/security.h>
  16. #include <linux/cred.h>
  17. #include <linux/ratelimit.h>
  18. #include "overlayfs.h"
  19. struct ovl_cache_entry {
  20. unsigned int len;
  21. unsigned int type;
  22. u64 real_ino;
  23. u64 ino;
  24. struct list_head l_node;
  25. struct rb_node node;
  26. struct ovl_cache_entry *next_maybe_whiteout;
  27. bool is_upper;
  28. bool is_whiteout;
  29. char name[];
  30. };
  31. struct ovl_dir_cache {
  32. long refcount;
  33. u64 version;
  34. struct list_head entries;
  35. struct rb_root root;
  36. };
  37. struct ovl_readdir_data {
  38. struct dir_context ctx;
  39. struct dentry *dentry;
  40. bool is_lowest;
  41. struct rb_root *root;
  42. struct list_head *list;
  43. struct list_head middle;
  44. struct ovl_cache_entry *first_maybe_whiteout;
  45. int count;
  46. int err;
  47. bool is_upper;
  48. bool d_type_supported;
  49. };
  50. struct ovl_dir_file {
  51. bool is_real;
  52. bool is_upper;
  53. struct ovl_dir_cache *cache;
  54. struct list_head *cursor;
  55. struct file *realfile;
  56. struct file *upperfile;
  57. };
  58. static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
  59. {
  60. return rb_entry(n, struct ovl_cache_entry, node);
  61. }
  62. static bool ovl_cache_entry_find_link(const char *name, int len,
  63. struct rb_node ***link,
  64. struct rb_node **parent)
  65. {
  66. bool found = false;
  67. struct rb_node **newp = *link;
  68. while (!found && *newp) {
  69. int cmp;
  70. struct ovl_cache_entry *tmp;
  71. *parent = *newp;
  72. tmp = ovl_cache_entry_from_node(*newp);
  73. cmp = strncmp(name, tmp->name, len);
  74. if (cmp > 0)
  75. newp = &tmp->node.rb_right;
  76. else if (cmp < 0 || len < tmp->len)
  77. newp = &tmp->node.rb_left;
  78. else
  79. found = true;
  80. }
  81. *link = newp;
  82. return found;
  83. }
  84. static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
  85. const char *name, int len)
  86. {
  87. struct rb_node *node = root->rb_node;
  88. int cmp;
  89. while (node) {
  90. struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
  91. cmp = strncmp(name, p->name, len);
  92. if (cmp > 0)
  93. node = p->node.rb_right;
  94. else if (cmp < 0 || len < p->len)
  95. node = p->node.rb_left;
  96. else
  97. return p;
  98. }
  99. return NULL;
  100. }
  101. static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
  102. struct ovl_cache_entry *p)
  103. {
  104. /* Don't care if not doing ovl_iter() */
  105. if (!rdd->dentry)
  106. return false;
  107. /* Always recalc d_ino when remapping lower inode numbers */
  108. if (ovl_xino_bits(rdd->dentry->d_sb))
  109. return true;
  110. /* Always recalc d_ino for parent */
  111. if (strcmp(p->name, "..") == 0)
  112. return true;
  113. /* If this is lower, then native d_ino will do */
  114. if (!rdd->is_upper)
  115. return false;
  116. /*
  117. * Recalc d_ino for '.' and for all entries if dir is impure (contains
  118. * copied up entries)
  119. */
  120. if ((p->name[0] == '.' && p->len == 1) ||
  121. ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
  122. return true;
  123. return false;
  124. }
  125. static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
  126. const char *name, int len,
  127. u64 ino, unsigned int d_type)
  128. {
  129. struct ovl_cache_entry *p;
  130. size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
  131. p = kmalloc(size, GFP_KERNEL);
  132. if (!p)
  133. return NULL;
  134. memcpy(p->name, name, len);
  135. p->name[len] = '\0';
  136. p->len = len;
  137. p->type = d_type;
  138. p->real_ino = ino;
  139. p->ino = ino;
  140. /* Defer setting d_ino for upper entry to ovl_iterate() */
  141. if (ovl_calc_d_ino(rdd, p))
  142. p->ino = 0;
  143. p->is_upper = rdd->is_upper;
  144. p->is_whiteout = false;
  145. if (d_type == DT_CHR) {
  146. p->next_maybe_whiteout = rdd->first_maybe_whiteout;
  147. rdd->first_maybe_whiteout = p;
  148. }
  149. return p;
  150. }
  151. static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
  152. const char *name, int len, u64 ino,
  153. unsigned int d_type)
  154. {
  155. struct rb_node **newp = &rdd->root->rb_node;
  156. struct rb_node *parent = NULL;
  157. struct ovl_cache_entry *p;
  158. if (ovl_cache_entry_find_link(name, len, &newp, &parent))
  159. return 0;
  160. p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
  161. if (p == NULL) {
  162. rdd->err = -ENOMEM;
  163. return -ENOMEM;
  164. }
  165. list_add_tail(&p->l_node, rdd->list);
  166. rb_link_node(&p->node, parent, newp);
  167. rb_insert_color(&p->node, rdd->root);
  168. return 0;
  169. }
  170. static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
  171. const char *name, int namelen,
  172. loff_t offset, u64 ino, unsigned int d_type)
  173. {
  174. struct ovl_cache_entry *p;
  175. p = ovl_cache_entry_find(rdd->root, name, namelen);
  176. if (p) {
  177. list_move_tail(&p->l_node, &rdd->middle);
  178. } else {
  179. p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
  180. if (p == NULL)
  181. rdd->err = -ENOMEM;
  182. else
  183. list_add_tail(&p->l_node, &rdd->middle);
  184. }
  185. return rdd->err;
  186. }
  187. void ovl_cache_free(struct list_head *list)
  188. {
  189. struct ovl_cache_entry *p;
  190. struct ovl_cache_entry *n;
  191. list_for_each_entry_safe(p, n, list, l_node)
  192. kfree(p);
  193. INIT_LIST_HEAD(list);
  194. }
  195. void ovl_dir_cache_free(struct inode *inode)
  196. {
  197. struct ovl_dir_cache *cache = ovl_dir_cache(inode);
  198. if (cache) {
  199. ovl_cache_free(&cache->entries);
  200. kfree(cache);
  201. }
  202. }
  203. static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
  204. {
  205. struct ovl_dir_cache *cache = od->cache;
  206. WARN_ON(cache->refcount <= 0);
  207. cache->refcount--;
  208. if (!cache->refcount) {
  209. if (ovl_dir_cache(d_inode(dentry)) == cache)
  210. ovl_set_dir_cache(d_inode(dentry), NULL);
  211. ovl_cache_free(&cache->entries);
  212. kfree(cache);
  213. }
  214. }
  215. static int ovl_fill_merge(struct dir_context *ctx, const char *name,
  216. int namelen, loff_t offset, u64 ino,
  217. unsigned int d_type)
  218. {
  219. struct ovl_readdir_data *rdd =
  220. container_of(ctx, struct ovl_readdir_data, ctx);
  221. rdd->count++;
  222. if (!rdd->is_lowest)
  223. return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
  224. else
  225. return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
  226. }
  227. static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
  228. {
  229. int err;
  230. struct ovl_cache_entry *p;
  231. struct dentry *dentry;
  232. const struct cred *old_cred;
  233. old_cred = ovl_override_creds(rdd->dentry->d_sb);
  234. err = down_write_killable(&dir->d_inode->i_rwsem);
  235. if (!err) {
  236. while (rdd->first_maybe_whiteout) {
  237. p = rdd->first_maybe_whiteout;
  238. rdd->first_maybe_whiteout = p->next_maybe_whiteout;
  239. dentry = lookup_one_len(p->name, dir, p->len);
  240. if (!IS_ERR(dentry)) {
  241. p->is_whiteout = ovl_is_whiteout(dentry);
  242. dput(dentry);
  243. }
  244. }
  245. inode_unlock(dir->d_inode);
  246. }
  247. revert_creds(old_cred);
  248. return err;
  249. }
  250. static inline int ovl_dir_read(struct path *realpath,
  251. struct ovl_readdir_data *rdd)
  252. {
  253. struct file *realfile;
  254. int err;
  255. realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
  256. if (IS_ERR(realfile))
  257. return PTR_ERR(realfile);
  258. rdd->first_maybe_whiteout = NULL;
  259. rdd->ctx.pos = 0;
  260. do {
  261. rdd->count = 0;
  262. rdd->err = 0;
  263. err = iterate_dir(realfile, &rdd->ctx);
  264. if (err >= 0)
  265. err = rdd->err;
  266. } while (!err && rdd->count);
  267. if (!err && rdd->first_maybe_whiteout && rdd->dentry)
  268. err = ovl_check_whiteouts(realpath->dentry, rdd);
  269. fput(realfile);
  270. return err;
  271. }
  272. /*
  273. * Can we iterate real dir directly?
  274. *
  275. * Non-merge dir may contain whiteouts from a time it was a merge upper, before
  276. * lower dir was removed under it and possibly before it was rotated from upper
  277. * to lower layer.
  278. */
  279. static bool ovl_dir_is_real(struct dentry *dir)
  280. {
  281. return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
  282. }
  283. static void ovl_dir_reset(struct file *file)
  284. {
  285. struct ovl_dir_file *od = file->private_data;
  286. struct ovl_dir_cache *cache = od->cache;
  287. struct dentry *dentry = file->f_path.dentry;
  288. bool is_real;
  289. if (cache && ovl_dentry_version_get(dentry) != cache->version) {
  290. ovl_cache_put(od, dentry);
  291. od->cache = NULL;
  292. od->cursor = NULL;
  293. }
  294. is_real = ovl_dir_is_real(dentry);
  295. if (od->is_real != is_real) {
  296. /* is_real can only become false when dir is copied up */
  297. if (WARN_ON(is_real))
  298. return;
  299. od->is_real = false;
  300. }
  301. }
  302. static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
  303. struct rb_root *root)
  304. {
  305. int err;
  306. struct path realpath;
  307. struct ovl_readdir_data rdd = {
  308. .ctx.actor = ovl_fill_merge,
  309. .dentry = dentry,
  310. .list = list,
  311. .root = root,
  312. .is_lowest = false,
  313. };
  314. int idx, next;
  315. for (idx = 0; idx != -1; idx = next) {
  316. next = ovl_path_next(idx, dentry, &realpath);
  317. rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
  318. if (next != -1) {
  319. err = ovl_dir_read(&realpath, &rdd);
  320. if (err)
  321. break;
  322. } else {
  323. /*
  324. * Insert lowest layer entries before upper ones, this
  325. * allows offsets to be reasonably constant
  326. */
  327. list_add(&rdd.middle, rdd.list);
  328. rdd.is_lowest = true;
  329. err = ovl_dir_read(&realpath, &rdd);
  330. list_del(&rdd.middle);
  331. }
  332. }
  333. return err;
  334. }
  335. static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
  336. {
  337. struct list_head *p;
  338. loff_t off = 0;
  339. list_for_each(p, &od->cache->entries) {
  340. if (off >= pos)
  341. break;
  342. off++;
  343. }
  344. /* Cursor is safe since the cache is stable */
  345. od->cursor = p;
  346. }
  347. static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
  348. {
  349. int res;
  350. struct ovl_dir_cache *cache;
  351. cache = ovl_dir_cache(d_inode(dentry));
  352. if (cache && ovl_dentry_version_get(dentry) == cache->version) {
  353. WARN_ON(!cache->refcount);
  354. cache->refcount++;
  355. return cache;
  356. }
  357. ovl_set_dir_cache(d_inode(dentry), NULL);
  358. cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
  359. if (!cache)
  360. return ERR_PTR(-ENOMEM);
  361. cache->refcount = 1;
  362. INIT_LIST_HEAD(&cache->entries);
  363. cache->root = RB_ROOT;
  364. res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
  365. if (res) {
  366. ovl_cache_free(&cache->entries);
  367. kfree(cache);
  368. return ERR_PTR(res);
  369. }
  370. cache->version = ovl_dentry_version_get(dentry);
  371. ovl_set_dir_cache(d_inode(dentry), cache);
  372. return cache;
  373. }
  374. /* Map inode number to lower fs unique range */
  375. static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
  376. const char *name, int namelen)
  377. {
  378. if (ino >> (64 - xinobits)) {
  379. pr_warn_ratelimited("overlayfs: d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
  380. namelen, name, ino, xinobits);
  381. return ino;
  382. }
  383. return ino | ((u64)fsid) << (64 - xinobits);
  384. }
  385. /*
  386. * Set d_ino for upper entries. Non-upper entries should always report
  387. * the uppermost real inode ino and should not call this function.
  388. *
  389. * When not all layer are on same fs, report real ino also for upper.
  390. *
  391. * When all layers are on the same fs, and upper has a reference to
  392. * copy up origin, call vfs_getattr() on the overlay entry to make
  393. * sure that d_ino will be consistent with st_ino from stat(2).
  394. */
  395. static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
  396. {
  397. struct dentry *dir = path->dentry;
  398. struct dentry *this = NULL;
  399. enum ovl_path_type type;
  400. u64 ino = p->real_ino;
  401. int xinobits = ovl_xino_bits(dir->d_sb);
  402. int err = 0;
  403. if (!ovl_same_sb(dir->d_sb) && !xinobits)
  404. goto out;
  405. if (p->name[0] == '.') {
  406. if (p->len == 1) {
  407. this = dget(dir);
  408. goto get;
  409. }
  410. if (p->len == 2 && p->name[1] == '.') {
  411. /* we shall not be moved */
  412. this = dget(dir->d_parent);
  413. goto get;
  414. }
  415. }
  416. this = lookup_one_len(p->name, dir, p->len);
  417. if (IS_ERR_OR_NULL(this) || !this->d_inode) {
  418. if (IS_ERR(this)) {
  419. err = PTR_ERR(this);
  420. this = NULL;
  421. goto fail;
  422. }
  423. goto out;
  424. }
  425. get:
  426. type = ovl_path_type(this);
  427. if (OVL_TYPE_ORIGIN(type)) {
  428. struct kstat stat;
  429. struct path statpath = *path;
  430. statpath.dentry = this;
  431. err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
  432. if (err)
  433. goto fail;
  434. /*
  435. * Directory inode is always on overlay st_dev.
  436. * Non-dir with ovl_same_dev() could be on pseudo st_dev in case
  437. * of xino bits overflow.
  438. */
  439. WARN_ON_ONCE(S_ISDIR(stat.mode) &&
  440. dir->d_sb->s_dev != stat.dev);
  441. ino = stat.ino;
  442. } else if (xinobits && !OVL_TYPE_UPPER(type)) {
  443. ino = ovl_remap_lower_ino(ino, xinobits,
  444. ovl_layer_lower(this)->fsid,
  445. p->name, p->len);
  446. }
  447. out:
  448. p->ino = ino;
  449. dput(this);
  450. return err;
  451. fail:
  452. pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n",
  453. p->name, err);
  454. goto out;
  455. }
  456. static int ovl_fill_plain(struct dir_context *ctx, const char *name,
  457. int namelen, loff_t offset, u64 ino,
  458. unsigned int d_type)
  459. {
  460. struct ovl_cache_entry *p;
  461. struct ovl_readdir_data *rdd =
  462. container_of(ctx, struct ovl_readdir_data, ctx);
  463. rdd->count++;
  464. p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
  465. if (p == NULL) {
  466. rdd->err = -ENOMEM;
  467. return -ENOMEM;
  468. }
  469. list_add_tail(&p->l_node, rdd->list);
  470. return 0;
  471. }
  472. static int ovl_dir_read_impure(struct path *path, struct list_head *list,
  473. struct rb_root *root)
  474. {
  475. int err;
  476. struct path realpath;
  477. struct ovl_cache_entry *p, *n;
  478. struct ovl_readdir_data rdd = {
  479. .ctx.actor = ovl_fill_plain,
  480. .list = list,
  481. .root = root,
  482. };
  483. INIT_LIST_HEAD(list);
  484. *root = RB_ROOT;
  485. ovl_path_upper(path->dentry, &realpath);
  486. err = ovl_dir_read(&realpath, &rdd);
  487. if (err)
  488. return err;
  489. list_for_each_entry_safe(p, n, list, l_node) {
  490. if (strcmp(p->name, ".") != 0 &&
  491. strcmp(p->name, "..") != 0) {
  492. err = ovl_cache_update_ino(path, p);
  493. if (err)
  494. return err;
  495. }
  496. if (p->ino == p->real_ino) {
  497. list_del(&p->l_node);
  498. kfree(p);
  499. } else {
  500. struct rb_node **newp = &root->rb_node;
  501. struct rb_node *parent = NULL;
  502. if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
  503. &newp, &parent)))
  504. return -EIO;
  505. rb_link_node(&p->node, parent, newp);
  506. rb_insert_color(&p->node, root);
  507. }
  508. }
  509. return 0;
  510. }
  511. static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
  512. {
  513. int res;
  514. struct dentry *dentry = path->dentry;
  515. struct ovl_dir_cache *cache;
  516. cache = ovl_dir_cache(d_inode(dentry));
  517. if (cache && ovl_dentry_version_get(dentry) == cache->version)
  518. return cache;
  519. /* Impure cache is not refcounted, free it here */
  520. ovl_dir_cache_free(d_inode(dentry));
  521. ovl_set_dir_cache(d_inode(dentry), NULL);
  522. cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
  523. if (!cache)
  524. return ERR_PTR(-ENOMEM);
  525. res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
  526. if (res) {
  527. ovl_cache_free(&cache->entries);
  528. kfree(cache);
  529. return ERR_PTR(res);
  530. }
  531. if (list_empty(&cache->entries)) {
  532. /*
  533. * A good opportunity to get rid of an unneeded "impure" flag.
  534. * Removing the "impure" xattr is best effort.
  535. */
  536. if (!ovl_want_write(dentry)) {
  537. ovl_do_removexattr(ovl_dentry_upper(dentry),
  538. OVL_XATTR_IMPURE);
  539. ovl_drop_write(dentry);
  540. }
  541. ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
  542. kfree(cache);
  543. return NULL;
  544. }
  545. cache->version = ovl_dentry_version_get(dentry);
  546. ovl_set_dir_cache(d_inode(dentry), cache);
  547. return cache;
  548. }
  549. struct ovl_readdir_translate {
  550. struct dir_context *orig_ctx;
  551. struct ovl_dir_cache *cache;
  552. struct dir_context ctx;
  553. u64 parent_ino;
  554. int fsid;
  555. int xinobits;
  556. };
  557. static int ovl_fill_real(struct dir_context *ctx, const char *name,
  558. int namelen, loff_t offset, u64 ino,
  559. unsigned int d_type)
  560. {
  561. struct ovl_readdir_translate *rdt =
  562. container_of(ctx, struct ovl_readdir_translate, ctx);
  563. struct dir_context *orig_ctx = rdt->orig_ctx;
  564. if (rdt->parent_ino && strcmp(name, "..") == 0) {
  565. ino = rdt->parent_ino;
  566. } else if (rdt->cache) {
  567. struct ovl_cache_entry *p;
  568. p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
  569. if (p)
  570. ino = p->ino;
  571. } else if (rdt->xinobits) {
  572. ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
  573. name, namelen);
  574. }
  575. return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
  576. }
  577. static bool ovl_is_impure_dir(struct file *file)
  578. {
  579. struct ovl_dir_file *od = file->private_data;
  580. struct inode *dir = d_inode(file->f_path.dentry);
  581. /*
  582. * Only upper dir can be impure, but if we are in the middle of
  583. * iterating a lower real dir, dir could be copied up and marked
  584. * impure. We only want the impure cache if we started iterating
  585. * a real upper dir to begin with.
  586. */
  587. return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
  588. }
  589. static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
  590. {
  591. int err;
  592. struct ovl_dir_file *od = file->private_data;
  593. struct dentry *dir = file->f_path.dentry;
  594. struct ovl_layer *lower_layer = ovl_layer_lower(dir);
  595. struct ovl_readdir_translate rdt = {
  596. .ctx.actor = ovl_fill_real,
  597. .orig_ctx = ctx,
  598. .xinobits = ovl_xino_bits(dir->d_sb),
  599. };
  600. if (rdt.xinobits && lower_layer)
  601. rdt.fsid = lower_layer->fsid;
  602. if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
  603. struct kstat stat;
  604. struct path statpath = file->f_path;
  605. statpath.dentry = dir->d_parent;
  606. err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
  607. if (err)
  608. return err;
  609. WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
  610. rdt.parent_ino = stat.ino;
  611. }
  612. if (ovl_is_impure_dir(file)) {
  613. rdt.cache = ovl_cache_get_impure(&file->f_path);
  614. if (IS_ERR(rdt.cache))
  615. return PTR_ERR(rdt.cache);
  616. }
  617. err = iterate_dir(od->realfile, &rdt.ctx);
  618. ctx->pos = rdt.ctx.pos;
  619. return err;
  620. }
  621. static int ovl_iterate(struct file *file, struct dir_context *ctx)
  622. {
  623. struct ovl_dir_file *od = file->private_data;
  624. struct dentry *dentry = file->f_path.dentry;
  625. struct ovl_cache_entry *p;
  626. int err;
  627. if (!ctx->pos)
  628. ovl_dir_reset(file);
  629. if (od->is_real) {
  630. /*
  631. * If parent is merge, then need to adjust d_ino for '..', if
  632. * dir is impure then need to adjust d_ino for copied up
  633. * entries.
  634. */
  635. if (ovl_xino_bits(dentry->d_sb) ||
  636. (ovl_same_sb(dentry->d_sb) &&
  637. (ovl_is_impure_dir(file) ||
  638. OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
  639. return ovl_iterate_real(file, ctx);
  640. }
  641. return iterate_dir(od->realfile, ctx);
  642. }
  643. if (!od->cache) {
  644. struct ovl_dir_cache *cache;
  645. cache = ovl_cache_get(dentry);
  646. if (IS_ERR(cache))
  647. return PTR_ERR(cache);
  648. od->cache = cache;
  649. ovl_seek_cursor(od, ctx->pos);
  650. }
  651. while (od->cursor != &od->cache->entries) {
  652. p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
  653. if (!p->is_whiteout) {
  654. if (!p->ino) {
  655. err = ovl_cache_update_ino(&file->f_path, p);
  656. if (err)
  657. return err;
  658. }
  659. if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
  660. break;
  661. }
  662. od->cursor = p->l_node.next;
  663. ctx->pos++;
  664. }
  665. return 0;
  666. }
  667. static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
  668. {
  669. loff_t res;
  670. struct ovl_dir_file *od = file->private_data;
  671. inode_lock(file_inode(file));
  672. if (!file->f_pos)
  673. ovl_dir_reset(file);
  674. if (od->is_real) {
  675. res = vfs_llseek(od->realfile, offset, origin);
  676. file->f_pos = od->realfile->f_pos;
  677. } else {
  678. res = -EINVAL;
  679. switch (origin) {
  680. case SEEK_CUR:
  681. offset += file->f_pos;
  682. break;
  683. case SEEK_SET:
  684. break;
  685. default:
  686. goto out_unlock;
  687. }
  688. if (offset < 0)
  689. goto out_unlock;
  690. if (offset != file->f_pos) {
  691. file->f_pos = offset;
  692. if (od->cache)
  693. ovl_seek_cursor(od, offset);
  694. }
  695. res = offset;
  696. }
  697. out_unlock:
  698. inode_unlock(file_inode(file));
  699. return res;
  700. }
  701. static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
  702. int datasync)
  703. {
  704. struct ovl_dir_file *od = file->private_data;
  705. struct dentry *dentry = file->f_path.dentry;
  706. struct file *realfile = od->realfile;
  707. /* Nothing to sync for lower */
  708. if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
  709. return 0;
  710. /*
  711. * Need to check if we started out being a lower dir, but got copied up
  712. */
  713. if (!od->is_upper) {
  714. struct inode *inode = file_inode(file);
  715. realfile = READ_ONCE(od->upperfile);
  716. if (!realfile) {
  717. struct path upperpath;
  718. ovl_path_upper(dentry, &upperpath);
  719. realfile = ovl_path_open(&upperpath, O_RDONLY);
  720. inode_lock(inode);
  721. if (!od->upperfile) {
  722. if (IS_ERR(realfile)) {
  723. inode_unlock(inode);
  724. return PTR_ERR(realfile);
  725. }
  726. smp_store_release(&od->upperfile, realfile);
  727. } else {
  728. /* somebody has beaten us to it */
  729. if (!IS_ERR(realfile))
  730. fput(realfile);
  731. realfile = od->upperfile;
  732. }
  733. inode_unlock(inode);
  734. }
  735. }
  736. return vfs_fsync_range(realfile, start, end, datasync);
  737. }
  738. static int ovl_dir_release(struct inode *inode, struct file *file)
  739. {
  740. struct ovl_dir_file *od = file->private_data;
  741. if (od->cache) {
  742. inode_lock(inode);
  743. ovl_cache_put(od, file->f_path.dentry);
  744. inode_unlock(inode);
  745. }
  746. fput(od->realfile);
  747. if (od->upperfile)
  748. fput(od->upperfile);
  749. kfree(od);
  750. return 0;
  751. }
  752. static int ovl_dir_open(struct inode *inode, struct file *file)
  753. {
  754. struct path realpath;
  755. struct file *realfile;
  756. struct ovl_dir_file *od;
  757. enum ovl_path_type type;
  758. od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
  759. if (!od)
  760. return -ENOMEM;
  761. type = ovl_path_real(file->f_path.dentry, &realpath);
  762. realfile = ovl_path_open(&realpath, file->f_flags);
  763. if (IS_ERR(realfile)) {
  764. kfree(od);
  765. return PTR_ERR(realfile);
  766. }
  767. od->realfile = realfile;
  768. od->is_real = ovl_dir_is_real(file->f_path.dentry);
  769. od->is_upper = OVL_TYPE_UPPER(type);
  770. file->private_data = od;
  771. return 0;
  772. }
  773. const struct file_operations ovl_dir_operations = {
  774. .read = generic_read_dir,
  775. .open = ovl_dir_open,
  776. .iterate = ovl_iterate,
  777. .llseek = ovl_dir_llseek,
  778. .fsync = ovl_dir_fsync,
  779. .release = ovl_dir_release,
  780. };
  781. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  782. {
  783. int err;
  784. struct ovl_cache_entry *p, *n;
  785. struct rb_root root = RB_ROOT;
  786. const struct cred *old_cred;
  787. old_cred = ovl_override_creds(dentry->d_sb);
  788. err = ovl_dir_read_merged(dentry, list, &root);
  789. revert_creds(old_cred);
  790. if (err)
  791. return err;
  792. err = 0;
  793. list_for_each_entry_safe(p, n, list, l_node) {
  794. /*
  795. * Select whiteouts in upperdir, they should
  796. * be cleared when deleting this directory.
  797. */
  798. if (p->is_whiteout) {
  799. if (p->is_upper)
  800. continue;
  801. goto del_entry;
  802. }
  803. if (p->name[0] == '.') {
  804. if (p->len == 1)
  805. goto del_entry;
  806. if (p->len == 2 && p->name[1] == '.')
  807. goto del_entry;
  808. }
  809. err = -ENOTEMPTY;
  810. break;
  811. del_entry:
  812. list_del(&p->l_node);
  813. kfree(p);
  814. }
  815. return err;
  816. }
  817. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
  818. {
  819. struct ovl_cache_entry *p;
  820. inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
  821. list_for_each_entry(p, list, l_node) {
  822. struct dentry *dentry;
  823. if (WARN_ON(!p->is_whiteout || !p->is_upper))
  824. continue;
  825. dentry = lookup_one_len(p->name, upper, p->len);
  826. if (IS_ERR(dentry)) {
  827. pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
  828. upper->d_name.name, p->len, p->name,
  829. (int) PTR_ERR(dentry));
  830. continue;
  831. }
  832. if (dentry->d_inode)
  833. ovl_cleanup(upper->d_inode, dentry);
  834. dput(dentry);
  835. }
  836. inode_unlock(upper->d_inode);
  837. }
  838. static int ovl_check_d_type(struct dir_context *ctx, const char *name,
  839. int namelen, loff_t offset, u64 ino,
  840. unsigned int d_type)
  841. {
  842. struct ovl_readdir_data *rdd =
  843. container_of(ctx, struct ovl_readdir_data, ctx);
  844. /* Even if d_type is not supported, DT_DIR is returned for . and .. */
  845. if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
  846. return 0;
  847. if (d_type != DT_UNKNOWN)
  848. rdd->d_type_supported = true;
  849. return 0;
  850. }
  851. /*
  852. * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
  853. * if error is encountered.
  854. */
  855. int ovl_check_d_type_supported(struct path *realpath)
  856. {
  857. int err;
  858. struct ovl_readdir_data rdd = {
  859. .ctx.actor = ovl_check_d_type,
  860. .d_type_supported = false,
  861. };
  862. err = ovl_dir_read(realpath, &rdd);
  863. if (err)
  864. return err;
  865. return rdd.d_type_supported;
  866. }
  867. static void ovl_workdir_cleanup_recurse(struct path *path, int level)
  868. {
  869. int err;
  870. struct inode *dir = path->dentry->d_inode;
  871. LIST_HEAD(list);
  872. struct rb_root root = RB_ROOT;
  873. struct ovl_cache_entry *p;
  874. struct ovl_readdir_data rdd = {
  875. .ctx.actor = ovl_fill_merge,
  876. .dentry = NULL,
  877. .list = &list,
  878. .root = &root,
  879. .is_lowest = false,
  880. };
  881. err = ovl_dir_read(path, &rdd);
  882. if (err)
  883. goto out;
  884. inode_lock_nested(dir, I_MUTEX_PARENT);
  885. list_for_each_entry(p, &list, l_node) {
  886. struct dentry *dentry;
  887. if (p->name[0] == '.') {
  888. if (p->len == 1)
  889. continue;
  890. if (p->len == 2 && p->name[1] == '.')
  891. continue;
  892. }
  893. dentry = lookup_one_len(p->name, path->dentry, p->len);
  894. if (IS_ERR(dentry))
  895. continue;
  896. if (dentry->d_inode)
  897. ovl_workdir_cleanup(dir, path->mnt, dentry, level);
  898. dput(dentry);
  899. }
  900. inode_unlock(dir);
  901. out:
  902. ovl_cache_free(&list);
  903. }
  904. void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  905. struct dentry *dentry, int level)
  906. {
  907. int err;
  908. if (!d_is_dir(dentry) || level > 1) {
  909. ovl_cleanup(dir, dentry);
  910. return;
  911. }
  912. err = ovl_do_rmdir(dir, dentry);
  913. if (err) {
  914. struct path path = { .mnt = mnt, .dentry = dentry };
  915. inode_unlock(dir);
  916. ovl_workdir_cleanup_recurse(&path, level + 1);
  917. inode_lock_nested(dir, I_MUTEX_PARENT);
  918. ovl_cleanup(dir, dentry);
  919. }
  920. }
  921. int ovl_indexdir_cleanup(struct ovl_fs *ofs)
  922. {
  923. int err;
  924. struct dentry *indexdir = ofs->indexdir;
  925. struct dentry *index = NULL;
  926. struct inode *dir = indexdir->d_inode;
  927. struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir };
  928. LIST_HEAD(list);
  929. struct rb_root root = RB_ROOT;
  930. struct ovl_cache_entry *p;
  931. struct ovl_readdir_data rdd = {
  932. .ctx.actor = ovl_fill_merge,
  933. .dentry = NULL,
  934. .list = &list,
  935. .root = &root,
  936. .is_lowest = false,
  937. };
  938. err = ovl_dir_read(&path, &rdd);
  939. if (err)
  940. goto out;
  941. inode_lock_nested(dir, I_MUTEX_PARENT);
  942. list_for_each_entry(p, &list, l_node) {
  943. if (p->name[0] == '.') {
  944. if (p->len == 1)
  945. continue;
  946. if (p->len == 2 && p->name[1] == '.')
  947. continue;
  948. }
  949. index = lookup_one_len(p->name, indexdir, p->len);
  950. if (IS_ERR(index)) {
  951. err = PTR_ERR(index);
  952. index = NULL;
  953. break;
  954. }
  955. err = ovl_verify_index(ofs, index);
  956. if (!err) {
  957. goto next;
  958. } else if (err == -ESTALE) {
  959. /* Cleanup stale index entries */
  960. err = ovl_cleanup(dir, index);
  961. } else if (err != -ENOENT) {
  962. /*
  963. * Abort mount to avoid corrupting the index if
  964. * an incompatible index entry was found or on out
  965. * of memory.
  966. */
  967. break;
  968. } else if (ofs->config.nfs_export) {
  969. /*
  970. * Whiteout orphan index to block future open by
  971. * handle after overlay nlink dropped to zero.
  972. */
  973. err = ovl_cleanup_and_whiteout(indexdir, dir, index);
  974. } else {
  975. /* Cleanup orphan index entries */
  976. err = ovl_cleanup(dir, index);
  977. }
  978. if (err)
  979. break;
  980. next:
  981. dput(index);
  982. index = NULL;
  983. }
  984. dput(index);
  985. inode_unlock(dir);
  986. out:
  987. ovl_cache_free(&list);
  988. if (err)
  989. pr_err("overlayfs: failed index dir cleanup (%i)\n", err);
  990. return err;
  991. }