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,
90 * int gfs2_filecmp - Compare two filenames
91 * @file1: The first filename
92 * @file2: The second filename
93 * @len_of_file2: The length of the second file
95 * This routine compares two filenames and returns 1 if they are equal.
97 * Returns: 1 if the files are the same, otherwise 0.
100 int gfs2_filecmp(struct qstr *file1, char *file2, int len_of_file2)
102 if (file1->len != len_of_file2)
104 if (memcmp(file1->name, file2, file1->len))
110 * dirent_first - Return the first dirent
111 * @dip: the directory
113 * @dent: Pointer to list of dirents
115 * return first dirent whether bh points to leaf or stuffed dinode
117 * Returns: IS_LEAF, IS_DINODE, or -errno
120 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
121 struct gfs2_dirent **dent)
123 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
125 if (be16_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
126 if (gfs2_meta_check(dip->i_sbd, bh))
128 *dent = (struct gfs2_dirent *)(bh->b_data +
129 sizeof(struct gfs2_leaf));
132 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
134 *dent = (struct gfs2_dirent *)(bh->b_data +
135 sizeof(struct gfs2_dinode));
141 * dirent_next - Next dirent
142 * @dip: the directory
144 * @dent: Pointer to list of dirents
146 * Returns: 0 on success, error code otherwise
149 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
150 struct gfs2_dirent **dent)
152 struct gfs2_dirent *tmp, *cur;
154 uint32_t cur_rec_len;
157 bh_end = bh->b_data + bh->b_size;
158 cur_rec_len = be32_to_cpu(cur->de_rec_len);
160 if ((char *)cur + cur_rec_len >= bh_end) {
161 if ((char *)cur + cur_rec_len > bh_end) {
162 gfs2_consist_inode(dip);
168 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
170 if ((char *)tmp + be32_to_cpu(tmp->de_rec_len) > bh_end) {
171 gfs2_consist_inode(dip);
174 /* Only the first dent could ever have de_inum.no_addr == 0 */
175 if (!tmp->de_inum.no_addr) {
176 gfs2_consist_inode(dip);
186 * dirent_del - Delete a dirent
187 * @dip: The GFS2 inode
189 * @prev: The previous dirent
190 * @cur: The current dirent
194 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
195 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
197 uint32_t cur_rec_len, prev_rec_len;
199 if (!cur->de_inum.no_addr) {
200 gfs2_consist_inode(dip);
204 gfs2_trans_add_bh(dip->i_gl, bh);
206 /* If there is no prev entry, this is the first entry in the block.
207 The de_rec_len is already as big as it needs to be. Just zero
208 out the inode number and return. */
211 cur->de_inum.no_addr = 0; /* No endianess worries */
215 /* Combine this dentry with the previous one. */
217 prev_rec_len = be32_to_cpu(prev->de_rec_len);
218 cur_rec_len = be32_to_cpu(cur->de_rec_len);
220 if ((char *)prev + prev_rec_len != (char *)cur)
221 gfs2_consist_inode(dip);
222 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
223 gfs2_consist_inode(dip);
225 prev_rec_len += cur_rec_len;
226 prev->de_rec_len = cpu_to_be32(prev_rec_len);
230 * gfs2_dirent_alloc - Allocate a directory entry
231 * @dip: The GFS2 inode
233 * @name_len: The length of the name
234 * @dent_out: Pointer to list of dirents
236 * Returns: 0 on success, error code otherwise
239 int gfs2_dirent_alloc(struct gfs2_inode *dip, struct buffer_head *bh,
240 int name_len, struct gfs2_dirent **dent_out)
242 struct gfs2_dirent *dent, *new;
243 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
244 unsigned int entries = 0, offset = 0;
247 type = dirent_first(dip, bh, &dent);
251 if (type == IS_LEAF) {
252 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
253 entries = be16_to_cpu(leaf->lf_entries);
254 offset = sizeof(struct gfs2_leaf);
256 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
257 entries = be32_to_cpu(dinode->di_entries);
258 offset = sizeof(struct gfs2_dinode);
262 if (dent->de_inum.no_addr) {
263 gfs2_consist_inode(dip);
267 gfs2_trans_add_bh(dip->i_gl, bh);
269 dent->de_rec_len = bh->b_size - offset;
270 dent->de_rec_len = cpu_to_be32(dent->de_rec_len);
271 dent->de_name_len = name_len;
278 uint32_t cur_rec_len, cur_name_len;
280 cur_rec_len = be32_to_cpu(dent->de_rec_len);
281 cur_name_len = dent->de_name_len;
283 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
284 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len)) {
285 gfs2_trans_add_bh(dip->i_gl, bh);
287 if (dent->de_inum.no_addr) {
288 new = (struct gfs2_dirent *)((char *)dent +
289 GFS2_DIRENT_SIZE(cur_name_len));
290 memset(new, 0, sizeof(struct gfs2_dirent));
292 new->de_rec_len = cur_rec_len - GFS2_DIRENT_SIZE(cur_name_len);
293 new->de_rec_len = cpu_to_be32(new->de_rec_len);
294 new->de_name_len = name_len;
296 dent->de_rec_len = cur_rec_len - be32_to_cpu(new->de_rec_len);
297 dent->de_rec_len = cpu_to_be32(dent->de_rec_len);
303 dent->de_name_len = name_len;
308 } while (dirent_next(dip, bh, &dent) == 0);
314 * dirent_fits - See if we can fit a entry in this buffer
315 * @dip: The GFS2 inode
317 * @name_len: The length of the name
319 * Returns: 1 if it can fit, 0 otherwise
322 static int dirent_fits(struct gfs2_inode *dip, struct buffer_head *bh,
325 struct gfs2_dirent *dent;
326 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len);
327 unsigned int entries = 0;
330 type = dirent_first(dip, bh, &dent);
334 if (type == IS_LEAF) {
335 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
336 entries = be16_to_cpu(leaf->lf_entries);
338 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
339 entries = be32_to_cpu(dinode->di_entries);
346 uint32_t cur_rec_len, cur_name_len;
348 cur_rec_len = be32_to_cpu(dent->de_rec_len);
349 cur_name_len = dent->de_name_len;
351 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
352 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len))
354 } while (dirent_next(dip, bh, &dent) == 0);
359 static int leaf_search(struct gfs2_inode *dip, struct buffer_head *bh,
360 struct qstr *filename, struct gfs2_dirent **dent_out,
361 struct gfs2_dirent **dent_prev)
364 struct gfs2_dirent *dent, *prev = NULL;
365 unsigned int entries = 0;
368 type = dirent_first(dip, bh, &dent);
372 if (type == IS_LEAF) {
373 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
374 entries = be16_to_cpu(leaf->lf_entries);
375 } else if (type == IS_DINODE) {
376 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
377 entries = be32_to_cpu(dinode->di_entries);
380 hash = gfs2_disk_hash(filename->name, filename->len);
383 if (!dent->de_inum.no_addr) {
388 if (be32_to_cpu(dent->de_hash) == hash &&
389 gfs2_filecmp(filename, (char *)(dent + 1),
390 dent->de_name_len)) {
399 } while (dirent_next(dip, bh, &dent) == 0);
404 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
405 struct buffer_head **bhp)
409 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
410 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
417 * get_leaf_nr - Get a leaf number associated with the index
418 * @dip: The GFS2 inode
422 * Returns: 0 on success, error code otherwise
425 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
431 error = gfs2_jdata_read_mem(dip, (char *)&leaf_no,
432 index * sizeof(uint64_t),
434 if (error != sizeof(uint64_t))
435 return (error < 0) ? error : -EIO;
437 *leaf_out = be64_to_cpu(leaf_no);
442 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
443 struct buffer_head **bh_out)
448 error = get_leaf_nr(dip, index, &leaf_no);
450 error = get_leaf(dip, leaf_no, bh_out);
455 static int get_next_leaf(struct gfs2_inode *dip, struct buffer_head *bh_in,
456 struct buffer_head **bh_out)
458 struct gfs2_leaf *leaf;
461 leaf = (struct gfs2_leaf *)bh_in->b_data;
466 error = get_leaf(dip, be64_to_cpu(leaf->lf_next), bh_out);
471 static int linked_leaf_search(struct gfs2_inode *dip, struct qstr *filename,
472 struct gfs2_dirent **dent_out,
473 struct gfs2_dirent **dent_prev,
474 struct buffer_head **bh_out)
476 struct buffer_head *bh = NULL, *bh_next;
477 uint32_t hsize, index;
481 hsize = 1 << dip->i_di.di_depth;
482 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
483 gfs2_consist_inode(dip);
487 /* Figure out the address of the leaf node. */
489 hash = gfs2_disk_hash(filename->name, filename->len);
490 index = hash >> (32 - dip->i_di.di_depth);
492 error = get_first_leaf(dip, index, &bh_next);
503 error = leaf_search(dip, bh, filename, dent_out, dent_prev);
517 error = get_next_leaf(dip, bh, &bh_next);
527 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
528 * @dip: The GFS2 inode
530 * Returns: 0 on success, error code otherwise
533 static int dir_make_exhash(struct gfs2_inode *dip)
535 struct gfs2_sbd *sdp = dip->i_sbd;
536 struct gfs2_dirent *dent;
537 struct buffer_head *bh, *dibh;
538 struct gfs2_leaf *leaf;
544 error = gfs2_meta_inode_buffer(dip, &dibh);
548 /* Allocate a new block for the first leaf node */
550 bn = gfs2_alloc_meta(dip);
552 /* Turn over a new leaf */
554 bh = gfs2_meta_new(dip->i_gl, bn);
555 gfs2_trans_add_bh(dip->i_gl, bh);
556 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
557 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
559 /* Fill in the leaf structure */
561 leaf = (struct gfs2_leaf *)bh->b_data;
563 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
565 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
566 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
570 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
571 sizeof(struct gfs2_dinode));
573 /* Find last entry */
576 dirent_first(dip, bh, &dent);
579 if (!dent->de_inum.no_addr)
581 if (++x == dip->i_di.di_entries)
584 while (dirent_next(dip, bh, &dent) == 0);
586 /* Adjust the last dirent's record length
587 (Remember that dent still points to the last entry.) */
589 dent->de_rec_len = be32_to_cpu(dent->de_rec_len) +
590 sizeof(struct gfs2_dinode) -
591 sizeof(struct gfs2_leaf);
592 dent->de_rec_len = cpu_to_be32(dent->de_rec_len);
596 /* We're done with the new leaf block, now setup the new
599 gfs2_trans_add_bh(dip->i_gl, dibh);
600 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
602 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
604 for (x = sdp->sd_hash_ptrs; x--; lp++)
605 *lp = cpu_to_be64(bn);
607 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
608 dip->i_di.di_blocks++;
609 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
610 dip->i_di.di_payload_format = 0;
612 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
613 dip->i_di.di_depth = y;
615 gfs2_dinode_out(&dip->i_di, dibh->b_data);
623 * dir_split_leaf - Split a leaf block into two
624 * @dip: The GFS2 inode
628 * Returns: 0 on success, error code on failure
631 static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index,
634 struct buffer_head *nbh, *obh, *dibh;
635 struct gfs2_leaf *nleaf, *oleaf;
636 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
637 uint32_t start, len, half_len, divider;
643 /* Allocate the new leaf block */
645 bn = gfs2_alloc_meta(dip);
647 /* Get the new leaf block */
649 nbh = gfs2_meta_new(dip->i_gl, bn);
650 gfs2_trans_add_bh(dip->i_gl, nbh);
651 gfs2_metatype_set(nbh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
652 gfs2_buffer_clear_tail(nbh, sizeof(struct gfs2_meta_header));
654 nleaf = (struct gfs2_leaf *)nbh->b_data;
656 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
658 /* Get the old leaf block */
660 error = get_leaf(dip, leaf_no, &obh);
664 gfs2_trans_add_bh(dip->i_gl, obh);
666 oleaf = (struct gfs2_leaf *)obh->b_data;
668 /* Compute the start and len of leaf pointers in the hash table. */
670 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
673 gfs2_consist_inode(dip);
678 start = (index & ~(len - 1));
680 /* Change the pointers.
681 Don't bother distinguishing stuffed from non-stuffed.
682 This code is complicated enough already. */
684 lp = kcalloc(half_len, sizeof(uint64_t), GFP_KERNEL | __GFP_NOFAIL);
686 error = gfs2_jdata_read_mem(dip, (char *)lp, start * sizeof(uint64_t),
687 half_len * sizeof(uint64_t));
688 if (error != half_len * sizeof(uint64_t)) {
694 /* Change the pointers */
696 for (x = 0; x < half_len; x++)
697 lp[x] = cpu_to_be64(bn);
699 error = gfs2_jdata_write_mem(dip, (char *)lp, start * sizeof(uint64_t),
700 half_len * sizeof(uint64_t));
701 if (error != half_len * sizeof(uint64_t)) {
709 /* Compute the divider */
711 divider = (start + half_len) << (32 - dip->i_di.di_depth);
713 /* Copy the entries */
715 dirent_first(dip, obh, &dent);
719 if (dirent_next(dip, obh, &next))
722 if (dent->de_inum.no_addr &&
723 be32_to_cpu(dent->de_hash) < divider) {
724 name_len = dent->de_name_len;
726 gfs2_dirent_alloc(dip, nbh, name_len, &new);
728 new->de_inum = dent->de_inum; /* No endian worries */
729 new->de_hash = dent->de_hash; /* No endian worries */
730 new->de_type = dent->de_type; /* No endian worries */
731 memcpy((char *)(new + 1), (char *)(dent + 1),
734 nleaf->lf_entries = be16_to_cpu(nleaf->lf_entries)+1;
735 nleaf->lf_entries = cpu_to_be16(nleaf->lf_entries);
737 dirent_del(dip, obh, prev, dent);
739 if (!oleaf->lf_entries)
740 gfs2_consist_inode(dip);
741 oleaf->lf_entries = be16_to_cpu(oleaf->lf_entries)-1;
742 oleaf->lf_entries = cpu_to_be16(oleaf->lf_entries);
755 /* If none of the entries got moved into the new leaf,
756 artificially fill in the first entry. */
759 gfs2_dirent_alloc(dip, nbh, 0, &new);
760 new->de_inum.no_addr = 0;
763 oleaf->lf_depth = be16_to_cpu(oleaf->lf_depth) + 1;
764 oleaf->lf_depth = cpu_to_be16(oleaf->lf_depth);
765 nleaf->lf_depth = oleaf->lf_depth;
767 error = gfs2_meta_inode_buffer(dip, &dibh);
768 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
769 dip->i_di.di_blocks++;
770 gfs2_dinode_out(&dip->i_di, dibh->b_data);
791 * dir_double_exhash - Double size of ExHash table
792 * @dip: The GFS2 dinode
794 * Returns: 0 on success, error code on failure
797 static int dir_double_exhash(struct gfs2_inode *dip)
799 struct gfs2_sbd *sdp = dip->i_sbd;
800 struct buffer_head *dibh;
808 hsize = 1 << dip->i_di.di_depth;
809 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
810 gfs2_consist_inode(dip);
814 /* Allocate both the "from" and "to" buffers in one big chunk */
816 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
818 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
819 error = gfs2_jdata_read_mem(dip, (char *)buf,
820 block * sdp->sd_hash_bsize,
822 if (error != sdp->sd_hash_bsize) {
829 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
831 for (x = sdp->sd_hash_ptrs; x--; from++) {
832 *to++ = *from; /* No endianess worries */
836 error = gfs2_jdata_write_mem(dip,
837 (char *)buf + sdp->sd_hash_bsize,
838 block * sdp->sd_sb.sb_bsize,
839 sdp->sd_sb.sb_bsize);
840 if (error != sdp->sd_sb.sb_bsize) {
849 error = gfs2_meta_inode_buffer(dip, &dibh);
850 if (!gfs2_assert_withdraw(sdp, !error)) {
851 dip->i_di.di_depth++;
852 gfs2_dinode_out(&dip->i_di, dibh->b_data);
865 * compare_dents - compare directory entries by hash value
869 * When comparing the hash entries of @a to @b:
875 static int compare_dents(const void *a, const void *b)
877 struct gfs2_dirent *dent_a, *dent_b;
878 uint32_t hash_a, hash_b;
881 dent_a = *(struct gfs2_dirent **)a;
882 hash_a = dent_a->de_hash;
883 hash_a = be32_to_cpu(hash_a);
885 dent_b = *(struct gfs2_dirent **)b;
886 hash_b = dent_b->de_hash;
887 hash_b = be32_to_cpu(hash_b);
891 else if (hash_a < hash_b)
894 unsigned int len_a = dent_a->de_name_len;
895 unsigned int len_b = dent_b->de_name_len;
899 else if (len_a < len_b)
902 ret = memcmp((char *)(dent_a + 1),
903 (char *)(dent_b + 1),
911 * do_filldir_main - read out directory entries
912 * @dip: The GFS2 inode
913 * @offset: The offset in the file to read from
914 * @opaque: opaque data to pass to filldir
915 * @filldir: The function to pass entries to
916 * @darr: an array of struct gfs2_dirent pointers to read
917 * @entries: the number of entries in darr
918 * @copied: pointer to int that's non-zero if a entry has been copied out
920 * Jump through some hoops to make sure that if there are hash collsions,
921 * they are read out at the beginning of a buffer. We want to minimize
922 * the possibility that they will fall into different readdir buffers or
923 * that someone will want to seek to that location.
925 * Returns: errno, >0 on exception from filldir
928 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
929 void *opaque, gfs2_filldir_t filldir,
930 struct gfs2_dirent **darr, uint32_t entries,
933 struct gfs2_dirent *dent, *dent_next;
934 struct gfs2_inum inum;
935 uint64_t off, off_next;
940 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
943 off_next = be32_to_cpu(dent_next->de_hash);
944 off_next = gfs2_disk_hash2offset(off_next);
946 for (x = 0, y = 1; x < entries; x++, y++) {
952 off_next = be32_to_cpu(dent_next->de_hash);
953 off_next = gfs2_disk_hash2offset(off_next);
959 if (off_next == off) {
971 gfs2_inum_in(&inum, (char *)&dent->de_inum);
973 error = filldir(opaque, (char *)(dent + 1),
983 /* Increment the *offset by one, so the next time we come into the
984 do_filldir fxn, we get the next entry instead of the last one in the
993 * do_filldir_single - Read directory entries out of a single block
994 * @dip: The GFS2 inode
995 * @offset: The offset in the file to read from
996 * @opaque: opaque data to pass to filldir
997 * @filldir: The function to pass entries to
999 * @entries: the number of entries in the block
1000 * @copied: pointer to int that's non-zero if a entry has been copied out
1002 * Returns: errno, >0 on exception from filldir
1005 static int do_filldir_single(struct gfs2_inode *dip, uint64_t *offset,
1006 void *opaque, gfs2_filldir_t filldir,
1007 struct buffer_head *bh, uint32_t entries,
1010 struct gfs2_dirent **darr;
1011 struct gfs2_dirent *de;
1018 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1022 dirent_first(dip, bh, &de);
1024 if (!de->de_inum.no_addr)
1027 gfs2_consist_inode(dip);
1033 while (dirent_next(dip, bh, &de) == 0);
1036 gfs2_consist_inode(dip);
1041 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1051 * do_filldir_multi - Read directory entries out of a linked leaf list
1052 * @dip: The GFS2 inode
1053 * @offset: The offset in the file to read from
1054 * @opaque: opaque data to pass to filldir
1055 * @filldir: The function to pass entries to
1056 * @bh: the first leaf in the list
1057 * @copied: pointer to int that's non-zero if a entry has been copied out
1059 * Returns: errno, >0 on exception from filldir
1062 static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1063 void *opaque, gfs2_filldir_t filldir,
1064 struct buffer_head *bh, int *copied)
1066 struct buffer_head **larr = NULL;
1067 struct gfs2_dirent **darr;
1068 struct gfs2_leaf *leaf;
1069 struct buffer_head *tmp_bh;
1070 struct gfs2_dirent *de;
1071 unsigned int entries, e = 0;
1072 unsigned int leaves = 0, l = 0;
1077 /* Count leaves and entries */
1079 leaf = (struct gfs2_leaf *)bh->b_data;
1080 entries = be16_to_cpu(leaf->lf_entries);
1084 ln = be64_to_cpu(ln);
1086 error = get_leaf(dip, ln, &tmp_bh);
1090 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1091 if (leaf->lf_entries) {
1092 entries += be16_to_cpu(leaf->lf_entries);
1104 larr = kcalloc(leaves, sizeof(struct buffer_head *),GFP_KERNEL);
1109 darr = kcalloc(entries, sizeof(struct gfs2_dirent *), GFP_KERNEL);
1115 leaf = (struct gfs2_leaf *)bh->b_data;
1116 if (leaf->lf_entries) {
1117 dirent_first(dip, bh, &de);
1119 if (!de->de_inum.no_addr)
1122 gfs2_consist_inode(dip);
1128 while (dirent_next(dip, bh, &de) == 0);
1133 ln = be64_to_cpu(ln);
1135 error = get_leaf(dip, ln, &tmp_bh);
1139 leaf = (struct gfs2_leaf *)tmp_bh->b_data;
1140 if (leaf->lf_entries) {
1141 dirent_first(dip, tmp_bh, &de);
1143 if (!de->de_inum.no_addr)
1146 gfs2_consist_inode(dip);
1152 while (dirent_next(dip, tmp_bh, &de) == 0);
1163 if (gfs2_assert_withdraw(dip->i_sbd, l == leaves)) {
1168 gfs2_consist_inode(dip);
1173 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1178 for (x = 0; x < l; x++)
1186 * dir_e_search - Search exhash (leaf) dir for inode matching name
1187 * @dip: The GFS2 inode
1188 * @filename: Filename string
1189 * @inode: If non-NULL, function fills with formal inode # and block address
1190 * @type: If non-NULL, function fills with DT_... dinode type
1195 static int dir_e_search(struct gfs2_inode *dip, struct qstr *filename,
1196 struct gfs2_inum *inum, unsigned int *type)
1198 struct buffer_head *bh;
1199 struct gfs2_dirent *dent;
1202 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1207 gfs2_inum_in(inum, (char *)&dent->de_inum);
1209 *type = dent->de_type;
1216 static int dir_e_add(struct gfs2_inode *dip, struct qstr *filename,
1217 struct gfs2_inum *inum, unsigned int type)
1219 struct buffer_head *bh, *nbh, *dibh;
1220 struct gfs2_leaf *leaf, *nleaf;
1221 struct gfs2_dirent *dent;
1222 uint32_t hsize, index;
1224 uint64_t leaf_no, bn;
1228 hsize = 1 << dip->i_di.di_depth;
1229 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1230 gfs2_consist_inode(dip);
1234 /* Figure out the address of the leaf node. */
1236 hash = gfs2_disk_hash(filename->name, filename->len);
1237 index = hash >> (32 - dip->i_di.di_depth);
1239 error = get_leaf_nr(dip, index, &leaf_no);
1243 /* Add entry to the leaf */
1246 error = get_leaf(dip, leaf_no, &bh);
1250 leaf = (struct gfs2_leaf *)bh->b_data;
1252 if (gfs2_dirent_alloc(dip, bh, filename->len, &dent)) {
1254 if (be16_to_cpu(leaf->lf_depth) < dip->i_di.di_depth) {
1255 /* Can we split the leaf? */
1259 error = dir_split_leaf(dip, index, leaf_no);
1265 } else if (dip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1266 /* Can we double the hash table? */
1270 error = dir_double_exhash(dip);
1276 } else if (leaf->lf_next) {
1277 /* Can we try the next leaf in the list? */
1278 leaf_no = be64_to_cpu(leaf->lf_next);
1283 /* Create a new leaf and add it to the list. */
1285 bn = gfs2_alloc_meta(dip);
1287 nbh = gfs2_meta_new(dip->i_gl, bn);
1288 gfs2_trans_add_bh(dip->i_gl, nbh);
1289 gfs2_metatype_set(nbh,
1292 gfs2_buffer_clear_tail(nbh,
1293 sizeof(struct gfs2_meta_header));
1295 gfs2_trans_add_bh(dip->i_gl, bh);
1296 leaf->lf_next = cpu_to_be64(bn);
1298 nleaf = (struct gfs2_leaf *)nbh->b_data;
1299 nleaf->lf_depth = leaf->lf_depth;
1300 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
1302 gfs2_dirent_alloc(dip, nbh, filename->len,
1305 dip->i_di.di_blocks++;
1314 /* If the gfs2_dirent_alloc() succeeded, it pinned the "bh" */
1316 gfs2_inum_out(inum, (char *)&dent->de_inum);
1317 dent->de_hash = cpu_to_be32(hash);
1318 dent->de_type = type;
1319 memcpy((char *)(dent + 1), filename->name, filename->len);
1321 leaf->lf_entries = be16_to_cpu(leaf->lf_entries) + 1;
1322 leaf->lf_entries = cpu_to_be16(leaf->lf_entries);
1326 error = gfs2_meta_inode_buffer(dip, &dibh);
1330 dip->i_di.di_entries++;
1331 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1333 gfs2_trans_add_bh(dip->i_gl, dibh);
1334 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1343 static int dir_e_del(struct gfs2_inode *dip, struct qstr *filename)
1345 struct buffer_head *bh, *dibh;
1346 struct gfs2_dirent *dent, *prev;
1347 struct gfs2_leaf *leaf;
1348 unsigned int entries;
1351 error = linked_leaf_search(dip, filename, &dent, &prev, &bh);
1352 if (error == -ENOENT) {
1353 gfs2_consist_inode(dip);
1359 dirent_del(dip, bh, prev, dent); /* Pins bh */
1361 leaf = (struct gfs2_leaf *)bh->b_data;
1362 entries = be16_to_cpu(leaf->lf_entries);
1364 gfs2_consist_inode(dip);
1366 leaf->lf_entries = cpu_to_be16(entries);
1370 error = gfs2_meta_inode_buffer(dip, &dibh);
1374 if (!dip->i_di.di_entries)
1375 gfs2_consist_inode(dip);
1376 dip->i_di.di_entries--;
1377 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1379 gfs2_trans_add_bh(dip->i_gl, dibh);
1380 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1387 * dir_e_read - Reads the entries from a directory into a filldir buffer
1388 * @dip: dinode pointer
1389 * @offset: the hash of the last entry read shifted to the right once
1390 * @opaque: buffer for the filldir function to fill
1391 * @filldir: points to the filldir function to use
1396 static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1397 gfs2_filldir_t filldir)
1399 struct gfs2_sbd *sdp = dip->i_sbd;
1400 struct buffer_head *bh;
1401 struct gfs2_leaf leaf;
1402 uint32_t hsize, len;
1403 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1404 uint32_t hash, index;
1409 hsize = 1 << dip->i_di.di_depth;
1410 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1411 gfs2_consist_inode(dip);
1415 hash = gfs2_dir_offset2hash(*offset);
1416 index = hash >> (32 - dip->i_di.di_depth);
1418 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1422 while (index < hsize) {
1423 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1424 ht_offset = index - lp_offset;
1426 if (ht_offset_cur != ht_offset) {
1427 error = gfs2_jdata_read_mem(dip, (char *)lp,
1428 ht_offset * sizeof(uint64_t),
1429 sdp->sd_hash_bsize);
1430 if (error != sdp->sd_hash_bsize) {
1435 ht_offset_cur = ht_offset;
1438 error = get_leaf(dip, be64_to_cpu(lp[lp_offset]), &bh);
1442 gfs2_leaf_in(&leaf, bh->b_data);
1445 error = do_filldir_multi(dip, offset, opaque, filldir,
1448 error = do_filldir_single(dip, offset, opaque, filldir,
1449 bh, leaf.lf_entries, &copied);
1459 len = 1 << (dip->i_di.di_depth - leaf.lf_depth);
1460 index = (index & ~(len - 1)) + len;
1469 static int dir_e_mvino(struct gfs2_inode *dip, struct qstr *filename,
1470 struct gfs2_inum *inum, unsigned int new_type)
1472 struct buffer_head *bh, *dibh;
1473 struct gfs2_dirent *dent;
1476 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1477 if (error == -ENOENT) {
1478 gfs2_consist_inode(dip);
1484 gfs2_trans_add_bh(dip->i_gl, bh);
1486 gfs2_inum_out(inum, (char *)&dent->de_inum);
1487 dent->de_type = new_type;
1491 error = gfs2_meta_inode_buffer(dip, &dibh);
1495 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1497 gfs2_trans_add_bh(dip->i_gl, dibh);
1498 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1505 * dir_l_search - Search linear (stuffed dinode) dir for inode matching name
1506 * @dip: The GFS2 inode
1507 * @filename: Filename string
1508 * @inode: If non-NULL, function fills with formal inode # and block address
1509 * @type: If non-NULL, function fills with DT_... dinode type
1514 static int dir_l_search(struct gfs2_inode *dip, struct qstr *filename,
1515 struct gfs2_inum *inum, unsigned int *type)
1517 struct buffer_head *dibh;
1518 struct gfs2_dirent *dent;
1521 if (!gfs2_is_stuffed(dip)) {
1522 gfs2_consist_inode(dip);
1526 error = gfs2_meta_inode_buffer(dip, &dibh);
1530 error = leaf_search(dip, dibh, filename, &dent, NULL);
1533 gfs2_inum_in(inum, (char *)&dent->de_inum);
1535 *type = dent->de_type;
1543 static int dir_l_add(struct gfs2_inode *dip, struct qstr *filename,
1544 struct gfs2_inum *inum, unsigned int type)
1546 struct buffer_head *dibh;
1547 struct gfs2_dirent *dent;
1550 if (!gfs2_is_stuffed(dip)) {
1551 gfs2_consist_inode(dip);
1555 error = gfs2_meta_inode_buffer(dip, &dibh);
1559 if (gfs2_dirent_alloc(dip, dibh, filename->len, &dent)) {
1562 error = dir_make_exhash(dip);
1564 error = dir_e_add(dip, filename, inum, type);
1569 /* gfs2_dirent_alloc() pins */
1571 gfs2_inum_out(inum, (char *)&dent->de_inum);
1572 dent->de_hash = gfs2_disk_hash(filename->name, filename->len);
1573 dent->de_hash = cpu_to_be32(dent->de_hash);
1574 dent->de_type = type;
1575 memcpy((char *)(dent + 1), filename->name, filename->len);
1577 dip->i_di.di_entries++;
1578 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1580 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1586 static int dir_l_del(struct gfs2_inode *dip, struct qstr *filename)
1588 struct buffer_head *dibh;
1589 struct gfs2_dirent *dent, *prev;
1592 if (!gfs2_is_stuffed(dip)) {
1593 gfs2_consist_inode(dip);
1597 error = gfs2_meta_inode_buffer(dip, &dibh);
1601 error = leaf_search(dip, dibh, filename, &dent, &prev);
1602 if (error == -ENOENT) {
1603 gfs2_consist_inode(dip);
1610 dirent_del(dip, dibh, prev, dent);
1612 /* dirent_del() pins */
1614 if (!dip->i_di.di_entries)
1615 gfs2_consist_inode(dip);
1616 dip->i_di.di_entries--;
1618 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1620 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1628 static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1629 gfs2_filldir_t filldir)
1631 struct buffer_head *dibh;
1635 if (!gfs2_is_stuffed(dip)) {
1636 gfs2_consist_inode(dip);
1640 if (!dip->i_di.di_entries)
1643 error = gfs2_meta_inode_buffer(dip, &dibh);
1647 error = do_filldir_single(dip, offset,
1649 dibh, dip->i_di.di_entries,
1659 static int dir_l_mvino(struct gfs2_inode *dip, struct qstr *filename,
1660 struct gfs2_inum *inum, unsigned int new_type)
1662 struct buffer_head *dibh;
1663 struct gfs2_dirent *dent;
1666 if (!gfs2_is_stuffed(dip)) {
1667 gfs2_consist_inode(dip);
1671 error = gfs2_meta_inode_buffer(dip, &dibh);
1675 error = leaf_search(dip, dibh, filename, &dent, NULL);
1676 if (error == -ENOENT) {
1677 gfs2_consist_inode(dip);
1684 gfs2_trans_add_bh(dip->i_gl, dibh);
1686 gfs2_inum_out(inum, (char *)&dent->de_inum);
1687 dent->de_type = new_type;
1689 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1691 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1700 * gfs2_dir_search - Search a directory
1701 * @dip: The GFS2 inode
1705 * This routine searches a directory for a file or another directory.
1706 * Assumes a glock is held on dip.
1711 int gfs2_dir_search(struct gfs2_inode *dip, struct qstr *filename,
1712 struct gfs2_inum *inum, unsigned int *type)
1716 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1717 error = dir_e_search(dip, filename, inum, type);
1719 error = dir_l_search(dip, filename, inum, type);
1725 * gfs2_dir_add - Add new filename into directory
1726 * @dip: The GFS2 inode
1727 * @filename: The new name
1728 * @inode: The inode number of the entry
1729 * @type: The type of the entry
1731 * Returns: 0 on success, error code on failure
1734 int gfs2_dir_add(struct gfs2_inode *dip, struct qstr *filename,
1735 struct gfs2_inum *inum, unsigned int type)
1739 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1740 error = dir_e_add(dip, filename, inum, type);
1742 error = dir_l_add(dip, filename, inum, type);
1748 * gfs2_dir_del - Delete a directory entry
1749 * @dip: The GFS2 inode
1750 * @filename: The filename
1752 * Returns: 0 on success, error code on failure
1755 int gfs2_dir_del(struct gfs2_inode *dip, struct qstr *filename)
1759 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1760 error = dir_e_del(dip, filename);
1762 error = dir_l_del(dip, filename);
1767 int gfs2_dir_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1768 gfs2_filldir_t filldir)
1772 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1773 error = dir_e_read(dip, offset, opaque, filldir);
1775 error = dir_l_read(dip, offset, opaque, filldir);
1781 * gfs2_dir_mvino - Change inode number of directory entry
1782 * @dip: The GFS2 inode
1786 * This routine changes the inode number of a directory entry. It's used
1787 * by rename to change ".." when a directory is moved.
1788 * Assumes a glock is held on dvp.
1793 int gfs2_dir_mvino(struct gfs2_inode *dip, struct qstr *filename,
1794 struct gfs2_inum *inum, unsigned int new_type)
1798 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1799 error = dir_e_mvino(dip, filename, inum, new_type);
1801 error = dir_l_mvino(dip, filename, inum, new_type);
1807 * foreach_leaf - call a function for each leaf in a directory
1808 * @dip: the directory
1809 * @lc: the function to call for each each
1810 * @data: private data to pass to it
1815 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1817 struct gfs2_sbd *sdp = dip->i_sbd;
1818 struct buffer_head *bh;
1819 struct gfs2_leaf leaf;
1820 uint32_t hsize, len;
1821 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1827 hsize = 1 << dip->i_di.di_depth;
1828 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1829 gfs2_consist_inode(dip);
1833 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1837 while (index < hsize) {
1838 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1839 ht_offset = index - lp_offset;
1841 if (ht_offset_cur != ht_offset) {
1842 error = gfs2_jdata_read_mem(dip, (char *)lp,
1843 ht_offset * sizeof(uint64_t),
1844 sdp->sd_hash_bsize);
1845 if (error != sdp->sd_hash_bsize) {
1850 ht_offset_cur = ht_offset;
1853 leaf_no = be64_to_cpu(lp[lp_offset]);
1855 error = get_leaf(dip, leaf_no, &bh);
1858 gfs2_leaf_in(&leaf, bh->b_data);
1861 len = 1 << (dip->i_di.di_depth - leaf.lf_depth);
1863 error = lc(dip, index, len, leaf_no, data);
1867 index = (index & ~(len - 1)) + len;
1872 if (index != hsize) {
1873 gfs2_consist_inode(dip);
1884 * leaf_dealloc - Deallocate a directory leaf
1885 * @dip: the directory
1886 * @index: the hash table offset in the directory
1887 * @len: the number of pointers to this leaf
1888 * @leaf_no: the leaf number
1894 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1895 uint64_t leaf_no, void *data)
1897 struct gfs2_sbd *sdp = dip->i_sbd;
1898 struct gfs2_leaf tmp_leaf;
1899 struct gfs2_rgrp_list rlist;
1900 struct buffer_head *bh, *dibh;
1902 unsigned int rg_blocks = 0, l_blocks = 0;
1904 unsigned int x, size = len * sizeof(uint64_t);
1907 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1909 ht = kzalloc(size, GFP_KERNEL);
1913 gfs2_alloc_get(dip);
1915 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1919 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1923 /* Count the number of leaves */
1925 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) {
1926 error = get_leaf(dip, blk, &bh);
1929 gfs2_leaf_in(&tmp_leaf, (bh)->b_data);
1932 gfs2_rlist_add(sdp, &rlist, blk);
1936 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1938 for (x = 0; x < rlist.rl_rgrps; x++) {
1939 struct gfs2_rgrpd *rgd;
1940 rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl);
1941 rg_blocks += rgd->rd_ri.ri_length;
1944 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1948 error = gfs2_trans_begin(sdp,
1949 rg_blocks + (DIV_RU(size, sdp->sd_jbsize) + 1) +
1950 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1952 goto out_rg_gunlock;
1954 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) {
1955 error = get_leaf(dip, blk, &bh);
1958 gfs2_leaf_in(&tmp_leaf, bh->b_data);
1961 gfs2_free_meta(dip, blk, 1);
1963 if (!dip->i_di.di_blocks)
1964 gfs2_consist_inode(dip);
1965 dip->i_di.di_blocks--;
1968 error = gfs2_jdata_write_mem(dip, ht, index * sizeof(uint64_t), size);
1969 if (error != size) {
1975 error = gfs2_meta_inode_buffer(dip, &dibh);
1979 gfs2_trans_add_bh(dip->i_gl, dibh);
1980 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1984 gfs2_trans_end(sdp);
1987 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1990 gfs2_rlist_free(&rlist);
1991 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1994 gfs2_quota_unhold(dip);
1997 gfs2_alloc_put(dip);
2004 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2005 * @dip: the directory
2007 * Dealloc all on-disk directory leaves to FREEMETA state
2008 * Change on-disk inode type to "regular file"
2013 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2015 struct gfs2_sbd *sdp = dip->i_sbd;
2016 struct buffer_head *bh;
2019 /* Dealloc on-disk leaves to FREEMETA state */
2020 error = foreach_leaf(dip, leaf_dealloc, NULL);
2024 /* Make this a regular file in case we crash.
2025 (We don't want to free these blocks a second time.) */
2027 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
2031 error = gfs2_meta_inode_buffer(dip, &bh);
2033 gfs2_trans_add_bh(dip->i_gl, bh);
2034 ((struct gfs2_dinode *)bh->b_data)->di_mode = cpu_to_be32(S_IFREG);
2038 gfs2_trans_end(sdp);
2044 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2045 * @ip: the file being written to
2046 * @filname: the filename that's going to be added
2047 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
2052 int gfs2_diradd_alloc_required(struct gfs2_inode *dip, struct qstr *filename,
2053 int *alloc_required)
2055 struct buffer_head *bh = NULL, *bh_next;
2056 uint32_t hsize, hash, index;
2059 *alloc_required = 0;
2061 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
2062 hsize = 1 << dip->i_di.di_depth;
2063 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
2064 gfs2_consist_inode(dip);
2068 hash = gfs2_disk_hash(filename->name, filename->len);
2069 index = hash >> (32 - dip->i_di.di_depth);
2071 error = get_first_leaf(dip, index, &bh_next);
2080 if (dirent_fits(dip, bh, filename->len))
2083 error = get_next_leaf(dip, bh, &bh_next);
2084 if (error == -ENOENT) {
2085 *alloc_required = 1;
2094 error = gfs2_meta_inode_buffer(dip, &bh);
2098 if (!dirent_fits(dip, bh, filename->len))
2099 *alloc_required = 1;