super.c 61 KB

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