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. #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. 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 *filp,
  96. void *dirent, filldir_t filldir)
  97. {
  98. int error = 0;
  99. unsigned int offset;
  100. int i, stored;
  101. struct ext4_dir_entry_2 *de;
  102. int err;
  103. struct inode *inode = file_inode(filp);
  104. struct super_block *sb = inode->i_sb;
  105. int ret = 0;
  106. int dir_has_error = 0;
  107. if (ext4_has_inline_data(inode)) {
  108. int has_inline_data = 1;
  109. ret = ext4_read_inline_dir(filp, dirent, filldir,
  110. &has_inline_data);
  111. if (has_inline_data)
  112. return ret;
  113. }
  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(file_inode(filp),
  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. brelse(bh);
  173. continue;
  174. }
  175. set_buffer_verified(bh);
  176. revalidate:
  177. /* If the dir block has changed since the last call to
  178. * readdir(2), then we might be pointing to an invalid
  179. * dirent right now. Scan from the start of the block
  180. * to make sure. */
  181. if (filp->f_version != inode->i_version) {
  182. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  183. de = (struct ext4_dir_entry_2 *)
  184. (bh->b_data + i);
  185. /* It's too expensive to do a full
  186. * dirent test each time round this
  187. * loop, but we do have to test at
  188. * least that it is non-zero. A
  189. * failure will be detected in the
  190. * dirent test below. */
  191. if (ext4_rec_len_from_disk(de->rec_len,
  192. sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
  193. break;
  194. i += ext4_rec_len_from_disk(de->rec_len,
  195. sb->s_blocksize);
  196. }
  197. offset = i;
  198. filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
  199. | offset;
  200. filp->f_version = inode->i_version;
  201. }
  202. while (!error && filp->f_pos < inode->i_size
  203. && offset < sb->s_blocksize) {
  204. de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
  205. if (ext4_check_dir_entry(inode, filp, de, bh,
  206. bh->b_data, bh->b_size,
  207. offset)) {
  208. /*
  209. * On error, skip the f_pos to the next block
  210. */
  211. filp->f_pos = (filp->f_pos |
  212. (sb->s_blocksize - 1)) + 1;
  213. brelse(bh);
  214. ret = stored;
  215. goto out;
  216. }
  217. offset += ext4_rec_len_from_disk(de->rec_len,
  218. sb->s_blocksize);
  219. if (le32_to_cpu(de->inode)) {
  220. /* We might block in the next section
  221. * if the data destination is
  222. * currently swapped out. So, use a
  223. * version stamp to detect whether or
  224. * not the directory has been modified
  225. * during the copy operation.
  226. */
  227. u64 version = filp->f_version;
  228. error = filldir(dirent, de->name,
  229. de->name_len,
  230. filp->f_pos,
  231. le32_to_cpu(de->inode),
  232. get_dtype(sb, de->file_type));
  233. if (error)
  234. break;
  235. if (version != filp->f_version)
  236. goto revalidate;
  237. stored++;
  238. }
  239. filp->f_pos += ext4_rec_len_from_disk(de->rec_len,
  240. sb->s_blocksize);
  241. }
  242. offset = 0;
  243. brelse(bh);
  244. }
  245. out:
  246. return ret;
  247. }
  248. static inline int is_32bit_api(void)
  249. {
  250. #ifdef CONFIG_COMPAT
  251. return is_compat_task();
  252. #else
  253. return (BITS_PER_LONG == 32);
  254. #endif
  255. }
  256. /*
  257. * These functions convert from the major/minor hash to an f_pos
  258. * value for dx directories
  259. *
  260. * Upper layer (for example NFS) should specify FMODE_32BITHASH or
  261. * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
  262. * directly on both 32-bit and 64-bit nodes, under such case, neither
  263. * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
  264. */
  265. static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
  266. {
  267. if ((filp->f_mode & FMODE_32BITHASH) ||
  268. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  269. return major >> 1;
  270. else
  271. return ((__u64)(major >> 1) << 32) | (__u64)minor;
  272. }
  273. static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
  274. {
  275. if ((filp->f_mode & FMODE_32BITHASH) ||
  276. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  277. return (pos << 1) & 0xffffffff;
  278. else
  279. return ((pos >> 32) << 1) & 0xffffffff;
  280. }
  281. static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
  282. {
  283. if ((filp->f_mode & FMODE_32BITHASH) ||
  284. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  285. return 0;
  286. else
  287. return pos & 0xffffffff;
  288. }
  289. /*
  290. * Return 32- or 64-bit end-of-file for dx directories
  291. */
  292. static inline loff_t ext4_get_htree_eof(struct file *filp)
  293. {
  294. if ((filp->f_mode & FMODE_32BITHASH) ||
  295. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  296. return EXT4_HTREE_EOF_32BIT;
  297. else
  298. return EXT4_HTREE_EOF_64BIT;
  299. }
  300. /*
  301. * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
  302. * directories, where the "offset" is in terms of the filename hash
  303. * value instead of the byte offset.
  304. *
  305. * Because we may return a 64-bit hash that is well beyond offset limits,
  306. * we need to pass the max hash as the maximum allowable offset in
  307. * the htree directory case.
  308. *
  309. * For non-htree, ext4_llseek already chooses the proper max offset.
  310. */
  311. static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
  312. {
  313. struct inode *inode = file->f_mapping->host;
  314. int dx_dir = is_dx_dir(inode);
  315. loff_t htree_max = ext4_get_htree_eof(file);
  316. if (likely(dx_dir))
  317. return generic_file_llseek_size(file, offset, whence,
  318. htree_max, htree_max);
  319. else
  320. return ext4_llseek(file, offset, whence);
  321. }
  322. /*
  323. * This structure holds the nodes of the red-black tree used to store
  324. * the directory entry in hash order.
  325. */
  326. struct fname {
  327. __u32 hash;
  328. __u32 minor_hash;
  329. struct rb_node rb_hash;
  330. struct fname *next;
  331. __u32 inode;
  332. __u8 name_len;
  333. __u8 file_type;
  334. char name[0];
  335. };
  336. /*
  337. * This functoin implements a non-recursive way of freeing all of the
  338. * nodes in the red-black tree.
  339. */
  340. static void free_rb_tree_fname(struct rb_root *root)
  341. {
  342. struct rb_node *n = root->rb_node;
  343. struct rb_node *parent;
  344. struct fname *fname;
  345. while (n) {
  346. /* Do the node's children first */
  347. if (n->rb_left) {
  348. n = n->rb_left;
  349. continue;
  350. }
  351. if (n->rb_right) {
  352. n = n->rb_right;
  353. continue;
  354. }
  355. /*
  356. * The node has no children; free it, and then zero
  357. * out parent's link to it. Finally go to the
  358. * beginning of the loop and try to free the parent
  359. * node.
  360. */
  361. parent = rb_parent(n);
  362. fname = rb_entry(n, struct fname, rb_hash);
  363. while (fname) {
  364. struct fname *old = fname;
  365. fname = fname->next;
  366. kfree(old);
  367. }
  368. if (!parent)
  369. *root = RB_ROOT;
  370. else if (parent->rb_left == n)
  371. parent->rb_left = NULL;
  372. else if (parent->rb_right == n)
  373. parent->rb_right = NULL;
  374. n = parent;
  375. }
  376. }
  377. static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
  378. loff_t pos)
  379. {
  380. struct dir_private_info *p;
  381. p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  382. if (!p)
  383. return NULL;
  384. p->curr_hash = pos2maj_hash(filp, pos);
  385. p->curr_minor_hash = pos2min_hash(filp, pos);
  386. return p;
  387. }
  388. void ext4_htree_free_dir_info(struct dir_private_info *p)
  389. {
  390. free_rb_tree_fname(&p->root);
  391. kfree(p);
  392. }
  393. /*
  394. * Given a directory entry, enter it into the fname rb tree.
  395. */
  396. int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  397. __u32 minor_hash,
  398. struct ext4_dir_entry_2 *dirent)
  399. {
  400. struct rb_node **p, *parent = NULL;
  401. struct fname *fname, *new_fn;
  402. struct dir_private_info *info;
  403. int len;
  404. info = dir_file->private_data;
  405. p = &info->root.rb_node;
  406. /* Create and allocate the fname structure */
  407. len = sizeof(struct fname) + dirent->name_len + 1;
  408. new_fn = kzalloc(len, GFP_KERNEL);
  409. if (!new_fn)
  410. return -ENOMEM;
  411. new_fn->hash = hash;
  412. new_fn->minor_hash = minor_hash;
  413. new_fn->inode = le32_to_cpu(dirent->inode);
  414. new_fn->name_len = dirent->name_len;
  415. new_fn->file_type = dirent->file_type;
  416. memcpy(new_fn->name, dirent->name, dirent->name_len);
  417. new_fn->name[dirent->name_len] = 0;
  418. while (*p) {
  419. parent = *p;
  420. fname = rb_entry(parent, struct fname, rb_hash);
  421. /*
  422. * If the hash and minor hash match up, then we put
  423. * them on a linked list. This rarely happens...
  424. */
  425. if ((new_fn->hash == fname->hash) &&
  426. (new_fn->minor_hash == fname->minor_hash)) {
  427. new_fn->next = fname->next;
  428. fname->next = new_fn;
  429. return 0;
  430. }
  431. if (new_fn->hash < fname->hash)
  432. p = &(*p)->rb_left;
  433. else if (new_fn->hash > fname->hash)
  434. p = &(*p)->rb_right;
  435. else if (new_fn->minor_hash < fname->minor_hash)
  436. p = &(*p)->rb_left;
  437. else /* if (new_fn->minor_hash > fname->minor_hash) */
  438. p = &(*p)->rb_right;
  439. }
  440. rb_link_node(&new_fn->rb_hash, parent, p);
  441. rb_insert_color(&new_fn->rb_hash, &info->root);
  442. return 0;
  443. }
  444. /*
  445. * This is a helper function for ext4_dx_readdir. It calls filldir
  446. * for all entres on the fname linked list. (Normally there is only
  447. * one entry on the linked list, unless there are 62 bit hash collisions.)
  448. */
  449. static int call_filldir(struct file *filp, void *dirent,
  450. filldir_t filldir, struct fname *fname)
  451. {
  452. struct dir_private_info *info = filp->private_data;
  453. loff_t curr_pos;
  454. struct inode *inode = file_inode(filp);
  455. struct super_block *sb;
  456. int error;
  457. sb = inode->i_sb;
  458. if (!fname) {
  459. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
  460. "called with null fname?!?", __func__, __LINE__,
  461. inode->i_ino, current->comm);
  462. return 0;
  463. }
  464. curr_pos = hash2pos(filp, fname->hash, fname->minor_hash);
  465. while (fname) {
  466. error = filldir(dirent, fname->name,
  467. fname->name_len, curr_pos,
  468. fname->inode,
  469. get_dtype(sb, fname->file_type));
  470. if (error) {
  471. filp->f_pos = curr_pos;
  472. info->extra_fname = fname;
  473. return error;
  474. }
  475. fname = fname->next;
  476. }
  477. return 0;
  478. }
  479. static int ext4_dx_readdir(struct file *filp,
  480. void *dirent, filldir_t filldir)
  481. {
  482. struct dir_private_info *info = filp->private_data;
  483. struct inode *inode = file_inode(filp);
  484. struct fname *fname;
  485. int ret;
  486. if (!info) {
  487. info = ext4_htree_create_dir_info(filp, filp->f_pos);
  488. if (!info)
  489. return -ENOMEM;
  490. filp->private_data = info;
  491. }
  492. if (filp->f_pos == ext4_get_htree_eof(filp))
  493. return 0; /* EOF */
  494. /* Some one has messed with f_pos; reset the world */
  495. if (info->last_pos != filp->f_pos) {
  496. free_rb_tree_fname(&info->root);
  497. info->curr_node = NULL;
  498. info->extra_fname = NULL;
  499. info->curr_hash = pos2maj_hash(filp, filp->f_pos);
  500. info->curr_minor_hash = pos2min_hash(filp, filp->f_pos);
  501. }
  502. /*
  503. * If there are any leftover names on the hash collision
  504. * chain, return them first.
  505. */
  506. if (info->extra_fname) {
  507. if (call_filldir(filp, dirent, filldir, info->extra_fname))
  508. goto finished;
  509. info->extra_fname = NULL;
  510. goto next_node;
  511. } else if (!info->curr_node)
  512. info->curr_node = rb_first(&info->root);
  513. while (1) {
  514. /*
  515. * Fill the rbtree if we have no more entries,
  516. * or the inode has changed since we last read in the
  517. * cached entries.
  518. */
  519. if ((!info->curr_node) ||
  520. (filp->f_version != inode->i_version)) {
  521. info->curr_node = NULL;
  522. free_rb_tree_fname(&info->root);
  523. filp->f_version = inode->i_version;
  524. ret = ext4_htree_fill_tree(filp, info->curr_hash,
  525. info->curr_minor_hash,
  526. &info->next_hash);
  527. if (ret < 0)
  528. return ret;
  529. if (ret == 0) {
  530. filp->f_pos = ext4_get_htree_eof(filp);
  531. break;
  532. }
  533. info->curr_node = rb_first(&info->root);
  534. }
  535. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  536. info->curr_hash = fname->hash;
  537. info->curr_minor_hash = fname->minor_hash;
  538. if (call_filldir(filp, dirent, filldir, fname))
  539. break;
  540. next_node:
  541. info->curr_node = rb_next(info->curr_node);
  542. if (info->curr_node) {
  543. fname = rb_entry(info->curr_node, struct fname,
  544. rb_hash);
  545. info->curr_hash = fname->hash;
  546. info->curr_minor_hash = fname->minor_hash;
  547. } else {
  548. if (info->next_hash == ~0) {
  549. filp->f_pos = ext4_get_htree_eof(filp);
  550. break;
  551. }
  552. info->curr_hash = info->next_hash;
  553. info->curr_minor_hash = 0;
  554. }
  555. }
  556. finished:
  557. info->last_pos = filp->f_pos;
  558. return 0;
  559. }
  560. static int ext4_release_dir(struct inode *inode, struct file *filp)
  561. {
  562. if (filp->private_data)
  563. ext4_htree_free_dir_info(filp->private_data);
  564. return 0;
  565. }
  566. const struct file_operations ext4_dir_operations = {
  567. .llseek = ext4_dir_llseek,
  568. .read = generic_read_dir,
  569. .readdir = ext4_readdir,
  570. .unlocked_ioctl = ext4_ioctl,
  571. #ifdef CONFIG_COMPAT
  572. .compat_ioctl = ext4_compat_ioctl,
  573. #endif
  574. .fsync = ext4_sync_file,
  575. .release = ext4_release_dir,
  576. };