super.c 54 KB

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