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