dir.c 13 KB

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