super.c 61 KB

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