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>
66 #include "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,
85 u32 index, u32 len, u64 leaf_no,
89 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
90 struct buffer_head **bhp)
92 struct buffer_head *bh;
94 bh = gfs2_meta_new(ip->i_gl, block);
95 gfs2_trans_add_bh(ip->i_gl, bh, 1);
96 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
97 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
102 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
103 struct buffer_head **bhp)
105 struct buffer_head *bh;
108 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
111 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
119 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
120 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->i_di, 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);
224 o = sizeof(struct gfs2_meta_header);
228 error = gfs2_meta_inode_buffer(ip, &dibh);
232 if (ip->i_di.di_size < offset + copied)
233 ip->i_di.di_size = offset + copied;
234 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
236 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
237 gfs2_dinode_out(&ip->i_di, dibh->b_data);
247 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
248 unsigned int offset, unsigned int size)
250 struct buffer_head *dibh;
253 error = gfs2_meta_inode_buffer(ip, &dibh);
255 offset += sizeof(struct gfs2_dinode);
256 memcpy(buf, dibh->b_data + offset, size);
260 return (error) ? error : size;
265 * gfs2_dir_read_data - Read a data from a directory inode
266 * @ip: The GFS2 Inode
267 * @buf: The buffer to place result into
268 * @offset: File offset to begin jdata_readng from
269 * @size: Amount of data to transfer
271 * Returns: The amount of data actually copied or the error
273 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
274 u64 offset, unsigned int size)
276 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
283 if (offset >= ip->i_di.di_size)
286 if ((offset + size) > ip->i_di.di_size)
287 size = ip->i_di.di_size - offset;
292 if (gfs2_is_stuffed(ip))
293 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
296 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
300 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
302 while (copied < size) {
304 struct buffer_head *bh;
307 amount = size - copied;
308 if (amount > sdp->sd_sb.sb_bsize - o)
309 amount = sdp->sd_sb.sb_bsize - o;
313 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
320 gfs2_meta_ra(ip->i_gl, dblock, extlen);
324 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
326 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
334 memcpy(buf, bh->b_data + o, amount);
343 o = sizeof(struct gfs2_meta_header);
348 return (copied) ? copied : error;
351 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
352 const struct qstr *name,
355 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
356 const struct qstr *name, int ret)
358 if (dent->de_inum.no_addr != 0 &&
359 be32_to_cpu(dent->de_hash) == name->hash &&
360 be16_to_cpu(dent->de_name_len) == name->len &&
361 memcmp((char *)(dent+1), name->name, name->len) == 0)
366 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
367 const struct qstr *name,
370 return __gfs2_dirent_find(dent, name, 1);
373 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
374 const struct qstr *name,
377 return __gfs2_dirent_find(dent, name, 2);
381 * name->name holds ptr to start of block.
382 * name->len holds size of block.
384 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
385 const struct qstr *name,
388 const char *start = name->name;
389 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
390 if (name->len == (end - start))
395 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
396 const struct qstr *name,
399 unsigned required = GFS2_DIRENT_SIZE(name->len);
400 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
401 unsigned totlen = be16_to_cpu(dent->de_rec_len);
403 if (!dent->de_inum.no_addr)
404 actual = GFS2_DIRENT_SIZE(0);
405 if ((totlen - actual) >= required)
410 struct dirent_gather {
411 const struct gfs2_dirent **pdent;
415 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
416 const struct qstr *name,
419 struct dirent_gather *g = opaque;
420 if (dent->de_inum.no_addr) {
421 g->pdent[g->offset++] = dent;
427 * Other possible things to check:
428 * - Inode located within filesystem size (and on valid block)
429 * - Valid directory entry type
430 * Not sure how heavy-weight we want to make this... could also check
431 * hash is correct for example, but that would take a lot of extra time.
432 * For now the most important thing is to check that the various sizes
435 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
436 unsigned int size, unsigned int len, int first)
438 const char *msg = "gfs2_dirent too small";
439 if (unlikely(size < sizeof(struct gfs2_dirent)))
441 msg = "gfs2_dirent misaligned";
442 if (unlikely(offset & 0x7))
444 msg = "gfs2_dirent points beyond end of block";
445 if (unlikely(offset + size > len))
447 msg = "zero inode number";
448 if (unlikely(!first && !dent->de_inum.no_addr))
450 msg = "name length is greater than space in dirent";
451 if (dent->de_inum.no_addr &&
452 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
457 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
458 first ? "first in block" : "not first in block");
462 static int gfs2_dirent_offset(const void *buf)
464 const struct gfs2_meta_header *h = buf;
469 switch(be32_to_cpu(h->mh_type)) {
470 case GFS2_METATYPE_LF:
471 offset = sizeof(struct gfs2_leaf);
473 case GFS2_METATYPE_DI:
474 offset = sizeof(struct gfs2_dinode);
481 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
482 be32_to_cpu(h->mh_type));
486 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
488 unsigned int len, gfs2_dscan_t scan,
489 const struct qstr *name,
492 struct gfs2_dirent *dent, *prev;
497 ret = gfs2_dirent_offset(buf);
503 dent = (struct gfs2_dirent *)(buf + offset);
504 size = be16_to_cpu(dent->de_rec_len);
505 if (gfs2_check_dirent(dent, offset, size, len, 1))
508 ret = scan(dent, name, opaque);
515 dent = (struct gfs2_dirent *)(buf + offset);
516 size = be16_to_cpu(dent->de_rec_len);
517 if (gfs2_check_dirent(dent, offset, size, len, 0))
527 return prev ? prev : dent;
534 gfs2_consist_inode(GFS2_I(inode));
535 return ERR_PTR(-EIO);
540 * dirent_first - Return the first dirent
541 * @dip: the directory
543 * @dent: Pointer to list of dirents
545 * return first dirent whether bh points to leaf or stuffed dinode
547 * Returns: IS_LEAF, IS_DINODE, or -errno
550 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
551 struct gfs2_dirent **dent)
553 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
555 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
556 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
558 *dent = (struct gfs2_dirent *)(bh->b_data +
559 sizeof(struct gfs2_leaf));
562 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
564 *dent = (struct gfs2_dirent *)(bh->b_data +
565 sizeof(struct gfs2_dinode));
571 * dirent_next - Next dirent
572 * @dip: the directory
574 * @dent: Pointer to list of dirents
576 * Returns: 0 on success, error code otherwise
579 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
580 struct gfs2_dirent **dent)
582 struct gfs2_dirent *tmp, *cur;
587 bh_end = bh->b_data + bh->b_size;
588 cur_rec_len = be16_to_cpu(cur->de_rec_len);
590 if ((char *)cur + cur_rec_len >= bh_end) {
591 if ((char *)cur + cur_rec_len > bh_end) {
592 gfs2_consist_inode(dip);
598 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
600 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
601 gfs2_consist_inode(dip);
605 if (cur_rec_len == 0) {
606 gfs2_consist_inode(dip);
610 /* Only the first dent could ever have de_inum.no_addr == 0 */
611 if (!tmp->de_inum.no_addr) {
612 gfs2_consist_inode(dip);
622 * dirent_del - Delete a dirent
623 * @dip: The GFS2 inode
625 * @prev: The previous dirent
626 * @cur: The current dirent
630 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
631 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
633 u16 cur_rec_len, prev_rec_len;
635 if (!cur->de_inum.no_addr) {
636 gfs2_consist_inode(dip);
640 gfs2_trans_add_bh(dip->i_gl, bh, 1);
642 /* If there is no prev entry, this is the first entry in the block.
643 The de_rec_len is already as big as it needs to be. Just zero
644 out the inode number and return. */
647 cur->de_inum.no_addr = 0; /* No endianess worries */
651 /* Combine this dentry with the previous one. */
653 prev_rec_len = be16_to_cpu(prev->de_rec_len);
654 cur_rec_len = be16_to_cpu(cur->de_rec_len);
656 if ((char *)prev + prev_rec_len != (char *)cur)
657 gfs2_consist_inode(dip);
658 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
659 gfs2_consist_inode(dip);
661 prev_rec_len += cur_rec_len;
662 prev->de_rec_len = cpu_to_be16(prev_rec_len);
666 * Takes a dent from which to grab space as an argument. Returns the
667 * newly created dent.
669 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
670 struct gfs2_dirent *dent,
671 const struct qstr *name,
672 struct buffer_head *bh)
674 struct gfs2_inode *ip = GFS2_I(inode);
675 struct gfs2_dirent *ndent;
676 unsigned offset = 0, totlen;
678 if (dent->de_inum.no_addr)
679 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
680 totlen = be16_to_cpu(dent->de_rec_len);
681 BUG_ON(offset + name->len > totlen);
682 gfs2_trans_add_bh(ip->i_gl, bh, 1);
683 ndent = (struct gfs2_dirent *)((char *)dent + offset);
684 dent->de_rec_len = cpu_to_be16(offset);
685 gfs2_qstr2dirent(name, totlen - offset, ndent);
689 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
690 struct buffer_head *bh,
691 const struct qstr *name)
693 struct gfs2_dirent *dent;
694 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
695 gfs2_dirent_find_space, name, NULL);
696 if (!dent || IS_ERR(dent))
698 return gfs2_init_dirent(inode, dent, name, bh);
701 static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
702 struct buffer_head **bhp)
706 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
707 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
708 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
716 * get_leaf_nr - Get a leaf number associated with the index
717 * @dip: The GFS2 inode
721 * Returns: 0 on success, error code otherwise
724 static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
730 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
733 if (error != sizeof(u64))
734 return (error < 0) ? error : -EIO;
736 *leaf_out = be64_to_cpu(leaf_no);
741 static int get_first_leaf(struct gfs2_inode *dip, u32 index,
742 struct buffer_head **bh_out)
747 error = get_leaf_nr(dip, index, &leaf_no);
749 error = get_leaf(dip, leaf_no, bh_out);
754 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
755 const struct qstr *name,
757 struct buffer_head **pbh)
759 struct buffer_head *bh;
760 struct gfs2_dirent *dent;
761 struct gfs2_inode *ip = GFS2_I(inode);
764 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
765 struct gfs2_leaf *leaf;
766 unsigned hsize = 1 << ip->i_di.di_depth;
769 if (hsize * sizeof(u64) != ip->i_di.di_size) {
770 gfs2_consist_inode(ip);
771 return ERR_PTR(-EIO);
774 index = name->hash >> (32 - ip->i_di.di_depth);
775 error = get_first_leaf(ip, index, &bh);
777 return ERR_PTR(error);
779 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
783 leaf = (struct gfs2_leaf *)bh->b_data;
784 ln = be64_to_cpu(leaf->lf_next);
789 error = get_leaf(ip, ln, &bh);
792 return error ? ERR_PTR(error) : NULL;
796 error = gfs2_meta_inode_buffer(ip, &bh);
798 return ERR_PTR(error);
799 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
801 if (unlikely(dent == NULL || IS_ERR(dent))) {
809 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
811 struct gfs2_inode *ip = GFS2_I(inode);
812 u64 bn = gfs2_alloc_meta(ip);
813 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
814 struct gfs2_leaf *leaf;
815 struct gfs2_dirent *dent;
816 struct qstr name = { .name = "", .len = 0, .hash = 0 };
820 gfs2_trans_add_bh(ip->i_gl, bh, 1);
821 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
822 leaf = (struct gfs2_leaf *)bh->b_data;
823 leaf->lf_depth = cpu_to_be16(depth);
824 leaf->lf_entries = cpu_to_be16(0);
825 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
826 leaf->lf_next = cpu_to_be64(0);
827 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
828 dent = (struct gfs2_dirent *)(leaf+1);
829 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
835 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
836 * @dip: The GFS2 inode
838 * Returns: 0 on success, error code otherwise
841 static int dir_make_exhash(struct inode *inode)
843 struct gfs2_inode *dip = GFS2_I(inode);
844 struct gfs2_sbd *sdp = GFS2_SB(inode);
845 struct gfs2_dirent *dent;
847 struct buffer_head *bh, *dibh;
848 struct gfs2_leaf *leaf;
854 error = gfs2_meta_inode_buffer(dip, &dibh);
858 /* Turn over a new leaf */
860 leaf = new_leaf(inode, &bh, 0);
865 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
866 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
870 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
871 sizeof(struct gfs2_dinode));
873 /* Find last entry */
876 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
877 sizeof(struct gfs2_leaf);
878 args.name = bh->b_data;
879 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
880 gfs2_dirent_last, &args, NULL);
889 return PTR_ERR(dent);
892 /* Adjust the last dirent's record length
893 (Remember that dent still points to the last entry.) */
895 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
896 sizeof(struct gfs2_dinode) -
897 sizeof(struct gfs2_leaf));
901 /* We're done with the new leaf block, now setup the new
904 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
905 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
907 lp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
909 for (x = sdp->sd_hash_ptrs; x--; lp++)
910 *lp = cpu_to_be64(bn);
912 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
913 dip->i_di.di_blocks++;
914 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
915 dip->i_di.di_payload_format = 0;
917 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
918 dip->i_di.di_depth = y;
920 gfs2_dinode_out(&dip->i_di, dibh->b_data);
928 * dir_split_leaf - Split a leaf block into two
929 * @dip: The GFS2 inode
933 * Returns: 0 on success, error code on failure
936 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
938 struct gfs2_inode *dip = GFS2_I(inode);
939 struct buffer_head *nbh, *obh, *dibh;
940 struct gfs2_leaf *nleaf, *oleaf;
941 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
942 u32 start, len, half_len, divider;
943 u64 bn, *lp, leaf_no;
948 index = name->hash >> (32 - dip->i_di.di_depth);
949 error = get_leaf_nr(dip, index, &leaf_no);
953 /* Get the old leaf block */
954 error = get_leaf(dip, leaf_no, &obh);
958 oleaf = (struct gfs2_leaf *)obh->b_data;
959 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
961 return 1; /* can't split */
964 gfs2_trans_add_bh(dip->i_gl, obh, 1);
966 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
973 /* Compute the start and len of leaf pointers in the hash table. */
974 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
977 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
978 gfs2_consist_inode(dip);
983 start = (index & ~(len - 1));
985 /* Change the pointers.
986 Don't bother distinguishing stuffed from non-stuffed.
987 This code is complicated enough already. */
988 lp = kmalloc(half_len * sizeof(u64), GFP_NOFS | __GFP_NOFAIL);
989 /* Change the pointers */
990 for (x = 0; x < half_len; x++)
991 lp[x] = cpu_to_be64(bn);
993 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
994 half_len * sizeof(u64));
995 if (error != half_len * sizeof(u64)) {
1003 /* Compute the divider */
1004 divider = (start + half_len) << (32 - dip->i_di.di_depth);
1006 /* Copy the entries */
1007 dirent_first(dip, obh, &dent);
1011 if (dirent_next(dip, obh, &next))
1014 if (dent->de_inum.no_addr &&
1015 be32_to_cpu(dent->de_hash) < divider) {
1017 str.name = (char*)(dent+1);
1018 str.len = be16_to_cpu(dent->de_name_len);
1019 str.hash = be32_to_cpu(dent->de_hash);
1020 new = gfs2_dirent_alloc(inode, nbh, &str);
1022 error = PTR_ERR(new);
1026 new->de_inum = dent->de_inum; /* No endian worries */
1027 new->de_type = dent->de_type; /* No endian worries */
1028 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
1030 dirent_del(dip, obh, prev, dent);
1032 if (!oleaf->lf_entries)
1033 gfs2_consist_inode(dip);
1034 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
1046 oleaf->lf_depth = nleaf->lf_depth;
1048 error = gfs2_meta_inode_buffer(dip, &dibh);
1049 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1050 dip->i_di.di_blocks++;
1051 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1070 * dir_double_exhash - Double size of ExHash table
1071 * @dip: The GFS2 dinode
1073 * Returns: 0 on success, error code on failure
1076 static int dir_double_exhash(struct gfs2_inode *dip)
1078 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1079 struct buffer_head *dibh;
1087 hsize = 1 << dip->i_di.di_depth;
1088 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1089 gfs2_consist_inode(dip);
1093 /* Allocate both the "from" and "to" buffers in one big chunk */
1095 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1097 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1098 error = gfs2_dir_read_data(dip, (char *)buf,
1099 block * sdp->sd_hash_bsize,
1100 sdp->sd_hash_bsize);
1101 if (error != sdp->sd_hash_bsize) {
1108 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
1110 for (x = sdp->sd_hash_ptrs; x--; from++) {
1111 *to++ = *from; /* No endianess worries */
1115 error = gfs2_dir_write_data(dip,
1116 (char *)buf + sdp->sd_hash_bsize,
1117 block * sdp->sd_sb.sb_bsize,
1118 sdp->sd_sb.sb_bsize);
1119 if (error != sdp->sd_sb.sb_bsize) {
1128 error = gfs2_meta_inode_buffer(dip, &dibh);
1129 if (!gfs2_assert_withdraw(sdp, !error)) {
1130 dip->i_di.di_depth++;
1131 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1143 * compare_dents - compare directory entries by hash value
1147 * When comparing the hash entries of @a to @b:
1153 static int compare_dents(const void *a, const void *b)
1155 struct gfs2_dirent *dent_a, *dent_b;
1159 dent_a = *(struct gfs2_dirent **)a;
1160 hash_a = be32_to_cpu(dent_a->de_hash);
1162 dent_b = *(struct gfs2_dirent **)b;
1163 hash_b = be32_to_cpu(dent_b->de_hash);
1165 if (hash_a > hash_b)
1167 else if (hash_a < hash_b)
1170 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1171 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1175 else if (len_a < len_b)
1178 ret = memcmp((char *)(dent_a + 1),
1179 (char *)(dent_b + 1),
1187 * do_filldir_main - read out directory entries
1188 * @dip: The GFS2 inode
1189 * @offset: The offset in the file to read from
1190 * @opaque: opaque data to pass to filldir
1191 * @filldir: The function to pass entries to
1192 * @darr: an array of struct gfs2_dirent pointers to read
1193 * @entries: the number of entries in darr
1194 * @copied: pointer to int that's non-zero if a entry has been copied out
1196 * Jump through some hoops to make sure that if there are hash collsions,
1197 * they are read out at the beginning of a buffer. We want to minimize
1198 * the possibility that they will fall into different readdir buffers or
1199 * that someone will want to seek to that location.
1201 * Returns: errno, >0 on exception from filldir
1204 static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
1205 void *opaque, gfs2_filldir_t filldir,
1206 const struct gfs2_dirent **darr, u32 entries,
1209 const struct gfs2_dirent *dent, *dent_next;
1210 struct gfs2_inum inum;
1216 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1218 dent_next = darr[0];
1219 off_next = be32_to_cpu(dent_next->de_hash);
1220 off_next = gfs2_disk_hash2offset(off_next);
1222 for (x = 0, y = 1; x < entries; x++, y++) {
1227 dent_next = darr[y];
1228 off_next = be32_to_cpu(dent_next->de_hash);
1229 off_next = gfs2_disk_hash2offset(off_next);
1235 if (off_next == off) {
1236 if (*copied && !run)
1247 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1249 error = filldir(opaque, (char *)(dent + 1),
1250 be16_to_cpu(dent->de_name_len),
1252 be16_to_cpu(dent->de_type));
1259 /* Increment the *offset by one, so the next time we come into the
1260 do_filldir fxn, we get the next entry instead of the last one in the
1268 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1269 gfs2_filldir_t filldir, int *copied,
1270 unsigned *depth, u64 leaf_no)
1272 struct gfs2_inode *ip = GFS2_I(inode);
1273 struct buffer_head *bh;
1274 struct gfs2_leaf *lf;
1275 unsigned entries = 0;
1276 unsigned leaves = 0;
1277 const struct gfs2_dirent **darr, *dent;
1278 struct dirent_gather g;
1279 struct buffer_head **larr;
1285 error = get_leaf(ip, lfn, &bh);
1288 lf = (struct gfs2_leaf *)bh->b_data;
1290 *depth = be16_to_cpu(lf->lf_depth);
1291 entries += be16_to_cpu(lf->lf_entries);
1293 lfn = be64_to_cpu(lf->lf_next);
1301 larr = vmalloc((leaves + entries) * sizeof(void*));
1304 darr = (const struct gfs2_dirent **)(larr + leaves);
1310 error = get_leaf(ip, lfn, &bh);
1313 lf = (struct gfs2_leaf *)bh->b_data;
1314 lfn = be64_to_cpu(lf->lf_next);
1315 if (lf->lf_entries) {
1316 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1317 gfs2_dirent_gather, NULL, &g);
1318 error = PTR_ERR(dent);
1329 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1332 for(i = 0; i < leaf; i++)
1340 * dir_e_read - Reads the entries from a directory into a filldir buffer
1341 * @dip: dinode pointer
1342 * @offset: the hash of the last entry read shifted to the right once
1343 * @opaque: buffer for the filldir function to fill
1344 * @filldir: points to the filldir function to use
1349 static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
1350 gfs2_filldir_t filldir)
1352 struct gfs2_inode *dip = GFS2_I(inode);
1353 struct gfs2_sbd *sdp = GFS2_SB(inode);
1355 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1362 hsize = 1 << dip->i_di.di_depth;
1363 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1364 gfs2_consist_inode(dip);
1368 hash = gfs2_dir_offset2hash(*offset);
1369 index = hash >> (32 - dip->i_di.di_depth);
1371 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1375 while (index < hsize) {
1376 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1377 ht_offset = index - lp_offset;
1379 if (ht_offset_cur != ht_offset) {
1380 error = gfs2_dir_read_data(dip, (char *)lp,
1381 ht_offset * sizeof(u64),
1382 sdp->sd_hash_bsize);
1383 if (error != sdp->sd_hash_bsize) {
1388 ht_offset_cur = ht_offset;
1391 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1393 be64_to_cpu(lp[lp_offset]));
1397 len = 1 << (dip->i_di.di_depth - depth);
1398 index = (index & ~(len - 1)) + len;
1408 int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
1409 gfs2_filldir_t filldir)
1411 struct gfs2_inode *dip = GFS2_I(inode);
1412 struct dirent_gather g;
1413 const struct gfs2_dirent **darr, *dent;
1414 struct buffer_head *dibh;
1418 if (!dip->i_di.di_entries)
1421 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1422 return dir_e_read(inode, offset, opaque, filldir);
1424 if (!gfs2_is_stuffed(dip)) {
1425 gfs2_consist_inode(dip);
1429 error = gfs2_meta_inode_buffer(dip, &dibh);
1434 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1439 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1440 gfs2_dirent_gather, NULL, &g);
1442 error = PTR_ERR(dent);
1445 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1446 dip->i_di.di_entries, &copied);
1460 * gfs2_dir_search - Search a directory
1461 * @dip: The GFS2 inode
1465 * This routine searches a directory for a file or another directory.
1466 * Assumes a glock is held on dip.
1471 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1472 struct gfs2_inum *inum, unsigned int *type)
1474 struct buffer_head *bh;
1475 struct gfs2_dirent *dent;
1477 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1480 return PTR_ERR(dent);
1482 gfs2_inum_in(inum, (char *)&dent->de_inum);
1484 *type = be16_to_cpu(dent->de_type);
1491 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1493 struct buffer_head *bh, *obh;
1494 struct gfs2_inode *ip = GFS2_I(inode);
1495 struct gfs2_leaf *leaf, *oleaf;
1500 index = name->hash >> (32 - ip->i_di.di_depth);
1501 error = get_first_leaf(ip, index, &obh);
1505 oleaf = (struct gfs2_leaf *)obh->b_data;
1506 bn = be64_to_cpu(oleaf->lf_next);
1510 error = get_leaf(ip, bn, &obh);
1515 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1517 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1522 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1526 error = gfs2_meta_inode_buffer(ip, &bh);
1529 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1530 ip->i_di.di_blocks++;
1531 gfs2_dinode_out(&ip->i_di, bh->b_data);
1537 * gfs2_dir_add - Add new filename into directory
1538 * @dip: The GFS2 inode
1539 * @filename: The new name
1540 * @inode: The inode number of the entry
1541 * @type: The type of the entry
1543 * Returns: 0 on success, error code on failure
1546 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1547 const struct gfs2_inum *inum, unsigned type)
1549 struct gfs2_inode *ip = GFS2_I(inode);
1550 struct buffer_head *bh;
1551 struct gfs2_dirent *dent;
1552 struct gfs2_leaf *leaf;
1556 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1560 return PTR_ERR(dent);
1561 dent = gfs2_init_dirent(inode, dent, name, bh);
1562 gfs2_inum_out(inum, (char *)&dent->de_inum);
1563 dent->de_type = cpu_to_be16(type);
1564 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1565 leaf = (struct gfs2_leaf *)bh->b_data;
1566 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1569 error = gfs2_meta_inode_buffer(ip, &bh);
1572 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1573 ip->i_di.di_entries++;
1574 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1575 gfs2_dinode_out(&ip->i_di, bh->b_data);
1580 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1581 error = dir_make_exhash(inode);
1586 error = dir_split_leaf(inode, name);
1591 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1592 error = dir_double_exhash(ip);
1595 error = dir_split_leaf(inode, name);
1601 error = dir_new_leaf(inode, name);
1612 * gfs2_dir_del - Delete a directory entry
1613 * @dip: The GFS2 inode
1614 * @filename: The filename
1616 * Returns: 0 on success, error code on failure
1619 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1621 struct gfs2_dirent *dent, *prev = NULL;
1622 struct buffer_head *bh;
1625 /* Returns _either_ the entry (if its first in block) or the
1626 previous entry otherwise */
1627 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1629 gfs2_consist_inode(dip);
1633 gfs2_consist_inode(dip);
1634 return PTR_ERR(dent);
1636 /* If not first in block, adjust pointers accordingly */
1637 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1639 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1642 dirent_del(dip, bh, prev, dent);
1643 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1644 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1645 u16 entries = be16_to_cpu(leaf->lf_entries);
1647 gfs2_consist_inode(dip);
1648 leaf->lf_entries = cpu_to_be16(--entries);
1652 error = gfs2_meta_inode_buffer(dip, &bh);
1656 if (!dip->i_di.di_entries)
1657 gfs2_consist_inode(dip);
1658 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1659 dip->i_di.di_entries--;
1660 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1661 gfs2_dinode_out(&dip->i_di, bh->b_data);
1663 mark_inode_dirty(&dip->i_inode);
1669 * gfs2_dir_mvino - Change inode number of directory entry
1670 * @dip: The GFS2 inode
1674 * This routine changes the inode number of a directory entry. It's used
1675 * by rename to change ".." when a directory is moved.
1676 * Assumes a glock is held on dvp.
1681 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1682 struct gfs2_inum *inum, unsigned int new_type)
1684 struct buffer_head *bh;
1685 struct gfs2_dirent *dent;
1688 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1690 gfs2_consist_inode(dip);
1694 return PTR_ERR(dent);
1696 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1697 gfs2_inum_out(inum, (char *)&dent->de_inum);
1698 dent->de_type = cpu_to_be16(new_type);
1700 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1702 error = gfs2_meta_inode_buffer(dip, &bh);
1705 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1708 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1709 gfs2_dinode_out(&dip->i_di, bh->b_data);
1715 * foreach_leaf - call a function for each leaf in a directory
1716 * @dip: the directory
1717 * @lc: the function to call for each each
1718 * @data: private data to pass to it
1723 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1725 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1726 struct buffer_head *bh;
1727 struct gfs2_leaf *leaf;
1729 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1735 hsize = 1 << dip->i_di.di_depth;
1736 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1737 gfs2_consist_inode(dip);
1741 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1745 while (index < hsize) {
1746 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1747 ht_offset = index - lp_offset;
1749 if (ht_offset_cur != ht_offset) {
1750 error = gfs2_dir_read_data(dip, (char *)lp,
1751 ht_offset * sizeof(u64),
1752 sdp->sd_hash_bsize);
1753 if (error != sdp->sd_hash_bsize) {
1758 ht_offset_cur = ht_offset;
1761 leaf_no = be64_to_cpu(lp[lp_offset]);
1763 error = get_leaf(dip, leaf_no, &bh);
1766 leaf = (struct gfs2_leaf *)bh->b_data;
1767 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1770 error = lc(dip, index, len, leaf_no, data);
1774 index = (index & ~(len - 1)) + len;
1779 if (index != hsize) {
1780 gfs2_consist_inode(dip);
1791 * leaf_dealloc - Deallocate a directory leaf
1792 * @dip: the directory
1793 * @index: the hash table offset in the directory
1794 * @len: the number of pointers to this leaf
1795 * @leaf_no: the leaf number
1801 static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1802 u64 leaf_no, void *data)
1804 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1805 struct gfs2_leaf *tmp_leaf;
1806 struct gfs2_rgrp_list rlist;
1807 struct buffer_head *bh, *dibh;
1809 unsigned int rg_blocks = 0, l_blocks = 0;
1811 unsigned int x, size = len * sizeof(u64);
1814 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1816 ht = kzalloc(size, GFP_KERNEL);
1820 gfs2_alloc_get(dip);
1822 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1826 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1830 /* Count the number of leaves */
1832 for (blk = leaf_no; blk; blk = nblk) {
1833 error = get_leaf(dip, blk, &bh);
1836 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1837 nblk = be64_to_cpu(tmp_leaf->lf_next);
1840 gfs2_rlist_add(sdp, &rlist, blk);
1844 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1846 for (x = 0; x < rlist.rl_rgrps; x++) {
1847 struct gfs2_rgrpd *rgd;
1848 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1849 rg_blocks += rgd->rd_ri.ri_length;
1852 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1856 error = gfs2_trans_begin(sdp,
1857 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1858 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1860 goto out_rg_gunlock;
1862 for (blk = leaf_no; blk; blk = nblk) {
1863 error = get_leaf(dip, blk, &bh);
1866 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1867 nblk = be64_to_cpu(tmp_leaf->lf_next);
1870 gfs2_free_meta(dip, blk, 1);
1872 if (!dip->i_di.di_blocks)
1873 gfs2_consist_inode(dip);
1874 dip->i_di.di_blocks--;
1877 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
1878 if (error != size) {
1884 error = gfs2_meta_inode_buffer(dip, &dibh);
1888 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1889 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1893 gfs2_trans_end(sdp);
1895 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1897 gfs2_rlist_free(&rlist);
1898 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1900 gfs2_quota_unhold(dip);
1902 gfs2_alloc_put(dip);
1908 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1909 * @dip: the directory
1911 * Dealloc all on-disk directory leaves to FREEMETA state
1912 * Change on-disk inode type to "regular file"
1917 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1919 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1920 struct buffer_head *bh;
1923 /* Dealloc on-disk leaves to FREEMETA state */
1924 error = foreach_leaf(dip, leaf_dealloc, NULL);
1928 /* Make this a regular file in case we crash.
1929 (We don't want to free these blocks a second time.) */
1931 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1935 error = gfs2_meta_inode_buffer(dip, &bh);
1937 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1938 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1939 cpu_to_be32(S_IFREG);
1943 gfs2_trans_end(sdp);
1949 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1950 * @ip: the file being written to
1951 * @filname: the filename that's going to be added
1953 * Returns: 1 if alloc required, 0 if not, -ve on error
1956 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
1958 struct gfs2_dirent *dent;
1959 struct buffer_head *bh;
1961 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1966 return PTR_ERR(dent);