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