aboutsummaryrefslogtreecommitdiffstats
path: root/amd/readdir.c
diff options
context:
space:
mode:
Diffstat (limited to 'amd/readdir.c')
-rw-r--r--amd/readdir.c528
1 files changed, 485 insertions, 43 deletions
diff --git a/amd/readdir.c b/amd/readdir.c
index 49f89943cfb4..4b5e61b3d56d 100644
--- a/amd/readdir.c
+++ b/amd/readdir.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997-2006 Erez Zadok
+ * Copyright (c) 1997-2014 Erez Zadok
* Copyright (c) 1990 Jan-Simon Pendry
* Copyright (c) 1990 Imperial College of Science, Technology & Medicine
* Copyright (c) 1990 The Regents of the University of California.
@@ -16,11 +16,7 @@
* 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. All advertising materials mentioning features or use of this software
- * must display the following acknowledgment:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
@@ -109,14 +105,14 @@ make_entry_chain(am_node *mp, const nfsentry *current_chain, int fully_browsable
plog(XLOG_DEBUG, "make_entry_chain: mp is (NULL)");
return retval;
}
- mf = mp->am_mnt;
+ mf = mp->am_al->al_mnt;
if (!mf) {
- plog(XLOG_DEBUG, "make_entry_chain: mp->am_mnt is (NULL)");
+ plog(XLOG_DEBUG, "make_entry_chain: mp->am_al->al_mnt is (NULL)");
return retval;
}
mmp = (mnt_map *) mf->mf_private;
if (!mmp) {
- plog(XLOG_DEBUG, "make_entry_chain: mp->am_mnt->mf_private is (NULL)");
+ plog(XLOG_DEBUG, "make_entry_chain: mp->am_al->al_mnt->mf_private is (NULL)");
return retval;
}
@@ -171,7 +167,7 @@ make_entry_chain(am_node *mp, const nfsentry *current_chain, int fully_browsable
/* out of space */
plog(XLOG_DEBUG, "make_entry_chain: no more space in chain");
if (num_entries > 0) {
- chain[num_entries - 1].ne_nextentry = 0;
+ chain[num_entries - 1].ne_nextentry = NULL;
retval = &chain[0];
}
return retval;
@@ -192,7 +188,7 @@ make_entry_chain(am_node *mp, const nfsentry *current_chain, int fully_browsable
/* terminate chain */
if (num_entries > 0) {
- chain[num_entries - 1].ne_nextentry = 0;
+ chain[num_entries - 1].ne_nextentry = NULL;
retval = &chain[0];
}
@@ -225,7 +221,7 @@ amfs_readdir_browsable(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *
* to be returned in a single packet. If it isn't (which would be
* fairly unbelievable) then tough.
*/
- dlog("amfs_readdir_browsable: default search");
+ dlog("%s: default search", __func__);
/*
* Check for enough room. This is extremely approximate but is more
* than enough space. Really need 2 times:
@@ -264,8 +260,8 @@ amfs_readdir_browsable(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *
ep[1].ne_fileid = mp->am_gen;
ep[1].ne_name = "..";
- ep[1].ne_nextentry = 0;
- *(u_int *) ep[1].ne_cookie = DOT_DOT_COOKIE;
+ ep[1].ne_nextentry = NULL;
+ (void)memcpy(ep[1].ne_cookie, &dotdotcookie, sizeof(dotdotcookie));
/*
* If map is browsable, call a function make_entry_chain() to construct
@@ -313,12 +309,12 @@ amfs_readdir_browsable(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *
return 0;
} /* end of "if (gen == 0)" statement */
- dlog("amfs_readdir_browsable: real child");
+ dlog("%s: real child", __func__);
if (gen == DOT_DOT_COOKIE) {
- dlog("amfs_readdir_browsable: End of readdir in %s", mp->am_path);
+ dlog("%s: End of readdir in %s", __func__, mp->am_path);
dp->dl_eof = TRUE;
- dp->dl_entries = 0;
+ dp->dl_entries = NULL;
return 0;
}
@@ -332,7 +328,7 @@ amfs_readdir_browsable(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *
te = te_next; /* reset 'te' from last saved te_next */
if (!te) { /* another indicator of end of readdir */
- dp->dl_entries = 0;
+ dp->dl_entries = NULL;
return 0;
}
/*
@@ -365,29 +361,14 @@ amfs_readdir_browsable(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *
return 0;
}
-
-/*
- * This readdir function which call a special version of it that allows
- * browsing if browsable_dirs=yes was set on the map.
- */
-int
-amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep, u_int count)
+static int
+amfs_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep, u_int count)
{
u_int gen = *(u_int *) cookie;
am_node *xp;
- mntent_t mnt;
dp->dl_eof = FALSE; /* assume readdir not done */
- /* check if map is browsable */
- if (mp->am_mnt && mp->am_mnt->mf_mopts) {
- mnt.mnt_opts = mp->am_mnt->mf_mopts;
- if (amu_hasmntopt(&mnt, "fullybrowsable"))
- return amfs_readdir_browsable(mp, cookie, dp, ep, count, TRUE);
- if (amu_hasmntopt(&mnt, "browsable"))
- return amfs_readdir_browsable(mp, cookie, dp, ep, count, FALSE);
- }
-
/* when gen is 0, we start reading from the beginning of the directory */
if (gen == 0) {
/*
@@ -398,7 +379,7 @@ amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep
* to be returned in a single packet. If it isn't (which would be
* fairly unbelievable) then tough.
*/
- dlog("amfs_generic_readdir: default search");
+ dlog("%s: default search", __func__);
/*
* Check for enough room. This is extremely approximate but is more
* than enough space. Really need 2 times:
@@ -407,8 +388,11 @@ amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep
* 4byte name length
* 4byte name
* plus the dirlist structure */
- if (count < (2 * (2 * (sizeof(*ep) + sizeof("..") + 4) + sizeof(*dp))))
+#define NEEDROOM (2 * (2 * (sizeof(*ep) + sizeof("..") + 4) + sizeof(*dp)))
+ if (count < NEEDROOM) {
+ dlog("%s: not enough room %u < %zu", __func__, count, NEEDROOM);
return EINVAL;
+ }
xp = next_nonerror_node(mp->am_child);
dp->dl_entries = ep;
@@ -425,8 +409,9 @@ amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep
else
ep[1].ne_fileid = mp->am_gen;
ep[1].ne_name = "..";
- ep[1].ne_nextentry = 0;
- *(u_int *) ep[1].ne_cookie = (xp ? xp->am_gen : DOT_DOT_COOKIE);
+ ep[1].ne_nextentry = NULL;
+ (void)memcpy(ep[1].ne_cookie, (xp ? &xp->am_gen : &dotdotcookie),
+ sizeof(dotdotcookie));
if (!xp)
dp->dl_eof = TRUE; /* by default assume readdir done */
@@ -443,12 +428,12 @@ amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep
}
return 0;
}
- dlog("amfs_generic_readdir: real child");
+ dlog("%s: real child", __func__);
if (gen == DOT_DOT_COOKIE) {
- dlog("amfs_generic_readdir: End of readdir in %s", mp->am_path);
+ dlog("%s: End of readdir in %s", __func__, mp->am_path);
dp->dl_eof = TRUE;
- dp->dl_entries = 0;
+ dp->dl_entries = NULL;
if (amuDebug(D_READDIR))
plog(XLOG_DEBUG, "end of readdir eof=TRUE, dl_entries=0\n");
return 0;
@@ -491,7 +476,7 @@ amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep
}
} while (todo > 0);
- ep->ne_nextentry = 0;
+ ep->ne_nextentry = NULL;
if (amuDebug(D_READDIR)) {
nfsentry *ne;
@@ -507,3 +492,460 @@ amfs_generic_readdir(am_node *mp, nfscookie cookie, nfsdirlist *dp, nfsentry *ep
}
return ESTALE;
}
+
+/*
+ * Search a chain for an entry with some name.
+ */
+static int
+key_already_in_chain3(char *keyname, const am_entry3 *chain)
+{
+ const am_entry3 *tmpchain = chain;
+
+ while (tmpchain) {
+ if (keyname && tmpchain->name && STREQ(keyname, tmpchain->name))
+ return 1;
+ tmpchain = tmpchain->nextentry;
+ }
+
+ return 0;
+}
+
+/*
+ * Create a chain of entries which are not linked.
+ */
+static am_entry3 *
+make_entry_chain3(am_node *mp, const am_entry3 *current_chain, int fully_browsable)
+{
+ static uint64 last_cookie = (uint64) 2; /* monotonically increasing */
+ static am_entry3 chain[MAX_CHAIN];
+ static int max_entries = MAX_CHAIN;
+ char *key;
+ int num_entries = 0, i;
+ u_int preflen = 0;
+ am_entry3 *retval = (am_entry3 *) NULL;
+ mntfs *mf;
+ mnt_map *mmp;
+
+ if (!mp) {
+ plog(XLOG_DEBUG, "make_entry_chain3: mp is (NULL)");
+ return retval;
+ }
+ mf = mp->am_al->al_mnt;
+ if (!mf) {
+ plog(XLOG_DEBUG, "make_entry_chain3: mp->am_al->al_mnt is (NULL)");
+ return retval;
+ }
+ mmp = (mnt_map *) mf->mf_private;
+ if (!mmp) {
+ plog(XLOG_DEBUG, "make_entry_chain3: mp->am_al->al_mnt->mf_private is (NULL)");
+ return retval;
+ }
+
+ if (mp->am_pref)
+ preflen = strlen(mp->am_pref);
+
+ /* iterate over keys */
+ for (i = 0; i < NKVHASH; i++) {
+ kv *k;
+ for (k = mmp->kvhash[i]; k ; k = k->next) {
+
+ /*
+ * Skip unwanted entries which are either not real entries or
+ * very difficult to interpret (wildcards...) This test needs
+ * lots of improvement. Any takers?
+ */
+ key = k->key;
+ if (!key)
+ continue;
+
+ /* Skip '/defaults' */
+ if (STREQ(key, "/defaults"))
+ continue;
+
+ /* Skip '*' */
+ if (!fully_browsable && strchr(key, '*'))
+ continue;
+
+ /*
+ * If the map has a prefix-string then check if the key starts with
+ * this string, and if it does, skip over this prefix. If it has a
+ * prefix and it doesn't match the start of the key, skip it.
+ */
+ if (preflen) {
+ if (preflen > strlen(key))
+ continue;
+ if (!NSTREQ(key, mp->am_pref, preflen))
+ continue;
+ key += preflen;
+ }
+
+ /* no more '/' are allowed, unless browsable_dirs=full was used */
+ if (!fully_browsable && strchr(key, '/'))
+ continue;
+
+ /* no duplicates allowed */
+ if (key_already_in_chain3(key, current_chain))
+ continue;
+
+ /* fill in a cell and link the entry */
+ if (num_entries >= max_entries) {
+ /* out of space */
+ plog(XLOG_DEBUG, "make_entry_chain3: no more space in chain");
+ if (num_entries > 0) {
+ chain[num_entries - 1].nextentry = NULL;
+ retval = &chain[0];
+ }
+ return retval;
+ }
+
+ /* we have space. put entry in next cell */
+ ++last_cookie;
+ chain[num_entries].fileid = last_cookie;
+ chain[num_entries].cookie = last_cookie;
+ chain[num_entries].name = key;
+ if (num_entries < max_entries - 1) { /* link to next one */
+ chain[num_entries].nextentry = &chain[num_entries + 1];
+ }
+ ++num_entries;
+ } /* end of "while (k)" */
+ } /* end of "for (i ... NKVHASH ..." */
+
+ /* terminate chain */
+ if (num_entries > 0) {
+ chain[num_entries - 1].nextentry = NULL;
+ retval = &chain[0];
+ }
+
+ return retval;
+}
+
+static size_t needroom3(void)
+{
+ /*
+ * Check for enough room. This is extremely approximate but should
+ * be enough space. Really need 2 times:
+ * (8byte fileid
+ * 8byte cookie
+ * 8byte name pointer
+ * 8byte next entry addres) = sizeof(am_entry3)
+ * 2byte name + 1byte terminator
+ * plus the size of the am_dirlist3 structure */
+ return ((2 * ((sizeof(am_entry3) + sizeof("..") + 1))) + sizeof(am_dirlist3));
+}
+
+/* This one is called only if map is browsable */
+static int
+amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie,
+ am_dirlist3 *dp, am_entry3 *ep, u_int count,
+ int fully_browsable)
+{
+ uint64 gen = *(uint64 *) cookie;
+ int chain_length, i;
+ static am_entry3 *te, *te_next;
+ static int j;
+
+ dp->eof = FALSE; /* assume readdir not done */
+
+ if (amuDebug(D_READDIR))
+ plog(XLOG_DEBUG, "amfs_readdir3_browsable gen=%lu, count=%d", gen, count);
+
+ if (gen == 0) {
+ size_t needed = needroom3();
+ /*
+ * In the default instance (which is used to start a search) we return
+ * "." and "..".
+ *
+ * This assumes that the count is big enough to allow both "." and ".."
+ * to be returned in a single packet. If it isn't (which would be
+ * fairly unbelievable) then tough.
+ */
+ dlog("%s: default search", __func__);
+
+ if (count < needed) {
+ dlog("%s: not enough room %u < %zu", __func__, count, needed);
+ return EINVAL;
+ }
+
+ /*
+ * compute # of entries to send in this chain.
+ * heuristics: 128 bytes per entry.
+ * This is too much probably, but it seems to work better because
+ * of the re-entrant nature of nfs_readdir, and esp. on systems
+ * like OpenBSD 2.2.
+ */
+ chain_length = count / 128;
+
+ /* reset static state counters */
+ te = te_next = NULL;
+
+ dp->entries = ep;
+
+ /* construct "." */
+ ep[0].fileid = mp->am_gen;
+ ep[0].name = ".";
+ ep[0].nextentry = &ep[1];
+ ep[0].cookie = 0;
+
+ /* construct ".." */
+ if (mp->am_parent)
+ ep[1].fileid = mp->am_parent->am_gen;
+ else
+ ep[1].fileid = mp->am_gen;
+
+ ep[1].name = "..";
+ ep[1].nextentry = NULL;
+ ep[1].cookie = dotdotcookie;
+
+ /*
+ * If map is browsable, call a function make_entry_chain() to construct
+ * a linked list of unmounted keys, and return it. Then link the chain
+ * to the regular list. Get the chain only once, but return
+ * chunks of it each time.
+ */
+ te = make_entry_chain3(mp, dp->entries, fully_browsable);
+ if (!te)
+ return 0;
+ if (amuDebug(D_READDIR)) {
+ am_entry3 *ne;
+ for (j = 0, ne = te; ne; ne = ne->ne_nextentry)
+ plog(XLOG_DEBUG, "gen1 key %4d \"%s\"", j++, ne->ne_name);
+ }
+
+ /* return only "chain_length" entries */
+ te_next = te;
+ for (i=1; i<chain_length; ++i) {
+ te_next = te_next->nextentry;
+ if (!te_next)
+ break;
+ }
+ if (te_next) {
+ am_entry3 *te_saved = te_next->nextentry;
+ te_next->nextentry = NULL; /* terminate "te" chain */
+ te_next = te_saved; /* save rest of "te" for next iteration */
+ dp->eof = FALSE; /* tell readdir there's more */
+ } else {
+ dp->eof = TRUE; /* tell readdir that's it */
+ }
+ ep[1].nextentry = te; /* append this chunk of "te" chain */
+ if (amuDebug(D_READDIR)) {
+ am_entry3 *ne;
+ for (j = 0, ne = te; ne; ne = ne->ne_nextentry)
+ plog(XLOG_DEBUG, "gen2 key %4d \"%s\"", j++, ne->name);
+ for (j = 0, ne = ep; ne; ne = ne->ne_nextentry) {
+ plog(XLOG_DEBUG, "gen2+ key %4d \"%s\" fi=%lu ck=%lu",
+ j++, ne->name, ne->fileid, ne->cookie);
+ }
+ plog(XLOG_DEBUG, "EOF is %d", dp->eof);
+ }
+ return 0;
+ } /* end of "if (gen == 0)" statement */
+
+ dlog("%s: real child", __func__);
+
+ if (gen == DOT_DOT_COOKIE) {
+ dlog("%s: End of readdir in %s", __func__, mp->am_path);
+ dp->eof = TRUE;
+ dp->entries = NULL;
+ return 0;
+ }
+
+ /*
+ * If browsable directories, then continue serving readdir() with another
+ * chunk of entries, starting from where we left off (when gen was equal
+ * to 0). Once again, assume last chunk served to readdir.
+ */
+ dp->eof = TRUE;
+ dp->entries = ep;
+
+ te = te_next; /* reset 'te' from last saved te_next */
+ if (!te) { /* another indicator of end of readdir */
+ dp->entries = NULL;
+ return 0;
+ }
+ /*
+ * compute # of entries to send in this chain.
+ * heuristics: 128 bytes per entry.
+ */
+ chain_length = count / 128;
+
+ /* return only "chain_length" entries */
+ for (i = 1; i < chain_length; ++i) {
+ te_next = te_next->nextentry;
+ if (!te_next)
+ break;
+ }
+ if (te_next) {
+ am_entry3 *te_saved = te_next->nextentry;
+ te_next->nextentry = NULL; /* terminate "te" chain */
+ te_next = te_saved; /* save rest of "te" for next iteration */
+ dp->eof = FALSE; /* tell readdir there's more */
+ }
+ ep = te; /* send next chunk of "te" chain */
+ dp->entries = ep;
+ if (amuDebug(D_READDIR)) {
+ am_entry3 *ne;
+ plog(XLOG_DEBUG,
+ "entries=%p, te_next=%p, eof=%d", dp->entries, te_next, dp->eof);
+ for (ne = te; ne; ne = ne->nextentry)
+ plog(XLOG_DEBUG, "gen3 key %4d \"%s\"", j++, ne->name);
+ }
+ return 0;
+}
+
+static int
+amfs_readdir3(am_node *mp, am_cookie3 cookie,
+ am_dirlist3 *dp, am_entry3 *ep, u_int count)
+{
+ uint64 gen = *(uint64 *) cookie;
+ am_node *xp;
+
+ if (amuDebug(D_READDIR))
+ plog(XLOG_DEBUG, "amfs_readdir3 gen=%lu, count=%d", gen, count);
+
+ dp->eof = FALSE; /* assume readdir not done */
+
+ /* when gen is 0, we start reading from the beginning of the directory */
+ if (gen == 0) {
+ size_t needed = needroom3();
+ /*
+ * In the default instance (which is used to start a search) we return
+ * "." and "..".
+ *
+ * This assumes that the count is big enough to allow both "." and ".."
+ * to be returned in a single packet. If it isn't (which would be
+ * fairly unbelievable) then tough.
+ */
+ dlog("%s: default search", __func__);
+
+ if (count < needed) {
+ dlog("%s: not enough room %u < %zu", __func__, count, needed);
+ return EINVAL;
+ }
+
+ xp = next_nonerror_node(mp->am_child);
+ dp->entries = ep;
+
+ /* construct "." */
+ ep[0].fileid = mp->am_gen;
+ ep[0].name = ".";
+ ep[0].cookie = 0;
+ ep[0].nextentry = &ep[1];
+
+ /* construct ".." */
+ if (mp->am_parent)
+ ep[1].fileid = mp->am_parent->am_gen;
+ else
+ ep[1].fileid = mp->am_gen;
+ ep[1].name = "..";
+ ep[1].nextentry = NULL;
+ ep[1].cookie = (xp ? xp->am_gen : dotdotcookie);
+
+ if (!xp)
+ dp->eof = TRUE; /* by default assume readdir done */
+
+ if (amuDebug(D_READDIR)) {
+ am_entry3 *ne;
+ int j;
+ for (j = 0, ne = ep; ne; ne = ne->nextentry) {
+ plog(XLOG_DEBUG, "gen1 key %4d \"%s\" fi=%lu ck=%lu",
+ j++, ne->name, ne->fileid, ne->cookie);
+ }
+ }
+ return 0;
+ }
+ dlog("%s: real child", __func__);
+
+ if (gen == (uint64) DOT_DOT_COOKIE) {
+ dlog("%s: End of readdir in %s", __func__, mp->am_path);
+ dp->eof = TRUE;
+ dp->entries = NULL;
+ if (amuDebug(D_READDIR))
+ plog(XLOG_DEBUG, "end of readdir eof=TRUE, dl_entries=0\n");
+ return 0;
+ }
+
+ /* non-browsable directories code */
+ xp = mp->am_child;
+ while (xp && xp->am_gen != gen)
+ xp = xp->am_osib;
+
+ if (xp) {
+ int nbytes = count / 2; /* conservative */
+ int todo = MAX_READDIR_ENTRIES;
+
+ dp->entries = ep;
+ do {
+ am_node *xp_next = next_nonerror_node(xp->am_osib);
+
+ if (xp_next) {
+ ep->cookie = xp_next->am_gen;
+ } else {
+ ep->cookie = (uint64) dotdotcookie;
+ dp->eof = TRUE;
+ }
+
+ ep->fileid = xp->am_gen;
+ ep->name = xp->am_name;
+ nbytes -= sizeof(*ep) + 1;
+ if (xp->am_name)
+ nbytes -= strlen(xp->am_name);
+
+ xp = xp_next;
+
+ if (nbytes > 0 && !dp->dl_eof && todo > 1) {
+ ep->nextentry = ep + 1;
+ ep++;
+ --todo;
+ } else {
+ todo = 0;
+ }
+ } while (todo > 0);
+
+ ep->nextentry = NULL;
+
+ if (amuDebug(D_READDIR)) {
+ am_entry3 *ne;
+ int j;
+ for (j = 0, ne = ep; ne; ne = ne->nextentry) {
+ plog(XLOG_DEBUG, "gen2 key %4d \"%s\" fi=%lu ck=%lu",
+ j++, ne->name, ne->fileid, ne->cookie);
+ }
+ }
+ return 0;
+ }
+ return ESTALE;
+}
+
+/*
+ * This readdir function which call a special version of it that allows
+ * browsing if browsable_dirs=yes was set on the map.
+ */
+int
+amfs_generic_readdir(am_node *mp, voidp cookie, voidp dp, voidp ep, u_int count)
+{
+ int browsable, full;
+
+ /* check if map is browsable */
+ browsable = 0;
+ if (mp->am_al->al_mnt && mp->am_al->al_mnt->mf_mopts) {
+ mntent_t mnt;
+ mnt.mnt_opts = mp->am_al->al_mnt->mf_mopts;
+ if (amu_hasmntopt(&mnt, "fullybrowsable"))
+ browsable = 2;
+ else if (amu_hasmntopt(&mnt, "browsable"))
+ browsable = 1;
+ }
+ full = (browsable == 2);
+
+ if (nfs_dispatcher == nfs_program_2) {
+ if (browsable)
+ return amfs_readdir_browsable(mp, cookie, dp, ep, count, full);
+ else
+ return amfs_readdir(mp, cookie, dp, ep, count);
+ } else {
+ if (browsable)
+ return amfs_readdir3_browsable(mp, (am_cookie3) cookie, dp, ep, count, full);
+ else
+ return amfs_readdir3(mp, (am_cookie3) cookie, dp, ep, count);
+ }
+}