super.c 62 KB

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