Blame view

fs/squashfs/inode.c 12.4 KB
6545b246a   Phillip Lougher   Squashfs: inode o...
1
2
3
4
  /*
   * Squashfs - a compressed read only filesystem for Linux
   *
   * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
d7f2ff671   Phillip Lougher   Squashfs: update ...
5
   * Phillip Lougher <phillip@squashfs.org.uk>
6545b246a   Phillip Lougher   Squashfs: inode o...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   *
   * inode.c
   */
  
  /*
   * This file implements code to create and read inodes from disk.
   *
   * Inodes in Squashfs are identified by a 48-bit inode which encodes the
   * location of the compressed metadata block containing the inode, and the byte
   * offset into that block where the inode is placed (<block, offset>).
   *
   * To maximise compression there are different inodes for each file type
   * (regular file, directory, device, etc.), the inode contents and length
   * varying with the type.
   *
   * To further maximise compression, two types of regular file inode and
   * directory inode are defined: inodes optimised for frequently occurring
   * regular files and directories, and extended types where extra
   * information has to be stored.
   */
  
  #include <linux/fs.h>
  #include <linux/vfs.h>
67f66cc6c   Phillip Lougher   squashfs: add new...
43
  #include <linux/xattr.h>
6545b246a   Phillip Lougher   Squashfs: inode o...
44
45
46
47
48
  
  #include "squashfs_fs.h"
  #include "squashfs_fs_sb.h"
  #include "squashfs_fs_i.h"
  #include "squashfs.h"
01e5b4e4e   Phillip Lougher   squashfs: add xat...
49
  #include "xattr.h"
6545b246a   Phillip Lougher   Squashfs: inode o...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  
  /*
   * Initialise VFS inode with the base inode information common to all
   * Squashfs inode types.  Sqsh_ino contains the unswapped base inode
   * off disk.
   */
  static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
  				struct squashfs_base_inode *sqsh_ino)
  {
  	int err;
  
  	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &inode->i_uid);
  	if (err)
  		return err;
  
  	err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &inode->i_gid);
  	if (err)
  		return err;
  
  	inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
  	inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
  	inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
  	inode->i_ctime.tv_sec = inode->i_mtime.tv_sec;
  	inode->i_mode = le16_to_cpu(sqsh_ino->mode);
  	inode->i_size = 0;
  
  	return err;
  }
  
  
  struct inode *squashfs_iget(struct super_block *sb, long long ino,
  				unsigned int ino_number)
  {
  	struct inode *inode = iget_locked(sb, ino_number);
  	int err;
  
  	TRACE("Entered squashfs_iget
  ");
  
  	if (!inode)
  		return ERR_PTR(-ENOMEM);
  	if (!(inode->i_state & I_NEW))
  		return inode;
  
  	err = squashfs_read_inode(inode, ino);
  	if (err) {
  		iget_failed(inode);
  		return ERR_PTR(err);
  	}
  
  	unlock_new_inode(inode);
  	return inode;
  }
  
  
  /*
   * Initialise VFS inode by reading inode from inode table (compressed
   * metadata).  The format and amount of data read depends on type.
   */
  int squashfs_read_inode(struct inode *inode, long long ino)
  {
  	struct super_block *sb = inode->i_sb;
  	struct squashfs_sb_info *msblk = sb->s_fs_info;
  	u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  	int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
  	union squashfs_inode squashfs_ino;
  	struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
67f66cc6c   Phillip Lougher   squashfs: add new...
117
  	int xattr_id = SQUASHFS_INVALID_XATTR;
6545b246a   Phillip Lougher   Squashfs: inode o...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  
  	TRACE("Entered squashfs_read_inode
  ");
  
  	/*
  	 * Read inode base common to all inode types.
  	 */
  	err = squashfs_read_metadata(sb, sqshb_ino, &block,
  				&offset, sizeof(*sqshb_ino));
  	if (err < 0)
  		goto failed_read;
  
  	err = squashfs_new_inode(sb, inode, sqshb_ino);
  	if (err)
  		goto failed_read;
  
  	block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  	offset = SQUASHFS_INODE_OFFSET(ino);
  
  	type = le16_to_cpu(sqshb_ino->inode_type);
  	switch (type) {
  	case SQUASHFS_REG_TYPE: {
f4f8056a8   Roel Kluin   Squashfs: frag_si...
140
141
  		unsigned int frag_offset, frag;
  		int frag_size;
6545b246a   Phillip Lougher   Squashfs: inode o...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  		u64 frag_blk;
  		struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  							sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
  
  		frag = le32_to_cpu(sqsh_ino->fragment);
  		if (frag != SQUASHFS_INVALID_FRAG) {
  			frag_offset = le32_to_cpu(sqsh_ino->offset);
  			frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  			if (frag_size < 0) {
  				err = frag_size;
  				goto failed_read;
  			}
  		} else {
  			frag_blk = SQUASHFS_INVALID_BLK;
  			frag_size = 0;
  			frag_offset = 0;
  		}
bfe868486   Miklos Szeredi   filesystems: add ...
163
  		set_nlink(inode, 1);
6545b246a   Phillip Lougher   Squashfs: inode o...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  		inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  		inode->i_fop = &generic_ro_fops;
  		inode->i_mode |= S_IFREG;
  		inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
  		squashfs_i(inode)->fragment_block = frag_blk;
  		squashfs_i(inode)->fragment_size = frag_size;
  		squashfs_i(inode)->fragment_offset = frag_offset;
  		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  		squashfs_i(inode)->block_list_start = block;
  		squashfs_i(inode)->offset = offset;
  		inode->i_data.a_ops = &squashfs_aops;
  
  		TRACE("File inode %x:%x, start_block %llx, block_list_start "
  			"%llx, offset %x
  ", SQUASHFS_INODE_BLK(ino),
  			offset, squashfs_i(inode)->start, block, offset);
  		break;
  	}
  	case SQUASHFS_LREG_TYPE: {
f4f8056a8   Roel Kluin   Squashfs: frag_si...
183
184
  		unsigned int frag_offset, frag;
  		int frag_size;
6545b246a   Phillip Lougher   Squashfs: inode o...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  		u64 frag_blk;
  		struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  							sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
  
  		frag = le32_to_cpu(sqsh_ino->fragment);
  		if (frag != SQUASHFS_INVALID_FRAG) {
  			frag_offset = le32_to_cpu(sqsh_ino->offset);
  			frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  			if (frag_size < 0) {
  				err = frag_size;
  				goto failed_read;
  			}
  		} else {
  			frag_blk = SQUASHFS_INVALID_BLK;
  			frag_size = 0;
  			frag_offset = 0;
  		}
67f66cc6c   Phillip Lougher   squashfs: add new...
206
  		xattr_id = le32_to_cpu(sqsh_ino->xattr);
bfe868486   Miklos Szeredi   filesystems: add ...
207
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
6545b246a   Phillip Lougher   Squashfs: inode o...
208
  		inode->i_size = le64_to_cpu(sqsh_ino->file_size);
67f66cc6c   Phillip Lougher   squashfs: add new...
209
  		inode->i_op = &squashfs_inode_ops;
6545b246a   Phillip Lougher   Squashfs: inode o...
210
211
  		inode->i_fop = &generic_ro_fops;
  		inode->i_mode |= S_IFREG;
3d4a1c80c   Phillip Lougher   Squashfs: fix i_b...
212
213
  		inode->i_blocks = (inode->i_size -
  				le64_to_cpu(sqsh_ino->sparse) + 511) >> 9;
6545b246a   Phillip Lougher   Squashfs: inode o...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  
  		squashfs_i(inode)->fragment_block = frag_blk;
  		squashfs_i(inode)->fragment_size = frag_size;
  		squashfs_i(inode)->fragment_offset = frag_offset;
  		squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  		squashfs_i(inode)->block_list_start = block;
  		squashfs_i(inode)->offset = offset;
  		inode->i_data.a_ops = &squashfs_aops;
  
  		TRACE("File inode %x:%x, start_block %llx, block_list_start "
  			"%llx, offset %x
  ", SQUASHFS_INODE_BLK(ino),
  			offset, squashfs_i(inode)->start, block, offset);
  		break;
  	}
  	case SQUASHFS_DIR_TYPE: {
  		struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
bfe868486   Miklos Szeredi   filesystems: add ...
236
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
6545b246a   Phillip Lougher   Squashfs: inode o...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  		inode->i_size = le16_to_cpu(sqsh_ino->file_size);
  		inode->i_op = &squashfs_dir_inode_ops;
  		inode->i_fop = &squashfs_dir_ops;
  		inode->i_mode |= S_IFDIR;
  		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  		squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  		squashfs_i(inode)->dir_idx_cnt = 0;
  		squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  
  		TRACE("Directory inode %x:%x, start_block %llx, offset %x
  ",
  				SQUASHFS_INODE_BLK(ino), offset,
  				squashfs_i(inode)->start,
  				le16_to_cpu(sqsh_ino->offset));
  		break;
  	}
  	case SQUASHFS_LDIR_TYPE: {
  		struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
67f66cc6c   Phillip Lougher   squashfs: add new...
260
  		xattr_id = le32_to_cpu(sqsh_ino->xattr);
bfe868486   Miklos Szeredi   filesystems: add ...
261
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
6545b246a   Phillip Lougher   Squashfs: inode o...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  		inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  		inode->i_op = &squashfs_dir_inode_ops;
  		inode->i_fop = &squashfs_dir_ops;
  		inode->i_mode |= S_IFDIR;
  		squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  		squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  		squashfs_i(inode)->dir_idx_start = block;
  		squashfs_i(inode)->dir_idx_offset = offset;
  		squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  		squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  
  		TRACE("Long directory inode %x:%x, start_block %llx, offset "
  				"%x
  ", SQUASHFS_INODE_BLK(ino), offset,
  				squashfs_i(inode)->start,
  				le16_to_cpu(sqsh_ino->offset));
  		break;
  	}
  	case SQUASHFS_SYMLINK_TYPE:
  	case SQUASHFS_LSYMLINK_TYPE: {
  		struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
bfe868486   Miklos Szeredi   filesystems: add ...
288
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
6545b246a   Phillip Lougher   Squashfs: inode o...
289
  		inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
67f66cc6c   Phillip Lougher   squashfs: add new...
290
  		inode->i_op = &squashfs_symlink_inode_ops;
6545b246a   Phillip Lougher   Squashfs: inode o...
291
292
293
294
  		inode->i_data.a_ops = &squashfs_symlink_aops;
  		inode->i_mode |= S_IFLNK;
  		squashfs_i(inode)->start = block;
  		squashfs_i(inode)->offset = offset;
67f66cc6c   Phillip Lougher   squashfs: add new...
295
296
297
298
299
300
301
302
303
304
305
306
307
  		if (type == SQUASHFS_LSYMLINK_TYPE) {
  			__le32 xattr;
  
  			err = squashfs_read_metadata(sb, NULL, &block,
  						&offset, inode->i_size);
  			if (err < 0)
  				goto failed_read;
  			err = squashfs_read_metadata(sb, &xattr, &block,
  						&offset, sizeof(xattr));
  			if (err < 0)
  				goto failed_read;
  			xattr_id = le32_to_cpu(xattr);
  		}
6545b246a   Phillip Lougher   Squashfs: inode o...
308
309
310
311
312
313
314
  		TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  				"%x
  ", SQUASHFS_INODE_BLK(ino), offset,
  				block, offset);
  		break;
  	}
  	case SQUASHFS_BLKDEV_TYPE:
67f66cc6c   Phillip Lougher   squashfs: add new...
315
  	case SQUASHFS_CHRDEV_TYPE: {
6545b246a   Phillip Lougher   Squashfs: inode o...
316
317
318
319
320
321
322
323
324
325
326
327
  		struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  		unsigned int rdev;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
  
  		if (type == SQUASHFS_CHRDEV_TYPE)
  			inode->i_mode |= S_IFCHR;
  		else
  			inode->i_mode |= S_IFBLK;
bfe868486   Miklos Szeredi   filesystems: add ...
328
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
6545b246a   Phillip Lougher   Squashfs: inode o...
329
330
331
332
333
334
335
336
  		rdev = le32_to_cpu(sqsh_ino->rdev);
  		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  
  		TRACE("Device inode %x:%x, rdev %x
  ",
  				SQUASHFS_INODE_BLK(ino), offset, rdev);
  		break;
  	}
67f66cc6c   Phillip Lougher   squashfs: add new...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  	case SQUASHFS_LBLKDEV_TYPE:
  	case SQUASHFS_LCHRDEV_TYPE: {
  		struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
  		unsigned int rdev;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
  
  		if (type == SQUASHFS_LCHRDEV_TYPE)
  			inode->i_mode |= S_IFCHR;
  		else
  			inode->i_mode |= S_IFBLK;
  		xattr_id = le32_to_cpu(sqsh_ino->xattr);
  		inode->i_op = &squashfs_inode_ops;
bfe868486   Miklos Szeredi   filesystems: add ...
353
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
67f66cc6c   Phillip Lougher   squashfs: add new...
354
355
356
357
358
359
360
361
  		rdev = le32_to_cpu(sqsh_ino->rdev);
  		init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  
  		TRACE("Device inode %x:%x, rdev %x
  ",
  				SQUASHFS_INODE_BLK(ino), offset, rdev);
  		break;
  	}
6545b246a   Phillip Lougher   Squashfs: inode o...
362
  	case SQUASHFS_FIFO_TYPE:
67f66cc6c   Phillip Lougher   squashfs: add new...
363
  	case SQUASHFS_SOCKET_TYPE: {
6545b246a   Phillip Lougher   Squashfs: inode o...
364
365
366
367
368
369
370
371
372
373
374
  		struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
  
  		if (type == SQUASHFS_FIFO_TYPE)
  			inode->i_mode |= S_IFIFO;
  		else
  			inode->i_mode |= S_IFSOCK;
bfe868486   Miklos Szeredi   filesystems: add ...
375
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
6545b246a   Phillip Lougher   Squashfs: inode o...
376
377
378
  		init_special_inode(inode, inode->i_mode, 0);
  		break;
  	}
67f66cc6c   Phillip Lougher   squashfs: add new...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  	case SQUASHFS_LFIFO_TYPE:
  	case SQUASHFS_LSOCKET_TYPE: {
  		struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
  
  		err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  				sizeof(*sqsh_ino));
  		if (err < 0)
  			goto failed_read;
  
  		if (type == SQUASHFS_LFIFO_TYPE)
  			inode->i_mode |= S_IFIFO;
  		else
  			inode->i_mode |= S_IFSOCK;
  		xattr_id = le32_to_cpu(sqsh_ino->xattr);
  		inode->i_op = &squashfs_inode_ops;
bfe868486   Miklos Szeredi   filesystems: add ...
394
  		set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
67f66cc6c   Phillip Lougher   squashfs: add new...
395
396
397
  		init_special_inode(inode, inode->i_mode, 0);
  		break;
  	}
6545b246a   Phillip Lougher   Squashfs: inode o...
398
399
400
401
402
  	default:
  		ERROR("Unknown inode type %d in squashfs_iget!
  ", type);
  		return -EINVAL;
  	}
67f66cc6c   Phillip Lougher   squashfs: add new...
403
404
405
406
407
408
409
410
411
412
413
  	if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
  		err = squashfs_xattr_lookup(sb, xattr_id,
  					&squashfs_i(inode)->xattr_count,
  					&squashfs_i(inode)->xattr_size,
  					&squashfs_i(inode)->xattr);
  		if (err < 0)
  			goto failed_read;
  		inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
  				+ 1;
  	} else
  		squashfs_i(inode)->xattr_count = 0;
6545b246a   Phillip Lougher   Squashfs: inode o...
414
415
416
417
418
419
420
  	return 0;
  
  failed_read:
  	ERROR("Unable to read inode 0x%llx
  ", ino);
  	return err;
  }
67f66cc6c   Phillip Lougher   squashfs: add new...
421
422
423
424
425
426
  
  
  const struct inode_operations squashfs_inode_ops = {
  	.getxattr = generic_getxattr,
  	.listxattr = squashfs_listxattr
  };