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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58 #include <linux/slab.h>
59 #include <linux/spinlock.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>
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81 #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
83 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
84 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
86 struct qstr gfs2_qdot __read_mostly;
87 struct qstr gfs2_qdotdot __read_mostly;
89 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
90 const struct qstr *name, void *opaque);
92 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
93 struct buffer_head **bhp)
95 struct buffer_head *bh;
97 bh = gfs2_meta_new(ip->i_gl, block);
98 gfs2_trans_add_meta(ip->i_gl, bh);
99 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
100 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
105 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
106 struct buffer_head **bhp)
108 struct buffer_head *bh;
111 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
114 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
122 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
123 unsigned int offset, unsigned int size)
125 struct buffer_head *dibh;
128 error = gfs2_meta_inode_buffer(ip, &dibh);
132 gfs2_trans_add_meta(ip->i_gl, dibh);
133 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
134 if (ip->i_inode.i_size < offset + size)
135 i_size_write(&ip->i_inode, offset + size);
136 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
137 gfs2_dinode_out(ip, 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 u64 offset, unsigned int size)
158 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
159 struct buffer_head *dibh;
170 if (gfs2_is_stuffed(ip) &&
171 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
172 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
175 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
178 if (gfs2_is_stuffed(ip)) {
179 error = gfs2_unstuff_dinode(ip, NULL);
185 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
187 while (copied < size) {
189 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_extent_map(&ip->i_inode, lblock, &new,
202 if (gfs2_assert_withdraw(sdp, dblock))
206 if (amount == sdp->sd_jbsize || new)
207 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
209 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
214 gfs2_trans_add_meta(ip->i_gl, bh);
215 memcpy(bh->b_data + o, buf, amount);
224 o = sizeof(struct gfs2_meta_header);
228 error = gfs2_meta_inode_buffer(ip, &dibh);
232 if (ip->i_inode.i_size < offset + copied)
233 i_size_write(&ip->i_inode, offset + copied);
234 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
236 gfs2_trans_add_meta(ip->i_gl, dibh);
237 gfs2_dinode_out(ip, dibh->b_data);
247 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
250 struct buffer_head *dibh;
253 error = gfs2_meta_inode_buffer(ip, &dibh);
255 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), 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 * @size: Amount of data to transfer
269 * Returns: The amount of data actually copied or the error
271 static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
274 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
281 if (gfs2_is_stuffed(ip))
282 return gfs2_dir_read_stuffed(ip, buf, size);
284 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
288 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
290 while (copied < size) {
292 struct buffer_head *bh;
295 amount = size - copied;
296 if (amount > sdp->sd_sb.sb_bsize - o)
297 amount = sdp->sd_sb.sb_bsize - o;
301 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
303 if (error || !dblock)
306 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
308 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
312 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
319 memcpy(buf, bh->b_data + o, amount);
321 buf += (amount/sizeof(__be64));
324 o = sizeof(struct gfs2_meta_header);
329 return (copied) ? copied : error;
333 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
334 * @ip: The inode in question
336 * Returns: The hash table or an error
339 static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
341 struct inode *inode = &ip->i_inode;
346 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
348 hc = ip->i_hash_cache;
352 hsize = 1 << ip->i_depth;
353 hsize *= sizeof(__be64);
354 if (hsize != i_size_read(&ip->i_inode)) {
355 gfs2_consist_inode(ip);
356 return ERR_PTR(-EIO);
359 hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
361 hc = __vmalloc(hsize, GFP_NOFS, PAGE_KERNEL);
364 return ERR_PTR(-ENOMEM);
366 ret = gfs2_dir_read_data(ip, hc, hsize);
368 if (is_vmalloc_addr(hc))
375 spin_lock(&inode->i_lock);
376 if (ip->i_hash_cache) {
377 if (is_vmalloc_addr(hc))
382 ip->i_hash_cache = hc;
384 spin_unlock(&inode->i_lock);
386 return ip->i_hash_cache;
390 * gfs2_dir_hash_inval - Invalidate dir hash
391 * @ip: The directory inode
393 * Must be called with an exclusive glock, or during glock invalidation.
395 void gfs2_dir_hash_inval(struct gfs2_inode *ip)
397 __be64 *hc = ip->i_hash_cache;
398 ip->i_hash_cache = NULL;
399 if (is_vmalloc_addr(hc))
405 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
407 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
410 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
411 const struct qstr *name, int ret)
413 if (!gfs2_dirent_sentinel(dent) &&
414 be32_to_cpu(dent->de_hash) == name->hash &&
415 be16_to_cpu(dent->de_name_len) == name->len &&
416 memcmp(dent+1, name->name, name->len) == 0)
421 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
422 const struct qstr *name,
425 return __gfs2_dirent_find(dent, name, 1);
428 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
429 const struct qstr *name,
432 return __gfs2_dirent_find(dent, name, 2);
436 * name->name holds ptr to start of block.
437 * name->len holds size of block.
439 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
440 const struct qstr *name,
443 const char *start = name->name;
444 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
445 if (name->len == (end - start))
450 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
451 const struct qstr *name,
454 unsigned required = GFS2_DIRENT_SIZE(name->len);
455 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
456 unsigned totlen = be16_to_cpu(dent->de_rec_len);
458 if (gfs2_dirent_sentinel(dent))
460 if (totlen - actual >= required)
465 struct dirent_gather {
466 const struct gfs2_dirent **pdent;
470 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
471 const struct qstr *name,
474 struct dirent_gather *g = opaque;
475 if (!gfs2_dirent_sentinel(dent)) {
476 g->pdent[g->offset++] = dent;
482 * Other possible things to check:
483 * - Inode located within filesystem size (and on valid block)
484 * - Valid directory entry type
485 * Not sure how heavy-weight we want to make this... could also check
486 * hash is correct for example, but that would take a lot of extra time.
487 * For now the most important thing is to check that the various sizes
490 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
491 unsigned int size, unsigned int len, int first)
493 const char *msg = "gfs2_dirent too small";
494 if (unlikely(size < sizeof(struct gfs2_dirent)))
496 msg = "gfs2_dirent misaligned";
497 if (unlikely(offset & 0x7))
499 msg = "gfs2_dirent points beyond end of block";
500 if (unlikely(offset + size > len))
502 msg = "zero inode number";
503 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
505 msg = "name length is greater than space in dirent";
506 if (!gfs2_dirent_sentinel(dent) &&
507 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
512 pr_warn("%s: %s (%s)\n",
513 __func__, msg, first ? "first in block" : "not first in block");
517 static int gfs2_dirent_offset(const void *buf)
519 const struct gfs2_meta_header *h = buf;
524 switch(be32_to_cpu(h->mh_type)) {
525 case GFS2_METATYPE_LF:
526 offset = sizeof(struct gfs2_leaf);
528 case GFS2_METATYPE_DI:
529 offset = sizeof(struct gfs2_dinode);
536 pr_warn("%s: wrong block type %u\n", __func__, be32_to_cpu(h->mh_type));
540 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
541 unsigned int len, gfs2_dscan_t scan,
542 const struct qstr *name,
545 struct gfs2_dirent *dent, *prev;
550 ret = gfs2_dirent_offset(buf);
557 size = be16_to_cpu(dent->de_rec_len);
558 if (gfs2_check_dirent(dent, offset, size, len, 1))
561 ret = scan(dent, name, opaque);
569 size = be16_to_cpu(dent->de_rec_len);
570 if (gfs2_check_dirent(dent, offset, size, len, 0))
580 return prev ? prev : dent;
587 gfs2_consist_inode(GFS2_I(inode));
588 return ERR_PTR(-EIO);
591 static int dirent_check_reclen(struct gfs2_inode *dip,
592 const struct gfs2_dirent *d, const void *end_p)
595 u16 rec_len = be16_to_cpu(d->de_rec_len);
597 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
605 gfs2_consist_inode(dip);
610 * dirent_next - Next dirent
611 * @dip: the directory
613 * @dent: Pointer to list of dirents
615 * Returns: 0 on success, error code otherwise
618 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
619 struct gfs2_dirent **dent)
621 struct gfs2_dirent *cur = *dent, *tmp;
622 char *bh_end = bh->b_data + bh->b_size;
625 ret = dirent_check_reclen(dip, cur, bh_end);
629 tmp = (void *)cur + ret;
630 ret = dirent_check_reclen(dip, tmp, bh_end);
634 /* Only the first dent could ever have de_inum.no_addr == 0 */
635 if (gfs2_dirent_sentinel(tmp)) {
636 gfs2_consist_inode(dip);
645 * dirent_del - Delete a dirent
646 * @dip: The GFS2 inode
648 * @prev: The previous dirent
649 * @cur: The current dirent
653 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
654 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
656 u16 cur_rec_len, prev_rec_len;
658 if (gfs2_dirent_sentinel(cur)) {
659 gfs2_consist_inode(dip);
663 gfs2_trans_add_meta(dip->i_gl, bh);
665 /* If there is no prev entry, this is the first entry in the block.
666 The de_rec_len is already as big as it needs to be. Just zero
667 out the inode number and return. */
670 cur->de_inum.no_addr = 0;
671 cur->de_inum.no_formal_ino = 0;
675 /* Combine this dentry with the previous one. */
677 prev_rec_len = be16_to_cpu(prev->de_rec_len);
678 cur_rec_len = be16_to_cpu(cur->de_rec_len);
680 if ((char *)prev + prev_rec_len != (char *)cur)
681 gfs2_consist_inode(dip);
682 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
683 gfs2_consist_inode(dip);
685 prev_rec_len += cur_rec_len;
686 prev->de_rec_len = cpu_to_be16(prev_rec_len);
690 * Takes a dent from which to grab space as an argument. Returns the
691 * newly created dent.
693 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
694 struct gfs2_dirent *dent,
695 const struct qstr *name,
696 struct buffer_head *bh)
698 struct gfs2_inode *ip = GFS2_I(inode);
699 struct gfs2_dirent *ndent;
700 unsigned offset = 0, totlen;
702 if (!gfs2_dirent_sentinel(dent))
703 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
704 totlen = be16_to_cpu(dent->de_rec_len);
705 BUG_ON(offset + name->len > totlen);
706 gfs2_trans_add_meta(ip->i_gl, bh);
707 ndent = (struct gfs2_dirent *)((char *)dent + offset);
708 dent->de_rec_len = cpu_to_be16(offset);
709 gfs2_qstr2dirent(name, totlen - offset, ndent);
713 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
714 struct buffer_head *bh,
715 const struct qstr *name)
717 struct gfs2_dirent *dent;
718 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
719 gfs2_dirent_find_space, name, NULL);
720 if (!dent || IS_ERR(dent))
722 return gfs2_init_dirent(inode, dent, name, bh);
725 static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
726 struct buffer_head **bhp)
730 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
731 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
732 /* pr_info("block num=%llu\n", leaf_no); */
740 * get_leaf_nr - Get a leaf number associated with the index
741 * @dip: The GFS2 inode
745 * Returns: 0 on success, error code otherwise
748 static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
753 hash = gfs2_dir_get_hash_table(dip);
755 return PTR_ERR(hash);
756 *leaf_out = be64_to_cpu(*(hash + index));
760 static int get_first_leaf(struct gfs2_inode *dip, u32 index,
761 struct buffer_head **bh_out)
766 error = get_leaf_nr(dip, index, &leaf_no);
768 error = get_leaf(dip, leaf_no, bh_out);
773 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
774 const struct qstr *name,
776 struct buffer_head **pbh)
778 struct buffer_head *bh;
779 struct gfs2_dirent *dent;
780 struct gfs2_inode *ip = GFS2_I(inode);
783 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
784 struct gfs2_leaf *leaf;
785 unsigned hsize = 1 << ip->i_depth;
788 if (hsize * sizeof(u64) != i_size_read(inode)) {
789 gfs2_consist_inode(ip);
790 return ERR_PTR(-EIO);
793 index = name->hash >> (32 - ip->i_depth);
794 error = get_first_leaf(ip, index, &bh);
796 return ERR_PTR(error);
798 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
802 leaf = (struct gfs2_leaf *)bh->b_data;
803 ln = be64_to_cpu(leaf->lf_next);
808 error = get_leaf(ip, ln, &bh);
811 return error ? ERR_PTR(error) : NULL;
815 error = gfs2_meta_inode_buffer(ip, &bh);
817 return ERR_PTR(error);
818 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
820 if (unlikely(dent == NULL || IS_ERR(dent))) {
828 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
830 struct gfs2_inode *ip = GFS2_I(inode);
834 struct buffer_head *bh;
835 struct gfs2_leaf *leaf;
836 struct gfs2_dirent *dent;
837 struct qstr name = { .name = "" };
838 struct timespec tv = CURRENT_TIME;
840 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
843 bh = gfs2_meta_new(ip->i_gl, bn);
847 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
848 gfs2_trans_add_meta(ip->i_gl, bh);
849 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
850 leaf = (struct gfs2_leaf *)bh->b_data;
851 leaf->lf_depth = cpu_to_be16(depth);
852 leaf->lf_entries = 0;
853 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
855 leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
856 leaf->lf_dist = cpu_to_be32(1);
857 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
858 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
859 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
860 dent = (struct gfs2_dirent *)(leaf+1);
861 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
867 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
868 * @dip: The GFS2 inode
870 * Returns: 0 on success, error code otherwise
873 static int dir_make_exhash(struct inode *inode)
875 struct gfs2_inode *dip = GFS2_I(inode);
876 struct gfs2_sbd *sdp = GFS2_SB(inode);
877 struct gfs2_dirent *dent;
879 struct buffer_head *bh, *dibh;
880 struct gfs2_leaf *leaf;
887 error = gfs2_meta_inode_buffer(dip, &dibh);
891 /* Turn over a new leaf */
893 leaf = new_leaf(inode, &bh, 0);
898 gfs2_assert(sdp, dip->i_entries < (1 << 16));
899 leaf->lf_entries = cpu_to_be16(dip->i_entries);
903 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
904 sizeof(struct gfs2_dinode));
906 /* Find last entry */
909 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
910 sizeof(struct gfs2_leaf);
911 args.name = bh->b_data;
912 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
913 gfs2_dirent_last, &args, NULL);
922 return PTR_ERR(dent);
925 /* Adjust the last dirent's record length
926 (Remember that dent still points to the last entry.) */
928 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
929 sizeof(struct gfs2_dinode) -
930 sizeof(struct gfs2_leaf));
934 /* We're done with the new leaf block, now setup the new
937 gfs2_trans_add_meta(dip->i_gl, dibh);
938 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
940 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
942 for (x = sdp->sd_hash_ptrs; x--; lp++)
943 *lp = cpu_to_be64(bn);
945 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
946 gfs2_add_inode_blocks(&dip->i_inode, 1);
947 dip->i_diskflags |= GFS2_DIF_EXHASH;
949 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
952 gfs2_dinode_out(dip, dibh->b_data);
960 * dir_split_leaf - Split a leaf block into two
961 * @dip: The GFS2 inode
965 * Returns: 0 on success, error code on failure
968 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
970 struct gfs2_inode *dip = GFS2_I(inode);
971 struct buffer_head *nbh, *obh, *dibh;
972 struct gfs2_leaf *nleaf, *oleaf;
973 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
974 u32 start, len, half_len, divider;
981 index = name->hash >> (32 - dip->i_depth);
982 error = get_leaf_nr(dip, index, &leaf_no);
986 /* Get the old leaf block */
987 error = get_leaf(dip, leaf_no, &obh);
991 oleaf = (struct gfs2_leaf *)obh->b_data;
992 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
994 return 1; /* can't split */
997 gfs2_trans_add_meta(dip->i_gl, obh);
999 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
1004 bn = nbh->b_blocknr;
1006 /* Compute the start and len of leaf pointers in the hash table. */
1007 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
1008 half_len = len >> 1;
1010 pr_warn("i_depth %u lf_depth %u index %u\n",
1011 dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
1012 gfs2_consist_inode(dip);
1017 start = (index & ~(len - 1));
1019 /* Change the pointers.
1020 Don't bother distinguishing stuffed from non-stuffed.
1021 This code is complicated enough already. */
1022 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1028 /* Change the pointers */
1029 for (x = 0; x < half_len; x++)
1030 lp[x] = cpu_to_be64(bn);
1032 gfs2_dir_hash_inval(dip);
1034 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1035 half_len * sizeof(u64));
1036 if (error != half_len * sizeof(u64)) {
1044 /* Compute the divider */
1045 divider = (start + half_len) << (32 - dip->i_depth);
1047 /* Copy the entries */
1048 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
1052 if (dirent_next(dip, obh, &next))
1055 if (!gfs2_dirent_sentinel(dent) &&
1056 be32_to_cpu(dent->de_hash) < divider) {
1058 str.name = (char*)(dent+1);
1059 str.len = be16_to_cpu(dent->de_name_len);
1060 str.hash = be32_to_cpu(dent->de_hash);
1061 new = gfs2_dirent_alloc(inode, nbh, &str);
1063 error = PTR_ERR(new);
1067 new->de_inum = dent->de_inum; /* No endian worries */
1068 new->de_type = dent->de_type; /* No endian worries */
1069 be16_add_cpu(&nleaf->lf_entries, 1);
1071 dirent_del(dip, obh, prev, dent);
1073 if (!oleaf->lf_entries)
1074 gfs2_consist_inode(dip);
1075 be16_add_cpu(&oleaf->lf_entries, -1);
1087 oleaf->lf_depth = nleaf->lf_depth;
1089 error = gfs2_meta_inode_buffer(dip, &dibh);
1090 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1091 gfs2_trans_add_meta(dip->i_gl, dibh);
1092 gfs2_add_inode_blocks(&dip->i_inode, 1);
1093 gfs2_dinode_out(dip, dibh->b_data);
1112 * dir_double_exhash - Double size of ExHash table
1113 * @dip: The GFS2 dinode
1115 * Returns: 0 on success, error code on failure
1118 static int dir_double_exhash(struct gfs2_inode *dip)
1120 struct buffer_head *dibh;
1128 hsize = 1 << dip->i_depth;
1129 hsize_bytes = hsize * sizeof(__be64);
1131 hc = gfs2_dir_get_hash_table(dip);
1135 hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
1137 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
1143 error = gfs2_meta_inode_buffer(dip, &dibh);
1147 for (x = 0; x < hsize; x++) {
1153 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1154 if (error != (hsize_bytes * 2))
1157 gfs2_dir_hash_inval(dip);
1158 dip->i_hash_cache = hc2;
1160 gfs2_dinode_out(dip, dibh->b_data);
1165 /* Replace original hash table & size */
1166 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1167 i_size_write(&dip->i_inode, hsize_bytes);
1168 gfs2_dinode_out(dip, dibh->b_data);
1171 if (is_vmalloc_addr(hc2))
1179 * compare_dents - compare directory entries by hash value
1183 * When comparing the hash entries of @a to @b:
1189 static int compare_dents(const void *a, const void *b)
1191 const struct gfs2_dirent *dent_a, *dent_b;
1195 dent_a = *(const struct gfs2_dirent **)a;
1196 hash_a = be32_to_cpu(dent_a->de_hash);
1198 dent_b = *(const struct gfs2_dirent **)b;
1199 hash_b = be32_to_cpu(dent_b->de_hash);
1201 if (hash_a > hash_b)
1203 else if (hash_a < hash_b)
1206 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1207 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1211 else if (len_a < len_b)
1214 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
1221 * do_filldir_main - read out directory entries
1222 * @dip: The GFS2 inode
1223 * @ctx: what to feed the entries to
1224 * @darr: an array of struct gfs2_dirent pointers to read
1225 * @entries: the number of entries in darr
1226 * @copied: pointer to int that's non-zero if a entry has been copied out
1228 * Jump through some hoops to make sure that if there are hash collsions,
1229 * they are read out at the beginning of a buffer. We want to minimize
1230 * the possibility that they will fall into different readdir buffers or
1231 * that someone will want to seek to that location.
1233 * Returns: errno, >0 if the actor tells you to stop
1236 static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
1237 const struct gfs2_dirent **darr, u32 entries,
1240 const struct gfs2_dirent *dent, *dent_next;
1245 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1247 dent_next = darr[0];
1248 off_next = be32_to_cpu(dent_next->de_hash);
1249 off_next = gfs2_disk_hash2offset(off_next);
1251 for (x = 0, y = 1; x < entries; x++, y++) {
1256 dent_next = darr[y];
1257 off_next = be32_to_cpu(dent_next->de_hash);
1258 off_next = gfs2_disk_hash2offset(off_next);
1264 if (off_next == off) {
1265 if (*copied && !run)
1276 if (!dir_emit(ctx, (const char *)(dent + 1),
1277 be16_to_cpu(dent->de_name_len),
1278 be64_to_cpu(dent->de_inum.no_addr),
1279 be16_to_cpu(dent->de_type)))
1285 /* Increment the ctx->pos by one, so the next time we come into the
1286 do_filldir fxn, we get the next entry instead of the last one in the
1294 static void *gfs2_alloc_sort_buffer(unsigned size)
1298 if (size < KMALLOC_MAX_SIZE)
1299 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1301 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1305 static void gfs2_free_sort_buffer(void *ptr)
1307 if (is_vmalloc_addr(ptr))
1313 static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1314 int *copied, unsigned *depth,
1317 struct gfs2_inode *ip = GFS2_I(inode);
1318 struct gfs2_sbd *sdp = GFS2_SB(inode);
1319 struct buffer_head *bh;
1320 struct gfs2_leaf *lf;
1321 unsigned entries = 0, entries2 = 0;
1322 unsigned leaves = 0;
1323 const struct gfs2_dirent **darr, *dent;
1324 struct dirent_gather g;
1325 struct buffer_head **larr;
1331 error = get_leaf(ip, lfn, &bh);
1334 lf = (struct gfs2_leaf *)bh->b_data;
1336 *depth = be16_to_cpu(lf->lf_depth);
1337 entries += be16_to_cpu(lf->lf_entries);
1339 lfn = be64_to_cpu(lf->lf_next);
1348 * The extra 99 entries are not normally used, but are a buffer
1349 * zone in case the number of entries in the leaf is corrupt.
1350 * 99 is the maximum number of entries that can fit in a single
1353 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
1356 darr = (const struct gfs2_dirent **)(larr + leaves);
1362 error = get_leaf(ip, lfn, &bh);
1365 lf = (struct gfs2_leaf *)bh->b_data;
1366 lfn = be64_to_cpu(lf->lf_next);
1367 if (lf->lf_entries) {
1368 entries2 += be16_to_cpu(lf->lf_entries);
1369 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1370 gfs2_dirent_gather, NULL, &g);
1371 error = PTR_ERR(dent);
1374 if (entries2 != g.offset) {
1375 fs_warn(sdp, "Number of entries corrupt in dir "
1376 "leaf %llu, entries2 (%u) != "
1378 (unsigned long long)bh->b_blocknr,
1379 entries2, g.offset);
1391 BUG_ON(entries2 != entries);
1392 error = do_filldir_main(ip, ctx, darr, entries, copied);
1394 for(i = 0; i < leaf; i++)
1396 gfs2_free_sort_buffer(larr);
1402 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
1404 * Note: we can't calculate each index like dir_e_read can because we don't
1405 * have the leaf, and therefore we don't have the depth, and therefore we
1406 * don't have the length. So we have to just read enough ahead to make up
1407 * for the loss of information.
1409 static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1410 struct file_ra_state *f_ra)
1412 struct gfs2_inode *ip = GFS2_I(inode);
1413 struct gfs2_glock *gl = ip->i_gl;
1414 struct buffer_head *bh;
1415 u64 blocknr = 0, last;
1418 /* First check if we've already read-ahead for the whole range. */
1419 if (index + MAX_RA_BLOCKS < f_ra->start)
1422 f_ra->start = max((pgoff_t)index, f_ra->start);
1423 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1424 if (f_ra->start >= hsize) /* if exceeded the hash table */
1428 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1430 if (blocknr == last)
1433 bh = gfs2_getbuf(gl, blocknr, 1);
1434 if (trylock_buffer(bh)) {
1435 if (buffer_uptodate(bh)) {
1440 bh->b_end_io = end_buffer_read_sync;
1441 submit_bh(READA | REQ_META, bh);
1449 * dir_e_read - Reads the entries from a directory into a filldir buffer
1450 * @dip: dinode pointer
1451 * @ctx: actor to feed the entries to
1456 static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1457 struct file_ra_state *f_ra)
1459 struct gfs2_inode *dip = GFS2_I(inode);
1467 hsize = 1 << dip->i_depth;
1468 hash = gfs2_dir_offset2hash(ctx->pos);
1469 index = hash >> (32 - dip->i_depth);
1471 if (dip->i_hash_cache == NULL)
1473 lp = gfs2_dir_get_hash_table(dip);
1477 gfs2_dir_readahead(inode, hsize, index, f_ra);
1479 while (index < hsize) {
1480 error = gfs2_dir_read_leaf(inode, ctx,
1482 be64_to_cpu(lp[index]));
1486 len = 1 << (dip->i_depth - depth);
1487 index = (index & ~(len - 1)) + len;
1495 int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1496 struct file_ra_state *f_ra)
1498 struct gfs2_inode *dip = GFS2_I(inode);
1499 struct gfs2_sbd *sdp = GFS2_SB(inode);
1500 struct dirent_gather g;
1501 const struct gfs2_dirent **darr, *dent;
1502 struct buffer_head *dibh;
1506 if (!dip->i_entries)
1509 if (dip->i_diskflags & GFS2_DIF_EXHASH)
1510 return dir_e_read(inode, ctx, f_ra);
1512 if (!gfs2_is_stuffed(dip)) {
1513 gfs2_consist_inode(dip);
1517 error = gfs2_meta_inode_buffer(dip, &dibh);
1522 /* 96 is max number of dirents which can be stuffed into an inode */
1523 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
1527 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1528 gfs2_dirent_gather, NULL, &g);
1530 error = PTR_ERR(dent);
1533 if (dip->i_entries != g.offset) {
1534 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
1535 "ip->i_entries (%u) != g.offset (%u)\n",
1536 (unsigned long long)dip->i_no_addr,
1542 error = do_filldir_main(dip, ctx, darr,
1543 dip->i_entries, &copied);
1557 * gfs2_dir_search - Search a directory
1558 * @dip: The GFS2 dir inode
1559 * @name: The name we are looking up
1560 * @fail_on_exist: Fail if the name exists rather than looking it up
1562 * This routine searches a directory for a file or another directory.
1563 * Assumes a glock is held on dip.
1568 struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1571 struct buffer_head *bh;
1572 struct gfs2_dirent *dent;
1573 u64 addr, formal_ino;
1576 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1579 return ERR_CAST(dent);
1580 dtype = be16_to_cpu(dent->de_type);
1581 addr = be64_to_cpu(dent->de_inum.no_addr);
1582 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
1585 return ERR_PTR(-EEXIST);
1586 return gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
1588 return ERR_PTR(-ENOENT);
1591 int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1592 const struct gfs2_inode *ip)
1594 struct buffer_head *bh;
1595 struct gfs2_dirent *dent;
1598 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1601 return PTR_ERR(dent);
1603 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1605 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1606 ip->i_no_formal_ino)
1608 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1609 be16_to_cpu(dent->de_type))) {
1610 gfs2_consist_inode(GFS2_I(dir));
1623 * dir_new_leaf - Add a new leaf onto hash chain
1624 * @inode: The directory
1625 * @name: The name we are adding
1627 * This adds a new dir leaf onto an existing leaf when there is not
1628 * enough space to add a new dir entry. This is a last resort after
1629 * we've expanded the hash table to max size and also split existing
1630 * leaf blocks, so it will only occur for very large directories.
1632 * The dist parameter is set to 1 for leaf blocks directly attached
1633 * to the hash table, 2 for one layer of indirection, 3 for two layers
1634 * etc. We are thus able to tell the difference between an old leaf
1635 * with dist set to zero (i.e. "don't know") and a new one where we
1636 * set this information for debug/fsck purposes.
1638 * Returns: 0 on success, or -ve on error
1641 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1643 struct buffer_head *bh, *obh;
1644 struct gfs2_inode *ip = GFS2_I(inode);
1645 struct gfs2_leaf *leaf, *oleaf;
1651 index = name->hash >> (32 - ip->i_depth);
1652 error = get_first_leaf(ip, index, &obh);
1657 oleaf = (struct gfs2_leaf *)obh->b_data;
1658 bn = be64_to_cpu(oleaf->lf_next);
1662 error = get_leaf(ip, bn, &obh);
1667 gfs2_trans_add_meta(ip->i_gl, obh);
1669 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1674 leaf->lf_dist = cpu_to_be32(dist);
1675 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1679 error = gfs2_meta_inode_buffer(ip, &bh);
1682 gfs2_trans_add_meta(ip->i_gl, bh);
1683 gfs2_add_inode_blocks(&ip->i_inode, 1);
1684 gfs2_dinode_out(ip, bh->b_data);
1689 static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
1691 u64 where = ip->i_no_addr + 1;
1692 if (ip->i_eattr == where)
1698 * gfs2_dir_add - Add new filename into directory
1699 * @inode: The directory inode
1700 * @name: The new name
1701 * @nip: The GFS2 inode to be linked in to the directory
1702 * @da: The directory addition info
1704 * If the call to gfs2_diradd_alloc_required resulted in there being
1705 * no need to allocate any new directory blocks, then it will contain
1706 * a pointer to the directory entry and the bh in which it resides. We
1707 * can use that without having to repeat the search. If there was no
1708 * free space, then we must now create more space.
1710 * Returns: 0 on success, error code on failure
1713 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1714 const struct gfs2_inode *nip, struct gfs2_diradd *da)
1716 struct gfs2_inode *ip = GFS2_I(inode);
1717 struct buffer_head *bh = da->bh;
1718 struct gfs2_dirent *dent = da->dent;
1720 struct gfs2_leaf *leaf;
1724 if (da->bh == NULL) {
1725 dent = gfs2_dirent_search(inode, name,
1726 gfs2_dirent_find_space, &bh);
1730 return PTR_ERR(dent);
1731 dent = gfs2_init_dirent(inode, dent, name, bh);
1732 gfs2_inum_out(nip, dent);
1733 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
1734 dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
1736 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
1737 leaf = (struct gfs2_leaf *)bh->b_data;
1738 be16_add_cpu(&leaf->lf_entries, 1);
1739 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1740 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
1746 ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
1747 if (S_ISDIR(nip->i_inode.i_mode))
1748 inc_nlink(&ip->i_inode);
1749 mark_inode_dirty(inode);
1753 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
1754 error = dir_make_exhash(inode);
1759 error = dir_split_leaf(inode, name);
1764 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
1765 error = dir_double_exhash(ip);
1768 error = dir_split_leaf(inode, name);
1774 error = dir_new_leaf(inode, name);
1785 * gfs2_dir_del - Delete a directory entry
1786 * @dip: The GFS2 inode
1787 * @filename: The filename
1789 * Returns: 0 on success, error code on failure
1792 int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
1794 const struct qstr *name = &dentry->d_name;
1795 struct gfs2_dirent *dent, *prev = NULL;
1796 struct buffer_head *bh;
1797 struct timespec tv = CURRENT_TIME;
1799 /* Returns _either_ the entry (if its first in block) or the
1800 previous entry otherwise */
1801 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1803 gfs2_consist_inode(dip);
1807 gfs2_consist_inode(dip);
1808 return PTR_ERR(dent);
1810 /* If not first in block, adjust pointers accordingly */
1811 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1813 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1816 dirent_del(dip, bh, prev, dent);
1817 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1818 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1819 u16 entries = be16_to_cpu(leaf->lf_entries);
1821 gfs2_consist_inode(dip);
1822 leaf->lf_entries = cpu_to_be16(--entries);
1823 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1824 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
1828 if (!dip->i_entries)
1829 gfs2_consist_inode(dip);
1831 dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
1832 if (S_ISDIR(dentry->d_inode->i_mode))
1833 drop_nlink(&dip->i_inode);
1834 mark_inode_dirty(&dip->i_inode);
1840 * gfs2_dir_mvino - Change inode number of directory entry
1841 * @dip: The GFS2 inode
1845 * This routine changes the inode number of a directory entry. It's used
1846 * by rename to change ".." when a directory is moved.
1847 * Assumes a glock is held on dvp.
1852 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1853 const struct gfs2_inode *nip, unsigned int new_type)
1855 struct buffer_head *bh;
1856 struct gfs2_dirent *dent;
1859 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1861 gfs2_consist_inode(dip);
1865 return PTR_ERR(dent);
1867 gfs2_trans_add_meta(dip->i_gl, bh);
1868 gfs2_inum_out(nip, dent);
1869 dent->de_type = cpu_to_be16(new_type);
1871 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
1873 error = gfs2_meta_inode_buffer(dip, &bh);
1876 gfs2_trans_add_meta(dip->i_gl, bh);
1879 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
1880 gfs2_dinode_out(dip, bh->b_data);
1886 * leaf_dealloc - Deallocate a directory leaf
1887 * @dip: the directory
1888 * @index: the hash table offset in the directory
1889 * @len: the number of pointers to this leaf
1890 * @leaf_no: the leaf number
1891 * @leaf_bh: buffer_head for the starting leaf
1892 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1897 static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1898 u64 leaf_no, struct buffer_head *leaf_bh,
1901 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1902 struct gfs2_leaf *tmp_leaf;
1903 struct gfs2_rgrp_list rlist;
1904 struct buffer_head *bh, *dibh;
1906 unsigned int rg_blocks = 0, l_blocks = 0;
1908 unsigned int x, size = len * sizeof(u64);
1911 error = gfs2_rindex_update(sdp);
1915 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1917 ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
1923 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1927 /* Count the number of leaves */
1930 for (blk = leaf_no; blk; blk = nblk) {
1931 if (blk != leaf_no) {
1932 error = get_leaf(dip, blk, &bh);
1936 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1937 nblk = be64_to_cpu(tmp_leaf->lf_next);
1941 gfs2_rlist_add(dip, &rlist, blk);
1945 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1947 for (x = 0; x < rlist.rl_rgrps; x++) {
1948 struct gfs2_rgrpd *rgd;
1949 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1950 rg_blocks += rgd->rd_length;
1953 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1957 error = gfs2_trans_begin(sdp,
1958 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1959 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1961 goto out_rg_gunlock;
1965 for (blk = leaf_no; blk; blk = nblk) {
1966 if (blk != leaf_no) {
1967 error = get_leaf(dip, blk, &bh);
1971 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1972 nblk = be64_to_cpu(tmp_leaf->lf_next);
1976 gfs2_free_meta(dip, blk, 1);
1977 gfs2_add_inode_blocks(&dip->i_inode, -1);
1980 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
1981 if (error != size) {
1987 error = gfs2_meta_inode_buffer(dip, &dibh);
1991 gfs2_trans_add_meta(dip->i_gl, dibh);
1992 /* On the last dealloc, make this a regular file in case we crash.
1993 (We don't want to free these blocks a second time.) */
1995 dip->i_inode.i_mode = S_IFREG;
1996 gfs2_dinode_out(dip, dibh->b_data);
2000 gfs2_trans_end(sdp);
2002 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
2004 gfs2_rlist_free(&rlist);
2005 gfs2_quota_unhold(dip);
2007 if (is_vmalloc_addr(ht))
2015 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2016 * @dip: the directory
2018 * Dealloc all on-disk directory leaves to FREEMETA state
2019 * Change on-disk inode type to "regular file"
2024 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2026 struct buffer_head *bh;
2027 struct gfs2_leaf *leaf;
2029 u32 index = 0, next_index;
2032 int error = 0, last;
2034 hsize = 1 << dip->i_depth;
2036 lp = gfs2_dir_get_hash_table(dip);
2040 while (index < hsize) {
2041 leaf_no = be64_to_cpu(lp[index]);
2043 error = get_leaf(dip, leaf_no, &bh);
2046 leaf = (struct gfs2_leaf *)bh->b_data;
2047 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
2049 next_index = (index & ~(len - 1)) + len;
2050 last = ((next_index >= hsize) ? 1 : 0);
2051 error = leaf_dealloc(dip, index, len, leaf_no, bh,
2061 if (index != hsize) {
2062 gfs2_consist_inode(dip);
2072 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2073 * @ip: the file being written to
2074 * @filname: the filename that's going to be added
2075 * @da: The structure to return dir alloc info
2077 * Returns: 0 if ok, -ve on error
2080 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
2081 struct gfs2_diradd *da)
2083 struct gfs2_inode *ip = GFS2_I(inode);
2084 struct gfs2_sbd *sdp = GFS2_SB(inode);
2085 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
2086 struct gfs2_dirent *dent;
2087 struct buffer_head *bh;
2093 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
2095 da->nr_blocks = sdp->sd_max_dirres;
2096 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
2097 (GFS2_DIRENT_SIZE(name->len) < extra))
2102 return PTR_ERR(dent);