super.c 66 KB

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