PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/gfs2/dir.c

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