source: trunk/libtransmission/fdlimit.c @ 8935

Last change on this file since 8935 was 8935, checked in by charles, 14 years ago

(trunk libT) remove dead code noticed by geirha

  • Property svn:keywords set to Date Rev Author Id
File size: 16.5 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 8935 2009-08-15 15:52:10Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef WIN32
26 #define HAVE_GETRLIMIT
27#endif
28
29#ifdef HAVE_POSIX_FADVISE
30 #ifdef _XOPEN_SOURCE
31  #undef _XOPEN_SOURCE
32 #endif
33 #define _XOPEN_SOURCE 600
34#endif
35
36#include <assert.h>
37#include <errno.h>
38#include <inttypes.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#ifdef SYS_DARWIN
43 #include <fcntl.h>
44#endif
45
46#ifdef HAVE_XFS_XFS_H
47 #include <xfs/xfs.h>
48#endif
49
50#include <sys/types.h>
51#include <sys/stat.h>
52#ifdef HAVE_GETRLIMIT
53 #include <sys/time.h> /* getrlimit */
54 #include <sys/resource.h> /* getrlimit */
55#endif
56#include <unistd.h>
57#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
58
59#include <evutil.h>
60
61#include "transmission.h"
62#include "fdlimit.h"
63#include "list.h"
64#include "net.h"
65#include "platform.h" /* MAX_PATH_LENGTH, TR_PATH_DELIMITER */
66#include "utils.h"
67
68#define dbgmsg( ... ) \
69    do { \
70        if( tr_deepLoggingIsActive( ) ) \
71            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
72    } while( 0 )
73
74/**
75***
76**/
77
78enum
79{
80    NOFILE_BUFFER = 512, /* the process' number of open files is
81                            globalMaxPeers + NOFILE_BUFFER */
82};
83
84struct tr_openfile
85{
86    tr_bool    isWritable;
87    int        torrentId;
88    char       filename[MAX_PATH_LENGTH];
89    int        fd;
90    uint64_t   date;
91};
92
93struct tr_fd_s
94{
95    int                   socketCount;
96    int                   socketLimit;
97    int                   openFileLimit;
98    struct tr_openfile  * openFiles;
99};
100
101static struct tr_fd_s * gFd = NULL;
102
103/***
104****
105****  Local Files
106****
107***/
108
109#ifndef O_LARGEFILE
110 #define O_LARGEFILE 0
111#endif
112
113static tr_bool
114preallocateFileSparse( int fd, uint64_t length )
115{
116    const char zero = '\0';
117
118    if( length == 0 )
119        return TRUE;
120
121    if( lseek( fd, length-1, SEEK_SET ) == -1 )
122        return FALSE;
123    if( write( fd, &zero, 1 ) == -1 )
124        return FALSE;
125    if( ftruncate( fd, length ) == -1 )
126        return FALSE;
127
128    return TRUE;
129}
130
131static tr_bool
132preallocateFileFull( const char * filename, uint64_t length )
133{
134    tr_bool success = 0;
135
136#ifdef WIN32
137
138    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
139    if( hFile != INVALID_HANDLE_VALUE )
140    {
141        LARGE_INTEGER li;
142        li.QuadPart = length;
143        success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
144        CloseHandle( hFile );
145    }
146
147#else
148
149    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
150    int fd = open( filename, flags, 0666 );
151    if( fd >= 0 )
152    {
153# ifdef HAVE_XFS_XFS_H
154        if( !success && platform_test_xfs_fd( fd ) )
155        {
156            xfs_flock64_t fl;
157            fl.l_whence = 0;
158            fl.l_start = 0;
159            fl.l_len = length;
160            success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl );
161        }
162# endif
163# ifdef SYS_DARWIN
164        if( !success )
165        {
166            fstore_t fst;
167            fst.fst_flags = F_ALLOCATECONTIG;
168            fst.fst_posmode = F_PEOFPOSMODE;
169            fst.fst_offset = 0;
170            fst.fst_length = length;
171            fst.fst_bytesalloc = 0;
172            success = !fcntl( fd, F_PREALLOCATE, &fst );
173        }
174# endif
175# ifdef HAVE_POSIX_FALLOCATE
176        if( !success )
177        {
178            success = !posix_fallocate( fd, 0, length );
179        }
180# endif
181
182        if( !success ) /* if nothing else works, do it the old-fashioned way */
183        {
184            uint8_t buf[ 4096 ];
185            memset( buf, 0, sizeof( buf ) );
186            success = TRUE;
187            while ( success && ( length > 0 ) )
188            {
189                const int thisPass = MIN( length, sizeof( buf ) );
190                success = write( fd, buf, thisPass ) == thisPass;
191                length -= thisPass;
192            }
193        }
194
195        close( fd );
196    }
197
198#endif
199
200    return success;
201}
202
203tr_bool
204tr_preallocate_file( const char * filename, uint64_t length )
205{
206    return preallocateFileFull( filename, length );
207}
208
209int
210tr_open_file_for_writing( const char * filename )
211{
212    int flags = O_WRONLY | O_CREAT;
213#ifdef O_BINARY
214    flags |= O_BINARY;
215#endif
216#ifdef O_LARGEFILE
217    flags |= O_LARGEFILE;
218#endif
219    return open( filename, flags, 0666 );
220}
221
222int
223tr_open_file_for_scanning( const char * filename )
224{
225    int fd;
226    int flags;
227
228    /* build the flags */
229    flags = O_RDONLY;
230#ifdef O_SEQUENTIAL
231    flags |= O_SEQUENTIAL;
232#endif
233#ifdef O_BINARY
234    flags |= O_BINARY;
235#endif
236#ifdef O_LARGEFILE
237    flags |= O_LARGEFILE;
238#endif
239
240    /* open the file */
241    fd = open( filename, flags, 0666 );
242    if( fd >= 0 )
243    {
244        /* Set hints about the lookahead buffer and caching. It's okay
245           for these to fail silently, so don't let them affect errno */
246        const int err = errno;
247#ifdef HAVE_POSIX_FADVISE
248        posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
249#endif
250#ifdef SYS_DARWIN
251        fcntl( fd, F_NOCACHE, 1 );
252        fcntl( fd, F_RDAHEAD, 1 );
253#endif
254        errno = err;
255    }
256
257    return fd;
258}
259
260void
261tr_close_file( int fd )
262{
263#if defined(HAVE_POSIX_FADVISE)
264    /* Set hint about not caching this file.
265       It's okay for this to fail silently, so don't let it affect errno */
266    const int err = errno;
267    posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
268    errno = err;
269#endif
270    close( fd );
271}
272
273/**
274 * returns 0 on success, or an errno value on failure.
275 * errno values include ENOENT if the parent folder doesn't exist,
276 * plus the errno values set by tr_mkdirp() and open().
277 */
278static int
279TrOpenFile( int                      i,
280            const char             * folder,
281            const char             * torrentFile,
282            tr_bool                  doWrite,
283            tr_preallocation_mode    preallocationMode,
284            uint64_t                 desiredFileSize )
285{
286    struct tr_openfile * file = &gFd->openFiles[i];
287    int                  flags;
288    char               * filename;
289    struct stat          sb;
290    tr_bool              alreadyExisted;
291
292    /* confirm the parent folder exists */
293    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
294    {
295        tr_err( _( "Couldn't create \"%1$s\": \"%2$s\" is not a folder" ), torrentFile, folder );
296        return ENOENT;
297    }
298
299    /* create subfolders, if any */
300    filename = tr_buildPath( folder, torrentFile, NULL );
301    if( doWrite )
302    {
303        char * tmp = tr_dirname( filename );
304        const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
305        if( err ) {
306            tr_err( _( "Couldn't create \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
307            tr_free( tmp );
308            tr_free( filename );
309            return err;
310        }
311        tr_free( tmp );
312    }
313
314    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
315
316    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
317        if( preallocateFileFull( filename, desiredFileSize ) )
318            tr_inf( _( "Preallocated file \"%s\"" ), filename );
319
320    /* open the file */
321    flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
322#ifdef O_SEQUENTIAL
323    flags |= O_SEQUENTIAL;
324#endif
325#ifdef O_LARGEFILE
326    flags |= O_LARGEFILE;
327#endif
328#ifdef WIN32
329    flags |= O_BINARY;
330#endif
331    file->fd = open( filename, flags, 0666 );
332    if( file->fd == -1 )
333    {
334        const int err = errno;
335        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
336        tr_free( filename );
337        return err;
338    }
339
340    /* If the file already exists and it's too large, truncate it.
341     * This is a fringe case that happens if a torrent's been updated
342     * and one of the updated torrent's files is smaller.
343     * http://trac.transmissionbt.com/ticket/2228
344     * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
345     */
346    if( alreadyExisted && ( desiredFileSize < (uint64_t)sb.st_size ) )
347        ftruncate( file->fd, desiredFileSize );
348
349    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
350        preallocateFileSparse( file->fd, desiredFileSize );
351
352#ifdef HAVE_POSIX_FADVISE
353    /* this doubles the OS level readahead buffer, which in practice
354     * turns out to be a good thing, because many (most?) clients request
355     * chunks of blocks in order */
356    posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
357#endif
358
359    tr_free( filename );
360    return 0;
361}
362
363static TR_INLINE tr_bool
364fileIsOpen( const struct tr_openfile * o )
365{
366    return o->fd >= 0;
367}
368
369static void
370TrCloseFile( struct tr_openfile * o )
371{
372    assert( o != NULL );
373    assert( fileIsOpen( o ) );
374
375    tr_close_file( o->fd );
376    o->fd = -1;
377}
378
379/* returns an fd on success, or a -1 on failure and sets errno */
380int
381tr_fdFileCheckout( int                      torrentId,
382                   const char             * folder,
383                   const char             * torrentFile,
384                   tr_bool                  doWrite,
385                   tr_preallocation_mode    preallocationMode,
386                   uint64_t                 desiredFileSize )
387{
388    int i, winner = -1;
389    struct tr_openfile * o;
390    char filename[MAX_PATH_LENGTH];
391
392    assert( torrentId > 0 );
393    assert( folder && *folder );
394    assert( torrentFile && *torrentFile );
395    assert( tr_isBool( doWrite ) );
396
397    tr_snprintf( filename, sizeof( filename ), "%s%c%s", folder, TR_PATH_DELIMITER, torrentFile );
398    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
399
400    /* is it already open? */
401    for( i=0; i<gFd->openFileLimit; ++i )
402    {
403        o = &gFd->openFiles[i];
404
405        if( !fileIsOpen( o ) )
406            continue;
407        if( torrentId != o->torrentId )
408            continue;
409        if( strcmp( filename, o->filename ) )
410            continue;
411
412        if( doWrite && !o->isWritable )
413        {
414            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
415            TrCloseFile( o );
416            break;
417        }
418
419        dbgmsg( "found it!  it's ready for use!" );
420        winner = i;
421        break;
422    }
423
424    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
425    while( winner < 0 )
426    {
427        uint64_t date = tr_date( ) + 1;
428
429        /* look for the file that's been open longest */
430        for( i=0; i<gFd->openFileLimit; ++i )
431        {
432            o = &gFd->openFiles[i];
433
434            if( !fileIsOpen( o ) )
435            {
436                winner = i;
437                dbgmsg( "found an empty slot in %d", winner );
438                break;
439            }
440
441            if( date > o->date )
442            {
443                date = o->date;
444                winner = i;
445            }
446        }
447
448        assert( winner >= 0 );
449
450        if( fileIsOpen( &gFd->openFiles[winner] ) )
451        {
452            dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
453            TrCloseFile( &gFd->openFiles[winner] );
454        }
455    }
456
457    assert( winner >= 0 );
458    o = &gFd->openFiles[winner];
459    if( !fileIsOpen( o ) )
460    {
461        const int err = TrOpenFile( winner, folder, torrentFile, doWrite,
462                                    preallocationMode, desiredFileSize );
463        if( err ) {
464            errno = err;
465            return -1;
466        }
467
468        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
469                doWrite ? 'y' : 'n' );
470        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
471        o->isWritable = doWrite;
472    }
473
474    dbgmsg( "checking out '%s' in slot %d", filename, winner );
475    o->torrentId = torrentId;
476    o->date = tr_date( );
477    return o->fd;
478}
479
480void
481tr_fdFileClose( const char * filename )
482{
483    struct tr_openfile * o;
484    const struct tr_openfile * end;
485
486    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
487    {
488        if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
489            continue;
490        dbgmsg( "tr_fdFileClose closing \"%s\"", filename );
491        TrCloseFile( o );
492    }
493}
494
495void
496tr_fdTorrentClose( int torrentId )
497{
498    struct tr_openfile * o;
499    const struct tr_openfile * end;
500
501    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
502        if( fileIsOpen( o ) && o->torrentId == torrentId )
503            TrCloseFile( o );
504}
505
506/***
507****
508****  Sockets
509****
510***/
511
512static TR_INLINE int
513getSocketMax( struct tr_fd_s * gFd )
514{
515    return gFd->socketLimit;
516}
517
518int
519tr_fdSocketCreate( int domain, int type )
520{
521    int s = -1;
522
523    if( gFd->socketCount < getSocketMax( gFd ) )
524        if( ( s = socket( domain, type, 0 ) ) < 0 )
525        {
526#ifdef SYS_DARWIN
527            if( sockerrno != EAFNOSUPPORT )
528#endif
529            tr_err( _( "Couldn't create socket: %s" ),
530                   tr_strerror( sockerrno ) );
531        }
532
533    if( s > -1 )
534        ++gFd->socketCount;
535
536    assert( gFd->socketCount >= 0 );
537
538    return s;
539}
540
541int
542tr_fdSocketAccept( int           b,
543                   tr_address  * addr,
544                   tr_port     * port )
545{
546    int s;
547    unsigned int len;
548    struct sockaddr_storage sock;
549
550    assert( addr );
551    assert( port );
552
553    len = sizeof( struct sockaddr_storage );
554    s = accept( b, (struct sockaddr *) &sock, &len );
555
556    if( ( s >= 0 ) && gFd->socketCount > getSocketMax( gFd ) )
557    {
558        EVUTIL_CLOSESOCKET( s );
559        s = -1;
560    }
561
562    if( s >= 0 )
563    {
564        /* "The ss_family field of the sockaddr_storage structure will always
565         * align with the family field of any protocol-specific structure." */
566        if( sock.ss_family == AF_INET )
567        {
568            struct sockaddr_in *si;
569            union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
570            s.dummy = sock;
571            si = &s.si;
572            addr->type = TR_AF_INET;
573            addr->addr.addr4.s_addr = si->sin_addr.s_addr;
574            *port = si->sin_port;
575        }
576        else
577        {
578            struct sockaddr_in6 *si;
579            union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
580            s.dummy = sock;
581            si = &s.si;
582            addr->type = TR_AF_INET6;
583            addr->addr.addr6 = si->sin6_addr;
584            *port = si->sin6_port;
585        }
586        ++gFd->socketCount;
587    }
588
589    return s;
590}
591
592void
593tr_fdSocketClose( int fd )
594{
595    if( fd >= 0 )
596    {
597        EVUTIL_CLOSESOCKET( fd );
598        --gFd->socketCount;
599    }
600
601    assert( gFd->socketCount >= 0 );
602}
603
604/***
605****
606****  Startup / Shutdown
607****
608***/
609
610void
611tr_fdInit( size_t openFileLimit, size_t socketLimit )
612{
613    int i;
614
615    assert( gFd == NULL );
616    gFd = tr_new0( struct tr_fd_s, 1 );
617    gFd->openFiles = tr_new0( struct tr_openfile, openFileLimit );
618    gFd->openFileLimit = openFileLimit;
619
620#ifdef HAVE_GETRLIMIT
621    {
622        struct rlimit rlim;
623        getrlimit( RLIMIT_NOFILE, &rlim );
624        rlim.rlim_cur = MIN( rlim.rlim_max,
625                            (rlim_t)( socketLimit + NOFILE_BUFFER ) );
626        setrlimit( RLIMIT_NOFILE, &rlim );
627        gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
628        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
629    }
630#else
631    gFd->socketLimit = socketLimit;
632#endif
633    tr_dbg( "%zu usable file descriptors", socketLimit );
634
635    for( i = 0; i < gFd->openFileLimit; ++i )
636        gFd->openFiles[i].fd = -1;
637}
638
639void
640tr_fdClose( void )
641{
642    struct tr_openfile * o;
643    const struct tr_openfile * end;
644
645    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
646        if( fileIsOpen( o ) )
647            TrCloseFile( o );
648
649    tr_free( gFd->openFiles );
650    tr_free( gFd );
651    gFd = NULL;
652}
653
654void
655tr_fdSetPeerLimit( uint16_t n )
656{
657    assert( gFd != NULL && "tr_fdInit() must be called first!" );
658    gFd->socketLimit = n;
659}
660
661uint16_t
662tr_fdGetPeerLimit( void )
663{
664    return gFd ? gFd->socketLimit : -1;
665}
Note: See TracBrowser for help on using the repository browser.