dir.c 15 KB

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