2011-04-29 06:24:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_ioctl.c
|
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-04-29 06:24:10 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-29 06:24:10 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-04-29 06:24:10 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-11-16 01:22:23 +08:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2011-04-29 06:24:10 +08:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard mountpoint ioctl method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
2011-05-06 04:01:43 +08:00
|
|
|
int ret;
|
2011-04-29 06:24:10 +08:00
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("cmd: %d arg: %08lx\n", cmd, arg);
|
2011-04-29 06:24:10 +08:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 17:17:05 +08:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2011-04-29 06:24:10 +08:00
|
|
|
|
|
|
|
/* Recover the file system state from the open file */
|
|
|
|
|
|
|
|
volume = filep->f_inode->i_private;
|
|
|
|
DEBUGASSERT(volume != NULL);
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
/* Get exclusive access to the volume. Note that the volume lock
|
2011-05-06 04:01:43 +08:00
|
|
|
* protects the open file list.
|
|
|
|
*/
|
2011-04-29 06:24:10 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
if (ret < 0)
|
2011-05-06 04:01:43 +08:00
|
|
|
{
|
2023-04-06 14:16:37 +08:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2011-05-06 04:01:43 +08:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-05-06 07:31:44 +08:00
|
|
|
/* Only a reformat and optimize commands are supported */
|
2011-05-06 04:01:43 +08:00
|
|
|
|
|
|
|
if (cmd == FIOC_REFORMAT)
|
|
|
|
{
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Reformat command\n");
|
2011-05-06 04:01:43 +08:00
|
|
|
|
|
|
|
/* We cannot reformat the volume if there are any open inodes */
|
|
|
|
|
|
|
|
if (volume->ofiles)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Open files\n");
|
2011-05-06 04:01:43 +08:00
|
|
|
ret = -EBUSY;
|
2022-09-06 14:18:45 +08:00
|
|
|
goto errout_with_lock;
|
2011-05-06 04:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-format the volume -- all is lost */
|
|
|
|
|
|
|
|
ret = nxffs_reformat(volume);
|
|
|
|
}
|
|
|
|
|
2011-05-06 07:31:44 +08:00
|
|
|
else if (cmd == FIOC_OPTIMIZE)
|
|
|
|
{
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Optimize command\n");
|
2011-05-06 07:31:44 +08:00
|
|
|
|
|
|
|
/* Pack the volume */
|
|
|
|
|
|
|
|
ret = nxffs_pack(volume);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-13 08:23:03 +08:00
|
|
|
/* Command not recognized, forward to the MTD driver */
|
2011-05-06 04:01:43 +08:00
|
|
|
|
2018-12-13 08:23:03 +08:00
|
|
|
ret = MTD_IOCTL(volume->mtd, cmd, arg);
|
2011-05-06 07:31:44 +08:00
|
|
|
}
|
2011-05-06 04:01:43 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
errout_with_lock:
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2011-05-06 04:01:43 +08:00
|
|
|
errout:
|
|
|
|
return ret;
|
2011-04-29 06:24:10 +08:00
|
|
|
}
|