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