super.c 61 KB

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