blob: abde1edc2235605c9a4a9680cfdcafbf830f6a12 [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>
19#include <linux/smp_lock.h>
20#include <linux/quotaops.h>
21
Dave Hansend8c76e62006-09-30 23:29:04 -070022#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 -070023#define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25// directory item contains array of entry headers. This performs
26// binary search through that array
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070027static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070029 struct item_head *ih = de->de_ih;
30 struct reiserfs_de_head *deh = de->de_deh;
31 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070033 lbound = 0;
34 rbound = I_ENTRY_COUNT(ih) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070036 for (j = (rbound + lbound) / 2; lbound <= rbound;
37 j = (rbound + lbound) / 2) {
38 if (off < deh_offset(deh + j)) {
39 rbound = j - 1;
40 continue;
41 }
42 if (off > deh_offset(deh + j)) {
43 lbound = j + 1;
44 continue;
45 }
46 // this is not name found, but matched third key component
47 de->de_entry_num = j;
48 return NAME_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051 de->de_entry_num = lbound;
52 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055// comment? maybe something like set de to point to what the path points to?
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070056static inline void set_de_item_location(struct reiserfs_dir_entry *de,
57 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070059 de->de_bh = get_last_bh(path);
60 de->de_ih = get_ih(path);
61 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
62 de->de_item_num = PATH_LAST_POSITION(path);
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65// de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070068 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Sesterhenn14a61442006-10-03 23:36:38 +020070 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
73 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
74 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh);
75 if (de->de_name[de->de_namelen - 1] == 0)
76 de->de_namelen = strlen(de->de_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079// what entry points to
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070080static inline void set_de_object_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Eric Sesterhenn14a61442006-10-03 23:36:38 +020082 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num]));
84 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Eric Sesterhenn14a61442006-10-03 23:36:38 +020091 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 /* store key of the found entry */
94 de->de_entry_key.version = KEY_FORMAT_3_5;
95 de->de_entry_key.on_disk_key.k_dir_id =
96 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
97 de->de_entry_key.on_disk_key.k_objectid =
98 le32_to_cpu(de->de_ih->ih_key.k_objectid);
99 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh));
100 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* We assign a key to each directory item, and place multiple entries
104in a single directory item. A directory item has a key equal to the
105key of the first directory entry in it.
106
107This function first calls search_by_key, then, if item whose first
108entry matches is not found it looks for the entry inside directory
109item found by search_by_key. Fills the path to the entry, and to the
110entry position in the item
111
112*/
113
114/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700115int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
116 struct path *path, struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120 retval = search_item(sb, key, path);
121 switch (retval) {
122 case ITEM_NOT_FOUND:
123 if (!PATH_LAST_POSITION(path)) {
124 reiserfs_warning(sb,
125 "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
126 pathrelse(path);
127 return IO_ERROR;
128 }
129 PATH_LAST_POSITION(path)--;
130
131 case ITEM_FOUND:
132 break;
133
134 case IO_ERROR:
135 return retval;
136
137 default:
138 pathrelse(path);
139 reiserfs_warning(sb,
140 "vs-7002: search_by_entry_key: no path to here");
141 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 if (!is_direntry_le_ih(de->de_ih) ||
148 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
149 print_block(de->de_bh, 0, -1, -1);
150 reiserfs_panic(sb,
151 "vs-7005: search_by_entry_key: found item %h is not directory item or "
152 "does not belong to the same directory as key %K",
153 de->de_ih, key);
154 }
155#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 /* binary search in directory item by third componen t of the
158 key. sets de->de_entry_num of de */
159 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
160 path->pos_in_item = de->de_entry_num;
161 if (retval != NAME_NOT_FOUND) {
162 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
163 set_de_name_and_namelen(de);
164 set_de_object_key(de);
165 }
166 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
170
171/* The third component is hashed, and you can choose from more than
172 one hash function. Per directory hashes are not yet implemented
173 but are thought about. This function should be moved to hashes.c
174 Jedi, please do so. -Hans */
175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176static __u32 get_third_component(struct super_block *s,
177 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181 if (!len || (len == 1 && name[0] == '.'))
182 return DOT_OFFSET;
183 if (len == 2 && name[0] == '.' && name[1] == '.')
184 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 // take bits from 7-th to 30-th including both bounds
189 res = GET_HASH_VALUE(res);
190 if (res == 0)
191 // needed to have no names before "." and ".." those have hash
192 // value == 0 and generation conters 1 and 2 accordingly
193 res = 128;
194 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197static int reiserfs_match(struct reiserfs_dir_entry *de,
198 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700202 if ((namelen == de->de_namelen) &&
203 !memcmp(de->de_name, name, de->de_namelen))
204 retval =
205 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
206 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
212
213 /* used when hash collisions exist */
214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215static int linear_search_in_dir_item(struct cpu_key *key,
216 struct reiserfs_dir_entry *de,
217 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 struct reiserfs_de_head *deh = de->de_deh;
220 int retval;
221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225 if (i == I_ENTRY_COUNT(de->de_ih) ||
226 GET_HASH_VALUE(deh_offset(deh + i)) !=
227 GET_HASH_VALUE(cpu_key_k_offset(key))) {
228 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
232 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700236 for (; i >= 0; i--, deh--) {
237 if (GET_HASH_VALUE(deh_offset(deh)) !=
238 GET_HASH_VALUE(cpu_key_k_offset(key))) {
239 // hash value does not match, no need to check whole name
240 return NAME_NOT_FOUND;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700243 /* mark, that this generation number is used */
244 if (de->de_gen_number_bit_string)
245 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500246 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 // calculate pointer to name and namelen
249 de->de_entry_num = i;
250 set_de_name_and_namelen(de);
251
252 if ((retval =
253 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
254 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
255
256 // key of pointed object
257 set_de_object_key(de);
258
259 store_de_entry_key(de);
260
261 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
262 return retval;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700266 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
267 /* we have reached left most entry in the node. In common we
268 have to go to the left neighbor, but if generation counter
269 is 0 already, we know for sure, that there is no name with
270 the same hash value */
271 // FIXME: this work correctly only because hash value can not
272 // be 0. Btw, in case of Yura's hash it is probably possible,
273 // so, this is a bug
274 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700276 RFALSE(de->de_item_num,
277 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700279 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282// may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
283// FIXME: should add something like IOERROR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700284static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
285 struct path *path_to_entry,
286 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 struct cpu_key key_to_search;
289 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
292 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 /* we will search for this key in the tree */
295 make_cpu_key(&key_to_search, dir,
296 get_third_component(dir->i_sb, name, namelen),
297 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700299 while (1) {
300 retval =
301 search_by_entry_key(dir->i_sb, &key_to_search,
302 path_to_entry, de);
303 if (retval == IO_ERROR) {
304 reiserfs_warning(dir->i_sb, "zam-7001: io error in %s",
305 __FUNCTION__);
306 return IO_ERROR;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700309 /* compare names for all entries having given hash value */
310 retval =
311 linear_search_in_dir_item(&key_to_search, de, name,
312 namelen);
313 if (retval != GOTO_PREVIOUS_ITEM) {
314 /* there is no need to scan directory anymore. Given entry found or does not exist */
315 path_to_entry->pos_in_item = de->de_entry_num;
316 return retval;
317 }
318
319 /* there is left neighboring item of this directory and given entry can be there */
320 set_cpu_key_k_offset(&key_to_search,
321 le_ih_k_offset(de->de_ih) - 1);
322 pathrelse(path_to_entry);
323
324 } /* while (1) */
325}
326
327static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
328 struct nameidata *nd)
329{
330 int retval;
331 struct inode *inode = NULL;
332 struct reiserfs_dir_entry de;
333 INITIALIZE_PATH(path_to_entry);
334
335 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
336 return ERR_PTR(-ENAMETOOLONG);
337
338 reiserfs_write_lock(dir->i_sb);
339 de.de_gen_number_bit_string = NULL;
340 retval =
341 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
342 &path_to_entry, &de);
343 pathrelse(&path_to_entry);
344 if (retval == NAME_FOUND) {
345 /* Hide the .reiserfs_priv directory */
346 if (reiserfs_xattrs(dir->i_sb) &&
347 !old_format_only(dir->i_sb) &&
348 REISERFS_SB(dir->i_sb)->priv_root &&
349 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
350 de.de_objectid ==
351 le32_to_cpu(INODE_PKEY
352 (REISERFS_SB(dir->i_sb)->priv_root->d_inode)->
353 k_objectid)) {
354 reiserfs_write_unlock(dir->i_sb);
355 return ERR_PTR(-EACCES);
356 }
357
358 inode =
359 reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
360 if (!inode || IS_ERR(inode)) {
361 reiserfs_write_unlock(dir->i_sb);
362 return ERR_PTR(-EACCES);
363 }
364
365 /* Propogate the priv_object flag so we know we're in the priv tree */
366 if (is_reiserfs_priv_object(dir))
367 reiserfs_mark_inode_private(inode);
368 }
369 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700371 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Pekka Enbergd9975d62006-01-14 13:21:09 -0800374 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
378** looks up the dentry of the parent directory for child.
379** taken from ext2_get_parent
380*/
381struct dentry *reiserfs_get_parent(struct dentry *child)
382{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700383 int retval;
384 struct inode *inode = NULL;
385 struct reiserfs_dir_entry de;
386 INITIALIZE_PATH(path_to_entry);
387 struct dentry *parent;
388 struct inode *dir = child->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 if (dir->i_nlink == 0) {
391 return ERR_PTR(-ENOENT);
392 }
393 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700395 reiserfs_write_lock(dir->i_sb);
396 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
397 pathrelse(&path_to_entry);
398 if (retval != NAME_FOUND) {
399 reiserfs_write_unlock(dir->i_sb);
400 return ERR_PTR(-ENOENT);
401 }
402 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700405 if (!inode || IS_ERR(inode)) {
406 return ERR_PTR(-EACCES);
407 }
408 parent = d_alloc_anon(inode);
409 if (!parent) {
410 iput(inode);
411 parent = ERR_PTR(-ENOMEM);
412 }
413 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/* add entry to the directory (entry can be hidden).
417
418insert definition of when hidden directories are used here -Hans
419
420 Does not mark dir inode dirty, do it after successesfull call to it */
421
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700422static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
423 struct inode *dir, const char *name, int namelen,
424 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 struct cpu_key entry_key;
427 struct reiserfs_de_head *deh;
428 INITIALIZE_PATH(path);
429 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500430 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700431 int gen_number;
432 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
433 if we create file with short name */
434 char *buffer;
435 int buflen, paste_size;
436 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700440 /* cannot allow items to be added into a busy deleted directory */
441 if (!namelen)
442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
445 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 /* each entry has unique key. compose it */
448 make_cpu_key(&entry_key, dir,
449 get_third_component(dir->i_sb, name, namelen),
450 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700452 /* get memory for composing the entry */
453 buflen = DEH_SIZE + ROUND_UP(namelen);
454 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800455 buffer = kmalloc(buflen, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 if (buffer == 0)
457 return -ENOMEM;
458 } else
459 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 paste_size =
462 (get_inode_sd_version(dir) ==
463 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
466 deh = (struct reiserfs_de_head *)buffer;
467 deh->deh_location = 0; /* JDM Endian safe if 0 */
468 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
469 deh->deh_state = 0; /* JDM Endian safe if 0 */
470 /* put key (ino analog) to de */
471 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
472 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700474 /* copy name */
475 memcpy((char *)(deh + 1), name, namelen);
476 /* padd by 0s to the 4 byte boundary */
477 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700479 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
480 mark_de_without_sd(deh);
481 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 /* find the proper place for the new entry */
484 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500485 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
487 if (retval != NAME_NOT_FOUND) {
488 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800489 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490 pathrelse(&path);
491
492 if (retval == IO_ERROR) {
493 return -EIO;
494 }
495
496 if (retval != NAME_FOUND) {
497 reiserfs_warning(dir->i_sb,
498 "zam-7002:%s: \"reiserfs_find_entry\" "
499 "has returned unexpected value (%d)",
500 __FUNCTION__, retval);
501 }
502
503 return -EEXIST;
504 }
505
506 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500507 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 MAX_GENERATION_NUMBER + 1);
509 if (gen_number > MAX_GENERATION_NUMBER) {
510 /* there is no free generation number */
511 reiserfs_warning(dir->i_sb,
512 "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
513 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800514 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700515 pathrelse(&path);
516 return -EBUSY;
517 }
518 /* adjust offset of directory enrty */
519 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
520 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
521
522 /* update max-hash-collisions counter in reiserfs_sb_info */
523 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
524
525 if (gen_number != 0) { /* we need to re-search for the insertion point */
526 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
527 NAME_NOT_FOUND) {
528 reiserfs_warning(dir->i_sb,
529 "vs-7032: reiserfs_add_entry: "
530 "entry with this key (%K) already exists",
531 &entry_key);
532
533 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800534 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535 pathrelse(&path);
536 return -EBUSY;
537 }
538 }
539
540 /* perform the insertion of the entry that we have prepared */
541 retval =
542 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
543 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800545 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 if (retval) {
547 reiserfs_check_path(&path);
548 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700551 dir->i_size += paste_size;
552 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
553 if (!S_ISDIR(inode->i_mode) && visible)
554 // reiserfs_mkdir or reiserfs_rename will do that by itself
555 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 reiserfs_check_path(&path);
558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/* quota utility function, call if you've had to abort after calling
562** new_inode_init, and have not called reiserfs_new_inode yet.
563** This should only be called on inodes that do not have stat data
564** inserted into the tree yet.
565*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566static int drop_new_inode(struct inode *inode)
567{
568 DQUOT_DROP(inode);
569 make_bad_inode(inode);
570 inode->i_flags |= S_NOQUOTA;
571 iput(inode);
572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/* utility function that does setup for reiserfs_new_inode.
576** DQUOT_INIT needs lots of credits so it's better to have it
577** outside of a transaction, so we had to pull some bits of
578** reiserfs_new_inode out into this func.
579*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700580static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
581{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700583 /* the quota init calls have to know who to charge the quota to, so
584 ** we have to set uid and gid here
585 */
586 inode->i_uid = current->fsuid;
587 inode->i_mode = mode;
Jan Kara1b0a74d2005-08-13 13:15:34 +0200588 /* Make inode invalid - just in case we are going to drop it before
589 * the initialization happens */
590 INODE_PKEY(inode)->k_objectid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700592 if (dir->i_mode & S_ISGID) {
593 inode->i_gid = dir->i_gid;
594 if (S_ISDIR(mode))
595 inode->i_mode |= S_ISGID;
596 } else {
597 inode->i_gid = current->fsgid;
598 }
599 DQUOT_INIT(inode);
600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700603static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
604 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700606 int retval;
607 struct inode *inode;
608 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
609 int jbegin_count =
610 JOURNAL_PER_BALANCE_CNT * 2 +
611 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
612 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
613 struct reiserfs_transaction_handle th;
614 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700616 if (!(inode = new_inode(dir->i_sb))) {
617 return -ENOMEM;
618 }
619 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700621 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625 if (locked)
626 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628 retval = journal_begin(&th, dir->i_sb, jbegin_count);
629 if (retval) {
630 drop_new_inode(inode);
631 goto out_failed;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700634 retval =
635 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
636 inode);
637 if (retval)
638 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700640 if (locked) {
641 reiserfs_write_unlock_xattrs(dir->i_sb);
642 locked = 0;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 inode->i_op = &reiserfs_file_inode_operations;
646 inode->i_fop = &reiserfs_file_operations;
647 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 retval =
650 reiserfs_add_entry(&th, dir, dentry->d_name.name,
651 dentry->d_name.len, inode, 1 /*visible */ );
652 if (retval) {
653 int err;
654 inode->i_nlink--;
655 reiserfs_update_sd(&th, inode);
656 err = journal_end(&th, dir->i_sb, jbegin_count);
657 if (err)
658 retval = err;
659 iput(inode);
660 goto out_failed;
661 }
662 reiserfs_update_inode_transaction(inode);
663 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700665 d_instantiate(dentry, inode);
666 retval = journal_end(&th, dir->i_sb, jbegin_count);
667
668 out_failed:
669 if (locked)
670 reiserfs_write_unlock_xattrs(dir->i_sb);
671 reiserfs_write_unlock(dir->i_sb);
672 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700675static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
676 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 int retval;
679 struct inode *inode;
680 struct reiserfs_transaction_handle th;
681 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
682 int jbegin_count =
683 JOURNAL_PER_BALANCE_CNT * 3 +
684 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
685 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
686 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 if (!new_valid_dev(rdev))
689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691 if (!(inode = new_inode(dir->i_sb))) {
692 return -ENOMEM;
693 }
694 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 if (locked)
701 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700703 retval = journal_begin(&th, dir->i_sb, jbegin_count);
704 if (retval) {
705 drop_new_inode(inode);
706 goto out_failed;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700709 retval =
710 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
711 inode);
712 if (retval) {
713 goto out_failed;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 if (locked) {
717 reiserfs_write_unlock_xattrs(dir->i_sb);
718 locked = 0;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700721 inode->i_op = &reiserfs_special_inode_operations;
722 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700724 //FIXME: needed for block and char devices only
725 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700727 reiserfs_update_inode_transaction(inode);
728 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700730 retval =
731 reiserfs_add_entry(&th, dir, dentry->d_name.name,
732 dentry->d_name.len, inode, 1 /*visible */ );
733 if (retval) {
734 int err;
735 inode->i_nlink--;
736 reiserfs_update_sd(&th, inode);
737 err = journal_end(&th, dir->i_sb, jbegin_count);
738 if (err)
739 retval = err;
740 iput(inode);
741 goto out_failed;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700744 d_instantiate(dentry, inode);
745 retval = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700747 out_failed:
748 if (locked)
749 reiserfs_write_unlock_xattrs(dir->i_sb);
750 reiserfs_write_unlock(dir->i_sb);
751 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700754static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756 int retval;
757 struct inode *inode;
758 struct reiserfs_transaction_handle th;
759 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
760 int jbegin_count =
761 JOURNAL_PER_BALANCE_CNT * 3 +
762 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
763 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
764 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700767 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
768 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 mode = S_IFDIR | mode;
771 if (!(inode = new_inode(dir->i_sb))) {
772 return -ENOMEM;
773 }
774 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700776 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778 reiserfs_write_lock(dir->i_sb);
779 if (locked)
780 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700782 retval = journal_begin(&th, dir->i_sb, jbegin_count);
783 if (retval) {
784 drop_new_inode(inode);
785 goto out_failed;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788 /* inc the link count now, so another writer doesn't overflow it while
789 ** we sleep later on.
790 */
791 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
794 old_format_only(dir->i_sb) ?
795 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
796 dentry, inode);
797 if (retval) {
798 dir->i_nlink--;
799 goto out_failed;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700802 if (locked) {
803 reiserfs_write_unlock_xattrs(dir->i_sb);
804 locked = 0;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700807 reiserfs_update_inode_transaction(inode);
808 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700810 inode->i_op = &reiserfs_dir_inode_operations;
811 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700813 // note, _this_ add_entry will not update dir's stat data
814 retval =
815 reiserfs_add_entry(&th, dir, dentry->d_name.name,
816 dentry->d_name.len, inode, 1 /*visible */ );
817 if (retval) {
818 int err;
819 inode->i_nlink = 0;
820 DEC_DIR_INODE_NLINK(dir);
821 reiserfs_update_sd(&th, inode);
822 err = journal_end(&th, dir->i_sb, jbegin_count);
823 if (err)
824 retval = err;
825 iput(inode);
826 goto out_failed;
827 }
828 // the above add_entry did not update dir's stat data
829 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 d_instantiate(dentry, inode);
832 retval = journal_end(&th, dir->i_sb, jbegin_count);
833 out_failed:
834 if (locked)
835 reiserfs_write_unlock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 return retval;
838}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840static inline int reiserfs_empty_dir(struct inode *inode)
841{
842 /* we can cheat because an old format dir cannot have
843 ** EMPTY_DIR_SIZE, and a new format dir cannot have
844 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
845 ** regardless of disk format version, the directory is empty.
846 */
847 if (inode->i_size != EMPTY_DIR_SIZE &&
848 inode->i_size != EMPTY_DIR_SIZE_V1) {
849 return 0;
850 }
851 return 1;
852}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700854static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
855{
856 int retval, err;
857 struct inode *inode;
858 struct reiserfs_transaction_handle th;
859 int jbegin_count;
860 INITIALIZE_PATH(path);
861 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700863 /* we will be doing 2 balancings and update 2 stat data, we change quotas
864 * of the owner of the directory and of the owner of the parent directory.
865 * The quota structure is possibly deleted only on last iput => outside
866 * of this transaction */
867 jbegin_count =
868 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
869 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700871 reiserfs_write_lock(dir->i_sb);
872 retval = journal_begin(&th, dir->i_sb, jbegin_count);
873 if (retval)
874 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700876 de.de_gen_number_bit_string = NULL;
877 if ((retval =
878 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
879 &path, &de)) == NAME_NOT_FOUND) {
880 retval = -ENOENT;
881 goto end_rmdir;
882 } else if (retval == IO_ERROR) {
883 retval = -EIO;
884 goto end_rmdir;
885 }
886
887 inode = dentry->d_inode;
888
889 reiserfs_update_inode_transaction(inode);
890 reiserfs_update_inode_transaction(dir);
891
892 if (de.de_objectid != inode->i_ino) {
893 // FIXME: compare key of an object and a key found in the
894 // entry
895 retval = -EIO;
896 goto end_rmdir;
897 }
898 if (!reiserfs_empty_dir(inode)) {
899 retval = -ENOTEMPTY;
900 goto end_rmdir;
901 }
902
903 /* cut entry from dir directory */
904 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
905 0 /*new file size - not used here */ );
906 if (retval < 0)
907 goto end_rmdir;
908
909 if (inode->i_nlink != 2 && inode->i_nlink != 1)
910 reiserfs_warning(inode->i_sb, "%s: empty directory has nlink "
911 "!= 2 (%d)", __FUNCTION__, inode->i_nlink);
912
Dave Hansence71ec32006-09-30 23:29:06 -0700913 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
915 reiserfs_update_sd(&th, inode);
916
917 DEC_DIR_INODE_NLINK(dir)
918 dir->i_size -= (DEH_SIZE + de.de_entrylen);
919 reiserfs_update_sd(&th, dir);
920
921 /* prevent empty directory from getting lost */
922 add_save_link(&th, inode, 0 /* not truncate */ );
923
924 retval = journal_end(&th, dir->i_sb, jbegin_count);
925 reiserfs_check_path(&path);
926 out_rmdir:
927 reiserfs_write_unlock(dir->i_sb);
928 return retval;
929
930 end_rmdir:
931 /* we must release path, because we did not call
932 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
933 release path if operation was not complete */
934 pathrelse(&path);
935 err = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 reiserfs_write_unlock(dir->i_sb);
937 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700940static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
941{
942 int retval, err;
943 struct inode *inode;
944 struct reiserfs_dir_entry de;
945 INITIALIZE_PATH(path);
946 struct reiserfs_transaction_handle th;
947 int jbegin_count;
948 unsigned long savelink;
949
950 inode = dentry->d_inode;
951
952 /* in this transaction we can be doing at max two balancings and update
953 * two stat datas, we change quotas of the owner of the directory and of
954 * the owner of the parent directory. The quota structure is possibly
955 * deleted only on iput => outside of this transaction */
956 jbegin_count =
957 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
958 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
959
960 reiserfs_write_lock(dir->i_sb);
961 retval = journal_begin(&th, dir->i_sb, jbegin_count);
962 if (retval)
963 goto out_unlink;
964
965 de.de_gen_number_bit_string = NULL;
966 if ((retval =
967 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
968 &path, &de)) == NAME_NOT_FOUND) {
969 retval = -ENOENT;
970 goto end_unlink;
971 } else if (retval == IO_ERROR) {
972 retval = -EIO;
973 goto end_unlink;
974 }
975
976 reiserfs_update_inode_transaction(inode);
977 reiserfs_update_inode_transaction(dir);
978
979 if (de.de_objectid != inode->i_ino) {
980 // FIXME: compare key of an object and a key found in the
981 // entry
982 retval = -EIO;
983 goto end_unlink;
984 }
985
986 if (!inode->i_nlink) {
987 reiserfs_warning(inode->i_sb, "%s: deleting nonexistent file "
988 "(%s:%lu), %d", __FUNCTION__,
989 reiserfs_bdevname(inode->i_sb), inode->i_ino,
990 inode->i_nlink);
991 inode->i_nlink = 1;
992 }
993
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700994 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700995
996 /*
997 * we schedule before doing the add_save_link call, save the link
998 * count so we don't race
999 */
1000 savelink = inode->i_nlink;
1001
1002 retval =
1003 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
1004 0);
1005 if (retval < 0) {
Dave Hansend8c76e62006-09-30 23:29:04 -07001006 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001007 goto end_unlink;
1008 }
1009 inode->i_ctime = CURRENT_TIME_SEC;
1010 reiserfs_update_sd(&th, inode);
1011
1012 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1013 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
1014 reiserfs_update_sd(&th, dir);
1015
1016 if (!savelink)
1017 /* prevent file from getting lost */
1018 add_save_link(&th, inode, 0 /* not truncate */ );
1019
1020 retval = journal_end(&th, dir->i_sb, jbegin_count);
1021 reiserfs_check_path(&path);
1022 reiserfs_write_unlock(dir->i_sb);
1023 return retval;
1024
1025 end_unlink:
1026 pathrelse(&path);
1027 err = journal_end(&th, dir->i_sb, jbegin_count);
1028 reiserfs_check_path(&path);
1029 if (err)
1030 retval = err;
1031 out_unlink:
1032 reiserfs_write_unlock(dir->i_sb);
1033 return retval;
1034}
1035
1036static int reiserfs_symlink(struct inode *parent_dir,
1037 struct dentry *dentry, const char *symname)
1038{
1039 int retval;
1040 struct inode *inode;
1041 char *name;
1042 int item_len;
1043 struct reiserfs_transaction_handle th;
1044 int mode = S_IFLNK | S_IRWXUGO;
1045 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1046 int jbegin_count =
1047 JOURNAL_PER_BALANCE_CNT * 3 +
1048 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1049 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1050
1051 if (!(inode = new_inode(parent_dir->i_sb))) {
1052 return -ENOMEM;
1053 }
1054 new_inode_init(inode, parent_dir, mode);
1055
1056 reiserfs_write_lock(parent_dir->i_sb);
1057 item_len = ROUND_UP(strlen(symname));
1058 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1059 retval = -ENAMETOOLONG;
1060 drop_new_inode(inode);
1061 goto out_failed;
1062 }
1063
Pekka Enbergd739b422006-02-01 03:06:43 -08001064 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001065 if (!name) {
1066 drop_new_inode(inode);
1067 retval = -ENOMEM;
1068 goto out_failed;
1069 }
1070 memcpy(name, symname, strlen(symname));
1071 padd_item(name, item_len, strlen(symname));
1072
1073 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1074
1075 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1076 if (retval) {
1077 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001078 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001079 goto out_failed;
1080 }
1081
1082 retval =
1083 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1084 dentry, inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001085 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 if (retval) { /* reiserfs_new_inode iputs for us */
1087 goto out_failed;
1088 }
1089
1090 reiserfs_update_inode_transaction(inode);
1091 reiserfs_update_inode_transaction(parent_dir);
1092
1093 inode->i_op = &reiserfs_symlink_inode_operations;
1094 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1095
1096 // must be sure this inode is written with this transaction
1097 //
1098 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1099
1100 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1101 dentry->d_name.len, inode, 1 /*visible */ );
1102 if (retval) {
1103 int err;
1104 inode->i_nlink--;
1105 reiserfs_update_sd(&th, inode);
1106 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1107 if (err)
1108 retval = err;
1109 iput(inode);
1110 goto out_failed;
1111 }
1112
1113 d_instantiate(dentry, inode);
1114 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1115 out_failed:
1116 reiserfs_write_unlock(parent_dir->i_sb);
1117 return retval;
1118}
1119
1120static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1121 struct dentry *dentry)
1122{
1123 int retval;
1124 struct inode *inode = old_dentry->d_inode;
1125 struct reiserfs_transaction_handle th;
1126 /* We need blocks for transaction + update of quotas for the owners of the directory */
1127 int jbegin_count =
1128 JOURNAL_PER_BALANCE_CNT * 3 +
1129 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1130
1131 reiserfs_write_lock(dir->i_sb);
1132 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1133 //FIXME: sd_nlink is 32 bit for new files
1134 reiserfs_write_unlock(dir->i_sb);
1135 return -EMLINK;
1136 }
1137 if (inode->i_nlink == 0) {
1138 reiserfs_write_unlock(dir->i_sb);
1139 return -ENOENT;
1140 }
1141
1142 /* inc before scheduling so reiserfs_unlink knows we are here */
Dave Hansend8c76e62006-09-30 23:29:04 -07001143 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144
1145 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1146 if (retval) {
1147 inode->i_nlink--;
1148 reiserfs_write_unlock(dir->i_sb);
1149 return retval;
1150 }
1151
1152 /* create new entry */
1153 retval =
1154 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1155 dentry->d_name.len, inode, 1 /*visible */ );
1156
1157 reiserfs_update_inode_transaction(inode);
1158 reiserfs_update_inode_transaction(dir);
1159
1160 if (retval) {
1161 int err;
1162 inode->i_nlink--;
1163 err = journal_end(&th, dir->i_sb, jbegin_count);
1164 reiserfs_write_unlock(dir->i_sb);
1165 return err ? err : retval;
1166 }
1167
1168 inode->i_ctime = CURRENT_TIME_SEC;
1169 reiserfs_update_sd(&th, inode);
1170
1171 atomic_inc(&inode->i_count);
1172 d_instantiate(dentry, inode);
1173 retval = journal_end(&th, dir->i_sb, jbegin_count);
1174 reiserfs_write_unlock(dir->i_sb);
1175 return retval;
1176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178// de contains information pointing to an entry which
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179static int de_still_valid(const char *name, int len,
1180 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001182 struct reiserfs_dir_entry tmp = *de;
1183
1184 // recalculate pointer to name and name length
1185 set_de_name_and_namelen(&tmp);
1186 // FIXME: could check more
1187 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1188 return 0;
1189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001192static int entry_points_to_object(const char *name, int len,
1193 struct reiserfs_dir_entry *de,
1194 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 if (!de_still_valid(name, len, de))
1197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199 if (inode) {
1200 if (!de_visible(de->de_deh + de->de_entry_num))
1201 reiserfs_panic(NULL,
1202 "vs-7042: entry_points_to_object: entry must be visible");
1203 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001206 /* this must be added hidden entry */
1207 if (de_visible(de->de_deh + de->de_entry_num))
1208 reiserfs_panic(NULL,
1209 "vs-7043: entry_points_to_object: entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1216 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 /* JDM These operations are endian safe - both are le */
1219 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1220 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/*
1224 * process, that is going to call fix_nodes/do_balance must hold only
1225 * one path. If it holds 2 or more, it can get into endless waiting in
1226 * get_empty_nodes or its clones
1227 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001228static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1229 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001231 int retval;
1232 INITIALIZE_PATH(old_entry_path);
1233 INITIALIZE_PATH(new_entry_path);
1234 INITIALIZE_PATH(dot_dot_entry_path);
1235 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1236 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1237 struct inode *old_inode, *new_dentry_inode;
1238 struct reiserfs_transaction_handle th;
1239 int jbegin_count;
1240 umode_t old_inode_mode;
1241 unsigned long savelink = 1;
1242 struct timespec ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 /* three balancings: (1) old name removal, (2) new name insertion
1245 and (3) maybe "save" link insertion
1246 stat data updates: (1) old directory,
1247 (2) new directory and (3) maybe old object stat data (when it is
1248 directory) and (4) maybe stat data of object to which new entry
1249 pointed initially and (5) maybe block containing ".." of
1250 renamed directory
1251 quota updates: two parent directories */
1252 jbegin_count =
1253 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1254 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001256 old_inode = old_dentry->d_inode;
1257 new_dentry_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001259 // make sure, that oldname still exists and points to an object we
1260 // are going to rename
1261 old_de.de_gen_number_bit_string = NULL;
1262 reiserfs_write_lock(old_dir->i_sb);
1263 retval =
1264 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1265 old_dentry->d_name.len, &old_entry_path,
1266 &old_de);
1267 pathrelse(&old_entry_path);
1268 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 reiserfs_write_unlock(old_dir->i_sb);
1270 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272
1273 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1274 reiserfs_write_unlock(old_dir->i_sb);
1275 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277
1278 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (S_ISDIR(old_inode_mode)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280 // make sure, that directory being renamed has correct ".."
1281 // and that its new parent directory has not too many links
1282 // already
1283
1284 if (new_dentry_inode) {
1285 if (!reiserfs_empty_dir(new_dentry_inode)) {
1286 reiserfs_write_unlock(old_dir->i_sb);
1287 return -ENOTEMPTY;
1288 }
1289 }
1290
1291 /* directory is renamed, its parent directory will be changed,
1292 ** so find ".." entry
1293 */
1294 dot_dot_de.de_gen_number_bit_string = NULL;
1295 retval =
1296 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1297 &dot_dot_de);
1298 pathrelse(&dot_dot_entry_path);
1299 if (retval != NAME_FOUND) {
1300 reiserfs_write_unlock(old_dir->i_sb);
1301 return -EIO;
1302 }
1303
1304 /* inode number of .. must equal old_dir->i_ino */
1305 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1306 reiserfs_write_unlock(old_dir->i_sb);
1307 return -EIO;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001311 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1312 if (retval) {
1313 reiserfs_write_unlock(old_dir->i_sb);
1314 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 /* add new entry (or find the existing one) */
1318 retval =
1319 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1320 new_dentry->d_name.len, old_inode, 0);
1321 if (retval == -EEXIST) {
1322 if (!new_dentry_inode) {
1323 reiserfs_panic(old_dir->i_sb,
1324 "vs-7050: new entry is found, new inode == 0\n");
1325 }
1326 } else if (retval) {
1327 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1328 reiserfs_write_unlock(old_dir->i_sb);
1329 return err ? err : retval;
1330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001332 reiserfs_update_inode_transaction(old_dir);
1333 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001335 /* this makes it so an fsync on an open fd for the old name will
1336 ** commit the rename operation
1337 */
1338 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001340 if (new_dentry_inode)
1341 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001343 while (1) {
1344 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1345 if ((retval =
1346 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1347 &old_entry_path,
1348 &old_de)) != NAME_FOUND) {
1349 pathrelse(&old_entry_path);
1350 journal_end(&th, old_dir->i_sb, jbegin_count);
1351 reiserfs_write_unlock(old_dir->i_sb);
1352 return -EIO;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001355 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001357 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1358
1359 // look for new name by reiserfs_find_entry
1360 new_de.de_gen_number_bit_string = NULL;
1361 retval =
1362 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1363 new_dentry->d_name.len, &new_entry_path,
1364 &new_de);
1365 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1366 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1367 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1368 pathrelse(&new_entry_path);
1369 pathrelse(&old_entry_path);
1370 journal_end(&th, old_dir->i_sb, jbegin_count);
1371 reiserfs_write_unlock(old_dir->i_sb);
1372 return -EIO;
1373 }
1374
1375 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1376
1377 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1378
1379 if (S_ISDIR(old_inode->i_mode)) {
1380 if ((retval =
1381 search_by_entry_key(new_dir->i_sb,
1382 &dot_dot_de.de_entry_key,
1383 &dot_dot_entry_path,
1384 &dot_dot_de)) != NAME_FOUND) {
1385 pathrelse(&dot_dot_entry_path);
1386 pathrelse(&new_entry_path);
1387 pathrelse(&old_entry_path);
1388 journal_end(&th, old_dir->i_sb, jbegin_count);
1389 reiserfs_write_unlock(old_dir->i_sb);
1390 return -EIO;
1391 }
1392 copy_item_head(&dot_dot_ih,
1393 get_ih(&dot_dot_entry_path));
1394 // node containing ".." gets into transaction
1395 reiserfs_prepare_for_journal(old_inode->i_sb,
1396 dot_dot_de.de_bh, 1);
1397 }
1398 /* we should check seals here, not do
1399 this stuff, yes? Then, having
1400 gathered everything into RAM we
1401 should lock the buffers, yes? -Hans */
1402 /* probably. our rename needs to hold more
1403 ** than one path at once. The seals would
1404 ** have to be written to deal with multi-path
1405 ** issues -chris
1406 */
1407 /* sanity checking before doing the rename - avoid races many
1408 ** of the above checks could have scheduled. We have to be
1409 ** sure our items haven't been shifted by another process.
1410 */
1411 if (item_moved(&new_entry_ih, &new_entry_path) ||
1412 !entry_points_to_object(new_dentry->d_name.name,
1413 new_dentry->d_name.len,
1414 &new_de, new_dentry_inode) ||
1415 item_moved(&old_entry_ih, &old_entry_path) ||
1416 !entry_points_to_object(old_dentry->d_name.name,
1417 old_dentry->d_name.len,
1418 &old_de, old_inode)) {
1419 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1420 new_de.de_bh);
1421 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1422 old_de.de_bh);
1423 if (S_ISDIR(old_inode_mode))
1424 reiserfs_restore_prepared_buffer(old_inode->
1425 i_sb,
1426 dot_dot_de.
1427 de_bh);
1428 continue;
1429 }
1430 if (S_ISDIR(old_inode_mode)) {
1431 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1432 !entry_points_to_object("..", 2, &dot_dot_de,
1433 old_dir)) {
1434 reiserfs_restore_prepared_buffer(old_inode->
1435 i_sb,
1436 old_de.de_bh);
1437 reiserfs_restore_prepared_buffer(old_inode->
1438 i_sb,
1439 new_de.de_bh);
1440 reiserfs_restore_prepared_buffer(old_inode->
1441 i_sb,
1442 dot_dot_de.
1443 de_bh);
1444 continue;
1445 }
1446 }
1447
1448 RFALSE(S_ISDIR(old_inode_mode) &&
1449 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1450
1451 break;
1452 }
1453
1454 /* ok, all the changes can be done in one fell swoop when we
1455 have claimed all the buffers needed. */
1456
1457 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1458 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1459 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1460
1461 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1462 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1463 ctime = CURRENT_TIME_SEC;
1464 old_dir->i_ctime = old_dir->i_mtime = ctime;
1465 new_dir->i_ctime = new_dir->i_mtime = ctime;
1466 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1467 renamed object */
1468 old_inode->i_ctime = ctime;
1469
1470 if (new_dentry_inode) {
1471 // adjust link number of the victim
1472 if (S_ISDIR(new_dentry_inode->i_mode)) {
Dave Hansence71ec32006-09-30 23:29:06 -07001473 clear_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001474 } else {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001475 drop_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001476 }
1477 new_dentry_inode->i_ctime = ctime;
1478 savelink = new_dentry_inode->i_nlink;
1479 }
1480
1481 if (S_ISDIR(old_inode_mode)) {
1482 // adjust ".." of renamed directory
1483 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1484 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1485
1486 if (!new_dentry_inode)
1487 /* there (in new_dir) was no directory, so it got new link
1488 (".." of renamed directory) */
1489 INC_DIR_INODE_NLINK(new_dir);
1490
1491 /* old directory lost one link - ".. " of renamed directory */
1492 DEC_DIR_INODE_NLINK(old_dir);
1493 }
1494 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1495 pathrelse(&new_entry_path);
1496 pathrelse(&dot_dot_entry_path);
1497
1498 // FIXME: this reiserfs_cut_from_item's return value may screw up
1499 // anybody, but it will panic if will not be able to find the
1500 // entry. This needs one more clean up
1501 if (reiserfs_cut_from_item
1502 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1503 0) < 0)
1504 reiserfs_warning(old_dir->i_sb,
1505 "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1506
1507 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1508
1509 reiserfs_update_sd(&th, old_dir);
1510 reiserfs_update_sd(&th, new_dir);
1511 reiserfs_update_sd(&th, old_inode);
1512
1513 if (new_dentry_inode) {
1514 if (savelink == 0)
1515 add_save_link(&th, new_dentry_inode,
1516 0 /* not truncate */ );
1517 reiserfs_update_sd(&th, new_dentry_inode);
1518 }
1519
1520 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1521 reiserfs_write_unlock(old_dir->i_sb);
1522 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525/*
1526 * directories can handle most operations...
1527 */
1528struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 //&reiserfs_dir_operations, /* default_file_ops */
1530 .create = reiserfs_create,
1531 .lookup = reiserfs_lookup,
1532 .link = reiserfs_link,
1533 .unlink = reiserfs_unlink,
1534 .symlink = reiserfs_symlink,
1535 .mkdir = reiserfs_mkdir,
1536 .rmdir = reiserfs_rmdir,
1537 .mknod = reiserfs_mknod,
1538 .rename = reiserfs_rename,
1539 .setattr = reiserfs_setattr,
1540 .setxattr = reiserfs_setxattr,
1541 .getxattr = reiserfs_getxattr,
1542 .listxattr = reiserfs_listxattr,
1543 .removexattr = reiserfs_removexattr,
1544 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545};
1546
1547/*
1548 * symlink operations.. same as page_symlink_inode_operations, with xattr
1549 * stuff added
1550 */
1551struct inode_operations reiserfs_symlink_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001552 .readlink = generic_readlink,
1553 .follow_link = page_follow_link_light,
1554 .put_link = page_put_link,
1555 .setattr = reiserfs_setattr,
1556 .setxattr = reiserfs_setxattr,
1557 .getxattr = reiserfs_getxattr,
1558 .listxattr = reiserfs_listxattr,
1559 .removexattr = reiserfs_removexattr,
1560 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562};
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/*
1565 * special file operations.. just xattr/acl stuff
1566 */
1567struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001568 .setattr = reiserfs_setattr,
1569 .setxattr = reiserfs_setxattr,
1570 .getxattr = reiserfs_getxattr,
1571 .listxattr = reiserfs_listxattr,
1572 .removexattr = reiserfs_removexattr,
1573 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575};