fs_files.c 24.8 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/****************************************************************************
 * fs/inode/fs_files.c
 *
 *   Copyright (C) 2007-2009, 2011-2013, 2016-2017 Gregory Nutt. All rights
 *     reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/
#include "unistd.h"
#include "vfs_config.h"
#include "sys/types.h"
#include "string.h"
W
wangchenyang 已提交
44
#include "dirent.h"
W
wenjun 已提交
45 46 47 48 49 50
#include "semaphore.h"
#include "assert.h"
#include "errno.h"
#include "fs/file.h"
#include "stdio.h"
#include "stdlib.h"
M
mucor 已提交
51
#include "vnode.h"
W
wenjun 已提交
52 53 54 55 56 57 58
#include "los_mux.h"
#include "fs/fd_table.h"
#ifdef LOSCFG_NET_LWIP_SACK
#include "lwip/sockets.h"
#endif
#include "los_process_pri.h"
#include "los_vm_filemap.h"
M
mamingshuai 已提交
59
#include "mqueue.h"
W
wenjun 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define ferr PRINTK

#if CONFIG_NFILE_DESCRIPTORS > 0
struct filelist tg_filelist;
#endif

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

struct filelist *sched_getfiles(void)
{
#if CONFIG_NFILE_DESCRIPTORS > 0
  return &tg_filelist;
#else
  return NULL;
#endif
}

/* 32: An unsigned int takes 32 bits */

static unsigned int bitmap[CONFIG_NFILE_DESCRIPTORS / 32 + 1] = {0};

static void set_bit(int i, void *addr)
{
  unsigned int tem = (unsigned int)i >> 5; /* Get the bitmap subscript */
  unsigned int *addri = (unsigned int *)addr + tem;
  unsigned int old = *addri;
  old = old | (1UL << ((unsigned int)i & 0x1f)); /* set the new map bit */
  *addri = old;
}

static void clear_bit(int i, void *addr)
{
  unsigned int tem = (unsigned int)i >> 5; /* Get the bitmap subscript */
  unsigned int *addri = (unsigned int *)addr + tem;
  unsigned int old = *addri;
  old = old & ~(1UL << ((unsigned int)i & 0x1f)); /* Clear the old map bit */
  *addri = old;
}

W
wangchenyang 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114
bool get_bit(int i)
{
  unsigned int *p = NULL;
  unsigned int mask;

  p = ((unsigned int *)bitmap) + (i >> 5); /* Gets the location in the bitmap */
  mask = 1 << (i & 0x1f); /* Gets the mask for the current bit int bitmap */
  if (!(~(*p) & mask)){
    return true;
  }
  return false;
}


W
wenjun 已提交
115 116 117 118 119 120 121 122
/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: _files_semtake
 ****************************************************************************/

W
wangchenyang 已提交
123
static void _files_semtake(struct filelist *list)
W
wenjun 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{
  /* Take the semaphore (perhaps waiting) */

  while (sem_wait(&list->fl_sem) != 0)
    {
      /* The only case that an error should occur here is if the wait was
       * awakened by a signal.
       */

      LOS_ASSERT(get_errno() == EINTR);
    }
}

/****************************************************************************
 * Name: _files_semgive
 ****************************************************************************/

#define _files_semgive(list)  (void)sem_post(&list->fl_sem)

/****************************************************************************
 * Name: _files_close
 *
 * Description:
W
wangchenyang 已提交
147
 *   Close an vnode (if open)
W
wenjun 已提交
148 149 150 151 152 153
 *
 * Assumuptions:
 *   Caller holds the list semaphore because the file descriptor will be freed.
 *
 ****************************************************************************/

W
wangchenyang 已提交
154
static int _files_close(struct file *filep)
W
wenjun 已提交
155
{
W
wangchenyang 已提交
156
  struct Vnode *vnode = filep->f_vnode;
W
wenjun 已提交
157 158
  int ret = OK;

W
wangchenyang 已提交
159 160
  /* Check if the struct file is open (i.e., assigned an vnode) */
  if (filep->f_oflags & O_DIRECTORY)
W
wenjun 已提交
161
    {
W
wangchenyang 已提交
162 163
      ret = closedir(filep->f_dir);
      if (ret != OK)
W
wenjun 已提交
164
        {
W
wangchenyang 已提交
165
          return ret;
W
wenjun 已提交
166
        }
W
wangchenyang 已提交
167 168 169 170 171
    }
  else
    {
      /* Close the file, driver, or mountpoint. */
      if (filep->ops && filep->ops->close)
W
wenjun 已提交
172
        {
W
wangchenyang 已提交
173
          /* Perform the close operation */
W
wenjun 已提交
174

W
wangchenyang 已提交
175 176
          ret = filep->ops->close(filep);
          if (ret != OK)
W
wenjun 已提交
177
            {
W
wangchenyang 已提交
178
              return ret;
W
wenjun 已提交
179 180
            }
        }
W
wangchenyang 已提交
181 182
      VnodeHold();
      vnode->useCount--;
183 184
      /* Block char device is removed when close */
      if (vnode->type == VNODE_TYPE_BCHR)
W
wangchenyang 已提交
185
        {
186 187 188 189 190
          ret = VnodeFree(vnode);
          if (ret < 0)
            {
              PRINTK("Removing bchar device %s failed\n", filep->f_path);
            }
W
wangchenyang 已提交
191 192 193
        }
      VnodeDrop();
    }
194

W
wangchenyang 已提交
195
  /* Release the path of file */
W
wenjun 已提交
196

W
wangchenyang 已提交
197
  free(filep->f_path);
W
wenjun 已提交
198

W
wangchenyang 已提交
199
  /* Release the file descriptor */
W
wenjun 已提交
200

W
wangchenyang 已提交
201 202 203 204 205 206 207 208 209
  filep->f_magicnum = 0;
  filep->f_oflags   = 0;
  filep->f_pos      = 0;
  filep->f_path     = NULL;
  filep->f_priv     = NULL;
  filep->f_vnode    = NULL;
  filep->f_refcount = 0;
  filep->f_mapping  = NULL;
  filep->f_dir      = NULL;
W
wenjun 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

  return ret;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: files_initialize
 *
 * Description:
 *   This is called from the FS initialization logic to configure the files.
 *
 ****************************************************************************/

void files_initialize(void)
{
}

/****************************************************************************
 * Name: files_initlist
 *
 * Description: Initializes the list of files for a new task
 *
 ****************************************************************************/

W
wangchenyang 已提交
237
void files_initlist(struct filelist *list)
W
wenjun 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
  DEBUGASSERT(list);

  /* Initialize the list access mutex */

  (void)sem_init(&list->fl_sem, 0, 1);
}

/****************************************************************************
 * Name: files_releaselist
 *
 * Description:
 *   Release a reference to the file list
 *
 ****************************************************************************/

W
wangchenyang 已提交
254
void files_releaselist(struct filelist *list)
W
wenjun 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
{
  int i;

  DEBUGASSERT(list);

  /* Close each file descriptor .. Normally, you would need take the list
   * semaphore, but it is safe to ignore the semaphore in this context because
   * there should not be any references in this context.
   */

  for (i = 0; i < CONFIG_NFILE_DESCRIPTORS; i++)
    {
      (void)_files_close(&list->fl_files[i]);
    }

  /* Destroy the semaphore */

  (void)sem_destroy(&list->fl_sem);
}

/****************************************************************************
 * Name: file_dup2
 *
 * Description:
W
wangchenyang 已提交
279
 *   Assign an vnode to a specific files structure.  This is the heart of
W
wenjun 已提交
280 281 282 283 284 285 286 287 288 289 290 291
 *   dup2.
 *
 *   Equivalent to the non-standard fs_dupfd2() function except that it
 *   accepts struct file instances instead of file descriptors and it does
 *   not set the errno variable.
 *
 * Returned Value:
 *   Zero (OK) is returned on success; a negated errno value is return on
 *   any failure.
 *
 ****************************************************************************/

W
wangchenyang 已提交
292
int file_dup2(struct file *filep1, struct file *filep2)
W
wenjun 已提交
293
{
W
wangchenyang 已提交
294 295
  struct filelist *list = NULL;
  struct Vnode *vnode_ptr = NULL;
W
wenjun 已提交
296 297 298 299 300 301
  char *fullpath = NULL;
  const char *relpath = NULL;
  int err;
  int len;
  int ret;

W
wangchenyang 已提交
302
  if (!filep1 || !filep1->f_vnode || !filep2)
W
wenjun 已提交
303
    {
W
wangchenyang 已提交
304
      err = -EBADF;
W
wenjun 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
      goto errout;
    }

  list = sched_getfiles();
  DEBUGASSERT(list);
  /* The file list can be NULL under two cases:  (1) One is an obscure
   * cornercase:  When memory management debug output is enabled.  Then
   * there may be attempts to write to stdout from malloc before the group
   * data has been allocated.  The other other is (2) if this is a kernel
   * thread.  Kernel threads have no allocated file descriptors.
   */

  if (list != NULL)
    {
      _files_semtake(list);
    }

W
wangchenyang 已提交
322 323
  /* If there is already an vnode contained in the new file structure,
   * close the file and release the vnode.
W
wenjun 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
   */

  ret = _files_close(filep2);
  if (ret < 0)
    {
      /* An error occurred while closing the driver */

      goto errout_with_ret;
    }

  len = strlen(filep1->f_path);
  if ((len == 0) || (len >= PATH_MAX))
    {
      ret = -EINVAL;
      goto errout_with_ret;
    }

  fullpath = (char *)zalloc(len + 1);
  if (fullpath == NULL)
    {
      ret = -ENOMEM;
      goto errout_with_ret;
    }

W
wangchenyang 已提交
348
  /* Increment the reference count on the contained vnode */
W
wenjun 已提交
349

W
wangchenyang 已提交
350
  vnode_ptr = filep1->f_vnode;
W
wenjun 已提交
351 352 353 354 355

  /* Then clone the file structure */

  filep2->f_oflags = filep1->f_oflags;
  filep2->f_pos    = filep1->f_pos;
W
wangchenyang 已提交
356
  filep2->f_vnode  = vnode_ptr;
W
wenjun 已提交
357 358 359 360 361 362 363 364 365 366
  filep2->f_priv   = filep1->f_priv;

  (void)strncpy_s(fullpath, len + 1, filep1->f_path, len);
  filep2->f_path = fullpath;
  filep2->f_relpath = relpath;

  /* Call the open method on the file, driver, mountpoint so that it
   * can maintain the correct open counts.
   */

W
wangchenyang 已提交
367
  if (vnode_ptr->vop)
W
wenjun 已提交
368
    {
W
wangchenyang 已提交
369
      if (vnode_ptr->originMount)
W
wenjun 已提交
370 371 372
        {
          /* Dup the open file on the in the new file structure */

W
wangchenyang 已提交
373
          if (vnode_ptr == NULL)
W
wenjun 已提交
374 375 376 377 378 379 380 381
            {
              ret = -ENOSYS;
            }
        }
      else
        {
          /* (Re-)open the pseudo file or device driver */

W
wangchenyang 已提交
382
          if (vnode_ptr->vop->Open)
W
wenjun 已提交
383
            {
W
wangchenyang 已提交
384
              ret = vnode_ptr->vop->Open(vnode_ptr, 0, 0, 0);
W
wenjun 已提交
385 386 387 388 389 390 391 392 393 394 395
            }
          else
            {
              ret = -ENOSYS;
            }
        }

      /* Handle open failures */

      if (ret < 0)
        {
W
wangchenyang 已提交
396
          goto errout_with_vnode;
W
wenjun 已提交
397 398 399 400 401 402 403 404 405 406 407
        }
    }

  if (list != NULL)
    {
      _files_semgive(list);
    }
  return OK;

  /* Handle various error conditions */

W
wangchenyang 已提交
408
errout_with_vnode:
W
wenjun 已提交
409 410 411 412

  free(filep2->f_path);
  filep2->f_oflags  = 0;
  filep2->f_pos     = 0;
W
wangchenyang 已提交
413
  filep2->f_vnode   = NULL;
W
wenjun 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  filep2->f_priv    = NULL;
  filep2->f_path    = NULL;
  filep2->f_relpath = NULL;

errout_with_ret:
  err               = -ret;
  _files_semgive(list);

errout:
  set_errno(err);
  return VFS_ERROR;
}

#define FILE_START_FD 3

static inline unsigned int files_magic_generate(void)
{
    static unsigned int files_magic = 0;
    return files_magic++;
}

/****************************************************************************
 * Name: files_allocate
 *
 * Description:
 *   Allocate a struct files instance and associate it with an inode instance.
 *   Returns the file descriptor == index into the files array.
 *
 ****************************************************************************/

W
wangchenyang 已提交
444
int files_allocate(struct Vnode *vnode_ptr, int oflags, off_t pos, void *priv, int minfd)
W
wenjun 已提交
445
{
W
wangchenyang 已提交
446
  struct filelist *list = NULL;
W
wenjun 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  unsigned int *p = NULL;
  unsigned int mask;
  unsigned int i;
  struct files_struct *process_files = NULL;

  /* minfd should be a positive number,and 0,1,2 had be distributed to stdin,stdout,stderr */

  if (minfd < FILE_START_FD)
    {
      minfd = FILE_START_FD;
    }
  i = (unsigned int)minfd;

  list = sched_getfiles();
  DEBUGASSERT(list);

  _files_semtake(list);

  while (i < CONFIG_NFILE_DESCRIPTORS)
    {
      p = ((unsigned int *)bitmap) + (i >> 5); /* Gets the location in the bitmap */
      mask = 1 << (i & 0x1f); /* Gets the mask for the current bit int bitmap */
      if ((~(*p) & mask))
        {
          set_bit(i, bitmap);
          list->fl_files[i].f_oflags   = oflags;
          list->fl_files[i].f_pos      = pos;
W
wangchenyang 已提交
474
          list->fl_files[i].f_vnode    = vnode_ptr;
W
wenjun 已提交
475 476 477 478 479 480
          list->fl_files[i].f_priv     = priv;
          list->fl_files[i].f_refcount = 1;
          list->fl_files[i].f_mapping  = NULL;
          list->fl_files[i].f_dir      = NULL;
          list->fl_files[i].f_magicnum = files_magic_generate();
          process_files = OsCurrProcessGet()->files;
M
mamingshuai 已提交
481
          if (process_files == NULL)
W
wenjun 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
            {
              PRINT_ERR("process files is NULL, %s %d\n", __FUNCTION__ ,__LINE__);
              _files_semgive(list);
              return VFS_ERROR;
            }
          _files_semgive(list);
          return (int)i;
        }
      i++;
    }

  _files_semgive(list);
  return VFS_ERROR;
}

Z
zhOu 已提交
497
int files_close_internal(int fd, LosProcessCB *processCB)
W
wenjun 已提交
498
{
W
wangchenyang 已提交
499 500
  int ret = OK;
  struct filelist *list = NULL;
W
wenjun 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  struct files_struct *process_files = NULL;

  /* 0,1,2 fd is not opened in system, no need to close them */
  if ((fd >= STDIN_FILENO) && (fd <= STDERR_FILENO))
    {
      return OK;
    }

  /* Get the thread-specific file list.  It should never be NULL in this
   * context.
   */

  list = sched_getfiles();
  DEBUGASSERT(list != NULL);

W
wangchenyang 已提交
516
  /* If the file was properly opened, there should be an vnode assigned */
W
wenjun 已提交
517

W
wangchenyang 已提交
518
  if (fd < 0 || fd >= CONFIG_NFILE_DESCRIPTORS || !list->fl_files[fd].f_vnode)
W
wenjun 已提交
519 520 521 522 523 524 525 526
    {
      return -EBADF;
    }

  /* Perform the protected close operation */

  _files_semtake(list);
  process_files = processCB->files;
M
mamingshuai 已提交
527
  if (process_files == NULL)
W
wenjun 已提交
528 529 530 531 532 533 534 535
    {
      PRINT_ERR("process files is NULL, %s %d\n", __FUNCTION__ ,__LINE__);
      _files_semgive(list);
      return -EINVAL;
    }

  /* The filep->f_refcount may not be zero here, when the filep is shared in parent-child processes.
     so, upon closing the filep in current process, relevant region must be released immediately */
Y
YOUR_NAME 已提交
536 537
#ifdef LOSCFG_KERNEL_VM
  struct file *filep = &list->fl_files[fd];
W
wangchenyang 已提交
538 539

  OsVmmFileRegionFree(filep, processCB);
Y
YOUR_NAME 已提交
540
#endif
W
wenjun 已提交
541

W
wangchenyang 已提交
542
  list->fl_files[fd].f_refcount--;
W
wenjun 已提交
543 544
  if (list->fl_files[fd].f_refcount == 0)
    {
Y
YOUR_NAME 已提交
545
#ifdef LOSCFG_KERNEL_VM
W
wangchenyang 已提交
546
      dec_mapping_nolock(filep->f_mapping);
Y
YOUR_NAME 已提交
547
#endif
W
wenjun 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
      ret = _files_close(&list->fl_files[fd]);
      if (ret == OK)
        {
          clear_bit(fd, bitmap);
        }
    }

  _files_semgive(list);
  return ret;
}

/****************************************************************************
 * Name: files_close
 *
 * Description:
 *   Close an inode (if open)
 *
 * Assumuptions:
 *   Caller holds the list semaphore because the file descriptor will be freed.
 *
 ****************************************************************************/

int files_close(int fd)
{
  return files_close_internal(fd, OsCurrProcessGet());
}

/****************************************************************************
 * Name: files_release
 *
 * Assumuptions:
 *   Similar to files_close().  Called only from open() logic on error
 *   conditions.
 *
 ****************************************************************************/

void files_release(int fd)
{
W
wangchenyang 已提交
586
  struct filelist *list = NULL;
W
wenjun 已提交
587 588 589 590 591 592 593 594 595 596

  list = sched_getfiles();
  DEBUGASSERT(list);

  if (fd >=0 && fd < CONFIG_NFILE_DESCRIPTORS)
    {
      _files_semtake(list);

      list->fl_files[fd].f_magicnum = 0;
      list->fl_files[fd].f_oflags   = 0;
W
wangchenyang 已提交
597
      list->fl_files[fd].f_vnode    = NULL;
W
wenjun 已提交
598 599 600 601 602 603 604 605 606 607 608 609
      list->fl_files[fd].f_pos      = 0;
      list->fl_files[fd].f_refcount = 0;
      list->fl_files[fd].f_path     = NULL;
      list->fl_files[fd].f_relpath  = NULL;
      list->fl_files[fd].f_priv     = NULL;
      list->fl_files[fd].f_mapping  = NULL;
      list->fl_files[fd].f_dir      = NULL;
      clear_bit(fd, bitmap);
      _files_semgive(list);
    }
}

W
wangchenyang 已提交
610
struct Vnode *files_get_openfile(int fd)
W
wenjun 已提交
611
{
W
wangchenyang 已提交
612
  struct filelist *list = NULL;
W
wenjun 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625
  unsigned int *p = NULL;
  unsigned int mask;

  list = sched_getfiles();
  DEBUGASSERT(list);

  p = ((unsigned int *)bitmap) + ((unsigned int)fd >> 5); /* Gets the location in the bitmap */
  mask = 1 << ((unsigned int)fd & 0x1f); /* Gets the mask for the current bit int bitmap */
  if ((~(*p) & mask))
    {
      return NULL;
    }

W
wangchenyang 已提交
626
  return list->fl_files[fd].f_vnode;
W
wenjun 已提交
627 628 629 630
}

int alloc_fd(int minfd)
{
W
wangchenyang 已提交
631
  struct filelist *list = NULL;
W
wenjun 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  unsigned int *p = NULL;
  unsigned int mask;
  unsigned int i;

  /* minfd should be a positive number,and 0,1,2 had be distributed to stdin,stdout,stderr */

  if (minfd < FILE_START_FD)
    {
      minfd = FILE_START_FD;
    }
  i = (unsigned int)minfd;

  list = sched_getfiles();
  DEBUGASSERT(list);

  _files_semtake(list);

  while (i < CONFIG_NFILE_DESCRIPTORS)
    {
      p = ((unsigned int *)bitmap) + (i >> 5); /* Gets the location in the bitmap */
      mask = 1 << (i & 0x1f); /* Gets the mask for the current bit int bitmap */
      if ((~(*p) & mask))
        {
          set_bit(i, bitmap);
          _files_semgive(list);
          return (int)i;
        }
      i++;
    }
  _files_semgive(list);
  return VFS_ERROR;
}

void clear_fd(int fd)
{
  clear_bit(fd, bitmap);
}

W
wangchenyang 已提交
670
int close_files(struct Vnode *vnode)
W
wenjun 已提交
671 672 673
{
  int fd = 0;
  int ret = 0;
W
wangchenyang 已提交
674
  struct Vnode *open_file_vnode = NULL;
W
wenjun 已提交
675 676 677

  for (fd = FILE_START_FD; fd < CONFIG_NFILE_DESCRIPTORS; fd++)
    {
W
wangchenyang 已提交
678 679
      open_file_vnode = files_get_openfile(fd);
      if (open_file_vnode && (open_file_vnode == vnode))
W
wenjun 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693
        {
          ret = files_close(fd);
          if (ret < 0)
            {
              return -EBUSY;
            }
        }
    }

  return 0;
}

void files_refer(int fd)
{
M
maguangyao 已提交
694 695
  struct file *filep = NULL;

M
mucor 已提交
696
  struct filelist *list = sched_getfiles();
W
wenjun 已提交
697 698 699 700
  if (!list || fd < 0 || fd >= CONFIG_NFILE_DESCRIPTORS)
    {
      return;
    }
M
maguangyao 已提交
701

W
wenjun 已提交
702
  _files_semtake(list);
M
maguangyao 已提交
703 704 705 706 707
  (void)fs_getfilep(fd, &filep);
  if (filep != NULL)
    {
      filep->f_refcount++;
    }
W
wenjun 已提交
708 709 710
  _files_semgive(list);
}

M
mamingshuai 已提交
711 712 713 714 715 716 717 718 719 720
void alloc_std_fd(struct fd_table_s *fdt)
{
  fdt->ft_fds[STDIN_FILENO].sysFd = STDIN_FILENO;
  fdt->ft_fds[STDOUT_FILENO].sysFd = STDOUT_FILENO;
  fdt->ft_fds[STDERR_FILENO].sysFd = STDERR_FILENO;
  FD_SET(STDIN_FILENO, fdt->proc_fds);
  FD_SET(STDOUT_FILENO, fdt->proc_fds);
  FD_SET(STDERR_FILENO, fdt->proc_fds);
}

W
wenjun 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static void copy_fds(const struct fd_table_s *new_fdt, const struct fd_table_s *old_fdt)
{
  unsigned int sz;

  sz = new_fdt->max_fds * sizeof(struct file_table_s);
  if (sz)
    {
      (void)memcpy_s(new_fdt->ft_fds, sz, old_fdt->ft_fds, sz);
    }
  (void)memcpy_s(new_fdt->proc_fds, sizeof(fd_set), old_fdt->proc_fds, sizeof(fd_set));
}

static void copy_fd_table(struct fd_table_s *new_fdt, struct fd_table_s *old_fdt)
{
  copy_fds((const struct fd_table_s *)new_fdt, (const struct fd_table_s *)old_fdt);
  for (int i = 0; i < new_fdt->max_fds; i++)
    {
      if (FD_ISSET(i, new_fdt->proc_fds))
        {
          int sysFd = GetAssociatedSystemFd(i);
M
mamingshuai 已提交
741 742 743 744 745 746
          if ((sysFd >= 0) && (sysFd < CONFIG_NFILE_DESCRIPTORS))
            {
              files_refer(sysFd);
            }
#if defined(LOSCFG_NET_LWIP_SACK)
          if ((sysFd >= CONFIG_NFILE_DESCRIPTORS) && (sysFd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)))
W
wenjun 已提交
747 748 749 750
            {
              socks_refer(sysFd);
            }
#endif
M
mamingshuai 已提交
751 752 753 754 755 756 757 758
#if defined(LOSCFG_COMPAT_POSIX)
          if ((sysFd >= MQUEUE_FD_OFFSET) && (sysFd < (MQUEUE_FD_OFFSET + CONFIG_NQUEUE_DESCRIPTORS)))
            {
              mqueue_refer(sysFd);
            }
#endif

        }
W
wenjun 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
    }
}

static struct fd_table_s * alloc_fd_table(unsigned int numbers)
{
  struct fd_table_s *fdt;
  void *data;

  fdt = LOS_MemAlloc(m_aucSysMem0, sizeof(struct fd_table_s));
  if (!fdt)
    {
      goto out;
    }
  fdt->max_fds = numbers;
  if (!numbers)
    {
      fdt->ft_fds = NULL;
      fdt->proc_fds = NULL;
      return fdt;
    }
  data = LOS_MemAlloc(m_aucSysMem0, numbers * sizeof(struct file_table_s));
  if (!data)
    {
      goto out_fdt;
    }
  fdt->ft_fds = data;

  for (int i = STDERR_FILENO + 1; i < numbers; i++)
    {
        fdt->ft_fds[i].sysFd = -1;
    }

  data = LOS_MemAlloc(m_aucSysMem0, sizeof(fd_set));
  if (!data)
    {
      goto out_arr;
    }
  (VOID)memset_s(data, sizeof(fd_set), 0, sizeof(fd_set));
  fdt->proc_fds = data;
M
mamingshuai 已提交
798 799

  alloc_std_fd(fdt);
W
wenjun 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902

  (void)sem_init(&fdt->ft_sem, 0, 1);

  return fdt;

out_arr:
  (VOID)LOS_MemFree(m_aucSysMem0, fdt->ft_fds);
out_fdt:
  (VOID)LOS_MemFree(m_aucSysMem0, fdt);
out:
  return NULL;
}

struct files_struct *alloc_files(void)
{
  struct files_struct *files = LOS_MemAlloc(m_aucSysMem0, sizeof(struct files_struct));
  if (!files)
    {
      ferr("malloc file_struct error\n");
      return NULL;
    }
  files->count = 1;
  files->file_lock = 0;
  files->next_fd = 3;
#ifdef VFS_USING_WORKDIR
  spin_lock_init(&files->workdir_lock);
  memset_s(files->workdir, PATH_MAX, 0, PATH_MAX);
  files->workdir[0] = '/';
#endif
  files->fdt = alloc_fd_table(NR_OPEN_DEFAULT);
  if (!files->fdt)
    {
      ferr("malloc fdt error\n");
      (VOID)LOS_MemFree(m_aucSysMem0, files);
      return NULL;
    }

  return files;
}

struct files_struct *dup_fd(struct files_struct *old_files)
{
  struct fd_table_s *old_fdt = NULL;
  struct fd_table_s *new_fdt = NULL;
  struct files_struct *files = NULL;
  if((old_files == NULL) || (old_files->fdt == NULL) || (old_files->fdt->max_fds == 0))
    {
      return alloc_files();
    }
  files = LOS_MemAlloc(m_aucSysMem0, sizeof(struct files_struct));
  if(!files)
    {
      ferr("malloc file_struct error\n");
      return NULL;
    }
  files->count = 1;
  files->file_lock = 0;
  files->next_fd = old_files->next_fd;
#ifdef VFS_USING_WORKDIR
  spin_lock_init(&files->workdir_lock);
  memset_s(files->workdir, PATH_MAX, 0, PATH_MAX);
  strncpy_s(files->workdir, PATH_MAX - 1, old_files->workdir, PATH_MAX - 1);
#endif
  old_fdt = old_files->fdt;
  new_fdt = alloc_fd_table(old_fdt->max_fds);
  if(new_fdt == NULL)
    {
      PRINT_ERR("alloc fd_table failed\n");
      (VOID)LOS_MemFree(m_aucSysMem0, files);
      return NULL;
    }
  copy_fd_table(new_fdt, old_fdt);
  files->fdt = new_fdt;

  return files;
}

/****************************************************************************
 * Name: delete_files
 *
 * Description:
 *   Close a processCB's files specified by processCB and files
 *
 * Assumuptions:
 *   processCB->files may != files and processCB may != current processCB.
 *
 ****************************************************************************/
void delete_files(LosProcessCB *processCB, struct files_struct *files)
{
  if (files == NULL || processCB == NULL)
    {
      return;
    }

  if (files->fdt == NULL)
    {
      goto out_file;
    }

  for (int i = 0; i < files->fdt->max_fds; i++)
    {
      if (FD_ISSET(i, files->fdt->proc_fds))
        {
M
mamingshuai 已提交
903 904 905
          int sysFd = DisassociateProcessFd(i);
          close(sysFd);
          FreeProcessFd(i);
W
wenjun 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
        }
    }

  (VOID)sem_destroy(&files->fdt->ft_sem);
  (VOID)LOS_MemFree(m_aucSysMem0, files->fdt->ft_fds);
  (VOID)LOS_MemFree(m_aucSysMem0, files->fdt->proc_fds);
  (VOID)LOS_MemFree(m_aucSysMem0, files->fdt);
out_file:
  (VOID)LOS_MemFree(m_aucSysMem0, files);
  return;
}

struct files_struct *create_files_snapshot(const struct files_struct *old_files)
{
  struct fd_table_s *old_fdt = NULL;
  struct fd_table_s *new_fdt = NULL;
  struct files_struct *files = NULL;
  if ((old_files == NULL) || (old_files->fdt == NULL) || (old_files->fdt->max_fds == 0))
    {
      return NULL;
    }
  files = LOS_MemAlloc(m_aucSysMem0, sizeof(struct files_struct));
  if (!files)
    {
      PRINT_ERR("malloc file_struct error\n");
      return NULL;
    }
  files->count = 1;
  files->file_lock = 0;
  files->next_fd = old_files->next_fd;
#ifdef VFS_USING_WORKDIR
  spin_lock_init(&files->workdir_lock);
  memset_s(files->workdir, PATH_MAX, 0, PATH_MAX);
  strncpy_s(files->workdir, PATH_MAX - 1, old_files->workdir, PATH_MAX - 1);
#endif
  old_fdt = old_files->fdt;
  new_fdt = alloc_fd_table(old_fdt->max_fds);
  if (new_fdt == NULL)
    {
      PRINT_ERR("alloc fd_table failed\n");
      (VOID)LOS_MemFree(m_aucSysMem0, files);
      return NULL;
    }
  copy_fds((const struct fd_table_s *)new_fdt, (const struct fd_table_s *)old_fdt);
  files->fdt = new_fdt;

  return files;

}

void delete_files_snapshot(struct files_struct *files)
{
  if (files == NULL)
    {
      return;
    }
  if (files->fdt == NULL)
    {
      goto out_file;
    }

  (VOID)sem_destroy(&files->fdt->ft_sem);
  (VOID)LOS_MemFree(m_aucSysMem0, files->fdt->ft_fds);
  (VOID)LOS_MemFree(m_aucSysMem0, files->fdt->proc_fds);
  (VOID)LOS_MemFree(m_aucSysMem0, files->fdt);
out_file:
  (VOID)LOS_MemFree(m_aucSysMem0, files);
  return;
}