dir.c 13 KB

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