2 * Routines to support hard-linking.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2004, 2005, 2006 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
30 extern int make_backups;
31 extern int protocol_version;
32 extern int remove_source_files;
33 extern int stdout_format_has_i;
34 extern int maybe_ATTRS_REPORT;
35 extern char *basis_dir[];
36 extern struct file_list *the_file_list;
38 #ifdef SUPPORT_HARD_LINKS
40 #define HASH_LOAD_LIMIT(size) ((size)*3/4)
41 #define FPTR(i) (the_file_list->files[i])
46 struct idev_node *buckets;
49 static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key);
51 /* Starting with protocol 30, we use a simple hashtable on the sending side
52 * for hashing the st_dev and st_ino info. The receiving side gets told
53 * (via flags and a "group index") which items are hard-linked together, so
54 * we can avoid the pool of dev+inode data. */
56 static struct ihash_table *ihash_create(int size)
58 struct ihash_table *tbl;
60 /* Pick a power of 2 that can hold the requested size. */
61 if (size & (size-1)) {
68 if (!(tbl = new(struct ihash_table))
69 || !(tbl->buckets = new_array(struct idev_node, size)))
70 out_of_memory("ihash_create");
71 memset(tbl->buckets, 0, size * sizeof tbl->buckets[0]);
78 static void ihash_destroy(struct ihash_table *tbl)
84 void init_hard_links(void)
86 dev_tbl = ihash_create(16);
89 static void expand_ihash(struct ihash_table *tbl)
91 struct idev_node *old_buckets;
92 int size = tbl->size * 2;
95 old_buckets = tbl->buckets;
96 if (!(tbl->buckets = new_array(struct idev_node, size)))
97 out_of_memory("ihash_create");
98 memset(tbl->buckets, 0, size * sizeof tbl->buckets[0]);
102 for (i = size / 2; i-- > 0; ) {
103 int64 key = old_buckets[i].key;
106 ihash_node(tbl, key)->data = old_buckets[i].data;
112 /* This returns the node for the indicated key, either newly created,
113 * or already existing. */
114 static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key)
118 if (tbl->entries > HASH_LOAD_LIMIT(tbl->size))
122 /* Based on Jenkins One-at-a-time hash. */
124 uchar *keyp = (uchar*)&key;
127 for (bkt = 0, i = 0; i < SIZEOF_INT64; i++) {
137 #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
138 /* Based on Jenkins hashword() from lookup3.c. */
142 /* Set up the internal state */
143 a = b = c = 0xdeadbeef + (8 << 2);
145 b += (uint32)(key >> 32);
147 c ^= b; c -= rot(b, 14);
148 a ^= c; a -= rot(c, 11);
149 b ^= a; b -= rot(a, 25);
150 c ^= b; c -= rot(b, 16);
151 a ^= c; a -= rot(c, 4);
152 b ^= a; b -= rot(a, 14);
153 c ^= b; c -= rot(b, 24);
158 /* If it already exists, return it. */
160 bkt &= tbl->size - 1;
161 if (tbl->buckets[bkt].key == key)
162 return &tbl->buckets[bkt];
163 if (tbl->buckets[bkt].key == 0)
168 /* Otherwise, take over this empty spot and then return it. */
169 tbl->buckets[bkt].key = key;
171 return &tbl->buckets[bkt];
174 struct idev_node *idev_node(int64 dev, int64 ino)
176 static struct idev_node *dev_node = NULL;
177 struct ihash_table *tbl;
179 if (!dev_node || dev_node->key != dev) {
180 /* We keep a separate hash table of inodes for every device. */
181 dev_node = ihash_node(dev_tbl, dev);
182 if (!(tbl = dev_node->data))
183 tbl = dev_node->data = ihash_create(512);
185 tbl = dev_node->data;
187 return ihash_node(tbl, ino);
190 void idev_destroy(void)
194 for (i = 0; i < dev_tbl->size; i++) {
195 if (dev_tbl->buckets[i].data)
196 ihash_destroy(dev_tbl->buckets[i].data);
199 ihash_destroy(dev_tbl);
202 static int hlink_compare_gnum(int *int1, int *int2)
204 struct file_struct *f1 = FPTR(*int1);
205 struct file_struct *f2 = FPTR(*int2);
206 int gnum1 = F_HL_GNUM(f1);
207 int gnum2 = F_HL_GNUM(f2);
210 return gnum1 > gnum2 ? 1 : -1;
212 return *int1 > *int2 ? 1 : -1;
215 static void match_gnums(int32 *ndx_list, int ndx_count)
218 struct file_struct *file, *file_next;
221 qsort(ndx_list, ndx_count, sizeof ndx_list[0],
222 (int (*)()) hlink_compare_gnum);
224 for (from = 0; from < ndx_count; from++) {
225 for (file = FPTR(ndx_list[from]), gnum = F_HL_GNUM(file), prev = -1;
227 file = file_next, gnum = gnum_next, prev = ndx_list[from++])
229 file_next = FPTR(ndx_list[from+1]);
230 gnum_next = F_HL_GNUM(file_next);
231 if (gnum != gnum_next)
234 file->flags |= FLAG_HLINK_FIRST;
235 F_HL_PREV(file) = prev;
238 file->flags &= ~FLAG_HLINKED;
240 file->flags |= FLAG_HLINK_LAST;
241 F_HL_PREV(file) = prev;
246 /* Analyze the hard-links in the file-list by creating a list of all the
247 * items that have hlink data, sorting them, and matching up identical
248 * values into clusters. These will be a single linked list from last
249 * to first when we're done. */
250 void match_hard_links(void)
252 int i, ndx_count = 0;
255 if (!(ndx_list = new_array(int32, the_file_list->count)))
256 out_of_memory("match_hard_links");
258 for (i = 0; i < the_file_list->count; i++) {
259 if (F_IS_HLINKED(FPTR(i)))
260 ndx_list[ndx_count++] = i;
264 match_gnums(ndx_list, ndx_count);
267 if (protocol_version < 30)
271 static int maybe_hard_link(struct file_struct *file, int ndx,
272 const char *fname, int statret, STRUCT_STAT *stp,
273 const char *oldname, STRUCT_STAT *old_stp,
274 const char *realname, int itemizing, enum logcode code)
277 if (stp->st_dev == old_stp->st_dev
278 && stp->st_ino == old_stp->st_ino) {
280 itemize(file, ndx, statret, stp,
281 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
284 if (verbose > 1 && maybe_ATTRS_REPORT)
285 rprintf(FCLIENT, "%s is uptodate\n", fname);
286 file->flags |= FLAG_HLINK_DONE;
290 if (!make_backup(fname))
292 } else if (robust_unlink(fname)) {
293 rsyserr(FERROR, errno, "unlink %s failed",
299 if (hard_link_one(file, fname, oldname, 0)) {
301 itemize(file, ndx, statret, stp,
302 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
305 if (code != FNONE && verbose)
306 rprintf(code, "%s => %s\n", fname, realname);
312 /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
313 * 0 = process the file, 1 = skip the file, -1 = error occurred. */
314 int hard_link_check(struct file_struct *file, int ndx, const char *fname,
315 int statret, STRUCT_STAT *stp, int itemizing,
319 char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname;
320 int alt_dest, prev_ndx = F_HL_PREV(file);
321 struct file_struct *prev_file = FPTR(prev_ndx);
323 /* Is the previous link is not complete yet? */
324 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
325 /* Is the previous link being transferred? */
326 if (prev_file->flags & FLAG_SENT) {
327 /* Add ourselves to the list of files that will be
328 * updated when the transfer completes, and mark
329 * ourself as waiting for the transfer. */
330 F_HL_PREV(file) = F_HL_PREV(prev_file);
331 F_HL_PREV(prev_file) = ndx;
332 file->flags |= FLAG_SENT;
338 /* There is a finished file to link with! */
339 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
340 /* The previous previous will be marked with FIRST. */
341 prev_ndx = F_HL_PREV(prev_file);
342 prev_file = FPTR(prev_ndx);
343 /* Update our previous pointer to point to the first. */
344 F_HL_PREV(file) = prev_ndx;
346 alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
347 if (alt_dest >= 0 && dry_run) {
348 pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest],
349 f_name(prev_file, NULL));
350 f_name(prev_file, altbuf);
353 f_name(prev_file, prev_name);
354 realname = prev_name;
357 if (link_stat(prev_name, &prev_st, 0) < 0) {
358 rsyserr(FERROR, errno, "stat %s failed",
359 full_fname(prev_name));
363 if (statret < 0 && basis_dir[0] != NULL) {
364 /* If we match an alt-dest item, we don't output this as a change. */
365 char cmpbuf[MAXPATHLEN];
369 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
370 if (link_stat(cmpbuf, &alt_st, 0) < 0)
373 if (prev_st.st_dev != alt_st.st_dev
374 || prev_st.st_ino != alt_st.st_ino)
378 if (verbose < 2 || !stdout_format_has_i) {
381 if (verbose > 1 && maybe_ATTRS_REPORT)
382 rprintf(FCLIENT, "%s is uptodate\n", fname);
386 if (!unchanged_file(cmpbuf, file, &alt_st))
390 if (unchanged_attrs(file, &alt_st))
392 } while (basis_dir[++j] != NULL);
395 if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st,
396 realname, itemizing, code) < 0)
399 if (remove_source_files == 1 && do_xfers)
400 send_msg_int(MSG_SUCCESS, ndx);
405 int hard_link_one(struct file_struct *file, const char *fname,
406 const char *oldname, int terse)
408 if (do_link(oldname, fname) < 0) {
416 rsyserr(code, errno, "link %s => %s failed",
417 full_fname(fname), oldname);
421 file->flags |= FLAG_HLINK_DONE;
426 void finish_hard_link(struct file_struct *file, const char *fname,
427 STRUCT_STAT *stp, int itemizing, enum logcode code,
430 STRUCT_STAT st, prev_st;
431 char alt_name[MAXPATHLEN], *prev_name;
432 const char *our_name;
433 int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
435 if (stp == NULL && prev_ndx >= 0) {
436 if (link_stat(fname, &st, 0) < 0) {
437 rsyserr(FERROR, errno, "stat %s failed",
444 /* FIRST combined with DONE means we were the first to get done. */
445 file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
446 F_HL_PREV(file) = alt_dest;
447 if (alt_dest >= 0 && dry_run) {
448 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
454 while ((ndx = prev_ndx) >= 0) {
456 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
457 prev_ndx = F_HL_PREV(file);
458 prev_name = f_name(file, NULL);
459 prev_statret = link_stat(prev_name, &prev_st, 0);
460 if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st,
461 our_name, stp, fname, itemizing, code) < 0)
463 if (remove_source_files == 1 && do_xfers)
464 send_msg_int(MSG_SUCCESS, ndx);