proc_vfs.c 10.6 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 48 49 50 51 52 53 54 55 56 57 58 59 60
#include "los_tables.h"
#include "internal.h"

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

static struct ProcDirEntry *VnodeToEntry(struct Vnode *node)
{
    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
    if (entry == NULL) {
        VnodeDrop();
        return -EPERM;
    }

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

141 142 143 144 145 146 147 148
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 已提交
149
    VnodeHold();
150
    entry = VnodeToEntry(filep->f_vnode);
Z
zhushengle 已提交
151 152 153 154 155
    if (entry == NULL) {
        VnodeDrop();
        return -EPERM;
    }

156 157
    size = (ssize_t)WriteProcFile(entry, (void *)buffer, buflen);
    filep->f_pos = entry->pf->fPos;
Z
zhushengle 已提交
158
    VnodeDrop();
159 160 161
    return size;
}

W
wangchenyang 已提交
162 163 164 165 166 167 168 169 170
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 已提交
171

W
wangchenyang 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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
    entry = entry->subdir;
    while (1) {
        if (entry == NULL) {
            return -ENOENT;
        }
        if (EntryMatch(name, len, entry)) {
            break;
        }
        entry = entry->next;
    }

    *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 已提交
230
    VnodeHold();
W
wangchenyang 已提交
231
    struct ProcDirEntry *entry = VnodeToEntry(node);
Z
zhushengle 已提交
232 233 234 235
    if (entry == NULL) {
        VnodeDrop();
        return -EPERM;
    }
W
wangchenyang 已提交
236 237
    (void)memset_s(buf, sizeof(struct stat), 0, sizeof(struct stat));
    buf->st_mode = entry->mode;
Z
zhushengle 已提交
238
    VnodeDrop();
W
wangchenyang 已提交
239 240 241 242 243 244 245
    return LOS_OK;
}

int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
{
    int result;
    char *buffer = NULL;
Z
zhushengle 已提交
246
    unsigned int minSize, dstNameSize;
W
wangchenyang 已提交
247 248 249 250 251 252 253 254 255
    struct ProcDirEntry *pde = NULL;
    int i = 0;

    if (dir == NULL) {
        return -EINVAL;
    }
    if (node->type != VNODE_TYPE_DIR) {
        return -ENOTDIR;
    }
Z
zhushengle 已提交
256
    VnodeHold();
W
wangchenyang 已提交
257
    pde = VnodeToEntry(node);
Z
zhushengle 已提交
258 259 260 261
    if (pde == NULL) {
        VnodeDrop();
        return -EPERM;
    }
W
wangchenyang 已提交
262

M
mucor 已提交
263
    while (i < dir->read_cnt) {
W
wangchenyang 已提交
264 265
        buffer = (char *)zalloc(sizeof(char) * NAME_MAX);
        if (buffer == NULL) {
Z
zhushengle 已提交
266
            VnodeDrop();
W
wangchenyang 已提交
267 268 269 270
            PRINT_ERR("malloc failed\n");
            return -ENOMEM;
        }

Z
zhushengle 已提交
271
        result = ReadProcFile(pde, (void *)buffer, NAME_MAX);
W
wangchenyang 已提交
272 273 274 275
        if (result != ENOERR) {
            free(buffer);
            break;
        }
Z
zhushengle 已提交
276 277 278
        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 已提交
279
        if (result != EOK) {
Z
zhushengle 已提交
280
            VnodeDrop();
W
wangchenyang 已提交
281 282 283
            free(buffer);
            return -ENAMETOOLONG;
        }
Z
zhushengle 已提交
284
        dir->fd_dir[i].d_name[dstNameSize - 1] = '\0';
W
wangchenyang 已提交
285 286 287 288 289 290 291
        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 已提交
292
    VnodeDrop();
W
wangchenyang 已提交
293 294 295 296 297
    return i;
}

int VfsProcfsOpendir(struct Vnode *node,  struct fs_dirent_s *dir)
{
Z
zhushengle 已提交
298
    VnodeHold();
W
wangchenyang 已提交
299 300
    struct ProcDirEntry *pde = VnodeToEntry(node);
    if (pde == NULL) {
Z
zhushengle 已提交
301
        VnodeDrop();
W
wangchenyang 已提交
302 303
        return -EINVAL;
    }
Z
zhushengle 已提交
304

W
wangchenyang 已提交
305
    pde->pdirCurrent = pde->subdir;
306
    if (pde->pf == NULL) {
Z
zhushengle 已提交
307
        VnodeDrop();
308 309
        return -EINVAL;
    }
W
wangchenyang 已提交
310
    pde->pf->fPos = 0;
Z
zhushengle 已提交
311
    VnodeDrop();
W
wangchenyang 已提交
312 313 314 315 316 317 318 319
    return LOS_OK;
}

int VfsProcfsOpen(struct file *filep)
{
    if (filep == NULL) {
        return -EINVAL;
    }
Z
zhushengle 已提交
320
    VnodeHold();
W
wangchenyang 已提交
321 322
    struct Vnode *node = filep->f_vnode;
    struct ProcDirEntry *pde = VnodeToEntry(node);
Z
zhushengle 已提交
323 324 325 326 327
    if (pde == NULL) {
        VnodeDrop();
        return -EPERM;
    }

W
wangchenyang 已提交
328 329 330
    if (ProcOpen(pde->pf) != OK) {
        return -ENOMEM;
    }
Z
zhangfanfan2 已提交
331 332 333 334 335 336 337
    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 已提交
338
    filep->f_priv = (void *)pde;
Z
zhushengle 已提交
339
    VnodeDrop();
W
wangchenyang 已提交
340 341 342 343 344 345 346 347 348
    return LOS_OK;
}

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

    VnodeHold();
W
wangchenyang 已提交
351 352
    struct Vnode *node = filep->f_vnode;
    struct ProcDirEntry *pde = VnodeToEntry(node);
Z
zhushengle 已提交
353 354 355 356 357
    if (pde == NULL) {
        VnodeDrop();
        return -EPERM;
    }

W
wangchenyang 已提交
358 359 360 361 362 363
    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 已提交
364
    VnodeDrop();
W
wangchenyang 已提交
365 366 367
    return result;
}

C
chenjing 已提交
368 369 370 371 372 373 374 375
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 已提交
376 377 378 379 380
int VfsProcfsClosedir(struct Vnode *vp, struct fs_dirent_s *dir)
{
    return LOS_OK;
}

381 382 383 384 385 386 387 388
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 已提交
389 390 391 392
    if (pde == NULL) {
        return -EPERM;
    }

393 394 395 396 397 398
    if ((pde->procFileOps != NULL) && (pde->procFileOps->readLink != NULL)) {
        result = pde->procFileOps->readLink(pde, buffer, bufLen);
    }
    return result;
}

W
wangchenyang 已提交
399 400 401
const struct MountOps procfs_operations = {
    .Mount = VfsProcfsMount,
    .Unmount = NULL,
C
chenjing 已提交
402
    .Statfs = VfsProcfsStatfs,
W
wangchenyang 已提交
403 404 405 406 407 408 409
};

static struct VnodeOps g_procfsVops = {
    .Lookup = VfsProcfsLookup,
    .Getattr = VfsProcfsStat,
    .Readdir = VfsProcfsReaddir,
    .Opendir = VfsProcfsOpendir,
410
    .Closedir = VfsProcfsClosedir,
411 412
    .Truncate = VfsProcfsTruncate,
    .Readlink = VfsProcfsReadlink,
W
wangchenyang 已提交
413 414 415 416
};

static struct file_operations_vfs g_procfsFops = {
    .read = VfsProcfsRead,
417
    .write = VfsProcfsWrite,
W
wangchenyang 已提交
418 419 420 421 422 423
    .open = VfsProcfsOpen,
    .close = VfsProcfsClose
};

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