super.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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. * https://www.ecma.ch/
  15. * https://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/vfs.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/errno.h>
  52. #include <linux/mount.h>
  53. #include <linux/seq_file.h>
  54. #include <linux/bitmap.h>
  55. #include <linux/crc-itu-t.h>
  56. #include <linux/log2.h>
  57. #include <asm/byteorder.h>
  58. #include <linux/iversion.h>
  59. #include "udf_sb.h"
  60. #include "udf_i.h"
  61. #include <linux/init.h>
  62. #include <linux/uaccess.h>
  63. enum {
  64. VDS_POS_PRIMARY_VOL_DESC,
  65. VDS_POS_UNALLOC_SPACE_DESC,
  66. VDS_POS_LOGICAL_VOL_DESC,
  67. VDS_POS_IMP_USE_VOL_DESC,
  68. VDS_POS_LENGTH
  69. };
  70. #define VSD_FIRST_SECTOR_OFFSET 32768
  71. #define VSD_MAX_SECTOR_OFFSET 0x800000
  72. /*
  73. * Maximum number of Terminating Descriptor / Logical Volume Integrity
  74. * Descriptor redirections. The chosen numbers are arbitrary - just that we
  75. * hopefully don't limit any real use of rewritten inode on write-once media
  76. * but avoid looping for too long on corrupted media.
  77. */
  78. #define UDF_MAX_TD_NESTING 64
  79. #define UDF_MAX_LVID_NESTING 1000
  80. enum { UDF_MAX_LINKS = 0xffff };
  81. /* These are the "meat" - everything else is stuffing */
  82. static int udf_fill_super(struct super_block *, void *, int);
  83. static void udf_put_super(struct super_block *);
  84. static int udf_sync_fs(struct super_block *, int);
  85. static int udf_remount_fs(struct super_block *, int *, char *);
  86. static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
  87. static void udf_open_lvid(struct super_block *);
  88. static void udf_close_lvid(struct super_block *);
  89. static unsigned int udf_count_free(struct super_block *);
  90. static int udf_statfs(struct dentry *, struct kstatfs *);
  91. static int udf_show_options(struct seq_file *, struct dentry *);
  92. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
  93. {
  94. struct logicalVolIntegrityDesc *lvid;
  95. unsigned int partnum;
  96. unsigned int offset;
  97. if (!UDF_SB(sb)->s_lvid_bh)
  98. return NULL;
  99. lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
  100. partnum = le32_to_cpu(lvid->numOfPartitions);
  101. /* The offset is to skip freeSpaceTable and sizeTable arrays */
  102. offset = partnum * 2 * sizeof(uint32_t);
  103. return (struct logicalVolIntegrityDescImpUse *)
  104. (((uint8_t *)(lvid + 1)) + offset);
  105. }
  106. /* UDF filesystem type */
  107. static struct dentry *udf_mount(struct file_system_type *fs_type,
  108. int flags, const char *dev_name, void *data)
  109. {
  110. return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
  111. }
  112. static struct file_system_type udf_fstype = {
  113. .owner = THIS_MODULE,
  114. .name = "udf",
  115. .mount = udf_mount,
  116. .kill_sb = kill_block_super,
  117. .fs_flags = FS_REQUIRES_DEV,
  118. };
  119. MODULE_ALIAS_FS("udf");
  120. static struct kmem_cache *udf_inode_cachep;
  121. static struct inode *udf_alloc_inode(struct super_block *sb)
  122. {
  123. struct udf_inode_info *ei;
  124. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  125. if (!ei)
  126. return NULL;
  127. ei->i_unique = 0;
  128. ei->i_lenExtents = 0;
  129. ei->i_lenStreams = 0;
  130. ei->i_next_alloc_block = 0;
  131. ei->i_next_alloc_goal = 0;
  132. ei->i_strat4096 = 0;
  133. ei->i_streamdir = 0;
  134. init_rwsem(&ei->i_data_sem);
  135. ei->cached_extent.lstart = -1;
  136. spin_lock_init(&ei->i_extent_cache_lock);
  137. inode_set_iversion(&ei->vfs_inode, 1);
  138. return &ei->vfs_inode;
  139. }
  140. static void udf_free_in_core_inode(struct inode *inode)
  141. {
  142. kmem_cache_free(udf_inode_cachep, UDF_I(inode));
  143. }
  144. static void init_once(void *foo)
  145. {
  146. struct udf_inode_info *ei = (struct udf_inode_info *)foo;
  147. ei->i_data = NULL;
  148. inode_init_once(&ei->vfs_inode);
  149. }
  150. static int __init init_inodecache(void)
  151. {
  152. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  153. sizeof(struct udf_inode_info),
  154. 0, (SLAB_RECLAIM_ACCOUNT |
  155. SLAB_MEM_SPREAD |
  156. SLAB_ACCOUNT),
  157. init_once);
  158. if (!udf_inode_cachep)
  159. return -ENOMEM;
  160. return 0;
  161. }
  162. static void destroy_inodecache(void)
  163. {
  164. /*
  165. * Make sure all delayed rcu free inodes are flushed before we
  166. * destroy cache.
  167. */
  168. rcu_barrier();
  169. kmem_cache_destroy(udf_inode_cachep);
  170. }
  171. /* Superblock operations */
  172. static const struct super_operations udf_sb_ops = {
  173. .alloc_inode = udf_alloc_inode,
  174. .free_inode = udf_free_in_core_inode,
  175. .write_inode = udf_write_inode,
  176. .evict_inode = udf_evict_inode,
  177. .put_super = udf_put_super,
  178. .sync_fs = udf_sync_fs,
  179. .statfs = udf_statfs,
  180. .remount_fs = udf_remount_fs,
  181. .show_options = udf_show_options,
  182. };
  183. struct udf_options {
  184. unsigned char novrs;
  185. unsigned int blocksize;
  186. unsigned int session;
  187. unsigned int lastblock;
  188. unsigned int anchor;
  189. unsigned int flags;
  190. umode_t umask;
  191. kgid_t gid;
  192. kuid_t uid;
  193. umode_t fmode;
  194. umode_t dmode;
  195. struct nls_table *nls_map;
  196. };
  197. static int __init init_udf_fs(void)
  198. {
  199. int err;
  200. err = init_inodecache();
  201. if (err)
  202. goto out1;
  203. err = register_filesystem(&udf_fstype);
  204. if (err)
  205. goto out;
  206. return 0;
  207. out:
  208. destroy_inodecache();
  209. out1:
  210. return err;
  211. }
  212. static void __exit exit_udf_fs(void)
  213. {
  214. unregister_filesystem(&udf_fstype);
  215. destroy_inodecache();
  216. }
  217. static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
  218. {
  219. struct udf_sb_info *sbi = UDF_SB(sb);
  220. sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
  221. if (!sbi->s_partmaps) {
  222. sbi->s_partitions = 0;
  223. return -ENOMEM;
  224. }
  225. sbi->s_partitions = count;
  226. return 0;
  227. }
  228. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  229. {
  230. int i;
  231. int nr_groups = bitmap->s_nr_groups;
  232. for (i = 0; i < nr_groups; i++)
  233. brelse(bitmap->s_block_bitmap[i]);
  234. kvfree(bitmap);
  235. }
  236. static void udf_free_partition(struct udf_part_map *map)
  237. {
  238. int i;
  239. struct udf_meta_data *mdata;
  240. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  241. iput(map->s_uspace.s_table);
  242. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  243. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  244. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  245. for (i = 0; i < 4; i++)
  246. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  247. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  248. mdata = &map->s_type_specific.s_metadata;
  249. iput(mdata->s_metadata_fe);
  250. mdata->s_metadata_fe = NULL;
  251. iput(mdata->s_mirror_fe);
  252. mdata->s_mirror_fe = NULL;
  253. iput(mdata->s_bitmap_fe);
  254. mdata->s_bitmap_fe = NULL;
  255. }
  256. }
  257. static void udf_sb_free_partitions(struct super_block *sb)
  258. {
  259. struct udf_sb_info *sbi = UDF_SB(sb);
  260. int i;
  261. if (!sbi->s_partmaps)
  262. return;
  263. for (i = 0; i < sbi->s_partitions; i++)
  264. udf_free_partition(&sbi->s_partmaps[i]);
  265. kfree(sbi->s_partmaps);
  266. sbi->s_partmaps = NULL;
  267. }
  268. static int udf_show_options(struct seq_file *seq, struct dentry *root)
  269. {
  270. struct super_block *sb = root->d_sb;
  271. struct udf_sb_info *sbi = UDF_SB(sb);
  272. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
  273. seq_puts(seq, ",nostrict");
  274. if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
  275. seq_printf(seq, ",bs=%lu", sb->s_blocksize);
  276. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  277. seq_puts(seq, ",unhide");
  278. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  279. seq_puts(seq, ",undelete");
  280. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
  281. seq_puts(seq, ",noadinicb");
  282. if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
  283. seq_puts(seq, ",shortad");
  284. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
  285. seq_puts(seq, ",uid=forget");
  286. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
  287. seq_puts(seq, ",gid=forget");
  288. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
  289. seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
  290. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
  291. seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
  292. if (sbi->s_umask != 0)
  293. seq_printf(seq, ",umask=%ho", sbi->s_umask);
  294. if (sbi->s_fmode != UDF_INVALID_MODE)
  295. seq_printf(seq, ",mode=%ho", sbi->s_fmode);
  296. if (sbi->s_dmode != UDF_INVALID_MODE)
  297. seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
  298. if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
  299. seq_printf(seq, ",session=%d", sbi->s_session);
  300. if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
  301. seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
  302. if (sbi->s_anchor != 0)
  303. seq_printf(seq, ",anchor=%u", sbi->s_anchor);
  304. if (sbi->s_nls_map)
  305. seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
  306. else
  307. seq_puts(seq, ",iocharset=utf8");
  308. return 0;
  309. }
  310. /*
  311. * udf_parse_options
  312. *
  313. * PURPOSE
  314. * Parse mount options.
  315. *
  316. * DESCRIPTION
  317. * The following mount options are supported:
  318. *
  319. * gid= Set the default group.
  320. * umask= Set the default umask.
  321. * mode= Set the default file permissions.
  322. * dmode= Set the default directory permissions.
  323. * uid= Set the default user.
  324. * bs= Set the block size.
  325. * unhide Show otherwise hidden files.
  326. * undelete Show deleted files in lists.
  327. * adinicb Embed data in the inode (default)
  328. * noadinicb Don't embed data in the inode
  329. * shortad Use short ad's
  330. * longad Use long ad's (default)
  331. * nostrict Unset strict conformance
  332. * iocharset= Set the NLS character set
  333. *
  334. * The remaining are for debugging and disaster recovery:
  335. *
  336. * novrs Skip volume sequence recognition
  337. *
  338. * The following expect a offset from 0.
  339. *
  340. * session= Set the CDROM session (default= last session)
  341. * anchor= Override standard anchor location. (default= 256)
  342. * volume= Override the VolumeDesc location. (unused)
  343. * partition= Override the PartitionDesc location. (unused)
  344. * lastblock= Set the last block of the filesystem/
  345. *
  346. * The following expect a offset from the partition root.
  347. *
  348. * fileset= Override the fileset block location. (unused)
  349. * rootdir= Override the root directory location. (unused)
  350. * WARNING: overriding the rootdir to a non-directory may
  351. * yield highly unpredictable results.
  352. *
  353. * PRE-CONDITIONS
  354. * options Pointer to mount options string.
  355. * uopts Pointer to mount options variable.
  356. *
  357. * POST-CONDITIONS
  358. * <return> 1 Mount options parsed okay.
  359. * <return> 0 Error parsing mount options.
  360. *
  361. * HISTORY
  362. * July 1, 1997 - Andrew E. Mileski
  363. * Written, tested, and released.
  364. */
  365. enum {
  366. Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
  367. Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
  368. Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
  369. Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
  370. Opt_rootdir, Opt_utf8, Opt_iocharset,
  371. Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
  372. Opt_fmode, Opt_dmode
  373. };
  374. static const match_table_t tokens = {
  375. {Opt_novrs, "novrs"},
  376. {Opt_nostrict, "nostrict"},
  377. {Opt_bs, "bs=%u"},
  378. {Opt_unhide, "unhide"},
  379. {Opt_undelete, "undelete"},
  380. {Opt_noadinicb, "noadinicb"},
  381. {Opt_adinicb, "adinicb"},
  382. {Opt_shortad, "shortad"},
  383. {Opt_longad, "longad"},
  384. {Opt_uforget, "uid=forget"},
  385. {Opt_uignore, "uid=ignore"},
  386. {Opt_gforget, "gid=forget"},
  387. {Opt_gignore, "gid=ignore"},
  388. {Opt_gid, "gid=%u"},
  389. {Opt_uid, "uid=%u"},
  390. {Opt_umask, "umask=%o"},
  391. {Opt_session, "session=%u"},
  392. {Opt_lastblock, "lastblock=%u"},
  393. {Opt_anchor, "anchor=%u"},
  394. {Opt_volume, "volume=%u"},
  395. {Opt_partition, "partition=%u"},
  396. {Opt_fileset, "fileset=%u"},
  397. {Opt_rootdir, "rootdir=%u"},
  398. {Opt_utf8, "utf8"},
  399. {Opt_iocharset, "iocharset=%s"},
  400. {Opt_fmode, "mode=%o"},
  401. {Opt_dmode, "dmode=%o"},
  402. {Opt_err, NULL}
  403. };
  404. static int udf_parse_options(char *options, struct udf_options *uopt,
  405. bool remount)
  406. {
  407. char *p;
  408. int option;
  409. uopt->novrs = 0;
  410. uopt->session = 0xFFFFFFFF;
  411. uopt->lastblock = 0;
  412. uopt->anchor = 0;
  413. if (!options)
  414. return 1;
  415. while ((p = strsep(&options, ",")) != NULL) {
  416. substring_t args[MAX_OPT_ARGS];
  417. int token;
  418. unsigned n;
  419. if (!*p)
  420. continue;
  421. token = match_token(p, tokens, args);
  422. switch (token) {
  423. case Opt_novrs:
  424. uopt->novrs = 1;
  425. break;
  426. case Opt_bs:
  427. if (match_int(&args[0], &option))
  428. return 0;
  429. n = option;
  430. if (n != 512 && n != 1024 && n != 2048 && n != 4096)
  431. return 0;
  432. uopt->blocksize = n;
  433. uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
  434. break;
  435. case Opt_unhide:
  436. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  437. break;
  438. case Opt_undelete:
  439. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  440. break;
  441. case Opt_noadinicb:
  442. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  443. break;
  444. case Opt_adinicb:
  445. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  446. break;
  447. case Opt_shortad:
  448. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  449. break;
  450. case Opt_longad:
  451. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  452. break;
  453. case Opt_gid:
  454. if (match_int(args, &option))
  455. return 0;
  456. uopt->gid = make_kgid(current_user_ns(), option);
  457. if (!gid_valid(uopt->gid))
  458. return 0;
  459. uopt->flags |= (1 << UDF_FLAG_GID_SET);
  460. break;
  461. case Opt_uid:
  462. if (match_int(args, &option))
  463. return 0;
  464. uopt->uid = make_kuid(current_user_ns(), option);
  465. if (!uid_valid(uopt->uid))
  466. return 0;
  467. uopt->flags |= (1 << UDF_FLAG_UID_SET);
  468. break;
  469. case Opt_umask:
  470. if (match_octal(args, &option))
  471. return 0;
  472. uopt->umask = option;
  473. break;
  474. case Opt_nostrict:
  475. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  476. break;
  477. case Opt_session:
  478. if (match_int(args, &option))
  479. return 0;
  480. uopt->session = option;
  481. if (!remount)
  482. uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
  483. break;
  484. case Opt_lastblock:
  485. if (match_int(args, &option))
  486. return 0;
  487. uopt->lastblock = option;
  488. if (!remount)
  489. uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
  490. break;
  491. case Opt_anchor:
  492. if (match_int(args, &option))
  493. return 0;
  494. uopt->anchor = option;
  495. break;
  496. case Opt_volume:
  497. case Opt_partition:
  498. case Opt_fileset:
  499. case Opt_rootdir:
  500. /* Ignored (never implemented properly) */
  501. break;
  502. case Opt_utf8:
  503. if (!remount) {
  504. unload_nls(uopt->nls_map);
  505. uopt->nls_map = NULL;
  506. }
  507. break;
  508. case Opt_iocharset:
  509. if (!remount) {
  510. unload_nls(uopt->nls_map);
  511. uopt->nls_map = NULL;
  512. }
  513. /* When nls_map is not loaded then UTF-8 is used */
  514. if (!remount && strcmp(args[0].from, "utf8") != 0) {
  515. uopt->nls_map = load_nls(args[0].from);
  516. if (!uopt->nls_map) {
  517. pr_err("iocharset %s not found\n",
  518. args[0].from);
  519. return 0;
  520. }
  521. }
  522. break;
  523. case Opt_uforget:
  524. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  525. break;
  526. case Opt_uignore:
  527. case Opt_gignore:
  528. /* These options are superseeded by uid=<number> */
  529. break;
  530. case Opt_gforget:
  531. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  532. break;
  533. case Opt_fmode:
  534. if (match_octal(args, &option))
  535. return 0;
  536. uopt->fmode = option & 0777;
  537. break;
  538. case Opt_dmode:
  539. if (match_octal(args, &option))
  540. return 0;
  541. uopt->dmode = option & 0777;
  542. break;
  543. default:
  544. pr_err("bad mount option \"%s\" or missing value\n", p);
  545. return 0;
  546. }
  547. }
  548. return 1;
  549. }
  550. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  551. {
  552. struct udf_options uopt;
  553. struct udf_sb_info *sbi = UDF_SB(sb);
  554. int error = 0;
  555. if (!(*flags & SB_RDONLY) && UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
  556. return -EACCES;
  557. sync_filesystem(sb);
  558. uopt.flags = sbi->s_flags;
  559. uopt.uid = sbi->s_uid;
  560. uopt.gid = sbi->s_gid;
  561. uopt.umask = sbi->s_umask;
  562. uopt.fmode = sbi->s_fmode;
  563. uopt.dmode = sbi->s_dmode;
  564. uopt.nls_map = NULL;
  565. if (!udf_parse_options(options, &uopt, true))
  566. return -EINVAL;
  567. write_lock(&sbi->s_cred_lock);
  568. sbi->s_flags = uopt.flags;
  569. sbi->s_uid = uopt.uid;
  570. sbi->s_gid = uopt.gid;
  571. sbi->s_umask = uopt.umask;
  572. sbi->s_fmode = uopt.fmode;
  573. sbi->s_dmode = uopt.dmode;
  574. write_unlock(&sbi->s_cred_lock);
  575. if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
  576. goto out_unlock;
  577. if (*flags & SB_RDONLY)
  578. udf_close_lvid(sb);
  579. else
  580. udf_open_lvid(sb);
  581. out_unlock:
  582. return error;
  583. }
  584. /*
  585. * Check VSD descriptor. Returns -1 in case we are at the end of volume
  586. * recognition area, 0 if the descriptor is valid but non-interesting, 1 if
  587. * we found one of NSR descriptors we are looking for.
  588. */
  589. static int identify_vsd(const struct volStructDesc *vsd)
  590. {
  591. int ret = 0;
  592. if (!memcmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN)) {
  593. switch (vsd->structType) {
  594. case 0:
  595. udf_debug("ISO9660 Boot Record found\n");
  596. break;
  597. case 1:
  598. udf_debug("ISO9660 Primary Volume Descriptor found\n");
  599. break;
  600. case 2:
  601. udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
  602. break;
  603. case 3:
  604. udf_debug("ISO9660 Volume Partition Descriptor found\n");
  605. break;
  606. case 255:
  607. udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
  608. break;
  609. default:
  610. udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
  611. break;
  612. }
  613. } else if (!memcmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN))
  614. ; /* ret = 0 */
  615. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN))
  616. ret = 1;
  617. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN))
  618. ret = 1;
  619. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_BOOT2, VSD_STD_ID_LEN))
  620. ; /* ret = 0 */
  621. else if (!memcmp(vsd->stdIdent, VSD_STD_ID_CDW02, VSD_STD_ID_LEN))
  622. ; /* ret = 0 */
  623. else {
  624. /* TEA01 or invalid id : end of volume recognition area */
  625. ret = -1;
  626. }
  627. return ret;
  628. }
  629. /*
  630. * Check Volume Structure Descriptors (ECMA 167 2/9.1)
  631. * We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1)
  632. * @return 1 if NSR02 or NSR03 found,
  633. * -1 if first sector read error, 0 otherwise
  634. */
  635. static int udf_check_vsd(struct super_block *sb)
  636. {
  637. struct volStructDesc *vsd = NULL;
  638. loff_t sector = VSD_FIRST_SECTOR_OFFSET;
  639. int sectorsize;
  640. struct buffer_head *bh = NULL;
  641. int nsr = 0;
  642. struct udf_sb_info *sbi;
  643. loff_t session_offset;
  644. sbi = UDF_SB(sb);
  645. if (sb->s_blocksize < sizeof(struct volStructDesc))
  646. sectorsize = sizeof(struct volStructDesc);
  647. else
  648. sectorsize = sb->s_blocksize;
  649. session_offset = (loff_t)sbi->s_session << sb->s_blocksize_bits;
  650. sector += session_offset;
  651. udf_debug("Starting at sector %u (%lu byte sectors)\n",
  652. (unsigned int)(sector >> sb->s_blocksize_bits),
  653. sb->s_blocksize);
  654. /* Process the sequence (if applicable). The hard limit on the sector
  655. * offset is arbitrary, hopefully large enough so that all valid UDF
  656. * filesystems will be recognised. There is no mention of an upper
  657. * bound to the size of the volume recognition area in the standard.
  658. * The limit will prevent the code to read all the sectors of a
  659. * specially crafted image (like a bluray disc full of CD001 sectors),
  660. * potentially causing minutes or even hours of uninterruptible I/O
  661. * activity. This actually happened with uninitialised SSD partitions
  662. * (all 0xFF) before the check for the limit and all valid IDs were
  663. * added */
  664. for (; !nsr && sector < VSD_MAX_SECTOR_OFFSET; sector += sectorsize) {
  665. /* Read a block */
  666. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  667. if (!bh)
  668. break;
  669. vsd = (struct volStructDesc *)(bh->b_data +
  670. (sector & (sb->s_blocksize - 1)));
  671. nsr = identify_vsd(vsd);
  672. /* Found NSR or end? */
  673. if (nsr) {
  674. brelse(bh);
  675. break;
  676. }
  677. /*
  678. * Special handling for improperly formatted VRS (e.g., Win10)
  679. * where components are separated by 2048 bytes even though
  680. * sectors are 4K
  681. */
  682. if (sb->s_blocksize == 4096) {
  683. nsr = identify_vsd(vsd + 1);
  684. /* Ignore unknown IDs... */
  685. if (nsr < 0)
  686. nsr = 0;
  687. }
  688. brelse(bh);
  689. }
  690. if (nsr > 0)
  691. return 1;
  692. else if (!bh && sector - session_offset == VSD_FIRST_SECTOR_OFFSET)
  693. return -1;
  694. else
  695. return 0;
  696. }
  697. static int udf_verify_domain_identifier(struct super_block *sb,
  698. struct regid *ident, char *dname)
  699. {
  700. struct domainIdentSuffix *suffix;
  701. if (memcmp(ident->ident, UDF_ID_COMPLIANT, strlen(UDF_ID_COMPLIANT))) {
  702. udf_warn(sb, "Not OSTA UDF compliant %s descriptor.\n", dname);
  703. goto force_ro;
  704. }
  705. if (ident->flags & ENTITYID_FLAGS_DIRTY) {
  706. udf_warn(sb, "Possibly not OSTA UDF compliant %s descriptor.\n",
  707. dname);
  708. goto force_ro;
  709. }
  710. suffix = (struct domainIdentSuffix *)ident->identSuffix;
  711. if ((suffix->domainFlags & DOMAIN_FLAGS_HARD_WRITE_PROTECT) ||
  712. (suffix->domainFlags & DOMAIN_FLAGS_SOFT_WRITE_PROTECT)) {
  713. if (!sb_rdonly(sb)) {
  714. udf_warn(sb, "Descriptor for %s marked write protected."
  715. " Forcing read only mount.\n", dname);
  716. }
  717. goto force_ro;
  718. }
  719. return 0;
  720. force_ro:
  721. if (!sb_rdonly(sb))
  722. return -EACCES;
  723. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  724. return 0;
  725. }
  726. static int udf_load_fileset(struct super_block *sb, struct fileSetDesc *fset,
  727. struct kernel_lb_addr *root)
  728. {
  729. int ret;
  730. ret = udf_verify_domain_identifier(sb, &fset->domainIdent, "file set");
  731. if (ret < 0)
  732. return ret;
  733. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  734. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  735. udf_debug("Rootdir at block=%u, partition=%u\n",
  736. root->logicalBlockNum, root->partitionReferenceNum);
  737. return 0;
  738. }
  739. static int udf_find_fileset(struct super_block *sb,
  740. struct kernel_lb_addr *fileset,
  741. struct kernel_lb_addr *root)
  742. {
  743. struct buffer_head *bh = NULL;
  744. uint16_t ident;
  745. int ret;
  746. if (fileset->logicalBlockNum == 0xFFFFFFFF &&
  747. fileset->partitionReferenceNum == 0xFFFF)
  748. return -EINVAL;
  749. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  750. if (!bh)
  751. return -EIO;
  752. if (ident != TAG_IDENT_FSD) {
  753. brelse(bh);
  754. return -EINVAL;
  755. }
  756. udf_debug("Fileset at block=%u, partition=%u\n",
  757. fileset->logicalBlockNum, fileset->partitionReferenceNum);
  758. UDF_SB(sb)->s_partition = fileset->partitionReferenceNum;
  759. ret = udf_load_fileset(sb, (struct fileSetDesc *)bh->b_data, root);
  760. brelse(bh);
  761. return ret;
  762. }
  763. /*
  764. * Load primary Volume Descriptor Sequence
  765. *
  766. * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
  767. * should be tried.
  768. */
  769. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  770. {
  771. struct primaryVolDesc *pvoldesc;
  772. uint8_t *outstr;
  773. struct buffer_head *bh;
  774. uint16_t ident;
  775. int ret;
  776. struct timestamp *ts;
  777. outstr = kmalloc(128, GFP_NOFS);
  778. if (!outstr)
  779. return -ENOMEM;
  780. bh = udf_read_tagged(sb, block, block, &ident);
  781. if (!bh) {
  782. ret = -EAGAIN;
  783. goto out2;
  784. }
  785. if (ident != TAG_IDENT_PVD) {
  786. ret = -EIO;
  787. goto out_bh;
  788. }
  789. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  790. udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  791. pvoldesc->recordingDateAndTime);
  792. ts = &pvoldesc->recordingDateAndTime;
  793. udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
  794. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  795. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  796. ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
  797. if (ret < 0) {
  798. strcpy(UDF_SB(sb)->s_volume_ident, "InvalidName");
  799. pr_warn("incorrect volume identification, setting to "
  800. "'InvalidName'\n");
  801. } else {
  802. strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
  803. }
  804. udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
  805. ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128);
  806. if (ret < 0) {
  807. ret = 0;
  808. goto out_bh;
  809. }
  810. outstr[ret] = 0;
  811. udf_debug("volSetIdent[] = '%s'\n", outstr);
  812. ret = 0;
  813. out_bh:
  814. brelse(bh);
  815. out2:
  816. kfree(outstr);
  817. return ret;
  818. }
  819. struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
  820. u32 meta_file_loc, u32 partition_ref)
  821. {
  822. struct kernel_lb_addr addr;
  823. struct inode *metadata_fe;
  824. addr.logicalBlockNum = meta_file_loc;
  825. addr.partitionReferenceNum = partition_ref;
  826. metadata_fe = udf_iget_special(sb, &addr);
  827. if (IS_ERR(metadata_fe)) {
  828. udf_warn(sb, "metadata inode efe not found\n");
  829. return metadata_fe;
  830. }
  831. if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
  832. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  833. iput(metadata_fe);
  834. return ERR_PTR(-EIO);
  835. }
  836. return metadata_fe;
  837. }
  838. static int udf_load_metadata_files(struct super_block *sb, int partition,
  839. int type1_index)
  840. {
  841. struct udf_sb_info *sbi = UDF_SB(sb);
  842. struct udf_part_map *map;
  843. struct udf_meta_data *mdata;
  844. struct kernel_lb_addr addr;
  845. struct inode *fe;
  846. map = &sbi->s_partmaps[partition];
  847. mdata = &map->s_type_specific.s_metadata;
  848. mdata->s_phys_partition_ref = type1_index;
  849. /* metadata address */
  850. udf_debug("Metadata file location: block = %u part = %u\n",
  851. mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
  852. fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
  853. mdata->s_phys_partition_ref);
  854. if (IS_ERR(fe)) {
  855. /* mirror file entry */
  856. udf_debug("Mirror metadata file location: block = %u part = %u\n",
  857. mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
  858. fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
  859. mdata->s_phys_partition_ref);
  860. if (IS_ERR(fe)) {
  861. udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
  862. return PTR_ERR(fe);
  863. }
  864. mdata->s_mirror_fe = fe;
  865. } else
  866. mdata->s_metadata_fe = fe;
  867. /*
  868. * bitmap file entry
  869. * Note:
  870. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  871. */
  872. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  873. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  874. addr.partitionReferenceNum = mdata->s_phys_partition_ref;
  875. udf_debug("Bitmap file location: block = %u part = %u\n",
  876. addr.logicalBlockNum, addr.partitionReferenceNum);
  877. fe = udf_iget_special(sb, &addr);
  878. if (IS_ERR(fe)) {
  879. if (sb_rdonly(sb))
  880. udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
  881. else {
  882. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  883. return PTR_ERR(fe);
  884. }
  885. } else
  886. mdata->s_bitmap_fe = fe;
  887. }
  888. udf_debug("udf_load_metadata_files Ok\n");
  889. return 0;
  890. }
  891. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  892. {
  893. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  894. return DIV_ROUND_UP(map->s_partition_len +
  895. (sizeof(struct spaceBitmapDesc) << 3),
  896. sb->s_blocksize * 8);
  897. }
  898. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  899. {
  900. struct udf_bitmap *bitmap;
  901. int nr_groups = udf_compute_nr_groups(sb, index);
  902. bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),
  903. GFP_KERNEL);
  904. if (!bitmap)
  905. return NULL;
  906. bitmap->s_nr_groups = nr_groups;
  907. return bitmap;
  908. }
  909. static int check_partition_desc(struct super_block *sb,
  910. struct partitionDesc *p,
  911. struct udf_part_map *map)
  912. {
  913. bool umap, utable, fmap, ftable;
  914. struct partitionHeaderDesc *phd;
  915. switch (le32_to_cpu(p->accessType)) {
  916. case PD_ACCESS_TYPE_READ_ONLY:
  917. case PD_ACCESS_TYPE_WRITE_ONCE:
  918. case PD_ACCESS_TYPE_NONE:
  919. goto force_ro;
  920. }
  921. /* No Partition Header Descriptor? */
  922. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  923. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  924. goto force_ro;
  925. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  926. utable = phd->unallocSpaceTable.extLength;
  927. umap = phd->unallocSpaceBitmap.extLength;
  928. ftable = phd->freedSpaceTable.extLength;
  929. fmap = phd->freedSpaceBitmap.extLength;
  930. /* No allocation info? */
  931. if (!utable && !umap && !ftable && !fmap)
  932. goto force_ro;
  933. /* We don't support blocks that require erasing before overwrite */
  934. if (ftable || fmap)
  935. goto force_ro;
  936. /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
  937. if (utable && umap)
  938. goto force_ro;
  939. if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  940. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  941. map->s_partition_type == UDF_METADATA_MAP25)
  942. goto force_ro;
  943. return 0;
  944. force_ro:
  945. if (!sb_rdonly(sb))
  946. return -EACCES;
  947. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  948. return 0;
  949. }
  950. static int udf_fill_partdesc_info(struct super_block *sb,
  951. struct partitionDesc *p, int p_index)
  952. {
  953. struct udf_part_map *map;
  954. struct udf_sb_info *sbi = UDF_SB(sb);
  955. struct partitionHeaderDesc *phd;
  956. int err;
  957. map = &sbi->s_partmaps[p_index];
  958. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  959. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  960. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  961. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  962. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  963. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  964. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  965. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  966. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  967. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  968. udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
  969. p_index, map->s_partition_type,
  970. map->s_partition_root, map->s_partition_len);
  971. err = check_partition_desc(sb, p, map);
  972. if (err)
  973. return err;
  974. /*
  975. * Skip loading allocation info it we cannot ever write to the fs.
  976. * This is a correctness thing as we may have decided to force ro mount
  977. * to avoid allocation info we don't support.
  978. */
  979. if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
  980. return 0;
  981. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  982. if (phd->unallocSpaceTable.extLength) {
  983. struct kernel_lb_addr loc = {
  984. .logicalBlockNum = le32_to_cpu(
  985. phd->unallocSpaceTable.extPosition),
  986. .partitionReferenceNum = p_index,
  987. };
  988. struct inode *inode;
  989. inode = udf_iget_special(sb, &loc);
  990. if (IS_ERR(inode)) {
  991. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  992. p_index);
  993. return PTR_ERR(inode);
  994. }
  995. map->s_uspace.s_table = inode;
  996. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  997. udf_debug("unallocSpaceTable (part %d) @ %lu\n",
  998. p_index, map->s_uspace.s_table->i_ino);
  999. }
  1000. if (phd->unallocSpaceBitmap.extLength) {
  1001. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  1002. if (!bitmap)
  1003. return -ENOMEM;
  1004. map->s_uspace.s_bitmap = bitmap;
  1005. bitmap->s_extPosition = le32_to_cpu(
  1006. phd->unallocSpaceBitmap.extPosition);
  1007. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  1008. udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
  1009. p_index, bitmap->s_extPosition);
  1010. }
  1011. return 0;
  1012. }
  1013. static void udf_find_vat_block(struct super_block *sb, int p_index,
  1014. int type1_index, sector_t start_block)
  1015. {
  1016. struct udf_sb_info *sbi = UDF_SB(sb);
  1017. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1018. sector_t vat_block;
  1019. struct kernel_lb_addr ino;
  1020. struct inode *inode;
  1021. /*
  1022. * VAT file entry is in the last recorded block. Some broken disks have
  1023. * it a few blocks before so try a bit harder...
  1024. */
  1025. ino.partitionReferenceNum = type1_index;
  1026. for (vat_block = start_block;
  1027. vat_block >= map->s_partition_root &&
  1028. vat_block >= start_block - 3; vat_block--) {
  1029. ino.logicalBlockNum = vat_block - map->s_partition_root;
  1030. inode = udf_iget_special(sb, &ino);
  1031. if (!IS_ERR(inode)) {
  1032. sbi->s_vat_inode = inode;
  1033. break;
  1034. }
  1035. }
  1036. }
  1037. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  1038. {
  1039. struct udf_sb_info *sbi = UDF_SB(sb);
  1040. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1041. struct buffer_head *bh = NULL;
  1042. struct udf_inode_info *vati;
  1043. uint32_t pos;
  1044. struct virtualAllocationTable20 *vat20;
  1045. sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
  1046. sb->s_blocksize_bits;
  1047. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  1048. if (!sbi->s_vat_inode &&
  1049. sbi->s_last_block != blocks - 1) {
  1050. pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
  1051. (unsigned long)sbi->s_last_block,
  1052. (unsigned long)blocks - 1);
  1053. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  1054. }
  1055. if (!sbi->s_vat_inode)
  1056. return -EIO;
  1057. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1058. map->s_type_specific.s_virtual.s_start_offset = 0;
  1059. map->s_type_specific.s_virtual.s_num_entries =
  1060. (sbi->s_vat_inode->i_size - 36) >> 2;
  1061. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1062. vati = UDF_I(sbi->s_vat_inode);
  1063. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1064. pos = udf_block_map(sbi->s_vat_inode, 0);
  1065. bh = sb_bread(sb, pos);
  1066. if (!bh)
  1067. return -EIO;
  1068. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1069. } else {
  1070. vat20 = (struct virtualAllocationTable20 *)
  1071. vati->i_data;
  1072. }
  1073. map->s_type_specific.s_virtual.s_start_offset =
  1074. le16_to_cpu(vat20->lengthHeader);
  1075. map->s_type_specific.s_virtual.s_num_entries =
  1076. (sbi->s_vat_inode->i_size -
  1077. map->s_type_specific.s_virtual.
  1078. s_start_offset) >> 2;
  1079. brelse(bh);
  1080. }
  1081. return 0;
  1082. }
  1083. /*
  1084. * Load partition descriptor block
  1085. *
  1086. * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
  1087. * sequence.
  1088. */
  1089. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1090. {
  1091. struct buffer_head *bh;
  1092. struct partitionDesc *p;
  1093. struct udf_part_map *map;
  1094. struct udf_sb_info *sbi = UDF_SB(sb);
  1095. int i, type1_idx;
  1096. uint16_t partitionNumber;
  1097. uint16_t ident;
  1098. int ret;
  1099. bh = udf_read_tagged(sb, block, block, &ident);
  1100. if (!bh)
  1101. return -EAGAIN;
  1102. if (ident != TAG_IDENT_PD) {
  1103. ret = 0;
  1104. goto out_bh;
  1105. }
  1106. p = (struct partitionDesc *)bh->b_data;
  1107. partitionNumber = le16_to_cpu(p->partitionNumber);
  1108. /* First scan for TYPE1 and SPARABLE partitions */
  1109. for (i = 0; i < sbi->s_partitions; i++) {
  1110. map = &sbi->s_partmaps[i];
  1111. udf_debug("Searching map: (%u == %u)\n",
  1112. map->s_partition_num, partitionNumber);
  1113. if (map->s_partition_num == partitionNumber &&
  1114. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1115. map->s_partition_type == UDF_SPARABLE_MAP15))
  1116. break;
  1117. }
  1118. if (i >= sbi->s_partitions) {
  1119. udf_debug("Partition (%u) not found in partition map\n",
  1120. partitionNumber);
  1121. ret = 0;
  1122. goto out_bh;
  1123. }
  1124. ret = udf_fill_partdesc_info(sb, p, i);
  1125. if (ret < 0)
  1126. goto out_bh;
  1127. /*
  1128. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1129. * PHYSICAL partitions are already set up
  1130. */
  1131. type1_idx = i;
  1132. map = NULL; /* supress 'maybe used uninitialized' warning */
  1133. for (i = 0; i < sbi->s_partitions; i++) {
  1134. map = &sbi->s_partmaps[i];
  1135. if (map->s_partition_num == partitionNumber &&
  1136. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1137. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1138. map->s_partition_type == UDF_METADATA_MAP25))
  1139. break;
  1140. }
  1141. if (i >= sbi->s_partitions) {
  1142. ret = 0;
  1143. goto out_bh;
  1144. }
  1145. ret = udf_fill_partdesc_info(sb, p, i);
  1146. if (ret < 0)
  1147. goto out_bh;
  1148. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1149. ret = udf_load_metadata_files(sb, i, type1_idx);
  1150. if (ret < 0) {
  1151. udf_err(sb, "error loading MetaData partition map %d\n",
  1152. i);
  1153. goto out_bh;
  1154. }
  1155. } else {
  1156. /*
  1157. * If we have a partition with virtual map, we don't handle
  1158. * writing to it (we overwrite blocks instead of relocating
  1159. * them).
  1160. */
  1161. if (!sb_rdonly(sb)) {
  1162. ret = -EACCES;
  1163. goto out_bh;
  1164. }
  1165. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  1166. ret = udf_load_vat(sb, i, type1_idx);
  1167. if (ret < 0)
  1168. goto out_bh;
  1169. }
  1170. ret = 0;
  1171. out_bh:
  1172. /* In case loading failed, we handle cleanup in udf_fill_super */
  1173. brelse(bh);
  1174. return ret;
  1175. }
  1176. static int udf_load_sparable_map(struct super_block *sb,
  1177. struct udf_part_map *map,
  1178. struct sparablePartitionMap *spm)
  1179. {
  1180. uint32_t loc;
  1181. uint16_t ident;
  1182. struct sparingTable *st;
  1183. struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
  1184. int i;
  1185. struct buffer_head *bh;
  1186. map->s_partition_type = UDF_SPARABLE_MAP15;
  1187. sdata->s_packet_len = le16_to_cpu(spm->packetLength);
  1188. if (!is_power_of_2(sdata->s_packet_len)) {
  1189. udf_err(sb, "error loading logical volume descriptor: "
  1190. "Invalid packet length %u\n",
  1191. (unsigned)sdata->s_packet_len);
  1192. return -EIO;
  1193. }
  1194. if (spm->numSparingTables > 4) {
  1195. udf_err(sb, "error loading logical volume descriptor: "
  1196. "Too many sparing tables (%d)\n",
  1197. (int)spm->numSparingTables);
  1198. return -EIO;
  1199. }
  1200. if (le32_to_cpu(spm->sizeSparingTable) > sb->s_blocksize) {
  1201. udf_err(sb, "error loading logical volume descriptor: "
  1202. "Too big sparing table size (%u)\n",
  1203. le32_to_cpu(spm->sizeSparingTable));
  1204. return -EIO;
  1205. }
  1206. for (i = 0; i < spm->numSparingTables; i++) {
  1207. loc = le32_to_cpu(spm->locSparingTable[i]);
  1208. bh = udf_read_tagged(sb, loc, loc, &ident);
  1209. if (!bh)
  1210. continue;
  1211. st = (struct sparingTable *)bh->b_data;
  1212. if (ident != 0 ||
  1213. strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
  1214. strlen(UDF_ID_SPARING)) ||
  1215. sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
  1216. sb->s_blocksize) {
  1217. brelse(bh);
  1218. continue;
  1219. }
  1220. sdata->s_spar_map[i] = bh;
  1221. }
  1222. map->s_partition_func = udf_get_pblock_spar15;
  1223. return 0;
  1224. }
  1225. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1226. struct kernel_lb_addr *fileset)
  1227. {
  1228. struct logicalVolDesc *lvd;
  1229. int i, offset;
  1230. uint8_t type;
  1231. struct udf_sb_info *sbi = UDF_SB(sb);
  1232. struct genericPartitionMap *gpm;
  1233. uint16_t ident;
  1234. struct buffer_head *bh;
  1235. unsigned int table_len;
  1236. int ret;
  1237. bh = udf_read_tagged(sb, block, block, &ident);
  1238. if (!bh)
  1239. return -EAGAIN;
  1240. BUG_ON(ident != TAG_IDENT_LVD);
  1241. lvd = (struct logicalVolDesc *)bh->b_data;
  1242. table_len = le32_to_cpu(lvd->mapTableLength);
  1243. if (table_len > sb->s_blocksize - sizeof(*lvd)) {
  1244. udf_err(sb, "error loading logical volume descriptor: "
  1245. "Partition table too long (%u > %lu)\n", table_len,
  1246. sb->s_blocksize - sizeof(*lvd));
  1247. ret = -EIO;
  1248. goto out_bh;
  1249. }
  1250. ret = udf_verify_domain_identifier(sb, &lvd->domainIdent,
  1251. "logical volume");
  1252. if (ret)
  1253. goto out_bh;
  1254. ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1255. if (ret)
  1256. goto out_bh;
  1257. for (i = 0, offset = 0;
  1258. i < sbi->s_partitions && offset < table_len;
  1259. i++, offset += gpm->partitionMapLength) {
  1260. struct udf_part_map *map = &sbi->s_partmaps[i];
  1261. gpm = (struct genericPartitionMap *)
  1262. &(lvd->partitionMaps[offset]);
  1263. type = gpm->partitionMapType;
  1264. if (type == 1) {
  1265. struct genericPartitionMap1 *gpm1 =
  1266. (struct genericPartitionMap1 *)gpm;
  1267. map->s_partition_type = UDF_TYPE1_MAP15;
  1268. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1269. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1270. map->s_partition_func = NULL;
  1271. } else if (type == 2) {
  1272. struct udfPartitionMap2 *upm2 =
  1273. (struct udfPartitionMap2 *)gpm;
  1274. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1275. strlen(UDF_ID_VIRTUAL))) {
  1276. u16 suf =
  1277. le16_to_cpu(((__le16 *)upm2->partIdent.
  1278. identSuffix)[0]);
  1279. if (suf < 0x0200) {
  1280. map->s_partition_type =
  1281. UDF_VIRTUAL_MAP15;
  1282. map->s_partition_func =
  1283. udf_get_pblock_virt15;
  1284. } else {
  1285. map->s_partition_type =
  1286. UDF_VIRTUAL_MAP20;
  1287. map->s_partition_func =
  1288. udf_get_pblock_virt20;
  1289. }
  1290. } else if (!strncmp(upm2->partIdent.ident,
  1291. UDF_ID_SPARABLE,
  1292. strlen(UDF_ID_SPARABLE))) {
  1293. ret = udf_load_sparable_map(sb, map,
  1294. (struct sparablePartitionMap *)gpm);
  1295. if (ret < 0)
  1296. goto out_bh;
  1297. } else if (!strncmp(upm2->partIdent.ident,
  1298. UDF_ID_METADATA,
  1299. strlen(UDF_ID_METADATA))) {
  1300. struct udf_meta_data *mdata =
  1301. &map->s_type_specific.s_metadata;
  1302. struct metadataPartitionMap *mdm =
  1303. (struct metadataPartitionMap *)
  1304. &(lvd->partitionMaps[offset]);
  1305. udf_debug("Parsing Logical vol part %d type %u id=%s\n",
  1306. i, type, UDF_ID_METADATA);
  1307. map->s_partition_type = UDF_METADATA_MAP25;
  1308. map->s_partition_func = udf_get_pblock_meta25;
  1309. mdata->s_meta_file_loc =
  1310. le32_to_cpu(mdm->metadataFileLoc);
  1311. mdata->s_mirror_file_loc =
  1312. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1313. mdata->s_bitmap_file_loc =
  1314. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1315. mdata->s_alloc_unit_size =
  1316. le32_to_cpu(mdm->allocUnitSize);
  1317. mdata->s_align_unit_size =
  1318. le16_to_cpu(mdm->alignUnitSize);
  1319. if (mdm->flags & 0x01)
  1320. mdata->s_flags |= MF_DUPLICATE_MD;
  1321. udf_debug("Metadata Ident suffix=0x%x\n",
  1322. le16_to_cpu(*(__le16 *)
  1323. mdm->partIdent.identSuffix));
  1324. udf_debug("Metadata part num=%u\n",
  1325. le16_to_cpu(mdm->partitionNum));
  1326. udf_debug("Metadata part alloc unit size=%u\n",
  1327. le32_to_cpu(mdm->allocUnitSize));
  1328. udf_debug("Metadata file loc=%u\n",
  1329. le32_to_cpu(mdm->metadataFileLoc));
  1330. udf_debug("Mirror file loc=%u\n",
  1331. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1332. udf_debug("Bitmap file loc=%u\n",
  1333. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1334. udf_debug("Flags: %d %u\n",
  1335. mdata->s_flags, mdm->flags);
  1336. } else {
  1337. udf_debug("Unknown ident: %s\n",
  1338. upm2->partIdent.ident);
  1339. continue;
  1340. }
  1341. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1342. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1343. }
  1344. udf_debug("Partition (%d:%u) type %u on volume %u\n",
  1345. i, map->s_partition_num, type, map->s_volumeseqnum);
  1346. }
  1347. if (fileset) {
  1348. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1349. *fileset = lelb_to_cpu(la->extLocation);
  1350. udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
  1351. fileset->logicalBlockNum,
  1352. fileset->partitionReferenceNum);
  1353. }
  1354. if (lvd->integritySeqExt.extLength)
  1355. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1356. ret = 0;
  1357. if (!sbi->s_lvid_bh) {
  1358. /* We can't generate unique IDs without a valid LVID */
  1359. if (sb_rdonly(sb)) {
  1360. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  1361. } else {
  1362. udf_warn(sb, "Damaged or missing LVID, forcing "
  1363. "readonly mount\n");
  1364. ret = -EACCES;
  1365. }
  1366. }
  1367. out_bh:
  1368. brelse(bh);
  1369. return ret;
  1370. }
  1371. /*
  1372. * Find the prevailing Logical Volume Integrity Descriptor.
  1373. */
  1374. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1375. {
  1376. struct buffer_head *bh, *final_bh;
  1377. uint16_t ident;
  1378. struct udf_sb_info *sbi = UDF_SB(sb);
  1379. struct logicalVolIntegrityDesc *lvid;
  1380. int indirections = 0;
  1381. u32 parts, impuselen;
  1382. while (++indirections <= UDF_MAX_LVID_NESTING) {
  1383. final_bh = NULL;
  1384. while (loc.extLength > 0 &&
  1385. (bh = udf_read_tagged(sb, loc.extLocation,
  1386. loc.extLocation, &ident))) {
  1387. if (ident != TAG_IDENT_LVID) {
  1388. brelse(bh);
  1389. break;
  1390. }
  1391. brelse(final_bh);
  1392. final_bh = bh;
  1393. loc.extLength -= sb->s_blocksize;
  1394. loc.extLocation++;
  1395. }
  1396. if (!final_bh)
  1397. return;
  1398. brelse(sbi->s_lvid_bh);
  1399. sbi->s_lvid_bh = final_bh;
  1400. lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
  1401. if (lvid->nextIntegrityExt.extLength == 0)
  1402. goto check;
  1403. loc = leea_to_cpu(lvid->nextIntegrityExt);
  1404. }
  1405. udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
  1406. UDF_MAX_LVID_NESTING);
  1407. out_err:
  1408. brelse(sbi->s_lvid_bh);
  1409. sbi->s_lvid_bh = NULL;
  1410. return;
  1411. check:
  1412. parts = le32_to_cpu(lvid->numOfPartitions);
  1413. impuselen = le32_to_cpu(lvid->lengthOfImpUse);
  1414. if (parts >= sb->s_blocksize || impuselen >= sb->s_blocksize ||
  1415. sizeof(struct logicalVolIntegrityDesc) + impuselen +
  1416. 2 * parts * sizeof(u32) > sb->s_blocksize) {
  1417. udf_warn(sb, "Corrupted LVID (parts=%u, impuselen=%u), "
  1418. "ignoring.\n", parts, impuselen);
  1419. goto out_err;
  1420. }
  1421. }
  1422. /*
  1423. * Step for reallocation of table of partition descriptor sequence numbers.
  1424. * Must be power of 2.
  1425. */
  1426. #define PART_DESC_ALLOC_STEP 32
  1427. struct part_desc_seq_scan_data {
  1428. struct udf_vds_record rec;
  1429. u32 partnum;
  1430. };
  1431. struct desc_seq_scan_data {
  1432. struct udf_vds_record vds[VDS_POS_LENGTH];
  1433. unsigned int size_part_descs;
  1434. unsigned int num_part_descs;
  1435. struct part_desc_seq_scan_data *part_descs_loc;
  1436. };
  1437. static struct udf_vds_record *handle_partition_descriptor(
  1438. struct buffer_head *bh,
  1439. struct desc_seq_scan_data *data)
  1440. {
  1441. struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
  1442. int partnum;
  1443. int i;
  1444. partnum = le16_to_cpu(desc->partitionNumber);
  1445. for (i = 0; i < data->num_part_descs; i++)
  1446. if (partnum == data->part_descs_loc[i].partnum)
  1447. return &(data->part_descs_loc[i].rec);
  1448. if (data->num_part_descs >= data->size_part_descs) {
  1449. struct part_desc_seq_scan_data *new_loc;
  1450. unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
  1451. new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL);
  1452. if (!new_loc)
  1453. return ERR_PTR(-ENOMEM);
  1454. memcpy(new_loc, data->part_descs_loc,
  1455. data->size_part_descs * sizeof(*new_loc));
  1456. kfree(data->part_descs_loc);
  1457. data->part_descs_loc = new_loc;
  1458. data->size_part_descs = new_size;
  1459. }
  1460. return &(data->part_descs_loc[data->num_part_descs++].rec);
  1461. }
  1462. static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
  1463. struct buffer_head *bh, struct desc_seq_scan_data *data)
  1464. {
  1465. switch (ident) {
  1466. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1467. return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
  1468. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1469. return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
  1470. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1471. return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
  1472. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1473. return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
  1474. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1475. return handle_partition_descriptor(bh, data);
  1476. }
  1477. return NULL;
  1478. }
  1479. /*
  1480. * Process a main/reserve volume descriptor sequence.
  1481. * @block First block of first extent of the sequence.
  1482. * @lastblock Lastblock of first extent of the sequence.
  1483. * @fileset There we store extent containing root fileset
  1484. *
  1485. * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
  1486. * sequence
  1487. */
  1488. static noinline int udf_process_sequence(
  1489. struct super_block *sb,
  1490. sector_t block, sector_t lastblock,
  1491. struct kernel_lb_addr *fileset)
  1492. {
  1493. struct buffer_head *bh = NULL;
  1494. struct udf_vds_record *curr;
  1495. struct generic_desc *gd;
  1496. struct volDescPtr *vdp;
  1497. bool done = false;
  1498. uint32_t vdsn;
  1499. uint16_t ident;
  1500. int ret;
  1501. unsigned int indirections = 0;
  1502. struct desc_seq_scan_data data;
  1503. unsigned int i;
  1504. memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1505. data.size_part_descs = PART_DESC_ALLOC_STEP;
  1506. data.num_part_descs = 0;
  1507. data.part_descs_loc = kcalloc(data.size_part_descs,
  1508. sizeof(*data.part_descs_loc),
  1509. GFP_KERNEL);
  1510. if (!data.part_descs_loc)
  1511. return -ENOMEM;
  1512. /*
  1513. * Read the main descriptor sequence and find which descriptors
  1514. * are in it.
  1515. */
  1516. for (; (!done && block <= lastblock); block++) {
  1517. bh = udf_read_tagged(sb, block, block, &ident);
  1518. if (!bh)
  1519. break;
  1520. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1521. gd = (struct generic_desc *)bh->b_data;
  1522. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1523. switch (ident) {
  1524. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1525. if (++indirections > UDF_MAX_TD_NESTING) {
  1526. udf_err(sb, "too many Volume Descriptor "
  1527. "Pointers (max %u supported)\n",
  1528. UDF_MAX_TD_NESTING);
  1529. brelse(bh);
  1530. ret = -EIO;
  1531. goto out;
  1532. }
  1533. vdp = (struct volDescPtr *)bh->b_data;
  1534. block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
  1535. lastblock = le32_to_cpu(
  1536. vdp->nextVolDescSeqExt.extLength) >>
  1537. sb->s_blocksize_bits;
  1538. lastblock += block - 1;
  1539. /* For loop is going to increment 'block' again */
  1540. block--;
  1541. break;
  1542. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1543. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1544. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1545. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1546. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1547. curr = get_volume_descriptor_record(ident, bh, &data);
  1548. if (IS_ERR(curr)) {
  1549. brelse(bh);
  1550. ret = PTR_ERR(curr);
  1551. goto out;
  1552. }
  1553. /* Descriptor we don't care about? */
  1554. if (!curr)
  1555. break;
  1556. if (vdsn >= curr->volDescSeqNum) {
  1557. curr->volDescSeqNum = vdsn;
  1558. curr->block = block;
  1559. }
  1560. break;
  1561. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1562. done = true;
  1563. break;
  1564. }
  1565. brelse(bh);
  1566. }
  1567. /*
  1568. * Now read interesting descriptors again and process them
  1569. * in a suitable order
  1570. */
  1571. if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1572. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1573. ret = -EAGAIN;
  1574. goto out;
  1575. }
  1576. ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
  1577. if (ret < 0)
  1578. goto out;
  1579. if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
  1580. ret = udf_load_logicalvol(sb,
  1581. data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
  1582. fileset);
  1583. if (ret < 0)
  1584. goto out;
  1585. }
  1586. /* Now handle prevailing Partition Descriptors */
  1587. for (i = 0; i < data.num_part_descs; i++) {
  1588. ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
  1589. if (ret < 0)
  1590. goto out;
  1591. }
  1592. ret = 0;
  1593. out:
  1594. kfree(data.part_descs_loc);
  1595. return ret;
  1596. }
  1597. /*
  1598. * Load Volume Descriptor Sequence described by anchor in bh
  1599. *
  1600. * Returns <0 on error, 0 on success
  1601. */
  1602. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1603. struct kernel_lb_addr *fileset)
  1604. {
  1605. struct anchorVolDescPtr *anchor;
  1606. sector_t main_s, main_e, reserve_s, reserve_e;
  1607. int ret;
  1608. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1609. /* Locate the main sequence */
  1610. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1611. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1612. main_e = main_e >> sb->s_blocksize_bits;
  1613. main_e += main_s - 1;
  1614. /* Locate the reserve sequence */
  1615. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1616. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1617. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1618. reserve_e += reserve_s - 1;
  1619. /* Process the main & reserve sequences */
  1620. /* responsible for finding the PartitionDesc(s) */
  1621. ret = udf_process_sequence(sb, main_s, main_e, fileset);
  1622. if (ret != -EAGAIN)
  1623. return ret;
  1624. udf_sb_free_partitions(sb);
  1625. ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1626. if (ret < 0) {
  1627. udf_sb_free_partitions(sb);
  1628. /* No sequence was OK, return -EIO */
  1629. if (ret == -EAGAIN)
  1630. ret = -EIO;
  1631. }
  1632. return ret;
  1633. }
  1634. /*
  1635. * Check whether there is an anchor block in the given block and
  1636. * load Volume Descriptor Sequence if so.
  1637. *
  1638. * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
  1639. * block
  1640. */
  1641. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1642. struct kernel_lb_addr *fileset)
  1643. {
  1644. struct buffer_head *bh;
  1645. uint16_t ident;
  1646. int ret;
  1647. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1648. udf_fixed_to_variable(block) >=
  1649. i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
  1650. return -EAGAIN;
  1651. bh = udf_read_tagged(sb, block, block, &ident);
  1652. if (!bh)
  1653. return -EAGAIN;
  1654. if (ident != TAG_IDENT_AVDP) {
  1655. brelse(bh);
  1656. return -EAGAIN;
  1657. }
  1658. ret = udf_load_sequence(sb, bh, fileset);
  1659. brelse(bh);
  1660. return ret;
  1661. }
  1662. /*
  1663. * Search for an anchor volume descriptor pointer.
  1664. *
  1665. * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
  1666. * of anchors.
  1667. */
  1668. static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
  1669. struct kernel_lb_addr *fileset)
  1670. {
  1671. sector_t last[6];
  1672. int i;
  1673. struct udf_sb_info *sbi = UDF_SB(sb);
  1674. int last_count = 0;
  1675. int ret;
  1676. /* First try user provided anchor */
  1677. if (sbi->s_anchor) {
  1678. ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
  1679. if (ret != -EAGAIN)
  1680. return ret;
  1681. }
  1682. /*
  1683. * according to spec, anchor is in either:
  1684. * block 256
  1685. * lastblock-256
  1686. * lastblock
  1687. * however, if the disc isn't closed, it could be 512.
  1688. */
  1689. ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
  1690. if (ret != -EAGAIN)
  1691. return ret;
  1692. /*
  1693. * The trouble is which block is the last one. Drives often misreport
  1694. * this so we try various possibilities.
  1695. */
  1696. last[last_count++] = *lastblock;
  1697. if (*lastblock >= 1)
  1698. last[last_count++] = *lastblock - 1;
  1699. last[last_count++] = *lastblock + 1;
  1700. if (*lastblock >= 2)
  1701. last[last_count++] = *lastblock - 2;
  1702. if (*lastblock >= 150)
  1703. last[last_count++] = *lastblock - 150;
  1704. if (*lastblock >= 152)
  1705. last[last_count++] = *lastblock - 152;
  1706. for (i = 0; i < last_count; i++) {
  1707. if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
  1708. sb->s_blocksize_bits)
  1709. continue;
  1710. ret = udf_check_anchor_block(sb, last[i], fileset);
  1711. if (ret != -EAGAIN) {
  1712. if (!ret)
  1713. *lastblock = last[i];
  1714. return ret;
  1715. }
  1716. if (last[i] < 256)
  1717. continue;
  1718. ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
  1719. if (ret != -EAGAIN) {
  1720. if (!ret)
  1721. *lastblock = last[i];
  1722. return ret;
  1723. }
  1724. }
  1725. /* Finally try block 512 in case media is open */
  1726. return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
  1727. }
  1728. /*
  1729. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1730. * area specified by it. The function expects sbi->s_lastblock to be the last
  1731. * block on the media.
  1732. *
  1733. * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
  1734. * was not found.
  1735. */
  1736. static int udf_find_anchor(struct super_block *sb,
  1737. struct kernel_lb_addr *fileset)
  1738. {
  1739. struct udf_sb_info *sbi = UDF_SB(sb);
  1740. sector_t lastblock = sbi->s_last_block;
  1741. int ret;
  1742. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1743. if (ret != -EAGAIN)
  1744. goto out;
  1745. /* No anchor found? Try VARCONV conversion of block numbers */
  1746. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1747. lastblock = udf_variable_to_fixed(sbi->s_last_block);
  1748. /* Firstly, we try to not convert number of the last block */
  1749. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1750. if (ret != -EAGAIN)
  1751. goto out;
  1752. lastblock = sbi->s_last_block;
  1753. /* Secondly, we try with converted number of the last block */
  1754. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1755. if (ret < 0) {
  1756. /* VARCONV didn't help. Clear it. */
  1757. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1758. }
  1759. out:
  1760. if (ret == 0)
  1761. sbi->s_last_block = lastblock;
  1762. return ret;
  1763. }
  1764. /*
  1765. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1766. * Descriptor Sequence.
  1767. *
  1768. * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
  1769. * block was not found.
  1770. */
  1771. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1772. int silent, struct kernel_lb_addr *fileset)
  1773. {
  1774. struct udf_sb_info *sbi = UDF_SB(sb);
  1775. int nsr = 0;
  1776. int ret;
  1777. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1778. if (!silent)
  1779. udf_warn(sb, "Bad block size\n");
  1780. return -EINVAL;
  1781. }
  1782. sbi->s_last_block = uopt->lastblock;
  1783. if (!uopt->novrs) {
  1784. /* Check that it is NSR02 compliant */
  1785. nsr = udf_check_vsd(sb);
  1786. if (!nsr) {
  1787. if (!silent)
  1788. udf_warn(sb, "No VRS found\n");
  1789. return -EINVAL;
  1790. }
  1791. if (nsr == -1)
  1792. udf_debug("Failed to read sector at offset %d. "
  1793. "Assuming open disc. Skipping validity "
  1794. "check\n", VSD_FIRST_SECTOR_OFFSET);
  1795. if (!sbi->s_last_block)
  1796. sbi->s_last_block = udf_get_last_block(sb);
  1797. } else {
  1798. udf_debug("Validity check skipped because of novrs option\n");
  1799. }
  1800. /* Look for anchor block and load Volume Descriptor Sequence */
  1801. sbi->s_anchor = uopt->anchor;
  1802. ret = udf_find_anchor(sb, fileset);
  1803. if (ret < 0) {
  1804. if (!silent && ret == -EAGAIN)
  1805. udf_warn(sb, "No anchor found\n");
  1806. return ret;
  1807. }
  1808. return 0;
  1809. }
  1810. static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid)
  1811. {
  1812. struct timespec64 ts;
  1813. ktime_get_real_ts64(&ts);
  1814. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
  1815. lvid->descTag.descCRC = cpu_to_le16(
  1816. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1817. le16_to_cpu(lvid->descTag.descCRCLength)));
  1818. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1819. }
  1820. static void udf_open_lvid(struct super_block *sb)
  1821. {
  1822. struct udf_sb_info *sbi = UDF_SB(sb);
  1823. struct buffer_head *bh = sbi->s_lvid_bh;
  1824. struct logicalVolIntegrityDesc *lvid;
  1825. struct logicalVolIntegrityDescImpUse *lvidiu;
  1826. if (!bh)
  1827. return;
  1828. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1829. lvidiu = udf_sb_lvidiu(sb);
  1830. if (!lvidiu)
  1831. return;
  1832. mutex_lock(&sbi->s_alloc_mutex);
  1833. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1834. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1835. if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
  1836. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1837. else
  1838. UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
  1839. udf_finalize_lvid(lvid);
  1840. mark_buffer_dirty(bh);
  1841. sbi->s_lvid_dirty = 0;
  1842. mutex_unlock(&sbi->s_alloc_mutex);
  1843. /* Make opening of filesystem visible on the media immediately */
  1844. sync_dirty_buffer(bh);
  1845. }
  1846. static void udf_close_lvid(struct super_block *sb)
  1847. {
  1848. struct udf_sb_info *sbi = UDF_SB(sb);
  1849. struct buffer_head *bh = sbi->s_lvid_bh;
  1850. struct logicalVolIntegrityDesc *lvid;
  1851. struct logicalVolIntegrityDescImpUse *lvidiu;
  1852. if (!bh)
  1853. return;
  1854. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1855. lvidiu = udf_sb_lvidiu(sb);
  1856. if (!lvidiu)
  1857. return;
  1858. mutex_lock(&sbi->s_alloc_mutex);
  1859. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1860. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1861. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1862. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1863. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1864. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1865. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1866. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1867. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
  1868. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1869. /*
  1870. * We set buffer uptodate unconditionally here to avoid spurious
  1871. * warnings from mark_buffer_dirty() when previous EIO has marked
  1872. * the buffer as !uptodate
  1873. */
  1874. set_buffer_uptodate(bh);
  1875. udf_finalize_lvid(lvid);
  1876. mark_buffer_dirty(bh);
  1877. sbi->s_lvid_dirty = 0;
  1878. mutex_unlock(&sbi->s_alloc_mutex);
  1879. /* Make closing of filesystem visible on the media immediately */
  1880. sync_dirty_buffer(bh);
  1881. }
  1882. u64 lvid_get_unique_id(struct super_block *sb)
  1883. {
  1884. struct buffer_head *bh;
  1885. struct udf_sb_info *sbi = UDF_SB(sb);
  1886. struct logicalVolIntegrityDesc *lvid;
  1887. struct logicalVolHeaderDesc *lvhd;
  1888. u64 uniqueID;
  1889. u64 ret;
  1890. bh = sbi->s_lvid_bh;
  1891. if (!bh)
  1892. return 0;
  1893. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1894. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1895. mutex_lock(&sbi->s_alloc_mutex);
  1896. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1897. if (!(++uniqueID & 0xFFFFFFFF))
  1898. uniqueID += 16;
  1899. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1900. udf_updated_lvid(sb);
  1901. mutex_unlock(&sbi->s_alloc_mutex);
  1902. return ret;
  1903. }
  1904. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1905. {
  1906. int ret = -EINVAL;
  1907. struct inode *inode = NULL;
  1908. struct udf_options uopt;
  1909. struct kernel_lb_addr rootdir, fileset;
  1910. struct udf_sb_info *sbi;
  1911. bool lvid_open = false;
  1912. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1913. /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
  1914. uopt.uid = make_kuid(current_user_ns(), overflowuid);
  1915. uopt.gid = make_kgid(current_user_ns(), overflowgid);
  1916. uopt.umask = 0;
  1917. uopt.fmode = UDF_INVALID_MODE;
  1918. uopt.dmode = UDF_INVALID_MODE;
  1919. uopt.nls_map = NULL;
  1920. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1921. if (!sbi)
  1922. return -ENOMEM;
  1923. sb->s_fs_info = sbi;
  1924. mutex_init(&sbi->s_alloc_mutex);
  1925. if (!udf_parse_options((char *)options, &uopt, false))
  1926. goto parse_options_failure;
  1927. fileset.logicalBlockNum = 0xFFFFFFFF;
  1928. fileset.partitionReferenceNum = 0xFFFF;
  1929. sbi->s_flags = uopt.flags;
  1930. sbi->s_uid = uopt.uid;
  1931. sbi->s_gid = uopt.gid;
  1932. sbi->s_umask = uopt.umask;
  1933. sbi->s_fmode = uopt.fmode;
  1934. sbi->s_dmode = uopt.dmode;
  1935. sbi->s_nls_map = uopt.nls_map;
  1936. rwlock_init(&sbi->s_cred_lock);
  1937. if (uopt.session == 0xFFFFFFFF)
  1938. sbi->s_session = udf_get_last_session(sb);
  1939. else
  1940. sbi->s_session = uopt.session;
  1941. udf_debug("Multi-session=%d\n", sbi->s_session);
  1942. /* Fill in the rest of the superblock */
  1943. sb->s_op = &udf_sb_ops;
  1944. sb->s_export_op = &udf_export_ops;
  1945. sb->s_magic = UDF_SUPER_MAGIC;
  1946. sb->s_time_gran = 1000;
  1947. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1948. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1949. } else {
  1950. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1951. while (uopt.blocksize <= 4096) {
  1952. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1953. if (ret < 0) {
  1954. if (!silent && ret != -EACCES) {
  1955. pr_notice("Scanning with blocksize %u failed\n",
  1956. uopt.blocksize);
  1957. }
  1958. brelse(sbi->s_lvid_bh);
  1959. sbi->s_lvid_bh = NULL;
  1960. /*
  1961. * EACCES is special - we want to propagate to
  1962. * upper layers that we cannot handle RW mount.
  1963. */
  1964. if (ret == -EACCES)
  1965. break;
  1966. } else
  1967. break;
  1968. uopt.blocksize <<= 1;
  1969. }
  1970. }
  1971. if (ret < 0) {
  1972. if (ret == -EAGAIN) {
  1973. udf_warn(sb, "No partition found (1)\n");
  1974. ret = -EINVAL;
  1975. }
  1976. goto error_out;
  1977. }
  1978. udf_debug("Lastblock=%u\n", sbi->s_last_block);
  1979. if (sbi->s_lvid_bh) {
  1980. struct logicalVolIntegrityDescImpUse *lvidiu =
  1981. udf_sb_lvidiu(sb);
  1982. uint16_t minUDFReadRev;
  1983. uint16_t minUDFWriteRev;
  1984. if (!lvidiu) {
  1985. ret = -EINVAL;
  1986. goto error_out;
  1987. }
  1988. minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1989. minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1990. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1991. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1992. minUDFReadRev,
  1993. UDF_MAX_READ_VERSION);
  1994. ret = -EINVAL;
  1995. goto error_out;
  1996. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
  1997. if (!sb_rdonly(sb)) {
  1998. ret = -EACCES;
  1999. goto error_out;
  2000. }
  2001. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  2002. }
  2003. sbi->s_udfrev = minUDFWriteRev;
  2004. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  2005. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  2006. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  2007. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  2008. }
  2009. if (!sbi->s_partitions) {
  2010. udf_warn(sb, "No partition found (2)\n");
  2011. ret = -EINVAL;
  2012. goto error_out;
  2013. }
  2014. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  2015. UDF_PART_FLAG_READ_ONLY) {
  2016. if (!sb_rdonly(sb)) {
  2017. ret = -EACCES;
  2018. goto error_out;
  2019. }
  2020. UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
  2021. }
  2022. ret = udf_find_fileset(sb, &fileset, &rootdir);
  2023. if (ret < 0) {
  2024. udf_warn(sb, "No fileset found\n");
  2025. goto error_out;
  2026. }
  2027. if (!silent) {
  2028. struct timestamp ts;
  2029. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  2030. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  2031. sbi->s_volume_ident,
  2032. le16_to_cpu(ts.year), ts.month, ts.day,
  2033. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  2034. }
  2035. if (!sb_rdonly(sb)) {
  2036. udf_open_lvid(sb);
  2037. lvid_open = true;
  2038. }
  2039. /* Assign the root inode */
  2040. /* assign inodes by physical block number */
  2041. /* perhaps it's not extensible enough, but for now ... */
  2042. inode = udf_iget(sb, &rootdir);
  2043. if (IS_ERR(inode)) {
  2044. udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
  2045. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  2046. ret = PTR_ERR(inode);
  2047. goto error_out;
  2048. }
  2049. /* Allocate a dentry for the root inode */
  2050. sb->s_root = d_make_root(inode);
  2051. if (!sb->s_root) {
  2052. udf_err(sb, "Couldn't allocate root dentry\n");
  2053. ret = -ENOMEM;
  2054. goto error_out;
  2055. }
  2056. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2057. sb->s_max_links = UDF_MAX_LINKS;
  2058. return 0;
  2059. error_out:
  2060. iput(sbi->s_vat_inode);
  2061. parse_options_failure:
  2062. unload_nls(uopt.nls_map);
  2063. if (lvid_open)
  2064. udf_close_lvid(sb);
  2065. brelse(sbi->s_lvid_bh);
  2066. udf_sb_free_partitions(sb);
  2067. kfree(sbi);
  2068. sb->s_fs_info = NULL;
  2069. return ret;
  2070. }
  2071. void _udf_err(struct super_block *sb, const char *function,
  2072. const char *fmt, ...)
  2073. {
  2074. struct va_format vaf;
  2075. va_list args;
  2076. va_start(args, fmt);
  2077. vaf.fmt = fmt;
  2078. vaf.va = &args;
  2079. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  2080. va_end(args);
  2081. }
  2082. void _udf_warn(struct super_block *sb, const char *function,
  2083. const char *fmt, ...)
  2084. {
  2085. struct va_format vaf;
  2086. va_list args;
  2087. va_start(args, fmt);
  2088. vaf.fmt = fmt;
  2089. vaf.va = &args;
  2090. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  2091. va_end(args);
  2092. }
  2093. static void udf_put_super(struct super_block *sb)
  2094. {
  2095. struct udf_sb_info *sbi;
  2096. sbi = UDF_SB(sb);
  2097. iput(sbi->s_vat_inode);
  2098. unload_nls(sbi->s_nls_map);
  2099. if (!sb_rdonly(sb))
  2100. udf_close_lvid(sb);
  2101. brelse(sbi->s_lvid_bh);
  2102. udf_sb_free_partitions(sb);
  2103. mutex_destroy(&sbi->s_alloc_mutex);
  2104. kfree(sb->s_fs_info);
  2105. sb->s_fs_info = NULL;
  2106. }
  2107. static int udf_sync_fs(struct super_block *sb, int wait)
  2108. {
  2109. struct udf_sb_info *sbi = UDF_SB(sb);
  2110. mutex_lock(&sbi->s_alloc_mutex);
  2111. if (sbi->s_lvid_dirty) {
  2112. struct buffer_head *bh = sbi->s_lvid_bh;
  2113. struct logicalVolIntegrityDesc *lvid;
  2114. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  2115. udf_finalize_lvid(lvid);
  2116. /*
  2117. * Blockdevice will be synced later so we don't have to submit
  2118. * the buffer for IO
  2119. */
  2120. mark_buffer_dirty(bh);
  2121. sbi->s_lvid_dirty = 0;
  2122. }
  2123. mutex_unlock(&sbi->s_alloc_mutex);
  2124. return 0;
  2125. }
  2126. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  2127. {
  2128. struct super_block *sb = dentry->d_sb;
  2129. struct udf_sb_info *sbi = UDF_SB(sb);
  2130. struct logicalVolIntegrityDescImpUse *lvidiu;
  2131. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  2132. lvidiu = udf_sb_lvidiu(sb);
  2133. buf->f_type = UDF_SUPER_MAGIC;
  2134. buf->f_bsize = sb->s_blocksize;
  2135. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  2136. buf->f_bfree = udf_count_free(sb);
  2137. buf->f_bavail = buf->f_bfree;
  2138. /*
  2139. * Let's pretend each free block is also a free 'inode' since UDF does
  2140. * not have separate preallocated table of inodes.
  2141. */
  2142. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  2143. le32_to_cpu(lvidiu->numDirs)) : 0)
  2144. + buf->f_bfree;
  2145. buf->f_ffree = buf->f_bfree;
  2146. buf->f_namelen = UDF_NAME_LEN;
  2147. buf->f_fsid = u64_to_fsid(id);
  2148. return 0;
  2149. }
  2150. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  2151. struct udf_bitmap *bitmap)
  2152. {
  2153. struct buffer_head *bh = NULL;
  2154. unsigned int accum = 0;
  2155. int index;
  2156. udf_pblk_t block = 0, newblock;
  2157. struct kernel_lb_addr loc;
  2158. uint32_t bytes;
  2159. uint8_t *ptr;
  2160. uint16_t ident;
  2161. struct spaceBitmapDesc *bm;
  2162. loc.logicalBlockNum = bitmap->s_extPosition;
  2163. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  2164. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  2165. if (!bh) {
  2166. udf_err(sb, "udf_count_free failed\n");
  2167. goto out;
  2168. } else if (ident != TAG_IDENT_SBD) {
  2169. brelse(bh);
  2170. udf_err(sb, "udf_count_free failed\n");
  2171. goto out;
  2172. }
  2173. bm = (struct spaceBitmapDesc *)bh->b_data;
  2174. bytes = le32_to_cpu(bm->numOfBytes);
  2175. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  2176. ptr = (uint8_t *)bh->b_data;
  2177. while (bytes > 0) {
  2178. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  2179. accum += bitmap_weight((const unsigned long *)(ptr + index),
  2180. cur_bytes * 8);
  2181. bytes -= cur_bytes;
  2182. if (bytes) {
  2183. brelse(bh);
  2184. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  2185. bh = udf_tread(sb, newblock);
  2186. if (!bh) {
  2187. udf_debug("read failed\n");
  2188. goto out;
  2189. }
  2190. index = 0;
  2191. ptr = (uint8_t *)bh->b_data;
  2192. }
  2193. }
  2194. brelse(bh);
  2195. out:
  2196. return accum;
  2197. }
  2198. static unsigned int udf_count_free_table(struct super_block *sb,
  2199. struct inode *table)
  2200. {
  2201. unsigned int accum = 0;
  2202. uint32_t elen;
  2203. struct kernel_lb_addr eloc;
  2204. int8_t etype;
  2205. struct extent_position epos;
  2206. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2207. epos.block = UDF_I(table)->i_location;
  2208. epos.offset = sizeof(struct unallocSpaceEntry);
  2209. epos.bh = NULL;
  2210. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2211. accum += (elen >> table->i_sb->s_blocksize_bits);
  2212. brelse(epos.bh);
  2213. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2214. return accum;
  2215. }
  2216. static unsigned int udf_count_free(struct super_block *sb)
  2217. {
  2218. unsigned int accum = 0;
  2219. struct udf_sb_info *sbi = UDF_SB(sb);
  2220. struct udf_part_map *map;
  2221. unsigned int part = sbi->s_partition;
  2222. int ptype = sbi->s_partmaps[part].s_partition_type;
  2223. if (ptype == UDF_METADATA_MAP25) {
  2224. part = sbi->s_partmaps[part].s_type_specific.s_metadata.
  2225. s_phys_partition_ref;
  2226. } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
  2227. /*
  2228. * Filesystems with VAT are append-only and we cannot write to
  2229. * them. Let's just report 0 here.
  2230. */
  2231. return 0;
  2232. }
  2233. if (sbi->s_lvid_bh) {
  2234. struct logicalVolIntegrityDesc *lvid =
  2235. (struct logicalVolIntegrityDesc *)
  2236. sbi->s_lvid_bh->b_data;
  2237. if (le32_to_cpu(lvid->numOfPartitions) > part) {
  2238. accum = le32_to_cpu(
  2239. lvid->freeSpaceTable[part]);
  2240. if (accum == 0xFFFFFFFF)
  2241. accum = 0;
  2242. }
  2243. }
  2244. if (accum)
  2245. return accum;
  2246. map = &sbi->s_partmaps[part];
  2247. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2248. accum += udf_count_free_bitmap(sb,
  2249. map->s_uspace.s_bitmap);
  2250. }
  2251. if (accum)
  2252. return accum;
  2253. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2254. accum += udf_count_free_table(sb,
  2255. map->s_uspace.s_table);
  2256. }
  2257. return accum;
  2258. }
  2259. MODULE_AUTHOR("Ben Fennema");
  2260. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  2261. MODULE_LICENSE("GPL");
  2262. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
  2263. module_init(init_udf_fs)
  2264. module_exit(exit_udf_fs)