super.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302
  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 "
  586. "found\n");
  587. break;
  588. case 2:
  589. udf_debug("ISO9660 Supplementary Volume "
  590. "Descriptor found\n");
  591. break;
  592. case 3:
  593. udf_debug("ISO9660 Volume Partition Descriptor "
  594. "found\n");
  595. break;
  596. case 255:
  597. udf_debug("ISO9660 Volume Descriptor Set "
  598. "Terminator found\n");
  599. break;
  600. default:
  601. udf_debug("ISO9660 VRS (%u) found\n",
  602. vsd->structType);
  603. break;
  604. }
  605. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  606. VSD_STD_ID_LEN))
  607. ; /* nothing */
  608. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  609. VSD_STD_ID_LEN)) {
  610. brelse(bh);
  611. break;
  612. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  613. VSD_STD_ID_LEN))
  614. nsr02 = sector;
  615. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  616. VSD_STD_ID_LEN))
  617. nsr03 = sector;
  618. brelse(bh);
  619. }
  620. if (nsr03)
  621. return nsr03;
  622. else if (nsr02)
  623. return nsr02;
  624. else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
  625. return -1;
  626. else
  627. return 0;
  628. }
  629. static int udf_find_fileset(struct super_block *sb,
  630. struct kernel_lb_addr *fileset,
  631. struct kernel_lb_addr *root)
  632. {
  633. struct buffer_head *bh = NULL;
  634. long lastblock;
  635. uint16_t ident;
  636. struct udf_sb_info *sbi;
  637. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  638. fileset->partitionReferenceNum != 0xFFFF) {
  639. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  640. if (!bh) {
  641. return 1;
  642. } else if (ident != TAG_IDENT_FSD) {
  643. brelse(bh);
  644. return 1;
  645. }
  646. }
  647. sbi = UDF_SB(sb);
  648. if (!bh) {
  649. /* Search backwards through the partitions */
  650. struct kernel_lb_addr newfileset;
  651. /* --> cvg: FIXME - is it reasonable? */
  652. return 1;
  653. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  654. (newfileset.partitionReferenceNum != 0xFFFF &&
  655. fileset->logicalBlockNum == 0xFFFFFFFF &&
  656. fileset->partitionReferenceNum == 0xFFFF);
  657. newfileset.partitionReferenceNum--) {
  658. lastblock = sbi->s_partmaps
  659. [newfileset.partitionReferenceNum]
  660. .s_partition_len;
  661. newfileset.logicalBlockNum = 0;
  662. do {
  663. bh = udf_read_ptagged(sb, &newfileset, 0,
  664. &ident);
  665. if (!bh) {
  666. newfileset.logicalBlockNum++;
  667. continue;
  668. }
  669. switch (ident) {
  670. case TAG_IDENT_SBD:
  671. {
  672. struct spaceBitmapDesc *sp;
  673. sp = (struct spaceBitmapDesc *)
  674. bh->b_data;
  675. newfileset.logicalBlockNum += 1 +
  676. ((le32_to_cpu(sp->numOfBytes) +
  677. sizeof(struct spaceBitmapDesc)
  678. - 1) >> sb->s_blocksize_bits);
  679. brelse(bh);
  680. break;
  681. }
  682. case TAG_IDENT_FSD:
  683. *fileset = newfileset;
  684. break;
  685. default:
  686. newfileset.logicalBlockNum++;
  687. brelse(bh);
  688. bh = NULL;
  689. break;
  690. }
  691. } while (newfileset.logicalBlockNum < lastblock &&
  692. fileset->logicalBlockNum == 0xFFFFFFFF &&
  693. fileset->partitionReferenceNum == 0xFFFF);
  694. }
  695. }
  696. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  697. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  698. udf_debug("Fileset at block=%d, partition=%d\n",
  699. fileset->logicalBlockNum,
  700. fileset->partitionReferenceNum);
  701. sbi->s_partition = fileset->partitionReferenceNum;
  702. udf_load_fileset(sb, bh, root);
  703. brelse(bh);
  704. return 0;
  705. }
  706. return 1;
  707. }
  708. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  709. {
  710. struct primaryVolDesc *pvoldesc;
  711. struct ustr *instr, *outstr;
  712. struct buffer_head *bh;
  713. uint16_t ident;
  714. int ret = 1;
  715. instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  716. if (!instr)
  717. return 1;
  718. outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  719. if (!outstr)
  720. goto out1;
  721. bh = udf_read_tagged(sb, block, block, &ident);
  722. if (!bh)
  723. goto out2;
  724. BUG_ON(ident != TAG_IDENT_PVD);
  725. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  726. if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  727. pvoldesc->recordingDateAndTime)) {
  728. #ifdef UDFFS_DEBUG
  729. struct timestamp *ts = &pvoldesc->recordingDateAndTime;
  730. udf_debug("recording time %04u/%02u/%02u"
  731. " %02u:%02u (%x)\n",
  732. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  733. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  734. #endif
  735. }
  736. if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
  737. if (udf_CS0toUTF8(outstr, instr)) {
  738. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  739. outstr->u_len > 31 ? 31 : outstr->u_len);
  740. udf_debug("volIdent[] = '%s'\n",
  741. UDF_SB(sb)->s_volume_ident);
  742. }
  743. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  744. if (udf_CS0toUTF8(outstr, instr))
  745. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  746. brelse(bh);
  747. ret = 0;
  748. out2:
  749. kfree(outstr);
  750. out1:
  751. kfree(instr);
  752. return ret;
  753. }
  754. static int udf_load_metadata_files(struct super_block *sb, int partition)
  755. {
  756. struct udf_sb_info *sbi = UDF_SB(sb);
  757. struct udf_part_map *map;
  758. struct udf_meta_data *mdata;
  759. struct kernel_lb_addr addr;
  760. int fe_error = 0;
  761. map = &sbi->s_partmaps[partition];
  762. mdata = &map->s_type_specific.s_metadata;
  763. /* metadata address */
  764. addr.logicalBlockNum = mdata->s_meta_file_loc;
  765. addr.partitionReferenceNum = map->s_partition_num;
  766. udf_debug("Metadata file location: block = %d part = %d\n",
  767. addr.logicalBlockNum, addr.partitionReferenceNum);
  768. mdata->s_metadata_fe = udf_iget(sb, &addr);
  769. if (mdata->s_metadata_fe == NULL) {
  770. udf_warn(sb, "metadata inode efe not found, will try mirror inode\n");
  771. fe_error = 1;
  772. } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
  773. ICBTAG_FLAG_AD_SHORT) {
  774. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  775. fe_error = 1;
  776. iput(mdata->s_metadata_fe);
  777. mdata->s_metadata_fe = NULL;
  778. }
  779. /* mirror file entry */
  780. addr.logicalBlockNum = mdata->s_mirror_file_loc;
  781. addr.partitionReferenceNum = map->s_partition_num;
  782. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  783. addr.logicalBlockNum, addr.partitionReferenceNum);
  784. mdata->s_mirror_fe = udf_iget(sb, &addr);
  785. if (mdata->s_mirror_fe == NULL) {
  786. if (fe_error) {
  787. udf_err(sb, "mirror inode efe not found and metadata inode is missing too, exiting...\n");
  788. goto error_exit;
  789. } else
  790. udf_warn(sb, "mirror inode efe not found, but metadata inode is OK\n");
  791. } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
  792. ICBTAG_FLAG_AD_SHORT) {
  793. udf_warn(sb, "mirror inode efe does not have short allocation descriptors!\n");
  794. iput(mdata->s_mirror_fe);
  795. mdata->s_mirror_fe = NULL;
  796. if (fe_error)
  797. goto error_exit;
  798. }
  799. /*
  800. * bitmap file entry
  801. * Note:
  802. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  803. */
  804. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  805. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  806. addr.partitionReferenceNum = map->s_partition_num;
  807. udf_debug("Bitmap file location: block = %d part = %d\n",
  808. addr.logicalBlockNum, addr.partitionReferenceNum);
  809. mdata->s_bitmap_fe = udf_iget(sb, &addr);
  810. if (mdata->s_bitmap_fe == NULL) {
  811. if (sb->s_flags & MS_RDONLY)
  812. udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
  813. else {
  814. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  815. goto error_exit;
  816. }
  817. }
  818. }
  819. udf_debug("udf_load_metadata_files Ok\n");
  820. return 0;
  821. error_exit:
  822. return 1;
  823. }
  824. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  825. struct kernel_lb_addr *root)
  826. {
  827. struct fileSetDesc *fset;
  828. fset = (struct fileSetDesc *)bh->b_data;
  829. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  830. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  831. udf_debug("Rootdir at block=%d, partition=%d\n",
  832. root->logicalBlockNum, root->partitionReferenceNum);
  833. }
  834. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  835. {
  836. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  837. return DIV_ROUND_UP(map->s_partition_len +
  838. (sizeof(struct spaceBitmapDesc) << 3),
  839. sb->s_blocksize * 8);
  840. }
  841. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  842. {
  843. struct udf_bitmap *bitmap;
  844. int nr_groups;
  845. int size;
  846. nr_groups = udf_compute_nr_groups(sb, index);
  847. size = sizeof(struct udf_bitmap) +
  848. (sizeof(struct buffer_head *) * nr_groups);
  849. if (size <= PAGE_SIZE)
  850. bitmap = kzalloc(size, GFP_KERNEL);
  851. else
  852. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  853. if (bitmap == NULL) {
  854. udf_err(sb, "Unable to allocate space for bitmap and %d buffer_head pointers\n",
  855. nr_groups);
  856. return NULL;
  857. }
  858. bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
  859. bitmap->s_nr_groups = nr_groups;
  860. return bitmap;
  861. }
  862. static int udf_fill_partdesc_info(struct super_block *sb,
  863. struct partitionDesc *p, int p_index)
  864. {
  865. struct udf_part_map *map;
  866. struct udf_sb_info *sbi = UDF_SB(sb);
  867. struct partitionHeaderDesc *phd;
  868. map = &sbi->s_partmaps[p_index];
  869. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  870. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  871. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  872. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  873. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  874. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  875. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  876. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  877. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  878. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  879. udf_debug("Partition (%d type %x) starts at physical %d, "
  880. "block length %d\n", p_index,
  881. map->s_partition_type, map->s_partition_root,
  882. map->s_partition_len);
  883. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  884. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  885. return 0;
  886. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  887. if (phd->unallocSpaceTable.extLength) {
  888. struct kernel_lb_addr loc = {
  889. .logicalBlockNum = le32_to_cpu(
  890. phd->unallocSpaceTable.extPosition),
  891. .partitionReferenceNum = p_index,
  892. };
  893. map->s_uspace.s_table = udf_iget(sb, &loc);
  894. if (!map->s_uspace.s_table) {
  895. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  896. p_index);
  897. return 1;
  898. }
  899. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  900. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  901. p_index, map->s_uspace.s_table->i_ino);
  902. }
  903. if (phd->unallocSpaceBitmap.extLength) {
  904. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  905. if (!bitmap)
  906. return 1;
  907. map->s_uspace.s_bitmap = bitmap;
  908. bitmap->s_extLength = le32_to_cpu(
  909. phd->unallocSpaceBitmap.extLength);
  910. bitmap->s_extPosition = le32_to_cpu(
  911. phd->unallocSpaceBitmap.extPosition);
  912. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  913. udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
  914. bitmap->s_extPosition);
  915. }
  916. if (phd->partitionIntegrityTable.extLength)
  917. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  918. if (phd->freedSpaceTable.extLength) {
  919. struct kernel_lb_addr loc = {
  920. .logicalBlockNum = le32_to_cpu(
  921. phd->freedSpaceTable.extPosition),
  922. .partitionReferenceNum = p_index,
  923. };
  924. map->s_fspace.s_table = udf_iget(sb, &loc);
  925. if (!map->s_fspace.s_table) {
  926. udf_debug("cannot load freedSpaceTable (part %d)\n",
  927. p_index);
  928. return 1;
  929. }
  930. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  931. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  932. p_index, map->s_fspace.s_table->i_ino);
  933. }
  934. if (phd->freedSpaceBitmap.extLength) {
  935. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  936. if (!bitmap)
  937. return 1;
  938. map->s_fspace.s_bitmap = bitmap;
  939. bitmap->s_extLength = le32_to_cpu(
  940. phd->freedSpaceBitmap.extLength);
  941. bitmap->s_extPosition = le32_to_cpu(
  942. phd->freedSpaceBitmap.extPosition);
  943. map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
  944. udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
  945. bitmap->s_extPosition);
  946. }
  947. return 0;
  948. }
  949. static void udf_find_vat_block(struct super_block *sb, int p_index,
  950. int type1_index, sector_t start_block)
  951. {
  952. struct udf_sb_info *sbi = UDF_SB(sb);
  953. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  954. sector_t vat_block;
  955. struct kernel_lb_addr ino;
  956. /*
  957. * VAT file entry is in the last recorded block. Some broken disks have
  958. * it a few blocks before so try a bit harder...
  959. */
  960. ino.partitionReferenceNum = type1_index;
  961. for (vat_block = start_block;
  962. vat_block >= map->s_partition_root &&
  963. vat_block >= start_block - 3 &&
  964. !sbi->s_vat_inode; vat_block--) {
  965. ino.logicalBlockNum = vat_block - map->s_partition_root;
  966. sbi->s_vat_inode = udf_iget(sb, &ino);
  967. }
  968. }
  969. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  970. {
  971. struct udf_sb_info *sbi = UDF_SB(sb);
  972. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  973. struct buffer_head *bh = NULL;
  974. struct udf_inode_info *vati;
  975. uint32_t pos;
  976. struct virtualAllocationTable20 *vat20;
  977. sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  978. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  979. if (!sbi->s_vat_inode &&
  980. sbi->s_last_block != blocks - 1) {
  981. pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
  982. (unsigned long)sbi->s_last_block,
  983. (unsigned long)blocks - 1);
  984. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  985. }
  986. if (!sbi->s_vat_inode)
  987. return 1;
  988. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  989. map->s_type_specific.s_virtual.s_start_offset = 0;
  990. map->s_type_specific.s_virtual.s_num_entries =
  991. (sbi->s_vat_inode->i_size - 36) >> 2;
  992. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  993. vati = UDF_I(sbi->s_vat_inode);
  994. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  995. pos = udf_block_map(sbi->s_vat_inode, 0);
  996. bh = sb_bread(sb, pos);
  997. if (!bh)
  998. return 1;
  999. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1000. } else {
  1001. vat20 = (struct virtualAllocationTable20 *)
  1002. vati->i_ext.i_data;
  1003. }
  1004. map->s_type_specific.s_virtual.s_start_offset =
  1005. le16_to_cpu(vat20->lengthHeader);
  1006. map->s_type_specific.s_virtual.s_num_entries =
  1007. (sbi->s_vat_inode->i_size -
  1008. map->s_type_specific.s_virtual.
  1009. s_start_offset) >> 2;
  1010. brelse(bh);
  1011. }
  1012. return 0;
  1013. }
  1014. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1015. {
  1016. struct buffer_head *bh;
  1017. struct partitionDesc *p;
  1018. struct udf_part_map *map;
  1019. struct udf_sb_info *sbi = UDF_SB(sb);
  1020. int i, type1_idx;
  1021. uint16_t partitionNumber;
  1022. uint16_t ident;
  1023. int ret = 0;
  1024. bh = udf_read_tagged(sb, block, block, &ident);
  1025. if (!bh)
  1026. return 1;
  1027. if (ident != TAG_IDENT_PD)
  1028. goto out_bh;
  1029. p = (struct partitionDesc *)bh->b_data;
  1030. partitionNumber = le16_to_cpu(p->partitionNumber);
  1031. /* First scan for TYPE1, SPARABLE and METADATA partitions */
  1032. for (i = 0; i < sbi->s_partitions; i++) {
  1033. map = &sbi->s_partmaps[i];
  1034. udf_debug("Searching map: (%d == %d)\n",
  1035. map->s_partition_num, partitionNumber);
  1036. if (map->s_partition_num == partitionNumber &&
  1037. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1038. map->s_partition_type == UDF_SPARABLE_MAP15))
  1039. break;
  1040. }
  1041. if (i >= sbi->s_partitions) {
  1042. udf_debug("Partition (%d) not found in partition map\n",
  1043. partitionNumber);
  1044. goto out_bh;
  1045. }
  1046. ret = udf_fill_partdesc_info(sb, p, i);
  1047. /*
  1048. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1049. * PHYSICAL partitions are already set up
  1050. */
  1051. type1_idx = i;
  1052. for (i = 0; i < sbi->s_partitions; i++) {
  1053. map = &sbi->s_partmaps[i];
  1054. if (map->s_partition_num == partitionNumber &&
  1055. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1056. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1057. map->s_partition_type == UDF_METADATA_MAP25))
  1058. break;
  1059. }
  1060. if (i >= sbi->s_partitions)
  1061. goto out_bh;
  1062. ret = udf_fill_partdesc_info(sb, p, i);
  1063. if (ret)
  1064. goto out_bh;
  1065. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1066. ret = udf_load_metadata_files(sb, i);
  1067. if (ret) {
  1068. udf_err(sb, "error loading MetaData partition map %d\n",
  1069. i);
  1070. goto out_bh;
  1071. }
  1072. } else {
  1073. ret = udf_load_vat(sb, i, type1_idx);
  1074. if (ret)
  1075. goto out_bh;
  1076. /*
  1077. * Mark filesystem read-only if we have a partition with
  1078. * virtual map since we don't handle writing to it (we
  1079. * overwrite blocks instead of relocating them).
  1080. */
  1081. sb->s_flags |= MS_RDONLY;
  1082. pr_notice("Filesystem marked read-only because writing to pseudooverwrite partition is not implemented\n");
  1083. }
  1084. out_bh:
  1085. /* In case loading failed, we handle cleanup in udf_fill_super */
  1086. brelse(bh);
  1087. return ret;
  1088. }
  1089. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1090. struct kernel_lb_addr *fileset)
  1091. {
  1092. struct logicalVolDesc *lvd;
  1093. int i, j, offset;
  1094. uint8_t type;
  1095. struct udf_sb_info *sbi = UDF_SB(sb);
  1096. struct genericPartitionMap *gpm;
  1097. uint16_t ident;
  1098. struct buffer_head *bh;
  1099. int ret = 0;
  1100. bh = udf_read_tagged(sb, block, block, &ident);
  1101. if (!bh)
  1102. return 1;
  1103. BUG_ON(ident != TAG_IDENT_LVD);
  1104. lvd = (struct logicalVolDesc *)bh->b_data;
  1105. i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1106. if (i != 0) {
  1107. ret = i;
  1108. goto out_bh;
  1109. }
  1110. for (i = 0, offset = 0;
  1111. i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
  1112. i++, offset += gpm->partitionMapLength) {
  1113. struct udf_part_map *map = &sbi->s_partmaps[i];
  1114. gpm = (struct genericPartitionMap *)
  1115. &(lvd->partitionMaps[offset]);
  1116. type = gpm->partitionMapType;
  1117. if (type == 1) {
  1118. struct genericPartitionMap1 *gpm1 =
  1119. (struct genericPartitionMap1 *)gpm;
  1120. map->s_partition_type = UDF_TYPE1_MAP15;
  1121. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1122. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1123. map->s_partition_func = NULL;
  1124. } else if (type == 2) {
  1125. struct udfPartitionMap2 *upm2 =
  1126. (struct udfPartitionMap2 *)gpm;
  1127. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1128. strlen(UDF_ID_VIRTUAL))) {
  1129. u16 suf =
  1130. le16_to_cpu(((__le16 *)upm2->partIdent.
  1131. identSuffix)[0]);
  1132. if (suf < 0x0200) {
  1133. map->s_partition_type =
  1134. UDF_VIRTUAL_MAP15;
  1135. map->s_partition_func =
  1136. udf_get_pblock_virt15;
  1137. } else {
  1138. map->s_partition_type =
  1139. UDF_VIRTUAL_MAP20;
  1140. map->s_partition_func =
  1141. udf_get_pblock_virt20;
  1142. }
  1143. } else if (!strncmp(upm2->partIdent.ident,
  1144. UDF_ID_SPARABLE,
  1145. strlen(UDF_ID_SPARABLE))) {
  1146. uint32_t loc;
  1147. struct sparingTable *st;
  1148. struct sparablePartitionMap *spm =
  1149. (struct sparablePartitionMap *)gpm;
  1150. map->s_partition_type = UDF_SPARABLE_MAP15;
  1151. map->s_type_specific.s_sparing.s_packet_len =
  1152. le16_to_cpu(spm->packetLength);
  1153. for (j = 0; j < spm->numSparingTables; j++) {
  1154. struct buffer_head *bh2;
  1155. loc = le32_to_cpu(
  1156. spm->locSparingTable[j]);
  1157. bh2 = udf_read_tagged(sb, loc, loc,
  1158. &ident);
  1159. map->s_type_specific.s_sparing.
  1160. s_spar_map[j] = bh2;
  1161. if (bh2 == NULL)
  1162. continue;
  1163. st = (struct sparingTable *)bh2->b_data;
  1164. if (ident != 0 || strncmp(
  1165. st->sparingIdent.ident,
  1166. UDF_ID_SPARING,
  1167. strlen(UDF_ID_SPARING))) {
  1168. brelse(bh2);
  1169. map->s_type_specific.s_sparing.
  1170. s_spar_map[j] = NULL;
  1171. }
  1172. }
  1173. map->s_partition_func = udf_get_pblock_spar15;
  1174. } else if (!strncmp(upm2->partIdent.ident,
  1175. UDF_ID_METADATA,
  1176. strlen(UDF_ID_METADATA))) {
  1177. struct udf_meta_data *mdata =
  1178. &map->s_type_specific.s_metadata;
  1179. struct metadataPartitionMap *mdm =
  1180. (struct metadataPartitionMap *)
  1181. &(lvd->partitionMaps[offset]);
  1182. udf_debug("Parsing Logical vol part %d "
  1183. "type %d id=%s\n", i, type,
  1184. UDF_ID_METADATA);
  1185. map->s_partition_type = UDF_METADATA_MAP25;
  1186. map->s_partition_func = udf_get_pblock_meta25;
  1187. mdata->s_meta_file_loc =
  1188. le32_to_cpu(mdm->metadataFileLoc);
  1189. mdata->s_mirror_file_loc =
  1190. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1191. mdata->s_bitmap_file_loc =
  1192. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1193. mdata->s_alloc_unit_size =
  1194. le32_to_cpu(mdm->allocUnitSize);
  1195. mdata->s_align_unit_size =
  1196. le16_to_cpu(mdm->alignUnitSize);
  1197. mdata->s_dup_md_flag =
  1198. mdm->flags & 0x01;
  1199. udf_debug("Metadata Ident suffix=0x%x\n",
  1200. (le16_to_cpu(
  1201. ((__le16 *)
  1202. mdm->partIdent.identSuffix)[0])));
  1203. udf_debug("Metadata part num=%d\n",
  1204. le16_to_cpu(mdm->partitionNum));
  1205. udf_debug("Metadata part alloc unit size=%d\n",
  1206. le32_to_cpu(mdm->allocUnitSize));
  1207. udf_debug("Metadata file loc=%d\n",
  1208. le32_to_cpu(mdm->metadataFileLoc));
  1209. udf_debug("Mirror file loc=%d\n",
  1210. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1211. udf_debug("Bitmap file loc=%d\n",
  1212. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1213. udf_debug("Duplicate Flag: %d %d\n",
  1214. mdata->s_dup_md_flag, mdm->flags);
  1215. } else {
  1216. udf_debug("Unknown ident: %s\n",
  1217. upm2->partIdent.ident);
  1218. continue;
  1219. }
  1220. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1221. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1222. }
  1223. udf_debug("Partition (%d:%d) type %d on volume %d\n",
  1224. i, map->s_partition_num, type,
  1225. map->s_volumeseqnum);
  1226. }
  1227. if (fileset) {
  1228. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1229. *fileset = lelb_to_cpu(la->extLocation);
  1230. udf_debug("FileSet found in LogicalVolDesc at block=%d, "
  1231. "partition=%d\n", fileset->logicalBlockNum,
  1232. fileset->partitionReferenceNum);
  1233. }
  1234. if (lvd->integritySeqExt.extLength)
  1235. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1236. out_bh:
  1237. brelse(bh);
  1238. return ret;
  1239. }
  1240. /*
  1241. * udf_load_logicalvolint
  1242. *
  1243. */
  1244. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1245. {
  1246. struct buffer_head *bh = NULL;
  1247. uint16_t ident;
  1248. struct udf_sb_info *sbi = UDF_SB(sb);
  1249. struct logicalVolIntegrityDesc *lvid;
  1250. while (loc.extLength > 0 &&
  1251. (bh = udf_read_tagged(sb, loc.extLocation,
  1252. loc.extLocation, &ident)) &&
  1253. ident == TAG_IDENT_LVID) {
  1254. sbi->s_lvid_bh = bh;
  1255. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1256. if (lvid->nextIntegrityExt.extLength)
  1257. udf_load_logicalvolint(sb,
  1258. leea_to_cpu(lvid->nextIntegrityExt));
  1259. if (sbi->s_lvid_bh != bh)
  1260. brelse(bh);
  1261. loc.extLength -= sb->s_blocksize;
  1262. loc.extLocation++;
  1263. }
  1264. if (sbi->s_lvid_bh != bh)
  1265. brelse(bh);
  1266. }
  1267. /*
  1268. * udf_process_sequence
  1269. *
  1270. * PURPOSE
  1271. * Process a main/reserve volume descriptor sequence.
  1272. *
  1273. * PRE-CONDITIONS
  1274. * sb Pointer to _locked_ superblock.
  1275. * block First block of first extent of the sequence.
  1276. * lastblock Lastblock of first extent of the sequence.
  1277. *
  1278. * HISTORY
  1279. * July 1, 1997 - Andrew E. Mileski
  1280. * Written, tested, and released.
  1281. */
  1282. static noinline int udf_process_sequence(struct super_block *sb, long block,
  1283. long lastblock, struct kernel_lb_addr *fileset)
  1284. {
  1285. struct buffer_head *bh = NULL;
  1286. struct udf_vds_record vds[VDS_POS_LENGTH];
  1287. struct udf_vds_record *curr;
  1288. struct generic_desc *gd;
  1289. struct volDescPtr *vdp;
  1290. int done = 0;
  1291. uint32_t vdsn;
  1292. uint16_t ident;
  1293. long next_s = 0, next_e = 0;
  1294. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1295. /*
  1296. * Read the main descriptor sequence and find which descriptors
  1297. * are in it.
  1298. */
  1299. for (; (!done && block <= lastblock); block++) {
  1300. bh = udf_read_tagged(sb, block, block, &ident);
  1301. if (!bh) {
  1302. udf_err(sb,
  1303. "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
  1304. (unsigned long long)block);
  1305. return 1;
  1306. }
  1307. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1308. gd = (struct generic_desc *)bh->b_data;
  1309. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1310. switch (ident) {
  1311. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1312. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1313. if (vdsn >= curr->volDescSeqNum) {
  1314. curr->volDescSeqNum = vdsn;
  1315. curr->block = block;
  1316. }
  1317. break;
  1318. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1319. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1320. if (vdsn >= curr->volDescSeqNum) {
  1321. curr->volDescSeqNum = vdsn;
  1322. curr->block = block;
  1323. vdp = (struct volDescPtr *)bh->b_data;
  1324. next_s = le32_to_cpu(
  1325. vdp->nextVolDescSeqExt.extLocation);
  1326. next_e = le32_to_cpu(
  1327. vdp->nextVolDescSeqExt.extLength);
  1328. next_e = next_e >> sb->s_blocksize_bits;
  1329. next_e += next_s;
  1330. }
  1331. break;
  1332. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1333. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1334. if (vdsn >= curr->volDescSeqNum) {
  1335. curr->volDescSeqNum = vdsn;
  1336. curr->block = block;
  1337. }
  1338. break;
  1339. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1340. curr = &vds[VDS_POS_PARTITION_DESC];
  1341. if (!curr->block)
  1342. curr->block = block;
  1343. break;
  1344. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1345. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1346. if (vdsn >= curr->volDescSeqNum) {
  1347. curr->volDescSeqNum = vdsn;
  1348. curr->block = block;
  1349. }
  1350. break;
  1351. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1352. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1353. if (vdsn >= curr->volDescSeqNum) {
  1354. curr->volDescSeqNum = vdsn;
  1355. curr->block = block;
  1356. }
  1357. break;
  1358. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1359. vds[VDS_POS_TERMINATING_DESC].block = block;
  1360. if (next_e) {
  1361. block = next_s;
  1362. lastblock = next_e;
  1363. next_s = next_e = 0;
  1364. } else
  1365. done = 1;
  1366. break;
  1367. }
  1368. brelse(bh);
  1369. }
  1370. /*
  1371. * Now read interesting descriptors again and process them
  1372. * in a suitable order
  1373. */
  1374. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1375. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1376. return 1;
  1377. }
  1378. if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
  1379. return 1;
  1380. if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
  1381. vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
  1382. return 1;
  1383. if (vds[VDS_POS_PARTITION_DESC].block) {
  1384. /*
  1385. * We rescan the whole descriptor sequence to find
  1386. * partition descriptor blocks and process them.
  1387. */
  1388. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1389. block < vds[VDS_POS_TERMINATING_DESC].block;
  1390. block++)
  1391. if (udf_load_partdesc(sb, block))
  1392. return 1;
  1393. }
  1394. return 0;
  1395. }
  1396. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1397. struct kernel_lb_addr *fileset)
  1398. {
  1399. struct anchorVolDescPtr *anchor;
  1400. long main_s, main_e, reserve_s, reserve_e;
  1401. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1402. /* Locate the main sequence */
  1403. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1404. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1405. main_e = main_e >> sb->s_blocksize_bits;
  1406. main_e += main_s;
  1407. /* Locate the reserve sequence */
  1408. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1409. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1410. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1411. reserve_e += reserve_s;
  1412. /* Process the main & reserve sequences */
  1413. /* responsible for finding the PartitionDesc(s) */
  1414. if (!udf_process_sequence(sb, main_s, main_e, fileset))
  1415. return 1;
  1416. return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1417. }
  1418. /*
  1419. * Check whether there is an anchor block in the given block and
  1420. * load Volume Descriptor Sequence if so.
  1421. */
  1422. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1423. struct kernel_lb_addr *fileset)
  1424. {
  1425. struct buffer_head *bh;
  1426. uint16_t ident;
  1427. int ret;
  1428. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1429. udf_fixed_to_variable(block) >=
  1430. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1431. return 0;
  1432. bh = udf_read_tagged(sb, block, block, &ident);
  1433. if (!bh)
  1434. return 0;
  1435. if (ident != TAG_IDENT_AVDP) {
  1436. brelse(bh);
  1437. return 0;
  1438. }
  1439. ret = udf_load_sequence(sb, bh, fileset);
  1440. brelse(bh);
  1441. return ret;
  1442. }
  1443. /* Search for an anchor volume descriptor pointer */
  1444. static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
  1445. struct kernel_lb_addr *fileset)
  1446. {
  1447. sector_t last[6];
  1448. int i;
  1449. struct udf_sb_info *sbi = UDF_SB(sb);
  1450. int last_count = 0;
  1451. /* First try user provided anchor */
  1452. if (sbi->s_anchor) {
  1453. if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
  1454. return lastblock;
  1455. }
  1456. /*
  1457. * according to spec, anchor is in either:
  1458. * block 256
  1459. * lastblock-256
  1460. * lastblock
  1461. * however, if the disc isn't closed, it could be 512.
  1462. */
  1463. if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
  1464. return lastblock;
  1465. /*
  1466. * The trouble is which block is the last one. Drives often misreport
  1467. * this so we try various possibilities.
  1468. */
  1469. last[last_count++] = lastblock;
  1470. if (lastblock >= 1)
  1471. last[last_count++] = lastblock - 1;
  1472. last[last_count++] = lastblock + 1;
  1473. if (lastblock >= 2)
  1474. last[last_count++] = lastblock - 2;
  1475. if (lastblock >= 150)
  1476. last[last_count++] = lastblock - 150;
  1477. if (lastblock >= 152)
  1478. last[last_count++] = lastblock - 152;
  1479. for (i = 0; i < last_count; i++) {
  1480. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1481. sb->s_blocksize_bits)
  1482. continue;
  1483. if (udf_check_anchor_block(sb, last[i], fileset))
  1484. return last[i];
  1485. if (last[i] < 256)
  1486. continue;
  1487. if (udf_check_anchor_block(sb, last[i] - 256, fileset))
  1488. return last[i];
  1489. }
  1490. /* Finally try block 512 in case media is open */
  1491. if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
  1492. return last[0];
  1493. return 0;
  1494. }
  1495. /*
  1496. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1497. * area specified by it. The function expects sbi->s_lastblock to be the last
  1498. * block on the media.
  1499. *
  1500. * Return 1 if ok, 0 if not found.
  1501. *
  1502. */
  1503. static int udf_find_anchor(struct super_block *sb,
  1504. struct kernel_lb_addr *fileset)
  1505. {
  1506. sector_t lastblock;
  1507. struct udf_sb_info *sbi = UDF_SB(sb);
  1508. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1509. if (lastblock)
  1510. goto out;
  1511. /* No anchor found? Try VARCONV conversion of block numbers */
  1512. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1513. /* Firstly, we try to not convert number of the last block */
  1514. lastblock = udf_scan_anchors(sb,
  1515. udf_variable_to_fixed(sbi->s_last_block),
  1516. fileset);
  1517. if (lastblock)
  1518. goto out;
  1519. /* Secondly, we try with converted number of the last block */
  1520. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1521. if (!lastblock) {
  1522. /* VARCONV didn't help. Clear it. */
  1523. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1524. return 0;
  1525. }
  1526. out:
  1527. sbi->s_last_block = lastblock;
  1528. return 1;
  1529. }
  1530. /*
  1531. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1532. * Descriptor Sequence
  1533. */
  1534. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1535. int silent, struct kernel_lb_addr *fileset)
  1536. {
  1537. struct udf_sb_info *sbi = UDF_SB(sb);
  1538. loff_t nsr_off;
  1539. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1540. if (!silent)
  1541. udf_warn(sb, "Bad block size\n");
  1542. return 0;
  1543. }
  1544. sbi->s_last_block = uopt->lastblock;
  1545. if (!uopt->novrs) {
  1546. /* Check that it is NSR02 compliant */
  1547. nsr_off = udf_check_vsd(sb);
  1548. if (!nsr_off) {
  1549. if (!silent)
  1550. udf_warn(sb, "No VRS found\n");
  1551. return 0;
  1552. }
  1553. if (nsr_off == -1)
  1554. udf_debug("Failed to read byte 32768. Assuming open "
  1555. "disc. Skipping validity check\n");
  1556. if (!sbi->s_last_block)
  1557. sbi->s_last_block = udf_get_last_block(sb);
  1558. } else {
  1559. udf_debug("Validity check skipped because of novrs option\n");
  1560. }
  1561. /* Look for anchor block and load Volume Descriptor Sequence */
  1562. sbi->s_anchor = uopt->anchor;
  1563. if (!udf_find_anchor(sb, fileset)) {
  1564. if (!silent)
  1565. udf_warn(sb, "No anchor found\n");
  1566. return 0;
  1567. }
  1568. return 1;
  1569. }
  1570. static void udf_open_lvid(struct super_block *sb)
  1571. {
  1572. struct udf_sb_info *sbi = UDF_SB(sb);
  1573. struct buffer_head *bh = sbi->s_lvid_bh;
  1574. struct logicalVolIntegrityDesc *lvid;
  1575. struct logicalVolIntegrityDescImpUse *lvidiu;
  1576. if (!bh)
  1577. return;
  1578. mutex_lock(&sbi->s_alloc_mutex);
  1579. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1580. lvidiu = udf_sb_lvidiu(sbi);
  1581. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1582. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1583. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1584. CURRENT_TIME);
  1585. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1586. lvid->descTag.descCRC = cpu_to_le16(
  1587. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1588. le16_to_cpu(lvid->descTag.descCRCLength)));
  1589. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1590. mark_buffer_dirty(bh);
  1591. sbi->s_lvid_dirty = 0;
  1592. mutex_unlock(&sbi->s_alloc_mutex);
  1593. }
  1594. static void udf_close_lvid(struct super_block *sb)
  1595. {
  1596. struct udf_sb_info *sbi = UDF_SB(sb);
  1597. struct buffer_head *bh = sbi->s_lvid_bh;
  1598. struct logicalVolIntegrityDesc *lvid;
  1599. struct logicalVolIntegrityDescImpUse *lvidiu;
  1600. if (!bh)
  1601. return;
  1602. mutex_lock(&sbi->s_alloc_mutex);
  1603. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1604. lvidiu = udf_sb_lvidiu(sbi);
  1605. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1606. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1607. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1608. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1609. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1610. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1611. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1612. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1613. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1614. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1615. lvid->descTag.descCRC = cpu_to_le16(
  1616. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1617. le16_to_cpu(lvid->descTag.descCRCLength)));
  1618. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1619. mark_buffer_dirty(bh);
  1620. sbi->s_lvid_dirty = 0;
  1621. mutex_unlock(&sbi->s_alloc_mutex);
  1622. }
  1623. u64 lvid_get_unique_id(struct super_block *sb)
  1624. {
  1625. struct buffer_head *bh;
  1626. struct udf_sb_info *sbi = UDF_SB(sb);
  1627. struct logicalVolIntegrityDesc *lvid;
  1628. struct logicalVolHeaderDesc *lvhd;
  1629. u64 uniqueID;
  1630. u64 ret;
  1631. bh = sbi->s_lvid_bh;
  1632. if (!bh)
  1633. return 0;
  1634. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1635. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1636. mutex_lock(&sbi->s_alloc_mutex);
  1637. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1638. if (!(++uniqueID & 0xFFFFFFFF))
  1639. uniqueID += 16;
  1640. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1641. mutex_unlock(&sbi->s_alloc_mutex);
  1642. mark_buffer_dirty(bh);
  1643. return ret;
  1644. }
  1645. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  1646. {
  1647. int i;
  1648. int nr_groups = bitmap->s_nr_groups;
  1649. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  1650. nr_groups);
  1651. for (i = 0; i < nr_groups; i++)
  1652. if (bitmap->s_block_bitmap[i])
  1653. brelse(bitmap->s_block_bitmap[i]);
  1654. if (size <= PAGE_SIZE)
  1655. kfree(bitmap);
  1656. else
  1657. vfree(bitmap);
  1658. }
  1659. static void udf_free_partition(struct udf_part_map *map)
  1660. {
  1661. int i;
  1662. struct udf_meta_data *mdata;
  1663. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  1664. iput(map->s_uspace.s_table);
  1665. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  1666. iput(map->s_fspace.s_table);
  1667. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  1668. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  1669. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  1670. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  1671. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  1672. for (i = 0; i < 4; i++)
  1673. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  1674. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  1675. mdata = &map->s_type_specific.s_metadata;
  1676. iput(mdata->s_metadata_fe);
  1677. mdata->s_metadata_fe = NULL;
  1678. iput(mdata->s_mirror_fe);
  1679. mdata->s_mirror_fe = NULL;
  1680. iput(mdata->s_bitmap_fe);
  1681. mdata->s_bitmap_fe = NULL;
  1682. }
  1683. }
  1684. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1685. {
  1686. int i;
  1687. int ret;
  1688. struct inode *inode = NULL;
  1689. struct udf_options uopt;
  1690. struct kernel_lb_addr rootdir, fileset;
  1691. struct udf_sb_info *sbi;
  1692. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1693. uopt.uid = -1;
  1694. uopt.gid = -1;
  1695. uopt.umask = 0;
  1696. uopt.fmode = UDF_INVALID_MODE;
  1697. uopt.dmode = UDF_INVALID_MODE;
  1698. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1699. if (!sbi)
  1700. return -ENOMEM;
  1701. sb->s_fs_info = sbi;
  1702. mutex_init(&sbi->s_alloc_mutex);
  1703. if (!udf_parse_options((char *)options, &uopt, false))
  1704. goto error_out;
  1705. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1706. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1707. udf_err(sb, "utf8 cannot be combined with iocharset\n");
  1708. goto error_out;
  1709. }
  1710. #ifdef CONFIG_UDF_NLS
  1711. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1712. uopt.nls_map = load_nls_default();
  1713. if (!uopt.nls_map)
  1714. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1715. else
  1716. udf_debug("Using default NLS map\n");
  1717. }
  1718. #endif
  1719. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1720. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1721. fileset.logicalBlockNum = 0xFFFFFFFF;
  1722. fileset.partitionReferenceNum = 0xFFFF;
  1723. sbi->s_flags = uopt.flags;
  1724. sbi->s_uid = uopt.uid;
  1725. sbi->s_gid = uopt.gid;
  1726. sbi->s_umask = uopt.umask;
  1727. sbi->s_fmode = uopt.fmode;
  1728. sbi->s_dmode = uopt.dmode;
  1729. sbi->s_nls_map = uopt.nls_map;
  1730. rwlock_init(&sbi->s_cred_lock);
  1731. if (uopt.session == 0xFFFFFFFF)
  1732. sbi->s_session = udf_get_last_session(sb);
  1733. else
  1734. sbi->s_session = uopt.session;
  1735. udf_debug("Multi-session=%d\n", sbi->s_session);
  1736. /* Fill in the rest of the superblock */
  1737. sb->s_op = &udf_sb_ops;
  1738. sb->s_export_op = &udf_export_ops;
  1739. sb->s_dirt = 0;
  1740. sb->s_magic = UDF_SUPER_MAGIC;
  1741. sb->s_time_gran = 1000;
  1742. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1743. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1744. } else {
  1745. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1746. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1747. if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1748. if (!silent)
  1749. pr_notice("Rescanning with blocksize %d\n",
  1750. UDF_DEFAULT_BLOCKSIZE);
  1751. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1752. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1753. }
  1754. }
  1755. if (!ret) {
  1756. udf_warn(sb, "No partition found (1)\n");
  1757. goto error_out;
  1758. }
  1759. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1760. if (sbi->s_lvid_bh) {
  1761. struct logicalVolIntegrityDescImpUse *lvidiu =
  1762. udf_sb_lvidiu(sbi);
  1763. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1764. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1765. /* uint16_t maxUDFWriteRev =
  1766. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1767. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1768. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1769. le16_to_cpu(lvidiu->minUDFReadRev),
  1770. UDF_MAX_READ_VERSION);
  1771. goto error_out;
  1772. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1773. sb->s_flags |= MS_RDONLY;
  1774. sbi->s_udfrev = minUDFWriteRev;
  1775. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1776. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1777. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1778. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1779. }
  1780. if (!sbi->s_partitions) {
  1781. udf_warn(sb, "No partition found (2)\n");
  1782. goto error_out;
  1783. }
  1784. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1785. UDF_PART_FLAG_READ_ONLY) {
  1786. pr_notice("Partition marked readonly; forcing readonly mount\n");
  1787. sb->s_flags |= MS_RDONLY;
  1788. }
  1789. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1790. udf_warn(sb, "No fileset found\n");
  1791. goto error_out;
  1792. }
  1793. if (!silent) {
  1794. struct timestamp ts;
  1795. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1796. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1797. sbi->s_volume_ident,
  1798. le16_to_cpu(ts.year), ts.month, ts.day,
  1799. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1800. }
  1801. if (!(sb->s_flags & MS_RDONLY))
  1802. udf_open_lvid(sb);
  1803. /* Assign the root inode */
  1804. /* assign inodes by physical block number */
  1805. /* perhaps it's not extensible enough, but for now ... */
  1806. inode = udf_iget(sb, &rootdir);
  1807. if (!inode) {
  1808. udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
  1809. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1810. goto error_out;
  1811. }
  1812. /* Allocate a dentry for the root inode */
  1813. sb->s_root = d_alloc_root(inode);
  1814. if (!sb->s_root) {
  1815. udf_err(sb, "Couldn't allocate root dentry\n");
  1816. iput(inode);
  1817. goto error_out;
  1818. }
  1819. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1820. return 0;
  1821. error_out:
  1822. if (sbi->s_vat_inode)
  1823. iput(sbi->s_vat_inode);
  1824. if (sbi->s_partitions)
  1825. for (i = 0; i < sbi->s_partitions; i++)
  1826. udf_free_partition(&sbi->s_partmaps[i]);
  1827. #ifdef CONFIG_UDF_NLS
  1828. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1829. unload_nls(sbi->s_nls_map);
  1830. #endif
  1831. if (!(sb->s_flags & MS_RDONLY))
  1832. udf_close_lvid(sb);
  1833. brelse(sbi->s_lvid_bh);
  1834. kfree(sbi->s_partmaps);
  1835. kfree(sbi);
  1836. sb->s_fs_info = NULL;
  1837. return -EINVAL;
  1838. }
  1839. void _udf_err(struct super_block *sb, const char *function,
  1840. const char *fmt, ...)
  1841. {
  1842. struct va_format vaf;
  1843. va_list args;
  1844. /* mark sb error */
  1845. if (!(sb->s_flags & MS_RDONLY))
  1846. sb->s_dirt = 1;
  1847. va_start(args, fmt);
  1848. vaf.fmt = fmt;
  1849. vaf.va = &args;
  1850. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  1851. va_end(args);
  1852. }
  1853. void _udf_warn(struct super_block *sb, const char *function,
  1854. const char *fmt, ...)
  1855. {
  1856. struct va_format vaf;
  1857. va_list args;
  1858. va_start(args, fmt);
  1859. vaf.fmt = fmt;
  1860. vaf.va = &args;
  1861. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  1862. va_end(args);
  1863. }
  1864. static void udf_put_super(struct super_block *sb)
  1865. {
  1866. int i;
  1867. struct udf_sb_info *sbi;
  1868. sbi = UDF_SB(sb);
  1869. if (sbi->s_vat_inode)
  1870. iput(sbi->s_vat_inode);
  1871. if (sbi->s_partitions)
  1872. for (i = 0; i < sbi->s_partitions; i++)
  1873. udf_free_partition(&sbi->s_partmaps[i]);
  1874. #ifdef CONFIG_UDF_NLS
  1875. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1876. unload_nls(sbi->s_nls_map);
  1877. #endif
  1878. if (!(sb->s_flags & MS_RDONLY))
  1879. udf_close_lvid(sb);
  1880. brelse(sbi->s_lvid_bh);
  1881. kfree(sbi->s_partmaps);
  1882. kfree(sb->s_fs_info);
  1883. sb->s_fs_info = NULL;
  1884. }
  1885. static int udf_sync_fs(struct super_block *sb, int wait)
  1886. {
  1887. struct udf_sb_info *sbi = UDF_SB(sb);
  1888. mutex_lock(&sbi->s_alloc_mutex);
  1889. if (sbi->s_lvid_dirty) {
  1890. /*
  1891. * Blockdevice will be synced later so we don't have to submit
  1892. * the buffer for IO
  1893. */
  1894. mark_buffer_dirty(sbi->s_lvid_bh);
  1895. sb->s_dirt = 0;
  1896. sbi->s_lvid_dirty = 0;
  1897. }
  1898. mutex_unlock(&sbi->s_alloc_mutex);
  1899. return 0;
  1900. }
  1901. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1902. {
  1903. struct super_block *sb = dentry->d_sb;
  1904. struct udf_sb_info *sbi = UDF_SB(sb);
  1905. struct logicalVolIntegrityDescImpUse *lvidiu;
  1906. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1907. if (sbi->s_lvid_bh != NULL)
  1908. lvidiu = udf_sb_lvidiu(sbi);
  1909. else
  1910. lvidiu = NULL;
  1911. buf->f_type = UDF_SUPER_MAGIC;
  1912. buf->f_bsize = sb->s_blocksize;
  1913. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1914. buf->f_bfree = udf_count_free(sb);
  1915. buf->f_bavail = buf->f_bfree;
  1916. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1917. le32_to_cpu(lvidiu->numDirs)) : 0)
  1918. + buf->f_bfree;
  1919. buf->f_ffree = buf->f_bfree;
  1920. buf->f_namelen = UDF_NAME_LEN - 2;
  1921. buf->f_fsid.val[0] = (u32)id;
  1922. buf->f_fsid.val[1] = (u32)(id >> 32);
  1923. return 0;
  1924. }
  1925. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1926. struct udf_bitmap *bitmap)
  1927. {
  1928. struct buffer_head *bh = NULL;
  1929. unsigned int accum = 0;
  1930. int index;
  1931. int block = 0, newblock;
  1932. struct kernel_lb_addr loc;
  1933. uint32_t bytes;
  1934. uint8_t *ptr;
  1935. uint16_t ident;
  1936. struct spaceBitmapDesc *bm;
  1937. loc.logicalBlockNum = bitmap->s_extPosition;
  1938. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1939. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  1940. if (!bh) {
  1941. udf_err(sb, "udf_count_free failed\n");
  1942. goto out;
  1943. } else if (ident != TAG_IDENT_SBD) {
  1944. brelse(bh);
  1945. udf_err(sb, "udf_count_free failed\n");
  1946. goto out;
  1947. }
  1948. bm = (struct spaceBitmapDesc *)bh->b_data;
  1949. bytes = le32_to_cpu(bm->numOfBytes);
  1950. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1951. ptr = (uint8_t *)bh->b_data;
  1952. while (bytes > 0) {
  1953. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1954. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1955. cur_bytes * 8);
  1956. bytes -= cur_bytes;
  1957. if (bytes) {
  1958. brelse(bh);
  1959. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  1960. bh = udf_tread(sb, newblock);
  1961. if (!bh) {
  1962. udf_debug("read failed\n");
  1963. goto out;
  1964. }
  1965. index = 0;
  1966. ptr = (uint8_t *)bh->b_data;
  1967. }
  1968. }
  1969. brelse(bh);
  1970. out:
  1971. return accum;
  1972. }
  1973. static unsigned int udf_count_free_table(struct super_block *sb,
  1974. struct inode *table)
  1975. {
  1976. unsigned int accum = 0;
  1977. uint32_t elen;
  1978. struct kernel_lb_addr eloc;
  1979. int8_t etype;
  1980. struct extent_position epos;
  1981. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  1982. epos.block = UDF_I(table)->i_location;
  1983. epos.offset = sizeof(struct unallocSpaceEntry);
  1984. epos.bh = NULL;
  1985. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  1986. accum += (elen >> table->i_sb->s_blocksize_bits);
  1987. brelse(epos.bh);
  1988. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  1989. return accum;
  1990. }
  1991. static unsigned int udf_count_free(struct super_block *sb)
  1992. {
  1993. unsigned int accum = 0;
  1994. struct udf_sb_info *sbi;
  1995. struct udf_part_map *map;
  1996. sbi = UDF_SB(sb);
  1997. if (sbi->s_lvid_bh) {
  1998. struct logicalVolIntegrityDesc *lvid =
  1999. (struct logicalVolIntegrityDesc *)
  2000. sbi->s_lvid_bh->b_data;
  2001. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2002. accum = le32_to_cpu(
  2003. lvid->freeSpaceTable[sbi->s_partition]);
  2004. if (accum == 0xFFFFFFFF)
  2005. accum = 0;
  2006. }
  2007. }
  2008. if (accum)
  2009. return accum;
  2010. map = &sbi->s_partmaps[sbi->s_partition];
  2011. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2012. accum += udf_count_free_bitmap(sb,
  2013. map->s_uspace.s_bitmap);
  2014. }
  2015. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2016. accum += udf_count_free_bitmap(sb,
  2017. map->s_fspace.s_bitmap);
  2018. }
  2019. if (accum)
  2020. return accum;
  2021. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2022. accum += udf_count_free_table(sb,
  2023. map->s_uspace.s_table);
  2024. }
  2025. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2026. accum += udf_count_free_table(sb,
  2027. map->s_fspace.s_table);
  2028. }
  2029. return accum;
  2030. }