dir.c 14 KB

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