dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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_readdir(struct file *, void *, filldir_t);
  33. static int ext4_dx_readdir(struct file *filp,
  34. void *dirent, filldir_t filldir);
  35. static int ext4_release_dir(struct inode *inode,
  36. struct file *filp);
  37. const struct file_operations ext4_dir_operations = {
  38. .llseek = ext4_llseek,
  39. .read = generic_read_dir,
  40. .readdir = ext4_readdir, /* we take BKL. needed?*/
  41. .unlocked_ioctl = ext4_ioctl,
  42. #ifdef CONFIG_COMPAT
  43. .compat_ioctl = ext4_compat_ioctl,
  44. #endif
  45. .fsync = ext4_sync_file,
  46. .release = ext4_release_dir,
  47. };
  48. static unsigned char get_dtype(struct super_block *sb, int filetype)
  49. {
  50. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE) ||
  51. (filetype >= EXT4_FT_MAX))
  52. return DT_UNKNOWN;
  53. return (ext4_filetype_table[filetype]);
  54. }
  55. /*
  56. * Return 0 if the directory entry is OK, and 1 if there is a problem
  57. *
  58. * Note: this is the opposite of what ext2 and ext3 historically returned...
  59. */
  60. int __ext4_check_dir_entry(const char *function, unsigned int line,
  61. struct inode *dir, struct file *filp,
  62. struct ext4_dir_entry_2 *de,
  63. struct buffer_head *bh,
  64. unsigned int offset)
  65. {
  66. const char *error_msg = NULL;
  67. const int rlen = ext4_rec_len_from_disk(de->rec_len,
  68. dir->i_sb->s_blocksize);
  69. if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
  70. error_msg = "rec_len is smaller than minimal";
  71. else if (unlikely(rlen % 4 != 0))
  72. error_msg = "rec_len % 4 != 0";
  73. else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
  74. error_msg = "rec_len is too small for name_len";
  75. else if (unlikely(((char *) de - bh->b_data) + rlen >
  76. dir->i_sb->s_blocksize))
  77. error_msg = "directory entry across blocks";
  78. else if (unlikely(le32_to_cpu(de->inode) >
  79. le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
  80. error_msg = "inode out of bounds";
  81. else
  82. return 0;
  83. if (filp)
  84. ext4_error_file(filp, function, line, bh ? bh->b_blocknr : 0,
  85. "bad entry in directory: %s - offset=%u(%u), "
  86. "inode=%u, rec_len=%d, name_len=%d",
  87. error_msg, (unsigned) (offset%bh->b_size),
  88. offset, le32_to_cpu(de->inode),
  89. rlen, de->name_len);
  90. else
  91. ext4_error_inode(dir, function, line, bh ? bh->b_blocknr : 0,
  92. "bad entry in directory: %s - offset=%u(%u), "
  93. "inode=%u, rec_len=%d, name_len=%d",
  94. error_msg, (unsigned) (offset%bh->b_size),
  95. offset, le32_to_cpu(de->inode),
  96. rlen, de->name_len);
  97. return 1;
  98. }
  99. static int ext4_readdir(struct file *filp,
  100. void *dirent, filldir_t filldir)
  101. {
  102. int error = 0;
  103. unsigned int offset;
  104. int i, stored;
  105. struct ext4_dir_entry_2 *de;
  106. struct super_block *sb;
  107. int err;
  108. struct inode *inode = filp->f_path.dentry->d_inode;
  109. int ret = 0;
  110. int dir_has_error = 0;
  111. sb = inode->i_sb;
  112. if (EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  113. EXT4_FEATURE_COMPAT_DIR_INDEX) &&
  114. ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
  115. ((inode->i_size >> sb->s_blocksize_bits) == 1))) {
  116. err = ext4_dx_readdir(filp, dirent, filldir);
  117. if (err != ERR_BAD_DX_DIR) {
  118. ret = err;
  119. goto out;
  120. }
  121. /*
  122. * We don't set the inode dirty flag since it's not
  123. * critical that it get flushed back to the disk.
  124. */
  125. ext4_clear_inode_flag(filp->f_path.dentry->d_inode,
  126. EXT4_INODE_INDEX);
  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. revalidate:
  166. /* If the dir block has changed since the last call to
  167. * readdir(2), then we might be pointing to an invalid
  168. * dirent right now. Scan from the start of the block
  169. * to make sure. */
  170. if (filp->f_version != inode->i_version) {
  171. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  172. de = (struct ext4_dir_entry_2 *)
  173. (bh->b_data + i);
  174. /* It's too expensive to do a full
  175. * dirent test each time round this
  176. * loop, but we do have to test at
  177. * least that it is non-zero. A
  178. * failure will be detected in the
  179. * dirent test below. */
  180. if (ext4_rec_len_from_disk(de->rec_len,
  181. sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
  182. break;
  183. i += ext4_rec_len_from_disk(de->rec_len,
  184. sb->s_blocksize);
  185. }
  186. offset = i;
  187. filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
  188. | offset;
  189. filp->f_version = inode->i_version;
  190. }
  191. while (!error && filp->f_pos < inode->i_size
  192. && offset < sb->s_blocksize) {
  193. de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
  194. if (ext4_check_dir_entry(inode, filp, de,
  195. bh, offset)) {
  196. /*
  197. * On error, skip the f_pos to the next block
  198. */
  199. filp->f_pos = (filp->f_pos |
  200. (sb->s_blocksize - 1)) + 1;
  201. brelse(bh);
  202. ret = stored;
  203. goto out;
  204. }
  205. offset += ext4_rec_len_from_disk(de->rec_len,
  206. sb->s_blocksize);
  207. if (le32_to_cpu(de->inode)) {
  208. /* We might block in the next section
  209. * if the data destination is
  210. * currently swapped out. So, use a
  211. * version stamp to detect whether or
  212. * not the directory has been modified
  213. * during the copy operation.
  214. */
  215. u64 version = filp->f_version;
  216. error = filldir(dirent, de->name,
  217. de->name_len,
  218. filp->f_pos,
  219. le32_to_cpu(de->inode),
  220. get_dtype(sb, de->file_type));
  221. if (error)
  222. break;
  223. if (version != filp->f_version)
  224. goto revalidate;
  225. stored++;
  226. }
  227. filp->f_pos += ext4_rec_len_from_disk(de->rec_len,
  228. sb->s_blocksize);
  229. }
  230. offset = 0;
  231. brelse(bh);
  232. }
  233. out:
  234. return ret;
  235. }
  236. /*
  237. * These functions convert from the major/minor hash to an f_pos
  238. * value.
  239. *
  240. * Currently we only use major hash numer. This is unfortunate, but
  241. * on 32-bit machines, the same VFS interface is used for lseek and
  242. * llseek, so if we use the 64 bit offset, then the 32-bit versions of
  243. * lseek/telldir/seekdir will blow out spectacularly, and from within
  244. * the ext2 low-level routine, we don't know if we're being called by
  245. * a 64-bit version of the system call or the 32-bit version of the
  246. * system call. Worse yet, NFSv2 only allows for a 32-bit readdir
  247. * cookie. Sigh.
  248. */
  249. #define hash2pos(major, minor) (major >> 1)
  250. #define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
  251. #define pos2min_hash(pos) (0)
  252. /*
  253. * This structure holds the nodes of the red-black tree used to store
  254. * the directory entry in hash order.
  255. */
  256. struct fname {
  257. __u32 hash;
  258. __u32 minor_hash;
  259. struct rb_node rb_hash;
  260. struct fname *next;
  261. __u32 inode;
  262. __u8 name_len;
  263. __u8 file_type;
  264. char name[0];
  265. };
  266. /*
  267. * This functoin implements a non-recursive way of freeing all of the
  268. * nodes in the red-black tree.
  269. */
  270. static void free_rb_tree_fname(struct rb_root *root)
  271. {
  272. struct rb_node *n = root->rb_node;
  273. struct rb_node *parent;
  274. struct fname *fname;
  275. while (n) {
  276. /* Do the node's children first */
  277. if (n->rb_left) {
  278. n = n->rb_left;
  279. continue;
  280. }
  281. if (n->rb_right) {
  282. n = n->rb_right;
  283. continue;
  284. }
  285. /*
  286. * The node has no children; free it, and then zero
  287. * out parent's link to it. Finally go to the
  288. * beginning of the loop and try to free the parent
  289. * node.
  290. */
  291. parent = rb_parent(n);
  292. fname = rb_entry(n, struct fname, rb_hash);
  293. while (fname) {
  294. struct fname *old = fname;
  295. fname = fname->next;
  296. kfree(old);
  297. }
  298. if (!parent)
  299. *root = RB_ROOT;
  300. else if (parent->rb_left == n)
  301. parent->rb_left = NULL;
  302. else if (parent->rb_right == n)
  303. parent->rb_right = NULL;
  304. n = parent;
  305. }
  306. }
  307. static struct dir_private_info *ext4_htree_create_dir_info(loff_t pos)
  308. {
  309. struct dir_private_info *p;
  310. p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  311. if (!p)
  312. return NULL;
  313. p->curr_hash = pos2maj_hash(pos);
  314. p->curr_minor_hash = pos2min_hash(pos);
  315. return p;
  316. }
  317. void ext4_htree_free_dir_info(struct dir_private_info *p)
  318. {
  319. free_rb_tree_fname(&p->root);
  320. kfree(p);
  321. }
  322. /*
  323. * Given a directory entry, enter it into the fname rb tree.
  324. */
  325. int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  326. __u32 minor_hash,
  327. struct ext4_dir_entry_2 *dirent)
  328. {
  329. struct rb_node **p, *parent = NULL;
  330. struct fname *fname, *new_fn;
  331. struct dir_private_info *info;
  332. int len;
  333. info = dir_file->private_data;
  334. p = &info->root.rb_node;
  335. /* Create and allocate the fname structure */
  336. len = sizeof(struct fname) + dirent->name_len + 1;
  337. new_fn = kzalloc(len, GFP_KERNEL);
  338. if (!new_fn)
  339. return -ENOMEM;
  340. new_fn->hash = hash;
  341. new_fn->minor_hash = minor_hash;
  342. new_fn->inode = le32_to_cpu(dirent->inode);
  343. new_fn->name_len = dirent->name_len;
  344. new_fn->file_type = dirent->file_type;
  345. memcpy(new_fn->name, dirent->name, dirent->name_len);
  346. new_fn->name[dirent->name_len] = 0;
  347. while (*p) {
  348. parent = *p;
  349. fname = rb_entry(parent, struct fname, rb_hash);
  350. /*
  351. * If the hash and minor hash match up, then we put
  352. * them on a linked list. This rarely happens...
  353. */
  354. if ((new_fn->hash == fname->hash) &&
  355. (new_fn->minor_hash == fname->minor_hash)) {
  356. new_fn->next = fname->next;
  357. fname->next = new_fn;
  358. return 0;
  359. }
  360. if (new_fn->hash < fname->hash)
  361. p = &(*p)->rb_left;
  362. else if (new_fn->hash > fname->hash)
  363. p = &(*p)->rb_right;
  364. else if (new_fn->minor_hash < fname->minor_hash)
  365. p = &(*p)->rb_left;
  366. else /* if (new_fn->minor_hash > fname->minor_hash) */
  367. p = &(*p)->rb_right;
  368. }
  369. rb_link_node(&new_fn->rb_hash, parent, p);
  370. rb_insert_color(&new_fn->rb_hash, &info->root);
  371. return 0;
  372. }
  373. /*
  374. * This is a helper function for ext4_dx_readdir. It calls filldir
  375. * for all entres on the fname linked list. (Normally there is only
  376. * one entry on the linked list, unless there are 62 bit hash collisions.)
  377. */
  378. static int call_filldir(struct file *filp, void *dirent,
  379. filldir_t filldir, struct fname *fname)
  380. {
  381. struct dir_private_info *info = filp->private_data;
  382. loff_t curr_pos;
  383. struct inode *inode = filp->f_path.dentry->d_inode;
  384. struct super_block *sb;
  385. int error;
  386. sb = inode->i_sb;
  387. if (!fname) {
  388. printk(KERN_ERR "EXT4-fs: call_filldir: called with "
  389. "null fname?!?\n");
  390. return 0;
  391. }
  392. curr_pos = hash2pos(fname->hash, fname->minor_hash);
  393. while (fname) {
  394. error = filldir(dirent, fname->name,
  395. fname->name_len, curr_pos,
  396. fname->inode,
  397. get_dtype(sb, fname->file_type));
  398. if (error) {
  399. filp->f_pos = curr_pos;
  400. info->extra_fname = fname;
  401. return error;
  402. }
  403. fname = fname->next;
  404. }
  405. return 0;
  406. }
  407. static int ext4_dx_readdir(struct file *filp,
  408. void *dirent, filldir_t filldir)
  409. {
  410. struct dir_private_info *info = filp->private_data;
  411. struct inode *inode = filp->f_path.dentry->d_inode;
  412. struct fname *fname;
  413. int ret;
  414. if (!info) {
  415. info = ext4_htree_create_dir_info(filp->f_pos);
  416. if (!info)
  417. return -ENOMEM;
  418. filp->private_data = info;
  419. }
  420. if (filp->f_pos == EXT4_HTREE_EOF)
  421. return 0; /* EOF */
  422. /* Some one has messed with f_pos; reset the world */
  423. if (info->last_pos != filp->f_pos) {
  424. free_rb_tree_fname(&info->root);
  425. info->curr_node = NULL;
  426. info->extra_fname = NULL;
  427. info->curr_hash = pos2maj_hash(filp->f_pos);
  428. info->curr_minor_hash = pos2min_hash(filp->f_pos);
  429. }
  430. /*
  431. * If there are any leftover names on the hash collision
  432. * chain, return them first.
  433. */
  434. if (info->extra_fname) {
  435. if (call_filldir(filp, dirent, filldir, info->extra_fname))
  436. goto finished;
  437. info->extra_fname = NULL;
  438. goto next_node;
  439. } else if (!info->curr_node)
  440. info->curr_node = rb_first(&info->root);
  441. while (1) {
  442. /*
  443. * Fill the rbtree if we have no more entries,
  444. * or the inode has changed since we last read in the
  445. * cached entries.
  446. */
  447. if ((!info->curr_node) ||
  448. (filp->f_version != inode->i_version)) {
  449. info->curr_node = NULL;
  450. free_rb_tree_fname(&info->root);
  451. filp->f_version = inode->i_version;
  452. ret = ext4_htree_fill_tree(filp, info->curr_hash,
  453. info->curr_minor_hash,
  454. &info->next_hash);
  455. if (ret < 0)
  456. return ret;
  457. if (ret == 0) {
  458. filp->f_pos = EXT4_HTREE_EOF;
  459. break;
  460. }
  461. info->curr_node = rb_first(&info->root);
  462. }
  463. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  464. info->curr_hash = fname->hash;
  465. info->curr_minor_hash = fname->minor_hash;
  466. if (call_filldir(filp, dirent, filldir, fname))
  467. break;
  468. next_node:
  469. info->curr_node = rb_next(info->curr_node);
  470. if (info->curr_node) {
  471. fname = rb_entry(info->curr_node, struct fname,
  472. rb_hash);
  473. info->curr_hash = fname->hash;
  474. info->curr_minor_hash = fname->minor_hash;
  475. } else {
  476. if (info->next_hash == ~0) {
  477. filp->f_pos = EXT4_HTREE_EOF;
  478. break;
  479. }
  480. info->curr_hash = info->next_hash;
  481. info->curr_minor_hash = 0;
  482. }
  483. }
  484. finished:
  485. info->last_pos = filp->f_pos;
  486. return 0;
  487. }
  488. static int ext4_release_dir(struct inode *inode, struct file *filp)
  489. {
  490. if (filp->private_data)
  491. ext4_htree_free_dir_info(filp->private_data);
  492. return 0;
  493. }