2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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.
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
39 * dip->i_diskflags & GFS2_DIF_EXHASH is true
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
43 * When the dirents are in leaves, the actual contents of the directory file are
44 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
45 * dirents are NOT in the directory file itself. There can be more than one
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the same leaf.
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
56 #include <linux/slab.h>
57 #include <linux/spinlock.h>
58 #include <linux/buffer_head.h>
59 #include <linux/sort.h>
60 #include <linux/gfs2_ondisk.h>
61 #include <linux/crc32.h>
62 #include <linux/vmalloc.h>
76 #define IS_LEAF 1 /* Hashed (leaf) directory */
77 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
79 #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
81 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
84 struct qstr gfs2_qdot __read_mostly;
85 struct qstr gfs2_qdotdot __read_mostly;
87 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
88 const struct qstr *name, void *opaque);
90 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
91 struct buffer_head **bhp)
93 struct buffer_head *bh;
95 bh = gfs2_meta_new(ip->i_gl, block);
96 gfs2_trans_add_bh(ip->i_gl, bh, 1);
97 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
103 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
104 struct buffer_head **bhp)
106 struct buffer_head *bh;
109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
120 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
123 struct buffer_head *dibh;
126 error = gfs2_meta_inode_buffer(ip, &dibh);
130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
132 if (ip->i_inode.i_size < offset + size)
133 i_size_write(&ip->i_inode, offset + size);
134 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
135 gfs2_dinode_out(ip, dibh->b_data);
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
151 * Returns: The number of bytes correctly written or error code
153 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
154 u64 offset, unsigned int size)
156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
157 struct buffer_head *dibh;
168 if (gfs2_is_stuffed(ip) &&
169 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
170 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
173 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
176 if (gfs2_is_stuffed(ip)) {
177 error = gfs2_unstuff_dinode(ip, NULL);
183 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
185 while (copied < size) {
187 struct buffer_head *bh;
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
200 if (gfs2_assert_withdraw(sdp, dblock))
204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
222 o = sizeof(struct gfs2_meta_header);
226 error = gfs2_meta_inode_buffer(ip, &dibh);
230 if (ip->i_inode.i_size < offset + copied)
231 i_size_write(&ip->i_inode, offset + copied);
232 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
235 gfs2_dinode_out(ip, dibh->b_data);
245 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
248 struct buffer_head *dibh;
251 error = gfs2_meta_inode_buffer(ip, &dibh);
253 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
257 return (error) ? error : size;
262 * gfs2_dir_read_data - Read a data from a directory inode
263 * @ip: The GFS2 Inode
264 * @buf: The buffer to place result into
265 * @size: Amount of data to transfer
267 * Returns: The amount of data actually copied or the error
269 static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
272 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
279 if (gfs2_is_stuffed(ip))
280 return gfs2_dir_read_stuffed(ip, buf, size);
282 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
286 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
288 while (copied < size) {
290 struct buffer_head *bh;
293 amount = size - copied;
294 if (amount > sdp->sd_sb.sb_bsize - o)
295 amount = sdp->sd_sb.sb_bsize - o;
299 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
301 if (error || !dblock)
304 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
306 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
310 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
317 memcpy(buf, bh->b_data + o, amount);
319 buf += (amount/sizeof(__be64));
322 o = sizeof(struct gfs2_meta_header);
327 return (copied) ? copied : error;
331 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
332 * @ip: The inode in question
334 * Returns: The hash table or an error
337 static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
339 struct inode *inode = &ip->i_inode;
344 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
346 hc = ip->i_hash_cache;
350 hsize = 1 << ip->i_depth;
351 hsize *= sizeof(__be64);
352 if (hsize != i_size_read(&ip->i_inode)) {
353 gfs2_consist_inode(ip);
354 return ERR_PTR(-EIO);
357 hc = kmalloc(hsize, GFP_NOFS);
360 return ERR_PTR(-ENOMEM);
362 ret = gfs2_dir_read_data(ip, hc, hsize);
368 spin_lock(&inode->i_lock);
369 if (ip->i_hash_cache)
372 ip->i_hash_cache = hc;
373 spin_unlock(&inode->i_lock);
375 return ip->i_hash_cache;
379 * gfs2_dir_hash_inval - Invalidate dir hash
380 * @ip: The directory inode
382 * Must be called with an exclusive glock, or during glock invalidation.
384 void gfs2_dir_hash_inval(struct gfs2_inode *ip)
386 __be64 *hc = ip->i_hash_cache;
387 ip->i_hash_cache = NULL;
391 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
393 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
396 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
397 const struct qstr *name, int ret)
399 if (!gfs2_dirent_sentinel(dent) &&
400 be32_to_cpu(dent->de_hash) == name->hash &&
401 be16_to_cpu(dent->de_name_len) == name->len &&
402 memcmp(dent+1, name->name, name->len) == 0)
407 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
408 const struct qstr *name,
411 return __gfs2_dirent_find(dent, name, 1);
414 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
415 const struct qstr *name,
418 return __gfs2_dirent_find(dent, name, 2);
422 * name->name holds ptr to start of block.
423 * name->len holds size of block.
425 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
426 const struct qstr *name,
429 const char *start = name->name;
430 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
431 if (name->len == (end - start))
436 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
437 const struct qstr *name,
440 unsigned required = GFS2_DIRENT_SIZE(name->len);
441 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
442 unsigned totlen = be16_to_cpu(dent->de_rec_len);
444 if (gfs2_dirent_sentinel(dent))
446 if (totlen - actual >= required)
451 struct dirent_gather {
452 const struct gfs2_dirent **pdent;
456 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
457 const struct qstr *name,
460 struct dirent_gather *g = opaque;
461 if (!gfs2_dirent_sentinel(dent)) {
462 g->pdent[g->offset++] = dent;
468 * Other possible things to check:
469 * - Inode located within filesystem size (and on valid block)
470 * - Valid directory entry type
471 * Not sure how heavy-weight we want to make this... could also check
472 * hash is correct for example, but that would take a lot of extra time.
473 * For now the most important thing is to check that the various sizes
476 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
477 unsigned int size, unsigned int len, int first)
479 const char *msg = "gfs2_dirent too small";
480 if (unlikely(size < sizeof(struct gfs2_dirent)))
482 msg = "gfs2_dirent misaligned";
483 if (unlikely(offset & 0x7))
485 msg = "gfs2_dirent points beyond end of block";
486 if (unlikely(offset + size > len))
488 msg = "zero inode number";
489 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
491 msg = "name length is greater than space in dirent";
492 if (!gfs2_dirent_sentinel(dent) &&
493 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
498 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
499 first ? "first in block" : "not first in block");
503 static int gfs2_dirent_offset(const void *buf)
505 const struct gfs2_meta_header *h = buf;
510 switch(be32_to_cpu(h->mh_type)) {
511 case GFS2_METATYPE_LF:
512 offset = sizeof(struct gfs2_leaf);
514 case GFS2_METATYPE_DI:
515 offset = sizeof(struct gfs2_dinode);
522 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
523 be32_to_cpu(h->mh_type));
527 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
528 unsigned int len, gfs2_dscan_t scan,
529 const struct qstr *name,
532 struct gfs2_dirent *dent, *prev;
537 ret = gfs2_dirent_offset(buf);
544 size = be16_to_cpu(dent->de_rec_len);
545 if (gfs2_check_dirent(dent, offset, size, len, 1))
548 ret = scan(dent, name, opaque);
556 size = be16_to_cpu(dent->de_rec_len);
557 if (gfs2_check_dirent(dent, offset, size, len, 0))
567 return prev ? prev : dent;
574 gfs2_consist_inode(GFS2_I(inode));
575 return ERR_PTR(-EIO);
578 static int dirent_check_reclen(struct gfs2_inode *dip,
579 const struct gfs2_dirent *d, const void *end_p)
582 u16 rec_len = be16_to_cpu(d->de_rec_len);
584 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
592 gfs2_consist_inode(dip);
597 * dirent_next - Next dirent
598 * @dip: the directory
600 * @dent: Pointer to list of dirents
602 * Returns: 0 on success, error code otherwise
605 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
606 struct gfs2_dirent **dent)
608 struct gfs2_dirent *cur = *dent, *tmp;
609 char *bh_end = bh->b_data + bh->b_size;
612 ret = dirent_check_reclen(dip, cur, bh_end);
616 tmp = (void *)cur + ret;
617 ret = dirent_check_reclen(dip, tmp, bh_end);
621 /* Only the first dent could ever have de_inum.no_addr == 0 */
622 if (gfs2_dirent_sentinel(tmp)) {
623 gfs2_consist_inode(dip);
632 * dirent_del - Delete a dirent
633 * @dip: The GFS2 inode
635 * @prev: The previous dirent
636 * @cur: The current dirent
640 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
641 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
643 u16 cur_rec_len, prev_rec_len;
645 if (gfs2_dirent_sentinel(cur)) {
646 gfs2_consist_inode(dip);
650 gfs2_trans_add_bh(dip->i_gl, bh, 1);
652 /* If there is no prev entry, this is the first entry in the block.
653 The de_rec_len is already as big as it needs to be. Just zero
654 out the inode number and return. */
657 cur->de_inum.no_addr = 0;
658 cur->de_inum.no_formal_ino = 0;
662 /* Combine this dentry with the previous one. */
664 prev_rec_len = be16_to_cpu(prev->de_rec_len);
665 cur_rec_len = be16_to_cpu(cur->de_rec_len);
667 if ((char *)prev + prev_rec_len != (char *)cur)
668 gfs2_consist_inode(dip);
669 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
670 gfs2_consist_inode(dip);
672 prev_rec_len += cur_rec_len;
673 prev->de_rec_len = cpu_to_be16(prev_rec_len);
677 * Takes a dent from which to grab space as an argument. Returns the
678 * newly created dent.
680 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
681 struct gfs2_dirent *dent,
682 const struct qstr *name,
683 struct buffer_head *bh)
685 struct gfs2_inode *ip = GFS2_I(inode);
686 struct gfs2_dirent *ndent;
687 unsigned offset = 0, totlen;
689 if (!gfs2_dirent_sentinel(dent))
690 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
691 totlen = be16_to_cpu(dent->de_rec_len);
692 BUG_ON(offset + name->len > totlen);
693 gfs2_trans_add_bh(ip->i_gl, bh, 1);
694 ndent = (struct gfs2_dirent *)((char *)dent + offset);
695 dent->de_rec_len = cpu_to_be16(offset);
696 gfs2_qstr2dirent(name, totlen - offset, ndent);
700 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
701 struct buffer_head *bh,
702 const struct qstr *name)
704 struct gfs2_dirent *dent;
705 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
706 gfs2_dirent_find_space, name, NULL);
707 if (!dent || IS_ERR(dent))
709 return gfs2_init_dirent(inode, dent, name, bh);
712 static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
713 struct buffer_head **bhp)
717 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
718 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
719 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
727 * get_leaf_nr - Get a leaf number associated with the index
728 * @dip: The GFS2 inode
732 * Returns: 0 on success, error code otherwise
735 static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
740 hash = gfs2_dir_get_hash_table(dip);
742 return PTR_ERR(hash);
743 *leaf_out = be64_to_cpu(*(hash + index));
747 static int get_first_leaf(struct gfs2_inode *dip, u32 index,
748 struct buffer_head **bh_out)
753 error = get_leaf_nr(dip, index, &leaf_no);
755 error = get_leaf(dip, leaf_no, bh_out);
760 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
761 const struct qstr *name,
763 struct buffer_head **pbh)
765 struct buffer_head *bh;
766 struct gfs2_dirent *dent;
767 struct gfs2_inode *ip = GFS2_I(inode);
770 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
771 struct gfs2_leaf *leaf;
772 unsigned hsize = 1 << ip->i_depth;
775 if (hsize * sizeof(u64) != i_size_read(inode)) {
776 gfs2_consist_inode(ip);
777 return ERR_PTR(-EIO);
780 index = name->hash >> (32 - ip->i_depth);
781 error = get_first_leaf(ip, index, &bh);
783 return ERR_PTR(error);
785 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
789 leaf = (struct gfs2_leaf *)bh->b_data;
790 ln = be64_to_cpu(leaf->lf_next);
795 error = get_leaf(ip, ln, &bh);
798 return error ? ERR_PTR(error) : NULL;
802 error = gfs2_meta_inode_buffer(ip, &bh);
804 return ERR_PTR(error);
805 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
807 if (unlikely(dent == NULL || IS_ERR(dent))) {
815 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
817 struct gfs2_inode *ip = GFS2_I(inode);
821 struct buffer_head *bh;
822 struct gfs2_leaf *leaf;
823 struct gfs2_dirent *dent;
824 struct qstr name = { .name = "" };
826 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
829 bh = gfs2_meta_new(ip->i_gl, bn);
833 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
834 gfs2_trans_add_bh(ip->i_gl, bh, 1);
835 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
836 leaf = (struct gfs2_leaf *)bh->b_data;
837 leaf->lf_depth = cpu_to_be16(depth);
838 leaf->lf_entries = 0;
839 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
841 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
842 dent = (struct gfs2_dirent *)(leaf+1);
843 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
849 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
850 * @dip: The GFS2 inode
852 * Returns: 0 on success, error code otherwise
855 static int dir_make_exhash(struct inode *inode)
857 struct gfs2_inode *dip = GFS2_I(inode);
858 struct gfs2_sbd *sdp = GFS2_SB(inode);
859 struct gfs2_dirent *dent;
861 struct buffer_head *bh, *dibh;
862 struct gfs2_leaf *leaf;
869 error = gfs2_meta_inode_buffer(dip, &dibh);
873 /* Turn over a new leaf */
875 leaf = new_leaf(inode, &bh, 0);
880 gfs2_assert(sdp, dip->i_entries < (1 << 16));
881 leaf->lf_entries = cpu_to_be16(dip->i_entries);
885 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
886 sizeof(struct gfs2_dinode));
888 /* Find last entry */
891 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
892 sizeof(struct gfs2_leaf);
893 args.name = bh->b_data;
894 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
895 gfs2_dirent_last, &args, NULL);
904 return PTR_ERR(dent);
907 /* Adjust the last dirent's record length
908 (Remember that dent still points to the last entry.) */
910 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
911 sizeof(struct gfs2_dinode) -
912 sizeof(struct gfs2_leaf));
916 /* We're done with the new leaf block, now setup the new
919 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
920 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
922 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
924 for (x = sdp->sd_hash_ptrs; x--; lp++)
925 *lp = cpu_to_be64(bn);
927 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
928 gfs2_add_inode_blocks(&dip->i_inode, 1);
929 dip->i_diskflags |= GFS2_DIF_EXHASH;
931 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
934 gfs2_dinode_out(dip, dibh->b_data);
942 * dir_split_leaf - Split a leaf block into two
943 * @dip: The GFS2 inode
947 * Returns: 0 on success, error code on failure
950 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
952 struct gfs2_inode *dip = GFS2_I(inode);
953 struct buffer_head *nbh, *obh, *dibh;
954 struct gfs2_leaf *nleaf, *oleaf;
955 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
956 u32 start, len, half_len, divider;
963 index = name->hash >> (32 - dip->i_depth);
964 error = get_leaf_nr(dip, index, &leaf_no);
968 /* Get the old leaf block */
969 error = get_leaf(dip, leaf_no, &obh);
973 oleaf = (struct gfs2_leaf *)obh->b_data;
974 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
976 return 1; /* can't split */
979 gfs2_trans_add_bh(dip->i_gl, obh, 1);
981 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
988 /* Compute the start and len of leaf pointers in the hash table. */
989 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
992 printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
993 gfs2_consist_inode(dip);
998 start = (index & ~(len - 1));
1000 /* Change the pointers.
1001 Don't bother distinguishing stuffed from non-stuffed.
1002 This code is complicated enough already. */
1003 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1009 /* Change the pointers */
1010 for (x = 0; x < half_len; x++)
1011 lp[x] = cpu_to_be64(bn);
1013 gfs2_dir_hash_inval(dip);
1015 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1016 half_len * sizeof(u64));
1017 if (error != half_len * sizeof(u64)) {
1025 /* Compute the divider */
1026 divider = (start + half_len) << (32 - dip->i_depth);
1028 /* Copy the entries */
1029 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
1033 if (dirent_next(dip, obh, &next))
1036 if (!gfs2_dirent_sentinel(dent) &&
1037 be32_to_cpu(dent->de_hash) < divider) {
1039 str.name = (char*)(dent+1);
1040 str.len = be16_to_cpu(dent->de_name_len);
1041 str.hash = be32_to_cpu(dent->de_hash);
1042 new = gfs2_dirent_alloc(inode, nbh, &str);
1044 error = PTR_ERR(new);
1048 new->de_inum = dent->de_inum; /* No endian worries */
1049 new->de_type = dent->de_type; /* No endian worries */
1050 be16_add_cpu(&nleaf->lf_entries, 1);
1052 dirent_del(dip, obh, prev, dent);
1054 if (!oleaf->lf_entries)
1055 gfs2_consist_inode(dip);
1056 be16_add_cpu(&oleaf->lf_entries, -1);
1068 oleaf->lf_depth = nleaf->lf_depth;
1070 error = gfs2_meta_inode_buffer(dip, &dibh);
1071 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1072 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1073 gfs2_add_inode_blocks(&dip->i_inode, 1);
1074 gfs2_dinode_out(dip, dibh->b_data);
1093 * dir_double_exhash - Double size of ExHash table
1094 * @dip: The GFS2 dinode
1096 * Returns: 0 on success, error code on failure
1099 static int dir_double_exhash(struct gfs2_inode *dip)
1101 struct buffer_head *dibh;
1109 hsize = 1 << dip->i_depth;
1110 hsize_bytes = hsize * sizeof(__be64);
1112 hc = gfs2_dir_get_hash_table(dip);
1116 h = hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS);
1120 error = gfs2_meta_inode_buffer(dip, &dibh);
1124 for (x = 0; x < hsize; x++) {
1130 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1131 if (error != (hsize_bytes * 2))
1134 gfs2_dir_hash_inval(dip);
1135 dip->i_hash_cache = hc2;
1137 gfs2_dinode_out(dip, dibh->b_data);
1142 /* Replace original hash table & size */
1143 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1144 i_size_write(&dip->i_inode, hsize_bytes);
1145 gfs2_dinode_out(dip, dibh->b_data);
1153 * compare_dents - compare directory entries by hash value
1157 * When comparing the hash entries of @a to @b:
1163 static int compare_dents(const void *a, const void *b)
1165 const struct gfs2_dirent *dent_a, *dent_b;
1169 dent_a = *(const struct gfs2_dirent **)a;
1170 hash_a = be32_to_cpu(dent_a->de_hash);
1172 dent_b = *(const struct gfs2_dirent **)b;
1173 hash_b = be32_to_cpu(dent_b->de_hash);
1175 if (hash_a > hash_b)
1177 else if (hash_a < hash_b)
1180 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1181 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1185 else if (len_a < len_b)
1188 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
1195 * do_filldir_main - read out directory entries
1196 * @dip: The GFS2 inode
1197 * @offset: The offset in the file to read from
1198 * @opaque: opaque data to pass to filldir
1199 * @filldir: The function to pass entries to
1200 * @darr: an array of struct gfs2_dirent pointers to read
1201 * @entries: the number of entries in darr
1202 * @copied: pointer to int that's non-zero if a entry has been copied out
1204 * Jump through some hoops to make sure that if there are hash collsions,
1205 * they are read out at the beginning of a buffer. We want to minimize
1206 * the possibility that they will fall into different readdir buffers or
1207 * that someone will want to seek to that location.
1209 * Returns: errno, >0 on exception from filldir
1212 static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
1213 void *opaque, filldir_t filldir,
1214 const struct gfs2_dirent **darr, u32 entries,
1217 const struct gfs2_dirent *dent, *dent_next;
1223 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1225 dent_next = darr[0];
1226 off_next = be32_to_cpu(dent_next->de_hash);
1227 off_next = gfs2_disk_hash2offset(off_next);
1229 for (x = 0, y = 1; x < entries; x++, y++) {
1234 dent_next = darr[y];
1235 off_next = be32_to_cpu(dent_next->de_hash);
1236 off_next = gfs2_disk_hash2offset(off_next);
1242 if (off_next == off) {
1243 if (*copied && !run)
1254 error = filldir(opaque, (const char *)(dent + 1),
1255 be16_to_cpu(dent->de_name_len),
1256 off, be64_to_cpu(dent->de_inum.no_addr),
1257 be16_to_cpu(dent->de_type));
1264 /* Increment the *offset by one, so the next time we come into the
1265 do_filldir fxn, we get the next entry instead of the last one in the
1273 static void *gfs2_alloc_sort_buffer(unsigned size)
1277 if (size < KMALLOC_MAX_SIZE)
1278 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1280 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1284 static void gfs2_free_sort_buffer(void *ptr)
1286 if (is_vmalloc_addr(ptr))
1292 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1293 filldir_t filldir, int *copied, unsigned *depth,
1296 struct gfs2_inode *ip = GFS2_I(inode);
1297 struct gfs2_sbd *sdp = GFS2_SB(inode);
1298 struct buffer_head *bh;
1299 struct gfs2_leaf *lf;
1300 unsigned entries = 0, entries2 = 0;
1301 unsigned leaves = 0;
1302 const struct gfs2_dirent **darr, *dent;
1303 struct dirent_gather g;
1304 struct buffer_head **larr;
1310 error = get_leaf(ip, lfn, &bh);
1313 lf = (struct gfs2_leaf *)bh->b_data;
1315 *depth = be16_to_cpu(lf->lf_depth);
1316 entries += be16_to_cpu(lf->lf_entries);
1318 lfn = be64_to_cpu(lf->lf_next);
1327 * The extra 99 entries are not normally used, but are a buffer
1328 * zone in case the number of entries in the leaf is corrupt.
1329 * 99 is the maximum number of entries that can fit in a single
1332 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
1335 darr = (const struct gfs2_dirent **)(larr + leaves);
1341 error = get_leaf(ip, lfn, &bh);
1344 lf = (struct gfs2_leaf *)bh->b_data;
1345 lfn = be64_to_cpu(lf->lf_next);
1346 if (lf->lf_entries) {
1347 entries2 += be16_to_cpu(lf->lf_entries);
1348 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1349 gfs2_dirent_gather, NULL, &g);
1350 error = PTR_ERR(dent);
1353 if (entries2 != g.offset) {
1354 fs_warn(sdp, "Number of entries corrupt in dir "
1355 "leaf %llu, entries2 (%u) != "
1357 (unsigned long long)bh->b_blocknr,
1358 entries2, g.offset);
1370 BUG_ON(entries2 != entries);
1371 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1374 for(i = 0; i < leaf; i++)
1376 gfs2_free_sort_buffer(larr);
1382 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
1384 * Note: we can't calculate each index like dir_e_read can because we don't
1385 * have the leaf, and therefore we don't have the depth, and therefore we
1386 * don't have the length. So we have to just read enough ahead to make up
1387 * for the loss of information.
1389 static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1390 struct file_ra_state *f_ra)
1392 struct gfs2_inode *ip = GFS2_I(inode);
1393 struct gfs2_glock *gl = ip->i_gl;
1394 struct buffer_head *bh;
1395 u64 blocknr = 0, last;
1398 /* First check if we've already read-ahead for the whole range. */
1399 if (index + MAX_RA_BLOCKS < f_ra->start)
1402 f_ra->start = max((pgoff_t)index, f_ra->start);
1403 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1404 if (f_ra->start >= hsize) /* if exceeded the hash table */
1408 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1410 if (blocknr == last)
1413 bh = gfs2_getbuf(gl, blocknr, 1);
1414 if (trylock_buffer(bh)) {
1415 if (buffer_uptodate(bh)) {
1420 bh->b_end_io = end_buffer_read_sync;
1421 submit_bh(READA | REQ_META, bh);
1429 * dir_e_read - Reads the entries from a directory into a filldir buffer
1430 * @dip: dinode pointer
1431 * @offset: the hash of the last entry read shifted to the right once
1432 * @opaque: buffer for the filldir function to fill
1433 * @filldir: points to the filldir function to use
1438 static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
1439 filldir_t filldir, struct file_ra_state *f_ra)
1441 struct gfs2_inode *dip = GFS2_I(inode);
1449 hsize = 1 << dip->i_depth;
1450 hash = gfs2_dir_offset2hash(*offset);
1451 index = hash >> (32 - dip->i_depth);
1453 if (dip->i_hash_cache == NULL)
1455 lp = gfs2_dir_get_hash_table(dip);
1459 gfs2_dir_readahead(inode, hsize, index, f_ra);
1461 while (index < hsize) {
1462 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1464 be64_to_cpu(lp[index]));
1468 len = 1 << (dip->i_depth - depth);
1469 index = (index & ~(len - 1)) + len;
1477 int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
1478 filldir_t filldir, struct file_ra_state *f_ra)
1480 struct gfs2_inode *dip = GFS2_I(inode);
1481 struct gfs2_sbd *sdp = GFS2_SB(inode);
1482 struct dirent_gather g;
1483 const struct gfs2_dirent **darr, *dent;
1484 struct buffer_head *dibh;
1488 if (!dip->i_entries)
1491 if (dip->i_diskflags & GFS2_DIF_EXHASH)
1492 return dir_e_read(inode, offset, opaque, filldir, f_ra);
1494 if (!gfs2_is_stuffed(dip)) {
1495 gfs2_consist_inode(dip);
1499 error = gfs2_meta_inode_buffer(dip, &dibh);
1504 /* 96 is max number of dirents which can be stuffed into an inode */
1505 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
1509 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1510 gfs2_dirent_gather, NULL, &g);
1512 error = PTR_ERR(dent);
1515 if (dip->i_entries != g.offset) {
1516 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
1517 "ip->i_entries (%u) != g.offset (%u)\n",
1518 (unsigned long long)dip->i_no_addr,
1524 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1525 dip->i_entries, &copied);
1539 * gfs2_dir_search - Search a directory
1540 * @dip: The GFS2 inode
1544 * This routine searches a directory for a file or another directory.
1545 * Assumes a glock is held on dip.
1550 struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
1552 struct buffer_head *bh;
1553 struct gfs2_dirent *dent;
1554 struct inode *inode;
1556 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1559 return ERR_CAST(dent);
1560 inode = gfs2_inode_lookup(dir->i_sb,
1561 be16_to_cpu(dent->de_type),
1562 be64_to_cpu(dent->de_inum.no_addr),
1563 be64_to_cpu(dent->de_inum.no_formal_ino), 0);
1567 return ERR_PTR(-ENOENT);
1570 int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1571 const struct gfs2_inode *ip)
1573 struct buffer_head *bh;
1574 struct gfs2_dirent *dent;
1577 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1580 return PTR_ERR(dent);
1582 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1584 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1585 ip->i_no_formal_ino)
1587 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1588 be16_to_cpu(dent->de_type))) {
1589 gfs2_consist_inode(GFS2_I(dir));
1601 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1603 struct buffer_head *bh, *obh;
1604 struct gfs2_inode *ip = GFS2_I(inode);
1605 struct gfs2_leaf *leaf, *oleaf;
1610 index = name->hash >> (32 - ip->i_depth);
1611 error = get_first_leaf(ip, index, &obh);
1615 oleaf = (struct gfs2_leaf *)obh->b_data;
1616 bn = be64_to_cpu(oleaf->lf_next);
1620 error = get_leaf(ip, bn, &obh);
1625 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1627 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1632 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1636 error = gfs2_meta_inode_buffer(ip, &bh);
1639 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1640 gfs2_add_inode_blocks(&ip->i_inode, 1);
1641 gfs2_dinode_out(ip, bh->b_data);
1647 * gfs2_dir_add - Add new filename into directory
1648 * @dip: The GFS2 inode
1649 * @filename: The new name
1650 * @inode: The inode number of the entry
1651 * @type: The type of the entry
1653 * Returns: 0 on success, error code on failure
1656 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1657 const struct gfs2_inode *nip)
1659 struct gfs2_inode *ip = GFS2_I(inode);
1660 struct buffer_head *bh;
1661 struct gfs2_dirent *dent;
1662 struct gfs2_leaf *leaf;
1666 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1670 return PTR_ERR(dent);
1671 dent = gfs2_init_dirent(inode, dent, name, bh);
1672 gfs2_inum_out(nip, dent);
1673 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
1674 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
1675 leaf = (struct gfs2_leaf *)bh->b_data;
1676 be16_add_cpu(&leaf->lf_entries, 1);
1680 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1681 if (S_ISDIR(nip->i_inode.i_mode))
1682 inc_nlink(&ip->i_inode);
1683 mark_inode_dirty(inode);
1687 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
1688 error = dir_make_exhash(inode);
1693 error = dir_split_leaf(inode, name);
1698 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
1699 error = dir_double_exhash(ip);
1702 error = dir_split_leaf(inode, name);
1708 error = dir_new_leaf(inode, name);
1719 * gfs2_dir_del - Delete a directory entry
1720 * @dip: The GFS2 inode
1721 * @filename: The filename
1723 * Returns: 0 on success, error code on failure
1726 int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
1728 const struct qstr *name = &dentry->d_name;
1729 struct gfs2_dirent *dent, *prev = NULL;
1730 struct buffer_head *bh;
1732 /* Returns _either_ the entry (if its first in block) or the
1733 previous entry otherwise */
1734 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1736 gfs2_consist_inode(dip);
1740 gfs2_consist_inode(dip);
1741 return PTR_ERR(dent);
1743 /* If not first in block, adjust pointers accordingly */
1744 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1746 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1749 dirent_del(dip, bh, prev, dent);
1750 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1751 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1752 u16 entries = be16_to_cpu(leaf->lf_entries);
1754 gfs2_consist_inode(dip);
1755 leaf->lf_entries = cpu_to_be16(--entries);
1759 if (!dip->i_entries)
1760 gfs2_consist_inode(dip);
1762 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
1763 if (S_ISDIR(dentry->d_inode->i_mode))
1764 drop_nlink(&dip->i_inode);
1765 mark_inode_dirty(&dip->i_inode);
1771 * gfs2_dir_mvino - Change inode number of directory entry
1772 * @dip: The GFS2 inode
1776 * This routine changes the inode number of a directory entry. It's used
1777 * by rename to change ".." when a directory is moved.
1778 * Assumes a glock is held on dvp.
1783 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1784 const struct gfs2_inode *nip, unsigned int new_type)
1786 struct buffer_head *bh;
1787 struct gfs2_dirent *dent;
1790 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1792 gfs2_consist_inode(dip);
1796 return PTR_ERR(dent);
1798 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1799 gfs2_inum_out(nip, dent);
1800 dent->de_type = cpu_to_be16(new_type);
1802 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1804 error = gfs2_meta_inode_buffer(dip, &bh);
1807 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1810 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
1811 gfs2_dinode_out(dip, bh->b_data);
1817 * leaf_dealloc - Deallocate a directory leaf
1818 * @dip: the directory
1819 * @index: the hash table offset in the directory
1820 * @len: the number of pointers to this leaf
1821 * @leaf_no: the leaf number
1822 * @leaf_bh: buffer_head for the starting leaf
1823 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1828 static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1829 u64 leaf_no, struct buffer_head *leaf_bh,
1832 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1833 struct gfs2_leaf *tmp_leaf;
1834 struct gfs2_rgrp_list rlist;
1835 struct buffer_head *bh, *dibh;
1837 unsigned int rg_blocks = 0, l_blocks = 0;
1839 unsigned int x, size = len * sizeof(u64);
1842 error = gfs2_rindex_update(sdp);
1846 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1848 ht = kzalloc(size, GFP_NOFS);
1852 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1856 /* Count the number of leaves */
1859 for (blk = leaf_no; blk; blk = nblk) {
1860 if (blk != leaf_no) {
1861 error = get_leaf(dip, blk, &bh);
1865 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1866 nblk = be64_to_cpu(tmp_leaf->lf_next);
1870 gfs2_rlist_add(dip, &rlist, blk);
1874 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1876 for (x = 0; x < rlist.rl_rgrps; x++) {
1877 struct gfs2_rgrpd *rgd;
1878 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1879 rg_blocks += rgd->rd_length;
1882 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1886 error = gfs2_trans_begin(sdp,
1887 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1888 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1890 goto out_rg_gunlock;
1894 for (blk = leaf_no; blk; blk = nblk) {
1895 if (blk != leaf_no) {
1896 error = get_leaf(dip, blk, &bh);
1900 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1901 nblk = be64_to_cpu(tmp_leaf->lf_next);
1905 gfs2_free_meta(dip, blk, 1);
1906 gfs2_add_inode_blocks(&dip->i_inode, -1);
1909 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
1910 if (error != size) {
1916 error = gfs2_meta_inode_buffer(dip, &dibh);
1920 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1921 /* On the last dealloc, make this a regular file in case we crash.
1922 (We don't want to free these blocks a second time.) */
1924 dip->i_inode.i_mode = S_IFREG;
1925 gfs2_dinode_out(dip, dibh->b_data);
1929 gfs2_trans_end(sdp);
1931 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1933 gfs2_rlist_free(&rlist);
1934 gfs2_quota_unhold(dip);
1941 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1942 * @dip: the directory
1944 * Dealloc all on-disk directory leaves to FREEMETA state
1945 * Change on-disk inode type to "regular file"
1950 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1952 struct buffer_head *bh;
1953 struct gfs2_leaf *leaf;
1955 u32 index = 0, next_index;
1958 int error = 0, last;
1960 hsize = 1 << dip->i_depth;
1962 lp = gfs2_dir_get_hash_table(dip);
1966 while (index < hsize) {
1967 leaf_no = be64_to_cpu(lp[index]);
1969 error = get_leaf(dip, leaf_no, &bh);
1972 leaf = (struct gfs2_leaf *)bh->b_data;
1973 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1975 next_index = (index & ~(len - 1)) + len;
1976 last = ((next_index >= hsize) ? 1 : 0);
1977 error = leaf_dealloc(dip, index, len, leaf_no, bh,
1987 if (index != hsize) {
1988 gfs2_consist_inode(dip);
1998 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1999 * @ip: the file being written to
2000 * @filname: the filename that's going to be added
2002 * Returns: 1 if alloc required, 0 if not, -ve on error
2005 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
2007 struct gfs2_dirent *dent;
2008 struct buffer_head *bh;
2010 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
2015 return PTR_ERR(dent);