super.c 65 KB

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