super.c 54 KB

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