super.c 61 KB

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