readdir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 "overlayfs.h"
  18. struct ovl_cache_entry {
  19. unsigned int len;
  20. unsigned int type;
  21. u64 ino;
  22. struct list_head l_node;
  23. struct rb_node node;
  24. struct ovl_cache_entry *next_maybe_whiteout;
  25. bool is_whiteout;
  26. char name[];
  27. };
  28. struct ovl_dir_cache {
  29. long refcount;
  30. u64 version;
  31. struct list_head entries;
  32. };
  33. struct ovl_readdir_data {
  34. struct dir_context ctx;
  35. struct dentry *dentry;
  36. bool is_lowest;
  37. struct rb_root root;
  38. struct list_head *list;
  39. struct list_head middle;
  40. struct ovl_cache_entry *first_maybe_whiteout;
  41. int count;
  42. int err;
  43. bool d_type_supported;
  44. };
  45. struct ovl_dir_file {
  46. bool is_real;
  47. bool is_upper;
  48. struct ovl_dir_cache *cache;
  49. struct list_head *cursor;
  50. struct file *realfile;
  51. struct file *upperfile;
  52. };
  53. static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
  54. {
  55. return container_of(n, struct ovl_cache_entry, node);
  56. }
  57. static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
  58. const char *name, int len)
  59. {
  60. struct rb_node *node = root->rb_node;
  61. int cmp;
  62. while (node) {
  63. struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
  64. cmp = strncmp(name, p->name, len);
  65. if (cmp > 0)
  66. node = p->node.rb_right;
  67. else if (cmp < 0 || len < p->len)
  68. node = p->node.rb_left;
  69. else
  70. return p;
  71. }
  72. return NULL;
  73. }
  74. static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
  75. const char *name, int len,
  76. u64 ino, unsigned int d_type)
  77. {
  78. struct ovl_cache_entry *p;
  79. size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
  80. p = kmalloc(size, GFP_KERNEL);
  81. if (!p)
  82. return NULL;
  83. memcpy(p->name, name, len);
  84. p->name[len] = '\0';
  85. p->len = len;
  86. p->type = d_type;
  87. p->ino = ino;
  88. p->is_whiteout = false;
  89. if (d_type == DT_CHR) {
  90. p->next_maybe_whiteout = rdd->first_maybe_whiteout;
  91. rdd->first_maybe_whiteout = p;
  92. }
  93. return p;
  94. }
  95. static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
  96. const char *name, int len, u64 ino,
  97. unsigned int d_type)
  98. {
  99. struct rb_node **newp = &rdd->root.rb_node;
  100. struct rb_node *parent = NULL;
  101. struct ovl_cache_entry *p;
  102. while (*newp) {
  103. int cmp;
  104. struct ovl_cache_entry *tmp;
  105. parent = *newp;
  106. tmp = ovl_cache_entry_from_node(*newp);
  107. cmp = strncmp(name, tmp->name, len);
  108. if (cmp > 0)
  109. newp = &tmp->node.rb_right;
  110. else if (cmp < 0 || len < tmp->len)
  111. newp = &tmp->node.rb_left;
  112. else
  113. return 0;
  114. }
  115. p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
  116. if (p == NULL)
  117. return -ENOMEM;
  118. list_add_tail(&p->l_node, rdd->list);
  119. rb_link_node(&p->node, parent, newp);
  120. rb_insert_color(&p->node, &rdd->root);
  121. return 0;
  122. }
  123. static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
  124. const char *name, int namelen,
  125. loff_t offset, u64 ino, unsigned int d_type)
  126. {
  127. struct ovl_cache_entry *p;
  128. p = ovl_cache_entry_find(&rdd->root, name, namelen);
  129. if (p) {
  130. list_move_tail(&p->l_node, &rdd->middle);
  131. } else {
  132. p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
  133. if (p == NULL)
  134. rdd->err = -ENOMEM;
  135. else
  136. list_add_tail(&p->l_node, &rdd->middle);
  137. }
  138. return rdd->err;
  139. }
  140. void ovl_cache_free(struct list_head *list)
  141. {
  142. struct ovl_cache_entry *p;
  143. struct ovl_cache_entry *n;
  144. list_for_each_entry_safe(p, n, list, l_node)
  145. kfree(p);
  146. INIT_LIST_HEAD(list);
  147. }
  148. static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
  149. {
  150. struct ovl_dir_cache *cache = od->cache;
  151. WARN_ON(cache->refcount <= 0);
  152. cache->refcount--;
  153. if (!cache->refcount) {
  154. if (ovl_dir_cache(dentry) == cache)
  155. ovl_set_dir_cache(dentry, NULL);
  156. ovl_cache_free(&cache->entries);
  157. kfree(cache);
  158. }
  159. }
  160. static int ovl_fill_merge(struct dir_context *ctx, const char *name,
  161. int namelen, loff_t offset, u64 ino,
  162. unsigned int d_type)
  163. {
  164. struct ovl_readdir_data *rdd =
  165. container_of(ctx, struct ovl_readdir_data, ctx);
  166. rdd->count++;
  167. if (!rdd->is_lowest)
  168. return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
  169. else
  170. return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
  171. }
  172. static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
  173. {
  174. int err;
  175. struct ovl_cache_entry *p;
  176. struct dentry *dentry;
  177. const struct cred *old_cred;
  178. old_cred = ovl_override_creds(rdd->dentry->d_sb);
  179. err = down_write_killable(&dir->d_inode->i_rwsem);
  180. if (!err) {
  181. while (rdd->first_maybe_whiteout) {
  182. p = rdd->first_maybe_whiteout;
  183. rdd->first_maybe_whiteout = p->next_maybe_whiteout;
  184. dentry = lookup_one_len(p->name, dir, p->len);
  185. if (!IS_ERR(dentry)) {
  186. p->is_whiteout = ovl_is_whiteout(dentry);
  187. dput(dentry);
  188. }
  189. }
  190. inode_unlock(dir->d_inode);
  191. }
  192. revert_creds(old_cred);
  193. return err;
  194. }
  195. static inline int ovl_dir_read(struct path *realpath,
  196. struct ovl_readdir_data *rdd)
  197. {
  198. struct file *realfile;
  199. int err;
  200. realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
  201. if (IS_ERR(realfile))
  202. return PTR_ERR(realfile);
  203. rdd->first_maybe_whiteout = NULL;
  204. rdd->ctx.pos = 0;
  205. do {
  206. rdd->count = 0;
  207. rdd->err = 0;
  208. err = iterate_dir(realfile, &rdd->ctx);
  209. if (err >= 0)
  210. err = rdd->err;
  211. } while (!err && rdd->count);
  212. if (!err && rdd->first_maybe_whiteout && rdd->dentry)
  213. err = ovl_check_whiteouts(realpath->dentry, rdd);
  214. fput(realfile);
  215. return err;
  216. }
  217. static void ovl_dir_reset(struct file *file)
  218. {
  219. struct ovl_dir_file *od = file->private_data;
  220. struct ovl_dir_cache *cache = od->cache;
  221. struct dentry *dentry = file->f_path.dentry;
  222. enum ovl_path_type type = ovl_path_type(dentry);
  223. if (cache && ovl_dentry_version_get(dentry) != cache->version) {
  224. ovl_cache_put(od, dentry);
  225. od->cache = NULL;
  226. od->cursor = NULL;
  227. }
  228. WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
  229. if (od->is_real && OVL_TYPE_MERGE(type))
  230. od->is_real = false;
  231. }
  232. static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
  233. {
  234. int err;
  235. struct path realpath;
  236. struct ovl_readdir_data rdd = {
  237. .ctx.actor = ovl_fill_merge,
  238. .dentry = dentry,
  239. .list = list,
  240. .root = RB_ROOT,
  241. .is_lowest = false,
  242. };
  243. int idx, next;
  244. for (idx = 0; idx != -1; idx = next) {
  245. next = ovl_path_next(idx, dentry, &realpath);
  246. if (next != -1) {
  247. err = ovl_dir_read(&realpath, &rdd);
  248. if (err)
  249. break;
  250. } else {
  251. /*
  252. * Insert lowest layer entries before upper ones, this
  253. * allows offsets to be reasonably constant
  254. */
  255. list_add(&rdd.middle, rdd.list);
  256. rdd.is_lowest = true;
  257. err = ovl_dir_read(&realpath, &rdd);
  258. list_del(&rdd.middle);
  259. }
  260. }
  261. return err;
  262. }
  263. static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
  264. {
  265. struct list_head *p;
  266. loff_t off = 0;
  267. list_for_each(p, &od->cache->entries) {
  268. if (off >= pos)
  269. break;
  270. off++;
  271. }
  272. /* Cursor is safe since the cache is stable */
  273. od->cursor = p;
  274. }
  275. static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
  276. {
  277. int res;
  278. struct ovl_dir_cache *cache;
  279. cache = ovl_dir_cache(dentry);
  280. if (cache && ovl_dentry_version_get(dentry) == cache->version) {
  281. cache->refcount++;
  282. return cache;
  283. }
  284. ovl_set_dir_cache(dentry, NULL);
  285. cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
  286. if (!cache)
  287. return ERR_PTR(-ENOMEM);
  288. cache->refcount = 1;
  289. INIT_LIST_HEAD(&cache->entries);
  290. res = ovl_dir_read_merged(dentry, &cache->entries);
  291. if (res) {
  292. ovl_cache_free(&cache->entries);
  293. kfree(cache);
  294. return ERR_PTR(res);
  295. }
  296. cache->version = ovl_dentry_version_get(dentry);
  297. ovl_set_dir_cache(dentry, cache);
  298. return cache;
  299. }
  300. static int ovl_iterate(struct file *file, struct dir_context *ctx)
  301. {
  302. struct ovl_dir_file *od = file->private_data;
  303. struct dentry *dentry = file->f_path.dentry;
  304. struct ovl_cache_entry *p;
  305. if (!ctx->pos)
  306. ovl_dir_reset(file);
  307. if (od->is_real)
  308. return iterate_dir(od->realfile, ctx);
  309. if (!od->cache) {
  310. struct ovl_dir_cache *cache;
  311. cache = ovl_cache_get(dentry);
  312. if (IS_ERR(cache))
  313. return PTR_ERR(cache);
  314. od->cache = cache;
  315. ovl_seek_cursor(od, ctx->pos);
  316. }
  317. while (od->cursor != &od->cache->entries) {
  318. p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
  319. if (!p->is_whiteout)
  320. if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
  321. break;
  322. od->cursor = p->l_node.next;
  323. ctx->pos++;
  324. }
  325. return 0;
  326. }
  327. static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
  328. {
  329. loff_t res;
  330. struct ovl_dir_file *od = file->private_data;
  331. inode_lock(file_inode(file));
  332. if (!file->f_pos)
  333. ovl_dir_reset(file);
  334. if (od->is_real) {
  335. res = vfs_llseek(od->realfile, offset, origin);
  336. file->f_pos = od->realfile->f_pos;
  337. } else {
  338. res = -EINVAL;
  339. switch (origin) {
  340. case SEEK_CUR:
  341. offset += file->f_pos;
  342. break;
  343. case SEEK_SET:
  344. break;
  345. default:
  346. goto out_unlock;
  347. }
  348. if (offset < 0)
  349. goto out_unlock;
  350. if (offset != file->f_pos) {
  351. file->f_pos = offset;
  352. if (od->cache)
  353. ovl_seek_cursor(od, offset);
  354. }
  355. res = offset;
  356. }
  357. out_unlock:
  358. inode_unlock(file_inode(file));
  359. return res;
  360. }
  361. static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
  362. int datasync)
  363. {
  364. struct ovl_dir_file *od = file->private_data;
  365. struct dentry *dentry = file->f_path.dentry;
  366. struct file *realfile = od->realfile;
  367. /*
  368. * Need to check if we started out being a lower dir, but got copied up
  369. */
  370. if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
  371. struct inode *inode = file_inode(file);
  372. realfile = lockless_dereference(od->upperfile);
  373. if (!realfile) {
  374. struct path upperpath;
  375. ovl_path_upper(dentry, &upperpath);
  376. realfile = ovl_path_open(&upperpath, O_RDONLY);
  377. smp_mb__before_spinlock();
  378. inode_lock(inode);
  379. if (!od->upperfile) {
  380. if (IS_ERR(realfile)) {
  381. inode_unlock(inode);
  382. return PTR_ERR(realfile);
  383. }
  384. od->upperfile = realfile;
  385. } else {
  386. /* somebody has beaten us to it */
  387. if (!IS_ERR(realfile))
  388. fput(realfile);
  389. realfile = od->upperfile;
  390. }
  391. inode_unlock(inode);
  392. }
  393. }
  394. return vfs_fsync_range(realfile, start, end, datasync);
  395. }
  396. static int ovl_dir_release(struct inode *inode, struct file *file)
  397. {
  398. struct ovl_dir_file *od = file->private_data;
  399. if (od->cache) {
  400. inode_lock(inode);
  401. ovl_cache_put(od, file->f_path.dentry);
  402. inode_unlock(inode);
  403. }
  404. fput(od->realfile);
  405. if (od->upperfile)
  406. fput(od->upperfile);
  407. kfree(od);
  408. return 0;
  409. }
  410. static int ovl_dir_open(struct inode *inode, struct file *file)
  411. {
  412. struct path realpath;
  413. struct file *realfile;
  414. struct ovl_dir_file *od;
  415. enum ovl_path_type type;
  416. od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
  417. if (!od)
  418. return -ENOMEM;
  419. type = ovl_path_real(file->f_path.dentry, &realpath);
  420. realfile = ovl_path_open(&realpath, file->f_flags);
  421. if (IS_ERR(realfile)) {
  422. kfree(od);
  423. return PTR_ERR(realfile);
  424. }
  425. od->realfile = realfile;
  426. od->is_real = !OVL_TYPE_MERGE(type);
  427. od->is_upper = OVL_TYPE_UPPER(type);
  428. file->private_data = od;
  429. return 0;
  430. }
  431. const struct file_operations ovl_dir_operations = {
  432. .read = generic_read_dir,
  433. .open = ovl_dir_open,
  434. .iterate = ovl_iterate,
  435. .llseek = ovl_dir_llseek,
  436. .fsync = ovl_dir_fsync,
  437. .release = ovl_dir_release,
  438. };
  439. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  440. {
  441. int err;
  442. struct ovl_cache_entry *p;
  443. err = ovl_dir_read_merged(dentry, list);
  444. if (err)
  445. return err;
  446. err = 0;
  447. list_for_each_entry(p, list, l_node) {
  448. if (p->is_whiteout)
  449. continue;
  450. if (p->name[0] == '.') {
  451. if (p->len == 1)
  452. continue;
  453. if (p->len == 2 && p->name[1] == '.')
  454. continue;
  455. }
  456. err = -ENOTEMPTY;
  457. break;
  458. }
  459. return err;
  460. }
  461. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
  462. {
  463. struct ovl_cache_entry *p;
  464. inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
  465. list_for_each_entry(p, list, l_node) {
  466. struct dentry *dentry;
  467. if (!p->is_whiteout)
  468. continue;
  469. dentry = lookup_one_len(p->name, upper, p->len);
  470. if (IS_ERR(dentry)) {
  471. pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
  472. upper->d_name.name, p->len, p->name,
  473. (int) PTR_ERR(dentry));
  474. continue;
  475. }
  476. if (dentry->d_inode)
  477. ovl_cleanup(upper->d_inode, dentry);
  478. dput(dentry);
  479. }
  480. inode_unlock(upper->d_inode);
  481. }
  482. static int ovl_check_d_type(struct dir_context *ctx, const char *name,
  483. int namelen, loff_t offset, u64 ino,
  484. unsigned int d_type)
  485. {
  486. struct ovl_readdir_data *rdd =
  487. container_of(ctx, struct ovl_readdir_data, ctx);
  488. /* Even if d_type is not supported, DT_DIR is returned for . and .. */
  489. if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
  490. return 0;
  491. if (d_type != DT_UNKNOWN)
  492. rdd->d_type_supported = true;
  493. return 0;
  494. }
  495. /*
  496. * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
  497. * if error is encountered.
  498. */
  499. int ovl_check_d_type_supported(struct path *realpath)
  500. {
  501. int err;
  502. struct ovl_readdir_data rdd = {
  503. .ctx.actor = ovl_check_d_type,
  504. .d_type_supported = false,
  505. };
  506. err = ovl_dir_read(realpath, &rdd);
  507. if (err)
  508. return err;
  509. return rdd.d_type_supported;
  510. }
  511. static void ovl_workdir_cleanup_recurse(struct path *path, int level)
  512. {
  513. int err;
  514. struct inode *dir = path->dentry->d_inode;
  515. LIST_HEAD(list);
  516. struct ovl_cache_entry *p;
  517. struct ovl_readdir_data rdd = {
  518. .ctx.actor = ovl_fill_merge,
  519. .dentry = NULL,
  520. .list = &list,
  521. .root = RB_ROOT,
  522. .is_lowest = false,
  523. };
  524. err = ovl_dir_read(path, &rdd);
  525. if (err)
  526. goto out;
  527. inode_lock_nested(dir, I_MUTEX_PARENT);
  528. list_for_each_entry(p, &list, l_node) {
  529. struct dentry *dentry;
  530. if (p->name[0] == '.') {
  531. if (p->len == 1)
  532. continue;
  533. if (p->len == 2 && p->name[1] == '.')
  534. continue;
  535. }
  536. dentry = lookup_one_len(p->name, path->dentry, p->len);
  537. if (IS_ERR(dentry))
  538. continue;
  539. if (dentry->d_inode)
  540. ovl_workdir_cleanup(dir, path->mnt, dentry, level);
  541. dput(dentry);
  542. }
  543. inode_unlock(dir);
  544. out:
  545. ovl_cache_free(&list);
  546. }
  547. void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  548. struct dentry *dentry, int level)
  549. {
  550. int err;
  551. if (!d_is_dir(dentry) || level > 1) {
  552. ovl_cleanup(dir, dentry);
  553. return;
  554. }
  555. err = ovl_do_rmdir(dir, dentry);
  556. if (err) {
  557. struct path path = { .mnt = mnt, .dentry = dentry };
  558. inode_unlock(dir);
  559. ovl_workdir_cleanup_recurse(&path, level + 1);
  560. inode_lock_nested(dir, I_MUTEX_PARENT);
  561. ovl_cleanup(dir, dentry);
  562. }
  563. }