super.c 67 KB

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