super.c 61 KB

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