super.c 67 KB

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