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