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 <asm/semaphore.h>
66 #include "lm_interface.h"
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
82 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
83 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
85 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
86 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
89 typedef int (*leaf_call_t) (struct gfs2_inode *dip,
90 uint32_t index, uint32_t len, uint64_t leaf_no,
93 int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
94 struct buffer_head **bhp)
96 struct buffer_head *bh;
100 bh = gfs2_meta_new(ip->i_gl, block);
101 gfs2_trans_add_bh(ip->i_gl, bh, 1);
102 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
103 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
105 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT,
109 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
121 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
122 unsigned int offset, unsigned int size)
125 struct buffer_head *dibh;
128 error = gfs2_meta_inode_buffer(ip, &dibh);
132 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
133 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
134 if (ip->i_di.di_size < offset + size)
135 ip->i_di.di_size = offset + size;
136 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
137 gfs2_dinode_out(&ip->i_di, dibh->b_data);
147 * gfs2_dir_write_data - Write directory information to the inode
148 * @ip: The GFS2 inode
149 * @buf: The buffer containing information to be written
150 * @offset: The file offset to start writing at
151 * @size: The amount of data to write
153 * Returns: The number of bytes correctly written or error code
155 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
156 uint64_t offset, unsigned int size)
158 struct gfs2_sbd *sdp = ip->i_sbd;
159 struct buffer_head *dibh;
160 uint64_t lblock, dblock;
169 if (gfs2_is_stuffed(ip) &&
170 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
171 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
174 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
177 if (gfs2_is_stuffed(ip)) {
178 error = gfs2_unstuff_dinode(ip, NULL, NULL);
184 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
186 while (copied < size) {
188 struct buffer_head *bh;
191 amount = size - copied;
192 if (amount > sdp->sd_sb.sb_bsize - o)
193 amount = sdp->sd_sb.sb_bsize - o;
197 error = gfs2_block_map(ip, lblock, &new, &dblock,
202 if (gfs2_assert_withdraw(sdp, dblock))
206 error = gfs2_dir_get_buffer(ip, dblock,
207 (amount == sdp->sd_jbsize) ?
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
223 o = sizeof(struct gfs2_meta_header);
227 error = gfs2_meta_inode_buffer(ip, &dibh);
231 if (ip->i_di.di_size < offset + copied)
232 ip->i_di.di_size = offset + copied;
233 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
235 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
236 gfs2_dinode_out(&ip->i_di, dibh->b_data);
246 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
247 unsigned int offset, unsigned int size)
249 struct buffer_head *dibh;
252 error = gfs2_meta_inode_buffer(ip, &dibh);
254 offset += sizeof(struct gfs2_dinode);
255 memcpy(buf, dibh->b_data + offset, size);
259 return (error) ? error : size;
264 * gfs2_dir_read_data - Read a data from a directory inode
265 * @ip: The GFS2 Inode
266 * @buf: The buffer to place result into
267 * @offset: File offset to begin jdata_readng from
268 * @size: Amount of data to transfer
270 * Returns: The amount of data actually copied or the error
272 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
273 uint64_t offset, unsigned int size)
275 struct gfs2_sbd *sdp = ip->i_sbd;
276 uint64_t lblock, dblock;
282 if (offset >= ip->i_di.di_size)
285 if ((offset + size) > ip->i_di.di_size)
286 size = ip->i_di.di_size - offset;
291 if (gfs2_is_stuffed(ip))
292 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
295 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
299 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
301 while (copied < size) {
303 struct buffer_head *bh;
306 amount = size - copied;
307 if (amount > sdp->sd_sb.sb_bsize - o)
308 amount = sdp->sd_sb.sb_bsize - o;
312 error = gfs2_block_map(ip, lblock, &new, &dblock,
319 gfs2_meta_ra(ip->i_gl, dblock, extlen);
322 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
330 memcpy(buf, bh->b_data + o, amount);
338 o = sizeof(struct gfs2_meta_header);
343 return (copied) ? copied : error;
346 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
347 const struct qstr *name);
349 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
350 const struct qstr *name, int ret)
352 if (dent->de_inum.no_addr != 0 &&
353 be32_to_cpu(dent->de_hash) == name->hash &&
354 be16_to_cpu(dent->de_name_len) == name->len &&
355 memcmp((char *)(dent+1), name->name, name->len) == 0)
360 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
361 const struct qstr *name)
363 return __gfs2_dirent_find(dent, name, 1);
366 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
367 const struct qstr *name)
369 return __gfs2_dirent_find(dent, name, 2);
373 * name->name holds ptr to start of block.
374 * name->len holds size of block.
376 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
377 const struct qstr *name)
379 const char *start = name->name;
380 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
381 if (name->len == (end - start))
386 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
387 const struct qstr *name)
389 unsigned required = GFS2_DIRENT_SIZE(name->len);
390 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
391 unsigned totlen = be16_to_cpu(dent->de_rec_len);
393 if ((totlen - actual) >= required)
399 * Other possible things to check:
400 * - Inode located within filesystem size (and on valid block)
401 * - Valid directory entry type
402 * Not sure how heavy-weight we want to make this... could also check
403 * hash is correct for example, but that would take a lot of extra time.
404 * For now the most important thing is to check that the various sizes
407 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
408 unsigned int size, unsigned int len, int first)
410 const char *msg = "gfs2_dirent too small";
411 if (unlikely(size < sizeof(struct gfs2_dirent)))
413 msg = "gfs2_dirent misaligned";
414 if (unlikely(offset & 0x7))
416 msg = "gfs2_dirent points beyond end of block";
417 if (unlikely(offset + size > len))
419 msg = "zero inode number";
420 if (unlikely(!first && !dent->de_inum.no_addr))
422 msg = "name length is greater than space in dirent";
423 if (dent->de_inum.no_addr &&
424 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
429 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
430 first ? "first in block" : "not first in block");
434 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
436 unsigned int len, gfs2_dscan_t scan,
437 const struct qstr *name)
439 struct gfs2_meta_header *h = buf;
440 struct gfs2_dirent *dent, *prev;
446 BUG_ON(name == NULL);
448 switch(be16_to_cpu(h->mh_type)) {
449 case GFS2_METATYPE_LF:
450 offset = sizeof(struct gfs2_leaf);
452 case GFS2_METATYPE_DI:
453 offset = sizeof(struct gfs2_dinode);
460 dent = (struct gfs2_dirent *)(buf + offset);
461 size = be16_to_cpu(dent->de_rec_len);
462 if (gfs2_check_dirent(dent, offset, size, len, 1))
465 ret = scan(dent, name);
472 dent = (struct gfs2_dirent *)(buf + offset);
473 size = be16_to_cpu(dent->de_rec_len);
474 if (gfs2_check_dirent(dent, offset, size, len, 0))
484 return prev ? prev : dent;
491 printk(KERN_WARNING "gfs2_scan_dirent: %p wrong block type %u\n", scan,
492 be16_to_cpu(h->mh_type));
494 gfs2_consist_inode(inode->u.generic_ip);
495 return ERR_PTR(-EIO);
500 * dirent_first - Return the first dirent
501 * @dip: the directory
503 * @dent: Pointer to list of dirents
505 * return first dirent whether bh points to leaf or stuffed dinode
507 * Returns: IS_LEAF, IS_DINODE, or -errno
510 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
511 struct gfs2_dirent **dent)
513 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
515 if (be16_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
516 if (gfs2_meta_check(dip->i_sbd, bh))
518 *dent = (struct gfs2_dirent *)(bh->b_data +
519 sizeof(struct gfs2_leaf));
522 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
524 *dent = (struct gfs2_dirent *)(bh->b_data +
525 sizeof(struct gfs2_dinode));
531 * dirent_next - Next dirent
532 * @dip: the directory
534 * @dent: Pointer to list of dirents
536 * Returns: 0 on success, error code otherwise
539 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
540 struct gfs2_dirent **dent)
542 struct gfs2_dirent *tmp, *cur;
544 uint16_t cur_rec_len;
547 bh_end = bh->b_data + bh->b_size;
548 cur_rec_len = be16_to_cpu(cur->de_rec_len);
550 if ((char *)cur + cur_rec_len >= bh_end) {
551 if ((char *)cur + cur_rec_len > bh_end) {
552 gfs2_consist_inode(dip);
558 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
560 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
561 gfs2_consist_inode(dip);
565 if (cur_rec_len == 0) {
566 gfs2_consist_inode(dip);
570 /* Only the first dent could ever have de_inum.no_addr == 0 */
571 if (!tmp->de_inum.no_addr) {
572 gfs2_consist_inode(dip);
582 * dirent_del - Delete a dirent
583 * @dip: The GFS2 inode
585 * @prev: The previous dirent
586 * @cur: The current dirent
590 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
591 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
593 uint16_t cur_rec_len, prev_rec_len;
595 if (!cur->de_inum.no_addr) {
596 gfs2_consist_inode(dip);
600 gfs2_trans_add_bh(dip->i_gl, bh, 1);
602 /* If there is no prev entry, this is the first entry in the block.
603 The de_rec_len is already as big as it needs to be. Just zero
604 out the inode number and return. */
607 cur->de_inum.no_addr = 0; /* No endianess worries */
611 /* Combine this dentry with the previous one. */
613 prev_rec_len = be16_to_cpu(prev->de_rec_len);
614 cur_rec_len = be16_to_cpu(cur->de_rec_len);
616 if ((char *)prev + prev_rec_len != (char *)cur)
617 gfs2_consist_inode(dip);
618 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
619 gfs2_consist_inode(dip);
621 prev_rec_len += cur_rec_len;
622 prev->de_rec_len = cpu_to_be16(prev_rec_len);
626 * Takes a dent from which to grab space as an argument. Returns the
627 * newly created dent.
629 struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
630 struct gfs2_dirent *dent,
631 const struct qstr *name,
632 struct buffer_head *bh)
634 struct gfs2_inode *ip = inode->u.generic_ip;
635 struct gfs2_dirent *ndent;
636 unsigned offset = 0, totlen;
638 if (dent->de_inum.no_addr)
639 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
640 totlen = be16_to_cpu(dent->de_rec_len);
641 BUG_ON(offset + name->len > totlen);
642 gfs2_trans_add_bh(ip->i_gl, bh, 1);
643 ndent = (struct gfs2_dirent *)((char *)dent + offset);
644 dent->de_rec_len = cpu_to_be16(offset);
645 gfs2_qstr2dirent(name, totlen - offset, ndent);
649 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
650 struct buffer_head *bh,
651 const struct qstr *name)
653 struct gfs2_dirent *dent;
654 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, gfs2_dirent_find_space, name);
655 if (!dent || IS_ERR(dent))
657 return gfs2_init_dirent(inode, dent, name, bh);
660 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
661 struct buffer_head **bhp)
665 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
666 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
673 * get_leaf_nr - Get a leaf number associated with the index
674 * @dip: The GFS2 inode
678 * Returns: 0 on success, error code otherwise
681 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
687 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
688 index * sizeof(uint64_t),
690 if (error != sizeof(uint64_t))
691 return (error < 0) ? error : -EIO;
693 *leaf_out = be64_to_cpu(leaf_no);
698 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
699 struct buffer_head **bh_out)
704 error = get_leaf_nr(dip, index, &leaf_no);
706 error = get_leaf(dip, leaf_no, bh_out);
711 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
712 const struct qstr *name,
714 struct buffer_head **pbh)
716 struct buffer_head *bh;
717 struct gfs2_dirent *dent;
718 struct gfs2_inode *ip = inode->u.generic_ip;
721 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
722 struct gfs2_leaf *leaf;
723 unsigned hsize = 1 << ip->i_di.di_depth;
726 if (hsize * sizeof(u64) != ip->i_di.di_size) {
727 gfs2_consist_inode(ip);
728 return ERR_PTR(-EIO);
731 index = name->hash >> (32 - ip->i_di.di_depth);
732 error = get_first_leaf(ip, index, &bh);
734 return ERR_PTR(error);
736 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
740 leaf = (struct gfs2_leaf *)bh->b_data;
741 ln = be64_to_cpu(leaf->lf_next);
745 error = get_leaf(ip, ln, &bh);
748 return error ? ERR_PTR(error) : NULL;
751 error = gfs2_meta_inode_buffer(ip, &bh);
753 return ERR_PTR(error);
754 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name);
760 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
762 struct gfs2_inode *ip = inode->u.generic_ip;
763 u64 bn = gfs2_alloc_meta(ip);
764 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
765 struct gfs2_leaf *leaf;
766 struct gfs2_dirent *dent;
769 gfs2_trans_add_bh(ip->i_gl, bh, 1);
770 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
771 leaf = (struct gfs2_leaf *)bh->b_data;
772 leaf->lf_depth = cpu_to_be16(depth);
773 leaf->lf_entries = cpu_to_be16(0);
774 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
775 leaf->lf_next = cpu_to_be64(0);
776 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
777 dent = (struct gfs2_dirent *)(leaf+1);
778 dent->de_inum.no_formal_ino = cpu_to_be64(0);
779 dent->de_inum.no_addr = cpu_to_be64(0);
780 dent->de_hash = cpu_to_be32(0);
781 dent->de_rec_len = cpu_to_be16(bh->b_size - sizeof(struct gfs2_leaf));
782 dent->de_name_len = cpu_to_be16(0);
783 dent->de_type = cpu_to_be16(0);
789 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
790 * @dip: The GFS2 inode
792 * Returns: 0 on success, error code otherwise
795 static int dir_make_exhash(struct inode *inode)
797 struct gfs2_inode *dip = inode->u.generic_ip;
798 struct gfs2_sbd *sdp = dip->i_sbd;
799 struct gfs2_dirent *dent;
801 struct buffer_head *bh, *dibh;
802 struct gfs2_leaf *leaf;
808 error = gfs2_meta_inode_buffer(dip, &dibh);
812 /* Turn over a new leaf */
814 leaf = new_leaf(inode, &bh, 0);
819 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
820 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
824 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
825 sizeof(struct gfs2_dinode));
827 /* Find last entry */
830 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
831 sizeof(struct gfs2_leaf);
832 args.name = bh->b_data;
833 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
834 gfs2_dirent_last, &args);
843 return PTR_ERR(dent);
846 /* Adjust the last dirent's record length
847 (Remember that dent still points to the last entry.) */
849 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
850 sizeof(struct gfs2_dinode) -
851 sizeof(struct gfs2_leaf));
855 /* We're done with the new leaf block, now setup the new
858 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
859 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
861 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
863 for (x = sdp->sd_hash_ptrs; x--; lp++)
864 *lp = cpu_to_be64(bn);
866 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
867 dip->i_di.di_blocks++;
868 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
869 dip->i_di.di_payload_format = 0;
871 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
872 dip->i_di.di_depth = y;
874 gfs2_dinode_out(&dip->i_di, dibh->b_data);
882 * dir_split_leaf - Split a leaf block into two
883 * @dip: The GFS2 inode
887 * Returns: 0 on success, error code on failure
890 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
892 struct gfs2_inode *dip = inode->u.generic_ip;
893 struct buffer_head *nbh, *obh, *dibh;
894 struct gfs2_leaf *nleaf, *oleaf;
895 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
896 uint32_t start, len, half_len, divider;
897 uint64_t bn, *lp, leaf_no;
902 index = name->hash >> (32 - dip->i_di.di_depth);
903 error = get_leaf_nr(dip, index, &leaf_no);
907 /* Get the old leaf block */
908 error = get_leaf(dip, leaf_no, &obh);
912 gfs2_trans_add_bh(dip->i_gl, obh, 1);
913 oleaf = (struct gfs2_leaf *)obh->b_data;
915 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
922 /* Compute the start and len of leaf pointers in the hash table. */
923 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
926 gfs2_consist_inode(dip);
931 start = (index & ~(len - 1));
933 /* Change the pointers.
934 Don't bother distinguishing stuffed from non-stuffed.
935 This code is complicated enough already. */
936 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
937 /* Change the pointers */
938 for (x = 0; x < half_len; x++)
939 lp[x] = cpu_to_be64(bn);
941 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
942 half_len * sizeof(uint64_t));
943 if (error != half_len * sizeof(uint64_t)) {
951 /* Compute the divider */
952 divider = (start + half_len) << (32 - dip->i_di.di_depth);
954 /* Copy the entries */
955 dirent_first(dip, obh, &dent);
959 if (dirent_next(dip, obh, &next))
962 if (dent->de_inum.no_addr &&
963 be32_to_cpu(dent->de_hash) < divider) {
965 str.name = (char*)(dent+1);
966 str.len = be16_to_cpu(dent->de_name_len);
967 str.hash = be32_to_cpu(dent->de_hash);
968 new = gfs2_dirent_alloc(dip->i_vnode, nbh, &str);
970 error = PTR_ERR(new);
974 new->de_inum = dent->de_inum; /* No endian worries */
975 new->de_type = dent->de_type; /* No endian worries */
976 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
978 dirent_del(dip, obh, prev, dent);
980 if (!oleaf->lf_entries)
981 gfs2_consist_inode(dip);
982 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
994 oleaf->lf_depth = nleaf->lf_depth;
996 error = gfs2_meta_inode_buffer(dip, &dibh);
997 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
998 dip->i_di.di_blocks++;
999 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1020 * dir_double_exhash - Double size of ExHash table
1021 * @dip: The GFS2 dinode
1023 * Returns: 0 on success, error code on failure
1026 static int dir_double_exhash(struct gfs2_inode *dip)
1028 struct gfs2_sbd *sdp = dip->i_sbd;
1029 struct buffer_head *dibh;
1032 uint64_t *from, *to;
1037 hsize = 1 << dip->i_di.di_depth;
1038 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1039 gfs2_consist_inode(dip);
1043 /* Allocate both the "from" and "to" buffers in one big chunk */
1045 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1047 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1048 error = gfs2_dir_read_data(dip, (char *)buf,
1049 block * sdp->sd_hash_bsize,
1050 sdp->sd_hash_bsize);
1051 if (error != sdp->sd_hash_bsize) {
1058 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1060 for (x = sdp->sd_hash_ptrs; x--; from++) {
1061 *to++ = *from; /* No endianess worries */
1065 error = gfs2_dir_write_data(dip,
1066 (char *)buf + sdp->sd_hash_bsize,
1067 block * sdp->sd_sb.sb_bsize,
1068 sdp->sd_sb.sb_bsize);
1069 if (error != sdp->sd_sb.sb_bsize) {
1078 error = gfs2_meta_inode_buffer(dip, &dibh);
1079 if (!gfs2_assert_withdraw(sdp, !error)) {
1080 dip->i_di.di_depth++;
1081 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1094 * compare_dents - compare directory entries by hash value
1098 * When comparing the hash entries of @a to @b:
1104 static int compare_dents(const void *a, const void *b)
1106 struct gfs2_dirent *dent_a, *dent_b;
1107 uint32_t hash_a, hash_b;
1110 dent_a = *(struct gfs2_dirent **)a;
1111 hash_a = be32_to_cpu(dent_a->de_hash);
1113 dent_b = *(struct gfs2_dirent **)b;
1114 hash_b = be32_to_cpu(dent_b->de_hash);
1116 if (hash_a > hash_b)
1118 else if (hash_a < hash_b)
1121 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1122 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1126 else if (len_a < len_b)
1129 ret = memcmp((char *)(dent_a + 1),
1130 (char *)(dent_b + 1),
1138 * do_filldir_main - read out directory entries
1139 * @dip: The GFS2 inode
1140 * @offset: The offset in the file to read from
1141 * @opaque: opaque data to pass to filldir
1142 * @filldir: The function to pass entries to
1143 * @darr: an array of struct gfs2_dirent pointers to read
1144 * @entries: the number of entries in darr
1145 * @copied: pointer to int that's non-zero if a entry has been copied out
1147 * Jump through some hoops to make sure that if there are hash collsions,
1148 * they are read out at the beginning of a buffer. We want to minimize
1149 * the possibility that they will fall into different readdir buffers or
1150 * that someone will want to seek to that location.
1152 * Returns: errno, >0 on exception from filldir
1155 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1156 void *opaque, gfs2_filldir_t filldir,
1157 struct gfs2_dirent **darr, uint32_t entries,
1160 struct gfs2_dirent *dent, *dent_next;
1161 struct gfs2_inum inum;
1162 uint64_t off, off_next;
1167 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1169 dent_next = darr[0];
1170 off_next = be32_to_cpu(dent_next->de_hash);
1171 off_next = gfs2_disk_hash2offset(off_next);
1173 for (x = 0, y = 1; x < entries; x++, y++) {
1178 dent_next = darr[y];
1179 off_next = be32_to_cpu(dent_next->de_hash);
1180 off_next = gfs2_disk_hash2offset(off_next);
1186 if (off_next == off) {
1187 if (*copied && !run)
1198 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1200 error = filldir(opaque, (char *)(dent + 1),
1201 be16_to_cpu(dent->de_name_len),
1203 be16_to_cpu(dent->de_type));
1210 /* Increment the *offset by one, so the next time we come into the
1211 do_filldir fxn, we get the next entry instead of the last one in the
1220 * do_filldir_single - Read directory entries out of a single block
1221 * @dip: The GFS2 inode
1222 * @offset: The offset in the file to read from
1223 * @opaque: opaque data to pass to filldir
1224 * @filldir: The function to pass entries to
1226 * @entries: the number of entries in the block
1227 * @copied: pointer to int that's non-zero if a entry has been copied out
1229 * Returns: errno, >0 on exception from filldir
1232 static int do_filldir_single(struct gfs2_inode *dip, uint64_t *offset,
1233 void *opaque, gfs2_filldir_t filldir,
1234 struct buffer_head *bh, uint32_t entries,
1237 struct gfs2_dirent **darr;
1238 struct gfs2_dirent *de;
1245 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1249 dirent_first(dip, bh, &de);
1251 if (!de->de_inum.no_addr)
1254 gfs2_consist_inode(dip);
1259 } while (dirent_next(dip, bh, &de) == 0);
1262 gfs2_consist_inode(dip);
1267 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1277 * do_filldir_multi - Read directory entries out of a linked leaf list
1278 * @dip: The GFS2 inode
1279 * @offset: The offset in the file to read from
1280 * @opaque: opaque data to pass to filldir
1281 * @filldir: The function to pass entries to
1282 * @bh: the first leaf in the list
1283 * @copied: pointer to int that's non-zero if a entry has been copied out
1285 * Returns: errno, >0 on exception from filldir
1288 static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1289 void *opaque, gfs2_filldir_t filldir,
1290 struct buffer_head *bh, int *copied)
1292 struct buffer_head **larr = NULL;
1293 struct gfs2_dirent **darr;
1294 struct gfs2_leaf *leaf;
1295 struct buffer_head *tmp_bh;
1296 struct gfs2_dirent *de;
1297 unsigned int entries, e = 0;
1298 unsigned int leaves = 0, l = 0;
1303 /* Count leaves and entries */
1305 leaf = (struct gfs2_leaf *)bh->b_data;
1306 entries = be16_to_cpu(leaf->lf_entries);
1307 ln = be64_to_cpu(leaf->lf_next);
1310 error = get_leaf(dip, ln, &tmp_bh);
1314 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1315 if (leaf->lf_entries) {
1316 entries += be16_to_cpu(leaf->lf_entries);
1319 ln = be64_to_cpu(leaf->lf_next);
1328 larr = kcalloc(leaves, sizeof(struct buffer_head *),GFP_KERNEL);
1333 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1339 leaf = (struct gfs2_leaf *)bh->b_data;
1340 if (leaf->lf_entries) {
1341 dirent_first(dip, bh, &de);
1343 if (!de->de_inum.no_addr)
1346 gfs2_consist_inode(dip);
1351 } while (dirent_next(dip, bh, &de) == 0);
1353 ln = be64_to_cpu(leaf->lf_next);
1356 error = get_leaf(dip, ln, &tmp_bh);
1360 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1361 if (leaf->lf_entries) {
1362 dirent_first(dip, tmp_bh, &de);
1364 if (!de->de_inum.no_addr)
1367 gfs2_consist_inode(dip);
1372 } while (dirent_next(dip, tmp_bh, &de) == 0);
1376 ln = be64_to_cpu(leaf->lf_next);
1378 ln = be64_to_cpu(leaf->lf_next);
1383 if (gfs2_assert_withdraw(dip->i_sbd, l == leaves)) {
1388 gfs2_consist_inode(dip);
1393 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1398 for (x = 0; x < l; x++)
1406 * dir_e_read - Reads the entries from a directory into a filldir buffer
1407 * @dip: dinode pointer
1408 * @offset: the hash of the last entry read shifted to the right once
1409 * @opaque: buffer for the filldir function to fill
1410 * @filldir: points to the filldir function to use
1415 static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1416 gfs2_filldir_t filldir)
1418 struct gfs2_sbd *sdp = dip->i_sbd;
1419 struct buffer_head *bh;
1420 struct gfs2_leaf *leaf;
1421 uint32_t hsize, len;
1422 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1423 uint32_t hash, index;
1428 hsize = 1 << dip->i_di.di_depth;
1429 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1430 gfs2_consist_inode(dip);
1434 hash = gfs2_dir_offset2hash(*offset);
1435 index = hash >> (32 - dip->i_di.di_depth);
1437 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1441 while (index < hsize) {
1442 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1443 ht_offset = index - lp_offset;
1445 if (ht_offset_cur != ht_offset) {
1446 error = gfs2_dir_read_data(dip, (char *)lp,
1447 ht_offset * sizeof(uint64_t),
1448 sdp->sd_hash_bsize);
1449 if (error != sdp->sd_hash_bsize) {
1454 ht_offset_cur = ht_offset;
1457 error = get_leaf(dip, be64_to_cpu(lp[lp_offset]), &bh);
1461 leaf = (struct gfs2_leaf *)bh->b_data;
1463 error = do_filldir_multi(dip, offset, opaque, filldir,
1466 error = do_filldir_single(dip, offset, opaque, filldir,
1468 be16_to_cpu(leaf->lf_entries),
1479 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1480 index = (index & ~(len - 1)) + len;
1489 static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1490 gfs2_filldir_t filldir)
1492 struct buffer_head *dibh;
1496 if (!gfs2_is_stuffed(dip)) {
1497 gfs2_consist_inode(dip);
1501 if (!dip->i_di.di_entries)
1504 error = gfs2_meta_inode_buffer(dip, &dibh);
1508 error = do_filldir_single(dip, offset,
1510 dibh, dip->i_di.di_entries,
1521 * gfs2_dir_search - Search a directory
1522 * @dip: The GFS2 inode
1526 * This routine searches a directory for a file or another directory.
1527 * Assumes a glock is held on dip.
1532 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1533 struct gfs2_inum *inum, unsigned int *type)
1535 struct buffer_head *bh;
1536 struct gfs2_dirent *dent;
1538 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1541 return PTR_ERR(dent);
1543 gfs2_inum_in(inum, (char *)&dent->de_inum);
1545 *type = be16_to_cpu(dent->de_type);
1552 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1554 struct buffer_head *bh, *obh;
1555 struct gfs2_inode *ip = inode->u.generic_ip;
1556 struct gfs2_leaf *leaf, *oleaf;
1561 index = name->hash >> (32 - ip->i_di.di_depth);
1562 error = get_first_leaf(ip, index, &obh);
1566 oleaf = (struct gfs2_leaf *)obh->b_data;
1567 bn = be64_to_cpu(oleaf->lf_next);
1571 error = get_leaf(ip, bn, &obh);
1576 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1578 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1583 oleaf->lf_next = cpu_to_be64(bn);
1587 error = gfs2_meta_inode_buffer(ip, &bh);
1590 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1591 ip->i_di.di_blocks++;
1592 gfs2_dinode_out(&ip->i_di, bh->b_data);
1598 * gfs2_dir_add - Add new filename into directory
1599 * @dip: The GFS2 inode
1600 * @filename: The new name
1601 * @inode: The inode number of the entry
1602 * @type: The type of the entry
1604 * Returns: 0 on success, error code on failure
1607 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1608 const struct gfs2_inum *inum, unsigned type)
1610 struct gfs2_inode *ip = inode->u.generic_ip;
1611 struct buffer_head *bh;
1612 struct gfs2_dirent *dent;
1613 struct gfs2_leaf *leaf;
1617 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1621 return PTR_ERR(dent);
1622 dent = gfs2_init_dirent(inode, dent, name, bh);
1623 gfs2_inum_out(inum, (char *)&dent->de_inum);
1624 dent->de_type = cpu_to_be16(type);
1625 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1626 leaf = (struct gfs2_leaf *)bh->b_data;
1627 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1630 error = gfs2_meta_inode_buffer(ip, &bh);
1633 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1634 ip->i_di.di_entries++;
1635 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1636 gfs2_dinode_out(&ip->i_di, bh->b_data);
1641 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1642 error = dir_make_exhash(inode);
1647 error = dir_split_leaf(inode, name);
1650 if (error != -ENOSPC)
1652 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1653 error = dir_double_exhash(ip);
1656 error = dir_split_leaf(inode, name);
1661 error = dir_new_leaf(inode, name);
1672 * gfs2_dir_del - Delete a directory entry
1673 * @dip: The GFS2 inode
1674 * @filename: The filename
1676 * Returns: 0 on success, error code on failure
1679 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1681 struct gfs2_dirent *dent, *prev = NULL;
1682 struct buffer_head *bh;
1685 /* Returns _either_ the entry (if its first in block) or the
1686 previous entry otherwise */
1687 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
1689 gfs2_consist_inode(dip);
1693 gfs2_consist_inode(dip);
1694 return PTR_ERR(dent);
1696 /* If not first in block, adjust pointers accordingly */
1697 if (gfs2_dirent_find(dent, name) == 0) {
1699 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1702 dirent_del(dip, bh, prev, dent);
1703 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1704 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1705 u16 entries = be16_to_cpu(leaf->lf_entries);
1707 gfs2_consist_inode(dip);
1708 leaf->lf_entries = cpu_to_be16(--entries);
1712 error = gfs2_meta_inode_buffer(dip, &bh);
1716 if (!dip->i_di.di_entries)
1717 gfs2_consist_inode(dip);
1718 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1719 dip->i_di.di_entries--;
1720 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1721 gfs2_dinode_out(&dip->i_di, bh->b_data);
1727 int gfs2_dir_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1728 gfs2_filldir_t filldir)
1732 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1733 error = dir_e_read(dip, offset, opaque, filldir);
1735 error = dir_l_read(dip, offset, opaque, filldir);
1741 * gfs2_dir_mvino - Change inode number of directory entry
1742 * @dip: The GFS2 inode
1746 * This routine changes the inode number of a directory entry. It's used
1747 * by rename to change ".." when a directory is moved.
1748 * Assumes a glock is held on dvp.
1753 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1754 struct gfs2_inum *inum, unsigned int new_type)
1756 struct buffer_head *bh;
1757 struct gfs2_dirent *dent;
1760 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
1762 gfs2_consist_inode(dip);
1766 return PTR_ERR(dent);
1768 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1769 gfs2_inum_out(inum, (char *)&dent->de_inum);
1770 dent->de_type = cpu_to_be16(new_type);
1772 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1774 error = gfs2_meta_inode_buffer(dip, &bh);
1777 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1780 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1781 gfs2_dinode_out(&dip->i_di, bh->b_data);
1787 * foreach_leaf - call a function for each leaf in a directory
1788 * @dip: the directory
1789 * @lc: the function to call for each each
1790 * @data: private data to pass to it
1795 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1797 struct gfs2_sbd *sdp = dip->i_sbd;
1798 struct buffer_head *bh;
1799 struct gfs2_leaf *leaf;
1800 uint32_t hsize, len;
1801 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1807 hsize = 1 << dip->i_di.di_depth;
1808 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1809 gfs2_consist_inode(dip);
1813 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1817 while (index < hsize) {
1818 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1819 ht_offset = index - lp_offset;
1821 if (ht_offset_cur != ht_offset) {
1822 error = gfs2_dir_read_data(dip, (char *)lp,
1823 ht_offset * sizeof(uint64_t),
1824 sdp->sd_hash_bsize);
1825 if (error != sdp->sd_hash_bsize) {
1830 ht_offset_cur = ht_offset;
1833 leaf_no = be64_to_cpu(lp[lp_offset]);
1835 error = get_leaf(dip, leaf_no, &bh);
1838 leaf = (struct gfs2_leaf *)bh->b_data;
1841 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1843 error = lc(dip, index, len, leaf_no, data);
1847 index = (index & ~(len - 1)) + len;
1852 if (index != hsize) {
1853 gfs2_consist_inode(dip);
1864 * leaf_dealloc - Deallocate a directory leaf
1865 * @dip: the directory
1866 * @index: the hash table offset in the directory
1867 * @len: the number of pointers to this leaf
1868 * @leaf_no: the leaf number
1874 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1875 uint64_t leaf_no, void *data)
1877 struct gfs2_sbd *sdp = dip->i_sbd;
1878 struct gfs2_leaf *tmp_leaf;
1879 struct gfs2_rgrp_list rlist;
1880 struct buffer_head *bh, *dibh;
1882 unsigned int rg_blocks = 0, l_blocks = 0;
1884 unsigned int x, size = len * sizeof(uint64_t);
1887 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1889 ht = kzalloc(size, GFP_KERNEL);
1893 gfs2_alloc_get(dip);
1895 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1899 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1903 /* Count the number of leaves */
1905 for (blk = leaf_no; blk; blk = nblk) {
1906 error = get_leaf(dip, blk, &bh);
1909 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1910 nblk = be64_to_cpu(tmp_leaf->lf_next);
1913 gfs2_rlist_add(sdp, &rlist, blk);
1917 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1919 for (x = 0; x < rlist.rl_rgrps; x++) {
1920 struct gfs2_rgrpd *rgd;
1921 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1922 rg_blocks += rgd->rd_ri.ri_length;
1925 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1929 error = gfs2_trans_begin(sdp,
1930 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1931 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1933 goto out_rg_gunlock;
1935 for (blk = leaf_no; blk; blk = nblk) {
1936 error = get_leaf(dip, blk, &bh);
1939 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1940 nblk = be64_to_cpu(tmp_leaf->lf_next);
1943 gfs2_free_meta(dip, blk, 1);
1945 if (!dip->i_di.di_blocks)
1946 gfs2_consist_inode(dip);
1947 dip->i_di.di_blocks--;
1950 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
1951 if (error != size) {
1957 error = gfs2_meta_inode_buffer(dip, &dibh);
1961 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1962 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1966 gfs2_trans_end(sdp);
1969 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1972 gfs2_rlist_free(&rlist);
1973 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1976 gfs2_quota_unhold(dip);
1979 gfs2_alloc_put(dip);
1986 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1987 * @dip: the directory
1989 * Dealloc all on-disk directory leaves to FREEMETA state
1990 * Change on-disk inode type to "regular file"
1995 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1997 struct gfs2_sbd *sdp = dip->i_sbd;
1998 struct buffer_head *bh;
2001 /* Dealloc on-disk leaves to FREEMETA state */
2002 error = foreach_leaf(dip, leaf_dealloc, NULL);
2006 /* Make this a regular file in case we crash.
2007 (We don't want to free these blocks a second time.) */
2009 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
2013 error = gfs2_meta_inode_buffer(dip, &bh);
2015 gfs2_trans_add_bh(dip->i_gl, bh, 1);
2016 ((struct gfs2_dinode *)bh->b_data)->di_mode =
2017 cpu_to_be32(S_IFREG);
2021 gfs2_trans_end(sdp);
2027 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2028 * @ip: the file being written to
2029 * @filname: the filename that's going to be added
2031 * Returns: 1 if alloc required, 0 if not, -ve on error
2034 int gfs2_diradd_alloc_required(struct inode *inode,
2035 const struct qstr *name)
2037 struct gfs2_dirent *dent;
2038 struct buffer_head *bh;
2040 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
2044 return PTR_ERR(dent);