readdir.c 12 KB

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