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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58 #include <linux/slab.h>
59 #include <linux/spinlock.h>
60 #include <linux/buffer_head.h>
61 #include <linux/sort.h>
62 #include <linux/gfs2_ondisk.h>
63 #include <linux/crc32.h>
64 #include <linux/vmalloc.h>
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81 #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
83 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
84 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
86 struct qstr gfs2_qdot __read_mostly
;
87 struct qstr gfs2_qdotdot __read_mostly
;
89 typedef int (*gfs2_dscan_t
)(const struct gfs2_dirent
*dent
,
90 const struct qstr
*name
, void *opaque
);
92 int gfs2_dir_get_new_buffer(struct gfs2_inode
*ip
, u64 block
,
93 struct buffer_head
**bhp
)
95 struct buffer_head
*bh
;
97 bh
= gfs2_meta_new(ip
->i_gl
, block
);
98 gfs2_trans_add_meta(ip
->i_gl
, bh
);
99 gfs2_metatype_set(bh
, GFS2_METATYPE_JD
, GFS2_FORMAT_JD
);
100 gfs2_buffer_clear_tail(bh
, sizeof(struct gfs2_meta_header
));
105 static int gfs2_dir_get_existing_buffer(struct gfs2_inode
*ip
, u64 block
,
106 struct buffer_head
**bhp
)
108 struct buffer_head
*bh
;
111 error
= gfs2_meta_read(ip
->i_gl
, block
, DIO_WAIT
, &bh
);
114 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_JD
)) {
122 static int gfs2_dir_write_stuffed(struct gfs2_inode
*ip
, const char *buf
,
123 unsigned int offset
, unsigned int size
)
125 struct buffer_head
*dibh
;
128 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
132 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
133 memcpy(dibh
->b_data
+ offset
+ sizeof(struct gfs2_dinode
), buf
, size
);
134 if (ip
->i_inode
.i_size
< offset
+ size
)
135 i_size_write(&ip
->i_inode
, offset
+ size
);
136 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
137 gfs2_dinode_out(ip
, dibh
->b_data
);
147 * gfs2_dir_write_data - Write directory information to the inode
148 * @ip: The GFS2 inode
149 * @buf: The buffer containing information to be written
150 * @offset: The file offset to start writing at
151 * @size: The amount of data to write
153 * Returns: The number of bytes correctly written or error code
155 static int gfs2_dir_write_data(struct gfs2_inode
*ip
, const char *buf
,
156 u64 offset
, unsigned int size
)
158 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
159 struct buffer_head
*dibh
;
170 if (gfs2_is_stuffed(ip
) &&
171 offset
+ size
<= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
172 return gfs2_dir_write_stuffed(ip
, buf
, (unsigned int)offset
,
175 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
178 if (gfs2_is_stuffed(ip
)) {
179 error
= gfs2_unstuff_dinode(ip
, NULL
);
185 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
187 while (copied
< size
) {
189 struct buffer_head
*bh
;
191 amount
= size
- copied
;
192 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
193 amount
= sdp
->sd_sb
.sb_bsize
- o
;
197 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
202 if (gfs2_assert_withdraw(sdp
, dblock
))
206 if (amount
== sdp
->sd_jbsize
|| new)
207 error
= gfs2_dir_get_new_buffer(ip
, dblock
, &bh
);
209 error
= gfs2_dir_get_existing_buffer(ip
, dblock
, &bh
);
214 gfs2_trans_add_meta(ip
->i_gl
, bh
);
215 memcpy(bh
->b_data
+ o
, buf
, amount
);
224 o
= sizeof(struct gfs2_meta_header
);
228 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
232 if (ip
->i_inode
.i_size
< offset
+ copied
)
233 i_size_write(&ip
->i_inode
, offset
+ copied
);
234 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
236 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
237 gfs2_dinode_out(ip
, dibh
->b_data
);
247 static int gfs2_dir_read_stuffed(struct gfs2_inode
*ip
, __be64
*buf
,
250 struct buffer_head
*dibh
;
253 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
255 memcpy(buf
, dibh
->b_data
+ sizeof(struct gfs2_dinode
), 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 * @size: Amount of data to transfer
269 * Returns: The amount of data actually copied or the error
271 static int gfs2_dir_read_data(struct gfs2_inode
*ip
, __be64
*buf
,
274 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
281 if (gfs2_is_stuffed(ip
))
282 return gfs2_dir_read_stuffed(ip
, buf
, size
);
284 if (gfs2_assert_warn(sdp
, gfs2_is_jdata(ip
)))
288 o
= do_div(lblock
, sdp
->sd_jbsize
) + sizeof(struct gfs2_meta_header
);
290 while (copied
< size
) {
292 struct buffer_head
*bh
;
295 amount
= size
- copied
;
296 if (amount
> sdp
->sd_sb
.sb_bsize
- o
)
297 amount
= sdp
->sd_sb
.sb_bsize
- o
;
301 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new,
303 if (error
|| !dblock
)
306 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
308 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_WAIT
, &bh
);
312 error
= gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_JD
);
319 memcpy(buf
, bh
->b_data
+ o
, amount
);
321 buf
+= (amount
/sizeof(__be64
));
324 o
= sizeof(struct gfs2_meta_header
);
329 return (copied
) ? copied
: error
;
333 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
334 * @ip: The inode in question
336 * Returns: The hash table or an error
339 static __be64
*gfs2_dir_get_hash_table(struct gfs2_inode
*ip
)
341 struct inode
*inode
= &ip
->i_inode
;
346 BUG_ON(!(ip
->i_diskflags
& GFS2_DIF_EXHASH
));
348 hc
= ip
->i_hash_cache
;
352 hsize
= 1 << ip
->i_depth
;
353 hsize
*= sizeof(__be64
);
354 if (hsize
!= i_size_read(&ip
->i_inode
)) {
355 gfs2_consist_inode(ip
);
356 return ERR_PTR(-EIO
);
359 hc
= kmalloc(hsize
, GFP_NOFS
| __GFP_NOWARN
);
361 hc
= __vmalloc(hsize
, GFP_NOFS
, PAGE_KERNEL
);
364 return ERR_PTR(-ENOMEM
);
366 ret
= gfs2_dir_read_data(ip
, hc
, hsize
);
372 spin_lock(&inode
->i_lock
);
373 if (likely(!ip
->i_hash_cache
)) {
374 ip
->i_hash_cache
= hc
;
377 spin_unlock(&inode
->i_lock
);
380 return ip
->i_hash_cache
;
384 * gfs2_dir_hash_inval - Invalidate dir hash
385 * @ip: The directory inode
387 * Must be called with an exclusive glock, or during glock invalidation.
389 void gfs2_dir_hash_inval(struct gfs2_inode
*ip
)
391 __be64
*hc
= ip
->i_hash_cache
;
392 ip
->i_hash_cache
= NULL
;
396 static inline int gfs2_dirent_sentinel(const struct gfs2_dirent
*dent
)
398 return dent
->de_inum
.no_addr
== 0 || dent
->de_inum
.no_formal_ino
== 0;
401 static inline int __gfs2_dirent_find(const struct gfs2_dirent
*dent
,
402 const struct qstr
*name
, int ret
)
404 if (!gfs2_dirent_sentinel(dent
) &&
405 be32_to_cpu(dent
->de_hash
) == name
->hash
&&
406 be16_to_cpu(dent
->de_name_len
) == name
->len
&&
407 memcmp(dent
+1, name
->name
, name
->len
) == 0)
412 static int gfs2_dirent_find(const struct gfs2_dirent
*dent
,
413 const struct qstr
*name
,
416 return __gfs2_dirent_find(dent
, name
, 1);
419 static int gfs2_dirent_prev(const struct gfs2_dirent
*dent
,
420 const struct qstr
*name
,
423 return __gfs2_dirent_find(dent
, name
, 2);
427 * name->name holds ptr to start of block.
428 * name->len holds size of block.
430 static int gfs2_dirent_last(const struct gfs2_dirent
*dent
,
431 const struct qstr
*name
,
434 const char *start
= name
->name
;
435 const char *end
= (const char *)dent
+ be16_to_cpu(dent
->de_rec_len
);
436 if (name
->len
== (end
- start
))
441 static int gfs2_dirent_find_space(const struct gfs2_dirent
*dent
,
442 const struct qstr
*name
,
445 unsigned required
= GFS2_DIRENT_SIZE(name
->len
);
446 unsigned actual
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
447 unsigned totlen
= be16_to_cpu(dent
->de_rec_len
);
449 if (gfs2_dirent_sentinel(dent
))
451 if (totlen
- actual
>= required
)
456 struct dirent_gather
{
457 const struct gfs2_dirent
**pdent
;
461 static int gfs2_dirent_gather(const struct gfs2_dirent
*dent
,
462 const struct qstr
*name
,
465 struct dirent_gather
*g
= opaque
;
466 if (!gfs2_dirent_sentinel(dent
)) {
467 g
->pdent
[g
->offset
++] = dent
;
473 * Other possible things to check:
474 * - Inode located within filesystem size (and on valid block)
475 * - Valid directory entry type
476 * Not sure how heavy-weight we want to make this... could also check
477 * hash is correct for example, but that would take a lot of extra time.
478 * For now the most important thing is to check that the various sizes
481 static int gfs2_check_dirent(struct gfs2_dirent
*dent
, unsigned int offset
,
482 unsigned int size
, unsigned int len
, int first
)
484 const char *msg
= "gfs2_dirent too small";
485 if (unlikely(size
< sizeof(struct gfs2_dirent
)))
487 msg
= "gfs2_dirent misaligned";
488 if (unlikely(offset
& 0x7))
490 msg
= "gfs2_dirent points beyond end of block";
491 if (unlikely(offset
+ size
> len
))
493 msg
= "zero inode number";
494 if (unlikely(!first
&& gfs2_dirent_sentinel(dent
)))
496 msg
= "name length is greater than space in dirent";
497 if (!gfs2_dirent_sentinel(dent
) &&
498 unlikely(sizeof(struct gfs2_dirent
)+be16_to_cpu(dent
->de_name_len
) >
503 pr_warn("%s: %s (%s)\n",
504 __func__
, msg
, first
? "first in block" : "not first in block");
508 static int gfs2_dirent_offset(const void *buf
)
510 const struct gfs2_meta_header
*h
= buf
;
515 switch(be32_to_cpu(h
->mh_type
)) {
516 case GFS2_METATYPE_LF
:
517 offset
= sizeof(struct gfs2_leaf
);
519 case GFS2_METATYPE_DI
:
520 offset
= sizeof(struct gfs2_dinode
);
527 pr_warn("%s: wrong block type %u\n", __func__
, be32_to_cpu(h
->mh_type
));
531 static struct gfs2_dirent
*gfs2_dirent_scan(struct inode
*inode
, void *buf
,
532 unsigned int len
, gfs2_dscan_t scan
,
533 const struct qstr
*name
,
536 struct gfs2_dirent
*dent
, *prev
;
541 ret
= gfs2_dirent_offset(buf
);
548 size
= be16_to_cpu(dent
->de_rec_len
);
549 if (gfs2_check_dirent(dent
, offset
, size
, len
, 1))
552 ret
= scan(dent
, name
, opaque
);
560 size
= be16_to_cpu(dent
->de_rec_len
);
561 if (gfs2_check_dirent(dent
, offset
, size
, len
, 0))
571 return prev
? prev
: dent
;
578 gfs2_consist_inode(GFS2_I(inode
));
579 return ERR_PTR(-EIO
);
582 static int dirent_check_reclen(struct gfs2_inode
*dip
,
583 const struct gfs2_dirent
*d
, const void *end_p
)
586 u16 rec_len
= be16_to_cpu(d
->de_rec_len
);
588 if (unlikely(rec_len
< sizeof(struct gfs2_dirent
)))
596 gfs2_consist_inode(dip
);
601 * dirent_next - Next dirent
602 * @dip: the directory
604 * @dent: Pointer to list of dirents
606 * Returns: 0 on success, error code otherwise
609 static int dirent_next(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
610 struct gfs2_dirent
**dent
)
612 struct gfs2_dirent
*cur
= *dent
, *tmp
;
613 char *bh_end
= bh
->b_data
+ bh
->b_size
;
616 ret
= dirent_check_reclen(dip
, cur
, bh_end
);
620 tmp
= (void *)cur
+ ret
;
621 ret
= dirent_check_reclen(dip
, tmp
, bh_end
);
625 /* Only the first dent could ever have de_inum.no_addr == 0 */
626 if (gfs2_dirent_sentinel(tmp
)) {
627 gfs2_consist_inode(dip
);
636 * dirent_del - Delete a dirent
637 * @dip: The GFS2 inode
639 * @prev: The previous dirent
640 * @cur: The current dirent
644 static void dirent_del(struct gfs2_inode
*dip
, struct buffer_head
*bh
,
645 struct gfs2_dirent
*prev
, struct gfs2_dirent
*cur
)
647 u16 cur_rec_len
, prev_rec_len
;
649 if (gfs2_dirent_sentinel(cur
)) {
650 gfs2_consist_inode(dip
);
654 gfs2_trans_add_meta(dip
->i_gl
, bh
);
656 /* If there is no prev entry, this is the first entry in the block.
657 The de_rec_len is already as big as it needs to be. Just zero
658 out the inode number and return. */
661 cur
->de_inum
.no_addr
= 0;
662 cur
->de_inum
.no_formal_ino
= 0;
666 /* Combine this dentry with the previous one. */
668 prev_rec_len
= be16_to_cpu(prev
->de_rec_len
);
669 cur_rec_len
= be16_to_cpu(cur
->de_rec_len
);
671 if ((char *)prev
+ prev_rec_len
!= (char *)cur
)
672 gfs2_consist_inode(dip
);
673 if ((char *)cur
+ cur_rec_len
> bh
->b_data
+ bh
->b_size
)
674 gfs2_consist_inode(dip
);
676 prev_rec_len
+= cur_rec_len
;
677 prev
->de_rec_len
= cpu_to_be16(prev_rec_len
);
681 * Takes a dent from which to grab space as an argument. Returns the
682 * newly created dent.
684 static struct gfs2_dirent
*gfs2_init_dirent(struct inode
*inode
,
685 struct gfs2_dirent
*dent
,
686 const struct qstr
*name
,
687 struct buffer_head
*bh
)
689 struct gfs2_inode
*ip
= GFS2_I(inode
);
690 struct gfs2_dirent
*ndent
;
691 unsigned offset
= 0, totlen
;
693 if (!gfs2_dirent_sentinel(dent
))
694 offset
= GFS2_DIRENT_SIZE(be16_to_cpu(dent
->de_name_len
));
695 totlen
= be16_to_cpu(dent
->de_rec_len
);
696 BUG_ON(offset
+ name
->len
> totlen
);
697 gfs2_trans_add_meta(ip
->i_gl
, bh
);
698 ndent
= (struct gfs2_dirent
*)((char *)dent
+ offset
);
699 dent
->de_rec_len
= cpu_to_be16(offset
);
700 gfs2_qstr2dirent(name
, totlen
- offset
, ndent
);
704 static struct gfs2_dirent
*gfs2_dirent_alloc(struct inode
*inode
,
705 struct buffer_head
*bh
,
706 const struct qstr
*name
)
708 struct gfs2_dirent
*dent
;
709 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
710 gfs2_dirent_find_space
, name
, NULL
);
711 if (!dent
|| IS_ERR(dent
))
713 return gfs2_init_dirent(inode
, dent
, name
, bh
);
716 static int get_leaf(struct gfs2_inode
*dip
, u64 leaf_no
,
717 struct buffer_head
**bhp
)
721 error
= gfs2_meta_read(dip
->i_gl
, leaf_no
, DIO_WAIT
, bhp
);
722 if (!error
&& gfs2_metatype_check(GFS2_SB(&dip
->i_inode
), *bhp
, GFS2_METATYPE_LF
)) {
723 /* pr_info("block num=%llu\n", leaf_no); */
731 * get_leaf_nr - Get a leaf number associated with the index
732 * @dip: The GFS2 inode
736 * Returns: 0 on success, error code otherwise
739 static int get_leaf_nr(struct gfs2_inode
*dip
, u32 index
,
744 hash
= gfs2_dir_get_hash_table(dip
);
746 return PTR_ERR(hash
);
747 *leaf_out
= be64_to_cpu(*(hash
+ index
));
751 static int get_first_leaf(struct gfs2_inode
*dip
, u32 index
,
752 struct buffer_head
**bh_out
)
757 error
= get_leaf_nr(dip
, index
, &leaf_no
);
759 error
= get_leaf(dip
, leaf_no
, bh_out
);
764 static struct gfs2_dirent
*gfs2_dirent_search(struct inode
*inode
,
765 const struct qstr
*name
,
767 struct buffer_head
**pbh
)
769 struct buffer_head
*bh
;
770 struct gfs2_dirent
*dent
;
771 struct gfs2_inode
*ip
= GFS2_I(inode
);
774 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
775 struct gfs2_leaf
*leaf
;
776 unsigned hsize
= 1 << ip
->i_depth
;
779 if (hsize
* sizeof(u64
) != i_size_read(inode
)) {
780 gfs2_consist_inode(ip
);
781 return ERR_PTR(-EIO
);
784 index
= name
->hash
>> (32 - ip
->i_depth
);
785 error
= get_first_leaf(ip
, index
, &bh
);
787 return ERR_PTR(error
);
789 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
793 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
794 ln
= be64_to_cpu(leaf
->lf_next
);
799 error
= get_leaf(ip
, ln
, &bh
);
802 return error
? ERR_PTR(error
) : NULL
;
806 error
= gfs2_meta_inode_buffer(ip
, &bh
);
808 return ERR_PTR(error
);
809 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
, scan
, name
, NULL
);
811 if (unlikely(dent
== NULL
|| IS_ERR(dent
))) {
819 static struct gfs2_leaf
*new_leaf(struct inode
*inode
, struct buffer_head
**pbh
, u16 depth
)
821 struct gfs2_inode
*ip
= GFS2_I(inode
);
825 struct buffer_head
*bh
;
826 struct gfs2_leaf
*leaf
;
827 struct gfs2_dirent
*dent
;
828 struct qstr name
= { .name
= "" };
829 struct timespec tv
= CURRENT_TIME
;
831 error
= gfs2_alloc_blocks(ip
, &bn
, &n
, 0, NULL
);
834 bh
= gfs2_meta_new(ip
->i_gl
, bn
);
838 gfs2_trans_add_unrevoke(GFS2_SB(inode
), bn
, 1);
839 gfs2_trans_add_meta(ip
->i_gl
, bh
);
840 gfs2_metatype_set(bh
, GFS2_METATYPE_LF
, GFS2_FORMAT_LF
);
841 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
842 leaf
->lf_depth
= cpu_to_be16(depth
);
843 leaf
->lf_entries
= 0;
844 leaf
->lf_dirent_format
= cpu_to_be32(GFS2_FORMAT_DE
);
846 leaf
->lf_inode
= cpu_to_be64(ip
->i_no_addr
);
847 leaf
->lf_dist
= cpu_to_be32(1);
848 leaf
->lf_nsec
= cpu_to_be32(tv
.tv_nsec
);
849 leaf
->lf_sec
= cpu_to_be64(tv
.tv_sec
);
850 memset(leaf
->lf_reserved2
, 0, sizeof(leaf
->lf_reserved2
));
851 dent
= (struct gfs2_dirent
*)(leaf
+1);
852 gfs2_qstr2dirent(&name
, bh
->b_size
- sizeof(struct gfs2_leaf
), dent
);
858 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
859 * @dip: The GFS2 inode
861 * Returns: 0 on success, error code otherwise
864 static int dir_make_exhash(struct inode
*inode
)
866 struct gfs2_inode
*dip
= GFS2_I(inode
);
867 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
868 struct gfs2_dirent
*dent
;
870 struct buffer_head
*bh
, *dibh
;
871 struct gfs2_leaf
*leaf
;
878 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
882 /* Turn over a new leaf */
884 leaf
= new_leaf(inode
, &bh
, 0);
889 gfs2_assert(sdp
, dip
->i_entries
< (1 << 16));
890 leaf
->lf_entries
= cpu_to_be16(dip
->i_entries
);
894 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_leaf
), dibh
,
895 sizeof(struct gfs2_dinode
));
897 /* Find last entry */
900 args
.len
= bh
->b_size
- sizeof(struct gfs2_dinode
) +
901 sizeof(struct gfs2_leaf
);
902 args
.name
= bh
->b_data
;
903 dent
= gfs2_dirent_scan(&dip
->i_inode
, bh
->b_data
, bh
->b_size
,
904 gfs2_dirent_last
, &args
, NULL
);
913 return PTR_ERR(dent
);
916 /* Adjust the last dirent's record length
917 (Remember that dent still points to the last entry.) */
919 dent
->de_rec_len
= cpu_to_be16(be16_to_cpu(dent
->de_rec_len
) +
920 sizeof(struct gfs2_dinode
) -
921 sizeof(struct gfs2_leaf
));
925 /* We're done with the new leaf block, now setup the new
928 gfs2_trans_add_meta(dip
->i_gl
, dibh
);
929 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
931 lp
= (__be64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
933 for (x
= sdp
->sd_hash_ptrs
; x
--; lp
++)
934 *lp
= cpu_to_be64(bn
);
936 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
/ 2);
937 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
938 dip
->i_diskflags
|= GFS2_DIF_EXHASH
;
940 for (x
= sdp
->sd_hash_ptrs
, y
= -1; x
; x
>>= 1, y
++) ;
943 gfs2_dinode_out(dip
, dibh
->b_data
);
951 * dir_split_leaf - Split a leaf block into two
952 * @dip: The GFS2 inode
956 * Returns: 0 on success, error code on failure
959 static int dir_split_leaf(struct inode
*inode
, const struct qstr
*name
)
961 struct gfs2_inode
*dip
= GFS2_I(inode
);
962 struct buffer_head
*nbh
, *obh
, *dibh
;
963 struct gfs2_leaf
*nleaf
, *oleaf
;
964 struct gfs2_dirent
*dent
= NULL
, *prev
= NULL
, *next
= NULL
, *new;
965 u32 start
, len
, half_len
, divider
;
972 index
= name
->hash
>> (32 - dip
->i_depth
);
973 error
= get_leaf_nr(dip
, index
, &leaf_no
);
977 /* Get the old leaf block */
978 error
= get_leaf(dip
, leaf_no
, &obh
);
982 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
983 if (dip
->i_depth
== be16_to_cpu(oleaf
->lf_depth
)) {
985 return 1; /* can't split */
988 gfs2_trans_add_meta(dip
->i_gl
, obh
);
990 nleaf
= new_leaf(inode
, &nbh
, be16_to_cpu(oleaf
->lf_depth
) + 1);
997 /* Compute the start and len of leaf pointers in the hash table. */
998 len
= 1 << (dip
->i_depth
- be16_to_cpu(oleaf
->lf_depth
));
1001 pr_warn("i_depth %u lf_depth %u index %u\n",
1002 dip
->i_depth
, be16_to_cpu(oleaf
->lf_depth
), index
);
1003 gfs2_consist_inode(dip
);
1008 start
= (index
& ~(len
- 1));
1010 /* Change the pointers.
1011 Don't bother distinguishing stuffed from non-stuffed.
1012 This code is complicated enough already. */
1013 lp
= kmalloc(half_len
* sizeof(__be64
), GFP_NOFS
);
1019 /* Change the pointers */
1020 for (x
= 0; x
< half_len
; x
++)
1021 lp
[x
] = cpu_to_be64(bn
);
1023 gfs2_dir_hash_inval(dip
);
1025 error
= gfs2_dir_write_data(dip
, (char *)lp
, start
* sizeof(u64
),
1026 half_len
* sizeof(u64
));
1027 if (error
!= half_len
* sizeof(u64
)) {
1035 /* Compute the divider */
1036 divider
= (start
+ half_len
) << (32 - dip
->i_depth
);
1038 /* Copy the entries */
1039 dent
= (struct gfs2_dirent
*)(obh
->b_data
+ sizeof(struct gfs2_leaf
));
1043 if (dirent_next(dip
, obh
, &next
))
1046 if (!gfs2_dirent_sentinel(dent
) &&
1047 be32_to_cpu(dent
->de_hash
) < divider
) {
1049 str
.name
= (char*)(dent
+1);
1050 str
.len
= be16_to_cpu(dent
->de_name_len
);
1051 str
.hash
= be32_to_cpu(dent
->de_hash
);
1052 new = gfs2_dirent_alloc(inode
, nbh
, &str
);
1054 error
= PTR_ERR(new);
1058 new->de_inum
= dent
->de_inum
; /* No endian worries */
1059 new->de_type
= dent
->de_type
; /* No endian worries */
1060 be16_add_cpu(&nleaf
->lf_entries
, 1);
1062 dirent_del(dip
, obh
, prev
, dent
);
1064 if (!oleaf
->lf_entries
)
1065 gfs2_consist_inode(dip
);
1066 be16_add_cpu(&oleaf
->lf_entries
, -1);
1078 oleaf
->lf_depth
= nleaf
->lf_depth
;
1080 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1081 if (!gfs2_assert_withdraw(GFS2_SB(&dip
->i_inode
), !error
)) {
1082 gfs2_trans_add_meta(dip
->i_gl
, dibh
);
1083 gfs2_add_inode_blocks(&dip
->i_inode
, 1);
1084 gfs2_dinode_out(dip
, dibh
->b_data
);
1103 * dir_double_exhash - Double size of ExHash table
1104 * @dip: The GFS2 dinode
1106 * Returns: 0 on success, error code on failure
1109 static int dir_double_exhash(struct gfs2_inode
*dip
)
1111 struct buffer_head
*dibh
;
1119 hsize
= 1 << dip
->i_depth
;
1120 hsize_bytes
= hsize
* sizeof(__be64
);
1122 hc
= gfs2_dir_get_hash_table(dip
);
1126 hc2
= kmalloc(hsize_bytes
* 2, GFP_NOFS
| __GFP_NOWARN
);
1128 hc2
= __vmalloc(hsize_bytes
* 2, GFP_NOFS
, PAGE_KERNEL
);
1134 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1138 for (x
= 0; x
< hsize
; x
++) {
1144 error
= gfs2_dir_write_data(dip
, (char *)hc2
, 0, hsize_bytes
* 2);
1145 if (error
!= (hsize_bytes
* 2))
1148 gfs2_dir_hash_inval(dip
);
1149 dip
->i_hash_cache
= hc2
;
1151 gfs2_dinode_out(dip
, dibh
->b_data
);
1156 /* Replace original hash table & size */
1157 gfs2_dir_write_data(dip
, (char *)hc
, 0, hsize_bytes
);
1158 i_size_write(&dip
->i_inode
, hsize_bytes
);
1159 gfs2_dinode_out(dip
, dibh
->b_data
);
1167 * compare_dents - compare directory entries by hash value
1171 * When comparing the hash entries of @a to @b:
1177 static int compare_dents(const void *a
, const void *b
)
1179 const struct gfs2_dirent
*dent_a
, *dent_b
;
1183 dent_a
= *(const struct gfs2_dirent
**)a
;
1184 hash_a
= be32_to_cpu(dent_a
->de_hash
);
1186 dent_b
= *(const struct gfs2_dirent
**)b
;
1187 hash_b
= be32_to_cpu(dent_b
->de_hash
);
1189 if (hash_a
> hash_b
)
1191 else if (hash_a
< hash_b
)
1194 unsigned int len_a
= be16_to_cpu(dent_a
->de_name_len
);
1195 unsigned int len_b
= be16_to_cpu(dent_b
->de_name_len
);
1199 else if (len_a
< len_b
)
1202 ret
= memcmp(dent_a
+ 1, dent_b
+ 1, len_a
);
1209 * do_filldir_main - read out directory entries
1210 * @dip: The GFS2 inode
1211 * @ctx: what to feed the entries to
1212 * @darr: an array of struct gfs2_dirent pointers to read
1213 * @entries: the number of entries in darr
1214 * @copied: pointer to int that's non-zero if a entry has been copied out
1216 * Jump through some hoops to make sure that if there are hash collsions,
1217 * they are read out at the beginning of a buffer. We want to minimize
1218 * the possibility that they will fall into different readdir buffers or
1219 * that someone will want to seek to that location.
1221 * Returns: errno, >0 if the actor tells you to stop
1224 static int do_filldir_main(struct gfs2_inode
*dip
, struct dir_context
*ctx
,
1225 const struct gfs2_dirent
**darr
, u32 entries
,
1228 const struct gfs2_dirent
*dent
, *dent_next
;
1233 sort(darr
, entries
, sizeof(struct gfs2_dirent
*), compare_dents
, NULL
);
1235 dent_next
= darr
[0];
1236 off_next
= be32_to_cpu(dent_next
->de_hash
);
1237 off_next
= gfs2_disk_hash2offset(off_next
);
1239 for (x
= 0, y
= 1; x
< entries
; x
++, y
++) {
1244 dent_next
= darr
[y
];
1245 off_next
= be32_to_cpu(dent_next
->de_hash
);
1246 off_next
= gfs2_disk_hash2offset(off_next
);
1252 if (off_next
== off
) {
1253 if (*copied
&& !run
)
1264 if (!dir_emit(ctx
, (const char *)(dent
+ 1),
1265 be16_to_cpu(dent
->de_name_len
),
1266 be64_to_cpu(dent
->de_inum
.no_addr
),
1267 be16_to_cpu(dent
->de_type
)))
1273 /* Increment the ctx->pos by one, so the next time we come into the
1274 do_filldir fxn, we get the next entry instead of the last one in the
1282 static void *gfs2_alloc_sort_buffer(unsigned size
)
1286 if (size
< KMALLOC_MAX_SIZE
)
1287 ptr
= kmalloc(size
, GFP_NOFS
| __GFP_NOWARN
);
1289 ptr
= __vmalloc(size
, GFP_NOFS
, PAGE_KERNEL
);
1293 static int gfs2_dir_read_leaf(struct inode
*inode
, struct dir_context
*ctx
,
1294 int *copied
, unsigned *depth
,
1297 struct gfs2_inode
*ip
= GFS2_I(inode
);
1298 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1299 struct buffer_head
*bh
;
1300 struct gfs2_leaf
*lf
;
1301 unsigned entries
= 0, entries2
= 0;
1302 unsigned leaves
= 0;
1303 const struct gfs2_dirent
**darr
, *dent
;
1304 struct dirent_gather g
;
1305 struct buffer_head
**larr
;
1311 error
= get_leaf(ip
, lfn
, &bh
);
1314 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1316 *depth
= be16_to_cpu(lf
->lf_depth
);
1317 entries
+= be16_to_cpu(lf
->lf_entries
);
1319 lfn
= be64_to_cpu(lf
->lf_next
);
1328 * The extra 99 entries are not normally used, but are a buffer
1329 * zone in case the number of entries in the leaf is corrupt.
1330 * 99 is the maximum number of entries that can fit in a single
1333 larr
= gfs2_alloc_sort_buffer((leaves
+ entries
+ 99) * sizeof(void *));
1336 darr
= (const struct gfs2_dirent
**)(larr
+ leaves
);
1342 error
= get_leaf(ip
, lfn
, &bh
);
1345 lf
= (struct gfs2_leaf
*)bh
->b_data
;
1346 lfn
= be64_to_cpu(lf
->lf_next
);
1347 if (lf
->lf_entries
) {
1348 entries2
+= be16_to_cpu(lf
->lf_entries
);
1349 dent
= gfs2_dirent_scan(inode
, bh
->b_data
, bh
->b_size
,
1350 gfs2_dirent_gather
, NULL
, &g
);
1351 error
= PTR_ERR(dent
);
1354 if (entries2
!= g
.offset
) {
1355 fs_warn(sdp
, "Number of entries corrupt in dir "
1356 "leaf %llu, entries2 (%u) != "
1358 (unsigned long long)bh
->b_blocknr
,
1359 entries2
, g
.offset
);
1371 BUG_ON(entries2
!= entries
);
1372 error
= do_filldir_main(ip
, ctx
, darr
, entries
, copied
);
1374 for(i
= 0; i
< leaf
; i
++)
1382 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
1384 * Note: we can't calculate each index like dir_e_read can because we don't
1385 * have the leaf, and therefore we don't have the depth, and therefore we
1386 * don't have the length. So we have to just read enough ahead to make up
1387 * for the loss of information.
1389 static void gfs2_dir_readahead(struct inode
*inode
, unsigned hsize
, u32 index
,
1390 struct file_ra_state
*f_ra
)
1392 struct gfs2_inode
*ip
= GFS2_I(inode
);
1393 struct gfs2_glock
*gl
= ip
->i_gl
;
1394 struct buffer_head
*bh
;
1395 u64 blocknr
= 0, last
;
1398 /* First check if we've already read-ahead for the whole range. */
1399 if (index
+ MAX_RA_BLOCKS
< f_ra
->start
)
1402 f_ra
->start
= max((pgoff_t
)index
, f_ra
->start
);
1403 for (count
= 0; count
< MAX_RA_BLOCKS
; count
++) {
1404 if (f_ra
->start
>= hsize
) /* if exceeded the hash table */
1408 blocknr
= be64_to_cpu(ip
->i_hash_cache
[f_ra
->start
]);
1410 if (blocknr
== last
)
1413 bh
= gfs2_getbuf(gl
, blocknr
, 1);
1414 if (trylock_buffer(bh
)) {
1415 if (buffer_uptodate(bh
)) {
1420 bh
->b_end_io
= end_buffer_read_sync
;
1421 submit_bh(READA
| REQ_META
, bh
);
1429 * dir_e_read - Reads the entries from a directory into a filldir buffer
1430 * @dip: dinode pointer
1431 * @ctx: actor to feed the entries to
1436 static int dir_e_read(struct inode
*inode
, struct dir_context
*ctx
,
1437 struct file_ra_state
*f_ra
)
1439 struct gfs2_inode
*dip
= GFS2_I(inode
);
1447 hsize
= 1 << dip
->i_depth
;
1448 hash
= gfs2_dir_offset2hash(ctx
->pos
);
1449 index
= hash
>> (32 - dip
->i_depth
);
1451 if (dip
->i_hash_cache
== NULL
)
1453 lp
= gfs2_dir_get_hash_table(dip
);
1457 gfs2_dir_readahead(inode
, hsize
, index
, f_ra
);
1459 while (index
< hsize
) {
1460 error
= gfs2_dir_read_leaf(inode
, ctx
,
1462 be64_to_cpu(lp
[index
]));
1466 len
= 1 << (dip
->i_depth
- depth
);
1467 index
= (index
& ~(len
- 1)) + len
;
1475 int gfs2_dir_read(struct inode
*inode
, struct dir_context
*ctx
,
1476 struct file_ra_state
*f_ra
)
1478 struct gfs2_inode
*dip
= GFS2_I(inode
);
1479 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1480 struct dirent_gather g
;
1481 const struct gfs2_dirent
**darr
, *dent
;
1482 struct buffer_head
*dibh
;
1486 if (!dip
->i_entries
)
1489 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
)
1490 return dir_e_read(inode
, ctx
, f_ra
);
1492 if (!gfs2_is_stuffed(dip
)) {
1493 gfs2_consist_inode(dip
);
1497 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1502 /* 96 is max number of dirents which can be stuffed into an inode */
1503 darr
= kmalloc(96 * sizeof(struct gfs2_dirent
*), GFP_NOFS
);
1507 dent
= gfs2_dirent_scan(inode
, dibh
->b_data
, dibh
->b_size
,
1508 gfs2_dirent_gather
, NULL
, &g
);
1510 error
= PTR_ERR(dent
);
1513 if (dip
->i_entries
!= g
.offset
) {
1514 fs_warn(sdp
, "Number of entries corrupt in dir %llu, "
1515 "ip->i_entries (%u) != g.offset (%u)\n",
1516 (unsigned long long)dip
->i_no_addr
,
1522 error
= do_filldir_main(dip
, ctx
, darr
,
1523 dip
->i_entries
, &copied
);
1537 * gfs2_dir_search - Search a directory
1538 * @dip: The GFS2 dir inode
1539 * @name: The name we are looking up
1540 * @fail_on_exist: Fail if the name exists rather than looking it up
1542 * This routine searches a directory for a file or another directory.
1543 * Assumes a glock is held on dip.
1548 struct inode
*gfs2_dir_search(struct inode
*dir
, const struct qstr
*name
,
1551 struct buffer_head
*bh
;
1552 struct gfs2_dirent
*dent
;
1553 u64 addr
, formal_ino
;
1556 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1559 return ERR_CAST(dent
);
1560 dtype
= be16_to_cpu(dent
->de_type
);
1561 addr
= be64_to_cpu(dent
->de_inum
.no_addr
);
1562 formal_ino
= be64_to_cpu(dent
->de_inum
.no_formal_ino
);
1565 return ERR_PTR(-EEXIST
);
1566 return gfs2_inode_lookup(dir
->i_sb
, dtype
, addr
, formal_ino
, 0);
1568 return ERR_PTR(-ENOENT
);
1571 int gfs2_dir_check(struct inode
*dir
, const struct qstr
*name
,
1572 const struct gfs2_inode
*ip
)
1574 struct buffer_head
*bh
;
1575 struct gfs2_dirent
*dent
;
1578 dent
= gfs2_dirent_search(dir
, name
, gfs2_dirent_find
, &bh
);
1581 return PTR_ERR(dent
);
1583 if (be64_to_cpu(dent
->de_inum
.no_addr
) != ip
->i_no_addr
)
1585 if (be64_to_cpu(dent
->de_inum
.no_formal_ino
) !=
1586 ip
->i_no_formal_ino
)
1588 if (unlikely(IF2DT(ip
->i_inode
.i_mode
) !=
1589 be16_to_cpu(dent
->de_type
))) {
1590 gfs2_consist_inode(GFS2_I(dir
));
1603 * dir_new_leaf - Add a new leaf onto hash chain
1604 * @inode: The directory
1605 * @name: The name we are adding
1607 * This adds a new dir leaf onto an existing leaf when there is not
1608 * enough space to add a new dir entry. This is a last resort after
1609 * we've expanded the hash table to max size and also split existing
1610 * leaf blocks, so it will only occur for very large directories.
1612 * The dist parameter is set to 1 for leaf blocks directly attached
1613 * to the hash table, 2 for one layer of indirection, 3 for two layers
1614 * etc. We are thus able to tell the difference between an old leaf
1615 * with dist set to zero (i.e. "don't know") and a new one where we
1616 * set this information for debug/fsck purposes.
1618 * Returns: 0 on success, or -ve on error
1621 static int dir_new_leaf(struct inode
*inode
, const struct qstr
*name
)
1623 struct buffer_head
*bh
, *obh
;
1624 struct gfs2_inode
*ip
= GFS2_I(inode
);
1625 struct gfs2_leaf
*leaf
, *oleaf
;
1631 index
= name
->hash
>> (32 - ip
->i_depth
);
1632 error
= get_first_leaf(ip
, index
, &obh
);
1637 oleaf
= (struct gfs2_leaf
*)obh
->b_data
;
1638 bn
= be64_to_cpu(oleaf
->lf_next
);
1642 error
= get_leaf(ip
, bn
, &obh
);
1647 gfs2_trans_add_meta(ip
->i_gl
, obh
);
1649 leaf
= new_leaf(inode
, &bh
, be16_to_cpu(oleaf
->lf_depth
));
1654 leaf
->lf_dist
= cpu_to_be32(dist
);
1655 oleaf
->lf_next
= cpu_to_be64(bh
->b_blocknr
);
1659 error
= gfs2_meta_inode_buffer(ip
, &bh
);
1662 gfs2_trans_add_meta(ip
->i_gl
, bh
);
1663 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1664 gfs2_dinode_out(ip
, bh
->b_data
);
1669 static u16
gfs2_inode_ra_len(const struct gfs2_inode
*ip
)
1671 u64 where
= ip
->i_no_addr
+ 1;
1672 if (ip
->i_eattr
== where
)
1678 * gfs2_dir_add - Add new filename into directory
1679 * @inode: The directory inode
1680 * @name: The new name
1681 * @nip: The GFS2 inode to be linked in to the directory
1682 * @da: The directory addition info
1684 * If the call to gfs2_diradd_alloc_required resulted in there being
1685 * no need to allocate any new directory blocks, then it will contain
1686 * a pointer to the directory entry and the bh in which it resides. We
1687 * can use that without having to repeat the search. If there was no
1688 * free space, then we must now create more space.
1690 * Returns: 0 on success, error code on failure
1693 int gfs2_dir_add(struct inode
*inode
, const struct qstr
*name
,
1694 const struct gfs2_inode
*nip
, struct gfs2_diradd
*da
)
1696 struct gfs2_inode
*ip
= GFS2_I(inode
);
1697 struct buffer_head
*bh
= da
->bh
;
1698 struct gfs2_dirent
*dent
= da
->dent
;
1700 struct gfs2_leaf
*leaf
;
1704 if (da
->bh
== NULL
) {
1705 dent
= gfs2_dirent_search(inode
, name
,
1706 gfs2_dirent_find_space
, &bh
);
1710 return PTR_ERR(dent
);
1711 dent
= gfs2_init_dirent(inode
, dent
, name
, bh
);
1712 gfs2_inum_out(nip
, dent
);
1713 dent
->de_type
= cpu_to_be16(IF2DT(nip
->i_inode
.i_mode
));
1714 dent
->de_rahead
= cpu_to_be16(gfs2_inode_ra_len(nip
));
1716 if (ip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1717 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1718 be16_add_cpu(&leaf
->lf_entries
, 1);
1719 leaf
->lf_nsec
= cpu_to_be32(tv
.tv_nsec
);
1720 leaf
->lf_sec
= cpu_to_be64(tv
.tv_sec
);
1726 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= tv
;
1727 if (S_ISDIR(nip
->i_inode
.i_mode
))
1728 inc_nlink(&ip
->i_inode
);
1729 mark_inode_dirty(inode
);
1733 if (!(ip
->i_diskflags
& GFS2_DIF_EXHASH
)) {
1734 error
= dir_make_exhash(inode
);
1739 error
= dir_split_leaf(inode
, name
);
1744 if (ip
->i_depth
< GFS2_DIR_MAX_DEPTH
) {
1745 error
= dir_double_exhash(ip
);
1748 error
= dir_split_leaf(inode
, name
);
1754 error
= dir_new_leaf(inode
, name
);
1765 * gfs2_dir_del - Delete a directory entry
1766 * @dip: The GFS2 inode
1767 * @filename: The filename
1769 * Returns: 0 on success, error code on failure
1772 int gfs2_dir_del(struct gfs2_inode
*dip
, const struct dentry
*dentry
)
1774 const struct qstr
*name
= &dentry
->d_name
;
1775 struct gfs2_dirent
*dent
, *prev
= NULL
;
1776 struct buffer_head
*bh
;
1777 struct timespec tv
= CURRENT_TIME
;
1779 /* Returns _either_ the entry (if its first in block) or the
1780 previous entry otherwise */
1781 dent
= gfs2_dirent_search(&dip
->i_inode
, name
, gfs2_dirent_prev
, &bh
);
1783 gfs2_consist_inode(dip
);
1787 gfs2_consist_inode(dip
);
1788 return PTR_ERR(dent
);
1790 /* If not first in block, adjust pointers accordingly */
1791 if (gfs2_dirent_find(dent
, name
, NULL
) == 0) {
1793 dent
= (struct gfs2_dirent
*)((char *)dent
+ be16_to_cpu(prev
->de_rec_len
));
1796 dirent_del(dip
, bh
, prev
, dent
);
1797 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1798 struct gfs2_leaf
*leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1799 u16 entries
= be16_to_cpu(leaf
->lf_entries
);
1801 gfs2_consist_inode(dip
);
1802 leaf
->lf_entries
= cpu_to_be16(--entries
);
1803 leaf
->lf_nsec
= cpu_to_be32(tv
.tv_nsec
);
1804 leaf
->lf_sec
= cpu_to_be64(tv
.tv_sec
);
1808 if (!dip
->i_entries
)
1809 gfs2_consist_inode(dip
);
1811 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= tv
;
1812 if (S_ISDIR(dentry
->d_inode
->i_mode
))
1813 drop_nlink(&dip
->i_inode
);
1814 mark_inode_dirty(&dip
->i_inode
);
1820 * gfs2_dir_mvino - Change inode number of directory entry
1821 * @dip: The GFS2 inode
1825 * This routine changes the inode number of a directory entry. It's used
1826 * by rename to change ".." when a directory is moved.
1827 * Assumes a glock is held on dvp.
1832 int gfs2_dir_mvino(struct gfs2_inode
*dip
, const struct qstr
*filename
,
1833 const struct gfs2_inode
*nip
, unsigned int new_type
)
1835 struct buffer_head
*bh
;
1836 struct gfs2_dirent
*dent
;
1839 dent
= gfs2_dirent_search(&dip
->i_inode
, filename
, gfs2_dirent_find
, &bh
);
1841 gfs2_consist_inode(dip
);
1845 return PTR_ERR(dent
);
1847 gfs2_trans_add_meta(dip
->i_gl
, bh
);
1848 gfs2_inum_out(nip
, dent
);
1849 dent
->de_type
= cpu_to_be16(new_type
);
1851 if (dip
->i_diskflags
& GFS2_DIF_EXHASH
) {
1853 error
= gfs2_meta_inode_buffer(dip
, &bh
);
1856 gfs2_trans_add_meta(dip
->i_gl
, bh
);
1859 dip
->i_inode
.i_mtime
= dip
->i_inode
.i_ctime
= CURRENT_TIME
;
1860 gfs2_dinode_out(dip
, bh
->b_data
);
1866 * leaf_dealloc - Deallocate a directory leaf
1867 * @dip: the directory
1868 * @index: the hash table offset in the directory
1869 * @len: the number of pointers to this leaf
1870 * @leaf_no: the leaf number
1871 * @leaf_bh: buffer_head for the starting leaf
1872 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1877 static int leaf_dealloc(struct gfs2_inode
*dip
, u32 index
, u32 len
,
1878 u64 leaf_no
, struct buffer_head
*leaf_bh
,
1881 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
1882 struct gfs2_leaf
*tmp_leaf
;
1883 struct gfs2_rgrp_list rlist
;
1884 struct buffer_head
*bh
, *dibh
;
1886 unsigned int rg_blocks
= 0, l_blocks
= 0;
1888 unsigned int x
, size
= len
* sizeof(u64
);
1891 error
= gfs2_rindex_update(sdp
);
1895 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1897 ht
= kzalloc(size
, GFP_NOFS
| __GFP_NOWARN
);
1903 error
= gfs2_quota_hold(dip
, NO_UID_QUOTA_CHANGE
, NO_GID_QUOTA_CHANGE
);
1907 /* Count the number of leaves */
1910 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1911 if (blk
!= leaf_no
) {
1912 error
= get_leaf(dip
, blk
, &bh
);
1916 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1917 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1921 gfs2_rlist_add(dip
, &rlist
, blk
);
1925 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
1927 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1928 struct gfs2_rgrpd
*rgd
;
1929 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1930 rg_blocks
+= rgd
->rd_length
;
1933 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1937 error
= gfs2_trans_begin(sdp
,
1938 rg_blocks
+ (DIV_ROUND_UP(size
, sdp
->sd_jbsize
) + 1) +
1939 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, l_blocks
);
1941 goto out_rg_gunlock
;
1945 for (blk
= leaf_no
; blk
; blk
= nblk
) {
1946 if (blk
!= leaf_no
) {
1947 error
= get_leaf(dip
, blk
, &bh
);
1951 tmp_leaf
= (struct gfs2_leaf
*)bh
->b_data
;
1952 nblk
= be64_to_cpu(tmp_leaf
->lf_next
);
1956 gfs2_free_meta(dip
, blk
, 1);
1957 gfs2_add_inode_blocks(&dip
->i_inode
, -1);
1960 error
= gfs2_dir_write_data(dip
, ht
, index
* sizeof(u64
), size
);
1961 if (error
!= size
) {
1967 error
= gfs2_meta_inode_buffer(dip
, &dibh
);
1971 gfs2_trans_add_meta(dip
->i_gl
, dibh
);
1972 /* On the last dealloc, make this a regular file in case we crash.
1973 (We don't want to free these blocks a second time.) */
1975 dip
->i_inode
.i_mode
= S_IFREG
;
1976 gfs2_dinode_out(dip
, dibh
->b_data
);
1980 gfs2_trans_end(sdp
);
1982 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1984 gfs2_rlist_free(&rlist
);
1985 gfs2_quota_unhold(dip
);
1992 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1993 * @dip: the directory
1995 * Dealloc all on-disk directory leaves to FREEMETA state
1996 * Change on-disk inode type to "regular file"
2001 int gfs2_dir_exhash_dealloc(struct gfs2_inode
*dip
)
2003 struct buffer_head
*bh
;
2004 struct gfs2_leaf
*leaf
;
2006 u32 index
= 0, next_index
;
2009 int error
= 0, last
;
2011 hsize
= 1 << dip
->i_depth
;
2013 lp
= gfs2_dir_get_hash_table(dip
);
2017 while (index
< hsize
) {
2018 leaf_no
= be64_to_cpu(lp
[index
]);
2020 error
= get_leaf(dip
, leaf_no
, &bh
);
2023 leaf
= (struct gfs2_leaf
*)bh
->b_data
;
2024 len
= 1 << (dip
->i_depth
- be16_to_cpu(leaf
->lf_depth
));
2026 next_index
= (index
& ~(len
- 1)) + len
;
2027 last
= ((next_index
>= hsize
) ? 1 : 0);
2028 error
= leaf_dealloc(dip
, index
, len
, leaf_no
, bh
,
2038 if (index
!= hsize
) {
2039 gfs2_consist_inode(dip
);
2049 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2050 * @ip: the file being written to
2051 * @filname: the filename that's going to be added
2052 * @da: The structure to return dir alloc info
2054 * Returns: 0 if ok, -ve on error
2057 int gfs2_diradd_alloc_required(struct inode
*inode
, const struct qstr
*name
,
2058 struct gfs2_diradd
*da
)
2060 struct gfs2_inode
*ip
= GFS2_I(inode
);
2061 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
2062 const unsigned int extra
= sizeof(struct gfs2_dinode
) - sizeof(struct gfs2_leaf
);
2063 struct gfs2_dirent
*dent
;
2064 struct buffer_head
*bh
;
2070 dent
= gfs2_dirent_search(inode
, name
, gfs2_dirent_find_space
, &bh
);
2072 da
->nr_blocks
= sdp
->sd_max_dirres
;
2073 if (!(ip
->i_diskflags
& GFS2_DIF_EXHASH
) &&
2074 (GFS2_DIRENT_SIZE(name
->len
) < extra
))
2079 return PTR_ERR(dent
);