Commit 82de647e1f81fd89afc48608d889dd3b33cb8983

Authored by Phillip Lougher
1 parent 117a91e0f2

Squashfs: move table allocation into squashfs_read_table()

This eliminates a lot of duplicate code.

Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>

Showing 7 changed files with 44 additions and 98 deletions Side-by-side Diff

... ... @@ -393,20 +393,37 @@
393 393 /*
394 394 * Read a filesystem table (uncompressed sequence of bytes) from disk
395 395 */
396   -int squashfs_read_table(struct super_block *sb, void *buffer, u64 block,
397   - int length)
  396 +void *squashfs_read_table(struct super_block *sb, u64 block, int length)
398 397 {
399 398 int pages = (length + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
400 399 int i, res;
401   - void **data = kcalloc(pages, sizeof(void *), GFP_KERNEL);
402   - if (data == NULL)
403   - return -ENOMEM;
  400 + void *table, *buffer, **data;
404 401  
  402 + table = buffer = kmalloc(length, GFP_KERNEL);
  403 + if (table == NULL)
  404 + return ERR_PTR(-ENOMEM);
  405 +
  406 + data = kcalloc(pages, sizeof(void *), GFP_KERNEL);
  407 + if (data == NULL) {
  408 + res = -ENOMEM;
  409 + goto failed;
  410 + }
  411 +
405 412 for (i = 0; i < pages; i++, buffer += PAGE_CACHE_SIZE)
406 413 data[i] = buffer;
  414 +
407 415 res = squashfs_read_data(sb, data, block, length |
408 416 SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, length, pages);
  417 +
409 418 kfree(data);
410   - return res;
  419 +
  420 + if (res < 0)
  421 + goto failed;
  422 +
  423 + return table;
  424 +
  425 +failed:
  426 + kfree(table);
  427 + return ERR_PTR(res);
411 428 }
fs/squashfs/export.c
... ... @@ -124,27 +124,10 @@
124 124 u64 lookup_table_start, unsigned int inodes)
125 125 {
126 126 unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
127   - __le64 *inode_lookup_table;
128   - int err;
129 127  
130 128 TRACE("In read_inode_lookup_table, length %d\n", length);
131 129  
132   - /* Allocate inode lookup table indexes */
133   - inode_lookup_table = kmalloc(length, GFP_KERNEL);
134   - if (inode_lookup_table == NULL) {
135   - ERROR("Failed to allocate inode lookup table\n");
136   - return ERR_PTR(-ENOMEM);
137   - }
138   -
139   - err = squashfs_read_table(sb, inode_lookup_table, lookup_table_start,
140   - length);
141   - if (err < 0) {
142   - ERROR("unable to read inode lookup table\n");
143   - kfree(inode_lookup_table);
144   - return ERR_PTR(err);
145   - }
146   -
147   - return inode_lookup_table;
  130 + return squashfs_read_table(sb, lookup_table_start, length);
148 131 }
149 132  
150 133  
fs/squashfs/fragment.c
... ... @@ -74,24 +74,7 @@
74 74 u64 fragment_table_start, unsigned int fragments)
75 75 {
76 76 unsigned int length = SQUASHFS_FRAGMENT_INDEX_BYTES(fragments);
77   - __le64 *fragment_index;
78   - int err;
79 77  
80   - /* Allocate fragment lookup table indexes */
81   - fragment_index = kmalloc(length, GFP_KERNEL);
82   - if (fragment_index == NULL) {
83   - ERROR("Failed to allocate fragment index table\n");
84   - return ERR_PTR(-ENOMEM);
85   - }
86   -
87   - err = squashfs_read_table(sb, fragment_index, fragment_table_start,
88   - length);
89   - if (err < 0) {
90   - ERROR("unable to read fragment index table\n");
91   - kfree(fragment_index);
92   - return ERR_PTR(err);
93   - }
94   -
95   - return fragment_index;
  78 + return squashfs_read_table(sb, fragment_table_start, length);
96 79 }
... ... @@ -69,25 +69,9 @@
69 69 u64 id_table_start, unsigned short no_ids)
70 70 {
71 71 unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
72   - __le64 *id_table;
73   - int err;
74 72  
75 73 TRACE("In read_id_index_table, length %d\n", length);
76 74  
77   - /* Allocate id lookup table indexes */
78   - id_table = kmalloc(length, GFP_KERNEL);
79   - if (id_table == NULL) {
80   - ERROR("Failed to allocate id index table\n");
81   - return ERR_PTR(-ENOMEM);
82   - }
83   -
84   - err = squashfs_read_table(sb, id_table, id_table_start, length);
85   - if (err < 0) {
86   - ERROR("unable to read id index table\n");
87   - kfree(id_table);
88   - return ERR_PTR(err);
89   - }
90   -
91   - return id_table;
  75 + return squashfs_read_table(sb, id_table_start, length);
92 76 }
fs/squashfs/squashfs.h
... ... @@ -44,7 +44,7 @@
44 44 u64, int);
45 45 extern struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *,
46 46 u64, int);
47   -extern int squashfs_read_table(struct super_block *, void *, u64, int);
  47 +extern void *squashfs_read_table(struct super_block *, u64, int);
48 48  
49 49 /* decompressor.c */
50 50 extern const struct squashfs_decompressor *squashfs_lookup_decompressor(int);
... ... @@ -95,12 +95,6 @@
95 95 }
96 96 msblk = sb->s_fs_info;
97 97  
98   - sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
99   - if (sblk == NULL) {
100   - ERROR("Failed to allocate squashfs_super_block\n");
101   - goto failure;
102   - }
103   -
104 98 msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE);
105 99 msblk->devblksize_log2 = ffz(~msblk->devblksize);
106 100  
107 101  
108 102  
... ... @@ -114,10 +108,12 @@
114 108 * of bytes_used) we need to set it to an initial sensible dummy value
115 109 */
116 110 msblk->bytes_used = sizeof(*sblk);
117   - err = squashfs_read_table(sb, sblk, SQUASHFS_START, sizeof(*sblk));
  111 + sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
118 112  
119   - if (err < 0) {
  113 + if (IS_ERR(sblk)) {
120 114 ERROR("unable to read squashfs_super_block\n");
  115 + err = PTR_ERR(sblk);
  116 + sblk = NULL;
121 117 goto failed_mount;
122 118 }
123 119  
... ... @@ -222,6 +218,7 @@
222 218 msblk->id_table = squashfs_read_id_index_table(sb,
223 219 le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids));
224 220 if (IS_ERR(msblk->id_table)) {
  221 + ERROR("unable to read id index table\n");
225 222 err = PTR_ERR(msblk->id_table);
226 223 msblk->id_table = NULL;
227 224 goto failed_mount;
... ... @@ -242,6 +239,7 @@
242 239 msblk->fragment_index = squashfs_read_fragment_index_table(sb,
243 240 le64_to_cpu(sblk->fragment_table_start), fragments);
244 241 if (IS_ERR(msblk->fragment_index)) {
  242 + ERROR("unable to read fragment index table\n");
245 243 err = PTR_ERR(msblk->fragment_index);
246 244 msblk->fragment_index = NULL;
247 245 goto failed_mount;
... ... @@ -256,6 +254,7 @@
256 254 msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
257 255 lookup_table_start, msblk->inodes);
258 256 if (IS_ERR(msblk->inode_lookup_table)) {
  257 + ERROR("unable to read inode lookup table\n");
259 258 err = PTR_ERR(msblk->inode_lookup_table);
260 259 msblk->inode_lookup_table = NULL;
261 260 goto failed_mount;
... ... @@ -273,6 +272,7 @@
273 272 msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
274 273 xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
275 274 if (IS_ERR(msblk->xattr_id_table)) {
  275 + ERROR("unable to read xattr id index table\n");
276 276 err = PTR_ERR(msblk->xattr_id_table);
277 277 msblk->xattr_id_table = NULL;
278 278 if (err != -ENOTSUPP)
... ... @@ -318,11 +318,6 @@
318 318 sb->s_fs_info = NULL;
319 319 kfree(sblk);
320 320 return err;
321   -
322   -failure:
323   - kfree(sb->s_fs_info);
324   - sb->s_fs_info = NULL;
325   - return -ENOMEM;
326 321 }
327 322  
328 323  
fs/squashfs/xattr_id.c
... ... @@ -67,35 +67,19 @@
67 67 u64 *xattr_table_start, int *xattr_ids)
68 68 {
69 69 unsigned int len;
70   - __le64 *xid_table;
71   - struct squashfs_xattr_id_table id_table;
72   - int err;
  70 + struct squashfs_xattr_id_table *id_table;
73 71  
74   - err = squashfs_read_table(sb, &id_table, start, sizeof(id_table));
75   - if (err < 0) {
76   - ERROR("unable to read xattr id table\n");
77   - return ERR_PTR(err);
78   - }
79   - *xattr_table_start = le64_to_cpu(id_table.xattr_table_start);
80   - *xattr_ids = le32_to_cpu(id_table.xattr_ids);
  72 + id_table = squashfs_read_table(sb, start, sizeof(*id_table));
  73 + if (IS_ERR(id_table))
  74 + return (__le64 *) id_table;
  75 +
  76 + *xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
  77 + *xattr_ids = le32_to_cpu(id_table->xattr_ids);
  78 + kfree(id_table);
81 79 len = SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids);
82 80  
83 81 TRACE("In read_xattr_index_table, length %d\n", len);
84 82  
85   - /* Allocate xattr id lookup table indexes */
86   - xid_table = kmalloc(len, GFP_KERNEL);
87   - if (xid_table == NULL) {
88   - ERROR("Failed to allocate xattr id index table\n");
89   - return ERR_PTR(-ENOMEM);
90   - }
91   -
92   - err = squashfs_read_table(sb, xid_table, start + sizeof(id_table), len);
93   - if (err < 0) {
94   - ERROR("unable to read xattr id index table\n");
95   - kfree(xid_table);
96   - return ERR_PTR(err);
97   - }
98   -
99   - return xid_table;
  83 + return squashfs_read_table(sb, start + sizeof(*id_table), len);
100 84 }