clang-format
This commit is contained in:
@@ -43,33 +43,29 @@
|
||||
* Read the directory entry out of slot SLOT of a directory vnode.
|
||||
* The "slot" is the index of the directory entry, starting at 0.
|
||||
*/
|
||||
static
|
||||
int
|
||||
sfs_readdir(struct sfs_vnode *sv, int slot, struct sfs_direntry *sd)
|
||||
{
|
||||
off_t actualpos;
|
||||
static int sfs_readdir(struct sfs_vnode *sv, int slot,
|
||||
struct sfs_direntry *sd) {
|
||||
off_t actualpos;
|
||||
|
||||
/* Compute the actual position in the directory to read. */
|
||||
actualpos = slot * sizeof(struct sfs_direntry);
|
||||
/* Compute the actual position in the directory to read. */
|
||||
actualpos = slot * sizeof(struct sfs_direntry);
|
||||
|
||||
return sfs_metaio(sv, actualpos, sd, sizeof(*sd), UIO_READ);
|
||||
return sfs_metaio(sv, actualpos, sd, sizeof(*sd), UIO_READ);
|
||||
}
|
||||
|
||||
/*
|
||||
* Write (overwrite) the directory entry in slot SLOT of a directory
|
||||
* vnode.
|
||||
*/
|
||||
static
|
||||
int
|
||||
sfs_writedir(struct sfs_vnode *sv, int slot, struct sfs_direntry *sd)
|
||||
{
|
||||
off_t actualpos;
|
||||
static int sfs_writedir(struct sfs_vnode *sv, int slot,
|
||||
struct sfs_direntry *sd) {
|
||||
off_t actualpos;
|
||||
|
||||
/* Compute the actual position in the directory. */
|
||||
KASSERT(slot>=0);
|
||||
actualpos = slot * sizeof(struct sfs_direntry);
|
||||
/* Compute the actual position in the directory. */
|
||||
KASSERT(slot >= 0);
|
||||
actualpos = slot * sizeof(struct sfs_direntry);
|
||||
|
||||
return sfs_metaio(sv, actualpos, sd, sizeof(*sd), UIO_WRITE);
|
||||
return sfs_metaio(sv, actualpos, sd, sizeof(*sd), UIO_WRITE);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -77,22 +73,19 @@ sfs_writedir(struct sfs_vnode *sv, int slot, struct sfs_direntry *sd)
|
||||
* This actually computes the number of existing slots, and does not
|
||||
* account for empty slots.
|
||||
*/
|
||||
static
|
||||
int
|
||||
sfs_dir_nentries(struct sfs_vnode *sv)
|
||||
{
|
||||
struct sfs_fs *sfs = sv->sv_absvn.vn_fs->fs_data;
|
||||
off_t size;
|
||||
static int sfs_dir_nentries(struct sfs_vnode *sv) {
|
||||
struct sfs_fs *sfs = sv->sv_absvn.vn_fs->fs_data;
|
||||
off_t size;
|
||||
|
||||
KASSERT(sv->sv_i.sfi_type == SFS_TYPE_DIR);
|
||||
KASSERT(sv->sv_i.sfi_type == SFS_TYPE_DIR);
|
||||
|
||||
size = sv->sv_i.sfi_size;
|
||||
if (size % sizeof(struct sfs_direntry) != 0) {
|
||||
panic("sfs: %s: directory %u: Invalid size %llu\n",
|
||||
sfs->sfs_sb.sb_volname, sv->sv_ino, size);
|
||||
}
|
||||
size = sv->sv_i.sfi_size;
|
||||
if (size % sizeof(struct sfs_direntry) != 0) {
|
||||
panic("sfs: %s: directory %u: Invalid size %llu\n", sfs->sfs_sb.sb_volname,
|
||||
sv->sv_ino, size);
|
||||
}
|
||||
|
||||
return size / sizeof(struct sfs_direntry);
|
||||
return size / sizeof(struct sfs_direntry);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -100,140 +93,130 @@ sfs_dir_nentries(struct sfs_vnode *sv)
|
||||
* return its inode number, its slot, and/or the slot number of an
|
||||
* empty directory slot if one is found.
|
||||
*/
|
||||
int
|
||||
sfs_dir_findname(struct sfs_vnode *sv, const char *name,
|
||||
uint32_t *ino, int *slot, int *emptyslot)
|
||||
{
|
||||
struct sfs_direntry tsd;
|
||||
int found, nentries, i, result;
|
||||
int sfs_dir_findname(struct sfs_vnode *sv, const char *name, uint32_t *ino,
|
||||
int *slot, int *emptyslot) {
|
||||
struct sfs_direntry tsd;
|
||||
int found, nentries, i, result;
|
||||
|
||||
nentries = sfs_dir_nentries(sv);
|
||||
nentries = sfs_dir_nentries(sv);
|
||||
|
||||
/* For each slot... */
|
||||
found = 0;
|
||||
for (i=0; i<nentries; i++) {
|
||||
/* For each slot... */
|
||||
found = 0;
|
||||
for (i = 0; i < nentries; i++) {
|
||||
|
||||
/* Read the entry from that slot */
|
||||
result = sfs_readdir(sv, i, &tsd);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
if (tsd.sfd_ino == SFS_NOINO) {
|
||||
/* Free slot - report it back if one was requested */
|
||||
if (emptyslot != NULL) {
|
||||
*emptyslot = i;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Ensure null termination, just in case */
|
||||
tsd.sfd_name[sizeof(tsd.sfd_name)-1] = 0;
|
||||
if (!strcmp(tsd.sfd_name, name)) {
|
||||
/* Read the entry from that slot */
|
||||
result = sfs_readdir(sv, i, &tsd);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
if (tsd.sfd_ino == SFS_NOINO) {
|
||||
/* Free slot - report it back if one was requested */
|
||||
if (emptyslot != NULL) {
|
||||
*emptyslot = i;
|
||||
}
|
||||
} else {
|
||||
/* Ensure null termination, just in case */
|
||||
tsd.sfd_name[sizeof(tsd.sfd_name) - 1] = 0;
|
||||
if (!strcmp(tsd.sfd_name, name)) {
|
||||
|
||||
/* Each name may legally appear only once... */
|
||||
KASSERT(found==0);
|
||||
/* Each name may legally appear only once... */
|
||||
KASSERT(found == 0);
|
||||
|
||||
found = 1;
|
||||
if (slot != NULL) {
|
||||
*slot = i;
|
||||
}
|
||||
if (ino != NULL) {
|
||||
*ino = tsd.sfd_ino;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
found = 1;
|
||||
if (slot != NULL) {
|
||||
*slot = i;
|
||||
}
|
||||
if (ino != NULL) {
|
||||
*ino = tsd.sfd_ino;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found ? 0 : ENOENT;
|
||||
return found ? 0 : ENOENT;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a link in a directory to the specified inode by number, with
|
||||
* the specified name, and optionally hand back the slot.
|
||||
*/
|
||||
int
|
||||
sfs_dir_link(struct sfs_vnode *sv, const char *name, uint32_t ino, int *slot)
|
||||
{
|
||||
int emptyslot = -1;
|
||||
int result;
|
||||
struct sfs_direntry sd;
|
||||
int sfs_dir_link(struct sfs_vnode *sv, const char *name, uint32_t ino,
|
||||
int *slot) {
|
||||
int emptyslot = -1;
|
||||
int result;
|
||||
struct sfs_direntry sd;
|
||||
|
||||
/* Look up the name. We want to make sure it *doesn't* exist. */
|
||||
result = sfs_dir_findname(sv, name, NULL, NULL, &emptyslot);
|
||||
if (result!=0 && result!=ENOENT) {
|
||||
return result;
|
||||
}
|
||||
if (result==0) {
|
||||
return EEXIST;
|
||||
}
|
||||
/* Look up the name. We want to make sure it *doesn't* exist. */
|
||||
result = sfs_dir_findname(sv, name, NULL, NULL, &emptyslot);
|
||||
if (result != 0 && result != ENOENT) {
|
||||
return result;
|
||||
}
|
||||
if (result == 0) {
|
||||
return EEXIST;
|
||||
}
|
||||
|
||||
if (strlen(name)+1 > sizeof(sd.sfd_name)) {
|
||||
return ENAMETOOLONG;
|
||||
}
|
||||
if (strlen(name) + 1 > sizeof(sd.sfd_name)) {
|
||||
return ENAMETOOLONG;
|
||||
}
|
||||
|
||||
/* If we didn't get an empty slot, add the entry at the end. */
|
||||
if (emptyslot < 0) {
|
||||
emptyslot = sfs_dir_nentries(sv);
|
||||
}
|
||||
/* If we didn't get an empty slot, add the entry at the end. */
|
||||
if (emptyslot < 0) {
|
||||
emptyslot = sfs_dir_nentries(sv);
|
||||
}
|
||||
|
||||
/* Set up the entry. */
|
||||
bzero(&sd, sizeof(sd));
|
||||
sd.sfd_ino = ino;
|
||||
strcpy(sd.sfd_name, name);
|
||||
/* Set up the entry. */
|
||||
bzero(&sd, sizeof(sd));
|
||||
sd.sfd_ino = ino;
|
||||
strcpy(sd.sfd_name, name);
|
||||
|
||||
/* Hand back the slot, if so requested. */
|
||||
if (slot) {
|
||||
*slot = emptyslot;
|
||||
}
|
||||
/* Hand back the slot, if so requested. */
|
||||
if (slot) {
|
||||
*slot = emptyslot;
|
||||
}
|
||||
|
||||
/* Write the entry. */
|
||||
return sfs_writedir(sv, emptyslot, &sd);
|
||||
/* Write the entry. */
|
||||
return sfs_writedir(sv, emptyslot, &sd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unlink a name in a directory, by slot number.
|
||||
*/
|
||||
int
|
||||
sfs_dir_unlink(struct sfs_vnode *sv, int slot)
|
||||
{
|
||||
struct sfs_direntry sd;
|
||||
int sfs_dir_unlink(struct sfs_vnode *sv, int slot) {
|
||||
struct sfs_direntry sd;
|
||||
|
||||
/* Initialize a suitable directory entry... */
|
||||
bzero(&sd, sizeof(sd));
|
||||
sd.sfd_ino = SFS_NOINO;
|
||||
/* Initialize a suitable directory entry... */
|
||||
bzero(&sd, sizeof(sd));
|
||||
sd.sfd_ino = SFS_NOINO;
|
||||
|
||||
/* ... and write it */
|
||||
return sfs_writedir(sv, slot, &sd);
|
||||
/* ... and write it */
|
||||
return sfs_writedir(sv, slot, &sd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Look for a name in a directory and hand back a vnode for the
|
||||
* file, if there is one.
|
||||
*/
|
||||
int
|
||||
sfs_lookonce(struct sfs_vnode *sv, const char *name,
|
||||
struct sfs_vnode **ret,
|
||||
int *slot)
|
||||
{
|
||||
struct sfs_fs *sfs = sv->sv_absvn.vn_fs->fs_data;
|
||||
uint32_t ino;
|
||||
int result;
|
||||
int sfs_lookonce(struct sfs_vnode *sv, const char *name, struct sfs_vnode **ret,
|
||||
int *slot) {
|
||||
struct sfs_fs *sfs = sv->sv_absvn.vn_fs->fs_data;
|
||||
uint32_t ino;
|
||||
int result;
|
||||
|
||||
result = sfs_dir_findname(sv, name, &ino, slot, NULL);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
result = sfs_dir_findname(sv, name, &ino, slot, NULL);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = sfs_loadvnode(sfs, ino, SFS_TYPE_INVAL, ret);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
result = sfs_loadvnode(sfs, ino, SFS_TYPE_INVAL, ret);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if ((*ret)->sv_i.sfi_linkcount == 0) {
|
||||
panic("sfs: %s: name %s (inode %u) in dir %u has "
|
||||
"linkcount 0\n", sfs->sfs_sb.sb_volname,
|
||||
name, (*ret)->sv_ino, sv->sv_ino);
|
||||
}
|
||||
if ((*ret)->sv_i.sfi_linkcount == 0) {
|
||||
panic("sfs: %s: name %s (inode %u) in dir %u has "
|
||||
"linkcount 0\n",
|
||||
sfs->sfs_sb.sb_volname, name, (*ret)->sv_ino, sv->sv_ino);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user