super.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * super.c
  4. *
  5. * PURPOSE
  6. * Super block routines for the OSTA-UDF(tm) filesystem.
  7. *
  8. * DESCRIPTION
  9. * OSTA-UDF(tm) = Optical Storage Technology Association
  10. * Universal Disk Format.
  11. *
  12. * This code is based on version 2.00 of the UDF specification,
  13. * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
  14. * http://www.osta.org/
  15. * https://www.ecma.ch/
  16. * https://www.iso.org/
  17. *
  18. * COPYRIGHT
  19. * (C) 1998 Dave Boynton
  20. * (C) 1998-2004 Ben Fennema
  21. * (C) 2000 Stelias Computing Inc
  22. *
  23. * HISTORY
  24. *
  25. * 09/24/98 dgb changed to allow compiling outside of kernel, and
  26. * added some debugging.
  27. * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
  28. * 10/16/98 attempting some multi-session support
  29. * 10/17/98 added freespace count for "df"
  30. * 11/11/98 gr added novrs option
  31. * 11/26/98 dgb added fileset,anchor mount options
  32. * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
  33. * vol descs. rewrote option handling based on isofs
  34. * 12/20/98 find the free space bitmap (if it exists)
  35. */
  36. #include "udfdecl.h"
  37. #include <linux/blkdev.h>
  38. #include <linux/slab.h>
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/stat.h>
  42. #include <linux/cdrom.h>
  43. #include <linux/nls.h>
  44. #include <linux/vfs.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/errno.h>
  47. #include <linux/seq_file.h>
  48. #include <linux/bitmap.h>
  49. #include <linux/crc-itu-t.h>
  50. #include <linux/log2.h>
  51. #include <asm/byteorder.h>
  52. #include <linux/iversion.h>
  53. #include <linux/fs_context.h>
  54. #include <linux/fs_parser.h>
  55. #include "udf_sb.h"
  56. #include "udf_i.h"
  57. #include <linux/init.h>
  58. #include <linux/uaccess.h>
  59. enum {
  60. VDS_POS_PRIMARY_VOL_DESC,
  61. VDS_POS_UNALLOC_SPACE_DESC,
  62. VDS_POS_LOGICAL_VOL_DESC,
  63. VDS_POS_IMP_USE_VOL_DESC,
  64. VDS_POS_LENGTH
  65. };
  66. #define VSD_FIRST_SECTOR_OFFSET 32768
  67. #define VSD_MAX_SECTOR_OFFSET 0x800000
  68. /*
  69. * Maximum number of Terminating Descriptor / Logical Volume Integrity
  70. * Descriptor redirections. The chosen numbers are arbitrary - just that we
  71. * hopefully don't limit any real use of rewritten inode on write-once media
  72. * but avoid looping for too long on corrupted media.
  73. */
  74. #define UDF_MAX_TD_NESTING 64
  75. #define UDF_MAX_LVID_NESTING 1000
  76. enum { UDF_MAX_LINKS = 0xffff };
  77. /*
  78. * We limit filesize to 4TB. This is arbitrary as the on-disk format supports
  79. * more but because the file space is described by a linked list of extents,
  80. * each of which can have at most 1GB, the creation and handling of extents
  81. * gets unusably slow beyond certain point...
  82. */
  83. #define UDF_MAX_FILESIZE (1ULL << 42)
  84. /* These are the "meat" - everything else is stuffing */
  85. static int udf_fill_super(struct super_block *sb, struct fs_context *fc);
  86. static void udf_put_super(struct super_block *);
  87. static int udf_sync_fs(struct super_block *, int);
  88. static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
  89. static void udf_open_lvid(struct super_block *);
  90. static void udf_close_lvid(struct super_block *);
  91. static unsigned int udf_count_free(struct super_block *);
  92. static int udf_statfs(struct dentry *, struct kstatfs *);
  93. static int udf_show_options(struct seq_file *, struct dentry *);
  94. static int udf_init_fs_context(struct fs_context *fc);
  95. static int udf_parse_param(struct fs_context *fc, struct fs_parameter *param);
  96. static int udf_reconfigure(struct fs_context *fc);
  97. static void udf_free_fc(struct fs_context *fc);
  98. static const struct fs_parameter_spec udf_param_spec[];
  99. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
  100. {
  101. struct logicalVolIntegrityDesc *lvid;
  102. unsigned int partnum;
  103. unsigned int offset;
  104. if (!UDF_SB(sb)->s_lvid_bh)
  105. return NULL;
  106. lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
  107. partnum = le32_to_cpu(lvid->numOfPartitions);
  108. /* The offset is to skip freeSpaceTable and sizeTable arrays */
  109. offset = partnum * 2 * sizeof(uint32_t);
  110. return (struct logicalVolIntegrityDescImpUse *)
  111. (((uint8_t *)(lvid + 1)) + offset);
  112. }
  113. /* UDF filesystem type */
  114. static int udf_get_tree(struct fs_context *fc)
  115. {
  116. return get_tree_bdev(fc, udf_fill_super);
  117. }
  118. static const struct fs_context_operations udf_context_ops = {
  119. .parse_param = udf_parse_param,
  120. .get_tree = udf_get_tree,
  121. .reconfigure = udf_reconfigure,
  122. .free = udf_free_fc,
  123. };
  124. static struct file_system_type udf_fstype = {
  125. .owner = THIS_MODULE,
  126. .name = "udf",
  127. .kill_sb = kill_block_super,
  128. .fs_flags = FS_REQUIRES_DEV,
  129. .init_fs_context = udf_init_fs_context,
  130. .parameters = udf_param_spec,
  131. };
  132. MODULE_ALIAS_FS("udf");
  133. static struct kmem_cache *udf_inode_cachep;
  134. static struct inode *udf_alloc_inode(struct super_block *sb)
  135. {
  136. struct udf_inode_info *ei;
  137. ei = alloc_inode_sb(sb, udf_inode_cachep, GFP_KERNEL);
  138. if (!ei)
  139. return NULL;
  140. ei->i_unique = 0;
  141. ei->i_lenExtents = 0;
  142. ei->i_lenStreams = 0;
  143. ei->i_next_alloc_block = 0;
  144. ei->i_next_alloc_goal = 0;
  145. ei->i_strat4096 = 0;
  146. ei->i_streamdir = 0;
  147. ei->i_hidden = 0;
  148. init_rwsem(&ei->i_data_sem);
  149. ei->cached_extent.lstart = -1;
  150. spin_lock_init(&ei->i_extent_cache_lock);
  151. inode_set_iversion(&ei->vfs_inode, 1);
  152. return &ei->vfs_inode;
  153. }
  154. static void udf_free_in_core_inode(struct inode *inode)
  155. {
  156. kmem_cache_free(udf_inode_cachep, UDF_I(inode));
  157. }
  158. static void init_once(void *foo)
  159. {
  160. struct udf_inode_info *ei = foo;
  161. ei->i_data = NULL;
  162. inode_init_once(&ei->vfs_inode);
  163. }
  164. static int __init init_inodecache(void)
  165. {
  166. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  167. sizeof(struct udf_inode_info),
  168. 0, (SLAB_RECLAIM_ACCOUNT |
  169. SLAB_ACCOUNT),
  170. init_once);
  171. if (!udf_inode_cachep)
  172. return -ENOMEM;
  173. return 0;
  174. }
  175. static void destroy_inodecache(void)
  176. {
  177. /*
  178. * Make sure all delayed rcu free inodes are flushed before we
  179. * destroy cache.
  180. */
  181. rcu_barrier();
  182. kmem_cache_destroy(udf_inode_cachep);
  183. }
  184. /* Superblock operations */
  185. static const struct super_operations udf_sb_ops = {
  186. .alloc_inode = udf_alloc_inode,
  187. .free_inode = udf_free_in_core_inode,
  188. .write_inode = udf_write_inode,
  189. .evict_inode = udf_evict_inode,
  190. .put_super = udf_put_super,
  191. .sync_fs = udf_sync_fs,
  192. .statfs = udf_statfs,
  193. .show_options = udf_show_options,
  194. };
  195. struct udf_options {
  196. unsigned int blocksize;
  197. unsigned int session;
  198. unsigned int lastblock;
  199. unsigned int anchor;
  200. unsigned int flags;
  201. umode_t umask;
  202. kgid_t gid;
  203. kuid_t uid;
  204. umode_t fmode;
  205. umode_t dmode;
  206. struct nls_table *nls_map;
  207. };
  208. /*
  209. * UDF has historically preserved prior mount options across
  210. * a remount, so copy those here if remounting, otherwise set
  211. * initial mount defaults.
  212. */
  213. static void udf_init_options(struct fs_context *fc, struct udf_options *uopt)
  214. {
  215. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  216. struct super_block *sb = fc->root->d_sb;
  217. struct udf_sb_info *sbi = UDF_SB(sb);
  218. uopt->flags = sbi->s_flags;
  219. uopt->uid = sbi->s_uid;
  220. uopt->gid = sbi->s_gid;
  221. uopt->umask = sbi->s_umask;
  222. uopt->fmode = sbi->s_fmode;
  223. uopt->dmode = sbi->s_dmode;
  224. uopt->nls_map = NULL;
  225. } else {
  226. uopt->flags = (1 << UDF_FLAG_USE_AD_IN_ICB) |
  227. (1 << UDF_FLAG_STRICT);
  228. /*
  229. * By default we'll use overflow[ug]id when UDF
  230. * inode [ug]id == -1
  231. */
  232. uopt->uid = make_kuid(current_user_ns(), overflowuid);
  233. uopt->gid = make_kgid(current_user_ns(), overflowgid);
  234. uopt->umask = 0;
  235. uopt->fmode = UDF_INVALID_MODE;
  236. uopt->dmode = UDF_INVALID_MODE;
  237. uopt->nls_map = NULL;
  238. uopt->session = 0xFFFFFFFF;
  239. }
  240. }
  241. static int udf_init_fs_context(struct fs_context *fc)
  242. {
  243. struct udf_options *uopt;
  244. uopt = kzalloc_obj(*uopt);
  245. if (!uopt)
  246. return -ENOMEM;
  247. udf_init_options(fc, uopt);
  248. fc->fs_private = uopt;
  249. fc->ops = &udf_context_ops;
  250. return 0;
  251. }
  252. static void udf_free_fc(struct fs_context *fc)
  253. {
  254. struct udf_options *uopt = fc->fs_private;
  255. unload_nls(uopt->nls_map);
  256. kfree(fc->fs_private);
  257. }
  258. static int __init init_udf_fs(void)
  259. {
  260. int err;
  261. err = init_inodecache();
  262. if (err)
  263. goto out1;
  264. err = register_filesystem(&udf_fstype);
  265. if (err)
  266. goto out;
  267. return 0;
  268. out:
  269. destroy_inodecache();
  270. out1:
  271. return err;
  272. }
  273. static void __exit exit_udf_fs(void)
  274. {
  275. unregister_filesystem(&udf_fstype);
  276. destroy_inodecache();
  277. }
  278. static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
  279. {
  280. struct udf_sb_info *sbi = UDF_SB(sb);
  281. sbi->s_partmaps = kzalloc_objs(*sbi->s_partmaps, count);
  282. if (!sbi->s_partmaps) {
  283. sbi->s_partitions = 0;
  284. return -ENOMEM;
  285. }
  286. sbi->s_partitions = count;
  287. return 0;
  288. }
  289. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  290. {
  291. int i;
  292. int nr_groups = bitmap->s_nr_groups;
  293. for (i = 0; i < nr_groups; i++)
  294. if (!IS_ERR_OR_NULL(bitmap->s_block_bitmap[i]))
  295. brelse(bitmap->s_block_bitmap[i]);
  296. kvfree(bitmap);
  297. }
  298. static void udf_free_partition(struct udf_part_map *map)
  299. {
  300. int i;
  301. struct udf_meta_data *mdata;
  302. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  303. iput(map->s_uspace.s_table);
  304. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  305. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  306. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  307. for (i = 0; i < 4; i++)
  308. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  309. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  310. mdata = &map->s_type_specific.s_metadata;
  311. iput(mdata->s_metadata_fe);
  312. mdata->s_metadata_fe = NULL;
  313. iput(mdata->s_mirror_fe);
  314. mdata->s_mirror_fe = NULL;
  315. iput(mdata->s_bitmap_fe);
  316. mdata->s_bitmap_fe = NULL;
  317. }
  318. }
  319. static void udf_sb_free_partitions(struct super_block *sb)
  320. {
  321. struct udf_sb_info *sbi = UDF_SB(sb);
  322. int i;
  323. if (!sbi->s_partmaps)
  324. return;
  325. for (i = 0; i < sbi->s_partitions; i++)
  326. udf_free_partition(&sbi->s_partmaps[i]);
  327. kfree(sbi->s_partmaps);
  328. sbi->s_partmaps = NULL;
  329. }
  330. static int udf_show_options(struct seq_file *seq, struct dentry *root)
  331. {
  332. struct super_block *sb = root->d_sb;
  333. struct udf_sb_info *sbi = UDF_SB(sb);
  334. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
  335. seq_puts(seq, ",nostrict");
  336. if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
  337. seq_printf(seq, ",bs=%lu", sb->s_blocksize);
  338. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  339. seq_puts(seq, ",unhide");
  340. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  341. seq_puts(seq, ",undelete");
  342. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
  343. seq_puts(seq, ",noadinicb");
  344. if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
  345. seq_puts(seq, ",shortad");
  346. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
  347. seq_puts(seq, ",uid=forget");
  348. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
  349. seq_puts(seq, ",gid=forget");
  350. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
  351. seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
  352. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
  353. seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
  354. if (sbi->s_umask != 0)
  355. seq_printf(seq, ",umask=%ho", sbi->s_umask);
  356. if (sbi->s_fmode != UDF_INVALID_MODE)
  357. seq_printf(seq, ",mode=%ho", sbi->s_fmode);
  358. if (sbi->s_dmode != UDF_INVALID_MODE)
  359. seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
  360. if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
  361. seq_printf(seq, ",session=%d", sbi->s_session);
  362. if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
  363. seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
  364. if (sbi->s_anchor != 0)
  365. seq_printf(seq, ",anchor=%u", sbi->s_anchor);
  366. if (sbi->s_nls_map)
  367. seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
  368. else
  369. seq_puts(seq, ",iocharset=utf8");
  370. return 0;
  371. }
  372. /*
  373. * udf_parse_param
  374. *
  375. * PURPOSE
  376. * Parse mount options.
  377. *
  378. * DESCRIPTION
  379. * The following mount options are supported:
  380. *
  381. * gid= Set the default group.
  382. * umask= Set the default umask.
  383. * mode= Set the default file permissions.
  384. * dmode= Set the default directory permissions.
  385. * uid= Set the default user.
  386. * bs= Set the block size.
  387. * unhide Show otherwise hidden files.
  388. * undelete Show deleted files in lists.
  389. * adinicb Embed data in the inode (default)
  390. * noadinicb Don't embed data in the inode
  391. * shortad Use short ad's
  392. * longad Use long ad's (default)
  393. * nostrict Unset strict conformance
  394. * iocharset= Set the NLS character set
  395. *
  396. * The remaining are for debugging and disaster recovery:
  397. *
  398. * novrs Skip volume sequence recognition
  399. *
  400. * The following expect a offset from 0.
  401. *
  402. * session= Set the CDROM session (default= last session)
  403. * anchor= Override standard anchor location. (default= 256)
  404. * volume= Override the VolumeDesc location. (unused)
  405. * partition= Override the PartitionDesc location. (unused)
  406. * lastblock= Set the last block of the filesystem/
  407. *
  408. * The following expect a offset from the partition root.
  409. *
  410. * fileset= Override the fileset block location. (unused)
  411. * rootdir= Override the root directory location. (unused)
  412. * WARNING: overriding the rootdir to a non-directory may
  413. * yield highly unpredictable results.
  414. *
  415. * PRE-CONDITIONS
  416. * fc fs_context with pointer to mount options variable.
  417. * param Pointer to fs_parameter being parsed.
  418. *
  419. * POST-CONDITIONS
  420. * <return> 0 Mount options parsed okay.
  421. * <return> errno Error parsing mount options.
  422. *
  423. * HISTORY
  424. * July 1, 1997 - Andrew E. Mileski
  425. * Written, tested, and released.
  426. */
  427. enum {
  428. Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
  429. Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
  430. Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
  431. Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
  432. Opt_rootdir, Opt_utf8, Opt_iocharset, Opt_err, Opt_fmode, Opt_dmode
  433. };
  434. static const struct fs_parameter_spec udf_param_spec[] = {
  435. fsparam_flag ("novrs", Opt_novrs),
  436. fsparam_flag ("nostrict", Opt_nostrict),
  437. fsparam_u32 ("bs", Opt_bs),
  438. fsparam_flag ("unhide", Opt_unhide),
  439. fsparam_flag ("undelete", Opt_undelete),
  440. fsparam_flag_no ("adinicb", Opt_adinicb),
  441. fsparam_flag ("shortad", Opt_shortad),
  442. fsparam_flag ("longad", Opt_longad),
  443. fsparam_string ("gid", Opt_gid),
  444. fsparam_string ("uid", Opt_uid),
  445. fsparam_u32 ("umask", Opt_umask),
  446. fsparam_u32 ("session", Opt_session),
  447. fsparam_u32 ("lastblock", Opt_lastblock),
  448. fsparam_u32 ("anchor", Opt_anchor),
  449. fsparam_u32 ("volume", Opt_volume),
  450. fsparam_u32 ("partition", Opt_partition),
  451. fsparam_u32 ("fileset", Opt_fileset),
  452. fsparam_u32 ("rootdir", Opt_rootdir),
  453. fsparam_flag ("utf8", Opt_utf8),
  454. fsparam_string ("iocharset", Opt_iocharset),
  455. fsparam_u32 ("mode", Opt_fmode),
  456. fsparam_u32 ("dmode", Opt_dmode),
  457. {}
  458. };
  459. static int udf_parse_param(struct fs_context *fc, struct fs_parameter *param)
  460. {
  461. unsigned int uv;
  462. unsigned int n;
  463. struct udf_options *uopt = fc->fs_private;
  464. struct fs_parse_result result;
  465. int token;
  466. bool remount = (fc->purpose & FS_CONTEXT_FOR_RECONFIGURE);
  467. token = fs_parse(fc, udf_param_spec, param, &result);
  468. if (token < 0)
  469. return token;
  470. switch (token) {
  471. case Opt_novrs:
  472. uopt->flags |= (1 << UDF_FLAG_NOVRS);
  473. break;
  474. case Opt_bs:
  475. n = result.uint_32;
  476. if (n != 512 && n != 1024 && n != 2048 && n != 4096)
  477. return -EINVAL;
  478. uopt->blocksize = n;
  479. uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
  480. break;
  481. case Opt_unhide:
  482. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  483. break;
  484. case Opt_undelete:
  485. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  486. break;
  487. case Opt_adinicb:
  488. if (result.negated)
  489. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  490. else
  491. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  492. break;
  493. case Opt_shortad:
  494. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  495. break;
  496. case Opt_longad:
  497. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  498. break;
  499. case Opt_gid:
  500. if (kstrtoint(param->string, 10, &uv) == 0) {
  501. kgid_t gid = make_kgid(current_user_ns(), uv);
  502. if (!gid_valid(gid))
  503. return -EINVAL;
  504. uopt->gid = gid;
  505. uopt->flags |= (1 << UDF_FLAG_GID_SET);
  506. } else if (!strcmp(param->string, "forget")) {
  507. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  508. } else if (!strcmp(param->string, "ignore")) {
  509. /* this option is superseded by gid=<number> */
  510. ;
  511. } else {
  512. return -EINVAL;
  513. }
  514. break;
  515. case Opt_uid:
  516. if (kstrtoint(param->string, 10, &uv) == 0) {
  517. kuid_t uid = make_kuid(current_user_ns(), uv);
  518. if (!uid_valid(uid))
  519. return -EINVAL;
  520. uopt->uid = uid;
  521. uopt->flags |= (1 << UDF_FLAG_UID_SET);
  522. } else if (!strcmp(param->string, "forget")) {
  523. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  524. } else if (!strcmp(param->string, "ignore")) {
  525. /* this option is superseded by uid=<number> */
  526. ;
  527. } else {
  528. return -EINVAL;
  529. }
  530. break;
  531. case Opt_umask:
  532. uopt->umask = result.uint_32;
  533. break;
  534. case Opt_nostrict:
  535. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  536. break;
  537. case Opt_session:
  538. uopt->session = result.uint_32;
  539. if (!remount)
  540. uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
  541. break;
  542. case Opt_lastblock:
  543. uopt->lastblock = result.uint_32;
  544. if (!remount)
  545. uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
  546. break;
  547. case Opt_anchor:
  548. uopt->anchor = result.uint_32;
  549. break;
  550. case Opt_volume:
  551. case Opt_partition:
  552. case Opt_fileset:
  553. case Opt_rootdir:
  554. /* Ignored (never implemented properly) */
  555. break;
  556. case Opt_utf8:
  557. if (!remount) {
  558. unload_nls(uopt->nls_map);
  559. uopt->nls_map = NULL;
  560. }
  561. break;
  562. case Opt_iocharset:
  563. if (!remount) {
  564. unload_nls(uopt->nls_map);
  565. uopt->nls_map = NULL;
  566. }
  567. /* When nls_map is not loaded then UTF-8 is used */
  568. if (!remount && strcmp(param->string, "utf8") != 0) {
  569. uopt->nls_map = load_nls(param->string);
  570. if (!uopt->nls_map) {
  571. errorf(fc, "iocharset %s not found",
  572. param->string);
  573. return -EINVAL;
  574. }
  575. }
  576. break;
  577. case Opt_fmode:
  578. uopt->fmode = result.uint_32 & 0777;
  579. break;
  580. case Opt_dmode:
  581. uopt->dmode = result.uint_32 & 0777;
  582. break;
  583. default:
  584. return -EINVAL;
  585. }
  586. return 0;
  587. }
  588. static int udf_reconfigure(struct fs_context *fc)
  589. {
  590. struct udf_options *uopt = fc->fs_private;
  591. struct super_block *sb = fc->root->d_sb;
  592. struct udf_sb_info *sbi = UDF_SB(sb);
  593. int readonly = fc->sb_flags & SB_RDONLY;
  594. int error = 0;
  595. if (!readonly && UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
  596. return -EACCES;
  597. sync_filesystem(sb);
  598. write_lock(&sbi->s_cred_lock);
  599. sbi->s_flags = uopt->flags;
  600. sbi->s_uid = uopt->uid;
  601. sbi->s_gid = uopt->gid;
  602. sbi->s_umask = uopt->umask;
  603. sbi->s_fmode = uopt->fmode;
  604. sbi->s_dmode = uopt->dmode;
  605. write_unlock(&sbi->s_cred_lock);
  606. if (readonly == sb_rdonly(sb))
  607. goto out_unlock;
  608. if (readonly)
  609. udf_close_lvid(sb);
  610. else
  611. udf_open_lvid(sb);
  612. out_unlock:
  613. return error;
  614. }
  615. /*
  616. * Check VSD descriptor. Returns -1 in case we are at the end of volume
  617. * recognition area, 0 if the descriptor is valid but non-interesting, 1 if
  618. * we found one of NSR descriptors we are looking for.
  619. */
  620. static int identify_vsd(const struct volStructDesc *vsd)
  621. {
  622. int ret = 0;
  623. if (!memcmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN)) {
  624. switch (vsd->structType) {
  625. case 0:
  626. udf_debug("ISO9660 Boot Record found\n");
  627. break;
  628. case 1:
  629. udf_debug("ISO9660 Primary Volume Descriptor found\n");
  630. break;
  631. case 2:
  632. udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
  633. break;
  634. case 3:
  635. udf_debug("ISO9660 Volume Partition Descriptor found\n");
  636. break;
  637. case 255:
  638. udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
  639. break;
  640. default:
  641. udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
  642. break;
  643. }
  644. } else if (!memcmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN))
  645. ; /* ret = 0 */
  646. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN))
  647. ret = 1;
  648. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN))
  649. ret = 1;
  650. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_BOOT2, VSD_STD_ID_LEN))
  651. ; /* ret = 0 */
  652. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_CDW02, VSD_STD_ID_LEN))
  653. ; /* ret = 0 */
  654. else {
  655. /* TEA01 or invalid id : end of volume recognition area */
  656. ret = -1;
  657. }
  658. return ret;
  659. }
  660. /*
  661. * Check Volume Structure Descriptors (ECMA 167 2/9.1)
  662. * We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1)
  663. * @return 1 if NSR02 or NSR03 found,
  664. * -1 if first sector read error, 0 otherwise
  665. */
  666. static int udf_check_vsd(struct super_block *sb)
  667. {
  668. struct volStructDesc *vsd = NULL;
  669. loff_t sector = VSD_FIRST_SECTOR_OFFSET;
  670. int sectorsize;
  671. struct buffer_head *bh = NULL;
  672. int nsr = 0;
  673. struct udf_sb_info *sbi;
  674. loff_t session_offset;
  675. sbi = UDF_SB(sb);
  676. if (sb->s_blocksize < sizeof(struct volStructDesc))
  677. sectorsize = sizeof(struct volStructDesc);
  678. else
  679. sectorsize = sb->s_blocksize;
  680. session_offset = (loff_t)sbi->s_session << sb->s_blocksize_bits;
  681. sector += session_offset;
  682. udf_debug("Starting at sector %u (%lu byte sectors)\n",
  683. (unsigned int)(sector >> sb->s_blocksize_bits),
  684. sb->s_blocksize);
  685. /* Process the sequence (if applicable). The hard limit on the sector
  686. * offset is arbitrary, hopefully large enough so that all valid UDF
  687. * filesystems will be recognised. There is no mention of an upper
  688. * bound to the size of the volume recognition area in the standard.
  689. * The limit will prevent the code to read all the sectors of a
  690. * specially crafted image (like a bluray disc full of CD001 sectors),
  691. * potentially causing minutes or even hours of uninterruptible I/O
  692. * activity. This actually happened with uninitialised SSD partitions
  693. * (all 0xFF) before the check for the limit and all valid IDs were
  694. * added */
  695. for (; !nsr && sector < VSD_MAX_SECTOR_OFFSET; sector += sectorsize) {
  696. /* Read a block */
  697. bh = sb_bread(sb, sector >> sb->s_blocksize_bits);
  698. if (!bh)
  699. break;
  700. vsd = (struct volStructDesc *)(bh->b_data +
  701. (sector & (sb->s_blocksize - 1)));
  702. nsr = identify_vsd(vsd);
  703. /* Found NSR or end? */
  704. if (nsr) {
  705. brelse(bh);
  706. break;
  707. }
  708. /*
  709. * Special handling for improperly formatted VRS (e.g., Win10)
  710. * where components are separated by 2048 bytes even though
  711. * sectors are 4K
  712. */
  713. if (sb->s_blocksize == 4096) {
  714. nsr = identify_vsd(vsd + 1);
  715. /* Ignore unknown IDs... */
  716. if (nsr < 0)
  717. nsr = 0;
  718. }
  719. brelse(bh);
  720. }
  721. if (nsr > 0)
  722. return 1;
  723. else if (!bh && sector - session_offset == VSD_FIRST_SECTOR_OFFSET)
  724. return -1;
  725. else
  726. return 0;
  727. }
  728. static int udf_verify_domain_identifier(struct super_block *sb,
  729. struct regid *ident, char *dname)
  730. {
  731. struct domainIdentSuffix *suffix;
  732. if (memcmp(ident->ident, UDF_ID_COMPLIANT, strlen(UDF_ID_COMPLIANT))) {
  733. udf_warn(sb, "Not OSTA UDF compliant %s descriptor.\n", dname);
  734. goto force_ro;
  735. }
  736. if (ident->flags & ENTITYID_FLAGS_DIRTY) {
  737. udf_warn(sb, "Possibly not OSTA UDF compliant %s descriptor.\n",
  738. dname);
  739. goto force_ro;
  740. }
  741. suffix = (struct domainIdentSuffix *)ident->identSuffix;
  742. if ((suffix->domainFlags & DOMAIN_FLAGS_HARD_WRITE_PROTECT) ||
  743. (suffix->domainFlags & DOMAIN_FLAGS_SOFT_WRITE_PROTECT)) {
  744. if (!sb_rdonly(sb)) {
  745. udf_warn(sb, "Descriptor for %s marked write protected."
  746. " Forcing read only mount.\n", dname);
  747. }
  748. goto force_ro;
  749. }
  750. return 0;
  751. force_ro:
  752. if (!sb_rdonly(sb))
  753. return -EACCES;
  754. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  755. return 0;
  756. }
  757. static int udf_load_fileset(struct super_block *sb, struct fileSetDesc *fset,
  758. struct kernel_lb_addr *root)
  759. {
  760. int ret;
  761. ret = udf_verify_domain_identifier(sb, &fset->domainIdent, "file set");
  762. if (ret < 0)
  763. return ret;
  764. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  765. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  766. udf_debug("Rootdir at block=%u, partition=%u\n",
  767. root->logicalBlockNum, root->partitionReferenceNum);
  768. return 0;
  769. }
  770. static int udf_find_fileset(struct super_block *sb,
  771. struct kernel_lb_addr *fileset,
  772. struct kernel_lb_addr *root)
  773. {
  774. struct buffer_head *bh;
  775. uint16_t ident;
  776. int ret;
  777. if (fileset->logicalBlockNum == 0xFFFFFFFF &&
  778. fileset->partitionReferenceNum == 0xFFFF)
  779. return -EINVAL;
  780. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  781. if (!bh)
  782. return -EIO;
  783. if (ident != TAG_IDENT_FSD) {
  784. brelse(bh);
  785. return -EINVAL;
  786. }
  787. udf_debug("Fileset at block=%u, partition=%u\n",
  788. fileset->logicalBlockNum, fileset->partitionReferenceNum);
  789. UDF_SB(sb)->s_partition = fileset->partitionReferenceNum;
  790. ret = udf_load_fileset(sb, (struct fileSetDesc *)bh->b_data, root);
  791. brelse(bh);
  792. return ret;
  793. }
  794. /*
  795. * Load primary Volume Descriptor Sequence
  796. *
  797. * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
  798. * should be tried.
  799. */
  800. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  801. {
  802. struct primaryVolDesc *pvoldesc;
  803. uint8_t *outstr;
  804. struct buffer_head *bh;
  805. uint16_t ident;
  806. int ret;
  807. struct timestamp *ts;
  808. outstr = kzalloc(128, GFP_KERNEL);
  809. if (!outstr)
  810. return -ENOMEM;
  811. bh = udf_read_tagged(sb, block, block, &ident);
  812. if (!bh) {
  813. ret = -EAGAIN;
  814. goto out2;
  815. }
  816. if (ident != TAG_IDENT_PVD) {
  817. ret = -EIO;
  818. goto out_bh;
  819. }
  820. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  821. udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  822. pvoldesc->recordingDateAndTime);
  823. ts = &pvoldesc->recordingDateAndTime;
  824. udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
  825. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  826. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  827. ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
  828. if (ret < 0) {
  829. strscpy_pad(UDF_SB(sb)->s_volume_ident, "InvalidName");
  830. pr_warn("incorrect volume identification, setting to "
  831. "'InvalidName'\n");
  832. } else {
  833. strscpy_pad(UDF_SB(sb)->s_volume_ident, outstr);
  834. }
  835. udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
  836. ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128);
  837. if (ret < 0) {
  838. ret = 0;
  839. goto out_bh;
  840. }
  841. outstr[ret] = 0;
  842. udf_debug("volSetIdent[] = '%s'\n", outstr);
  843. ret = 0;
  844. out_bh:
  845. brelse(bh);
  846. out2:
  847. kfree(outstr);
  848. return ret;
  849. }
  850. struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
  851. u32 meta_file_loc, u32 partition_ref)
  852. {
  853. struct kernel_lb_addr addr;
  854. struct inode *metadata_fe;
  855. addr.logicalBlockNum = meta_file_loc;
  856. addr.partitionReferenceNum = partition_ref;
  857. metadata_fe = udf_iget_special(sb, &addr);
  858. if (IS_ERR(metadata_fe)) {
  859. udf_warn(sb, "metadata inode efe not found\n");
  860. return metadata_fe;
  861. }
  862. if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
  863. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  864. iput(metadata_fe);
  865. return ERR_PTR(-EIO);
  866. }
  867. return metadata_fe;
  868. }
  869. static int udf_load_metadata_files(struct super_block *sb, int partition,
  870. int type1_index)
  871. {
  872. struct udf_sb_info *sbi = UDF_SB(sb);
  873. struct udf_part_map *map;
  874. struct udf_meta_data *mdata;
  875. struct kernel_lb_addr addr;
  876. struct inode *fe;
  877. map = &sbi->s_partmaps[partition];
  878. mdata = &map->s_type_specific.s_metadata;
  879. mdata->s_phys_partition_ref = type1_index;
  880. /* metadata address */
  881. udf_debug("Metadata file location: block = %u part = %u\n",
  882. mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
  883. fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
  884. mdata->s_phys_partition_ref);
  885. if (IS_ERR(fe)) {
  886. /* mirror file entry */
  887. udf_debug("Mirror metadata file location: block = %u part = %u\n",
  888. mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
  889. fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
  890. mdata->s_phys_partition_ref);
  891. if (IS_ERR(fe)) {
  892. udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
  893. return PTR_ERR(fe);
  894. }
  895. mdata->s_mirror_fe = fe;
  896. } else
  897. mdata->s_metadata_fe = fe;
  898. /*
  899. * bitmap file entry
  900. * Note:
  901. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  902. */
  903. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  904. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  905. addr.partitionReferenceNum = mdata->s_phys_partition_ref;
  906. udf_debug("Bitmap file location: block = %u part = %u\n",
  907. addr.logicalBlockNum, addr.partitionReferenceNum);
  908. fe = udf_iget_special(sb, &addr);
  909. if (IS_ERR(fe)) {
  910. if (sb_rdonly(sb))
  911. udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
  912. else {
  913. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  914. return PTR_ERR(fe);
  915. }
  916. } else
  917. mdata->s_bitmap_fe = fe;
  918. }
  919. udf_debug("udf_load_metadata_files Ok\n");
  920. return 0;
  921. }
  922. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  923. {
  924. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  925. return DIV_ROUND_UP(map->s_partition_len +
  926. (sizeof(struct spaceBitmapDesc) << 3),
  927. sb->s_blocksize * 8);
  928. }
  929. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  930. {
  931. struct udf_bitmap *bitmap;
  932. int nr_groups = udf_compute_nr_groups(sb, index);
  933. bitmap = kvzalloc_flex(*bitmap, s_block_bitmap, nr_groups);
  934. if (!bitmap)
  935. return NULL;
  936. bitmap->s_nr_groups = nr_groups;
  937. return bitmap;
  938. }
  939. static int check_partition_desc(struct super_block *sb,
  940. struct partitionDesc *p,
  941. struct udf_part_map *map)
  942. {
  943. bool umap, utable, fmap, ftable;
  944. struct partitionHeaderDesc *phd;
  945. switch (le32_to_cpu(p->accessType)) {
  946. case PD_ACCESS_TYPE_READ_ONLY:
  947. case PD_ACCESS_TYPE_WRITE_ONCE:
  948. case PD_ACCESS_TYPE_NONE:
  949. goto force_ro;
  950. }
  951. /* No Partition Header Descriptor? */
  952. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  953. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  954. goto force_ro;
  955. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  956. utable = phd->unallocSpaceTable.extLength;
  957. umap = phd->unallocSpaceBitmap.extLength;
  958. ftable = phd->freedSpaceTable.extLength;
  959. fmap = phd->freedSpaceBitmap.extLength;
  960. /* No allocation info? */
  961. if (!utable && !umap && !ftable && !fmap)
  962. goto force_ro;
  963. /* We don't support blocks that require erasing before overwrite */
  964. if (ftable || fmap)
  965. goto force_ro;
  966. /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
  967. if (utable && umap)
  968. goto force_ro;
  969. if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  970. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  971. map->s_partition_type == UDF_METADATA_MAP25)
  972. goto force_ro;
  973. return 0;
  974. force_ro:
  975. if (!sb_rdonly(sb))
  976. return -EACCES;
  977. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  978. return 0;
  979. }
  980. static int udf_fill_partdesc_info(struct super_block *sb,
  981. struct partitionDesc *p, int p_index)
  982. {
  983. struct udf_part_map *map;
  984. struct udf_sb_info *sbi = UDF_SB(sb);
  985. struct partitionHeaderDesc *phd;
  986. u32 sum;
  987. int err;
  988. map = &sbi->s_partmaps[p_index];
  989. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  990. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  991. if (check_add_overflow(map->s_partition_root, map->s_partition_len,
  992. &sum)) {
  993. udf_err(sb, "Partition %d has invalid location %u + %u\n",
  994. p_index, map->s_partition_root, map->s_partition_len);
  995. return -EFSCORRUPTED;
  996. }
  997. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  998. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  999. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  1000. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  1001. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  1002. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  1003. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  1004. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  1005. udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
  1006. p_index, map->s_partition_type,
  1007. map->s_partition_root, map->s_partition_len);
  1008. err = check_partition_desc(sb, p, map);
  1009. if (err)
  1010. return err;
  1011. /*
  1012. * Skip loading allocation info it we cannot ever write to the fs.
  1013. * This is a correctness thing as we may have decided to force ro mount
  1014. * to avoid allocation info we don't support.
  1015. */
  1016. if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
  1017. return 0;
  1018. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  1019. if (phd->unallocSpaceTable.extLength) {
  1020. struct kernel_lb_addr loc = {
  1021. .logicalBlockNum = le32_to_cpu(
  1022. phd->unallocSpaceTable.extPosition),
  1023. .partitionReferenceNum = p_index,
  1024. };
  1025. struct inode *inode;
  1026. inode = udf_iget_special(sb, &loc);
  1027. if (IS_ERR(inode)) {
  1028. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  1029. p_index);
  1030. return PTR_ERR(inode);
  1031. }
  1032. map->s_uspace.s_table = inode;
  1033. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  1034. udf_debug("unallocSpaceTable (part %d) @ %lu\n",
  1035. p_index, map->s_uspace.s_table->i_ino);
  1036. }
  1037. if (phd->unallocSpaceBitmap.extLength) {
  1038. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  1039. if (!bitmap)
  1040. return -ENOMEM;
  1041. map->s_uspace.s_bitmap = bitmap;
  1042. bitmap->s_extPosition = le32_to_cpu(
  1043. phd->unallocSpaceBitmap.extPosition);
  1044. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  1045. /* Check whether math over bitmap won't overflow. */
  1046. if (check_add_overflow(map->s_partition_len,
  1047. sizeof(struct spaceBitmapDesc) << 3,
  1048. &sum)) {
  1049. udf_err(sb, "Partition %d is too long (%u)\n", p_index,
  1050. map->s_partition_len);
  1051. return -EFSCORRUPTED;
  1052. }
  1053. udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
  1054. p_index, bitmap->s_extPosition);
  1055. }
  1056. return 0;
  1057. }
  1058. static void udf_find_vat_block(struct super_block *sb, int p_index,
  1059. int type1_index, sector_t start_block)
  1060. {
  1061. struct udf_sb_info *sbi = UDF_SB(sb);
  1062. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1063. sector_t vat_block;
  1064. struct kernel_lb_addr ino;
  1065. struct inode *inode;
  1066. /*
  1067. * VAT file entry is in the last recorded block. Some broken disks have
  1068. * it a few blocks before so try a bit harder...
  1069. */
  1070. ino.partitionReferenceNum = type1_index;
  1071. for (vat_block = start_block;
  1072. vat_block >= map->s_partition_root &&
  1073. vat_block >= start_block - 3; vat_block--) {
  1074. ino.logicalBlockNum = vat_block - map->s_partition_root;
  1075. inode = udf_iget_special(sb, &ino);
  1076. if (!IS_ERR(inode)) {
  1077. sbi->s_vat_inode = inode;
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  1083. {
  1084. struct udf_sb_info *sbi = UDF_SB(sb);
  1085. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1086. struct buffer_head *bh = NULL;
  1087. struct udf_inode_info *vati;
  1088. struct virtualAllocationTable20 *vat20;
  1089. sector_t blocks = sb_bdev_nr_blocks(sb);
  1090. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  1091. if (!sbi->s_vat_inode &&
  1092. sbi->s_last_block != blocks - 1) {
  1093. pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
  1094. (unsigned long)sbi->s_last_block,
  1095. (unsigned long)blocks - 1);
  1096. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  1097. }
  1098. if (!sbi->s_vat_inode)
  1099. return -EIO;
  1100. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1101. map->s_type_specific.s_virtual.s_start_offset = 0;
  1102. map->s_type_specific.s_virtual.s_num_entries =
  1103. (sbi->s_vat_inode->i_size - 36) >> 2;
  1104. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1105. vati = UDF_I(sbi->s_vat_inode);
  1106. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1107. int err = 0;
  1108. bh = udf_bread(sbi->s_vat_inode, 0, 0, &err);
  1109. if (!bh) {
  1110. if (!err)
  1111. err = -EFSCORRUPTED;
  1112. return err;
  1113. }
  1114. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1115. } else {
  1116. vat20 = (struct virtualAllocationTable20 *)
  1117. vati->i_data;
  1118. }
  1119. map->s_type_specific.s_virtual.s_start_offset =
  1120. le16_to_cpu(vat20->lengthHeader);
  1121. map->s_type_specific.s_virtual.s_num_entries =
  1122. (sbi->s_vat_inode->i_size -
  1123. map->s_type_specific.s_virtual.
  1124. s_start_offset) >> 2;
  1125. brelse(bh);
  1126. }
  1127. return 0;
  1128. }
  1129. /*
  1130. * Load partition descriptor block
  1131. *
  1132. * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
  1133. * sequence.
  1134. */
  1135. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1136. {
  1137. struct buffer_head *bh;
  1138. struct partitionDesc *p;
  1139. struct udf_part_map *map;
  1140. struct udf_sb_info *sbi = UDF_SB(sb);
  1141. int i, type1_idx;
  1142. uint16_t partitionNumber;
  1143. uint16_t ident;
  1144. int ret;
  1145. bh = udf_read_tagged(sb, block, block, &ident);
  1146. if (!bh)
  1147. return -EAGAIN;
  1148. if (ident != TAG_IDENT_PD) {
  1149. ret = 0;
  1150. goto out_bh;
  1151. }
  1152. p = (struct partitionDesc *)bh->b_data;
  1153. partitionNumber = le16_to_cpu(p->partitionNumber);
  1154. /* First scan for TYPE1 and SPARABLE partitions */
  1155. for (i = 0; i < sbi->s_partitions; i++) {
  1156. map = &sbi->s_partmaps[i];
  1157. udf_debug("Searching map: (%u == %u)\n",
  1158. map->s_partition_num, partitionNumber);
  1159. if (map->s_partition_num == partitionNumber &&
  1160. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1161. map->s_partition_type == UDF_SPARABLE_MAP15))
  1162. break;
  1163. }
  1164. if (i >= sbi->s_partitions) {
  1165. udf_debug("Partition (%u) not found in partition map\n",
  1166. partitionNumber);
  1167. ret = 0;
  1168. goto out_bh;
  1169. }
  1170. ret = udf_fill_partdesc_info(sb, p, i);
  1171. if (ret < 0)
  1172. goto out_bh;
  1173. /*
  1174. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1175. * PHYSICAL partitions are already set up
  1176. */
  1177. type1_idx = i;
  1178. map = NULL; /* supress 'maybe used uninitialized' warning */
  1179. for (i = 0; i < sbi->s_partitions; i++) {
  1180. map = &sbi->s_partmaps[i];
  1181. if (map->s_partition_num == partitionNumber &&
  1182. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1183. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1184. map->s_partition_type == UDF_METADATA_MAP25))
  1185. break;
  1186. }
  1187. if (i >= sbi->s_partitions) {
  1188. ret = 0;
  1189. goto out_bh;
  1190. }
  1191. ret = udf_fill_partdesc_info(sb, p, i);
  1192. if (ret < 0)
  1193. goto out_bh;
  1194. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1195. ret = udf_load_metadata_files(sb, i, type1_idx);
  1196. if (ret < 0) {
  1197. udf_err(sb, "error loading MetaData partition map %d\n",
  1198. i);
  1199. goto out_bh;
  1200. }
  1201. } else {
  1202. /*
  1203. * If we have a partition with virtual map, we don't handle
  1204. * writing to it (we overwrite blocks instead of relocating
  1205. * them).
  1206. */
  1207. if (!sb_rdonly(sb)) {
  1208. ret = -EACCES;
  1209. goto out_bh;
  1210. }
  1211. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  1212. ret = udf_load_vat(sb, i, type1_idx);
  1213. if (ret < 0)
  1214. goto out_bh;
  1215. }
  1216. ret = 0;
  1217. out_bh:
  1218. /* In case loading failed, we handle cleanup in udf_fill_super */
  1219. brelse(bh);
  1220. return ret;
  1221. }
  1222. static int udf_load_sparable_map(struct super_block *sb,
  1223. struct udf_part_map *map,
  1224. struct sparablePartitionMap *spm)
  1225. {
  1226. uint32_t loc;
  1227. uint16_t ident;
  1228. struct sparingTable *st;
  1229. struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
  1230. int i;
  1231. struct buffer_head *bh;
  1232. map->s_partition_type = UDF_SPARABLE_MAP15;
  1233. sdata->s_packet_len = le16_to_cpu(spm->packetLength);
  1234. if (!is_power_of_2(sdata->s_packet_len)) {
  1235. udf_err(sb, "error loading logical volume descriptor: "
  1236. "Invalid packet length %u\n",
  1237. (unsigned)sdata->s_packet_len);
  1238. return -EIO;
  1239. }
  1240. if (spm->numSparingTables > 4) {
  1241. udf_err(sb, "error loading logical volume descriptor: "
  1242. "Too many sparing tables (%d)\n",
  1243. (int)spm->numSparingTables);
  1244. return -EIO;
  1245. }
  1246. if (le32_to_cpu(spm->sizeSparingTable) > sb->s_blocksize) {
  1247. udf_err(sb, "error loading logical volume descriptor: "
  1248. "Too big sparing table size (%u)\n",
  1249. le32_to_cpu(spm->sizeSparingTable));
  1250. return -EIO;
  1251. }
  1252. for (i = 0; i < spm->numSparingTables; i++) {
  1253. loc = le32_to_cpu(spm->locSparingTable[i]);
  1254. bh = udf_read_tagged(sb, loc, loc, &ident);
  1255. if (!bh)
  1256. continue;
  1257. st = (struct sparingTable *)bh->b_data;
  1258. if (ident != 0 ||
  1259. strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
  1260. strlen(UDF_ID_SPARING)) ||
  1261. sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
  1262. sb->s_blocksize) {
  1263. brelse(bh);
  1264. continue;
  1265. }
  1266. sdata->s_spar_map[i] = bh;
  1267. }
  1268. map->s_partition_func = udf_get_pblock_spar15;
  1269. return 0;
  1270. }
  1271. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1272. struct kernel_lb_addr *fileset)
  1273. {
  1274. struct logicalVolDesc *lvd;
  1275. int i, offset;
  1276. uint8_t type;
  1277. struct udf_sb_info *sbi = UDF_SB(sb);
  1278. struct genericPartitionMap *gpm;
  1279. uint16_t ident;
  1280. struct buffer_head *bh;
  1281. unsigned int table_len, part_map_count;
  1282. int ret;
  1283. bh = udf_read_tagged(sb, block, block, &ident);
  1284. if (!bh)
  1285. return -EAGAIN;
  1286. BUG_ON(ident != TAG_IDENT_LVD);
  1287. lvd = (struct logicalVolDesc *)bh->b_data;
  1288. table_len = le32_to_cpu(lvd->mapTableLength);
  1289. if (table_len > sb->s_blocksize - sizeof(*lvd)) {
  1290. udf_err(sb, "error loading logical volume descriptor: "
  1291. "Partition table too long (%u > %lu)\n", table_len,
  1292. sb->s_blocksize - sizeof(*lvd));
  1293. ret = -EIO;
  1294. goto out_bh;
  1295. }
  1296. ret = udf_verify_domain_identifier(sb, &lvd->domainIdent,
  1297. "logical volume");
  1298. if (ret)
  1299. goto out_bh;
  1300. part_map_count = le32_to_cpu(lvd->numPartitionMaps);
  1301. if (part_map_count > table_len / sizeof(struct genericPartitionMap1)) {
  1302. udf_err(sb, "error loading logical volume descriptor: "
  1303. "Too many partition maps (%u > %u)\n", part_map_count,
  1304. table_len / (unsigned)sizeof(struct genericPartitionMap1));
  1305. ret = -EIO;
  1306. goto out_bh;
  1307. }
  1308. ret = udf_sb_alloc_partition_maps(sb, part_map_count);
  1309. if (ret)
  1310. goto out_bh;
  1311. for (i = 0, offset = 0;
  1312. i < sbi->s_partitions && offset < table_len;
  1313. i++, offset += gpm->partitionMapLength) {
  1314. struct udf_part_map *map = &sbi->s_partmaps[i];
  1315. gpm = (struct genericPartitionMap *)
  1316. &(lvd->partitionMaps[offset]);
  1317. type = gpm->partitionMapType;
  1318. if (type == 1) {
  1319. struct genericPartitionMap1 *gpm1 =
  1320. (struct genericPartitionMap1 *)gpm;
  1321. map->s_partition_type = UDF_TYPE1_MAP15;
  1322. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1323. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1324. map->s_partition_func = NULL;
  1325. } else if (type == 2) {
  1326. struct udfPartitionMap2 *upm2 =
  1327. (struct udfPartitionMap2 *)gpm;
  1328. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1329. strlen(UDF_ID_VIRTUAL))) {
  1330. u16 suf =
  1331. le16_to_cpu(((__le16 *)upm2->partIdent.
  1332. identSuffix)[0]);
  1333. if (suf < 0x0200) {
  1334. map->s_partition_type =
  1335. UDF_VIRTUAL_MAP15;
  1336. map->s_partition_func =
  1337. udf_get_pblock_virt15;
  1338. } else {
  1339. map->s_partition_type =
  1340. UDF_VIRTUAL_MAP20;
  1341. map->s_partition_func =
  1342. udf_get_pblock_virt20;
  1343. }
  1344. } else if (!strncmp(upm2->partIdent.ident,
  1345. UDF_ID_SPARABLE,
  1346. strlen(UDF_ID_SPARABLE))) {
  1347. ret = udf_load_sparable_map(sb, map,
  1348. (struct sparablePartitionMap *)gpm);
  1349. if (ret < 0)
  1350. goto out_bh;
  1351. } else if (!strncmp(upm2->partIdent.ident,
  1352. UDF_ID_METADATA,
  1353. strlen(UDF_ID_METADATA))) {
  1354. struct udf_meta_data *mdata =
  1355. &map->s_type_specific.s_metadata;
  1356. struct metadataPartitionMap *mdm =
  1357. (struct metadataPartitionMap *)
  1358. &(lvd->partitionMaps[offset]);
  1359. udf_debug("Parsing Logical vol part %d type %u id=%s\n",
  1360. i, type, UDF_ID_METADATA);
  1361. map->s_partition_type = UDF_METADATA_MAP25;
  1362. map->s_partition_func = udf_get_pblock_meta25;
  1363. mdata->s_meta_file_loc =
  1364. le32_to_cpu(mdm->metadataFileLoc);
  1365. mdata->s_mirror_file_loc =
  1366. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1367. mdata->s_bitmap_file_loc =
  1368. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1369. mdata->s_alloc_unit_size =
  1370. le32_to_cpu(mdm->allocUnitSize);
  1371. mdata->s_align_unit_size =
  1372. le16_to_cpu(mdm->alignUnitSize);
  1373. if (mdm->flags & 0x01)
  1374. mdata->s_flags |= MF_DUPLICATE_MD;
  1375. udf_debug("Metadata Ident suffix=0x%x\n",
  1376. le16_to_cpu(*(__le16 *)
  1377. mdm->partIdent.identSuffix));
  1378. udf_debug("Metadata part num=%u\n",
  1379. le16_to_cpu(mdm->partitionNum));
  1380. udf_debug("Metadata part alloc unit size=%u\n",
  1381. le32_to_cpu(mdm->allocUnitSize));
  1382. udf_debug("Metadata file loc=%u\n",
  1383. le32_to_cpu(mdm->metadataFileLoc));
  1384. udf_debug("Mirror file loc=%u\n",
  1385. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1386. udf_debug("Bitmap file loc=%u\n",
  1387. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1388. udf_debug("Flags: %d %u\n",
  1389. mdata->s_flags, mdm->flags);
  1390. } else {
  1391. udf_debug("Unknown ident: %s\n",
  1392. upm2->partIdent.ident);
  1393. continue;
  1394. }
  1395. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1396. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1397. }
  1398. udf_debug("Partition (%d:%u) type %u on volume %u\n",
  1399. i, map->s_partition_num, type, map->s_volumeseqnum);
  1400. }
  1401. if (fileset) {
  1402. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1403. *fileset = lelb_to_cpu(la->extLocation);
  1404. udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
  1405. fileset->logicalBlockNum,
  1406. fileset->partitionReferenceNum);
  1407. }
  1408. if (lvd->integritySeqExt.extLength)
  1409. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1410. ret = 0;
  1411. if (!sbi->s_lvid_bh) {
  1412. /* We can't generate unique IDs without a valid LVID */
  1413. if (sb_rdonly(sb)) {
  1414. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  1415. } else {
  1416. udf_warn(sb, "Damaged or missing LVID, forcing "
  1417. "readonly mount\n");
  1418. ret = -EACCES;
  1419. }
  1420. }
  1421. out_bh:
  1422. brelse(bh);
  1423. return ret;
  1424. }
  1425. static bool udf_lvid_valid(struct super_block *sb,
  1426. struct logicalVolIntegrityDesc *lvid)
  1427. {
  1428. u32 parts, impuselen;
  1429. parts = le32_to_cpu(lvid->numOfPartitions);
  1430. impuselen = le32_to_cpu(lvid->lengthOfImpUse);
  1431. if (parts >= sb->s_blocksize || impuselen >= sb->s_blocksize ||
  1432. sizeof(struct logicalVolIntegrityDesc) + impuselen +
  1433. 2 * parts * sizeof(u32) > sb->s_blocksize)
  1434. return false;
  1435. return true;
  1436. }
  1437. /*
  1438. * Find the prevailing Logical Volume Integrity Descriptor.
  1439. */
  1440. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1441. {
  1442. struct buffer_head *bh, *final_bh;
  1443. uint16_t ident;
  1444. struct udf_sb_info *sbi = UDF_SB(sb);
  1445. struct logicalVolIntegrityDesc *lvid;
  1446. int indirections = 0;
  1447. while (++indirections <= UDF_MAX_LVID_NESTING) {
  1448. final_bh = NULL;
  1449. while (loc.extLength > 0 &&
  1450. (bh = udf_read_tagged(sb, loc.extLocation,
  1451. loc.extLocation, &ident))) {
  1452. if (ident != TAG_IDENT_LVID) {
  1453. brelse(bh);
  1454. break;
  1455. }
  1456. brelse(final_bh);
  1457. final_bh = bh;
  1458. loc.extLength -= sb->s_blocksize;
  1459. loc.extLocation++;
  1460. }
  1461. if (!final_bh)
  1462. return;
  1463. lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
  1464. if (udf_lvid_valid(sb, lvid)) {
  1465. brelse(sbi->s_lvid_bh);
  1466. sbi->s_lvid_bh = final_bh;
  1467. } else {
  1468. udf_warn(sb, "Corrupted LVID (parts=%u, impuselen=%u), "
  1469. "ignoring.\n",
  1470. le32_to_cpu(lvid->numOfPartitions),
  1471. le32_to_cpu(lvid->lengthOfImpUse));
  1472. }
  1473. if (lvid->nextIntegrityExt.extLength == 0)
  1474. return;
  1475. loc = leea_to_cpu(lvid->nextIntegrityExt);
  1476. }
  1477. udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
  1478. UDF_MAX_LVID_NESTING);
  1479. brelse(sbi->s_lvid_bh);
  1480. sbi->s_lvid_bh = NULL;
  1481. }
  1482. /*
  1483. * Step for reallocation of table of partition descriptor sequence numbers.
  1484. * Must be power of 2.
  1485. */
  1486. #define PART_DESC_ALLOC_STEP 32
  1487. struct part_desc_seq_scan_data {
  1488. struct udf_vds_record rec;
  1489. u32 partnum;
  1490. };
  1491. struct desc_seq_scan_data {
  1492. struct udf_vds_record vds[VDS_POS_LENGTH];
  1493. unsigned int size_part_descs;
  1494. unsigned int num_part_descs;
  1495. struct part_desc_seq_scan_data *part_descs_loc;
  1496. };
  1497. static struct udf_vds_record *handle_partition_descriptor(
  1498. struct buffer_head *bh,
  1499. struct desc_seq_scan_data *data)
  1500. {
  1501. struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
  1502. int partnum;
  1503. int i;
  1504. partnum = le16_to_cpu(desc->partitionNumber);
  1505. for (i = 0; i < data->num_part_descs; i++)
  1506. if (partnum == data->part_descs_loc[i].partnum)
  1507. return &(data->part_descs_loc[i].rec);
  1508. if (data->num_part_descs >= data->size_part_descs) {
  1509. struct part_desc_seq_scan_data *new_loc;
  1510. unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
  1511. new_loc = kzalloc_objs(*new_loc, new_size);
  1512. if (!new_loc)
  1513. return ERR_PTR(-ENOMEM);
  1514. memcpy(new_loc, data->part_descs_loc,
  1515. data->size_part_descs * sizeof(*new_loc));
  1516. kfree(data->part_descs_loc);
  1517. data->part_descs_loc = new_loc;
  1518. data->size_part_descs = new_size;
  1519. }
  1520. return &(data->part_descs_loc[data->num_part_descs++].rec);
  1521. }
  1522. static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
  1523. struct buffer_head *bh, struct desc_seq_scan_data *data)
  1524. {
  1525. switch (ident) {
  1526. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1527. return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
  1528. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1529. return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
  1530. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1531. return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
  1532. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1533. return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
  1534. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1535. return handle_partition_descriptor(bh, data);
  1536. }
  1537. return NULL;
  1538. }
  1539. /*
  1540. * Process a main/reserve volume descriptor sequence.
  1541. * @block First block of first extent of the sequence.
  1542. * @lastblock Lastblock of first extent of the sequence.
  1543. * @fileset There we store extent containing root fileset
  1544. *
  1545. * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
  1546. * sequence
  1547. */
  1548. static noinline int udf_process_sequence(
  1549. struct super_block *sb,
  1550. sector_t block, sector_t lastblock,
  1551. struct kernel_lb_addr *fileset)
  1552. {
  1553. struct buffer_head *bh = NULL;
  1554. struct udf_vds_record *curr;
  1555. struct generic_desc *gd;
  1556. struct volDescPtr *vdp;
  1557. bool done = false;
  1558. uint32_t vdsn;
  1559. uint16_t ident;
  1560. int ret;
  1561. unsigned int indirections = 0;
  1562. struct desc_seq_scan_data data;
  1563. unsigned int i;
  1564. memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1565. data.size_part_descs = PART_DESC_ALLOC_STEP;
  1566. data.num_part_descs = 0;
  1567. data.part_descs_loc = kzalloc_objs(*data.part_descs_loc,
  1568. data.size_part_descs);
  1569. if (!data.part_descs_loc)
  1570. return -ENOMEM;
  1571. /*
  1572. * Read the main descriptor sequence and find which descriptors
  1573. * are in it.
  1574. */
  1575. for (; (!done && block <= lastblock); block++) {
  1576. bh = udf_read_tagged(sb, block, block, &ident);
  1577. if (!bh)
  1578. break;
  1579. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1580. gd = (struct generic_desc *)bh->b_data;
  1581. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1582. switch (ident) {
  1583. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1584. if (++indirections > UDF_MAX_TD_NESTING) {
  1585. udf_err(sb, "too many Volume Descriptor "
  1586. "Pointers (max %u supported)\n",
  1587. UDF_MAX_TD_NESTING);
  1588. brelse(bh);
  1589. ret = -EIO;
  1590. goto out;
  1591. }
  1592. vdp = (struct volDescPtr *)bh->b_data;
  1593. block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
  1594. lastblock = le32_to_cpu(
  1595. vdp->nextVolDescSeqExt.extLength) >>
  1596. sb->s_blocksize_bits;
  1597. lastblock += block - 1;
  1598. /* For loop is going to increment 'block' again */
  1599. block--;
  1600. break;
  1601. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1602. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1603. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1604. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1605. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1606. curr = get_volume_descriptor_record(ident, bh, &data);
  1607. if (IS_ERR(curr)) {
  1608. brelse(bh);
  1609. ret = PTR_ERR(curr);
  1610. goto out;
  1611. }
  1612. /* Descriptor we don't care about? */
  1613. if (!curr)
  1614. break;
  1615. if (vdsn >= curr->volDescSeqNum) {
  1616. curr->volDescSeqNum = vdsn;
  1617. curr->block = block;
  1618. }
  1619. break;
  1620. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1621. done = true;
  1622. break;
  1623. }
  1624. brelse(bh);
  1625. }
  1626. /*
  1627. * Now read interesting descriptors again and process them
  1628. * in a suitable order
  1629. */
  1630. if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1631. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1632. ret = -EAGAIN;
  1633. goto out;
  1634. }
  1635. ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
  1636. if (ret < 0)
  1637. goto out;
  1638. if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
  1639. ret = udf_load_logicalvol(sb,
  1640. data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
  1641. fileset);
  1642. if (ret < 0)
  1643. goto out;
  1644. }
  1645. /* Now handle prevailing Partition Descriptors */
  1646. for (i = 0; i < data.num_part_descs; i++) {
  1647. ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
  1648. if (ret < 0)
  1649. goto out;
  1650. }
  1651. ret = 0;
  1652. out:
  1653. kfree(data.part_descs_loc);
  1654. return ret;
  1655. }
  1656. /*
  1657. * Load Volume Descriptor Sequence described by anchor in bh
  1658. *
  1659. * Returns <0 on error, 0 on success
  1660. */
  1661. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1662. struct kernel_lb_addr *fileset)
  1663. {
  1664. struct anchorVolDescPtr *anchor;
  1665. sector_t main_s, main_e, reserve_s, reserve_e;
  1666. int ret;
  1667. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1668. /* Locate the main sequence */
  1669. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1670. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1671. main_e = main_e >> sb->s_blocksize_bits;
  1672. main_e += main_s - 1;
  1673. /* Locate the reserve sequence */
  1674. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1675. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1676. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1677. reserve_e += reserve_s - 1;
  1678. /* Process the main & reserve sequences */
  1679. /* responsible for finding the PartitionDesc(s) */
  1680. ret = udf_process_sequence(sb, main_s, main_e, fileset);
  1681. if (ret != -EAGAIN)
  1682. return ret;
  1683. udf_sb_free_partitions(sb);
  1684. ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1685. if (ret < 0) {
  1686. udf_sb_free_partitions(sb);
  1687. /* No sequence was OK, return -EIO */
  1688. if (ret == -EAGAIN)
  1689. ret = -EIO;
  1690. }
  1691. return ret;
  1692. }
  1693. /*
  1694. * Check whether there is an anchor block in the given block and
  1695. * load Volume Descriptor Sequence if so.
  1696. *
  1697. * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
  1698. * block
  1699. */
  1700. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1701. struct kernel_lb_addr *fileset)
  1702. {
  1703. struct buffer_head *bh;
  1704. uint16_t ident;
  1705. int ret;
  1706. bh = udf_read_tagged(sb, block, block, &ident);
  1707. if (!bh)
  1708. return -EAGAIN;
  1709. if (ident != TAG_IDENT_AVDP) {
  1710. brelse(bh);
  1711. return -EAGAIN;
  1712. }
  1713. ret = udf_load_sequence(sb, bh, fileset);
  1714. brelse(bh);
  1715. return ret;
  1716. }
  1717. /*
  1718. * Search for an anchor volume descriptor pointer.
  1719. *
  1720. * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
  1721. * of anchors.
  1722. */
  1723. static int udf_scan_anchors(struct super_block *sb, udf_pblk_t *lastblock,
  1724. struct kernel_lb_addr *fileset)
  1725. {
  1726. udf_pblk_t last[6];
  1727. int i;
  1728. struct udf_sb_info *sbi = UDF_SB(sb);
  1729. int last_count = 0;
  1730. int ret;
  1731. /* First try user provided anchor */
  1732. if (sbi->s_anchor) {
  1733. ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
  1734. if (ret != -EAGAIN)
  1735. return ret;
  1736. }
  1737. /*
  1738. * according to spec, anchor is in either:
  1739. * block 256
  1740. * lastblock-256
  1741. * lastblock
  1742. * however, if the disc isn't closed, it could be 512.
  1743. */
  1744. ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
  1745. if (ret != -EAGAIN)
  1746. return ret;
  1747. /*
  1748. * The trouble is which block is the last one. Drives often misreport
  1749. * this so we try various possibilities.
  1750. */
  1751. last[last_count++] = *lastblock;
  1752. if (*lastblock >= 1)
  1753. last[last_count++] = *lastblock - 1;
  1754. last[last_count++] = *lastblock + 1;
  1755. if (*lastblock >= 2)
  1756. last[last_count++] = *lastblock - 2;
  1757. if (*lastblock >= 150)
  1758. last[last_count++] = *lastblock - 150;
  1759. if (*lastblock >= 152)
  1760. last[last_count++] = *lastblock - 152;
  1761. for (i = 0; i < last_count; i++) {
  1762. if (last[i] >= sb_bdev_nr_blocks(sb))
  1763. continue;
  1764. ret = udf_check_anchor_block(sb, last[i], fileset);
  1765. if (ret != -EAGAIN) {
  1766. if (!ret)
  1767. *lastblock = last[i];
  1768. return ret;
  1769. }
  1770. if (last[i] < 256)
  1771. continue;
  1772. ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
  1773. if (ret != -EAGAIN) {
  1774. if (!ret)
  1775. *lastblock = last[i];
  1776. return ret;
  1777. }
  1778. }
  1779. /* Finally try block 512 in case media is open */
  1780. return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
  1781. }
  1782. /*
  1783. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1784. * Descriptor Sequence.
  1785. *
  1786. * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
  1787. * block was not found.
  1788. */
  1789. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1790. int silent, struct kernel_lb_addr *fileset)
  1791. {
  1792. struct udf_sb_info *sbi = UDF_SB(sb);
  1793. int nsr = 0;
  1794. int ret;
  1795. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1796. if (!silent)
  1797. udf_warn(sb, "Bad block size\n");
  1798. return -EINVAL;
  1799. }
  1800. sbi->s_last_block = uopt->lastblock;
  1801. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_NOVRS)) {
  1802. /* Check that it is NSR02 compliant */
  1803. nsr = udf_check_vsd(sb);
  1804. if (!nsr) {
  1805. if (!silent)
  1806. udf_warn(sb, "No VRS found\n");
  1807. return -EINVAL;
  1808. }
  1809. if (nsr == -1)
  1810. udf_debug("Failed to read sector at offset %d. "
  1811. "Assuming open disc. Skipping validity "
  1812. "check\n", VSD_FIRST_SECTOR_OFFSET);
  1813. if (!sbi->s_last_block)
  1814. sbi->s_last_block = udf_get_last_block(sb);
  1815. } else {
  1816. udf_debug("Validity check skipped because of novrs option\n");
  1817. }
  1818. /* Look for anchor block and load Volume Descriptor Sequence */
  1819. sbi->s_anchor = uopt->anchor;
  1820. ret = udf_scan_anchors(sb, &sbi->s_last_block, fileset);
  1821. if (ret < 0) {
  1822. if (!silent && ret == -EAGAIN)
  1823. udf_warn(sb, "No anchor found\n");
  1824. return ret;
  1825. }
  1826. return 0;
  1827. }
  1828. static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid)
  1829. {
  1830. struct timespec64 ts;
  1831. ktime_get_real_ts64(&ts);
  1832. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
  1833. lvid->descTag.descCRC = cpu_to_le16(
  1834. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1835. le16_to_cpu(lvid->descTag.descCRCLength)));
  1836. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1837. }
  1838. static void udf_open_lvid(struct super_block *sb)
  1839. {
  1840. struct udf_sb_info *sbi = UDF_SB(sb);
  1841. struct buffer_head *bh = sbi->s_lvid_bh;
  1842. struct logicalVolIntegrityDesc *lvid;
  1843. struct logicalVolIntegrityDescImpUse *lvidiu;
  1844. if (!bh)
  1845. return;
  1846. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1847. lvidiu = udf_sb_lvidiu(sb);
  1848. if (!lvidiu)
  1849. return;
  1850. mutex_lock(&sbi->s_alloc_mutex);
  1851. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1852. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1853. if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
  1854. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1855. else
  1856. UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
  1857. udf_finalize_lvid(lvid);
  1858. mark_buffer_dirty(bh);
  1859. sbi->s_lvid_dirty = 0;
  1860. mutex_unlock(&sbi->s_alloc_mutex);
  1861. /* Make opening of filesystem visible on the media immediately */
  1862. sync_dirty_buffer(bh);
  1863. }
  1864. static void udf_close_lvid(struct super_block *sb)
  1865. {
  1866. struct udf_sb_info *sbi = UDF_SB(sb);
  1867. struct buffer_head *bh = sbi->s_lvid_bh;
  1868. struct logicalVolIntegrityDesc *lvid;
  1869. struct logicalVolIntegrityDescImpUse *lvidiu;
  1870. if (!bh)
  1871. return;
  1872. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1873. lvidiu = udf_sb_lvidiu(sb);
  1874. if (!lvidiu)
  1875. return;
  1876. mutex_lock(&sbi->s_alloc_mutex);
  1877. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1878. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1879. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1880. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1881. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1882. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1883. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1884. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1885. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
  1886. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1887. /*
  1888. * We set buffer uptodate unconditionally here to avoid spurious
  1889. * warnings from mark_buffer_dirty() when previous EIO has marked
  1890. * the buffer as !uptodate
  1891. */
  1892. set_buffer_uptodate(bh);
  1893. udf_finalize_lvid(lvid);
  1894. mark_buffer_dirty(bh);
  1895. sbi->s_lvid_dirty = 0;
  1896. mutex_unlock(&sbi->s_alloc_mutex);
  1897. /* Make closing of filesystem visible on the media immediately */
  1898. sync_dirty_buffer(bh);
  1899. }
  1900. u64 lvid_get_unique_id(struct super_block *sb)
  1901. {
  1902. struct buffer_head *bh;
  1903. struct udf_sb_info *sbi = UDF_SB(sb);
  1904. struct logicalVolIntegrityDesc *lvid;
  1905. struct logicalVolHeaderDesc *lvhd;
  1906. u64 uniqueID;
  1907. u64 ret;
  1908. bh = sbi->s_lvid_bh;
  1909. if (!bh)
  1910. return 0;
  1911. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1912. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1913. mutex_lock(&sbi->s_alloc_mutex);
  1914. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1915. if (!(++uniqueID & 0xFFFFFFFF))
  1916. uniqueID += 16;
  1917. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1918. udf_updated_lvid(sb);
  1919. mutex_unlock(&sbi->s_alloc_mutex);
  1920. return ret;
  1921. }
  1922. static int udf_fill_super(struct super_block *sb, struct fs_context *fc)
  1923. {
  1924. int ret = -EINVAL;
  1925. struct inode *inode = NULL;
  1926. struct udf_options *uopt = fc->fs_private;
  1927. struct kernel_lb_addr rootdir, fileset;
  1928. struct udf_sb_info *sbi;
  1929. bool lvid_open = false;
  1930. int silent = fc->sb_flags & SB_SILENT;
  1931. sbi = kzalloc_obj(*sbi);
  1932. if (!sbi)
  1933. return -ENOMEM;
  1934. sb->s_fs_info = sbi;
  1935. mutex_init(&sbi->s_alloc_mutex);
  1936. fileset.logicalBlockNum = 0xFFFFFFFF;
  1937. fileset.partitionReferenceNum = 0xFFFF;
  1938. sbi->s_flags = uopt->flags;
  1939. sbi->s_uid = uopt->uid;
  1940. sbi->s_gid = uopt->gid;
  1941. sbi->s_umask = uopt->umask;
  1942. sbi->s_fmode = uopt->fmode;
  1943. sbi->s_dmode = uopt->dmode;
  1944. sbi->s_nls_map = uopt->nls_map;
  1945. uopt->nls_map = NULL;
  1946. rwlock_init(&sbi->s_cred_lock);
  1947. if (uopt->session == 0xFFFFFFFF)
  1948. sbi->s_session = udf_get_last_session(sb);
  1949. else
  1950. sbi->s_session = uopt->session;
  1951. udf_debug("Multi-session=%d\n", sbi->s_session);
  1952. /* Fill in the rest of the superblock */
  1953. sb->s_op = &udf_sb_ops;
  1954. sb->s_export_op = &udf_export_ops;
  1955. sb->s_magic = UDF_SUPER_MAGIC;
  1956. sb->s_time_gran = 1000;
  1957. if (uopt->flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1958. ret = udf_load_vrs(sb, uopt, silent, &fileset);
  1959. } else {
  1960. uopt->blocksize = bdev_logical_block_size(sb->s_bdev);
  1961. while (uopt->blocksize <= 4096) {
  1962. ret = udf_load_vrs(sb, uopt, silent, &fileset);
  1963. if (ret < 0) {
  1964. if (!silent && ret != -EACCES) {
  1965. pr_notice("Scanning with blocksize %u failed\n",
  1966. uopt->blocksize);
  1967. }
  1968. brelse(sbi->s_lvid_bh);
  1969. sbi->s_lvid_bh = NULL;
  1970. /*
  1971. * EACCES is special - we want to propagate to
  1972. * upper layers that we cannot handle RW mount.
  1973. */
  1974. if (ret == -EACCES)
  1975. break;
  1976. } else
  1977. break;
  1978. uopt->blocksize <<= 1;
  1979. }
  1980. }
  1981. if (ret < 0) {
  1982. if (ret == -EAGAIN) {
  1983. udf_warn(sb, "No partition found (1)\n");
  1984. ret = -EINVAL;
  1985. }
  1986. goto error_out;
  1987. }
  1988. udf_debug("Lastblock=%u\n", sbi->s_last_block);
  1989. if (sbi->s_lvid_bh) {
  1990. struct logicalVolIntegrityDescImpUse *lvidiu =
  1991. udf_sb_lvidiu(sb);
  1992. uint16_t minUDFReadRev;
  1993. uint16_t minUDFWriteRev;
  1994. if (!lvidiu) {
  1995. ret = -EINVAL;
  1996. goto error_out;
  1997. }
  1998. minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1999. minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  2000. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  2001. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  2002. minUDFReadRev,
  2003. UDF_MAX_READ_VERSION);
  2004. ret = -EINVAL;
  2005. goto error_out;
  2006. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
  2007. if (!sb_rdonly(sb)) {
  2008. ret = -EACCES;
  2009. goto error_out;
  2010. }
  2011. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  2012. }
  2013. sbi->s_udfrev = minUDFWriteRev;
  2014. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  2015. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  2016. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  2017. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  2018. }
  2019. if (!sbi->s_partitions) {
  2020. udf_warn(sb, "No partition found (2)\n");
  2021. ret = -EINVAL;
  2022. goto error_out;
  2023. }
  2024. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  2025. UDF_PART_FLAG_READ_ONLY) {
  2026. if (!sb_rdonly(sb)) {
  2027. ret = -EACCES;
  2028. goto error_out;
  2029. }
  2030. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  2031. }
  2032. ret = udf_find_fileset(sb, &fileset, &rootdir);
  2033. if (ret < 0) {
  2034. udf_warn(sb, "No fileset found\n");
  2035. goto error_out;
  2036. }
  2037. if (!silent) {
  2038. struct timestamp ts;
  2039. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  2040. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  2041. sbi->s_volume_ident,
  2042. le16_to_cpu(ts.year), ts.month, ts.day,
  2043. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  2044. }
  2045. if (!sb_rdonly(sb)) {
  2046. udf_open_lvid(sb);
  2047. lvid_open = true;
  2048. }
  2049. /* Assign the root inode */
  2050. /* assign inodes by physical block number */
  2051. /* perhaps it's not extensible enough, but for now ... */
  2052. inode = udf_iget(sb, &rootdir);
  2053. if (IS_ERR(inode)) {
  2054. udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
  2055. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  2056. ret = PTR_ERR(inode);
  2057. goto error_out;
  2058. }
  2059. /* Allocate a dentry for the root inode */
  2060. sb->s_root = d_make_root(inode);
  2061. if (!sb->s_root) {
  2062. udf_err(sb, "Couldn't allocate root dentry\n");
  2063. ret = -ENOMEM;
  2064. goto error_out;
  2065. }
  2066. sb->s_maxbytes = UDF_MAX_FILESIZE;
  2067. sb->s_max_links = UDF_MAX_LINKS;
  2068. return 0;
  2069. error_out:
  2070. iput(sbi->s_vat_inode);
  2071. unload_nls(uopt->nls_map);
  2072. if (lvid_open)
  2073. udf_close_lvid(sb);
  2074. brelse(sbi->s_lvid_bh);
  2075. udf_sb_free_partitions(sb);
  2076. kfree(sbi);
  2077. sb->s_fs_info = NULL;
  2078. return ret;
  2079. }
  2080. void _udf_err(struct super_block *sb, const char *function,
  2081. const char *fmt, ...)
  2082. {
  2083. struct va_format vaf;
  2084. va_list args;
  2085. va_start(args, fmt);
  2086. vaf.fmt = fmt;
  2087. vaf.va = &args;
  2088. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  2089. va_end(args);
  2090. }
  2091. void _udf_warn(struct super_block *sb, const char *function,
  2092. const char *fmt, ...)
  2093. {
  2094. struct va_format vaf;
  2095. va_list args;
  2096. va_start(args, fmt);
  2097. vaf.fmt = fmt;
  2098. vaf.va = &args;
  2099. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  2100. va_end(args);
  2101. }
  2102. static void udf_put_super(struct super_block *sb)
  2103. {
  2104. struct udf_sb_info *sbi;
  2105. sbi = UDF_SB(sb);
  2106. iput(sbi->s_vat_inode);
  2107. unload_nls(sbi->s_nls_map);
  2108. if (!sb_rdonly(sb))
  2109. udf_close_lvid(sb);
  2110. brelse(sbi->s_lvid_bh);
  2111. udf_sb_free_partitions(sb);
  2112. mutex_destroy(&sbi->s_alloc_mutex);
  2113. kfree(sb->s_fs_info);
  2114. sb->s_fs_info = NULL;
  2115. }
  2116. static int udf_sync_fs(struct super_block *sb, int wait)
  2117. {
  2118. struct udf_sb_info *sbi = UDF_SB(sb);
  2119. mutex_lock(&sbi->s_alloc_mutex);
  2120. if (sbi->s_lvid_dirty) {
  2121. struct buffer_head *bh = sbi->s_lvid_bh;
  2122. struct logicalVolIntegrityDesc *lvid;
  2123. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  2124. udf_finalize_lvid(lvid);
  2125. /*
  2126. * Blockdevice will be synced later so we don't have to submit
  2127. * the buffer for IO
  2128. */
  2129. mark_buffer_dirty(bh);
  2130. sbi->s_lvid_dirty = 0;
  2131. }
  2132. mutex_unlock(&sbi->s_alloc_mutex);
  2133. return 0;
  2134. }
  2135. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  2136. {
  2137. struct super_block *sb = dentry->d_sb;
  2138. struct udf_sb_info *sbi = UDF_SB(sb);
  2139. struct logicalVolIntegrityDescImpUse *lvidiu;
  2140. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  2141. lvidiu = udf_sb_lvidiu(sb);
  2142. buf->f_type = UDF_SUPER_MAGIC;
  2143. buf->f_bsize = sb->s_blocksize;
  2144. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  2145. buf->f_bfree = udf_count_free(sb);
  2146. buf->f_bavail = buf->f_bfree;
  2147. /*
  2148. * Let's pretend each free block is also a free 'inode' since UDF does
  2149. * not have separate preallocated table of inodes.
  2150. */
  2151. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  2152. le32_to_cpu(lvidiu->numDirs)) : 0)
  2153. + buf->f_bfree;
  2154. buf->f_ffree = buf->f_bfree;
  2155. buf->f_namelen = UDF_NAME_LEN;
  2156. buf->f_fsid = u64_to_fsid(id);
  2157. return 0;
  2158. }
  2159. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  2160. struct udf_bitmap *bitmap)
  2161. {
  2162. struct buffer_head *bh = NULL;
  2163. unsigned int accum = 0;
  2164. int index;
  2165. udf_pblk_t block = 0, newblock;
  2166. struct kernel_lb_addr loc;
  2167. uint32_t bytes;
  2168. uint8_t *ptr;
  2169. uint16_t ident;
  2170. struct spaceBitmapDesc *bm;
  2171. loc.logicalBlockNum = bitmap->s_extPosition;
  2172. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  2173. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  2174. if (!bh) {
  2175. udf_err(sb, "udf_count_free failed\n");
  2176. goto out;
  2177. } else if (ident != TAG_IDENT_SBD) {
  2178. brelse(bh);
  2179. udf_err(sb, "udf_count_free failed\n");
  2180. goto out;
  2181. }
  2182. bm = (struct spaceBitmapDesc *)bh->b_data;
  2183. bytes = le32_to_cpu(bm->numOfBytes);
  2184. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  2185. ptr = (uint8_t *)bh->b_data;
  2186. while (bytes > 0) {
  2187. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  2188. accum += bitmap_weight((const unsigned long *)(ptr + index),
  2189. cur_bytes * 8);
  2190. bytes -= cur_bytes;
  2191. if (bytes) {
  2192. brelse(bh);
  2193. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  2194. bh = sb_bread(sb, newblock);
  2195. if (!bh) {
  2196. udf_debug("read failed\n");
  2197. goto out;
  2198. }
  2199. index = 0;
  2200. ptr = (uint8_t *)bh->b_data;
  2201. }
  2202. }
  2203. brelse(bh);
  2204. out:
  2205. return accum;
  2206. }
  2207. static unsigned int udf_count_free_table(struct super_block *sb,
  2208. struct inode *table)
  2209. {
  2210. unsigned int accum = 0;
  2211. uint32_t elen;
  2212. struct kernel_lb_addr eloc;
  2213. struct extent_position epos;
  2214. int8_t etype;
  2215. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2216. epos.block = UDF_I(table)->i_location;
  2217. epos.offset = sizeof(struct unallocSpaceEntry);
  2218. epos.bh = NULL;
  2219. while (udf_next_aext(table, &epos, &eloc, &elen, &etype, 1) > 0)
  2220. accum += (elen >> table->i_sb->s_blocksize_bits);
  2221. brelse(epos.bh);
  2222. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2223. return accum;
  2224. }
  2225. static unsigned int udf_count_free(struct super_block *sb)
  2226. {
  2227. unsigned int accum = 0;
  2228. struct udf_sb_info *sbi = UDF_SB(sb);
  2229. struct udf_part_map *map;
  2230. unsigned int part = sbi->s_partition;
  2231. int ptype = sbi->s_partmaps[part].s_partition_type;
  2232. if (ptype == UDF_METADATA_MAP25) {
  2233. part = sbi->s_partmaps[part].s_type_specific.s_metadata.
  2234. s_phys_partition_ref;
  2235. } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
  2236. /*
  2237. * Filesystems with VAT are append-only and we cannot write to
  2238. * them. Let's just report 0 here.
  2239. */
  2240. return 0;
  2241. }
  2242. if (sbi->s_lvid_bh) {
  2243. struct logicalVolIntegrityDesc *lvid =
  2244. (struct logicalVolIntegrityDesc *)
  2245. sbi->s_lvid_bh->b_data;
  2246. if (le32_to_cpu(lvid->numOfPartitions) > part) {
  2247. accum = le32_to_cpu(
  2248. lvid->freeSpaceTable[part]);
  2249. if (accum == 0xFFFFFFFF)
  2250. accum = 0;
  2251. }
  2252. }
  2253. if (accum)
  2254. return accum;
  2255. map = &sbi->s_partmaps[part];
  2256. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2257. accum += udf_count_free_bitmap(sb,
  2258. map->s_uspace.s_bitmap);
  2259. }
  2260. if (accum)
  2261. return accum;
  2262. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2263. accum += udf_count_free_table(sb,
  2264. map->s_uspace.s_table);
  2265. }
  2266. return accum;
  2267. }
  2268. MODULE_AUTHOR("Ben Fennema");
  2269. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  2270. MODULE_LICENSE("GPL");
  2271. module_init(init_udf_fs)
  2272. module_exit(exit_udf_fs)