super.c 62 KB

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