super.c 54 KB

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