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;
258 memcpy(&vid, ibuf, sizeof( vid ));
262 if (NULL == ( vol = getvolbyvid( vid ))) {
263 return( AFPERR_PARAM );
266 memcpy(&did, ibuf, sizeof( did ));
267 ibuf += sizeof( int );
269 if (NULL == ( dir = dirlookup( vol, did ))) {
273 memcpy(&bitmap, ibuf, sizeof( bitmap ));
274 bitmap = ntohs( bitmap );
275 ibuf += sizeof( bitmap );
276 memcpy(&access, ibuf, sizeof( access ));
277 access = ntohs( access );
278 ibuf += sizeof( access );
280 if ((vol->v_flags & AFPVOL_RO) && (access & OPENACC_WR)) {
284 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
285 return get_afp_errno(AFPERR_PARAM);
288 if (*s_path->m_name == '\0') {
290 return AFPERR_BADTYPE;
293 /* stat() data fork st is set because it's not a dir */
294 switch ( s_path->st_errno ) {
300 return (access & OPENACC_WR) ? AFPERR_LOCK : AFPERR_ACCESS;
302 LOG(log_error, logtype_afpd, "afp_openfork(%s): %s", s_path->m_name, strerror(errno));
306 upath = s_path->u_name;
307 path = s_path->m_name;
310 if (!vol_unix_priv(vol)) {
311 if (check_access(obj, vol, upath, access ) < 0) {
312 return AFPERR_ACCESS;
315 if (file_access(obj, vol, s_path, access ) < 0) {
316 return AFPERR_ACCESS;
320 if ((opened = of_findname(s_path))) {
321 adsame = opened->of_ad;
324 adflags = ADFLAGS_SETSHRMD;
326 if (fork == OPENFORK_DATA) {
328 adflags |= ADFLAGS_DF;
331 adflags |= ADFLAGS_RF;
332 if (!(access & OPENACC_WR))
333 adflags |= ADFLAGS_NORF;
336 if (access & OPENACC_WR) {
337 adflags |= ADFLAGS_RDWR;
338 if (fork != OPENFORK_DATA)
340 * We only try to create the resource
341 * fork if the user wants to open it for write acess.
343 adflags |= ADFLAGS_CREATE;
345 adflags |= ADFLAGS_RDONLY;
348 if ((ofork = of_alloc(vol, curdir, path, &ofrefnum, eid, adsame, st)) == NULL)
351 LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\", %s, %s)",
352 fullpathname(s_path->u_name),
353 (fork == OPENFORK_DATA) ? "data" : "reso",
354 !(access & OPENACC_WR) ? "O_RDONLY" : "O_RDWR");
358 /* First ad_open(), opens data or ressource fork */
359 if (ad_open(ofork->of_ad, upath, adflags, 0666) < 0) {
372 ret = AFPERR_BADTYPE;
375 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_open: %s", s_path->m_name, strerror(errno) );
382 * Create metadata if we open rw, otherwise only open existing metadata
384 if (access & OPENACC_WR) {
385 adflags = ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE;
387 adflags = ADFLAGS_HF | ADFLAGS_RDONLY;
390 if (ad_open(ofork->of_ad, upath, adflags, 0666) == 0) {
391 ofork->of_flags |= AFPFORK_META;
396 /* no metadata? We don't care! */
405 ret = AFPERR_BADTYPE;
408 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_open: %s", s_path->m_name, strerror(errno) );
414 if ((adflags & ADFLAGS_RF) && (ad_get_RF_flags( ofork->of_ad) & O_CREAT)) {
415 if (ad_setname(ofork->of_ad, path)) {
416 ad_flush( ofork->of_ad );
420 if ((ret = getforkparams(obj, ofork, bitmap, rbuf + 2 * sizeof(int16_t), &buflen)) != AFP_OK) {
421 ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
425 *rbuflen = buflen + 2 * sizeof( uint16_t );
426 bitmap = htons( bitmap );
427 memcpy(rbuf, &bitmap, sizeof( uint16_t ));
428 rbuf += sizeof( uint16_t );
430 /* check WriteInhibit bit if we have a ressource fork
431 * the test is done here, after some Mac trafic capture
433 if (ad_meta_fileno(ofork->of_ad) != -1) { /* META */
434 ad_getattr(ofork->of_ad, &bshort);
435 if ((bshort & htons(ATTRBIT_NOWRITE)) && (access & OPENACC_WR)) {
436 ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
439 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
440 return(AFPERR_OLOCK);
445 * synchronization locks:
448 /* don't try to lock non-existent rforks. */
449 if ((eid == ADEID_DFORK)
450 || (ad_reso_fileno(ofork->of_ad) != -1)
451 || (ofork->of_ad->ad_vers == AD_VERSION_EA)) {
452 ret = fork_setmode(obj, ofork->of_ad, eid, access, ofrefnum);
453 /* can we access the fork? */
455 ofork->of_flags |= AFPFORK_ERROR;
457 ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
460 case EAGAIN: /* return data anyway */
464 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
465 return( AFPERR_DENYCONF );
469 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_lock: %s", s_path->m_name, strerror(ret) );
470 return( AFPERR_PARAM );
473 if ((access & OPENACC_WR))
474 ofork->of_flags |= AFPFORK_ACCWR;
476 /* the file may be open read only without ressource fork */
477 if ((access & OPENACC_RD))
478 ofork->of_flags |= AFPFORK_ACCRD;
480 LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\"): fork: %" PRIu16,
481 fullpathname(s_path->m_name), ofork->of_refnum);
483 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
489 return (access & OPENACC_WR) ? AFPERR_LOCK : AFPERR_ACCESS;
493 int afp_setforkparams(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf _U_, size_t *rbuflen)
497 uint16_t ofrefnum, bitmap;
505 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
506 ibuf += sizeof( ofrefnum );
508 memcpy(&bitmap, ibuf, sizeof(bitmap));
509 bitmap = ntohs(bitmap);
510 ibuf += sizeof( bitmap );
513 if (NULL == ( ofork = of_find( ofrefnum )) ) {
514 LOG(log_error, logtype_afpd, "afp_setforkparams: of_find(%d) could not locate fork", ofrefnum );
515 return( AFPERR_PARAM );
518 if (ofork->of_vol->v_flags & AFPVOL_RO)
521 if ((ofork->of_flags & AFPFORK_ACCWR) == 0)
522 return AFPERR_ACCESS;
524 if ( ofork->of_flags & AFPFORK_DATA) {
526 } else if (ofork->of_flags & AFPFORK_RSRC) {
531 if ( ( (bitmap & ( (1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN) ))
532 && eid == ADEID_RFORK
534 ( (bitmap & ( (1<<FILPBIT_RFLEN) | (1<<FILPBIT_EXTRFLEN) ))
535 && eid == ADEID_DFORK)) {
536 return AFPERR_BITMAP;
540 if ((bitmap & ( (1<<FILPBIT_EXTDFLEN) | (1<<FILPBIT_EXTRFLEN) ))) {
541 if (obj->afp_version >= 30) {
545 return AFPERR_BITMAP;
548 if (ibuflen < 2+ sizeof(ofrefnum) + sizeof(bitmap) + is64 +4)
549 return AFPERR_PARAM ;
551 size = get_off_t(&ibuf, is64);
554 return AFPERR_PARAM; /* Some MacOS don't return an error they just don't change the size! */
557 if (bitmap == (1<<FILPBIT_DFLEN) || bitmap == (1<<FILPBIT_EXTDFLEN)) {
558 st_size = ad_size(ofork->of_ad, eid);
560 if (st_size > size &&
561 ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, size, st_size -size, ofork->of_refnum) < 0)
562 goto afp_setfork_err;
564 err = ad_dtruncate( ofork->of_ad, size );
566 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, size, st_size -size, ofork->of_refnum);
568 goto afp_setfork_err;
569 } else if (bitmap == (1<<FILPBIT_RFLEN) || bitmap == (1<<FILPBIT_EXTRFLEN)) {
570 ad_refresh(NULL, ofork->of_ad );
572 st_size = ad_size(ofork->of_ad, eid);
574 if (st_size > size &&
575 ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, size, st_size -size, ofork->of_refnum) < 0) {
576 goto afp_setfork_err;
579 err = ad_rtruncate(ofork->of_ad, size);
581 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, size, st_size -size, ofork->of_refnum);
583 goto afp_setfork_err;
585 if (ad_flush( ofork->of_ad ) < 0) {
586 LOG(log_error, logtype_afpd, "afp_setforkparams(%s): ad_flush: %s", of_name(ofork), strerror(errno) );
590 return AFPERR_BITMAP;
593 if ( flushfork( ofork ) < 0 ) {
594 LOG(log_error, logtype_afpd, "afp_setforkparams(%s): flushfork: %s", of_name(ofork), strerror(errno) );
609 return AFPERR_ACCESS;
613 LOG(log_error, logtype_afpd, "afp_setforkparams: DISK FULL");
621 /* for this to work correctly, we need to check for locks before each
622 * read and write. that's most easily handled by always doing an
623 * appropriate check before each ad_read/ad_write. other things
624 * that can change files like truncate are handled internally to those
627 #define ENDBIT(a) ((a) & 0x80)
628 #define UNLOCKBIT(a) ((a) & 0x01)
631 /* ---------------------- */
632 static int byte_lock(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
635 off_t offset, length;
643 /* figure out parameters */
645 flags = *ibuf; /* first bit = endflag, lastbit = lockflag */
647 memcpy(&ofrefnum, ibuf, sizeof(ofrefnum));
648 ibuf += sizeof(ofrefnum);
650 if (NULL == ( ofork = of_find( ofrefnum )) ) {
651 LOG(log_error, logtype_afpd, "byte_lock: of_find(%d) could not locate fork", ofrefnum );
652 return( AFPERR_PARAM );
655 if ( ofork->of_flags & AFPFORK_DATA) {
657 } else if (ofork->of_flags & AFPFORK_RSRC) {
662 offset = get_off_t(&ibuf, is64);
663 length = get_off_t(&ibuf, is64);
666 length = BYTELOCK_MAX;
667 else if (!length || is_neg(is64, length)) {
669 } else if ((length >= AD_FILELOCK_BASE) && -1 == (ad_reso_fileno(ofork->of_ad))) { /* HF ?*/
674 offset += ad_size(ofork->of_ad, eid);
675 /* FIXME what do we do if file size > 2 GB and
676 it's not byte_lock_ext?
679 if (offset < 0) /* error if we have a negative offset */
682 /* if the file is a read-only file, we use read locks instead of
683 * write locks. that way, we can prevent anyone from initiating
685 lockop = UNLOCKBIT(flags) ? ADLOCK_CLR : ADLOCK_WR;
686 if (ad_lock(ofork->of_ad, eid, lockop, offset, length,
687 ofork->of_refnum) < 0) {
691 return UNLOCKBIT(flags) ? AFPERR_NORANGE : AFPERR_LOCK;
697 return UNLOCKBIT(flags) ? AFPERR_NORANGE : AFPERR_RANGEOVR;
705 *rbuflen = set_off_t (offset, rbuf, is64);
709 /* --------------------------- */
710 int afp_bytelock(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
712 return byte_lock ( obj, ibuf, ibuflen, rbuf, rbuflen , 0);
715 /* --------------------------- */
716 int afp_bytelock_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
718 return byte_lock ( obj, ibuf, ibuflen, rbuf, rbuflen , 1);
724 * Read *rbuflen bytes from fork at offset
726 * @param ofork (r) fork handle
727 * @param eid (r) data fork or ressource fork entry id
728 * @param offset (r) offset
729 * @param rbuf (r) data buffer
730 * @param rbuflen (rw) in: number of bytes to read, out: bytes read
732 * @return AFP status code
734 static int read_file(const struct ofork *ofork, int eid, off_t offset, char *rbuf, size_t *rbuflen)
740 cc = ad_read(ofork->of_ad, eid, offset, rbuf, *rbuflen);
742 LOG(log_error, logtype_afpd, "afp_read(%s): ad_read: %s", of_name(ofork), strerror(errno) );
744 return( AFPERR_PARAM );
748 if ((size_t)cc < *rbuflen)
753 static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
757 off_t offset, saveoff, reqcount, savereqcount, size;
762 /* we break the AFP spec here by not supporting nlmask and nlchar anymore */
765 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
766 ibuf += sizeof( u_short );
768 if (NULL == ( ofork = of_find( ofrefnum )) ) {
769 LOG(log_error, logtype_afpd, "afp_read: of_find(%d) could not locate fork", ofrefnum );
774 if ((ofork->of_flags & AFPFORK_ACCRD) == 0) {
779 if ( ofork->of_flags & AFPFORK_DATA) {
781 } else if (ofork->of_flags & AFPFORK_RSRC) {
783 } else { /* fork wasn't opened. this should never really happen. */
788 offset = get_off_t(&ibuf, is64);
789 reqcount = get_off_t(&ibuf, is64);
791 /* zero request count */
797 /* reqcount isn't always truthful. we need to deal with that. */
798 size = ad_size(ofork->of_ad, eid);
800 LOG(log_debug, logtype_afpd,
801 "afp_read(fork: %" PRIu16 " [%s], off: %" PRIu64 ", len: %" PRIu64 ", size: %" PRIu64 ")",
802 ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "reso", offset, reqcount, size);
809 /* subtract off the offset */
810 if (reqcount + offset > size) {
811 reqcount = size - offset;
815 savereqcount = reqcount;
818 if (reqcount < 0 || offset < 0) {
823 if (obj->options.flags & OPTION_AFP_READ_LOCK) {
824 if (ad_tmplock(ofork->of_ad, eid, ADLOCK_RD, offset, reqcount, ofork->of_refnum) < 0) {
831 if (!(eid == ADEID_DFORK && ad_data_fileno(ofork->of_ad) == AD_SYMLINK) &&
832 !(obj->options.flags & OPTION_NOSENDFILE)) {
833 int fd = ad_readfile_init(ofork->of_ad, eid, &offset, 0);
834 if (dsi_stream_read_file(dsi, fd, offset, reqcount, err) < 0) {
835 LOG(log_error, logtype_afpd, "afp_read(%s): ad_readfile: %s",
836 of_name(ofork), strerror(errno));
843 *rbuflen = MIN(reqcount, *rbuflen);
845 err = read_file(ofork, eid, offset, rbuf, rbuflen);
849 LOG(log_debug, logtype_afpd,
850 "afp_read(name: \"%s\", offset: %jd, reqcount: %jd): got %jd bytes from file",
851 of_name(ofork), (intmax_t)offset, (intmax_t)reqcount, (intmax_t)*rbuflen);
856 * dsi_readinit() returns size of next read buffer. by this point,
857 * we know that we're sending some data. if we fail, something
860 if ((cc = dsi_readinit(dsi, rbuf, *rbuflen, reqcount, err)) < 0)
864 while (*rbuflen > 0) {
865 cc = read_file(ofork, eid, offset, rbuf, rbuflen);
870 /* dsi_read() also returns buffer size of next allocation */
871 cc = dsi_read(dsi, rbuf, *rbuflen); /* send it off */
880 LOG(log_error, logtype_afpd, "afp_read(%s): %s", of_name(ofork), strerror(errno));
882 if (obj->options.flags & OPTION_AFP_READ_LOCK)
883 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, savereqcount, ofork->of_refnum);
884 obj->exit(EXITERR_CLNT);
887 if (obj->options.flags & OPTION_AFP_READ_LOCK)
888 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, savereqcount, ofork->of_refnum);
896 /* ---------------------- */
897 int afp_read(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
899 return read_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 0);
902 /* ---------------------- */
903 int afp_read_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
905 return read_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 1);
908 /* ---------------------- */
909 int afp_flush(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
917 memcpy(&vid, ibuf, sizeof(vid));
918 if (NULL == ( vol = getvolbyvid( vid )) ) {
919 return( AFPERR_PARAM );
926 int afp_flushfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
933 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
935 if (NULL == ( ofork = of_find( ofrefnum )) ) {
936 LOG(log_error, logtype_afpd, "afp_flushfork: of_find(%d) could not locate fork", ofrefnum );
937 return( AFPERR_PARAM );
940 LOG(log_debug, logtype_afpd, "afp_flushfork(fork: %s)",
941 (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
943 if ( flushfork( ofork ) < 0 ) {
944 LOG(log_error, logtype_afpd, "afp_flushfork(%s): %s", of_name(ofork), strerror(errno) );
952 There is a lot to tell about fsync, fdatasync, F_FULLFSYNC.
953 fsync(2) on OSX is implemented differently than on other platforms.
954 see: http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf.
956 int afp_syncfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
964 memcpy(&ofrefnum, ibuf, sizeof(ofrefnum));
965 ibuf += sizeof( ofrefnum );
967 if (NULL == ( ofork = of_find( ofrefnum )) ) {
968 LOG(log_error, logtype_afpd, "afpd_syncfork: of_find(%d) could not locate fork", ofrefnum );
969 return( AFPERR_PARAM );
972 LOG(log_debug, logtype_afpd, "afp_syncfork(fork: %s)",
973 (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
975 if ( flushfork( ofork ) < 0 ) {
976 LOG(log_error, logtype_afpd, "flushfork(%s): %s", of_name(ofork), strerror(errno) );
983 /* this is very similar to closefork */
984 int flushfork(struct ofork *ofork)
988 int err = 0, doflush = 0;
990 if ( ad_data_fileno( ofork->of_ad ) != -1 &&
991 fsync( ad_data_fileno( ofork->of_ad )) < 0 ) {
992 LOG(log_error, logtype_afpd, "flushfork(%s): dfile(%d) %s",
993 of_name(ofork), ad_data_fileno(ofork->of_ad), strerror(errno) );
997 if ( ad_reso_fileno( ofork->of_ad ) != -1 && /* HF */
998 (ofork->of_flags & AFPFORK_RSRC)) {
1000 /* read in the rfork length */
1001 ad_refresh(NULL, ofork->of_ad);
1003 /* set the date if we're dirty */
1004 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
1005 ad_setdate(ofork->of_ad, AD_DATE_MODIFY|AD_DATE_UNIX, tv.tv_sec);
1006 ofork->of_flags &= ~AFPFORK_DIRTY;
1010 /* flush the header */
1011 if (doflush && ad_flush(ofork->of_ad) < 0)
1014 if (fsync( ad_reso_fileno( ofork->of_ad )) < 0)
1018 LOG(log_error, logtype_afpd, "flushfork(%s): hfile(%d) %s",
1019 of_name(ofork), ad_reso_fileno(ofork->of_ad), strerror(errno) );
1025 int afp_closefork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1027 struct ofork *ofork;
1032 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1034 if (NULL == ( ofork = of_find( ofrefnum )) ) {
1035 LOG(log_error, logtype_afpd, "afp_closefork: of_find(%d) could not locate fork", ofrefnum );
1036 return( AFPERR_PARAM );
1039 LOG(log_debug, logtype_afpd, "afp_closefork(fork: %" PRIu16 " [%s])",
1040 ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "rsrc");
1042 if (of_closefork(obj, ofork) < 0 ) {
1043 LOG(log_error, logtype_afpd, "afp_closefork(%s): of_closefork: %s", of_name(ofork), strerror(errno) );
1044 return( AFPERR_PARAM );
1051 static ssize_t write_file(struct ofork *ofork, int eid,
1052 off_t offset, char *rbuf,
1058 LOG(log_debug, logtype_afpd, "write_file(off: %ju, size: %zu)",
1059 (uintmax_t)offset, rbuflen);
1061 if (( cc = ad_write(ofork->of_ad, eid, offset, 0,
1062 rbuf, rbuflen)) < 0 ) {
1067 LOG(log_error, logtype_afpd, "write_file: DISK FULL");
1068 return( AFPERR_DFULL );
1070 return AFPERR_ACCESS;
1072 LOG(log_error, logtype_afpd, "afp_write(%s): ad_write: %s", of_name(ofork), strerror(errno) );
1073 return( AFPERR_PARAM );
1082 * FPWrite. NOTE: on an error, we always use afp_write_err as
1083 * the client may have sent us a bunch of data that's not reflected
1084 * in reqcount et al.
1086 static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
1088 struct ofork *ofork;
1089 off_t offset, saveoff, reqcount, oldsize, newsize;
1090 int endflag, eid, err = AFP_OK;
1093 DSI *dsi = obj->dsi;
1094 char *rcvbuf = dsi->commands;
1095 size_t rcvbuflen = dsi->server_quantum;
1097 /* figure out parameters */
1099 endflag = ENDBIT(*ibuf);
1101 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1102 ibuf += sizeof( ofrefnum );
1104 offset = get_off_t(&ibuf, is64);
1105 reqcount = get_off_t(&ibuf, is64);
1107 if (NULL == ( ofork = of_find( ofrefnum )) ) {
1108 LOG(log_error, logtype_afpd, "afp_write: of_find(%d) could not locate fork", ofrefnum );
1113 LOG(log_debug, logtype_afpd, "afp_write(fork: %" PRIu16 " [%s], off: %" PRIu64 ", size: %" PRIu64 ")",
1114 ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "reso", offset, reqcount);
1116 if ((ofork->of_flags & AFPFORK_ACCWR) == 0) {
1117 err = AFPERR_ACCESS;
1122 writtenfork = ofork;
1125 if ( ofork->of_flags & AFPFORK_DATA) {
1127 } else if (ofork->of_flags & AFPFORK_RSRC) {
1130 err = AFPERR_ACCESS; /* should never happen */
1134 oldsize = ad_size(ofork->of_ad, eid);
1138 /* handle bogus parameters */
1139 if (reqcount < 0 || offset < 0) {
1144 newsize = ((offset + reqcount) > oldsize) ? (offset + reqcount) : oldsize;
1146 /* offset can overflow on 64-bit capable filesystems.
1147 * report disk full if that's going to happen. */
1148 if (sum_neg(is64, offset, reqcount)) {
1149 LOG(log_error, logtype_afpd, "write_fork: DISK FULL");
1154 if (!reqcount) { /* handle request counts of 0 */
1156 *rbuflen = set_off_t (offset, rbuf, is64);
1161 if (obj->options.flags & OPTION_AFP_READ_LOCK) {
1162 if (ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, saveoff, reqcount, ofork->of_refnum) < 0) {
1168 /* find out what we have already */
1169 cc = dsi_writeinit(dsi, rcvbuf, rcvbuflen);
1171 if (!cc || (cc = write_file(ofork, eid, offset, rcvbuf, cc)) < 0) {
1172 dsi_writeflush(dsi);
1174 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1175 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1181 #if 0 /*def HAVE_SENDFILE_WRITE*/
1182 if ((cc = ad_writefile(ofork->of_ad, eid, dsi->socket, offset, dsi->datasize)) < 0) {
1190 LOG(log_error, logtype_afpd, "afp_write: ad_writefile: %s", strerror(errno) );
1191 goto afp_write_loop;
1193 dsi_writeflush(dsi);
1195 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1196 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1201 goto afp_write_done;
1202 #endif /* 0, was HAVE_SENDFILE_WRITE */
1204 /* loop until everything gets written. currently
1205 * dsi_write handles the end case by itself. */
1206 while ((cc = dsi_write(dsi, rcvbuf, rcvbuflen))) {
1208 if ((cc = write_file(ofork, eid, offset, rcvbuf, cc)) < 0) {
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);
1216 LOG(log_debug, logtype_afpd, "afp_write: wrote: %jd, offset: %jd",
1217 (intmax_t)cc, (intmax_t)offset);
1222 if (obj->options.flags & OPTION_AFP_READ_LOCK)
1223 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1224 if ( ad_meta_fileno( ofork->of_ad ) != -1 ) /* META */
1225 ofork->of_flags |= AFPFORK_DIRTY;
1227 /* we have modified any fork, remember until close_fork */
1228 ofork->of_flags |= AFPFORK_MODIFIED;
1230 /* update write count */
1231 ofork->of_vol->v_appended += (newsize > oldsize) ? (newsize - oldsize) : 0;
1233 *rbuflen = set_off_t (offset, rbuf, is64);
1237 dsi_writeinit(dsi, rcvbuf, rcvbuflen);
1238 dsi_writeflush(dsi);
1240 if (err != AFP_OK) {
1246 /* ---------------------------- */
1247 int afp_write(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
1249 return write_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 0);
1252 /* ----------------------------
1253 * FIXME need to deal with SIGXFSZ signal
1255 int afp_write_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
1257 return write_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 1);
1260 /* ---------------------------- */
1261 int afp_getforkparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1263 struct ofork *ofork;
1265 uint16_t ofrefnum, bitmap;
1268 memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1269 ibuf += sizeof( ofrefnum );
1270 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1271 bitmap = ntohs( bitmap );
1272 ibuf += sizeof( bitmap );
1275 if (NULL == ( ofork = of_find( ofrefnum )) ) {
1276 LOG(log_error, logtype_afpd, "afp_getforkparams: of_find(%d) could not locate fork", ofrefnum );
1277 return( AFPERR_PARAM );
1280 if (AD_META_OPEN(ofork->of_ad)) {
1281 if ( ad_refresh(NULL, ofork->of_ad ) < 0 ) {
1282 LOG(log_error, logtype_afpd, "getforkparams(%s): ad_refresh: %s", of_name(ofork), strerror(errno) );
1283 return( AFPERR_PARAM );
1287 if (AFP_OK != (ret = getforkparams(obj, ofork, bitmap, rbuf + sizeof( u_short ), &buflen ))) {
1291 *rbuflen = buflen + sizeof( u_short );
1292 bitmap = htons( bitmap );
1293 memcpy(rbuf, &bitmap, sizeof( bitmap ));