super.c 66 KB

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