]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/ofork.c
Pre-decrement ad_refcount
[netatalk.git] / etc / afpd / ofork.c
1 /*
2  * Copyright (c) 1996 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <string.h>
16 #include <sys/stat.h> /* works around a bug */
17 #include <sys/param.h>
18 #include <errno.h>
19
20 #include <atalk/logger.h>
21 #include <atalk/util.h>
22 #include <atalk/bstrlib.h>
23 #include <atalk/bstradd.h>
24
25 #include "globals.h"
26 #include "volume.h"
27 #include "directory.h"
28 #include "fork.h"
29
30 /* we need to have a hashed list of oforks (by dev inode). just hash
31  * by first letter. */
32 #define OFORK_HASHSIZE  64
33 static struct ofork     *ofork_table[OFORK_HASHSIZE];
34
35 static struct ofork **oforks = NULL;
36 static int          nforks = 0;
37 static u_short      lastrefnum = 0;
38
39
40 /* OR some of each character for the hash*/
41 static unsigned long hashfn(const struct file_key *key)
42 {
43 #if 0
44     unsigned long i = 0;
45     while (*name) {
46         i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
47     }
48 #endif
49     return key->inode & (OFORK_HASHSIZE - 1);
50 }
51
52 static void of_hash(struct ofork *of)
53 {
54     struct ofork **table;
55
56     table = &ofork_table[hashfn(&of->key)];
57     if ((of->next = *table) != NULL)
58         (*table)->prevp = &of->next;
59     *table = of;
60     of->prevp = table;
61 }
62
63 static void of_unhash(struct ofork *of)
64 {
65     if (of->prevp) {
66         if (of->next)
67             of->next->prevp = of->prevp;
68         *(of->prevp) = of->next;
69     }
70 }
71
72 #ifdef DEBUG1
73 void of_pforkdesc( FILE *f)
74 {
75     int ofrefnum;
76
77     if (!oforks)
78         return;
79
80     for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
81         if ( oforks[ ofrefnum ] != NULL ) {
82             fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
83         }
84     }
85 }
86 #endif
87
88 int of_flush(const struct vol *vol)
89 {
90     int refnum;
91
92     if (!oforks)
93         return 0;
94
95     for ( refnum = 0; refnum < nforks; refnum++ ) {
96         if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
97             flushfork( oforks[ refnum ] ) < 0 ) {
98             LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
99         }
100     }
101     return( 0 );
102 }
103
104 int of_rename(const struct vol *vol,
105               struct ofork *s_of,
106               struct dir *olddir, const char *oldpath _U_,
107               struct dir *newdir, const char *newpath)
108 {
109     struct ofork *of, *next;
110     int done = 0;
111
112     if (!s_of)
113         return AFP_OK;
114
115     next = ofork_table[hashfn(&s_of->key)];
116     while ((of = next)) {
117         next = next->next; /* so we can unhash and still be all right. */
118
119         if (vol == of->of_vol
120             && olddir->d_did == of->of_did
121             && s_of->key.dev == of->key.dev
122             && s_of->key.inode == of->key.inode ) {
123             if (!done) {
124                 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
125                 done = 1;
126             }
127             if (newdir != olddir)
128                 of->of_did = newdir->d_did;
129         }
130     }
131
132     return AFP_OK;
133 }
134
135 #define min(a,b)    ((a)<(b)?(a):(b))
136
137 struct ofork *
138 of_alloc(struct vol *vol,
139          struct dir    *dir,
140          char      *path,
141          u_int16_t     *ofrefnum,
142          const int      eid,
143          struct adouble *ad,
144          struct stat    *st)
145 {
146     struct ofork        *of;
147     u_int16_t       refnum, of_refnum;
148
149     int         i;
150
151     if (!oforks) {
152         nforks = getdtablesize() - 10;
153         /* protect against insane ulimit -n */
154         nforks = min(nforks, 0xffff);
155         oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
156         if (!oforks)
157             return NULL;
158     }
159
160     for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
161         /* cf AFP3.0.pdf, File fork page 40 */
162         if (!refnum)
163             refnum++;
164         if ( oforks[ refnum % nforks ] == NULL ) {
165             break;
166         }
167     }
168     /* grr, Apple and their 'uniquely identifies'
169        the next line is a protection against
170        of_alloc()
171        refnum % nforks = 3
172        lastrefnum = 3
173        oforks[3] != NULL
174        refnum = 4
175        oforks[4] == NULL
176        return 4
177
178        close(oforks[4])
179
180        of_alloc()
181        refnum % nforks = 4
182        ...
183        return 4
184        same if lastrefnum++ rather than ++lastrefnum.
185     */
186     lastrefnum = refnum;
187     if ( i == nforks ) {
188         LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
189         return( NULL );
190     }
191
192     of_refnum = refnum % nforks;
193     if (( oforks[ of_refnum ] =
194           (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
195         LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
196         return NULL;
197     }
198     of = oforks[of_refnum];
199
200     /* see if we need to allocate space for the adouble struct */
201     if (!ad) {
202         ad = malloc( sizeof( struct adouble ) );
203         if (!ad) {
204             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
205             free(of);
206             oforks[ of_refnum ] = NULL;
207             return NULL;
208         }
209
210         /* initialize to zero. This is important to ensure that
211            ad_open really does reinitialize the structure. */
212         ad_init(ad, vol->v_adouble, vol->v_ad_options);
213
214         ad->ad_m_namelen = 255 +1;
215         /* here's the deal: we allocate enough for the standard mac file length.
216          * in the future, we'll reallocate in fairly large jumps in case
217          * of long unicode names */
218         if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
219             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
220             free(ad);
221             free(of);
222             oforks[ of_refnum ] = NULL;
223             return NULL;
224         }
225         strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
226     }
227
228     ad_ref(ad);
229     of->of_ad = ad;
230     of->of_vol = vol;
231     of->of_did = dir->d_did;
232
233     *ofrefnum = refnum;
234     of->of_refnum = refnum;
235     of->key.dev = st->st_dev;
236     of->key.inode = st->st_ino;
237     if (eid == ADEID_DFORK)
238         of->of_flags = AFPFORK_DATA;
239     else
240         of->of_flags = AFPFORK_RSRC;
241
242     of_hash(of);
243     return( of );
244 }
245
246 struct ofork *of_find(const u_int16_t ofrefnum )
247 {
248     if (!oforks || !nforks)
249         return NULL;
250
251     return( oforks[ ofrefnum % nforks ] );
252 }
253
254 /* -------------------------- */
255 int of_stat(struct path *path)
256 {
257     int ret;
258
259     path->st_errno = 0;
260     path->st_valid = 1;
261
262     if ((ret = lstat(path->u_name, &path->st)) < 0) {
263         LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
264             cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
265         path->st_errno = errno;
266     }
267
268     return ret;
269 }
270
271
272 #ifdef HAVE_RENAMEAT
273 int of_fstatat(int dirfd, struct path *path)
274 {
275     int ret;
276
277     path->st_errno = 0;
278     path->st_valid = 1;
279
280     if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
281         path->st_errno = errno;
282
283    return ret;
284 }
285 #endif /* HAVE_RENAMEAT */
286
287 /* -------------------------- 
288    stat the current directory.
289    stat(".") works even if "." is deleted thus
290    we have to stat ../name because we want to know if it's there
291 */
292 int of_statdir(struct vol *vol, struct path *path)
293 {
294     static char pathname[ MAXPATHLEN + 1] = "../";
295     int ret;
296     size_t len;
297     struct dir *dir;
298
299     if (*path->m_name) {
300         /* not curdir */
301         return of_stat (path);
302     }
303     path->st_errno = 0;
304     path->st_valid = 1;
305     /* FIXME, what about: we don't have r-x perm anymore ? */
306     len = blength(path->d_dir->d_u_name);
307     if (len > (MAXPATHLEN - 3))
308         len = MAXPATHLEN - 3;
309     strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
310
311     LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
312
313     if (!(ret = lstat(pathname, &path->st)))
314         return 0;
315
316     path->st_errno = errno;
317
318     /* hmm, can't stat curdir anymore */
319     if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
320        if (movecwd(vol, dir)) 
321            return -1;
322        path->st_errno = 0;
323
324        if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0) 
325            path->st_errno = errno;
326     }
327
328     return ret;
329 }
330
331 /* -------------------------- */
332 struct ofork *of_findname(struct path *path)
333 {
334     struct ofork *of;
335     struct file_key key;
336
337     if (!path->st_valid) {
338         of_stat(path);
339     }
340
341     if (path->st_errno)
342         return NULL;
343
344     key.dev = path->st.st_dev;
345     key.inode = path->st.st_ino;
346
347     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
348         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
349             return of;
350         }
351     }
352
353     return NULL;
354 }
355
356 /*!
357  * @brief Search for open fork by dirfd/name
358  *
359  * Function call of_fstatat with dirfd and path and uses dev and ino
360  * to search the open fork table.
361  *
362  * @param dirfd     (r) directory fd
363  * @param path      (rw) pointer to struct path
364  */
365 #ifdef HAVE_RENAMEAT
366 struct ofork *of_findnameat(int dirfd, struct path *path)
367 {
368     struct ofork *of;
369     struct file_key key;
370     
371     if ( ! path->st_valid) {
372         of_fstatat(dirfd, path);
373     }
374         
375     if (path->st_errno)
376         return NULL;
377
378     key.dev = path->st.st_dev;
379     key.inode = path->st.st_ino;
380
381     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
382         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
383             return of;
384         }
385     }
386
387     return NULL;
388 }
389 #endif
390
391 void of_dealloc( struct ofork *of)
392 {
393     if (!oforks)
394         return;
395
396     of_unhash(of);
397     oforks[ of->of_refnum % nforks ] = NULL;
398     ad_unlock(of->of_ad, of->of_refnum);
399
400     free( of );
401 }
402
403 /* --------------------------- */
404 int of_closefork(struct ofork *ofork)
405 {
406     struct timeval      tv;
407     int         doflush = 0;
408     int                 ret;
409
410     if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
411         /*
412          * Only set the rfork's length if we're closing the rfork.
413          */
414         if ((ofork->of_flags & AFPFORK_RSRC)) {
415             ad_refresh( ofork->of_ad );
416             if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
417                 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
418                 doflush++;
419             }
420             if ( doflush ) {
421                 ad_flush( ofork->of_ad );
422             }
423         }
424     }
425     ret = 0;
426     
427     if (ad_unref(ofork->of_ad) == 0) {
428         if ( ad_close( ofork->of_ad, 0 ) < 0 ) {
429             ret = -1;
430         }
431     }
432
433     of_dealloc( ofork );
434     return ret;
435 }
436
437 /* ----------------------
438
439  */
440 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
441 {
442     struct ofork        *of;
443     struct adouble      *adp;
444
445     if ((of = of_findname(path))) {
446         adp = of->of_ad;
447     } else {
448         ad_init(ad, vol->v_adouble, vol->v_ad_options);
449         adp = ad;
450     }
451     return adp;
452 }
453
454 /* ----------------------
455    close all forks for a volume
456 */
457 void of_closevol(const struct vol *vol)
458 {
459     int refnum;
460
461     if (!oforks)
462         return;
463
464     for ( refnum = 0; refnum < nforks; refnum++ ) {
465         if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
466             if (of_closefork( oforks[ refnum ]) < 0 ) {
467                 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
468             }
469         }
470     }
471     return;
472 }
473