super.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  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(sizeof(*uopt), GFP_KERNEL);
  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 = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
  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(struct_size(bitmap, s_block_bitmap, nr_groups),
  934. GFP_KERNEL);
  935. if (!bitmap)
  936. return NULL;
  937. bitmap->s_nr_groups = nr_groups;
  938. return bitmap;
  939. }
  940. static int check_partition_desc(struct super_block *sb,
  941. struct partitionDesc *p,
  942. struct udf_part_map *map)
  943. {
  944. bool umap, utable, fmap, ftable;
  945. struct partitionHeaderDesc *phd;
  946. switch (le32_to_cpu(p->accessType)) {
  947. case PD_ACCESS_TYPE_READ_ONLY:
  948. case PD_ACCESS_TYPE_WRITE_ONCE:
  949. case PD_ACCESS_TYPE_NONE:
  950. goto force_ro;
  951. }
  952. /* No Partition Header Descriptor? */
  953. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  954. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  955. goto force_ro;
  956. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  957. utable = phd->unallocSpaceTable.extLength;
  958. umap = phd->unallocSpaceBitmap.extLength;
  959. ftable = phd->freedSpaceTable.extLength;
  960. fmap = phd->freedSpaceBitmap.extLength;
  961. /* No allocation info? */
  962. if (!utable && !umap && !ftable && !fmap)
  963. goto force_ro;
  964. /* We don't support blocks that require erasing before overwrite */
  965. if (ftable || fmap)
  966. goto force_ro;
  967. /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
  968. if (utable && umap)
  969. goto force_ro;
  970. if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  971. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  972. map->s_partition_type == UDF_METADATA_MAP25)
  973. goto force_ro;
  974. return 0;
  975. force_ro:
  976. if (!sb_rdonly(sb))
  977. return -EACCES;
  978. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  979. return 0;
  980. }
  981. static int udf_fill_partdesc_info(struct super_block *sb,
  982. struct partitionDesc *p, int p_index)
  983. {
  984. struct udf_part_map *map;
  985. struct udf_sb_info *sbi = UDF_SB(sb);
  986. struct partitionHeaderDesc *phd;
  987. u32 sum;
  988. int err;
  989. map = &sbi->s_partmaps[p_index];
  990. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  991. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  992. if (check_add_overflow(map->s_partition_root, map->s_partition_len,
  993. &sum)) {
  994. udf_err(sb, "Partition %d has invalid location %u + %u\n",
  995. p_index, map->s_partition_root, map->s_partition_len);
  996. return -EFSCORRUPTED;
  997. }
  998. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  999. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  1000. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  1001. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  1002. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  1003. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  1004. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  1005. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  1006. udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
  1007. p_index, map->s_partition_type,
  1008. map->s_partition_root, map->s_partition_len);
  1009. err = check_partition_desc(sb, p, map);
  1010. if (err)
  1011. return err;
  1012. /*
  1013. * Skip loading allocation info it we cannot ever write to the fs.
  1014. * This is a correctness thing as we may have decided to force ro mount
  1015. * to avoid allocation info we don't support.
  1016. */
  1017. if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
  1018. return 0;
  1019. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  1020. if (phd->unallocSpaceTable.extLength) {
  1021. struct kernel_lb_addr loc = {
  1022. .logicalBlockNum = le32_to_cpu(
  1023. phd->unallocSpaceTable.extPosition),
  1024. .partitionReferenceNum = p_index,
  1025. };
  1026. struct inode *inode;
  1027. inode = udf_iget_special(sb, &loc);
  1028. if (IS_ERR(inode)) {
  1029. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  1030. p_index);
  1031. return PTR_ERR(inode);
  1032. }
  1033. map->s_uspace.s_table = inode;
  1034. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  1035. udf_debug("unallocSpaceTable (part %d) @ %lu\n",
  1036. p_index, map->s_uspace.s_table->i_ino);
  1037. }
  1038. if (phd->unallocSpaceBitmap.extLength) {
  1039. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  1040. if (!bitmap)
  1041. return -ENOMEM;
  1042. map->s_uspace.s_bitmap = bitmap;
  1043. bitmap->s_extPosition = le32_to_cpu(
  1044. phd->unallocSpaceBitmap.extPosition);
  1045. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  1046. /* Check whether math over bitmap won't overflow. */
  1047. if (check_add_overflow(map->s_partition_len,
  1048. sizeof(struct spaceBitmapDesc) << 3,
  1049. &sum)) {
  1050. udf_err(sb, "Partition %d is too long (%u)\n", p_index,
  1051. map->s_partition_len);
  1052. return -EFSCORRUPTED;
  1053. }
  1054. udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
  1055. p_index, bitmap->s_extPosition);
  1056. }
  1057. return 0;
  1058. }
  1059. static void udf_find_vat_block(struct super_block *sb, int p_index,
  1060. int type1_index, sector_t start_block)
  1061. {
  1062. struct udf_sb_info *sbi = UDF_SB(sb);
  1063. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1064. sector_t vat_block;
  1065. struct kernel_lb_addr ino;
  1066. struct inode *inode;
  1067. /*
  1068. * VAT file entry is in the last recorded block. Some broken disks have
  1069. * it a few blocks before so try a bit harder...
  1070. */
  1071. ino.partitionReferenceNum = type1_index;
  1072. for (vat_block = start_block;
  1073. vat_block >= map->s_partition_root &&
  1074. vat_block >= start_block - 3; vat_block--) {
  1075. ino.logicalBlockNum = vat_block - map->s_partition_root;
  1076. inode = udf_iget_special(sb, &ino);
  1077. if (!IS_ERR(inode)) {
  1078. sbi->s_vat_inode = inode;
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  1084. {
  1085. struct udf_sb_info *sbi = UDF_SB(sb);
  1086. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1087. struct buffer_head *bh = NULL;
  1088. struct udf_inode_info *vati;
  1089. struct virtualAllocationTable20 *vat20;
  1090. sector_t blocks = sb_bdev_nr_blocks(sb);
  1091. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  1092. if (!sbi->s_vat_inode &&
  1093. sbi->s_last_block != blocks - 1) {
  1094. pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
  1095. (unsigned long)sbi->s_last_block,
  1096. (unsigned long)blocks - 1);
  1097. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  1098. }
  1099. if (!sbi->s_vat_inode)
  1100. return -EIO;
  1101. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1102. map->s_type_specific.s_virtual.s_start_offset = 0;
  1103. map->s_type_specific.s_virtual.s_num_entries =
  1104. (sbi->s_vat_inode->i_size - 36) >> 2;
  1105. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1106. vati = UDF_I(sbi->s_vat_inode);
  1107. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1108. int err = 0;
  1109. bh = udf_bread(sbi->s_vat_inode, 0, 0, &err);
  1110. if (!bh) {
  1111. if (!err)
  1112. err = -EFSCORRUPTED;
  1113. return err;
  1114. }
  1115. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1116. } else {
  1117. vat20 = (struct virtualAllocationTable20 *)
  1118. vati->i_data;
  1119. }
  1120. map->s_type_specific.s_virtual.s_start_offset =
  1121. le16_to_cpu(vat20->lengthHeader);
  1122. map->s_type_specific.s_virtual.s_num_entries =
  1123. (sbi->s_vat_inode->i_size -
  1124. map->s_type_specific.s_virtual.
  1125. s_start_offset) >> 2;
  1126. brelse(bh);
  1127. }
  1128. return 0;
  1129. }
  1130. /*
  1131. * Load partition descriptor block
  1132. *
  1133. * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
  1134. * sequence.
  1135. */
  1136. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1137. {
  1138. struct buffer_head *bh;
  1139. struct partitionDesc *p;
  1140. struct udf_part_map *map;
  1141. struct udf_sb_info *sbi = UDF_SB(sb);
  1142. int i, type1_idx;
  1143. uint16_t partitionNumber;
  1144. uint16_t ident;
  1145. int ret;
  1146. bh = udf_read_tagged(sb, block, block, &ident);
  1147. if (!bh)
  1148. return -EAGAIN;
  1149. if (ident != TAG_IDENT_PD) {
  1150. ret = 0;
  1151. goto out_bh;
  1152. }
  1153. p = (struct partitionDesc *)bh->b_data;
  1154. partitionNumber = le16_to_cpu(p->partitionNumber);
  1155. /* First scan for TYPE1 and SPARABLE partitions */
  1156. for (i = 0; i < sbi->s_partitions; i++) {
  1157. map = &sbi->s_partmaps[i];
  1158. udf_debug("Searching map: (%u == %u)\n",
  1159. map->s_partition_num, partitionNumber);
  1160. if (map->s_partition_num == partitionNumber &&
  1161. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1162. map->s_partition_type == UDF_SPARABLE_MAP15))
  1163. break;
  1164. }
  1165. if (i >= sbi->s_partitions) {
  1166. udf_debug("Partition (%u) not found in partition map\n",
  1167. partitionNumber);
  1168. ret = 0;
  1169. goto out_bh;
  1170. }
  1171. ret = udf_fill_partdesc_info(sb, p, i);
  1172. if (ret < 0)
  1173. goto out_bh;
  1174. /*
  1175. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1176. * PHYSICAL partitions are already set up
  1177. */
  1178. type1_idx = i;
  1179. map = NULL; /* supress 'maybe used uninitialized' warning */
  1180. for (i = 0; i < sbi->s_partitions; i++) {
  1181. map = &sbi->s_partmaps[i];
  1182. if (map->s_partition_num == partitionNumber &&
  1183. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1184. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1185. map->s_partition_type == UDF_METADATA_MAP25))
  1186. break;
  1187. }
  1188. if (i >= sbi->s_partitions) {
  1189. ret = 0;
  1190. goto out_bh;
  1191. }
  1192. ret = udf_fill_partdesc_info(sb, p, i);
  1193. if (ret < 0)
  1194. goto out_bh;
  1195. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1196. ret = udf_load_metadata_files(sb, i, type1_idx);
  1197. if (ret < 0) {
  1198. udf_err(sb, "error loading MetaData partition map %d\n",
  1199. i);
  1200. goto out_bh;
  1201. }
  1202. } else {
  1203. /*
  1204. * If we have a partition with virtual map, we don't handle
  1205. * writing to it (we overwrite blocks instead of relocating
  1206. * them).
  1207. */
  1208. if (!sb_rdonly(sb)) {
  1209. ret = -EACCES;
  1210. goto out_bh;
  1211. }
  1212. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  1213. ret = udf_load_vat(sb, i, type1_idx);
  1214. if (ret < 0)
  1215. goto out_bh;
  1216. }
  1217. ret = 0;
  1218. out_bh:
  1219. /* In case loading failed, we handle cleanup in udf_fill_super */
  1220. brelse(bh);
  1221. return ret;
  1222. }
  1223. static int udf_load_sparable_map(struct super_block *sb,
  1224. struct udf_part_map *map,
  1225. struct sparablePartitionMap *spm)
  1226. {
  1227. uint32_t loc;
  1228. uint16_t ident;
  1229. struct sparingTable *st;
  1230. struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
  1231. int i;
  1232. struct buffer_head *bh;
  1233. map->s_partition_type = UDF_SPARABLE_MAP15;
  1234. sdata->s_packet_len = le16_to_cpu(spm->packetLength);
  1235. if (!is_power_of_2(sdata->s_packet_len)) {
  1236. udf_err(sb, "error loading logical volume descriptor: "
  1237. "Invalid packet length %u\n",
  1238. (unsigned)sdata->s_packet_len);
  1239. return -EIO;
  1240. }
  1241. if (spm->numSparingTables > 4) {
  1242. udf_err(sb, "error loading logical volume descriptor: "
  1243. "Too many sparing tables (%d)\n",
  1244. (int)spm->numSparingTables);
  1245. return -EIO;
  1246. }
  1247. if (le32_to_cpu(spm->sizeSparingTable) > sb->s_blocksize) {
  1248. udf_err(sb, "error loading logical volume descriptor: "
  1249. "Too big sparing table size (%u)\n",
  1250. le32_to_cpu(spm->sizeSparingTable));
  1251. return -EIO;
  1252. }
  1253. for (i = 0; i < spm->numSparingTables; i++) {
  1254. loc = le32_to_cpu(spm->locSparingTable[i]);
  1255. bh = udf_read_tagged(sb, loc, loc, &ident);
  1256. if (!bh)
  1257. continue;
  1258. st = (struct sparingTable *)bh->b_data;
  1259. if (ident != 0 ||
  1260. strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
  1261. strlen(UDF_ID_SPARING)) ||
  1262. sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
  1263. sb->s_blocksize) {
  1264. brelse(bh);
  1265. continue;
  1266. }
  1267. sdata->s_spar_map[i] = bh;
  1268. }
  1269. map->s_partition_func = udf_get_pblock_spar15;
  1270. return 0;
  1271. }
  1272. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1273. struct kernel_lb_addr *fileset)
  1274. {
  1275. struct logicalVolDesc *lvd;
  1276. int i, offset;
  1277. uint8_t type;
  1278. struct udf_sb_info *sbi = UDF_SB(sb);
  1279. struct genericPartitionMap *gpm;
  1280. uint16_t ident;
  1281. struct buffer_head *bh;
  1282. unsigned int table_len, part_map_count;
  1283. int ret;
  1284. bh = udf_read_tagged(sb, block, block, &ident);
  1285. if (!bh)
  1286. return -EAGAIN;
  1287. BUG_ON(ident != TAG_IDENT_LVD);
  1288. lvd = (struct logicalVolDesc *)bh->b_data;
  1289. table_len = le32_to_cpu(lvd->mapTableLength);
  1290. if (table_len > sb->s_blocksize - sizeof(*lvd)) {
  1291. udf_err(sb, "error loading logical volume descriptor: "
  1292. "Partition table too long (%u > %lu)\n", table_len,
  1293. sb->s_blocksize - sizeof(*lvd));
  1294. ret = -EIO;
  1295. goto out_bh;
  1296. }
  1297. ret = udf_verify_domain_identifier(sb, &lvd->domainIdent,
  1298. "logical volume");
  1299. if (ret)
  1300. goto out_bh;
  1301. part_map_count = le32_to_cpu(lvd->numPartitionMaps);
  1302. if (part_map_count > table_len / sizeof(struct genericPartitionMap1)) {
  1303. udf_err(sb, "error loading logical volume descriptor: "
  1304. "Too many partition maps (%u > %u)\n", part_map_count,
  1305. table_len / (unsigned)sizeof(struct genericPartitionMap1));
  1306. ret = -EIO;
  1307. goto out_bh;
  1308. }
  1309. ret = udf_sb_alloc_partition_maps(sb, part_map_count);
  1310. if (ret)
  1311. goto out_bh;
  1312. for (i = 0, offset = 0;
  1313. i < sbi->s_partitions && offset < table_len;
  1314. i++, offset += gpm->partitionMapLength) {
  1315. struct udf_part_map *map = &sbi->s_partmaps[i];
  1316. gpm = (struct genericPartitionMap *)
  1317. &(lvd->partitionMaps[offset]);
  1318. type = gpm->partitionMapType;
  1319. if (type == 1) {
  1320. struct genericPartitionMap1 *gpm1 =
  1321. (struct genericPartitionMap1 *)gpm;
  1322. map->s_partition_type = UDF_TYPE1_MAP15;
  1323. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1324. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1325. map->s_partition_func = NULL;
  1326. } else if (type == 2) {
  1327. struct udfPartitionMap2 *upm2 =
  1328. (struct udfPartitionMap2 *)gpm;
  1329. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1330. strlen(UDF_ID_VIRTUAL))) {
  1331. u16 suf =
  1332. le16_to_cpu(((__le16 *)upm2->partIdent.
  1333. identSuffix)[0]);
  1334. if (suf < 0x0200) {
  1335. map->s_partition_type =
  1336. UDF_VIRTUAL_MAP15;
  1337. map->s_partition_func =
  1338. udf_get_pblock_virt15;
  1339. } else {
  1340. map->s_partition_type =
  1341. UDF_VIRTUAL_MAP20;
  1342. map->s_partition_func =
  1343. udf_get_pblock_virt20;
  1344. }
  1345. } else if (!strncmp(upm2->partIdent.ident,
  1346. UDF_ID_SPARABLE,
  1347. strlen(UDF_ID_SPARABLE))) {
  1348. ret = udf_load_sparable_map(sb, map,
  1349. (struct sparablePartitionMap *)gpm);
  1350. if (ret < 0)
  1351. goto out_bh;
  1352. } else if (!strncmp(upm2->partIdent.ident,
  1353. UDF_ID_METADATA,
  1354. strlen(UDF_ID_METADATA))) {
  1355. struct udf_meta_data *mdata =
  1356. &map->s_type_specific.s_metadata;
  1357. struct metadataPartitionMap *mdm =
  1358. (struct metadataPartitionMap *)
  1359. &(lvd->partitionMaps[offset]);
  1360. udf_debug("Parsing Logical vol part %d type %u id=%s\n",
  1361. i, type, UDF_ID_METADATA);
  1362. map->s_partition_type = UDF_METADATA_MAP25;
  1363. map->s_partition_func = udf_get_pblock_meta25;
  1364. mdata->s_meta_file_loc =
  1365. le32_to_cpu(mdm->metadataFileLoc);
  1366. mdata->s_mirror_file_loc =
  1367. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1368. mdata->s_bitmap_file_loc =
  1369. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1370. mdata->s_alloc_unit_size =
  1371. le32_to_cpu(mdm->allocUnitSize);
  1372. mdata->s_align_unit_size =
  1373. le16_to_cpu(mdm->alignUnitSize);
  1374. if (mdm->flags & 0x01)
  1375. mdata->s_flags |= MF_DUPLICATE_MD;
  1376. udf_debug("Metadata Ident suffix=0x%x\n",
  1377. le16_to_cpu(*(__le16 *)
  1378. mdm->partIdent.identSuffix));
  1379. udf_debug("Metadata part num=%u\n",
  1380. le16_to_cpu(mdm->partitionNum));
  1381. udf_debug("Metadata part alloc unit size=%u\n",
  1382. le32_to_cpu(mdm->allocUnitSize));
  1383. udf_debug("Metadata file loc=%u\n",
  1384. le32_to_cpu(mdm->metadataFileLoc));
  1385. udf_debug("Mirror file loc=%u\n",
  1386. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1387. udf_debug("Bitmap file loc=%u\n",
  1388. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1389. udf_debug("Flags: %d %u\n",
  1390. mdata->s_flags, mdm->flags);
  1391. } else {
  1392. udf_debug("Unknown ident: %s\n",
  1393. upm2->partIdent.ident);
  1394. continue;
  1395. }
  1396. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1397. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1398. }
  1399. udf_debug("Partition (%d:%u) type %u on volume %u\n",
  1400. i, map->s_partition_num, type, map->s_volumeseqnum);
  1401. }
  1402. if (fileset) {
  1403. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1404. *fileset = lelb_to_cpu(la->extLocation);
  1405. udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
  1406. fileset->logicalBlockNum,
  1407. fileset->partitionReferenceNum);
  1408. }
  1409. if (lvd->integritySeqExt.extLength)
  1410. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1411. ret = 0;
  1412. if (!sbi->s_lvid_bh) {
  1413. /* We can't generate unique IDs without a valid LVID */
  1414. if (sb_rdonly(sb)) {
  1415. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  1416. } else {
  1417. udf_warn(sb, "Damaged or missing LVID, forcing "
  1418. "readonly mount\n");
  1419. ret = -EACCES;
  1420. }
  1421. }
  1422. out_bh:
  1423. brelse(bh);
  1424. return ret;
  1425. }
  1426. static bool udf_lvid_valid(struct super_block *sb,
  1427. struct logicalVolIntegrityDesc *lvid)
  1428. {
  1429. u32 parts, impuselen;
  1430. parts = le32_to_cpu(lvid->numOfPartitions);
  1431. impuselen = le32_to_cpu(lvid->lengthOfImpUse);
  1432. if (parts >= sb->s_blocksize || impuselen >= sb->s_blocksize ||
  1433. sizeof(struct logicalVolIntegrityDesc) + impuselen +
  1434. 2 * parts * sizeof(u32) > sb->s_blocksize)
  1435. return false;
  1436. return true;
  1437. }
  1438. /*
  1439. * Find the prevailing Logical Volume Integrity Descriptor.
  1440. */
  1441. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1442. {
  1443. struct buffer_head *bh, *final_bh;
  1444. uint16_t ident;
  1445. struct udf_sb_info *sbi = UDF_SB(sb);
  1446. struct logicalVolIntegrityDesc *lvid;
  1447. int indirections = 0;
  1448. while (++indirections <= UDF_MAX_LVID_NESTING) {
  1449. final_bh = NULL;
  1450. while (loc.extLength > 0 &&
  1451. (bh = udf_read_tagged(sb, loc.extLocation,
  1452. loc.extLocation, &ident))) {
  1453. if (ident != TAG_IDENT_LVID) {
  1454. brelse(bh);
  1455. break;
  1456. }
  1457. brelse(final_bh);
  1458. final_bh = bh;
  1459. loc.extLength -= sb->s_blocksize;
  1460. loc.extLocation++;
  1461. }
  1462. if (!final_bh)
  1463. return;
  1464. lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
  1465. if (udf_lvid_valid(sb, lvid)) {
  1466. brelse(sbi->s_lvid_bh);
  1467. sbi->s_lvid_bh = final_bh;
  1468. } else {
  1469. udf_warn(sb, "Corrupted LVID (parts=%u, impuselen=%u), "
  1470. "ignoring.\n",
  1471. le32_to_cpu(lvid->numOfPartitions),
  1472. le32_to_cpu(lvid->lengthOfImpUse));
  1473. }
  1474. if (lvid->nextIntegrityExt.extLength == 0)
  1475. return;
  1476. loc = leea_to_cpu(lvid->nextIntegrityExt);
  1477. }
  1478. udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
  1479. UDF_MAX_LVID_NESTING);
  1480. brelse(sbi->s_lvid_bh);
  1481. sbi->s_lvid_bh = NULL;
  1482. }
  1483. /*
  1484. * Step for reallocation of table of partition descriptor sequence numbers.
  1485. * Must be power of 2.
  1486. */
  1487. #define PART_DESC_ALLOC_STEP 32
  1488. struct part_desc_seq_scan_data {
  1489. struct udf_vds_record rec;
  1490. u32 partnum;
  1491. };
  1492. struct desc_seq_scan_data {
  1493. struct udf_vds_record vds[VDS_POS_LENGTH];
  1494. unsigned int size_part_descs;
  1495. unsigned int num_part_descs;
  1496. struct part_desc_seq_scan_data *part_descs_loc;
  1497. };
  1498. static struct udf_vds_record *handle_partition_descriptor(
  1499. struct buffer_head *bh,
  1500. struct desc_seq_scan_data *data)
  1501. {
  1502. struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
  1503. int partnum;
  1504. int i;
  1505. partnum = le16_to_cpu(desc->partitionNumber);
  1506. for (i = 0; i < data->num_part_descs; i++)
  1507. if (partnum == data->part_descs_loc[i].partnum)
  1508. return &(data->part_descs_loc[i].rec);
  1509. if (data->num_part_descs >= data->size_part_descs) {
  1510. struct part_desc_seq_scan_data *new_loc;
  1511. unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
  1512. new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL);
  1513. if (!new_loc)
  1514. return ERR_PTR(-ENOMEM);
  1515. memcpy(new_loc, data->part_descs_loc,
  1516. data->size_part_descs * sizeof(*new_loc));
  1517. kfree(data->part_descs_loc);
  1518. data->part_descs_loc = new_loc;
  1519. data->size_part_descs = new_size;
  1520. }
  1521. return &(data->part_descs_loc[data->num_part_descs++].rec);
  1522. }
  1523. static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
  1524. struct buffer_head *bh, struct desc_seq_scan_data *data)
  1525. {
  1526. switch (ident) {
  1527. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1528. return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
  1529. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1530. return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
  1531. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1532. return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
  1533. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1534. return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
  1535. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1536. return handle_partition_descriptor(bh, data);
  1537. }
  1538. return NULL;
  1539. }
  1540. /*
  1541. * Process a main/reserve volume descriptor sequence.
  1542. * @block First block of first extent of the sequence.
  1543. * @lastblock Lastblock of first extent of the sequence.
  1544. * @fileset There we store extent containing root fileset
  1545. *
  1546. * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
  1547. * sequence
  1548. */
  1549. static noinline int udf_process_sequence(
  1550. struct super_block *sb,
  1551. sector_t block, sector_t lastblock,
  1552. struct kernel_lb_addr *fileset)
  1553. {
  1554. struct buffer_head *bh = NULL;
  1555. struct udf_vds_record *curr;
  1556. struct generic_desc *gd;
  1557. struct volDescPtr *vdp;
  1558. bool done = false;
  1559. uint32_t vdsn;
  1560. uint16_t ident;
  1561. int ret;
  1562. unsigned int indirections = 0;
  1563. struct desc_seq_scan_data data;
  1564. unsigned int i;
  1565. memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1566. data.size_part_descs = PART_DESC_ALLOC_STEP;
  1567. data.num_part_descs = 0;
  1568. data.part_descs_loc = kcalloc(data.size_part_descs,
  1569. sizeof(*data.part_descs_loc),
  1570. GFP_KERNEL);
  1571. if (!data.part_descs_loc)
  1572. return -ENOMEM;
  1573. /*
  1574. * Read the main descriptor sequence and find which descriptors
  1575. * are in it.
  1576. */
  1577. for (; (!done && block <= lastblock); block++) {
  1578. bh = udf_read_tagged(sb, block, block, &ident);
  1579. if (!bh)
  1580. break;
  1581. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1582. gd = (struct generic_desc *)bh->b_data;
  1583. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1584. switch (ident) {
  1585. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1586. if (++indirections > UDF_MAX_TD_NESTING) {
  1587. udf_err(sb, "too many Volume Descriptor "
  1588. "Pointers (max %u supported)\n",
  1589. UDF_MAX_TD_NESTING);
  1590. brelse(bh);
  1591. ret = -EIO;
  1592. goto out;
  1593. }
  1594. vdp = (struct volDescPtr *)bh->b_data;
  1595. block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
  1596. lastblock = le32_to_cpu(
  1597. vdp->nextVolDescSeqExt.extLength) >>
  1598. sb->s_blocksize_bits;
  1599. lastblock += block - 1;
  1600. /* For loop is going to increment 'block' again */
  1601. block--;
  1602. break;
  1603. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1604. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1605. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1606. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1607. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1608. curr = get_volume_descriptor_record(ident, bh, &data);
  1609. if (IS_ERR(curr)) {
  1610. brelse(bh);
  1611. ret = PTR_ERR(curr);
  1612. goto out;
  1613. }
  1614. /* Descriptor we don't care about? */
  1615. if (!curr)
  1616. break;
  1617. if (vdsn >= curr->volDescSeqNum) {
  1618. curr->volDescSeqNum = vdsn;
  1619. curr->block = block;
  1620. }
  1621. break;
  1622. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1623. done = true;
  1624. break;
  1625. }
  1626. brelse(bh);
  1627. }
  1628. /*
  1629. * Now read interesting descriptors again and process them
  1630. * in a suitable order
  1631. */
  1632. if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1633. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1634. ret = -EAGAIN;
  1635. goto out;
  1636. }
  1637. ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
  1638. if (ret < 0)
  1639. goto out;
  1640. if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
  1641. ret = udf_load_logicalvol(sb,
  1642. data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
  1643. fileset);
  1644. if (ret < 0)
  1645. goto out;
  1646. }
  1647. /* Now handle prevailing Partition Descriptors */
  1648. for (i = 0; i < data.num_part_descs; i++) {
  1649. ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
  1650. if (ret < 0)
  1651. goto out;
  1652. }
  1653. ret = 0;
  1654. out:
  1655. kfree(data.part_descs_loc);
  1656. return ret;
  1657. }
  1658. /*
  1659. * Load Volume Descriptor Sequence described by anchor in bh
  1660. *
  1661. * Returns <0 on error, 0 on success
  1662. */
  1663. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1664. struct kernel_lb_addr *fileset)
  1665. {
  1666. struct anchorVolDescPtr *anchor;
  1667. sector_t main_s, main_e, reserve_s, reserve_e;
  1668. int ret;
  1669. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1670. /* Locate the main sequence */
  1671. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1672. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1673. main_e = main_e >> sb->s_blocksize_bits;
  1674. main_e += main_s - 1;
  1675. /* Locate the reserve sequence */
  1676. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1677. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1678. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1679. reserve_e += reserve_s - 1;
  1680. /* Process the main & reserve sequences */
  1681. /* responsible for finding the PartitionDesc(s) */
  1682. ret = udf_process_sequence(sb, main_s, main_e, fileset);
  1683. if (ret != -EAGAIN)
  1684. return ret;
  1685. udf_sb_free_partitions(sb);
  1686. ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1687. if (ret < 0) {
  1688. udf_sb_free_partitions(sb);
  1689. /* No sequence was OK, return -EIO */
  1690. if (ret == -EAGAIN)
  1691. ret = -EIO;
  1692. }
  1693. return ret;
  1694. }
  1695. /*
  1696. * Check whether there is an anchor block in the given block and
  1697. * load Volume Descriptor Sequence if so.
  1698. *
  1699. * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
  1700. * block
  1701. */
  1702. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1703. struct kernel_lb_addr *fileset)
  1704. {
  1705. struct buffer_head *bh;
  1706. uint16_t ident;
  1707. int ret;
  1708. bh = udf_read_tagged(sb, block, block, &ident);
  1709. if (!bh)
  1710. return -EAGAIN;
  1711. if (ident != TAG_IDENT_AVDP) {
  1712. brelse(bh);
  1713. return -EAGAIN;
  1714. }
  1715. ret = udf_load_sequence(sb, bh, fileset);
  1716. brelse(bh);
  1717. return ret;
  1718. }
  1719. /*
  1720. * Search for an anchor volume descriptor pointer.
  1721. *
  1722. * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
  1723. * of anchors.
  1724. */
  1725. static int udf_scan_anchors(struct super_block *sb, udf_pblk_t *lastblock,
  1726. struct kernel_lb_addr *fileset)
  1727. {
  1728. udf_pblk_t last[6];
  1729. int i;
  1730. struct udf_sb_info *sbi = UDF_SB(sb);
  1731. int last_count = 0;
  1732. int ret;
  1733. /* First try user provided anchor */
  1734. if (sbi->s_anchor) {
  1735. ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
  1736. if (ret != -EAGAIN)
  1737. return ret;
  1738. }
  1739. /*
  1740. * according to spec, anchor is in either:
  1741. * block 256
  1742. * lastblock-256
  1743. * lastblock
  1744. * however, if the disc isn't closed, it could be 512.
  1745. */
  1746. ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
  1747. if (ret != -EAGAIN)
  1748. return ret;
  1749. /*
  1750. * The trouble is which block is the last one. Drives often misreport
  1751. * this so we try various possibilities.
  1752. */
  1753. last[last_count++] = *lastblock;
  1754. if (*lastblock >= 1)
  1755. last[last_count++] = *lastblock - 1;
  1756. last[last_count++] = *lastblock + 1;
  1757. if (*lastblock >= 2)
  1758. last[last_count++] = *lastblock - 2;
  1759. if (*lastblock >= 150)
  1760. last[last_count++] = *lastblock - 150;
  1761. if (*lastblock >= 152)
  1762. last[last_count++] = *lastblock - 152;
  1763. for (i = 0; i < last_count; i++) {
  1764. if (last[i] >= sb_bdev_nr_blocks(sb))
  1765. continue;
  1766. ret = udf_check_anchor_block(sb, last[i], fileset);
  1767. if (ret != -EAGAIN) {
  1768. if (!ret)
  1769. *lastblock = last[i];
  1770. return ret;
  1771. }
  1772. if (last[i] < 256)
  1773. continue;
  1774. ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
  1775. if (ret != -EAGAIN) {
  1776. if (!ret)
  1777. *lastblock = last[i];
  1778. return ret;
  1779. }
  1780. }
  1781. /* Finally try block 512 in case media is open */
  1782. return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
  1783. }
  1784. /*
  1785. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1786. * Descriptor Sequence.
  1787. *
  1788. * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
  1789. * block was not found.
  1790. */
  1791. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1792. int silent, struct kernel_lb_addr *fileset)
  1793. {
  1794. struct udf_sb_info *sbi = UDF_SB(sb);
  1795. int nsr = 0;
  1796. int ret;
  1797. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1798. if (!silent)
  1799. udf_warn(sb, "Bad block size\n");
  1800. return -EINVAL;
  1801. }
  1802. sbi->s_last_block = uopt->lastblock;
  1803. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_NOVRS)) {
  1804. /* Check that it is NSR02 compliant */
  1805. nsr = udf_check_vsd(sb);
  1806. if (!nsr) {
  1807. if (!silent)
  1808. udf_warn(sb, "No VRS found\n");
  1809. return -EINVAL;
  1810. }
  1811. if (nsr == -1)
  1812. udf_debug("Failed to read sector at offset %d. "
  1813. "Assuming open disc. Skipping validity "
  1814. "check\n", VSD_FIRST_SECTOR_OFFSET);
  1815. if (!sbi->s_last_block)
  1816. sbi->s_last_block = udf_get_last_block(sb);
  1817. } else {
  1818. udf_debug("Validity check skipped because of novrs option\n");
  1819. }
  1820. /* Look for anchor block and load Volume Descriptor Sequence */
  1821. sbi->s_anchor = uopt->anchor;
  1822. ret = udf_scan_anchors(sb, &sbi->s_last_block, fileset);
  1823. if (ret < 0) {
  1824. if (!silent && ret == -EAGAIN)
  1825. udf_warn(sb, "No anchor found\n");
  1826. return ret;
  1827. }
  1828. return 0;
  1829. }
  1830. static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid)
  1831. {
  1832. struct timespec64 ts;
  1833. ktime_get_real_ts64(&ts);
  1834. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
  1835. lvid->descTag.descCRC = cpu_to_le16(
  1836. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1837. le16_to_cpu(lvid->descTag.descCRCLength)));
  1838. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1839. }
  1840. static void udf_open_lvid(struct super_block *sb)
  1841. {
  1842. struct udf_sb_info *sbi = UDF_SB(sb);
  1843. struct buffer_head *bh = sbi->s_lvid_bh;
  1844. struct logicalVolIntegrityDesc *lvid;
  1845. struct logicalVolIntegrityDescImpUse *lvidiu;
  1846. if (!bh)
  1847. return;
  1848. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1849. lvidiu = udf_sb_lvidiu(sb);
  1850. if (!lvidiu)
  1851. return;
  1852. mutex_lock(&sbi->s_alloc_mutex);
  1853. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1854. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1855. if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
  1856. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1857. else
  1858. UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
  1859. udf_finalize_lvid(lvid);
  1860. mark_buffer_dirty(bh);
  1861. sbi->s_lvid_dirty = 0;
  1862. mutex_unlock(&sbi->s_alloc_mutex);
  1863. /* Make opening of filesystem visible on the media immediately */
  1864. sync_dirty_buffer(bh);
  1865. }
  1866. static void udf_close_lvid(struct super_block *sb)
  1867. {
  1868. struct udf_sb_info *sbi = UDF_SB(sb);
  1869. struct buffer_head *bh = sbi->s_lvid_bh;
  1870. struct logicalVolIntegrityDesc *lvid;
  1871. struct logicalVolIntegrityDescImpUse *lvidiu;
  1872. if (!bh)
  1873. return;
  1874. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1875. lvidiu = udf_sb_lvidiu(sb);
  1876. if (!lvidiu)
  1877. return;
  1878. mutex_lock(&sbi->s_alloc_mutex);
  1879. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1880. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1881. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1882. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1883. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1884. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1885. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1886. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1887. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
  1888. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1889. /*
  1890. * We set buffer uptodate unconditionally here to avoid spurious
  1891. * warnings from mark_buffer_dirty() when previous EIO has marked
  1892. * the buffer as !uptodate
  1893. */
  1894. set_buffer_uptodate(bh);
  1895. udf_finalize_lvid(lvid);
  1896. mark_buffer_dirty(bh);
  1897. sbi->s_lvid_dirty = 0;
  1898. mutex_unlock(&sbi->s_alloc_mutex);
  1899. /* Make closing of filesystem visible on the media immediately */
  1900. sync_dirty_buffer(bh);
  1901. }
  1902. u64 lvid_get_unique_id(struct super_block *sb)
  1903. {
  1904. struct buffer_head *bh;
  1905. struct udf_sb_info *sbi = UDF_SB(sb);
  1906. struct logicalVolIntegrityDesc *lvid;
  1907. struct logicalVolHeaderDesc *lvhd;
  1908. u64 uniqueID;
  1909. u64 ret;
  1910. bh = sbi->s_lvid_bh;
  1911. if (!bh)
  1912. return 0;
  1913. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1914. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1915. mutex_lock(&sbi->s_alloc_mutex);
  1916. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1917. if (!(++uniqueID & 0xFFFFFFFF))
  1918. uniqueID += 16;
  1919. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1920. udf_updated_lvid(sb);
  1921. mutex_unlock(&sbi->s_alloc_mutex);
  1922. return ret;
  1923. }
  1924. static int udf_fill_super(struct super_block *sb, struct fs_context *fc)
  1925. {
  1926. int ret = -EINVAL;
  1927. struct inode *inode = NULL;
  1928. struct udf_options *uopt = fc->fs_private;
  1929. struct kernel_lb_addr rootdir, fileset;
  1930. struct udf_sb_info *sbi;
  1931. bool lvid_open = false;
  1932. int silent = fc->sb_flags & SB_SILENT;
  1933. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1934. if (!sbi)
  1935. return -ENOMEM;
  1936. sb->s_fs_info = sbi;
  1937. mutex_init(&sbi->s_alloc_mutex);
  1938. fileset.logicalBlockNum = 0xFFFFFFFF;
  1939. fileset.partitionReferenceNum = 0xFFFF;
  1940. sbi->s_flags = uopt->flags;
  1941. sbi->s_uid = uopt->uid;
  1942. sbi->s_gid = uopt->gid;
  1943. sbi->s_umask = uopt->umask;
  1944. sbi->s_fmode = uopt->fmode;
  1945. sbi->s_dmode = uopt->dmode;
  1946. sbi->s_nls_map = uopt->nls_map;
  1947. uopt->nls_map = NULL;
  1948. rwlock_init(&sbi->s_cred_lock);
  1949. if (uopt->session == 0xFFFFFFFF)
  1950. sbi->s_session = udf_get_last_session(sb);
  1951. else
  1952. sbi->s_session = uopt->session;
  1953. udf_debug("Multi-session=%d\n", sbi->s_session);
  1954. /* Fill in the rest of the superblock */
  1955. sb->s_op = &udf_sb_ops;
  1956. sb->s_export_op = &udf_export_ops;
  1957. sb->s_magic = UDF_SUPER_MAGIC;
  1958. sb->s_time_gran = 1000;
  1959. if (uopt->flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1960. ret = udf_load_vrs(sb, uopt, silent, &fileset);
  1961. } else {
  1962. uopt->blocksize = bdev_logical_block_size(sb->s_bdev);
  1963. while (uopt->blocksize <= 4096) {
  1964. ret = udf_load_vrs(sb, uopt, silent, &fileset);
  1965. if (ret < 0) {
  1966. if (!silent && ret != -EACCES) {
  1967. pr_notice("Scanning with blocksize %u failed\n",
  1968. uopt->blocksize);
  1969. }
  1970. brelse(sbi->s_lvid_bh);
  1971. sbi->s_lvid_bh = NULL;
  1972. /*
  1973. * EACCES is special - we want to propagate to
  1974. * upper layers that we cannot handle RW mount.
  1975. */
  1976. if (ret == -EACCES)
  1977. break;
  1978. } else
  1979. break;
  1980. uopt->blocksize <<= 1;
  1981. }
  1982. }
  1983. if (ret < 0) {
  1984. if (ret == -EAGAIN) {
  1985. udf_warn(sb, "No partition found (1)\n");
  1986. ret = -EINVAL;
  1987. }
  1988. goto error_out;
  1989. }
  1990. udf_debug("Lastblock=%u\n", sbi->s_last_block);
  1991. if (sbi->s_lvid_bh) {
  1992. struct logicalVolIntegrityDescImpUse *lvidiu =
  1993. udf_sb_lvidiu(sb);
  1994. uint16_t minUDFReadRev;
  1995. uint16_t minUDFWriteRev;
  1996. if (!lvidiu) {
  1997. ret = -EINVAL;
  1998. goto error_out;
  1999. }
  2000. minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  2001. minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  2002. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  2003. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  2004. minUDFReadRev,
  2005. UDF_MAX_READ_VERSION);
  2006. ret = -EINVAL;
  2007. goto error_out;
  2008. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
  2009. if (!sb_rdonly(sb)) {
  2010. ret = -EACCES;
  2011. goto error_out;
  2012. }
  2013. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  2014. }
  2015. sbi->s_udfrev = minUDFWriteRev;
  2016. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  2017. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  2018. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  2019. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  2020. }
  2021. if (!sbi->s_partitions) {
  2022. udf_warn(sb, "No partition found (2)\n");
  2023. ret = -EINVAL;
  2024. goto error_out;
  2025. }
  2026. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  2027. UDF_PART_FLAG_READ_ONLY) {
  2028. if (!sb_rdonly(sb)) {
  2029. ret = -EACCES;
  2030. goto error_out;
  2031. }
  2032. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  2033. }
  2034. ret = udf_find_fileset(sb, &fileset, &rootdir);
  2035. if (ret < 0) {
  2036. udf_warn(sb, "No fileset found\n");
  2037. goto error_out;
  2038. }
  2039. if (!silent) {
  2040. struct timestamp ts;
  2041. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  2042. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  2043. sbi->s_volume_ident,
  2044. le16_to_cpu(ts.year), ts.month, ts.day,
  2045. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  2046. }
  2047. if (!sb_rdonly(sb)) {
  2048. udf_open_lvid(sb);
  2049. lvid_open = true;
  2050. }
  2051. /* Assign the root inode */
  2052. /* assign inodes by physical block number */
  2053. /* perhaps it's not extensible enough, but for now ... */
  2054. inode = udf_iget(sb, &rootdir);
  2055. if (IS_ERR(inode)) {
  2056. udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
  2057. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  2058. ret = PTR_ERR(inode);
  2059. goto error_out;
  2060. }
  2061. /* Allocate a dentry for the root inode */
  2062. sb->s_root = d_make_root(inode);
  2063. if (!sb->s_root) {
  2064. udf_err(sb, "Couldn't allocate root dentry\n");
  2065. ret = -ENOMEM;
  2066. goto error_out;
  2067. }
  2068. sb->s_maxbytes = UDF_MAX_FILESIZE;
  2069. sb->s_max_links = UDF_MAX_LINKS;
  2070. return 0;
  2071. error_out:
  2072. iput(sbi->s_vat_inode);
  2073. unload_nls(uopt->nls_map);
  2074. if (lvid_open)
  2075. udf_close_lvid(sb);
  2076. brelse(sbi->s_lvid_bh);
  2077. udf_sb_free_partitions(sb);
  2078. kfree(sbi);
  2079. sb->s_fs_info = NULL;
  2080. return ret;
  2081. }
  2082. void _udf_err(struct super_block *sb, const char *function,
  2083. const char *fmt, ...)
  2084. {
  2085. struct va_format vaf;
  2086. va_list args;
  2087. va_start(args, fmt);
  2088. vaf.fmt = fmt;
  2089. vaf.va = &args;
  2090. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  2091. va_end(args);
  2092. }
  2093. void _udf_warn(struct super_block *sb, const char *function,
  2094. const char *fmt, ...)
  2095. {
  2096. struct va_format vaf;
  2097. va_list args;
  2098. va_start(args, fmt);
  2099. vaf.fmt = fmt;
  2100. vaf.va = &args;
  2101. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  2102. va_end(args);
  2103. }
  2104. static void udf_put_super(struct super_block *sb)
  2105. {
  2106. struct udf_sb_info *sbi;
  2107. sbi = UDF_SB(sb);
  2108. iput(sbi->s_vat_inode);
  2109. unload_nls(sbi->s_nls_map);
  2110. if (!sb_rdonly(sb))
  2111. udf_close_lvid(sb);
  2112. brelse(sbi->s_lvid_bh);
  2113. udf_sb_free_partitions(sb);
  2114. mutex_destroy(&sbi->s_alloc_mutex);
  2115. kfree(sb->s_fs_info);
  2116. sb->s_fs_info = NULL;
  2117. }
  2118. static int udf_sync_fs(struct super_block *sb, int wait)
  2119. {
  2120. struct udf_sb_info *sbi = UDF_SB(sb);
  2121. mutex_lock(&sbi->s_alloc_mutex);
  2122. if (sbi->s_lvid_dirty) {
  2123. struct buffer_head *bh = sbi->s_lvid_bh;
  2124. struct logicalVolIntegrityDesc *lvid;
  2125. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  2126. udf_finalize_lvid(lvid);
  2127. /*
  2128. * Blockdevice will be synced later so we don't have to submit
  2129. * the buffer for IO
  2130. */
  2131. mark_buffer_dirty(bh);
  2132. sbi->s_lvid_dirty = 0;
  2133. }
  2134. mutex_unlock(&sbi->s_alloc_mutex);
  2135. return 0;
  2136. }
  2137. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  2138. {
  2139. struct super_block *sb = dentry->d_sb;
  2140. struct udf_sb_info *sbi = UDF_SB(sb);
  2141. struct logicalVolIntegrityDescImpUse *lvidiu;
  2142. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  2143. lvidiu = udf_sb_lvidiu(sb);
  2144. buf->f_type = UDF_SUPER_MAGIC;
  2145. buf->f_bsize = sb->s_blocksize;
  2146. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  2147. buf->f_bfree = udf_count_free(sb);
  2148. buf->f_bavail = buf->f_bfree;
  2149. /*
  2150. * Let's pretend each free block is also a free 'inode' since UDF does
  2151. * not have separate preallocated table of inodes.
  2152. */
  2153. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  2154. le32_to_cpu(lvidiu->numDirs)) : 0)
  2155. + buf->f_bfree;
  2156. buf->f_ffree = buf->f_bfree;
  2157. buf->f_namelen = UDF_NAME_LEN;
  2158. buf->f_fsid = u64_to_fsid(id);
  2159. return 0;
  2160. }
  2161. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  2162. struct udf_bitmap *bitmap)
  2163. {
  2164. struct buffer_head *bh = NULL;
  2165. unsigned int accum = 0;
  2166. int index;
  2167. udf_pblk_t block = 0, newblock;
  2168. struct kernel_lb_addr loc;
  2169. uint32_t bytes;
  2170. uint8_t *ptr;
  2171. uint16_t ident;
  2172. struct spaceBitmapDesc *bm;
  2173. loc.logicalBlockNum = bitmap->s_extPosition;
  2174. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  2175. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  2176. if (!bh) {
  2177. udf_err(sb, "udf_count_free failed\n");
  2178. goto out;
  2179. } else if (ident != TAG_IDENT_SBD) {
  2180. brelse(bh);
  2181. udf_err(sb, "udf_count_free failed\n");
  2182. goto out;
  2183. }
  2184. bm = (struct spaceBitmapDesc *)bh->b_data;
  2185. bytes = le32_to_cpu(bm->numOfBytes);
  2186. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  2187. ptr = (uint8_t *)bh->b_data;
  2188. while (bytes > 0) {
  2189. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  2190. accum += bitmap_weight((const unsigned long *)(ptr + index),
  2191. cur_bytes * 8);
  2192. bytes -= cur_bytes;
  2193. if (bytes) {
  2194. brelse(bh);
  2195. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  2196. bh = sb_bread(sb, newblock);
  2197. if (!bh) {
  2198. udf_debug("read failed\n");
  2199. goto out;
  2200. }
  2201. index = 0;
  2202. ptr = (uint8_t *)bh->b_data;
  2203. }
  2204. }
  2205. brelse(bh);
  2206. out:
  2207. return accum;
  2208. }
  2209. static unsigned int udf_count_free_table(struct super_block *sb,
  2210. struct inode *table)
  2211. {
  2212. unsigned int accum = 0;
  2213. uint32_t elen;
  2214. struct kernel_lb_addr eloc;
  2215. struct extent_position epos;
  2216. int8_t etype;
  2217. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2218. epos.block = UDF_I(table)->i_location;
  2219. epos.offset = sizeof(struct unallocSpaceEntry);
  2220. epos.bh = NULL;
  2221. while (udf_next_aext(table, &epos, &eloc, &elen, &etype, 1) > 0)
  2222. accum += (elen >> table->i_sb->s_blocksize_bits);
  2223. brelse(epos.bh);
  2224. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2225. return accum;
  2226. }
  2227. static unsigned int udf_count_free(struct super_block *sb)
  2228. {
  2229. unsigned int accum = 0;
  2230. struct udf_sb_info *sbi = UDF_SB(sb);
  2231. struct udf_part_map *map;
  2232. unsigned int part = sbi->s_partition;
  2233. int ptype = sbi->s_partmaps[part].s_partition_type;
  2234. if (ptype == UDF_METADATA_MAP25) {
  2235. part = sbi->s_partmaps[part].s_type_specific.s_metadata.
  2236. s_phys_partition_ref;
  2237. } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
  2238. /*
  2239. * Filesystems with VAT are append-only and we cannot write to
  2240. * them. Let's just report 0 here.
  2241. */
  2242. return 0;
  2243. }
  2244. if (sbi->s_lvid_bh) {
  2245. struct logicalVolIntegrityDesc *lvid =
  2246. (struct logicalVolIntegrityDesc *)
  2247. sbi->s_lvid_bh->b_data;
  2248. if (le32_to_cpu(lvid->numOfPartitions) > part) {
  2249. accum = le32_to_cpu(
  2250. lvid->freeSpaceTable[part]);
  2251. if (accum == 0xFFFFFFFF)
  2252. accum = 0;
  2253. }
  2254. }
  2255. if (accum)
  2256. return accum;
  2257. map = &sbi->s_partmaps[part];
  2258. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2259. accum += udf_count_free_bitmap(sb,
  2260. map->s_uspace.s_bitmap);
  2261. }
  2262. if (accum)
  2263. return accum;
  2264. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2265. accum += udf_count_free_table(sb,
  2266. map->s_uspace.s_table);
  2267. }
  2268. return accum;
  2269. }
  2270. MODULE_AUTHOR("Ben Fennema");
  2271. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  2272. MODULE_LICENSE("GPL");
  2273. module_init(init_udf_fs)
  2274. module_exit(exit_udf_fs)