super.c 61 KB

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