2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 block
46 * pointer in the array that points to the same leaf. In fact, when a directory
47 * is first converted from linear to exhash, all of the pointers point to the
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/completion.h>
60 #include <linux/buffer_head.h>
61 #include <linux/sort.h>
62 #include <linux/gfs2_ondisk.h>
63 #include <linux/crc32.h>
64 #include <linux/vmalloc.h>
65 #include <asm/semaphore.h>
68 #include "lm_interface.h"
80 #define IS_LEAF 1 /* Hashed (leaf) directory */
81 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
84 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
85 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
87 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
88 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
91 typedef int (*leaf_call_t) (struct gfs2_inode *dip,
92 uint32_t index, uint32_t len, uint64_t leaf_no,
95 int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
96 struct buffer_head **bhp)
98 struct buffer_head *bh;
102 bh = gfs2_meta_new(ip->i_gl, block);
103 gfs2_trans_add_bh(ip->i_gl, bh, 1);
104 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
105 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
107 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT,
111 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
123 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
124 unsigned int offset, unsigned int size)
127 struct buffer_head *dibh;
130 error = gfs2_meta_inode_buffer(ip, &dibh);
134 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
135 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
136 if (ip->i_di.di_size < offset + size)
137 ip->i_di.di_size = offset + size;
138 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
139 gfs2_dinode_out(&ip->i_di, dibh->b_data);
149 * gfs2_dir_write_data - Write directory information to the inode
150 * @ip: The GFS2 inode
151 * @buf: The buffer containing information to be written
152 * @offset: The file offset to start writing at
153 * @size: The amount of data to write
155 * Returns: The number of bytes correctly written or error code
157 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
158 uint64_t offset, unsigned int size)
160 struct gfs2_sbd *sdp = ip->i_sbd;
161 struct buffer_head *dibh;
162 uint64_t lblock, dblock;
171 if (gfs2_is_stuffed(ip) &&
172 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
173 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
176 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
179 if (gfs2_is_stuffed(ip)) {
180 error = gfs2_unstuff_dinode(ip, NULL, NULL);
186 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
188 while (copied < size) {
190 struct buffer_head *bh;
193 amount = size - copied;
194 if (amount > sdp->sd_sb.sb_bsize - o)
195 amount = sdp->sd_sb.sb_bsize - o;
199 error = gfs2_block_map(ip, lblock, &new, &dblock,
204 if (gfs2_assert_withdraw(sdp, dblock))
208 error = gfs2_dir_get_buffer(ip, dblock,
209 (amount == sdp->sd_jbsize) ?
214 gfs2_trans_add_bh(ip->i_gl, bh, 1);
215 memcpy(bh->b_data + o, buf, amount);
225 o = sizeof(struct gfs2_meta_header);
229 error = gfs2_meta_inode_buffer(ip, &dibh);
233 if (ip->i_di.di_size < offset + copied)
234 ip->i_di.di_size = offset + copied;
235 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
237 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
238 gfs2_dinode_out(&ip->i_di, dibh->b_data);
248 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
249 unsigned int offset, unsigned int size)
251 struct buffer_head *dibh;
254 error = gfs2_meta_inode_buffer(ip, &dibh);
256 offset += sizeof(struct gfs2_dinode);
257 memcpy(buf, dibh->b_data + offset, size);
261 return (error) ? error : size;
266 * gfs2_dir_read_data - Read a data from a directory inode
267 * @ip: The GFS2 Inode
268 * @buf: The buffer to place result into
269 * @offset: File offset to begin jdata_readng from
270 * @size: Amount of data to transfer
272 * Returns: The amount of data actually copied or the error
274 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
275 uint64_t offset, unsigned int size)
277 struct gfs2_sbd *sdp = ip->i_sbd;
278 uint64_t lblock, dblock;
284 if (offset >= ip->i_di.di_size)
287 if ((offset + size) > ip->i_di.di_size)
288 size = ip->i_di.di_size - offset;
293 if (gfs2_is_stuffed(ip))
294 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
297 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
301 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
303 while (copied < size) {
305 struct buffer_head *bh;
308 amount = size - copied;
309 if (amount > sdp->sd_sb.sb_bsize - o)
310 amount = sdp->sd_sb.sb_bsize - o;
314 error = gfs2_block_map(ip, lblock, &new, &dblock,
321 gfs2_meta_ra(ip->i_gl, dblock, extlen);
324 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
332 memcpy(buf, bh->b_data + o, amount);
340 o = sizeof(struct gfs2_meta_header);
345 return (copied) ? copied : error;
348 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
349 const struct qstr *name,
352 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
353 const struct qstr *name, int ret)
355 if (dent->de_inum.no_addr != 0 &&
356 be32_to_cpu(dent->de_hash) == name->hash &&
357 be16_to_cpu(dent->de_name_len) == name->len &&
358 memcmp((char *)(dent+1), name->name, name->len) == 0)
363 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
364 const struct qstr *name,
367 return __gfs2_dirent_find(dent, name, 1);
370 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
371 const struct qstr *name,
374 return __gfs2_dirent_find(dent, name, 2);
378 * name->name holds ptr to start of block.
379 * name->len holds size of block.
381 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
382 const struct qstr *name,
385 const char *start = name->name;
386 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
387 if (name->len == (end - start))
392 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
393 const struct qstr *name,
396 unsigned required = GFS2_DIRENT_SIZE(name->len);
397 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
398 unsigned totlen = be16_to_cpu(dent->de_rec_len);
400 if (!dent->de_inum.no_addr)
401 actual = GFS2_DIRENT_SIZE(0);
402 if ((totlen - actual) >= required)
407 struct dirent_gather {
408 const struct gfs2_dirent **pdent;
412 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
413 const struct qstr *name,
416 struct dirent_gather *g = opaque;
417 if (dent->de_inum.no_addr) {
418 g->pdent[g->offset++] = dent;
424 * Other possible things to check:
425 * - Inode located within filesystem size (and on valid block)
426 * - Valid directory entry type
427 * Not sure how heavy-weight we want to make this... could also check
428 * hash is correct for example, but that would take a lot of extra time.
429 * For now the most important thing is to check that the various sizes
432 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
433 unsigned int size, unsigned int len, int first)
435 const char *msg = "gfs2_dirent too small";
436 if (unlikely(size < sizeof(struct gfs2_dirent)))
438 msg = "gfs2_dirent misaligned";
439 if (unlikely(offset & 0x7))
441 msg = "gfs2_dirent points beyond end of block";
442 if (unlikely(offset + size > len))
444 msg = "zero inode number";
445 if (unlikely(!first && !dent->de_inum.no_addr))
447 msg = "name length is greater than space in dirent";
448 if (dent->de_inum.no_addr &&
449 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
454 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
455 first ? "first in block" : "not first in block");
459 static int gfs2_dirent_offset(const void *buf)
461 const struct gfs2_meta_header *h = buf;
466 switch(be32_to_cpu(h->mh_type)) {
467 case GFS2_METATYPE_LF:
468 offset = sizeof(struct gfs2_leaf);
470 case GFS2_METATYPE_DI:
471 offset = sizeof(struct gfs2_dinode);
478 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
479 be32_to_cpu(h->mh_type));
483 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
485 unsigned int len, gfs2_dscan_t scan,
486 const struct qstr *name,
489 struct gfs2_dirent *dent, *prev;
494 ret = gfs2_dirent_offset(buf);
500 dent = (struct gfs2_dirent *)(buf + offset);
501 size = be16_to_cpu(dent->de_rec_len);
502 if (gfs2_check_dirent(dent, offset, size, len, 1))
505 ret = scan(dent, name, opaque);
512 dent = (struct gfs2_dirent *)(buf + offset);
513 size = be16_to_cpu(dent->de_rec_len);
514 if (gfs2_check_dirent(dent, offset, size, len, 0))
524 return prev ? prev : dent;
531 gfs2_consist_inode(inode->u.generic_ip);
532 return ERR_PTR(-EIO);
537 * dirent_first - Return the first dirent
538 * @dip: the directory
540 * @dent: Pointer to list of dirents
542 * return first dirent whether bh points to leaf or stuffed dinode
544 * Returns: IS_LEAF, IS_DINODE, or -errno
547 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
548 struct gfs2_dirent **dent)
550 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
552 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
553 if (gfs2_meta_check(dip->i_sbd, bh))
555 *dent = (struct gfs2_dirent *)(bh->b_data +
556 sizeof(struct gfs2_leaf));
559 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
561 *dent = (struct gfs2_dirent *)(bh->b_data +
562 sizeof(struct gfs2_dinode));
568 * dirent_next - Next dirent
569 * @dip: the directory
571 * @dent: Pointer to list of dirents
573 * Returns: 0 on success, error code otherwise
576 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
577 struct gfs2_dirent **dent)
579 struct gfs2_dirent *tmp, *cur;
581 uint16_t cur_rec_len;
584 bh_end = bh->b_data + bh->b_size;
585 cur_rec_len = be16_to_cpu(cur->de_rec_len);
587 if ((char *)cur + cur_rec_len >= bh_end) {
588 if ((char *)cur + cur_rec_len > bh_end) {
589 gfs2_consist_inode(dip);
595 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
597 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
598 gfs2_consist_inode(dip);
602 if (cur_rec_len == 0) {
603 gfs2_consist_inode(dip);
607 /* Only the first dent could ever have de_inum.no_addr == 0 */
608 if (!tmp->de_inum.no_addr) {
609 gfs2_consist_inode(dip);
619 * dirent_del - Delete a dirent
620 * @dip: The GFS2 inode
622 * @prev: The previous dirent
623 * @cur: The current dirent
627 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
628 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
630 uint16_t cur_rec_len, prev_rec_len;
632 if (!cur->de_inum.no_addr) {
633 gfs2_consist_inode(dip);
637 gfs2_trans_add_bh(dip->i_gl, bh, 1);
639 /* If there is no prev entry, this is the first entry in the block.
640 The de_rec_len is already as big as it needs to be. Just zero
641 out the inode number and return. */
644 cur->de_inum.no_addr = 0; /* No endianess worries */
648 /* Combine this dentry with the previous one. */
650 prev_rec_len = be16_to_cpu(prev->de_rec_len);
651 cur_rec_len = be16_to_cpu(cur->de_rec_len);
653 if ((char *)prev + prev_rec_len != (char *)cur)
654 gfs2_consist_inode(dip);
655 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
656 gfs2_consist_inode(dip);
658 prev_rec_len += cur_rec_len;
659 prev->de_rec_len = cpu_to_be16(prev_rec_len);
663 * Takes a dent from which to grab space as an argument. Returns the
664 * newly created dent.
666 struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
667 struct gfs2_dirent *dent,
668 const struct qstr *name,
669 struct buffer_head *bh)
671 struct gfs2_inode *ip = inode->u.generic_ip;
672 struct gfs2_dirent *ndent;
673 unsigned offset = 0, totlen;
675 if (dent->de_inum.no_addr)
676 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
677 totlen = be16_to_cpu(dent->de_rec_len);
678 BUG_ON(offset + name->len > totlen);
679 gfs2_trans_add_bh(ip->i_gl, bh, 1);
680 ndent = (struct gfs2_dirent *)((char *)dent + offset);
681 dent->de_rec_len = cpu_to_be16(offset);
682 gfs2_qstr2dirent(name, totlen - offset, ndent);
686 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
687 struct buffer_head *bh,
688 const struct qstr *name)
690 struct gfs2_dirent *dent;
691 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
692 gfs2_dirent_find_space, name, NULL);
693 if (!dent || IS_ERR(dent))
695 return gfs2_init_dirent(inode, dent, name, bh);
698 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
699 struct buffer_head **bhp)
703 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
704 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
711 * get_leaf_nr - Get a leaf number associated with the index
712 * @dip: The GFS2 inode
716 * Returns: 0 on success, error code otherwise
719 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
725 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
726 index * sizeof(uint64_t),
728 if (error != sizeof(uint64_t))
729 return (error < 0) ? error : -EIO;
731 *leaf_out = be64_to_cpu(leaf_no);
736 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
737 struct buffer_head **bh_out)
742 error = get_leaf_nr(dip, index, &leaf_no);
744 error = get_leaf(dip, leaf_no, bh_out);
749 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
750 const struct qstr *name,
752 struct buffer_head **pbh)
754 struct buffer_head *bh;
755 struct gfs2_dirent *dent;
756 struct gfs2_inode *ip = inode->u.generic_ip;
759 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
760 struct gfs2_leaf *leaf;
761 unsigned hsize = 1 << ip->i_di.di_depth;
764 if (hsize * sizeof(u64) != ip->i_di.di_size) {
765 gfs2_consist_inode(ip);
766 return ERR_PTR(-EIO);
769 index = name->hash >> (32 - ip->i_di.di_depth);
770 error = get_first_leaf(ip, index, &bh);
772 return ERR_PTR(error);
774 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
778 leaf = (struct gfs2_leaf *)bh->b_data;
779 ln = be64_to_cpu(leaf->lf_next);
783 error = get_leaf(ip, ln, &bh);
786 return error ? ERR_PTR(error) : NULL;
789 error = gfs2_meta_inode_buffer(ip, &bh);
791 return ERR_PTR(error);
792 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
794 if (unlikely(dent == NULL || IS_ERR(dent))) {
802 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
804 struct gfs2_inode *ip = inode->u.generic_ip;
805 u64 bn = gfs2_alloc_meta(ip);
806 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
807 struct gfs2_leaf *leaf;
808 struct gfs2_dirent *dent;
809 struct qstr name = { .name = "", .len = 0, .hash = 0 };
812 gfs2_trans_add_bh(ip->i_gl, bh, 1);
813 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
814 leaf = (struct gfs2_leaf *)bh->b_data;
815 leaf->lf_depth = cpu_to_be16(depth);
816 leaf->lf_entries = cpu_to_be16(0);
817 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
818 leaf->lf_next = cpu_to_be64(0);
819 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
820 dent = (struct gfs2_dirent *)(leaf+1);
821 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
827 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
828 * @dip: The GFS2 inode
830 * Returns: 0 on success, error code otherwise
833 static int dir_make_exhash(struct inode *inode)
835 struct gfs2_inode *dip = inode->u.generic_ip;
836 struct gfs2_sbd *sdp = dip->i_sbd;
837 struct gfs2_dirent *dent;
839 struct buffer_head *bh, *dibh;
840 struct gfs2_leaf *leaf;
846 error = gfs2_meta_inode_buffer(dip, &dibh);
850 /* Turn over a new leaf */
852 leaf = new_leaf(inode, &bh, 0);
857 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
858 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
862 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
863 sizeof(struct gfs2_dinode));
865 /* Find last entry */
868 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
869 sizeof(struct gfs2_leaf);
870 args.name = bh->b_data;
871 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
872 gfs2_dirent_last, &args, NULL);
881 return PTR_ERR(dent);
884 /* Adjust the last dirent's record length
885 (Remember that dent still points to the last entry.) */
887 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
888 sizeof(struct gfs2_dinode) -
889 sizeof(struct gfs2_leaf));
893 /* We're done with the new leaf block, now setup the new
896 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
897 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
899 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
901 for (x = sdp->sd_hash_ptrs; x--; lp++)
902 *lp = cpu_to_be64(bn);
904 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
905 dip->i_di.di_blocks++;
906 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
907 dip->i_di.di_payload_format = 0;
909 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
910 dip->i_di.di_depth = y;
912 gfs2_dinode_out(&dip->i_di, dibh->b_data);
920 * dir_split_leaf - Split a leaf block into two
921 * @dip: The GFS2 inode
925 * Returns: 0 on success, error code on failure
928 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
930 struct gfs2_inode *dip = inode->u.generic_ip;
931 struct buffer_head *nbh, *obh, *dibh;
932 struct gfs2_leaf *nleaf, *oleaf;
933 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
934 uint32_t start, len, half_len, divider;
935 uint64_t bn, *lp, leaf_no;
940 index = name->hash >> (32 - dip->i_di.di_depth);
941 error = get_leaf_nr(dip, index, &leaf_no);
945 /* Get the old leaf block */
946 error = get_leaf(dip, leaf_no, &obh);
950 oleaf = (struct gfs2_leaf *)obh->b_data;
951 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
953 return 1; /* can't split */
956 gfs2_trans_add_bh(dip->i_gl, obh, 1);
958 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
965 /* Compute the start and len of leaf pointers in the hash table. */
966 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
969 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
970 gfs2_consist_inode(dip);
975 start = (index & ~(len - 1));
977 /* Change the pointers.
978 Don't bother distinguishing stuffed from non-stuffed.
979 This code is complicated enough already. */
980 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
981 /* Change the pointers */
982 for (x = 0; x < half_len; x++)
983 lp[x] = cpu_to_be64(bn);
985 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
986 half_len * sizeof(uint64_t));
987 if (error != half_len * sizeof(uint64_t)) {
995 /* Compute the divider */
996 divider = (start + half_len) << (32 - dip->i_di.di_depth);
998 /* Copy the entries */
999 dirent_first(dip, obh, &dent);
1003 if (dirent_next(dip, obh, &next))
1006 if (dent->de_inum.no_addr &&
1007 be32_to_cpu(dent->de_hash) < divider) {
1009 str.name = (char*)(dent+1);
1010 str.len = be16_to_cpu(dent->de_name_len);
1011 str.hash = be32_to_cpu(dent->de_hash);
1012 new = gfs2_dirent_alloc(inode, nbh, &str);
1014 error = PTR_ERR(new);
1018 new->de_inum = dent->de_inum; /* No endian worries */
1019 new->de_type = dent->de_type; /* No endian worries */
1020 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
1022 dirent_del(dip, obh, prev, dent);
1024 if (!oleaf->lf_entries)
1025 gfs2_consist_inode(dip);
1026 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
1038 oleaf->lf_depth = nleaf->lf_depth;
1040 error = gfs2_meta_inode_buffer(dip, &dibh);
1041 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
1042 dip->i_di.di_blocks++;
1043 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1062 * dir_double_exhash - Double size of ExHash table
1063 * @dip: The GFS2 dinode
1065 * Returns: 0 on success, error code on failure
1068 static int dir_double_exhash(struct gfs2_inode *dip)
1070 struct gfs2_sbd *sdp = dip->i_sbd;
1071 struct buffer_head *dibh;
1074 uint64_t *from, *to;
1079 hsize = 1 << dip->i_di.di_depth;
1080 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1081 gfs2_consist_inode(dip);
1085 /* Allocate both the "from" and "to" buffers in one big chunk */
1087 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1089 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1090 error = gfs2_dir_read_data(dip, (char *)buf,
1091 block * sdp->sd_hash_bsize,
1092 sdp->sd_hash_bsize);
1093 if (error != sdp->sd_hash_bsize) {
1100 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1102 for (x = sdp->sd_hash_ptrs; x--; from++) {
1103 *to++ = *from; /* No endianess worries */
1107 error = gfs2_dir_write_data(dip,
1108 (char *)buf + sdp->sd_hash_bsize,
1109 block * sdp->sd_sb.sb_bsize,
1110 sdp->sd_sb.sb_bsize);
1111 if (error != sdp->sd_sb.sb_bsize) {
1120 error = gfs2_meta_inode_buffer(dip, &dibh);
1121 if (!gfs2_assert_withdraw(sdp, !error)) {
1122 dip->i_di.di_depth++;
1123 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1136 * compare_dents - compare directory entries by hash value
1140 * When comparing the hash entries of @a to @b:
1146 static int compare_dents(const void *a, const void *b)
1148 struct gfs2_dirent *dent_a, *dent_b;
1149 uint32_t hash_a, hash_b;
1152 dent_a = *(struct gfs2_dirent **)a;
1153 hash_a = be32_to_cpu(dent_a->de_hash);
1155 dent_b = *(struct gfs2_dirent **)b;
1156 hash_b = be32_to_cpu(dent_b->de_hash);
1158 if (hash_a > hash_b)
1160 else if (hash_a < hash_b)
1163 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1164 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1168 else if (len_a < len_b)
1171 ret = memcmp((char *)(dent_a + 1),
1172 (char *)(dent_b + 1),
1180 * do_filldir_main - read out directory entries
1181 * @dip: The GFS2 inode
1182 * @offset: The offset in the file to read from
1183 * @opaque: opaque data to pass to filldir
1184 * @filldir: The function to pass entries to
1185 * @darr: an array of struct gfs2_dirent pointers to read
1186 * @entries: the number of entries in darr
1187 * @copied: pointer to int that's non-zero if a entry has been copied out
1189 * Jump through some hoops to make sure that if there are hash collsions,
1190 * they are read out at the beginning of a buffer. We want to minimize
1191 * the possibility that they will fall into different readdir buffers or
1192 * that someone will want to seek to that location.
1194 * Returns: errno, >0 on exception from filldir
1197 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1198 void *opaque, gfs2_filldir_t filldir,
1199 const struct gfs2_dirent **darr, uint32_t entries,
1202 const struct gfs2_dirent *dent, *dent_next;
1203 struct gfs2_inum inum;
1204 uint64_t off, off_next;
1209 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1211 dent_next = darr[0];
1212 off_next = be32_to_cpu(dent_next->de_hash);
1213 off_next = gfs2_disk_hash2offset(off_next);
1215 for (x = 0, y = 1; x < entries; x++, y++) {
1220 dent_next = darr[y];
1221 off_next = be32_to_cpu(dent_next->de_hash);
1222 off_next = gfs2_disk_hash2offset(off_next);
1228 if (off_next == off) {
1229 if (*copied && !run)
1240 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1242 error = filldir(opaque, (char *)(dent + 1),
1243 be16_to_cpu(dent->de_name_len),
1245 be16_to_cpu(dent->de_type));
1252 /* Increment the *offset by one, so the next time we come into the
1253 do_filldir fxn, we get the next entry instead of the last one in the
1261 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1262 gfs2_filldir_t filldir, int *copied,
1263 unsigned *depth, u64 leaf_no)
1265 struct gfs2_inode *ip = inode->u.generic_ip;
1266 struct buffer_head *bh;
1267 struct gfs2_leaf *lf;
1268 unsigned entries = 0;
1269 unsigned leaves = 0;
1270 const struct gfs2_dirent **darr, *dent;
1271 struct dirent_gather g;
1272 struct buffer_head **larr;
1278 error = get_leaf(ip, lfn, &bh);
1281 lf = (struct gfs2_leaf *)bh->b_data;
1283 *depth = be16_to_cpu(lf->lf_depth);
1284 entries += be16_to_cpu(lf->lf_entries);
1286 lfn = be64_to_cpu(lf->lf_next);
1294 larr = vmalloc((leaves + entries) * sizeof(void*));
1297 darr = (const struct gfs2_dirent **)(larr + leaves);
1303 error = get_leaf(ip, lfn, &bh);
1306 lf = (struct gfs2_leaf *)bh->b_data;
1307 lfn = be64_to_cpu(lf->lf_next);
1308 if (lf->lf_entries) {
1309 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1310 gfs2_dirent_gather, NULL, &g);
1311 error = PTR_ERR(dent);
1322 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1325 for(i = 0; i < leaf; i++)
1333 * dir_e_read - Reads the entries from a directory into a filldir buffer
1334 * @dip: dinode pointer
1335 * @offset: the hash of the last entry read shifted to the right once
1336 * @opaque: buffer for the filldir function to fill
1337 * @filldir: points to the filldir function to use
1342 static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
1343 gfs2_filldir_t filldir)
1345 struct gfs2_inode *dip = inode->u.generic_ip;
1346 struct gfs2_sbd *sdp = dip->i_sbd;
1347 uint32_t hsize, len = 0;
1348 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1349 uint32_t hash, index;
1355 hsize = 1 << dip->i_di.di_depth;
1356 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1357 gfs2_consist_inode(dip);
1361 hash = gfs2_dir_offset2hash(*offset);
1362 index = hash >> (32 - dip->i_di.di_depth);
1364 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1368 while (index < hsize) {
1369 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1370 ht_offset = index - lp_offset;
1372 if (ht_offset_cur != ht_offset) {
1373 error = gfs2_dir_read_data(dip, (char *)lp,
1374 ht_offset * sizeof(uint64_t),
1375 sdp->sd_hash_bsize);
1376 if (error != sdp->sd_hash_bsize) {
1381 ht_offset_cur = ht_offset;
1384 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1386 be64_to_cpu(lp[lp_offset]));
1390 len = 1 << (dip->i_di.di_depth - depth);
1391 index = (index & ~(len - 1)) + len;
1401 int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1402 gfs2_filldir_t filldir)
1404 struct gfs2_inode *dip = inode->u.generic_ip;
1405 struct dirent_gather g;
1406 const struct gfs2_dirent **darr, *dent;
1407 struct buffer_head *dibh;
1411 if (!dip->i_di.di_entries)
1414 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1415 return dir_e_read(inode, offset, opaque, filldir);
1417 if (!gfs2_is_stuffed(dip)) {
1418 gfs2_consist_inode(dip);
1422 error = gfs2_meta_inode_buffer(dip, &dibh);
1427 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1432 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1433 gfs2_dirent_gather, NULL, &g);
1435 error = PTR_ERR(dent);
1438 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1439 dip->i_di.di_entries, &copied);
1453 * gfs2_dir_search - Search a directory
1454 * @dip: The GFS2 inode
1458 * This routine searches a directory for a file or another directory.
1459 * Assumes a glock is held on dip.
1464 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1465 struct gfs2_inum *inum, unsigned int *type)
1467 struct buffer_head *bh;
1468 struct gfs2_dirent *dent;
1470 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1473 return PTR_ERR(dent);
1475 gfs2_inum_in(inum, (char *)&dent->de_inum);
1477 *type = be16_to_cpu(dent->de_type);
1484 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1486 struct buffer_head *bh, *obh;
1487 struct gfs2_inode *ip = inode->u.generic_ip;
1488 struct gfs2_leaf *leaf, *oleaf;
1493 index = name->hash >> (32 - ip->i_di.di_depth);
1494 error = get_first_leaf(ip, index, &obh);
1498 oleaf = (struct gfs2_leaf *)obh->b_data;
1499 bn = be64_to_cpu(oleaf->lf_next);
1503 error = get_leaf(ip, bn, &obh);
1508 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1510 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1515 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1519 error = gfs2_meta_inode_buffer(ip, &bh);
1522 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1523 ip->i_di.di_blocks++;
1524 gfs2_dinode_out(&ip->i_di, bh->b_data);
1530 * gfs2_dir_add - Add new filename into directory
1531 * @dip: The GFS2 inode
1532 * @filename: The new name
1533 * @inode: The inode number of the entry
1534 * @type: The type of the entry
1536 * Returns: 0 on success, error code on failure
1539 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1540 const struct gfs2_inum *inum, unsigned type)
1542 struct gfs2_inode *ip = inode->u.generic_ip;
1543 struct buffer_head *bh;
1544 struct gfs2_dirent *dent;
1545 struct gfs2_leaf *leaf;
1549 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1553 return PTR_ERR(dent);
1554 dent = gfs2_init_dirent(inode, dent, name, bh);
1555 gfs2_inum_out(inum, (char *)&dent->de_inum);
1556 dent->de_type = cpu_to_be16(type);
1557 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1558 leaf = (struct gfs2_leaf *)bh->b_data;
1559 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1562 error = gfs2_meta_inode_buffer(ip, &bh);
1565 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1566 ip->i_di.di_entries++;
1567 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1568 gfs2_dinode_out(&ip->i_di, bh->b_data);
1573 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1574 error = dir_make_exhash(inode);
1579 error = dir_split_leaf(inode, name);
1584 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1585 error = dir_double_exhash(ip);
1588 error = dir_split_leaf(inode, name);
1594 error = dir_new_leaf(inode, name);
1605 * gfs2_dir_del - Delete a directory entry
1606 * @dip: The GFS2 inode
1607 * @filename: The filename
1609 * Returns: 0 on success, error code on failure
1612 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1614 struct gfs2_dirent *dent, *prev = NULL;
1615 struct buffer_head *bh;
1618 /* Returns _either_ the entry (if its first in block) or the
1619 previous entry otherwise */
1620 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
1622 gfs2_consist_inode(dip);
1626 gfs2_consist_inode(dip);
1627 return PTR_ERR(dent);
1629 /* If not first in block, adjust pointers accordingly */
1630 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1632 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1635 dirent_del(dip, bh, prev, dent);
1636 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1637 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1638 u16 entries = be16_to_cpu(leaf->lf_entries);
1640 gfs2_consist_inode(dip);
1641 leaf->lf_entries = cpu_to_be16(--entries);
1645 error = gfs2_meta_inode_buffer(dip, &bh);
1649 if (!dip->i_di.di_entries)
1650 gfs2_consist_inode(dip);
1651 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1652 dip->i_di.di_entries--;
1653 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1654 gfs2_dinode_out(&dip->i_di, bh->b_data);
1661 * gfs2_dir_mvino - Change inode number of directory entry
1662 * @dip: The GFS2 inode
1666 * This routine changes the inode number of a directory entry. It's used
1667 * by rename to change ".." when a directory is moved.
1668 * Assumes a glock is held on dvp.
1673 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1674 struct gfs2_inum *inum, unsigned int new_type)
1676 struct buffer_head *bh;
1677 struct gfs2_dirent *dent;
1680 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
1682 gfs2_consist_inode(dip);
1686 return PTR_ERR(dent);
1688 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1689 gfs2_inum_out(inum, (char *)&dent->de_inum);
1690 dent->de_type = cpu_to_be16(new_type);
1692 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1694 error = gfs2_meta_inode_buffer(dip, &bh);
1697 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1700 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1701 gfs2_dinode_out(&dip->i_di, bh->b_data);
1707 * foreach_leaf - call a function for each leaf in a directory
1708 * @dip: the directory
1709 * @lc: the function to call for each each
1710 * @data: private data to pass to it
1715 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1717 struct gfs2_sbd *sdp = dip->i_sbd;
1718 struct buffer_head *bh;
1719 struct gfs2_leaf *leaf;
1720 uint32_t hsize, len;
1721 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1727 hsize = 1 << dip->i_di.di_depth;
1728 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1729 gfs2_consist_inode(dip);
1733 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1737 while (index < hsize) {
1738 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1739 ht_offset = index - lp_offset;
1741 if (ht_offset_cur != ht_offset) {
1742 error = gfs2_dir_read_data(dip, (char *)lp,
1743 ht_offset * sizeof(uint64_t),
1744 sdp->sd_hash_bsize);
1745 if (error != sdp->sd_hash_bsize) {
1750 ht_offset_cur = ht_offset;
1753 leaf_no = be64_to_cpu(lp[lp_offset]);
1755 error = get_leaf(dip, leaf_no, &bh);
1758 leaf = (struct gfs2_leaf *)bh->b_data;
1761 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1763 error = lc(dip, index, len, leaf_no, data);
1767 index = (index & ~(len - 1)) + len;
1772 if (index != hsize) {
1773 gfs2_consist_inode(dip);
1784 * leaf_dealloc - Deallocate a directory leaf
1785 * @dip: the directory
1786 * @index: the hash table offset in the directory
1787 * @len: the number of pointers to this leaf
1788 * @leaf_no: the leaf number
1794 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1795 uint64_t leaf_no, void *data)
1797 struct gfs2_sbd *sdp = dip->i_sbd;
1798 struct gfs2_leaf *tmp_leaf;
1799 struct gfs2_rgrp_list rlist;
1800 struct buffer_head *bh, *dibh;
1802 unsigned int rg_blocks = 0, l_blocks = 0;
1804 unsigned int x, size = len * sizeof(uint64_t);
1807 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1809 ht = kzalloc(size, GFP_KERNEL);
1813 gfs2_alloc_get(dip);
1815 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1819 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1823 /* Count the number of leaves */
1825 for (blk = leaf_no; blk; blk = nblk) {
1826 error = get_leaf(dip, blk, &bh);
1829 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1830 nblk = be64_to_cpu(tmp_leaf->lf_next);
1833 gfs2_rlist_add(sdp, &rlist, blk);
1837 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1839 for (x = 0; x < rlist.rl_rgrps; x++) {
1840 struct gfs2_rgrpd *rgd;
1841 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1842 rg_blocks += rgd->rd_ri.ri_length;
1845 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1849 error = gfs2_trans_begin(sdp,
1850 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1851 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1853 goto out_rg_gunlock;
1855 for (blk = leaf_no; blk; blk = nblk) {
1856 error = get_leaf(dip, blk, &bh);
1859 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1860 nblk = be64_to_cpu(tmp_leaf->lf_next);
1863 gfs2_free_meta(dip, blk, 1);
1865 if (!dip->i_di.di_blocks)
1866 gfs2_consist_inode(dip);
1867 dip->i_di.di_blocks--;
1870 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
1871 if (error != size) {
1877 error = gfs2_meta_inode_buffer(dip, &dibh);
1881 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1882 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1886 gfs2_trans_end(sdp);
1889 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1892 gfs2_rlist_free(&rlist);
1893 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1896 gfs2_quota_unhold(dip);
1899 gfs2_alloc_put(dip);
1906 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1907 * @dip: the directory
1909 * Dealloc all on-disk directory leaves to FREEMETA state
1910 * Change on-disk inode type to "regular file"
1915 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1917 struct gfs2_sbd *sdp = dip->i_sbd;
1918 struct buffer_head *bh;
1921 /* Dealloc on-disk leaves to FREEMETA state */
1922 error = foreach_leaf(dip, leaf_dealloc, NULL);
1926 /* Make this a regular file in case we crash.
1927 (We don't want to free these blocks a second time.) */
1929 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1933 error = gfs2_meta_inode_buffer(dip, &bh);
1935 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1936 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1937 cpu_to_be32(S_IFREG);
1941 gfs2_trans_end(sdp);
1947 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1948 * @ip: the file being written to
1949 * @filname: the filename that's going to be added
1951 * Returns: 1 if alloc required, 0 if not, -ve on error
1954 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
1956 struct gfs2_dirent *dent;
1957 struct buffer_head *bh;
1959 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1964 return PTR_ERR(dent);