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 v.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_di.di_flags & 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/sched.h>
57 #include <linux/slab.h>
58 #include <linux/spinlock.h>
59 #include <linux/buffer_head.h>
60 #include <linux/sort.h>
61 #include <linux/gfs2_ondisk.h>
62 #include <linux/crc32.h>
63 #include <linux/vmalloc.h>
66 #include "lm_interface.h"
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
82 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
84 typedef int (*leaf_call_t) (struct gfs2_inode *dip,
85 uint32_t index, uint32_t len, uint64_t leaf_no,
89 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, uint64_t block,
90 struct buffer_head **bhp)
92 struct buffer_head *bh;
94 bh = gfs2_meta_new(ip->i_gl, block);
95 gfs2_trans_add_bh(ip->i_gl, bh, 1);
96 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
97 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
102 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, uint64_t block,
103 struct buffer_head **bhp)
105 struct buffer_head *bh;
108 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
111 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
119 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
120 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_di.di_size < offset + size)
133 ip->i_di.di_size = offset + size;
134 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
135 gfs2_dinode_out(&ip->i_di, 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 uint64_t offset, unsigned int size)
156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
157 struct buffer_head *dibh;
158 uint64_t lblock, dblock;
167 if (gfs2_is_stuffed(ip) &&
168 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
169 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
172 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
175 if (gfs2_is_stuffed(ip)) {
176 error = gfs2_unstuff_dinode(ip, NULL);
182 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
184 while (copied < size) {
186 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);
224 o = sizeof(struct gfs2_meta_header);
228 error = gfs2_meta_inode_buffer(ip, &dibh);
232 if (ip->i_di.di_size < offset + copied)
233 ip->i_di.di_size = offset + copied;
234 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
236 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
237 gfs2_dinode_out(&ip->i_di, dibh->b_data);
247 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
248 unsigned int offset, unsigned int size)
250 struct buffer_head *dibh;
253 error = gfs2_meta_inode_buffer(ip, &dibh);
255 offset += sizeof(struct gfs2_dinode);
256 memcpy(buf, dibh->b_data + offset, size);
260 return (error) ? error : size;
265 * gfs2_dir_read_data - Read a data from a directory inode
266 * @ip: The GFS2 Inode
267 * @buf: The buffer to place result into
268 * @offset: File offset to begin jdata_readng from
269 * @size: Amount of data to transfer
271 * Returns: The amount of data actually copied or the error
273 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
274 uint64_t offset, unsigned int size)
276 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
277 uint64_t lblock, dblock;
283 if (offset >= ip->i_di.di_size)
286 if ((offset + size) > ip->i_di.di_size)
287 size = ip->i_di.di_size - offset;
292 if (gfs2_is_stuffed(ip))
293 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
296 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
300 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
302 while (copied < size) {
304 struct buffer_head *bh;
307 amount = size - copied;
308 if (amount > sdp->sd_sb.sb_bsize - o)
309 amount = sdp->sd_sb.sb_bsize - o;
313 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
320 gfs2_meta_ra(ip->i_gl, dblock, extlen);
324 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
326 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
334 memcpy(buf, bh->b_data + o, amount);
343 o = sizeof(struct gfs2_meta_header);
348 return (copied) ? copied : error;
351 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
352 const struct qstr *name,
355 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
356 const struct qstr *name, int ret)
358 if (dent->de_inum.no_addr != 0 &&
359 be32_to_cpu(dent->de_hash) == name->hash &&
360 be16_to_cpu(dent->de_name_len) == name->len &&
361 memcmp((char *)(dent+1), name->name, name->len) == 0)
366 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
367 const struct qstr *name,
370 return __gfs2_dirent_find(dent, name, 1);
373 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
374 const struct qstr *name,
377 return __gfs2_dirent_find(dent, name, 2);
381 * name->name holds ptr to start of block.
382 * name->len holds size of block.
384 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
385 const struct qstr *name,
388 const char *start = name->name;
389 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
390 if (name->len == (end - start))
395 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
396 const struct qstr *name,
399 unsigned required = GFS2_DIRENT_SIZE(name->len);
400 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
401 unsigned totlen = be16_to_cpu(dent->de_rec_len);
403 if (!dent->de_inum.no_addr)
404 actual = GFS2_DIRENT_SIZE(0);
405 if ((totlen - actual) >= required)
410 struct dirent_gather {
411 const struct gfs2_dirent **pdent;
415 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
416 const struct qstr *name,
419 struct dirent_gather *g = opaque;
420 if (dent->de_inum.no_addr) {
421 g->pdent[g->offset++] = dent;
427 * Other possible things to check:
428 * - Inode located within filesystem size (and on valid block)
429 * - Valid directory entry type
430 * Not sure how heavy-weight we want to make this... could also check
431 * hash is correct for example, but that would take a lot of extra time.
432 * For now the most important thing is to check that the various sizes
435 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
436 unsigned int size, unsigned int len, int first)
438 const char *msg = "gfs2_dirent too small";
439 if (unlikely(size < sizeof(struct gfs2_dirent)))
441 msg = "gfs2_dirent misaligned";
442 if (unlikely(offset & 0x7))
444 msg = "gfs2_dirent points beyond end of block";
445 if (unlikely(offset + size > len))
447 msg = "zero inode number";
448 if (unlikely(!first && !dent->de_inum.no_addr))
450 msg = "name length is greater than space in dirent";
451 if (dent->de_inum.no_addr &&
452 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
457 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
458 first ? "first in block" : "not first in block");
462 static int gfs2_dirent_offset(const void *buf)
464 const struct gfs2_meta_header *h = buf;
469 switch(be32_to_cpu(h->mh_type)) {
470 case GFS2_METATYPE_LF:
471 offset = sizeof(struct gfs2_leaf);
473 case GFS2_METATYPE_DI:
474 offset = sizeof(struct gfs2_dinode);
481 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
482 be32_to_cpu(h->mh_type));
486 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
488 unsigned int len, gfs2_dscan_t scan,
489 const struct qstr *name,
492 struct gfs2_dirent *dent, *prev;
497 ret = gfs2_dirent_offset(buf);
503 dent = (struct gfs2_dirent *)(buf + offset);
504 size = be16_to_cpu(dent->de_rec_len);
505 if (gfs2_check_dirent(dent, offset, size, len, 1))
508 ret = scan(dent, name, opaque);
515 dent = (struct gfs2_dirent *)(buf + offset);
516 size = be16_to_cpu(dent->de_rec_len);
517 if (gfs2_check_dirent(dent, offset, size, len, 0))
527 return prev ? prev : dent;
534 gfs2_consist_inode(GFS2_I(inode));
535 return ERR_PTR(-EIO);
540 * dirent_first - Return the first dirent
541 * @dip: the directory
543 * @dent: Pointer to list of dirents
545 * return first dirent whether bh points to leaf or stuffed dinode
547 * Returns: IS_LEAF, IS_DINODE, or -errno
550 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
551 struct gfs2_dirent **dent)
553 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
555 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
556 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
558 *dent = (struct gfs2_dirent *)(bh->b_data +
559 sizeof(struct gfs2_leaf));
562 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
564 *dent = (struct gfs2_dirent *)(bh->b_data +
565 sizeof(struct gfs2_dinode));
571 * dirent_next - Next dirent
572 * @dip: the directory
574 * @dent: Pointer to list of dirents
576 * Returns: 0 on success, error code otherwise
579 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
580 struct gfs2_dirent **dent)
582 struct gfs2_dirent *tmp, *cur;
584 uint16_t cur_rec_len;
587 bh_end = bh->b_data + bh->b_size;
588 cur_rec_len = be16_to_cpu(cur->de_rec_len);
590 if ((char *)cur + cur_rec_len >= bh_end) {
591 if ((char *)cur + cur_rec_len > bh_end) {
592 gfs2_consist_inode(dip);
598 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
600 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
601 gfs2_consist_inode(dip);
605 if (cur_rec_len == 0) {
606 gfs2_consist_inode(dip);
610 /* Only the first dent could ever have de_inum.no_addr == 0 */
611 if (!tmp->de_inum.no_addr) {
612 gfs2_consist_inode(dip);
622 * dirent_del - Delete a dirent
623 * @dip: The GFS2 inode
625 * @prev: The previous dirent
626 * @cur: The current dirent
630 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
631 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
633 uint16_t cur_rec_len, prev_rec_len;
635 if (!cur->de_inum.no_addr) {
636 gfs2_consist_inode(dip);
640 gfs2_trans_add_bh(dip->i_gl, bh, 1);
642 /* If there is no prev entry, this is the first entry in the block.
643 The de_rec_len is already as big as it needs to be. Just zero
644 out the inode number and return. */
647 cur->de_inum.no_addr = 0; /* No endianess worries */
651 /* Combine this dentry with the previous one. */
653 prev_rec_len = be16_to_cpu(prev->de_rec_len);
654 cur_rec_len = be16_to_cpu(cur->de_rec_len);
656 if ((char *)prev + prev_rec_len != (char *)cur)
657 gfs2_consist_inode(dip);
658 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
659 gfs2_consist_inode(dip);
661 prev_rec_len += cur_rec_len;
662 prev->de_rec_len = cpu_to_be16(prev_rec_len);
666 * Takes a dent from which to grab space as an argument. Returns the
667 * newly created dent.
669 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
670 struct gfs2_dirent *dent,
671 const struct qstr *name,
672 struct buffer_head *bh)
674 struct gfs2_inode *ip = GFS2_I(inode);
675 struct gfs2_dirent *ndent;
676 unsigned offset = 0, totlen;
678 if (dent->de_inum.no_addr)
679 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
680 totlen = be16_to_cpu(dent->de_rec_len);
681 BUG_ON(offset + name->len > totlen);
682 gfs2_trans_add_bh(ip->i_gl, bh, 1);
683 ndent = (struct gfs2_dirent *)((char *)dent + offset);
684 dent->de_rec_len = cpu_to_be16(offset);
685 gfs2_qstr2dirent(name, totlen - offset, ndent);
689 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
690 struct buffer_head *bh,
691 const struct qstr *name)
693 struct gfs2_dirent *dent;
694 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
695 gfs2_dirent_find_space, name, NULL);
696 if (!dent || IS_ERR(dent))
698 return gfs2_init_dirent(inode, dent, name, bh);
701 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
702 struct buffer_head **bhp)
706 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
707 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
708 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
716 * get_leaf_nr - Get a leaf number associated with the index
717 * @dip: The GFS2 inode
721 * Returns: 0 on success, error code otherwise
724 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
730 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
731 index * sizeof(uint64_t),
733 if (error != sizeof(uint64_t))
734 return (error < 0) ? error : -EIO;
736 *leaf_out = be64_to_cpu(leaf_no);
741 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
742 struct buffer_head **bh_out)
747 error = get_leaf_nr(dip, index, &leaf_no);
749 error = get_leaf(dip, leaf_no, bh_out);
754 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
755 const struct qstr *name,
757 struct buffer_head **pbh)
759 struct buffer_head *bh;
760 struct gfs2_dirent *dent;
761 struct gfs2_inode *ip = GFS2_I(inode);
764 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
765 struct gfs2_leaf *leaf;
766 unsigned hsize = 1 << ip->i_di.di_depth;
769 if (hsize * sizeof(u64) != ip->i_di.di_size) {
770 gfs2_consist_inode(ip);
771 return ERR_PTR(-EIO);
774 index = name->hash >> (32 - ip->i_di.di_depth);
775 error = get_first_leaf(ip, index, &bh);
777 return ERR_PTR(error);
779 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
783 leaf = (struct gfs2_leaf *)bh->b_data;
784 ln = be64_to_cpu(leaf->lf_next);
789 error = get_leaf(ip, ln, &bh);
792 return error ? ERR_PTR(error) : NULL;
796 error = gfs2_meta_inode_buffer(ip, &bh);
798 return ERR_PTR(error);
799 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
801 if (unlikely(dent == NULL || IS_ERR(dent))) {
809 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
811 struct gfs2_inode *ip = GFS2_I(inode);
812 u64 bn = gfs2_alloc_meta(ip);
813 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
814 struct gfs2_leaf *leaf;
815 struct gfs2_dirent *dent;
816 struct qstr name = { .name = "", .len = 0, .hash = 0 };
820 gfs2_trans_add_bh(ip->i_gl, bh, 1);
821 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
822 leaf = (struct gfs2_leaf *)bh->b_data;
823 leaf->lf_depth = cpu_to_be16(depth);
824 leaf->lf_entries = cpu_to_be16(0);
825 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
826 leaf->lf_next = cpu_to_be64(0);
827 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
828 dent = (struct gfs2_dirent *)(leaf+1);
829 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
835 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
836 * @dip: The GFS2 inode
838 * Returns: 0 on success, error code otherwise
841 static int dir_make_exhash(struct inode *inode)
843 struct gfs2_inode *dip = GFS2_I(inode);
844 struct gfs2_sbd *sdp = GFS2_SB(inode);
845 struct gfs2_dirent *dent;
847 struct buffer_head *bh, *dibh;
848 struct gfs2_leaf *leaf;
854 error = gfs2_meta_inode_buffer(dip, &dibh);
858 /* Turn over a new leaf */
860 leaf = new_leaf(inode, &bh, 0);
865 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
866 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
870 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
871 sizeof(struct gfs2_dinode));
873 /* Find last entry */
876 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
877 sizeof(struct gfs2_leaf);
878 args.name = bh->b_data;
879 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
880 gfs2_dirent_last, &args, NULL);
889 return PTR_ERR(dent);
892 /* Adjust the last dirent's record length
893 (Remember that dent still points to the last entry.) */
895 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
896 sizeof(struct gfs2_dinode) -
897 sizeof(struct gfs2_leaf));
901 /* We're done with the new leaf block, now setup the new
904 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
905 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
907 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
909 for (x = sdp->sd_hash_ptrs; x--; lp++)
910 *lp = cpu_to_be64(bn);
912 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
913 dip->i_di.di_blocks++;
914 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
915 dip->i_di.di_payload_format = 0;
917 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
918 dip->i_di.di_depth = y;
920 gfs2_dinode_out(&dip->i_di, dibh->b_data);
928 * dir_split_leaf - Split a leaf block into two
929 * @dip: The GFS2 inode
933 * Returns: 0 on success, error code on failure
936 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
938 struct gfs2_inode *dip = GFS2_I(inode);
939 struct buffer_head *nbh, *obh, *dibh;
940 struct gfs2_leaf *nleaf, *oleaf;
941 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
942 uint32_t start, len, half_len, divider;
943 uint64_t bn, *lp, leaf_no;
948 index = name->hash >> (32 - dip->i_di.di_depth);
949 error = get_leaf_nr(dip, index, &leaf_no);
953 /* Get the old leaf block */
954 error = get_leaf(dip, leaf_no, &obh);
958 oleaf = (struct gfs2_leaf *)obh->b_data;
959 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
961 return 1; /* can't split */
964 gfs2_trans_add_bh(dip->i_gl, obh, 1);
966 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
973 /* Compute the start and len of leaf pointers in the hash table. */
974 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
977 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
978 gfs2_consist_inode(dip);
983 start = (index & ~(len - 1));
985 /* Change the pointers.
986 Don't bother distinguishing stuffed from non-stuffed.
987 This code is complicated enough already. */
988 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
989 /* Change the pointers */
990 for (x = 0; x < half_len; x++)
991 lp[x] = cpu_to_be64(bn);
993 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
994 half_len * sizeof(uint64_t));
995 if (error != half_len * sizeof(uint64_t)) {
1003 /* Compute the divider */
1004 divider = (start + half_len) << (32 - dip->i_di.di_depth);
1006 /* Copy the entries */
1007 dirent_first(dip, obh, &dent);
1011 if (dirent_next(dip, obh, &next))
1014 if (dent->de_inum.no_addr &&
1015 be32_to_cpu(dent->de_hash) < divider) {
1017 str.name = (char*)(dent+1);
1018 str.len = be16_to_cpu(dent->de_name_len);
1019 str.hash = be32_to_cpu(dent->de_hash);
1020 new = gfs2_dirent_alloc(inode, nbh, &str);
1022 error = PTR_ERR(new);
1026 new->de_inum = dent->de_inum; /* No endian worries */
1027 new->de_type = dent->de_type; /* No endian worries */
1028 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
1030 dirent_del(dip, obh, prev, dent);
1032 if (!oleaf->lf_entries)
1033 gfs2_consist_inode(dip);
1034 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
1046 oleaf->lf_depth = nleaf->lf_depth;
1048 error = gfs2_meta_inode_buffer(dip, &dibh);
1049 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1050 dip->i_di.di_blocks++;
1051 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1070 * dir_double_exhash - Double size of ExHash table
1071 * @dip: The GFS2 dinode
1073 * Returns: 0 on success, error code on failure
1076 static int dir_double_exhash(struct gfs2_inode *dip)
1078 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1079 struct buffer_head *dibh;
1082 uint64_t *from, *to;
1087 hsize = 1 << dip->i_di.di_depth;
1088 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1089 gfs2_consist_inode(dip);
1093 /* Allocate both the "from" and "to" buffers in one big chunk */
1095 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1097 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1098 error = gfs2_dir_read_data(dip, (char *)buf,
1099 block * sdp->sd_hash_bsize,
1100 sdp->sd_hash_bsize);
1101 if (error != sdp->sd_hash_bsize) {
1108 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1110 for (x = sdp->sd_hash_ptrs; x--; from++) {
1111 *to++ = *from; /* No endianess worries */
1115 error = gfs2_dir_write_data(dip,
1116 (char *)buf + sdp->sd_hash_bsize,
1117 block * sdp->sd_sb.sb_bsize,
1118 sdp->sd_sb.sb_bsize);
1119 if (error != sdp->sd_sb.sb_bsize) {
1128 error = gfs2_meta_inode_buffer(dip, &dibh);
1129 if (!gfs2_assert_withdraw(sdp, !error)) {
1130 dip->i_di.di_depth++;
1131 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1144 * compare_dents - compare directory entries by hash value
1148 * When comparing the hash entries of @a to @b:
1154 static int compare_dents(const void *a, const void *b)
1156 struct gfs2_dirent *dent_a, *dent_b;
1157 uint32_t hash_a, hash_b;
1160 dent_a = *(struct gfs2_dirent **)a;
1161 hash_a = be32_to_cpu(dent_a->de_hash);
1163 dent_b = *(struct gfs2_dirent **)b;
1164 hash_b = be32_to_cpu(dent_b->de_hash);
1166 if (hash_a > hash_b)
1168 else if (hash_a < hash_b)
1171 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1172 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1176 else if (len_a < len_b)
1179 ret = memcmp((char *)(dent_a + 1),
1180 (char *)(dent_b + 1),
1188 * do_filldir_main - read out directory entries
1189 * @dip: The GFS2 inode
1190 * @offset: The offset in the file to read from
1191 * @opaque: opaque data to pass to filldir
1192 * @filldir: The function to pass entries to
1193 * @darr: an array of struct gfs2_dirent pointers to read
1194 * @entries: the number of entries in darr
1195 * @copied: pointer to int that's non-zero if a entry has been copied out
1197 * Jump through some hoops to make sure that if there are hash collsions,
1198 * they are read out at the beginning of a buffer. We want to minimize
1199 * the possibility that they will fall into different readdir buffers or
1200 * that someone will want to seek to that location.
1202 * Returns: errno, >0 on exception from filldir
1205 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1206 void *opaque, gfs2_filldir_t filldir,
1207 const struct gfs2_dirent **darr, uint32_t entries,
1210 const struct gfs2_dirent *dent, *dent_next;
1211 struct gfs2_inum inum;
1212 uint64_t off, off_next;
1217 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1219 dent_next = darr[0];
1220 off_next = be32_to_cpu(dent_next->de_hash);
1221 off_next = gfs2_disk_hash2offset(off_next);
1223 for (x = 0, y = 1; x < entries; x++, y++) {
1228 dent_next = darr[y];
1229 off_next = be32_to_cpu(dent_next->de_hash);
1230 off_next = gfs2_disk_hash2offset(off_next);
1236 if (off_next == off) {
1237 if (*copied && !run)
1248 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1250 error = filldir(opaque, (char *)(dent + 1),
1251 be16_to_cpu(dent->de_name_len),
1253 be16_to_cpu(dent->de_type));
1260 /* Increment the *offset by one, so the next time we come into the
1261 do_filldir fxn, we get the next entry instead of the last one in the
1269 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1270 gfs2_filldir_t filldir, int *copied,
1271 unsigned *depth, u64 leaf_no)
1273 struct gfs2_inode *ip = GFS2_I(inode);
1274 struct buffer_head *bh;
1275 struct gfs2_leaf *lf;
1276 unsigned entries = 0;
1277 unsigned leaves = 0;
1278 const struct gfs2_dirent **darr, *dent;
1279 struct dirent_gather g;
1280 struct buffer_head **larr;
1286 error = get_leaf(ip, lfn, &bh);
1289 lf = (struct gfs2_leaf *)bh->b_data;
1291 *depth = be16_to_cpu(lf->lf_depth);
1292 entries += be16_to_cpu(lf->lf_entries);
1294 lfn = be64_to_cpu(lf->lf_next);
1302 larr = vmalloc((leaves + entries) * sizeof(void*));
1305 darr = (const struct gfs2_dirent **)(larr + leaves);
1311 error = get_leaf(ip, lfn, &bh);
1314 lf = (struct gfs2_leaf *)bh->b_data;
1315 lfn = be64_to_cpu(lf->lf_next);
1316 if (lf->lf_entries) {
1317 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1318 gfs2_dirent_gather, NULL, &g);
1319 error = PTR_ERR(dent);
1330 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1333 for(i = 0; i < leaf; i++)
1341 * dir_e_read - Reads the entries from a directory into a filldir buffer
1342 * @dip: dinode pointer
1343 * @offset: the hash of the last entry read shifted to the right once
1344 * @opaque: buffer for the filldir function to fill
1345 * @filldir: points to the filldir function to use
1350 static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
1351 gfs2_filldir_t filldir)
1353 struct gfs2_inode *dip = GFS2_I(inode);
1354 struct gfs2_sbd *sdp = GFS2_SB(inode);
1355 uint32_t hsize, len = 0;
1356 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1357 uint32_t hash, index;
1363 hsize = 1 << dip->i_di.di_depth;
1364 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1365 gfs2_consist_inode(dip);
1369 hash = gfs2_dir_offset2hash(*offset);
1370 index = hash >> (32 - dip->i_di.di_depth);
1372 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1376 while (index < hsize) {
1377 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1378 ht_offset = index - lp_offset;
1380 if (ht_offset_cur != ht_offset) {
1381 error = gfs2_dir_read_data(dip, (char *)lp,
1382 ht_offset * sizeof(uint64_t),
1383 sdp->sd_hash_bsize);
1384 if (error != sdp->sd_hash_bsize) {
1389 ht_offset_cur = ht_offset;
1392 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1394 be64_to_cpu(lp[lp_offset]));
1398 len = 1 << (dip->i_di.di_depth - depth);
1399 index = (index & ~(len - 1)) + len;
1409 int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1410 gfs2_filldir_t filldir)
1412 struct gfs2_inode *dip = GFS2_I(inode);
1413 struct dirent_gather g;
1414 const struct gfs2_dirent **darr, *dent;
1415 struct buffer_head *dibh;
1419 if (!dip->i_di.di_entries)
1422 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1423 return dir_e_read(inode, offset, opaque, filldir);
1425 if (!gfs2_is_stuffed(dip)) {
1426 gfs2_consist_inode(dip);
1430 error = gfs2_meta_inode_buffer(dip, &dibh);
1435 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1440 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1441 gfs2_dirent_gather, NULL, &g);
1443 error = PTR_ERR(dent);
1446 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1447 dip->i_di.di_entries, &copied);
1461 * gfs2_dir_search - Search a directory
1462 * @dip: The GFS2 inode
1466 * This routine searches a directory for a file or another directory.
1467 * Assumes a glock is held on dip.
1472 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1473 struct gfs2_inum *inum, unsigned int *type)
1475 struct buffer_head *bh;
1476 struct gfs2_dirent *dent;
1478 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1481 return PTR_ERR(dent);
1483 gfs2_inum_in(inum, (char *)&dent->de_inum);
1485 *type = be16_to_cpu(dent->de_type);
1492 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1494 struct buffer_head *bh, *obh;
1495 struct gfs2_inode *ip = GFS2_I(inode);
1496 struct gfs2_leaf *leaf, *oleaf;
1501 index = name->hash >> (32 - ip->i_di.di_depth);
1502 error = get_first_leaf(ip, index, &obh);
1506 oleaf = (struct gfs2_leaf *)obh->b_data;
1507 bn = be64_to_cpu(oleaf->lf_next);
1511 error = get_leaf(ip, bn, &obh);
1516 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1518 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1523 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1527 error = gfs2_meta_inode_buffer(ip, &bh);
1530 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1531 ip->i_di.di_blocks++;
1532 gfs2_dinode_out(&ip->i_di, bh->b_data);
1538 * gfs2_dir_add - Add new filename into directory
1539 * @dip: The GFS2 inode
1540 * @filename: The new name
1541 * @inode: The inode number of the entry
1542 * @type: The type of the entry
1544 * Returns: 0 on success, error code on failure
1547 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1548 const struct gfs2_inum *inum, unsigned type)
1550 struct gfs2_inode *ip = GFS2_I(inode);
1551 struct buffer_head *bh;
1552 struct gfs2_dirent *dent;
1553 struct gfs2_leaf *leaf;
1557 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1561 return PTR_ERR(dent);
1562 dent = gfs2_init_dirent(inode, dent, name, bh);
1563 gfs2_inum_out(inum, (char *)&dent->de_inum);
1564 dent->de_type = cpu_to_be16(type);
1565 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1566 leaf = (struct gfs2_leaf *)bh->b_data;
1567 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1570 error = gfs2_meta_inode_buffer(ip, &bh);
1573 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1574 ip->i_di.di_entries++;
1575 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1576 gfs2_dinode_out(&ip->i_di, bh->b_data);
1581 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1582 error = dir_make_exhash(inode);
1587 error = dir_split_leaf(inode, name);
1592 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1593 error = dir_double_exhash(ip);
1596 error = dir_split_leaf(inode, name);
1602 error = dir_new_leaf(inode, name);
1613 * gfs2_dir_del - Delete a directory entry
1614 * @dip: The GFS2 inode
1615 * @filename: The filename
1617 * Returns: 0 on success, error code on failure
1620 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1622 struct gfs2_dirent *dent, *prev = NULL;
1623 struct buffer_head *bh;
1626 /* Returns _either_ the entry (if its first in block) or the
1627 previous entry otherwise */
1628 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1630 gfs2_consist_inode(dip);
1634 gfs2_consist_inode(dip);
1635 return PTR_ERR(dent);
1637 /* If not first in block, adjust pointers accordingly */
1638 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1640 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1643 dirent_del(dip, bh, prev, dent);
1644 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1645 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1646 u16 entries = be16_to_cpu(leaf->lf_entries);
1648 gfs2_consist_inode(dip);
1649 leaf->lf_entries = cpu_to_be16(--entries);
1653 error = gfs2_meta_inode_buffer(dip, &bh);
1657 if (!dip->i_di.di_entries)
1658 gfs2_consist_inode(dip);
1659 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1660 dip->i_di.di_entries--;
1661 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1662 gfs2_dinode_out(&dip->i_di, bh->b_data);
1664 mark_inode_dirty(&dip->i_inode);
1670 * gfs2_dir_mvino - Change inode number of directory entry
1671 * @dip: The GFS2 inode
1675 * This routine changes the inode number of a directory entry. It's used
1676 * by rename to change ".." when a directory is moved.
1677 * Assumes a glock is held on dvp.
1682 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1683 struct gfs2_inum *inum, unsigned int new_type)
1685 struct buffer_head *bh;
1686 struct gfs2_dirent *dent;
1689 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1691 gfs2_consist_inode(dip);
1695 return PTR_ERR(dent);
1697 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1698 gfs2_inum_out(inum, (char *)&dent->de_inum);
1699 dent->de_type = cpu_to_be16(new_type);
1701 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1703 error = gfs2_meta_inode_buffer(dip, &bh);
1706 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1709 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1710 gfs2_dinode_out(&dip->i_di, bh->b_data);
1716 * foreach_leaf - call a function for each leaf in a directory
1717 * @dip: the directory
1718 * @lc: the function to call for each each
1719 * @data: private data to pass to it
1724 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1726 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1727 struct buffer_head *bh;
1728 struct gfs2_leaf *leaf;
1729 uint32_t hsize, len;
1730 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1736 hsize = 1 << dip->i_di.di_depth;
1737 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1738 gfs2_consist_inode(dip);
1742 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1746 while (index < hsize) {
1747 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1748 ht_offset = index - lp_offset;
1750 if (ht_offset_cur != ht_offset) {
1751 error = gfs2_dir_read_data(dip, (char *)lp,
1752 ht_offset * sizeof(uint64_t),
1753 sdp->sd_hash_bsize);
1754 if (error != sdp->sd_hash_bsize) {
1759 ht_offset_cur = ht_offset;
1762 leaf_no = be64_to_cpu(lp[lp_offset]);
1764 error = get_leaf(dip, leaf_no, &bh);
1767 leaf = (struct gfs2_leaf *)bh->b_data;
1768 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1771 error = lc(dip, index, len, leaf_no, data);
1775 index = (index & ~(len - 1)) + len;
1780 if (index != hsize) {
1781 gfs2_consist_inode(dip);
1792 * leaf_dealloc - Deallocate a directory leaf
1793 * @dip: the directory
1794 * @index: the hash table offset in the directory
1795 * @len: the number of pointers to this leaf
1796 * @leaf_no: the leaf number
1802 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1803 uint64_t leaf_no, void *data)
1805 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1806 struct gfs2_leaf *tmp_leaf;
1807 struct gfs2_rgrp_list rlist;
1808 struct buffer_head *bh, *dibh;
1810 unsigned int rg_blocks = 0, l_blocks = 0;
1812 unsigned int x, size = len * sizeof(uint64_t);
1815 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1817 ht = kzalloc(size, GFP_KERNEL);
1821 gfs2_alloc_get(dip);
1823 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1827 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1831 /* Count the number of leaves */
1833 for (blk = leaf_no; blk; blk = nblk) {
1834 error = get_leaf(dip, blk, &bh);
1837 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1838 nblk = be64_to_cpu(tmp_leaf->lf_next);
1841 gfs2_rlist_add(sdp, &rlist, blk);
1845 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1847 for (x = 0; x < rlist.rl_rgrps; x++) {
1848 struct gfs2_rgrpd *rgd;
1849 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1850 rg_blocks += rgd->rd_ri.ri_length;
1853 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1857 error = gfs2_trans_begin(sdp,
1858 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1859 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1861 goto out_rg_gunlock;
1863 for (blk = leaf_no; blk; blk = nblk) {
1864 error = get_leaf(dip, blk, &bh);
1867 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1868 nblk = be64_to_cpu(tmp_leaf->lf_next);
1871 gfs2_free_meta(dip, blk, 1);
1873 if (!dip->i_di.di_blocks)
1874 gfs2_consist_inode(dip);
1875 dip->i_di.di_blocks--;
1878 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
1879 if (error != size) {
1885 error = gfs2_meta_inode_buffer(dip, &dibh);
1889 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1890 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1894 gfs2_trans_end(sdp);
1897 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1900 gfs2_rlist_free(&rlist);
1901 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1904 gfs2_quota_unhold(dip);
1907 gfs2_alloc_put(dip);
1914 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1915 * @dip: the directory
1917 * Dealloc all on-disk directory leaves to FREEMETA state
1918 * Change on-disk inode type to "regular file"
1923 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1925 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1926 struct buffer_head *bh;
1929 /* Dealloc on-disk leaves to FREEMETA state */
1930 error = foreach_leaf(dip, leaf_dealloc, NULL);
1934 /* Make this a regular file in case we crash.
1935 (We don't want to free these blocks a second time.) */
1937 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1941 error = gfs2_meta_inode_buffer(dip, &bh);
1943 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1944 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1945 cpu_to_be32(S_IFREG);
1949 gfs2_trans_end(sdp);
1955 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1956 * @ip: the file being written to
1957 * @filname: the filename that's going to be added
1959 * Returns: 1 if alloc required, 0 if not, -ve on error
1962 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
1964 struct gfs2_dirent *dent;
1965 struct buffer_head *bh;
1967 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1972 return PTR_ERR(dent);