2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.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 block
46 * pointer in the array that points to the same leaf. In fact, when a directory
47 * is first converted from linear to exhash, all of the pointers point to the
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/completion.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 <asm/semaphore.h>
67 #include "lm_interface.h"
79 #define IS_LEAF 1 /* Hashed (leaf) directory */
80 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
83 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
84 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
86 #define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
87 #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
90 typedef int (*leaf_call_t) (struct gfs2_inode *dip,
91 uint32_t index, uint32_t len, uint64_t leaf_no,
94 int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
95 struct buffer_head **bhp)
97 struct buffer_head *bh;
101 bh = gfs2_meta_new(ip->i_gl, block);
102 gfs2_trans_add_bh(ip->i_gl, bh, 1);
103 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
104 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
106 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT,
110 if (gfs2_metatype_check(ip->i_sbd, 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)
126 struct buffer_head *dibh;
129 error = gfs2_meta_inode_buffer(ip, &dibh);
133 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
134 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
135 if (ip->i_di.di_size < offset + size)
136 ip->i_di.di_size = offset + size;
137 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
138 gfs2_dinode_out(&ip->i_di, dibh->b_data);
148 * gfs2_dir_write_data - Write directory information to the inode
149 * @ip: The GFS2 inode
150 * @buf: The buffer containing information to be written
151 * @offset: The file offset to start writing at
152 * @size: The amount of data to write
154 * Returns: The number of bytes correctly written or error code
156 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
157 uint64_t offset, unsigned int size)
159 struct gfs2_sbd *sdp = ip->i_sbd;
160 struct buffer_head *dibh;
161 uint64_t lblock, dblock;
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, NULL);
185 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
187 while (copied < size) {
189 struct buffer_head *bh;
192 amount = size - copied;
193 if (amount > sdp->sd_sb.sb_bsize - o)
194 amount = sdp->sd_sb.sb_bsize - o;
198 error = gfs2_block_map(ip, lblock, &new, &dblock,
203 if (gfs2_assert_withdraw(sdp, dblock))
207 error = gfs2_dir_get_buffer(ip, dblock,
208 (amount == sdp->sd_jbsize) ?
213 gfs2_trans_add_bh(ip->i_gl, bh, 1);
214 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 uint64_t offset, unsigned int size)
276 struct gfs2_sbd *sdp = ip->i_sbd;
277 uint64_t lblock, dblock;
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_block_map(ip, lblock, &new, &dblock,
320 gfs2_meta_ra(ip->i_gl, dblock, extlen);
323 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
331 memcpy(buf, bh->b_data + o, amount);
339 o = sizeof(struct gfs2_meta_header);
344 return (copied) ? copied : error;
347 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
348 const struct qstr *name,
351 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
352 const struct qstr *name, int ret)
354 if (dent->de_inum.no_addr != 0 &&
355 be32_to_cpu(dent->de_hash) == name->hash &&
356 be16_to_cpu(dent->de_name_len) == name->len &&
357 memcmp((char *)(dent+1), name->name, name->len) == 0)
362 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
363 const struct qstr *name,
366 return __gfs2_dirent_find(dent, name, 1);
369 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
370 const struct qstr *name,
373 return __gfs2_dirent_find(dent, name, 2);
377 * name->name holds ptr to start of block.
378 * name->len holds size of block.
380 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
381 const struct qstr *name,
384 const char *start = name->name;
385 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
386 if (name->len == (end - start))
391 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
392 const struct qstr *name,
395 unsigned required = GFS2_DIRENT_SIZE(name->len);
396 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
397 unsigned totlen = be16_to_cpu(dent->de_rec_len);
399 if (!dent->de_inum.no_addr)
400 actual = GFS2_DIRENT_SIZE(0);
401 if ((totlen - actual) >= required)
406 struct dirent_gather {
407 const struct gfs2_dirent **pdent;
411 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
412 const struct qstr *name,
415 struct dirent_gather *g = opaque;
416 if (dent->de_inum.no_addr) {
417 g->pdent[g->offset++] = dent;
423 * Other possible things to check:
424 * - Inode located within filesystem size (and on valid block)
425 * - Valid directory entry type
426 * Not sure how heavy-weight we want to make this... could also check
427 * hash is correct for example, but that would take a lot of extra time.
428 * For now the most important thing is to check that the various sizes
431 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
432 unsigned int size, unsigned int len, int first)
434 const char *msg = "gfs2_dirent too small";
435 if (unlikely(size < sizeof(struct gfs2_dirent)))
437 msg = "gfs2_dirent misaligned";
438 if (unlikely(offset & 0x7))
440 msg = "gfs2_dirent points beyond end of block";
441 if (unlikely(offset + size > len))
443 msg = "zero inode number";
444 if (unlikely(!first && !dent->de_inum.no_addr))
446 msg = "name length is greater than space in dirent";
447 if (dent->de_inum.no_addr &&
448 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
453 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
454 first ? "first in block" : "not first in block");
458 static int gfs2_dirent_offset(const void *buf)
460 const struct gfs2_meta_header *h = buf;
465 switch(be32_to_cpu(h->mh_type)) {
466 case GFS2_METATYPE_LF:
467 offset = sizeof(struct gfs2_leaf);
469 case GFS2_METATYPE_DI:
470 offset = sizeof(struct gfs2_dinode);
477 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
478 be32_to_cpu(h->mh_type));
482 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
484 unsigned int len, gfs2_dscan_t scan,
485 const struct qstr *name,
488 struct gfs2_dirent *dent, *prev;
493 ret = gfs2_dirent_offset(buf);
499 dent = (struct gfs2_dirent *)(buf + offset);
500 size = be16_to_cpu(dent->de_rec_len);
501 if (gfs2_check_dirent(dent, offset, size, len, 1))
504 ret = scan(dent, name, opaque);
511 dent = (struct gfs2_dirent *)(buf + offset);
512 size = be16_to_cpu(dent->de_rec_len);
513 if (gfs2_check_dirent(dent, offset, size, len, 0))
523 return prev ? prev : dent;
530 gfs2_consist_inode(inode->u.generic_ip);
531 return ERR_PTR(-EIO);
536 * dirent_first - Return the first dirent
537 * @dip: the directory
539 * @dent: Pointer to list of dirents
541 * return first dirent whether bh points to leaf or stuffed dinode
543 * Returns: IS_LEAF, IS_DINODE, or -errno
546 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
547 struct gfs2_dirent **dent)
549 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
551 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
552 if (gfs2_meta_check(dip->i_sbd, bh))
554 *dent = (struct gfs2_dirent *)(bh->b_data +
555 sizeof(struct gfs2_leaf));
558 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
560 *dent = (struct gfs2_dirent *)(bh->b_data +
561 sizeof(struct gfs2_dinode));
567 * dirent_next - Next dirent
568 * @dip: the directory
570 * @dent: Pointer to list of dirents
572 * Returns: 0 on success, error code otherwise
575 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
576 struct gfs2_dirent **dent)
578 struct gfs2_dirent *tmp, *cur;
580 uint16_t cur_rec_len;
583 bh_end = bh->b_data + bh->b_size;
584 cur_rec_len = be16_to_cpu(cur->de_rec_len);
586 if ((char *)cur + cur_rec_len >= bh_end) {
587 if ((char *)cur + cur_rec_len > bh_end) {
588 gfs2_consist_inode(dip);
594 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
596 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
597 gfs2_consist_inode(dip);
601 if (cur_rec_len == 0) {
602 gfs2_consist_inode(dip);
606 /* Only the first dent could ever have de_inum.no_addr == 0 */
607 if (!tmp->de_inum.no_addr) {
608 gfs2_consist_inode(dip);
618 * dirent_del - Delete a dirent
619 * @dip: The GFS2 inode
621 * @prev: The previous dirent
622 * @cur: The current dirent
626 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
627 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
629 uint16_t cur_rec_len, prev_rec_len;
631 if (!cur->de_inum.no_addr) {
632 gfs2_consist_inode(dip);
636 gfs2_trans_add_bh(dip->i_gl, bh, 1);
638 /* If there is no prev entry, this is the first entry in the block.
639 The de_rec_len is already as big as it needs to be. Just zero
640 out the inode number and return. */
643 cur->de_inum.no_addr = 0; /* No endianess worries */
647 /* Combine this dentry with the previous one. */
649 prev_rec_len = be16_to_cpu(prev->de_rec_len);
650 cur_rec_len = be16_to_cpu(cur->de_rec_len);
652 if ((char *)prev + prev_rec_len != (char *)cur)
653 gfs2_consist_inode(dip);
654 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
655 gfs2_consist_inode(dip);
657 prev_rec_len += cur_rec_len;
658 prev->de_rec_len = cpu_to_be16(prev_rec_len);
662 * Takes a dent from which to grab space as an argument. Returns the
663 * newly created dent.
665 struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
666 struct gfs2_dirent *dent,
667 const struct qstr *name,
668 struct buffer_head *bh)
670 struct gfs2_inode *ip = inode->u.generic_ip;
671 struct gfs2_dirent *ndent;
672 unsigned offset = 0, totlen;
674 if (dent->de_inum.no_addr)
675 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
676 totlen = be16_to_cpu(dent->de_rec_len);
677 BUG_ON(offset + name->len > totlen);
678 gfs2_trans_add_bh(ip->i_gl, bh, 1);
679 ndent = (struct gfs2_dirent *)((char *)dent + offset);
680 dent->de_rec_len = cpu_to_be16(offset);
681 gfs2_qstr2dirent(name, totlen - offset, ndent);
685 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
686 struct buffer_head *bh,
687 const struct qstr *name)
689 struct gfs2_dirent *dent;
690 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
691 gfs2_dirent_find_space, name, NULL);
692 if (!dent || IS_ERR(dent))
694 return gfs2_init_dirent(inode, dent, name, bh);
697 static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
698 struct buffer_head **bhp)
702 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
703 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
710 * get_leaf_nr - Get a leaf number associated with the index
711 * @dip: The GFS2 inode
715 * Returns: 0 on success, error code otherwise
718 static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
724 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
725 index * sizeof(uint64_t),
727 if (error != sizeof(uint64_t))
728 return (error < 0) ? error : -EIO;
730 *leaf_out = be64_to_cpu(leaf_no);
735 static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
736 struct buffer_head **bh_out)
741 error = get_leaf_nr(dip, index, &leaf_no);
743 error = get_leaf(dip, leaf_no, bh_out);
748 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
749 const struct qstr *name,
751 struct buffer_head **pbh)
753 struct buffer_head *bh;
754 struct gfs2_dirent *dent;
755 struct gfs2_inode *ip = inode->u.generic_ip;
758 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
759 struct gfs2_leaf *leaf;
760 unsigned hsize = 1 << ip->i_di.di_depth;
763 if (hsize * sizeof(u64) != ip->i_di.di_size) {
764 gfs2_consist_inode(ip);
765 return ERR_PTR(-EIO);
768 index = name->hash >> (32 - ip->i_di.di_depth);
769 error = get_first_leaf(ip, index, &bh);
771 return ERR_PTR(error);
773 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
777 leaf = (struct gfs2_leaf *)bh->b_data;
778 ln = be64_to_cpu(leaf->lf_next);
782 error = get_leaf(ip, ln, &bh);
785 return error ? ERR_PTR(error) : NULL;
788 error = gfs2_meta_inode_buffer(ip, &bh);
790 return ERR_PTR(error);
791 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
793 if (unlikely(IS_ERR(dent))) {
801 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
803 struct gfs2_inode *ip = inode->u.generic_ip;
804 u64 bn = gfs2_alloc_meta(ip);
805 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
806 struct gfs2_leaf *leaf;
807 struct gfs2_dirent *dent;
808 struct qstr name = { .name = "", .len = 0, .hash = 0 };
811 gfs2_trans_add_bh(ip->i_gl, bh, 1);
812 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
813 leaf = (struct gfs2_leaf *)bh->b_data;
814 leaf->lf_depth = cpu_to_be16(depth);
815 leaf->lf_entries = cpu_to_be16(0);
816 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
817 leaf->lf_next = cpu_to_be64(0);
818 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
819 dent = (struct gfs2_dirent *)(leaf+1);
820 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
826 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
827 * @dip: The GFS2 inode
829 * Returns: 0 on success, error code otherwise
832 static int dir_make_exhash(struct inode *inode)
834 struct gfs2_inode *dip = inode->u.generic_ip;
835 struct gfs2_sbd *sdp = dip->i_sbd;
836 struct gfs2_dirent *dent;
838 struct buffer_head *bh, *dibh;
839 struct gfs2_leaf *leaf;
845 error = gfs2_meta_inode_buffer(dip, &dibh);
849 /* Turn over a new leaf */
851 leaf = new_leaf(inode, &bh, 0);
856 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
857 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
861 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
862 sizeof(struct gfs2_dinode));
864 /* Find last entry */
867 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
868 sizeof(struct gfs2_leaf);
869 args.name = bh->b_data;
870 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
871 gfs2_dirent_last, &args, NULL);
880 return PTR_ERR(dent);
883 /* Adjust the last dirent's record length
884 (Remember that dent still points to the last entry.) */
886 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
887 sizeof(struct gfs2_dinode) -
888 sizeof(struct gfs2_leaf));
892 /* We're done with the new leaf block, now setup the new
895 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
896 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
898 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
900 for (x = sdp->sd_hash_ptrs; x--; lp++)
901 *lp = cpu_to_be64(bn);
903 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
904 dip->i_di.di_blocks++;
905 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
906 dip->i_di.di_payload_format = 0;
908 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
909 dip->i_di.di_depth = y;
911 gfs2_dinode_out(&dip->i_di, dibh->b_data);
919 * dir_split_leaf - Split a leaf block into two
920 * @dip: The GFS2 inode
924 * Returns: 0 on success, error code on failure
927 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
929 struct gfs2_inode *dip = inode->u.generic_ip;
930 struct buffer_head *nbh, *obh, *dibh;
931 struct gfs2_leaf *nleaf, *oleaf;
932 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
933 uint32_t start, len, half_len, divider;
934 uint64_t bn, *lp, leaf_no;
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(uint64_t), 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(uint64_t),
985 half_len * sizeof(uint64_t));
986 if (error != half_len * sizeof(uint64_t)) {
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(dip->i_sbd, !error)) {
1041 dip->i_di.di_blocks++;
1042 gfs2_dinode_out(&dip->i_di, 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 = dip->i_sbd;
1070 struct buffer_head *dibh;
1073 uint64_t *from, *to;
1078 hsize = 1 << dip->i_di.di_depth;
1079 if (hsize * sizeof(uint64_t) != 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);
1092 if (error != sdp->sd_hash_bsize) {
1099 to = (uint64_t *)((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->i_di, dibh->b_data);
1135 * compare_dents - compare directory entries by hash value
1139 * When comparing the hash entries of @a to @b:
1145 static int compare_dents(const void *a, const void *b)
1147 struct gfs2_dirent *dent_a, *dent_b;
1148 uint32_t hash_a, hash_b;
1151 dent_a = *(struct gfs2_dirent **)a;
1152 hash_a = be32_to_cpu(dent_a->de_hash);
1154 dent_b = *(struct gfs2_dirent **)b;
1155 hash_b = be32_to_cpu(dent_b->de_hash);
1157 if (hash_a > hash_b)
1159 else if (hash_a < hash_b)
1162 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1163 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1167 else if (len_a < len_b)
1170 ret = memcmp((char *)(dent_a + 1),
1171 (char *)(dent_b + 1),
1179 * do_filldir_main - read out directory entries
1180 * @dip: The GFS2 inode
1181 * @offset: The offset in the file to read from
1182 * @opaque: opaque data to pass to filldir
1183 * @filldir: The function to pass entries to
1184 * @darr: an array of struct gfs2_dirent pointers to read
1185 * @entries: the number of entries in darr
1186 * @copied: pointer to int that's non-zero if a entry has been copied out
1188 * Jump through some hoops to make sure that if there are hash collsions,
1189 * they are read out at the beginning of a buffer. We want to minimize
1190 * the possibility that they will fall into different readdir buffers or
1191 * that someone will want to seek to that location.
1193 * Returns: errno, >0 on exception from filldir
1196 static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1197 void *opaque, gfs2_filldir_t filldir,
1198 const struct gfs2_dirent **darr, uint32_t entries,
1201 const struct gfs2_dirent *dent, *dent_next;
1202 struct gfs2_inum inum;
1203 uint64_t off, off_next;
1208 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1210 dent_next = darr[0];
1211 off_next = be32_to_cpu(dent_next->de_hash);
1212 off_next = gfs2_disk_hash2offset(off_next);
1214 for (x = 0, y = 1; x < entries; x++, y++) {
1219 dent_next = darr[y];
1220 off_next = be32_to_cpu(dent_next->de_hash);
1221 off_next = gfs2_disk_hash2offset(off_next);
1227 if (off_next == off) {
1228 if (*copied && !run)
1239 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1241 error = filldir(opaque, (char *)(dent + 1),
1242 be16_to_cpu(dent->de_name_len),
1244 be16_to_cpu(dent->de_type));
1251 /* Increment the *offset by one, so the next time we come into the
1252 do_filldir fxn, we get the next entry instead of the last one in the
1260 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1261 gfs2_filldir_t filldir, int *copied,
1262 unsigned *depth, u64 leaf_no)
1264 struct gfs2_inode *ip = inode->u.generic_ip;
1265 struct buffer_head *bh;
1266 struct gfs2_leaf *lf;
1267 unsigned entries = 0;
1268 unsigned leaves = 0;
1269 const struct gfs2_dirent **darr, *dent;
1270 struct dirent_gather g;
1271 struct buffer_head **larr;
1277 error = get_leaf(ip, lfn, &bh);
1280 lf = (struct gfs2_leaf *)bh->b_data;
1282 *depth = be16_to_cpu(lf->lf_depth);
1283 entries += be16_to_cpu(lf->lf_entries);
1285 lfn = be64_to_cpu(lf->lf_next);
1293 larr = kmalloc((leaves + entries) * sizeof(void*), GFP_KERNEL);
1296 darr = (const struct gfs2_dirent **)(larr + leaves);
1302 error = get_leaf(ip, lfn, &bh);
1305 lf = (struct gfs2_leaf *)bh->b_data;
1306 lfn = be64_to_cpu(lf->lf_next);
1307 if (lf->lf_entries) {
1308 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1309 gfs2_dirent_gather, NULL, &g);
1310 error = PTR_ERR(dent);
1321 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1324 for(i = 0; i < leaf; i++)
1332 * dir_e_read - Reads the entries from a directory into a filldir buffer
1333 * @dip: dinode pointer
1334 * @offset: the hash of the last entry read shifted to the right once
1335 * @opaque: buffer for the filldir function to fill
1336 * @filldir: points to the filldir function to use
1341 static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
1342 gfs2_filldir_t filldir)
1344 struct gfs2_inode *dip = inode->u.generic_ip;
1345 struct gfs2_sbd *sdp = dip->i_sbd;
1346 uint32_t hsize, len = 0;
1347 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1348 uint32_t hash, index;
1354 hsize = 1 << dip->i_di.di_depth;
1355 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1356 gfs2_consist_inode(dip);
1360 hash = gfs2_dir_offset2hash(*offset);
1361 index = hash >> (32 - dip->i_di.di_depth);
1363 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1367 while (index < hsize) {
1368 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1369 ht_offset = index - lp_offset;
1371 if (ht_offset_cur != ht_offset) {
1372 error = gfs2_dir_read_data(dip, (char *)lp,
1373 ht_offset * sizeof(uint64_t),
1374 sdp->sd_hash_bsize);
1375 if (error != sdp->sd_hash_bsize) {
1380 ht_offset_cur = ht_offset;
1383 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1385 be64_to_cpu(lp[lp_offset]));
1389 len = 1 << (dip->i_di.di_depth - depth);
1390 index = (index & ~(len - 1)) + len;
1400 int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1401 gfs2_filldir_t filldir)
1403 struct gfs2_inode *dip = inode->u.generic_ip;
1404 struct dirent_gather g;
1405 const struct gfs2_dirent **darr, *dent;
1406 struct buffer_head *dibh;
1410 if (!dip->i_di.di_entries)
1413 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1414 return dir_e_read(inode, offset, opaque, filldir);
1416 if (!gfs2_is_stuffed(dip)) {
1417 gfs2_consist_inode(dip);
1421 error = gfs2_meta_inode_buffer(dip, &dibh);
1426 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1431 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1432 gfs2_dirent_gather, NULL, &g);
1434 error = PTR_ERR(dent);
1437 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1438 dip->i_di.di_entries, &copied);
1452 * gfs2_dir_search - Search a directory
1453 * @dip: The GFS2 inode
1457 * This routine searches a directory for a file or another directory.
1458 * Assumes a glock is held on dip.
1463 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1464 struct gfs2_inum *inum, unsigned int *type)
1466 struct buffer_head *bh;
1467 struct gfs2_dirent *dent;
1469 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1472 return PTR_ERR(dent);
1474 gfs2_inum_in(inum, (char *)&dent->de_inum);
1476 *type = be16_to_cpu(dent->de_type);
1484 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1486 struct buffer_head *bh, *obh;
1487 struct gfs2_inode *ip = inode->u.generic_ip;
1488 struct gfs2_leaf *leaf, *oleaf;
1493 index = name->hash >> (32 - ip->i_di.di_depth);
1494 error = get_first_leaf(ip, index, &obh);
1498 oleaf = (struct gfs2_leaf *)obh->b_data;
1499 bn = be64_to_cpu(oleaf->lf_next);
1503 error = get_leaf(ip, bn, &obh);
1508 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1510 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1515 oleaf->lf_next = cpu_to_be64(bn);
1519 error = gfs2_meta_inode_buffer(ip, &bh);
1522 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1523 ip->i_di.di_blocks++;
1524 gfs2_dinode_out(&ip->i_di, bh->b_data);
1530 * gfs2_dir_add - Add new filename into directory
1531 * @dip: The GFS2 inode
1532 * @filename: The new name
1533 * @inode: The inode number of the entry
1534 * @type: The type of the entry
1536 * Returns: 0 on success, error code on failure
1539 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1540 const struct gfs2_inum *inum, unsigned type)
1542 struct gfs2_inode *ip = inode->u.generic_ip;
1543 struct buffer_head *bh;
1544 struct gfs2_dirent *dent;
1545 struct gfs2_leaf *leaf;
1549 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1553 return PTR_ERR(dent);
1554 dent = gfs2_init_dirent(inode, dent, name, bh);
1555 gfs2_inum_out(inum, (char *)&dent->de_inum);
1556 dent->de_type = cpu_to_be16(type);
1557 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1558 leaf = (struct gfs2_leaf *)bh->b_data;
1559 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1562 error = gfs2_meta_inode_buffer(ip, &bh);
1565 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1566 ip->i_di.di_entries++;
1567 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1568 gfs2_dinode_out(&ip->i_di, bh->b_data);
1573 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1574 error = dir_make_exhash(inode);
1579 error = dir_split_leaf(inode, name);
1584 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1585 error = dir_double_exhash(ip);
1588 error = dir_split_leaf(inode, name);
1594 error = dir_new_leaf(inode, name);
1605 * gfs2_dir_del - Delete a directory entry
1606 * @dip: The GFS2 inode
1607 * @filename: The filename
1609 * Returns: 0 on success, error code on failure
1612 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1614 struct gfs2_dirent *dent, *prev = NULL;
1615 struct buffer_head *bh;
1618 /* Returns _either_ the entry (if its first in block) or the
1619 previous entry otherwise */
1620 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
1623 gfs2_consist_inode(dip);
1627 gfs2_consist_inode(dip);
1628 return PTR_ERR(dent);
1630 /* If not first in block, adjust pointers accordingly */
1631 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1633 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1636 dirent_del(dip, bh, prev, dent);
1637 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1638 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1639 u16 entries = be16_to_cpu(leaf->lf_entries);
1641 gfs2_consist_inode(dip);
1642 leaf->lf_entries = cpu_to_be16(--entries);
1646 error = gfs2_meta_inode_buffer(dip, &bh);
1650 if (!dip->i_di.di_entries)
1651 gfs2_consist_inode(dip);
1652 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1653 dip->i_di.di_entries--;
1654 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1655 gfs2_dinode_out(&dip->i_di, bh->b_data);
1662 * gfs2_dir_mvino - Change inode number of directory entry
1663 * @dip: The GFS2 inode
1667 * This routine changes the inode number of a directory entry. It's used
1668 * by rename to change ".." when a directory is moved.
1669 * Assumes a glock is held on dvp.
1674 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1675 struct gfs2_inum *inum, unsigned int new_type)
1677 struct buffer_head *bh;
1678 struct gfs2_dirent *dent;
1681 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
1684 gfs2_consist_inode(dip);
1688 return PTR_ERR(dent);
1690 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1691 gfs2_inum_out(inum, (char *)&dent->de_inum);
1692 dent->de_type = cpu_to_be16(new_type);
1694 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1696 error = gfs2_meta_inode_buffer(dip, &bh);
1699 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1702 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1703 gfs2_dinode_out(&dip->i_di, bh->b_data);
1709 * foreach_leaf - call a function for each leaf in a directory
1710 * @dip: the directory
1711 * @lc: the function to call for each each
1712 * @data: private data to pass to it
1717 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1719 struct gfs2_sbd *sdp = dip->i_sbd;
1720 struct buffer_head *bh;
1721 struct gfs2_leaf *leaf;
1722 uint32_t hsize, len;
1723 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1729 hsize = 1 << dip->i_di.di_depth;
1730 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1731 gfs2_consist_inode(dip);
1735 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1739 while (index < hsize) {
1740 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1741 ht_offset = index - lp_offset;
1743 if (ht_offset_cur != ht_offset) {
1744 error = gfs2_dir_read_data(dip, (char *)lp,
1745 ht_offset * sizeof(uint64_t),
1746 sdp->sd_hash_bsize);
1747 if (error != sdp->sd_hash_bsize) {
1752 ht_offset_cur = ht_offset;
1755 leaf_no = be64_to_cpu(lp[lp_offset]);
1757 error = get_leaf(dip, leaf_no, &bh);
1760 leaf = (struct gfs2_leaf *)bh->b_data;
1763 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1765 error = lc(dip, index, len, leaf_no, data);
1769 index = (index & ~(len - 1)) + len;
1774 if (index != hsize) {
1775 gfs2_consist_inode(dip);
1786 * leaf_dealloc - Deallocate a directory leaf
1787 * @dip: the directory
1788 * @index: the hash table offset in the directory
1789 * @len: the number of pointers to this leaf
1790 * @leaf_no: the leaf number
1796 static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1797 uint64_t leaf_no, void *data)
1799 struct gfs2_sbd *sdp = dip->i_sbd;
1800 struct gfs2_leaf *tmp_leaf;
1801 struct gfs2_rgrp_list rlist;
1802 struct buffer_head *bh, *dibh;
1804 unsigned int rg_blocks = 0, l_blocks = 0;
1806 unsigned int x, size = len * sizeof(uint64_t);
1809 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1811 ht = kzalloc(size, GFP_KERNEL);
1815 gfs2_alloc_get(dip);
1817 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1821 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1825 /* Count the number of leaves */
1827 for (blk = leaf_no; blk; blk = nblk) {
1828 error = get_leaf(dip, blk, &bh);
1831 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1832 nblk = be64_to_cpu(tmp_leaf->lf_next);
1835 gfs2_rlist_add(sdp, &rlist, blk);
1839 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1841 for (x = 0; x < rlist.rl_rgrps; x++) {
1842 struct gfs2_rgrpd *rgd;
1843 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1844 rg_blocks += rgd->rd_ri.ri_length;
1847 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1851 error = gfs2_trans_begin(sdp,
1852 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1853 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1855 goto out_rg_gunlock;
1857 for (blk = leaf_no; blk; blk = nblk) {
1858 error = get_leaf(dip, blk, &bh);
1861 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1862 nblk = be64_to_cpu(tmp_leaf->lf_next);
1865 gfs2_free_meta(dip, blk, 1);
1867 if (!dip->i_di.di_blocks)
1868 gfs2_consist_inode(dip);
1869 dip->i_di.di_blocks--;
1872 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
1873 if (error != size) {
1879 error = gfs2_meta_inode_buffer(dip, &dibh);
1883 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1884 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1888 gfs2_trans_end(sdp);
1891 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1894 gfs2_rlist_free(&rlist);
1895 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1898 gfs2_quota_unhold(dip);
1901 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 = dip->i_sbd;
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,
1957 const struct qstr *name)
1959 struct gfs2_dirent *dent;
1960 struct buffer_head *bh;
1962 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1968 return PTR_ERR(dent);