^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Creates, reads, walks and deletes directory-nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2002, 2004 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Portions of this code from linux/fs/ext3/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (C) 1992, 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Laboratoire MASI - Institut Blaise pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * linux/fs/minix/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <cluster/masklog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "ocfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "blockcheck.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include "dlmglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include "extent_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include "file.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "journal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include "namei.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include "suballoc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "super.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "sysfile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "uptodate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include "ocfs2_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include "buffer_head_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define NAMEI_RA_CHUNKS 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define NAMEI_RA_BLOCKS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int ocfs2_do_extend_dir(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct ocfs2_alloc_context *meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct buffer_head **new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int ocfs2_dir_indexed(struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * These are distinct checks because future versions of the file system will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * want to have a trailing dirent structure independent of indexing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int ocfs2_supports_dir_trailer(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * "new' here refers to the point at which we're creating a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * directory via "mkdir()", but also when we're expanding an inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * directory. In either case, we don't yet have the indexing bit set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * on the directory, so the standard checks will fail in when metaecc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * is turned off. Only directory-initialization type functions should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * use this then. Everything else wants ocfs2_supports_dir_trailer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int ocfs2_new_dir_wants_trailer(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return ocfs2_meta_ecc(osb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) ocfs2_supports_indexed_dirs(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * them more consistent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) char *p = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return (struct ocfs2_dir_block_trailer *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * XXX: This is executed once on every dirent. We should consider optimizing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int ocfs2_skip_dir_trailer(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct ocfs2_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long blklen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!ocfs2_supports_dir_trailer(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (offset != toff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void ocfs2_init_dir_trailer(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct buffer_head *bh, u16 rec_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct ocfs2_dir_block_trailer *trailer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) trailer->db_compat_rec_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) trailer->db_free_rec_len = cpu_to_le16(rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Link an unindexed block with a dir trailer structure into the index free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * list. This function will modify dirdata_bh, but assumes you've already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * passed it to the journal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct buffer_head *dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct buffer_head *dirdata_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct ocfs2_dir_block_trailer *trailer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) trailer->db_free_next = dx_root->dr_free_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ocfs2_journal_dirty(handle, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return res->dl_prev_leaf_bh == NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) brelse(res->dl_dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) brelse(res->dl_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) brelse(res->dl_dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) brelse(res->dl_prev_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int ocfs2_dir_indexed(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Hashing code adapted from ext3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #define DELTA 0x9E3779B9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static void TEA_transform(__u32 buf[4], __u32 const in[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) __u32 sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) __u32 b0 = buf[0], b1 = buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) __u32 a = in[0], b = in[1], c = in[2], d = in[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int n = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) sum += DELTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) } while (--n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) buf[0] += b0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) buf[1] += b1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) __u32 pad, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) pad = (__u32)len | ((__u32)len << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pad |= pad << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (len > num*4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) len = num * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) for (i = 0; i < len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if ((i % 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) val = msg[i] + (val << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if ((i % 4) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *buf++ = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) val = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) num--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (--num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *buf++ = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) while (--num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *buf++ = pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct ocfs2_dx_hinfo *hinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) __u32 in[8], buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * XXX: Is this really necessary, if the index is never looked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * at by readdir? Is a hash value of '0' a bad idea?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if ((len == 1 && !strncmp(".", name, 1)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) (len == 2 && !strncmp("..", name, 2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) buf[0] = buf[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) #ifdef OCFS2_DEBUG_DX_DIRS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * This makes it very easy to debug indexing problems. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * should never allow this to be selected without hand editing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * this file though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) buf[0] = buf[1] = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) memcpy(buf, osb->osb_dx_seed, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) p = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) str2hashbuf(p, len, in, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) TEA_transform(buf, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) len -= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) p += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) hinfo->major_hash = buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) hinfo->minor_hash = buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * bh passed here can be an inode block or a dir data block, depending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * on the inode inline data flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int ocfs2_check_dir_entry(struct inode * dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct ocfs2_dir_entry * de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct buffer_head * bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) const char *error_msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) const int rlen = le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) error_msg = "rec_len is smaller than minimal";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) else if (unlikely(rlen % 4 != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) error_msg = "rec_len % 4 != 0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) error_msg = "rec_len is too small for name_len";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) else if (unlikely(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) error_msg = "directory entry across blocks";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (unlikely(error_msg != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) de->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return error_msg == NULL ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static inline int ocfs2_match(int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) const char * const name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct ocfs2_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (len != de->name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!de->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return !memcmp(name, de->name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * Returns 0 if not found, -1 on failure, and 1 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static inline int ocfs2_search_dirblock(struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) char *first_de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) unsigned int bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct ocfs2_dir_entry **res_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) char *dlimit, *de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int de_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) de_buf = first_de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dlimit = de_buf + bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) while (de_buf < dlimit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* this code is executed quadratically often */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* do minimal checking `by hand' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) de = (struct ocfs2_dir_entry *) de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (de_buf + namelen <= dlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ocfs2_match(namelen, name, de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* found a match - just to be sure, do a full check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) *res_dir = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* prevent looping on a bad block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) de_len = le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (de_len <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) de_buf += de_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) offset += de_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) trace_ocfs2_search_dirblock(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static struct buffer_head *ocfs2_find_entry_id(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct ocfs2_dir_entry **res_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int ret, found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct ocfs2_inline_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) ret = ocfs2_read_inode_block(dir, &di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) data = &di->id2.i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) data->id_data, i_size_read(dir), res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (found == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return di_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int ocfs2_validate_dir_block(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct ocfs2_dir_block_trailer *trailer =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) ocfs2_trailer_from_bh(bh, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * We don't validate dirents here, that's handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * in-place when the code walks them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) BUG_ON(!buffer_uptodate(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * If the ecc fails, we return the error but otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * leave the filesystem running. We know any error is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * local to this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * Note that we are safe to call this even if the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * doesn't have a trailer. Filesystems without metaecc will do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * nothing, and filesystems with it will have one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) (unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Validate a directory trailer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * We check the trailer here rather than in ocfs2_validate_dir_block()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * because that function doesn't have the inode to test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) struct ocfs2_dir_block_trailer *trailer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) rc = ocfs2_error(dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) "Invalid dirblock #%llu: signature = %.*s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) (unsigned long long)bh->b_blocknr, 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) trailer->db_signature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) rc = ocfs2_error(dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) "Directory block #%llu has an invalid db_blkno of %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) (unsigned long long)le64_to_cpu(trailer->db_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (le64_to_cpu(trailer->db_parent_dinode) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) OCFS2_I(dir)->ip_blkno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) rc = ocfs2_error(dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) "Directory block #%llu on dinode #%llu has an invalid parent_dinode of %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) (unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) (unsigned long long)le64_to_cpu(trailer->db_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * This function forces all errors to -EIO for consistency with its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * predecessor, ocfs2_bread(). We haven't audited what returning the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * real error codes would do to callers. We log the real codes with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * mlog_errno() before we squash them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct buffer_head **bh, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct buffer_head *tmp = *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) ocfs2_validate_dir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) mlog_errno(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (!(flags & OCFS2_BH_READAHEAD) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ocfs2_supports_dir_trailer(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) rc = ocfs2_check_dir_trailer(inode, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) brelse(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) mlog_errno(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) *bh = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return rc ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * Read the block at 'phys' which belongs to this directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * inode. This function does no virtual->physical block translation -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * what's passed in is assumed to be a valid directory block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct buffer_head **bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct buffer_head *tmp = *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) ocfs2_validate_dir_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (ocfs2_supports_dir_trailer(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) ret = ocfs2_check_dir_trailer(dir, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (!*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) brelse(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (!ret && !*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) *bh = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static int ocfs2_validate_dx_root(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) BUG_ON(!buffer_uptodate(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) mlog(ML_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) "Checksum failed for dir index root block %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) (unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) ret = ocfs2_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) "Dir Index Root # %llu has bad signature %.*s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 7, dx_root->dr_signature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct buffer_head **dx_root_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) u64 blkno = le64_to_cpu(di->i_dx_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct buffer_head *tmp = *dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) ocfs2_validate_dx_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /* If ocfs2_read_block() got us a new bh, pass it up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (!ret && !*dx_root_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) *dx_root_bh = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int ocfs2_validate_dx_leaf(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) BUG_ON(!buffer_uptodate(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) mlog(ML_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) "Checksum failed for dir index leaf block %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) (unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 7, dx_leaf->dl_signature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct buffer_head **dx_leaf_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct buffer_head *tmp = *dx_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) ocfs2_validate_dx_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /* If ocfs2_read_block() got us a new bh, pass it up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (!ret && !*dx_leaf_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) *dx_leaf_bh = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * Read a series of dx_leaf blocks. This expects all buffer_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * pointers to be NULL on function entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) struct buffer_head **dx_leaf_bhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ocfs2_validate_dx_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) struct ocfs2_dir_entry **res_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct buffer_head *bh_use[NAMEI_RA_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct buffer_head *bh, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) unsigned long start, block, b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) int ra_max = 0; /* Number of bh's in the readahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) buffer, bh_use[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) int ra_ptr = 0; /* Current index into readahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) int num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int nblocks, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) start = OCFS2_I(dir)->ip_dir_start_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (start >= nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) block = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * We deal with the read-ahead logic here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (ra_ptr >= ra_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /* Refill the readahead buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ra_ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) b = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * Terminate if we reach the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * directory and must wrap, or if our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * search has finished at this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (b >= nblocks || (num && block == start)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) bh_use[ra_max] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) num++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) ocfs2_read_dir_block(dir, b++, &bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) OCFS2_BH_READAHEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) bh_use[ra_max] = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if ((bh = bh_use[ra_ptr++]) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /* read error, skip block & hope for the best.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * ocfs2_read_dir_block() has released the bh. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) mlog(ML_ERROR, "reading directory %llu, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) "offset %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) (unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) i = ocfs2_search_dirblock(bh, dir, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) block << sb->s_blocksize_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) bh->b_data, sb->s_blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (i == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) OCFS2_I(dir)->ip_dir_start_lookup = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) ret = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) goto cleanup_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (++block >= nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) } while (block != start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) * If the directory has grown while we were searching, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * search the last part of the directory before giving up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) block = nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (block < nblocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) cleanup_and_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) /* Clean up the read-ahead blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) for (; ra_ptr < ra_max; ra_ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) brelse(bh_use[ra_ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) trace_ocfs2_find_entry_el(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) struct ocfs2_extent_list *el,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) u32 major_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) u32 *ret_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) u64 *ret_phys_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) unsigned int *ret_clen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) int ret = 0, i, found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct buffer_head *eb_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct ocfs2_extent_block *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) struct ocfs2_extent_rec *rec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (el->l_tree_depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) &eb_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) eb = (struct ocfs2_extent_block *) eb_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) el = &eb->h_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (el->l_tree_depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) ret = ocfs2_error(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) "Inode %lu has non zero tree depth in btree tree block %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) (unsigned long long)eb_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) rec = &el->l_recs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (le32_to_cpu(rec->e_cpos) <= major_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) ret = ocfs2_error(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) "Inode %lu has bad extent record (%u, %u, 0) in btree\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) le32_to_cpu(rec->e_cpos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ocfs2_rec_clusters(el, rec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (ret_phys_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (ret_cpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) *ret_cpos = le32_to_cpu(rec->e_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (ret_clen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) brelse(eb_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * Returns the block index, from the start of the cluster which this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) * hash belongs too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) u32 minor_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return minor_hash & osb->osb_dx_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) struct ocfs2_dx_hinfo *hinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) static int ocfs2_dx_dir_lookup(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct ocfs2_extent_list *el,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct ocfs2_dx_hinfo *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) u32 *ret_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) u64 *ret_phys_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) unsigned int cend, clen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) u32 cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) u32 name_hash = hinfo->major_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) &clen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) cend = cpos + clen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (name_hash >= cend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) /* We want the last cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) cpos += clen - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) blkno += ocfs2_clusters_to_blocks(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) name_hash - cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) cpos = name_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * We now have the cluster which should hold our entry. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * find the exact block from the start of the cluster to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * search, we take the lower bits of the hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (ret_phys_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) *ret_phys_blkno = blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (ret_cpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) *ret_cpos = cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) static int ocfs2_dx_dir_search(const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct ocfs2_dx_root_block *dx_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) struct ocfs2_dir_lookup_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) int ret, i, found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) u64 phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) struct buffer_head *dx_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) struct ocfs2_dx_leaf *dx_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) struct ocfs2_dx_entry *dx_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) struct buffer_head *dir_ent_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) struct ocfs2_dir_entry *dir_ent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct ocfs2_extent_list *dr_el;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct ocfs2_dx_entry_list *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (ocfs2_dx_root_inline(dx_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) entry_list = &dx_root->dr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) goto search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) dr_el = &dx_root->dr_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) namelen, name, hinfo->major_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) hinfo->minor_hash, (unsigned long long)phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) trace_ocfs2_dx_dir_search_leaf_info(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) le16_to_cpu(dx_leaf->dl_list.de_num_used),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) le16_to_cpu(dx_leaf->dl_list.de_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) entry_list = &dx_leaf->dl_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) search:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * Empty leaf is legal, so no need to check for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) dx_entry = &entry_list->de_entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * Search unindexed leaf block now. We're not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * guaranteed to find anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) ret = ocfs2_read_dir_block_direct(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) le64_to_cpu(dx_entry->dx_dirent_blk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) &dir_ent_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * XXX: We should check the unindexed block here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * before using it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) 0, dir_ent_bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) dir->i_sb->s_blocksize, &dir_ent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) if (found == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (found == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) /* This means we found a bad directory entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) brelse(dir_ent_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) dir_ent_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (found <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) res->dl_leaf_bh = dir_ent_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) res->dl_entry = dir_ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) res->dl_dx_leaf_bh = dx_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) res->dl_dx_entry = dx_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) brelse(dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) brelse(dir_ent_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int ocfs2_find_entry_dx(const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) ret = ocfs2_read_inode_block(dir, &di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) lookup->dl_dx_root_bh = dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * Try to find an entry of the provided name within 'dir'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * If nothing was found, -ENOENT is returned. Otherwise, zero is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * returned and the struct 'res' will contain information useful to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * other directory manipulation functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * Caller can NOT assume anything about the contents of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * buffer_heads - they are passed back only so that it can be passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * into any one of the manipulation functions (add entry, delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * entry, etc). As an example, bh in the extent directory case is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * data block, in the inline-data case it actually points to an inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) * in the indexed directory case, multiple buffers are involved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) int ocfs2_find_entry(const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) struct ocfs2_dir_entry *res_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (ocfs2_dir_indexed(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) return ocfs2_find_entry_dx(name, namelen, dir, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * The unindexed dir code only uses part of the lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * structure, so there's no reason to push it down further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * than this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (bh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) lookup->dl_leaf_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) lookup->dl_entry = res_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * Update inode number and type of a previously found directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) int ocfs2_update_entry(struct inode *dir, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) struct ocfs2_dir_lookup_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) struct inode *new_entry_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) ocfs2_journal_access_func access = ocfs2_journal_access_db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) struct ocfs2_dir_entry *de = res->dl_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) struct buffer_head *de_bh = res->dl_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * The same code works fine for both inline-data and extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) * based directories, so no need to split this up. The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) * difference is the journal_access function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) access = ocfs2_journal_access_di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) ret = access(handle, INODE_CACHE(dir), de_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) ocfs2_set_de_type(de, new_entry_inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) ocfs2_journal_dirty(handle, de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * __ocfs2_delete_entry deletes a directory entry by merging it with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * previous entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) struct ocfs2_dir_entry *de_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) struct buffer_head *bh, char *first_de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) struct ocfs2_dir_entry *de, *pde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) int i, status = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) ocfs2_journal_access_func access = ocfs2_journal_access_db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) access = ocfs2_journal_access_di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) pde = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) de = (struct ocfs2_dir_entry *) first_de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) while (i < bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) status = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) if (de == de_del) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) status = access(handle, INODE_CACHE(dir), bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) status = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if (pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) le16_add_cpu(&pde->rec_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) le16_to_cpu(de->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) de->inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) ocfs2_journal_dirty(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) i += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) pde = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) unsigned int hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) if (le64_to_cpu(de->inode) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) hole = le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) hole = le16_to_cpu(de->rec_len) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) OCFS2_DIR_REC_LEN(de->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) return hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) static int ocfs2_find_max_rec_len(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) struct buffer_head *dirblock_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) int size, this_hole, largest_hole = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) size = ocfs2_dir_trailer_blk_off(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) limit = start + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) de_buf = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (de_buf != trailer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) this_hole = ocfs2_figure_dirent_hole(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (this_hole > largest_hole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) largest_hole = this_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) de_buf += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) } while (de_buf < limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) return largest_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) int num_used = le16_to_cpu(entry_list->de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) if (num_used == 1 || index == (num_used - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) goto clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) memmove(&entry_list->de_entries[index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) &entry_list->de_entries[index + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) clear:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) num_used--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) memset(&entry_list->de_entries[num_used], 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) sizeof(struct ocfs2_dx_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) entry_list->de_num_used = cpu_to_le16(num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) int ret, index, max_rec_len, add_to_free_list = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) struct ocfs2_dx_leaf *dx_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) struct ocfs2_dir_block_trailer *trailer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) struct ocfs2_dx_entry_list *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) * This function gets a bit messy because we might have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) * modify the root block, regardless of whether the indexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) * entries are stored inline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * *Only* set 'entry_list' here, based on where we're looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) * for the indexed entries. Later, we might still want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) * journal both blocks, based on free list state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (ocfs2_dx_root_inline(dx_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) entry_list = &dx_root->dr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) entry_list = &dx_leaf->dl_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) /* Neither of these are a disk corruption - that should have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) * been caught by lookup, before we got here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) index = (char *)dx_entry - (char *)entry_list->de_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) index /= sizeof(*dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (index >= le16_to_cpu(entry_list->de_num_used)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) entry_list, dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) * We know that removal of this dirent will leave enough room
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) * for a new one, so add this block to the free list if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) * isn't already there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (trailer->db_free_rec_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) add_to_free_list = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) * Add the block holding our index into the journal before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) * removing the unindexed entry. If we get an error return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) * from __ocfs2_delete_entry(), then it hasn't removed the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) * entry yet. Likewise, successful return means we *must*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) * remove the indexed entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) * We're also careful to journal the root tree block here as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) * the entry count needs to be updated. Also, we might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) * adding to the start of the free list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) if (!ocfs2_dx_root_inline(dx_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) lookup->dl_dx_leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (add_to_free_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) trailer->db_free_next = dx_root->dr_free_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) ocfs2_journal_dirty(handle, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) ocfs2_journal_dirty(handle, leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) le32_add_cpu(&dx_root->dr_num_entries, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) ocfs2_journal_dirty(handle, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) ocfs2_dx_list_remove_entry(entry_list, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (!ocfs2_dx_root_inline(dx_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) static inline int ocfs2_delete_entry_id(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) struct ocfs2_dir_entry *de_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) struct ocfs2_inline_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) ret = ocfs2_read_inode_block(dir, &di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) data = &di->id2.i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) i_size_read(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) static inline int ocfs2_delete_entry_el(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) struct ocfs2_dir_entry *de_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) * Delete a directory entry. Hide the details of directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) * implementation from the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) int ocfs2_delete_entry(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) struct ocfs2_dir_lookup_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) if (ocfs2_dir_indexed(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) return ocfs2_delete_entry_dx(handle, dir, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) res->dl_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) res->dl_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) * Check whether 'de' has enough room to hold an entry of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) * 'new_rec_len' bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) unsigned int new_rec_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) unsigned int de_really_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) /* Check whether this is an empty record with enough space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) if (le64_to_cpu(de->inode) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) le16_to_cpu(de->rec_len) >= new_rec_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) * Record might have free space at the end which we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) * use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) struct ocfs2_dx_entry *dx_new_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) struct ocfs2_dx_hinfo *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) u64 dirent_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) struct ocfs2_dx_entry *dx_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) i = le16_to_cpu(entry_list->de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) dx_entry = &entry_list->de_entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) memset(dx_entry, 0, sizeof(*dx_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) le16_add_cpu(&entry_list->de_num_used, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) struct ocfs2_dx_hinfo *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) u64 dirent_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) struct buffer_head *dx_leaf_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) struct ocfs2_dx_leaf *dx_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) ocfs2_journal_dirty(handle, dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) struct ocfs2_dx_hinfo *hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) u64 dirent_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) struct ocfs2_dx_root_block *dx_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) if (ocfs2_dx_root_inline(dx_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) ocfs2_dx_inline_root_insert(dir, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) &lookup->dl_hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) lookup->dl_leaf_bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) dx_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) lookup->dl_leaf_bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) lookup->dl_dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) le32_add_cpu(&dx_root->dr_num_entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) ocfs2_journal_dirty(handle, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) static void ocfs2_remove_block_from_free_list(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) struct ocfs2_dir_block_trailer *trailer, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (ocfs2_free_list_at_root(lookup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) bh = lookup->dl_dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) dx_root->dr_free_blk = trailer->db_free_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) bh = lookup->dl_prev_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) prev->db_free_next = trailer->db_free_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) trailer->db_free_rec_len = cpu_to_le16(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) trailer->db_free_next = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) ocfs2_journal_dirty(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * This expects that a journal write has been reserved on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) int max_rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) struct ocfs2_dir_block_trailer *trailer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) if (max_rec_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) * There's still room in this block, so no need to remove it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) * from the free list. In this case, we just want to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) * the rec len accounting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) ocfs2_remove_block_from_free_list(dir, handle, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) /* we don't always have a dentry for what we want to add, so people
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) * like orphan dir can call this instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) * The lookup context must have been filled from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) * ocfs2_prepare_dir_for_insert.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) int __ocfs2_add_entry(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) struct inode *inode, u64 blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) unsigned short rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) struct ocfs2_dir_entry *de, *de1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) unsigned int size = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) struct buffer_head *insert_bh = lookup->dl_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) char *data_start = insert_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) if (!namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) if (ocfs2_dir_indexed(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) * An indexed dir may require that we update the free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * list. Reserve a write to the previous node in the list so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) * that we don't fail later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * XXX: This can be either a dx_root_block, or an unindexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) * directory tree leaf block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) if (ocfs2_free_list_at_root(lookup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) bh = lookup->dl_dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) retval = ocfs2_journal_access_dr(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) INODE_CACHE(dir), bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) bh = lookup->dl_prev_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) retval = ocfs2_journal_access_db(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) INODE_CACHE(dir), bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) mlog_errno(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) data_start = di->id2.i_data.id_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) size = i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) BUG_ON(insert_bh != parent_fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) rec_len = OCFS2_DIR_REC_LEN(namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) de = (struct ocfs2_dir_entry *) data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) BUG_ON((char *)de >= (size + data_start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) /* These checks should've already been passed by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) * prepare function, but I guess we can leave them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) * here anyway. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) if (ocfs2_match(namelen, name, de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) retval = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) /* We're guaranteed that we should have space, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) * can't possibly have hit the trailer...right? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) "Hit dir trailer trying to insert %.*s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) "(namelen %d) into directory %llu. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) "offset is %lu, trailer offset is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) namelen, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) (unsigned long long)parent_fe_bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) if (ocfs2_dirent_would_fit(de, rec_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) mlog_errno(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (insert_bh == parent_fe_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) retval = ocfs2_journal_access_di(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) insert_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) retval = ocfs2_journal_access_db(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) insert_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) if (!retval && ocfs2_dir_indexed(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) retval = ocfs2_dx_dir_insert(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) mlog_errno(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) /* By now the buffer is marked for journaling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) offset += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) if (le64_to_cpu(de->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) de1 = (struct ocfs2_dir_entry *)((char *) de +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) OCFS2_DIR_REC_LEN(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) de1->rec_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) cpu_to_le16(le16_to_cpu(de->rec_len) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) OCFS2_DIR_REC_LEN(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) de = de1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) de->file_type = FT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) if (blkno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) de->inode = cpu_to_le64(blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) ocfs2_set_de_type(de, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) de->inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) de->name_len = namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) memcpy(de->name, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) if (ocfs2_dir_indexed(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) ocfs2_recalc_free_list(dir, handle, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) ocfs2_journal_dirty(handle, insert_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) offset += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) /* when you think about it, the assert above should prevent us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) * from ever getting here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) retval = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) mlog_errno(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) static int ocfs2_dir_foreach_blk_id(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) u64 *f_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) unsigned long offset = ctx->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) struct ocfs2_inline_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) ret = ocfs2_read_inode_block(inode, &di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) (unsigned long long)OCFS2_I(inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) data = &di->id2.i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) while (ctx->pos < i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) /* If the dir block has changed since the last call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) * readdir(2), then we might be pointing to an invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) * dirent right now. Scan from the start of the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) * to make sure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) if (!inode_eq_iversion(inode, *f_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) for (i = 0; i < i_size_read(inode) && i < offset; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) de = (struct ocfs2_dir_entry *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) (data->id_data + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) /* It's too expensive to do a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) * dirent test each time round this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) * loop, but we do have to test at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) * least that it is non-zero. A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) * failure will be detected in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) * dirent test below. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) if (le16_to_cpu(de->rec_len) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) OCFS2_DIR_REC_LEN(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) i += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) ctx->pos = offset = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) *f_version = inode_query_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) /* On error, skip the f_pos to the end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) ctx->pos = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) offset += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) if (le64_to_cpu(de->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) if (!dir_emit(ctx, de->name, de->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) le64_to_cpu(de->inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) fs_ftype_to_dtype(de->file_type)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) ctx->pos += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) * NOTE: This function can be called against unindexed directories,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) * and indexed ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) static int ocfs2_dir_foreach_blk_el(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) u64 *f_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) bool persist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) unsigned long offset, blk, last_ra_blk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) struct buffer_head * bh, * tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) struct ocfs2_dir_entry * de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) struct super_block * sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) unsigned int ra_sectors = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) int stored = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) offset = ctx->pos & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) while (ctx->pos < i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) blk = ctx->pos >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) /* Skip the corrupt dirblock and keep trying */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) ctx->pos += sb->s_blocksize - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) /* The idea here is to begin with 8k read-ahead and to stay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) * 4k ahead of our current position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) * TODO: Use the pagecache for this. We just need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) * make sure it's cluster-safe... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) if (!last_ra_blk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) OCFS2_BH_READAHEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) brelse(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) last_ra_blk = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) ra_sectors = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) /* If the dir block has changed since the last call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) * readdir(2), then we might be pointing to an invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) * dirent right now. Scan from the start of the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) * to make sure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) if (!inode_eq_iversion(inode, *f_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) for (i = 0; i < sb->s_blocksize && i < offset; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) de = (struct ocfs2_dir_entry *) (bh->b_data + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) /* It's too expensive to do a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) * dirent test each time round this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) * loop, but we do have to test at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) * least that it is non-zero. A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) * failure will be detected in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) * dirent test below. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) if (le16_to_cpu(de->rec_len) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) OCFS2_DIR_REC_LEN(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) i += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) offset = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) | offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) *f_version = inode_query_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) while (ctx->pos < i_size_read(inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) && offset < sb->s_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) /* On error, skip the f_pos to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) next block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (le64_to_cpu(de->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (!dir_emit(ctx, de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) de->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) le64_to_cpu(de->inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) fs_ftype_to_dtype(de->file_type))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) stored++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) offset += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) ctx->pos += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) if (!persist && stored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) bool persist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) * This is intended to be called from inside other kernel functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) * so we fake some arguments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) u64 version = inode_query_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) ocfs2_dir_foreach_blk(inode, &version, ctx, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) * ocfs2_readdir()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) int ocfs2_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) int lock_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) if (lock_level && error >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) /* We release EX lock which used to update atime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) * and get PR lock again to reduce contention
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) * on commonly accessed directories. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) ocfs2_inode_unlock(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) lock_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) error = ocfs2_inode_lock(inode, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) if (error != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) mlog_errno(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) /* we haven't got any yet, so propagate the error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) goto bail_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) ocfs2_inode_unlock(inode, lock_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) mlog_errno(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) bail_nolock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) * NOTE: this should always be called with parent dir i_mutex taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) int ocfs2_find_files_on_disk(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) u64 *blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) int status = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) trace_ocfs2_find_files_on_disk(namelen, name, blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) (unsigned long long)OCFS2_I(inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) status = ocfs2_find_entry(name, namelen, inode, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) *blkno = le64_to_cpu(lookup->dl_entry->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) * Convenience function for callers which just want the block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) * mapped to a name and don't require the full dirent info, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) int namelen, u64 *blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) struct ocfs2_dir_lookup_result lookup = { NULL, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) ocfs2_free_dir_lookup_result(&lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) /* Check for a name within a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) * Return 0 if the name does not exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) * Return -EEXIST if the directory contains the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) * Callers should have i_mutex + a cluster lock on dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) int ocfs2_check_dir_for_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) struct ocfs2_dir_lookup_result lookup = { NULL, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) trace_ocfs2_check_dir_for_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) ocfs2_free_dir_lookup_result(&lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) struct ocfs2_empty_dir_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) struct dir_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) unsigned seen_dot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) unsigned seen_dot_dot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) unsigned seen_other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) unsigned dx_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) int name_len, loff_t pos, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) unsigned type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) struct ocfs2_empty_dir_priv *p =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) * Check the positions of "." and ".." records to be sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) * they're in the correct place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) * Indexed directories don't need to proceed past the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) * two entries, so we end the scan after seeing '..'. Despite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) * that, we allow the scan to proceed In the event that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) * have a corrupted indexed directory (no dot or dot dot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) * entries). This allows us to double check for existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) * entries which might not have been found in the index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) p->seen_dot = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) if (name_len == 2 && !strncmp("..", name, 2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) pos == OCFS2_DIR_REC_LEN(1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) p->seen_dot_dot = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) if (p->dx_dir && p->seen_dot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) p->seen_other = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) static int ocfs2_empty_dir_dx(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) struct ocfs2_empty_dir_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) priv->dx_dir = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) ret = ocfs2_read_inode_block(inode, &di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) if (le32_to_cpu(dx_root->dr_num_entries) != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) priv->seen_other = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) * routine to check that the specified directory is empty (for rmdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) * Returns 1 if dir is empty, zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) * XXX: This is a performance problem for unindexed directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) int ocfs2_empty_dir(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) struct ocfs2_empty_dir_priv priv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) .ctx.actor = ocfs2_empty_dir_filldir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) if (ocfs2_dir_indexed(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) ret = ocfs2_empty_dir_dx(inode, &priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) * We still run ocfs2_dir_foreach to get the checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) * for "." and "..".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) ret = ocfs2_dir_foreach(inode, &priv.ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) if (!priv.seen_dot || !priv.seen_dot_dot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) (unsigned long long)OCFS2_I(inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) * XXX: Is it really safe to allow an unlink to continue?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) return !priv.seen_other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) * Fills "." and ".." dirents in a new directory block. Returns dirent for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) * "..", which might be used during creation of a directory with a trailing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) * header. It is otherwise safe to ignore the return code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) char *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) de->name_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) de->rec_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) strcpy(de->name, ".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) ocfs2_set_de_type(de, S_IFDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) de->name_len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) strcpy(de->name, "..");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) ocfs2_set_de_type(de, S_IFDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) return de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) * This works together with code in ocfs2_mknod_locked() which sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) * the inline-data flag and initializes the inline-data section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) struct buffer_head *di_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) struct ocfs2_inline_data *data = &di->id2.i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) unsigned int size = le16_to_cpu(data->id_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) i_size_write(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) inode->i_blocks = ocfs2_inode_sector_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) struct buffer_head *fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) struct buffer_head **ret_new_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) unsigned int size = osb->sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) struct buffer_head *new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) if (ocfs2_new_dir_wants_trailer(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) size = ocfs2_dir_trailer_blk_off(parent->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) data_ac, NULL, &new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) memset(new_bh->b_data, 0, osb->sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) if (ocfs2_new_dir_wants_trailer(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) int size = le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) * Figure out the size of the hole left over after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) * insertion of '.' and '..'. The trailer wants this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) * information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) size -= OCFS2_DIR_REC_LEN(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) size -= sizeof(struct ocfs2_dir_block_trailer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) ocfs2_init_dir_trailer(inode, new_bh, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) ocfs2_journal_dirty(handle, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) i_size_write(inode, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) inode->i_blocks = ocfs2_inode_sector_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) if (ret_new_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) *ret_new_bh = new_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) struct buffer_head *dirdata_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) struct ocfs2_alloc_context *meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) int dx_inline, u32 num_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) struct buffer_head **ret_dx_root_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) u16 dr_suballoc_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) u64 suballoc_loc, dr_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) unsigned int num_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) struct ocfs2_dir_block_trailer *trailer =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) &dr_suballoc_bit, &num_bits, &dr_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) trace_ocfs2_dx_dir_attach_index(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) (unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) (unsigned long long)dr_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) dx_root_bh = sb_getblk(osb->sb, dr_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) if (dx_root_bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) memset(dx_root, 0, osb->sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) dx_root->dr_blkno = cpu_to_le64(dr_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) dx_root->dr_num_entries = cpu_to_le32(num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) if (le16_to_cpu(trailer->db_free_rec_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) dx_root->dr_free_blk = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) if (dx_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) dx_root->dr_entries.de_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) dx_root->dr_list.l_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) ocfs2_journal_dirty(handle, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) di->i_dx_root = cpu_to_le64(dr_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) spin_lock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) spin_unlock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) *ret_dx_root_bh = dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) struct buffer_head **dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) int num_dx_leaves, u64 start_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) struct ocfs2_dx_leaf *dx_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) for (i = 0; i < num_dx_leaves; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) bh = sb_getblk(osb->sb, start_blk + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) if (bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) dx_leaves[i] = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) memset(dx_leaf, 0, osb->sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) dx_leaf->dl_list.de_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) trace_ocfs2_dx_dir_format_cluster(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) (unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) le16_to_cpu(dx_leaf->dl_list.de_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) ocfs2_journal_dirty(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) * Allocates and formats a new cluster for use in an indexed dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) * leaf. This version will not do the extent insert, so that it can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) * used by operations which need careful ordering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) u32 cpos, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) struct buffer_head **dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) int num_dx_leaves, u64 *ret_phys_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) u32 phys, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) u64 phys_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) * XXX: For create, this should claim cluster for the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) * *before* the unindexed insert so that we have a better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) * chance of contiguousness as the directory grows in number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) * of entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) * Format the new cluster first. That way, we're inserting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) * valid data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) num_dx_leaves, phys_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) *ret_phys_blkno = phys_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) static int ocfs2_dx_dir_new_cluster(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) struct ocfs2_extent_tree *et,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) u32 cpos, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) struct ocfs2_alloc_context *meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) struct buffer_head **dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) int num_dx_leaves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) u64 phys_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) num_dx_leaves, &phys_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) int *ret_num_leaves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) struct buffer_head **dx_leaves;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) if (dx_leaves && ret_num_leaves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) *ret_num_leaves = num_dx_leaves;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) return dx_leaves;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) struct ocfs2_alloc_context *meta_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) struct buffer_head *leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) struct ocfs2_dx_hinfo hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) struct ocfs2_dx_entry_list *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) * Our strategy is to create the directory as though it were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) * unindexed, then add the index block. This works with very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) * little complication since the state of a new directory is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) * very well known quantity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) * Essentially, we have two dirents ("." and ".."), in the 1st
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) * block which need indexing. These are easily inserted into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) * the index block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) data_ac, &leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) meta_ac, 1, 2, &dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) entry_list = &dx_root->dr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) brelse(leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) int ocfs2_fill_new_dir(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) struct buffer_head *fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) struct ocfs2_alloc_context *meta_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) if (ocfs2_supports_indexed_dirs(osb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) data_ac, meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) data_ac, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) static int ocfs2_dx_dir_index_block(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) struct buffer_head **dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) int num_dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) u32 *num_dx_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) struct buffer_head *dirent_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) int ret = 0, namelen, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) char *de_buf, *limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) struct buffer_head *dx_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) struct ocfs2_dx_hinfo hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) u64 dirent_blk = dirent_bh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) de_buf = dirent_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) limit = de_buf + dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) while (de_buf < limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) namelen = de->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) if (!namelen || !de->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) goto inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) dx_leaf_bh = dx_leaves[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) dirent_blk, dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) *num_dx_entries = *num_dx_entries + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) inc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) de_buf += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) * XXX: This expects dx_root_bh to already be part of the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) static void ocfs2_dx_dir_index_root_block(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) struct buffer_head *dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) struct buffer_head *dirent_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) char *de_buf, *limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) struct ocfs2_dx_hinfo hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) u64 dirent_blk = dirent_bh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) de_buf = dirent_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) limit = de_buf + dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) while (de_buf < limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) if (!de->name_len || !de->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) goto inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) trace_ocfs2_dx_dir_index_root_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) (unsigned long long)dir->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) hinfo.major_hash, hinfo.minor_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) de->name_len, de->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) le16_to_cpu(dx_root->dr_entries.de_num_used));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) dirent_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) le32_add_cpu(&dx_root->dr_num_entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) inc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) de_buf += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) * Count the number of inline directory entries in di_bh and compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) * them against the number of entries we can hold in an inline dx root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) static int ocfs2_new_dx_should_be_inline(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) struct buffer_head *di_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) int dirent_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) char *de_buf, *limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) de_buf = di->id2.i_data.id_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) limit = de_buf + i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) while (de_buf < limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) if (de->name_len && de->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) dirent_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) de_buf += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) /* We are careful to leave room for one extra record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) * Expand rec_len of the rightmost dirent in a directory block so that it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) * contains the end of our valid space for dirents. We do this during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) * expansion from an inline directory to one with extents. The first dir block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) * in that case is taken from the inline data portion of the inode block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) * This will also return the largest amount of contiguous space for a dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) * in the block. That value is *not* necessarily the last dirent, even after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) * expansion. The directory indexing code wants this value for free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) * accounting. We do this here since we're already walking the entire dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) * We add the dir trailer if this filesystem wants it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) struct ocfs2_dir_entry *prev_de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) char *de_buf, *limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) unsigned int new_size = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) unsigned int bytes, this_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) unsigned int largest_hole = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) if (ocfs2_new_dir_wants_trailer(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) new_size = ocfs2_dir_trailer_blk_off(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) bytes = new_size - old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) limit = start + old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) de_buf = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) this_hole = ocfs2_figure_dirent_hole(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) if (this_hole > largest_hole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) largest_hole = this_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) prev_de = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) de_buf += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) } while (de_buf < limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) le16_add_cpu(&prev_de->rec_len, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) /* We need to double check this after modification of the final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) * dirent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) this_hole = ocfs2_figure_dirent_hole(prev_de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) if (this_hole > largest_hole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) largest_hole = this_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) return largest_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) * We allocate enough clusters to fulfill "blocks_wanted", but set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) * i_size to exactly one block. Ocfs2_extend_dir() will handle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) * rest automatically for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) * *first_block_bh is a pointer to the 1st data block allocated to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) * directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) unsigned int blocks_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) struct ocfs2_dir_lookup_result *lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) struct buffer_head **first_block_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) int ret, i, num_dx_leaves = 0, dx_inline = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) credits = ocfs2_inline_to_extents_credits(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) u64 dx_insert_blkno, blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) bytes = blocks_wanted << sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) struct ocfs2_inode_info *oi = OCFS2_I(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) struct ocfs2_alloc_context *data_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) struct ocfs2_alloc_context *meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) struct buffer_head *dirdata_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) struct buffer_head **dx_leaves = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) struct ocfs2_extent_tree et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) struct ocfs2_extent_tree dx_et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) int did_quota = 0, bytes_allocated = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) alloc = ocfs2_clusters_for_bytes(sb, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) dx_alloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) down_write(&oi->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) if (ocfs2_supports_indexed_dirs(osb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) credits += ocfs2_add_dir_index_credits(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) if (!dx_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) /* Add one more cluster for an index leaf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) dx_alloc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) &num_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) if (!dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) /* This gets us the dx_root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) * We should never need more than 2 clusters for the unindexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) * tree - maximum dirent size is far less than one block. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) * fact, the only time we'd need more than one cluster is if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) * blocksize == clustersize and the dirent won't fit in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) * extra space that the expansion to a single block gives. As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) * of today, that only happens on 4k/4k file systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) BUG_ON(alloc > 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) * Prepare for worst case allocation scenario of two separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) * extents in the unindexed tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) if (alloc == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) credits += OCFS2_SUBALLOC_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) handle = ocfs2_start_trans(osb, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) ret = dquot_alloc_space_nodirty(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) did_quota = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) * Allocate our index cluster first, to maximize the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) * possibility that unindexed leaves grow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) * contiguously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) dx_leaves, num_dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) &dx_insert_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) * Try to claim as many clusters as the bitmap can give though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * if we only get one now, that's enough to continue. The rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * will be claimed after the conversion to extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) if (ocfs2_dir_resv_allowed(osb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) data_ac->ac_resv = &oi->ip_la_data_resv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) * Operations are carefully ordered so that we set up the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) * data block first. The conversion from inline data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) * extents follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) dirdata_bh = sb_getblk(sb, blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) if (!dirdata_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) memset(dirdata_bh->b_data + i_size_read(dir), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) sb->s_blocksize - i_size_read(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) if (ocfs2_new_dir_wants_trailer(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) * Prepare the dir trailer up front. It will otherwise look
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) * like a valid dirent. Even if inserting the index fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) * (unlikely), then all we'll have done is given first dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) * block a small amount of fragmentation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) ocfs2_init_dir_trailer(dir, dirdata_bh, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) ocfs2_update_inode_fsync_trans(handle, dir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) ocfs2_journal_dirty(handle, dirdata_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) * Dx dirs with an external cluster need to do this up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) * front. Inline dx root's get handled later, after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) * we've allocated our root block. We get passed back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) * a total number of items so that dr_num_entries can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) * be correctly set once the dx_root has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) * allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) num_dx_leaves, &num_dx_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) dirdata_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) * Set extent, i_size, etc on the directory. After this, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) * inode should contain the same exact dirents as before and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) * be fully accessible from system calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) * We let the later dirent insert modify c/mtime - to the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) * the data hasn't changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) spin_lock(&oi->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) spin_unlock(&oi->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) ocfs2_dinode_new_extent_list(dir, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) i_size_write(dir, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) di->i_size = cpu_to_le64(sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) ocfs2_update_inode_fsync_trans(handle, dir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) * This should never fail as our extent list is empty and all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) * related blocks have been journaled already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) * Set i_blocks after the extent insert for the most up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) * date ip_clusters value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) dir->i_blocks = ocfs2_inode_sector_count(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) if (ocfs2_supports_indexed_dirs(osb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) dirdata_bh, meta_ac, dx_inline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) num_dx_entries, &dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) if (dx_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) dirdata_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) ocfs2_init_dx_root_extent_tree(&dx_et,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) ret = ocfs2_insert_extent(handle, &dx_et, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) dx_insert_blkno, 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) * We asked for two clusters, but only got one in the 1st
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) * pass. Claim the 2nd cluster as a separate extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) if (alloc > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) ret = ocfs2_insert_extent(handle, &et, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) blkno, len, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) *first_block_bh = dirdata_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) dirdata_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) if (ocfs2_supports_indexed_dirs(osb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) unsigned int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) if (!dx_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) * We need to return the correct block within the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) * cluster which should hold our entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) off = ocfs2_dx_dir_hash_idx(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) &lookup->dl_hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) get_bh(dx_leaves[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) lookup->dl_dx_leaf_bh = dx_leaves[off];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) lookup->dl_dx_root_bh = dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) if (ret < 0 && did_quota)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) dquot_free_space_nodirty(dir, bytes_allocated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) up_write(&oi->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) if (data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) ocfs2_free_alloc_context(data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) if (meta_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) ocfs2_free_alloc_context(meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) if (dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) for (i = 0; i < num_dx_leaves; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) brelse(dx_leaves[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) kfree(dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) brelse(dirdata_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) /* returns a bh of the 1st new block in the allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) static int ocfs2_do_extend_dir(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) struct ocfs2_alloc_context *data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) struct ocfs2_alloc_context *meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) struct buffer_head **new_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) int extend, did_quota = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) u64 p_blkno, v_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) spin_lock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) spin_unlock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) if (extend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) u32 offset = OCFS2_I(dir)->ip_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) status = dquot_alloc_space_nodirty(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) ocfs2_clusters_to_bytes(sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) did_quota = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 1, 0, parent_fe_bh, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) data_ac, meta_ac, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) BUG_ON(status == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) *new_bh = sb_getblk(sb, p_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) if (!*new_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) if (did_quota && status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) * Assumes you already have a cluster lock on the directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) * 'blocks_wanted' is only used if we have an inline directory which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) * is to be turned into an extent based one. The size of the dirent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) * insert might be larger than the space gained by growing to just one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) * block, so we may have to grow the inode by two blocks in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) * If the directory is already indexed, dx_root_bh must be provided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) static int ocfs2_extend_dir(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) unsigned int blocks_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) struct ocfs2_dir_lookup_result *lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) struct buffer_head **new_de_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) int credits, num_free_extents, drop_alloc_sem = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) loff_t dir_i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) struct ocfs2_extent_list *el = &fe->id2.i_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) struct ocfs2_alloc_context *data_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) struct ocfs2_alloc_context *meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) struct buffer_head *new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) struct ocfs2_dir_entry * de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) struct super_block *sb = osb->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) struct ocfs2_extent_tree et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) * This would be a code error as an inline directory should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) * never have an index root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) BUG_ON(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) blocks_wanted, lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) &new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) /* Expansion from inline to an indexed directory will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) * have given us this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) dx_root_bh = lookup->dl_dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) if (blocks_wanted == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) * If the new dirent will fit inside the space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) * created by pushing out to one block, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) * we can complete the operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) * here. Otherwise we have to expand i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) * and format the 2nd block below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) BUG_ON(new_bh == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) goto bail_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) * Get rid of 'new_bh' - we want to format the 2nd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) * data block and return that instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) down_write(&OCFS2_I(dir)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) drop_alloc_sem = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) dir_i_size = i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) goto do_extend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) down_write(&OCFS2_I(dir)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) drop_alloc_sem = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) dir_i_size = i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) dir_i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) /* dir->i_size is always block aligned. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) spin_lock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) spin_unlock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) parent_fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) num_free_extents = ocfs2_num_free_extents(&et);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) if (num_free_extents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) status = num_free_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) if (!num_free_extents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) status = ocfs2_reserve_clusters(osb, 1, &data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) if (ocfs2_dir_resv_allowed(osb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) credits = ocfs2_calc_extend_credits(sb, el);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) spin_unlock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) do_extend:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) if (ocfs2_dir_indexed(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) credits++; /* For attaching the new dirent block to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) * dx_root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) handle = ocfs2_start_trans(osb, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) data_ac, meta_ac, &new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) memset(new_bh->b_data, 0, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) de = (struct ocfs2_dir_entry *) new_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) de->inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) if (ocfs2_supports_dir_trailer(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) if (ocfs2_dir_indexed(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) status = ocfs2_dx_dir_link_trailer(dir, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) dx_root_bh, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) de->rec_len = cpu_to_le16(sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) ocfs2_update_inode_fsync_trans(handle, dir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) ocfs2_journal_dirty(handle, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) dir_i_size += dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) i_size_write(dir, dir_i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) dir->i_blocks = ocfs2_inode_sector_count(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) bail_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) *new_de_bh = new_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) get_bh(*new_de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) if (drop_alloc_sem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) up_write(&OCFS2_I(dir)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) if (data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) ocfs2_free_alloc_context(data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) if (meta_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) ocfs2_free_alloc_context(meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) struct buffer_head **ret_de_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) unsigned int *blocks_wanted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) struct ocfs2_dir_entry *de, *last_de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) char *de_buf, *limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) unsigned long offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) * This calculates how many free bytes we'd have in block zero, should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) * this function force expansion to an extent tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) if (ocfs2_new_dir_wants_trailer(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) free_space = dir->i_sb->s_blocksize - i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) de_buf = di->id2.i_data.id_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) limit = de_buf + i_size_read(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) rec_len = OCFS2_DIR_REC_LEN(namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) while (de_buf < limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) de = (struct ocfs2_dir_entry *)de_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) if (ocfs2_match(namelen, name, de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) * No need to check for a trailing dirent record here as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) * they're not used for inline dirs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) if (ocfs2_dirent_would_fit(de, rec_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) /* Ok, we found a spot. Return this bh and let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) * the caller actually fill it in. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) *ret_de_bh = di_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) get_bh(*ret_de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) last_de = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) de_buf += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) offset += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) * We're going to require expansion of the directory - figure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) * out how many blocks we'll need so that a place for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) * dirent can be found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) *blocks_wanted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) *blocks_wanted = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) int namelen, struct buffer_head **ret_de_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) unsigned short rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) struct ocfs2_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) int blocksize = dir->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) status = ocfs2_read_dir_block(dir, 0, &bh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) rec_len = OCFS2_DIR_REC_LEN(namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) de = (struct ocfs2_dir_entry *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) if ((char *)de >= sb->s_blocksize + bh->b_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) if (i_size_read(dir) <= offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) * Caller will have to expand this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) * directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) status = ocfs2_read_dir_block(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) offset >> sb->s_blocksize_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) &bh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) /* move to next block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) de = (struct ocfs2_dir_entry *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) status = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) if (ocfs2_match(namelen, name, de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) status = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) if (ocfs2_dirent_would_fit(de, rec_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) /* Ok, we found a spot. Return this bh and let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) * the caller actually fill it in. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) *ret_de_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) get_bh(*ret_de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) offset += le16_to_cpu(de->rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) static int dx_leaf_sort_cmp(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) const struct ocfs2_dx_entry *entry1 = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) const struct ocfs2_dx_entry *entry2 = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) if (major_hash1 > major_hash2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) if (major_hash1 < major_hash2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) * It is not strictly necessary to sort by minor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) if (minor_hash1 > minor_hash2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) if (minor_hash1 < minor_hash2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) static void dx_leaf_sort_swap(void *a, void *b, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) struct ocfs2_dx_entry *entry1 = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) struct ocfs2_dx_entry *entry2 = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) BUG_ON(size != sizeof(*entry1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) swap(*entry1, *entry2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) int i, num = le16_to_cpu(dl_list->de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) for (i = 0; i < (num - 1); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) * Find the optimal value to split this leaf on. This expects the leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) * entries to be in sorted order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) * the hash we want to insert.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) * This function is only concerned with the major hash - that which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) * determines which cluster an item belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) u32 leaf_cpos, u32 insert_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) u32 *split_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) int i, num_used = le16_to_cpu(dl_list->de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) int allsame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) * There's a couple rare, but nasty corner cases we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) * check for here. All of them involve a leaf where all value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) * have the same hash, which is what we look for first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) * Most of the time, all of the above is false, and we simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) * pick the median value for a split.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) allsame = ocfs2_dx_leaf_same_major(dx_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) if (allsame) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) if (val == insert_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) * No matter where we would choose to split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) * the new entry would want to occupy the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) * block as these. Since there's no space left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) * in their existing block, we know there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) * won't be space after the split.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) if (val == leaf_cpos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) * Because val is the same as leaf_cpos (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) * is the smallest value this leaf can have),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) * yet is not equal to insert_hash, then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) * know that insert_hash *must* be larger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) * val (and leaf_cpos). At least cpos+1 in value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) * We also know then, that there cannot be an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) * adjacent extent (otherwise we'd be looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) * at it). Choosing this value gives us a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) * chance to get some contiguousness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) *split_hash = leaf_cpos + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) if (val > insert_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) * val can not be the same as insert hash, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) * also must be larger than leaf_cpos. Also,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) * we know that there can't be a leaf between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) * cpos and val, otherwise the entries with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) * hash 'val' would be there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) *split_hash = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) *split_hash = insert_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) * Since the records are sorted and the checks above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) * guaranteed that not all records in this block are the same,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) * we simple travel forward, from the median, and pick the 1st
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) * record whose value is larger than leaf_cpos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) for (i = (num_used / 2); i < num_used; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) leaf_cpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) BUG_ON(i == num_used); /* Should be impossible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) * Transfer all entries in orig_dx_leaves whose major hash is equal to or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) * larger than split_hash into new_dx_leaves. We use a temporary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) * Since the block offset inside a leaf (cluster) is a constant mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) * of minor_hash, we can optimize - an item at block offset X within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) * the original cluster, will be at offset X within the new cluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) struct ocfs2_dx_leaf *tmp_dx_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) struct buffer_head **orig_dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) struct buffer_head **new_dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) int num_dx_leaves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) int i, j, num_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) u32 major_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) struct ocfs2_dx_entry_list *orig_list, *tmp_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) struct ocfs2_dx_entry *dx_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) tmp_list = &tmp_dx_leaf->dl_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) for (i = 0; i < num_dx_leaves; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) orig_list = &orig_dx_leaf->dl_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) num_used = le16_to_cpu(orig_list->de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) tmp_list->de_num_used = cpu_to_le16(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) for (j = 0; j < num_used; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) dx_entry = &orig_list->de_entries[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) major_hash = le32_to_cpu(dx_entry->dx_major_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) if (major_hash >= split_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) ocfs2_journal_dirty(handle, new_dx_leaves[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) struct ocfs2_dx_root_block *dx_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) int credits = ocfs2_clusters_to_blocks(osb->sb, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) credits += ocfs2_quota_trans_credits(osb->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) return credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) * Find the median value in dx_leaf_bh and allocate a new leaf to move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) * half our entries into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) struct buffer_head *dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) struct buffer_head *dx_leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) u64 leaf_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) int credits, ret, i, num_used, did_quota = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) u32 cpos, split_hash, insert_hash = hinfo->major_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) u64 orig_leaves_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) int num_dx_leaves;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) struct buffer_head **orig_dx_leaves = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) struct buffer_head **new_dx_leaves = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) struct ocfs2_extent_tree et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) (unsigned long long)leaf_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) insert_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) * XXX: This is a rather large limit. We should use a more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) * realistic value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) (unsigned long long)leaf_blkno, num_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) if (!orig_dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) if (!new_dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) if (ret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) handle = ocfs2_start_trans(osb, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) ret = dquot_alloc_space_nodirty(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) ocfs2_clusters_to_bytes(dir->i_sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) did_quota = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) * This block is changing anyway, so we can sort it in place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) sort(dx_leaf->dl_list.de_entries, num_used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) dx_leaf_sort_swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) ocfs2_journal_dirty(handle, dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) &split_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) * We have to carefully order operations here. There are items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) * which want to be in the new cluster before insert, but in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) * order to put those items in the new cluster, we alter the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) * old cluster. A failure to insert gets nasty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) * So, start by reserving writes to the old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) * the new cluster for us, before inserting it. The insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) * won't happen if there's an error before that. Once the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) * insert is done then, we can transfer from one leaf into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) * other without fear of hitting any error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) * The leaf transfer wants some scratch space so that we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) * wind up doing a bunch of expensive memmove().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) if (!tmp_dx_leaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) orig_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) cpos = split_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) data_ac, meta_ac, new_dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) num_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) for (i = 0; i < num_dx_leaves; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) orig_dx_leaves[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) new_dx_leaves[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) orig_dx_leaves, new_dx_leaves, num_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) if (ret < 0 && did_quota)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) dquot_free_space_nodirty(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) ocfs2_clusters_to_bytes(dir->i_sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) ocfs2_update_inode_fsync_trans(handle, dir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) if (orig_dx_leaves || new_dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) for (i = 0; i < num_dx_leaves; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) if (orig_dx_leaves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) brelse(orig_dx_leaves[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) if (new_dx_leaves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) brelse(new_dx_leaves[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) kfree(orig_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) kfree(new_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) if (meta_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) ocfs2_free_alloc_context(meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) if (data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) ocfs2_free_alloc_context(data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) kfree(tmp_dx_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) struct buffer_head *dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) int ret, rebalanced = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) struct buffer_head *dx_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) struct ocfs2_dx_leaf *dx_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) u32 leaf_cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) restart_search:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) &leaf_cpos, &blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) le16_to_cpu(dx_leaf->dl_list.de_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) if (rebalanced) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) * Rebalancing should have provided us with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) * space in an appropriate leaf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) * XXX: Is this an abnormal condition then?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) * Should we print a message here?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) &lookup->dl_hinfo, leaf_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) if (ret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) * Restart the lookup. The rebalance might have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) * changed which block our item fits into. Mark our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) * progress, so we only execute this once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) brelse(dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) dx_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) rebalanced = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) goto restart_search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) lookup->dl_dx_leaf_bh = dx_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) dx_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) brelse(dx_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) static int ocfs2_search_dx_free_list(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) struct buffer_head *dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) int ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) struct ocfs2_dir_block_trailer *db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) u64 next_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) int rec_len = OCFS2_DIR_REC_LEN(namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) next_block = le64_to_cpu(dx_root->dr_free_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) while (next_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) brelse(prev_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) prev_leaf_bh = leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) lookup->dl_leaf_bh = leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) lookup->dl_prev_leaf_bh = prev_leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) prev_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) next_block = le64_to_cpu(db->db_free_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) if (!next_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) brelse(leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) brelse(prev_leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) static int ocfs2_expand_inline_dx_root(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) struct buffer_head *dx_root_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) int ret, num_dx_leaves, i, j, did_quota = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) struct buffer_head **dx_leaves = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) struct ocfs2_extent_tree et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) u64 insert_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) struct ocfs2_alloc_context *data_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) struct ocfs2_dx_entry_list *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) struct ocfs2_dx_entry *dx_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) struct ocfs2_dx_leaf *target_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) if (!dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) ret = dquot_alloc_space_nodirty(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) ocfs2_clusters_to_bytes(osb->sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) did_quota = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) * We do this up front, before the allocation, so that a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) * failure to add the dx_root_bh to the journal won't result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) * us losing clusters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) num_dx_leaves, &insert_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) * Transfer the entries from our dx_root into the appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) * block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) entry_list = &dx_root->dr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) dx_entry = &entry_list->de_entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) j = __ocfs2_dx_dir_hash_idx(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) le32_to_cpu(dx_entry->dx_minor_hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) /* Each leaf has been passed to the journal already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) * via __ocfs2_dx_dir_new_cluster() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) offsetof(struct ocfs2_dx_root_block, dr_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) dx_root->dr_list.l_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) /* This should never fail considering we start with an empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) * dx_root. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) did_quota = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) ocfs2_update_inode_fsync_trans(handle, dir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) ocfs2_journal_dirty(handle, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) if (ret < 0 && did_quota)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) dquot_free_space_nodirty(dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) ocfs2_clusters_to_bytes(dir->i_sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) if (data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) ocfs2_free_alloc_context(data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) if (dx_leaves) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) for (i = 0; i < num_dx_leaves; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) brelse(dx_leaves[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) kfree(dx_leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) struct ocfs2_dx_entry_list *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) entry_list = &dx_root->dr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) if (le16_to_cpu(entry_list->de_num_used) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) le16_to_cpu(entry_list->de_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) int ret, free_dx_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) struct buffer_head *leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) if (ocfs2_dx_root_inline(dx_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) ret = ocfs2_inline_dx_has_space(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) goto search_el;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) * We ran out of room in the root block. Expand it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) * an extent, then allow ocfs2_find_dir_space_dx to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) * the rest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) * Insert preparation for an indexed directory is split into two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) * steps. The call to find_dir_space_dx reserves room in the index for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) * an additional item. If we run out of space there, it's a real error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) * we can't continue on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) namelen, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) search_el:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) * Next, we need to find space in the unindexed tree. This call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) * searches using the free space linked list. If the unindexed tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) * lacks sufficient space, we'll expand it below. The expansion code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) * is smart enough to add any new blocks to the free space list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) if (ret && ret != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) /* Do this up here - ocfs2_extend_dir might need the dx_root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) lookup->dl_dx_root_bh = dx_root_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) free_dx_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) * We make the assumption here that new leaf blocks are added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) * to the front of our free list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) lookup->dl_prev_leaf_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) lookup->dl_leaf_bh = leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) if (free_dx_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) * Get a directory ready for insert. Any directory allocation required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) * happens here. Success returns zero, and enough context in the dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) * lookup result that ocfs2_add_entry() will be able complete the task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) * with minimal performance impact.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) struct ocfs2_dir_lookup_result *lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) unsigned int blocks_wanted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) trace_ocfs2_prepare_dir_for_insert(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) if (!namelen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) * Do this up front to reduce confusion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) * The directory might start inline, then be turned into an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) * indexed one, in which case we'd need to hash deep inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) * ocfs2_find_dir_space_id(). Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) * ocfs2_prepare_dx_dir_for_insert() also needs this hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) * done, there seems no point in spreading out the calls. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) * can optimize away the case where the file system doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) * support indexing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) if (ocfs2_supports_indexed_dirs(osb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) if (ocfs2_dir_indexed(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) name, namelen, lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) namelen, &bh, &blocks_wanted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) if (ret && ret != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) * We have to expand the directory to add this name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) BUG_ON(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) lookup, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) if (ret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) BUG_ON(!bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) lookup->dl_leaf_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) static int ocfs2_dx_dir_remove_index(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) struct buffer_head *dx_root_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) struct inode *dx_alloc_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) struct buffer_head *dx_alloc_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) u64 blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) u16 bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) u64 bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) dx_alloc_inode = ocfs2_get_system_file_inode(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) EXTENT_ALLOC_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) le16_to_cpu(dx_root->dr_suballoc_slot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) if (!dx_alloc_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) inode_lock(dx_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) goto out_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) spin_lock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) spin_unlock(&OCFS2_I(dir)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) di->i_dx_root = cpu_to_le64(0ULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) ocfs2_update_inode_fsync_trans(handle, dir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) blk = le64_to_cpu(dx_root->dr_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) bit = le16_to_cpu(dx_root->dr_suballoc_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) if (dx_root->dr_suballoc_loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) bg_blkno = ocfs2_which_suballoc_group(blk, bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) bit, bg_blkno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) ocfs2_inode_unlock(dx_alloc_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) out_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) inode_unlock(dx_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) brelse(dx_alloc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) iput(dx_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) unsigned int clen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) u32 major_hash = UINT_MAX, p_cpos, cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) struct buffer_head *dx_root_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) struct ocfs2_dx_root_block *dx_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) struct ocfs2_cached_dealloc_ctxt dealloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) struct ocfs2_extent_tree et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) ocfs2_init_dealloc_ctxt(&dealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) if (!ocfs2_dir_indexed(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) if (ocfs2_dx_root_inline(dx_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) goto remove_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) /* XXX: What if dr_clusters is too large? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) while (le32_to_cpu(dx_root->dr_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) major_hash, &cpos, &blkno, &clen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) &dealloc, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) if (cpos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) major_hash = cpos - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) remove_index:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) ocfs2_schedule_truncate_log_flush(osb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) ocfs2_run_deallocs(osb, &dealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) brelse(dx_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) }