dir.c 18 KB

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