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)
64 LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_v2");
68 temp = htonl( ad->ad_magic );
69 memcpy(buf, &temp, sizeof( temp ));
70 buf += sizeof( temp );
72 temp = htonl( ad->ad_version );
73 memcpy(buf, &temp, sizeof( temp ));
74 buf += sizeof( temp );
76 buf += sizeof( ad->ad_filler );
79 buf += sizeof( nent );
80 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
81 if (ad->ad_eid[ eid ].ade_off == 0)
83 temp = htonl( EID_DISK(eid) );
84 memcpy(buf, &temp, sizeof( temp ));
85 buf += sizeof( temp );
87 temp = htonl( ad->ad_eid[ eid ].ade_off );
88 memcpy(buf, &temp, sizeof( temp ));
89 buf += sizeof( temp );
91 temp = htonl( ad->ad_eid[ eid ].ade_len );
92 memcpy(buf, &temp, sizeof( temp ));
93 buf += sizeof( temp );
97 memcpy(nentp, &nent, sizeof( nent ));
99 return ad_getentryoff(ad, ADEID_RFORK);
102 int ad_rebuild_adouble_header_ea(struct adouble *ad)
110 LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_ea");
114 temp = htonl( ad->ad_magic );
115 memcpy(buf, &temp, sizeof( temp ));
116 buf += sizeof( temp );
118 temp = htonl( ad->ad_version );
119 memcpy(buf, &temp, sizeof( temp ));
120 buf += sizeof( temp );
122 buf += sizeof( ad->ad_filler );
125 buf += sizeof( nent );
126 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
127 if ((ad->ad_eid[ eid ].ade_off == 0) || (eid == ADEID_RFORK))
129 temp = htonl( EID_DISK(eid) );
130 memcpy(buf, &temp, sizeof( temp ));
131 buf += sizeof( temp );
133 temp = htonl( ad->ad_eid[ eid ].ade_off );
134 memcpy(buf, &temp, sizeof( temp ));
135 buf += sizeof( temp );
137 temp = htonl( ad->ad_eid[ eid ].ade_len );
138 memcpy(buf, &temp, sizeof( temp ));
139 buf += sizeof( temp );
142 nent = htons( nent );
143 memcpy(nentp, &nent, sizeof( nent ));
149 * Prepare adbuf buffer from struct adouble for writing on disk
151 static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
158 LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_osx");
162 temp = htonl( ad->ad_magic );
163 memcpy(buf, &temp, sizeof( temp ));
164 buf += sizeof( temp );
166 temp = htonl( ad->ad_version );
167 memcpy(buf, &temp, sizeof( temp ));
168 buf += sizeof( temp );
170 memset(buf, 0, sizeof(ad->ad_filler));
171 buf += sizeof( ad->ad_filler );
173 nent = htons(ADEID_NUM_OSX);
174 memcpy(buf, &nent, sizeof( nent ));
175 buf += sizeof( nent );
178 temp = htonl(EID_DISK(ADEID_FINDERI));
179 memcpy(buf, &temp, sizeof( temp ));
180 buf += sizeof( temp );
182 temp = htonl(ADEDOFF_FINDERI_OSX);
183 memcpy(buf, &temp, sizeof( temp ));
184 buf += sizeof( temp );
186 temp = htonl(ADEDLEN_FINDERI);
187 memcpy(buf, &temp, sizeof( temp ));
188 buf += sizeof( temp );
190 memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
193 temp = htonl( EID_DISK(ADEID_RFORK) );
194 memcpy(buf, &temp, sizeof( temp ));
195 buf += sizeof( temp );
197 temp = htonl(ADEDOFF_RFORK_OSX);
198 memcpy(buf, &temp, sizeof( temp ));
199 buf += sizeof( temp );
201 temp = htonl( ad->ad_rlen);
202 memcpy(buf, &temp, sizeof( temp ));
203 buf += sizeof( temp );
205 return AD_DATASZ_OSX;
208 /* -------------------
209 * XXX copy only header with same size or comment
210 * doesn't work well for adouble with different version.
213 int ad_copy_header(struct adouble *add, struct adouble *ads)
218 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
219 if ( ads->ad_eid[ eid ].ade_off == 0 || add->ad_eid[ eid ].ade_off == 0 )
222 len = ads->ad_eid[ eid ].ade_len;
223 if (!len || len != add->ad_eid[ eid ].ade_len)
234 ad_setentrylen( add, eid, len );
235 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
238 add->ad_rlen = ads->ad_rlen;
240 if ((ads->ad_vers == AD_VERSION2) && (add->ad_vers = AD_VERSION_EA)
241 || (ads->ad_vers == AD_VERSION_EA) && (add->ad_vers = AD_VERSION2)) {
243 memcpy(&id, ad_entry(add, ADEID_PRIVID), sizeof(cnid_t));
245 memcpy(ad_entry(add, ADEID_PRIVID), &id, sizeof(cnid_t));
250 static int ad_flush_hf(struct adouble *ad)
256 LOG(log_debug, logtype_default, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
260 switch (ad->ad_vers) {
265 adf = &ad->ad_data_fork;
268 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
272 if ((adf->adf_flags & O_RDWR)
273 || ((ad->ad_adflags & ADFLAGS_DIR) && (ad->ad_adflags & ADFLAGS_RDWR))) {
274 if (ad_getentryoff(ad, ADEID_RFORK)) {
275 if (ad->ad_rlen > 0xffffffff)
276 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
278 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
280 len = ad->ad_ops->ad_rebuild_header(ad);
282 switch (ad->ad_vers) {
284 if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
291 if (AD_META_OPEN(ad)) {
292 if (ad->ad_adflags & ADFLAGS_DIR) {
293 EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
294 EC_NEG1_LOG( fchdir(ad_data_fileno(ad)) );
295 EC_ZERO_LOG( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
296 EC_NEG1_LOG( fchdir(cwd) );
297 EC_NEG1_LOG( close(cwd) );
300 EC_ZERO_LOG( sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
305 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
312 if (fchdir(cwd) != 0) {
313 AFP_PANIC("ad_flush: cant fchdir");
320 /* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
321 static int ad_flush_rf(struct adouble *ad)
324 char adbuf[AD_DATASZ_OSX];
329 if (ad->ad_vers != AD_VERSION_EA)
332 LOG(log_debug, logtype_default, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
334 if ((ad->ad_rfp->adf_flags & O_RDWR)) {
335 if (ad_getentryoff(ad, ADEID_RFORK)) {
336 if (ad->ad_rlen > 0xffffffff)
337 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
339 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
341 len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
343 if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
352 int ad_flush(struct adouble *ad)
356 LOG(log_debug, logtype_default, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
358 if (AD_META_OPEN(ad)) {
359 EC_ZERO( ad_flush_hf(ad) );
362 if (AD_RSRC_OPEN(ad)) {
363 EC_ZERO( ad_flush_rf(ad) );
370 static int ad_data_closefd(struct adouble *ad)
374 if (ad_data_fileno(ad) == AD_SYMLINK) {
375 free(ad->ad_data_fork.adf_syml);
376 ad->ad_data_fork.adf_syml = NULL;
378 if (close(ad_data_fileno(ad)) < 0)
381 ad_data_fileno(ad) = -1;
386 * Close a struct adouble freeing all resources
388 int ad_close(struct adouble *ad, int adflags)
395 LOG(log_debug, logtype_default,
396 "ad_close(%s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
397 adflags2logstr(adflags),
398 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
399 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
400 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
402 if (adflags & (ADFLAGS_SETSHRMD | ADFLAGS_CHECK_OF)) {
403 /* sharemode locks are stored in the data fork, adouble:v2 needs this extra handling */
404 adflags |= ADFLAGS_DF;
407 if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF))
408 adflags |= ADFLAGS_HF;
410 if ((adflags & ADFLAGS_DF)
411 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == AD_SYMLINK)
412 && --ad->ad_data_fork.adf_refcount == 0) {
413 if (ad_data_closefd(ad) < 0)
415 adf_lock_free(&ad->ad_data_fork);
418 if ((adflags & ADFLAGS_HF)
419 && (ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
420 if (close( ad_meta_fileno(ad)) < 0)
422 ad_meta_fileno(ad) = -1;
423 if (ad->ad_vers == AD_VERSION2)
424 adf_lock_free(ad->ad_mdp);
427 if ((adflags & ADFLAGS_RF) && (ad->ad_vers == AD_VERSION_EA)) {
428 if ((ad_reso_fileno(ad) != -1)
429 && !(--ad->ad_rfp->adf_refcount)) {
430 if (close(ad->ad_rfp->adf_fd) < 0)
433 ad_reso_fileno(ad) = -1;
434 adf_lock_free(ad->ad_rfp);
438 LOG(log_debug, logtype_default,
439 "ad_close(%s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
440 adflags2logstr(adflags), err,
441 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
442 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
443 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);