2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid[] = "@(#)log_register.c 10.12 (Sleepycat) 9/29/97";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
24 #include "common_ext.h"
28 * Register a file name.
31 log_register(dblp, dbp, name, type, idp)
51 fnp = fidp = namep = NULL;
53 /* Check the arguments. */
54 if (type != DB_BTREE && type != DB_HASH && type != DB_RECNO) {
55 __db_err(dblp->dbenv, "log_register: unknown DB file type");
59 /* Get the log file id. */
60 if ((ret = __db_appname(dblp->dbenv,
61 DB_APP_DATA, NULL, name, NULL, &fullname)) != 0)
67 * See if we've already got this file in the log, finding the
68 * next-to-lowest file id currently in use as we do it.
70 for (fid = 1, fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
71 fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) {
74 if (!memcmp(dbp->lock.fileid,
75 R_ADDR(dblp, fnp->fileid_off), DB_FILE_ID_LEN)) {
78 if (!F_ISSET(dblp, DB_AM_RECOVER) &&
79 (ret = __log_add_logid(dblp, dbp, fid) != 0))
85 /* Allocate a new file name structure. */
86 if ((ret = __db_shalloc(dblp->addr, sizeof(FNAME), 0, &fnp)) != 0)
92 if ((ret = __db_shalloc(dblp->addr, DB_FILE_ID_LEN, 0, &fidp)) != 0)
95 * XXX Now that uids are fixed size, we can put them in the fnp
98 fnp->fileid_off = R_OFFSET(dblp, fidp);
99 memcpy(fidp, dbp->lock.fileid, DB_FILE_ID_LEN);
101 len = strlen(name) + 1;
102 if ((ret = __db_shalloc(dblp->addr, len, 0, &namep)) != 0)
104 fnp->name_off = R_OFFSET(dblp, namep);
105 memcpy(namep, name, len);
107 SH_TAILQ_INSERT_HEAD(&dblp->lp->fq, fnp, q, __fname);
110 /* Log the registry. */
111 if (!F_ISSET(dblp, DB_AM_RECOVER)) {
112 r_name.data = (void *)name; /* XXX: Yuck! */
113 r_name.size = strlen(name) + 1;
114 memset(&fid_dbt, 0, sizeof(fid_dbt));
115 fid_dbt.data = dbp->lock.fileid;
116 fid_dbt.size = DB_FILE_ID_LEN;
117 if ((ret = __log_register_log(dblp, NULL, &r_unused,
118 0, &r_name, &fid_dbt, fid, type)) != 0)
120 if ((ret = __log_add_logid(dblp, dbp, fid)) != 0)
127 * We should grow the region.
130 SH_TAILQ_REMOVE(&dblp->lp->fq, fnp, q, __fname);
132 __db_shalloc_free(dblp->addr, namep);
134 __db_shalloc_free(dblp->addr, fidp);
136 __db_shalloc_free(dblp->addr, fnp);
139 ret1: UNLOCK_LOGREGION(dblp);
141 if (fullname != NULL)
151 * Discard a registered file name.
154 log_unregister(dblp, fid)
163 LOCK_LOGREGION(dblp);
165 /* Unlog the registry. */
166 if (!F_ISSET(dblp, DB_AM_RECOVER) &&
167 (ret = __log_unregister_log(dblp, NULL, &r_unused, 0, fid)) != 0)
170 /* Find the entry in the log. */
171 for (fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
172 fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname))
176 __db_err(dblp->dbenv, "log_unregister: non-existent file id");
181 /* If more than 1 reference, decrement the reference and return. */
187 /* Free the unique file information, name and structure. */
188 __db_shalloc_free(dblp->addr, R_ADDR(dblp, fnp->fileid_off));
189 __db_shalloc_free(dblp->addr, R_ADDR(dblp, fnp->name_off));
190 SH_TAILQ_REMOVE(&dblp->lp->fq, fnp, q, __fname);
191 __db_shalloc_free(dblp->addr, fnp);
194 * Remove from the process local table. If this operation is taking
195 * place during recovery, then the logid was never added to the table,
196 * so do not remove it.
198 if (!F_ISSET(dblp, DB_AM_RECOVER))
199 __log_rem_logid(dblp, fid);
201 ret1: UNLOCK_LOGREGION(dblp);