super.c 65 KB

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