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 <asm/semaphore.h>
74 #define IS_LEAF 1 /* Hashed (leaf) directory */
75 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
78 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
79 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
81 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
82 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
85 typedef int (*leaf_call_t) (struct gfs2_inode *dip,
86 uint32_t index, uint32_t len, uint64_t leaf_no,
89 int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
90 struct buffer_head **bhp)
92 struct buffer_head *bh;
96 bh = gfs2_meta_new(ip->i_gl, block);
97 gfs2_trans_add_bh(ip->i_gl, bh, 1);
98 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
99 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
101 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
104 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
116 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
117 unsigned int offset, unsigned int size)
120 struct buffer_head *dibh;
123 error = gfs2_meta_inode_buffer(ip, &dibh);
127 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
128 memcpy(dibh->b_data + offset + sizeof(struct gfs2_inode), buf, size);
129 if (ip->i_di.di_size < offset + size)
130 ip->i_di.di_size = offset + size;
131 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
132 gfs2_dinode_out(&ip->i_di, dibh->b_data);
142 * gfs2_dir_write_data - Write directory information to the inode
143 * @ip: The GFS2 inode
144 * @buf: The buffer containing information to be written
145 * @offset: The file offset to start writing at
146 * @size: The amount of data to write
148 * Returns: The number of bytes correctly written or error code
150 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
151 uint64_t offset, unsigned int size)
153 struct gfs2_sbd *sdp = ip->i_sbd;
154 struct buffer_head *dibh;
155 uint64_t lblock, dblock;
164 if (gfs2_is_stuffed(ip) &&
165 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
166 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset, size);
168 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
171 if (gfs2_is_stuffed(ip)) {
172 error = gfs2_unstuff_dinode(ip, NULL, NULL);
178 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
180 while (copied < size) {
182 struct buffer_head *bh;
185 amount = size - copied;
186 if (amount > sdp->sd_sb.sb_bsize - o)
187 amount = sdp->sd_sb.sb_bsize - o;
191 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
195 if (gfs2_assert_withdraw(sdp, dblock))
199 error = gfs2_dir_get_buffer(ip, dblock, (amount == sdp->sd_jbsize) ? 1 : new, &bh);
203 gfs2_trans_add_bh(ip->i_gl, bh, 1);
204 memcpy(bh->b_data + o, buf, amount);
214 o = sizeof(struct gfs2_meta_header);
218 error = gfs2_meta_inode_buffer(ip, &dibh);
222 if (ip->i_di.di_size < offset + copied)
223 ip->i_di.di_size = offset + copied;
224 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
226 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
227 gfs2_dinode_out(&ip->i_di, dibh->b_data);
237 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
238 unsigned int offset, unsigned int size)
240 struct buffer_head *dibh;
243 error = gfs2_meta_inode_buffer(ip, &dibh);
245 offset += sizeof(struct gfs2_dinode);
246 memcpy(buf, dibh->b_data + offset, size);
250 return (error) ? error : size;
255 * gfs2_dir_read_data - Read a data from a directory inode
256 * @ip: The GFS2 Inode
257 * @buf: The buffer to place result into
258 * @offset: File offset to begin jdata_readng from
259 * @size: Amount of data to transfer
261 * Returns: The amount of data actually copied or the error
263 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
264 uint64_t offset, unsigned int size)
266 struct gfs2_sbd *sdp = ip->i_sbd;
267 uint64_t lblock, dblock;
273 if (offset >= ip->i_di.di_size)
276 if ((offset + size) > ip->i_di.di_size)
277 size = ip->i_di.di_size - offset;
282 if (gfs2_is_stuffed(ip))
283 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset, size);
285 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
289 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
291 while (copied < size) {
293 struct buffer_head *bh;
296 amount = size - copied;
297 if (amount > sdp->sd_sb.sb_bsize - o)
298 amount = sdp->sd_sb.sb_bsize - o;
302 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
308 gfs2_meta_ra(ip->i_gl, dblock, extlen);
311 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
319 memcpy(buf, bh->b_data + o, amount);
327 o = sizeof(struct gfs2_meta_header);
332 return (copied) ? copied : error;
336 * int gfs2_filecmp - Compare two filenames
337 * @file1: The first filename
338 * @file2: The second filename
339 * @len_of_file2: The length of the second file
341 * This routine compares two filenames and returns 1 if they are equal.
343 * Returns: 1 if the files are the same, otherwise 0.
346 int gfs2_filecmp(struct qstr *file1, char *file2, int len_of_file2)
348 if (file1->len != len_of_file2)
350 if (memcmp(file1->name, file2, file1->len))
356 * dirent_first - Return the first dirent
357 * @dip: the directory
359 * @dent: Pointer to list of dirents
361 * return first dirent whether bh points to leaf or stuffed dinode
363 * Returns: IS_LEAF, IS_DINODE, or -errno
366 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
367 struct gfs2_dirent **dent)
369 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
371 if (be16_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
372 if (gfs2_meta_check(dip->i_sbd, bh))
374 *dent = (struct gfs2_dirent *)(bh->b_data +
375 sizeof(struct gfs2_leaf));
378 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
380 *dent = (struct gfs2_dirent *)(bh->b_data +
381 sizeof(struct gfs2_dinode));
387 * dirent_next - Next dirent
388 * @dip: the directory
390 * @dent: Pointer to list of dirents
392 * Returns: 0 on success, error code otherwise
395 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
396 struct gfs2_dirent **dent)
398 struct gfs2_dirent *tmp, *cur;
400 uint16_t cur_rec_len;
403 bh_end = bh->b_data + bh->b_size;
404 cur_rec_len = be16_to_cpu(cur->de_rec_len);
406 if ((char *)cur + cur_rec_len >= bh_end) {
407 if ((char *)cur + cur_rec_len > bh_end) {
408 gfs2_consist_inode(dip);
414 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
416 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
417 gfs2_consist_inode(dip);
421 if (cur_rec_len == 0) {
422 gfs2_consist_inode(dip);
426 /* Only the first dent could ever have de_inum.no_addr == 0 */
427 if (!tmp->de_inum.no_addr) {
428 gfs2_consist_inode(dip);
438 * dirent_del - Delete a dirent
439 * @dip: The GFS2 inode
441 * @prev: The previous dirent
442 * @cur: The current dirent
446 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
447 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
449 uint16_t cur_rec_len, prev_rec_len;
451 if (!cur->de_inum.no_addr) {
452 gfs2_consist_inode(dip);
456 gfs2_trans_add_bh(dip->i_gl, bh, 1);
458 /* If there is no prev entry, this is the first entry in the block.
459 The de_rec_len is already as big as it needs to be. Just zero
460 out the inode number and return. */
463 cur->de_inum.no_addr = 0; /* No endianess worries */
467 /* Combine this dentry with the previous one. */
469 prev_rec_len = be16_to_cpu(prev->de_rec_len);
470 cur_rec_len = be16_to_cpu(cur->de_rec_len);
472 if ((char *)prev + prev_rec_len != (char *)cur)
473 gfs2_consist_inode(dip);
474 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
475 gfs2_consist_inode(dip);
477 prev_rec_len += cur_rec_len;
478 prev->de_rec_len = cpu_to_be16(prev_rec_len);
482 * gfs2_dirent_alloc - Allocate a directory entry
483 * @dip: The GFS2 inode
485 * @name_len: The length of the name
486 * @dent_out: Pointer to list of dirents
488 * Returns: 0 on success, error code otherwise
491 int gfs2_dirent_alloc(struct gfs2_inode *dip, struct buffer_head *bh,
492 int name_len, struct gfs2_dirent **dent_out)
494 struct gfs2_dirent *dent, *new;
495 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
496 unsigned int entries = 0, offset = 0;
499 type = dirent_first(dip, bh, &dent);
503 if (type == IS_LEAF) {
504 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
505 entries = be16_to_cpu(leaf->lf_entries);
506 offset = sizeof(struct gfs2_leaf);
508 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
509 entries = be32_to_cpu(dinode->di_entries);
510 offset = sizeof(struct gfs2_dinode);
514 if (dent->de_inum.no_addr) {
515 gfs2_consist_inode(dip);
519 gfs2_trans_add_bh(dip->i_gl, bh, 1);
521 dent->de_rec_len = cpu_to_be16(bh->b_size - offset);
522 dent->de_name_len = cpu_to_be16(name_len);
529 uint16_t cur_rec_len;
530 uint16_t cur_name_len;
532 cur_rec_len = be16_to_cpu(dent->de_rec_len);
533 cur_name_len = be16_to_cpu(dent->de_name_len);
535 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
536 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len)) {
537 gfs2_trans_add_bh(dip->i_gl, bh, 1);
539 if (dent->de_inum.no_addr) {
540 new = (struct gfs2_dirent *)((char *)dent +
541 GFS2_DIRENT_SIZE(cur_name_len));
542 memset(new, 0, sizeof(struct gfs2_dirent));
544 new->de_rec_len = cpu_to_be16(cur_rec_len - GFS2_DIRENT_SIZE(cur_name_len));
545 new->de_name_len = cpu_to_be16(name_len);
547 dent->de_rec_len = cpu_to_be16(cur_rec_len - be16_to_cpu(new->de_rec_len));
553 dent->de_name_len = cpu_to_be16(name_len);
558 } while (dirent_next(dip, bh, &dent) == 0);
564 * dirent_fits - See if we can fit a entry in this buffer
565 * @dip: The GFS2 inode
567 * @name_len: The length of the name
569 * Returns: 1 if it can fit, 0 otherwise
572 static int dirent_fits(struct gfs2_inode *dip, struct buffer_head *bh,
575 struct gfs2_dirent *dent;
576 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
577 unsigned int entries = 0;
580 type = dirent_first(dip, bh, &dent);
584 if (type == IS_LEAF) {
585 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
586 entries = be16_to_cpu(leaf->lf_entries);
588 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
589 entries = be32_to_cpu(dinode->di_entries);
596 uint16_t cur_rec_len;
597 uint32_t cur_name_len;
599 cur_rec_len = be16_to_cpu(dent->de_rec_len);
600 cur_name_len = be16_to_cpu(dent->de_name_len);
602 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
603 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len))
605 } while (dirent_next(dip, bh, &dent) == 0);
610 static int leaf_search(struct gfs2_inode *dip, struct buffer_head *bh,
611 struct qstr *filename, struct gfs2_dirent **dent_out,
612 struct gfs2_dirent **dent_prev)
615 struct gfs2_dirent *dent, *prev = NULL;
616 unsigned int entries = 0;
619 type = dirent_first(dip, bh, &dent);
623 if (type == IS_LEAF) {
624 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
625 entries = be16_to_cpu(leaf->lf_entries);
626 } else if (type == IS_DINODE) {
627 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
628 entries = be32_to_cpu(dinode->di_entries);
631 hash = gfs2_disk_hash(filename->name, filename->len);
634 if (!dent->de_inum.no_addr) {
639 if (be32_to_cpu(dent->de_hash) == hash &&
640 gfs2_filecmp(filename, (char *)(dent + 1),
641 be16_to_cpu(dent->de_name_len))) {
650 } while (dirent_next(dip, bh, &dent) == 0);
655 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
656 struct buffer_head **bhp)
660 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
661 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
668 * get_leaf_nr - Get a leaf number associated with the index
669 * @dip: The GFS2 inode
673 * Returns: 0 on success, error code otherwise
676 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
682 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
683 index * sizeof(uint64_t),
685 if (error != sizeof(uint64_t))
686 return (error < 0) ? error : -EIO;
688 *leaf_out = be64_to_cpu(leaf_no);
693 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
694 struct buffer_head **bh_out)
699 error = get_leaf_nr(dip, index, &leaf_no);
701 error = get_leaf(dip, leaf_no, bh_out);
706 static int get_next_leaf(struct gfs2_inode *dip, struct buffer_head *bh_in,
707 struct buffer_head **bh_out)
709 struct gfs2_leaf *leaf;
712 leaf = (struct gfs2_leaf *)bh_in->b_data;
717 error = get_leaf(dip, be64_to_cpu(leaf->lf_next), bh_out);
722 static int linked_leaf_search(struct gfs2_inode *dip, struct qstr *filename,
723 struct gfs2_dirent **dent_out,
724 struct gfs2_dirent **dent_prev,
725 struct buffer_head **bh_out)
727 struct buffer_head *bh = NULL, *bh_next;
728 uint32_t hsize, index;
732 hsize = 1 << dip->i_di.di_depth;
733 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
734 gfs2_consist_inode(dip);
738 /* Figure out the address of the leaf node. */
740 hash = gfs2_disk_hash(filename->name, filename->len);
741 index = hash >> (32 - dip->i_di.di_depth);
743 error = get_first_leaf(dip, index, &bh_next);
754 error = leaf_search(dip, bh, filename, dent_out, dent_prev);
768 error = get_next_leaf(dip, bh, &bh_next);
778 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
779 * @dip: The GFS2 inode
781 * Returns: 0 on success, error code otherwise
784 static int dir_make_exhash(struct gfs2_inode *dip)
786 struct gfs2_sbd *sdp = dip->i_sbd;
787 struct gfs2_dirent *dent;
788 struct buffer_head *bh, *dibh;
789 struct gfs2_leaf *leaf;
795 error = gfs2_meta_inode_buffer(dip, &dibh);
799 /* Allocate a new block for the first leaf node */
801 bn = gfs2_alloc_meta(dip);
803 /* Turn over a new leaf */
805 bh = gfs2_meta_new(dip->i_gl, bn);
806 gfs2_trans_add_bh(dip->i_gl, bh, 1);
807 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
808 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
810 /* Fill in the leaf structure */
812 leaf = (struct gfs2_leaf *)bh->b_data;
814 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
816 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
817 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
821 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
822 sizeof(struct gfs2_dinode));
824 /* Find last entry */
827 dirent_first(dip, bh, &dent);
830 if (!dent->de_inum.no_addr)
832 if (++x == dip->i_di.di_entries)
835 while (dirent_next(dip, bh, &dent) == 0);
837 /* Adjust the last dirent's record length
838 (Remember that dent still points to the last entry.) */
840 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
841 sizeof(struct gfs2_dinode) -
842 sizeof(struct gfs2_leaf));
846 /* We're done with the new leaf block, now setup the new
849 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
850 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
852 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
854 for (x = sdp->sd_hash_ptrs; x--; lp++)
855 *lp = cpu_to_be64(bn);
857 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
858 dip->i_di.di_blocks++;
859 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
860 dip->i_di.di_payload_format = 0;
862 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
863 dip->i_di.di_depth = y;
865 gfs2_dinode_out(&dip->i_di, dibh->b_data);
873 * dir_split_leaf - Split a leaf block into two
874 * @dip: The GFS2 inode
878 * Returns: 0 on success, error code on failure
881 static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index,
884 struct buffer_head *nbh, *obh, *dibh;
885 struct gfs2_leaf *nleaf, *oleaf;
886 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
887 uint32_t start, len, half_len, divider;
893 /* Allocate the new leaf block */
895 bn = gfs2_alloc_meta(dip);
897 /* Get the new leaf block */
899 nbh = gfs2_meta_new(dip->i_gl, bn);
900 gfs2_trans_add_bh(dip->i_gl, nbh, 1);
901 gfs2_metatype_set(nbh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
902 gfs2_buffer_clear_tail(nbh, sizeof(struct gfs2_meta_header));
904 nleaf = (struct gfs2_leaf *)nbh->b_data;
906 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
908 /* Get the old leaf block */
910 error = get_leaf(dip, leaf_no, &obh);
914 gfs2_trans_add_bh(dip->i_gl, obh, 1);
916 oleaf = (struct gfs2_leaf *)obh->b_data;
918 /* Compute the start and len of leaf pointers in the hash table. */
920 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
923 gfs2_consist_inode(dip);
928 start = (index & ~(len - 1));
930 /* Change the pointers.
931 Don't bother distinguishing stuffed from non-stuffed.
932 This code is complicated enough already. */
934 lp = kcalloc(half_len, sizeof(uint64_t), GFP_KERNEL | __GFP_NOFAIL);
936 error = gfs2_dir_read_data(dip, (char *)lp, start * sizeof(uint64_t),
937 half_len * sizeof(uint64_t));
938 if (error != half_len * sizeof(uint64_t)) {
944 /* Change the pointers */
946 for (x = 0; x < half_len; x++)
947 lp[x] = cpu_to_be64(bn);
949 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
950 half_len * sizeof(uint64_t));
951 if (error != half_len * sizeof(uint64_t)) {
959 /* Compute the divider */
961 divider = (start + half_len) << (32 - dip->i_di.di_depth);
963 /* Copy the entries */
965 dirent_first(dip, obh, &dent);
969 if (dirent_next(dip, obh, &next))
972 if (dent->de_inum.no_addr &&
973 be32_to_cpu(dent->de_hash) < divider) {
974 name_len = be16_to_cpu(dent->de_name_len);
976 gfs2_dirent_alloc(dip, nbh, name_len, &new);
978 new->de_inum = dent->de_inum; /* No endian worries */
979 new->de_hash = dent->de_hash; /* No endian worries */
980 new->de_type = dent->de_type; /* No endian worries */
981 memcpy((char *)(new + 1), (char *)(dent + 1),
984 nleaf->lf_entries = be16_to_cpu(nleaf->lf_entries)+1;
985 nleaf->lf_entries = cpu_to_be16(nleaf->lf_entries);
987 dirent_del(dip, obh, prev, dent);
989 if (!oleaf->lf_entries)
990 gfs2_consist_inode(dip);
991 oleaf->lf_entries = be16_to_cpu(oleaf->lf_entries)-1;
992 oleaf->lf_entries = cpu_to_be16(oleaf->lf_entries);
1005 /* If none of the entries got moved into the new leaf,
1006 artificially fill in the first entry. */
1009 gfs2_dirent_alloc(dip, nbh, 0, &new);
1010 new->de_inum.no_addr = 0;
1013 oleaf->lf_depth = be16_to_cpu(oleaf->lf_depth) + 1;
1014 oleaf->lf_depth = cpu_to_be16(oleaf->lf_depth);
1015 nleaf->lf_depth = oleaf->lf_depth;
1017 error = gfs2_meta_inode_buffer(dip, &dibh);
1018 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
1019 dip->i_di.di_blocks++;
1020 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1041 * dir_double_exhash - Double size of ExHash table
1042 * @dip: The GFS2 dinode
1044 * Returns: 0 on success, error code on failure
1047 static int dir_double_exhash(struct gfs2_inode *dip)
1049 struct gfs2_sbd *sdp = dip->i_sbd;
1050 struct buffer_head *dibh;
1053 uint64_t *from, *to;
1058 hsize = 1 << dip->i_di.di_depth;
1059 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1060 gfs2_consist_inode(dip);
1064 /* Allocate both the "from" and "to" buffers in one big chunk */
1066 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1068 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1069 error = gfs2_dir_read_data(dip, (char *)buf,
1070 block * sdp->sd_hash_bsize,
1071 sdp->sd_hash_bsize);
1072 if (error != sdp->sd_hash_bsize) {
1079 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1081 for (x = sdp->sd_hash_ptrs; x--; from++) {
1082 *to++ = *from; /* No endianess worries */
1086 error = gfs2_dir_write_data(dip,
1087 (char *)buf + sdp->sd_hash_bsize,
1088 block * sdp->sd_sb.sb_bsize,
1089 sdp->sd_sb.sb_bsize);
1090 if (error != sdp->sd_sb.sb_bsize) {
1099 error = gfs2_meta_inode_buffer(dip, &dibh);
1100 if (!gfs2_assert_withdraw(sdp, !error)) {
1101 dip->i_di.di_depth++;
1102 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1115 * compare_dents - compare directory entries by hash value
1119 * When comparing the hash entries of @a to @b:
1125 static int compare_dents(const void *a, const void *b)
1127 struct gfs2_dirent *dent_a, *dent_b;
1128 uint32_t hash_a, hash_b;
1131 dent_a = *(struct gfs2_dirent **)a;
1132 hash_a = dent_a->de_hash;
1133 hash_a = be32_to_cpu(hash_a);
1135 dent_b = *(struct gfs2_dirent **)b;
1136 hash_b = dent_b->de_hash;
1137 hash_b = be32_to_cpu(hash_b);
1139 if (hash_a > hash_b)
1141 else if (hash_a < hash_b)
1144 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1145 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1149 else if (len_a < len_b)
1152 ret = memcmp((char *)(dent_a + 1),
1153 (char *)(dent_b + 1),
1161 * do_filldir_main - read out directory entries
1162 * @dip: The GFS2 inode
1163 * @offset: The offset in the file to read from
1164 * @opaque: opaque data to pass to filldir
1165 * @filldir: The function to pass entries to
1166 * @darr: an array of struct gfs2_dirent pointers to read
1167 * @entries: the number of entries in darr
1168 * @copied: pointer to int that's non-zero if a entry has been copied out
1170 * Jump through some hoops to make sure that if there are hash collsions,
1171 * they are read out at the beginning of a buffer. We want to minimize
1172 * the possibility that they will fall into different readdir buffers or
1173 * that someone will want to seek to that location.
1175 * Returns: errno, >0 on exception from filldir
1178 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1179 void *opaque, gfs2_filldir_t filldir,
1180 struct gfs2_dirent **darr, uint32_t entries,
1183 struct gfs2_dirent *dent, *dent_next;
1184 struct gfs2_inum inum;
1185 uint64_t off, off_next;
1190 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1192 dent_next = darr[0];
1193 off_next = be32_to_cpu(dent_next->de_hash);
1194 off_next = gfs2_disk_hash2offset(off_next);
1196 for (x = 0, y = 1; x < entries; x++, y++) {
1201 dent_next = darr[y];
1202 off_next = be32_to_cpu(dent_next->de_hash);
1203 off_next = gfs2_disk_hash2offset(off_next);
1209 if (off_next == off) {
1210 if (*copied && !run)
1221 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1223 error = filldir(opaque, (char *)(dent + 1),
1224 be16_to_cpu(dent->de_name_len),
1226 be16_to_cpu(dent->de_type));
1233 /* Increment the *offset by one, so the next time we come into the
1234 do_filldir fxn, we get the next entry instead of the last one in the
1243 * do_filldir_single - Read directory entries out of a single block
1244 * @dip: The GFS2 inode
1245 * @offset: The offset in the file to read from
1246 * @opaque: opaque data to pass to filldir
1247 * @filldir: The function to pass entries to
1249 * @entries: the number of entries in the block
1250 * @copied: pointer to int that's non-zero if a entry has been copied out
1252 * Returns: errno, >0 on exception from filldir
1255 static int do_filldir_single(struct gfs2_inode *dip, uint64_t *offset,
1256 void *opaque, gfs2_filldir_t filldir,
1257 struct buffer_head *bh, uint32_t entries,
1260 struct gfs2_dirent **darr;
1261 struct gfs2_dirent *de;
1268 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1272 dirent_first(dip, bh, &de);
1274 if (!de->de_inum.no_addr)
1277 gfs2_consist_inode(dip);
1283 while (dirent_next(dip, bh, &de) == 0);
1286 gfs2_consist_inode(dip);
1291 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1301 * do_filldir_multi - Read directory entries out of a linked leaf list
1302 * @dip: The GFS2 inode
1303 * @offset: The offset in the file to read from
1304 * @opaque: opaque data to pass to filldir
1305 * @filldir: The function to pass entries to
1306 * @bh: the first leaf in the list
1307 * @copied: pointer to int that's non-zero if a entry has been copied out
1309 * Returns: errno, >0 on exception from filldir
1312 static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1313 void *opaque, gfs2_filldir_t filldir,
1314 struct buffer_head *bh, int *copied)
1316 struct buffer_head **larr = NULL;
1317 struct gfs2_dirent **darr;
1318 struct gfs2_leaf *leaf;
1319 struct buffer_head *tmp_bh;
1320 struct gfs2_dirent *de;
1321 unsigned int entries, e = 0;
1322 unsigned int leaves = 0, l = 0;
1327 /* Count leaves and entries */
1329 leaf = (struct gfs2_leaf *)bh->b_data;
1330 entries = be16_to_cpu(leaf->lf_entries);
1334 ln = be64_to_cpu(ln);
1336 error = get_leaf(dip, ln, &tmp_bh);
1340 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1341 if (leaf->lf_entries) {
1342 entries += be16_to_cpu(leaf->lf_entries);
1354 larr = kcalloc(leaves, sizeof(struct buffer_head *),GFP_KERNEL);
1359 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1365 leaf = (struct gfs2_leaf *)bh->b_data;
1366 if (leaf->lf_entries) {
1367 dirent_first(dip, bh, &de);
1369 if (!de->de_inum.no_addr)
1372 gfs2_consist_inode(dip);
1378 while (dirent_next(dip, bh, &de) == 0);
1383 ln = be64_to_cpu(ln);
1385 error = get_leaf(dip, ln, &tmp_bh);
1389 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1390 if (leaf->lf_entries) {
1391 dirent_first(dip, tmp_bh, &de);
1393 if (!de->de_inum.no_addr)
1396 gfs2_consist_inode(dip);
1402 while (dirent_next(dip, tmp_bh, &de) == 0);
1413 if (gfs2_assert_withdraw(dip->i_sbd, l == leaves)) {
1418 gfs2_consist_inode(dip);
1423 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1428 for (x = 0; x < l; x++)
1436 * dir_e_search - Search exhash (leaf) dir for inode matching name
1437 * @dip: The GFS2 inode
1438 * @filename: Filename string
1439 * @inode: If non-NULL, function fills with formal inode # and block address
1440 * @type: If non-NULL, function fills with DT_... dinode type
1445 static int dir_e_search(struct gfs2_inode *dip, struct qstr *filename,
1446 struct gfs2_inum *inum, unsigned int *type)
1448 struct buffer_head *bh;
1449 struct gfs2_dirent *dent;
1452 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1457 gfs2_inum_in(inum, (char *)&dent->de_inum);
1459 *type = be16_to_cpu(dent->de_type);
1466 static int dir_e_add(struct gfs2_inode *dip, struct qstr *filename,
1467 struct gfs2_inum *inum, unsigned int type)
1469 struct buffer_head *bh, *nbh, *dibh;
1470 struct gfs2_leaf *leaf, *nleaf;
1471 struct gfs2_dirent *dent;
1472 uint32_t hsize, index;
1474 uint64_t leaf_no, bn;
1478 hsize = 1 << dip->i_di.di_depth;
1479 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1480 gfs2_consist_inode(dip);
1484 /* Figure out the address of the leaf node. */
1486 hash = gfs2_disk_hash(filename->name, filename->len);
1487 index = hash >> (32 - dip->i_di.di_depth);
1489 error = get_leaf_nr(dip, index, &leaf_no);
1493 /* Add entry to the leaf */
1496 error = get_leaf(dip, leaf_no, &bh);
1500 leaf = (struct gfs2_leaf *)bh->b_data;
1502 if (gfs2_dirent_alloc(dip, bh, filename->len, &dent)) {
1504 if (be16_to_cpu(leaf->lf_depth) < dip->i_di.di_depth) {
1505 /* Can we split the leaf? */
1509 error = dir_split_leaf(dip, index, leaf_no);
1515 } else if (dip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1516 /* Can we double the hash table? */
1520 error = dir_double_exhash(dip);
1526 } else if (leaf->lf_next) {
1527 /* Can we try the next leaf in the list? */
1528 leaf_no = be64_to_cpu(leaf->lf_next);
1533 /* Create a new leaf and add it to the list. */
1535 bn = gfs2_alloc_meta(dip);
1537 nbh = gfs2_meta_new(dip->i_gl, bn);
1538 gfs2_trans_add_bh(dip->i_gl, nbh, 1);
1539 gfs2_metatype_set(nbh,
1542 gfs2_buffer_clear_tail(nbh,
1543 sizeof(struct gfs2_meta_header));
1545 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1546 leaf->lf_next = cpu_to_be64(bn);
1548 nleaf = (struct gfs2_leaf *)nbh->b_data;
1549 nleaf->lf_depth = leaf->lf_depth;
1550 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
1552 gfs2_dirent_alloc(dip, nbh, filename->len,
1555 dip->i_di.di_blocks++;
1564 /* If the gfs2_dirent_alloc() succeeded, it pinned the "bh" */
1566 gfs2_inum_out(inum, (char *)&dent->de_inum);
1567 dent->de_hash = cpu_to_be32(hash);
1568 dent->de_type = cpu_to_be16(type);
1569 memcpy((char *)(dent + 1), filename->name, filename->len);
1571 leaf->lf_entries = be16_to_cpu(leaf->lf_entries) + 1;
1572 leaf->lf_entries = cpu_to_be16(leaf->lf_entries);
1576 error = gfs2_meta_inode_buffer(dip, &dibh);
1580 dip->i_di.di_entries++;
1581 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1583 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1584 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1593 static int dir_e_del(struct gfs2_inode *dip, struct qstr *filename)
1595 struct buffer_head *bh, *dibh;
1596 struct gfs2_dirent *dent, *prev;
1597 struct gfs2_leaf *leaf;
1598 unsigned int entries;
1601 error = linked_leaf_search(dip, filename, &dent, &prev, &bh);
1602 if (error == -ENOENT) {
1603 gfs2_consist_inode(dip);
1609 dirent_del(dip, bh, prev, dent); /* Pins bh */
1611 leaf = (struct gfs2_leaf *)bh->b_data;
1612 entries = be16_to_cpu(leaf->lf_entries);
1614 gfs2_consist_inode(dip);
1616 leaf->lf_entries = cpu_to_be16(entries);
1620 error = gfs2_meta_inode_buffer(dip, &dibh);
1624 if (!dip->i_di.di_entries)
1625 gfs2_consist_inode(dip);
1626 dip->i_di.di_entries--;
1627 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1629 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1630 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1637 * dir_e_read - Reads the entries from a directory into a filldir buffer
1638 * @dip: dinode pointer
1639 * @offset: the hash of the last entry read shifted to the right once
1640 * @opaque: buffer for the filldir function to fill
1641 * @filldir: points to the filldir function to use
1646 static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1647 gfs2_filldir_t filldir)
1649 struct gfs2_sbd *sdp = dip->i_sbd;
1650 struct buffer_head *bh;
1651 struct gfs2_leaf leaf;
1652 uint32_t hsize, len;
1653 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1654 uint32_t hash, index;
1659 hsize = 1 << dip->i_di.di_depth;
1660 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1661 gfs2_consist_inode(dip);
1665 hash = gfs2_dir_offset2hash(*offset);
1666 index = hash >> (32 - dip->i_di.di_depth);
1668 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1672 while (index < hsize) {
1673 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1674 ht_offset = index - lp_offset;
1676 if (ht_offset_cur != ht_offset) {
1677 error = gfs2_dir_read_data(dip, (char *)lp,
1678 ht_offset * sizeof(uint64_t),
1679 sdp->sd_hash_bsize);
1680 if (error != sdp->sd_hash_bsize) {
1685 ht_offset_cur = ht_offset;
1688 error = get_leaf(dip, be64_to_cpu(lp[lp_offset]), &bh);
1692 gfs2_leaf_in(&leaf, bh->b_data);
1695 error = do_filldir_multi(dip, offset, opaque, filldir,
1698 error = do_filldir_single(dip, offset, opaque, filldir,
1699 bh, leaf.lf_entries, &copied);
1709 len = 1 << (dip->i_di.di_depth - leaf.lf_depth);
1710 index = (index & ~(len - 1)) + len;
1719 static int dir_e_mvino(struct gfs2_inode *dip, struct qstr *filename,
1720 struct gfs2_inum *inum, unsigned int new_type)
1722 struct buffer_head *bh, *dibh;
1723 struct gfs2_dirent *dent;
1726 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1727 if (error == -ENOENT) {
1728 gfs2_consist_inode(dip);
1734 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1736 gfs2_inum_out(inum, (char *)&dent->de_inum);
1737 dent->de_type = cpu_to_be16(new_type);
1741 error = gfs2_meta_inode_buffer(dip, &dibh);
1745 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1747 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1748 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1755 * dir_l_search - Search linear (stuffed dinode) dir for inode matching name
1756 * @dip: The GFS2 inode
1757 * @filename: Filename string
1758 * @inode: If non-NULL, function fills with formal inode # and block address
1759 * @type: If non-NULL, function fills with DT_... dinode type
1764 static int dir_l_search(struct gfs2_inode *dip, struct qstr *filename,
1765 struct gfs2_inum *inum, unsigned int *type)
1767 struct buffer_head *dibh;
1768 struct gfs2_dirent *dent;
1771 if (!gfs2_is_stuffed(dip)) {
1772 gfs2_consist_inode(dip);
1776 error = gfs2_meta_inode_buffer(dip, &dibh);
1780 error = leaf_search(dip, dibh, filename, &dent, NULL);
1783 gfs2_inum_in(inum, (char *)&dent->de_inum);
1785 *type = be16_to_cpu(dent->de_type);
1793 static int dir_l_add(struct gfs2_inode *dip, struct qstr *filename,
1794 struct gfs2_inum *inum, unsigned int type)
1796 struct buffer_head *dibh;
1797 struct gfs2_dirent *dent;
1800 if (!gfs2_is_stuffed(dip)) {
1801 gfs2_consist_inode(dip);
1805 error = gfs2_meta_inode_buffer(dip, &dibh);
1809 if (gfs2_dirent_alloc(dip, dibh, filename->len, &dent)) {
1812 error = dir_make_exhash(dip);
1814 error = dir_e_add(dip, filename, inum, type);
1819 /* gfs2_dirent_alloc() pins */
1821 gfs2_inum_out(inum, (char *)&dent->de_inum);
1822 dent->de_hash = gfs2_disk_hash(filename->name, filename->len);
1823 dent->de_hash = cpu_to_be32(dent->de_hash);
1824 dent->de_type = cpu_to_be16(type);
1825 memcpy((char *)(dent + 1), filename->name, filename->len);
1827 dip->i_di.di_entries++;
1828 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1830 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1836 static int dir_l_del(struct gfs2_inode *dip, struct qstr *filename)
1838 struct buffer_head *dibh;
1839 struct gfs2_dirent *dent, *prev;
1842 if (!gfs2_is_stuffed(dip)) {
1843 gfs2_consist_inode(dip);
1847 error = gfs2_meta_inode_buffer(dip, &dibh);
1851 error = leaf_search(dip, dibh, filename, &dent, &prev);
1852 if (error == -ENOENT) {
1853 gfs2_consist_inode(dip);
1860 dirent_del(dip, dibh, prev, dent);
1862 /* dirent_del() pins */
1864 if (!dip->i_di.di_entries)
1865 gfs2_consist_inode(dip);
1866 dip->i_di.di_entries--;
1868 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1870 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1878 static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1879 gfs2_filldir_t filldir)
1881 struct buffer_head *dibh;
1885 if (!gfs2_is_stuffed(dip)) {
1886 gfs2_consist_inode(dip);
1890 if (!dip->i_di.di_entries)
1893 error = gfs2_meta_inode_buffer(dip, &dibh);
1897 error = do_filldir_single(dip, offset,
1899 dibh, dip->i_di.di_entries,
1909 static int dir_l_mvino(struct gfs2_inode *dip, struct qstr *filename,
1910 struct gfs2_inum *inum, unsigned int new_type)
1912 struct buffer_head *dibh;
1913 struct gfs2_dirent *dent;
1916 if (!gfs2_is_stuffed(dip)) {
1917 gfs2_consist_inode(dip);
1921 error = gfs2_meta_inode_buffer(dip, &dibh);
1925 error = leaf_search(dip, dibh, filename, &dent, NULL);
1926 if (error == -ENOENT) {
1927 gfs2_consist_inode(dip);
1934 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1936 gfs2_inum_out(inum, (char *)&dent->de_inum);
1937 dent->de_type = cpu_to_be16(new_type);
1939 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1941 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1950 * gfs2_dir_search - Search a directory
1951 * @dip: The GFS2 inode
1955 * This routine searches a directory for a file or another directory.
1956 * Assumes a glock is held on dip.
1961 int gfs2_dir_search(struct gfs2_inode *dip, struct qstr *filename,
1962 struct gfs2_inum *inum, unsigned int *type)
1966 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1967 error = dir_e_search(dip, filename, inum, type);
1969 error = dir_l_search(dip, filename, inum, type);
1975 * gfs2_dir_add - Add new filename into directory
1976 * @dip: The GFS2 inode
1977 * @filename: The new name
1978 * @inode: The inode number of the entry
1979 * @type: The type of the entry
1981 * Returns: 0 on success, error code on failure
1984 int gfs2_dir_add(struct gfs2_inode *dip, struct qstr *filename,
1985 struct gfs2_inum *inum, unsigned int type)
1989 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1990 error = dir_e_add(dip, filename, inum, type);
1992 error = dir_l_add(dip, filename, inum, type);
1998 * gfs2_dir_del - Delete a directory entry
1999 * @dip: The GFS2 inode
2000 * @filename: The filename
2002 * Returns: 0 on success, error code on failure
2005 int gfs2_dir_del(struct gfs2_inode *dip, struct qstr *filename)
2009 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2010 error = dir_e_del(dip, filename);
2012 error = dir_l_del(dip, filename);
2017 int gfs2_dir_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
2018 gfs2_filldir_t filldir)
2022 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2023 error = dir_e_read(dip, offset, opaque, filldir);
2025 error = dir_l_read(dip, offset, opaque, filldir);
2031 * gfs2_dir_mvino - Change inode number of directory entry
2032 * @dip: The GFS2 inode
2036 * This routine changes the inode number of a directory entry. It's used
2037 * by rename to change ".." when a directory is moved.
2038 * Assumes a glock is held on dvp.
2043 int gfs2_dir_mvino(struct gfs2_inode *dip, struct qstr *filename,
2044 struct gfs2_inum *inum, unsigned int new_type)
2048 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2049 error = dir_e_mvino(dip, filename, inum, new_type);
2051 error = dir_l_mvino(dip, filename, inum, new_type);
2057 * foreach_leaf - call a function for each leaf in a directory
2058 * @dip: the directory
2059 * @lc: the function to call for each each
2060 * @data: private data to pass to it
2065 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
2067 struct gfs2_sbd *sdp = dip->i_sbd;
2068 struct buffer_head *bh;
2069 struct gfs2_leaf leaf;
2070 uint32_t hsize, len;
2071 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
2077 hsize = 1 << dip->i_di.di_depth;
2078 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
2079 gfs2_consist_inode(dip);
2083 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
2087 while (index < hsize) {
2088 lp_offset = index & (sdp->sd_hash_ptrs - 1);
2089 ht_offset = index - lp_offset;
2091 if (ht_offset_cur != ht_offset) {
2092 error = gfs2_dir_read_data(dip, (char *)lp,
2093 ht_offset * sizeof(uint64_t),
2094 sdp->sd_hash_bsize);
2095 if (error != sdp->sd_hash_bsize) {
2100 ht_offset_cur = ht_offset;
2103 leaf_no = be64_to_cpu(lp[lp_offset]);
2105 error = get_leaf(dip, leaf_no, &bh);
2108 gfs2_leaf_in(&leaf, bh->b_data);
2111 len = 1 << (dip->i_di.di_depth - leaf.lf_depth);
2113 error = lc(dip, index, len, leaf_no, data);
2117 index = (index & ~(len - 1)) + len;
2122 if (index != hsize) {
2123 gfs2_consist_inode(dip);
2134 * leaf_dealloc - Deallocate a directory leaf
2135 * @dip: the directory
2136 * @index: the hash table offset in the directory
2137 * @len: the number of pointers to this leaf
2138 * @leaf_no: the leaf number
2144 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
2145 uint64_t leaf_no, void *data)
2147 struct gfs2_sbd *sdp = dip->i_sbd;
2148 struct gfs2_leaf tmp_leaf;
2149 struct gfs2_rgrp_list rlist;
2150 struct buffer_head *bh, *dibh;
2152 unsigned int rg_blocks = 0, l_blocks = 0;
2154 unsigned int x, size = len * sizeof(uint64_t);
2157 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
2159 ht = kzalloc(size, GFP_KERNEL);
2163 gfs2_alloc_get(dip);
2165 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
2169 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
2173 /* Count the number of leaves */
2175 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) {
2176 error = get_leaf(dip, blk, &bh);
2179 gfs2_leaf_in(&tmp_leaf, (bh)->b_data);
2182 gfs2_rlist_add(sdp, &rlist, blk);
2186 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
2188 for (x = 0; x < rlist.rl_rgrps; x++) {
2189 struct gfs2_rgrpd *rgd;
2190 rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl);
2191 rg_blocks += rgd->rd_ri.ri_length;
2194 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
2198 error = gfs2_trans_begin(sdp,
2199 rg_blocks + (DIV_RU(size, sdp->sd_jbsize) + 1) +
2200 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
2202 goto out_rg_gunlock;
2204 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) {
2205 error = get_leaf(dip, blk, &bh);
2208 gfs2_leaf_in(&tmp_leaf, bh->b_data);
2211 gfs2_free_meta(dip, blk, 1);
2213 if (!dip->i_di.di_blocks)
2214 gfs2_consist_inode(dip);
2215 dip->i_di.di_blocks--;
2218 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
2219 if (error != size) {
2225 error = gfs2_meta_inode_buffer(dip, &dibh);
2229 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
2230 gfs2_dinode_out(&dip->i_di, dibh->b_data);
2234 gfs2_trans_end(sdp);
2237 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
2240 gfs2_rlist_free(&rlist);
2241 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
2244 gfs2_quota_unhold(dip);
2247 gfs2_alloc_put(dip);
2254 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2255 * @dip: the directory
2257 * Dealloc all on-disk directory leaves to FREEMETA state
2258 * Change on-disk inode type to "regular file"
2263 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2265 struct gfs2_sbd *sdp = dip->i_sbd;
2266 struct buffer_head *bh;
2269 /* Dealloc on-disk leaves to FREEMETA state */
2270 error = foreach_leaf(dip, leaf_dealloc, NULL);
2274 /* Make this a regular file in case we crash.
2275 (We don't want to free these blocks a second time.) */
2277 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
2281 error = gfs2_meta_inode_buffer(dip, &bh);
2283 gfs2_trans_add_bh(dip->i_gl, bh, 1);
2284 ((struct gfs2_dinode *)bh->b_data)->di_mode = cpu_to_be32(S_IFREG);
2288 gfs2_trans_end(sdp);
2294 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2295 * @ip: the file being written to
2296 * @filname: the filename that's going to be added
2297 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
2302 int gfs2_diradd_alloc_required(struct gfs2_inode *dip, struct qstr *filename,
2303 int *alloc_required)
2305 struct buffer_head *bh = NULL, *bh_next;
2306 uint32_t hsize, hash, index;
2309 *alloc_required = 0;
2311 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
2312 hsize = 1 << dip->i_di.di_depth;
2313 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
2314 gfs2_consist_inode(dip);
2318 hash = gfs2_disk_hash(filename->name, filename->len);
2319 index = hash >> (32 - dip->i_di.di_depth);
2321 error = get_first_leaf(dip, index, &bh_next);
2330 if (dirent_fits(dip, bh, filename->len))
2333 error = get_next_leaf(dip, bh, &bh_next);
2334 if (error == -ENOENT) {
2335 *alloc_required = 1;
2344 error = gfs2_meta_inode_buffer(dip, &bh);
2348 if (!dirent_fits(dip, bh, filename->len))
2349 *alloc_required = 1;