super.c 62 KB

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