PageRenderTime 66ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-2.6.21.x/fs/gfs2/dir.c

https://bitbucket.org/altlc/wive-rtnl-ralink-rt305x-routers-firmware-amod
C | 1964 lines | 1390 code | 291 blank | 283 comment | 248 complexity | b6a93a940adf8203d692e551d05dd20a MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, 0BSD, AGPL-1.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/slab.h>
  55. #include <linux/spinlock.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/sort.h>
  58. #include <linux/gfs2_ondisk.h>
  59. #include <linux/crc32.h>
  60. #include <linux/vmalloc.h>
  61. #include <linux/lm_interface.h>
  62. #include "gfs2.h"
  63. #include "incore.h"
  64. #include "dir.h"
  65. #include "glock.h"
  66. #include "inode.h"
  67. #include "meta_io.h"
  68. #include "quota.h"
  69. #include "rgrp.h"
  70. #include "trans.h"
  71. #include "bmap.h"
  72. #include "util.h"
  73. #define IS_LEAF 1 /* Hashed (leaf) directory */
  74. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  75. #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
  76. #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
  77. typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
  78. u64 leaf_no, void *data);
  79. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  80. const struct qstr *name, void *opaque);
  81. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
  82. struct buffer_head **bhp)
  83. {
  84. struct buffer_head *bh;
  85. bh = gfs2_meta_new(ip->i_gl, block);
  86. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  87. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  88. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  89. *bhp = bh;
  90. return 0;
  91. }
  92. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
  93. struct buffer_head **bhp)
  94. {
  95. struct buffer_head *bh;
  96. int error;
  97. error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
  98. if (error)
  99. return error;
  100. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  101. brelse(bh);
  102. return -EIO;
  103. }
  104. *bhp = bh;
  105. return 0;
  106. }
  107. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  108. unsigned int offset, unsigned int size)
  109. {
  110. struct buffer_head *dibh;
  111. int error;
  112. error = gfs2_meta_inode_buffer(ip, &dibh);
  113. if (error)
  114. return error;
  115. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  116. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  117. if (ip->i_di.di_size < offset + size)
  118. ip->i_di.di_size = offset + size;
  119. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
  120. gfs2_dinode_out(ip, dibh->b_data);
  121. brelse(dibh);
  122. return size;
  123. }
  124. /**
  125. * gfs2_dir_write_data - Write directory information to the inode
  126. * @ip: The GFS2 inode
  127. * @buf: The buffer containing information to be written
  128. * @offset: The file offset to start writing at
  129. * @size: The amount of data to write
  130. *
  131. * Returns: The number of bytes correctly written or error code
  132. */
  133. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  134. u64 offset, unsigned int size)
  135. {
  136. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  137. struct buffer_head *dibh;
  138. u64 lblock, dblock;
  139. u32 extlen = 0;
  140. unsigned int o;
  141. int copied = 0;
  142. int error = 0;
  143. if (!size)
  144. return 0;
  145. if (gfs2_is_stuffed(ip) &&
  146. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  147. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  148. size);
  149. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  150. return -EINVAL;
  151. if (gfs2_is_stuffed(ip)) {
  152. error = gfs2_unstuff_dinode(ip, NULL);
  153. if (error)
  154. return error;
  155. }
  156. lblock = offset;
  157. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  158. while (copied < size) {
  159. unsigned int amount;
  160. struct buffer_head *bh;
  161. int new = 0;
  162. amount = size - copied;
  163. if (amount > sdp->sd_sb.sb_bsize - o)
  164. amount = sdp->sd_sb.sb_bsize - o;
  165. if (!extlen) {
  166. new = 1;
  167. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  168. &dblock, &extlen);
  169. if (error)
  170. goto fail;
  171. error = -EIO;
  172. if (gfs2_assert_withdraw(sdp, dblock))
  173. goto fail;
  174. }
  175. if (amount == sdp->sd_jbsize || new)
  176. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  177. else
  178. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  179. if (error)
  180. goto fail;
  181. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  182. memcpy(bh->b_data + o, buf, amount);
  183. brelse(bh);
  184. buf += amount;
  185. copied += amount;
  186. lblock++;
  187. dblock++;
  188. extlen--;
  189. o = sizeof(struct gfs2_meta_header);
  190. }
  191. out:
  192. error = gfs2_meta_inode_buffer(ip, &dibh);
  193. if (error)
  194. return error;
  195. if (ip->i_di.di_size < offset + copied)
  196. ip->i_di.di_size = offset + copied;
  197. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
  198. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  199. gfs2_dinode_out(ip, dibh->b_data);
  200. brelse(dibh);
  201. return copied;
  202. fail:
  203. if (copied)
  204. goto out;
  205. return error;
  206. }
  207. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
  208. u64 offset, unsigned int size)
  209. {
  210. struct buffer_head *dibh;
  211. int error;
  212. error = gfs2_meta_inode_buffer(ip, &dibh);
  213. if (!error) {
  214. offset += sizeof(struct gfs2_dinode);
  215. memcpy(buf, dibh->b_data + offset, size);
  216. brelse(dibh);
  217. }
  218. return (error) ? error : size;
  219. }
  220. /**
  221. * gfs2_dir_read_data - Read a data from a directory inode
  222. * @ip: The GFS2 Inode
  223. * @buf: The buffer to place result into
  224. * @offset: File offset to begin jdata_readng from
  225. * @size: Amount of data to transfer
  226. *
  227. * Returns: The amount of data actually copied or the error
  228. */
  229. static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
  230. unsigned int size, unsigned ra)
  231. {
  232. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  233. u64 lblock, dblock;
  234. u32 extlen = 0;
  235. unsigned int o;
  236. int copied = 0;
  237. int error = 0;
  238. if (offset >= ip->i_di.di_size)
  239. return 0;
  240. if (offset + size > ip->i_di.di_size)
  241. size = ip->i_di.di_size - offset;
  242. if (!size)
  243. return 0;
  244. if (gfs2_is_stuffed(ip))
  245. return gfs2_dir_read_stuffed(ip, buf, offset, size);
  246. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  247. return -EINVAL;
  248. lblock = offset;
  249. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  250. while (copied < size) {
  251. unsigned int amount;
  252. struct buffer_head *bh;
  253. int new;
  254. amount = size - copied;
  255. if (amount > sdp->sd_sb.sb_bsize - o)
  256. amount = sdp->sd_sb.sb_bsize - o;
  257. if (!extlen) {
  258. new = 0;
  259. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  260. &dblock, &extlen);
  261. if (error || !dblock)
  262. goto fail;
  263. BUG_ON(extlen < 1);
  264. if (!ra)
  265. extlen = 1;
  266. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  267. } else {
  268. error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
  269. if (error)
  270. goto fail;
  271. }
  272. error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
  273. if (error) {
  274. brelse(bh);
  275. goto fail;
  276. }
  277. dblock++;
  278. extlen--;
  279. memcpy(buf, bh->b_data + o, amount);
  280. brelse(bh);
  281. buf += amount;
  282. copied += amount;
  283. lblock++;
  284. o = sizeof(struct gfs2_meta_header);
  285. }
  286. return copied;
  287. fail:
  288. return (copied) ? copied : error;
  289. }
  290. static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
  291. {
  292. return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
  293. }
  294. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  295. const struct qstr *name, int ret)
  296. {
  297. if (!gfs2_dirent_sentinel(dent) &&
  298. be32_to_cpu(dent->de_hash) == name->hash &&
  299. be16_to_cpu(dent->de_name_len) == name->len &&
  300. memcmp(dent+1, name->name, name->len) == 0)
  301. return ret;
  302. return 0;
  303. }
  304. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  305. const struct qstr *name,
  306. void *opaque)
  307. {
  308. return __gfs2_dirent_find(dent, name, 1);
  309. }
  310. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  311. const struct qstr *name,
  312. void *opaque)
  313. {
  314. return __gfs2_dirent_find(dent, name, 2);
  315. }
  316. /*
  317. * name->name holds ptr to start of block.
  318. * name->len holds size of block.
  319. */
  320. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  321. const struct qstr *name,
  322. void *opaque)
  323. {
  324. const char *start = name->name;
  325. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  326. if (name->len == (end - start))
  327. return 1;
  328. return 0;
  329. }
  330. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  331. const struct qstr *name,
  332. void *opaque)
  333. {
  334. unsigned required = GFS2_DIRENT_SIZE(name->len);
  335. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  336. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  337. if (gfs2_dirent_sentinel(dent))
  338. actual = GFS2_DIRENT_SIZE(0);
  339. if (totlen - actual >= required)
  340. return 1;
  341. return 0;
  342. }
  343. struct dirent_gather {
  344. const struct gfs2_dirent **pdent;
  345. unsigned offset;
  346. };
  347. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  348. const struct qstr *name,
  349. void *opaque)
  350. {
  351. struct dirent_gather *g = opaque;
  352. if (!gfs2_dirent_sentinel(dent)) {
  353. g->pdent[g->offset++] = dent;
  354. }
  355. return 0;
  356. }
  357. /*
  358. * Other possible things to check:
  359. * - Inode located within filesystem size (and on valid block)
  360. * - Valid directory entry type
  361. * Not sure how heavy-weight we want to make this... could also check
  362. * hash is correct for example, but that would take a lot of extra time.
  363. * For now the most important thing is to check that the various sizes
  364. * are correct.
  365. */
  366. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  367. unsigned int size, unsigned int len, int first)
  368. {
  369. const char *msg = "gfs2_dirent too small";
  370. if (unlikely(size < sizeof(struct gfs2_dirent)))
  371. goto error;
  372. msg = "gfs2_dirent misaligned";
  373. if (unlikely(offset & 0x7))
  374. goto error;
  375. msg = "gfs2_dirent points beyond end of block";
  376. if (unlikely(offset + size > len))
  377. goto error;
  378. msg = "zero inode number";
  379. if (unlikely(!first && gfs2_dirent_sentinel(dent)))
  380. goto error;
  381. msg = "name length is greater than space in dirent";
  382. if (!gfs2_dirent_sentinel(dent) &&
  383. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  384. size))
  385. goto error;
  386. return 0;
  387. error:
  388. printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
  389. first ? "first in block" : "not first in block");
  390. return -EIO;
  391. }
  392. static int gfs2_dirent_offset(const void *buf)
  393. {
  394. const struct gfs2_meta_header *h = buf;
  395. int offset;
  396. BUG_ON(buf == NULL);
  397. switch(be32_to_cpu(h->mh_type)) {
  398. case GFS2_METATYPE_LF:
  399. offset = sizeof(struct gfs2_leaf);
  400. break;
  401. case GFS2_METATYPE_DI:
  402. offset = sizeof(struct gfs2_dinode);
  403. break;
  404. default:
  405. goto wrong_type;
  406. }
  407. return offset;
  408. wrong_type:
  409. printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
  410. be32_to_cpu(h->mh_type));
  411. return -1;
  412. }
  413. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
  414. unsigned int len, gfs2_dscan_t scan,
  415. const struct qstr *name,
  416. void *opaque)
  417. {
  418. struct gfs2_dirent *dent, *prev;
  419. unsigned offset;
  420. unsigned size;
  421. int ret = 0;
  422. ret = gfs2_dirent_offset(buf);
  423. if (ret < 0)
  424. goto consist_inode;
  425. offset = ret;
  426. prev = NULL;
  427. dent = buf + offset;
  428. size = be16_to_cpu(dent->de_rec_len);
  429. if (gfs2_check_dirent(dent, offset, size, len, 1))
  430. goto consist_inode;
  431. do {
  432. ret = scan(dent, name, opaque);
  433. if (ret)
  434. break;
  435. offset += size;
  436. if (offset == len)
  437. break;
  438. prev = dent;
  439. dent = buf + offset;
  440. size = be16_to_cpu(dent->de_rec_len);
  441. if (gfs2_check_dirent(dent, offset, size, len, 0))
  442. goto consist_inode;
  443. } while(1);
  444. switch(ret) {
  445. case 0:
  446. return NULL;
  447. case 1:
  448. return dent;
  449. case 2:
  450. return prev ? prev : dent;
  451. default:
  452. BUG_ON(ret > 0);
  453. return ERR_PTR(ret);
  454. }
  455. consist_inode:
  456. gfs2_consist_inode(GFS2_I(inode));
  457. return ERR_PTR(-EIO);
  458. }
  459. /**
  460. * dirent_first - Return the first dirent
  461. * @dip: the directory
  462. * @bh: The buffer
  463. * @dent: Pointer to list of dirents
  464. *
  465. * return first dirent whether bh points to leaf or stuffed dinode
  466. *
  467. * Returns: IS_LEAF, IS_DINODE, or -errno
  468. */
  469. static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
  470. struct gfs2_dirent **dent)
  471. {
  472. struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
  473. if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
  474. if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
  475. return -EIO;
  476. *dent = (struct gfs2_dirent *)(bh->b_data +
  477. sizeof(struct gfs2_leaf));
  478. return IS_LEAF;
  479. } else {
  480. if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
  481. return -EIO;
  482. *dent = (struct gfs2_dirent *)(bh->b_data +
  483. sizeof(struct gfs2_dinode));
  484. return IS_DINODE;
  485. }
  486. }
  487. static int dirent_check_reclen(struct gfs2_inode *dip,
  488. const struct gfs2_dirent *d, const void *end_p)
  489. {
  490. const void *ptr = d;
  491. u16 rec_len = be16_to_cpu(d->de_rec_len);
  492. if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
  493. goto broken;
  494. ptr += rec_len;
  495. if (ptr < end_p)
  496. return rec_len;
  497. if (ptr == end_p)
  498. return -ENOENT;
  499. broken:
  500. gfs2_consist_inode(dip);
  501. return -EIO;
  502. }
  503. /**
  504. * dirent_next - Next dirent
  505. * @dip: the directory
  506. * @bh: The buffer
  507. * @dent: Pointer to list of dirents
  508. *
  509. * Returns: 0 on success, error code otherwise
  510. */
  511. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  512. struct gfs2_dirent **dent)
  513. {
  514. struct gfs2_dirent *cur = *dent, *tmp;
  515. char *bh_end = bh->b_data + bh->b_size;
  516. int ret;
  517. ret = dirent_check_reclen(dip, cur, bh_end);
  518. if (ret < 0)
  519. return ret;
  520. tmp = (void *)cur + ret;
  521. ret = dirent_check_reclen(dip, tmp, bh_end);
  522. if (ret == -EIO)
  523. return ret;
  524. /* Only the first dent could ever have de_inum.no_addr == 0 */
  525. if (gfs2_dirent_sentinel(tmp)) {
  526. gfs2_consist_inode(dip);
  527. return -EIO;
  528. }
  529. *dent = tmp;
  530. return 0;
  531. }
  532. /**
  533. * dirent_del - Delete a dirent
  534. * @dip: The GFS2 inode
  535. * @bh: The buffer
  536. * @prev: The previous dirent
  537. * @cur: The current dirent
  538. *
  539. */
  540. static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
  541. struct gfs2_dirent *prev, struct gfs2_dirent *cur)
  542. {
  543. u16 cur_rec_len, prev_rec_len;
  544. if (gfs2_dirent_sentinel(cur)) {
  545. gfs2_consist_inode(dip);
  546. return;
  547. }
  548. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  549. /* If there is no prev entry, this is the first entry in the block.
  550. The de_rec_len is already as big as it needs to be. Just zero
  551. out the inode number and return. */
  552. if (!prev) {
  553. cur->de_inum.no_addr = 0;
  554. cur->de_inum.no_formal_ino = 0;
  555. return;
  556. }
  557. /* Combine this dentry with the previous one. */
  558. prev_rec_len = be16_to_cpu(prev->de_rec_len);
  559. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  560. if ((char *)prev + prev_rec_len != (char *)cur)
  561. gfs2_consist_inode(dip);
  562. if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
  563. gfs2_consist_inode(dip);
  564. prev_rec_len += cur_rec_len;
  565. prev->de_rec_len = cpu_to_be16(prev_rec_len);
  566. }
  567. /*
  568. * Takes a dent from which to grab space as an argument. Returns the
  569. * newly created dent.
  570. */
  571. static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
  572. struct gfs2_dirent *dent,
  573. const struct qstr *name,
  574. struct buffer_head *bh)
  575. {
  576. struct gfs2_inode *ip = GFS2_I(inode);
  577. struct gfs2_dirent *ndent;
  578. unsigned offset = 0, totlen;
  579. if (!gfs2_dirent_sentinel(dent))
  580. offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  581. totlen = be16_to_cpu(dent->de_rec_len);
  582. BUG_ON(offset + name->len > totlen);
  583. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  584. ndent = (struct gfs2_dirent *)((char *)dent + offset);
  585. dent->de_rec_len = cpu_to_be16(offset);
  586. gfs2_qstr2dirent(name, totlen - offset, ndent);
  587. return ndent;
  588. }
  589. static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
  590. struct buffer_head *bh,
  591. const struct qstr *name)
  592. {
  593. struct gfs2_dirent *dent;
  594. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  595. gfs2_dirent_find_space, name, NULL);
  596. if (!dent || IS_ERR(dent))
  597. return dent;
  598. return gfs2_init_dirent(inode, dent, name, bh);
  599. }
  600. static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
  601. struct buffer_head **bhp)
  602. {
  603. int error;
  604. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
  605. if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
  606. /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
  607. error = -EIO;
  608. }
  609. return error;
  610. }
  611. /**
  612. * get_leaf_nr - Get a leaf number associated with the index
  613. * @dip: The GFS2 inode
  614. * @index:
  615. * @leaf_out:
  616. *
  617. * Returns: 0 on success, error code otherwise
  618. */
  619. static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
  620. u64 *leaf_out)
  621. {
  622. __be64 leaf_no;
  623. int error;
  624. error = gfs2_dir_read_data(dip, (char *)&leaf_no,
  625. index * sizeof(__be64),
  626. sizeof(__be64), 0);
  627. if (error != sizeof(u64))
  628. return (error < 0) ? error : -EIO;
  629. *leaf_out = be64_to_cpu(leaf_no);
  630. return 0;
  631. }
  632. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  633. struct buffer_head **bh_out)
  634. {
  635. u64 leaf_no;
  636. int error;
  637. error = get_leaf_nr(dip, index, &leaf_no);
  638. if (!error)
  639. error = get_leaf(dip, leaf_no, bh_out);
  640. return error;
  641. }
  642. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  643. const struct qstr *name,
  644. gfs2_dscan_t scan,
  645. struct buffer_head **pbh)
  646. {
  647. struct buffer_head *bh;
  648. struct gfs2_dirent *dent;
  649. struct gfs2_inode *ip = GFS2_I(inode);
  650. int error;
  651. if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
  652. struct gfs2_leaf *leaf;
  653. unsigned hsize = 1 << ip->i_di.di_depth;
  654. unsigned index;
  655. u64 ln;
  656. if (hsize * sizeof(u64) != ip->i_di.di_size) {
  657. gfs2_consist_inode(ip);
  658. return ERR_PTR(-EIO);
  659. }
  660. index = name->hash >> (32 - ip->i_di.di_depth);
  661. error = get_first_leaf(ip, index, &bh);
  662. if (error)
  663. return ERR_PTR(error);
  664. do {
  665. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  666. scan, name, NULL);
  667. if (dent)
  668. goto got_dent;
  669. leaf = (struct gfs2_leaf *)bh->b_data;
  670. ln = be64_to_cpu(leaf->lf_next);
  671. brelse(bh);
  672. if (!ln)
  673. break;
  674. error = get_leaf(ip, ln, &bh);
  675. } while(!error);
  676. return error ? ERR_PTR(error) : NULL;
  677. }
  678. error = gfs2_meta_inode_buffer(ip, &bh);
  679. if (error)
  680. return ERR_PTR(error);
  681. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  682. got_dent:
  683. if (unlikely(dent == NULL || IS_ERR(dent))) {
  684. brelse(bh);
  685. bh = NULL;
  686. }
  687. *pbh = bh;
  688. return dent;
  689. }
  690. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  691. {
  692. struct gfs2_inode *ip = GFS2_I(inode);
  693. u64 bn = gfs2_alloc_meta(ip);
  694. struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
  695. struct gfs2_leaf *leaf;
  696. struct gfs2_dirent *dent;
  697. struct qstr name = { .name = "", .len = 0, .hash = 0 };
  698. if (!bh)
  699. return NULL;
  700. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  701. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  702. leaf = (struct gfs2_leaf *)bh->b_data;
  703. leaf->lf_depth = cpu_to_be16(depth);
  704. leaf->lf_entries = 0;
  705. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  706. leaf->lf_next = 0;
  707. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  708. dent = (struct gfs2_dirent *)(leaf+1);
  709. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  710. *pbh = bh;
  711. return leaf;
  712. }
  713. /**
  714. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  715. * @dip: The GFS2 inode
  716. *
  717. * Returns: 0 on success, error code otherwise
  718. */
  719. static int dir_make_exhash(struct inode *inode)
  720. {
  721. struct gfs2_inode *dip = GFS2_I(inode);
  722. struct gfs2_sbd *sdp = GFS2_SB(inode);
  723. struct gfs2_dirent *dent;
  724. struct qstr args;
  725. struct buffer_head *bh, *dibh;
  726. struct gfs2_leaf *leaf;
  727. int y;
  728. u32 x;
  729. __be64 *lp;
  730. u64 bn;
  731. int error;
  732. error = gfs2_meta_inode_buffer(dip, &dibh);
  733. if (error)
  734. return error;
  735. /* Turn over a new leaf */
  736. leaf = new_leaf(inode, &bh, 0);
  737. if (!leaf)
  738. return -ENOSPC;
  739. bn = bh->b_blocknr;
  740. gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
  741. leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
  742. /* Copy dirents */
  743. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  744. sizeof(struct gfs2_dinode));
  745. /* Find last entry */
  746. x = 0;
  747. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  748. sizeof(struct gfs2_leaf);
  749. args.name = bh->b_data;
  750. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  751. gfs2_dirent_last, &args, NULL);
  752. if (!dent) {
  753. brelse(bh);
  754. brelse(dibh);
  755. return -EIO;
  756. }
  757. if (IS_ERR(dent)) {
  758. brelse(bh);
  759. brelse(dibh);
  760. return PTR_ERR(dent);
  761. }
  762. /* Adjust the last dirent's record length
  763. (Remember that dent still points to the last entry.) */
  764. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  765. sizeof(struct gfs2_dinode) -
  766. sizeof(struct gfs2_leaf));
  767. brelse(bh);
  768. /* We're done with the new leaf block, now setup the new
  769. hash table. */
  770. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  771. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  772. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  773. for (x = sdp->sd_hash_ptrs; x--; lp++)
  774. *lp = cpu_to_be64(bn);
  775. dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
  776. dip->i_di.di_blocks++;
  777. gfs2_set_inode_blocks(&dip->i_inode);
  778. dip->i_di.di_flags |= GFS2_DIF_EXHASH;
  779. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  780. dip->i_di.di_depth = y;
  781. gfs2_dinode_out(dip, dibh->b_data);
  782. brelse(dibh);
  783. return 0;
  784. }
  785. /**
  786. * dir_split_leaf - Split a leaf block into two
  787. * @dip: The GFS2 inode
  788. * @index:
  789. * @leaf_no:
  790. *
  791. * Returns: 0 on success, error code on failure
  792. */
  793. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  794. {
  795. struct gfs2_inode *dip = GFS2_I(inode);
  796. struct buffer_head *nbh, *obh, *dibh;
  797. struct gfs2_leaf *nleaf, *oleaf;
  798. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  799. u32 start, len, half_len, divider;
  800. u64 bn, leaf_no;
  801. __be64 *lp;
  802. u32 index;
  803. int x, moved = 0;
  804. int error;
  805. index = name->hash >> (32 - dip->i_di.di_depth);
  806. error = get_leaf_nr(dip, index, &leaf_no);
  807. if (error)
  808. return error;
  809. /* Get the old leaf block */
  810. error = get_leaf(dip, leaf_no, &obh);
  811. if (error)
  812. return error;
  813. oleaf = (struct gfs2_leaf *)obh->b_data;
  814. if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
  815. brelse(obh);
  816. return 1; /* can't split */
  817. }
  818. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  819. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  820. if (!nleaf) {
  821. brelse(obh);
  822. return -ENOSPC;
  823. }
  824. bn = nbh->b_blocknr;
  825. /* Compute the start and len of leaf pointers in the hash table. */
  826. len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
  827. half_len = len >> 1;
  828. if (!half_len) {
  829. printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
  830. gfs2_consist_inode(dip);
  831. error = -EIO;
  832. goto fail_brelse;
  833. }
  834. start = (index & ~(len - 1));
  835. /* Change the pointers.
  836. Don't bother distinguishing stuffed from non-stuffed.
  837. This code is complicated enough already. */
  838. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
  839. /* Change the pointers */
  840. for (x = 0; x < half_len; x++)
  841. lp[x] = cpu_to_be64(bn);
  842. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  843. half_len * sizeof(u64));
  844. if (error != half_len * sizeof(u64)) {
  845. if (error >= 0)
  846. error = -EIO;
  847. goto fail_lpfree;
  848. }
  849. kfree(lp);
  850. /* Compute the divider */
  851. divider = (start + half_len) << (32 - dip->i_di.di_depth);
  852. /* Copy the entries */
  853. dirent_first(dip, obh, &dent);
  854. do {
  855. next = dent;
  856. if (dirent_next(dip, obh, &next))
  857. next = NULL;
  858. if (!gfs2_dirent_sentinel(dent) &&
  859. be32_to_cpu(dent->de_hash) < divider) {
  860. struct qstr str;
  861. str.name = (char*)(dent+1);
  862. str.len = be16_to_cpu(dent->de_name_len);
  863. str.hash = be32_to_cpu(dent->de_hash);
  864. new = gfs2_dirent_alloc(inode, nbh, &str);
  865. if (IS_ERR(new)) {
  866. error = PTR_ERR(new);
  867. break;
  868. }
  869. new->de_inum = dent->de_inum; /* No endian worries */
  870. new->de_type = dent->de_type; /* No endian worries */
  871. nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
  872. dirent_del(dip, obh, prev, dent);
  873. if (!oleaf->lf_entries)
  874. gfs2_consist_inode(dip);
  875. oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
  876. if (!prev)
  877. prev = dent;
  878. moved = 1;
  879. } else {
  880. prev = dent;
  881. }
  882. dent = next;
  883. } while (dent);
  884. oleaf->lf_depth = nleaf->lf_depth;
  885. error = gfs2_meta_inode_buffer(dip, &dibh);
  886. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  887. dip->i_di.di_blocks++;
  888. gfs2_set_inode_blocks(&dip->i_inode);
  889. gfs2_dinode_out(dip, dibh->b_data);
  890. brelse(dibh);
  891. }
  892. brelse(obh);
  893. brelse(nbh);
  894. return error;
  895. fail_lpfree:
  896. kfree(lp);
  897. fail_brelse:
  898. brelse(obh);
  899. brelse(nbh);
  900. return error;
  901. }
  902. /**
  903. * dir_double_exhash - Double size of ExHash table
  904. * @dip: The GFS2 dinode
  905. *
  906. * Returns: 0 on success, error code on failure
  907. */
  908. static int dir_double_exhash(struct gfs2_inode *dip)
  909. {
  910. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  911. struct buffer_head *dibh;
  912. u32 hsize;
  913. u64 *buf;
  914. u64 *from, *to;
  915. u64 block;
  916. int x;
  917. int error = 0;
  918. hsize = 1 << dip->i_di.di_depth;
  919. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  920. gfs2_consist_inode(dip);
  921. return -EIO;
  922. }
  923. /* Allocate both the "from" and "to" buffers in one big chunk */
  924. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
  925. for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
  926. error = gfs2_dir_read_data(dip, (char *)buf,
  927. block * sdp->sd_hash_bsize,
  928. sdp->sd_hash_bsize, 1);
  929. if (error != sdp->sd_hash_bsize) {
  930. if (error >= 0)
  931. error = -EIO;
  932. goto fail;
  933. }
  934. from = buf;
  935. to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
  936. for (x = sdp->sd_hash_ptrs; x--; from++) {
  937. *to++ = *from; /* No endianess worries */
  938. *to++ = *from;
  939. }
  940. error = gfs2_dir_write_data(dip,
  941. (char *)buf + sdp->sd_hash_bsize,
  942. block * sdp->sd_sb.sb_bsize,
  943. sdp->sd_sb.sb_bsize);
  944. if (error != sdp->sd_sb.sb_bsize) {
  945. if (error >= 0)
  946. error = -EIO;
  947. goto fail;
  948. }
  949. }
  950. kfree(buf);
  951. error = gfs2_meta_inode_buffer(dip, &dibh);
  952. if (!gfs2_assert_withdraw(sdp, !error)) {
  953. dip->i_di.di_depth++;
  954. gfs2_dinode_out(dip, dibh->b_data);
  955. brelse(dibh);
  956. }
  957. return error;
  958. fail:
  959. kfree(buf);
  960. return error;
  961. }
  962. /**
  963. * compare_dents - compare directory entries by hash value
  964. * @a: first dent
  965. * @b: second dent
  966. *
  967. * When comparing the hash entries of @a to @b:
  968. * gt: returns 1
  969. * lt: returns -1
  970. * eq: returns 0
  971. */
  972. static int compare_dents(const void *a, const void *b)
  973. {
  974. const struct gfs2_dirent *dent_a, *dent_b;
  975. u32 hash_a, hash_b;
  976. int ret = 0;
  977. dent_a = *(const struct gfs2_dirent **)a;
  978. hash_a = be32_to_cpu(dent_a->de_hash);
  979. dent_b = *(const struct gfs2_dirent **)b;
  980. hash_b = be32_to_cpu(dent_b->de_hash);
  981. if (hash_a > hash_b)
  982. ret = 1;
  983. else if (hash_a < hash_b)
  984. ret = -1;
  985. else {
  986. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  987. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  988. if (len_a > len_b)
  989. ret = 1;
  990. else if (len_a < len_b)
  991. ret = -1;
  992. else
  993. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  994. }
  995. return ret;
  996. }
  997. /**
  998. * do_filldir_main - read out directory entries
  999. * @dip: The GFS2 inode
  1000. * @offset: The offset in the file to read from
  1001. * @opaque: opaque data to pass to filldir
  1002. * @filldir: The function to pass entries to
  1003. * @darr: an array of struct gfs2_dirent pointers to read
  1004. * @entries: the number of entries in darr
  1005. * @copied: pointer to int that's non-zero if a entry has been copied out
  1006. *
  1007. * Jump through some hoops to make sure that if there are hash collsions,
  1008. * they are read out at the beginning of a buffer. We want to minimize
  1009. * the possibility that they will fall into different readdir buffers or
  1010. * that someone will want to seek to that location.
  1011. *
  1012. * Returns: errno, >0 on exception from filldir
  1013. */
  1014. static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
  1015. void *opaque, filldir_t filldir,
  1016. const struct gfs2_dirent **darr, u32 entries,
  1017. int *copied)
  1018. {
  1019. const struct gfs2_dirent *dent, *dent_next;
  1020. u64 off, off_next;
  1021. unsigned int x, y;
  1022. int run = 0;
  1023. int error = 0;
  1024. sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1025. dent_next = darr[0];
  1026. off_next = be32_to_cpu(dent_next->de_hash);
  1027. off_next = gfs2_disk_hash2offset(off_next);
  1028. for (x = 0, y = 1; x < entries; x++, y++) {
  1029. dent = dent_next;
  1030. off = off_next;
  1031. if (y < entries) {
  1032. dent_next = darr[y];
  1033. off_next = be32_to_cpu(dent_next->de_hash);
  1034. off_next = gfs2_disk_hash2offset(off_next);
  1035. if (off < *offset)
  1036. continue;
  1037. *offset = off;
  1038. if (off_next == off) {
  1039. if (*copied && !run)
  1040. return 1;
  1041. run = 1;
  1042. } else
  1043. run = 0;
  1044. } else {
  1045. if (off < *offset)
  1046. continue;
  1047. *offset = off;
  1048. }
  1049. error = filldir(opaque, (const char *)(dent + 1),
  1050. be16_to_cpu(dent->de_name_len),
  1051. off, be64_to_cpu(dent->de_inum.no_addr),
  1052. be16_to_cpu(dent->de_type));
  1053. if (error)
  1054. return 1;
  1055. *copied = 1;
  1056. }
  1057. /* Increment the *offset by one, so the next time we come into the
  1058. do_filldir fxn, we get the next entry instead of the last one in the
  1059. current leaf */
  1060. (*offset)++;
  1061. return 0;
  1062. }
  1063. static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
  1064. filldir_t filldir, int *copied, unsigned *depth,
  1065. u64 leaf_no)
  1066. {
  1067. struct gfs2_inode *ip = GFS2_I(inode);
  1068. struct buffer_head *bh;
  1069. struct gfs2_leaf *lf;
  1070. unsigned entries = 0;
  1071. unsigned leaves = 0;
  1072. const struct gfs2_dirent **darr, *dent;
  1073. struct dirent_gather g;
  1074. struct buffer_head **larr;
  1075. int leaf = 0;
  1076. int error, i;
  1077. u64 lfn = leaf_no;
  1078. do {
  1079. error = get_leaf(ip, lfn, &bh);
  1080. if (error)
  1081. goto out;
  1082. lf = (struct gfs2_leaf *)bh->b_data;
  1083. if (leaves == 0)
  1084. *depth = be16_to_cpu(lf->lf_depth);
  1085. entries += be16_to_cpu(lf->lf_entries);
  1086. leaves++;
  1087. lfn = be64_to_cpu(lf->lf_next);
  1088. brelse(bh);
  1089. } while(lfn);
  1090. if (!entries)
  1091. return 0;
  1092. error = -ENOMEM;
  1093. larr = vmalloc((leaves + entries) * sizeof(void *));
  1094. if (!larr)
  1095. goto out;
  1096. darr = (const struct gfs2_dirent **)(larr + leaves);
  1097. g.pdent = darr;
  1098. g.offset = 0;
  1099. lfn = leaf_no;
  1100. do {
  1101. error = get_leaf(ip, lfn, &bh);
  1102. if (error)
  1103. goto out_kfree;
  1104. lf = (struct gfs2_leaf *)bh->b_data;
  1105. lfn = be64_to_cpu(lf->lf_next);
  1106. if (lf->lf_entries) {
  1107. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1108. gfs2_dirent_gather, NULL, &g);
  1109. error = PTR_ERR(dent);
  1110. if (IS_ERR(dent)) {
  1111. goto out_kfree;
  1112. }
  1113. error = 0;
  1114. larr[leaf++] = bh;
  1115. } else {
  1116. brelse(bh);
  1117. }
  1118. } while(lfn);
  1119. error = do_filldir_main(ip, offset, opaque, filldir, darr,
  1120. entries, copied);
  1121. out_kfree:
  1122. for(i = 0; i < leaf; i++)
  1123. brelse(larr[i]);
  1124. vfree(larr);
  1125. out:
  1126. return error;
  1127. }
  1128. /**
  1129. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1130. * @dip: dinode pointer
  1131. * @offset: the hash of the last entry read shifted to the right once
  1132. * @opaque: buffer for the filldir function to fill
  1133. * @filldir: points to the filldir function to use
  1134. *
  1135. * Returns: errno
  1136. */
  1137. static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
  1138. filldir_t filldir)
  1139. {
  1140. struct gfs2_inode *dip = GFS2_I(inode);
  1141. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1142. u32 hsize, len = 0;
  1143. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1144. u32 hash, index;
  1145. __be64 *lp;
  1146. int copied = 0;
  1147. int error = 0;
  1148. unsigned depth = 0;
  1149. hsize = 1 << dip->i_di.di_depth;
  1150. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1151. gfs2_consist_inode(dip);
  1152. return -EIO;
  1153. }
  1154. hash = gfs2_dir_offset2hash(*offset);
  1155. index = hash >> (32 - dip->i_di.di_depth);
  1156. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1157. if (!lp)
  1158. return -ENOMEM;
  1159. while (index < hsize) {
  1160. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1161. ht_offset = index - lp_offset;
  1162. if (ht_offset_cur != ht_offset) {
  1163. error = gfs2_dir_read_data(dip, (char *)lp,
  1164. ht_offset * sizeof(__be64),
  1165. sdp->sd_hash_bsize, 1);
  1166. if (error != sdp->sd_hash_bsize) {
  1167. if (error >= 0)
  1168. error = -EIO;
  1169. goto out;
  1170. }
  1171. ht_offset_cur = ht_offset;
  1172. }
  1173. error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
  1174. &copied, &depth,
  1175. be64_to_cpu(lp[lp_offset]));
  1176. if (error)
  1177. break;
  1178. len = 1 << (dip->i_di.di_depth - depth);
  1179. index = (index & ~(len - 1)) + len;
  1180. }
  1181. out:
  1182. kfree(lp);
  1183. if (error > 0)
  1184. error = 0;
  1185. return error;
  1186. }
  1187. int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
  1188. filldir_t filldir)
  1189. {
  1190. struct gfs2_inode *dip = GFS2_I(inode);
  1191. struct dirent_gather g;
  1192. const struct gfs2_dirent **darr, *dent;
  1193. struct buffer_head *dibh;
  1194. int copied = 0;
  1195. int error;
  1196. if (!dip->i_di.di_entries)
  1197. return 0;
  1198. if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
  1199. return dir_e_read(inode, offset, opaque, filldir);
  1200. if (!gfs2_is_stuffed(dip)) {
  1201. gfs2_consist_inode(dip);
  1202. return -EIO;
  1203. }
  1204. error = gfs2_meta_inode_buffer(dip, &dibh);
  1205. if (error)
  1206. return error;
  1207. error = -ENOMEM;
  1208. darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
  1209. GFP_KERNEL);
  1210. if (darr) {
  1211. g.pdent = darr;
  1212. g.offset = 0;
  1213. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1214. gfs2_dirent_gather, NULL, &g);
  1215. if (IS_ERR(dent)) {
  1216. error = PTR_ERR(dent);
  1217. goto out;
  1218. }
  1219. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1220. dip->i_di.di_entries, &copied);
  1221. out:
  1222. kfree(darr);
  1223. }
  1224. if (error > 0)
  1225. error = 0;
  1226. brelse(dibh);
  1227. return error;
  1228. }
  1229. /**
  1230. * gfs2_dir_search - Search a directory
  1231. * @dip: The GFS2 inode
  1232. * @filename:
  1233. * @inode:
  1234. *
  1235. * This routine searches a directory for a file or another directory.
  1236. * Assumes a glock is held on dip.
  1237. *
  1238. * Returns: errno
  1239. */
  1240. int gfs2_dir_search(struct inode *dir, const struct qstr *name,
  1241. struct gfs2_inum_host *inum, unsigned int *type)
  1242. {
  1243. struct buffer_head *bh;
  1244. struct gfs2_dirent *dent;
  1245. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1246. if (dent) {
  1247. if (IS_ERR(dent))
  1248. return PTR_ERR(dent);
  1249. if (inum)
  1250. gfs2_inum_in(inum, (char *)&dent->de_inum);
  1251. if (type)
  1252. *type = be16_to_cpu(dent->de_type);
  1253. brelse(bh);
  1254. return 0;
  1255. }
  1256. return -ENOENT;
  1257. }
  1258. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1259. {
  1260. struct buffer_head *bh, *obh;
  1261. struct gfs2_inode *ip = GFS2_I(inode);
  1262. struct gfs2_leaf *leaf, *oleaf;
  1263. int error;
  1264. u32 index;
  1265. u64 bn;
  1266. index = name->hash >> (32 - ip->i_di.di_depth);
  1267. error = get_first_leaf(ip, index, &obh);
  1268. if (error)
  1269. return error;
  1270. do {
  1271. oleaf = (struct gfs2_leaf *)obh->b_data;
  1272. bn = be64_to_cpu(oleaf->lf_next);
  1273. if (!bn)
  1274. break;
  1275. brelse(obh);
  1276. error = get_leaf(ip, bn, &obh);
  1277. if (error)
  1278. return error;
  1279. } while(1);
  1280. gfs2_trans_add_bh(ip->i_gl, obh, 1);
  1281. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1282. if (!leaf) {
  1283. brelse(obh);
  1284. return -ENOSPC;
  1285. }
  1286. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1287. brelse(bh);
  1288. brelse(obh);
  1289. error = gfs2_meta_inode_buffer(ip, &bh);
  1290. if (error)
  1291. return error;
  1292. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1293. ip->i_di.di_blocks++;
  1294. gfs2_set_inode_blocks(&ip->i_inode);
  1295. gfs2_dinode_out(ip, bh->b_data);
  1296. brelse(bh);
  1297. return 0;
  1298. }
  1299. /**
  1300. * gfs2_dir_add - Add new filename into directory
  1301. * @dip: The GFS2 inode
  1302. * @filename: The new name
  1303. * @inode: The inode number of the entry
  1304. * @type: The type of the entry
  1305. *
  1306. * Returns: 0 on success, error code on failure
  1307. */
  1308. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1309. const struct gfs2_inum_host *inum, unsigned type)
  1310. {
  1311. struct gfs2_inode *ip = GFS2_I(inode);
  1312. struct buffer_head *bh;
  1313. struct gfs2_dirent *dent;
  1314. struct gfs2_leaf *leaf;
  1315. int error;
  1316. while(1) {
  1317. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
  1318. &bh);
  1319. if (dent) {
  1320. if (IS_ERR(dent))
  1321. return PTR_ERR(dent);
  1322. dent = gfs2_init_dirent(inode, dent, name, bh);
  1323. gfs2_inum_out(inum, (char *)&dent->de_inum);
  1324. dent->de_type = cpu_to_be16(type);
  1325. if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1326. leaf = (struct gfs2_leaf *)bh->b_data;
  1327. leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
  1328. }
  1329. brelse(bh);
  1330. error = gfs2_meta_inode_buffer(ip, &bh);
  1331. if (error)
  1332. break;
  1333. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1334. ip->i_di.di_entries++;
  1335. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
  1336. gfs2_dinode_out(ip, bh->b_data);
  1337. brelse(bh);
  1338. error = 0;
  1339. break;
  1340. }
  1341. if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  1342. error = dir_make_exhash(inode);
  1343. if (error)
  1344. break;
  1345. continue;
  1346. }
  1347. error = dir_split_leaf(inode, name);
  1348. if (error == 0)
  1349. continue;
  1350. if (error < 0)
  1351. break;
  1352. if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
  1353. error = dir_double_exhash(ip);
  1354. if (error)
  1355. break;
  1356. error = dir_split_leaf(inode, name);
  1357. if (error < 0)
  1358. break;
  1359. if (error == 0)
  1360. continue;
  1361. }
  1362. error = dir_new_leaf(inode, name);
  1363. if (!error)
  1364. continue;
  1365. error = -ENOSPC;
  1366. break;
  1367. }
  1368. return error;
  1369. }
  1370. /**
  1371. * gfs2_dir_del - Delete a directory entry
  1372. * @dip: The GFS2 inode
  1373. * @filename: The filename
  1374. *
  1375. * Returns: 0 on success, error code on failure
  1376. */
  1377. int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
  1378. {
  1379. struct gfs2_dirent *dent, *prev = NULL;
  1380. struct buffer_head *bh;
  1381. int error;
  1382. /* Returns _either_ the entry (if its first in block) or the
  1383. previous entry otherwise */
  1384. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1385. if (!dent) {
  1386. gfs2_consist_inode(dip);
  1387. return -EIO;
  1388. }
  1389. if (IS_ERR(dent)) {
  1390. gfs2_consist_inode(dip);
  1391. return PTR_ERR(dent);
  1392. }
  1393. /* If not first in block, adjust pointers accordingly */
  1394. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1395. prev = dent;
  1396. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1397. }
  1398. dirent_del(dip, bh, prev, dent);
  1399. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1400. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1401. u16 entries = be16_to_cpu(leaf->lf_entries);
  1402. if (!entries)
  1403. gfs2_consist_inode(dip);
  1404. leaf->lf_entries = cpu_to_be16(--entries);
  1405. }
  1406. brelse(bh);
  1407. error = gfs2_meta_inode_buffer(dip, &bh);
  1408. if (error)
  1409. return error;
  1410. if (!dip->i_di.di_entries)
  1411. gfs2_consist_inode(dip);
  1412. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1413. dip->i_di.di_entries--;
  1414. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME_SEC;
  1415. gfs2_dinode_out(dip, bh->b_data);
  1416. brelse(bh);
  1417. mark_inode_dirty(&dip->i_inode);
  1418. return error;
  1419. }
  1420. /**
  1421. * gfs2_dir_mvino - Change inode number of directory entry
  1422. * @dip: The GFS2 inode
  1423. * @filename:
  1424. * @new_inode:
  1425. *
  1426. * This routine changes the inode number of a directory entry. It's used
  1427. * by rename to change ".." when a directory is moved.
  1428. * Assumes a glock is held on dvp.
  1429. *
  1430. * Returns: errno
  1431. */
  1432. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1433. struct gfs2_inum_host *inum, unsigned int new_type)
  1434. {
  1435. struct buffer_head *bh;
  1436. struct gfs2_dirent *dent;
  1437. int error;
  1438. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1439. if (!dent) {
  1440. gfs2_consist_inode(dip);
  1441. return -EIO;
  1442. }
  1443. if (IS_ERR(dent))
  1444. return PTR_ERR(dent);
  1445. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1446. gfs2_inum_out(inum, (char *)&dent->de_inum);
  1447. dent->de_type = cpu_to_be16(new_type);
  1448. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1449. brelse(bh);
  1450. error = gfs2_meta_inode_buffer(dip, &bh);
  1451. if (error)
  1452. return error;
  1453. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1454. }
  1455. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME_SEC;
  1456. gfs2_dinode_out(dip, bh->b_data);
  1457. brelse(bh);
  1458. return 0;
  1459. }
  1460. /**
  1461. * foreach_leaf - call a function for each leaf in a directory
  1462. * @dip: the directory
  1463. * @lc: the function to call for each each
  1464. * @data: private data to pass to it
  1465. *
  1466. * Returns: errno
  1467. */
  1468. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1469. {
  1470. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1471. struct buffer_head *bh;
  1472. struct gfs2_leaf *leaf;
  1473. u32 hsize, len;
  1474. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1475. u32 index = 0;
  1476. __be64 *lp;
  1477. u64 leaf_no;
  1478. int error = 0;
  1479. hsize = 1 << dip->i_di.di_depth;
  1480. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1481. gfs2_consist_inode(dip);
  1482. return -EIO;
  1483. }
  1484. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1485. if (!lp)
  1486. return -ENOMEM;
  1487. while (index < hsize) {
  1488. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1489. ht_offset = index - lp_offset;
  1490. if (ht_offset_cur != ht_offset) {
  1491. error = gfs2_dir_read_data(dip, (char *)lp,
  1492. ht_offset * sizeof(__be64),
  1493. sdp->sd_hash_bsize, 1);
  1494. if (error != sdp->sd_hash_bsize) {
  1495. if (error >= 0)
  1496. error = -EIO;
  1497. goto out;
  1498. }
  1499. ht_offset_cur = ht_offset;
  1500. }
  1501. leaf_no = be64_to_cpu(lp[lp_offset]);
  1502. if (leaf_no) {
  1503. error = get_leaf(dip, leaf_no, &bh);
  1504. if (error)
  1505. goto out;
  1506. leaf = (struct gfs2_leaf *)bh->b_data;
  1507. len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
  1508. brelse(bh);
  1509. error = lc(dip, index, len, leaf_no, data);
  1510. if (error)
  1511. goto out;
  1512. index = (index & ~(len - 1)) + len;
  1513. } else
  1514. index++;
  1515. }
  1516. if (index != hsize) {
  1517. gfs2_consist_inode(dip);
  1518. error = -EIO;
  1519. }
  1520. out:
  1521. kfree(lp);
  1522. return error;
  1523. }
  1524. /**
  1525. * leaf_dealloc - Deallocate a directory leaf
  1526. * @dip: the directory
  1527. * @index: the hash table offset in the directory
  1528. * @len: the number of pointers to this leaf
  1529. * @leaf_no: the leaf number
  1530. * @data: not used
  1531. *
  1532. * Returns: errno
  1533. */
  1534. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1535. u64 leaf_no, void *data)
  1536. {
  1537. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1538. struct gfs2_leaf *tmp_leaf;
  1539. struct gfs2_rgrp_list rlist;
  1540. struct buffer_head *bh, *dibh;
  1541. u64 blk, nblk;
  1542. unsigned int rg_blocks = 0, l_blocks = 0;
  1543. char *ht;
  1544. unsigned int x, size = len * sizeof(u64);
  1545. int error;
  1546. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1547. ht = kzalloc(size, GFP_KERNEL);
  1548. if (!ht)
  1549. return -ENOMEM;
  1550. gfs2_alloc_get(dip);
  1551. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1552. if (error)
  1553. goto out;
  1554. error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
  1555. if (error)
  1556. goto out_qs;
  1557. /* Count the number of leaves */
  1558. for (blk = leaf_no; blk; blk = nblk) {
  1559. error = get_leaf(dip, blk, &bh);
  1560. if (error)
  1561. goto out_rlist;
  1562. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1563. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1564. brelse(bh);
  1565. gfs2_rlist_add(sdp, &rlist, blk);
  1566. l_blocks++;
  1567. }
  1568. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  1569. for (x = 0; x < rlist.rl_rgrps; x++) {
  1570. struct gfs2_rgrpd *rgd;
  1571. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1572. rg_blocks += rgd->rd_ri.ri_length;
  1573. }
  1574. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1575. if (error)
  1576. goto out_rlist;
  1577. error = gfs2_trans_begin(sdp,
  1578. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1579. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1580. if (error)
  1581. goto out_rg_gunlock;
  1582. for (blk = leaf_no; blk; blk = nblk) {
  1583. error = get_leaf(dip, blk, &bh);
  1584. if (error)
  1585. goto out_end_trans;
  1586. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1587. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1588. brelse(bh);
  1589. gfs2_free_meta(dip, blk, 1);
  1590. if (!dip->i_di.di_blocks)
  1591. gfs2_consist_inode(dip);
  1592. dip->i_di.di_blocks--;
  1593. gfs2_set_inode_blocks(&dip->i_inode);
  1594. }
  1595. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1596. if (error != size) {
  1597. if (error >= 0)
  1598. error = -EIO;
  1599. goto out_end_trans;
  1600. }
  1601. error = gfs2_meta_inode_buffer(dip, &dibh);
  1602. if (error)
  1603. goto out_end_trans;
  1604. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1605. gfs2_dinode_out(dip, dibh->b_data);
  1606. brelse(dibh);
  1607. out_end_trans:
  1608. gfs2_trans_end(sdp);
  1609. out_rg_gunlock:
  1610. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1611. out_rlist:
  1612. gfs2_rlist_free(&rlist);
  1613. gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
  1614. out_qs:
  1615. gfs2_quota_unhold(dip);
  1616. out:
  1617. gfs2_alloc_put(dip);
  1618. kfree(ht);
  1619. return error;
  1620. }
  1621. /**
  1622. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1623. * @dip: the directory
  1624. *
  1625. * Dealloc all on-disk directory leaves to FREEMETA state
  1626. * Change on-disk inode type to "regular file"
  1627. *
  1628. * Returns: errno
  1629. */
  1630. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1631. {
  1632. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1633. struct buffer_head *bh;
  1634. int error;
  1635. /* Dealloc on-disk leaves to FREEMETA state */
  1636. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1637. if (error)
  1638. return error;
  1639. /* Make this a regular file in case we crash.
  1640. (We don't want to free these blocks a second time.) */
  1641. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1642. if (error)
  1643. return error;
  1644. error = gfs2_meta_inode_buffer(dip, &bh);
  1645. if (!error) {
  1646. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1647. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1648. cpu_to_be32(S_IFREG);
  1649. brelse(bh);
  1650. }
  1651. gfs2_trans_end(sdp);
  1652. return error;
  1653. }
  1654. /**
  1655. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1656. * @ip: the file being written to
  1657. * @filname: the filename that's going to be added
  1658. *
  1659. * Returns: 1 if alloc required, 0 if not, -ve on error
  1660. */
  1661. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1662. {
  1663. struct gfs2_dirent *dent;
  1664. struct buffer_head *bh;
  1665. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1666. if (!dent) {
  1667. return 1;
  1668. }
  1669. if (IS_ERR(dent))
  1670. return PTR_ERR(dent);
  1671. brelse(bh);
  1672. return 0;
  1673. }