PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext4/dir.c

https://bitbucket.org/Don2x/mod-kernel-m7-sources
C | 561 lines | 468 code | 71 blank | 22 comment | 107 complexity | 7011c2986e45a8144a4e2c8b41253df1 MD5 | raw file
  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/buffer_head.h>
  26. #include <linux/slab.h>
  27. #include <linux/rbtree.h>
  28. #include "ext4.h"
  29. static unsigned char ext4_filetype_table[] = {
  30. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  31. };
  32. static int ext4_dx_readdir(struct file *filp,
  33. void *dirent, filldir_t filldir);
  34. static unsigned char get_dtype(struct super_block *sb, int filetype)
  35. {
  36. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE) ||
  37. (filetype >= EXT4_FT_MAX))
  38. return DT_UNKNOWN;
  39. return (ext4_filetype_table[filetype]);
  40. }
  41. static int is_dx_dir(struct inode *inode)
  42. {
  43. struct super_block *sb = inode->i_sb;
  44. if (EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  45. EXT4_FEATURE_COMPAT_DIR_INDEX) &&
  46. ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
  47. ((inode->i_size >> sb->s_blocksize_bits) == 1)))
  48. return 1;
  49. return 0;
  50. }
  51. int __ext4_check_dir_entry(const char *function, unsigned int line,
  52. struct inode *dir, struct file *filp,
  53. struct ext4_dir_entry_2 *de,
  54. struct buffer_head *bh,
  55. unsigned int offset)
  56. {
  57. const char *error_msg = NULL;
  58. const int rlen = ext4_rec_len_from_disk(de->rec_len,
  59. dir->i_sb->s_blocksize);
  60. if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
  61. error_msg = "rec_len is smaller than minimal";
  62. else if (unlikely(rlen % 4 != 0))
  63. error_msg = "rec_len % 4 != 0";
  64. else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
  65. error_msg = "rec_len is too small for name_len";
  66. else if (unlikely(((char *) de - bh->b_data) + rlen >
  67. dir->i_sb->s_blocksize))
  68. error_msg = "directory entry across blocks";
  69. else if (unlikely(le32_to_cpu(de->inode) >
  70. le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
  71. error_msg = "inode out of bounds";
  72. else
  73. return 0;
  74. if (filp)
  75. ext4_error_file(filp, function, line, bh->b_blocknr,
  76. "bad entry in directory: %s - offset=%u(%u), "
  77. "inode=%u, rec_len=%d, name_len=%d",
  78. error_msg, (unsigned) (offset % bh->b_size),
  79. offset, le32_to_cpu(de->inode),
  80. rlen, de->name_len);
  81. else
  82. ext4_error_inode(dir, function, line, bh->b_blocknr,
  83. "bad entry in directory: %s - offset=%u(%u), "
  84. "inode=%u, rec_len=%d, name_len=%d",
  85. error_msg, (unsigned) (offset % bh->b_size),
  86. offset, le32_to_cpu(de->inode),
  87. rlen, de->name_len);
  88. return 1;
  89. }
  90. static int ext4_readdir(struct file *filp,
  91. void *dirent, filldir_t filldir)
  92. {
  93. int error = 0;
  94. unsigned int offset;
  95. int i, stored;
  96. struct ext4_dir_entry_2 *de;
  97. int err;
  98. struct inode *inode = filp->f_path.dentry->d_inode;
  99. struct super_block *sb = inode->i_sb;
  100. int ret = 0;
  101. int dir_has_error = 0;
  102. if (is_dx_dir(inode)) {
  103. err = ext4_dx_readdir(filp, dirent, filldir);
  104. if (err != ERR_BAD_DX_DIR) {
  105. ret = err;
  106. goto out;
  107. }
  108. ext4_clear_inode_flag(filp->f_path.dentry->d_inode,
  109. EXT4_INODE_INDEX);
  110. }
  111. stored = 0;
  112. offset = filp->f_pos & (sb->s_blocksize - 1);
  113. while (!error && !stored && filp->f_pos < inode->i_size) {
  114. struct ext4_map_blocks map;
  115. struct buffer_head *bh = NULL;
  116. map.m_lblk = filp->f_pos >> EXT4_BLOCK_SIZE_BITS(sb);
  117. map.m_len = 1;
  118. err = ext4_map_blocks(NULL, inode, &map, 0);
  119. if (err > 0) {
  120. pgoff_t index = map.m_pblk >>
  121. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  122. if (!ra_has_index(&filp->f_ra, index))
  123. page_cache_sync_readahead(
  124. sb->s_bdev->bd_inode->i_mapping,
  125. &filp->f_ra, filp,
  126. index, 1);
  127. filp->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
  128. bh = ext4_bread(NULL, inode, map.m_lblk, 0, &err);
  129. }
  130. if (!bh) {
  131. if (!dir_has_error) {
  132. EXT4_ERROR_FILE(filp, 0,
  133. "directory contains a "
  134. "hole at offset %llu",
  135. (unsigned long long) filp->f_pos);
  136. dir_has_error = 1;
  137. }
  138. if (filp->f_pos > inode->i_blocks << 9)
  139. break;
  140. filp->f_pos += sb->s_blocksize - offset;
  141. continue;
  142. }
  143. revalidate:
  144. if (filp->f_version != inode->i_version) {
  145. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  146. de = (struct ext4_dir_entry_2 *)
  147. (bh->b_data + i);
  148. if (ext4_rec_len_from_disk(de->rec_len,
  149. sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
  150. break;
  151. i += ext4_rec_len_from_disk(de->rec_len,
  152. sb->s_blocksize);
  153. }
  154. offset = i;
  155. filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
  156. | offset;
  157. filp->f_version = inode->i_version;
  158. }
  159. while (!error && filp->f_pos < inode->i_size
  160. && offset < sb->s_blocksize) {
  161. de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
  162. if (ext4_check_dir_entry(inode, filp, de,
  163. bh, offset)) {
  164. filp->f_pos = (filp->f_pos |
  165. (sb->s_blocksize - 1)) + 1;
  166. brelse(bh);
  167. ret = stored;
  168. goto out;
  169. }
  170. offset += ext4_rec_len_from_disk(de->rec_len,
  171. sb->s_blocksize);
  172. if (le32_to_cpu(de->inode)) {
  173. u64 version = filp->f_version;
  174. error = filldir(dirent, de->name,
  175. de->name_len,
  176. filp->f_pos,
  177. le32_to_cpu(de->inode),
  178. get_dtype(sb, de->file_type));
  179. if (error)
  180. break;
  181. if (version != filp->f_version)
  182. goto revalidate;
  183. stored++;
  184. }
  185. filp->f_pos += ext4_rec_len_from_disk(de->rec_len,
  186. sb->s_blocksize);
  187. }
  188. offset = 0;
  189. brelse(bh);
  190. }
  191. out:
  192. return ret;
  193. }
  194. static inline int is_32bit_api(void)
  195. {
  196. #ifdef CONFIG_COMPAT
  197. return is_compat_task();
  198. #else
  199. return (BITS_PER_LONG == 32);
  200. #endif
  201. }
  202. static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
  203. {
  204. if ((filp->f_mode & FMODE_32BITHASH) ||
  205. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  206. return major >> 1;
  207. else
  208. return ((__u64)(major >> 1) << 32) | (__u64)minor;
  209. }
  210. static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
  211. {
  212. if ((filp->f_mode & FMODE_32BITHASH) ||
  213. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  214. return (pos << 1) & 0xffffffff;
  215. else
  216. return ((pos >> 32) << 1) & 0xffffffff;
  217. }
  218. static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
  219. {
  220. if ((filp->f_mode & FMODE_32BITHASH) ||
  221. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  222. return 0;
  223. else
  224. return pos & 0xffffffff;
  225. }
  226. static inline loff_t ext4_get_htree_eof(struct file *filp)
  227. {
  228. if ((filp->f_mode & FMODE_32BITHASH) ||
  229. (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
  230. return EXT4_HTREE_EOF_32BIT;
  231. else
  232. return EXT4_HTREE_EOF_64BIT;
  233. }
  234. loff_t ext4_dir_llseek(struct file *file, loff_t offset, int origin)
  235. {
  236. struct inode *inode = file->f_mapping->host;
  237. loff_t ret = -EINVAL;
  238. int dx_dir = is_dx_dir(inode);
  239. mutex_lock(&inode->i_mutex);
  240. switch (origin) {
  241. case SEEK_END:
  242. if (unlikely(offset > 0))
  243. goto out_err;
  244. if (dx_dir)
  245. offset += ext4_get_htree_eof(file);
  246. else
  247. offset += inode->i_size;
  248. break;
  249. case SEEK_CUR:
  250. if (offset == 0) {
  251. offset = file->f_pos;
  252. goto out_ok;
  253. }
  254. offset += file->f_pos;
  255. break;
  256. }
  257. if (unlikely(offset < 0))
  258. goto out_err;
  259. if (!dx_dir) {
  260. if (offset > inode->i_sb->s_maxbytes)
  261. goto out_err;
  262. } else if (offset > ext4_get_htree_eof(file))
  263. goto out_err;
  264. if (offset != file->f_pos) {
  265. file->f_pos = offset;
  266. file->f_version = 0;
  267. }
  268. out_ok:
  269. ret = offset;
  270. out_err:
  271. mutex_unlock(&inode->i_mutex);
  272. return ret;
  273. }
  274. struct fname {
  275. __u32 hash;
  276. __u32 minor_hash;
  277. struct rb_node rb_hash;
  278. struct fname *next;
  279. __u32 inode;
  280. __u8 name_len;
  281. __u8 file_type;
  282. char name[0];
  283. };
  284. static void free_rb_tree_fname(struct rb_root *root)
  285. {
  286. struct rb_node *n = root->rb_node;
  287. struct rb_node *parent;
  288. struct fname *fname;
  289. while (n) {
  290. if (n->rb_left) {
  291. n = n->rb_left;
  292. continue;
  293. }
  294. if (n->rb_right) {
  295. n = n->rb_right;
  296. continue;
  297. }
  298. parent = rb_parent(n);
  299. fname = rb_entry(n, struct fname, rb_hash);
  300. while (fname) {
  301. struct fname *old = fname;
  302. fname = fname->next;
  303. kfree(old);
  304. }
  305. if (!parent)
  306. *root = RB_ROOT;
  307. else if (parent->rb_left == n)
  308. parent->rb_left = NULL;
  309. else if (parent->rb_right == n)
  310. parent->rb_right = NULL;
  311. n = parent;
  312. }
  313. }
  314. static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
  315. loff_t pos)
  316. {
  317. struct dir_private_info *p;
  318. p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
  319. if (!p)
  320. return NULL;
  321. p->curr_hash = pos2maj_hash(filp, pos);
  322. p->curr_minor_hash = pos2min_hash(filp, pos);
  323. return p;
  324. }
  325. void ext4_htree_free_dir_info(struct dir_private_info *p)
  326. {
  327. free_rb_tree_fname(&p->root);
  328. kfree(p);
  329. }
  330. int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  331. __u32 minor_hash,
  332. struct ext4_dir_entry_2 *dirent)
  333. {
  334. struct rb_node **p, *parent = NULL;
  335. struct fname *fname, *new_fn;
  336. struct dir_private_info *info;
  337. int len;
  338. info = dir_file->private_data;
  339. p = &info->root.rb_node;
  340. len = sizeof(struct fname) + dirent->name_len + 1;
  341. new_fn = kzalloc(len, GFP_KERNEL);
  342. if (!new_fn)
  343. return -ENOMEM;
  344. new_fn->hash = hash;
  345. new_fn->minor_hash = minor_hash;
  346. new_fn->inode = le32_to_cpu(dirent->inode);
  347. new_fn->name_len = dirent->name_len;
  348. new_fn->file_type = dirent->file_type;
  349. memcpy(new_fn->name, dirent->name, dirent->name_len);
  350. new_fn->name[dirent->name_len] = 0;
  351. while (*p) {
  352. parent = *p;
  353. fname = rb_entry(parent, struct fname, rb_hash);
  354. if ((new_fn->hash == fname->hash) &&
  355. (new_fn->minor_hash == fname->minor_hash)) {
  356. new_fn->next = fname->next;
  357. fname->next = new_fn;
  358. return 0;
  359. }
  360. if (new_fn->hash < fname->hash)
  361. p = &(*p)->rb_left;
  362. else if (new_fn->hash > fname->hash)
  363. p = &(*p)->rb_right;
  364. else if (new_fn->minor_hash < fname->minor_hash)
  365. p = &(*p)->rb_left;
  366. else
  367. p = &(*p)->rb_right;
  368. }
  369. rb_link_node(&new_fn->rb_hash, parent, p);
  370. rb_insert_color(&new_fn->rb_hash, &info->root);
  371. return 0;
  372. }
  373. static int call_filldir(struct file *filp, void *dirent,
  374. filldir_t filldir, struct fname *fname)
  375. {
  376. struct dir_private_info *info = filp->private_data;
  377. loff_t curr_pos;
  378. struct inode *inode = filp->f_path.dentry->d_inode;
  379. struct super_block *sb;
  380. int error;
  381. sb = inode->i_sb;
  382. if (!fname) {
  383. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
  384. "called with null fname?!?", __func__, __LINE__,
  385. inode->i_ino, current->comm);
  386. return 0;
  387. }
  388. curr_pos = hash2pos(filp, fname->hash, fname->minor_hash);
  389. while (fname) {
  390. error = filldir(dirent, fname->name,
  391. fname->name_len, curr_pos,
  392. fname->inode,
  393. get_dtype(sb, fname->file_type));
  394. if (error) {
  395. filp->f_pos = curr_pos;
  396. info->extra_fname = fname;
  397. return error;
  398. }
  399. fname = fname->next;
  400. }
  401. return 0;
  402. }
  403. static int ext4_dx_readdir(struct file *filp,
  404. void *dirent, filldir_t filldir)
  405. {
  406. struct dir_private_info *info = filp->private_data;
  407. struct inode *inode = filp->f_path.dentry->d_inode;
  408. struct fname *fname;
  409. int ret;
  410. if (!info) {
  411. info = ext4_htree_create_dir_info(filp, filp->f_pos);
  412. if (!info)
  413. return -ENOMEM;
  414. filp->private_data = info;
  415. }
  416. if (filp->f_pos == ext4_get_htree_eof(filp))
  417. return 0;
  418. if (info->last_pos != filp->f_pos) {
  419. free_rb_tree_fname(&info->root);
  420. info->curr_node = NULL;
  421. info->extra_fname = NULL;
  422. info->curr_hash = pos2maj_hash(filp, filp->f_pos);
  423. info->curr_minor_hash = pos2min_hash(filp, filp->f_pos);
  424. }
  425. if (info->extra_fname) {
  426. if (call_filldir(filp, dirent, filldir, info->extra_fname))
  427. goto finished;
  428. info->extra_fname = NULL;
  429. goto next_node;
  430. } else if (!info->curr_node)
  431. info->curr_node = rb_first(&info->root);
  432. while (1) {
  433. if ((!info->curr_node) ||
  434. (filp->f_version != inode->i_version)) {
  435. info->curr_node = NULL;
  436. free_rb_tree_fname(&info->root);
  437. filp->f_version = inode->i_version;
  438. ret = ext4_htree_fill_tree(filp, info->curr_hash,
  439. info->curr_minor_hash,
  440. &info->next_hash);
  441. if (ret < 0)
  442. return ret;
  443. if (ret == 0) {
  444. filp->f_pos = ext4_get_htree_eof(filp);
  445. break;
  446. }
  447. info->curr_node = rb_first(&info->root);
  448. }
  449. fname = rb_entry(info->curr_node, struct fname, rb_hash);
  450. info->curr_hash = fname->hash;
  451. info->curr_minor_hash = fname->minor_hash;
  452. if (call_filldir(filp, dirent, filldir, fname))
  453. break;
  454. next_node:
  455. info->curr_node = rb_next(info->curr_node);
  456. if (info->curr_node) {
  457. fname = rb_entry(info->curr_node, struct fname,
  458. rb_hash);
  459. info->curr_hash = fname->hash;
  460. info->curr_minor_hash = fname->minor_hash;
  461. } else {
  462. if (info->next_hash == ~0) {
  463. filp->f_pos = ext4_get_htree_eof(filp);
  464. break;
  465. }
  466. info->curr_hash = info->next_hash;
  467. info->curr_minor_hash = 0;
  468. }
  469. }
  470. finished:
  471. info->last_pos = filp->f_pos;
  472. return 0;
  473. }
  474. static int ext4_release_dir(struct inode *inode, struct file *filp)
  475. {
  476. if (filp->private_data)
  477. ext4_htree_free_dir_info(filp->private_data);
  478. return 0;
  479. }
  480. const struct file_operations ext4_dir_operations = {
  481. .llseek = ext4_dir_llseek,
  482. .read = generic_read_dir,
  483. .readdir = ext4_readdir,
  484. .unlocked_ioctl = ext4_ioctl,
  485. #ifdef CONFIG_COMPAT
  486. .compat_ioctl = ext4_compat_ioctl,
  487. #endif
  488. .fsync = ext4_sync_file,
  489. .release = ext4_release_dir,
  490. };