super.c 66 KB

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