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