2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
9 * Copyright (c) 2007, The Storage Networking Industry Association.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * - Neither the name of The Storage Networking Industry Association (SNIA)
23 * nor the names of its contributors may be used to endorse or promote
24 * products derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/queue.h>
43 #include <sys/syslog.h>
45 #include "tlm_proto.h"
47 #define HL_DBG_INIT 0x0001
48 #define HL_DBG_CLEANUP 0x0002
49 #define HL_DBG_GET 0x0004
50 #define HL_DBG_ADD 0x0008
52 static int hardlink_q_dbg
= -1;
58 struct hardlink_q
*qhead
;
60 qhead
= (struct hardlink_q
*)malloc(sizeof (struct hardlink_q
));
65 if (hardlink_q_dbg
& HL_DBG_INIT
)
66 NDMP_LOG(LOG_DEBUG
, "qhead = %p", qhead
);
72 hardlink_q_cleanup(struct hardlink_q
*hl_q
)
74 struct hardlink_node
*hl
;
76 if (hardlink_q_dbg
& HL_DBG_CLEANUP
)
77 NDMP_LOG(LOG_DEBUG
, "(1): qhead = %p", hl_q
);
82 while (!SLIST_EMPTY(hl_q
)) {
83 hl
= SLIST_FIRST(hl_q
);
85 if (hardlink_q_dbg
& HL_DBG_CLEANUP
)
86 NDMP_LOG(LOG_DEBUG
, "(2): remove node, inode = %lu",
89 SLIST_REMOVE_HEAD(hl_q
, next_hardlink
);
91 /* remove the temporary file */
94 NDMP_LOG(LOG_DEBUG
, "(3): remove temp file %s",
96 if (remove(hl
->path
)) {
98 "error removing temp file");
101 NDMP_LOG(LOG_DEBUG
, "no link name, inode = %lu",
114 * Return 0 if a list node has the same inode, and initialize offset and path
115 * with the information in the list node.
116 * Return -1 if no matching node is found.
119 hardlink_q_get(struct hardlink_q
*hl_q
, unsigned long inode
,
120 unsigned long long *offset
, char **path
)
122 struct hardlink_node
*hl
;
124 if (hardlink_q_dbg
& HL_DBG_GET
)
125 NDMP_LOG(LOG_DEBUG
, "(1): qhead = %p, inode = %lu",
131 SLIST_FOREACH(hl
, hl_q
, next_hardlink
) {
132 if (hardlink_q_dbg
& HL_DBG_GET
)
133 NDMP_LOG(LOG_DEBUG
, "(2): checking, inode = %lu",
136 if (hl
->inode
!= inode
)
140 *offset
= hl
->offset
;
152 * Add a node to hardlink_q. Reject a duplicated entry.
154 * Return 0 if successful, and -1 if failed.
157 hardlink_q_add(struct hardlink_q
*hl_q
, unsigned long inode
,
158 unsigned long long offset
, char *path
, int is_tmp_file
)
160 struct hardlink_node
*hl
;
162 if (hardlink_q_dbg
& HL_DBG_ADD
)
164 "(1): qhead = %p, inode = %lu, path = %p (%s)",
165 hl_q
, inode
, path
, path
? path
: "(--)");
170 if (!hardlink_q_get(hl_q
, inode
, 0, 0)) {
171 NDMP_LOG(LOG_DEBUG
, "hardlink (inode = %lu) exists in queue %p",
176 hl
= (struct hardlink_node
*)malloc(sizeof (struct hardlink_node
));
182 hl
->is_tmp
= is_tmp_file
;
184 hl
->path
= strdup(path
);
188 if (hardlink_q_dbg
& HL_DBG_ADD
)
190 "(2): added node, inode = %lu, path = %p (%s)",
191 hl
->inode
, hl
->path
, hl
->path
? hl
->path
: "(--)");
193 SLIST_INSERT_HEAD(hl_q
, hl
, next_hardlink
);
199 hardlink_q_dump(struct hardlink_q
*hl_q
)
201 struct hardlink_node
*hl
;
206 (void) printf("Dumping hardlink_q, head = %p:\n", (void *) hl_q
);
208 SLIST_FOREACH(hl
, hl_q
, next_hardlink
)
210 "\t node = %lu, offset = %llu, path = %s, is_tmp = %d\n",
211 hl
->inode
, hl
->offset
, hl
->path
? hl
->path
: "--",