2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
39 * dip->i_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
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the same leaf.
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/spinlock.h>
59 #include <linux/buffer_head.h>
60 #include <linux/sort.h>
61 #include <linux/gfs2_ondisk.h>
62 #include <linux/crc32.h>
63 #include <linux/vmalloc.h>
64 #include <linux/lm_interface.h>
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
84 typedef int (*leaf_call_t
) (struct gfs2_inode
*dip
, u32 index
, u32 len
,
85 u64 leaf_no
, void *data
);
86 typedef int (*gfs2_dscan_t
)(const struct gfs2_dirent
*dent
,
87 const struct qstr
*name
, void *opaque
);
90 int gfs2_dir_get_new_buffer(struct gfs2_inode
*ip
, u64 block
,
91 struct buffer_head
**bhp
)
93 struct buffer_head
*bh
;
95 bh
= gfs2_meta_new(ip
->i_gl
, block
);
96 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
97 gfs2_metatype_set(bh
, GFS2_METATYPE_JD
, GFS2_FORMAT_JD
);
98 gfs2_buffer_clear_tail(bh
, sizeof(struct gfs2_meta_header
));
103 static int gfs2_dir_get_existing_buffer(struct gfs2_inode
*ip
, u64 block
,
104 struct buffer_head
**bhp
)
106 struct buffer_head
*bh
;
109 error
= gfs2_meta_read(ip
->i_gl
, block
, DIO_WAIT
, &bh
);
112 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_JD
)) {
120 static int gfs2_dir_write_stuffed(struct gfs2_inode
*ip
, const char *buf
,
121 unsigned int offset
, unsigned int size
)
123 struct buffer_head
*dibh
;
126 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
130 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
131 memcpy(dibh
->b_data
+ offset
+ sizeof(struct gfs2_dinode
), buf
, size
);
132 if (ip
->i_di
.di_size
< offset
+ size
)
133 ip
->i_di
.di_size
= offset
+ size
;
134 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
135 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
151 * Returns: The number of bytes correctly written or error code
153 static int gfs2_dir_write_data(struct gfs2_inode
*ip
, const char *buf
,
154 u64 offset
, unsigned int size
)
156 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
157 struct buffer_head
*dibh
;
167 if (gfs2_is_stuffed(ip
) &&
168 offset
+ size
<= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
169 return gfs2_dir_write_stuffed(ip
, buf
, (unsigned int)offset
,
172 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
175 if (gfs2_is_stuffed(ip
)) {
176 error
= gfs2_unstuff_dinode(ip
, NULL
);
182 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
184 while (copied
< size
) {
186 struct buffer_head
*bh
;
189 amount
= size
- copied
;
190 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
191 amount
= sdp
->sd_sb
.sb_bsize
- o
;
195 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
200 if (gfs2_assert_withdraw(sdp
, dblock
))
204 if (amount
== sdp
->sd_jbsize
|| new)
205 error
= gfs2_dir_get_new_buffer(ip
, dblock
, &bh
);
207 error
= gfs2_dir_get_existing_buffer(ip
, dblock
, &bh
);
212 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
213 memcpy(bh
->b_data
+ o
, buf
, amount
);
224 o
= sizeof(struct gfs2_meta_header
);
228 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
232 if (ip
->i_di
.di_size
< offset
+ copied
)
233 ip
->i_di
.di_size
= offset
+ copied
;
234 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
236 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
237 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
247 static int gfs2_dir_read_stuffed(struct gfs2_inode
*ip
, char *buf
,
248 u64 offset
, unsigned int size
)
250 struct buffer_head
*dibh
;
253 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
255 offset
+= sizeof(struct gfs2_dinode
);
256 memcpy(buf
, dibh
->b_data
+ offset
, size
);
260 return (error
) ? error
: size
;
265 * gfs2_dir_read_data - Read a data from a directory inode
266 * @ip: The GFS2 Inode
267 * @buf: The buffer to place result into
268 * @offset: File offset to begin jdata_readng from
269 * @size: Amount of data to transfer
271 * Returns: The amount of data actually copied or the error
273 static int gfs2_dir_read_data(struct gfs2_inode
*ip
, char *buf
, u64 offset
,
274 unsigned int size
, unsigned ra
)
276 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
283 if (offset
>= ip
->i_di
.di_size
)
286 if (offset
+ size
> ip
->i_di
.di_size
)
287 size
= ip
->i_di
.di_size
- offset
;
292 if (gfs2_is_stuffed(ip
))
293 return gfs2_dir_read_stuffed(ip
, buf
, offset
, size
);
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_extent_map(&ip
->i_inode
, lblock
, &new,
314 if (error
|| !dblock
)
319 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
322 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_WAIT
, &bh
);
326 error
= gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_JD
);
333 memcpy(buf
, bh
->b_data
+ o
, amount
);
339 o
= sizeof(struct gfs2_meta_header
);
344 return (copied
) ? copied
: error
;
347 static inline int __gfs2_dirent_find(const struct gfs2_dirent
*dent
,
348 const struct qstr
*name
, int ret
)
350 if (dent
->de_inum
.no_addr
!= 0 &&
351 be32_to_cpu(dent
->de_hash
) == name
->hash
&&
352 be16_to_cpu(dent
->de_name_len
) == name
->len
&&
353 memcmp(dent
+1, name
->name
, name
->len
) == 0)
358 static int gfs2_dirent_find(const struct gfs2_dirent
*dent
,
359 const struct qstr
*name
,
362 return __gfs2_dirent_find(dent
, name
, 1);
365 static int gfs2_dirent_prev(const struct gfs2_dirent
*dent
,
366 const struct qstr
*name
,
369 return __gfs2_dirent_find(dent
, name
, 2);
373 * name->name holds ptr to start of block.
374 * name->len holds size of block.
376 static int gfs2_dirent_last(const struct gfs2_dirent
*dent
,
377 const struct qstr
*name
,
380 const char *start
= name
->name
;
381 const char *end
= (const char *)dent
+ be16_to_cpu(dent
->de_rec_len
);
382 if (name
->len
== (end
- start
))
387 static int gfs2_dirent_find_space(const struct gfs2_dirent
*dent
,
388 const struct qstr
*name
,
391 unsigned required
= GFS2_DIRENT_SIZE(name
->len
);
392 unsigned actual
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
393 unsigned totlen
= be16_to_cpu(dent
->de_rec_len
);
395 if (!dent
->de_inum
.no_addr
)
396 actual
= GFS2_DIRENT_SIZE(0);
397 if (totlen
- actual
>= required
)
402 struct dirent_gather
{
403 const struct gfs2_dirent
**pdent
;
407 static int gfs2_dirent_gather(const struct gfs2_dirent
*dent
,
408 const struct qstr
*name
,
411 struct dirent_gather
*g
= opaque
;
412 if (dent
->de_inum
.no_addr
) {
413 g
->pdent
[g
->offset
++] = dent
;
419 * Other possible things to check:
420 * - Inode located within filesystem size (and on valid block)
421 * - Valid directory entry type
422 * Not sure how heavy-weight we want to make this... could also check
423 * hash is correct for example, but that would take a lot of extra time.
424 * For now the most important thing is to check that the various sizes
427 static int gfs2_check_dirent(struct gfs2_dirent
*dent
, unsigned int offset
,
428 unsigned int size
, unsigned int len
, int first
)
430 const char *msg
= "gfs2_dirent too small";
431 if (unlikely(size
< sizeof(struct gfs2_dirent
)))
433 msg
= "gfs2_dirent misaligned";
434 if (unlikely(offset
& 0x7))
436 msg
= "gfs2_dirent points beyond end of block";
437 if (unlikely(offset
+ size
> len
))
439 msg
= "zero inode number";
440 if (unlikely(!first
&& !dent
->de_inum
.no_addr
))
442 msg
= "name length is greater than space in dirent";
443 if (dent
->de_inum
.no_addr
&&
444 unlikely(sizeof(struct gfs2_dirent
)+be16_to_cpu(dent
->de_name_len
) >
449 printk(KERN_WARNING
"gfs2_check_dirent: %s (%s)\n", msg
,
450 first
? "first in block" : "not first in block");
454 static int gfs2_dirent_offset(const void *buf
)
456 const struct gfs2_meta_header
*h
= buf
;
461 switch(be32_to_cpu(h
->mh_type
)) {
462 case GFS2_METATYPE_LF
:
463 offset
= sizeof(struct gfs2_leaf
);
465 case GFS2_METATYPE_DI
:
466 offset
= sizeof(struct gfs2_dinode
);
473 printk(KERN_WARNING
"gfs2_scan_dirent: wrong block type %u\n",
474 be32_to_cpu(h
->mh_type
));
478 static struct gfs2_dirent
*gfs2_dirent_scan(struct inode
*inode
, void *buf
,
479 unsigned int len
, gfs2_dscan_t scan
,
480 const struct qstr
*name
,
483 struct gfs2_dirent
*dent
, *prev
;
488 ret
= gfs2_dirent_offset(buf
);
495 size
= be16_to_cpu(dent
->de_rec_len
);
496 if (gfs2_check_dirent(dent
, offset
, size
, len
, 1))
499 ret
= scan(dent
, name
, opaque
);
507 size
= be16_to_cpu(dent
->de_rec_len
);
508 if (gfs2_check_dirent(dent
, offset
, size
, len
, 0))
518 return prev
? prev
: dent
;
525 gfs2_consist_inode(GFS2_I(inode
));
526 return ERR_PTR(-EIO
);
531 * dirent_first - Return the first dirent
532 * @dip: the directory
534 * @dent: Pointer to list of dirents
536 * return first dirent whether bh points to leaf or stuffed dinode
538 * Returns: IS_LEAF, IS_DINODE, or -errno
541 static int dirent_first(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
542 struct gfs2_dirent
**dent
)
544 struct gfs2_meta_header
*h
= (struct gfs2_meta_header
*)bh
->b_data
;
546 if (be32_to_cpu(h
->mh_type
) == GFS2_METATYPE_LF
) {
547 if (gfs2_meta_check(GFS2_SB(&dip
->i_inode
), bh
))
549 *dent
= (struct gfs2_dirent
*)(bh
->b_data
+
550 sizeof(struct gfs2_leaf
));
553 if (gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), bh
, GFS2_METATYPE_DI
))
555 *dent
= (struct gfs2_dirent
*)(bh
->b_data
+
556 sizeof(struct gfs2_dinode
));
561 static int dirent_check_reclen(struct gfs2_inode
*dip
,
562 const struct gfs2_dirent
*d
, const void *end_p
)
565 u16 rec_len
= be16_to_cpu(d
->de_rec_len
);
567 if (unlikely(rec_len
< sizeof(struct gfs2_dirent
)))
575 gfs2_consist_inode(dip
);
580 * dirent_next - Next dirent
581 * @dip: the directory
583 * @dent: Pointer to list of dirents
585 * Returns: 0 on success, error code otherwise
588 static int dirent_next(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
589 struct gfs2_dirent
**dent
)
591 struct gfs2_dirent
*cur
= *dent
, *tmp
;
592 char *bh_end
= bh
->b_data
+ bh
->b_size
;
595 ret
= dirent_check_reclen(dip
, cur
, bh_end
);
599 tmp
= (void *)cur
+ ret
;
600 ret
= dirent_check_reclen(dip
, tmp
, bh_end
);
604 /* Only the first dent could ever have de_inum.no_addr == 0 */
605 if (!tmp
->de_inum
.no_addr
) {
606 gfs2_consist_inode(dip
);
615 * dirent_del - Delete a dirent
616 * @dip: The GFS2 inode
618 * @prev: The previous dirent
619 * @cur: The current dirent
623 static void dirent_del(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
624 struct gfs2_dirent
*prev
, struct gfs2_dirent
*cur
)
626 u16 cur_rec_len
, prev_rec_len
;
628 if (!cur
->de_inum
.no_addr
) {
629 gfs2_consist_inode(dip
);
633 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
635 /* If there is no prev entry, this is the first entry in the block.
636 The de_rec_len is already as big as it needs to be. Just zero
637 out the inode number and return. */
640 cur
->de_inum
.no_addr
= 0; /* No endianess worries */
644 /* Combine this dentry with the previous one. */
646 prev_rec_len
= be16_to_cpu(prev
->de_rec_len
);
647 cur_rec_len
= be16_to_cpu(cur
->de_rec_len
);
649 if ((char *)prev
+ prev_rec_len
!= (char *)cur
)
650 gfs2_consist_inode(dip
);
651 if ((char *)cur
+ cur_rec_len
> bh
->b_data
+ bh
->b_size
)
652 gfs2_consist_inode(dip
);
654 prev_rec_len
+= cur_rec_len
;
655 prev
->de_rec_len
= cpu_to_be16(prev_rec_len
);
659 * Takes a dent from which to grab space as an argument. Returns the
660 * newly created dent.
662 static struct gfs2_dirent
*gfs2_init_dirent(struct inode
*inode
,
663 struct gfs2_dirent
*dent
,
664 const struct qstr
*name
,
665 struct buffer_head
*bh
)
667 struct gfs2_inode
*ip
= GFS2_I(inode
);
668 struct gfs2_dirent
*ndent
;
669 unsigned offset
= 0, totlen
;
671 if (dent
->de_inum
.no_addr
)
672 offset
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
673 totlen
= be16_to_cpu(dent
->de_rec_len
);
674 BUG_ON(offset
+ name
->len
> totlen
);
675 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
676 ndent
= (struct gfs2_dirent
*)((char *)dent
+ offset
);
677 dent
->de_rec_len
= cpu_to_be16(offset
);
678 gfs2_qstr2dirent(name
, totlen
- offset
, ndent
);
682 static struct gfs2_dirent
*gfs2_dirent_alloc(struct inode
*inode
,
683 struct buffer_head
*bh
,
684 const struct qstr
*name
)
686 struct gfs2_dirent
*dent
;
687 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
688 gfs2_dirent_find_space
, name
, NULL
);
689 if (!dent
|| IS_ERR(dent
))
691 return gfs2_init_dirent(inode
, dent
, name
, bh
);
694 static int get_leaf(struct gfs2_inode
*dip
, u64 leaf_no
,
695 struct buffer_head
**bhp
)
699 error
= gfs2_meta_read(dip
->i_gl
, leaf_no
, DIO_WAIT
, bhp
);
700 if (!error
&& gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), *bhp
, GFS2_METATYPE_LF
)) {
701 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
709 * get_leaf_nr - Get a leaf number associated with the index
710 * @dip: The GFS2 inode
714 * Returns: 0 on success, error code otherwise
717 static int get_leaf_nr(struct gfs2_inode
*dip
, u32 index
,
723 error
= gfs2_dir_read_data(dip
, (char *)&leaf_no
,
726 if (error
!= sizeof(u64
))
727 return (error
< 0) ? error
: -EIO
;
729 *leaf_out
= be64_to_cpu(leaf_no
);
734 static int get_first_leaf(struct gfs2_inode
*dip
, u32 index
,
735 struct buffer_head
**bh_out
)
740 error
= get_leaf_nr(dip
, index
, &leaf_no
);
742 error
= get_leaf(dip
, leaf_no
, bh_out
);
747 static struct gfs2_dirent
*gfs2_dirent_search(struct inode
*inode
,
748 const struct qstr
*name
,
750 struct buffer_head
**pbh
)
752 struct buffer_head
*bh
;
753 struct gfs2_dirent
*dent
;
754 struct gfs2_inode
*ip
= GFS2_I(inode
);
757 if (ip
->i_di
.di_flags
& GFS2_DIF_EXHASH
) {
758 struct gfs2_leaf
*leaf
;
759 unsigned hsize
= 1 << ip
->i_di
.di_depth
;
762 if (hsize
* sizeof(u64
) != ip
->i_di
.di_size
) {
763 gfs2_consist_inode(ip
);
764 return ERR_PTR(-EIO
);
767 index
= name
->hash
>> (32 - ip
->i_di
.di_depth
);
768 error
= get_first_leaf(ip
, index
, &bh
);
770 return ERR_PTR(error
);
772 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
776 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
777 ln
= be64_to_cpu(leaf
->lf_next
);
782 error
= get_leaf(ip
, ln
, &bh
);
785 return error
? ERR_PTR(error
) : NULL
;
789 error
= gfs2_meta_inode_buffer(ip
, &bh
);
791 return ERR_PTR(error
);
792 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
, scan
, name
, NULL
);
794 if (unlikely(dent
== NULL
|| IS_ERR(dent
))) {
802 static struct gfs2_leaf
*new_leaf(struct inode
*inode
, struct buffer_head
**pbh
, u16 depth
)
804 struct gfs2_inode
*ip
= GFS2_I(inode
);
805 u64 bn
= gfs2_alloc_meta(ip
);
806 struct buffer_head
*bh
= gfs2_meta_new(ip
->i_gl
, bn
);
807 struct gfs2_leaf
*leaf
;
808 struct gfs2_dirent
*dent
;
809 struct qstr name
= { .name
= "", .len
= 0, .hash
= 0 };
813 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
814 gfs2_metatype_set(bh
, GFS2_METATYPE_LF
, GFS2_FORMAT_LF
);
815 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
816 leaf
->lf_depth
= cpu_to_be16(depth
);
817 leaf
->lf_entries
= 0;
818 leaf
->lf_dirent_format
= cpu_to_be16(GFS2_FORMAT_DE
);
820 memset(leaf
->lf_reserved
, 0, sizeof(leaf
->lf_reserved
));
821 dent
= (struct gfs2_dirent
*)(leaf
+1);
822 gfs2_qstr2dirent(&name
, bh
->b_size
- sizeof(struct gfs2_leaf
), dent
);
828 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
829 * @dip: The GFS2 inode
831 * Returns: 0 on success, error code otherwise
834 static int dir_make_exhash(struct inode
*inode
)
836 struct gfs2_inode
*dip
= GFS2_I(inode
);
837 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
838 struct gfs2_dirent
*dent
;
840 struct buffer_head
*bh
, *dibh
;
841 struct gfs2_leaf
*leaf
;
847 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
851 /* Turn over a new leaf */
853 leaf
= new_leaf(inode
, &bh
, 0);
858 gfs2_assert(sdp
, dip
->i_di
.di_entries
< (1 << 16));
859 leaf
->lf_entries
= cpu_to_be16(dip
->i_di
.di_entries
);
863 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_leaf
), dibh
,
864 sizeof(struct gfs2_dinode
));
866 /* Find last entry */
869 args
.len
= bh
->b_size
- sizeof(struct gfs2_dinode
) +
870 sizeof(struct gfs2_leaf
);
871 args
.name
= bh
->b_data
;
872 dent
= gfs2_dirent_scan(&dip
->i_inode
, bh
->b_data
, bh
->b_size
,
873 gfs2_dirent_last
, &args
, NULL
);
882 return PTR_ERR(dent
);
885 /* Adjust the last dirent's record length
886 (Remember that dent still points to the last entry.) */
888 dent
->de_rec_len
= cpu_to_be16(be16_to_cpu(dent
->de_rec_len
) +
889 sizeof(struct gfs2_dinode
) -
890 sizeof(struct gfs2_leaf
));
894 /* We're done with the new leaf block, now setup the new
897 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
898 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
900 lp
= (u64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
902 for (x
= sdp
->sd_hash_ptrs
; x
--; lp
++)
903 *lp
= cpu_to_be64(bn
);
905 dip
->i_di
.di_size
= sdp
->sd_sb
.sb_bsize
/ 2;
906 dip
->i_di
.di_blocks
++;
907 dip
->i_di
.di_flags
|= GFS2_DIF_EXHASH
;
908 dip
->i_di
.di_payload_format
= 0;
910 for (x
= sdp
->sd_hash_ptrs
, y
= -1; x
; x
>>= 1, y
++) ;
911 dip
->i_di
.di_depth
= y
;
913 gfs2_dinode_out(&dip
->i_di
, dibh
->b_data
);
921 * dir_split_leaf - Split a leaf block into two
922 * @dip: The GFS2 inode
926 * Returns: 0 on success, error code on failure
929 static int dir_split_leaf(struct inode
*inode
, const struct qstr
*name
)
931 struct gfs2_inode
*dip
= GFS2_I(inode
);
932 struct buffer_head
*nbh
, *obh
, *dibh
;
933 struct gfs2_leaf
*nleaf
, *oleaf
;
934 struct gfs2_dirent
*dent
= NULL
, *prev
= NULL
, *next
= NULL
, *new;
935 u32 start
, len
, half_len
, divider
;
936 u64 bn
, *lp
, leaf_no
;
941 index
= name
->hash
>> (32 - dip
->i_di
.di_depth
);
942 error
= get_leaf_nr(dip
, index
, &leaf_no
);
946 /* Get the old leaf block */
947 error
= get_leaf(dip
, leaf_no
, &obh
);
951 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
952 if (dip
->i_di
.di_depth
== be16_to_cpu(oleaf
->lf_depth
)) {
954 return 1; /* can't split */
957 gfs2_trans_add_bh(dip
->i_gl
, obh
, 1);
959 nleaf
= new_leaf(inode
, &nbh
, be16_to_cpu(oleaf
->lf_depth
) + 1);
966 /* Compute the start and len of leaf pointers in the hash table. */
967 len
= 1 << (dip
->i_di
.di_depth
- be16_to_cpu(oleaf
->lf_depth
));
970 printk(KERN_WARNING
"di_depth %u lf_depth %u index %u\n", dip
->i_di
.di_depth
, be16_to_cpu(oleaf
->lf_depth
), index
);
971 gfs2_consist_inode(dip
);
976 start
= (index
& ~(len
- 1));
978 /* Change the pointers.
979 Don't bother distinguishing stuffed from non-stuffed.
980 This code is complicated enough already. */
981 lp
= kmalloc(half_len
* sizeof(u64
), GFP_NOFS
| __GFP_NOFAIL
);
982 /* Change the pointers */
983 for (x
= 0; x
< half_len
; x
++)
984 lp
[x
] = cpu_to_be64(bn
);
986 error
= gfs2_dir_write_data(dip
, (char *)lp
, start
* sizeof(u64
),
987 half_len
* sizeof(u64
));
988 if (error
!= half_len
* sizeof(u64
)) {
996 /* Compute the divider */
997 divider
= (start
+ half_len
) << (32 - dip
->i_di
.di_depth
);
999 /* Copy the entries */
1000 dirent_first(dip
, obh
, &dent
);
1004 if (dirent_next(dip
, obh
, &next
))
1007 if (dent
->de_inum
.no_addr
&&
1008 be32_to_cpu(dent
->de_hash
) < divider
) {
1010 str
.name
= (char*)(dent
+1);
1011 str
.len
= be16_to_cpu(dent
->de_name_len
);
1012 str
.hash
= be32_to_cpu(dent
->de_hash
);
1013 new = gfs2_dirent_alloc(inode
, nbh
, &str
);
1015 error
= PTR_ERR(new);
1019 new->de_inum
= dent
->de_inum
; /* No endian worries */
1020 new->de_type
= dent
->de_type
; /* No endian worries */
1021 nleaf
->lf_entries
= cpu_to_be16(be16_to_cpu(nleaf
->lf_entries
)+1);
1023 dirent_del(dip
, obh
, prev
, dent
);
1025 if (!oleaf
->lf_entries
)
1026 gfs2_consist_inode(dip
);
1027 oleaf
->lf_entries
= cpu_to_be16(be16_to_cpu(oleaf
->lf_entries
)-1);
1039 oleaf
->lf_depth
= nleaf
->lf_depth
;
1041 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1042 if (!gfs2_assert_withdraw(GFS2_SB(&dip
->i_inode
), !error
)) {
1043 dip
->i_di
.di_blocks
++;
1044 gfs2_dinode_out(&dip
->i_di
, dibh
->b_data
);
1063 * dir_double_exhash - Double size of ExHash table
1064 * @dip: The GFS2 dinode
1066 * Returns: 0 on success, error code on failure
1069 static int dir_double_exhash(struct gfs2_inode
*dip
)
1071 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1072 struct buffer_head
*dibh
;
1080 hsize
= 1 << dip
->i_di
.di_depth
;
1081 if (hsize
* sizeof(u64
) != dip
->i_di
.di_size
) {
1082 gfs2_consist_inode(dip
);
1086 /* Allocate both the "from" and "to" buffers in one big chunk */
1088 buf
= kcalloc(3, sdp
->sd_hash_bsize
, GFP_KERNEL
| __GFP_NOFAIL
);
1090 for (block
= dip
->i_di
.di_size
>> sdp
->sd_hash_bsize_shift
; block
--;) {
1091 error
= gfs2_dir_read_data(dip
, (char *)buf
,
1092 block
* sdp
->sd_hash_bsize
,
1093 sdp
->sd_hash_bsize
, 1);
1094 if (error
!= sdp
->sd_hash_bsize
) {
1101 to
= (u64
*)((char *)buf
+ sdp
->sd_hash_bsize
);
1103 for (x
= sdp
->sd_hash_ptrs
; x
--; from
++) {
1104 *to
++ = *from
; /* No endianess worries */
1108 error
= gfs2_dir_write_data(dip
,
1109 (char *)buf
+ sdp
->sd_hash_bsize
,
1110 block
* sdp
->sd_sb
.sb_bsize
,
1111 sdp
->sd_sb
.sb_bsize
);
1112 if (error
!= sdp
->sd_sb
.sb_bsize
) {
1121 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1122 if (!gfs2_assert_withdraw(sdp
, !error
)) {
1123 dip
->i_di
.di_depth
++;
1124 gfs2_dinode_out(&dip
->i_di
, dibh
->b_data
);
1136 * compare_dents - compare directory entries by hash value
1140 * When comparing the hash entries of @a to @b:
1146 static int compare_dents(const void *a
, const void *b
)
1148 const struct gfs2_dirent
*dent_a
, *dent_b
;
1152 dent_a
= *(const struct gfs2_dirent
**)a
;
1153 hash_a
= be32_to_cpu(dent_a
->de_hash
);
1155 dent_b
= *(const struct gfs2_dirent
**)b
;
1156 hash_b
= be32_to_cpu(dent_b
->de_hash
);
1158 if (hash_a
> hash_b
)
1160 else if (hash_a
< hash_b
)
1163 unsigned int len_a
= be16_to_cpu(dent_a
->de_name_len
);
1164 unsigned int len_b
= be16_to_cpu(dent_b
->de_name_len
);
1168 else if (len_a
< len_b
)
1171 ret
= memcmp(dent_a
+ 1, dent_b
+ 1, len_a
);
1178 * do_filldir_main - read out directory entries
1179 * @dip: The GFS2 inode
1180 * @offset: The offset in the file to read from
1181 * @opaque: opaque data to pass to filldir
1182 * @filldir: The function to pass entries to
1183 * @darr: an array of struct gfs2_dirent pointers to read
1184 * @entries: the number of entries in darr
1185 * @copied: pointer to int that's non-zero if a entry has been copied out
1187 * Jump through some hoops to make sure that if there are hash collsions,
1188 * they are read out at the beginning of a buffer. We want to minimize
1189 * the possibility that they will fall into different readdir buffers or
1190 * that someone will want to seek to that location.
1192 * Returns: errno, >0 on exception from filldir
1195 static int do_filldir_main(struct gfs2_inode
*dip
, u64
*offset
,
1196 void *opaque
, gfs2_filldir_t filldir
,
1197 const struct gfs2_dirent
**darr
, u32 entries
,
1200 const struct gfs2_dirent
*dent
, *dent_next
;
1201 struct gfs2_inum inum
;
1207 sort(darr
, entries
, sizeof(struct gfs2_dirent
*), compare_dents
, NULL
);
1209 dent_next
= darr
[0];
1210 off_next
= be32_to_cpu(dent_next
->de_hash
);
1211 off_next
= gfs2_disk_hash2offset(off_next
);
1213 for (x
= 0, y
= 1; x
< entries
; x
++, y
++) {
1218 dent_next
= darr
[y
];
1219 off_next
= be32_to_cpu(dent_next
->de_hash
);
1220 off_next
= gfs2_disk_hash2offset(off_next
);
1226 if (off_next
== off
) {
1227 if (*copied
&& !run
)
1238 gfs2_inum_in(&inum
, (char *)&dent
->de_inum
);
1240 error
= filldir(opaque
, (const char *)(dent
+ 1),
1241 be16_to_cpu(dent
->de_name_len
),
1243 be16_to_cpu(dent
->de_type
));
1250 /* Increment the *offset by one, so the next time we come into the
1251 do_filldir fxn, we get the next entry instead of the last one in the
1259 static int gfs2_dir_read_leaf(struct inode
*inode
, u64
*offset
, void *opaque
,
1260 gfs2_filldir_t filldir
, int *copied
,
1261 unsigned *depth
, u64 leaf_no
)
1263 struct gfs2_inode
*ip
= GFS2_I(inode
);
1264 struct buffer_head
*bh
;
1265 struct gfs2_leaf
*lf
;
1266 unsigned entries
= 0;
1267 unsigned leaves
= 0;
1268 const struct gfs2_dirent
**darr
, *dent
;
1269 struct dirent_gather g
;
1270 struct buffer_head
**larr
;
1276 error
= get_leaf(ip
, lfn
, &bh
);
1279 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1281 *depth
= be16_to_cpu(lf
->lf_depth
);
1282 entries
+= be16_to_cpu(lf
->lf_entries
);
1284 lfn
= be64_to_cpu(lf
->lf_next
);
1292 larr
= vmalloc((leaves
+ entries
) * sizeof(void *));
1295 darr
= (const struct gfs2_dirent
**)(larr
+ leaves
);
1301 error
= get_leaf(ip
, lfn
, &bh
);
1304 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1305 lfn
= be64_to_cpu(lf
->lf_next
);
1306 if (lf
->lf_entries
) {
1307 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
1308 gfs2_dirent_gather
, NULL
, &g
);
1309 error
= PTR_ERR(dent
);
1320 error
= do_filldir_main(ip
, offset
, opaque
, filldir
, darr
,
1323 for(i
= 0; i
< leaf
; i
++)
1331 * dir_e_read - Reads the entries from a directory into a filldir buffer
1332 * @dip: dinode pointer
1333 * @offset: the hash of the last entry read shifted to the right once
1334 * @opaque: buffer for the filldir function to fill
1335 * @filldir: points to the filldir function to use
1340 static int dir_e_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1341 gfs2_filldir_t filldir
)
1343 struct gfs2_inode
*dip
= GFS2_I(inode
);
1344 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1346 u32 ht_offset
, lp_offset
, ht_offset_cur
= -1;
1353 hsize
= 1 << dip
->i_di
.di_depth
;
1354 if (hsize
* sizeof(u64
) != dip
->i_di
.di_size
) {
1355 gfs2_consist_inode(dip
);
1359 hash
= gfs2_dir_offset2hash(*offset
);
1360 index
= hash
>> (32 - dip
->i_di
.di_depth
);
1362 lp
= kmalloc(sdp
->sd_hash_bsize
, GFP_KERNEL
);
1366 while (index
< hsize
) {
1367 lp_offset
= index
& (sdp
->sd_hash_ptrs
- 1);
1368 ht_offset
= index
- lp_offset
;
1370 if (ht_offset_cur
!= ht_offset
) {
1371 error
= gfs2_dir_read_data(dip
, (char *)lp
,
1372 ht_offset
* sizeof(u64
),
1373 sdp
->sd_hash_bsize
, 1);
1374 if (error
!= sdp
->sd_hash_bsize
) {
1379 ht_offset_cur
= ht_offset
;
1382 error
= gfs2_dir_read_leaf(inode
, offset
, opaque
, filldir
,
1384 be64_to_cpu(lp
[lp_offset
]));
1388 len
= 1 << (dip
->i_di
.di_depth
- depth
);
1389 index
= (index
& ~(len
- 1)) + len
;
1399 int gfs2_dir_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1400 gfs2_filldir_t filldir
)
1402 struct gfs2_inode
*dip
= GFS2_I(inode
);
1403 struct dirent_gather g
;
1404 const struct gfs2_dirent
**darr
, *dent
;
1405 struct buffer_head
*dibh
;
1409 if (!dip
->i_di
.di_entries
)
1412 if (dip
->i_di
.di_flags
& GFS2_DIF_EXHASH
)
1413 return dir_e_read(inode
, offset
, opaque
, filldir
);
1415 if (!gfs2_is_stuffed(dip
)) {
1416 gfs2_consist_inode(dip
);
1420 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1425 darr
= kmalloc(dip
->i_di
.di_entries
* sizeof(struct gfs2_dirent
*),
1430 dent
= gfs2_dirent_scan(inode
, dibh
->b_data
, dibh
->b_size
,
1431 gfs2_dirent_gather
, NULL
, &g
);
1433 error
= PTR_ERR(dent
);
1436 error
= do_filldir_main(dip
, offset
, opaque
, filldir
, darr
,
1437 dip
->i_di
.di_entries
, &copied
);
1451 * gfs2_dir_search - Search a directory
1452 * @dip: The GFS2 inode
1456 * This routine searches a directory for a file or another directory.
1457 * Assumes a glock is held on dip.
1462 int gfs2_dir_search(struct inode
*dir
, const struct qstr
*name
,
1463 struct gfs2_inum
*inum
, unsigned int *type
)
1465 struct buffer_head
*bh
;
1466 struct gfs2_dirent
*dent
;
1468 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1471 return PTR_ERR(dent
);
1473 gfs2_inum_in(inum
, (char *)&dent
->de_inum
);
1475 *type
= be16_to_cpu(dent
->de_type
);
1482 static int dir_new_leaf(struct inode
*inode
, const struct qstr
*name
)
1484 struct buffer_head
*bh
, *obh
;
1485 struct gfs2_inode
*ip
= GFS2_I(inode
);
1486 struct gfs2_leaf
*leaf
, *oleaf
;
1491 index
= name
->hash
>> (32 - ip
->i_di
.di_depth
);
1492 error
= get_first_leaf(ip
, index
, &obh
);
1496 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
1497 bn
= be64_to_cpu(oleaf
->lf_next
);
1501 error
= get_leaf(ip
, bn
, &obh
);
1506 gfs2_trans_add_bh(ip
->i_gl
, obh
, 1);
1508 leaf
= new_leaf(inode
, &bh
, be16_to_cpu(oleaf
->lf_depth
));
1513 oleaf
->lf_next
= cpu_to_be64(bh
->b_blocknr
);
1517 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1520 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1521 ip
->i_di
.di_blocks
++;
1522 gfs2_dinode_out(&ip
->i_di
, bh
->b_data
);
1528 * gfs2_dir_add - Add new filename into directory
1529 * @dip: The GFS2 inode
1530 * @filename: The new name
1531 * @inode: The inode number of the entry
1532 * @type: The type of the entry
1534 * Returns: 0 on success, error code on failure
1537 int gfs2_dir_add(struct inode
*inode
, const struct qstr
*name
,
1538 const struct gfs2_inum
*inum
, unsigned type
)
1540 struct gfs2_inode
*ip
= GFS2_I(inode
);
1541 struct buffer_head
*bh
;
1542 struct gfs2_dirent
*dent
;
1543 struct gfs2_leaf
*leaf
;
1547 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
,
1551 return PTR_ERR(dent
);
1552 dent
= gfs2_init_dirent(inode
, dent
, name
, bh
);
1553 gfs2_inum_out(inum
, (char *)&dent
->de_inum
);
1554 dent
->de_type
= cpu_to_be16(type
);
1555 if (ip
->i_di
.di_flags
& GFS2_DIF_EXHASH
) {
1556 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1557 leaf
->lf_entries
= cpu_to_be16(be16_to_cpu(leaf
->lf_entries
) + 1);
1560 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1563 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1564 ip
->i_di
.di_entries
++;
1565 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
1566 gfs2_dinode_out(&ip
->i_di
, bh
->b_data
);
1571 if (!(ip
->i_di
.di_flags
& GFS2_DIF_EXHASH
)) {
1572 error
= dir_make_exhash(inode
);
1577 error
= dir_split_leaf(inode
, name
);
1582 if (ip
->i_di
.di_depth
< GFS2_DIR_MAX_DEPTH
) {
1583 error
= dir_double_exhash(ip
);
1586 error
= dir_split_leaf(inode
, name
);
1592 error
= dir_new_leaf(inode
, name
);
1603 * gfs2_dir_del - Delete a directory entry
1604 * @dip: The GFS2 inode
1605 * @filename: The filename
1607 * Returns: 0 on success, error code on failure
1610 int gfs2_dir_del(struct gfs2_inode
*dip
, const struct qstr
*name
)
1612 struct gfs2_dirent
*dent
, *prev
= NULL
;
1613 struct buffer_head
*bh
;
1616 /* Returns _either_ the entry (if its first in block) or the
1617 previous entry otherwise */
1618 dent
= gfs2_dirent_search(&dip
->i_inode
, name
, gfs2_dirent_prev
, &bh
);
1620 gfs2_consist_inode(dip
);
1624 gfs2_consist_inode(dip
);
1625 return PTR_ERR(dent
);
1627 /* If not first in block, adjust pointers accordingly */
1628 if (gfs2_dirent_find(dent
, name
, NULL
) == 0) {
1630 dent
= (struct gfs2_dirent
*)((char *)dent
+ be16_to_cpu(prev
->de_rec_len
));
1633 dirent_del(dip
, bh
, prev
, dent
);
1634 if (dip
->i_di
.di_flags
& GFS2_DIF_EXHASH
) {
1635 struct gfs2_leaf
*leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1636 u16 entries
= be16_to_cpu(leaf
->lf_entries
);
1638 gfs2_consist_inode(dip
);
1639 leaf
->lf_entries
= cpu_to_be16(--entries
);
1643 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1647 if (!dip
->i_di
.di_entries
)
1648 gfs2_consist_inode(dip
);
1649 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1650 dip
->i_di
.di_entries
--;
1651 dip
->i_di
.di_mtime
= dip
->i_di
.di_ctime
= get_seconds();
1652 gfs2_dinode_out(&dip
->i_di
, bh
->b_data
);
1654 mark_inode_dirty(&dip
->i_inode
);
1660 * gfs2_dir_mvino - Change inode number of directory entry
1661 * @dip: The GFS2 inode
1665 * This routine changes the inode number of a directory entry. It's used
1666 * by rename to change ".." when a directory is moved.
1667 * Assumes a glock is held on dvp.
1672 int gfs2_dir_mvino(struct gfs2_inode
*dip
, const struct qstr
*filename
,
1673 struct gfs2_inum
*inum
, unsigned int new_type
)
1675 struct buffer_head
*bh
;
1676 struct gfs2_dirent
*dent
;
1679 dent
= gfs2_dirent_search(&dip
->i_inode
, filename
, gfs2_dirent_find
, &bh
);
1681 gfs2_consist_inode(dip
);
1685 return PTR_ERR(dent
);
1687 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1688 gfs2_inum_out(inum
, (char *)&dent
->de_inum
);
1689 dent
->de_type
= cpu_to_be16(new_type
);
1691 if (dip
->i_di
.di_flags
& GFS2_DIF_EXHASH
) {
1693 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1696 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1699 dip
->i_di
.di_mtime
= dip
->i_di
.di_ctime
= get_seconds();
1700 gfs2_dinode_out(&dip
->i_di
, bh
->b_data
);
1706 * foreach_leaf - call a function for each leaf in a directory
1707 * @dip: the directory
1708 * @lc: the function to call for each each
1709 * @data: private data to pass to it
1714 static int foreach_leaf(struct gfs2_inode
*dip
, leaf_call_t lc
, void *data
)
1716 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1717 struct buffer_head
*bh
;
1718 struct gfs2_leaf
*leaf
;
1720 u32 ht_offset
, lp_offset
, ht_offset_cur
= -1;
1726 hsize
= 1 << dip
->i_di
.di_depth
;
1727 if (hsize
* sizeof(u64
) != dip
->i_di
.di_size
) {
1728 gfs2_consist_inode(dip
);
1732 lp
= kmalloc(sdp
->sd_hash_bsize
, GFP_KERNEL
);
1736 while (index
< hsize
) {
1737 lp_offset
= index
& (sdp
->sd_hash_ptrs
- 1);
1738 ht_offset
= index
- lp_offset
;
1740 if (ht_offset_cur
!= ht_offset
) {
1741 error
= gfs2_dir_read_data(dip
, (char *)lp
,
1742 ht_offset
* sizeof(u64
),
1743 sdp
->sd_hash_bsize
, 1);
1744 if (error
!= sdp
->sd_hash_bsize
) {
1749 ht_offset_cur
= ht_offset
;
1752 leaf_no
= be64_to_cpu(lp
[lp_offset
]);
1754 error
= get_leaf(dip
, leaf_no
, &bh
);
1757 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1758 len
= 1 << (dip
->i_di
.di_depth
- be16_to_cpu(leaf
->lf_depth
));
1761 error
= lc(dip
, index
, len
, leaf_no
, data
);
1765 index
= (index
& ~(len
- 1)) + len
;
1770 if (index
!= hsize
) {
1771 gfs2_consist_inode(dip
);
1782 * leaf_dealloc - Deallocate a directory leaf
1783 * @dip: the directory
1784 * @index: the hash table offset in the directory
1785 * @len: the number of pointers to this leaf
1786 * @leaf_no: the leaf number
1792 static int leaf_dealloc(struct gfs2_inode
*dip
, u32 index
, u32 len
,
1793 u64 leaf_no
, void *data
)
1795 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1796 struct gfs2_leaf
*tmp_leaf
;
1797 struct gfs2_rgrp_list rlist
;
1798 struct buffer_head
*bh
, *dibh
;
1800 unsigned int rg_blocks
= 0, l_blocks
= 0;
1802 unsigned int x
, size
= len
* sizeof(u64
);
1805 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1807 ht
= kzalloc(size
, GFP_KERNEL
);
1811 gfs2_alloc_get(dip
);
1813 error
= gfs2_quota_hold(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1817 error
= gfs2_rindex_hold(sdp
, &dip
->i_alloc
.al_ri_gh
);
1821 /* Count the number of leaves */
1823 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1824 error
= get_leaf(dip
, blk
, &bh
);
1827 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1828 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1831 gfs2_rlist_add(sdp
, &rlist
, blk
);
1835 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
1837 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1838 struct gfs2_rgrpd
*rgd
;
1839 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1840 rg_blocks
+= rgd
->rd_ri
.ri_length
;
1843 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1847 error
= gfs2_trans_begin(sdp
,
1848 rg_blocks
+ (DIV_ROUND_UP(size
, sdp
->sd_jbsize
) + 1) +
1849 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, l_blocks
);
1851 goto out_rg_gunlock
;
1853 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1854 error
= get_leaf(dip
, blk
, &bh
);
1857 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1858 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1861 gfs2_free_meta(dip
, blk
, 1);
1863 if (!dip
->i_di
.di_blocks
)
1864 gfs2_consist_inode(dip
);
1865 dip
->i_di
.di_blocks
--;
1868 error
= gfs2_dir_write_data(dip
, ht
, index
* sizeof(u64
), size
);
1869 if (error
!= size
) {
1875 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1879 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1880 gfs2_dinode_out(&dip
->i_di
, dibh
->b_data
);
1884 gfs2_trans_end(sdp
);
1886 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1888 gfs2_rlist_free(&rlist
);
1889 gfs2_glock_dq_uninit(&dip
->i_alloc
.al_ri_gh
);
1891 gfs2_quota_unhold(dip
);
1893 gfs2_alloc_put(dip
);
1899 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1900 * @dip: the directory
1902 * Dealloc all on-disk directory leaves to FREEMETA state
1903 * Change on-disk inode type to "regular file"
1908 int gfs2_dir_exhash_dealloc(struct gfs2_inode
*dip
)
1910 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1911 struct buffer_head
*bh
;
1914 /* Dealloc on-disk leaves to FREEMETA state */
1915 error
= foreach_leaf(dip
, leaf_dealloc
, NULL
);
1919 /* Make this a regular file in case we crash.
1920 (We don't want to free these blocks a second time.) */
1922 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
1926 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1928 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1929 ((struct gfs2_dinode
*)bh
->b_data
)->di_mode
=
1930 cpu_to_be32(S_IFREG
);
1934 gfs2_trans_end(sdp
);
1940 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1941 * @ip: the file being written to
1942 * @filname: the filename that's going to be added
1944 * Returns: 1 if alloc required, 0 if not, -ve on error
1947 int gfs2_diradd_alloc_required(struct inode
*inode
, const struct qstr
*name
)
1949 struct gfs2_dirent
*dent
;
1950 struct buffer_head
*bh
;
1952 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
, &bh
);
1957 return PTR_ERR(dent
);