super.c 61 KB

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