+static int nfs_lookup_path_async_internal(struct nfs_context *nfs, fattr3 *attr, struct nfs_cb_data *data, struct nfs_fh3 *fh)
+{
+ char *path, *slash;
+ LOOKUP3args args;
+
+ while (*data->path == '/') {
+ data->path++;
+ }
+
+ path = data->path;
+ slash = strchr(path, '/');
+
+ if (attr && attr->type == NF3LNK && (!data->no_follow || *path != '\0')) {
+ READLINK3args args;
+
+ if (data->link_count++ >= MAX_LINK_COUNT) {
+ data->cb(-ELOOP, nfs, "Too many levels of symbolic links", data->private_data);
+ free_nfs_cb_data(data);
+ return -1;
+ }
+
+ args.symlink = *fh;
+
+ if (rpc_nfs3_readlink_async(nfs->rpc, nfs_lookup_path_2_cb, &args, data) != 0) {
+ rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path);
+ data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+ free_nfs_cb_data(data);
+ return -1;
+ }
+
+ if (slash != NULL) {
+ *slash = '/';
+ }
+ return 0;
+ }
+
+ if (slash != NULL) {
+ /* Clear slash so that path is a zero terminated string for
+ * the current path component. Set it back to '/' again later
+ * when we are finished referencing this component so that
+ * data->saved_path will still point to the full
+ * normalized path.
+ */
+ *slash = 0;
+ data->path = slash+1;
+ } else {
+ while (*data->path != 0) {
+ data->path++;
+ }
+ }
+
+ if (*path == 0) {
+ data->fh.data.data_len = fh->data.data_len;
+ data->fh.data.data_val = malloc(data->fh.data.data_len);
+ if (data->fh.data.data_val == NULL) {
+ rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path);
+ data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+ free_nfs_cb_data(data);
+ return -1;
+ }
+ memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len);
+ if (slash != NULL) {
+ *slash = '/';
+ }
+ data->continue_cb(nfs, attr, data);
+ return 0;
+ }
+
+ memset(&args, 0, sizeof(LOOKUP3args));
+ args.what.dir = *fh;
+ args.what.name = path;
+
+ if (rpc_nfs3_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, &args, data) != 0) {
+ rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path);
+ data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+ free_nfs_cb_data(data);
+ return -1;
+ }
+ if (slash != NULL) {
+ *slash = '/';
+ }
+ return 0;
+}
+
+static int nfs_normalize_path(struct nfs_context *nfs, char *path)
+{
+ char *str;
+ int len;
+
+ /* // -> / */
+ while ((str = strstr(path, "//"))) {
+ while(*str) {
+ *str = *(str + 1);
+ str++;
+ }
+ }
+
+ /* /./ -> / */
+ while ((str = strstr(path, "/./"))) {
+ while(*(str + 1)) {
+ *str = *(str + 2);
+ str++;
+ }
+ }
+
+ /* ^/../ -> error */
+ if (!strncmp(path, "/../", 4)) {
+ rpc_set_error(nfs->rpc,
+ "Absolute path starts with '/../' "
+ "during normalization");
+ return -1;
+ }
+
+ /* ^[^/] -> error */
+ if (path[0] != '/') {
+ rpc_set_error(nfs->rpc,
+ "Absolute path does not start with '/'");
+ return -1;
+ }
+
+ /* /string/../ -> / */
+ while ((str = strstr(path, "/../"))) {
+ char *tmp;
+
+ if (!strncmp(path, "/../", 4)) {
+ rpc_set_error(nfs->rpc,
+ "Absolute path starts with '/../' "
+ "during normalization");
+ return -1;
+ }
+
+ tmp = str - 1;
+ while (*tmp != '/') {
+ tmp--;
+ }
+ str += 3;
+ while((*(tmp++) = *(str++)) != '\0')
+ ;
+ }
+
+ /* /$ -> \0 */
+ len = strlen(path);
+ if (len > 1) {
+ if (path[len - 1] == '/') {
+ path[len - 1] = '\0';
+ len--;
+ }
+ }
+ if (path[0] == '\0') {
+ rpc_set_error(nfs->rpc,
+ "Absolute path became '' "
+ "during normalization");
+ return -1;
+ }
+
+ /* /.$ -> \0 */
+ if (len >= 2) {
+ if (!strcmp(&path[len - 2], "/.")) {
+ path[len - 2] = '\0';
+ len -= 2;
+ }
+ }
+
+ /* ^/..$ -> error */
+ if (!strcmp(path, "/..")) {
+ rpc_set_error(nfs->rpc,
+ "Absolute path is '/..' "
+ "during normalization");
+ return -1;
+ }
+
+ /* /string/..$ -> / */
+ if (len >= 3) {
+ if (!strcmp(&path[len - 3], "/..")) {
+ char *tmp = &path[len - 3];
+ while (*--tmp != '/')
+ ;
+ *tmp = '\0';
+ }
+ }
+
+ return 0;
+}
+
+static void nfs_lookup_path_getattr_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)