super.c 54 KB

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