readdir.c 12 KB

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