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