Merge from vendor branch NETGRAPH:
[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 *
c82af904 34 * $DragonFly: src/sys/vfs/hammer/hammer_object.c,v 1.76 2008/06/26 04:06:23 dillon Exp $
66325755
MD
35 */
36
37#include "hammer.h"
38
47637bff 39static int hammer_mem_add(hammer_record_t record);
45a014dc 40static int hammer_mem_lookup(hammer_cursor_t cursor);
4e17f465 41static int hammer_mem_first(hammer_cursor_t cursor);
0832c9bb
MD
42static int hammer_rec_trunc_callback(hammer_record_t record,
43 void *data __unused);
cb51be26 44static int hammer_record_needs_overwrite_delete(hammer_record_t record);
8cd0a023 45
47637bff
MD
46struct rec_trunc_info {
47 u_int16_t rec_type;
48 int64_t trunc_off;
49};
50
66325755 51/*
0832c9bb 52 * Red-black tree support. Comparison code for insertion.
66325755 53 */
8cd0a023 54static int
a89aec1b 55hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
8cd0a023 56{
11ad5ade 57 if (rec1->leaf.base.rec_type < rec2->leaf.base.rec_type)
8cd0a023 58 return(-1);
11ad5ade 59 if (rec1->leaf.base.rec_type > rec2->leaf.base.rec_type)
8cd0a023
MD
60 return(1);
61
11ad5ade 62 if (rec1->leaf.base.key < rec2->leaf.base.key)
8cd0a023 63 return(-1);
11ad5ade 64 if (rec1->leaf.base.key > rec2->leaf.base.key)
8cd0a023
MD
65 return(1);
66
ec4e8497 67 /*
d36ec43b 68 * Never match against an item deleted by the front-end.
bf3b416b
MD
69 *
70 * rec1 is greater then rec2 if rec1 is marked deleted.
71 * rec1 is less then rec2 if rec2 is marked deleted.
72 *
73 * Multiple deleted records may be present, do not return 0
74 * if both are marked deleted.
ec4e8497
MD
75 */
76 if (rec1->flags & HAMMER_RECF_DELETED_FE)
77 return(1);
78 if (rec2->flags & HAMMER_RECF_DELETED_FE)
79 return(-1);
80
8cd0a023
MD
81 return(0);
82}
83
0832c9bb
MD
84/*
85 * Basic record comparison code similar to hammer_btree_cmp().
86 */
8cd0a023 87static int
0832c9bb 88hammer_rec_cmp(hammer_base_elm_t elm, hammer_record_t rec)
66325755 89{
0832c9bb 90 if (elm->rec_type < rec->leaf.base.rec_type)
d26d0ae9 91 return(-3);
0832c9bb 92 if (elm->rec_type > rec->leaf.base.rec_type)
d26d0ae9 93 return(3);
8cd0a023 94
0832c9bb 95 if (elm->key < rec->leaf.base.key)
8cd0a023 96 return(-2);
0832c9bb 97 if (elm->key > rec->leaf.base.key)
8cd0a023
MD
98 return(2);
99
cebe9493
MD
100 /*
101 * Never match against an item deleted by the front-end.
bf3b416b 102 * elm is less then rec if rec is marked deleted.
cebe9493
MD
103 */
104 if (rec->flags & HAMMER_RECF_DELETED_FE)
bf3b416b 105 return(-1);
0832c9bb
MD
106 return(0);
107}
108
109/*
110 * Special LOOKUP_INFO to locate an overlapping record. This used by
111 * the reservation code to implement small-block records (whos keys will
112 * be different depending on data_len, when representing the same base
113 * offset).
114 *
115 * NOTE: The base file offset of a data record is (key - data_len), not (key).
116 */
117static int
118hammer_rec_overlap_compare(hammer_btree_leaf_elm_t leaf, hammer_record_t rec)
119{
120 if (leaf->base.rec_type < rec->leaf.base.rec_type)
121 return(-3);
122 if (leaf->base.rec_type > rec->leaf.base.rec_type)
123 return(3);
124
4a2796f3
MD
125 /*
126 * Overlap compare
127 */
0832c9bb 128 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
cebe9493 129 /* leaf_end <= rec_beg */
0832c9bb
MD
130 if (leaf->base.key <= rec->leaf.base.key - rec->leaf.data_len)
131 return(-2);
cebe9493 132 /* leaf_beg >= rec_end */
0832c9bb
MD
133 if (leaf->base.key - leaf->data_len >= rec->leaf.base.key)
134 return(2);
135 } else {
136 if (leaf->base.key < rec->leaf.base.key)
137 return(-2);
138 if (leaf->base.key > rec->leaf.base.key)
139 return(2);
140 }
141
cebe9493
MD
142 /*
143 * Never match against an item deleted by the front-end.
bf3b416b 144 * leaf is less then rec if rec is marked deleted.
4a2796f3
MD
145 *
146 * We must still return the proper code for the scan to continue
147 * along the correct branches.
cebe9493 148 */
4a2796f3
MD
149 if (rec->flags & HAMMER_RECF_DELETED_FE) {
150 if (leaf->base.key < rec->leaf.base.key)
151 return(-2);
152 if (leaf->base.key > rec->leaf.base.key)
153 return(2);
bf3b416b 154 return(-1);
4a2796f3 155 }
8cd0a023 156 return(0);
66325755
MD
157}
158
6b4f890b 159/*
7f7c1f84 160 * RB_SCAN comparison code for hammer_mem_first(). The argument order
6b4f890b 161 * is reversed so the comparison result has to be negated. key_beg and
7f7c1f84
MD
162 * key_end are both range-inclusive.
163 *
7f7c1f84 164 * Localized deletions are not cached in-memory.
6b4f890b
MD
165 */
166static
167int
168hammer_rec_scan_cmp(hammer_record_t rec, void *data)
169{
170 hammer_cursor_t cursor = data;
171 int r;
172
0832c9bb 173 r = hammer_rec_cmp(&cursor->key_beg, rec);
7f7c1f84 174 if (r > 1)
6b4f890b 175 return(-1);
0832c9bb 176 r = hammer_rec_cmp(&cursor->key_end, rec);
7f7c1f84 177 if (r < -1)
6b4f890b
MD
178 return(1);
179 return(0);
180}
181
2ae23e2e
MD
182/*
183 * This compare function is used when simply looking up key_beg.
184 */
185static
186int
187hammer_rec_find_cmp(hammer_record_t rec, void *data)
188{
189 hammer_cursor_t cursor = data;
190 int r;
191
0832c9bb 192 r = hammer_rec_cmp(&cursor->key_beg, rec);
2ae23e2e
MD
193 if (r > 1)
194 return(-1);
195 if (r < -1)
196 return(1);
197 return(0);
198}
199
47637bff
MD
200/*
201 * Locate blocks within the truncation range. Partial blocks do not count.
202 */
203static
204int
205hammer_rec_trunc_cmp(hammer_record_t rec, void *data)
206{
207 struct rec_trunc_info *info = data;
208
209 if (rec->leaf.base.rec_type < info->rec_type)
210 return(-1);
211 if (rec->leaf.base.rec_type > info->rec_type)
212 return(1);
213
214 switch(rec->leaf.base.rec_type) {
215 case HAMMER_RECTYPE_DB:
216 /*
217 * DB record key is not beyond the truncation point, retain.
218 */
219 if (rec->leaf.base.key < info->trunc_off)
220 return(-1);
221 break;
222 case HAMMER_RECTYPE_DATA:
223 /*
224 * DATA record offset start is not beyond the truncation point,
225 * retain.
226 */
227 if (rec->leaf.base.key - rec->leaf.data_len < info->trunc_off)
228 return(-1);
229 break;
230 default:
231 panic("hammer_rec_trunc_cmp: unexpected record type");
232 }
233
234 /*
235 * The record start is >= the truncation point, return match,
236 * the record should be destroyed.
237 */
238 return(0);
239}
240
8cd0a023
MD
241RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
242RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
0832c9bb 243 hammer_rec_overlap_compare, hammer_btree_leaf_elm_t);
8cd0a023 244
a89aec1b 245/*
d26d0ae9 246 * Allocate a record for the caller to finish filling in. The record is
b3deaf57 247 * returned referenced.
a89aec1b
MD
248 */
249hammer_record_t
11ad5ade 250hammer_alloc_mem_record(hammer_inode_t ip, int data_len)
a89aec1b
MD
251{
252 hammer_record_t record;
253
b3deaf57 254 ++hammer_count_records;
11ad5ade 255 record = kmalloc(sizeof(*record), M_HAMMER, M_WAITOK | M_ZERO);
1f07f686 256 record->flush_state = HAMMER_FST_IDLE;
a89aec1b 257 record->ip = ip;
11ad5ade
MD
258 record->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
259 record->leaf.data_len = data_len;
d26d0ae9 260 hammer_ref(&record->lock);
11ad5ade
MD
261
262 if (data_len) {
263 record->data = kmalloc(data_len, M_HAMMER, M_WAITOK | M_ZERO);
264 record->flags |= HAMMER_RECF_ALLOCDATA;
265 ++hammer_count_record_datas;
266 }
267
a89aec1b
MD
268 return (record);
269}
270
b84de5af 271void
af209b0f 272hammer_wait_mem_record_ident(hammer_record_t record, const char *ident)
b84de5af 273{
1f07f686 274 while (record->flush_state == HAMMER_FST_FLUSH) {
b84de5af 275 record->flags |= HAMMER_RECF_WANTED;
af209b0f 276 tsleep(record, 0, ident, 0);
b84de5af
MD
277 }
278}
279
280/*
d36ec43b
MD
281 * Called from the backend, hammer_inode.c, after a record has been
282 * flushed to disk. The record has been exclusively locked by the
283 * caller and interlocked with BE.
b84de5af 284 *
d36ec43b
MD
285 * We clean up the state, unlock, and release the record (the record
286 * was referenced by the fact that it was in the HAMMER_FST_FLUSH state).
b84de5af
MD
287 */
288void
d36ec43b 289hammer_flush_record_done(hammer_record_t record, int error)
b84de5af 290{
1f07f686 291 hammer_inode_t target_ip;
1f07f686
MD
292
293 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
d36ec43b
MD
294 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
295
296 if (error) {
297 /*
298 * An error occured, the backend was unable to sync the
299 * record to its media. Leave the record intact.
300 */
1f07f686 301 Debugger("flush_record_done error");
d36ec43b 302 }
4e17f465
MD
303
304 if (record->flags & HAMMER_RECF_DELETED_BE) {
1f07f686
MD
305 if ((target_ip = record->target_ip) != NULL) {
306 TAILQ_REMOVE(&target_ip->target_list, record,
307 target_entry);
308 record->target_ip = NULL;
309 hammer_test_inode(target_ip);
310 }
311 record->flush_state = HAMMER_FST_IDLE;
312 } else {
c4bae5fd 313 if (record->target_ip) {
1f07f686 314 record->flush_state = HAMMER_FST_SETUP;
c4bae5fd
MD
315 hammer_test_inode(record->ip);
316 hammer_test_inode(record->target_ip);
317 } else {
1f07f686 318 record->flush_state = HAMMER_FST_IDLE;
c4bae5fd 319 }
1f07f686 320 }
d36ec43b 321 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
b84de5af
MD
322 if (record->flags & HAMMER_RECF_WANTED) {
323 record->flags &= ~HAMMER_RECF_WANTED;
324 wakeup(record);
325 }
326 hammer_rel_mem_record(record);
327}
328
a89aec1b 329/*
b3deaf57
MD
330 * Release a memory record. Records marked for deletion are immediately
331 * removed from the RB-Tree but otherwise left intact until the last ref
332 * goes away.
a89aec1b
MD
333 */
334void
b3deaf57 335hammer_rel_mem_record(struct hammer_record *record)
a89aec1b 336{
1f07f686
MD
337 hammer_inode_t ip, target_ip;
338
b3deaf57 339 hammer_unref(&record->lock);
b33e2cc0 340
0832c9bb
MD
341 if (record->lock.refs == 0) {
342 /*
343 * Upon release of the last reference wakeup any waiters.
344 * The record structure may get destroyed so callers will
345 * loop up and do a relookup.
bf3b416b
MD
346 *
347 * WARNING! Record must be removed from RB-TREE before we
348 * might possibly block. hammer_test_inode() can block!
0832c9bb
MD
349 */
350 ip = record->ip;
0832c9bb
MD
351
352 /*
353 * Upon release of the last reference a record marked deleted
354 * is destroyed.
355 */
356 if (record->flags & HAMMER_RECF_DELETED_FE) {
bf3b416b 357 KKASSERT(ip->lock.refs > 0);
1f07f686
MD
358 KKASSERT(record->flush_state != HAMMER_FST_FLUSH);
359
bf3b416b
MD
360 /*
361 * target_ip may have zero refs, we have to ref it
362 * to prevent it from being ripped out from under
363 * us.
364 */
1f07f686
MD
365 if ((target_ip = record->target_ip) != NULL) {
366 TAILQ_REMOVE(&target_ip->target_list,
367 record, target_entry);
368 record->target_ip = NULL;
bf3b416b 369 hammer_ref(&target_ip->lock);
1f07f686 370 }
d36ec43b 371
b84de5af
MD
372 if (record->flags & HAMMER_RECF_ONRBTREE) {
373 RB_REMOVE(hammer_rec_rb_tree,
374 &record->ip->rec_tree,
375 record);
47637bff 376 KKASSERT(ip->rsv_recs > 0);
e63644f0 377 --ip->hmp->rsv_recs;
47637bff 378 --ip->rsv_recs;
e63644f0 379 ip->hmp->rsv_databytes -= record->leaf.data_len;
b84de5af 380 record->flags &= ~HAMMER_RECF_ONRBTREE;
a99b9ea2 381
ae8bb789
MD
382 if (RB_EMPTY(&record->ip->rec_tree)) {
383 record->ip->flags &= ~HAMMER_INODE_XDIRTY;
0832c9bb 384 record->ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
ae8bb789
MD
385 hammer_test_inode(record->ip);
386 }
b84de5af 387 }
bf3b416b
MD
388
389 /*
390 * Do this test after removing record from the B-Tree.
391 */
392 if (target_ip) {
393 hammer_test_inode(target_ip);
394 hammer_rel_inode(target_ip, 0);
395 }
396
b3deaf57
MD
397 if (record->flags & HAMMER_RECF_ALLOCDATA) {
398 --hammer_count_record_datas;
399 kfree(record->data, M_HAMMER);
400 record->flags &= ~HAMMER_RECF_ALLOCDATA;
401 }
0832c9bb
MD
402 if (record->resv) {
403 hammer_blockmap_reserve_complete(ip->hmp,
404 record->resv);
405 record->resv = NULL;
406 }
b3deaf57
MD
407 record->data = NULL;
408 --hammer_count_records;
409 kfree(record, M_HAMMER);
a89aec1b 410 }
a89aec1b 411 }
a89aec1b
MD
412}
413
b84de5af 414/*
a5fddc16
MD
415 * Record visibility depends on whether the record is being accessed by
416 * the backend or the frontend.
417 *
418 * Return non-zero if the record is visible, zero if it isn't or if it is
419 * deleted.
b84de5af
MD
420 */
421static __inline
422int
a5fddc16 423hammer_ip_iterate_mem_good(hammer_cursor_t cursor, hammer_record_t record)
b84de5af
MD
424{
425 if (cursor->flags & HAMMER_CURSOR_BACKEND) {
a5fddc16
MD
426 if (record->flags & HAMMER_RECF_DELETED_BE)
427 return(0);
b84de5af 428 } else {
a5fddc16 429 if (record->flags & HAMMER_RECF_DELETED_FE)
b84de5af
MD
430 return(0);
431 }
432 return(1);
433}
434
a89aec1b 435/*
2ae23e2e
MD
436 * This callback is used as part of the RB_SCAN function for in-memory
437 * records. We terminate it (return -1) as soon as we get a match.
6b4f890b 438 *
b84de5af
MD
439 * This routine is used by frontend code.
440 *
2ae23e2e
MD
441 * The primary compare code does not account for ASOF lookups. This
442 * code handles that case as well as a few others.
6b4f890b
MD
443 */
444static
445int
446hammer_rec_scan_callback(hammer_record_t rec, void *data)
447{
448 hammer_cursor_t cursor = data;
449
b33e2cc0
MD
450 /*
451 * We terminate on success, so this should be NULL on entry.
452 */
453 KKASSERT(cursor->iprec == NULL);
454
455 /*
b84de5af 456 * Skip if the record was marked deleted.
b33e2cc0 457 */
b84de5af 458 if (hammer_ip_iterate_mem_good(cursor, rec) == 0)
b33e2cc0
MD
459 return(0);
460
7f7c1f84
MD
461 /*
462 * Skip if not visible due to our as-of TID
463 */
d5530d22 464 if (cursor->flags & HAMMER_CURSOR_ASOF) {
11ad5ade 465 if (cursor->asof < rec->leaf.base.create_tid)
7f7c1f84 466 return(0);
11ad5ade
MD
467 if (rec->leaf.base.delete_tid &&
468 cursor->asof >= rec->leaf.base.delete_tid) {
7f7c1f84
MD
469 return(0);
470 }
471 }
472
473 /*
b84de5af
MD
474 * If the record is queued to the flusher we have to block until
475 * it isn't. Otherwise we may see duplication between our memory
476 * cache and the media.
7f7c1f84 477 */
b33e2cc0 478 hammer_ref(&rec->lock);
b84de5af
MD
479
480#warning "This deadlocks"
481#if 0
1f07f686 482 if (rec->flush_state == HAMMER_FST_FLUSH)
b84de5af
MD
483 hammer_wait_mem_record(rec);
484#endif
b33e2cc0
MD
485
486 /*
487 * The record may have been deleted while we were blocked.
488 */
b84de5af
MD
489 if (hammer_ip_iterate_mem_good(cursor, rec) == 0) {
490 hammer_rel_mem_record(rec);
b33e2cc0
MD
491 return(0);
492 }
493
494 /*
495 * Set the matching record and stop the scan.
496 */
497 cursor->iprec = rec;
498 return(-1);
6b4f890b
MD
499}
500
2ae23e2e
MD
501
502/*
503 * Lookup an in-memory record given the key specified in the cursor. Works
504 * just like hammer_btree_lookup() but operates on an inode's in-memory
505 * record list.
506 *
507 * The lookup must fail if the record is marked for deferred deletion.
508 */
509static
510int
45a014dc 511hammer_mem_lookup(hammer_cursor_t cursor)
2ae23e2e
MD
512{
513 int error;
514
45a014dc 515 KKASSERT(cursor->ip);
2ae23e2e
MD
516 if (cursor->iprec) {
517 hammer_rel_mem_record(cursor->iprec);
518 cursor->iprec = NULL;
519 }
45a014dc 520 hammer_rec_rb_tree_RB_SCAN(&cursor->ip->rec_tree, hammer_rec_find_cmp,
2ae23e2e
MD
521 hammer_rec_scan_callback, cursor);
522
523 if (cursor->iprec == NULL)
524 error = ENOENT;
525 else
526 error = 0;
527 return(error);
528}
529
530/*
531 * hammer_mem_first() - locate the first in-memory record matching the
532 * cursor within the bounds of the key range.
533 */
6b4f890b
MD
534static
535int
4e17f465 536hammer_mem_first(hammer_cursor_t cursor)
6b4f890b 537{
4e17f465
MD
538 hammer_inode_t ip;
539
540 ip = cursor->ip;
541 KKASSERT(ip != NULL);
542
b3deaf57
MD
543 if (cursor->iprec) {
544 hammer_rel_mem_record(cursor->iprec);
545 cursor->iprec = NULL;
546 }
b3deaf57 547
6b4f890b
MD
548 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
549 hammer_rec_scan_callback, cursor);
7f7c1f84
MD
550
551 /*
552 * Adjust scan.node and keep it linked into the RB-tree so we can
553 * hold the cursor through third party modifications of the RB-tree.
554 */
4e17f465 555 if (cursor->iprec)
6b4f890b 556 return(0);
6b4f890b
MD
557 return(ENOENT);
558}
559
560void
561hammer_mem_done(hammer_cursor_t cursor)
562{
b3deaf57
MD
563 if (cursor->iprec) {
564 hammer_rel_mem_record(cursor->iprec);
565 cursor->iprec = NULL;
566 }
6b4f890b
MD
567}
568
a89aec1b
MD
569/************************************************************************
570 * HAMMER IN-MEMORY RECORD FUNCTIONS *
571 ************************************************************************
572 *
573 * These functions manipulate in-memory records. Such records typically
574 * exist prior to being committed to disk or indexed via the on-disk B-Tree.
575 */
576
8cd0a023
MD
577/*
578 * Add a directory entry (dip,ncp) which references inode (ip).
579 *
580 * Note that the low 32 bits of the namekey are set temporarily to create
581 * a unique in-memory record, and may be modified a second time when the
582 * record is synchronized to disk. In particular, the low 32 bits cannot be
583 * all 0's when synching to disk, which is not handled here.
5a930e66
MD
584 *
585 * NOTE: bytes does not include any terminating \0 on name, and name might
586 * not be terminated.
8cd0a023 587 */
66325755 588int
a89aec1b 589hammer_ip_add_directory(struct hammer_transaction *trans,
5a930e66 590 struct hammer_inode *dip, const char *name, int bytes,
66325755
MD
591 struct hammer_inode *ip)
592{
c82af904 593 struct hammer_cursor cursor;
a89aec1b 594 hammer_record_t record;
8cd0a023 595 int error;
c82af904
MD
596 int count;
597 u_int32_t iterator;
8cd0a023 598
11ad5ade 599 record = hammer_alloc_mem_record(dip, HAMMER_ENTRY_SIZE(bytes));
6b4f890b
MD
600 if (++trans->hmp->namekey_iterator == 0)
601 ++trans->hmp->namekey_iterator;
8cd0a023 602
1f07f686 603 record->type = HAMMER_MEM_RECORD_ADD;
5a930e66
MD
604 record->leaf.base.localization = dip->obj_localization +
605 HAMMER_LOCALIZE_MISC;
11ad5ade 606 record->leaf.base.obj_id = dip->obj_id;
5a930e66 607 record->leaf.base.key = hammer_directory_namekey(name, bytes);
11ad5ade
MD
608 record->leaf.base.key += trans->hmp->namekey_iterator;
609 record->leaf.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
610 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
611 record->data->entry.obj_id = ip->obj_id;
43c665ae 612 record->data->entry.localization = ip->obj_localization;
5a930e66 613 bcopy(name, record->data->entry.name, bytes);
11ad5ade
MD
614
615 ++ip->ino_data.nlinks;
47637bff 616 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
ec4e8497 617
c82af904
MD
618 /*
619 * Find an unused namekey. Both the in-memory record tree and
620 * the B-Tree are checked. Exact matches also match create_tid
621 * so use an ASOF search to (mostly) ignore it.
622 */
623 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
624 cursor.key_beg = record->leaf.base;
625 cursor.flags |= HAMMER_CURSOR_ASOF;
626 cursor.asof = ip->obj_asof;
627
628 count = 0;
629 while (hammer_ip_lookup(&cursor) == 0) {
630 iterator = (u_int32_t)record->leaf.base.key + 1;
631 if (iterator == 0)
632 iterator = 1;
633 record->leaf.base.key &= ~0xFFFFFFFFLL;
634 record->leaf.base.key |= iterator;
635 cursor.key_beg.key = record->leaf.base.key;
636 if (++count == 1000000000) {
637 hammer_rel_mem_record(record);
638 error = ENOSPC;
639 goto failed;
640 }
641 }
642
ec4e8497 643 /*
1f07f686
MD
644 * The target inode and the directory entry are bound together.
645 */
646 record->target_ip = ip;
647 record->flush_state = HAMMER_FST_SETUP;
648 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
649
650 /*
651 * The inode now has a dependancy and must be taken out of the idle
652 * state. An inode not in an idle state is given an extra reference.
ec4e8497 653 */
1f07f686
MD
654 if (ip->flush_state == HAMMER_FST_IDLE) {
655 hammer_ref(&ip->lock);
656 ip->flush_state = HAMMER_FST_SETUP;
657 }
47637bff 658 error = hammer_mem_add(record);
c82af904
MD
659failed:
660 hammer_done_cursor(&cursor);
8cd0a023
MD
661 return(error);
662}
663
664/*
a89aec1b
MD
665 * Delete the directory entry and update the inode link count. The
666 * cursor must be seeked to the directory entry record being deleted.
667 *
b84de5af
MD
668 * The related inode should be share-locked by the caller. The caller is
669 * on the frontend.
6a37e7e4
MD
670 *
671 * This function can return EDEADLK requiring the caller to terminate
b84de5af 672 * the cursor, any locks, wait on the returned record, and retry.
8cd0a023 673 */
a89aec1b
MD
674int
675hammer_ip_del_directory(struct hammer_transaction *trans,
676 hammer_cursor_t cursor, struct hammer_inode *dip,
677 struct hammer_inode *ip)
8cd0a023 678{
b84de5af 679 hammer_record_t record;
a89aec1b 680 int error;
8cd0a023 681
47637bff 682 if (hammer_cursor_inmem(cursor)) {
b84de5af
MD
683 /*
684 * In-memory (unsynchronized) records can simply be freed.
685 * Even though the HAMMER_RECF_DELETED_FE flag is ignored
686 * by the backend, we must still avoid races against the
687 * backend potentially syncing the record to the media.
688 *
689 * We cannot call hammer_ip_delete_record(), that routine may
690 * only be called from the backend.
691 */
692 record = cursor->iprec;
d36ec43b 693 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
b84de5af
MD
694 KKASSERT(cursor->deadlk_rec == NULL);
695 hammer_ref(&record->lock);
696 cursor->deadlk_rec = record;
697 error = EDEADLK;
698 } else {
1f07f686 699 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
d36ec43b 700 record->flags |= HAMMER_RECF_DELETED_FE;
b84de5af
MD
701 error = 0;
702 }
703 } else {
704 /*
705 * If the record is on-disk we have to queue the deletion by
706 * the record's key. This also causes lookups to skip the
707 * record.
708 */
98f7132d
MD
709 KKASSERT(dip->flags &
710 (HAMMER_INODE_ONDISK | HAMMER_INODE_DONDISK));
11ad5ade 711 record = hammer_alloc_mem_record(dip, 0);
1f07f686 712 record->type = HAMMER_MEM_RECORD_DEL;
11ad5ade 713 record->leaf.base = cursor->leaf->base;
1f07f686
MD
714
715 record->target_ip = ip;
716 record->flush_state = HAMMER_FST_SETUP;
717 TAILQ_INSERT_TAIL(&ip->target_list, record, target_entry);
b84de5af 718
ec4e8497 719 /*
1f07f686
MD
720 * The inode now has a dependancy and must be taken out of
721 * the idle state. An inode not in an idle state is given
722 * an extra reference.
ec4e8497 723 */
1f07f686
MD
724 if (ip->flush_state == HAMMER_FST_IDLE) {
725 hammer_ref(&ip->lock);
726 ip->flush_state = HAMMER_FST_SETUP;
727 }
ec4e8497 728
47637bff 729 error = hammer_mem_add(record);
b84de5af 730 }
a89aec1b
MD
731
732 /*
c0ade690 733 * One less link. The file may still be open in the OS even after
3bf2d80a 734 * all links have gone away.
6a37e7e4
MD
735 *
736 * We have to terminate the cursor before syncing the inode to
3bf2d80a
MD
737 * avoid deadlocking against ourselves. XXX this may no longer
738 * be true.
855942b6 739 *
3bf2d80a
MD
740 * If nlinks drops to zero and the vnode is inactive (or there is
741 * no vnode), call hammer_inode_unloadable_check() to zonk the
742 * inode. If we don't do this here the inode will not be destroyed
743 * on-media until we unmount.
a89aec1b
MD
744 */
745 if (error == 0) {
11ad5ade 746 --ip->ino_data.nlinks;
47637bff 747 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
11ad5ade 748 if (ip->ino_data.nlinks == 0 &&
d113fda1 749 (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))) {
6a37e7e4 750 hammer_done_cursor(cursor);
e8599db1 751 hammer_inode_unloadable_check(ip, 1);
3bf2d80a 752 hammer_flush_inode(ip, 0);
d113fda1 753 }
76376933 754
a89aec1b
MD
755 }
756 return(error);
66325755
MD
757}
758
7a04d74f
MD
759/*
760 * Add a record to an inode.
761 *
762 * The caller must allocate the record with hammer_alloc_mem_record(ip) and
763 * initialize the following additional fields:
764 *
b84de5af
MD
765 * The related inode should be share-locked by the caller. The caller is
766 * on the frontend.
767 *
7a04d74f
MD
768 * record->rec.entry.base.base.key
769 * record->rec.entry.base.base.rec_type
770 * record->rec.entry.base.base.data_len
47197d71 771 * record->data (a copy will be kmalloc'd if it cannot be embedded)
7a04d74f
MD
772 */
773int
774hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
775{
776 hammer_inode_t ip = record->ip;
777 int error;
7a04d74f 778
2f85fa4d 779 KKASSERT(record->leaf.base.localization != 0);
11ad5ade
MD
780 record->leaf.base.obj_id = ip->obj_id;
781 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
47637bff 782 error = hammer_mem_add(record);
7a04d74f
MD
783 return(error);
784}
785
8cd0a023 786/*
47637bff
MD
787 * Locate a bulk record in-memory. Bulk records allow disk space to be
788 * reserved so the front-end can flush large data writes without having
789 * to queue the BIO to the flusher. Only the related record gets queued
790 * to the flusher.
791 */
792static hammer_record_t
793hammer_ip_get_bulk(hammer_inode_t ip, off_t file_offset, int bytes)
794{
795 hammer_record_t record;
0832c9bb
MD
796 struct hammer_btree_leaf_elm leaf;
797
798 bzero(&leaf, sizeof(leaf));
799 leaf.base.obj_id = ip->obj_id;
800 leaf.base.key = file_offset + bytes;
801 leaf.base.create_tid = 0;
802 leaf.base.delete_tid = 0;
803 leaf.base.rec_type = HAMMER_RECTYPE_DATA;
804 leaf.base.obj_type = 0; /* unused */
805 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD; /* unused */
5a930e66 806 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_MISC;
0832c9bb
MD
807 leaf.data_len = bytes;
808
809 record = hammer_rec_rb_tree_RB_LOOKUP_INFO(&ip->rec_tree, &leaf);
47637bff
MD
810 if (record)
811 hammer_ref(&record->lock);
812 return(record);
813}
814
815/*
816 * Reserve blockmap space placemarked with an in-memory record.
817 *
bcac4bbb
MD
818 * This routine is called by the frontend in order to be able to directly
819 * flush a buffer cache buffer. The frontend has locked the related buffer
820 * cache buffers and we should be able to manipulate any overlapping
821 * in-memory records.
47637bff
MD
822 */
823hammer_record_t
0832c9bb
MD
824hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset, void *data, int bytes,
825 int *errorp)
47637bff
MD
826{
827 hammer_record_t record;
828 hammer_record_t conflict;
0832c9bb 829 int zone;
bf3b416b 830 int flags;
47637bff
MD
831
832 /*
7bc5b8c2
MD
833 * Deal with conflicting in-memory records. We cannot have multiple
834 * in-memory records for the same offset without seriously confusing
835 * the backend, including but not limited to the backend issuing
836 * delete-create-delete sequences and asserting on the delete_tid
837 * being the same as the create_tid.
47637bff 838 *
7bc5b8c2
MD
839 * If we encounter a record with the backend interlock set we cannot
840 * immediately delete it without confusing the backend.
47637bff 841 */
0832c9bb 842 while ((conflict = hammer_ip_get_bulk(ip, file_offset, bytes)) !=NULL) {
7bc5b8c2
MD
843 if (conflict->flags & HAMMER_RECF_INTERLOCK_BE) {
844 conflict->flags |= HAMMER_RECF_WANTED;
845 tsleep(conflict, 0, "hmrrc3", 0);
bf3b416b
MD
846 } else {
847 conflict->flags |= HAMMER_RECF_DELETED_FE;
47637bff 848 }
7bc5b8c2 849 hammer_rel_mem_record(conflict);
47637bff
MD
850 }
851
852 /*
0832c9bb
MD
853 * Create a record to cover the direct write. This is called with
854 * the related BIO locked so there should be no possible conflict.
855 *
856 * The backend is responsible for finalizing the space reserved in
857 * this record.
858 *
859 * XXX bytes not aligned, depend on the reservation code to
860 * align the reservation.
47637bff
MD
861 */
862 record = hammer_alloc_mem_record(ip, 0);
0832c9bb
MD
863 zone = (bytes >= HAMMER_BUFSIZE) ? HAMMER_ZONE_LARGE_DATA_INDEX :
864 HAMMER_ZONE_SMALL_DATA_INDEX;
865 record->resv = hammer_blockmap_reserve(ip->hmp, zone, bytes,
866 &record->leaf.data_offset,
867 errorp);
868 if (record->resv == NULL) {
cebe9493 869 kprintf("hammer_ip_add_bulk: reservation failed\n");
47637bff
MD
870 hammer_rel_mem_record(record);
871 return(NULL);
872 }
873 record->type = HAMMER_MEM_RECORD_DATA;
874 record->leaf.base.rec_type = HAMMER_RECTYPE_DATA;
875 record->leaf.base.obj_type = ip->ino_leaf.base.obj_type;
876 record->leaf.base.obj_id = ip->obj_id;
877 record->leaf.base.key = file_offset + bytes;
5a930e66
MD
878 record->leaf.base.localization = ip->obj_localization +
879 HAMMER_LOCALIZE_MISC;
47637bff 880 record->leaf.data_len = bytes;
ddfdf542 881 hammer_crc_set_leaf(data, &record->leaf);
bf3b416b 882 flags = record->flags;
47637bff
MD
883
884 hammer_ref(&record->lock); /* mem_add eats a reference */
0832c9bb 885 *errorp = hammer_mem_add(record);
4a2796f3
MD
886 if (*errorp) {
887 conflict = hammer_ip_get_bulk(ip, file_offset, bytes);
888 kprintf("hammer_ip_add_bulk: error %d conflict %p file_offset %lld bytes %d\n",
889 *errorp, conflict, file_offset, bytes);
890 if (conflict)
891 kprintf("conflict %lld %d\n", conflict->leaf.base.key, conflict->leaf.data_len);
892 if (conflict)
893 hammer_rel_mem_record(conflict);
894 }
cebe9493 895 KKASSERT(*errorp == 0);
4a2796f3
MD
896 conflict = hammer_ip_get_bulk(ip, file_offset, bytes);
897 if (conflict != record) {
898 kprintf("conflict mismatch %p %p %08x\n", conflict, record, record->flags);
899 if (conflict)
900 kprintf("conflict mismatch %lld/%d %lld/%d\n", conflict->leaf.base.key, conflict->leaf.data_len, record->leaf.base.key, record->leaf.data_len);
901 }
902 KKASSERT(conflict == record);
903 hammer_rel_mem_record(conflict);
0832c9bb 904
47637bff
MD
905 return (record);
906}
907
908/*
909 * Frontend truncation code. Scan in-memory records only. On-disk records
910 * and records in a flushing state are handled by the backend. The vnops
911 * setattr code will handle the block containing the truncation point.
912 *
913 * Partial blocks are not deleted.
914 */
47637bff
MD
915int
916hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size)
917{
918 struct rec_trunc_info info;
919
920 switch(ip->ino_data.obj_type) {
921 case HAMMER_OBJTYPE_REGFILE:
922 info.rec_type = HAMMER_RECTYPE_DATA;
923 break;
924 case HAMMER_OBJTYPE_DBFILE:
925 info.rec_type = HAMMER_RECTYPE_DB;
926 break;
927 default:
928 return(EINVAL);
929 }
930 info.trunc_off = file_size;
931 hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_trunc_cmp,
0832c9bb 932 hammer_rec_trunc_callback, &info);
47637bff
MD
933 return(0);
934}
935
0832c9bb
MD
936static int
937hammer_rec_trunc_callback(hammer_record_t record, void *data __unused)
938{
939 if (record->flags & HAMMER_RECF_DELETED_FE)
940 return(0);
941 if (record->flush_state == HAMMER_FST_FLUSH)
942 return(0);
943 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
944 hammer_ref(&record->lock);
945 record->flags |= HAMMER_RECF_DELETED_FE;
946 hammer_rel_mem_record(record);
947 return(0);
948}
949
cb51be26
MD
950/*
951 * Return 1 if the caller must check for and delete existing records
952 * before writing out a new data record.
953 *
954 * Return 0 if the caller can just insert the record into the B-Tree without
955 * checking.
956 */
957static int
958hammer_record_needs_overwrite_delete(hammer_record_t record)
959{
960 hammer_inode_t ip = record->ip;
961 int64_t file_offset;
962 int r;
963
964 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE)
965 file_offset = record->leaf.base.key;
966 else
967 file_offset = record->leaf.base.key - record->leaf.data_len;
968 r = (file_offset < ip->sync_trunc_off);
969 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
970 if (ip->sync_trunc_off <= record->leaf.base.key)
971 ip->sync_trunc_off = record->leaf.base.key + 1;
972 } else {
973 if (ip->sync_trunc_off < record->leaf.base.key)
974 ip->sync_trunc_off = record->leaf.base.key;
975 }
976 return(r);
977}
978
47637bff
MD
979/*
980 * Backend code. Sync a record to the media.
981 */
4e17f465
MD
982int
983hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t record)
984{
985 hammer_transaction_t trans = cursor->trans;
cebe9493 986 int64_t file_offset;
4a2796f3 987 int bytes;
40043e7f 988 void *bdata;
c0ade690
MD
989 int error;
990
1f07f686 991 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
d36ec43b 992 KKASSERT(record->flags & HAMMER_RECF_INTERLOCK_BE);
2f85fa4d 993 KKASSERT(record->leaf.base.localization != 0);
b33e2cc0 994
47637bff
MD
995 /*
996 * If this is a bulk-data record placemarker there may be an existing
997 * record on-disk, indicating a data overwrite. If there is the
998 * on-disk record must be deleted before we can insert our new record.
999 *
1000 * We've synthesized this record and do not know what the create_tid
1001 * on-disk is, nor how much data it represents.
1002 *
1003 * Keep in mind that (key) for data records is (base_offset + len),
1004 * not (base_offset). Also, we only want to get rid of on-disk
1005 * records since we are trying to sync our in-memory record, call
1006 * hammer_ip_delete_range() with truncating set to 1 to make sure
1007 * it skips in-memory records.
1008 *
1009 * It is ok for the lookup to return ENOENT.
cb51be26
MD
1010 *
1011 * NOTE OPTIMIZATION: sync_trunc_off is used to determine if we have
1012 * to call hammer_ip_delete_range() or not. This also means we must
1013 * update sync_trunc_off() as we write.
47637bff 1014 */
cb51be26
MD
1015 if (record->type == HAMMER_MEM_RECORD_DATA &&
1016 hammer_record_needs_overwrite_delete(record)) {
cebe9493 1017 file_offset = record->leaf.base.key - record->leaf.data_len;
4a2796f3
MD
1018 bytes = (record->leaf.data_len + HAMMER_BUFMASK) &
1019 ~HAMMER_BUFMASK;
cebe9493 1020 KKASSERT((file_offset & HAMMER_BUFMASK) == 0);
47637bff
MD
1021 error = hammer_ip_delete_range(
1022 cursor, record->ip,
4a2796f3 1023 file_offset, file_offset + bytes - 1,
cebe9493 1024 1);
47637bff
MD
1025 if (error && error != ENOENT)
1026 goto done;
1027 }
1028
1029 /*
1030 * Setup the cursor.
1031 */
4e17f465 1032 hammer_normalize_cursor(cursor);
11ad5ade 1033 cursor->key_beg = record->leaf.base;
4e17f465
MD
1034 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1035 cursor->flags |= HAMMER_CURSOR_BACKEND;
1036 cursor->flags &= ~HAMMER_CURSOR_INSERT;
c0ade690 1037
98f7132d
MD
1038 /*
1039 * Records can wind up on-media before the inode itself is on-media.
1040 * Flag the case.
1041 */
1042 record->ip->flags |= HAMMER_INODE_DONDISK;
1043
c0ade690 1044 /*
47637bff
MD
1045 * If we are deleting a directory entry an exact match must be
1046 * found on-disk.
b84de5af 1047 */
1f07f686 1048 if (record->type == HAMMER_MEM_RECORD_DEL) {
4e17f465
MD
1049 error = hammer_btree_lookup(cursor);
1050 if (error == 0) {
e63644f0
MD
1051 error = hammer_ip_delete_record(cursor, record->ip,
1052 trans->tid);
4e17f465
MD
1053 if (error == 0) {
1054 record->flags |= HAMMER_RECF_DELETED_FE;
1055 record->flags |= HAMMER_RECF_DELETED_BE;
1056 }
1057 }
b84de5af
MD
1058 goto done;
1059 }
1060
1061 /*
1062 * We are inserting.
1063 *
d26d0ae9 1064 * Issue a lookup to position the cursor and locate the cluster. The
b3deaf57
MD
1065 * target key should not exist. If we are creating a directory entry
1066 * we may have to iterate the low 32 bits of the key to find an unused
1067 * key.
c0ade690 1068 */
4e17f465 1069 cursor->flags |= HAMMER_CURSOR_INSERT;
b84de5af 1070
c82af904
MD
1071 error = hammer_btree_lookup(cursor);
1072 if (hammer_debug_inode)
1073 kprintf("DOINSERT LOOKUP %d\n", error);
1074 if (error == 0) {
1075 kprintf("hammer_ip_sync_record: duplicate rec "
1076 "at (%016llx)\n", record->leaf.base.key);
1077 Debugger("duplicate record1");
1078 error = EIO;
c0ade690 1079 }
47637bff
MD
1080#if 0
1081 if (record->type == HAMMER_MEM_RECORD_DATA)
1082 kprintf("sync_record %016llx ---------------- %016llx %d\n",
1083 record->leaf.base.key - record->leaf.data_len,
1084 record->leaf.data_offset, error);
1085#endif
47637bff 1086
c0ade690
MD
1087 if (error != ENOENT)
1088 goto done;
1089
1090 /*
47197d71
MD
1091 * Allocate the record and data. The result buffers will be
1092 * marked as being modified and further calls to
1093 * hammer_modify_buffer() will result in unneeded UNDO records.
1094 *
40043e7f 1095 * Support zero-fill records (data == NULL and data_len != 0)
c0ade690 1096 */
47637bff
MD
1097 if (record->type == HAMMER_MEM_RECORD_DATA) {
1098 /*
1099 * The data portion of a bulk-data record has already been
1100 * committed to disk, we need only adjust the layer2
1101 * statistics in the same transaction as our B-Tree insert.
1102 */
1103 KKASSERT(record->leaf.data_offset != 0);
4a2796f3
MD
1104 hammer_blockmap_finalize(trans, record->leaf.data_offset,
1105 record->leaf.data_len);
47637bff
MD
1106 error = 0;
1107 } else if (record->data && record->leaf.data_len) {
1108 /*
1109 * Wholely cached record, with data. Allocate the data.
1110 */
11ad5ade 1111 bdata = hammer_alloc_data(trans, record->leaf.data_len,
bf3b416b 1112 record->leaf.base.rec_type,
11ad5ade 1113 &record->leaf.data_offset,
4e17f465 1114 &cursor->data_buffer, &error);
11ad5ade
MD
1115 if (bdata == NULL)
1116 goto done;
ddfdf542 1117 hammer_crc_set_leaf(record->data, &record->leaf);
4e17f465 1118 hammer_modify_buffer(trans, cursor->data_buffer, NULL, 0);
11ad5ade 1119 bcopy(record->data, bdata, record->leaf.data_len);
4e17f465 1120 hammer_modify_buffer_done(cursor->data_buffer);
47197d71 1121 } else {
47637bff
MD
1122 /*
1123 * Wholely cached record, without data.
1124 */
11ad5ade
MD
1125 record->leaf.data_offset = 0;
1126 record->leaf.data_crc = 0;
c0ade690 1127 }
c0ade690 1128
11ad5ade 1129 error = hammer_btree_insert(cursor, &record->leaf);
47637bff
MD
1130 if (hammer_debug_inode && error)
1131 kprintf("BTREE INSERT error %d @ %016llx:%d key %016llx\n", error, cursor->node->node_offset, cursor->index, record->leaf.base.key);
b3deaf57
MD
1132
1133 /*
47637bff
MD
1134 * Our record is on-disk, normally mark the in-memory version as
1135 * deleted. If the record represented a directory deletion but
1136 * we had to sync a valid directory entry to disk we must convert
1137 * the record to a covering delete so the frontend does not have
1138 * visibility on the synced entry.
b3deaf57 1139 */
4e17f465
MD
1140 if (error == 0) {
1141 if (record->flags & HAMMER_RECF_CONVERT_DELETE) {
1142 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1143 record->flags &= ~HAMMER_RECF_DELETED_FE;
1144 record->type = HAMMER_MEM_RECORD_DEL;
c4bae5fd 1145 KKASSERT(record->flush_state == HAMMER_FST_FLUSH);
4e17f465 1146 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
c4bae5fd 1147 /* hammer_flush_record_done takes care of the rest */
4e17f465
MD
1148 } else {
1149 record->flags |= HAMMER_RECF_DELETED_FE;
1150 record->flags |= HAMMER_RECF_DELETED_BE;
1f07f686 1151 }
4e17f465 1152 } else {
11ad5ade
MD
1153 if (record->leaf.data_offset) {
1154 hammer_blockmap_free(trans, record->leaf.data_offset,
1155 record->leaf.data_len);
19619882 1156 }
4e17f465 1157 }
d36ec43b 1158
47197d71 1159done:
c0ade690 1160 return(error);
8cd0a023 1161}
66325755
MD
1162
1163/*
a89aec1b
MD
1164 * Add the record to the inode's rec_tree. The low 32 bits of a directory
1165 * entry's key is used to deal with hash collisions in the upper 32 bits.
1166 * A unique 64 bit key is generated in-memory and may be regenerated a
1167 * second time when the directory record is flushed to the on-disk B-Tree.
d26d0ae9 1168 *
b3deaf57
MD
1169 * A referenced record is passed to this function. This function
1170 * eats the reference. If an error occurs the record will be deleted.
47197d71
MD
1171 *
1172 * A copy of the temporary record->data pointer provided by the caller
1173 * will be made.
66325755 1174 */
8cd0a023 1175static
66325755 1176int
47637bff 1177hammer_mem_add(hammer_record_t record)
66325755 1178{
47637bff
MD
1179 hammer_mount_t hmp = record->ip->hmp;
1180
47197d71
MD
1181 /*
1182 * Make a private copy of record->data
1183 */
11ad5ade
MD
1184 if (record->data)
1185 KKASSERT(record->flags & HAMMER_RECF_ALLOCDATA);
47197d71
MD
1186
1187 /*
c82af904
MD
1188 * Insert into the RB tree. A unique key should have already
1189 * been selected if this is a directory entry.
47197d71 1190 */
c82af904
MD
1191 if (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
1192 record->flags |= HAMMER_RECF_DELETED_FE;
1193 hammer_rel_mem_record(record);
1194 return (EEXIST);
8cd0a023 1195 }
4a2796f3 1196 ++hmp->count_newrecords;
47637bff
MD
1197 ++hmp->rsv_recs;
1198 ++record->ip->rsv_recs;
e63644f0 1199 record->ip->hmp->rsv_databytes += record->leaf.data_len;
8cd0a023 1200 record->flags |= HAMMER_RECF_ONRBTREE;
47637bff 1201 hammer_modify_inode(record->ip, HAMMER_INODE_XDIRTY);
b3deaf57 1202 hammer_rel_mem_record(record);
8cd0a023 1203 return(0);
66325755
MD
1204}
1205
a89aec1b
MD
1206/************************************************************************
1207 * HAMMER INODE MERGED-RECORD FUNCTIONS *
1208 ************************************************************************
1209 *
1210 * These functions augment the B-Tree scanning functions in hammer_btree.c
1211 * by merging in-memory records with on-disk records.
1212 */
1213
1214/*
1215 * Locate a particular record either in-memory or on-disk.
1216 *
1217 * NOTE: This is basically a standalone routine, hammer_ip_next() may
1218 * NOT be called to iterate results.
1219 */
1220int
45a014dc 1221hammer_ip_lookup(hammer_cursor_t cursor)
a89aec1b
MD
1222{
1223 int error;
1224
1225 /*
1226 * If the element is in-memory return it without searching the
1227 * on-disk B-Tree
1228 */
45a014dc
MD
1229 KKASSERT(cursor->ip);
1230 error = hammer_mem_lookup(cursor);
a89aec1b 1231 if (error == 0) {
11ad5ade 1232 cursor->leaf = &cursor->iprec->leaf;
a89aec1b
MD
1233 return(error);
1234 }
1235 if (error != ENOENT)
1236 return(error);
1237
1238 /*
1239 * If the inode has on-disk components search the on-disk B-Tree.
1240 */
45a014dc 1241 if ((cursor->ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) == 0)
a89aec1b
MD
1242 return(error);
1243 error = hammer_btree_lookup(cursor);
1244 if (error == 0)
11ad5ade 1245 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
a89aec1b
MD
1246 return(error);
1247}
1248
1249/*
1250 * Locate the first record within the cursor's key_beg/key_end range,
1251 * restricted to a particular inode. 0 is returned on success, ENOENT
1252 * if no records matched the requested range, or some other error.
1253 *
1254 * When 0 is returned hammer_ip_next() may be used to iterate additional
1255 * records within the requested range.
6a37e7e4
MD
1256 *
1257 * This function can return EDEADLK, requiring the caller to terminate
1258 * the cursor and try again.
a89aec1b
MD
1259 */
1260int
4e17f465 1261hammer_ip_first(hammer_cursor_t cursor)
a89aec1b 1262{
4e17f465 1263 hammer_inode_t ip = cursor->ip;
a89aec1b
MD
1264 int error;
1265
4e17f465
MD
1266 KKASSERT(ip != NULL);
1267
a89aec1b
MD
1268 /*
1269 * Clean up fields and setup for merged scan
1270 */
195c19a1 1271 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
6b4f890b 1272 cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
a89aec1b 1273 cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
b3deaf57
MD
1274 if (cursor->iprec) {
1275 hammer_rel_mem_record(cursor->iprec);
1276 cursor->iprec = NULL;
1277 }
a89aec1b
MD
1278
1279 /*
c0ade690
MD
1280 * Search the on-disk B-Tree. hammer_btree_lookup() only does an
1281 * exact lookup so if we get ENOENT we have to call the iterate
1282 * function to validate the first record after the begin key.
1283 *
1284 * The ATEDISK flag is used by hammer_btree_iterate to determine
d26d0ae9
MD
1285 * whether it must index forwards or not. It is also used here
1286 * to select the next record from in-memory or on-disk.
6a37e7e4
MD
1287 *
1288 * EDEADLK can only occur if the lookup hit an empty internal
1289 * element and couldn't delete it. Since this could only occur
1290 * in-range, we can just iterate from the failure point.
a89aec1b 1291 */
0a72edae 1292 if (ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DONDISK)) {
a89aec1b 1293 error = hammer_btree_lookup(cursor);
6a37e7e4 1294 if (error == ENOENT || error == EDEADLK) {
c0ade690 1295 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
855942b6
MD
1296 if (hammer_debug_general & 0x2000)
1297 kprintf("error %d node %p %016llx index %d\n", error, cursor->node, cursor->node->node_offset, cursor->index);
c0ade690
MD
1298 error = hammer_btree_iterate(cursor);
1299 }
1300 if (error && error != ENOENT)
a89aec1b 1301 return(error);
6b4f890b 1302 if (error == 0) {
c0ade690
MD
1303 cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
1304 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1305 } else {
1306 cursor->flags |= HAMMER_CURSOR_ATEDISK;
6b4f890b 1307 }
a89aec1b
MD
1308 }
1309
1310 /*
c0ade690 1311 * Search the in-memory record list (Red-Black tree). Unlike the
7f7c1f84 1312 * B-Tree search, mem_first checks for records in the range.
a89aec1b 1313 */
4e17f465 1314 error = hammer_mem_first(cursor);
a89aec1b
MD
1315 if (error && error != ENOENT)
1316 return(error);
6b4f890b 1317 if (error == 0) {
a89aec1b 1318 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
6b4f890b 1319 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
b84de5af
MD
1320 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0)
1321 cursor->flags |= HAMMER_CURSOR_ATEMEM;
6b4f890b 1322 }
a89aec1b
MD
1323
1324 /*
1325 * This will return the first matching record.
1326 */
1327 return(hammer_ip_next(cursor));
1328}
1329
1330/*
1331 * Retrieve the next record in a merged iteration within the bounds of the
1332 * cursor. This call may be made multiple times after the cursor has been
1333 * initially searched with hammer_ip_first().
1334 *
1335 * 0 is returned on success, ENOENT if no further records match the
1336 * requested range, or some other error code is returned.
1337 */
1338int
1339hammer_ip_next(hammer_cursor_t cursor)
1340{
1341 hammer_btree_elm_t elm;
b84de5af 1342 hammer_record_t rec, save;
a89aec1b
MD
1343 int error;
1344 int r;
1345
b84de5af 1346next_btree:
a89aec1b
MD
1347 /*
1348 * Load the current on-disk and in-memory record. If we ate any
1349 * records we have to get the next one.
1350 *
195c19a1
MD
1351 * If we deleted the last on-disk record we had scanned ATEDISK will
1352 * be clear and DELBTREE will be set, forcing a call to iterate. The
1353 * fact that ATEDISK is clear causes iterate to re-test the 'current'
1354 * element. If ATEDISK is set, iterate will skip the 'current'
1355 * element.
1356 *
a89aec1b
MD
1357 * Get the next on-disk record
1358 */
195c19a1 1359 if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
a89aec1b
MD
1360 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1361 error = hammer_btree_iterate(cursor);
b3deaf57 1362 cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
cb51be26 1363 if (error == 0) {
a89aec1b 1364 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
bcac4bbb
MD
1365 hammer_cache_node(&cursor->ip->cache[1],
1366 cursor->node);
cb51be26 1367 } else {
195c19a1
MD
1368 cursor->flags |= HAMMER_CURSOR_DISKEOF |
1369 HAMMER_CURSOR_ATEDISK;
cb51be26 1370 }
a89aec1b
MD
1371 }
1372 }
1373
b84de5af 1374next_memory:
a89aec1b 1375 /*
6b4f890b
MD
1376 * Get the next in-memory record. The record can be ripped out
1377 * of the RB tree so we maintain a scan_info structure to track
1378 * the next node.
7f7c1f84
MD
1379 *
1380 * hammer_rec_scan_cmp: Is the record still in our general range,
1381 * (non-inclusive of snapshot exclusions)?
1382 * hammer_rec_scan_callback: Is the record in our snapshot?
a89aec1b
MD
1383 */
1384 if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
1385 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
b84de5af
MD
1386 save = cursor->iprec;
1387 cursor->iprec = NULL;
1388 rec = save ? hammer_rec_rb_tree_RB_NEXT(save) : NULL;
7f7c1f84 1389 while (rec) {
ec4e8497
MD
1390 if (hammer_rec_scan_cmp(rec, cursor) != 0)
1391 break;
1392 if (hammer_rec_scan_callback(rec, cursor) != 0)
1393 break;
7f7c1f84
MD
1394 rec = hammer_rec_rb_tree_RB_NEXT(rec);
1395 }
b84de5af
MD
1396 if (save)
1397 hammer_rel_mem_record(save);
7f7c1f84 1398 if (cursor->iprec) {
b3deaf57 1399 KKASSERT(cursor->iprec == rec);
a89aec1b 1400 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
a89aec1b
MD
1401 } else {
1402 cursor->flags |= HAMMER_CURSOR_MEMEOF;
1403 }
a89aec1b
MD
1404 }
1405 }
1406
4e17f465
MD
1407 /*
1408 * The memory record may have become stale while being held in
1409 * cursor->iprec. We are interlocked against the backend on
1410 * with regards to B-Tree entries.
1411 */
1412 if ((cursor->flags & HAMMER_CURSOR_ATEMEM) == 0) {
1413 if (hammer_ip_iterate_mem_good(cursor, cursor->iprec) == 0) {
1414 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1415 goto next_memory;
1416 }
1417 }
1418
a89aec1b
MD
1419 /*
1420 * Extract either the disk or memory record depending on their
1421 * relative position.
1422 */
1423 error = 0;
6b4f890b 1424 switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
a89aec1b
MD
1425 case 0:
1426 /*
9f5097dc
MD
1427 * Both entries valid. Compare the entries and nominally
1428 * return the first one in the sort order. Numerous cases
1429 * require special attention, however.
a89aec1b
MD
1430 */
1431 elm = &cursor->node->ondisk->elms[cursor->index];
11ad5ade 1432 r = hammer_btree_cmp(&elm->base, &cursor->iprec->leaf.base);
47637bff
MD
1433
1434 /*
9f5097dc
MD
1435 * If the two entries differ only by their key (-2/2) or
1436 * create_tid (-1/1), and are DATA records, we may have a
1437 * nominal match. We have to calculate the base file
1438 * offset of the data.
47637bff 1439 */
9f5097dc
MD
1440 if (r <= 2 && r >= -2 && r != 0 &&
1441 cursor->ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE &&
1442 cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1443 int64_t base1 = elm->leaf.base.key - elm->leaf.data_len;
1444 int64_t base2 = cursor->iprec->leaf.base.key -
1445 cursor->iprec->leaf.data_len;
bf3b416b 1446 if (base1 == base2)
9f5097dc 1447 r = 0;
9f5097dc
MD
1448 }
1449
a89aec1b
MD
1450 if (r < 0) {
1451 error = hammer_btree_extract(cursor,
11ad5ade 1452 HAMMER_CURSOR_GET_LEAF);
a89aec1b
MD
1453 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1454 break;
1455 }
b84de5af
MD
1456
1457 /*
47637bff
MD
1458 * If the entries match exactly the memory entry is either
1459 * an on-disk directory entry deletion or a bulk data
1460 * overwrite. If it is a directory entry deletion we eat
1461 * both entries.
1462 *
1463 * For the bulk-data overwrite case it is possible to have
1464 * visibility into both, which simply means the syncer
1465 * hasn't gotten around to doing the delete+insert sequence
1466 * on the B-Tree. Use the memory entry and throw away the
1467 * on-disk entry.
1f07f686 1468 *
47637bff 1469 * If the in-memory record is not either of these we
1f07f686
MD
1470 * probably caught the syncer while it was syncing it to
1471 * the media. Since we hold a shared lock on the cursor,
1472 * the in-memory record had better be marked deleted at
1473 * this point.
b84de5af
MD
1474 */
1475 if (r == 0) {
1f07f686
MD
1476 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL) {
1477 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1478 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1479 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1480 goto next_btree;
1481 }
47637bff
MD
1482 } else if (cursor->iprec->type == HAMMER_MEM_RECORD_DATA) {
1483 if ((cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
1484 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1485 }
1486 /* fall through to memory entry */
1f07f686 1487 } else {
4e17f465 1488 panic("hammer_ip_next: duplicate mem/b-tree entry");
b84de5af 1489 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1f07f686 1490 goto next_memory;
b84de5af
MD
1491 }
1492 }
a89aec1b 1493 /* fall through to the memory entry */
6b4f890b 1494 case HAMMER_CURSOR_ATEDISK:
a89aec1b 1495 /*
4e17f465 1496 * Only the memory entry is valid.
a89aec1b 1497 */
11ad5ade 1498 cursor->leaf = &cursor->iprec->leaf;
a89aec1b 1499 cursor->flags |= HAMMER_CURSOR_ATEMEM;
4e17f465
MD
1500
1501 /*
1502 * If the memory entry is an on-disk deletion we should have
1503 * also had found a B-Tree record. If the backend beat us
1504 * to it it would have interlocked the cursor and we should
1505 * have seen the in-memory record marked DELETED_FE.
1506 */
98f7132d
MD
1507 if (cursor->iprec->type == HAMMER_MEM_RECORD_DEL &&
1508 (cursor->flags & HAMMER_CURSOR_DELETE_VISIBILITY) == 0) {
4e17f465 1509 panic("hammer_ip_next: del-on-disk with no b-tree entry");
b84de5af 1510 }
a89aec1b 1511 break;
6b4f890b 1512 case HAMMER_CURSOR_ATEMEM:
a89aec1b
MD
1513 /*
1514 * Only the disk entry is valid
1515 */
11ad5ade 1516 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
a89aec1b
MD
1517 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1518 break;
1519 default:
1520 /*
1521 * Neither entry is valid
1522 *
1523 * XXX error not set properly
1524 */
11ad5ade 1525 cursor->leaf = NULL;
a89aec1b
MD
1526 error = ENOENT;
1527 break;
1528 }
1529 return(error);
1530}
1531
1532/*
40043e7f 1533 * Resolve the cursor->data pointer for the current cursor position in
a89aec1b
MD
1534 * a merged iteration.
1535 */
1536int
1537hammer_ip_resolve_data(hammer_cursor_t cursor)
1538{
47637bff 1539 hammer_record_t record;
a89aec1b
MD
1540 int error;
1541
47637bff
MD
1542 if (hammer_cursor_inmem(cursor)) {
1543 /*
1544 * The data associated with an in-memory record is usually
1545 * kmalloced, but reserve-ahead data records will have an
1546 * on-disk reference.
1547 *
1548 * NOTE: Reserve-ahead data records must be handled in the
1549 * context of the related high level buffer cache buffer
1550 * to interlock against async writes.
1551 */
1552 record = cursor->iprec;
1553 cursor->data = record->data;
a89aec1b 1554 error = 0;
47637bff
MD
1555 if (cursor->data == NULL) {
1556 KKASSERT(record->leaf.base.rec_type ==
1557 HAMMER_RECTYPE_DATA);
4a2796f3 1558 cursor->data = hammer_bread_ext(cursor->trans->hmp,
47637bff 1559 record->leaf.data_offset,
4a2796f3 1560 record->leaf.data_len,
47637bff
MD
1561 &error,
1562 &cursor->data_buffer);
1563 }
a89aec1b 1564 } else {
11ad5ade 1565 cursor->leaf = &cursor->node->ondisk->elms[cursor->index].leaf;
a89aec1b
MD
1566 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1567 }
1568 return(error);
1569}
1570
66325755 1571/*
47637bff
MD
1572 * Backend truncation / record replacement - delete records in range.
1573 *
1574 * Delete all records within the specified range for inode ip. In-memory
1575 * records still associated with the frontend are ignored.
a89aec1b
MD
1576 *
1577 * NOTE: An unaligned range will cause new records to be added to cover
76376933 1578 * the edge cases. (XXX not implemented yet).
a89aec1b 1579 *
47637bff
MD
1580 * NOTE: Replacement via reservations (see hammer_ip_sync_record_cursor())
1581 * also do not deal with unaligned ranges.
1582 *
a89aec1b 1583 * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
76376933
MD
1584 *
1585 * NOTE: Record keys for regular file data have to be special-cased since
1586 * they indicate the end of the range (key = base + bytes).
66325755
MD
1587 */
1588int
4e17f465 1589hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
47637bff 1590 int64_t ran_beg, int64_t ran_end, int truncating)
66325755 1591{
4e17f465 1592 hammer_transaction_t trans = cursor->trans;
11ad5ade 1593 hammer_btree_leaf_elm_t leaf;
a89aec1b
MD
1594 int error;
1595 int64_t off;
1596
b84de5af
MD
1597#if 0
1598 kprintf("delete_range %p %016llx-%016llx\n", ip, ran_beg, ran_end);
1599#endif
1600
1601 KKASSERT(trans->type == HAMMER_TRANS_FLS);
6a37e7e4 1602retry:
4e17f465 1603 hammer_normalize_cursor(cursor);
5a930e66
MD
1604 cursor->key_beg.localization = ip->obj_localization +
1605 HAMMER_LOCALIZE_MISC;
4e17f465
MD
1606 cursor->key_beg.obj_id = ip->obj_id;
1607 cursor->key_beg.create_tid = 0;
1608 cursor->key_beg.delete_tid = 0;
1609 cursor->key_beg.obj_type = 0;
1610 cursor->asof = ip->obj_asof;
1611 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1612 cursor->flags |= HAMMER_CURSOR_ASOF;
1613 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1614 cursor->flags |= HAMMER_CURSOR_BACKEND;
1615
1616 cursor->key_end = cursor->key_beg;
11ad5ade 1617 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
4e17f465
MD
1618 cursor->key_beg.key = ran_beg;
1619 cursor->key_beg.rec_type = HAMMER_RECTYPE_DB;
1620 cursor->key_end.rec_type = HAMMER_RECTYPE_DB;
1621 cursor->key_end.key = ran_end;
a89aec1b 1622 } else {
195c19a1
MD
1623 /*
1624 * The key in the B-Tree is (base+bytes), so the first possible
1625 * matching key is ran_beg + 1.
1626 */
1627 int64_t tmp64;
1628
4e17f465
MD
1629 cursor->key_beg.key = ran_beg + 1;
1630 cursor->key_beg.rec_type = HAMMER_RECTYPE_DATA;
1631 cursor->key_end.rec_type = HAMMER_RECTYPE_DATA;
195c19a1
MD
1632
1633 tmp64 = ran_end + MAXPHYS + 1; /* work around GCC-4 bug */
1634 if (tmp64 < ran_end)
4e17f465 1635 cursor->key_end.key = 0x7FFFFFFFFFFFFFFFLL;
a89aec1b 1636 else
4e17f465 1637 cursor->key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 1638 }
4e17f465 1639 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE;
a89aec1b 1640
4e17f465 1641 error = hammer_ip_first(cursor);
a89aec1b
MD
1642
1643 /*
1644 * Iterate through matching records and mark them as deleted.
1645 */
1646 while (error == 0) {
11ad5ade 1647 leaf = cursor->leaf;
a89aec1b 1648
11ad5ade 1649 KKASSERT(leaf->base.delete_tid == 0);
a89aec1b
MD
1650
1651 /*
1652 * There may be overlap cases for regular file data. Also
47637bff
MD
1653 * remember the key for a regular file record is (base + len),
1654 * NOT (base).
a89aec1b 1655 */
11ad5ade 1656 if (leaf->base.rec_type == HAMMER_RECTYPE_DATA) {
11ad5ade 1657 off = leaf->base.key - leaf->data_len;
a89aec1b 1658 /*
76376933
MD
1659 * Check the left edge case. We currently do not
1660 * split existing records.
a89aec1b
MD
1661 */
1662 if (off < ran_beg) {
76376933 1663 panic("hammer left edge case %016llx %d\n",
11ad5ade 1664 leaf->base.key, leaf->data_len);
a89aec1b
MD
1665 }
1666
1667 /*
1668 * Check the right edge case. Note that the
1669 * record can be completely out of bounds, which
1670 * terminates the search.
1671 *
76376933
MD
1672 * base->key is exclusive of the right edge while
1673 * ran_end is inclusive of the right edge. The
1674 * (key - data_len) left boundary is inclusive.
195c19a1
MD
1675 *
1676 * XXX theory-check this test at some point, are
1677 * we missing a + 1 somewhere? Note that ran_end
1678 * could overflow.
a89aec1b 1679 */
11ad5ade
MD
1680 if (leaf->base.key - 1 > ran_end) {
1681 if (leaf->base.key - leaf->data_len > ran_end)
a89aec1b 1682 break;
a89aec1b
MD
1683 panic("hammer right edge case\n");
1684 }
1685 }
1686
1687 /*
47637bff
MD
1688 * Delete the record. When truncating we do not delete
1689 * in-memory (data) records because they represent data
1690 * written after the truncation.
1691 *
1692 * This will also physically destroy the B-Tree entry and
195c19a1
MD
1693 * data if the retention policy dictates. The function
1694 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1695 * uses to perform a fixup.
a89aec1b 1696 */
47637bff
MD
1697 if (truncating == 0 || hammer_cursor_ondisk(cursor))
1698 error = hammer_ip_delete_record(cursor, ip, trans->tid);
195c19a1
MD
1699 if (error)
1700 break;
4e17f465
MD
1701 error = hammer_ip_next(cursor);
1702 }
cb51be26 1703 if (cursor->node)
bcac4bbb 1704 hammer_cache_node(&ip->cache[1], cursor->node);
cb51be26 1705
4e17f465
MD
1706 if (error == EDEADLK) {
1707 hammer_done_cursor(cursor);
bcac4bbb 1708 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
4e17f465
MD
1709 if (error == 0)
1710 goto retry;
a89aec1b 1711 }
a89aec1b
MD
1712 if (error == ENOENT)
1713 error = 0;
1714 return(error);
66325755
MD
1715}
1716
b3deaf57 1717/*
47637bff
MD
1718 * Backend truncation - delete all records.
1719 *
1f07f686
MD
1720 * Delete all user records associated with an inode except the inode record
1721 * itself. Directory entries are not deleted (they must be properly disposed
1722 * of or nlinks would get upset).
b3deaf57 1723 */
7a04d74f 1724int
4e17f465 1725hammer_ip_delete_range_all(hammer_cursor_t cursor, hammer_inode_t ip,
869e8f55 1726 int *countp)
7a04d74f 1727{
4e17f465 1728 hammer_transaction_t trans = cursor->trans;
11ad5ade 1729 hammer_btree_leaf_elm_t leaf;
7a04d74f
MD
1730 int error;
1731
b84de5af 1732 KKASSERT(trans->type == HAMMER_TRANS_FLS);
6a37e7e4 1733retry:
4e17f465 1734 hammer_normalize_cursor(cursor);
5a930e66
MD
1735 cursor->key_beg.localization = ip->obj_localization +
1736 HAMMER_LOCALIZE_MISC;
4e17f465
MD
1737 cursor->key_beg.obj_id = ip->obj_id;
1738 cursor->key_beg.create_tid = 0;
1739 cursor->key_beg.delete_tid = 0;
1740 cursor->key_beg.obj_type = 0;
1741 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1742 cursor->key_beg.key = HAMMER_MIN_KEY;
1743
1744 cursor->key_end = cursor->key_beg;
1745 cursor->key_end.rec_type = 0xFFFF;
1746 cursor->key_end.key = HAMMER_MAX_KEY;
1747
1748 cursor->asof = ip->obj_asof;
1749 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1750 cursor->flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1751 cursor->flags |= HAMMER_CURSOR_DELETE_VISIBILITY;
1752 cursor->flags |= HAMMER_CURSOR_BACKEND;
1753
1754 error = hammer_ip_first(cursor);
7a04d74f
MD
1755
1756 /*
1757 * Iterate through matching records and mark them as deleted.
1758 */
1759 while (error == 0) {
11ad5ade 1760 leaf = cursor->leaf;
7a04d74f 1761
11ad5ade 1762 KKASSERT(leaf->base.delete_tid == 0);
7a04d74f
MD
1763
1764 /*
1765 * Mark the record and B-Tree entry as deleted. This will
1766 * also physically delete the B-Tree entry, record, and
1767 * data if the retention policy dictates. The function
1768 * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1769 * uses to perform a fixup.
1f07f686
MD
1770 *
1771 * Directory entries (and delete-on-disk directory entries)
1772 * must be synced and cannot be deleted.
7a04d74f 1773 */
11ad5ade 1774 if (leaf->base.rec_type != HAMMER_RECTYPE_DIRENTRY) {
e63644f0 1775 error = hammer_ip_delete_record(cursor, ip, trans->tid);
869e8f55
MD
1776 ++*countp;
1777 }
7a04d74f
MD
1778 if (error)
1779 break;
4e17f465
MD
1780 error = hammer_ip_next(cursor);
1781 }
cb51be26 1782 if (cursor->node)
bcac4bbb 1783 hammer_cache_node(&ip->cache[1], cursor->node);
4e17f465
MD
1784 if (error == EDEADLK) {
1785 hammer_done_cursor(cursor);
bcac4bbb 1786 error = hammer_init_cursor(trans, cursor, &ip->cache[1], ip);
4e17f465
MD
1787 if (error == 0)
1788 goto retry;
7a04d74f 1789 }
7a04d74f
MD
1790 if (error == ENOENT)
1791 error = 0;
1792 return(error);
1793}
1794
195c19a1 1795/*
46fe7ae1
MD
1796 * Delete the record at the current cursor. On success the cursor will
1797 * be positioned appropriately for an iteration but may no longer be at
1798 * a leaf node.
6a37e7e4 1799 *
b84de5af
MD
1800 * This routine is only called from the backend.
1801 *
6a37e7e4
MD
1802 * NOTE: This can return EDEADLK, requiring the caller to terminate the
1803 * cursor and retry.
195c19a1
MD
1804 */
1805int
e63644f0
MD
1806hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
1807 hammer_tid_t tid)
195c19a1 1808{
4a2796f3
MD
1809 hammer_off_t zone2_offset;
1810 hammer_record_t iprec;
195c19a1
MD
1811 hammer_btree_elm_t elm;
1812 hammer_mount_t hmp;
1813 int error;
32c90105 1814 int dodelete;
195c19a1 1815
d36ec43b 1816 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
cebe9493 1817 KKASSERT(tid != 0);
4a2796f3 1818 hmp = cursor->node->hmp;
d36ec43b 1819
195c19a1 1820 /*
d36ec43b
MD
1821 * In-memory (unsynchronized) records can simply be freed. This
1822 * only occurs in range iterations since all other records are
1823 * individually synchronized. Thus there should be no confusion with
1824 * the interlock.
4a2796f3
MD
1825 *
1826 * An in-memory record may be deleted before being committed to disk,
1827 * but could have been accessed in the mean time. The backing store
1828 * may never been marked allocated and so hammer_blockmap_free() may
1829 * never get called on it. Because of this we have to make sure that
1830 * we've gotten rid of any related hammer_buffer or buffer cache
1831 * buffer.
195c19a1 1832 */
47637bff 1833 if (hammer_cursor_inmem(cursor)) {
4a2796f3
MD
1834 iprec = cursor->iprec;
1835 KKASSERT((iprec->flags & HAMMER_RECF_INTERLOCK_BE) ==0);
1836 iprec->flags |= HAMMER_RECF_DELETED_FE;
1837 iprec->flags |= HAMMER_RECF_DELETED_BE;
1838
1839 if (iprec->leaf.data_offset && iprec->leaf.data_len) {
1840 zone2_offset = hammer_blockmap_lookup(hmp, iprec->leaf.data_offset, &error);
1841 KKASSERT(error == 0);
1842 hammer_del_buffers(hmp,
1843 iprec->leaf.data_offset,
1844 zone2_offset,
1845 iprec->leaf.data_len);
1846 }
195c19a1
MD
1847 return(0);
1848 }
1849
1850 /*
1851 * On-disk records are marked as deleted by updating their delete_tid.
9582c7da
MD
1852 * This does not effect their position in the B-Tree (which is based
1853 * on their create_tid).
195c19a1 1854 */
11ad5ade 1855 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_LEAF);
195c19a1 1856 elm = NULL;
195c19a1 1857
19619882
MD
1858 /*
1859 * If we were mounted with the nohistory option, we physically
1860 * delete the record.
1861 */
e63644f0 1862 dodelete = hammer_nohistory(ip);
19619882 1863
195c19a1 1864 if (error == 0) {
6a37e7e4
MD
1865 error = hammer_cursor_upgrade(cursor);
1866 if (error == 0) {
6a37e7e4 1867 elm = &cursor->node->ondisk->elms[cursor->index];
36f82b23 1868 hammer_modify_node(cursor->trans, cursor->node,
dd94f1b1 1869 elm, sizeof(*elm));
6a37e7e4 1870 elm->leaf.base.delete_tid = tid;
dd94f1b1 1871 elm->leaf.delete_ts = cursor->trans->time32;
10a5d1ba 1872 hammer_modify_node_done(cursor->node);
855942b6
MD
1873
1874 /*
1875 * An on-disk record cannot have the same delete_tid
1876 * as its create_tid. In a chain of record updates
1877 * this could result in a duplicate record.
1878 */
1879 KKASSERT(elm->leaf.base.delete_tid != elm->leaf.base.create_tid);
6a37e7e4 1880 }
195c19a1
MD
1881 }
1882
32c90105 1883 if (error == 0 && dodelete) {
7dc57964 1884 error = hammer_delete_at_cursor(cursor, NULL);
195c19a1 1885 if (error) {
b3deaf57 1886 panic("hammer_ip_delete_record: unable to physically delete the record!\n");
195c19a1
MD
1887 error = 0;
1888 }
1889 }
1890 return(error);
1891}
1892
7dc57964
MD
1893int
1894hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes)
1895{
1896 hammer_btree_elm_t elm;
47197d71 1897 hammer_off_t data_offset;
7dc57964 1898 int32_t data_len;
bf686dbe 1899 u_int16_t rec_type;
7dc57964
MD
1900 int error;
1901
1902 elm = &cursor->node->ondisk->elms[cursor->index];
1903 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
1904
7dc57964
MD
1905 data_offset = elm->leaf.data_offset;
1906 data_len = elm->leaf.data_len;
1907 rec_type = elm->leaf.base.rec_type;
1908
7dc57964
MD
1909 error = hammer_btree_delete(cursor);
1910 if (error == 0) {
1911 /*
1912 * This forces a fixup for the iteration because
1913 * the cursor is now either sitting at the 'next'
1914 * element or sitting at the end of a leaf.
1915 */
1916 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1917 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1918 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1919 }
40043e7f 1920 }
eb3f8f1f
MD
1921 if (error == 0) {
1922 switch(data_offset & HAMMER_OFF_ZONE_MASK) {
1923 case HAMMER_ZONE_LARGE_DATA:
1924 case HAMMER_ZONE_SMALL_DATA:
bf3b416b 1925 case HAMMER_ZONE_META:
36f82b23 1926 hammer_blockmap_free(cursor->trans,
eb3f8f1f
MD
1927 data_offset, data_len);
1928 break;
1929 default:
1930 break;
1931 }
7dc57964 1932 }
7dc57964
MD
1933 return (error);
1934}
1935
b3deaf57 1936/*
1f07f686
MD
1937 * Determine whether we can remove a directory. This routine checks whether
1938 * a directory is empty or not and enforces flush connectivity.
1939 *
1940 * Flush connectivity requires that we block if the target directory is
1941 * currently flushing, otherwise it may not end up in the same flush group.
1942 *
1943 * Returns 0 on success, ENOTEMPTY or EDEADLK (or other errors) on failure.
b3deaf57
MD
1944 */
1945int
98f7132d 1946hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
b3deaf57
MD
1947{
1948 struct hammer_cursor cursor;
1949 int error;
1950
1f07f686
MD
1951 /*
1952 * Check directory empty
1953 */
bcac4bbb 1954 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
b3deaf57 1955
5a930e66
MD
1956 cursor.key_beg.localization = ip->obj_localization +
1957 HAMMER_LOCALIZE_MISC;
b3deaf57 1958 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1959 cursor.key_beg.create_tid = 0;
b3deaf57
MD
1960 cursor.key_beg.delete_tid = 0;
1961 cursor.key_beg.obj_type = 0;
1962 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1963 cursor.key_beg.key = HAMMER_MIN_KEY;
1964
1965 cursor.key_end = cursor.key_beg;
1966 cursor.key_end.rec_type = 0xFFFF;
1967 cursor.key_end.key = HAMMER_MAX_KEY;
1968
d5530d22
MD
1969 cursor.asof = ip->obj_asof;
1970 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
b3deaf57 1971
4e17f465 1972 error = hammer_ip_first(&cursor);
b3deaf57
MD
1973 if (error == ENOENT)
1974 error = 0;
1975 else if (error == 0)
1976 error = ENOTEMPTY;
1977 hammer_done_cursor(&cursor);
1978 return(error);
1979}
1980