dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 could potentially get converted 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. static 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 fname *fname, *next;
  284. rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
  285. do {
  286. struct fname *old = fname;
  287. fname = fname->next;
  288. kfree(old);
  289. } while (fname);
  290. *root = RB_ROOT;
  291. }
  292. static struct dir_private_info *ext3_htree_create_dir_info(struct file *filp,
  293. loff_t pos)
  294. {
  295. struct dir_private_info *p;
  296. p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  297. if (!p)
  298. return NULL;
  299. p->curr_hash = pos2maj_hash(filp, pos);
  300. p->curr_minor_hash = pos2min_hash(filp, pos);
  301. return p;
  302. }
  303. void ext3_htree_free_dir_info(struct dir_private_info *p)
  304. {
  305. free_rb_tree_fname(&p->root);
  306. kfree(p);
  307. }
  308. /*
  309. * Given a directory entry, enter it into the fname rb tree.
  310. */
  311. int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
  312. __u32 minor_hash,
  313. struct ext3_dir_entry_2 *dirent)
  314. {
  315. struct rb_node **p, *parent = NULL;
  316. struct fname * fname, *new_fn;
  317. struct dir_private_info *info;
  318. int len;
  319. info = (struct dir_private_info *) dir_file->private_data;
  320. p = &info->root.rb_node;
  321. /* Create and allocate the fname structure */
  322. len = sizeof(struct fname) + dirent->name_len + 1;
  323. new_fn = kzalloc(len, GFP_KERNEL);
  324. if (!new_fn)
  325. return -ENOMEM;
  326. new_fn->hash = hash;
  327. new_fn->minor_hash = minor_hash;
  328. new_fn->inode = le32_to_cpu(dirent->inode);
  329. new_fn->name_len = dirent->name_len;
  330. new_fn->file_type = dirent->file_type;
  331. memcpy(new_fn->name, dirent->name, dirent->name_len);
  332. new_fn->name[dirent->name_len] = 0;
  333. while (*p) {
  334. parent = *p;
  335. fname = rb_entry(parent, struct fname, rb_hash);
  336. /*
  337. * If the hash and minor hash match up, then we put
  338. * them on a linked list. This rarely happens...
  339. */
  340. if ((new_fn->hash == fname->hash) &&
  341. (new_fn->minor_hash == fname->minor_hash)) {
  342. new_fn->next = fname->next;
  343. fname->next = new_fn;
  344. return 0;
  345. }
  346. if (new_fn->hash < fname->hash)
  347. p = &(*p)->rb_left;
  348. else if (new_fn->hash > fname->hash)
  349. p = &(*p)->rb_right;
  350. else if (new_fn->minor_hash < fname->minor_hash)
  351. p = &(*p)->rb_left;
  352. else /* if (new_fn->minor_hash > fname->minor_hash) */
  353. p = &(*p)->rb_right;
  354. }
  355. rb_link_node(&new_fn->rb_hash, parent, p);
  356. rb_insert_color(&new_fn->rb_hash, &info->root);
  357. return 0;
  358. }
  359. /*
  360. * This is a helper function for ext3_dx_readdir. It calls filldir
  361. * for all entres on the fname linked list. (Normally there is only
  362. * one entry on the linked list, unless there are 62 bit hash collisions.)
  363. */
  364. static bool call_filldir(struct file *file, struct dir_context *ctx,
  365. struct fname *fname)
  366. {
  367. struct dir_private_info *info = file->private_data;
  368. struct inode *inode = file_inode(file);
  369. struct super_block *sb = inode->i_sb;
  370. if (!fname) {
  371. printk("call_filldir: called with null fname?!?\n");
  372. return true;
  373. }
  374. ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
  375. while (fname) {
  376. if (!dir_emit(ctx, fname->name, fname->name_len,
  377. fname->inode,
  378. get_dtype(sb, fname->file_type))) {
  379. info->extra_fname = fname;
  380. return false;
  381. }
  382. fname = fname->next;
  383. }
  384. return true;
  385. }
  386. static int ext3_dx_readdir(struct file *file, struct dir_context *ctx)
  387. {
  388. struct dir_private_info *info = file->private_data;
  389. struct inode *inode = file_inode(file);
  390. struct fname *fname;
  391. int ret;
  392. if (!info) {
  393. info = ext3_htree_create_dir_info(file, ctx->pos);
  394. if (!info)
  395. return -ENOMEM;
  396. file->private_data = info;
  397. }
  398. if (ctx->pos == ext3_get_htree_eof(file))
  399. return 0; /* EOF */
  400. /* Some one has messed with f_pos; reset the world */
  401. if (info->last_pos != ctx->pos) {
  402. free_rb_tree_fname(&info->root);
  403. info->curr_node = NULL;
  404. info->extra_fname = NULL;
  405. info->curr_hash = pos2maj_hash(file, ctx->pos);
  406. info->curr_minor_hash = pos2min_hash(file, ctx->pos);
  407. }
  408. /*
  409. * If there are any leftover names on the hash collision
  410. * chain, return them first.
  411. */
  412. if (info->extra_fname) {
  413. if (!call_filldir(file, ctx, info->extra_fname))
  414. goto finished;
  415. info->extra_fname = NULL;
  416. goto next_node;
  417. } else if (!info->curr_node)
  418. info->curr_node = rb_first(&info->root);
  419. while (1) {
  420. /*
  421. * Fill the rbtree if we have no more entries,
  422. * or the inode has changed since we last read in the
  423. * cached entries.
  424. */
  425. if ((!info->curr_node) ||
  426. (file->f_version != inode->i_version)) {
  427. info->curr_node = NULL;
  428. free_rb_tree_fname(&info->root);
  429. file->f_version = inode->i_version;
  430. ret = ext3_htree_fill_tree(file, info->curr_hash,
  431. info->curr_minor_hash,
  432. &info->next_hash);
  433. if (ret < 0)
  434. return ret;
  435. if (ret == 0) {
  436. ctx->pos = ext3_get_htree_eof(file);
  437. break;
  438. }
  439. info->curr_node = rb_first(&info->root);
  440. }
  441. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  442. info->curr_hash = fname->hash;
  443. info->curr_minor_hash = fname->minor_hash;
  444. if (!call_filldir(file, ctx, fname))
  445. break;
  446. next_node:
  447. info->curr_node = rb_next(info->curr_node);
  448. if (info->curr_node) {
  449. fname = rb_entry(info->curr_node, struct fname,
  450. rb_hash);
  451. info->curr_hash = fname->hash;
  452. info->curr_minor_hash = fname->minor_hash;
  453. } else {
  454. if (info->next_hash == ~0) {
  455. ctx->pos = ext3_get_htree_eof(file);
  456. break;
  457. }
  458. info->curr_hash = info->next_hash;
  459. info->curr_minor_hash = 0;
  460. }
  461. }
  462. finished:
  463. info->last_pos = ctx->pos;
  464. return 0;
  465. }
  466. static int ext3_release_dir (struct inode * inode, struct file * filp)
  467. {
  468. if (filp->private_data)
  469. ext3_htree_free_dir_info(filp->private_data);
  470. return 0;
  471. }
  472. const struct file_operations ext3_dir_operations = {
  473. .llseek = ext3_dir_llseek,
  474. .read = generic_read_dir,
  475. .iterate = ext3_readdir,
  476. .unlocked_ioctl = ext3_ioctl,
  477. #ifdef CONFIG_COMPAT
  478. .compat_ioctl = ext3_compat_ioctl,
  479. #endif
  480. .fsync = ext3_sync_file,
  481. .release = ext3_release_dir,
  482. };