@@ -28,9 +28,11 @@
#define P9_CMD_VERSION 100
#define P9_CMD_ATTACH 104
#define P9_CMD_ERROR 107
+#define P9_CMD_WALK 110
#define P9_MIN_MSIZE 2048
#define P9_VERSION "9P2000.u"
+#define P9_WALK_MAXELEM 16
struct p9_qid {
uint8_t type;
@@ -478,14 +480,29 @@ static struct p9_fid *find_fid(device *device, unsigned int fid)
return NULL;
}
+static struct p9_fid *get_fid_ref(device *device, unsigned int fid)
+{
+ struct p9_fid *fidp;
+
+ pthread_mutex_lock(&device->fid_mutex);
+
+ fidp = find_fid(device, fid);
+ if ( fidp )
+ fidp->ref++;
+
+ pthread_mutex_unlock(&device->fid_mutex);
+
+ return fidp;
+}
+
static struct p9_fid *alloc_fid_mem(device *device, unsigned int fid,
const char *path)
{
struct p9_fid *fidp;
size_t pathlen;
- pathlen = strlen(path);
- fidp = calloc(sizeof(*fidp) + pathlen + 1, 1);
+ pathlen = strlen(path) + 1;
+ fidp = calloc(sizeof(*fidp) + pathlen, 1);
if ( !fidp )
return NULL;
@@ -504,7 +521,7 @@ static struct p9_fid *alloc_fid(device *device, unsigned int fid,
if ( find_fid(device, fid) )
{
- errno = EBADFD;
+ errno = EBADF;
goto out;
}
@@ -578,6 +595,10 @@ static int fill_qid(device *device, const char *path, struct p9_qid *qid,
stbuf = &st;
}
+ /* Don't allow symbolic links. */
+ if ( S_ISLNK(stbuf->st_mode) )
+ return EMLINK;
+
qid->type = S_ISDIR(stbuf->st_mode) ? QID_TYPE_DIR : 0;
qid->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
qid->path = stbuf->st_ino;
@@ -585,6 +606,20 @@ static int fill_qid(device *device, const char *path, struct p9_qid *qid,
return 0;
}
+static bool name_ok(const char *str)
+{
+ if ( !*str )
+ return false;
+
+ if ( strchr(str, '/' ) )
+ return false;
+
+ if ( !strcmp(str, "..") || !strcmp(str, ".") )
+ return false;
+
+ return true;
+}
+
static void p9_error(struct ring *ring, uint16_t tag, uint32_t err)
{
unsigned int erroff;
@@ -662,6 +697,138 @@ static void p9_attach(struct ring *ring, struct p9_header *hdr)
fill_buffer(ring, hdr->cmd + 1, hdr->tag, "Q", &qid);
}
+static void p9_walk(struct ring *ring, struct p9_header *hdr)
+{
+ device *device = ring->device;
+ uint32_t fid;
+ uint32_t newfid;
+ struct p9_fid *fidp = NULL;
+ struct p9_qid *qids = NULL;
+ unsigned int n_names = 0;
+ unsigned int *names = NULL;
+ unsigned int walked = 0;
+ unsigned int i;
+ char *path = NULL;
+ unsigned int path_len;
+ int ret;
+
+ ret = fill_data(ring, "UUaS", &fid, &newfid, &n_names, &names);
+ if ( n_names > P9_WALK_MAXELEM )
+ {
+ p9_error(ring, hdr->tag, EINVAL);
+ goto out;
+ }
+ if ( ret != 3 + n_names )
+ {
+ p9_error(ring, hdr->tag, errno);
+ goto out;
+ }
+
+ fidp = get_fid_ref(device, fid);
+ if ( !fidp )
+ {
+ p9_error(ring, hdr->tag, ENOENT);
+ goto out;
+ }
+ if ( fidp->opened )
+ {
+ p9_error(ring, hdr->tag, EINVAL);
+ goto out;
+ }
+
+ path_len = strlen(fidp->path) + 1;
+ for ( i = 0; i < n_names; i++ )
+ {
+ if ( !name_ok(ring->str + names[i]) )
+ {
+ p9_error(ring, hdr->tag, ENOENT);
+ goto out;
+ }
+ path_len += strlen(ring->str + names[i]) + 1;
+ }
+ path = calloc(path_len + 1, 1);
+ if ( !path )
+ {
+ p9_error(ring, hdr->tag, ENOMEM);
+ goto out;
+ }
+ strcpy(path, fidp->path);
+
+ if ( n_names )
+ {
+ qids = calloc(n_names, sizeof(*qids));
+ if ( !qids )
+ {
+ p9_error(ring, hdr->tag, ENOMEM);
+ goto out;
+ }
+ for ( i = 0; i < n_names; i++ )
+ {
+ strcat(path, "/");
+ strcat(path, ring->str + names[i]);
+ ret = fill_qid(device, path, qids + i, NULL);
+ if ( ret )
+ {
+ if ( !walked )
+ {
+ p9_error(ring, hdr->tag, errno);
+ goto out;
+ }
+ break;
+ }
+ walked++;
+ }
+ }
+
+ if ( walked == n_names )
+ {
+ bool ok = false;
+
+ if ( fid == newfid )
+ {
+ struct p9_fid *new_fidp;
+
+ pthread_mutex_lock(&device->fid_mutex);
+
+ if ( fidp->ref != 2 )
+ {
+ errno = EBUSY;
+ }
+ else
+ {
+ new_fidp = alloc_fid_mem(device, fid, path);
+ if ( new_fidp )
+ {
+ new_fidp->ref = 2;
+ XEN_TAILQ_REMOVE(&device->fids, fidp, list);
+ XEN_TAILQ_INSERT_HEAD(&device->fids, new_fidp, list);
+ free(fidp);
+ fidp = new_fidp;
+ ok = true;
+ }
+ }
+
+ pthread_mutex_unlock(&device->fid_mutex);
+ }
+ else
+ ok = alloc_fid(device, newfid, path);
+
+ if ( !ok )
+ {
+ p9_error(ring, hdr->tag, errno);
+ goto out;
+ }
+ }
+
+ fill_buffer(ring, hdr->cmd + 1, hdr->tag, "aQ", &walked, qids);
+
+ out:
+ free_fid(device, fidp);
+ free(qids);
+ free(path);
+ free(names);
+}
+
void *io_thread(void *arg)
{
struct ring *ring = arg;
@@ -725,6 +892,10 @@ void *io_thread(void *arg)
p9_attach(ring, &hdr);
break;
+ case P9_CMD_WALK:
+ p9_walk(ring, &hdr);
+ break;
+
default:
syslog(LOG_DEBUG, "%u.%u sent unhandled command %u\n",
ring->device->domid, ring->device->devid, hdr.cmd);
@@ -25,6 +25,7 @@ struct p9_fid {
XEN_TAILQ_ENTRY(struct p9_fid) list;
unsigned int fid;
unsigned int ref;
+ bool opened;
char path[];
};