Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * super.c | |
3 | * | |
4 | * PURPOSE | |
5 | * Super block routines for the OSTA-UDF(tm) filesystem. | |
6 | * | |
7 | * DESCRIPTION | |
8 | * OSTA-UDF(tm) = Optical Storage Technology Association | |
9 | * Universal Disk Format. | |
10 | * | |
11 | * This code is based on version 2.00 of the UDF specification, | |
12 | * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346]. | |
13 | * http://www.osta.org/ | |
14 | * http://www.ecma.ch/ | |
15 | * http://www.iso.org/ | |
16 | * | |
1da177e4 LT |
17 | * COPYRIGHT |
18 | * This file is distributed under the terms of the GNU General Public | |
19 | * License (GPL). Copies of the GPL can be obtained from: | |
20 | * ftp://prep.ai.mit.edu/pub/gnu/GPL | |
21 | * Each contributing author retains all rights to their own work. | |
22 | * | |
23 | * (C) 1998 Dave Boynton | |
24 | * (C) 1998-2004 Ben Fennema | |
25 | * (C) 2000 Stelias Computing Inc | |
26 | * | |
27 | * HISTORY | |
28 | * | |
29 | * 09/24/98 dgb changed to allow compiling outside of kernel, and | |
30 | * added some debugging. | |
31 | * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34 | |
32 | * 10/16/98 attempting some multi-session support | |
33 | * 10/17/98 added freespace count for "df" | |
34 | * 11/11/98 gr added novrs option | |
35 | * 11/26/98 dgb added fileset,anchor mount options | |
36 | * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced vol descs | |
37 | * rewrote option handling based on isofs | |
38 | * 12/20/98 find the free space bitmap (if it exists) | |
39 | */ | |
40 | ||
cb00ea35 | 41 | #include "udfdecl.h" |
1da177e4 | 42 | |
1da177e4 LT |
43 | #include <linux/blkdev.h> |
44 | #include <linux/slab.h> | |
45 | #include <linux/kernel.h> | |
46 | #include <linux/module.h> | |
47 | #include <linux/parser.h> | |
48 | #include <linux/stat.h> | |
49 | #include <linux/cdrom.h> | |
50 | #include <linux/nls.h> | |
51 | #include <linux/smp_lock.h> | |
52 | #include <linux/buffer_head.h> | |
53 | #include <linux/vfs.h> | |
54 | #include <linux/vmalloc.h> | |
55 | #include <asm/byteorder.h> | |
56 | ||
57 | #include <linux/udf_fs.h> | |
58 | #include "udf_sb.h" | |
59 | #include "udf_i.h" | |
60 | ||
61 | #include <linux/init.h> | |
62 | #include <asm/uaccess.h> | |
63 | ||
64 | #define VDS_POS_PRIMARY_VOL_DESC 0 | |
65 | #define VDS_POS_UNALLOC_SPACE_DESC 1 | |
66 | #define VDS_POS_LOGICAL_VOL_DESC 2 | |
67 | #define VDS_POS_PARTITION_DESC 3 | |
68 | #define VDS_POS_IMP_USE_VOL_DESC 4 | |
69 | #define VDS_POS_VOL_DESC_PTR 5 | |
70 | #define VDS_POS_TERMINATING_DESC 6 | |
71 | #define VDS_POS_LENGTH 7 | |
72 | ||
73 | static char error_buf[1024]; | |
74 | ||
75 | /* These are the "meat" - everything else is stuffing */ | |
76 | static int udf_fill_super(struct super_block *, void *, int); | |
77 | static void udf_put_super(struct super_block *); | |
78 | static void udf_write_super(struct super_block *); | |
79 | static int udf_remount_fs(struct super_block *, int *, char *); | |
80 | static int udf_check_valid(struct super_block *, int, int); | |
81 | static int udf_vrs(struct super_block *sb, int silent); | |
82 | static int udf_load_partition(struct super_block *, kernel_lb_addr *); | |
cb00ea35 CG |
83 | static int udf_load_logicalvol(struct super_block *, struct buffer_head *, |
84 | kernel_lb_addr *); | |
1da177e4 LT |
85 | static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad); |
86 | static void udf_find_anchor(struct super_block *); | |
cb00ea35 CG |
87 | static int udf_find_fileset(struct super_block *, kernel_lb_addr *, |
88 | kernel_lb_addr *); | |
1da177e4 | 89 | static void udf_load_pvoldesc(struct super_block *, struct buffer_head *); |
cb00ea35 CG |
90 | static void udf_load_fileset(struct super_block *, struct buffer_head *, |
91 | kernel_lb_addr *); | |
1da177e4 LT |
92 | static void udf_load_partdesc(struct super_block *, struct buffer_head *); |
93 | static void udf_open_lvid(struct super_block *); | |
94 | static void udf_close_lvid(struct super_block *); | |
95 | static unsigned int udf_count_free(struct super_block *); | |
726c3342 | 96 | static int udf_statfs(struct dentry *, struct kstatfs *); |
1da177e4 LT |
97 | |
98 | /* UDF filesystem type */ | |
454e2398 | 99 | static int udf_get_sb(struct file_system_type *fs_type, |
cb00ea35 CG |
100 | int flags, const char *dev_name, void *data, |
101 | struct vfsmount *mnt) | |
1da177e4 | 102 | { |
454e2398 | 103 | return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt); |
1da177e4 LT |
104 | } |
105 | ||
106 | static struct file_system_type udf_fstype = { | |
28de7948 CG |
107 | .owner = THIS_MODULE, |
108 | .name = "udf", | |
109 | .get_sb = udf_get_sb, | |
110 | .kill_sb = kill_block_super, | |
111 | .fs_flags = FS_REQUIRES_DEV, | |
1da177e4 LT |
112 | }; |
113 | ||
cb00ea35 | 114 | static struct kmem_cache *udf_inode_cachep; |
1da177e4 LT |
115 | |
116 | static struct inode *udf_alloc_inode(struct super_block *sb) | |
117 | { | |
118 | struct udf_inode_info *ei; | |
28de7948 | 119 | ei = (struct udf_inode_info *)kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
120 | if (!ei) |
121 | return NULL; | |
95f8797f DB |
122 | |
123 | ei->i_unique = 0; | |
124 | ei->i_lenExtents = 0; | |
125 | ei->i_next_alloc_block = 0; | |
126 | ei->i_next_alloc_goal = 0; | |
127 | ei->i_strat4096 = 0; | |
128 | ||
1da177e4 LT |
129 | return &ei->vfs_inode; |
130 | } | |
131 | ||
132 | static void udf_destroy_inode(struct inode *inode) | |
133 | { | |
134 | kmem_cache_free(udf_inode_cachep, UDF_I(inode)); | |
135 | } | |
136 | ||
cb00ea35 | 137 | static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags) |
1da177e4 | 138 | { |
cb00ea35 | 139 | struct udf_inode_info *ei = (struct udf_inode_info *)foo; |
1da177e4 | 140 | |
a35afb83 CL |
141 | ei->i_ext.i_data = NULL; |
142 | inode_init_once(&ei->vfs_inode); | |
1da177e4 LT |
143 | } |
144 | ||
145 | static int init_inodecache(void) | |
146 | { | |
147 | udf_inode_cachep = kmem_cache_create("udf_inode_cache", | |
148 | sizeof(struct udf_inode_info), | |
cb00ea35 CG |
149 | 0, (SLAB_RECLAIM_ACCOUNT | |
150 | SLAB_MEM_SPREAD), | |
20c2df83 | 151 | init_once); |
28de7948 | 152 | if (!udf_inode_cachep) |
1da177e4 LT |
153 | return -ENOMEM; |
154 | return 0; | |
155 | } | |
156 | ||
157 | static void destroy_inodecache(void) | |
158 | { | |
1a1d92c1 | 159 | kmem_cache_destroy(udf_inode_cachep); |
1da177e4 LT |
160 | } |
161 | ||
162 | /* Superblock operations */ | |
ee9b6d61 | 163 | static const struct super_operations udf_sb_ops = { |
28de7948 CG |
164 | .alloc_inode = udf_alloc_inode, |
165 | .destroy_inode = udf_destroy_inode, | |
166 | .write_inode = udf_write_inode, | |
167 | .delete_inode = udf_delete_inode, | |
168 | .clear_inode = udf_clear_inode, | |
169 | .put_super = udf_put_super, | |
170 | .write_super = udf_write_super, | |
171 | .statfs = udf_statfs, | |
172 | .remount_fs = udf_remount_fs, | |
1da177e4 LT |
173 | }; |
174 | ||
cb00ea35 | 175 | struct udf_options { |
1da177e4 LT |
176 | unsigned char novrs; |
177 | unsigned int blocksize; | |
178 | unsigned int session; | |
179 | unsigned int lastblock; | |
180 | unsigned int anchor; | |
181 | unsigned int volume; | |
182 | unsigned short partition; | |
183 | unsigned int fileset; | |
184 | unsigned int rootdir; | |
185 | unsigned int flags; | |
186 | mode_t umask; | |
187 | gid_t gid; | |
188 | uid_t uid; | |
189 | struct nls_table *nls_map; | |
190 | }; | |
191 | ||
192 | static int __init init_udf_fs(void) | |
193 | { | |
194 | int err; | |
28de7948 | 195 | |
1da177e4 LT |
196 | err = init_inodecache(); |
197 | if (err) | |
198 | goto out1; | |
199 | err = register_filesystem(&udf_fstype); | |
200 | if (err) | |
201 | goto out; | |
28de7948 | 202 | |
1da177e4 | 203 | return 0; |
28de7948 CG |
204 | |
205 | out: | |
1da177e4 | 206 | destroy_inodecache(); |
28de7948 CG |
207 | |
208 | out1: | |
1da177e4 LT |
209 | return err; |
210 | } | |
211 | ||
212 | static void __exit exit_udf_fs(void) | |
213 | { | |
214 | unregister_filesystem(&udf_fstype); | |
215 | destroy_inodecache(); | |
216 | } | |
217 | ||
218 | module_init(init_udf_fs) | |
28de7948 | 219 | module_exit(exit_udf_fs) |
1da177e4 LT |
220 | |
221 | /* | |
222 | * udf_parse_options | |
223 | * | |
224 | * PURPOSE | |
225 | * Parse mount options. | |
226 | * | |
227 | * DESCRIPTION | |
228 | * The following mount options are supported: | |
229 | * | |
230 | * gid= Set the default group. | |
231 | * umask= Set the default umask. | |
232 | * uid= Set the default user. | |
233 | * bs= Set the block size. | |
234 | * unhide Show otherwise hidden files. | |
235 | * undelete Show deleted files in lists. | |
236 | * adinicb Embed data in the inode (default) | |
237 | * noadinicb Don't embed data in the inode | |
238 | * shortad Use short ad's | |
239 | * longad Use long ad's (default) | |
240 | * nostrict Unset strict conformance | |
241 | * iocharset= Set the NLS character set | |
242 | * | |
243 | * The remaining are for debugging and disaster recovery: | |
244 | * | |
28de7948 | 245 | * novrs Skip volume sequence recognition |
1da177e4 LT |
246 | * |
247 | * The following expect a offset from 0. | |
248 | * | |
249 | * session= Set the CDROM session (default= last session) | |
250 | * anchor= Override standard anchor location. (default= 256) | |
251 | * volume= Override the VolumeDesc location. (unused) | |
252 | * partition= Override the PartitionDesc location. (unused) | |
253 | * lastblock= Set the last block of the filesystem/ | |
254 | * | |
255 | * The following expect a offset from the partition root. | |
256 | * | |
257 | * fileset= Override the fileset block location. (unused) | |
258 | * rootdir= Override the root directory location. (unused) | |
259 | * WARNING: overriding the rootdir to a non-directory may | |
260 | * yield highly unpredictable results. | |
261 | * | |
262 | * PRE-CONDITIONS | |
263 | * options Pointer to mount options string. | |
264 | * uopts Pointer to mount options variable. | |
265 | * | |
266 | * POST-CONDITIONS | |
267 | * <return> 1 Mount options parsed okay. | |
268 | * <return> 0 Error parsing mount options. | |
269 | * | |
270 | * HISTORY | |
271 | * July 1, 1997 - Andrew E. Mileski | |
272 | * Written, tested, and released. | |
273 | */ | |
28de7948 | 274 | |
1da177e4 LT |
275 | enum { |
276 | Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete, | |
277 | Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad, | |
278 | Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock, | |
279 | Opt_anchor, Opt_volume, Opt_partition, Opt_fileset, | |
280 | Opt_rootdir, Opt_utf8, Opt_iocharset, | |
4d6660eb | 281 | Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore |
1da177e4 LT |
282 | }; |
283 | ||
284 | static match_table_t tokens = { | |
28de7948 CG |
285 | {Opt_novrs, "novrs"}, |
286 | {Opt_nostrict, "nostrict"}, | |
287 | {Opt_bs, "bs=%u"}, | |
288 | {Opt_unhide, "unhide"}, | |
289 | {Opt_undelete, "undelete"}, | |
290 | {Opt_noadinicb, "noadinicb"}, | |
291 | {Opt_adinicb, "adinicb"}, | |
292 | {Opt_shortad, "shortad"}, | |
293 | {Opt_longad, "longad"}, | |
294 | {Opt_uforget, "uid=forget"}, | |
295 | {Opt_uignore, "uid=ignore"}, | |
296 | {Opt_gforget, "gid=forget"}, | |
297 | {Opt_gignore, "gid=ignore"}, | |
298 | {Opt_gid, "gid=%u"}, | |
299 | {Opt_uid, "uid=%u"}, | |
300 | {Opt_umask, "umask=%o"}, | |
301 | {Opt_session, "session=%u"}, | |
302 | {Opt_lastblock, "lastblock=%u"}, | |
303 | {Opt_anchor, "anchor=%u"}, | |
304 | {Opt_volume, "volume=%u"}, | |
305 | {Opt_partition, "partition=%u"}, | |
306 | {Opt_fileset, "fileset=%u"}, | |
307 | {Opt_rootdir, "rootdir=%u"}, | |
308 | {Opt_utf8, "utf8"}, | |
309 | {Opt_iocharset, "iocharset=%s"}, | |
310 | {Opt_err, NULL} | |
1da177e4 LT |
311 | }; |
312 | ||
cb00ea35 | 313 | static int udf_parse_options(char *options, struct udf_options *uopt) |
1da177e4 LT |
314 | { |
315 | char *p; | |
316 | int option; | |
317 | ||
318 | uopt->novrs = 0; | |
319 | uopt->blocksize = 2048; | |
320 | uopt->partition = 0xFFFF; | |
321 | uopt->session = 0xFFFFFFFF; | |
322 | uopt->lastblock = 0; | |
323 | uopt->anchor = 0; | |
324 | uopt->volume = 0xFFFFFFFF; | |
325 | uopt->rootdir = 0xFFFFFFFF; | |
326 | uopt->fileset = 0xFFFFFFFF; | |
327 | uopt->nls_map = NULL; | |
328 | ||
329 | if (!options) | |
330 | return 1; | |
331 | ||
cb00ea35 | 332 | while ((p = strsep(&options, ",")) != NULL) { |
1da177e4 LT |
333 | substring_t args[MAX_OPT_ARGS]; |
334 | int token; | |
335 | if (!*p) | |
336 | continue; | |
337 | ||
338 | token = match_token(p, tokens, args); | |
cb00ea35 CG |
339 | switch (token) { |
340 | case Opt_novrs: | |
341 | uopt->novrs = 1; | |
342 | case Opt_bs: | |
343 | if (match_int(&args[0], &option)) | |
344 | return 0; | |
345 | uopt->blocksize = option; | |
346 | break; | |
347 | case Opt_unhide: | |
348 | uopt->flags |= (1 << UDF_FLAG_UNHIDE); | |
349 | break; | |
350 | case Opt_undelete: | |
351 | uopt->flags |= (1 << UDF_FLAG_UNDELETE); | |
352 | break; | |
353 | case Opt_noadinicb: | |
354 | uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB); | |
355 | break; | |
356 | case Opt_adinicb: | |
357 | uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB); | |
358 | break; | |
359 | case Opt_shortad: | |
360 | uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD); | |
361 | break; | |
362 | case Opt_longad: | |
363 | uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD); | |
364 | break; | |
365 | case Opt_gid: | |
366 | if (match_int(args, &option)) | |
367 | return 0; | |
368 | uopt->gid = option; | |
369 | break; | |
370 | case Opt_uid: | |
371 | if (match_int(args, &option)) | |
372 | return 0; | |
373 | uopt->uid = option; | |
374 | break; | |
375 | case Opt_umask: | |
376 | if (match_octal(args, &option)) | |
377 | return 0; | |
378 | uopt->umask = option; | |
379 | break; | |
380 | case Opt_nostrict: | |
381 | uopt->flags &= ~(1 << UDF_FLAG_STRICT); | |
382 | break; | |
383 | case Opt_session: | |
384 | if (match_int(args, &option)) | |
385 | return 0; | |
386 | uopt->session = option; | |
387 | break; | |
388 | case Opt_lastblock: | |
389 | if (match_int(args, &option)) | |
390 | return 0; | |
391 | uopt->lastblock = option; | |
392 | break; | |
393 | case Opt_anchor: | |
394 | if (match_int(args, &option)) | |
395 | return 0; | |
396 | uopt->anchor = option; | |
397 | break; | |
398 | case Opt_volume: | |
399 | if (match_int(args, &option)) | |
400 | return 0; | |
401 | uopt->volume = option; | |
402 | break; | |
403 | case Opt_partition: | |
404 | if (match_int(args, &option)) | |
405 | return 0; | |
406 | uopt->partition = option; | |
407 | break; | |
408 | case Opt_fileset: | |
409 | if (match_int(args, &option)) | |
410 | return 0; | |
411 | uopt->fileset = option; | |
412 | break; | |
413 | case Opt_rootdir: | |
414 | if (match_int(args, &option)) | |
415 | return 0; | |
416 | uopt->rootdir = option; | |
417 | break; | |
418 | case Opt_utf8: | |
419 | uopt->flags |= (1 << UDF_FLAG_UTF8); | |
420 | break; | |
1da177e4 | 421 | #ifdef CONFIG_UDF_NLS |
cb00ea35 CG |
422 | case Opt_iocharset: |
423 | uopt->nls_map = load_nls(args[0].from); | |
424 | uopt->flags |= (1 << UDF_FLAG_NLS_MAP); | |
425 | break; | |
1da177e4 | 426 | #endif |
cb00ea35 CG |
427 | case Opt_uignore: |
428 | uopt->flags |= (1 << UDF_FLAG_UID_IGNORE); | |
429 | break; | |
430 | case Opt_uforget: | |
431 | uopt->flags |= (1 << UDF_FLAG_UID_FORGET); | |
432 | break; | |
433 | case Opt_gignore: | |
434 | uopt->flags |= (1 << UDF_FLAG_GID_IGNORE); | |
435 | break; | |
436 | case Opt_gforget: | |
437 | uopt->flags |= (1 << UDF_FLAG_GID_FORGET); | |
438 | break; | |
439 | default: | |
440 | printk(KERN_ERR "udf: bad mount option \"%s\" " | |
441 | "or missing value\n", p); | |
1da177e4 LT |
442 | return 0; |
443 | } | |
444 | } | |
445 | return 1; | |
446 | } | |
447 | ||
cb00ea35 | 448 | void udf_write_super(struct super_block *sb) |
1da177e4 LT |
449 | { |
450 | lock_kernel(); | |
28de7948 | 451 | |
1da177e4 LT |
452 | if (!(sb->s_flags & MS_RDONLY)) |
453 | udf_open_lvid(sb); | |
454 | sb->s_dirt = 0; | |
28de7948 | 455 | |
1da177e4 LT |
456 | unlock_kernel(); |
457 | } | |
458 | ||
cb00ea35 | 459 | static int udf_remount_fs(struct super_block *sb, int *flags, char *options) |
1da177e4 LT |
460 | { |
461 | struct udf_options uopt; | |
462 | ||
cb00ea35 | 463 | uopt.flags = UDF_SB(sb)->s_flags; |
28de7948 CG |
464 | uopt.uid = UDF_SB(sb)->s_uid; |
465 | uopt.gid = UDF_SB(sb)->s_gid; | |
cb00ea35 | 466 | uopt.umask = UDF_SB(sb)->s_umask; |
1da177e4 | 467 | |
cb00ea35 | 468 | if (!udf_parse_options(options, &uopt)) |
1da177e4 LT |
469 | return -EINVAL; |
470 | ||
471 | UDF_SB(sb)->s_flags = uopt.flags; | |
28de7948 CG |
472 | UDF_SB(sb)->s_uid = uopt.uid; |
473 | UDF_SB(sb)->s_gid = uopt.gid; | |
1da177e4 LT |
474 | UDF_SB(sb)->s_umask = uopt.umask; |
475 | ||
476 | if (UDF_SB_LVIDBH(sb)) { | |
477 | int write_rev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev); | |
478 | if (write_rev > UDF_MAX_WRITE_VERSION) | |
479 | *flags |= MS_RDONLY; | |
480 | } | |
481 | ||
482 | if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) | |
483 | return 0; | |
484 | if (*flags & MS_RDONLY) | |
485 | udf_close_lvid(sb); | |
486 | else | |
487 | udf_open_lvid(sb); | |
488 | ||
489 | return 0; | |
490 | } | |
491 | ||
492 | /* | |
493 | * udf_set_blocksize | |
494 | * | |
495 | * PURPOSE | |
496 | * Set the block size to be used in all transfers. | |
497 | * | |
498 | * DESCRIPTION | |
499 | * To allow room for a DMA transfer, it is best to guess big when unsure. | |
500 | * This routine picks 2048 bytes as the blocksize when guessing. This | |
501 | * should be adequate until devices with larger block sizes become common. | |
502 | * | |
503 | * Note that the Linux kernel can currently only deal with blocksizes of | |
504 | * 512, 1024, 2048, 4096, and 8192 bytes. | |
505 | * | |
506 | * PRE-CONDITIONS | |
507 | * sb Pointer to _locked_ superblock. | |
508 | * | |
509 | * POST-CONDITIONS | |
510 | * sb->s_blocksize Blocksize. | |
511 | * sb->s_blocksize_bits log2 of blocksize. | |
512 | * <return> 0 Blocksize is valid. | |
513 | * <return> 1 Blocksize is invalid. | |
514 | * | |
515 | * HISTORY | |
516 | * July 1, 1997 - Andrew E. Mileski | |
517 | * Written, tested, and released. | |
518 | */ | |
cb00ea35 | 519 | static int udf_set_blocksize(struct super_block *sb, int bsize) |
1da177e4 LT |
520 | { |
521 | if (!sb_min_blocksize(sb, bsize)) { | |
522 | udf_debug("Bad block size (%d)\n", bsize); | |
523 | printk(KERN_ERR "udf: bad block size (%d)\n", bsize); | |
524 | return 0; | |
525 | } | |
28de7948 | 526 | |
1da177e4 LT |
527 | return sb->s_blocksize; |
528 | } | |
529 | ||
cb00ea35 | 530 | static int udf_vrs(struct super_block *sb, int silent) |
1da177e4 LT |
531 | { |
532 | struct volStructDesc *vsd = NULL; | |
533 | int sector = 32768; | |
534 | int sectorsize; | |
535 | struct buffer_head *bh = NULL; | |
cb00ea35 CG |
536 | int iso9660 = 0; |
537 | int nsr02 = 0; | |
538 | int nsr03 = 0; | |
1da177e4 LT |
539 | |
540 | /* Block size must be a multiple of 512 */ | |
541 | if (sb->s_blocksize & 511) | |
542 | return 0; | |
543 | ||
544 | if (sb->s_blocksize < sizeof(struct volStructDesc)) | |
545 | sectorsize = sizeof(struct volStructDesc); | |
546 | else | |
547 | sectorsize = sb->s_blocksize; | |
548 | ||
549 | sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits); | |
550 | ||
551 | udf_debug("Starting at sector %u (%ld byte sectors)\n", | |
cb00ea35 | 552 | (sector >> sb->s_blocksize_bits), sb->s_blocksize); |
1da177e4 | 553 | /* Process the sequence (if applicable) */ |
cb00ea35 | 554 | for (; !nsr02 && !nsr03; sector += sectorsize) { |
1da177e4 LT |
555 | /* Read a block */ |
556 | bh = udf_tread(sb, sector >> sb->s_blocksize_bits); | |
557 | if (!bh) | |
558 | break; | |
559 | ||
560 | /* Look for ISO descriptors */ | |
561 | vsd = (struct volStructDesc *)(bh->b_data + | |
28de7948 | 562 | (sector & (sb->s_blocksize - 1))); |
1da177e4 | 563 | |
cb00ea35 | 564 | if (vsd->stdIdent[0] == 0) { |
3bf25cb4 | 565 | brelse(bh); |
1da177e4 | 566 | break; |
28de7948 | 567 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN)) { |
1da177e4 | 568 | iso9660 = sector; |
cb00ea35 CG |
569 | switch (vsd->structType) { |
570 | case 0: | |
571 | udf_debug("ISO9660 Boot Record found\n"); | |
572 | break; | |
573 | case 1: | |
574 | udf_debug | |
575 | ("ISO9660 Primary Volume Descriptor found\n"); | |
576 | break; | |
577 | case 2: | |
578 | udf_debug | |
579 | ("ISO9660 Supplementary Volume Descriptor found\n"); | |
580 | break; | |
581 | case 3: | |
582 | udf_debug | |
583 | ("ISO9660 Volume Partition Descriptor found\n"); | |
584 | break; | |
585 | case 255: | |
586 | udf_debug | |
587 | ("ISO9660 Volume Descriptor Set Terminator found\n"); | |
588 | break; | |
589 | default: | |
590 | udf_debug("ISO9660 VRS (%u) found\n", | |
591 | vsd->structType); | |
592 | break; | |
1da177e4 | 593 | } |
28de7948 CG |
594 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN)) { |
595 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01, VSD_STD_ID_LEN)) { | |
3bf25cb4 | 596 | brelse(bh); |
1da177e4 | 597 | break; |
28de7948 | 598 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN)) { |
1da177e4 | 599 | nsr02 = sector; |
28de7948 | 600 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN)) { |
1da177e4 LT |
601 | nsr03 = sector; |
602 | } | |
3bf25cb4 | 603 | brelse(bh); |
1da177e4 LT |
604 | } |
605 | ||
606 | if (nsr03) | |
607 | return nsr03; | |
608 | else if (nsr02) | |
609 | return nsr02; | |
610 | else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768) | |
611 | return -1; | |
612 | else | |
613 | return 0; | |
614 | } | |
615 | ||
616 | /* | |
617 | * udf_find_anchor | |
618 | * | |
619 | * PURPOSE | |
620 | * Find an anchor volume descriptor. | |
621 | * | |
622 | * PRE-CONDITIONS | |
623 | * sb Pointer to _locked_ superblock. | |
624 | * lastblock Last block on media. | |
625 | * | |
626 | * POST-CONDITIONS | |
627 | * <return> 1 if not found, 0 if ok | |
628 | * | |
629 | * HISTORY | |
630 | * July 1, 1997 - Andrew E. Mileski | |
631 | * Written, tested, and released. | |
632 | */ | |
cb00ea35 | 633 | static void udf_find_anchor(struct super_block *sb) |
1da177e4 LT |
634 | { |
635 | int lastblock = UDF_SB_LASTBLOCK(sb); | |
636 | struct buffer_head *bh = NULL; | |
637 | uint16_t ident; | |
638 | uint32_t location; | |
639 | int i; | |
640 | ||
cb00ea35 | 641 | if (lastblock) { |
1da177e4 | 642 | int varlastblock = udf_variable_to_fixed(lastblock); |
28de7948 CG |
643 | int last[] = { lastblock, lastblock - 2, |
644 | lastblock - 150, lastblock - 152, | |
645 | varlastblock, varlastblock - 2, | |
646 | varlastblock - 150, varlastblock - 152 }; | |
1da177e4 LT |
647 | |
648 | lastblock = 0; | |
649 | ||
650 | /* Search for an anchor volume descriptor pointer */ | |
651 | ||
652 | /* according to spec, anchor is in either: | |
653 | * block 256 | |
654 | * lastblock-256 | |
655 | * lastblock | |
656 | * however, if the disc isn't closed, it could be 512 */ | |
657 | ||
e8c96f8c | 658 | for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) { |
cb00ea35 | 659 | if (last[i] < 0 || !(bh = sb_bread(sb, last[i]))) { |
1da177e4 | 660 | ident = location = 0; |
cb00ea35 | 661 | } else { |
28de7948 CG |
662 | ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent); |
663 | location = le32_to_cpu(((tag *)bh->b_data)->tagLocation); | |
3bf25cb4 | 664 | brelse(bh); |
1da177e4 | 665 | } |
e8c96f8c | 666 | |
cb00ea35 CG |
667 | if (ident == TAG_IDENT_AVDP) { |
668 | if (location == last[i] - UDF_SB_SESSION(sb)) { | |
28de7948 CG |
669 | lastblock = UDF_SB_ANCHOR(sb)[0] = last[i] - UDF_SB_SESSION(sb); |
670 | UDF_SB_ANCHOR(sb)[1] = last[i] - 256 - UDF_SB_SESSION(sb); | |
671 | } else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb)) { | |
1da177e4 | 672 | UDF_SET_FLAG(sb, UDF_FLAG_VARCONV); |
28de7948 CG |
673 | lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb); |
674 | UDF_SB_ANCHOR(sb)[1] = lastblock - 256 - UDF_SB_SESSION(sb); | |
675 | } else { | |
676 | udf_debug("Anchor found at block %d, location mismatch %d.\n", | |
677 | last[i], location); | |
678 | } | |
679 | } else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE) { | |
1da177e4 LT |
680 | lastblock = last[i]; |
681 | UDF_SB_ANCHOR(sb)[3] = 512; | |
cb00ea35 | 682 | } else { |
28de7948 | 683 | if (last[i] < 256 || !(bh = sb_bread(sb, last[i] - 256))) { |
1da177e4 | 684 | ident = location = 0; |
cb00ea35 | 685 | } else { |
28de7948 CG |
686 | ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent); |
687 | location = le32_to_cpu(((tag *)bh->b_data)->tagLocation); | |
3bf25cb4 | 688 | brelse(bh); |
1da177e4 | 689 | } |
cb00ea35 | 690 | |
1da177e4 | 691 | if (ident == TAG_IDENT_AVDP && |
28de7948 | 692 | location == last[i] - 256 - UDF_SB_SESSION(sb)) { |
1da177e4 LT |
693 | lastblock = last[i]; |
694 | UDF_SB_ANCHOR(sb)[1] = last[i] - 256; | |
cb00ea35 | 695 | } else { |
28de7948 CG |
696 | if (last[i] < 312 + UDF_SB_SESSION(sb) || |
697 | !(bh = sb_bread(sb, last[i] - 312 - UDF_SB_SESSION(sb)))) { | |
1da177e4 | 698 | ident = location = 0; |
cb00ea35 | 699 | } else { |
28de7948 CG |
700 | ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent); |
701 | location = le32_to_cpu(((tag *)bh->b_data)->tagLocation); | |
3bf25cb4 | 702 | brelse(bh); |
1da177e4 | 703 | } |
cb00ea35 | 704 | |
1da177e4 | 705 | if (ident == TAG_IDENT_AVDP && |
28de7948 CG |
706 | location == udf_variable_to_fixed(last[i]) - 256) { |
707 | UDF_SET_FLAG(sb, UDF_FLAG_VARCONV); | |
708 | lastblock = udf_variable_to_fixed(last[i]); | |
709 | UDF_SB_ANCHOR(sb)[1] = lastblock - 256; | |
1da177e4 LT |
710 | } |
711 | } | |
712 | } | |
713 | } | |
714 | } | |
715 | ||
cb00ea35 | 716 | if (!lastblock) { |
1da177e4 | 717 | /* We havn't found the lastblock. check 312 */ |
cb00ea35 | 718 | if ((bh = sb_bread(sb, 312 + UDF_SB_SESSION(sb)))) { |
28de7948 CG |
719 | ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent); |
720 | location = le32_to_cpu(((tag *)bh->b_data)->tagLocation); | |
3bf25cb4 | 721 | brelse(bh); |
1da177e4 LT |
722 | |
723 | if (ident == TAG_IDENT_AVDP && location == 256) | |
724 | UDF_SET_FLAG(sb, UDF_FLAG_VARCONV); | |
725 | } | |
726 | } | |
727 | ||
e8c96f8c | 728 | for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) { |
cb00ea35 | 729 | if (UDF_SB_ANCHOR(sb)[i]) { |
28de7948 CG |
730 | if (!(bh = udf_read_tagged(sb, UDF_SB_ANCHOR(sb)[i], |
731 | UDF_SB_ANCHOR(sb)[i], &ident))) { | |
1da177e4 | 732 | UDF_SB_ANCHOR(sb)[i] = 0; |
cb00ea35 | 733 | } else { |
3bf25cb4 | 734 | brelse(bh); |
28de7948 CG |
735 | if ((ident != TAG_IDENT_AVDP) && |
736 | (i || (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE))) { | |
1da177e4 LT |
737 | UDF_SB_ANCHOR(sb)[i] = 0; |
738 | } | |
739 | } | |
740 | } | |
741 | } | |
742 | ||
743 | UDF_SB_LASTBLOCK(sb) = lastblock; | |
744 | } | |
745 | ||
28de7948 | 746 | static int udf_find_fileset(struct super_block *sb, kernel_lb_addr *fileset, kernel_lb_addr *root) |
1da177e4 LT |
747 | { |
748 | struct buffer_head *bh = NULL; | |
749 | long lastblock; | |
750 | uint16_t ident; | |
751 | ||
752 | if (fileset->logicalBlockNum != 0xFFFFFFFF || | |
cb00ea35 | 753 | fileset->partitionReferenceNum != 0xFFFF) { |
1da177e4 LT |
754 | bh = udf_read_ptagged(sb, *fileset, 0, &ident); |
755 | ||
28de7948 | 756 | if (!bh) { |
1da177e4 | 757 | return 1; |
28de7948 | 758 | } else if (ident != TAG_IDENT_FSD) { |
3bf25cb4 | 759 | brelse(bh); |
1da177e4 LT |
760 | return 1; |
761 | } | |
cb00ea35 | 762 | |
1da177e4 LT |
763 | } |
764 | ||
28de7948 | 765 | if (!bh) { /* Search backwards through the partitions */ |
1da177e4 LT |
766 | kernel_lb_addr newfileset; |
767 | ||
28de7948 | 768 | /* --> cvg: FIXME - is it reasonable? */ |
1da177e4 | 769 | return 1; |
cb00ea35 CG |
770 | |
771 | for (newfileset.partitionReferenceNum = UDF_SB_NUMPARTS(sb) - 1; | |
772 | (newfileset.partitionReferenceNum != 0xFFFF && | |
773 | fileset->logicalBlockNum == 0xFFFFFFFF && | |
774 | fileset->partitionReferenceNum == 0xFFFF); | |
775 | newfileset.partitionReferenceNum--) { | |
28de7948 | 776 | lastblock = UDF_SB_PARTLEN(sb, newfileset.partitionReferenceNum); |
1da177e4 LT |
777 | newfileset.logicalBlockNum = 0; |
778 | ||
cb00ea35 | 779 | do { |
28de7948 | 780 | bh = udf_read_ptagged(sb, newfileset, 0, &ident); |
cb00ea35 CG |
781 | if (!bh) { |
782 | newfileset.logicalBlockNum++; | |
1da177e4 LT |
783 | continue; |
784 | } | |
785 | ||
cb00ea35 CG |
786 | switch (ident) { |
787 | case TAG_IDENT_SBD: | |
28de7948 CG |
788 | { |
789 | struct spaceBitmapDesc *sp; | |
790 | sp = (struct spaceBitmapDesc *)bh->b_data; | |
791 | newfileset.logicalBlockNum += 1 + | |
792 | ((le32_to_cpu(sp->numOfBytes) + | |
793 | sizeof(struct spaceBitmapDesc) - 1) | |
794 | >> sb->s_blocksize_bits); | |
795 | brelse(bh); | |
796 | break; | |
797 | } | |
cb00ea35 | 798 | case TAG_IDENT_FSD: |
28de7948 CG |
799 | *fileset = newfileset; |
800 | break; | |
cb00ea35 | 801 | default: |
28de7948 CG |
802 | newfileset.logicalBlockNum++; |
803 | brelse(bh); | |
804 | bh = NULL; | |
805 | break; | |
1da177e4 | 806 | } |
28de7948 CG |
807 | } while (newfileset.logicalBlockNum < lastblock && |
808 | fileset->logicalBlockNum == 0xFFFFFFFF && | |
809 | fileset->partitionReferenceNum == 0xFFFF); | |
1da177e4 LT |
810 | } |
811 | } | |
812 | ||
813 | if ((fileset->logicalBlockNum != 0xFFFFFFFF || | |
cb00ea35 | 814 | fileset->partitionReferenceNum != 0xFFFF) && bh) { |
1da177e4 | 815 | udf_debug("Fileset at block=%d, partition=%d\n", |
cb00ea35 CG |
816 | fileset->logicalBlockNum, |
817 | fileset->partitionReferenceNum); | |
1da177e4 LT |
818 | |
819 | UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum; | |
820 | udf_load_fileset(sb, bh, root); | |
3bf25cb4 | 821 | brelse(bh); |
1da177e4 LT |
822 | return 0; |
823 | } | |
824 | return 1; | |
825 | } | |
826 | ||
cb00ea35 | 827 | static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh) |
1da177e4 LT |
828 | { |
829 | struct primaryVolDesc *pvoldesc; | |
830 | time_t recording; | |
831 | long recording_usec; | |
832 | struct ustr instr; | |
833 | struct ustr outstr; | |
834 | ||
835 | pvoldesc = (struct primaryVolDesc *)bh->b_data; | |
836 | ||
cb00ea35 CG |
837 | if (udf_stamp_to_time(&recording, &recording_usec, |
838 | lets_to_cpu(pvoldesc->recordingDateAndTime))) { | |
1da177e4 LT |
839 | kernel_timestamp ts; |
840 | ts = lets_to_cpu(pvoldesc->recordingDateAndTime); | |
28de7948 CG |
841 | udf_debug("recording time %ld/%ld, %04u/%02u/%02u %02u:%02u (%x)\n", |
842 | recording, recording_usec, | |
843 | ts.year, ts.month, ts.day, ts.hour, | |
844 | ts.minute, ts.typeAndTimezone); | |
1da177e4 LT |
845 | UDF_SB_RECORDTIME(sb).tv_sec = recording; |
846 | UDF_SB_RECORDTIME(sb).tv_nsec = recording_usec * 1000; | |
847 | } | |
848 | ||
cb00ea35 CG |
849 | if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) { |
850 | if (udf_CS0toUTF8(&outstr, &instr)) { | |
851 | strncpy(UDF_SB_VOLIDENT(sb), outstr.u_name, | |
1da177e4 LT |
852 | outstr.u_len > 31 ? 31 : outstr.u_len); |
853 | udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb)); | |
854 | } | |
855 | } | |
856 | ||
cb00ea35 | 857 | if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) { |
1da177e4 LT |
858 | if (udf_CS0toUTF8(&outstr, &instr)) |
859 | udf_debug("volSetIdent[] = '%s'\n", outstr.u_name); | |
860 | } | |
861 | } | |
862 | ||
28de7948 CG |
863 | static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh, |
864 | kernel_lb_addr *root) | |
1da177e4 LT |
865 | { |
866 | struct fileSetDesc *fset; | |
867 | ||
868 | fset = (struct fileSetDesc *)bh->b_data; | |
869 | ||
870 | *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation); | |
871 | ||
872 | UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum); | |
873 | ||
cb00ea35 CG |
874 | udf_debug("Rootdir at block=%d, partition=%d\n", |
875 | root->logicalBlockNum, root->partitionReferenceNum); | |
1da177e4 LT |
876 | } |
877 | ||
cb00ea35 | 878 | static void udf_load_partdesc(struct super_block *sb, struct buffer_head *bh) |
1da177e4 LT |
879 | { |
880 | struct partitionDesc *p; | |
881 | int i; | |
882 | ||
883 | p = (struct partitionDesc *)bh->b_data; | |
884 | ||
cb00ea35 CG |
885 | for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) { |
886 | udf_debug("Searching map: (%d == %d)\n", | |
28de7948 CG |
887 | UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber)); |
888 | if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber)) { | |
889 | UDF_SB_PARTLEN(sb,i) = le32_to_cpu(p->partitionLength); /* blocks */ | |
890 | UDF_SB_PARTROOT(sb,i) = le32_to_cpu(p->partitionStartingLocation); | |
891 | if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY) | |
892 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_READ_ONLY; | |
893 | if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE) | |
894 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_WRITE_ONCE; | |
895 | if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE) | |
896 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_REWRITABLE; | |
897 | if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE) | |
898 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_OVERWRITABLE; | |
899 | ||
900 | if (!strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) || | |
901 | !strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03)) { | |
1da177e4 LT |
902 | struct partitionHeaderDesc *phd; |
903 | ||
28de7948 | 904 | phd = (struct partitionHeaderDesc *)(p->partitionContentsUse); |
cb00ea35 | 905 | if (phd->unallocSpaceTable.extLength) { |
28de7948 CG |
906 | kernel_lb_addr loc = { |
907 | .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition), | |
908 | .partitionReferenceNum = i, | |
909 | }; | |
910 | ||
911 | UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table = | |
912 | udf_iget(sb, loc); | |
913 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_TABLE; | |
914 | udf_debug("unallocSpaceTable (part %d) @ %ld\n", | |
915 | i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table->i_ino); | |
1da177e4 | 916 | } |
cb00ea35 | 917 | if (phd->unallocSpaceBitmap.extLength) { |
1da177e4 | 918 | UDF_SB_ALLOC_BITMAP(sb, i, s_uspace); |
28de7948 CG |
919 | if (UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap != NULL) { |
920 | UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extLength = | |
921 | le32_to_cpu(phd->unallocSpaceBitmap.extLength); | |
922 | UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition = | |
923 | le32_to_cpu(phd->unallocSpaceBitmap.extPosition); | |
924 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_BITMAP; | |
925 | udf_debug("unallocSpaceBitmap (part %d) @ %d\n", | |
926 | i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition); | |
1da177e4 LT |
927 | } |
928 | } | |
929 | if (phd->partitionIntegrityTable.extLength) | |
28de7948 | 930 | udf_debug("partitionIntegrityTable (part %d)\n", i); |
cb00ea35 | 931 | if (phd->freedSpaceTable.extLength) { |
28de7948 CG |
932 | kernel_lb_addr loc = { |
933 | .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition), | |
934 | .partitionReferenceNum = i, | |
935 | }; | |
936 | ||
937 | UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table = | |
938 | udf_iget(sb, loc); | |
939 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_TABLE; | |
940 | udf_debug("freedSpaceTable (part %d) @ %ld\n", | |
941 | i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table->i_ino); | |
1da177e4 | 942 | } |
cb00ea35 | 943 | if (phd->freedSpaceBitmap.extLength) { |
1da177e4 | 944 | UDF_SB_ALLOC_BITMAP(sb, i, s_fspace); |
28de7948 CG |
945 | if (UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap != NULL) { |
946 | UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extLength = | |
947 | le32_to_cpu(phd->freedSpaceBitmap.extLength); | |
948 | UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition = | |
949 | le32_to_cpu(phd->freedSpaceBitmap.extPosition); | |
950 | UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_BITMAP; | |
951 | udf_debug("freedSpaceBitmap (part %d) @ %d\n", | |
952 | i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition); | |
1da177e4 LT |
953 | } |
954 | } | |
955 | } | |
956 | break; | |
957 | } | |
958 | } | |
cb00ea35 CG |
959 | if (i == UDF_SB_NUMPARTS(sb)) { |
960 | udf_debug("Partition (%d) not found in partition map\n", | |
961 | le16_to_cpu(p->partitionNumber)); | |
962 | } else { | |
28de7948 CG |
963 | udf_debug("Partition (%d:%d type %x) starts at physical %d, block length %d\n", |
964 | le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb,i), | |
965 | UDF_SB_PARTROOT(sb,i), UDF_SB_PARTLEN(sb,i)); | |
1da177e4 LT |
966 | } |
967 | } | |
968 | ||
28de7948 CG |
969 | static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh, |
970 | kernel_lb_addr *fileset) | |
1da177e4 LT |
971 | { |
972 | struct logicalVolDesc *lvd; | |
973 | int i, j, offset; | |
974 | uint8_t type; | |
975 | ||
976 | lvd = (struct logicalVolDesc *)bh->b_data; | |
977 | ||
978 | UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps)); | |
979 | ||
cb00ea35 | 980 | for (i = 0, offset = 0; |
28de7948 CG |
981 | i < UDF_SB_NUMPARTS(sb) && offset < le32_to_cpu(lvd->mapTableLength); |
982 | i++, offset += ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength) { | |
983 | type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType; | |
cb00ea35 | 984 | if (type == 1) { |
28de7948 CG |
985 | struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]); |
986 | UDF_SB_PARTTYPE(sb,i) = UDF_TYPE1_MAP15; | |
987 | UDF_SB_PARTVSN(sb,i) = le16_to_cpu(gpm1->volSeqNum); | |
988 | UDF_SB_PARTNUM(sb,i) = le16_to_cpu(gpm1->partitionNum); | |
989 | UDF_SB_PARTFUNC(sb,i) = NULL; | |
cb00ea35 | 990 | } else if (type == 2) { |
28de7948 CG |
991 | struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]); |
992 | if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL))) { | |
993 | if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0150) { | |
994 | UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP15; | |
995 | UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt15; | |
996 | } else if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0200) { | |
997 | UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP20; | |
998 | UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt20; | |
1da177e4 | 999 | } |
28de7948 | 1000 | } else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE))) { |
1da177e4 LT |
1001 | uint32_t loc; |
1002 | uint16_t ident; | |
1003 | struct sparingTable *st; | |
28de7948 CG |
1004 | struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]); |
1005 | ||
1006 | UDF_SB_PARTTYPE(sb,i) = UDF_SPARABLE_MAP15; | |
1007 | UDF_SB_TYPESPAR(sb,i).s_packet_len = le16_to_cpu(spm->packetLength); | |
cb00ea35 | 1008 | for (j = 0; j < spm->numSparingTables; j++) { |
28de7948 CG |
1009 | loc = le32_to_cpu(spm->locSparingTable[j]); |
1010 | UDF_SB_TYPESPAR(sb,i).s_spar_map[j] = | |
1011 | udf_read_tagged(sb, loc, loc, &ident); | |
1012 | if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL) { | |
1013 | st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,i).s_spar_map[j]->b_data; | |
1014 | if (ident != 0 || | |
1015 | strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING))) { | |
1016 | brelse(UDF_SB_TYPESPAR(sb,i).s_spar_map[j]); | |
1017 | UDF_SB_TYPESPAR(sb,i).s_spar_map[j] = NULL; | |
1da177e4 LT |
1018 | } |
1019 | } | |
1020 | } | |
28de7948 | 1021 | UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_spar15; |
cb00ea35 | 1022 | } else { |
28de7948 | 1023 | udf_debug("Unknown ident: %s\n", upm2->partIdent.ident); |
1da177e4 LT |
1024 | continue; |
1025 | } | |
28de7948 CG |
1026 | UDF_SB_PARTVSN(sb,i) = le16_to_cpu(upm2->volSeqNum); |
1027 | UDF_SB_PARTNUM(sb,i) = le16_to_cpu(upm2->partitionNum); | |
1da177e4 LT |
1028 | } |
1029 | udf_debug("Partition (%d:%d) type %d on volume %d\n", | |
28de7948 | 1030 | i, UDF_SB_PARTNUM(sb,i), type, UDF_SB_PARTVSN(sb,i)); |
1da177e4 LT |
1031 | } |
1032 | ||
cb00ea35 | 1033 | if (fileset) { |
28de7948 | 1034 | long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]); |
1da177e4 LT |
1035 | |
1036 | *fileset = lelb_to_cpu(la->extLocation); | |
28de7948 CG |
1037 | udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n", |
1038 | fileset->logicalBlockNum, | |
1039 | fileset->partitionReferenceNum); | |
1da177e4 LT |
1040 | } |
1041 | if (lvd->integritySeqExt.extLength) | |
1042 | udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt)); | |
28de7948 | 1043 | |
1da177e4 LT |
1044 | return 0; |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * udf_load_logicalvolint | |
1049 | * | |
1050 | */ | |
cb00ea35 | 1051 | static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc) |
1da177e4 LT |
1052 | { |
1053 | struct buffer_head *bh = NULL; | |
1054 | uint16_t ident; | |
1055 | ||
1056 | while (loc.extLength > 0 && | |
cb00ea35 CG |
1057 | (bh = udf_read_tagged(sb, loc.extLocation, |
1058 | loc.extLocation, &ident)) && | |
1059 | ident == TAG_IDENT_LVID) { | |
1da177e4 | 1060 | UDF_SB_LVIDBH(sb) = bh; |
cb00ea35 | 1061 | |
1da177e4 | 1062 | if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength) |
28de7948 | 1063 | udf_load_logicalvolint(sb, leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt)); |
cb00ea35 | 1064 | |
1da177e4 | 1065 | if (UDF_SB_LVIDBH(sb) != bh) |
3bf25cb4 | 1066 | brelse(bh); |
1da177e4 | 1067 | loc.extLength -= sb->s_blocksize; |
cb00ea35 | 1068 | loc.extLocation++; |
1da177e4 LT |
1069 | } |
1070 | if (UDF_SB_LVIDBH(sb) != bh) | |
3bf25cb4 | 1071 | brelse(bh); |
1da177e4 LT |
1072 | } |
1073 | ||
1074 | /* | |
1075 | * udf_process_sequence | |
1076 | * | |
1077 | * PURPOSE | |
1078 | * Process a main/reserve volume descriptor sequence. | |
1079 | * | |
1080 | * PRE-CONDITIONS | |
1081 | * sb Pointer to _locked_ superblock. | |
1082 | * block First block of first extent of the sequence. | |
1083 | * lastblock Lastblock of first extent of the sequence. | |
1084 | * | |
1085 | * HISTORY | |
1086 | * July 1, 1997 - Andrew E. Mileski | |
1087 | * Written, tested, and released. | |
1088 | */ | |
28de7948 CG |
1089 | static int udf_process_sequence(struct super_block *sb, long block, long lastblock, |
1090 | kernel_lb_addr *fileset) | |
1da177e4 LT |
1091 | { |
1092 | struct buffer_head *bh = NULL; | |
1093 | struct udf_vds_record vds[VDS_POS_LENGTH]; | |
1094 | struct generic_desc *gd; | |
1095 | struct volDescPtr *vdp; | |
cb00ea35 CG |
1096 | int done = 0; |
1097 | int i, j; | |
1da177e4 LT |
1098 | uint32_t vdsn; |
1099 | uint16_t ident; | |
1100 | long next_s = 0, next_e = 0; | |
1101 | ||
1102 | memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH); | |
1103 | ||
1104 | /* Read the main descriptor sequence */ | |
cb00ea35 | 1105 | for (; (!done && block <= lastblock); block++) { |
1da177e4 LT |
1106 | |
1107 | bh = udf_read_tagged(sb, block, block, &ident); | |
cb00ea35 | 1108 | if (!bh) |
1da177e4 LT |
1109 | break; |
1110 | ||
1111 | /* Process each descriptor (ISO 13346 3/8.3-8.4) */ | |
1112 | gd = (struct generic_desc *)bh->b_data; | |
1113 | vdsn = le32_to_cpu(gd->volDescSeqNum); | |
cb00ea35 | 1114 | switch (ident) { |
28de7948 | 1115 | case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */ |
cb00ea35 | 1116 | if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) { |
28de7948 | 1117 | vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn; |
cb00ea35 CG |
1118 | vds[VDS_POS_PRIMARY_VOL_DESC].block = block; |
1119 | } | |
1120 | break; | |
28de7948 | 1121 | case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */ |
cb00ea35 CG |
1122 | if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) { |
1123 | vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn; | |
1124 | vds[VDS_POS_VOL_DESC_PTR].block = block; | |
1125 | ||
1126 | vdp = (struct volDescPtr *)bh->b_data; | |
28de7948 CG |
1127 | next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation); |
1128 | next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength); | |
cb00ea35 CG |
1129 | next_e = next_e >> sb->s_blocksize_bits; |
1130 | next_e += next_s; | |
1131 | } | |
1132 | break; | |
28de7948 | 1133 | case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */ |
cb00ea35 | 1134 | if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) { |
28de7948 | 1135 | vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn; |
cb00ea35 CG |
1136 | vds[VDS_POS_IMP_USE_VOL_DESC].block = block; |
1137 | } | |
1138 | break; | |
28de7948 | 1139 | case TAG_IDENT_PD: /* ISO 13346 3/10.5 */ |
cb00ea35 CG |
1140 | if (!vds[VDS_POS_PARTITION_DESC].block) |
1141 | vds[VDS_POS_PARTITION_DESC].block = block; | |
1142 | break; | |
28de7948 | 1143 | case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */ |
cb00ea35 | 1144 | if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) { |
28de7948 | 1145 | vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn; |
cb00ea35 CG |
1146 | vds[VDS_POS_LOGICAL_VOL_DESC].block = block; |
1147 | } | |
1148 | break; | |
28de7948 CG |
1149 | case TAG_IDENT_USD: /* ISO 13346 3/10.8 */ |
1150 | if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) { | |
1151 | vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn; | |
cb00ea35 CG |
1152 | vds[VDS_POS_UNALLOC_SPACE_DESC].block = block; |
1153 | } | |
1154 | break; | |
28de7948 | 1155 | case TAG_IDENT_TD: /* ISO 13346 3/10.9 */ |
cb00ea35 CG |
1156 | vds[VDS_POS_TERMINATING_DESC].block = block; |
1157 | if (next_e) { | |
1158 | block = next_s; | |
1159 | lastblock = next_e; | |
1160 | next_s = next_e = 0; | |
28de7948 | 1161 | } else { |
cb00ea35 | 1162 | done = 1; |
28de7948 | 1163 | } |
cb00ea35 | 1164 | break; |
1da177e4 | 1165 | } |
3bf25cb4 | 1166 | brelse(bh); |
1da177e4 | 1167 | } |
cb00ea35 CG |
1168 | for (i = 0; i < VDS_POS_LENGTH; i++) { |
1169 | if (vds[i].block) { | |
28de7948 | 1170 | bh = udf_read_tagged(sb, vds[i].block, vds[i].block, &ident); |
1da177e4 | 1171 | |
28de7948 | 1172 | if (i == VDS_POS_PRIMARY_VOL_DESC) { |
1da177e4 | 1173 | udf_load_pvoldesc(sb, bh); |
28de7948 | 1174 | } else if (i == VDS_POS_LOGICAL_VOL_DESC) { |
1da177e4 | 1175 | udf_load_logicalvol(sb, bh, fileset); |
28de7948 | 1176 | } else if (i == VDS_POS_PARTITION_DESC) { |
1da177e4 LT |
1177 | struct buffer_head *bh2 = NULL; |
1178 | udf_load_partdesc(sb, bh); | |
28de7948 | 1179 | for (j = vds[i].block + 1; j < vds[VDS_POS_TERMINATING_DESC].block; j++) { |
1da177e4 LT |
1180 | bh2 = udf_read_tagged(sb, j, j, &ident); |
1181 | gd = (struct generic_desc *)bh2->b_data; | |
1182 | if (ident == TAG_IDENT_PD) | |
1183 | udf_load_partdesc(sb, bh2); | |
3bf25cb4 | 1184 | brelse(bh2); |
1da177e4 LT |
1185 | } |
1186 | } | |
3bf25cb4 | 1187 | brelse(bh); |
1da177e4 LT |
1188 | } |
1189 | } | |
1190 | ||
1191 | return 0; | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | * udf_check_valid() | |
1196 | */ | |
cb00ea35 | 1197 | static int udf_check_valid(struct super_block *sb, int novrs, int silent) |
1da177e4 LT |
1198 | { |
1199 | long block; | |
1200 | ||
cb00ea35 | 1201 | if (novrs) { |
1da177e4 LT |
1202 | udf_debug("Validity check skipped because of novrs option\n"); |
1203 | return 0; | |
1204 | } | |
1205 | /* Check that it is NSR02 compliant */ | |
1206 | /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */ | |
cb00ea35 | 1207 | else if ((block = udf_vrs(sb, silent)) == -1) { |
28de7948 CG |
1208 | udf_debug("Failed to read byte 32768. Assuming open disc. " |
1209 | "Skipping validity check\n"); | |
1da177e4 LT |
1210 | if (!UDF_SB_LASTBLOCK(sb)) |
1211 | UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb); | |
1212 | return 0; | |
28de7948 | 1213 | } else { |
1da177e4 | 1214 | return !block; |
28de7948 | 1215 | } |
1da177e4 LT |
1216 | } |
1217 | ||
28de7948 | 1218 | static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset) |
1da177e4 LT |
1219 | { |
1220 | struct anchorVolDescPtr *anchor; | |
1221 | uint16_t ident; | |
1222 | struct buffer_head *bh; | |
1223 | long main_s, main_e, reserve_s, reserve_e; | |
1224 | int i, j; | |
1225 | ||
1226 | if (!sb) | |
1227 | return 1; | |
1228 | ||
e8c96f8c | 1229 | for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) { |
28de7948 CG |
1230 | if (UDF_SB_ANCHOR(sb)[i] && |
1231 | (bh = udf_read_tagged(sb, UDF_SB_ANCHOR(sb)[i], | |
1232 | UDF_SB_ANCHOR(sb)[i], &ident))) { | |
1da177e4 LT |
1233 | anchor = (struct anchorVolDescPtr *)bh->b_data; |
1234 | ||
1235 | /* Locate the main sequence */ | |
28de7948 CG |
1236 | main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation); |
1237 | main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength ); | |
1da177e4 LT |
1238 | main_e = main_e >> sb->s_blocksize_bits; |
1239 | main_e += main_s; | |
e8c96f8c | 1240 | |
1da177e4 | 1241 | /* Locate the reserve sequence */ |
28de7948 CG |
1242 | reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation); |
1243 | reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength); | |
1da177e4 LT |
1244 | reserve_e = reserve_e >> sb->s_blocksize_bits; |
1245 | reserve_e += reserve_s; | |
1246 | ||
3bf25cb4 | 1247 | brelse(bh); |
1da177e4 LT |
1248 | |
1249 | /* Process the main & reserve sequences */ | |
1250 | /* responsible for finding the PartitionDesc(s) */ | |
28de7948 CG |
1251 | if (!(udf_process_sequence(sb, main_s, main_e, fileset) && |
1252 | udf_process_sequence(sb, reserve_s, reserve_e, fileset))) { | |
1da177e4 LT |
1253 | break; |
1254 | } | |
1255 | } | |
1256 | } | |
1257 | ||
e8c96f8c | 1258 | if (i == ARRAY_SIZE(UDF_SB_ANCHOR(sb))) { |
1da177e4 LT |
1259 | udf_debug("No Anchor block found\n"); |
1260 | return 1; | |
e8c96f8c | 1261 | } else |
1da177e4 LT |
1262 | udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]); |
1263 | ||
cb00ea35 | 1264 | for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) { |
28de7948 | 1265 | kernel_lb_addr uninitialized_var(ino); |
cb00ea35 CG |
1266 | switch (UDF_SB_PARTTYPE(sb, i)) { |
1267 | case UDF_VIRTUAL_MAP15: | |
1268 | case UDF_VIRTUAL_MAP20: | |
28de7948 CG |
1269 | if (!UDF_SB_LASTBLOCK(sb)) { |
1270 | UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb); | |
1271 | udf_find_anchor(sb); | |
1272 | } | |
1da177e4 | 1273 | |
28de7948 CG |
1274 | if (!UDF_SB_LASTBLOCK(sb)) { |
1275 | udf_debug("Unable to determine Lastblock (For " | |
1276 | "Virtual Partition)\n"); | |
1277 | return 1; | |
1278 | } | |
1da177e4 | 1279 | |
28de7948 CG |
1280 | for (j = 0; j < UDF_SB_NUMPARTS(sb); j++) { |
1281 | if (j != i && UDF_SB_PARTVSN(sb, i) == | |
1282 | UDF_SB_PARTVSN(sb, j) && | |
1283 | UDF_SB_PARTNUM(sb, i) == | |
1284 | UDF_SB_PARTNUM(sb, j)) { | |
1285 | ino.partitionReferenceNum = j; | |
1286 | ino.logicalBlockNum = | |
1287 | UDF_SB_LASTBLOCK(sb) - | |
1288 | UDF_SB_PARTROOT(sb, j); | |
1289 | break; | |
1da177e4 | 1290 | } |
28de7948 | 1291 | } |
1da177e4 | 1292 | |
28de7948 CG |
1293 | if (j == UDF_SB_NUMPARTS(sb)) |
1294 | return 1; | |
1da177e4 | 1295 | |
28de7948 CG |
1296 | if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino))) |
1297 | return 1; | |
1da177e4 | 1298 | |
28de7948 CG |
1299 | if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP15) { |
1300 | UDF_SB_TYPEVIRT(sb, i).s_start_offset = | |
1301 | udf_ext0_offset(UDF_SB_VAT(sb)); | |
1302 | UDF_SB_TYPEVIRT(sb, i).s_num_entries = | |
1303 | (UDF_SB_VAT(sb)->i_size - 36) >> 2; | |
1304 | } else if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP20) { | |
1305 | struct buffer_head *bh = NULL; | |
1306 | uint32_t pos; | |
1da177e4 | 1307 | |
28de7948 CG |
1308 | pos = udf_block_map(UDF_SB_VAT(sb), 0); |
1309 | bh = sb_bread(sb, pos); | |
1310 | if (!bh) | |
1311 | return 1; | |
1312 | UDF_SB_TYPEVIRT(sb, i).s_start_offset = | |
1313 | le16_to_cpu(((struct | |
1314 | virtualAllocationTable20 *)bh->b_data + | |
1315 | udf_ext0_offset(UDF_SB_VAT(sb)))-> | |
1316 | lengthHeader) + | |
1317 | udf_ext0_offset(UDF_SB_VAT(sb)); | |
1318 | UDF_SB_TYPEVIRT(sb, i).s_num_entries = | |
1319 | (UDF_SB_VAT(sb)->i_size - | |
1320 | UDF_SB_TYPEVIRT(sb, i).s_start_offset) >> 2; | |
1321 | brelse(bh); | |
1da177e4 | 1322 | } |
28de7948 CG |
1323 | UDF_SB_PARTROOT(sb, i) = udf_get_pblock(sb, 0, i, 0); |
1324 | UDF_SB_PARTLEN(sb, i) = UDF_SB_PARTLEN(sb, | |
1325 | ino.partitionReferenceNum); | |
1da177e4 LT |
1326 | } |
1327 | } | |
1328 | return 0; | |
1329 | } | |
1330 | ||
1331 | static void udf_open_lvid(struct super_block *sb) | |
1332 | { | |
cb00ea35 | 1333 | if (UDF_SB_LVIDBH(sb)) { |
1da177e4 LT |
1334 | int i; |
1335 | kernel_timestamp cpu_time; | |
1336 | ||
1337 | UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; | |
1338 | UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; | |
1339 | if (udf_time_to_stamp(&cpu_time, CURRENT_TIME)) | |
cb00ea35 CG |
1340 | UDF_SB_LVID(sb)->recordingDateAndTime = |
1341 | cpu_to_lets(cpu_time); | |
1da177e4 LT |
1342 | UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_OPEN; |
1343 | ||
1344 | UDF_SB_LVID(sb)->descTag.descCRC = | |
cb00ea35 CG |
1345 | cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag), |
1346 | le16_to_cpu(UDF_SB_LVID(sb)->descTag. | |
1347 | descCRCLength), 0)); | |
1da177e4 LT |
1348 | |
1349 | UDF_SB_LVID(sb)->descTag.tagChecksum = 0; | |
cb00ea35 | 1350 | for (i = 0; i < 16; i++) |
1da177e4 LT |
1351 | if (i != 4) |
1352 | UDF_SB_LVID(sb)->descTag.tagChecksum += | |
cb00ea35 CG |
1353 | ((uint8_t *) & |
1354 | (UDF_SB_LVID(sb)->descTag))[i]; | |
1da177e4 LT |
1355 | |
1356 | mark_buffer_dirty(UDF_SB_LVIDBH(sb)); | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | static void udf_close_lvid(struct super_block *sb) | |
1361 | { | |
28de7948 CG |
1362 | kernel_timestamp cpu_time; |
1363 | int i; | |
1364 | ||
1da177e4 | 1365 | if (UDF_SB_LVIDBH(sb) && |
cb00ea35 | 1366 | UDF_SB_LVID(sb)->integrityType == LVID_INTEGRITY_TYPE_OPEN) { |
1da177e4 LT |
1367 | UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; |
1368 | UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; | |
1369 | if (udf_time_to_stamp(&cpu_time, CURRENT_TIME)) | |
28de7948 CG |
1370 | UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time); |
1371 | if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev)) | |
1372 | UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION); | |
1373 | if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev)) | |
1374 | UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb)); | |
1375 | if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev)) | |
1376 | UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb)); | |
1377 | UDF_SB_LVID(sb)->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE); | |
1da177e4 LT |
1378 | |
1379 | UDF_SB_LVID(sb)->descTag.descCRC = | |
28de7948 CG |
1380 | cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag), |
1381 | le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0)); | |
1da177e4 LT |
1382 | |
1383 | UDF_SB_LVID(sb)->descTag.tagChecksum = 0; | |
cb00ea35 | 1384 | for (i = 0; i < 16; i++) |
1da177e4 LT |
1385 | if (i != 4) |
1386 | UDF_SB_LVID(sb)->descTag.tagChecksum += | |
28de7948 | 1387 | ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i]; |
1da177e4 LT |
1388 | |
1389 | mark_buffer_dirty(UDF_SB_LVIDBH(sb)); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | /* | |
1394 | * udf_read_super | |
1395 | * | |
1396 | * PURPOSE | |
1397 | * Complete the specified super block. | |
1398 | * | |
1399 | * PRE-CONDITIONS | |
1400 | * sb Pointer to superblock to complete - never NULL. | |
1401 | * sb->s_dev Device to read suberblock from. | |
1402 | * options Pointer to mount options. | |
1403 | * silent Silent flag. | |
1404 | * | |
1405 | * HISTORY | |
1406 | * July 1, 1997 - Andrew E. Mileski | |
1407 | * Written, tested, and released. | |
1408 | */ | |
1409 | static int udf_fill_super(struct super_block *sb, void *options, int silent) | |
1410 | { | |
1411 | int i; | |
cb00ea35 | 1412 | struct inode *inode = NULL; |
1da177e4 LT |
1413 | struct udf_options uopt; |
1414 | kernel_lb_addr rootdir, fileset; | |
1415 | struct udf_sb_info *sbi; | |
1416 | ||
1417 | uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT); | |
1418 | uopt.uid = -1; | |
1419 | uopt.gid = -1; | |
1420 | uopt.umask = 0; | |
1421 | ||
1422 | sbi = kmalloc(sizeof(struct udf_sb_info), GFP_KERNEL); | |
1423 | if (!sbi) | |
1424 | return -ENOMEM; | |
28de7948 | 1425 | |
1da177e4 LT |
1426 | sb->s_fs_info = sbi; |
1427 | memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info)); | |
1428 | ||
1e7933de | 1429 | mutex_init(&sbi->s_alloc_mutex); |
1da177e4 LT |
1430 | |
1431 | if (!udf_parse_options((char *)options, &uopt)) | |
1432 | goto error_out; | |
1433 | ||
1434 | if (uopt.flags & (1 << UDF_FLAG_UTF8) && | |
cb00ea35 | 1435 | uopt.flags & (1 << UDF_FLAG_NLS_MAP)) { |
1da177e4 | 1436 | udf_error(sb, "udf_read_super", |
cb00ea35 | 1437 | "utf8 cannot be combined with iocharset\n"); |
1da177e4 LT |
1438 | goto error_out; |
1439 | } | |
1440 | #ifdef CONFIG_UDF_NLS | |
cb00ea35 | 1441 | if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) { |
1da177e4 LT |
1442 | uopt.nls_map = load_nls_default(); |
1443 | if (!uopt.nls_map) | |
1444 | uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP); | |
1445 | else | |
1446 | udf_debug("Using default NLS map\n"); | |
1447 | } | |
1448 | #endif | |
1449 | if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP))) | |
1450 | uopt.flags |= (1 << UDF_FLAG_UTF8); | |
1451 | ||
1452 | fileset.logicalBlockNum = 0xFFFFFFFF; | |
1453 | fileset.partitionReferenceNum = 0xFFFF; | |
1454 | ||
1455 | UDF_SB(sb)->s_flags = uopt.flags; | |
1456 | UDF_SB(sb)->s_uid = uopt.uid; | |
1457 | UDF_SB(sb)->s_gid = uopt.gid; | |
1458 | UDF_SB(sb)->s_umask = uopt.umask; | |
1459 | UDF_SB(sb)->s_nls_map = uopt.nls_map; | |
1460 | ||
1461 | /* Set the block size for all transfers */ | |
1462 | if (!udf_set_blocksize(sb, uopt.blocksize)) | |
1463 | goto error_out; | |
1464 | ||
cb00ea35 | 1465 | if (uopt.session == 0xFFFFFFFF) |
1da177e4 LT |
1466 | UDF_SB_SESSION(sb) = udf_get_last_session(sb); |
1467 | else | |
1468 | UDF_SB_SESSION(sb) = uopt.session; | |
1469 | ||
1470 | udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb)); | |
1471 | ||
1472 | UDF_SB_LASTBLOCK(sb) = uopt.lastblock; | |
1473 | UDF_SB_ANCHOR(sb)[0] = UDF_SB_ANCHOR(sb)[1] = 0; | |
1474 | UDF_SB_ANCHOR(sb)[2] = uopt.anchor; | |
1475 | UDF_SB_ANCHOR(sb)[3] = 256; | |
1476 | ||
28de7948 | 1477 | if (udf_check_valid(sb, uopt.novrs, silent)) { /* read volume recognition sequences */ |
1da177e4 | 1478 | printk("UDF-fs: No VRS found\n"); |
cb00ea35 | 1479 | goto error_out; |
1da177e4 LT |
1480 | } |
1481 | ||
1482 | udf_find_anchor(sb); | |
1483 | ||
1484 | /* Fill in the rest of the superblock */ | |
1485 | sb->s_op = &udf_sb_ops; | |
1486 | sb->dq_op = NULL; | |
1487 | sb->s_dirt = 0; | |
1488 | sb->s_magic = UDF_SUPER_MAGIC; | |
1489 | sb->s_time_gran = 1000; | |
1490 | ||
cb00ea35 | 1491 | if (udf_load_partition(sb, &fileset)) { |
1da177e4 LT |
1492 | printk("UDF-fs: No partition found (1)\n"); |
1493 | goto error_out; | |
1494 | } | |
1495 | ||
1496 | udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb)); | |
1497 | ||
cb00ea35 | 1498 | if (UDF_SB_LVIDBH(sb)) { |
28de7948 CG |
1499 | uint16_t minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev); |
1500 | uint16_t minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev); | |
1da177e4 LT |
1501 | /* uint16_t maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */ |
1502 | ||
cb00ea35 | 1503 | if (minUDFReadRev > UDF_MAX_READ_VERSION) { |
1da177e4 | 1504 | printk("UDF-fs: minUDFReadRev=%x (max is %x)\n", |
cb00ea35 CG |
1505 | le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev), |
1506 | UDF_MAX_READ_VERSION); | |
1da177e4 | 1507 | goto error_out; |
cb00ea35 | 1508 | } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) { |
1da177e4 LT |
1509 | sb->s_flags |= MS_RDONLY; |
1510 | } | |
1511 | ||
1512 | UDF_SB_UDFREV(sb) = minUDFWriteRev; | |
1513 | ||
1514 | if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE) | |
1515 | UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE); | |
1516 | if (minUDFReadRev >= UDF_VERS_USE_STREAMS) | |
1517 | UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS); | |
1518 | } | |
1519 | ||
cb00ea35 | 1520 | if (!UDF_SB_NUMPARTS(sb)) { |
1da177e4 LT |
1521 | printk("UDF-fs: No partition found (2)\n"); |
1522 | goto error_out; | |
1523 | } | |
1524 | ||
28de7948 CG |
1525 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_READ_ONLY) { |
1526 | printk("UDF-fs: Partition marked readonly; forcing readonly mount\n"); | |
39b3f6d6 | 1527 | sb->s_flags |= MS_RDONLY; |
c1a26e7d | 1528 | } |
39b3f6d6 | 1529 | |
cb00ea35 | 1530 | if (udf_find_fileset(sb, &fileset, &rootdir)) { |
1da177e4 LT |
1531 | printk("UDF-fs: No fileset found\n"); |
1532 | goto error_out; | |
1533 | } | |
1534 | ||
cb00ea35 | 1535 | if (!silent) { |
1da177e4 LT |
1536 | kernel_timestamp ts; |
1537 | udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb)); | |
28de7948 CG |
1538 | udf_info("UDF %s (%s) Mounting volume '%s', " |
1539 | "timestamp %04u/%02u/%02u %02u:%02u (%x)\n", | |
1540 | UDFFS_VERSION, UDFFS_DATE, | |
1541 | UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute, | |
1542 | ts.typeAndTimezone); | |
1da177e4 LT |
1543 | } |
1544 | if (!(sb->s_flags & MS_RDONLY)) | |
1545 | udf_open_lvid(sb); | |
1546 | ||
1547 | /* Assign the root inode */ | |
1548 | /* assign inodes by physical block number */ | |
1549 | /* perhaps it's not extensible enough, but for now ... */ | |
cb00ea35 CG |
1550 | inode = udf_iget(sb, rootdir); |
1551 | if (!inode) { | |
1da177e4 | 1552 | printk("UDF-fs: Error in udf_iget, block=%d, partition=%d\n", |
cb00ea35 | 1553 | rootdir.logicalBlockNum, rootdir.partitionReferenceNum); |
1da177e4 LT |
1554 | goto error_out; |
1555 | } | |
1556 | ||
1557 | /* Allocate a dentry for the root inode */ | |
1558 | sb->s_root = d_alloc_root(inode); | |
cb00ea35 | 1559 | if (!sb->s_root) { |
1da177e4 LT |
1560 | printk("UDF-fs: Couldn't allocate root dentry\n"); |
1561 | iput(inode); | |
1562 | goto error_out; | |
1563 | } | |
31170b6a | 1564 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
1da177e4 LT |
1565 | return 0; |
1566 | ||
28de7948 | 1567 | error_out: |
1da177e4 LT |
1568 | if (UDF_SB_VAT(sb)) |
1569 | iput(UDF_SB_VAT(sb)); | |
cb00ea35 | 1570 | if (UDF_SB_NUMPARTS(sb)) { |
28de7948 CG |
1571 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE) |
1572 | iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table); | |
1573 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE) | |
1574 | iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table); | |
1575 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP) | |
1576 | UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_uspace); | |
1577 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP) | |
1578 | UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_fspace); | |
1579 | if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15) { | |
cb00ea35 | 1580 | for (i = 0; i < 4; i++) |
28de7948 | 1581 | brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]); |
1da177e4 LT |
1582 | } |
1583 | } | |
1584 | #ifdef CONFIG_UDF_NLS | |
1585 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) | |
1586 | unload_nls(UDF_SB(sb)->s_nls_map); | |
1587 | #endif | |
1588 | if (!(sb->s_flags & MS_RDONLY)) | |
1589 | udf_close_lvid(sb); | |
3bf25cb4 | 1590 | brelse(UDF_SB_LVIDBH(sb)); |
1da177e4 LT |
1591 | UDF_SB_FREE(sb); |
1592 | kfree(sbi); | |
1593 | sb->s_fs_info = NULL; | |
28de7948 | 1594 | |
1da177e4 LT |
1595 | return -EINVAL; |
1596 | } | |
1597 | ||
1598 | void udf_error(struct super_block *sb, const char *function, | |
cb00ea35 | 1599 | const char *fmt, ...) |
1da177e4 LT |
1600 | { |
1601 | va_list args; | |
1602 | ||
cb00ea35 | 1603 | if (!(sb->s_flags & MS_RDONLY)) { |
1da177e4 LT |
1604 | /* mark sb error */ |
1605 | sb->s_dirt = 1; | |
1606 | } | |
1607 | va_start(args, fmt); | |
4a6e617a | 1608 | vsnprintf(error_buf, sizeof(error_buf), fmt, args); |
1da177e4 | 1609 | va_end(args); |
28de7948 CG |
1610 | printk (KERN_CRIT "UDF-fs error (device %s): %s: %s\n", |
1611 | sb->s_id, function, error_buf); | |
1da177e4 LT |
1612 | } |
1613 | ||
1614 | void udf_warning(struct super_block *sb, const char *function, | |
cb00ea35 | 1615 | const char *fmt, ...) |
1da177e4 LT |
1616 | { |
1617 | va_list args; | |
1618 | ||
cb00ea35 | 1619 | va_start(args, fmt); |
4a6e617a | 1620 | vsnprintf(error_buf, sizeof(error_buf), fmt, args); |
1da177e4 LT |
1621 | va_end(args); |
1622 | printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n", | |
cb00ea35 | 1623 | sb->s_id, function, error_buf); |
1da177e4 LT |
1624 | } |
1625 | ||
1626 | /* | |
1627 | * udf_put_super | |
1628 | * | |
1629 | * PURPOSE | |
1630 | * Prepare for destruction of the superblock. | |
1631 | * | |
1632 | * DESCRIPTION | |
1633 | * Called before the filesystem is unmounted. | |
1634 | * | |
1635 | * HISTORY | |
1636 | * July 1, 1997 - Andrew E. Mileski | |
1637 | * Written, tested, and released. | |
1638 | */ | |
cb00ea35 | 1639 | static void udf_put_super(struct super_block *sb) |
1da177e4 LT |
1640 | { |
1641 | int i; | |
1642 | ||
1643 | if (UDF_SB_VAT(sb)) | |
1644 | iput(UDF_SB_VAT(sb)); | |
cb00ea35 | 1645 | if (UDF_SB_NUMPARTS(sb)) { |
28de7948 CG |
1646 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE) |
1647 | iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table); | |
1648 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE) | |
1649 | iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table); | |
1650 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP) | |
1651 | UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_uspace); | |
1652 | if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP) | |
1653 | UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_fspace); | |
1654 | if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15) { | |
cb00ea35 | 1655 | for (i = 0; i < 4; i++) |
28de7948 | 1656 | brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]); |
1da177e4 LT |
1657 | } |
1658 | } | |
1659 | #ifdef CONFIG_UDF_NLS | |
1660 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) | |
1661 | unload_nls(UDF_SB(sb)->s_nls_map); | |
1662 | #endif | |
1663 | if (!(sb->s_flags & MS_RDONLY)) | |
1664 | udf_close_lvid(sb); | |
3bf25cb4 | 1665 | brelse(UDF_SB_LVIDBH(sb)); |
1da177e4 LT |
1666 | UDF_SB_FREE(sb); |
1667 | kfree(sb->s_fs_info); | |
1668 | sb->s_fs_info = NULL; | |
1669 | } | |
1670 | ||
1671 | /* | |
1672 | * udf_stat_fs | |
1673 | * | |
1674 | * PURPOSE | |
1675 | * Return info about the filesystem. | |
1676 | * | |
1677 | * DESCRIPTION | |
1678 | * Called by sys_statfs() | |
1679 | * | |
1680 | * HISTORY | |
1681 | * July 1, 1997 - Andrew E. Mileski | |
1682 | * Written, tested, and released. | |
1683 | */ | |
cb00ea35 | 1684 | static int udf_statfs(struct dentry *dentry, struct kstatfs *buf) |
1da177e4 | 1685 | { |
726c3342 DH |
1686 | struct super_block *sb = dentry->d_sb; |
1687 | ||
1da177e4 LT |
1688 | buf->f_type = UDF_SUPER_MAGIC; |
1689 | buf->f_bsize = sb->s_blocksize; | |
1690 | buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb)); | |
1691 | buf->f_bfree = udf_count_free(sb); | |
1692 | buf->f_bavail = buf->f_bfree; | |
1693 | buf->f_files = (UDF_SB_LVIDBH(sb) ? | |
cb00ea35 | 1694 | (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + |
28de7948 | 1695 | le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + buf->f_bfree; |
1da177e4 LT |
1696 | buf->f_ffree = buf->f_bfree; |
1697 | /* __kernel_fsid_t f_fsid */ | |
cb00ea35 | 1698 | buf->f_namelen = UDF_NAME_LEN - 2; |
1da177e4 LT |
1699 | |
1700 | return 0; | |
1701 | } | |
1702 | ||
1703 | static unsigned char udf_bitmap_lookup[16] = { | |
1704 | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 | |
1705 | }; | |
1706 | ||
28de7948 | 1707 | static unsigned int udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap) |
1da177e4 LT |
1708 | { |
1709 | struct buffer_head *bh = NULL; | |
1710 | unsigned int accum = 0; | |
1711 | int index; | |
1712 | int block = 0, newblock; | |
1713 | kernel_lb_addr loc; | |
1714 | uint32_t bytes; | |
1715 | uint8_t value; | |
1716 | uint8_t *ptr; | |
1717 | uint16_t ident; | |
1718 | struct spaceBitmapDesc *bm; | |
1719 | ||
1720 | lock_kernel(); | |
1721 | ||
1722 | loc.logicalBlockNum = bitmap->s_extPosition; | |
1723 | loc.partitionReferenceNum = UDF_SB_PARTITION(sb); | |
1724 | bh = udf_read_ptagged(sb, loc, 0, &ident); | |
1725 | ||
cb00ea35 | 1726 | if (!bh) { |
1da177e4 LT |
1727 | printk(KERN_ERR "udf: udf_count_free failed\n"); |
1728 | goto out; | |
cb00ea35 | 1729 | } else if (ident != TAG_IDENT_SBD) { |
3bf25cb4 | 1730 | brelse(bh); |
1da177e4 LT |
1731 | printk(KERN_ERR "udf: udf_count_free failed\n"); |
1732 | goto out; | |
1733 | } | |
1734 | ||
1735 | bm = (struct spaceBitmapDesc *)bh->b_data; | |
1736 | bytes = le32_to_cpu(bm->numOfBytes); | |
28de7948 CG |
1737 | index = sizeof(struct spaceBitmapDesc); /* offset in first block only */ |
1738 | ptr = (uint8_t *)bh->b_data; | |
1da177e4 | 1739 | |
cb00ea35 CG |
1740 | while (bytes > 0) { |
1741 | while ((bytes > 0) && (index < sb->s_blocksize)) { | |
1da177e4 | 1742 | value = ptr[index]; |
cb00ea35 CG |
1743 | accum += udf_bitmap_lookup[value & 0x0f]; |
1744 | accum += udf_bitmap_lookup[value >> 4]; | |
1da177e4 LT |
1745 | index++; |
1746 | bytes--; | |
1747 | } | |
cb00ea35 | 1748 | if (bytes) { |
3bf25cb4 | 1749 | brelse(bh); |
1da177e4 LT |
1750 | newblock = udf_get_lb_pblock(sb, loc, ++block); |
1751 | bh = udf_tread(sb, newblock); | |
cb00ea35 | 1752 | if (!bh) { |
1da177e4 LT |
1753 | udf_debug("read failed\n"); |
1754 | goto out; | |
1755 | } | |
1756 | index = 0; | |
28de7948 | 1757 | ptr = (uint8_t *)bh->b_data; |
1da177e4 LT |
1758 | } |
1759 | } | |
3bf25cb4 | 1760 | brelse(bh); |
1da177e4 | 1761 | |
28de7948 | 1762 | out: |
1da177e4 LT |
1763 | unlock_kernel(); |
1764 | ||
1765 | return accum; | |
1766 | } | |
1767 | ||
28de7948 | 1768 | static unsigned int udf_count_free_table(struct super_block *sb, struct inode *table) |
1da177e4 LT |
1769 | { |
1770 | unsigned int accum = 0; | |
ff116fc8 JK |
1771 | uint32_t elen; |
1772 | kernel_lb_addr eloc; | |
1da177e4 | 1773 | int8_t etype; |
ff116fc8 | 1774 | struct extent_position epos; |
1da177e4 LT |
1775 | |
1776 | lock_kernel(); | |
1777 | ||
ff116fc8 JK |
1778 | epos.block = UDF_I_LOCATION(table); |
1779 | epos.offset = sizeof(struct unallocSpaceEntry); | |
1780 | epos.bh = NULL; | |
1da177e4 | 1781 | |
28de7948 | 1782 | while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
1da177e4 | 1783 | accum += (elen >> table->i_sb->s_blocksize_bits); |
28de7948 | 1784 | } |
3bf25cb4 | 1785 | brelse(epos.bh); |
1da177e4 LT |
1786 | |
1787 | unlock_kernel(); | |
1788 | ||
1789 | return accum; | |
1790 | } | |
cb00ea35 CG |
1791 | |
1792 | static unsigned int udf_count_free(struct super_block *sb) | |
1da177e4 LT |
1793 | { |
1794 | unsigned int accum = 0; | |
1795 | ||
cb00ea35 | 1796 | if (UDF_SB_LVIDBH(sb)) { |
28de7948 CG |
1797 | if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb)) { |
1798 | accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]); | |
1da177e4 LT |
1799 | if (accum == 0xFFFFFFFF) |
1800 | accum = 0; | |
1801 | } | |
1802 | } | |
1803 | ||
1804 | if (accum) | |
1805 | return accum; | |
1806 | ||
28de7948 CG |
1807 | if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP) { |
1808 | accum += udf_count_free_bitmap(sb, | |
1809 | UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap); | |
1da177e4 | 1810 | } |
28de7948 CG |
1811 | if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP) { |
1812 | accum += udf_count_free_bitmap(sb, | |
1813 | UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap); | |
1da177e4 LT |
1814 | } |
1815 | if (accum) | |
1816 | return accum; | |
1817 | ||
28de7948 CG |
1818 | if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE) { |
1819 | accum += udf_count_free_table(sb, | |
1820 | UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table); | |
1da177e4 | 1821 | } |
28de7948 CG |
1822 | if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE) { |
1823 | accum += udf_count_free_table(sb, | |
1824 | UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table); | |
1da177e4 LT |
1825 | } |
1826 | ||
1827 | return accum; | |
1828 | } |