File: dbd_lookup.c

package info (click to toggle)
netatalk 3.1.12~ds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,756 kB
  • sloc: ansic: 104,976; sh: 8,247; xml: 7,394; perl: 1,936; makefile: 1,430; python: 1,342; yacc: 309; lex: 49
file content (316 lines) | stat: -rw-r--r-- 10,933 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 *
 * Copyright (C) Joerg Lenneis 2003
 * Copyright (C) Frank Lahm 2009
 * All Rights Reserved.  See COPYING.
 */

/* 
CNID salvation spec:
general rule: better safe then sorry, so we always delete CNIDs and assign
new ones in case of a lookup mismatch. afpd also sends us the CNID found
in the adouble file. In certain cases we can use this hint to determince
the right CNID.


The lines...

Id  Did T   Dev Inode   Name
============================
a   b   c   d   e       name1
-->
f   g   h   i   h       name2

...are the expected results of certain operations. (f) is the speced CNID, in some
cases it's only intermediate as described in the text and is overridden by another
spec.

1) UNIX rename (via mv) or inode reusage(!)
-------------------------------------------
Name is possibly changed (rename case) but inode is the same.
We should try to keep the CNID, but we can't, because inode reusage is probably
much to frequent.

rename:
15  2   f   1   1       file
-->
15  x   f   1   1       renamedfile

inode reusage:
15  2   f   1   1       file
-->
16  y   f   1   1       inodereusagefile

Result in dbd_lookup (-: not found, +: found):
+ devino
- didname

Possible solution:
None. Delete old data, file gets new CNID in both cases (rename and inode).
If we got a hint and hint matches the CNID from devino we keep it and update
the record.

2) UNIX mv from one folder to another
----------------------------------------
Name is unchanged and inode stays the same, but DID is different.
We should try to keep the CNID.

15  2   f   1   1       file
-->
15  x   f   1   1       file

Result in dbd_lookup:
+ devino
- didname

Possible solution:
strcmp names, if they match keep CNID. Unfortunately this also can't be
distinguished from a new file with a reused inode. So me must assign
a new CNID.
If we got a hint and hint matches the CNID from devino we keep it and update
the record.

3) Restore from backup i.e. change of inode number -- or emacs
------------------------------------------------------------

15  2   f   1   1       file
-->
15  2   f   1   2       file

Result in dbd_lookup:
- devino
+ didname

Possible fixup solution:
test-suite test235 tests and ensures that the CNID is _changed_. The reason for
this is somewhat lost in time, but nevertheless we believe our test suite.

Similar things happen with emas: emacs uses a backup file (file~). When saving
because of inode reusage of the fs, both files most likely exchange inodes.

15  2   f   1   1       file
16  2   f   1   2       file~
--> this would be nice:
15  2   f   1   2       file
16  2   f   1   1       file~
--> but for the reasons described above we must implement
17  2   f   1   2       file
18  2   f   1   1       file~

Result in dbd_lookup for the emacs case:
+ devino --> CNID: 16
+ didname -> CNID: 15
devino search and didname search result in different CNIDs !!

Possible fixup solution:
to be safe we must assign new CNIDs to both files.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */


#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <errno.h>
#include <arpa/inet.h>

#include <atalk/logger.h>
#include <atalk/cnid_bdb_private.h>
#include <atalk/cnid.h>

#include "pack.h"
#include "dbif.h"
#include "dbd.h"

/*
 *  This returns the CNID corresponding to a particular file.  It will also fix
 *  up the database if there's a problem.
 */

int dbd_lookup(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
{
    unsigned char *buf;
    DBT key, devdata, diddata;
    int devino = 1, didname = 1; 
    int rc;
    cnid_t id_devino, id_didname;
    u_int32_t type_devino  = (unsigned)-1;
    u_int32_t type_didname = (unsigned)-1;
    int update = 0;
    
    memset(&key, 0, sizeof(key));
    memset(&diddata, 0, sizeof(diddata));
    memset(&devdata, 0, sizeof(devdata));

    rply->namelen = 0;
    rply->cnid = 0;

    LOG(log_maxdebug, logtype_cnid, "dbd_lookup(): START");
    
    buf = pack_cnid_data(rqst); 

    /* Look for a CNID.  We have two options: dev/ino or did/name.  If we
       only get a match in one of them, that means a file has moved. */
    key.data = buf + CNID_DEVINO_OFS;
    key.size = CNID_DEVINO_LEN;

    if ((rc = dbif_get(dbd, DBIF_IDX_DEVINO, &key, &devdata, 0))  < 0) {
        LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
            ntohl(rqst->did), rqst->name);
        rply->result = CNID_DBD_RES_ERR_DB;
        return -1;
    }
    if (rc == 0) {
        devino = 0;
    }
    else {
        memcpy(&id_devino, devdata.data, sizeof(rply->cnid));
        memcpy(&type_devino, (char *)devdata.data +CNID_TYPE_OFS, sizeof(type_devino));
        type_devino = ntohl(type_devino);
    }

    key.data = buf + CNID_DID_OFS;
    key.size = CNID_DID_LEN + rqst->namelen + 1;

    if ((rc = dbif_get(dbd, DBIF_IDX_DIDNAME, &key, &diddata, 0))  < 0) {
        LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
            ntohl(rqst->did), rqst->name);
        rply->result = CNID_DBD_RES_ERR_DB;
        return -1;
    }
    if (rc == 0) {
        didname = 0;
    }
    else {
        memcpy(&id_didname, diddata.data, sizeof(rply->cnid));
        memcpy(&type_didname, (char *)diddata.data +CNID_TYPE_OFS, sizeof(type_didname));
        type_didname = ntohl(type_didname);
    }

    LOG(log_maxdebug, logtype_cnid, "dbd_lookup(name:'%s', did:%u, dev/ino:0x%llx/0x%llx) {devino: %u, didname: %u}", 
        rqst->name, ntohl(rqst->did), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, devino, didname);

    /* Have we found anything at all ? */
    if (!devino && !didname) {  
        /* nothing found */
        LOG(log_debug, logtype_cnid, "dbd_lookup: name: '%s', did: %u, dev/ino: 0x%llx/0x%llx is not in the CNID database", 
            rqst->name, ntohl(rqst->did), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
        rply->result = CNID_DBD_RES_NOTFOUND;
        return 1;
    }

    /* Check for type (file/dir) mismatch */
    if ((devino && (type_devino != rqst->type)) || (didname && (type_didname != rqst->type))) {

        if (devino && (type_devino != rqst->type)) {
            /* one is a dir one is a file, remove from db */

            LOG(log_debug, logtype_cnid, "dbd_lookup(name:'%s', did:%u, dev/ino:0x%llx/0x%llx): type mismatch for devino", 
                rqst->name, ntohl(rqst->did), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);

            rqst->cnid = id_devino;
            rc = dbd_delete(dbd, rqst, rply, DBIF_CNID);
            rc += dbd_delete(dbd, rqst, rply, DBIF_IDX_DEVINO);
            rc += dbd_delete(dbd, rqst, rply, DBIF_IDX_DIDNAME);
            if (rc < 0) {
                LOG(log_error, logtype_cnid, "dbd_lookup(name:'%s', did:%u, dev/ino:0x%llx/0x%llx): error deleting type mismatch for devino", 
                    rqst->name, ntohl(rqst->did), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
                return -1;
            }
        }

        if (didname && (type_didname != rqst->type)) {
            /* same: one is a dir one is a file, remove from db */

            LOG(log_debug, logtype_cnid, "dbd_lookup(name:'%s', did:%u, dev/ino:0x%llx/0x%llx): type mismatch for didname", 
                rqst->name, ntohl(rqst->did), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);

            rqst->cnid = id_didname;
            rc = dbd_delete(dbd, rqst, rply, DBIF_CNID);
            rc += dbd_delete(dbd, rqst, rply, DBIF_IDX_DEVINO);
            rc += dbd_delete(dbd, rqst, rply, DBIF_IDX_DIDNAME);
            if (rc < 0) {
                LOG(log_error, logtype_cnid, "dbd_lookup(name:'%s', did:%u, dev/ino:0x%llx/0x%llx): error deleting type mismatch for didname", 
                    rqst->name, ntohl(rqst->did), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
                return -1;
            }
        }

        rply->result = CNID_DBD_RES_NOTFOUND;
        return 1;
    }

    if (devino && didname && id_devino == id_didname) {
        /* everything is fine */
        LOG(log_debug, logtype_cnid, "dbd_lookup(DID:%u/'%s',0x%llx/0x%llx): Got CNID: %u",
            ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, htonl(id_didname));
        rply->cnid = id_didname;
        rply->result = CNID_DBD_RES_OK;
        return 1;
    }

    if (devino && didname && id_devino != id_didname) {
        /* CNIDs don't match, something of a worst case, or possibly 3) emacs! */
        LOG(log_debug, logtype_cnid, "dbd_lookup: CNID mismatch: (DID:%u/'%s') --> %u , (0x%llx/0x%llx) --> %u",
            ntohl(rqst->did), rqst->name, ntohl(id_didname),
            (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(id_devino));

        rqst->cnid = id_devino;
        if (dbd_delete(dbd, rqst, rply, DBIF_CNID) < 0)
            return -1;

        rqst->cnid = id_didname;
        if (dbd_delete(dbd, rqst, rply, DBIF_CNID) < 0)
            return -1;

        rply->result = CNID_DBD_RES_NOTFOUND;
        return 1;
    }

    if ( ! didname) {
        LOG(log_debug, logtype_cnid, "dbd_lookup(CNID hint: %u, DID:%u, \"%s\", 0x%llx/0x%llx): CNID resolve problem: server side rename oder reused inode",
            ntohl(rqst->cnid), ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
        if (rqst->cnid == id_devino) {
            LOG(log_debug, logtype_cnid, "dbd_lookup: server side mv (with resource fork)");
            update = 1;
        } else {
            rqst->cnid = id_devino;
            if (dbd_delete(dbd, rqst, rply, DBIF_CNID) < 0)
                return -1;
            rply->result = CNID_DBD_RES_NOTFOUND;
            rqst->cnid = CNID_INVALID; /* invalidate CNID hint */
            return 1;
        }
    }

    if ( ! devino) {
        LOG(log_debug, logtype_cnid, "dbd_lookup(DID:%u/'%s',0x%llx/0x%llx): CNID resolve problem: changed dev/ino",
            ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
        rqst->cnid = id_didname;
        if (dbd_delete(dbd, rqst, rply, DBIF_CNID) < 0)
            return -1;
        rply->result = CNID_DBD_RES_NOTFOUND;
        rqst->cnid = CNID_INVALID; /* invalidate CNID hint */
        return 1;
    }

    /* This is also a catch all if we've forgot to catch some possibility with the preceding ifs*/
    if (!update) {
        rply->result = CNID_DBD_RES_NOTFOUND;
        return 1;
    }

    /* Fix up the database */
    rc = dbd_update(dbd, rqst, rply);
    if (rc >0) {
        rply->cnid = rqst->cnid;
    }

    LOG(log_debug, logtype_cnid, "dbd_lookup(DID:%u/'%s',0x%llx/0x%llx): Got CNID (needed update): %u", 
        ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(rply->cnid));

    return rc;
}