blob: e825f8b63e6b7433c125e040117de525a2d0cf31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 *
4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
5 *
6 * Trivial Changes:
7 * Rights granted to Hans Reiser to redistribute under other terms providing
8 * he accepts all liability including but not limited to patent, fitness
9 * for purpose, and direct or indirect claims arising from failure to perform.
10 *
11 * NO WARRANTY
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/time.h>
15#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Al Virof466c6f2012-03-17 01:16:43 -040017#include "reiserfs.h"
Al Viroa3063ab2012-03-17 01:03:10 -040018#include "acl.h"
Al Viroc45ac882012-03-17 00:59:06 -040019#include "xattr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/quotaops.h>
21
Miklos Szeredibfe86842011-10-28 14:13:29 +020022#define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) set_nlink(i, 1); }
Dave Hansen9a53c3a2006-09-30 23:29:03 -070023#define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -080057 struct treepath *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
Jeff Mahoney0222e652009-03-30 14:02:44 -0400110entry position in the item
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
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,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800116 struct treepath *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)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400124 reiserfs_error(sb, "vs-7000", "search_by_key "
125 "returned item position == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700126 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);
Jeff Mahoney0030b642009-03-30 14:02:28 -0400139 reiserfs_error(sb, "vs-7002", "no path to here");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700140 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 if (!is_direntry_le_ih(de->de_ih) ||
147 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
148 print_block(de->de_bh, 0, -1, -1);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400149 reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
150 "item or does not belong to the same directory "
151 "as key %K", de->de_ih, key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152 }
153#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 /* binary search in directory item by third componen t of the
156 key. sets de->de_entry_num of de */
157 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
158 path->pos_in_item = de->de_entry_num;
159 if (retval != NAME_NOT_FOUND) {
160 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
161 set_de_name_and_namelen(de);
162 set_de_object_key(de);
163 }
164 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
168
169/* The third component is hashed, and you can choose from more than
170 one hash function. Per directory hashes are not yet implemented
171 but are thought about. This function should be moved to hashes.c
172 Jedi, please do so. -Hans */
173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174static __u32 get_third_component(struct super_block *s,
175 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 if (!len || (len == 1 && name[0] == '.'))
180 return DOT_OFFSET;
181 if (len == 2 && name[0] == '.' && name[1] == '.')
182 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 // take bits from 7-th to 30-th including both bounds
187 res = GET_HASH_VALUE(res);
188 if (res == 0)
189 // needed to have no names before "." and ".." those have hash
190 // value == 0 and generation conters 1 and 2 accordingly
191 res = 128;
192 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700195static int reiserfs_match(struct reiserfs_dir_entry *de,
196 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 if ((namelen == de->de_namelen) &&
201 !memcmp(de->de_name, name, de->de_namelen))
202 retval =
203 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
204 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
210
211 /* used when hash collisions exist */
212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700213static int linear_search_in_dir_item(struct cpu_key *key,
214 struct reiserfs_dir_entry *de,
215 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217 struct reiserfs_de_head *deh = de->de_deh;
218 int retval;
219 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700221 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 if (i == I_ENTRY_COUNT(de->de_ih) ||
224 GET_HASH_VALUE(deh_offset(deh + i)) !=
225 GET_HASH_VALUE(cpu_key_k_offset(key))) {
226 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
230 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 for (; i >= 0; i--, deh--) {
235 if (GET_HASH_VALUE(deh_offset(deh)) !=
236 GET_HASH_VALUE(cpu_key_k_offset(key))) {
237 // hash value does not match, no need to check whole name
238 return NAME_NOT_FOUND;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 /* mark, that this generation number is used */
242 if (de->de_gen_number_bit_string)
243 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500244 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 // calculate pointer to name and namelen
247 de->de_entry_num = i;
248 set_de_name_and_namelen(de);
249
250 if ((retval =
251 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
252 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
253
254 // key of pointed object
255 set_de_object_key(de);
256
257 store_de_entry_key(de);
258
259 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
260 return retval;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
265 /* we have reached left most entry in the node. In common we
266 have to go to the left neighbor, but if generation counter
267 is 0 already, we know for sure, that there is no name with
268 the same hash value */
269 // FIXME: this work correctly only because hash value can not
270 // be 0. Btw, in case of Yura's hash it is probably possible,
271 // so, this is a bug
272 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700274 RFALSE(de->de_item_num,
275 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280// may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
281// FIXME: should add something like IOERROR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700282static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800283 struct treepath *path_to_entry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700284 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700286 struct cpu_key key_to_search;
287 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700289 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
290 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 /* we will search for this key in the tree */
293 make_cpu_key(&key_to_search, dir,
294 get_third_component(dir->i_sb, name, namelen),
295 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700297 while (1) {
298 retval =
299 search_by_entry_key(dir->i_sb, &key_to_search,
300 path_to_entry, de);
301 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400302 reiserfs_error(dir->i_sb, "zam-7001", "io error");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700303 return IO_ERROR;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306 /* compare names for all entries having given hash value */
307 retval =
308 linear_search_in_dir_item(&key_to_search, de, name,
309 namelen);
310 if (retval != GOTO_PREVIOUS_ITEM) {
311 /* there is no need to scan directory anymore. Given entry found or does not exist */
312 path_to_entry->pos_in_item = de->de_entry_num;
313 return retval;
314 }
315
316 /* there is left neighboring item of this directory and given entry can be there */
317 set_cpu_key_k_offset(&key_to_search,
318 le_ih_k_offset(de->de_ih) - 1);
319 pathrelse(path_to_entry);
320
321 } /* while (1) */
322}
323
324static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400325 unsigned int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700326{
327 int retval;
328 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
Jeff Mahoney278f6672013-08-08 17:34:46 -0400335 reiserfs_write_lock(dir->i_sb);
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700337 de.de_gen_number_bit_string = NULL;
338 retval =
339 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
340 &path_to_entry, &de);
341 pathrelse(&path_to_entry);
342 if (retval == NAME_FOUND) {
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400343 inode = reiserfs_iget(dir->i_sb,
344 (struct cpu_key *)&(de.de_dir_id));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700345 if (!inode || IS_ERR(inode)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -0400346 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700347 return ERR_PTR(-EACCES);
348 }
349
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400350 /* Propagate the private flag so we know we're
351 * in the priv tree */
352 if (IS_PRIVATE(dir))
353 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400355 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700357 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Pekka Enbergd9975d62006-01-14 13:21:09 -0800360 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Jeff Mahoney0222e652009-03-30 14:02:44 -0400363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364** looks up the dentry of the parent directory for child.
365** taken from ext2_get_parent
366*/
367struct dentry *reiserfs_get_parent(struct dentry *child)
368{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 int retval;
370 struct inode *inode = NULL;
371 struct reiserfs_dir_entry de;
372 INITIALIZE_PATH(path_to_entry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373 struct inode *dir = child->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 if (dir->i_nlink == 0) {
376 return ERR_PTR(-ENOENT);
377 }
378 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700380 reiserfs_write_lock(dir->i_sb);
381 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
382 pathrelse(&path_to_entry);
383 if (retval != NAME_FOUND) {
384 reiserfs_write_unlock(dir->i_sb);
385 return ERR_PTR(-ENOENT);
386 }
387 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Christoph Hellwig44003722008-08-11 15:49:04 +0200390 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Jeff Mahoney0222e652009-03-30 14:02:44 -0400393/* add entry to the directory (entry can be hidden).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395insert definition of when hidden directories are used here -Hans
396
397 Does not mark dir inode dirty, do it after successesfull call to it */
398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
400 struct inode *dir, const char *name, int namelen,
401 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700403 struct cpu_key entry_key;
404 struct reiserfs_de_head *deh;
405 INITIALIZE_PATH(path);
406 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500407 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408 int gen_number;
409 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
410 if we create file with short name */
411 char *buffer;
412 int buflen, paste_size;
413 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417 /* cannot allow items to be added into a busy deleted directory */
418 if (!namelen)
419 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
422 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700424 /* each entry has unique key. compose it */
425 make_cpu_key(&entry_key, dir,
426 get_third_component(dir->i_sb, name, namelen),
427 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429 /* get memory for composing the entry */
430 buflen = DEH_SIZE + ROUND_UP(namelen);
431 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800432 buffer = kmalloc(buflen, GFP_NOFS);
Al Viro9dce07f2008-03-29 03:07:28 +0000433 if (!buffer)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 return -ENOMEM;
435 } else
436 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438 paste_size =
439 (get_inode_sd_version(dir) ==
440 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700442 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
443 deh = (struct reiserfs_de_head *)buffer;
444 deh->deh_location = 0; /* JDM Endian safe if 0 */
445 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
446 deh->deh_state = 0; /* JDM Endian safe if 0 */
447 /* put key (ino analog) to de */
448 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
449 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451 /* copy name */
452 memcpy((char *)(deh + 1), name, namelen);
453 /* padd by 0s to the 4 byte boundary */
454 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
457 mark_de_without_sd(deh);
458 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 /* find the proper place for the new entry */
461 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500462 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700463 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
464 if (retval != NAME_NOT_FOUND) {
465 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800466 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467 pathrelse(&path);
468
469 if (retval == IO_ERROR) {
470 return -EIO;
471 }
472
473 if (retval != NAME_FOUND) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400474 reiserfs_error(dir->i_sb, "zam-7002",
475 "reiserfs_find_entry() returned "
476 "unexpected value (%d)", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 }
478
479 return -EEXIST;
480 }
481
482 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500483 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700484 MAX_GENERATION_NUMBER + 1);
485 if (gen_number > MAX_GENERATION_NUMBER) {
486 /* there is no free generation number */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400487 reiserfs_warning(dir->i_sb, "reiserfs-7010",
488 "Congratulations! we have got hash function "
489 "screwed up");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800491 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 pathrelse(&path);
493 return -EBUSY;
494 }
495 /* adjust offset of directory enrty */
496 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
497 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
498
499 /* update max-hash-collisions counter in reiserfs_sb_info */
500 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
501
502 if (gen_number != 0) { /* we need to re-search for the insertion point */
503 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
504 NAME_NOT_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400505 reiserfs_warning(dir->i_sb, "vs-7032",
506 "entry with this key (%K) already "
507 "exists", &entry_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508
509 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800510 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 pathrelse(&path);
512 return -EBUSY;
513 }
514 }
515
516 /* perform the insertion of the entry that we have prepared */
517 retval =
518 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
519 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800521 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700522 if (retval) {
523 reiserfs_check_path(&path);
524 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 dir->i_size += paste_size;
528 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
529 if (!S_ISDIR(inode->i_mode) && visible)
530 // reiserfs_mkdir or reiserfs_rename will do that by itself
531 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 reiserfs_check_path(&path);
534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/* quota utility function, call if you've had to abort after calling
538** new_inode_init, and have not called reiserfs_new_inode yet.
539** This should only be called on inodes that do not have stat data
540** inserted into the tree yet.
541*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700542static int drop_new_inode(struct inode *inode)
543{
Christoph Hellwig9f754752010-03-03 09:05:05 -0500544 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700545 make_bad_inode(inode);
546 inode->i_flags |= S_NOQUOTA;
547 iput(inode);
548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Jeff Mahoney0222e652009-03-30 14:02:44 -0400551/* utility function that does setup for reiserfs_new_inode.
Christoph Hellwig871a2932010-03-03 09:05:07 -0500552** dquot_initialize needs lots of credits so it's better to have it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553** outside of a transaction, so we had to pull some bits of
554** reiserfs_new_inode out into this func.
555*/
Al Viro8e071892011-07-26 02:50:53 -0400556static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557{
Jan Kara1b0a74d2005-08-13 13:15:34 +0200558 /* Make inode invalid - just in case we are going to drop it before
559 * the initialization happens */
560 INODE_PKEY(inode)->k_objectid = 0;
Dmitry Monakhov04b7ed02010-03-04 17:32:19 +0300561 /* the quota init calls have to know who to charge the quota to, so
562 ** we have to set uid and gid here
563 */
564 inode_init_owner(inode, dir, mode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500565 dquot_initialize(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Al Viro4acdaf22011-07-26 01:42:34 -0400569static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400570 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 int retval;
573 struct inode *inode;
574 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
575 int jbegin_count =
576 JOURNAL_PER_BALANCE_CNT * 2 +
577 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
578 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
579 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400580 struct reiserfs_security_handle security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Christoph Hellwig871a2932010-03-03 09:05:07 -0500582 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500583
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700584 if (!(inode = new_inode(dir->i_sb))) {
585 return -ENOMEM;
586 }
587 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400589 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500590 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400591 if (retval < 0) {
592 drop_new_inode(inode);
593 return retval;
594 }
595 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700598 retval = journal_begin(&th, dir->i_sb, jbegin_count);
599 if (retval) {
600 drop_new_inode(inode);
601 goto out_failed;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604 retval =
605 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400606 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607 if (retval)
608 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700610 inode->i_op = &reiserfs_file_inode_operations;
611 inode->i_fop = &reiserfs_file_operations;
612 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700614 retval =
615 reiserfs_add_entry(&th, dir, dentry->d_name.name,
616 dentry->d_name.len, inode, 1 /*visible */ );
617 if (retval) {
618 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200619 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700620 reiserfs_update_sd(&th, inode);
621 err = journal_end(&th, dir->i_sb, jbegin_count);
622 if (err)
623 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500624 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625 iput(inode);
626 goto out_failed;
627 }
628 reiserfs_update_inode_transaction(inode);
629 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Al Viroc1eaa262008-12-30 02:03:58 -0500631 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400632 d_instantiate(dentry, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 retval = journal_end(&th, dir->i_sb, jbegin_count);
634
635 out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700636 reiserfs_write_unlock(dir->i_sb);
637 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Al Viro1a67aaf2011-07-26 01:52:52 -0400640static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700641 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 int retval;
644 struct inode *inode;
645 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400646 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700647 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
648 int jbegin_count =
649 JOURNAL_PER_BALANCE_CNT * 3 +
650 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
651 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653 if (!new_valid_dev(rdev))
654 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Christoph Hellwig871a2932010-03-03 09:05:07 -0500656 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500657
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700658 if (!(inode = new_inode(dir->i_sb))) {
659 return -ENOMEM;
660 }
661 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400663 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500664 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400665 if (retval < 0) {
666 drop_new_inode(inode);
667 return retval;
668 }
669 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 retval = journal_begin(&th, dir->i_sb, jbegin_count);
673 if (retval) {
674 drop_new_inode(inode);
675 goto out_failed;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 retval =
679 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400680 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 if (retval) {
682 goto out_failed;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685 inode->i_op = &reiserfs_special_inode_operations;
686 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 //FIXME: needed for block and char devices only
689 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691 reiserfs_update_inode_transaction(inode);
692 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700694 retval =
695 reiserfs_add_entry(&th, dir, dentry->d_name.name,
696 dentry->d_name.len, inode, 1 /*visible */ );
697 if (retval) {
698 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200699 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 reiserfs_update_sd(&th, inode);
701 err = journal_end(&th, dir->i_sb, jbegin_count);
702 if (err)
703 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500704 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 iput(inode);
706 goto out_failed;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Al Viroc1eaa262008-12-30 02:03:58 -0500709 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400710 d_instantiate(dentry, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700711 retval = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700713 out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714 reiserfs_write_unlock(dir->i_sb);
715 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Al Viro18bb1db2011-07-26 01:41:39 -0400718static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 int retval;
721 struct inode *inode;
722 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400723 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700724 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
725 int jbegin_count =
726 JOURNAL_PER_BALANCE_CNT * 3 +
727 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
728 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Christoph Hellwig871a2932010-03-03 09:05:07 -0500730 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
734 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 mode = S_IFDIR | mode;
737 if (!(inode = new_inode(dir->i_sb))) {
738 return -ENOMEM;
739 }
740 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400742 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500743 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400744 if (retval < 0) {
745 drop_new_inode(inode);
746 return retval;
747 }
748 jbegin_count += retval;
Jeff Mahoney278f6672013-08-08 17:34:46 -0400749 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751 retval = journal_begin(&th, dir->i_sb, jbegin_count);
752 if (retval) {
753 drop_new_inode(inode);
754 goto out_failed;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757 /* inc the link count now, so another writer doesn't overflow it while
758 ** we sleep later on.
759 */
760 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
763 old_format_only(dir->i_sb) ?
764 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400765 dentry, inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766 if (retval) {
Al Viro99890a32011-03-02 09:35:13 -0500767 DEC_DIR_INODE_NLINK(dir)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 goto out_failed;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700771 reiserfs_update_inode_transaction(inode);
772 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774 inode->i_op = &reiserfs_dir_inode_operations;
775 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700777 // note, _this_ add_entry will not update dir's stat data
778 retval =
779 reiserfs_add_entry(&th, dir, dentry->d_name.name,
780 dentry->d_name.len, inode, 1 /*visible */ );
781 if (retval) {
782 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200783 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700784 DEC_DIR_INODE_NLINK(dir);
785 reiserfs_update_sd(&th, inode);
786 err = journal_end(&th, dir->i_sb, jbegin_count);
787 if (err)
788 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500789 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700790 iput(inode);
791 goto out_failed;
792 }
793 // the above add_entry did not update dir's stat data
794 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Al Viroc1eaa262008-12-30 02:03:58 -0500796 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400797 d_instantiate(dentry, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700798 retval = journal_end(&th, dir->i_sb, jbegin_count);
Frederic Weisbeckerb10ab4c2009-08-25 02:44:21 +0200799out_failed:
Jeff Mahoney278f6672013-08-08 17:34:46 -0400800 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700801 return retval;
802}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700804static inline int reiserfs_empty_dir(struct inode *inode)
805{
806 /* we can cheat because an old format dir cannot have
807 ** EMPTY_DIR_SIZE, and a new format dir cannot have
Jeff Mahoney0222e652009-03-30 14:02:44 -0400808 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700809 ** regardless of disk format version, the directory is empty.
810 */
811 if (inode->i_size != EMPTY_DIR_SIZE &&
812 inode->i_size != EMPTY_DIR_SIZE_V1) {
813 return 0;
814 }
815 return 1;
816}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
819{
820 int retval, err;
821 struct inode *inode;
822 struct reiserfs_transaction_handle th;
823 int jbegin_count;
824 INITIALIZE_PATH(path);
825 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 /* we will be doing 2 balancings and update 2 stat data, we change quotas
828 * of the owner of the directory and of the owner of the parent directory.
829 * The quota structure is possibly deleted only on last iput => outside
830 * of this transaction */
831 jbegin_count =
832 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
833 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Christoph Hellwig871a2932010-03-03 09:05:07 -0500835 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500836
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 reiserfs_write_lock(dir->i_sb);
838 retval = journal_begin(&th, dir->i_sb, jbegin_count);
839 if (retval)
840 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700842 de.de_gen_number_bit_string = NULL;
843 if ((retval =
844 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
845 &path, &de)) == NAME_NOT_FOUND) {
846 retval = -ENOENT;
847 goto end_rmdir;
848 } else if (retval == IO_ERROR) {
849 retval = -EIO;
850 goto end_rmdir;
851 }
852
853 inode = dentry->d_inode;
854
855 reiserfs_update_inode_transaction(inode);
856 reiserfs_update_inode_transaction(dir);
857
858 if (de.de_objectid != inode->i_ino) {
859 // FIXME: compare key of an object and a key found in the
860 // entry
861 retval = -EIO;
862 goto end_rmdir;
863 }
864 if (!reiserfs_empty_dir(inode)) {
865 retval = -ENOTEMPTY;
866 goto end_rmdir;
867 }
868
869 /* cut entry from dir directory */
870 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
871 0 /*new file size - not used here */ );
872 if (retval < 0)
873 goto end_rmdir;
874
875 if (inode->i_nlink != 2 && inode->i_nlink != 1)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400876 reiserfs_error(inode->i_sb, "reiserfs-7040",
877 "empty directory has nlink != 2 (%d)",
878 inode->i_nlink);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700879
Dave Hansence71ec32006-09-30 23:29:06 -0700880 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700881 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
882 reiserfs_update_sd(&th, inode);
883
884 DEC_DIR_INODE_NLINK(dir)
885 dir->i_size -= (DEH_SIZE + de.de_entrylen);
886 reiserfs_update_sd(&th, dir);
887
888 /* prevent empty directory from getting lost */
889 add_save_link(&th, inode, 0 /* not truncate */ );
890
891 retval = journal_end(&th, dir->i_sb, jbegin_count);
892 reiserfs_check_path(&path);
893 out_rmdir:
894 reiserfs_write_unlock(dir->i_sb);
895 return retval;
896
897 end_rmdir:
898 /* we must release path, because we did not call
899 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
900 release path if operation was not complete */
901 pathrelse(&path);
902 err = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 reiserfs_write_unlock(dir->i_sb);
904 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700907static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
908{
909 int retval, err;
910 struct inode *inode;
911 struct reiserfs_dir_entry de;
912 INITIALIZE_PATH(path);
913 struct reiserfs_transaction_handle th;
914 int jbegin_count;
915 unsigned long savelink;
916
Christoph Hellwig871a2932010-03-03 09:05:07 -0500917 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500918
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700919 inode = dentry->d_inode;
920
921 /* in this transaction we can be doing at max two balancings and update
922 * two stat datas, we change quotas of the owner of the directory and of
923 * the owner of the parent directory. The quota structure is possibly
924 * deleted only on iput => outside of this transaction */
925 jbegin_count =
926 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
927 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
928
Jeff Mahoney278f6672013-08-08 17:34:46 -0400929 reiserfs_write_lock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700930 retval = journal_begin(&th, dir->i_sb, jbegin_count);
931 if (retval)
932 goto out_unlink;
933
934 de.de_gen_number_bit_string = NULL;
935 if ((retval =
936 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
937 &path, &de)) == NAME_NOT_FOUND) {
938 retval = -ENOENT;
939 goto end_unlink;
940 } else if (retval == IO_ERROR) {
941 retval = -EIO;
942 goto end_unlink;
943 }
944
945 reiserfs_update_inode_transaction(inode);
946 reiserfs_update_inode_transaction(dir);
947
948 if (de.de_objectid != inode->i_ino) {
949 // FIXME: compare key of an object and a key found in the
950 // entry
951 retval = -EIO;
952 goto end_unlink;
953 }
954
955 if (!inode->i_nlink) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400956 reiserfs_warning(inode->i_sb, "reiserfs-7042",
957 "deleting nonexistent file (%lu), %d",
958 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200959 set_nlink(inode, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700960 }
961
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700962 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700963
964 /*
965 * we schedule before doing the add_save_link call, save the link
966 * count so we don't race
967 */
968 savelink = inode->i_nlink;
969
970 retval =
971 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
972 0);
973 if (retval < 0) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700974 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700975 goto end_unlink;
976 }
977 inode->i_ctime = CURRENT_TIME_SEC;
978 reiserfs_update_sd(&th, inode);
979
980 dir->i_size -= (de.de_entrylen + DEH_SIZE);
981 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
982 reiserfs_update_sd(&th, dir);
983
984 if (!savelink)
985 /* prevent file from getting lost */
986 add_save_link(&th, inode, 0 /* not truncate */ );
987
988 retval = journal_end(&th, dir->i_sb, jbegin_count);
989 reiserfs_check_path(&path);
Jeff Mahoney278f6672013-08-08 17:34:46 -0400990 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700991 return retval;
992
993 end_unlink:
994 pathrelse(&path);
995 err = journal_end(&th, dir->i_sb, jbegin_count);
996 reiserfs_check_path(&path);
997 if (err)
998 retval = err;
999 out_unlink:
Jeff Mahoney278f6672013-08-08 17:34:46 -04001000 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 return retval;
1002}
1003
1004static int reiserfs_symlink(struct inode *parent_dir,
1005 struct dentry *dentry, const char *symname)
1006{
1007 int retval;
1008 struct inode *inode;
1009 char *name;
1010 int item_len;
1011 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001012 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001013 int mode = S_IFLNK | S_IRWXUGO;
1014 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1015 int jbegin_count =
1016 JOURNAL_PER_BALANCE_CNT * 3 +
1017 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1018 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1019
Christoph Hellwig871a2932010-03-03 09:05:07 -05001020 dquot_initialize(parent_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001021
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001022 if (!(inode = new_inode(parent_dir->i_sb))) {
1023 return -ENOMEM;
1024 }
1025 new_inode_init(inode, parent_dir, mode);
1026
Eric Paris2a7dba32011-02-01 11:05:39 -05001027 retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1028 &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001029 if (retval < 0) {
1030 drop_new_inode(inode);
1031 return retval;
1032 }
1033 jbegin_count += retval;
1034
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001035 reiserfs_write_lock(parent_dir->i_sb);
1036 item_len = ROUND_UP(strlen(symname));
1037 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1038 retval = -ENAMETOOLONG;
1039 drop_new_inode(inode);
1040 goto out_failed;
1041 }
1042
Pekka Enbergd739b422006-02-01 03:06:43 -08001043 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 if (!name) {
1045 drop_new_inode(inode);
1046 retval = -ENOMEM;
1047 goto out_failed;
1048 }
1049 memcpy(name, symname, strlen(symname));
1050 padd_item(name, item_len, strlen(symname));
1051
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001052 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1053 if (retval) {
1054 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001055 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056 goto out_failed;
1057 }
1058
1059 retval =
1060 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001061 dentry, inode, &security);
Pekka Enbergd739b422006-02-01 03:06:43 -08001062 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063 if (retval) { /* reiserfs_new_inode iputs for us */
1064 goto out_failed;
1065 }
1066
1067 reiserfs_update_inode_transaction(inode);
1068 reiserfs_update_inode_transaction(parent_dir);
1069
1070 inode->i_op = &reiserfs_symlink_inode_operations;
1071 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1072
1073 // must be sure this inode is written with this transaction
1074 //
1075 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1076
1077 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1078 dentry->d_name.len, inode, 1 /*visible */ );
1079 if (retval) {
1080 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001081 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001082 reiserfs_update_sd(&th, inode);
1083 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1084 if (err)
1085 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -05001086 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087 iput(inode);
1088 goto out_failed;
1089 }
1090
Al Viroc1eaa262008-12-30 02:03:58 -05001091 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04001092 d_instantiate(dentry, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001093 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1094 out_failed:
1095 reiserfs_write_unlock(parent_dir->i_sb);
1096 return retval;
1097}
1098
1099static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1100 struct dentry *dentry)
1101{
1102 int retval;
1103 struct inode *inode = old_dentry->d_inode;
1104 struct reiserfs_transaction_handle th;
1105 /* We need blocks for transaction + update of quotas for the owners of the directory */
1106 int jbegin_count =
1107 JOURNAL_PER_BALANCE_CNT * 3 +
1108 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1109
Christoph Hellwig871a2932010-03-03 09:05:07 -05001110 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001111
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001112 reiserfs_write_lock(dir->i_sb);
1113 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1114 //FIXME: sd_nlink is 32 bit for new files
1115 reiserfs_write_unlock(dir->i_sb);
1116 return -EMLINK;
1117 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001118
1119 /* inc before scheduling so reiserfs_unlink knows we are here */
Dave Hansend8c76e62006-09-30 23:29:04 -07001120 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001121
1122 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1123 if (retval) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001124 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001125 reiserfs_write_unlock(dir->i_sb);
1126 return retval;
1127 }
1128
1129 /* create new entry */
1130 retval =
1131 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1132 dentry->d_name.len, inode, 1 /*visible */ );
1133
1134 reiserfs_update_inode_transaction(inode);
1135 reiserfs_update_inode_transaction(dir);
1136
1137 if (retval) {
1138 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001139 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001140 err = journal_end(&th, dir->i_sb, jbegin_count);
1141 reiserfs_write_unlock(dir->i_sb);
1142 return err ? err : retval;
1143 }
1144
1145 inode->i_ctime = CURRENT_TIME_SEC;
1146 reiserfs_update_sd(&th, inode);
1147
Al Viro7de9c6ee2010-10-23 11:11:40 -04001148 ihold(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 d_instantiate(dentry, inode);
1150 retval = journal_end(&th, dir->i_sb, jbegin_count);
1151 reiserfs_write_unlock(dir->i_sb);
1152 return retval;
1153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jeff Mahoney0222e652009-03-30 14:02:44 -04001155/* de contains information pointing to an entry which */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001156static int de_still_valid(const char *name, int len,
1157 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001159 struct reiserfs_dir_entry tmp = *de;
1160
1161 // recalculate pointer to name and name length
1162 set_de_name_and_namelen(&tmp);
1163 // FIXME: could check more
1164 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1165 return 0;
1166 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001169static int entry_points_to_object(const char *name, int len,
1170 struct reiserfs_dir_entry *de,
1171 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001173 if (!de_still_valid(name, len, de))
1174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001176 if (inode) {
1177 if (!de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001178 reiserfs_panic(inode->i_sb, "vs-7042",
1179 "entry must be visible");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 /* this must be added hidden entry */
1184 if (de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001185 reiserfs_panic(NULL, "vs-7043", "entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001187 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1192 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001194 /* JDM These operations are endian safe - both are le */
1195 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1196 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Jeff Mahoney0222e652009-03-30 14:02:44 -04001199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * process, that is going to call fix_nodes/do_balance must hold only
1201 * one path. If it holds 2 or more, it can get into endless waiting in
Jeff Mahoney0222e652009-03-30 14:02:44 -04001202 * get_empty_nodes or its clones
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1205 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001207 int retval;
1208 INITIALIZE_PATH(old_entry_path);
1209 INITIALIZE_PATH(new_entry_path);
1210 INITIALIZE_PATH(dot_dot_entry_path);
1211 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1212 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1213 struct inode *old_inode, *new_dentry_inode;
1214 struct reiserfs_transaction_handle th;
1215 int jbegin_count;
1216 umode_t old_inode_mode;
1217 unsigned long savelink = 1;
1218 struct timespec ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001220 /* three balancings: (1) old name removal, (2) new name insertion
1221 and (3) maybe "save" link insertion
1222 stat data updates: (1) old directory,
1223 (2) new directory and (3) maybe old object stat data (when it is
1224 directory) and (4) maybe stat data of object to which new entry
1225 pointed initially and (5) maybe block containing ".." of
1226 renamed directory
1227 quota updates: two parent directories */
1228 jbegin_count =
1229 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1230 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Christoph Hellwig871a2932010-03-03 09:05:07 -05001232 dquot_initialize(old_dir);
1233 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001235 old_inode = old_dentry->d_inode;
1236 new_dentry_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001238 // make sure, that oldname still exists and points to an object we
1239 // are going to rename
1240 old_de.de_gen_number_bit_string = NULL;
1241 reiserfs_write_lock(old_dir->i_sb);
1242 retval =
1243 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1244 old_dentry->d_name.len, &old_entry_path,
1245 &old_de);
1246 pathrelse(&old_entry_path);
1247 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 reiserfs_write_unlock(old_dir->i_sb);
1249 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001251
1252 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1253 reiserfs_write_unlock(old_dir->i_sb);
1254 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001256
1257 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001259 // make sure, that directory being renamed has correct ".."
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001260 // and that its new parent directory has not too many links
1261 // already
1262
1263 if (new_dentry_inode) {
1264 if (!reiserfs_empty_dir(new_dentry_inode)) {
1265 reiserfs_write_unlock(old_dir->i_sb);
1266 return -ENOTEMPTY;
1267 }
1268 }
1269
Jeff Mahoney0222e652009-03-30 14:02:44 -04001270 /* directory is renamed, its parent directory will be changed,
1271 ** so find ".." entry
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 */
1273 dot_dot_de.de_gen_number_bit_string = NULL;
1274 retval =
1275 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1276 &dot_dot_de);
1277 pathrelse(&dot_dot_entry_path);
1278 if (retval != NAME_FOUND) {
1279 reiserfs_write_unlock(old_dir->i_sb);
1280 return -EIO;
1281 }
1282
1283 /* inode number of .. must equal old_dir->i_ino */
1284 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1285 reiserfs_write_unlock(old_dir->i_sb);
1286 return -EIO;
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001290 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1291 if (retval) {
1292 reiserfs_write_unlock(old_dir->i_sb);
1293 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001296 /* add new entry (or find the existing one) */
1297 retval =
1298 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1299 new_dentry->d_name.len, old_inode, 0);
1300 if (retval == -EEXIST) {
1301 if (!new_dentry_inode) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001302 reiserfs_panic(old_dir->i_sb, "vs-7050",
1303 "new entry is found, new inode == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001304 }
1305 } else if (retval) {
1306 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1307 reiserfs_write_unlock(old_dir->i_sb);
1308 return err ? err : retval;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001311 reiserfs_update_inode_transaction(old_dir);
1312 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001314 /* this makes it so an fsync on an open fd for the old name will
1315 ** commit the rename operation
1316 */
1317 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001319 if (new_dentry_inode)
1320 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001322 while (1) {
1323 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1324 if ((retval =
1325 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1326 &old_entry_path,
1327 &old_de)) != NAME_FOUND) {
1328 pathrelse(&old_entry_path);
1329 journal_end(&th, old_dir->i_sb, jbegin_count);
1330 reiserfs_write_unlock(old_dir->i_sb);
1331 return -EIO;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001334 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1337
1338 // look for new name by reiserfs_find_entry
1339 new_de.de_gen_number_bit_string = NULL;
1340 retval =
1341 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1342 new_dentry->d_name.len, &new_entry_path,
1343 &new_de);
1344 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1345 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1346 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1347 pathrelse(&new_entry_path);
1348 pathrelse(&old_entry_path);
1349 journal_end(&th, old_dir->i_sb, jbegin_count);
1350 reiserfs_write_unlock(old_dir->i_sb);
1351 return -EIO;
1352 }
1353
1354 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1355
1356 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1357
1358 if (S_ISDIR(old_inode->i_mode)) {
1359 if ((retval =
1360 search_by_entry_key(new_dir->i_sb,
1361 &dot_dot_de.de_entry_key,
1362 &dot_dot_entry_path,
1363 &dot_dot_de)) != NAME_FOUND) {
1364 pathrelse(&dot_dot_entry_path);
1365 pathrelse(&new_entry_path);
1366 pathrelse(&old_entry_path);
1367 journal_end(&th, old_dir->i_sb, jbegin_count);
1368 reiserfs_write_unlock(old_dir->i_sb);
1369 return -EIO;
1370 }
1371 copy_item_head(&dot_dot_ih,
1372 get_ih(&dot_dot_entry_path));
1373 // node containing ".." gets into transaction
1374 reiserfs_prepare_for_journal(old_inode->i_sb,
1375 dot_dot_de.de_bh, 1);
1376 }
1377 /* we should check seals here, not do
1378 this stuff, yes? Then, having
1379 gathered everything into RAM we
1380 should lock the buffers, yes? -Hans */
Jeff Mahoney0222e652009-03-30 14:02:44 -04001381 /* probably. our rename needs to hold more
1382 ** than one path at once. The seals would
1383 ** have to be written to deal with multi-path
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 ** issues -chris
1385 */
1386 /* sanity checking before doing the rename - avoid races many
1387 ** of the above checks could have scheduled. We have to be
1388 ** sure our items haven't been shifted by another process.
1389 */
1390 if (item_moved(&new_entry_ih, &new_entry_path) ||
1391 !entry_points_to_object(new_dentry->d_name.name,
1392 new_dentry->d_name.len,
1393 &new_de, new_dentry_inode) ||
1394 item_moved(&old_entry_ih, &old_entry_path) ||
1395 !entry_points_to_object(old_dentry->d_name.name,
1396 old_dentry->d_name.len,
1397 &old_de, old_inode)) {
1398 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1399 new_de.de_bh);
1400 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1401 old_de.de_bh);
1402 if (S_ISDIR(old_inode_mode))
1403 reiserfs_restore_prepared_buffer(old_inode->
1404 i_sb,
1405 dot_dot_de.
1406 de_bh);
1407 continue;
1408 }
1409 if (S_ISDIR(old_inode_mode)) {
1410 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1411 !entry_points_to_object("..", 2, &dot_dot_de,
1412 old_dir)) {
1413 reiserfs_restore_prepared_buffer(old_inode->
1414 i_sb,
1415 old_de.de_bh);
1416 reiserfs_restore_prepared_buffer(old_inode->
1417 i_sb,
1418 new_de.de_bh);
1419 reiserfs_restore_prepared_buffer(old_inode->
1420 i_sb,
1421 dot_dot_de.
1422 de_bh);
1423 continue;
1424 }
1425 }
1426
1427 RFALSE(S_ISDIR(old_inode_mode) &&
1428 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1429
1430 break;
1431 }
1432
1433 /* ok, all the changes can be done in one fell swoop when we
1434 have claimed all the buffers needed. */
1435
1436 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1437 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1438 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1439
1440 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1441 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1442 ctime = CURRENT_TIME_SEC;
1443 old_dir->i_ctime = old_dir->i_mtime = ctime;
1444 new_dir->i_ctime = new_dir->i_mtime = ctime;
1445 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1446 renamed object */
1447 old_inode->i_ctime = ctime;
1448
1449 if (new_dentry_inode) {
1450 // adjust link number of the victim
1451 if (S_ISDIR(new_dentry_inode->i_mode)) {
Dave Hansence71ec32006-09-30 23:29:06 -07001452 clear_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001453 } else {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001454 drop_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001455 }
1456 new_dentry_inode->i_ctime = ctime;
1457 savelink = new_dentry_inode->i_nlink;
1458 }
1459
1460 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001461 /* adjust ".." of renamed directory */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001462 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1463 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1464
1465 if (!new_dentry_inode)
1466 /* there (in new_dir) was no directory, so it got new link
1467 (".." of renamed directory) */
1468 INC_DIR_INODE_NLINK(new_dir);
1469
1470 /* old directory lost one link - ".. " of renamed directory */
1471 DEC_DIR_INODE_NLINK(old_dir);
1472 }
1473 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1474 pathrelse(&new_entry_path);
1475 pathrelse(&dot_dot_entry_path);
1476
1477 // FIXME: this reiserfs_cut_from_item's return value may screw up
1478 // anybody, but it will panic if will not be able to find the
1479 // entry. This needs one more clean up
1480 if (reiserfs_cut_from_item
1481 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1482 0) < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -04001483 reiserfs_error(old_dir->i_sb, "vs-7060",
1484 "couldn't not cut old name. Fsck later?");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001485
1486 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1487
1488 reiserfs_update_sd(&th, old_dir);
1489 reiserfs_update_sd(&th, new_dir);
1490 reiserfs_update_sd(&th, old_inode);
1491
1492 if (new_dentry_inode) {
1493 if (savelink == 0)
1494 add_save_link(&th, new_dentry_inode,
1495 0 /* not truncate */ );
1496 reiserfs_update_sd(&th, new_dentry_inode);
1497 }
1498
1499 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1500 reiserfs_write_unlock(old_dir->i_sb);
1501 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504/*
1505 * directories can handle most operations...
1506 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001507const struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001508 //&reiserfs_dir_operations, /* default_file_ops */
1509 .create = reiserfs_create,
1510 .lookup = reiserfs_lookup,
1511 .link = reiserfs_link,
1512 .unlink = reiserfs_unlink,
1513 .symlink = reiserfs_symlink,
1514 .mkdir = reiserfs_mkdir,
1515 .rmdir = reiserfs_rmdir,
1516 .mknod = reiserfs_mknod,
1517 .rename = reiserfs_rename,
1518 .setattr = reiserfs_setattr,
1519 .setxattr = reiserfs_setxattr,
1520 .getxattr = reiserfs_getxattr,
1521 .listxattr = reiserfs_listxattr,
1522 .removexattr = reiserfs_removexattr,
1523 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001524 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -08001525 .set_acl = reiserfs_set_acl,
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544/*
1545 * special file operations.. just xattr/acl stuff
1546 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001547const struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001548 .setattr = reiserfs_setattr,
1549 .setxattr = reiserfs_setxattr,
1550 .getxattr = reiserfs_getxattr,
1551 .listxattr = reiserfs_listxattr,
1552 .removexattr = reiserfs_removexattr,
1553 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001554 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -08001555 .set_acl = reiserfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556};