Lines Matching +full:ip +full:- +full:block
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
14 * within a single block. The field de_name_len is the number of bytes
17 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
19 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * When deleting the first dirent in a block, there is no previous dirent so
25 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
33 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
34 * beginning of the leaf block. The dirents reside in leaves when
36 * dip->i_diskflags & GFS2_DIF_EXHASH is true
38 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
41 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
43 * block pointer in the array that points to the same leaf. In fact, when a
76 #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
89 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, in gfs2_dir_get_new_buffer() argument
94 bh = gfs2_meta_new(ip->i_gl, block); in gfs2_dir_get_new_buffer()
95 gfs2_trans_add_meta(ip->i_gl, bh); in gfs2_dir_get_new_buffer()
102 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block, in gfs2_dir_get_existing_buffer() argument
108 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh); in gfs2_dir_get_existing_buffer()
111 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) { in gfs2_dir_get_existing_buffer()
113 return -EIO; in gfs2_dir_get_existing_buffer()
119 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf, in gfs2_dir_write_stuffed() argument
125 error = gfs2_meta_inode_buffer(ip, &dibh); in gfs2_dir_write_stuffed()
129 gfs2_trans_add_meta(ip->i_gl, dibh); in gfs2_dir_write_stuffed()
130 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size); in gfs2_dir_write_stuffed()
131 if (ip->i_inode.i_size < offset + size) in gfs2_dir_write_stuffed()
132 i_size_write(&ip->i_inode, offset + size); in gfs2_dir_write_stuffed()
133 inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode)); in gfs2_dir_write_stuffed()
134 gfs2_dinode_out(ip, dibh->b_data); in gfs2_dir_write_stuffed()
144 * gfs2_dir_write_data - Write directory information to the inode
145 * @ip: The GFS2 inode
152 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf, in gfs2_dir_write_data() argument
155 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_dir_write_data()
167 if (gfs2_is_stuffed(ip) && offset + size <= gfs2_max_stuffed_size(ip)) in gfs2_dir_write_data()
168 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset, in gfs2_dir_write_data()
171 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) in gfs2_dir_write_data()
172 return -EINVAL; in gfs2_dir_write_data()
174 if (gfs2_is_stuffed(ip)) { in gfs2_dir_write_data()
175 error = gfs2_unstuff_dinode(ip); in gfs2_dir_write_data()
181 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); in gfs2_dir_write_data()
187 amount = size - copied; in gfs2_dir_write_data()
188 if (amount > sdp->sd_sb.sb_bsize - o) in gfs2_dir_write_data()
189 amount = sdp->sd_sb.sb_bsize - o; in gfs2_dir_write_data()
193 error = gfs2_alloc_extent(&ip->i_inode, lblock, &dblock, in gfs2_dir_write_data()
197 error = -EIO; in gfs2_dir_write_data()
202 if (amount == sdp->sd_jbsize || new) in gfs2_dir_write_data()
203 error = gfs2_dir_get_new_buffer(ip, dblock, &bh); in gfs2_dir_write_data()
205 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh); in gfs2_dir_write_data()
210 gfs2_trans_add_meta(ip->i_gl, bh); in gfs2_dir_write_data()
211 memcpy(bh->b_data + o, buf, amount); in gfs2_dir_write_data()
218 extlen--; in gfs2_dir_write_data()
224 error = gfs2_meta_inode_buffer(ip, &dibh); in gfs2_dir_write_data()
228 if (ip->i_inode.i_size < offset + copied) in gfs2_dir_write_data()
229 i_size_write(&ip->i_inode, offset + copied); in gfs2_dir_write_data()
230 inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode)); in gfs2_dir_write_data()
232 gfs2_trans_add_meta(ip->i_gl, dibh); in gfs2_dir_write_data()
233 gfs2_dinode_out(ip, dibh->b_data); in gfs2_dir_write_data()
243 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf, in gfs2_dir_read_stuffed() argument
249 error = gfs2_meta_inode_buffer(ip, &dibh); in gfs2_dir_read_stuffed()
251 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size); in gfs2_dir_read_stuffed()
260 * gfs2_dir_read_data - Read a data from a directory inode
261 * @ip: The GFS2 Inode
267 static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf, in gfs2_dir_read_data() argument
270 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_dir_read_data()
277 if (gfs2_is_stuffed(ip)) in gfs2_dir_read_data()
278 return gfs2_dir_read_stuffed(ip, buf, size); in gfs2_dir_read_data()
280 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) in gfs2_dir_read_data()
281 return -EINVAL; in gfs2_dir_read_data()
284 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); in gfs2_dir_read_data()
290 amount = size - copied; in gfs2_dir_read_data()
291 if (amount > sdp->sd_sb.sb_bsize - o) in gfs2_dir_read_data()
292 amount = sdp->sd_sb.sb_bsize - o; in gfs2_dir_read_data()
296 error = gfs2_get_extent(&ip->i_inode, lblock, in gfs2_dir_read_data()
301 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); in gfs2_dir_read_data()
303 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &bh); in gfs2_dir_read_data()
313 extlen--; in gfs2_dir_read_data()
314 memcpy(buf, bh->b_data + o, amount); in gfs2_dir_read_data()
328 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
329 * @ip: The inode in question
334 static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip) in gfs2_dir_get_hash_table() argument
336 struct inode *inode = &ip->i_inode; in gfs2_dir_get_hash_table()
341 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH)); in gfs2_dir_get_hash_table()
343 hc = ip->i_hash_cache; in gfs2_dir_get_hash_table()
347 hsize = BIT(ip->i_depth); in gfs2_dir_get_hash_table()
349 if (hsize != i_size_read(&ip->i_inode)) { in gfs2_dir_get_hash_table()
350 gfs2_consist_inode(ip); in gfs2_dir_get_hash_table()
351 return ERR_PTR(-EIO); in gfs2_dir_get_hash_table()
359 return ERR_PTR(-ENOMEM); in gfs2_dir_get_hash_table()
361 ret = gfs2_dir_read_data(ip, hc, hsize); in gfs2_dir_get_hash_table()
367 spin_lock(&inode->i_lock); in gfs2_dir_get_hash_table()
368 if (likely(!ip->i_hash_cache)) { in gfs2_dir_get_hash_table()
369 ip->i_hash_cache = hc; in gfs2_dir_get_hash_table()
372 spin_unlock(&inode->i_lock); in gfs2_dir_get_hash_table()
375 return ip->i_hash_cache; in gfs2_dir_get_hash_table()
379 * gfs2_dir_hash_inval - Invalidate dir hash
380 * @ip: The directory inode
384 void gfs2_dir_hash_inval(struct gfs2_inode *ip) in gfs2_dir_hash_inval() argument
388 spin_lock(&ip->i_inode.i_lock); in gfs2_dir_hash_inval()
389 hc = ip->i_hash_cache; in gfs2_dir_hash_inval()
390 ip->i_hash_cache = NULL; in gfs2_dir_hash_inval()
391 spin_unlock(&ip->i_inode.i_lock); in gfs2_dir_hash_inval()
398 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0; in gfs2_dirent_sentinel()
405 be32_to_cpu(dent->de_hash) == name->hash && in __gfs2_dirent_find()
406 be16_to_cpu(dent->de_name_len) == name->len && in __gfs2_dirent_find()
407 memcmp(dent+1, name->name, name->len) == 0) in __gfs2_dirent_find()
427 * name->name holds ptr to start of block.
428 * name->len holds size of block.
434 const char *start = name->name; in gfs2_dirent_last()
435 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len); in gfs2_dirent_last()
436 if (name->len == (end - start)) in gfs2_dirent_last()
447 unsigned required = GFS2_DIRENT_SIZE(name->len); in gfs2_dirent_find_offset()
448 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); in gfs2_dirent_find_offset()
449 unsigned totlen = be16_to_cpu(dent->de_rec_len); in gfs2_dirent_find_offset()
456 return -1; in gfs2_dirent_find_offset()
459 return -1; in gfs2_dirent_find_offset()
466 unsigned required = GFS2_DIRENT_SIZE(name->len); in gfs2_dirent_find_space()
467 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); in gfs2_dirent_find_space()
468 unsigned totlen = be16_to_cpu(dent->de_rec_len); in gfs2_dirent_find_space()
472 if (totlen - actual >= required) in gfs2_dirent_find_space()
488 g->pdent[g->offset++] = dent; in gfs2_dirent_gather()
495 * - Inode located within filesystem size (and on valid block)
496 * - Valid directory entry type
497 * Not sure how heavy-weight we want to make this... could also check
512 msg = "gfs2_dirent points beyond end of block"; in gfs2_check_dirent()
520 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) > in gfs2_check_dirent()
526 __func__, msg, first ? "first in block" : "not first in block"); in gfs2_check_dirent()
527 return -EIO; in gfs2_check_dirent()
537 switch(be32_to_cpu(h->mh_type)) { in gfs2_dirent_offset()
549 fs_warn(sdp, "%s: wrong block type %u\n", __func__, in gfs2_dirent_offset()
550 be32_to_cpu(h->mh_type)); in gfs2_dirent_offset()
551 return -1; in gfs2_dirent_offset()
567 return ERR_PTR(-EIO); in gfs2_dirent_scan()
572 size = be16_to_cpu(dent->de_rec_len); in gfs2_dirent_scan()
575 return ERR_PTR(-EIO); in gfs2_dirent_scan()
586 size = be16_to_cpu(dent->de_rec_len); in gfs2_dirent_scan()
590 return ERR_PTR(-EIO); in gfs2_dirent_scan()
611 u16 rec_len = be16_to_cpu(d->de_rec_len); in dirent_check_reclen()
615 return -EIO; in dirent_check_reclen()
621 return -ENOENT; in dirent_check_reclen()
624 return -EIO; in dirent_check_reclen()
628 * dirent_next - Next dirent
640 char *bh_end = bh->b_data + bh->b_size; in dirent_next()
649 if (ret == -EIO) in dirent_next()
655 return -EIO; in dirent_next()
663 * dirent_del - Delete a dirent
681 gfs2_trans_add_meta(dip->i_gl, bh); in dirent_del()
683 /* If there is no prev entry, this is the first entry in the block. in dirent_del()
688 cur->de_inum.no_addr = 0; in dirent_del()
689 cur->de_inum.no_formal_ino = 0; in dirent_del()
695 prev_rec_len = be16_to_cpu(prev->de_rec_len); in dirent_del()
696 cur_rec_len = be16_to_cpu(cur->de_rec_len); in dirent_del()
700 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size) in dirent_del()
704 prev->de_rec_len = cpu_to_be16(prev_rec_len); in dirent_del()
714 struct gfs2_inode *ip = GFS2_I(inode); in do_init_dirent() local
718 totlen = be16_to_cpu(dent->de_rec_len); in do_init_dirent()
719 BUG_ON(offset + name->len > totlen); in do_init_dirent()
720 gfs2_trans_add_meta(ip->i_gl, bh); in do_init_dirent()
722 dent->de_rec_len = cpu_to_be16(offset); in do_init_dirent()
723 gfs2_qstr2dirent(name, totlen - offset, ndent); in do_init_dirent()
740 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); in gfs2_init_dirent()
750 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, in gfs2_dirent_split_alloc()
755 (unsigned)(ptr - (void *)dent)); in gfs2_dirent_split_alloc()
763 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp); in get_leaf()
764 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) { in get_leaf()
765 /* pr_info("block num=%llu\n", leaf_no); */ in get_leaf()
766 error = -EIO; in get_leaf()
773 * get_leaf_nr - Get a leaf number associated with the index
776 * @leaf_out: Resulting leaf block number
815 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_dirent_search() local
818 if (ip->i_diskflags & GFS2_DIF_EXHASH) { in gfs2_dirent_search()
820 unsigned int hsize = BIT(ip->i_depth); in gfs2_dirent_search()
824 gfs2_consist_inode(ip); in gfs2_dirent_search()
825 return ERR_PTR(-EIO); in gfs2_dirent_search()
828 index = name->hash >> (32 - ip->i_depth); in gfs2_dirent_search()
829 error = get_first_leaf(ip, index, &bh); in gfs2_dirent_search()
833 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, in gfs2_dirent_search()
837 leaf = (struct gfs2_leaf *)bh->b_data; in gfs2_dirent_search()
838 ln = be64_to_cpu(leaf->lf_next); in gfs2_dirent_search()
843 error = get_leaf(ip, ln, &bh); in gfs2_dirent_search()
850 error = gfs2_meta_inode_buffer(ip, &bh); in gfs2_dirent_search()
853 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL); in gfs2_dirent_search()
865 struct gfs2_inode *ip = GFS2_I(inode); in new_leaf() local
874 error = gfs2_alloc_blocks(ip, &bn, &n, 0); in new_leaf()
877 bh = gfs2_meta_new(ip->i_gl, bn); in new_leaf()
882 gfs2_trans_add_meta(ip->i_gl, bh); in new_leaf()
884 leaf = (struct gfs2_leaf *)bh->b_data; in new_leaf()
885 leaf->lf_depth = cpu_to_be16(depth); in new_leaf()
886 leaf->lf_entries = 0; in new_leaf()
887 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE); in new_leaf()
888 leaf->lf_next = 0; in new_leaf()
889 leaf->lf_inode = cpu_to_be64(ip->i_no_addr); in new_leaf()
890 leaf->lf_dist = cpu_to_be32(1); in new_leaf()
891 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); in new_leaf()
892 leaf->lf_sec = cpu_to_be64(tv.tv_sec); in new_leaf()
893 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2)); in new_leaf()
895 gfs2_qstr2dirent(&empty_name, bh->b_size - sizeof(struct gfs2_leaf), dent); in new_leaf()
901 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
929 return -ENOSPC; in dir_make_exhash()
930 bn = bh->b_blocknr; in dir_make_exhash()
932 gfs2_assert(sdp, dip->i_entries < BIT(16)); in dir_make_exhash()
933 leaf->lf_entries = cpu_to_be16(dip->i_entries); in dir_make_exhash()
943 args.len = bh->b_size - sizeof(struct gfs2_dinode) + in dir_make_exhash()
945 args.name = bh->b_data; in dir_make_exhash()
946 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size, in dir_make_exhash()
951 return -EIO; in dir_make_exhash()
962 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) + in dir_make_exhash()
963 sizeof(struct gfs2_dinode) - in dir_make_exhash()
968 /* We're done with the new leaf block, now setup the new in dir_make_exhash()
971 gfs2_trans_add_meta(dip->i_gl, dibh); in dir_make_exhash()
974 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode)); in dir_make_exhash()
976 for (x = sdp->sd_hash_ptrs; x--; lp++) in dir_make_exhash()
979 i_size_write(inode, sdp->sd_sb.sb_bsize / 2); in dir_make_exhash()
980 gfs2_add_inode_blocks(&dip->i_inode, 1); in dir_make_exhash()
981 dip->i_diskflags |= GFS2_DIF_EXHASH; in dir_make_exhash()
983 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ; in dir_make_exhash()
984 dip->i_depth = y; in dir_make_exhash()
986 gfs2_dinode_out(dip, dibh->b_data); in dir_make_exhash()
994 * dir_split_leaf - Split a leaf block into two
1014 index = name->hash >> (32 - dip->i_depth); in dir_split_leaf()
1019 /* Get the old leaf block */ in dir_split_leaf()
1024 oleaf = (struct gfs2_leaf *)obh->b_data; in dir_split_leaf()
1025 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) { in dir_split_leaf()
1030 gfs2_trans_add_meta(dip->i_gl, obh); in dir_split_leaf()
1032 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1); in dir_split_leaf()
1035 return -ENOSPC; in dir_split_leaf()
1037 bn = nbh->b_blocknr; in dir_split_leaf()
1040 len = BIT(dip->i_depth - be16_to_cpu(oleaf->lf_depth)); in dir_split_leaf()
1044 dip->i_depth, be16_to_cpu(oleaf->lf_depth), index); in dir_split_leaf()
1046 error = -EIO; in dir_split_leaf()
1050 start = (index & ~(len - 1)); in dir_split_leaf()
1053 Don't bother distinguishing stuffed from non-stuffed. in dir_split_leaf()
1057 error = -ENOMEM; in dir_split_leaf()
1071 error = -EIO; in dir_split_leaf()
1078 divider = (start + half_len) << (32 - dip->i_depth); in dir_split_leaf()
1081 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf)); in dir_split_leaf()
1089 be32_to_cpu(dent->de_hash) < divider) { in dir_split_leaf()
1091 void *ptr = ((char *)dent - obh->b_data) + nbh->b_data; in dir_split_leaf()
1093 str.len = be16_to_cpu(dent->de_name_len); in dir_split_leaf()
1094 str.hash = be32_to_cpu(dent->de_hash); in dir_split_leaf()
1101 new->de_inum = dent->de_inum; /* No endian worries */ in dir_split_leaf()
1102 new->de_type = dent->de_type; /* No endian worries */ in dir_split_leaf()
1103 be16_add_cpu(&nleaf->lf_entries, 1); in dir_split_leaf()
1107 if (!oleaf->lf_entries) in dir_split_leaf()
1109 be16_add_cpu(&oleaf->lf_entries, -1); in dir_split_leaf()
1119 oleaf->lf_depth = nleaf->lf_depth; in dir_split_leaf()
1122 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) { in dir_split_leaf()
1123 gfs2_trans_add_meta(dip->i_gl, dibh); in dir_split_leaf()
1124 gfs2_add_inode_blocks(&dip->i_inode, 1); in dir_split_leaf()
1125 gfs2_dinode_out(dip, dibh->b_data); in dir_split_leaf()
1144 * dir_double_exhash - Double size of ExHash table
1160 hsize = BIT(dip->i_depth); in dir_double_exhash()
1172 return -ENOMEM; in dir_double_exhash()
1190 dip->i_hash_cache = hc2; in dir_double_exhash()
1191 dip->i_depth++; in dir_double_exhash()
1192 gfs2_dinode_out(dip, dibh->b_data); in dir_double_exhash()
1199 i_size_write(&dip->i_inode, hsize_bytes); in dir_double_exhash()
1200 gfs2_dinode_out(dip, dibh->b_data); in dir_double_exhash()
1208 * compare_dents - compare directory entries by hash value
1214 * lt: returns -1
1225 hash_a = dent_a->de_cookie; in compare_dents()
1228 hash_b = dent_b->de_cookie; in compare_dents()
1233 ret = -1; in compare_dents()
1235 unsigned int len_a = be16_to_cpu(dent_a->de_name_len); in compare_dents()
1236 unsigned int len_b = be16_to_cpu(dent_b->de_name_len); in compare_dents()
1241 ret = -1; in compare_dents()
1250 * do_filldir_main - read out directory entries
1256 * @copied: pointer to int that's non-zero if a entry has been copied out
1276 sort(&darr[sort_start], entries - sort_start, in do_filldir_main()
1280 off_next = dent_next->de_cookie; in do_filldir_main()
1288 off_next = dent_next->de_cookie; in do_filldir_main()
1290 if (off < ctx->pos) in do_filldir_main()
1292 ctx->pos = off; in do_filldir_main()
1301 if (off < ctx->pos) in do_filldir_main()
1303 ctx->pos = off; in do_filldir_main()
1307 be16_to_cpu(dent->de_name_len), in do_filldir_main()
1308 be64_to_cpu(dent->de_inum.no_addr), in do_filldir_main()
1309 be16_to_cpu(dent->de_type))) in do_filldir_main()
1315 /* Increment the ctx->pos by one, so the next time we come into the in do_filldir_main()
1319 ctx->pos++; in do_filldir_main()
1340 int sort_id = -1; in gfs2_set_cookies()
1346 darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash); in gfs2_set_cookies()
1347 darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie); in gfs2_set_cookies()
1349 if (!sdp->sd_args.ar_loccookie) in gfs2_set_cookies()
1351 offset = (char *)(darr[i]) - in gfs2_set_cookies()
1352 (bh->b_data + gfs2_dirent_offset(sdp, bh->b_data)); in gfs2_set_cookies()
1354 offset += leaf_nr * sdp->sd_max_dents_per_leaf; in gfs2_set_cookies()
1357 darr[i]->de_cookie |= GFS2_USE_HASH_FLAG; in gfs2_set_cookies()
1362 darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK; in gfs2_set_cookies()
1363 darr[i]->de_cookie |= offset; in gfs2_set_cookies()
1373 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_dir_read_leaf() local
1386 error = get_leaf(ip, lfn, &bh); in gfs2_dir_read_leaf()
1389 lf = (struct gfs2_leaf *)bh->b_data; in gfs2_dir_read_leaf()
1391 *depth = be16_to_cpu(lf->lf_depth); in gfs2_dir_read_leaf()
1392 entries += be16_to_cpu(lf->lf_entries); in gfs2_dir_read_leaf()
1394 lfn = be64_to_cpu(lf->lf_next); in gfs2_dir_read_leaf()
1398 if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) { in gfs2_dir_read_leaf()
1406 error = -ENOMEM; in gfs2_dir_read_leaf()
1411 * leaf block. in gfs2_dir_read_leaf()
1422 error = get_leaf(ip, lfn, &bh); in gfs2_dir_read_leaf()
1425 lf = (struct gfs2_leaf *)bh->b_data; in gfs2_dir_read_leaf()
1426 lfn = be64_to_cpu(lf->lf_next); in gfs2_dir_read_leaf()
1427 if (lf->lf_entries) { in gfs2_dir_read_leaf()
1429 entries2 += be16_to_cpu(lf->lf_entries); in gfs2_dir_read_leaf()
1430 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, in gfs2_dir_read_leaf()
1439 (unsigned long long)bh->b_blocknr, in gfs2_dir_read_leaf()
1441 gfs2_consist_inode(ip); in gfs2_dir_read_leaf()
1442 error = -EIO; in gfs2_dir_read_leaf()
1447 be16_to_cpu(lf->lf_entries)); in gfs2_dir_read_leaf()
1460 error = do_filldir_main(ip, ctx, darr, entries, need_sort ? in gfs2_dir_read_leaf()
1471 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
1475 * @f_ra: read-ahead parameters
1485 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_dir_readahead() local
1486 struct gfs2_glock *gl = ip->i_gl; in gfs2_dir_readahead()
1491 /* First check if we've already read-ahead for the whole range. */ in gfs2_dir_readahead()
1492 if (index + MAX_RA_BLOCKS < f_ra->start) in gfs2_dir_readahead()
1495 f_ra->start = max((pgoff_t)index, f_ra->start); in gfs2_dir_readahead()
1497 if (f_ra->start >= hsize) /* if exceeded the hash table */ in gfs2_dir_readahead()
1501 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]); in gfs2_dir_readahead()
1502 f_ra->start++; in gfs2_dir_readahead()
1513 bh->b_end_io = end_buffer_read_sync; in gfs2_dir_readahead()
1523 * dir_e_read - Reads the entries from a directory into a filldir buffer
1526 * @f_ra: read-ahead parameters
1542 hsize = BIT(dip->i_depth); in dir_e_read()
1543 hash = gfs2_dir_offset2hash(ctx->pos); in dir_e_read()
1544 index = hash >> (32 - dip->i_depth); in dir_e_read()
1546 if (dip->i_hash_cache == NULL) in dir_e_read()
1547 f_ra->start = 0; in dir_e_read()
1561 len = BIT(dip->i_depth - depth); in dir_e_read()
1562 index = (index & ~(len - 1)) + len; in dir_e_read()
1581 if (!dip->i_entries) in gfs2_dir_read()
1584 if (dip->i_diskflags & GFS2_DIF_EXHASH) in gfs2_dir_read()
1589 return -EIO; in gfs2_dir_read()
1596 error = -ENOMEM; in gfs2_dir_read()
1602 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size, in gfs2_dir_read()
1608 if (dip->i_entries != g.offset) { in gfs2_dir_read()
1610 "ip->i_entries (%u) != g.offset (%u)\n", in gfs2_dir_read()
1611 (unsigned long long)dip->i_no_addr, in gfs2_dir_read()
1612 dip->i_entries, in gfs2_dir_read()
1615 error = -EIO; in gfs2_dir_read()
1618 gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries); in gfs2_dir_read()
1620 dip->i_entries, 0, &copied); in gfs2_dir_read()
1634 * gfs2_dir_search - Search a directory
1660 dtype = be16_to_cpu(dent->de_type); in gfs2_dir_search()
1661 rahead = be16_to_cpu(dent->de_rahead); in gfs2_dir_search()
1662 addr = be64_to_cpu(dent->de_inum.no_addr); in gfs2_dir_search()
1663 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino); in gfs2_dir_search()
1666 return ERR_PTR(-EEXIST); in gfs2_dir_search()
1667 inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, in gfs2_dir_search()
1670 GFS2_I(inode)->i_rahead = rahead; in gfs2_dir_search()
1673 return ERR_PTR(-ENOENT); in gfs2_dir_search()
1677 const struct gfs2_inode *ip) in gfs2_dir_check() argument
1681 int ret = -ENOENT; in gfs2_dir_check()
1687 if (ip) { in gfs2_dir_check()
1688 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr) in gfs2_dir_check()
1690 if (be64_to_cpu(dent->de_inum.no_formal_ino) != in gfs2_dir_check()
1691 ip->i_no_formal_ino) in gfs2_dir_check()
1693 if (unlikely(IF2DT(ip->i_inode.i_mode) != in gfs2_dir_check()
1694 be16_to_cpu(dent->de_type))) { in gfs2_dir_check()
1696 ret = -EIO; in gfs2_dir_check()
1708 * dir_new_leaf - Add a new leaf onto hash chain
1723 * Returns: 0 on success, or -ve on error
1729 struct gfs2_inode *ip = GFS2_I(inode); in dir_new_leaf() local
1736 index = name->hash >> (32 - ip->i_depth); in dir_new_leaf()
1737 error = get_first_leaf(ip, index, &obh); in dir_new_leaf()
1742 oleaf = (struct gfs2_leaf *)obh->b_data; in dir_new_leaf()
1743 bn = be64_to_cpu(oleaf->lf_next); in dir_new_leaf()
1747 error = get_leaf(ip, bn, &obh); in dir_new_leaf()
1752 gfs2_trans_add_meta(ip->i_gl, obh); in dir_new_leaf()
1754 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth)); in dir_new_leaf()
1757 return -ENOSPC; in dir_new_leaf()
1759 leaf->lf_dist = cpu_to_be32(dist); in dir_new_leaf()
1760 oleaf->lf_next = cpu_to_be64(bh->b_blocknr); in dir_new_leaf()
1764 error = gfs2_meta_inode_buffer(ip, &bh); in dir_new_leaf()
1767 gfs2_trans_add_meta(ip->i_gl, bh); in dir_new_leaf()
1768 gfs2_add_inode_blocks(&ip->i_inode, 1); in dir_new_leaf()
1769 gfs2_dinode_out(ip, bh->b_data); in dir_new_leaf()
1774 static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip) in gfs2_inode_ra_len() argument
1776 u64 where = ip->i_no_addr + 1; in gfs2_inode_ra_len()
1777 if (ip->i_eattr == where) in gfs2_inode_ra_len()
1783 * gfs2_dir_add - Add new filename into directory
1801 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_dir_add() local
1802 struct buffer_head *bh = da->bh; in gfs2_dir_add()
1803 struct gfs2_dirent *dent = da->dent; in gfs2_dir_add()
1809 if (da->bh == NULL) { in gfs2_dir_add()
1818 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode)); in gfs2_dir_add()
1819 dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip)); in gfs2_dir_add()
1820 tv = inode_set_ctime_current(&ip->i_inode); in gfs2_dir_add()
1821 if (ip->i_diskflags & GFS2_DIF_EXHASH) { in gfs2_dir_add()
1822 leaf = (struct gfs2_leaf *)bh->b_data; in gfs2_dir_add()
1823 be16_add_cpu(&leaf->lf_entries, 1); in gfs2_dir_add()
1824 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); in gfs2_dir_add()
1825 leaf->lf_sec = cpu_to_be64(tv.tv_sec); in gfs2_dir_add()
1827 da->dent = NULL; in gfs2_dir_add()
1828 da->bh = NULL; in gfs2_dir_add()
1830 ip->i_entries++; in gfs2_dir_add()
1831 inode_set_mtime_to_ts(&ip->i_inode, tv); in gfs2_dir_add()
1832 if (S_ISDIR(nip->i_inode.i_mode)) in gfs2_dir_add()
1833 inc_nlink(&ip->i_inode); in gfs2_dir_add()
1838 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) { in gfs2_dir_add()
1849 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) { in gfs2_dir_add()
1850 error = dir_double_exhash(ip); in gfs2_dir_add()
1862 error = -ENOSPC; in gfs2_dir_add()
1870 * gfs2_dir_del - Delete a directory entry
1879 const struct qstr *name = &dentry->d_name; in gfs2_dir_del()
1884 /* Returns _either_ the entry (if its first in block) or the in gfs2_dir_del()
1886 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh); in gfs2_dir_del()
1889 return -EIO; in gfs2_dir_del()
1895 /* If not first in block, adjust pointers accordingly */ in gfs2_dir_del()
1898 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len)); in gfs2_dir_del()
1902 tv = inode_set_ctime_current(&dip->i_inode); in gfs2_dir_del()
1903 if (dip->i_diskflags & GFS2_DIF_EXHASH) { in gfs2_dir_del()
1904 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data; in gfs2_dir_del()
1905 u16 entries = be16_to_cpu(leaf->lf_entries); in gfs2_dir_del()
1908 leaf->lf_entries = cpu_to_be16(--entries); in gfs2_dir_del()
1909 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); in gfs2_dir_del()
1910 leaf->lf_sec = cpu_to_be64(tv.tv_sec); in gfs2_dir_del()
1914 if (!dip->i_entries) in gfs2_dir_del()
1916 dip->i_entries--; in gfs2_dir_del()
1917 inode_set_mtime_to_ts(&dip->i_inode, tv); in gfs2_dir_del()
1919 drop_nlink(&dip->i_inode); in gfs2_dir_del()
1920 mark_inode_dirty(&dip->i_inode); in gfs2_dir_del()
1926 * gfs2_dir_mvino - Change inode number of directory entry
1945 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh); in gfs2_dir_mvino()
1948 return -EIO; in gfs2_dir_mvino()
1953 gfs2_trans_add_meta(dip->i_gl, bh); in gfs2_dir_mvino()
1955 dent->de_type = cpu_to_be16(new_type); in gfs2_dir_mvino()
1958 inode_set_mtime_to_ts(&dip->i_inode, inode_set_ctime_current(&dip->i_inode)); in gfs2_dir_mvino()
1959 mark_inode_dirty_sync(&dip->i_inode); in gfs2_dir_mvino()
1964 * leaf_dealloc - Deallocate a directory leaf
1979 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); in leaf_dealloc()
1999 return -ENOMEM; in leaf_dealloc()
2014 tmp_leaf = (struct gfs2_leaf *)bh->b_data; in leaf_dealloc()
2015 nblk = be64_to_cpu(tmp_leaf->lf_next); in leaf_dealloc()
2028 rg_blocks += rgd->rd_length; in leaf_dealloc()
2036 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) + in leaf_dealloc()
2052 tmp_leaf = (struct gfs2_leaf *)bh->b_data; in leaf_dealloc()
2053 nblk = be64_to_cpu(tmp_leaf->lf_next); in leaf_dealloc()
2059 gfs2_add_inode_blocks(&dip->i_inode, -1); in leaf_dealloc()
2065 error = -EIO; in leaf_dealloc()
2073 gfs2_trans_add_meta(dip->i_gl, dibh); in leaf_dealloc()
2077 dip->i_inode.i_mode = S_IFREG; in leaf_dealloc()
2078 gfs2_dinode_out(dip, dibh->b_data); in leaf_dealloc()
2094 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2097 * Dealloc all on-disk directory leaves to FREEMETA state
2098 * Change on-disk inode type to "regular file"
2113 hsize = BIT(dip->i_depth); in gfs2_dir_exhash_dealloc()
2125 leaf = (struct gfs2_leaf *)bh->b_data; in gfs2_dir_exhash_dealloc()
2126 len = BIT(dip->i_depth - be16_to_cpu(leaf->lf_depth)); in gfs2_dir_exhash_dealloc()
2128 next_index = (index & ~(len - 1)) + len; in gfs2_dir_exhash_dealloc()
2142 error = -EIO; in gfs2_dir_exhash_dealloc()
2151 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2156 * Returns: 0 if ok, -ve on error
2162 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_diradd_alloc_required() local
2164 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf); in gfs2_diradd_alloc_required()
2168 da->nr_blocks = 0; in gfs2_diradd_alloc_required()
2169 da->bh = NULL; in gfs2_diradd_alloc_required()
2170 da->dent = NULL; in gfs2_diradd_alloc_required()
2174 da->nr_blocks = sdp->sd_max_dirres; in gfs2_diradd_alloc_required()
2175 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) && in gfs2_diradd_alloc_required()
2176 (GFS2_DIRENT_SIZE(name->len) < extra)) in gfs2_diradd_alloc_required()
2177 da->nr_blocks = 1; in gfs2_diradd_alloc_required()
2183 if (da->save_loc) { in gfs2_diradd_alloc_required()
2184 da->bh = bh; in gfs2_diradd_alloc_required()
2185 da->dent = dent; in gfs2_diradd_alloc_required()