proc_vfs.c 11.4 KB
Newer Older
W
wangchenyang 已提交
1
/*
2
 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. All rights reserved.
W
wangchenyang 已提交
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
 *
 * 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 "proc_file.h"

#include <sys/statfs.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#include "fs/dirent_fs.h"
M
mucor 已提交
39 40
#include "fs/mount.h"
#include "fs/fs.h"
W
wangchenyang 已提交
41 42 43 44 45 46 47
#include "los_tables.h"
#include "internal.h"

#ifdef LOSCFG_FS_PROC
static struct VnodeOps g_procfsVops;
static struct file_operations_vfs g_procfsFops;

Z
zhushengle 已提交
48
struct ProcDirEntry *VnodeToEntry(struct Vnode *node)
W
wangchenyang 已提交
49 50 51 52 53 54 55 56 57 58 59 60
{
    return (struct ProcDirEntry *)(node->data);
}

static struct Vnode *EntryToVnode(struct ProcDirEntry *entry)
{
    struct Vnode *node = NULL;

    (void)VnodeAlloc(&g_procfsVops, &node);
    node->fop = &g_procfsFops;
    node->data = entry;
    node->type = entry->type;
C
chenwei 已提交
61 62 63
    node->uid = entry->uid;
    node->gid = entry->gid;
    node->mode = entry->mode;
W
wangchenyang 已提交
64 65 66 67 68 69 70 71 72 73 74
    return node;
}

static int EntryMatch(const char *name, int len, const struct ProcDirEntry *pn)
{
    if (len != pn->nameLen) {
        return 0;
    }
    return !strncmp(name, pn->name, len);
}

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
int VfsProcfsTruncate(struct Vnode *pVnode, off_t len)
{
    return 0;
}

int VfsProcfsCreate(struct Vnode* parent, const char *name, int mode, struct Vnode **vnode)
{
    int ret;
    struct Vnode *vp = NULL;
    struct ProcDirEntry *curEntry = NULL;

    struct ProcDirEntry *parentEntry = VnodeToEntry(parent);
    if (parentEntry == NULL) {
        return -ENODATA;
    }

    ret = VnodeAlloc(&g_procfsVops, &vp);
    if (ret != 0) {
        return -ENOMEM;
    }

    curEntry = ProcCreate(name, mode, parentEntry, NULL);
    if (curEntry == NULL) {
        VnodeFree(vp);
        return -ENODATA;
    }

    vp->data = curEntry;
    vp->type = curEntry->type;
    if (vp->type == VNODE_TYPE_DIR) {
        vp->mode = S_IFDIR | PROCFS_DEFAULT_MODE;
    } else {
        vp->mode = S_IFREG | PROCFS_DEFAULT_MODE;
    }

    vp->vop = parent->vop;
    vp->fop = parent->fop;
    vp->parent = parent;
    vp->originMount = parent->originMount;

    *vnode = vp;

    return LOS_OK;
}

W
wangchenyang 已提交
120 121 122 123 124 125 126 127
int VfsProcfsRead(struct file *filep, char *buffer, size_t buflen)
{
    ssize_t size;
    struct ProcDirEntry *entry = NULL;
    if ((filep == NULL) || (filep->f_vnode == NULL) || (buffer == NULL)) {
        return -EINVAL;
    }

Z
zhushengle 已提交
128
    VnodeHold();
W
wangchenyang 已提交
129
    entry = VnodeToEntry(filep->f_vnode);
Z
zhushengle 已提交
130 131 132 133 134 135
    if (entry == NULL) {
        VnodeDrop();
        return -EPERM;
    }

    spin_lock(&entry->pdeUnloadLock);
W
wangchenyang 已提交
136 137
    size = (ssize_t)ReadProcFile(entry, (void *)buffer, buflen);
    filep->f_pos = entry->pf->fPos;
Z
zhushengle 已提交
138 139
    spin_unlock(&entry->pdeUnloadLock);
    VnodeDrop();
W
wangchenyang 已提交
140 141 142
    return size;
}

143 144 145 146 147 148 149 150
int VfsProcfsWrite(struct file *filep, const char *buffer, size_t buflen)
{
    ssize_t size;
    struct ProcDirEntry *entry = NULL;
    if ((filep == NULL) || (filep->f_vnode == NULL) || (buffer == NULL)) {
        return -EINVAL;
    }

Z
zhushengle 已提交
151
    VnodeHold();
152
    entry = VnodeToEntry(filep->f_vnode);
Z
zhushengle 已提交
153 154 155 156 157 158
    if (entry == NULL) {
        VnodeDrop();
        return -EPERM;
    }

    spin_lock(&entry->pdeUnloadLock);
159 160
    size = (ssize_t)WriteProcFile(entry, (void *)buffer, buflen);
    filep->f_pos = entry->pf->fPos;
Z
zhushengle 已提交
161 162
    spin_unlock(&entry->pdeUnloadLock);
    VnodeDrop();
163 164 165
    return size;
}

W
wangchenyang 已提交
166 167 168 169 170 171 172 173 174
int VfsProcfsLookup(struct Vnode *parent, const char *name, int len, struct Vnode **vpp)
{
    if (parent == NULL || name == NULL || len <= 0 || vpp == NULL) {
        return -EINVAL;
    }
    struct ProcDirEntry *entry = VnodeToEntry(parent);
    if (entry == NULL) {
        return -ENODATA;
    }
Z
zhushengle 已提交
175 176

    spin_lock(&procfsLock);
W
wangchenyang 已提交
177 178 179
    entry = entry->subdir;
    while (1) {
        if (entry == NULL) {
Z
zhushengle 已提交
180
            spin_unlock(&procfsLock);
W
wangchenyang 已提交
181 182 183 184 185 186 187
            return -ENOENT;
        }
        if (EntryMatch(name, len, entry)) {
            break;
        }
        entry = entry->next;
    }
Z
zhushengle 已提交
188
    spin_unlock(&procfsLock);
W
wangchenyang 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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

    *vpp = EntryToVnode(entry);
    if ((*vpp) == NULL) {
        return -ENOMEM;
    }
    (*vpp)->originMount = parent->originMount;
    (*vpp)->parent = parent;
    return LOS_OK;
}

int VfsProcfsMount(struct Mount *mnt, struct Vnode *device, const void *data)
{
    struct Vnode *vp = NULL;
    int ret;

    spin_lock_init(&procfsLock);
    procfsInit = true;

    ret = VnodeAlloc(&g_procfsVops, &vp);
    if (ret != 0) {
        return -ENOMEM;
    }

    struct ProcDirEntry *root = GetProcRootEntry();
    vp->data = root;
    vp->originMount = mnt;
    vp->fop = &g_procfsFops;
    mnt->data = NULL;
    mnt->vnodeCovered = vp;
    vp->type = root->type;
    if (vp->type == VNODE_TYPE_DIR) {
        vp->mode = S_IFDIR | PROCFS_DEFAULT_MODE;
    } else {
        vp->mode = S_IFREG | PROCFS_DEFAULT_MODE;
    }

    return LOS_OK;
}

int VfsProcfsUnmount(void *handle, struct Vnode **blkdriver)
{
    (void)handle;
    (void)blkdriver;
    return -EPERM;
}

int VfsProcfsStat(struct Vnode *node, struct stat *buf)
{
Z
zhushengle 已提交
237
    VnodeHold();
W
wangchenyang 已提交
238
    struct ProcDirEntry *entry = VnodeToEntry(node);
Z
zhushengle 已提交
239 240 241 242
    if (entry == NULL) {
        VnodeDrop();
        return -EPERM;
    }
W
wangchenyang 已提交
243
    (void)memset_s(buf, sizeof(struct stat), 0, sizeof(struct stat));
Z
zhushengle 已提交
244
    spin_lock(&entry->pdeUnloadLock);
W
wangchenyang 已提交
245
    buf->st_mode = entry->mode;
Z
zhushengle 已提交
246 247
    spin_unlock(&entry->pdeUnloadLock);
    VnodeDrop();
W
wangchenyang 已提交
248 249 250 251 252 253 254
    return LOS_OK;
}

int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
{
    int result;
    char *buffer = NULL;
Z
zhushengle 已提交
255
    unsigned int minSize, dstNameSize;
W
wangchenyang 已提交
256 257 258 259 260 261 262 263 264
    struct ProcDirEntry *pde = NULL;
    int i = 0;

    if (dir == NULL) {
        return -EINVAL;
    }
    if (node->type != VNODE_TYPE_DIR) {
        return -ENOTDIR;
    }
Z
zhushengle 已提交
265
    VnodeHold();
W
wangchenyang 已提交
266
    pde = VnodeToEntry(node);
Z
zhushengle 已提交
267 268 269 270
    if (pde == NULL) {
        VnodeDrop();
        return -EPERM;
    }
W
wangchenyang 已提交
271

Z
zhushengle 已提交
272
    spin_lock(&pde->pdeUnloadLock);
M
mucor 已提交
273
    while (i < dir->read_cnt) {
W
wangchenyang 已提交
274 275
        buffer = (char *)zalloc(sizeof(char) * NAME_MAX);
        if (buffer == NULL) {
Z
zhushengle 已提交
276 277
            spin_unlock(&pde->pdeUnloadLock);
            VnodeDrop();
W
wangchenyang 已提交
278 279 280 281
            PRINT_ERR("malloc failed\n");
            return -ENOMEM;
        }

Z
zhushengle 已提交
282
        result = ReadProcFile(pde, (void *)buffer, NAME_MAX);
W
wangchenyang 已提交
283 284 285 286
        if (result != ENOERR) {
            free(buffer);
            break;
        }
Z
zhushengle 已提交
287 288 289
        dstNameSize = sizeof(dir->fd_dir[i].d_name);
        minSize = (dstNameSize < NAME_MAX) ? dstNameSize : NAME_MAX;
        result = strncpy_s(dir->fd_dir[i].d_name, dstNameSize, buffer, minSize);
W
wangchenyang 已提交
290
        if (result != EOK) {
Z
zhushengle 已提交
291 292
            spin_unlock(&pde->pdeUnloadLock);
            VnodeDrop();
W
wangchenyang 已提交
293 294 295
            free(buffer);
            return -ENAMETOOLONG;
        }
Z
zhushengle 已提交
296
        dir->fd_dir[i].d_name[dstNameSize - 1] = '\0';
W
wangchenyang 已提交
297 298 299 300 301 302 303
        dir->fd_position++;
        dir->fd_dir[i].d_off = dir->fd_position;
        dir->fd_dir[i].d_reclen = (uint16_t)sizeof(struct dirent);

        i++;
        free(buffer);
    }
Z
zhushengle 已提交
304 305
    spin_unlock(&pde->pdeUnloadLock);
    VnodeDrop();
W
wangchenyang 已提交
306 307 308 309 310
    return i;
}

int VfsProcfsOpendir(struct Vnode *node,  struct fs_dirent_s *dir)
{
Z
zhushengle 已提交
311
    VnodeHold();
W
wangchenyang 已提交
312 313
    struct ProcDirEntry *pde = VnodeToEntry(node);
    if (pde == NULL) {
Z
zhushengle 已提交
314
        VnodeDrop();
W
wangchenyang 已提交
315 316
        return -EINVAL;
    }
Z
zhushengle 已提交
317 318

    spin_lock(&pde->pdeUnloadLock);
W
wangchenyang 已提交
319
    pde->pdirCurrent = pde->subdir;
320
    if (pde->pf == NULL) {
Z
zhushengle 已提交
321 322
        spin_unlock(&pde->pdeUnloadLock);
        VnodeDrop();
323 324
        return -EINVAL;
    }
W
wangchenyang 已提交
325
    pde->pf->fPos = 0;
Z
zhushengle 已提交
326 327
    spin_unlock(&pde->pdeUnloadLock);
    VnodeDrop();
W
wangchenyang 已提交
328 329 330 331 332 333 334 335
    return LOS_OK;
}

int VfsProcfsOpen(struct file *filep)
{
    if (filep == NULL) {
        return -EINVAL;
    }
Z
zhushengle 已提交
336
    VnodeHold();
W
wangchenyang 已提交
337 338
    struct Vnode *node = filep->f_vnode;
    struct ProcDirEntry *pde = VnodeToEntry(node);
Z
zhushengle 已提交
339 340 341 342 343 344
    if (pde == NULL) {
        VnodeDrop();
        return -EPERM;
    }

    spin_lock(&pde->pdeUnloadLock);
W
wangchenyang 已提交
345
    if (ProcOpen(pde->pf) != OK) {
Z
zhushengle 已提交
346
        spin_unlock(&pde->pdeUnloadLock);
W
wangchenyang 已提交
347 348
        return -ENOMEM;
    }
Z
zhangfanfan2 已提交
349 350 351 352 353 354 355
    if (S_ISREG(pde->mode) && (pde->procFileOps != NULL) && (pde->procFileOps->open != NULL)) {
        (void)pde->procFileOps->open((struct Vnode *)pde, pde->pf);
    }
    if (S_ISDIR(pde->mode)) {
        pde->pdirCurrent = pde->subdir;
        pde->pf->fPos = 0;
    }
W
wangchenyang 已提交
356
    filep->f_priv = (void *)pde;
Z
zhushengle 已提交
357 358
    spin_unlock(&pde->pdeUnloadLock);
    VnodeDrop();
W
wangchenyang 已提交
359 360 361 362 363 364 365 366 367
    return LOS_OK;
}

int VfsProcfsClose(struct file *filep)
{
    int result = 0;
    if (filep == NULL) {
        return -EINVAL;
    }
Z
zhushengle 已提交
368 369

    VnodeHold();
W
wangchenyang 已提交
370 371
    struct Vnode *node = filep->f_vnode;
    struct ProcDirEntry *pde = VnodeToEntry(node);
Z
zhushengle 已提交
372 373 374 375 376 377
    if (pde == NULL) {
        VnodeDrop();
        return -EPERM;
    }

    spin_lock(&pde->pdeUnloadLock);
W
wangchenyang 已提交
378 379 380 381 382 383
    pde->pf->fPos = 0;
    if ((pde->procFileOps != NULL) && (pde->procFileOps->release != NULL)) {
        result = pde->procFileOps->release((struct Vnode *)pde, pde->pf);
    }
    LosBufRelease(pde->pf->sbuf);
    pde->pf->sbuf = NULL;
Z
zhushengle 已提交
384 385
    spin_unlock(&pde->pdeUnloadLock);
    VnodeDrop();
W
wangchenyang 已提交
386 387 388
    return result;
}

C
chenjing 已提交
389 390 391 392 393 394 395 396
int VfsProcfsStatfs(struct Mount *mnt, struct statfs *buf)
{
    (void)memset_s(buf, sizeof(struct statfs), 0, sizeof(struct statfs));
    buf->f_type = PROCFS_MAGIC;

    return LOS_OK;
}

C
chenjing 已提交
397 398 399 400 401
int VfsProcfsClosedir(struct Vnode *vp, struct fs_dirent_s *dir)
{
    return LOS_OK;
}

402 403 404 405 406 407 408 409
ssize_t VfsProcfsReadlink(struct Vnode *vnode, char *buffer, size_t bufLen)
{
    int result = -EINVAL;
    if (vnode == NULL) {
        return result;
    }

    struct ProcDirEntry *pde = VnodeToEntry(vnode);
Z
zhushengle 已提交
410 411 412 413 414
    if (pde == NULL) {
        return -EPERM;
    }

    spin_lock(&pde->pdeUnloadLock);
415 416 417
    if ((pde->procFileOps != NULL) && (pde->procFileOps->readLink != NULL)) {
        result = pde->procFileOps->readLink(pde, buffer, bufLen);
    }
Z
zhushengle 已提交
418
    spin_unlock(&pde->pdeUnloadLock);
419 420 421
    return result;
}

W
wangchenyang 已提交
422 423 424
const struct MountOps procfs_operations = {
    .Mount = VfsProcfsMount,
    .Unmount = NULL,
C
chenjing 已提交
425
    .Statfs = VfsProcfsStatfs,
W
wangchenyang 已提交
426 427 428 429 430 431 432
};

static struct VnodeOps g_procfsVops = {
    .Lookup = VfsProcfsLookup,
    .Getattr = VfsProcfsStat,
    .Readdir = VfsProcfsReaddir,
    .Opendir = VfsProcfsOpendir,
433
    .Closedir = VfsProcfsClosedir,
434 435
    .Truncate = VfsProcfsTruncate,
    .Readlink = VfsProcfsReadlink,
W
wangchenyang 已提交
436 437 438 439
};

static struct file_operations_vfs g_procfsFops = {
    .read = VfsProcfsRead,
440
    .write = VfsProcfsWrite,
W
wangchenyang 已提交
441 442 443 444 445 446
    .open = VfsProcfsOpen,
    .close = VfsProcfsClose
};

FSMAP_ENTRY(procfs_fsmap, "procfs", procfs_operations, FALSE, FALSE);
#endif