super.c 66 KB

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