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 (*leaf_call_t
) (struct gfs2_inode
*dip
, u32 index
, u32 len
,
86 u64 leaf_no
, void *data
);
87 typedef int (*gfs2_dscan_t
)(const struct gfs2_dirent
*dent
,
88 const struct qstr
*name
, void *opaque
);
91 int gfs2_dir_get_new_buffer(struct gfs2_inode
*ip
, u64 block
,
92 struct buffer_head
**bhp
)
94 struct buffer_head
*bh
;
96 bh
= gfs2_meta_new(ip
->i_gl
, block
);
97 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
98 gfs2_metatype_set(bh
, GFS2_METATYPE_JD
, GFS2_FORMAT_JD
);
99 gfs2_buffer_clear_tail(bh
, sizeof(struct gfs2_meta_header
));
104 static int gfs2_dir_get_existing_buffer(struct gfs2_inode
*ip
, u64 block
,
105 struct buffer_head
**bhp
)
107 struct buffer_head
*bh
;
110 error
= gfs2_meta_read(ip
->i_gl
, block
, DIO_WAIT
, &bh
);
113 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_JD
)) {
121 static int gfs2_dir_write_stuffed(struct gfs2_inode
*ip
, const char *buf
,
122 unsigned int offset
, unsigned int size
)
124 struct buffer_head
*dibh
;
127 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
131 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
132 memcpy(dibh
->b_data
+ offset
+ sizeof(struct gfs2_dinode
), buf
, size
);
133 if (ip
->i_inode
.i_size
< offset
+ size
)
134 i_size_write(&ip
->i_inode
, offset
+ size
);
135 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
136 gfs2_dinode_out(ip
, dibh
->b_data
);
146 * gfs2_dir_write_data - Write directory information to the inode
147 * @ip: The GFS2 inode
148 * @buf: The buffer containing information to be written
149 * @offset: The file offset to start writing at
150 * @size: The amount of data to write
152 * Returns: The number of bytes correctly written or error code
154 static int gfs2_dir_write_data(struct gfs2_inode
*ip
, const char *buf
,
155 u64 offset
, unsigned int size
)
157 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
158 struct buffer_head
*dibh
;
169 if (gfs2_is_stuffed(ip
) &&
170 offset
+ size
<= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
171 return gfs2_dir_write_stuffed(ip
, buf
, (unsigned int)offset
,
174 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
177 if (gfs2_is_stuffed(ip
)) {
178 error
= gfs2_unstuff_dinode(ip
, NULL
);
184 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
186 while (copied
< size
) {
188 struct buffer_head
*bh
;
190 amount
= size
- copied
;
191 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
192 amount
= sdp
->sd_sb
.sb_bsize
- o
;
196 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
201 if (gfs2_assert_withdraw(sdp
, dblock
))
205 if (amount
== sdp
->sd_jbsize
|| new)
206 error
= gfs2_dir_get_new_buffer(ip
, dblock
, &bh
);
208 error
= gfs2_dir_get_existing_buffer(ip
, dblock
, &bh
);
213 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
214 memcpy(bh
->b_data
+ o
, buf
, amount
);
223 o
= sizeof(struct gfs2_meta_header
);
227 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
231 if (ip
->i_inode
.i_size
< offset
+ copied
)
232 i_size_write(&ip
->i_inode
, offset
+ copied
);
233 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
235 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
236 gfs2_dinode_out(ip
, dibh
->b_data
);
246 static int gfs2_dir_read_stuffed(struct gfs2_inode
*ip
, char *buf
,
247 u64 offset
, unsigned int size
)
249 struct buffer_head
*dibh
;
252 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
254 offset
+= sizeof(struct gfs2_dinode
);
255 memcpy(buf
, dibh
->b_data
+ offset
, size
);
259 return (error
) ? error
: size
;
264 * gfs2_dir_read_data - Read a data from a directory inode
265 * @ip: The GFS2 Inode
266 * @buf: The buffer to place result into
267 * @offset: File offset to begin jdata_readng from
268 * @size: Amount of data to transfer
270 * Returns: The amount of data actually copied or the error
272 static int gfs2_dir_read_data(struct gfs2_inode
*ip
, char *buf
, u64 offset
,
273 unsigned int size
, unsigned ra
)
275 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
281 u64 disksize
= i_size_read(&ip
->i_inode
);
283 if (offset
>= disksize
)
286 if (offset
+ size
> disksize
)
287 size
= disksize
- offset
;
292 if (gfs2_is_stuffed(ip
))
293 return gfs2_dir_read_stuffed(ip
, buf
, offset
, size
);
295 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
299 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
301 while (copied
< size
) {
303 struct buffer_head
*bh
;
306 amount
= size
- copied
;
307 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
308 amount
= sdp
->sd_sb
.sb_bsize
- o
;
312 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
314 if (error
|| !dblock
)
319 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
321 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_WAIT
, &bh
);
325 error
= gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_JD
);
332 memcpy(buf
, bh
->b_data
+ o
, amount
);
337 o
= sizeof(struct gfs2_meta_header
);
342 return (copied
) ? copied
: error
;
345 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent
*dent
)
347 return dent
->de_inum
.no_addr
== 0 || dent
->de_inum
.no_formal_ino
== 0;
350 static inline int __gfs2_dirent_find(const struct gfs2_dirent
*dent
,
351 const struct qstr
*name
, int ret
)
353 if (!gfs2_dirent_sentinel(dent
) &&
354 be32_to_cpu(dent
->de_hash
) == name
->hash
&&
355 be16_to_cpu(dent
->de_name_len
) == name
->len
&&
356 memcmp(dent
+1, name
->name
, name
->len
) == 0)
361 static int gfs2_dirent_find(const struct gfs2_dirent
*dent
,
362 const struct qstr
*name
,
365 return __gfs2_dirent_find(dent
, name
, 1);
368 static int gfs2_dirent_prev(const struct gfs2_dirent
*dent
,
369 const struct qstr
*name
,
372 return __gfs2_dirent_find(dent
, name
, 2);
376 * name->name holds ptr to start of block.
377 * name->len holds size of block.
379 static int gfs2_dirent_last(const struct gfs2_dirent
*dent
,
380 const struct qstr
*name
,
383 const char *start
= name
->name
;
384 const char *end
= (const char *)dent
+ be16_to_cpu(dent
->de_rec_len
);
385 if (name
->len
== (end
- start
))
390 static int gfs2_dirent_find_space(const struct gfs2_dirent
*dent
,
391 const struct qstr
*name
,
394 unsigned required
= GFS2_DIRENT_SIZE(name
->len
);
395 unsigned actual
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
396 unsigned totlen
= be16_to_cpu(dent
->de_rec_len
);
398 if (gfs2_dirent_sentinel(dent
))
400 if (totlen
- actual
>= required
)
405 struct dirent_gather
{
406 const struct gfs2_dirent
**pdent
;
410 static int gfs2_dirent_gather(const struct gfs2_dirent
*dent
,
411 const struct qstr
*name
,
414 struct dirent_gather
*g
= opaque
;
415 if (!gfs2_dirent_sentinel(dent
)) {
416 g
->pdent
[g
->offset
++] = dent
;
422 * Other possible things to check:
423 * - Inode located within filesystem size (and on valid block)
424 * - Valid directory entry type
425 * Not sure how heavy-weight we want to make this... could also check
426 * hash is correct for example, but that would take a lot of extra time.
427 * For now the most important thing is to check that the various sizes
430 static int gfs2_check_dirent(struct gfs2_dirent
*dent
, unsigned int offset
,
431 unsigned int size
, unsigned int len
, int first
)
433 const char *msg
= "gfs2_dirent too small";
434 if (unlikely(size
< sizeof(struct gfs2_dirent
)))
436 msg
= "gfs2_dirent misaligned";
437 if (unlikely(offset
& 0x7))
439 msg
= "gfs2_dirent points beyond end of block";
440 if (unlikely(offset
+ size
> len
))
442 msg
= "zero inode number";
443 if (unlikely(!first
&& gfs2_dirent_sentinel(dent
)))
445 msg
= "name length is greater than space in dirent";
446 if (!gfs2_dirent_sentinel(dent
) &&
447 unlikely(sizeof(struct gfs2_dirent
)+be16_to_cpu(dent
->de_name_len
) >
452 printk(KERN_WARNING
"gfs2_check_dirent: %s (%s)\n", msg
,
453 first
? "first in block" : "not first in block");
457 static int gfs2_dirent_offset(const void *buf
)
459 const struct gfs2_meta_header
*h
= buf
;
464 switch(be32_to_cpu(h
->mh_type
)) {
465 case GFS2_METATYPE_LF
:
466 offset
= sizeof(struct gfs2_leaf
);
468 case GFS2_METATYPE_DI
:
469 offset
= sizeof(struct gfs2_dinode
);
476 printk(KERN_WARNING
"gfs2_scan_dirent: wrong block type %u\n",
477 be32_to_cpu(h
->mh_type
));
481 static struct gfs2_dirent
*gfs2_dirent_scan(struct inode
*inode
, void *buf
,
482 unsigned int len
, gfs2_dscan_t scan
,
483 const struct qstr
*name
,
486 struct gfs2_dirent
*dent
, *prev
;
491 ret
= gfs2_dirent_offset(buf
);
498 size
= be16_to_cpu(dent
->de_rec_len
);
499 if (gfs2_check_dirent(dent
, offset
, size
, len
, 1))
502 ret
= scan(dent
, name
, opaque
);
510 size
= be16_to_cpu(dent
->de_rec_len
);
511 if (gfs2_check_dirent(dent
, offset
, size
, len
, 0))
521 return prev
? prev
: dent
;
528 gfs2_consist_inode(GFS2_I(inode
));
529 return ERR_PTR(-EIO
);
532 static int dirent_check_reclen(struct gfs2_inode
*dip
,
533 const struct gfs2_dirent
*d
, const void *end_p
)
536 u16 rec_len
= be16_to_cpu(d
->de_rec_len
);
538 if (unlikely(rec_len
< sizeof(struct gfs2_dirent
)))
546 gfs2_consist_inode(dip
);
551 * dirent_next - Next dirent
552 * @dip: the directory
554 * @dent: Pointer to list of dirents
556 * Returns: 0 on success, error code otherwise
559 static int dirent_next(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
560 struct gfs2_dirent
**dent
)
562 struct gfs2_dirent
*cur
= *dent
, *tmp
;
563 char *bh_end
= bh
->b_data
+ bh
->b_size
;
566 ret
= dirent_check_reclen(dip
, cur
, bh_end
);
570 tmp
= (void *)cur
+ ret
;
571 ret
= dirent_check_reclen(dip
, tmp
, bh_end
);
575 /* Only the first dent could ever have de_inum.no_addr == 0 */
576 if (gfs2_dirent_sentinel(tmp
)) {
577 gfs2_consist_inode(dip
);
586 * dirent_del - Delete a dirent
587 * @dip: The GFS2 inode
589 * @prev: The previous dirent
590 * @cur: The current dirent
594 static void dirent_del(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
595 struct gfs2_dirent
*prev
, struct gfs2_dirent
*cur
)
597 u16 cur_rec_len
, prev_rec_len
;
599 if (gfs2_dirent_sentinel(cur
)) {
600 gfs2_consist_inode(dip
);
604 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
606 /* If there is no prev entry, this is the first entry in the block.
607 The de_rec_len is already as big as it needs to be. Just zero
608 out the inode number and return. */
611 cur
->de_inum
.no_addr
= 0;
612 cur
->de_inum
.no_formal_ino
= 0;
616 /* Combine this dentry with the previous one. */
618 prev_rec_len
= be16_to_cpu(prev
->de_rec_len
);
619 cur_rec_len
= be16_to_cpu(cur
->de_rec_len
);
621 if ((char *)prev
+ prev_rec_len
!= (char *)cur
)
622 gfs2_consist_inode(dip
);
623 if ((char *)cur
+ cur_rec_len
> bh
->b_data
+ bh
->b_size
)
624 gfs2_consist_inode(dip
);
626 prev_rec_len
+= cur_rec_len
;
627 prev
->de_rec_len
= cpu_to_be16(prev_rec_len
);
631 * Takes a dent from which to grab space as an argument. Returns the
632 * newly created dent.
634 static struct gfs2_dirent
*gfs2_init_dirent(struct inode
*inode
,
635 struct gfs2_dirent
*dent
,
636 const struct qstr
*name
,
637 struct buffer_head
*bh
)
639 struct gfs2_inode
*ip
= GFS2_I(inode
);
640 struct gfs2_dirent
*ndent
;
641 unsigned offset
= 0, totlen
;
643 if (!gfs2_dirent_sentinel(dent
))
644 offset
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
645 totlen
= be16_to_cpu(dent
->de_rec_len
);
646 BUG_ON(offset
+ name
->len
> totlen
);
647 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
648 ndent
= (struct gfs2_dirent
*)((char *)dent
+ offset
);
649 dent
->de_rec_len
= cpu_to_be16(offset
);
650 gfs2_qstr2dirent(name
, totlen
- offset
, ndent
);
654 static struct gfs2_dirent
*gfs2_dirent_alloc(struct inode
*inode
,
655 struct buffer_head
*bh
,
656 const struct qstr
*name
)
658 struct gfs2_dirent
*dent
;
659 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
660 gfs2_dirent_find_space
, name
, NULL
);
661 if (!dent
|| IS_ERR(dent
))
663 return gfs2_init_dirent(inode
, dent
, name
, bh
);
666 static int get_leaf(struct gfs2_inode
*dip
, u64 leaf_no
,
667 struct buffer_head
**bhp
)
671 error
= gfs2_meta_read(dip
->i_gl
, leaf_no
, DIO_WAIT
, bhp
);
672 if (!error
&& gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), *bhp
, GFS2_METATYPE_LF
)) {
673 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
681 * get_leaf_nr - Get a leaf number associated with the index
682 * @dip: The GFS2 inode
686 * Returns: 0 on success, error code otherwise
689 static int get_leaf_nr(struct gfs2_inode
*dip
, u32 index
,
695 error
= gfs2_dir_read_data(dip
, (char *)&leaf_no
,
696 index
* sizeof(__be64
),
698 if (error
!= sizeof(u64
))
699 return (error
< 0) ? error
: -EIO
;
701 *leaf_out
= be64_to_cpu(leaf_no
);
706 static int get_first_leaf(struct gfs2_inode
*dip
, u32 index
,
707 struct buffer_head
**bh_out
)
712 error
= get_leaf_nr(dip
, index
, &leaf_no
);
714 error
= get_leaf(dip
, leaf_no
, bh_out
);
719 static struct gfs2_dirent
*gfs2_dirent_search(struct inode
*inode
,
720 const struct qstr
*name
,
722 struct buffer_head
**pbh
)
724 struct buffer_head
*bh
;
725 struct gfs2_dirent
*dent
;
726 struct gfs2_inode
*ip
= GFS2_I(inode
);
729 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
730 struct gfs2_leaf
*leaf
;
731 unsigned hsize
= 1 << ip
->i_depth
;
734 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
735 gfs2_consist_inode(ip
);
736 return ERR_PTR(-EIO
);
739 index
= name
->hash
>> (32 - ip
->i_depth
);
740 error
= get_first_leaf(ip
, index
, &bh
);
742 return ERR_PTR(error
);
744 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
748 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
749 ln
= be64_to_cpu(leaf
->lf_next
);
754 error
= get_leaf(ip
, ln
, &bh
);
757 return error
? ERR_PTR(error
) : NULL
;
761 error
= gfs2_meta_inode_buffer(ip
, &bh
);
763 return ERR_PTR(error
);
764 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
, scan
, name
, NULL
);
766 if (unlikely(dent
== NULL
|| IS_ERR(dent
))) {
774 static struct gfs2_leaf
*new_leaf(struct inode
*inode
, struct buffer_head
**pbh
, u16 depth
)
776 struct gfs2_inode
*ip
= GFS2_I(inode
);
780 struct buffer_head
*bh
;
781 struct gfs2_leaf
*leaf
;
782 struct gfs2_dirent
*dent
;
783 struct qstr name
= { .name
= "", .len
= 0, .hash
= 0 };
785 error
= gfs2_alloc_block(ip
, &bn
, &n
);
788 bh
= gfs2_meta_new(ip
->i_gl
, bn
);
792 gfs2_trans_add_unrevoke(GFS2_SB(inode
), bn
, 1);
793 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
794 gfs2_metatype_set(bh
, GFS2_METATYPE_LF
, GFS2_FORMAT_LF
);
795 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
796 leaf
->lf_depth
= cpu_to_be16(depth
);
797 leaf
->lf_entries
= 0;
798 leaf
->lf_dirent_format
= cpu_to_be32(GFS2_FORMAT_DE
);
800 memset(leaf
->lf_reserved
, 0, sizeof(leaf
->lf_reserved
));
801 dent
= (struct gfs2_dirent
*)(leaf
+1);
802 gfs2_qstr2dirent(&name
, bh
->b_size
- sizeof(struct gfs2_leaf
), dent
);
808 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
809 * @dip: The GFS2 inode
811 * Returns: 0 on success, error code otherwise
814 static int dir_make_exhash(struct inode
*inode
)
816 struct gfs2_inode
*dip
= GFS2_I(inode
);
817 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
818 struct gfs2_dirent
*dent
;
820 struct buffer_head
*bh
, *dibh
;
821 struct gfs2_leaf
*leaf
;
828 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
832 /* Turn over a new leaf */
834 leaf
= new_leaf(inode
, &bh
, 0);
839 gfs2_assert(sdp
, dip
->i_entries
< (1 << 16));
840 leaf
->lf_entries
= cpu_to_be16(dip
->i_entries
);
844 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_leaf
), dibh
,
845 sizeof(struct gfs2_dinode
));
847 /* Find last entry */
850 args
.len
= bh
->b_size
- sizeof(struct gfs2_dinode
) +
851 sizeof(struct gfs2_leaf
);
852 args
.name
= bh
->b_data
;
853 dent
= gfs2_dirent_scan(&dip
->i_inode
, bh
->b_data
, bh
->b_size
,
854 gfs2_dirent_last
, &args
, NULL
);
863 return PTR_ERR(dent
);
866 /* Adjust the last dirent's record length
867 (Remember that dent still points to the last entry.) */
869 dent
->de_rec_len
= cpu_to_be16(be16_to_cpu(dent
->de_rec_len
) +
870 sizeof(struct gfs2_dinode
) -
871 sizeof(struct gfs2_leaf
));
875 /* We're done with the new leaf block, now setup the new
878 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
879 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
881 lp
= (__be64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
883 for (x
= sdp
->sd_hash_ptrs
; x
--; lp
++)
884 *lp
= cpu_to_be64(bn
);
886 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
/ 2);
887 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
888 dip
->i_diskflags
|= GFS2_DIF_EXHASH
;
890 for (x
= sdp
->sd_hash_ptrs
, y
= -1; x
; x
>>= 1, y
++) ;
893 gfs2_dinode_out(dip
, dibh
->b_data
);
901 * dir_split_leaf - Split a leaf block into two
902 * @dip: The GFS2 inode
906 * Returns: 0 on success, error code on failure
909 static int dir_split_leaf(struct inode
*inode
, const struct qstr
*name
)
911 struct gfs2_inode
*dip
= GFS2_I(inode
);
912 struct buffer_head
*nbh
, *obh
, *dibh
;
913 struct gfs2_leaf
*nleaf
, *oleaf
;
914 struct gfs2_dirent
*dent
= NULL
, *prev
= NULL
, *next
= NULL
, *new;
915 u32 start
, len
, half_len
, divider
;
922 index
= name
->hash
>> (32 - dip
->i_depth
);
923 error
= get_leaf_nr(dip
, index
, &leaf_no
);
927 /* Get the old leaf block */
928 error
= get_leaf(dip
, leaf_no
, &obh
);
932 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
933 if (dip
->i_depth
== be16_to_cpu(oleaf
->lf_depth
)) {
935 return 1; /* can't split */
938 gfs2_trans_add_bh(dip
->i_gl
, obh
, 1);
940 nleaf
= new_leaf(inode
, &nbh
, be16_to_cpu(oleaf
->lf_depth
) + 1);
947 /* Compute the start and len of leaf pointers in the hash table. */
948 len
= 1 << (dip
->i_depth
- be16_to_cpu(oleaf
->lf_depth
));
951 printk(KERN_WARNING
"i_depth %u lf_depth %u index %u\n", dip
->i_depth
, be16_to_cpu(oleaf
->lf_depth
), index
);
952 gfs2_consist_inode(dip
);
957 start
= (index
& ~(len
- 1));
959 /* Change the pointers.
960 Don't bother distinguishing stuffed from non-stuffed.
961 This code is complicated enough already. */
962 lp
= kmalloc(half_len
* sizeof(__be64
), GFP_NOFS
);
968 /* Change the pointers */
969 for (x
= 0; x
< half_len
; x
++)
970 lp
[x
] = cpu_to_be64(bn
);
972 error
= gfs2_dir_write_data(dip
, (char *)lp
, start
* sizeof(u64
),
973 half_len
* sizeof(u64
));
974 if (error
!= half_len
* sizeof(u64
)) {
982 /* Compute the divider */
983 divider
= (start
+ half_len
) << (32 - dip
->i_depth
);
985 /* Copy the entries */
986 dent
= (struct gfs2_dirent
*)(obh
->b_data
+ sizeof(struct gfs2_leaf
));
990 if (dirent_next(dip
, obh
, &next
))
993 if (!gfs2_dirent_sentinel(dent
) &&
994 be32_to_cpu(dent
->de_hash
) < divider
) {
996 str
.name
= (char*)(dent
+1);
997 str
.len
= be16_to_cpu(dent
->de_name_len
);
998 str
.hash
= be32_to_cpu(dent
->de_hash
);
999 new = gfs2_dirent_alloc(inode
, nbh
, &str
);
1001 error
= PTR_ERR(new);
1005 new->de_inum
= dent
->de_inum
; /* No endian worries */
1006 new->de_type
= dent
->de_type
; /* No endian worries */
1007 be16_add_cpu(&nleaf
->lf_entries
, 1);
1009 dirent_del(dip
, obh
, prev
, dent
);
1011 if (!oleaf
->lf_entries
)
1012 gfs2_consist_inode(dip
);
1013 be16_add_cpu(&oleaf
->lf_entries
, -1);
1025 oleaf
->lf_depth
= nleaf
->lf_depth
;
1027 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1028 if (!gfs2_assert_withdraw(GFS2_SB(&dip
->i_inode
), !error
)) {
1029 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1030 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
1031 gfs2_dinode_out(dip
, dibh
->b_data
);
1050 * dir_double_exhash - Double size of ExHash table
1051 * @dip: The GFS2 dinode
1053 * Returns: 0 on success, error code on failure
1056 static int dir_double_exhash(struct gfs2_inode
*dip
)
1058 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1059 struct buffer_head
*dibh
;
1064 u64 disksize
= i_size_read(&dip
->i_inode
);
1068 hsize
= 1 << dip
->i_depth
;
1069 if (hsize
* sizeof(u64
) != disksize
) {
1070 gfs2_consist_inode(dip
);
1074 /* Allocate both the "from" and "to" buffers in one big chunk */
1076 buf
= kcalloc(3, sdp
->sd_hash_bsize
, GFP_NOFS
);
1080 for (block
= disksize
>> sdp
->sd_hash_bsize_shift
; block
--;) {
1081 error
= gfs2_dir_read_data(dip
, (char *)buf
,
1082 block
* sdp
->sd_hash_bsize
,
1083 sdp
->sd_hash_bsize
, 1);
1084 if (error
!= sdp
->sd_hash_bsize
) {
1091 to
= (u64
*)((char *)buf
+ sdp
->sd_hash_bsize
);
1093 for (x
= sdp
->sd_hash_ptrs
; x
--; from
++) {
1094 *to
++ = *from
; /* No endianess worries */
1098 error
= gfs2_dir_write_data(dip
,
1099 (char *)buf
+ sdp
->sd_hash_bsize
,
1100 block
* sdp
->sd_sb
.sb_bsize
,
1101 sdp
->sd_sb
.sb_bsize
);
1102 if (error
!= sdp
->sd_sb
.sb_bsize
) {
1111 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1112 if (!gfs2_assert_withdraw(sdp
, !error
)) {
1114 gfs2_dinode_out(dip
, dibh
->b_data
);
1126 * compare_dents - compare directory entries by hash value
1130 * When comparing the hash entries of @a to @b:
1136 static int compare_dents(const void *a
, const void *b
)
1138 const struct gfs2_dirent
*dent_a
, *dent_b
;
1142 dent_a
= *(const struct gfs2_dirent
**)a
;
1143 hash_a
= be32_to_cpu(dent_a
->de_hash
);
1145 dent_b
= *(const struct gfs2_dirent
**)b
;
1146 hash_b
= be32_to_cpu(dent_b
->de_hash
);
1148 if (hash_a
> hash_b
)
1150 else if (hash_a
< hash_b
)
1153 unsigned int len_a
= be16_to_cpu(dent_a
->de_name_len
);
1154 unsigned int len_b
= be16_to_cpu(dent_b
->de_name_len
);
1158 else if (len_a
< len_b
)
1161 ret
= memcmp(dent_a
+ 1, dent_b
+ 1, len_a
);
1168 * do_filldir_main - read out directory entries
1169 * @dip: The GFS2 inode
1170 * @offset: The offset in the file to read from
1171 * @opaque: opaque data to pass to filldir
1172 * @filldir: The function to pass entries to
1173 * @darr: an array of struct gfs2_dirent pointers to read
1174 * @entries: the number of entries in darr
1175 * @copied: pointer to int that's non-zero if a entry has been copied out
1177 * Jump through some hoops to make sure that if there are hash collsions,
1178 * they are read out at the beginning of a buffer. We want to minimize
1179 * the possibility that they will fall into different readdir buffers or
1180 * that someone will want to seek to that location.
1182 * Returns: errno, >0 on exception from filldir
1185 static int do_filldir_main(struct gfs2_inode
*dip
, u64
*offset
,
1186 void *opaque
, filldir_t filldir
,
1187 const struct gfs2_dirent
**darr
, u32 entries
,
1190 const struct gfs2_dirent
*dent
, *dent_next
;
1196 sort(darr
, entries
, sizeof(struct gfs2_dirent
*), compare_dents
, NULL
);
1198 dent_next
= darr
[0];
1199 off_next
= be32_to_cpu(dent_next
->de_hash
);
1200 off_next
= gfs2_disk_hash2offset(off_next
);
1202 for (x
= 0, y
= 1; x
< entries
; x
++, y
++) {
1207 dent_next
= darr
[y
];
1208 off_next
= be32_to_cpu(dent_next
->de_hash
);
1209 off_next
= gfs2_disk_hash2offset(off_next
);
1215 if (off_next
== off
) {
1216 if (*copied
&& !run
)
1227 error
= filldir(opaque
, (const char *)(dent
+ 1),
1228 be16_to_cpu(dent
->de_name_len
),
1229 off
, be64_to_cpu(dent
->de_inum
.no_addr
),
1230 be16_to_cpu(dent
->de_type
));
1237 /* Increment the *offset by one, so the next time we come into the
1238 do_filldir fxn, we get the next entry instead of the last one in the
1246 static void *gfs2_alloc_sort_buffer(unsigned size
)
1250 if (size
< KMALLOC_MAX_SIZE
)
1251 ptr
= kmalloc(size
, GFP_NOFS
| __GFP_NOWARN
);
1253 ptr
= __vmalloc(size
, GFP_NOFS
, PAGE_KERNEL
);
1257 static void gfs2_free_sort_buffer(void *ptr
)
1259 if (is_vmalloc_addr(ptr
))
1265 static int gfs2_dir_read_leaf(struct inode
*inode
, u64
*offset
, void *opaque
,
1266 filldir_t filldir
, int *copied
, unsigned *depth
,
1269 struct gfs2_inode
*ip
= GFS2_I(inode
);
1270 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1271 struct buffer_head
*bh
;
1272 struct gfs2_leaf
*lf
;
1273 unsigned entries
= 0, entries2
= 0;
1274 unsigned leaves
= 0;
1275 const struct gfs2_dirent
**darr
, *dent
;
1276 struct dirent_gather g
;
1277 struct buffer_head
**larr
;
1283 error
= get_leaf(ip
, lfn
, &bh
);
1286 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1288 *depth
= be16_to_cpu(lf
->lf_depth
);
1289 entries
+= be16_to_cpu(lf
->lf_entries
);
1291 lfn
= be64_to_cpu(lf
->lf_next
);
1300 * The extra 99 entries are not normally used, but are a buffer
1301 * zone in case the number of entries in the leaf is corrupt.
1302 * 99 is the maximum number of entries that can fit in a single
1305 larr
= gfs2_alloc_sort_buffer((leaves
+ entries
+ 99) * sizeof(void *));
1308 darr
= (const struct gfs2_dirent
**)(larr
+ leaves
);
1314 error
= get_leaf(ip
, lfn
, &bh
);
1317 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1318 lfn
= be64_to_cpu(lf
->lf_next
);
1319 if (lf
->lf_entries
) {
1320 entries2
+= be16_to_cpu(lf
->lf_entries
);
1321 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
1322 gfs2_dirent_gather
, NULL
, &g
);
1323 error
= PTR_ERR(dent
);
1326 if (entries2
!= g
.offset
) {
1327 fs_warn(sdp
, "Number of entries corrupt in dir "
1328 "leaf %llu, entries2 (%u) != "
1330 (unsigned long long)bh
->b_blocknr
,
1331 entries2
, g
.offset
);
1343 BUG_ON(entries2
!= entries
);
1344 error
= do_filldir_main(ip
, offset
, opaque
, filldir
, darr
,
1347 for(i
= 0; i
< leaf
; i
++)
1349 gfs2_free_sort_buffer(larr
);
1355 * dir_e_read - Reads the entries from a directory into a filldir buffer
1356 * @dip: dinode pointer
1357 * @offset: the hash of the last entry read shifted to the right once
1358 * @opaque: buffer for the filldir function to fill
1359 * @filldir: points to the filldir function to use
1364 static int dir_e_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1367 struct gfs2_inode
*dip
= GFS2_I(inode
);
1368 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1370 u32 ht_offset
, lp_offset
, ht_offset_cur
= -1;
1377 hsize
= 1 << dip
->i_depth
;
1378 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
1379 gfs2_consist_inode(dip
);
1383 hash
= gfs2_dir_offset2hash(*offset
);
1384 index
= hash
>> (32 - dip
->i_depth
);
1386 lp
= kmalloc(sdp
->sd_hash_bsize
, GFP_NOFS
);
1390 while (index
< hsize
) {
1391 lp_offset
= index
& (sdp
->sd_hash_ptrs
- 1);
1392 ht_offset
= index
- lp_offset
;
1394 if (ht_offset_cur
!= ht_offset
) {
1395 error
= gfs2_dir_read_data(dip
, (char *)lp
,
1396 ht_offset
* sizeof(__be64
),
1397 sdp
->sd_hash_bsize
, 1);
1398 if (error
!= sdp
->sd_hash_bsize
) {
1403 ht_offset_cur
= ht_offset
;
1406 error
= gfs2_dir_read_leaf(inode
, offset
, opaque
, filldir
,
1408 be64_to_cpu(lp
[lp_offset
]));
1412 len
= 1 << (dip
->i_depth
- depth
);
1413 index
= (index
& ~(len
- 1)) + len
;
1423 int gfs2_dir_read(struct inode
*inode
, u64
*offset
, void *opaque
,
1426 struct gfs2_inode
*dip
= GFS2_I(inode
);
1427 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1428 struct dirent_gather g
;
1429 const struct gfs2_dirent
**darr
, *dent
;
1430 struct buffer_head
*dibh
;
1434 if (!dip
->i_entries
)
1437 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
)
1438 return dir_e_read(inode
, offset
, opaque
, filldir
);
1440 if (!gfs2_is_stuffed(dip
)) {
1441 gfs2_consist_inode(dip
);
1445 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1450 /* 96 is max number of dirents which can be stuffed into an inode */
1451 darr
= kmalloc(96 * sizeof(struct gfs2_dirent
*), GFP_NOFS
);
1455 dent
= gfs2_dirent_scan(inode
, dibh
->b_data
, dibh
->b_size
,
1456 gfs2_dirent_gather
, NULL
, &g
);
1458 error
= PTR_ERR(dent
);
1461 if (dip
->i_entries
!= g
.offset
) {
1462 fs_warn(sdp
, "Number of entries corrupt in dir %llu, "
1463 "ip->i_entries (%u) != g.offset (%u)\n",
1464 (unsigned long long)dip
->i_no_addr
,
1470 error
= do_filldir_main(dip
, offset
, opaque
, filldir
, darr
,
1471 dip
->i_entries
, &copied
);
1485 * gfs2_dir_search - Search a directory
1486 * @dip: The GFS2 inode
1490 * This routine searches a directory for a file or another directory.
1491 * Assumes a glock is held on dip.
1496 struct inode
*gfs2_dir_search(struct inode
*dir
, const struct qstr
*name
)
1498 struct buffer_head
*bh
;
1499 struct gfs2_dirent
*dent
;
1500 struct inode
*inode
;
1502 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1505 return ERR_CAST(dent
);
1506 inode
= gfs2_inode_lookup(dir
->i_sb
,
1507 be16_to_cpu(dent
->de_type
),
1508 be64_to_cpu(dent
->de_inum
.no_addr
),
1509 be64_to_cpu(dent
->de_inum
.no_formal_ino
));
1513 return ERR_PTR(-ENOENT
);
1516 int gfs2_dir_check(struct inode
*dir
, const struct qstr
*name
,
1517 const struct gfs2_inode
*ip
)
1519 struct buffer_head
*bh
;
1520 struct gfs2_dirent
*dent
;
1523 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1526 return PTR_ERR(dent
);
1528 if (be64_to_cpu(dent
->de_inum
.no_addr
) != ip
->i_no_addr
)
1530 if (be64_to_cpu(dent
->de_inum
.no_formal_ino
) !=
1531 ip
->i_no_formal_ino
)
1533 if (unlikely(IF2DT(ip
->i_inode
.i_mode
) !=
1534 be16_to_cpu(dent
->de_type
))) {
1535 gfs2_consist_inode(GFS2_I(dir
));
1547 static int dir_new_leaf(struct inode
*inode
, const struct qstr
*name
)
1549 struct buffer_head
*bh
, *obh
;
1550 struct gfs2_inode
*ip
= GFS2_I(inode
);
1551 struct gfs2_leaf
*leaf
, *oleaf
;
1556 index
= name
->hash
>> (32 - ip
->i_depth
);
1557 error
= get_first_leaf(ip
, index
, &obh
);
1561 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
1562 bn
= be64_to_cpu(oleaf
->lf_next
);
1566 error
= get_leaf(ip
, bn
, &obh
);
1571 gfs2_trans_add_bh(ip
->i_gl
, obh
, 1);
1573 leaf
= new_leaf(inode
, &bh
, be16_to_cpu(oleaf
->lf_depth
));
1578 oleaf
->lf_next
= cpu_to_be64(bh
->b_blocknr
);
1582 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1585 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1586 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1587 gfs2_dinode_out(ip
, bh
->b_data
);
1593 * gfs2_dir_add - Add new filename into directory
1594 * @dip: The GFS2 inode
1595 * @filename: The new name
1596 * @inode: The inode number of the entry
1597 * @type: The type of the entry
1599 * Returns: 0 on success, error code on failure
1602 int gfs2_dir_add(struct inode
*inode
, const struct qstr
*name
,
1603 const struct gfs2_inode
*nip
, unsigned type
)
1605 struct gfs2_inode
*ip
= GFS2_I(inode
);
1606 struct buffer_head
*bh
;
1607 struct gfs2_dirent
*dent
;
1608 struct gfs2_leaf
*leaf
;
1612 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
,
1616 return PTR_ERR(dent
);
1617 dent
= gfs2_init_dirent(inode
, dent
, name
, bh
);
1618 gfs2_inum_out(nip
, dent
);
1619 dent
->de_type
= cpu_to_be16(type
);
1620 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1621 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1622 be16_add_cpu(&leaf
->lf_entries
, 1);
1625 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1628 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
1630 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1631 gfs2_dinode_out(ip
, bh
->b_data
);
1636 if (!(ip
->i_diskflags
& GFS2_DIF_EXHASH
)) {
1637 error
= dir_make_exhash(inode
);
1642 error
= dir_split_leaf(inode
, name
);
1647 if (ip
->i_depth
< GFS2_DIR_MAX_DEPTH
) {
1648 error
= dir_double_exhash(ip
);
1651 error
= dir_split_leaf(inode
, name
);
1657 error
= dir_new_leaf(inode
, name
);
1668 * gfs2_dir_del - Delete a directory entry
1669 * @dip: The GFS2 inode
1670 * @filename: The filename
1672 * Returns: 0 on success, error code on failure
1675 int gfs2_dir_del(struct gfs2_inode
*dip
, const struct qstr
*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 gfs2_dinode_out(dip
, bh
->b_data
);
1719 mark_inode_dirty(&dip
->i_inode
);
1725 * gfs2_dir_mvino - Change inode number of directory entry
1726 * @dip: The GFS2 inode
1730 * This routine changes the inode number of a directory entry. It's used
1731 * by rename to change ".." when a directory is moved.
1732 * Assumes a glock is held on dvp.
1737 int gfs2_dir_mvino(struct gfs2_inode
*dip
, const struct qstr
*filename
,
1738 const struct gfs2_inode
*nip
, unsigned int new_type
)
1740 struct buffer_head
*bh
;
1741 struct gfs2_dirent
*dent
;
1744 dent
= gfs2_dirent_search(&dip
->i_inode
, filename
, gfs2_dirent_find
, &bh
);
1746 gfs2_consist_inode(dip
);
1750 return PTR_ERR(dent
);
1752 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1753 gfs2_inum_out(nip
, dent
);
1754 dent
->de_type
= cpu_to_be16(new_type
);
1756 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1758 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1761 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1764 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1765 gfs2_dinode_out(dip
, bh
->b_data
);
1771 * foreach_leaf - call a function for each leaf in a directory
1772 * @dip: the directory
1773 * @lc: the function to call for each each
1774 * @data: private data to pass to it
1779 static int foreach_leaf(struct gfs2_inode
*dip
, leaf_call_t lc
, void *data
)
1781 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1782 struct buffer_head
*bh
;
1783 struct gfs2_leaf
*leaf
;
1785 u32 ht_offset
, lp_offset
, ht_offset_cur
= -1;
1791 hsize
= 1 << dip
->i_depth
;
1792 if (hsize
* sizeof(u64
) != i_size_read(&dip
->i_inode
)) {
1793 gfs2_consist_inode(dip
);
1797 lp
= kmalloc(sdp
->sd_hash_bsize
, GFP_NOFS
);
1801 while (index
< hsize
) {
1802 lp_offset
= index
& (sdp
->sd_hash_ptrs
- 1);
1803 ht_offset
= index
- lp_offset
;
1805 if (ht_offset_cur
!= ht_offset
) {
1806 error
= gfs2_dir_read_data(dip
, (char *)lp
,
1807 ht_offset
* sizeof(__be64
),
1808 sdp
->sd_hash_bsize
, 1);
1809 if (error
!= sdp
->sd_hash_bsize
) {
1814 ht_offset_cur
= ht_offset
;
1817 leaf_no
= be64_to_cpu(lp
[lp_offset
]);
1819 error
= get_leaf(dip
, leaf_no
, &bh
);
1822 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1823 len
= 1 << (dip
->i_depth
- be16_to_cpu(leaf
->lf_depth
));
1826 error
= lc(dip
, index
, len
, leaf_no
, data
);
1830 index
= (index
& ~(len
- 1)) + len
;
1835 if (index
!= hsize
) {
1836 gfs2_consist_inode(dip
);
1847 * leaf_dealloc - Deallocate a directory leaf
1848 * @dip: the directory
1849 * @index: the hash table offset in the directory
1850 * @len: the number of pointers to this leaf
1851 * @leaf_no: the leaf number
1857 static int leaf_dealloc(struct gfs2_inode
*dip
, u32 index
, u32 len
,
1858 u64 leaf_no
, void *data
)
1860 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1861 struct gfs2_leaf
*tmp_leaf
;
1862 struct gfs2_rgrp_list rlist
;
1863 struct buffer_head
*bh
, *dibh
;
1865 unsigned int rg_blocks
= 0, l_blocks
= 0;
1867 unsigned int x
, size
= len
* sizeof(u64
);
1870 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1872 ht
= kzalloc(size
, GFP_NOFS
);
1876 if (!gfs2_alloc_get(dip
)) {
1881 error
= gfs2_quota_hold(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1885 error
= gfs2_rindex_hold(sdp
, &dip
->i_alloc
->al_ri_gh
);
1889 /* Count the number of leaves */
1891 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1892 error
= get_leaf(dip
, blk
, &bh
);
1895 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1896 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1899 gfs2_rlist_add(sdp
, &rlist
, blk
);
1903 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
1905 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1906 struct gfs2_rgrpd
*rgd
;
1907 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1908 rg_blocks
+= rgd
->rd_length
;
1911 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1915 error
= gfs2_trans_begin(sdp
,
1916 rg_blocks
+ (DIV_ROUND_UP(size
, sdp
->sd_jbsize
) + 1) +
1917 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, l_blocks
);
1919 goto out_rg_gunlock
;
1921 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1922 error
= get_leaf(dip
, blk
, &bh
);
1925 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1926 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1929 gfs2_free_meta(dip
, blk
, 1);
1930 gfs2_add_inode_blocks(&dip
->i_inode
, -1);
1933 error
= gfs2_dir_write_data(dip
, ht
, index
* sizeof(u64
), size
);
1934 if (error
!= size
) {
1940 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1944 gfs2_trans_add_bh(dip
->i_gl
, dibh
, 1);
1945 gfs2_dinode_out(dip
, dibh
->b_data
);
1949 gfs2_trans_end(sdp
);
1951 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1953 gfs2_rlist_free(&rlist
);
1954 gfs2_glock_dq_uninit(&dip
->i_alloc
->al_ri_gh
);
1956 gfs2_quota_unhold(dip
);
1958 gfs2_alloc_put(dip
);
1965 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1966 * @dip: the directory
1968 * Dealloc all on-disk directory leaves to FREEMETA state
1969 * Change on-disk inode type to "regular file"
1974 int gfs2_dir_exhash_dealloc(struct gfs2_inode
*dip
)
1976 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1977 struct buffer_head
*bh
;
1980 /* Dealloc on-disk leaves to FREEMETA state */
1981 error
= foreach_leaf(dip
, leaf_dealloc
, NULL
);
1985 /* Make this a regular file in case we crash.
1986 (We don't want to free these blocks a second time.) */
1988 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
1992 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1994 gfs2_trans_add_bh(dip
->i_gl
, bh
, 1);
1995 ((struct gfs2_dinode
*)bh
->b_data
)->di_mode
=
1996 cpu_to_be32(S_IFREG
);
2000 gfs2_trans_end(sdp
);
2006 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2007 * @ip: the file being written to
2008 * @filname: the filename that's going to be added
2010 * Returns: 1 if alloc required, 0 if not, -ve on error
2013 int gfs2_diradd_alloc_required(struct inode
*inode
, const struct qstr
*name
)
2015 struct gfs2_dirent
*dent
;
2016 struct buffer_head
*bh
;
2018 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
, &bh
);
2023 return PTR_ERR(dent
);