super.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  1. /*
  2. * super.c
  3. *
  4. * PURPOSE
  5. * Super block routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * DESCRIPTION
  8. * OSTA-UDF(tm) = Optical Storage Technology Association
  9. * Universal Disk Format.
  10. *
  11. * This code is based on version 2.00 of the UDF specification,
  12. * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
  13. * http://www.osta.org/
  14. * http://www.ecma.ch/
  15. * http://www.iso.org/
  16. *
  17. * COPYRIGHT
  18. * This file is distributed under the terms of the GNU General Public
  19. * License (GPL). Copies of the GPL can be obtained from:
  20. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  21. * Each contributing author retains all rights to their own work.
  22. *
  23. * (C) 1998 Dave Boynton
  24. * (C) 1998-2004 Ben Fennema
  25. * (C) 2000 Stelias Computing Inc
  26. *
  27. * HISTORY
  28. *
  29. * 09/24/98 dgb changed to allow compiling outside of kernel, and
  30. * added some debugging.
  31. * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
  32. * 10/16/98 attempting some multi-session support
  33. * 10/17/98 added freespace count for "df"
  34. * 11/11/98 gr added novrs option
  35. * 11/26/98 dgb added fileset,anchor mount options
  36. * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
  37. * vol descs. rewrote option handling based on isofs
  38. * 12/20/98 find the free space bitmap (if it exists)
  39. */
  40. #include "udfdecl.h"
  41. #include <linux/blkdev.h>
  42. #include <linux/slab.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/parser.h>
  46. #include <linux/stat.h>
  47. #include <linux/cdrom.h>
  48. #include <linux/nls.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/buffer_head.h>
  51. #include <linux/vfs.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/errno.h>
  54. #include <linux/mount.h>
  55. #include <linux/seq_file.h>
  56. #include <linux/bitmap.h>
  57. #include <linux/crc-itu-t.h>
  58. #include <asm/byteorder.h>
  59. #include "udf_sb.h"
  60. #include "udf_i.h"
  61. #include <linux/init.h>
  62. #include <asm/uaccess.h>
  63. #define VDS_POS_PRIMARY_VOL_DESC 0
  64. #define VDS_POS_UNALLOC_SPACE_DESC 1
  65. #define VDS_POS_LOGICAL_VOL_DESC 2
  66. #define VDS_POS_PARTITION_DESC 3
  67. #define VDS_POS_IMP_USE_VOL_DESC 4
  68. #define VDS_POS_VOL_DESC_PTR 5
  69. #define VDS_POS_TERMINATING_DESC 6
  70. #define VDS_POS_LENGTH 7
  71. #define UDF_DEFAULT_BLOCKSIZE 2048
  72. static char error_buf[1024];
  73. /* These are the "meat" - everything else is stuffing */
  74. static int udf_fill_super(struct super_block *, void *, int);
  75. static void udf_put_super(struct super_block *);
  76. static void udf_write_super(struct super_block *);
  77. static int udf_remount_fs(struct super_block *, int *, char *);
  78. static int udf_check_valid(struct super_block *, int, int);
  79. static int udf_vrs(struct super_block *sb, int silent);
  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, __func__,
  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. loff_t 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. (unsigned int)(sector >> sb->s_blocksize_bits),
  555. sb->s_blocksize);
  556. /* Process the sequence (if applicable) */
  557. for (; !nsr02 && !nsr03; sector += sectorsize) {
  558. /* Read a block */
  559. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  560. if (!bh)
  561. break;
  562. /* Look for ISO descriptors */
  563. vsd = (struct volStructDesc *)(bh->b_data +
  564. (sector & (sb->s_blocksize - 1)));
  565. if (vsd->stdIdent[0] == 0) {
  566. brelse(bh);
  567. break;
  568. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
  569. VSD_STD_ID_LEN)) {
  570. iso9660 = sector;
  571. switch (vsd->structType) {
  572. case 0:
  573. udf_debug("ISO9660 Boot Record found\n");
  574. break;
  575. case 1:
  576. udf_debug("ISO9660 Primary Volume Descriptor "
  577. "found\n");
  578. break;
  579. case 2:
  580. udf_debug("ISO9660 Supplementary Volume "
  581. "Descriptor found\n");
  582. break;
  583. case 3:
  584. udf_debug("ISO9660 Volume Partition Descriptor "
  585. "found\n");
  586. break;
  587. case 255:
  588. udf_debug("ISO9660 Volume Descriptor Set "
  589. "Terminator found\n");
  590. break;
  591. default:
  592. udf_debug("ISO9660 VRS (%u) found\n",
  593. vsd->structType);
  594. break;
  595. }
  596. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  597. VSD_STD_ID_LEN))
  598. ; /* nothing */
  599. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  600. VSD_STD_ID_LEN)) {
  601. brelse(bh);
  602. break;
  603. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  604. VSD_STD_ID_LEN))
  605. nsr02 = sector;
  606. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  607. VSD_STD_ID_LEN))
  608. nsr03 = sector;
  609. brelse(bh);
  610. }
  611. if (nsr03)
  612. return nsr03;
  613. else if (nsr02)
  614. return nsr02;
  615. else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
  616. return -1;
  617. else
  618. return 0;
  619. }
  620. /*
  621. * Check whether there is an anchor block in the given block
  622. */
  623. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  624. bool varconv)
  625. {
  626. struct buffer_head *bh = NULL;
  627. tag *t;
  628. uint16_t ident;
  629. uint32_t location;
  630. if (varconv) {
  631. if (udf_fixed_to_variable(block) >=
  632. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  633. return 0;
  634. bh = sb_bread(sb, udf_fixed_to_variable(block));
  635. }
  636. else
  637. bh = sb_bread(sb, block);
  638. if (!bh)
  639. return 0;
  640. t = (tag *)bh->b_data;
  641. ident = le16_to_cpu(t->tagIdent);
  642. location = le32_to_cpu(t->tagLocation);
  643. brelse(bh);
  644. if (ident != TAG_IDENT_AVDP)
  645. return 0;
  646. return location == block;
  647. }
  648. /* Search for an anchor volume descriptor pointer */
  649. static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
  650. sector_t lastblock)
  651. {
  652. sector_t last[6];
  653. int i;
  654. struct udf_sb_info *sbi = UDF_SB(sb);
  655. last[0] = lastblock;
  656. last[1] = last[0] - 1;
  657. last[2] = last[0] + 1;
  658. last[3] = last[0] - 2;
  659. last[4] = last[0] - 150;
  660. last[5] = last[0] - 152;
  661. /* according to spec, anchor is in either:
  662. * block 256
  663. * lastblock-256
  664. * lastblock
  665. * however, if the disc isn't closed, it could be 512 */
  666. for (i = 0; i < ARRAY_SIZE(last); i++) {
  667. if (last[i] < 0)
  668. continue;
  669. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  670. sb->s_blocksize_bits)
  671. continue;
  672. if (udf_check_anchor_block(sb, last[i], varconv)) {
  673. sbi->s_anchor[0] = last[i];
  674. sbi->s_anchor[1] = last[i] - 256;
  675. return last[i];
  676. }
  677. if (last[i] < 256)
  678. continue;
  679. if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
  680. sbi->s_anchor[1] = last[i] - 256;
  681. return last[i];
  682. }
  683. }
  684. if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
  685. sbi->s_anchor[0] = sbi->s_session + 256;
  686. return last[0];
  687. }
  688. if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
  689. sbi->s_anchor[0] = sbi->s_session + 512;
  690. return last[0];
  691. }
  692. return 0;
  693. }
  694. /*
  695. * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
  696. * be the last block on the media.
  697. *
  698. * Return 1 if not found, 0 if ok
  699. *
  700. */
  701. static void udf_find_anchor(struct super_block *sb)
  702. {
  703. sector_t lastblock;
  704. struct buffer_head *bh = NULL;
  705. uint16_t ident;
  706. int i;
  707. struct udf_sb_info *sbi = UDF_SB(sb);
  708. lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
  709. if (lastblock)
  710. goto check_anchor;
  711. /* No anchor found? Try VARCONV conversion of block numbers */
  712. /* Firstly, we try to not convert number of the last block */
  713. lastblock = udf_scan_anchors(sb, 1,
  714. udf_variable_to_fixed(sbi->s_last_block));
  715. if (lastblock) {
  716. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  717. goto check_anchor;
  718. }
  719. /* Secondly, we try with converted number of the last block */
  720. lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
  721. if (lastblock)
  722. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  723. check_anchor:
  724. /*
  725. * Check located anchors and the anchor block supplied via
  726. * mount options
  727. */
  728. for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
  729. if (!sbi->s_anchor[i])
  730. continue;
  731. bh = udf_read_tagged(sb, sbi->s_anchor[i],
  732. sbi->s_anchor[i], &ident);
  733. if (!bh)
  734. sbi->s_anchor[i] = 0;
  735. else {
  736. brelse(bh);
  737. if (ident != TAG_IDENT_AVDP)
  738. sbi->s_anchor[i] = 0;
  739. }
  740. }
  741. sbi->s_last_block = lastblock;
  742. }
  743. static int udf_find_fileset(struct super_block *sb,
  744. kernel_lb_addr *fileset,
  745. kernel_lb_addr *root)
  746. {
  747. struct buffer_head *bh = NULL;
  748. long lastblock;
  749. uint16_t ident;
  750. struct udf_sb_info *sbi;
  751. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  752. fileset->partitionReferenceNum != 0xFFFF) {
  753. bh = udf_read_ptagged(sb, *fileset, 0, &ident);
  754. if (!bh) {
  755. return 1;
  756. } else if (ident != TAG_IDENT_FSD) {
  757. brelse(bh);
  758. return 1;
  759. }
  760. }
  761. sbi = UDF_SB(sb);
  762. if (!bh) {
  763. /* Search backwards through the partitions */
  764. kernel_lb_addr newfileset;
  765. /* --> cvg: FIXME - is it reasonable? */
  766. return 1;
  767. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  768. (newfileset.partitionReferenceNum != 0xFFFF &&
  769. fileset->logicalBlockNum == 0xFFFFFFFF &&
  770. fileset->partitionReferenceNum == 0xFFFF);
  771. newfileset.partitionReferenceNum--) {
  772. lastblock = sbi->s_partmaps
  773. [newfileset.partitionReferenceNum]
  774. .s_partition_len;
  775. newfileset.logicalBlockNum = 0;
  776. do {
  777. bh = udf_read_ptagged(sb, newfileset, 0,
  778. &ident);
  779. if (!bh) {
  780. newfileset.logicalBlockNum++;
  781. continue;
  782. }
  783. switch (ident) {
  784. case TAG_IDENT_SBD:
  785. {
  786. struct spaceBitmapDesc *sp;
  787. sp = (struct spaceBitmapDesc *)
  788. bh->b_data;
  789. newfileset.logicalBlockNum += 1 +
  790. ((le32_to_cpu(sp->numOfBytes) +
  791. sizeof(struct spaceBitmapDesc)
  792. - 1) >> sb->s_blocksize_bits);
  793. brelse(bh);
  794. break;
  795. }
  796. case TAG_IDENT_FSD:
  797. *fileset = newfileset;
  798. break;
  799. default:
  800. newfileset.logicalBlockNum++;
  801. brelse(bh);
  802. bh = NULL;
  803. break;
  804. }
  805. } while (newfileset.logicalBlockNum < lastblock &&
  806. fileset->logicalBlockNum == 0xFFFFFFFF &&
  807. fileset->partitionReferenceNum == 0xFFFF);
  808. }
  809. }
  810. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  811. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  812. udf_debug("Fileset at block=%d, partition=%d\n",
  813. fileset->logicalBlockNum,
  814. fileset->partitionReferenceNum);
  815. sbi->s_partition = fileset->partitionReferenceNum;
  816. udf_load_fileset(sb, bh, root);
  817. brelse(bh);
  818. return 0;
  819. }
  820. return 1;
  821. }
  822. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  823. {
  824. struct primaryVolDesc *pvoldesc;
  825. struct ustr instr;
  826. struct ustr outstr;
  827. struct buffer_head *bh;
  828. uint16_t ident;
  829. bh = udf_read_tagged(sb, block, block, &ident);
  830. if (!bh)
  831. return 1;
  832. BUG_ON(ident != TAG_IDENT_PVD);
  833. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  834. if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  835. pvoldesc->recordingDateAndTime)) {
  836. #ifdef UDFFS_DEBUG
  837. timestamp *ts = &pvoldesc->recordingDateAndTime;
  838. udf_debug("recording time %04u/%02u/%02u"
  839. " %02u:%02u (%x)\n",
  840. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  841. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  842. #endif
  843. }
  844. if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
  845. if (udf_CS0toUTF8(&outstr, &instr)) {
  846. strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
  847. outstr.u_len > 31 ? 31 : outstr.u_len);
  848. udf_debug("volIdent[] = '%s'\n",
  849. UDF_SB(sb)->s_volume_ident);
  850. }
  851. if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
  852. if (udf_CS0toUTF8(&outstr, &instr))
  853. udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
  854. brelse(bh);
  855. return 0;
  856. }
  857. static int udf_load_metadata_files(struct super_block *sb, int partition)
  858. {
  859. struct udf_sb_info *sbi = UDF_SB(sb);
  860. struct udf_part_map *map;
  861. struct udf_meta_data *mdata;
  862. kernel_lb_addr addr;
  863. int fe_error = 0;
  864. map = &sbi->s_partmaps[partition];
  865. mdata = &map->s_type_specific.s_metadata;
  866. /* metadata address */
  867. addr.logicalBlockNum = mdata->s_meta_file_loc;
  868. addr.partitionReferenceNum = map->s_partition_num;
  869. udf_debug("Metadata file location: block = %d part = %d\n",
  870. addr.logicalBlockNum, addr.partitionReferenceNum);
  871. mdata->s_metadata_fe = udf_iget(sb, addr);
  872. if (mdata->s_metadata_fe == NULL) {
  873. udf_warning(sb, __func__, "metadata inode efe not found, "
  874. "will try mirror inode.");
  875. fe_error = 1;
  876. } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
  877. ICBTAG_FLAG_AD_SHORT) {
  878. udf_warning(sb, __func__, "metadata inode efe does not have "
  879. "short allocation descriptors!");
  880. fe_error = 1;
  881. iput(mdata->s_metadata_fe);
  882. mdata->s_metadata_fe = NULL;
  883. }
  884. /* mirror file entry */
  885. addr.logicalBlockNum = mdata->s_mirror_file_loc;
  886. addr.partitionReferenceNum = map->s_partition_num;
  887. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  888. addr.logicalBlockNum, addr.partitionReferenceNum);
  889. mdata->s_mirror_fe = udf_iget(sb, addr);
  890. if (mdata->s_mirror_fe == NULL) {
  891. if (fe_error) {
  892. udf_error(sb, __func__, "mirror inode efe not found "
  893. "and metadata inode is missing too, exiting...");
  894. goto error_exit;
  895. } else
  896. udf_warning(sb, __func__, "mirror inode efe not found,"
  897. " but metadata inode is OK");
  898. } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
  899. ICBTAG_FLAG_AD_SHORT) {
  900. udf_warning(sb, __func__, "mirror inode efe does not have "
  901. "short allocation descriptors!");
  902. iput(mdata->s_mirror_fe);
  903. mdata->s_mirror_fe = NULL;
  904. if (fe_error)
  905. goto error_exit;
  906. }
  907. /*
  908. * bitmap file entry
  909. * Note:
  910. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  911. */
  912. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  913. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  914. addr.partitionReferenceNum = map->s_partition_num;
  915. udf_debug("Bitmap file location: block = %d part = %d\n",
  916. addr.logicalBlockNum, addr.partitionReferenceNum);
  917. mdata->s_bitmap_fe = udf_iget(sb, addr);
  918. if (mdata->s_bitmap_fe == NULL) {
  919. if (sb->s_flags & MS_RDONLY)
  920. udf_warning(sb, __func__, "bitmap inode efe "
  921. "not found but it's ok since the disc"
  922. " is mounted read-only");
  923. else {
  924. udf_error(sb, __func__, "bitmap inode efe not "
  925. "found and attempted read-write mount");
  926. goto error_exit;
  927. }
  928. }
  929. }
  930. udf_debug("udf_load_metadata_files Ok\n");
  931. return 0;
  932. error_exit:
  933. return 1;
  934. }
  935. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  936. kernel_lb_addr *root)
  937. {
  938. struct fileSetDesc *fset;
  939. fset = (struct fileSetDesc *)bh->b_data;
  940. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  941. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  942. udf_debug("Rootdir at block=%d, partition=%d\n",
  943. root->logicalBlockNum, root->partitionReferenceNum);
  944. }
  945. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  946. {
  947. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  948. return DIV_ROUND_UP(map->s_partition_len +
  949. (sizeof(struct spaceBitmapDesc) << 3),
  950. sb->s_blocksize * 8);
  951. }
  952. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  953. {
  954. struct udf_bitmap *bitmap;
  955. int nr_groups;
  956. int size;
  957. nr_groups = udf_compute_nr_groups(sb, index);
  958. size = sizeof(struct udf_bitmap) +
  959. (sizeof(struct buffer_head *) * nr_groups);
  960. if (size <= PAGE_SIZE)
  961. bitmap = kmalloc(size, GFP_KERNEL);
  962. else
  963. bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
  964. if (bitmap == NULL) {
  965. udf_error(sb, __func__,
  966. "Unable to allocate space for bitmap "
  967. "and %d buffer_head pointers", nr_groups);
  968. return NULL;
  969. }
  970. memset(bitmap, 0x00, size);
  971. bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
  972. bitmap->s_nr_groups = nr_groups;
  973. return bitmap;
  974. }
  975. static int udf_fill_partdesc_info(struct super_block *sb,
  976. struct partitionDesc *p, int p_index)
  977. {
  978. struct udf_part_map *map;
  979. struct udf_sb_info *sbi = UDF_SB(sb);
  980. struct partitionHeaderDesc *phd;
  981. map = &sbi->s_partmaps[p_index];
  982. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  983. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  984. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  985. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  986. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  987. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  988. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  989. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  990. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  991. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  992. udf_debug("Partition (%d type %x) starts at physical %d, "
  993. "block length %d\n", p_index,
  994. map->s_partition_type, map->s_partition_root,
  995. map->s_partition_len);
  996. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  997. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  998. return 0;
  999. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  1000. if (phd->unallocSpaceTable.extLength) {
  1001. kernel_lb_addr loc = {
  1002. .logicalBlockNum = le32_to_cpu(
  1003. phd->unallocSpaceTable.extPosition),
  1004. .partitionReferenceNum = p_index,
  1005. };
  1006. map->s_uspace.s_table = udf_iget(sb, loc);
  1007. if (!map->s_uspace.s_table) {
  1008. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  1009. p_index);
  1010. return 1;
  1011. }
  1012. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  1013. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  1014. p_index, map->s_uspace.s_table->i_ino);
  1015. }
  1016. if (phd->unallocSpaceBitmap.extLength) {
  1017. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  1018. if (!bitmap)
  1019. return 1;
  1020. map->s_uspace.s_bitmap = bitmap;
  1021. bitmap->s_extLength = le32_to_cpu(
  1022. phd->unallocSpaceBitmap.extLength);
  1023. bitmap->s_extPosition = le32_to_cpu(
  1024. phd->unallocSpaceBitmap.extPosition);
  1025. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  1026. udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
  1027. bitmap->s_extPosition);
  1028. }
  1029. if (phd->partitionIntegrityTable.extLength)
  1030. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  1031. if (phd->freedSpaceTable.extLength) {
  1032. kernel_lb_addr loc = {
  1033. .logicalBlockNum = le32_to_cpu(
  1034. phd->freedSpaceTable.extPosition),
  1035. .partitionReferenceNum = p_index,
  1036. };
  1037. map->s_fspace.s_table = udf_iget(sb, loc);
  1038. if (!map->s_fspace.s_table) {
  1039. udf_debug("cannot load freedSpaceTable (part %d)\n",
  1040. p_index);
  1041. return 1;
  1042. }
  1043. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  1044. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  1045. p_index, map->s_fspace.s_table->i_ino);
  1046. }
  1047. if (phd->freedSpaceBitmap.extLength) {
  1048. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  1049. if (!bitmap)
  1050. return 1;
  1051. map->s_fspace.s_bitmap = bitmap;
  1052. bitmap->s_extLength = le32_to_cpu(
  1053. phd->freedSpaceBitmap.extLength);
  1054. bitmap->s_extPosition = le32_to_cpu(
  1055. phd->freedSpaceBitmap.extPosition);
  1056. map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
  1057. udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
  1058. bitmap->s_extPosition);
  1059. }
  1060. return 0;
  1061. }
  1062. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  1063. {
  1064. struct udf_sb_info *sbi = UDF_SB(sb);
  1065. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1066. kernel_lb_addr ino;
  1067. struct buffer_head *bh = NULL;
  1068. struct udf_inode_info *vati;
  1069. uint32_t pos;
  1070. struct virtualAllocationTable20 *vat20;
  1071. /* VAT file entry is in the last recorded block */
  1072. ino.partitionReferenceNum = type1_index;
  1073. ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
  1074. sbi->s_vat_inode = udf_iget(sb, ino);
  1075. if (!sbi->s_vat_inode)
  1076. return 1;
  1077. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1078. map->s_type_specific.s_virtual.s_start_offset = 0;
  1079. map->s_type_specific.s_virtual.s_num_entries =
  1080. (sbi->s_vat_inode->i_size - 36) >> 2;
  1081. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1082. vati = UDF_I(sbi->s_vat_inode);
  1083. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1084. pos = udf_block_map(sbi->s_vat_inode, 0);
  1085. bh = sb_bread(sb, pos);
  1086. if (!bh)
  1087. return 1;
  1088. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1089. } else {
  1090. vat20 = (struct virtualAllocationTable20 *)
  1091. vati->i_ext.i_data;
  1092. }
  1093. map->s_type_specific.s_virtual.s_start_offset =
  1094. le16_to_cpu(vat20->lengthHeader);
  1095. map->s_type_specific.s_virtual.s_num_entries =
  1096. (sbi->s_vat_inode->i_size -
  1097. map->s_type_specific.s_virtual.
  1098. s_start_offset) >> 2;
  1099. brelse(bh);
  1100. }
  1101. return 0;
  1102. }
  1103. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1104. {
  1105. struct buffer_head *bh;
  1106. struct partitionDesc *p;
  1107. struct udf_part_map *map;
  1108. struct udf_sb_info *sbi = UDF_SB(sb);
  1109. int i, type1_idx;
  1110. uint16_t partitionNumber;
  1111. uint16_t ident;
  1112. int ret = 0;
  1113. bh = udf_read_tagged(sb, block, block, &ident);
  1114. if (!bh)
  1115. return 1;
  1116. if (ident != TAG_IDENT_PD)
  1117. goto out_bh;
  1118. p = (struct partitionDesc *)bh->b_data;
  1119. partitionNumber = le16_to_cpu(p->partitionNumber);
  1120. /* First scan for TYPE1, SPARABLE and METADATA partitions */
  1121. for (i = 0; i < sbi->s_partitions; i++) {
  1122. map = &sbi->s_partmaps[i];
  1123. udf_debug("Searching map: (%d == %d)\n",
  1124. map->s_partition_num, partitionNumber);
  1125. if (map->s_partition_num == partitionNumber &&
  1126. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1127. map->s_partition_type == UDF_SPARABLE_MAP15))
  1128. break;
  1129. }
  1130. if (i >= sbi->s_partitions) {
  1131. udf_debug("Partition (%d) not found in partition map\n",
  1132. partitionNumber);
  1133. goto out_bh;
  1134. }
  1135. ret = udf_fill_partdesc_info(sb, p, i);
  1136. /*
  1137. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1138. * PHYSICAL partitions are already set up
  1139. */
  1140. type1_idx = i;
  1141. for (i = 0; i < sbi->s_partitions; i++) {
  1142. map = &sbi->s_partmaps[i];
  1143. if (map->s_partition_num == partitionNumber &&
  1144. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1145. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1146. map->s_partition_type == UDF_METADATA_MAP25))
  1147. break;
  1148. }
  1149. if (i >= sbi->s_partitions)
  1150. goto out_bh;
  1151. ret = udf_fill_partdesc_info(sb, p, i);
  1152. if (ret)
  1153. goto out_bh;
  1154. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1155. ret = udf_load_metadata_files(sb, i);
  1156. if (ret) {
  1157. printk(KERN_ERR "UDF-fs: error loading MetaData "
  1158. "partition map %d\n", i);
  1159. goto out_bh;
  1160. }
  1161. } else {
  1162. ret = udf_load_vat(sb, i, type1_idx);
  1163. if (ret)
  1164. goto out_bh;
  1165. /*
  1166. * Mark filesystem read-only if we have a partition with
  1167. * virtual map since we don't handle writing to it (we
  1168. * overwrite blocks instead of relocating them).
  1169. */
  1170. sb->s_flags |= MS_RDONLY;
  1171. printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
  1172. "because writing to pseudooverwrite partition is "
  1173. "not implemented.\n");
  1174. }
  1175. out_bh:
  1176. /* In case loading failed, we handle cleanup in udf_fill_super */
  1177. brelse(bh);
  1178. return ret;
  1179. }
  1180. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1181. kernel_lb_addr *fileset)
  1182. {
  1183. struct logicalVolDesc *lvd;
  1184. int i, j, offset;
  1185. uint8_t type;
  1186. struct udf_sb_info *sbi = UDF_SB(sb);
  1187. struct genericPartitionMap *gpm;
  1188. uint16_t ident;
  1189. struct buffer_head *bh;
  1190. int ret = 0;
  1191. bh = udf_read_tagged(sb, block, block, &ident);
  1192. if (!bh)
  1193. return 1;
  1194. BUG_ON(ident != TAG_IDENT_LVD);
  1195. lvd = (struct logicalVolDesc *)bh->b_data;
  1196. i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1197. if (i != 0) {
  1198. ret = i;
  1199. goto out_bh;
  1200. }
  1201. for (i = 0, offset = 0;
  1202. i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
  1203. i++, offset += gpm->partitionMapLength) {
  1204. struct udf_part_map *map = &sbi->s_partmaps[i];
  1205. gpm = (struct genericPartitionMap *)
  1206. &(lvd->partitionMaps[offset]);
  1207. type = gpm->partitionMapType;
  1208. if (type == 1) {
  1209. struct genericPartitionMap1 *gpm1 =
  1210. (struct genericPartitionMap1 *)gpm;
  1211. map->s_partition_type = UDF_TYPE1_MAP15;
  1212. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1213. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1214. map->s_partition_func = NULL;
  1215. } else if (type == 2) {
  1216. struct udfPartitionMap2 *upm2 =
  1217. (struct udfPartitionMap2 *)gpm;
  1218. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1219. strlen(UDF_ID_VIRTUAL))) {
  1220. u16 suf =
  1221. le16_to_cpu(((__le16 *)upm2->partIdent.
  1222. identSuffix)[0]);
  1223. if (suf < 0x0200) {
  1224. map->s_partition_type =
  1225. UDF_VIRTUAL_MAP15;
  1226. map->s_partition_func =
  1227. udf_get_pblock_virt15;
  1228. } else {
  1229. map->s_partition_type =
  1230. UDF_VIRTUAL_MAP20;
  1231. map->s_partition_func =
  1232. udf_get_pblock_virt20;
  1233. }
  1234. } else if (!strncmp(upm2->partIdent.ident,
  1235. UDF_ID_SPARABLE,
  1236. strlen(UDF_ID_SPARABLE))) {
  1237. uint32_t loc;
  1238. struct sparingTable *st;
  1239. struct sparablePartitionMap *spm =
  1240. (struct sparablePartitionMap *)gpm;
  1241. map->s_partition_type = UDF_SPARABLE_MAP15;
  1242. map->s_type_specific.s_sparing.s_packet_len =
  1243. le16_to_cpu(spm->packetLength);
  1244. for (j = 0; j < spm->numSparingTables; j++) {
  1245. struct buffer_head *bh2;
  1246. loc = le32_to_cpu(
  1247. spm->locSparingTable[j]);
  1248. bh2 = udf_read_tagged(sb, loc, loc,
  1249. &ident);
  1250. map->s_type_specific.s_sparing.
  1251. s_spar_map[j] = bh2;
  1252. if (bh2 == NULL)
  1253. continue;
  1254. st = (struct sparingTable *)bh2->b_data;
  1255. if (ident != 0 || strncmp(
  1256. st->sparingIdent.ident,
  1257. UDF_ID_SPARING,
  1258. strlen(UDF_ID_SPARING))) {
  1259. brelse(bh2);
  1260. map->s_type_specific.s_sparing.
  1261. s_spar_map[j] = NULL;
  1262. }
  1263. }
  1264. map->s_partition_func = udf_get_pblock_spar15;
  1265. } else if (!strncmp(upm2->partIdent.ident,
  1266. UDF_ID_METADATA,
  1267. strlen(UDF_ID_METADATA))) {
  1268. struct udf_meta_data *mdata =
  1269. &map->s_type_specific.s_metadata;
  1270. struct metadataPartitionMap *mdm =
  1271. (struct metadataPartitionMap *)
  1272. &(lvd->partitionMaps[offset]);
  1273. udf_debug("Parsing Logical vol part %d "
  1274. "type %d id=%s\n", i, type,
  1275. UDF_ID_METADATA);
  1276. map->s_partition_type = UDF_METADATA_MAP25;
  1277. map->s_partition_func = udf_get_pblock_meta25;
  1278. mdata->s_meta_file_loc =
  1279. le32_to_cpu(mdm->metadataFileLoc);
  1280. mdata->s_mirror_file_loc =
  1281. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1282. mdata->s_bitmap_file_loc =
  1283. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1284. mdata->s_alloc_unit_size =
  1285. le32_to_cpu(mdm->allocUnitSize);
  1286. mdata->s_align_unit_size =
  1287. le16_to_cpu(mdm->alignUnitSize);
  1288. mdata->s_dup_md_flag =
  1289. mdm->flags & 0x01;
  1290. udf_debug("Metadata Ident suffix=0x%x\n",
  1291. (le16_to_cpu(
  1292. ((__le16 *)
  1293. mdm->partIdent.identSuffix)[0])));
  1294. udf_debug("Metadata part num=%d\n",
  1295. le16_to_cpu(mdm->partitionNum));
  1296. udf_debug("Metadata part alloc unit size=%d\n",
  1297. le32_to_cpu(mdm->allocUnitSize));
  1298. udf_debug("Metadata file loc=%d\n",
  1299. le32_to_cpu(mdm->metadataFileLoc));
  1300. udf_debug("Mirror file loc=%d\n",
  1301. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1302. udf_debug("Bitmap file loc=%d\n",
  1303. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1304. udf_debug("Duplicate Flag: %d %d\n",
  1305. mdata->s_dup_md_flag, mdm->flags);
  1306. } else {
  1307. udf_debug("Unknown ident: %s\n",
  1308. upm2->partIdent.ident);
  1309. continue;
  1310. }
  1311. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1312. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1313. }
  1314. udf_debug("Partition (%d:%d) type %d on volume %d\n",
  1315. i, map->s_partition_num, type,
  1316. map->s_volumeseqnum);
  1317. }
  1318. if (fileset) {
  1319. long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
  1320. *fileset = lelb_to_cpu(la->extLocation);
  1321. udf_debug("FileSet found in LogicalVolDesc at block=%d, "
  1322. "partition=%d\n", fileset->logicalBlockNum,
  1323. fileset->partitionReferenceNum);
  1324. }
  1325. if (lvd->integritySeqExt.extLength)
  1326. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1327. out_bh:
  1328. brelse(bh);
  1329. return ret;
  1330. }
  1331. /*
  1332. * udf_load_logicalvolint
  1333. *
  1334. */
  1335. static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
  1336. {
  1337. struct buffer_head *bh = NULL;
  1338. uint16_t ident;
  1339. struct udf_sb_info *sbi = UDF_SB(sb);
  1340. struct logicalVolIntegrityDesc *lvid;
  1341. while (loc.extLength > 0 &&
  1342. (bh = udf_read_tagged(sb, loc.extLocation,
  1343. loc.extLocation, &ident)) &&
  1344. ident == TAG_IDENT_LVID) {
  1345. sbi->s_lvid_bh = bh;
  1346. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1347. if (lvid->nextIntegrityExt.extLength)
  1348. udf_load_logicalvolint(sb,
  1349. leea_to_cpu(lvid->nextIntegrityExt));
  1350. if (sbi->s_lvid_bh != bh)
  1351. brelse(bh);
  1352. loc.extLength -= sb->s_blocksize;
  1353. loc.extLocation++;
  1354. }
  1355. if (sbi->s_lvid_bh != bh)
  1356. brelse(bh);
  1357. }
  1358. /*
  1359. * udf_process_sequence
  1360. *
  1361. * PURPOSE
  1362. * Process a main/reserve volume descriptor sequence.
  1363. *
  1364. * PRE-CONDITIONS
  1365. * sb Pointer to _locked_ superblock.
  1366. * block First block of first extent of the sequence.
  1367. * lastblock Lastblock of first extent of the sequence.
  1368. *
  1369. * HISTORY
  1370. * July 1, 1997 - Andrew E. Mileski
  1371. * Written, tested, and released.
  1372. */
  1373. static noinline int udf_process_sequence(struct super_block *sb, long block,
  1374. long lastblock, kernel_lb_addr *fileset)
  1375. {
  1376. struct buffer_head *bh = NULL;
  1377. struct udf_vds_record vds[VDS_POS_LENGTH];
  1378. struct udf_vds_record *curr;
  1379. struct generic_desc *gd;
  1380. struct volDescPtr *vdp;
  1381. int done = 0;
  1382. uint32_t vdsn;
  1383. uint16_t ident;
  1384. long next_s = 0, next_e = 0;
  1385. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1386. /*
  1387. * Read the main descriptor sequence and find which descriptors
  1388. * are in it.
  1389. */
  1390. for (; (!done && block <= lastblock); block++) {
  1391. bh = udf_read_tagged(sb, block, block, &ident);
  1392. if (!bh) {
  1393. printk(KERN_ERR "udf: Block %Lu of volume descriptor "
  1394. "sequence is corrupted or we could not read "
  1395. "it.\n", (unsigned long long)block);
  1396. return 1;
  1397. }
  1398. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1399. gd = (struct generic_desc *)bh->b_data;
  1400. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1401. switch (ident) {
  1402. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1403. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1404. if (vdsn >= curr->volDescSeqNum) {
  1405. curr->volDescSeqNum = vdsn;
  1406. curr->block = block;
  1407. }
  1408. break;
  1409. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1410. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1411. if (vdsn >= curr->volDescSeqNum) {
  1412. curr->volDescSeqNum = vdsn;
  1413. curr->block = block;
  1414. vdp = (struct volDescPtr *)bh->b_data;
  1415. next_s = le32_to_cpu(
  1416. vdp->nextVolDescSeqExt.extLocation);
  1417. next_e = le32_to_cpu(
  1418. vdp->nextVolDescSeqExt.extLength);
  1419. next_e = next_e >> sb->s_blocksize_bits;
  1420. next_e += next_s;
  1421. }
  1422. break;
  1423. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1424. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1425. if (vdsn >= curr->volDescSeqNum) {
  1426. curr->volDescSeqNum = vdsn;
  1427. curr->block = block;
  1428. }
  1429. break;
  1430. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1431. curr = &vds[VDS_POS_PARTITION_DESC];
  1432. if (!curr->block)
  1433. curr->block = block;
  1434. break;
  1435. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1436. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1437. if (vdsn >= curr->volDescSeqNum) {
  1438. curr->volDescSeqNum = vdsn;
  1439. curr->block = block;
  1440. }
  1441. break;
  1442. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1443. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1444. if (vdsn >= curr->volDescSeqNum) {
  1445. curr->volDescSeqNum = vdsn;
  1446. curr->block = block;
  1447. }
  1448. break;
  1449. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1450. vds[VDS_POS_TERMINATING_DESC].block = block;
  1451. if (next_e) {
  1452. block = next_s;
  1453. lastblock = next_e;
  1454. next_s = next_e = 0;
  1455. } else
  1456. done = 1;
  1457. break;
  1458. }
  1459. brelse(bh);
  1460. }
  1461. /*
  1462. * Now read interesting descriptors again and process them
  1463. * in a suitable order
  1464. */
  1465. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1466. printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
  1467. return 1;
  1468. }
  1469. if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
  1470. return 1;
  1471. if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
  1472. vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
  1473. return 1;
  1474. if (vds[VDS_POS_PARTITION_DESC].block) {
  1475. /*
  1476. * We rescan the whole descriptor sequence to find
  1477. * partition descriptor blocks and process them.
  1478. */
  1479. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1480. block < vds[VDS_POS_TERMINATING_DESC].block;
  1481. block++)
  1482. if (udf_load_partdesc(sb, block))
  1483. return 1;
  1484. }
  1485. return 0;
  1486. }
  1487. /*
  1488. * udf_check_valid()
  1489. */
  1490. static int udf_check_valid(struct super_block *sb, int novrs, int silent)
  1491. {
  1492. long block;
  1493. struct udf_sb_info *sbi = UDF_SB(sb);
  1494. if (novrs) {
  1495. udf_debug("Validity check skipped because of novrs option\n");
  1496. return 0;
  1497. }
  1498. /* Check that it is NSR02 compliant */
  1499. /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
  1500. block = udf_vrs(sb, silent);
  1501. if (block == -1)
  1502. udf_debug("Failed to read byte 32768. Assuming open "
  1503. "disc. Skipping validity check\n");
  1504. if (block && !sbi->s_last_block)
  1505. sbi->s_last_block = udf_get_last_block(sb);
  1506. return !block;
  1507. }
  1508. static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
  1509. {
  1510. struct anchorVolDescPtr *anchor;
  1511. uint16_t ident;
  1512. struct buffer_head *bh;
  1513. long main_s, main_e, reserve_s, reserve_e;
  1514. int i;
  1515. struct udf_sb_info *sbi;
  1516. if (!sb)
  1517. return 1;
  1518. sbi = UDF_SB(sb);
  1519. for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
  1520. if (!sbi->s_anchor[i])
  1521. continue;
  1522. bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
  1523. &ident);
  1524. if (!bh)
  1525. continue;
  1526. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1527. /* Locate the main sequence */
  1528. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1529. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1530. main_e = main_e >> sb->s_blocksize_bits;
  1531. main_e += main_s;
  1532. /* Locate the reserve sequence */
  1533. reserve_s = le32_to_cpu(
  1534. anchor->reserveVolDescSeqExt.extLocation);
  1535. reserve_e = le32_to_cpu(
  1536. anchor->reserveVolDescSeqExt.extLength);
  1537. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1538. reserve_e += reserve_s;
  1539. brelse(bh);
  1540. /* Process the main & reserve sequences */
  1541. /* responsible for finding the PartitionDesc(s) */
  1542. if (!(udf_process_sequence(sb, main_s, main_e,
  1543. fileset) &&
  1544. udf_process_sequence(sb, reserve_s, reserve_e,
  1545. fileset)))
  1546. break;
  1547. }
  1548. if (i == ARRAY_SIZE(sbi->s_anchor)) {
  1549. udf_debug("No Anchor block found\n");
  1550. return 1;
  1551. }
  1552. udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
  1553. return 0;
  1554. }
  1555. static void udf_open_lvid(struct super_block *sb)
  1556. {
  1557. struct udf_sb_info *sbi = UDF_SB(sb);
  1558. struct buffer_head *bh = sbi->s_lvid_bh;
  1559. struct logicalVolIntegrityDesc *lvid;
  1560. struct logicalVolIntegrityDescImpUse *lvidiu;
  1561. if (!bh)
  1562. return;
  1563. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1564. lvidiu = udf_sb_lvidiu(sbi);
  1565. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1566. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1567. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1568. CURRENT_TIME);
  1569. lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
  1570. lvid->descTag.descCRC = cpu_to_le16(
  1571. crc_itu_t(0, (char *)lvid + sizeof(tag),
  1572. le16_to_cpu(lvid->descTag.descCRCLength)));
  1573. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1574. mark_buffer_dirty(bh);
  1575. }
  1576. static void udf_close_lvid(struct super_block *sb)
  1577. {
  1578. struct udf_sb_info *sbi = UDF_SB(sb);
  1579. struct buffer_head *bh = sbi->s_lvid_bh;
  1580. struct logicalVolIntegrityDesc *lvid;
  1581. struct logicalVolIntegrityDescImpUse *lvidiu;
  1582. if (!bh)
  1583. return;
  1584. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1585. if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
  1586. return;
  1587. lvidiu = udf_sb_lvidiu(sbi);
  1588. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1589. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1590. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1591. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1592. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1593. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1594. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1595. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1596. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1597. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1598. lvid->descTag.descCRC = cpu_to_le16(
  1599. crc_itu_t(0, (char *)lvid + sizeof(tag),
  1600. le16_to_cpu(lvid->descTag.descCRCLength)));
  1601. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1602. mark_buffer_dirty(bh);
  1603. }
  1604. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  1605. {
  1606. int i;
  1607. int nr_groups = bitmap->s_nr_groups;
  1608. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  1609. nr_groups);
  1610. for (i = 0; i < nr_groups; i++)
  1611. if (bitmap->s_block_bitmap[i])
  1612. brelse(bitmap->s_block_bitmap[i]);
  1613. if (size <= PAGE_SIZE)
  1614. kfree(bitmap);
  1615. else
  1616. vfree(bitmap);
  1617. }
  1618. static void udf_free_partition(struct udf_part_map *map)
  1619. {
  1620. int i;
  1621. struct udf_meta_data *mdata;
  1622. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  1623. iput(map->s_uspace.s_table);
  1624. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  1625. iput(map->s_fspace.s_table);
  1626. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  1627. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  1628. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  1629. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  1630. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  1631. for (i = 0; i < 4; i++)
  1632. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  1633. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  1634. mdata = &map->s_type_specific.s_metadata;
  1635. iput(mdata->s_metadata_fe);
  1636. mdata->s_metadata_fe = NULL;
  1637. iput(mdata->s_mirror_fe);
  1638. mdata->s_mirror_fe = NULL;
  1639. iput(mdata->s_bitmap_fe);
  1640. mdata->s_bitmap_fe = NULL;
  1641. }
  1642. }
  1643. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1644. {
  1645. int i;
  1646. struct inode *inode = NULL;
  1647. struct udf_options uopt;
  1648. kernel_lb_addr rootdir, fileset;
  1649. struct udf_sb_info *sbi;
  1650. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1651. uopt.uid = -1;
  1652. uopt.gid = -1;
  1653. uopt.umask = 0;
  1654. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1655. if (!sbi)
  1656. return -ENOMEM;
  1657. sb->s_fs_info = sbi;
  1658. mutex_init(&sbi->s_alloc_mutex);
  1659. if (!udf_parse_options((char *)options, &uopt, false))
  1660. goto error_out;
  1661. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1662. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1663. udf_error(sb, "udf_read_super",
  1664. "utf8 cannot be combined with iocharset\n");
  1665. goto error_out;
  1666. }
  1667. #ifdef CONFIG_UDF_NLS
  1668. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1669. uopt.nls_map = load_nls_default();
  1670. if (!uopt.nls_map)
  1671. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1672. else
  1673. udf_debug("Using default NLS map\n");
  1674. }
  1675. #endif
  1676. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1677. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1678. fileset.logicalBlockNum = 0xFFFFFFFF;
  1679. fileset.partitionReferenceNum = 0xFFFF;
  1680. sbi->s_flags = uopt.flags;
  1681. sbi->s_uid = uopt.uid;
  1682. sbi->s_gid = uopt.gid;
  1683. sbi->s_umask = uopt.umask;
  1684. sbi->s_nls_map = uopt.nls_map;
  1685. /* Set the block size for all transfers */
  1686. if (!sb_min_blocksize(sb, uopt.blocksize)) {
  1687. udf_debug("Bad block size (%d)\n", uopt.blocksize);
  1688. printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
  1689. goto error_out;
  1690. }
  1691. if (uopt.session == 0xFFFFFFFF)
  1692. sbi->s_session = udf_get_last_session(sb);
  1693. else
  1694. sbi->s_session = uopt.session;
  1695. udf_debug("Multi-session=%d\n", sbi->s_session);
  1696. sbi->s_last_block = uopt.lastblock;
  1697. sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
  1698. sbi->s_anchor[2] = uopt.anchor;
  1699. if (udf_check_valid(sb, uopt.novrs, silent)) {
  1700. /* read volume recognition sequences */
  1701. printk(KERN_WARNING "UDF-fs: No VRS found\n");
  1702. goto error_out;
  1703. }
  1704. udf_find_anchor(sb);
  1705. /* Fill in the rest of the superblock */
  1706. sb->s_op = &udf_sb_ops;
  1707. sb->s_export_op = &udf_export_ops;
  1708. sb->dq_op = NULL;
  1709. sb->s_dirt = 0;
  1710. sb->s_magic = UDF_SUPER_MAGIC;
  1711. sb->s_time_gran = 1000;
  1712. if (udf_load_sequence(sb, &fileset)) {
  1713. printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
  1714. goto error_out;
  1715. }
  1716. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1717. if (sbi->s_lvid_bh) {
  1718. struct logicalVolIntegrityDescImpUse *lvidiu =
  1719. udf_sb_lvidiu(sbi);
  1720. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1721. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1722. /* uint16_t maxUDFWriteRev =
  1723. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1724. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1725. printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
  1726. "(max is %x)\n",
  1727. le16_to_cpu(lvidiu->minUDFReadRev),
  1728. UDF_MAX_READ_VERSION);
  1729. goto error_out;
  1730. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1731. sb->s_flags |= MS_RDONLY;
  1732. sbi->s_udfrev = minUDFWriteRev;
  1733. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1734. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1735. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1736. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1737. }
  1738. if (!sbi->s_partitions) {
  1739. printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
  1740. goto error_out;
  1741. }
  1742. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1743. UDF_PART_FLAG_READ_ONLY) {
  1744. printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
  1745. "forcing readonly mount\n");
  1746. sb->s_flags |= MS_RDONLY;
  1747. }
  1748. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1749. printk(KERN_WARNING "UDF-fs: No fileset found\n");
  1750. goto error_out;
  1751. }
  1752. if (!silent) {
  1753. timestamp ts;
  1754. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1755. udf_info("UDF: Mounting volume '%s', "
  1756. "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1757. sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
  1758. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1759. }
  1760. if (!(sb->s_flags & MS_RDONLY))
  1761. udf_open_lvid(sb);
  1762. /* Assign the root inode */
  1763. /* assign inodes by physical block number */
  1764. /* perhaps it's not extensible enough, but for now ... */
  1765. inode = udf_iget(sb, rootdir);
  1766. if (!inode) {
  1767. printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
  1768. "partition=%d\n",
  1769. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1770. goto error_out;
  1771. }
  1772. /* Allocate a dentry for the root inode */
  1773. sb->s_root = d_alloc_root(inode);
  1774. if (!sb->s_root) {
  1775. printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
  1776. iput(inode);
  1777. goto error_out;
  1778. }
  1779. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1780. return 0;
  1781. error_out:
  1782. if (sbi->s_vat_inode)
  1783. iput(sbi->s_vat_inode);
  1784. if (sbi->s_partitions)
  1785. for (i = 0; i < sbi->s_partitions; i++)
  1786. udf_free_partition(&sbi->s_partmaps[i]);
  1787. #ifdef CONFIG_UDF_NLS
  1788. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1789. unload_nls(sbi->s_nls_map);
  1790. #endif
  1791. if (!(sb->s_flags & MS_RDONLY))
  1792. udf_close_lvid(sb);
  1793. brelse(sbi->s_lvid_bh);
  1794. kfree(sbi->s_partmaps);
  1795. kfree(sbi);
  1796. sb->s_fs_info = NULL;
  1797. return -EINVAL;
  1798. }
  1799. static void udf_error(struct super_block *sb, const char *function,
  1800. const char *fmt, ...)
  1801. {
  1802. va_list args;
  1803. if (!(sb->s_flags & MS_RDONLY)) {
  1804. /* mark sb error */
  1805. sb->s_dirt = 1;
  1806. }
  1807. va_start(args, fmt);
  1808. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1809. va_end(args);
  1810. printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
  1811. sb->s_id, function, error_buf);
  1812. }
  1813. void udf_warning(struct super_block *sb, const char *function,
  1814. const char *fmt, ...)
  1815. {
  1816. va_list args;
  1817. va_start(args, fmt);
  1818. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1819. va_end(args);
  1820. printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
  1821. sb->s_id, function, error_buf);
  1822. }
  1823. static void udf_put_super(struct super_block *sb)
  1824. {
  1825. int i;
  1826. struct udf_sb_info *sbi;
  1827. sbi = UDF_SB(sb);
  1828. if (sbi->s_vat_inode)
  1829. iput(sbi->s_vat_inode);
  1830. if (sbi->s_partitions)
  1831. for (i = 0; i < sbi->s_partitions; i++)
  1832. udf_free_partition(&sbi->s_partmaps[i]);
  1833. #ifdef CONFIG_UDF_NLS
  1834. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1835. unload_nls(sbi->s_nls_map);
  1836. #endif
  1837. if (!(sb->s_flags & MS_RDONLY))
  1838. udf_close_lvid(sb);
  1839. brelse(sbi->s_lvid_bh);
  1840. kfree(sbi->s_partmaps);
  1841. kfree(sb->s_fs_info);
  1842. sb->s_fs_info = NULL;
  1843. }
  1844. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1845. {
  1846. struct super_block *sb = dentry->d_sb;
  1847. struct udf_sb_info *sbi = UDF_SB(sb);
  1848. struct logicalVolIntegrityDescImpUse *lvidiu;
  1849. if (sbi->s_lvid_bh != NULL)
  1850. lvidiu = udf_sb_lvidiu(sbi);
  1851. else
  1852. lvidiu = NULL;
  1853. buf->f_type = UDF_SUPER_MAGIC;
  1854. buf->f_bsize = sb->s_blocksize;
  1855. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1856. buf->f_bfree = udf_count_free(sb);
  1857. buf->f_bavail = buf->f_bfree;
  1858. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1859. le32_to_cpu(lvidiu->numDirs)) : 0)
  1860. + buf->f_bfree;
  1861. buf->f_ffree = buf->f_bfree;
  1862. /* __kernel_fsid_t f_fsid */
  1863. buf->f_namelen = UDF_NAME_LEN - 2;
  1864. return 0;
  1865. }
  1866. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1867. struct udf_bitmap *bitmap)
  1868. {
  1869. struct buffer_head *bh = NULL;
  1870. unsigned int accum = 0;
  1871. int index;
  1872. int block = 0, newblock;
  1873. kernel_lb_addr loc;
  1874. uint32_t bytes;
  1875. uint8_t *ptr;
  1876. uint16_t ident;
  1877. struct spaceBitmapDesc *bm;
  1878. lock_kernel();
  1879. loc.logicalBlockNum = bitmap->s_extPosition;
  1880. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1881. bh = udf_read_ptagged(sb, loc, 0, &ident);
  1882. if (!bh) {
  1883. printk(KERN_ERR "udf: udf_count_free failed\n");
  1884. goto out;
  1885. } else if (ident != TAG_IDENT_SBD) {
  1886. brelse(bh);
  1887. printk(KERN_ERR "udf: udf_count_free failed\n");
  1888. goto out;
  1889. }
  1890. bm = (struct spaceBitmapDesc *)bh->b_data;
  1891. bytes = le32_to_cpu(bm->numOfBytes);
  1892. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1893. ptr = (uint8_t *)bh->b_data;
  1894. while (bytes > 0) {
  1895. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1896. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1897. cur_bytes * 8);
  1898. bytes -= cur_bytes;
  1899. if (bytes) {
  1900. brelse(bh);
  1901. newblock = udf_get_lb_pblock(sb, loc, ++block);
  1902. bh = udf_tread(sb, newblock);
  1903. if (!bh) {
  1904. udf_debug("read failed\n");
  1905. goto out;
  1906. }
  1907. index = 0;
  1908. ptr = (uint8_t *)bh->b_data;
  1909. }
  1910. }
  1911. brelse(bh);
  1912. out:
  1913. unlock_kernel();
  1914. return accum;
  1915. }
  1916. static unsigned int udf_count_free_table(struct super_block *sb,
  1917. struct inode *table)
  1918. {
  1919. unsigned int accum = 0;
  1920. uint32_t elen;
  1921. kernel_lb_addr eloc;
  1922. int8_t etype;
  1923. struct extent_position epos;
  1924. lock_kernel();
  1925. epos.block = UDF_I(table)->i_location;
  1926. epos.offset = sizeof(struct unallocSpaceEntry);
  1927. epos.bh = NULL;
  1928. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  1929. accum += (elen >> table->i_sb->s_blocksize_bits);
  1930. brelse(epos.bh);
  1931. unlock_kernel();
  1932. return accum;
  1933. }
  1934. static unsigned int udf_count_free(struct super_block *sb)
  1935. {
  1936. unsigned int accum = 0;
  1937. struct udf_sb_info *sbi;
  1938. struct udf_part_map *map;
  1939. sbi = UDF_SB(sb);
  1940. if (sbi->s_lvid_bh) {
  1941. struct logicalVolIntegrityDesc *lvid =
  1942. (struct logicalVolIntegrityDesc *)
  1943. sbi->s_lvid_bh->b_data;
  1944. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  1945. accum = le32_to_cpu(
  1946. lvid->freeSpaceTable[sbi->s_partition]);
  1947. if (accum == 0xFFFFFFFF)
  1948. accum = 0;
  1949. }
  1950. }
  1951. if (accum)
  1952. return accum;
  1953. map = &sbi->s_partmaps[sbi->s_partition];
  1954. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  1955. accum += udf_count_free_bitmap(sb,
  1956. map->s_uspace.s_bitmap);
  1957. }
  1958. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  1959. accum += udf_count_free_bitmap(sb,
  1960. map->s_fspace.s_bitmap);
  1961. }
  1962. if (accum)
  1963. return accum;
  1964. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  1965. accum += udf_count_free_table(sb,
  1966. map->s_uspace.s_table);
  1967. }
  1968. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  1969. accum += udf_count_free_table(sb,
  1970. map->s_fspace.s_table);
  1971. }
  1972. return accum;
  1973. }