PageRenderTime 601ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/gfs2/dir.c

https://bitbucket.org/emiliolopez/linux
C | 2197 lines | 1569 code | 313 blank | 315 comment | 284 complexity | 98b4814a62280bdfb83b20b9a9d7b763 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.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_diskflags & 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  55. #include <linux/slab.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/buffer_head.h>
  58. #include <linux/sort.h>
  59. #include <linux/gfs2_ondisk.h>
  60. #include <linux/crc32.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/bio.h>
  63. #include "gfs2.h"
  64. #include "incore.h"
  65. #include "dir.h"
  66. #include "glock.h"
  67. #include "inode.h"
  68. #include "meta_io.h"
  69. #include "quota.h"
  70. #include "rgrp.h"
  71. #include "trans.h"
  72. #include "bmap.h"
  73. #include "util.h"
  74. #define IS_LEAF 1 /* Hashed (leaf) directory */
  75. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  76. #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
  77. #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
  78. #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
  79. #define GFS2_HASH_INDEX_MASK 0xffffc000
  80. #define GFS2_USE_HASH_FLAG 0x2000
  81. struct qstr gfs2_qdot __read_mostly;
  82. struct qstr gfs2_qdotdot __read_mostly;
  83. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  84. const struct qstr *name, void *opaque);
  85. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
  86. struct buffer_head **bhp)
  87. {
  88. struct buffer_head *bh;
  89. bh = gfs2_meta_new(ip->i_gl, block);
  90. gfs2_trans_add_meta(ip->i_gl, bh);
  91. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  92. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  93. *bhp = bh;
  94. return 0;
  95. }
  96. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
  97. struct buffer_head **bhp)
  98. {
  99. struct buffer_head *bh;
  100. int error;
  101. error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh);
  102. if (error)
  103. return error;
  104. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  105. brelse(bh);
  106. return -EIO;
  107. }
  108. *bhp = bh;
  109. return 0;
  110. }
  111. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  112. unsigned int offset, unsigned int size)
  113. {
  114. struct buffer_head *dibh;
  115. int error;
  116. error = gfs2_meta_inode_buffer(ip, &dibh);
  117. if (error)
  118. return error;
  119. gfs2_trans_add_meta(ip->i_gl, dibh);
  120. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  121. if (ip->i_inode.i_size < offset + size)
  122. i_size_write(&ip->i_inode, offset + size);
  123. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  124. gfs2_dinode_out(ip, dibh->b_data);
  125. brelse(dibh);
  126. return size;
  127. }
  128. /**
  129. * gfs2_dir_write_data - Write directory information to the inode
  130. * @ip: The GFS2 inode
  131. * @buf: The buffer containing information to be written
  132. * @offset: The file offset to start writing at
  133. * @size: The amount of data to write
  134. *
  135. * Returns: The number of bytes correctly written or error code
  136. */
  137. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  138. u64 offset, unsigned int size)
  139. {
  140. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  141. struct buffer_head *dibh;
  142. u64 lblock, dblock;
  143. u32 extlen = 0;
  144. unsigned int o;
  145. int copied = 0;
  146. int error = 0;
  147. int new = 0;
  148. if (!size)
  149. return 0;
  150. if (gfs2_is_stuffed(ip) &&
  151. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  152. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  153. size);
  154. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  155. return -EINVAL;
  156. if (gfs2_is_stuffed(ip)) {
  157. error = gfs2_unstuff_dinode(ip, NULL);
  158. if (error)
  159. return error;
  160. }
  161. lblock = offset;
  162. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  163. while (copied < size) {
  164. unsigned int amount;
  165. struct buffer_head *bh;
  166. amount = size - copied;
  167. if (amount > sdp->sd_sb.sb_bsize - o)
  168. amount = sdp->sd_sb.sb_bsize - o;
  169. if (!extlen) {
  170. new = 1;
  171. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  172. &dblock, &extlen);
  173. if (error)
  174. goto fail;
  175. error = -EIO;
  176. if (gfs2_assert_withdraw(sdp, dblock))
  177. goto fail;
  178. }
  179. if (amount == sdp->sd_jbsize || new)
  180. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  181. else
  182. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  183. if (error)
  184. goto fail;
  185. gfs2_trans_add_meta(ip->i_gl, bh);
  186. memcpy(bh->b_data + o, buf, amount);
  187. brelse(bh);
  188. buf += amount;
  189. copied += amount;
  190. lblock++;
  191. dblock++;
  192. extlen--;
  193. o = sizeof(struct gfs2_meta_header);
  194. }
  195. out:
  196. error = gfs2_meta_inode_buffer(ip, &dibh);
  197. if (error)
  198. return error;
  199. if (ip->i_inode.i_size < offset + copied)
  200. i_size_write(&ip->i_inode, offset + copied);
  201. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  202. gfs2_trans_add_meta(ip->i_gl, dibh);
  203. gfs2_dinode_out(ip, dibh->b_data);
  204. brelse(dibh);
  205. return copied;
  206. fail:
  207. if (copied)
  208. goto out;
  209. return error;
  210. }
  211. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
  212. unsigned int size)
  213. {
  214. struct buffer_head *dibh;
  215. int error;
  216. error = gfs2_meta_inode_buffer(ip, &dibh);
  217. if (!error) {
  218. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  219. brelse(dibh);
  220. }
  221. return (error) ? error : size;
  222. }
  223. /**
  224. * gfs2_dir_read_data - Read a data from a directory inode
  225. * @ip: The GFS2 Inode
  226. * @buf: The buffer to place result into
  227. * @size: Amount of data to transfer
  228. *
  229. * Returns: The amount of data actually copied or the error
  230. */
  231. static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
  232. unsigned int size)
  233. {
  234. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  235. u64 lblock, dblock;
  236. u32 extlen = 0;
  237. unsigned int o;
  238. int copied = 0;
  239. int error = 0;
  240. if (gfs2_is_stuffed(ip))
  241. return gfs2_dir_read_stuffed(ip, buf, size);
  242. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  243. return -EINVAL;
  244. lblock = 0;
  245. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  246. while (copied < size) {
  247. unsigned int amount;
  248. struct buffer_head *bh;
  249. int new;
  250. amount = size - copied;
  251. if (amount > sdp->sd_sb.sb_bsize - o)
  252. amount = sdp->sd_sb.sb_bsize - o;
  253. if (!extlen) {
  254. new = 0;
  255. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  256. &dblock, &extlen);
  257. if (error || !dblock)
  258. goto fail;
  259. BUG_ON(extlen < 1);
  260. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  261. } else {
  262. error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &bh);
  263. if (error)
  264. goto fail;
  265. }
  266. error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
  267. if (error) {
  268. brelse(bh);
  269. goto fail;
  270. }
  271. dblock++;
  272. extlen--;
  273. memcpy(buf, bh->b_data + o, amount);
  274. brelse(bh);
  275. buf += (amount/sizeof(__be64));
  276. copied += amount;
  277. lblock++;
  278. o = sizeof(struct gfs2_meta_header);
  279. }
  280. return copied;
  281. fail:
  282. return (copied) ? copied : error;
  283. }
  284. /**
  285. * gfs2_dir_get_hash_table - Get pointer to the dir hash table
  286. * @ip: The inode in question
  287. *
  288. * Returns: The hash table or an error
  289. */
  290. static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
  291. {
  292. struct inode *inode = &ip->i_inode;
  293. int ret;
  294. u32 hsize;
  295. __be64 *hc;
  296. BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
  297. hc = ip->i_hash_cache;
  298. if (hc)
  299. return hc;
  300. hsize = BIT(ip->i_depth);
  301. hsize *= sizeof(__be64);
  302. if (hsize != i_size_read(&ip->i_inode)) {
  303. gfs2_consist_inode(ip);
  304. return ERR_PTR(-EIO);
  305. }
  306. hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
  307. if (hc == NULL)
  308. hc = __vmalloc(hsize, GFP_NOFS, PAGE_KERNEL);
  309. if (hc == NULL)
  310. return ERR_PTR(-ENOMEM);
  311. ret = gfs2_dir_read_data(ip, hc, hsize);
  312. if (ret < 0) {
  313. kvfree(hc);
  314. return ERR_PTR(ret);
  315. }
  316. spin_lock(&inode->i_lock);
  317. if (likely(!ip->i_hash_cache)) {
  318. ip->i_hash_cache = hc;
  319. hc = NULL;
  320. }
  321. spin_unlock(&inode->i_lock);
  322. kvfree(hc);
  323. return ip->i_hash_cache;
  324. }
  325. /**
  326. * gfs2_dir_hash_inval - Invalidate dir hash
  327. * @ip: The directory inode
  328. *
  329. * Must be called with an exclusive glock, or during glock invalidation.
  330. */
  331. void gfs2_dir_hash_inval(struct gfs2_inode *ip)
  332. {
  333. __be64 *hc;
  334. spin_lock(&ip->i_inode.i_lock);
  335. hc = ip->i_hash_cache;
  336. ip->i_hash_cache = NULL;
  337. spin_unlock(&ip->i_inode.i_lock);
  338. kvfree(hc);
  339. }
  340. static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
  341. {
  342. return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
  343. }
  344. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  345. const struct qstr *name, int ret)
  346. {
  347. if (!gfs2_dirent_sentinel(dent) &&
  348. be32_to_cpu(dent->de_hash) == name->hash &&
  349. be16_to_cpu(dent->de_name_len) == name->len &&
  350. memcmp(dent+1, name->name, name->len) == 0)
  351. return ret;
  352. return 0;
  353. }
  354. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  355. const struct qstr *name,
  356. void *opaque)
  357. {
  358. return __gfs2_dirent_find(dent, name, 1);
  359. }
  360. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  361. const struct qstr *name,
  362. void *opaque)
  363. {
  364. return __gfs2_dirent_find(dent, name, 2);
  365. }
  366. /*
  367. * name->name holds ptr to start of block.
  368. * name->len holds size of block.
  369. */
  370. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  371. const struct qstr *name,
  372. void *opaque)
  373. {
  374. const char *start = name->name;
  375. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  376. if (name->len == (end - start))
  377. return 1;
  378. return 0;
  379. }
  380. /* Look for the dirent that contains the offset specified in data. Once we
  381. * find that dirent, there must be space available there for the new dirent */
  382. static int gfs2_dirent_find_offset(const struct gfs2_dirent *dent,
  383. const struct qstr *name,
  384. void *ptr)
  385. {
  386. unsigned required = GFS2_DIRENT_SIZE(name->len);
  387. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  388. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  389. if (ptr < (void *)dent || ptr >= (void *)dent + totlen)
  390. return 0;
  391. if (gfs2_dirent_sentinel(dent))
  392. actual = 0;
  393. if (ptr < (void *)dent + actual)
  394. return -1;
  395. if ((void *)dent + totlen >= ptr + required)
  396. return 1;
  397. return -1;
  398. }
  399. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  400. const struct qstr *name,
  401. void *opaque)
  402. {
  403. unsigned required = GFS2_DIRENT_SIZE(name->len);
  404. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  405. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  406. if (gfs2_dirent_sentinel(dent))
  407. actual = 0;
  408. if (totlen - actual >= required)
  409. return 1;
  410. return 0;
  411. }
  412. struct dirent_gather {
  413. const struct gfs2_dirent **pdent;
  414. unsigned offset;
  415. };
  416. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  417. const struct qstr *name,
  418. void *opaque)
  419. {
  420. struct dirent_gather *g = opaque;
  421. if (!gfs2_dirent_sentinel(dent)) {
  422. g->pdent[g->offset++] = dent;
  423. }
  424. return 0;
  425. }
  426. /*
  427. * Other possible things to check:
  428. * - Inode located within filesystem size (and on valid block)
  429. * - Valid directory entry type
  430. * Not sure how heavy-weight we want to make this... could also check
  431. * hash is correct for example, but that would take a lot of extra time.
  432. * For now the most important thing is to check that the various sizes
  433. * are correct.
  434. */
  435. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  436. unsigned int size, unsigned int len, int first)
  437. {
  438. const char *msg = "gfs2_dirent too small";
  439. if (unlikely(size < sizeof(struct gfs2_dirent)))
  440. goto error;
  441. msg = "gfs2_dirent misaligned";
  442. if (unlikely(offset & 0x7))
  443. goto error;
  444. msg = "gfs2_dirent points beyond end of block";
  445. if (unlikely(offset + size > len))
  446. goto error;
  447. msg = "zero inode number";
  448. if (unlikely(!first && gfs2_dirent_sentinel(dent)))
  449. goto error;
  450. msg = "name length is greater than space in dirent";
  451. if (!gfs2_dirent_sentinel(dent) &&
  452. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  453. size))
  454. goto error;
  455. return 0;
  456. error:
  457. pr_warn("%s: %s (%s)\n",
  458. __func__, msg, first ? "first in block" : "not first in block");
  459. return -EIO;
  460. }
  461. static int gfs2_dirent_offset(const void *buf)
  462. {
  463. const struct gfs2_meta_header *h = buf;
  464. int offset;
  465. BUG_ON(buf == NULL);
  466. switch(be32_to_cpu(h->mh_type)) {
  467. case GFS2_METATYPE_LF:
  468. offset = sizeof(struct gfs2_leaf);
  469. break;
  470. case GFS2_METATYPE_DI:
  471. offset = sizeof(struct gfs2_dinode);
  472. break;
  473. default:
  474. goto wrong_type;
  475. }
  476. return offset;
  477. wrong_type:
  478. pr_warn("%s: wrong block type %u\n", __func__, be32_to_cpu(h->mh_type));
  479. return -1;
  480. }
  481. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
  482. unsigned int len, gfs2_dscan_t scan,
  483. const struct qstr *name,
  484. void *opaque)
  485. {
  486. struct gfs2_dirent *dent, *prev;
  487. unsigned offset;
  488. unsigned size;
  489. int ret = 0;
  490. ret = gfs2_dirent_offset(buf);
  491. if (ret < 0)
  492. goto consist_inode;
  493. offset = ret;
  494. prev = NULL;
  495. dent = buf + offset;
  496. size = be16_to_cpu(dent->de_rec_len);
  497. if (gfs2_check_dirent(dent, offset, size, len, 1))
  498. goto consist_inode;
  499. do {
  500. ret = scan(dent, name, opaque);
  501. if (ret)
  502. break;
  503. offset += size;
  504. if (offset == len)
  505. break;
  506. prev = dent;
  507. dent = buf + offset;
  508. size = be16_to_cpu(dent->de_rec_len);
  509. if (gfs2_check_dirent(dent, offset, size, len, 0))
  510. goto consist_inode;
  511. } while(1);
  512. switch(ret) {
  513. case 0:
  514. return NULL;
  515. case 1:
  516. return dent;
  517. case 2:
  518. return prev ? prev : dent;
  519. default:
  520. BUG_ON(ret > 0);
  521. return ERR_PTR(ret);
  522. }
  523. consist_inode:
  524. gfs2_consist_inode(GFS2_I(inode));
  525. return ERR_PTR(-EIO);
  526. }
  527. static int dirent_check_reclen(struct gfs2_inode *dip,
  528. const struct gfs2_dirent *d, const void *end_p)
  529. {
  530. const void *ptr = d;
  531. u16 rec_len = be16_to_cpu(d->de_rec_len);
  532. if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
  533. goto broken;
  534. ptr += rec_len;
  535. if (ptr < end_p)
  536. return rec_len;
  537. if (ptr == end_p)
  538. return -ENOENT;
  539. broken:
  540. gfs2_consist_inode(dip);
  541. return -EIO;
  542. }
  543. /**
  544. * dirent_next - Next dirent
  545. * @dip: the directory
  546. * @bh: The buffer
  547. * @dent: Pointer to list of dirents
  548. *
  549. * Returns: 0 on success, error code otherwise
  550. */
  551. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  552. struct gfs2_dirent **dent)
  553. {
  554. struct gfs2_dirent *cur = *dent, *tmp;
  555. char *bh_end = bh->b_data + bh->b_size;
  556. int ret;
  557. ret = dirent_check_reclen(dip, cur, bh_end);
  558. if (ret < 0)
  559. return ret;
  560. tmp = (void *)cur + ret;
  561. ret = dirent_check_reclen(dip, tmp, bh_end);
  562. if (ret == -EIO)
  563. return ret;
  564. /* Only the first dent could ever have de_inum.no_addr == 0 */
  565. if (gfs2_dirent_sentinel(tmp)) {
  566. gfs2_consist_inode(dip);
  567. return -EIO;
  568. }
  569. *dent = tmp;
  570. return 0;
  571. }
  572. /**
  573. * dirent_del - Delete a dirent
  574. * @dip: The GFS2 inode
  575. * @bh: The buffer
  576. * @prev: The previous dirent
  577. * @cur: The current dirent
  578. *
  579. */
  580. static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
  581. struct gfs2_dirent *prev, struct gfs2_dirent *cur)
  582. {
  583. u16 cur_rec_len, prev_rec_len;
  584. if (gfs2_dirent_sentinel(cur)) {
  585. gfs2_consist_inode(dip);
  586. return;
  587. }
  588. gfs2_trans_add_meta(dip->i_gl, bh);
  589. /* If there is no prev entry, this is the first entry in the block.
  590. The de_rec_len is already as big as it needs to be. Just zero
  591. out the inode number and return. */
  592. if (!prev) {
  593. cur->de_inum.no_addr = 0;
  594. cur->de_inum.no_formal_ino = 0;
  595. return;
  596. }
  597. /* Combine this dentry with the previous one. */
  598. prev_rec_len = be16_to_cpu(prev->de_rec_len);
  599. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  600. if ((char *)prev + prev_rec_len != (char *)cur)
  601. gfs2_consist_inode(dip);
  602. if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
  603. gfs2_consist_inode(dip);
  604. prev_rec_len += cur_rec_len;
  605. prev->de_rec_len = cpu_to_be16(prev_rec_len);
  606. }
  607. static struct gfs2_dirent *do_init_dirent(struct inode *inode,
  608. struct gfs2_dirent *dent,
  609. const struct qstr *name,
  610. struct buffer_head *bh,
  611. unsigned offset)
  612. {
  613. struct gfs2_inode *ip = GFS2_I(inode);
  614. struct gfs2_dirent *ndent;
  615. unsigned totlen;
  616. totlen = be16_to_cpu(dent->de_rec_len);
  617. BUG_ON(offset + name->len > totlen);
  618. gfs2_trans_add_meta(ip->i_gl, bh);
  619. ndent = (struct gfs2_dirent *)((char *)dent + offset);
  620. dent->de_rec_len = cpu_to_be16(offset);
  621. gfs2_qstr2dirent(name, totlen - offset, ndent);
  622. return ndent;
  623. }
  624. /*
  625. * Takes a dent from which to grab space as an argument. Returns the
  626. * newly created dent.
  627. */
  628. static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
  629. struct gfs2_dirent *dent,
  630. const struct qstr *name,
  631. struct buffer_head *bh)
  632. {
  633. unsigned offset = 0;
  634. if (!gfs2_dirent_sentinel(dent))
  635. offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  636. return do_init_dirent(inode, dent, name, bh, offset);
  637. }
  638. static struct gfs2_dirent *gfs2_dirent_split_alloc(struct inode *inode,
  639. struct buffer_head *bh,
  640. const struct qstr *name,
  641. void *ptr)
  642. {
  643. struct gfs2_dirent *dent;
  644. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  645. gfs2_dirent_find_offset, name, ptr);
  646. if (!dent || IS_ERR(dent))
  647. return dent;
  648. return do_init_dirent(inode, dent, name, bh,
  649. (unsigned)(ptr - (void *)dent));
  650. }
  651. static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
  652. struct buffer_head **bhp)
  653. {
  654. int error;
  655. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp);
  656. if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
  657. /* pr_info("block num=%llu\n", leaf_no); */
  658. error = -EIO;
  659. }
  660. return error;
  661. }
  662. /**
  663. * get_leaf_nr - Get a leaf number associated with the index
  664. * @dip: The GFS2 inode
  665. * @index:
  666. * @leaf_out:
  667. *
  668. * Returns: 0 on success, error code otherwise
  669. */
  670. static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
  671. u64 *leaf_out)
  672. {
  673. __be64 *hash;
  674. int error;
  675. hash = gfs2_dir_get_hash_table(dip);
  676. error = PTR_ERR_OR_ZERO(hash);
  677. if (!error)
  678. *leaf_out = be64_to_cpu(*(hash + index));
  679. return error;
  680. }
  681. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  682. struct buffer_head **bh_out)
  683. {
  684. u64 leaf_no;
  685. int error;
  686. error = get_leaf_nr(dip, index, &leaf_no);
  687. if (!error)
  688. error = get_leaf(dip, leaf_no, bh_out);
  689. return error;
  690. }
  691. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  692. const struct qstr *name,
  693. gfs2_dscan_t scan,
  694. struct buffer_head **pbh)
  695. {
  696. struct buffer_head *bh;
  697. struct gfs2_dirent *dent;
  698. struct gfs2_inode *ip = GFS2_I(inode);
  699. int error;
  700. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  701. struct gfs2_leaf *leaf;
  702. unsigned int hsize = BIT(ip->i_depth);
  703. unsigned int index;
  704. u64 ln;
  705. if (hsize * sizeof(u64) != i_size_read(inode)) {
  706. gfs2_consist_inode(ip);
  707. return ERR_PTR(-EIO);
  708. }
  709. index = name->hash >> (32 - ip->i_depth);
  710. error = get_first_leaf(ip, index, &bh);
  711. if (error)
  712. return ERR_PTR(error);
  713. do {
  714. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  715. scan, name, NULL);
  716. if (dent)
  717. goto got_dent;
  718. leaf = (struct gfs2_leaf *)bh->b_data;
  719. ln = be64_to_cpu(leaf->lf_next);
  720. brelse(bh);
  721. if (!ln)
  722. break;
  723. error = get_leaf(ip, ln, &bh);
  724. } while(!error);
  725. return error ? ERR_PTR(error) : NULL;
  726. }
  727. error = gfs2_meta_inode_buffer(ip, &bh);
  728. if (error)
  729. return ERR_PTR(error);
  730. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  731. got_dent:
  732. if (unlikely(dent == NULL || IS_ERR(dent))) {
  733. brelse(bh);
  734. bh = NULL;
  735. }
  736. *pbh = bh;
  737. return dent;
  738. }
  739. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  740. {
  741. struct gfs2_inode *ip = GFS2_I(inode);
  742. unsigned int n = 1;
  743. u64 bn;
  744. int error;
  745. struct buffer_head *bh;
  746. struct gfs2_leaf *leaf;
  747. struct gfs2_dirent *dent;
  748. struct timespec tv = current_time(inode);
  749. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  750. if (error)
  751. return NULL;
  752. bh = gfs2_meta_new(ip->i_gl, bn);
  753. if (!bh)
  754. return NULL;
  755. gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
  756. gfs2_trans_add_meta(ip->i_gl, bh);
  757. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  758. leaf = (struct gfs2_leaf *)bh->b_data;
  759. leaf->lf_depth = cpu_to_be16(depth);
  760. leaf->lf_entries = 0;
  761. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  762. leaf->lf_next = 0;
  763. leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
  764. leaf->lf_dist = cpu_to_be32(1);
  765. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  766. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  767. memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
  768. dent = (struct gfs2_dirent *)(leaf+1);
  769. gfs2_qstr2dirent(&empty_name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  770. *pbh = bh;
  771. return leaf;
  772. }
  773. /**
  774. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  775. * @dip: The GFS2 inode
  776. *
  777. * Returns: 0 on success, error code otherwise
  778. */
  779. static int dir_make_exhash(struct inode *inode)
  780. {
  781. struct gfs2_inode *dip = GFS2_I(inode);
  782. struct gfs2_sbd *sdp = GFS2_SB(inode);
  783. struct gfs2_dirent *dent;
  784. struct qstr args;
  785. struct buffer_head *bh, *dibh;
  786. struct gfs2_leaf *leaf;
  787. int y;
  788. u32 x;
  789. __be64 *lp;
  790. u64 bn;
  791. int error;
  792. error = gfs2_meta_inode_buffer(dip, &dibh);
  793. if (error)
  794. return error;
  795. /* Turn over a new leaf */
  796. leaf = new_leaf(inode, &bh, 0);
  797. if (!leaf)
  798. return -ENOSPC;
  799. bn = bh->b_blocknr;
  800. gfs2_assert(sdp, dip->i_entries < BIT(16));
  801. leaf->lf_entries = cpu_to_be16(dip->i_entries);
  802. /* Copy dirents */
  803. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  804. sizeof(struct gfs2_dinode));
  805. /* Find last entry */
  806. x = 0;
  807. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  808. sizeof(struct gfs2_leaf);
  809. args.name = bh->b_data;
  810. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  811. gfs2_dirent_last, &args, NULL);
  812. if (!dent) {
  813. brelse(bh);
  814. brelse(dibh);
  815. return -EIO;
  816. }
  817. if (IS_ERR(dent)) {
  818. brelse(bh);
  819. brelse(dibh);
  820. return PTR_ERR(dent);
  821. }
  822. /* Adjust the last dirent's record length
  823. (Remember that dent still points to the last entry.) */
  824. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  825. sizeof(struct gfs2_dinode) -
  826. sizeof(struct gfs2_leaf));
  827. brelse(bh);
  828. /* We're done with the new leaf block, now setup the new
  829. hash table. */
  830. gfs2_trans_add_meta(dip->i_gl, dibh);
  831. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  832. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  833. for (x = sdp->sd_hash_ptrs; x--; lp++)
  834. *lp = cpu_to_be64(bn);
  835. i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
  836. gfs2_add_inode_blocks(&dip->i_inode, 1);
  837. dip->i_diskflags |= GFS2_DIF_EXHASH;
  838. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  839. dip->i_depth = y;
  840. gfs2_dinode_out(dip, dibh->b_data);
  841. brelse(dibh);
  842. return 0;
  843. }
  844. /**
  845. * dir_split_leaf - Split a leaf block into two
  846. * @dip: The GFS2 inode
  847. * @index:
  848. * @leaf_no:
  849. *
  850. * Returns: 0 on success, error code on failure
  851. */
  852. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  853. {
  854. struct gfs2_inode *dip = GFS2_I(inode);
  855. struct buffer_head *nbh, *obh, *dibh;
  856. struct gfs2_leaf *nleaf, *oleaf;
  857. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  858. u32 start, len, half_len, divider;
  859. u64 bn, leaf_no;
  860. __be64 *lp;
  861. u32 index;
  862. int x, moved = 0;
  863. int error;
  864. index = name->hash >> (32 - dip->i_depth);
  865. error = get_leaf_nr(dip, index, &leaf_no);
  866. if (error)
  867. return error;
  868. /* Get the old leaf block */
  869. error = get_leaf(dip, leaf_no, &obh);
  870. if (error)
  871. return error;
  872. oleaf = (struct gfs2_leaf *)obh->b_data;
  873. if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
  874. brelse(obh);
  875. return 1; /* can't split */
  876. }
  877. gfs2_trans_add_meta(dip->i_gl, obh);
  878. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  879. if (!nleaf) {
  880. brelse(obh);
  881. return -ENOSPC;
  882. }
  883. bn = nbh->b_blocknr;
  884. /* Compute the start and len of leaf pointers in the hash table. */
  885. len = BIT(dip->i_depth - be16_to_cpu(oleaf->lf_depth));
  886. half_len = len >> 1;
  887. if (!half_len) {
  888. pr_warn("i_depth %u lf_depth %u index %u\n",
  889. dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
  890. gfs2_consist_inode(dip);
  891. error = -EIO;
  892. goto fail_brelse;
  893. }
  894. start = (index & ~(len - 1));
  895. /* Change the pointers.
  896. Don't bother distinguishing stuffed from non-stuffed.
  897. This code is complicated enough already. */
  898. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
  899. if (!lp) {
  900. error = -ENOMEM;
  901. goto fail_brelse;
  902. }
  903. /* Change the pointers */
  904. for (x = 0; x < half_len; x++)
  905. lp[x] = cpu_to_be64(bn);
  906. gfs2_dir_hash_inval(dip);
  907. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  908. half_len * sizeof(u64));
  909. if (error != half_len * sizeof(u64)) {
  910. if (error >= 0)
  911. error = -EIO;
  912. goto fail_lpfree;
  913. }
  914. kfree(lp);
  915. /* Compute the divider */
  916. divider = (start + half_len) << (32 - dip->i_depth);
  917. /* Copy the entries */
  918. dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
  919. do {
  920. next = dent;
  921. if (dirent_next(dip, obh, &next))
  922. next = NULL;
  923. if (!gfs2_dirent_sentinel(dent) &&
  924. be32_to_cpu(dent->de_hash) < divider) {
  925. struct qstr str;
  926. void *ptr = ((char *)dent - obh->b_data) + nbh->b_data;
  927. str.name = (char*)(dent+1);
  928. str.len = be16_to_cpu(dent->de_name_len);
  929. str.hash = be32_to_cpu(dent->de_hash);
  930. new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr);
  931. if (IS_ERR(new)) {
  932. error = PTR_ERR(new);
  933. break;
  934. }
  935. new->de_inum = dent->de_inum; /* No endian worries */
  936. new->de_type = dent->de_type; /* No endian worries */
  937. be16_add_cpu(&nleaf->lf_entries, 1);
  938. dirent_del(dip, obh, prev, dent);
  939. if (!oleaf->lf_entries)
  940. gfs2_consist_inode(dip);
  941. be16_add_cpu(&oleaf->lf_entries, -1);
  942. if (!prev)
  943. prev = dent;
  944. moved = 1;
  945. } else {
  946. prev = dent;
  947. }
  948. dent = next;
  949. } while (dent);
  950. oleaf->lf_depth = nleaf->lf_depth;
  951. error = gfs2_meta_inode_buffer(dip, &dibh);
  952. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  953. gfs2_trans_add_meta(dip->i_gl, dibh);
  954. gfs2_add_inode_blocks(&dip->i_inode, 1);
  955. gfs2_dinode_out(dip, dibh->b_data);
  956. brelse(dibh);
  957. }
  958. brelse(obh);
  959. brelse(nbh);
  960. return error;
  961. fail_lpfree:
  962. kfree(lp);
  963. fail_brelse:
  964. brelse(obh);
  965. brelse(nbh);
  966. return error;
  967. }
  968. /**
  969. * dir_double_exhash - Double size of ExHash table
  970. * @dip: The GFS2 dinode
  971. *
  972. * Returns: 0 on success, error code on failure
  973. */
  974. static int dir_double_exhash(struct gfs2_inode *dip)
  975. {
  976. struct buffer_head *dibh;
  977. u32 hsize;
  978. u32 hsize_bytes;
  979. __be64 *hc;
  980. __be64 *hc2, *h;
  981. int x;
  982. int error = 0;
  983. hsize = BIT(dip->i_depth);
  984. hsize_bytes = hsize * sizeof(__be64);
  985. hc = gfs2_dir_get_hash_table(dip);
  986. if (IS_ERR(hc))
  987. return PTR_ERR(hc);
  988. hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
  989. if (hc2 == NULL)
  990. hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
  991. if (!hc2)
  992. return -ENOMEM;
  993. h = hc2;
  994. error = gfs2_meta_inode_buffer(dip, &dibh);
  995. if (error)
  996. goto out_kfree;
  997. for (x = 0; x < hsize; x++) {
  998. *h++ = *hc;
  999. *h++ = *hc;
  1000. hc++;
  1001. }
  1002. error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
  1003. if (error != (hsize_bytes * 2))
  1004. goto fail;
  1005. gfs2_dir_hash_inval(dip);
  1006. dip->i_hash_cache = hc2;
  1007. dip->i_depth++;
  1008. gfs2_dinode_out(dip, dibh->b_data);
  1009. brelse(dibh);
  1010. return 0;
  1011. fail:
  1012. /* Replace original hash table & size */
  1013. gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
  1014. i_size_write(&dip->i_inode, hsize_bytes);
  1015. gfs2_dinode_out(dip, dibh->b_data);
  1016. brelse(dibh);
  1017. out_kfree:
  1018. kvfree(hc2);
  1019. return error;
  1020. }
  1021. /**
  1022. * compare_dents - compare directory entries by hash value
  1023. * @a: first dent
  1024. * @b: second dent
  1025. *
  1026. * When comparing the hash entries of @a to @b:
  1027. * gt: returns 1
  1028. * lt: returns -1
  1029. * eq: returns 0
  1030. */
  1031. static int compare_dents(const void *a, const void *b)
  1032. {
  1033. const struct gfs2_dirent *dent_a, *dent_b;
  1034. u32 hash_a, hash_b;
  1035. int ret = 0;
  1036. dent_a = *(const struct gfs2_dirent **)a;
  1037. hash_a = dent_a->de_cookie;
  1038. dent_b = *(const struct gfs2_dirent **)b;
  1039. hash_b = dent_b->de_cookie;
  1040. if (hash_a > hash_b)
  1041. ret = 1;
  1042. else if (hash_a < hash_b)
  1043. ret = -1;
  1044. else {
  1045. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  1046. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  1047. if (len_a > len_b)
  1048. ret = 1;
  1049. else if (len_a < len_b)
  1050. ret = -1;
  1051. else
  1052. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  1053. }
  1054. return ret;
  1055. }
  1056. /**
  1057. * do_filldir_main - read out directory entries
  1058. * @dip: The GFS2 inode
  1059. * @ctx: what to feed the entries to
  1060. * @darr: an array of struct gfs2_dirent pointers to read
  1061. * @entries: the number of entries in darr
  1062. * @copied: pointer to int that's non-zero if a entry has been copied out
  1063. *
  1064. * Jump through some hoops to make sure that if there are hash collsions,
  1065. * they are read out at the beginning of a buffer. We want to minimize
  1066. * the possibility that they will fall into different readdir buffers or
  1067. * that someone will want to seek to that location.
  1068. *
  1069. * Returns: errno, >0 if the actor tells you to stop
  1070. */
  1071. static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
  1072. struct gfs2_dirent **darr, u32 entries,
  1073. u32 sort_start, int *copied)
  1074. {
  1075. const struct gfs2_dirent *dent, *dent_next;
  1076. u64 off, off_next;
  1077. unsigned int x, y;
  1078. int run = 0;
  1079. if (sort_start < entries)
  1080. sort(&darr[sort_start], entries - sort_start,
  1081. sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1082. dent_next = darr[0];
  1083. off_next = dent_next->de_cookie;
  1084. for (x = 0, y = 1; x < entries; x++, y++) {
  1085. dent = dent_next;
  1086. off = off_next;
  1087. if (y < entries) {
  1088. dent_next = darr[y];
  1089. off_next = dent_next->de_cookie;
  1090. if (off < ctx->pos)
  1091. continue;
  1092. ctx->pos = off;
  1093. if (off_next == off) {
  1094. if (*copied && !run)
  1095. return 1;
  1096. run = 1;
  1097. } else
  1098. run = 0;
  1099. } else {
  1100. if (off < ctx->pos)
  1101. continue;
  1102. ctx->pos = off;
  1103. }
  1104. if (!dir_emit(ctx, (const char *)(dent + 1),
  1105. be16_to_cpu(dent->de_name_len),
  1106. be64_to_cpu(dent->de_inum.no_addr),
  1107. be16_to_cpu(dent->de_type)))
  1108. return 1;
  1109. *copied = 1;
  1110. }
  1111. /* Increment the ctx->pos by one, so the next time we come into the
  1112. do_filldir fxn, we get the next entry instead of the last one in the
  1113. current leaf */
  1114. ctx->pos++;
  1115. return 0;
  1116. }
  1117. static void *gfs2_alloc_sort_buffer(unsigned size)
  1118. {
  1119. void *ptr = NULL;
  1120. if (size < KMALLOC_MAX_SIZE)
  1121. ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
  1122. if (!ptr)
  1123. ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
  1124. return ptr;
  1125. }
  1126. static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
  1127. unsigned leaf_nr, struct gfs2_dirent **darr,
  1128. unsigned entries)
  1129. {
  1130. int sort_id = -1;
  1131. int i;
  1132. for (i = 0; i < entries; i++) {
  1133. unsigned offset;
  1134. darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash);
  1135. darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie);
  1136. if (!sdp->sd_args.ar_loccookie)
  1137. continue;
  1138. offset = (char *)(darr[i]) -
  1139. (bh->b_data + gfs2_dirent_offset(bh->b_data));
  1140. offset /= GFS2_MIN_DIRENT_SIZE;
  1141. offset += leaf_nr * sdp->sd_max_dents_per_leaf;
  1142. if (offset >= GFS2_USE_HASH_FLAG ||
  1143. leaf_nr >= GFS2_USE_HASH_FLAG) {
  1144. darr[i]->de_cookie |= GFS2_USE_HASH_FLAG;
  1145. if (sort_id < 0)
  1146. sort_id = i;
  1147. continue;
  1148. }
  1149. darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK;
  1150. darr[i]->de_cookie |= offset;
  1151. }
  1152. return sort_id;
  1153. }
  1154. static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
  1155. int *copied, unsigned *depth,
  1156. u64 leaf_no)
  1157. {
  1158. struct gfs2_inode *ip = GFS2_I(inode);
  1159. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1160. struct buffer_head *bh;
  1161. struct gfs2_leaf *lf;
  1162. unsigned entries = 0, entries2 = 0;
  1163. unsigned leaves = 0, leaf = 0, offset, sort_offset;
  1164. struct gfs2_dirent **darr, *dent;
  1165. struct dirent_gather g;
  1166. struct buffer_head **larr;
  1167. int error, i, need_sort = 0, sort_id;
  1168. u64 lfn = leaf_no;
  1169. do {
  1170. error = get_leaf(ip, lfn, &bh);
  1171. if (error)
  1172. goto out;
  1173. lf = (struct gfs2_leaf *)bh->b_data;
  1174. if (leaves == 0)
  1175. *depth = be16_to_cpu(lf->lf_depth);
  1176. entries += be16_to_cpu(lf->lf_entries);
  1177. leaves++;
  1178. lfn = be64_to_cpu(lf->lf_next);
  1179. brelse(bh);
  1180. } while(lfn);
  1181. if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) {
  1182. need_sort = 1;
  1183. sort_offset = 0;
  1184. }
  1185. if (!entries)
  1186. return 0;
  1187. error = -ENOMEM;
  1188. /*
  1189. * The extra 99 entries are not normally used, but are a buffer
  1190. * zone in case the number of entries in the leaf is corrupt.
  1191. * 99 is the maximum number of entries that can fit in a single
  1192. * leaf block.
  1193. */
  1194. larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
  1195. if (!larr)
  1196. goto out;
  1197. darr = (struct gfs2_dirent **)(larr + leaves);
  1198. g.pdent = (const struct gfs2_dirent **)darr;
  1199. g.offset = 0;
  1200. lfn = leaf_no;
  1201. do {
  1202. error = get_leaf(ip, lfn, &bh);
  1203. if (error)
  1204. goto out_free;
  1205. lf = (struct gfs2_leaf *)bh->b_data;
  1206. lfn = be64_to_cpu(lf->lf_next);
  1207. if (lf->lf_entries) {
  1208. offset = g.offset;
  1209. entries2 += be16_to_cpu(lf->lf_entries);
  1210. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1211. gfs2_dirent_gather, NULL, &g);
  1212. error = PTR_ERR(dent);
  1213. if (IS_ERR(dent))
  1214. goto out_free;
  1215. if (entries2 != g.offset) {
  1216. fs_warn(sdp, "Number of entries corrupt in dir "
  1217. "leaf %llu, entries2 (%u) != "
  1218. "g.offset (%u)\n",
  1219. (unsigned long long)bh->b_blocknr,
  1220. entries2, g.offset);
  1221. gfs2_consist_inode(ip);
  1222. error = -EIO;
  1223. goto out_free;
  1224. }
  1225. error = 0;
  1226. sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset],
  1227. be16_to_cpu(lf->lf_entries));
  1228. if (!need_sort && sort_id >= 0) {
  1229. need_sort = 1;
  1230. sort_offset = offset + sort_id;
  1231. }
  1232. larr[leaf++] = bh;
  1233. } else {
  1234. larr[leaf++] = NULL;
  1235. brelse(bh);
  1236. }
  1237. } while(lfn);
  1238. BUG_ON(entries2 != entries);
  1239. error = do_filldir_main(ip, ctx, darr, entries, need_sort ?
  1240. sort_offset : entries, copied);
  1241. out_free:
  1242. for(i = 0; i < leaf; i++)
  1243. if (larr[i])
  1244. brelse(larr[i]);
  1245. kvfree(larr);
  1246. out:
  1247. return error;
  1248. }
  1249. /**
  1250. * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
  1251. *
  1252. * Note: we can't calculate each index like dir_e_read can because we don't
  1253. * have the leaf, and therefore we don't have the depth, and therefore we
  1254. * don't have the length. So we have to just read enough ahead to make up
  1255. * for the loss of information.
  1256. */
  1257. static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
  1258. struct file_ra_state *f_ra)
  1259. {
  1260. struct gfs2_inode *ip = GFS2_I(inode);
  1261. struct gfs2_glock *gl = ip->i_gl;
  1262. struct buffer_head *bh;
  1263. u64 blocknr = 0, last;
  1264. unsigned count;
  1265. /* First check if we've already read-ahead for the whole range. */
  1266. if (index + MAX_RA_BLOCKS < f_ra->start)
  1267. return;
  1268. f_ra->start = max((pgoff_t)index, f_ra->start);
  1269. for (count = 0; count < MAX_RA_BLOCKS; count++) {
  1270. if (f_ra->start >= hsize) /* if exceeded the hash table */
  1271. break;
  1272. last = blocknr;
  1273. blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
  1274. f_ra->start++;
  1275. if (blocknr == last)
  1276. continue;
  1277. bh = gfs2_getbuf(gl, blocknr, 1);
  1278. if (trylock_buffer(bh)) {
  1279. if (buffer_uptodate(bh)) {
  1280. unlock_buffer(bh);
  1281. brelse(bh);
  1282. continue;
  1283. }
  1284. bh->b_end_io = end_buffer_read_sync;
  1285. submit_bh(REQ_OP_READ,
  1286. REQ_RAHEAD | REQ_META | REQ_PRIO,
  1287. bh);
  1288. continue;
  1289. }
  1290. brelse(bh);
  1291. }
  1292. }
  1293. /**
  1294. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1295. * @dip: dinode pointer
  1296. * @ctx: actor to feed the entries to
  1297. *
  1298. * Returns: errno
  1299. */
  1300. static int dir_e_read(struct inode *inode, struct dir_context *ctx,
  1301. struct file_ra_state *f_ra)
  1302. {
  1303. struct gfs2_inode *dip = GFS2_I(inode);
  1304. u32 hsize, len = 0;
  1305. u32 hash, index;
  1306. __be64 *lp;
  1307. int copied = 0;
  1308. int error = 0;
  1309. unsigned depth = 0;
  1310. hsize = BIT(dip->i_depth);
  1311. hash = gfs2_dir_offset2hash(ctx->pos);
  1312. index = hash >> (32 - dip->i_depth);
  1313. if (dip->i_hash_cache == NULL)
  1314. f_ra->start = 0;
  1315. lp = gfs2_dir_get_hash_table(dip);
  1316. if (IS_ERR(lp))
  1317. return PTR_ERR(lp);
  1318. gfs2_dir_readahead(inode, hsize, index, f_ra);
  1319. while (index < hsize) {
  1320. error = gfs2_dir_read_leaf(inode, ctx,
  1321. &copied, &depth,
  1322. be64_to_cpu(lp[index]));
  1323. if (error)
  1324. break;
  1325. len = BIT(dip->i_depth - depth);
  1326. index = (index & ~(len - 1)) + len;
  1327. }
  1328. if (error > 0)
  1329. error = 0;
  1330. return error;
  1331. }
  1332. int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
  1333. struct file_ra_state *f_ra)
  1334. {
  1335. struct gfs2_inode *dip = GFS2_I(inode);
  1336. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1337. struct dirent_gather g;
  1338. struct gfs2_dirent **darr, *dent;
  1339. struct buffer_head *dibh;
  1340. int copied = 0;
  1341. int error;
  1342. if (!dip->i_entries)
  1343. return 0;
  1344. if (dip->i_diskflags & GFS2_DIF_EXHASH)
  1345. return dir_e_read(inode, ctx, f_ra);
  1346. if (!gfs2_is_stuffed(dip)) {
  1347. gfs2_consist_inode(dip);
  1348. return -EIO;
  1349. }
  1350. error = gfs2_meta_inode_buffer(dip, &dibh);
  1351. if (error)
  1352. return error;
  1353. error = -ENOMEM;
  1354. /* 96 is max number of dirents which can be stuffed into an inode */
  1355. darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
  1356. if (darr) {
  1357. g.pdent = (const struct gfs2_dirent **)darr;
  1358. g.offset = 0;
  1359. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1360. gfs2_dirent_gather, NULL, &g);
  1361. if (IS_ERR(dent)) {
  1362. error = PTR_ERR(dent);
  1363. goto out;
  1364. }
  1365. if (dip->i_entries != g.offset) {
  1366. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1367. "ip->i_entries (%u) != g.offset (%u)\n",
  1368. (unsigned long long)dip->i_no_addr,
  1369. dip->i_entries,
  1370. g.offset);
  1371. gfs2_consist_inode(dip);
  1372. error = -EIO;
  1373. goto out;
  1374. }
  1375. gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries);
  1376. error = do_filldir_main(dip, ctx, darr,
  1377. dip->i_entries, 0, &copied);
  1378. out:
  1379. kfree(darr);
  1380. }
  1381. if (error > 0)
  1382. error = 0;
  1383. brelse(dibh);
  1384. return error;
  1385. }
  1386. /**
  1387. * gfs2_dir_search - Search a directory
  1388. * @dip: The GFS2 dir inode
  1389. * @name: The name we are looking up
  1390. * @fail_on_exist: Fail if the name exists rather than looking it up
  1391. *
  1392. * This routine searches a directory for a file or another directory.
  1393. * Assumes a glock is held on dip.
  1394. *
  1395. * Returns: errno
  1396. */
  1397. struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
  1398. bool fail_on_exist)
  1399. {
  1400. struct buffer_head *bh;
  1401. struct gfs2_dirent *dent;
  1402. u64 addr, formal_ino;
  1403. u16 dtype;
  1404. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1405. if (dent) {
  1406. struct inode *inode;
  1407. u16 rahead;
  1408. if (IS_ERR(dent))
  1409. return ERR_CAST(dent);
  1410. dtype = be16_to_cpu(dent->de_type);
  1411. rahead = be16_to_cpu(dent->de_rahead);
  1412. addr = be64_to_cpu(dent->de_inum.no_addr);
  1413. formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
  1414. brelse(bh);
  1415. if (fail_on_exist)
  1416. return ERR_PTR(-EEXIST);
  1417. inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino,
  1418. GFS2_BLKST_FREE /* ignore */);
  1419. if (!IS_ERR(inode))
  1420. GFS2_I(inode)->i_rahead = rahead;
  1421. return inode;
  1422. }
  1423. return ERR_PTR(-ENOENT);
  1424. }
  1425. int gfs2_dir_check(struct inode *dir, const struct qstr *name,
  1426. const struct gfs2_inode *ip)
  1427. {
  1428. struct buffer_head *bh;
  1429. struct gfs2_dirent *dent;
  1430. int ret = -ENOENT;
  1431. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1432. if (dent) {
  1433. if (IS_ERR(dent))
  1434. return PTR_ERR(dent);
  1435. if (ip) {
  1436. if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
  1437. goto out;
  1438. if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
  1439. ip->i_no_formal_ino)
  1440. goto out;
  1441. if (unlikely(IF2DT(ip->i_inode.i_mode) !=
  1442. be16_to_cpu(dent->de_type))) {
  1443. gfs2_consist_inode(GFS2_I(dir));
  1444. ret = -EIO;
  1445. goto out;
  1446. }
  1447. }
  1448. ret = 0;
  1449. out:
  1450. brelse(bh);
  1451. }
  1452. return ret;
  1453. }
  1454. /**
  1455. * dir_new_leaf - Add a new leaf onto hash chain
  1456. * @inode: The directory
  1457. * @name: The name we are adding
  1458. *
  1459. * This adds a new dir leaf onto an existing leaf when there is not
  1460. * enough space to add a new dir entry. This is a last resort after
  1461. * we've expanded the hash table to max size and also split existing
  1462. * leaf blocks, so it will only occur for very large directories.
  1463. *
  1464. * The dist parameter is set to 1 for leaf blocks directly attached
  1465. * to the hash table, 2 for one layer of indirection, 3 for two layers
  1466. * etc. We are thus able to tell the difference between an old leaf
  1467. * with dist set to zero (i.e. "don't know") and a new one where we
  1468. * set this information for debug/fsck purposes.
  1469. *
  1470. * Returns: 0 on success, or -ve on error
  1471. */
  1472. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1473. {
  1474. struct buffer_head *bh, *obh;
  1475. struct gfs2_inode *ip = GFS2_I(inode);
  1476. struct gfs2_leaf *leaf, *oleaf;
  1477. u32 dist = 1;
  1478. int error;
  1479. u32 index;
  1480. u64 bn;
  1481. index = name->hash >> (32 - ip->i_depth);
  1482. error = get_first_leaf(ip, index, &obh);
  1483. if (error)
  1484. return error;
  1485. do {
  1486. dist++;
  1487. oleaf = (struct gfs2_leaf *)obh->b_data;
  1488. bn = be64_to_cpu(oleaf->lf_next);
  1489. if (!bn)
  1490. break;
  1491. brelse(obh);
  1492. error = get_leaf(ip, bn, &obh);
  1493. if (error)
  1494. return error;
  1495. } while(1);
  1496. gfs2_trans_add_meta(ip->i_gl, obh);
  1497. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1498. if (!leaf) {
  1499. brelse(obh);
  1500. return -ENOSPC;
  1501. }
  1502. leaf->lf_dist = cpu_to_be32(dist);
  1503. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1504. brelse(bh);
  1505. brelse(obh);
  1506. error = gfs2_meta_inode_buffer(ip, &bh);
  1507. if (error)
  1508. return error;
  1509. gfs2_trans_add_meta(ip->i_gl, bh);
  1510. gfs2_add_inode_blocks(&ip->i_inode, 1);
  1511. gfs2_dinode_out(ip, bh->b_data);
  1512. brelse(bh);
  1513. return 0;
  1514. }
  1515. static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
  1516. {
  1517. u64 where = ip->i_no_addr + 1;
  1518. if (ip->i_eattr == where)
  1519. return 1;
  1520. return 0;
  1521. }
  1522. /**
  1523. * gfs2_dir_add - Add new filename into directory
  1524. * @inode: The directory inode
  1525. * @name: The new name
  1526. * @nip: The GFS2 inode to be linked in to the directory
  1527. * @da: The directory addition info
  1528. *
  1529. * If the call to gfs2_diradd_alloc_required resulted in there being
  1530. * no need to allocate any new directory blocks, then it will contain
  1531. * a pointer to the directory entry and the bh in which it resides. We
  1532. * can use that without having to repeat the search. If there was no
  1533. * free space, then we must now create more space.
  1534. *
  1535. * Returns: 0 on success, error code on failure
  1536. */
  1537. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1538. const struct gfs2_inode *nip, struct gfs2_diradd *da)
  1539. {
  1540. struct gfs2_inode *ip = GFS2_I(inode);
  1541. struct buffer_head *bh = da->bh;
  1542. struct gfs2_dirent *dent = da->dent;
  1543. struct timespec tv;
  1544. struct gfs2_leaf *leaf;
  1545. int error;
  1546. while(1) {
  1547. if (da->bh == NULL) {
  1548. dent = gfs2_dirent_search(inode, name,
  1549. gfs2_dirent_find_space, &bh);
  1550. }
  1551. if (dent) {
  1552. if (IS_ERR(dent))
  1553. return PTR_ERR(dent);
  1554. dent = gfs2_init_dirent(inode, dent, name, bh);
  1555. gfs2_inum_out(nip, dent);
  1556. dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
  1557. dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
  1558. tv = current_time(&ip->i_inode);
  1559. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  1560. leaf = (struct gfs2_leaf *)bh->b_data;
  1561. be16_add_cpu(&leaf->lf_entries, 1);
  1562. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  1563. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  1564. }
  1565. da->dent = NULL;
  1566. da->bh = NULL;
  1567. brelse(bh);
  1568. ip->i_entries++;
  1569. ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
  1570. if (S_ISDIR(nip->i_inode.i_mode))
  1571. inc_nlink(&ip->i_inode);
  1572. mark_inode_dirty(inode);
  1573. error = 0;
  1574. break;
  1575. }
  1576. if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1577. error = dir_make_exhash(inode);
  1578. if (error)
  1579. break;
  1580. continue;
  1581. }
  1582. error = dir_split_leaf(inode, name);
  1583. if (error == 0)
  1584. continue;
  1585. if (error < 0)
  1586. break;
  1587. if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
  1588. error = dir_double_exhash(ip);
  1589. if (error)
  1590. break;
  1591. error = dir_split_leaf(inode, name);
  1592. if (error < 0)
  1593. break;
  1594. if (error == 0)
  1595. continue;
  1596. }
  1597. error = dir_new_leaf(inode, name);
  1598. if (!error)
  1599. continue;
  1600. error = -ENOSPC;
  1601. break;
  1602. }
  1603. return error;
  1604. }
  1605. /**
  1606. * gfs2_dir_del - Delete a directory entry
  1607. * @dip: The GFS2 inode
  1608. * @filename: The filename
  1609. *
  1610. * Returns: 0 on success, error code on failure
  1611. */
  1612. int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
  1613. {
  1614. const struct qstr *name = &dentry->d_name;
  1615. struct gfs2_dirent *dent, *prev = NULL;
  1616. struct buffer_head *bh;
  1617. struct timespec tv = current_time(&dip->i_inode);
  1618. /* Returns _either_ the entry (if its first in block) or the
  1619. previous entry otherwise */
  1620. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1621. if (!dent) {
  1622. gfs2_consist_inode(dip);
  1623. return -EIO;
  1624. }
  1625. if (IS_ERR(dent)) {
  1626. gfs2_consist_inode(dip);
  1627. return PTR_ERR(dent);
  1628. }
  1629. /* If not first in block, adjust pointers accordingly */
  1630. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1631. prev = dent;
  1632. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1633. }
  1634. dirent_del(dip, bh, prev, dent);
  1635. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1636. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1637. u16 entries = be16_to_cpu(leaf->lf_entries);
  1638. if (!entries)
  1639. gfs2_consist_inode(dip);
  1640. leaf->lf_entries = cpu_to_be16(--entries);
  1641. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  1642. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  1643. }
  1644. brelse(bh);
  1645. if (!dip->i_entries)
  1646. gfs2_consist_inode(dip);
  1647. dip->i_entries--;
  1648. dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
  1649. if (d_is_dir(dentry))
  1650. drop_nlink(&dip->i_inode);
  1651. mark_inode_dirty(&dip->i_inode);
  1652. return 0;
  1653. }
  1654. /**
  1655. * gfs2_dir_mvino - Change inode number of directory entry
  1656. * @dip: The GFS2 inode
  1657. * @filename:
  1658. * @new_inode:
  1659. *
  1660. * This routine changes the inode number of a directory entry. It's used
  1661. * by rename to change ".." when a directory is moved.
  1662. * Assumes a glock is held on dvp.
  1663. *
  1664. * Returns: errno
  1665. */
  1666. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1667. const struct gfs2_inode *nip, unsigned int new_type)
  1668. {
  1669. struct buffer_head *bh;
  1670. struct gfs2_dirent *dent;
  1671. int error;
  1672. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1673. if (!dent) {
  1674. gfs2_consist_inode(dip);
  1675. return -EIO;
  1676. }
  1677. if (IS_ERR(dent))
  1678. return PTR_ERR(dent);
  1679. gfs2_trans_add_meta(dip->i_gl, bh);
  1680. gfs2_inum_out(nip, dent);
  1681. dent->de_type = cpu_to_be16(new_type);
  1682. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1683. brelse(bh);
  1684. error = gfs2_meta_inode_buffer(dip, &bh);
  1685. if (error)
  1686. return error;
  1687. gfs2_trans_add_meta(dip->i_gl, bh);
  1688. }
  1689. dip->i_inode.i_mtime = dip->i_inode.i_ctime = current_time(&dip->i_inode);
  1690. gfs2_dinode_out(dip, bh->b_data);
  1691. brelse(bh);
  1692. return 0;
  1693. }
  1694. /**
  1695. * leaf_dealloc - Deallocate a directory leaf
  1696. * @dip: the directory
  1697. * @index: the hash table offset in the directory
  1698. * @len: the number of pointers to this leaf
  1699. * @leaf_no: the leaf number
  1700. * @leaf_bh: buffer_head for the starting leaf
  1701. * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
  1702. *
  1703. * Returns: errno
  1704. */
  1705. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1706. u64 leaf_no, struct buffer_head *leaf_bh,
  1707. int last_dealloc)
  1708. {
  1709. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1710. struct gfs2_leaf *tmp_leaf;
  1711. struct gfs2_rgrp_list rlist;
  1712. struct buffer_head *bh, *dibh;
  1713. u64 blk, nblk;
  1714. unsigned int rg_blocks = 0, l_blocks = 0;
  1715. char *ht;
  1716. unsigned int x, size = len * sizeof(u64);
  1717. int error;
  1718. error = gfs2_rindex_update(sdp);
  1719. if (error)
  1720. return error;
  1721. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1722. ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
  1723. if (ht == NULL)
  1724. ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO,
  1725. PAGE_KERNEL);
  1726. if (!ht)
  1727. return -ENOMEM;
  1728. error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1729. if (error)
  1730. goto out;
  1731. /* Count the number of leaves */
  1732. bh = leaf_bh;
  1733. for (blk = leaf_no; blk; blk = nblk) {
  1734. if (blk != leaf_no) {
  1735. error = get_leaf(dip, blk, &bh);
  1736. if (error)
  1737. goto out_rlist;
  1738. }
  1739. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1740. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1741. if (blk != leaf_no)
  1742. brelse(bh);
  1743. gfs2_rlist_add(dip, &rlist, blk);
  1744. l_blocks++;
  1745. }
  1746. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1747. for (x = 0; x < rlist.rl_rgrps; x++) {
  1748. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
  1749. rg_blocks += rgd->rd_length;
  1750. }
  1751. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1752. if (error)
  1753. goto out_rlist;
  1754. error = gfs2_trans_begin(sdp,
  1755. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1756. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1757. if (error)
  1758. goto out_rg_gunlock;
  1759. bh = leaf_bh;
  1760. for (blk = leaf_no; blk; blk = nblk) {
  1761. if (blk != leaf_no) {
  1762. error = get_leaf(dip, blk, &bh);
  1763. if (error)
  1764. goto out_end_trans;
  1765. }
  1766. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1767. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1768. if (blk != leaf_no)
  1769. brelse(bh);
  1770. gfs2_free_meta(dip, blk, 1);
  1771. gfs2_add_inode_blocks(&dip->i_inode, -1);
  1772. }
  1773. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1774. if (error != size) {
  1775. if (error >= 0)
  1776. error = -EIO;
  1777. goto out_end_trans;
  1778. }
  1779. error = gfs2_meta_inode_buffer(dip, &dibh);
  1780. if (error)
  1781. goto out_end_trans;
  1782. gfs2_trans_add_meta(dip->i_gl, dibh);
  1783. /* On the last dealloc, make this a regular file in case we crash.
  1784. (We don't want to free these blocks a second time.) */
  1785. if (last_dealloc)
  1786. dip->i_inode.i_mode = S_IFREG;
  1787. gfs2_dinode_out(dip, dibh->b_data);
  1788. brelse(dibh);
  1789. out_end_trans:
  1790. gfs2_trans_end(sdp);
  1791. out_rg_gunlock:
  1792. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1793. out_rlist:
  1794. gfs2_rlist_free(&rlist);
  1795. gfs2_quota_unhold(dip);
  1796. out:
  1797. kvfree(ht);
  1798. return error;
  1799. }
  1800. /**
  1801. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1802. * @dip: the directory
  1803. *
  1804. * Dealloc all on-disk directory leaves to FREEMETA state
  1805. * Change on-disk inode type to "regular file"
  1806. *
  1807. * Returns: errno
  1808. */
  1809. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1810. {
  1811. struct buffer_head *bh;
  1812. struct gfs2_leaf *leaf;
  1813. u32 hsize, len;
  1814. u32 index = 0, next_index;
  1815. __be64 *lp;
  1816. u64 leaf_no;
  1817. int error = 0, last;
  1818. hsize = BIT(dip->i_depth);
  1819. lp = gfs2_dir_get_hash_table(dip);
  1820. if (IS_ERR(lp))
  1821. return PTR_ERR(lp);
  1822. while (index < hsize) {
  1823. leaf_no = be64_to_cpu(lp[index]);
  1824. if (leaf_no) {
  1825. error = get_leaf(dip, leaf_no, &bh);
  1826. if (error)
  1827. goto out;
  1828. leaf = (struct gfs2_leaf *)bh->b_data;
  1829. len = BIT(dip->i_depth - be16_to_cpu(leaf->lf_depth));
  1830. next_index = (index & ~(len - 1)) + len;
  1831. last = ((next_index >= hsize) ? 1 : 0);
  1832. error = leaf_dealloc(dip, index, len, leaf_no, bh,
  1833. last);
  1834. brelse(bh);
  1835. if (error)
  1836. goto out;
  1837. index = next_index;
  1838. } else
  1839. index++;
  1840. }
  1841. if (index != hsize) {
  1842. gfs2_consist_inode(dip);
  1843. error = -EIO;
  1844. }
  1845. out:
  1846. return error;
  1847. }
  1848. /**
  1849. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1850. * @ip: the file being written to
  1851. * @filname: the filename that's going to be added
  1852. * @da: The structure to return dir alloc info
  1853. *
  1854. * Returns: 0 if ok, -ve on error
  1855. */
  1856. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
  1857. struct gfs2_diradd *da)
  1858. {
  1859. struct gfs2_inode *ip = GFS2_I(inode);
  1860. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1861. const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
  1862. struct gfs2_dirent *dent;
  1863. struct buffer_head *bh;
  1864. da->nr_blocks = 0;
  1865. da->bh = NULL;
  1866. da->dent = NULL;
  1867. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1868. if (!dent) {
  1869. da->nr_blocks = sdp->sd_max_dirres;
  1870. if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
  1871. (GFS2_DIRENT_SIZE(name->len) < extra))
  1872. da->nr_blocks = 1;
  1873. return 0;
  1874. }
  1875. if (IS_ERR(dent))
  1876. return PTR_ERR(dent);
  1877. if (da->save_loc) {
  1878. da->bh = bh;
  1879. da->dent = dent;
  1880. } else {
  1881. brelse(bh);
  1882. }
  1883. return 0;
  1884. }