2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 2010 Frank Lahm
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
16 #include <sys/socket.h>
19 #include <atalk/dsi.h>
20 #include <atalk/afp.h>
21 #include <atalk/adouble.h>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
24 #include <atalk/cnid.h>
25 #include <atalk/bstrlib.h>
26 #include <atalk/bstradd.h>
27 #include <atalk/globals.h>
28 #include <atalk/netatalk_conf.h>
33 #include "directory.h"
38 struct ofork *writtenfork;
41 static int getforkparams(const AFPObj *obj, struct ofork *ofork, uint16_t bitmap, char *buf, size_t *buflen)
51 /* can only get the length of the opened fork */
52 if ( ( (bitmap & ((1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN)))
53 && (ofork->of_flags & AFPFORK_RSRC))
55 ( (bitmap & ((1<<FILPBIT_RFLEN) | (1<<FILPBIT_EXTRFLEN)))
56 && (ofork->of_flags & AFPFORK_DATA))) {
57 return( AFPERR_BITMAP );
60 if (! AD_META_OPEN(ofork->of_ad)) {
67 dir = dirlookup(vol, ofork->of_did);
69 if (NULL == (path.m_name = utompath(vol, of_name(ofork), dir->d_did, utf8_encoding(obj)))) {
70 return( AFPERR_MISC );
72 path.u_name = of_name(ofork);
75 if ( bitmap & ( (1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN) |
76 (1<<FILPBIT_FNUM) | (1 << FILPBIT_CDATE) |
77 (1 << FILPBIT_MDATE) | (1 << FILPBIT_BDATE))) {
78 if ( ad_data_fileno( ofork->of_ad ) <= 0 ) {
79 /* 0 is for symlink */
80 if (movecwd(vol, dir) < 0)
81 return( AFPERR_NOOBJ );
82 if ( lstat( path.u_name, st ) < 0 )
83 return( AFPERR_NOOBJ );
85 if ( fstat( ad_data_fileno( ofork->of_ad ), st ) < 0 ) {
86 return( AFPERR_BITMAP );
90 return getmetadata(obj, vol, bitmap, &path, dir, buf, buflen, adp );
93 static off_t get_off_t(char **ibuf, int is64)
99 memcpy(&temp, *ibuf, sizeof( temp ));
100 ret = ntohl(temp); /* ntohl is unsigned */
101 *ibuf += sizeof(temp);
104 memcpy(&temp, *ibuf, sizeof( temp ));
105 *ibuf += sizeof(temp);
106 ret = ntohl(temp)| (ret << 32);
109 ret = (int)ret; /* sign extend */
114 static int set_off_t(off_t offset, char *rbuf, int is64)
121 temp = htonl(offset >> 32);
122 memcpy(rbuf, &temp, sizeof( temp ));
123 rbuf += sizeof(temp);
124 ret = sizeof( temp );
125 offset &= 0xffffffff;
127 temp = htonl(offset);
128 memcpy(rbuf, &temp, sizeof( temp ));
129 ret += sizeof( temp );
134 static int is_neg(int is64, off_t val)
136 if (val < 0 || (sizeof(off_t) == 8 && !is64 && (val & 0x80000000U)))
141 static int sum_neg(int is64, off_t offset, off_t reqcount)
143 if (is_neg(is64, offset +reqcount) )
148 static int fork_setmode(const AFPObj *obj, struct adouble *adp, int eid, int access, int ofrefnum)
159 if (obj->options.flags & OPTION_SHARE_RESERV) {
160 shmd.f_access = (access & OPENACC_RD ? F_RDACC : 0) | (access & OPENACC_WR ? F_WRACC : 0);
161 if (shmd.f_access == 0)
162 /* we must give an access mode, otherwise fcntl will complain */
163 shmd.f_access = F_RDACC;
164 shmd.f_deny = (access & OPENACC_DRD ? F_RDDNY : F_NODNY) | (access & OPENACC_DWR) ? F_WRDNY : 0;
165 shmd.f_id = ofrefnum;
167 int fd = (eid == ADEID_DFORK) ? ad_data_fileno(adp) : ad_reso_fileno(adp);
169 if (fd != -1 && fd != AD_SYMLINK && fcntl(fd, F_SHARE, &shmd) != 0) {
170 LOG(log_debug, logtype_afpd, "fork_setmode: fcntl: %s", strerror(errno));
177 if (! (access & (OPENACC_WR | OPENACC_RD | OPENACC_DWR | OPENACC_DRD))) {
178 return ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_OPEN_NONE, 1, ofrefnum);
181 if ((access & (OPENACC_RD | OPENACC_DRD))) {
182 if ((readset = ad_testlock(adp, eid, AD_FILELOCK_OPEN_RD)) <0)
184 if ((denyreadset = ad_testlock(adp, eid, AD_FILELOCK_DENY_RD)) <0)
187 if ((access & OPENACC_RD) && denyreadset) {
191 if ((access & OPENACC_DRD) && readset) {
195 /* boolean logic is not enough, because getforkmode is not always telling the
198 if ((access & OPENACC_RD)) {
199 ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_OPEN_RD, 1, ofrefnum);
203 if ((access & OPENACC_DRD)) {
204 ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_DENY_RD, 1, ofrefnum);
209 /* ------------same for writing -------------- */
210 if ((access & (OPENACC_WR | OPENACC_DWR))) {
211 if ((writeset = ad_testlock(adp, eid, AD_FILELOCK_OPEN_WR)) <0)
213 if ((denywriteset = ad_testlock(adp, eid, AD_FILELOCK_DENY_WR)) <0)
216 if ((access & OPENACC_WR) && denywriteset) {
220 if ((access & OPENACC_DWR) && writeset) {
224 if ((access & OPENACC_WR)) {
225 ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_OPEN_WR, 1, ofrefnum);
229 if ((access & OPENACC_DWR)) {
230 ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_DENY_WR, 1, ofrefnum);
239 /* ----------------------- */
240 int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
244 struct ofork *ofork, *opened;
245 struct adouble *adsame = NULL;
247 int ret, adflags, eid;
249 uint16_t vid, bitmap, access, ofrefnum;
250 char fork, *path, *upath;
257 memcpy(&vid, ibuf, sizeof( vid ));
261 if (NULL == ( vol = getvolbyvid( vid ))) {
262 return( AFPERR_PARAM );
265 memcpy(&did, ibuf, sizeof( did ));
266 ibuf += sizeof( int );
268 if (NULL == ( dir = dirlookup( vol, did ))) {
272 memcpy(&bitmap, ibuf, sizeof( bitmap ));
273 bitmap = ntohs( bitmap );
274 ibuf += sizeof( bitmap );
275 memcpy(&access, ibuf, sizeof( access ));
276 access = ntohs( access );
277 ibuf += sizeof( access );
279 if ((vol->v_flags & AFPVOL_RO) && (access & OPENACC_WR)) {
283 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
284 return get_afp_errno(AFPERR_PARAM);
287 if (*s_path->m_name == '\0') {
289 return AFPERR_BADTYPE;
292 /* stat() data fork st is set because it's not a dir */
293 switch ( s_path->st_errno ) {
299 return (access & OPENACC_WR) ? AFPERR_LOCK : AFPERR_ACCESS;
301 LOG(log_error, logtype_afpd, "afp_openfork(%s): %s", s_path->m_name, strerror(errno));
305 upath = s_path->u_name;
306 path = s_path->m_name;
309 if (!vol_unix_priv(vol)) {
310 if (check_access(obj, vol, upath, access ) < 0) {
311 return AFPERR_ACCESS;
314 if (file_access(obj, vol, s_path, access ) < 0) {
315 return AFPERR_ACCESS;
319 if ((opened = of_findname(vol, s_path))) {
320 adsame = opened->of_ad;
323 adflags = ADFLAGS_SETSHRMD;
325 if (fork == OPENFORK_DATA) {
327 adflags |= ADFLAGS_DF;
330 adflags |= ADFLAGS_RF;
333 if (access & OPENACC_WR) {
334 adflags |= ADFLAGS_RDWR;
335 if (fork != OPENFORK_DATA)
337 * We only try to create the resource
338 * fork if the user wants to open it for write acess.
340 adflags |= ADFLAGS_CREATE;
342 adflags |= ADFLAGS_RDONLY;
345 if ((ofork = of_alloc(vol, curdir, path, &ofrefnum, eid, adsame, st)) == NULL)
348 LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\", %s, %s)",
349 fullpathname(s_path->u_name),
350 (fork == OPENFORK_DATA) ? "data" : "reso",
351 !(access & OPENACC_WR) ? "O_RDONLY" : "O_RDWR");
355 /* First ad_open(), opens data or ressource fork */
356 if (ad_open(ofork->of_ad, upath, adflags, 0666) < 0) {
369 ret = AFPERR_BADTYPE;
372 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_open: %s", s_path->m_name, strerror(errno) );
379 * Create metadata if we open rw, otherwise only open existing metadata
381 if (access & OPENACC_WR) {
382 adflags = ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE;
384 adflags = ADFLAGS_HF | ADFLAGS_RDONLY;
387 if (ad_open(ofork->of_ad, upath, adflags, 0666) == 0) {
388 ofork->of_flags |= AFPFORK_META;
389 if (ad_get_MD_flags(ofork->of_ad) & O_CREAT) {
390 LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\"): setting CNID", upath);
392 if ((id = get_id(vol, ofork->of_ad, st, dir->d_did, upath, strlen(upath))) == CNID_INVALID) {
393 LOG(log_error, logtype_afpd, "afp_createfile(\"%s\"): CNID error", upath);
396 (void)ad_setid(ofork->of_ad, st->st_dev, st->st_ino, id, dir->d_did, vol->v_stamp);
397 ad_flush(ofork->of_ad);
403 /* no metadata? We don't care! */
412 ret = AFPERR_BADTYPE;
415 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_open: %s", s_path->m_name, strerror(errno) );
421 if ((adflags & ADFLAGS_RF) && (ad_get_RF_flags( ofork->of_ad) & O_CREAT)) {
422 if (ad_setname(ofork->of_ad, path)) {
423 ad_flush( ofork->of_ad );
427 if ((ret = getforkparams(obj, ofork, bitmap, rbuf + 2 * sizeof(int16_t), &buflen)) != AFP_OK) {
428 ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
432 *rbuflen = buflen + 2 * sizeof( uint16_t );
433 bitmap = htons( bitmap );
434 memcpy(rbuf, &bitmap, sizeof( uint16_t ));
435 rbuf += sizeof( uint16_t );
437 /* check WriteInhibit bit if we have a ressource fork
438 * the test is done here, after some Mac trafic capture
440 if (ad_meta_fileno(ofork->of_ad) != -1) { /* META */
441 ad_getattr(ofork->of_ad, &bshort);
442 if ((bshort & htons(ATTRBIT_NOWRITE)) && (access & OPENACC_WR)) {
443 ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
446 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
447 return(AFPERR_OLOCK);
452 * synchronization locks:
455 /* don't try to lock non-existent rforks. */
456 if ((eid == ADEID_DFORK)
457 || (ad_reso_fileno(ofork->of_ad) != -1)
458 || (ofork->of_ad->ad_vers == AD_VERSION_EA)) {
459 ret = fork_setmode(obj, ofork->of_ad, eid, access, ofrefnum);
460 /* can we access the fork? */
462 ofork->of_flags |= AFPFORK_ERROR;
464 ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
467 case EAGAIN: /* return data anyway */
471 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
472 return( AFPERR_DENYCONF );
476 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_lock: %s", s_path->m_name, strerror(ret) );
477 return( AFPERR_PARAM );
480 if ((access & OPENACC_WR))
481 ofork->of_flags |= AFPFORK_ACCWR;
483 /* the file may be open read only without ressource fork */
484 if ((access & OPENACC_RD))
485 ofork->of_flags |= AFPFORK_ACCRD;
487 LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\"): fork: %" PRIu16,
488 fullpathname(s_path->m_name), ofork->of_refnum);
490 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
496 return (access & OPENACC_WR) ? AFPERR_LOCK : AFPERR_ACCESS;
500 int afp_setforkparams(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf _U_, size_t *rbuflen)
504 uint16_t ofrefnum, bitmap;
512 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
513 ibuf += sizeof( ofrefnum );
515 memcpy(&bitmap, ibuf, sizeof(bitmap));
516 bitmap = ntohs(bitmap);
517 ibuf += sizeof( bitmap );
520 if (NULL == ( ofork = of_find( ofrefnum )) ) {
521 LOG(log_error, logtype_afpd, "afp_setforkparams: of_find(%d) could not locate fork", ofrefnum );
522 return( AFPERR_PARAM );
525 if (ofork->of_vol->v_flags & AFPVOL_RO)
528 if ((ofork->of_flags & AFPFORK_ACCWR) == 0)
529 return AFPERR_ACCESS;
531 if ( ofork->of_flags & AFPFORK_DATA) {
533 } else if (ofork->of_flags & AFPFORK_RSRC) {
538 if ( ( (bitmap & ( (1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN) ))
539 && eid == ADEID_RFORK
541 ( (bitmap & ( (1<<FILPBIT_RFLEN) | (1<<FILPBIT_EXTRFLEN) ))
542 && eid == ADEID_DFORK)) {
543 return AFPERR_BITMAP;
547 if ((bitmap & ( (1<<FILPBIT_EXTDFLEN) | (1<<FILPBIT_EXTRFLEN) ))) {
548 if (obj->afp_version >= 30) {
552 return AFPERR_BITMAP;
555 if (ibuflen < 2+ sizeof(ofrefnum) + sizeof(bitmap) + is64 +4)
556 return AFPERR_PARAM ;
558 size = get_off_t(&ibuf, is64);
561 return AFPERR_PARAM; /* Some MacOS don't return an error they just don't change the size! */
564 if (bitmap == (1<<FILPBIT_DFLEN) || bitmap == (1<<FILPBIT_EXTDFLEN)) {
565 st_size = ad_size(ofork->of_ad, eid);
567 if (st_size > size &&
568 ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, size, st_size -size, ofork->of_refnum) < 0)
569 goto afp_setfork_err;
571 err = ad_dtruncate( ofork->of_ad, size );
573 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, size, st_size -size, ofork->of_refnum);
575 goto afp_setfork_err;
576 } else if (bitmap == (1<<FILPBIT_RFLEN) || bitmap == (1<<FILPBIT_EXTRFLEN)) {
577 ad_refresh(NULL, ofork->of_ad );
579 st_size = ad_size(ofork->of_ad, eid);
581 if (st_size > size &&
582 ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, size, st_size -size, ofork->of_refnum) < 0) {
583 goto afp_setfork_err;
586 err = ad_rtruncate(ofork->of_ad, mtoupath(ofork->of_vol, of_name(ofork), ofork->of_did, utf8_encoding(obj)), size);
588 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, size, st_size -size, ofork->of_refnum);
590 goto afp_setfork_err;
592 if (ad_flush( ofork->of_ad ) < 0) {
593 LOG(log_error, logtype_afpd, "afp_setforkparams(%s): ad_flush: %s", of_name(ofork), strerror(errno) );
597 return AFPERR_BITMAP;
600 if ( flushfork( ofork ) < 0 ) {
601 LOG(log_error, logtype_afpd, "afp_setforkparams(%s): flushfork: %s", of_name(ofork), strerror(errno) );
616 return AFPERR_ACCESS;
620 LOG(log_error, logtype_afpd, "afp_setforkparams: DISK FULL");
628 /* for this to work correctly, we need to check for locks before each
629 * read and write. that's most easily handled by always doing an
630 * appropriate check before each ad_read/ad_write. other things
631 * that can change files like truncate are handled internally to those
634 #define ENDBIT(a) ((a) & 0x80)
635 #define UNLOCKBIT(a) ((a) & 0x01)
638 /* ---------------------- */
639 static int byte_lock(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
642 off_t offset, length;
650 /* figure out parameters */
652 flags = *ibuf; /* first bit = endflag, lastbit = lockflag */
654 memcpy(&ofrefnum, ibuf, sizeof(ofrefnum));
655 ibuf += sizeof(ofrefnum);
657 if (NULL == ( ofork = of_find( ofrefnum )) ) {
658 LOG(log_error, logtype_afpd, "byte_lock: of_find(%d) could not locate fork", ofrefnum );
659 return( AFPERR_PARAM );
662 if ( ofork->of_flags & AFPFORK_DATA) {
664 } else if (ofork->of_flags & AFPFORK_RSRC) {
669 offset = get_off_t(&ibuf, is64);
670 length = get_off_t(&ibuf, is64);
673 length = BYTELOCK_MAX;
674 else if (!length || is_neg(is64, length)) {
676 } else if ((length >= AD_FILELOCK_BASE) && -1 == (ad_reso_fileno(ofork->of_ad))) { /* HF ?*/
681 offset += ad_size(ofork->of_ad, eid);
682 /* FIXME what do we do if file size > 2 GB and
683 it's not byte_lock_ext?
686 if (offset < 0) /* error if we have a negative offset */
689 /* if the file is a read-only file, we use read locks instead of
690 * write locks. that way, we can prevent anyone from initiating
692 lockop = UNLOCKBIT(flags) ? ADLOCK_CLR : ADLOCK_WR;
693 if (ad_lock(ofork->of_ad, eid, lockop, offset, length,
694 ofork->of_refnum) < 0) {
698 return UNLOCKBIT(flags) ? AFPERR_NORANGE : AFPERR_LOCK;
704 return UNLOCKBIT(flags) ? AFPERR_NORANGE : AFPERR_RANGEOVR;
712 *rbuflen = set_off_t (offset, rbuf, is64);
716 /* --------------------------- */
717 int afp_bytelock(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
719 return byte_lock ( obj, ibuf, ibuflen, rbuf, rbuflen , 0);
722 /* --------------------------- */
723 int afp_bytelock_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
725 return byte_lock ( obj, ibuf, ibuflen, rbuf, rbuflen , 1);
731 * Read *rbuflen bytes from fork at offset
733 * @param ofork (r) fork handle
734 * @param eid (r) data fork or ressource fork entry id
735 * @param offset (r) offset
736 * @param rbuf (r) data buffer
737 * @param rbuflen (rw) in: number of bytes to read, out: bytes read
739 * @return AFP status code
741 static int read_file(const struct ofork *ofork, int eid, off_t offset, char *rbuf, size_t *rbuflen)
745 cc = ad_read(ofork->of_ad, eid, offset, rbuf, *rbuflen);
747 LOG(log_error, logtype_afpd, "afp_read(%s): ad_read: %s", of_name(ofork), strerror(errno) );
749 return( AFPERR_PARAM );
753 if ((size_t)cc < *rbuflen)
758 static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
762 off_t offset, saveoff, reqcount, savereqcount, size;
767 /* we break the AFP spec here by not supporting nlmask and nlchar anymore */
770 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
771 ibuf += sizeof( u_short );
773 if (NULL == ( ofork = of_find( ofrefnum )) ) {
774 LOG(log_error, logtype_afpd, "afp_read: of_find(%d) could not locate fork", ofrefnum );
779 if ((ofork->of_flags & AFPFORK_ACCRD) == 0) {
784 if ( ofork->of_flags & AFPFORK_DATA) {
786 } else if (ofork->of_flags & AFPFORK_RSRC) {
788 } else { /* fork wasn't opened. this should never really happen. */
793 offset = get_off_t(&ibuf, is64);
794 reqcount = get_off_t(&ibuf, is64);
796 /* zero request count */
802 AFP_READ_START((long)reqcount);
804 /* reqcount isn't always truthful. we need to deal with that. */
805 size = ad_size(ofork->of_ad, eid);
807 LOG(log_debug, logtype_afpd,
808 "afp_read(fork: %" PRIu16 " [%s], off: %" PRIu64 ", len: %" PRIu64 ", size: %" PRIu64 ")",
809 ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "reso", offset, reqcount, size);
811 if (offset >= size) {
816 /* subtract off the offset */
817 if (reqcount + offset > size) {
818 reqcount = size - offset;
822 savereqcount = reqcount;
825 if (reqcount < 0 || offset < 0) {
830 if (obj->options.flags & OPTION_AFP_READ_LOCK) {
831 if (ad_tmplock(ofork->of_ad, eid, ADLOCK_RD, offset, reqcount, ofork->of_refnum) < 0) {
838 if (!(eid == ADEID_DFORK && ad_data_fileno(ofork->of_ad) == AD_SYMLINK) &&
839 !(obj->options.flags & OPTION_NOSENDFILE)) {
840 int fd = ad_readfile_init(ofork->of_ad, eid, &offset, 0);
841 if (dsi_stream_read_file(dsi, fd, offset, reqcount, err) < 0) {
842 LOG(log_error, logtype_afpd, "afp_read(%s): ad_readfile: %s",
843 of_name(ofork), strerror(errno));
850 *rbuflen = MIN(reqcount, dsi->server_quantum);
852 cc = read_file(ofork, eid, offset, ibuf, rbuflen);
858 LOG(log_debug, logtype_afpd,
859 "afp_read(name: \"%s\", offset: %jd, reqcount: %jd): got %jd bytes from file",
860 of_name(ofork), (intmax_t)offset, (intmax_t)reqcount, (intmax_t)*rbuflen);
865 * dsi_readinit() returns size of next read buffer. by this point,
866 * we know that we're sending some data. if we fail, something
869 if ((cc = dsi_readinit(dsi, ibuf, *rbuflen, reqcount, err)) < 0)
873 while (*rbuflen > 0) {
875 * This loop isn't really entered anymore, we've already
876 * sent the whole requested block in dsi_readinit().
878 cc = read_file(ofork, eid, offset, ibuf, rbuflen);
883 /* dsi_read() also returns buffer size of next allocation */
884 cc = dsi_read(dsi, ibuf, *rbuflen); /* send it off */
893 LOG(log_error, logtype_afpd, "afp_read(%s): %s", of_name(ofork), strerror(errno));
895 if (obj->options.flags & OPTION_AFP_READ_LOCK)
896 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, savereqcount, ofork->of_refnum);
897 obj->exit(EXITERR_CLNT);
900 if (obj->options.flags & OPTION_AFP_READ_LOCK)
901 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, savereqcount, ofork->of_refnum);
911 /* ---------------------- */
912 int afp_read(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
914 return read_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 0);
917 /* ---------------------- */
918 int afp_read_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
920 return read_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 1);
923 /* ---------------------- */
924 int afp_flush(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
932 memcpy(&vid, ibuf, sizeof(vid));
933 if (NULL == ( vol = getvolbyvid( vid )) ) {
934 return( AFPERR_PARAM );
941 int afp_flushfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
948 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
950 if (NULL == ( ofork = of_find( ofrefnum )) ) {
951 LOG(log_error, logtype_afpd, "afp_flushfork: of_find(%d) could not locate fork", ofrefnum );
952 return( AFPERR_PARAM );
955 LOG(log_debug, logtype_afpd, "afp_flushfork(fork: %s)",
956 (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
958 if ( flushfork( ofork ) < 0 ) {
959 LOG(log_error, logtype_afpd, "afp_flushfork(%s): %s", of_name(ofork), strerror(errno) );
967 There is a lot to tell about fsync, fdatasync, F_FULLFSYNC.
968 fsync(2) on OSX is implemented differently than on other platforms.
969 see: http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf.
971 int afp_syncfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
979 memcpy(&ofrefnum, ibuf, sizeof(ofrefnum));
980 ibuf += sizeof( ofrefnum );
982 if (NULL == ( ofork = of_find( ofrefnum )) ) {
983 LOG(log_error, logtype_afpd, "afpd_syncfork: of_find(%d) could not locate fork", ofrefnum );
984 return( AFPERR_PARAM );
987 LOG(log_debug, logtype_afpd, "afp_syncfork(fork: %s)",
988 (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
990 if ( flushfork( ofork ) < 0 ) {
991 LOG(log_error, logtype_afpd, "flushfork(%s): %s", of_name(ofork), strerror(errno) );
998 /* this is very similar to closefork */
999 int flushfork(struct ofork *ofork)
1003 int err = 0, doflush = 0;
1005 if ( ad_data_fileno( ofork->of_ad ) != -1 &&
1006 fsync( ad_data_fileno( ofork->of_ad )) < 0 ) {
1007 LOG(log_error, logtype_afpd, "flushfork(%s): dfile(%d) %s",
1008 of_name(ofork), ad_data_fileno(ofork->of_ad), strerror(errno) );
1012 if ( ad_reso_fileno( ofork->of_ad ) != -1 && /* HF */
1013 (ofork->of_flags & AFPFORK_RSRC)) {
1015 /* read in the rfork length */
1016 ad_refresh(NULL, ofork->of_ad);
1018 /* set the date if we're dirty */
1019 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
1020 ad_setdate(ofork->of_ad, AD_DATE_MODIFY|AD_DATE_UNIX, tv.tv_sec);
1021 ofork->of_flags &= ~AFPFORK_DIRTY;
1025 /* flush the header */
1026 if (doflush && ad_flush(ofork->of_ad) < 0)
1029 if (fsync( ad_reso_fileno( ofork->of_ad )) < 0)
1033 LOG(log_error, logtype_afpd, "flushfork(%s): hfile(%d) %s",
1034 of_name(ofork), ad_reso_fileno(ofork->of_ad), strerror(errno) );
1040 int afp_closefork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1042 struct ofork *ofork;
1047 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1049 if (NULL == ( ofork = of_find( ofrefnum )) ) {
1050 LOG(log_error, logtype_afpd, "afp_closefork: of_find(%d) could not locate fork", ofrefnum );
1051 return( AFPERR_PARAM );
1054 LOG(log_debug, logtype_afpd, "afp_closefork(fork: %" PRIu16 " [%s])",
1055 ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "rsrc");
1057 if (of_closefork(obj, ofork) < 0 ) {
1058 LOG(log_error, logtype_afpd, "afp_closefork: of_closefork: %s", strerror(errno) );
1059 return( AFPERR_PARAM );
1066 static ssize_t write_file(struct ofork *ofork, int eid,
1067 off_t offset, char *rbuf,
1072 LOG(log_debug, logtype_afpd, "write_file(off: %ju, size: %zu)",
1073 (uintmax_t)offset, rbuflen);
1075 if (( cc = ad_write(ofork->of_ad, eid, offset, 0,
1076 rbuf, rbuflen)) < 0 ) {
1081 LOG(log_error, logtype_afpd, "write_file: DISK FULL");
1082 return( AFPERR_DFULL );
1084 return AFPERR_ACCESS;
1086 LOG(log_error, logtype_afpd, "afp_write(%s): ad_write: %s", of_name(ofork), strerror(errno) );
1087 return( AFPERR_PARAM );
1096 * FPWrite. NOTE: on an error, we always use afp_write_err as
1097 * the client may have sent us a bunch of data that's not reflected
1098 * in reqcount et al.
1100 static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
1102 struct ofork *ofork;
1103 off_t offset, saveoff, reqcount, oldsize, newsize;
1104 int endflag, eid, err = AFP_OK;
1107 DSI *dsi = obj->dsi;
1108 char *rcvbuf = (char *)dsi->commands;
1109 size_t rcvbuflen = dsi->server_quantum;
1111 /* figure out parameters */
1113 endflag = ENDBIT(*ibuf);
1115 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1116 ibuf += sizeof( ofrefnum );
1118 offset = get_off_t(&ibuf, is64);
1119 reqcount = get_off_t(&ibuf, is64);
1121 if (NULL == ( ofork = of_find( ofrefnum )) ) {
1122 LOG(log_error, logtype_afpd, "afp_write: of_find(%d) could not locate fork", ofrefnum );
1127 LOG(log_debug, logtype_afpd, "afp_write(fork: %" PRIu16 " [%s], off: %" PRIu64 ", size: %" PRIu64 ")",
1128 ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "reso", offset, reqcount);
1130 if ((ofork->of_flags & AFPFORK_ACCWR) == 0) {
1131 err = AFPERR_ACCESS;
1136 writtenfork = ofork;
1139 if ( ofork->of_flags & AFPFORK_DATA) {
1141 } else if (ofork->of_flags & AFPFORK_RSRC) {
1144 err = AFPERR_ACCESS; /* should never happen */
1148 oldsize = ad_size(ofork->of_ad, eid);
1152 /* handle bogus parameters */
1153 if (reqcount < 0 || offset < 0) {
1158 newsize = ((offset + reqcount) > oldsize) ? (offset + reqcount) : oldsize;
1160 /* offset can overflow on 64-bit capable filesystems.
1161 * report disk full if that's going to happen. */
1162 if (sum_neg(is64, offset, reqcount)) {
1163 LOG(log_error, logtype_afpd, "write_fork: DISK FULL");
1168 if (!reqcount) { /* handle request counts of 0 */
1170 *rbuflen = set_off_t (offset, rbuf, is64);
1174 AFP_WRITE_START((long)reqcount);
1177 if (obj->options.flags & OPTION_AFP_READ_LOCK) {
1178 if (ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, saveoff, reqcount, ofork->of_refnum) < 0) {
1184 /* find out what we have already */
1185 cc = dsi_writeinit(dsi, rcvbuf, rcvbuflen);
1187 if (!cc || (cc = write_file(ofork, eid, offset, rcvbuf, cc)) < 0) {
1188 dsi_writeflush(dsi);
1190 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1191 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1197 #if 0 /*def HAVE_SENDFILE_WRITE*/
1198 if ((cc = ad_writefile(ofork->of_ad, eid, dsi->socket, offset, dsi->datasize)) < 0) {
1206 LOG(log_error, logtype_afpd, "afp_write: ad_writefile: %s", strerror(errno) );
1207 goto afp_write_loop;
1209 dsi_writeflush(dsi);
1211 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1212 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1217 goto afp_write_done;
1218 #endif /* 0, was HAVE_SENDFILE_WRITE */
1220 /* loop until everything gets written. currently
1221 * dsi_write handles the end case by itself. */
1222 while ((cc = dsi_write(dsi, rcvbuf, rcvbuflen))) {
1224 if ((cc = write_file(ofork, eid, offset, rcvbuf, cc)) < 0) {
1225 dsi_writeflush(dsi);
1227 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1228 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1232 LOG(log_debug, logtype_afpd, "afp_write: wrote: %jd, offset: %jd",
1233 (intmax_t)cc, (intmax_t)offset);
1238 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1239 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1240 if ( ad_meta_fileno( ofork->of_ad ) != -1 ) /* META */
1241 ofork->of_flags |= AFPFORK_DIRTY;
1243 /* we have modified any fork, remember until close_fork */
1244 ofork->of_flags |= AFPFORK_MODIFIED;
1246 /* update write count */
1247 ofork->of_vol->v_appended += (newsize > oldsize) ? (newsize - oldsize) : 0;
1249 *rbuflen = set_off_t (offset, rbuf, is64);
1254 dsi_writeinit(dsi, rcvbuf, rcvbuflen);
1255 dsi_writeflush(dsi);
1257 if (err != AFP_OK) {
1263 /* ---------------------------- */
1264 int afp_write(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
1266 return write_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 0);
1269 /* ----------------------------
1270 * FIXME need to deal with SIGXFSZ signal
1272 int afp_write_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
1274 return write_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 1);
1277 /* ---------------------------- */
1278 int afp_getforkparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1280 struct ofork *ofork;
1282 uint16_t ofrefnum, bitmap;
1285 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1286 ibuf += sizeof( ofrefnum );
1287 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1288 bitmap = ntohs( bitmap );
1289 ibuf += sizeof( bitmap );
1292 if (NULL == ( ofork = of_find( ofrefnum )) ) {
1293 LOG(log_error, logtype_afpd, "afp_getforkparams: of_find(%d) could not locate fork", ofrefnum );
1294 return( AFPERR_PARAM );
1297 if (AD_META_OPEN(ofork->of_ad)) {
1298 if ( ad_refresh(NULL, ofork->of_ad ) < 0 ) {
1299 LOG(log_error, logtype_afpd, "getforkparams(%s): ad_refresh: %s", of_name(ofork), strerror(errno) );
1300 return( AFPERR_PARAM );
1304 if (AFP_OK != (ret = getforkparams(obj, ofork, bitmap, rbuf + sizeof( u_short ), &buflen ))) {
1308 *rbuflen = buflen + sizeof( u_short );
1309 bitmap = htons( bitmap );
1310 memcpy(rbuf, &bitmap, sizeof( bitmap ));