dir.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/dir.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/dir.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * ext4 directory handling functions
  17. *
  18. * Big-endian to little-endian byte-swapping/bitmaps by
  19. * David S. Miller (davem@caip.rutgers.edu), 1995
  20. *
  21. * Hash Tree Directory indexing (c) 2001 Daniel Phillips
  22. *
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/buffer_head.h>
  26. #include <linux/slab.h>
  27. #include <linux/iversion.h>
  28. #include <linux/unicode.h>
  29. #include "ext4.h"
  30. #include "xattr.h"
  31. #define DOTDOT_OFFSET 12
  32. static int ext4_dx_readdir(struct file *, struct dir_context *);
  33. /**
  34. * is_dx_dir() - check if a directory is using htree indexing
  35. * @inode: directory inode
  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 (ext4_has_feature_dir_index(inode->i_sb) &&
  47. ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
  48. ((inode->i_size >> sb->s_blocksize_bits) == 1) ||
  49. ext4_has_inline_data(inode)))
  50. return 1;
  51. return 0;
  52. }
  53. static bool is_fake_entry(struct inode *dir, ext4_lblk_t lblk,
  54. unsigned int offset, unsigned int blocksize)
  55. {
  56. /* Entries in the first block before this value refer to . or .. */
  57. if (lblk == 0 && offset <= DOTDOT_OFFSET)
  58. return true;
  59. /* Check if this is likely the csum entry */
  60. if (ext4_has_metadata_csum(dir->i_sb) && offset % blocksize ==
  61. blocksize - sizeof(struct ext4_dir_entry_tail))
  62. return true;
  63. return false;
  64. }
  65. /*
  66. * Return 0 if the directory entry is OK, and 1 if there is a problem
  67. *
  68. * Note: this is the opposite of what ext2 and ext3 historically returned...
  69. *
  70. * bh passed here can be an inode block or a dir data block, depending
  71. * on the inode inline data flag.
  72. */
  73. int __ext4_check_dir_entry(const char *function, unsigned int line,
  74. struct inode *dir, struct file *filp,
  75. struct ext4_dir_entry_2 *de,
  76. struct buffer_head *bh, char *buf, int size,
  77. ext4_lblk_t lblk,
  78. unsigned int offset)
  79. {
  80. const char *error_msg = NULL;
  81. const int rlen = ext4_rec_len_from_disk(de->rec_len,
  82. dir->i_sb->s_blocksize);
  83. const int next_offset = ((char *) de - buf) + rlen;
  84. unsigned int blocksize = dir->i_sb->s_blocksize;
  85. bool fake = is_fake_entry(dir, lblk, offset, blocksize);
  86. bool next_fake = is_fake_entry(dir, lblk, next_offset, blocksize);
  87. if (unlikely(rlen < ext4_dir_rec_len(1, fake ? NULL : dir)))
  88. error_msg = "rec_len is smaller than minimal";
  89. else if (unlikely(rlen % 4 != 0))
  90. error_msg = "rec_len % 4 != 0";
  91. else if (unlikely(rlen < ext4_dir_rec_len(de->name_len,
  92. fake ? NULL : dir)))
  93. error_msg = "rec_len is too small for name_len";
  94. else if (unlikely(next_offset > size))
  95. error_msg = "directory entry overrun";
  96. else if (unlikely(next_offset > size - ext4_dir_rec_len(1,
  97. next_fake ? NULL : dir) &&
  98. next_offset != size))
  99. error_msg = "directory entry too close to block end";
  100. else if (unlikely(le32_to_cpu(de->inode) >
  101. le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
  102. error_msg = "inode out of bounds";
  103. else
  104. return 0;
  105. if (filp)
  106. ext4_error_file(filp, function, line, bh->b_blocknr,
  107. "bad entry in directory: %s - offset=%u, "
  108. "inode=%u, rec_len=%d, lblk=%d, size=%d fake=%d",
  109. error_msg, offset, le32_to_cpu(de->inode),
  110. rlen, lblk, size, fake);
  111. else
  112. ext4_error_inode(dir, function, line, bh->b_blocknr,
  113. "bad entry in directory: %s - offset=%u, "
  114. "inode=%u, rec_len=%d, lblk=%d, size=%d fake=%d",
  115. error_msg, offset, le32_to_cpu(de->inode),
  116. rlen, lblk, size, fake);
  117. return 1;
  118. }
  119. static int ext4_readdir(struct file *file, struct dir_context *ctx)
  120. {
  121. unsigned int offset;
  122. int i;
  123. struct ext4_dir_entry_2 *de;
  124. int err;
  125. struct inode *inode = file_inode(file);
  126. struct super_block *sb = inode->i_sb;
  127. struct buffer_head *bh = NULL;
  128. struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
  129. err = fscrypt_prepare_readdir(inode);
  130. if (err)
  131. return err;
  132. if (is_dx_dir(inode)) {
  133. err = ext4_dx_readdir(file, ctx);
  134. if (err != ERR_BAD_DX_DIR) {
  135. return err;
  136. }
  137. /* Can we just clear INDEX flag to ignore htree information? */
  138. if (!ext4_has_metadata_csum(sb)) {
  139. /*
  140. * We don't set the inode dirty flag since it's not
  141. * critical that it gets flushed back to the disk.
  142. */
  143. ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
  144. }
  145. }
  146. if (ext4_has_inline_data(inode)) {
  147. int has_inline_data = 1;
  148. err = ext4_read_inline_dir(file, ctx,
  149. &has_inline_data);
  150. if (has_inline_data)
  151. return err;
  152. }
  153. if (IS_ENCRYPTED(inode)) {
  154. err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN, &fstr);
  155. if (err < 0)
  156. return err;
  157. }
  158. while (ctx->pos < inode->i_size) {
  159. struct ext4_map_blocks map;
  160. if (fatal_signal_pending(current)) {
  161. err = -ERESTARTSYS;
  162. goto errout;
  163. }
  164. cond_resched();
  165. offset = ctx->pos & (sb->s_blocksize - 1);
  166. map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
  167. map.m_len = 1;
  168. err = ext4_map_blocks(NULL, inode, &map, 0);
  169. if (err == 0) {
  170. /* m_len should never be zero but let's avoid
  171. * an infinite loop if it somehow is */
  172. if (map.m_len == 0)
  173. map.m_len = 1;
  174. ctx->pos += map.m_len * sb->s_blocksize;
  175. continue;
  176. }
  177. if (err > 0) {
  178. pgoff_t index = map.m_pblk >>
  179. (PAGE_SHIFT - inode->i_blkbits);
  180. if (!ra_has_index(&file->f_ra, index))
  181. page_cache_sync_readahead(
  182. sb->s_bdev->bd_inode->i_mapping,
  183. &file->f_ra, file,
  184. index, 1);
  185. file->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
  186. bh = ext4_bread(NULL, inode, map.m_lblk, 0);
  187. if (IS_ERR(bh)) {
  188. err = PTR_ERR(bh);
  189. bh = NULL;
  190. goto errout;
  191. }
  192. }
  193. if (!bh) {
  194. /* corrupt size? Maybe no more blocks to read */
  195. if (ctx->pos > inode->i_blocks << 9)
  196. break;
  197. ctx->pos += sb->s_blocksize - offset;
  198. continue;
  199. }
  200. /* Check the checksum */
  201. if (!buffer_verified(bh) &&
  202. !ext4_dirblock_csum_verify(inode, bh)) {
  203. EXT4_ERROR_FILE(file, 0, "directory fails checksum "
  204. "at offset %llu",
  205. (unsigned long long)ctx->pos);
  206. ctx->pos += sb->s_blocksize - offset;
  207. brelse(bh);
  208. bh = NULL;
  209. continue;
  210. }
  211. set_buffer_verified(bh);
  212. /* If the dir block has changed since the last call to
  213. * readdir(2), then we might be pointing to an invalid
  214. * dirent right now. Scan from the start of the block
  215. * to make sure. */
  216. if (!inode_eq_iversion(inode, file->f_version)) {
  217. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  218. de = (struct ext4_dir_entry_2 *)
  219. (bh->b_data + i);
  220. /* It's too expensive to do a full
  221. * dirent test each time round this
  222. * loop, but we do have to test at
  223. * least that it is non-zero. A
  224. * failure will be detected in the
  225. * dirent test below. */
  226. if (ext4_rec_len_from_disk(de->rec_len,
  227. sb->s_blocksize) < ext4_dir_rec_len(1,
  228. inode))
  229. break;
  230. i += ext4_rec_len_from_disk(de->rec_len,
  231. sb->s_blocksize);
  232. }
  233. offset = i;
  234. ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
  235. | offset;
  236. file->f_version = inode_query_iversion(inode);
  237. }
  238. while (ctx->pos < inode->i_size
  239. && offset < sb->s_blocksize) {
  240. de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
  241. if (ext4_check_dir_entry(inode, file, de, bh,
  242. bh->b_data, bh->b_size,
  243. map.m_lblk, offset)) {
  244. /*
  245. * On error, skip to the next block
  246. */
  247. ctx->pos = (ctx->pos |
  248. (sb->s_blocksize - 1)) + 1;
  249. break;
  250. }
  251. offset += ext4_rec_len_from_disk(de->rec_len,
  252. sb->s_blocksize);
  253. if (le32_to_cpu(de->inode)) {
  254. if (!IS_ENCRYPTED(inode)) {
  255. if (!dir_emit(ctx, de->name,
  256. de->name_len,
  257. le32_to_cpu(de->inode),
  258. get_dtype(sb, de->file_type)))
  259. goto done;
  260. } else {
  261. int save_len = fstr.len;
  262. struct fscrypt_str de_name =
  263. FSTR_INIT(de->name,
  264. de->name_len);
  265. /* Directory is encrypted */
  266. err = fscrypt_fname_disk_to_usr(inode,
  267. EXT4_DIRENT_HASH(de),
  268. EXT4_DIRENT_MINOR_HASH(de),
  269. &de_name, &fstr);
  270. de_name = fstr;
  271. fstr.len = save_len;
  272. if (err)
  273. goto errout;
  274. if (!dir_emit(ctx,
  275. de_name.name, de_name.len,
  276. le32_to_cpu(de->inode),
  277. get_dtype(sb, de->file_type)))
  278. goto done;
  279. }
  280. }
  281. ctx->pos += ext4_rec_len_from_disk(de->rec_len,
  282. sb->s_blocksize);
  283. }
  284. if ((ctx->pos < inode->i_size) && !dir_relax_shared(inode))
  285. goto done;
  286. brelse(bh);
  287. bh = NULL;
  288. offset = 0;
  289. }
  290. done:
  291. err = 0;
  292. errout:
  293. fscrypt_fname_free_buffer(&fstr);
  294. brelse(bh);
  295. return err;
  296. }
  297. static inline int is_32bit_api(void)
  298. {
  299. #ifdef CONFIG_COMPAT
  300. return in_compat_syscall();
  301. #else
  302. return (BITS_PER_LONG == 32);
  303. #endif
  304. }
  305. /*
  306. * These functions convert from the major/minor hash to an f_pos
  307. * value for dx directories
  308. *
  309. * Upper layer (for example NFS) should specify FMODE_32BITHASH or
  310. * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
  311. * directly on both 32-bit and 64-bit nodes, under such case, neither
  312. * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
  313. */
  314. static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
  315. {
  316. if ((filp->f_mode & FMODE_32BITHASH) ||
  317. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  318. return major >> 1;
  319. else
  320. return ((__u64)(major >> 1) << 32) | (__u64)minor;
  321. }
  322. static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
  323. {
  324. if ((filp->f_mode & FMODE_32BITHASH) ||
  325. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  326. return (pos << 1) & 0xffffffff;
  327. else
  328. return ((pos >> 32) << 1) & 0xffffffff;
  329. }
  330. static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
  331. {
  332. if ((filp->f_mode & FMODE_32BITHASH) ||
  333. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  334. return 0;
  335. else
  336. return pos & 0xffffffff;
  337. }
  338. /*
  339. * Return 32- or 64-bit end-of-file for dx directories
  340. */
  341. static inline loff_t ext4_get_htree_eof(struct file *filp)
  342. {
  343. if ((filp->f_mode & FMODE_32BITHASH) ||
  344. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  345. return EXT4_HTREE_EOF_32BIT;
  346. else
  347. return EXT4_HTREE_EOF_64BIT;
  348. }
  349. /*
  350. * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
  351. * directories, where the "offset" is in terms of the filename hash
  352. * value instead of the byte offset.
  353. *
  354. * Because we may return a 64-bit hash that is well beyond offset limits,
  355. * we need to pass the max hash as the maximum allowable offset in
  356. * the htree directory case.
  357. *
  358. * For non-htree, ext4_llseek already chooses the proper max offset.
  359. */
  360. static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
  361. {
  362. struct inode *inode = file->f_mapping->host;
  363. int dx_dir = is_dx_dir(inode);
  364. loff_t ret, htree_max = ext4_get_htree_eof(file);
  365. if (likely(dx_dir))
  366. ret = generic_file_llseek_size(file, offset, whence,
  367. htree_max, htree_max);
  368. else
  369. ret = ext4_llseek(file, offset, whence);
  370. file->f_version = inode_peek_iversion(inode) - 1;
  371. return ret;
  372. }
  373. /*
  374. * This structure holds the nodes of the red-black tree used to store
  375. * the directory entry in hash order.
  376. */
  377. struct fname {
  378. __u32 hash;
  379. __u32 minor_hash;
  380. struct rb_node rb_hash;
  381. struct fname *next;
  382. __u32 inode;
  383. __u8 name_len;
  384. __u8 file_type;
  385. char name[];
  386. };
  387. /*
  388. * This functoin implements a non-recursive way of freeing all of the
  389. * nodes in the red-black tree.
  390. */
  391. static void free_rb_tree_fname(struct rb_root *root)
  392. {
  393. struct fname *fname, *next;
  394. rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
  395. while (fname) {
  396. struct fname *old = fname;
  397. fname = fname->next;
  398. kfree(old);
  399. }
  400. *root = RB_ROOT;
  401. }
  402. static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
  403. loff_t pos)
  404. {
  405. struct dir_private_info *p;
  406. p = kzalloc(sizeof(*p), GFP_KERNEL);
  407. if (!p)
  408. return NULL;
  409. p->curr_hash = pos2maj_hash(filp, pos);
  410. p->curr_minor_hash = pos2min_hash(filp, pos);
  411. return p;
  412. }
  413. void ext4_htree_free_dir_info(struct dir_private_info *p)
  414. {
  415. free_rb_tree_fname(&p->root);
  416. kfree(p);
  417. }
  418. /*
  419. * Given a directory entry, enter it into the fname rb tree.
  420. *
  421. * When filename encryption is enabled, the dirent will hold the
  422. * encrypted filename, while the htree will hold decrypted filename.
  423. * The decrypted filename is passed in via ent_name. parameter.
  424. */
  425. int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  426. __u32 minor_hash,
  427. struct ext4_dir_entry_2 *dirent,
  428. struct fscrypt_str *ent_name)
  429. {
  430. struct rb_node **p, *parent = NULL;
  431. struct fname *fname, *new_fn;
  432. struct dir_private_info *info;
  433. int len;
  434. info = dir_file->private_data;
  435. p = &info->root.rb_node;
  436. /* Create and allocate the fname structure */
  437. len = sizeof(struct fname) + ent_name->len + 1;
  438. new_fn = kzalloc(len, GFP_KERNEL);
  439. if (!new_fn)
  440. return -ENOMEM;
  441. new_fn->hash = hash;
  442. new_fn->minor_hash = minor_hash;
  443. new_fn->inode = le32_to_cpu(dirent->inode);
  444. new_fn->name_len = ent_name->len;
  445. new_fn->file_type = dirent->file_type;
  446. memcpy(new_fn->name, ent_name->name, ent_name->len);
  447. while (*p) {
  448. parent = *p;
  449. fname = rb_entry(parent, struct fname, rb_hash);
  450. /*
  451. * If the hash and minor hash match up, then we put
  452. * them on a linked list. This rarely happens...
  453. */
  454. if ((new_fn->hash == fname->hash) &&
  455. (new_fn->minor_hash == fname->minor_hash)) {
  456. new_fn->next = fname->next;
  457. fname->next = new_fn;
  458. return 0;
  459. }
  460. if (new_fn->hash < fname->hash)
  461. p = &(*p)->rb_left;
  462. else if (new_fn->hash > fname->hash)
  463. p = &(*p)->rb_right;
  464. else if (new_fn->minor_hash < fname->minor_hash)
  465. p = &(*p)->rb_left;
  466. else /* if (new_fn->minor_hash > fname->minor_hash) */
  467. p = &(*p)->rb_right;
  468. }
  469. rb_link_node(&new_fn->rb_hash, parent, p);
  470. rb_insert_color(&new_fn->rb_hash, &info->root);
  471. return 0;
  472. }
  473. /*
  474. * This is a helper function for ext4_dx_readdir. It calls filldir
  475. * for all entres on the fname linked list. (Normally there is only
  476. * one entry on the linked list, unless there are 62 bit hash collisions.)
  477. */
  478. static int call_filldir(struct file *file, struct dir_context *ctx,
  479. struct fname *fname)
  480. {
  481. struct dir_private_info *info = file->private_data;
  482. struct inode *inode = file_inode(file);
  483. struct super_block *sb = inode->i_sb;
  484. if (!fname) {
  485. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
  486. "called with null fname?!?", __func__, __LINE__,
  487. inode->i_ino, current->comm);
  488. return 0;
  489. }
  490. ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
  491. while (fname) {
  492. if (!dir_emit(ctx, fname->name,
  493. fname->name_len,
  494. fname->inode,
  495. get_dtype(sb, fname->file_type))) {
  496. info->extra_fname = fname;
  497. return 1;
  498. }
  499. fname = fname->next;
  500. }
  501. return 0;
  502. }
  503. static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
  504. {
  505. struct dir_private_info *info = file->private_data;
  506. struct inode *inode = file_inode(file);
  507. struct fname *fname;
  508. int ret = 0;
  509. if (!info) {
  510. info = ext4_htree_create_dir_info(file, ctx->pos);
  511. if (!info)
  512. return -ENOMEM;
  513. file->private_data = info;
  514. }
  515. if (ctx->pos == ext4_get_htree_eof(file))
  516. return 0; /* EOF */
  517. /* Some one has messed with f_pos; reset the world */
  518. if (info->last_pos != ctx->pos) {
  519. free_rb_tree_fname(&info->root);
  520. info->curr_node = NULL;
  521. info->extra_fname = NULL;
  522. info->curr_hash = pos2maj_hash(file, ctx->pos);
  523. info->curr_minor_hash = pos2min_hash(file, ctx->pos);
  524. }
  525. /*
  526. * If there are any leftover names on the hash collision
  527. * chain, return them first.
  528. */
  529. if (info->extra_fname) {
  530. if (call_filldir(file, ctx, info->extra_fname))
  531. goto finished;
  532. info->extra_fname = NULL;
  533. goto next_node;
  534. } else if (!info->curr_node)
  535. info->curr_node = rb_first(&info->root);
  536. while (1) {
  537. /*
  538. * Fill the rbtree if we have no more entries,
  539. * or the inode has changed since we last read in the
  540. * cached entries.
  541. */
  542. if ((!info->curr_node) ||
  543. !inode_eq_iversion(inode, file->f_version)) {
  544. info->curr_node = NULL;
  545. free_rb_tree_fname(&info->root);
  546. file->f_version = inode_query_iversion(inode);
  547. ret = ext4_htree_fill_tree(file, info->curr_hash,
  548. info->curr_minor_hash,
  549. &info->next_hash);
  550. if (ret < 0)
  551. goto finished;
  552. if (ret == 0) {
  553. ctx->pos = ext4_get_htree_eof(file);
  554. break;
  555. }
  556. info->curr_node = rb_first(&info->root);
  557. }
  558. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  559. info->curr_hash = fname->hash;
  560. info->curr_minor_hash = fname->minor_hash;
  561. if (call_filldir(file, ctx, fname))
  562. break;
  563. next_node:
  564. info->curr_node = rb_next(info->curr_node);
  565. if (info->curr_node) {
  566. fname = rb_entry(info->curr_node, struct fname,
  567. rb_hash);
  568. info->curr_hash = fname->hash;
  569. info->curr_minor_hash = fname->minor_hash;
  570. } else {
  571. if (info->next_hash == ~0) {
  572. ctx->pos = ext4_get_htree_eof(file);
  573. break;
  574. }
  575. info->curr_hash = info->next_hash;
  576. info->curr_minor_hash = 0;
  577. }
  578. }
  579. finished:
  580. info->last_pos = ctx->pos;
  581. return ret < 0 ? ret : 0;
  582. }
  583. static int ext4_release_dir(struct inode *inode, struct file *filp)
  584. {
  585. if (filp->private_data)
  586. ext4_htree_free_dir_info(filp->private_data);
  587. return 0;
  588. }
  589. int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
  590. int buf_size)
  591. {
  592. struct ext4_dir_entry_2 *de;
  593. int rlen;
  594. unsigned int offset = 0;
  595. char *top;
  596. de = (struct ext4_dir_entry_2 *)buf;
  597. top = buf + buf_size;
  598. while ((char *) de < top) {
  599. if (ext4_check_dir_entry(dir, NULL, de, bh,
  600. buf, buf_size, 0, offset))
  601. return -EFSCORRUPTED;
  602. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  603. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  604. offset += rlen;
  605. }
  606. if ((char *) de > top)
  607. return -EFSCORRUPTED;
  608. return 0;
  609. }
  610. const struct file_operations ext4_dir_operations = {
  611. .llseek = ext4_dir_llseek,
  612. .read = generic_read_dir,
  613. .iterate_shared = ext4_readdir,
  614. .unlocked_ioctl = ext4_ioctl,
  615. #ifdef CONFIG_COMPAT
  616. .compat_ioctl = ext4_compat_ioctl,
  617. #endif
  618. .fsync = ext4_sync_file,
  619. .release = ext4_release_dir,
  620. };