2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* An IHandle_t is an abstraction allowing the file and volume operations to
11 * pass the elements required to identify a file to the underlying file
12 * systen. For the usual Vice inode operations, this is no more than the
13 * usual device and inode numbers. For the user space file system used on NT
14 * we also need the volume id to identify the file.
16 * An FdHandle_t is an abstraction used to associate file descroptors
17 * with Inode handles. IH_OPEN is used to get a file descriptor that
18 * can be used in subsequent I/O operations. File descriptor handles are
19 * cached by IO_CLOSE. To make sure a file descriptor is really closed call
22 * The IHandle_t also provides a place to do other optimizations. In the
23 * NT user space file system, we keep a separate special file for the
24 * link counts and using the IHandle_t allows keeping the details of
25 * that at a lower level than the IDEC and IINC calls.
27 * To use the IHandle_t there are a new set of IH_xxxx/FDH_XXXX operations.
28 * Each takes a pointer to an IHandle_t or an FdHandle_t as the first
29 * argument. This pointer is considered an in/out variable. In particular,
30 * the open file descriptors for a given Inode are stored in a linked list
31 * of FdHandle_t hanging off of each IHandle_t. IH_OPEN returns NULL on error,
32 * and a valid FdHandle_t otherwise. All other IH_xxxx/FDH_xxxx macros return
33 * -1 on error and 0 on success.
35 * Inode handle operations:
36 * IH_INIT - Initialize the Inode handle with the device, volume id, and ino
37 * IH_COPY - Copy Inode handle info to a new handle with no open descriptors.
38 * IH_REALLYCLOSE - Close all cached file descriptors for Inode handle
39 * IH_RELEASE - release a Inode handle, close all cached file descriptors
40 * IH_CONDSYNC - snyc the Inode if it has any open file descriptors
42 * Inode operation replacements:
43 * IH_CREATE - create a file in the underlying filesystem and setup the
44 * information needed to reference this file in the IHandle_t.
45 * IH_OPEN - open the file belonging to the associated inode and set the
47 * IH_IREAD/IH_IWRITE - read/write an Inode.
48 * IH_INC/IH_DEC - increment/decrement the link count.
50 * Replacements for C runtime file operations
51 * FDH_READ/FDH_WRITE - read/write using the file descriptor.
52 * FDH_READV/FDH_WRITEV - readv/writev (Unix only)
53 * FDH_SEEK - set file handle's read/write position
54 * FDH_CLOSE - return a file descriptor to the cache
55 * FDH_REALLYCLOSE - Close a file descriptor, do not return to the cache
56 * FDH_SYNC - Unconditionally sync an open file.
57 * FDH_TRUNC - Truncate a file
60 * FDH_SIZE - returns the size of the file.
61 * FDH_NLINK - returns the link count of the file.
64 * FDH_FDOPEN - create a descriptor for buffered I/O
65 * STREAM_READ/STREAM_WRITE - buffered file I/O
71 #ifdef AFS_PTHREAD_ENV
74 extern pthread_once_t ih_glock_once;
75 extern pthread_mutex_t ih_glock_mutex;
76 extern void ih_glock_init(void);
78 assert(pthread_once(&ih_glock_once, ih_glock_init) == 0 && \
79 pthread_mutex_lock(&ih_glock_mutex) == 0)
81 assert(pthread_mutex_unlock(&ih_glock_mutex) == 0)
82 #else /* AFS_PTHREAD_ENV */
85 #endif /* AFS_PTHREAD_ENV */
89 * Macro to initialize a doubly linked list, lifted from Encina
91 #define DLL_INIT_LIST(head, tail) \
98 * Macro to remove an element from a doubly linked list
100 #define DLL_DELETE(ptr,head,tail,next,prev) \
103 (ptr)->next->prev = (ptr)->prev; \
105 (tail) = (ptr)->prev; \
107 (ptr)->prev->next = (ptr)->next; \
109 (head) = (ptr)->next; \
110 (ptr)->next = (ptr)->prev = NULL; \
111 assert(!(head) || !((head)->prev)); \
115 * Macro to insert an element at the tail of a doubly linked list
117 #define DLL_INSERT_TAIL(ptr,head,tail,next,prev) \
119 (ptr)->next = NULL; \
120 (ptr)->prev = (tail); \
123 (ptr)->prev->next = (ptr); \
126 assert((head) && ((head)->prev == NULL)); \
129 #endif /* DLL_INIT_LIST */
132 typedef __int64 Inode;
134 #include <afs/afssyscalls.h>
137 /* The dir package's page hashing function is dependent upon the layout of
138 * IHandle_t as well as the containing DirHandle in viced/viced.h. Make
139 * Sure the volume id is still used as the hash after any changes to either
143 /* forward declaration */
146 /* File descriptors are HANDLE's on NT. The following typedef helps catch
147 * type errors. duplicated in libadmin/vos/afs_vosAdmin.c
154 #define INVALID_FD ((FD_t)-1)
156 /* file descriptor handle */
157 typedef struct FdHandle_s {
158 int fd_status; /* status flags */
159 FD_t fd_fd; /* file descriptor */
160 struct IHandle_s *fd_ih; /* Pointer to Inode handle */
161 struct FdHandle_s *fd_next; /* LRU/Avail list pointers */
162 struct FdHandle_s *fd_prev;
163 struct FdHandle_s *fd_ihnext; /* Inode handle's list of file descriptors */
164 struct FdHandle_s *fd_ihprev;
167 /* File descriptor status values */
168 #define FD_HANDLE_AVAIL 1 /* handle is not open and available */
169 #define FD_HANDLE_OPEN 2 /* handle is open and not in use */
170 #define FD_HANDLE_INUSE 3 /* handle is open and in use */
172 /* buffered file descriptor handle */
173 #define STREAM_HANDLE_BUFSIZE 2048 /* buffer size for STR_READ/STR_WRITE */
174 typedef struct StreamHandle_s {
175 FD_t str_fd; /* file descriptor */
176 int str_direction; /* current read/write direction */
177 afs_sfsize_t str_buflen; /* bytes remaining in buffer */
178 afs_foff_t str_bufoff; /* current offset into buffer */
179 int str_error; /* error code */
180 int str_eof; /* end of file flag */
181 struct StreamHandle_s *str_next; /* Avail list pointers */
182 struct StreamHandle_s *str_prev;
183 char str_buffer[STREAM_HANDLE_BUFSIZE]; /* data buffer */
186 #define STREAM_DIRECTION_NONE 1 /* stream is in initial mode */
187 #define STREAM_DIRECTION_READ 2 /* stream is in input mode */
188 #define STREAM_DIRECTION_WRITE 3 /* stream is in output mode */
190 /* number handles allocated at a shot */
191 #define I_HANDLE_MALLOCSIZE ((size_t)((4096/sizeof(IHandle_t))))
192 #define FD_HANDLE_MALLOCSIZE ((size_t)((4096/sizeof(FdHandle_t))))
193 #define STREAM_HANDLE_MALLOCSIZE 1
198 * On modern platforms tuned for I/O intensive workloads, there may be
199 * thousands of file descriptors available (64K on 32-bit Solaris 7,
200 * for example), and threading in Solaris 9 and Linux 2.6 (NPTL) are
201 * tuned for (many) thousands of concurrent threads at peak.
203 * On these platforms, it makes sense to allow administrators to set
204 * appropriate limits for their hardware. Clients may now set desired
205 * values in the exported vol_io_params, of type ih_init_params.
208 typedef struct ih_init_params
210 afs_uint32 fd_handle_setaside; /* for non-cached i/o, trad. was 128 */
211 afs_uint32 fd_initial_cachesize; /* what was 'default' */
212 afs_uint32 fd_max_cachesize; /* max open files if large-cache activated */
216 /* Number of file descriptors needed for non-cached I/O */
217 #define FD_HANDLE_SETASIDE 128 /* Match to MAX_FILESERVER_THREAD */
219 /* Which systems have 8-bit fileno? On GNU/Linux systems, the
220 * fileno member of FILE is an int. On NetBSD 5, it's a short.
221 * Ditto for OpenBSD 4.5. Through Solaris 10 8/07 it's unsigned char.
224 /* Don't try to have more than 256 files open at once if you are planning
225 * to use fopen or fdopen. The FILE structure has an eight bit field for
226 * the file descriptor. */
227 #define FD_DEFAULT_CACHESIZE (255-FD_HANDLE_SETASIDE)
229 /* We need some limit on the number of files open at once. Some systems
230 * say we can open lots of files, but when we do they run out of slots
233 #define FD_MAX_CACHESIZE (2000 - FD_HANDLE_SETASIDE)
235 /* On modern platforms, this is sized higher than the note implies.
236 * For HP, see http://forums11.itrc.hp.com/service/forums/questionanswer.do?admit=109447626+1242508538748+28353475&threadId=302950
237 * On AIX, it's said to be self-tuning (sar -v)
238 * On Solaris, http://www.princeton.edu/~unix/Solaris/troubleshoot/kerntune.html
239 * says stdio limit (FILE) may exist, but then backtracks and says the 64bit
240 * solaris and POLL (rather than select) io avoid the issue. Solaris Internals
241 * states that Solaris 7 and above deal with up to 64K on 32bit.
242 * However, extended FILE must be enabled to use this. See
243 * enable_extended_FILE_stdio(3C)
247 typedef struct IHandle_s {
248 afs_uint32 ih_vid; /* Parent volume id. */
249 int ih_dev; /* device id. */
250 int ih_flags; /* Flags */
251 int ih_synced; /* should be synced next time */
252 Inode ih_ino; /* Inode number */
253 int ih_refcnt; /* reference count */
254 struct FdHandle_s *ih_fdhead; /* List of open file desciptors */
255 struct FdHandle_s *ih_fdtail;
256 struct IHandle_s *ih_next; /* Links for avail list/hash chains */
257 struct IHandle_s *ih_prev;
260 /* Flags for the Inode handle */
261 #define IH_REALLY_CLOSED 1
263 /* Hash function for inode handles */
264 #define I_HANDLE_HASH_SIZE 2048 /* power of 2 */
266 /* The casts to int's ensure NT gets the xor operation correct. */
267 #define IH_HASH(D, V, I) ((int)(((D)^(V)^((int)(I)))&(I_HANDLE_HASH_SIZE-1)))
270 * Hash buckets for inode handles
272 typedef struct IHashBucket_s {
273 IHandle_t *ihash_head;
274 IHandle_t *ihash_tail;
277 /* Prototypes for handle support routines. */
282 #include "namei_ops.h"
284 extern void ih_clear(IHandle_t * h);
285 extern Inode ih_create(IHandle_t * h, int dev, char *part, Inode nI, int p1,
286 int p2, int p3, int p4);
287 extern FILE *ih_fdopen(FdHandle_t * h, char *fdperms);
288 #endif /* AFS_NAMEI_ENV */
291 * Prototypes for file descriptor cache routines
293 extern void ih_PkgDefaults(void);
294 extern void ih_Initialize(void);
295 extern void ih_UseLargeCache(void);
296 extern IHandle_t *ih_init(int /*@alt Device@ */ dev, int /*@alt VolId@ */ vid,
298 extern IHandle_t *ih_copy(IHandle_t * ihP);
299 extern FdHandle_t *ih_open(IHandle_t * ihP);
300 extern int fd_close(FdHandle_t * fdP);
301 extern int fd_reallyclose(FdHandle_t * fdP);
302 extern StreamHandle_t *stream_fdopen(FD_t fd);
303 extern StreamHandle_t *stream_open(const char *file, const char *mode);
304 extern afs_sfsize_t stream_read(void *ptr, afs_fsize_t size,
305 afs_fsize_t nitems, StreamHandle_t * streamP);
306 extern afs_sfsize_t stream_write(void *ptr, afs_fsize_t size,
308 StreamHandle_t * streamP);
309 extern int stream_seek(StreamHandle_t * streamP, afs_foff_t offset,
311 extern int stream_flush(StreamHandle_t * streamP);
312 extern int stream_close(StreamHandle_t * streamP, int reallyClose);
313 extern int ih_reallyclose(IHandle_t * ihP);
314 extern int ih_release(IHandle_t * ihP);
315 extern int ih_condsync(IHandle_t * ihP);
317 /* Macros common to user space and inode API's. */
318 #define IH_INIT(H, D, V, I) ((H) = ih_init((D), (V), (I)))
320 #define IH_COPY(D, S) ((D) = ih_copy(S))
322 #define IH_NLINK(H) ih_nlink(H)
324 #define IH_OPEN(H) ih_open(H)
326 #define FDH_CLOSE(H) (fd_close(H), (H)=NULL, 0)
328 #define FDH_REALLYCLOSE(H) (fd_reallyclose(H), (H)=NULL, 0)
330 #define FDH_FDOPEN(H, A) stream_fdopen((H)->fd_fd)
332 #define STREAM_FDOPEN(A, B) stream_fdopen(A)
334 #define STREAM_OPEN(A, B) stream_open(A, B)
336 #define STREAM_READ(A, B, C, H) stream_read(A, B, C, H)
338 #define STREAM_WRITE(A, B, C, H) stream_write(A, B, C, H)
340 #define STREAM_SEEK(H, A, B) stream_seek(H, A, B)
342 #define STREAM_FLUSH(H) stream_flush(H)
344 #define STREAM_ERROR(H) ((H)->str_error)
346 #define STREAM_EOF(H) ((H)->str_eof)
348 #define STREAM_CLOSE(H) stream_close(H, 0)
350 #define STREAM_REALLYCLOSE(H) stream_close(H, 1)
352 #define IH_RELEASE(H) (ih_release(H), (H)=NULL, 0)
354 #define IH_REALLYCLOSE(H) ih_reallyclose(H)
356 #define IH_CONDSYNC(H) ih_condsync(H)
361 #define IH_CREATE(H, D, P, N, P1, P2, P3, P4) \
362 nt_icreate(H, P, P1, P2, P3, P4)
364 #define OS_IOPEN(H) nt_iopen(H)
365 #define OS_OPEN(F, M, P) nt_open(F, M, P)
366 #define OS_CLOSE(FD) nt_close(FD)
368 #define OS_READ(FD, B, S) nt_read(FD, B, S)
369 #define OS_WRITE(FD, B, S) nt_write(FD, B, S)
370 #define OS_SEEK(FD, O, F) nt_seek(FD, O, F)
372 #define OS_SYNC(FD) nt_fsync(FD)
373 #define OS_TRUNC(FD, L) nt_ftruncate(FD, L)
374 #define OS_SIZE(FD) nt_size(FD)
376 #define IH_INC(H, I, P) nt_inc(H, I, P)
377 #define IH_DEC(H, I, P) nt_dec(H, I, P)
378 #define IH_IREAD(H, O, B, S) nt_iread(H, O, B, S)
379 #define IH_IWRITE(H, O, B, S) nt_iwrite(H, O, B, S)
381 #else /* AFS_NT40_ENV */
383 /*@+fcnmacros +macrofcndecl@*/
385 extern Inode IH_CREATE(IHandle_t * H, int /*@alt Device @ */ D,
386 char *P, Inode N, int /*@alt VolumeId @ */ P1,
387 int /*@alt VnodeId @ */ P2,
388 int /*@alt Unique @ */ P3,
389 int /*@alt unsigned @ */ P4);
390 extern FD_t OS_IOPEN(IHandle_t * H);
391 extern int OS_OPEN(const char *F, int M, mode_t P);
392 extern int OS_CLOSE(int FD);
393 extern ssize_t OS_READ(int FD, void *B, size_t S);
394 extern ssize_t OS_WRITE(int FD, void *B, size_t S);
395 extern int OS_SYNC(int FD);
396 extern afs_sfsize_t OS_SIZE(int FD);
397 extern int IH_INC(IHandle_t * H, Inode I, int /*@alt VolId, VolumeId @ */ P);
398 extern int IH_DEC(IHandle_t * H, Inode I, int /*@alt VolId, VolumeId @ */ P);
399 extern afs_sfsize_t IH_IREAD(IHandle_t * H, afs_foff_t O, void *B,
401 extern afs_sfsize_t IH_IWRITE(IHandle_t * H, afs_foff_t O, void *B,
404 extern off64_t OS_SEEK(int FD, off64_t O, int F);
405 extern int OS_TRUNC(int FD, off64_t L);
406 #else /* !O_LARGEFILE */
407 extern off_t OS_SEEK(int FD, off_t O, int F);
408 extern int OS_TRUNC(int FD, off_t L);
409 #endif /* !O_LARGEFILE */
410 #endif /*S_SPLINT_S */
412 #define IH_CREATE(H, D, P, N, P1, P2, P3, P4) \
413 namei_icreate(H, P, P1, P2, P3, P4)
415 #define OS_IOPEN(H) namei_iopen(H)
417 #define OS_OPEN(F, M, P) open64(F, M, P)
418 #else /* !O_LARGEFILE */
419 #define OS_OPEN(F, M, P) open(F, M, P)
420 #endif /* !O_LARGEFILE */
421 #define OS_CLOSE(FD) close(FD)
423 #define OS_READ(FD, B, S) read(FD, B, S)
424 #define OS_WRITE(FD, B, S) write(FD, B, S)
426 #define OS_SEEK(FD, O, F) lseek64(FD, (off64_t) (O), F)
427 #else /* !O_LARGEFILE */
428 #define OS_SEEK(FD, O, F) lseek(FD, (off_t) (O), F)
429 #endif /* !O_LARGEFILE */
431 #define OS_SYNC(FD) fsync(FD)
433 #define OS_TRUNC(FD, L) ftruncate64(FD, (off64_t) (L))
434 #else /* !O_LARGEFILE */
435 #define OS_TRUNC(FD, L) ftruncate(FD, (off_t) (L))
436 #endif /* !O_LARGEFILE */
437 #define OS_SIZE(FD) ih_size(FD)
438 extern afs_sfsize_t ih_size(int fd);
440 #define IH_INC(H, I, P) namei_inc(H, I, P)
441 #define IH_DEC(H, I, P) namei_dec(H, I, P)
442 #define IH_IREAD(H, O, B, S) namei_iread(H, O, B, S)
443 #define IH_IWRITE(H, O, B, S) namei_iwrite(H, O, B, S)
444 /*@=fcnmacros =macrofcndecl@*/
445 #endif /* AFS_NT40_ENV */
447 #else /* AFS_NAMEI_ENV */
448 extern Inode ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1,
449 int p2, int p3, int p4);
451 #define IH_CREATE(H, D, P, N, P1, P2, P3, P4) \
452 ih_icreate(H, D, P, N, P1, P2, P3, P4)
454 #ifdef AFS_LINUX22_ENV
455 #define OS_IOPEN(H) -1
458 #define OS_IOPEN(H) (IOPEN((H)->ih_dev, (H)->ih_ino, O_RDWR|O_LARGEFILE))
460 #define OS_IOPEN(H) (IOPEN((H)->ih_dev, (H)->ih_ino, O_RDWR))
463 #define OS_OPEN(F, M, P) open(F, M, P)
464 #define OS_CLOSE(FD) close(FD)
466 #define OS_READ(FD, B, S) read(FD, B, S)
467 #define OS_WRITE(FD, B, S) write(FD, B, S)
469 #define OS_SEEK(FD, O, F) lseek64(FD, (off64_t) (O), F)
470 #else /* !O_LARGEFILE */
471 #define OS_SEEK(FD, O, F) lseek(FD, (off_t) (O), F)
472 #endif /* !O_LARGEFILE */
474 #define OS_SYNC(FD) fsync(FD)
476 #define OS_TRUNC(FD, L) ftruncate64(FD, (off64_t) (L))
477 #else /* !O_LARGEFILE */
478 #define OS_TRUNC(FD, L) ftruncate(FD, (off_t) (L))
479 #endif /* !O_LARGEFILE */
480 #define OS_SIZE(FD) ih_size(FD)
481 extern afs_sfsize_t ih_size(int fd);
483 #ifdef AFS_LINUX22_ENV
484 #define IH_INC(H, I, P) -1
485 #define IH_DEC(H, I, P) -1
486 #define IH_IREAD(H, O, B, S) -1
487 #define IH_IWRITE(H, O, B, S) -1
489 #define IH_INC(H, I, P) IINC((H)->ih_dev, I, P)
490 #define IH_DEC(H, I, P) IDEC((H)->ih_dev, I, P)
491 #define IH_IREAD(H, O, B, S) inode_read((H)->ih_dev, (H)->ih_ino, (H)->ih_vid,\
493 #define IH_IWRITE(H, O, B, S) \
494 inode_write((H)->ih_dev, (H)->ih_ino, (H)->ih_vid, O, B, S)
495 #endif /* AFS_LINUX22_ENV */
498 #endif /* AFS_NAMEI_ENV */
501 #define FDH_READV(H, I, N) readv((H)->fd_fd, I, N)
502 #define FDH_WRITEV(H, I, N) writev((H)->fd_fd, I, N)
505 #define FDH_READ(H, B, S) OS_READ((H)->fd_fd, B, S)
506 #define FDH_WRITE(H, B, S) OS_WRITE((H)->fd_fd, B, S)
507 #define FDH_SEEK(H, O, F) OS_SEEK((H)->fd_fd, O, F)
509 #define FDH_SYNC(H) ((H->fd_ih!=NULL) ? ( H->fd_ih->ih_synced = 1) - 1 : 1)
510 #define FDH_TRUNC(H, L) OS_TRUNC((H)->fd_fd, L)
511 #define FDH_SIZE(H) OS_SIZE((H)->fd_fd)
513 #endif /* _IHANDLE_H_ */