PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ext4/namei.c

https://github.com/mstsirkin/linux
C | 1824 lines | 1411 code | 153 blank | 260 comment | 240 complexity | c9a91e9ba962cb28f864837a42237ddf MD5 | raw file
  1. /*
  2. * linux/fs/ext4/namei.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/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/time.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/bio.h>
  36. #include "ext4.h"
  37. #include "ext4_jbd2.h"
  38. #include "xattr.h"
  39. #include "acl.h"
  40. #include <trace/events/ext4.h>
  41. /*
  42. * define how far ahead to read directories while searching them.
  43. */
  44. #define NAMEI_RA_CHUNKS 2
  45. #define NAMEI_RA_BLOCKS 4
  46. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  47. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  48. static struct buffer_head *ext4_append(handle_t *handle,
  49. struct inode *inode,
  50. ext4_lblk_t *block, int *err)
  51. {
  52. struct buffer_head *bh;
  53. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  54. bh = ext4_bread(handle, inode, *block, 1, err);
  55. if (bh) {
  56. inode->i_size += inode->i_sb->s_blocksize;
  57. EXT4_I(inode)->i_disksize = inode->i_size;
  58. *err = ext4_journal_get_write_access(handle, bh);
  59. if (*err) {
  60. brelse(bh);
  61. bh = NULL;
  62. }
  63. }
  64. return bh;
  65. }
  66. #ifndef assert
  67. #define assert(test) J_ASSERT(test)
  68. #endif
  69. #ifdef DX_DEBUG
  70. #define dxtrace(command) command
  71. #else
  72. #define dxtrace(command)
  73. #endif
  74. struct fake_dirent
  75. {
  76. __le32 inode;
  77. __le16 rec_len;
  78. u8 name_len;
  79. u8 file_type;
  80. };
  81. struct dx_countlimit
  82. {
  83. __le16 limit;
  84. __le16 count;
  85. };
  86. struct dx_entry
  87. {
  88. __le32 hash;
  89. __le32 block;
  90. };
  91. /*
  92. * dx_root_info is laid out so that if it should somehow get overlaid by a
  93. * dirent the two low bits of the hash version will be zero. Therefore, the
  94. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  95. */
  96. struct dx_root
  97. {
  98. struct fake_dirent dot;
  99. char dot_name[4];
  100. struct fake_dirent dotdot;
  101. char dotdot_name[4];
  102. struct dx_root_info
  103. {
  104. __le32 reserved_zero;
  105. u8 hash_version;
  106. u8 info_length; /* 8 */
  107. u8 indirect_levels;
  108. u8 unused_flags;
  109. }
  110. info;
  111. struct dx_entry entries[0];
  112. };
  113. struct dx_node
  114. {
  115. struct fake_dirent fake;
  116. struct dx_entry entries[0];
  117. };
  118. struct dx_frame
  119. {
  120. struct buffer_head *bh;
  121. struct dx_entry *entries;
  122. struct dx_entry *at;
  123. };
  124. struct dx_map_entry
  125. {
  126. u32 hash;
  127. u16 offs;
  128. u16 size;
  129. };
  130. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  131. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  132. static inline unsigned dx_get_hash(struct dx_entry *entry);
  133. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  134. static unsigned dx_get_count(struct dx_entry *entries);
  135. static unsigned dx_get_limit(struct dx_entry *entries);
  136. static void dx_set_count(struct dx_entry *entries, unsigned value);
  137. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  138. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  139. static unsigned dx_node_limit(struct inode *dir);
  140. static struct dx_frame *dx_probe(const struct qstr *d_name,
  141. struct inode *dir,
  142. struct dx_hash_info *hinfo,
  143. struct dx_frame *frame,
  144. int *err);
  145. static void dx_release(struct dx_frame *frames);
  146. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  147. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  148. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  149. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  150. struct dx_map_entry *offsets, int count, unsigned blocksize);
  151. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  152. static void dx_insert_block(struct dx_frame *frame,
  153. u32 hash, ext4_lblk_t block);
  154. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  155. struct dx_frame *frame,
  156. struct dx_frame *frames,
  157. __u32 *start_hash);
  158. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  159. const struct qstr *d_name,
  160. struct ext4_dir_entry_2 **res_dir,
  161. int *err);
  162. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  163. struct inode *inode);
  164. /*
  165. * p is at least 6 bytes before the end of page
  166. */
  167. static inline struct ext4_dir_entry_2 *
  168. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  169. {
  170. return (struct ext4_dir_entry_2 *)((char *)p +
  171. ext4_rec_len_from_disk(p->rec_len, blocksize));
  172. }
  173. /*
  174. * Future: use high four bits of block for coalesce-on-delete flags
  175. * Mask them off for now.
  176. */
  177. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  178. {
  179. return le32_to_cpu(entry->block) & 0x00ffffff;
  180. }
  181. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  182. {
  183. entry->block = cpu_to_le32(value);
  184. }
  185. static inline unsigned dx_get_hash(struct dx_entry *entry)
  186. {
  187. return le32_to_cpu(entry->hash);
  188. }
  189. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  190. {
  191. entry->hash = cpu_to_le32(value);
  192. }
  193. static inline unsigned dx_get_count(struct dx_entry *entries)
  194. {
  195. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  196. }
  197. static inline unsigned dx_get_limit(struct dx_entry *entries)
  198. {
  199. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  200. }
  201. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  202. {
  203. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  204. }
  205. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  206. {
  207. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  208. }
  209. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  210. {
  211. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  212. EXT4_DIR_REC_LEN(2) - infosize;
  213. return entry_space / sizeof(struct dx_entry);
  214. }
  215. static inline unsigned dx_node_limit(struct inode *dir)
  216. {
  217. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  218. return entry_space / sizeof(struct dx_entry);
  219. }
  220. /*
  221. * Debug
  222. */
  223. #ifdef DX_DEBUG
  224. static void dx_show_index(char * label, struct dx_entry *entries)
  225. {
  226. int i, n = dx_get_count (entries);
  227. printk(KERN_DEBUG "%s index ", label);
  228. for (i = 0; i < n; i++) {
  229. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  230. 0, (unsigned long)dx_get_block(entries + i));
  231. }
  232. printk("\n");
  233. }
  234. struct stats
  235. {
  236. unsigned names;
  237. unsigned space;
  238. unsigned bcount;
  239. };
  240. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  241. int size, int show_names)
  242. {
  243. unsigned names = 0, space = 0;
  244. char *base = (char *) de;
  245. struct dx_hash_info h = *hinfo;
  246. printk("names: ");
  247. while ((char *) de < base + size)
  248. {
  249. if (de->inode)
  250. {
  251. if (show_names)
  252. {
  253. int len = de->name_len;
  254. char *name = de->name;
  255. while (len--) printk("%c", *name++);
  256. ext4fs_dirhash(de->name, de->name_len, &h);
  257. printk(":%x.%u ", h.hash,
  258. (unsigned) ((char *) de - base));
  259. }
  260. space += EXT4_DIR_REC_LEN(de->name_len);
  261. names++;
  262. }
  263. de = ext4_next_entry(de, size);
  264. }
  265. printk("(%i)\n", names);
  266. return (struct stats) { names, space, 1 };
  267. }
  268. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  269. struct dx_entry *entries, int levels)
  270. {
  271. unsigned blocksize = dir->i_sb->s_blocksize;
  272. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  273. unsigned bcount = 0;
  274. struct buffer_head *bh;
  275. int err;
  276. printk("%i indexed blocks...\n", count);
  277. for (i = 0; i < count; i++, entries++)
  278. {
  279. ext4_lblk_t block = dx_get_block(entries);
  280. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  281. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  282. struct stats stats;
  283. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  284. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  285. stats = levels?
  286. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  287. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  288. names += stats.names;
  289. space += stats.space;
  290. bcount += stats.bcount;
  291. brelse(bh);
  292. }
  293. if (bcount)
  294. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  295. levels ? "" : " ", names, space/bcount,
  296. (space/bcount)*100/blocksize);
  297. return (struct stats) { names, space, bcount};
  298. }
  299. #endif /* DX_DEBUG */
  300. /*
  301. * Probe for a directory leaf block to search.
  302. *
  303. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  304. * error in the directory index, and the caller should fall back to
  305. * searching the directory normally. The callers of dx_probe **MUST**
  306. * check for this error code, and make sure it never gets reflected
  307. * back to userspace.
  308. */
  309. static struct dx_frame *
  310. dx_probe(const struct qstr *d_name, struct inode *dir,
  311. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  312. {
  313. unsigned count, indirect;
  314. struct dx_entry *at, *entries, *p, *q, *m;
  315. struct dx_root *root;
  316. struct buffer_head *bh;
  317. struct dx_frame *frame = frame_in;
  318. u32 hash;
  319. frame->bh = NULL;
  320. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  321. goto fail;
  322. root = (struct dx_root *) bh->b_data;
  323. if (root->info.hash_version != DX_HASH_TEA &&
  324. root->info.hash_version != DX_HASH_HALF_MD4 &&
  325. root->info.hash_version != DX_HASH_LEGACY) {
  326. ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
  327. root->info.hash_version);
  328. brelse(bh);
  329. *err = ERR_BAD_DX_DIR;
  330. goto fail;
  331. }
  332. hinfo->hash_version = root->info.hash_version;
  333. if (hinfo->hash_version <= DX_HASH_TEA)
  334. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  335. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  336. if (d_name)
  337. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  338. hash = hinfo->hash;
  339. if (root->info.unused_flags & 1) {
  340. ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
  341. root->info.unused_flags);
  342. brelse(bh);
  343. *err = ERR_BAD_DX_DIR;
  344. goto fail;
  345. }
  346. if ((indirect = root->info.indirect_levels) > 1) {
  347. ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
  348. root->info.indirect_levels);
  349. brelse(bh);
  350. *err = ERR_BAD_DX_DIR;
  351. goto fail;
  352. }
  353. entries = (struct dx_entry *) (((char *)&root->info) +
  354. root->info.info_length);
  355. if (dx_get_limit(entries) != dx_root_limit(dir,
  356. root->info.info_length)) {
  357. ext4_warning(dir->i_sb, "dx entry: limit != root limit");
  358. brelse(bh);
  359. *err = ERR_BAD_DX_DIR;
  360. goto fail;
  361. }
  362. dxtrace(printk("Look up %x", hash));
  363. while (1)
  364. {
  365. count = dx_get_count(entries);
  366. if (!count || count > dx_get_limit(entries)) {
  367. ext4_warning(dir->i_sb,
  368. "dx entry: no count or count > limit");
  369. brelse(bh);
  370. *err = ERR_BAD_DX_DIR;
  371. goto fail2;
  372. }
  373. p = entries + 1;
  374. q = entries + count - 1;
  375. while (p <= q)
  376. {
  377. m = p + (q - p)/2;
  378. dxtrace(printk("."));
  379. if (dx_get_hash(m) > hash)
  380. q = m - 1;
  381. else
  382. p = m + 1;
  383. }
  384. if (0) // linear search cross check
  385. {
  386. unsigned n = count - 1;
  387. at = entries;
  388. while (n--)
  389. {
  390. dxtrace(printk(","));
  391. if (dx_get_hash(++at) > hash)
  392. {
  393. at--;
  394. break;
  395. }
  396. }
  397. assert (at == p - 1);
  398. }
  399. at = p - 1;
  400. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  401. frame->bh = bh;
  402. frame->entries = entries;
  403. frame->at = at;
  404. if (!indirect--) return frame;
  405. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  406. goto fail2;
  407. at = entries = ((struct dx_node *) bh->b_data)->entries;
  408. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  409. ext4_warning(dir->i_sb,
  410. "dx entry: limit != node limit");
  411. brelse(bh);
  412. *err = ERR_BAD_DX_DIR;
  413. goto fail2;
  414. }
  415. frame++;
  416. frame->bh = NULL;
  417. }
  418. fail2:
  419. while (frame >= frame_in) {
  420. brelse(frame->bh);
  421. frame--;
  422. }
  423. fail:
  424. if (*err == ERR_BAD_DX_DIR)
  425. ext4_warning(dir->i_sb,
  426. "Corrupt dir inode %ld, running e2fsck is "
  427. "recommended.", dir->i_ino);
  428. return NULL;
  429. }
  430. static void dx_release (struct dx_frame *frames)
  431. {
  432. if (frames[0].bh == NULL)
  433. return;
  434. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  435. brelse(frames[1].bh);
  436. brelse(frames[0].bh);
  437. }
  438. /*
  439. * This function increments the frame pointer to search the next leaf
  440. * block, and reads in the necessary intervening nodes if the search
  441. * should be necessary. Whether or not the search is necessary is
  442. * controlled by the hash parameter. If the hash value is even, then
  443. * the search is only continued if the next block starts with that
  444. * hash value. This is used if we are searching for a specific file.
  445. *
  446. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  447. *
  448. * This function returns 1 if the caller should continue to search,
  449. * or 0 if it should not. If there is an error reading one of the
  450. * index blocks, it will a negative error code.
  451. *
  452. * If start_hash is non-null, it will be filled in with the starting
  453. * hash of the next page.
  454. */
  455. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  456. struct dx_frame *frame,
  457. struct dx_frame *frames,
  458. __u32 *start_hash)
  459. {
  460. struct dx_frame *p;
  461. struct buffer_head *bh;
  462. int err, num_frames = 0;
  463. __u32 bhash;
  464. p = frame;
  465. /*
  466. * Find the next leaf page by incrementing the frame pointer.
  467. * If we run out of entries in the interior node, loop around and
  468. * increment pointer in the parent node. When we break out of
  469. * this loop, num_frames indicates the number of interior
  470. * nodes need to be read.
  471. */
  472. while (1) {
  473. if (++(p->at) < p->entries + dx_get_count(p->entries))
  474. break;
  475. if (p == frames)
  476. return 0;
  477. num_frames++;
  478. p--;
  479. }
  480. /*
  481. * If the hash is 1, then continue only if the next page has a
  482. * continuation hash of any value. This is used for readdir
  483. * handling. Otherwise, check to see if the hash matches the
  484. * desired contiuation hash. If it doesn't, return since
  485. * there's no point to read in the successive index pages.
  486. */
  487. bhash = dx_get_hash(p->at);
  488. if (start_hash)
  489. *start_hash = bhash;
  490. if ((hash & 1) == 0) {
  491. if ((bhash & ~1) != hash)
  492. return 0;
  493. }
  494. /*
  495. * If the hash is HASH_NB_ALWAYS, we always go to the next
  496. * block so no check is necessary
  497. */
  498. while (num_frames--) {
  499. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  500. 0, &err)))
  501. return err; /* Failure */
  502. p++;
  503. brelse(p->bh);
  504. p->bh = bh;
  505. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  506. }
  507. return 1;
  508. }
  509. /*
  510. * This function fills a red-black tree with information from a
  511. * directory block. It returns the number directory entries loaded
  512. * into the tree. If there is an error it is returned in err.
  513. */
  514. static int htree_dirblock_to_tree(struct file *dir_file,
  515. struct inode *dir, ext4_lblk_t block,
  516. struct dx_hash_info *hinfo,
  517. __u32 start_hash, __u32 start_minor_hash)
  518. {
  519. struct buffer_head *bh;
  520. struct ext4_dir_entry_2 *de, *top;
  521. int err, count = 0;
  522. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  523. (unsigned long)block));
  524. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  525. return err;
  526. de = (struct ext4_dir_entry_2 *) bh->b_data;
  527. top = (struct ext4_dir_entry_2 *) ((char *) de +
  528. dir->i_sb->s_blocksize -
  529. EXT4_DIR_REC_LEN(0));
  530. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  531. if (ext4_check_dir_entry(dir, NULL, de, bh,
  532. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  533. + ((char *)de - bh->b_data))) {
  534. /* On error, skip the f_pos to the next block. */
  535. dir_file->f_pos = (dir_file->f_pos |
  536. (dir->i_sb->s_blocksize - 1)) + 1;
  537. brelse(bh);
  538. return count;
  539. }
  540. ext4fs_dirhash(de->name, de->name_len, hinfo);
  541. if ((hinfo->hash < start_hash) ||
  542. ((hinfo->hash == start_hash) &&
  543. (hinfo->minor_hash < start_minor_hash)))
  544. continue;
  545. if (de->inode == 0)
  546. continue;
  547. if ((err = ext4_htree_store_dirent(dir_file,
  548. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  549. brelse(bh);
  550. return err;
  551. }
  552. count++;
  553. }
  554. brelse(bh);
  555. return count;
  556. }
  557. /*
  558. * This function fills a red-black tree with information from a
  559. * directory. We start scanning the directory in hash order, starting
  560. * at start_hash and start_minor_hash.
  561. *
  562. * This function returns the number of entries inserted into the tree,
  563. * or a negative error code.
  564. */
  565. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  566. __u32 start_minor_hash, __u32 *next_hash)
  567. {
  568. struct dx_hash_info hinfo;
  569. struct ext4_dir_entry_2 *de;
  570. struct dx_frame frames[2], *frame;
  571. struct inode *dir;
  572. ext4_lblk_t block;
  573. int count = 0;
  574. int ret, err;
  575. __u32 hashval;
  576. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  577. start_hash, start_minor_hash));
  578. dir = dir_file->f_path.dentry->d_inode;
  579. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  580. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  581. if (hinfo.hash_version <= DX_HASH_TEA)
  582. hinfo.hash_version +=
  583. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  584. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  585. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  586. start_hash, start_minor_hash);
  587. *next_hash = ~0;
  588. return count;
  589. }
  590. hinfo.hash = start_hash;
  591. hinfo.minor_hash = 0;
  592. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  593. if (!frame)
  594. return err;
  595. /* Add '.' and '..' from the htree header */
  596. if (!start_hash && !start_minor_hash) {
  597. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  598. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  599. goto errout;
  600. count++;
  601. }
  602. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  603. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  604. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  605. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  606. goto errout;
  607. count++;
  608. }
  609. while (1) {
  610. block = dx_get_block(frame->at);
  611. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  612. start_hash, start_minor_hash);
  613. if (ret < 0) {
  614. err = ret;
  615. goto errout;
  616. }
  617. count += ret;
  618. hashval = ~0;
  619. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  620. frame, frames, &hashval);
  621. *next_hash = hashval;
  622. if (ret < 0) {
  623. err = ret;
  624. goto errout;
  625. }
  626. /*
  627. * Stop if: (a) there are no more entries, or
  628. * (b) we have inserted at least one entry and the
  629. * next hash value is not a continuation
  630. */
  631. if ((ret == 0) ||
  632. (count && ((hashval & 1) == 0)))
  633. break;
  634. }
  635. dx_release(frames);
  636. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  637. "next hash: %x\n", count, *next_hash));
  638. return count;
  639. errout:
  640. dx_release(frames);
  641. return (err);
  642. }
  643. /*
  644. * Directory block splitting, compacting
  645. */
  646. /*
  647. * Create map of hash values, offsets, and sizes, stored at end of block.
  648. * Returns number of entries mapped.
  649. */
  650. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  651. struct dx_hash_info *hinfo,
  652. struct dx_map_entry *map_tail)
  653. {
  654. int count = 0;
  655. char *base = (char *) de;
  656. struct dx_hash_info h = *hinfo;
  657. while ((char *) de < base + blocksize) {
  658. if (de->name_len && de->inode) {
  659. ext4fs_dirhash(de->name, de->name_len, &h);
  660. map_tail--;
  661. map_tail->hash = h.hash;
  662. map_tail->offs = ((char *) de - base)>>2;
  663. map_tail->size = le16_to_cpu(de->rec_len);
  664. count++;
  665. cond_resched();
  666. }
  667. /* XXX: do we need to check rec_len == 0 case? -Chris */
  668. de = ext4_next_entry(de, blocksize);
  669. }
  670. return count;
  671. }
  672. /* Sort map by hash value */
  673. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  674. {
  675. struct dx_map_entry *p, *q, *top = map + count - 1;
  676. int more;
  677. /* Combsort until bubble sort doesn't suck */
  678. while (count > 2) {
  679. count = count*10/13;
  680. if (count - 9 < 2) /* 9, 10 -> 11 */
  681. count = 11;
  682. for (p = top, q = p - count; q >= map; p--, q--)
  683. if (p->hash < q->hash)
  684. swap(*p, *q);
  685. }
  686. /* Garden variety bubble sort */
  687. do {
  688. more = 0;
  689. q = top;
  690. while (q-- > map) {
  691. if (q[1].hash >= q[0].hash)
  692. continue;
  693. swap(*(q+1), *q);
  694. more = 1;
  695. }
  696. } while(more);
  697. }
  698. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  699. {
  700. struct dx_entry *entries = frame->entries;
  701. struct dx_entry *old = frame->at, *new = old + 1;
  702. int count = dx_get_count(entries);
  703. assert(count < dx_get_limit(entries));
  704. assert(old < entries + count);
  705. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  706. dx_set_hash(new, hash);
  707. dx_set_block(new, block);
  708. dx_set_count(entries, count + 1);
  709. }
  710. static void ext4_update_dx_flag(struct inode *inode)
  711. {
  712. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  713. EXT4_FEATURE_COMPAT_DIR_INDEX))
  714. ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
  715. }
  716. /*
  717. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  718. *
  719. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  720. * `de != NULL' is guaranteed by caller.
  721. */
  722. static inline int ext4_match (int len, const char * const name,
  723. struct ext4_dir_entry_2 * de)
  724. {
  725. if (len != de->name_len)
  726. return 0;
  727. if (!de->inode)
  728. return 0;
  729. return !memcmp(name, de->name, len);
  730. }
  731. /*
  732. * Returns 0 if not found, -1 on failure, and 1 on success
  733. */
  734. static inline int search_dirblock(struct buffer_head *bh,
  735. struct inode *dir,
  736. const struct qstr *d_name,
  737. unsigned int offset,
  738. struct ext4_dir_entry_2 ** res_dir)
  739. {
  740. struct ext4_dir_entry_2 * de;
  741. char * dlimit;
  742. int de_len;
  743. const char *name = d_name->name;
  744. int namelen = d_name->len;
  745. de = (struct ext4_dir_entry_2 *) bh->b_data;
  746. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  747. while ((char *) de < dlimit) {
  748. /* this code is executed quadratically often */
  749. /* do minimal checking `by hand' */
  750. if ((char *) de + namelen <= dlimit &&
  751. ext4_match (namelen, name, de)) {
  752. /* found a match - just to be sure, do a full check */
  753. if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
  754. return -1;
  755. *res_dir = de;
  756. return 1;
  757. }
  758. /* prevent looping on a bad block */
  759. de_len = ext4_rec_len_from_disk(de->rec_len,
  760. dir->i_sb->s_blocksize);
  761. if (de_len <= 0)
  762. return -1;
  763. offset += de_len;
  764. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  765. }
  766. return 0;
  767. }
  768. /*
  769. * ext4_find_entry()
  770. *
  771. * finds an entry in the specified directory with the wanted name. It
  772. * returns the cache buffer in which the entry was found, and the entry
  773. * itself (as a parameter - res_dir). It does NOT read the inode of the
  774. * entry - you'll have to do that yourself if you want to.
  775. *
  776. * The returned buffer_head has ->b_count elevated. The caller is expected
  777. * to brelse() it when appropriate.
  778. */
  779. static struct buffer_head * ext4_find_entry (struct inode *dir,
  780. const struct qstr *d_name,
  781. struct ext4_dir_entry_2 ** res_dir)
  782. {
  783. struct super_block *sb;
  784. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  785. struct buffer_head *bh, *ret = NULL;
  786. ext4_lblk_t start, block, b;
  787. const u8 *name = d_name->name;
  788. int ra_max = 0; /* Number of bh's in the readahead
  789. buffer, bh_use[] */
  790. int ra_ptr = 0; /* Current index into readahead
  791. buffer */
  792. int num = 0;
  793. ext4_lblk_t nblocks;
  794. int i, err;
  795. int namelen;
  796. *res_dir = NULL;
  797. sb = dir->i_sb;
  798. namelen = d_name->len;
  799. if (namelen > EXT4_NAME_LEN)
  800. return NULL;
  801. if ((namelen <= 2) && (name[0] == '.') &&
  802. (name[1] == '.' || name[1] == '\0')) {
  803. /*
  804. * "." or ".." will only be in the first block
  805. * NFS may look up ".."; "." should be handled by the VFS
  806. */
  807. block = start = 0;
  808. nblocks = 1;
  809. goto restart;
  810. }
  811. if (is_dx(dir)) {
  812. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  813. /*
  814. * On success, or if the error was file not found,
  815. * return. Otherwise, fall back to doing a search the
  816. * old fashioned way.
  817. */
  818. if (bh || (err != ERR_BAD_DX_DIR))
  819. return bh;
  820. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  821. "falling back\n"));
  822. }
  823. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  824. start = EXT4_I(dir)->i_dir_start_lookup;
  825. if (start >= nblocks)
  826. start = 0;
  827. block = start;
  828. restart:
  829. do {
  830. /*
  831. * We deal with the read-ahead logic here.
  832. */
  833. if (ra_ptr >= ra_max) {
  834. /* Refill the readahead buffer */
  835. ra_ptr = 0;
  836. b = block;
  837. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  838. /*
  839. * Terminate if we reach the end of the
  840. * directory and must wrap, or if our
  841. * search has finished at this block.
  842. */
  843. if (b >= nblocks || (num && block == start)) {
  844. bh_use[ra_max] = NULL;
  845. break;
  846. }
  847. num++;
  848. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  849. bh_use[ra_max] = bh;
  850. if (bh)
  851. ll_rw_block(READ_META, 1, &bh);
  852. }
  853. }
  854. if ((bh = bh_use[ra_ptr++]) == NULL)
  855. goto next;
  856. wait_on_buffer(bh);
  857. if (!buffer_uptodate(bh)) {
  858. /* read error, skip block & hope for the best */
  859. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  860. (unsigned long) block);
  861. brelse(bh);
  862. goto next;
  863. }
  864. i = search_dirblock(bh, dir, d_name,
  865. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  866. if (i == 1) {
  867. EXT4_I(dir)->i_dir_start_lookup = block;
  868. ret = bh;
  869. goto cleanup_and_exit;
  870. } else {
  871. brelse(bh);
  872. if (i < 0)
  873. goto cleanup_and_exit;
  874. }
  875. next:
  876. if (++block >= nblocks)
  877. block = 0;
  878. } while (block != start);
  879. /*
  880. * If the directory has grown while we were searching, then
  881. * search the last part of the directory before giving up.
  882. */
  883. block = nblocks;
  884. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  885. if (block < nblocks) {
  886. start = 0;
  887. goto restart;
  888. }
  889. cleanup_and_exit:
  890. /* Clean up the read-ahead blocks */
  891. for (; ra_ptr < ra_max; ra_ptr++)
  892. brelse(bh_use[ra_ptr]);
  893. return ret;
  894. }
  895. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  896. struct ext4_dir_entry_2 **res_dir, int *err)
  897. {
  898. struct super_block * sb = dir->i_sb;
  899. struct dx_hash_info hinfo;
  900. struct dx_frame frames[2], *frame;
  901. struct buffer_head *bh;
  902. ext4_lblk_t block;
  903. int retval;
  904. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  905. return NULL;
  906. do {
  907. block = dx_get_block(frame->at);
  908. if (!(bh = ext4_bread(NULL, dir, block, 0, err)))
  909. goto errout;
  910. retval = search_dirblock(bh, dir, d_name,
  911. block << EXT4_BLOCK_SIZE_BITS(sb),
  912. res_dir);
  913. if (retval == 1) { /* Success! */
  914. dx_release(frames);
  915. return bh;
  916. }
  917. brelse(bh);
  918. if (retval == -1) {
  919. *err = ERR_BAD_DX_DIR;
  920. goto errout;
  921. }
  922. /* Check to see if we should continue to search */
  923. retval = ext4_htree_next_block(dir, hinfo.hash, frame,
  924. frames, NULL);
  925. if (retval < 0) {
  926. ext4_warning(sb,
  927. "error reading index page in directory #%lu",
  928. dir->i_ino);
  929. *err = retval;
  930. goto errout;
  931. }
  932. } while (retval == 1);
  933. *err = -ENOENT;
  934. errout:
  935. dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
  936. dx_release (frames);
  937. return NULL;
  938. }
  939. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  940. {
  941. struct inode *inode;
  942. struct ext4_dir_entry_2 *de;
  943. struct buffer_head *bh;
  944. if (dentry->d_name.len > EXT4_NAME_LEN)
  945. return ERR_PTR(-ENAMETOOLONG);
  946. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  947. inode = NULL;
  948. if (bh) {
  949. __u32 ino = le32_to_cpu(de->inode);
  950. brelse(bh);
  951. if (!ext4_valid_inum(dir->i_sb, ino)) {
  952. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  953. return ERR_PTR(-EIO);
  954. }
  955. inode = ext4_iget(dir->i_sb, ino);
  956. if (inode == ERR_PTR(-ESTALE)) {
  957. EXT4_ERROR_INODE(dir,
  958. "deleted inode referenced: %u",
  959. ino);
  960. return ERR_PTR(-EIO);
  961. }
  962. }
  963. return d_splice_alias(inode, dentry);
  964. }
  965. struct dentry *ext4_get_parent(struct dentry *child)
  966. {
  967. __u32 ino;
  968. static const struct qstr dotdot = {
  969. .name = "..",
  970. .len = 2,
  971. };
  972. struct ext4_dir_entry_2 * de;
  973. struct buffer_head *bh;
  974. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  975. if (!bh)
  976. return ERR_PTR(-ENOENT);
  977. ino = le32_to_cpu(de->inode);
  978. brelse(bh);
  979. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  980. EXT4_ERROR_INODE(child->d_inode,
  981. "bad parent inode number: %u", ino);
  982. return ERR_PTR(-EIO);
  983. }
  984. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  985. }
  986. #define S_SHIFT 12
  987. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  988. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  989. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  990. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  991. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  992. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  993. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  994. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  995. };
  996. static inline void ext4_set_de_type(struct super_block *sb,
  997. struct ext4_dir_entry_2 *de,
  998. umode_t mode) {
  999. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1000. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1001. }
  1002. /*
  1003. * Move count entries from end of map between two memory locations.
  1004. * Returns pointer to last entry moved.
  1005. */
  1006. static struct ext4_dir_entry_2 *
  1007. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1008. unsigned blocksize)
  1009. {
  1010. unsigned rec_len = 0;
  1011. while (count--) {
  1012. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1013. (from + (map->offs<<2));
  1014. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1015. memcpy (to, de, rec_len);
  1016. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1017. ext4_rec_len_to_disk(rec_len, blocksize);
  1018. de->inode = 0;
  1019. map++;
  1020. to += rec_len;
  1021. }
  1022. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1023. }
  1024. /*
  1025. * Compact each dir entry in the range to the minimal rec_len.
  1026. * Returns pointer to last entry in range.
  1027. */
  1028. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1029. {
  1030. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1031. unsigned rec_len = 0;
  1032. prev = to = de;
  1033. while ((char*)de < base + blocksize) {
  1034. next = ext4_next_entry(de, blocksize);
  1035. if (de->inode && de->name_len) {
  1036. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1037. if (de > to)
  1038. memmove(to, de, rec_len);
  1039. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1040. prev = to;
  1041. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1042. }
  1043. de = next;
  1044. }
  1045. return prev;
  1046. }
  1047. /*
  1048. * Split a full leaf block to make room for a new dir entry.
  1049. * Allocate a new block, and move entries so that they are approx. equally full.
  1050. * Returns pointer to de in block into which the new entry will be inserted.
  1051. */
  1052. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1053. struct buffer_head **bh,struct dx_frame *frame,
  1054. struct dx_hash_info *hinfo, int *error)
  1055. {
  1056. unsigned blocksize = dir->i_sb->s_blocksize;
  1057. unsigned count, continued;
  1058. struct buffer_head *bh2;
  1059. ext4_lblk_t newblock;
  1060. u32 hash2;
  1061. struct dx_map_entry *map;
  1062. char *data1 = (*bh)->b_data, *data2;
  1063. unsigned split, move, size;
  1064. struct ext4_dir_entry_2 *de = NULL, *de2;
  1065. int err = 0, i;
  1066. bh2 = ext4_append (handle, dir, &newblock, &err);
  1067. if (!(bh2)) {
  1068. brelse(*bh);
  1069. *bh = NULL;
  1070. goto errout;
  1071. }
  1072. BUFFER_TRACE(*bh, "get_write_access");
  1073. err = ext4_journal_get_write_access(handle, *bh);
  1074. if (err)
  1075. goto journal_error;
  1076. BUFFER_TRACE(frame->bh, "get_write_access");
  1077. err = ext4_journal_get_write_access(handle, frame->bh);
  1078. if (err)
  1079. goto journal_error;
  1080. data2 = bh2->b_data;
  1081. /* create map in the end of data2 block */
  1082. map = (struct dx_map_entry *) (data2 + blocksize);
  1083. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1084. blocksize, hinfo, map);
  1085. map -= count;
  1086. dx_sort_map(map, count);
  1087. /* Split the existing block in the middle, size-wise */
  1088. size = 0;
  1089. move = 0;
  1090. for (i = count-1; i >= 0; i--) {
  1091. /* is more than half of this entry in 2nd half of the block? */
  1092. if (size + map[i].size/2 > blocksize/2)
  1093. break;
  1094. size += map[i].size;
  1095. move++;
  1096. }
  1097. /* map index at which we will split */
  1098. split = count - move;
  1099. hash2 = map[split].hash;
  1100. continued = hash2 == map[split - 1].hash;
  1101. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1102. (unsigned long)dx_get_block(frame->at),
  1103. hash2, split, count-split));
  1104. /* Fancy dance to stay within two buffers */
  1105. de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
  1106. de = dx_pack_dirents(data1, blocksize);
  1107. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1108. blocksize);
  1109. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2,
  1110. blocksize);
  1111. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1112. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1113. /* Which block gets the new entry? */
  1114. if (hinfo->hash >= hash2)
  1115. {
  1116. swap(*bh, bh2);
  1117. de = de2;
  1118. }
  1119. dx_insert_block(frame, hash2 + continued, newblock);
  1120. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1121. if (err)
  1122. goto journal_error;
  1123. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1124. if (err)
  1125. goto journal_error;
  1126. brelse(bh2);
  1127. dxtrace(dx_show_index("frame", frame->entries));
  1128. return de;
  1129. journal_error:
  1130. brelse(*bh);
  1131. brelse(bh2);
  1132. *bh = NULL;
  1133. ext4_std_error(dir->i_sb, err);
  1134. errout:
  1135. *error = err;
  1136. return NULL;
  1137. }
  1138. /*
  1139. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1140. * it points to a directory entry which is guaranteed to be large
  1141. * enough for new directory entry. If de is NULL, then
  1142. * add_dirent_to_buf will attempt search the directory block for
  1143. * space. It will return -ENOSPC if no space is available, and -EIO
  1144. * and -EEXIST if directory entry already exists.
  1145. */
  1146. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1147. struct inode *inode, struct ext4_dir_entry_2 *de,
  1148. struct buffer_head *bh)
  1149. {
  1150. struct inode *dir = dentry->d_parent->d_inode;
  1151. const char *name = dentry->d_name.name;
  1152. int namelen = dentry->d_name.len;
  1153. unsigned int offset = 0;
  1154. unsigned int blocksize = dir->i_sb->s_blocksize;
  1155. unsigned short reclen;
  1156. int nlen, rlen, err;
  1157. char *top;
  1158. reclen = EXT4_DIR_REC_LEN(namelen);
  1159. if (!de) {
  1160. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1161. top = bh->b_data + blocksize - reclen;
  1162. while ((char *) de <= top) {
  1163. if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
  1164. return -EIO;
  1165. if (ext4_match(namelen, name, de))
  1166. return -EEXIST;
  1167. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1168. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1169. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1170. break;
  1171. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1172. offset += rlen;
  1173. }
  1174. if ((char *) de > top)
  1175. return -ENOSPC;
  1176. }
  1177. BUFFER_TRACE(bh, "get_write_access");
  1178. err = ext4_journal_get_write_access(handle, bh);
  1179. if (err) {
  1180. ext4_std_error(dir->i_sb, err);
  1181. return err;
  1182. }
  1183. /* By now the buffer is marked for journaling */
  1184. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1185. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1186. if (de->inode) {
  1187. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1188. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
  1189. de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
  1190. de = de1;
  1191. }
  1192. de->file_type = EXT4_FT_UNKNOWN;
  1193. if (inode) {
  1194. de->inode = cpu_to_le32(inode->i_ino);
  1195. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1196. } else
  1197. de->inode = 0;
  1198. de->name_len = namelen;
  1199. memcpy(de->name, name, namelen);
  1200. /*
  1201. * XXX shouldn't update any times until successful
  1202. * completion of syscall, but too many callers depend
  1203. * on this.
  1204. *
  1205. * XXX similarly, too many callers depend on
  1206. * ext4_new_inode() setting the times, but error
  1207. * recovery deletes the inode, so the worst that can
  1208. * happen is that the times are slightly out of date
  1209. * and/or different from the directory change time.
  1210. */
  1211. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1212. ext4_update_dx_flag(dir);
  1213. dir->i_version++;
  1214. ext4_mark_inode_dirty(handle, dir);
  1215. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1216. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1217. if (err)
  1218. ext4_std_error(dir->i_sb, err);
  1219. return 0;
  1220. }
  1221. /*
  1222. * This converts a one block unindexed directory to a 3 block indexed
  1223. * directory, and adds the dentry to the indexed directory.
  1224. */
  1225. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1226. struct inode *inode, struct buffer_head *bh)
  1227. {
  1228. struct inode *dir = dentry->d_parent->d_inode;
  1229. const char *name = dentry->d_name.name;
  1230. int namelen = dentry->d_name.len;
  1231. struct buffer_head *bh2;
  1232. struct dx_root *root;
  1233. struct dx_frame frames[2], *frame;
  1234. struct dx_entry *entries;
  1235. struct ext4_dir_entry_2 *de, *de2;
  1236. char *data1, *top;
  1237. unsigned len;
  1238. int retval;
  1239. unsigned blocksize;
  1240. struct dx_hash_info hinfo;
  1241. ext4_lblk_t block;
  1242. struct fake_dirent *fde;
  1243. blocksize = dir->i_sb->s_blocksize;
  1244. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1245. retval = ext4_journal_get_write_access(handle, bh);
  1246. if (retval) {
  1247. ext4_std_error(dir->i_sb, retval);
  1248. brelse(bh);
  1249. return retval;
  1250. }
  1251. root = (struct dx_root *) bh->b_data;
  1252. /* The 0th block becomes the root, move the dirents out */
  1253. fde = &root->dotdot;
  1254. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1255. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1256. if ((char *) de >= (((char *) root) + blocksize)) {
  1257. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1258. brelse(bh);
  1259. return -EIO;
  1260. }
  1261. len = ((char *) root) + blocksize - (char *) de;
  1262. /* Allocate new block for the 0th block's dirents */
  1263. bh2 = ext4_append(handle, dir, &block, &retval);
  1264. if (!(bh2)) {
  1265. brelse(bh);
  1266. return retval;
  1267. }
  1268. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1269. data1 = bh2->b_data;
  1270. memcpy (data1, de, len);
  1271. de = (struct ext4_dir_entry_2 *) data1;
  1272. top = data1 + len;
  1273. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1274. de = de2;
  1275. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1276. blocksize);
  1277. /* Initialize the root; the dot dirents already exist */
  1278. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1279. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1280. blocksize);
  1281. memset (&root->info, 0, sizeof(root->info));
  1282. root->info.info_length = sizeof(root->info);
  1283. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1284. entries = root->entries;
  1285. dx_set_block(entries, 1);
  1286. dx_set_count(entries, 1);
  1287. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1288. /* Initialize as for dx_probe */
  1289. hinfo.hash_version = root->info.hash_version;
  1290. if (hinfo.hash_version <= DX_HASH_TEA)
  1291. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1292. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1293. ext4fs_dirhash(name, namelen, &hinfo);
  1294. frame = frames;
  1295. frame->entries = entries;
  1296. frame->at = entries;
  1297. frame->bh = bh;
  1298. bh = bh2;
  1299. ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1300. ext4_handle_dirty_metadata(handle, dir, bh);
  1301. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1302. if (!de) {
  1303. /*
  1304. * Even if the block split failed, we have to properly write
  1305. * out all the changes we did so far. Otherwise we can end up
  1306. * with corrupted filesystem.
  1307. */
  1308. ext4_mark_inode_dirty(handle, dir);
  1309. dx_release(frames);
  1310. return retval;
  1311. }
  1312. dx_release(frames);
  1313. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1314. brelse(bh);
  1315. return retval;
  1316. }
  1317. /*
  1318. * ext4_add_entry()
  1319. *
  1320. * adds a file entry to the specified directory, using the same
  1321. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1322. *
  1323. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1324. * may not sleep between calling this and putting something into
  1325. * the entry, as someone else might have used it while you slept.
  1326. */
  1327. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1328. struct inode *inode)
  1329. {
  1330. struct inode *dir = dentry->d_parent->d_inode;
  1331. struct buffer_head *bh;
  1332. struct ext4_dir_entry_2 *de;
  1333. struct super_block *sb;
  1334. int retval;
  1335. int dx_fallback=0;
  1336. unsigned blocksize;
  1337. ext4_lblk_t block, blocks;
  1338. sb = dir->i_sb;
  1339. blocksize = sb->s_blocksize;
  1340. if (!dentry->d_name.len)
  1341. return -EINVAL;
  1342. if (is_dx(dir)) {
  1343. retval = ext4_dx_add_entry(handle, dentry, inode);
  1344. if (!retval || (retval != ERR_BAD_DX_DIR))
  1345. return retval;
  1346. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1347. dx_fallback++;
  1348. ext4_mark_inode_dirty(handle, dir);
  1349. }
  1350. blocks = dir->i_size >> sb->s_blocksize_bits;
  1351. for (block = 0; block < blocks; block++) {
  1352. bh = ext4_bread(handle, dir, block, 0, &retval);
  1353. if(!bh)
  1354. return retval;
  1355. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1356. if (retval != -ENOSPC) {
  1357. brelse(bh);
  1358. return retval;
  1359. }
  1360. if (blocks == 1 && !dx_fallback &&
  1361. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1362. return make_indexed_dir(handle, dentry, inode, bh);
  1363. brelse(bh);
  1364. }
  1365. bh = ext4_append(handle, dir, &block, &retval);
  1366. if (!bh)
  1367. return retval;
  1368. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1369. de->inode = 0;
  1370. de->rec_len = ext4_rec_len_to_disk(blocksize, blocksize);
  1371. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1372. brelse(bh);
  1373. if (retval == 0)
  1374. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1375. return retval;
  1376. }
  1377. /*
  1378. * Returns 0 for success, or a negative error value
  1379. */
  1380. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1381. struct inode *inode)
  1382. {
  1383. struct dx_frame frames[2], *frame;
  1384. struct dx_entry *entries, *at;
  1385. struct dx_hash_info hinfo;
  1386. struct buffer_head *bh;
  1387. struct inode *dir = dentry->d_parent->d_inode;
  1388. struct super_block *sb = dir->i_sb;
  1389. struct ext4_dir_entry_2 *de;
  1390. int err;
  1391. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1392. if (!frame)
  1393. return err;
  1394. entries = frame->entries;
  1395. at = frame->at;
  1396. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1397. goto cleanup;
  1398. BUFFER_TRACE(bh, "get_write_access");
  1399. err = ext4_journal_get_write_access(handle, bh);
  1400. if (err)
  1401. goto journal_error;
  1402. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1403. if (err != -ENOSPC)
  1404. goto cleanup;
  1405. /* Block full, should compress but for now just split */
  1406. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1407. dx_get_count(entries), dx_get_limit(entries)));
  1408. /* Need to split index? */
  1409. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1410. ext4_lblk_t newblock;
  1411. unsigned icount = dx_get_count(entries);
  1412. int levels = frame - frames;
  1413. struct dx_entry *entries2;
  1414. struct dx_node *node2;
  1415. struct buffer_head *bh2;
  1416. if (levels && (dx_get_count(frames->entries) ==
  1417. dx_get_limit(frames->entries))) {
  1418. ext4_warning(sb, "Directory index full!");
  1419. err = -ENOSPC;
  1420. goto cleanup;
  1421. }
  1422. bh2 = ext4_append (handle, dir, &newblock, &err);
  1423. if (!(bh2))
  1424. goto cleanup;
  1425. node2 = (struct dx_node *)(bh2->b_data);
  1426. entries2 = node2->entries;
  1427. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  1428. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  1429. sb->s_blocksize);
  1430. BUFFER_TRACE(frame->bh, "get_write_access");
  1431. err = ext4_journal_get_write_access(handle, frame->bh);
  1432. if (err)
  1433. goto journal_error;
  1434. if (levels) {
  1435. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1436. unsigned hash2 = dx_get_hash(entries + icount1);
  1437. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1438. icount1, icount2));
  1439. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1440. err = ext4_journal_get_write_access(handle,
  1441. frames[0].bh);
  1442. if (err)
  1443. goto journal_error;
  1444. memcpy((char *) entries2, (char *) (entries + icount1),
  1445. icount2 * sizeof(struct dx_entry));
  1446. dx_set_count(entries, icount1);
  1447. dx_set_count(entries2, icount2);
  1448. dx_set_limit(entries2, dx_node_limit(dir));
  1449. /* Which index block gets the new entry? */
  1450. if (at - entries >= icount1) {
  1451. frame->at = at = at - entries - icount1 + entries2;
  1452. frame->entries = entries = entries2;
  1453. swap(frame->bh, bh2);
  1454. }
  1455. dx_insert_block(frames + 0, hash2, newblock);
  1456. dxtrace(dx_show_index("node", frames[1].entries));
  1457. dxtrace(dx_show_index("node",
  1458. ((struct dx_node *) bh2->b_data)->entries));
  1459. err = ext4_handle_dirty_metadata(handle, inode, bh2);
  1460. if (err)
  1461. goto journal_error;
  1462. brelse (bh2);
  1463. } else {
  1464. dxtrace(printk(KERN_DEBUG
  1465. "Creating second level index...\n"));
  1466. memcpy((char *) entries2, (char *) entries,
  1467. icount * sizeof(struct dx_entry));
  1468. dx_set_limit(entries2, dx_node_limit(dir));
  1469. /* Set up root */
  1470. dx_set_count(entries, 1);
  1471. dx_set_block(entries + 0, newblock);
  1472. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1473. /* Add new access path frame */
  1474. frame = frames + 1;
  1475. frame->at = at = at - entries + entries2;
  1476. frame->entries = entries = entries2;
  1477. frame->bh = bh2;
  1478. err = ext4_journal_get_write_access(handle,
  1479. frame->bh);
  1480. if (err)
  1481. goto journal_error;
  1482. }
  1483. err = ext4_handle_dirty_metadata(handle, inode, frames[0].bh);
  1484. if (err) {
  1485. ext4_std_error(inode->i_sb, err);
  1486. goto cleanup;
  1487. }
  1488. }
  1489. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1490. if (!de)
  1491. goto cleanup;
  1492. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1493. goto cleanup;
  1494. journal_error:
  1495. ext4_std_error(dir->i_sb, err);
  1496. cleanup:
  1497. if (bh)
  1498. brelse(bh);
  1499. dx_release(frames);
  1500. return err;
  1501. }
  1502. /*
  1503. * ext4_delete_entry deletes a directory entry by merging it with the
  1504. * previous entry
  1505. */
  1506. static int ext4_delete_entry(handle_t *handle,
  1507. struct inode *dir,
  1508. struct ext4_dir_entry_2 *de_del,
  1509. struct buffer_head *bh)
  1510. {
  1511. struct ext4_dir_entry_2 *de, *pde;
  1512. unsigned int blocksize = dir->i_sb->s_blocksize;
  1513. int i, err;
  1514. i = 0;
  1515. pde = NULL;
  1516. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1517. while (i < bh->b_size) {
  1518. if (ext4_check_dir_entry(dir, NULL, de, bh, i))
  1519. return -EIO;
  1520. if (de == de_del) {
  1521. BUFFER_TRACE(bh, "get_write_access");
  1522. err = ext4_journal_get_write_access(handle, bh);
  1523. if (unlikely(err)) {
  1524. ext4_std_error(dir->i_sb, err);
  1525. return err;
  1526. }
  1527. if (pde)
  1528. pde->rec_len = ext4_rec_len_to_disk(
  1529. ext4_rec_len_from_disk(pde->rec_len,
  1530. blocksize) +
  1531. ext4_rec_len_from_disk(de->rec_len,
  1532. blocksize),
  1533. blocksize);
  1534. else
  1535. de->inode = 0;
  1536. dir->i_version++;
  1537. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1538. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1539. if (unlikely(err)) {
  1540. ext4_std_error(dir->i_sb, err);
  1541. return err;
  1542. }
  1543. return 0;
  1544. }
  1545. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  1546. pde = de;
  1547. de = ext4_next_entry(de, blocksize);
  1548. }
  1549. return -ENOENT;
  1550. }
  1551. /*
  1552. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1553. * since this indicates that nlinks count was previously 1.
  1554. */
  1555. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1556. {
  1557. inc_nlink(inode);
  1558. if (is_dx(inode) && inode->i_nlink > 1) {
  1559. /* limit is 16-bit i_links_count */
  1560. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1561. inode->i_nlink = 1;
  1562. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1563. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1564. }
  1565. }
  1566. }
  1567. /*
  1568. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1569. * directory has >EXT4_LINK_MAX subdirs.
  1570. */
  1571. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1572. {
  1573. drop_nlink(inode);
  1574. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1575. inc_nlink(inode);
  1576. }
  1577. static int ext4_add_nondir(handle_t *handle,
  1578. struct dentry *dentry, struct inode *inode)
  1579. {
  1580. int err = ext4_add_entry(handle, dentry, inode);
  1581. if (!err) {
  1582. ext4_mark_inode_dirty(handle, inode);
  1583. d_instantiate(dentry, inode);
  1584. unlock_new_inode(inode);
  1585. return 0;
  1586. }
  1587. drop_nlink(inode);
  1588. unlock_new_inode(inode);
  1589. iput(inode);
  1590. return err;
  1591. }
  1592. /*
  1593. * By the time this is called, we already have created
  1594. * the directory cache entry for the new file, but it
  1595. * is so far negative - it has no inode.
  1596. *
  1597. * If the create succeeds, we fill in the inode information
  1598. * with d_instantiate().
  1599. */
  1600. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1601. struct nameidata *nd)
  1602. {
  1603. handle_t *handle;
  1604. struct inode *inode;
  1605. int err, retries = 0;
  1606. dquot_initialize(dir);
  1607. retry:
  1608. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1609. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1610. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1611. if (IS_ERR(handle))
  1612. return PTR_ERR(handle);
  1613. if (IS_DIRSYNC(dir))
  1614. ext4_handle_sync(handle);
  1615. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1616. err = PTR_ERR(inode);
  1617. if (!IS_ERR(inode)) {
  1618. inode->i_op = &ext4_file_inode_operations;
  1619. inode->i_fop = &ext4_file_operations;
  1620. ext4_set_aops(inode);
  1621. err = ext4_add_nondir(handle, dentry, inode);
  1622. }
  1623. ext4_journal_stop(handle);
  1624. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1625. goto retry;
  1626. return err;
  1627. }
  1628. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1629. int mode, dev_t rdev)
  1630. {
  1631. handle_t *handle;
  1632. struct inode *inode;
  1633. int err, retries = 0;
  1634. if (!new_valid_dev(rdev))
  1635. return -EINVAL;
  1636. dquot_initialize(dir);
  1637. retry:
  1638. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1639. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1640. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1641. if (IS_ERR(handle))
  1642. return PTR_ERR(handle);
  1643. if (IS_DIRSYNC(dir))
  1644. ext4_handle_sync(handle);
  1645. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1646. err = PTR_ERR(inode);
  1647. if (!IS_ERR(inode)) {
  1648. init_special_inode(inode, inode->i_mode, rdev);
  1649. #ifdef CONFIG_EXT4_FS_XATTR
  1650. inode->i_op = &ext4_special_inode_operations;
  1651. #endif
  1652. err = ext4_add_nondir(handle, dentry, inode);
  1653. }
  1654. ext4_journal_stop(handle);
  1655. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1656. goto retry;
  1657. return err;
  1658. }
  1659. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1660. {
  1661. handle_t *handle;
  1662. struct inode *inode;
  1663. struct buffer_head *dir_block = NULL;
  1664. struct ext4_dir_entry_2 *de;
  1665. unsigned int blocksize = dir->i_sb->s_blocksize;
  1666. int err, retries = 0;
  1667. if (EXT4_DIR_LINK_MAX(dir))
  1668. return -EMLINK;
  1669. dquot_initialize(dir);
  1670. retry:
  1671. handle = ext4_journal_start(dir, EXT4_DATA_TRANS