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
, __be64
*buf
,
246 struct buffer_head
*dibh
;
249 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
251 memcpy(buf
, dibh
->b_data
+ sizeof(struct gfs2_dinode
), size
);
255 return (error
) ? error
: size
;
260 * gfs2_dir_read_data - Read a data from a directory inode
261 * @ip: The GFS2 Inode
262 * @buf: The buffer to place result into
263 * @size: Amount of data to transfer
265 * Returns: The amount of data actually copied or the error
267 static int gfs2_dir_read_data(struct gfs2_inode
*ip
, __be64
*buf
,
270 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
277 if (gfs2_is_stuffed(ip
))
278 return gfs2_dir_read_stuffed(ip
, buf
, size
);
280 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
284 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
286 while (copied
< size
) {
288 struct buffer_head
*bh
;
291 amount
= size
- copied
;
292 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
293 amount
= sdp
->sd_sb
.sb_bsize
- o
;
297 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
299 if (error
|| !dblock
)
302 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
304 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_WAIT
, &bh
);
308 error
= gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_JD
);
315 memcpy(buf
, bh
->b_data
+ o
, amount
);
317 buf
+= (amount
/sizeof(__be64
));
320 o
= sizeof(struct gfs2_meta_header
);
325 return (copied
) ? copied
: error
;
329 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
330 * @ip: The inode in question
332 * Returns: The hash table or an error
335 static __be64
*gfs2_dir_get_hash_table(struct gfs2_inode
*ip
)
337 struct inode
*inode
= &ip
->i_inode
;
342 BUG_ON(!(ip
->i_diskflags
& GFS2_DIF_EXHASH
));
344 hc
= ip
->i_hash_cache
;
348 hsize
= 1 << ip
->i_depth
;
349 hsize
*= sizeof(__be64
);
350 if (hsize
!= i_size_read(&ip
->i_inode
)) {
351 gfs2_consist_inode(ip
);
352 return ERR_PTR(-EIO
);
355 hc
= kmalloc(hsize
, GFP_NOFS
);
358 return ERR_PTR(-ENOMEM
);
360 ret
= gfs2_dir_read_data(ip
, hc
, hsize
);
366 spin_lock(&inode
->i_lock
);
367 if (ip
->i_hash_cache
)
370 ip
->i_hash_cache
= hc
;
371 spin_unlock(&inode
->i_lock
);
373 return ip
->i_hash_cache
;
377 * gfs2_dir_hash_inval - Invalidate dir hash
378 * @ip: The directory inode
380 * Must be called with an exclusive glock, or during glock invalidation.
382 void gfs2_dir_hash_inval(struct gfs2_inode
*ip
)
384 __be64
*hc
= ip
->i_hash_cache
;
385 ip
->i_hash_cache
= NULL
;
389 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent
*dent
)
391 return dent
->de_inum
.no_addr
== 0 || dent
->de_inum
.no_formal_ino
== 0;
394 static inline int __gfs2_dirent_find(const struct gfs2_dirent
*dent
,
395 const struct qstr
*name
, int ret
)
397 if (!gfs2_dirent_sentinel(dent
) &&
398 be32_to_cpu(dent
->de_hash
) == name
->hash
&&
399 be16_to_cpu(dent
->de_name_len
) == name
->len
&&
400 memcmp(dent
+1, name
->name
, name
->len
) == 0)
405 static int gfs2_dirent_find(const struct gfs2_dirent
*dent
,
406 const struct qstr
*name
,
409 return __gfs2_dirent_find(dent
, name
, 1);
412 static int gfs2_dirent_prev(const struct gfs2_dirent
*dent
,
413 const struct qstr
*name
,
416 return __gfs2_dirent_find(dent
, name
, 2);
420 * name->name holds ptr to start of block.
421 * name->len holds size of block.
423 static int gfs2_dirent_last(const struct gfs2_dirent
*dent
,
424 const struct qstr
*name
,
427 const char *start
= name
->name
;
428 const char *end
= (const char *)dent
+ be16_to_cpu(dent
->de_rec_len
);
429 if (name
->len
== (end
- start
))
434 static int gfs2_dirent_find_space(const struct gfs2_dirent
*dent
,
435 const struct qstr
*name
,
438 unsigned required
= GFS2_DIRENT_SIZE(name
->len
);
439 unsigned actual
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
440 unsigned totlen
= be16_to_cpu(dent
->de_rec_len
);
442 if (gfs2_dirent_sentinel(dent
))
444 if (totlen
- actual
>= required
)
449 struct dirent_gather
{
450 const struct gfs2_dirent
**pdent
;
454 static int gfs2_dirent_gather(const struct gfs2_dirent
*dent
,
455 const struct qstr
*name
,
458 struct dirent_gather
*g
= opaque
;
459 if (!gfs2_dirent_sentinel(dent
)) {
460 g
->pdent
[g
->offset
++] = dent
;
466 * Other possible things to check:
467 * - Inode located within filesystem size (and on valid block)
468 * - Valid directory entry type
469 * Not sure how heavy-weight we want to make this... could also check
470 * hash is correct for example, but that would take a lot of extra time.
471 * For now the most important thing is to check that the various sizes
474 static int gfs2_check_dirent(struct gfs2_dirent
*dent
, unsigned int offset
,
475 unsigned int size
, unsigned int len
, int first
)
477 const char *msg
= "gfs2_dirent too small";
478 if (unlikely(size
< sizeof(struct gfs2_dirent
)))
480 msg
= "gfs2_dirent misaligned";
481 if (unlikely(offset
& 0x7))
483 msg
= "gfs2_dirent points beyond end of block";
484 if (unlikely(offset
+ size
> len
))
486 msg
= "zero inode number";
487 if (unlikely(!first
&& gfs2_dirent_sentinel(dent
)))
489 msg
= "name length is greater than space in dirent";
490 if (!gfs2_dirent_sentinel(dent
) &&
491 unlikely(sizeof(struct gfs2_dirent
)+be16_to_cpu(dent
->de_name_len
) >
496 printk(KERN_WARNING
"gfs2_check_dirent: %s (%s)\n", msg
,
497 first
? "first in block" : "not first in block");
501 static int gfs2_dirent_offset(const void *buf
)
503 const struct gfs2_meta_header
*h
= buf
;
508 switch(be32_to_cpu(h
->mh_type
)) {
509 case GFS2_METATYPE_LF
:
510 offset
= sizeof(struct gfs2_leaf
);
512 case GFS2_METATYPE_DI
:
513 offset
= sizeof(struct gfs2_dinode
);
520 printk(KERN_WARNING
"gfs2_scan_dirent: wrong block type %u\n",
521 be32_to_cpu(h
->mh_type
));
525 static struct gfs2_dirent
*gfs2_dirent_scan(struct inode
*inode
, void *buf
,
526 unsigned int len
, gfs2_dscan_t scan
,
527 const struct qstr
*name
,
530 struct gfs2_dirent
*dent
, *prev
;
535 ret
= gfs2_dirent_offset(buf
);
542 size
= be16_to_cpu(dent
->de_rec_len
);
543 if (gfs2_check_dirent(dent
, offset
, size
, len
, 1))
546 ret
= scan(dent
, name
, opaque
);
554 size
= be16_to_cpu(dent
->de_rec_len
);
555 if (gfs2_check_dirent(dent
, offset
, size
, len
, 0))
565 return prev
? prev
: dent
;
572 gfs2_consist_inode(GFS2_I(inode
));
573 return ERR_PTR(-EIO
);
576 static int dirent_check_reclen(struct gfs2_inode
*dip
,
577 const struct gfs2_dirent
*d
, const void *end_p
)
580 u16 rec_len
= be16_to_cpu(d
->de_rec_len
);
582 if (unlikely(rec_len
< sizeof(struct gfs2_dirent
)))
590 gfs2_consist_inode(dip
);
595 * dirent_next - Next dirent
596 * @dip: the directory
598 * @dent: Pointer to list of dirents
600 * Returns: 0 on success, error code otherwise
603 static int dirent_next(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
604 struct gfs2_dirent
**dent
)
606 struct gfs2_dirent
*cur
= *dent
, *tmp
;
607 char *bh_end
= bh
->b_data
+ bh
->b_size
;
610 ret
= dirent_check_reclen(dip
, cur
, bh_end
);
614 tmp
= (void *)cur
+ ret
;
615 ret
= dirent_check_reclen(dip
, tmp
, bh_end
);
619 /* Only the first dent could ever have de_inum.no_addr == 0 */
620 if (gfs2_dirent_sentinel(tmp
)) {
621 gfs2_consist_inode(dip
);
630 * dirent_del - Delete a dirent
631 * @dip: The GFS2 inode
633 * @prev: The previous dirent
634 * @cur: The current dirent
638 static void dirent_del(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
639 struct gfs2_dirent
*prev
, struct gfs2_dirent
*cur
)
641 u16 cur_rec_len
, prev_rec_len
;
643 if (gfs2_dirent_sentinel(cur
)) {
644 gfs2_consist_inode(dip
);
648 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
650 /* If there is no prev entry, this is the first entry in the block.
651 The de_rec_len is already as big as it needs to be. Just zero
652 out the inode number and return. */
655 cur
->de_inum
.no_addr
= 0;
656 cur
->de_inum
.no_formal_ino
= 0;
660 /* Combine this dentry with the previous one. */
662 prev_rec_len
= be16_to_cpu(prev
->de_rec_len
);
663 cur_rec_len
= be16_to_cpu(cur
->de_rec_len
);
665 if ((char *)prev
+ prev_rec_len
!= (char *)cur
)
666 gfs2_consist_inode(dip
);
667 if ((char *)cur
+ cur_rec_len
> bh
->b_data
+ bh
->b_size
)
668 gfs2_consist_inode(dip
);
670 prev_rec_len
+= cur_rec_len
;
671 prev
->de_rec_len
= cpu_to_be16(prev_rec_len
);
675 * Takes a dent from which to grab space as an argument. Returns the
676 * newly created dent.
678 static struct gfs2_dirent
*gfs2_init_dirent(struct inode
*inode
,
679 struct gfs2_dirent
*dent
,
680 const struct qstr
*name
,
681 struct buffer_head
*bh
)
683 struct gfs2_inode
*ip
= GFS2_I(inode
);
684 struct gfs2_dirent
*ndent
;
685 unsigned offset
= 0, totlen
;
687 if (!gfs2_dirent_sentinel(dent
))
688 offset
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
689 totlen
= be16_to_cpu(dent
->de_rec_len
);
690 BUG_ON(offset
+ name
->len
> totlen
);
691 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
692 ndent
= (struct gfs2_dirent
*)((char *)dent
+ offset
);
693 dent
->de_rec_len
= cpu_to_be16(offset
);
694 gfs2_qstr2dirent(name
, totlen
- offset
, ndent
);
698 static struct gfs2_dirent
*gfs2_dirent_alloc(struct inode
*inode
,
699 struct buffer_head
*bh
,
700 const struct qstr
*name
)
702 struct gfs2_dirent
*dent
;
703 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
704 gfs2_dirent_find_space
, name
, NULL
);
705 if (!dent
|| IS_ERR(dent
))
707 return gfs2_init_dirent(inode
, dent
, name
, bh
);
710 static int get_leaf(struct gfs2_inode
*dip
, u64 leaf_no
,
711 struct buffer_head
**bhp
)
715 error
= gfs2_meta_read(dip
->i_gl
, leaf_no
, DIO_WAIT
, bhp
);
716 if (!error
&& gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), *bhp
, GFS2_METATYPE_LF
)) {
717 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
725 * get_leaf_nr - Get a leaf number associated with the index
726 * @dip: The GFS2 inode
730 * Returns: 0 on success, error code otherwise
733 static int get_leaf_nr(struct gfs2_inode
*dip
, u32 index
,
738 hash
= gfs2_dir_get_hash_table(dip
);
740 return PTR_ERR(hash
);
741 *leaf_out
= be64_to_cpu(*(hash
+ index
));
745 static int get_first_leaf(struct gfs2_inode
*dip
, u32 index
,
746 struct buffer_head
**bh_out
)
751 error
= get_leaf_nr(dip
, index
, &leaf_no
);
753 error
= get_leaf(dip
, leaf_no
, bh_out
);
758 static struct gfs2_dirent
*gfs2_dirent_search(struct inode
*inode
,
759 const struct qstr
*name
,
761 struct buffer_head
**pbh
)
763 struct buffer_head
*bh
;
764 struct gfs2_dirent
*dent
;
765 struct gfs2_inode
*ip
= GFS2_I(inode
);
768 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
769 struct gfs2_leaf
*leaf
;
770 unsigned hsize
= 1 << ip
->i_depth
;
773 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
774 gfs2_consist_inode(ip
);
775 return ERR_PTR(-EIO
);
778 index
= name
->hash
>> (32 - ip
->i_depth
);
779 error
= get_first_leaf(ip
, index
, &bh
);
781 return ERR_PTR(error
);
783 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
787 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
788 ln
= be64_to_cpu(leaf
->lf_next
);
793 error
= get_leaf(ip
, ln
, &bh
);
796 return error
? ERR_PTR(error
) : NULL
;
800 error
= gfs2_meta_inode_buffer(ip
, &bh
);
802 return ERR_PTR(error
);
803 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
, scan
, name
, NULL
);
805 if (unlikely(dent
== NULL
|| IS_ERR(dent
))) {
813 static struct gfs2_leaf
*new_leaf(struct inode
*inode
, struct buffer_head
**pbh
, u16 depth
)
815 struct gfs2_inode
*ip
= GFS2_I(inode
);
819 struct buffer_head
*bh
;
820 struct gfs2_leaf
*leaf
;
821 struct gfs2_dirent
*dent
;
822 struct qstr name
= { .name
= "", .len
= 0, .hash
= 0 };
824 error
= gfs2_alloc_block(ip
, &bn
, &n
);
827 bh
= gfs2_meta_new(ip
->i_gl
, bn
);
831 gfs2_trans_add_unrevoke(GFS2_SB(inode
), bn
, 1);
832 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
833 gfs2_metatype_set(bh
, GFS2_METATYPE_LF
, GFS2_FORMAT_LF
);
834 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
835 leaf
->lf_depth
= cpu_to_be16(depth
);
836 leaf
->lf_entries
= 0;
837 leaf
->lf_dirent_format
= cpu_to_be32(GFS2_FORMAT_DE
);
839 memset(leaf
->lf_reserved
, 0, sizeof(leaf
->lf_reserved
));
840 dent
= (struct gfs2_dirent
*)(leaf
+1);
841 gfs2_qstr2dirent(&name
, bh
->b_size
- sizeof(struct gfs2_leaf
), dent
);
847 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
848 * @dip: The GFS2 inode
850 * Returns: 0 on success, error code otherwise
853 static int dir_make_exhash(struct inode
*inode
)
855 struct gfs2_inode
*dip
= GFS2_I(inode
);
856 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
857 struct gfs2_dirent
*dent
;
859 struct buffer_head
*bh
, *dibh
;
860 struct gfs2_leaf
*leaf
;
867 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
871 /* Turn over a new leaf */
873 leaf
= new_leaf(inode
, &bh
, 0);
878 gfs2_assert(sdp
, dip
->i_entries
< (1 << 16));
879 leaf
->lf_entries
= cpu_to_be16(dip
->i_entries
);
883 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_leaf
), dibh
,
884 sizeof(struct gfs2_dinode
));
886 /* Find last entry */
889 args
.len
= bh
->b_size
- sizeof(struct gfs2_dinode
) +
890 sizeof(struct gfs2_leaf
);
891 args
.name
= bh
->b_data
;
892 dent
= gfs2_dirent_scan(&dip
->i_inode
, bh
->b_data
, bh
->b_size
,
893 gfs2_dirent_last
, &args
, NULL
);
902 return PTR_ERR(dent
);
905 /* Adjust the last dirent's record length
906 (Remember that dent still points to the last entry.) */
908 dent
->de_rec_len
= cpu_to_be16(be16_to_cpu(dent
->de_rec_len
) +
909 sizeof(struct gfs2_dinode
) -
910 sizeof(struct gfs2_leaf
));
914 /* We're done with the new leaf block, now setup the new
917 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
918 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
920 lp
= (__be64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
922 for (x
= sdp
->sd_hash_ptrs
; x
--; lp
++)
923 *lp
= cpu_to_be64(bn
);
925 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
/ 2);
926 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
927 dip
->i_diskflags
|= GFS2_DIF_EXHASH
;
929 for (x
= sdp
->sd_hash_ptrs
, y
= -1; x
; x
>>= 1, y
++) ;
932 gfs2_dinode_out(dip
, dibh
->b_data
);
940 * dir_split_leaf - Split a leaf block into two
941 * @dip: The GFS2 inode
945 * Returns: 0 on success, error code on failure
948 static int dir_split_leaf(struct inode
*inode
, const struct qstr
*name
)
950 struct gfs2_inode
*dip
= GFS2_I(inode
);
951 struct buffer_head
*nbh
, *obh
, *dibh
;
952 struct gfs2_leaf
*nleaf
, *oleaf
;
953 struct gfs2_dirent
*dent
= NULL
, *prev
= NULL
, *next
= NULL
, *new;
954 u32 start
, len
, half_len
, divider
;
961 index
= name
->hash
>> (32 - dip
->i_depth
);
962 error
= get_leaf_nr(dip
, index
, &leaf_no
);
966 /* Get the old leaf block */
967 error
= get_leaf(dip
, leaf_no
, &obh
);
971 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
972 if (dip
->i_depth
== be16_to_cpu(oleaf
->lf_depth
)) {
974 return 1; /* can't split */
977 gfs2_trans_add_bh(dip
->i_gl
, obh
, 1);
979 nleaf
= new_leaf(inode
, &nbh
, be16_to_cpu(oleaf
->lf_depth
) + 1);
986 /* Compute the start and len of leaf pointers in the hash table. */
987 len
= 1 << (dip
->i_depth
- be16_to_cpu(oleaf
->lf_depth
));
990 printk(KERN_WARNING
"i_depth %u lf_depth %u index %u\n", dip
->i_depth
, be16_to_cpu(oleaf
->lf_depth
), index
);
991 gfs2_consist_inode(dip
);
996 start
= (index
& ~(len
- 1));
998 /* Change the pointers.
999 Don't bother distinguishing stuffed from non-stuffed.
1000 This code is complicated enough already. */
1001 lp
= kmalloc(half_len
* sizeof(__be64
), GFP_NOFS
);
1007 /* Change the pointers */
1008 for (x
= 0; x
< half_len
; x
++)
1009 lp
[x
] = cpu_to_be64(bn
);
1011 gfs2_dir_hash_inval(dip
);
1013 error
= gfs2_dir_write_data(dip
, (char *)lp
, start
* sizeof(u64
),
1014 half_len
* sizeof(u64
));
1015 if (error
!= half_len
* sizeof(u64
)) {
1023 /* Compute the divider */
1024 divider
= (start
+ half_len
) << (32 - dip
->i_depth
);
1026 /* Copy the entries */
1027 dent
= (struct gfs2_dirent
*)(obh
->b_data
+ sizeof(struct gfs2_leaf
));
1031 if (dirent_next(dip
, obh
, &next
))
1034 if (!gfs2_dirent_sentinel(dent
) &&
1035 be32_to_cpu(dent
->de_hash
) < divider
) {
1037 str
.name
= (char*)(dent
+1);
1038 str
.len
= be16_to_cpu(dent
->de_name_len
);
1039 str
.hash
= be32_to_cpu(dent
->de_hash
);
1040 new = gfs2_dirent_alloc(inode
, nbh
, &str
);
1042 error
= PTR_ERR(new);
1046 new->de_inum
= dent
->de_inum
; /* No endian worries */
1047 new->de_type
= dent
->de_type
; /* No endian worries */
1048 be16_add_cpu(&nleaf
->lf_entries
, 1);
1050 dirent_del(dip
, obh
, prev
, dent
);
1052 if (!oleaf
->lf_entries
)
1053 gfs2_consist_inode(dip
);
1054 be16_add_cpu(&oleaf
->lf_entries
, -1);
1066 oleaf
->lf_depth
= nleaf
->lf_depth
;
1068 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1069 if (!gfs2_assert_withdraw(GFS2_SB(&dip
->i_inode
), !error
)) {
1070 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1071 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
1072 gfs2_dinode_out(dip
, dibh
->b_data
);
1091 * dir_double_exhash - Double size of ExHash table
1092 * @dip: The GFS2 dinode
1094 * Returns: 0 on success, error code on failure
1097 static int dir_double_exhash(struct gfs2_inode
*dip
)
1099 struct buffer_head
*dibh
;
1107 hsize
= 1 << dip
->i_depth
;
1108 hsize_bytes
= hsize
* sizeof(__be64
);
1110 hc
= gfs2_dir_get_hash_table(dip
);
1114 h
= hc2
= kmalloc(hsize_bytes
* 2, GFP_NOFS
);
1118 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1122 for (x
= 0; x
< hsize
; x
++) {
1128 error
= gfs2_dir_write_data(dip
, (char *)hc2
, 0, hsize_bytes
* 2);
1129 if (error
!= (hsize_bytes
* 2))
1132 gfs2_dir_hash_inval(dip
);
1133 dip
->i_hash_cache
= hc2
;
1135 gfs2_dinode_out(dip
, dibh
->b_data
);
1140 /* Replace original hash table & size */
1141 gfs2_dir_write_data(dip
, (char *)hc
, 0, hsize_bytes
);
1142 i_size_write(&dip
->i_inode
, hsize_bytes
);
1143 gfs2_dinode_out(dip
, dibh
->b_data
);
1151 * compare_dents - compare directory entries by hash value
1155 * When comparing the hash entries of @a to @b:
1161 static int compare_dents(const void *a
, const void *b
)
1163 const struct gfs2_dirent
*dent_a
, *dent_b
;
1167 dent_a
= *(const struct gfs2_dirent
**)a
;
1168 hash_a
= be32_to_cpu(dent_a
->de_hash
);
1170 dent_b
= *(const struct gfs2_dirent
**)b
;
1171 hash_b
= be32_to_cpu(dent_b
->de_hash
);
1173 if (hash_a
> hash_b
)
1175 else if (hash_a
< hash_b
)
1178 unsigned int len_a
= be16_to_cpu(dent_a
->de_name_len
);
1179 unsigned int len_b
= be16_to_cpu(dent_b
->de_name_len
);
1183 else if (len_a
< len_b
)
1186 ret
= memcmp(dent_a
+ 1, dent_b
+ 1, len_a
);
1193 * do_filldir_main - read out directory entries
1194 * @dip: The GFS2 inode
1195 * @offset: The offset in the file to read from
1196 * @opaque: opaque data to pass to filldir
1197 * @filldir: The function to pass entries to
1198 * @darr: an array of struct gfs2_dirent pointers to read
1199 * @entries: the number of entries in darr
1200 * @copied: pointer to int that's non-zero if a entry has been copied out
1202 * Jump through some hoops to make sure that if there are hash collsions,
1203 * they are read out at the beginning of a buffer. We want to minimize
1204 * the possibility that they will fall into different readdir buffers or
1205 * that someone will want to seek to that location.
1207 * Returns: errno, >0 on exception from filldir
1210 static int do_filldir_main(struct gfs2_inode
*dip
, u64
*offset
,
1211 void *opaque
, filldir_t filldir
,
1212 const struct gfs2_dirent
**darr
, u32 entries
,
1215 const struct gfs2_dirent
*dent
, *dent_next
;
1221 sort(darr
, entries
, sizeof(struct gfs2_dirent
*), compare_dents
, NULL
);
1223 dent_next
= darr
[0];
1224 off_next
= be32_to_cpu(dent_next
->de_hash
);
1225 off_next
= gfs2_disk_hash2offset(off_next
);
1227 for (x
= 0, y
= 1; x
< entries
; x
++, y
++) {
1232 dent_next
= darr
[y
];
1233 off_next
= be32_to_cpu(dent_next
->de_hash
);
1234 off_next
= gfs2_disk_hash2offset(off_next
);
1240 if (off_next
== off
) {
1241 if (*copied
&& !run
)
1252 error
= filldir(opaque
, (const char *)(dent
+ 1),
1253 be16_to_cpu(dent
->de_name_len
),
1254 off
, be64_to_cpu(dent
->de_inum
.no_addr
),
1255 be16_to_cpu(dent
->de_type
));
1262 /* Increment the *offset by one, so the next time we come into the
1263 do_filldir fxn, we get the next entry instead of the last one in the
1271 static void *gfs2_alloc_sort_buffer(unsigned size
)
1275 if (size
< KMALLOC_MAX_SIZE
)
1276 ptr
= kmalloc(size
, GFP_NOFS
| __GFP_NOWARN
);
1278 ptr
= __vmalloc(size
, GFP_NOFS
, PAGE_KERNEL
);
1282 static void gfs2_free_sort_buffer(void *ptr
)
1284 if (is_vmalloc_addr(ptr
))
1290 static int gfs2_dir_read_leaf(struct inode
*inode
, u64
*offset
, void *opaque
,
1291 filldir_t filldir
, int *copied
, unsigned *depth
,
1294 struct gfs2_inode
*ip
= GFS2_I(inode
);
1295 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1296 struct buffer_head
*bh
;
1297 struct gfs2_leaf
*lf
;
1298 unsigned entries
= 0, entries2
= 0;
1299 unsigned leaves
= 0;
1300 const struct gfs2_dirent
**darr
, *dent
;
1301 struct dirent_gather g
;
1302 struct buffer_head
**larr
;
1308 error
= get_leaf(ip
, lfn
, &bh
);
1311 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1313 *depth
= be16_to_cpu(lf
->lf_depth
);
1314 entries
+= be16_to_cpu(lf
->lf_entries
);
1316 lfn
= be64_to_cpu(lf
->lf_next
);
1325 * The extra 99 entries are not normally used, but are a buffer
1326 * zone in case the number of entries in the leaf is corrupt.
1327 * 99 is the maximum number of entries that can fit in a single
1330 larr
= gfs2_alloc_sort_buffer((leaves
+ entries
+ 99) * sizeof(void *));
1333 darr
= (const struct gfs2_dirent
**)(larr
+ leaves
);
1339 error
= get_leaf(ip
, lfn
, &bh
);
1342 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1343 lfn
= be64_to_cpu(lf
->lf_next
);
1344 if (lf
->lf_entries
) {
1345 entries2
+= be16_to_cpu(lf
->lf_entries
);
1346 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
1347 gfs2_dirent_gather
, NULL
, &g
);
1348 error
= PTR_ERR(dent
);
1351 if (entries2
!= g
.offset
) {
1352 fs_warn(sdp
, "Number of entries corrupt in dir "
1353 "leaf %llu, entries2 (%u) != "
1355 (unsigned long long)bh
->b_blocknr
,
1356 entries2
, g
.offset
);
1368 BUG_ON(entries2
!= entries
);
1369 error
= do_filldir_main(ip
, offset
, opaque
, filldir
, darr
,
1372 for(i
= 0; i
< leaf
; i
++)
1374 gfs2_free_sort_buffer(larr
);
1381 * dir_e_read - Reads the entries from a directory into a filldir buffer
1382 * @dip: dinode pointer
1383 * @offset: the hash of the last entry read shifted to the right once
1384 * @opaque: buffer for the filldir function to fill
1385 * @filldir: points to the filldir function to use
1390 static int dir_e_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1393 struct gfs2_inode
*dip
= GFS2_I(inode
);
1401 hsize
= 1 << dip
->i_depth
;
1402 hash
= gfs2_dir_offset2hash(*offset
);
1403 index
= hash
>> (32 - dip
->i_depth
);
1405 lp
= gfs2_dir_get_hash_table(dip
);
1409 while (index
< hsize
) {
1410 error
= gfs2_dir_read_leaf(inode
, offset
, opaque
, filldir
,
1412 be64_to_cpu(lp
[index
]));
1416 len
= 1 << (dip
->i_depth
- depth
);
1417 index
= (index
& ~(len
- 1)) + len
;
1425 int gfs2_dir_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1428 struct gfs2_inode
*dip
= GFS2_I(inode
);
1429 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1430 struct dirent_gather g
;
1431 const struct gfs2_dirent
**darr
, *dent
;
1432 struct buffer_head
*dibh
;
1436 if (!dip
->i_entries
)
1439 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
)
1440 return dir_e_read(inode
, offset
, opaque
, filldir
);
1442 if (!gfs2_is_stuffed(dip
)) {
1443 gfs2_consist_inode(dip
);
1447 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1452 /* 96 is max number of dirents which can be stuffed into an inode */
1453 darr
= kmalloc(96 * sizeof(struct gfs2_dirent
*), GFP_NOFS
);
1457 dent
= gfs2_dirent_scan(inode
, dibh
->b_data
, dibh
->b_size
,
1458 gfs2_dirent_gather
, NULL
, &g
);
1460 error
= PTR_ERR(dent
);
1463 if (dip
->i_entries
!= g
.offset
) {
1464 fs_warn(sdp
, "Number of entries corrupt in dir %llu, "
1465 "ip->i_entries (%u) != g.offset (%u)\n",
1466 (unsigned long long)dip
->i_no_addr
,
1472 error
= do_filldir_main(dip
, offset
, opaque
, filldir
, darr
,
1473 dip
->i_entries
, &copied
);
1487 * gfs2_dir_search - Search a directory
1488 * @dip: The GFS2 inode
1492 * This routine searches a directory for a file or another directory.
1493 * Assumes a glock is held on dip.
1498 struct inode
*gfs2_dir_search(struct inode
*dir
, const struct qstr
*name
)
1500 struct buffer_head
*bh
;
1501 struct gfs2_dirent
*dent
;
1502 struct inode
*inode
;
1504 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1507 return ERR_CAST(dent
);
1508 inode
= gfs2_inode_lookup(dir
->i_sb
,
1509 be16_to_cpu(dent
->de_type
),
1510 be64_to_cpu(dent
->de_inum
.no_addr
),
1511 be64_to_cpu(dent
->de_inum
.no_formal_ino
), 0);
1515 return ERR_PTR(-ENOENT
);
1518 int gfs2_dir_check(struct inode
*dir
, const struct qstr
*name
,
1519 const struct gfs2_inode
*ip
)
1521 struct buffer_head
*bh
;
1522 struct gfs2_dirent
*dent
;
1525 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1528 return PTR_ERR(dent
);
1530 if (be64_to_cpu(dent
->de_inum
.no_addr
) != ip
->i_no_addr
)
1532 if (be64_to_cpu(dent
->de_inum
.no_formal_ino
) !=
1533 ip
->i_no_formal_ino
)
1535 if (unlikely(IF2DT(ip
->i_inode
.i_mode
) !=
1536 be16_to_cpu(dent
->de_type
))) {
1537 gfs2_consist_inode(GFS2_I(dir
));
1549 static int dir_new_leaf(struct inode
*inode
, const struct qstr
*name
)
1551 struct buffer_head
*bh
, *obh
;
1552 struct gfs2_inode
*ip
= GFS2_I(inode
);
1553 struct gfs2_leaf
*leaf
, *oleaf
;
1558 index
= name
->hash
>> (32 - ip
->i_depth
);
1559 error
= get_first_leaf(ip
, index
, &obh
);
1563 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
1564 bn
= be64_to_cpu(oleaf
->lf_next
);
1568 error
= get_leaf(ip
, bn
, &obh
);
1573 gfs2_trans_add_bh(ip
->i_gl
, obh
, 1);
1575 leaf
= new_leaf(inode
, &bh
, be16_to_cpu(oleaf
->lf_depth
));
1580 oleaf
->lf_next
= cpu_to_be64(bh
->b_blocknr
);
1584 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1587 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1588 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1589 gfs2_dinode_out(ip
, bh
->b_data
);
1595 * gfs2_dir_add - Add new filename into directory
1596 * @dip: The GFS2 inode
1597 * @filename: The new name
1598 * @inode: The inode number of the entry
1599 * @type: The type of the entry
1601 * Returns: 0 on success, error code on failure
1604 int gfs2_dir_add(struct inode
*inode
, const struct qstr
*name
,
1605 const struct gfs2_inode
*nip
)
1607 struct gfs2_inode
*ip
= GFS2_I(inode
);
1608 struct buffer_head
*bh
;
1609 struct gfs2_dirent
*dent
;
1610 struct gfs2_leaf
*leaf
;
1614 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
,
1618 return PTR_ERR(dent
);
1619 dent
= gfs2_init_dirent(inode
, dent
, name
, bh
);
1620 gfs2_inum_out(nip
, dent
);
1621 dent
->de_type
= cpu_to_be16(IF2DT(nip
->i_inode
.i_mode
));
1622 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1623 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1624 be16_add_cpu(&leaf
->lf_entries
, 1);
1627 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1630 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1632 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1633 if (S_ISDIR(nip
->i_inode
.i_mode
))
1634 inc_nlink(&ip
->i_inode
);
1635 gfs2_dinode_out(ip
, bh
->b_data
);
1640 if (!(ip
->i_diskflags
& GFS2_DIF_EXHASH
)) {
1641 error
= dir_make_exhash(inode
);
1646 error
= dir_split_leaf(inode
, name
);
1651 if (ip
->i_depth
< GFS2_DIR_MAX_DEPTH
) {
1652 error
= dir_double_exhash(ip
);
1655 error
= dir_split_leaf(inode
, name
);
1661 error
= dir_new_leaf(inode
, name
);
1672 * gfs2_dir_del - Delete a directory entry
1673 * @dip: The GFS2 inode
1674 * @filename: The filename
1676 * Returns: 0 on success, error code on failure
1679 int gfs2_dir_del(struct gfs2_inode
*dip
, const struct dentry
*dentry
)
1681 const struct qstr
*name
= &dentry
->d_name
;
1682 struct gfs2_dirent
*dent
, *prev
= NULL
;
1683 struct buffer_head
*bh
;
1685 /* Returns _either_ the entry (if its first in block) or the
1686 previous entry otherwise */
1687 dent
= gfs2_dirent_search(&dip
->i_inode
, name
, gfs2_dirent_prev
, &bh
);
1689 gfs2_consist_inode(dip
);
1693 gfs2_consist_inode(dip
);
1694 return PTR_ERR(dent
);
1696 /* If not first in block, adjust pointers accordingly */
1697 if (gfs2_dirent_find(dent
, name
, NULL
) == 0) {
1699 dent
= (struct gfs2_dirent
*)((char *)dent
+ be16_to_cpu(prev
->de_rec_len
));
1702 dirent_del(dip
, bh
, prev
, dent
);
1703 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1704 struct gfs2_leaf
*leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1705 u16 entries
= be16_to_cpu(leaf
->lf_entries
);
1707 gfs2_consist_inode(dip
);
1708 leaf
->lf_entries
= cpu_to_be16(--entries
);
1712 if (!dip
->i_entries
)
1713 gfs2_consist_inode(dip
);
1715 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1716 if (S_ISDIR(dentry
->d_inode
->i_mode
))
1717 drop_nlink(&dip
->i_inode
);
1718 mark_inode_dirty(&dip
->i_inode
);
1724 * gfs2_dir_mvino - Change inode number of directory entry
1725 * @dip: The GFS2 inode
1729 * This routine changes the inode number of a directory entry. It's used
1730 * by rename to change ".." when a directory is moved.
1731 * Assumes a glock is held on dvp.
1736 int gfs2_dir_mvino(struct gfs2_inode
*dip
, const struct qstr
*filename
,
1737 const struct gfs2_inode
*nip
, unsigned int new_type
)
1739 struct buffer_head
*bh
;
1740 struct gfs2_dirent
*dent
;
1743 dent
= gfs2_dirent_search(&dip
->i_inode
, filename
, gfs2_dirent_find
, &bh
);
1745 gfs2_consist_inode(dip
);
1749 return PTR_ERR(dent
);
1751 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1752 gfs2_inum_out(nip
, dent
);
1753 dent
->de_type
= cpu_to_be16(new_type
);
1755 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1757 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1760 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1763 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1764 gfs2_dinode_out(dip
, bh
->b_data
);
1770 * leaf_dealloc - Deallocate a directory leaf
1771 * @dip: the directory
1772 * @index: the hash table offset in the directory
1773 * @len: the number of pointers to this leaf
1774 * @leaf_no: the leaf number
1775 * @leaf_bh: buffer_head for the starting leaf
1776 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1781 static int leaf_dealloc(struct gfs2_inode
*dip
, u32 index
, u32 len
,
1782 u64 leaf_no
, struct buffer_head
*leaf_bh
,
1785 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1786 struct gfs2_leaf
*tmp_leaf
;
1787 struct gfs2_rgrp_list rlist
;
1788 struct buffer_head
*bh
, *dibh
;
1790 unsigned int rg_blocks
= 0, l_blocks
= 0;
1792 unsigned int x
, size
= len
* sizeof(u64
);
1795 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1797 ht
= kzalloc(size
, GFP_NOFS
);
1801 if (!gfs2_alloc_get(dip
)) {
1806 error
= gfs2_quota_hold(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1810 /* Count the number of leaves */
1813 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1814 if (blk
!= leaf_no
) {
1815 error
= get_leaf(dip
, blk
, &bh
);
1819 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1820 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1824 gfs2_rlist_add(dip
, &rlist
, blk
);
1828 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
1830 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1831 struct gfs2_rgrpd
*rgd
;
1832 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1833 rg_blocks
+= rgd
->rd_length
;
1836 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1840 error
= gfs2_trans_begin(sdp
,
1841 rg_blocks
+ (DIV_ROUND_UP(size
, sdp
->sd_jbsize
) + 1) +
1842 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, l_blocks
);
1844 goto out_rg_gunlock
;
1848 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1849 if (blk
!= leaf_no
) {
1850 error
= get_leaf(dip
, blk
, &bh
);
1854 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1855 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1859 gfs2_free_meta(dip
, blk
, 1);
1860 gfs2_add_inode_blocks(&dip
->i_inode
, -1);
1863 error
= gfs2_dir_write_data(dip
, ht
, index
* sizeof(u64
), size
);
1864 if (error
!= size
) {
1870 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1874 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1875 /* On the last dealloc, make this a regular file in case we crash.
1876 (We don't want to free these blocks a second time.) */
1878 dip
->i_inode
.i_mode
= S_IFREG
;
1879 gfs2_dinode_out(dip
, dibh
->b_data
);
1883 gfs2_trans_end(sdp
);
1885 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1887 gfs2_rlist_free(&rlist
);
1888 gfs2_quota_unhold(dip
);
1890 gfs2_alloc_put(dip
);
1897 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1898 * @dip: the directory
1900 * Dealloc all on-disk directory leaves to FREEMETA state
1901 * Change on-disk inode type to "regular file"
1906 int gfs2_dir_exhash_dealloc(struct gfs2_inode
*dip
)
1908 struct buffer_head
*bh
;
1909 struct gfs2_leaf
*leaf
;
1911 u32 index
= 0, next_index
;
1914 int error
= 0, last
;
1916 hsize
= 1 << dip
->i_depth
;
1918 lp
= gfs2_dir_get_hash_table(dip
);
1922 while (index
< hsize
) {
1923 leaf_no
= be64_to_cpu(lp
[index
]);
1925 error
= get_leaf(dip
, leaf_no
, &bh
);
1928 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1929 len
= 1 << (dip
->i_depth
- be16_to_cpu(leaf
->lf_depth
));
1931 next_index
= (index
& ~(len
- 1)) + len
;
1932 last
= ((next_index
>= hsize
) ? 1 : 0);
1933 error
= leaf_dealloc(dip
, index
, len
, leaf_no
, bh
,
1943 if (index
!= hsize
) {
1944 gfs2_consist_inode(dip
);
1954 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1955 * @ip: the file being written to
1956 * @filname: the filename that's going to be added
1958 * Returns: 1 if alloc required, 0 if not, -ve on error
1961 int gfs2_diradd_alloc_required(struct inode
*inode
, const struct qstr
*name
)
1963 struct gfs2_dirent
*dent
;
1964 struct buffer_head
*bh
;
1966 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
, &bh
);
1971 return PTR_ERR(dent
);