super.c 61 KB

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