blob: e6a2b406af367071920a332bf6dc62a5d72e8fcf [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
Jeff Mahoney098297b2014-04-23 10:00:36 -040025/*
26 * directory item contains array of entry headers. This performs
27 * binary search through that array
28 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070029static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070031 struct item_head *ih = de->de_ih;
32 struct reiserfs_de_head *deh = de->de_deh;
33 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070035 lbound = 0;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040036 rbound = ih_entry_count(ih) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 for (j = (rbound + lbound) / 2; lbound <= rbound;
39 j = (rbound + lbound) / 2) {
40 if (off < deh_offset(deh + j)) {
41 rbound = j - 1;
42 continue;
43 }
44 if (off > deh_offset(deh + j)) {
45 lbound = j + 1;
46 continue;
47 }
Jeff Mahoney098297b2014-04-23 10:00:36 -040048 /* this is not name found, but matched third key component */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070049 de->de_entry_num = j;
50 return NAME_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 de->de_entry_num = lbound;
54 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Jeff Mahoney098297b2014-04-23 10:00:36 -040057/*
58 * comment? maybe something like set de to point to what the path points to?
59 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060static inline void set_de_item_location(struct reiserfs_dir_entry *de,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -080061 struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 de->de_bh = get_last_bh(path);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040064 de->de_ih = tp_item_head(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
66 de->de_item_num = PATH_LAST_POSITION(path);
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Mahoney098297b2014-04-23 10:00:36 -040069/*
70 * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
71 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070074 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Sesterhenn14a61442006-10-03 23:36:38 +020076 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070078 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
79 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -040080 de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 if (de->de_name[de->de_namelen - 1] == 0)
82 de->de_namelen = strlen(de->de_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Jeff Mahoney098297b2014-04-23 10:00:36 -040085/* what entry points to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086static inline void set_de_object_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Sesterhenn14a61442006-10-03 23:36:38 +020088 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Jeff Mahoneya228bf82014-04-23 10:00:42 -040089 de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
90 de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric Sesterhenn14a61442006-10-03 23:36:38 +020097 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099 /* store key of the found entry */
100 de->de_entry_key.version = KEY_FORMAT_3_5;
101 de->de_entry_key.on_disk_key.k_dir_id =
102 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
103 de->de_entry_key.on_disk_key.k_objectid =
104 le32_to_cpu(de->de_ih->ih_key.k_objectid);
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400105 set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
106 set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Jeff Mahoney098297b2014-04-23 10:00:36 -0400109/*
110 * We assign a key to each directory item, and place multiple entries in a
111 * single directory item. A directory item has a key equal to the key of
112 * the first directory entry in it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jeff Mahoney098297b2014-04-23 10:00:36 -0400114 * This function first calls search_by_key, then, if item whose first entry
115 * matches is not found it looks for the entry inside directory item found
116 * by search_by_key. Fills the path to the entry, and to the entry position
117 * in the item
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800121 struct treepath *path, struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 retval = search_item(sb, key, path);
126 switch (retval) {
127 case ITEM_NOT_FOUND:
128 if (!PATH_LAST_POSITION(path)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400129 reiserfs_error(sb, "vs-7000", "search_by_key "
130 "returned item position == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131 pathrelse(path);
132 return IO_ERROR;
133 }
134 PATH_LAST_POSITION(path)--;
135
136 case ITEM_FOUND:
137 break;
138
139 case IO_ERROR:
140 return retval;
141
142 default:
143 pathrelse(path);
Jeff Mahoney0030b642009-03-30 14:02:28 -0400144 reiserfs_error(sb, "vs-7002", "no path to here");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 if (!is_direntry_le_ih(de->de_ih) ||
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400152 COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 print_block(de->de_bh, 0, -1, -1);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400154 reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
155 "item or does not belong to the same directory "
156 "as key %K", de->de_ih, key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 }
158#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jeff Mahoney098297b2014-04-23 10:00:36 -0400160 /*
161 * binary search in directory item by third component of the
162 * key. sets de->de_entry_num of de
163 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
165 path->pos_in_item = de->de_entry_num;
166 if (retval != NAME_NOT_FOUND) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400167 /*
168 * ugly, but rename needs de_bh, de_deh, de_name,
169 * de_namelen, de_objectid set
170 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 set_de_name_and_namelen(de);
172 set_de_object_key(de);
173 }
174 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
178
Jeff Mahoney098297b2014-04-23 10:00:36 -0400179/*
180 * The third component is hashed, and you can choose from more than
181 * one hash function. Per directory hashes are not yet implemented
182 * but are thought about. This function should be moved to hashes.c
183 * Jedi, please do so. -Hans
184 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185static __u32 get_third_component(struct super_block *s,
186 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 if (!len || (len == 1 && name[0] == '.'))
191 return DOT_OFFSET;
192 if (len == 2 && name[0] == '.' && name[1] == '.')
193 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700195 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jeff Mahoney098297b2014-04-23 10:00:36 -0400197 /* take bits from 7-th to 30-th including both bounds */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198 res = GET_HASH_VALUE(res);
199 if (res == 0)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400200 /*
201 * needed to have no names before "." and ".." those have hash
202 * value == 0 and generation conters 1 and 2 accordingly
203 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 res = 128;
205 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208static int reiserfs_match(struct reiserfs_dir_entry *de,
209 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700211 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700213 if ((namelen == de->de_namelen) &&
214 !memcmp(de->de_name, name, de->de_namelen))
215 retval =
216 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
217 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
223
Jeff Mahoney098297b2014-04-23 10:00:36 -0400224/* used when hash collisions exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226static int linear_search_in_dir_item(struct cpu_key *key,
227 struct reiserfs_dir_entry *de,
228 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230 struct reiserfs_de_head *deh = de->de_deh;
231 int retval;
232 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400236 if (i == ih_entry_count(de->de_ih) ||
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 GET_HASH_VALUE(deh_offset(deh + i)) !=
238 GET_HASH_VALUE(cpu_key_k_offset(key))) {
239 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
243 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247 for (; i >= 0; i--, deh--) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400248 /* hash value does not match, no need to check whole name */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 if (GET_HASH_VALUE(deh_offset(deh)) !=
250 GET_HASH_VALUE(cpu_key_k_offset(key))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700251 return NAME_NOT_FOUND;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jeff Mahoney098297b2014-04-23 10:00:36 -0400254 /* mark that this generation number is used */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 if (de->de_gen_number_bit_string)
256 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500257 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Jeff Mahoney098297b2014-04-23 10:00:36 -0400259 /* calculate pointer to name and namelen */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 de->de_entry_num = i;
261 set_de_name_and_namelen(de);
262
Jeff Mahoney098297b2014-04-23 10:00:36 -0400263 /*
264 * de's de_name, de_namelen, de_recordlen are set.
265 * Fill the rest.
266 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267 if ((retval =
268 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700269
Jeff Mahoney098297b2014-04-23 10:00:36 -0400270 /* key of pointed object */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700271 set_de_object_key(de);
272
273 store_de_entry_key(de);
274
Jeff Mahoney098297b2014-04-23 10:00:36 -0400275 /* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700276 return retval;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400281 /*
282 * we have reached left most entry in the node. In common we
283 * have to go to the left neighbor, but if generation counter
284 * is 0 already, we know for sure, that there is no name with
285 * the same hash value
286 */
287 /*
288 * FIXME: this work correctly only because hash value can not
289 * be 0. Btw, in case of Yura's hash it is probably possible,
290 * so, this is a bug
291 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 RFALSE(de->de_item_num,
295 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700297 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Jeff Mahoney098297b2014-04-23 10:00:36 -0400300/*
301 * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
302 * FIXME: should add something like IOERROR
303 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700304static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800305 struct treepath *path_to_entry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308 struct cpu_key key_to_search;
309 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700311 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
312 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 /* we will search for this key in the tree */
315 make_cpu_key(&key_to_search, dir,
316 get_third_component(dir->i_sb, name, namelen),
317 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700319 while (1) {
320 retval =
321 search_by_entry_key(dir->i_sb, &key_to_search,
322 path_to_entry, de);
323 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400324 reiserfs_error(dir->i_sb, "zam-7001", "io error");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325 return IO_ERROR;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700328 /* compare names for all entries having given hash value */
329 retval =
330 linear_search_in_dir_item(&key_to_search, de, name,
331 namelen);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400332 /*
333 * there is no need to scan directory anymore.
334 * Given entry found or does not exist
335 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700336 if (retval != GOTO_PREVIOUS_ITEM) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700337 path_to_entry->pos_in_item = de->de_entry_num;
338 return retval;
339 }
340
Jeff Mahoney098297b2014-04-23 10:00:36 -0400341 /*
342 * there is left neighboring item of this directory
343 * and given entry can be there
344 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700345 set_cpu_key_k_offset(&key_to_search,
346 le_ih_k_offset(de->de_ih) - 1);
347 pathrelse(path_to_entry);
348
349 } /* while (1) */
350}
351
352static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400353 unsigned int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354{
355 int retval;
356 struct inode *inode = NULL;
357 struct reiserfs_dir_entry de;
358 INITIALIZE_PATH(path_to_entry);
359
360 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
361 return ERR_PTR(-ENAMETOOLONG);
362
Jeff Mahoney278f6672013-08-08 17:34:46 -0400363 reiserfs_write_lock(dir->i_sb);
Frederic Weisbeckerb1c839b2009-05-08 01:05:06 +0200364
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700365 de.de_gen_number_bit_string = NULL;
366 retval =
367 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
368 &path_to_entry, &de);
369 pathrelse(&path_to_entry);
370 if (retval == NAME_FOUND) {
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400371 inode = reiserfs_iget(dir->i_sb,
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400372 (struct cpu_key *)&de.de_dir_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373 if (!inode || IS_ERR(inode)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -0400374 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 return ERR_PTR(-EACCES);
376 }
377
Jeff Mahoney098297b2014-04-23 10:00:36 -0400378 /*
379 * Propagate the private flag so we know we're
380 * in the priv tree
381 */
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400382 if (IS_PRIVATE(dir))
383 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700384 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400385 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Pekka Enbergd9975d62006-01-14 13:21:09 -0800390 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Jeff Mahoney0222e652009-03-30 14:02:44 -0400393/*
Jeff Mahoney098297b2014-04-23 10:00:36 -0400394 * looks up the dentry of the parent directory for child.
395 * taken from ext2_get_parent
396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397struct dentry *reiserfs_get_parent(struct dentry *child)
398{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 int retval;
400 struct inode *inode = NULL;
401 struct reiserfs_dir_entry de;
402 INITIALIZE_PATH(path_to_entry);
David Howells2b0143b2015-03-17 22:25:59 +0000403 struct inode *dir = d_inode(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700405 if (dir->i_nlink == 0) {
406 return ERR_PTR(-ENOENT);
407 }
408 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700410 reiserfs_write_lock(dir->i_sb);
411 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
412 pathrelse(&path_to_entry);
413 if (retval != NAME_FOUND) {
414 reiserfs_write_unlock(dir->i_sb);
415 return ERR_PTR(-ENOENT);
416 }
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400417 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christoph Hellwig44003722008-08-11 15:49:04 +0200420 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Jeff Mahoney0222e652009-03-30 14:02:44 -0400423/* add entry to the directory (entry can be hidden).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425insert definition of when hidden directories are used here -Hans
426
427 Does not mark dir inode dirty, do it after successesfull call to it */
428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
430 struct inode *dir, const char *name, int namelen,
431 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433 struct cpu_key entry_key;
434 struct reiserfs_de_head *deh;
435 INITIALIZE_PATH(path);
436 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500437 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438 int gen_number;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400439
440 /*
441 * 48 bytes now and we avoid kmalloc if we
442 * create file with short name
443 */
444 char small_buf[32 + DEH_SIZE];
445
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446 char *buffer;
447 int buflen, paste_size;
448 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700452 /* cannot allow items to be added into a busy deleted directory */
453 if (!namelen)
454 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
457 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 /* each entry has unique key. compose it */
460 make_cpu_key(&entry_key, dir,
461 get_third_component(dir->i_sb, name, namelen),
462 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700464 /* get memory for composing the entry */
465 buflen = DEH_SIZE + ROUND_UP(namelen);
466 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800467 buffer = kmalloc(buflen, GFP_NOFS);
Al Viro9dce07f2008-03-29 03:07:28 +0000468 if (!buffer)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700469 return -ENOMEM;
470 } else
471 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 paste_size =
474 (get_inode_sd_version(dir) ==
475 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jeff Mahoney098297b2014-04-23 10:00:36 -0400477 /*
478 * fill buffer : directory entry head, name[, dir objectid | ,
479 * stat data | ,stat data, dir objectid ]
480 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700481 deh = (struct reiserfs_de_head *)buffer;
482 deh->deh_location = 0; /* JDM Endian safe if 0 */
483 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
484 deh->deh_state = 0; /* JDM Endian safe if 0 */
485 /* put key (ino analog) to de */
Jeff Mahoney098297b2014-04-23 10:00:36 -0400486
487 /* safe: k_dir_id is le */
488 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
489 /* safe: k_objectid is le */
490 deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 /* copy name */
493 memcpy((char *)(deh + 1), name, namelen);
494 /* padd by 0s to the 4 byte boundary */
495 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Jeff Mahoney098297b2014-04-23 10:00:36 -0400497 /*
498 * entry is ready to be pasted into tree, set 'visibility'
499 * and 'stat data in entry' attributes
500 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 mark_de_without_sd(deh);
502 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 /* find the proper place for the new entry */
505 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500506 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700507 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
508 if (retval != NAME_NOT_FOUND) {
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
513 if (retval == IO_ERROR) {
514 return -EIO;
515 }
516
517 if (retval != NAME_FOUND) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400518 reiserfs_error(dir->i_sb, "zam-7002",
519 "reiserfs_find_entry() returned "
520 "unexpected value (%d)", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700521 }
522
523 return -EEXIST;
524 }
525
526 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500527 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700528 MAX_GENERATION_NUMBER + 1);
529 if (gen_number > MAX_GENERATION_NUMBER) {
530 /* there is no free generation number */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400531 reiserfs_warning(dir->i_sb, "reiserfs-7010",
532 "Congratulations! we have got hash function "
533 "screwed up");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700534 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800535 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700536 pathrelse(&path);
537 return -EBUSY;
538 }
539 /* adjust offset of directory enrty */
540 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
541 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
542
543 /* update max-hash-collisions counter in reiserfs_sb_info */
544 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
545
Jeff Mahoney098297b2014-04-23 10:00:36 -0400546 /* we need to re-search for the insertion point */
547 if (gen_number != 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700548 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
549 NAME_NOT_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400550 reiserfs_warning(dir->i_sb, "vs-7032",
551 "entry with this key (%K) already "
552 "exists", &entry_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700553
554 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800555 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700556 pathrelse(&path);
557 return -EBUSY;
558 }
559 }
560
561 /* perform the insertion of the entry that we have prepared */
562 retval =
563 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
564 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800566 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 if (retval) {
568 reiserfs_check_path(&path);
569 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 dir->i_size += paste_size;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700573 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700574 if (!S_ISDIR(inode->i_mode) && visible)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400575 /* reiserfs_mkdir or reiserfs_rename will do that by itself */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700578 reiserfs_check_path(&path);
579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Jeff Mahoney098297b2014-04-23 10:00:36 -0400582/*
583 * quota utility function, call if you've had to abort after calling
584 * new_inode_init, and have not called reiserfs_new_inode yet.
585 * This should only be called on inodes that do not have stat data
586 * inserted into the tree yet.
587 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588static int drop_new_inode(struct inode *inode)
589{
Christoph Hellwig9f754752010-03-03 09:05:05 -0500590 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591 make_bad_inode(inode);
592 inode->i_flags |= S_NOQUOTA;
593 iput(inode);
594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Jeff Mahoney098297b2014-04-23 10:00:36 -0400597/*
598 * utility function that does setup for reiserfs_new_inode.
599 * dquot_initialize needs lots of credits so it's better to have it
600 * outside of a transaction, so we had to pull some bits of
601 * reiserfs_new_inode out into this func.
602 */
Al Viro8e071892011-07-26 02:50:53 -0400603static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400605 /*
606 * Make inode invalid - just in case we are going to drop it before
607 * the initialization happens
608 */
Jan Kara1b0a74d2005-08-13 13:15:34 +0200609 INODE_PKEY(inode)->k_objectid = 0;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400610
611 /*
612 * the quota init calls have to know who to charge the quota to, so
613 * we have to set uid and gid here
Dmitry Monakhov04b7ed02010-03-04 17:32:19 +0300614 */
615 inode_init_owner(inode, dir, mode);
Jan Kara2e6c97e2015-07-14 13:51:55 +0200616 return dquot_initialize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Al Viro4acdaf22011-07-26 01:42:34 -0400619static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400620 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622 int retval;
623 struct inode *inode;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400624 /*
625 * We need blocks for transaction + (user+group)*(quotas
626 * for new inode + update of quota for directory owner)
627 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628 int jbegin_count =
629 JOURNAL_PER_BALANCE_CNT * 2 +
630 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
631 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
632 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400633 struct reiserfs_security_handle security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jan Kara2e6c97e2015-07-14 13:51:55 +0200635 retval = dquot_initialize(dir);
636 if (retval)
637 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500638
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700639 if (!(inode = new_inode(dir->i_sb))) {
640 return -ENOMEM;
641 }
Jan Kara2e6c97e2015-07-14 13:51:55 +0200642 retval = new_inode_init(inode, dir, mode);
643 if (retval) {
644 drop_new_inode(inode);
645 return retval;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400648 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500649 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400650 if (retval < 0) {
651 drop_new_inode(inode);
652 return retval;
653 }
654 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 retval = journal_begin(&th, dir->i_sb, jbegin_count);
658 if (retval) {
659 drop_new_inode(inode);
660 goto out_failed;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700663 retval =
664 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400665 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700666 if (retval)
667 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 inode->i_op = &reiserfs_file_inode_operations;
670 inode->i_fop = &reiserfs_file_operations;
671 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700673 retval =
674 reiserfs_add_entry(&th, dir, dentry->d_name.name,
675 dentry->d_name.len, inode, 1 /*visible */ );
676 if (retval) {
677 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200678 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400680 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 if (err)
682 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500683 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700684 iput(inode);
685 goto out_failed;
686 }
687 reiserfs_update_inode_transaction(inode);
688 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Al Viroc1eaa262008-12-30 02:03:58 -0500690 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400691 d_instantiate(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400692 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400694out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695 reiserfs_write_unlock(dir->i_sb);
696 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Al Viro1a67aaf2011-07-26 01:52:52 -0400699static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700702 int retval;
703 struct inode *inode;
704 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400705 struct reiserfs_security_handle security;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400706 /*
707 * We need blocks for transaction + (user+group)*(quotas
708 * for new inode + update of quota for directory owner)
709 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 int jbegin_count =
711 JOURNAL_PER_BALANCE_CNT * 3 +
712 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
713 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Jan Kara2e6c97e2015-07-14 13:51:55 +0200715 retval = dquot_initialize(dir);
716 if (retval)
717 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500718
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 if (!(inode = new_inode(dir->i_sb))) {
720 return -ENOMEM;
721 }
Jan Kara2e6c97e2015-07-14 13:51:55 +0200722 retval = new_inode_init(inode, dir, mode);
723 if (retval) {
724 drop_new_inode(inode);
725 return retval;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400728 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500729 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400730 if (retval < 0) {
731 drop_new_inode(inode);
732 return retval;
733 }
734 jbegin_count += retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700735 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700737 retval = journal_begin(&th, dir->i_sb, jbegin_count);
738 if (retval) {
739 drop_new_inode(inode);
740 goto out_failed;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 retval =
744 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400745 inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 if (retval) {
747 goto out_failed;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700750 inode->i_op = &reiserfs_special_inode_operations;
751 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Jeff Mahoney098297b2014-04-23 10:00:36 -0400753 /* FIXME: needed for block and char devices only */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700754 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756 reiserfs_update_inode_transaction(inode);
757 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 retval =
760 reiserfs_add_entry(&th, dir, dentry->d_name.name,
761 dentry->d_name.len, inode, 1 /*visible */ );
762 if (retval) {
763 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200764 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400766 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700767 if (err)
768 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500769 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 iput(inode);
771 goto out_failed;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Al Viroc1eaa262008-12-30 02:03:58 -0500774 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400775 d_instantiate(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400776 retval = journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400778out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700779 reiserfs_write_unlock(dir->i_sb);
780 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Al Viro18bb1db2011-07-26 01:41:39 -0400783static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700785 int retval;
786 struct inode *inode;
787 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400788 struct reiserfs_security_handle security;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400789 /*
790 * We need blocks for transaction + (user+group)*(quotas
791 * for new inode + update of quota for directory owner)
792 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 int jbegin_count =
794 JOURNAL_PER_BALANCE_CNT * 3 +
795 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
796 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jan Kara2e6c97e2015-07-14 13:51:55 +0200798 retval = dquot_initialize(dir);
799 if (retval)
800 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Jeff Mahoney098297b2014-04-23 10:00:36 -0400803 /*
804 * set flag that new packing locality created and new blocks
805 * for the content of that directory are not displaced yet
806 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700807 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700809 mode = S_IFDIR | mode;
810 if (!(inode = new_inode(dir->i_sb))) {
811 return -ENOMEM;
812 }
Jan Kara2e6c97e2015-07-14 13:51:55 +0200813 retval = new_inode_init(inode, dir, mode);
814 if (retval) {
815 drop_new_inode(inode);
816 return retval;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400819 jbegin_count += reiserfs_cache_default_acl(dir);
Eric Paris2a7dba32011-02-01 11:05:39 -0500820 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400821 if (retval < 0) {
822 drop_new_inode(inode);
823 return retval;
824 }
825 jbegin_count += retval;
Jeff Mahoney278f6672013-08-08 17:34:46 -0400826 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700828 retval = journal_begin(&th, dir->i_sb, jbegin_count);
829 if (retval) {
830 drop_new_inode(inode);
831 goto out_failed;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jeff Mahoney098297b2014-04-23 10:00:36 -0400834 /*
835 * inc the link count now, so another writer doesn't overflow
836 * it while we sleep later on.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 */
838 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
841 old_format_only(dir->i_sb) ?
842 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -0400843 dentry, inode, &security);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 if (retval) {
Al Viro99890a32011-03-02 09:35:13 -0500845 DEC_DIR_INODE_NLINK(dir)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700846 goto out_failed;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700849 reiserfs_update_inode_transaction(inode);
850 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700852 inode->i_op = &reiserfs_dir_inode_operations;
853 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Jeff Mahoney098297b2014-04-23 10:00:36 -0400855 /* note, _this_ add_entry will not update dir's stat data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856 retval =
857 reiserfs_add_entry(&th, dir, dentry->d_name.name,
858 dentry->d_name.len, inode, 1 /*visible */ );
859 if (retval) {
860 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200861 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700862 DEC_DIR_INODE_NLINK(dir);
863 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400864 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700865 if (err)
866 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -0500867 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700868 iput(inode);
869 goto out_failed;
870 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400871 /* the above add_entry did not update dir's stat data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700872 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Al Viroc1eaa262008-12-30 02:03:58 -0500874 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400875 d_instantiate(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400876 retval = journal_end(&th);
Frederic Weisbeckerb10ab4c2009-08-25 02:44:21 +0200877out_failed:
Jeff Mahoney278f6672013-08-08 17:34:46 -0400878 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700879 return retval;
880}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882static inline int reiserfs_empty_dir(struct inode *inode)
883{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400884 /*
885 * we can cheat because an old format dir cannot have
886 * EMPTY_DIR_SIZE, and a new format dir cannot have
887 * EMPTY_DIR_SIZE_V1. So, if the inode is either size,
888 * regardless of disk format version, the directory is empty.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 */
890 if (inode->i_size != EMPTY_DIR_SIZE &&
891 inode->i_size != EMPTY_DIR_SIZE_V1) {
892 return 0;
893 }
894 return 1;
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700897static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
898{
899 int retval, err;
900 struct inode *inode;
901 struct reiserfs_transaction_handle th;
902 int jbegin_count;
903 INITIALIZE_PATH(path);
904 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Jeff Mahoney098297b2014-04-23 10:00:36 -0400906 /*
907 * we will be doing 2 balancings and update 2 stat data, we
908 * change quotas of the owner of the directory and of the owner
909 * of the parent directory. The quota structure is possibly
910 * deleted only on last iput => outside of this transaction
911 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700912 jbegin_count =
913 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
914 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Jan Kara2e6c97e2015-07-14 13:51:55 +0200916 retval = dquot_initialize(dir);
917 if (retval)
918 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500919
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 reiserfs_write_lock(dir->i_sb);
921 retval = journal_begin(&th, dir->i_sb, jbegin_count);
922 if (retval)
923 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700925 de.de_gen_number_bit_string = NULL;
926 if ((retval =
927 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
928 &path, &de)) == NAME_NOT_FOUND) {
929 retval = -ENOENT;
930 goto end_rmdir;
931 } else if (retval == IO_ERROR) {
932 retval = -EIO;
933 goto end_rmdir;
934 }
935
David Howells2b0143b2015-03-17 22:25:59 +0000936 inode = d_inode(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700937
938 reiserfs_update_inode_transaction(inode);
939 reiserfs_update_inode_transaction(dir);
940
941 if (de.de_objectid != inode->i_ino) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400942 /*
943 * FIXME: compare key of an object and a key found in the entry
944 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700945 retval = -EIO;
946 goto end_rmdir;
947 }
948 if (!reiserfs_empty_dir(inode)) {
949 retval = -ENOTEMPTY;
950 goto end_rmdir;
951 }
952
953 /* cut entry from dir directory */
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400954 retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
955 dir, NULL, /* page */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700956 0 /*new file size - not used here */ );
957 if (retval < 0)
958 goto end_rmdir;
959
960 if (inode->i_nlink != 2 && inode->i_nlink != 1)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400961 reiserfs_error(inode->i_sb, "reiserfs-7040",
962 "empty directory has nlink != 2 (%d)",
963 inode->i_nlink);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700964
Dave Hansence71ec32006-09-30 23:29:06 -0700965 clear_nlink(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700966 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700967 reiserfs_update_sd(&th, inode);
968
969 DEC_DIR_INODE_NLINK(dir)
970 dir->i_size -= (DEH_SIZE + de.de_entrylen);
971 reiserfs_update_sd(&th, dir);
972
973 /* prevent empty directory from getting lost */
974 add_save_link(&th, inode, 0 /* not truncate */ );
975
Jeff Mahoney58d85422014-04-23 10:00:38 -0400976 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700977 reiserfs_check_path(&path);
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400978out_rmdir:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 reiserfs_write_unlock(dir->i_sb);
980 return retval;
981
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400982end_rmdir:
Jeff Mahoney098297b2014-04-23 10:00:36 -0400983 /*
984 * we must release path, because we did not call
985 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
986 * release path if operation was not complete
987 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700988 pathrelse(&path);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400989 err = journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 reiserfs_write_unlock(dir->i_sb);
991 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700994static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
995{
996 int retval, err;
997 struct inode *inode;
998 struct reiserfs_dir_entry de;
999 INITIALIZE_PATH(path);
1000 struct reiserfs_transaction_handle th;
1001 int jbegin_count;
1002 unsigned long savelink;
1003
Jan Kara2e6c97e2015-07-14 13:51:55 +02001004 retval = dquot_initialize(dir);
1005 if (retval)
1006 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001007
David Howells2b0143b2015-03-17 22:25:59 +00001008 inode = d_inode(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001009
Jeff Mahoney098297b2014-04-23 10:00:36 -04001010 /*
1011 * in this transaction we can be doing at max two balancings and
1012 * update two stat datas, we change quotas of the owner of the
1013 * directory and of the owner of the parent directory. The quota
1014 * structure is possibly deleted only on iput => outside of
1015 * this transaction
1016 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017 jbegin_count =
1018 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
1019 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1020
Jeff Mahoney278f6672013-08-08 17:34:46 -04001021 reiserfs_write_lock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001022 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1023 if (retval)
1024 goto out_unlink;
1025
1026 de.de_gen_number_bit_string = NULL;
1027 if ((retval =
1028 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
1029 &path, &de)) == NAME_NOT_FOUND) {
1030 retval = -ENOENT;
1031 goto end_unlink;
1032 } else if (retval == IO_ERROR) {
1033 retval = -EIO;
1034 goto end_unlink;
1035 }
1036
1037 reiserfs_update_inode_transaction(inode);
1038 reiserfs_update_inode_transaction(dir);
1039
1040 if (de.de_objectid != inode->i_ino) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001041 /*
1042 * FIXME: compare key of an object and a key found in the entry
1043 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 retval = -EIO;
1045 goto end_unlink;
1046 }
1047
1048 if (!inode->i_nlink) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001049 reiserfs_warning(inode->i_sb, "reiserfs-7042",
1050 "deleting nonexistent file (%lu), %d",
1051 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001052 set_nlink(inode, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 }
1054
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001055 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056
1057 /*
1058 * we schedule before doing the add_save_link call, save the link
1059 * count so we don't race
1060 */
1061 savelink = inode->i_nlink;
1062
1063 retval =
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001064 reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001065 0);
1066 if (retval < 0) {
Dave Hansend8c76e62006-09-30 23:29:04 -07001067 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001068 goto end_unlink;
1069 }
Deepa Dinamani02027d42016-09-14 07:48:05 -07001070 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001071 reiserfs_update_sd(&th, inode);
1072
1073 dir->i_size -= (de.de_entrylen + DEH_SIZE);
Deepa Dinamani02027d42016-09-14 07:48:05 -07001074 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001075 reiserfs_update_sd(&th, dir);
1076
1077 if (!savelink)
1078 /* prevent file from getting lost */
1079 add_save_link(&th, inode, 0 /* not truncate */ );
1080
Jeff Mahoney58d85422014-04-23 10:00:38 -04001081 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001082 reiserfs_check_path(&path);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001083 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084 return retval;
1085
Jeff Mahoneycf776a72014-04-23 10:00:41 -04001086end_unlink:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087 pathrelse(&path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001088 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001089 reiserfs_check_path(&path);
1090 if (err)
1091 retval = err;
Jeff Mahoneycf776a72014-04-23 10:00:41 -04001092out_unlink:
Jeff Mahoney278f6672013-08-08 17:34:46 -04001093 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 return retval;
1095}
1096
1097static int reiserfs_symlink(struct inode *parent_dir,
1098 struct dentry *dentry, const char *symname)
1099{
1100 int retval;
1101 struct inode *inode;
1102 char *name;
1103 int item_len;
1104 struct reiserfs_transaction_handle th;
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001105 struct reiserfs_security_handle security;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 int mode = S_IFLNK | S_IRWXUGO;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001107 /*
1108 * We need blocks for transaction + (user+group)*(quotas for
1109 * new inode + update of quota for directory owner)
1110 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001111 int jbegin_count =
1112 JOURNAL_PER_BALANCE_CNT * 3 +
1113 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1114 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1115
Jan Kara2e6c97e2015-07-14 13:51:55 +02001116 retval = dquot_initialize(parent_dir);
1117 if (retval)
1118 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001120 if (!(inode = new_inode(parent_dir->i_sb))) {
1121 return -ENOMEM;
1122 }
Jan Kara2e6c97e2015-07-14 13:51:55 +02001123 retval = new_inode_init(inode, parent_dir, mode);
1124 if (retval) {
1125 drop_new_inode(inode);
1126 return retval;
1127 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001128
Eric Paris2a7dba32011-02-01 11:05:39 -05001129 retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1130 &security);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001131 if (retval < 0) {
1132 drop_new_inode(inode);
1133 return retval;
1134 }
1135 jbegin_count += retval;
1136
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001137 reiserfs_write_lock(parent_dir->i_sb);
1138 item_len = ROUND_UP(strlen(symname));
1139 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1140 retval = -ENAMETOOLONG;
1141 drop_new_inode(inode);
1142 goto out_failed;
1143 }
1144
Pekka Enbergd739b422006-02-01 03:06:43 -08001145 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001146 if (!name) {
1147 drop_new_inode(inode);
1148 retval = -ENOMEM;
1149 goto out_failed;
1150 }
1151 memcpy(name, symname, strlen(symname));
1152 padd_item(name, item_len, strlen(symname));
1153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001154 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1155 if (retval) {
1156 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001157 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001158 goto out_failed;
1159 }
1160
1161 retval =
1162 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001163 dentry, inode, &security);
Pekka Enbergd739b422006-02-01 03:06:43 -08001164 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001165 if (retval) { /* reiserfs_new_inode iputs for us */
1166 goto out_failed;
1167 }
1168
1169 reiserfs_update_inode_transaction(inode);
1170 reiserfs_update_inode_transaction(parent_dir);
1171
1172 inode->i_op = &reiserfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001173 inode_nohighmem(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001174 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001176 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1177 dentry->d_name.len, inode, 1 /*visible */ );
1178 if (retval) {
1179 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001180 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001181 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001182 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 if (err)
1184 retval = err;
Al Viroc1eaa262008-12-30 02:03:58 -05001185 unlock_new_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186 iput(inode);
1187 goto out_failed;
1188 }
1189
Al Viroc1eaa262008-12-30 02:03:58 -05001190 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04001191 d_instantiate(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001192 retval = journal_end(&th);
Jeff Mahoneycf776a72014-04-23 10:00:41 -04001193out_failed:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001194 reiserfs_write_unlock(parent_dir->i_sb);
1195 return retval;
1196}
1197
1198static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1199 struct dentry *dentry)
1200{
1201 int retval;
David Howells2b0143b2015-03-17 22:25:59 +00001202 struct inode *inode = d_inode(old_dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001203 struct reiserfs_transaction_handle th;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001204 /*
1205 * We need blocks for transaction + update of quotas for
1206 * the owners of the directory
1207 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001208 int jbegin_count =
1209 JOURNAL_PER_BALANCE_CNT * 3 +
1210 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1211
Jan Kara2e6c97e2015-07-14 13:51:55 +02001212 retval = dquot_initialize(dir);
1213 if (retval)
1214 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001216 reiserfs_write_lock(dir->i_sb);
1217 if (inode->i_nlink >= REISERFS_LINK_MAX) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001218 /* FIXME: sd_nlink is 32 bit for new files */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219 reiserfs_write_unlock(dir->i_sb);
1220 return -EMLINK;
1221 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222
1223 /* inc before scheduling so reiserfs_unlink knows we are here */
Dave Hansend8c76e62006-09-30 23:29:04 -07001224 inc_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001225
1226 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1227 if (retval) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001228 drop_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001229 reiserfs_write_unlock(dir->i_sb);
1230 return retval;
1231 }
1232
1233 /* create new entry */
1234 retval =
1235 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1236 dentry->d_name.len, inode, 1 /*visible */ );
1237
1238 reiserfs_update_inode_transaction(inode);
1239 reiserfs_update_inode_transaction(dir);
1240
1241 if (retval) {
1242 int err;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001243 drop_nlink(inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001244 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001245 reiserfs_write_unlock(dir->i_sb);
1246 return err ? err : retval;
1247 }
1248
Deepa Dinamani02027d42016-09-14 07:48:05 -07001249 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001250 reiserfs_update_sd(&th, inode);
1251
Al Viro7de9c6ee2010-10-23 11:11:40 -04001252 ihold(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001253 d_instantiate(dentry, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001254 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001255 reiserfs_write_unlock(dir->i_sb);
1256 return retval;
1257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jeff Mahoney0222e652009-03-30 14:02:44 -04001259/* de contains information pointing to an entry which */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001260static int de_still_valid(const char *name, int len,
1261 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001263 struct reiserfs_dir_entry tmp = *de;
1264
Jeff Mahoney098297b2014-04-23 10:00:36 -04001265 /* recalculate pointer to name and name length */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266 set_de_name_and_namelen(&tmp);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001267 /* FIXME: could check more */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001268 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1269 return 0;
1270 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001273static int entry_points_to_object(const char *name, int len,
1274 struct reiserfs_dir_entry *de,
1275 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277 if (!de_still_valid(name, len, de))
1278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280 if (inode) {
1281 if (!de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001282 reiserfs_panic(inode->i_sb, "vs-7042",
1283 "entry must be visible");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001284 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 /* this must be added hidden entry */
1288 if (de_visible(de->de_deh + de->de_entry_num))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001289 reiserfs_panic(NULL, "vs-7043", "entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001295static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1296 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 /* JDM These operations are endian safe - both are le */
1299 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1300 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Jeff Mahoney0222e652009-03-30 14:02:44 -04001303/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * process, that is going to call fix_nodes/do_balance must hold only
1305 * one path. If it holds 2 or more, it can get into endless waiting in
Jeff Mahoney0222e652009-03-30 14:02:44 -04001306 * get_empty_nodes or its clones
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001308static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001309 struct inode *new_dir, struct dentry *new_dentry,
1310 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001312 int retval;
1313 INITIALIZE_PATH(old_entry_path);
1314 INITIALIZE_PATH(new_entry_path);
1315 INITIALIZE_PATH(dot_dot_entry_path);
1316 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1317 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1318 struct inode *old_inode, *new_dentry_inode;
1319 struct reiserfs_transaction_handle th;
1320 int jbegin_count;
1321 umode_t old_inode_mode;
1322 unsigned long savelink = 1;
1323 struct timespec ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001325 if (flags & ~RENAME_NOREPLACE)
1326 return -EINVAL;
1327
Jeff Mahoney098297b2014-04-23 10:00:36 -04001328 /*
1329 * three balancings: (1) old name removal, (2) new name insertion
1330 * and (3) maybe "save" link insertion
1331 * stat data updates: (1) old directory,
1332 * (2) new directory and (3) maybe old object stat data (when it is
1333 * directory) and (4) maybe stat data of object to which new entry
1334 * pointed initially and (5) maybe block containing ".." of
1335 * renamed directory
1336 * quota updates: two parent directories
1337 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001338 jbegin_count =
1339 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1340 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jan Kara2e6c97e2015-07-14 13:51:55 +02001342 retval = dquot_initialize(old_dir);
1343 if (retval)
1344 return retval;
1345 retval = dquot_initialize(new_dir);
1346 if (retval)
1347 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001348
David Howells2b0143b2015-03-17 22:25:59 +00001349 old_inode = d_inode(old_dentry);
1350 new_dentry_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Jeff Mahoney098297b2014-04-23 10:00:36 -04001352 /*
1353 * make sure that oldname still exists and points to an object we
1354 * are going to rename
1355 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001356 old_de.de_gen_number_bit_string = NULL;
1357 reiserfs_write_lock(old_dir->i_sb);
1358 retval =
1359 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1360 old_dentry->d_name.len, &old_entry_path,
1361 &old_de);
1362 pathrelse(&old_entry_path);
1363 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 reiserfs_write_unlock(old_dir->i_sb);
1365 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001367
1368 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1369 reiserfs_write_unlock(old_dir->i_sb);
1370 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001372
1373 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001375 /*
1376 * make sure that directory being renamed has correct ".."
1377 * and that its new parent directory has not too many links
1378 * already
1379 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001380 if (new_dentry_inode) {
1381 if (!reiserfs_empty_dir(new_dentry_inode)) {
1382 reiserfs_write_unlock(old_dir->i_sb);
1383 return -ENOTEMPTY;
1384 }
1385 }
1386
Jeff Mahoney098297b2014-04-23 10:00:36 -04001387 /*
1388 * directory is renamed, its parent directory will be changed,
1389 * so find ".." entry
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001390 */
1391 dot_dot_de.de_gen_number_bit_string = NULL;
1392 retval =
1393 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1394 &dot_dot_de);
1395 pathrelse(&dot_dot_entry_path);
1396 if (retval != NAME_FOUND) {
1397 reiserfs_write_unlock(old_dir->i_sb);
1398 return -EIO;
1399 }
1400
1401 /* inode number of .. must equal old_dir->i_ino */
1402 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1403 reiserfs_write_unlock(old_dir->i_sb);
1404 return -EIO;
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1409 if (retval) {
1410 reiserfs_write_unlock(old_dir->i_sb);
1411 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001414 /* add new entry (or find the existing one) */
1415 retval =
1416 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1417 new_dentry->d_name.len, old_inode, 0);
1418 if (retval == -EEXIST) {
1419 if (!new_dentry_inode) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001420 reiserfs_panic(old_dir->i_sb, "vs-7050",
1421 "new entry is found, new inode == 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001422 }
1423 } else if (retval) {
Jeff Mahoney58d85422014-04-23 10:00:38 -04001424 int err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001425 reiserfs_write_unlock(old_dir->i_sb);
1426 return err ? err : retval;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001429 reiserfs_update_inode_transaction(old_dir);
1430 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jeff Mahoney098297b2014-04-23 10:00:36 -04001432 /*
1433 * this makes it so an fsync on an open fd for the old name will
1434 * commit the rename operation
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001435 */
1436 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001438 if (new_dentry_inode)
1439 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001441 while (1) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001442 /*
1443 * look for old name using corresponding entry key
1444 * (found by reiserfs_find_entry)
1445 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001446 if ((retval =
1447 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1448 &old_entry_path,
1449 &old_de)) != NAME_FOUND) {
1450 pathrelse(&old_entry_path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001451 journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001452 reiserfs_write_unlock(old_dir->i_sb);
1453 return -EIO;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001456 copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001458 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1459
Jeff Mahoney098297b2014-04-23 10:00:36 -04001460 /* look for new name by reiserfs_find_entry */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001461 new_de.de_gen_number_bit_string = NULL;
1462 retval =
1463 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1464 new_dentry->d_name.len, &new_entry_path,
1465 &new_de);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001466 /*
1467 * reiserfs_add_entry should not return IO_ERROR,
1468 * because it is called with essentially same parameters from
1469 * reiserfs_add_entry above, and we'll catch any i/o errors
1470 * before we get here.
1471 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001472 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1473 pathrelse(&new_entry_path);
1474 pathrelse(&old_entry_path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001475 journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001476 reiserfs_write_unlock(old_dir->i_sb);
1477 return -EIO;
1478 }
1479
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001480 copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001481
1482 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1483
1484 if (S_ISDIR(old_inode->i_mode)) {
1485 if ((retval =
1486 search_by_entry_key(new_dir->i_sb,
1487 &dot_dot_de.de_entry_key,
1488 &dot_dot_entry_path,
1489 &dot_dot_de)) != NAME_FOUND) {
1490 pathrelse(&dot_dot_entry_path);
1491 pathrelse(&new_entry_path);
1492 pathrelse(&old_entry_path);
Jeff Mahoney58d85422014-04-23 10:00:38 -04001493 journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001494 reiserfs_write_unlock(old_dir->i_sb);
1495 return -EIO;
1496 }
1497 copy_item_head(&dot_dot_ih,
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001498 tp_item_head(&dot_dot_entry_path));
Jeff Mahoney098297b2014-04-23 10:00:36 -04001499 /* node containing ".." gets into transaction */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001500 reiserfs_prepare_for_journal(old_inode->i_sb,
1501 dot_dot_de.de_bh, 1);
1502 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001503 /*
1504 * we should check seals here, not do
1505 * this stuff, yes? Then, having
1506 * gathered everything into RAM we
1507 * should lock the buffers, yes? -Hans
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001508 */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001509 /*
1510 * probably. our rename needs to hold more
1511 * than one path at once. The seals would
1512 * have to be written to deal with multi-path
1513 * issues -chris
1514 */
1515 /*
1516 * sanity checking before doing the rename - avoid races many
1517 * of the above checks could have scheduled. We have to be
1518 * sure our items haven't been shifted by another process.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 */
1520 if (item_moved(&new_entry_ih, &new_entry_path) ||
1521 !entry_points_to_object(new_dentry->d_name.name,
1522 new_dentry->d_name.len,
1523 &new_de, new_dentry_inode) ||
1524 item_moved(&old_entry_ih, &old_entry_path) ||
1525 !entry_points_to_object(old_dentry->d_name.name,
1526 old_dentry->d_name.len,
1527 &old_de, old_inode)) {
1528 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1529 new_de.de_bh);
1530 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1531 old_de.de_bh);
1532 if (S_ISDIR(old_inode_mode))
1533 reiserfs_restore_prepared_buffer(old_inode->
1534 i_sb,
1535 dot_dot_de.
1536 de_bh);
1537 continue;
1538 }
1539 if (S_ISDIR(old_inode_mode)) {
1540 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1541 !entry_points_to_object("..", 2, &dot_dot_de,
1542 old_dir)) {
1543 reiserfs_restore_prepared_buffer(old_inode->
1544 i_sb,
1545 old_de.de_bh);
1546 reiserfs_restore_prepared_buffer(old_inode->
1547 i_sb,
1548 new_de.de_bh);
1549 reiserfs_restore_prepared_buffer(old_inode->
1550 i_sb,
1551 dot_dot_de.
1552 de_bh);
1553 continue;
1554 }
1555 }
1556
1557 RFALSE(S_ISDIR(old_inode_mode) &&
1558 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1559
1560 break;
1561 }
1562
Jeff Mahoney098297b2014-04-23 10:00:36 -04001563 /*
1564 * ok, all the changes can be done in one fell swoop when we
1565 * have claimed all the buffers needed.
1566 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001567
1568 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1569 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001570 journal_mark_dirty(&th, new_de.de_bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001571
1572 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001573 journal_mark_dirty(&th, old_de.de_bh);
Deepa Dinamani02027d42016-09-14 07:48:05 -07001574 ctime = current_time(old_dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001575 old_dir->i_ctime = old_dir->i_mtime = ctime;
1576 new_dir->i_ctime = new_dir->i_mtime = ctime;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001577 /*
1578 * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
1579 * which adds ctime update of renamed object
1580 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001581 old_inode->i_ctime = ctime;
1582
1583 if (new_dentry_inode) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001584 /* adjust link number of the victim */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001585 if (S_ISDIR(new_dentry_inode->i_mode)) {
Dave Hansence71ec32006-09-30 23:29:06 -07001586 clear_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001587 } else {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001588 drop_nlink(new_dentry_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001589 }
1590 new_dentry_inode->i_ctime = ctime;
1591 savelink = new_dentry_inode->i_nlink;
1592 }
1593
1594 if (S_ISDIR(old_inode_mode)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001595 /* adjust ".." of renamed directory */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001596 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001597 journal_mark_dirty(&th, dot_dot_de.de_bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001598
Jeff Mahoney098297b2014-04-23 10:00:36 -04001599 /*
1600 * there (in new_dir) was no directory, so it got new link
1601 * (".." of renamed directory)
1602 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001603 if (!new_dentry_inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001604 INC_DIR_INODE_NLINK(new_dir);
1605
1606 /* old directory lost one link - ".. " of renamed directory */
1607 DEC_DIR_INODE_NLINK(old_dir);
1608 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001609 /*
1610 * looks like in 2.3.99pre3 brelse is atomic.
1611 * so we can use pathrelse
1612 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001613 pathrelse(&new_entry_path);
1614 pathrelse(&dot_dot_entry_path);
1615
Jeff Mahoney098297b2014-04-23 10:00:36 -04001616 /*
1617 * FIXME: this reiserfs_cut_from_item's return value may screw up
1618 * anybody, but it will panic if will not be able to find the
1619 * entry. This needs one more clean up
1620 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001621 if (reiserfs_cut_from_item
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001622 (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001623 0) < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -04001624 reiserfs_error(old_dir->i_sb, "vs-7060",
1625 "couldn't not cut old name. Fsck later?");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001626
1627 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1628
1629 reiserfs_update_sd(&th, old_dir);
1630 reiserfs_update_sd(&th, new_dir);
1631 reiserfs_update_sd(&th, old_inode);
1632
1633 if (new_dentry_inode) {
1634 if (savelink == 0)
1635 add_save_link(&th, new_dentry_inode,
1636 0 /* not truncate */ );
1637 reiserfs_update_sd(&th, new_dentry_inode);
1638 }
1639
Jeff Mahoney58d85422014-04-23 10:00:38 -04001640 retval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001641 reiserfs_write_unlock(old_dir->i_sb);
1642 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Jeff Mahoney098297b2014-04-23 10:00:36 -04001645/* directories can handle most operations... */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001646const struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001647 .create = reiserfs_create,
1648 .lookup = reiserfs_lookup,
1649 .link = reiserfs_link,
1650 .unlink = reiserfs_unlink,
1651 .symlink = reiserfs_symlink,
1652 .mkdir = reiserfs_mkdir,
1653 .rmdir = reiserfs_rmdir,
1654 .mknod = reiserfs_mknod,
1655 .rename = reiserfs_rename,
1656 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001657 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001658 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001659 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -08001660 .set_acl = reiserfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661};
1662
1663/*
1664 * symlink operations.. same as page_symlink_inode_operations, with xattr
1665 * stuff added
1666 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001667const struct inode_operations reiserfs_symlink_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001668 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001669 .get_link = page_get_link,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001670 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001671 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001672 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673};
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/*
1676 * special file operations.. just xattr/acl stuff
1677 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001678const struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001679 .setattr = reiserfs_setattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001680 .listxattr = reiserfs_listxattr,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001681 .permission = reiserfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001682 .get_acl = reiserfs_get_acl,
Christoph Hellwig47f70d02013-12-20 05:16:49 -08001683 .set_acl = reiserfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684};