dir.c 16 KB

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