blob: e296ff72a6ccb2bc2e99cfbb97472c7da39f3253 [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
Jeff Mahoney0222e652009-03-30 14:02:44 -0400109entry position in the item
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
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)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400123 reiserfs_error(sb, "vs-7000", "search_by_key "
124 "returned item position == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 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);
Jeff Mahoney0030b642009-03-30 14:02:28 -0400138 reiserfs_error(sb, "vs-7002", "no path to here");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 if (!is_direntry_le_ih(de->de_ih) ||
146 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
147 print_block(de->de_bh, 0, -1, -1);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400148 reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
149 "item or does not belong to the same directory "
150 "as key %K", de->de_ih, key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 }
152#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 /* binary search in directory item by third componen t of the
155 key. sets de->de_entry_num of de */
156 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
157 path->pos_in_item = de->de_entry_num;
158 if (retval != NAME_NOT_FOUND) {
159 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
160 set_de_name_and_namelen(de);
161 set_de_object_key(de);
162 }
163 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
167
168/* The third component is hashed, and you can choose from more than
169 one hash function. Per directory hashes are not yet implemented
170 but are thought about. This function should be moved to hashes.c
171 Jedi, please do so. -Hans */
172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173static __u32 get_third_component(struct super_block *s,
174 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 if (!len || (len == 1 && name[0] == '.'))
179 return DOT_OFFSET;
180 if (len == 2 && name[0] == '.' && name[1] == '.')
181 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 // take bits from 7-th to 30-th including both bounds
186 res = GET_HASH_VALUE(res);
187 if (res == 0)
188 // needed to have no names before "." and ".." those have hash
189 // value == 0 and generation conters 1 and 2 accordingly
190 res = 128;
191 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194static int reiserfs_match(struct reiserfs_dir_entry *de,
195 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 if ((namelen == de->de_namelen) &&
200 !memcmp(de->de_name, name, de->de_namelen))
201 retval =
202 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
203 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
209
210 /* used when hash collisions exist */
211
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212static int linear_search_in_dir_item(struct cpu_key *key,
213 struct reiserfs_dir_entry *de,
214 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 struct reiserfs_de_head *deh = de->de_deh;
217 int retval;
218 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 if (i == I_ENTRY_COUNT(de->de_ih) ||
223 GET_HASH_VALUE(deh_offset(deh + i)) !=
224 GET_HASH_VALUE(cpu_key_k_offset(key))) {
225 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
229 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233 for (; i >= 0; i--, deh--) {
234 if (GET_HASH_VALUE(deh_offset(deh)) !=
235 GET_HASH_VALUE(cpu_key_k_offset(key))) {
236 // hash value does not match, no need to check whole name
237 return NAME_NOT_FOUND;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 /* mark, that this generation number is used */
241 if (de->de_gen_number_bit_string)
242 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500243 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 // calculate pointer to name and namelen
246 de->de_entry_num = i;
247 set_de_name_and_namelen(de);
248
249 if ((retval =
250 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
251 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
252
253 // key of pointed object
254 set_de_object_key(de);
255
256 store_de_entry_key(de);
257
258 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
259 return retval;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
264 /* we have reached left most entry in the node. In common we
265 have to go to the left neighbor, but if generation counter
266 is 0 already, we know for sure, that there is no name with
267 the same hash value */
268 // FIXME: this work correctly only because hash value can not
269 // be 0. Btw, in case of Yura's hash it is probably possible,
270 // so, this is a bug
271 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 RFALSE(de->de_item_num,
274 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700276 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279// may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
280// FIXME: should add something like IOERROR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800282 struct treepath *path_to_entry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700285 struct cpu_key key_to_search;
286 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
289 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 /* we will search for this key in the tree */
292 make_cpu_key(&key_to_search, dir,
293 get_third_component(dir->i_sb, name, namelen),
294 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700296 while (1) {
297 retval =
298 search_by_entry_key(dir->i_sb, &key_to_search,
299 path_to_entry, de);
300 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400301 reiserfs_error(dir->i_sb, "zam-7001", "io error");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700302 return IO_ERROR;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305 /* compare names for all entries having given hash value */
306 retval =
307 linear_search_in_dir_item(&key_to_search, de, name,
308 namelen);
309 if (retval != GOTO_PREVIOUS_ITEM) {
310 /* there is no need to scan directory anymore. Given entry found or does not exist */
311 path_to_entry->pos_in_item = de->de_entry_num;
312 return retval;
313 }
314
315 /* there is left neighboring item of this directory and given entry can be there */
316 set_cpu_key_k_offset(&key_to_search,
317 le_ih_k_offset(de->de_ih) - 1);
318 pathrelse(path_to_entry);
319
320 } /* while (1) */
321}
322
323static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
324 struct nameidata *nd)
325{
326 int retval;
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200327 int lock_depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700328 struct inode *inode = NULL;
329 struct reiserfs_dir_entry de;
330 INITIALIZE_PATH(path_to_entry);
331
332 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
333 return ERR_PTR(-ENAMETOOLONG);
334
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200335 /*
336 * Might be called with or without the write lock, must be careful
337 * to not recursively hold it in case we want to release the lock
338 * before rescheduling.
339 */
340 lock_depth = reiserfs_write_lock_once(dir->i_sb);
341
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342 de.de_gen_number_bit_string = NULL;
343 retval =
344 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
345 &path_to_entry, &de);
346 pathrelse(&path_to_entry);
347 if (retval == NAME_FOUND) {
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400348 inode = reiserfs_iget(dir->i_sb,
349 (struct cpu_key *)&(de.de_dir_id));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700350 if (!inode || IS_ERR(inode)) {
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200351 reiserfs_write_unlock_once(dir->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700352 return ERR_PTR(-EACCES);
353 }
354
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400355 /* Propagate the private flag so we know we're
356 * in the priv tree */
357 if (IS_PRIVATE(dir))
358 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700359 }
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200360 reiserfs_write_unlock_once(dir->i_sb, lock_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Pekka Enbergd9975d62006-01-14 13:21:09 -0800365 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Jeff Mahoney0222e652009-03-30 14:02:44 -0400368/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369** looks up the dentry of the parent directory for child.
370** taken from ext2_get_parent
371*/
372struct dentry *reiserfs_get_parent(struct dentry *child)
373{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 int retval;
375 struct inode *inode = NULL;
376 struct reiserfs_dir_entry de;
377 INITIALIZE_PATH(path_to_entry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378 struct inode *dir = child->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700380 if (dir->i_nlink == 0) {
381 return ERR_PTR(-ENOENT);
382 }
383 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 reiserfs_write_lock(dir->i_sb);
386 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
387 pathrelse(&path_to_entry);
388 if (retval != NAME_FOUND) {
389 reiserfs_write_unlock(dir->i_sb);
390 return ERR_PTR(-ENOENT);
391 }
392 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Christoph Hellwig44003722008-08-11 15:49:04 +0200395 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Jeff Mahoney0222e652009-03-30 14:02:44 -0400398/* add entry to the directory (entry can be hidden).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400insert definition of when hidden directories are used here -Hans
401
402 Does not mark dir inode dirty, do it after successesfull call to it */
403
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700404static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
405 struct inode *dir, const char *name, int namelen,
406 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408 struct cpu_key entry_key;
409 struct reiserfs_de_head *deh;
410 INITIALIZE_PATH(path);
411 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500412 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 int gen_number;
414 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
415 if we create file with short name */
416 char *buffer;
417 int buflen, paste_size;
418 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700420 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700422 /* cannot allow items to be added into a busy deleted directory */
423 if (!namelen)
424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
427 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429 /* each entry has unique key. compose it */
430 make_cpu_key(&entry_key, dir,
431 get_third_component(dir->i_sb, name, namelen),
432 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 /* get memory for composing the entry */
435 buflen = DEH_SIZE + ROUND_UP(namelen);
436 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800437 buffer = kmalloc(buflen, GFP_NOFS);
Al Viro9dce07f2008-03-29 03:07:28 +0000438 if (!buffer)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700439 return -ENOMEM;
440 } else
441 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443 paste_size =
444 (get_inode_sd_version(dir) ==
445 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
448 deh = (struct reiserfs_de_head *)buffer;
449 deh->deh_location = 0; /* JDM Endian safe if 0 */
450 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
451 deh->deh_state = 0; /* JDM Endian safe if 0 */
452 /* put key (ino analog) to de */
453 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
454 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 /* copy name */
457 memcpy((char *)(deh + 1), name, namelen);
458 /* padd by 0s to the 4 byte boundary */
459 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
462 mark_de_without_sd(deh);
463 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465 /* find the proper place for the new entry */
466 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500467 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
469 if (retval != NAME_NOT_FOUND) {
470 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800471 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 pathrelse(&path);
473
474 if (retval == IO_ERROR) {
475 return -EIO;
476 }
477
478 if (retval != NAME_FOUND) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400479 reiserfs_error(dir->i_sb, "zam-7002",
480 "reiserfs_find_entry() returned "
481 "unexpected value (%d)", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700482 }
483
484 return -EEXIST;
485 }
486
487 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500488 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700489 MAX_GENERATION_NUMBER + 1);
490 if (gen_number > MAX_GENERATION_NUMBER) {
491 /* there is no free generation number */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400492 reiserfs_warning(dir->i_sb, "reiserfs-7010",
493 "Congratulations! we have got hash function "
494 "screwed up");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700495 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800496 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700497 pathrelse(&path);
498 return -EBUSY;
499 }
500 /* adjust offset of directory enrty */
501 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
502 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
503
504 /* update max-hash-collisions counter in reiserfs_sb_info */
505 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
506
507 if (gen_number != 0) { /* we need to re-search for the insertion point */
508 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
509 NAME_NOT_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400510 reiserfs_warning(dir->i_sb, "vs-7032",
511 "entry with this key (%K) already "
512 "exists", &entry_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700513
514 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800515 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700516 pathrelse(&path);
517 return -EBUSY;
518 }
519 }
520
521 /* perform the insertion of the entry that we have prepared */
522 retval =
523 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
524 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800526 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 if (retval) {
528 reiserfs_check_path(&path);
529 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532 dir->i_size += paste_size;
533 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
534 if (!S_ISDIR(inode->i_mode) && visible)
535 // reiserfs_mkdir or reiserfs_rename will do that by itself
536 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700538 reiserfs_check_path(&path);
539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/* quota utility function, call if you've had to abort after calling
543** new_inode_init, and have not called reiserfs_new_inode yet.
544** This should only be called on inodes that do not have stat data
545** inserted into the tree yet.
546*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700547static int drop_new_inode(struct inode *inode)
548{
Jan Kara77db4f22009-01-26 17:14:18 +0100549 vfs_dq_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700550 make_bad_inode(inode);
551 inode->i_flags |= S_NOQUOTA;
552 iput(inode);
553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Jeff Mahoney0222e652009-03-30 14:02:44 -0400556/* utility function that does setup for reiserfs_new_inode.
Jan Kara77db4f22009-01-26 17:14:18 +0100557** vfs_dq_init needs lots of credits so it's better to have it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558** outside of a transaction, so we had to pull some bits of
559** reiserfs_new_inode out into this func.
560*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
562{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700564 /* the quota init calls have to know who to charge the quota to, so
565 ** we have to set uid and gid here
566 */
David Howells414cb202008-11-14 10:39:01 +1100567 inode->i_uid = current_fsuid();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700568 inode->i_mode = mode;
Jan Kara1b0a74d2005-08-13 13:15:34 +0200569 /* Make inode invalid - just in case we are going to drop it before
570 * the initialization happens */
571 INODE_PKEY(inode)->k_objectid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 if (dir->i_mode & S_ISGID) {
574 inode->i_gid = dir->i_gid;
575 if (S_ISDIR(mode))
576 inode->i_mode |= S_ISGID;
577 } else {
David Howells414cb202008-11-14 10:39:01 +1100578 inode->i_gid = current_fsgid();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700579 }
Jan Kara77db4f22009-01-26 17:14:18 +0100580 vfs_dq_init(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700584static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
585 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700587 int retval;
588 struct inode *inode;
589 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
590 int jbegin_count =
591 JOURNAL_PER_BALANCE_CNT * 2 +
592 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
593 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
594 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400595 struct reiserfs_security_handle security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 if (!(inode = new_inode(dir->i_sb))) {
598 return -ENOMEM;
599 }
600 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400602 jbegin_count += reiserfs_cache_default_acl(dir);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400603 retval = reiserfs_security_init(dir, inode, &security);
604 if (retval < 0) {
605 drop_new_inode(inode);
606 return retval;
607 }
608 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700609 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700611 retval = journal_begin(&th, dir->i_sb, jbegin_count);
612 if (retval) {
613 drop_new_inode(inode);
614 goto out_failed;
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 retval =
618 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400619 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700620 if (retval)
621 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 inode->i_op = &reiserfs_file_inode_operations;
624 inode->i_fop = &reiserfs_file_operations;
625 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627 retval =
628 reiserfs_add_entry(&th, dir, dentry->d_name.name,
629 dentry->d_name.len, inode, 1 /*visible */ );
630 if (retval) {
631 int err;
632 inode->i_nlink--;
633 reiserfs_update_sd(&th, inode);
634 err = journal_end(&th, dir->i_sb, jbegin_count);
635 if (err)
636 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500637 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 iput(inode);
639 goto out_failed;
640 }
641 reiserfs_update_inode_transaction(inode);
642 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700644 d_instantiate(dentry, inode);
Al Viroc1eaa262008-12-30 02:03:58 -0500645 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700646 retval = journal_end(&th, dir->i_sb, jbegin_count);
647
648 out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 reiserfs_write_unlock(dir->i_sb);
650 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
654 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 int retval;
657 struct inode *inode;
658 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400659 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700660 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
661 int jbegin_count =
662 JOURNAL_PER_BALANCE_CNT * 3 +
663 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
664 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700666 if (!new_valid_dev(rdev))
667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 if (!(inode = new_inode(dir->i_sb))) {
670 return -ENOMEM;
671 }
672 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400674 jbegin_count += reiserfs_cache_default_acl(dir);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400675 retval = reiserfs_security_init(dir, inode, &security);
676 if (retval < 0) {
677 drop_new_inode(inode);
678 return retval;
679 }
680 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700683 retval = journal_begin(&th, dir->i_sb, jbegin_count);
684 if (retval) {
685 drop_new_inode(inode);
686 goto out_failed;
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700689 retval =
690 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400691 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700692 if (retval) {
693 goto out_failed;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696 inode->i_op = &reiserfs_special_inode_operations;
697 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700699 //FIXME: needed for block and char devices only
700 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700702 reiserfs_update_inode_transaction(inode);
703 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 retval =
706 reiserfs_add_entry(&th, dir, dentry->d_name.name,
707 dentry->d_name.len, inode, 1 /*visible */ );
708 if (retval) {
709 int err;
710 inode->i_nlink--;
711 reiserfs_update_sd(&th, inode);
712 err = journal_end(&th, dir->i_sb, jbegin_count);
713 if (err)
714 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500715 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 iput(inode);
717 goto out_failed;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 d_instantiate(dentry, inode);
Al Viroc1eaa262008-12-30 02:03:58 -0500721 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 retval = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700724 out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700725 reiserfs_write_unlock(dir->i_sb);
726 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700729static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 int retval;
732 struct inode *inode;
733 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400734 struct reiserfs_security_handle security;
Frederic Weisbeckerb10ab4c2009-08-25 02:44:21 +0200735 int lock_depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
737 int jbegin_count =
738 JOURNAL_PER_BALANCE_CNT * 3 +
739 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
740 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
744 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 mode = S_IFDIR | mode;
747 if (!(inode = new_inode(dir->i_sb))) {
748 return -ENOMEM;
749 }
750 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400752 jbegin_count += reiserfs_cache_default_acl(dir);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400753 retval = reiserfs_security_init(dir, inode, &security);
754 if (retval < 0) {
755 drop_new_inode(inode);
756 return retval;
757 }
758 jbegin_count += retval;
Frederic Weisbeckerb10ab4c2009-08-25 02:44:21 +0200759 lock_depth = reiserfs_write_lock_once(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700761 retval = journal_begin(&th, dir->i_sb, jbegin_count);
762 if (retval) {
763 drop_new_inode(inode);
764 goto out_failed;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700767 /* inc the link count now, so another writer doesn't overflow it while
768 ** we sleep later on.
769 */
770 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700772 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
773 old_format_only(dir->i_sb) ?
774 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400775 dentry, inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700776 if (retval) {
777 dir->i_nlink--;
778 goto out_failed;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781 reiserfs_update_inode_transaction(inode);
782 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700784 inode->i_op = &reiserfs_dir_inode_operations;
785 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700787 // note, _this_ add_entry will not update dir's stat data
788 retval =
789 reiserfs_add_entry(&th, dir, dentry->d_name.name,
790 dentry->d_name.len, inode, 1 /*visible */ );
791 if (retval) {
792 int err;
793 inode->i_nlink = 0;
794 DEC_DIR_INODE_NLINK(dir);
795 reiserfs_update_sd(&th, inode);
796 err = journal_end(&th, dir->i_sb, jbegin_count);
797 if (err)
798 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500799 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700800 iput(inode);
801 goto out_failed;
802 }
803 // the above add_entry did not update dir's stat data
804 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 d_instantiate(dentry, inode);
Al Viroc1eaa262008-12-30 02:03:58 -0500807 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700808 retval = journal_end(&th, dir->i_sb, jbegin_count);
Frederic Weisbeckerb10ab4c2009-08-25 02:44:21 +0200809out_failed:
810 reiserfs_write_unlock_once(dir->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700811 return retval;
812}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814static inline int reiserfs_empty_dir(struct inode *inode)
815{
816 /* we can cheat because an old format dir cannot have
817 ** EMPTY_DIR_SIZE, and a new format dir cannot have
Jeff Mahoney0222e652009-03-30 14:02:44 -0400818 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700819 ** regardless of disk format version, the directory is empty.
820 */
821 if (inode->i_size != EMPTY_DIR_SIZE &&
822 inode->i_size != EMPTY_DIR_SIZE_V1) {
823 return 0;
824 }
825 return 1;
826}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700828static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
829{
830 int retval, err;
831 struct inode *inode;
832 struct reiserfs_transaction_handle th;
833 int jbegin_count;
834 INITIALIZE_PATH(path);
835 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 /* we will be doing 2 balancings and update 2 stat data, we change quotas
838 * of the owner of the directory and of the owner of the parent directory.
839 * The quota structure is possibly deleted only on last iput => outside
840 * of this transaction */
841 jbegin_count =
842 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
843 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700845 reiserfs_write_lock(dir->i_sb);
846 retval = journal_begin(&th, dir->i_sb, jbegin_count);
847 if (retval)
848 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700850 de.de_gen_number_bit_string = NULL;
851 if ((retval =
852 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
853 &path, &de)) == NAME_NOT_FOUND) {
854 retval = -ENOENT;
855 goto end_rmdir;
856 } else if (retval == IO_ERROR) {
857 retval = -EIO;
858 goto end_rmdir;
859 }
860
861 inode = dentry->d_inode;
862
863 reiserfs_update_inode_transaction(inode);
864 reiserfs_update_inode_transaction(dir);
865
866 if (de.de_objectid != inode->i_ino) {
867 // FIXME: compare key of an object and a key found in the
868 // entry
869 retval = -EIO;
870 goto end_rmdir;
871 }
872 if (!reiserfs_empty_dir(inode)) {
873 retval = -ENOTEMPTY;
874 goto end_rmdir;
875 }
876
877 /* cut entry from dir directory */
878 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
879 0 /*new file size - not used here */ );
880 if (retval < 0)
881 goto end_rmdir;
882
883 if (inode->i_nlink != 2 && inode->i_nlink != 1)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400884 reiserfs_error(inode->i_sb, "reiserfs-7040",
885 "empty directory has nlink != 2 (%d)",
886 inode->i_nlink);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887
Dave Hansence71ec32006-09-30 23:29:06 -0700888 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
890 reiserfs_update_sd(&th, inode);
891
892 DEC_DIR_INODE_NLINK(dir)
893 dir->i_size -= (DEH_SIZE + de.de_entrylen);
894 reiserfs_update_sd(&th, dir);
895
896 /* prevent empty directory from getting lost */
897 add_save_link(&th, inode, 0 /* not truncate */ );
898
899 retval = journal_end(&th, dir->i_sb, jbegin_count);
900 reiserfs_check_path(&path);
901 out_rmdir:
902 reiserfs_write_unlock(dir->i_sb);
903 return retval;
904
905 end_rmdir:
906 /* we must release path, because we did not call
907 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
908 release path if operation was not complete */
909 pathrelse(&path);
910 err = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 reiserfs_write_unlock(dir->i_sb);
912 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700915static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
916{
917 int retval, err;
918 struct inode *inode;
919 struct reiserfs_dir_entry de;
920 INITIALIZE_PATH(path);
921 struct reiserfs_transaction_handle th;
922 int jbegin_count;
923 unsigned long savelink;
924
925 inode = dentry->d_inode;
926
927 /* in this transaction we can be doing at max two balancings and update
928 * two stat datas, we change quotas of the owner of the directory and of
929 * the owner of the parent directory. The quota structure is possibly
930 * deleted only on iput => outside of this transaction */
931 jbegin_count =
932 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
933 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
934
935 reiserfs_write_lock(dir->i_sb);
936 retval = journal_begin(&th, dir->i_sb, jbegin_count);
937 if (retval)
938 goto out_unlink;
939
940 de.de_gen_number_bit_string = NULL;
941 if ((retval =
942 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
943 &path, &de)) == NAME_NOT_FOUND) {
944 retval = -ENOENT;
945 goto end_unlink;
946 } else if (retval == IO_ERROR) {
947 retval = -EIO;
948 goto end_unlink;
949 }
950
951 reiserfs_update_inode_transaction(inode);
952 reiserfs_update_inode_transaction(dir);
953
954 if (de.de_objectid != inode->i_ino) {
955 // FIXME: compare key of an object and a key found in the
956 // entry
957 retval = -EIO;
958 goto end_unlink;
959 }
960
961 if (!inode->i_nlink) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400962 reiserfs_warning(inode->i_sb, "reiserfs-7042",
963 "deleting nonexistent file (%lu), %d",
964 inode->i_ino, inode->i_nlink);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965 inode->i_nlink = 1;
966 }
967
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700968 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969
970 /*
971 * we schedule before doing the add_save_link call, save the link
972 * count so we don't race
973 */
974 savelink = inode->i_nlink;
975
976 retval =
977 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
978 0);
979 if (retval < 0) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700980 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700981 goto end_unlink;
982 }
983 inode->i_ctime = CURRENT_TIME_SEC;
984 reiserfs_update_sd(&th, inode);
985
986 dir->i_size -= (de.de_entrylen + DEH_SIZE);
987 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
988 reiserfs_update_sd(&th, dir);
989
990 if (!savelink)
991 /* prevent file from getting lost */
992 add_save_link(&th, inode, 0 /* not truncate */ );
993
994 retval = journal_end(&th, dir->i_sb, jbegin_count);
995 reiserfs_check_path(&path);
996 reiserfs_write_unlock(dir->i_sb);
997 return retval;
998
999 end_unlink:
1000 pathrelse(&path);
1001 err = journal_end(&th, dir->i_sb, jbegin_count);
1002 reiserfs_check_path(&path);
1003 if (err)
1004 retval = err;
1005 out_unlink:
1006 reiserfs_write_unlock(dir->i_sb);
1007 return retval;
1008}
1009
1010static int reiserfs_symlink(struct inode *parent_dir,
1011 struct dentry *dentry, const char *symname)
1012{
1013 int retval;
1014 struct inode *inode;
1015 char *name;
1016 int item_len;
1017 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001018 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001019 int mode = S_IFLNK | S_IRWXUGO;
1020 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1021 int jbegin_count =
1022 JOURNAL_PER_BALANCE_CNT * 3 +
1023 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1024 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1025
1026 if (!(inode = new_inode(parent_dir->i_sb))) {
1027 return -ENOMEM;
1028 }
1029 new_inode_init(inode, parent_dir, mode);
1030
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001031 retval = reiserfs_security_init(parent_dir, inode, &security);
1032 if (retval < 0) {
1033 drop_new_inode(inode);
1034 return retval;
1035 }
1036 jbegin_count += retval;
1037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001038 reiserfs_write_lock(parent_dir->i_sb);
1039 item_len = ROUND_UP(strlen(symname));
1040 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1041 retval = -ENAMETOOLONG;
1042 drop_new_inode(inode);
1043 goto out_failed;
1044 }
1045
Pekka Enbergd739b422006-02-01 03:06:43 -08001046 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001047 if (!name) {
1048 drop_new_inode(inode);
1049 retval = -ENOMEM;
1050 goto out_failed;
1051 }
1052 memcpy(name, symname, strlen(symname));
1053 padd_item(name, item_len, strlen(symname));
1054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1056 if (retval) {
1057 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001058 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001059 goto out_failed;
1060 }
1061
1062 retval =
1063 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001064 dentry, inode, &security);
Pekka Enbergd739b422006-02-01 03:06:43 -08001065 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001066 if (retval) { /* reiserfs_new_inode iputs for us */
1067 goto out_failed;
1068 }
1069
1070 reiserfs_update_inode_transaction(inode);
1071 reiserfs_update_inode_transaction(parent_dir);
1072
1073 inode->i_op = &reiserfs_symlink_inode_operations;
1074 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1075
1076 // must be sure this inode is written with this transaction
1077 //
1078 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1079
1080 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1081 dentry->d_name.len, inode, 1 /*visible */ );
1082 if (retval) {
1083 int err;
1084 inode->i_nlink--;
1085 reiserfs_update_sd(&th, inode);
1086 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1087 if (err)
1088 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -05001089 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001090 iput(inode);
1091 goto out_failed;
1092 }
1093
1094 d_instantiate(dentry, inode);
Al Viroc1eaa262008-12-30 02:03:58 -05001095 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1097 out_failed:
1098 reiserfs_write_unlock(parent_dir->i_sb);
1099 return retval;
1100}
1101
1102static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1103 struct dentry *dentry)
1104{
1105 int retval;
1106 struct inode *inode = old_dentry->d_inode;
1107 struct reiserfs_transaction_handle th;
1108 /* We need blocks for transaction + update of quotas for the owners of the directory */
1109 int jbegin_count =
1110 JOURNAL_PER_BALANCE_CNT * 3 +
1111 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1112
1113 reiserfs_write_lock(dir->i_sb);
1114 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1115 //FIXME: sd_nlink is 32 bit for new files
1116 reiserfs_write_unlock(dir->i_sb);
1117 return -EMLINK;
1118 }
1119 if (inode->i_nlink == 0) {
1120 reiserfs_write_unlock(dir->i_sb);
1121 return -ENOENT;
1122 }
1123
1124 /* inc before scheduling so reiserfs_unlink knows we are here */
Dave Hansend8c76e62006-09-30 23:29:04 -07001125 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001126
1127 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1128 if (retval) {
1129 inode->i_nlink--;
1130 reiserfs_write_unlock(dir->i_sb);
1131 return retval;
1132 }
1133
1134 /* create new entry */
1135 retval =
1136 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1137 dentry->d_name.len, inode, 1 /*visible */ );
1138
1139 reiserfs_update_inode_transaction(inode);
1140 reiserfs_update_inode_transaction(dir);
1141
1142 if (retval) {
1143 int err;
1144 inode->i_nlink--;
1145 err = journal_end(&th, dir->i_sb, jbegin_count);
1146 reiserfs_write_unlock(dir->i_sb);
1147 return err ? err : retval;
1148 }
1149
1150 inode->i_ctime = CURRENT_TIME_SEC;
1151 reiserfs_update_sd(&th, inode);
1152
1153 atomic_inc(&inode->i_count);
1154 d_instantiate(dentry, inode);
1155 retval = journal_end(&th, dir->i_sb, jbegin_count);
1156 reiserfs_write_unlock(dir->i_sb);
1157 return retval;
1158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jeff Mahoney0222e652009-03-30 14:02:44 -04001160/* de contains information pointing to an entry which */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161static int de_still_valid(const char *name, int len,
1162 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001164 struct reiserfs_dir_entry tmp = *de;
1165
1166 // recalculate pointer to name and name length
1167 set_de_name_and_namelen(&tmp);
1168 // FIXME: could check more
1169 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1170 return 0;
1171 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001174static int entry_points_to_object(const char *name, int len,
1175 struct reiserfs_dir_entry *de,
1176 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001178 if (!de_still_valid(name, len, de))
1179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001181 if (inode) {
1182 if (!de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001183 reiserfs_panic(inode->i_sb, "vs-7042",
1184 "entry must be visible");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001185 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001188 /* this must be added hidden entry */
1189 if (de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001190 reiserfs_panic(NULL, "vs-7043", "entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001192 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1197 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199 /* JDM These operations are endian safe - both are le */
1200 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1201 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Jeff Mahoney0222e652009-03-30 14:02:44 -04001204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * process, that is going to call fix_nodes/do_balance must hold only
1206 * one path. If it holds 2 or more, it can get into endless waiting in
Jeff Mahoney0222e652009-03-30 14:02:44 -04001207 * get_empty_nodes or its clones
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001209static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1210 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001212 int retval;
1213 INITIALIZE_PATH(old_entry_path);
1214 INITIALIZE_PATH(new_entry_path);
1215 INITIALIZE_PATH(dot_dot_entry_path);
1216 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1217 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1218 struct inode *old_inode, *new_dentry_inode;
1219 struct reiserfs_transaction_handle th;
1220 int jbegin_count;
1221 umode_t old_inode_mode;
1222 unsigned long savelink = 1;
1223 struct timespec ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001225 /* three balancings: (1) old name removal, (2) new name insertion
1226 and (3) maybe "save" link insertion
1227 stat data updates: (1) old directory,
1228 (2) new directory and (3) maybe old object stat data (when it is
1229 directory) and (4) maybe stat data of object to which new entry
1230 pointed initially and (5) maybe block containing ".." of
1231 renamed directory
1232 quota updates: two parent directories */
1233 jbegin_count =
1234 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1235 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 old_inode = old_dentry->d_inode;
1238 new_dentry_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001240 // make sure, that oldname still exists and points to an object we
1241 // are going to rename
1242 old_de.de_gen_number_bit_string = NULL;
1243 reiserfs_write_lock(old_dir->i_sb);
1244 retval =
1245 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1246 old_dentry->d_name.len, &old_entry_path,
1247 &old_de);
1248 pathrelse(&old_entry_path);
1249 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 reiserfs_write_unlock(old_dir->i_sb);
1251 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001253
1254 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1255 reiserfs_write_unlock(old_dir->i_sb);
1256 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001258
1259 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001261 // make sure, that directory being renamed has correct ".."
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001262 // and that its new parent directory has not too many links
1263 // already
1264
1265 if (new_dentry_inode) {
1266 if (!reiserfs_empty_dir(new_dentry_inode)) {
1267 reiserfs_write_unlock(old_dir->i_sb);
1268 return -ENOTEMPTY;
1269 }
1270 }
1271
Jeff Mahoney0222e652009-03-30 14:02:44 -04001272 /* directory is renamed, its parent directory will be changed,
1273 ** so find ".." entry
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001274 */
1275 dot_dot_de.de_gen_number_bit_string = NULL;
1276 retval =
1277 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1278 &dot_dot_de);
1279 pathrelse(&dot_dot_entry_path);
1280 if (retval != NAME_FOUND) {
1281 reiserfs_write_unlock(old_dir->i_sb);
1282 return -EIO;
1283 }
1284
1285 /* inode number of .. must equal old_dir->i_ino */
1286 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1287 reiserfs_write_unlock(old_dir->i_sb);
1288 return -EIO;
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001292 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1293 if (retval) {
1294 reiserfs_write_unlock(old_dir->i_sb);
1295 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 /* add new entry (or find the existing one) */
1299 retval =
1300 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1301 new_dentry->d_name.len, old_inode, 0);
1302 if (retval == -EEXIST) {
1303 if (!new_dentry_inode) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001304 reiserfs_panic(old_dir->i_sb, "vs-7050",
1305 "new entry is found, new inode == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001306 }
1307 } else if (retval) {
1308 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1309 reiserfs_write_unlock(old_dir->i_sb);
1310 return err ? err : retval;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001313 reiserfs_update_inode_transaction(old_dir);
1314 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001316 /* this makes it so an fsync on an open fd for the old name will
1317 ** commit the rename operation
1318 */
1319 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001321 if (new_dentry_inode)
1322 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001324 while (1) {
1325 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1326 if ((retval =
1327 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1328 &old_entry_path,
1329 &old_de)) != NAME_FOUND) {
1330 pathrelse(&old_entry_path);
1331 journal_end(&th, old_dir->i_sb, jbegin_count);
1332 reiserfs_write_unlock(old_dir->i_sb);
1333 return -EIO;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001338 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1339
1340 // look for new name by reiserfs_find_entry
1341 new_de.de_gen_number_bit_string = NULL;
1342 retval =
1343 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1344 new_dentry->d_name.len, &new_entry_path,
1345 &new_de);
1346 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1347 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1348 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1349 pathrelse(&new_entry_path);
1350 pathrelse(&old_entry_path);
1351 journal_end(&th, old_dir->i_sb, jbegin_count);
1352 reiserfs_write_unlock(old_dir->i_sb);
1353 return -EIO;
1354 }
1355
1356 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1357
1358 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1359
1360 if (S_ISDIR(old_inode->i_mode)) {
1361 if ((retval =
1362 search_by_entry_key(new_dir->i_sb,
1363 &dot_dot_de.de_entry_key,
1364 &dot_dot_entry_path,
1365 &dot_dot_de)) != NAME_FOUND) {
1366 pathrelse(&dot_dot_entry_path);
1367 pathrelse(&new_entry_path);
1368 pathrelse(&old_entry_path);
1369 journal_end(&th, old_dir->i_sb, jbegin_count);
1370 reiserfs_write_unlock(old_dir->i_sb);
1371 return -EIO;
1372 }
1373 copy_item_head(&dot_dot_ih,
1374 get_ih(&dot_dot_entry_path));
1375 // node containing ".." gets into transaction
1376 reiserfs_prepare_for_journal(old_inode->i_sb,
1377 dot_dot_de.de_bh, 1);
1378 }
1379 /* we should check seals here, not do
1380 this stuff, yes? Then, having
1381 gathered everything into RAM we
1382 should lock the buffers, yes? -Hans */
Jeff Mahoney0222e652009-03-30 14:02:44 -04001383 /* probably. our rename needs to hold more
1384 ** than one path at once. The seals would
1385 ** have to be written to deal with multi-path
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001386 ** issues -chris
1387 */
1388 /* sanity checking before doing the rename - avoid races many
1389 ** of the above checks could have scheduled. We have to be
1390 ** sure our items haven't been shifted by another process.
1391 */
1392 if (item_moved(&new_entry_ih, &new_entry_path) ||
1393 !entry_points_to_object(new_dentry->d_name.name,
1394 new_dentry->d_name.len,
1395 &new_de, new_dentry_inode) ||
1396 item_moved(&old_entry_ih, &old_entry_path) ||
1397 !entry_points_to_object(old_dentry->d_name.name,
1398 old_dentry->d_name.len,
1399 &old_de, old_inode)) {
1400 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1401 new_de.de_bh);
1402 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1403 old_de.de_bh);
1404 if (S_ISDIR(old_inode_mode))
1405 reiserfs_restore_prepared_buffer(old_inode->
1406 i_sb,
1407 dot_dot_de.
1408 de_bh);
1409 continue;
1410 }
1411 if (S_ISDIR(old_inode_mode)) {
1412 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1413 !entry_points_to_object("..", 2, &dot_dot_de,
1414 old_dir)) {
1415 reiserfs_restore_prepared_buffer(old_inode->
1416 i_sb,
1417 old_de.de_bh);
1418 reiserfs_restore_prepared_buffer(old_inode->
1419 i_sb,
1420 new_de.de_bh);
1421 reiserfs_restore_prepared_buffer(old_inode->
1422 i_sb,
1423 dot_dot_de.
1424 de_bh);
1425 continue;
1426 }
1427 }
1428
1429 RFALSE(S_ISDIR(old_inode_mode) &&
1430 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1431
1432 break;
1433 }
1434
1435 /* ok, all the changes can be done in one fell swoop when we
1436 have claimed all the buffers needed. */
1437
1438 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1439 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1440 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1441
1442 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1443 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1444 ctime = CURRENT_TIME_SEC;
1445 old_dir->i_ctime = old_dir->i_mtime = ctime;
1446 new_dir->i_ctime = new_dir->i_mtime = ctime;
1447 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1448 renamed object */
1449 old_inode->i_ctime = ctime;
1450
1451 if (new_dentry_inode) {
1452 // adjust link number of the victim
1453 if (S_ISDIR(new_dentry_inode->i_mode)) {
Dave Hansence71ec32006-09-30 23:29:06 -07001454 clear_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001455 } else {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001456 drop_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001457 }
1458 new_dentry_inode->i_ctime = ctime;
1459 savelink = new_dentry_inode->i_nlink;
1460 }
1461
1462 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001463 /* adjust ".." of renamed directory */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001464 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1465 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1466
1467 if (!new_dentry_inode)
1468 /* there (in new_dir) was no directory, so it got new link
1469 (".." of renamed directory) */
1470 INC_DIR_INODE_NLINK(new_dir);
1471
1472 /* old directory lost one link - ".. " of renamed directory */
1473 DEC_DIR_INODE_NLINK(old_dir);
1474 }
1475 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1476 pathrelse(&new_entry_path);
1477 pathrelse(&dot_dot_entry_path);
1478
1479 // FIXME: this reiserfs_cut_from_item's return value may screw up
1480 // anybody, but it will panic if will not be able to find the
1481 // entry. This needs one more clean up
1482 if (reiserfs_cut_from_item
1483 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1484 0) < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -04001485 reiserfs_error(old_dir->i_sb, "vs-7060",
1486 "couldn't not cut old name. Fsck later?");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487
1488 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1489
1490 reiserfs_update_sd(&th, old_dir);
1491 reiserfs_update_sd(&th, new_dir);
1492 reiserfs_update_sd(&th, old_inode);
1493
1494 if (new_dentry_inode) {
1495 if (savelink == 0)
1496 add_save_link(&th, new_dentry_inode,
1497 0 /* not truncate */ );
1498 reiserfs_update_sd(&th, new_dentry_inode);
1499 }
1500
1501 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1502 reiserfs_write_unlock(old_dir->i_sb);
1503 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506/*
1507 * directories can handle most operations...
1508 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001509const struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001510 //&reiserfs_dir_operations, /* default_file_ops */
1511 .create = reiserfs_create,
1512 .lookup = reiserfs_lookup,
1513 .link = reiserfs_link,
1514 .unlink = reiserfs_unlink,
1515 .symlink = reiserfs_symlink,
1516 .mkdir = reiserfs_mkdir,
1517 .rmdir = reiserfs_rmdir,
1518 .mknod = reiserfs_mknod,
1519 .rename = reiserfs_rename,
1520 .setattr = reiserfs_setattr,
1521 .setxattr = reiserfs_setxattr,
1522 .getxattr = reiserfs_getxattr,
1523 .listxattr = reiserfs_listxattr,
1524 .removexattr = reiserfs_removexattr,
1525 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526};
1527
1528/*
1529 * symlink operations.. same as page_symlink_inode_operations, with xattr
1530 * stuff added
1531 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001532const struct inode_operations reiserfs_symlink_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001533 .readlink = generic_readlink,
1534 .follow_link = page_follow_link_light,
1535 .put_link = page_put_link,
1536 .setattr = reiserfs_setattr,
1537 .setxattr = reiserfs_setxattr,
1538 .getxattr = reiserfs_getxattr,
1539 .listxattr = reiserfs_listxattr,
1540 .removexattr = reiserfs_removexattr,
1541 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543};
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/*
1546 * special file operations.. just xattr/acl stuff
1547 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001548const struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001549 .setattr = reiserfs_setattr,
1550 .setxattr = reiserfs_setxattr,
1551 .getxattr = reiserfs_getxattr,
1552 .listxattr = reiserfs_listxattr,
1553 .removexattr = reiserfs_removexattr,
1554 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556};