super.c 51 KB

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