HAMMER - Add live dedup sysctl and support
[dragonfly.git] / sys / vfs / hammer / hammer_object.c
CommitLineData
66325755 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
66325755
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
35a49944 34 * $DragonFly: src/sys/vfs/hammer/hammer_object.c,v 1.97 2008/09/23 22:28:56 dillon Exp $
66325755
MD
35 */
36
37#include "hammer.h"
38
45a014dc 39static int hammer_mem_lookup(hammer_cursor_t cursor);
3214ade6 40static void hammer_mem_first(hammer_cursor_t cursor);
312de84d 41static int hammer_frontend_trunc_callback(hammer_record_t record,
0832c9bb 42 void *data __unused);
d7e278bb 43static int hammer_bulk_scan_callback(hammer_record_t record, void *data);
cb51be26 44static int hammer_record_needs_overwrite_delete(hammer_record_t record);
5fa5c92f 45static int hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
83f2a3aa
MD
46 hammer_btree_leaf_elm_t leaf);
47static int hammer_cursor_localize_data(hammer_data_ondisk_t data,
48 hammer_btree_leaf_elm_t leaf);
8cd0a023 49
47637bff
MD
50struct rec_trunc_info {
51 u_int16_t rec_type;
52 int64_t trunc_off;
53};
54
d7e278bb
MD
55struct hammer_bulk_info {
56 hammer_record_t record;
6362a262 57 hammer_record_t conflict;
d7e278bb
MD
58};
59
66325755 60/*
0832c9bb 61 * Red-black tree support. Comparison code for insertion.
66325755 62 */
8cd0a023 63static int
a89aec1b 64hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
8cd0a023 65{
11ad5ade 66 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
8cd0a023 67 return(-1);
11ad5ade 68 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
8cd0a023
MD
69 return(1);
70
11ad5ade 71 if (rec1->leaf.base.key < rec2->leaf.base.key)
8cd0a023 72 return(-1);
11ad5ade 73 if (rec1->leaf.base.key > rec2->leaf.base.key)
8cd0a023
MD
74 return(1);
75
ec4e8497 76 /*
3214ade6
MD
77 * For search & insertion purposes records deleted by the
78 * frontend or deleted/committed by the backend are silently
79 * ignored. Otherwise pipelined insertions will get messed
80 * up.
bf3b416b
MD
81 *
82 * rec1 is greater then rec2 if rec1 is marked deleted.
83 * rec1 is less then rec2 if rec2 is marked deleted.
84 *
85 * Multiple deleted records may be present, do not return 0
86 * if both are marked deleted.
ec4e8497 87 */
3214ade6
MD
88 if (rec1->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
89 HAMMER_RECF_COMMITTED)) {
ec4e8497 90 return(1);
3214ade6
MD
91 }
92 if (rec2->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
93 HAMMER_RECF_COMMITTED)) {
ec4e8497 94 return(-1);
3214ade6 95 }
ec4e8497 96
8cd0a023
MD
97 return(0);
98}
99
0832c9bb
MD
100/*
101 * Basic record comparison code similar to hammer_btree_cmp().
c9ce54d6
MD
102 *
103 * obj_id is not compared and may not yet be assigned in the record.
0832c9bb 104 */
8cd0a023 105static int
0832c9bb 106hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
66325755 107{
0832c9bb 108 if (elm->rec_type < rec->leaf.base.rec_type)
d26d0ae9 109 return(-3);
0832c9bb 110 if (elm->rec_type > rec->leaf.base.rec_type)
d26d0ae9 111 return(3);
8cd0a023 112
0832c9bb 113 if (elm->key < rec->leaf.base.key)
8cd0a023 114 return(-2);
0832c9bb 115 if (elm->key > rec->leaf.base.key)
8cd0a023
MD
116 return(2);
117
cebe9493 118 /*
3214ade6
MD
119 * Never match against an item deleted by the frontend
120 * or backend, or committed by the backend.
121 *
bf3b416b 122 * elm is less then rec if rec is marked deleted.
cebe9493 123 */
3214ade6
MD
124 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
125 HAMMER_RECF_COMMITTED)) {
bf3b416b 126 return(-1);
3214ade6 127 }
0832c9bb
MD
128 return(0);
129}
130
131/*
d7e278bb
MD
132 * Ranged scan to locate overlapping record(s). This is used by
133 * hammer_ip_get_bulk() to locate an overlapping record. We have
134 * to use a ranged scan because the keys for data records with the
135 * same file base offset can be different due to differing data_len's.
0832c9bb
MD
136 *
137 * NOTE: The base file offset of a data record is (key - data_len), not (key).
138 */
139static int
d7e278bb 140hammer_rec_overlap_cmp(hammer_record_t rec, void *data)
0832c9bb 141{
d7e278bb 142 struct hammer_bulk_info *info = data;
6362a262 143 hammer_btree_leaf_elm_t leaf = &info->record->leaf;
d7e278bb
MD
144
145 if (rec->leaf.base.rec_type < leaf->base.rec_type)
0832c9bb 146 return(-3);
d7e278bb 147 if (rec->leaf.base.rec_type > leaf->base.rec_type)
0832c9bb
MD
148 return(3);
149
4a2796f3
MD
150 /*
151 * Overlap compare
152 */
0832c9bb 153 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
d7e278bb
MD
154 /* rec_beg >= leaf_end */
155 if (rec->leaf.base.key - rec->leaf.data_len >= leaf->base.key)
0832c9bb 156 return(2);
d7e278bb
MD
157 /* rec_end <= leaf_beg */
158 if (rec->leaf.base.key <= leaf->base.key - leaf->data_len)
159 return(-2);
0832c9bb 160 } else {
d7e278bb 161 if (rec->leaf.base.key < leaf->base.key)
0832c9bb 162 return(-2);
d7e278bb 163 if (rec->leaf.base.key > leaf->base.key)
0832c9bb
MD
164 return(2);
165 }
166
cebe9493 167 /*
d7e278bb
MD
168 * We have to return 0 at this point, even if DELETED_FE is set,
169 * because returning anything else will cause the scan to ignore
170 * one of the branches when we really want it to check both.
cebe9493 171 */
8cd0a023 172 return(0);
66325755
MD
173}
174
6b4f890b 175/*
7f7c1f84 176 * RB_SCAN comparison code for hammer_mem_first(). The argument order
6b4f890b 177 * is reversed so the comparison result has to be negated. key_beg and
7f7c1f84
MD
178 * key_end are both range-inclusive.
179 *
7f7c1f84 180 * Localized deletions are not cached in-memory.
6b4f890b
MD
181 */
182static
183int
184hammer_rec_scan_cmp(hammer_record_t rec, void *data)
185{
186 hammer_cursor_t cursor = data;
187 int r;
188
0832c9bb 189 r = hammer_rec_cmp(&cursor->key_beg, rec);
7f7c1f84 190 if (r > 1)
6b4f890b 191 return(-1);
0832c9bb 192 r = hammer_rec_cmp(&cursor->key_end, rec);
7f7c1f84 193 if (r < -1)
6b4f890b
MD
194 return(1);
195 return(0);
196}
197
2ae23e2e
MD
198/*
199 * This compare function is used when simply looking up key_beg.
200 */
201static
202int
203hammer_rec_find_cmp(hammer_record_t rec, void *data)
204{
205 hammer_cursor_t cursor = data;
206 int r;
207
0832c9bb 208 r = hammer_rec_cmp(&cursor->key_beg, rec);
2ae23e2e
MD
209 if (r > 1)
210 return(-1);
211 if (r < -1)
212 return(1);
213 return(0);
214}
215
47637bff
MD
216/*
217 * Locate blocks within the truncation range. Partial blocks do not count.
218 */
219static
220int
221hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
222{
223 struct rec_trunc_info *info = data;
224
225 if (rec->leaf.base.rec_type < info->rec_type)
226 return(-1);
227 if (rec->leaf.base.rec_type > info->rec_type)
228 return(1);
229
230 switch(rec->leaf.base.rec_type) {
231 case HAMMER_RECTYPE_DB:
232 /*
233 * DB record key is not beyond the truncation point, retain.
234 */
235 if (rec->leaf.base.key < info->trunc_off)
236 return(-1);
237 break;
238 case HAMMER_RECTYPE_DATA:
239 /*
240 * DATA record offset start is not beyond the truncation point,
241 * retain.
242 */
243 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
244 return(-1);
245 break;
246 default:
247 panic("hammer_rec_trunc_cmp: unexpected record type");
248 }
249
250 /*
251 * The record start is >= the truncation point, return match,
252 * the record should be destroyed.
253 */
254 return(0);
255}
256
8cd0a023 257RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
8cd0a023
MD
258
259/*
d26d0ae9 260 * Allocate a record for the caller to finish filling in. The record is
b3deaf57 261 * returned referenced.
a89aec1b
MD
262 */
263hammer_record_t
11ad5ade 264hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
a89aec1b
MD
265{
266 hammer_record_t record;
bac808fe 267 hammer_mount_t hmp;
a89aec1b 268
bac808fe 269 hmp = ip->hmp;
b3deaf57 270 ++hammer_count_records;
bac808fe 271 record = kmalloc(sizeof(*record), hmp->m_misc,
df301614 272 M_WAITOK | M_ZERO | M_USE_RESERVE);
1f07f686 273 record->flush_state = HAMMER_FST_IDLE;
a89aec1b 274 record->ip = ip;
11ad5ade
MD
275 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
276 record->leaf.data_len = data_len;
d26d0ae9 277 hammer_ref(&record->lock);
11ad5ade
MD
278
279 if (data_len) {
bac808fe 280 record->data = kmalloc(data_len, hmp->m_misc, M_WAITOK | M_ZERO);
11ad5ade
MD
281 record->flags |= HAMMER_RECF_ALLOCDATA;
282 ++hammer_count_record_datas;
283 }
284
a89aec1b
MD
285 return (record);
286}
287
b84de5af 288void
af209b0f 289hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
b84de5af 290{
1f07f686 291 while (record->flush_state == HAMMER_FST_FLUSH) {
b84de5af 292 record->flags |= HAMMER_RECF_WANTED;
af209b0f 293 tsleep(record, 0, ident, 0);
b84de5af
MD
294 }
295}
296
297/*
d36ec43b
MD
298 * Called from the backend, hammer_inode.c, after a record has been
299 * flushed to disk. The record has been exclusively locked by the
300 * caller and interlocked with BE.
b84de5af 301 *
d36ec43b
MD
302 * We clean up the state, unlock, and release the record (the record
303 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
b84de5af
MD
304 */
305void
d36ec43b 306hammer_flush_record_done(hammer_record_t record, int error)
b84de5af 307{
1f07f686 308 hammer_inode_t target_ip;
1f07f686
MD
309
310 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
d36ec43b
MD
311 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
312
3214ade6
MD
313 /*
314 * If an error occured, the backend was unable to sync the
315 * record to its media. Leave the record intact.
316 */
d36ec43b 317 if (error) {
cdb6e4e6
MD
318 hammer_critical_error(record->ip->hmp, record->ip, error,
319 "while flushing record");
d36ec43b 320 }
4e17f465 321
7a61b85d
MD
322 --record->flush_group->refs;
323 record->flush_group = NULL;
324
3214ade6
MD
325 /*
326 * Adjust the flush state and dependancy based on success or
327 * failure.
328 */
329 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
1f07f686
MD
330 if ((target_ip = record->target_ip) != NULL) {
331 TAILQ_REMOVE(&target_ip->target_list, record,
332 target_entry);
333 record->target_ip = NULL;
334 hammer_test_inode(target_ip);
335 }
336 record->flush_state = HAMMER_FST_IDLE;
337 } else {
c4bae5fd 338 if (record->target_ip) {
1f07f686 339 record->flush_state = HAMMER_FST_SETUP;
c4bae5fd
MD
340 hammer_test_inode(record->ip);
341 hammer_test_inode(record->target_ip);
342 } else {
1f07f686 343 record->flush_state = HAMMER_FST_IDLE;
c4bae5fd 344 }
1f07f686 345 }
d36ec43b 346 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
3214ade6
MD
347
348 /*
349 * Cleanup
350 */
b84de5af
MD
351 if (record->flags & HAMMER_RECF_WANTED) {
352 record->flags &= ~HAMMER_RECF_WANTED;
353 wakeup(record);
354 }
355 hammer_rel_mem_record(record);
356}
357
ec4e8497 358/*
b3deaf57
MD
359 * Release a memory record. Records marked for deletion are immediately
360 * removed from the RB-Tree but otherwise left intact until the last ref
361 * goes away.
a89aec1b
MD
362 */
363void
b3deaf57 364hammer_rel_mem_record(struct hammer_record *record)
a89aec1b 365{
1b0ab2c3
MD
366 hammer_mount_t hmp;
367 hammer_reserve_t resv;
368 hammer_inode_t ip;
369 hammer_inode_t target_ip;
de996e86 370 int diddrop;
1f07f686 371
250aec18 372 hammer_rel(&record->lock);
b33e2cc0 373
250aec18 374 if (hammer_norefs(&record->lock)) {
0832c9bb
MD
375 /*
376 * Upon release of the last reference wakeup any waiters.
377 * The record structure may get destroyed so callers will
378 * loop up and do a relookup.
bf3b416b
MD
379 *
380 * WARNING! Record must be removed from RB-TREE before we
381 * might possibly block. hammer_test_inode() can block!
0832c9bb
MD
382 */
383 ip = record->ip;
1b0ab2c3 384 hmp = ip->hmp;
0832c9bb
MD
385
386 /*
387 * Upon release of the last reference a record marked deleted
3214ade6 388 * by the front or backend, or committed by the backend,
0832c9bb
MD
389 * is destroyed.
390 */
3214ade6
MD
391 if (record->flags & (HAMMER_RECF_DELETED_FE |
392 HAMMER_RECF_DELETED_BE |
393 HAMMER_RECF_COMMITTED)) {
250aec18 394 KKASSERT(hammer_isactive(&ip->lock) > 0);
1f07f686
MD
395 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
396
bf3b416b
MD
397 /*
398 * target_ip may have zero refs, we have to ref it
399 * to prevent it from being ripped out from under
400 * us.
401 */
1f07f686
MD
402 if ((target_ip = record->target_ip) != NULL) {
403 TAILQ_REMOVE(&target_ip->target_list,
404 record, target_entry);
405 record->target_ip = NULL;
bf3b416b 406 hammer_ref(&target_ip->lock);
1f07f686 407 }
d36ec43b 408
de996e86
MD
409 /*
410 * Remove the record from the B-Tree
411 */
b84de5af
MD
412 if (record->flags & HAMMER_RECF_ONRBTREE) {
413 RB_REMOVE(hammer_rec_rb_tree,
414 &record->ip->rec_tree,
415 record);
416 record->flags &= ~HAMMER_RECF_ONRBTREE;
de996e86 417 KKASSERT(ip->rsv_recs > 0);
2010e32f
MD
418 if (RB_EMPTY(&record->ip->rec_tree)) {
419 record->ip->flags &=
420 ~HAMMER_INODE_XDIRTY;
421 record->ip->sync_flags &=
422 ~HAMMER_INODE_XDIRTY;
423 }
de996e86
MD
424 diddrop = 1;
425 } else {
426 diddrop = 0;
b84de5af 427 }
bf3b416b
MD
428
429 /*
1b0ab2c3 430 * We must wait for any direct-IO to complete before
e469566b
MD
431 * we can destroy the record because the bio may
432 * have a reference to it.
1b0ab2c3 433 */
77912481
MD
434 if (record->gflags &
435 (HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL)) {
1b0ab2c3 436 hammer_io_direct_wait(record);
e469566b 437 }
1b0ab2c3 438
de996e86
MD
439 /*
440 * Account for the completion after the direct IO
441 * has completed.
442 */
443 if (diddrop) {
444 --hmp->rsv_recs;
445 --ip->rsv_recs;
446 hmp->rsv_databytes -= record->leaf.data_len;
447
2010e32f 448 if (RB_EMPTY(&record->ip->rec_tree))
de996e86 449 hammer_test_inode(record->ip);
de996e86
MD
450 if (ip->rsv_recs == hammer_limit_inode_recs - 1)
451 wakeup(&ip->rsv_recs);
452 }
1b0ab2c3
MD
453
454 /*
bf3b416b
MD
455 * Do this test after removing record from the B-Tree.
456 */
457 if (target_ip) {
458 hammer_test_inode(target_ip);
459 hammer_rel_inode(target_ip, 0);
460 }
461
b3deaf57
MD
462 if (record->flags & HAMMER_RECF_ALLOCDATA) {
463 --hammer_count_record_datas;
bac808fe 464 kfree(record->data, hmp->m_misc);
b3deaf57
MD
465 record->flags &= ~HAMMER_RECF_ALLOCDATA;
466 }
1b0ab2c3
MD
467
468 /*
3214ade6
MD
469 * Release the reservation.
470 *
471 * If the record was not committed we can theoretically
472 * undo the reservation. However, doing so might
473 * create weird edge cases with the ordering of
474 * direct writes because the related buffer cache
475 * elements are per-vnode. So we don't try.
1b0ab2c3
MD
476 */
477 if ((resv = record->resv) != NULL) {
3214ade6 478 /* XXX undo leaf.data_offset,leaf.data_len */
1b0ab2c3 479 hammer_blockmap_reserve_complete(hmp, resv);
0832c9bb
MD
480 record->resv = NULL;
481 }
b3deaf57
MD
482 record->data = NULL;
483 --hammer_count_records;
bac808fe 484 kfree(record, hmp->m_misc);
a89aec1b 485 }
a89aec1b 486 }
a89aec1b
MD
487}
488
489/*
a5fddc16 490 * Record visibility depends on whether the record is being accessed by
3214ade6
MD
491 * the backend or the frontend. Backend tests ignore the frontend delete
492 * flag. Frontend tests do NOT ignore the backend delete/commit flags and
493 * must also check for commit races.
a5fddc16
MD
494 *
495 * Return non-zero if the record is visible, zero if it isn't or if it is
3214ade6
MD
496 * deleted. Returns 0 if the record has been comitted (unless the special
497 * delete-visibility flag is set). A committed record must be located
498 * via the media B-Tree. Returns non-zero if the record is good.
5e435c92
MD
499 *
500 * If HAMMER_CURSOR_DELETE_VISIBILITY is set we allow deleted memory
501 * records to be returned. This is so pending deletions are detected
502 * when using an iterator to locate an unused hash key, or when we need
503 * to locate historical records on-disk to destroy.
b84de5af
MD
504 */
505static __inline
506int
a5fddc16 507hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
b84de5af 508{
5e435c92
MD
509 if (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY)
510 return(1);
b84de5af 511 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
3214ade6
MD
512 if (record->flags & (HAMMER_RECF_DELETED_BE |
513 HAMMER_RECF_COMMITTED)) {
a5fddc16 514 return(0);
3214ade6 515 }
b84de5af 516 } else {
3214ade6
MD
517 if (record->flags & (HAMMER_RECF_DELETED_FE |
518 HAMMER_RECF_DELETED_BE |
519 HAMMER_RECF_COMMITTED)) {
b84de5af 520 return(0);
3214ade6 521 }
b84de5af
MD
522 }
523 return(1);
524}
525
526/*
2ae23e2e
MD
527 * This callback is used as part of the RB_SCAN function for in-memory
528 * records. We terminate it (return -1) as soon as we get a match.
6b4f890b 529 *
b84de5af
MD
530 * This routine is used by frontend code.
531 *
2ae23e2e
MD
532 * The primary compare code does not account for ASOF lookups. This
533 * code handles that case as well as a few others.
6b4f890b
MD
534 */
535static
536int
537hammer_rec_scan_callback(hammer_record_t rec, void *data)
538{
539 hammer_cursor_t cursor = data;
540
7f7c1f84 541 /*
b33e2cc0
MD
542 * We terminate on success, so this should be NULL on entry.
543 */
544 KKASSERT(cursor->iprec == NULL);
545
546 /*
3214ade6 547 * Skip if the record was marked deleted or committed.
b33e2cc0 548 */
b84de5af 549 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
b33e2cc0
MD
550 return(0);
551
552 /*
7f7c1f84
MD
553 * Skip if not visible due to our as-of TID
554 */
d5530d22 555 if (cursor->flags & HAMMER_CURSOR_ASOF) {
11ad5ade 556 if (cursor->asof < rec->leaf.base.create_tid)
7f7c1f84 557 return(0);
11ad5ade
MD
558 if (rec->leaf.base.delete_tid &&
559 cursor->asof >= rec->leaf.base.delete_tid) {
7f7c1f84
MD
560 return(0);
561 }
562 }
563
564 /*
3f43fb33
MD
565 * ref the record. The record is protected from backend B-Tree
566 * interactions by virtue of the cursor's IP lock.
7f7c1f84 567 */
b33e2cc0 568 hammer_ref(&rec->lock);
b84de5af 569
b33e2cc0 570 /*
3214ade6
MD
571 * The record may have been deleted or committed while we
572 * were blocked. XXX remove?
b33e2cc0 573 */
b84de5af
MD
574 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
575 hammer_rel_mem_record(rec);
b33e2cc0
MD
576 return(0);
577 }
578
579 /*
580 * Set the matching record and stop the scan.
581 */
582 cursor->iprec = rec;
583 return(-1);
6b4f890b
MD
584}
585
2ae23e2e
MD
586
587/*
588 * Lookup an in-memory record given the key specified in the cursor. Works
589 * just like hammer_btree_lookup() but operates on an inode's in-memory
590 * record list.
591 *
592 * The lookup must fail if the record is marked for deferred deletion.
3214ade6
MD
593 *
594 * The API for mem/btree_lookup() does not mess with the ATE/EOF bits.
2ae23e2e
MD
595 */
596static
597int
45a014dc 598hammer_mem_lookup(hammer_cursor_t cursor)
2ae23e2e 599{
45a014dc 600 KKASSERT(cursor->ip);
2ae23e2e
MD
601 if (cursor->iprec) {
602 hammer_rel_mem_record(cursor->iprec);
603 cursor->iprec = NULL;
604 }
45a014dc 605 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
2ae23e2e
MD
606 hammer_rec_scan_callback, cursor);
607
3214ade6 608 return (cursor->iprec ? 0 : ENOENT);
2ae23e2e
MD
609}
610
611/*
612 * hammer_mem_first() - locate the first in-memory record matching the
613 * cursor within the bounds of the key range.
3214ade6
MD
614 *
615 * WARNING! API is slightly different from btree_first(). hammer_mem_first()
616 * will set ATEMEM the same as MEMEOF, and does not return any error.
2ae23e2e 617 */
6b4f890b 618static
3214ade6 619void
4e17f465 620hammer_mem_first(hammer_cursor_t cursor)
6b4f890b 621{
4e17f465
MD
622 hammer_inode_t ip;
623
624 ip = cursor->ip;
625 KKASSERT(ip != NULL);
626
b3deaf57
MD
627 if (cursor->iprec) {
628 hammer_rel_mem_record(cursor->iprec);
629 cursor->iprec = NULL;
630 }
6b4f890b
MD
631 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
632 hammer_rec_scan_callback, cursor);
7f7c1f84 633
4e17f465 634 if (cursor->iprec)
3214ade6
MD
635 cursor->flags &= ~(HAMMER_CURSOR_MEMEOF | HAMMER_CURSOR_ATEMEM);
636 else
637 cursor->flags |= HAMMER_CURSOR_MEMEOF | HAMMER_CURSOR_ATEMEM;
6b4f890b
MD
638}
639
a89aec1b
MD
640/************************************************************************
641 * HAMMER IN-MEMORY RECORD FUNCTIONS *
642 ************************************************************************
643 *
644 * These functions manipulate in-memory records. Such records typically
645 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
646 */
647
648/*
8cd0a023
MD
649 * Add a directory entry (dip,ncp) which references inode (ip).
650 *
651 * Note that the low 32 bits of the namekey are set temporarily to create
652 * a unique in-memory record, and may be modified a second time when the
653 * record is synchronized to disk. In particular, the low 32 bits cannot be
654 * all 0's when synching to disk, which is not handled here.
5a930e66
MD
655 *
656 * NOTE: bytes does not include any terminating \0 on name, and name might
657 * not be terminated.
8cd0a023 658 */
66325755 659int
a89aec1b 660hammer_ip_add_directory(struct hammer_transaction *trans,
5a930e66 661 struct hammer_inode *dip, const char *name, int bytes,
66325755
MD
662 struct hammer_inode *ip)
663{
c82af904 664 struct hammer_cursor cursor;
a89aec1b 665 hammer_record_t record;
8cd0a023 666 int error;
5e435c92 667 u_int32_t max_iterations;
8cd0a023 668
11ad5ade 669 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
8cd0a023 670
1f07f686 671 record->type = HAMMER_MEM_RECORD_ADD;
5a930e66 672 record->leaf.base.localization = dip->obj_localization +
beec5dc4 673 hammer_dir_localization(dip);
11ad5ade 674 record->leaf.base.obj_id = dip->obj_id;
5e435c92
MD
675 record->leaf.base.key = hammer_directory_namekey(dip, name, bytes,
676 &max_iterations);
11ad5ade
MD
677 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
678 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
679 record->data->entry.obj_id = ip->obj_id;
43c665ae 680 record->data->entry.localization = ip->obj_localization;
5a930e66 681 bcopy(name, record->data->entry.name, bytes);
11ad5ade
MD
682
683 ++ip->ino_data.nlinks;
cc0758d0 684 ip->ino_data.ctime = trans->time;
e98f1b96 685 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
ec4e8497
MD
686
687 /*
c82af904 688 * Find an unused namekey. Both the in-memory record tree and
5e435c92
MD
689 * the B-Tree are checked. We do not want historically deleted
690 * names to create a collision as our iteration space may be limited,
691 * and since create_tid wouldn't match anyway an ASOF search
692 * must be used to locate collisions.
06ad81ff
MD
693 *
694 * delete-visibility is set so pending deletions do not give us
695 * a false-negative on our ability to use an iterator.
5e435c92
MD
696 *
697 * The iterator must not rollover the key. Directory keys only
698 * use the positive key space.
c82af904
MD
699 */
700 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
701 cursor.key_beg = record->leaf.base;
702 cursor.flags |= HAMMER_CURSOR_ASOF;
06ad81ff 703 cursor.flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
c82af904
MD
704 cursor.asof = ip->obj_asof;
705
c82af904 706 while (hammer_ip_lookup(&cursor) == 0) {
5e435c92
MD
707 ++record->leaf.base.key;
708 KKASSERT(record->leaf.base.key > 0);
c82af904 709 cursor.key_beg.key = record->leaf.base.key;
5e435c92 710 if (--max_iterations == 0) {
c82af904
MD
711 hammer_rel_mem_record(record);
712 error = ENOSPC;
713 goto failed;
714 }
715 }
716
717 /*
1f07f686
MD
718 * The target inode and the directory entry are bound together.
719 */
720 record->target_ip = ip;
721 record->flush_state = HAMMER_FST_SETUP;
722 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
723
724 /*
725 * The inode now has a dependancy and must be taken out of the idle
726 * state. An inode not in an idle state is given an extra reference.
f153644d
MD
727 *
728 * When transitioning to a SETUP state flag for an automatic reflush
729 * when the dependancies are disposed of if someone is waiting on
730 * the inode.
ec4e8497 731 */
1f07f686
MD
732 if (ip->flush_state == HAMMER_FST_IDLE) {
733 hammer_ref(&ip->lock);
734 ip->flush_state = HAMMER_FST_SETUP;
f153644d
MD
735 if (ip->flags & HAMMER_INODE_FLUSHW)
736 ip->flags |= HAMMER_INODE_REFLUSH;
1f07f686 737 }
47637bff 738 error = hammer_mem_add(record);
f437a2ab
MD
739 if (error == 0) {
740 dip->ino_data.mtime = trans->time;
e98f1b96 741 hammer_modify_inode(trans, dip, HAMMER_INODE_MTIME);
f437a2ab 742 }
c82af904
MD
743failed:
744 hammer_done_cursor(&cursor);
8cd0a023
MD
745 return(error);
746}
747
748/*
a89aec1b
MD
749 * Delete the directory entry and update the inode link count. The
750 * cursor must be seeked to the directory entry record being deleted.
751 *
b84de5af 752 * The related inode should be share-locked by the caller. The caller is
4c286c36
MD
753 * on the frontend. It could also be NULL indicating that the directory
754 * entry being removed has no related inode.
6a37e7e4
MD
755 *
756 * This function can return EDEADLK requiring the caller to terminate
b84de5af 757 * the cursor, any locks, wait on the returned record, and retry.
8cd0a023 758 */
a89aec1b
MD
759int
760hammer_ip_del_directory(struct hammer_transaction *trans,
761 hammer_cursor_t cursor, struct hammer_inode *dip,
762 struct hammer_inode *ip)
8cd0a023 763{
b84de5af 764 hammer_record_t record;
a89aec1b 765 int error;
8cd0a023 766
47637bff 767 if (hammer_cursor_inmem(cursor)) {
b84de5af
MD
768 /*
769 * In-memory (unsynchronized) records can simply be freed.
3214ade6 770 *
b84de5af
MD
771 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
772 * by the backend, we must still avoid races against the
3214ade6 773 * backend potentially syncing the record to the media.
b84de5af
MD
774 *
775 * We cannot call hammer_ip_delete_record(), that routine may
776 * only be called from the backend.
777 */
778 record = cursor->iprec;
3214ade6
MD
779 if (record->flags & (HAMMER_RECF_INTERLOCK_BE |
780 HAMMER_RECF_DELETED_BE |
781 HAMMER_RECF_COMMITTED)) {
b84de5af
MD
782 KKASSERT(cursor->deadlk_rec == NULL);
783 hammer_ref(&record->lock);
784 cursor->deadlk_rec = record;
785 error = EDEADLK;
786 } else {
1f07f686 787 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
d36ec43b 788 record->flags |= HAMMER_RECF_DELETED_FE;
b84de5af
MD
789 error = 0;
790 }
791 } else {
792 /*
793 * If the record is on-disk we have to queue the deletion by
794 * the record's key. This also causes lookups to skip the
c9ce54d6
MD
795 * record (lookups for the purposes of finding an unused
796 * directory key do not skip the record).
b84de5af 797 */
98f7132d
MD
798 KKASSERT(dip->flags &
799 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
11ad5ade 800 record = hammer_alloc_mem_record(dip, 0);
1f07f686 801 record->type = HAMMER_MEM_RECORD_DEL;
11ad5ade 802 record->leaf.base = cursor->leaf->base;
c9ce54d6 803 KKASSERT(dip->obj_id == record->leaf.base.obj_id);
1f07f686 804
4c286c36
MD
805 /*
806 * ip may be NULL, indicating the deletion of a directory
807 * entry which has no related inode.
808 */
1f07f686 809 record->target_ip = ip;
4c286c36
MD
810 if (ip) {
811 record->flush_state = HAMMER_FST_SETUP;
812 TAILQ_INSERT_TAIL(&ip->target_list, record,
813 target_entry);
814 } else {
815 record->flush_state = HAMMER_FST_IDLE;
816 }
b84de5af 817
ec4e8497 818 /*
1f07f686
MD
819 * The inode now has a dependancy and must be taken out of
820 * the idle state. An inode not in an idle state is given
821 * an extra reference.
f153644d
MD
822 *
823 * When transitioning to a SETUP state flag for an automatic
824 * reflush when the dependancies are disposed of if someone
825 * is waiting on the inode.
ec4e8497 826 */
4c286c36 827 if (ip && ip->flush_state == HAMMER_FST_IDLE) {
1f07f686
MD
828 hammer_ref(&ip->lock);
829 ip->flush_state = HAMMER_FST_SETUP;
f153644d
MD
830 if (ip->flags & HAMMER_INODE_FLUSHW)
831 ip->flags |= HAMMER_INODE_REFLUSH;
1f07f686 832 }
ec4e8497 833
47637bff 834 error = hammer_mem_add(record);
b84de5af 835 }
a89aec1b
MD
836
837 /*
c0ade690 838 * One less link. The file may still be open in the OS even after
3bf2d80a 839 * all links have gone away.
6a37e7e4
MD
840 *
841 * We have to terminate the cursor before syncing the inode to
3bf2d80a
MD
842 * avoid deadlocking against ourselves. XXX this may no longer
843 * be true.
855942b6 844 *
3bf2d80a
MD
845 * If nlinks drops to zero and the vnode is inactive (or there is
846 * no vnode), call hammer_inode_unloadable_check() to zonk the
847 * inode. If we don't do this here the inode will not be destroyed
848 * on-media until we unmount.
a89aec1b
MD
849 */
850 if (error == 0) {
cc0758d0 851 if (ip) {
4c286c36 852 --ip->ino_data.nlinks; /* do before we might block */
cc0758d0
MD
853 ip->ino_data.ctime = trans->time;
854 }
35a49944 855 dip->ino_data.mtime = trans->time;
e98f1b96 856 hammer_modify_inode(trans, dip, HAMMER_INODE_MTIME);
4c286c36 857 if (ip) {
e98f1b96 858 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
4c286c36
MD
859 if (ip->ino_data.nlinks == 0 &&
860 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
861 hammer_done_cursor(cursor);
862 hammer_inode_unloadable_check(ip, 1);
863 hammer_flush_inode(ip, 0);
864 }
d113fda1 865 }
76376933 866
a89aec1b
MD
867 }
868 return(error);
66325755
MD
869}
870
8cd0a023 871/*
7a04d74f
MD
872 * Add a record to an inode.
873 *
874 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
875 * initialize the following additional fields:
876 *
b84de5af
MD
877 * The related inode should be share-locked by the caller. The caller is
878 * on the frontend.
879 *
7a04d74f
MD
880 * record->rec.entry.base.base.key
881 * record->rec.entry.base.base.rec_type
882 * record->rec.entry.base.base.data_len
47197d71 883 * record->data (a copy will be kmalloc'd if it cannot be embedded)
7a04d74f
MD
884 */
885int
886hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
887{
888 hammer_inode_t ip = record->ip;
889 int error;
7a04d74f 890
2f85fa4d 891 KKASSERT(record->leaf.base.localization != 0);
11ad5ade
MD
892 record->leaf.base.obj_id = ip->obj_id;
893 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
47637bff 894 error = hammer_mem_add(record);
7a04d74f
MD
895 return(error);
896}
897
898/*
6362a262
MD
899 * Locate a pre-existing bulk record in memory. The caller wishes to
900 * replace the record with a new one. The existing record may have a
901 * different length (and thus a different key) so we have to use an
902 * overlap check function.
47637bff
MD
903 */
904static hammer_record_t
6362a262 905hammer_ip_get_bulk(hammer_record_t record)
47637bff 906{
d7e278bb 907 struct hammer_bulk_info info;
6362a262 908 hammer_inode_t ip = record->ip;
d7e278bb 909
6362a262
MD
910 info.record = record;
911 info.conflict = NULL;
d7e278bb
MD
912 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_overlap_cmp,
913 hammer_bulk_scan_callback, &info);
914
6362a262 915 return(info.conflict); /* may be NULL */
d7e278bb
MD
916}
917
918/*
919 * Take records vetted by overlap_cmp. The first non-deleted record
920 * (if any) stops the scan.
921 */
922static int
923hammer_bulk_scan_callback(hammer_record_t record, void *data)
924{
925 struct hammer_bulk_info *info = data;
0832c9bb 926
3214ade6
MD
927 if (record->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
928 HAMMER_RECF_COMMITTED)) {
d7e278bb 929 return(0);
3214ade6 930 }
d7e278bb 931 hammer_ref(&record->lock);
6362a262 932 info->conflict = record;
d7e278bb 933 return(-1); /* stop scan */
47637bff
MD
934}
935
936/*
937 * Reserve blockmap space placemarked with an in-memory record.
938 *
bcac4bbb
MD
939 * This routine is called by the frontend in order to be able to directly
940 * flush a buffer cache buffer. The frontend has locked the related buffer
941 * cache buffers and we should be able to manipulate any overlapping
942 * in-memory records.
e469566b 943 *
6362a262
MD
944 * The caller is responsible for adding the returned record and deleting
945 * the returned conflicting record (if any), typically by calling
946 * hammer_ip_replace_bulk() (via hammer_io_direct_write()).
47637bff
MD
947 */
948hammer_record_t
0832c9bb
MD
949hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
950 int *errorp)
47637bff
MD
951{
952 hammer_record_t record;
507df98a
ID
953 hammer_dedup_cache_t dcp;
954 hammer_crc_t crc;
0832c9bb 955 int zone;
47637bff
MD
956
957 /*
6362a262
MD
958 * Create a record to cover the direct write. The record cannot
959 * be added to the in-memory RB tree here as it might conflict
960 * with an existing memory record. See hammer_io_direct_write().
0832c9bb
MD
961 *
962 * The backend is responsible for finalizing the space reserved in
963 * this record.
964 *
965 * XXX bytes not aligned, depend on the reservation code to
966 * align the reservation.
47637bff
MD
967 */
968 record = hammer_alloc_mem_record(ip, 0);
0832c9bb
MD
969 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
970 HAMMER_ZONE_SMALL_DATA_INDEX;
507df98a
ID
971 if (bytes == 0)
972 crc = 0;
973 else
974 crc = crc32(data, bytes);
975
976 if (hammer_live_dedup == 0)
977 goto nodedup;
978 if ((dcp = hammer_dedup_cache_lookup(ip->hmp, crc)) != NULL) {
979 struct hammer_dedup_cache tmp = *dcp;
980
981 record->resv = hammer_blockmap_reserve_dedup(ip->hmp, zone,
982 bytes, tmp.data_offset, errorp);
983 if (record->resv == NULL)
984 goto nodedup;
985
986 if (!hammer_dedup_validate(&tmp, zone, bytes, data)) {
987 hammer_blockmap_reserve_complete(ip->hmp, record->resv);
988 goto nodedup;
989 }
990
991 record->leaf.data_offset = tmp.data_offset;
992 record->flags |= HAMMER_RECF_DEDUPED;
993 } else {
994nodedup:
995 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
996 &record->leaf.data_offset, errorp);
997 if (record->resv == NULL) {
998 kprintf("hammer_ip_add_bulk: reservation failed\n");
999 hammer_rel_mem_record(record);
1000 return(NULL);
1001 }
47637bff 1002 }
507df98a 1003
47637bff
MD
1004 record->type = HAMMER_MEM_RECORD_DATA;
1005 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
1006 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
1007 record->leaf.base.obj_id = ip->obj_id;
1008 record->leaf.base.key = file_offset + bytes;
5a930e66
MD
1009 record->leaf.base.localization = ip->obj_localization +
1010 HAMMER_LOCALIZE_MISC;
47637bff 1011 record->leaf.data_len = bytes;
507df98a 1012 record->leaf.data_crc = crc;
cebe9493 1013 KKASSERT(*errorp == 0);
6362a262 1014
e469566b 1015 return(record);
47637bff
MD
1016}
1017
1018/*
6362a262
MD
1019 * Called by hammer_io_direct_write() prior to any possible completion
1020 * of the BIO to emplace the memory record associated with the I/O and
1021 * to replace any prior memory record which might still be active.
1022 *
1023 * Setting the FE deleted flag on the old record (if any) avoids any RB
1024 * tree insertion conflict, amoung other things.
1025 *
1026 * This has to be done prior to the caller completing any related buffer
1027 * cache I/O or a reinstantiation of the buffer may load data from the
1028 * old media location instead of the new media location. The holding
1029 * of the locked buffer cache buffer serves to interlock the record
1030 * replacement operation.
1031 */
1032void
1033hammer_ip_replace_bulk(hammer_mount_t hmp, hammer_record_t record)
1034{
1035 hammer_record_t conflict;
1036 int error;
1037
1038 while ((conflict = hammer_ip_get_bulk(record)) != NULL) {
1039 if ((conflict->flags & HAMMER_RECF_INTERLOCK_BE) == 0) {
1040 conflict->flags |= HAMMER_RECF_DELETED_FE;
1041 break;
1042 }
1043 conflict->flags |= HAMMER_RECF_WANTED;
1044 tsleep(conflict, 0, "hmrrc3", 0);
1045 hammer_rel_mem_record(conflict);
1046 }
1047 error = hammer_mem_add(record);
1048 if (conflict)
1049 hammer_rel_mem_record(conflict);
1050 KKASSERT(error == 0);
1051}
1052
1053/*
47637bff
MD
1054 * Frontend truncation code. Scan in-memory records only. On-disk records
1055 * and records in a flushing state are handled by the backend. The vnops
1056 * setattr code will handle the block containing the truncation point.
1057 *
1058 * Partial blocks are not deleted.
6362a262
MD
1059 *
1060 * This code is only called on regular files.
47637bff 1061 */
47637bff
MD
1062int
1063hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
1064{
1065 struct rec_trunc_info info;
1066
1067 switch(ip->ino_data.obj_type) {
1068 case HAMMER_OBJTYPE_REGFILE:
1069 info.rec_type = HAMMER_RECTYPE_DATA;
1070 break;
1071 case HAMMER_OBJTYPE_DBFILE:
1072 info.rec_type = HAMMER_RECTYPE_DB;
1073 break;
1074 default:
1075 return(EINVAL);
1076 }
1077 info.trunc_off = file_size;
1078 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
312de84d 1079 hammer_frontend_trunc_callback, &info);
47637bff
MD
1080 return(0);
1081}
1082
6362a262
MD
1083/*
1084 * Scan callback for frontend records to destroy during a truncation.
1085 * We must ensure that DELETED_FE is set on the record or the frontend
1086 * will get confused in future read() calls.
1087 *
1088 * NOTE: DELETED_FE cannot be set while the record interlock (BE) is held.
1089 * In this rare case we must wait for the interlock to be cleared.
1090 *
1091 * NOTE: This function is only called on regular files. There are further
1092 * restrictions to the setting of DELETED_FE on directory records
1093 * undergoing a flush due to sensitive inode link count calculations.
1094 */
0832c9bb 1095static int
312de84d 1096hammer_frontend_trunc_callback(hammer_record_t record, void *data __unused)
0832c9bb
MD
1097{
1098 if (record->flags & HAMMER_RECF_DELETED_FE)
1099 return(0);
6362a262 1100#if 0
0832c9bb
MD
1101 if (record->flush_state == HAMMER_FST_FLUSH)
1102 return(0);
6362a262 1103#endif
0832c9bb 1104 hammer_ref(&record->lock);
6362a262
MD
1105 while (record->flags & HAMMER_RECF_INTERLOCK_BE)
1106 hammer_wait_mem_record_ident(record, "hmmtrr");
0832c9bb
MD
1107 record->flags |= HAMMER_RECF_DELETED_FE;
1108 hammer_rel_mem_record(record);
1109 return(0);
1110}
1111
47637bff 1112/*
cb51be26
MD
1113 * Return 1 if the caller must check for and delete existing records
1114 * before writing out a new data record.
1115 *
1116 * Return 0 if the caller can just insert the record into the B-Tree without
1117 * checking.
1118 */
1119static int
1120hammer_record_needs_overwrite_delete(hammer_record_t record)
1121{
1122 hammer_inode_t ip = record->ip;
1123 int64_t file_offset;
1124 int r;
1125
1126 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
1127 file_offset = record->leaf.base.key;
1128 else
1129 file_offset = record->leaf.base.key - record->leaf.data_len;
a9d52b76 1130 r = (file_offset < ip->save_trunc_off);
cb51be26 1131 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
a9d52b76
MD
1132 if (ip->save_trunc_off <= record->leaf.base.key)
1133 ip->save_trunc_off = record->leaf.base.key + 1;
cb51be26 1134 } else {
a9d52b76
MD
1135 if (ip->save_trunc_off < record->leaf.base.key)
1136 ip->save_trunc_off = record->leaf.base.key;
cb51be26
MD
1137 }
1138 return(r);
1139}
1140
1141/*
47637bff
MD
1142 * Backend code. Sync a record to the media.
1143 */
4e17f465
MD
1144int
1145hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
1146{
1147 hammer_transaction_t trans = cursor->trans;
cebe9493 1148 int64_t file_offset;
4a2796f3 1149 int bytes;
40043e7f 1150 void *bdata;
c0ade690 1151 int error;
602c6cb8 1152 int doprop;
c0ade690 1153
1f07f686 1154 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
d36ec43b 1155 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
2f85fa4d 1156 KKASSERT(record->leaf.base.localization != 0);
b33e2cc0 1157
47637bff 1158 /*
e469566b
MD
1159 * Any direct-write related to the record must complete before we
1160 * can sync the record to the on-disk media.
1161 */
77912481 1162 if (record->gflags & (HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL))
e469566b
MD
1163 hammer_io_direct_wait(record);
1164
1165 /*
47637bff
MD
1166 * If this is a bulk-data record placemarker there may be an existing
1167 * record on-disk, indicating a data overwrite. If there is the
1168 * on-disk record must be deleted before we can insert our new record.
1169 *
1170 * We've synthesized this record and do not know what the create_tid
1171 * on-disk is, nor how much data it represents.
1172 *
1173 * Keep in mind that (key) for data records is (base_offset + len),
1174 * not (base_offset). Also, we only want to get rid of on-disk
1175 * records since we are trying to sync our in-memory record, call
1176 * hammer_ip_delete_range() with truncating set to 1 to make sure
1177 * it skips in-memory records.
1178 *
1179 * It is ok for the lookup to return ENOENT.
cb51be26
MD
1180 *
1181 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1182 * to call hammer_ip_delete_range() or not. This also means we must
1183 * update sync_trunc_off() as we write.
47637bff 1184 */
cb51be26
MD
1185 if (record->type == HAMMER_MEM_RECORD_DATA &&
1186 hammer_record_needs_overwrite_delete(record)) {
cebe9493 1187 file_offset = record->leaf.base.key - record->leaf.data_len;
4a2796f3
MD
1188 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1189 ~HAMMER_BUFMASK;
cebe9493 1190 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
47637bff
MD
1191 error = hammer_ip_delete_range(
1192 cursor, record->ip,
4a2796f3 1193 file_offset, file_offset + bytes - 1,
cebe9493 1194 1);
47637bff
MD
1195 if (error && error != ENOENT)
1196 goto done;
1197 }
1198
1199 /*
5fa5c92f
MD
1200 * If this is a general record there may be an on-disk version
1201 * that must be deleted before we can insert the new record.
1202 */
1203 if (record->type == HAMMER_MEM_RECORD_GENERAL) {
1204 error = hammer_delete_general(cursor, record->ip,
1205 &record->leaf);
1206 if (error && error != ENOENT)
1207 goto done;
1208 }
1209
1210 /*
47637bff
MD
1211 * Setup the cursor.
1212 */
4e17f465 1213 hammer_normalize_cursor(cursor);
11ad5ade 1214 cursor->key_beg = record->leaf.base;
4e17f465
MD
1215 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1216 cursor->flags |= HAMMER_CURSOR_BACKEND;
1217 cursor->flags &= ~HAMMER_CURSOR_INSERT;
c0ade690
MD
1218
1219 /*
98f7132d
MD
1220 * Records can wind up on-media before the inode itself is on-media.
1221 * Flag the case.
1222 */
1223 record->ip->flags |= HAMMER_INODE_DONDISK;
1224
1225 /*
47637bff
MD
1226 * If we are deleting a directory entry an exact match must be
1227 * found on-disk.
b84de5af 1228 */
1f07f686 1229 if (record->type == HAMMER_MEM_RECORD_DEL) {
4e17f465
MD
1230 error = hammer_btree_lookup(cursor);
1231 if (error == 0) {
5c8d05e2 1232 KKASSERT(cursor->iprec == NULL);
e63644f0
MD
1233 error = hammer_ip_delete_record(cursor, record->ip,
1234 trans->tid);
4e17f465 1235 if (error == 0) {
3214ade6
MD
1236 record->flags |= HAMMER_RECF_DELETED_BE |
1237 HAMMER_RECF_COMMITTED;
1238 ++record->ip->rec_generation;
4e17f465
MD
1239 }
1240 }
b84de5af
MD
1241 goto done;
1242 }
1243
1244 /*
1245 * We are inserting.
1246 *
df2ccbac
MD
1247 * Issue a lookup to position the cursor and locate the insertion
1248 * point. The target key should not exist. If we are creating a
1249 * directory entry we may have to iterate the low 32 bits of the
1250 * key to find an unused key.
c0ade690 1251 */
98da6d8c 1252 hammer_sync_lock_sh(trans);
4e17f465 1253 cursor->flags |= HAMMER_CURSOR_INSERT;
c82af904
MD
1254 error = hammer_btree_lookup(cursor);
1255 if (hammer_debug_inode)
1256 kprintf("DOINSERT LOOKUP %d\n", error);
1257 if (error == 0) {
1258 kprintf("hammer_ip_sync_record: duplicate rec "
973c11b9 1259 "at (%016llx)\n", (long long)record->leaf.base.key);
fc73edd8
MD
1260 if (hammer_debug_critical)
1261 Debugger("duplicate record1");
c82af904 1262 error = EIO;
c0ade690 1263 }
47637bff
MD
1264#if 0
1265 if (record->type == HAMMER_MEM_RECORD_DATA)
1266 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1267 record->leaf.base.key - record->leaf.data_len,
1268 record->leaf.data_offset, error);
1269#endif
47637bff 1270
c0ade690 1271 if (error != ENOENT)
98da6d8c 1272 goto done_unlock;
c0ade690
MD
1273
1274 /*
47197d71
MD
1275 * Allocate the record and data. The result buffers will be
1276 * marked as being modified and further calls to
1277 * hammer_modify_buffer() will result in unneeded UNDO records.
1278 *
40043e7f 1279 * Support zero-fill records (data == NULL and data_len != 0)
c0ade690 1280 */
47637bff
MD
1281 if (record->type == HAMMER_MEM_RECORD_DATA) {
1282 /*
1283 * The data portion of a bulk-data record has already been
1284 * committed to disk, we need only adjust the layer2
1285 * statistics in the same transaction as our B-Tree insert.
1286 */
1287 KKASSERT(record->leaf.data_offset != 0);
cdb6e4e6 1288 error = hammer_blockmap_finalize(trans,
5e435c92 1289 record->resv,
cdb6e4e6
MD
1290 record->leaf.data_offset,
1291 record->leaf.data_len);
507df98a
ID
1292
1293 if (hammer_live_dedup == 2 &&
1294 (record->flags & HAMMER_RECF_DEDUPED) == 0) {
1295 hammer_dedup_cache_add(record->ip, &record->leaf);
1296 }
47637bff
MD
1297 } else if (record->data && record->leaf.data_len) {
1298 /*
1299 * Wholely cached record, with data. Allocate the data.
1300 */
11ad5ade 1301 bdata = hammer_alloc_data(trans, record->leaf.data_len,
bf3b416b 1302 record->leaf.base.rec_type,
11ad5ade 1303 &record->leaf.data_offset,
df2ccbac
MD
1304 &cursor->data_buffer,
1305 0, &error);
11ad5ade 1306 if (bdata == NULL)
98da6d8c 1307 goto done_unlock;
ddfdf542 1308 hammer_crc_set_leaf(record->data, &record->leaf);
4e17f465 1309 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
11ad5ade 1310 bcopy(record->data, bdata, record->leaf.data_len);
4e17f465 1311 hammer_modify_buffer_done(cursor->data_buffer);
47197d71 1312 } else {
47637bff
MD
1313 /*
1314 * Wholely cached record, without data.
1315 */
11ad5ade
MD
1316 record->leaf.data_offset = 0;
1317 record->leaf.data_crc = 0;
c0ade690 1318 }
c0ade690 1319
602c6cb8 1320 error = hammer_btree_insert(cursor, &record->leaf, &doprop);
973c11b9
MD
1321 if (hammer_debug_inode && error) {
1322 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n",
1323 error,
1324 (long long)cursor->node->node_offset,
1325 cursor->index,
1326 (long long)record->leaf.base.key);
1327 }
b3deaf57
MD
1328
1329 /*
3214ade6
MD
1330 * Our record is on-disk and we normally mark the in-memory version
1331 * as having been committed (and not BE-deleted).
1332 *
1333 * If the record represented a directory deletion but we had to
1334 * sync a valid directory entry to disk due to dependancies,
1335 * we must convert the record to a covering delete so the
1336 * frontend does not have visibility on the synced entry.
c9ce54d6
MD
1337 *
1338 * WARNING: cursor's leaf pointer may have changed after do_propagation
1339 * returns!
b3deaf57 1340 */
4e17f465 1341 if (error == 0) {
602c6cb8 1342 if (doprop) {
4c038e17
MD
1343 hammer_btree_do_propagation(cursor,
1344 record->ip->pfsm,
602c6cb8
MD
1345 &record->leaf);
1346 }
4e17f465 1347 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
3214ade6
MD
1348 /*
1349 * Must convert deleted directory entry add
1350 * to a directory entry delete.
1351 */
4e17f465
MD
1352 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1353 record->flags &= ~HAMMER_RECF_DELETED_FE;
1354 record->type = HAMMER_MEM_RECORD_DEL;
c9ce54d6 1355 KKASSERT(record->ip->obj_id == record->leaf.base.obj_id);
c4bae5fd 1356 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
4e17f465 1357 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
3214ade6
MD
1358 KKASSERT((record->flags & (HAMMER_RECF_COMMITTED |
1359 HAMMER_RECF_DELETED_BE)) == 0);
1360 /* converted record is not yet committed */
c4bae5fd 1361 /* hammer_flush_record_done takes care of the rest */
4e17f465 1362 } else {
3214ade6
MD
1363 /*
1364 * Everything went fine and we are now done with
1365 * this record.
1366 */
1367 record->flags |= HAMMER_RECF_COMMITTED;
1368 ++record->ip->rec_generation;
1f07f686 1369 }
4e17f465 1370 } else {
11ad5ade
MD
1371 if (record->leaf.data_offset) {
1372 hammer_blockmap_free(trans, record->leaf.data_offset,
1373 record->leaf.data_len);
19619882 1374 }
4e17f465 1375 }
98da6d8c
MD
1376done_unlock:
1377 hammer_sync_unlock(trans);
47197d71 1378done:
c0ade690 1379 return(error);
8cd0a023 1380}
66325755 1381
d26d0ae9 1382/*
a89aec1b
MD
1383 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1384 * entry's key is used to deal with hash collisions in the upper 32 bits.
1385 * A unique 64 bit key is generated in-memory and may be regenerated a
1386 * second time when the directory record is flushed to the on-disk B-Tree.
d26d0ae9 1387 *
b3deaf57
MD
1388 * A referenced record is passed to this function. This function
1389 * eats the reference. If an error occurs the record will be deleted.
47197d71
MD
1390 *
1391 * A copy of the temporary record->data pointer provided by the caller
1392 * will be made.
66325755
MD
1393 */
1394int
47637bff 1395hammer_mem_add(hammer_record_t record)
66325755 1396{
47637bff
MD
1397 hammer_mount_t hmp = record->ip->hmp;
1398
47197d71
MD
1399 /*
1400 * Make a private copy of record->data
1401 */
11ad5ade
MD
1402 if (record->data)
1403 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
47197d71
MD
1404
1405 /*
c82af904
MD
1406 * Insert into the RB tree. A unique key should have already
1407 * been selected if this is a directory entry.
47197d71 1408 */
c82af904
MD
1409 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1410 record->flags |= HAMMER_RECF_DELETED_FE;
1411 hammer_rel_mem_record(record);
1412 return (EEXIST);
8cd0a023 1413 }
4a2796f3 1414 ++hmp->count_newrecords;
47637bff
MD
1415 ++hmp->rsv_recs;
1416 ++record->ip->rsv_recs;
e63644f0 1417 record->ip->hmp->rsv_databytes += record->leaf.data_len;
8cd0a023 1418 record->flags |= HAMMER_RECF_ONRBTREE;
e98f1b96 1419 hammer_modify_inode(NULL, record->ip, HAMMER_INODE_XDIRTY);
b3deaf57 1420 hammer_rel_mem_record(record);
8cd0a023 1421 return(0);
66325755
MD
1422}
1423
a89aec1b
MD
1424/************************************************************************
1425 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1426 ************************************************************************
1427 *
1428 * These functions augment the B-Tree scanning functions in hammer_btree.c
1429 * by merging in-memory records with on-disk records.
1430 */
1431
1432/*
1433 * Locate a particular record either in-memory or on-disk.
1434 *
1435 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1436 * NOT be called to iterate results.
1437 */
1438int
45a014dc 1439hammer_ip_lookup(hammer_cursor_t cursor)
a89aec1b
MD
1440{
1441 int error;
1442
1443 /*
1444 * If the element is in-memory return it without searching the
1445 * on-disk B-Tree
1446 */
45a014dc
MD
1447 KKASSERT(cursor->ip);
1448 error = hammer_mem_lookup(cursor);
a89aec1b 1449 if (error == 0) {
11ad5ade 1450 cursor->leaf = &cursor->iprec->leaf;
a89aec1b
MD
1451 return(error);
1452 }
1453 if (error != ENOENT)
1454 return(error);
1455
1456 /*
1457 * If the inode has on-disk components search the on-disk B-Tree.
1458 */
45a014dc 1459 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
a89aec1b
MD
1460 return(error);
1461 error = hammer_btree_lookup(cursor);
1462 if (error == 0)
11ad5ade 1463 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
a89aec1b
MD
1464 return(error);
1465}
1466
1467/*
3214ade6
MD
1468 * Helper for hammer_ip_first()/hammer_ip_next()
1469 *
1470 * NOTE: Both ATEDISK and DISKEOF will be set the same. This sets up
1471 * hammer_ip_first() for calling hammer_ip_next(), and sets up the re-seek
1472 * state if hammer_ip_next() needs to re-seek.
1473 */
1474static __inline
1475int
1476_hammer_ip_seek_btree(hammer_cursor_t cursor)
1477{
1478 hammer_inode_t ip = cursor->ip;
1479 int error;
1480
1481 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
1482 error = hammer_btree_lookup(cursor);
1483 if (error == ENOENT || error == EDEADLK) {
973c11b9
MD
1484 if (hammer_debug_general & 0x2000) {
1485 kprintf("error %d node %p %016llx index %d\n",
1486 error, cursor->node,
1487 (long long)cursor->node->node_offset,
1488 cursor->index);
1489 }
3214ade6
MD
1490 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1491 error = hammer_btree_iterate(cursor);
1492 }
1493 if (error == 0) {
1494 cursor->flags &= ~(HAMMER_CURSOR_DISKEOF |
1495 HAMMER_CURSOR_ATEDISK);
1496 } else {
1497 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1498 HAMMER_CURSOR_ATEDISK;
1499 if (error == ENOENT)
1500 error = 0;
1501 }
1502 } else {
1503 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_ATEDISK;
1504 error = 0;
1505 }
1506 return(error);
1507}
1508
1509/*
1510 * Helper for hammer_ip_next()
1511 *
1512 * The caller has determined that the media cursor is further along than the
1513 * memory cursor and must be reseeked after a generation number change.
1514 */
1515static
1516int
1517_hammer_ip_reseek(hammer_cursor_t cursor)
1518{
1519 struct hammer_base_elm save;
1520 hammer_btree_elm_t elm;
1521 int error;
1522 int r;
1523 int again = 0;
1524
1525 /*
1526 * Do the re-seek.
1527 */
1528 kprintf("HAMMER: Debug: re-seeked during scan @ino=%016llx\n",
1529 (long long)cursor->ip->obj_id);
1530 save = cursor->key_beg;
1531 cursor->key_beg = cursor->iprec->leaf.base;
1532 error = _hammer_ip_seek_btree(cursor);
1533 KKASSERT(error == 0);
1534 cursor->key_beg = save;
1535
1536 /*
1537 * If the memory record was previous returned to
1538 * the caller and the media record matches
1539 * (-1/+1: only create_tid differs), then iterate
1540 * the media record to avoid a double result.
1541 */
1542 if ((cursor->flags & HAMMER_CURSOR_ATEDISK) == 0 &&
1543 (cursor->flags & HAMMER_CURSOR_LASTWASMEM)) {
1544 elm = &cursor->node->ondisk->elms[cursor->index];
1545 r = hammer_btree_cmp(&elm->base,
1546 &cursor->iprec->leaf.base);
1547 if (cursor->flags & HAMMER_CURSOR_ASOF) {
1548 if (r >= -1 && r <= 1) {
1549 kprintf("HAMMER: Debug: iterated after "
1550 "re-seek (asof r=%d)\n", r);
1551 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1552 again = 1;
1553 }
1554 } else {
1555 if (r == 0) {
1556 kprintf("HAMMER: Debug: iterated after "
1557 "re-seek\n");
1558 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1559 again = 1;
1560 }
1561 }
1562 }
1563 return(again);
1564}
1565
1566/*
a89aec1b
MD
1567 * Locate the first record within the cursor's key_beg/key_end range,
1568 * restricted to a particular inode. 0 is returned on success, ENOENT
1569 * if no records matched the requested range, or some other error.
1570 *
1571 * When 0 is returned hammer_ip_next() may be used to iterate additional
1572 * records within the requested range.
6a37e7e4
MD
1573 *
1574 * This function can return EDEADLK, requiring the caller to terminate
1575 * the cursor and try again.
a89aec1b 1576 */
3214ade6 1577
a89aec1b 1578int
4e17f465 1579hammer_ip_first(hammer_cursor_t cursor)
a89aec1b 1580{
f64b567c 1581 hammer_inode_t ip __debugvar = cursor->ip;
a89aec1b
MD
1582 int error;
1583
4e17f465
MD
1584 KKASSERT(ip != NULL);
1585
a89aec1b
MD
1586 /*
1587 * Clean up fields and setup for merged scan
1588 */
5c8d05e2 1589 cursor->flags &= ~HAMMER_CURSOR_RETEST;
a89aec1b
MD
1590
1591 /*
3214ade6
MD
1592 * Search the in-memory record list (Red-Black tree). Unlike the
1593 * B-Tree search, mem_first checks for records in the range.
6a37e7e4 1594 *
3214ade6
MD
1595 * This function will setup both ATEMEM and MEMEOF properly for
1596 * the ip iteration. ATEMEM will be set if MEMEOF is set.
a89aec1b 1597 */
3214ade6 1598 hammer_mem_first(cursor);
a89aec1b
MD
1599
1600 /*
3214ade6
MD
1601 * Detect generation changes during blockages, including
1602 * blockages which occur on the initial btree search.
a89aec1b 1603 */
3214ade6 1604 cursor->rec_generation = cursor->ip->rec_generation;
a89aec1b
MD
1605
1606 /*
3214ade6 1607 * Initial search and result
a89aec1b 1608 */
3214ade6
MD
1609 error = _hammer_ip_seek_btree(cursor);
1610 if (error == 0)
1611 error = hammer_ip_next(cursor);
1612
1613 return (error);
a89aec1b
MD
1614}
1615
1616/*
1617 * Retrieve the next record in a merged iteration within the bounds of the
1618 * cursor. This call may be made multiple times after the cursor has been
1619 * initially searched with hammer_ip_first().
1620 *
3214ade6
MD
1621 * There are numerous special cases in this code to deal with races between
1622 * in-memory records and on-media records.
1623 *
a89aec1b
MD
1624 * 0 is returned on success, ENOENT if no further records match the
1625 * requested range, or some other error code is returned.
1626 */
1627int
1628hammer_ip_next(hammer_cursor_t cursor)
1629{
1630 hammer_btree_elm_t elm;
3214ade6
MD
1631 hammer_record_t rec;
1632 hammer_record_t tmprec;
a89aec1b
MD
1633 int error;
1634 int r;
1635
3214ade6 1636again:
a89aec1b 1637 /*
a89aec1b 1638 * Get the next on-disk record
3214ade6
MD
1639 *
1640 * NOTE: If we deleted the last on-disk record we had scanned
1641 * ATEDISK will be clear and RETEST will be set, forcing
1642 * a call to iterate. The fact that ATEDISK is clear causes
1643 * iterate to re-test the 'current' element. If ATEDISK is
1644 * set, iterate will skip the 'current' element.
a89aec1b 1645 */
3214ade6
MD
1646 error = 0;
1647 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1648 if (cursor->flags & (HAMMER_CURSOR_ATEDISK |
1649 HAMMER_CURSOR_RETEST)) {
a89aec1b 1650 error = hammer_btree_iterate(cursor);
5c8d05e2 1651 cursor->flags &= ~HAMMER_CURSOR_RETEST;
cb51be26 1652 if (error == 0) {
a89aec1b 1653 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
bcac4bbb
MD
1654 hammer_cache_node(&cursor->ip->cache[1],
1655 cursor->node);
3214ade6 1656 } else if (error == ENOENT) {
195c19a1
MD
1657 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1658 HAMMER_CURSOR_ATEDISK;
3214ade6 1659 error = 0;
cb51be26 1660 }
a89aec1b
MD
1661 }
1662 }
1663
1664 /*
3214ade6
MD
1665 * If the generation changed the backend has deleted or committed
1666 * one or more memory records since our last check.
1667 *
1668 * When this case occurs if the disk cursor is > current memory record
1669 * or the disk cursor is at EOF, we must re-seek the disk-cursor.
1670 * Since the cursor is ahead it must have not yet been eaten (if
1671 * not at eof anyway). (XXX data offset case?)
1672 *
1673 * NOTE: we are not doing a full check here. That will be handled
1674 * later on.
1675 *
1676 * If we have exhausted all memory records we do not have to do any
1677 * further seeks.
1678 */
1679 while (cursor->rec_generation != cursor->ip->rec_generation &&
1680 error == 0
1681 ) {
1682 kprintf("HAMMER: Debug: generation changed during scan @ino=%016llx\n", (long long)cursor->ip->obj_id);
1683 cursor->rec_generation = cursor->ip->rec_generation;
1684 if (cursor->flags & HAMMER_CURSOR_MEMEOF)
1685 break;
1686 if (cursor->flags & HAMMER_CURSOR_DISKEOF) {
1687 r = 1;
1688 } else {
1689 KKASSERT((cursor->flags & HAMMER_CURSOR_ATEDISK) == 0);
1690 elm = &cursor->node->ondisk->elms[cursor->index];
1691 r = hammer_btree_cmp(&elm->base,
1692 &cursor->iprec->leaf.base);
1693 }
1694
1695 /*
1696 * Do we re-seek the media cursor?
1697 */
1698 if (r > 0) {
1699 if (_hammer_ip_reseek(cursor))
1700 goto again;
1701 }
1702 }
1703
1704 /*
1705 * We can now safely get the next in-memory record. We cannot
1706 * block here.
7f7c1f84
MD
1707 *
1708 * hammer_rec_scan_cmp: Is the record still in our general range,
1709 * (non-inclusive of snapshot exclusions)?
1710 * hammer_rec_scan_callback: Is the record in our snapshot?
a89aec1b 1711 */
3214ade6
MD
1712 tmprec = NULL;
1713 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1714 /*
1715 * If the current memory record was eaten then get the next
1716 * one. Stale records are skipped.
1717 */
1718 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1719 tmprec = cursor->iprec;
b84de5af 1720 cursor->iprec = NULL;
3214ade6 1721 rec = hammer_rec_rb_tree_RB_NEXT(tmprec);
7f7c1f84 1722 while (rec) {
ec4e8497
MD
1723 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1724 break;
1725 if (hammer_rec_scan_callback(rec, cursor) != 0)
1726 break;
7f7c1f84
MD
1727 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1728 }
1729 if (cursor->iprec) {
b3deaf57 1730 KKASSERT(cursor->iprec == rec);
a89aec1b 1731 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
a89aec1b
MD
1732 } else {
1733 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1734 }
3214ade6 1735 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
a89aec1b
MD
1736 }
1737 }
1738
1739 /*
3214ade6
MD
1740 * MEMORY RECORD VALIDITY TEST
1741 *
1742 * (We still can't block, which is why tmprec is being held so
1743 * long).
1744 *
1745 * If the memory record is no longer valid we skip it. It may
1746 * have been deleted by the frontend. If it was deleted or
1747 * committed by the backend the generation change re-seeked the
1748 * disk cursor and the record will be present there.
4e17f465 1749 */
3214ade6
MD
1750 if (error == 0 && (cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
1751 KKASSERT(cursor->iprec);
1752 KKASSERT((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0);
1753 if (!hammer_ip_iterate_mem_good(cursor, cursor->iprec)) {
4e17f465 1754 cursor->flags |= HAMMER_CURSOR_ATEMEM;
3214ade6
MD
1755 if (tmprec)
1756 hammer_rel_mem_record(tmprec);
1757 goto again;
4e17f465
MD
1758 }
1759 }
3214ade6
MD
1760 if (tmprec)
1761 hammer_rel_mem_record(tmprec);
4e17f465
MD
1762
1763 /*
a89aec1b
MD
1764 * Extract either the disk or memory record depending on their
1765 * relative position.
1766 */
1767 error = 0;
6b4f890b 1768 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
a89aec1b
MD
1769 case 0:
1770 /*
9f5097dc
MD
1771 * Both entries valid. Compare the entries and nominally
1772 * return the first one in the sort order. Numerous cases
1773 * require special attention, however.
a89aec1b
MD
1774 */
1775 elm = &cursor->node->ondisk->elms[cursor->index];
11ad5ade 1776 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
47637bff
MD
1777
1778 /*
9f5097dc
MD
1779 * If the two entries differ only by their key (-2/2) or
1780 * create_tid (-1/1), and are DATA records, we may have a
1781 * nominal match. We have to calculate the base file
1782 * offset of the data.
47637bff 1783 */
9f5097dc
MD
1784 if (r <= 2 && r >= -2 && r != 0 &&
1785 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1786 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1787 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1788 int64_t base2 = cursor->iprec->leaf.base.key -
1789 cursor->iprec->leaf.data_len;
bf3b416b 1790 if (base1 == base2)
9f5097dc 1791 r = 0;
9f5097dc
MD
1792 }
1793
a89aec1b
MD
1794 if (r < 0) {
1795 error = hammer_btree_extract(cursor,
11ad5ade 1796 HAMMER_CURSOR_GET_LEAF);
a89aec1b 1797 cursor->flags |= HAMMER_CURSOR_ATEDISK;
3214ade6 1798 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
a89aec1b
MD
1799 break;
1800 }
b84de5af
MD
1801
1802 /*
47637bff
MD
1803 * If the entries match exactly the memory entry is either
1804 * an on-disk directory entry deletion or a bulk data
1805 * overwrite. If it is a directory entry deletion we eat
1806 * both entries.
1807 *
1808 * For the bulk-data overwrite case it is possible to have
1809 * visibility into both, which simply means the syncer
1810 * hasn't gotten around to doing the delete+insert sequence
1811 * on the B-Tree. Use the memory entry and throw away the
1812 * on-disk entry.
1f07f686 1813 *
47637bff 1814 * If the in-memory record is not either of these we
1f07f686
MD
1815 * probably caught the syncer while it was syncing it to
1816 * the media. Since we hold a shared lock on the cursor,
1817 * the in-memory record had better be marked deleted at
1818 * this point.
b84de5af
MD
1819 */
1820 if (r == 0) {
1f07f686
MD
1821 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1822 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1823 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1824 cursor->flags |= HAMMER_CURSOR_ATEMEM;
3214ade6 1825 goto again;
1f07f686 1826 }
47637bff
MD
1827 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1828 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1829 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1830 }
1831 /* fall through to memory entry */
1f07f686 1832 } else {
3f43fb33 1833 panic("hammer_ip_next: duplicate mem/b-tree entry %p %d %08x", cursor->iprec, cursor->iprec->type, cursor->iprec->flags);
b84de5af 1834 cursor->flags |= HAMMER_CURSOR_ATEMEM;
3214ade6 1835 goto again;
b84de5af
MD
1836 }
1837 }
a89aec1b 1838 /* fall through to the memory entry */
6b4f890b 1839 case HAMMER_CURSOR_ATEDISK:
a89aec1b 1840 /*
4e17f465 1841 * Only the memory entry is valid.
a89aec1b 1842 */
11ad5ade 1843 cursor->leaf = &cursor->iprec->leaf;
a89aec1b 1844 cursor->flags |= HAMMER_CURSOR_ATEMEM;
3214ade6 1845 cursor->flags |= HAMMER_CURSOR_LASTWASMEM;
4e17f465
MD
1846
1847 /*
1848 * If the memory entry is an on-disk deletion we should have
1849 * also had found a B-Tree record. If the backend beat us
1850 * to it it would have interlocked the cursor and we should
1851 * have seen the in-memory record marked DELETED_FE.
1852 */
98f7132d
MD
1853 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1854 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
3f43fb33 1855 panic("hammer_ip_next: del-on-disk with no b-tree entry iprec %p flags %08x", cursor->iprec, cursor->iprec->flags);
b84de5af 1856 }
a89aec1b 1857 break;
6b4f890b 1858 case HAMMER_CURSOR_ATEMEM:
a89aec1b
MD
1859 /*
1860 * Only the disk entry is valid
1861 */
11ad5ade 1862 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
a89aec1b 1863 cursor->flags |= HAMMER_CURSOR_ATEDISK;
3214ade6 1864 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
a89aec1b
MD
1865 break;
1866 default:
1867 /*
1868 * Neither entry is valid
1869 *
1870 * XXX error not set properly
1871 */
3214ade6 1872 cursor->flags &= ~HAMMER_CURSOR_LASTWASMEM;
11ad5ade 1873 cursor->leaf = NULL;
a89aec1b
MD
1874 error = ENOENT;
1875 break;
1876 }
1877 return(error);
1878}
1879
1880/*
40043e7f 1881 * Resolve the cursor->data pointer for the current cursor position in
a89aec1b
MD
1882 * a merged iteration.
1883 */
1884int
1885hammer_ip_resolve_data(hammer_cursor_t cursor)
1886{
47637bff 1887 hammer_record_t record;
a89aec1b
MD
1888 int error;
1889
47637bff
MD
1890 if (hammer_cursor_inmem(cursor)) {
1891 /*
1892 * The data associated with an in-memory record is usually
1893 * kmalloced, but reserve-ahead data records will have an
1894 * on-disk reference.
1895 *
1896 * NOTE: Reserve-ahead data records must be handled in the
1897 * context of the related high level buffer cache buffer
1898 * to interlock against async writes.
1899 */
1900 record = cursor->iprec;
1901 cursor->data = record->data;
a89aec1b 1902 error = 0;
47637bff
MD
1903 if (cursor->data == NULL) {
1904 KKASSERT(record->leaf.base.rec_type ==
1905 HAMMER_RECTYPE_DATA);
4a2796f3 1906 cursor->data = hammer_bread_ext(cursor->trans->hmp,
47637bff 1907 record->leaf.data_offset,
4a2796f3 1908 record->leaf.data_len,
47637bff
MD
1909 &error,
1910 &cursor->data_buffer);
1911 }
a89aec1b 1912 } else {
11ad5ade 1913 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
a89aec1b
MD
1914 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1915 }
1916 return(error);
1917}
1918
66325755 1919/*
47637bff
MD
1920 * Backend truncation / record replacement - delete records in range.
1921 *
1922 * Delete all records within the specified range for inode ip. In-memory
06ad81ff
MD
1923 * records still associated with the frontend are ignored.
1924 *
1925 * If truncating is non-zero in-memory records associated with the back-end
1926 * are ignored. If truncating is > 1 we can return EWOULDBLOCK.
a89aec1b 1927 *
a9d52b76 1928 * NOTES:
a89aec1b 1929 *
a9d52b76
MD
1930 * * An unaligned range will cause new records to be added to cover
1931 * the edge cases. (XXX not implemented yet).
47637bff 1932 *
a9d52b76
MD
1933 * * Replacement via reservations (see hammer_ip_sync_record_cursor())
1934 * also do not deal with unaligned ranges.
76376933 1935 *
a9d52b76
MD
1936 * * ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1937 *
1938 * * Record keys for regular file data have to be special-cased since
1939 * they indicate the end of the range (key = base + bytes).
1940 *
1941 * * This function may be asked to delete ridiculously huge ranges, for
1942 * example if someone truncates or removes a 1TB regular file. We
1943 * must be very careful on restarts and we may have to stop w/
1944 * EWOULDBLOCK to avoid blowing out the buffer cache.
66325755
MD
1945 */
1946int
4e17f465 1947hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
47637bff 1948 int64_t ran_beg, int64_t ran_end, int truncating)
66325755 1949{
4e17f465 1950 hammer_transaction_t trans = cursor->trans;
11ad5ade 1951 hammer_btree_leaf_elm_t leaf;
a89aec1b
MD
1952 int error;
1953 int64_t off;
a9d52b76 1954 int64_t tmp64;
a89aec1b 1955
b84de5af
MD
1956#if 0
1957 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1958#endif
1959
1960 KKASSERT(trans->type == HAMMER_TRANS_FLS);
6a37e7e4 1961retry:
4e17f465 1962 hammer_normalize_cursor(cursor);
5a930e66
MD
1963 cursor->key_beg.localization = ip->obj_localization +
1964 HAMMER_LOCALIZE_MISC;
4e17f465
MD
1965 cursor->key_beg.obj_id = ip->obj_id;
1966 cursor->key_beg.create_tid = 0;
1967 cursor->key_beg.delete_tid = 0;
1968 cursor->key_beg.obj_type = 0;
4e17f465 1969
11ad5ade 1970 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
4e17f465
MD
1971 cursor->key_beg.key = ran_beg;
1972 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
a89aec1b 1973 } else {
195c19a1
MD
1974 /*
1975 * The key in the B-Tree is (base+bytes), so the first possible
1976 * matching key is ran_beg + 1.
1977 */
4e17f465
MD
1978 cursor->key_beg.key = ran_beg + 1;
1979 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
a9d52b76 1980 }
195c19a1 1981
a9d52b76
MD
1982 cursor->key_end = cursor->key_beg;
1983 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1984 cursor->key_end.key = ran_end;
1985 } else {
195c19a1
MD
1986 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1987 if (tmp64 < ran_end)
4e17f465 1988 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
a89aec1b 1989 else
4e17f465 1990 cursor->key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 1991 }
a9d52b76
MD
1992
1993 cursor->asof = ip->obj_asof;
1994 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1995 cursor->flags |= HAMMER_CURSOR_ASOF;
1996 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1997 cursor->flags |= HAMMER_CURSOR_BACKEND;
4e17f465 1998 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
a89aec1b 1999
4e17f465 2000 error = hammer_ip_first(cursor);
a89aec1b
MD
2001
2002 /*
2003 * Iterate through matching records and mark them as deleted.
2004 */
2005 while (error == 0) {
11ad5ade 2006 leaf = cursor->leaf;
a89aec1b 2007
11ad5ade 2008 KKASSERT(leaf->base.delete_tid == 0);
e4a5ff06 2009 KKASSERT(leaf->base.obj_id == ip->obj_id);
a89aec1b
MD
2010
2011 /*
2012 * There may be overlap cases for regular file data. Also
47637bff
MD
2013 * remember the key for a regular file record is (base + len),
2014 * NOT (base).
e4a5ff06 2015 *
ef11b6e1 2016 * Note that due to duplicates (mem & media) allowed by
e4a5ff06 2017 * DELETE_VISIBILITY, off can wind up less then ran_beg.
a89aec1b 2018 */
11ad5ade 2019 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
11ad5ade 2020 off = leaf->base.key - leaf->data_len;
a89aec1b 2021 /*
76376933
MD
2022 * Check the left edge case. We currently do not
2023 * split existing records.
a89aec1b 2024 */
e4a5ff06 2025 if (off < ran_beg && leaf->base.key > ran_beg) {
76376933 2026 panic("hammer left edge case %016llx %d\n",
973c11b9
MD
2027 (long long)leaf->base.key,
2028 leaf->data_len);
a89aec1b
MD
2029 }
2030
2031 /*
2032 * Check the right edge case. Note that the
2033 * record can be completely out of bounds, which
2034 * terminates the search.
2035 *
76376933
MD
2036 * base->key is exclusive of the right edge while
2037 * ran_end is inclusive of the right edge. The
2038 * (key - data_len) left boundary is inclusive.
195c19a1
MD
2039 *
2040 * XXX theory-check this test at some point, are
2041 * we missing a + 1 somewhere? Note that ran_end
2042 * could overflow.
a89aec1b 2043 */
11ad5ade
MD
2044 if (leaf->base.key - 1 > ran_end) {
2045 if (leaf->base.key - leaf->data_len > ran_end)
a89aec1b 2046 break;
a89aec1b
MD
2047 panic("hammer right edge case\n");
2048 }
a9d52b76
MD
2049 } else {
2050 off = leaf->base.key;
a89aec1b
MD
2051 }
2052
2053 /*
47637bff
MD
2054 * Delete the record. When truncating we do not delete
2055 * in-memory (data) records because they represent data
2056 * written after the truncation.
2057 *
2058 * This will also physically destroy the B-Tree entry and
195c19a1 2059 * data if the retention policy dictates. The function
5c8d05e2
MD
2060 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
2061 * to retest the new 'current' element.
a89aec1b 2062 */
06ad81ff 2063 if (truncating == 0 || hammer_cursor_ondisk(cursor)) {
47637bff 2064 error = hammer_ip_delete_record(cursor, ip, trans->tid);
a9d52b76
MD
2065 /*
2066 * If we have built up too many meta-buffers we risk
2067 * deadlocking the kernel and must stop. This can
2068 * occur when deleting ridiculously huge files.
2069 * sync_trunc_off is updated so the next cycle does
2070 * not re-iterate records we have already deleted.
2071 *
2072 * This is only done with formal truncations.
2073 */
06ad81ff
MD
2074 if (truncating > 1 && error == 0 &&
2075 hammer_flusher_meta_limit(ip->hmp)) {
a9d52b76 2076 ip->sync_trunc_off = off;
06ad81ff
MD
2077 error = EWOULDBLOCK;
2078 }
2079 }
195c19a1
MD
2080 if (error)
2081 break;
a9d52b76 2082 ran_beg = off; /* for restart */
4e17f465
MD
2083 error = hammer_ip_next(cursor);
2084 }
cb51be26 2085 if (cursor->node)
bcac4bbb 2086 hammer_cache_node(&ip->cache[1], cursor->node);
cb51be26 2087
4e17f465
MD
2088 if (error == EDEADLK) {
2089 hammer_done_cursor(cursor);
bcac4bbb 2090 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
4e17f465
MD
2091 if (error == 0)
2092 goto retry;
a89aec1b 2093 }
a89aec1b
MD
2094 if (error == ENOENT)
2095 error = 0;
2096 return(error);
66325755
MD
2097}
2098
b3deaf57 2099/*
5fa5c92f
MD
2100 * This backend function deletes the specified record on-disk, similar to
2101 * delete_range but for a specific record. Unlike the exact deletions
2102 * used when deleting a directory entry this function uses an ASOF search
2103 * like delete_range.
2104 *
2105 * This function may be called with ip->obj_asof set for a slave snapshot,
2106 * so don't use it. We always delete non-historical records only.
2107 */
2108static int
2109hammer_delete_general(hammer_cursor_t cursor, hammer_inode_t ip,
2110 hammer_btree_leaf_elm_t leaf)
2111{
2112 hammer_transaction_t trans = cursor->trans;
2113 int error;
2114
2115 KKASSERT(trans->type == HAMMER_TRANS_FLS);
2116retry:
2117 hammer_normalize_cursor(cursor);
2118 cursor->key_beg = leaf->base;
2119 cursor->asof = HAMMER_MAX_TID;
2120 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2121 cursor->flags |= HAMMER_CURSOR_ASOF;
2122 cursor->flags |= HAMMER_CURSOR_BACKEND;
2123 cursor->flags &= ~HAMMER_CURSOR_INSERT;
2124
2125 error = hammer_btree_lookup(cursor);
2126 if (error == 0) {
2127 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2128 }
2129 if (error == EDEADLK) {
2130 hammer_done_cursor(cursor);
2131 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
2132 if (error == 0)
2133 goto retry;
2134 }
2135 return(error);
2136}
2137
2138/*
a9d52b76
MD
2139 * This function deletes remaining auxillary records when an inode is
2140 * being deleted. This function explicitly does not delete the
2141 * inode record, directory entry, data, or db records. Those must be
2142 * properly disposed of prior to this call.
b3deaf57 2143 */
7a04d74f 2144int
a9d52b76 2145hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip, int *countp)
7a04d74f 2146{
4e17f465 2147 hammer_transaction_t trans = cursor->trans;
11ad5ade 2148 hammer_btree_leaf_elm_t leaf;
7a04d74f
MD
2149 int error;
2150
b84de5af 2151 KKASSERT(trans->type == HAMMER_TRANS_FLS);
6a37e7e4 2152retry:
4e17f465 2153 hammer_normalize_cursor(cursor);
5a930e66
MD
2154 cursor->key_beg.localization = ip->obj_localization +
2155 HAMMER_LOCALIZE_MISC;
4e17f465
MD
2156 cursor->key_beg.obj_id = ip->obj_id;
2157 cursor->key_beg.create_tid = 0;
2158 cursor->key_beg.delete_tid = 0;
2159 cursor->key_beg.obj_type = 0;
a9d52b76 2160 cursor->key_beg.rec_type = HAMMER_RECTYPE_CLEAN_START;
4e17f465
MD
2161 cursor->key_beg.key = HAMMER_MIN_KEY;
2162
2163 cursor->key_end = cursor->key_beg;
a9d52b76 2164 cursor->key_end.rec_type = HAMMER_RECTYPE_MAX;
4e17f465
MD
2165 cursor->key_end.key = HAMMER_MAX_KEY;
2166
2167 cursor->asof = ip->obj_asof;
2168 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
2169 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2170 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
2171 cursor->flags |= HAMMER_CURSOR_BACKEND;
2172
2173 error = hammer_ip_first(cursor);
7a04d74f
MD
2174
2175 /*
2176 * Iterate through matching records and mark them as deleted.
2177 */
2178 while (error == 0) {
11ad5ade 2179 leaf = cursor->leaf;
7a04d74f 2180
11ad5ade 2181 KKASSERT(leaf->base.delete_tid == 0);
7a04d74f
MD
2182
2183 /*
2184 * Mark the record and B-Tree entry as deleted. This will
2185 * also physically delete the B-Tree entry, record, and
2186 * data if the retention policy dictates. The function
5c8d05e2
MD
2187 * will set HAMMER_CURSOR_RETEST to cause hammer_ip_next()
2188 * to retest the new 'current' element.
1f07f686
MD
2189 *
2190 * Directory entries (and delete-on-disk directory entries)
2191 * must be synced and cannot be deleted.
7a04d74f 2192 */
a9d52b76
MD
2193 error = hammer_ip_delete_record(cursor, ip, trans->tid);
2194 ++*countp;
7a04d74f
MD
2195 if (error)
2196 break;
4e17f465
MD
2197 error = hammer_ip_next(cursor);
2198 }
cb51be26 2199 if (cursor->node)
bcac4bbb 2200 hammer_cache_node(&ip->cache[1], cursor->node);
4e17f465
MD
2201 if (error == EDEADLK) {
2202 hammer_done_cursor(cursor);
bcac4bbb 2203 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
4e17f465
MD
2204 if (error == 0)
2205 goto retry;
7a04d74f 2206 }
7a04d74f
MD
2207 if (error == ENOENT)
2208 error = 0;
2209 return(error);
2210}
2211
195c19a1 2212/*
46fe7ae1
MD
2213 * Delete the record at the current cursor. On success the cursor will
2214 * be positioned appropriately for an iteration but may no longer be at
2215 * a leaf node.
6a37e7e4 2216 *
b84de5af
MD
2217 * This routine is only called from the backend.
2218 *
6a37e7e4
MD
2219 * NOTE: This can return EDEADLK, requiring the caller to terminate the
2220 * cursor and retry.
195c19a1
MD
2221 */
2222int
e63644f0
MD
2223hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
2224 hammer_tid_t tid)
195c19a1 2225{
4a2796f3 2226 hammer_record_t iprec;
195c19a1
MD
2227 hammer_mount_t hmp;
2228 int error;
2229
d36ec43b 2230 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
cebe9493 2231 KKASSERT(tid != 0);
4a2796f3 2232 hmp = cursor->node->hmp;
d36ec43b 2233
195c19a1 2234 /*
d36ec43b
MD
2235 * In-memory (unsynchronized) records can simply be freed. This
2236 * only occurs in range iterations since all other records are
2237 * individually synchronized. Thus there should be no confusion with
2238 * the interlock.
4a2796f3
MD
2239 *
2240 * An in-memory record may be deleted before being committed to disk,
1b0ab2c3
MD
2241 * but could have been accessed in the mean time. The reservation
2242 * code will deal with the case.
195c19a1 2243 */
47637bff 2244 if (hammer_cursor_inmem(cursor)) {
4a2796f3
MD
2245 iprec = cursor->iprec;
2246 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
2247 iprec->flags |= HAMMER_RECF_DELETED_FE;
2248 iprec->flags |= HAMMER_RECF_DELETED_BE;
3214ade6
MD
2249 KKASSERT(iprec->ip == ip);
2250 ++ip->rec_generation;
195c19a1
MD
2251 return(0);
2252 }
2253
2254 /*
2255 * On-disk records are marked as deleted by updating their delete_tid.
9582c7da
MD
2256 * This does not effect their position in the B-Tree (which is based
2257 * on their create_tid).
842e7a70
MD
2258 *
2259 * Frontend B-Tree operations track inodes so we tell
2260 * hammer_delete_at_cursor() not to.
195c19a1 2261 */
11ad5ade 2262 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
195c19a1
MD
2263
2264 if (error == 0) {
602c6cb8
MD
2265 error = hammer_delete_at_cursor(
2266 cursor,
2267 HAMMER_DELETE_ADJUST | hammer_nohistory(ip),
842e7a70
MD
2268 cursor->trans->tid,
2269 cursor->trans->time32,
2270 0, NULL);
195c19a1
MD
2271 }
2272 return(error);
2273}
2274
602c6cb8 2275/*
83f2a3aa
MD
2276 * Used to write a generic record w/optional data to the media b-tree
2277 * when no inode context is available. Used by the mirroring and
2278 * snapshot code.
2279 *
2280 * Caller must set cursor->key_beg to leaf->base. The cursor must be
2281 * flagged for backend operation and not flagged ASOF (since we are
2282 * doing an insertion).
2283 *
2284 * This function will acquire the appropriate sync lock and will set
2285 * the cursor insertion flag for the operation, do the btree lookup,
2286 * and the insertion, and clear the insertion flag and sync lock before
2287 * returning. The cursor state will be such that the caller can continue
2288 * scanning (used by the mirroring code).
2289 *
2290 * mode: HAMMER_CREATE_MODE_UMIRROR copyin data, check crc
2291 * HAMMER_CREATE_MODE_SYS bcopy data, generate crc
2292 *
2293 * NOTE: EDEADLK can be returned. The caller must do deadlock handling and
2294 * retry.
2295 *
2296 * EALREADY can be returned if the record already exists (WARNING,
2297 * because ASOF cannot be used no check is made for illegal
2298 * duplicates).
2299 *
2300 * NOTE: Do not use the function for normal inode-related records as this
2301 * functions goes directly to the media and is not integrated with
2302 * in-memory records.
2303 */
2304int
2305hammer_create_at_cursor(hammer_cursor_t cursor, hammer_btree_leaf_elm_t leaf,
2306 void *udata, int mode)
2307{
2308 hammer_transaction_t trans;
2309 hammer_buffer_t data_buffer;
2310 hammer_off_t ndata_offset;
2311 hammer_tid_t high_tid;
2312 void *ndata;
2313 int error;
2314 int doprop;
2315
2316 trans = cursor->trans;
2317 data_buffer = NULL;
2318 ndata_offset = 0;
2319 doprop = 0;
2320
2321 KKASSERT((cursor->flags &
2322 (HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF)) ==
2323 (HAMMER_CURSOR_BACKEND));
2324
2325 hammer_sync_lock_sh(trans);
2326
2327 if (leaf->data_len) {
2328 ndata = hammer_alloc_data(trans, leaf->data_len,
2329 leaf->base.rec_type,
2330 &ndata_offset, &data_buffer,
2331 0, &error);
2332 if (ndata == NULL) {
2333 hammer_sync_unlock(trans);
2334 return (error);
2335 }
2336 leaf->data_offset = ndata_offset;
2337 hammer_modify_buffer(trans, data_buffer, NULL, 0);
2338
2339 switch(mode) {
2340 case HAMMER_CREATE_MODE_UMIRROR:
2341 error = copyin(udata, ndata, leaf->data_len);
2342 if (error == 0) {
2343 if (hammer_crc_test_leaf(ndata, leaf) == 0) {
2344 kprintf("data crc mismatch on pipe\n");
2345 error = EINVAL;
2346 } else {
2347 error = hammer_cursor_localize_data(
2348 ndata, leaf);
2349 }
2350 }
2351 break;
2352 case HAMMER_CREATE_MODE_SYS:
2353 bcopy(udata, ndata, leaf->data_len);
2354 error = 0;
2355 hammer_crc_set_leaf(ndata, leaf);
2356 break;
2357 default:
2358 panic("hammer: hammer_create_at_cursor: bad mode %d",
2359 mode);
2360 break; /* NOT REACHED */
2361 }
2362 hammer_modify_buffer_done(data_buffer);
2363 } else {
2364 leaf->data_offset = 0;
2365 error = 0;
2366 ndata = NULL;
2367 }
2368 if (error)
2369 goto failed;
2370
2371 /*
2372 * Do the insertion. This can fail with a EDEADLK or EALREADY
2373 */
2374 cursor->flags |= HAMMER_CURSOR_INSERT;
2375 error = hammer_btree_lookup(cursor);
2376 if (error != ENOENT) {
2377 if (error == 0)
2378 error = EALREADY;
2379 goto failed;
2380 }
2381 error = hammer_btree_insert(cursor, leaf, &doprop);
2382
2383 /*
2384 * Cursor is left on current element, we want to skip it now.
2385 * (in case the caller is scanning)
2386 */
2387 cursor->flags |= HAMMER_CURSOR_ATEDISK;
2388 cursor->flags &= ~HAMMER_CURSOR_INSERT;
2389
2390 /*
2391 * If the insertion happens to be creating (and not just replacing)
2392 * an inode we have to track it.
2393 */
2394 if (error == 0 &&
2395 leaf->base.rec_type == HAMMER_RECTYPE_INODE &&
2396 leaf->base.delete_tid == 0) {
2397 hammer_modify_volume_field(trans, trans->rootvol,
2398 vol0_stat_inodes);
2399 ++trans->hmp->rootvol->ondisk->vol0_stat_inodes;
2400 hammer_modify_volume_done(trans->rootvol);
2401 }
2402
2403 /*
2404 * vol0_next_tid must track the highest TID stored in the filesystem.
2405 * We do not need to generate undo for this update.
2406 */
2407 high_tid = leaf->base.create_tid;
2408 if (high_tid < leaf->base.delete_tid)
2409 high_tid = leaf->base.delete_tid;
2410 if (trans->rootvol->ondisk->vol0_next_tid < high_tid) {
2411 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
2412 trans->rootvol->ondisk->vol0_next_tid = high_tid;
2413 hammer_modify_volume_done(trans->rootvol);
2414 }
2415
2416 /*
2417 * WARNING! cursor's leaf pointer may have changed after
2418 * do_propagation returns.
2419 */
2420 if (error == 0 && doprop)
2421 hammer_btree_do_propagation(cursor, NULL, leaf);
2422
2423failed:
2424 /*
2425 * Cleanup
2426 */
2427 if (error && leaf->data_offset) {
2428 hammer_blockmap_free(trans, leaf->data_offset, leaf->data_len);
2429
2430 }
2431 hammer_sync_unlock(trans);
2432 if (data_buffer)
2433 hammer_rel_buffer(data_buffer, 0);
2434 return (error);
2435}
2436
2437/*
602c6cb8
MD
2438 * Delete the B-Tree element at the current cursor and do any necessary
2439 * mirror propagation.
2440 *
2441 * The cursor must be properly positioned for an iteration on return but
2442 * may be pointing at an internal element.
842e7a70
MD
2443 *
2444 * An element can be un-deleted by passing a delete_tid of 0 with
2445 * HAMMER_DELETE_ADJUST.
602c6cb8 2446 */
7dc57964 2447int
602c6cb8 2448hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
842e7a70
MD
2449 hammer_tid_t delete_tid, u_int32_t delete_ts,
2450 int track, int64_t *stat_bytes)
7dc57964 2451{
602c6cb8 2452 struct hammer_btree_leaf_elm save_leaf;
842e7a70 2453 hammer_transaction_t trans;
602c6cb8
MD
2454 hammer_btree_leaf_elm_t leaf;
2455 hammer_node_t node;
7dc57964 2456 hammer_btree_elm_t elm;
47197d71 2457 hammer_off_t data_offset;
7dc57964 2458 int32_t data_len;
bf686dbe 2459 u_int16_t rec_type;
7dc57964 2460 int error;
842e7a70 2461 int icount;
602c6cb8 2462 int doprop;
7dc57964 2463
602c6cb8
MD
2464 error = hammer_cursor_upgrade(cursor);
2465 if (error)
2466 return(error);
2467
842e7a70 2468 trans = cursor->trans;
602c6cb8
MD
2469 node = cursor->node;
2470 elm = &node->ondisk->elms[cursor->index];
2471 leaf = &elm->leaf;
7dc57964
MD
2472 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
2473
842e7a70
MD
2474 hammer_sync_lock_sh(trans);
2475 doprop = 0;
2476 icount = 0;
2477
602c6cb8
MD
2478 /*
2479 * Adjust the delete_tid. Update the mirror_tid propagation field
02325004 2480 * as well. delete_tid can be 0 (undelete -- used by mirroring).
602c6cb8 2481 */
602c6cb8 2482 if (delete_flags & HAMMER_DELETE_ADJUST) {
842e7a70
MD
2483 if (elm->base.rec_type == HAMMER_RECTYPE_INODE) {
2484 if (elm->leaf.base.delete_tid == 0 && delete_tid)
2485 icount = -1;
2486 if (elm->leaf.base.delete_tid && delete_tid == 0)
2487 icount = 1;
2488 }
2489
2490 hammer_modify_node(trans, node, elm, sizeof(*elm));
2491 elm->leaf.base.delete_tid = delete_tid;
2492 elm->leaf.delete_ts = delete_ts;
602c6cb8
MD
2493 hammer_modify_node_done(node);
2494
2495 if (elm->leaf.base.delete_tid > node->ondisk->mirror_tid) {
842e7a70 2496 hammer_modify_node_field(trans, node, mirror_tid);
602c6cb8
MD
2497 node->ondisk->mirror_tid = elm->leaf.base.delete_tid;
2498 hammer_modify_node_done(node);
2499 doprop = 1;
02325004
MD
2500 if (hammer_debug_general & 0x0002) {
2501 kprintf("delete_at_cursor: propagate %016llx"
2502 " @%016llx\n",
973c11b9
MD
2503 (long long)elm->leaf.base.delete_tid,
2504 (long long)node->node_offset);
02325004 2505 }
602c6cb8 2506 }
7dc57964 2507
7dc57964 2508 /*
602c6cb8
MD
2509 * Adjust for the iteration. We have deleted the current
2510 * element and want to clear ATEDISK so the iteration does
2511 * not skip the element after, which now becomes the current
5c8d05e2
MD
2512 * element. This element must be re-tested if doing an
2513 * iteration, which is handled by the RETEST flag.
7dc57964
MD
2514 */
2515 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
5c8d05e2 2516 cursor->flags |= HAMMER_CURSOR_RETEST;
7dc57964
MD
2517 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2518 }
602c6cb8
MD
2519
2520 /*
2521 * An on-disk record cannot have the same delete_tid
2522 * as its create_tid. In a chain of record updates
2523 * this could result in a duplicate record.
2524 */
2525 KKASSERT(elm->leaf.base.delete_tid !=
2526 elm->leaf.base.create_tid);
40043e7f 2527 }
602c6cb8
MD
2528
2529 /*
2530 * Destroy the B-Tree element if asked (typically if a nohistory
2531 * file or mount, or when called by the pruning code).
2532 *
2533 * Adjust the ATEDISK flag to properly support iterations.
2534 */
2535 if (delete_flags & HAMMER_DELETE_DESTROY) {
2536 data_offset = elm->leaf.data_offset;
2537 data_len = elm->leaf.data_len;
2538 rec_type = elm->leaf.base.rec_type;
2539 if (doprop) {
2540 save_leaf = elm->leaf;
2541 leaf = &save_leaf;
eb3f8f1f 2542 }
842e7a70
MD
2543 if (elm->base.rec_type == HAMMER_RECTYPE_INODE &&
2544 elm->leaf.base.delete_tid == 0) {
2545 icount = -1;
2546 }
602c6cb8
MD
2547
2548 error = hammer_btree_delete(cursor);
2549 if (error == 0) {
2550 /*
5c8d05e2
MD
2551 * The deletion moves the next element (if any) to
2552 * the current element position. We must clear
2553 * ATEDISK so this element is not skipped and we
2554 * must set RETEST to force any iteration to re-test
2555 * the element.
602c6cb8
MD
2556 */
2557 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
5c8d05e2 2558 cursor->flags |= HAMMER_CURSOR_RETEST;
602c6cb8
MD
2559 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
2560 }
2561 }
2562 if (error == 0) {
2563 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
2564 case HAMMER_ZONE_LARGE_DATA:
2565 case HAMMER_ZONE_SMALL_DATA:
2566 case HAMMER_ZONE_META:
842e7a70 2567 hammer_blockmap_free(trans,
602c6cb8
MD
2568 data_offset, data_len);
2569 break;
2570 default:
2571 break;
2572 }
2573 }
2574 }
2575
2576 /*
842e7a70
MD
2577 * Track inode count and next_tid. This is used by the mirroring
2578 * and PFS code. icount can be negative, zero, or positive.
2579 */
2580 if (error == 0 && track) {
2581 if (icount) {
2582 hammer_modify_volume_field(trans, trans->rootvol,
2583 vol0_stat_inodes);
2584 trans->rootvol->ondisk->vol0_stat_inodes += icount;
2585 hammer_modify_volume_done(trans->rootvol);
2586 }
2587 if (trans->rootvol->ondisk->vol0_next_tid < delete_tid) {
2588 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
2589 trans->rootvol->ondisk->vol0_next_tid = delete_tid;
2590 hammer_modify_volume_done(trans->rootvol);
2591 }
2592 }
2593
2594 /*
602c6cb8
MD
2595 * mirror_tid propagation occurs if the node's mirror_tid had to be
2596 * updated while adjusting the delete_tid.
2597 *
2598 * This occurs when deleting even in nohistory mode, but does not
2599 * occur when pruning an already-deleted node.
842e7a70
MD
2600 *
2601 * cursor->ip is NULL when called from the pruning, mirroring,
2602 * and pfs code. If non-NULL propagation will be conditionalized
2603 * on whether the PFS is in no-history mode or not.
c9ce54d6
MD
2604 *
2605 * WARNING: cursor's leaf pointer may have changed after do_propagation
2606 * returns!
602c6cb8
MD
2607 */
2608 if (doprop) {
842e7a70
MD
2609 if (cursor->ip)
2610 hammer_btree_do_propagation(cursor, cursor->ip->pfsm, leaf);
2611 else
2612 hammer_btree_do_propagation(cursor, NULL, leaf);
7dc57964 2613 }
842e7a70 2614 hammer_sync_unlock(trans);
7dc57964
MD
2615 return (error);
2616}
2617
b3deaf57 2618/*
1f07f686
MD
2619 * Determine whether we can remove a directory. This routine checks whether
2620 * a directory is empty or not and enforces flush connectivity.
2621 *
2622 * Flush connectivity requires that we block if the target directory is
2623 * currently flushing, otherwise it may not end up in the same flush group.
2624 *
2625 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
b3deaf57
MD
2626 */
2627int
98f7132d 2628hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
b3deaf57
MD
2629{
2630 struct hammer_cursor cursor;
2631 int error;
2632
1f07f686
MD
2633 /*
2634 * Check directory empty
2635 */
bcac4bbb 2636 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
b3deaf57 2637
5a930e66 2638 cursor.key_beg.localization = ip->obj_localization +
beec5dc4 2639 hammer_dir_localization(ip);
b3deaf57 2640 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 2641 cursor.key_beg.create_tid = 0;
b3deaf57
MD
2642 cursor.key_beg.delete_tid = 0;
2643 cursor.key_beg.obj_type = 0;
2644 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
2645 cursor.key_beg.key = HAMMER_MIN_KEY;
2646
2647 cursor.key_end = cursor.key_beg;
2648 cursor.key_end.rec_type = 0xFFFF;
2649 cursor.key_end.key = HAMMER_MAX_KEY;
2650
d5530d22
MD
2651 cursor.asof = ip->obj_asof;
2652 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
b3deaf57 2653
4e17f465 2654 error = hammer_ip_first(&cursor);
b3deaf57
MD
2655 if (error == ENOENT)
2656 error = 0;
2657 else if (error == 0)
2658 error = ENOTEMPTY;
2659 hammer_done_cursor(&cursor);
2660 return(error);
2661}
2662
83f2a3aa
MD
2663/*
2664 * Localize the data payload. Directory entries may need their
2665 * localization adjusted.
2666 */
2667static
2668int
2669hammer_cursor_localize_data(hammer_data_ondisk_t data,
2670 hammer_btree_leaf_elm_t leaf)
2671{
2672 u_int32_t localization;
2673
2674 if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
2675 localization = leaf->base.localization &
2676 HAMMER_LOCALIZE_PSEUDOFS_MASK;
2677 if (data->entry.localization != localization) {
2678 data->entry.localization = localization;
2679 hammer_crc_set_leaf(data, leaf);
2680 }
2681 }
2682 return(0);
2683}