2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
16 dbd specs and implementation progress
17 =====================================
26 OK If -f is requested, ensure -e is too.
27 Check if volumes is using AFPVOL_CACHE, then wipe db from disk. Rebuild from ad-files.
34 OK F/D Make sure ad file exists
35 OK D Make sure .AppleDouble dir exist, create if missing. Error creating
36 it is fatal as that shouldn't happen as root.
37 OK F/D Delete orphaned ad-files, log dirs in ad-dir
38 OK F/D Check name encoding by roundtripping, log on error
39 OK F/D try: read CNID from ad file (if cnid caching is on)
40 try: fetch CNID from database
41 -> on mismatch: use CNID from file, update database (deleting both found CNIDs first)
42 -> if no CNID in ad file: write CNID from database to ad file
43 -> if no CNID in database: add CNID from ad file to database
44 -> on no CNID at all: create one and store in both places
45 OK F/D Add found CNID, DID, filename, dev/inode, stamp to rebuild database
46 OK F/D Check/update stamp (implicitly done while checking CNIDs)
49 2nd pass: Delete unused CNIDs
50 -----------------------------
54 OK Step through dbd (the one on disk) and rebuild-db from pass 1 and delete any CNID from
55 dbd not in rebuild db. This in only done in exclusive mode.
60 #endif /* HAVE_CONFIG_H */
63 #include <sys/types.h>
72 #include <atalk/logger.h>
73 #include <atalk/cnid_dbd_private.h>
74 #include <atalk/volinfo.h>
80 #define LOCKFILENAME "lock"
81 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
83 int nocniddb = 0; /* Dont open CNID database, only scan filesystem */
84 volatile sig_atomic_t alarmed; /* flags for signals */
85 int db_locked; /* have we got the fcntl lock on lockfile ? */
88 static int verbose; /* Logging flag */
89 static int exclusive; /* Exclusive volume access */
90 static struct db_param db_param = {
91 NULL, /* Volume dirpath */
92 1, /* bdb logfile autoremove */
93 64 * 1024, /* bdb cachesize (64 MB) */
94 0, /* flush_interval */
95 0, /* flush_frequency */
97 -1, /* fd_table_size */
98 -1, /* idle_timeout */
101 static char dbpath[PATH_MAX]; /* Path to the dbd database */
106 void dbd_log(enum logtype lt, char *fmt, ...)
109 static char logbuffer[1024];
112 if ( (lt == LOGSTD) || (verbose == 1)) {
114 len = vsnprintf(logbuffer, 1023, fmt, args);
118 printf("%s\n", logbuffer);
124 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
127 static void sig_handler(int signo)
133 static void set_signal(void)
137 sv.sa_handler = sig_handler;
138 sv.sa_flags = SA_RESTART;
139 sigemptyset(&sv.sa_mask);
140 if (sigaction(SIGTERM, &sv, NULL) < 0) {
141 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
144 if (sigaction(SIGINT, &sv, NULL) < 0) {
145 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
149 memset(&sv, 0, sizeof(struct sigaction));
150 sv.sa_handler = SIG_IGN;
151 sigemptyset(&sv.sa_mask);
153 if (sigaction(SIGABRT, &sv, NULL) < 0) {
154 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
157 if (sigaction(SIGHUP, &sv, NULL) < 0) {
158 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
161 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
162 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
169 * Get lock on db lock file
171 * @args cmd (r) lock command:
172 * LOCK_FREE: close lockfd
173 * LOCK_UNLOCK: unlock lockm keep lockfd open
174 * LOCK_EXCL: F_WRLCK on lockfd
175 * LOCK_SHRD: F_RDLCK on lockfd
176 * @args dbpath (r) path to lockfile, only used on first call,
177 * later the stored fd is used
178 * @returns LOCK_FREE/LOCK_UNLOCK return 0 on success, -1 on error
179 * LOCK_EXCL/LOCK_SHRD return 1 if lock was acquired,
180 * 0 if file is already locked, -1 on error
182 int get_lock(lockcmd_t cmd, const char *dbpath)
184 static int lockfd = -1;
185 char lockpath[PATH_MAX];
203 lock.l_whence = SEEK_SET;
205 lock.l_type = F_UNLCK;
206 fcntl(lockfd, F_SETLK, &lock);
212 if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
213 dbd_log( LOGSTD, ".AppleDB pathname too long");
216 strncpy(lockpath, dbpath, PATH_MAX - 1);
217 strcat(lockpath, "/");
218 strcat(lockpath, LOCKFILENAME);
220 if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
221 dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
225 if ((stat(dbpath, &st)) != 0) {
226 dbd_log( LOGSTD, "Error statting lockfile: %s", strerror(errno));
230 if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
231 dbd_log( LOGSTD, "Error inheriting lockfile permissions: %s",
238 lock.l_whence = SEEK_SET;
240 if (cmd == LOCK_EXCL)
241 lock.l_type = F_WRLCK;
243 lock.l_type = F_RDLCK;
245 if (fcntl(lockfd, F_SETLK, &lock) < 0) {
246 if (errno == EACCES || errno == EAGAIN) {
247 dbd_log(LOGDEBUG, "get_lock: couldn't lock");
250 dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s",
259 dbd_log(LOGDEBUG, "get_lock: got lock");
263 static void usage (void)
265 printf("Usage: dbd [-e|-t|-v|-x] -d [-i] | -s [-c|-n]| -r [-c|-f] | -u <path to netatalk volume>\n"
266 "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
267 "dbd must be run with appropiate permissions i.e. as root.\n\n"
268 "Main commands are:\n"
269 " -d Dump CNID database\n"
270 " Option: -i dump indexes too\n\n"
272 " 1. Compare CNIDs in database with volume\n"
273 " 2. Check if .AppleDouble dirs exist\n"
274 " 3. Check if AppleDouble file exist\n"
275 " 4. Report orphaned AppleDouble files\n"
276 " 5. Check for directories inside AppleDouble directories\n"
277 " 6. Check name encoding by roundtripping, log on error\n"
278 " 7. Check for orphaned CNIDs in database (requires -e)\n"
279 " 8. Open and close adouble files\n"
280 " Options: -c Don't check .AppleDouble stuff, only ckeck orphaned.\n"
281 " -n Don't open CNID database, skip CNID checks\n\n"
282 " -r Rebuild volume:\n"
283 " 1. Sync CNIDSs in database with volume\n"
284 " 2. Make sure .AppleDouble dir exist, create if missing\n"
285 " 3. Make sure AppleDouble file exists, create if missing\n"
286 " 4. Delete orphaned AppleDouble files\n"
287 " 5. Check for directories inside AppleDouble directories\n"
288 " 6. Check name encoding by roundtripping, log on error\n"
289 " 7. Check for orphaned CNIDs in database (requires -e)\n"
290 " 8. Open and close adouble files\n"
291 " Options: -c Don't create .AppleDouble stuff, only cleanup orphaned.\n"
292 " -f wipe database and rebuild from IDs stored in AppleDouble files,\n"
293 " only available for volumes without 'nocnidcache' option. Implies -e.\n\n"
294 " -u Prepare upgrade:\n"
295 " Before installing an upgraded version of Netatalk that is linked against\n"
296 " a newer BerkeleyDB lib, run `dbd -u ...` from the OLD Netatalk pior to\n"
297 " upgrading on all volumes. This removes the BerkleyDB environment.\n"
298 " On exit cnid_dbd does this automatically, so normally calling dbd -u should not be necessary.\n\n"
300 " -e only work on inactive volumes and lock them (exclusive)\n"
301 " -x rebuild indexes (just for completeness, mostly useless!)\n"
302 " -t show statistics while running\n"
305 "For -r -f restore of the CNID database from the adouble files, the CNID must of course\n"
306 "be synched to them files first with a plain -r rebuild !\n"
310 int main(int argc, char **argv)
312 int c, lockfd, ret = -1;
313 int dump=0, scan=0, rebuild=0, prep_upgrade=0, rebuildindexes=0, dumpindexes=0, force=0;
314 dbd_flags_t flags = 0;
316 struct volinfo volinfo;
319 if (geteuid() != 0) {
323 /* Inhereting perms in ad_mkdir etc requires this */
326 while ((c = getopt(argc, argv, ":cdefinrstuvx")) != -1) {
329 flags |= DBD_FLAGS_CLEANUP;
339 flags |= DBD_FLAGS_SCAN;
342 nocniddb = 1; /* FIXME: this could/should be a flag too for consistency */
348 flags |= DBD_FLAGS_STATS;
358 flags |= DBD_FLAGS_EXCL;
366 flags |= DBD_FLAGS_FORCE | DBD_FLAGS_EXCL;
376 if ((dump + scan + rebuild + prep_upgrade) != 1) {
381 if ( (optind + 1) != argc ) {
385 volpath = argv[optind];
387 setvbuf(stdout, (char *) NULL, _IONBF, 0);
390 if ((cdir = open(".", O_RDONLY)) < 0) {
391 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
395 /* Setup signal handling */
398 /* Setup logging. Should be portable among *NIXes */
400 setuplog("default log_info /dev/tty");
402 setuplog("default log_debug /dev/tty");
404 /* Load .volinfo file */
405 if (loadvolinfo(volpath, &volinfo) == -1) {
406 dbd_log( LOGSTD, "Not a Netatalk volume at '%s', no .volinfo file at '%s/.AppleDesktop/.volinfo' or unknown volume options", volpath, volpath);
409 if (vol_load_charsets(&volinfo) == -1) {
410 dbd_log( LOGSTD, "Error loading charsets!");
414 /* Sanity checks to ensure we can touch this volume */
415 if (volinfo.v_vfs_ea != AFPVOL_EA_AD && volinfo.v_vfs_ea != AFPVOL_EA_SYS) {
416 dbd_log( LOGSTD, "Unknown Extended Attributes option: %u", volinfo.v_vfs_ea);
420 /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
421 if ( (strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
422 dbd_log( LOGSTD, "Volume pathname too long");
425 strncpy(dbpath, volinfo.v_dbpath, PATH_MAX - 9 - 1);
426 strcat(dbpath, "/.AppleDB");
428 /* Check or create dbpath */
429 int dbdirfd = open(dbpath, O_RDONLY);
430 if (dbdirfd == -1 && errno == ENOENT) {
431 if (errno == ENOENT) {
432 if ((mkdir(dbpath, 0755)) != 0) {
433 dbd_log( LOGSTD, "Can't create .AppleDB for \"%s\": %s", dbpath, strerror(errno));
437 dbd_log( LOGSTD, "Somethings wrong with .AppleDB for \"%s\", giving up: %s", dbpath, strerror(errno));
445 if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
448 /* Couldn't get exclusive lock, try shared lock if -e wasn't requested */
450 dbd_log(LOGSTD, "Database is in use and exlusive was requested");
453 if ((db_locked = get_lock(LOCK_SHRD, dbpath)) != 1)
458 /* Prepare upgrade ? */
460 if (dbif_prep_upgrade(dbpath))
465 /* Check if -f is requested and wipe db if yes */
466 if ((flags & DBD_FLAGS_FORCE) && rebuild && (volinfo.v_flags & AFPVOL_CACHE)) {
467 char cmd[8 + MAXPATHLEN];
468 if ((db_locked = get_lock(LOCK_FREE, NULL)) != 0)
470 snprintf(cmd, 8 + MAXPATHLEN, "rm -f %s/*", dbpath);
471 dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
473 dbd_log( LOGDEBUG, "Removed old database.");
474 if ((db_locked = get_lock(LOCK_EXCL, NULL)) == -1)
479 Lets start with the BerkeleyDB stuff
482 if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
485 if (dbif_env_open(dbd,
487 exclusive ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
488 dbd_log( LOGSTD, "error opening database!");
493 dbd_log( LOGDEBUG, "Finished recovery.");
495 if (dbif_open(dbd, NULL, rebuildindexes) < 0) {
500 if (dbd_stamp(dbd) < 0) {
506 /* Now execute given command scan|rebuild|dump */
507 if (dump && ! nocniddb) {
508 if (dbif_dump(dbd, dumpindexes) < 0) {
509 dbd_log( LOGSTD, "Error dumping database");
511 } else if ((rebuild && ! nocniddb) || scan) {
512 if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
513 dbd_log( LOGSTD, "Error repairing database.");
518 dbd_log(LOGDEBUG, "Closing db");
520 if (dbif_close(dbd) < 0) {
521 dbd_log( LOGSTD, "Error closing database");
532 if ((fchdir(cdir)) < 0)
533 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));