HAMMER Utilities: Stabilization
[dragonfly.git] / sys / vfs / hammer / hammer_mirror.c
1 /*
2  * Copyright (c) 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_mirror.c,v 1.5 2008/07/02 21:57:54 dillon Exp $
35  */
36 /*
37  * HAMMER mirroring ioctls - serialize and deserialize modifications made
38  *                           to a filesystem.
39  */
40
41 #include "hammer.h"
42
43 static int hammer_mirror_check(hammer_cursor_t cursor,
44                                 struct hammer_ioc_mrecord *mrec);
45 static int hammer_mirror_update(hammer_cursor_t cursor,
46                                 struct hammer_ioc_mrecord *mrec);
47 static int hammer_mirror_write(hammer_cursor_t cursor,
48                                 struct hammer_ioc_mrecord *mrec,
49                                 char *udata);
50 static int hammer_mirror_localize_data(hammer_data_ondisk_t data,
51                                 hammer_btree_leaf_elm_t leaf);
52
53 /*
54  * All B-Tree records within the specified key range which also conform
55  * to the transaction id range are returned.  Mirroring code keeps track
56  * of the last transaction id fully scanned and can efficiently pick up
57  * where it left off if interrupted.
58  */
59 int
60 hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip,
61                        struct hammer_ioc_mirror_rw *mirror)
62 {
63         struct hammer_cursor cursor;
64         struct hammer_ioc_mrecord mrec;
65         hammer_btree_leaf_elm_t elm;
66         const int head_size = HAMMER_MREC_HEADSIZE;
67         const int crc_start = HAMMER_MREC_CRCOFF;
68         char *uptr;
69         int error;
70         int data_len;
71         int bytes;
72
73         if ((mirror->key_beg.localization | mirror->key_end.localization) &
74             HAMMER_LOCALIZE_PSEUDOFS_MASK) {
75                 return(EINVAL);
76         }
77         if (hammer_btree_cmp(&mirror->key_beg, &mirror->key_end) > 0)
78                 return(EINVAL);
79
80         mirror->key_cur = mirror->key_beg;
81         mirror->key_cur.localization += ip->obj_localization;
82         bzero(&mrec, sizeof(mrec));
83
84 retry:
85         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
86         if (error) {
87                 hammer_done_cursor(&cursor);
88                 goto failed;
89         }
90         cursor.key_beg = mirror->key_cur;
91         cursor.key_end = mirror->key_end;
92         cursor.key_end.localization += ip->obj_localization;
93
94         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
95         cursor.flags |= HAMMER_CURSOR_BACKEND;
96
97         /*
98          * This flag filters the search to only return elements whos create
99          * or delete TID is >= mirror_tid.  The B-Tree uses the mirror_tid
100          * field stored with internal and leaf nodes to shortcut the scan.
101          */
102         cursor.flags |= HAMMER_CURSOR_MIRROR_FILTERED;
103         cursor.mirror_tid = mirror->tid_beg;
104
105         error = hammer_btree_first(&cursor);
106         while (error == 0) {
107                 /*
108                  * Leaf node.  Only return elements modified in the range
109                  * requested by userland.
110                  */
111                 KKASSERT(cursor.node->ondisk->type == HAMMER_BTREE_TYPE_LEAF);
112                 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
113
114                 if (elm->base.create_tid < mirror->tid_beg ||
115                     elm->base.create_tid >= mirror->tid_end) {
116                         if (elm->base.delete_tid < mirror->tid_beg ||
117                             elm->base.delete_tid >= mirror->tid_end) {
118                                 goto skip;
119                         }
120                 }
121
122                 mirror->key_cur = elm->base;
123
124                 /*
125                  * Yield to more important tasks
126                  */
127                 if ((error = hammer_signal_check(trans->hmp)) != 0)
128                         break;
129                 if (trans->hmp->sync_lock.wanted) {
130                         tsleep(trans, 0, "hmrslo", hz / 10);
131                 }
132                 if (trans->hmp->locked_dirty_space +
133                     trans->hmp->io_running_space > hammer_limit_dirtybufspace) {
134                         hammer_flusher_async(trans->hmp);
135                         tsleep(trans, 0, "hmrslo", hz / 10);
136                 }
137
138                 /*
139                  * The core code exports the data to userland.
140                  */
141                 data_len = (elm->data_offset) ? elm->data_len : 0;
142                 if (data_len) {
143                         error = hammer_btree_extract(&cursor,
144                                                      HAMMER_CURSOR_GET_DATA);
145                         if (error)
146                                 break;
147                 }
148                 bytes = sizeof(struct hammer_ioc_mrecord) + data_len;
149                 bytes = (bytes + HAMMER_HEAD_ALIGN_MASK) &
150                         ~HAMMER_HEAD_ALIGN_MASK;
151                 if (mirror->count + bytes > mirror->size)
152                         break;
153
154                 /*
155                  * Construct the record for userland and copyout.
156                  *
157                  * The user is asking for a snapshot, if the record was
158                  * deleted beyond the user-requested ending tid, the record
159                  * is not considered deleted from the point of view of
160                  * userland and delete_tid is cleared.
161                  */
162                 mrec.signature = HAMMER_IOC_MIRROR_SIGNATURE;
163                 mrec.type = HAMMER_MREC_TYPE_REC;
164                 mrec.rec_size = bytes;
165                 mrec.leaf = *elm;
166                 if (elm->base.delete_tid >= mirror->tid_end)
167                         mrec.leaf.base.delete_tid = 0;
168                 mrec.rec_crc = crc32(&mrec.rec_size, head_size - crc_start);
169                 uptr = (char *)mirror->ubuf + mirror->count;
170                 error = copyout(&mrec, uptr, head_size);
171                 if (data_len && error == 0) {
172                         error = copyout(cursor.data, uptr + head_size,
173                                         data_len);
174                 }
175                 if (error == 0)
176                         mirror->count += bytes;
177 skip:
178                 if (error == 0) {
179                         cursor.flags |= HAMMER_CURSOR_ATEDISK;
180                         error = hammer_btree_iterate(&cursor);
181                 }
182         }
183         if (error == ENOENT) {
184                 mirror->key_cur = mirror->key_end;
185                 error = 0;
186         }
187         hammer_done_cursor(&cursor);
188         if (error == EDEADLK)
189                 goto retry;
190         if (error == EINTR) {
191                 mirror->head.flags |= HAMMER_IOC_HEAD_INTR;
192                 error = 0;
193         }
194 failed:
195         mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
196         return(error);
197 }
198
199 /*
200  * Copy records from userland to the target mirror.  Records which already
201  * exist may only have their delete_tid updated.
202  */
203 int
204 hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
205                        struct hammer_ioc_mirror_rw *mirror)
206 {
207         struct hammer_cursor cursor;
208         struct hammer_ioc_mrecord mrec;
209         const int head_size = HAMMER_MREC_HEADSIZE;
210         const int crc_start = HAMMER_MREC_CRCOFF;
211         u_int32_t rec_crc;
212         int error;
213         char *uptr;
214
215         if (mirror->size < 0 || mirror->size > 0x70000000)
216                 return(EINVAL);
217
218         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
219 retry:
220         hammer_normalize_cursor(&cursor);
221
222         while (error == 0 && mirror->count + head_size <= mirror->size) {
223                 /*
224                  * Acquire and validate header
225                  */
226                 uptr = (char *)mirror->ubuf + mirror->count;
227                 error = copyin(uptr, &mrec, head_size);
228                 if (error)
229                         break;
230                 rec_crc = crc32(&mrec.rec_size, head_size - crc_start);
231                 if (mrec.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
232                         error = EINVAL;
233                         break;
234                 }
235                 if (mrec.type != HAMMER_MREC_TYPE_REC) {
236                         error = EINVAL;
237                         break;
238                 }
239                 if (rec_crc != mrec.rec_crc) {
240                         error = EINVAL;
241                         break;
242                 }
243                 if (mrec.rec_size < head_size ||
244                     mrec.rec_size > head_size + HAMMER_XBUFSIZE + 16 ||
245                     mirror->count + mrec.rec_size > mirror->size) {
246                         error = EINVAL;
247                         break;
248                 }
249                 if (mrec.leaf.data_len < 0 || 
250                     mrec.leaf.data_len > HAMMER_XBUFSIZE ||
251                     sizeof(struct hammer_ioc_mrecord) + mrec.leaf.data_len > mrec.rec_size) {
252                         error = EINVAL;
253                 }
254
255                 /*
256                  * Re-localize for target.  relocalization of data is handled
257                  * by hammer_mirror_write().
258                  */
259                 mrec.leaf.base.localization &= HAMMER_LOCALIZE_MASK;
260                 mrec.leaf.base.localization += ip->obj_localization;
261
262                 /*
263                  * Locate the record.
264                  *
265                  * If the record exists only the delete_tid may be updated.
266                  *
267                  * If the record does not exist we create it.  For now we
268                  * ignore records with a non-zero delete_tid.  Note that
269                  * mirror operations are effective an as-of operation and
270                  * delete_tid can be 0 for mirroring purposes even if it is
271                  * not actually 0 at the originator.
272                  */
273                 hammer_normalize_cursor(&cursor);
274                 cursor.key_beg = mrec.leaf.base;
275                 cursor.flags |= HAMMER_CURSOR_BACKEND;
276                 cursor.flags &= ~HAMMER_CURSOR_INSERT;
277                 error = hammer_btree_lookup(&cursor);
278
279                 if (error == 0 && hammer_mirror_check(&cursor, &mrec)) {
280                         hammer_sync_lock_sh(trans);
281                         error = hammer_mirror_update(&cursor, &mrec);
282                         hammer_sync_unlock(trans);
283                 } else if (error == ENOENT && mrec.leaf.base.delete_tid == 0) {
284                         hammer_sync_lock_sh(trans);
285                         error = hammer_mirror_write(&cursor, &mrec,
286                                                     uptr + head_size);
287                         hammer_sync_unlock(trans);
288                 }
289
290                 /*
291                  * Setup for loop
292                  */
293                 if (error == EDEADLK) {
294                         hammer_done_cursor(&cursor);
295                         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
296                         goto retry;
297                 }
298                 if (error == 0) {
299                         mirror->count += mrec.rec_size;
300                 }
301         }
302         hammer_done_cursor(&cursor);
303         return(0);
304 }
305
306 /*
307  * Check whether an update is needed in the case where a match already
308  * exists on the target.  The only type of update allowed in this case
309  * is an update of the delete_tid.
310  *
311  * Return non-zero if the update should proceed.
312  */
313 static
314 int
315 hammer_mirror_check(hammer_cursor_t cursor, struct hammer_ioc_mrecord *mrec)
316 {
317         hammer_btree_leaf_elm_t leaf = cursor->leaf;
318
319         if (leaf->base.delete_tid != mrec->leaf.base.delete_tid) {
320                 if (leaf->base.delete_tid != 0)
321                         return(1);
322         }
323         return(0);
324 }
325
326 /*
327  * Update a record in-place.  Only the delete_tid can change.
328  */
329 static
330 int
331 hammer_mirror_update(hammer_cursor_t cursor, struct hammer_ioc_mrecord *mrec)
332 {
333         hammer_btree_leaf_elm_t elm;
334
335         elm = cursor->leaf;
336
337         if (mrec->leaf.base.delete_tid == 0) {
338                 kprintf("mirror_write: object %016llx:%016llx deleted on "
339                         "target, not deleted on source\n",
340                         elm->base.obj_id, elm->base.key);
341                 return(0);
342         }
343
344         KKASSERT(elm->base.create_tid < mrec->leaf.base.delete_tid);
345         hammer_modify_node(cursor->trans, cursor->node, elm, sizeof(*elm));
346         elm->base.delete_tid = mrec->leaf.base.delete_tid;
347         elm->delete_ts = mrec->leaf.delete_ts;
348         hammer_modify_node_done(cursor->node);
349         return(0);
350 }
351
352 /*
353  * Write out a new record.
354  *
355  * XXX this is messy.
356  */
357 static
358 int
359 hammer_mirror_write(hammer_cursor_t cursor, struct hammer_ioc_mrecord *mrec,
360                     char *udata)
361 {
362         hammer_buffer_t data_buffer = NULL;
363         hammer_off_t ndata_offset;
364         void *ndata;
365         int error;
366         int wanted_skip = 0;
367
368         if (mrec->leaf.data_len && mrec->leaf.data_offset) {
369                 ndata = hammer_alloc_data(cursor->trans, mrec->leaf.data_len,
370                                           mrec->leaf.base.rec_type,
371                                           &ndata_offset, &data_buffer, &error);
372                 if (ndata == NULL)
373                         return(error);
374                 mrec->leaf.data_offset = ndata_offset;
375                 hammer_modify_buffer(cursor->trans, data_buffer, NULL, 0);
376                 error = copyin(udata, ndata, mrec->leaf.data_len);
377                 if (error == 0) {
378                         if (hammer_crc_test_leaf(ndata, &mrec->leaf) == 0) {
379                                 kprintf("data crc mismatch on pipe\n");
380                                 error = EINVAL;
381                         } else {
382                                 error = hammer_mirror_localize_data(
383                                                         ndata, &mrec->leaf);
384                                 if (error)
385                                         wanted_skip = 1;
386                         }
387                 }
388                 hammer_modify_buffer_done(data_buffer);
389         } else {
390                 mrec->leaf.data_offset = 0;
391                 error = 0;
392                 ndata = NULL;
393         }
394         if (error)
395                 goto failed;
396         cursor->flags |= HAMMER_CURSOR_INSERT;
397         error = hammer_btree_lookup(cursor);
398         if (error != ENOENT) {
399                 if (error == 0)
400                         error = EALREADY;
401                 goto failed;
402         }
403         error = 0;
404
405         /*
406          * Physical insertion
407          */
408         error = hammer_btree_insert(cursor, &mrec->leaf);
409
410 failed:
411         /*
412          * Cleanup
413          */
414         if (error && mrec->leaf.data_offset) {
415                 hammer_blockmap_free(cursor->trans,
416                                      mrec->leaf.data_offset,
417                                      mrec->leaf.data_len);
418         }
419         if (data_buffer)
420                 hammer_rel_buffer(data_buffer, 0);
421         if (wanted_skip)
422                 error = 0;
423         return(error);
424 }
425
426 /*
427  * Localize the data payload.  Directory entries may need their
428  * localization adjusted.
429  *
430  * Pseudo-fs directory entries must be skipped entirely (EBADF).
431  *
432  * The root inode must be skipped, it will exist on the target with a
433  * different create_tid so updating it would result in a duplicate.  This
434  * also prevents inode updates on the root directory (aka mtime, ctime, etc)
435  * from mirroring, which is ok.
436  *
437  * XXX Root directory inode updates - parent_obj_localization is broken.
438  */
439 static
440 int
441 hammer_mirror_localize_data(hammer_data_ondisk_t data,
442                             hammer_btree_leaf_elm_t leaf)
443 {
444         int modified = 0;
445         int error = 0;
446         u_int32_t localization;
447
448         if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
449                 localization = leaf->base.localization &
450                                HAMMER_LOCALIZE_PSEUDOFS_MASK;
451                 if (data->entry.localization != localization) {
452                         data->entry.localization = localization;
453                         modified = 1;
454                 }
455                 if (data->entry.obj_id == 1)
456                         error = EBADF;
457         }
458         if (leaf->base.obj_id == HAMMER_OBJID_ROOT) {
459                 if (leaf->base.rec_type == HAMMER_RECTYPE_INODE ||
460                     leaf->base.rec_type == HAMMER_RECTYPE_FIX) {
461                         error = EBADF;
462                 }
463         }
464         if (modified)
465                 hammer_crc_set_leaf(data, leaf);
466         return(error);
467 }
468
469 /*
470  * Set mirroring/pseudo-fs information
471  */
472 int
473 hammer_ioc_set_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
474                         struct hammer_ioc_pseudofs_rw *pfs)
475 {
476         hammer_pseudofs_inmem_t pfsm;
477         int error;
478
479         pfsm = ip->pfsm;
480         error = 0;
481
482         if (pfs->pseudoid != ip->obj_localization)
483                 error = EINVAL;
484         if (pfs->bytes != sizeof(pfsm->pfsd))
485                 error = EINVAL;
486         if (pfs->version != HAMMER_IOC_PSEUDOFS_VERSION)
487                 error = EINVAL;
488         if (error == 0 && pfs->ondisk) {
489                 if (ip->obj_id != HAMMER_OBJID_ROOT)
490                         error = EINVAL;
491                 if (error == 0) {
492                         error = copyin(pfs->ondisk, &ip->pfsm->pfsd,
493                                        sizeof(ip->pfsm->pfsd));
494                 }
495                 if (error == 0)
496                         error = hammer_save_pseudofs(trans, ip);
497         }
498         return(error);
499 }
500
501 /*
502  * Get mirroring/pseudo-fs information
503  */
504 int
505 hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
506                         struct hammer_ioc_pseudofs_rw *pfs)
507 {
508         hammer_pseudofs_inmem_t pfsm;
509         int error;
510
511         pfs->pseudoid = ip->obj_localization;
512         pfs->bytes = sizeof(struct hammer_pseudofs_data);
513         pfs->version = HAMMER_IOC_PSEUDOFS_VERSION;
514
515         /*
516          * Update pfsm->sync_end_tid if a master
517          */
518         pfsm = ip->pfsm;
519         if (pfsm->pfsd.master_id >= 0)
520                 pfsm->pfsd.sync_end_tid = trans->rootvol->ondisk->vol0_next_tid;
521
522         /*
523          * Return PFS information for root inodes only.
524          */
525         error = 0;
526         if (pfs->ondisk) {
527                 if (ip->obj_id != HAMMER_OBJID_ROOT)
528                         error = EINVAL;
529                 if (error == 0) {
530                         error = copyout(&ip->pfsm->pfsd, pfs->ondisk,
531                                         sizeof(ip->pfsm->pfsd));
532                 }
533         }
534         return(error);
535 }
536