filehash/ffdb_pagepool.c
int
ffdb_pagepool_open_fd (ffdb_pagepool_t* pgp,
		       int fd,
		       const unsigned int pagesize,
		       const unsigned int maxcache)
{
  unsigned int oflags = 0;
  unsigned int flags = 0;
  struct stat sb;

  /* check whether some other threads already opened this file */
  FFDB_LOCK(pgp->lock);
  if (pgp->fd != -1) {
    FFDB_UNLOCK(pgp->lock);
    return 0;
  }

  /* get flags of this opened file descriptor 
   * this flag has the same values defined in open(2) system call
   */
  oflags = fcntl (fd, F_GETFL);
  if ((oflags & O_ACCMODE) == O_RDONLY)
    FFDB_FLAG_SET (flags, FFDB_RDONLY);

  /**
   * first check open flags
   */
  unsigned int ok_flags = FFDB_CREATE | FFDB_RDONLY | FFDB_DIRECT;
  if (FFDB_FLAG_ISSET(flags, ~ok_flags)) {
    fprintf (stderr, "ffdb_pagepool_open wrong flags specification\n");
    goto openerr;
  }
  pgp->fileflags = flags;

  /**
   * Check page size
   */
  if (!IS_VALID_PAGESIZE(pagesize)) {
    fprintf (stderr, "ffdb_pagepool_open wrong pagesize\n");
    errno = EINVAL;
    goto openerr;
  }
   
  /* Now check a few more things about this file */
  if (fstat (fd,  &sb) != 0) 
    goto openerr;

  if (!S_ISREG(sb.st_mode)) {
    errno = EINVAL;
    goto openerr;
  }

  /* File size should be multiple of pagesize */
  if (sb.st_size % pagesize != 0) {
    fprintf (stderr, "filesize %lld is not multiple of pagesize %d\n",
	     sb.st_size, pagesize);
    errno = EINVAL;
    goto openerr;
  }

  /* Set up some attribute of ffdb_pagepool structure */
  pgp->fd = fd;
  pgp->close_fd = 0;
  pgp->maxcache = maxcache;
  pgp->pagesize = pagesize;

  /* number of pages I am holding */
  pgp->npages = sb.st_size / pagesize;

  /* maximum page number the pool has now */
  pgp->maxpgno = pgp->npages;

  /* unlock the code */
  FFDB_UNLOCK(pgp->lock);
  return 0;

 openerr:
  FFDB_UNLOCK(pgp->lock);
  return errno;
}
/**
 * Open a page cache poll object using a given file
 */
Open a page cache poll object using a given file
Open a page cache poll object using a given file
int
ffdb_pagepool_open_fd (ffdb_pagepool_t* pgp,
		       int fd,
		       const unsigned int pagesize,
		       const unsigned int maxcache)
{
  unsigned int oflags = 0;
  unsigned int flags = 0;
  struct stat sb;
  FFDB_LOCK(pgp->lock);
  if (pgp->fd != -1) {
    FFDB_UNLOCK(pgp->lock);
    return 0;
  }
  oflags = fcntl (fd, F_GETFL);
  if ((oflags & O_ACCMODE) == O_RDONLY)
    FFDB_FLAG_SET (flags, FFDB_RDONLY);
  unsigned int ok_flags = FFDB_CREATE | FFDB_RDONLY | FFDB_DIRECT;
  if (FFDB_FLAG_ISSET(flags, ~ok_flags)) {
    fprintf (stderr, "ffdb_pagepool_open wrong flags specification\n");
    goto openerr;
  }
  pgp->fileflags = flags;
  if (!IS_VALID_PAGESIZE(pagesize)) {
    fprintf (stderr, "ffdb_pagepool_open wrong pagesize\n");
    errno = EINVAL;
    goto openerr;
  }
  if (fstat (fd,  &sb) != 0) 
    goto openerr;
  if (!S_ISREG(sb.st_mode)) {
    errno = EINVAL;
    goto openerr;
  }
  if (sb.st_size % pagesize != 0) {
    fprintf (stderr, "filesize %lld is not multiple of pagesize %d\n",
	     sb.st_size, pagesize);
    errno = EINVAL;
    goto openerr;
  }
  pgp->fd = fd;
  pgp->close_fd = 0;
  pgp->maxcache = maxcache;
  pgp->pagesize = pagesize;
  pgp->npages = sb.st_size / pagesize;
  pgp->maxpgno = pgp->npages;
  FFDB_UNLOCK(pgp->lock);
  return 0;
 openerr:
  FFDB_UNLOCK(pgp->lock);
  return errno;
}
int ffdb_pagepool_open_fd ( ffdb_pagepool_t * pgp , int fd , const unsigned int pagesize , const unsigned int maxcache ) { unsigned int oflags = 0 ; unsigned int flags = 0 ; struct stat sb ; FFDB_LOCK ( pgp -> lock ) ; if ( pgp -> fd != -1 ) { FFDB_UNLOCK ( pgp -> lock ) ; return 0 ; } oflags = fcntl ( fd , F_GETFL ) ; if ( ( oflags & O_ACCMODE ) == O_RDONLY ) FFDB_FLAG_SET ( flags , FFDB_RDONLY ) ; unsigned int ok_flags = FFDB_CREATE | FFDB_RDONLY | FFDB_DIRECT ; if ( FFDB_FLAG_ISSET ( flags , ~ ok_flags ) ) { fprintf ( stderr , " \n " ) ; goto openerr ; } pgp -> fileflags = flags ; if ( ! IS_VALID_PAGESIZE ( pagesize ) ) { fprintf ( stderr , " \n " ) ; errno = EINVAL ; goto openerr ; } if ( fstat ( fd , & sb ) != 0 ) goto openerr ; if ( ! S_ISREG ( sb . st_mode ) ) { errno = EINVAL ; goto openerr ; } if ( sb . st_size % pagesize != 0 ) { fprintf ( stderr , " \n " , sb . st_size , pagesize ) ; errno = EINVAL ; goto openerr ; } pgp -> fd = fd ; pgp -> close_fd = 0 ; pgp -> maxcache = maxcache ; pgp -> pagesize = pagesize ; pgp -> npages = sb . st_size / pagesize ; pgp -> maxpgno = pgp -> npages ; FFDB_UNLOCK ( pgp -> lock ) ; return 0 ; openerr : FFDB_UNLOCK ( pgp -> lock ) ; return errno ; }
Open a page cache poll object using a given file
Open a page cache poll object using a given file
/* check whether some other threads already opened this file */ /* get flags of this opened file descriptor 
   * this flag has the same values defined in open(2) system call
   */ /**
   * first check open flags
   */ /**
   * Check page size
   */ /* Now check a few more things about this file */ /* File size should be multiple of pagesize */ /* Set up some attribute of ffdb_pagepool structure */ /* number of pages I am holding */ /* maximum page number the pool has now */ /* unlock the code */