super.c 66 KB

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