rem: err = EBADF;
add: err = FS_EBADF;
context: new_fd(bool kernel, int nfd, ofile *f, int fd, bool coe){    int         i, j, num, end;    fdarray     *fds;    ofile       *of;    int         err;    long        cnt;    if (kernel)        fds = global_fds;    else        fds = get_cur_ioctx()->fds;    LOCK(fds->lock);    num = fds->num;    if (!f) {        if ((fd < 0) || (fd >= num)) {            err = EBADF;            goto error1;        }        f = fds->fds[fd];        if (!f) {            err = EBADF;            goto error1;        }    }    atomic_add(&f->rcnt, 1);    atomic_add(&f->ocnt, 1);    if (nfd >= 0) {        if (nfd >= num) {            err = EBADF;            goto error2;        }        of = fds->fds[nfd];        fds->fds[nfd] = f;        SETBIT(fds->alloc, nfd, TRUE);        SETBIT(fds->coes, nfd, coe);        UNLOCK(fds->lock);        if (of) {            cnt = atomic_add(&of->ocnt, -1);            if (cnt == 1)                invoke_close(of);            cnt = atomic_add(&of->rcnt, -1);            if (cnt == 1)                invoke_free(of);        }        return nfd;    }    end = num & ~31;    for(j=0; j<end; j+=32)        if (fds->alloc[j/32] != 0xffffffff)            for(i=j; i<j+32; i++)                if (!GETBIT(fds->alloc, i))                    goto found;    for(i=end; i<num; i++)        if (!GETBIT(fds->alloc, i))            goto found;    err = EMFILE;    goto error2;found:    SETBIT(fds->alloc, i, 1);    fds->fds[i] = f;    SETBIT(fds->coes, i, coe);    UNLOCK(fds->lock);    return i;error2:    atomic_add(&f->rcnt, -1);    atomic_add(&f->ocnt, -1);error1:    UNLOCK(fds->lock);    return err;}