2 * $Id: ofork.c,v 1.20.6.6.2.3 2005-07-21 17:00:09 didg 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>
20 #include <atalk/logger.h>
23 #include <atalk/util.h>
27 #include "directory.h"
30 /* we need to have a hashed list of oforks (by dev inode). just hash
32 #define OFORK_HASHSIZE 64
33 static struct ofork *ofork_table[OFORK_HASHSIZE];
35 static struct ofork **oforks = NULL;
36 static int nforks = 0;
37 static u_short lastrefnum = 0;
40 /* OR some of each character for the hash*/
41 static __inline__ unsigned long hashfn(const struct file_key *key)
46 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
49 return key->inode & (OFORK_HASHSIZE - 1);
52 static __inline__ void of_hash(struct ofork *of)
56 table = &ofork_table[hashfn(&of->key)];
57 if ((of->next = *table) != NULL)
58 (*table)->prevp = &of->next;
63 static __inline__ void of_unhash(struct ofork *of)
67 of->next->prevp = of->prevp;
68 *(of->prevp) = of->next;
73 void of_pforkdesc( f )
81 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
82 if ( oforks[ ofrefnum ] != NULL ) {
83 fprintf( f, "%hu <%s>\n", ofrefnum, oforks[ ofrefnum ]->of_name);
89 int of_flush(const struct vol *vol)
96 for ( refnum = 0; refnum < nforks; refnum++ ) {
97 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
98 flushfork( oforks[ refnum ] ) < 0 ) {
99 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
105 int of_rename(vol, s_of, olddir, oldpath, newdir, newpath)
106 const struct vol *vol;
108 struct dir *olddir, *newdir;
109 const char *oldpath, *newpath;
111 struct ofork *of, *next, *d_ofork;
116 next = ofork_table[hashfn(&s_of->key)];
117 while ((of = next)) {
118 next = next->next; /* so we can unhash and still be all right. */
120 if (vol == of->of_vol && olddir == of->of_dir &&
121 s_of->key.dev == of->key.dev &&
122 s_of->key.inode == of->key.inode ) {
123 strlcpy( of->of_name, newpath, of->of_namelen);
124 if (newdir != olddir) {
125 of->of_d_prev->of_d_next = of->of_d_next;
126 of->of_d_next->of_d_prev = of->of_d_prev;
127 if (of->of_dir->d_ofork == of) {
128 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
131 if (!(d_ofork = newdir->d_ofork)) {
132 newdir->d_ofork = of;
133 of->of_d_next = of->of_d_prev = of;
135 of->of_d_next = d_ofork;
136 of->of_d_prev = d_ofork->of_d_prev;
137 of->of_d_prev->of_d_next = of;
138 d_ofork->of_d_prev = of;
147 #define min(a,b) ((a)<(b)?(a):(b))
150 of_alloc(vol, dir, path, ofrefnum, eid, ad, st)
159 struct ofork *of, *d_ofork;
160 u_int16_t refnum, of_refnum;
165 nforks = (getdtablesize() - 10) / 2;
166 /* protect against insane ulimit -n */
167 nforks = min(nforks, 0xffff);
168 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
173 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
174 /* cf AFP3.0.pdf, File fork page 40 */
177 if ( oforks[ refnum % nforks ] == NULL ) {
181 /* grr, Apple and their 'uniquely identifies'
182 the next line is a protection against
197 same if lastrefnum++ rather than ++lastrefnum.
201 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
205 of_refnum = refnum % nforks;
206 if (( oforks[ of_refnum ] =
207 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
208 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
211 of = oforks[of_refnum];
213 /* here's the deal: we allocate enough for the standard mac file length.
214 * in the future, we'll reallocate in fairly large jumps in case
215 * of long unicode names */
216 of->of_namelen = 255 +1;
217 if (( of->of_name =(char *)malloc( of->of_namelen )) == NULL ) {
218 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
220 oforks[ of_refnum ] = NULL;
224 /* see if we need to allocate space for the adouble struct */
226 ad = malloc( sizeof( struct adouble ) );
228 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
231 oforks[ of_refnum ] = NULL;
235 /* initialize to zero. This is important to ensure that
236 ad_open really does reinitialize the structure. */
237 ad_init(ad, vol->v_adouble, vol->v_ad_options);
239 /* Increase the refcount on this struct adouble. This is
240 decremented again in oforc_dealloc. */
248 if (!(d_ofork = dir->d_ofork)) {
250 of->of_d_next = of->of_d_prev = of;
252 of->of_d_next = d_ofork;
253 of->of_d_prev = d_ofork->of_d_prev;
254 d_ofork->of_d_prev->of_d_next = of;
255 d_ofork->of_d_prev = of;
258 strlcpy( of->of_name, path, of->of_namelen);
260 of->of_refnum = refnum;
261 of->key.dev = st->st_dev;
262 of->key.inode = st->st_ino;
263 if (eid == ADEID_DFORK)
264 of->of_flags = AFPFORK_DATA;
266 of->of_flags = AFPFORK_RSRC;
272 struct ofork *of_find(const u_int16_t ofrefnum )
274 if (!oforks || !nforks)
277 return( oforks[ ofrefnum % nforks ] );
280 /* -------------------------- */
281 int of_stat (struct path *path)
286 if ((ret = stat(path->u_name, &path->st)) < 0)
287 path->st_errno = errno;
291 /* -------------------------- */
292 int of_statdir (const struct vol *vol, struct path *path)
294 static char pathname[ MAXPATHLEN + 1];
299 return of_stat (path);
303 /* FIXME, what about: we don't have r-x perm anymore ? */
304 strcpy(pathname, "../");
305 strlcat(pathname, path->d_dir->d_u_name, MAXPATHLEN);
307 if (!(ret = stat(pathname, &path->st)))
310 path->st_errno = errno;
311 /* hmm, can't stat curdir anymore */
312 if (errno == EACCES && curdir->d_parent ) {
313 if (movecwd(vol, curdir->d_parent))
316 if ((ret = stat(path->d_dir->d_u_name, &path->st)) < 0)
317 path->st_errno = errno;
322 /* -------------------------- */
324 of_findname(struct path *path)
329 if (!path->st_valid) {
336 key.dev = path->st.st_dev;
337 key.inode = path->st.st_ino;
339 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
340 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
348 void of_dealloc( of )
357 of->of_d_prev->of_d_next = of->of_d_next;
358 of->of_d_next->of_d_prev = of->of_d_prev;
359 if (of->of_dir->d_ofork == of) {
360 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
363 oforks[ of->of_refnum % nforks ] = NULL;
366 /* decrease refcount */
367 of->of_ad->ad_refcount--;
369 if ( of->of_ad->ad_refcount <= 0) {
371 } else {/* someone's still using it. just free this user's locks */
372 ad_unlock(of->of_ad, of->of_refnum);
378 /* ----------------------
381 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
386 if ((of = of_findname(path))) {
389 ad_init(ad, vol->v_adouble, vol->v_ad_options);