2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
39 * dip->i_diskflags & GFS2_DIF_EXHASH is true
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
43 * When the dirents are in leaves, the actual contents of the directory file are
44 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
45 * dirents are NOT in the directory file itself. There can be more than one
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the same leaf.
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
56 #include <linux/slab.h>
57 #include <linux/spinlock.h>
58 #include <linux/buffer_head.h>
59 #include <linux/sort.h>
60 #include <linux/gfs2_ondisk.h>
61 #include <linux/crc32.h>
62 #include <linux/vmalloc.h>
76 #define IS_LEAF 1 /* Hashed (leaf) directory */
77 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
79 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
80 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
82 struct qstr gfs2_qdot __read_mostly
;
83 struct qstr gfs2_qdotdot __read_mostly
;
85 typedef int (*gfs2_dscan_t
)(const struct gfs2_dirent
*dent
,
86 const struct qstr
*name
, void *opaque
);
88 int gfs2_dir_get_new_buffer(struct gfs2_inode
*ip
, u64 block
,
89 struct buffer_head
**bhp
)
91 struct buffer_head
*bh
;
93 bh
= gfs2_meta_new(ip
->i_gl
, block
);
94 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
95 gfs2_metatype_set(bh
, GFS2_METATYPE_JD
, GFS2_FORMAT_JD
);
96 gfs2_buffer_clear_tail(bh
, sizeof(struct gfs2_meta_header
));
101 static int gfs2_dir_get_existing_buffer(struct gfs2_inode
*ip
, u64 block
,
102 struct buffer_head
**bhp
)
104 struct buffer_head
*bh
;
107 error
= gfs2_meta_read(ip
->i_gl
, block
, DIO_WAIT
, &bh
);
110 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_JD
)) {
118 static int gfs2_dir_write_stuffed(struct gfs2_inode
*ip
, const char *buf
,
119 unsigned int offset
, unsigned int size
)
121 struct buffer_head
*dibh
;
124 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
128 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
129 memcpy(dibh
->b_data
+ offset
+ sizeof(struct gfs2_dinode
), buf
, size
);
130 if (ip
->i_inode
.i_size
< offset
+ size
)
131 i_size_write(&ip
->i_inode
, offset
+ size
);
132 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
133 gfs2_dinode_out(ip
, dibh
->b_data
);
143 * gfs2_dir_write_data - Write directory information to the inode
144 * @ip: The GFS2 inode
145 * @buf: The buffer containing information to be written
146 * @offset: The file offset to start writing at
147 * @size: The amount of data to write
149 * Returns: The number of bytes correctly written or error code
151 static int gfs2_dir_write_data(struct gfs2_inode
*ip
, const char *buf
,
152 u64 offset
, unsigned int size
)
154 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
155 struct buffer_head
*dibh
;
166 if (gfs2_is_stuffed(ip
) &&
167 offset
+ size
<= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
168 return gfs2_dir_write_stuffed(ip
, buf
, (unsigned int)offset
,
171 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
174 if (gfs2_is_stuffed(ip
)) {
175 error
= gfs2_unstuff_dinode(ip
, NULL
);
181 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
183 while (copied
< size
) {
185 struct buffer_head
*bh
;
187 amount
= size
- copied
;
188 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
189 amount
= sdp
->sd_sb
.sb_bsize
- o
;
193 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
198 if (gfs2_assert_withdraw(sdp
, dblock
))
202 if (amount
== sdp
->sd_jbsize
|| new)
203 error
= gfs2_dir_get_new_buffer(ip
, dblock
, &bh
);
205 error
= gfs2_dir_get_existing_buffer(ip
, dblock
, &bh
);
210 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
211 memcpy(bh
->b_data
+ o
, buf
, amount
);
220 o
= sizeof(struct gfs2_meta_header
);
224 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
228 if (ip
->i_inode
.i_size
< offset
+ copied
)
229 i_size_write(&ip
->i_inode
, offset
+ copied
);
230 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
232 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
233 gfs2_dinode_out(ip
, dibh
->b_data
);
243 static int gfs2_dir_read_stuffed(struct gfs2_inode
*ip
, char *buf
,
244 u64 offset
, unsigned int size
)
246 struct buffer_head
*dibh
;
249 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
251 offset
+= sizeof(struct gfs2_dinode
);
252 memcpy(buf
, dibh
->b_data
+ offset
, size
);
256 return (error
) ? error
: size
;
261 * gfs2_dir_read_data - Read a data from a directory inode
262 * @ip: The GFS2 Inode
263 * @buf: The buffer to place result into
264 * @offset: File offset to begin jdata_readng from
265 * @size: Amount of data to transfer
267 * Returns: The amount of data actually copied or the error
269 static int gfs2_dir_read_data(struct gfs2_inode
*ip
, char *buf
, u64 offset
,
270 unsigned int size
, unsigned ra
)
272 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
278 u64 disksize
= i_size_read(&ip
->i_inode
);
280 if (offset
>= disksize
)
283 if (offset
+ size
> disksize
)
284 size
= disksize
- offset
;
289 if (gfs2_is_stuffed(ip
))
290 return gfs2_dir_read_stuffed(ip
, buf
, offset
, size
);
292 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
296 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
298 while (copied
< size
) {
300 struct buffer_head
*bh
;
303 amount
= size
- copied
;
304 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
305 amount
= sdp
->sd_sb
.sb_bsize
- o
;
309 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
311 if (error
|| !dblock
)
316 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
318 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_WAIT
, &bh
);
322 error
= gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_JD
);
329 memcpy(buf
, bh
->b_data
+ o
, amount
);
334 o
= sizeof(struct gfs2_meta_header
);
339 return (copied
) ? copied
: error
;
343 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
344 * @ip: The inode in question
346 * Returns: The hash table or an error
349 static __be64
*gfs2_dir_get_hash_table(struct gfs2_inode
*ip
)
351 struct inode
*inode
= &ip
->i_inode
;
356 BUG_ON(!(ip
->i_diskflags
& GFS2_DIF_EXHASH
));
358 hc
= ip
->i_hash_cache
;
362 hsize
= 1 << ip
->i_depth
;
363 hsize
*= sizeof(__be64
);
364 if (hsize
!= i_size_read(&ip
->i_inode
)) {
365 gfs2_consist_inode(ip
);
366 return ERR_PTR(-EIO
);
369 hc
= kmalloc(hsize
, GFP_NOFS
);
372 return ERR_PTR(-ENOMEM
);
374 ret
= gfs2_dir_read_data(ip
, (char *)hc
, 0, hsize
, 1);
380 spin_lock(&inode
->i_lock
);
381 if (ip
->i_hash_cache
)
384 ip
->i_hash_cache
= hc
;
385 spin_unlock(&inode
->i_lock
);
387 return ip
->i_hash_cache
;
391 * gfs2_dir_hash_inval - Invalidate dir hash
392 * @ip: The directory inode
394 * Must be called with an exclusive glock, or during glock invalidation.
396 void gfs2_dir_hash_inval(struct gfs2_inode
*ip
)
398 __be64
*hc
= ip
->i_hash_cache
;
399 ip
->i_hash_cache
= NULL
;
403 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent
*dent
)
405 return dent
->de_inum
.no_addr
== 0 || dent
->de_inum
.no_formal_ino
== 0;
408 static inline int __gfs2_dirent_find(const struct gfs2_dirent
*dent
,
409 const struct qstr
*name
, int ret
)
411 if (!gfs2_dirent_sentinel(dent
) &&
412 be32_to_cpu(dent
->de_hash
) == name
->hash
&&
413 be16_to_cpu(dent
->de_name_len
) == name
->len
&&
414 memcmp(dent
+1, name
->name
, name
->len
) == 0)
419 static int gfs2_dirent_find(const struct gfs2_dirent
*dent
,
420 const struct qstr
*name
,
423 return __gfs2_dirent_find(dent
, name
, 1);
426 static int gfs2_dirent_prev(const struct gfs2_dirent
*dent
,
427 const struct qstr
*name
,
430 return __gfs2_dirent_find(dent
, name
, 2);
434 * name->name holds ptr to start of block.
435 * name->len holds size of block.
437 static int gfs2_dirent_last(const struct gfs2_dirent
*dent
,
438 const struct qstr
*name
,
441 const char *start
= name
->name
;
442 const char *end
= (const char *)dent
+ be16_to_cpu(dent
->de_rec_len
);
443 if (name
->len
== (end
- start
))
448 static int gfs2_dirent_find_space(const struct gfs2_dirent
*dent
,
449 const struct qstr
*name
,
452 unsigned required
= GFS2_DIRENT_SIZE(name
->len
);
453 unsigned actual
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
454 unsigned totlen
= be16_to_cpu(dent
->de_rec_len
);
456 if (gfs2_dirent_sentinel(dent
))
458 if (totlen
- actual
>= required
)
463 struct dirent_gather
{
464 const struct gfs2_dirent
**pdent
;
468 static int gfs2_dirent_gather(const struct gfs2_dirent
*dent
,
469 const struct qstr
*name
,
472 struct dirent_gather
*g
= opaque
;
473 if (!gfs2_dirent_sentinel(dent
)) {
474 g
->pdent
[g
->offset
++] = dent
;
480 * Other possible things to check:
481 * - Inode located within filesystem size (and on valid block)
482 * - Valid directory entry type
483 * Not sure how heavy-weight we want to make this... could also check
484 * hash is correct for example, but that would take a lot of extra time.
485 * For now the most important thing is to check that the various sizes
488 static int gfs2_check_dirent(struct gfs2_dirent
*dent
, unsigned int offset
,
489 unsigned int size
, unsigned int len
, int first
)
491 const char *msg
= "gfs2_dirent too small";
492 if (unlikely(size
< sizeof(struct gfs2_dirent
)))
494 msg
= "gfs2_dirent misaligned";
495 if (unlikely(offset
& 0x7))
497 msg
= "gfs2_dirent points beyond end of block";
498 if (unlikely(offset
+ size
> len
))
500 msg
= "zero inode number";
501 if (unlikely(!first
&& gfs2_dirent_sentinel(dent
)))
503 msg
= "name length is greater than space in dirent";
504 if (!gfs2_dirent_sentinel(dent
) &&
505 unlikely(sizeof(struct gfs2_dirent
)+be16_to_cpu(dent
->de_name_len
) >
510 printk(KERN_WARNING
"gfs2_check_dirent: %s (%s)\n", msg
,
511 first
? "first in block" : "not first in block");
515 static int gfs2_dirent_offset(const void *buf
)
517 const struct gfs2_meta_header
*h
= buf
;
522 switch(be32_to_cpu(h
->mh_type
)) {
523 case GFS2_METATYPE_LF
:
524 offset
= sizeof(struct gfs2_leaf
);
526 case GFS2_METATYPE_DI
:
527 offset
= sizeof(struct gfs2_dinode
);
534 printk(KERN_WARNING
"gfs2_scan_dirent: wrong block type %u\n",
535 be32_to_cpu(h
->mh_type
));
539 static struct gfs2_dirent
*gfs2_dirent_scan(struct inode
*inode
, void *buf
,
540 unsigned int len
, gfs2_dscan_t scan
,
541 const struct qstr
*name
,
544 struct gfs2_dirent
*dent
, *prev
;
549 ret
= gfs2_dirent_offset(buf
);
556 size
= be16_to_cpu(dent
->de_rec_len
);
557 if (gfs2_check_dirent(dent
, offset
, size
, len
, 1))
560 ret
= scan(dent
, name
, opaque
);
568 size
= be16_to_cpu(dent
->de_rec_len
);
569 if (gfs2_check_dirent(dent
, offset
, size
, len
, 0))
579 return prev
? prev
: dent
;
586 gfs2_consist_inode(GFS2_I(inode
));
587 return ERR_PTR(-EIO
);
590 static int dirent_check_reclen(struct gfs2_inode
*dip
,
591 const struct gfs2_dirent
*d
, const void *end_p
)
594 u16 rec_len
= be16_to_cpu(d
->de_rec_len
);
596 if (unlikely(rec_len
< sizeof(struct gfs2_dirent
)))
604 gfs2_consist_inode(dip
);
609 * dirent_next - Next dirent
610 * @dip: the directory
612 * @dent: Pointer to list of dirents
614 * Returns: 0 on success, error code otherwise
617 static int dirent_next(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
618 struct gfs2_dirent
**dent
)
620 struct gfs2_dirent
*cur
= *dent
, *tmp
;
621 char *bh_end
= bh
->b_data
+ bh
->b_size
;
624 ret
= dirent_check_reclen(dip
, cur
, bh_end
);
628 tmp
= (void *)cur
+ ret
;
629 ret
= dirent_check_reclen(dip
, tmp
, bh_end
);
633 /* Only the first dent could ever have de_inum.no_addr == 0 */
634 if (gfs2_dirent_sentinel(tmp
)) {
635 gfs2_consist_inode(dip
);
644 * dirent_del - Delete a dirent
645 * @dip: The GFS2 inode
647 * @prev: The previous dirent
648 * @cur: The current dirent
652 static void dirent_del(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
653 struct gfs2_dirent
*prev
, struct gfs2_dirent
*cur
)
655 u16 cur_rec_len
, prev_rec_len
;
657 if (gfs2_dirent_sentinel(cur
)) {
658 gfs2_consist_inode(dip
);
662 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
664 /* If there is no prev entry, this is the first entry in the block.
665 The de_rec_len is already as big as it needs to be. Just zero
666 out the inode number and return. */
669 cur
->de_inum
.no_addr
= 0;
670 cur
->de_inum
.no_formal_ino
= 0;
674 /* Combine this dentry with the previous one. */
676 prev_rec_len
= be16_to_cpu(prev
->de_rec_len
);
677 cur_rec_len
= be16_to_cpu(cur
->de_rec_len
);
679 if ((char *)prev
+ prev_rec_len
!= (char *)cur
)
680 gfs2_consist_inode(dip
);
681 if ((char *)cur
+ cur_rec_len
> bh
->b_data
+ bh
->b_size
)
682 gfs2_consist_inode(dip
);
684 prev_rec_len
+= cur_rec_len
;
685 prev
->de_rec_len
= cpu_to_be16(prev_rec_len
);
689 * Takes a dent from which to grab space as an argument. Returns the
690 * newly created dent.
692 static struct gfs2_dirent
*gfs2_init_dirent(struct inode
*inode
,
693 struct gfs2_dirent
*dent
,
694 const struct qstr
*name
,
695 struct buffer_head
*bh
)
697 struct gfs2_inode
*ip
= GFS2_I(inode
);
698 struct gfs2_dirent
*ndent
;
699 unsigned offset
= 0, totlen
;
701 if (!gfs2_dirent_sentinel(dent
))
702 offset
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
703 totlen
= be16_to_cpu(dent
->de_rec_len
);
704 BUG_ON(offset
+ name
->len
> totlen
);
705 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
706 ndent
= (struct gfs2_dirent
*)((char *)dent
+ offset
);
707 dent
->de_rec_len
= cpu_to_be16(offset
);
708 gfs2_qstr2dirent(name
, totlen
- offset
, ndent
);
712 static struct gfs2_dirent
*gfs2_dirent_alloc(struct inode
*inode
,
713 struct buffer_head
*bh
,
714 const struct qstr
*name
)
716 struct gfs2_dirent
*dent
;
717 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
718 gfs2_dirent_find_space
, name
, NULL
);
719 if (!dent
|| IS_ERR(dent
))
721 return gfs2_init_dirent(inode
, dent
, name
, bh
);
724 static int get_leaf(struct gfs2_inode
*dip
, u64 leaf_no
,
725 struct buffer_head
**bhp
)
729 error
= gfs2_meta_read(dip
->i_gl
, leaf_no
, DIO_WAIT
, bhp
);
730 if (!error
&& gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), *bhp
, GFS2_METATYPE_LF
)) {
731 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
739 * get_leaf_nr - Get a leaf number associated with the index
740 * @dip: The GFS2 inode
744 * Returns: 0 on success, error code otherwise
747 static int get_leaf_nr(struct gfs2_inode
*dip
, u32 index
,
752 hash
= gfs2_dir_get_hash_table(dip
);
754 return PTR_ERR(hash
);
755 *leaf_out
= be64_to_cpu(*(hash
+ index
));
759 static int get_first_leaf(struct gfs2_inode
*dip
, u32 index
,
760 struct buffer_head
**bh_out
)
765 error
= get_leaf_nr(dip
, index
, &leaf_no
);
767 error
= get_leaf(dip
, leaf_no
, bh_out
);
772 static struct gfs2_dirent
*gfs2_dirent_search(struct inode
*inode
,
773 const struct qstr
*name
,
775 struct buffer_head
**pbh
)
777 struct buffer_head
*bh
;
778 struct gfs2_dirent
*dent
;
779 struct gfs2_inode
*ip
= GFS2_I(inode
);
782 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
783 struct gfs2_leaf
*leaf
;
784 unsigned hsize
= 1 << ip
->i_depth
;
787 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
788 gfs2_consist_inode(ip
);
789 return ERR_PTR(-EIO
);
792 index
= name
->hash
>> (32 - ip
->i_depth
);
793 error
= get_first_leaf(ip
, index
, &bh
);
795 return ERR_PTR(error
);
797 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
801 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
802 ln
= be64_to_cpu(leaf
->lf_next
);
807 error
= get_leaf(ip
, ln
, &bh
);
810 return error
? ERR_PTR(error
) : NULL
;
814 error
= gfs2_meta_inode_buffer(ip
, &bh
);
816 return ERR_PTR(error
);
817 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
, scan
, name
, NULL
);
819 if (unlikely(dent
== NULL
|| IS_ERR(dent
))) {
827 static struct gfs2_leaf
*new_leaf(struct inode
*inode
, struct buffer_head
**pbh
, u16 depth
)
829 struct gfs2_inode
*ip
= GFS2_I(inode
);
833 struct buffer_head
*bh
;
834 struct gfs2_leaf
*leaf
;
835 struct gfs2_dirent
*dent
;
836 struct qstr name
= { .name
= "", .len
= 0, .hash
= 0 };
838 error
= gfs2_alloc_block(ip
, &bn
, &n
);
841 bh
= gfs2_meta_new(ip
->i_gl
, bn
);
845 gfs2_trans_add_unrevoke(GFS2_SB(inode
), bn
, 1);
846 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
847 gfs2_metatype_set(bh
, GFS2_METATYPE_LF
, GFS2_FORMAT_LF
);
848 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
849 leaf
->lf_depth
= cpu_to_be16(depth
);
850 leaf
->lf_entries
= 0;
851 leaf
->lf_dirent_format
= cpu_to_be32(GFS2_FORMAT_DE
);
853 memset(leaf
->lf_reserved
, 0, sizeof(leaf
->lf_reserved
));
854 dent
= (struct gfs2_dirent
*)(leaf
+1);
855 gfs2_qstr2dirent(&name
, bh
->b_size
- sizeof(struct gfs2_leaf
), dent
);
861 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
862 * @dip: The GFS2 inode
864 * Returns: 0 on success, error code otherwise
867 static int dir_make_exhash(struct inode
*inode
)
869 struct gfs2_inode
*dip
= GFS2_I(inode
);
870 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
871 struct gfs2_dirent
*dent
;
873 struct buffer_head
*bh
, *dibh
;
874 struct gfs2_leaf
*leaf
;
881 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
885 /* Turn over a new leaf */
887 leaf
= new_leaf(inode
, &bh
, 0);
892 gfs2_assert(sdp
, dip
->i_entries
< (1 << 16));
893 leaf
->lf_entries
= cpu_to_be16(dip
->i_entries
);
897 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_leaf
), dibh
,
898 sizeof(struct gfs2_dinode
));
900 /* Find last entry */
903 args
.len
= bh
->b_size
- sizeof(struct gfs2_dinode
) +
904 sizeof(struct gfs2_leaf
);
905 args
.name
= bh
->b_data
;
906 dent
= gfs2_dirent_scan(&dip
->i_inode
, bh
->b_data
, bh
->b_size
,
907 gfs2_dirent_last
, &args
, NULL
);
916 return PTR_ERR(dent
);
919 /* Adjust the last dirent's record length
920 (Remember that dent still points to the last entry.) */
922 dent
->de_rec_len
= cpu_to_be16(be16_to_cpu(dent
->de_rec_len
) +
923 sizeof(struct gfs2_dinode
) -
924 sizeof(struct gfs2_leaf
));
928 /* We're done with the new leaf block, now setup the new
931 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
932 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
934 lp
= (__be64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
936 for (x
= sdp
->sd_hash_ptrs
; x
--; lp
++)
937 *lp
= cpu_to_be64(bn
);
939 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
/ 2);
940 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
941 dip
->i_diskflags
|= GFS2_DIF_EXHASH
;
943 for (x
= sdp
->sd_hash_ptrs
, y
= -1; x
; x
>>= 1, y
++) ;
946 gfs2_dinode_out(dip
, dibh
->b_data
);
954 * dir_split_leaf - Split a leaf block into two
955 * @dip: The GFS2 inode
959 * Returns: 0 on success, error code on failure
962 static int dir_split_leaf(struct inode
*inode
, const struct qstr
*name
)
964 struct gfs2_inode
*dip
= GFS2_I(inode
);
965 struct buffer_head
*nbh
, *obh
, *dibh
;
966 struct gfs2_leaf
*nleaf
, *oleaf
;
967 struct gfs2_dirent
*dent
= NULL
, *prev
= NULL
, *next
= NULL
, *new;
968 u32 start
, len
, half_len
, divider
;
975 index
= name
->hash
>> (32 - dip
->i_depth
);
976 error
= get_leaf_nr(dip
, index
, &leaf_no
);
980 /* Get the old leaf block */
981 error
= get_leaf(dip
, leaf_no
, &obh
);
985 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
986 if (dip
->i_depth
== be16_to_cpu(oleaf
->lf_depth
)) {
988 return 1; /* can't split */
991 gfs2_trans_add_bh(dip
->i_gl
, obh
, 1);
993 nleaf
= new_leaf(inode
, &nbh
, be16_to_cpu(oleaf
->lf_depth
) + 1);
1000 /* Compute the start and len of leaf pointers in the hash table. */
1001 len
= 1 << (dip
->i_depth
- be16_to_cpu(oleaf
->lf_depth
));
1002 half_len
= len
>> 1;
1004 printk(KERN_WARNING
"i_depth %u lf_depth %u index %u\n", dip
->i_depth
, be16_to_cpu(oleaf
->lf_depth
), index
);
1005 gfs2_consist_inode(dip
);
1010 start
= (index
& ~(len
- 1));
1012 /* Change the pointers.
1013 Don't bother distinguishing stuffed from non-stuffed.
1014 This code is complicated enough already. */
1015 lp
= kmalloc(half_len
* sizeof(__be64
), GFP_NOFS
);
1021 /* Change the pointers */
1022 for (x
= 0; x
< half_len
; x
++)
1023 lp
[x
] = cpu_to_be64(bn
);
1025 gfs2_dir_hash_inval(dip
);
1027 error
= gfs2_dir_write_data(dip
, (char *)lp
, start
* sizeof(u64
),
1028 half_len
* sizeof(u64
));
1029 if (error
!= half_len
* sizeof(u64
)) {
1037 /* Compute the divider */
1038 divider
= (start
+ half_len
) << (32 - dip
->i_depth
);
1040 /* Copy the entries */
1041 dent
= (struct gfs2_dirent
*)(obh
->b_data
+ sizeof(struct gfs2_leaf
));
1045 if (dirent_next(dip
, obh
, &next
))
1048 if (!gfs2_dirent_sentinel(dent
) &&
1049 be32_to_cpu(dent
->de_hash
) < divider
) {
1051 str
.name
= (char*)(dent
+1);
1052 str
.len
= be16_to_cpu(dent
->de_name_len
);
1053 str
.hash
= be32_to_cpu(dent
->de_hash
);
1054 new = gfs2_dirent_alloc(inode
, nbh
, &str
);
1056 error
= PTR_ERR(new);
1060 new->de_inum
= dent
->de_inum
; /* No endian worries */
1061 new->de_type
= dent
->de_type
; /* No endian worries */
1062 be16_add_cpu(&nleaf
->lf_entries
, 1);
1064 dirent_del(dip
, obh
, prev
, dent
);
1066 if (!oleaf
->lf_entries
)
1067 gfs2_consist_inode(dip
);
1068 be16_add_cpu(&oleaf
->lf_entries
, -1);
1080 oleaf
->lf_depth
= nleaf
->lf_depth
;
1082 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1083 if (!gfs2_assert_withdraw(GFS2_SB(&dip
->i_inode
), !error
)) {
1084 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1085 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
1086 gfs2_dinode_out(dip
, dibh
->b_data
);
1105 * dir_double_exhash - Double size of ExHash table
1106 * @dip: The GFS2 dinode
1108 * Returns: 0 on success, error code on failure
1111 static int dir_double_exhash(struct gfs2_inode
*dip
)
1113 struct buffer_head
*dibh
;
1121 hsize
= 1 << dip
->i_depth
;
1122 hsize_bytes
= hsize
* sizeof(__be64
);
1124 hc
= gfs2_dir_get_hash_table(dip
);
1128 h
= hc2
= kmalloc(hsize_bytes
* 2, GFP_NOFS
);
1132 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1136 for (x
= 0; x
< hsize
; x
++) {
1142 error
= gfs2_dir_write_data(dip
, (char *)hc2
, 0, hsize_bytes
* 2);
1143 if (error
!= (hsize_bytes
* 2))
1146 gfs2_dir_hash_inval(dip
);
1147 dip
->i_hash_cache
= hc2
;
1149 gfs2_dinode_out(dip
, dibh
->b_data
);
1154 /* Replace original hash table & size */
1155 gfs2_dir_write_data(dip
, (char *)hc
, 0, hsize_bytes
);
1156 i_size_write(&dip
->i_inode
, hsize_bytes
);
1157 gfs2_dinode_out(dip
, dibh
->b_data
);
1165 * compare_dents - compare directory entries by hash value
1169 * When comparing the hash entries of @a to @b:
1175 static int compare_dents(const void *a
, const void *b
)
1177 const struct gfs2_dirent
*dent_a
, *dent_b
;
1181 dent_a
= *(const struct gfs2_dirent
**)a
;
1182 hash_a
= be32_to_cpu(dent_a
->de_hash
);
1184 dent_b
= *(const struct gfs2_dirent
**)b
;
1185 hash_b
= be32_to_cpu(dent_b
->de_hash
);
1187 if (hash_a
> hash_b
)
1189 else if (hash_a
< hash_b
)
1192 unsigned int len_a
= be16_to_cpu(dent_a
->de_name_len
);
1193 unsigned int len_b
= be16_to_cpu(dent_b
->de_name_len
);
1197 else if (len_a
< len_b
)
1200 ret
= memcmp(dent_a
+ 1, dent_b
+ 1, len_a
);
1207 * do_filldir_main - read out directory entries
1208 * @dip: The GFS2 inode
1209 * @offset: The offset in the file to read from
1210 * @opaque: opaque data to pass to filldir
1211 * @filldir: The function to pass entries to
1212 * @darr: an array of struct gfs2_dirent pointers to read
1213 * @entries: the number of entries in darr
1214 * @copied: pointer to int that's non-zero if a entry has been copied out
1216 * Jump through some hoops to make sure that if there are hash collsions,
1217 * they are read out at the beginning of a buffer. We want to minimize
1218 * the possibility that they will fall into different readdir buffers or
1219 * that someone will want to seek to that location.
1221 * Returns: errno, >0 on exception from filldir
1224 static int do_filldir_main(struct gfs2_inode
*dip
, u64
*offset
,
1225 void *opaque
, filldir_t filldir
,
1226 const struct gfs2_dirent
**darr
, u32 entries
,
1229 const struct gfs2_dirent
*dent
, *dent_next
;
1235 sort(darr
, entries
, sizeof(struct gfs2_dirent
*), compare_dents
, NULL
);
1237 dent_next
= darr
[0];
1238 off_next
= be32_to_cpu(dent_next
->de_hash
);
1239 off_next
= gfs2_disk_hash2offset(off_next
);
1241 for (x
= 0, y
= 1; x
< entries
; x
++, y
++) {
1246 dent_next
= darr
[y
];
1247 off_next
= be32_to_cpu(dent_next
->de_hash
);
1248 off_next
= gfs2_disk_hash2offset(off_next
);
1254 if (off_next
== off
) {
1255 if (*copied
&& !run
)
1266 error
= filldir(opaque
, (const char *)(dent
+ 1),
1267 be16_to_cpu(dent
->de_name_len
),
1268 off
, be64_to_cpu(dent
->de_inum
.no_addr
),
1269 be16_to_cpu(dent
->de_type
));
1276 /* Increment the *offset by one, so the next time we come into the
1277 do_filldir fxn, we get the next entry instead of the last one in the
1285 static void *gfs2_alloc_sort_buffer(unsigned size
)
1289 if (size
< KMALLOC_MAX_SIZE
)
1290 ptr
= kmalloc(size
, GFP_NOFS
| __GFP_NOWARN
);
1292 ptr
= __vmalloc(size
, GFP_NOFS
, PAGE_KERNEL
);
1296 static void gfs2_free_sort_buffer(void *ptr
)
1298 if (is_vmalloc_addr(ptr
))
1304 static int gfs2_dir_read_leaf(struct inode
*inode
, u64
*offset
, void *opaque
,
1305 filldir_t filldir
, int *copied
, unsigned *depth
,
1308 struct gfs2_inode
*ip
= GFS2_I(inode
);
1309 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1310 struct buffer_head
*bh
;
1311 struct gfs2_leaf
*lf
;
1312 unsigned entries
= 0, entries2
= 0;
1313 unsigned leaves
= 0;
1314 const struct gfs2_dirent
**darr
, *dent
;
1315 struct dirent_gather g
;
1316 struct buffer_head
**larr
;
1322 error
= get_leaf(ip
, lfn
, &bh
);
1325 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1327 *depth
= be16_to_cpu(lf
->lf_depth
);
1328 entries
+= be16_to_cpu(lf
->lf_entries
);
1330 lfn
= be64_to_cpu(lf
->lf_next
);
1339 * The extra 99 entries are not normally used, but are a buffer
1340 * zone in case the number of entries in the leaf is corrupt.
1341 * 99 is the maximum number of entries that can fit in a single
1344 larr
= gfs2_alloc_sort_buffer((leaves
+ entries
+ 99) * sizeof(void *));
1347 darr
= (const struct gfs2_dirent
**)(larr
+ leaves
);
1353 error
= get_leaf(ip
, lfn
, &bh
);
1356 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1357 lfn
= be64_to_cpu(lf
->lf_next
);
1358 if (lf
->lf_entries
) {
1359 entries2
+= be16_to_cpu(lf
->lf_entries
);
1360 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
1361 gfs2_dirent_gather
, NULL
, &g
);
1362 error
= PTR_ERR(dent
);
1365 if (entries2
!= g
.offset
) {
1366 fs_warn(sdp
, "Number of entries corrupt in dir "
1367 "leaf %llu, entries2 (%u) != "
1369 (unsigned long long)bh
->b_blocknr
,
1370 entries2
, g
.offset
);
1382 BUG_ON(entries2
!= entries
);
1383 error
= do_filldir_main(ip
, offset
, opaque
, filldir
, darr
,
1386 for(i
= 0; i
< leaf
; i
++)
1388 gfs2_free_sort_buffer(larr
);
1395 * dir_e_read - Reads the entries from a directory into a filldir buffer
1396 * @dip: dinode pointer
1397 * @offset: the hash of the last entry read shifted to the right once
1398 * @opaque: buffer for the filldir function to fill
1399 * @filldir: points to the filldir function to use
1404 static int dir_e_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1407 struct gfs2_inode
*dip
= GFS2_I(inode
);
1415 hsize
= 1 << dip
->i_depth
;
1416 hash
= gfs2_dir_offset2hash(*offset
);
1417 index
= hash
>> (32 - dip
->i_depth
);
1419 lp
= gfs2_dir_get_hash_table(dip
);
1423 while (index
< hsize
) {
1424 error
= gfs2_dir_read_leaf(inode
, offset
, opaque
, filldir
,
1426 be64_to_cpu(lp
[index
]));
1430 len
= 1 << (dip
->i_depth
- depth
);
1431 index
= (index
& ~(len
- 1)) + len
;
1439 int gfs2_dir_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1442 struct gfs2_inode
*dip
= GFS2_I(inode
);
1443 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1444 struct dirent_gather g
;
1445 const struct gfs2_dirent
**darr
, *dent
;
1446 struct buffer_head
*dibh
;
1450 if (!dip
->i_entries
)
1453 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
)
1454 return dir_e_read(inode
, offset
, opaque
, filldir
);
1456 if (!gfs2_is_stuffed(dip
)) {
1457 gfs2_consist_inode(dip
);
1461 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1466 /* 96 is max number of dirents which can be stuffed into an inode */
1467 darr
= kmalloc(96 * sizeof(struct gfs2_dirent
*), GFP_NOFS
);
1471 dent
= gfs2_dirent_scan(inode
, dibh
->b_data
, dibh
->b_size
,
1472 gfs2_dirent_gather
, NULL
, &g
);
1474 error
= PTR_ERR(dent
);
1477 if (dip
->i_entries
!= g
.offset
) {
1478 fs_warn(sdp
, "Number of entries corrupt in dir %llu, "
1479 "ip->i_entries (%u) != g.offset (%u)\n",
1480 (unsigned long long)dip
->i_no_addr
,
1486 error
= do_filldir_main(dip
, offset
, opaque
, filldir
, darr
,
1487 dip
->i_entries
, &copied
);
1501 * gfs2_dir_search - Search a directory
1502 * @dip: The GFS2 inode
1506 * This routine searches a directory for a file or another directory.
1507 * Assumes a glock is held on dip.
1512 struct inode
*gfs2_dir_search(struct inode
*dir
, const struct qstr
*name
)
1514 struct buffer_head
*bh
;
1515 struct gfs2_dirent
*dent
;
1516 struct inode
*inode
;
1518 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1521 return ERR_CAST(dent
);
1522 inode
= gfs2_inode_lookup(dir
->i_sb
,
1523 be16_to_cpu(dent
->de_type
),
1524 be64_to_cpu(dent
->de_inum
.no_addr
),
1525 be64_to_cpu(dent
->de_inum
.no_formal_ino
), 0);
1529 return ERR_PTR(-ENOENT
);
1532 int gfs2_dir_check(struct inode
*dir
, const struct qstr
*name
,
1533 const struct gfs2_inode
*ip
)
1535 struct buffer_head
*bh
;
1536 struct gfs2_dirent
*dent
;
1539 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1542 return PTR_ERR(dent
);
1544 if (be64_to_cpu(dent
->de_inum
.no_addr
) != ip
->i_no_addr
)
1546 if (be64_to_cpu(dent
->de_inum
.no_formal_ino
) !=
1547 ip
->i_no_formal_ino
)
1549 if (unlikely(IF2DT(ip
->i_inode
.i_mode
) !=
1550 be16_to_cpu(dent
->de_type
))) {
1551 gfs2_consist_inode(GFS2_I(dir
));
1563 static int dir_new_leaf(struct inode
*inode
, const struct qstr
*name
)
1565 struct buffer_head
*bh
, *obh
;
1566 struct gfs2_inode
*ip
= GFS2_I(inode
);
1567 struct gfs2_leaf
*leaf
, *oleaf
;
1572 index
= name
->hash
>> (32 - ip
->i_depth
);
1573 error
= get_first_leaf(ip
, index
, &obh
);
1577 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
1578 bn
= be64_to_cpu(oleaf
->lf_next
);
1582 error
= get_leaf(ip
, bn
, &obh
);
1587 gfs2_trans_add_bh(ip
->i_gl
, obh
, 1);
1589 leaf
= new_leaf(inode
, &bh
, be16_to_cpu(oleaf
->lf_depth
));
1594 oleaf
->lf_next
= cpu_to_be64(bh
->b_blocknr
);
1598 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1601 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1602 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1603 gfs2_dinode_out(ip
, bh
->b_data
);
1609 * gfs2_dir_add - Add new filename into directory
1610 * @dip: The GFS2 inode
1611 * @filename: The new name
1612 * @inode: The inode number of the entry
1613 * @type: The type of the entry
1615 * Returns: 0 on success, error code on failure
1618 int gfs2_dir_add(struct inode
*inode
, const struct qstr
*name
,
1619 const struct gfs2_inode
*nip
)
1621 struct gfs2_inode
*ip
= GFS2_I(inode
);
1622 struct buffer_head
*bh
;
1623 struct gfs2_dirent
*dent
;
1624 struct gfs2_leaf
*leaf
;
1628 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
,
1632 return PTR_ERR(dent
);
1633 dent
= gfs2_init_dirent(inode
, dent
, name
, bh
);
1634 gfs2_inum_out(nip
, dent
);
1635 dent
->de_type
= cpu_to_be16(IF2DT(nip
->i_inode
.i_mode
));
1636 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1637 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1638 be16_add_cpu(&leaf
->lf_entries
, 1);
1641 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1644 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1646 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1647 if (S_ISDIR(nip
->i_inode
.i_mode
))
1648 inc_nlink(&ip
->i_inode
);
1649 gfs2_dinode_out(ip
, bh
->b_data
);
1654 if (!(ip
->i_diskflags
& GFS2_DIF_EXHASH
)) {
1655 error
= dir_make_exhash(inode
);
1660 error
= dir_split_leaf(inode
, name
);
1665 if (ip
->i_depth
< GFS2_DIR_MAX_DEPTH
) {
1666 error
= dir_double_exhash(ip
);
1669 error
= dir_split_leaf(inode
, name
);
1675 error
= dir_new_leaf(inode
, name
);
1686 * gfs2_dir_del - Delete a directory entry
1687 * @dip: The GFS2 inode
1688 * @filename: The filename
1690 * Returns: 0 on success, error code on failure
1693 int gfs2_dir_del(struct gfs2_inode
*dip
, const struct dentry
*dentry
)
1695 const struct qstr
*name
= &dentry
->d_name
;
1696 struct gfs2_dirent
*dent
, *prev
= NULL
;
1697 struct buffer_head
*bh
;
1700 /* Returns _either_ the entry (if its first in block) or the
1701 previous entry otherwise */
1702 dent
= gfs2_dirent_search(&dip
->i_inode
, name
, gfs2_dirent_prev
, &bh
);
1704 gfs2_consist_inode(dip
);
1708 gfs2_consist_inode(dip
);
1709 return PTR_ERR(dent
);
1711 /* If not first in block, adjust pointers accordingly */
1712 if (gfs2_dirent_find(dent
, name
, NULL
) == 0) {
1714 dent
= (struct gfs2_dirent
*)((char *)dent
+ be16_to_cpu(prev
->de_rec_len
));
1717 dirent_del(dip
, bh
, prev
, dent
);
1718 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1719 struct gfs2_leaf
*leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1720 u16 entries
= be16_to_cpu(leaf
->lf_entries
);
1722 gfs2_consist_inode(dip
);
1723 leaf
->lf_entries
= cpu_to_be16(--entries
);
1727 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1731 if (!dip
->i_entries
)
1732 gfs2_consist_inode(dip
);
1733 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1735 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1736 if (S_ISDIR(dentry
->d_inode
->i_mode
))
1737 drop_nlink(&dip
->i_inode
);
1738 gfs2_dinode_out(dip
, bh
->b_data
);
1740 mark_inode_dirty(&dip
->i_inode
);
1746 * gfs2_dir_mvino - Change inode number of directory entry
1747 * @dip: The GFS2 inode
1751 * This routine changes the inode number of a directory entry. It's used
1752 * by rename to change ".." when a directory is moved.
1753 * Assumes a glock is held on dvp.
1758 int gfs2_dir_mvino(struct gfs2_inode
*dip
, const struct qstr
*filename
,
1759 const struct gfs2_inode
*nip
, unsigned int new_type
)
1761 struct buffer_head
*bh
;
1762 struct gfs2_dirent
*dent
;
1765 dent
= gfs2_dirent_search(&dip
->i_inode
, filename
, gfs2_dirent_find
, &bh
);
1767 gfs2_consist_inode(dip
);
1771 return PTR_ERR(dent
);
1773 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1774 gfs2_inum_out(nip
, dent
);
1775 dent
->de_type
= cpu_to_be16(new_type
);
1777 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1779 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1782 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1785 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1786 gfs2_dinode_out(dip
, bh
->b_data
);
1792 * leaf_dealloc - Deallocate a directory leaf
1793 * @dip: the directory
1794 * @index: the hash table offset in the directory
1795 * @len: the number of pointers to this leaf
1796 * @leaf_no: the leaf number
1797 * @leaf_bh: buffer_head for the starting leaf
1798 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1803 static int leaf_dealloc(struct gfs2_inode
*dip
, u32 index
, u32 len
,
1804 u64 leaf_no
, struct buffer_head
*leaf_bh
,
1807 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1808 struct gfs2_leaf
*tmp_leaf
;
1809 struct gfs2_rgrp_list rlist
;
1810 struct buffer_head
*bh
, *dibh
;
1812 unsigned int rg_blocks
= 0, l_blocks
= 0;
1814 unsigned int x
, size
= len
* sizeof(u64
);
1817 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1819 ht
= kzalloc(size
, GFP_NOFS
);
1823 if (!gfs2_alloc_get(dip
)) {
1828 error
= gfs2_quota_hold(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1832 error
= gfs2_rindex_hold(sdp
, &dip
->i_alloc
->al_ri_gh
);
1836 /* Count the number of leaves */
1839 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1840 if (blk
!= leaf_no
) {
1841 error
= get_leaf(dip
, blk
, &bh
);
1845 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1846 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1850 gfs2_rlist_add(sdp
, &rlist
, blk
);
1854 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
1856 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1857 struct gfs2_rgrpd
*rgd
;
1858 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1859 rg_blocks
+= rgd
->rd_length
;
1862 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1866 error
= gfs2_trans_begin(sdp
,
1867 rg_blocks
+ (DIV_ROUND_UP(size
, sdp
->sd_jbsize
) + 1) +
1868 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, l_blocks
);
1870 goto out_rg_gunlock
;
1874 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1875 if (blk
!= leaf_no
) {
1876 error
= get_leaf(dip
, blk
, &bh
);
1880 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1881 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1885 gfs2_free_meta(dip
, blk
, 1);
1886 gfs2_add_inode_blocks(&dip
->i_inode
, -1);
1889 error
= gfs2_dir_write_data(dip
, ht
, index
* sizeof(u64
), size
);
1890 if (error
!= size
) {
1896 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1900 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1901 /* On the last dealloc, make this a regular file in case we crash.
1902 (We don't want to free these blocks a second time.) */
1904 dip
->i_inode
.i_mode
= S_IFREG
;
1905 gfs2_dinode_out(dip
, dibh
->b_data
);
1909 gfs2_trans_end(sdp
);
1911 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1913 gfs2_rlist_free(&rlist
);
1914 gfs2_glock_dq_uninit(&dip
->i_alloc
->al_ri_gh
);
1916 gfs2_quota_unhold(dip
);
1918 gfs2_alloc_put(dip
);
1925 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1926 * @dip: the directory
1928 * Dealloc all on-disk directory leaves to FREEMETA state
1929 * Change on-disk inode type to "regular file"
1934 int gfs2_dir_exhash_dealloc(struct gfs2_inode
*dip
)
1936 struct buffer_head
*bh
;
1937 struct gfs2_leaf
*leaf
;
1939 u32 index
= 0, next_index
;
1942 int error
= 0, last
;
1944 hsize
= 1 << dip
->i_depth
;
1946 lp
= gfs2_dir_get_hash_table(dip
);
1950 while (index
< hsize
) {
1951 leaf_no
= be64_to_cpu(lp
[index
]);
1953 error
= get_leaf(dip
, leaf_no
, &bh
);
1956 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1957 len
= 1 << (dip
->i_depth
- be16_to_cpu(leaf
->lf_depth
));
1959 next_index
= (index
& ~(len
- 1)) + len
;
1960 last
= ((next_index
>= hsize
) ? 1 : 0);
1961 error
= leaf_dealloc(dip
, index
, len
, leaf_no
, bh
,
1971 if (index
!= hsize
) {
1972 gfs2_consist_inode(dip
);
1982 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1983 * @ip: the file being written to
1984 * @filname: the filename that's going to be added
1986 * Returns: 1 if alloc required, 0 if not, -ve on error
1989 int gfs2_diradd_alloc_required(struct inode
*inode
, const struct qstr
*name
)
1991 struct gfs2_dirent
*dent
;
1992 struct buffer_head
*bh
;
1994 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
, &bh
);
1999 return PTR_ERR(dent
);