dir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * linux/fs/ext3/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. * ext3 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/compat.h>
  24. #include "ext3.h"
  25. static unsigned char ext3_filetype_table[] = {
  26. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  27. };
  28. static int ext3_dx_readdir(struct file *, struct dir_context *);
  29. static unsigned char get_dtype(struct super_block *sb, int filetype)
  30. {
  31. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
  32. (filetype >= EXT3_FT_MAX))
  33. return DT_UNKNOWN;
  34. return (ext3_filetype_table[filetype]);
  35. }
  36. /**
  37. * Check if the given dir-inode refers to an htree-indexed directory
  38. * (or a directory which chould potentially get coverted to use htree
  39. * indexing).
  40. *
  41. * Return 1 if it is a dx dir, 0 if not
  42. */
  43. static int is_dx_dir(struct inode *inode)
  44. {
  45. struct super_block *sb = inode->i_sb;
  46. if (EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
  47. EXT3_FEATURE_COMPAT_DIR_INDEX) &&
  48. ((EXT3_I(inode)->i_flags & EXT3_INDEX_FL) ||
  49. ((inode->i_size >> sb->s_blocksize_bits) == 1)))
  50. return 1;
  51. return 0;
  52. }
  53. int ext3_check_dir_entry (const char * function, struct inode * dir,
  54. struct ext3_dir_entry_2 * de,
  55. struct buffer_head * bh,
  56. unsigned long offset)
  57. {
  58. const char * error_msg = NULL;
  59. const int rlen = ext3_rec_len_from_disk(de->rec_len);
  60. if (unlikely(rlen < EXT3_DIR_REC_LEN(1)))
  61. error_msg = "rec_len is smaller than minimal";
  62. else if (unlikely(rlen % 4 != 0))
  63. error_msg = "rec_len % 4 != 0";
  64. else if (unlikely(rlen < EXT3_DIR_REC_LEN(de->name_len)))
  65. error_msg = "rec_len is too small for name_len";
  66. else if (unlikely((((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)))
  67. error_msg = "directory entry across blocks";
  68. else if (unlikely(le32_to_cpu(de->inode) >
  69. le32_to_cpu(EXT3_SB(dir->i_sb)->s_es->s_inodes_count)))
  70. error_msg = "inode out of bounds";
  71. if (unlikely(error_msg != NULL))
  72. ext3_error (dir->i_sb, function,
  73. "bad entry in directory #%lu: %s - "
  74. "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
  75. dir->i_ino, error_msg, offset,
  76. (unsigned long) le32_to_cpu(de->inode),
  77. rlen, de->name_len);
  78. return error_msg == NULL ? 1 : 0;
  79. }
  80. static int ext3_readdir(struct file *file, struct dir_context *ctx)
  81. {
  82. unsigned long offset;
  83. int i;
  84. struct ext3_dir_entry_2 *de;
  85. int err;
  86. struct inode *inode = file_inode(file);
  87. struct super_block *sb = inode->i_sb;
  88. int dir_has_error = 0;
  89. if (is_dx_dir(inode)) {
  90. err = ext3_dx_readdir(file, ctx);
  91. if (err != ERR_BAD_DX_DIR)
  92. return err;
  93. /*
  94. * We don't set the inode dirty flag since it's not
  95. * critical that it get flushed back to the disk.
  96. */
  97. EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
  98. }
  99. offset = ctx->pos & (sb->s_blocksize - 1);
  100. while (ctx->pos < inode->i_size) {
  101. unsigned long blk = ctx->pos >> EXT3_BLOCK_SIZE_BITS(sb);
  102. struct buffer_head map_bh;
  103. struct buffer_head *bh = NULL;
  104. map_bh.b_state = 0;
  105. err = ext3_get_blocks_handle(NULL, inode, blk, 1, &map_bh, 0);
  106. if (err > 0) {
  107. pgoff_t index = map_bh.b_blocknr >>
  108. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  109. if (!ra_has_index(&file->f_ra, index))
  110. page_cache_sync_readahead(
  111. sb->s_bdev->bd_inode->i_mapping,
  112. &file->f_ra, file,
  113. index, 1);
  114. file->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  115. bh = ext3_bread(NULL, inode, blk, 0, &err);
  116. }
  117. /*
  118. * We ignore I/O errors on directories so users have a chance
  119. * of recovering data when there's a bad sector
  120. */
  121. if (!bh) {
  122. if (!dir_has_error) {
  123. ext3_error(sb, __func__, "directory #%lu "
  124. "contains a hole at offset %lld",
  125. inode->i_ino, ctx->pos);
  126. dir_has_error = 1;
  127. }
  128. /* corrupt size? Maybe no more blocks to read */
  129. if (ctx->pos > inode->i_blocks << 9)
  130. break;
  131. ctx->pos += sb->s_blocksize - offset;
  132. continue;
  133. }
  134. /* If the dir block has changed since the last call to
  135. * readdir(2), then we might be pointing to an invalid
  136. * dirent right now. Scan from the start of the block
  137. * to make sure. */
  138. if (offset && file->f_version != inode->i_version) {
  139. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  140. de = (struct ext3_dir_entry_2 *)
  141. (bh->b_data + i);
  142. /* It's too expensive to do a full
  143. * dirent test each time round this
  144. * loop, but we do have to test at
  145. * least that it is non-zero. A
  146. * failure will be detected in the
  147. * dirent test below. */
  148. if (ext3_rec_len_from_disk(de->rec_len) <
  149. EXT3_DIR_REC_LEN(1))
  150. break;
  151. i += ext3_rec_len_from_disk(de->rec_len);
  152. }
  153. offset = i;
  154. ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
  155. | offset;
  156. file->f_version = inode->i_version;
  157. }
  158. while (ctx->pos < inode->i_size
  159. && offset < sb->s_blocksize) {
  160. de = (struct ext3_dir_entry_2 *) (bh->b_data + offset);
  161. if (!ext3_check_dir_entry ("ext3_readdir", inode, de,
  162. bh, offset)) {
  163. /* On error, skip the to the
  164. next block. */
  165. ctx->pos = (ctx->pos |
  166. (sb->s_blocksize - 1)) + 1;
  167. break;
  168. }
  169. offset += ext3_rec_len_from_disk(de->rec_len);
  170. if (le32_to_cpu(de->inode)) {
  171. if (!dir_emit(ctx, de->name, de->name_len,
  172. le32_to_cpu(de->inode),
  173. get_dtype(sb, de->file_type))) {
  174. brelse(bh);
  175. return 0;
  176. }
  177. }
  178. ctx->pos += ext3_rec_len_from_disk(de->rec_len);
  179. }
  180. offset = 0;
  181. brelse (bh);
  182. if (ctx->pos < inode->i_size)
  183. if (!dir_relax(inode))
  184. return 0;
  185. }
  186. return 0;
  187. }
  188. static inline int is_32bit_api(void)
  189. {
  190. #ifdef CONFIG_COMPAT
  191. return is_compat_task();
  192. #else
  193. return (BITS_PER_LONG == 32);
  194. #endif
  195. }
  196. /*
  197. * These functions convert from the major/minor hash to an f_pos
  198. * value for dx directories
  199. *
  200. * Upper layer (for example NFS) should specify FMODE_32BITHASH or
  201. * FMODE_64BITHASH explicitly. On the other hand, we allow ext3 to be mounted
  202. * directly on both 32-bit and 64-bit nodes, under such case, neither
  203. * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
  204. */
  205. static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
  206. {
  207. if ((filp->f_mode & FMODE_32BITHASH) ||
  208. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  209. return major >> 1;
  210. else
  211. return ((__u64)(major >> 1) << 32) | (__u64)minor;
  212. }
  213. static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
  214. {
  215. if ((filp->f_mode & FMODE_32BITHASH) ||
  216. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  217. return (pos << 1) & 0xffffffff;
  218. else
  219. return ((pos >> 32) << 1) & 0xffffffff;
  220. }
  221. static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
  222. {
  223. if ((filp->f_mode & FMODE_32BITHASH) ||
  224. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  225. return 0;
  226. else
  227. return pos & 0xffffffff;
  228. }
  229. /*
  230. * Return 32- or 64-bit end-of-file for dx directories
  231. */
  232. static inline loff_t ext3_get_htree_eof(struct file *filp)
  233. {
  234. if ((filp->f_mode & FMODE_32BITHASH) ||
  235. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  236. return EXT3_HTREE_EOF_32BIT;
  237. else
  238. return EXT3_HTREE_EOF_64BIT;
  239. }
  240. /*
  241. * ext3_dir_llseek() calls generic_file_llseek[_size]() to handle both
  242. * non-htree and htree directories, where the "offset" is in terms
  243. * of the filename hash value instead of the byte offset.
  244. *
  245. * Because we may return a 64-bit hash that is well beyond s_maxbytes,
  246. * we need to pass the max hash as the maximum allowable offset in
  247. * the htree directory case.
  248. *
  249. * NOTE: offsets obtained *before* ext3_set_inode_flag(dir, EXT3_INODE_INDEX)
  250. * will be invalid once the directory was converted into a dx directory
  251. */
  252. loff_t ext3_dir_llseek(struct file *file, loff_t offset, int whence)
  253. {
  254. struct inode *inode = file->f_mapping->host;
  255. int dx_dir = is_dx_dir(inode);
  256. loff_t htree_max = ext3_get_htree_eof(file);
  257. if (likely(dx_dir))
  258. return generic_file_llseek_size(file, offset, whence,
  259. htree_max, htree_max);
  260. else
  261. return generic_file_llseek(file, offset, whence);
  262. }
  263. /*
  264. * This structure holds the nodes of the red-black tree used to store
  265. * the directory entry in hash order.
  266. */
  267. struct fname {
  268. __u32 hash;
  269. __u32 minor_hash;
  270. struct rb_node rb_hash;
  271. struct fname *next;
  272. __u32 inode;
  273. __u8 name_len;
  274. __u8 file_type;
  275. char name[0];
  276. };
  277. /*
  278. * This functoin implements a non-recursive way of freeing all of the
  279. * nodes in the red-black tree.
  280. */
  281. static void free_rb_tree_fname(struct rb_root *root)
  282. {
  283. struct rb_node *n = root->rb_node;
  284. struct rb_node *parent;
  285. struct fname *fname;
  286. while (n) {
  287. /* Do the node's children first */
  288. if (n->rb_left) {
  289. n = n->rb_left;
  290. continue;
  291. }
  292. if (n->rb_right) {
  293. n = n->rb_right;
  294. continue;
  295. }
  296. /*
  297. * The node has no children; free it, and then zero
  298. * out parent's link to it. Finally go to the
  299. * beginning of the loop and try to free the parent
  300. * node.
  301. */
  302. parent = rb_parent(n);
  303. fname = rb_entry(n, struct fname, rb_hash);
  304. while (fname) {
  305. struct fname * old = fname;
  306. fname = fname->next;
  307. kfree (old);
  308. }
  309. if (!parent)
  310. *root = RB_ROOT;
  311. else if (parent->rb_left == n)
  312. parent->rb_left = NULL;
  313. else if (parent->rb_right == n)
  314. parent->rb_right = NULL;
  315. n = parent;
  316. }
  317. }
  318. static struct dir_private_info *ext3_htree_create_dir_info(struct file *filp,
  319. loff_t pos)
  320. {
  321. struct dir_private_info *p;
  322. p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  323. if (!p)
  324. return NULL;
  325. p->curr_hash = pos2maj_hash(filp, pos);
  326. p->curr_minor_hash = pos2min_hash(filp, pos);
  327. return p;
  328. }
  329. void ext3_htree_free_dir_info(struct dir_private_info *p)
  330. {
  331. free_rb_tree_fname(&p->root);
  332. kfree(p);
  333. }
  334. /*
  335. * Given a directory entry, enter it into the fname rb tree.
  336. */
  337. int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
  338. __u32 minor_hash,
  339. struct ext3_dir_entry_2 *dirent)
  340. {
  341. struct rb_node **p, *parent = NULL;
  342. struct fname * fname, *new_fn;
  343. struct dir_private_info *info;
  344. int len;
  345. info = (struct dir_private_info *) dir_file->private_data;
  346. p = &info->root.rb_node;
  347. /* Create and allocate the fname structure */
  348. len = sizeof(struct fname) + dirent->name_len + 1;
  349. new_fn = kzalloc(len, GFP_KERNEL);
  350. if (!new_fn)
  351. return -ENOMEM;
  352. new_fn->hash = hash;
  353. new_fn->minor_hash = minor_hash;
  354. new_fn->inode = le32_to_cpu(dirent->inode);
  355. new_fn->name_len = dirent->name_len;
  356. new_fn->file_type = dirent->file_type;
  357. memcpy(new_fn->name, dirent->name, dirent->name_len);
  358. new_fn->name[dirent->name_len] = 0;
  359. while (*p) {
  360. parent = *p;
  361. fname = rb_entry(parent, struct fname, rb_hash);
  362. /*
  363. * If the hash and minor hash match up, then we put
  364. * them on a linked list. This rarely happens...
  365. */
  366. if ((new_fn->hash == fname->hash) &&
  367. (new_fn->minor_hash == fname->minor_hash)) {
  368. new_fn->next = fname->next;
  369. fname->next = new_fn;
  370. return 0;
  371. }
  372. if (new_fn->hash < fname->hash)
  373. p = &(*p)->rb_left;
  374. else if (new_fn->hash > fname->hash)
  375. p = &(*p)->rb_right;
  376. else if (new_fn->minor_hash < fname->minor_hash)
  377. p = &(*p)->rb_left;
  378. else /* if (new_fn->minor_hash > fname->minor_hash) */
  379. p = &(*p)->rb_right;
  380. }
  381. rb_link_node(&new_fn->rb_hash, parent, p);
  382. rb_insert_color(&new_fn->rb_hash, &info->root);
  383. return 0;
  384. }
  385. /*
  386. * This is a helper function for ext3_dx_readdir. It calls filldir
  387. * for all entres on the fname linked list. (Normally there is only
  388. * one entry on the linked list, unless there are 62 bit hash collisions.)
  389. */
  390. static bool call_filldir(struct file *file, struct dir_context *ctx,
  391. struct fname *fname)
  392. {
  393. struct dir_private_info *info = file->private_data;
  394. struct inode *inode = file_inode(file);
  395. struct super_block *sb = inode->i_sb;
  396. if (!fname) {
  397. printk("call_filldir: called with null fname?!?\n");
  398. return true;
  399. }
  400. ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
  401. while (fname) {
  402. if (!dir_emit(ctx, fname->name, fname->name_len,
  403. fname->inode,
  404. get_dtype(sb, fname->file_type))) {
  405. info->extra_fname = fname;
  406. return false;
  407. }
  408. fname = fname->next;
  409. }
  410. return true;
  411. }
  412. static int ext3_dx_readdir(struct file *file, struct dir_context *ctx)
  413. {
  414. struct dir_private_info *info = file->private_data;
  415. struct inode *inode = file_inode(file);
  416. struct fname *fname;
  417. int ret;
  418. if (!info) {
  419. info = ext3_htree_create_dir_info(file, ctx->pos);
  420. if (!info)
  421. return -ENOMEM;
  422. file->private_data = info;
  423. }
  424. if (ctx->pos == ext3_get_htree_eof(file))
  425. return 0; /* EOF */
  426. /* Some one has messed with f_pos; reset the world */
  427. if (info->last_pos != ctx->pos) {
  428. free_rb_tree_fname(&info->root);
  429. info->curr_node = NULL;
  430. info->extra_fname = NULL;
  431. info->curr_hash = pos2maj_hash(file, ctx->pos);
  432. info->curr_minor_hash = pos2min_hash(file, ctx->pos);
  433. }
  434. /*
  435. * If there are any leftover names on the hash collision
  436. * chain, return them first.
  437. */
  438. if (info->extra_fname) {
  439. if (!call_filldir(file, ctx, info->extra_fname))
  440. goto finished;
  441. info->extra_fname = NULL;
  442. goto next_node;
  443. } else if (!info->curr_node)
  444. info->curr_node = rb_first(&info->root);
  445. while (1) {
  446. /*
  447. * Fill the rbtree if we have no more entries,
  448. * or the inode has changed since we last read in the
  449. * cached entries.
  450. */
  451. if ((!info->curr_node) ||
  452. (file->f_version != inode->i_version)) {
  453. info->curr_node = NULL;
  454. free_rb_tree_fname(&info->root);
  455. file->f_version = inode->i_version;
  456. ret = ext3_htree_fill_tree(file, info->curr_hash,
  457. info->curr_minor_hash,
  458. &info->next_hash);
  459. if (ret < 0)
  460. return ret;
  461. if (ret == 0) {
  462. ctx->pos = ext3_get_htree_eof(file);
  463. break;
  464. }
  465. info->curr_node = rb_first(&info->root);
  466. }
  467. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  468. info->curr_hash = fname->hash;
  469. info->curr_minor_hash = fname->minor_hash;
  470. if (!call_filldir(file, ctx, fname))
  471. break;
  472. next_node:
  473. info->curr_node = rb_next(info->curr_node);
  474. if (info->curr_node) {
  475. fname = rb_entry(info->curr_node, struct fname,
  476. rb_hash);
  477. info->curr_hash = fname->hash;
  478. info->curr_minor_hash = fname->minor_hash;
  479. } else {
  480. if (info->next_hash == ~0) {
  481. ctx->pos = ext3_get_htree_eof(file);
  482. break;
  483. }
  484. info->curr_hash = info->next_hash;
  485. info->curr_minor_hash = 0;
  486. }
  487. }
  488. finished:
  489. info->last_pos = ctx->pos;
  490. return 0;
  491. }
  492. static int ext3_release_dir (struct inode * inode, struct file * filp)
  493. {
  494. if (filp->private_data)
  495. ext3_htree_free_dir_info(filp->private_data);
  496. return 0;
  497. }
  498. const struct file_operations ext3_dir_operations = {
  499. .llseek = ext3_dir_llseek,
  500. .read = generic_read_dir,
  501. .iterate = ext3_readdir,
  502. .unlocked_ioctl = ext3_ioctl,
  503. #ifdef CONFIG_COMPAT
  504. .compat_ioctl = ext3_compat_ioctl,
  505. #endif
  506. .fsync = ext3_sync_file,
  507. .release = ext3_release_dir,
  508. };