super.c 54 KB

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