super.c 65 KB

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