blob: 4f322e5ed8405f63e3f980256de23b9acc2c9849 [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>
16#include <linux/reiserfs_fs.h>
17#include <linux/reiserfs_acl.h>
18#include <linux/reiserfs_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/quotaops.h>
20
Dave Hansend8c76e62006-09-30 23:29:04 -070021#define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) i->i_nlink=1; }
Dave Hansen9a53c3a2006-09-30 23:29:03 -070022#define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24// directory item contains array of entry headers. This performs
25// binary search through that array
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070026static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070028 struct item_head *ih = de->de_ih;
29 struct reiserfs_de_head *deh = de->de_deh;
30 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070032 lbound = 0;
33 rbound = I_ENTRY_COUNT(ih) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070035 for (j = (rbound + lbound) / 2; lbound <= rbound;
36 j = (rbound + lbound) / 2) {
37 if (off < deh_offset(deh + j)) {
38 rbound = j - 1;
39 continue;
40 }
41 if (off > deh_offset(deh + j)) {
42 lbound = j + 1;
43 continue;
44 }
45 // this is not name found, but matched third key component
46 de->de_entry_num = j;
47 return NAME_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070050 de->de_entry_num = lbound;
51 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054// comment? maybe something like set de to point to what the path points to?
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070055static inline void set_de_item_location(struct reiserfs_dir_entry *de,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -080056 struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058 de->de_bh = get_last_bh(path);
59 de->de_ih = get_ih(path);
60 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
61 de->de_item_num = PATH_LAST_POSITION(path);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64// de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric Sesterhenn14a61442006-10-03 23:36:38 +020069 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
72 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
73 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh);
74 if (de->de_name[de->de_namelen - 1] == 0)
75 de->de_namelen = strlen(de->de_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078// what entry points to
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079static inline void set_de_object_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Eric Sesterhenn14a61442006-10-03 23:36:38 +020081 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num]));
83 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric Sesterhenn14a61442006-10-03 23:36:38 +020090 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092 /* store key of the found entry */
93 de->de_entry_key.version = KEY_FORMAT_3_5;
94 de->de_entry_key.on_disk_key.k_dir_id =
95 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
96 de->de_entry_key.on_disk_key.k_objectid =
97 le32_to_cpu(de->de_ih->ih_key.k_objectid);
98 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh));
99 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* We assign a key to each directory item, and place multiple entries
103in a single directory item. A directory item has a key equal to the
104key of the first directory entry in it.
105
106This function first calls search_by_key, then, if item whose first
107entry matches is not found it looks for the entry inside directory
108item found by search_by_key. Fills the path to the entry, and to the
109entry position in the item
110
111*/
112
113/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700114int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800115 struct treepath *path, struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700119 retval = search_item(sb, key, path);
120 switch (retval) {
121 case ITEM_NOT_FOUND:
122 if (!PATH_LAST_POSITION(path)) {
123 reiserfs_warning(sb,
124 "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
125 pathrelse(path);
126 return IO_ERROR;
127 }
128 PATH_LAST_POSITION(path)--;
129
130 case ITEM_FOUND:
131 break;
132
133 case IO_ERROR:
134 return retval;
135
136 default:
137 pathrelse(path);
138 reiserfs_warning(sb,
139 "vs-7002: search_by_entry_key: no path to here");
140 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 if (!is_direntry_le_ih(de->de_ih) ||
147 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
148 print_block(de->de_bh, 0, -1, -1);
149 reiserfs_panic(sb,
150 "vs-7005: search_by_entry_key: found item %h is not directory item or "
151 "does not belong to the same directory as key %K",
152 de->de_ih, key);
153 }
154#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 /* binary search in directory item by third componen t of the
157 key. sets de->de_entry_num of de */
158 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
159 path->pos_in_item = de->de_entry_num;
160 if (retval != NAME_NOT_FOUND) {
161 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
162 set_de_name_and_namelen(de);
163 set_de_object_key(de);
164 }
165 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
169
170/* The third component is hashed, and you can choose from more than
171 one hash function. Per directory hashes are not yet implemented
172 but are thought about. This function should be moved to hashes.c
173 Jedi, please do so. -Hans */
174
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700175static __u32 get_third_component(struct super_block *s,
176 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 if (!len || (len == 1 && name[0] == '.'))
181 return DOT_OFFSET;
182 if (len == 2 && name[0] == '.' && name[1] == '.')
183 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 // take bits from 7-th to 30-th including both bounds
188 res = GET_HASH_VALUE(res);
189 if (res == 0)
190 // needed to have no names before "." and ".." those have hash
191 // value == 0 and generation conters 1 and 2 accordingly
192 res = 128;
193 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196static int reiserfs_match(struct reiserfs_dir_entry *de,
197 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 if ((namelen == de->de_namelen) &&
202 !memcmp(de->de_name, name, de->de_namelen))
203 retval =
204 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
205 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700207 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
211
212 /* used when hash collisions exist */
213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214static int linear_search_in_dir_item(struct cpu_key *key,
215 struct reiserfs_dir_entry *de,
216 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 struct reiserfs_de_head *deh = de->de_deh;
219 int retval;
220 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 if (i == I_ENTRY_COUNT(de->de_ih) ||
225 GET_HASH_VALUE(deh_offset(deh + i)) !=
226 GET_HASH_VALUE(cpu_key_k_offset(key))) {
227 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
231 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 for (; i >= 0; i--, deh--) {
236 if (GET_HASH_VALUE(deh_offset(deh)) !=
237 GET_HASH_VALUE(cpu_key_k_offset(key))) {
238 // hash value does not match, no need to check whole name
239 return NAME_NOT_FOUND;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242 /* mark, that this generation number is used */
243 if (de->de_gen_number_bit_string)
244 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500245 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247 // calculate pointer to name and namelen
248 de->de_entry_num = i;
249 set_de_name_and_namelen(de);
250
251 if ((retval =
252 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
253 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
254
255 // key of pointed object
256 set_de_object_key(de);
257
258 store_de_entry_key(de);
259
260 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
261 return retval;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700265 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
266 /* we have reached left most entry in the node. In common we
267 have to go to the left neighbor, but if generation counter
268 is 0 already, we know for sure, that there is no name with
269 the same hash value */
270 // FIXME: this work correctly only because hash value can not
271 // be 0. Btw, in case of Yura's hash it is probably possible,
272 // so, this is a bug
273 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275 RFALSE(de->de_item_num,
276 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700278 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281// may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
282// FIXME: should add something like IOERROR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800284 struct treepath *path_to_entry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700285 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 struct cpu_key key_to_search;
288 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
291 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700293 /* we will search for this key in the tree */
294 make_cpu_key(&key_to_search, dir,
295 get_third_component(dir->i_sb, name, namelen),
296 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 while (1) {
299 retval =
300 search_by_entry_key(dir->i_sb, &key_to_search,
301 path_to_entry, de);
302 if (retval == IO_ERROR) {
303 reiserfs_warning(dir->i_sb, "zam-7001: io error in %s",
Harvey Harrisonfbe54982008-04-28 02:16:22 -0700304 __func__);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305 return IO_ERROR;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308 /* compare names for all entries having given hash value */
309 retval =
310 linear_search_in_dir_item(&key_to_search, de, name,
311 namelen);
312 if (retval != GOTO_PREVIOUS_ITEM) {
313 /* there is no need to scan directory anymore. Given entry found or does not exist */
314 path_to_entry->pos_in_item = de->de_entry_num;
315 return retval;
316 }
317
318 /* there is left neighboring item of this directory and given entry can be there */
319 set_cpu_key_k_offset(&key_to_search,
320 le_ih_k_offset(de->de_ih) - 1);
321 pathrelse(path_to_entry);
322
323 } /* while (1) */
324}
325
326static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
327 struct nameidata *nd)
328{
329 int retval;
330 struct inode *inode = NULL;
331 struct reiserfs_dir_entry de;
332 INITIALIZE_PATH(path_to_entry);
333
334 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
335 return ERR_PTR(-ENAMETOOLONG);
336
337 reiserfs_write_lock(dir->i_sb);
338 de.de_gen_number_bit_string = NULL;
339 retval =
340 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
341 &path_to_entry, &de);
342 pathrelse(&path_to_entry);
343 if (retval == NAME_FOUND) {
344 /* Hide the .reiserfs_priv directory */
345 if (reiserfs_xattrs(dir->i_sb) &&
346 !old_format_only(dir->i_sb) &&
347 REISERFS_SB(dir->i_sb)->priv_root &&
348 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
349 de.de_objectid ==
350 le32_to_cpu(INODE_PKEY
351 (REISERFS_SB(dir->i_sb)->priv_root->d_inode)->
352 k_objectid)) {
353 reiserfs_write_unlock(dir->i_sb);
354 return ERR_PTR(-EACCES);
355 }
356
357 inode =
358 reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
359 if (!inode || IS_ERR(inode)) {
360 reiserfs_write_unlock(dir->i_sb);
361 return ERR_PTR(-EACCES);
362 }
363
364 /* Propogate the priv_object flag so we know we're in the priv tree */
365 if (is_reiserfs_priv_object(dir))
366 reiserfs_mark_inode_private(inode);
367 }
368 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700370 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Pekka Enbergd9975d62006-01-14 13:21:09 -0800373 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
377** looks up the dentry of the parent directory for child.
378** taken from ext2_get_parent
379*/
380struct dentry *reiserfs_get_parent(struct dentry *child)
381{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700382 int retval;
383 struct inode *inode = NULL;
384 struct reiserfs_dir_entry de;
385 INITIALIZE_PATH(path_to_entry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700386 struct inode *dir = child->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 if (dir->i_nlink == 0) {
389 return ERR_PTR(-ENOENT);
390 }
391 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700393 reiserfs_write_lock(dir->i_sb);
394 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
395 pathrelse(&path_to_entry);
396 if (retval != NAME_FOUND) {
397 reiserfs_write_unlock(dir->i_sb);
398 return ERR_PTR(-ENOENT);
399 }
400 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Christoph Hellwig44003722008-08-11 15:49:04 +0200403 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* add entry to the directory (entry can be hidden).
407
408insert definition of when hidden directories are used here -Hans
409
410 Does not mark dir inode dirty, do it after successesfull call to it */
411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
413 struct inode *dir, const char *name, int namelen,
414 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700416 struct cpu_key entry_key;
417 struct reiserfs_de_head *deh;
418 INITIALIZE_PATH(path);
419 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500420 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 int gen_number;
422 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
423 if we create file with short name */
424 char *buffer;
425 int buflen, paste_size;
426 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430 /* cannot allow items to be added into a busy deleted directory */
431 if (!namelen)
432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
435 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 /* each entry has unique key. compose it */
438 make_cpu_key(&entry_key, dir,
439 get_third_component(dir->i_sb, name, namelen),
440 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700442 /* get memory for composing the entry */
443 buflen = DEH_SIZE + ROUND_UP(namelen);
444 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800445 buffer = kmalloc(buflen, GFP_NOFS);
Al Viro9dce07f2008-03-29 03:07:28 +0000446 if (!buffer)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 return -ENOMEM;
448 } else
449 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451 paste_size =
452 (get_inode_sd_version(dir) ==
453 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
456 deh = (struct reiserfs_de_head *)buffer;
457 deh->deh_location = 0; /* JDM Endian safe if 0 */
458 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
459 deh->deh_state = 0; /* JDM Endian safe if 0 */
460 /* put key (ino analog) to de */
461 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
462 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700464 /* copy name */
465 memcpy((char *)(deh + 1), name, namelen);
466 /* padd by 0s to the 4 byte boundary */
467 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700469 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
470 mark_de_without_sd(deh);
471 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 /* find the proper place for the new entry */
474 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500475 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700476 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
477 if (retval != NAME_NOT_FOUND) {
478 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800479 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 pathrelse(&path);
481
482 if (retval == IO_ERROR) {
483 return -EIO;
484 }
485
486 if (retval != NAME_FOUND) {
487 reiserfs_warning(dir->i_sb,
488 "zam-7002:%s: \"reiserfs_find_entry\" "
489 "has returned unexpected value (%d)",
Harvey Harrisonfbe54982008-04-28 02:16:22 -0700490 __func__, retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491 }
492
493 return -EEXIST;
494 }
495
496 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500497 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700498 MAX_GENERATION_NUMBER + 1);
499 if (gen_number > MAX_GENERATION_NUMBER) {
500 /* there is no free generation number */
501 reiserfs_warning(dir->i_sb,
502 "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
503 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800504 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 pathrelse(&path);
506 return -EBUSY;
507 }
508 /* adjust offset of directory enrty */
509 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
510 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
511
512 /* update max-hash-collisions counter in reiserfs_sb_info */
513 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
514
515 if (gen_number != 0) { /* we need to re-search for the insertion point */
516 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
517 NAME_NOT_FOUND) {
518 reiserfs_warning(dir->i_sb,
519 "vs-7032: reiserfs_add_entry: "
520 "entry with this key (%K) already exists",
521 &entry_key);
522
523 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800524 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700525 pathrelse(&path);
526 return -EBUSY;
527 }
528 }
529
530 /* perform the insertion of the entry that we have prepared */
531 retval =
532 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
533 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800535 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700536 if (retval) {
537 reiserfs_check_path(&path);
538 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541 dir->i_size += paste_size;
542 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
543 if (!S_ISDIR(inode->i_mode) && visible)
544 // reiserfs_mkdir or reiserfs_rename will do that by itself
545 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700547 reiserfs_check_path(&path);
548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551/* quota utility function, call if you've had to abort after calling
552** new_inode_init, and have not called reiserfs_new_inode yet.
553** This should only be called on inodes that do not have stat data
554** inserted into the tree yet.
555*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700556static int drop_new_inode(struct inode *inode)
557{
558 DQUOT_DROP(inode);
559 make_bad_inode(inode);
560 inode->i_flags |= S_NOQUOTA;
561 iput(inode);
562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/* utility function that does setup for reiserfs_new_inode.
566** DQUOT_INIT needs lots of credits so it's better to have it
567** outside of a transaction, so we had to pull some bits of
568** reiserfs_new_inode out into this func.
569*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 /* the quota init calls have to know who to charge the quota to, so
574 ** we have to set uid and gid here
575 */
David Howells414cb202008-11-14 10:39:01 +1100576 inode->i_uid = current_fsuid();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700577 inode->i_mode = mode;
Jan Kara1b0a74d2005-08-13 13:15:34 +0200578 /* Make inode invalid - just in case we are going to drop it before
579 * the initialization happens */
580 INODE_PKEY(inode)->k_objectid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700582 if (dir->i_mode & S_ISGID) {
583 inode->i_gid = dir->i_gid;
584 if (S_ISDIR(mode))
585 inode->i_mode |= S_ISGID;
586 } else {
David Howells414cb202008-11-14 10:39:01 +1100587 inode->i_gid = current_fsgid();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588 }
589 DQUOT_INIT(inode);
590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700593static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
594 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 int retval;
597 struct inode *inode;
598 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
599 int jbegin_count =
600 JOURNAL_PER_BALANCE_CNT * 2 +
601 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
602 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
603 struct reiserfs_transaction_handle th;
604 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700606 if (!(inode = new_inode(dir->i_sb))) {
607 return -ENOMEM;
608 }
609 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700611 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700613 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700615 if (locked)
616 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700618 retval = journal_begin(&th, dir->i_sb, jbegin_count);
619 if (retval) {
620 drop_new_inode(inode);
621 goto out_failed;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700624 retval =
625 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
626 inode);
627 if (retval)
628 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700630 if (locked) {
631 reiserfs_write_unlock_xattrs(dir->i_sb);
632 locked = 0;
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700635 inode->i_op = &reiserfs_file_inode_operations;
636 inode->i_fop = &reiserfs_file_operations;
637 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700639 retval =
640 reiserfs_add_entry(&th, dir, dentry->d_name.name,
641 dentry->d_name.len, inode, 1 /*visible */ );
642 if (retval) {
643 int err;
644 inode->i_nlink--;
645 reiserfs_update_sd(&th, inode);
646 err = journal_end(&th, dir->i_sb, jbegin_count);
647 if (err)
648 retval = err;
649 iput(inode);
650 goto out_failed;
651 }
652 reiserfs_update_inode_transaction(inode);
653 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 d_instantiate(dentry, inode);
656 retval = journal_end(&th, dir->i_sb, jbegin_count);
657
658 out_failed:
659 if (locked)
660 reiserfs_write_unlock_xattrs(dir->i_sb);
661 reiserfs_write_unlock(dir->i_sb);
662 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700665static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
666 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668 int retval;
669 struct inode *inode;
670 struct reiserfs_transaction_handle th;
671 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
672 int jbegin_count =
673 JOURNAL_PER_BALANCE_CNT * 3 +
674 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
675 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
676 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 if (!new_valid_dev(rdev))
679 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 if (!(inode = new_inode(dir->i_sb))) {
682 return -ENOMEM;
683 }
684 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700686 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 if (locked)
691 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 retval = journal_begin(&th, dir->i_sb, jbegin_count);
694 if (retval) {
695 drop_new_inode(inode);
696 goto out_failed;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700699 retval =
700 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
701 inode);
702 if (retval) {
703 goto out_failed;
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700706 if (locked) {
707 reiserfs_write_unlock_xattrs(dir->i_sb);
708 locked = 0;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700711 inode->i_op = &reiserfs_special_inode_operations;
712 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714 //FIXME: needed for block and char devices only
715 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 reiserfs_update_inode_transaction(inode);
718 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 retval =
721 reiserfs_add_entry(&th, dir, dentry->d_name.name,
722 dentry->d_name.len, inode, 1 /*visible */ );
723 if (retval) {
724 int err;
725 inode->i_nlink--;
726 reiserfs_update_sd(&th, inode);
727 err = journal_end(&th, dir->i_sb, jbegin_count);
728 if (err)
729 retval = err;
730 iput(inode);
731 goto out_failed;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 d_instantiate(dentry, inode);
735 retval = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700737 out_failed:
738 if (locked)
739 reiserfs_write_unlock_xattrs(dir->i_sb);
740 reiserfs_write_unlock(dir->i_sb);
741 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700744static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 int retval;
747 struct inode *inode;
748 struct reiserfs_transaction_handle th;
749 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
750 int jbegin_count =
751 JOURNAL_PER_BALANCE_CNT * 3 +
752 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
753 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
754 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
758 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760 mode = S_IFDIR | mode;
761 if (!(inode = new_inode(dir->i_sb))) {
762 return -ENOMEM;
763 }
764 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 reiserfs_write_lock(dir->i_sb);
769 if (locked)
770 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700772 retval = journal_begin(&th, dir->i_sb, jbegin_count);
773 if (retval) {
774 drop_new_inode(inode);
775 goto out_failed;
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778 /* inc the link count now, so another writer doesn't overflow it while
779 ** we sleep later on.
780 */
781 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700783 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
784 old_format_only(dir->i_sb) ?
785 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
786 dentry, inode);
787 if (retval) {
788 dir->i_nlink--;
789 goto out_failed;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700792 if (locked) {
793 reiserfs_write_unlock_xattrs(dir->i_sb);
794 locked = 0;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700797 reiserfs_update_inode_transaction(inode);
798 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700800 inode->i_op = &reiserfs_dir_inode_operations;
801 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700803 // note, _this_ add_entry will not update dir's stat data
804 retval =
805 reiserfs_add_entry(&th, dir, dentry->d_name.name,
806 dentry->d_name.len, inode, 1 /*visible */ );
807 if (retval) {
808 int err;
809 inode->i_nlink = 0;
810 DEC_DIR_INODE_NLINK(dir);
811 reiserfs_update_sd(&th, inode);
812 err = journal_end(&th, dir->i_sb, jbegin_count);
813 if (err)
814 retval = err;
815 iput(inode);
816 goto out_failed;
817 }
818 // the above add_entry did not update dir's stat data
819 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700821 d_instantiate(dentry, inode);
822 retval = journal_end(&th, dir->i_sb, jbegin_count);
823 out_failed:
824 if (locked)
825 reiserfs_write_unlock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 return retval;
828}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700830static inline int reiserfs_empty_dir(struct inode *inode)
831{
832 /* we can cheat because an old format dir cannot have
833 ** EMPTY_DIR_SIZE, and a new format dir cannot have
834 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
835 ** regardless of disk format version, the directory is empty.
836 */
837 if (inode->i_size != EMPTY_DIR_SIZE &&
838 inode->i_size != EMPTY_DIR_SIZE_V1) {
839 return 0;
840 }
841 return 1;
842}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
845{
846 int retval, err;
847 struct inode *inode;
848 struct reiserfs_transaction_handle th;
849 int jbegin_count;
850 INITIALIZE_PATH(path);
851 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700853 /* we will be doing 2 balancings and update 2 stat data, we change quotas
854 * of the owner of the directory and of the owner of the parent directory.
855 * The quota structure is possibly deleted only on last iput => outside
856 * of this transaction */
857 jbegin_count =
858 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
859 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861 reiserfs_write_lock(dir->i_sb);
862 retval = journal_begin(&th, dir->i_sb, jbegin_count);
863 if (retval)
864 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700866 de.de_gen_number_bit_string = NULL;
867 if ((retval =
868 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
869 &path, &de)) == NAME_NOT_FOUND) {
870 retval = -ENOENT;
871 goto end_rmdir;
872 } else if (retval == IO_ERROR) {
873 retval = -EIO;
874 goto end_rmdir;
875 }
876
877 inode = dentry->d_inode;
878
879 reiserfs_update_inode_transaction(inode);
880 reiserfs_update_inode_transaction(dir);
881
882 if (de.de_objectid != inode->i_ino) {
883 // FIXME: compare key of an object and a key found in the
884 // entry
885 retval = -EIO;
886 goto end_rmdir;
887 }
888 if (!reiserfs_empty_dir(inode)) {
889 retval = -ENOTEMPTY;
890 goto end_rmdir;
891 }
892
893 /* cut entry from dir directory */
894 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
895 0 /*new file size - not used here */ );
896 if (retval < 0)
897 goto end_rmdir;
898
899 if (inode->i_nlink != 2 && inode->i_nlink != 1)
900 reiserfs_warning(inode->i_sb, "%s: empty directory has nlink "
Harvey Harrisonfbe54982008-04-28 02:16:22 -0700901 "!= 2 (%d)", __func__, inode->i_nlink);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700902
Dave Hansence71ec32006-09-30 23:29:06 -0700903 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
905 reiserfs_update_sd(&th, inode);
906
907 DEC_DIR_INODE_NLINK(dir)
908 dir->i_size -= (DEH_SIZE + de.de_entrylen);
909 reiserfs_update_sd(&th, dir);
910
911 /* prevent empty directory from getting lost */
912 add_save_link(&th, inode, 0 /* not truncate */ );
913
914 retval = journal_end(&th, dir->i_sb, jbegin_count);
915 reiserfs_check_path(&path);
916 out_rmdir:
917 reiserfs_write_unlock(dir->i_sb);
918 return retval;
919
920 end_rmdir:
921 /* we must release path, because we did not call
922 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
923 release path if operation was not complete */
924 pathrelse(&path);
925 err = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 reiserfs_write_unlock(dir->i_sb);
927 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700930static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
931{
932 int retval, err;
933 struct inode *inode;
934 struct reiserfs_dir_entry de;
935 INITIALIZE_PATH(path);
936 struct reiserfs_transaction_handle th;
937 int jbegin_count;
938 unsigned long savelink;
939
940 inode = dentry->d_inode;
941
942 /* in this transaction we can be doing at max two balancings and update
943 * two stat datas, we change quotas of the owner of the directory and of
944 * the owner of the parent directory. The quota structure is possibly
945 * deleted only on iput => outside of this transaction */
946 jbegin_count =
947 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
948 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
949
950 reiserfs_write_lock(dir->i_sb);
951 retval = journal_begin(&th, dir->i_sb, jbegin_count);
952 if (retval)
953 goto out_unlink;
954
955 de.de_gen_number_bit_string = NULL;
956 if ((retval =
957 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
958 &path, &de)) == NAME_NOT_FOUND) {
959 retval = -ENOENT;
960 goto end_unlink;
961 } else if (retval == IO_ERROR) {
962 retval = -EIO;
963 goto end_unlink;
964 }
965
966 reiserfs_update_inode_transaction(inode);
967 reiserfs_update_inode_transaction(dir);
968
969 if (de.de_objectid != inode->i_ino) {
970 // FIXME: compare key of an object and a key found in the
971 // entry
972 retval = -EIO;
973 goto end_unlink;
974 }
975
976 if (!inode->i_nlink) {
977 reiserfs_warning(inode->i_sb, "%s: deleting nonexistent file "
Harvey Harrisonfbe54982008-04-28 02:16:22 -0700978 "(%s:%lu), %d", __func__,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 reiserfs_bdevname(inode->i_sb), inode->i_ino,
980 inode->i_nlink);
981 inode->i_nlink = 1;
982 }
983
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700984 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700985
986 /*
987 * we schedule before doing the add_save_link call, save the link
988 * count so we don't race
989 */
990 savelink = inode->i_nlink;
991
992 retval =
993 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
994 0);
995 if (retval < 0) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700996 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700997 goto end_unlink;
998 }
999 inode->i_ctime = CURRENT_TIME_SEC;
1000 reiserfs_update_sd(&th, inode);
1001
1002 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1003 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
1004 reiserfs_update_sd(&th, dir);
1005
1006 if (!savelink)
1007 /* prevent file from getting lost */
1008 add_save_link(&th, inode, 0 /* not truncate */ );
1009
1010 retval = journal_end(&th, dir->i_sb, jbegin_count);
1011 reiserfs_check_path(&path);
1012 reiserfs_write_unlock(dir->i_sb);
1013 return retval;
1014
1015 end_unlink:
1016 pathrelse(&path);
1017 err = journal_end(&th, dir->i_sb, jbegin_count);
1018 reiserfs_check_path(&path);
1019 if (err)
1020 retval = err;
1021 out_unlink:
1022 reiserfs_write_unlock(dir->i_sb);
1023 return retval;
1024}
1025
1026static int reiserfs_symlink(struct inode *parent_dir,
1027 struct dentry *dentry, const char *symname)
1028{
1029 int retval;
1030 struct inode *inode;
1031 char *name;
1032 int item_len;
1033 struct reiserfs_transaction_handle th;
1034 int mode = S_IFLNK | S_IRWXUGO;
1035 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1036 int jbegin_count =
1037 JOURNAL_PER_BALANCE_CNT * 3 +
1038 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1039 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1040
1041 if (!(inode = new_inode(parent_dir->i_sb))) {
1042 return -ENOMEM;
1043 }
1044 new_inode_init(inode, parent_dir, mode);
1045
1046 reiserfs_write_lock(parent_dir->i_sb);
1047 item_len = ROUND_UP(strlen(symname));
1048 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1049 retval = -ENAMETOOLONG;
1050 drop_new_inode(inode);
1051 goto out_failed;
1052 }
1053
Pekka Enbergd739b422006-02-01 03:06:43 -08001054 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 if (!name) {
1056 drop_new_inode(inode);
1057 retval = -ENOMEM;
1058 goto out_failed;
1059 }
1060 memcpy(name, symname, strlen(symname));
1061 padd_item(name, item_len, strlen(symname));
1062
1063 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1064
1065 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1066 if (retval) {
1067 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001068 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001069 goto out_failed;
1070 }
1071
1072 retval =
1073 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1074 dentry, inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001075 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001076 if (retval) { /* reiserfs_new_inode iputs for us */
1077 goto out_failed;
1078 }
1079
1080 reiserfs_update_inode_transaction(inode);
1081 reiserfs_update_inode_transaction(parent_dir);
1082
1083 inode->i_op = &reiserfs_symlink_inode_operations;
1084 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1085
1086 // must be sure this inode is written with this transaction
1087 //
1088 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1089
1090 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1091 dentry->d_name.len, inode, 1 /*visible */ );
1092 if (retval) {
1093 int err;
1094 inode->i_nlink--;
1095 reiserfs_update_sd(&th, inode);
1096 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1097 if (err)
1098 retval = err;
1099 iput(inode);
1100 goto out_failed;
1101 }
1102
1103 d_instantiate(dentry, inode);
1104 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1105 out_failed:
1106 reiserfs_write_unlock(parent_dir->i_sb);
1107 return retval;
1108}
1109
1110static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1111 struct dentry *dentry)
1112{
1113 int retval;
1114 struct inode *inode = old_dentry->d_inode;
1115 struct reiserfs_transaction_handle th;
1116 /* We need blocks for transaction + update of quotas for the owners of the directory */
1117 int jbegin_count =
1118 JOURNAL_PER_BALANCE_CNT * 3 +
1119 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1120
1121 reiserfs_write_lock(dir->i_sb);
1122 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1123 //FIXME: sd_nlink is 32 bit for new files
1124 reiserfs_write_unlock(dir->i_sb);
1125 return -EMLINK;
1126 }
1127 if (inode->i_nlink == 0) {
1128 reiserfs_write_unlock(dir->i_sb);
1129 return -ENOENT;
1130 }
1131
1132 /* inc before scheduling so reiserfs_unlink knows we are here */
Dave Hansend8c76e62006-09-30 23:29:04 -07001133 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134
1135 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1136 if (retval) {
1137 inode->i_nlink--;
1138 reiserfs_write_unlock(dir->i_sb);
1139 return retval;
1140 }
1141
1142 /* create new entry */
1143 retval =
1144 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1145 dentry->d_name.len, inode, 1 /*visible */ );
1146
1147 reiserfs_update_inode_transaction(inode);
1148 reiserfs_update_inode_transaction(dir);
1149
1150 if (retval) {
1151 int err;
1152 inode->i_nlink--;
1153 err = journal_end(&th, dir->i_sb, jbegin_count);
1154 reiserfs_write_unlock(dir->i_sb);
1155 return err ? err : retval;
1156 }
1157
1158 inode->i_ctime = CURRENT_TIME_SEC;
1159 reiserfs_update_sd(&th, inode);
1160
1161 atomic_inc(&inode->i_count);
1162 d_instantiate(dentry, inode);
1163 retval = journal_end(&th, dir->i_sb, jbegin_count);
1164 reiserfs_write_unlock(dir->i_sb);
1165 return retval;
1166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168// de contains information pointing to an entry which
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001169static int de_still_valid(const char *name, int len,
1170 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172 struct reiserfs_dir_entry tmp = *de;
1173
1174 // recalculate pointer to name and name length
1175 set_de_name_and_namelen(&tmp);
1176 // FIXME: could check more
1177 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1178 return 0;
1179 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001182static int entry_points_to_object(const char *name, int len,
1183 struct reiserfs_dir_entry *de,
1184 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186 if (!de_still_valid(name, len, de))
1187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001189 if (inode) {
1190 if (!de_visible(de->de_deh + de->de_entry_num))
1191 reiserfs_panic(NULL,
1192 "vs-7042: entry_points_to_object: entry must be visible");
1193 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 /* this must be added hidden entry */
1197 if (de_visible(de->de_deh + de->de_entry_num))
1198 reiserfs_panic(NULL,
1199 "vs-7043: entry_points_to_object: entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001201 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1206 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001208 /* JDM These operations are endian safe - both are le */
1209 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1210 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
1214 * process, that is going to call fix_nodes/do_balance must hold only
1215 * one path. If it holds 2 or more, it can get into endless waiting in
1216 * get_empty_nodes or its clones
1217 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1219 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001221 int retval;
1222 INITIALIZE_PATH(old_entry_path);
1223 INITIALIZE_PATH(new_entry_path);
1224 INITIALIZE_PATH(dot_dot_entry_path);
1225 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1226 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1227 struct inode *old_inode, *new_dentry_inode;
1228 struct reiserfs_transaction_handle th;
1229 int jbegin_count;
1230 umode_t old_inode_mode;
1231 unsigned long savelink = 1;
1232 struct timespec ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001234 /* three balancings: (1) old name removal, (2) new name insertion
1235 and (3) maybe "save" link insertion
1236 stat data updates: (1) old directory,
1237 (2) new directory and (3) maybe old object stat data (when it is
1238 directory) and (4) maybe stat data of object to which new entry
1239 pointed initially and (5) maybe block containing ".." of
1240 renamed directory
1241 quota updates: two parent directories */
1242 jbegin_count =
1243 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1244 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 old_inode = old_dentry->d_inode;
1247 new_dentry_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001249 // make sure, that oldname still exists and points to an object we
1250 // are going to rename
1251 old_de.de_gen_number_bit_string = NULL;
1252 reiserfs_write_lock(old_dir->i_sb);
1253 retval =
1254 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1255 old_dentry->d_name.len, &old_entry_path,
1256 &old_de);
1257 pathrelse(&old_entry_path);
1258 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 reiserfs_write_unlock(old_dir->i_sb);
1260 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001262
1263 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1264 reiserfs_write_unlock(old_dir->i_sb);
1265 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001267
1268 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (S_ISDIR(old_inode_mode)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001270 // make sure, that directory being renamed has correct ".."
1271 // and that its new parent directory has not too many links
1272 // already
1273
1274 if (new_dentry_inode) {
1275 if (!reiserfs_empty_dir(new_dentry_inode)) {
1276 reiserfs_write_unlock(old_dir->i_sb);
1277 return -ENOTEMPTY;
1278 }
1279 }
1280
1281 /* directory is renamed, its parent directory will be changed,
1282 ** so find ".." entry
1283 */
1284 dot_dot_de.de_gen_number_bit_string = NULL;
1285 retval =
1286 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1287 &dot_dot_de);
1288 pathrelse(&dot_dot_entry_path);
1289 if (retval != NAME_FOUND) {
1290 reiserfs_write_unlock(old_dir->i_sb);
1291 return -EIO;
1292 }
1293
1294 /* inode number of .. must equal old_dir->i_ino */
1295 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1296 reiserfs_write_unlock(old_dir->i_sb);
1297 return -EIO;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001301 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1302 if (retval) {
1303 reiserfs_write_unlock(old_dir->i_sb);
1304 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001307 /* add new entry (or find the existing one) */
1308 retval =
1309 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1310 new_dentry->d_name.len, old_inode, 0);
1311 if (retval == -EEXIST) {
1312 if (!new_dentry_inode) {
1313 reiserfs_panic(old_dir->i_sb,
1314 "vs-7050: new entry is found, new inode == 0\n");
1315 }
1316 } else if (retval) {
1317 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1318 reiserfs_write_unlock(old_dir->i_sb);
1319 return err ? err : retval;
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001322 reiserfs_update_inode_transaction(old_dir);
1323 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001325 /* this makes it so an fsync on an open fd for the old name will
1326 ** commit the rename operation
1327 */
1328 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001330 if (new_dentry_inode)
1331 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001333 while (1) {
1334 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1335 if ((retval =
1336 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1337 &old_entry_path,
1338 &old_de)) != NAME_FOUND) {
1339 pathrelse(&old_entry_path);
1340 journal_end(&th, old_dir->i_sb, jbegin_count);
1341 reiserfs_write_unlock(old_dir->i_sb);
1342 return -EIO;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001345 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001347 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1348
1349 // look for new name by reiserfs_find_entry
1350 new_de.de_gen_number_bit_string = NULL;
1351 retval =
1352 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1353 new_dentry->d_name.len, &new_entry_path,
1354 &new_de);
1355 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1356 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1357 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1358 pathrelse(&new_entry_path);
1359 pathrelse(&old_entry_path);
1360 journal_end(&th, old_dir->i_sb, jbegin_count);
1361 reiserfs_write_unlock(old_dir->i_sb);
1362 return -EIO;
1363 }
1364
1365 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1366
1367 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1368
1369 if (S_ISDIR(old_inode->i_mode)) {
1370 if ((retval =
1371 search_by_entry_key(new_dir->i_sb,
1372 &dot_dot_de.de_entry_key,
1373 &dot_dot_entry_path,
1374 &dot_dot_de)) != NAME_FOUND) {
1375 pathrelse(&dot_dot_entry_path);
1376 pathrelse(&new_entry_path);
1377 pathrelse(&old_entry_path);
1378 journal_end(&th, old_dir->i_sb, jbegin_count);
1379 reiserfs_write_unlock(old_dir->i_sb);
1380 return -EIO;
1381 }
1382 copy_item_head(&dot_dot_ih,
1383 get_ih(&dot_dot_entry_path));
1384 // node containing ".." gets into transaction
1385 reiserfs_prepare_for_journal(old_inode->i_sb,
1386 dot_dot_de.de_bh, 1);
1387 }
1388 /* we should check seals here, not do
1389 this stuff, yes? Then, having
1390 gathered everything into RAM we
1391 should lock the buffers, yes? -Hans */
1392 /* probably. our rename needs to hold more
1393 ** than one path at once. The seals would
1394 ** have to be written to deal with multi-path
1395 ** issues -chris
1396 */
1397 /* sanity checking before doing the rename - avoid races many
1398 ** of the above checks could have scheduled. We have to be
1399 ** sure our items haven't been shifted by another process.
1400 */
1401 if (item_moved(&new_entry_ih, &new_entry_path) ||
1402 !entry_points_to_object(new_dentry->d_name.name,
1403 new_dentry->d_name.len,
1404 &new_de, new_dentry_inode) ||
1405 item_moved(&old_entry_ih, &old_entry_path) ||
1406 !entry_points_to_object(old_dentry->d_name.name,
1407 old_dentry->d_name.len,
1408 &old_de, old_inode)) {
1409 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1410 new_de.de_bh);
1411 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1412 old_de.de_bh);
1413 if (S_ISDIR(old_inode_mode))
1414 reiserfs_restore_prepared_buffer(old_inode->
1415 i_sb,
1416 dot_dot_de.
1417 de_bh);
1418 continue;
1419 }
1420 if (S_ISDIR(old_inode_mode)) {
1421 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1422 !entry_points_to_object("..", 2, &dot_dot_de,
1423 old_dir)) {
1424 reiserfs_restore_prepared_buffer(old_inode->
1425 i_sb,
1426 old_de.de_bh);
1427 reiserfs_restore_prepared_buffer(old_inode->
1428 i_sb,
1429 new_de.de_bh);
1430 reiserfs_restore_prepared_buffer(old_inode->
1431 i_sb,
1432 dot_dot_de.
1433 de_bh);
1434 continue;
1435 }
1436 }
1437
1438 RFALSE(S_ISDIR(old_inode_mode) &&
1439 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1440
1441 break;
1442 }
1443
1444 /* ok, all the changes can be done in one fell swoop when we
1445 have claimed all the buffers needed. */
1446
1447 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1448 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1449 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1450
1451 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1452 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1453 ctime = CURRENT_TIME_SEC;
1454 old_dir->i_ctime = old_dir->i_mtime = ctime;
1455 new_dir->i_ctime = new_dir->i_mtime = ctime;
1456 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1457 renamed object */
1458 old_inode->i_ctime = ctime;
1459
1460 if (new_dentry_inode) {
1461 // adjust link number of the victim
1462 if (S_ISDIR(new_dentry_inode->i_mode)) {
Dave Hansence71ec32006-09-30 23:29:06 -07001463 clear_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001464 } else {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001465 drop_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001466 }
1467 new_dentry_inode->i_ctime = ctime;
1468 savelink = new_dentry_inode->i_nlink;
1469 }
1470
1471 if (S_ISDIR(old_inode_mode)) {
1472 // adjust ".." of renamed directory
1473 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1474 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1475
1476 if (!new_dentry_inode)
1477 /* there (in new_dir) was no directory, so it got new link
1478 (".." of renamed directory) */
1479 INC_DIR_INODE_NLINK(new_dir);
1480
1481 /* old directory lost one link - ".. " of renamed directory */
1482 DEC_DIR_INODE_NLINK(old_dir);
1483 }
1484 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1485 pathrelse(&new_entry_path);
1486 pathrelse(&dot_dot_entry_path);
1487
1488 // FIXME: this reiserfs_cut_from_item's return value may screw up
1489 // anybody, but it will panic if will not be able to find the
1490 // entry. This needs one more clean up
1491 if (reiserfs_cut_from_item
1492 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1493 0) < 0)
1494 reiserfs_warning(old_dir->i_sb,
1495 "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1496
1497 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1498
1499 reiserfs_update_sd(&th, old_dir);
1500 reiserfs_update_sd(&th, new_dir);
1501 reiserfs_update_sd(&th, old_inode);
1502
1503 if (new_dentry_inode) {
1504 if (savelink == 0)
1505 add_save_link(&th, new_dentry_inode,
1506 0 /* not truncate */ );
1507 reiserfs_update_sd(&th, new_dentry_inode);
1508 }
1509
1510 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1511 reiserfs_write_unlock(old_dir->i_sb);
1512 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/*
1516 * directories can handle most operations...
1517 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001518const struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 //&reiserfs_dir_operations, /* default_file_ops */
1520 .create = reiserfs_create,
1521 .lookup = reiserfs_lookup,
1522 .link = reiserfs_link,
1523 .unlink = reiserfs_unlink,
1524 .symlink = reiserfs_symlink,
1525 .mkdir = reiserfs_mkdir,
1526 .rmdir = reiserfs_rmdir,
1527 .mknod = reiserfs_mknod,
1528 .rename = reiserfs_rename,
1529 .setattr = reiserfs_setattr,
1530 .setxattr = reiserfs_setxattr,
1531 .getxattr = reiserfs_getxattr,
1532 .listxattr = reiserfs_listxattr,
1533 .removexattr = reiserfs_removexattr,
1534 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535};
1536
1537/*
1538 * symlink operations.. same as page_symlink_inode_operations, with xattr
1539 * stuff added
1540 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001541const struct inode_operations reiserfs_symlink_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001542 .readlink = generic_readlink,
1543 .follow_link = page_follow_link_light,
1544 .put_link = page_put_link,
1545 .setattr = reiserfs_setattr,
1546 .setxattr = reiserfs_setxattr,
1547 .getxattr = reiserfs_getxattr,
1548 .listxattr = reiserfs_listxattr,
1549 .removexattr = reiserfs_removexattr,
1550 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552};
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * special file operations.. just xattr/acl stuff
1556 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001557const struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001558 .setattr = reiserfs_setattr,
1559 .setxattr = reiserfs_setxattr,
1560 .getxattr = reiserfs_getxattr,
1561 .listxattr = reiserfs_listxattr,
1562 .removexattr = reiserfs_removexattr,
1563 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565};