readdir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. bool is_whiteout;
  23. struct list_head l_node;
  24. struct rb_node node;
  25. char name[];
  26. };
  27. struct ovl_dir_cache {
  28. long refcount;
  29. u64 version;
  30. struct list_head entries;
  31. };
  32. struct ovl_readdir_data {
  33. struct dir_context ctx;
  34. bool is_merge;
  35. struct rb_root root;
  36. struct list_head *list;
  37. struct list_head middle;
  38. int count;
  39. int err;
  40. };
  41. struct ovl_dir_file {
  42. bool is_real;
  43. bool is_upper;
  44. struct ovl_dir_cache *cache;
  45. struct ovl_cache_entry cursor;
  46. struct file *realfile;
  47. struct file *upperfile;
  48. };
  49. static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
  50. {
  51. return container_of(n, struct ovl_cache_entry, node);
  52. }
  53. static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
  54. const char *name, int len)
  55. {
  56. struct rb_node *node = root->rb_node;
  57. int cmp;
  58. while (node) {
  59. struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
  60. cmp = strncmp(name, p->name, len);
  61. if (cmp > 0)
  62. node = p->node.rb_right;
  63. else if (cmp < 0 || len < p->len)
  64. node = p->node.rb_left;
  65. else
  66. return p;
  67. }
  68. return NULL;
  69. }
  70. static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
  71. u64 ino, unsigned int d_type)
  72. {
  73. struct ovl_cache_entry *p;
  74. size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
  75. p = kmalloc(size, GFP_KERNEL);
  76. if (p) {
  77. memcpy(p->name, name, len);
  78. p->name[len] = '\0';
  79. p->len = len;
  80. p->type = d_type;
  81. p->ino = ino;
  82. p->is_whiteout = false;
  83. }
  84. return p;
  85. }
  86. static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
  87. const char *name, int len, u64 ino,
  88. unsigned int d_type)
  89. {
  90. struct rb_node **newp = &rdd->root.rb_node;
  91. struct rb_node *parent = NULL;
  92. struct ovl_cache_entry *p;
  93. while (*newp) {
  94. int cmp;
  95. struct ovl_cache_entry *tmp;
  96. parent = *newp;
  97. tmp = ovl_cache_entry_from_node(*newp);
  98. cmp = strncmp(name, tmp->name, len);
  99. if (cmp > 0)
  100. newp = &tmp->node.rb_right;
  101. else if (cmp < 0 || len < tmp->len)
  102. newp = &tmp->node.rb_left;
  103. else
  104. return 0;
  105. }
  106. p = ovl_cache_entry_new(name, len, ino, d_type);
  107. if (p == NULL)
  108. return -ENOMEM;
  109. list_add_tail(&p->l_node, rdd->list);
  110. rb_link_node(&p->node, parent, newp);
  111. rb_insert_color(&p->node, &rdd->root);
  112. return 0;
  113. }
  114. static int ovl_fill_lower(struct ovl_readdir_data *rdd,
  115. const char *name, int namelen,
  116. loff_t offset, u64 ino, unsigned int d_type)
  117. {
  118. struct ovl_cache_entry *p;
  119. p = ovl_cache_entry_find(&rdd->root, name, namelen);
  120. if (p) {
  121. list_move_tail(&p->l_node, &rdd->middle);
  122. } else {
  123. p = ovl_cache_entry_new(name, namelen, ino, d_type);
  124. if (p == NULL)
  125. rdd->err = -ENOMEM;
  126. else
  127. list_add_tail(&p->l_node, &rdd->middle);
  128. }
  129. return rdd->err;
  130. }
  131. void ovl_cache_free(struct list_head *list)
  132. {
  133. struct ovl_cache_entry *p;
  134. struct ovl_cache_entry *n;
  135. list_for_each_entry_safe(p, n, list, l_node)
  136. kfree(p);
  137. INIT_LIST_HEAD(list);
  138. }
  139. static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
  140. {
  141. struct ovl_dir_cache *cache = od->cache;
  142. list_del(&od->cursor.l_node);
  143. WARN_ON(cache->refcount <= 0);
  144. cache->refcount--;
  145. if (!cache->refcount) {
  146. if (ovl_dir_cache(dentry) == cache)
  147. ovl_set_dir_cache(dentry, NULL);
  148. ovl_cache_free(&cache->entries);
  149. kfree(cache);
  150. }
  151. }
  152. static int ovl_fill_merge(void *buf, const char *name, int namelen,
  153. loff_t offset, u64 ino, unsigned int d_type)
  154. {
  155. struct ovl_readdir_data *rdd = buf;
  156. rdd->count++;
  157. if (!rdd->is_merge)
  158. return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
  159. else
  160. return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
  161. }
  162. static inline int ovl_dir_read(struct path *realpath,
  163. struct ovl_readdir_data *rdd)
  164. {
  165. struct file *realfile;
  166. int err;
  167. realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
  168. if (IS_ERR(realfile))
  169. return PTR_ERR(realfile);
  170. rdd->ctx.pos = 0;
  171. do {
  172. rdd->count = 0;
  173. rdd->err = 0;
  174. err = iterate_dir(realfile, &rdd->ctx);
  175. if (err >= 0)
  176. err = rdd->err;
  177. } while (!err && rdd->count);
  178. fput(realfile);
  179. return err;
  180. }
  181. static void ovl_dir_reset(struct file *file)
  182. {
  183. struct ovl_dir_file *od = file->private_data;
  184. struct ovl_dir_cache *cache = od->cache;
  185. struct dentry *dentry = file->f_path.dentry;
  186. enum ovl_path_type type = ovl_path_type(dentry);
  187. if (cache && ovl_dentry_version_get(dentry) != cache->version) {
  188. ovl_cache_put(od, dentry);
  189. od->cache = NULL;
  190. }
  191. WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
  192. if (od->is_real && type == OVL_PATH_MERGE)
  193. od->is_real = false;
  194. }
  195. static int ovl_dir_mark_whiteouts(struct dentry *dir,
  196. struct ovl_readdir_data *rdd)
  197. {
  198. struct ovl_cache_entry *p;
  199. struct dentry *dentry;
  200. const struct cred *old_cred;
  201. struct cred *override_cred;
  202. override_cred = prepare_creds();
  203. if (!override_cred) {
  204. ovl_cache_free(rdd->list);
  205. return -ENOMEM;
  206. }
  207. /*
  208. * CAP_DAC_OVERRIDE for lookup
  209. */
  210. cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  211. old_cred = override_creds(override_cred);
  212. mutex_lock(&dir->d_inode->i_mutex);
  213. list_for_each_entry(p, rdd->list, l_node) {
  214. if (!p->name)
  215. continue;
  216. if (p->type != DT_CHR)
  217. continue;
  218. dentry = lookup_one_len(p->name, dir, p->len);
  219. if (IS_ERR(dentry))
  220. continue;
  221. p->is_whiteout = ovl_is_whiteout(dentry);
  222. dput(dentry);
  223. }
  224. mutex_unlock(&dir->d_inode->i_mutex);
  225. revert_creds(old_cred);
  226. put_cred(override_cred);
  227. return 0;
  228. }
  229. static inline int ovl_dir_read_merged(struct path *upperpath,
  230. struct path *lowerpath,
  231. struct list_head *list)
  232. {
  233. int err;
  234. struct ovl_readdir_data rdd = {
  235. .ctx.actor = ovl_fill_merge,
  236. .list = list,
  237. .root = RB_ROOT,
  238. .is_merge = false,
  239. };
  240. if (upperpath->dentry) {
  241. err = ovl_dir_read(upperpath, &rdd);
  242. if (err)
  243. goto out;
  244. if (lowerpath->dentry) {
  245. err = ovl_dir_mark_whiteouts(upperpath->dentry, &rdd);
  246. if (err)
  247. goto out;
  248. }
  249. }
  250. if (lowerpath->dentry) {
  251. /*
  252. * Insert lowerpath entries before upperpath ones, this allows
  253. * offsets to be reasonably constant
  254. */
  255. list_add(&rdd.middle, rdd.list);
  256. rdd.is_merge = true;
  257. err = ovl_dir_read(lowerpath, &rdd);
  258. list_del(&rdd.middle);
  259. }
  260. out:
  261. return err;
  262. }
  263. static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
  264. {
  265. struct ovl_cache_entry *p;
  266. loff_t off = 0;
  267. list_for_each_entry(p, &od->cache->entries, l_node) {
  268. if (!p->name)
  269. continue;
  270. if (off >= pos)
  271. break;
  272. off++;
  273. }
  274. list_move_tail(&od->cursor.l_node, &p->l_node);
  275. }
  276. static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
  277. {
  278. int res;
  279. struct path lowerpath;
  280. struct path upperpath;
  281. struct ovl_dir_cache *cache;
  282. cache = ovl_dir_cache(dentry);
  283. if (cache && ovl_dentry_version_get(dentry) == cache->version) {
  284. cache->refcount++;
  285. return cache;
  286. }
  287. ovl_set_dir_cache(dentry, NULL);
  288. cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
  289. if (!cache)
  290. return ERR_PTR(-ENOMEM);
  291. cache->refcount = 1;
  292. INIT_LIST_HEAD(&cache->entries);
  293. ovl_path_lower(dentry, &lowerpath);
  294. ovl_path_upper(dentry, &upperpath);
  295. res = ovl_dir_read_merged(&upperpath, &lowerpath, &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->name) {
  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_MERGE) {
  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. file->private_data = od;
  439. return 0;
  440. }
  441. const struct file_operations ovl_dir_operations = {
  442. .read = generic_read_dir,
  443. .open = ovl_dir_open,
  444. .iterate = ovl_iterate,
  445. .llseek = ovl_dir_llseek,
  446. .fsync = ovl_dir_fsync,
  447. .release = ovl_dir_release,
  448. };
  449. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  450. {
  451. int err;
  452. struct path lowerpath;
  453. struct path upperpath;
  454. struct ovl_cache_entry *p;
  455. ovl_path_upper(dentry, &upperpath);
  456. ovl_path_lower(dentry, &lowerpath);
  457. err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
  458. if (err)
  459. return err;
  460. err = 0;
  461. list_for_each_entry(p, list, l_node) {
  462. if (p->is_whiteout)
  463. continue;
  464. if (p->name[0] == '.') {
  465. if (p->len == 1)
  466. continue;
  467. if (p->len == 2 && p->name[1] == '.')
  468. continue;
  469. }
  470. err = -ENOTEMPTY;
  471. break;
  472. }
  473. return err;
  474. }
  475. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
  476. {
  477. struct ovl_cache_entry *p;
  478. mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_PARENT);
  479. list_for_each_entry(p, list, l_node) {
  480. struct dentry *dentry;
  481. if (!p->is_whiteout)
  482. continue;
  483. dentry = lookup_one_len(p->name, upper, p->len);
  484. if (IS_ERR(dentry)) {
  485. pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
  486. upper->d_name.name, p->len, p->name,
  487. (int) PTR_ERR(dentry));
  488. continue;
  489. }
  490. ovl_cleanup(upper->d_inode, dentry);
  491. dput(dentry);
  492. }
  493. mutex_unlock(&upper->d_inode->i_mutex);
  494. }