source: trunk/libtransmission/fdlimit.c @ 9066

Last change on this file since 9066 was 9066, checked in by charles, 12 years ago

add better logging to track down tr_ioRead() error messages reported by Rabbitbunny

  • Property svn:keywords set to Date Rev Author Id
File size: 16.6 KB
Line 
1/******************************************************************************
2 * $Id: fdlimit.c 9066 2009-09-08 06:25:40Z 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     * It's okay for this to fail silently, so don't let it affect errno */
357    {
358        const int err = errno;
359        posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
360        errno = err;
361    }
362#endif
363
364    tr_free( filename );
365    return 0;
366}
367
368static TR_INLINE tr_bool
369fileIsOpen( const struct tr_openfile * o )
370{
371    return o->fd >= 0;
372}
373
374static void
375TrCloseFile( struct tr_openfile * o )
376{
377    assert( o != NULL );
378    assert( fileIsOpen( o ) );
379
380    tr_close_file( o->fd );
381    o->fd = -1;
382}
383
384/* returns an fd on success, or a -1 on failure and sets errno */
385int
386tr_fdFileCheckout( int                      torrentId,
387                   const char             * folder,
388                   const char             * torrentFile,
389                   tr_bool                  doWrite,
390                   tr_preallocation_mode    preallocationMode,
391                   uint64_t                 desiredFileSize )
392{
393    int i, winner = -1;
394    struct tr_openfile * o;
395    char filename[MAX_PATH_LENGTH];
396
397    assert( torrentId > 0 );
398    assert( folder && *folder );
399    assert( torrentFile && *torrentFile );
400    assert( tr_isBool( doWrite ) );
401
402    tr_snprintf( filename, sizeof( filename ), "%s%c%s", folder, TR_PATH_DELIMITER, torrentFile );
403    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
404
405    /* is it already open? */
406    for( i=0; i<gFd->openFileLimit; ++i )
407    {
408        o = &gFd->openFiles[i];
409
410        if( !fileIsOpen( o ) )
411            continue;
412        if( torrentId != o->torrentId )
413            continue;
414        if( strcmp( filename, o->filename ) )
415            continue;
416
417        if( doWrite && !o->isWritable )
418        {
419            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
420            TrCloseFile( o );
421            break;
422        }
423
424        dbgmsg( "found it!  it's ready for use!" );
425        winner = i;
426        break;
427    }
428
429    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
430    while( winner < 0 )
431    {
432        uint64_t date = tr_date( ) + 1;
433
434        /* look for the file that's been open longest */
435        for( i=0; i<gFd->openFileLimit; ++i )
436        {
437            o = &gFd->openFiles[i];
438
439            if( !fileIsOpen( o ) )
440            {
441                winner = i;
442                dbgmsg( "found an empty slot in %d", winner );
443                break;
444            }
445
446            if( date > o->date )
447            {
448                date = o->date;
449                winner = i;
450            }
451        }
452
453        assert( winner >= 0 );
454
455        if( fileIsOpen( &gFd->openFiles[winner] ) )
456        {
457            dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
458            TrCloseFile( &gFd->openFiles[winner] );
459        }
460    }
461
462    assert( winner >= 0 );
463    o = &gFd->openFiles[winner];
464    if( !fileIsOpen( o ) )
465    {
466        const int err = TrOpenFile( winner, folder, torrentFile, doWrite,
467                                    preallocationMode, desiredFileSize );
468        if( err ) {
469            errno = err;
470            return -1;
471        }
472
473        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
474                doWrite ? 'y' : 'n' );
475        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
476        o->isWritable = doWrite;
477    }
478
479    dbgmsg( "checking out '%s' in slot %d", filename, winner );
480    o->torrentId = torrentId;
481    o->date = tr_date( );
482    return o->fd;
483}
484
485void
486tr_fdFileClose( const char * filename )
487{
488    struct tr_openfile * o;
489    const struct tr_openfile * end;
490
491    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
492    {
493        if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
494            continue;
495        dbgmsg( "tr_fdFileClose closing \"%s\"", filename );
496        TrCloseFile( o );
497    }
498}
499
500void
501tr_fdTorrentClose( int torrentId )
502{
503    struct tr_openfile * o;
504    const struct tr_openfile * end;
505
506    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
507        if( fileIsOpen( o ) && o->torrentId == torrentId )
508            TrCloseFile( o );
509}
510
511/***
512****
513****  Sockets
514****
515***/
516
517static TR_INLINE int
518getSocketMax( struct tr_fd_s * gFd )
519{
520    return gFd->socketLimit;
521}
522
523int
524tr_fdSocketCreate( int domain, int type )
525{
526    int s = -1;
527
528    if( gFd->socketCount < getSocketMax( gFd ) )
529        if( ( s = socket( domain, type, 0 ) ) < 0 )
530        {
531            if( sockerrno != EAFNOSUPPORT )
532                tr_err( _( "Couldn't create socket: %s" ),
533                        tr_strerror( sockerrno ) );
534        }
535
536    if( s > -1 )
537        ++gFd->socketCount;
538
539    assert( gFd->socketCount >= 0 );
540
541    return s;
542}
543
544int
545tr_fdSocketAccept( int           b,
546                   tr_address  * addr,
547                   tr_port     * port )
548{
549    int s;
550    unsigned int len;
551    struct sockaddr_storage sock;
552
553    assert( addr );
554    assert( port );
555
556    len = sizeof( struct sockaddr_storage );
557    s = accept( b, (struct sockaddr *) &sock, &len );
558
559    if( ( s >= 0 ) && gFd->socketCount > getSocketMax( gFd ) )
560    {
561        EVUTIL_CLOSESOCKET( s );
562        s = -1;
563    }
564
565    if( s >= 0 )
566    {
567        /* "The ss_family field of the sockaddr_storage structure will always
568         * align with the family field of any protocol-specific structure." */
569        if( sock.ss_family == AF_INET )
570        {
571            struct sockaddr_in *si;
572            union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
573            s.dummy = sock;
574            si = &s.si;
575            addr->type = TR_AF_INET;
576            addr->addr.addr4.s_addr = si->sin_addr.s_addr;
577            *port = si->sin_port;
578        }
579        else
580        {
581            struct sockaddr_in6 *si;
582            union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
583            s.dummy = sock;
584            si = &s.si;
585            addr->type = TR_AF_INET6;
586            addr->addr.addr6 = si->sin6_addr;
587            *port = si->sin6_port;
588        }
589        ++gFd->socketCount;
590    }
591
592    return s;
593}
594
595void
596tr_fdSocketClose( int fd )
597{
598    if( fd >= 0 )
599    {
600        EVUTIL_CLOSESOCKET( fd );
601        --gFd->socketCount;
602    }
603
604    assert( gFd->socketCount >= 0 );
605}
606
607/***
608****
609****  Startup / Shutdown
610****
611***/
612
613void
614tr_fdInit( size_t openFileLimit, size_t socketLimit )
615{
616    int i;
617
618    assert( gFd == NULL );
619    gFd = tr_new0( struct tr_fd_s, 1 );
620    gFd->openFiles = tr_new0( struct tr_openfile, openFileLimit );
621    gFd->openFileLimit = openFileLimit;
622
623#ifdef HAVE_GETRLIMIT
624    {
625        struct rlimit rlim;
626        getrlimit( RLIMIT_NOFILE, &rlim );
627        rlim.rlim_cur = MIN( rlim.rlim_max,
628                            (rlim_t)( socketLimit + NOFILE_BUFFER ) );
629        setrlimit( RLIMIT_NOFILE, &rlim );
630        gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
631        tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
632    }
633#else
634    gFd->socketLimit = socketLimit;
635#endif
636    tr_dbg( "%zu usable file descriptors", socketLimit );
637
638    for( i = 0; i < gFd->openFileLimit; ++i )
639        gFd->openFiles[i].fd = -1;
640}
641
642void
643tr_fdClose( void )
644{
645    struct tr_openfile * o;
646    const struct tr_openfile * end;
647
648    for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
649        if( fileIsOpen( o ) )
650            TrCloseFile( o );
651
652    tr_free( gFd->openFiles );
653    tr_free( gFd );
654    gFd = NULL;
655}
656
657void
658tr_fdSetPeerLimit( uint16_t n )
659{
660    assert( gFd != NULL && "tr_fdInit() must be called first!" );
661    gFd->socketLimit = n;
662}
663
664uint16_t
665tr_fdGetPeerLimit( void )
666{
667    return gFd ? gFd->socketLimit : -1;
668}
Note: See TracBrowser for help on using the repository browser.