super.c 51 KB

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