super.c 51 KB

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