super.c 54 KB

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