super.c 53 KB

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