super.c 58 KB

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