super.c 61 KB

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