blob: 3d7a35d6a18bcbc7543070d59988963714ddffb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 *
4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
5 *
6 * Trivial Changes:
7 * Rights granted to Hans Reiser to redistribute under other terms providing
8 * he accepts all liability including but not limited to patent, fitness
9 * for purpose, and direct or indirect claims arising from failure to perform.
10 *
11 * NO WARRANTY
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/time.h>
15#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Al Virof466c6f2012-03-17 01:16:43 -040017#include "reiserfs.h"
Al Viroa3063ab2012-03-17 01:03:10 -040018#include "acl.h"
Al Viroc45ac882012-03-17 00:59:06 -040019#include "xattr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/quotaops.h>
21
Miklos Szeredibfe86842011-10-28 14:13:29 +020022#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); }
Dave Hansen9a53c3a2006-09-30 23:29:03 -070023#define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jeff Mahoney098297b2014-04-23 10:00:36 -040025/*
26 * directory item contains array of entry headers. This performs
27 * binary search through that array
28 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070029static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070031 struct item_head *ih = de->de_ih;
32 struct reiserfs_de_head *deh = de->de_deh;
33 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070035 lbound = 0;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040036 rbound = ih_entry_count(ih) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 for (j = (rbound + lbound) / 2; lbound <= rbound;
39 j = (rbound + lbound) / 2) {
40 if (off < deh_offset(deh + j)) {
41 rbound = j - 1;
42 continue;
43 }
44 if (off > deh_offset(deh + j)) {
45 lbound = j + 1;
46 continue;
47 }
Jeff Mahoney098297b2014-04-23 10:00:36 -040048 /* this is not name found, but matched third key component */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070049 de->de_entry_num = j;
50 return NAME_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 de->de_entry_num = lbound;
54 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Jeff Mahoney098297b2014-04-23 10:00:36 -040057/*
58 * comment? maybe something like set de to point to what the path points to?
59 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060static inline void set_de_item_location(struct reiserfs_dir_entry *de,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -080061 struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 de->de_bh = get_last_bh(path);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040064 de->de_ih = tp_item_head(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
66 de->de_item_num = PATH_LAST_POSITION(path);
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Mahoney098297b2014-04-23 10:00:36 -040069/*
70 * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
71 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070074 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Sesterhenn14a61442006-10-03 23:36:38 +020076 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070078 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
79 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040080 de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 if (de->de_name[de->de_namelen - 1] == 0)
82 de->de_namelen = strlen(de->de_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Jeff Mahoney098297b2014-04-23 10:00:36 -040085/* what entry points to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086static inline void set_de_object_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Sesterhenn14a61442006-10-03 23:36:38 +020088 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Jeff Mahoneya228bf82014-04-23 10:00:42 -040089 de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
90 de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric Sesterhenn14a61442006-10-03 23:36:38 +020097 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099 /* store key of the found entry */
100 de->de_entry_key.version = KEY_FORMAT_3_5;
101 de->de_entry_key.on_disk_key.k_dir_id =
102 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
103 de->de_entry_key.on_disk_key.k_objectid =
104 le32_to_cpu(de->de_ih->ih_key.k_objectid);
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400105 set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
106 set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Jeff Mahoney098297b2014-04-23 10:00:36 -0400109/*
110 * We assign a key to each directory item, and place multiple entries in a
111 * single directory item. A directory item has a key equal to the key of
112 * the first directory entry in it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jeff Mahoney098297b2014-04-23 10:00:36 -0400114 * This function first calls search_by_key, then, if item whose first entry
115 * matches is not found it looks for the entry inside directory item found
116 * by search_by_key. Fills the path to the entry, and to the entry position
117 * in the item
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800121 struct treepath *path, struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 retval = search_item(sb, key, path);
126 switch (retval) {
127 case ITEM_NOT_FOUND:
128 if (!PATH_LAST_POSITION(path)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400129 reiserfs_error(sb, "vs-7000", "search_by_key "
130 "returned item position == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131 pathrelse(path);
132 return IO_ERROR;
133 }
134 PATH_LAST_POSITION(path)--;
Gustavo A. R. Silvac3754da2020-11-20 12:32:14 -0600135 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136
137 case ITEM_FOUND:
138 break;
139
140 case IO_ERROR:
141 return retval;
142
143 default:
144 pathrelse(path);
Jeff Mahoney0030b642009-03-30 14:02:28 -0400145 reiserfs_error(sb, "vs-7002", "no path to here");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700149 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152 if (!is_direntry_le_ih(de->de_ih) ||
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400153 COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 print_block(de->de_bh, 0, -1, -1);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400155 reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
156 "item or does not belong to the same directory "
157 "as key %K", de->de_ih, key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158 }
159#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jeff Mahoney098297b2014-04-23 10:00:36 -0400161 /*
162 * binary search in directory item by third component of the
163 * key. sets de->de_entry_num of de
164 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700165 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
166 path->pos_in_item = de->de_entry_num;
167 if (retval != NAME_NOT_FOUND) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400168 /*
169 * ugly, but rename needs de_bh, de_deh, de_name,
170 * de_namelen, de_objectid set
171 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700172 set_de_name_and_namelen(de);
173 set_de_object_key(de);
174 }
175 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
179
Jeff Mahoney098297b2014-04-23 10:00:36 -0400180/*
181 * The third component is hashed, and you can choose from more than
182 * one hash function. Per directory hashes are not yet implemented
183 * but are thought about. This function should be moved to hashes.c
184 * Jedi, please do so. -Hans
185 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186static __u32 get_third_component(struct super_block *s,
187 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 if (!len || (len == 1 && name[0] == '.'))
192 return DOT_OFFSET;
193 if (len == 2 && name[0] == '.' && name[1] == '.')
194 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jeff Mahoney098297b2014-04-23 10:00:36 -0400198 /* take bits from 7-th to 30-th including both bounds */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 res = GET_HASH_VALUE(res);
200 if (res == 0)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400201 /*
202 * needed to have no names before "." and ".." those have hash
203 * value == 0 and generation conters 1 and 2 accordingly
204 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 res = 128;
206 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700209static int reiserfs_match(struct reiserfs_dir_entry *de,
210 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 if ((namelen == de->de_namelen) &&
215 !memcmp(de->de_name, name, de->de_namelen))
216 retval =
217 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
218 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
224
Jeff Mahoney098297b2014-04-23 10:00:36 -0400225/* used when hash collisions exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227static int linear_search_in_dir_item(struct cpu_key *key,
228 struct reiserfs_dir_entry *de,
229 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 struct reiserfs_de_head *deh = de->de_deh;
232 int retval;
233 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400237 if (i == ih_entry_count(de->de_ih) ||
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700238 GET_HASH_VALUE(deh_offset(deh + i)) !=
239 GET_HASH_VALUE(cpu_key_k_offset(key))) {
240 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700243 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
244 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 for (; i >= 0; i--, deh--) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400249 /* hash value does not match, no need to check whole name */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700250 if (GET_HASH_VALUE(deh_offset(deh)) !=
251 GET_HASH_VALUE(cpu_key_k_offset(key))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 return NAME_NOT_FOUND;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jeff Mahoney098297b2014-04-23 10:00:36 -0400255 /* mark that this generation number is used */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700256 if (de->de_gen_number_bit_string)
257 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500258 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Jeff Mahoney098297b2014-04-23 10:00:36 -0400260 /* calculate pointer to name and namelen */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 de->de_entry_num = i;
262 set_de_name_and_namelen(de);
263
Jeff Mahoney098297b2014-04-23 10:00:36 -0400264 /*
265 * de's de_name, de_namelen, de_recordlen are set.
266 * Fill the rest.
267 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700268 if ((retval =
269 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270
Jeff Mahoney098297b2014-04-23 10:00:36 -0400271 /* key of pointed object */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700272 set_de_object_key(de);
273
274 store_de_entry_key(de);
275
Jeff Mahoney098297b2014-04-23 10:00:36 -0400276 /* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 return retval;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400282 /*
283 * we have reached left most entry in the node. In common we
284 * have to go to the left neighbor, but if generation counter
285 * is 0 already, we know for sure, that there is no name with
286 * the same hash value
287 */
288 /*
289 * FIXME: this work correctly only because hash value can not
290 * be 0. Btw, in case of Yura's hash it is probably possible,
291 * so, this is a bug
292 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700293 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 RFALSE(de->de_item_num,
296 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Jeff Mahoney098297b2014-04-23 10:00:36 -0400301/*
302 * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
303 * FIXME: should add something like IOERROR
304 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800306 struct treepath *path_to_entry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700307 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700309 struct cpu_key key_to_search;
310 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700312 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
313 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700315 /* we will search for this key in the tree */
316 make_cpu_key(&key_to_search, dir,
317 get_third_component(dir->i_sb, name, namelen),
318 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320 while (1) {
321 retval =
322 search_by_entry_key(dir->i_sb, &key_to_search,
323 path_to_entry, de);
324 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400325 reiserfs_error(dir->i_sb, "zam-7001", "io error");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700326 return IO_ERROR;
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700329 /* compare names for all entries having given hash value */
330 retval =
331 linear_search_in_dir_item(&key_to_search, de, name,
332 namelen);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400333 /*
334 * there is no need to scan directory anymore.
335 * Given entry found or does not exist
336 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700337 if (retval != GOTO_PREVIOUS_ITEM) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700338 path_to_entry->pos_in_item = de->de_entry_num;
339 return retval;
340 }
341
Jeff Mahoney098297b2014-04-23 10:00:36 -0400342 /*
343 * there is left neighboring item of this directory
344 * and given entry can be there
345 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700346 set_cpu_key_k_offset(&key_to_search,
347 le_ih_k_offset(de->de_ih) - 1);
348 pathrelse(path_to_entry);
349
350 } /* while (1) */
351}
352
353static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400354 unsigned int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355{
356 int retval;
357 struct inode *inode = NULL;
358 struct reiserfs_dir_entry de;
359 INITIALIZE_PATH(path_to_entry);
360
361 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
362 return ERR_PTR(-ENAMETOOLONG);
363
Jeff Mahoney278f6672013-08-08 17:34:46 -0400364 reiserfs_write_lock(dir->i_sb);
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200365
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700366 de.de_gen_number_bit_string = NULL;
367 retval =
368 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
369 &path_to_entry, &de);
370 pathrelse(&path_to_entry);
371 if (retval == NAME_FOUND) {
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400372 inode = reiserfs_iget(dir->i_sb,
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400373 (struct cpu_key *)&de.de_dir_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 if (!inode || IS_ERR(inode)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -0400375 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700376 return ERR_PTR(-EACCES);
377 }
378
Jeff Mahoney098297b2014-04-23 10:00:36 -0400379 /*
380 * Propagate the private flag so we know we're
Jeff Mahoney60e4cf62019-10-24 10:31:27 -0400381 * in the priv tree. Also clear IOP_XATTR
382 * since we don't have xattrs on xattr files.
Jeff Mahoney098297b2014-04-23 10:00:36 -0400383 */
Jeff Mahoney60e4cf62019-10-24 10:31:27 -0400384 if (IS_PRIVATE(dir)) {
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400385 inode->i_flags |= S_PRIVATE;
Jeff Mahoney60e4cf62019-10-24 10:31:27 -0400386 inode->i_opflags &= ~IOP_XATTR;
387 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400389 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Pekka Enbergd9975d62006-01-14 13:21:09 -0800394 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Jeff Mahoney0222e652009-03-30 14:02:44 -0400397/*
Jeff Mahoney098297b2014-04-23 10:00:36 -0400398 * looks up the dentry of the parent directory for child.
399 * taken from ext2_get_parent
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401struct dentry *reiserfs_get_parent(struct dentry *child)
402{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700403 int retval;
404 struct inode *inode = NULL;
405 struct reiserfs_dir_entry de;
406 INITIALIZE_PATH(path_to_entry);
David Howells2b0143b2015-03-17 22:25:59 +0000407 struct inode *dir = d_inode(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 if (dir->i_nlink == 0) {
410 return ERR_PTR(-ENOENT);
411 }
412 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700414 reiserfs_write_lock(dir->i_sb);
415 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
416 pathrelse(&path_to_entry);
417 if (retval != NAME_FOUND) {
418 reiserfs_write_unlock(dir->i_sb);
419 return ERR_PTR(-ENOENT);
420 }
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400421 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Hellwig44003722008-08-11 15:49:04 +0200424 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Jeff Mahoney0222e652009-03-30 14:02:44 -0400427/* add entry to the directory (entry can be hidden).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429insert definition of when hidden directories are used here -Hans
430
431 Does not mark dir inode dirty, do it after successesfull call to it */
432
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
434 struct inode *dir, const char *name, int namelen,
435 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 struct cpu_key entry_key;
438 struct reiserfs_de_head *deh;
439 INITIALIZE_PATH(path);
440 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500441 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700442 int gen_number;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400443
444 /*
445 * 48 bytes now and we avoid kmalloc if we
446 * create file with short name
447 */
448 char small_buf[32 + DEH_SIZE];
449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 char *buffer;
451 int buflen, paste_size;
452 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700454 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 /* cannot allow items to be added into a busy deleted directory */
457 if (!namelen)
458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
461 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700463 /* each entry has unique key. compose it */
464 make_cpu_key(&entry_key, dir,
465 get_third_component(dir->i_sb, name, namelen),
466 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 /* get memory for composing the entry */
469 buflen = DEH_SIZE + ROUND_UP(namelen);
470 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800471 buffer = kmalloc(buflen, GFP_NOFS);
Al Viro9dce07f2008-03-29 03:07:28 +0000472 if (!buffer)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 return -ENOMEM;
474 } else
475 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 paste_size =
478 (get_inode_sd_version(dir) ==
479 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jeff Mahoney098297b2014-04-23 10:00:36 -0400481 /*
482 * fill buffer : directory entry head, name[, dir objectid | ,
483 * stat data | ,stat data, dir objectid ]
484 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 deh = (struct reiserfs_de_head *)buffer;
486 deh->deh_location = 0; /* JDM Endian safe if 0 */
487 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
488 deh->deh_state = 0; /* JDM Endian safe if 0 */
489 /* put key (ino analog) to de */
Jeff Mahoney098297b2014-04-23 10:00:36 -0400490
491 /* safe: k_dir_id is le */
492 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
493 /* safe: k_objectid is le */
494 deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700496 /* copy name */
497 memcpy((char *)(deh + 1), name, namelen);
498 /* padd by 0s to the 4 byte boundary */
499 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jeff Mahoney098297b2014-04-23 10:00:36 -0400501 /*
502 * entry is ready to be pasted into tree, set 'visibility'
503 * and 'stat data in entry' attributes
504 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 mark_de_without_sd(deh);
506 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 /* find the proper place for the new entry */
509 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500510 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
512 if (retval != NAME_NOT_FOUND) {
513 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800514 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700515 pathrelse(&path);
516
517 if (retval == IO_ERROR) {
518 return -EIO;
519 }
520
521 if (retval != NAME_FOUND) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400522 reiserfs_error(dir->i_sb, "zam-7002",
523 "reiserfs_find_entry() returned "
524 "unexpected value (%d)", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700525 }
526
527 return -EEXIST;
528 }
529
530 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500531 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532 MAX_GENERATION_NUMBER + 1);
533 if (gen_number > MAX_GENERATION_NUMBER) {
534 /* there is no free generation number */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400535 reiserfs_warning(dir->i_sb, "reiserfs-7010",
536 "Congratulations! we have got hash function "
537 "screwed up");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700538 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800539 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540 pathrelse(&path);
541 return -EBUSY;
542 }
543 /* adjust offset of directory enrty */
544 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
545 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
546
547 /* update max-hash-collisions counter in reiserfs_sb_info */
548 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
549
Jeff Mahoney098297b2014-04-23 10:00:36 -0400550 /* we need to re-search for the insertion point */
551 if (gen_number != 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700552 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
553 NAME_NOT_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400554 reiserfs_warning(dir->i_sb, "vs-7032",
555 "entry with this key (%K) already "
556 "exists", &entry_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557
558 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800559 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700560 pathrelse(&path);
561 return -EBUSY;
562 }
563 }
564
565 /* perform the insertion of the entry that we have prepared */
566 retval =
567 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
568 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800570 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571 if (retval) {
572 reiserfs_check_path(&path);
573 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 dir->i_size += paste_size;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700577 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700578 if (!S_ISDIR(inode->i_mode) && visible)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400579 /* reiserfs_mkdir or reiserfs_rename will do that by itself */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700580 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700582 reiserfs_check_path(&path);
583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Jeff Mahoney098297b2014-04-23 10:00:36 -0400586/*
587 * quota utility function, call if you've had to abort after calling
588 * new_inode_init, and have not called reiserfs_new_inode yet.
589 * This should only be called on inodes that do not have stat data
590 * inserted into the tree yet.
591 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700592static int drop_new_inode(struct inode *inode)
593{
Christoph Hellwig9f754752010-03-03 09:05:05 -0500594 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700595 make_bad_inode(inode);
596 inode->i_flags |= S_NOQUOTA;
597 iput(inode);
598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Jeff Mahoney098297b2014-04-23 10:00:36 -0400601/*
602 * utility function that does setup for reiserfs_new_inode.
603 * dquot_initialize needs lots of credits so it's better to have it
604 * outside of a transaction, so we had to pull some bits of
605 * reiserfs_new_inode out into this func.
606 */
Al Viro8e071892011-07-26 02:50:53 -0400607static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700608{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400609 /*
610 * Make inode invalid - just in case we are going to drop it before
611 * the initialization happens
612 */
Jan Kara1b0a74d2005-08-13 13:15:34 +0200613 INODE_PKEY(inode)->k_objectid = 0;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400614
615 /*
616 * the quota init calls have to know who to charge the quota to, so
617 * we have to set uid and gid here
Dmitry Monakhov04b7ed02010-03-04 17:32:19 +0300618 */
Christian Brauner21cb47b2021-01-21 14:19:25 +0100619 inode_init_owner(&init_user_ns, inode, dir, mode);
Jan Kara2e6c97e2015-07-14 13:51:55 +0200620 return dquot_initialize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Christian Brauner549c7292021-01-21 14:19:43 +0100623static int reiserfs_create(struct user_namespace *mnt_userns, struct inode *dir,
624 struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626 int retval;
627 struct inode *inode;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400628 /*
629 * We need blocks for transaction + (user+group)*(quotas
630 * for new inode + update of quota for directory owner)
631 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700632 int jbegin_count =
633 JOURNAL_PER_BALANCE_CNT * 2 +
634 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
635 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
636 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400637 struct reiserfs_security_handle security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jan Kara2e6c97e2015-07-14 13:51:55 +0200639 retval = dquot_initialize(dir);
640 if (retval)
641 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 if (!(inode = new_inode(dir->i_sb))) {
644 return -ENOMEM;
645 }
Jan Kara2e6c97e2015-07-14 13:51:55 +0200646 retval = new_inode_init(inode, dir, mode);
647 if (retval) {
648 drop_new_inode(inode);
649 return retval;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400652 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500653 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400654 if (retval < 0) {
655 drop_new_inode(inode);
656 return retval;
657 }
658 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700659 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700661 retval = journal_begin(&th, dir->i_sb, jbegin_count);
662 if (retval) {
663 drop_new_inode(inode);
664 goto out_failed;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700667 retval =
668 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400669 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 if (retval)
671 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700673 inode->i_op = &reiserfs_file_inode_operations;
674 inode->i_fop = &reiserfs_file_operations;
675 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 retval =
678 reiserfs_add_entry(&th, dir, dentry->d_name.name,
679 dentry->d_name.len, inode, 1 /*visible */ );
680 if (retval) {
681 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200682 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700683 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400684 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685 if (err)
686 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500687 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 iput(inode);
689 goto out_failed;
690 }
691 reiserfs_update_inode_transaction(inode);
692 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Al Viro1e2e5472018-05-04 08:23:01 -0400694 d_instantiate_new(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400695 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400697out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 reiserfs_write_unlock(dir->i_sb);
699 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Christian Brauner549c7292021-01-21 14:19:43 +0100702static int reiserfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
703 struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 int retval;
706 struct inode *inode;
707 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400708 struct reiserfs_security_handle security;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400709 /*
710 * We need blocks for transaction + (user+group)*(quotas
711 * for new inode + update of quota for directory owner)
712 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700713 int jbegin_count =
714 JOURNAL_PER_BALANCE_CNT * 3 +
715 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
716 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jan Kara2e6c97e2015-07-14 13:51:55 +0200718 retval = dquot_initialize(dir);
719 if (retval)
720 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500721
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 if (!(inode = new_inode(dir->i_sb))) {
723 return -ENOMEM;
724 }
Jan Kara2e6c97e2015-07-14 13:51:55 +0200725 retval = new_inode_init(inode, dir, mode);
726 if (retval) {
727 drop_new_inode(inode);
728 return retval;
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400731 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500732 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400733 if (retval < 0) {
734 drop_new_inode(inode);
735 return retval;
736 }
737 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700738 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740 retval = journal_begin(&th, dir->i_sb, jbegin_count);
741 if (retval) {
742 drop_new_inode(inode);
743 goto out_failed;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 retval =
747 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400748 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700749 if (retval) {
750 goto out_failed;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 inode->i_op = &reiserfs_special_inode_operations;
754 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jeff Mahoney098297b2014-04-23 10:00:36 -0400756 /* FIXME: needed for block and char devices only */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 reiserfs_update_inode_transaction(inode);
760 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 retval =
763 reiserfs_add_entry(&th, dir, dentry->d_name.name,
764 dentry->d_name.len, inode, 1 /*visible */ );
765 if (retval) {
766 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200767 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400769 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 if (err)
771 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500772 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 iput(inode);
774 goto out_failed;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Al Viro1e2e5472018-05-04 08:23:01 -0400777 d_instantiate_new(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400778 retval = journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400780out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781 reiserfs_write_unlock(dir->i_sb);
782 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Christian Brauner549c7292021-01-21 14:19:43 +0100785static int reiserfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
786 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788 int retval;
789 struct inode *inode;
790 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400791 struct reiserfs_security_handle security;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400792 /*
793 * We need blocks for transaction + (user+group)*(quotas
794 * for new inode + update of quota for directory owner)
795 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700796 int jbegin_count =
797 JOURNAL_PER_BALANCE_CNT * 3 +
798 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
799 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Jan Kara2e6c97e2015-07-14 13:51:55 +0200801 retval = dquot_initialize(dir);
802 if (retval)
803 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Jeff Mahoney098297b2014-04-23 10:00:36 -0400806 /*
807 * set flag that new packing locality created and new blocks
808 * for the content of that directory are not displaced yet
809 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700810 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700812 mode = S_IFDIR | mode;
813 if (!(inode = new_inode(dir->i_sb))) {
814 return -ENOMEM;
815 }
Jan Kara2e6c97e2015-07-14 13:51:55 +0200816 retval = new_inode_init(inode, dir, mode);
817 if (retval) {
818 drop_new_inode(inode);
819 return retval;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400822 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500823 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400824 if (retval < 0) {
825 drop_new_inode(inode);
826 return retval;
827 }
828 jbegin_count += retval;
Jeff Mahoney278f6672013-08-08 17:34:46 -0400829 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 retval = journal_begin(&th, dir->i_sb, jbegin_count);
832 if (retval) {
833 drop_new_inode(inode);
834 goto out_failed;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jeff Mahoney098297b2014-04-23 10:00:36 -0400837 /*
838 * inc the link count now, so another writer doesn't overflow
839 * it while we sleep later on.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840 */
841 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Colin Ian King5404e7e2020-04-06 20:11:46 -0700843 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */,
844 old_format_only(dir->i_sb) ?
845 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
846 dentry, inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700847 if (retval) {
Al Viro99890a32011-03-02 09:35:13 -0500848 DEC_DIR_INODE_NLINK(dir)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700849 goto out_failed;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700852 reiserfs_update_inode_transaction(inode);
853 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700855 inode->i_op = &reiserfs_dir_inode_operations;
856 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Jeff Mahoney098297b2014-04-23 10:00:36 -0400858 /* note, _this_ add_entry will not update dir's stat data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700859 retval =
860 reiserfs_add_entry(&th, dir, dentry->d_name.name,
861 dentry->d_name.len, inode, 1 /*visible */ );
862 if (retval) {
863 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200864 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700865 DEC_DIR_INODE_NLINK(dir);
866 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400867 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700868 if (err)
869 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500870 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700871 iput(inode);
872 goto out_failed;
873 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400874 /* the above add_entry did not update dir's stat data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700875 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Al Viro1e2e5472018-05-04 08:23:01 -0400877 d_instantiate_new(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400878 retval = journal_end(&th);
Frederic Weisbeckerb10ab4c2009-08-25 02:44:21 +0200879out_failed:
Jeff Mahoney278f6672013-08-08 17:34:46 -0400880 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700881 return retval;
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700884static inline int reiserfs_empty_dir(struct inode *inode)
885{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400886 /*
887 * we can cheat because an old format dir cannot have
888 * EMPTY_DIR_SIZE, and a new format dir cannot have
889 * EMPTY_DIR_SIZE_V1. So, if the inode is either size,
890 * regardless of disk format version, the directory is empty.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 */
892 if (inode->i_size != EMPTY_DIR_SIZE &&
893 inode->i_size != EMPTY_DIR_SIZE_V1) {
894 return 0;
895 }
896 return 1;
897}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700899static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
900{
901 int retval, err;
902 struct inode *inode;
903 struct reiserfs_transaction_handle th;
904 int jbegin_count;
905 INITIALIZE_PATH(path);
906 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Jeff Mahoney098297b2014-04-23 10:00:36 -0400908 /*
909 * we will be doing 2 balancings and update 2 stat data, we
910 * change quotas of the owner of the directory and of the owner
911 * of the parent directory. The quota structure is possibly
912 * deleted only on last iput => outside of this transaction
913 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914 jbegin_count =
915 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
916 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Jan Kara2e6c97e2015-07-14 13:51:55 +0200918 retval = dquot_initialize(dir);
919 if (retval)
920 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500921
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700922 reiserfs_write_lock(dir->i_sb);
923 retval = journal_begin(&th, dir->i_sb, jbegin_count);
924 if (retval)
925 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700927 de.de_gen_number_bit_string = NULL;
928 if ((retval =
929 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
930 &path, &de)) == NAME_NOT_FOUND) {
931 retval = -ENOENT;
932 goto end_rmdir;
933 } else if (retval == IO_ERROR) {
934 retval = -EIO;
935 goto end_rmdir;
936 }
937
David Howells2b0143b2015-03-17 22:25:59 +0000938 inode = d_inode(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700939
940 reiserfs_update_inode_transaction(inode);
941 reiserfs_update_inode_transaction(dir);
942
943 if (de.de_objectid != inode->i_ino) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400944 /*
945 * FIXME: compare key of an object and a key found in the entry
946 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 retval = -EIO;
948 goto end_rmdir;
949 }
950 if (!reiserfs_empty_dir(inode)) {
951 retval = -ENOTEMPTY;
952 goto end_rmdir;
953 }
954
955 /* cut entry from dir directory */
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400956 retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
957 dir, NULL, /* page */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700958 0 /*new file size - not used here */ );
959 if (retval < 0)
960 goto end_rmdir;
961
962 if (inode->i_nlink != 2 && inode->i_nlink != 1)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400963 reiserfs_error(inode->i_sb, "reiserfs-7040",
964 "empty directory has nlink != 2 (%d)",
965 inode->i_nlink);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966
Dave Hansence71ec32006-09-30 23:29:06 -0700967 clear_nlink(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700968 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969 reiserfs_update_sd(&th, inode);
970
971 DEC_DIR_INODE_NLINK(dir)
Colin Ian King5404e7e2020-04-06 20:11:46 -0700972 dir->i_size -= (DEH_SIZE + de.de_entrylen);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700973 reiserfs_update_sd(&th, dir);
974
975 /* prevent empty directory from getting lost */
976 add_save_link(&th, inode, 0 /* not truncate */ );
977
Jeff Mahoney58d85422014-04-23 10:00:38 -0400978 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 reiserfs_check_path(&path);
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400980out_rmdir:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700981 reiserfs_write_unlock(dir->i_sb);
982 return retval;
983
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400984end_rmdir:
Jeff Mahoney098297b2014-04-23 10:00:36 -0400985 /*
986 * we must release path, because we did not call
987 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
988 * release path if operation was not complete
989 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700990 pathrelse(&path);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400991 err = journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 reiserfs_write_unlock(dir->i_sb);
993 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700996static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
997{
998 int retval, err;
999 struct inode *inode;
1000 struct reiserfs_dir_entry de;
1001 INITIALIZE_PATH(path);
1002 struct reiserfs_transaction_handle th;
1003 int jbegin_count;
1004 unsigned long savelink;
1005
Jan Kara2e6c97e2015-07-14 13:51:55 +02001006 retval = dquot_initialize(dir);
1007 if (retval)
1008 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001009
David Howells2b0143b2015-03-17 22:25:59 +00001010 inode = d_inode(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001011
Jeff Mahoney098297b2014-04-23 10:00:36 -04001012 /*
1013 * in this transaction we can be doing at max two balancings and
1014 * update two stat datas, we change quotas of the owner of the
1015 * directory and of the owner of the parent directory. The quota
1016 * structure is possibly deleted only on iput => outside of
1017 * this transaction
1018 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001019 jbegin_count =
1020 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
1021 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1022
Jeff Mahoney278f6672013-08-08 17:34:46 -04001023 reiserfs_write_lock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001024 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1025 if (retval)
1026 goto out_unlink;
1027
1028 de.de_gen_number_bit_string = NULL;
1029 if ((retval =
1030 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
1031 &path, &de)) == NAME_NOT_FOUND) {
1032 retval = -ENOENT;
1033 goto end_unlink;
1034 } else if (retval == IO_ERROR) {
1035 retval = -EIO;
1036 goto end_unlink;
1037 }
1038
1039 reiserfs_update_inode_transaction(inode);
1040 reiserfs_update_inode_transaction(dir);
1041
1042 if (de.de_objectid != inode->i_ino) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001043 /*
1044 * FIXME: compare key of an object and a key found in the entry
1045 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001046 retval = -EIO;
1047 goto end_unlink;
1048 }
1049
1050 if (!inode->i_nlink) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001051 reiserfs_warning(inode->i_sb, "reiserfs-7042",
1052 "deleting nonexistent file (%lu), %d",
1053 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001054 set_nlink(inode, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 }
1056
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001057 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001058
1059 /*
1060 * we schedule before doing the add_save_link call, save the link
1061 * count so we don't race
1062 */
1063 savelink = inode->i_nlink;
1064
1065 retval =
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001066 reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001067 0);
1068 if (retval < 0) {
Dave Hansend8c76e62006-09-30 23:29:04 -07001069 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070 goto end_unlink;
1071 }
Deepa Dinamani02027d42016-09-14 07:48:05 -07001072 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073 reiserfs_update_sd(&th, inode);
1074
1075 dir->i_size -= (de.de_entrylen + DEH_SIZE);
Deepa Dinamani02027d42016-09-14 07:48:05 -07001076 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001077 reiserfs_update_sd(&th, dir);
1078
1079 if (!savelink)
1080 /* prevent file from getting lost */
1081 add_save_link(&th, inode, 0 /* not truncate */ );
1082
Jeff Mahoney58d85422014-04-23 10:00:38 -04001083 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084 reiserfs_check_path(&path);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001085 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 return retval;
1087
Jeff Mahoneycf776a72014-04-23 10:00:41 -04001088end_unlink:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001089 pathrelse(&path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001090 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 reiserfs_check_path(&path);
1092 if (err)
1093 retval = err;
Jeff Mahoneycf776a72014-04-23 10:00:41 -04001094out_unlink:
Jeff Mahoney278f6672013-08-08 17:34:46 -04001095 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096 return retval;
1097}
1098
Christian Brauner549c7292021-01-21 14:19:43 +01001099static int reiserfs_symlink(struct user_namespace *mnt_userns,
1100 struct inode *parent_dir, struct dentry *dentry,
1101 const char *symname)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001102{
1103 int retval;
1104 struct inode *inode;
1105 char *name;
1106 int item_len;
1107 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001108 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 int mode = S_IFLNK | S_IRWXUGO;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001110 /*
1111 * We need blocks for transaction + (user+group)*(quotas for
1112 * new inode + update of quota for directory owner)
1113 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001114 int jbegin_count =
1115 JOURNAL_PER_BALANCE_CNT * 3 +
1116 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1117 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1118
Jan Kara2e6c97e2015-07-14 13:51:55 +02001119 retval = dquot_initialize(parent_dir);
1120 if (retval)
1121 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001123 if (!(inode = new_inode(parent_dir->i_sb))) {
1124 return -ENOMEM;
1125 }
Jan Kara2e6c97e2015-07-14 13:51:55 +02001126 retval = new_inode_init(inode, parent_dir, mode);
1127 if (retval) {
1128 drop_new_inode(inode);
1129 return retval;
1130 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001131
Eric Paris2a7dba32011-02-01 11:05:39 -05001132 retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1133 &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001134 if (retval < 0) {
1135 drop_new_inode(inode);
1136 return retval;
1137 }
1138 jbegin_count += retval;
1139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001140 reiserfs_write_lock(parent_dir->i_sb);
1141 item_len = ROUND_UP(strlen(symname));
1142 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1143 retval = -ENAMETOOLONG;
1144 drop_new_inode(inode);
1145 goto out_failed;
1146 }
1147
Pekka Enbergd739b422006-02-01 03:06:43 -08001148 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 if (!name) {
1150 drop_new_inode(inode);
1151 retval = -ENOMEM;
1152 goto out_failed;
1153 }
1154 memcpy(name, symname, strlen(symname));
1155 padd_item(name, item_len, strlen(symname));
1156
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001157 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1158 if (retval) {
1159 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001160 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161 goto out_failed;
1162 }
1163
1164 retval =
1165 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001166 dentry, inode, &security);
Pekka Enbergd739b422006-02-01 03:06:43 -08001167 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 if (retval) { /* reiserfs_new_inode iputs for us */
1169 goto out_failed;
1170 }
1171
1172 reiserfs_update_inode_transaction(inode);
1173 reiserfs_update_inode_transaction(parent_dir);
1174
1175 inode->i_op = &reiserfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001176 inode_nohighmem(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001177 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1178
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1180 dentry->d_name.len, inode, 1 /*visible */ );
1181 if (retval) {
1182 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001183 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001184 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001185 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186 if (err)
1187 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -05001188 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001189 iput(inode);
1190 goto out_failed;
1191 }
1192
Al Viro1e2e5472018-05-04 08:23:01 -04001193 d_instantiate_new(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001194 retval = journal_end(&th);
Jeff Mahoneycf776a72014-04-23 10:00:41 -04001195out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 reiserfs_write_unlock(parent_dir->i_sb);
1197 return retval;
1198}
1199
1200static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1201 struct dentry *dentry)
1202{
1203 int retval;
David Howells2b0143b2015-03-17 22:25:59 +00001204 struct inode *inode = d_inode(old_dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 struct reiserfs_transaction_handle th;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001206 /*
1207 * We need blocks for transaction + update of quotas for
1208 * the owners of the directory
1209 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001210 int jbegin_count =
1211 JOURNAL_PER_BALANCE_CNT * 3 +
1212 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1213
Jan Kara2e6c97e2015-07-14 13:51:55 +02001214 retval = dquot_initialize(dir);
1215 if (retval)
1216 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001217
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 reiserfs_write_lock(dir->i_sb);
1219 if (inode->i_nlink >= REISERFS_LINK_MAX) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001220 /* FIXME: sd_nlink is 32 bit for new files */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001221 reiserfs_write_unlock(dir->i_sb);
1222 return -EMLINK;
1223 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001224
1225 /* inc before scheduling so reiserfs_unlink knows we are here */
Dave Hansend8c76e62006-09-30 23:29:04 -07001226 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227
1228 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1229 if (retval) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001230 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001231 reiserfs_write_unlock(dir->i_sb);
1232 return retval;
1233 }
1234
1235 /* create new entry */
1236 retval =
1237 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1238 dentry->d_name.len, inode, 1 /*visible */ );
1239
1240 reiserfs_update_inode_transaction(inode);
1241 reiserfs_update_inode_transaction(dir);
1242
1243 if (retval) {
1244 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001245 drop_nlink(inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001246 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001247 reiserfs_write_unlock(dir->i_sb);
1248 return err ? err : retval;
1249 }
1250
Deepa Dinamani02027d42016-09-14 07:48:05 -07001251 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001252 reiserfs_update_sd(&th, inode);
1253
Al Viro7de9c6ee2010-10-23 11:11:40 -04001254 ihold(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001255 d_instantiate(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001256 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001257 reiserfs_write_unlock(dir->i_sb);
1258 return retval;
1259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jeff Mahoney0222e652009-03-30 14:02:44 -04001261/* de contains information pointing to an entry which */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001262static int de_still_valid(const char *name, int len,
1263 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 struct reiserfs_dir_entry tmp = *de;
1266
Jeff Mahoney098297b2014-04-23 10:00:36 -04001267 /* recalculate pointer to name and name length */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001268 set_de_name_and_namelen(&tmp);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001269 /* FIXME: could check more */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001270 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1271 return 0;
1272 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001275static int entry_points_to_object(const char *name, int len,
1276 struct reiserfs_dir_entry *de,
1277 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001279 if (!de_still_valid(name, len, de))
1280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001282 if (inode) {
1283 if (!de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001284 reiserfs_panic(inode->i_sb, "vs-7042",
1285 "entry must be visible");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001286 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001289 /* this must be added hidden entry */
1290 if (de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001291 reiserfs_panic(NULL, "vs-7043", "entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001293 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001297static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1298 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001300 /* JDM These operations are endian safe - both are le */
1301 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1302 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Jeff Mahoney0222e652009-03-30 14:02:44 -04001305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * process, that is going to call fix_nodes/do_balance must hold only
1307 * one path. If it holds 2 or more, it can get into endless waiting in
Jeff Mahoney0222e652009-03-30 14:02:44 -04001308 * get_empty_nodes or its clones
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Christian Brauner549c7292021-01-21 14:19:43 +01001310static int reiserfs_rename(struct user_namespace *mnt_userns,
1311 struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001312 struct inode *new_dir, struct dentry *new_dentry,
1313 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 int retval;
1316 INITIALIZE_PATH(old_entry_path);
1317 INITIALIZE_PATH(new_entry_path);
1318 INITIALIZE_PATH(dot_dot_entry_path);
1319 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1320 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1321 struct inode *old_inode, *new_dentry_inode;
1322 struct reiserfs_transaction_handle th;
1323 int jbegin_count;
1324 umode_t old_inode_mode;
1325 unsigned long savelink = 1;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001326 struct timespec64 ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001328 if (flags & ~RENAME_NOREPLACE)
1329 return -EINVAL;
1330
Jeff Mahoney098297b2014-04-23 10:00:36 -04001331 /*
1332 * three balancings: (1) old name removal, (2) new name insertion
1333 * and (3) maybe "save" link insertion
1334 * stat data updates: (1) old directory,
1335 * (2) new directory and (3) maybe old object stat data (when it is
1336 * directory) and (4) maybe stat data of object to which new entry
1337 * pointed initially and (5) maybe block containing ".." of
1338 * renamed directory
1339 * quota updates: two parent directories
1340 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 jbegin_count =
1342 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1343 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Jan Kara2e6c97e2015-07-14 13:51:55 +02001345 retval = dquot_initialize(old_dir);
1346 if (retval)
1347 return retval;
1348 retval = dquot_initialize(new_dir);
1349 if (retval)
1350 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001351
David Howells2b0143b2015-03-17 22:25:59 +00001352 old_inode = d_inode(old_dentry);
1353 new_dentry_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Jeff Mahoney098297b2014-04-23 10:00:36 -04001355 /*
1356 * make sure that oldname still exists and points to an object we
1357 * are going to rename
1358 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001359 old_de.de_gen_number_bit_string = NULL;
1360 reiserfs_write_lock(old_dir->i_sb);
1361 retval =
1362 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1363 old_dentry->d_name.len, &old_entry_path,
1364 &old_de);
1365 pathrelse(&old_entry_path);
1366 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 reiserfs_write_unlock(old_dir->i_sb);
1368 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001370
1371 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1372 reiserfs_write_unlock(old_dir->i_sb);
1373 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001375
1376 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001378 /*
1379 * make sure that directory being renamed has correct ".."
1380 * and that its new parent directory has not too many links
1381 * already
1382 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001383 if (new_dentry_inode) {
1384 if (!reiserfs_empty_dir(new_dentry_inode)) {
1385 reiserfs_write_unlock(old_dir->i_sb);
1386 return -ENOTEMPTY;
1387 }
1388 }
1389
Jeff Mahoney098297b2014-04-23 10:00:36 -04001390 /*
1391 * directory is renamed, its parent directory will be changed,
1392 * so find ".." entry
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001393 */
1394 dot_dot_de.de_gen_number_bit_string = NULL;
1395 retval =
1396 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1397 &dot_dot_de);
1398 pathrelse(&dot_dot_entry_path);
1399 if (retval != NAME_FOUND) {
1400 reiserfs_write_unlock(old_dir->i_sb);
1401 return -EIO;
1402 }
1403
1404 /* inode number of .. must equal old_dir->i_ino */
1405 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1406 reiserfs_write_unlock(old_dir->i_sb);
1407 return -EIO;
1408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001411 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1412 if (retval) {
1413 reiserfs_write_unlock(old_dir->i_sb);
1414 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001417 /* add new entry (or find the existing one) */
1418 retval =
1419 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1420 new_dentry->d_name.len, old_inode, 0);
1421 if (retval == -EEXIST) {
1422 if (!new_dentry_inode) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001423 reiserfs_panic(old_dir->i_sb, "vs-7050",
1424 "new entry is found, new inode == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001425 }
1426 } else if (retval) {
Jeff Mahoney58d85422014-04-23 10:00:38 -04001427 int err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 reiserfs_write_unlock(old_dir->i_sb);
1429 return err ? err : retval;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001432 reiserfs_update_inode_transaction(old_dir);
1433 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Jeff Mahoney098297b2014-04-23 10:00:36 -04001435 /*
1436 * this makes it so an fsync on an open fd for the old name will
1437 * commit the rename operation
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001438 */
1439 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001441 if (new_dentry_inode)
1442 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001444 while (1) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001445 /*
1446 * look for old name using corresponding entry key
1447 * (found by reiserfs_find_entry)
1448 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001449 if ((retval =
1450 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1451 &old_entry_path,
1452 &old_de)) != NAME_FOUND) {
1453 pathrelse(&old_entry_path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001454 journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001455 reiserfs_write_unlock(old_dir->i_sb);
1456 return -EIO;
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001459 copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001461 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1462
Jeff Mahoney098297b2014-04-23 10:00:36 -04001463 /* look for new name by reiserfs_find_entry */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001464 new_de.de_gen_number_bit_string = NULL;
1465 retval =
1466 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1467 new_dentry->d_name.len, &new_entry_path,
1468 &new_de);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001469 /*
1470 * reiserfs_add_entry should not return IO_ERROR,
1471 * because it is called with essentially same parameters from
1472 * reiserfs_add_entry above, and we'll catch any i/o errors
1473 * before we get here.
1474 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001475 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1476 pathrelse(&new_entry_path);
1477 pathrelse(&old_entry_path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001478 journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001479 reiserfs_write_unlock(old_dir->i_sb);
1480 return -EIO;
1481 }
1482
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001483 copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001484
1485 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1486
1487 if (S_ISDIR(old_inode->i_mode)) {
1488 if ((retval =
1489 search_by_entry_key(new_dir->i_sb,
1490 &dot_dot_de.de_entry_key,
1491 &dot_dot_entry_path,
1492 &dot_dot_de)) != NAME_FOUND) {
1493 pathrelse(&dot_dot_entry_path);
1494 pathrelse(&new_entry_path);
1495 pathrelse(&old_entry_path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001496 journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001497 reiserfs_write_unlock(old_dir->i_sb);
1498 return -EIO;
1499 }
1500 copy_item_head(&dot_dot_ih,
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001501 tp_item_head(&dot_dot_entry_path));
Jeff Mahoney098297b2014-04-23 10:00:36 -04001502 /* node containing ".." gets into transaction */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503 reiserfs_prepare_for_journal(old_inode->i_sb,
1504 dot_dot_de.de_bh, 1);
1505 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001506 /*
1507 * we should check seals here, not do
1508 * this stuff, yes? Then, having
1509 * gathered everything into RAM we
1510 * should lock the buffers, yes? -Hans
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001511 */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001512 /*
1513 * probably. our rename needs to hold more
1514 * than one path at once. The seals would
1515 * have to be written to deal with multi-path
1516 * issues -chris
1517 */
1518 /*
1519 * sanity checking before doing the rename - avoid races many
1520 * of the above checks could have scheduled. We have to be
1521 * sure our items haven't been shifted by another process.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001522 */
1523 if (item_moved(&new_entry_ih, &new_entry_path) ||
1524 !entry_points_to_object(new_dentry->d_name.name,
1525 new_dentry->d_name.len,
1526 &new_de, new_dentry_inode) ||
1527 item_moved(&old_entry_ih, &old_entry_path) ||
1528 !entry_points_to_object(old_dentry->d_name.name,
1529 old_dentry->d_name.len,
1530 &old_de, old_inode)) {
1531 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1532 new_de.de_bh);
1533 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1534 old_de.de_bh);
1535 if (S_ISDIR(old_inode_mode))
1536 reiserfs_restore_prepared_buffer(old_inode->
1537 i_sb,
1538 dot_dot_de.
1539 de_bh);
1540 continue;
1541 }
1542 if (S_ISDIR(old_inode_mode)) {
1543 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1544 !entry_points_to_object("..", 2, &dot_dot_de,
1545 old_dir)) {
1546 reiserfs_restore_prepared_buffer(old_inode->
1547 i_sb,
1548 old_de.de_bh);
1549 reiserfs_restore_prepared_buffer(old_inode->
1550 i_sb,
1551 new_de.de_bh);
1552 reiserfs_restore_prepared_buffer(old_inode->
1553 i_sb,
1554 dot_dot_de.
1555 de_bh);
1556 continue;
1557 }
1558 }
1559
1560 RFALSE(S_ISDIR(old_inode_mode) &&
1561 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1562
1563 break;
1564 }
1565
Jeff Mahoney098297b2014-04-23 10:00:36 -04001566 /*
1567 * ok, all the changes can be done in one fell swoop when we
1568 * have claimed all the buffers needed.
1569 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001570
1571 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1572 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001573 journal_mark_dirty(&th, new_de.de_bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001574
1575 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001576 journal_mark_dirty(&th, old_de.de_bh);
Deepa Dinamani02027d42016-09-14 07:48:05 -07001577 ctime = current_time(old_dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001578 old_dir->i_ctime = old_dir->i_mtime = ctime;
1579 new_dir->i_ctime = new_dir->i_mtime = ctime;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001580 /*
1581 * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
1582 * which adds ctime update of renamed object
1583 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001584 old_inode->i_ctime = ctime;
1585
1586 if (new_dentry_inode) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001587 /* adjust link number of the victim */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001588 if (S_ISDIR(new_dentry_inode->i_mode)) {
Dave Hansence71ec32006-09-30 23:29:06 -07001589 clear_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001590 } else {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001591 drop_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001592 }
1593 new_dentry_inode->i_ctime = ctime;
1594 savelink = new_dentry_inode->i_nlink;
1595 }
1596
1597 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001598 /* adjust ".." of renamed directory */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001599 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001600 journal_mark_dirty(&th, dot_dot_de.de_bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001601
Jeff Mahoney098297b2014-04-23 10:00:36 -04001602 /*
1603 * there (in new_dir) was no directory, so it got new link
1604 * (".." of renamed directory)
1605 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001606 if (!new_dentry_inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001607 INC_DIR_INODE_NLINK(new_dir);
1608
1609 /* old directory lost one link - ".. " of renamed directory */
1610 DEC_DIR_INODE_NLINK(old_dir);
1611 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001612 /*
1613 * looks like in 2.3.99pre3 brelse is atomic.
1614 * so we can use pathrelse
1615 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001616 pathrelse(&new_entry_path);
1617 pathrelse(&dot_dot_entry_path);
1618
Jeff Mahoney098297b2014-04-23 10:00:36 -04001619 /*
1620 * FIXME: this reiserfs_cut_from_item's return value may screw up
1621 * anybody, but it will panic if will not be able to find the
1622 * entry. This needs one more clean up
1623 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001624 if (reiserfs_cut_from_item
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001625 (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001626 0) < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -04001627 reiserfs_error(old_dir->i_sb, "vs-7060",
1628 "couldn't not cut old name. Fsck later?");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001629
1630 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1631
1632 reiserfs_update_sd(&th, old_dir);
1633 reiserfs_update_sd(&th, new_dir);
1634 reiserfs_update_sd(&th, old_inode);
1635
1636 if (new_dentry_inode) {
1637 if (savelink == 0)
1638 add_save_link(&th, new_dentry_inode,
1639 0 /* not truncate */ );
1640 reiserfs_update_sd(&th, new_dentry_inode);
1641 }
1642
Jeff Mahoney58d85422014-04-23 10:00:38 -04001643 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001644 reiserfs_write_unlock(old_dir->i_sb);
1645 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Jeff Mahoney098297b2014-04-23 10:00:36 -04001648/* directories can handle most operations... */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001649const struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001650 .create = reiserfs_create,
1651 .lookup = reiserfs_lookup,
1652 .link = reiserfs_link,
1653 .unlink = reiserfs_unlink,
1654 .symlink = reiserfs_symlink,
1655 .mkdir = reiserfs_mkdir,
1656 .rmdir = reiserfs_rmdir,
1657 .mknod = reiserfs_mknod,
1658 .rename = reiserfs_rename,
1659 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001660 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001661 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001662 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -08001663 .set_acl = reiserfs_set_acl,
Miklos Szeredi03eb6062021-04-07 14:36:44 +02001664 .fileattr_get = reiserfs_fileattr_get,
1665 .fileattr_set = reiserfs_fileattr_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666};
1667
1668/*
1669 * symlink operations.. same as page_symlink_inode_operations, with xattr
1670 * stuff added
1671 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001672const struct inode_operations reiserfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001673 .get_link = page_get_link,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001674 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001675 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001676 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677};
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/*
1680 * special file operations.. just xattr/acl stuff
1681 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001682const struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001683 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001684 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001685 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001686 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -08001687 .set_acl = reiserfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688};