HAMMER 13/many - Stabilization commit
[dragonfly.git] / sys / vfs / hammer / hammer_object.c
1 /*
2  * Copyright (c) 2007 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.13 2007/12/31 05:33:12 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static int hammer_mem_add(hammer_transaction_t trans,
40                              hammer_record_t record);
41 static int hammer_mem_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
42 static int hammer_mem_first(hammer_cursor_t cursor, hammer_inode_t ip);
43
44 /*
45  * Red-black tree support.
46  */
47 static int
48 hammer_rec_rb_compare(hammer_record_t rec1, hammer_record_t rec2)
49 {
50         if (rec1->rec.base.base.rec_type < rec2->rec.base.base.rec_type)
51                 return(-1);
52         if (rec1->rec.base.base.rec_type > rec2->rec.base.base.rec_type)
53                 return(1);
54
55         if (rec1->rec.base.base.key < rec2->rec.base.base.key)
56                 return(-1);
57         if (rec1->rec.base.base.key > rec2->rec.base.base.key)
58                 return(1);
59
60         if (rec1->rec.base.base.create_tid < rec2->rec.base.base.create_tid)
61                 return(-1);
62         if (rec1->rec.base.base.create_tid > rec2->rec.base.base.create_tid)
63                 return(1);
64         return(0);
65 }
66
67 static int
68 hammer_rec_compare(hammer_base_elm_t info, hammer_record_t rec)
69 {
70         if (info->rec_type < rec->rec.base.base.rec_type)
71                 return(-3);
72         if (info->rec_type > rec->rec.base.base.rec_type)
73                 return(3);
74
75         if (info->key < rec->rec.base.base.key)
76                 return(-2);
77         if (info->key > rec->rec.base.base.key)
78                 return(2);
79
80         /*
81          * This test has a number of special cases.  create_tid in key1 is
82          * the as-of transction id, and delete_tid in key1 is NOT USED.
83          *
84          * A key1->create_tid of 0 matches any record regardles of when
85          * it was created or destroyed.  0xFFFFFFFFFFFFFFFFULL should be
86          * used to search for the most current state of the object.
87          *
88          * key2->create_tid is a HAMMER record and will never be
89          * 0.   key2->delete_tid is the deletion transaction id or 0 if
90          * the record has not yet been deleted.
91          */
92         if (info->create_tid) {
93                 if (info->create_tid < rec->rec.base.base.create_tid)
94                         return(-1);
95                 if (rec->rec.base.base.delete_tid &&
96                     info->create_tid >= rec->rec.base.base.delete_tid) {
97                         return(1);
98                 }
99         }
100         return(0);
101 }
102
103 /*
104  * RB_SCAN comparison code for hammer_mem_first().  The argument order
105  * is reversed so the comparison result has to be negated.  key_beg and
106  * key_end are both range-inclusive.
107  *
108  * The creation timestamp can cause hammer_rec_compare() to return -1 or +1.
109  * These do not stop the scan.
110  *
111  * Localized deletions are not cached in-memory.
112  */
113 static
114 int
115 hammer_rec_scan_cmp(hammer_record_t rec, void *data)
116 {
117         hammer_cursor_t cursor = data;
118         int r;
119
120         r = hammer_rec_compare(&cursor->key_beg, rec);
121         if (r > 1)
122                 return(-1);
123         if (r == 0)
124                 return(0);
125         r = hammer_rec_compare(&cursor->key_end, rec);
126         if (r < -1)
127                 return(1);
128         return(0);
129 }
130
131 RB_GENERATE(hammer_rec_rb_tree, hammer_record, rb_node, hammer_rec_rb_compare);
132 RB_GENERATE_XLOOKUP(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
133                     hammer_rec_compare, hammer_base_elm_t);
134
135 /*
136  * Allocate a record for the caller to finish filling in.  The record is
137  * returned referenced.
138  */
139 hammer_record_t
140 hammer_alloc_mem_record(hammer_inode_t ip)
141 {
142         hammer_record_t record;
143
144         ++hammer_count_records;
145         record = kmalloc(sizeof(*record), M_HAMMER, M_WAITOK|M_ZERO);
146         record->ip = ip;
147         hammer_ref(&record->lock);
148         return (record);
149 }
150
151 /*
152  * Release a memory record.  Records marked for deletion are immediately
153  * removed from the RB-Tree but otherwise left intact until the last ref
154  * goes away.
155  */
156 void
157 hammer_rel_mem_record(struct hammer_record *record)
158 {
159         hammer_unref(&record->lock);
160         if (record->flags & HAMMER_RECF_DELETED) {
161                 if (record->flags & HAMMER_RECF_ONRBTREE) {
162                         RB_REMOVE(hammer_rec_rb_tree, &record->ip->rec_tree,
163                                   record);
164                         record->flags &= ~HAMMER_RECF_ONRBTREE;
165                 }
166                 if (record->lock.refs == 0) {
167                         if (record->flags & HAMMER_RECF_ALLOCDATA) {
168                                 --hammer_count_record_datas;
169                                 kfree(record->data, M_HAMMER);
170                                 record->flags &= ~HAMMER_RECF_ALLOCDATA;
171                         }
172                         record->data = NULL;
173                         --hammer_count_records;
174                         kfree(record, M_HAMMER);
175                 }
176         }
177 }
178
179 /*
180  * Lookup an in-memory record given the key specified in the cursor.  Works
181  * just like hammer_btree_lookup() but operates on an inode's in-memory
182  * record list.
183  *
184  * The lookup must fail if the record is marked for deferred deletion.
185  */
186 static
187 int
188 hammer_mem_lookup(hammer_cursor_t cursor, hammer_inode_t ip)
189 {
190         int error;
191
192         if (cursor->iprec) {
193                 hammer_rel_mem_record(cursor->iprec);
194                 cursor->iprec = NULL;
195         }
196         if (cursor->ip) {
197                 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
198                                                   &cursor->ip->rec_tree);
199         }
200         cursor->ip = ip;
201         hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
202         cursor->scan.node = NULL;
203         cursor->iprec = hammer_rec_rb_tree_RB_LOOKUP_INFO(
204                                 &ip->rec_tree, &cursor->key_beg);
205         if (cursor->iprec == NULL) {
206                 error = ENOENT;
207         } else {
208                 hammer_ref(&cursor->iprec->lock);
209                 error = 0;
210         }
211         return(error);
212 }
213
214 /*
215  * hammer_mem_first() - locate the first in-memory record matching the
216  * cursor.
217  *
218  * The RB_SCAN function we use is designed as a callback.  We terminate it
219  * (return -1) as soon as we get a match.
220  */
221 static
222 int
223 hammer_rec_scan_callback(hammer_record_t rec, void *data)
224 {
225         hammer_cursor_t cursor = data;
226
227         /*
228          * Skip if not visible due to our as-of TID
229          */
230         if (cursor->key_beg.create_tid) {
231                 if (cursor->key_beg.create_tid < rec->rec.base.base.create_tid)
232                         return(0);
233                 if (rec->rec.base.base.delete_tid &&
234                     cursor->key_beg.create_tid >=
235                      rec->rec.base.base.delete_tid) {
236                         return(0);
237                 }
238         }
239
240         /*
241          * Return the first matching record and stop the scan
242          */
243         if (cursor->iprec == NULL) {
244                 cursor->iprec = rec;
245                 hammer_ref(&rec->lock);
246                 return(-1);
247         }
248         return(0);
249 }
250
251 static
252 int
253 hammer_mem_first(hammer_cursor_t cursor, hammer_inode_t ip)
254 {
255         if (cursor->iprec) {
256                 hammer_rel_mem_record(cursor->iprec);
257                 cursor->iprec = NULL;
258         }
259         if (cursor->ip) {
260                 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
261                                                   &cursor->ip->rec_tree);
262         }
263         cursor->ip = ip;
264         hammer_rec_rb_tree_scan_info_link(&cursor->scan, &ip->rec_tree);
265
266         cursor->scan.node = NULL;
267         hammer_rec_rb_tree_RB_SCAN(&ip->rec_tree, hammer_rec_scan_cmp,
268                                    hammer_rec_scan_callback, cursor);
269
270         /*
271          * Adjust scan.node and keep it linked into the RB-tree so we can
272          * hold the cursor through third party modifications of the RB-tree.
273          */
274         if (cursor->iprec) {
275                 cursor->scan.node = hammer_rec_rb_tree_RB_NEXT(cursor->iprec);
276                 return(0);
277         }
278         return(ENOENT);
279 }
280
281 void
282 hammer_mem_done(hammer_cursor_t cursor)
283 {
284         if (cursor->ip) {
285                 hammer_rec_rb_tree_scan_info_done(&cursor->scan,
286                                                   &cursor->ip->rec_tree);
287                 cursor->ip = NULL;
288         }
289         if (cursor->iprec) {
290                 hammer_rel_mem_record(cursor->iprec);
291                 cursor->iprec = NULL;
292         }
293 }
294
295 /************************************************************************
296  *                   HAMMER IN-MEMORY RECORD FUNCTIONS                  *
297  ************************************************************************
298  *
299  * These functions manipulate in-memory records.  Such records typically
300  * exist prior to being committed to disk or indexed via the on-disk B-Tree.
301  */
302
303 /*
304  * Add a directory entry (dip,ncp) which references inode (ip).
305  *
306  * Note that the low 32 bits of the namekey are set temporarily to create
307  * a unique in-memory record, and may be modified a second time when the
308  * record is synchronized to disk.  In particular, the low 32 bits cannot be
309  * all 0's when synching to disk, which is not handled here.
310  */
311 int
312 hammer_ip_add_directory(struct hammer_transaction *trans,
313                      struct hammer_inode *dip, struct namecache *ncp,
314                      struct hammer_inode *ip)
315 {
316         hammer_record_t record;
317         int error;
318         int bytes;
319
320         record = hammer_alloc_mem_record(dip);
321
322         bytes = ncp->nc_nlen;   /* NOTE: terminating \0 is NOT included */
323         if (++trans->hmp->namekey_iterator == 0)
324                 ++trans->hmp->namekey_iterator;
325
326         record->rec.entry.base.base.obj_id = dip->obj_id;
327         record->rec.entry.base.base.key =
328                 hammer_directory_namekey(ncp->nc_name, bytes);
329         record->rec.entry.base.base.key += trans->hmp->namekey_iterator;
330         record->rec.entry.base.base.create_tid = trans->tid;
331         record->rec.entry.base.base.rec_type = HAMMER_RECTYPE_DIRENTRY;
332         record->rec.entry.base.base.obj_type = ip->ino_rec.base.base.obj_type;
333         record->rec.entry.obj_id = ip->obj_id;
334         if (bytes <= sizeof(record->rec.entry.den_name)) {
335                 record->data = (void *)record->rec.entry.den_name;
336                 record->flags |= HAMMER_RECF_EMBEDDED_DATA;
337         } else {
338                 ++hammer_count_record_datas;
339                 record->data = kmalloc(bytes, M_HAMMER, M_WAITOK);
340                 record->flags |= HAMMER_RECF_ALLOCDATA;
341         }
342         bcopy(ncp->nc_name, record->data, bytes);
343         record->rec.entry.base.data_len = bytes;
344         ++ip->ino_rec.ino_nlinks;
345         hammer_modify_inode(trans, ip,
346                             HAMMER_INODE_RDIRTY | HAMMER_INODE_TID);
347         error = hammer_mem_add(trans, record);
348         return(error);
349 }
350
351 /*
352  * Delete the directory entry and update the inode link count.  The
353  * cursor must be seeked to the directory entry record being deleted.
354  *
355  * NOTE: HAMMER_CURSOR_DELETE may not have been set.  XXX remove flag.
356  */
357 int
358 hammer_ip_del_directory(struct hammer_transaction *trans,
359                      hammer_cursor_t cursor, struct hammer_inode *dip,
360                      struct hammer_inode *ip)
361 {
362         int error;
363
364         error = hammer_ip_delete_record(cursor, trans->tid);
365
366         /*
367          * One less link.  The file may still be open in the OS even after
368          * all links have gone away so we don't destroy the inode's data
369          * here.
370          */
371         if (error == 0) {
372                 --ip->ino_rec.ino_nlinks;
373                 hammer_modify_inode(trans, ip,
374                                     HAMMER_INODE_RDIRTY | HAMMER_INODE_TID);
375                 if (ip->vp == NULL || (ip->vp->v_flag & VINACTIVE))
376                         hammer_sync_inode(ip, MNT_NOWAIT, 1);
377
378         }
379         return(error);
380 }
381
382 /*
383  * Add a record to an inode.
384  *
385  * The caller must allocate the record with hammer_alloc_mem_record(ip) and
386  * initialize the following additional fields:
387  *
388  * record->rec.entry.base.base.key
389  * record->rec.entry.base.base.rec_type
390  * record->rec.entry.base.base.data_len
391  * record->data         (a copy will be kmalloc'd if not embedded)
392  */
393 int
394 hammer_ip_add_record(struct hammer_transaction *trans, hammer_record_t record)
395 {
396         hammer_inode_t ip = record->ip;
397         int error;
398         int bytes;
399         void *data;
400
401         record->rec.base.base.obj_id = ip->obj_id;
402         record->rec.base.base.create_tid = trans->tid;
403         record->rec.base.base.obj_type = ip->ino_rec.base.base.obj_type;
404         bytes = record->rec.base.data_len;
405
406         if (record->data) {
407                 if ((char *)record->data < (char *)&record->rec ||
408                     (char *)record->data >= (char *)(&record->rec + 1)) {
409                         ++hammer_count_record_datas;
410                         data = kmalloc(bytes, M_HAMMER, M_WAITOK);
411                         record->flags |= HAMMER_RECF_ALLOCDATA;
412                         bcopy(record->data, data, bytes);
413                         record->data = data;
414                 } else {
415                         record->flags |= HAMMER_RECF_EMBEDDED_DATA;
416                 }
417         }
418         hammer_modify_inode(trans, ip,
419                             HAMMER_INODE_RDIRTY | HAMMER_INODE_TID);
420         error = hammer_mem_add(trans, record);
421         return(error);
422 }
423
424 /*
425  * Sync data from a buffer cache buffer (typically) to the filesystem.  This
426  * is called via the strategy called from a cached data source.  This code
427  * is responsible for actually writing a data record out to the disk.
428  */
429 int
430 hammer_ip_sync_data(hammer_transaction_t trans, hammer_inode_t ip,
431                        int64_t offset, void *data, int bytes,
432                        struct hammer_cursor **spike)
433 {
434         struct hammer_cursor cursor;
435         hammer_record_ondisk_t rec;
436         union hammer_btree_elm elm;
437         void *bdata;
438         int error;
439
440         error = hammer_init_cursor_ip(&cursor, ip);
441         if (error)
442                 return(error);
443         cursor.key_beg.obj_id = ip->obj_id;
444         cursor.key_beg.key = offset + bytes;
445         cursor.key_beg.create_tid = trans->tid;
446         cursor.key_beg.delete_tid = 0;
447         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
448         cursor.flags = HAMMER_CURSOR_INSERT;
449
450         /*
451          * Issue a lookup to position the cursor and locate the cluster
452          */
453         error = hammer_btree_lookup(&cursor);
454         if (error == 0) {
455                 kprintf("hammer_ip_sync_data: duplicate data at (%lld,%d)\n",
456                         offset, bytes);
457                 hammer_print_btree_elm(&cursor.node->ondisk->elms[cursor.index],
458                                        HAMMER_BTREE_TYPE_LEAF, cursor.index);
459                 error = EIO;
460         }
461         if (error != ENOENT)
462                 goto done;
463
464         /*
465          * Allocate record and data space now that we know which cluster
466          * the B-Tree node ended up in.
467          */
468         bdata = hammer_alloc_data(cursor.node->cluster, bytes, &error,
469                                   &cursor.data_buffer);
470         if (bdata == NULL)
471                 goto done;
472         rec = hammer_alloc_record(cursor.node->cluster, &error,
473                                   &cursor.record_buffer);
474         if (rec == NULL)
475                 goto fail1;
476
477         /*
478          * Fill everything in and insert our B-Tree node.
479          */
480         hammer_modify_buffer(cursor.record_buffer);
481         rec->base.base = cursor.key_beg;
482         rec->base.data_crc = crc32(data, bytes);
483         rec->base.rec_id = 0;   /* XXX */
484         rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer, bdata);
485         rec->base.data_len = bytes;
486         hammer_modify_buffer_done(cursor.record_buffer);
487
488         hammer_modify_buffer(cursor.data_buffer);
489         bcopy(data, bdata, bytes);
490         hammer_modify_buffer_done(cursor.data_buffer);
491
492         elm.leaf.base = cursor.key_beg;
493         elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
494         elm.leaf.data_offset = rec->base.data_offset;
495         elm.leaf.data_len = bytes;
496         elm.leaf.data_crc = rec->base.data_crc;
497
498         error = hammer_btree_insert(&cursor, &elm);
499         if (error == 0)
500                 goto done;
501
502         hammer_free_record_ptr(cursor.record_buffer, rec);
503 fail1:
504         hammer_free_data_ptr(cursor.data_buffer, bdata, bytes);
505 done:
506         /*
507          * If ENOSPC in cluster fill in the spike structure and return
508          * ENOSPC.
509          */
510         if (error == ENOSPC)
511                 hammer_load_spike(&cursor, spike);
512         hammer_done_cursor(&cursor);
513         return(error);
514 }
515
516 /*
517  * Sync an in-memory record to the disk.  this is typically called via fsync
518  * from a cached record source.  This code is responsible for actually
519  * writing a record out to the disk.
520  */
521 int
522 hammer_ip_sync_record(hammer_record_t record, struct hammer_cursor **spike)
523 {
524         struct hammer_cursor cursor;
525         hammer_record_ondisk_t rec;
526         hammer_mount_t hmp;
527         union hammer_btree_elm elm;
528         void *bdata;
529         int error;
530
531         error = hammer_init_cursor_ip(&cursor, record->ip);
532         if (error)
533                 return(error);
534         cursor.key_beg = record->rec.base.base;
535         cursor.flags = HAMMER_CURSOR_INSERT;
536
537         /*
538          * Issue a lookup to position the cursor and locate the cluster.  The
539          * target key should not exist.  If we are creating a directory entry
540          * we may have to iterate the low 32 bits of the key to find an unused
541          * key.
542          *
543          * If we run out of space trying to adjust the B-Tree for the
544          * insert, re-lookup without the insert flag so the cursor
545          * is properly positioned for the spike.
546          */
547 again:
548         error = hammer_btree_lookup(&cursor);
549         if (error == 0) {
550                 if (record->rec.base.base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
551                         hmp = cursor.node->cluster->volume->hmp;
552                         if (++hmp->namekey_iterator == 0)
553                                 ++hmp->namekey_iterator;
554                         record->rec.base.base.key &= ~(0xFFFFFFFFLL);
555                         record->rec.base.base.key |= hmp->namekey_iterator;
556                         goto again;
557                 }
558                 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
559                         record->rec.base.base.key);
560                 Debugger("duplicate record1");
561                 error = EIO;
562         }
563         if (error != ENOENT)
564                 goto done;
565
566         /*
567          * Mark the record as undergoing synchronization.  Our cursor is
568          * holding a locked B-Tree node for the insertion which interlocks
569          * anyone trying to access this record.
570          *
571          * XXX There is still a race present related to iterations.  An
572          * iteration may process the record, a sync may occur, and then
573          * later process the B-Tree element for the same record.
574          *
575          * We do not try to synchronize a deleted record.
576          */
577         if (record->flags & (HAMMER_RECF_DELETED | HAMMER_RECF_SYNCING)) {
578                 error = 0;
579                 goto done;
580         }
581         record->flags |= HAMMER_RECF_SYNCING;
582
583         /*
584          * Allocate record and data space now that we know which cluster
585          * the B-Tree node ended up in.
586          */
587         if (record->data == NULL ||
588             (record->flags & HAMMER_RECF_EMBEDDED_DATA)) {
589                 bdata = record->data;
590         } else {
591                 bdata = hammer_alloc_data(cursor.node->cluster,
592                                           record->rec.base.data_len, &error,
593                                           &cursor.data_buffer);
594                 if (bdata == NULL)
595                         goto fail2;
596         }
597         rec = hammer_alloc_record(cursor.node->cluster, &error,
598                                   &cursor.record_buffer);
599         if (rec == NULL)
600                 goto fail1;
601
602         /*
603          * Fill everything in and insert our B-Tree node.
604          *
605          * XXX assign rec_id here
606          */
607         hammer_modify_buffer(cursor.record_buffer);
608         *rec = record->rec;
609         if (bdata) {
610                 rec->base.data_crc = crc32(record->data,
611                                            record->rec.base.data_len);
612                 if (record->flags & HAMMER_RECF_EMBEDDED_DATA) {
613                         /*
614                          * Data embedded in record
615                          */
616                         rec->base.data_offset = ((char *)bdata -
617                                                  (char *)&record->rec);
618                         KKASSERT(rec->base.data_offset >= 0 && 
619                                  rec->base.data_offset + rec->base.data_len <=
620                                   sizeof(*rec));
621                         rec->base.data_offset += hammer_bclu_offset(cursor.record_buffer, rec);
622                 } else {
623                         /*
624                          * Data separate from record
625                          */
626                         rec->base.data_offset = hammer_bclu_offset(cursor.data_buffer,bdata);
627                         hammer_modify_buffer(cursor.data_buffer);
628                         bcopy(record->data, bdata, rec->base.data_len);
629                         hammer_modify_buffer_done(cursor.data_buffer);
630                 }
631         }
632         rec->base.rec_id = 0;   /* XXX */
633         hammer_modify_buffer_done(cursor.record_buffer);
634
635         elm.leaf.base = cursor.key_beg;
636         elm.leaf.rec_offset = hammer_bclu_offset(cursor.record_buffer, rec);
637         elm.leaf.data_offset = rec->base.data_offset;
638         elm.leaf.data_len = rec->base.data_len;
639         elm.leaf.data_crc = rec->base.data_crc;
640
641         error = hammer_btree_insert(&cursor, &elm);
642
643         /*
644          * Clean up on success, or fall through on error.
645          */
646         if (error == 0) {
647                 record->flags |= HAMMER_RECF_DELETED;
648                 record->flags &= ~HAMMER_RECF_SYNCING;
649                 goto done;
650         }
651
652         hammer_free_record_ptr(cursor.record_buffer, rec);
653 fail1:
654         if (record->data && (record->flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
655                 hammer_free_data_ptr(cursor.data_buffer, bdata,
656                                      record->rec.base.data_len);
657         }
658 fail2:
659         record->flags &= ~HAMMER_RECF_SYNCING;
660 done:
661         /*
662          * If ENOSPC in cluster fill in the spike structure and return
663          * ENOSPC.
664          */
665         if (error == ENOSPC)
666                 hammer_load_spike(&cursor, spike);
667         hammer_done_cursor(&cursor);
668         return(error);
669 }
670
671 /*
672  * Write out a record using the specified cursor.  The caller does not have
673  * to seek the cursor.  The flags are used to determine whether the data
674  * (if any) is embedded in the record or not.
675  *
676  * The target cursor will be modified by this call.  Note in particular
677  * that HAMMER_CURSOR_INSERT is set.
678  */
679 int
680 hammer_write_record(hammer_cursor_t cursor, hammer_record_ondisk_t orec,
681                     void *data, int cursor_flags)
682 {
683         union hammer_btree_elm elm;
684         hammer_record_ondisk_t nrec;
685         void *bdata;
686         int error;
687
688         cursor->key_beg = orec->base.base;
689         cursor->flags |= HAMMER_CURSOR_INSERT;
690
691         /*
692          * Issue a lookup to position the cursor and locate the cluster.  The
693          * target key should not exist.
694          *
695          * If we run out of space trying to adjust the B-Tree for the
696          * insert, re-lookup without the insert flag so the cursor
697          * is properly positioned for the spike.
698          */
699         error = hammer_btree_lookup(cursor);
700         if (error == 0) {
701                 kprintf("hammer_ip_sync_record: duplicate rec at (%016llx)\n",
702                         orec->base.base.key);
703                 Debugger("duplicate record2");
704                 error = EIO;
705         }
706         if (error != ENOENT)
707                 goto done;
708
709         /*
710          * Allocate record and data space now that we know which cluster
711          * the B-Tree node ended up in.
712          */
713         if (data == NULL ||
714             (cursor_flags & HAMMER_RECF_EMBEDDED_DATA)) {
715                 bdata = data;
716         } else {
717                 bdata = hammer_alloc_data(cursor->node->cluster,
718                                           orec->base.data_len, &error,
719                                           &cursor->data_buffer);
720                 if (bdata == NULL)
721                         goto done;
722         }
723         nrec = hammer_alloc_record(cursor->node->cluster, &error,
724                                   &cursor->record_buffer);
725         if (nrec == NULL)
726                 goto fail1;
727
728         /*
729          * Fill everything in and insert our B-Tree node.
730          *
731          * XXX assign rec_id here
732          */
733         hammer_modify_buffer(cursor->record_buffer);
734         *nrec = *orec;
735         nrec->base.data_offset = 0;
736         if (bdata) {
737                 nrec->base.data_crc = crc32(bdata, nrec->base.data_len);
738                 if (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) {
739                         /*
740                          * Data embedded in record
741                          */
742                         nrec->base.data_offset = ((char *)bdata - (char *)orec);
743                         KKASSERT(nrec->base.data_offset >= 0 && 
744                                  nrec->base.data_offset + nrec->base.data_len <
745                                   sizeof(*nrec));
746                         nrec->base.data_offset += hammer_bclu_offset(cursor->record_buffer, nrec);
747                 } else {
748                         /*
749                          * Data separate from record
750                          */
751                         nrec->base.data_offset = hammer_bclu_offset(cursor->data_buffer, bdata);
752                         hammer_modify_buffer(cursor->data_buffer);
753                         bcopy(data, bdata, nrec->base.data_len);
754                         hammer_modify_buffer_done(cursor->data_buffer);
755                 }
756         }
757         nrec->base.rec_id = 0;  /* XXX */
758         hammer_modify_buffer_done(cursor->record_buffer);
759
760         elm.leaf.base = nrec->base.base;
761         elm.leaf.rec_offset = hammer_bclu_offset(cursor->record_buffer, nrec);
762         elm.leaf.data_offset = nrec->base.data_offset;
763         elm.leaf.data_len = nrec->base.data_len;
764         elm.leaf.data_crc = nrec->base.data_crc;
765
766         error = hammer_btree_insert(cursor, &elm);
767         if (error == 0)
768                 goto done;
769
770         hammer_free_record_ptr(cursor->record_buffer, nrec);
771 fail1:
772         if (data && (cursor_flags & HAMMER_RECF_EMBEDDED_DATA) == 0) {
773                 hammer_free_data_ptr(cursor->data_buffer, bdata,
774                                      orec->base.data_len);
775         }
776 done:
777         /* leave cursor intact */
778         return(error);
779 }
780
781 /*
782  * Add the record to the inode's rec_tree.  The low 32 bits of a directory
783  * entry's key is used to deal with hash collisions in the upper 32 bits.
784  * A unique 64 bit key is generated in-memory and may be regenerated a
785  * second time when the directory record is flushed to the on-disk B-Tree.
786  *
787  * A referenced record is passed to this function.  This function
788  * eats the reference.  If an error occurs the record will be deleted.
789  */
790 static
791 int
792 hammer_mem_add(struct hammer_transaction *trans, hammer_record_t record)
793 {
794         while (RB_INSERT(hammer_rec_rb_tree, &record->ip->rec_tree, record)) {
795                 if (record->rec.base.base.rec_type != HAMMER_RECTYPE_DIRENTRY){
796                         record->flags |= HAMMER_RECF_DELETED;
797                         hammer_rel_mem_record(record);
798                         return (EEXIST);
799                 }
800                 if (++trans->hmp->namekey_iterator == 0)
801                         ++trans->hmp->namekey_iterator;
802                 record->rec.base.base.key &= ~(0xFFFFFFFFLL);
803                 record->rec.base.base.key |= trans->hmp->namekey_iterator;
804         }
805         record->flags |= HAMMER_RECF_ONRBTREE;
806         hammer_rel_mem_record(record);
807         return(0);
808 }
809
810 /************************************************************************
811  *                   HAMMER INODE MERGED-RECORD FUNCTIONS               *
812  ************************************************************************
813  *
814  * These functions augment the B-Tree scanning functions in hammer_btree.c
815  * by merging in-memory records with on-disk records.
816  */
817
818 /*
819  * Locate a particular record either in-memory or on-disk.
820  *
821  * NOTE: This is basically a standalone routine, hammer_ip_next() may
822  * NOT be called to iterate results.
823  */
824 int
825 hammer_ip_lookup(hammer_cursor_t cursor, struct hammer_inode *ip)
826 {
827         int error;
828
829         /*
830          * If the element is in-memory return it without searching the
831          * on-disk B-Tree
832          */
833         error = hammer_mem_lookup(cursor, ip);
834         if (error == 0) {
835                 cursor->record = &cursor->iprec->rec;
836                 return(error);
837         }
838         if (error != ENOENT)
839                 return(error);
840
841         /*
842          * If the inode has on-disk components search the on-disk B-Tree.
843          */
844         if ((ip->flags & HAMMER_INODE_ONDISK) == 0)
845                 return(error);
846         error = hammer_btree_lookup(cursor);
847         if (error == 0)
848                 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
849         return(error);
850 }
851
852 /*
853  * Locate the first record within the cursor's key_beg/key_end range,
854  * restricted to a particular inode.  0 is returned on success, ENOENT
855  * if no records matched the requested range, or some other error.
856  *
857  * When 0 is returned hammer_ip_next() may be used to iterate additional
858  * records within the requested range.
859  */
860 int
861 hammer_ip_first(hammer_cursor_t cursor, struct hammer_inode *ip)
862 {
863         int error;
864
865         /*
866          * Clean up fields and setup for merged scan
867          */
868         cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
869         cursor->flags |= HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM;
870         cursor->flags |= HAMMER_CURSOR_DISKEOF | HAMMER_CURSOR_MEMEOF;
871         if (cursor->iprec) {
872                 hammer_rel_mem_record(cursor->iprec);
873                 cursor->iprec = NULL;
874         }
875
876         /*
877          * Search the on-disk B-Tree.  hammer_btree_lookup() only does an
878          * exact lookup so if we get ENOENT we have to call the iterate
879          * function to validate the first record after the begin key.
880          *
881          * The ATEDISK flag is used by hammer_btree_iterate to determine
882          * whether it must index forwards or not.  It is also used here
883          * to select the next record from in-memory or on-disk.
884          */
885         if (ip->flags & HAMMER_INODE_ONDISK) {
886                 error = hammer_btree_lookup(cursor);
887                 if (error == ENOENT) {
888                         cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
889                         error = hammer_btree_iterate(cursor);
890                 }
891                 if (error && error != ENOENT) 
892                         return(error);
893                 if (error == 0) {
894                         cursor->flags &= ~HAMMER_CURSOR_DISKEOF;
895                         cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
896                 } else {
897                         cursor->flags |= HAMMER_CURSOR_ATEDISK;
898                 }
899         }
900
901         /*
902          * Search the in-memory record list (Red-Black tree).  Unlike the
903          * B-Tree search, mem_first checks for records in the range.
904          */
905         error = hammer_mem_first(cursor, ip);
906         if (error && error != ENOENT)
907                 return(error);
908         if (error == 0) {
909                 cursor->flags &= ~HAMMER_CURSOR_MEMEOF;
910                 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
911         }
912
913         /*
914          * This will return the first matching record.
915          */
916         return(hammer_ip_next(cursor));
917 }
918
919 /*
920  * Retrieve the next record in a merged iteration within the bounds of the
921  * cursor.  This call may be made multiple times after the cursor has been
922  * initially searched with hammer_ip_first().
923  *
924  * 0 is returned on success, ENOENT if no further records match the
925  * requested range, or some other error code is returned.
926  */
927 int
928 hammer_ip_next(hammer_cursor_t cursor)
929 {
930         hammer_btree_elm_t elm;
931         hammer_record_t rec;
932         int error;
933         int r;
934
935         /*
936          * Load the current on-disk and in-memory record.  If we ate any
937          * records we have to get the next one. 
938          *
939          * If we deleted the last on-disk record we had scanned ATEDISK will
940          * be clear and DELBTREE will be set, forcing a call to iterate. The
941          * fact that ATEDISK is clear causes iterate to re-test the 'current'
942          * element.  If ATEDISK is set, iterate will skip the 'current'
943          * element.
944          *
945          * Get the next on-disk record
946          */
947         if (cursor->flags & (HAMMER_CURSOR_ATEDISK|HAMMER_CURSOR_DELBTREE)) {
948                 if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
949                         error = hammer_btree_iterate(cursor);
950                         cursor->flags &= ~HAMMER_CURSOR_DELBTREE;
951                         if (error == 0)
952                                 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
953                         else
954                                 cursor->flags |= HAMMER_CURSOR_DISKEOF |
955                                                  HAMMER_CURSOR_ATEDISK;
956                 }
957         }
958
959         /*
960          * Get the next in-memory record.  The record can be ripped out
961          * of the RB tree so we maintain a scan_info structure to track
962          * the next node.
963          *
964          * hammer_rec_scan_cmp:  Is the record still in our general range,
965          *                       (non-inclusive of snapshot exclusions)?
966          * hammer_rec_scan_callback: Is the record in our snapshot?
967          */
968         if (cursor->flags & HAMMER_CURSOR_ATEMEM) {
969                 if ((cursor->flags & HAMMER_CURSOR_MEMEOF) == 0) {
970                         if (cursor->iprec) {
971                                 hammer_rel_mem_record(cursor->iprec);
972                                 cursor->iprec = NULL;
973                         }
974                         rec = cursor->scan.node;        /* next node */
975                         while (rec) {
976                                 if (hammer_rec_scan_cmp(rec, cursor) != 0)
977                                         break;
978                                 if (hammer_rec_scan_callback(rec, cursor) != 0)
979                                         break;
980                                 rec = hammer_rec_rb_tree_RB_NEXT(rec);
981                         }
982                         if (cursor->iprec) {
983                                 KKASSERT(cursor->iprec == rec);
984                                 cursor->flags &= ~HAMMER_CURSOR_ATEMEM;
985                                 cursor->scan.node =
986                                         hammer_rec_rb_tree_RB_NEXT(rec);
987                         } else {
988                                 cursor->flags |= HAMMER_CURSOR_MEMEOF;
989                         }
990                 }
991         }
992
993         /*
994          * Extract either the disk or memory record depending on their
995          * relative position.
996          */
997         error = 0;
998         switch(cursor->flags & (HAMMER_CURSOR_ATEDISK | HAMMER_CURSOR_ATEMEM)) {
999         case 0:
1000                 /*
1001                  * Both entries valid
1002                  */
1003                 elm = &cursor->node->ondisk->elms[cursor->index];
1004                 r = hammer_btree_cmp(&elm->base, &cursor->iprec->rec.base.base);
1005                 if (r < 0) {
1006                         error = hammer_btree_extract(cursor,
1007                                                      HAMMER_CURSOR_GET_RECORD);
1008                         cursor->flags |= HAMMER_CURSOR_ATEDISK;
1009                         break;
1010                 }
1011                 /* fall through to the memory entry */
1012         case HAMMER_CURSOR_ATEDISK:
1013                 /*
1014                  * Only the memory entry is valid
1015                  */
1016                 cursor->record = &cursor->iprec->rec;
1017                 cursor->flags |= HAMMER_CURSOR_ATEMEM;
1018                 break;
1019         case HAMMER_CURSOR_ATEMEM:
1020                 /*
1021                  * Only the disk entry is valid
1022                  */
1023                 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1024                 cursor->flags |= HAMMER_CURSOR_ATEDISK;
1025                 break;
1026         default:
1027                 /*
1028                  * Neither entry is valid
1029                  *
1030                  * XXX error not set properly
1031                  */
1032                 cursor->record = NULL;
1033                 error = ENOENT;
1034                 break;
1035         }
1036         return(error);
1037 }
1038
1039 /*
1040  * Resolve the cursor->data pointer for the current cursor position in
1041  * a merged iteration.
1042  */
1043 int
1044 hammer_ip_resolve_data(hammer_cursor_t cursor)
1045 {
1046         int error;
1047
1048         if (cursor->iprec && cursor->record == &cursor->iprec->rec) {
1049                 cursor->data = cursor->iprec->data;
1050                 error = 0;
1051         } else {
1052                 error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA);
1053         }
1054         return(error);
1055 }
1056
1057 /*
1058  * Delete all records within the specified range for inode ip.
1059  *
1060  * NOTE: An unaligned range will cause new records to be added to cover
1061  * the edge cases. (XXX not implemented yet).
1062  *
1063  * NOTE: ran_end is inclusive (e.g. 0,1023 instead of 0,1024).
1064  *
1065  * NOTE: Record keys for regular file data have to be special-cased since
1066  * they indicate the end of the range (key = base + bytes).
1067  *
1068  * NOTE: The spike structure must be filled in if we return ENOSPC.
1069  */
1070 int
1071 hammer_ip_delete_range(hammer_transaction_t trans, hammer_inode_t ip,
1072                        int64_t ran_beg, int64_t ran_end,
1073                        struct hammer_cursor **spike)
1074 {
1075         struct hammer_cursor cursor;
1076         hammer_record_ondisk_t rec;
1077         hammer_base_elm_t base;
1078         int error;
1079         int64_t off;
1080
1081         hammer_init_cursor_ip(&cursor, ip);
1082
1083         cursor.key_beg.obj_id = ip->obj_id;
1084         cursor.key_beg.create_tid = ip->obj_asof;
1085         cursor.key_beg.delete_tid = 0;
1086         cursor.key_beg.obj_type = 0;
1087
1088         cursor.key_end = cursor.key_beg;
1089         if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1090                 cursor.key_beg.key = ran_beg;
1091                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1092                 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1093                 cursor.key_end.key = ran_end;
1094         } else {
1095                 /*
1096                  * The key in the B-Tree is (base+bytes), so the first possible
1097                  * matching key is ran_beg + 1.
1098                  */
1099                 int64_t tmp64;
1100
1101                 cursor.key_beg.key = ran_beg + 1;
1102                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1103                 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1104
1105                 tmp64 = ran_end + MAXPHYS + 1;  /* work around GCC-4 bug */
1106                 if (tmp64 < ran_end)
1107                         cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1108                 else
1109                         cursor.key_end.key = ran_end + MAXPHYS + 1;
1110         }
1111         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1112
1113         error = hammer_ip_first(&cursor, ip);
1114
1115         /*
1116          * Iterate through matching records and mark them as deleted.
1117          */
1118         while (error == 0) {
1119                 rec = cursor.record;
1120                 base = &rec->base.base;
1121
1122                 KKASSERT(base->delete_tid == 0);
1123
1124                 /*
1125                  * There may be overlap cases for regular file data.  Also
1126                  * remember the key for a regular file record is the offset
1127                  * of the last byte of the record (base + len - 1), NOT the
1128                  * base offset.
1129                  */
1130 #if 0
1131                 kprintf("delete_range rec_type %02x\n", base->rec_type);
1132 #endif
1133                 if (base->rec_type == HAMMER_RECTYPE_DATA) {
1134 #if 0
1135                         kprintf("delete_range loop key %016llx\n",
1136                                 base->key - rec->base.data_len);
1137 #endif
1138                         off = base->key - rec->base.data_len;
1139                         /*
1140                          * Check the left edge case.  We currently do not
1141                          * split existing records.
1142                          */
1143                         if (off < ran_beg) {
1144                                 panic("hammer left edge case %016llx %d\n",
1145                                         base->key, rec->base.data_len);
1146                         }
1147
1148                         /*
1149                          * Check the right edge case.  Note that the
1150                          * record can be completely out of bounds, which
1151                          * terminates the search.
1152                          *
1153                          * base->key is exclusive of the right edge while
1154                          * ran_end is inclusive of the right edge.  The
1155                          * (key - data_len) left boundary is inclusive.
1156                          *
1157                          * XXX theory-check this test at some point, are
1158                          * we missing a + 1 somewhere?  Note that ran_end
1159                          * could overflow.
1160                          */
1161                         if (base->key > ran_end) {
1162                                 if (base->key - rec->base.data_len > ran_end) {
1163                                         kprintf("right edge OOB\n");
1164                                         break;
1165                                 }
1166                                 panic("hammer right edge case\n");
1167                         }
1168                 }
1169
1170                 /*
1171                  * Mark the record and B-Tree entry as deleted.  This will
1172                  * also physically delete the B-Tree entry, record, and
1173                  * data if the retention policy dictates.  The function
1174                  * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1175                  * uses to perform a fixup.
1176                  */
1177                 error = hammer_ip_delete_record(&cursor, trans->tid);
1178                 if (error)
1179                         break;
1180                 error = hammer_ip_next(&cursor);
1181         }
1182         hammer_done_cursor(&cursor);
1183         if (error == ENOENT)
1184                 error = 0;
1185         return(error);
1186 }
1187
1188 /*
1189  * Delete all records associated with an inode except the inode record
1190  * itself.
1191  */
1192 int
1193 hammer_ip_delete_range_all(hammer_transaction_t trans, hammer_inode_t ip)
1194 {
1195         struct hammer_cursor cursor;
1196         hammer_record_ondisk_t rec;
1197         hammer_base_elm_t base;
1198         int error;
1199
1200         hammer_init_cursor_ip(&cursor, ip);
1201
1202         cursor.key_beg.obj_id = ip->obj_id;
1203         cursor.key_beg.create_tid = ip->obj_asof;
1204         cursor.key_beg.delete_tid = 0;
1205         cursor.key_beg.obj_type = 0;
1206         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1207         cursor.key_beg.key = HAMMER_MIN_KEY;
1208
1209         cursor.key_end = cursor.key_beg;
1210         cursor.key_end.rec_type = 0xFFFF;
1211         cursor.key_end.key = HAMMER_MAX_KEY;
1212
1213         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1214
1215         error = hammer_ip_first(&cursor, ip);
1216
1217         /*
1218          * Iterate through matching records and mark them as deleted.
1219          */
1220         while (error == 0) {
1221                 rec = cursor.record;
1222                 base = &rec->base.base;
1223
1224                 KKASSERT(base->delete_tid == 0);
1225
1226                 /*
1227                  * Mark the record and B-Tree entry as deleted.  This will
1228                  * also physically delete the B-Tree entry, record, and
1229                  * data if the retention policy dictates.  The function
1230                  * will set HAMMER_CURSOR_DELBTREE which hammer_ip_next()
1231                  * uses to perform a fixup.
1232                  */
1233                 error = hammer_ip_delete_record(&cursor, trans->tid);
1234                 if (error)
1235                         break;
1236                 error = hammer_ip_next(&cursor);
1237         }
1238         hammer_done_cursor(&cursor);
1239         if (error == ENOENT)
1240                 error = 0;
1241         return(error);
1242 }
1243
1244 /*
1245  * Delete the record at the current cursor
1246  */
1247 int
1248 hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid)
1249 {
1250         hammer_btree_elm_t elm;
1251         hammer_mount_t hmp;
1252         int error;
1253
1254         /*
1255          * In-memory (unsynchronized) records can simply be freed.
1256          */
1257         if (cursor->record == &cursor->iprec->rec) {
1258                 cursor->iprec->flags |= HAMMER_RECF_DELETED;
1259                 return(0);
1260         }
1261
1262         /*
1263          * On-disk records are marked as deleted by updating their delete_tid.
1264          */
1265         error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_RECORD);
1266         elm = NULL;
1267         hmp = cursor->node->cluster->volume->hmp;
1268
1269         if (error == 0) {
1270                 hammer_modify_buffer(cursor->record_buffer);
1271                 cursor->record->base.base.delete_tid = tid;
1272                 hammer_modify_buffer_done(cursor->record_buffer);
1273                 hammer_modify_node(cursor->node);
1274                 elm = &cursor->node->ondisk->elms[cursor->index];
1275                 elm->leaf.base.delete_tid = tid;
1276                 hammer_modify_node_done(cursor->node);
1277         }
1278
1279         /*
1280          * If we were mounted with the nohistory option, we physically
1281          * delete the record.
1282          */
1283         if (error == 0 && (hmp->hflags & HMNT_NOHISTORY)) {
1284                 int32_t rec_offset;
1285                 int32_t data_offset;
1286                 int32_t data_len;
1287                 hammer_cluster_t cluster;
1288
1289                 rec_offset = elm->leaf.rec_offset;
1290                 data_offset = elm->leaf.data_offset;
1291                 data_len = elm->leaf.data_len;
1292 #if 0
1293                 kprintf("hammer_ip_delete_record: %08x %08x/%d\n",
1294                         rec_offset, data_offset, data_len);
1295 #endif
1296                 cluster = cursor->node->cluster;
1297                 hammer_ref_cluster(cluster);
1298
1299                 error = hammer_btree_delete(cursor);
1300                 if (error == 0) {
1301                         /*
1302                          * This forces a fixup for the iteration because
1303                          * the cursor is now either sitting at the 'next'
1304                          * element or sitting at the end of a leaf.
1305                          */
1306                         if ((cursor->flags & HAMMER_CURSOR_DISKEOF) == 0) {
1307                                 cursor->flags |= HAMMER_CURSOR_DELBTREE;
1308                                 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1309                         }
1310                         hammer_free_record(cluster, rec_offset);
1311                         if (data_offset && (data_offset - rec_offset < 0 ||
1312                             data_offset - rec_offset >= HAMMER_RECORD_SIZE)) {
1313                                 hammer_free_data(cluster, data_offset,data_len);
1314                         }
1315                 }
1316                 hammer_rel_cluster(cluster, 0);
1317                 if (error) {
1318                         panic("hammer_ip_delete_record: unable to physically delete the record!\n");
1319                         error = 0;
1320                 }
1321         }
1322         return(error);
1323 }
1324
1325 /*
1326  * Determine whether a directory is empty or not.  Returns 0 if the directory
1327  * is empty, ENOTEMPTY if it isn't, plus other possible errors.
1328  */
1329 int
1330 hammer_ip_check_directory_empty(hammer_transaction_t trans, hammer_inode_t ip)
1331 {
1332         struct hammer_cursor cursor;
1333         int error;
1334
1335         hammer_init_cursor_ip(&cursor, ip);
1336
1337         cursor.key_beg.obj_id = ip->obj_id;
1338         cursor.key_beg.create_tid = ip->obj_asof;
1339         cursor.key_beg.delete_tid = 0;
1340         cursor.key_beg.obj_type = 0;
1341         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE + 1;
1342         cursor.key_beg.key = HAMMER_MIN_KEY;
1343
1344         cursor.key_end = cursor.key_beg;
1345         cursor.key_end.rec_type = 0xFFFF;
1346         cursor.key_end.key = HAMMER_MAX_KEY;
1347
1348         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1349
1350         error = hammer_ip_first(&cursor, ip);
1351         if (error == ENOENT)
1352                 error = 0;
1353         else if (error == 0)
1354                 error = ENOTEMPTY;
1355         hammer_done_cursor(&cursor);
1356         return(error);
1357 }
1358