]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/spa_history.c
Copy stable/9 to releng/9.1 as part of the 9.1-RELEASE release process.
[FreeBSD/releng/9.1.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / spa_history.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011 by Delphix. All rights reserved.
25  */
26
27 #include <sys/spa.h>
28 #include <sys/spa_impl.h>
29 #include <sys/zap.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/utsname.h>
34 #include <sys/sunddi.h>
35 #include "zfs_comutil.h"
36 #ifdef _KERNEL
37 #include <sys/cmn_err.h>
38 #include <sys/zone.h>
39 #endif
40
41 /*
42  * Routines to manage the on-disk history log.
43  *
44  * The history log is stored as a dmu object containing
45  * <packed record length, record nvlist> tuples.
46  *
47  * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
48  * "packed record length" is the packed length of the "record nvlist" stored
49  * as a little endian uint64_t.
50  *
51  * The log is implemented as a ring buffer, though the original creation
52  * of the pool ('zpool create') is never overwritten.
53  *
54  * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
55  * of 'spa_history' stores the offsets for logging/retrieving history as
56  * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
57  * where the 'zpool create' record is stored.  This allows us to never
58  * overwrite the original creation of the pool.  'sh_phys_max_off' is the
59  * physical ending offset in bytes of the log.  This tells you the length of
60  * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
61  * is added, 'sh_eof' is incremented by the the size of the record.
62  * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
63  * This is where the consumer should start reading from after reading in
64  * the 'zpool create' portion of the log.
65  *
66  * 'sh_records_lost' keeps track of how many records have been overwritten
67  * and permanently lost.
68  */
69
70 /* convert a logical offset to physical */
71 static uint64_t
72 spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
73 {
74         uint64_t phys_len;
75
76         phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
77         return ((log_off - shpp->sh_pool_create_len) % phys_len
78             + shpp->sh_pool_create_len);
79 }
80
81 void
82 spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
83 {
84         dmu_buf_t *dbp;
85         spa_history_phys_t *shpp;
86         objset_t *mos = spa->spa_meta_objset;
87
88         ASSERT(spa->spa_history == 0);
89         spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
90             SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
91             sizeof (spa_history_phys_t), tx);
92
93         VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
94             DMU_POOL_HISTORY, sizeof (uint64_t), 1,
95             &spa->spa_history, tx) == 0);
96
97         VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
98         ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
99
100         shpp = dbp->db_data;
101         dmu_buf_will_dirty(dbp, tx);
102
103         /*
104          * Figure out maximum size of history log.  We set it at
105          * 0.1% of pool size, with a max of 1G and min of 128KB.
106          */
107         shpp->sh_phys_max_off =
108             metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
109         shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
110         shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
111
112         dmu_buf_rele(dbp, FTAG);
113 }
114
115 /*
116  * Change 'sh_bof' to the beginning of the next record.
117  */
118 static int
119 spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
120 {
121         objset_t *mos = spa->spa_meta_objset;
122         uint64_t firstread, reclen, phys_bof;
123         char buf[sizeof (reclen)];
124         int err;
125
126         phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
127         firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
128
129         if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
130             buf, DMU_READ_PREFETCH)) != 0)
131                 return (err);
132         if (firstread != sizeof (reclen)) {
133                 if ((err = dmu_read(mos, spa->spa_history,
134                     shpp->sh_pool_create_len, sizeof (reclen) - firstread,
135                     buf + firstread, DMU_READ_PREFETCH)) != 0)
136                         return (err);
137         }
138
139         reclen = LE_64(*((uint64_t *)buf));
140         shpp->sh_bof += reclen + sizeof (reclen);
141         shpp->sh_records_lost++;
142         return (0);
143 }
144
145 static int
146 spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
147     dmu_tx_t *tx)
148 {
149         uint64_t firstwrite, phys_eof;
150         objset_t *mos = spa->spa_meta_objset;
151         int err;
152
153         ASSERT(MUTEX_HELD(&spa->spa_history_lock));
154
155         /* see if we need to reset logical BOF */
156         while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
157             (shpp->sh_eof - shpp->sh_bof) <= len) {
158                 if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
159                         return (err);
160                 }
161         }
162
163         phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
164         firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
165         shpp->sh_eof += len;
166         dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
167
168         len -= firstwrite;
169         if (len > 0) {
170                 /* write out the rest at the beginning of physical file */
171                 dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
172                     len, (char *)buf + firstwrite, tx);
173         }
174
175         return (0);
176 }
177
178 static char *
179 spa_history_zone()
180 {
181 #ifdef _KERNEL
182         /* XXX: pr_hostname can be changed by default from within a jail! */
183         if (jailed(curthread->td_ucred))
184                 return (curthread->td_ucred->cr_prison->pr_hostname);
185 #endif
186         return ("global");
187 }
188
189 /*
190  * Write out a history event.
191  */
192 /*ARGSUSED*/
193 static void
194 spa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx)
195 {
196         spa_t           *spa = arg1;
197         history_arg_t   *hap = arg2;
198         const char      *history_str = hap->ha_history_str;
199         objset_t        *mos = spa->spa_meta_objset;
200         dmu_buf_t       *dbp;
201         spa_history_phys_t *shpp;
202         size_t          reclen;
203         uint64_t        le_len;
204         nvlist_t        *nvrecord;
205         char            *record_packed = NULL;
206         int             ret;
207
208         /*
209          * If we have an older pool that doesn't have a command
210          * history object, create it now.
211          */
212         mutex_enter(&spa->spa_history_lock);
213         if (!spa->spa_history)
214                 spa_history_create_obj(spa, tx);
215         mutex_exit(&spa->spa_history_lock);
216
217         /*
218          * Get the offset of where we need to write via the bonus buffer.
219          * Update the offset when the write completes.
220          */
221         VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
222         shpp = dbp->db_data;
223
224         dmu_buf_will_dirty(dbp, tx);
225
226 #ifdef ZFS_DEBUG
227         {
228                 dmu_object_info_t doi;
229                 dmu_object_info_from_db(dbp, &doi);
230                 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
231         }
232 #endif
233
234         VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
235         VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
236             gethrestime_sec()) == 0);
237         VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO, hap->ha_uid) == 0);
238         if (hap->ha_zone != NULL)
239                 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
240                     hap->ha_zone) == 0);
241 #ifdef _KERNEL
242         VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
243             utsname.nodename) == 0);
244 #endif
245         if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
246             hap->ha_log_type == LOG_CMD_NORMAL) {
247                 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
248                     history_str) == 0);
249
250                 zfs_dbgmsg("command: %s", history_str);
251         } else {
252                 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
253                     hap->ha_event) == 0);
254                 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
255                     tx->tx_txg) == 0);
256                 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
257                     history_str) == 0);
258
259                 zfs_dbgmsg("internal %s pool:%s txg:%llu %s",
260                     zfs_history_event_names[hap->ha_event], spa_name(spa),
261                     (longlong_t)tx->tx_txg, history_str);
262
263         }
264
265         VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
266         record_packed = kmem_alloc(reclen, KM_SLEEP);
267
268         VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
269             NV_ENCODE_XDR, KM_SLEEP) == 0);
270
271         mutex_enter(&spa->spa_history_lock);
272         if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
273                 VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
274
275         /* write out the packed length as little endian */
276         le_len = LE_64((uint64_t)reclen);
277         ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
278         if (!ret)
279                 ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
280
281         if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
282                 shpp->sh_pool_create_len += sizeof (le_len) + reclen;
283                 shpp->sh_bof = shpp->sh_pool_create_len;
284         }
285
286         mutex_exit(&spa->spa_history_lock);
287         nvlist_free(nvrecord);
288         kmem_free(record_packed, reclen);
289         dmu_buf_rele(dbp, FTAG);
290
291         strfree(hap->ha_history_str);
292         if (hap->ha_zone != NULL)
293                 strfree(hap->ha_zone);
294         kmem_free(hap, sizeof (history_arg_t));
295 }
296
297 /*
298  * Write out a history event.
299  */
300 int
301 spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
302 {
303         history_arg_t *ha;
304         int err = 0;
305         dmu_tx_t *tx;
306
307         ASSERT(what != LOG_INTERNAL);
308
309         tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
310         err = dmu_tx_assign(tx, TXG_WAIT);
311         if (err) {
312                 dmu_tx_abort(tx);
313                 return (err);
314         }
315
316         ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
317         ha->ha_history_str = strdup(history_str);
318         ha->ha_zone = strdup(spa_history_zone());
319         ha->ha_log_type = what;
320         ha->ha_uid = crgetuid(CRED());
321
322         /* Kick this off asynchronously; errors are ignored. */
323         dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
324             spa_history_log_sync, spa, ha, 0, tx);
325         dmu_tx_commit(tx);
326
327         /* spa_history_log_sync will free ha and strings */
328         return (err);
329 }
330
331 /*
332  * Read out the command history.
333  */
334 int
335 spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
336 {
337         objset_t *mos = spa->spa_meta_objset;
338         dmu_buf_t *dbp;
339         uint64_t read_len, phys_read_off, phys_eof;
340         uint64_t leftover = 0;
341         spa_history_phys_t *shpp;
342         int err;
343
344         /*
345          * If the command history  doesn't exist (older pool),
346          * that's ok, just return ENOENT.
347          */
348         if (!spa->spa_history)
349                 return (ENOENT);
350
351         /*
352          * The history is logged asynchronously, so when they request
353          * the first chunk of history, make sure everything has been
354          * synced to disk so that we get it.
355          */
356         if (*offp == 0 && spa_writeable(spa))
357                 txg_wait_synced(spa_get_dsl(spa), 0);
358
359         if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
360                 return (err);
361         shpp = dbp->db_data;
362
363 #ifdef ZFS_DEBUG
364         {
365                 dmu_object_info_t doi;
366                 dmu_object_info_from_db(dbp, &doi);
367                 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
368         }
369 #endif
370
371         mutex_enter(&spa->spa_history_lock);
372         phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
373
374         if (*offp < shpp->sh_pool_create_len) {
375                 /* read in just the zpool create history */
376                 phys_read_off = *offp;
377                 read_len = MIN(*len, shpp->sh_pool_create_len -
378                     phys_read_off);
379         } else {
380                 /*
381                  * Need to reset passed in offset to BOF if the passed in
382                  * offset has since been overwritten.
383                  */
384                 *offp = MAX(*offp, shpp->sh_bof);
385                 phys_read_off = spa_history_log_to_phys(*offp, shpp);
386
387                 /*
388                  * Read up to the minimum of what the user passed down or
389                  * the EOF (physical or logical).  If we hit physical EOF,
390                  * use 'leftover' to read from the physical BOF.
391                  */
392                 if (phys_read_off <= phys_eof) {
393                         read_len = MIN(*len, phys_eof - phys_read_off);
394                 } else {
395                         read_len = MIN(*len,
396                             shpp->sh_phys_max_off - phys_read_off);
397                         if (phys_read_off + *len > shpp->sh_phys_max_off) {
398                                 leftover = MIN(*len - read_len,
399                                     phys_eof - shpp->sh_pool_create_len);
400                         }
401                 }
402         }
403
404         /* offset for consumer to use next */
405         *offp += read_len + leftover;
406
407         /* tell the consumer how much you actually read */
408         *len = read_len + leftover;
409
410         if (read_len == 0) {
411                 mutex_exit(&spa->spa_history_lock);
412                 dmu_buf_rele(dbp, FTAG);
413                 return (0);
414         }
415
416         err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
417             DMU_READ_PREFETCH);
418         if (leftover && err == 0) {
419                 err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
420                     leftover, buf + read_len, DMU_READ_PREFETCH);
421         }
422         mutex_exit(&spa->spa_history_lock);
423
424         dmu_buf_rele(dbp, FTAG);
425         return (err);
426 }
427
428 static void
429 log_internal(history_internal_events_t event, spa_t *spa,
430     dmu_tx_t *tx, const char *fmt, va_list adx)
431 {
432         history_arg_t *ha;
433         va_list adx2;
434
435         /*
436          * If this is part of creating a pool, not everything is
437          * initialized yet, so don't bother logging the internal events.
438          */
439         if (tx->tx_txg == TXG_INITIAL)
440                 return;
441
442         va_copy(adx2, adx);
443
444         ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
445         ha->ha_history_str = kmem_alloc(vsnprintf(NULL, 0, fmt, adx2) + 1,
446             KM_SLEEP);
447
448         va_end(adx2);
449
450         (void) vsprintf(ha->ha_history_str, fmt, adx);
451
452         ha->ha_log_type = LOG_INTERNAL;
453         ha->ha_event = event;
454         ha->ha_zone = NULL;
455         ha->ha_uid = 0;
456
457         if (dmu_tx_is_syncing(tx)) {
458                 spa_history_log_sync(spa, ha, tx);
459         } else {
460                 dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
461                     spa_history_log_sync, spa, ha, 0, tx);
462         }
463         /* spa_history_log_sync() will free ha and strings */
464 }
465
466 void
467 spa_history_log_internal(history_internal_events_t event, spa_t *spa,
468     dmu_tx_t *tx, const char *fmt, ...)
469 {
470         dmu_tx_t *htx = tx;
471         va_list adx;
472
473         /* create a tx if we didn't get one */
474         if (tx == NULL) {
475                 htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
476                 if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
477                         dmu_tx_abort(htx);
478                         return;
479                 }
480         }
481
482         va_start(adx, fmt);
483         log_internal(event, spa, htx, fmt, adx);
484         va_end(adx);
485
486         /* if we didn't get a tx from the caller, commit the one we made */
487         if (tx == NULL)
488                 dmu_tx_commit(htx);
489 }
490
491 void
492 spa_history_log_version(spa_t *spa, history_internal_events_t event)
493 {
494 #ifdef _KERNEL
495         uint64_t current_vers = spa_version(spa);
496
497         if (current_vers >= SPA_VERSION_ZPOOL_HISTORY) {
498                 spa_history_log_internal(event, spa, NULL,
499                     "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s",
500                     (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION,
501                     utsname.nodename, utsname.release, utsname.version,
502                     utsname.machine);
503         }
504 #if 0
505         cmn_err(CE_CONT, "!%s version %llu pool %s using %llu",
506             event == LOG_POOL_IMPORT ? "imported" :
507             event == LOG_POOL_CREATE ? "created" : "accessed",
508             (u_longlong_t)current_vers, spa_name(spa), SPA_VERSION);
509 #endif
510 #endif
511 }