Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Implements Extendible Hashing as described in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *   "Extendible Hashing" by Fagin, et al in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *     __ACM Trans. on Database Systems__, Sept 1979.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * Here's the layout of dirents which is essentially the same as that of ext2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * within a single block. The field de_name_len is the number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * actually required for the name (no null terminator). The field de_rec_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * is the number of bytes allocated to the dirent. The offset of the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * deleted, the preceding dirent inherits its allocated space, ie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * by adding de_rec_len to the current dirent, this essentially causes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * deleted dirent to get jumped over when iterating through all the dirents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * When deleting the first dirent in a block, there is no previous dirent so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * the field de_ino is set to zero to designate it as deleted. When allocating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * dirent is allocated. Otherwise it must go through all the 'used' dirents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * searching for one in which the amount of total space minus the amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * used space will provide enough space for the new dirent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * There are two types of blocks in which dirents reside. In a stuffed dinode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * the block.  In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * beginning of the leaf block. The dirents reside in leaves when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * dip->i_diskflags & GFS2_DIF_EXHASH is true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * Otherwise, the dirents are "linear", within a single stuffed dinode block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * When the dirents are in leaves, the actual contents of the directory file are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * used as an array of 64-bit block pointers pointing to the leaf blocks. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * dirents are NOT in the directory file itself. There can be more than one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * block pointer in the array that points to the same leaf. In fact, when a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * directory is first converted from linear to exhash, all of the pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * point to the same leaf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * When a leaf is completely full, the size of the hash table can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * doubled unless it is already at the maximum size which is hard coded into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * but never before the maximum hash table size has been reached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #include <linux/gfs2_ondisk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #include "gfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #include "incore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #include "glock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #include "meta_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #include "quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #include "rgrp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #include "trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #include "bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define GFS2_HASH_INDEX_MASK 0xffffc000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define GFS2_USE_HASH_FLAG 0x2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) struct qstr gfs2_qdot __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) struct qstr gfs2_qdotdot __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 			    const struct qstr *name, void *opaque);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 			    struct buffer_head **bhp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	bh = gfs2_meta_new(ip->i_gl, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	gfs2_trans_add_meta(ip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	*bhp = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	return 0;
^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) static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 					struct buffer_head **bhp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	*bhp = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 				  unsigned int offset, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct buffer_head *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	error = gfs2_meta_inode_buffer(ip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	gfs2_trans_add_meta(ip->i_gl, dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (ip->i_inode.i_size < offset + size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		i_size_write(&ip->i_inode, offset + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	gfs2_dinode_out(ip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  * gfs2_dir_write_data - Write directory information to the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  * @ip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * @buf: The buffer containing information to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * @offset: The file offset to start writing at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  * @size: The amount of data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * Returns: The number of bytes correctly written or error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			       u64 offset, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	struct buffer_head *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	u64 lblock, dblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	u32 extlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	unsigned int o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	int new = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	if (gfs2_is_stuffed(ip) && offset + size <= gfs2_max_stuffed_size(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 					      size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	if (gfs2_is_stuffed(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		error = gfs2_unstuff_dinode(ip, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	lblock = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	while (copied < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		unsigned int amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		amount = size - copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		if (amount > sdp->sd_sb.sb_bsize - o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 			amount = sdp->sd_sb.sb_bsize - o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		if (!extlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			new = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 			error = gfs2_extent_map(&ip->i_inode, lblock, &new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 						&dblock, &extlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 			if (gfs2_assert_withdraw(sdp, dblock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		if (amount == sdp->sd_jbsize || new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		gfs2_trans_add_meta(ip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		memcpy(bh->b_data + o, buf, amount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		buf += amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		copied += amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		lblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		dblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		extlen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		o = sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	error = gfs2_meta_inode_buffer(ip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	if (ip->i_inode.i_size < offset + copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		i_size_write(&ip->i_inode, offset + copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	gfs2_trans_add_meta(ip->i_gl, dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	gfs2_dinode_out(ip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	if (copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 				 unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct buffer_head *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	error = gfs2_meta_inode_buffer(ip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	return (error) ? error : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  * gfs2_dir_read_data - Read a data from a directory inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * @ip: The GFS2 Inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  * @buf: The buffer to place result into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263)  * @size: Amount of data to transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * Returns: The amount of data actually copied or the error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			      unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	u64 lblock, dblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	u32 extlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	unsigned int o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (gfs2_is_stuffed(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		return gfs2_dir_read_stuffed(ip, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	lblock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	while (copied < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		unsigned int amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		int new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		amount = size - copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		if (amount > sdp->sd_sb.sb_bsize - o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			amount = sdp->sd_sb.sb_bsize - o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		if (!extlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			new = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			error = gfs2_extent_map(&ip->i_inode, lblock, &new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 						&dblock, &extlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 			if (error || !dblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			BUG_ON(extlen < 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		dblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		extlen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		memcpy(buf, bh->b_data + o, amount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		buf += (amount/sizeof(__be64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		copied += amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		lblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		o = sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	return (copied) ? copied : error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329)  * gfs2_dir_get_hash_table - Get pointer to the dir hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330)  * @ip: The inode in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * Returns: The hash table or an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	struct inode *inode = &ip->i_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	u32 hsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	__be64 *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	hc = ip->i_hash_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	hsize = BIT(ip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	hsize *= sizeof(__be64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	if (hsize != i_size_read(&ip->i_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		gfs2_consist_inode(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (hc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		hc = __vmalloc(hsize, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (hc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	ret = gfs2_dir_read_data(ip, hc, hsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		kvfree(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (likely(!ip->i_hash_cache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		ip->i_hash_cache = hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		hc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	kvfree(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	return ip->i_hash_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  * gfs2_dir_hash_inval - Invalidate dir hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  * @ip: The directory inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  * Must be called with an exclusive glock, or during glock invalidation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) void gfs2_dir_hash_inval(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	__be64 *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	spin_lock(&ip->i_inode.i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	hc = ip->i_hash_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	ip->i_hash_cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	spin_unlock(&ip->i_inode.i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	kvfree(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 				     const struct qstr *name, int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (!gfs2_dirent_sentinel(dent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	    be32_to_cpu(dent->de_hash) == name->hash &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	    be16_to_cpu(dent->de_name_len) == name->len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	    memcmp(dent+1, name->name, name->len) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) static int gfs2_dirent_find(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			    const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			    void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	return __gfs2_dirent_find(dent, name, 1);
^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 gfs2_dirent_prev(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			    const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 			    void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	return __gfs2_dirent_find(dent, name, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) }
^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)  * name->name holds ptr to start of block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * name->len holds size of block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) static int gfs2_dirent_last(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			    const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			    void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	const char *start = name->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (name->len == (end - start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) /* Look for the dirent that contains the offset specified in data. Once we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443)  * find that dirent, there must be space available there for the new dirent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) static int gfs2_dirent_find_offset(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 				  const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 				  void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	unsigned required = GFS2_DIRENT_SIZE(name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	unsigned totlen = be16_to_cpu(dent->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (ptr < (void *)dent || ptr >= (void *)dent + totlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (gfs2_dirent_sentinel(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (ptr < (void *)dent + actual)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	if ((void *)dent + totlen >= ptr + required)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 				  const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 				  void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	unsigned required = GFS2_DIRENT_SIZE(name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	unsigned totlen = be16_to_cpu(dent->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (gfs2_dirent_sentinel(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (totlen - actual >= required)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) struct dirent_gather {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	const struct gfs2_dirent **pdent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	unsigned offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			      const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			      void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	struct dirent_gather *g = opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	if (!gfs2_dirent_sentinel(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		g->pdent[g->offset++] = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  * Other possible things to check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * - Inode located within filesystem size (and on valid block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  * - Valid directory entry type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  * Not sure how heavy-weight we want to make this... could also check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499)  * hash is correct for example, but that would take a lot of extra time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  * For now the most important thing is to check that the various sizes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  * are correct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) static int gfs2_check_dirent(struct gfs2_sbd *sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			     struct gfs2_dirent *dent, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			     unsigned int size, unsigned int len, int first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	const char *msg = "gfs2_dirent too small";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (unlikely(size < sizeof(struct gfs2_dirent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	msg = "gfs2_dirent misaligned";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if (unlikely(offset & 0x7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	msg = "gfs2_dirent points beyond end of block";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (unlikely(offset + size > len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	msg = "zero inode number";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	if (unlikely(!first && gfs2_dirent_sentinel(dent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	msg = "name length is greater than space in dirent";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	if (!gfs2_dirent_sentinel(dent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	    unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		     size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	fs_warn(sdp, "%s: %s (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		__func__, msg, first ? "first in block" : "not first in block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	return -EIO;
^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) static int gfs2_dirent_offset(struct gfs2_sbd *sdp, const void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	const struct gfs2_meta_header *h = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	BUG_ON(buf == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	switch(be32_to_cpu(h->mh_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	case GFS2_METATYPE_LF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		offset = sizeof(struct gfs2_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	case GFS2_METATYPE_DI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		offset = sizeof(struct gfs2_dinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		goto wrong_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) wrong_type:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	fs_warn(sdp, "%s: wrong block type %u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		be32_to_cpu(h->mh_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 					    unsigned int len, gfs2_dscan_t scan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 					    const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 					    void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	struct gfs2_dirent *dent, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	unsigned offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	unsigned size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	ret = gfs2_dirent_offset(GFS2_SB(inode), buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		goto consist_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	offset = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	dent = buf + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	size = be16_to_cpu(dent->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size, len, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		goto consist_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		ret = scan(dent, name, opaque);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		offset += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		if (offset == len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		prev = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		dent = buf + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		size = be16_to_cpu(dent->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 				      len, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 			goto consist_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	} while(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	switch(ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		return dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		return prev ? prev : dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		BUG_ON(ret > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) consist_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	gfs2_consist_inode(GFS2_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) static int dirent_check_reclen(struct gfs2_inode *dip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			       const struct gfs2_dirent *d, const void *end_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	const void *ptr = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	u16 rec_len = be16_to_cpu(d->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		goto broken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	ptr += rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (ptr < end_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (ptr == end_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) broken:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	return -EIO;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626)  * dirent_next - Next dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627)  * @dip: the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628)  * @bh: The buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  * @dent: Pointer to list of dirents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * Returns: 0 on success, error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		       struct gfs2_dirent **dent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct gfs2_dirent *cur = *dent, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	char *bh_end = bh->b_data + bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	ret = dirent_check_reclen(dip, cur, bh_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	tmp = (void *)cur + ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	ret = dirent_check_reclen(dip, tmp, bh_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)         /* Only the first dent could ever have de_inum.no_addr == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	if (gfs2_dirent_sentinel(tmp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	*dent = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  * dirent_del - Delete a dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  * @bh: The buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * @prev: The previous dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * @cur: The current dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		       struct gfs2_dirent *prev, struct gfs2_dirent *cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	u16 cur_rec_len, prev_rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (gfs2_dirent_sentinel(cur)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	gfs2_trans_add_meta(dip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	/* If there is no prev entry, this is the first entry in the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	   The de_rec_len is already as big as it needs to be.  Just zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	   out the inode number and return.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (!prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		cur->de_inum.no_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		cur->de_inum.no_formal_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	/*  Combine this dentry with the previous one.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	prev_rec_len = be16_to_cpu(prev->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	cur_rec_len = be16_to_cpu(cur->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if ((char *)prev + prev_rec_len != (char *)cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	prev_rec_len += cur_rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	prev->de_rec_len = cpu_to_be16(prev_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) static struct gfs2_dirent *do_init_dirent(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 					  struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 					  const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 					  struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 					  unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct gfs2_dirent *ndent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	unsigned totlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	totlen = be16_to_cpu(dent->de_rec_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	BUG_ON(offset + name->len > totlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	gfs2_trans_add_meta(ip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	ndent = (struct gfs2_dirent *)((char *)dent + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	dent->de_rec_len = cpu_to_be16(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	gfs2_qstr2dirent(name, totlen - offset, ndent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	return ndent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727)  * Takes a dent from which to grab space as an argument. Returns the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728)  * newly created dent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 					    struct gfs2_dirent *dent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 					    const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 					    struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	unsigned offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (!gfs2_dirent_sentinel(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	return do_init_dirent(inode, dent, name, bh, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) static struct gfs2_dirent *gfs2_dirent_split_alloc(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 						   struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 						   const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 						   void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 				gfs2_dirent_find_offset, name, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (IS_ERR_OR_NULL(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		return dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	return do_init_dirent(inode, dent, name, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			      (unsigned)(ptr - (void *)dent));
^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) static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		    struct buffer_head **bhp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		/* pr_info("block num=%llu\n", leaf_no); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  * get_leaf_nr - Get a leaf number associated with the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)  * @index:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774)  * @leaf_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  * Returns: 0 on success, error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		       u64 *leaf_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	__be64 *hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	hash = gfs2_dir_get_hash_table(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	error = PTR_ERR_OR_ZERO(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		*leaf_out = be64_to_cpu(*(hash + index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static int get_first_leaf(struct gfs2_inode *dip, u32 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			  struct buffer_head **bh_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	u64 leaf_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	error = get_leaf_nr(dip, index, &leaf_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		error = get_leaf(dip, leaf_no, bh_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	return error;
^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) static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 					      const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 					      gfs2_dscan_t scan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 					      struct buffer_head **pbh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	if (ip->i_diskflags & GFS2_DIF_EXHASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		struct gfs2_leaf *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		unsigned int hsize = BIT(ip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		u64 ln;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		if (hsize * sizeof(u64) != i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			gfs2_consist_inode(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		index = name->hash >> (32 - ip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		error = get_first_leaf(ip, index, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 						scan, name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			if (dent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 				goto got_dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			ln = be64_to_cpu(leaf->lf_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			if (!ln)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			error = get_leaf(ip, ln, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		} while(!error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		return error ? ERR_PTR(error) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	error = gfs2_meta_inode_buffer(ip, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) got_dent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (IS_ERR_OR_NULL(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	*pbh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	return dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	unsigned int n = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	u64 bn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	struct gfs2_leaf *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	struct timespec64 tv = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	bh = gfs2_meta_new(ip->i_gl, bn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	gfs2_trans_remove_revoke(GFS2_SB(inode), bn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	gfs2_trans_add_meta(ip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	leaf->lf_depth = cpu_to_be16(depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	leaf->lf_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	leaf->lf_next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	leaf->lf_dist = cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	leaf->lf_sec = cpu_to_be64(tv.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	dent = (struct gfs2_dirent *)(leaf+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	gfs2_qstr2dirent(&empty_name, bh->b_size - sizeof(struct gfs2_leaf), dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	*pbh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  * dir_make_exhash - Convert a stuffed directory into an ExHash directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  * Returns: 0 on success, error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) static int dir_make_exhash(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct gfs2_inode *dip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	struct gfs2_sbd *sdp = GFS2_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct qstr args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	struct buffer_head *bh, *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	struct gfs2_leaf *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	int y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	u32 x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	__be64 *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	u64 bn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	error = gfs2_meta_inode_buffer(dip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	/*  Turn over a new leaf  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	leaf = new_leaf(inode, &bh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	if (!leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	bn = bh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	gfs2_assert(sdp, dip->i_entries < BIT(16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	leaf->lf_entries = cpu_to_be16(dip->i_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	/*  Copy dirents  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			     sizeof(struct gfs2_dinode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	/*  Find last entry  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	x = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	args.len = bh->b_size - sizeof(struct gfs2_dinode) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		   sizeof(struct gfs2_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	args.name = bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 				gfs2_dirent_last, &args, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	if (!dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		return PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	/*  Adjust the last dirent's record length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	   (Remember that dent still points to the last entry.)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		sizeof(struct gfs2_dinode) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		sizeof(struct gfs2_leaf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	/*  We're done with the new leaf block, now setup the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	    hash table.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	gfs2_trans_add_meta(dip->i_gl, dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	for (x = sdp->sd_hash_ptrs; x--; lp++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		*lp = cpu_to_be64(bn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	gfs2_add_inode_blocks(&dip->i_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	dip->i_diskflags |= GFS2_DIF_EXHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	dip->i_depth = y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	gfs2_dinode_out(dip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993)  * dir_split_leaf - Split a leaf block into two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  * @index:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  * @leaf_no:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * Returns: 0 on success, error code on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) static int dir_split_leaf(struct inode *inode, const struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	struct gfs2_inode *dip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	struct buffer_head *nbh, *obh, *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	struct gfs2_leaf *nleaf, *oleaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	u32 start, len, half_len, divider;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	u64 bn, leaf_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	__be64 *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	index = name->hash >> (32 - dip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	error = get_leaf_nr(dip, index, &leaf_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	/*  Get the old leaf block  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	error = get_leaf(dip, leaf_no, &obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	oleaf = (struct gfs2_leaf *)obh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		return 1; /* can't split */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	gfs2_trans_add_meta(dip->i_gl, obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	if (!nleaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	bn = nbh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	/*  Compute the start and len of leaf pointers in the hash table.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	len = BIT(dip->i_depth - be16_to_cpu(oleaf->lf_depth));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	half_len = len >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (!half_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		fs_warn(GFS2_SB(inode), "i_depth %u lf_depth %u index %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		goto fail_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	start = (index & ~(len - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	/* Change the pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	   Don't bother distinguishing stuffed from non-stuffed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	   This code is complicated enough already. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	lp = kmalloc_array(half_len, sizeof(__be64), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (!lp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		goto fail_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	/*  Change the pointers  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	for (x = 0; x < half_len; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		lp[x] = cpu_to_be64(bn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	gfs2_dir_hash_inval(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 				    half_len * sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	if (error != half_len * sizeof(u64)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		if (error >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		goto fail_lpfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	kfree(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	/*  Compute the divider  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	divider = (start + half_len) << (32 - dip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	/*  Copy the entries  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		next = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		if (dirent_next(dip, obh, &next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		if (!gfs2_dirent_sentinel(dent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		    be32_to_cpu(dent->de_hash) < divider) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			struct qstr str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			void *ptr = ((char *)dent - obh->b_data) + nbh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			str.name = (char*)(dent+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			str.len = be16_to_cpu(dent->de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			str.hash = be32_to_cpu(dent->de_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			if (IS_ERR(new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 				error = PTR_ERR(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			new->de_inum = dent->de_inum; /* No endian worries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			new->de_type = dent->de_type; /* No endian worries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			be16_add_cpu(&nleaf->lf_entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			dirent_del(dip, obh, prev, dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			if (!oleaf->lf_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 				gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			be16_add_cpu(&oleaf->lf_entries, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			if (!prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 				prev = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			prev = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		dent = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	} while (dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	oleaf->lf_depth = nleaf->lf_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	error = gfs2_meta_inode_buffer(dip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		gfs2_trans_add_meta(dip->i_gl, dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		gfs2_add_inode_blocks(&dip->i_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		gfs2_dinode_out(dip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	brelse(nbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) fail_lpfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	kfree(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) fail_brelse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	brelse(nbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)  * dir_double_exhash - Double size of ExHash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)  * @dip: The GFS2 dinode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)  * Returns: 0 on success, error code on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) static int dir_double_exhash(struct gfs2_inode *dip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	struct buffer_head *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	u32 hsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	u32 hsize_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	__be64 *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	__be64 *hc2, *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	hsize = BIT(dip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	hsize_bytes = hsize * sizeof(__be64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	hc = gfs2_dir_get_hash_table(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (IS_ERR(hc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		return PTR_ERR(hc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	hc2 = kmalloc_array(hsize_bytes, 2, GFP_NOFS | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (hc2 == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	if (!hc2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	h = hc2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	error = gfs2_meta_inode_buffer(dip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	for (x = 0; x < hsize; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		*h++ = *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		*h++ = *hc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		hc++;
^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) 	error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	if (error != (hsize_bytes * 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	gfs2_dir_hash_inval(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	dip->i_hash_cache = hc2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	dip->i_depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	gfs2_dinode_out(dip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/* Replace original hash table & size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	i_size_write(&dip->i_inode, hsize_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	gfs2_dinode_out(dip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) out_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	kvfree(hc2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  * compare_dents - compare directory entries by hash value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  * @a: first dent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  * @b: second dent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  * When comparing the hash entries of @a to @b:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  *   gt: returns 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  *   lt: returns -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)  *   eq: returns 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) static int compare_dents(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	const struct gfs2_dirent *dent_a, *dent_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	u32 hash_a, hash_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	dent_a = *(const struct gfs2_dirent **)a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	hash_a = dent_a->de_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	dent_b = *(const struct gfs2_dirent **)b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	hash_b = dent_b->de_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	if (hash_a > hash_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	else if (hash_a < hash_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		if (len_a > len_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		else if (len_a < len_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			ret = memcmp(dent_a + 1, dent_b + 1, len_a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	return ret;
^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)  * do_filldir_main - read out directory entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  * @ctx: what to feed the entries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  * @darr: an array of struct gfs2_dirent pointers to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)  * @entries: the number of entries in darr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)  * @copied: pointer to int that's non-zero if a entry has been copied out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)  * Jump through some hoops to make sure that if there are hash collsions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)  * they are read out at the beginning of a buffer.  We want to minimize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)  * the possibility that they will fall into different readdir buffers or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)  * that someone will want to seek to that location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)  * Returns: errno, >0 if the actor tells you to stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			   struct gfs2_dirent **darr, u32 entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			   u32 sort_start, int *copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	const struct gfs2_dirent *dent, *dent_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	u64 off, off_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	unsigned int x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	int run = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (sort_start < entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		sort(&darr[sort_start], entries - sort_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		     sizeof(struct gfs2_dirent *), compare_dents, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	dent_next = darr[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	off_next = dent_next->de_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	for (x = 0, y = 1; x < entries; x++, y++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		dent = dent_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		off = off_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		if (y < entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			dent_next = darr[y];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			off_next = dent_next->de_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			if (off < ctx->pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			ctx->pos = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			if (off_next == off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 				if (*copied && !run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 					return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 				run = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 				run = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			if (off < ctx->pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			ctx->pos = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		if (!dir_emit(ctx, (const char *)(dent + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 				be16_to_cpu(dent->de_name_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 				be64_to_cpu(dent->de_inum.no_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 				be16_to_cpu(dent->de_type)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		*copied = 1;
^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) 	/* Increment the ctx->pos by one, so the next time we come into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	   do_filldir fxn, we get the next entry instead of the last one in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	   current leaf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static void *gfs2_alloc_sort_buffer(unsigned size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	void *ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (size < KMALLOC_MAX_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		ptr = __vmalloc(size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			    unsigned leaf_nr, struct gfs2_dirent **darr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			    unsigned entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	int sort_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	for (i = 0; i < entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		unsigned offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		if (!sdp->sd_args.ar_loccookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		offset = (char *)(darr[i]) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			(bh->b_data + gfs2_dirent_offset(sdp, bh->b_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		offset /= GFS2_MIN_DIRENT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		offset += leaf_nr * sdp->sd_max_dents_per_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		if (offset >= GFS2_USE_HASH_FLAG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		    leaf_nr >= GFS2_USE_HASH_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			darr[i]->de_cookie |= GFS2_USE_HASH_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			if (sort_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 				sort_id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		darr[i]->de_cookie |= offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	return sort_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) }	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			      int *copied, unsigned *depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			      u64 leaf_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	struct gfs2_sbd *sdp = GFS2_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	struct gfs2_leaf *lf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	unsigned entries = 0, entries2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	unsigned leaves = 0, leaf = 0, offset, sort_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	struct gfs2_dirent **darr, *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	struct dirent_gather g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	struct buffer_head **larr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	int error, i, need_sort = 0, sort_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	u64 lfn = leaf_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		error = get_leaf(ip, lfn, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		lf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		if (leaves == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			*depth = be16_to_cpu(lf->lf_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		entries += be16_to_cpu(lf->lf_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		leaves++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		lfn = be64_to_cpu(lf->lf_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	} while(lfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		need_sort = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		sort_offset = 0;
^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) 	if (!entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	 * The extra 99 entries are not normally used, but are a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	 * zone in case the number of entries in the leaf is corrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 * 99 is the maximum number of entries that can fit in a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * leaf block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	if (!larr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	darr = (struct gfs2_dirent **)(larr + leaves);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	g.pdent = (const struct gfs2_dirent **)darr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	g.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	lfn = leaf_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		error = get_leaf(ip, lfn, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		lf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		lfn = be64_to_cpu(lf->lf_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		if (lf->lf_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			offset = g.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			entries2 += be16_to_cpu(lf->lf_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 						gfs2_dirent_gather, NULL, &g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			error = PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 				goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			if (entries2 != g.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 				fs_warn(sdp, "Number of entries corrupt in dir "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 						"leaf %llu, entries2 (%u) != "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 						"g.offset (%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 					(unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 					entries2, g.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 				gfs2_consist_inode(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 				error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 				goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 						   be16_to_cpu(lf->lf_entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 			if (!need_sort && sort_id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 				need_sort = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 				sort_offset = offset + sort_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			larr[leaf++] = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			larr[leaf++] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	} while(lfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	BUG_ON(entries2 != entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	error = do_filldir_main(ip, ctx, darr, entries, need_sort ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 				sort_offset : entries, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	for(i = 0; i < leaf; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		brelse(larr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	kvfree(larr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  * Note: we can't calculate each index like dir_e_read can because we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  * have the leaf, and therefore we don't have the depth, and therefore we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)  * don't have the length. So we have to just read enough ahead to make up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  * for the loss of information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 			       struct file_ra_state *f_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	struct gfs2_glock *gl = ip->i_gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	u64 blocknr = 0, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	unsigned count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	/* First check if we've already read-ahead for the whole range. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (index + MAX_RA_BLOCKS < f_ra->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	f_ra->start = max((pgoff_t)index, f_ra->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	for (count = 0; count < MAX_RA_BLOCKS; count++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		if (f_ra->start >= hsize) /* if exceeded the hash table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		last = blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		f_ra->start++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		if (blocknr == last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		bh = gfs2_getbuf(gl, blocknr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		if (trylock_buffer(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 			if (buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 				unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 			bh->b_end_io = end_buffer_read_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 			submit_bh(REQ_OP_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 				  REQ_RAHEAD | REQ_META | REQ_PRIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 				  bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)  * dir_e_read - Reads the entries from a directory into a filldir buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)  * @dip: dinode pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)  * @ctx: actor to feed the entries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) static int dir_e_read(struct inode *inode, struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		      struct file_ra_state *f_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	struct gfs2_inode *dip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	u32 hsize, len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	u32 hash, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	__be64 *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	unsigned depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	hsize = BIT(dip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	hash = gfs2_dir_offset2hash(ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	index = hash >> (32 - dip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (dip->i_hash_cache == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		f_ra->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	lp = gfs2_dir_get_hash_table(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	if (IS_ERR(lp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		return PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	gfs2_dir_readahead(inode, hsize, index, f_ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	while (index < hsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		error = gfs2_dir_read_leaf(inode, ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 					   &copied, &depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 					   be64_to_cpu(lp[index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		len = BIT(dip->i_depth - depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		index = (index & ~(len - 1)) + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (error > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		  struct file_ra_state *f_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	struct gfs2_inode *dip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	struct gfs2_sbd *sdp = GFS2_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	struct dirent_gather g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	struct gfs2_dirent **darr, *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	struct buffer_head *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	if (!dip->i_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	if (dip->i_diskflags & GFS2_DIF_EXHASH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		return dir_e_read(inode, ctx, f_ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	if (!gfs2_is_stuffed(dip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	error = gfs2_meta_inode_buffer(dip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	/* 96 is max number of dirents which can be stuffed into an inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	darr = kmalloc_array(96, sizeof(struct gfs2_dirent *), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	if (darr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		g.pdent = (const struct gfs2_dirent **)darr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		g.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 					gfs2_dirent_gather, NULL, &g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			error = PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		if (dip->i_entries != g.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 			fs_warn(sdp, "Number of entries corrupt in dir %llu, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				"ip->i_entries (%u) != g.offset (%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 				(unsigned long long)dip->i_no_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 				dip->i_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 				g.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		error = do_filldir_main(dip, ctx, darr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 					dip->i_entries, 0, &copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		kfree(darr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (error > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)  * gfs2_dir_search - Search a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)  * @dip: The GFS2 dir inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)  * @name: The name we are looking up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)  * @fail_on_exist: Fail if the name exists rather than looking it up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)  * This routine searches a directory for a file or another directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)  * Assumes a glock is held on dip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			      bool fail_on_exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	u64 addr, formal_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	u16 dtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		u16 rahead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			return ERR_CAST(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		dtype = be16_to_cpu(dent->de_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		rahead = be16_to_cpu(dent->de_rahead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		addr = be64_to_cpu(dent->de_inum.no_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		if (fail_on_exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			return ERR_PTR(-EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 					  GFS2_BLKST_FREE /* ignore */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		if (!IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			GFS2_I(inode)->i_rahead = rahead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) int gfs2_dir_check(struct inode *dir, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		   const struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	if (dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			return PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		if (ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 			if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 			    ip->i_no_formal_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 			if (unlikely(IF2DT(ip->i_inode.i_mode) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 			    be16_to_cpu(dent->de_type))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 				gfs2_consist_inode(GFS2_I(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 				ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703)  * dir_new_leaf - Add a new leaf onto hash chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)  * @inode: The directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)  * @name: The name we are adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)  * This adds a new dir leaf onto an existing leaf when there is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)  * enough space to add a new dir entry. This is a last resort after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)  * we've expanded the hash table to max size and also split existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)  * leaf blocks, so it will only occur for very large directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)  * The dist parameter is set to 1 for leaf blocks directly attached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)  * to the hash table, 2 for one layer of indirection, 3 for two layers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)  * etc. We are thus able to tell the difference between an old leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)  * with dist set to zero (i.e. "don't know") and a new one where we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)  * set this information for debug/fsck purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)  * Returns: 0 on success, or -ve on error
^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) static int dir_new_leaf(struct inode *inode, const struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	struct buffer_head *bh, *obh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	struct gfs2_leaf *leaf, *oleaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	u32 dist = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	u64 bn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	index = name->hash >> (32 - ip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	error = get_first_leaf(ip, index, &obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		dist++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		oleaf = (struct gfs2_leaf *)obh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		bn = be64_to_cpu(oleaf->lf_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		if (!bn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		error = get_leaf(ip, bn, &obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	} while(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	gfs2_trans_add_meta(ip->i_gl, obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	if (!leaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	leaf->lf_dist = cpu_to_be32(dist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	brelse(obh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	error = gfs2_meta_inode_buffer(ip, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	gfs2_trans_add_meta(ip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	gfs2_add_inode_blocks(&ip->i_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	gfs2_dinode_out(ip, bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	u64 where = ip->i_no_addr + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	if (ip->i_eattr == where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)  * gfs2_dir_add - Add new filename into directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)  * @inode: The directory inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780)  * @name: The new name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)  * @nip: The GFS2 inode to be linked in to the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)  * @da: The directory addition info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)  * If the call to gfs2_diradd_alloc_required resulted in there being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)  * no need to allocate any new directory blocks, then it will contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786)  * a pointer to the directory entry and the bh in which it resides. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787)  * can use that without having to repeat the search. If there was no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788)  * free space, then we must now create more space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790)  * Returns: 0 on success, error code on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) int gfs2_dir_add(struct inode *inode, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		 const struct gfs2_inode *nip, struct gfs2_diradd *da)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	struct buffer_head *bh = da->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	struct gfs2_dirent *dent = da->dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	struct timespec64 tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	struct gfs2_leaf *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	while(1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		if (da->bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 			dent = gfs2_dirent_search(inode, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 						  gfs2_dirent_find_space, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		if (dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 				return PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			dent = gfs2_init_dirent(inode, dent, name, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 			gfs2_inum_out(nip, dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 			dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			tv = current_time(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			if (ip->i_diskflags & GFS2_DIF_EXHASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 				leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 				be16_add_cpu(&leaf->lf_entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 				leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 				leaf->lf_sec = cpu_to_be64(tv.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 			da->dent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 			da->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 			ip->i_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 			ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 			if (S_ISDIR(nip->i_inode.i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 				inc_nlink(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 			mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 			error = dir_make_exhash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		error = dir_split_leaf(inode, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		if (error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			error = dir_double_exhash(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 			error = dir_split_leaf(inode, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 			if (error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		error = dir_new_leaf(inode, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		error = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)  * gfs2_dir_del - Delete a directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)  * @filename: The filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)  * Returns: 0 on success, error code on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	const struct qstr *name = &dentry->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	struct gfs2_dirent *dent, *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	struct timespec64 tv = current_time(&dip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	/* Returns _either_ the entry (if its first in block) or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	   previous entry otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	if (!dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	if (IS_ERR(dent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		return PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	/* If not first in block, adjust pointers accordingly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	if (gfs2_dirent_find(dent, name, NULL) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		prev = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	dirent_del(dip, bh, prev, dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	if (dip->i_diskflags & GFS2_DIF_EXHASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		u16 entries = be16_to_cpu(leaf->lf_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		if (!entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		leaf->lf_entries = cpu_to_be16(--entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		leaf->lf_sec = cpu_to_be64(tv.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	if (!dip->i_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	dip->i_entries--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	if (d_is_dir(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		drop_nlink(&dip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	mark_inode_dirty(&dip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)  * gfs2_dir_mvino - Change inode number of directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  * @dip: The GFS2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)  * @filename:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)  * @new_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)  * This routine changes the inode number of a directory entry.  It's used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)  * by rename to change ".." when a directory is moved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927)  * Assumes a glock is held on dvp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		   const struct gfs2_inode *nip, unsigned int new_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	if (!dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		return PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	gfs2_trans_add_meta(dip->i_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	gfs2_inum_out(nip, dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	dent->de_type = cpu_to_be16(new_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	dip->i_inode.i_mtime = dip->i_inode.i_ctime = current_time(&dip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	mark_inode_dirty_sync(&dip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957)  * leaf_dealloc - Deallocate a directory leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958)  * @dip: the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959)  * @index: the hash table offset in the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)  * @len: the number of pointers to this leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961)  * @leaf_no: the leaf number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)  * @leaf_bh: buffer_head for the starting leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963)  * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 			u64 leaf_no, struct buffer_head *leaf_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 			int last_dealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	struct gfs2_leaf *tmp_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	struct gfs2_rgrp_list rlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	struct buffer_head *bh, *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	u64 blk, nblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	unsigned int rg_blocks = 0, l_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	char *ht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	unsigned int x, size = len * sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	error = gfs2_rindex_update(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	if (ht == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	if (!ht)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	/*  Count the number of leaves  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	bh = leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	for (blk = leaf_no; blk; blk = nblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		if (blk != leaf_no) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 			error = get_leaf(dip, blk, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 				goto out_rlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		tmp_leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		nblk = be64_to_cpu(tmp_leaf->lf_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		if (blk != leaf_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		gfs2_rlist_add(dip, &rlist, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		l_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	gfs2_rlist_alloc(&rlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	for (x = 0; x < rlist.rl_rgrps; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		rg_blocks += rgd->rd_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		goto out_rlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	error = gfs2_trans_begin(sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 			RES_DINODE + RES_STATFS + RES_QUOTA, RES_DINODE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 				 l_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		goto out_rg_gunlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	bh = leaf_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	for (blk = leaf_no; blk; blk = nblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		if (blk != leaf_no) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			error = get_leaf(dip, blk, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 				goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		tmp_leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		nblk = be64_to_cpu(tmp_leaf->lf_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		if (blk != leaf_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		rgd = gfs2_blk2rgrpd(sdp, blk, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		gfs2_free_meta(dip, rgd, blk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		gfs2_add_inode_blocks(&dip->i_inode, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	if (error != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		if (error >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 		goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	error = gfs2_meta_inode_buffer(dip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	gfs2_trans_add_meta(dip->i_gl, dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	/* On the last dealloc, make this a regular file in case we crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	   (We don't want to free these blocks a second time.)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	if (last_dealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 		dip->i_inode.i_mode = S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	gfs2_dinode_out(dip, dibh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) out_end_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	gfs2_trans_end(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) out_rg_gunlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) out_rlist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	gfs2_rlist_free(&rlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	gfs2_quota_unhold(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	kvfree(ht);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  * @dip: the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)  * Dealloc all on-disk directory leaves to FREEMETA state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)  * Change on-disk inode type to "regular file"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	struct gfs2_leaf *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	u32 hsize, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	u32 index = 0, next_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	__be64 *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	u64 leaf_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	int error = 0, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	hsize = BIT(dip->i_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	lp = gfs2_dir_get_hash_table(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	if (IS_ERR(lp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		return PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	while (index < hsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		leaf_no = be64_to_cpu(lp[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		if (leaf_no) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			error = get_leaf(dip, leaf_no, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 			leaf = (struct gfs2_leaf *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 			len = BIT(dip->i_depth - be16_to_cpu(leaf->lf_depth));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 			next_index = (index & ~(len - 1)) + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 			last = ((next_index >= hsize) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 			error = leaf_dealloc(dip, index, len, leaf_no, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 					     last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 			index = next_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 			index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	if (index != hsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		gfs2_consist_inode(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)  * gfs2_diradd_alloc_required - find if adding entry will require an allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)  * @ip: the file being written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)  * @filname: the filename that's going to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)  * @da: The structure to return dir alloc info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)  * Returns: 0 if ok, -ve on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 			       struct gfs2_diradd *da)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	struct gfs2_sbd *sdp = GFS2_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	struct gfs2_dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	da->nr_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	da->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	da->dent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	if (!dent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 		da->nr_blocks = sdp->sd_max_dirres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		    (GFS2_DIRENT_SIZE(name->len) < extra))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 			da->nr_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		return PTR_ERR(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	if (da->save_loc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		da->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 		da->dent = dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)