super.c 59 KB

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