super.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  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. static struct kmem_cache *udf_inode_cachep;
  111. static struct inode *udf_alloc_inode(struct super_block *sb)
  112. {
  113. struct udf_inode_info *ei;
  114. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  115. if (!ei)
  116. return NULL;
  117. ei->i_unique = 0;
  118. ei->i_lenExtents = 0;
  119. ei->i_next_alloc_block = 0;
  120. ei->i_next_alloc_goal = 0;
  121. ei->i_strat4096 = 0;
  122. init_rwsem(&ei->i_data_sem);
  123. return &ei->vfs_inode;
  124. }
  125. static void udf_i_callback(struct rcu_head *head)
  126. {
  127. struct inode *inode = container_of(head, struct inode, i_rcu);
  128. kmem_cache_free(udf_inode_cachep, UDF_I(inode));
  129. }
  130. static void udf_destroy_inode(struct inode *inode)
  131. {
  132. call_rcu(&inode->i_rcu, udf_i_callback);
  133. }
  134. static void init_once(void *foo)
  135. {
  136. struct udf_inode_info *ei = (struct udf_inode_info *)foo;
  137. ei->i_ext.i_data = NULL;
  138. inode_init_once(&ei->vfs_inode);
  139. }
  140. static int init_inodecache(void)
  141. {
  142. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  143. sizeof(struct udf_inode_info),
  144. 0, (SLAB_RECLAIM_ACCOUNT |
  145. SLAB_MEM_SPREAD),
  146. init_once);
  147. if (!udf_inode_cachep)
  148. return -ENOMEM;
  149. return 0;
  150. }
  151. static void destroy_inodecache(void)
  152. {
  153. /*
  154. * Make sure all delayed rcu free inodes are flushed before we
  155. * destroy cache.
  156. */
  157. rcu_barrier();
  158. kmem_cache_destroy(udf_inode_cachep);
  159. }
  160. /* Superblock operations */
  161. static const struct super_operations udf_sb_ops = {
  162. .alloc_inode = udf_alloc_inode,
  163. .destroy_inode = udf_destroy_inode,
  164. .write_inode = udf_write_inode,
  165. .evict_inode = udf_evict_inode,
  166. .put_super = udf_put_super,
  167. .sync_fs = udf_sync_fs,
  168. .statfs = udf_statfs,
  169. .remount_fs = udf_remount_fs,
  170. .show_options = udf_show_options,
  171. };
  172. struct udf_options {
  173. unsigned char novrs;
  174. unsigned int blocksize;
  175. unsigned int session;
  176. unsigned int lastblock;
  177. unsigned int anchor;
  178. unsigned int volume;
  179. unsigned short partition;
  180. unsigned int fileset;
  181. unsigned int rootdir;
  182. unsigned int flags;
  183. umode_t umask;
  184. kgid_t gid;
  185. kuid_t uid;
  186. umode_t fmode;
  187. umode_t dmode;
  188. struct nls_table *nls_map;
  189. };
  190. static int __init init_udf_fs(void)
  191. {
  192. int err;
  193. err = init_inodecache();
  194. if (err)
  195. goto out1;
  196. err = register_filesystem(&udf_fstype);
  197. if (err)
  198. goto out;
  199. return 0;
  200. out:
  201. destroy_inodecache();
  202. out1:
  203. return err;
  204. }
  205. static void __exit exit_udf_fs(void)
  206. {
  207. unregister_filesystem(&udf_fstype);
  208. destroy_inodecache();
  209. }
  210. module_init(init_udf_fs)
  211. module_exit(exit_udf_fs)
  212. static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
  213. {
  214. struct udf_sb_info *sbi = UDF_SB(sb);
  215. sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
  216. GFP_KERNEL);
  217. if (!sbi->s_partmaps) {
  218. udf_err(sb, "Unable to allocate space for %d partition maps\n",
  219. count);
  220. sbi->s_partitions = 0;
  221. return -ENOMEM;
  222. }
  223. sbi->s_partitions = count;
  224. return 0;
  225. }
  226. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  227. {
  228. int i;
  229. int nr_groups = bitmap->s_nr_groups;
  230. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  231. nr_groups);
  232. for (i = 0; i < nr_groups; i++)
  233. if (bitmap->s_block_bitmap[i])
  234. brelse(bitmap->s_block_bitmap[i]);
  235. if (size <= PAGE_SIZE)
  236. kfree(bitmap);
  237. else
  238. vfree(bitmap);
  239. }
  240. static void udf_free_partition(struct udf_part_map *map)
  241. {
  242. int i;
  243. struct udf_meta_data *mdata;
  244. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  245. iput(map->s_uspace.s_table);
  246. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  247. iput(map->s_fspace.s_table);
  248. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  249. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  250. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  251. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  252. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  253. for (i = 0; i < 4; i++)
  254. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  255. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  256. mdata = &map->s_type_specific.s_metadata;
  257. iput(mdata->s_metadata_fe);
  258. mdata->s_metadata_fe = NULL;
  259. iput(mdata->s_mirror_fe);
  260. mdata->s_mirror_fe = NULL;
  261. iput(mdata->s_bitmap_fe);
  262. mdata->s_bitmap_fe = NULL;
  263. }
  264. }
  265. static void udf_sb_free_partitions(struct super_block *sb)
  266. {
  267. struct udf_sb_info *sbi = UDF_SB(sb);
  268. int i;
  269. for (i = 0; i < sbi->s_partitions; i++)
  270. udf_free_partition(&sbi->s_partmaps[i]);
  271. kfree(sbi->s_partmaps);
  272. sbi->s_partmaps = NULL;
  273. }
  274. static int udf_show_options(struct seq_file *seq, struct dentry *root)
  275. {
  276. struct super_block *sb = root->d_sb;
  277. struct udf_sb_info *sbi = UDF_SB(sb);
  278. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
  279. seq_puts(seq, ",nostrict");
  280. if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
  281. seq_printf(seq, ",bs=%lu", sb->s_blocksize);
  282. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  283. seq_puts(seq, ",unhide");
  284. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  285. seq_puts(seq, ",undelete");
  286. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
  287. seq_puts(seq, ",noadinicb");
  288. if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
  289. seq_puts(seq, ",shortad");
  290. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
  291. seq_puts(seq, ",uid=forget");
  292. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
  293. seq_puts(seq, ",uid=ignore");
  294. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
  295. seq_puts(seq, ",gid=forget");
  296. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
  297. seq_puts(seq, ",gid=ignore");
  298. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
  299. seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
  300. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
  301. seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
  302. if (sbi->s_umask != 0)
  303. seq_printf(seq, ",umask=%ho", sbi->s_umask);
  304. if (sbi->s_fmode != UDF_INVALID_MODE)
  305. seq_printf(seq, ",mode=%ho", sbi->s_fmode);
  306. if (sbi->s_dmode != UDF_INVALID_MODE)
  307. seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
  308. if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
  309. seq_printf(seq, ",session=%u", sbi->s_session);
  310. if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
  311. seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
  312. if (sbi->s_anchor != 0)
  313. seq_printf(seq, ",anchor=%u", sbi->s_anchor);
  314. /*
  315. * volume, partition, fileset and rootdir seem to be ignored
  316. * currently
  317. */
  318. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
  319. seq_puts(seq, ",utf8");
  320. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
  321. seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
  322. return 0;
  323. }
  324. /*
  325. * udf_parse_options
  326. *
  327. * PURPOSE
  328. * Parse mount options.
  329. *
  330. * DESCRIPTION
  331. * The following mount options are supported:
  332. *
  333. * gid= Set the default group.
  334. * umask= Set the default umask.
  335. * mode= Set the default file permissions.
  336. * dmode= Set the default directory permissions.
  337. * uid= Set the default user.
  338. * bs= Set the block size.
  339. * unhide Show otherwise hidden files.
  340. * undelete Show deleted files in lists.
  341. * adinicb Embed data in the inode (default)
  342. * noadinicb Don't embed data in the inode
  343. * shortad Use short ad's
  344. * longad Use long ad's (default)
  345. * nostrict Unset strict conformance
  346. * iocharset= Set the NLS character set
  347. *
  348. * The remaining are for debugging and disaster recovery:
  349. *
  350. * novrs Skip volume sequence recognition
  351. *
  352. * The following expect a offset from 0.
  353. *
  354. * session= Set the CDROM session (default= last session)
  355. * anchor= Override standard anchor location. (default= 256)
  356. * volume= Override the VolumeDesc location. (unused)
  357. * partition= Override the PartitionDesc location. (unused)
  358. * lastblock= Set the last block of the filesystem/
  359. *
  360. * The following expect a offset from the partition root.
  361. *
  362. * fileset= Override the fileset block location. (unused)
  363. * rootdir= Override the root directory location. (unused)
  364. * WARNING: overriding the rootdir to a non-directory may
  365. * yield highly unpredictable results.
  366. *
  367. * PRE-CONDITIONS
  368. * options Pointer to mount options string.
  369. * uopts Pointer to mount options variable.
  370. *
  371. * POST-CONDITIONS
  372. * <return> 1 Mount options parsed okay.
  373. * <return> 0 Error parsing mount options.
  374. *
  375. * HISTORY
  376. * July 1, 1997 - Andrew E. Mileski
  377. * Written, tested, and released.
  378. */
  379. enum {
  380. Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
  381. Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
  382. Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
  383. Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
  384. Opt_rootdir, Opt_utf8, Opt_iocharset,
  385. Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
  386. Opt_fmode, Opt_dmode
  387. };
  388. static const match_table_t tokens = {
  389. {Opt_novrs, "novrs"},
  390. {Opt_nostrict, "nostrict"},
  391. {Opt_bs, "bs=%u"},
  392. {Opt_unhide, "unhide"},
  393. {Opt_undelete, "undelete"},
  394. {Opt_noadinicb, "noadinicb"},
  395. {Opt_adinicb, "adinicb"},
  396. {Opt_shortad, "shortad"},
  397. {Opt_longad, "longad"},
  398. {Opt_uforget, "uid=forget"},
  399. {Opt_uignore, "uid=ignore"},
  400. {Opt_gforget, "gid=forget"},
  401. {Opt_gignore, "gid=ignore"},
  402. {Opt_gid, "gid=%u"},
  403. {Opt_uid, "uid=%u"},
  404. {Opt_umask, "umask=%o"},
  405. {Opt_session, "session=%u"},
  406. {Opt_lastblock, "lastblock=%u"},
  407. {Opt_anchor, "anchor=%u"},
  408. {Opt_volume, "volume=%u"},
  409. {Opt_partition, "partition=%u"},
  410. {Opt_fileset, "fileset=%u"},
  411. {Opt_rootdir, "rootdir=%u"},
  412. {Opt_utf8, "utf8"},
  413. {Opt_iocharset, "iocharset=%s"},
  414. {Opt_fmode, "mode=%o"},
  415. {Opt_dmode, "dmode=%o"},
  416. {Opt_err, NULL}
  417. };
  418. static int udf_parse_options(char *options, struct udf_options *uopt,
  419. bool remount)
  420. {
  421. char *p;
  422. int option;
  423. uopt->novrs = 0;
  424. uopt->partition = 0xFFFF;
  425. uopt->session = 0xFFFFFFFF;
  426. uopt->lastblock = 0;
  427. uopt->anchor = 0;
  428. uopt->volume = 0xFFFFFFFF;
  429. uopt->rootdir = 0xFFFFFFFF;
  430. uopt->fileset = 0xFFFFFFFF;
  431. uopt->nls_map = NULL;
  432. if (!options)
  433. return 1;
  434. while ((p = strsep(&options, ",")) != NULL) {
  435. substring_t args[MAX_OPT_ARGS];
  436. int token;
  437. if (!*p)
  438. continue;
  439. token = match_token(p, tokens, args);
  440. switch (token) {
  441. case Opt_novrs:
  442. uopt->novrs = 1;
  443. break;
  444. case Opt_bs:
  445. if (match_int(&args[0], &option))
  446. return 0;
  447. uopt->blocksize = option;
  448. uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
  449. break;
  450. case Opt_unhide:
  451. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  452. break;
  453. case Opt_undelete:
  454. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  455. break;
  456. case Opt_noadinicb:
  457. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  458. break;
  459. case Opt_adinicb:
  460. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  461. break;
  462. case Opt_shortad:
  463. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  464. break;
  465. case Opt_longad:
  466. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  467. break;
  468. case Opt_gid:
  469. if (match_int(args, &option))
  470. return 0;
  471. uopt->gid = make_kgid(current_user_ns(), option);
  472. if (!gid_valid(uopt->gid))
  473. return 0;
  474. uopt->flags |= (1 << UDF_FLAG_GID_SET);
  475. break;
  476. case Opt_uid:
  477. if (match_int(args, &option))
  478. return 0;
  479. uopt->uid = make_kuid(current_user_ns(), option);
  480. if (!uid_valid(uopt->uid))
  481. return 0;
  482. uopt->flags |= (1 << UDF_FLAG_UID_SET);
  483. break;
  484. case Opt_umask:
  485. if (match_octal(args, &option))
  486. return 0;
  487. uopt->umask = option;
  488. break;
  489. case Opt_nostrict:
  490. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  491. break;
  492. case Opt_session:
  493. if (match_int(args, &option))
  494. return 0;
  495. uopt->session = option;
  496. if (!remount)
  497. uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
  498. break;
  499. case Opt_lastblock:
  500. if (match_int(args, &option))
  501. return 0;
  502. uopt->lastblock = option;
  503. if (!remount)
  504. uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
  505. break;
  506. case Opt_anchor:
  507. if (match_int(args, &option))
  508. return 0;
  509. uopt->anchor = option;
  510. break;
  511. case Opt_volume:
  512. if (match_int(args, &option))
  513. return 0;
  514. uopt->volume = option;
  515. break;
  516. case Opt_partition:
  517. if (match_int(args, &option))
  518. return 0;
  519. uopt->partition = option;
  520. break;
  521. case Opt_fileset:
  522. if (match_int(args, &option))
  523. return 0;
  524. uopt->fileset = option;
  525. break;
  526. case Opt_rootdir:
  527. if (match_int(args, &option))
  528. return 0;
  529. uopt->rootdir = option;
  530. break;
  531. case Opt_utf8:
  532. uopt->flags |= (1 << UDF_FLAG_UTF8);
  533. break;
  534. #ifdef CONFIG_UDF_NLS
  535. case Opt_iocharset:
  536. uopt->nls_map = load_nls(args[0].from);
  537. uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
  538. break;
  539. #endif
  540. case Opt_uignore:
  541. uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
  542. break;
  543. case Opt_uforget:
  544. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  545. break;
  546. case Opt_gignore:
  547. uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
  548. break;
  549. case Opt_gforget:
  550. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  551. break;
  552. case Opt_fmode:
  553. if (match_octal(args, &option))
  554. return 0;
  555. uopt->fmode = option & 0777;
  556. break;
  557. case Opt_dmode:
  558. if (match_octal(args, &option))
  559. return 0;
  560. uopt->dmode = option & 0777;
  561. break;
  562. default:
  563. pr_err("bad mount option \"%s\" or missing value\n", p);
  564. return 0;
  565. }
  566. }
  567. return 1;
  568. }
  569. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  570. {
  571. struct udf_options uopt;
  572. struct udf_sb_info *sbi = UDF_SB(sb);
  573. int error = 0;
  574. uopt.flags = sbi->s_flags;
  575. uopt.uid = sbi->s_uid;
  576. uopt.gid = sbi->s_gid;
  577. uopt.umask = sbi->s_umask;
  578. uopt.fmode = sbi->s_fmode;
  579. uopt.dmode = sbi->s_dmode;
  580. if (!udf_parse_options(options, &uopt, true))
  581. return -EINVAL;
  582. write_lock(&sbi->s_cred_lock);
  583. sbi->s_flags = uopt.flags;
  584. sbi->s_uid = uopt.uid;
  585. sbi->s_gid = uopt.gid;
  586. sbi->s_umask = uopt.umask;
  587. sbi->s_fmode = uopt.fmode;
  588. sbi->s_dmode = uopt.dmode;
  589. write_unlock(&sbi->s_cred_lock);
  590. if (sbi->s_lvid_bh) {
  591. int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
  592. if (write_rev > UDF_MAX_WRITE_VERSION)
  593. *flags |= MS_RDONLY;
  594. }
  595. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  596. goto out_unlock;
  597. if (*flags & MS_RDONLY)
  598. udf_close_lvid(sb);
  599. else
  600. udf_open_lvid(sb);
  601. out_unlock:
  602. return error;
  603. }
  604. /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
  605. /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
  606. static loff_t udf_check_vsd(struct super_block *sb)
  607. {
  608. struct volStructDesc *vsd = NULL;
  609. loff_t sector = 32768;
  610. int sectorsize;
  611. struct buffer_head *bh = NULL;
  612. int nsr02 = 0;
  613. int nsr03 = 0;
  614. struct udf_sb_info *sbi;
  615. sbi = UDF_SB(sb);
  616. if (sb->s_blocksize < sizeof(struct volStructDesc))
  617. sectorsize = sizeof(struct volStructDesc);
  618. else
  619. sectorsize = sb->s_blocksize;
  620. sector += (sbi->s_session << sb->s_blocksize_bits);
  621. udf_debug("Starting at sector %u (%ld byte sectors)\n",
  622. (unsigned int)(sector >> sb->s_blocksize_bits),
  623. sb->s_blocksize);
  624. /* Process the sequence (if applicable) */
  625. for (; !nsr02 && !nsr03; sector += sectorsize) {
  626. /* Read a block */
  627. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  628. if (!bh)
  629. break;
  630. /* Look for ISO descriptors */
  631. vsd = (struct volStructDesc *)(bh->b_data +
  632. (sector & (sb->s_blocksize - 1)));
  633. if (vsd->stdIdent[0] == 0) {
  634. brelse(bh);
  635. break;
  636. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
  637. VSD_STD_ID_LEN)) {
  638. switch (vsd->structType) {
  639. case 0:
  640. udf_debug("ISO9660 Boot Record found\n");
  641. break;
  642. case 1:
  643. udf_debug("ISO9660 Primary Volume Descriptor found\n");
  644. break;
  645. case 2:
  646. udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
  647. break;
  648. case 3:
  649. udf_debug("ISO9660 Volume Partition Descriptor found\n");
  650. break;
  651. case 255:
  652. udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
  653. break;
  654. default:
  655. udf_debug("ISO9660 VRS (%u) found\n",
  656. vsd->structType);
  657. break;
  658. }
  659. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  660. VSD_STD_ID_LEN))
  661. ; /* nothing */
  662. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  663. VSD_STD_ID_LEN)) {
  664. brelse(bh);
  665. break;
  666. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  667. VSD_STD_ID_LEN))
  668. nsr02 = sector;
  669. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  670. VSD_STD_ID_LEN))
  671. nsr03 = sector;
  672. brelse(bh);
  673. }
  674. if (nsr03)
  675. return nsr03;
  676. else if (nsr02)
  677. return nsr02;
  678. else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
  679. return -1;
  680. else
  681. return 0;
  682. }
  683. static int udf_find_fileset(struct super_block *sb,
  684. struct kernel_lb_addr *fileset,
  685. struct kernel_lb_addr *root)
  686. {
  687. struct buffer_head *bh = NULL;
  688. long lastblock;
  689. uint16_t ident;
  690. struct udf_sb_info *sbi;
  691. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  692. fileset->partitionReferenceNum != 0xFFFF) {
  693. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  694. if (!bh) {
  695. return 1;
  696. } else if (ident != TAG_IDENT_FSD) {
  697. brelse(bh);
  698. return 1;
  699. }
  700. }
  701. sbi = UDF_SB(sb);
  702. if (!bh) {
  703. /* Search backwards through the partitions */
  704. struct kernel_lb_addr newfileset;
  705. /* --> cvg: FIXME - is it reasonable? */
  706. return 1;
  707. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  708. (newfileset.partitionReferenceNum != 0xFFFF &&
  709. fileset->logicalBlockNum == 0xFFFFFFFF &&
  710. fileset->partitionReferenceNum == 0xFFFF);
  711. newfileset.partitionReferenceNum--) {
  712. lastblock = sbi->s_partmaps
  713. [newfileset.partitionReferenceNum]
  714. .s_partition_len;
  715. newfileset.logicalBlockNum = 0;
  716. do {
  717. bh = udf_read_ptagged(sb, &newfileset, 0,
  718. &ident);
  719. if (!bh) {
  720. newfileset.logicalBlockNum++;
  721. continue;
  722. }
  723. switch (ident) {
  724. case TAG_IDENT_SBD:
  725. {
  726. struct spaceBitmapDesc *sp;
  727. sp = (struct spaceBitmapDesc *)
  728. bh->b_data;
  729. newfileset.logicalBlockNum += 1 +
  730. ((le32_to_cpu(sp->numOfBytes) +
  731. sizeof(struct spaceBitmapDesc)
  732. - 1) >> sb->s_blocksize_bits);
  733. brelse(bh);
  734. break;
  735. }
  736. case TAG_IDENT_FSD:
  737. *fileset = newfileset;
  738. break;
  739. default:
  740. newfileset.logicalBlockNum++;
  741. brelse(bh);
  742. bh = NULL;
  743. break;
  744. }
  745. } while (newfileset.logicalBlockNum < lastblock &&
  746. fileset->logicalBlockNum == 0xFFFFFFFF &&
  747. fileset->partitionReferenceNum == 0xFFFF);
  748. }
  749. }
  750. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  751. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  752. udf_debug("Fileset at block=%d, partition=%d\n",
  753. fileset->logicalBlockNum,
  754. fileset->partitionReferenceNum);
  755. sbi->s_partition = fileset->partitionReferenceNum;
  756. udf_load_fileset(sb, bh, root);
  757. brelse(bh);
  758. return 0;
  759. }
  760. return 1;
  761. }
  762. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  763. {
  764. struct primaryVolDesc *pvoldesc;
  765. struct ustr *instr, *outstr;
  766. struct buffer_head *bh;
  767. uint16_t ident;
  768. int ret = 1;
  769. instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  770. if (!instr)
  771. return 1;
  772. outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  773. if (!outstr)
  774. goto out1;
  775. bh = udf_read_tagged(sb, block, block, &ident);
  776. if (!bh)
  777. goto out2;
  778. BUG_ON(ident != TAG_IDENT_PVD);
  779. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  780. if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  781. pvoldesc->recordingDateAndTime)) {
  782. #ifdef UDFFS_DEBUG
  783. struct timestamp *ts = &pvoldesc->recordingDateAndTime;
  784. udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
  785. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  786. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  787. #endif
  788. }
  789. if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
  790. if (udf_CS0toUTF8(outstr, instr)) {
  791. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  792. outstr->u_len > 31 ? 31 : outstr->u_len);
  793. udf_debug("volIdent[] = '%s'\n",
  794. UDF_SB(sb)->s_volume_ident);
  795. }
  796. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  797. if (udf_CS0toUTF8(outstr, instr))
  798. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  799. brelse(bh);
  800. ret = 0;
  801. out2:
  802. kfree(outstr);
  803. out1:
  804. kfree(instr);
  805. return ret;
  806. }
  807. struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
  808. u32 meta_file_loc, u32 partition_num)
  809. {
  810. struct kernel_lb_addr addr;
  811. struct inode *metadata_fe;
  812. addr.logicalBlockNum = meta_file_loc;
  813. addr.partitionReferenceNum = partition_num;
  814. metadata_fe = udf_iget(sb, &addr);
  815. if (metadata_fe == NULL)
  816. udf_warn(sb, "metadata inode efe not found\n");
  817. else if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
  818. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  819. iput(metadata_fe);
  820. metadata_fe = NULL;
  821. }
  822. return metadata_fe;
  823. }
  824. static int udf_load_metadata_files(struct super_block *sb, int partition)
  825. {
  826. struct udf_sb_info *sbi = UDF_SB(sb);
  827. struct udf_part_map *map;
  828. struct udf_meta_data *mdata;
  829. struct kernel_lb_addr addr;
  830. map = &sbi->s_partmaps[partition];
  831. mdata = &map->s_type_specific.s_metadata;
  832. /* metadata address */
  833. udf_debug("Metadata file location: block = %d part = %d\n",
  834. mdata->s_meta_file_loc, map->s_partition_num);
  835. mdata->s_metadata_fe = udf_find_metadata_inode_efe(sb,
  836. mdata->s_meta_file_loc, map->s_partition_num);
  837. if (mdata->s_metadata_fe == NULL) {
  838. /* mirror file entry */
  839. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  840. mdata->s_mirror_file_loc, map->s_partition_num);
  841. mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
  842. mdata->s_mirror_file_loc, map->s_partition_num);
  843. if (mdata->s_mirror_fe == NULL) {
  844. udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
  845. goto error_exit;
  846. }
  847. }
  848. /*
  849. * bitmap file entry
  850. * Note:
  851. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  852. */
  853. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  854. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  855. addr.partitionReferenceNum = map->s_partition_num;
  856. udf_debug("Bitmap file location: block = %d part = %d\n",
  857. addr.logicalBlockNum, addr.partitionReferenceNum);
  858. mdata->s_bitmap_fe = udf_iget(sb, &addr);
  859. if (mdata->s_bitmap_fe == NULL) {
  860. if (sb->s_flags & MS_RDONLY)
  861. udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
  862. else {
  863. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  864. goto error_exit;
  865. }
  866. }
  867. }
  868. udf_debug("udf_load_metadata_files Ok\n");
  869. return 0;
  870. error_exit:
  871. return 1;
  872. }
  873. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  874. struct kernel_lb_addr *root)
  875. {
  876. struct fileSetDesc *fset;
  877. fset = (struct fileSetDesc *)bh->b_data;
  878. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  879. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  880. udf_debug("Rootdir at block=%d, partition=%d\n",
  881. root->logicalBlockNum, root->partitionReferenceNum);
  882. }
  883. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  884. {
  885. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  886. return DIV_ROUND_UP(map->s_partition_len +
  887. (sizeof(struct spaceBitmapDesc) << 3),
  888. sb->s_blocksize * 8);
  889. }
  890. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  891. {
  892. struct udf_bitmap *bitmap;
  893. int nr_groups;
  894. int size;
  895. nr_groups = udf_compute_nr_groups(sb, index);
  896. size = sizeof(struct udf_bitmap) +
  897. (sizeof(struct buffer_head *) * nr_groups);
  898. if (size <= PAGE_SIZE)
  899. bitmap = kzalloc(size, GFP_KERNEL);
  900. else
  901. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  902. if (bitmap == NULL)
  903. return NULL;
  904. bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
  905. bitmap->s_nr_groups = nr_groups;
  906. return bitmap;
  907. }
  908. static int udf_fill_partdesc_info(struct super_block *sb,
  909. struct partitionDesc *p, int p_index)
  910. {
  911. struct udf_part_map *map;
  912. struct udf_sb_info *sbi = UDF_SB(sb);
  913. struct partitionHeaderDesc *phd;
  914. map = &sbi->s_partmaps[p_index];
  915. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  916. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  917. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  918. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  919. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  920. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  921. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  922. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  923. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  924. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  925. udf_debug("Partition (%d type %x) starts at physical %d, block length %d\n",
  926. p_index, map->s_partition_type,
  927. map->s_partition_root, map->s_partition_len);
  928. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  929. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  930. return 0;
  931. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  932. if (phd->unallocSpaceTable.extLength) {
  933. struct kernel_lb_addr loc = {
  934. .logicalBlockNum = le32_to_cpu(
  935. phd->unallocSpaceTable.extPosition),
  936. .partitionReferenceNum = p_index,
  937. };
  938. map->s_uspace.s_table = udf_iget(sb, &loc);
  939. if (!map->s_uspace.s_table) {
  940. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  941. p_index);
  942. return 1;
  943. }
  944. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  945. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  946. p_index, map->s_uspace.s_table->i_ino);
  947. }
  948. if (phd->unallocSpaceBitmap.extLength) {
  949. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  950. if (!bitmap)
  951. return 1;
  952. map->s_uspace.s_bitmap = bitmap;
  953. bitmap->s_extLength = le32_to_cpu(
  954. phd->unallocSpaceBitmap.extLength);
  955. bitmap->s_extPosition = le32_to_cpu(
  956. phd->unallocSpaceBitmap.extPosition);
  957. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  958. udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
  959. p_index, bitmap->s_extPosition);
  960. }
  961. if (phd->partitionIntegrityTable.extLength)
  962. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  963. if (phd->freedSpaceTable.extLength) {
  964. struct kernel_lb_addr loc = {
  965. .logicalBlockNum = le32_to_cpu(
  966. phd->freedSpaceTable.extPosition),
  967. .partitionReferenceNum = p_index,
  968. };
  969. map->s_fspace.s_table = udf_iget(sb, &loc);
  970. if (!map->s_fspace.s_table) {
  971. udf_debug("cannot load freedSpaceTable (part %d)\n",
  972. p_index);
  973. return 1;
  974. }
  975. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  976. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  977. p_index, map->s_fspace.s_table->i_ino);
  978. }
  979. if (phd->freedSpaceBitmap.extLength) {
  980. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  981. if (!bitmap)
  982. return 1;
  983. map->s_fspace.s_bitmap = bitmap;
  984. bitmap->s_extLength = le32_to_cpu(
  985. phd->freedSpaceBitmap.extLength);
  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. }
  1666. static void udf_close_lvid(struct super_block *sb)
  1667. {
  1668. struct udf_sb_info *sbi = UDF_SB(sb);
  1669. struct buffer_head *bh = sbi->s_lvid_bh;
  1670. struct logicalVolIntegrityDesc *lvid;
  1671. struct logicalVolIntegrityDescImpUse *lvidiu;
  1672. if (!bh)
  1673. return;
  1674. mutex_lock(&sbi->s_alloc_mutex);
  1675. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1676. lvidiu = udf_sb_lvidiu(sbi);
  1677. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1678. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1679. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1680. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1681. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1682. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1683. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1684. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1685. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1686. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1687. lvid->descTag.descCRC = cpu_to_le16(
  1688. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1689. le16_to_cpu(lvid->descTag.descCRCLength)));
  1690. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1691. /*
  1692. * We set buffer uptodate unconditionally here to avoid spurious
  1693. * warnings from mark_buffer_dirty() when previous EIO has marked
  1694. * the buffer as !uptodate
  1695. */
  1696. set_buffer_uptodate(bh);
  1697. mark_buffer_dirty(bh);
  1698. sbi->s_lvid_dirty = 0;
  1699. mutex_unlock(&sbi->s_alloc_mutex);
  1700. }
  1701. u64 lvid_get_unique_id(struct super_block *sb)
  1702. {
  1703. struct buffer_head *bh;
  1704. struct udf_sb_info *sbi = UDF_SB(sb);
  1705. struct logicalVolIntegrityDesc *lvid;
  1706. struct logicalVolHeaderDesc *lvhd;
  1707. u64 uniqueID;
  1708. u64 ret;
  1709. bh = sbi->s_lvid_bh;
  1710. if (!bh)
  1711. return 0;
  1712. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1713. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1714. mutex_lock(&sbi->s_alloc_mutex);
  1715. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1716. if (!(++uniqueID & 0xFFFFFFFF))
  1717. uniqueID += 16;
  1718. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1719. mutex_unlock(&sbi->s_alloc_mutex);
  1720. mark_buffer_dirty(bh);
  1721. return ret;
  1722. }
  1723. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1724. {
  1725. int ret;
  1726. struct inode *inode = NULL;
  1727. struct udf_options uopt;
  1728. struct kernel_lb_addr rootdir, fileset;
  1729. struct udf_sb_info *sbi;
  1730. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1731. uopt.uid = INVALID_UID;
  1732. uopt.gid = INVALID_GID;
  1733. uopt.umask = 0;
  1734. uopt.fmode = UDF_INVALID_MODE;
  1735. uopt.dmode = UDF_INVALID_MODE;
  1736. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1737. if (!sbi)
  1738. return -ENOMEM;
  1739. sb->s_fs_info = sbi;
  1740. mutex_init(&sbi->s_alloc_mutex);
  1741. if (!udf_parse_options((char *)options, &uopt, false))
  1742. goto error_out;
  1743. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1744. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1745. udf_err(sb, "utf8 cannot be combined with iocharset\n");
  1746. goto error_out;
  1747. }
  1748. #ifdef CONFIG_UDF_NLS
  1749. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1750. uopt.nls_map = load_nls_default();
  1751. if (!uopt.nls_map)
  1752. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1753. else
  1754. udf_debug("Using default NLS map\n");
  1755. }
  1756. #endif
  1757. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1758. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1759. fileset.logicalBlockNum = 0xFFFFFFFF;
  1760. fileset.partitionReferenceNum = 0xFFFF;
  1761. sbi->s_flags = uopt.flags;
  1762. sbi->s_uid = uopt.uid;
  1763. sbi->s_gid = uopt.gid;
  1764. sbi->s_umask = uopt.umask;
  1765. sbi->s_fmode = uopt.fmode;
  1766. sbi->s_dmode = uopt.dmode;
  1767. sbi->s_nls_map = uopt.nls_map;
  1768. rwlock_init(&sbi->s_cred_lock);
  1769. if (uopt.session == 0xFFFFFFFF)
  1770. sbi->s_session = udf_get_last_session(sb);
  1771. else
  1772. sbi->s_session = uopt.session;
  1773. udf_debug("Multi-session=%d\n", sbi->s_session);
  1774. /* Fill in the rest of the superblock */
  1775. sb->s_op = &udf_sb_ops;
  1776. sb->s_export_op = &udf_export_ops;
  1777. sb->s_magic = UDF_SUPER_MAGIC;
  1778. sb->s_time_gran = 1000;
  1779. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1780. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1781. } else {
  1782. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1783. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1784. if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1785. if (!silent)
  1786. pr_notice("Rescanning with blocksize %d\n",
  1787. UDF_DEFAULT_BLOCKSIZE);
  1788. brelse(sbi->s_lvid_bh);
  1789. sbi->s_lvid_bh = NULL;
  1790. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1791. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1792. }
  1793. }
  1794. if (!ret) {
  1795. udf_warn(sb, "No partition found (1)\n");
  1796. goto error_out;
  1797. }
  1798. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1799. if (sbi->s_lvid_bh) {
  1800. struct logicalVolIntegrityDescImpUse *lvidiu =
  1801. udf_sb_lvidiu(sbi);
  1802. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1803. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1804. /* uint16_t maxUDFWriteRev =
  1805. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1806. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1807. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1808. le16_to_cpu(lvidiu->minUDFReadRev),
  1809. UDF_MAX_READ_VERSION);
  1810. goto error_out;
  1811. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1812. sb->s_flags |= MS_RDONLY;
  1813. sbi->s_udfrev = minUDFWriteRev;
  1814. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1815. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1816. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1817. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1818. }
  1819. if (!sbi->s_partitions) {
  1820. udf_warn(sb, "No partition found (2)\n");
  1821. goto error_out;
  1822. }
  1823. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1824. UDF_PART_FLAG_READ_ONLY) {
  1825. pr_notice("Partition marked readonly; forcing readonly mount\n");
  1826. sb->s_flags |= MS_RDONLY;
  1827. }
  1828. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1829. udf_warn(sb, "No fileset found\n");
  1830. goto error_out;
  1831. }
  1832. if (!silent) {
  1833. struct timestamp ts;
  1834. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1835. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1836. sbi->s_volume_ident,
  1837. le16_to_cpu(ts.year), ts.month, ts.day,
  1838. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1839. }
  1840. if (!(sb->s_flags & MS_RDONLY))
  1841. udf_open_lvid(sb);
  1842. /* Assign the root inode */
  1843. /* assign inodes by physical block number */
  1844. /* perhaps it's not extensible enough, but for now ... */
  1845. inode = udf_iget(sb, &rootdir);
  1846. if (!inode) {
  1847. udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
  1848. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1849. goto error_out;
  1850. }
  1851. /* Allocate a dentry for the root inode */
  1852. sb->s_root = d_make_root(inode);
  1853. if (!sb->s_root) {
  1854. udf_err(sb, "Couldn't allocate root dentry\n");
  1855. goto error_out;
  1856. }
  1857. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1858. sb->s_max_links = UDF_MAX_LINKS;
  1859. return 0;
  1860. error_out:
  1861. if (sbi->s_vat_inode)
  1862. iput(sbi->s_vat_inode);
  1863. #ifdef CONFIG_UDF_NLS
  1864. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1865. unload_nls(sbi->s_nls_map);
  1866. #endif
  1867. if (!(sb->s_flags & MS_RDONLY))
  1868. udf_close_lvid(sb);
  1869. brelse(sbi->s_lvid_bh);
  1870. udf_sb_free_partitions(sb);
  1871. kfree(sbi);
  1872. sb->s_fs_info = NULL;
  1873. return -EINVAL;
  1874. }
  1875. void _udf_err(struct super_block *sb, const char *function,
  1876. const char *fmt, ...)
  1877. {
  1878. struct va_format vaf;
  1879. va_list args;
  1880. va_start(args, fmt);
  1881. vaf.fmt = fmt;
  1882. vaf.va = &args;
  1883. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  1884. va_end(args);
  1885. }
  1886. void _udf_warn(struct super_block *sb, const char *function,
  1887. const char *fmt, ...)
  1888. {
  1889. struct va_format vaf;
  1890. va_list args;
  1891. va_start(args, fmt);
  1892. vaf.fmt = fmt;
  1893. vaf.va = &args;
  1894. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  1895. va_end(args);
  1896. }
  1897. static void udf_put_super(struct super_block *sb)
  1898. {
  1899. struct udf_sb_info *sbi;
  1900. sbi = UDF_SB(sb);
  1901. if (sbi->s_vat_inode)
  1902. iput(sbi->s_vat_inode);
  1903. #ifdef CONFIG_UDF_NLS
  1904. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1905. unload_nls(sbi->s_nls_map);
  1906. #endif
  1907. if (!(sb->s_flags & MS_RDONLY))
  1908. udf_close_lvid(sb);
  1909. brelse(sbi->s_lvid_bh);
  1910. udf_sb_free_partitions(sb);
  1911. kfree(sb->s_fs_info);
  1912. sb->s_fs_info = NULL;
  1913. }
  1914. static int udf_sync_fs(struct super_block *sb, int wait)
  1915. {
  1916. struct udf_sb_info *sbi = UDF_SB(sb);
  1917. mutex_lock(&sbi->s_alloc_mutex);
  1918. if (sbi->s_lvid_dirty) {
  1919. /*
  1920. * Blockdevice will be synced later so we don't have to submit
  1921. * the buffer for IO
  1922. */
  1923. mark_buffer_dirty(sbi->s_lvid_bh);
  1924. sbi->s_lvid_dirty = 0;
  1925. }
  1926. mutex_unlock(&sbi->s_alloc_mutex);
  1927. return 0;
  1928. }
  1929. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1930. {
  1931. struct super_block *sb = dentry->d_sb;
  1932. struct udf_sb_info *sbi = UDF_SB(sb);
  1933. struct logicalVolIntegrityDescImpUse *lvidiu;
  1934. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1935. if (sbi->s_lvid_bh != NULL)
  1936. lvidiu = udf_sb_lvidiu(sbi);
  1937. else
  1938. lvidiu = NULL;
  1939. buf->f_type = UDF_SUPER_MAGIC;
  1940. buf->f_bsize = sb->s_blocksize;
  1941. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1942. buf->f_bfree = udf_count_free(sb);
  1943. buf->f_bavail = buf->f_bfree;
  1944. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1945. le32_to_cpu(lvidiu->numDirs)) : 0)
  1946. + buf->f_bfree;
  1947. buf->f_ffree = buf->f_bfree;
  1948. buf->f_namelen = UDF_NAME_LEN - 2;
  1949. buf->f_fsid.val[0] = (u32)id;
  1950. buf->f_fsid.val[1] = (u32)(id >> 32);
  1951. return 0;
  1952. }
  1953. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1954. struct udf_bitmap *bitmap)
  1955. {
  1956. struct buffer_head *bh = NULL;
  1957. unsigned int accum = 0;
  1958. int index;
  1959. int block = 0, newblock;
  1960. struct kernel_lb_addr loc;
  1961. uint32_t bytes;
  1962. uint8_t *ptr;
  1963. uint16_t ident;
  1964. struct spaceBitmapDesc *bm;
  1965. loc.logicalBlockNum = bitmap->s_extPosition;
  1966. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1967. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  1968. if (!bh) {
  1969. udf_err(sb, "udf_count_free failed\n");
  1970. goto out;
  1971. } else if (ident != TAG_IDENT_SBD) {
  1972. brelse(bh);
  1973. udf_err(sb, "udf_count_free failed\n");
  1974. goto out;
  1975. }
  1976. bm = (struct spaceBitmapDesc *)bh->b_data;
  1977. bytes = le32_to_cpu(bm->numOfBytes);
  1978. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1979. ptr = (uint8_t *)bh->b_data;
  1980. while (bytes > 0) {
  1981. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1982. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1983. cur_bytes * 8);
  1984. bytes -= cur_bytes;
  1985. if (bytes) {
  1986. brelse(bh);
  1987. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  1988. bh = udf_tread(sb, newblock);
  1989. if (!bh) {
  1990. udf_debug("read failed\n");
  1991. goto out;
  1992. }
  1993. index = 0;
  1994. ptr = (uint8_t *)bh->b_data;
  1995. }
  1996. }
  1997. brelse(bh);
  1998. out:
  1999. return accum;
  2000. }
  2001. static unsigned int udf_count_free_table(struct super_block *sb,
  2002. struct inode *table)
  2003. {
  2004. unsigned int accum = 0;
  2005. uint32_t elen;
  2006. struct kernel_lb_addr eloc;
  2007. int8_t etype;
  2008. struct extent_position epos;
  2009. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2010. epos.block = UDF_I(table)->i_location;
  2011. epos.offset = sizeof(struct unallocSpaceEntry);
  2012. epos.bh = NULL;
  2013. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2014. accum += (elen >> table->i_sb->s_blocksize_bits);
  2015. brelse(epos.bh);
  2016. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2017. return accum;
  2018. }
  2019. static unsigned int udf_count_free(struct super_block *sb)
  2020. {
  2021. unsigned int accum = 0;
  2022. struct udf_sb_info *sbi;
  2023. struct udf_part_map *map;
  2024. sbi = UDF_SB(sb);
  2025. if (sbi->s_lvid_bh) {
  2026. struct logicalVolIntegrityDesc *lvid =
  2027. (struct logicalVolIntegrityDesc *)
  2028. sbi->s_lvid_bh->b_data;
  2029. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2030. accum = le32_to_cpu(
  2031. lvid->freeSpaceTable[sbi->s_partition]);
  2032. if (accum == 0xFFFFFFFF)
  2033. accum = 0;
  2034. }
  2035. }
  2036. if (accum)
  2037. return accum;
  2038. map = &sbi->s_partmaps[sbi->s_partition];
  2039. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2040. accum += udf_count_free_bitmap(sb,
  2041. map->s_uspace.s_bitmap);
  2042. }
  2043. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2044. accum += udf_count_free_bitmap(sb,
  2045. map->s_fspace.s_bitmap);
  2046. }
  2047. if (accum)
  2048. return accum;
  2049. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2050. accum += udf_count_free_table(sb,
  2051. map->s_uspace.s_table);
  2052. }
  2053. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2054. accum += udf_count_free_table(sb,
  2055. map->s_fspace.s_table);
  2056. }
  2057. return accum;
  2058. }