super.c 55 KB

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