2 * $Id: ofork.c,v 1.30.4.1 2010-02-01 10:56:08 franklahm Exp $
4 * Copyright (c) 1996 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #include <sys/stat.h> /* works around a bug */
19 #include <sys/param.h>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
24 #include <atalk/bstrlib.h>
25 #include <atalk/bstradd.h>
29 #include "directory.h"
32 /* we need to have a hashed list of oforks (by dev inode). just hash
34 #define OFORK_HASHSIZE 64
35 static struct ofork *ofork_table[OFORK_HASHSIZE];
37 static struct ofork **oforks = NULL;
38 static int nforks = 0;
39 static u_short lastrefnum = 0;
42 /* OR some of each character for the hash*/
43 static unsigned long hashfn(const struct file_key *key)
48 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
51 return key->inode & (OFORK_HASHSIZE - 1);
54 static void of_hash(struct ofork *of)
58 table = &ofork_table[hashfn(&of->key)];
59 if ((of->next = *table) != NULL)
60 (*table)->prevp = &of->next;
65 static void of_unhash(struct ofork *of)
69 of->next->prevp = of->prevp;
70 *(of->prevp) = of->next;
75 void of_pforkdesc( FILE *f)
82 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
83 if ( oforks[ ofrefnum ] != NULL ) {
84 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
90 int of_flush(const struct vol *vol)
97 for ( refnum = 0; refnum < nforks; refnum++ ) {
98 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
99 flushfork( oforks[ refnum ] ) < 0 ) {
100 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
107 const struct vol *vol,
109 struct dir *olddir, const char *oldpath _U_,
110 struct dir *newdir, const char *newpath)
112 struct ofork *of, *next, *d_ofork;
118 next = ofork_table[hashfn(&s_of->key)];
119 while ((of = next)) {
120 next = next->next; /* so we can unhash and still be all right. */
122 if (vol == of->of_vol && olddir == of->of_dir &&
123 s_of->key.dev == of->key.dev &&
124 s_of->key.inode == of->key.inode ) {
126 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
129 if (newdir != olddir) {
130 of->of_d_prev->of_d_next = of->of_d_next;
131 of->of_d_next->of_d_prev = of->of_d_prev;
132 if (of->of_dir->d_ofork == of) {
133 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
136 if (!(d_ofork = newdir->d_ofork)) {
137 newdir->d_ofork = of;
138 of->of_d_next = of->of_d_prev = of;
140 of->of_d_next = d_ofork;
141 of->of_d_prev = d_ofork->of_d_prev;
142 of->of_d_prev->of_d_next = of;
143 d_ofork->of_d_prev = of;
152 #define min(a,b) ((a)<(b)?(a):(b))
155 of_alloc(struct vol *vol,
163 struct ofork *of, *d_ofork;
164 u_int16_t refnum, of_refnum;
169 nforks = getdtablesize() - 10;
170 /* protect against insane ulimit -n */
171 nforks = min(nforks, 0xffff);
172 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
177 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
178 /* cf AFP3.0.pdf, File fork page 40 */
181 if ( oforks[ refnum % nforks ] == NULL ) {
185 /* grr, Apple and their 'uniquely identifies'
186 the next line is a protection against
201 same if lastrefnum++ rather than ++lastrefnum.
205 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
209 of_refnum = refnum % nforks;
210 if (( oforks[ of_refnum ] =
211 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
212 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
215 of = oforks[of_refnum];
217 /* see if we need to allocate space for the adouble struct */
219 ad = malloc( sizeof( struct adouble ) );
221 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
223 oforks[ of_refnum ] = NULL;
227 /* initialize to zero. This is important to ensure that
228 ad_open really does reinitialize the structure. */
229 ad_init(ad, vol->v_adouble, vol->v_ad_options);
231 ad->ad_m_namelen = 255 +1;
232 /* here's the deal: we allocate enough for the standard mac file length.
233 * in the future, we'll reallocate in fairly large jumps in case
234 * of long unicode names */
235 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
236 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
239 oforks[ of_refnum ] = NULL;
242 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
244 /* Increase the refcount on this struct adouble. This is
245 decremented again in oforc_dealloc. */
253 if (!(d_ofork = dir->d_ofork)) {
255 of->of_d_next = of->of_d_prev = of;
257 of->of_d_next = d_ofork;
258 of->of_d_prev = d_ofork->of_d_prev;
259 d_ofork->of_d_prev->of_d_next = of;
260 d_ofork->of_d_prev = of;
264 of->of_refnum = refnum;
265 of->key.dev = st->st_dev;
266 of->key.inode = st->st_ino;
267 if (eid == ADEID_DFORK)
268 of->of_flags = AFPFORK_DATA;
270 of->of_flags = AFPFORK_RSRC;
276 struct ofork *of_find(const u_int16_t ofrefnum )
278 if (!oforks || !nforks)
281 return( oforks[ ofrefnum % nforks ] );
284 /* -------------------------- */
285 int of_stat(struct path *path)
289 LOG(log_maxdebug, logtype_afpd, "of_stat: {stat:'%s/%s'}", getcwdpath(), path->u_name);
293 if ((ret = stat(path->u_name, &path->st)) < 0)
294 path->st_errno = errno;
298 /* --------------------------
299 stat the current directory.
300 stat(".") works even if "." is deleted thus
301 we have to stat ../name because we want to know if it's there
303 int of_statdir(struct vol *vol, struct path *path)
305 static char pathname[ MAXPATHLEN + 1] = "../";
312 return of_stat (path);
316 /* FIXME, what about: we don't have r-x perm anymore ? */
317 len = blength(path->d_dir->d_u_name);
318 if (len > (MAXPATHLEN - 3))
319 len = MAXPATHLEN - 3;
320 strncpy(pathname + 3, cfrombstring(path->d_dir->d_u_name), len + 1);
322 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
324 if (!(ret = stat(pathname, &path->st)))
327 path->st_errno = errno;
329 /* hmm, can't stat curdir anymore */
330 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
331 if (movecwd(vol, dir))
334 if ((ret = stat(cfrombstring(path->d_dir->d_u_name), &path->st)) < 0)
335 path->st_errno = errno;
341 /* -------------------------- */
342 struct ofork *of_findname(struct path *path)
347 if (!path->st_valid) {
354 key.dev = path->st.st_dev;
355 key.inode = path->st.st_ino;
357 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
358 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
366 void of_dealloc( struct ofork *of)
374 of->of_d_prev->of_d_next = of->of_d_next;
375 of->of_d_next->of_d_prev = of->of_d_prev;
376 if (of->of_dir->d_ofork == of) {
377 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
380 oforks[ of->of_refnum % nforks ] = NULL;
382 /* decrease refcount */
383 of->of_ad->ad_refcount--;
385 if ( of->of_ad->ad_refcount <= 0) {
386 free( of->of_ad->ad_m_name );
388 } else {/* someone's still using it. just free this user's locks */
389 ad_unlock(of->of_ad, of->of_refnum);
395 /* --------------------------- */
396 int of_closefork(struct ofork *ofork)
399 int adflags, doflush = 0;
403 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
404 adflags |= ADFLAGS_DF;
406 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
407 adflags |= ADFLAGS_HF;
409 * Only set the rfork's length if we're closing the rfork.
411 if ((ofork->of_flags & AFPFORK_RSRC)) {
412 ad_refresh( ofork->of_ad );
413 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
414 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
418 ad_flush( ofork->of_ad );
423 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
431 /* ----------------------
434 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
439 if ((of = of_findname(path))) {
442 ad_init(ad, vol->v_adouble, vol->v_ad_options);
448 /* ----------------------
449 close all forks for a volume
451 void of_closevol(const struct vol *vol)
458 for ( refnum = 0; refnum < nforks; refnum++ ) {
459 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
460 if (of_closefork( oforks[ refnum ]) < 0 ) {
461 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );