super.c 54 KB

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