readdir.c 12 KB

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