super.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  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 struct kmem_cache * 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, GFP_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, struct kmem_cache * cachep, unsigned long flags)
  119. {
  120. struct udf_inode_info *ei = (struct udf_inode_info *) foo;
  121. if (flags & SLAB_CTOR_CONSTRUCTOR) {
  122. ei->i_ext.i_data = NULL;
  123. inode_init_once(&ei->vfs_inode);
  124. }
  125. }
  126. static int init_inodecache(void)
  127. {
  128. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  129. sizeof(struct udf_inode_info),
  130. 0, (SLAB_RECLAIM_ACCOUNT|
  131. SLAB_MEM_SPREAD),
  132. init_once, NULL);
  133. if (udf_inode_cachep == NULL)
  134. return -ENOMEM;
  135. return 0;
  136. }
  137. static void destroy_inodecache(void)
  138. {
  139. kmem_cache_destroy(udf_inode_cachep);
  140. }
  141. /* Superblock operations */
  142. static const struct super_operations udf_sb_ops = {
  143. .alloc_inode = udf_alloc_inode,
  144. .destroy_inode = udf_destroy_inode,
  145. .write_inode = udf_write_inode,
  146. .delete_inode = udf_delete_inode,
  147. .clear_inode = udf_clear_inode,
  148. .put_super = udf_put_super,
  149. .write_super = udf_write_super,
  150. .statfs = udf_statfs,
  151. .remount_fs = udf_remount_fs,
  152. };
  153. struct udf_options
  154. {
  155. unsigned char novrs;
  156. unsigned int blocksize;
  157. unsigned int session;
  158. unsigned int lastblock;
  159. unsigned int anchor;
  160. unsigned int volume;
  161. unsigned short partition;
  162. unsigned int fileset;
  163. unsigned int rootdir;
  164. unsigned int flags;
  165. mode_t umask;
  166. gid_t gid;
  167. uid_t uid;
  168. struct nls_table *nls_map;
  169. };
  170. static int __init init_udf_fs(void)
  171. {
  172. int err;
  173. err = init_inodecache();
  174. if (err)
  175. goto out1;
  176. err = register_filesystem(&udf_fstype);
  177. if (err)
  178. goto out;
  179. return 0;
  180. out:
  181. destroy_inodecache();
  182. out1:
  183. return err;
  184. }
  185. static void __exit exit_udf_fs(void)
  186. {
  187. unregister_filesystem(&udf_fstype);
  188. destroy_inodecache();
  189. }
  190. module_init(init_udf_fs)
  191. module_exit(exit_udf_fs)
  192. /*
  193. * udf_parse_options
  194. *
  195. * PURPOSE
  196. * Parse mount options.
  197. *
  198. * DESCRIPTION
  199. * The following mount options are supported:
  200. *
  201. * gid= Set the default group.
  202. * umask= Set the default umask.
  203. * uid= Set the default user.
  204. * bs= Set the block size.
  205. * unhide Show otherwise hidden files.
  206. * undelete Show deleted files in lists.
  207. * adinicb Embed data in the inode (default)
  208. * noadinicb Don't embed data in the inode
  209. * shortad Use short ad's
  210. * longad Use long ad's (default)
  211. * nostrict Unset strict conformance
  212. * iocharset= Set the NLS character set
  213. *
  214. * The remaining are for debugging and disaster recovery:
  215. *
  216. * novrs Skip volume sequence recognition
  217. *
  218. * The following expect a offset from 0.
  219. *
  220. * session= Set the CDROM session (default= last session)
  221. * anchor= Override standard anchor location. (default= 256)
  222. * volume= Override the VolumeDesc location. (unused)
  223. * partition= Override the PartitionDesc location. (unused)
  224. * lastblock= Set the last block of the filesystem/
  225. *
  226. * The following expect a offset from the partition root.
  227. *
  228. * fileset= Override the fileset block location. (unused)
  229. * rootdir= Override the root directory location. (unused)
  230. * WARNING: overriding the rootdir to a non-directory may
  231. * yield highly unpredictable results.
  232. *
  233. * PRE-CONDITIONS
  234. * options Pointer to mount options string.
  235. * uopts Pointer to mount options variable.
  236. *
  237. * POST-CONDITIONS
  238. * <return> 1 Mount options parsed okay.
  239. * <return> 0 Error parsing mount options.
  240. *
  241. * HISTORY
  242. * July 1, 1997 - Andrew E. Mileski
  243. * Written, tested, and released.
  244. */
  245. enum {
  246. Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
  247. Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
  248. Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
  249. Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
  250. Opt_rootdir, Opt_utf8, Opt_iocharset,
  251. Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
  252. };
  253. static match_table_t tokens = {
  254. {Opt_novrs, "novrs"},
  255. {Opt_nostrict, "nostrict"},
  256. {Opt_bs, "bs=%u"},
  257. {Opt_unhide, "unhide"},
  258. {Opt_undelete, "undelete"},
  259. {Opt_noadinicb, "noadinicb"},
  260. {Opt_adinicb, "adinicb"},
  261. {Opt_shortad, "shortad"},
  262. {Opt_longad, "longad"},
  263. {Opt_uforget, "uid=forget"},
  264. {Opt_uignore, "uid=ignore"},
  265. {Opt_gforget, "gid=forget"},
  266. {Opt_gignore, "gid=ignore"},
  267. {Opt_gid, "gid=%u"},
  268. {Opt_uid, "uid=%u"},
  269. {Opt_umask, "umask=%o"},
  270. {Opt_session, "session=%u"},
  271. {Opt_lastblock, "lastblock=%u"},
  272. {Opt_anchor, "anchor=%u"},
  273. {Opt_volume, "volume=%u"},
  274. {Opt_partition, "partition=%u"},
  275. {Opt_fileset, "fileset=%u"},
  276. {Opt_rootdir, "rootdir=%u"},
  277. {Opt_utf8, "utf8"},
  278. {Opt_iocharset, "iocharset=%s"},
  279. {Opt_err, NULL}
  280. };
  281. static int
  282. udf_parse_options(char *options, struct udf_options *uopt)
  283. {
  284. char *p;
  285. int option;
  286. uopt->novrs = 0;
  287. uopt->blocksize = 2048;
  288. uopt->partition = 0xFFFF;
  289. uopt->session = 0xFFFFFFFF;
  290. uopt->lastblock = 0;
  291. uopt->anchor = 0;
  292. uopt->volume = 0xFFFFFFFF;
  293. uopt->rootdir = 0xFFFFFFFF;
  294. uopt->fileset = 0xFFFFFFFF;
  295. uopt->nls_map = NULL;
  296. if (!options)
  297. return 1;
  298. while ((p = strsep(&options, ",")) != NULL)
  299. {
  300. substring_t args[MAX_OPT_ARGS];
  301. int token;
  302. if (!*p)
  303. continue;
  304. token = match_token(p, tokens, args);
  305. switch (token)
  306. {
  307. case Opt_novrs:
  308. uopt->novrs = 1;
  309. case Opt_bs:
  310. if (match_int(&args[0], &option))
  311. return 0;
  312. uopt->blocksize = option;
  313. break;
  314. case Opt_unhide:
  315. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  316. break;
  317. case Opt_undelete:
  318. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  319. break;
  320. case Opt_noadinicb:
  321. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  322. break;
  323. case Opt_adinicb:
  324. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  325. break;
  326. case Opt_shortad:
  327. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  328. break;
  329. case Opt_longad:
  330. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  331. break;
  332. case Opt_gid:
  333. if (match_int(args, &option))
  334. return 0;
  335. uopt->gid = option;
  336. break;
  337. case Opt_uid:
  338. if (match_int(args, &option))
  339. return 0;
  340. uopt->uid = option;
  341. break;
  342. case Opt_umask:
  343. if (match_octal(args, &option))
  344. return 0;
  345. uopt->umask = option;
  346. break;
  347. case Opt_nostrict:
  348. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  349. break;
  350. case Opt_session:
  351. if (match_int(args, &option))
  352. return 0;
  353. uopt->session = option;
  354. break;
  355. case Opt_lastblock:
  356. if (match_int(args, &option))
  357. return 0;
  358. uopt->lastblock = option;
  359. break;
  360. case Opt_anchor:
  361. if (match_int(args, &option))
  362. return 0;
  363. uopt->anchor = option;
  364. break;
  365. case Opt_volume:
  366. if (match_int(args, &option))
  367. return 0;
  368. uopt->volume = option;
  369. break;
  370. case Opt_partition:
  371. if (match_int(args, &option))
  372. return 0;
  373. uopt->partition = option;
  374. break;
  375. case Opt_fileset:
  376. if (match_int(args, &option))
  377. return 0;
  378. uopt->fileset = option;
  379. break;
  380. case Opt_rootdir:
  381. if (match_int(args, &option))
  382. return 0;
  383. uopt->rootdir = option;
  384. break;
  385. case Opt_utf8:
  386. uopt->flags |= (1 << UDF_FLAG_UTF8);
  387. break;
  388. #ifdef CONFIG_UDF_NLS
  389. case Opt_iocharset:
  390. uopt->nls_map = load_nls(args[0].from);
  391. uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
  392. break;
  393. #endif
  394. case Opt_uignore:
  395. uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
  396. break;
  397. case Opt_uforget:
  398. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  399. break;
  400. case Opt_gignore:
  401. uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
  402. break;
  403. case Opt_gforget:
  404. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  405. break;
  406. default:
  407. printk(KERN_ERR "udf: bad mount option \"%s\" "
  408. "or missing value\n", p);
  409. return 0;
  410. }
  411. }
  412. return 1;
  413. }
  414. void
  415. udf_write_super(struct super_block *sb)
  416. {
  417. lock_kernel();
  418. if (!(sb->s_flags & MS_RDONLY))
  419. udf_open_lvid(sb);
  420. sb->s_dirt = 0;
  421. unlock_kernel();
  422. }
  423. static int
  424. udf_remount_fs(struct super_block *sb, int *flags, char *options)
  425. {
  426. struct udf_options uopt;
  427. uopt.flags = UDF_SB(sb)->s_flags ;
  428. uopt.uid = UDF_SB(sb)->s_uid ;
  429. uopt.gid = UDF_SB(sb)->s_gid ;
  430. uopt.umask = UDF_SB(sb)->s_umask ;
  431. if ( !udf_parse_options(options, &uopt) )
  432. return -EINVAL;
  433. UDF_SB(sb)->s_flags = uopt.flags;
  434. UDF_SB(sb)->s_uid = uopt.uid;
  435. UDF_SB(sb)->s_gid = uopt.gid;
  436. UDF_SB(sb)->s_umask = uopt.umask;
  437. if (UDF_SB_LVIDBH(sb)) {
  438. int write_rev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
  439. if (write_rev > UDF_MAX_WRITE_VERSION)
  440. *flags |= MS_RDONLY;
  441. }
  442. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  443. return 0;
  444. if (*flags & MS_RDONLY)
  445. udf_close_lvid(sb);
  446. else
  447. udf_open_lvid(sb);
  448. return 0;
  449. }
  450. /*
  451. * udf_set_blocksize
  452. *
  453. * PURPOSE
  454. * Set the block size to be used in all transfers.
  455. *
  456. * DESCRIPTION
  457. * To allow room for a DMA transfer, it is best to guess big when unsure.
  458. * This routine picks 2048 bytes as the blocksize when guessing. This
  459. * should be adequate until devices with larger block sizes become common.
  460. *
  461. * Note that the Linux kernel can currently only deal with blocksizes of
  462. * 512, 1024, 2048, 4096, and 8192 bytes.
  463. *
  464. * PRE-CONDITIONS
  465. * sb Pointer to _locked_ superblock.
  466. *
  467. * POST-CONDITIONS
  468. * sb->s_blocksize Blocksize.
  469. * sb->s_blocksize_bits log2 of blocksize.
  470. * <return> 0 Blocksize is valid.
  471. * <return> 1 Blocksize is invalid.
  472. *
  473. * HISTORY
  474. * July 1, 1997 - Andrew E. Mileski
  475. * Written, tested, and released.
  476. */
  477. static int
  478. udf_set_blocksize(struct super_block *sb, int bsize)
  479. {
  480. if (!sb_min_blocksize(sb, bsize)) {
  481. udf_debug("Bad block size (%d)\n", bsize);
  482. printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
  483. return 0;
  484. }
  485. return sb->s_blocksize;
  486. }
  487. static int
  488. udf_vrs(struct super_block *sb, int silent)
  489. {
  490. struct volStructDesc *vsd = NULL;
  491. int sector = 32768;
  492. int sectorsize;
  493. struct buffer_head *bh = NULL;
  494. int iso9660=0;
  495. int nsr02=0;
  496. int nsr03=0;
  497. /* Block size must be a multiple of 512 */
  498. if (sb->s_blocksize & 511)
  499. return 0;
  500. if (sb->s_blocksize < sizeof(struct volStructDesc))
  501. sectorsize = sizeof(struct volStructDesc);
  502. else
  503. sectorsize = sb->s_blocksize;
  504. sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
  505. udf_debug("Starting at sector %u (%ld byte sectors)\n",
  506. (sector >> sb->s_blocksize_bits), sb->s_blocksize);
  507. /* Process the sequence (if applicable) */
  508. for (;!nsr02 && !nsr03; sector += sectorsize)
  509. {
  510. /* Read a block */
  511. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  512. if (!bh)
  513. break;
  514. /* Look for ISO descriptors */
  515. vsd = (struct volStructDesc *)(bh->b_data +
  516. (sector & (sb->s_blocksize - 1)));
  517. if (vsd->stdIdent[0] == 0)
  518. {
  519. brelse(bh);
  520. break;
  521. }
  522. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN))
  523. {
  524. iso9660 = sector;
  525. switch (vsd->structType)
  526. {
  527. case 0:
  528. udf_debug("ISO9660 Boot Record found\n");
  529. break;
  530. case 1:
  531. udf_debug("ISO9660 Primary Volume Descriptor found\n");
  532. break;
  533. case 2:
  534. udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
  535. break;
  536. case 3:
  537. udf_debug("ISO9660 Volume Partition Descriptor found\n");
  538. break;
  539. case 255:
  540. udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
  541. break;
  542. default:
  543. udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
  544. break;
  545. }
  546. }
  547. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN))
  548. {
  549. }
  550. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01, VSD_STD_ID_LEN))
  551. {
  552. brelse(bh);
  553. break;
  554. }
  555. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN))
  556. {
  557. nsr02 = sector;
  558. }
  559. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN))
  560. {
  561. nsr03 = sector;
  562. }
  563. brelse(bh);
  564. }
  565. if (nsr03)
  566. return nsr03;
  567. else if (nsr02)
  568. return nsr02;
  569. else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
  570. return -1;
  571. else
  572. return 0;
  573. }
  574. /*
  575. * udf_find_anchor
  576. *
  577. * PURPOSE
  578. * Find an anchor volume descriptor.
  579. *
  580. * PRE-CONDITIONS
  581. * sb Pointer to _locked_ superblock.
  582. * lastblock Last block on media.
  583. *
  584. * POST-CONDITIONS
  585. * <return> 1 if not found, 0 if ok
  586. *
  587. * HISTORY
  588. * July 1, 1997 - Andrew E. Mileski
  589. * Written, tested, and released.
  590. */
  591. static void
  592. udf_find_anchor(struct super_block *sb)
  593. {
  594. int lastblock = UDF_SB_LASTBLOCK(sb);
  595. struct buffer_head *bh = NULL;
  596. uint16_t ident;
  597. uint32_t location;
  598. int i;
  599. if (lastblock)
  600. {
  601. int varlastblock = udf_variable_to_fixed(lastblock);
  602. int last[] = { lastblock, lastblock - 2,
  603. lastblock - 150, lastblock - 152,
  604. varlastblock, varlastblock - 2,
  605. varlastblock - 150, varlastblock - 152 };
  606. lastblock = 0;
  607. /* Search for an anchor volume descriptor pointer */
  608. /* according to spec, anchor is in either:
  609. * block 256
  610. * lastblock-256
  611. * lastblock
  612. * however, if the disc isn't closed, it could be 512 */
  613. for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
  614. if (last[i] < 0 || !(bh = sb_bread(sb, last[i])))
  615. {
  616. ident = location = 0;
  617. }
  618. else
  619. {
  620. ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
  621. location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
  622. brelse(bh);
  623. }
  624. if (ident == TAG_IDENT_AVDP)
  625. {
  626. if (location == last[i] - UDF_SB_SESSION(sb))
  627. {
  628. lastblock = UDF_SB_ANCHOR(sb)[0] = last[i] - UDF_SB_SESSION(sb);
  629. UDF_SB_ANCHOR(sb)[1] = last[i] - 256 - UDF_SB_SESSION(sb);
  630. }
  631. else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb))
  632. {
  633. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  634. lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb);
  635. UDF_SB_ANCHOR(sb)[1] = lastblock - 256 - UDF_SB_SESSION(sb);
  636. }
  637. else
  638. udf_debug("Anchor found at block %d, location mismatch %d.\n",
  639. last[i], location);
  640. }
  641. else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE)
  642. {
  643. lastblock = last[i];
  644. UDF_SB_ANCHOR(sb)[3] = 512;
  645. }
  646. else
  647. {
  648. if (last[i] < 256 || !(bh = sb_bread(sb, last[i] - 256)))
  649. {
  650. ident = location = 0;
  651. }
  652. else
  653. {
  654. ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
  655. location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
  656. brelse(bh);
  657. }
  658. if (ident == TAG_IDENT_AVDP &&
  659. location == last[i] - 256 - UDF_SB_SESSION(sb))
  660. {
  661. lastblock = last[i];
  662. UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
  663. }
  664. else
  665. {
  666. if (last[i] < 312 + UDF_SB_SESSION(sb) || !(bh = sb_bread(sb, last[i] - 312 - UDF_SB_SESSION(sb))))
  667. {
  668. ident = location = 0;
  669. }
  670. else
  671. {
  672. ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
  673. location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
  674. brelse(bh);
  675. }
  676. if (ident == TAG_IDENT_AVDP &&
  677. location == udf_variable_to_fixed(last[i]) - 256)
  678. {
  679. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  680. lastblock = udf_variable_to_fixed(last[i]);
  681. UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
  682. }
  683. }
  684. }
  685. }
  686. }
  687. if (!lastblock)
  688. {
  689. /* We havn't found the lastblock. check 312 */
  690. if ((bh = sb_bread(sb, 312 + UDF_SB_SESSION(sb))))
  691. {
  692. ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
  693. location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
  694. brelse(bh);
  695. if (ident == TAG_IDENT_AVDP && location == 256)
  696. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  697. }
  698. }
  699. for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
  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. brelse(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. brelse(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. brelse(bh);
  768. break;
  769. }
  770. case TAG_IDENT_FSD:
  771. {
  772. *fileset = newfileset;
  773. break;
  774. }
  775. default:
  776. {
  777. newfileset.logicalBlockNum ++;
  778. brelse(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. brelse(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. brelse(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. brelse(bh);
  1041. loc.extLength -= sb->s_blocksize;
  1042. loc.extLocation ++;
  1043. }
  1044. if (UDF_SB_LVIDBH(sb) != bh)
  1045. brelse(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. brelse(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. brelse(bh2);
  1164. }
  1165. }
  1166. brelse(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 < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
  1206. if (UDF_SB_ANCHOR(sb)[i] && (bh = udf_read_tagged(sb,
  1207. UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
  1208. {
  1209. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1210. /* Locate the main sequence */
  1211. main_s = le32_to_cpu( anchor->mainVolDescSeqExt.extLocation );
  1212. main_e = le32_to_cpu( anchor->mainVolDescSeqExt.extLength );
  1213. main_e = main_e >> sb->s_blocksize_bits;
  1214. main_e += main_s;
  1215. /* Locate the reserve sequence */
  1216. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1217. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1218. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1219. reserve_e += reserve_s;
  1220. brelse(bh);
  1221. /* Process the main & reserve sequences */
  1222. /* responsible for finding the PartitionDesc(s) */
  1223. if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
  1224. udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
  1225. {
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. if (i == ARRAY_SIZE(UDF_SB_ANCHOR(sb))) {
  1231. udf_debug("No Anchor block found\n");
  1232. return 1;
  1233. } else
  1234. udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
  1235. for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
  1236. {
  1237. switch UDF_SB_PARTTYPE(sb, i)
  1238. {
  1239. case UDF_VIRTUAL_MAP15:
  1240. case UDF_VIRTUAL_MAP20:
  1241. {
  1242. kernel_lb_addr ino;
  1243. if (!UDF_SB_LASTBLOCK(sb))
  1244. {
  1245. UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
  1246. udf_find_anchor(sb);
  1247. }
  1248. if (!UDF_SB_LASTBLOCK(sb))
  1249. {
  1250. udf_debug("Unable to determine Lastblock (For Virtual Partition)\n");
  1251. return 1;
  1252. }
  1253. for (j=0; j<UDF_SB_NUMPARTS(sb); j++)
  1254. {
  1255. if (j != i &&
  1256. UDF_SB_PARTVSN(sb,i) == UDF_SB_PARTVSN(sb,j) &&
  1257. UDF_SB_PARTNUM(sb,i) == UDF_SB_PARTNUM(sb,j))
  1258. {
  1259. ino.partitionReferenceNum = j;
  1260. ino.logicalBlockNum = UDF_SB_LASTBLOCK(sb) -
  1261. UDF_SB_PARTROOT(sb,j);
  1262. break;
  1263. }
  1264. }
  1265. if (j == UDF_SB_NUMPARTS(sb))
  1266. return 1;
  1267. if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
  1268. return 1;
  1269. if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP15)
  1270. {
  1271. UDF_SB_TYPEVIRT(sb,i).s_start_offset = udf_ext0_offset(UDF_SB_VAT(sb));
  1272. UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size - 36) >> 2;
  1273. }
  1274. else if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP20)
  1275. {
  1276. struct buffer_head *bh = NULL;
  1277. uint32_t pos;
  1278. pos = udf_block_map(UDF_SB_VAT(sb), 0);
  1279. bh = sb_bread(sb, pos);
  1280. if (!bh)
  1281. return 1;
  1282. UDF_SB_TYPEVIRT(sb,i).s_start_offset =
  1283. le16_to_cpu(((struct virtualAllocationTable20 *)bh->b_data + udf_ext0_offset(UDF_SB_VAT(sb)))->lengthHeader) +
  1284. udf_ext0_offset(UDF_SB_VAT(sb));
  1285. UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size -
  1286. UDF_SB_TYPEVIRT(sb,i).s_start_offset) >> 2;
  1287. brelse(bh);
  1288. }
  1289. UDF_SB_PARTROOT(sb,i) = udf_get_pblock(sb, 0, i, 0);
  1290. UDF_SB_PARTLEN(sb,i) = UDF_SB_PARTLEN(sb,ino.partitionReferenceNum);
  1291. }
  1292. }
  1293. }
  1294. return 0;
  1295. }
  1296. static void udf_open_lvid(struct super_block *sb)
  1297. {
  1298. if (UDF_SB_LVIDBH(sb))
  1299. {
  1300. int i;
  1301. kernel_timestamp cpu_time;
  1302. UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1303. UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1304. if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
  1305. UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
  1306. UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_OPEN;
  1307. UDF_SB_LVID(sb)->descTag.descCRC =
  1308. cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
  1309. le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
  1310. UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
  1311. for (i=0; i<16; i++)
  1312. if (i != 4)
  1313. UDF_SB_LVID(sb)->descTag.tagChecksum +=
  1314. ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i];
  1315. mark_buffer_dirty(UDF_SB_LVIDBH(sb));
  1316. }
  1317. }
  1318. static void udf_close_lvid(struct super_block *sb)
  1319. {
  1320. if (UDF_SB_LVIDBH(sb) &&
  1321. UDF_SB_LVID(sb)->integrityType == LVID_INTEGRITY_TYPE_OPEN)
  1322. {
  1323. int i;
  1324. kernel_timestamp cpu_time;
  1325. UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1326. UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1327. if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
  1328. UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
  1329. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
  1330. UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1331. if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
  1332. UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb));
  1333. if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
  1334. UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb));
  1335. UDF_SB_LVID(sb)->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1336. UDF_SB_LVID(sb)->descTag.descCRC =
  1337. cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
  1338. le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
  1339. UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
  1340. for (i=0; i<16; i++)
  1341. if (i != 4)
  1342. UDF_SB_LVID(sb)->descTag.tagChecksum +=
  1343. ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i];
  1344. mark_buffer_dirty(UDF_SB_LVIDBH(sb));
  1345. }
  1346. }
  1347. /*
  1348. * udf_read_super
  1349. *
  1350. * PURPOSE
  1351. * Complete the specified super block.
  1352. *
  1353. * PRE-CONDITIONS
  1354. * sb Pointer to superblock to complete - never NULL.
  1355. * sb->s_dev Device to read suberblock from.
  1356. * options Pointer to mount options.
  1357. * silent Silent flag.
  1358. *
  1359. * HISTORY
  1360. * July 1, 1997 - Andrew E. Mileski
  1361. * Written, tested, and released.
  1362. */
  1363. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1364. {
  1365. int i;
  1366. struct inode *inode=NULL;
  1367. struct udf_options uopt;
  1368. kernel_lb_addr rootdir, fileset;
  1369. struct udf_sb_info *sbi;
  1370. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1371. uopt.uid = -1;
  1372. uopt.gid = -1;
  1373. uopt.umask = 0;
  1374. sbi = kmalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1375. if (!sbi)
  1376. return -ENOMEM;
  1377. sb->s_fs_info = sbi;
  1378. memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
  1379. mutex_init(&sbi->s_alloc_mutex);
  1380. if (!udf_parse_options((char *)options, &uopt))
  1381. goto error_out;
  1382. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1383. uopt.flags & (1 << UDF_FLAG_NLS_MAP))
  1384. {
  1385. udf_error(sb, "udf_read_super",
  1386. "utf8 cannot be combined with iocharset\n");
  1387. goto error_out;
  1388. }
  1389. #ifdef CONFIG_UDF_NLS
  1390. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map)
  1391. {
  1392. uopt.nls_map = load_nls_default();
  1393. if (!uopt.nls_map)
  1394. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1395. else
  1396. udf_debug("Using default NLS map\n");
  1397. }
  1398. #endif
  1399. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1400. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1401. fileset.logicalBlockNum = 0xFFFFFFFF;
  1402. fileset.partitionReferenceNum = 0xFFFF;
  1403. UDF_SB(sb)->s_flags = uopt.flags;
  1404. UDF_SB(sb)->s_uid = uopt.uid;
  1405. UDF_SB(sb)->s_gid = uopt.gid;
  1406. UDF_SB(sb)->s_umask = uopt.umask;
  1407. UDF_SB(sb)->s_nls_map = uopt.nls_map;
  1408. /* Set the block size for all transfers */
  1409. if (!udf_set_blocksize(sb, uopt.blocksize))
  1410. goto error_out;
  1411. if ( uopt.session == 0xFFFFFFFF )
  1412. UDF_SB_SESSION(sb) = udf_get_last_session(sb);
  1413. else
  1414. UDF_SB_SESSION(sb) = uopt.session;
  1415. udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
  1416. UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
  1417. UDF_SB_ANCHOR(sb)[0] = UDF_SB_ANCHOR(sb)[1] = 0;
  1418. UDF_SB_ANCHOR(sb)[2] = uopt.anchor;
  1419. UDF_SB_ANCHOR(sb)[3] = 256;
  1420. if (udf_check_valid(sb, uopt.novrs, silent)) /* read volume recognition sequences */
  1421. {
  1422. printk("UDF-fs: No VRS found\n");
  1423. goto error_out;
  1424. }
  1425. udf_find_anchor(sb);
  1426. /* Fill in the rest of the superblock */
  1427. sb->s_op = &udf_sb_ops;
  1428. sb->dq_op = NULL;
  1429. sb->s_dirt = 0;
  1430. sb->s_magic = UDF_SUPER_MAGIC;
  1431. sb->s_time_gran = 1000;
  1432. if (udf_load_partition(sb, &fileset))
  1433. {
  1434. printk("UDF-fs: No partition found (1)\n");
  1435. goto error_out;
  1436. }
  1437. udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
  1438. if ( UDF_SB_LVIDBH(sb) )
  1439. {
  1440. uint16_t minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
  1441. uint16_t minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
  1442. /* uint16_t maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
  1443. if (minUDFReadRev > UDF_MAX_READ_VERSION)
  1444. {
  1445. printk("UDF-fs: minUDFReadRev=%x (max is %x)\n",
  1446. le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev),
  1447. UDF_MAX_READ_VERSION);
  1448. goto error_out;
  1449. }
  1450. else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1451. {
  1452. sb->s_flags |= MS_RDONLY;
  1453. }
  1454. UDF_SB_UDFREV(sb) = minUDFWriteRev;
  1455. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1456. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1457. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1458. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1459. }
  1460. if ( !UDF_SB_NUMPARTS(sb) )
  1461. {
  1462. printk("UDF-fs: No partition found (2)\n");
  1463. goto error_out;
  1464. }
  1465. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_READ_ONLY) {
  1466. printk("UDF-fs: Partition marked readonly; forcing readonly mount\n");
  1467. sb->s_flags |= MS_RDONLY;
  1468. }
  1469. if ( udf_find_fileset(sb, &fileset, &rootdir) )
  1470. {
  1471. printk("UDF-fs: No fileset found\n");
  1472. goto error_out;
  1473. }
  1474. if (!silent)
  1475. {
  1476. kernel_timestamp ts;
  1477. udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb));
  1478. udf_info("UDF %s (%s) Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1479. UDFFS_VERSION, UDFFS_DATE,
  1480. UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute,
  1481. ts.typeAndTimezone);
  1482. }
  1483. if (!(sb->s_flags & MS_RDONLY))
  1484. udf_open_lvid(sb);
  1485. /* Assign the root inode */
  1486. /* assign inodes by physical block number */
  1487. /* perhaps it's not extensible enough, but for now ... */
  1488. inode = udf_iget(sb, rootdir);
  1489. if (!inode)
  1490. {
  1491. printk("UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
  1492. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1493. goto error_out;
  1494. }
  1495. /* Allocate a dentry for the root inode */
  1496. sb->s_root = d_alloc_root(inode);
  1497. if (!sb->s_root)
  1498. {
  1499. printk("UDF-fs: Couldn't allocate root dentry\n");
  1500. iput(inode);
  1501. goto error_out;
  1502. }
  1503. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1504. return 0;
  1505. error_out:
  1506. if (UDF_SB_VAT(sb))
  1507. iput(UDF_SB_VAT(sb));
  1508. if (UDF_SB_NUMPARTS(sb))
  1509. {
  1510. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
  1511. iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
  1512. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
  1513. iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
  1514. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
  1515. UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb),s_uspace);
  1516. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
  1517. UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb),s_fspace);
  1518. if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15)
  1519. {
  1520. for (i=0; i<4; i++)
  1521. brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
  1522. }
  1523. }
  1524. #ifdef CONFIG_UDF_NLS
  1525. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1526. unload_nls(UDF_SB(sb)->s_nls_map);
  1527. #endif
  1528. if (!(sb->s_flags & MS_RDONLY))
  1529. udf_close_lvid(sb);
  1530. brelse(UDF_SB_LVIDBH(sb));
  1531. UDF_SB_FREE(sb);
  1532. kfree(sbi);
  1533. sb->s_fs_info = NULL;
  1534. return -EINVAL;
  1535. }
  1536. void udf_error(struct super_block *sb, const char *function,
  1537. const char *fmt, ...)
  1538. {
  1539. va_list args;
  1540. if (!(sb->s_flags & MS_RDONLY))
  1541. {
  1542. /* mark sb error */
  1543. sb->s_dirt = 1;
  1544. }
  1545. va_start(args, fmt);
  1546. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1547. va_end(args);
  1548. printk (KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
  1549. sb->s_id, function, error_buf);
  1550. }
  1551. void udf_warning(struct super_block *sb, const char *function,
  1552. const char *fmt, ...)
  1553. {
  1554. va_list args;
  1555. va_start (args, fmt);
  1556. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1557. va_end(args);
  1558. printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
  1559. sb->s_id, function, error_buf);
  1560. }
  1561. /*
  1562. * udf_put_super
  1563. *
  1564. * PURPOSE
  1565. * Prepare for destruction of the superblock.
  1566. *
  1567. * DESCRIPTION
  1568. * Called before the filesystem is unmounted.
  1569. *
  1570. * HISTORY
  1571. * July 1, 1997 - Andrew E. Mileski
  1572. * Written, tested, and released.
  1573. */
  1574. static void
  1575. udf_put_super(struct super_block *sb)
  1576. {
  1577. int i;
  1578. if (UDF_SB_VAT(sb))
  1579. iput(UDF_SB_VAT(sb));
  1580. if (UDF_SB_NUMPARTS(sb))
  1581. {
  1582. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
  1583. iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
  1584. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
  1585. iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
  1586. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
  1587. UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb),s_uspace);
  1588. if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
  1589. UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb),s_fspace);
  1590. if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15)
  1591. {
  1592. for (i=0; i<4; i++)
  1593. brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
  1594. }
  1595. }
  1596. #ifdef CONFIG_UDF_NLS
  1597. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1598. unload_nls(UDF_SB(sb)->s_nls_map);
  1599. #endif
  1600. if (!(sb->s_flags & MS_RDONLY))
  1601. udf_close_lvid(sb);
  1602. brelse(UDF_SB_LVIDBH(sb));
  1603. UDF_SB_FREE(sb);
  1604. kfree(sb->s_fs_info);
  1605. sb->s_fs_info = NULL;
  1606. }
  1607. /*
  1608. * udf_stat_fs
  1609. *
  1610. * PURPOSE
  1611. * Return info about the filesystem.
  1612. *
  1613. * DESCRIPTION
  1614. * Called by sys_statfs()
  1615. *
  1616. * HISTORY
  1617. * July 1, 1997 - Andrew E. Mileski
  1618. * Written, tested, and released.
  1619. */
  1620. static int
  1621. udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1622. {
  1623. struct super_block *sb = dentry->d_sb;
  1624. buf->f_type = UDF_SUPER_MAGIC;
  1625. buf->f_bsize = sb->s_blocksize;
  1626. buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
  1627. buf->f_bfree = udf_count_free(sb);
  1628. buf->f_bavail = buf->f_bfree;
  1629. buf->f_files = (UDF_SB_LVIDBH(sb) ?
  1630. (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
  1631. le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + buf->f_bfree;
  1632. buf->f_ffree = buf->f_bfree;
  1633. /* __kernel_fsid_t f_fsid */
  1634. buf->f_namelen = UDF_NAME_LEN-2;
  1635. return 0;
  1636. }
  1637. static unsigned char udf_bitmap_lookup[16] = {
  1638. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  1639. };
  1640. static unsigned int
  1641. udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
  1642. {
  1643. struct buffer_head *bh = NULL;
  1644. unsigned int accum = 0;
  1645. int index;
  1646. int block = 0, newblock;
  1647. kernel_lb_addr loc;
  1648. uint32_t bytes;
  1649. uint8_t value;
  1650. uint8_t *ptr;
  1651. uint16_t ident;
  1652. struct spaceBitmapDesc *bm;
  1653. lock_kernel();
  1654. loc.logicalBlockNum = bitmap->s_extPosition;
  1655. loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
  1656. bh = udf_read_ptagged(sb, loc, 0, &ident);
  1657. if (!bh)
  1658. {
  1659. printk(KERN_ERR "udf: udf_count_free failed\n");
  1660. goto out;
  1661. }
  1662. else if (ident != TAG_IDENT_SBD)
  1663. {
  1664. brelse(bh);
  1665. printk(KERN_ERR "udf: udf_count_free failed\n");
  1666. goto out;
  1667. }
  1668. bm = (struct spaceBitmapDesc *)bh->b_data;
  1669. bytes = le32_to_cpu(bm->numOfBytes);
  1670. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1671. ptr = (uint8_t *)bh->b_data;
  1672. while ( bytes > 0 )
  1673. {
  1674. while ((bytes > 0) && (index < sb->s_blocksize))
  1675. {
  1676. value = ptr[index];
  1677. accum += udf_bitmap_lookup[ value & 0x0f ];
  1678. accum += udf_bitmap_lookup[ value >> 4 ];
  1679. index++;
  1680. bytes--;
  1681. }
  1682. if ( bytes )
  1683. {
  1684. brelse(bh);
  1685. newblock = udf_get_lb_pblock(sb, loc, ++block);
  1686. bh = udf_tread(sb, newblock);
  1687. if (!bh)
  1688. {
  1689. udf_debug("read failed\n");
  1690. goto out;
  1691. }
  1692. index = 0;
  1693. ptr = (uint8_t *)bh->b_data;
  1694. }
  1695. }
  1696. brelse(bh);
  1697. out:
  1698. unlock_kernel();
  1699. return accum;
  1700. }
  1701. static unsigned int
  1702. udf_count_free_table(struct super_block *sb, struct inode * table)
  1703. {
  1704. unsigned int accum = 0;
  1705. uint32_t elen;
  1706. kernel_lb_addr eloc;
  1707. int8_t etype;
  1708. struct extent_position epos;
  1709. lock_kernel();
  1710. epos.block = UDF_I_LOCATION(table);
  1711. epos.offset = sizeof(struct unallocSpaceEntry);
  1712. epos.bh = NULL;
  1713. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  1714. accum += (elen >> table->i_sb->s_blocksize_bits);
  1715. brelse(epos.bh);
  1716. unlock_kernel();
  1717. return accum;
  1718. }
  1719. static unsigned int
  1720. udf_count_free(struct super_block *sb)
  1721. {
  1722. unsigned int accum = 0;
  1723. if (UDF_SB_LVIDBH(sb))
  1724. {
  1725. if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb))
  1726. {
  1727. accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]);
  1728. if (accum == 0xFFFFFFFF)
  1729. accum = 0;
  1730. }
  1731. }
  1732. if (accum)
  1733. return accum;
  1734. if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
  1735. {
  1736. accum += udf_count_free_bitmap(sb,
  1737. UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap);
  1738. }
  1739. if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
  1740. {
  1741. accum += udf_count_free_bitmap(sb,
  1742. UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap);
  1743. }
  1744. if (accum)
  1745. return accum;
  1746. if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
  1747. {
  1748. accum += udf_count_free_table(sb,
  1749. UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
  1750. }
  1751. if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
  1752. {
  1753. accum += udf_count_free_table(sb,
  1754. UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
  1755. }
  1756. return accum;
  1757. }