dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * linux/fs/ext4/dir.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/dir.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * ext4 directory handling functions
  16. *
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. *
  20. * Hash Tree Directory indexing (c) 2001 Daniel Phillips
  21. *
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/jbd2.h>
  25. #include <linux/buffer_head.h>
  26. #include <linux/slab.h>
  27. #include <linux/rbtree.h>
  28. #include "ext4.h"
  29. #include "xattr.h"
  30. static int ext4_dx_readdir(struct file *, struct dir_context *);
  31. /**
  32. * Check if the given dir-inode refers to an htree-indexed directory
  33. * (or a directory which could potentially get converted to use htree
  34. * indexing).
  35. *
  36. * Return 1 if it is a dx dir, 0 if not
  37. */
  38. static int is_dx_dir(struct inode *inode)
  39. {
  40. struct super_block *sb = inode->i_sb;
  41. if (EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  42. EXT4_FEATURE_COMPAT_DIR_INDEX) &&
  43. ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
  44. ((inode->i_size >> sb->s_blocksize_bits) == 1) ||
  45. ext4_has_inline_data(inode)))
  46. return 1;
  47. return 0;
  48. }
  49. /*
  50. * Return 0 if the directory entry is OK, and 1 if there is a problem
  51. *
  52. * Note: this is the opposite of what ext2 and ext3 historically returned...
  53. *
  54. * bh passed here can be an inode block or a dir data block, depending
  55. * on the inode inline data flag.
  56. */
  57. int __ext4_check_dir_entry(const char *function, unsigned int line,
  58. struct inode *dir, struct file *filp,
  59. struct ext4_dir_entry_2 *de,
  60. struct buffer_head *bh, char *buf, int size,
  61. unsigned int offset)
  62. {
  63. const char *error_msg = NULL;
  64. const int rlen = ext4_rec_len_from_disk(de->rec_len,
  65. dir->i_sb->s_blocksize);
  66. if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
  67. error_msg = "rec_len is smaller than minimal";
  68. else if (unlikely(rlen % 4 != 0))
  69. error_msg = "rec_len % 4 != 0";
  70. else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
  71. error_msg = "rec_len is too small for name_len";
  72. else if (unlikely(((char *) de - buf) + rlen > size))
  73. error_msg = "directory entry across range";
  74. else if (unlikely(le32_to_cpu(de->inode) >
  75. le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
  76. error_msg = "inode out of bounds";
  77. else
  78. return 0;
  79. if (filp)
  80. ext4_error_file(filp, function, line, bh->b_blocknr,
  81. "bad entry in directory: %s - offset=%u(%u), "
  82. "inode=%u, rec_len=%d, name_len=%d",
  83. error_msg, (unsigned) (offset % size),
  84. offset, le32_to_cpu(de->inode),
  85. rlen, de->name_len);
  86. else
  87. ext4_error_inode(dir, function, line, bh->b_blocknr,
  88. "bad entry in directory: %s - offset=%u(%u), "
  89. "inode=%u, rec_len=%d, name_len=%d",
  90. error_msg, (unsigned) (offset % size),
  91. offset, le32_to_cpu(de->inode),
  92. rlen, de->name_len);
  93. return 1;
  94. }
  95. static int ext4_readdir(struct file *file, struct dir_context *ctx)
  96. {
  97. unsigned int offset;
  98. int i;
  99. struct ext4_dir_entry_2 *de;
  100. int err;
  101. struct inode *inode = file_inode(file);
  102. struct super_block *sb = inode->i_sb;
  103. int dir_has_error = 0;
  104. if (is_dx_dir(inode)) {
  105. err = ext4_dx_readdir(file, ctx);
  106. if (err != ERR_BAD_DX_DIR) {
  107. return err;
  108. }
  109. /*
  110. * We don't set the inode dirty flag since it's not
  111. * critical that it get flushed back to the disk.
  112. */
  113. ext4_clear_inode_flag(file_inode(file),
  114. EXT4_INODE_INDEX);
  115. }
  116. if (ext4_has_inline_data(inode)) {
  117. int has_inline_data = 1;
  118. int ret = ext4_read_inline_dir(file, ctx,
  119. &has_inline_data);
  120. if (has_inline_data)
  121. return ret;
  122. }
  123. offset = ctx->pos & (sb->s_blocksize - 1);
  124. while (ctx->pos < inode->i_size) {
  125. struct ext4_map_blocks map;
  126. struct buffer_head *bh = NULL;
  127. map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
  128. map.m_len = 1;
  129. err = ext4_map_blocks(NULL, inode, &map, 0);
  130. if (err > 0) {
  131. pgoff_t index = map.m_pblk >>
  132. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  133. if (!ra_has_index(&file->f_ra, index))
  134. page_cache_sync_readahead(
  135. sb->s_bdev->bd_inode->i_mapping,
  136. &file->f_ra, file,
  137. index, 1);
  138. file->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  139. bh = ext4_bread(NULL, inode, map.m_lblk, 0);
  140. if (IS_ERR(bh))
  141. return PTR_ERR(bh);
  142. }
  143. if (!bh) {
  144. if (!dir_has_error) {
  145. EXT4_ERROR_FILE(file, 0,
  146. "directory contains a "
  147. "hole at offset %llu",
  148. (unsigned long long) ctx->pos);
  149. dir_has_error = 1;
  150. }
  151. /* corrupt size? Maybe no more blocks to read */
  152. if (ctx->pos > inode->i_blocks << 9)
  153. break;
  154. ctx->pos += sb->s_blocksize - offset;
  155. continue;
  156. }
  157. /* Check the checksum */
  158. if (!buffer_verified(bh) &&
  159. !ext4_dirent_csum_verify(inode,
  160. (struct ext4_dir_entry *)bh->b_data)) {
  161. EXT4_ERROR_FILE(file, 0, "directory fails checksum "
  162. "at offset %llu",
  163. (unsigned long long)ctx->pos);
  164. ctx->pos += sb->s_blocksize - offset;
  165. brelse(bh);
  166. continue;
  167. }
  168. set_buffer_verified(bh);
  169. /* If the dir block has changed since the last call to
  170. * readdir(2), then we might be pointing to an invalid
  171. * dirent right now. Scan from the start of the block
  172. * to make sure. */
  173. if (file->f_version != inode->i_version) {
  174. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  175. de = (struct ext4_dir_entry_2 *)
  176. (bh->b_data + i);
  177. /* It's too expensive to do a full
  178. * dirent test each time round this
  179. * loop, but we do have to test at
  180. * least that it is non-zero. A
  181. * failure will be detected in the
  182. * dirent test below. */
  183. if (ext4_rec_len_from_disk(de->rec_len,
  184. sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
  185. break;
  186. i += ext4_rec_len_from_disk(de->rec_len,
  187. sb->s_blocksize);
  188. }
  189. offset = i;
  190. ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
  191. | offset;
  192. file->f_version = inode->i_version;
  193. }
  194. while (ctx->pos < inode->i_size
  195. && offset < sb->s_blocksize) {
  196. de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
  197. if (ext4_check_dir_entry(inode, file, de, bh,
  198. bh->b_data, bh->b_size,
  199. offset)) {
  200. /*
  201. * On error, skip to the next block
  202. */
  203. ctx->pos = (ctx->pos |
  204. (sb->s_blocksize - 1)) + 1;
  205. break;
  206. }
  207. offset += ext4_rec_len_from_disk(de->rec_len,
  208. sb->s_blocksize);
  209. if (le32_to_cpu(de->inode)) {
  210. if (!dir_emit(ctx, de->name,
  211. de->name_len,
  212. le32_to_cpu(de->inode),
  213. get_dtype(sb, de->file_type))) {
  214. brelse(bh);
  215. return 0;
  216. }
  217. }
  218. ctx->pos += ext4_rec_len_from_disk(de->rec_len,
  219. sb->s_blocksize);
  220. }
  221. offset = 0;
  222. brelse(bh);
  223. if (ctx->pos < inode->i_size) {
  224. if (!dir_relax(inode))
  225. return 0;
  226. }
  227. }
  228. return 0;
  229. }
  230. static inline int is_32bit_api(void)
  231. {
  232. #ifdef CONFIG_COMPAT
  233. return is_compat_task();
  234. #else
  235. return (BITS_PER_LONG == 32);
  236. #endif
  237. }
  238. /*
  239. * These functions convert from the major/minor hash to an f_pos
  240. * value for dx directories
  241. *
  242. * Upper layer (for example NFS) should specify FMODE_32BITHASH or
  243. * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
  244. * directly on both 32-bit and 64-bit nodes, under such case, neither
  245. * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
  246. */
  247. static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
  248. {
  249. if ((filp->f_mode & FMODE_32BITHASH) ||
  250. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  251. return major >> 1;
  252. else
  253. return ((__u64)(major >> 1) << 32) | (__u64)minor;
  254. }
  255. static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
  256. {
  257. if ((filp->f_mode & FMODE_32BITHASH) ||
  258. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  259. return (pos << 1) & 0xffffffff;
  260. else
  261. return ((pos >> 32) << 1) & 0xffffffff;
  262. }
  263. static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
  264. {
  265. if ((filp->f_mode & FMODE_32BITHASH) ||
  266. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  267. return 0;
  268. else
  269. return pos & 0xffffffff;
  270. }
  271. /*
  272. * Return 32- or 64-bit end-of-file for dx directories
  273. */
  274. static inline loff_t ext4_get_htree_eof(struct file *filp)
  275. {
  276. if ((filp->f_mode & FMODE_32BITHASH) ||
  277. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  278. return EXT4_HTREE_EOF_32BIT;
  279. else
  280. return EXT4_HTREE_EOF_64BIT;
  281. }
  282. /*
  283. * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
  284. * directories, where the "offset" is in terms of the filename hash
  285. * value instead of the byte offset.
  286. *
  287. * Because we may return a 64-bit hash that is well beyond offset limits,
  288. * we need to pass the max hash as the maximum allowable offset in
  289. * the htree directory case.
  290. *
  291. * For non-htree, ext4_llseek already chooses the proper max offset.
  292. */
  293. static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
  294. {
  295. struct inode *inode = file->f_mapping->host;
  296. int dx_dir = is_dx_dir(inode);
  297. loff_t htree_max = ext4_get_htree_eof(file);
  298. if (likely(dx_dir))
  299. return generic_file_llseek_size(file, offset, whence,
  300. htree_max, htree_max);
  301. else
  302. return ext4_llseek(file, offset, whence);
  303. }
  304. /*
  305. * This structure holds the nodes of the red-black tree used to store
  306. * the directory entry in hash order.
  307. */
  308. struct fname {
  309. __u32 hash;
  310. __u32 minor_hash;
  311. struct rb_node rb_hash;
  312. struct fname *next;
  313. __u32 inode;
  314. __u8 name_len;
  315. __u8 file_type;
  316. char name[0];
  317. };
  318. /*
  319. * This functoin implements a non-recursive way of freeing all of the
  320. * nodes in the red-black tree.
  321. */
  322. static void free_rb_tree_fname(struct rb_root *root)
  323. {
  324. struct fname *fname, *next;
  325. rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
  326. while (fname) {
  327. struct fname *old = fname;
  328. fname = fname->next;
  329. kfree(old);
  330. }
  331. *root = RB_ROOT;
  332. }
  333. static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
  334. loff_t pos)
  335. {
  336. struct dir_private_info *p;
  337. p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  338. if (!p)
  339. return NULL;
  340. p->curr_hash = pos2maj_hash(filp, pos);
  341. p->curr_minor_hash = pos2min_hash(filp, pos);
  342. return p;
  343. }
  344. void ext4_htree_free_dir_info(struct dir_private_info *p)
  345. {
  346. free_rb_tree_fname(&p->root);
  347. kfree(p);
  348. }
  349. /*
  350. * Given a directory entry, enter it into the fname rb tree.
  351. */
  352. int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  353. __u32 minor_hash,
  354. struct ext4_dir_entry_2 *dirent)
  355. {
  356. struct rb_node **p, *parent = NULL;
  357. struct fname *fname, *new_fn;
  358. struct dir_private_info *info;
  359. int len;
  360. info = dir_file->private_data;
  361. p = &info->root.rb_node;
  362. /* Create and allocate the fname structure */
  363. len = sizeof(struct fname) + dirent->name_len + 1;
  364. new_fn = kzalloc(len, GFP_KERNEL);
  365. if (!new_fn)
  366. return -ENOMEM;
  367. new_fn->hash = hash;
  368. new_fn->minor_hash = minor_hash;
  369. new_fn->inode = le32_to_cpu(dirent->inode);
  370. new_fn->name_len = dirent->name_len;
  371. new_fn->file_type = dirent->file_type;
  372. memcpy(new_fn->name, dirent->name, dirent->name_len);
  373. new_fn->name[dirent->name_len] = 0;
  374. while (*p) {
  375. parent = *p;
  376. fname = rb_entry(parent, struct fname, rb_hash);
  377. /*
  378. * If the hash and minor hash match up, then we put
  379. * them on a linked list. This rarely happens...
  380. */
  381. if ((new_fn->hash == fname->hash) &&
  382. (new_fn->minor_hash == fname->minor_hash)) {
  383. new_fn->next = fname->next;
  384. fname->next = new_fn;
  385. return 0;
  386. }
  387. if (new_fn->hash < fname->hash)
  388. p = &(*p)->rb_left;
  389. else if (new_fn->hash > fname->hash)
  390. p = &(*p)->rb_right;
  391. else if (new_fn->minor_hash < fname->minor_hash)
  392. p = &(*p)->rb_left;
  393. else /* if (new_fn->minor_hash > fname->minor_hash) */
  394. p = &(*p)->rb_right;
  395. }
  396. rb_link_node(&new_fn->rb_hash, parent, p);
  397. rb_insert_color(&new_fn->rb_hash, &info->root);
  398. return 0;
  399. }
  400. /*
  401. * This is a helper function for ext4_dx_readdir. It calls filldir
  402. * for all entres on the fname linked list. (Normally there is only
  403. * one entry on the linked list, unless there are 62 bit hash collisions.)
  404. */
  405. static int call_filldir(struct file *file, struct dir_context *ctx,
  406. struct fname *fname)
  407. {
  408. struct dir_private_info *info = file->private_data;
  409. struct inode *inode = file_inode(file);
  410. struct super_block *sb = inode->i_sb;
  411. if (!fname) {
  412. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
  413. "called with null fname?!?", __func__, __LINE__,
  414. inode->i_ino, current->comm);
  415. return 0;
  416. }
  417. ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
  418. while (fname) {
  419. if (!dir_emit(ctx, fname->name,
  420. fname->name_len,
  421. fname->inode,
  422. get_dtype(sb, fname->file_type))) {
  423. info->extra_fname = fname;
  424. return 1;
  425. }
  426. fname = fname->next;
  427. }
  428. return 0;
  429. }
  430. static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
  431. {
  432. struct dir_private_info *info = file->private_data;
  433. struct inode *inode = file_inode(file);
  434. struct fname *fname;
  435. int ret;
  436. if (!info) {
  437. info = ext4_htree_create_dir_info(file, ctx->pos);
  438. if (!info)
  439. return -ENOMEM;
  440. file->private_data = info;
  441. }
  442. if (ctx->pos == ext4_get_htree_eof(file))
  443. return 0; /* EOF */
  444. /* Some one has messed with f_pos; reset the world */
  445. if (info->last_pos != ctx->pos) {
  446. free_rb_tree_fname(&info->root);
  447. info->curr_node = NULL;
  448. info->extra_fname = NULL;
  449. info->curr_hash = pos2maj_hash(file, ctx->pos);
  450. info->curr_minor_hash = pos2min_hash(file, ctx->pos);
  451. }
  452. /*
  453. * If there are any leftover names on the hash collision
  454. * chain, return them first.
  455. */
  456. if (info->extra_fname) {
  457. if (call_filldir(file, ctx, info->extra_fname))
  458. goto finished;
  459. info->extra_fname = NULL;
  460. goto next_node;
  461. } else if (!info->curr_node)
  462. info->curr_node = rb_first(&info->root);
  463. while (1) {
  464. /*
  465. * Fill the rbtree if we have no more entries,
  466. * or the inode has changed since we last read in the
  467. * cached entries.
  468. */
  469. if ((!info->curr_node) ||
  470. (file->f_version != inode->i_version)) {
  471. info->curr_node = NULL;
  472. free_rb_tree_fname(&info->root);
  473. file->f_version = inode->i_version;
  474. ret = ext4_htree_fill_tree(file, info->curr_hash,
  475. info->curr_minor_hash,
  476. &info->next_hash);
  477. if (ret < 0)
  478. return ret;
  479. if (ret == 0) {
  480. ctx->pos = ext4_get_htree_eof(file);
  481. break;
  482. }
  483. info->curr_node = rb_first(&info->root);
  484. }
  485. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  486. info->curr_hash = fname->hash;
  487. info->curr_minor_hash = fname->minor_hash;
  488. if (call_filldir(file, ctx, fname))
  489. break;
  490. next_node:
  491. info->curr_node = rb_next(info->curr_node);
  492. if (info->curr_node) {
  493. fname = rb_entry(info->curr_node, struct fname,
  494. rb_hash);
  495. info->curr_hash = fname->hash;
  496. info->curr_minor_hash = fname->minor_hash;
  497. } else {
  498. if (info->next_hash == ~0) {
  499. ctx->pos = ext4_get_htree_eof(file);
  500. break;
  501. }
  502. info->curr_hash = info->next_hash;
  503. info->curr_minor_hash = 0;
  504. }
  505. }
  506. finished:
  507. info->last_pos = ctx->pos;
  508. return 0;
  509. }
  510. static int ext4_release_dir(struct inode *inode, struct file *filp)
  511. {
  512. if (filp->private_data)
  513. ext4_htree_free_dir_info(filp->private_data);
  514. return 0;
  515. }
  516. int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
  517. int buf_size)
  518. {
  519. struct ext4_dir_entry_2 *de;
  520. int nlen, rlen;
  521. unsigned int offset = 0;
  522. char *top;
  523. de = (struct ext4_dir_entry_2 *)buf;
  524. top = buf + buf_size;
  525. while ((char *) de < top) {
  526. if (ext4_check_dir_entry(dir, NULL, de, bh,
  527. buf, buf_size, offset))
  528. return -EIO;
  529. nlen = EXT4_DIR_REC_LEN(de->name_len);
  530. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  531. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  532. offset += rlen;
  533. }
  534. if ((char *) de > top)
  535. return -EIO;
  536. return 0;
  537. }
  538. const struct file_operations ext4_dir_operations = {
  539. .llseek = ext4_dir_llseek,
  540. .read = generic_read_dir,
  541. .iterate = ext4_readdir,
  542. .unlocked_ioctl = ext4_ioctl,
  543. #ifdef CONFIG_COMPAT
  544. .compat_ioctl = ext4_compat_ioctl,
  545. #endif
  546. .fsync = ext4_sync_file,
  547. .release = ext4_release_dir,
  548. };