2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
39 * dip->i_diskflags & GFS2_DIF_EXHASH is true
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
43 * When the dirents are in leaves, the actual contents of the directory file are
44 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
45 * dirents are NOT in the directory file itself. There can be more than one
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the same leaf.
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
56 #include <linux/slab.h>
57 #include <linux/spinlock.h>
58 #include <linux/buffer_head.h>
59 #include <linux/sort.h>
60 #include <linux/gfs2_ondisk.h>
61 #include <linux/crc32.h>
62 #include <linux/vmalloc.h>
76 #define IS_LEAF 1 /* Hashed (leaf) directory */
77 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
79 #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
81 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
84 struct qstr gfs2_qdot __read_mostly;
85 struct qstr gfs2_qdotdot __read_mostly;
87 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
88 const struct qstr *name, void *opaque);
90 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
91 struct buffer_head **bhp)
93 struct buffer_head *bh;
95 bh = gfs2_meta_new(ip->i_gl, block);
96 gfs2_trans_add_meta(ip->i_gl, bh);
97 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
103 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
104 struct buffer_head **bhp)
106 struct buffer_head *bh;
109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
120 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
123 struct buffer_head *dibh;
126 error = gfs2_meta_inode_buffer(ip, &dibh);
130 gfs2_trans_add_meta(ip->i_gl, dibh);
131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
132 if (ip->i_inode.i_size < offset + size)
133 i_size_write(&ip->i_inode, offset + size);
134 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
135 gfs2_dinode_out(ip, dibh->b_data);
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
151 * Returns: The number of bytes correctly written or error code
153 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
154 u64 offset, unsigned int size)
156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
157 struct buffer_head *dibh;
168 if (gfs2_is_stuffed(ip) &&
169 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
170 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
173 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
176 if (gfs2_is_stuffed(ip)) {
177 error = gfs2_unstuff_dinode(ip, NULL);
183 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
185 while (copied < size) {
187 struct buffer_head *bh;
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
200 if (gfs2_assert_withdraw(sdp, dblock))
204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
212 gfs2_trans_add_meta(ip->i_gl, bh);
213 memcpy(bh->b_data + o, buf, amount);
222 o = sizeof(struct gfs2_meta_header);
226 error = gfs2_meta_inode_buffer(ip, &dibh);
230 if (ip->i_inode.i_size < offset + copied)
231 i_size_write(&ip->i_inode, offset + copied);
232 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
234 gfs2_trans_add_meta(ip->i_gl, dibh);
235 gfs2_dinode_out(ip, dibh->b_data);
245 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
248 struct buffer_head *dibh;
251 error = gfs2_meta_inode_buffer(ip, &dibh);
253 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
257 return (error) ? error : size;
262 * gfs2_dir_read_data - Read a data from a directory inode
263 * @ip: The GFS2 Inode
264 * @buf: The buffer to place result into
265 * @size: Amount of data to transfer
267 * Returns: The amount of data actually copied or the error
269 static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
272 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
279 if (gfs2_is_stuffed(ip))
280 return gfs2_dir_read_stuffed(ip, buf, size);
282 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
286 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
288 while (copied < size) {
290 struct buffer_head *bh;
293 amount = size - copied;
294 if (amount > sdp->sd_sb.sb_bsize - o)
295 amount = sdp->sd_sb.sb_bsize - o;
299 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
301 if (error || !dblock)
304 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
306 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
310 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
317 memcpy(buf, bh->b_data + o, amount);
319 buf += (amount/sizeof(__be64));
322 o = sizeof(struct gfs2_meta_header);
327 return (copied) ? copied : error;
331 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
332 * @ip: The inode in question
334 * Returns: The hash table or an error
337 static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
339 struct inode *inode = &ip->i_inode;
344 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
346 hc = ip->i_hash_cache;
350 hsize = 1 << ip->i_depth;
351 hsize *= sizeof(__be64);
352 if (hsize != i_size_read(&ip->i_inode)) {
353 gfs2_consist_inode(ip);
354 return ERR_PTR(-EIO);
357 hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
359 hc = __vmalloc(hsize, GFP_NOFS, PAGE_KERNEL);
362 return ERR_PTR(-ENOMEM);
364 ret = gfs2_dir_read_data(ip, hc, hsize);
366 if (is_vmalloc_addr(hc))
373 spin_lock(&inode->i_lock);
374 if (ip->i_hash_cache) {
375 if (is_vmalloc_addr(hc))
380 ip->i_hash_cache = hc;
382 spin_unlock(&inode->i_lock);
384 return ip->i_hash_cache;
388 * gfs2_dir_hash_inval - Invalidate dir hash
389 * @ip: The directory inode
391 * Must be called with an exclusive glock, or during glock invalidation.
393 void gfs2_dir_hash_inval(struct gfs2_inode *ip)
395 __be64 *hc = ip->i_hash_cache;
396 ip->i_hash_cache = NULL;
397 if (is_vmalloc_addr(hc))
403 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
405 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
408 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
409 const struct qstr *name, int ret)
411 if (!gfs2_dirent_sentinel(dent) &&
412 be32_to_cpu(dent->de_hash) == name->hash &&
413 be16_to_cpu(dent->de_name_len) == name->len &&
414 memcmp(dent+1, name->name, name->len) == 0)
419 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
420 const struct qstr *name,
423 return __gfs2_dirent_find(dent, name, 1);
426 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
427 const struct qstr *name,
430 return __gfs2_dirent_find(dent, name, 2);
434 * name->name holds ptr to start of block.
435 * name->len holds size of block.
437 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
438 const struct qstr *name,
441 const char *start = name->name;
442 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
443 if (name->len == (end - start))
448 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
449 const struct qstr *name,
452 unsigned required = GFS2_DIRENT_SIZE(name->len);
453 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
454 unsigned totlen = be16_to_cpu(dent->de_rec_len);
456 if (gfs2_dirent_sentinel(dent))
458 if (totlen - actual >= required)
463 struct dirent_gather {
464 const struct gfs2_dirent **pdent;
468 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
469 const struct qstr *name,
472 struct dirent_gather *g = opaque;
473 if (!gfs2_dirent_sentinel(dent)) {
474 g->pdent[g->offset++] = dent;
480 * Other possible things to check:
481 * - Inode located within filesystem size (and on valid block)
482 * - Valid directory entry type
483 * Not sure how heavy-weight we want to make this... could also check
484 * hash is correct for example, but that would take a lot of extra time.
485 * For now the most important thing is to check that the various sizes
488 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
489 unsigned int size, unsigned int len, int first)
491 const char *msg = "gfs2_dirent too small";
492 if (unlikely(size < sizeof(struct gfs2_dirent)))
494 msg = "gfs2_dirent misaligned";
495 if (unlikely(offset & 0x7))
497 msg = "gfs2_dirent points beyond end of block";
498 if (unlikely(offset + size > len))
500 msg = "zero inode number";
501 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
503 msg = "name length is greater than space in dirent";
504 if (!gfs2_dirent_sentinel(dent) &&
505 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
510 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
511 first ? "first in block" : "not first in block");
515 static int gfs2_dirent_offset(const void *buf)
517 const struct gfs2_meta_header *h = buf;
522 switch(be32_to_cpu(h->mh_type)) {
523 case GFS2_METATYPE_LF:
524 offset = sizeof(struct gfs2_leaf);
526 case GFS2_METATYPE_DI:
527 offset = sizeof(struct gfs2_dinode);
534 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
535 be32_to_cpu(h->mh_type));
539 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
540 unsigned int len, gfs2_dscan_t scan,
541 const struct qstr *name,
544 struct gfs2_dirent *dent, *prev;
549 ret = gfs2_dirent_offset(buf);
556 size = be16_to_cpu(dent->de_rec_len);
557 if (gfs2_check_dirent(dent, offset, size, len, 1))
560 ret = scan(dent, name, opaque);
568 size = be16_to_cpu(dent->de_rec_len);
569 if (gfs2_check_dirent(dent, offset, size, len, 0))
579 return prev ? prev : dent;
586 gfs2_consist_inode(GFS2_I(inode));
587 return ERR_PTR(-EIO);
590 static int dirent_check_reclen(struct gfs2_inode *dip,
591 const struct gfs2_dirent *d, const void *end_p)
594 u16 rec_len = be16_to_cpu(d->de_rec_len);
596 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
604 gfs2_consist_inode(dip);
609 * dirent_next - Next dirent
610 * @dip: the directory
612 * @dent: Pointer to list of dirents
614 * Returns: 0 on success, error code otherwise
617 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
618 struct gfs2_dirent **dent)
620 struct gfs2_dirent *cur = *dent, *tmp;
621 char *bh_end = bh->b_data + bh->b_size;
624 ret = dirent_check_reclen(dip, cur, bh_end);
628 tmp = (void *)cur + ret;
629 ret = dirent_check_reclen(dip, tmp, bh_end);
633 /* Only the first dent could ever have de_inum.no_addr == 0 */
634 if (gfs2_dirent_sentinel(tmp)) {
635 gfs2_consist_inode(dip);
644 * dirent_del - Delete a dirent
645 * @dip: The GFS2 inode
647 * @prev: The previous dirent
648 * @cur: The current dirent
652 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
653 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
655 u16 cur_rec_len, prev_rec_len;
657 if (gfs2_dirent_sentinel(cur)) {
658 gfs2_consist_inode(dip);
662 gfs2_trans_add_meta(dip->i_gl, bh);
664 /* If there is no prev entry, this is the first entry in the block.
665 The de_rec_len is already as big as it needs to be. Just zero
666 out the inode number and return. */
669 cur->de_inum.no_addr = 0;
670 cur->de_inum.no_formal_ino = 0;
674 /* Combine this dentry with the previous one. */
676 prev_rec_len = be16_to_cpu(prev->de_rec_len);
677 cur_rec_len = be16_to_cpu(cur->de_rec_len);
679 if ((char *)prev + prev_rec_len != (char *)cur)
680 gfs2_consist_inode(dip);
681 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
682 gfs2_consist_inode(dip);
684 prev_rec_len += cur_rec_len;
685 prev->de_rec_len = cpu_to_be16(prev_rec_len);
689 * Takes a dent from which to grab space as an argument. Returns the
690 * newly created dent.
692 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
693 struct gfs2_dirent *dent,
694 const struct qstr *name,
695 struct buffer_head *bh)
697 struct gfs2_inode *ip = GFS2_I(inode);
698 struct gfs2_dirent *ndent;
699 unsigned offset = 0, totlen;
701 if (!gfs2_dirent_sentinel(dent))
702 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
703 totlen = be16_to_cpu(dent->de_rec_len);
704 BUG_ON(offset + name->len > totlen);
705 gfs2_trans_add_meta(ip->i_gl, bh);
706 ndent = (struct gfs2_dirent *)((char *)dent + offset);
707 dent->de_rec_len = cpu_to_be16(offset);
708 gfs2_qstr2dirent(name, totlen - offset, ndent);
712 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
713 struct buffer_head *bh,
714 const struct qstr *name)
716 struct gfs2_dirent *dent;
717 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
718 gfs2_dirent_find_space, name, NULL);
719 if (!dent || IS_ERR(dent))
721 return gfs2_init_dirent(inode, dent, name, bh);
724 static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
725 struct buffer_head **bhp)
729 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
730 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
731 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
739 * get_leaf_nr - Get a leaf number associated with the index
740 * @dip: The GFS2 inode
744 * Returns: 0 on success, error code otherwise
747 static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
752 hash = gfs2_dir_get_hash_table(dip);
754 return PTR_ERR(hash);
755 *leaf_out = be64_to_cpu(*(hash + index));
759 static int get_first_leaf(struct gfs2_inode *dip, u32 index,
760 struct buffer_head **bh_out)
765 error = get_leaf_nr(dip, index, &leaf_no);
767 error = get_leaf(dip, leaf_no, bh_out);
772 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
773 const struct qstr *name,
775 struct buffer_head **pbh)
777 struct buffer_head *bh;
778 struct gfs2_dirent *dent;
779 struct gfs2_inode *ip = GFS2_I(inode);
782 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
783 struct gfs2_leaf *leaf;
784 unsigned hsize = 1 << ip->i_depth;
787 if (hsize * sizeof(u64) != i_size_read(inode)) {
788 gfs2_consist_inode(ip);
789 return ERR_PTR(-EIO);
792 index = name->hash >> (32 - ip->i_depth);
793 error = get_first_leaf(ip, index, &bh);
795 return ERR_PTR(error);
797 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
801 leaf = (struct gfs2_leaf *)bh->b_data;
802 ln = be64_to_cpu(leaf->lf_next);
807 error = get_leaf(ip, ln, &bh);
810 return error ? ERR_PTR(error) : NULL;
814 error = gfs2_meta_inode_buffer(ip, &bh);
816 return ERR_PTR(error);
817 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
819 if (unlikely(dent == NULL || IS_ERR(dent))) {
827 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
829 struct gfs2_inode *ip = GFS2_I(inode);
833 struct buffer_head *bh;
834 struct gfs2_leaf *leaf;
835 struct gfs2_dirent *dent;
836 struct qstr name = { .name = "" };
837 struct timespec tv = CURRENT_TIME;
839 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
842 bh = gfs2_meta_new(ip->i_gl, bn);
846 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
847 gfs2_trans_add_meta(ip->i_gl, bh);
848 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
849 leaf = (struct gfs2_leaf *)bh->b_data;
850 leaf->lf_depth = cpu_to_be16(depth);
851 leaf->lf_entries = 0;
852 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
854 leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
855 leaf->lf_dist = cpu_to_be32(1);
856 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
857 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
858 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
859 dent = (struct gfs2_dirent *)(leaf+1);
860 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
866 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
867 * @dip: The GFS2 inode
869 * Returns: 0 on success, error code otherwise
872 static int dir_make_exhash(struct inode *inode)
874 struct gfs2_inode *dip = GFS2_I(inode);
875 struct gfs2_sbd *sdp = GFS2_SB(inode);
876 struct gfs2_dirent *dent;
878 struct buffer_head *bh, *dibh;
879 struct gfs2_leaf *leaf;
886 error = gfs2_meta_inode_buffer(dip, &dibh);
890 /* Turn over a new leaf */
892 leaf = new_leaf(inode, &bh, 0);
897 gfs2_assert(sdp, dip->i_entries < (1 << 16));
898 leaf->lf_entries = cpu_to_be16(dip->i_entries);
902 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
903 sizeof(struct gfs2_dinode));
905 /* Find last entry */
908 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
909 sizeof(struct gfs2_leaf);
910 args.name = bh->b_data;
911 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
912 gfs2_dirent_last, &args, NULL);
921 return PTR_ERR(dent);
924 /* Adjust the last dirent's record length
925 (Remember that dent still points to the last entry.) */
927 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
928 sizeof(struct gfs2_dinode) -
929 sizeof(struct gfs2_leaf));
933 /* We're done with the new leaf block, now setup the new
936 gfs2_trans_add_meta(dip->i_gl, dibh);
937 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
939 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
941 for (x = sdp->sd_hash_ptrs; x--; lp++)
942 *lp = cpu_to_be64(bn);
944 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
945 gfs2_add_inode_blocks(&dip->i_inode, 1);
946 dip->i_diskflags |= GFS2_DIF_EXHASH;
948 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
951 gfs2_dinode_out(dip, dibh->b_data);
959 * dir_split_leaf - Split a leaf block into two
960 * @dip: The GFS2 inode
964 * Returns: 0 on success, error code on failure
967 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
969 struct gfs2_inode *dip = GFS2_I(inode);
970 struct buffer_head *nbh, *obh, *dibh;
971 struct gfs2_leaf *nleaf, *oleaf;
972 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
973 u32 start, len, half_len, divider;
980 index = name->hash >> (32 - dip->i_depth);
981 error = get_leaf_nr(dip, index, &leaf_no);
985 /* Get the old leaf block */
986 error = get_leaf(dip, leaf_no, &obh);
990 oleaf = (struct gfs2_leaf *)obh->b_data;
991 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
993 return 1; /* can't split */
996 gfs2_trans_add_meta(dip->i_gl, obh);
998 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
1003 bn = nbh->b_blocknr;
1005 /* Compute the start and len of leaf pointers in the hash table. */
1006 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
1007 half_len = len >> 1;
1009 printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
1010 gfs2_consist_inode(dip);
1015 start = (index & ~(len - 1));
1017 /* Change the pointers.
1018 Don't bother distinguishing stuffed from non-stuffed.
1019 This code is complicated enough already. */
1020 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1026 /* Change the pointers */
1027 for (x = 0; x < half_len; x++)
1028 lp[x] = cpu_to_be64(bn);
1030 gfs2_dir_hash_inval(dip);
1032 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1033 half_len * sizeof(u64));
1034 if (error != half_len * sizeof(u64)) {
1042 /* Compute the divider */
1043 divider = (start + half_len) << (32 - dip->i_depth);
1045 /* Copy the entries */
1046 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
1050 if (dirent_next(dip, obh, &next))
1053 if (!gfs2_dirent_sentinel(dent) &&
1054 be32_to_cpu(dent->de_hash) < divider) {
1056 str.name = (char*)(dent+1);
1057 str.len = be16_to_cpu(dent->de_name_len);
1058 str.hash = be32_to_cpu(dent->de_hash);
1059 new = gfs2_dirent_alloc(inode, nbh, &str);
1061 error = PTR_ERR(new);
1065 new->de_inum = dent->de_inum; /* No endian worries */
1066 new->de_type = dent->de_type; /* No endian worries */
1067 be16_add_cpu(&nleaf->lf_entries, 1);
1069 dirent_del(dip, obh, prev, dent);
1071 if (!oleaf->lf_entries)
1072 gfs2_consist_inode(dip);
1073 be16_add_cpu(&oleaf->lf_entries, -1);
1085 oleaf->lf_depth = nleaf->lf_depth;
1087 error = gfs2_meta_inode_buffer(dip, &dibh);
1088 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1089 gfs2_trans_add_meta(dip->i_gl, dibh);
1090 gfs2_add_inode_blocks(&dip->i_inode, 1);
1091 gfs2_dinode_out(dip, dibh->b_data);
1110 * dir_double_exhash - Double size of ExHash table
1111 * @dip: The GFS2 dinode
1113 * Returns: 0 on success, error code on failure
1116 static int dir_double_exhash(struct gfs2_inode *dip)
1118 struct buffer_head *dibh;
1126 hsize = 1 << dip->i_depth;
1127 hsize_bytes = hsize * sizeof(__be64);
1129 hc = gfs2_dir_get_hash_table(dip);
1133 hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
1135 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
1141 error = gfs2_meta_inode_buffer(dip, &dibh);
1145 for (x = 0; x < hsize; x++) {
1151 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1152 if (error != (hsize_bytes * 2))
1155 gfs2_dir_hash_inval(dip);
1156 dip->i_hash_cache = hc2;
1158 gfs2_dinode_out(dip, dibh->b_data);
1163 /* Replace original hash table & size */
1164 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1165 i_size_write(&dip->i_inode, hsize_bytes);
1166 gfs2_dinode_out(dip, dibh->b_data);
1169 if (is_vmalloc_addr(hc2))
1177 * compare_dents - compare directory entries by hash value
1181 * When comparing the hash entries of @a to @b:
1187 static int compare_dents(const void *a, const void *b)
1189 const struct gfs2_dirent *dent_a, *dent_b;
1193 dent_a = *(const struct gfs2_dirent **)a;
1194 hash_a = be32_to_cpu(dent_a->de_hash);
1196 dent_b = *(const struct gfs2_dirent **)b;
1197 hash_b = be32_to_cpu(dent_b->de_hash);
1199 if (hash_a > hash_b)
1201 else if (hash_a < hash_b)
1204 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1205 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1209 else if (len_a < len_b)
1212 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
1219 * do_filldir_main - read out directory entries
1220 * @dip: The GFS2 inode
1221 * @ctx: what to feed the entries to
1222 * @darr: an array of struct gfs2_dirent pointers to read
1223 * @entries: the number of entries in darr
1224 * @copied: pointer to int that's non-zero if a entry has been copied out
1226 * Jump through some hoops to make sure that if there are hash collsions,
1227 * they are read out at the beginning of a buffer. We want to minimize
1228 * the possibility that they will fall into different readdir buffers or
1229 * that someone will want to seek to that location.
1231 * Returns: errno, >0 if the actor tells you to stop
1234 static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
1235 const struct gfs2_dirent **darr, u32 entries,
1238 const struct gfs2_dirent *dent, *dent_next;
1243 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1245 dent_next = darr[0];
1246 off_next = be32_to_cpu(dent_next->de_hash);
1247 off_next = gfs2_disk_hash2offset(off_next);
1249 for (x = 0, y = 1; x < entries; x++, y++) {
1254 dent_next = darr[y];
1255 off_next = be32_to_cpu(dent_next->de_hash);
1256 off_next = gfs2_disk_hash2offset(off_next);
1262 if (off_next == off) {
1263 if (*copied && !run)
1274 if (!dir_emit(ctx, (const char *)(dent + 1),
1275 be16_to_cpu(dent->de_name_len),
1276 be64_to_cpu(dent->de_inum.no_addr),
1277 be16_to_cpu(dent->de_type)))
1283 /* Increment the ctx->pos by one, so the next time we come into the
1284 do_filldir fxn, we get the next entry instead of the last one in the
1292 static void *gfs2_alloc_sort_buffer(unsigned size)
1296 if (size < KMALLOC_MAX_SIZE)
1297 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1299 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1303 static void gfs2_free_sort_buffer(void *ptr)
1305 if (is_vmalloc_addr(ptr))
1311 static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1312 int *copied, unsigned *depth,
1315 struct gfs2_inode *ip = GFS2_I(inode);
1316 struct gfs2_sbd *sdp = GFS2_SB(inode);
1317 struct buffer_head *bh;
1318 struct gfs2_leaf *lf;
1319 unsigned entries = 0, entries2 = 0;
1320 unsigned leaves = 0;
1321 const struct gfs2_dirent **darr, *dent;
1322 struct dirent_gather g;
1323 struct buffer_head **larr;
1329 error = get_leaf(ip, lfn, &bh);
1332 lf = (struct gfs2_leaf *)bh->b_data;
1334 *depth = be16_to_cpu(lf->lf_depth);
1335 entries += be16_to_cpu(lf->lf_entries);
1337 lfn = be64_to_cpu(lf->lf_next);
1346 * The extra 99 entries are not normally used, but are a buffer
1347 * zone in case the number of entries in the leaf is corrupt.
1348 * 99 is the maximum number of entries that can fit in a single
1351 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
1354 darr = (const struct gfs2_dirent **)(larr + leaves);
1360 error = get_leaf(ip, lfn, &bh);
1363 lf = (struct gfs2_leaf *)bh->b_data;
1364 lfn = be64_to_cpu(lf->lf_next);
1365 if (lf->lf_entries) {
1366 entries2 += be16_to_cpu(lf->lf_entries);
1367 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1368 gfs2_dirent_gather, NULL, &g);
1369 error = PTR_ERR(dent);
1372 if (entries2 != g.offset) {
1373 fs_warn(sdp, "Number of entries corrupt in dir "
1374 "leaf %llu, entries2 (%u) != "
1376 (unsigned long long)bh->b_blocknr,
1377 entries2, g.offset);
1389 BUG_ON(entries2 != entries);
1390 error = do_filldir_main(ip, ctx, darr, entries, copied);
1392 for(i = 0; i < leaf; i++)
1394 gfs2_free_sort_buffer(larr);
1400 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
1402 * Note: we can't calculate each index like dir_e_read can because we don't
1403 * have the leaf, and therefore we don't have the depth, and therefore we
1404 * don't have the length. So we have to just read enough ahead to make up
1405 * for the loss of information.
1407 static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1408 struct file_ra_state *f_ra)
1410 struct gfs2_inode *ip = GFS2_I(inode);
1411 struct gfs2_glock *gl = ip->i_gl;
1412 struct buffer_head *bh;
1413 u64 blocknr = 0, last;
1416 /* First check if we've already read-ahead for the whole range. */
1417 if (index + MAX_RA_BLOCKS < f_ra->start)
1420 f_ra->start = max((pgoff_t)index, f_ra->start);
1421 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1422 if (f_ra->start >= hsize) /* if exceeded the hash table */
1426 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1428 if (blocknr == last)
1431 bh = gfs2_getbuf(gl, blocknr, 1);
1432 if (trylock_buffer(bh)) {
1433 if (buffer_uptodate(bh)) {
1438 bh->b_end_io = end_buffer_read_sync;
1439 submit_bh(READA | REQ_META, bh);
1447 * dir_e_read - Reads the entries from a directory into a filldir buffer
1448 * @dip: dinode pointer
1449 * @ctx: actor to feed the entries to
1454 static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1455 struct file_ra_state *f_ra)
1457 struct gfs2_inode *dip = GFS2_I(inode);
1465 hsize = 1 << dip->i_depth;
1466 hash = gfs2_dir_offset2hash(ctx->pos);
1467 index = hash >> (32 - dip->i_depth);
1469 if (dip->i_hash_cache == NULL)
1471 lp = gfs2_dir_get_hash_table(dip);
1475 gfs2_dir_readahead(inode, hsize, index, f_ra);
1477 while (index < hsize) {
1478 error = gfs2_dir_read_leaf(inode, ctx,
1480 be64_to_cpu(lp[index]));
1484 len = 1 << (dip->i_depth - depth);
1485 index = (index & ~(len - 1)) + len;
1493 int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1494 struct file_ra_state *f_ra)
1496 struct gfs2_inode *dip = GFS2_I(inode);
1497 struct gfs2_sbd *sdp = GFS2_SB(inode);
1498 struct dirent_gather g;
1499 const struct gfs2_dirent **darr, *dent;
1500 struct buffer_head *dibh;
1504 if (!dip->i_entries)
1507 if (dip->i_diskflags & GFS2_DIF_EXHASH)
1508 return dir_e_read(inode, ctx, f_ra);
1510 if (!gfs2_is_stuffed(dip)) {
1511 gfs2_consist_inode(dip);
1515 error = gfs2_meta_inode_buffer(dip, &dibh);
1520 /* 96 is max number of dirents which can be stuffed into an inode */
1521 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
1525 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1526 gfs2_dirent_gather, NULL, &g);
1528 error = PTR_ERR(dent);
1531 if (dip->i_entries != g.offset) {
1532 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
1533 "ip->i_entries (%u) != g.offset (%u)\n",
1534 (unsigned long long)dip->i_no_addr,
1540 error = do_filldir_main(dip, ctx, darr,
1541 dip->i_entries, &copied);
1555 * gfs2_dir_search - Search a directory
1556 * @dip: The GFS2 dir inode
1557 * @name: The name we are looking up
1558 * @fail_on_exist: Fail if the name exists rather than looking it up
1560 * This routine searches a directory for a file or another directory.
1561 * Assumes a glock is held on dip.
1566 struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1569 struct buffer_head *bh;
1570 struct gfs2_dirent *dent;
1571 u64 addr, formal_ino;
1574 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1577 return ERR_CAST(dent);
1578 dtype = be16_to_cpu(dent->de_type);
1579 addr = be64_to_cpu(dent->de_inum.no_addr);
1580 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
1583 return ERR_PTR(-EEXIST);
1584 return gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
1586 return ERR_PTR(-ENOENT);
1589 int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1590 const struct gfs2_inode *ip)
1592 struct buffer_head *bh;
1593 struct gfs2_dirent *dent;
1596 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1599 return PTR_ERR(dent);
1601 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1603 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1604 ip->i_no_formal_ino)
1606 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1607 be16_to_cpu(dent->de_type))) {
1608 gfs2_consist_inode(GFS2_I(dir));
1621 * dir_new_leaf - Add a new leaf onto hash chain
1622 * @inode: The directory
1623 * @name: The name we are adding
1625 * This adds a new dir leaf onto an existing leaf when there is not
1626 * enough space to add a new dir entry. This is a last resort after
1627 * we've expanded the hash table to max size and also split existing
1628 * leaf blocks, so it will only occur for very large directories.
1630 * The dist parameter is set to 1 for leaf blocks directly attached
1631 * to the hash table, 2 for one layer of indirection, 3 for two layers
1632 * etc. We are thus able to tell the difference between an old leaf
1633 * with dist set to zero (i.e. "don't know") and a new one where we
1634 * set this information for debug/fsck purposes.
1636 * Returns: 0 on success, or -ve on error
1639 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1641 struct buffer_head *bh, *obh;
1642 struct gfs2_inode *ip = GFS2_I(inode);
1643 struct gfs2_leaf *leaf, *oleaf;
1649 index = name->hash >> (32 - ip->i_depth);
1650 error = get_first_leaf(ip, index, &obh);
1655 oleaf = (struct gfs2_leaf *)obh->b_data;
1656 bn = be64_to_cpu(oleaf->lf_next);
1660 error = get_leaf(ip, bn, &obh);
1665 gfs2_trans_add_meta(ip->i_gl, obh);
1667 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1672 leaf->lf_dist = cpu_to_be32(dist);
1673 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1677 error = gfs2_meta_inode_buffer(ip, &bh);
1680 gfs2_trans_add_meta(ip->i_gl, bh);
1681 gfs2_add_inode_blocks(&ip->i_inode, 1);
1682 gfs2_dinode_out(ip, bh->b_data);
1688 * gfs2_dir_add - Add new filename into directory
1689 * @inode: The directory inode
1690 * @name: The new name
1691 * @nip: The GFS2 inode to be linked in to the directory
1692 * @da: The directory addition info
1694 * If the call to gfs2_diradd_alloc_required resulted in there being
1695 * no need to allocate any new directory blocks, then it will contain
1696 * a pointer to the directory entry and the bh in which it resides. We
1697 * can use that without having to repeat the search. If there was no
1698 * free space, then we must now create more space.
1700 * Returns: 0 on success, error code on failure
1703 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1704 const struct gfs2_inode *nip, struct gfs2_diradd *da)
1706 struct gfs2_inode *ip = GFS2_I(inode);
1707 struct buffer_head *bh = da->bh;
1708 struct gfs2_dirent *dent = da->dent;
1710 struct gfs2_leaf *leaf;
1714 if (da->bh == NULL) {
1715 dent = gfs2_dirent_search(inode, name,
1716 gfs2_dirent_find_space, &bh);
1720 return PTR_ERR(dent);
1721 dent = gfs2_init_dirent(inode, dent, name, bh);
1722 gfs2_inum_out(nip, dent);
1723 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
1725 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
1726 leaf = (struct gfs2_leaf *)bh->b_data;
1727 be16_add_cpu(&leaf->lf_entries, 1);
1728 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1729 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
1735 ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
1736 if (S_ISDIR(nip->i_inode.i_mode))
1737 inc_nlink(&ip->i_inode);
1738 mark_inode_dirty(inode);
1742 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
1743 error = dir_make_exhash(inode);
1748 error = dir_split_leaf(inode, name);
1753 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
1754 error = dir_double_exhash(ip);
1757 error = dir_split_leaf(inode, name);
1763 error = dir_new_leaf(inode, name);
1774 * gfs2_dir_del - Delete a directory entry
1775 * @dip: The GFS2 inode
1776 * @filename: The filename
1778 * Returns: 0 on success, error code on failure
1781 int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
1783 const struct qstr *name = &dentry->d_name;
1784 struct gfs2_dirent *dent, *prev = NULL;
1785 struct buffer_head *bh;
1786 struct timespec tv = CURRENT_TIME;
1788 /* Returns _either_ the entry (if its first in block) or the
1789 previous entry otherwise */
1790 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1792 gfs2_consist_inode(dip);
1796 gfs2_consist_inode(dip);
1797 return PTR_ERR(dent);
1799 /* If not first in block, adjust pointers accordingly */
1800 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1802 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1805 dirent_del(dip, bh, prev, dent);
1806 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1807 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1808 u16 entries = be16_to_cpu(leaf->lf_entries);
1810 gfs2_consist_inode(dip);
1811 leaf->lf_entries = cpu_to_be16(--entries);
1812 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1813 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
1817 if (!dip->i_entries)
1818 gfs2_consist_inode(dip);
1820 dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
1821 if (S_ISDIR(dentry->d_inode->i_mode))
1822 drop_nlink(&dip->i_inode);
1823 mark_inode_dirty(&dip->i_inode);
1829 * gfs2_dir_mvino - Change inode number of directory entry
1830 * @dip: The GFS2 inode
1834 * This routine changes the inode number of a directory entry. It's used
1835 * by rename to change ".." when a directory is moved.
1836 * Assumes a glock is held on dvp.
1841 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1842 const struct gfs2_inode *nip, unsigned int new_type)
1844 struct buffer_head *bh;
1845 struct gfs2_dirent *dent;
1848 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1850 gfs2_consist_inode(dip);
1854 return PTR_ERR(dent);
1856 gfs2_trans_add_meta(dip->i_gl, bh);
1857 gfs2_inum_out(nip, dent);
1858 dent->de_type = cpu_to_be16(new_type);
1860 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1862 error = gfs2_meta_inode_buffer(dip, &bh);
1865 gfs2_trans_add_meta(dip->i_gl, bh);
1868 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
1869 gfs2_dinode_out(dip, bh->b_data);
1875 * leaf_dealloc - Deallocate a directory leaf
1876 * @dip: the directory
1877 * @index: the hash table offset in the directory
1878 * @len: the number of pointers to this leaf
1879 * @leaf_no: the leaf number
1880 * @leaf_bh: buffer_head for the starting leaf
1881 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1886 static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1887 u64 leaf_no, struct buffer_head *leaf_bh,
1890 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1891 struct gfs2_leaf *tmp_leaf;
1892 struct gfs2_rgrp_list rlist;
1893 struct buffer_head *bh, *dibh;
1895 unsigned int rg_blocks = 0, l_blocks = 0;
1897 unsigned int x, size = len * sizeof(u64);
1900 error = gfs2_rindex_update(sdp);
1904 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1906 ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
1912 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1916 /* Count the number of leaves */
1919 for (blk = leaf_no; blk; blk = nblk) {
1920 if (blk != leaf_no) {
1921 error = get_leaf(dip, blk, &bh);
1925 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1926 nblk = be64_to_cpu(tmp_leaf->lf_next);
1930 gfs2_rlist_add(dip, &rlist, blk);
1934 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1936 for (x = 0; x < rlist.rl_rgrps; x++) {
1937 struct gfs2_rgrpd *rgd;
1938 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1939 rg_blocks += rgd->rd_length;
1942 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1946 error = gfs2_trans_begin(sdp,
1947 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1948 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1950 goto out_rg_gunlock;
1954 for (blk = leaf_no; blk; blk = nblk) {
1955 if (blk != leaf_no) {
1956 error = get_leaf(dip, blk, &bh);
1960 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1961 nblk = be64_to_cpu(tmp_leaf->lf_next);
1965 gfs2_free_meta(dip, blk, 1);
1966 gfs2_add_inode_blocks(&dip->i_inode, -1);
1969 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
1970 if (error != size) {
1976 error = gfs2_meta_inode_buffer(dip, &dibh);
1980 gfs2_trans_add_meta(dip->i_gl, dibh);
1981 /* On the last dealloc, make this a regular file in case we crash.
1982 (We don't want to free these blocks a second time.) */
1984 dip->i_inode.i_mode = S_IFREG;
1985 gfs2_dinode_out(dip, dibh->b_data);
1989 gfs2_trans_end(sdp);
1991 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1993 gfs2_rlist_free(&rlist);
1994 gfs2_quota_unhold(dip);
1996 if (is_vmalloc_addr(ht))
2004 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2005 * @dip: the directory
2007 * Dealloc all on-disk directory leaves to FREEMETA state
2008 * Change on-disk inode type to "regular file"
2013 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2015 struct buffer_head *bh;
2016 struct gfs2_leaf *leaf;
2018 u32 index = 0, next_index;
2021 int error = 0, last;
2023 hsize = 1 << dip->i_depth;
2025 lp = gfs2_dir_get_hash_table(dip);
2029 while (index < hsize) {
2030 leaf_no = be64_to_cpu(lp[index]);
2032 error = get_leaf(dip, leaf_no, &bh);
2035 leaf = (struct gfs2_leaf *)bh->b_data;
2036 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
2038 next_index = (index & ~(len - 1)) + len;
2039 last = ((next_index >= hsize) ? 1 : 0);
2040 error = leaf_dealloc(dip, index, len, leaf_no, bh,
2050 if (index != hsize) {
2051 gfs2_consist_inode(dip);
2061 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2062 * @ip: the file being written to
2063 * @filname: the filename that's going to be added
2064 * @da: The structure to return dir alloc info
2066 * Returns: 0 if ok, -ve on error
2069 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
2070 struct gfs2_diradd *da)
2072 struct gfs2_inode *ip = GFS2_I(inode);
2073 struct gfs2_sbd *sdp = GFS2_SB(inode);
2074 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
2075 struct gfs2_dirent *dent;
2076 struct buffer_head *bh;
2082 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
2084 da->nr_blocks = sdp->sd_max_dirres;
2085 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
2086 (GFS2_DIRENT_SIZE(name->len) < extra))
2091 return PTR_ERR(dent);