super.c 54 KB

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