super.c 61 KB

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