super.c 62 KB

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