1*2654012fSReza Sabdar /*
2*2654012fSReza Sabdar * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3*2654012fSReza Sabdar * Use is subject to license terms.
4*2654012fSReza Sabdar */
5*2654012fSReza Sabdar
6*2654012fSReza Sabdar /*
7*2654012fSReza Sabdar * BSD 3 Clause License
8*2654012fSReza Sabdar *
9*2654012fSReza Sabdar * Copyright (c) 2007, The Storage Networking Industry Association.
10*2654012fSReza Sabdar *
11*2654012fSReza Sabdar * Redistribution and use in source and binary forms, with or without
12*2654012fSReza Sabdar * modification, are permitted provided that the following conditions
13*2654012fSReza Sabdar * are met:
14*2654012fSReza Sabdar * - Redistributions of source code must retain the above copyright
15*2654012fSReza Sabdar * notice, this list of conditions and the following disclaimer.
16*2654012fSReza Sabdar *
17*2654012fSReza Sabdar * - Redistributions in binary form must reproduce the above copyright
18*2654012fSReza Sabdar * notice, this list of conditions and the following disclaimer in
19*2654012fSReza Sabdar * the documentation and/or other materials provided with the
20*2654012fSReza Sabdar * distribution.
21*2654012fSReza Sabdar *
22*2654012fSReza Sabdar * - Neither the name of The Storage Networking Industry Association (SNIA)
23*2654012fSReza Sabdar * nor the names of its contributors may be used to endorse or promote
24*2654012fSReza Sabdar * products derived from this software without specific prior written
25*2654012fSReza Sabdar * permission.
26*2654012fSReza Sabdar *
27*2654012fSReza Sabdar * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28*2654012fSReza Sabdar * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29*2654012fSReza Sabdar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30*2654012fSReza Sabdar * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31*2654012fSReza Sabdar * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32*2654012fSReza Sabdar * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33*2654012fSReza Sabdar * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34*2654012fSReza Sabdar * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35*2654012fSReza Sabdar * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36*2654012fSReza Sabdar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37*2654012fSReza Sabdar * POSSIBILITY OF SUCH DAMAGE.
38*2654012fSReza Sabdar */
39*2654012fSReza Sabdar #include <stdlib.h>
40*2654012fSReza Sabdar #include <stdio.h>
41*2654012fSReza Sabdar #include <string.h>
42*2654012fSReza Sabdar #include <sys/queue.h>
43*2654012fSReza Sabdar #include <sys/syslog.h>
44*2654012fSReza Sabdar #include "tlm.h"
45*2654012fSReza Sabdar #include "tlm_proto.h"
46*2654012fSReza Sabdar
47*2654012fSReza Sabdar #define HL_DBG_INIT 0x0001
48*2654012fSReza Sabdar #define HL_DBG_CLEANUP 0x0002
49*2654012fSReza Sabdar #define HL_DBG_GET 0x0004
50*2654012fSReza Sabdar #define HL_DBG_ADD 0x0008
51*2654012fSReza Sabdar
52*2654012fSReza Sabdar static int hardlink_q_dbg = -1;
53*2654012fSReza Sabdar
54*2654012fSReza Sabdar
55*2654012fSReza Sabdar struct hardlink_q *
hardlink_q_init()56*2654012fSReza Sabdar hardlink_q_init()
57*2654012fSReza Sabdar {
58*2654012fSReza Sabdar struct hardlink_q *qhead;
59*2654012fSReza Sabdar
60*2654012fSReza Sabdar qhead = (struct hardlink_q *)malloc(sizeof (struct hardlink_q));
61*2654012fSReza Sabdar if (qhead) {
62*2654012fSReza Sabdar SLIST_INIT(qhead);
63*2654012fSReza Sabdar }
64*2654012fSReza Sabdar
65*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_INIT)
66*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "qhead = %p", qhead);
67*2654012fSReza Sabdar
68*2654012fSReza Sabdar return (qhead);
69*2654012fSReza Sabdar }
70*2654012fSReza Sabdar
71*2654012fSReza Sabdar void
hardlink_q_cleanup(struct hardlink_q * hl_q)72*2654012fSReza Sabdar hardlink_q_cleanup(struct hardlink_q *hl_q)
73*2654012fSReza Sabdar {
74*2654012fSReza Sabdar struct hardlink_node *hl;
75*2654012fSReza Sabdar
76*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_CLEANUP)
77*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "(1): qhead = %p", hl_q);
78*2654012fSReza Sabdar
79*2654012fSReza Sabdar if (!hl_q)
80*2654012fSReza Sabdar return;
81*2654012fSReza Sabdar
82*2654012fSReza Sabdar while (!SLIST_EMPTY(hl_q)) {
83*2654012fSReza Sabdar hl = SLIST_FIRST(hl_q);
84*2654012fSReza Sabdar
85*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_CLEANUP)
86*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "(2): remove node, inode = %lu",
87*2654012fSReza Sabdar hl->inode);
88*2654012fSReza Sabdar
89*2654012fSReza Sabdar SLIST_REMOVE_HEAD(hl_q, next_hardlink);
90*2654012fSReza Sabdar
91*2654012fSReza Sabdar /* remove the temporary file */
92*2654012fSReza Sabdar if (hl->is_tmp) {
93*2654012fSReza Sabdar if (hl->path) {
94*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "(3): remove temp file %s",
95*2654012fSReza Sabdar hl->path);
96*2654012fSReza Sabdar if (remove(hl->path)) {
97*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG,
98*2654012fSReza Sabdar "error removing temp file");
99*2654012fSReza Sabdar }
100*2654012fSReza Sabdar } else {
101*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "no link name, inode = %lu",
102*2654012fSReza Sabdar hl->inode);
103*2654012fSReza Sabdar }
104*2654012fSReza Sabdar }
105*2654012fSReza Sabdar
106*2654012fSReza Sabdar if (hl->path)
107*2654012fSReza Sabdar free(hl->path);
108*2654012fSReza Sabdar free(hl);
109*2654012fSReza Sabdar }
110*2654012fSReza Sabdar
111*2654012fSReza Sabdar free(hl_q);
112*2654012fSReza Sabdar }
113*2654012fSReza Sabdar
114*2654012fSReza Sabdar /*
115*2654012fSReza Sabdar * Return 0 if a list node has the same inode, and initialize offset and path
116*2654012fSReza Sabdar * with the information in the list node.
117*2654012fSReza Sabdar * Return -1 if no matching node is found.
118*2654012fSReza Sabdar */
119*2654012fSReza Sabdar int
hardlink_q_get(struct hardlink_q * hl_q,unsigned long inode,unsigned long long * offset,char ** path)120*2654012fSReza Sabdar hardlink_q_get(struct hardlink_q *hl_q, unsigned long inode,
121*2654012fSReza Sabdar unsigned long long *offset, char **path)
122*2654012fSReza Sabdar {
123*2654012fSReza Sabdar struct hardlink_node *hl;
124*2654012fSReza Sabdar
125*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_GET)
126*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "(1): qhead = %p, inode = %lu",
127*2654012fSReza Sabdar hl_q, inode);
128*2654012fSReza Sabdar
129*2654012fSReza Sabdar if (!hl_q)
130*2654012fSReza Sabdar return (-1);
131*2654012fSReza Sabdar
132*2654012fSReza Sabdar SLIST_FOREACH(hl, hl_q, next_hardlink) {
133*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_GET)
134*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "(2): checking, inode = %lu",
135*2654012fSReza Sabdar hl->inode);
136*2654012fSReza Sabdar
137*2654012fSReza Sabdar if (hl->inode != inode)
138*2654012fSReza Sabdar continue;
139*2654012fSReza Sabdar
140*2654012fSReza Sabdar if (offset)
141*2654012fSReza Sabdar *offset = hl->offset;
142*2654012fSReza Sabdar
143*2654012fSReza Sabdar if (path)
144*2654012fSReza Sabdar *path = hl->path;
145*2654012fSReza Sabdar
146*2654012fSReza Sabdar return (0);
147*2654012fSReza Sabdar }
148*2654012fSReza Sabdar
149*2654012fSReza Sabdar return (-1);
150*2654012fSReza Sabdar }
151*2654012fSReza Sabdar
152*2654012fSReza Sabdar /*
153*2654012fSReza Sabdar * Add a node to hardlink_q. Reject a duplicated entry.
154*2654012fSReza Sabdar *
155*2654012fSReza Sabdar * Return 0 if successful, and -1 if failed.
156*2654012fSReza Sabdar */
157*2654012fSReza Sabdar int
hardlink_q_add(struct hardlink_q * hl_q,unsigned long inode,unsigned long long offset,char * path,int is_tmp_file)158*2654012fSReza Sabdar hardlink_q_add(struct hardlink_q *hl_q, unsigned long inode,
159*2654012fSReza Sabdar unsigned long long offset, char *path, int is_tmp_file)
160*2654012fSReza Sabdar {
161*2654012fSReza Sabdar struct hardlink_node *hl;
162*2654012fSReza Sabdar
163*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_ADD)
164*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG,
165*2654012fSReza Sabdar "(1): qhead = %p, inode = %lu, path = %p (%s)",
166*2654012fSReza Sabdar hl_q, inode, path, path? path : "(--)");
167*2654012fSReza Sabdar
168*2654012fSReza Sabdar if (!hl_q)
169*2654012fSReza Sabdar return (-1);
170*2654012fSReza Sabdar
171*2654012fSReza Sabdar if (!hardlink_q_get(hl_q, inode, 0, 0)) {
172*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG, "hardlink (inode = %lu) exists in queue %p",
173*2654012fSReza Sabdar inode, hl_q);
174*2654012fSReza Sabdar return (-1);
175*2654012fSReza Sabdar }
176*2654012fSReza Sabdar
177*2654012fSReza Sabdar hl = (struct hardlink_node *)malloc(sizeof (struct hardlink_node));
178*2654012fSReza Sabdar if (!hl)
179*2654012fSReza Sabdar return (-1);
180*2654012fSReza Sabdar
181*2654012fSReza Sabdar hl->inode = inode;
182*2654012fSReza Sabdar hl->offset = offset;
183*2654012fSReza Sabdar hl->is_tmp = is_tmp_file;
184*2654012fSReza Sabdar if (path)
185*2654012fSReza Sabdar hl->path = strdup(path);
186*2654012fSReza Sabdar else
187*2654012fSReza Sabdar hl->path = NULL;
188*2654012fSReza Sabdar
189*2654012fSReza Sabdar if (hardlink_q_dbg & HL_DBG_ADD)
190*2654012fSReza Sabdar NDMP_LOG(LOG_DEBUG,
191*2654012fSReza Sabdar "(2): added node, inode = %lu, path = %p (%s)",
192*2654012fSReza Sabdar hl->inode, hl->path, hl->path? hl->path : "(--)");
193*2654012fSReza Sabdar
194*2654012fSReza Sabdar SLIST_INSERT_HEAD(hl_q, hl, next_hardlink);
195*2654012fSReza Sabdar
196*2654012fSReza Sabdar return (0);
197*2654012fSReza Sabdar }
198*2654012fSReza Sabdar
199*2654012fSReza Sabdar int
hardlink_q_dump(struct hardlink_q * hl_q)200*2654012fSReza Sabdar hardlink_q_dump(struct hardlink_q *hl_q)
201*2654012fSReza Sabdar {
202*2654012fSReza Sabdar struct hardlink_node *hl;
203*2654012fSReza Sabdar
204*2654012fSReza Sabdar if (!hl_q)
205*2654012fSReza Sabdar return (0);
206*2654012fSReza Sabdar
207*2654012fSReza Sabdar (void) printf("Dumping hardlink_q, head = %p:\n", (void *) hl_q);
208*2654012fSReza Sabdar
209*2654012fSReza Sabdar SLIST_FOREACH(hl, hl_q, next_hardlink)
210*2654012fSReza Sabdar (void) printf(
211*2654012fSReza Sabdar "\t node = %lu, offset = %llu, path = %s, is_tmp = %d\n",
212*2654012fSReza Sabdar hl->inode, hl->offset, hl->path? hl->path : "--",
213*2654012fSReza Sabdar hl->is_tmp);
214*2654012fSReza Sabdar
215*2654012fSReza Sabdar return (0);
216*2654012fSReza Sabdar }
217