]> wimlib.net Git - wimlib/blobdiff - src/mount.c
Mount fixes
[wimlib] / src / mount.c
index b67f40c6faacaec36cbb8e969fabcce36899bb21..b1d7381072f224de30778839dba6847890f3b1bb 100644 (file)
@@ -108,6 +108,10 @@ static int alloc_wimlib_fd(struct inode *inode,
        static const u16 fds_per_alloc = 8;
        static const u16 max_fds = 0xffff;
 
+       DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
+             stream_id, inode->ino, inode->num_opened_fds,
+             inode->num_allocated_fds);
+
        if (inode->num_opened_fds == inode->num_allocated_fds) {
                struct wimlib_fd **fds;
                u16 num_new_fds;
@@ -138,7 +142,9 @@ static int alloc_wimlib_fd(struct inode *inode,
                        *fd_ret        = fd;
                        inode->fds[i]  = fd;
                        inode->num_opened_fds++;
-                       lte->num_opened_fds++;
+                       if (lte)
+                               lte->num_opened_fds++;
+                       DEBUG("Allocated fd");
                        return 0;
                }
        }
@@ -182,6 +188,9 @@ static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
 static int close_wimlib_fd(struct wimlib_fd *fd)
 {
        int ret;
+       DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
+             fd->inode->ino, fd->inode->num_opened_fds,
+             fd->inode->num_allocated_fds);
        ret = lte_put_fd(fd->lte, fd);
        if (ret != 0)
                return ret;
@@ -207,7 +216,7 @@ static void remove_dentry(struct dentry *dentry,
        struct lookup_table_entry *lte;
        unsigned i;
 
-       for (i = 0; <= inode->num_ads; i++) {
+       for (i = 0; <= inode->num_ads; i++) {
                lte = inode_stream_lte_resolved(inode, i);
                if (lte)
                        lte_decrement_refcnt(lte, lookup_table);
@@ -346,6 +355,9 @@ static int create_staging_file(char **name_ret, int open_flags)
  *
  * @inode:  Inode that contains the stream we are extracting
  *
+ * @stream_id: Identifier for the stream (it stays constant even if the indices
+ * of the stream entries are changed)
+ *
  * @lte: Pointer to pointer to the lookup table entry for the stream we need to
  * extract, or NULL if there was no lookup table entry present for the stream
  *
@@ -380,63 +392,102 @@ static int extract_resource_to_staging_dir(struct inode *inode,
                goto out_delete_staging_file;
        }
 
-       if (old_lte) {
-               wimlib_assert(old_lte->resource_location == RESOURCE_IN_WIM);
-               if (inode->link_count == old_lte->refcnt) {
-                       /* The reference count of the existing lookup table
-                        * entry is the same as the size of the hard link group
-                        * associated with the dentry; therefore, ALL the
-                        * references to the lookup table entry correspond to
-                        * the stream we're trying to extract.  So the lookup
-                        * table entry can be re-used.
-                        */
-                       DEBUG("Re-using lookup table entry");
-                       lookup_table_unlink(w->lookup_table, old_lte);
-                       new_lte = old_lte;
-               } else {
+       if (old_lte && inode->link_count == old_lte->refcnt) {
+               /* The reference count of the existing lookup table
+                * entry is the same as the link count of the inode that
+                * contains the stream we're opening.  Therefore, ALL
+                * the references to the lookup table entry correspond
+                * to the stream we're trying to extract, so the lookup
+                * table entry can be re-used.  */
+               DEBUG("Re-using lookup table entry");
+               lookup_table_unlink(w->lookup_table, old_lte);
+               new_lte = old_lte;
+       } else {
+               if (old_lte) {
+                       /* There's an existing lookup table entry, but its
+                        * reference count is creater than the link count for
+                        * the inode containing a stream we're opening.
+                        * Therefore, we need to split the lookup table entry.
+                        * */
+                       wimlib_assert(old_lte->refcnt > inode->link_count);
                        DEBUG("Splitting lookup table entry "
-                             "(inode->link_count = %zu, lte refcnt = %u)",
+                             "(inode->link_count = %zu, old_lte->refcnt = %u)",
                              inode->link_count, old_lte->refcnt);
 
-                       wimlib_assert(old_lte->refcnt > inode->link_count);
+               }
 
-                       new_lte = new_lookup_table_entry();
-                       if (!new_lte) {
-                               ret = -ENOMEM;
-                               goto out_delete_staging_file;
-                       }
-                       old_lte->refcnt -= inode->link_count;
+               new_lte = new_lookup_table_entry();
+               if (!new_lte) {
+                       ret = -ENOMEM;
+                       goto out_delete_staging_file;
+               }
 
-                       for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
-                               if (inode->fds[i]) {
-                                       if (inode->fds[i]->stream_id == stream_id) {
-                                               wimlib_assert(inode->fds[i]->lte == old_lte);
-                                               inode->fds[i]->lte = new_lte;
+               /* There may already be open file descriptors to this stream if
+                * it's previously been opened read-only, but just now we're
+                * opening it read-write.  Identify those file descriptors and
+                * change their lookup table entry pointers to point to the new
+                * lookup table entry, and open staging file descriptors for
+                * them. 
+                *
+                * At the same time, we need to count the number of these opened
+                * file descriptors to the new lookup table entry.  If there's
+                * an old lookup table entry, this number needs to be subtracted
+                * from the fd's opened to the old entry. */
+               for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
+                       struct wimlib_fd *fd = inode->fds[i];
+                       if (fd) {
+                               if (fd->stream_id == stream_id) {
+                                       wimlib_assert(fd->lte == old_lte);
+                                       fd->lte = new_lte;
+                                       new_lte->num_opened_fds++;
+                                       fd->staging_fd = open(staging_file_name, O_RDONLY);
+                                       if (fd->staging_fd == -1) {
+                                               ret = -errno;
+                                               goto out_revert_fd_changes;
                                        }
-                                       j++;
                                }
+                               j++;
                        }
                }
-       } else {
-               /* No old_lte was supplied, so the resource had no lookup table
-                * entry before (it must be an empty resource) */
-               wimlib_assert(inode->num_opened_fds == 0);
-               new_lte = new_lookup_table_entry();
-               if (!new_lte) {
-                       ret = -ENOMEM;
-                       goto out_delete_staging_file;
+               DEBUG("%zu fd's were already opened to the file we extracted",
+                     new_lte->num_opened_fds);
+               if (old_lte) {
+                       old_lte->num_opened_fds -= new_lte->num_opened_fds;
+                       old_lte->refcnt -= inode->link_count;
                }
        }
+
        new_lte->resource_entry.original_size = size;
        new_lte->refcnt = inode->link_count;
        random_hash(new_lte->hash);
        new_lte->staging_file_name = staging_file_name;
        new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
 
+       if (stream_id == 0)
+               inode->lte = new_lte;
+       else
+               for (u16 i = 0; i < inode->num_ads; i++)
+                       if (inode->ads_entries[i]->stream_id == stream_id)
+                               inode->ads_entries[i]->lte = new_lte;
+
        lookup_table_insert(w->lookup_table, new_lte);
        list_add(&new_lte->staging_list, &staging_list);
        *lte = new_lte;
        return 0;
+out_revert_fd_changes:
+       for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
+               struct wimlib_fd *fd = inode->fds[i];
+               if (fd && fd->stream_id == stream_id && fd->lte == new_lte) {
+                       fd->lte = old_lte;
+                       if (fd->staging_fd != -1) {
+                               close(fd->staging_fd);
+                               fd->staging_fd = -1;
+                       }
+                       j++;
+               }
+       }
+out_free_new_lte:
+       free_lookup_table_entry(new_lte);
 out_delete_staging_file:
        unlink(staging_file_name);
        FREE(staging_file_name);
@@ -1158,10 +1209,10 @@ static int wimfs_open(const char *path, struct fuse_file_info *fi)
         * opening it read-only, but we need to extract the resource to the
         * staging directory if we are opening it writable. */
        if (flags_writable(fi->flags) &&
-             lte->resource_location != RESOURCE_IN_STAGING_FILE) {
+            (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
+               u64 size = (lte) ? wim_resource_size(lte) : 0;
                ret = extract_resource_to_staging_dir(inode, stream_id,
-                                                     &lte,
-                                                     wim_resource_size(lte));
+                                                     &lte, size);
                if (ret != 0)
                        return ret;
        }
@@ -1170,7 +1221,7 @@ static int wimfs_open(const char *path, struct fuse_file_info *fi)
        if (ret != 0)
                return ret;
 
-       if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
+       if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
                fd->staging_fd = open(lte->staging_file_name, fi->flags);
                if (fd->staging_fd == -1) {
                        close_wimlib_fd(fd);
@@ -1184,17 +1235,13 @@ static int wimfs_open(const char *path, struct fuse_file_info *fi)
 /* Opens a directory. */
 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
 {
-       struct dentry *dentry;
        struct inode *inode;
        int ret;
        struct wimlib_fd *fd = NULL;
        
-       dentry = get_dentry(w, path);
-       if (!dentry)
+       inode = wim_pathname_to_inode(w, path);
+       if (!inode)
                return -ENOENT;
-
-       inode = dentry->inode;
-
        if (!inode_is_directory(inode))
                return -ENOTDIR;
        ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
@@ -1425,7 +1472,6 @@ static int wimfs_rmdir(const char *path)
 static int wimfs_setxattr(const char *path, const char *name,
                          const char *value, size_t size, int flags)
 {
-       struct dentry *dentry;
        struct ads_entry *existing_ads_entry;
        struct ads_entry *new_ads_entry;
        struct lookup_table_entry *existing_lte;
@@ -1441,21 +1487,20 @@ static int wimfs_setxattr(const char *path, const char *name,
                return -ENOATTR;
        name += 5;
 
-       dentry = get_dentry(w, path);
-       if (!dentry)
+       inode = wim_pathname_to_inode(w, path);
+       if (!inode)
                return -ENOENT;
-       inode = dentry->inode;
 
        existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
        if (existing_ads_entry) {
                if (flags & XATTR_CREATE)
                        return -EEXIST;
-               inode_remove_ads(dentry->inode, ads_idx, w->lookup_table);
+               inode_remove_ads(inode, ads_idx, w->lookup_table);
        } else {
                if (flags & XATTR_REPLACE)
                        return -ENOATTR;
        }
-       new_ads_entry = inode_add_ads(dentry, name);
+       new_ads_entry = inode_add_ads(inode, name);
        if (!new_ads_entry)
                return -ENOMEM;
 
@@ -1494,7 +1539,7 @@ static int wimfs_symlink(const char *to, const char *from)
 {
        struct dentry *dentry_parent, *dentry;
        const char *link_name;
-       struct lookup_table_entry *lte;
+       struct inode *inode;
        
        dentry_parent = get_parent_dentry(w, from);
        if (!dentry_parent)
@@ -1506,24 +1551,18 @@ static int wimfs_symlink(const char *to, const char *from)
 
        if (get_dentry_child_with_name(dentry_parent, link_name))
                return -EEXIST;
-       dentry = new_dentry(link_name);
+       dentry = new_dentry_with_inode(link_name);
        if (!dentry)
                return -ENOMEM;
+       inode = dentry->inode;
 
-       dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
-       dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
-       dentry->link_group_id = next_link_group_id++;
+       inode->attributes  = FILE_ATTRIBUTE_REPARSE_POINT;
+       inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
+       inode->ino         = next_ino++;
 
-       if (dentry_set_symlink(dentry, to, w->lookup_table, &lte) != 0)
+       if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
                goto out_free_dentry;
 
-       wimlib_assert(lte);
-
-       dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
-       list_add(&dentry->ads_entries[1].lte_group_list.list,
-                &lte->lte_group_list);
-       wimlib_assert(dentry->resolved);
-
        link_dentry(dentry, dentry_parent);
        return 0;
 out_free_dentry:
@@ -1538,7 +1577,9 @@ static int wimfs_truncate(const char *path, off_t size)
        struct dentry *dentry;
        struct lookup_table_entry *lte;
        int ret;
-       unsigned stream_idx;
+       u16 stream_idx;
+       u32 stream_id;
+       struct inode *inode;
        
        ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
                              &lte, &stream_idx);
@@ -1549,6 +1590,13 @@ static int wimfs_truncate(const char *path, off_t size)
        if (!lte) /* Already a zero-length file */
                return 0;
 
+       inode = dentry->inode;
+
+       if (stream_idx == 0)
+               stream_id = 0;
+       else
+               stream_id = inode->ads_entries[stream_idx - 1]->stream_id;
+
        if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
                wimlib_assert(lte->staging_file_name);
                ret = truncate(lte->staging_file_name, size);
@@ -1559,7 +1607,7 @@ static int wimfs_truncate(const char *path, off_t size)
                wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
                /* File in WIM.  Extract it to the staging directory, but only
                 * the first @size bytes of it. */
-               ret = extract_resource_to_staging_dir(dentry, stream_idx,
+               ret = extract_resource_to_staging_dir(inode, stream_id,
                                                      &lte, size);
        }
        return ret;
@@ -1572,7 +1620,7 @@ static int wimfs_unlink(const char *path)
        struct lookup_table_entry *lte;
        struct inode *inode;
        int ret;
-       unsigned stream_idx;
+       u16 stream_idx;
        unsigned i;
        
        ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
@@ -1711,46 +1759,6 @@ static struct fuse_operations wimfs_operations = {
 };
 
 
-static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
-{
-       size_t lte_group_size = 0;
-       struct list_head *cur;
-       list_for_each(cur, &lte->lte_group_list)
-               lte_group_size++;
-       if (lte_group_size > lte->refcnt) {
-#ifdef ENABLE_ERROR_MESSAGES
-               struct dentry *example_dentry;
-               struct list_head *next;
-               struct stream_list_head *head;
-               WARNING("The following lookup table entry has a reference count "
-                     "of %u, but", lte->refcnt);
-               WARNING("We found %zu references to it", lte_group_size);
-               next = lte->lte_group_list.next;
-               head = container_of(next, struct stream_list_head, list);
-               if (head->type == STREAM_TYPE_NORMAL) {
-                       example_dentry = container_of(head, struct dentry,
-                                                     lte_group_list);
-                       WARNING("(One dentry referencing it is at `%s')",
-                               example_dentry->full_path_utf8);
-               }
-               print_lookup_table_entry(lte);
-#endif
-               /* Guess what!  install.wim for Windows 8 contains a stream with
-                * 2 dentries referencing it, but the lookup table entry has
-                * reference count of 1.  So we will need to handle this case
-                * and not just make it be an error...  I'm just setting the
-                * reference count to the number of references we found. */
-
-               #if 1
-               lte->refcnt = lte_group_size;
-               WARNING("Fixing reference count");
-               #else
-               return WIMLIB_ERR_INVALID_DENTRY;
-               #endif
-       }
-       return 0;
-}
-
 /* Mounts a WIM file. */
 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir, 
                           int flags, WIMStruct **additional_swms,
@@ -1761,6 +1769,7 @@ WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
        int ret;
        char *p;
        struct lookup_table *joined_tab, *wim_tab_save;
+       struct image_metadata *imd;
 
        DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
                        wim, image, dir, flags);
@@ -1787,23 +1796,21 @@ WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
        if (ret != 0)
                goto out;
 
+       imd = &wim->image_metadata[image - 1];
+
        DEBUG("Selected image %d", image);
 
-       next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
+       next_ino = assign_inode_numbers(&imd->inode_list);
+
+       DEBUG("(next_ino = %"PRIu64")", next_ino);
 
-       DEBUG("Resolving lookup table entries");
        /* Resolve all the lookup table entries of the dentry tree */
-       for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
+       DEBUG("Resolving lookup table entries");
+       for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
                           wim->lookup_table);
 
-       DEBUG("Checking lookup table entry reference counts");
-
-       ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
-       if (ret != 0)
-               goto out;
-
        if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
-               wim_get_current_image_metadata(wim)->modified = true;
+               imd->modified = true;
 
        if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
                       WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |