dir.c 17 KB

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