2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
3 * Copyright (c) 2010 Frank Lahm
6 * Permission to use, copy, modify, and distribute this software and
7 * its documentation for any purpose and without fee is hereby granted,
8 * provided that the above copyright notice appears in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation, and that the name of The University
11 * of Michigan not be used in advertising or publicity pertaining to
12 * distribution of the software without specific, written prior
13 * permission. This software is supplied as is without expressed or
14 * implied warranties of any kind.
16 * Research Systems Unix Group
17 * The University of Michigan
19 * 535 W. William Street
22 * netatalk@itd.umich.edu
27 #endif /* HAVE_CONFIG_H */
33 #include <arpa/inet.h>
35 #include <atalk/adouble.h>
37 #include <atalk/logger.h>
38 #include <atalk/bstrlib.h>
39 #include <atalk/bstradd.h>
40 #include <atalk/errchk.h>
41 #include <atalk/util.h>
45 static const uint32_t set_eid[] = {
48 AD_DEV, AD_INO, AD_SYN, AD_ID
51 #define EID_DISK(a) (set_eid[a])
54 * Prepare ad->ad_data buffer from struct adouble for writing on disk
56 int ad_rebuild_adouble_header_v2(struct adouble *ad)
63 LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_v2");
67 temp = htonl( ad->ad_magic );
68 memcpy(buf, &temp, sizeof( temp ));
69 buf += sizeof( temp );
71 temp = htonl( ad->ad_version );
72 memcpy(buf, &temp, sizeof( temp ));
73 buf += sizeof( temp );
75 buf += sizeof( ad->ad_filler );
78 buf += sizeof( nent );
79 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
80 if (ad->ad_eid[ eid ].ade_off == 0)
82 temp = htonl( EID_DISK(eid) );
83 memcpy(buf, &temp, sizeof( temp ));
84 buf += sizeof( temp );
86 temp = htonl( ad->ad_eid[ eid ].ade_off );
87 memcpy(buf, &temp, sizeof( temp ));
88 buf += sizeof( temp );
90 temp = htonl( ad->ad_eid[ eid ].ade_len );
91 memcpy(buf, &temp, sizeof( temp ));
92 buf += sizeof( temp );
96 memcpy(nentp, &nent, sizeof( nent ));
98 return ad_getentryoff(ad, ADEID_RFORK);
101 int ad_rebuild_adouble_header_ea(struct adouble *ad)
108 LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_ea");
112 temp = htonl( ad->ad_magic );
113 memcpy(buf, &temp, sizeof( temp ));
114 buf += sizeof( temp );
116 temp = htonl( ad->ad_version );
117 memcpy(buf, &temp, sizeof( temp ));
118 buf += sizeof( temp );
120 buf += sizeof( ad->ad_filler );
123 buf += sizeof( nent );
124 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
125 if ((ad->ad_eid[ eid ].ade_off == 0) || (eid == ADEID_RFORK))
127 temp = htonl( EID_DISK(eid) );
128 memcpy(buf, &temp, sizeof( temp ));
129 buf += sizeof( temp );
131 temp = htonl( ad->ad_eid[ eid ].ade_off );
132 memcpy(buf, &temp, sizeof( temp ));
133 buf += sizeof( temp );
135 temp = htonl( ad->ad_eid[ eid ].ade_len );
136 memcpy(buf, &temp, sizeof( temp ));
137 buf += sizeof( temp );
140 nent = htons( nent );
141 memcpy(nentp, &nent, sizeof( nent ));
147 * Prepare adbuf buffer from struct adouble for writing on disk
149 int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
155 LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_osx");
159 temp = htonl( ad->ad_magic );
160 memcpy(buf, &temp, sizeof( temp ));
161 buf += sizeof( temp );
163 temp = htonl( ad->ad_version );
164 memcpy(buf, &temp, sizeof( temp ));
165 buf += sizeof( temp );
167 memcpy(buf, AD_FILLER_NETATALK, strlen(AD_FILLER_NETATALK));
168 buf += sizeof( ad->ad_filler );
170 nent = htons(ADEID_NUM_OSX);
171 memcpy(buf, &nent, sizeof( nent ));
172 buf += sizeof( nent );
175 temp = htonl(EID_DISK(ADEID_FINDERI));
176 memcpy(buf, &temp, sizeof( temp ));
177 buf += sizeof( temp );
179 temp = htonl(ADEDOFF_FINDERI_OSX);
180 memcpy(buf, &temp, sizeof( temp ));
181 buf += sizeof( temp );
183 temp = htonl(ADEDLEN_FINDERI);
184 memcpy(buf, &temp, sizeof( temp ));
185 buf += sizeof( temp );
187 memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
190 temp = htonl( EID_DISK(ADEID_RFORK) );
191 memcpy(buf, &temp, sizeof( temp ));
192 buf += sizeof( temp );
194 temp = htonl(ADEDOFF_RFORK_OSX);
195 memcpy(buf, &temp, sizeof( temp ));
196 buf += sizeof( temp );
198 temp = htonl( ad->ad_rlen);
199 memcpy(buf, &temp, sizeof( temp ));
200 buf += sizeof( temp );
202 return AD_DATASZ_OSX;
205 /* -------------------
206 * XXX copy only header with same size or comment
207 * doesn't work well for adouble with different version.
210 int ad_copy_header(struct adouble *add, struct adouble *ads)
215 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
216 if ( ads->ad_eid[ eid ].ade_off == 0 || add->ad_eid[ eid ].ade_off == 0 )
219 len = ads->ad_eid[ eid ].ade_len;
228 ad_setentrylen( add, eid, len );
229 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
232 add->ad_rlen = ads->ad_rlen;
234 if (((ads->ad_vers == AD_VERSION2) && (add->ad_vers == AD_VERSION_EA))
235 || ((ads->ad_vers == AD_VERSION_EA) && (add->ad_vers == AD_VERSION2))) {
237 memcpy(&id, ad_entry(add, ADEID_PRIVID), sizeof(cnid_t));
239 memcpy(ad_entry(add, ADEID_PRIVID), &id, sizeof(cnid_t));
244 static int ad_flush_hf(struct adouble *ad)
250 LOG(log_debug, logtype_ad, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
254 switch (ad->ad_vers) {
259 adf = &ad->ad_data_fork;
262 LOG(log_error, logtype_ad, "ad_flush: unexpected adouble version");
266 if ((adf->adf_flags & O_RDWR)
267 || ((ad->ad_adflags & ADFLAGS_DIR) && (ad->ad_adflags & ADFLAGS_RDWR))) {
268 if (ad_getentryoff(ad, ADEID_RFORK)) {
269 if (ad->ad_rlen > 0xffffffff)
270 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
272 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
274 len = ad->ad_ops->ad_rebuild_header(ad);
276 switch (ad->ad_vers) {
278 if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
285 if (AD_META_OPEN(ad)) {
286 if (ad->ad_adflags & ADFLAGS_DIR) {
287 EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
288 EC_NEG1_LOG( fchdir(ad_data_fileno(ad)) );
289 EC_ZERO_LOGSTR( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0),
290 "sys_lsetxattr(\"%s\"): %s", fullpathname(".") ,strerror(errno));
291 EC_NEG1_LOG( fchdir(cwd) );
292 EC_NEG1_LOG( close(cwd) );
295 EC_ZERO_LOG( sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
300 LOG(log_error, logtype_ad, "ad_flush: unexpected adouble version");
307 if (fchdir(cwd) != 0) {
308 AFP_PANIC("ad_flush: cant fchdir");
315 /* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
316 static int ad_flush_rf(struct adouble *ad)
319 char adbuf[AD_DATASZ_OSX];
324 if (ad->ad_vers != AD_VERSION_EA)
327 LOG(log_debug, logtype_ad, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
329 if ((ad->ad_rfp->adf_flags & O_RDWR)) {
330 if (ad_getentryoff(ad, ADEID_RFORK)) {
331 if (ad->ad_rlen > 0xffffffff)
332 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
334 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
336 len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
338 if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
347 int ad_flush(struct adouble *ad)
351 LOG(log_debug, logtype_ad, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
353 if (AD_META_OPEN(ad)) {
354 EC_ZERO( ad_flush_hf(ad) );
357 if (AD_RSRC_OPEN(ad)) {
358 EC_ZERO( ad_flush_rf(ad) );
365 static int ad_data_closefd(struct adouble *ad)
369 if (ad_data_fileno(ad) == AD_SYMLINK) {
370 free(ad->ad_data_fork.adf_syml);
371 ad->ad_data_fork.adf_syml = NULL;
373 if (close(ad_data_fileno(ad)) < 0)
376 ad_data_fileno(ad) = -1;
381 * Close a struct adouble freeing all resources
383 int ad_close(struct adouble *ad, int adflags)
391 LOG(log_debug, logtype_ad,
392 "ad_close(%s): BEGIN: {d: %d, m: %d, r: %d} "
393 "[dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
394 adflags2logstr(adflags),
395 ad->ad_data_refcount, ad->ad_meta_refcount, ad->ad_reso_refcount,
396 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
397 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
398 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
400 if (adflags & (ADFLAGS_SETSHRMD | ADFLAGS_CHECK_OF)) {
401 /* sharemode locks are stored in the data fork, adouble:v2 needs this extra handling */
402 adflags |= ADFLAGS_DF;
405 if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF))
406 adflags |= ADFLAGS_HF;
408 if ((adflags & ADFLAGS_DF) && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == AD_SYMLINK)) {
409 if (ad->ad_data_refcount)
410 if (--ad->ad_data_refcount == 0)
411 adf_lock_free(&ad->ad_data_fork);
412 if (--ad->ad_data_fork.adf_refcount == 0) {
413 if (ad_data_closefd(ad) < 0)
418 if ((adflags & ADFLAGS_HF) && (ad_meta_fileno(ad) != -1)) {
419 if (ad->ad_meta_refcount)
420 ad->ad_meta_refcount--;
421 if (!(--ad->ad_mdp->adf_refcount)) {
422 if (close( ad_meta_fileno(ad)) < 0)
424 ad_meta_fileno(ad) = -1;
428 if (adflags & ADFLAGS_RF) {
429 /* HF is automatically opened when opening an RF, close it. */
430 if ((ad->ad_vers == AD_VERSION2) && (ad_meta_fileno(ad) != -1)) {
431 if (ad->ad_meta_refcount)
432 ad->ad_meta_refcount--;
433 if (!(--ad->ad_mdp->adf_refcount)) {
434 if (close( ad_meta_fileno(ad)) < 0)
436 ad_meta_fileno(ad) = -1;
440 if (ad->ad_reso_refcount)
441 if (--ad->ad_reso_refcount == 0)
442 adf_lock_free(ad->ad_rfp);
443 if (ad->ad_vers == AD_VERSION_EA) {
444 if ((ad_reso_fileno(ad) != -1)
445 && !(--ad->ad_rfp->adf_refcount)) {
446 if (close(ad->ad_rfp->adf_fd) < 0)
449 ad_reso_fileno(ad) = -1;
454 LOG(log_debug, logtype_ad,
455 "ad_close(%s): END: %d {d: %d, m: %d, r: %d} "
456 "[dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
457 adflags2logstr(adflags), err,
458 ad->ad_data_refcount, ad->ad_meta_refcount, ad->ad_reso_refcount,
459 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
460 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
461 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);