super.c 51 KB

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