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