super.c 56 KB

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