super.c 67 KB

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