super.c 51 KB

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