readdir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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(void *buf, const char *name, int namelen,
  155. loff_t offset, u64 ino, unsigned int d_type)
  156. {
  157. struct ovl_readdir_data *rdd = buf;
  158. rdd->count++;
  159. if (!rdd->is_merge)
  160. return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
  161. else
  162. return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
  163. }
  164. static inline int ovl_dir_read(struct path *realpath,
  165. struct ovl_readdir_data *rdd)
  166. {
  167. struct file *realfile;
  168. int err;
  169. realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
  170. if (IS_ERR(realfile))
  171. return PTR_ERR(realfile);
  172. rdd->ctx.pos = 0;
  173. do {
  174. rdd->count = 0;
  175. rdd->err = 0;
  176. err = iterate_dir(realfile, &rdd->ctx);
  177. if (err >= 0)
  178. err = rdd->err;
  179. } while (!err && rdd->count);
  180. fput(realfile);
  181. return err;
  182. }
  183. static void ovl_dir_reset(struct file *file)
  184. {
  185. struct ovl_dir_file *od = file->private_data;
  186. struct ovl_dir_cache *cache = od->cache;
  187. struct dentry *dentry = file->f_path.dentry;
  188. enum ovl_path_type type = ovl_path_type(dentry);
  189. if (cache && ovl_dentry_version_get(dentry) != cache->version) {
  190. ovl_cache_put(od, dentry);
  191. od->cache = NULL;
  192. }
  193. WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
  194. if (od->is_real && type == OVL_PATH_MERGE)
  195. od->is_real = false;
  196. }
  197. static int ovl_dir_mark_whiteouts(struct dentry *dir,
  198. struct ovl_readdir_data *rdd)
  199. {
  200. struct ovl_cache_entry *p;
  201. struct dentry *dentry;
  202. const struct cred *old_cred;
  203. struct cred *override_cred;
  204. override_cred = prepare_creds();
  205. if (!override_cred) {
  206. ovl_cache_free(rdd->list);
  207. return -ENOMEM;
  208. }
  209. /*
  210. * CAP_DAC_OVERRIDE for lookup
  211. */
  212. cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  213. old_cred = override_creds(override_cred);
  214. mutex_lock(&dir->d_inode->i_mutex);
  215. list_for_each_entry(p, rdd->list, l_node) {
  216. if (p->is_cursor)
  217. continue;
  218. if (p->type != DT_CHR)
  219. continue;
  220. dentry = lookup_one_len(p->name, dir, p->len);
  221. if (IS_ERR(dentry))
  222. continue;
  223. p->is_whiteout = ovl_is_whiteout(dentry);
  224. dput(dentry);
  225. }
  226. mutex_unlock(&dir->d_inode->i_mutex);
  227. revert_creds(old_cred);
  228. put_cred(override_cred);
  229. return 0;
  230. }
  231. static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
  232. {
  233. int err;
  234. struct path lowerpath;
  235. struct path upperpath;
  236. struct ovl_readdir_data rdd = {
  237. .ctx.actor = ovl_fill_merge,
  238. .list = list,
  239. .root = RB_ROOT,
  240. .is_merge = false,
  241. };
  242. ovl_path_lower(dentry, &lowerpath);
  243. ovl_path_upper(dentry, &upperpath);
  244. if (upperpath.dentry) {
  245. err = ovl_dir_read(&upperpath, &rdd);
  246. if (err)
  247. goto out;
  248. if (lowerpath.dentry) {
  249. err = ovl_dir_mark_whiteouts(upperpath.dentry, &rdd);
  250. if (err)
  251. goto out;
  252. }
  253. }
  254. if (lowerpath.dentry) {
  255. /*
  256. * Insert lowerpath entries before upperpath ones, this allows
  257. * offsets to be reasonably constant
  258. */
  259. list_add(&rdd.middle, rdd.list);
  260. rdd.is_merge = true;
  261. err = ovl_dir_read(&lowerpath, &rdd);
  262. list_del(&rdd.middle);
  263. }
  264. out:
  265. return err;
  266. }
  267. static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
  268. {
  269. struct ovl_cache_entry *p;
  270. loff_t off = 0;
  271. list_for_each_entry(p, &od->cache->entries, l_node) {
  272. if (p->is_cursor)
  273. continue;
  274. if (off >= pos)
  275. break;
  276. off++;
  277. }
  278. list_move_tail(&od->cursor.l_node, &p->l_node);
  279. }
  280. static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
  281. {
  282. int res;
  283. struct ovl_dir_cache *cache;
  284. cache = ovl_dir_cache(dentry);
  285. if (cache && ovl_dentry_version_get(dentry) == cache->version) {
  286. cache->refcount++;
  287. return cache;
  288. }
  289. ovl_set_dir_cache(dentry, NULL);
  290. cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
  291. if (!cache)
  292. return ERR_PTR(-ENOMEM);
  293. cache->refcount = 1;
  294. INIT_LIST_HEAD(&cache->entries);
  295. res = ovl_dir_read_merged(dentry, &cache->entries);
  296. if (res) {
  297. ovl_cache_free(&cache->entries);
  298. kfree(cache);
  299. return ERR_PTR(res);
  300. }
  301. cache->version = ovl_dentry_version_get(dentry);
  302. ovl_set_dir_cache(dentry, cache);
  303. return cache;
  304. }
  305. static int ovl_iterate(struct file *file, struct dir_context *ctx)
  306. {
  307. struct ovl_dir_file *od = file->private_data;
  308. struct dentry *dentry = file->f_path.dentry;
  309. if (!ctx->pos)
  310. ovl_dir_reset(file);
  311. if (od->is_real)
  312. return iterate_dir(od->realfile, ctx);
  313. if (!od->cache) {
  314. struct ovl_dir_cache *cache;
  315. cache = ovl_cache_get(dentry);
  316. if (IS_ERR(cache))
  317. return PTR_ERR(cache);
  318. od->cache = cache;
  319. ovl_seek_cursor(od, ctx->pos);
  320. }
  321. while (od->cursor.l_node.next != &od->cache->entries) {
  322. struct ovl_cache_entry *p;
  323. p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node);
  324. /* Skip cursors */
  325. if (!p->is_cursor) {
  326. if (!p->is_whiteout) {
  327. if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
  328. break;
  329. }
  330. ctx->pos++;
  331. }
  332. list_move(&od->cursor.l_node, &p->l_node);
  333. }
  334. return 0;
  335. }
  336. static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
  337. {
  338. loff_t res;
  339. struct ovl_dir_file *od = file->private_data;
  340. mutex_lock(&file_inode(file)->i_mutex);
  341. if (!file->f_pos)
  342. ovl_dir_reset(file);
  343. if (od->is_real) {
  344. res = vfs_llseek(od->realfile, offset, origin);
  345. file->f_pos = od->realfile->f_pos;
  346. } else {
  347. res = -EINVAL;
  348. switch (origin) {
  349. case SEEK_CUR:
  350. offset += file->f_pos;
  351. break;
  352. case SEEK_SET:
  353. break;
  354. default:
  355. goto out_unlock;
  356. }
  357. if (offset < 0)
  358. goto out_unlock;
  359. if (offset != file->f_pos) {
  360. file->f_pos = offset;
  361. if (od->cache)
  362. ovl_seek_cursor(od, offset);
  363. }
  364. res = offset;
  365. }
  366. out_unlock:
  367. mutex_unlock(&file_inode(file)->i_mutex);
  368. return res;
  369. }
  370. static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
  371. int datasync)
  372. {
  373. struct ovl_dir_file *od = file->private_data;
  374. struct dentry *dentry = file->f_path.dentry;
  375. struct file *realfile = od->realfile;
  376. /*
  377. * Need to check if we started out being a lower dir, but got copied up
  378. */
  379. if (!od->is_upper && ovl_path_type(dentry) != OVL_PATH_LOWER) {
  380. struct inode *inode = file_inode(file);
  381. realfile = lockless_dereference(od->upperfile);
  382. if (!realfile) {
  383. struct path upperpath;
  384. ovl_path_upper(dentry, &upperpath);
  385. realfile = ovl_path_open(&upperpath, O_RDONLY);
  386. smp_mb__before_spinlock();
  387. mutex_lock(&inode->i_mutex);
  388. if (!od->upperfile) {
  389. if (IS_ERR(realfile)) {
  390. mutex_unlock(&inode->i_mutex);
  391. return PTR_ERR(realfile);
  392. }
  393. od->upperfile = realfile;
  394. } else {
  395. /* somebody has beaten us to it */
  396. if (!IS_ERR(realfile))
  397. fput(realfile);
  398. realfile = od->upperfile;
  399. }
  400. mutex_unlock(&inode->i_mutex);
  401. }
  402. }
  403. return vfs_fsync_range(realfile, start, end, datasync);
  404. }
  405. static int ovl_dir_release(struct inode *inode, struct file *file)
  406. {
  407. struct ovl_dir_file *od = file->private_data;
  408. if (od->cache) {
  409. mutex_lock(&inode->i_mutex);
  410. ovl_cache_put(od, file->f_path.dentry);
  411. mutex_unlock(&inode->i_mutex);
  412. }
  413. fput(od->realfile);
  414. if (od->upperfile)
  415. fput(od->upperfile);
  416. kfree(od);
  417. return 0;
  418. }
  419. static int ovl_dir_open(struct inode *inode, struct file *file)
  420. {
  421. struct path realpath;
  422. struct file *realfile;
  423. struct ovl_dir_file *od;
  424. enum ovl_path_type type;
  425. od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
  426. if (!od)
  427. return -ENOMEM;
  428. type = ovl_path_real(file->f_path.dentry, &realpath);
  429. realfile = ovl_path_open(&realpath, file->f_flags);
  430. if (IS_ERR(realfile)) {
  431. kfree(od);
  432. return PTR_ERR(realfile);
  433. }
  434. INIT_LIST_HEAD(&od->cursor.l_node);
  435. od->realfile = realfile;
  436. od->is_real = (type != OVL_PATH_MERGE);
  437. od->is_upper = (type != OVL_PATH_LOWER);
  438. od->cursor.is_cursor = true;
  439. file->private_data = od;
  440. return 0;
  441. }
  442. const struct file_operations ovl_dir_operations = {
  443. .read = generic_read_dir,
  444. .open = ovl_dir_open,
  445. .iterate = ovl_iterate,
  446. .llseek = ovl_dir_llseek,
  447. .fsync = ovl_dir_fsync,
  448. .release = ovl_dir_release,
  449. };
  450. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  451. {
  452. int err;
  453. struct ovl_cache_entry *p;
  454. err = ovl_dir_read_merged(dentry, list);
  455. if (err)
  456. return err;
  457. err = 0;
  458. list_for_each_entry(p, list, l_node) {
  459. if (p->is_whiteout)
  460. continue;
  461. if (p->name[0] == '.') {
  462. if (p->len == 1)
  463. continue;
  464. if (p->len == 2 && p->name[1] == '.')
  465. continue;
  466. }
  467. err = -ENOTEMPTY;
  468. break;
  469. }
  470. return err;
  471. }
  472. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
  473. {
  474. struct ovl_cache_entry *p;
  475. mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_CHILD);
  476. list_for_each_entry(p, list, l_node) {
  477. struct dentry *dentry;
  478. if (!p->is_whiteout)
  479. continue;
  480. dentry = lookup_one_len(p->name, upper, p->len);
  481. if (IS_ERR(dentry)) {
  482. pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
  483. upper->d_name.name, p->len, p->name,
  484. (int) PTR_ERR(dentry));
  485. continue;
  486. }
  487. ovl_cleanup(upper->d_inode, dentry);
  488. dput(dentry);
  489. }
  490. mutex_unlock(&upper->d_inode->i_mutex);
  491. }