super.c 53 KB

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