~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/udf/super.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  *
 17  * CONTACTS
 18  *  E-mail regarding any portion of the Linux UDF file system should be
 19  *  directed to the development team mailing list (run by majordomo):
 20  *        linux_udf@hootie.lvld.hp.com
 21  *
 22  * COPYRIGHT
 23  *  This file is distributed under the terms of the GNU General Public
 24  *  License (GPL). Copies of the GPL can be obtained from:
 25  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
 26  *  Each contributing author retains all rights to their own work.
 27  *
 28  *  (C) 1998 Dave Boynton
 29  *  (C) 1998-2000 Ben Fennema
 30  *  (C) 2000 Stelias Computing Inc
 31  *
 32  * HISTORY
 33  *
 34  *  09/24/98 dgb  changed to allow compiling outside of kernel, and
 35  *                added some debugging.
 36  *  10/01/98 dgb  updated to allow (some) possibility of compiling w/2.0.34
 37  *  10/16/98      attempting some multi-session support
 38  *  10/17/98      added freespace count for "df"
 39  *  11/11/98 gr   added novrs option
 40  *  11/26/98 dgb  added fileset,anchor mount options
 41  *  12/06/98 blf  really hosed things royally. vat/sparing support. sequenced vol descs
 42  *                rewrote option handling based on isofs
 43  *  12/20/98      find the free space bitmap (if it exists)
 44  */
 45 
 46 #include "udfdecl.h"    
 47 
 48 #include <linux/config.h>
 49 #include <linux/version.h>
 50 #include <linux/blkdev.h>
 51 #include <linux/malloc.h>
 52 #include <linux/kernel.h>
 53 #include <linux/locks.h>
 54 #include <linux/module.h>
 55 #include <linux/stat.h>
 56 #include <linux/cdrom.h>
 57 #include <linux/nls.h>
 58 #include <asm/byteorder.h>
 59 
 60 #include <linux/udf_fs.h>
 61 #include "udf_sb.h"
 62 #include "udf_i.h"
 63 
 64 #include <linux/init.h>
 65 #include <asm/uaccess.h>
 66 
 67 #define VDS_POS_PRIMARY_VOL_DESC        0
 68 #define VDS_POS_UNALLOC_SPACE_DESC      1
 69 #define VDS_POS_LOGICAL_VOL_DESC        2
 70 #define VDS_POS_PARTITION_DESC          3
 71 #define VDS_POS_IMP_USE_VOL_DESC        4
 72 #define VDS_POS_VOL_DESC_PTR            5
 73 #define VDS_POS_TERMINATING_DESC        6
 74 #define VDS_POS_LENGTH                          7
 75 
 76 static char error_buf[1024];
 77 
 78 /* These are the "meat" - everything else is stuffing */
 79 static struct super_block *udf_read_super(struct super_block *, void *, int);
 80 static void udf_put_super(struct super_block *);
 81 static void udf_write_super(struct super_block *);
 82 static int udf_remount_fs(struct super_block *, int *, char *);
 83 static int udf_check_valid(struct super_block *, int, int);
 84 static int udf_vrs(struct super_block *sb, int silent);
 85 static int udf_load_partition(struct super_block *, lb_addr *);
 86 static int udf_load_logicalvol(struct super_block *, struct buffer_head *, lb_addr *);
 87 static void udf_load_logicalvolint(struct super_block *, extent_ad);
 88 static int udf_find_anchor(struct super_block *, int, int);
 89 static int udf_find_fileset(struct super_block *, lb_addr *, lb_addr *);
 90 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
 91 static void udf_load_fileset(struct super_block *, struct buffer_head *, lb_addr *);
 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 *);
 96 static int udf_statfs(struct super_block *, struct statfs *);
 97 
 98 /* UDF filesystem type */
 99 static DECLARE_FSTYPE_DEV(udf_fstype, "udf", udf_read_super);
100 
101 /* Superblock operations */
102 static struct super_operations udf_sb_ops = {
103         read_inode:             udf_read_inode,
104         write_inode:            udf_write_inode,
105         put_inode:              udf_put_inode,
106         delete_inode:           udf_delete_inode,
107         put_super:              udf_put_super,
108         write_super:            udf_write_super,
109         statfs:                 udf_statfs,
110         remount_fs:             udf_remount_fs,
111 };
112 
113 struct udf_options
114 {
115         unsigned char novrs;
116         unsigned int blocksize;
117         unsigned int session;
118         unsigned int lastblock;
119         unsigned int anchor;
120         unsigned int volume;
121         unsigned short partition;
122         unsigned int fileset;
123         unsigned int rootdir;
124         unsigned int flags;
125         mode_t umask;
126         gid_t gid;
127         uid_t uid;
128 };
129 
130 static int __init init_udf_fs(void)
131 {
132         printk(KERN_NOTICE "udf: registering filesystem\n");
133         return register_filesystem(&udf_fstype);
134 }
135 
136 static void __exit exit_udf_fs(void)
137 {
138         printk(KERN_NOTICE "udf: unregistering filesystem\n");
139         unregister_filesystem(&udf_fstype);
140 }
141 
142 module_init(init_udf_fs)
143 module_exit(exit_udf_fs)
144 
145 /*
146  * udf_parse_options
147  *
148  * PURPOSE
149  *      Parse mount options.
150  *
151  * DESCRIPTION
152  *      The following mount options are supported:
153  *
154  *      gid=            Set the default group.
155  *      umask=          Set the default umask.
156  *      uid=            Set the default user.
157  *      bs=                     Set the block size.
158  *      unhide          Show otherwise hidden files.
159  *      undelete        Show deleted files in lists.
160  *      adinicb         Embed data in the inode (default)
161  *      noadinicb       Don't embed data in the inode
162  *      shortad         Use short ad's
163  *      longad          Use long ad's (default)
164  *      strict          Set strict conformance (unused)
165  *
166  *      The remaining are for debugging and disaster recovery:
167  *
168  *      novrs           Skip volume sequence recognition 
169  *
170  *      The following expect a offset from 0.
171  *
172  *      session=        Set the CDROM session (default= last session)
173  *      anchor=         Override standard anchor location. (default= 256)
174  *      volume=         Override the VolumeDesc location. (unused)
175  *      partition=      Override the PartitionDesc location. (unused)
176  *      lastblock=      Set the last block of the filesystem/
177  *
178  *      The following expect a offset from the partition root.
179  *
180  *      fileset=        Override the fileset block location. (unused)
181  *      rootdir=        Override the root directory location. (unused)
182  *              WARNING: overriding the rootdir to a non-directory may
183  *              yield highly unpredictable results.
184  *
185  * PRE-CONDITIONS
186  *      options         Pointer to mount options string.
187  *      uopts           Pointer to mount options variable.
188  *
189  * POST-CONDITIONS
190  *      <return>        0       Mount options parsed okay.
191  *      <return>        -1      Error parsing mount options.
192  *
193  * HISTORY
194  *      July 1, 1997 - Andrew E. Mileski
195  *      Written, tested, and released.
196  */
197 
198 static int
199 udf_parse_options(char *options, struct udf_options *uopt)
200 {
201         char *opt, *val;
202 
203         uopt->novrs = 0;
204         uopt->blocksize = 512;
205         uopt->partition = 0xFFFF;
206         uopt->session = 0xFFFFFFFF;
207         uopt->lastblock = 0xFFFFFFFF;
208         uopt->anchor = 0xFFFFFFFF;
209         uopt->volume = 0xFFFFFFFF;
210         uopt->rootdir = 0xFFFFFFFF;
211         uopt->fileset = 0xFFFFFFFF;
212 
213         if (!options)
214                 return 1;
215 
216         for (opt = strtok(options, ","); opt; opt = strtok(NULL, ","))
217         {
218                 /* Make "opt=val" into two strings */
219                 val = strchr(opt, '=');
220                 if (val)
221                         *(val++) = 0;
222                 if (!strcmp(opt, "novrs") && !val)
223                         uopt->novrs = 1;
224                 else if (!strcmp(opt, "bs") && val)
225                         uopt->blocksize = simple_strtoul(val, NULL, 0);
226                 else if (!strcmp(opt, "unhide") && !val)
227                         uopt->flags |= (1 << UDF_FLAG_UNHIDE);
228                 else if (!strcmp(opt, "undelete") && !val)
229                         uopt->flags |= (1 << UDF_FLAG_UNDELETE);
230                 else if (!strcmp(opt, "noadinicb") && !val)
231                         uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
232                 else if (!strcmp(opt, "adinicb") && !val)
233                         uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
234                 else if (!strcmp(opt, "shortad") && !val)
235                         uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
236                 else if (!strcmp(opt, "longad") && !val)
237                         uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
238                 else if (!strcmp(opt, "gid") && val)
239                         uopt->gid = simple_strtoul(val, NULL, 0);
240                 else if (!strcmp(opt, "umask") && val)
241                         uopt->umask = simple_strtoul(val, NULL, 0);
242                 else if (!strcmp(opt, "strict") && !val)
243                         uopt->flags |= (1 << UDF_FLAG_STRICT);
244                 else if (!strcmp(opt, "uid") && val)
245                         uopt->uid = simple_strtoul(val, NULL, 0);
246                 else if (!strcmp(opt, "session") && val)
247                         uopt->session = simple_strtoul(val, NULL, 0);
248                 else if (!strcmp(opt, "lastblock") && val)
249                         uopt->lastblock = simple_strtoul(val, NULL, 0);
250                 else if (!strcmp(opt, "anchor") && val)
251                         uopt->anchor = simple_strtoul(val, NULL, 0);
252                 else if (!strcmp(opt, "volume") && val)
253                         uopt->volume = simple_strtoul(val, NULL, 0);
254                 else if (!strcmp(opt, "partition") && val)
255                         uopt->partition = simple_strtoul(val, NULL, 0);
256                 else if (!strcmp(opt, "fileset") && val)
257                         uopt->fileset = simple_strtoul(val, NULL, 0);
258                 else if (!strcmp(opt, "rootdir") && val)
259                         uopt->rootdir = simple_strtoul(val, NULL, 0);
260                 else if (val)
261                 {
262                         printk(KERN_ERR "udf: bad mount option \"%s=%s\"\n",
263                                 opt, val);
264                         return 0;
265                 }
266                 else
267                 {
268                         printk(KERN_ERR "udf: bad mount option \"%s\"\n",
269                                 opt);
270                         return 0;
271                 }
272         }
273         return 1;
274 }
275 
276 void
277 udf_write_super(struct super_block *sb)
278 {
279         if (!(sb->s_flags & MS_RDONLY))
280                 udf_open_lvid(sb);
281         sb->s_dirt = 0;
282 }
283                 
284 static int
285 udf_remount_fs(struct super_block *sb, int *flags, char *options)
286 {
287         struct udf_options uopt;
288 
289         uopt.flags = UDF_SB(sb)->s_flags ;
290         uopt.uid   = UDF_SB(sb)->s_uid ;
291         uopt.gid   = UDF_SB(sb)->s_gid ;
292         uopt.umask = UDF_SB(sb)->s_umask ;
293 
294         if ( !udf_parse_options(options, &uopt) )
295                 return -EINVAL;
296 
297         UDF_SB(sb)->s_flags = uopt.flags;
298         UDF_SB(sb)->s_uid   = uopt.uid;
299         UDF_SB(sb)->s_gid   = uopt.gid;
300         UDF_SB(sb)->s_umask = uopt.umask;
301 
302 #if CONFIG_UDF_RW != 1
303         *flags |= MS_RDONLY;
304 #endif
305 
306         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
307                 return 0;
308         if (*flags & MS_RDONLY)
309                 udf_close_lvid(sb);
310         else
311                 udf_open_lvid(sb);
312 
313         return 0;
314 }
315 
316 /*
317  * udf_set_blocksize
318  *
319  * PURPOSE
320  *      Set the block size to be used in all transfers.
321  *
322  * DESCRIPTION
323  *      To allow room for a DMA transfer, it is best to guess big when unsure.
324  *      This routine picks 2048 bytes as the blocksize when guessing. This
325  *      should be adequate until devices with larger block sizes become common.
326  *
327  *      Note that the Linux kernel can currently only deal with blocksizes of
328  *      512, 1024, 2048, 4096, and 8192 bytes.
329  *
330  * PRE-CONDITIONS
331  *      sb                      Pointer to _locked_ superblock.
332  *
333  * POST-CONDITIONS
334  *      sb->s_blocksize         Blocksize.
335  *      sb->s_blocksize_bits    log2 of blocksize.
336  *      <return>        0       Blocksize is valid.
337  *      <return>        1       Blocksize is invalid.
338  *
339  * HISTORY
340  *      July 1, 1997 - Andrew E. Mileski
341  *      Written, tested, and released.
342  */
343 static  int
344 udf_set_blocksize(struct super_block *sb, int bsize)
345 {
346         /* Use specified block size if specified */
347         if (!(sb->s_blocksize = get_hardblocksize(sb->s_dev)))
348                 sb->s_blocksize = 2048;
349         if (bsize > sb->s_blocksize)
350                 sb->s_blocksize = bsize;
351 
352         /* Block size must be an even multiple of 512 */
353         switch (sb->s_blocksize) {
354                 case 512: sb->s_blocksize_bits = 9;     break;
355                 case 1024: sb->s_blocksize_bits = 10; break;
356                 case 2048: sb->s_blocksize_bits = 11; break;
357                 case 4096: sb->s_blocksize_bits = 12; break;
358                 case 8192: sb->s_blocksize_bits = 13; break;
359                 default:
360                 {
361                         udf_debug("Bad block size (%ld)\n", sb->s_blocksize);
362                         printk(KERN_ERR "udf: bad block size (%ld)\n", sb->s_blocksize);
363                         return 0;
364                 }
365         }
366 
367         /* Set the block size */
368         set_blocksize(sb->s_dev, sb->s_blocksize);
369         return sb->s_blocksize;
370 }
371 
372 static int
373 udf_vrs(struct super_block *sb, int silent)
374 {
375         struct VolStructDesc *vsd = NULL;
376         int sector = 32768;
377         struct buffer_head *bh = NULL;
378         int iso9660=0;
379         int nsr02=0;
380         int nsr03=0;
381 
382         /* Block size must be a multiple of 512 */
383         if (sb->s_blocksize & 511)
384                 return sector;
385 
386         sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
387 
388         udf_debug("Starting at sector %u (%ld byte sectors)\n",
389                 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
390         /* Process the sequence (if applicable) */
391         for (;!nsr02 && !nsr03; sector += 2048)
392         {
393                 /* Read a block */
394                 bh = udf_tread(sb, sector >> sb->s_blocksize_bits, sb->s_blocksize);
395                 if (!bh)
396                         break;
397 
398                 /* Look for ISO  descriptors */
399                 vsd = (struct VolStructDesc *)(bh->b_data +
400                         (sector & (sb->s_blocksize - 1)));
401 
402                 if (vsd->stdIdent[0] == 0)
403                 {
404                         udf_release_data(bh);
405                         break;
406                 }
407                 else if (!strncmp(vsd->stdIdent, STD_ID_CD001, STD_ID_LEN))
408                 {
409                         iso9660 = sector;
410                         switch (vsd->structType)
411                         {
412                                 case 0: 
413                                         udf_debug("ISO9660 Boot Record found\n");
414                                         break;
415                                 case 1: 
416                                         udf_debug("ISO9660 Primary Volume Descriptor found\n");
417                                         break;
418                                 case 2: 
419                                         udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
420                                         break;
421                                 case 3: 
422                                         udf_debug("ISO9660 Volume Partition Descriptor found\n");
423                                         break;
424                                 case 255: 
425                                         udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
426                                         break;
427                                 default: 
428                                         udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
429                                         break;
430                         }
431                 }
432                 else if (!strncmp(vsd->stdIdent, STD_ID_BEA01, STD_ID_LEN))
433                 {
434                 }
435                 else if (!strncmp(vsd->stdIdent, STD_ID_TEA01, STD_ID_LEN))
436                 {
437                         udf_release_data(bh);
438                         break;
439                 }
440                 else if (!strncmp(vsd->stdIdent, STD_ID_NSR02, STD_ID_LEN))
441                 {
442                         nsr02 = sector;
443                 }
444                 else if (!strncmp(vsd->stdIdent, STD_ID_NSR03, STD_ID_LEN))
445                 {
446                         nsr03 = sector;
447                 }
448                 udf_release_data(bh);
449         }
450 
451         if (nsr03)
452                 return nsr03;
453         else if (nsr02)
454                 return nsr02;
455         else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
456                 return -1;
457         else
458                 return 0;
459 }
460 
461 /*
462  * udf_find_anchor
463  *
464  * PURPOSE
465  *      Find an anchor volume descriptor.
466  *
467  * PRE-CONDITIONS
468  *      sb                      Pointer to _locked_ superblock.
469  *      lastblock               Last block on media.
470  *
471  * POST-CONDITIONS
472  *      <return>                1 if not found, 0 if ok
473  *
474  * HISTORY
475  *      July 1, 1997 - Andrew E. Mileski
476  *      Written, tested, and released.
477  */
478 static int
479 udf_find_anchor(struct super_block *sb, int useranchor, int lastblock)
480 {
481         int varlastblock = udf_variable_to_fixed(lastblock);
482         int last[] =  { lastblock, lastblock - 2,
483                                         lastblock - 150, lastblock - 152,
484                                         varlastblock, varlastblock - 2,
485                                         varlastblock - 150, varlastblock - 152 };
486         struct buffer_head *bh = NULL;
487         Uint16 ident;
488         Uint32 location;
489         int i;
490 
491         UDF_SB_ANCHOR(sb)[0] = 0;
492         UDF_SB_ANCHOR(sb)[1] = 0;
493         UDF_SB_ANCHOR(sb)[2] = 0;
494         UDF_SB_ANCHOR(sb)[3] = 256 + UDF_SB_SESSION(sb);
495 
496         lastblock = 0;
497 
498         /* Search for an anchor volume descriptor pointer */
499 
500         /*  according to spec, anchor is in either:
501          *     block 256
502          *     lastblock-256
503          *     lastblock
504          *  however, if the disc isn't closed, it could be 512 */
505 
506         for (i=0; (!lastblock && i<sizeof(last)/sizeof(int)); i++)
507         {
508                 if (!(bh = bread(sb->s_dev, last[i], sb->s_blocksize)))
509                 {
510                         ident = location = 0;
511                 }
512                 else
513                 {
514                         ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
515                         location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
516                         udf_release_data(bh);
517                 }
518 
519                 if (ident == TID_ANCHOR_VOL_DESC_PTR)
520                 {
521                         if (location == last[i] - UDF_SB_SESSION(sb))
522                         {
523                                 lastblock = UDF_SB_ANCHOR(sb)[0] = last[i];
524                                 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
525                         }
526                         else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb))
527                         {
528                                 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
529                                 lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]);
530                                 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
531                         }
532                         else
533                                 udf_debug("Anchor found at block %d, location mismatch %d.\n",
534                                         last[i], location);
535                 }
536                 else if (ident == TID_FILE_ENTRY || ident == TID_EXTENDED_FILE_ENTRY)
537                 {
538                         lastblock = last[i];
539                         UDF_SB_ANCHOR(sb)[2] = 512 + UDF_SB_SESSION(sb);
540                 }
541                 else
542                 {
543                         if (!(bh = bread(sb->s_dev, last[i] - 256, sb->s_blocksize)))
544                         {
545                                 ident = location = 0;
546                         }
547                         else
548                         {
549                                 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
550                                 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
551                                 udf_release_data(bh);
552                         }
553 
554                         if (ident == TID_ANCHOR_VOL_DESC_PTR &&
555                                 location == last[i] - 256 - UDF_SB_SESSION(sb))
556                         {
557                                 lastblock = last[i];
558                                 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
559                         }
560                         else
561                         {
562                                 if (!(bh = bread(sb->s_dev, last[i] - 312 - UDF_SB_SESSION(sb),
563                                         sb->s_blocksize)))
564                                 {
565                                         ident = location = 0;
566                                 }
567                                 else
568                                 {
569                                         ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
570                                         location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
571                                         udf_release_data(bh);
572                                 }
573 
574                                 if (ident == TID_ANCHOR_VOL_DESC_PTR &&
575                                         location == udf_variable_to_fixed(last[i]) - 256)
576                                 {
577                                         UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
578                                         lastblock = udf_variable_to_fixed(last[i]);
579                                         UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
580                                 }
581                         }
582                 }
583         }
584 
585         if (!lastblock)
586         {
587                 /* We havn't found the lastblock. check 312 */
588                 if ((bh = bread(sb->s_dev, 312 + UDF_SB_SESSION(sb), sb->s_blocksize)))
589                 {
590                         ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
591                         location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
592                         udf_release_data(bh);
593 
594                         if (ident == TID_ANCHOR_VOL_DESC_PTR && location == 256)
595                                 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
596                 }
597         }
598 
599         for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
600         {
601                 if (UDF_SB_ANCHOR(sb)[i])
602                 {
603                         if (!(bh = udf_read_tagged(sb,
604                                 UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
605                         {
606                                 UDF_SB_ANCHOR(sb)[i] = 0;
607                         }
608                         else
609                         {
610                                 udf_release_data(bh);
611                                 if ((ident != TID_ANCHOR_VOL_DESC_PTR) && (i ||
612                                         (ident != TID_FILE_ENTRY && ident != TID_EXTENDED_FILE_ENTRY)))
613                                 {
614                                         UDF_SB_ANCHOR(sb)[i] = 0;
615                                 }
616                         }
617                 }
618                 else if (useranchor != 0xFFFFFFFF)
619                 {
620                         UDF_SB_ANCHOR(sb)[i] = useranchor;
621                         useranchor = 0xFFFFFFFF;
622                         i --;
623                 }
624         }
625 
626         return lastblock;
627 }
628 
629 static int 
630 udf_find_fileset(struct super_block *sb, lb_addr *fileset, lb_addr *root)
631 {
632         struct buffer_head *bh = NULL;
633         long lastblock;
634         Uint16 ident;
635 
636         if (fileset->logicalBlockNum != 0xFFFFFFFF ||
637                 fileset->partitionReferenceNum != 0xFFFF)
638         {
639                 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
640 
641                 if (!bh)
642                         return 1;
643                 else if (ident != TID_FILE_SET_DESC)
644                 {
645                         udf_release_data(bh);
646                         return 1;
647                 }
648                         
649         }
650 
651         if (!bh) /* Search backwards through the partitions */
652         {
653                 lb_addr newfileset;
654 
655                 return 1;
656                 
657                 for (newfileset.partitionReferenceNum=UDF_SB_NUMPARTS(sb)-1;
658                         (newfileset.partitionReferenceNum != 0xFFFF &&
659                                 fileset->logicalBlockNum == 0xFFFFFFFF &&
660                                 fileset->partitionReferenceNum == 0xFFFF);
661                         newfileset.partitionReferenceNum--)
662                 {
663                         lastblock = UDF_SB_PARTLEN(sb, newfileset.partitionReferenceNum);
664                         newfileset.logicalBlockNum = 0;
665 
666                         do
667                         {
668                                 bh = udf_read_ptagged(sb, newfileset, 0, &ident);
669                                 if (!bh)
670                                 {
671                                         newfileset.logicalBlockNum ++;
672                                         continue;
673                                 }
674 
675                                 switch (ident)
676                                 {
677                                         case TID_SPACE_BITMAP_DESC:
678                                         {
679                                                 struct SpaceBitmapDesc *sp;
680                                                 sp = (struct SpaceBitmapDesc *)bh->b_data;
681                                                 newfileset.logicalBlockNum += 1 +
682                                                         ((le32_to_cpu(sp->numOfBytes) + sizeof(struct SpaceBitmapDesc) - 1)
683                                                                 >> sb->s_blocksize_bits);
684                                                 udf_release_data(bh);
685                                                 break;
686                                         }
687                                         case TID_FILE_SET_DESC:
688                                         {
689                                                 *fileset = newfileset;
690                                                 break;
691                                         }
692                                         default:
693                                         {
694                                                 newfileset.logicalBlockNum ++;
695                                                 udf_release_data(bh);
696                                                 bh = NULL;
697                                                 break;
698                                         }
699                                 }
700                         }
701                         while (newfileset.logicalBlockNum < lastblock &&
702                                 fileset->logicalBlockNum == 0xFFFFFFFF &&
703                                 fileset->partitionReferenceNum == 0xFFFF);
704                 }
705         }
706 
707         if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
708                 fileset->partitionReferenceNum != 0xFFFF) && bh)
709         {
710                 udf_debug("Fileset at block=%d, partition=%d\n",
711                         fileset->logicalBlockNum, fileset->partitionReferenceNum);
712 
713                 UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
714                 udf_load_fileset(sb, bh, root);
715                 udf_release_data(bh);
716                 return 0;
717         }
718         return 1;
719 }
720 
721 static void 
722 udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
723 {
724         struct PrimaryVolDesc *pvoldesc;
725         time_t recording;
726         long recording_usec;
727         struct ustr instr;
728         struct ustr outstr;
729 
730         pvoldesc = (struct PrimaryVolDesc *)bh->b_data;
731 
732         if ( udf_stamp_to_time(&recording, &recording_usec,
733                 lets_to_cpu(pvoldesc->recordingDateAndTime)) )
734         {
735                 timestamp ts;
736                 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
737                 udf_debug("recording time %ld/%ld, %04u/%02u/%02u %02u:%02u (%x)\n",
738                         recording, recording_usec,
739                         ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.typeAndTimezone);
740                 UDF_SB_RECORDTIME(sb) = recording;
741         }
742 
743         if ( !udf_build_ustr(&instr, pvoldesc->volIdent, 32) )
744         {
745                 if (udf_CS0toUTF8(&outstr, &instr))
746                 {
747                         strncpy( UDF_SB_VOLIDENT(sb), outstr.u_name,
748                                 outstr.u_len > 31 ? 31 : outstr.u_len);
749                         udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb));
750                 }
751         }
752 
753         if ( !udf_build_ustr(&instr, pvoldesc->volSetIdent, 128) )
754         {
755                 if (udf_CS0toUTF8(&outstr, &instr))
756                         udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
757         }
758 }
759 
760 static void 
761 udf_load_fileset(struct super_block *sb, struct buffer_head *bh, lb_addr *root)
762 {
763         struct FileSetDesc *fset;
764 
765         fset = (struct FileSetDesc *)bh->b_data;
766 
767         *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
768 
769         UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
770 
771         udf_debug("Rootdir at block=%d, partition=%d\n", 
772                 root->logicalBlockNum, root->partitionReferenceNum);
773 }
774 
775 static void 
776 udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
777 {
778         struct PartitionDesc *p;
779         int i;
780 
781         p=(struct PartitionDesc *)bh->b_data;
782 
783         for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
784         {
785                 udf_debug("Searching map: (%d == %d)\n", 
786                         UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber));
787                 if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber))
788                 {
789                         UDF_SB_PARTLEN(sb,i) = le32_to_cpu(p->partitionLength); /* blocks */
790                         UDF_SB_PARTROOT(sb,i) = le32_to_cpu(p->partitionStartingLocation) + UDF_SB_SESSION(sb);
791 
792                         if (UDF_SB_PARTTYPE(sb,i) == UDF_SPARABLE_MAP15)
793                                 udf_fill_spartable(sb, &UDF_SB_TYPESPAR(sb,i), UDF_SB_PARTLEN(sb,i));
794 
795                         if (!strcmp(p->partitionContents.ident, PARTITION_CONTENTS_NSR02) ||
796                                 !strcmp(p->partitionContents.ident, PARTITION_CONTENTS_NSR03))
797                         {
798                                 struct PartitionHeaderDesc *phd;
799 
800                                 phd = (struct PartitionHeaderDesc *)(p->partitionContentsUse);
801                                 if (phd->unallocatedSpaceTable.extLength)
802                                         udf_debug("unallocatedSpaceTable (part %d)\n", i);
803                                 if (phd->unallocatedSpaceBitmap.extLength)
804                                 {
805                                         UDF_SB_PARTMAPS(sb)[i].s_uspace.bitmap =
806                                                 le32_to_cpu(phd->unallocatedSpaceBitmap.extPosition);
807                                         UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_BITMAP;
808                                         udf_debug("unallocatedSpaceBitmap (part %d) @ %d\n",
809                                                 i, UDF_SB_PARTMAPS(sb)[i].s_uspace.bitmap);
810                                 }
811                                 if (phd->partitionIntegrityTable.extLength)
812                                         udf_debug("partitionIntegrityTable (part %d)\n", i);
813                                 if (phd->freedSpaceTable.extLength)
814                                         udf_debug("freedSpaceTable (part %d)\n", i);
815                                 if (phd->freedSpaceBitmap.extLength)
816                                 {
817                                         UDF_SB_PARTMAPS(sb)[i].s_fspace.bitmap =
818                                                 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
819                                         UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_BITMAP;
820                                         udf_debug("freedSpaceBitmap (part %d) @ %d\n",
821                                                 i, UDF_SB_PARTMAPS(sb)[i].s_fspace.bitmap);
822                                 }
823                         }
824                         break;
825                 }
826         }
827         if (i == UDF_SB_NUMPARTS(sb))
828         {
829                 udf_debug("Partition (%d) not found in partition map\n", le16_to_cpu(p->partitionNumber));
830         }
831         else
832         {
833                 udf_debug("Partition (%d:%d type %x) starts at physical %d, block length %d\n",
834                         le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb,i),
835                         UDF_SB_PARTROOT(sb,i), UDF_SB_PARTLEN(sb,i));
836         }
837 }
838 
839 static int 
840 udf_load_logicalvol(struct super_block *sb, struct buffer_head * bh, lb_addr *fileset)
841 {
842         struct LogicalVolDesc *lvd;
843         int i, j, offset;
844         Uint8 type;
845 
846         lvd = (struct LogicalVolDesc *)bh->b_data;
847 
848         UDF_SB_NUMPARTS(sb) = le32_to_cpu(lvd->numPartitionMaps);
849         UDF_SB_ALLOC_PARTMAPS(sb, UDF_SB_NUMPARTS(sb));
850 
851         for (i=0,offset=0;
852                  i<UDF_SB_NUMPARTS(sb) && offset<le32_to_cpu(lvd->mapTableLength);
853                  i++,offset+=((struct GenericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength)
854         {
855                 type = ((struct GenericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
856                 udf_debug("Partition (%d) type %d\n", i, type);
857                 if (type == 1)
858                 {
859                         struct GenericPartitionMap1 *gpm1 = (struct GenericPartitionMap1 *)&(lvd->partitionMaps[offset]);
860                         UDF_SB_PARTTYPE(sb,i) = UDF_TYPE1_MAP15;
861                         UDF_SB_PARTVSN(sb,i) = le16_to_cpu(gpm1->volSeqNum);
862                         UDF_SB_PARTNUM(sb,i) = le16_to_cpu(gpm1->partitionNum);
863                         UDF_SB_PARTFUNC(sb,i) = NULL;
864                 }
865                 else if (type == 2)
866                 {
867                         struct UdfPartitionMap2 *upm2 = (struct UdfPartitionMap2 *)&(lvd->partitionMaps[offset]);
868                         if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL)))
869                         {
870                                 if (le16_to_cpu(((Uint16 *)upm2->partIdent.identSuffix)[0]) == 0x0150)
871                                 {
872                                         UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP15;
873                                         UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt15;
874                                 }
875                                 else if (le16_to_cpu(((Uint16 *)upm2->partIdent.identSuffix)[0]) == 0x0200)
876                                 {
877                                         UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP20;
878                                         UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt20;
879                                 }
880                         }
881                         else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE)))
882                         {
883                                 int plen;
884 
885                                 struct SparablePartitionMap *spm = (struct SparablePartitionMap *)&(lvd->partitionMaps[offset]);
886                                 UDF_SB_PARTTYPE(sb,i) = UDF_SPARABLE_MAP15;
887                                 plen = le16_to_cpu(spm->packetLength);
888                                 UDF_SB_TYPESPAR(sb,i).s_spar_pshift = 0;
889                                 while (plen >>= 1)
890                                         UDF_SB_TYPESPAR(sb,i).s_spar_pshift ++;
891                                 for (j=0; j<spm->numSparingTables; j++)
892                                         UDF_SB_TYPESPAR(sb,i).s_spar_loc[j] = le32_to_cpu(spm->locSparingTable[j]);
893                                 UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_spar15;
894                         }
895                         else
896                         {
897                                 udf_debug("Unknown ident: %s\n", upm2->partIdent.ident);
898                                 continue;
899                         }
900                         UDF_SB_PARTVSN(sb,i) = le16_to_cpu(upm2->volSeqNum);
901                         UDF_SB_PARTNUM(sb,i) = le16_to_cpu(upm2->partitionNum);
902                 }
903         }
904 
905         if (fileset)
906         {
907                 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
908 
909                 *fileset = lelb_to_cpu(la->extLocation);
910                 udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
911                         fileset->logicalBlockNum,
912                         fileset->partitionReferenceNum);
913         }
914         if (lvd->integritySeqExt.extLength)
915                 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
916         return 0;
917 }
918 
919 /*
920  * udf_load_logicalvolint
921  *
922  */
923 static void
924 udf_load_logicalvolint(struct super_block *sb, extent_ad loc)
925 {
926         struct buffer_head *bh = NULL;
927         Uint16 ident;
928 
929         while ((bh = udf_read_tagged(sb, loc.extLocation, loc.extLocation, &ident)) &&
930                 ident == TID_LOGICAL_VOL_INTEGRITY_DESC && loc.extLength > 0)
931         {
932                 UDF_SB_LVIDBH(sb) = bh;
933                 
934                 if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
935                         udf_load_logicalvolint(sb, leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt));
936                 
937                 if (UDF_SB_LVIDBH(sb) != bh)
938                         udf_release_data(bh);
939                 loc.extLength -= sb->s_blocksize;
940                 loc.extLocation ++;
941         }
942         if (UDF_SB_LVIDBH(sb) != bh)
943                 udf_release_data(bh);
944 }
945 
946 /*
947  * udf_process_sequence
948  *
949  * PURPOSE
950  *      Process a main/reserve volume descriptor sequence.
951  *
952  * PRE-CONDITIONS
953  *      sb                      Pointer to _locked_ superblock.
954  *      block                   First block of first extent of the sequence.
955  *      lastblock               Lastblock of first extent of the sequence.
956  *
957  * HISTORY
958  *      July 1, 1997 - Andrew E. Mileski
959  *      Written, tested, and released.
960  */
961 static  int
962 udf_process_sequence(struct super_block *sb, long block, long lastblock, lb_addr *fileset)
963 {
964         struct buffer_head *bh = NULL;
965         struct udf_vds_record vds[VDS_POS_LENGTH];
966         struct GenericDesc *gd;
967         int done=0;
968         int i,j;
969         Uint32 vdsn;
970         Uint16 ident;
971 
972         memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
973 
974         /* Read the main descriptor sequence */
975         for (;(!done && block <= lastblock); block++)
976         {
977 
978                 bh = udf_read_tagged(sb, block, block, &ident);
979                 if (!bh) 
980                         break;
981 
982                 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
983                 gd = (struct GenericDesc *)bh->b_data;
984                 vdsn = le32_to_cpu(gd->volDescSeqNum);
985                 switch (ident)
986                 {
987                         case TID_PRIMARY_VOL_DESC: /* ISO 13346 3/10.1 */
988                                 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum)
989                                 {
990                                         vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
991                                         vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
992                                 }
993                                 break;
994                         case TID_VOL_DESC_PTR: /* ISO 13346 3/10.3 */
995                                 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum)
996                                 {
997                                         vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
998                                         vds[VDS_POS_VOL_DESC_PTR].block = block;
999                                 }
1000                                 break;
1001                         case TID_IMP_USE_VOL_DESC: /* ISO 13346 3/10.4 */
1002                                 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum)
1003                                 {
1004                                         vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
1005                                         vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1006                                 }
1007                                 break;
1008                         case TID_PARTITION_DESC: /* ISO 13346 3/10.5 */
1009                                 if (!vds[VDS_POS_PARTITION_DESC].block)
1010                                         vds[VDS_POS_PARTITION_DESC].block = block;
1011                                 break;
1012                         case TID_LOGICAL_VOL_DESC: /* ISO 13346 3/10.6 */
1013                                 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum)
1014                                 {
1015                                         vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
1016                                         vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1017                                 }
1018                                 break;
1019                         case TID_UNALLOC_SPACE_DESC: /* ISO 13346 3/10.8 */
1020                                 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum)
1021                                 {
1022                                         vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
1023                                         vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1024                                 }
1025                                 break;
1026                         case TID_TERMINATING_DESC: /* ISO 13346 3/10.9 */
1027                                 vds[VDS_POS_TERMINATING_DESC].block = block;
1028                                 done = 1;
1029                                 break;
1030                 }
1031                 udf_release_data(bh);
1032         }
1033         for (i=0; i<VDS_POS_LENGTH; i++)
1034         {
1035                 if (vds[i].block)
1036                 {
1037                         bh = udf_read_tagged(sb, vds[i].block, vds[i].block, &ident);
1038 
1039                         if (i == VDS_POS_PRIMARY_VOL_DESC)
1040                                 udf_load_pvoldesc(sb, bh);
1041                         else if (i == VDS_POS_LOGICAL_VOL_DESC)
1042                                 udf_load_logicalvol(sb, bh, fileset);
1043                         else if (i == VDS_POS_PARTITION_DESC)
1044                         {
1045                                 struct buffer_head *bh2 = NULL;
1046                                 udf_load_partdesc(sb, bh);
1047                                 for (j=vds[i].block+1; j<vds[VDS_POS_TERMINATING_DESC].block; j++)
1048                                 {
1049                                         bh2 = udf_read_tagged(sb, j, j, &ident);
1050                                         gd = (struct GenericDesc *)bh2->b_data;
1051                                         if (ident == TID_PARTITION_DESC)
1052                                                 udf_load_partdesc(sb, bh2);
1053                                         udf_release_data(bh2);
1054                                 }
1055                         }
1056                         udf_release_data(bh);
1057                 }
1058         }
1059 
1060         return 0;
1061 }
1062 
1063 /*
1064  * udf_check_valid()
1065  */
1066 static int
1067 udf_check_valid(struct super_block *sb, int novrs, int silent)
1068 {
1069         long block;
1070 
1071         if (novrs)
1072         {
1073                 udf_debug("Validity check skipped because of novrs option\n");
1074                 return 0;
1075         }
1076         /* Check that it is NSR02 compliant */
1077         /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1078         else if ((block = udf_vrs(sb, silent)) == -1)
1079         {
1080                 udf_debug("Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
1081                 return 0;
1082         }
1083         else 
1084                 return !block;
1085 }
1086 
1087 static int
1088 udf_load_partition(struct super_block *sb, lb_addr *fileset)
1089 {
1090         struct AnchorVolDescPtr *anchor;
1091         Uint16 ident;
1092         struct buffer_head *bh;
1093         long main_s, main_e, reserve_s, reserve_e;
1094         int i, j;
1095 
1096         if (!sb)
1097                 return 1;
1098 
1099         for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
1100         {
1101                 if (UDF_SB_ANCHOR(sb)[i] && (bh = udf_read_tagged(sb,
1102                         UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i] - UDF_SB_SESSION(sb), &ident)))
1103                 {
1104                         anchor = (struct AnchorVolDescPtr *)bh->b_data;
1105 
1106                         /* Locate the main sequence */
1107                         main_s = le32_to_cpu( anchor->mainVolDescSeqExt.extLocation );
1108                         main_e = le32_to_cpu( anchor->mainVolDescSeqExt.extLength );
1109                         main_e = main_e >> sb->s_blocksize_bits;
1110                         main_e += main_s;
1111         
1112                         /* Locate the reserve sequence */
1113                         reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1114                         reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1115                         reserve_e = reserve_e >> sb->s_blocksize_bits;
1116                         reserve_e += reserve_s;
1117 
1118                         udf_release_data(bh);
1119 
1120                         /* Process the main & reserve sequences */
1121                         /* responsible for finding the PartitionDesc(s) */
1122                         if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
1123                                 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
1124                         {
1125                                 break;
1126                         }
1127                 }
1128         }
1129 
1130         if (i == sizeof(UDF_SB_ANCHOR(sb))/sizeof(int))
1131         {
1132                 udf_debug("No Anchor block found\n");
1133                 return 1;
1134         }
1135         else
1136                 udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1137 
1138         for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
1139         {
1140                 switch UDF_SB_PARTTYPE(sb, i)
1141                 {
1142                         case UDF_VIRTUAL_MAP15:
1143                         case UDF_VIRTUAL_MAP20:
1144                         {
1145                                 lb_addr ino;
1146 
1147                                 if (!UDF_SB_LASTBLOCK(sb))
1148                                 {
1149                                         udf_debug("Unable to determine Lastblock (For Virtual Partition)\n");
1150                                         return 1;
1151                                 }
1152 
1153                                 for (j=0; j<UDF_SB_NUMPARTS(sb); j++)
1154                                 {
1155                                         if (j != i &&
1156                                                 UDF_SB_PARTVSN(sb,i) == UDF_SB_PARTVSN(sb,j) &&
1157                                                 UDF_SB_PARTNUM(sb,i) == UDF_SB_PARTNUM(sb,j))
1158                                         {
1159                                                         ino.partitionReferenceNum = j;
1160                                                         ino.logicalBlockNum = UDF_SB_LASTBLOCK(sb) -
1161                                                                 UDF_SB_PARTROOT(sb,j);
1162                                                         break;
1163                                         }
1164                                 }
1165 
1166                                 if (j == UDF_SB_NUMPARTS(sb))
1167                                         return 1;
1168 
1169                                 if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
1170                                         return 1;
1171 
1172                                 if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP15)
1173                                 {
1174                                         UDF_SB_TYPEVIRT(sb,i).s_start_offset = udf_ext0_offset(UDF_SB_VAT(sb));
1175                                         UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size - 36) >> 2;
1176                                 }
1177                                 else if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP20)
1178                                 {
1179                                         struct buffer_head *bh = NULL;
1180                                         Uint32 pos;
1181 
1182                                         pos = udf_block_map(UDF_SB_VAT(sb), 0);
1183                                         bh = bread(sb->s_dev, pos, sb->s_blocksize);
1184                                         UDF_SB_TYPEVIRT(sb,i).s_start_offset =
1185                                                 le16_to_cpu(((struct VirtualAllocationTable20 *)bh->b_data + udf_ext0_offset(UDF_SB_VAT(sb)))->lengthHeader) +
1186                                                         udf_ext0_offset(UDF_SB_VAT(sb));
1187                                         UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size -
1188                                                 UDF_SB_TYPEVIRT(sb,i).s_start_offset) >> 2;
1189                                         udf_release_data(bh);
1190                                 }
1191                                 UDF_SB_PARTROOT(sb,i) = udf_get_pblock(sb, 0, i, 0);
1192                                 UDF_SB_PARTLEN(sb,i) = UDF_SB_PARTLEN(sb,ino.partitionReferenceNum);
1193                         }
1194                 }
1195         }
1196         return 0;
1197 }
1198 
1199 static void udf_open_lvid(struct super_block *sb)
1200 {
1201         if (UDF_SB_LVIDBH(sb))
1202         {
1203                 int i;
1204                 timestamp cpu_time;
1205 
1206                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1207                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1208                 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME, CURRENT_UTIME))
1209                         UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1210                 UDF_SB_LVID(sb)->integrityType = INTEGRITY_TYPE_OPEN;
1211 
1212                 UDF_SB_LVID(sb)->descTag.descCRC =
1213                         cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1214                         le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1215 
1216                 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1217                 for (i=0; i<16; i++)
1218                         if (i != 4)
1219                                 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1220                                         ((Uint8 *)&(UDF_SB_LVID(sb)->descTag))[i];
1221 
1222                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1223                 sb->s_dirt = 0;
1224         }
1225 }
1226 
1227 static void udf_close_lvid(struct super_block *sb)
1228 {
1229         if (UDF_SB_LVIDBH(sb) &&
1230                 UDF_SB_LVID(sb)->integrityType == INTEGRITY_TYPE_OPEN)
1231         {
1232                 int i;
1233                 timestamp cpu_time;
1234 
1235                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1236                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1237                 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME, CURRENT_UTIME))
1238                         UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1239                 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
1240                         UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1241                 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
1242                         UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1243                 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
1244                         UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1245                 UDF_SB_LVID(sb)->integrityType = INTEGRITY_TYPE_CLOSE;
1246 
1247                 UDF_SB_LVID(sb)->descTag.descCRC =
1248                         cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1249                         le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1250 
1251                 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1252                 for (i=0; i<16; i++)
1253                         if (i != 4)
1254                                 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1255                                         ((Uint8 *)&(UDF_SB_LVID(sb)->descTag))[i];
1256 
1257                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1258         }
1259 }
1260 
1261 /*
1262  * udf_read_super
1263  *
1264  * PURPOSE
1265  *      Complete the specified super block.
1266  *
1267  * PRE-CONDITIONS
1268  *      sb                      Pointer to superblock to complete - never NULL.
1269  *      sb->s_dev               Device to read suberblock from.
1270  *      options                 Pointer to mount options.
1271  *      silent                  Silent flag.
1272  *
1273  * HISTORY
1274  *      July 1, 1997 - Andrew E. Mileski
1275  *      Written, tested, and released.
1276  */
1277 static struct super_block *
1278 udf_read_super(struct super_block *sb, void *options, int silent)
1279 {
1280         struct inode *inode=NULL;
1281         struct udf_options uopt;
1282         lb_addr rootdir, fileset;
1283         int i;
1284 
1285         uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB);
1286         uopt.uid = -1;
1287         uopt.gid = -1;
1288         uopt.umask = 0;
1289 
1290         memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
1291 
1292 #if CONFIG_UDF_RW != 1
1293         sb->s_flags |= MS_RDONLY;
1294 #endif
1295 
1296         if (!udf_parse_options((char *)options, &uopt))
1297                 goto error_out;
1298 
1299         fileset.logicalBlockNum = 0xFFFFFFFF;
1300         fileset.partitionReferenceNum = 0xFFFF;
1301 
1302         UDF_SB(sb)->s_flags = uopt.flags;
1303         UDF_SB(sb)->s_uid = uopt.uid;
1304         UDF_SB(sb)->s_gid = uopt.gid;
1305         UDF_SB(sb)->s_umask = uopt.umask;
1306 
1307         /* Set the block size for all transfers */
1308         if (!udf_set_blocksize(sb, uopt.blocksize))
1309                 goto error_out;
1310 
1311         if ( uopt.session == 0xFFFFFFFF )
1312                 UDF_SB_SESSION(sb) = udf_get_last_session(sb);
1313         else
1314                 UDF_SB_SESSION(sb) = uopt.session;
1315 
1316         udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1317 
1318         if ( uopt.lastblock == 0xFFFFFFFF )
1319                 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1320         else
1321                 UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1322 
1323         UDF_SB_LASTBLOCK(sb) = udf_find_anchor(sb, uopt.anchor, UDF_SB_LASTBLOCK(sb));
1324 
1325         udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1326 
1327         if (udf_check_valid(sb, uopt.novrs, silent)) /* read volume recognition sequences */
1328         {
1329                 printk("UDF-fs: No VRS found\n");
1330                 goto error_out;
1331         }
1332 
1333         /* Fill in the rest of the superblock */
1334         sb->s_op = &udf_sb_ops;
1335         sb->dq_op = NULL;
1336         sb->s_dirt = 0;
1337         sb->s_magic = UDF_SUPER_MAGIC;
1338 
1339         for (i=0; i<UDF_MAX_BLOCK_LOADED; i++)
1340         {
1341                 UDF_SB_BLOCK_BITMAP_NUMBER(sb,i) = 0;
1342                 UDF_SB_BLOCK_BITMAP(sb,i) = NULL;
1343         }
1344         UDF_SB_LOADED_BLOCK_BITMAPS(sb) = 0;
1345 
1346         if (udf_load_partition(sb, &fileset))
1347         {
1348                 printk("UDF-fs: No partition found (1)\n");
1349                 goto error_out;
1350         }
1351 
1352         if ( UDF_SB_LVIDBH(sb) )
1353         {
1354                 Uint16 minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
1355                 Uint16 minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
1356                 /* Uint16 maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
1357 
1358                 if (minUDFReadRev > UDF_MAX_READ_VERSION)
1359                 {
1360                         printk("UDF-fs: minUDFReadRev=%x (max is %x)\n",
1361                                 UDF_SB_LVIDIU(sb)->minUDFReadRev, UDF_MAX_READ_VERSION);
1362                         goto error_out;
1363                 }
1364                 else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1365                 {
1366                         sb->s_flags |= MS_RDONLY;
1367                 }
1368 
1369                 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1370                         UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1371                 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1372                         UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1373         }
1374 
1375         if ( !UDF_SB_NUMPARTS(sb) )
1376         {
1377                 printk("UDF-fs: No partition found (2)\n");
1378                 goto error_out;
1379         }
1380 
1381         if ( udf_find_fileset(sb, &fileset, &rootdir) )
1382         {
1383                 printk("UDF-fs: No fileset found\n");
1384                 goto error_out;
1385         }
1386 
1387         if (!silent)
1388         {
1389                 timestamp ts;
1390                 udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb), 0);
1391                 udf_info("UDF %s (%s) Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1392                         UDFFS_VERSION, UDFFS_DATE,
1393                         UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute,
1394                         ts.typeAndTimezone);
1395         }
1396         if (!(sb->s_flags & MS_RDONLY))
1397                 udf_open_lvid(sb);
1398 
1399         /* Assign the root inode */
1400         /* assign inodes by physical block number */
1401         /* perhaps it's not extensible enough, but for now ... */
1402         inode = udf_iget(sb, rootdir); 
1403         if (!inode)
1404         {
1405                 printk("UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
1406                         rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1407                 goto error_out;
1408         }
1409 
1410         /* Allocate a dentry for the root inode */
1411         sb->s_root = d_alloc_root(inode);
1412         if (!sb->s_root)
1413         {
1414                 printk("UDF-fs: Couldn't allocate root dentry\n");
1415                 iput(inode);
1416                 goto error_out;
1417         }
1418 
1419         return sb;
1420 
1421 error_out:
1422         if (UDF_SB_VAT(sb))
1423                 iput(UDF_SB_VAT(sb));
1424         if (!(sb->s_flags & MS_RDONLY))
1425                 udf_close_lvid(sb);
1426         udf_release_data(UDF_SB_LVIDBH(sb));
1427         UDF_SB_FREE(sb);
1428         return NULL;
1429 }
1430 
1431 void udf_error(struct super_block *sb, const char *function,
1432         const char *fmt, ...)
1433 {
1434         va_list args;
1435 
1436         if (!(sb->s_flags & MS_RDONLY))
1437         {
1438                 /* mark sb error */
1439                 sb->s_dirt = 1;
1440         }
1441         va_start(args, fmt);
1442         vsprintf(error_buf, fmt, args);
1443         va_end(args);
1444         printk (KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1445                 bdevname(sb->s_dev), function, error_buf);
1446 }
1447 
1448 void udf_warning(struct super_block *sb, const char *function,
1449         const char *fmt, ...)
1450 {
1451         va_list args;
1452 
1453         va_start (args, fmt);
1454         vsprintf(error_buf, fmt, args);
1455         va_end(args);
1456         printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1457                 bdevname(sb->s_dev), function, error_buf);
1458 }
1459 
1460 /*
1461  * udf_put_super
1462  *
1463  * PURPOSE
1464  *      Prepare for destruction of the superblock.
1465  *
1466  * DESCRIPTION
1467  *      Called before the filesystem is unmounted.
1468  *
1469  * HISTORY
1470  *      July 1, 1997 - Andrew E. Mileski
1471  *      Written, tested, and released.
1472  */
1473 static void
1474 udf_put_super(struct super_block *sb)
1475 {
1476         int i;
1477 
1478         if (UDF_SB_VAT(sb))
1479                 iput(UDF_SB_VAT(sb));
1480         if (!(sb->s_flags & MS_RDONLY))
1481                 udf_close_lvid(sb);
1482         udf_release_data(UDF_SB_LVIDBH(sb));
1483         for (i=0; i<UDF_MAX_BLOCK_LOADED; i++)
1484                 udf_release_data(UDF_SB_BLOCK_BITMAP(sb, i));
1485         UDF_SB_FREE(sb);
1486 }
1487 
1488 /*
1489  * udf_stat_fs
1490  *
1491  * PURPOSE
1492  *      Return info about the filesystem.
1493  *
1494  * DESCRIPTION
1495  *      Called by sys_statfs()
1496  *
1497  * HISTORY
1498  *      July 1, 1997 - Andrew E. Mileski
1499  *      Written, tested, and released.
1500  */
1501 static int
1502 udf_statfs(struct super_block *sb, struct statfs *buf)
1503 {
1504         buf->f_type = UDF_SUPER_MAGIC;
1505         buf->f_bsize = sb->s_blocksize;
1506         buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
1507         buf->f_bfree = udf_count_free(sb);
1508         buf->f_bavail = buf->f_bfree;
1509         buf->f_files = (UDF_SB_LVIDBH(sb) ?
1510                 (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
1511                 le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + buf->f_bfree;
1512         buf->f_ffree = buf->f_bfree;
1513         /* __kernel_fsid_t f_fsid */
1514         buf->f_namelen = UDF_NAME_LEN;
1515 
1516         return 0;
1517 }
1518 
1519 static unsigned char udf_bitmap_lookup[16] = {
1520         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1521 };
1522         
1523 static unsigned int
1524 udf_count_free(struct super_block *sb)
1525 {
1526         struct buffer_head *bh = NULL;
1527         unsigned int accum = 0;
1528         lb_addr loc;
1529         Uint32 bitmap;
1530 
1531         if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1532                 bitmap = UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.bitmap;
1533         else if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1534                 bitmap = UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.bitmap;
1535         else
1536                 bitmap = 0xFFFFFFFF;
1537 
1538         if (bitmap != 0xFFFFFFFF)
1539         {
1540                 struct SpaceBitmapDesc *bm;
1541                 int block = 0, newblock, index;
1542                 Uint16 ident;
1543                 Uint32 bytes;
1544                 Uint8 value;
1545                 Uint8 * ptr;
1546 
1547                 loc.logicalBlockNum = bitmap;
1548                 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
1549                 bh = udf_read_ptagged(sb, loc, 0, &ident);
1550 
1551                 if (!bh)
1552                 {
1553                         printk(KERN_ERR "udf: udf_count_free failed\n");
1554                         return 0;
1555                 }
1556                 else if (ident != TID_SPACE_BITMAP_DESC)
1557                 {
1558                         udf_release_data(bh);
1559                         printk(KERN_ERR "udf: udf_count_free failed\n");
1560                         return 0;
1561                 }
1562 
1563                 bm = (struct SpaceBitmapDesc *)bh->b_data;
1564                 bytes = bm->numOfBytes;
1565                 index = sizeof(struct SpaceBitmapDesc); /* offset in first block only */
1566                 ptr = (Uint8 *)bh->b_data;
1567 
1568                 while ( bytes > 0 )
1569                 {
1570                         while ((bytes > 0) && (index < sb->s_blocksize))
1571                         {
1572                                 value = ptr[index];
1573                                 accum += udf_bitmap_lookup[ value & 0x0f ];
1574                                 accum += udf_bitmap_lookup[ value >> 4 ];
1575                                 index++;
1576                                 bytes--;
1577                         }
1578                         if ( bytes )
1579                         {
1580                                 udf_release_data(bh);
1581                                 newblock = udf_get_lb_pblock(sb, loc, ++block);
1582                                 bh = udf_tread(sb, newblock, sb->s_blocksize);
1583                                 if (!bh)
1584                                 {
1585                                         udf_debug("read failed\n");
1586                                         return accum;
1587                                 }
1588                                 index = 0;
1589                                 ptr = (Uint8 *)bh->b_data;
1590                         }
1591                 }
1592                 udf_release_data(bh);
1593         }
1594         else
1595         {
1596                 if (UDF_SB_LVIDBH(sb))
1597                 {
1598                         if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb))
1599                                 accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]);
1600 
1601                         if (accum == 0xFFFFFFFF)
1602                                 accum = 0;
1603                 }
1604         }
1605 
1606         return accum;
1607 }
1608 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.