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/filelock.h>
  27. #include <linux/slab.h>
  28. #include <linux/iversion.h>
  29. #include <linux/unicode.h>
  30. #include "ext4.h"
  31. #include "xattr.h"
  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_dir_entry(struct ext4_dir_entry_2 *de)
  54. {
  55. /* Check if . or .. , or skip if namelen is 0 */
  56. if ((de->name_len > 0) && (de->name_len <= 2) && (de->name[0] == '.') &&
  57. (de->name[1] == '.' || de->name[1] == '\0'))
  58. return true;
  59. /* Check if this is a csum entry */
  60. if (de->file_type == EXT4_FT_DIR_CSUM)
  61. return true;
  62. return false;
  63. }
  64. /*
  65. * Return 0 if the directory entry is OK, and 1 if there is a problem
  66. *
  67. * Note: this is the opposite of what ext2 and ext3 historically returned...
  68. *
  69. * bh passed here can be an inode block or a dir data block, depending
  70. * on the inode inline data flag.
  71. */
  72. int __ext4_check_dir_entry(const char *function, unsigned int line,
  73. struct inode *dir, struct file *filp,
  74. struct ext4_dir_entry_2 *de,
  75. struct buffer_head *bh, char *buf, int size,
  76. unsigned int offset)
  77. {
  78. const char *error_msg = NULL;
  79. const int rlen = ext4_rec_len_from_disk(de->rec_len,
  80. dir->i_sb->s_blocksize);
  81. const int next_offset = ((char *) de - buf) + rlen;
  82. bool fake = is_fake_dir_entry(de);
  83. bool has_csum = ext4_has_feature_metadata_csum(dir->i_sb);
  84. if (unlikely(rlen < ext4_dir_rec_len(1, fake ? NULL : dir)))
  85. error_msg = "rec_len is smaller than minimal";
  86. else if (unlikely(rlen % 4 != 0))
  87. error_msg = "rec_len % 4 != 0";
  88. else if (unlikely(rlen < ext4_dir_rec_len(de->name_len,
  89. fake ? NULL : dir)))
  90. error_msg = "rec_len is too small for name_len";
  91. else if (unlikely(next_offset > size))
  92. error_msg = "directory entry overrun";
  93. else if (unlikely(next_offset > size - ext4_dir_rec_len(1,
  94. has_csum ? NULL : dir) &&
  95. next_offset != size))
  96. error_msg = "directory entry too close to block end";
  97. else if (unlikely(le32_to_cpu(de->inode) >
  98. le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
  99. error_msg = "inode out of bounds";
  100. else if (unlikely(next_offset == size && de->name_len == 1 &&
  101. de->name[0] == '.'))
  102. error_msg = "'.' directory cannot be the last in data block";
  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, size=%d fake=%d",
  109. error_msg, offset, le32_to_cpu(de->inode),
  110. rlen, 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, size=%d fake=%d",
  115. error_msg, offset, le32_to_cpu(de->inode),
  116. rlen, 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. struct dir_private_info *info = file->private_data;
  130. err = fscrypt_prepare_readdir(inode);
  131. if (err)
  132. return err;
  133. if (is_dx_dir(inode)) {
  134. err = ext4_dx_readdir(file, ctx);
  135. if (err != ERR_BAD_DX_DIR)
  136. return err;
  137. /* Can we just clear INDEX flag to ignore htree information? */
  138. if (!ext4_has_feature_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 << inode->i_blkbits >>
  179. PAGE_SHIFT;
  180. if (!ra_has_index(&file->f_ra, index))
  181. page_cache_sync_readahead(
  182. sb->s_bdev->bd_mapping,
  183. &file->f_ra, file, index,
  184. 1 << EXT4_SB(sb)->s_min_folio_order);
  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, info->cookie)) {
  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. info->cookie = 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. 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. u32 hash;
  266. u32 minor_hash;
  267. if (IS_CASEFOLDED(inode)) {
  268. hash = EXT4_DIRENT_HASH(de);
  269. minor_hash = EXT4_DIRENT_MINOR_HASH(de);
  270. } else {
  271. hash = 0;
  272. minor_hash = 0;
  273. }
  274. /* Directory is encrypted */
  275. err = fscrypt_fname_disk_to_usr(inode,
  276. hash, minor_hash, &de_name, &fstr);
  277. de_name = fstr;
  278. fstr.len = save_len;
  279. if (err)
  280. goto errout;
  281. if (!dir_emit(ctx,
  282. de_name.name, de_name.len,
  283. le32_to_cpu(de->inode),
  284. get_dtype(sb, de->file_type)))
  285. goto done;
  286. }
  287. }
  288. ctx->pos += ext4_rec_len_from_disk(de->rec_len,
  289. sb->s_blocksize);
  290. }
  291. if ((ctx->pos < inode->i_size) && !dir_relax_shared(inode))
  292. goto done;
  293. brelse(bh);
  294. bh = NULL;
  295. }
  296. done:
  297. err = 0;
  298. errout:
  299. fscrypt_fname_free_buffer(&fstr);
  300. brelse(bh);
  301. return err;
  302. }
  303. static inline int is_32bit_api(void)
  304. {
  305. #ifdef CONFIG_COMPAT
  306. return in_compat_syscall();
  307. #else
  308. return (BITS_PER_LONG == 32);
  309. #endif
  310. }
  311. /*
  312. * These functions convert from the major/minor hash to an f_pos
  313. * value for dx directories
  314. *
  315. * Upper layer (for example NFS) should specify FMODE_32BITHASH or
  316. * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
  317. * directly on both 32-bit and 64-bit nodes, under such case, neither
  318. * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
  319. */
  320. static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
  321. {
  322. if ((filp->f_mode & FMODE_32BITHASH) ||
  323. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  324. return major >> 1;
  325. else
  326. return ((__u64)(major >> 1) << 32) | (__u64)minor;
  327. }
  328. static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
  329. {
  330. if ((filp->f_mode & FMODE_32BITHASH) ||
  331. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  332. return (pos << 1) & 0xffffffff;
  333. else
  334. return ((pos >> 32) << 1) & 0xffffffff;
  335. }
  336. static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
  337. {
  338. if ((filp->f_mode & FMODE_32BITHASH) ||
  339. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  340. return 0;
  341. else
  342. return pos & 0xffffffff;
  343. }
  344. /*
  345. * Return 32- or 64-bit end-of-file for dx directories
  346. */
  347. static inline loff_t ext4_get_htree_eof(struct file *filp)
  348. {
  349. if ((filp->f_mode & FMODE_32BITHASH) ||
  350. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  351. return EXT4_HTREE_EOF_32BIT;
  352. else
  353. return EXT4_HTREE_EOF_64BIT;
  354. }
  355. /*
  356. * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
  357. * directories, where the "offset" is in terms of the filename hash
  358. * value instead of the byte offset.
  359. *
  360. * Because we may return a 64-bit hash that is well beyond offset limits,
  361. * we need to pass the max hash as the maximum allowable offset in
  362. * the htree directory case.
  363. *
  364. * For non-htree, ext4_llseek already chooses the proper max offset.
  365. */
  366. static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
  367. {
  368. struct inode *inode = file->f_mapping->host;
  369. struct dir_private_info *info = file->private_data;
  370. int dx_dir = is_dx_dir(inode);
  371. loff_t ret, htree_max = ext4_get_htree_eof(file);
  372. if (likely(dx_dir))
  373. ret = generic_file_llseek_size(file, offset, whence,
  374. htree_max, htree_max);
  375. else
  376. ret = ext4_llseek(file, offset, whence);
  377. info->cookie = inode_peek_iversion(inode) - 1;
  378. return ret;
  379. }
  380. /*
  381. * This structure holds the nodes of the red-black tree used to store
  382. * the directory entry in hash order.
  383. */
  384. struct fname {
  385. __u32 hash;
  386. __u32 minor_hash;
  387. struct rb_node rb_hash;
  388. struct fname *next;
  389. __u32 inode;
  390. __u8 name_len;
  391. __u8 file_type;
  392. char name[] __counted_by(name_len);
  393. };
  394. /*
  395. * This function implements a non-recursive way of freeing all of the
  396. * nodes in the red-black tree.
  397. */
  398. static void free_rb_tree_fname(struct rb_root *root)
  399. {
  400. struct fname *fname, *next;
  401. rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
  402. while (fname) {
  403. struct fname *old = fname;
  404. fname = fname->next;
  405. kfree(old);
  406. }
  407. *root = RB_ROOT;
  408. }
  409. static void ext4_htree_init_dir_info(struct file *filp, loff_t pos)
  410. {
  411. struct dir_private_info *p = filp->private_data;
  412. if (is_dx_dir(file_inode(filp)) && !p->initialized) {
  413. p->curr_hash = pos2maj_hash(filp, pos);
  414. p->curr_minor_hash = pos2min_hash(filp, pos);
  415. p->initialized = true;
  416. }
  417. }
  418. void ext4_htree_free_dir_info(struct dir_private_info *p)
  419. {
  420. free_rb_tree_fname(&p->root);
  421. kfree(p);
  422. }
  423. /*
  424. * Given a directory entry, enter it into the fname rb tree.
  425. *
  426. * When filename encryption is enabled, the dirent will hold the
  427. * encrypted filename, while the htree will hold decrypted filename.
  428. * The decrypted filename is passed in via ent_name. parameter.
  429. */
  430. int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  431. __u32 minor_hash,
  432. struct ext4_dir_entry_2 *dirent,
  433. struct fscrypt_str *ent_name)
  434. {
  435. struct rb_node **p, *parent = NULL;
  436. struct fname *fname, *new_fn;
  437. struct dir_private_info *info;
  438. info = dir_file->private_data;
  439. p = &info->root.rb_node;
  440. /* Create and allocate the fname structure */
  441. new_fn = kzalloc_flex(*new_fn, name, ent_name->len + 1);
  442. if (!new_fn)
  443. return -ENOMEM;
  444. new_fn->hash = hash;
  445. new_fn->minor_hash = minor_hash;
  446. new_fn->inode = le32_to_cpu(dirent->inode);
  447. new_fn->name_len = ent_name->len;
  448. new_fn->file_type = dirent->file_type;
  449. memcpy(new_fn->name, ent_name->name, ent_name->len);
  450. while (*p) {
  451. parent = *p;
  452. fname = rb_entry(parent, struct fname, rb_hash);
  453. /*
  454. * If the hash and minor hash match up, then we put
  455. * them on a linked list. This rarely happens...
  456. */
  457. if ((new_fn->hash == fname->hash) &&
  458. (new_fn->minor_hash == fname->minor_hash)) {
  459. new_fn->next = fname->next;
  460. fname->next = new_fn;
  461. return 0;
  462. }
  463. if (new_fn->hash < fname->hash)
  464. p = &(*p)->rb_left;
  465. else if (new_fn->hash > fname->hash)
  466. p = &(*p)->rb_right;
  467. else if (new_fn->minor_hash < fname->minor_hash)
  468. p = &(*p)->rb_left;
  469. else /* if (new_fn->minor_hash > fname->minor_hash) */
  470. p = &(*p)->rb_right;
  471. }
  472. rb_link_node(&new_fn->rb_hash, parent, p);
  473. rb_insert_color(&new_fn->rb_hash, &info->root);
  474. return 0;
  475. }
  476. /*
  477. * This is a helper function for ext4_dx_readdir. It calls filldir
  478. * for all entries on the fname linked list. (Normally there is only
  479. * one entry on the linked list, unless there are 62 bit hash collisions.)
  480. */
  481. static int call_filldir(struct file *file, struct dir_context *ctx,
  482. struct fname *fname)
  483. {
  484. struct dir_private_info *info = file->private_data;
  485. struct inode *inode = file_inode(file);
  486. struct super_block *sb = inode->i_sb;
  487. if (!fname) {
  488. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
  489. "called with null fname?!?", __func__, __LINE__,
  490. inode->i_ino, current->comm);
  491. return 0;
  492. }
  493. ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
  494. while (fname) {
  495. if (!dir_emit(ctx, fname->name,
  496. fname->name_len,
  497. fname->inode,
  498. get_dtype(sb, fname->file_type))) {
  499. info->extra_fname = fname;
  500. return 1;
  501. }
  502. fname = fname->next;
  503. }
  504. return 0;
  505. }
  506. static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
  507. {
  508. struct dir_private_info *info = file->private_data;
  509. struct inode *inode = file_inode(file);
  510. struct fname *fname;
  511. int ret = 0;
  512. ext4_htree_init_dir_info(file, ctx->pos);
  513. if (ctx->pos == ext4_get_htree_eof(file))
  514. return 0; /* EOF */
  515. /* Some one has messed with f_pos; reset the world */
  516. if (info->last_pos != ctx->pos) {
  517. free_rb_tree_fname(&info->root);
  518. info->curr_node = NULL;
  519. info->extra_fname = NULL;
  520. info->curr_hash = pos2maj_hash(file, ctx->pos);
  521. info->curr_minor_hash = pos2min_hash(file, ctx->pos);
  522. }
  523. /*
  524. * If there are any leftover names on the hash collision
  525. * chain, return them first.
  526. */
  527. if (info->extra_fname) {
  528. if (call_filldir(file, ctx, info->extra_fname))
  529. goto finished;
  530. info->extra_fname = NULL;
  531. goto next_node;
  532. } else if (!info->curr_node)
  533. info->curr_node = rb_first(&info->root);
  534. while (1) {
  535. /*
  536. * Fill the rbtree if we have no more entries,
  537. * or the inode has changed since we last read in the
  538. * cached entries.
  539. */
  540. if ((!info->curr_node) ||
  541. !inode_eq_iversion(inode, info->cookie)) {
  542. info->curr_node = NULL;
  543. free_rb_tree_fname(&info->root);
  544. info->cookie = inode_query_iversion(inode);
  545. ret = ext4_htree_fill_tree(file, info->curr_hash,
  546. info->curr_minor_hash,
  547. &info->next_hash);
  548. if (ret < 0)
  549. goto finished;
  550. if (ret == 0) {
  551. ctx->pos = ext4_get_htree_eof(file);
  552. break;
  553. }
  554. info->curr_node = rb_first(&info->root);
  555. }
  556. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  557. info->curr_hash = fname->hash;
  558. info->curr_minor_hash = fname->minor_hash;
  559. if (call_filldir(file, ctx, fname))
  560. break;
  561. next_node:
  562. info->curr_node = rb_next(info->curr_node);
  563. if (info->curr_node) {
  564. fname = rb_entry(info->curr_node, struct fname,
  565. rb_hash);
  566. info->curr_hash = fname->hash;
  567. info->curr_minor_hash = fname->minor_hash;
  568. } else {
  569. if (info->next_hash == ~0) {
  570. ctx->pos = ext4_get_htree_eof(file);
  571. break;
  572. }
  573. info->curr_hash = info->next_hash;
  574. info->curr_minor_hash = 0;
  575. }
  576. }
  577. finished:
  578. info->last_pos = ctx->pos;
  579. return ret < 0 ? ret : 0;
  580. }
  581. static int ext4_release_dir(struct inode *inode, struct file *filp)
  582. {
  583. if (filp->private_data)
  584. ext4_htree_free_dir_info(filp->private_data);
  585. return 0;
  586. }
  587. int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
  588. int buf_size)
  589. {
  590. struct ext4_dir_entry_2 *de;
  591. int rlen;
  592. unsigned int offset = 0;
  593. char *top;
  594. de = buf;
  595. top = buf + buf_size;
  596. while ((char *) de < top) {
  597. if (ext4_check_dir_entry(dir, NULL, de, bh,
  598. buf, buf_size, offset))
  599. return -EFSCORRUPTED;
  600. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  601. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  602. offset += rlen;
  603. }
  604. if ((char *) de > top)
  605. return -EFSCORRUPTED;
  606. return 0;
  607. }
  608. static int ext4_dir_open(struct inode *inode, struct file *file)
  609. {
  610. struct dir_private_info *info;
  611. info = kzalloc_obj(*info);
  612. if (!info)
  613. return -ENOMEM;
  614. file->private_data = info;
  615. return 0;
  616. }
  617. const struct file_operations ext4_dir_operations = {
  618. .open = ext4_dir_open,
  619. .llseek = ext4_dir_llseek,
  620. .read = generic_read_dir,
  621. .iterate_shared = ext4_readdir,
  622. .unlocked_ioctl = ext4_ioctl,
  623. #ifdef CONFIG_COMPAT
  624. .compat_ioctl = ext4_compat_ioctl,
  625. #endif
  626. .fsync = ext4_sync_file,
  627. .release = ext4_release_dir,
  628. .setlease = generic_setlease,
  629. };