2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
39 * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
43 * When the dirents are in leaves, the actual contents of the directory file are
44 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
45 * dirents are NOT in the directory file itself. There can be more than one block
46 * pointer in the array that points to the same leaf. In fact, when a directory
47 * is first converted from linear to exhash, all of the pointers point to the
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/spinlock.h>
59 #include <linux/completion.h>
60 #include <linux/buffer_head.h>
61 #include <linux/sort.h>
62 #include <linux/gfs2_ondisk.h>
63 #include <asm/semaphore.h>
66 #include "lm_interface.h"
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
82 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
83 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
85 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
86 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
89 typedef int (*leaf_call_t) (struct gfs2_inode *dip,
90 uint32_t index, uint32_t len, uint64_t leaf_no,
93 int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
94 struct buffer_head **bhp)
96 struct buffer_head *bh;
100 bh = gfs2_meta_new(ip->i_gl, block);
101 gfs2_trans_add_bh(ip->i_gl, bh, 1);
102 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
103 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
105 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT,
109 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
121 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
122 unsigned int offset, unsigned int size)
125 struct buffer_head *dibh;
128 error = gfs2_meta_inode_buffer(ip, &dibh);
132 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
133 memcpy(dibh->b_data + offset + sizeof(struct gfs2_inode), buf, size);
134 if (ip->i_di.di_size < offset + size)
135 ip->i_di.di_size = offset + size;
136 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
137 gfs2_dinode_out(&ip->i_di, dibh->b_data);
147 * gfs2_dir_write_data - Write directory information to the inode
148 * @ip: The GFS2 inode
149 * @buf: The buffer containing information to be written
150 * @offset: The file offset to start writing at
151 * @size: The amount of data to write
153 * Returns: The number of bytes correctly written or error code
155 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
156 uint64_t offset, unsigned int size)
158 struct gfs2_sbd *sdp = ip->i_sbd;
159 struct buffer_head *dibh;
160 uint64_t lblock, dblock;
169 if (gfs2_is_stuffed(ip) &&
170 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
171 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
174 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
177 if (gfs2_is_stuffed(ip)) {
178 error = gfs2_unstuff_dinode(ip, NULL, NULL);
184 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
186 while (copied < size) {
188 struct buffer_head *bh;
191 amount = size - copied;
192 if (amount > sdp->sd_sb.sb_bsize - o)
193 amount = sdp->sd_sb.sb_bsize - o;
197 error = gfs2_block_map(ip, lblock, &new, &dblock,
202 if (gfs2_assert_withdraw(sdp, dblock))
206 error = gfs2_dir_get_buffer(ip, dblock,
207 (amount == sdp->sd_jbsize) ?
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
223 o = sizeof(struct gfs2_meta_header);
227 error = gfs2_meta_inode_buffer(ip, &dibh);
231 if (ip->i_di.di_size < offset + copied)
232 ip->i_di.di_size = offset + copied;
233 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
235 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
236 gfs2_dinode_out(&ip->i_di, dibh->b_data);
246 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
247 unsigned int offset, unsigned int size)
249 struct buffer_head *dibh;
252 error = gfs2_meta_inode_buffer(ip, &dibh);
254 offset += sizeof(struct gfs2_dinode);
255 memcpy(buf, dibh->b_data + offset, size);
259 return (error) ? error : size;
264 * gfs2_dir_read_data - Read a data from a directory inode
265 * @ip: The GFS2 Inode
266 * @buf: The buffer to place result into
267 * @offset: File offset to begin jdata_readng from
268 * @size: Amount of data to transfer
270 * Returns: The amount of data actually copied or the error
272 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
273 uint64_t offset, unsigned int size)
275 struct gfs2_sbd *sdp = ip->i_sbd;
276 uint64_t lblock, dblock;
282 if (offset >= ip->i_di.di_size)
285 if ((offset + size) > ip->i_di.di_size)
286 size = ip->i_di.di_size - offset;
291 if (gfs2_is_stuffed(ip))
292 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
295 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
299 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
301 while (copied < size) {
303 struct buffer_head *bh;
306 amount = size - copied;
307 if (amount > sdp->sd_sb.sb_bsize - o)
308 amount = sdp->sd_sb.sb_bsize - o;
312 error = gfs2_block_map(ip, lblock, &new, &dblock,
319 gfs2_meta_ra(ip->i_gl, dblock, extlen);
322 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
330 memcpy(buf, bh->b_data + o, amount);
338 o = sizeof(struct gfs2_meta_header);
343 return (copied) ? copied : error;
347 * int gfs2_filecmp - Compare two filenames
348 * @file1: The first filename
349 * @file2: The second filename
350 * @len_of_file2: The length of the second file
352 * This routine compares two filenames and returns 1 if they are equal.
354 * Returns: 1 if the files are the same, otherwise 0.
357 int gfs2_filecmp(struct qstr *file1, char *file2, int len_of_file2)
359 if (file1->len != len_of_file2)
361 if (memcmp(file1->name, file2, file1->len))
367 * dirent_first - Return the first dirent
368 * @dip: the directory
370 * @dent: Pointer to list of dirents
372 * return first dirent whether bh points to leaf or stuffed dinode
374 * Returns: IS_LEAF, IS_DINODE, or -errno
377 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
378 struct gfs2_dirent **dent)
380 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
382 if (be16_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
383 if (gfs2_meta_check(dip->i_sbd, bh))
385 *dent = (struct gfs2_dirent *)(bh->b_data +
386 sizeof(struct gfs2_leaf));
389 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
391 *dent = (struct gfs2_dirent *)(bh->b_data +
392 sizeof(struct gfs2_dinode));
398 * dirent_next - Next dirent
399 * @dip: the directory
401 * @dent: Pointer to list of dirents
403 * Returns: 0 on success, error code otherwise
406 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
407 struct gfs2_dirent **dent)
409 struct gfs2_dirent *tmp, *cur;
411 uint16_t cur_rec_len;
414 bh_end = bh->b_data + bh->b_size;
415 cur_rec_len = be16_to_cpu(cur->de_rec_len);
417 if ((char *)cur + cur_rec_len >= bh_end) {
418 if ((char *)cur + cur_rec_len > bh_end) {
419 gfs2_consist_inode(dip);
425 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
427 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
428 gfs2_consist_inode(dip);
432 if (cur_rec_len == 0) {
433 gfs2_consist_inode(dip);
437 /* Only the first dent could ever have de_inum.no_addr == 0 */
438 if (!tmp->de_inum.no_addr) {
439 gfs2_consist_inode(dip);
449 * dirent_del - Delete a dirent
450 * @dip: The GFS2 inode
452 * @prev: The previous dirent
453 * @cur: The current dirent
457 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
458 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
460 uint16_t cur_rec_len, prev_rec_len;
462 if (!cur->de_inum.no_addr) {
463 gfs2_consist_inode(dip);
467 gfs2_trans_add_bh(dip->i_gl, bh, 1);
469 /* If there is no prev entry, this is the first entry in the block.
470 The de_rec_len is already as big as it needs to be. Just zero
471 out the inode number and return. */
474 cur->de_inum.no_addr = 0; /* No endianess worries */
478 /* Combine this dentry with the previous one. */
480 prev_rec_len = be16_to_cpu(prev->de_rec_len);
481 cur_rec_len = be16_to_cpu(cur->de_rec_len);
483 if ((char *)prev + prev_rec_len != (char *)cur)
484 gfs2_consist_inode(dip);
485 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
486 gfs2_consist_inode(dip);
488 prev_rec_len += cur_rec_len;
489 prev->de_rec_len = cpu_to_be16(prev_rec_len);
493 * gfs2_dirent_alloc - Allocate a directory entry
494 * @dip: The GFS2 inode
496 * @name_len: The length of the name
497 * @dent_out: Pointer to list of dirents
499 * Returns: 0 on success, error code otherwise
502 int gfs2_dirent_alloc(struct gfs2_inode *dip, struct buffer_head *bh,
503 int name_len, struct gfs2_dirent **dent_out)
505 struct gfs2_dirent *dent, *new;
506 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
507 unsigned int entries = 0, offset = 0;
510 type = dirent_first(dip, bh, &dent);
514 if (type == IS_LEAF) {
515 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
516 entries = be16_to_cpu(leaf->lf_entries);
517 offset = sizeof(struct gfs2_leaf);
519 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
520 entries = be32_to_cpu(dinode->di_entries);
521 offset = sizeof(struct gfs2_dinode);
525 if (dent->de_inum.no_addr) {
526 gfs2_consist_inode(dip);
530 gfs2_trans_add_bh(dip->i_gl, bh, 1);
532 dent->de_rec_len = cpu_to_be16(bh->b_size - offset);
533 dent->de_name_len = cpu_to_be16(name_len);
540 uint16_t cur_rec_len;
541 uint16_t cur_name_len;
543 cur_rec_len = be16_to_cpu(dent->de_rec_len);
544 cur_name_len = be16_to_cpu(dent->de_name_len);
546 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
547 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len)) {
548 gfs2_trans_add_bh(dip->i_gl, bh, 1);
550 if (dent->de_inum.no_addr) {
551 new = (struct gfs2_dirent *)((char *)dent +
552 GFS2_DIRENT_SIZE(cur_name_len));
553 memset(new, 0, sizeof(struct gfs2_dirent));
555 new->de_rec_len = cpu_to_be16(cur_rec_len -
556 GFS2_DIRENT_SIZE(cur_name_len));
557 new->de_name_len = cpu_to_be16(name_len);
559 dent->de_rec_len = cpu_to_be16(cur_rec_len -
560 be16_to_cpu(new->de_rec_len));
566 dent->de_name_len = cpu_to_be16(name_len);
571 } while (dirent_next(dip, bh, &dent) == 0);
577 * dirent_fits - See if we can fit a entry in this buffer
578 * @dip: The GFS2 inode
580 * @name_len: The length of the name
582 * Returns: 1 if it can fit, 0 otherwise
585 static int dirent_fits(struct gfs2_inode *dip, struct buffer_head *bh,
588 struct gfs2_dirent *dent;
589 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
590 unsigned int entries = 0;
593 type = dirent_first(dip, bh, &dent);
597 if (type == IS_LEAF) {
598 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
599 entries = be16_to_cpu(leaf->lf_entries);
601 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
602 entries = be32_to_cpu(dinode->di_entries);
609 uint16_t cur_rec_len;
610 uint32_t cur_name_len;
612 cur_rec_len = be16_to_cpu(dent->de_rec_len);
613 cur_name_len = be16_to_cpu(dent->de_name_len);
615 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
616 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len))
618 } while (dirent_next(dip, bh, &dent) == 0);
623 static int leaf_search(struct gfs2_inode *dip, struct buffer_head *bh,
624 struct qstr *filename, struct gfs2_dirent **dent_out,
625 struct gfs2_dirent **dent_prev)
628 struct gfs2_dirent *dent, *prev = NULL;
629 unsigned int entries = 0;
632 type = dirent_first(dip, bh, &dent);
636 if (type == IS_LEAF) {
637 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
638 entries = be16_to_cpu(leaf->lf_entries);
639 } else if (type == IS_DINODE) {
640 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
641 entries = be32_to_cpu(dinode->di_entries);
644 hash = gfs2_disk_hash(filename->name, filename->len);
647 if (!dent->de_inum.no_addr) {
652 if (be32_to_cpu(dent->de_hash) == hash &&
653 gfs2_filecmp(filename, (char *)(dent + 1),
654 be16_to_cpu(dent->de_name_len))) {
663 } while (dirent_next(dip, bh, &dent) == 0);
668 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
669 struct buffer_head **bhp)
673 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
674 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
681 * get_leaf_nr - Get a leaf number associated with the index
682 * @dip: The GFS2 inode
686 * Returns: 0 on success, error code otherwise
689 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
695 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
696 index * sizeof(uint64_t),
698 if (error != sizeof(uint64_t))
699 return (error < 0) ? error : -EIO;
701 *leaf_out = be64_to_cpu(leaf_no);
706 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
707 struct buffer_head **bh_out)
712 error = get_leaf_nr(dip, index, &leaf_no);
714 error = get_leaf(dip, leaf_no, bh_out);
719 static int get_next_leaf(struct gfs2_inode *dip, struct buffer_head *bh_in,
720 struct buffer_head **bh_out)
722 struct gfs2_leaf *leaf;
725 leaf = (struct gfs2_leaf *)bh_in->b_data;
730 error = get_leaf(dip, be64_to_cpu(leaf->lf_next), bh_out);
735 static int linked_leaf_search(struct gfs2_inode *dip, struct qstr *filename,
736 struct gfs2_dirent **dent_out,
737 struct gfs2_dirent **dent_prev,
738 struct buffer_head **bh_out)
740 struct buffer_head *bh = NULL, *bh_next;
741 uint32_t hsize, index;
745 hsize = 1 << dip->i_di.di_depth;
746 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
747 gfs2_consist_inode(dip);
751 /* Figure out the address of the leaf node. */
753 hash = gfs2_disk_hash(filename->name, filename->len);
754 index = hash >> (32 - dip->i_di.di_depth);
756 error = get_first_leaf(dip, index, &bh_next);
767 error = leaf_search(dip, bh, filename, dent_out, dent_prev);
781 error = get_next_leaf(dip, bh, &bh_next);
791 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
792 * @dip: The GFS2 inode
794 * Returns: 0 on success, error code otherwise
797 static int dir_make_exhash(struct gfs2_inode *dip)
799 struct gfs2_sbd *sdp = dip->i_sbd;
800 struct gfs2_dirent *dent;
801 struct buffer_head *bh, *dibh;
802 struct gfs2_leaf *leaf;
808 error = gfs2_meta_inode_buffer(dip, &dibh);
812 /* Allocate a new block for the first leaf node */
814 bn = gfs2_alloc_meta(dip);
816 /* Turn over a new leaf */
818 bh = gfs2_meta_new(dip->i_gl, bn);
819 gfs2_trans_add_bh(dip->i_gl, bh, 1);
820 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
821 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
823 /* Fill in the leaf structure */
825 leaf = (struct gfs2_leaf *)bh->b_data;
827 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
829 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
830 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
834 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
835 sizeof(struct gfs2_dinode));
837 /* Find last entry */
840 dirent_first(dip, bh, &dent);
843 if (!dent->de_inum.no_addr)
845 if (++x == dip->i_di.di_entries)
848 while (dirent_next(dip, bh, &dent) == 0);
850 /* Adjust the last dirent's record length
851 (Remember that dent still points to the last entry.) */
853 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
854 sizeof(struct gfs2_dinode) -
855 sizeof(struct gfs2_leaf));
859 /* We're done with the new leaf block, now setup the new
862 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
863 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
865 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
867 for (x = sdp->sd_hash_ptrs; x--; lp++)
868 *lp = cpu_to_be64(bn);
870 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
871 dip->i_di.di_blocks++;
872 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
873 dip->i_di.di_payload_format = 0;
875 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
876 dip->i_di.di_depth = y;
878 gfs2_dinode_out(&dip->i_di, dibh->b_data);
886 * dir_split_leaf - Split a leaf block into two
887 * @dip: The GFS2 inode
891 * Returns: 0 on success, error code on failure
894 static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index,
897 struct buffer_head *nbh, *obh, *dibh;
898 struct gfs2_leaf *nleaf, *oleaf;
899 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
900 uint32_t start, len, half_len, divider;
906 /* Allocate the new leaf block */
908 bn = gfs2_alloc_meta(dip);
910 /* Get the new leaf block */
912 nbh = gfs2_meta_new(dip->i_gl, bn);
913 gfs2_trans_add_bh(dip->i_gl, nbh, 1);
914 gfs2_metatype_set(nbh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
915 gfs2_buffer_clear_tail(nbh, sizeof(struct gfs2_meta_header));
917 nleaf = (struct gfs2_leaf *)nbh->b_data;
919 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
921 /* Get the old leaf block */
923 error = get_leaf(dip, leaf_no, &obh);
927 gfs2_trans_add_bh(dip->i_gl, obh, 1);
929 oleaf = (struct gfs2_leaf *)obh->b_data;
931 /* Compute the start and len of leaf pointers in the hash table. */
933 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
936 gfs2_consist_inode(dip);
941 start = (index & ~(len - 1));
943 /* Change the pointers.
944 Don't bother distinguishing stuffed from non-stuffed.
945 This code is complicated enough already. */
947 lp = kcalloc(half_len, sizeof(uint64_t), GFP_KERNEL | __GFP_NOFAIL);
949 error = gfs2_dir_read_data(dip, (char *)lp, start * sizeof(uint64_t),
950 half_len * sizeof(uint64_t));
951 if (error != half_len * sizeof(uint64_t)) {
957 /* Change the pointers */
959 for (x = 0; x < half_len; x++)
960 lp[x] = cpu_to_be64(bn);
962 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
963 half_len * sizeof(uint64_t));
964 if (error != half_len * sizeof(uint64_t)) {
972 /* Compute the divider */
974 divider = (start + half_len) << (32 - dip->i_di.di_depth);
976 /* Copy the entries */
978 dirent_first(dip, obh, &dent);
982 if (dirent_next(dip, obh, &next))
985 if (dent->de_inum.no_addr &&
986 be32_to_cpu(dent->de_hash) < divider) {
987 name_len = be16_to_cpu(dent->de_name_len);
989 gfs2_dirent_alloc(dip, nbh, name_len, &new);
991 new->de_inum = dent->de_inum; /* No endian worries */
992 new->de_hash = dent->de_hash; /* No endian worries */
993 new->de_type = dent->de_type; /* No endian worries */
994 memcpy((char *)(new + 1), (char *)(dent + 1),
997 nleaf->lf_entries = be16_to_cpu(nleaf->lf_entries)+1;
998 nleaf->lf_entries = cpu_to_be16(nleaf->lf_entries);
1000 dirent_del(dip, obh, prev, dent);
1002 if (!oleaf->lf_entries)
1003 gfs2_consist_inode(dip);
1004 oleaf->lf_entries = be16_to_cpu(oleaf->lf_entries)-1;
1005 oleaf->lf_entries = cpu_to_be16(oleaf->lf_entries);
1018 /* If none of the entries got moved into the new leaf,
1019 artificially fill in the first entry. */
1022 gfs2_dirent_alloc(dip, nbh, 0, &new);
1023 new->de_inum.no_addr = 0;
1026 oleaf->lf_depth = be16_to_cpu(oleaf->lf_depth) + 1;
1027 oleaf->lf_depth = cpu_to_be16(oleaf->lf_depth);
1028 nleaf->lf_depth = oleaf->lf_depth;
1030 error = gfs2_meta_inode_buffer(dip, &dibh);
1031 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
1032 dip->i_di.di_blocks++;
1033 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1054 * dir_double_exhash - Double size of ExHash table
1055 * @dip: The GFS2 dinode
1057 * Returns: 0 on success, error code on failure
1060 static int dir_double_exhash(struct gfs2_inode *dip)
1062 struct gfs2_sbd *sdp = dip->i_sbd;
1063 struct buffer_head *dibh;
1066 uint64_t *from, *to;
1071 hsize = 1 << dip->i_di.di_depth;
1072 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1073 gfs2_consist_inode(dip);
1077 /* Allocate both the "from" and "to" buffers in one big chunk */
1079 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1081 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1082 error = gfs2_dir_read_data(dip, (char *)buf,
1083 block * sdp->sd_hash_bsize,
1084 sdp->sd_hash_bsize);
1085 if (error != sdp->sd_hash_bsize) {
1092 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1094 for (x = sdp->sd_hash_ptrs; x--; from++) {
1095 *to++ = *from; /* No endianess worries */
1099 error = gfs2_dir_write_data(dip,
1100 (char *)buf + sdp->sd_hash_bsize,
1101 block * sdp->sd_sb.sb_bsize,
1102 sdp->sd_sb.sb_bsize);
1103 if (error != sdp->sd_sb.sb_bsize) {
1112 error = gfs2_meta_inode_buffer(dip, &dibh);
1113 if (!gfs2_assert_withdraw(sdp, !error)) {
1114 dip->i_di.di_depth++;
1115 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1128 * compare_dents - compare directory entries by hash value
1132 * When comparing the hash entries of @a to @b:
1138 static int compare_dents(const void *a, const void *b)
1140 struct gfs2_dirent *dent_a, *dent_b;
1141 uint32_t hash_a, hash_b;
1144 dent_a = *(struct gfs2_dirent **)a;
1145 hash_a = dent_a->de_hash;
1146 hash_a = be32_to_cpu(hash_a);
1148 dent_b = *(struct gfs2_dirent **)b;
1149 hash_b = dent_b->de_hash;
1150 hash_b = be32_to_cpu(hash_b);
1152 if (hash_a > hash_b)
1154 else if (hash_a < hash_b)
1157 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1158 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1162 else if (len_a < len_b)
1165 ret = memcmp((char *)(dent_a + 1),
1166 (char *)(dent_b + 1),
1174 * do_filldir_main - read out directory entries
1175 * @dip: The GFS2 inode
1176 * @offset: The offset in the file to read from
1177 * @opaque: opaque data to pass to filldir
1178 * @filldir: The function to pass entries to
1179 * @darr: an array of struct gfs2_dirent pointers to read
1180 * @entries: the number of entries in darr
1181 * @copied: pointer to int that's non-zero if a entry has been copied out
1183 * Jump through some hoops to make sure that if there are hash collsions,
1184 * they are read out at the beginning of a buffer. We want to minimize
1185 * the possibility that they will fall into different readdir buffers or
1186 * that someone will want to seek to that location.
1188 * Returns: errno, >0 on exception from filldir
1191 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1192 void *opaque, gfs2_filldir_t filldir,
1193 struct gfs2_dirent **darr, uint32_t entries,
1196 struct gfs2_dirent *dent, *dent_next;
1197 struct gfs2_inum inum;
1198 uint64_t off, off_next;
1203 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1205 dent_next = darr[0];
1206 off_next = be32_to_cpu(dent_next->de_hash);
1207 off_next = gfs2_disk_hash2offset(off_next);
1209 for (x = 0, y = 1; x < entries; x++, y++) {
1214 dent_next = darr[y];
1215 off_next = be32_to_cpu(dent_next->de_hash);
1216 off_next = gfs2_disk_hash2offset(off_next);
1222 if (off_next == off) {
1223 if (*copied && !run)
1234 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1236 error = filldir(opaque, (char *)(dent + 1),
1237 be16_to_cpu(dent->de_name_len),
1239 be16_to_cpu(dent->de_type));
1246 /* Increment the *offset by one, so the next time we come into the
1247 do_filldir fxn, we get the next entry instead of the last one in the
1256 * do_filldir_single - Read directory entries out of a single block
1257 * @dip: The GFS2 inode
1258 * @offset: The offset in the file to read from
1259 * @opaque: opaque data to pass to filldir
1260 * @filldir: The function to pass entries to
1262 * @entries: the number of entries in the block
1263 * @copied: pointer to int that's non-zero if a entry has been copied out
1265 * Returns: errno, >0 on exception from filldir
1268 static int do_filldir_single(struct gfs2_inode *dip, uint64_t *offset,
1269 void *opaque, gfs2_filldir_t filldir,
1270 struct buffer_head *bh, uint32_t entries,
1273 struct gfs2_dirent **darr;
1274 struct gfs2_dirent *de;
1281 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1285 dirent_first(dip, bh, &de);
1287 if (!de->de_inum.no_addr)
1290 gfs2_consist_inode(dip);
1296 while (dirent_next(dip, bh, &de) == 0);
1299 gfs2_consist_inode(dip);
1304 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1314 * do_filldir_multi - Read directory entries out of a linked leaf list
1315 * @dip: The GFS2 inode
1316 * @offset: The offset in the file to read from
1317 * @opaque: opaque data to pass to filldir
1318 * @filldir: The function to pass entries to
1319 * @bh: the first leaf in the list
1320 * @copied: pointer to int that's non-zero if a entry has been copied out
1322 * Returns: errno, >0 on exception from filldir
1325 static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1326 void *opaque, gfs2_filldir_t filldir,
1327 struct buffer_head *bh, int *copied)
1329 struct buffer_head **larr = NULL;
1330 struct gfs2_dirent **darr;
1331 struct gfs2_leaf *leaf;
1332 struct buffer_head *tmp_bh;
1333 struct gfs2_dirent *de;
1334 unsigned int entries, e = 0;
1335 unsigned int leaves = 0, l = 0;
1340 /* Count leaves and entries */
1342 leaf = (struct gfs2_leaf *)bh->b_data;
1343 entries = be16_to_cpu(leaf->lf_entries);
1347 ln = be64_to_cpu(ln);
1349 error = get_leaf(dip, ln, &tmp_bh);
1353 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1354 if (leaf->lf_entries) {
1355 entries += be16_to_cpu(leaf->lf_entries);
1367 larr = kcalloc(leaves, sizeof(struct buffer_head *),GFP_KERNEL);
1372 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1378 leaf = (struct gfs2_leaf *)bh->b_data;
1379 if (leaf->lf_entries) {
1380 dirent_first(dip, bh, &de);
1382 if (!de->de_inum.no_addr)
1385 gfs2_consist_inode(dip);
1391 while (dirent_next(dip, bh, &de) == 0);
1396 ln = be64_to_cpu(ln);
1398 error = get_leaf(dip, ln, &tmp_bh);
1402 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1403 if (leaf->lf_entries) {
1404 dirent_first(dip, tmp_bh, &de);
1406 if (!de->de_inum.no_addr)
1409 gfs2_consist_inode(dip);
1415 while (dirent_next(dip, tmp_bh, &de) == 0);
1426 if (gfs2_assert_withdraw(dip->i_sbd, l == leaves)) {
1431 gfs2_consist_inode(dip);
1436 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1441 for (x = 0; x < l; x++)
1449 * dir_e_search - Search exhash (leaf) dir for inode matching name
1450 * @dip: The GFS2 inode
1451 * @filename: Filename string
1452 * @inode: If non-NULL, function fills with formal inode # and block address
1453 * @type: If non-NULL, function fills with DT_... dinode type
1458 static int dir_e_search(struct gfs2_inode *dip, struct qstr *filename,
1459 struct gfs2_inum *inum, unsigned int *type)
1461 struct buffer_head *bh;
1462 struct gfs2_dirent *dent;
1465 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1470 gfs2_inum_in(inum, (char *)&dent->de_inum);
1472 *type = be16_to_cpu(dent->de_type);
1479 static int dir_e_add(struct gfs2_inode *dip, struct qstr *filename,
1480 struct gfs2_inum *inum, unsigned int type)
1482 struct buffer_head *bh, *nbh, *dibh;
1483 struct gfs2_leaf *leaf, *nleaf;
1484 struct gfs2_dirent *dent;
1485 uint32_t hsize, index;
1487 uint64_t leaf_no, bn;
1491 hsize = 1 << dip->i_di.di_depth;
1492 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1493 gfs2_consist_inode(dip);
1497 /* Figure out the address of the leaf node. */
1499 hash = gfs2_disk_hash(filename->name, filename->len);
1500 index = hash >> (32 - dip->i_di.di_depth);
1502 error = get_leaf_nr(dip, index, &leaf_no);
1506 /* Add entry to the leaf */
1509 error = get_leaf(dip, leaf_no, &bh);
1513 leaf = (struct gfs2_leaf *)bh->b_data;
1515 if (gfs2_dirent_alloc(dip, bh, filename->len, &dent)) {
1517 if (be16_to_cpu(leaf->lf_depth) < dip->i_di.di_depth) {
1518 /* Can we split the leaf? */
1522 error = dir_split_leaf(dip, index, leaf_no);
1528 } else if (dip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1529 /* Can we double the hash table? */
1533 error = dir_double_exhash(dip);
1539 } else if (leaf->lf_next) {
1540 /* Can we try the next leaf in the list? */
1541 leaf_no = be64_to_cpu(leaf->lf_next);
1546 /* Create a new leaf and add it to the list. */
1548 bn = gfs2_alloc_meta(dip);
1550 nbh = gfs2_meta_new(dip->i_gl, bn);
1551 gfs2_trans_add_bh(dip->i_gl, nbh, 1);
1552 gfs2_metatype_set(nbh,
1555 gfs2_buffer_clear_tail(nbh,
1556 sizeof(struct gfs2_meta_header));
1558 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1559 leaf->lf_next = cpu_to_be64(bn);
1561 nleaf = (struct gfs2_leaf *)nbh->b_data;
1562 nleaf->lf_depth = leaf->lf_depth;
1563 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
1565 gfs2_dirent_alloc(dip, nbh, filename->len,
1568 dip->i_di.di_blocks++;
1577 /* If the gfs2_dirent_alloc() succeeded, it pinned the "bh" */
1579 gfs2_inum_out(inum, (char *)&dent->de_inum);
1580 dent->de_hash = cpu_to_be32(hash);
1581 dent->de_type = cpu_to_be16(type);
1582 memcpy((char *)(dent + 1), filename->name, filename->len);
1584 leaf->lf_entries = be16_to_cpu(leaf->lf_entries) + 1;
1585 leaf->lf_entries = cpu_to_be16(leaf->lf_entries);
1589 error = gfs2_meta_inode_buffer(dip, &dibh);
1593 dip->i_di.di_entries++;
1594 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1596 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1597 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1606 static int dir_e_del(struct gfs2_inode *dip, struct qstr *filename)
1608 struct buffer_head *bh, *dibh;
1609 struct gfs2_dirent *dent, *prev;
1610 struct gfs2_leaf *leaf;
1611 unsigned int entries;
1614 error = linked_leaf_search(dip, filename, &dent, &prev, &bh);
1615 if (error == -ENOENT) {
1616 gfs2_consist_inode(dip);
1622 dirent_del(dip, bh, prev, dent); /* Pins bh */
1624 leaf = (struct gfs2_leaf *)bh->b_data;
1625 entries = be16_to_cpu(leaf->lf_entries);
1627 gfs2_consist_inode(dip);
1629 leaf->lf_entries = cpu_to_be16(entries);
1633 error = gfs2_meta_inode_buffer(dip, &dibh);
1637 if (!dip->i_di.di_entries)
1638 gfs2_consist_inode(dip);
1639 dip->i_di.di_entries--;
1640 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1642 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1643 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1650 * dir_e_read - Reads the entries from a directory into a filldir buffer
1651 * @dip: dinode pointer
1652 * @offset: the hash of the last entry read shifted to the right once
1653 * @opaque: buffer for the filldir function to fill
1654 * @filldir: points to the filldir function to use
1659 static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1660 gfs2_filldir_t filldir)
1662 struct gfs2_sbd *sdp = dip->i_sbd;
1663 struct buffer_head *bh;
1664 struct gfs2_leaf leaf;
1665 uint32_t hsize, len;
1666 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1667 uint32_t hash, index;
1672 hsize = 1 << dip->i_di.di_depth;
1673 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1674 gfs2_consist_inode(dip);
1678 hash = gfs2_dir_offset2hash(*offset);
1679 index = hash >> (32 - dip->i_di.di_depth);
1681 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1685 while (index < hsize) {
1686 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1687 ht_offset = index - lp_offset;
1689 if (ht_offset_cur != ht_offset) {
1690 error = gfs2_dir_read_data(dip, (char *)lp,
1691 ht_offset * sizeof(uint64_t),
1692 sdp->sd_hash_bsize);
1693 if (error != sdp->sd_hash_bsize) {
1698 ht_offset_cur = ht_offset;
1701 error = get_leaf(dip, be64_to_cpu(lp[lp_offset]), &bh);
1705 gfs2_leaf_in(&leaf, bh->b_data);
1708 error = do_filldir_multi(dip, offset, opaque, filldir,
1711 error = do_filldir_single(dip, offset, opaque, filldir,
1712 bh, leaf.lf_entries, &copied);
1722 len = 1 << (dip->i_di.di_depth - leaf.lf_depth);
1723 index = (index & ~(len - 1)) + len;
1732 static int dir_e_mvino(struct gfs2_inode *dip, struct qstr *filename,
1733 struct gfs2_inum *inum, unsigned int new_type)
1735 struct buffer_head *bh, *dibh;
1736 struct gfs2_dirent *dent;
1739 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1740 if (error == -ENOENT) {
1741 gfs2_consist_inode(dip);
1747 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1749 gfs2_inum_out(inum, (char *)&dent->de_inum);
1750 dent->de_type = cpu_to_be16(new_type);
1754 error = gfs2_meta_inode_buffer(dip, &dibh);
1758 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1760 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1761 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1768 * dir_l_search - Search linear (stuffed dinode) dir for inode matching name
1769 * @dip: The GFS2 inode
1770 * @filename: Filename string
1771 * @inode: If non-NULL, function fills with formal inode # and block address
1772 * @type: If non-NULL, function fills with DT_... dinode type
1777 static int dir_l_search(struct gfs2_inode *dip, struct qstr *filename,
1778 struct gfs2_inum *inum, unsigned int *type)
1780 struct buffer_head *dibh;
1781 struct gfs2_dirent *dent;
1784 if (!gfs2_is_stuffed(dip)) {
1785 gfs2_consist_inode(dip);
1789 error = gfs2_meta_inode_buffer(dip, &dibh);
1793 error = leaf_search(dip, dibh, filename, &dent, NULL);
1796 gfs2_inum_in(inum, (char *)&dent->de_inum);
1798 *type = be16_to_cpu(dent->de_type);
1806 static int dir_l_add(struct gfs2_inode *dip, struct qstr *filename,
1807 struct gfs2_inum *inum, unsigned int type)
1809 struct buffer_head *dibh;
1810 struct gfs2_dirent *dent;
1813 if (!gfs2_is_stuffed(dip)) {
1814 gfs2_consist_inode(dip);
1818 error = gfs2_meta_inode_buffer(dip, &dibh);
1822 if (gfs2_dirent_alloc(dip, dibh, filename->len, &dent)) {
1825 error = dir_make_exhash(dip);
1827 error = dir_e_add(dip, filename, inum, type);
1832 /* gfs2_dirent_alloc() pins */
1834 gfs2_inum_out(inum, (char *)&dent->de_inum);
1835 dent->de_hash = gfs2_disk_hash(filename->name, filename->len);
1836 dent->de_hash = cpu_to_be32(dent->de_hash);
1837 dent->de_type = cpu_to_be16(type);
1838 memcpy((char *)(dent + 1), filename->name, filename->len);
1840 dip->i_di.di_entries++;
1841 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1843 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1849 static int dir_l_del(struct gfs2_inode *dip, struct qstr *filename)
1851 struct buffer_head *dibh;
1852 struct gfs2_dirent *dent, *prev;
1855 if (!gfs2_is_stuffed(dip)) {
1856 gfs2_consist_inode(dip);
1860 error = gfs2_meta_inode_buffer(dip, &dibh);
1864 error = leaf_search(dip, dibh, filename, &dent, &prev);
1865 if (error == -ENOENT) {
1866 gfs2_consist_inode(dip);
1873 dirent_del(dip, dibh, prev, dent);
1875 /* dirent_del() pins */
1877 if (!dip->i_di.di_entries)
1878 gfs2_consist_inode(dip);
1879 dip->i_di.di_entries--;
1881 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1883 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1891 static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1892 gfs2_filldir_t filldir)
1894 struct buffer_head *dibh;
1898 if (!gfs2_is_stuffed(dip)) {
1899 gfs2_consist_inode(dip);
1903 if (!dip->i_di.di_entries)
1906 error = gfs2_meta_inode_buffer(dip, &dibh);
1910 error = do_filldir_single(dip, offset,
1912 dibh, dip->i_di.di_entries,
1922 static int dir_l_mvino(struct gfs2_inode *dip, struct qstr *filename,
1923 struct gfs2_inum *inum, unsigned int new_type)
1925 struct buffer_head *dibh;
1926 struct gfs2_dirent *dent;
1929 if (!gfs2_is_stuffed(dip)) {
1930 gfs2_consist_inode(dip);
1934 error = gfs2_meta_inode_buffer(dip, &dibh);
1938 error = leaf_search(dip, dibh, filename, &dent, NULL);
1939 if (error == -ENOENT) {
1940 gfs2_consist_inode(dip);
1947 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1949 gfs2_inum_out(inum, (char *)&dent->de_inum);
1950 dent->de_type = cpu_to_be16(new_type);
1952 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1954 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1963 * gfs2_dir_search - Search a directory
1964 * @dip: The GFS2 inode
1968 * This routine searches a directory for a file or another directory.
1969 * Assumes a glock is held on dip.
1974 int gfs2_dir_search(struct gfs2_inode *dip, struct qstr *filename,
1975 struct gfs2_inum *inum, unsigned int *type)
1979 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1980 error = dir_e_search(dip, filename, inum, type);
1982 error = dir_l_search(dip, filename, inum, type);
1988 * gfs2_dir_add - Add new filename into directory
1989 * @dip: The GFS2 inode
1990 * @filename: The new name
1991 * @inode: The inode number of the entry
1992 * @type: The type of the entry
1994 * Returns: 0 on success, error code on failure
1997 int gfs2_dir_add(struct gfs2_inode *dip, struct qstr *filename,
1998 struct gfs2_inum *inum, unsigned int type)
2002 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2003 error = dir_e_add(dip, filename, inum, type);
2005 error = dir_l_add(dip, filename, inum, type);
2011 * gfs2_dir_del - Delete a directory entry
2012 * @dip: The GFS2 inode
2013 * @filename: The filename
2015 * Returns: 0 on success, error code on failure
2018 int gfs2_dir_del(struct gfs2_inode *dip, struct qstr *filename)
2022 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2023 error = dir_e_del(dip, filename);
2025 error = dir_l_del(dip, filename);
2030 int gfs2_dir_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
2031 gfs2_filldir_t filldir)
2035 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2036 error = dir_e_read(dip, offset, opaque, filldir);
2038 error = dir_l_read(dip, offset, opaque, filldir);
2044 * gfs2_dir_mvino - Change inode number of directory entry
2045 * @dip: The GFS2 inode
2049 * This routine changes the inode number of a directory entry. It's used
2050 * by rename to change ".." when a directory is moved.
2051 * Assumes a glock is held on dvp.
2056 int gfs2_dir_mvino(struct gfs2_inode *dip, struct qstr *filename,
2057 struct gfs2_inum *inum, unsigned int new_type)
2061 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
2062 error = dir_e_mvino(dip, filename, inum, new_type);
2064 error = dir_l_mvino(dip, filename, inum, new_type);
2070 * foreach_leaf - call a function for each leaf in a directory
2071 * @dip: the directory
2072 * @lc: the function to call for each each
2073 * @data: private data to pass to it
2078 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
2080 struct gfs2_sbd *sdp = dip->i_sbd;
2081 struct buffer_head *bh;
2082 struct gfs2_leaf leaf;
2083 uint32_t hsize, len;
2084 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
2090 hsize = 1 << dip->i_di.di_depth;
2091 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
2092 gfs2_consist_inode(dip);
2096 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
2100 while (index < hsize) {
2101 lp_offset = index & (sdp->sd_hash_ptrs - 1);
2102 ht_offset = index - lp_offset;
2104 if (ht_offset_cur != ht_offset) {
2105 error = gfs2_dir_read_data(dip, (char *)lp,
2106 ht_offset * sizeof(uint64_t),
2107 sdp->sd_hash_bsize);
2108 if (error != sdp->sd_hash_bsize) {
2113 ht_offset_cur = ht_offset;
2116 leaf_no = be64_to_cpu(lp[lp_offset]);
2118 error = get_leaf(dip, leaf_no, &bh);
2121 gfs2_leaf_in(&leaf, bh->b_data);
2124 len = 1 << (dip->i_di.di_depth - leaf.lf_depth);
2126 error = lc(dip, index, len, leaf_no, data);
2130 index = (index & ~(len - 1)) + len;
2135 if (index != hsize) {
2136 gfs2_consist_inode(dip);
2147 * leaf_dealloc - Deallocate a directory leaf
2148 * @dip: the directory
2149 * @index: the hash table offset in the directory
2150 * @len: the number of pointers to this leaf
2151 * @leaf_no: the leaf number
2157 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
2158 uint64_t leaf_no, void *data)
2160 struct gfs2_sbd *sdp = dip->i_sbd;
2161 struct gfs2_leaf tmp_leaf;
2162 struct gfs2_rgrp_list rlist;
2163 struct buffer_head *bh, *dibh;
2165 unsigned int rg_blocks = 0, l_blocks = 0;
2167 unsigned int x, size = len * sizeof(uint64_t);
2170 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
2172 ht = kzalloc(size, GFP_KERNEL);
2176 gfs2_alloc_get(dip);
2178 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
2182 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
2186 /* Count the number of leaves */
2188 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) {
2189 error = get_leaf(dip, blk, &bh);
2192 gfs2_leaf_in(&tmp_leaf, (bh)->b_data);
2195 gfs2_rlist_add(sdp, &rlist, blk);
2199 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
2201 for (x = 0; x < rlist.rl_rgrps; x++) {
2202 struct gfs2_rgrpd *rgd;
2203 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
2204 rg_blocks += rgd->rd_ri.ri_length;
2207 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
2211 error = gfs2_trans_begin(sdp,
2212 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
2213 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
2215 goto out_rg_gunlock;
2217 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) {
2218 error = get_leaf(dip, blk, &bh);
2221 gfs2_leaf_in(&tmp_leaf, bh->b_data);
2224 gfs2_free_meta(dip, blk, 1);
2226 if (!dip->i_di.di_blocks)
2227 gfs2_consist_inode(dip);
2228 dip->i_di.di_blocks--;
2231 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
2232 if (error != size) {
2238 error = gfs2_meta_inode_buffer(dip, &dibh);
2242 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
2243 gfs2_dinode_out(&dip->i_di, dibh->b_data);
2247 gfs2_trans_end(sdp);
2250 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
2253 gfs2_rlist_free(&rlist);
2254 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
2257 gfs2_quota_unhold(dip);
2260 gfs2_alloc_put(dip);
2267 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2268 * @dip: the directory
2270 * Dealloc all on-disk directory leaves to FREEMETA state
2271 * Change on-disk inode type to "regular file"
2276 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2278 struct gfs2_sbd *sdp = dip->i_sbd;
2279 struct buffer_head *bh;
2282 /* Dealloc on-disk leaves to FREEMETA state */
2283 error = foreach_leaf(dip, leaf_dealloc, NULL);
2287 /* Make this a regular file in case we crash.
2288 (We don't want to free these blocks a second time.) */
2290 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
2294 error = gfs2_meta_inode_buffer(dip, &bh);
2296 gfs2_trans_add_bh(dip->i_gl, bh, 1);
2297 ((struct gfs2_dinode *)bh->b_data)->di_mode =
2298 cpu_to_be32(S_IFREG);
2302 gfs2_trans_end(sdp);
2308 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2309 * @ip: the file being written to
2310 * @filname: the filename that's going to be added
2311 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
2316 int gfs2_diradd_alloc_required(struct gfs2_inode *dip, struct qstr *filename,
2317 int *alloc_required)
2319 struct buffer_head *bh = NULL, *bh_next;
2320 uint32_t hsize, hash, index;
2323 *alloc_required = 0;
2325 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
2326 hsize = 1 << dip->i_di.di_depth;
2327 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
2328 gfs2_consist_inode(dip);
2332 hash = gfs2_disk_hash(filename->name, filename->len);
2333 index = hash >> (32 - dip->i_di.di_depth);
2335 error = get_first_leaf(dip, index, &bh_next);
2344 if (dirent_fits(dip, bh, filename->len))
2347 error = get_next_leaf(dip, bh, &bh_next);
2348 if (error == -ENOENT) {
2349 *alloc_required = 1;
2358 error = gfs2_meta_inode_buffer(dip, &bh);
2362 if (!dirent_fits(dip, bh, filename->len))
2363 *alloc_required = 1;