fs_file_mapping.c 15.0 KB
Newer Older
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
/*
 * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
 *
 * 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 of the copyright holder 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 HOLDER 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.
 */

#include "fs/fs.h"
#include "fs/fs_operation.h"
#include "fs_other.h"
#include "unistd.h"
#include "los_mux.h"
#include "los_list.h"
#include "los_atomic.h"
#include "los_vm_filemap.h"
40 41

#if 0 //@note_#if0
42 43
定义见于 ..\third_party\NuttX\include\nuttx\fs\fs.h
typedef volatile INT32 Atomic;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
//page_mapping描述的是一个文件在内存中被映射了多少页,<文件,文件页的关系>
/* file mapped in VMM pages */
struct page_mapping {//记录文件页和文件关系的结构体,叫文件页映射
  LOS_DL_LIST                           page_list;    /* all pages */ //链表上挂的是属于该文件的所有FilePage,这些页的内容都来源同一个文件
  SPIN_LOCK_S                           list_lock;    /* lock protecting it */ //操作page_list的自旋锁
  LosMux                                mux_lock;     /* mutex lock */	//		//操作page_mapping的互斥量
  unsigned long                         nrpages;      /* number of total pages */ //page_list的节点数量	
  unsigned long                         flags;			//@note_why 全量代码中也没查到源码中对其操作	
  Atomic                                ref;          /* reference counting */	//引用次数(自增/自减),对应add_mapping/dec_mapping
  struct file                           *host;        /* owner of this mapping *///属于哪个文件的映射
};

/* map: full_path(owner) <-> mapping */ //叫文件映射
struct file_map { //为在内核层面文件在内存的身份证,每个需映射到内存的文件必须创建一个file_map,都挂到全局g_file_mapping链表上
  LOS_DL_LIST           head;		//链表节点,用于挂到g_file_mapping上
  LosMux                lock;         /* lock to protect this mapping */
  struct page_mapping   mapping;	//每个文件都有唯一的page_mapping标识其在内存的身份
  char                  *owner;     /* owner: full path of file *///文件全路径来标识唯一性
62 63
};

64
struct file //文件系统最重要的两个结构体之一,另一个是inode
65
{
66 67 68 69 70 71 72 73 74 75
  unsigned int         f_magicnum;  /* file magic number */ //文件魔法数字
  int                  f_oflags;    /* Open mode flags */	//打开模式标签
  FAR struct inode     *f_inode;    /* Driver interface */	//设备驱动程序
  loff_t               f_pos;       /* File position */		//文件的位置
  unsigned long        f_refcount;  /* reference count */	//被引用的数量,一个文件可被多个进程打开
  char                 *f_path;     /* File fullpath */		//全路径
  void                 *f_priv;     /* Per file driver private data */ //文件私有数据
  const char           *f_relpath;  /* realpath */			//真实路径
  struct page_mapping  *f_mapping;  /* mapping file to memory */ //与内存的映射 page-cache
  void                 *f_dir;      /* DIR struct for iterate the directory if open a directory */ //所在目录
76
};
77

78
#endif
79

80
static struct file_map g_file_mapping = {0};//用于挂载所有文件的file_map
81 82 83

/**************************************************************************************************
 初始化文件映射模块,
84
 file_map: 每个需映射到内存的文件必须创建一个 file_map,都挂到全局g_file_mapping链表上
85 86 87
 page_mapping: 记录的是<文件,文件页>的关系,一个文件在操作过程中被映射成了多少个页,
 file:是文件系统管理层面的概念
**************************************************************************************************/
88 89 90 91
uint init_file_mapping()
{
    uint ret;

92
    LOS_ListInit(&g_file_mapping.head);//初始化全局文件映射节点,所有文件的映射都将g_file_mapping.head挂在链表上
93 94 95 96 97 98 99 100

    ret = LOS_MuxInit(&g_file_mapping.lock, NULL);//初始化文件映射互斥锁
    if (ret != LOS_OK) {
        PRINT_ERR("Create mutex for file map of page cache failed, (ret=%u)\n", ret);
    }

    return ret;
}
101
//以无锁的方式通过文件名查找文件映射并返回
102 103 104 105 106
static struct page_mapping *find_mapping_nolock(const char *fullpath)
{
    struct file_map *fmap = NULL;

    LOS_DL_LIST_FOR_EACH_ENTRY(fmap, &g_file_mapping.head, struct file_map, head) {//遍历文件映射链表
107
        if (!strcmp(fmap->owner, fullpath)) {//用整个文件路径来标识文件的唯一性
108 109 110 111 112 113
            return &fmap->mapping;
        }
    }

    return NULL;
}
114 115 116 117 118
/**************************************************************************************************
 增加一个文件映射,这个函数被do_open()函数调用,每打开一次文件就会调用一次
 注意不同的进程打开同一个文件,拿到的file是不一样的。
 https://blog.csdn.net/cywosp/article/details/38965239
**************************************************************************************************/
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void add_mapping(struct file *filep, const char *fullpath)
{
    void *tmp = NULL;
    struct file_map *fmap = NULL;
    int fmap_len = sizeof(struct file_map);
    int path_len;
    struct page_mapping *mapping = NULL;
    status_t retval;

    if (filep == NULL || fullpath == NULL) {
        return;
    }

    (VOID)LOS_MuxLock(&g_file_mapping.lock, LOS_WAIT_FOREVER);//操作临界区,先拿锁

    path_len = strlen(fullpath) + 1;
135
    mapping = find_mapping_nolock(fullpath);//是否已有文件映射
136 137
    if (mapping) {//有映射过的情况
        LOS_AtomicInc(&mapping->ref);//引用自增
138 139
        filep->f_mapping = mapping;//记录文件自己在内存的身份
        mapping->host = filep;	//记录page_mapping的老板
140 141 142 143 144 145 146 147
        (VOID)LOS_MuxUnlock(&g_file_mapping.lock);//释放锁
        return;//收工
    }

    (VOID)LOS_MuxUnlock(&g_file_mapping.lock);//没有映射过的情况下 先释放锁

    fmap = (struct file_map *)LOS_MemAlloc(m_aucSysMem0, fmap_len);//分配一个file_map

148
    /* page-cache as a optimization feature, just return when out of memory */
149

150
    if (!fmap) {//页面缓存作为一个优化功能,当内存不足时返回
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        PRINT_WARN("%s-%d: Mem alloc failed. fmap length(%d)\n",
                   __FUNCTION__, __LINE__, fmap_len);
        return;
    }
    tmp = LOS_MemAlloc(m_aucSysMem0, path_len);//分配一块内存放文件路径,为什么要这么做?因为需要在内核区操作,而参数路径在用户区

    /* page-cache as a optimization feature, just return when out of memory */

    if (!tmp) {
        PRINT_WARN("%s-%d: Mem alloc failed. fmap length(%d), fmap(%p), path length(%d)\n",
                   __FUNCTION__, __LINE__, fmap_len, fmap, path_len);
        LOS_MemFree(m_aucSysMem0, fmap);
        return;
    }

    (void)memset_s(fmap, fmap_len, 0, fmap_len);//清0
167
    fmap->owner = tmp;//赋值,此时owner指向内核区,但要记得释放掉
168
    LOS_AtomicSet(&fmap->mapping.ref, 1);//引用设为1
169
    (void)strcpy_s(fmap->owner, path_len, fullpath);//将参数fullpath由用户区拷贝到内核区 @note_thinking 鸿蒙提供了专门的拷贝函数 arch_copy_from_user 为啥不用?
170 171 172 173 174 175 176

    LOS_ListInit(&fmap->mapping.page_list);//初始化文件映射的页表链表,上面将会挂这个文件映射的所有虚拟内存页
    LOS_SpinInit(&fmap->mapping.list_lock);//初始化文件映射的自旋锁
    retval = LOS_MuxInit(&fmap->mapping.mux_lock, NULL);//初始化文件映射的互斥锁
    if (retval != LOS_OK) {
        PRINT_ERR("%s %d, Create mutex for mapping.mux_lock failed, status: %d\n", __FUNCTION__, __LINE__, retval);
    }
177 178
    (VOID)LOS_MuxLock(&g_file_mapping.lock, LOS_WAIT_FOREVER);//拿锁操作g_file_mapping
    LOS_ListTailInsert(&g_file_mapping.head, &fmap->head);//将文件映射结点挂入全局链表
179 180
    (VOID)LOS_MuxUnlock(&g_file_mapping.lock);//释放锁

181 182
    filep->f_mapping = &fmap->mapping;//<file,file_map>之间互绑
    filep->f_mapping->host = filep;//<file,file_map>之间互绑,从此相亲相爱一家人
183 184 185

    return;
}
186
//通过参数路径查找文件映射并返回
187 188 189 190 191 192 193 194 195 196
struct page_mapping *find_mapping(const char *fullpath)
{
    struct page_mapping *mapping = NULL;

    if (fullpath == NULL) {
        return NULL;
    }

    (VOID)LOS_MuxLock(&g_file_mapping.lock, LOS_WAIT_FOREVER);

197
    mapping = find_mapping_nolock(fullpath);//找啊找!外面加锁了,就无需再锁了
198 199 200 201 202 203 204 205
    if (mapping) {//找到
        LOS_AtomicInc(&mapping->ref);//引用自增
    }

    (VOID)LOS_MuxUnlock(&g_file_mapping.lock);

    return mapping;
}
206
//引用递减,删除或关闭文件时 由 files_close_internal调用
207 208 209 210 211 212 213 214 215 216 217 218
void dec_mapping(struct page_mapping *mapping)
{
    if (mapping == NULL) {
        return;
    }

    (VOID)LOS_MuxLock(&g_file_mapping.lock, LOS_WAIT_FOREVER);
    if (LOS_AtomicRead(&mapping->ref) > 0) {
        LOS_AtomicDec(&mapping->ref);//ref 递减
    }
    (VOID)LOS_MuxUnlock(&g_file_mapping.lock);
}
219
//以无锁方式清除文件映射
220 221 222 223 224
void clear_file_mapping_nolock(const struct page_mapping *mapping)
{
    unsigned int i = 3; /* file start fd */
    struct file *filp = NULL;

225
    while (i < CONFIG_NFILE_DESCRIPTORS) {//循环遍历查找,0,1,2已经固定分配了,固从3始
226
        filp = &tg_filelist.fl_files[i];//一个个来
227 228 229
        if (filp->f_mapping == mapping) {//找到了
            filp->f_mapping = NULL;//直接NULL,注意这里并没有break,而是继续撸到最后,因为 file:mapping 是 N:1的关系
        }//仔细想想:多个进程会使用同一个page_mapping,因为page_mapping的创建是由fullPath来唯一标识的
230 231 232
        i++;
    }
}
233 234 235 236 237 238

/******************************************************************************
 删除一个文件映射,需要有个三个地方删除才算断开了文件和内存的联系.
 
 1. 进程文件
******************************************************************************/
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
int remove_mapping_nolock(const char *fullpath, const struct file *ex_filp)
{
    int fd;
    struct file *filp = NULL;
    struct file_map *fmap = NULL;
    struct page_mapping *mapping = NULL;
    struct inode *node = NULL;

    if (fullpath == NULL) {
        set_errno(EINVAL);
        return EINVAL;
    }

    /* file start fd */

    for (fd = 3; fd < CONFIG_NFILE_DESCRIPTORS; fd++) {
        node = files_get_openfile(fd);//通过文件句柄获取inode
        if (node == NULL) {
            continue;
        }
        filp = &tg_filelist.fl_files[fd];//拿到文件

        /* ex_filp NULL: do not exclude any file, just matching the file name ; ex_filp not NULL: exclude it.
         * filp != ex_filp includes the two scenarios.
         */

        if (filp != ex_filp) {
            if (filp->f_path == NULL) {
                continue;
            }
            if ((strcmp(filp->f_path, fullpath) == 0)) {
                PRINT_WARN("%s is open(fd=%d), remove cache failed.\n", fullpath, fd);
                set_errno(EBUSY);
                return EBUSY;
            }
        }
    }

    (VOID)LOS_MuxLock(&g_file_mapping.lock, LOS_WAIT_FOREVER);

    mapping = find_mapping_nolock(fullpath);
    if (!mapping) {
        /* this scenario is a normal case */

        goto out;
    }

    (VOID)LOS_MuxDestroy(&mapping->mux_lock);
287 288
    clear_file_mapping_nolock(mapping);//清除进程对page_mapping的映射
    OsFileCacheRemove(mapping);//从页高速缓存中删除文件页
289
    fmap = LOS_DL_LIST_ENTRY(mapping,
290 291
    struct file_map, mapping);//通过page_mapping找到fmap
    LOS_ListDelete(&fmap->head);//从g_file_mapping链表上摘掉自己
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    LOS_MemFree(m_aucSysMem0, fmap);

out:
    (VOID)LOS_MuxUnlock(&g_file_mapping.lock);

    return OK;
}
//删除文件映射
int remove_mapping(const char *fullpath, const struct file *ex_filp)
{
    int ret;
    struct filelist *f_list = NULL;

    f_list = &tg_filelist;
    ret = sem_wait(&f_list->fl_sem);//等待信号量
    if (ret < 0) {
        PRINTK("sem_wait error, ret=%d\n", ret);
        return VFS_ERROR;
    }

    ret = remove_mapping_nolock(fullpath, ex_filp);//调用删除实体

    (void)sem_post(&f_list->fl_sem);//发出信号量
    return OK;
}
317
//重命名文件映射,
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
void rename_mapping(const char *src_path, const char *dst_path)
{
    int ret;
    void *tmp = NULL;
    int path_len;
    struct file_map *fmap = NULL;
    struct page_mapping *mapping = NULL;

    if (src_path == NULL || dst_path == NULL) {
        return;
    }

    path_len = strlen(dst_path) + 1;

    /* protect the whole list in case of this node been deleted just after we found it */

    (VOID)LOS_MuxLock(&g_file_mapping.lock, LOS_WAIT_FOREVER);

    mapping = find_mapping_nolock(src_path);
    if (!mapping) {
        /* this scenario is a normal case */

        goto out;
    }

    fmap = LOS_DL_LIST_ENTRY(mapping,
    struct file_map, mapping);

    tmp = LOS_MemAlloc(m_aucSysMem0, path_len);
    if (!tmp) {
        /* in this extremly low-memory situation, un-referenced page caches can be recycled by Pagecache LRU */

        PRINT_ERR("%s-%d: Mem alloc failed, path length(%d)\n", __FUNCTION__, __LINE__, path_len);
        goto out;
    }
    ret = strcpy_s(tmp, path_len, dst_path);
    if (ret != 0) {
        (VOID)LOS_MemFree(m_aucSysMem0, tmp);
        goto out;
    }

    /* whole list is locked, so we don't protect this node here */

    (VOID)LOS_MemFree(m_aucSysMem0, fmap->owner);
    fmap->owner = tmp;

out:
    (VOID)LOS_MuxUnlock(&g_file_mapping.lock);
    return;
}