super.c 66 KB

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