super.c 61 KB

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