dir.c 16 KB

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