readdir.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
  435. ino = stat.ino;
  436. } else if (xinobits && !OVL_TYPE_UPPER(type)) {
  437. ino = ovl_remap_lower_ino(ino, xinobits,
  438. ovl_layer_lower(this)->fsid,
  439. p->name, p->len);
  440. }
  441. out:
  442. p->ino = ino;
  443. dput(this);
  444. return err;
  445. fail:
  446. pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n",
  447. p->name, err);
  448. goto out;
  449. }
  450. static int ovl_fill_plain(struct dir_context *ctx, const char *name,
  451. int namelen, loff_t offset, u64 ino,
  452. unsigned int d_type)
  453. {
  454. struct ovl_cache_entry *p;
  455. struct ovl_readdir_data *rdd =
  456. container_of(ctx, struct ovl_readdir_data, ctx);
  457. rdd->count++;
  458. p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
  459. if (p == NULL) {
  460. rdd->err = -ENOMEM;
  461. return -ENOMEM;
  462. }
  463. list_add_tail(&p->l_node, rdd->list);
  464. return 0;
  465. }
  466. static int ovl_dir_read_impure(struct path *path, struct list_head *list,
  467. struct rb_root *root)
  468. {
  469. int err;
  470. struct path realpath;
  471. struct ovl_cache_entry *p, *n;
  472. struct ovl_readdir_data rdd = {
  473. .ctx.actor = ovl_fill_plain,
  474. .list = list,
  475. .root = root,
  476. };
  477. INIT_LIST_HEAD(list);
  478. *root = RB_ROOT;
  479. ovl_path_upper(path->dentry, &realpath);
  480. err = ovl_dir_read(&realpath, &rdd);
  481. if (err)
  482. return err;
  483. list_for_each_entry_safe(p, n, list, l_node) {
  484. if (strcmp(p->name, ".") != 0 &&
  485. strcmp(p->name, "..") != 0) {
  486. err = ovl_cache_update_ino(path, p);
  487. if (err)
  488. return err;
  489. }
  490. if (p->ino == p->real_ino) {
  491. list_del(&p->l_node);
  492. kfree(p);
  493. } else {
  494. struct rb_node **newp = &root->rb_node;
  495. struct rb_node *parent = NULL;
  496. if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
  497. &newp, &parent)))
  498. return -EIO;
  499. rb_link_node(&p->node, parent, newp);
  500. rb_insert_color(&p->node, root);
  501. }
  502. }
  503. return 0;
  504. }
  505. static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
  506. {
  507. int res;
  508. struct dentry *dentry = path->dentry;
  509. struct ovl_dir_cache *cache;
  510. cache = ovl_dir_cache(d_inode(dentry));
  511. if (cache && ovl_dentry_version_get(dentry) == cache->version)
  512. return cache;
  513. /* Impure cache is not refcounted, free it here */
  514. ovl_dir_cache_free(d_inode(dentry));
  515. ovl_set_dir_cache(d_inode(dentry), NULL);
  516. cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
  517. if (!cache)
  518. return ERR_PTR(-ENOMEM);
  519. res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
  520. if (res) {
  521. ovl_cache_free(&cache->entries);
  522. kfree(cache);
  523. return ERR_PTR(res);
  524. }
  525. if (list_empty(&cache->entries)) {
  526. /*
  527. * A good opportunity to get rid of an unneeded "impure" flag.
  528. * Removing the "impure" xattr is best effort.
  529. */
  530. if (!ovl_want_write(dentry)) {
  531. ovl_do_removexattr(ovl_dentry_upper(dentry),
  532. OVL_XATTR_IMPURE);
  533. ovl_drop_write(dentry);
  534. }
  535. ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
  536. kfree(cache);
  537. return NULL;
  538. }
  539. cache->version = ovl_dentry_version_get(dentry);
  540. ovl_set_dir_cache(d_inode(dentry), cache);
  541. return cache;
  542. }
  543. struct ovl_readdir_translate {
  544. struct dir_context *orig_ctx;
  545. struct ovl_dir_cache *cache;
  546. struct dir_context ctx;
  547. u64 parent_ino;
  548. int fsid;
  549. int xinobits;
  550. };
  551. static int ovl_fill_real(struct dir_context *ctx, const char *name,
  552. int namelen, loff_t offset, u64 ino,
  553. unsigned int d_type)
  554. {
  555. struct ovl_readdir_translate *rdt =
  556. container_of(ctx, struct ovl_readdir_translate, ctx);
  557. struct dir_context *orig_ctx = rdt->orig_ctx;
  558. if (rdt->parent_ino && strcmp(name, "..") == 0) {
  559. ino = rdt->parent_ino;
  560. } else if (rdt->cache) {
  561. struct ovl_cache_entry *p;
  562. p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
  563. if (p)
  564. ino = p->ino;
  565. } else if (rdt->xinobits) {
  566. ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
  567. name, namelen);
  568. }
  569. return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
  570. }
  571. static bool ovl_is_impure_dir(struct file *file)
  572. {
  573. struct ovl_dir_file *od = file->private_data;
  574. struct inode *dir = d_inode(file->f_path.dentry);
  575. /*
  576. * Only upper dir can be impure, but if we are in the middle of
  577. * iterating a lower real dir, dir could be copied up and marked
  578. * impure. We only want the impure cache if we started iterating
  579. * a real upper dir to begin with.
  580. */
  581. return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
  582. }
  583. static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
  584. {
  585. int err;
  586. struct ovl_dir_file *od = file->private_data;
  587. struct dentry *dir = file->f_path.dentry;
  588. struct ovl_layer *lower_layer = ovl_layer_lower(dir);
  589. struct ovl_readdir_translate rdt = {
  590. .ctx.actor = ovl_fill_real,
  591. .orig_ctx = ctx,
  592. .xinobits = ovl_xino_bits(dir->d_sb),
  593. };
  594. if (rdt.xinobits && lower_layer)
  595. rdt.fsid = lower_layer->fsid;
  596. if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
  597. struct kstat stat;
  598. struct path statpath = file->f_path;
  599. statpath.dentry = dir->d_parent;
  600. err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
  601. if (err)
  602. return err;
  603. WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
  604. rdt.parent_ino = stat.ino;
  605. }
  606. if (ovl_is_impure_dir(file)) {
  607. rdt.cache = ovl_cache_get_impure(&file->f_path);
  608. if (IS_ERR(rdt.cache))
  609. return PTR_ERR(rdt.cache);
  610. }
  611. err = iterate_dir(od->realfile, &rdt.ctx);
  612. ctx->pos = rdt.ctx.pos;
  613. return err;
  614. }
  615. static int ovl_iterate(struct file *file, struct dir_context *ctx)
  616. {
  617. struct ovl_dir_file *od = file->private_data;
  618. struct dentry *dentry = file->f_path.dentry;
  619. struct ovl_cache_entry *p;
  620. int err;
  621. if (!ctx->pos)
  622. ovl_dir_reset(file);
  623. if (od->is_real) {
  624. /*
  625. * If parent is merge, then need to adjust d_ino for '..', if
  626. * dir is impure then need to adjust d_ino for copied up
  627. * entries.
  628. */
  629. if (ovl_xino_bits(dentry->d_sb) ||
  630. (ovl_same_sb(dentry->d_sb) &&
  631. (ovl_is_impure_dir(file) ||
  632. OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
  633. return ovl_iterate_real(file, ctx);
  634. }
  635. return iterate_dir(od->realfile, ctx);
  636. }
  637. if (!od->cache) {
  638. struct ovl_dir_cache *cache;
  639. cache = ovl_cache_get(dentry);
  640. if (IS_ERR(cache))
  641. return PTR_ERR(cache);
  642. od->cache = cache;
  643. ovl_seek_cursor(od, ctx->pos);
  644. }
  645. while (od->cursor != &od->cache->entries) {
  646. p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
  647. if (!p->is_whiteout) {
  648. if (!p->ino) {
  649. err = ovl_cache_update_ino(&file->f_path, p);
  650. if (err)
  651. return err;
  652. }
  653. if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
  654. break;
  655. }
  656. od->cursor = p->l_node.next;
  657. ctx->pos++;
  658. }
  659. return 0;
  660. }
  661. static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
  662. {
  663. loff_t res;
  664. struct ovl_dir_file *od = file->private_data;
  665. inode_lock(file_inode(file));
  666. if (!file->f_pos)
  667. ovl_dir_reset(file);
  668. if (od->is_real) {
  669. res = vfs_llseek(od->realfile, offset, origin);
  670. file->f_pos = od->realfile->f_pos;
  671. } else {
  672. res = -EINVAL;
  673. switch (origin) {
  674. case SEEK_CUR:
  675. offset += file->f_pos;
  676. break;
  677. case SEEK_SET:
  678. break;
  679. default:
  680. goto out_unlock;
  681. }
  682. if (offset < 0)
  683. goto out_unlock;
  684. if (offset != file->f_pos) {
  685. file->f_pos = offset;
  686. if (od->cache)
  687. ovl_seek_cursor(od, offset);
  688. }
  689. res = offset;
  690. }
  691. out_unlock:
  692. inode_unlock(file_inode(file));
  693. return res;
  694. }
  695. static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
  696. int datasync)
  697. {
  698. struct ovl_dir_file *od = file->private_data;
  699. struct dentry *dentry = file->f_path.dentry;
  700. struct file *realfile = od->realfile;
  701. /* Nothing to sync for lower */
  702. if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
  703. return 0;
  704. /*
  705. * Need to check if we started out being a lower dir, but got copied up
  706. */
  707. if (!od->is_upper) {
  708. struct inode *inode = file_inode(file);
  709. realfile = READ_ONCE(od->upperfile);
  710. if (!realfile) {
  711. struct path upperpath;
  712. ovl_path_upper(dentry, &upperpath);
  713. realfile = ovl_path_open(&upperpath, O_RDONLY);
  714. inode_lock(inode);
  715. if (!od->upperfile) {
  716. if (IS_ERR(realfile)) {
  717. inode_unlock(inode);
  718. return PTR_ERR(realfile);
  719. }
  720. smp_store_release(&od->upperfile, realfile);
  721. } else {
  722. /* somebody has beaten us to it */
  723. if (!IS_ERR(realfile))
  724. fput(realfile);
  725. realfile = od->upperfile;
  726. }
  727. inode_unlock(inode);
  728. }
  729. }
  730. return vfs_fsync_range(realfile, start, end, datasync);
  731. }
  732. static int ovl_dir_release(struct inode *inode, struct file *file)
  733. {
  734. struct ovl_dir_file *od = file->private_data;
  735. if (od->cache) {
  736. inode_lock(inode);
  737. ovl_cache_put(od, file->f_path.dentry);
  738. inode_unlock(inode);
  739. }
  740. fput(od->realfile);
  741. if (od->upperfile)
  742. fput(od->upperfile);
  743. kfree(od);
  744. return 0;
  745. }
  746. static int ovl_dir_open(struct inode *inode, struct file *file)
  747. {
  748. struct path realpath;
  749. struct file *realfile;
  750. struct ovl_dir_file *od;
  751. enum ovl_path_type type;
  752. od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
  753. if (!od)
  754. return -ENOMEM;
  755. type = ovl_path_real(file->f_path.dentry, &realpath);
  756. realfile = ovl_path_open(&realpath, file->f_flags);
  757. if (IS_ERR(realfile)) {
  758. kfree(od);
  759. return PTR_ERR(realfile);
  760. }
  761. od->realfile = realfile;
  762. od->is_real = ovl_dir_is_real(file->f_path.dentry);
  763. od->is_upper = OVL_TYPE_UPPER(type);
  764. file->private_data = od;
  765. return 0;
  766. }
  767. const struct file_operations ovl_dir_operations = {
  768. .read = generic_read_dir,
  769. .open = ovl_dir_open,
  770. .iterate = ovl_iterate,
  771. .llseek = ovl_dir_llseek,
  772. .fsync = ovl_dir_fsync,
  773. .release = ovl_dir_release,
  774. };
  775. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  776. {
  777. int err;
  778. struct ovl_cache_entry *p, *n;
  779. struct rb_root root = RB_ROOT;
  780. const struct cred *old_cred;
  781. old_cred = ovl_override_creds(dentry->d_sb);
  782. err = ovl_dir_read_merged(dentry, list, &root);
  783. revert_creds(old_cred);
  784. if (err)
  785. return err;
  786. err = 0;
  787. list_for_each_entry_safe(p, n, list, l_node) {
  788. /*
  789. * Select whiteouts in upperdir, they should
  790. * be cleared when deleting this directory.
  791. */
  792. if (p->is_whiteout) {
  793. if (p->is_upper)
  794. continue;
  795. goto del_entry;
  796. }
  797. if (p->name[0] == '.') {
  798. if (p->len == 1)
  799. goto del_entry;
  800. if (p->len == 2 && p->name[1] == '.')
  801. goto del_entry;
  802. }
  803. err = -ENOTEMPTY;
  804. break;
  805. del_entry:
  806. list_del(&p->l_node);
  807. kfree(p);
  808. }
  809. return err;
  810. }
  811. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
  812. {
  813. struct ovl_cache_entry *p;
  814. inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
  815. list_for_each_entry(p, list, l_node) {
  816. struct dentry *dentry;
  817. if (WARN_ON(!p->is_whiteout || !p->is_upper))
  818. continue;
  819. dentry = lookup_one_len(p->name, upper, p->len);
  820. if (IS_ERR(dentry)) {
  821. pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
  822. upper->d_name.name, p->len, p->name,
  823. (int) PTR_ERR(dentry));
  824. continue;
  825. }
  826. if (dentry->d_inode)
  827. ovl_cleanup(upper->d_inode, dentry);
  828. dput(dentry);
  829. }
  830. inode_unlock(upper->d_inode);
  831. }
  832. static int ovl_check_d_type(struct dir_context *ctx, const char *name,
  833. int namelen, loff_t offset, u64 ino,
  834. unsigned int d_type)
  835. {
  836. struct ovl_readdir_data *rdd =
  837. container_of(ctx, struct ovl_readdir_data, ctx);
  838. /* Even if d_type is not supported, DT_DIR is returned for . and .. */
  839. if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
  840. return 0;
  841. if (d_type != DT_UNKNOWN)
  842. rdd->d_type_supported = true;
  843. return 0;
  844. }
  845. /*
  846. * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
  847. * if error is encountered.
  848. */
  849. int ovl_check_d_type_supported(struct path *realpath)
  850. {
  851. int err;
  852. struct ovl_readdir_data rdd = {
  853. .ctx.actor = ovl_check_d_type,
  854. .d_type_supported = false,
  855. };
  856. err = ovl_dir_read(realpath, &rdd);
  857. if (err)
  858. return err;
  859. return rdd.d_type_supported;
  860. }
  861. static void ovl_workdir_cleanup_recurse(struct path *path, int level)
  862. {
  863. int err;
  864. struct inode *dir = path->dentry->d_inode;
  865. LIST_HEAD(list);
  866. struct rb_root root = RB_ROOT;
  867. struct ovl_cache_entry *p;
  868. struct ovl_readdir_data rdd = {
  869. .ctx.actor = ovl_fill_merge,
  870. .dentry = NULL,
  871. .list = &list,
  872. .root = &root,
  873. .is_lowest = false,
  874. };
  875. err = ovl_dir_read(path, &rdd);
  876. if (err)
  877. goto out;
  878. inode_lock_nested(dir, I_MUTEX_PARENT);
  879. list_for_each_entry(p, &list, l_node) {
  880. struct dentry *dentry;
  881. if (p->name[0] == '.') {
  882. if (p->len == 1)
  883. continue;
  884. if (p->len == 2 && p->name[1] == '.')
  885. continue;
  886. }
  887. dentry = lookup_one_len(p->name, path->dentry, p->len);
  888. if (IS_ERR(dentry))
  889. continue;
  890. if (dentry->d_inode)
  891. ovl_workdir_cleanup(dir, path->mnt, dentry, level);
  892. dput(dentry);
  893. }
  894. inode_unlock(dir);
  895. out:
  896. ovl_cache_free(&list);
  897. }
  898. void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  899. struct dentry *dentry, int level)
  900. {
  901. int err;
  902. if (!d_is_dir(dentry) || level > 1) {
  903. ovl_cleanup(dir, dentry);
  904. return;
  905. }
  906. err = ovl_do_rmdir(dir, dentry);
  907. if (err) {
  908. struct path path = { .mnt = mnt, .dentry = dentry };
  909. inode_unlock(dir);
  910. ovl_workdir_cleanup_recurse(&path, level + 1);
  911. inode_lock_nested(dir, I_MUTEX_PARENT);
  912. ovl_cleanup(dir, dentry);
  913. }
  914. }
  915. int ovl_indexdir_cleanup(struct ovl_fs *ofs)
  916. {
  917. int err;
  918. struct dentry *indexdir = ofs->indexdir;
  919. struct dentry *index = NULL;
  920. struct inode *dir = indexdir->d_inode;
  921. struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir };
  922. LIST_HEAD(list);
  923. struct rb_root root = RB_ROOT;
  924. struct ovl_cache_entry *p;
  925. struct ovl_readdir_data rdd = {
  926. .ctx.actor = ovl_fill_merge,
  927. .dentry = NULL,
  928. .list = &list,
  929. .root = &root,
  930. .is_lowest = false,
  931. };
  932. err = ovl_dir_read(&path, &rdd);
  933. if (err)
  934. goto out;
  935. inode_lock_nested(dir, I_MUTEX_PARENT);
  936. list_for_each_entry(p, &list, l_node) {
  937. if (p->name[0] == '.') {
  938. if (p->len == 1)
  939. continue;
  940. if (p->len == 2 && p->name[1] == '.')
  941. continue;
  942. }
  943. index = lookup_one_len(p->name, indexdir, p->len);
  944. if (IS_ERR(index)) {
  945. err = PTR_ERR(index);
  946. index = NULL;
  947. break;
  948. }
  949. err = ovl_verify_index(ofs, index);
  950. if (!err) {
  951. goto next;
  952. } else if (err == -ESTALE) {
  953. /* Cleanup stale index entries */
  954. err = ovl_cleanup(dir, index);
  955. } else if (err != -ENOENT) {
  956. /*
  957. * Abort mount to avoid corrupting the index if
  958. * an incompatible index entry was found or on out
  959. * of memory.
  960. */
  961. break;
  962. } else if (ofs->config.nfs_export) {
  963. /*
  964. * Whiteout orphan index to block future open by
  965. * handle after overlay nlink dropped to zero.
  966. */
  967. err = ovl_cleanup_and_whiteout(indexdir, dir, index);
  968. } else {
  969. /* Cleanup orphan index entries */
  970. err = ovl_cleanup(dir, index);
  971. }
  972. if (err)
  973. break;
  974. next:
  975. dput(index);
  976. index = NULL;
  977. }
  978. dput(index);
  979. inode_unlock(dir);
  980. out:
  981. ovl_cache_free(&list);
  982. if (err)
  983. pr_err("overlayfs: failed index dir cleanup (%i)\n", err);
  984. return err;
  985. }