readdir.c 24 KB

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