super.c 61 KB

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