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. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
  88. {
  89. struct logicalVolIntegrityDesc *lvid =
  90. (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
  91. __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
  92. __u32 offset = number_of_partitions * 2 *
  93. sizeof(uint32_t)/sizeof(uint8_t);
  94. return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
  95. }
  96. /* UDF filesystem type */
  97. static struct dentry *udf_mount(struct file_system_type *fs_type,
  98. int flags, const char *dev_name, void *data)
  99. {
  100. return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
  101. }
  102. static struct file_system_type udf_fstype = {
  103. .owner = THIS_MODULE,
  104. .name = "udf",
  105. .mount = udf_mount,
  106. .kill_sb = kill_block_super,
  107. .fs_flags = FS_REQUIRES_DEV,
  108. };
  109. static struct kmem_cache *udf_inode_cachep;
  110. static struct inode *udf_alloc_inode(struct super_block *sb)
  111. {
  112. struct udf_inode_info *ei;
  113. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  114. if (!ei)
  115. return NULL;
  116. ei->i_unique = 0;
  117. ei->i_lenExtents = 0;
  118. ei->i_next_alloc_block = 0;
  119. ei->i_next_alloc_goal = 0;
  120. ei->i_strat4096 = 0;
  121. init_rwsem(&ei->i_data_sem);
  122. return &ei->vfs_inode;
  123. }
  124. static void udf_i_callback(struct rcu_head *head)
  125. {
  126. struct inode *inode = container_of(head, struct inode, i_rcu);
  127. INIT_LIST_HEAD(&inode->i_dentry);
  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. mode_t umask;
  179. gid_t gid;
  180. uid_t uid;
  181. mode_t fmode;
  182. mode_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 vfsmount *mnt)
  222. {
  223. struct super_block *sb = mnt->mnt_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=%o", sbi->s_umask);
  251. if (sbi->s_fmode != UDF_INVALID_MODE)
  252. seq_printf(seq, ",mode=%o", sbi->s_fmode);
  253. if (sbi->s_dmode != UDF_INVALID_MODE)
  254. seq_printf(seq, ",dmode=%o", 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. printk(KERN_ERR "udf: bad mount option \"%s\" "
  507. "or missing value\n", p);
  508. return 0;
  509. }
  510. }
  511. return 1;
  512. }
  513. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  514. {
  515. struct udf_options uopt;
  516. struct udf_sb_info *sbi = UDF_SB(sb);
  517. int error = 0;
  518. uopt.flags = sbi->s_flags;
  519. uopt.uid = sbi->s_uid;
  520. uopt.gid = sbi->s_gid;
  521. uopt.umask = sbi->s_umask;
  522. uopt.fmode = sbi->s_fmode;
  523. uopt.dmode = sbi->s_dmode;
  524. if (!udf_parse_options(options, &uopt, true))
  525. return -EINVAL;
  526. write_lock(&sbi->s_cred_lock);
  527. sbi->s_flags = uopt.flags;
  528. sbi->s_uid = uopt.uid;
  529. sbi->s_gid = uopt.gid;
  530. sbi->s_umask = uopt.umask;
  531. sbi->s_fmode = uopt.fmode;
  532. sbi->s_dmode = uopt.dmode;
  533. write_unlock(&sbi->s_cred_lock);
  534. if (sbi->s_lvid_bh) {
  535. int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
  536. if (write_rev > UDF_MAX_WRITE_VERSION)
  537. *flags |= MS_RDONLY;
  538. }
  539. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  540. goto out_unlock;
  541. if (*flags & MS_RDONLY)
  542. udf_close_lvid(sb);
  543. else
  544. udf_open_lvid(sb);
  545. out_unlock:
  546. return error;
  547. }
  548. /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
  549. /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
  550. static loff_t udf_check_vsd(struct super_block *sb)
  551. {
  552. struct volStructDesc *vsd = NULL;
  553. loff_t sector = 32768;
  554. int sectorsize;
  555. struct buffer_head *bh = NULL;
  556. int nsr02 = 0;
  557. int nsr03 = 0;
  558. struct udf_sb_info *sbi;
  559. sbi = UDF_SB(sb);
  560. if (sb->s_blocksize < sizeof(struct volStructDesc))
  561. sectorsize = sizeof(struct volStructDesc);
  562. else
  563. sectorsize = sb->s_blocksize;
  564. sector += (sbi->s_session << sb->s_blocksize_bits);
  565. udf_debug("Starting at sector %u (%ld byte sectors)\n",
  566. (unsigned int)(sector >> sb->s_blocksize_bits),
  567. sb->s_blocksize);
  568. /* Process the sequence (if applicable) */
  569. for (; !nsr02 && !nsr03; sector += sectorsize) {
  570. /* Read a block */
  571. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  572. if (!bh)
  573. break;
  574. /* Look for ISO descriptors */
  575. vsd = (struct volStructDesc *)(bh->b_data +
  576. (sector & (sb->s_blocksize - 1)));
  577. if (vsd->stdIdent[0] == 0) {
  578. brelse(bh);
  579. break;
  580. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
  581. VSD_STD_ID_LEN)) {
  582. switch (vsd->structType) {
  583. case 0:
  584. udf_debug("ISO9660 Boot Record found\n");
  585. break;
  586. case 1:
  587. udf_debug("ISO9660 Primary Volume Descriptor "
  588. "found\n");
  589. break;
  590. case 2:
  591. udf_debug("ISO9660 Supplementary Volume "
  592. "Descriptor found\n");
  593. break;
  594. case 3:
  595. udf_debug("ISO9660 Volume Partition Descriptor "
  596. "found\n");
  597. break;
  598. case 255:
  599. udf_debug("ISO9660 Volume Descriptor Set "
  600. "Terminator found\n");
  601. break;
  602. default:
  603. udf_debug("ISO9660 VRS (%u) found\n",
  604. vsd->structType);
  605. break;
  606. }
  607. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  608. VSD_STD_ID_LEN))
  609. ; /* nothing */
  610. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  611. VSD_STD_ID_LEN)) {
  612. brelse(bh);
  613. break;
  614. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  615. VSD_STD_ID_LEN))
  616. nsr02 = sector;
  617. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  618. VSD_STD_ID_LEN))
  619. nsr03 = sector;
  620. brelse(bh);
  621. }
  622. if (nsr03)
  623. return nsr03;
  624. else if (nsr02)
  625. return nsr02;
  626. else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
  627. return -1;
  628. else
  629. return 0;
  630. }
  631. static int udf_find_fileset(struct super_block *sb,
  632. struct kernel_lb_addr *fileset,
  633. struct kernel_lb_addr *root)
  634. {
  635. struct buffer_head *bh = NULL;
  636. long lastblock;
  637. uint16_t ident;
  638. struct udf_sb_info *sbi;
  639. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  640. fileset->partitionReferenceNum != 0xFFFF) {
  641. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  642. if (!bh) {
  643. return 1;
  644. } else if (ident != TAG_IDENT_FSD) {
  645. brelse(bh);
  646. return 1;
  647. }
  648. }
  649. sbi = UDF_SB(sb);
  650. if (!bh) {
  651. /* Search backwards through the partitions */
  652. struct kernel_lb_addr newfileset;
  653. /* --> cvg: FIXME - is it reasonable? */
  654. return 1;
  655. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  656. (newfileset.partitionReferenceNum != 0xFFFF &&
  657. fileset->logicalBlockNum == 0xFFFFFFFF &&
  658. fileset->partitionReferenceNum == 0xFFFF);
  659. newfileset.partitionReferenceNum--) {
  660. lastblock = sbi->s_partmaps
  661. [newfileset.partitionReferenceNum]
  662. .s_partition_len;
  663. newfileset.logicalBlockNum = 0;
  664. do {
  665. bh = udf_read_ptagged(sb, &newfileset, 0,
  666. &ident);
  667. if (!bh) {
  668. newfileset.logicalBlockNum++;
  669. continue;
  670. }
  671. switch (ident) {
  672. case TAG_IDENT_SBD:
  673. {
  674. struct spaceBitmapDesc *sp;
  675. sp = (struct spaceBitmapDesc *)
  676. bh->b_data;
  677. newfileset.logicalBlockNum += 1 +
  678. ((le32_to_cpu(sp->numOfBytes) +
  679. sizeof(struct spaceBitmapDesc)
  680. - 1) >> sb->s_blocksize_bits);
  681. brelse(bh);
  682. break;
  683. }
  684. case TAG_IDENT_FSD:
  685. *fileset = newfileset;
  686. break;
  687. default:
  688. newfileset.logicalBlockNum++;
  689. brelse(bh);
  690. bh = NULL;
  691. break;
  692. }
  693. } while (newfileset.logicalBlockNum < lastblock &&
  694. fileset->logicalBlockNum == 0xFFFFFFFF &&
  695. fileset->partitionReferenceNum == 0xFFFF);
  696. }
  697. }
  698. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  699. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  700. udf_debug("Fileset at block=%d, partition=%d\n",
  701. fileset->logicalBlockNum,
  702. fileset->partitionReferenceNum);
  703. sbi->s_partition = fileset->partitionReferenceNum;
  704. udf_load_fileset(sb, bh, root);
  705. brelse(bh);
  706. return 0;
  707. }
  708. return 1;
  709. }
  710. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  711. {
  712. struct primaryVolDesc *pvoldesc;
  713. struct ustr *instr, *outstr;
  714. struct buffer_head *bh;
  715. uint16_t ident;
  716. int ret = 1;
  717. instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  718. if (!instr)
  719. return 1;
  720. outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  721. if (!outstr)
  722. goto out1;
  723. bh = udf_read_tagged(sb, block, block, &ident);
  724. if (!bh)
  725. goto out2;
  726. BUG_ON(ident != TAG_IDENT_PVD);
  727. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  728. if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  729. pvoldesc->recordingDateAndTime)) {
  730. #ifdef UDFFS_DEBUG
  731. struct timestamp *ts = &pvoldesc->recordingDateAndTime;
  732. udf_debug("recording time %04u/%02u/%02u"
  733. " %02u:%02u (%x)\n",
  734. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  735. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  736. #endif
  737. }
  738. if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
  739. if (udf_CS0toUTF8(outstr, instr)) {
  740. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  741. outstr->u_len > 31 ? 31 : outstr->u_len);
  742. udf_debug("volIdent[] = '%s'\n",
  743. UDF_SB(sb)->s_volume_ident);
  744. }
  745. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  746. if (udf_CS0toUTF8(outstr, instr))
  747. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  748. brelse(bh);
  749. ret = 0;
  750. out2:
  751. kfree(outstr);
  752. out1:
  753. kfree(instr);
  754. return ret;
  755. }
  756. static int udf_load_metadata_files(struct super_block *sb, int partition)
  757. {
  758. struct udf_sb_info *sbi = UDF_SB(sb);
  759. struct udf_part_map *map;
  760. struct udf_meta_data *mdata;
  761. struct kernel_lb_addr addr;
  762. int fe_error = 0;
  763. map = &sbi->s_partmaps[partition];
  764. mdata = &map->s_type_specific.s_metadata;
  765. /* metadata address */
  766. addr.logicalBlockNum = mdata->s_meta_file_loc;
  767. addr.partitionReferenceNum = map->s_partition_num;
  768. udf_debug("Metadata file location: block = %d part = %d\n",
  769. addr.logicalBlockNum, addr.partitionReferenceNum);
  770. mdata->s_metadata_fe = udf_iget(sb, &addr);
  771. if (mdata->s_metadata_fe == NULL) {
  772. udf_warning(sb, __func__, "metadata inode efe not found, "
  773. "will try mirror inode.");
  774. fe_error = 1;
  775. } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
  776. ICBTAG_FLAG_AD_SHORT) {
  777. udf_warning(sb, __func__, "metadata inode efe does not have "
  778. "short allocation descriptors!");
  779. fe_error = 1;
  780. iput(mdata->s_metadata_fe);
  781. mdata->s_metadata_fe = NULL;
  782. }
  783. /* mirror file entry */
  784. addr.logicalBlockNum = mdata->s_mirror_file_loc;
  785. addr.partitionReferenceNum = map->s_partition_num;
  786. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  787. addr.logicalBlockNum, addr.partitionReferenceNum);
  788. mdata->s_mirror_fe = udf_iget(sb, &addr);
  789. if (mdata->s_mirror_fe == NULL) {
  790. if (fe_error) {
  791. udf_err(sb, "mirror inode efe not found and metadata inode is missing too, exiting...\n");
  792. goto error_exit;
  793. } else
  794. udf_warning(sb, __func__, "mirror inode efe not found,"
  795. " but metadata inode is OK");
  796. } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
  797. ICBTAG_FLAG_AD_SHORT) {
  798. udf_warning(sb, __func__, "mirror inode efe does not have "
  799. "short allocation descriptors!");
  800. iput(mdata->s_mirror_fe);
  801. mdata->s_mirror_fe = NULL;
  802. if (fe_error)
  803. goto error_exit;
  804. }
  805. /*
  806. * bitmap file entry
  807. * Note:
  808. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  809. */
  810. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  811. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  812. addr.partitionReferenceNum = map->s_partition_num;
  813. udf_debug("Bitmap file location: block = %d part = %d\n",
  814. addr.logicalBlockNum, addr.partitionReferenceNum);
  815. mdata->s_bitmap_fe = udf_iget(sb, &addr);
  816. if (mdata->s_bitmap_fe == NULL) {
  817. if (sb->s_flags & MS_RDONLY)
  818. udf_warning(sb, __func__, "bitmap inode efe "
  819. "not found but it's ok since the disc"
  820. " is mounted read-only");
  821. else {
  822. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  823. goto error_exit;
  824. }
  825. }
  826. }
  827. udf_debug("udf_load_metadata_files Ok\n");
  828. return 0;
  829. error_exit:
  830. return 1;
  831. }
  832. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  833. struct kernel_lb_addr *root)
  834. {
  835. struct fileSetDesc *fset;
  836. fset = (struct fileSetDesc *)bh->b_data;
  837. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  838. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  839. udf_debug("Rootdir at block=%d, partition=%d\n",
  840. root->logicalBlockNum, root->partitionReferenceNum);
  841. }
  842. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  843. {
  844. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  845. return DIV_ROUND_UP(map->s_partition_len +
  846. (sizeof(struct spaceBitmapDesc) << 3),
  847. sb->s_blocksize * 8);
  848. }
  849. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  850. {
  851. struct udf_bitmap *bitmap;
  852. int nr_groups;
  853. int size;
  854. nr_groups = udf_compute_nr_groups(sb, index);
  855. size = sizeof(struct udf_bitmap) +
  856. (sizeof(struct buffer_head *) * nr_groups);
  857. if (size <= PAGE_SIZE)
  858. bitmap = kzalloc(size, GFP_KERNEL);
  859. else
  860. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  861. if (bitmap == NULL) {
  862. udf_err(sb, "Unable to allocate space for bitmap and %d buffer_head pointers\n",
  863. 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_err(sb, "utf8 cannot be combined with iocharset\n");
  1720. goto error_out;
  1721. }
  1722. #ifdef CONFIG_UDF_NLS
  1723. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1724. uopt.nls_map = load_nls_default();
  1725. if (!uopt.nls_map)
  1726. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1727. else
  1728. udf_debug("Using default NLS map\n");
  1729. }
  1730. #endif
  1731. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1732. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1733. fileset.logicalBlockNum = 0xFFFFFFFF;
  1734. fileset.partitionReferenceNum = 0xFFFF;
  1735. sbi->s_flags = uopt.flags;
  1736. sbi->s_uid = uopt.uid;
  1737. sbi->s_gid = uopt.gid;
  1738. sbi->s_umask = uopt.umask;
  1739. sbi->s_fmode = uopt.fmode;
  1740. sbi->s_dmode = uopt.dmode;
  1741. sbi->s_nls_map = uopt.nls_map;
  1742. rwlock_init(&sbi->s_cred_lock);
  1743. if (uopt.session == 0xFFFFFFFF)
  1744. sbi->s_session = udf_get_last_session(sb);
  1745. else
  1746. sbi->s_session = uopt.session;
  1747. udf_debug("Multi-session=%d\n", sbi->s_session);
  1748. /* Fill in the rest of the superblock */
  1749. sb->s_op = &udf_sb_ops;
  1750. sb->s_export_op = &udf_export_ops;
  1751. sb->s_dirt = 0;
  1752. sb->s_magic = UDF_SUPER_MAGIC;
  1753. sb->s_time_gran = 1000;
  1754. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1755. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1756. } else {
  1757. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1758. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1759. if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1760. if (!silent)
  1761. printk(KERN_NOTICE
  1762. "UDF-fs: Rescanning with blocksize "
  1763. "%d\n", UDF_DEFAULT_BLOCKSIZE);
  1764. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1765. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1766. }
  1767. }
  1768. if (!ret) {
  1769. printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
  1770. goto error_out;
  1771. }
  1772. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1773. if (sbi->s_lvid_bh) {
  1774. struct logicalVolIntegrityDescImpUse *lvidiu =
  1775. udf_sb_lvidiu(sbi);
  1776. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1777. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1778. /* uint16_t maxUDFWriteRev =
  1779. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1780. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1781. printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
  1782. "(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. printk(KERN_WARNING "UDF-fs: 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. printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
  1801. "forcing readonly mount\n");
  1802. sb->s_flags |= MS_RDONLY;
  1803. }
  1804. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1805. printk(KERN_WARNING "UDF-fs: No fileset found\n");
  1806. goto error_out;
  1807. }
  1808. if (!silent) {
  1809. struct timestamp ts;
  1810. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1811. udf_info("UDF: Mounting volume '%s', "
  1812. "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1813. sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
  1814. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1815. }
  1816. if (!(sb->s_flags & MS_RDONLY))
  1817. udf_open_lvid(sb);
  1818. /* Assign the root inode */
  1819. /* assign inodes by physical block number */
  1820. /* perhaps it's not extensible enough, but for now ... */
  1821. inode = udf_iget(sb, &rootdir);
  1822. if (!inode) {
  1823. printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
  1824. "partition=%d\n",
  1825. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1826. goto error_out;
  1827. }
  1828. /* Allocate a dentry for the root inode */
  1829. sb->s_root = d_alloc_root(inode);
  1830. if (!sb->s_root) {
  1831. printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
  1832. iput(inode);
  1833. goto error_out;
  1834. }
  1835. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1836. return 0;
  1837. error_out:
  1838. if (sbi->s_vat_inode)
  1839. iput(sbi->s_vat_inode);
  1840. if (sbi->s_partitions)
  1841. for (i = 0; i < sbi->s_partitions; i++)
  1842. udf_free_partition(&sbi->s_partmaps[i]);
  1843. #ifdef CONFIG_UDF_NLS
  1844. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1845. unload_nls(sbi->s_nls_map);
  1846. #endif
  1847. if (!(sb->s_flags & MS_RDONLY))
  1848. udf_close_lvid(sb);
  1849. brelse(sbi->s_lvid_bh);
  1850. kfree(sbi->s_partmaps);
  1851. kfree(sbi);
  1852. sb->s_fs_info = NULL;
  1853. return -EINVAL;
  1854. }
  1855. void _udf_err(struct super_block *sb, const char *function,
  1856. const char *fmt, ...)
  1857. {
  1858. va_list args;
  1859. if (!(sb->s_flags & MS_RDONLY)) {
  1860. /* mark sb error */
  1861. sb->s_dirt = 1;
  1862. }
  1863. va_start(args, fmt);
  1864. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1865. va_end(args);
  1866. printk(KERN_CRIT "UDF-fs error (device %s): %s: %s",
  1867. sb->s_id, function, error_buf);
  1868. }
  1869. void udf_warning(struct super_block *sb, const char *function,
  1870. const char *fmt, ...)
  1871. {
  1872. va_list args;
  1873. va_start(args, fmt);
  1874. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1875. va_end(args);
  1876. printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
  1877. sb->s_id, function, error_buf);
  1878. }
  1879. static void udf_put_super(struct super_block *sb)
  1880. {
  1881. int i;
  1882. struct udf_sb_info *sbi;
  1883. sbi = UDF_SB(sb);
  1884. if (sbi->s_vat_inode)
  1885. iput(sbi->s_vat_inode);
  1886. if (sbi->s_partitions)
  1887. for (i = 0; i < sbi->s_partitions; i++)
  1888. udf_free_partition(&sbi->s_partmaps[i]);
  1889. #ifdef CONFIG_UDF_NLS
  1890. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1891. unload_nls(sbi->s_nls_map);
  1892. #endif
  1893. if (!(sb->s_flags & MS_RDONLY))
  1894. udf_close_lvid(sb);
  1895. brelse(sbi->s_lvid_bh);
  1896. kfree(sbi->s_partmaps);
  1897. kfree(sb->s_fs_info);
  1898. sb->s_fs_info = NULL;
  1899. }
  1900. static int udf_sync_fs(struct super_block *sb, int wait)
  1901. {
  1902. struct udf_sb_info *sbi = UDF_SB(sb);
  1903. mutex_lock(&sbi->s_alloc_mutex);
  1904. if (sbi->s_lvid_dirty) {
  1905. /*
  1906. * Blockdevice will be synced later so we don't have to submit
  1907. * the buffer for IO
  1908. */
  1909. mark_buffer_dirty(sbi->s_lvid_bh);
  1910. sb->s_dirt = 0;
  1911. sbi->s_lvid_dirty = 0;
  1912. }
  1913. mutex_unlock(&sbi->s_alloc_mutex);
  1914. return 0;
  1915. }
  1916. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1917. {
  1918. struct super_block *sb = dentry->d_sb;
  1919. struct udf_sb_info *sbi = UDF_SB(sb);
  1920. struct logicalVolIntegrityDescImpUse *lvidiu;
  1921. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1922. if (sbi->s_lvid_bh != NULL)
  1923. lvidiu = udf_sb_lvidiu(sbi);
  1924. else
  1925. lvidiu = NULL;
  1926. buf->f_type = UDF_SUPER_MAGIC;
  1927. buf->f_bsize = sb->s_blocksize;
  1928. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1929. buf->f_bfree = udf_count_free(sb);
  1930. buf->f_bavail = buf->f_bfree;
  1931. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1932. le32_to_cpu(lvidiu->numDirs)) : 0)
  1933. + buf->f_bfree;
  1934. buf->f_ffree = buf->f_bfree;
  1935. buf->f_namelen = UDF_NAME_LEN - 2;
  1936. buf->f_fsid.val[0] = (u32)id;
  1937. buf->f_fsid.val[1] = (u32)(id >> 32);
  1938. return 0;
  1939. }
  1940. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1941. struct udf_bitmap *bitmap)
  1942. {
  1943. struct buffer_head *bh = NULL;
  1944. unsigned int accum = 0;
  1945. int index;
  1946. int block = 0, newblock;
  1947. struct kernel_lb_addr loc;
  1948. uint32_t bytes;
  1949. uint8_t *ptr;
  1950. uint16_t ident;
  1951. struct spaceBitmapDesc *bm;
  1952. loc.logicalBlockNum = bitmap->s_extPosition;
  1953. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1954. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  1955. if (!bh) {
  1956. printk(KERN_ERR "udf: udf_count_free failed\n");
  1957. goto out;
  1958. } else if (ident != TAG_IDENT_SBD) {
  1959. brelse(bh);
  1960. printk(KERN_ERR "udf: udf_count_free failed\n");
  1961. goto out;
  1962. }
  1963. bm = (struct spaceBitmapDesc *)bh->b_data;
  1964. bytes = le32_to_cpu(bm->numOfBytes);
  1965. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1966. ptr = (uint8_t *)bh->b_data;
  1967. while (bytes > 0) {
  1968. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1969. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1970. cur_bytes * 8);
  1971. bytes -= cur_bytes;
  1972. if (bytes) {
  1973. brelse(bh);
  1974. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  1975. bh = udf_tread(sb, newblock);
  1976. if (!bh) {
  1977. udf_debug("read failed\n");
  1978. goto out;
  1979. }
  1980. index = 0;
  1981. ptr = (uint8_t *)bh->b_data;
  1982. }
  1983. }
  1984. brelse(bh);
  1985. out:
  1986. return accum;
  1987. }
  1988. static unsigned int udf_count_free_table(struct super_block *sb,
  1989. struct inode *table)
  1990. {
  1991. unsigned int accum = 0;
  1992. uint32_t elen;
  1993. struct kernel_lb_addr eloc;
  1994. int8_t etype;
  1995. struct extent_position epos;
  1996. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  1997. epos.block = UDF_I(table)->i_location;
  1998. epos.offset = sizeof(struct unallocSpaceEntry);
  1999. epos.bh = NULL;
  2000. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2001. accum += (elen >> table->i_sb->s_blocksize_bits);
  2002. brelse(epos.bh);
  2003. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2004. return accum;
  2005. }
  2006. static unsigned int udf_count_free(struct super_block *sb)
  2007. {
  2008. unsigned int accum = 0;
  2009. struct udf_sb_info *sbi;
  2010. struct udf_part_map *map;
  2011. sbi = UDF_SB(sb);
  2012. if (sbi->s_lvid_bh) {
  2013. struct logicalVolIntegrityDesc *lvid =
  2014. (struct logicalVolIntegrityDesc *)
  2015. sbi->s_lvid_bh->b_data;
  2016. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2017. accum = le32_to_cpu(
  2018. lvid->freeSpaceTable[sbi->s_partition]);
  2019. if (accum == 0xFFFFFFFF)
  2020. accum = 0;
  2021. }
  2022. }
  2023. if (accum)
  2024. return accum;
  2025. map = &sbi->s_partmaps[sbi->s_partition];
  2026. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2027. accum += udf_count_free_bitmap(sb,
  2028. map->s_uspace.s_bitmap);
  2029. }
  2030. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2031. accum += udf_count_free_bitmap(sb,
  2032. map->s_fspace.s_bitmap);
  2033. }
  2034. if (accum)
  2035. return accum;
  2036. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2037. accum += udf_count_free_table(sb,
  2038. map->s_uspace.s_table);
  2039. }
  2040. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2041. accum += udf_count_free_table(sb,
  2042. map->s_fspace.s_table);
  2043. }
  2044. return accum;
  2045. }