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
;
342 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent
*dent
)
344 return dent
->de_inum
.no_addr
== 0 || dent
->de_inum
.no_formal_ino
== 0;
347 static inline int __gfs2_dirent_find(const struct gfs2_dirent
*dent
,
348 const struct qstr
*name
, int ret
)
350 if (!gfs2_dirent_sentinel(dent
) &&
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 (gfs2_dirent_sentinel(dent
))
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 (!gfs2_dirent_sentinel(dent
)) {
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
&& gfs2_dirent_sentinel(dent
)))
442 msg
= "name length is greater than space in dirent";
443 if (!gfs2_dirent_sentinel(dent
) &&
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
);
529 static int dirent_check_reclen(struct gfs2_inode
*dip
,
530 const struct gfs2_dirent
*d
, const void *end_p
)
533 u16 rec_len
= be16_to_cpu(d
->de_rec_len
);
535 if (unlikely(rec_len
< sizeof(struct gfs2_dirent
)))
543 gfs2_consist_inode(dip
);
548 * dirent_next - Next dirent
549 * @dip: the directory
551 * @dent: Pointer to list of dirents
553 * Returns: 0 on success, error code otherwise
556 static int dirent_next(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
557 struct gfs2_dirent
**dent
)
559 struct gfs2_dirent
*cur
= *dent
, *tmp
;
560 char *bh_end
= bh
->b_data
+ bh
->b_size
;
563 ret
= dirent_check_reclen(dip
, cur
, bh_end
);
567 tmp
= (void *)cur
+ ret
;
568 ret
= dirent_check_reclen(dip
, tmp
, bh_end
);
572 /* Only the first dent could ever have de_inum.no_addr == 0 */
573 if (gfs2_dirent_sentinel(tmp
)) {
574 gfs2_consist_inode(dip
);
583 * dirent_del - Delete a dirent
584 * @dip: The GFS2 inode
586 * @prev: The previous dirent
587 * @cur: The current dirent
591 static void dirent_del(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
592 struct gfs2_dirent
*prev
, struct gfs2_dirent
*cur
)
594 u16 cur_rec_len
, prev_rec_len
;
596 if (gfs2_dirent_sentinel(cur
)) {
597 gfs2_consist_inode(dip
);
601 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
603 /* If there is no prev entry, this is the first entry in the block.
604 The de_rec_len is already as big as it needs to be. Just zero
605 out the inode number and return. */
608 cur
->de_inum
.no_addr
= 0;
609 cur
->de_inum
.no_formal_ino
= 0;
613 /* Combine this dentry with the previous one. */
615 prev_rec_len
= be16_to_cpu(prev
->de_rec_len
);
616 cur_rec_len
= be16_to_cpu(cur
->de_rec_len
);
618 if ((char *)prev
+ prev_rec_len
!= (char *)cur
)
619 gfs2_consist_inode(dip
);
620 if ((char *)cur
+ cur_rec_len
> bh
->b_data
+ bh
->b_size
)
621 gfs2_consist_inode(dip
);
623 prev_rec_len
+= cur_rec_len
;
624 prev
->de_rec_len
= cpu_to_be16(prev_rec_len
);
628 * Takes a dent from which to grab space as an argument. Returns the
629 * newly created dent.
631 static struct gfs2_dirent
*gfs2_init_dirent(struct inode
*inode
,
632 struct gfs2_dirent
*dent
,
633 const struct qstr
*name
,
634 struct buffer_head
*bh
)
636 struct gfs2_inode
*ip
= GFS2_I(inode
);
637 struct gfs2_dirent
*ndent
;
638 unsigned offset
= 0, totlen
;
640 if (!gfs2_dirent_sentinel(dent
))
641 offset
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
642 totlen
= be16_to_cpu(dent
->de_rec_len
);
643 BUG_ON(offset
+ name
->len
> totlen
);
644 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
645 ndent
= (struct gfs2_dirent
*)((char *)dent
+ offset
);
646 dent
->de_rec_len
= cpu_to_be16(offset
);
647 gfs2_qstr2dirent(name
, totlen
- offset
, ndent
);
651 static struct gfs2_dirent
*gfs2_dirent_alloc(struct inode
*inode
,
652 struct buffer_head
*bh
,
653 const struct qstr
*name
)
655 struct gfs2_dirent
*dent
;
656 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
657 gfs2_dirent_find_space
, name
, NULL
);
658 if (!dent
|| IS_ERR(dent
))
660 return gfs2_init_dirent(inode
, dent
, name
, bh
);
663 static int get_leaf(struct gfs2_inode
*dip
, u64 leaf_no
,
664 struct buffer_head
**bhp
)
668 error
= gfs2_meta_read(dip
->i_gl
, leaf_no
, DIO_WAIT
, bhp
);
669 if (!error
&& gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), *bhp
, GFS2_METATYPE_LF
)) {
670 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
678 * get_leaf_nr - Get a leaf number associated with the index
679 * @dip: The GFS2 inode
683 * Returns: 0 on success, error code otherwise
686 static int get_leaf_nr(struct gfs2_inode
*dip
, u32 index
,
692 error
= gfs2_dir_read_data(dip
, (char *)&leaf_no
,
693 index
* sizeof(__be64
),
695 if (error
!= sizeof(u64
))
696 return (error
< 0) ? error
: -EIO
;
698 *leaf_out
= be64_to_cpu(leaf_no
);
703 static int get_first_leaf(struct gfs2_inode
*dip
, u32 index
,
704 struct buffer_head
**bh_out
)
709 error
= get_leaf_nr(dip
, index
, &leaf_no
);
711 error
= get_leaf(dip
, leaf_no
, bh_out
);
716 static struct gfs2_dirent
*gfs2_dirent_search(struct inode
*inode
,
717 const struct qstr
*name
,
719 struct buffer_head
**pbh
)
721 struct buffer_head
*bh
;
722 struct gfs2_dirent
*dent
;
723 struct gfs2_inode
*ip
= GFS2_I(inode
);
726 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
727 struct gfs2_leaf
*leaf
;
728 unsigned hsize
= 1 << ip
->i_depth
;
731 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
732 gfs2_consist_inode(ip
);
733 return ERR_PTR(-EIO
);
736 index
= name
->hash
>> (32 - ip
->i_depth
);
737 error
= get_first_leaf(ip
, index
, &bh
);
739 return ERR_PTR(error
);
741 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
745 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
746 ln
= be64_to_cpu(leaf
->lf_next
);
751 error
= get_leaf(ip
, ln
, &bh
);
754 return error
? ERR_PTR(error
) : NULL
;
758 error
= gfs2_meta_inode_buffer(ip
, &bh
);
760 return ERR_PTR(error
);
761 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
, scan
, name
, NULL
);
763 if (unlikely(dent
== NULL
|| IS_ERR(dent
))) {
771 static struct gfs2_leaf
*new_leaf(struct inode
*inode
, struct buffer_head
**pbh
, u16 depth
)
773 struct gfs2_inode
*ip
= GFS2_I(inode
);
777 struct buffer_head
*bh
;
778 struct gfs2_leaf
*leaf
;
779 struct gfs2_dirent
*dent
;
780 struct qstr name
= { .name
= "", .len
= 0, .hash
= 0 };
782 error
= gfs2_alloc_block(ip
, &bn
, &n
);
785 bh
= gfs2_meta_new(ip
->i_gl
, bn
);
789 gfs2_trans_add_unrevoke(GFS2_SB(inode
), bn
, 1);
790 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
791 gfs2_metatype_set(bh
, GFS2_METATYPE_LF
, GFS2_FORMAT_LF
);
792 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
793 leaf
->lf_depth
= cpu_to_be16(depth
);
794 leaf
->lf_entries
= 0;
795 leaf
->lf_dirent_format
= cpu_to_be32(GFS2_FORMAT_DE
);
797 memset(leaf
->lf_reserved
, 0, sizeof(leaf
->lf_reserved
));
798 dent
= (struct gfs2_dirent
*)(leaf
+1);
799 gfs2_qstr2dirent(&name
, bh
->b_size
- sizeof(struct gfs2_leaf
), dent
);
805 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
806 * @dip: The GFS2 inode
808 * Returns: 0 on success, error code otherwise
811 static int dir_make_exhash(struct inode
*inode
)
813 struct gfs2_inode
*dip
= GFS2_I(inode
);
814 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
815 struct gfs2_dirent
*dent
;
817 struct buffer_head
*bh
, *dibh
;
818 struct gfs2_leaf
*leaf
;
825 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
829 /* Turn over a new leaf */
831 leaf
= new_leaf(inode
, &bh
, 0);
836 gfs2_assert(sdp
, dip
->i_entries
< (1 << 16));
837 leaf
->lf_entries
= cpu_to_be16(dip
->i_entries
);
841 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_leaf
), dibh
,
842 sizeof(struct gfs2_dinode
));
844 /* Find last entry */
847 args
.len
= bh
->b_size
- sizeof(struct gfs2_dinode
) +
848 sizeof(struct gfs2_leaf
);
849 args
.name
= bh
->b_data
;
850 dent
= gfs2_dirent_scan(&dip
->i_inode
, bh
->b_data
, bh
->b_size
,
851 gfs2_dirent_last
, &args
, NULL
);
860 return PTR_ERR(dent
);
863 /* Adjust the last dirent's record length
864 (Remember that dent still points to the last entry.) */
866 dent
->de_rec_len
= cpu_to_be16(be16_to_cpu(dent
->de_rec_len
) +
867 sizeof(struct gfs2_dinode
) -
868 sizeof(struct gfs2_leaf
));
872 /* We're done with the new leaf block, now setup the new
875 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
876 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
878 lp
= (__be64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
880 for (x
= sdp
->sd_hash_ptrs
; x
--; lp
++)
881 *lp
= cpu_to_be64(bn
);
883 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
/ 2);
884 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
885 dip
->i_diskflags
|= GFS2_DIF_EXHASH
;
887 for (x
= sdp
->sd_hash_ptrs
, y
= -1; x
; x
>>= 1, y
++) ;
890 gfs2_dinode_out(dip
, dibh
->b_data
);
898 * dir_split_leaf - Split a leaf block into two
899 * @dip: The GFS2 inode
903 * Returns: 0 on success, error code on failure
906 static int dir_split_leaf(struct inode
*inode
, const struct qstr
*name
)
908 struct gfs2_inode
*dip
= GFS2_I(inode
);
909 struct buffer_head
*nbh
, *obh
, *dibh
;
910 struct gfs2_leaf
*nleaf
, *oleaf
;
911 struct gfs2_dirent
*dent
= NULL
, *prev
= NULL
, *next
= NULL
, *new;
912 u32 start
, len
, half_len
, divider
;
919 index
= name
->hash
>> (32 - dip
->i_depth
);
920 error
= get_leaf_nr(dip
, index
, &leaf_no
);
924 /* Get the old leaf block */
925 error
= get_leaf(dip
, leaf_no
, &obh
);
929 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
930 if (dip
->i_depth
== be16_to_cpu(oleaf
->lf_depth
)) {
932 return 1; /* can't split */
935 gfs2_trans_add_bh(dip
->i_gl
, obh
, 1);
937 nleaf
= new_leaf(inode
, &nbh
, be16_to_cpu(oleaf
->lf_depth
) + 1);
944 /* Compute the start and len of leaf pointers in the hash table. */
945 len
= 1 << (dip
->i_depth
- be16_to_cpu(oleaf
->lf_depth
));
948 printk(KERN_WARNING
"i_depth %u lf_depth %u index %u\n", dip
->i_depth
, be16_to_cpu(oleaf
->lf_depth
), index
);
949 gfs2_consist_inode(dip
);
954 start
= (index
& ~(len
- 1));
956 /* Change the pointers.
957 Don't bother distinguishing stuffed from non-stuffed.
958 This code is complicated enough already. */
959 lp
= kmalloc(half_len
* sizeof(__be64
), GFP_NOFS
);
965 /* Change the pointers */
966 for (x
= 0; x
< half_len
; x
++)
967 lp
[x
] = cpu_to_be64(bn
);
969 error
= gfs2_dir_write_data(dip
, (char *)lp
, start
* sizeof(u64
),
970 half_len
* sizeof(u64
));
971 if (error
!= half_len
* sizeof(u64
)) {
979 /* Compute the divider */
980 divider
= (start
+ half_len
) << (32 - dip
->i_depth
);
982 /* Copy the entries */
983 dent
= (struct gfs2_dirent
*)(obh
->b_data
+ sizeof(struct gfs2_leaf
));
987 if (dirent_next(dip
, obh
, &next
))
990 if (!gfs2_dirent_sentinel(dent
) &&
991 be32_to_cpu(dent
->de_hash
) < divider
) {
993 str
.name
= (char*)(dent
+1);
994 str
.len
= be16_to_cpu(dent
->de_name_len
);
995 str
.hash
= be32_to_cpu(dent
->de_hash
);
996 new = gfs2_dirent_alloc(inode
, nbh
, &str
);
998 error
= PTR_ERR(new);
1002 new->de_inum
= dent
->de_inum
; /* No endian worries */
1003 new->de_type
= dent
->de_type
; /* No endian worries */
1004 be16_add_cpu(&nleaf
->lf_entries
, 1);
1006 dirent_del(dip
, obh
, prev
, dent
);
1008 if (!oleaf
->lf_entries
)
1009 gfs2_consist_inode(dip
);
1010 be16_add_cpu(&oleaf
->lf_entries
, -1);
1022 oleaf
->lf_depth
= nleaf
->lf_depth
;
1024 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1025 if (!gfs2_assert_withdraw(GFS2_SB(&dip
->i_inode
), !error
)) {
1026 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1027 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
1028 gfs2_dinode_out(dip
, dibh
->b_data
);
1047 * dir_double_exhash - Double size of ExHash table
1048 * @dip: The GFS2 dinode
1050 * Returns: 0 on success, error code on failure
1053 static int dir_double_exhash(struct gfs2_inode
*dip
)
1055 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1056 struct buffer_head
*dibh
;
1061 u64 disksize
= i_size_read(&dip
->i_inode
);
1065 hsize
= 1 << dip
->i_depth
;
1066 if (hsize
* sizeof(u64
) != disksize
) {
1067 gfs2_consist_inode(dip
);
1071 /* Allocate both the "from" and "to" buffers in one big chunk */
1073 buf
= kcalloc(3, sdp
->sd_hash_bsize
, GFP_NOFS
);
1077 for (block
= disksize
>> sdp
->sd_hash_bsize_shift
; block
--;) {
1078 error
= gfs2_dir_read_data(dip
, (char *)buf
,
1079 block
* sdp
->sd_hash_bsize
,
1080 sdp
->sd_hash_bsize
, 1);
1081 if (error
!= sdp
->sd_hash_bsize
) {
1088 to
= (u64
*)((char *)buf
+ sdp
->sd_hash_bsize
);
1090 for (x
= sdp
->sd_hash_ptrs
; x
--; from
++) {
1091 *to
++ = *from
; /* No endianess worries */
1095 error
= gfs2_dir_write_data(dip
,
1096 (char *)buf
+ sdp
->sd_hash_bsize
,
1097 block
* sdp
->sd_sb
.sb_bsize
,
1098 sdp
->sd_sb
.sb_bsize
);
1099 if (error
!= sdp
->sd_sb
.sb_bsize
) {
1108 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1109 if (!gfs2_assert_withdraw(sdp
, !error
)) {
1111 gfs2_dinode_out(dip
, dibh
->b_data
);
1123 * compare_dents - compare directory entries by hash value
1127 * When comparing the hash entries of @a to @b:
1133 static int compare_dents(const void *a
, const void *b
)
1135 const struct gfs2_dirent
*dent_a
, *dent_b
;
1139 dent_a
= *(const struct gfs2_dirent
**)a
;
1140 hash_a
= be32_to_cpu(dent_a
->de_hash
);
1142 dent_b
= *(const struct gfs2_dirent
**)b
;
1143 hash_b
= be32_to_cpu(dent_b
->de_hash
);
1145 if (hash_a
> hash_b
)
1147 else if (hash_a
< hash_b
)
1150 unsigned int len_a
= be16_to_cpu(dent_a
->de_name_len
);
1151 unsigned int len_b
= be16_to_cpu(dent_b
->de_name_len
);
1155 else if (len_a
< len_b
)
1158 ret
= memcmp(dent_a
+ 1, dent_b
+ 1, len_a
);
1165 * do_filldir_main - read out directory entries
1166 * @dip: The GFS2 inode
1167 * @offset: The offset in the file to read from
1168 * @opaque: opaque data to pass to filldir
1169 * @filldir: The function to pass entries to
1170 * @darr: an array of struct gfs2_dirent pointers to read
1171 * @entries: the number of entries in darr
1172 * @copied: pointer to int that's non-zero if a entry has been copied out
1174 * Jump through some hoops to make sure that if there are hash collsions,
1175 * they are read out at the beginning of a buffer. We want to minimize
1176 * the possibility that they will fall into different readdir buffers or
1177 * that someone will want to seek to that location.
1179 * Returns: errno, >0 on exception from filldir
1182 static int do_filldir_main(struct gfs2_inode
*dip
, u64
*offset
,
1183 void *opaque
, filldir_t filldir
,
1184 const struct gfs2_dirent
**darr
, u32 entries
,
1187 const struct gfs2_dirent
*dent
, *dent_next
;
1193 sort(darr
, entries
, sizeof(struct gfs2_dirent
*), compare_dents
, NULL
);
1195 dent_next
= darr
[0];
1196 off_next
= be32_to_cpu(dent_next
->de_hash
);
1197 off_next
= gfs2_disk_hash2offset(off_next
);
1199 for (x
= 0, y
= 1; x
< entries
; x
++, y
++) {
1204 dent_next
= darr
[y
];
1205 off_next
= be32_to_cpu(dent_next
->de_hash
);
1206 off_next
= gfs2_disk_hash2offset(off_next
);
1212 if (off_next
== off
) {
1213 if (*copied
&& !run
)
1224 error
= filldir(opaque
, (const char *)(dent
+ 1),
1225 be16_to_cpu(dent
->de_name_len
),
1226 off
, be64_to_cpu(dent
->de_inum
.no_addr
),
1227 be16_to_cpu(dent
->de_type
));
1234 /* Increment the *offset by one, so the next time we come into the
1235 do_filldir fxn, we get the next entry instead of the last one in the
1243 static void *gfs2_alloc_sort_buffer(unsigned size
)
1247 if (size
< KMALLOC_MAX_SIZE
)
1248 ptr
= kmalloc(size
, GFP_NOFS
| __GFP_NOWARN
);
1250 ptr
= __vmalloc(size
, GFP_NOFS
, PAGE_KERNEL
);
1254 static void gfs2_free_sort_buffer(void *ptr
)
1256 if (is_vmalloc_addr(ptr
))
1262 static int gfs2_dir_read_leaf(struct inode
*inode
, u64
*offset
, void *opaque
,
1263 filldir_t filldir
, int *copied
, unsigned *depth
,
1266 struct gfs2_inode
*ip
= GFS2_I(inode
);
1267 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1268 struct buffer_head
*bh
;
1269 struct gfs2_leaf
*lf
;
1270 unsigned entries
= 0, entries2
= 0;
1271 unsigned leaves
= 0;
1272 const struct gfs2_dirent
**darr
, *dent
;
1273 struct dirent_gather g
;
1274 struct buffer_head
**larr
;
1280 error
= get_leaf(ip
, lfn
, &bh
);
1283 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1285 *depth
= be16_to_cpu(lf
->lf_depth
);
1286 entries
+= be16_to_cpu(lf
->lf_entries
);
1288 lfn
= be64_to_cpu(lf
->lf_next
);
1297 * The extra 99 entries are not normally used, but are a buffer
1298 * zone in case the number of entries in the leaf is corrupt.
1299 * 99 is the maximum number of entries that can fit in a single
1302 larr
= gfs2_alloc_sort_buffer((leaves
+ entries
+ 99) * sizeof(void *));
1305 darr
= (const struct gfs2_dirent
**)(larr
+ leaves
);
1311 error
= get_leaf(ip
, lfn
, &bh
);
1314 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1315 lfn
= be64_to_cpu(lf
->lf_next
);
1316 if (lf
->lf_entries
) {
1317 entries2
+= be16_to_cpu(lf
->lf_entries
);
1318 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
1319 gfs2_dirent_gather
, NULL
, &g
);
1320 error
= PTR_ERR(dent
);
1323 if (entries2
!= g
.offset
) {
1324 fs_warn(sdp
, "Number of entries corrupt in dir "
1325 "leaf %llu, entries2 (%u) != "
1327 (unsigned long long)bh
->b_blocknr
,
1328 entries2
, g
.offset
);
1340 BUG_ON(entries2
!= entries
);
1341 error
= do_filldir_main(ip
, offset
, opaque
, filldir
, darr
,
1344 for(i
= 0; i
< leaf
; i
++)
1346 gfs2_free_sort_buffer(larr
);
1352 * dir_e_read - Reads the entries from a directory into a filldir buffer
1353 * @dip: dinode pointer
1354 * @offset: the hash of the last entry read shifted to the right once
1355 * @opaque: buffer for the filldir function to fill
1356 * @filldir: points to the filldir function to use
1361 static int dir_e_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1364 struct gfs2_inode
*dip
= GFS2_I(inode
);
1365 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1367 u32 ht_offset
, lp_offset
, ht_offset_cur
= -1;
1374 hsize
= 1 << dip
->i_depth
;
1375 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
1376 gfs2_consist_inode(dip
);
1380 hash
= gfs2_dir_offset2hash(*offset
);
1381 index
= hash
>> (32 - dip
->i_depth
);
1383 lp
= kmalloc(sdp
->sd_hash_bsize
, GFP_NOFS
);
1387 while (index
< hsize
) {
1388 lp_offset
= index
& (sdp
->sd_hash_ptrs
- 1);
1389 ht_offset
= index
- lp_offset
;
1391 if (ht_offset_cur
!= ht_offset
) {
1392 error
= gfs2_dir_read_data(dip
, (char *)lp
,
1393 ht_offset
* sizeof(__be64
),
1394 sdp
->sd_hash_bsize
, 1);
1395 if (error
!= sdp
->sd_hash_bsize
) {
1400 ht_offset_cur
= ht_offset
;
1403 error
= gfs2_dir_read_leaf(inode
, offset
, opaque
, filldir
,
1405 be64_to_cpu(lp
[lp_offset
]));
1409 len
= 1 << (dip
->i_depth
- depth
);
1410 index
= (index
& ~(len
- 1)) + len
;
1420 int gfs2_dir_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1423 struct gfs2_inode
*dip
= GFS2_I(inode
);
1424 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1425 struct dirent_gather g
;
1426 const struct gfs2_dirent
**darr
, *dent
;
1427 struct buffer_head
*dibh
;
1431 if (!dip
->i_entries
)
1434 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
)
1435 return dir_e_read(inode
, offset
, opaque
, filldir
);
1437 if (!gfs2_is_stuffed(dip
)) {
1438 gfs2_consist_inode(dip
);
1442 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1447 /* 96 is max number of dirents which can be stuffed into an inode */
1448 darr
= kmalloc(96 * sizeof(struct gfs2_dirent
*), GFP_NOFS
);
1452 dent
= gfs2_dirent_scan(inode
, dibh
->b_data
, dibh
->b_size
,
1453 gfs2_dirent_gather
, NULL
, &g
);
1455 error
= PTR_ERR(dent
);
1458 if (dip
->i_entries
!= g
.offset
) {
1459 fs_warn(sdp
, "Number of entries corrupt in dir %llu, "
1460 "ip->i_entries (%u) != g.offset (%u)\n",
1461 (unsigned long long)dip
->i_no_addr
,
1467 error
= do_filldir_main(dip
, offset
, opaque
, filldir
, darr
,
1468 dip
->i_entries
, &copied
);
1482 * gfs2_dir_search - Search a directory
1483 * @dip: The GFS2 inode
1487 * This routine searches a directory for a file or another directory.
1488 * Assumes a glock is held on dip.
1493 struct inode
*gfs2_dir_search(struct inode
*dir
, const struct qstr
*name
)
1495 struct buffer_head
*bh
;
1496 struct gfs2_dirent
*dent
;
1497 struct inode
*inode
;
1499 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1502 return ERR_CAST(dent
);
1503 inode
= gfs2_inode_lookup(dir
->i_sb
,
1504 be16_to_cpu(dent
->de_type
),
1505 be64_to_cpu(dent
->de_inum
.no_addr
),
1506 be64_to_cpu(dent
->de_inum
.no_formal_ino
), 0);
1510 return ERR_PTR(-ENOENT
);
1513 int gfs2_dir_check(struct inode
*dir
, const struct qstr
*name
,
1514 const struct gfs2_inode
*ip
)
1516 struct buffer_head
*bh
;
1517 struct gfs2_dirent
*dent
;
1520 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1523 return PTR_ERR(dent
);
1525 if (be64_to_cpu(dent
->de_inum
.no_addr
) != ip
->i_no_addr
)
1527 if (be64_to_cpu(dent
->de_inum
.no_formal_ino
) !=
1528 ip
->i_no_formal_ino
)
1530 if (unlikely(IF2DT(ip
->i_inode
.i_mode
) !=
1531 be16_to_cpu(dent
->de_type
))) {
1532 gfs2_consist_inode(GFS2_I(dir
));
1544 static int dir_new_leaf(struct inode
*inode
, const struct qstr
*name
)
1546 struct buffer_head
*bh
, *obh
;
1547 struct gfs2_inode
*ip
= GFS2_I(inode
);
1548 struct gfs2_leaf
*leaf
, *oleaf
;
1553 index
= name
->hash
>> (32 - ip
->i_depth
);
1554 error
= get_first_leaf(ip
, index
, &obh
);
1558 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
1559 bn
= be64_to_cpu(oleaf
->lf_next
);
1563 error
= get_leaf(ip
, bn
, &obh
);
1568 gfs2_trans_add_bh(ip
->i_gl
, obh
, 1);
1570 leaf
= new_leaf(inode
, &bh
, be16_to_cpu(oleaf
->lf_depth
));
1575 oleaf
->lf_next
= cpu_to_be64(bh
->b_blocknr
);
1579 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1582 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1583 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1584 gfs2_dinode_out(ip
, bh
->b_data
);
1590 * gfs2_dir_add - Add new filename into directory
1591 * @dip: The GFS2 inode
1592 * @filename: The new name
1593 * @inode: The inode number of the entry
1594 * @type: The type of the entry
1596 * Returns: 0 on success, error code on failure
1599 int gfs2_dir_add(struct inode
*inode
, const struct qstr
*name
,
1600 const struct gfs2_inode
*nip
)
1602 struct gfs2_inode
*ip
= GFS2_I(inode
);
1603 struct buffer_head
*bh
;
1604 struct gfs2_dirent
*dent
;
1605 struct gfs2_leaf
*leaf
;
1609 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
,
1613 return PTR_ERR(dent
);
1614 dent
= gfs2_init_dirent(inode
, dent
, name
, bh
);
1615 gfs2_inum_out(nip
, dent
);
1616 dent
->de_type
= cpu_to_be16(IF2DT(nip
->i_inode
.i_mode
));
1617 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1618 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1619 be16_add_cpu(&leaf
->lf_entries
, 1);
1622 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1625 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1627 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1628 if (S_ISDIR(nip
->i_inode
.i_mode
))
1629 inc_nlink(&ip
->i_inode
);
1630 gfs2_dinode_out(ip
, bh
->b_data
);
1635 if (!(ip
->i_diskflags
& GFS2_DIF_EXHASH
)) {
1636 error
= dir_make_exhash(inode
);
1641 error
= dir_split_leaf(inode
, name
);
1646 if (ip
->i_depth
< GFS2_DIR_MAX_DEPTH
) {
1647 error
= dir_double_exhash(ip
);
1650 error
= dir_split_leaf(inode
, name
);
1656 error
= dir_new_leaf(inode
, name
);
1667 * gfs2_dir_del - Delete a directory entry
1668 * @dip: The GFS2 inode
1669 * @filename: The filename
1671 * Returns: 0 on success, error code on failure
1674 int gfs2_dir_del(struct gfs2_inode
*dip
, const struct dentry
*dentry
)
1676 const struct qstr
*name
= &dentry
->d_name
;
1677 struct gfs2_dirent
*dent
, *prev
= NULL
;
1678 struct buffer_head
*bh
;
1681 /* Returns _either_ the entry (if its first in block) or the
1682 previous entry otherwise */
1683 dent
= gfs2_dirent_search(&dip
->i_inode
, name
, gfs2_dirent_prev
, &bh
);
1685 gfs2_consist_inode(dip
);
1689 gfs2_consist_inode(dip
);
1690 return PTR_ERR(dent
);
1692 /* If not first in block, adjust pointers accordingly */
1693 if (gfs2_dirent_find(dent
, name
, NULL
) == 0) {
1695 dent
= (struct gfs2_dirent
*)((char *)dent
+ be16_to_cpu(prev
->de_rec_len
));
1698 dirent_del(dip
, bh
, prev
, dent
);
1699 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1700 struct gfs2_leaf
*leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1701 u16 entries
= be16_to_cpu(leaf
->lf_entries
);
1703 gfs2_consist_inode(dip
);
1704 leaf
->lf_entries
= cpu_to_be16(--entries
);
1708 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1712 if (!dip
->i_entries
)
1713 gfs2_consist_inode(dip
);
1714 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1716 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1717 if (S_ISDIR(dentry
->d_inode
->i_mode
))
1718 drop_nlink(&dip
->i_inode
);
1719 gfs2_dinode_out(dip
, bh
->b_data
);
1721 mark_inode_dirty(&dip
->i_inode
);
1727 * gfs2_dir_mvino - Change inode number of directory entry
1728 * @dip: The GFS2 inode
1732 * This routine changes the inode number of a directory entry. It's used
1733 * by rename to change ".." when a directory is moved.
1734 * Assumes a glock is held on dvp.
1739 int gfs2_dir_mvino(struct gfs2_inode
*dip
, const struct qstr
*filename
,
1740 const struct gfs2_inode
*nip
, unsigned int new_type
)
1742 struct buffer_head
*bh
;
1743 struct gfs2_dirent
*dent
;
1746 dent
= gfs2_dirent_search(&dip
->i_inode
, filename
, gfs2_dirent_find
, &bh
);
1748 gfs2_consist_inode(dip
);
1752 return PTR_ERR(dent
);
1754 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1755 gfs2_inum_out(nip
, dent
);
1756 dent
->de_type
= cpu_to_be16(new_type
);
1758 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1760 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1763 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1766 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1767 gfs2_dinode_out(dip
, bh
->b_data
);
1773 * leaf_dealloc - Deallocate a directory leaf
1774 * @dip: the directory
1775 * @index: the hash table offset in the directory
1776 * @len: the number of pointers to this leaf
1777 * @leaf_no: the leaf number
1778 * @leaf_bh: buffer_head for the starting leaf
1779 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1784 static int leaf_dealloc(struct gfs2_inode
*dip
, u32 index
, u32 len
,
1785 u64 leaf_no
, struct buffer_head
*leaf_bh
,
1788 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1789 struct gfs2_leaf
*tmp_leaf
;
1790 struct gfs2_rgrp_list rlist
;
1791 struct buffer_head
*bh
, *dibh
;
1793 unsigned int rg_blocks
= 0, l_blocks
= 0;
1795 unsigned int x
, size
= len
* sizeof(u64
);
1798 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1800 ht
= kzalloc(size
, GFP_NOFS
);
1804 if (!gfs2_alloc_get(dip
)) {
1809 error
= gfs2_quota_hold(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1813 error
= gfs2_rindex_hold(sdp
, &dip
->i_alloc
->al_ri_gh
);
1817 /* Count the number of leaves */
1820 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1821 if (blk
!= leaf_no
) {
1822 error
= get_leaf(dip
, blk
, &bh
);
1826 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1827 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1831 gfs2_rlist_add(sdp
, &rlist
, blk
);
1835 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
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_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
;
1855 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1856 if (blk
!= leaf_no
) {
1857 error
= get_leaf(dip
, blk
, &bh
);
1861 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1862 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1866 gfs2_free_meta(dip
, blk
, 1);
1867 gfs2_add_inode_blocks(&dip
->i_inode
, -1);
1870 error
= gfs2_dir_write_data(dip
, ht
, index
* sizeof(u64
), size
);
1871 if (error
!= size
) {
1877 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1881 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1882 /* On the last dealloc, make this a regular file in case we crash.
1883 (We don't want to free these blocks a second time.) */
1885 dip
->i_inode
.i_mode
= S_IFREG
;
1886 gfs2_dinode_out(dip
, dibh
->b_data
);
1890 gfs2_trans_end(sdp
);
1892 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1894 gfs2_rlist_free(&rlist
);
1895 gfs2_glock_dq_uninit(&dip
->i_alloc
->al_ri_gh
);
1897 gfs2_quota_unhold(dip
);
1899 gfs2_alloc_put(dip
);
1906 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1907 * @dip: the directory
1909 * Dealloc all on-disk directory leaves to FREEMETA state
1910 * Change on-disk inode type to "regular file"
1915 int gfs2_dir_exhash_dealloc(struct gfs2_inode
*dip
)
1917 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1918 struct buffer_head
*bh
;
1919 struct gfs2_leaf
*leaf
;
1921 u32 ht_offset
, lp_offset
, ht_offset_cur
= -1;
1922 u32 index
= 0, next_index
;
1925 int error
= 0, last
;
1927 hsize
= 1 << dip
->i_depth
;
1928 if (hsize
* sizeof(u64
) != i_size_read(&dip
->i_inode
)) {
1929 gfs2_consist_inode(dip
);
1933 lp
= kmalloc(sdp
->sd_hash_bsize
, GFP_NOFS
);
1937 while (index
< hsize
) {
1938 lp_offset
= index
& (sdp
->sd_hash_ptrs
- 1);
1939 ht_offset
= index
- lp_offset
;
1941 if (ht_offset_cur
!= ht_offset
) {
1942 error
= gfs2_dir_read_data(dip
, (char *)lp
,
1943 ht_offset
* sizeof(__be64
),
1944 sdp
->sd_hash_bsize
, 1);
1945 if (error
!= sdp
->sd_hash_bsize
) {
1950 ht_offset_cur
= ht_offset
;
1953 leaf_no
= be64_to_cpu(lp
[lp_offset
]);
1955 error
= get_leaf(dip
, leaf_no
, &bh
);
1958 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1959 len
= 1 << (dip
->i_depth
- be16_to_cpu(leaf
->lf_depth
));
1961 next_index
= (index
& ~(len
- 1)) + len
;
1962 last
= ((next_index
>= hsize
) ? 1 : 0);
1963 error
= leaf_dealloc(dip
, index
, len
, leaf_no
, bh
,
1973 if (index
!= hsize
) {
1974 gfs2_consist_inode(dip
);
1985 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1986 * @ip: the file being written to
1987 * @filname: the filename that's going to be added
1989 * Returns: 1 if alloc required, 0 if not, -ve on error
1992 int gfs2_diradd_alloc_required(struct inode
*inode
, const struct qstr
*name
)
1994 struct gfs2_dirent
*dent
;
1995 struct buffer_head
*bh
;
1997 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
, &bh
);
2002 return PTR_ERR(dent
);