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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Trivial Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Rights granted to Hans Reiser to redistribute under other terms providing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * he accepts all liability including but not limited to patent, fitness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * for purpose, and direct or indirect claims arising from failure to perform.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * NO WARRANTY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "reiserfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) set_nlink(i, 1); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * directory item contains array of entry headers. This performs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * binary search through that array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	struct item_head *ih = de->de_ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	struct reiserfs_de_head *deh = de->de_deh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	int rbound, lbound, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	lbound = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	rbound = ih_entry_count(ih) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	for (j = (rbound + lbound) / 2; lbound <= rbound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	     j = (rbound + lbound) / 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 		if (off < deh_offset(deh + j)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 			rbound = j - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 		if (off > deh_offset(deh + j)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 			lbound = j + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 		/* this is not name found, but matched third key component */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 		de->de_entry_num = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 		return NAME_FOUND;
^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) 	de->de_entry_num = lbound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	return NAME_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * comment?  maybe something like set de to point to what the path points to?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static inline void set_de_item_location(struct reiserfs_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 					struct treepath *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	de->de_bh = get_last_bh(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	de->de_ih = tp_item_head(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	de->de_item_num = PATH_LAST_POSITION(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	if (de->de_name[de->de_namelen - 1] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		de->de_namelen = strlen(de->de_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) /* what entry points to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) static inline void set_de_object_key(struct reiserfs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	/* store key of the found entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	de->de_entry_key.version = KEY_FORMAT_3_5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	de->de_entry_key.on_disk_key.k_dir_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	    le32_to_cpu(de->de_ih->ih_key.k_dir_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	de->de_entry_key.on_disk_key.k_objectid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	    le32_to_cpu(de->de_ih->ih_key.k_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * We assign a key to each directory item, and place multiple entries in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * single directory item.  A directory item has a key equal to the key of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  * the first directory entry in it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * This function first calls search_by_key, then, if item whose first entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * matches is not found it looks for the entry inside directory item found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * by search_by_key. Fills the path to the entry, and to the entry position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * in the item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) /* The function is NOT SCHEDULE-SAFE! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			struct treepath *path, struct reiserfs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	retval = search_item(sb, key, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	switch (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	case ITEM_NOT_FOUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		if (!PATH_LAST_POSITION(path)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 			reiserfs_error(sb, "vs-7000", "search_by_key "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 				       "returned item position == 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 			pathrelse(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 			return IO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		PATH_LAST_POSITION(path)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	case ITEM_FOUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	case IO_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		pathrelse(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		reiserfs_error(sb, "vs-7002", "no path to here");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		return IO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	set_de_item_location(de, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	if (!is_direntry_le_ih(de->de_ih) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	    COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		print_block(de->de_bh, 0, -1, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 			       "item or does not belong to the same directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			       "as key %K", de->de_ih, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) #endif				/* CONFIG_REISERFS_CHECK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	 * binary search in directory item by third component of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	 * key. sets de->de_entry_num of de
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	path->pos_in_item = de->de_entry_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	if (retval != NAME_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		 * ugly, but rename needs de_bh, de_deh, de_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		 * de_namelen, de_objectid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		set_de_name_and_namelen(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		set_de_object_key(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
^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)  * The third component is hashed, and you can choose from more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * one hash function.  Per directory hashes are not yet implemented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  * but are thought about. This function should be moved to hashes.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  * Jedi, please do so.  -Hans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static __u32 get_third_component(struct super_block *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 				 const char *name, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	__u32 res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	if (!len || (len == 1 && name[0] == '.'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return DOT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (len == 2 && name[0] == '.' && name[1] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		return DOT_DOT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	res = REISERFS_SB(s)->s_hash_function(name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	/* take bits from 7-th to 30-th including both bounds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	res = GET_HASH_VALUE(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		 * needed to have no names before "." and ".." those have hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		 * value == 0 and generation conters 1 and 2 accordingly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		res = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	return res + MAX_GENERATION_NUMBER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static int reiserfs_match(struct reiserfs_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			  const char *name, int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	int retval = NAME_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	if ((namelen == de->de_namelen) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	    !memcmp(de->de_name, name, de->de_namelen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		    (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		     NAME_FOUND_INVISIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) /* used when hash collisions exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) static int linear_search_in_dir_item(struct cpu_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 				     struct reiserfs_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 				     const char *name, int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	struct reiserfs_de_head *deh = de->de_deh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	i = de->de_entry_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	if (i == ih_entry_count(de->de_ih) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	    GET_HASH_VALUE(deh_offset(deh + i)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	    GET_HASH_VALUE(cpu_key_k_offset(key))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	       "vs-7010: array of entry headers not found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	deh += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	for (; i >= 0; i--, deh--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		/* hash value does not match, no need to check whole name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		if (GET_HASH_VALUE(deh_offset(deh)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		    GET_HASH_VALUE(cpu_key_k_offset(key))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			return NAME_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		/* mark that this generation number is used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		if (de->de_gen_number_bit_string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 			set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 				de->de_gen_number_bit_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		/* calculate pointer to name and namelen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		de->de_entry_num = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		set_de_name_and_namelen(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		 * de's de_name, de_namelen, de_recordlen are set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		 * Fill the rest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		if ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		     reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 			/* key of pointed object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 			set_de_object_key(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			store_de_entry_key(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			/* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		 * we have reached left most entry in the node. In common we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		 * have to go to the left neighbor, but if generation counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		 * is 0 already, we know for sure, that there is no name with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		 * the same hash value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		 * FIXME: this work correctly only because hash value can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		 *  be 0. Btw, in case of Yura's hash it is probably possible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		 * so, this is a bug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		return NAME_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	RFALSE(de->de_item_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	       "vs-7015: two diritems of the same directory in one node?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	return GOTO_PREVIOUS_ITEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301)  * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302)  * FIXME: should add something like IOERROR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			       struct treepath *path_to_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 			       struct reiserfs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct cpu_key key_to_search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		return NAME_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	/* we will search for this key in the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	make_cpu_key(&key_to_search, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		     get_third_component(dir->i_sb, name, namelen),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		     TYPE_DIRENTRY, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		    search_by_entry_key(dir->i_sb, &key_to_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 					path_to_entry, de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			reiserfs_error(dir->i_sb, "zam-7001", "io error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			return IO_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) 		/* compare names for all entries having given hash value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		    linear_search_in_dir_item(&key_to_search, de, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 					      namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		 * there is no need to scan directory anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		 * Given entry found or does not exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		if (retval != GOTO_PREVIOUS_ITEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 			path_to_entry->pos_in_item = de->de_entry_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		 * there is left neighboring item of this directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		 * and given entry can be there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		set_cpu_key_k_offset(&key_to_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 				     le_ih_k_offset(de->de_ih) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		pathrelse(path_to_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}			/* while (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 				      unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	struct reiserfs_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	INITIALIZE_PATH(path_to_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	    reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 				&path_to_entry, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	pathrelse(&path_to_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	if (retval == NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		inode = reiserfs_iget(dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 				      (struct cpu_key *)&de.de_dir_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		if (!inode || IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 			reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		}
^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) 		 * Propagate the private flag so we know we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		 * in the priv tree.  Also clear IOP_XATTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		 * since we don't have xattrs on xattr files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		if (IS_PRIVATE(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 			inode->i_flags |= S_PRIVATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			inode->i_opflags &= ~IOP_XATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) }
^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)  * looks up the dentry of the parent directory for child.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * taken from ext2_get_parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) struct dentry *reiserfs_get_parent(struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct reiserfs_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	INITIALIZE_PATH(path_to_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	struct inode *dir = d_inode(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (dir->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	pathrelse(&path_to_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (retval != NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	return d_obtain_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) /* add entry to the directory (entry can be hidden).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) insert definition of when hidden directories are used here -Hans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  Does not mark dir   inode dirty, do it after successesfull call to it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			      struct inode *dir, const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			      struct inode *inode, int visible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	struct cpu_key entry_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	struct reiserfs_de_head *deh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	INITIALIZE_PATH(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	struct reiserfs_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	int gen_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	 * 48 bytes now and we avoid kmalloc if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	 * create file with short name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	char small_buf[32 + DEH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	int buflen, paste_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	/* cannot allow items to be added into a busy deleted directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (!namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	/* each entry has unique key. compose it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	make_cpu_key(&entry_key, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		     get_third_component(dir->i_sb, name, namelen),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		     TYPE_DIRENTRY, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	/* get memory for composing the entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	buflen = DEH_SIZE + ROUND_UP(namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (buflen > sizeof(small_buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		buffer = kmalloc(buflen, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		buffer = small_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	paste_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	    (get_inode_sd_version(dir) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	     STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	 * fill buffer : directory entry head, name[, dir objectid | ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	 * stat data | ,stat data, dir objectid ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	deh = (struct reiserfs_de_head *)buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	deh->deh_location = 0;	/* JDM Endian safe if 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	put_deh_offset(deh, cpu_key_k_offset(&entry_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	deh->deh_state = 0;	/* JDM Endian safe if 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	/* put key (ino analog) to de */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	/* safe: k_dir_id is le */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	/* safe: k_objectid is le */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	/* copy name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	memcpy((char *)(deh + 1), name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	/* padd by 0s to the 4 byte boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	 * entry is ready to be pasted into tree, set 'visibility'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	 * and 'stat data in entry' attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	mark_de_without_sd(deh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	visible ? mark_de_visible(deh) : mark_de_hidden(deh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	/* find the proper place for the new entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	memset(bit_string, 0, sizeof(bit_string));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	de.de_gen_number_bit_string = bit_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if (retval != NAME_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		if (buffer != small_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		pathrelse(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		if (retval != NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			reiserfs_error(dir->i_sb, "zam-7002",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 				       "reiserfs_find_entry() returned "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 				       "unexpected value (%d)", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	gen_number =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	    find_first_zero_bit(bit_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 				MAX_GENERATION_NUMBER + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (gen_number > MAX_GENERATION_NUMBER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		/* there is no free generation number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		reiserfs_warning(dir->i_sb, "reiserfs-7010",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 				 "Congratulations! we have got hash function "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 				 "screwed up");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		if (buffer != small_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 			kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		pathrelse(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	/* adjust offset of directory enrty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	set_cpu_key_k_offset(&entry_key, deh_offset(deh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	/* update max-hash-collisions counter in reiserfs_sb_info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	/* we need to re-search for the insertion point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (gen_number != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		    NAME_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			reiserfs_warning(dir->i_sb, "vs-7032",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 					 "entry with this key (%K) already "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 					 "exists", &entry_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 			if (buffer != small_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 				kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			pathrelse(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	/* perform the insertion of the entry that we have prepared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	    reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 				     paste_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	if (buffer != small_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		reiserfs_check_path(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	dir->i_size += paste_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (!S_ISDIR(inode->i_mode) && visible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		/* reiserfs_mkdir or reiserfs_rename will do that by itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		reiserfs_update_sd(th, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	reiserfs_check_path(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586)  * quota utility function, call if you've had to abort after calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587)  * new_inode_init, and have not called reiserfs_new_inode yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  * This should only be called on inodes that do not have stat data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  * inserted into the tree yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static int drop_new_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	dquot_drop(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	inode->i_flags |= S_NOQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * utility function that does setup for reiserfs_new_inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * dquot_initialize needs lots of credits so it's better to have it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * outside of a transaction, so we had to pull some bits of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  * reiserfs_new_inode out into this func.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	 * Make inode invalid - just in case we are going to drop it before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	 * the initialization happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	INODE_PKEY(inode)->k_objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	 * the quota init calls have to know who to charge the quota to, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	 * we have to set uid and gid here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	inode_init_owner(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	return dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			   bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 * We need blocks for transaction + (user+group)*(quotas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	 * for new inode + update of quota for directory owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	int jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	    JOURNAL_PER_BALANCE_CNT * 2 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	struct reiserfs_security_handle security;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (!(inode = new_inode(dir->i_sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	retval = new_inode_init(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	jbegin_count += reiserfs_cache_default_acl(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	jbegin_count += retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			       inode, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	inode->i_op = &reiserfs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	inode->i_fop = &reiserfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			       dentry->d_name.len, inode, 1 /*visible */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			retval = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	reiserfs_update_inode_transaction(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) out_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			  dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	struct reiserfs_security_handle security;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	 * We need blocks for transaction + (user+group)*(quotas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	 * for new inode + update of quota for directory owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	int jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	    JOURNAL_PER_BALANCE_CNT * 3 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (!(inode = new_inode(dir->i_sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	retval = new_inode_init(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	jbegin_count += reiserfs_cache_default_acl(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	jbegin_count += retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			       inode, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	inode->i_op = &reiserfs_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	init_special_inode(inode, inode->i_mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	/* FIXME: needed for block and char devices only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	reiserfs_update_inode_transaction(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			       dentry->d_name.len, inode, 1 /*visible */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			retval = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) out_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct reiserfs_security_handle security;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	 * We need blocks for transaction + (user+group)*(quotas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	 * for new inode + update of quota for directory owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	int jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	    JOURNAL_PER_BALANCE_CNT * 3 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) #ifdef DISPLACE_NEW_PACKING_LOCALITIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	 * set flag that new packing locality created and new blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	 * for the content of that directory are not displaced yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	REISERFS_I(dir)->new_packing_locality = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	mode = S_IFDIR | mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (!(inode = new_inode(dir->i_sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	retval = new_inode_init(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	jbegin_count += reiserfs_cache_default_acl(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	jbegin_count += retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	 * inc the link count now, so another writer doesn't overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	 * it while we sleep later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	INC_DIR_INODE_NLINK(dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 				    old_format_only(dir->i_sb) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 				    EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 				    dentry, inode, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		DEC_DIR_INODE_NLINK(dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	reiserfs_update_inode_transaction(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	inode->i_op = &reiserfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	inode->i_fop = &reiserfs_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	/* note, _this_ add_entry will not update dir's stat data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			       dentry->d_name.len, inode, 1 /*visible */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		DEC_DIR_INODE_NLINK(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			retval = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	/* the above add_entry did not update dir's stat data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	reiserfs_update_sd(&th, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) out_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static inline int reiserfs_empty_dir(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	 * we can cheat because an old format dir cannot have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	 * EMPTY_DIR_SIZE, and a new format dir cannot have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	 * EMPTY_DIR_SIZE_V1.  So, if the inode is either size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	 * regardless of disk format version, the directory is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (inode->i_size != EMPTY_DIR_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	    inode->i_size != EMPTY_DIR_SIZE_V1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	int retval, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	int jbegin_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	INITIALIZE_PATH(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	struct reiserfs_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	 * we will be doing 2 balancings and update 2 stat data, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	 * change quotas of the owner of the directory and of the owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	 * of the parent directory.  The quota structure is possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	 * deleted only on last iput => outside of this transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		goto out_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 				 &path, &de)) == NAME_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	} else if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	reiserfs_update_inode_transaction(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	if (de.de_objectid != inode->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		 * FIXME: compare key of an object and a key found in the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	if (!reiserfs_empty_dir(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		retval = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	/* cut entry from dir directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 					dir, NULL,	/* page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 					0 /*new file size - not used here */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		goto end_rmdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (inode->i_nlink != 2 && inode->i_nlink != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		reiserfs_error(inode->i_sb, "reiserfs-7040",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			       "empty directory has nlink != 2 (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			       inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	DEC_DIR_INODE_NLINK(dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	dir->i_size -= (DEH_SIZE + de.de_entrylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	reiserfs_update_sd(&th, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	/* prevent empty directory from getting lost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	add_save_link(&th, inode, 0 /* not truncate */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	reiserfs_check_path(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) out_rmdir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) end_rmdir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	 * we must release path, because we did not call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * release path if operation was not complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	pathrelse(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	return err ? err : retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	int retval, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct reiserfs_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	INITIALIZE_PATH(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	int jbegin_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	unsigned long savelink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	 * in this transaction we can be doing at max two balancings and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	 * update two stat datas, we change quotas of the owner of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	 * directory and of the owner of the parent directory. The quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	 * structure is possibly deleted only on iput => outside of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	 * this transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		goto out_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	if ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 				 &path, &de)) == NAME_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		goto end_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	} else if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		goto end_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	reiserfs_update_inode_transaction(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	if (de.de_objectid != inode->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		 * FIXME: compare key of an object and a key found in the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		goto end_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	if (!inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		reiserfs_warning(inode->i_sb, "reiserfs-7042",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 				 "deleting nonexistent file (%lu), %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				 inode->i_ino, inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	 * we schedule before doing the add_save_link call, save the link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	 * count so we don't race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	savelink = inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	    reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 				   0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		goto end_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	dir->i_size -= (de.de_entrylen + DEH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	dir->i_ctime = dir->i_mtime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	reiserfs_update_sd(&th, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	if (!savelink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		/* prevent file from getting lost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		add_save_link(&th, inode, 0 /* not truncate */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	reiserfs_check_path(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) end_unlink:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	pathrelse(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	reiserfs_check_path(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		retval = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) out_unlink:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static int reiserfs_symlink(struct inode *parent_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			    struct dentry *dentry, const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	int item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	struct reiserfs_security_handle security;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	int mode = S_IFLNK | S_IRWXUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	 * We need blocks for transaction + (user+group)*(quotas for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	 * new inode + update of quota for directory owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	int jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	    JOURNAL_PER_BALANCE_CNT * 3 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	retval = dquot_initialize(parent_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	if (!(inode = new_inode(parent_dir->i_sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	retval = new_inode_init(inode, parent_dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		return retval;
^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) 	retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 					&security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	jbegin_count += retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	reiserfs_write_lock(parent_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	item_len = ROUND_UP(strlen(symname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		retval = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	name = kmalloc(item_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	memcpy(name, symname, strlen(symname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	padd_item(name, item_len, strlen(symname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		drop_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	    reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			       dentry, inode, &security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (retval) {		/* reiserfs_new_inode iputs for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	reiserfs_update_inode_transaction(parent_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	inode->i_op = &reiserfs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 				    dentry->d_name.len, inode, 1 /*visible */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			retval = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) out_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	reiserfs_write_unlock(parent_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			 struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	struct inode *inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	 * We need blocks for transaction + update of quotas for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	 * the owners of the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	int jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	    JOURNAL_PER_BALANCE_CNT * 3 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	    2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	retval = dquot_initialize(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	reiserfs_write_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	if (inode->i_nlink >= REISERFS_LINK_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		/* FIXME: sd_nlink is 32 bit for new files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		return -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	/* inc before scheduling so reiserfs_unlink knows we are here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	/* create new entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			       dentry->d_name.len, inode, 1 /*visible */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	reiserfs_update_inode_transaction(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		return err ? err : retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	reiserfs_update_sd(&th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	reiserfs_write_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) /* de contains information pointing to an entry which */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) static int de_still_valid(const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			  struct reiserfs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	struct reiserfs_dir_entry tmp = *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	/* recalculate pointer to name and name length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	set_de_name_and_namelen(&tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	/* FIXME: could check more */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) static int entry_points_to_object(const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 				  struct reiserfs_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 				  struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	if (!de_still_valid(name, len, de))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		if (!de_visible(de->de_deh + de->de_entry_num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 			reiserfs_panic(inode->i_sb, "vs-7042",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 				       "entry must be visible");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		return (de->de_objectid == inode->i_ino) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	/* this must be added hidden entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	if (de_visible(de->de_deh + de->de_entry_num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		reiserfs_panic(NULL, "vs-7043", "entry must be visible");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) /* sets key of objectid the entry has to point to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 				 struct reiserfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	/* JDM These operations are endian safe - both are le */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)  * process, that is going to call fix_nodes/do_balance must hold only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)  * one path. If it holds 2 or more, it can get into endless waiting in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)  * get_empty_nodes or its clones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			   struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			   unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	INITIALIZE_PATH(old_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	INITIALIZE_PATH(new_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	INITIALIZE_PATH(dot_dot_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	struct inode *old_inode, *new_dentry_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	struct reiserfs_transaction_handle th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	int jbegin_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	umode_t old_inode_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	unsigned long savelink = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	struct timespec64 ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	if (flags & ~RENAME_NOREPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	 * three balancings: (1) old name removal, (2) new name insertion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	 * and (3) maybe "save" link insertion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	 * stat data updates: (1) old directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	 * (2) new directory and (3) maybe old object stat data (when it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	 * directory) and (4) maybe stat data of object to which new entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	 * pointed initially and (5) maybe block containing ".." of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 * renamed directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 * quota updates: two parent directories
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	jbegin_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	    JOURNAL_PER_BALANCE_CNT * 3 + 5 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	retval = dquot_initialize(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	retval = dquot_initialize(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	old_inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	new_dentry_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	 * make sure that oldname still exists and points to an object we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	 * are going to rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	old_de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	reiserfs_write_lock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	    reiserfs_find_entry(old_dir, old_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 				old_dentry->d_name.len, &old_entry_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 				&old_de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	pathrelse(&old_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		return -EIO;
^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) 	if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	old_inode_mode = old_inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	if (S_ISDIR(old_inode_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		 * make sure that directory being renamed has correct ".."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		 * and that its new parent directory has not too many links
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		 * already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		if (new_dentry_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			if (!reiserfs_empty_dir(new_dentry_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 				reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 				return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		 * directory is renamed, its parent directory will be changed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		 * so find ".." entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		dot_dot_de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		    reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 					&dot_dot_de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		pathrelse(&dot_dot_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		if (retval != NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		/* inode number of .. must equal old_dir->i_ino */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		if (dot_dot_de.de_objectid != old_dir->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	/* add new entry (or find the existing one) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	    reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			       new_dentry->d_name.len, old_inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	if (retval == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		if (!new_dentry_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 			reiserfs_panic(old_dir->i_sb, "vs-7050",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 				       "new entry is found, new inode == 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	} else if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		int err = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		return err ? err : retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	reiserfs_update_inode_transaction(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	reiserfs_update_inode_transaction(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	 * this makes it so an fsync on an open fd for the old name will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	 * commit the rename operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	reiserfs_update_inode_transaction(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	if (new_dentry_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		reiserfs_update_inode_transaction(new_dentry_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		 * look for old name using corresponding entry key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		 * (found by reiserfs_find_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		if ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		     search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 					 &old_entry_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 					 &old_de)) != NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 			pathrelse(&old_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 			journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		/* look for new name by reiserfs_find_entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		new_de.de_gen_number_bit_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		    reiserfs_find_entry(new_dir, new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 					new_dentry->d_name.len, &new_entry_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 					&new_de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		 * reiserfs_add_entry should not return IO_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		 * because it is called with essentially same parameters from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		 * reiserfs_add_entry above, and we'll catch any i/o errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		 * before we get here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			pathrelse(&new_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 			pathrelse(&old_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 			journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 			reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		if (S_ISDIR(old_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 			if ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 			     search_by_entry_key(new_dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 						 &dot_dot_de.de_entry_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 						 &dot_dot_entry_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 						 &dot_dot_de)) != NAME_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 				pathrelse(&dot_dot_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 				pathrelse(&new_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 				pathrelse(&old_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 				journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 				reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 			copy_item_head(&dot_dot_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 				       tp_item_head(&dot_dot_entry_path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 			/* node containing ".." gets into transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			reiserfs_prepare_for_journal(old_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 						     dot_dot_de.de_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		 * we should check seals here, not do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		 * this stuff, yes? Then, having
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		 * gathered everything into RAM we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		 * should lock the buffers, yes?  -Hans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		 * probably.  our rename needs to hold more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		 * than one path at once.  The seals would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		 * have to be written to deal with multi-path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		 * issues -chris
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		 * sanity checking before doing the rename - avoid races many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		 * of the above checks could have scheduled.  We have to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		 * sure our items haven't been shifted by another process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		if (item_moved(&new_entry_ih, &new_entry_path) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		    !entry_points_to_object(new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 					    new_dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 					    &new_de, new_dentry_inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		    item_moved(&old_entry_ih, &old_entry_path) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		    !entry_points_to_object(old_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 					    old_dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 					    &old_de, old_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 			reiserfs_restore_prepared_buffer(old_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 							 new_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			reiserfs_restore_prepared_buffer(old_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 							 old_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			if (S_ISDIR(old_inode_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 				reiserfs_restore_prepared_buffer(old_inode->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 								 i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 								 dot_dot_de.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 								 de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		if (S_ISDIR(old_inode_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			    !entry_points_to_object("..", 2, &dot_dot_de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 						    old_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 				reiserfs_restore_prepared_buffer(old_inode->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 								 i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 								 old_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 				reiserfs_restore_prepared_buffer(old_inode->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 								 i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 								 new_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				reiserfs_restore_prepared_buffer(old_inode->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 								 i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 								 dot_dot_de.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 								 de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		RFALSE(S_ISDIR(old_inode_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		       !buffer_journal_prepared(dot_dot_de.de_bh), "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	 * ok, all the changes can be done in one fell swoop when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	 * have claimed all the buffers needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	mark_de_visible(new_de.de_deh + new_de.de_entry_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	journal_mark_dirty(&th, new_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	journal_mark_dirty(&th, old_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	ctime = current_time(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	old_dir->i_ctime = old_dir->i_mtime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	new_dir->i_ctime = new_dir->i_mtime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	 * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	 * which adds ctime update of renamed object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	old_inode->i_ctime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	if (new_dentry_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		/* adjust link number of the victim */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		if (S_ISDIR(new_dentry_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			clear_nlink(new_dentry_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			drop_nlink(new_dentry_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		new_dentry_inode->i_ctime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		savelink = new_dentry_inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	if (S_ISDIR(old_inode_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		/* adjust ".." of renamed directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		journal_mark_dirty(&th, dot_dot_de.de_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		 * there (in new_dir) was no directory, so it got new link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		 * (".."  of renamed directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		if (!new_dentry_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 			INC_DIR_INODE_NLINK(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		/* old directory lost one link - ".. " of renamed directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		DEC_DIR_INODE_NLINK(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	 * looks like in 2.3.99pre3 brelse is atomic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	 * so we can use pathrelse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	pathrelse(&new_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	pathrelse(&dot_dot_entry_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	 * FIXME: this reiserfs_cut_from_item's return value may screw up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	 * anybody, but it will panic if will not be able to find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	 * entry. This needs one more clean up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (reiserfs_cut_from_item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	    (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	     0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		reiserfs_error(old_dir->i_sb, "vs-7060",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			       "couldn't not cut old name. Fsck later?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	reiserfs_update_sd(&th, old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	reiserfs_update_sd(&th, new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	reiserfs_update_sd(&th, old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (new_dentry_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		if (savelink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			add_save_link(&th, new_dentry_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 				      0 /* not truncate */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		reiserfs_update_sd(&th, new_dentry_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	retval = journal_end(&th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	reiserfs_write_unlock(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) /* directories can handle most operations...  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) const struct inode_operations reiserfs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	.create = reiserfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	.lookup = reiserfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	.link = reiserfs_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	.unlink = reiserfs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	.symlink = reiserfs_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	.mkdir = reiserfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	.rmdir = reiserfs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	.mknod = reiserfs_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	.rename = reiserfs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	.setattr = reiserfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	.listxattr = reiserfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	.permission = reiserfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	.get_acl = reiserfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	.set_acl = reiserfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  * symlink operations.. same as page_symlink_inode_operations, with xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  * stuff added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) const struct inode_operations reiserfs_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	.get_link	= page_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	.setattr = reiserfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	.listxattr = reiserfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	.permission = reiserfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)  * special file operations.. just xattr/acl stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) const struct inode_operations reiserfs_special_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	.setattr = reiserfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	.listxattr = reiserfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	.permission = reiserfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	.get_acl = reiserfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	.set_acl = reiserfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) };