super.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  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 <linux/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_special(sb, &addr);
  870. if (IS_ERR(metadata_fe)) {
  871. udf_warn(sb, "metadata inode efe not found\n");
  872. return metadata_fe;
  873. }
  874. if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
  875. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  876. iput(metadata_fe);
  877. return ERR_PTR(-EIO);
  878. }
  879. return metadata_fe;
  880. }
  881. static int udf_load_metadata_files(struct super_block *sb, int partition)
  882. {
  883. struct udf_sb_info *sbi = UDF_SB(sb);
  884. struct udf_part_map *map;
  885. struct udf_meta_data *mdata;
  886. struct kernel_lb_addr addr;
  887. struct inode *fe;
  888. map = &sbi->s_partmaps[partition];
  889. mdata = &map->s_type_specific.s_metadata;
  890. /* metadata address */
  891. udf_debug("Metadata file location: block = %d part = %d\n",
  892. mdata->s_meta_file_loc, map->s_partition_num);
  893. fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
  894. map->s_partition_num);
  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, map->s_partition_num);
  899. fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
  900. map->s_partition_num);
  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 = map->s_partition_num;
  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, SPARABLE and METADATA 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);
  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. * udf_load_logicalvolint
  1392. *
  1393. */
  1394. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1395. {
  1396. struct buffer_head *bh = NULL;
  1397. uint16_t ident;
  1398. struct udf_sb_info *sbi = UDF_SB(sb);
  1399. struct logicalVolIntegrityDesc *lvid;
  1400. while (loc.extLength > 0 &&
  1401. (bh = udf_read_tagged(sb, loc.extLocation,
  1402. loc.extLocation, &ident)) &&
  1403. ident == TAG_IDENT_LVID) {
  1404. sbi->s_lvid_bh = bh;
  1405. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1406. if (lvid->nextIntegrityExt.extLength)
  1407. udf_load_logicalvolint(sb,
  1408. leea_to_cpu(lvid->nextIntegrityExt));
  1409. if (sbi->s_lvid_bh != bh)
  1410. brelse(bh);
  1411. loc.extLength -= sb->s_blocksize;
  1412. loc.extLocation++;
  1413. }
  1414. if (sbi->s_lvid_bh != bh)
  1415. brelse(bh);
  1416. }
  1417. /*
  1418. * Process a main/reserve volume descriptor sequence.
  1419. * @block First block of first extent of the sequence.
  1420. * @lastblock Lastblock of first extent of the sequence.
  1421. * @fileset There we store extent containing root fileset
  1422. *
  1423. * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
  1424. * sequence
  1425. */
  1426. static noinline int udf_process_sequence(
  1427. struct super_block *sb,
  1428. sector_t block, sector_t lastblock,
  1429. struct kernel_lb_addr *fileset)
  1430. {
  1431. struct buffer_head *bh = NULL;
  1432. struct udf_vds_record vds[VDS_POS_LENGTH];
  1433. struct udf_vds_record *curr;
  1434. struct generic_desc *gd;
  1435. struct volDescPtr *vdp;
  1436. int done = 0;
  1437. uint32_t vdsn;
  1438. uint16_t ident;
  1439. long next_s = 0, next_e = 0;
  1440. int ret;
  1441. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1442. /*
  1443. * Read the main descriptor sequence and find which descriptors
  1444. * are in it.
  1445. */
  1446. for (; (!done && block <= lastblock); block++) {
  1447. bh = udf_read_tagged(sb, block, block, &ident);
  1448. if (!bh) {
  1449. udf_err(sb,
  1450. "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
  1451. (unsigned long long)block);
  1452. return -EAGAIN;
  1453. }
  1454. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1455. gd = (struct generic_desc *)bh->b_data;
  1456. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1457. switch (ident) {
  1458. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1459. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1460. if (vdsn >= curr->volDescSeqNum) {
  1461. curr->volDescSeqNum = vdsn;
  1462. curr->block = block;
  1463. }
  1464. break;
  1465. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1466. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1467. if (vdsn >= curr->volDescSeqNum) {
  1468. curr->volDescSeqNum = vdsn;
  1469. curr->block = block;
  1470. vdp = (struct volDescPtr *)bh->b_data;
  1471. next_s = le32_to_cpu(
  1472. vdp->nextVolDescSeqExt.extLocation);
  1473. next_e = le32_to_cpu(
  1474. vdp->nextVolDescSeqExt.extLength);
  1475. next_e = next_e >> sb->s_blocksize_bits;
  1476. next_e += next_s;
  1477. }
  1478. break;
  1479. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1480. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1481. if (vdsn >= curr->volDescSeqNum) {
  1482. curr->volDescSeqNum = vdsn;
  1483. curr->block = block;
  1484. }
  1485. break;
  1486. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1487. curr = &vds[VDS_POS_PARTITION_DESC];
  1488. if (!curr->block)
  1489. curr->block = block;
  1490. break;
  1491. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1492. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1493. if (vdsn >= curr->volDescSeqNum) {
  1494. curr->volDescSeqNum = vdsn;
  1495. curr->block = block;
  1496. }
  1497. break;
  1498. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1499. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1500. if (vdsn >= curr->volDescSeqNum) {
  1501. curr->volDescSeqNum = vdsn;
  1502. curr->block = block;
  1503. }
  1504. break;
  1505. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1506. vds[VDS_POS_TERMINATING_DESC].block = block;
  1507. if (next_e) {
  1508. block = next_s;
  1509. lastblock = next_e;
  1510. next_s = next_e = 0;
  1511. } else
  1512. done = 1;
  1513. break;
  1514. }
  1515. brelse(bh);
  1516. }
  1517. /*
  1518. * Now read interesting descriptors again and process them
  1519. * in a suitable order
  1520. */
  1521. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1522. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1523. return -EAGAIN;
  1524. }
  1525. ret = udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block);
  1526. if (ret < 0)
  1527. return ret;
  1528. if (vds[VDS_POS_LOGICAL_VOL_DESC].block) {
  1529. ret = udf_load_logicalvol(sb,
  1530. vds[VDS_POS_LOGICAL_VOL_DESC].block,
  1531. fileset);
  1532. if (ret < 0)
  1533. return ret;
  1534. }
  1535. if (vds[VDS_POS_PARTITION_DESC].block) {
  1536. /*
  1537. * We rescan the whole descriptor sequence to find
  1538. * partition descriptor blocks and process them.
  1539. */
  1540. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1541. block < vds[VDS_POS_TERMINATING_DESC].block;
  1542. block++) {
  1543. ret = udf_load_partdesc(sb, block);
  1544. if (ret < 0)
  1545. return ret;
  1546. }
  1547. }
  1548. return 0;
  1549. }
  1550. /*
  1551. * Load Volume Descriptor Sequence described by anchor in bh
  1552. *
  1553. * Returns <0 on error, 0 on success
  1554. */
  1555. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1556. struct kernel_lb_addr *fileset)
  1557. {
  1558. struct anchorVolDescPtr *anchor;
  1559. sector_t main_s, main_e, reserve_s, reserve_e;
  1560. int ret;
  1561. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1562. /* Locate the main sequence */
  1563. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1564. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1565. main_e = main_e >> sb->s_blocksize_bits;
  1566. main_e += main_s;
  1567. /* Locate the reserve sequence */
  1568. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1569. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1570. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1571. reserve_e += reserve_s;
  1572. /* Process the main & reserve sequences */
  1573. /* responsible for finding the PartitionDesc(s) */
  1574. ret = udf_process_sequence(sb, main_s, main_e, fileset);
  1575. if (ret != -EAGAIN)
  1576. return ret;
  1577. udf_sb_free_partitions(sb);
  1578. ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1579. if (ret < 0) {
  1580. udf_sb_free_partitions(sb);
  1581. /* No sequence was OK, return -EIO */
  1582. if (ret == -EAGAIN)
  1583. ret = -EIO;
  1584. }
  1585. return ret;
  1586. }
  1587. /*
  1588. * Check whether there is an anchor block in the given block and
  1589. * load Volume Descriptor Sequence if so.
  1590. *
  1591. * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
  1592. * block
  1593. */
  1594. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1595. struct kernel_lb_addr *fileset)
  1596. {
  1597. struct buffer_head *bh;
  1598. uint16_t ident;
  1599. int ret;
  1600. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1601. udf_fixed_to_variable(block) >=
  1602. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1603. return -EAGAIN;
  1604. bh = udf_read_tagged(sb, block, block, &ident);
  1605. if (!bh)
  1606. return -EAGAIN;
  1607. if (ident != TAG_IDENT_AVDP) {
  1608. brelse(bh);
  1609. return -EAGAIN;
  1610. }
  1611. ret = udf_load_sequence(sb, bh, fileset);
  1612. brelse(bh);
  1613. return ret;
  1614. }
  1615. /*
  1616. * Search for an anchor volume descriptor pointer.
  1617. *
  1618. * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
  1619. * of anchors.
  1620. */
  1621. static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
  1622. struct kernel_lb_addr *fileset)
  1623. {
  1624. sector_t last[6];
  1625. int i;
  1626. struct udf_sb_info *sbi = UDF_SB(sb);
  1627. int last_count = 0;
  1628. int ret;
  1629. /* First try user provided anchor */
  1630. if (sbi->s_anchor) {
  1631. ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
  1632. if (ret != -EAGAIN)
  1633. return ret;
  1634. }
  1635. /*
  1636. * according to spec, anchor is in either:
  1637. * block 256
  1638. * lastblock-256
  1639. * lastblock
  1640. * however, if the disc isn't closed, it could be 512.
  1641. */
  1642. ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
  1643. if (ret != -EAGAIN)
  1644. return ret;
  1645. /*
  1646. * The trouble is which block is the last one. Drives often misreport
  1647. * this so we try various possibilities.
  1648. */
  1649. last[last_count++] = *lastblock;
  1650. if (*lastblock >= 1)
  1651. last[last_count++] = *lastblock - 1;
  1652. last[last_count++] = *lastblock + 1;
  1653. if (*lastblock >= 2)
  1654. last[last_count++] = *lastblock - 2;
  1655. if (*lastblock >= 150)
  1656. last[last_count++] = *lastblock - 150;
  1657. if (*lastblock >= 152)
  1658. last[last_count++] = *lastblock - 152;
  1659. for (i = 0; i < last_count; i++) {
  1660. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1661. sb->s_blocksize_bits)
  1662. continue;
  1663. ret = udf_check_anchor_block(sb, last[i], fileset);
  1664. if (ret != -EAGAIN) {
  1665. if (!ret)
  1666. *lastblock = last[i];
  1667. return ret;
  1668. }
  1669. if (last[i] < 256)
  1670. continue;
  1671. ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
  1672. if (ret != -EAGAIN) {
  1673. if (!ret)
  1674. *lastblock = last[i];
  1675. return ret;
  1676. }
  1677. }
  1678. /* Finally try block 512 in case media is open */
  1679. return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
  1680. }
  1681. /*
  1682. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1683. * area specified by it. The function expects sbi->s_lastblock to be the last
  1684. * block on the media.
  1685. *
  1686. * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
  1687. * was not found.
  1688. */
  1689. static int udf_find_anchor(struct super_block *sb,
  1690. struct kernel_lb_addr *fileset)
  1691. {
  1692. struct udf_sb_info *sbi = UDF_SB(sb);
  1693. sector_t lastblock = sbi->s_last_block;
  1694. int ret;
  1695. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1696. if (ret != -EAGAIN)
  1697. goto out;
  1698. /* No anchor found? Try VARCONV conversion of block numbers */
  1699. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1700. lastblock = udf_variable_to_fixed(sbi->s_last_block);
  1701. /* Firstly, we try to not convert number of the last block */
  1702. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1703. if (ret != -EAGAIN)
  1704. goto out;
  1705. lastblock = sbi->s_last_block;
  1706. /* Secondly, we try with converted number of the last block */
  1707. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1708. if (ret < 0) {
  1709. /* VARCONV didn't help. Clear it. */
  1710. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1711. }
  1712. out:
  1713. if (ret == 0)
  1714. sbi->s_last_block = lastblock;
  1715. return ret;
  1716. }
  1717. /*
  1718. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1719. * Descriptor Sequence.
  1720. *
  1721. * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
  1722. * block was not found.
  1723. */
  1724. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1725. int silent, struct kernel_lb_addr *fileset)
  1726. {
  1727. struct udf_sb_info *sbi = UDF_SB(sb);
  1728. loff_t nsr_off;
  1729. int ret;
  1730. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1731. if (!silent)
  1732. udf_warn(sb, "Bad block size\n");
  1733. return -EINVAL;
  1734. }
  1735. sbi->s_last_block = uopt->lastblock;
  1736. if (!uopt->novrs) {
  1737. /* Check that it is NSR02 compliant */
  1738. nsr_off = udf_check_vsd(sb);
  1739. if (!nsr_off) {
  1740. if (!silent)
  1741. udf_warn(sb, "No VRS found\n");
  1742. return 0;
  1743. }
  1744. if (nsr_off == -1)
  1745. udf_debug("Failed to read sector at offset %d. "
  1746. "Assuming open disc. Skipping validity "
  1747. "check\n", VSD_FIRST_SECTOR_OFFSET);
  1748. if (!sbi->s_last_block)
  1749. sbi->s_last_block = udf_get_last_block(sb);
  1750. } else {
  1751. udf_debug("Validity check skipped because of novrs option\n");
  1752. }
  1753. /* Look for anchor block and load Volume Descriptor Sequence */
  1754. sbi->s_anchor = uopt->anchor;
  1755. ret = udf_find_anchor(sb, fileset);
  1756. if (ret < 0) {
  1757. if (!silent && ret == -EAGAIN)
  1758. udf_warn(sb, "No anchor found\n");
  1759. return ret;
  1760. }
  1761. return 0;
  1762. }
  1763. static void udf_open_lvid(struct super_block *sb)
  1764. {
  1765. struct udf_sb_info *sbi = UDF_SB(sb);
  1766. struct buffer_head *bh = sbi->s_lvid_bh;
  1767. struct logicalVolIntegrityDesc *lvid;
  1768. struct logicalVolIntegrityDescImpUse *lvidiu;
  1769. if (!bh)
  1770. return;
  1771. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1772. lvidiu = udf_sb_lvidiu(sb);
  1773. if (!lvidiu)
  1774. return;
  1775. mutex_lock(&sbi->s_alloc_mutex);
  1776. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1777. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1778. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1779. CURRENT_TIME);
  1780. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1781. lvid->descTag.descCRC = cpu_to_le16(
  1782. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1783. le16_to_cpu(lvid->descTag.descCRCLength)));
  1784. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1785. mark_buffer_dirty(bh);
  1786. sbi->s_lvid_dirty = 0;
  1787. mutex_unlock(&sbi->s_alloc_mutex);
  1788. /* Make opening of filesystem visible on the media immediately */
  1789. sync_dirty_buffer(bh);
  1790. }
  1791. static void udf_close_lvid(struct super_block *sb)
  1792. {
  1793. struct udf_sb_info *sbi = UDF_SB(sb);
  1794. struct buffer_head *bh = sbi->s_lvid_bh;
  1795. struct logicalVolIntegrityDesc *lvid;
  1796. struct logicalVolIntegrityDescImpUse *lvidiu;
  1797. if (!bh)
  1798. return;
  1799. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1800. lvidiu = udf_sb_lvidiu(sb);
  1801. if (!lvidiu)
  1802. return;
  1803. mutex_lock(&sbi->s_alloc_mutex);
  1804. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1805. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1806. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1807. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1808. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1809. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1810. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1811. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1812. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1813. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1814. lvid->descTag.descCRC = cpu_to_le16(
  1815. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1816. le16_to_cpu(lvid->descTag.descCRCLength)));
  1817. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1818. /*
  1819. * We set buffer uptodate unconditionally here to avoid spurious
  1820. * warnings from mark_buffer_dirty() when previous EIO has marked
  1821. * the buffer as !uptodate
  1822. */
  1823. set_buffer_uptodate(bh);
  1824. mark_buffer_dirty(bh);
  1825. sbi->s_lvid_dirty = 0;
  1826. mutex_unlock(&sbi->s_alloc_mutex);
  1827. /* Make closing of filesystem visible on the media immediately */
  1828. sync_dirty_buffer(bh);
  1829. }
  1830. u64 lvid_get_unique_id(struct super_block *sb)
  1831. {
  1832. struct buffer_head *bh;
  1833. struct udf_sb_info *sbi = UDF_SB(sb);
  1834. struct logicalVolIntegrityDesc *lvid;
  1835. struct logicalVolHeaderDesc *lvhd;
  1836. u64 uniqueID;
  1837. u64 ret;
  1838. bh = sbi->s_lvid_bh;
  1839. if (!bh)
  1840. return 0;
  1841. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1842. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1843. mutex_lock(&sbi->s_alloc_mutex);
  1844. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1845. if (!(++uniqueID & 0xFFFFFFFF))
  1846. uniqueID += 16;
  1847. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1848. mutex_unlock(&sbi->s_alloc_mutex);
  1849. mark_buffer_dirty(bh);
  1850. return ret;
  1851. }
  1852. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1853. {
  1854. int ret = -EINVAL;
  1855. struct inode *inode = NULL;
  1856. struct udf_options uopt;
  1857. struct kernel_lb_addr rootdir, fileset;
  1858. struct udf_sb_info *sbi;
  1859. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1860. uopt.uid = INVALID_UID;
  1861. uopt.gid = INVALID_GID;
  1862. uopt.umask = 0;
  1863. uopt.fmode = UDF_INVALID_MODE;
  1864. uopt.dmode = UDF_INVALID_MODE;
  1865. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1866. if (!sbi)
  1867. return -ENOMEM;
  1868. sb->s_fs_info = sbi;
  1869. mutex_init(&sbi->s_alloc_mutex);
  1870. if (!udf_parse_options((char *)options, &uopt, false))
  1871. goto parse_options_failure;
  1872. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1873. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1874. udf_err(sb, "utf8 cannot be combined with iocharset\n");
  1875. goto parse_options_failure;
  1876. }
  1877. #ifdef CONFIG_UDF_NLS
  1878. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1879. uopt.nls_map = load_nls_default();
  1880. if (!uopt.nls_map)
  1881. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1882. else
  1883. udf_debug("Using default NLS map\n");
  1884. }
  1885. #endif
  1886. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1887. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1888. fileset.logicalBlockNum = 0xFFFFFFFF;
  1889. fileset.partitionReferenceNum = 0xFFFF;
  1890. sbi->s_flags = uopt.flags;
  1891. sbi->s_uid = uopt.uid;
  1892. sbi->s_gid = uopt.gid;
  1893. sbi->s_umask = uopt.umask;
  1894. sbi->s_fmode = uopt.fmode;
  1895. sbi->s_dmode = uopt.dmode;
  1896. sbi->s_nls_map = uopt.nls_map;
  1897. rwlock_init(&sbi->s_cred_lock);
  1898. if (uopt.session == 0xFFFFFFFF)
  1899. sbi->s_session = udf_get_last_session(sb);
  1900. else
  1901. sbi->s_session = uopt.session;
  1902. udf_debug("Multi-session=%d\n", sbi->s_session);
  1903. /* Fill in the rest of the superblock */
  1904. sb->s_op = &udf_sb_ops;
  1905. sb->s_export_op = &udf_export_ops;
  1906. sb->s_magic = UDF_SUPER_MAGIC;
  1907. sb->s_time_gran = 1000;
  1908. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1909. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1910. } else {
  1911. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1912. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1913. if (ret == -EAGAIN && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1914. if (!silent)
  1915. pr_notice("Rescanning with blocksize %d\n",
  1916. UDF_DEFAULT_BLOCKSIZE);
  1917. brelse(sbi->s_lvid_bh);
  1918. sbi->s_lvid_bh = NULL;
  1919. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1920. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1921. }
  1922. }
  1923. if (ret < 0) {
  1924. if (ret == -EAGAIN) {
  1925. udf_warn(sb, "No partition found (1)\n");
  1926. ret = -EINVAL;
  1927. }
  1928. goto error_out;
  1929. }
  1930. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1931. if (sbi->s_lvid_bh) {
  1932. struct logicalVolIntegrityDescImpUse *lvidiu =
  1933. udf_sb_lvidiu(sb);
  1934. uint16_t minUDFReadRev;
  1935. uint16_t minUDFWriteRev;
  1936. if (!lvidiu) {
  1937. ret = -EINVAL;
  1938. goto error_out;
  1939. }
  1940. minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1941. minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1942. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1943. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1944. minUDFReadRev,
  1945. UDF_MAX_READ_VERSION);
  1946. ret = -EINVAL;
  1947. goto error_out;
  1948. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
  1949. !(sb->s_flags & MS_RDONLY)) {
  1950. ret = -EACCES;
  1951. goto error_out;
  1952. }
  1953. sbi->s_udfrev = minUDFWriteRev;
  1954. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1955. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1956. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1957. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1958. }
  1959. if (!sbi->s_partitions) {
  1960. udf_warn(sb, "No partition found (2)\n");
  1961. ret = -EINVAL;
  1962. goto error_out;
  1963. }
  1964. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1965. UDF_PART_FLAG_READ_ONLY &&
  1966. !(sb->s_flags & MS_RDONLY)) {
  1967. ret = -EACCES;
  1968. goto error_out;
  1969. }
  1970. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1971. udf_warn(sb, "No fileset found\n");
  1972. ret = -EINVAL;
  1973. goto error_out;
  1974. }
  1975. if (!silent) {
  1976. struct timestamp ts;
  1977. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1978. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1979. sbi->s_volume_ident,
  1980. le16_to_cpu(ts.year), ts.month, ts.day,
  1981. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1982. }
  1983. if (!(sb->s_flags & MS_RDONLY))
  1984. udf_open_lvid(sb);
  1985. /* Assign the root inode */
  1986. /* assign inodes by physical block number */
  1987. /* perhaps it's not extensible enough, but for now ... */
  1988. inode = udf_iget(sb, &rootdir);
  1989. if (IS_ERR(inode)) {
  1990. udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
  1991. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1992. ret = PTR_ERR(inode);
  1993. goto error_out;
  1994. }
  1995. /* Allocate a dentry for the root inode */
  1996. sb->s_root = d_make_root(inode);
  1997. if (!sb->s_root) {
  1998. udf_err(sb, "Couldn't allocate root dentry\n");
  1999. ret = -ENOMEM;
  2000. goto error_out;
  2001. }
  2002. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2003. sb->s_max_links = UDF_MAX_LINKS;
  2004. return 0;
  2005. error_out:
  2006. iput(sbi->s_vat_inode);
  2007. parse_options_failure:
  2008. #ifdef CONFIG_UDF_NLS
  2009. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  2010. unload_nls(sbi->s_nls_map);
  2011. #endif
  2012. if (!(sb->s_flags & MS_RDONLY))
  2013. udf_close_lvid(sb);
  2014. brelse(sbi->s_lvid_bh);
  2015. udf_sb_free_partitions(sb);
  2016. kfree(sbi);
  2017. sb->s_fs_info = NULL;
  2018. return ret;
  2019. }
  2020. void _udf_err(struct super_block *sb, const char *function,
  2021. const char *fmt, ...)
  2022. {
  2023. struct va_format vaf;
  2024. va_list args;
  2025. va_start(args, fmt);
  2026. vaf.fmt = fmt;
  2027. vaf.va = &args;
  2028. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  2029. va_end(args);
  2030. }
  2031. void _udf_warn(struct super_block *sb, const char *function,
  2032. const char *fmt, ...)
  2033. {
  2034. struct va_format vaf;
  2035. va_list args;
  2036. va_start(args, fmt);
  2037. vaf.fmt = fmt;
  2038. vaf.va = &args;
  2039. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  2040. va_end(args);
  2041. }
  2042. static void udf_put_super(struct super_block *sb)
  2043. {
  2044. struct udf_sb_info *sbi;
  2045. sbi = UDF_SB(sb);
  2046. iput(sbi->s_vat_inode);
  2047. #ifdef CONFIG_UDF_NLS
  2048. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  2049. unload_nls(sbi->s_nls_map);
  2050. #endif
  2051. if (!(sb->s_flags & MS_RDONLY))
  2052. udf_close_lvid(sb);
  2053. brelse(sbi->s_lvid_bh);
  2054. udf_sb_free_partitions(sb);
  2055. kfree(sb->s_fs_info);
  2056. sb->s_fs_info = NULL;
  2057. }
  2058. static int udf_sync_fs(struct super_block *sb, int wait)
  2059. {
  2060. struct udf_sb_info *sbi = UDF_SB(sb);
  2061. mutex_lock(&sbi->s_alloc_mutex);
  2062. if (sbi->s_lvid_dirty) {
  2063. /*
  2064. * Blockdevice will be synced later so we don't have to submit
  2065. * the buffer for IO
  2066. */
  2067. mark_buffer_dirty(sbi->s_lvid_bh);
  2068. sbi->s_lvid_dirty = 0;
  2069. }
  2070. mutex_unlock(&sbi->s_alloc_mutex);
  2071. return 0;
  2072. }
  2073. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  2074. {
  2075. struct super_block *sb = dentry->d_sb;
  2076. struct udf_sb_info *sbi = UDF_SB(sb);
  2077. struct logicalVolIntegrityDescImpUse *lvidiu;
  2078. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  2079. lvidiu = udf_sb_lvidiu(sb);
  2080. buf->f_type = UDF_SUPER_MAGIC;
  2081. buf->f_bsize = sb->s_blocksize;
  2082. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  2083. buf->f_bfree = udf_count_free(sb);
  2084. buf->f_bavail = buf->f_bfree;
  2085. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  2086. le32_to_cpu(lvidiu->numDirs)) : 0)
  2087. + buf->f_bfree;
  2088. buf->f_ffree = buf->f_bfree;
  2089. buf->f_namelen = UDF_NAME_LEN - 2;
  2090. buf->f_fsid.val[0] = (u32)id;
  2091. buf->f_fsid.val[1] = (u32)(id >> 32);
  2092. return 0;
  2093. }
  2094. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  2095. struct udf_bitmap *bitmap)
  2096. {
  2097. struct buffer_head *bh = NULL;
  2098. unsigned int accum = 0;
  2099. int index;
  2100. int block = 0, newblock;
  2101. struct kernel_lb_addr loc;
  2102. uint32_t bytes;
  2103. uint8_t *ptr;
  2104. uint16_t ident;
  2105. struct spaceBitmapDesc *bm;
  2106. loc.logicalBlockNum = bitmap->s_extPosition;
  2107. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  2108. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  2109. if (!bh) {
  2110. udf_err(sb, "udf_count_free failed\n");
  2111. goto out;
  2112. } else if (ident != TAG_IDENT_SBD) {
  2113. brelse(bh);
  2114. udf_err(sb, "udf_count_free failed\n");
  2115. goto out;
  2116. }
  2117. bm = (struct spaceBitmapDesc *)bh->b_data;
  2118. bytes = le32_to_cpu(bm->numOfBytes);
  2119. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  2120. ptr = (uint8_t *)bh->b_data;
  2121. while (bytes > 0) {
  2122. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  2123. accum += bitmap_weight((const unsigned long *)(ptr + index),
  2124. cur_bytes * 8);
  2125. bytes -= cur_bytes;
  2126. if (bytes) {
  2127. brelse(bh);
  2128. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  2129. bh = udf_tread(sb, newblock);
  2130. if (!bh) {
  2131. udf_debug("read failed\n");
  2132. goto out;
  2133. }
  2134. index = 0;
  2135. ptr = (uint8_t *)bh->b_data;
  2136. }
  2137. }
  2138. brelse(bh);
  2139. out:
  2140. return accum;
  2141. }
  2142. static unsigned int udf_count_free_table(struct super_block *sb,
  2143. struct inode *table)
  2144. {
  2145. unsigned int accum = 0;
  2146. uint32_t elen;
  2147. struct kernel_lb_addr eloc;
  2148. int8_t etype;
  2149. struct extent_position epos;
  2150. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2151. epos.block = UDF_I(table)->i_location;
  2152. epos.offset = sizeof(struct unallocSpaceEntry);
  2153. epos.bh = NULL;
  2154. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2155. accum += (elen >> table->i_sb->s_blocksize_bits);
  2156. brelse(epos.bh);
  2157. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2158. return accum;
  2159. }
  2160. static unsigned int udf_count_free(struct super_block *sb)
  2161. {
  2162. unsigned int accum = 0;
  2163. struct udf_sb_info *sbi;
  2164. struct udf_part_map *map;
  2165. sbi = UDF_SB(sb);
  2166. if (sbi->s_lvid_bh) {
  2167. struct logicalVolIntegrityDesc *lvid =
  2168. (struct logicalVolIntegrityDesc *)
  2169. sbi->s_lvid_bh->b_data;
  2170. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2171. accum = le32_to_cpu(
  2172. lvid->freeSpaceTable[sbi->s_partition]);
  2173. if (accum == 0xFFFFFFFF)
  2174. accum = 0;
  2175. }
  2176. }
  2177. if (accum)
  2178. return accum;
  2179. map = &sbi->s_partmaps[sbi->s_partition];
  2180. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2181. accum += udf_count_free_bitmap(sb,
  2182. map->s_uspace.s_bitmap);
  2183. }
  2184. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2185. accum += udf_count_free_bitmap(sb,
  2186. map->s_fspace.s_bitmap);
  2187. }
  2188. if (accum)
  2189. return accum;
  2190. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2191. accum += udf_count_free_table(sb,
  2192. map->s_uspace.s_table);
  2193. }
  2194. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2195. accum += udf_count_free_table(sb,
  2196. map->s_fspace.s_table);
  2197. }
  2198. return accum;
  2199. }