blob: 8bf88d690729e38e8cb278df108adcbb06d882e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
5/*
6 * Written by Anatoly P. Pinchuk pap@namesys.botik.ru
7 * Programm System Institute
8 * Pereslavl-Zalessky Russia
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/time.h>
12#include <linux/string.h>
13#include <linux/pagemap.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060014#include <linux/bio.h>
Al Virof466c6f2012-03-17 01:16:43 -040015#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/buffer_head.h>
17#include <linux/quotaops.h>
18
19/* Does the buffer contain a disk block which is in the tree. */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -040020inline int B_IS_IN_TREE(const struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -040023 RFALSE(B_LEVEL(bh) > MAX_HEIGHT,
24 "PAP-1010: block (%b) has too big level (%z)", bh, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -040026 return (B_LEVEL(bh) != FREE_LEVEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
Jeff Mahoney098297b2014-04-23 10:00:36 -040029/* to get item head in le form */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -040030inline void copy_item_head(struct item_head *to,
31 const struct item_head *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -040033 memcpy(to, from, IH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
Jeff Mahoney098297b2014-04-23 10:00:36 -040036/*
37 * k1 is pointer to on-disk structure which is stored in little-endian
38 * form. k2 is pointer to cpu variable. For key of items of the same
39 * object this returns 0.
40 * Returns: -1 if key1 < key2
41 * 0 if key1 == key2
42 * 1 if key1 > key2
43 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044inline int comp_short_keys(const struct reiserfs_key *le_key,
45 const struct cpu_key *cpu_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070047 __u32 n;
48 n = le32_to_cpu(le_key->k_dir_id);
49 if (n < cpu_key->on_disk_key.k_dir_id)
50 return -1;
51 if (n > cpu_key->on_disk_key.k_dir_id)
52 return 1;
53 n = le32_to_cpu(le_key->k_objectid);
54 if (n < cpu_key->on_disk_key.k_objectid)
55 return -1;
56 if (n > cpu_key->on_disk_key.k_objectid)
57 return 1;
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Jeff Mahoney098297b2014-04-23 10:00:36 -040061/*
62 * k1 is pointer to on-disk structure which is stored in little-endian
63 * form. k2 is pointer to cpu variable.
64 * Compare keys using all 4 key fields.
65 * Returns: -1 if key1 < key2 0
66 * if key1 = key2 1 if key1 > key2
67 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070068static inline int comp_keys(const struct reiserfs_key *le_key,
69 const struct cpu_key *cpu_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 retval = comp_short_keys(le_key, cpu_key);
74 if (retval)
75 return retval;
76 if (le_key_k_offset(le_key_version(le_key), le_key) <
77 cpu_key_k_offset(cpu_key))
78 return -1;
79 if (le_key_k_offset(le_key_version(le_key), le_key) >
80 cpu_key_k_offset(cpu_key))
81 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 if (cpu_key->key_length == 3)
84 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086 /* this part is needed only when tail conversion is in progress */
87 if (le_key_k_type(le_key_version(le_key), le_key) <
88 cpu_key_k_type(cpu_key))
89 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091 if (le_key_k_type(le_key_version(le_key), le_key) >
92 cpu_key_k_type(cpu_key))
93 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070098inline int comp_short_le_keys(const struct reiserfs_key *key1,
99 const struct reiserfs_key *key2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400101 __u32 *k1_u32, *k2_u32;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400102 int key_length = REISERFS_SHORT_KEY_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400104 k1_u32 = (__u32 *) key1;
105 k2_u32 = (__u32 *) key2;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400106 for (; key_length--; ++k1_u32, ++k2_u32) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400107 if (le32_to_cpu(*k1_u32) < le32_to_cpu(*k2_u32))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 return -1;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400109 if (le32_to_cpu(*k1_u32) > le32_to_cpu(*k2_u32))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 return 1;
111 }
112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700115inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 int version;
118 to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
119 to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
120
Jeff Mahoney098297b2014-04-23 10:00:36 -0400121 /* find out version of the key */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700122 version = le_key_version(from);
123 to->version = version;
124 to->on_disk_key.k_offset = le_key_k_offset(version, from);
125 to->on_disk_key.k_type = le_key_k_type(version, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jeff Mahoney098297b2014-04-23 10:00:36 -0400128/*
129 * this does not say which one is bigger, it only returns 1 if keys
130 * are not equal, 0 otherwise
131 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132inline int comp_le_keys(const struct reiserfs_key *k1,
133 const struct reiserfs_key *k2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700135 return memcmp(k1, k2, sizeof(struct reiserfs_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138/**************************************************************************
139 * Binary search toolkit function *
140 * Search for an item in the array by the item key *
141 * Returns: 1 if found, 0 if not found; *
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400142 * *pos = number of the searched element if found, else the *
143 * number of the first element that is larger than key. *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 **************************************************************************/
Jeff Mahoney098297b2014-04-23 10:00:36 -0400145/*
146 * For those not familiar with binary search: lbound is the leftmost item
147 * that it could be, rbound the rightmost item that it could be. We examine
148 * the item halfway between lbound and rbound, and that tells us either
149 * that we can increase lbound, or decrease rbound, or that we have found it,
150 * or if lbound <= rbound that there are no possible items, and we have not
151 * found it. With each examination we cut the number of possible items it
152 * could be by one more than half rounded down, or we find it.
153 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400154static inline int bin_search(const void *key, /* Key to search for. */
155 const void *base, /* First item in the array. */
156 int num, /* Number of items in the array. */
Jeff Mahoney098297b2014-04-23 10:00:36 -0400157 /*
158 * Item size in the array. searched. Lest the
159 * reader be confused, note that this is crafted
160 * as a general function, and when it is applied
161 * specifically to the array of item headers in a
162 * node, width is actually the item header size
163 * not the item size.
164 */
165 int width,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400166 int *pos /* Number of the searched for element. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167 )
168{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400169 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jeff Mahoneyee939612009-03-30 14:02:50 -0400171 for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
172 lbound <= rbound; j = (rbound + lbound) / 2)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 switch (comp_keys
Jeff Mahoneyee939612009-03-30 14:02:50 -0400174 ((struct reiserfs_key *)((char *)base + j * width),
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400175 (struct cpu_key *)key)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 case -1:
Jeff Mahoneyee939612009-03-30 14:02:50 -0400177 lbound = j + 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 continue;
179 case 1:
Jeff Mahoneyee939612009-03-30 14:02:50 -0400180 rbound = j - 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181 continue;
182 case 0:
Jeff Mahoneyee939612009-03-30 14:02:50 -0400183 *pos = j;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 return ITEM_FOUND; /* Key found in the array. */
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jeff Mahoney098297b2014-04-23 10:00:36 -0400187 /*
188 * bin_search did not find given key, it returns position of key,
189 * that is minimal and greater than the given one.
190 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400191 *pos = lbound;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 return ITEM_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* Minimal possible key. It is never in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/* Maximal possible key. It is never in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200static const struct reiserfs_key MAX_KEY = {
Fabian Frederickafd54c62014-05-03 22:33:24 +0200201 cpu_to_le32(0xffffffff),
202 cpu_to_le32(0xffffffff),
203 {{cpu_to_le32(0xffffffff),
204 cpu_to_le32(0xffffffff)},}
Al Viro3e8962b2005-05-01 08:59:18 -0700205};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jeff Mahoney098297b2014-04-23 10:00:36 -0400207/*
208 * Get delimiting key of the buffer by looking for it in the buffers in the
209 * path, starting from the bottom of the path, and going upwards. We must
210 * check the path's validity at each step. If the key is not in the path,
211 * there is no delimiting key in the tree (buffer is first or last buffer
212 * in tree), and in this case we return a special key, either MIN_KEY or
213 * MAX_KEY.
214 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400215static inline const struct reiserfs_key *get_lkey(const struct treepath *chk_path,
216 const struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400218 int position, path_offset = chk_path->path_length;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400219 struct buffer_head *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jeff Mahoneyee939612009-03-30 14:02:50 -0400221 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 "PAP-5010: invalid offset in the path");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 /* While not higher in path than first element. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400225 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227 RFALSE(!buffer_uptodate
Jeff Mahoneyee939612009-03-30 14:02:50 -0400228 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 "PAP-5020: parent is not uptodate");
230
231 /* Parent at the path is not in the tree now. */
232 if (!B_IS_IN_TREE
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400233 (parent =
Jeff Mahoneyee939612009-03-30 14:02:50 -0400234 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 return &MAX_KEY;
236 /* Check whether position in the parent is correct. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400237 if ((position =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400238 PATH_OFFSET_POSITION(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400239 path_offset)) >
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400240 B_NR_ITEMS(parent))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 return &MAX_KEY;
242 /* Check whether parent at the path really points to the child. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400243 if (B_N_CHILD_NUM(parent, position) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400244 PATH_OFFSET_PBUFFER(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400245 path_offset + 1)->b_blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 return &MAX_KEY;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400247 /*
248 * Return delimiting key if position in the parent
249 * is not equal to zero.
250 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400251 if (position)
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400252 return internal_key(parent, position - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 }
254 /* Return MIN_KEY if we are in the root of the buffer tree. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400255 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400256 b_blocknr == SB_ROOT_BLOCK(sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700257 return &MIN_KEY;
258 return &MAX_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/* Get delimiting key of the buffer at the path and its right neighbor. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400262inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400263 const struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400265 int position, path_offset = chk_path->path_length;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400266 struct buffer_head *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jeff Mahoneyee939612009-03-30 14:02:50 -0400268 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700269 "PAP-5030: invalid offset in the path");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jeff Mahoneyee939612009-03-30 14:02:50 -0400271 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 RFALSE(!buffer_uptodate
Jeff Mahoneyee939612009-03-30 14:02:50 -0400274 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275 "PAP-5040: parent is not uptodate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 /* Parent at the path is not in the tree now. */
278 if (!B_IS_IN_TREE
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400279 (parent =
Jeff Mahoneyee939612009-03-30 14:02:50 -0400280 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281 return &MIN_KEY;
282 /* Check whether position in the parent is correct. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400283 if ((position =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400284 PATH_OFFSET_POSITION(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400285 path_offset)) >
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400286 B_NR_ITEMS(parent))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 return &MIN_KEY;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400288 /*
289 * Check whether parent at the path really points
290 * to the child.
291 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400292 if (B_N_CHILD_NUM(parent, position) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400293 PATH_OFFSET_PBUFFER(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400294 path_offset + 1)->b_blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 return &MIN_KEY;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400296
297 /*
298 * Return delimiting key if position in the parent
299 * is not the last one.
300 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400301 if (position != B_NR_ITEMS(parent))
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400302 return internal_key(parent, position);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700303 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400304
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305 /* Return MAX_KEY if we are in the root of the buffer tree. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400306 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400307 b_blocknr == SB_ROOT_BLOCK(sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308 return &MAX_KEY;
309 return &MIN_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Jeff Mahoney098297b2014-04-23 10:00:36 -0400312/*
313 * Check whether a key is contained in the tree rooted from a buffer at a path.
314 * This works by looking at the left and right delimiting keys for the buffer
315 * in the last path_element in the path. These delimiting keys are stored
316 * at least one level above that buffer in the tree. If the buffer is the
317 * first or last node in the tree order then one of the delimiting keys may
318 * be absent, and in this case get_lkey and get_rkey return a special key
319 * which is MIN_KEY or MAX_KEY.
320 */
321static inline int key_in_buffer(
322 /* Path which should be checked. */
323 struct treepath *chk_path,
324 /* Key which should be checked. */
325 const struct cpu_key *key,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400326 struct super_block *sb
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 )
328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400330 RFALSE(!key || chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
331 || chk_path->path_length > MAX_HEIGHT,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700332 "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400333 key, chk_path->path_length);
334 RFALSE(!PATH_PLAST_BUFFER(chk_path)->b_bdev,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 "PAP-5060: device must not be NODEV");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400337 if (comp_keys(get_lkey(chk_path, sb), key) == 1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700338 /* left delimiting key is bigger, that the key we look for */
339 return 0;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400340 /* if ( comp_keys(key, get_rkey(chk_path, sb)) != -1 ) */
341 if (comp_keys(get_rkey(chk_path, sb), key) != 1)
342 /* key must be less than right delimitiing key */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700343 return 0;
344 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800347int reiserfs_check_path(struct treepath *p)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700348{
349 RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
350 "path not properly relsed");
351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Jeff Mahoney098297b2014-04-23 10:00:36 -0400354/*
355 * Drop the reference to each buffer in a path and restore
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400356 * dirty bits clean when preparing the buffer for the log.
Jeff Mahoney098297b2014-04-23 10:00:36 -0400357 * This version should only be called from fix_nodes()
358 */
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400359void pathrelse_and_restore(struct super_block *sb,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400360 struct treepath *search_path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700361{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400362 int path_offset = search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Jeff Mahoneyee939612009-03-30 14:02:50 -0400364 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700365 "clm-4000: invalid path offset");
366
Jeff Mahoneyee939612009-03-30 14:02:50 -0400367 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400368 struct buffer_head *bh;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400369 bh = PATH_OFFSET_PBUFFER(search_path, path_offset--);
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400370 reiserfs_restore_prepared_buffer(sb, bh);
371 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400373 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400376/* Drop the reference to each buffer in a path */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400377void pathrelse(struct treepath *search_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400379 int path_offset = search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jeff Mahoneyee939612009-03-30 14:02:50 -0400381 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700382 "PAP-5090: invalid path offset");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jeff Mahoneyee939612009-03-30 14:02:50 -0400384 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
385 brelse(PATH_OFFSET_PBUFFER(search_path, path_offset--));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400387 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
391{
392 struct block_head *blkh;
393 struct item_head *ih;
394 int used_space;
395 int prev_location;
396 int i;
397 int nr;
398
399 blkh = (struct block_head *)buf;
400 if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400401 reiserfs_warning(NULL, "reiserfs-5080",
402 "this should be caught earlier");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700403 return 0;
404 }
405
406 nr = blkh_nr_item(blkh);
407 if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
408 /* item number is too big or too small */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400409 reiserfs_warning(NULL, "reiserfs-5081",
410 "nr_item seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700411 return 0;
412 }
413 ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
414 used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
Jeff Mahoney098297b2014-04-23 10:00:36 -0400415
416 /* free space does not match to calculated amount of use space */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417 if (used_space != blocksize - blkh_free_space(blkh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400418 reiserfs_warning(NULL, "reiserfs-5082",
419 "free space seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700420 return 0;
421 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400422 /*
423 * FIXME: it is_leaf will hit performance too much - we may have
424 * return 1 here
425 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426
427 /* check tables of item heads */
428 ih = (struct item_head *)(buf + BLKH_SIZE);
429 prev_location = blocksize;
430 for (i = 0; i < nr; i++, ih++) {
431 if (le_ih_k_type(ih) == TYPE_ANY) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400432 reiserfs_warning(NULL, "reiserfs-5083",
433 "wrong item type for item %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 ih);
435 return 0;
436 }
437 if (ih_location(ih) >= blocksize
438 || ih_location(ih) < IH_SIZE * nr) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400439 reiserfs_warning(NULL, "reiserfs-5084",
440 "item location seems wrong: %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 ih);
442 return 0;
443 }
444 if (ih_item_len(ih) < 1
445 || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400446 reiserfs_warning(NULL, "reiserfs-5085",
447 "item length seems wrong: %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448 ih);
449 return 0;
450 }
451 if (prev_location - ih_location(ih) != ih_item_len(ih)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400452 reiserfs_warning(NULL, "reiserfs-5086",
453 "item location seems wrong "
454 "(second one): %h", ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 return 0;
456 }
457 prev_location = ih_location(ih);
458 }
459
Jeff Mahoney098297b2014-04-23 10:00:36 -0400460 /* one may imagine many more checks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 return 1;
462}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/* returns 1 if buf looks like an internal node, 0 otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467 struct block_head *blkh;
468 int nr;
469 int used_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 blkh = (struct block_head *)buf;
472 nr = blkh_level(blkh);
473 if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
474 /* this level is not possible for internal nodes */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400475 reiserfs_warning(NULL, "reiserfs-5087",
476 "this should be caught earlier");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 return 0;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 nr = blkh_nr_item(blkh);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400481 /* for internal which is not root we might check min number of keys */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700482 if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400483 reiserfs_warning(NULL, "reiserfs-5088",
484 "number of key seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 return 0;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700488 used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
489 if (used_space != blocksize - blkh_free_space(blkh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400490 reiserfs_warning(NULL, "reiserfs-5089",
491 "free space seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 return 0;
493 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400494
495 /* one may imagine many more checks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700496 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Jeff Mahoney098297b2014-04-23 10:00:36 -0400499/*
500 * make sure that bh contains formatted node of reiserfs tree of
501 * 'level'-th level
502 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700503static int is_tree_node(struct buffer_head *bh, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 if (B_LEVEL(bh) != level) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400506 reiserfs_warning(NULL, "reiserfs-5090", "node level %d does "
507 "not match to the expected one %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 B_LEVEL(bh), level);
509 return 0;
510 }
511 if (level == DISK_LEAF_NODE_LEVEL)
512 return is_leaf(bh->b_data, bh->b_size, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 return is_internal(bh->b_data, bh->b_size, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#define SEARCH_BY_KEY_READA 16
518
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200519/*
520 * The function is NOT SCHEDULE-SAFE!
521 * It might unlock the write lock if we needed to wait for a block
522 * to be read. Note that in this case it won't recover the lock to avoid
523 * high contention resulting from too much lock requests, especially
524 * the caller (search_by_key) will perform other schedule-unsafe
525 * operations just after calling this function.
526 *
Jeff Mahoney278f6672013-08-08 17:34:46 -0400527 * @return depth of lock to be restored after read completes
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200528 */
Jeff Mahoney278f6672013-08-08 17:34:46 -0400529static int search_by_key_reada(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700530 struct buffer_head **bh,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700531 b_blocknr_t *b, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 int i, j;
Jeff Mahoney278f6672013-08-08 17:34:46 -0400534 int depth = -1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535
536 for (i = 0; i < num; i++) {
537 bh[i] = sb_getblk(s, b[i]);
538 }
Frederic Weisbecker09eb47a2009-05-08 14:21:33 +0200539 /*
540 * We are going to read some blocks on which we
541 * have a reference. It's safe, though we might be
542 * reading blocks concurrently changed if we release
543 * the lock. But it's still fine because we check later
544 * if the tree changed
545 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 for (j = 0; j < i; j++) {
547 /*
548 * note, this needs attention if we are getting rid of the BKL
Jeff Mahoney098297b2014-04-23 10:00:36 -0400549 * you have to make sure the prepared bit isn't set on this
550 * buffer
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700551 */
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200552 if (!buffer_uptodate(bh[j])) {
Jeff Mahoney278f6672013-08-08 17:34:46 -0400553 if (depth == -1)
554 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig70246282016-07-19 11:28:41 +0200555 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, bh + j);
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200556 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 brelse(bh[j]);
558 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400559 return depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Jeff Mahoney098297b2014-04-23 10:00:36 -0400562/*
563 * This function fills up the path from the root to the leaf as it
564 * descends the tree looking for the key. It uses reiserfs_bread to
565 * try to find buffers in the cache given their block number. If it
566 * does not find them in the cache it reads them from disk. For each
567 * node search_by_key finds using reiserfs_bread it then uses
568 * bin_search to look through that node. bin_search will find the
569 * position of the block_number of the next node if it is looking
570 * through an internal node. If it is looking through a leaf node
571 * bin_search will find the position of the item which has key either
572 * equal to given key, or which is the maximal key less than the given
573 * key. search_by_key returns a path that must be checked for the
574 * correctness of the top of the path but need not be checked for the
575 * correctness of the bottom of the path
576 */
577/*
578 * search_by_key - search for key (and item) in stree
579 * @sb: superblock
580 * @key: pointer to key to search for
581 * @search_path: Allocated and initialized struct treepath; Returned filled
582 * on success.
583 * @stop_level: How far down the tree to search, Use DISK_LEAF_NODE_LEVEL to
584 * stop at leaf level.
585 *
586 * The function is NOT SCHEDULE-SAFE!
587 */
588int search_by_key(struct super_block *sb, const struct cpu_key *key,
589 struct treepath *search_path, int stop_level)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700590{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400591 b_blocknr_t block_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700592 int expected_level;
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400593 struct buffer_head *bh;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400594 struct path_element *last_element;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400595 int node_level, retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 int fs_gen;
597 struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700598 b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700599 int reada_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -0400602 int repeat_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400605 PROC_INFO_INC(sb, search_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Jeff Mahoney098297b2014-04-23 10:00:36 -0400607 /*
608 * As we add each node to a path we increase its count. This means
609 * that we must be careful to release all nodes in a path before we
610 * either discard the path struct or re-use the path struct, as we
611 * do here.
612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400614 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700615
Jeff Mahoney098297b2014-04-23 10:00:36 -0400616 /*
617 * With each iteration of this loop we search through the items in the
618 * current node, and calculate the next current node(next path element)
619 * for the next iteration of this loop..
620 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400621 block_number = SB_ROOT_BLOCK(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622 expected_level = -1;
623 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -0400626 if (!(++repeat_counter % 50000))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400627 reiserfs_warning(sb, "PAP-5100",
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400628 "%s: there were %d iterations of "
629 "while loop looking for key %K",
Jeff Mahoneyee939612009-03-30 14:02:50 -0400630 current->comm, repeat_counter,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400631 key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#endif
633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700634 /* prep path to have another element added to it. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400635 last_element =
636 PATH_OFFSET_PELEMENT(search_path,
637 ++search_path->path_length);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400638 fs_gen = get_generation(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Jeff Mahoney098297b2014-04-23 10:00:36 -0400640 /*
641 * Read the next tree node, and set the last element
642 * in the path to have a pointer to it.
643 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400644 if ((bh = last_element->pe_buffer =
Jeff Mahoneyee939612009-03-30 14:02:50 -0400645 sb_getblk(sb, block_number))) {
Jeff Mahoney278f6672013-08-08 17:34:46 -0400646
647 /*
648 * We'll need to drop the lock if we encounter any
649 * buffers that need to be read. If all of them are
650 * already up to date, we don't need to drop the lock.
651 */
652 int depth = -1;
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200653
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400654 if (!buffer_uptodate(bh) && reada_count > 1)
Jeff Mahoney278f6672013-08-08 17:34:46 -0400655 depth = search_by_key_reada(sb, reada_bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 reada_blocks, reada_count);
Jeff Mahoney278f6672013-08-08 17:34:46 -0400657
658 if (!buffer_uptodate(bh) && depth == -1)
659 depth = reiserfs_write_unlock_nested(sb);
660
Mike Christiedfec8a12016-06-05 14:31:44 -0500661 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400662 wait_on_buffer(bh);
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200663
Jeff Mahoney278f6672013-08-08 17:34:46 -0400664 if (depth != -1)
665 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400666 if (!buffer_uptodate(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700667 goto io_error;
668 } else {
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400669io_error:
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400670 search_path->path_length--;
671 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700674 reada_count = 0;
675 if (expected_level == -1)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400676 expected_level = SB_TREE_HEIGHT(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 expected_level--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jeff Mahoney098297b2014-04-23 10:00:36 -0400679 /*
680 * It is possible that schedule occurred. We must check
681 * whether the key to search is still in the tree rooted
682 * from the current buffer. If not then repeat search
683 * from the root.
684 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400685 if (fs_changed(fs_gen, sb) &&
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400686 (!B_IS_IN_TREE(bh) ||
687 B_LEVEL(bh) != expected_level ||
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400688 !key_in_buffer(search_path, key, sb))) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400689 PROC_INFO_INC(sb, search_by_key_fs_changed);
690 PROC_INFO_INC(sb, search_by_key_restarted);
691 PROC_INFO_INC(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700692 sbk_restarted[expected_level - 1]);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400693 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700694
Jeff Mahoney098297b2014-04-23 10:00:36 -0400695 /*
696 * Get the root block number so that we can
697 * repeat the search starting from the root.
698 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400699 block_number = SB_ROOT_BLOCK(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 expected_level = -1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701
702 /* repeat search from the root */
703 continue;
704 }
705
Jeff Mahoney098297b2014-04-23 10:00:36 -0400706 /*
707 * only check that the key is in the buffer if key is not
708 * equal to the MAX_KEY. Latter case is only possible in
709 * "finish_unfinished()" processing during mount.
710 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400711 RFALSE(comp_keys(&MAX_KEY, key) &&
712 !key_in_buffer(search_path, key, sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700713 "PAP-5130: key is not in the buffer");
714#ifdef CONFIG_REISERFS_CHECK
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +0200715 if (REISERFS_SB(sb)->cur_tb) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 print_cur_tb("5140");
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400717 reiserfs_panic(sb, "PAP-5140",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400718 "schedule occurred in do_balance!");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 }
720#endif
721
Jeff Mahoney098297b2014-04-23 10:00:36 -0400722 /*
723 * make sure, that the node contents look like a node of
724 * certain level
725 */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400726 if (!is_tree_node(bh, expected_level)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400727 reiserfs_error(sb, "vs-5150",
Jeff Mahoney0030b642009-03-30 14:02:28 -0400728 "invalid format found in block %ld. "
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400729 "Fsck?", bh->b_blocknr);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400730 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 return IO_ERROR;
732 }
733
734 /* ok, we have acquired next formatted node in the tree */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400735 node_level = B_LEVEL(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736
Jeff Mahoneyee939612009-03-30 14:02:50 -0400737 PROC_INFO_BH_STAT(sb, bh, node_level - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700738
Jeff Mahoneyee939612009-03-30 14:02:50 -0400739 RFALSE(node_level < stop_level,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740 "vs-5152: tree level (%d) is less than stop level (%d)",
Jeff Mahoneyee939612009-03-30 14:02:50 -0400741 node_level, stop_level);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700742
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400743 retval = bin_search(key, item_head(bh, 0),
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400744 B_NR_ITEMS(bh),
Jeff Mahoneyee939612009-03-30 14:02:50 -0400745 (node_level ==
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
747 KEY_SIZE,
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400748 &last_element->pe_position);
Jeff Mahoneyee939612009-03-30 14:02:50 -0400749 if (node_level == stop_level) {
750 return retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751 }
752
753 /* we are not in the stop level */
Jeff Mahoney098297b2014-04-23 10:00:36 -0400754 /*
755 * item has been found, so we choose the pointer which
756 * is to the right of the found one
757 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400758 if (retval == ITEM_FOUND)
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400759 last_element->pe_position++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760
Jeff Mahoney098297b2014-04-23 10:00:36 -0400761 /*
762 * if item was not found we choose the position which is to
763 * the left of the found item. This requires no code,
764 * bin_search did it already.
765 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766
Jeff Mahoney098297b2014-04-23 10:00:36 -0400767 /*
768 * So we have chosen a position in the current node which is
769 * an internal node. Now we calculate child block number by
770 * position in the node.
771 */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400772 block_number =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400773 B_N_CHILD_NUM(bh, last_element->pe_position);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774
Jeff Mahoney098297b2014-04-23 10:00:36 -0400775 /*
776 * if we are going to read leaf nodes, try for read
777 * ahead as well
778 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400779 if ((search_path->reada & PATH_READA) &&
Jeff Mahoneyee939612009-03-30 14:02:50 -0400780 node_level == DISK_LEAF_NODE_LEVEL + 1) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400781 int pos = last_element->pe_position;
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400782 int limit = B_NR_ITEMS(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700783 struct reiserfs_key *le_key;
784
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400785 if (search_path->reada & PATH_READA_BACK)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700786 limit = 0;
787 while (reada_count < SEARCH_BY_KEY_READA) {
788 if (pos == limit)
789 break;
790 reada_blocks[reada_count++] =
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400791 B_N_CHILD_NUM(bh, pos);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400792 if (search_path->reada & PATH_READA_BACK)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 pos--;
794 else
795 pos++;
796
797 /*
798 * check to make sure we're in the same object
799 */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400800 le_key = internal_key(bh, pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700801 if (le32_to_cpu(le_key->k_objectid) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400802 key->on_disk_key.k_objectid) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700803 break;
804 }
805 }
806 }
807 }
808}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Jeff Mahoney098297b2014-04-23 10:00:36 -0400810/*
811 * Form the path to an item and position in this item which contains
812 * file byte defined by key. If there is no such item
813 * corresponding to the key, we point the path to the item with
814 * maximal key less than key, and *pos_in_item is set to one
815 * past the last entry/byte in the item. If searching for entry in a
816 * directory item, and it is not found, *pos_in_item is set to one
817 * entry more than the entry with maximal key which is less than the
818 * sought key.
819 *
820 * Note that if there is no entry in this same node which is one more,
821 * then we point to an imaginary entry. for direct items, the
822 * position is in units of bytes, for indirect items the position is
823 * in units of blocknr entries, for directory items the position is in
824 * units of directory entries.
825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/* The function is NOT SCHEDULE-SAFE! */
Jeff Mahoney098297b2014-04-23 10:00:36 -0400827int search_for_position_by_key(struct super_block *sb,
828 /* Key to search (cpu variable) */
829 const struct cpu_key *p_cpu_key,
830 /* Filled up by this function. */
831 struct treepath *search_path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700832{
833 struct item_head *p_le_ih; /* pointer to on-disk structure */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400834 int blk_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700835 loff_t item_offset, offset;
836 struct reiserfs_dir_entry de;
837 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700839 /* If searching for directory entry. */
840 if (is_direntry_cpu_key(p_cpu_key))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400841 return search_by_entry_key(sb, p_cpu_key, search_path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700842 &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 /* If not searching for directory entry. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700846 /* If item is found. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400847 retval = search_item(sb, p_cpu_key, search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700848 if (retval == IO_ERROR)
849 return retval;
850 if (retval == ITEM_FOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700852 RFALSE(!ih_item_len
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400853 (item_head
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400854 (PATH_PLAST_BUFFER(search_path),
855 PATH_LAST_POSITION(search_path))),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856 "PAP-5165: item length equals zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400858 pos_in_item(search_path) = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700859 return POSITION_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400862 RFALSE(!PATH_LAST_POSITION(search_path),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700863 "PAP-5170: position equals zero");
864
865 /* Item is not found. Set path to the previous item. */
866 p_le_ih =
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400867 item_head(PATH_PLAST_BUFFER(search_path),
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400868 --PATH_LAST_POSITION(search_path));
Jeff Mahoneyee939612009-03-30 14:02:50 -0400869 blk_size = sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700870
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400871 if (comp_short_keys(&p_le_ih->ih_key, p_cpu_key))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700872 return FILE_NOT_FOUND;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400873
874 /* FIXME: quite ugly this far */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700875
876 item_offset = le_ih_k_offset(p_le_ih);
877 offset = cpu_key_k_offset(p_cpu_key);
878
879 /* Needed byte is contained in the item pointed to by the path. */
880 if (item_offset <= offset &&
Jeff Mahoneyee939612009-03-30 14:02:50 -0400881 item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400882 pos_in_item(search_path) = offset - item_offset;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700883 if (is_indirect_le_ih(p_le_ih)) {
Jeff Mahoneyee939612009-03-30 14:02:50 -0400884 pos_in_item(search_path) /= blk_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700885 }
886 return POSITION_FOUND;
887 }
888
Jeff Mahoney098297b2014-04-23 10:00:36 -0400889 /*
890 * Needed byte is not contained in the item pointed to by the
891 * path. Set pos_in_item out of the item.
892 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700893 if (is_indirect_le_ih(p_le_ih))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400894 pos_in_item(search_path) =
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700895 ih_item_len(p_le_ih) / UNFM_P_SIZE;
896 else
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400897 pos_in_item(search_path) = ih_item_len(p_le_ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898
899 return POSITION_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/* Compare given item and item pointed to by the path. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400903int comp_items(const struct item_head *stored_ih, const struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400905 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700906 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700908 /* Last buffer at the path is not in the tree. */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400909 if (!B_IS_IN_TREE(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700910 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700912 /* Last path position is invalid. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400913 if (PATH_LAST_POSITION(path) >= B_NR_ITEMS(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700916 /* we need only to know, whether it is the same item */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400917 ih = tp_item_head(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700918 return memcmp(stored_ih, ih, IH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Jeff Mahoney098297b2014-04-23 10:00:36 -0400921/* prepare for delete or cut of direct item */
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800922static inline int prepare_for_direct_item(struct treepath *path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700923 struct item_head *le_ih,
924 struct inode *inode,
925 loff_t new_file_length, int *cut_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700927 loff_t round_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 if (new_file_length == max_reiserfs_offset(inode)) {
930 /* item has to be deleted */
931 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
932 return M_DELETE;
933 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400934 /* new file gets truncated */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700936 round_len = ROUND_UP(new_file_length);
Jeff Mahoneyee939612009-03-30 14:02:50 -0400937 /* this was new_file_length < le_ih ... */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700938 if (round_len < le_ih_k_offset(le_ih)) {
939 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
940 return M_DELETE; /* Delete this item. */
941 }
942 /* Calculate first position and size for cutting from item. */
943 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
944 *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700946 return M_CUT; /* Cut from this item. */
947 }
948
Jeff Mahoney098297b2014-04-23 10:00:36 -0400949 /* old file: items may have any length */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700950
951 if (new_file_length < le_ih_k_offset(le_ih)) {
952 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
953 return M_DELETE; /* Delete this item. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Calculate first position and size for cutting from item. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700957 *cut_size = -(ih_item_len(le_ih) -
958 (pos_in_item(path) =
959 new_file_length + 1 - le_ih_k_offset(le_ih)));
960 return M_CUT; /* Cut from this item. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800963static inline int prepare_for_direntry_item(struct treepath *path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700964 struct item_head *le_ih,
965 struct inode *inode,
966 loff_t new_file_length,
967 int *cut_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969 if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
970 new_file_length == max_reiserfs_offset(inode)) {
971 RFALSE(ih_entry_count(le_ih) != 2,
972 "PAP-5220: incorrect empty directory item (%h)", le_ih);
973 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
Jeff Mahoney098297b2014-04-23 10:00:36 -0400974 /* Delete the directory item containing "." and ".." entry. */
975 return M_DELETE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700978 if (ih_entry_count(le_ih) == 1) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400979 /*
980 * Delete the directory item such as there is one record only
981 * in this item
982 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700983 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
984 return M_DELETE;
985 }
986
987 /* Cut one record from the directory item. */
988 *cut_size =
989 -(DEH_SIZE +
990 entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
991 return M_CUT;
992}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -0800994#define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
995
Jeff Mahoney098297b2014-04-23 10:00:36 -0400996/*
997 * If the path points to a directory or direct item, calculate mode
998 * and the size cut, for balance.
999 * If the path points to an indirect item, remove some number of its
1000 * unformatted nodes.
1001 * In case of file truncate calculate whether this item must be
1002 * deleted/truncated or last unformatted node of this item will be
1003 * converted to a direct item.
1004 * This function returns a determination of what balance mode the
1005 * calling function should employ.
1006 */
1007static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th,
1008 struct inode *inode,
1009 struct treepath *path,
1010 const struct cpu_key *item_key,
1011 /*
1012 * Number of unformatted nodes
1013 * which were removed from end
1014 * of the file.
1015 */
1016 int *removed,
1017 int *cut_size,
1018 /* MAX_KEY_OFFSET in case of delete. */
1019 unsigned long long new_file_length
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 )
1021{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001022 struct super_block *sb = inode->i_sb;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001023 struct item_head *p_le_ih = tp_item_head(path);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001024 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001026 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 /* Stat_data item. */
1029 if (is_statdata_le_ih(p_le_ih)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Jeff Mahoneyee939612009-03-30 14:02:50 -04001031 RFALSE(new_file_length != max_reiserfs_offset(inode),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001032 "PAP-5210: mode must be M_DELETE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001034 *cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001035 return M_DELETE;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001038 /* Directory item. */
1039 if (is_direntry_le_ih(p_le_ih))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001040 return prepare_for_direntry_item(path, p_le_ih, inode,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001041 new_file_length,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001042 cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 /* Direct item. */
1045 if (is_direct_le_ih(p_le_ih))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001046 return prepare_for_direct_item(path, p_le_ih, inode,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001047 new_file_length, cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001049 /* Case of an indirect item. */
1050 {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001051 int blk_size = sb->s_blocksize;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001052 struct item_head s_ih;
1053 int need_re_search;
1054 int delete = 0;
1055 int result = M_CUT;
1056 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Jeff Mahoneyee939612009-03-30 14:02:50 -04001058 if ( new_file_length == max_reiserfs_offset (inode) ) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001059 /*
1060 * prepare_for_delete_or_cut() is called by
1061 * reiserfs_delete_item()
1062 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001063 new_file_length = 0;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001064 delete = 1;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001067 do {
1068 need_re_search = 0;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001069 *cut_size = 0;
1070 bh = PATH_PLAST_BUFFER(path);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001071 copy_item_head(&s_ih, tp_item_head(path));
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001072 pos = I_UNFM_NUM(&s_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Jeff Mahoneyee939612009-03-30 14:02:50 -04001074 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
Al Viro87588dd2007-07-26 17:47:03 +01001075 __le32 *unfm;
1076 __u32 block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jeff Mahoney098297b2014-04-23 10:00:36 -04001078 /*
1079 * Each unformatted block deletion may involve
1080 * one additional bitmap block into the transaction,
1081 * thereby the initial journal space reservation
1082 * might not be enough.
1083 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001084 if (!delete && (*cut_size) != 0 &&
1085 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001088 unfm = (__le32 *)ih_item_body(bh, &s_ih) + pos - 1;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001089 block = get_block_num(unfm, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001091 if (block != 0) {
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04001092 reiserfs_prepare_for_journal(sb, bh, 1);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001093 put_block_num(unfm, 0, 0);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001094 journal_mark_dirty(th, bh);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001095 reiserfs_free_block(th, inode, block, 1);
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jeff Mahoney278f6672013-08-08 17:34:46 -04001098 reiserfs_cond_resched(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001100 if (item_moved (&s_ih, path)) {
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001101 need_re_search = 1;
1102 break;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001105 pos --;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001106 (*removed)++;
1107 (*cut_size) -= UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001109 if (pos == 0) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001110 (*cut_size) -= IH_SIZE;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001111 result = M_DELETE;
1112 break;
1113 }
1114 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001115 /*
1116 * a trick. If the buffer has been logged, this will
1117 * do nothing. If we've broken the loop without logging
1118 * it, it will restore the buffer
1119 */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04001120 reiserfs_restore_prepared_buffer(sb, bh);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001121 } while (need_re_search &&
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001122 search_for_position_by_key(sb, item_key, path) == POSITION_FOUND);
1123 pos_in_item(path) = pos * UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001125 if (*cut_size == 0) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001126 /*
1127 * Nothing was cut. maybe convert last unformatted node to the
1128 * direct item?
1129 */
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001130 result = M_CONVERT;
1131 }
1132 return result;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136/* Calculate number of bytes which will be deleted or cut during balance */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001137static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jeff Mahoneyee939612009-03-30 14:02:50 -04001139 int del_size;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001140 struct item_head *p_le_ih = tp_item_head(tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001142 if (is_statdata_le_ih(p_le_ih))
1143 return 0;
1144
Jeff Mahoneyee939612009-03-30 14:02:50 -04001145 del_size =
1146 (mode ==
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001147 M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 if (is_direntry_le_ih(p_le_ih)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001149 /*
1150 * return EMPTY_DIR_SIZE; We delete emty directories only.
1151 * we can't use EMPTY_DIR_SIZE, as old format dirs have a
1152 * different empty size. ick. FIXME, is this right?
1153 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001154 return del_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001155 }
1156
1157 if (is_indirect_le_ih(p_le_ih))
Jeff Mahoneyee939612009-03-30 14:02:50 -04001158 del_size = (del_size / UNFM_P_SIZE) *
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001159 (PATH_PLAST_BUFFER(tb->tb_path)->b_size);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001160 return del_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161}
1162
1163static void init_tb_struct(struct reiserfs_transaction_handle *th,
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001164 struct tree_balance *tb,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001165 struct super_block *sb,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001166 struct treepath *path, int size)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001167{
1168
1169 BUG_ON(!th->t_trans_id);
1170
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001171 memset(tb, '\0', sizeof(struct tree_balance));
1172 tb->transaction_handle = th;
1173 tb->tb_sb = sb;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001174 tb->tb_path = path;
1175 PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
1176 PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001177 tb->insert_size[0] = size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001178}
1179
1180void padd_item(char *item, int total_length, int length)
1181{
1182 int i;
1183
1184 for (i = total_length; i > length;)
1185 item[--i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188#ifdef REISERQUOTA_DEBUG
1189char key2type(struct reiserfs_key *ih)
1190{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191 if (is_direntry_le_key(2, ih))
1192 return 'd';
1193 if (is_direct_le_key(2, ih))
1194 return 'D';
1195 if (is_indirect_le_key(2, ih))
1196 return 'i';
1197 if (is_statdata_le_key(2, ih))
1198 return 's';
1199 return 'u';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202char head2type(struct item_head *ih)
1203{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204 if (is_direntry_le_ih(ih))
1205 return 'd';
1206 if (is_direct_le_ih(ih))
1207 return 'D';
1208 if (is_indirect_le_ih(ih))
1209 return 'i';
1210 if (is_statdata_le_ih(ih))
1211 return 's';
1212 return 'u';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214#endif
1215
Jeff Mahoney098297b2014-04-23 10:00:36 -04001216/*
1217 * Delete object item.
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001218 * th - active transaction handle
1219 * path - path to the deleted item
1220 * item_key - key to search for the deleted item
1221 * indode - used for updating i_blocks and quotas
1222 * un_bh - NULL or unformatted node pointer
1223 */
1224int reiserfs_delete_item(struct reiserfs_transaction_handle *th,
1225 struct treepath *path, const struct cpu_key *item_key,
1226 struct inode *inode, struct buffer_head *un_bh)
1227{
Jeff Mahoney995c7622009-03-30 14:02:47 -04001228 struct super_block *sb = inode->i_sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001229 struct tree_balance s_del_balance;
1230 struct item_head s_ih;
1231 struct item_head *q_ih;
1232 int quota_cut_bytes;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001233 int ret_value, del_size, removed;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001234 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -04001237 char mode;
1238 int iter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239#endif
1240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001241 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001243 init_tb_struct(th, &s_del_balance, sb, path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 0 /*size is unknown */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 while (1) {
Jeff Mahoneyee939612009-03-30 14:02:50 -04001247 removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -04001250 iter++;
1251 mode =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#endif
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001253 prepare_for_delete_or_cut(th, inode, path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001254 item_key, &removed,
1255 &del_size,
Jeff Mahoney995c7622009-03-30 14:02:47 -04001256 max_reiserfs_offset(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jeff Mahoneyee939612009-03-30 14:02:50 -04001258 RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001260 copy_item_head(&s_ih, tp_item_head(path));
Jeff Mahoneyee939612009-03-30 14:02:50 -04001261 s_del_balance.insert_size[0] = del_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jeff Mahoneyee939612009-03-30 14:02:50 -04001263 ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1264 if (ret_value != REPEAT_SEARCH)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001267 PROC_INFO_INC(sb, delete_item_restarted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jeff Mahoney098297b2014-04-23 10:00:36 -04001269 /* file system changed, repeat search */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001270 ret_value =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001271 search_for_position_by_key(sb, item_key, path);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001272 if (ret_value == IO_ERROR)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001273 break;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001274 if (ret_value == FILE_NOT_FOUND) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001275 reiserfs_warning(sb, "vs-5340",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001276 "no items of the file %K found",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001277 item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001278 break;
1279 }
1280 } /* while (1) */
1281
Jeff Mahoneyee939612009-03-30 14:02:50 -04001282 if (ret_value != CARRY_ON) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001283 unfix_nodes(&s_del_balance);
1284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001286
1287 /* reiserfs_delete_item returns item length when success */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001288 ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001289 q_ih = tp_item_head(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001290 quota_cut_bytes = ih_item_len(q_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jeff Mahoney098297b2014-04-23 10:00:36 -04001292 /*
1293 * hack so the quota code doesn't have to guess if the file has a
1294 * tail. On tail insert, we allocate quota for 1 unformatted node.
1295 * We test the offset because the tail might have been
1296 * split into multiple items, and we only want to decrement for
1297 * the unfm node once
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001299 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(q_ih)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001300 if ((le_ih_k_offset(q_ih) & (sb->s_blocksize - 1)) == 1) {
1301 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001302 } else {
1303 quota_cut_bytes = 0;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001307 if (un_bh) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001308 int off;
1309 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Jeff Mahoney098297b2014-04-23 10:00:36 -04001311 /*
1312 * We are in direct2indirect conversion, so move tail contents
1313 * to the unformatted node
1314 */
1315 /*
1316 * note, we do the copy before preparing the buffer because we
1317 * don't care about the contents of the unformatted node yet.
1318 * the only thing we really care about is the direct item's
1319 * data is in the unformatted node.
1320 *
1321 * Otherwise, we would have to call
1322 * reiserfs_prepare_for_journal on the unformatted node,
1323 * which might schedule, meaning we'd have to loop all the
1324 * way back up to the start of the while loop.
1325 *
1326 * The unformatted node must be dirtied later on. We can't be
1327 * sure here if the entire tail has been deleted yet.
1328 *
1329 * un_bh is from the page cache (all unformatted nodes are
1330 * from the page cache) and might be a highmem page. So, we
1331 * can't use un_bh->b_data.
1332 * -clm
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Cong Wang883da602011-11-25 23:14:35 +08001335 data = kmap_atomic(un_bh->b_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001336 off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_SIZE - 1));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337 memcpy(data + off,
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001338 ih_item_body(PATH_PLAST_BUFFER(path), &s_ih),
Jeff Mahoneyee939612009-03-30 14:02:50 -04001339 ret_value);
Cong Wang883da602011-11-25 23:14:35 +08001340 kunmap_atomic(data);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001342
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001343 /* Perform balancing after all resources have been collected at once. */
1344 do_balance(&s_del_balance, NULL, NULL, M_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346#ifdef REISERQUOTA_DEBUG
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001347 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348 "reiserquota delete_item(): freeing %u, id=%u type=%c",
Jeff Mahoney995c7622009-03-30 14:02:47 -04001349 quota_cut_bytes, inode->i_uid, head2type(&s_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001351 depth = reiserfs_write_unlock_nested(inode->i_sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001352 dquot_free_space_nodirty(inode, quota_cut_bytes);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001353 reiserfs_write_lock_nested(inode->i_sb, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001355 /* Return deleted body length */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001356 return ret_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Jeff Mahoney098297b2014-04-23 10:00:36 -04001359/*
1360 * Summary Of Mechanisms For Handling Collisions Between Processes:
1361 *
1362 * deletion of the body of the object is performed by iput(), with the
1363 * result that if multiple processes are operating on a file, the
1364 * deletion of the body of the file is deferred until the last process
1365 * that has an open inode performs its iput().
1366 *
1367 * writes and truncates are protected from collisions by use of
1368 * semaphores.
1369 *
1370 * creates, linking, and mknod are protected from collisions with other
1371 * processes by making the reiserfs_add_entry() the last step in the
1372 * creation, and then rolling back all changes if there was a collision.
1373 * - Hans
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374*/
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/* this deletes item which never gets split */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001377void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
1378 struct inode *inode, struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001380 struct super_block *sb = th->t_super;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001381 struct tree_balance tb;
1382 INITIALIZE_PATH(path);
1383 int item_len = 0;
1384 int tb_init = 0;
1385 struct cpu_key cpu_key;
1386 int retval;
1387 int quota_cut_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001389 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001391 le_key2cpu_key(&cpu_key, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001393 while (1) {
1394 retval = search_item(th->t_super, &cpu_key, &path);
1395 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001396 reiserfs_error(th->t_super, "vs-5350",
1397 "i/o failure occurred trying "
1398 "to delete %K", &cpu_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001399 break;
1400 }
1401 if (retval != ITEM_FOUND) {
1402 pathrelse(&path);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001403 /*
1404 * No need for a warning, if there is just no free
1405 * space to insert '..' item into the
1406 * newly-created subdir
1407 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408 if (!
1409 ((unsigned long long)
1410 GET_HASH_VALUE(le_key_k_offset
1411 (le_key_version(key), key)) == 0
1412 && (unsigned long long)
1413 GET_GENERATION_NUMBER(le_key_k_offset
1414 (le_key_version(key),
1415 key)) == 1))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001416 reiserfs_warning(th->t_super, "vs-5355",
1417 "%k not found", key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001418 break;
1419 }
1420 if (!tb_init) {
1421 tb_init = 1;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001422 item_len = ih_item_len(tp_item_head(&path));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001423 init_tb_struct(th, &tb, th->t_super, &path,
1424 -(IH_SIZE + item_len));
1425 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001426 quota_cut_bytes = ih_item_len(tp_item_head(&path));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001427
1428 retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
1429 if (retval == REPEAT_SEARCH) {
1430 PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
1431 continue;
1432 }
1433
1434 if (retval == CARRY_ON) {
1435 do_balance(&tb, NULL, NULL, M_DELETE);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001436 /*
1437 * Should we count quota for item? (we don't
1438 * count quotas for save-links)
1439 */
1440 if (inode) {
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001441 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001443 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
1444 "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
1445 quota_cut_bytes, inode->i_uid,
1446 key2type(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001448 depth = reiserfs_write_unlock_nested(sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001449 dquot_free_space_nodirty(inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001450 quota_cut_bytes);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001451 reiserfs_write_lock_nested(sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001452 }
1453 break;
1454 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001455
1456 /* IO_ERROR, NO_DISK_SPACE, etc */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001457 reiserfs_warning(th->t_super, "vs-5360",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001458 "could not delete %K due to fix_nodes failure",
1459 &cpu_key);
1460 unfix_nodes(&tb);
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001464 reiserfs_check_path(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001467int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
1468 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001470 int err;
1471 inode->i_size = 0;
1472 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001474 /* for directory this deletes item containing "." and ".." */
1475 err =
1476 reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ );
1477 if (err)
1478 return err;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480#if defined( USE_INODE_GENERATION_COUNTER )
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001481 if (!old_format_only(th->t_super)) {
1482 __le32 *inode_generation;
1483
1484 inode_generation =
1485 &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
Marcin Slusarz9e902df2008-04-28 02:16:20 -07001486 le32_add_cpu(inode_generation, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488/* USE_INODE_GENERATION_COUNTER */
1489#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001490 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001492 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001495static void unmap_buffers(struct page *page, loff_t pos)
1496{
1497 struct buffer_head *bh;
1498 struct buffer_head *head;
1499 struct buffer_head *next;
1500 unsigned long tail_index;
1501 unsigned long cur_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503 if (page) {
1504 if (page_has_buffers(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001505 tail_index = pos & (PAGE_SIZE - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001506 cur_index = 0;
1507 head = page_buffers(page);
1508 bh = head;
1509 do {
1510 next = bh->b_this_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Jeff Mahoney098297b2014-04-23 10:00:36 -04001512 /*
1513 * we want to unmap the buffers that contain
1514 * the tail, and all the buffers after it
1515 * (since the tail must be at the end of the
1516 * file). We don't want to unmap file data
1517 * before the tail, since it might be dirty
1518 * and waiting to reach disk
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 */
1520 cur_index += bh->b_size;
1521 if (cur_index > tail_index) {
1522 reiserfs_unmap_buffer(bh);
1523 }
1524 bh = next;
1525 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001530static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
Jeff Mahoney995c7622009-03-30 14:02:47 -04001531 struct inode *inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001532 struct page *page,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001533 struct treepath *path,
1534 const struct cpu_key *item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001535 loff_t new_file_size, char *mode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001536{
Jeff Mahoney995c7622009-03-30 14:02:47 -04001537 struct super_block *sb = inode->i_sb;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001538 int block_size = sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001539 int cut_bytes;
1540 BUG_ON(!th->t_trans_id);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001541 BUG_ON(new_file_size != inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Jeff Mahoney098297b2014-04-23 10:00:36 -04001543 /*
1544 * the page being sent in could be NULL if there was an i/o error
1545 * reading in the last block. The user will hit problems trying to
1546 * read the file, but for now we just skip the indirect2direct
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001547 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001548 if (atomic_read(&inode->i_count) > 1 ||
1549 !tail_has_to_be_packed(inode) ||
1550 !page || (REISERFS_I(inode)->i_flags & i_nopack_mask)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001551 /* leave tail in an unformatted node */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001552 *mode = M_SKIP_BALANCING;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001553 cut_bytes =
Jeff Mahoneyee939612009-03-30 14:02:50 -04001554 block_size - (new_file_size & (block_size - 1));
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001555 pathrelse(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001556 return cut_bytes;
1557 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001558
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001559 /* Perform the conversion to a direct_item. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001560 return indirect2direct(th, inode, page, path, item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001561 new_file_size, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Jeff Mahoney098297b2014-04-23 10:00:36 -04001564/*
1565 * we did indirect_to_direct conversion. And we have inserted direct
1566 * item successesfully, but there were no disk space to cut unfm
1567 * pointer being converted. Therefore we have to delete inserted
1568 * direct item(s)
1569 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001570static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001571 struct inode *inode, struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001573 struct cpu_key tail_key;
1574 int tail_len;
1575 int removed;
1576 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jeff Mahoney098297b2014-04-23 10:00:36 -04001578 make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001579 tail_key.key_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001581 tail_len =
1582 (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
1583 while (tail_len) {
1584 /* look for the last byte of the tail */
1585 if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
1586 POSITION_NOT_FOUND)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001587 reiserfs_panic(inode->i_sb, "vs-5615",
1588 "found invalid item");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001589 RFALSE(path->pos_in_item !=
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001590 ih_item_len(tp_item_head(path)) - 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001591 "vs-5616: appended bytes found");
1592 PATH_LAST_POSITION(path)--;
1593
1594 removed =
1595 reiserfs_delete_item(th, path, &tail_key, inode,
1596 NULL /*unbh not needed */ );
1597 RFALSE(removed <= 0
1598 || removed > tail_len,
1599 "vs-5617: there was tail %d bytes, removed item length %d bytes",
1600 tail_len, removed);
1601 tail_len -= removed;
1602 set_cpu_key_k_offset(&tail_key,
1603 cpu_key_k_offset(&tail_key) - removed);
1604 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001605 reiserfs_warning(inode->i_sb, "reiserfs-5091", "indirect_to_direct "
1606 "conversion has been rolled back due to "
1607 "lack of disk space");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001608 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001612int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001613 struct treepath *path,
1614 struct cpu_key *item_key,
Jeff Mahoney995c7622009-03-30 14:02:47 -04001615 struct inode *inode,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001616 struct page *page, loff_t new_file_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Jeff Mahoney995c7622009-03-30 14:02:47 -04001618 struct super_block *sb = inode->i_sb;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001619 /*
1620 * Every function which is going to call do_balance must first
1621 * create a tree_balance structure. Then it must fill up this
1622 * structure by using the init_tb_struct and fix_nodes functions.
1623 * After that we can make tree balancing.
1624 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001625 struct tree_balance s_cut_balance;
1626 struct item_head *p_le_ih;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001627 int cut_size = 0; /* Amount to be cut. */
1628 int ret_value = CARRY_ON;
1629 int removed = 0; /* Number of the removed unformatted nodes. */
1630 int is_inode_locked = 0;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001631 char mode; /* Mode of the balance. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001632 int retval2 = -1;
1633 int quota_cut_bytes;
1634 loff_t tail_pos = 0;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001635 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001637 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001639 init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001640 cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jeff Mahoney098297b2014-04-23 10:00:36 -04001642 /*
1643 * Repeat this loop until we either cut the item without needing
1644 * to balance, or we fix_nodes without schedule occurring
1645 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001646 while (1) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001647 /*
1648 * Determine the balance mode, position of the first byte to
1649 * be cut, and size to be cut. In case of the indirect item
1650 * free unformatted nodes which are pointed to by the cut
1651 * pointers.
1652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jeff Mahoneyee939612009-03-30 14:02:50 -04001654 mode =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001655 prepare_for_delete_or_cut(th, inode, path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001656 item_key, &removed,
1657 &cut_size, new_file_size);
1658 if (mode == M_CONVERT) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001659 /*
1660 * convert last unformatted node to direct item or
1661 * leave tail in the unformatted node
1662 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001663 RFALSE(ret_value != CARRY_ON,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001664 "PAP-5570: can not convert twice");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jeff Mahoneyee939612009-03-30 14:02:50 -04001666 ret_value =
Jeff Mahoney995c7622009-03-30 14:02:47 -04001667 maybe_indirect_to_direct(th, inode, page,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001668 path, item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001669 new_file_size, &mode);
1670 if (mode == M_SKIP_BALANCING)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001671 /* tail has been left in the unformatted node */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001672 return ret_value;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001673
Jeff Mahoneyee939612009-03-30 14:02:50 -04001674 is_inode_locked = 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001675
Jeff Mahoney098297b2014-04-23 10:00:36 -04001676 /*
1677 * removing of last unformatted node will
1678 * change value we have to return to truncate.
1679 * Save it
1680 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001681 retval2 = ret_value;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001682
Jeff Mahoney098297b2014-04-23 10:00:36 -04001683 /*
1684 * So, we have performed the first part of the
1685 * conversion:
1686 * inserting the new direct item. Now we are
1687 * removing the last unformatted node pointer.
1688 * Set key to search for it.
1689 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001690 set_cpu_key_k_type(item_key, TYPE_INDIRECT);
1691 item_key->key_length = 4;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001692 new_file_size -=
1693 (new_file_size & (sb->s_blocksize - 1));
1694 tail_pos = new_file_size;
1695 set_cpu_key_k_offset(item_key, new_file_size + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001696 if (search_for_position_by_key
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001697 (sb, item_key,
1698 path) == POSITION_NOT_FOUND) {
1699 print_block(PATH_PLAST_BUFFER(path), 3,
1700 PATH_LAST_POSITION(path) - 1,
1701 PATH_LAST_POSITION(path) + 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001702 reiserfs_panic(sb, "PAP-5580", "item to "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001703 "convert does not exist (%K)",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001704 item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001705 }
1706 continue;
1707 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04001708 if (cut_size == 0) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001709 pathrelse(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001710 return 0;
1711 }
1712
Jeff Mahoneyee939612009-03-30 14:02:50 -04001713 s_cut_balance.insert_size[0] = cut_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001714
Jeff Mahoneyee939612009-03-30 14:02:50 -04001715 ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
1716 if (ret_value != REPEAT_SEARCH)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001717 break;
1718
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001719 PROC_INFO_INC(sb, cut_from_item_restarted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001720
Jeff Mahoneyee939612009-03-30 14:02:50 -04001721 ret_value =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001722 search_for_position_by_key(sb, item_key, path);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001723 if (ret_value == POSITION_FOUND)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001724 continue;
1725
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001726 reiserfs_warning(sb, "PAP-5610", "item %K not found",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001727 item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001728 unfix_nodes(&s_cut_balance);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001729 return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001730 } /* while */
1731
Jeff Mahoney098297b2014-04-23 10:00:36 -04001732 /* check fix_nodes results (IO_ERROR or NO_DISK_SPACE) */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001733 if (ret_value != CARRY_ON) {
1734 if (is_inode_locked) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001735 /*
1736 * FIXME: this seems to be not needed: we are always
1737 * able to cut item
1738 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001739 indirect_to_direct_roll_back(th, inode, path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001740 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04001741 if (ret_value == NO_DISK_SPACE)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001742 reiserfs_warning(sb, "reiserfs-5092",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001743 "NO_DISK_SPACE");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001744 unfix_nodes(&s_cut_balance);
1745 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001748 /* go ahead and perform balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Jeff Mahoneyee939612009-03-30 14:02:50 -04001750 RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001752 /* Calculate number of bytes that need to be cut from the item. */
1753 quota_cut_bytes =
Jeff Mahoneyee939612009-03-30 14:02:50 -04001754 (mode ==
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001755 M_DELETE) ? ih_item_len(tp_item_head(path)) : -s_cut_balance.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001756 insert_size[0];
1757 if (retval2 == -1)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001758 ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001759 else
Jeff Mahoneyee939612009-03-30 14:02:50 -04001760 ret_value = retval2;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001761
Jeff Mahoney098297b2014-04-23 10:00:36 -04001762 /*
1763 * For direct items, we only change the quota when deleting the last
1764 * item.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001765 */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001766 p_le_ih = tp_item_head(s_cut_balance.tb_path);
Jeff Mahoney995c7622009-03-30 14:02:47 -04001767 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
Jeff Mahoneyee939612009-03-30 14:02:50 -04001768 if (mode == M_DELETE &&
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001769 (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001770 1) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001771 /* FIXME: this is to keep 3.5 happy */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001772 REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001773 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001774 } else {
1775 quota_cut_bytes = 0;
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -04001779 if (is_inode_locked) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001780 struct item_head *le_ih =
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001781 tp_item_head(s_cut_balance.tb_path);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001782 /*
1783 * we are going to complete indirect2direct conversion. Make
1784 * sure, that we exactly remove last unformatted node pointer
1785 * of the item
1786 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001787 if (!is_indirect_le_ih(le_ih))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001788 reiserfs_panic(sb, "vs-5652",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001789 "item must be indirect %h", le_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jeff Mahoneyee939612009-03-30 14:02:50 -04001791 if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001792 reiserfs_panic(sb, "vs-5653", "completing "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001793 "indirect2direct conversion indirect "
1794 "item %h being deleted must be of "
1795 "4 byte long", le_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Jeff Mahoneyee939612009-03-30 14:02:50 -04001797 if (mode == M_CUT
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001798 && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001799 reiserfs_panic(sb, "vs-5654", "can not complete "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001800 "indirect2direct conversion of %h "
1801 "(CUT, insert_size==%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001802 le_ih, s_cut_balance.insert_size[0]);
1803 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001804 /*
1805 * it would be useful to make sure, that right neighboring
1806 * item is direct item of this file
1807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001810
Jeff Mahoneyee939612009-03-30 14:02:50 -04001811 do_balance(&s_cut_balance, NULL, NULL, mode);
1812 if (is_inode_locked) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001813 /*
1814 * we've done an indirect->direct conversion. when the
1815 * data block was freed, it was removed from the list of
1816 * blocks that must be flushed before the transaction
1817 * commits, make sure to unmap and invalidate it
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001818 */
1819 unmap_buffers(page, tail_pos);
Jeff Mahoney995c7622009-03-30 14:02:47 -04001820 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822#ifdef REISERQUOTA_DEBUG
Jeff Mahoney995c7622009-03-30 14:02:47 -04001823 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001824 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
Jeff Mahoney995c7622009-03-30 14:02:47 -04001825 quota_cut_bytes, inode->i_uid, '?');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001827 depth = reiserfs_write_unlock_nested(sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001828 dquot_free_space_nodirty(inode, quota_cut_bytes);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001829 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001830 return ret_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001833static void truncate_directory(struct reiserfs_transaction_handle *th,
1834 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001836 BUG_ON(!th->t_trans_id);
1837 if (inode->i_nlink)
Jeff Mahoney0030b642009-03-30 14:02:28 -04001838 reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001840 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
1841 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
1842 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1843 reiserfs_update_sd(th, inode);
1844 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
1845 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Jeff Mahoney098297b2014-04-23 10:00:36 -04001848/*
1849 * Truncate file to the new size. Note, this must be called with a
1850 * transaction already started
1851 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001852int reiserfs_do_truncate(struct reiserfs_transaction_handle *th,
Jeff Mahoney098297b2014-04-23 10:00:36 -04001853 struct inode *inode, /* ->i_size contains new size */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001854 struct page *page, /* up to date for last block */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001855 /*
1856 * when it is called by file_release to convert
1857 * the tail - no timestamps should be updated
1858 */
1859 int update_timestamps
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001860 )
1861{
1862 INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
1863 struct item_head *p_le_ih; /* Pointer to an item header. */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001864
1865 /* Key to search for a previous file item. */
1866 struct cpu_key s_item_key;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001867 loff_t file_size, /* Old file size. */
1868 new_file_size; /* New file size. */
1869 int deleted; /* Number of deleted or truncated bytes. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001870 int retval;
1871 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001873 BUG_ON(!th->t_trans_id);
1874 if (!
Jeff Mahoney995c7622009-03-30 14:02:47 -04001875 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1876 || S_ISLNK(inode->i_mode)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Jeff Mahoney098297b2014-04-23 10:00:36 -04001879 /* deletion of directory - no need to update timestamps */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001880 if (S_ISDIR(inode->i_mode)) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001881 truncate_directory(th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001885 /* Get new file size. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001886 new_file_size = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Jeff Mahoney098297b2014-04-23 10:00:36 -04001888 /* FIXME: note, that key type is unimportant here */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001889 make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001890 TYPE_DIRECT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001892 retval =
Jeff Mahoney995c7622009-03-30 14:02:47 -04001893 search_for_position_by_key(inode->i_sb, &s_item_key,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001894 &s_search_path);
1895 if (retval == IO_ERROR) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001896 reiserfs_error(inode->i_sb, "vs-5657",
Jeff Mahoney0030b642009-03-30 14:02:28 -04001897 "i/o failure occurred trying to truncate %K",
1898 &s_item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001899 err = -EIO;
1900 goto out;
1901 }
1902 if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001903 reiserfs_error(inode->i_sb, "PAP-5660",
Jeff Mahoney0030b642009-03-30 14:02:28 -04001904 "wrong result %d of search for %K", retval,
1905 &s_item_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001907 err = -EIO;
1908 goto out;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001911 s_search_path.pos_in_item--;
1912
1913 /* Get real file size (total length of all file items) */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001914 p_le_ih = tp_item_head(&s_search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001915 if (is_statdata_le_ih(p_le_ih))
Jeff Mahoneyee939612009-03-30 14:02:50 -04001916 file_size = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001917 else {
1918 loff_t offset = le_ih_k_offset(p_le_ih);
1919 int bytes =
Jeff Mahoney995c7622009-03-30 14:02:47 -04001920 op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001921
Jeff Mahoney098297b2014-04-23 10:00:36 -04001922 /*
1923 * this may mismatch with real file size: if last direct item
1924 * had no padding zeros and last unformatted node had no free
1925 * space, this file would have this file size
1926 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001927 file_size = offset + bytes - 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /*
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001930 * are we doing a full truncate or delete, if so
1931 * kick in the reada code
1932 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001933 if (new_file_size == 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001934 s_search_path.reada = PATH_READA | PATH_READA_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Jeff Mahoneyee939612009-03-30 14:02:50 -04001936 if (file_size == 0 || file_size < new_file_size) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001937 goto update_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001940 /* Update key to search for the last file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001941 set_cpu_key_k_offset(&s_item_key, file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001943 do {
1944 /* Cut or delete file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001945 deleted =
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001946 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001947 inode, page, new_file_size);
1948 if (deleted < 0) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001949 reiserfs_warning(inode->i_sb, "vs-5665",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001950 "reiserfs_cut_from_item failed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001951 reiserfs_check_path(&s_search_path);
1952 return 0;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Jeff Mahoneyee939612009-03-30 14:02:50 -04001955 RFALSE(deleted > file_size,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001956 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
Jeff Mahoneyee939612009-03-30 14:02:50 -04001957 deleted, file_size, &s_item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001958
1959 /* Change key to search the last file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001960 file_size -= deleted;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001961
Jeff Mahoneyee939612009-03-30 14:02:50 -04001962 set_cpu_key_k_offset(&s_item_key, file_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001963
Jeff Mahoney098297b2014-04-23 10:00:36 -04001964 /*
1965 * While there are bytes to truncate and previous
1966 * file item is presented in the tree.
1967 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001968
1969 /*
Jeff Mahoney098297b2014-04-23 10:00:36 -04001970 * This loop could take a really long time, and could log
1971 * many more blocks than a transaction can hold. So, we do
1972 * a polite journal end here, and if the transaction needs
1973 * ending, we make sure the file is consistent before ending
1974 * the current trans and starting a new one
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001975 */
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001976 if (journal_transaction_should_end(th, 0) ||
1977 reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -04001978 pathrelse(&s_search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001979
1980 if (update_timestamps) {
Deepa Dinamani02027d42016-09-14 07:48:05 -07001981 inode->i_mtime = current_time(inode);
1982 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001983 }
Jeff Mahoney995c7622009-03-30 14:02:47 -04001984 reiserfs_update_sd(th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001985
Jeff Mahoney58d85422014-04-23 10:00:38 -04001986 err = journal_end(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001987 if (err)
1988 goto out;
Jeff Mahoney995c7622009-03-30 14:02:47 -04001989 err = journal_begin(th, inode->i_sb,
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001990 JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001991 if (err)
1992 goto out;
Jeff Mahoney995c7622009-03-30 14:02:47 -04001993 reiserfs_update_inode_transaction(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001994 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04001995 } while (file_size > ROUND_UP(new_file_size) &&
Jeff Mahoney995c7622009-03-30 14:02:47 -04001996 search_for_position_by_key(inode->i_sb, &s_item_key,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001997 &s_search_path) == POSITION_FOUND);
1998
Jeff Mahoneyee939612009-03-30 14:02:50 -04001999 RFALSE(file_size > ROUND_UP(new_file_size),
Fabian Frederick53872ed2014-08-08 14:21:10 -07002000 "PAP-5680: truncate did not finish: new_file_size %lld, current %lld, oid %d",
Jeff Mahoneyee939612009-03-30 14:02:50 -04002001 new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002002
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002003update_and_out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002004 if (update_timestamps) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04002005 /* this is truncate, not file closing */
Deepa Dinamani02027d42016-09-14 07:48:05 -07002006 inode->i_mtime = current_time(inode);
2007 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002008 }
Jeff Mahoney995c7622009-03-30 14:02:47 -04002009 reiserfs_update_sd(th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002010
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002011out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002012 pathrelse(&s_search_path);
2013 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney098297b2014-04-23 10:00:36 -04002017/* this makes sure, that we __append__, not overwrite or add holes */
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08002018static void check_research_for_paste(struct treepath *path,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002019 const struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04002021 struct item_head *found_ih = tp_item_head(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002023 if (is_direct_le_ih(found_ih)) {
2024 if (le_ih_k_offset(found_ih) +
2025 op_bytes_number(found_ih,
2026 get_last_bh(path)->b_size) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002027 cpu_key_k_offset(key)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002028 || op_bytes_number(found_ih,
2029 get_last_bh(path)->b_size) !=
2030 pos_in_item(path))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002031 reiserfs_panic(NULL, "PAP-5720", "found direct item "
2032 "%h or position (%d) does not match "
2033 "to key %K", found_ih,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002034 pos_in_item(path), key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002035 }
2036 if (is_indirect_le_ih(found_ih)) {
2037 if (le_ih_k_offset(found_ih) +
2038 op_bytes_number(found_ih,
2039 get_last_bh(path)->b_size) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002040 cpu_key_k_offset(key)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002041 || I_UNFM_NUM(found_ih) != pos_in_item(path)
2042 || get_ih_free_space(found_ih) != 0)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04002043 reiserfs_panic(NULL, "PAP-5730", "found indirect "
2044 "item (%h) or position (%d) does not "
2045 "match to key (%K)",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002046 found_ih, pos_in_item(path), key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002047 }
2048}
2049#endif /* config reiserfs check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Jeff Mahoney098297b2014-04-23 10:00:36 -04002051/*
2052 * Paste bytes to the existing item.
2053 * Returns bytes number pasted into the item.
2054 */
2055int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th,
2056 /* Path to the pasted item. */
2057 struct treepath *search_path,
2058 /* Key to search for the needed item. */
2059 const struct cpu_key *key,
2060 /* Inode item belongs to */
2061 struct inode *inode,
2062 /* Pointer to the bytes to paste. */
2063 const char *body,
2064 /* Size of pasted bytes. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04002065 int pasted_size)
Jeff Mahoney098297b2014-04-23 10:00:36 -04002066{
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002067 struct super_block *sb = inode->i_sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002068 struct tree_balance s_paste_balance;
2069 int retval;
2070 int fs_gen;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002071 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002073 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002075 fs_gen = get_generation(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002078 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2079 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
Jeff Mahoneyee939612009-03-30 14:02:50 -04002080 pasted_size, inode->i_uid,
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002081 key2type(&key->on_disk_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#endif
2083
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002084 depth = reiserfs_write_unlock_nested(sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002085 retval = dquot_alloc_space_nodirty(inode, pasted_size);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002086 reiserfs_write_lock_nested(sb, depth);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002087 if (retval) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002088 pathrelse(search_path);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002089 return retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002090 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002091 init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04002092 pasted_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002094 s_paste_balance.key = key->on_disk_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095#endif
2096
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002097 /* DQUOT_* can schedule, must check before the fix_nodes */
2098 if (fs_changed(fs_gen, inode->i_sb)) {
2099 goto search_again;
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002102 while ((retval =
2103 fix_nodes(M_PASTE, &s_paste_balance, NULL,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002104 body)) == REPEAT_SEARCH) {
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002105search_again:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002106 /* file system changed while we were in the fix_nodes */
2107 PROC_INFO_INC(th->t_super, paste_into_item_restarted);
2108 retval =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002109 search_for_position_by_key(th->t_super, key,
2110 search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002111 if (retval == IO_ERROR) {
2112 retval = -EIO;
2113 goto error_out;
2114 }
2115 if (retval == POSITION_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002116 reiserfs_warning(inode->i_sb, "PAP-5710",
2117 "entry or pasted byte (%K) exists",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002118 key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002119 retval = -EEXIST;
2120 goto error_out;
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002123 check_research_for_paste(search_path, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Jeff Mahoney098297b2014-04-23 10:00:36 -04002127 /*
2128 * Perform balancing after all resources are collected by fix_nodes,
2129 * and accessing them will not risk triggering schedule.
2130 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002131 if (retval == CARRY_ON) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002132 do_balance(&s_paste_balance, NULL /*ih */ , body, M_PASTE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002133 return 0;
2134 }
2135 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002136error_out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002137 /* this also releases the path */
2138 unfix_nodes(&s_paste_balance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002140 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2141 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
Jeff Mahoneyee939612009-03-30 14:02:50 -04002142 pasted_size, inode->i_uid,
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002143 key2type(&key->on_disk_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002145 depth = reiserfs_write_unlock_nested(sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002146 dquot_free_space_nodirty(inode, pasted_size);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002147 reiserfs_write_lock_nested(sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002148 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Jeff Mahoney098297b2014-04-23 10:00:36 -04002151/*
2152 * Insert new item into the buffer at the path.
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002153 * th - active transaction handle
2154 * path - path to the inserted item
2155 * ih - pointer to the item header to insert
2156 * body - pointer to the bytes to insert
2157 */
2158int reiserfs_insert_item(struct reiserfs_transaction_handle *th,
2159 struct treepath *path, const struct cpu_key *key,
2160 struct item_head *ih, struct inode *inode,
2161 const char *body)
2162{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002163 struct tree_balance s_ins_balance;
2164 int retval;
2165 int fs_gen = 0;
2166 int quota_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002168 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002170 if (inode) { /* Do we count quotas for item? */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002171 int depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002172 fs_gen = get_generation(inode->i_sb);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002173 quota_bytes = ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Jeff Mahoney098297b2014-04-23 10:00:36 -04002175 /*
2176 * hack so the quota code doesn't have to guess
2177 * if the file has a tail, links are always tails,
2178 * so there's no guessing needed
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002179 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002180 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(ih))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002181 quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002183 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2184 "reiserquota insert_item(): allocating %u id=%u type=%c",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002185 quota_bytes, inode->i_uid, head2type(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#endif
Jeff Mahoney098297b2014-04-23 10:00:36 -04002187 /*
2188 * We can't dirty inode here. It would be immediately
2189 * written but appropriate stat item isn't inserted yet...
2190 */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002191 depth = reiserfs_write_unlock_nested(inode->i_sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002192 retval = dquot_alloc_space_nodirty(inode, quota_bytes);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002193 reiserfs_write_lock_nested(inode->i_sb, depth);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002194 if (retval) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002195 pathrelse(path);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002196 return retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002199 init_tb_struct(th, &s_ins_balance, th->t_super, path,
2200 IH_SIZE + ih_item_len(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002202 s_ins_balance.key = key->on_disk_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203#endif
Jeff Mahoney098297b2014-04-23 10:00:36 -04002204 /*
2205 * DQUOT_* can schedule, must check to be sure calling
2206 * fix_nodes is safe
2207 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002208 if (inode && fs_changed(fs_gen, inode->i_sb)) {
2209 goto search_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002211
2212 while ((retval =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002213 fix_nodes(M_INSERT, &s_ins_balance, ih,
2214 body)) == REPEAT_SEARCH) {
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002215search_again:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002216 /* file system changed while we were in the fix_nodes */
2217 PROC_INFO_INC(th->t_super, insert_item_restarted);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002218 retval = search_item(th->t_super, key, path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002219 if (retval == IO_ERROR) {
2220 retval = -EIO;
2221 goto error_out;
2222 }
2223 if (retval == ITEM_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002224 reiserfs_warning(th->t_super, "PAP-5760",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002225 "key %K already exists in the tree",
2226 key);
2227 retval = -EEXIST;
2228 goto error_out;
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002232 /* make balancing after all resources will be collected at a time */
2233 if (retval == CARRY_ON) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002234 do_balance(&s_ins_balance, ih, body, M_INSERT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002235 return 0;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002238 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
Jeff Mahoneycf776a72014-04-23 10:00:41 -04002239error_out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002240 /* also releases the path */
2241 unfix_nodes(&s_ins_balance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#ifdef REISERQUOTA_DEBUG
Yunfeng Yeaacee542020-01-30 22:17:26 -08002243 if (inode)
2244 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002245 "reiserquota insert_item(): freeing %u id=%u type=%c",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002246 quota_bytes, inode->i_uid, head2type(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002248 if (inode) {
2249 int depth = reiserfs_write_unlock_nested(inode->i_sb);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002250 dquot_free_space_nodirty(inode, quota_bytes);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002251 reiserfs_write_lock_nested(inode->i_sb, depth);
2252 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002253 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}