readdir.c 23 KB

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