super.c 59 KB

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