dir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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/fs.h>
  24. #include <linux/jbd.h>
  25. #include <linux/ext3_fs.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/slab.h>
  28. #include <linux/rbtree.h>
  29. static unsigned char ext3_filetype_table[] = {
  30. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  31. };
  32. static int ext3_readdir(struct file *, void *, filldir_t);
  33. static int ext3_dx_readdir(struct file * filp,
  34. void * dirent, filldir_t filldir);
  35. static int ext3_release_dir (struct inode * inode,
  36. struct file * filp);
  37. const struct file_operations ext3_dir_operations = {
  38. .llseek = generic_file_llseek,
  39. .read = generic_read_dir,
  40. .readdir = ext3_readdir, /* we take BKL. needed?*/
  41. .ioctl = ext3_ioctl, /* BKL held */
  42. #ifdef CONFIG_COMPAT
  43. .compat_ioctl = ext3_compat_ioctl,
  44. #endif
  45. .fsync = ext3_sync_file, /* BKL held */
  46. #ifdef CONFIG_EXT3_INDEX
  47. .release = ext3_release_dir,
  48. #endif
  49. };
  50. static unsigned char get_dtype(struct super_block *sb, int filetype)
  51. {
  52. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
  53. (filetype >= EXT3_FT_MAX))
  54. return DT_UNKNOWN;
  55. return (ext3_filetype_table[filetype]);
  56. }
  57. int ext3_check_dir_entry (const char * function, struct inode * dir,
  58. struct ext3_dir_entry_2 * de,
  59. struct buffer_head * bh,
  60. unsigned long offset)
  61. {
  62. const char * error_msg = NULL;
  63. const int rlen = le16_to_cpu(de->rec_len);
  64. if (rlen < EXT3_DIR_REC_LEN(1))
  65. error_msg = "rec_len is smaller than minimal";
  66. else if (rlen % 4 != 0)
  67. error_msg = "rec_len % 4 != 0";
  68. else if (rlen < EXT3_DIR_REC_LEN(de->name_len))
  69. error_msg = "rec_len is too small for name_len";
  70. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  71. error_msg = "directory entry across blocks";
  72. else if (le32_to_cpu(de->inode) >
  73. le32_to_cpu(EXT3_SB(dir->i_sb)->s_es->s_inodes_count))
  74. error_msg = "inode out of bounds";
  75. if (error_msg != NULL)
  76. ext3_error (dir->i_sb, function,
  77. "bad entry in directory #%lu: %s - "
  78. "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
  79. dir->i_ino, error_msg, offset,
  80. (unsigned long) le32_to_cpu(de->inode),
  81. rlen, de->name_len);
  82. return error_msg == NULL ? 1 : 0;
  83. }
  84. static int ext3_readdir(struct file * filp,
  85. void * dirent, filldir_t filldir)
  86. {
  87. int error = 0;
  88. unsigned long offset;
  89. int i, stored;
  90. struct ext3_dir_entry_2 *de;
  91. struct super_block *sb;
  92. int err;
  93. struct inode *inode = filp->f_path.dentry->d_inode;
  94. int ret = 0;
  95. sb = inode->i_sb;
  96. #ifdef CONFIG_EXT3_INDEX
  97. if (EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
  98. EXT3_FEATURE_COMPAT_DIR_INDEX) &&
  99. ((EXT3_I(inode)->i_flags & EXT3_INDEX_FL) ||
  100. ((inode->i_size >> sb->s_blocksize_bits) == 1))) {
  101. err = ext3_dx_readdir(filp, dirent, filldir);
  102. if (err != ERR_BAD_DX_DIR) {
  103. ret = err;
  104. goto out;
  105. }
  106. /*
  107. * We don't set the inode dirty flag since it's not
  108. * critical that it get flushed back to the disk.
  109. */
  110. EXT3_I(filp->f_path.dentry->d_inode)->i_flags &= ~EXT3_INDEX_FL;
  111. }
  112. #endif
  113. stored = 0;
  114. offset = filp->f_pos & (sb->s_blocksize - 1);
  115. while (!error && !stored && filp->f_pos < inode->i_size) {
  116. unsigned long blk = filp->f_pos >> EXT3_BLOCK_SIZE_BITS(sb);
  117. struct buffer_head map_bh;
  118. struct buffer_head *bh = NULL;
  119. map_bh.b_state = 0;
  120. err = ext3_get_blocks_handle(NULL, inode, blk, 1,
  121. &map_bh, 0, 0);
  122. if (err > 0) {
  123. page_cache_readahead(sb->s_bdev->bd_inode->i_mapping,
  124. &filp->f_ra,
  125. filp,
  126. map_bh.b_blocknr >>
  127. (PAGE_CACHE_SHIFT - inode->i_blkbits),
  128. 1);
  129. bh = ext3_bread(NULL, inode, blk, 0, &err);
  130. }
  131. /*
  132. * We ignore I/O errors on directories so users have a chance
  133. * of recovering data when there's a bad sector
  134. */
  135. if (!bh) {
  136. ext3_error (sb, "ext3_readdir",
  137. "directory #%lu contains a hole at offset %lu",
  138. inode->i_ino, (unsigned long)filp->f_pos);
  139. /* corrupt size? Maybe no more blocks to read */
  140. if (filp->f_pos > inode->i_blocks << 9)
  141. break;
  142. filp->f_pos += sb->s_blocksize - offset;
  143. continue;
  144. }
  145. revalidate:
  146. /* If the dir block has changed since the last call to
  147. * readdir(2), then we might be pointing to an invalid
  148. * dirent right now. Scan from the start of the block
  149. * to make sure. */
  150. if (filp->f_version != inode->i_version) {
  151. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  152. de = (struct ext3_dir_entry_2 *)
  153. (bh->b_data + i);
  154. /* It's too expensive to do a full
  155. * dirent test each time round this
  156. * loop, but we do have to test at
  157. * least that it is non-zero. A
  158. * failure will be detected in the
  159. * dirent test below. */
  160. if (le16_to_cpu(de->rec_len) <
  161. EXT3_DIR_REC_LEN(1))
  162. break;
  163. i += le16_to_cpu(de->rec_len);
  164. }
  165. offset = i;
  166. filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
  167. | offset;
  168. filp->f_version = inode->i_version;
  169. }
  170. while (!error && filp->f_pos < inode->i_size
  171. && offset < sb->s_blocksize) {
  172. de = (struct ext3_dir_entry_2 *) (bh->b_data + offset);
  173. if (!ext3_check_dir_entry ("ext3_readdir", inode, de,
  174. bh, offset)) {
  175. /* On error, skip the f_pos to the
  176. next block. */
  177. filp->f_pos = (filp->f_pos |
  178. (sb->s_blocksize - 1)) + 1;
  179. brelse (bh);
  180. ret = stored;
  181. goto out;
  182. }
  183. offset += le16_to_cpu(de->rec_len);
  184. if (le32_to_cpu(de->inode)) {
  185. /* We might block in the next section
  186. * if the data destination is
  187. * currently swapped out. So, use a
  188. * version stamp to detect whether or
  189. * not the directory has been modified
  190. * during the copy operation.
  191. */
  192. unsigned long version = filp->f_version;
  193. error = filldir(dirent, de->name,
  194. de->name_len,
  195. filp->f_pos,
  196. le32_to_cpu(de->inode),
  197. get_dtype(sb, de->file_type));
  198. if (error)
  199. break;
  200. if (version != filp->f_version)
  201. goto revalidate;
  202. stored ++;
  203. }
  204. filp->f_pos += le16_to_cpu(de->rec_len);
  205. }
  206. offset = 0;
  207. brelse (bh);
  208. }
  209. out:
  210. return ret;
  211. }
  212. #ifdef CONFIG_EXT3_INDEX
  213. /*
  214. * These functions convert from the major/minor hash to an f_pos
  215. * value.
  216. *
  217. * Currently we only use major hash numer. This is unfortunate, but
  218. * on 32-bit machines, the same VFS interface is used for lseek and
  219. * llseek, so if we use the 64 bit offset, then the 32-bit versions of
  220. * lseek/telldir/seekdir will blow out spectacularly, and from within
  221. * the ext2 low-level routine, we don't know if we're being called by
  222. * a 64-bit version of the system call or the 32-bit version of the
  223. * system call. Worse yet, NFSv2 only allows for a 32-bit readdir
  224. * cookie. Sigh.
  225. */
  226. #define hash2pos(major, minor) (major >> 1)
  227. #define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
  228. #define pos2min_hash(pos) (0)
  229. /*
  230. * This structure holds the nodes of the red-black tree used to store
  231. * the directory entry in hash order.
  232. */
  233. struct fname {
  234. __u32 hash;
  235. __u32 minor_hash;
  236. struct rb_node rb_hash;
  237. struct fname *next;
  238. __u32 inode;
  239. __u8 name_len;
  240. __u8 file_type;
  241. char name[0];
  242. };
  243. /*
  244. * This functoin implements a non-recursive way of freeing all of the
  245. * nodes in the red-black tree.
  246. */
  247. static void free_rb_tree_fname(struct rb_root *root)
  248. {
  249. struct rb_node *n = root->rb_node;
  250. struct rb_node *parent;
  251. struct fname *fname;
  252. while (n) {
  253. /* Do the node's children first */
  254. if ((n)->rb_left) {
  255. n = n->rb_left;
  256. continue;
  257. }
  258. if (n->rb_right) {
  259. n = n->rb_right;
  260. continue;
  261. }
  262. /*
  263. * The node has no children; free it, and then zero
  264. * out parent's link to it. Finally go to the
  265. * beginning of the loop and try to free the parent
  266. * node.
  267. */
  268. parent = rb_parent(n);
  269. fname = rb_entry(n, struct fname, rb_hash);
  270. while (fname) {
  271. struct fname * old = fname;
  272. fname = fname->next;
  273. kfree (old);
  274. }
  275. if (!parent)
  276. root->rb_node = NULL;
  277. else if (parent->rb_left == n)
  278. parent->rb_left = NULL;
  279. else if (parent->rb_right == n)
  280. parent->rb_right = NULL;
  281. n = parent;
  282. }
  283. root->rb_node = NULL;
  284. }
  285. static struct dir_private_info *create_dir_info(loff_t pos)
  286. {
  287. struct dir_private_info *p;
  288. p = kmalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  289. if (!p)
  290. return NULL;
  291. p->root.rb_node = NULL;
  292. p->curr_node = NULL;
  293. p->extra_fname = NULL;
  294. p->last_pos = 0;
  295. p->curr_hash = pos2maj_hash(pos);
  296. p->curr_minor_hash = pos2min_hash(pos);
  297. p->next_hash = 0;
  298. return p;
  299. }
  300. void ext3_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 ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
  309. __u32 minor_hash,
  310. struct ext3_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 ext3_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("call_filldir: called with null fname?!?\n");
  372. return 0;
  373. }
  374. curr_pos = hash2pos(fname->hash, fname->minor_hash);
  375. while (fname) {
  376. error = filldir(dirent, fname->name,
  377. fname->name_len, curr_pos,
  378. fname->inode,
  379. get_dtype(sb, fname->file_type));
  380. if (error) {
  381. filp->f_pos = curr_pos;
  382. info->extra_fname = fname->next;
  383. return error;
  384. }
  385. fname = fname->next;
  386. }
  387. return 0;
  388. }
  389. static int ext3_dx_readdir(struct file * filp,
  390. void * dirent, filldir_t filldir)
  391. {
  392. struct dir_private_info *info = filp->private_data;
  393. struct inode *inode = filp->f_path.dentry->d_inode;
  394. struct fname *fname;
  395. int ret;
  396. if (!info) {
  397. info = create_dir_info(filp->f_pos);
  398. if (!info)
  399. return -ENOMEM;
  400. filp->private_data = info;
  401. }
  402. if (filp->f_pos == EXT3_HTREE_EOF)
  403. return 0; /* EOF */
  404. /* Some one has messed with f_pos; reset the world */
  405. if (info->last_pos != filp->f_pos) {
  406. free_rb_tree_fname(&info->root);
  407. info->curr_node = NULL;
  408. info->extra_fname = NULL;
  409. info->curr_hash = pos2maj_hash(filp->f_pos);
  410. info->curr_minor_hash = pos2min_hash(filp->f_pos);
  411. }
  412. /*
  413. * If there are any leftover names on the hash collision
  414. * chain, return them first.
  415. */
  416. if (info->extra_fname &&
  417. call_filldir(filp, dirent, filldir, info->extra_fname))
  418. goto finished;
  419. if (!info->curr_node)
  420. info->curr_node = rb_first(&info->root);
  421. while (1) {
  422. /*
  423. * Fill the rbtree if we have no more entries,
  424. * or the inode has changed since we last read in the
  425. * cached entries.
  426. */
  427. if ((!info->curr_node) ||
  428. (filp->f_version != inode->i_version)) {
  429. info->curr_node = NULL;
  430. free_rb_tree_fname(&info->root);
  431. filp->f_version = inode->i_version;
  432. ret = ext3_htree_fill_tree(filp, info->curr_hash,
  433. info->curr_minor_hash,
  434. &info->next_hash);
  435. if (ret < 0)
  436. return ret;
  437. if (ret == 0) {
  438. filp->f_pos = EXT3_HTREE_EOF;
  439. break;
  440. }
  441. info->curr_node = rb_first(&info->root);
  442. }
  443. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  444. info->curr_hash = fname->hash;
  445. info->curr_minor_hash = fname->minor_hash;
  446. if (call_filldir(filp, dirent, filldir, fname))
  447. break;
  448. info->curr_node = rb_next(info->curr_node);
  449. if (!info->curr_node) {
  450. if (info->next_hash == ~0) {
  451. filp->f_pos = EXT3_HTREE_EOF;
  452. break;
  453. }
  454. info->curr_hash = info->next_hash;
  455. info->curr_minor_hash = 0;
  456. }
  457. }
  458. finished:
  459. info->last_pos = filp->f_pos;
  460. return 0;
  461. }
  462. static int ext3_release_dir (struct inode * inode, struct file * filp)
  463. {
  464. if (filp->private_data)
  465. ext3_htree_free_dir_info(filp->private_data);
  466. return 0;
  467. }
  468. #endif