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_di.di_flags & 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/sched.h>
57 #include <linux/slab.h>
58 #include <linux/spinlock.h>
59 #include <linux/buffer_head.h>
60 #include <linux/sort.h>
61 #include <linux/gfs2_ondisk.h>
62 #include <linux/crc32.h>
63 #include <linux/vmalloc.h>
64 #include <linux/lm_interface.h>
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
84 typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
85 u64 leaf_no, void *data);
86 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
87 const struct qstr *name, void *opaque);
90 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
91 struct buffer_head **bhp)
93 struct buffer_head *bh;
95 bh = gfs2_meta_new(ip->i_gl, block);
96 gfs2_trans_add_bh(ip->i_gl, bh, 1);
97 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
103 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
104 struct buffer_head **bhp)
106 struct buffer_head *bh;
109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
120 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
123 struct buffer_head *dibh;
126 error = gfs2_meta_inode_buffer(ip, &dibh);
130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
132 if (ip->i_di.di_size < offset + size)
133 ip->i_di.di_size = offset + size;
134 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
135 gfs2_dinode_out(ip, dibh->b_data);
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
151 * Returns: The number of bytes correctly written or error code
153 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
154 u64 offset, unsigned int size)
156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
157 struct buffer_head *dibh;
167 if (gfs2_is_stuffed(ip) &&
168 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
169 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
172 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
175 if (gfs2_is_stuffed(ip)) {
176 error = gfs2_unstuff_dinode(ip, NULL);
182 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
184 while (copied < size) {
186 struct buffer_head *bh;
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
200 if (gfs2_assert_withdraw(sdp, dblock))
204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
222 o = sizeof(struct gfs2_meta_header);
226 error = gfs2_meta_inode_buffer(ip, &dibh);
230 if (ip->i_di.di_size < offset + copied)
231 ip->i_di.di_size = offset + copied;
232 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
235 gfs2_dinode_out(ip, dibh->b_data);
245 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
246 u64 offset, unsigned int size)
248 struct buffer_head *dibh;
251 error = gfs2_meta_inode_buffer(ip, &dibh);
253 offset += sizeof(struct gfs2_dinode);
254 memcpy(buf, dibh->b_data + offset, size);
258 return (error) ? error : size;
263 * gfs2_dir_read_data - Read a data from a directory inode
264 * @ip: The GFS2 Inode
265 * @buf: The buffer to place result into
266 * @offset: File offset to begin jdata_readng from
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, char *buf, u64 offset,
272 unsigned int size, unsigned ra)
274 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
281 if (offset >= ip->i_di.di_size)
284 if (offset + size > ip->i_di.di_size)
285 size = ip->i_di.di_size - offset;
290 if (gfs2_is_stuffed(ip))
291 return gfs2_dir_read_stuffed(ip, buf, offset, size);
293 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
297 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
299 while (copied < size) {
301 struct buffer_head *bh;
304 amount = size - copied;
305 if (amount > sdp->sd_sb.sb_bsize - o)
306 amount = sdp->sd_sb.sb_bsize - o;
310 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
312 if (error || !dblock)
317 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
319 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
323 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
330 memcpy(buf, bh->b_data + o, amount);
335 o = sizeof(struct gfs2_meta_header);
340 return (copied) ? copied : error;
343 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
344 const struct qstr *name, int ret)
346 if (dent->de_inum.no_addr != 0 &&
347 be32_to_cpu(dent->de_hash) == name->hash &&
348 be16_to_cpu(dent->de_name_len) == name->len &&
349 memcmp(dent+1, name->name, name->len) == 0)
354 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
355 const struct qstr *name,
358 return __gfs2_dirent_find(dent, name, 1);
361 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
362 const struct qstr *name,
365 return __gfs2_dirent_find(dent, name, 2);
369 * name->name holds ptr to start of block.
370 * name->len holds size of block.
372 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
373 const struct qstr *name,
376 const char *start = name->name;
377 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
378 if (name->len == (end - start))
383 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
384 const struct qstr *name,
387 unsigned required = GFS2_DIRENT_SIZE(name->len);
388 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
389 unsigned totlen = be16_to_cpu(dent->de_rec_len);
391 if (!dent->de_inum.no_addr)
392 actual = GFS2_DIRENT_SIZE(0);
393 if (totlen - actual >= required)
398 struct dirent_gather {
399 const struct gfs2_dirent **pdent;
403 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
404 const struct qstr *name,
407 struct dirent_gather *g = opaque;
408 if (dent->de_inum.no_addr) {
409 g->pdent[g->offset++] = dent;
415 * Other possible things to check:
416 * - Inode located within filesystem size (and on valid block)
417 * - Valid directory entry type
418 * Not sure how heavy-weight we want to make this... could also check
419 * hash is correct for example, but that would take a lot of extra time.
420 * For now the most important thing is to check that the various sizes
423 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
424 unsigned int size, unsigned int len, int first)
426 const char *msg = "gfs2_dirent too small";
427 if (unlikely(size < sizeof(struct gfs2_dirent)))
429 msg = "gfs2_dirent misaligned";
430 if (unlikely(offset & 0x7))
432 msg = "gfs2_dirent points beyond end of block";
433 if (unlikely(offset + size > len))
435 msg = "zero inode number";
436 if (unlikely(!first && !dent->de_inum.no_addr))
438 msg = "name length is greater than space in dirent";
439 if (dent->de_inum.no_addr &&
440 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
445 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
446 first ? "first in block" : "not first in block");
450 static int gfs2_dirent_offset(const void *buf)
452 const struct gfs2_meta_header *h = buf;
457 switch(be32_to_cpu(h->mh_type)) {
458 case GFS2_METATYPE_LF:
459 offset = sizeof(struct gfs2_leaf);
461 case GFS2_METATYPE_DI:
462 offset = sizeof(struct gfs2_dinode);
469 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
470 be32_to_cpu(h->mh_type));
474 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
475 unsigned int len, gfs2_dscan_t scan,
476 const struct qstr *name,
479 struct gfs2_dirent *dent, *prev;
484 ret = gfs2_dirent_offset(buf);
491 size = be16_to_cpu(dent->de_rec_len);
492 if (gfs2_check_dirent(dent, offset, size, len, 1))
495 ret = scan(dent, name, opaque);
503 size = be16_to_cpu(dent->de_rec_len);
504 if (gfs2_check_dirent(dent, offset, size, len, 0))
514 return prev ? prev : dent;
521 gfs2_consist_inode(GFS2_I(inode));
522 return ERR_PTR(-EIO);
527 * dirent_first - Return the first dirent
528 * @dip: the directory
530 * @dent: Pointer to list of dirents
532 * return first dirent whether bh points to leaf or stuffed dinode
534 * Returns: IS_LEAF, IS_DINODE, or -errno
537 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
538 struct gfs2_dirent **dent)
540 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
542 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
543 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
545 *dent = (struct gfs2_dirent *)(bh->b_data +
546 sizeof(struct gfs2_leaf));
549 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
551 *dent = (struct gfs2_dirent *)(bh->b_data +
552 sizeof(struct gfs2_dinode));
557 static int dirent_check_reclen(struct gfs2_inode *dip,
558 const struct gfs2_dirent *d, const void *end_p)
561 u16 rec_len = be16_to_cpu(d->de_rec_len);
563 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
571 gfs2_consist_inode(dip);
576 * dirent_next - Next dirent
577 * @dip: the directory
579 * @dent: Pointer to list of dirents
581 * Returns: 0 on success, error code otherwise
584 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
585 struct gfs2_dirent **dent)
587 struct gfs2_dirent *cur = *dent, *tmp;
588 char *bh_end = bh->b_data + bh->b_size;
591 ret = dirent_check_reclen(dip, cur, bh_end);
595 tmp = (void *)cur + ret;
596 ret = dirent_check_reclen(dip, tmp, bh_end);
600 /* Only the first dent could ever have de_inum.no_addr == 0 */
601 if (!tmp->de_inum.no_addr) {
602 gfs2_consist_inode(dip);
611 * dirent_del - Delete a dirent
612 * @dip: The GFS2 inode
614 * @prev: The previous dirent
615 * @cur: The current dirent
619 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
620 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
622 u16 cur_rec_len, prev_rec_len;
624 if (!cur->de_inum.no_addr) {
625 gfs2_consist_inode(dip);
629 gfs2_trans_add_bh(dip->i_gl, bh, 1);
631 /* If there is no prev entry, this is the first entry in the block.
632 The de_rec_len is already as big as it needs to be. Just zero
633 out the inode number and return. */
636 cur->de_inum.no_addr = 0; /* No endianess worries */
640 /* Combine this dentry with the previous one. */
642 prev_rec_len = be16_to_cpu(prev->de_rec_len);
643 cur_rec_len = be16_to_cpu(cur->de_rec_len);
645 if ((char *)prev + prev_rec_len != (char *)cur)
646 gfs2_consist_inode(dip);
647 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
648 gfs2_consist_inode(dip);
650 prev_rec_len += cur_rec_len;
651 prev->de_rec_len = cpu_to_be16(prev_rec_len);
655 * Takes a dent from which to grab space as an argument. Returns the
656 * newly created dent.
658 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
659 struct gfs2_dirent *dent,
660 const struct qstr *name,
661 struct buffer_head *bh)
663 struct gfs2_inode *ip = GFS2_I(inode);
664 struct gfs2_dirent *ndent;
665 unsigned offset = 0, totlen;
667 if (dent->de_inum.no_addr)
668 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
669 totlen = be16_to_cpu(dent->de_rec_len);
670 BUG_ON(offset + name->len > totlen);
671 gfs2_trans_add_bh(ip->i_gl, bh, 1);
672 ndent = (struct gfs2_dirent *)((char *)dent + offset);
673 dent->de_rec_len = cpu_to_be16(offset);
674 gfs2_qstr2dirent(name, totlen - offset, ndent);
678 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
679 struct buffer_head *bh,
680 const struct qstr *name)
682 struct gfs2_dirent *dent;
683 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
684 gfs2_dirent_find_space, name, NULL);
685 if (!dent || IS_ERR(dent))
687 return gfs2_init_dirent(inode, dent, name, bh);
690 static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
691 struct buffer_head **bhp)
695 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
696 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
697 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
705 * get_leaf_nr - Get a leaf number associated with the index
706 * @dip: The GFS2 inode
710 * Returns: 0 on success, error code otherwise
713 static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
719 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
720 index * sizeof(__be64),
722 if (error != sizeof(u64))
723 return (error < 0) ? error : -EIO;
725 *leaf_out = be64_to_cpu(leaf_no);
730 static int get_first_leaf(struct gfs2_inode *dip, u32 index,
731 struct buffer_head **bh_out)
736 error = get_leaf_nr(dip, index, &leaf_no);
738 error = get_leaf(dip, leaf_no, bh_out);
743 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
744 const struct qstr *name,
746 struct buffer_head **pbh)
748 struct buffer_head *bh;
749 struct gfs2_dirent *dent;
750 struct gfs2_inode *ip = GFS2_I(inode);
753 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
754 struct gfs2_leaf *leaf;
755 unsigned hsize = 1 << ip->i_di.di_depth;
758 if (hsize * sizeof(u64) != ip->i_di.di_size) {
759 gfs2_consist_inode(ip);
760 return ERR_PTR(-EIO);
763 index = name->hash >> (32 - ip->i_di.di_depth);
764 error = get_first_leaf(ip, index, &bh);
766 return ERR_PTR(error);
768 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
772 leaf = (struct gfs2_leaf *)bh->b_data;
773 ln = be64_to_cpu(leaf->lf_next);
778 error = get_leaf(ip, ln, &bh);
781 return error ? ERR_PTR(error) : NULL;
785 error = gfs2_meta_inode_buffer(ip, &bh);
787 return ERR_PTR(error);
788 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
790 if (unlikely(dent == NULL || IS_ERR(dent))) {
798 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
800 struct gfs2_inode *ip = GFS2_I(inode);
801 u64 bn = gfs2_alloc_meta(ip);
802 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
803 struct gfs2_leaf *leaf;
804 struct gfs2_dirent *dent;
805 struct qstr name = { .name = "", .len = 0, .hash = 0 };
809 gfs2_trans_add_bh(ip->i_gl, bh, 1);
810 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
811 leaf = (struct gfs2_leaf *)bh->b_data;
812 leaf->lf_depth = cpu_to_be16(depth);
813 leaf->lf_entries = 0;
814 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
816 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
817 dent = (struct gfs2_dirent *)(leaf+1);
818 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
824 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
825 * @dip: The GFS2 inode
827 * Returns: 0 on success, error code otherwise
830 static int dir_make_exhash(struct inode *inode)
832 struct gfs2_inode *dip = GFS2_I(inode);
833 struct gfs2_sbd *sdp = GFS2_SB(inode);
834 struct gfs2_dirent *dent;
836 struct buffer_head *bh, *dibh;
837 struct gfs2_leaf *leaf;
844 error = gfs2_meta_inode_buffer(dip, &dibh);
848 /* Turn over a new leaf */
850 leaf = new_leaf(inode, &bh, 0);
855 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
856 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
860 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
861 sizeof(struct gfs2_dinode));
863 /* Find last entry */
866 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
867 sizeof(struct gfs2_leaf);
868 args.name = bh->b_data;
869 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
870 gfs2_dirent_last, &args, NULL);
879 return PTR_ERR(dent);
882 /* Adjust the last dirent's record length
883 (Remember that dent still points to the last entry.) */
885 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
886 sizeof(struct gfs2_dinode) -
887 sizeof(struct gfs2_leaf));
891 /* We're done with the new leaf block, now setup the new
894 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
895 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
897 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
899 for (x = sdp->sd_hash_ptrs; x--; lp++)
900 *lp = cpu_to_be64(bn);
902 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
903 dip->i_di.di_blocks++;
904 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
905 dip->i_di.di_payload_format = 0;
907 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
908 dip->i_di.di_depth = y;
910 gfs2_dinode_out(dip, dibh->b_data);
918 * dir_split_leaf - Split a leaf block into two
919 * @dip: The GFS2 inode
923 * Returns: 0 on success, error code on failure
926 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
928 struct gfs2_inode *dip = GFS2_I(inode);
929 struct buffer_head *nbh, *obh, *dibh;
930 struct gfs2_leaf *nleaf, *oleaf;
931 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
932 u32 start, len, half_len, divider;
939 index = name->hash >> (32 - dip->i_di.di_depth);
940 error = get_leaf_nr(dip, index, &leaf_no);
944 /* Get the old leaf block */
945 error = get_leaf(dip, leaf_no, &obh);
949 oleaf = (struct gfs2_leaf *)obh->b_data;
950 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
952 return 1; /* can't split */
955 gfs2_trans_add_bh(dip->i_gl, obh, 1);
957 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
964 /* Compute the start and len of leaf pointers in the hash table. */
965 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
968 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
969 gfs2_consist_inode(dip);
974 start = (index & ~(len - 1));
976 /* Change the pointers.
977 Don't bother distinguishing stuffed from non-stuffed.
978 This code is complicated enough already. */
979 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
980 /* Change the pointers */
981 for (x = 0; x < half_len; x++)
982 lp[x] = cpu_to_be64(bn);
984 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
985 half_len * sizeof(u64));
986 if (error != half_len * sizeof(u64)) {
994 /* Compute the divider */
995 divider = (start + half_len) << (32 - dip->i_di.di_depth);
997 /* Copy the entries */
998 dirent_first(dip, obh, &dent);
1002 if (dirent_next(dip, obh, &next))
1005 if (dent->de_inum.no_addr &&
1006 be32_to_cpu(dent->de_hash) < divider) {
1008 str.name = (char*)(dent+1);
1009 str.len = be16_to_cpu(dent->de_name_len);
1010 str.hash = be32_to_cpu(dent->de_hash);
1011 new = gfs2_dirent_alloc(inode, nbh, &str);
1013 error = PTR_ERR(new);
1017 new->de_inum = dent->de_inum; /* No endian worries */
1018 new->de_type = dent->de_type; /* No endian worries */
1019 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
1021 dirent_del(dip, obh, prev, dent);
1023 if (!oleaf->lf_entries)
1024 gfs2_consist_inode(dip);
1025 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
1037 oleaf->lf_depth = nleaf->lf_depth;
1039 error = gfs2_meta_inode_buffer(dip, &dibh);
1040 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1041 dip->i_di.di_blocks++;
1042 gfs2_dinode_out(dip, dibh->b_data);
1061 * dir_double_exhash - Double size of ExHash table
1062 * @dip: The GFS2 dinode
1064 * Returns: 0 on success, error code on failure
1067 static int dir_double_exhash(struct gfs2_inode *dip)
1069 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1070 struct buffer_head *dibh;
1078 hsize = 1 << dip->i_di.di_depth;
1079 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1080 gfs2_consist_inode(dip);
1084 /* Allocate both the "from" and "to" buffers in one big chunk */
1086 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1088 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1089 error = gfs2_dir_read_data(dip, (char *)buf,
1090 block * sdp->sd_hash_bsize,
1091 sdp->sd_hash_bsize, 1);
1092 if (error != sdp->sd_hash_bsize) {
1099 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
1101 for (x = sdp->sd_hash_ptrs; x--; from++) {
1102 *to++ = *from; /* No endianess worries */
1106 error = gfs2_dir_write_data(dip,
1107 (char *)buf + sdp->sd_hash_bsize,
1108 block * sdp->sd_sb.sb_bsize,
1109 sdp->sd_sb.sb_bsize);
1110 if (error != sdp->sd_sb.sb_bsize) {
1119 error = gfs2_meta_inode_buffer(dip, &dibh);
1120 if (!gfs2_assert_withdraw(sdp, !error)) {
1121 dip->i_di.di_depth++;
1122 gfs2_dinode_out(dip, dibh->b_data);
1134 * compare_dents - compare directory entries by hash value
1138 * When comparing the hash entries of @a to @b:
1144 static int compare_dents(const void *a, const void *b)
1146 const struct gfs2_dirent *dent_a, *dent_b;
1150 dent_a = *(const struct gfs2_dirent **)a;
1151 hash_a = be32_to_cpu(dent_a->de_hash);
1153 dent_b = *(const struct gfs2_dirent **)b;
1154 hash_b = be32_to_cpu(dent_b->de_hash);
1156 if (hash_a > hash_b)
1158 else if (hash_a < hash_b)
1161 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1162 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1166 else if (len_a < len_b)
1169 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
1176 * do_filldir_main - read out directory entries
1177 * @dip: The GFS2 inode
1178 * @offset: The offset in the file to read from
1179 * @opaque: opaque data to pass to filldir
1180 * @filldir: The function to pass entries to
1181 * @darr: an array of struct gfs2_dirent pointers to read
1182 * @entries: the number of entries in darr
1183 * @copied: pointer to int that's non-zero if a entry has been copied out
1185 * Jump through some hoops to make sure that if there are hash collsions,
1186 * they are read out at the beginning of a buffer. We want to minimize
1187 * the possibility that they will fall into different readdir buffers or
1188 * that someone will want to seek to that location.
1190 * Returns: errno, >0 on exception from filldir
1193 static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
1194 void *opaque, gfs2_filldir_t filldir,
1195 const struct gfs2_dirent **darr, u32 entries,
1198 const struct gfs2_dirent *dent, *dent_next;
1199 struct gfs2_inum_host inum;
1205 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1207 dent_next = darr[0];
1208 off_next = be32_to_cpu(dent_next->de_hash);
1209 off_next = gfs2_disk_hash2offset(off_next);
1211 for (x = 0, y = 1; x < entries; x++, y++) {
1216 dent_next = darr[y];
1217 off_next = be32_to_cpu(dent_next->de_hash);
1218 off_next = gfs2_disk_hash2offset(off_next);
1224 if (off_next == off) {
1225 if (*copied && !run)
1236 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1238 error = filldir(opaque, (const char *)(dent + 1),
1239 be16_to_cpu(dent->de_name_len),
1241 be16_to_cpu(dent->de_type));
1248 /* Increment the *offset by one, so the next time we come into the
1249 do_filldir fxn, we get the next entry instead of the last one in the
1257 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1258 gfs2_filldir_t filldir, int *copied,
1259 unsigned *depth, u64 leaf_no)
1261 struct gfs2_inode *ip = GFS2_I(inode);
1262 struct buffer_head *bh;
1263 struct gfs2_leaf *lf;
1264 unsigned entries = 0;
1265 unsigned leaves = 0;
1266 const struct gfs2_dirent **darr, *dent;
1267 struct dirent_gather g;
1268 struct buffer_head **larr;
1274 error = get_leaf(ip, lfn, &bh);
1277 lf = (struct gfs2_leaf *)bh->b_data;
1279 *depth = be16_to_cpu(lf->lf_depth);
1280 entries += be16_to_cpu(lf->lf_entries);
1282 lfn = be64_to_cpu(lf->lf_next);
1290 larr = vmalloc((leaves + entries) * sizeof(void *));
1293 darr = (const struct gfs2_dirent **)(larr + leaves);
1299 error = get_leaf(ip, lfn, &bh);
1302 lf = (struct gfs2_leaf *)bh->b_data;
1303 lfn = be64_to_cpu(lf->lf_next);
1304 if (lf->lf_entries) {
1305 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1306 gfs2_dirent_gather, NULL, &g);
1307 error = PTR_ERR(dent);
1318 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1321 for(i = 0; i < leaf; i++)
1329 * dir_e_read - Reads the entries from a directory into a filldir buffer
1330 * @dip: dinode pointer
1331 * @offset: the hash of the last entry read shifted to the right once
1332 * @opaque: buffer for the filldir function to fill
1333 * @filldir: points to the filldir function to use
1338 static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
1339 gfs2_filldir_t filldir)
1341 struct gfs2_inode *dip = GFS2_I(inode);
1342 struct gfs2_sbd *sdp = GFS2_SB(inode);
1344 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1351 hsize = 1 << dip->i_di.di_depth;
1352 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1353 gfs2_consist_inode(dip);
1357 hash = gfs2_dir_offset2hash(*offset);
1358 index = hash >> (32 - dip->i_di.di_depth);
1360 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1364 while (index < hsize) {
1365 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1366 ht_offset = index - lp_offset;
1368 if (ht_offset_cur != ht_offset) {
1369 error = gfs2_dir_read_data(dip, (char *)lp,
1370 ht_offset * sizeof(__be64),
1371 sdp->sd_hash_bsize, 1);
1372 if (error != sdp->sd_hash_bsize) {
1377 ht_offset_cur = ht_offset;
1380 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1382 be64_to_cpu(lp[lp_offset]));
1386 len = 1 << (dip->i_di.di_depth - depth);
1387 index = (index & ~(len - 1)) + len;
1397 int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
1398 gfs2_filldir_t filldir)
1400 struct gfs2_inode *dip = GFS2_I(inode);
1401 struct dirent_gather g;
1402 const struct gfs2_dirent **darr, *dent;
1403 struct buffer_head *dibh;
1407 if (!dip->i_di.di_entries)
1410 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1411 return dir_e_read(inode, offset, opaque, filldir);
1413 if (!gfs2_is_stuffed(dip)) {
1414 gfs2_consist_inode(dip);
1418 error = gfs2_meta_inode_buffer(dip, &dibh);
1423 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1428 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1429 gfs2_dirent_gather, NULL, &g);
1431 error = PTR_ERR(dent);
1434 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1435 dip->i_di.di_entries, &copied);
1449 * gfs2_dir_search - Search a directory
1450 * @dip: The GFS2 inode
1454 * This routine searches a directory for a file or another directory.
1455 * Assumes a glock is held on dip.
1460 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1461 struct gfs2_inum_host *inum, unsigned int *type)
1463 struct buffer_head *bh;
1464 struct gfs2_dirent *dent;
1466 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1469 return PTR_ERR(dent);
1471 gfs2_inum_in(inum, (char *)&dent->de_inum);
1473 *type = be16_to_cpu(dent->de_type);
1480 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1482 struct buffer_head *bh, *obh;
1483 struct gfs2_inode *ip = GFS2_I(inode);
1484 struct gfs2_leaf *leaf, *oleaf;
1489 index = name->hash >> (32 - ip->i_di.di_depth);
1490 error = get_first_leaf(ip, index, &obh);
1494 oleaf = (struct gfs2_leaf *)obh->b_data;
1495 bn = be64_to_cpu(oleaf->lf_next);
1499 error = get_leaf(ip, bn, &obh);
1504 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1506 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1511 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1515 error = gfs2_meta_inode_buffer(ip, &bh);
1518 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1519 ip->i_di.di_blocks++;
1520 gfs2_dinode_out(ip, bh->b_data);
1526 * gfs2_dir_add - Add new filename into directory
1527 * @dip: The GFS2 inode
1528 * @filename: The new name
1529 * @inode: The inode number of the entry
1530 * @type: The type of the entry
1532 * Returns: 0 on success, error code on failure
1535 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1536 const struct gfs2_inum_host *inum, unsigned type)
1538 struct gfs2_inode *ip = GFS2_I(inode);
1539 struct buffer_head *bh;
1540 struct gfs2_dirent *dent;
1541 struct gfs2_leaf *leaf;
1545 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1549 return PTR_ERR(dent);
1550 dent = gfs2_init_dirent(inode, dent, name, bh);
1551 gfs2_inum_out(inum, (char *)&dent->de_inum);
1552 dent->de_type = cpu_to_be16(type);
1553 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1554 leaf = (struct gfs2_leaf *)bh->b_data;
1555 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1558 error = gfs2_meta_inode_buffer(ip, &bh);
1561 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1562 ip->i_di.di_entries++;
1563 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1564 gfs2_dinode_out(ip, bh->b_data);
1569 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1570 error = dir_make_exhash(inode);
1575 error = dir_split_leaf(inode, name);
1580 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1581 error = dir_double_exhash(ip);
1584 error = dir_split_leaf(inode, name);
1590 error = dir_new_leaf(inode, name);
1601 * gfs2_dir_del - Delete a directory entry
1602 * @dip: The GFS2 inode
1603 * @filename: The filename
1605 * Returns: 0 on success, error code on failure
1608 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1610 struct gfs2_dirent *dent, *prev = NULL;
1611 struct buffer_head *bh;
1614 /* Returns _either_ the entry (if its first in block) or the
1615 previous entry otherwise */
1616 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1618 gfs2_consist_inode(dip);
1622 gfs2_consist_inode(dip);
1623 return PTR_ERR(dent);
1625 /* If not first in block, adjust pointers accordingly */
1626 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1628 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1631 dirent_del(dip, bh, prev, dent);
1632 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1633 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1634 u16 entries = be16_to_cpu(leaf->lf_entries);
1636 gfs2_consist_inode(dip);
1637 leaf->lf_entries = cpu_to_be16(--entries);
1641 error = gfs2_meta_inode_buffer(dip, &bh);
1645 if (!dip->i_di.di_entries)
1646 gfs2_consist_inode(dip);
1647 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1648 dip->i_di.di_entries--;
1649 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1650 gfs2_dinode_out(dip, bh->b_data);
1652 mark_inode_dirty(&dip->i_inode);
1658 * gfs2_dir_mvino - Change inode number of directory entry
1659 * @dip: The GFS2 inode
1663 * This routine changes the inode number of a directory entry. It's used
1664 * by rename to change ".." when a directory is moved.
1665 * Assumes a glock is held on dvp.
1670 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1671 struct gfs2_inum_host *inum, unsigned int new_type)
1673 struct buffer_head *bh;
1674 struct gfs2_dirent *dent;
1677 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1679 gfs2_consist_inode(dip);
1683 return PTR_ERR(dent);
1685 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1686 gfs2_inum_out(inum, (char *)&dent->de_inum);
1687 dent->de_type = cpu_to_be16(new_type);
1689 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1691 error = gfs2_meta_inode_buffer(dip, &bh);
1694 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1697 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1698 gfs2_dinode_out(dip, bh->b_data);
1704 * foreach_leaf - call a function for each leaf in a directory
1705 * @dip: the directory
1706 * @lc: the function to call for each each
1707 * @data: private data to pass to it
1712 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1714 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1715 struct buffer_head *bh;
1716 struct gfs2_leaf *leaf;
1718 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1724 hsize = 1 << dip->i_di.di_depth;
1725 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1726 gfs2_consist_inode(dip);
1730 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1734 while (index < hsize) {
1735 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1736 ht_offset = index - lp_offset;
1738 if (ht_offset_cur != ht_offset) {
1739 error = gfs2_dir_read_data(dip, (char *)lp,
1740 ht_offset * sizeof(__be64),
1741 sdp->sd_hash_bsize, 1);
1742 if (error != sdp->sd_hash_bsize) {
1747 ht_offset_cur = ht_offset;
1750 leaf_no = be64_to_cpu(lp[lp_offset]);
1752 error = get_leaf(dip, leaf_no, &bh);
1755 leaf = (struct gfs2_leaf *)bh->b_data;
1756 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1759 error = lc(dip, index, len, leaf_no, data);
1763 index = (index & ~(len - 1)) + len;
1768 if (index != hsize) {
1769 gfs2_consist_inode(dip);
1780 * leaf_dealloc - Deallocate a directory leaf
1781 * @dip: the directory
1782 * @index: the hash table offset in the directory
1783 * @len: the number of pointers to this leaf
1784 * @leaf_no: the leaf number
1790 static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1791 u64 leaf_no, void *data)
1793 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1794 struct gfs2_leaf *tmp_leaf;
1795 struct gfs2_rgrp_list rlist;
1796 struct buffer_head *bh, *dibh;
1798 unsigned int rg_blocks = 0, l_blocks = 0;
1800 unsigned int x, size = len * sizeof(u64);
1803 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1805 ht = kzalloc(size, GFP_KERNEL);
1809 gfs2_alloc_get(dip);
1811 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1815 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1819 /* Count the number of leaves */
1821 for (blk = leaf_no; blk; blk = nblk) {
1822 error = get_leaf(dip, blk, &bh);
1825 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1826 nblk = be64_to_cpu(tmp_leaf->lf_next);
1829 gfs2_rlist_add(sdp, &rlist, blk);
1833 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1835 for (x = 0; x < rlist.rl_rgrps; x++) {
1836 struct gfs2_rgrpd *rgd;
1837 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1838 rg_blocks += rgd->rd_ri.ri_length;
1841 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1845 error = gfs2_trans_begin(sdp,
1846 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1847 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1849 goto out_rg_gunlock;
1851 for (blk = leaf_no; blk; blk = nblk) {
1852 error = get_leaf(dip, blk, &bh);
1855 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1856 nblk = be64_to_cpu(tmp_leaf->lf_next);
1859 gfs2_free_meta(dip, blk, 1);
1861 if (!dip->i_di.di_blocks)
1862 gfs2_consist_inode(dip);
1863 dip->i_di.di_blocks--;
1866 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
1867 if (error != size) {
1873 error = gfs2_meta_inode_buffer(dip, &dibh);
1877 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1878 gfs2_dinode_out(dip, dibh->b_data);
1882 gfs2_trans_end(sdp);
1884 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1886 gfs2_rlist_free(&rlist);
1887 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1889 gfs2_quota_unhold(dip);
1891 gfs2_alloc_put(dip);
1897 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1898 * @dip: the directory
1900 * Dealloc all on-disk directory leaves to FREEMETA state
1901 * Change on-disk inode type to "regular file"
1906 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1908 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1909 struct buffer_head *bh;
1912 /* Dealloc on-disk leaves to FREEMETA state */
1913 error = foreach_leaf(dip, leaf_dealloc, NULL);
1917 /* Make this a regular file in case we crash.
1918 (We don't want to free these blocks a second time.) */
1920 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1924 error = gfs2_meta_inode_buffer(dip, &bh);
1926 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1927 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1928 cpu_to_be32(S_IFREG);
1932 gfs2_trans_end(sdp);
1938 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1939 * @ip: the file being written to
1940 * @filname: the filename that's going to be added
1942 * Returns: 1 if alloc required, 0 if not, -ve on error
1945 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
1947 struct gfs2_dirent *dent;
1948 struct buffer_head *bh;
1950 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1955 return PTR_ERR(dent);