readdir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 = mutex_lock_killable(&dir->d_inode->i_mutex);
  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. mutex_unlock(&dir->d_inode->i_mutex);
  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. mutex_lock(&file_inode(file)->i_mutex);
  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. mutex_unlock(&file_inode(file)->i_mutex);
  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. /* Nothing to sync for lower */
  368. if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
  369. return 0;
  370. /*
  371. * Need to check if we started out being a lower dir, but got copied up
  372. */
  373. if (!od->is_upper) {
  374. struct inode *inode = file_inode(file);
  375. realfile = lockless_dereference(od->upperfile);
  376. if (!realfile) {
  377. struct path upperpath;
  378. ovl_path_upper(dentry, &upperpath);
  379. realfile = ovl_path_open(&upperpath, O_RDONLY);
  380. smp_mb__before_spinlock();
  381. mutex_lock(&inode->i_mutex);
  382. if (!od->upperfile) {
  383. if (IS_ERR(realfile)) {
  384. mutex_unlock(&inode->i_mutex);
  385. return PTR_ERR(realfile);
  386. }
  387. od->upperfile = realfile;
  388. } else {
  389. /* somebody has beaten us to it */
  390. if (!IS_ERR(realfile))
  391. fput(realfile);
  392. realfile = od->upperfile;
  393. }
  394. mutex_unlock(&inode->i_mutex);
  395. }
  396. }
  397. return vfs_fsync_range(realfile, start, end, datasync);
  398. }
  399. static int ovl_dir_release(struct inode *inode, struct file *file)
  400. {
  401. struct ovl_dir_file *od = file->private_data;
  402. if (od->cache) {
  403. mutex_lock(&inode->i_mutex);
  404. ovl_cache_put(od, file->f_path.dentry);
  405. mutex_unlock(&inode->i_mutex);
  406. }
  407. fput(od->realfile);
  408. if (od->upperfile)
  409. fput(od->upperfile);
  410. kfree(od);
  411. return 0;
  412. }
  413. static int ovl_dir_open(struct inode *inode, struct file *file)
  414. {
  415. struct path realpath;
  416. struct file *realfile;
  417. struct ovl_dir_file *od;
  418. enum ovl_path_type type;
  419. od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
  420. if (!od)
  421. return -ENOMEM;
  422. type = ovl_path_real(file->f_path.dentry, &realpath);
  423. realfile = ovl_path_open(&realpath, file->f_flags);
  424. if (IS_ERR(realfile)) {
  425. kfree(od);
  426. return PTR_ERR(realfile);
  427. }
  428. od->realfile = realfile;
  429. od->is_real = !OVL_TYPE_MERGE(type);
  430. od->is_upper = OVL_TYPE_UPPER(type);
  431. file->private_data = od;
  432. return 0;
  433. }
  434. const struct file_operations ovl_dir_operations = {
  435. .read = generic_read_dir,
  436. .open = ovl_dir_open,
  437. .iterate = ovl_iterate,
  438. .llseek = ovl_dir_llseek,
  439. .fsync = ovl_dir_fsync,
  440. .release = ovl_dir_release,
  441. };
  442. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  443. {
  444. int err;
  445. struct ovl_cache_entry *p;
  446. err = ovl_dir_read_merged(dentry, list);
  447. if (err)
  448. return err;
  449. err = 0;
  450. list_for_each_entry(p, list, l_node) {
  451. if (p->is_whiteout)
  452. continue;
  453. if (p->name[0] == '.') {
  454. if (p->len == 1)
  455. continue;
  456. if (p->len == 2 && p->name[1] == '.')
  457. continue;
  458. }
  459. err = -ENOTEMPTY;
  460. break;
  461. }
  462. return err;
  463. }
  464. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
  465. {
  466. struct ovl_cache_entry *p;
  467. mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_CHILD);
  468. list_for_each_entry(p, list, l_node) {
  469. struct dentry *dentry;
  470. if (!p->is_whiteout)
  471. continue;
  472. dentry = lookup_one_len(p->name, upper, p->len);
  473. if (IS_ERR(dentry)) {
  474. pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
  475. upper->d_name.name, p->len, p->name,
  476. (int) PTR_ERR(dentry));
  477. continue;
  478. }
  479. if (dentry->d_inode)
  480. ovl_cleanup(upper->d_inode, dentry);
  481. dput(dentry);
  482. }
  483. mutex_unlock(&upper->d_inode->i_mutex);
  484. }
  485. static int ovl_check_d_type(struct dir_context *ctx, const char *name,
  486. int namelen, loff_t offset, u64 ino,
  487. unsigned int d_type)
  488. {
  489. struct ovl_readdir_data *rdd =
  490. container_of(ctx, struct ovl_readdir_data, ctx);
  491. /* Even if d_type is not supported, DT_DIR is returned for . and .. */
  492. if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
  493. return 0;
  494. if (d_type != DT_UNKNOWN)
  495. rdd->d_type_supported = true;
  496. return 0;
  497. }
  498. /*
  499. * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
  500. * if error is encountered.
  501. */
  502. int ovl_check_d_type_supported(struct path *realpath)
  503. {
  504. int err;
  505. struct ovl_readdir_data rdd = {
  506. .ctx.actor = ovl_check_d_type,
  507. .d_type_supported = false,
  508. };
  509. err = ovl_dir_read(realpath, &rdd);
  510. if (err)
  511. return err;
  512. return rdd.d_type_supported;
  513. }
  514. static void ovl_workdir_cleanup_recurse(struct path *path, int level)
  515. {
  516. int err;
  517. struct inode *dir = path->dentry->d_inode;
  518. LIST_HEAD(list);
  519. struct ovl_cache_entry *p;
  520. struct ovl_readdir_data rdd = {
  521. .ctx.actor = ovl_fill_merge,
  522. .dentry = NULL,
  523. .list = &list,
  524. .root = RB_ROOT,
  525. .is_lowest = false,
  526. };
  527. err = ovl_dir_read(path, &rdd);
  528. if (err)
  529. goto out;
  530. inode_lock_nested(dir, I_MUTEX_PARENT);
  531. list_for_each_entry(p, &list, l_node) {
  532. struct dentry *dentry;
  533. if (p->name[0] == '.') {
  534. if (p->len == 1)
  535. continue;
  536. if (p->len == 2 && p->name[1] == '.')
  537. continue;
  538. }
  539. dentry = lookup_one_len(p->name, path->dentry, p->len);
  540. if (IS_ERR(dentry))
  541. continue;
  542. if (dentry->d_inode)
  543. ovl_workdir_cleanup(dir, path->mnt, dentry, level);
  544. dput(dentry);
  545. }
  546. inode_unlock(dir);
  547. out:
  548. ovl_cache_free(&list);
  549. }
  550. void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  551. struct dentry *dentry, int level)
  552. {
  553. int err;
  554. if (!d_is_dir(dentry) || level > 1) {
  555. ovl_cleanup(dir, dentry);
  556. return;
  557. }
  558. err = ovl_do_rmdir(dir, dentry);
  559. if (err) {
  560. struct path path = { .mnt = mnt, .dentry = dentry };
  561. inode_unlock(dir);
  562. ovl_workdir_cleanup_recurse(&path, level + 1);
  563. inode_lock_nested(dir, I_MUTEX_PARENT);
  564. ovl_cleanup(dir, dentry);
  565. }
  566. }