super.c 51 KB

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