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