super.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  1. /*
  2. * super.c
  3. *
  4. * PURPOSE
  5. * Super block routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * DESCRIPTION
  8. * OSTA-UDF(tm) = Optical Storage Technology Association
  9. * Universal Disk Format.
  10. *
  11. * This code is based on version 2.00 of the UDF specification,
  12. * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
  13. * http://www.osta.org/
  14. * http://www.ecma.ch/
  15. * http://www.iso.org/
  16. *
  17. * COPYRIGHT
  18. * This file is distributed under the terms of the GNU General Public
  19. * License (GPL). Copies of the GPL can be obtained from:
  20. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  21. * Each contributing author retains all rights to their own work.
  22. *
  23. * (C) 1998 Dave Boynton
  24. * (C) 1998-2004 Ben Fennema
  25. * (C) 2000 Stelias Computing Inc
  26. *
  27. * HISTORY
  28. *
  29. * 09/24/98 dgb changed to allow compiling outside of kernel, and
  30. * added some debugging.
  31. * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
  32. * 10/16/98 attempting some multi-session support
  33. * 10/17/98 added freespace count for "df"
  34. * 11/11/98 gr added novrs option
  35. * 11/26/98 dgb added fileset,anchor mount options
  36. * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
  37. * vol descs. rewrote option handling based on isofs
  38. * 12/20/98 find the free space bitmap (if it exists)
  39. */
  40. #include "udfdecl.h"
  41. #include <linux/blkdev.h>
  42. #include <linux/slab.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/parser.h>
  46. #include <linux/stat.h>
  47. #include <linux/cdrom.h>
  48. #include <linux/nls.h>
  49. #include <linux/buffer_head.h>
  50. #include <linux/vfs.h>
  51. #include <linux/vmalloc.h>
  52. #include <linux/errno.h>
  53. #include <linux/mount.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/bitmap.h>
  56. #include <linux/crc-itu-t.h>
  57. #include <asm/byteorder.h>
  58. #include "udf_sb.h"
  59. #include "udf_i.h"
  60. #include <linux/init.h>
  61. #include <asm/uaccess.h>
  62. #define VDS_POS_PRIMARY_VOL_DESC 0
  63. #define VDS_POS_UNALLOC_SPACE_DESC 1
  64. #define VDS_POS_LOGICAL_VOL_DESC 2
  65. #define VDS_POS_PARTITION_DESC 3
  66. #define VDS_POS_IMP_USE_VOL_DESC 4
  67. #define VDS_POS_VOL_DESC_PTR 5
  68. #define VDS_POS_TERMINATING_DESC 6
  69. #define VDS_POS_LENGTH 7
  70. #define UDF_DEFAULT_BLOCKSIZE 2048
  71. static char error_buf[1024];
  72. /* These are the "meat" - everything else is stuffing */
  73. static int udf_fill_super(struct super_block *, void *, int);
  74. static void udf_put_super(struct super_block *);
  75. static int udf_sync_fs(struct super_block *, int);
  76. static int udf_remount_fs(struct super_block *, int *, char *);
  77. static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
  78. static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
  79. struct kernel_lb_addr *);
  80. static void udf_load_fileset(struct super_block *, struct buffer_head *,
  81. struct kernel_lb_addr *);
  82. static void udf_open_lvid(struct super_block *);
  83. static void udf_close_lvid(struct super_block *);
  84. static unsigned int udf_count_free(struct super_block *);
  85. static int udf_statfs(struct dentry *, struct kstatfs *);
  86. static int udf_show_options(struct seq_file *, struct vfsmount *);
  87. static void udf_error(struct super_block *sb, const char *function,
  88. const char *fmt, ...);
  89. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
  90. {
  91. struct logicalVolIntegrityDesc *lvid =
  92. (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
  93. __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
  94. __u32 offset = number_of_partitions * 2 *
  95. sizeof(uint32_t)/sizeof(uint8_t);
  96. return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
  97. }
  98. /* UDF filesystem type */
  99. static struct dentry *udf_mount(struct file_system_type *fs_type,
  100. int flags, const char *dev_name, void *data)
  101. {
  102. return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
  103. }
  104. static struct file_system_type udf_fstype = {
  105. .owner = THIS_MODULE,
  106. .name = "udf",
  107. .mount = udf_mount,
  108. .kill_sb = kill_block_super,
  109. .fs_flags = FS_REQUIRES_DEV,
  110. };
  111. static struct kmem_cache *udf_inode_cachep;
  112. static struct inode *udf_alloc_inode(struct super_block *sb)
  113. {
  114. struct udf_inode_info *ei;
  115. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  116. if (!ei)
  117. return NULL;
  118. ei->i_unique = 0;
  119. ei->i_lenExtents = 0;
  120. ei->i_next_alloc_block = 0;
  121. ei->i_next_alloc_goal = 0;
  122. ei->i_strat4096 = 0;
  123. init_rwsem(&ei->i_data_sem);
  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. write_lock(&sbi->s_cred_lock);
  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. write_unlock(&sbi->s_cred_lock);
  537. if (sbi->s_lvid_bh) {
  538. int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
  539. if (write_rev > UDF_MAX_WRITE_VERSION)
  540. *flags |= MS_RDONLY;
  541. }
  542. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  543. goto out_unlock;
  544. if (*flags & MS_RDONLY)
  545. udf_close_lvid(sb);
  546. else
  547. udf_open_lvid(sb);
  548. out_unlock:
  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 = kzalloc(size, GFP_KERNEL);
  864. else
  865. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  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. bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
  873. bitmap->s_nr_groups = nr_groups;
  874. return bitmap;
  875. }
  876. static int udf_fill_partdesc_info(struct super_block *sb,
  877. struct partitionDesc *p, int p_index)
  878. {
  879. struct udf_part_map *map;
  880. struct udf_sb_info *sbi = UDF_SB(sb);
  881. struct partitionHeaderDesc *phd;
  882. map = &sbi->s_partmaps[p_index];
  883. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  884. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  885. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  886. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  887. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  888. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  889. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  890. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  891. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  892. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  893. udf_debug("Partition (%d type %x) starts at physical %d, "
  894. "block length %d\n", p_index,
  895. map->s_partition_type, map->s_partition_root,
  896. map->s_partition_len);
  897. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  898. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  899. return 0;
  900. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  901. if (phd->unallocSpaceTable.extLength) {
  902. struct kernel_lb_addr loc = {
  903. .logicalBlockNum = le32_to_cpu(
  904. phd->unallocSpaceTable.extPosition),
  905. .partitionReferenceNum = p_index,
  906. };
  907. map->s_uspace.s_table = udf_iget(sb, &loc);
  908. if (!map->s_uspace.s_table) {
  909. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  910. p_index);
  911. return 1;
  912. }
  913. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  914. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  915. p_index, map->s_uspace.s_table->i_ino);
  916. }
  917. if (phd->unallocSpaceBitmap.extLength) {
  918. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  919. if (!bitmap)
  920. return 1;
  921. map->s_uspace.s_bitmap = bitmap;
  922. bitmap->s_extLength = le32_to_cpu(
  923. phd->unallocSpaceBitmap.extLength);
  924. bitmap->s_extPosition = le32_to_cpu(
  925. phd->unallocSpaceBitmap.extPosition);
  926. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  927. udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
  928. bitmap->s_extPosition);
  929. }
  930. if (phd->partitionIntegrityTable.extLength)
  931. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  932. if (phd->freedSpaceTable.extLength) {
  933. struct kernel_lb_addr loc = {
  934. .logicalBlockNum = le32_to_cpu(
  935. phd->freedSpaceTable.extPosition),
  936. .partitionReferenceNum = p_index,
  937. };
  938. map->s_fspace.s_table = udf_iget(sb, &loc);
  939. if (!map->s_fspace.s_table) {
  940. udf_debug("cannot load freedSpaceTable (part %d)\n",
  941. p_index);
  942. return 1;
  943. }
  944. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  945. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  946. p_index, map->s_fspace.s_table->i_ino);
  947. }
  948. if (phd->freedSpaceBitmap.extLength) {
  949. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  950. if (!bitmap)
  951. return 1;
  952. map->s_fspace.s_bitmap = bitmap;
  953. bitmap->s_extLength = le32_to_cpu(
  954. phd->freedSpaceBitmap.extLength);
  955. bitmap->s_extPosition = le32_to_cpu(
  956. phd->freedSpaceBitmap.extPosition);
  957. map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
  958. udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
  959. bitmap->s_extPosition);
  960. }
  961. return 0;
  962. }
  963. static void udf_find_vat_block(struct super_block *sb, int p_index,
  964. int type1_index, sector_t start_block)
  965. {
  966. struct udf_sb_info *sbi = UDF_SB(sb);
  967. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  968. sector_t vat_block;
  969. struct kernel_lb_addr ino;
  970. /*
  971. * VAT file entry is in the last recorded block. Some broken disks have
  972. * it a few blocks before so try a bit harder...
  973. */
  974. ino.partitionReferenceNum = type1_index;
  975. for (vat_block = start_block;
  976. vat_block >= map->s_partition_root &&
  977. vat_block >= start_block - 3 &&
  978. !sbi->s_vat_inode; vat_block--) {
  979. ino.logicalBlockNum = vat_block - map->s_partition_root;
  980. sbi->s_vat_inode = udf_iget(sb, &ino);
  981. }
  982. }
  983. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  984. {
  985. struct udf_sb_info *sbi = UDF_SB(sb);
  986. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  987. struct buffer_head *bh = NULL;
  988. struct udf_inode_info *vati;
  989. uint32_t pos;
  990. struct virtualAllocationTable20 *vat20;
  991. sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  992. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  993. if (!sbi->s_vat_inode &&
  994. sbi->s_last_block != blocks - 1) {
  995. printk(KERN_NOTICE "UDF-fs: Failed to read VAT inode from the"
  996. " last recorded block (%lu), retrying with the last "
  997. "block of the device (%lu).\n",
  998. (unsigned long)sbi->s_last_block,
  999. (unsigned long)blocks - 1);
  1000. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  1001. }
  1002. if (!sbi->s_vat_inode)
  1003. return 1;
  1004. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1005. map->s_type_specific.s_virtual.s_start_offset = 0;
  1006. map->s_type_specific.s_virtual.s_num_entries =
  1007. (sbi->s_vat_inode->i_size - 36) >> 2;
  1008. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1009. vati = UDF_I(sbi->s_vat_inode);
  1010. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1011. pos = udf_block_map(sbi->s_vat_inode, 0);
  1012. bh = sb_bread(sb, pos);
  1013. if (!bh)
  1014. return 1;
  1015. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1016. } else {
  1017. vat20 = (struct virtualAllocationTable20 *)
  1018. vati->i_ext.i_data;
  1019. }
  1020. map->s_type_specific.s_virtual.s_start_offset =
  1021. le16_to_cpu(vat20->lengthHeader);
  1022. map->s_type_specific.s_virtual.s_num_entries =
  1023. (sbi->s_vat_inode->i_size -
  1024. map->s_type_specific.s_virtual.
  1025. s_start_offset) >> 2;
  1026. brelse(bh);
  1027. }
  1028. return 0;
  1029. }
  1030. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1031. {
  1032. struct buffer_head *bh;
  1033. struct partitionDesc *p;
  1034. struct udf_part_map *map;
  1035. struct udf_sb_info *sbi = UDF_SB(sb);
  1036. int i, type1_idx;
  1037. uint16_t partitionNumber;
  1038. uint16_t ident;
  1039. int ret = 0;
  1040. bh = udf_read_tagged(sb, block, block, &ident);
  1041. if (!bh)
  1042. return 1;
  1043. if (ident != TAG_IDENT_PD)
  1044. goto out_bh;
  1045. p = (struct partitionDesc *)bh->b_data;
  1046. partitionNumber = le16_to_cpu(p->partitionNumber);
  1047. /* First scan for TYPE1, SPARABLE and METADATA partitions */
  1048. for (i = 0; i < sbi->s_partitions; i++) {
  1049. map = &sbi->s_partmaps[i];
  1050. udf_debug("Searching map: (%d == %d)\n",
  1051. map->s_partition_num, partitionNumber);
  1052. if (map->s_partition_num == partitionNumber &&
  1053. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1054. map->s_partition_type == UDF_SPARABLE_MAP15))
  1055. break;
  1056. }
  1057. if (i >= sbi->s_partitions) {
  1058. udf_debug("Partition (%d) not found in partition map\n",
  1059. partitionNumber);
  1060. goto out_bh;
  1061. }
  1062. ret = udf_fill_partdesc_info(sb, p, i);
  1063. /*
  1064. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1065. * PHYSICAL partitions are already set up
  1066. */
  1067. type1_idx = i;
  1068. for (i = 0; i < sbi->s_partitions; i++) {
  1069. map = &sbi->s_partmaps[i];
  1070. if (map->s_partition_num == partitionNumber &&
  1071. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1072. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1073. map->s_partition_type == UDF_METADATA_MAP25))
  1074. break;
  1075. }
  1076. if (i >= sbi->s_partitions)
  1077. goto out_bh;
  1078. ret = udf_fill_partdesc_info(sb, p, i);
  1079. if (ret)
  1080. goto out_bh;
  1081. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1082. ret = udf_load_metadata_files(sb, i);
  1083. if (ret) {
  1084. printk(KERN_ERR "UDF-fs: error loading MetaData "
  1085. "partition map %d\n", i);
  1086. goto out_bh;
  1087. }
  1088. } else {
  1089. ret = udf_load_vat(sb, i, type1_idx);
  1090. if (ret)
  1091. goto out_bh;
  1092. /*
  1093. * Mark filesystem read-only if we have a partition with
  1094. * virtual map since we don't handle writing to it (we
  1095. * overwrite blocks instead of relocating them).
  1096. */
  1097. sb->s_flags |= MS_RDONLY;
  1098. printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
  1099. "because writing to pseudooverwrite partition is "
  1100. "not implemented.\n");
  1101. }
  1102. out_bh:
  1103. /* In case loading failed, we handle cleanup in udf_fill_super */
  1104. brelse(bh);
  1105. return ret;
  1106. }
  1107. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1108. struct kernel_lb_addr *fileset)
  1109. {
  1110. struct logicalVolDesc *lvd;
  1111. int i, j, offset;
  1112. uint8_t type;
  1113. struct udf_sb_info *sbi = UDF_SB(sb);
  1114. struct genericPartitionMap *gpm;
  1115. uint16_t ident;
  1116. struct buffer_head *bh;
  1117. int ret = 0;
  1118. bh = udf_read_tagged(sb, block, block, &ident);
  1119. if (!bh)
  1120. return 1;
  1121. BUG_ON(ident != TAG_IDENT_LVD);
  1122. lvd = (struct logicalVolDesc *)bh->b_data;
  1123. i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1124. if (i != 0) {
  1125. ret = i;
  1126. goto out_bh;
  1127. }
  1128. for (i = 0, offset = 0;
  1129. i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
  1130. i++, offset += gpm->partitionMapLength) {
  1131. struct udf_part_map *map = &sbi->s_partmaps[i];
  1132. gpm = (struct genericPartitionMap *)
  1133. &(lvd->partitionMaps[offset]);
  1134. type = gpm->partitionMapType;
  1135. if (type == 1) {
  1136. struct genericPartitionMap1 *gpm1 =
  1137. (struct genericPartitionMap1 *)gpm;
  1138. map->s_partition_type = UDF_TYPE1_MAP15;
  1139. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1140. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1141. map->s_partition_func = NULL;
  1142. } else if (type == 2) {
  1143. struct udfPartitionMap2 *upm2 =
  1144. (struct udfPartitionMap2 *)gpm;
  1145. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1146. strlen(UDF_ID_VIRTUAL))) {
  1147. u16 suf =
  1148. le16_to_cpu(((__le16 *)upm2->partIdent.
  1149. identSuffix)[0]);
  1150. if (suf < 0x0200) {
  1151. map->s_partition_type =
  1152. UDF_VIRTUAL_MAP15;
  1153. map->s_partition_func =
  1154. udf_get_pblock_virt15;
  1155. } else {
  1156. map->s_partition_type =
  1157. UDF_VIRTUAL_MAP20;
  1158. map->s_partition_func =
  1159. udf_get_pblock_virt20;
  1160. }
  1161. } else if (!strncmp(upm2->partIdent.ident,
  1162. UDF_ID_SPARABLE,
  1163. strlen(UDF_ID_SPARABLE))) {
  1164. uint32_t loc;
  1165. struct sparingTable *st;
  1166. struct sparablePartitionMap *spm =
  1167. (struct sparablePartitionMap *)gpm;
  1168. map->s_partition_type = UDF_SPARABLE_MAP15;
  1169. map->s_type_specific.s_sparing.s_packet_len =
  1170. le16_to_cpu(spm->packetLength);
  1171. for (j = 0; j < spm->numSparingTables; j++) {
  1172. struct buffer_head *bh2;
  1173. loc = le32_to_cpu(
  1174. spm->locSparingTable[j]);
  1175. bh2 = udf_read_tagged(sb, loc, loc,
  1176. &ident);
  1177. map->s_type_specific.s_sparing.
  1178. s_spar_map[j] = bh2;
  1179. if (bh2 == NULL)
  1180. continue;
  1181. st = (struct sparingTable *)bh2->b_data;
  1182. if (ident != 0 || strncmp(
  1183. st->sparingIdent.ident,
  1184. UDF_ID_SPARING,
  1185. strlen(UDF_ID_SPARING))) {
  1186. brelse(bh2);
  1187. map->s_type_specific.s_sparing.
  1188. s_spar_map[j] = NULL;
  1189. }
  1190. }
  1191. map->s_partition_func = udf_get_pblock_spar15;
  1192. } else if (!strncmp(upm2->partIdent.ident,
  1193. UDF_ID_METADATA,
  1194. strlen(UDF_ID_METADATA))) {
  1195. struct udf_meta_data *mdata =
  1196. &map->s_type_specific.s_metadata;
  1197. struct metadataPartitionMap *mdm =
  1198. (struct metadataPartitionMap *)
  1199. &(lvd->partitionMaps[offset]);
  1200. udf_debug("Parsing Logical vol part %d "
  1201. "type %d id=%s\n", i, type,
  1202. UDF_ID_METADATA);
  1203. map->s_partition_type = UDF_METADATA_MAP25;
  1204. map->s_partition_func = udf_get_pblock_meta25;
  1205. mdata->s_meta_file_loc =
  1206. le32_to_cpu(mdm->metadataFileLoc);
  1207. mdata->s_mirror_file_loc =
  1208. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1209. mdata->s_bitmap_file_loc =
  1210. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1211. mdata->s_alloc_unit_size =
  1212. le32_to_cpu(mdm->allocUnitSize);
  1213. mdata->s_align_unit_size =
  1214. le16_to_cpu(mdm->alignUnitSize);
  1215. mdata->s_dup_md_flag =
  1216. mdm->flags & 0x01;
  1217. udf_debug("Metadata Ident suffix=0x%x\n",
  1218. (le16_to_cpu(
  1219. ((__le16 *)
  1220. mdm->partIdent.identSuffix)[0])));
  1221. udf_debug("Metadata part num=%d\n",
  1222. le16_to_cpu(mdm->partitionNum));
  1223. udf_debug("Metadata part alloc unit size=%d\n",
  1224. le32_to_cpu(mdm->allocUnitSize));
  1225. udf_debug("Metadata file loc=%d\n",
  1226. le32_to_cpu(mdm->metadataFileLoc));
  1227. udf_debug("Mirror file loc=%d\n",
  1228. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1229. udf_debug("Bitmap file loc=%d\n",
  1230. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1231. udf_debug("Duplicate Flag: %d %d\n",
  1232. mdata->s_dup_md_flag, mdm->flags);
  1233. } else {
  1234. udf_debug("Unknown ident: %s\n",
  1235. upm2->partIdent.ident);
  1236. continue;
  1237. }
  1238. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1239. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1240. }
  1241. udf_debug("Partition (%d:%d) type %d on volume %d\n",
  1242. i, map->s_partition_num, type,
  1243. map->s_volumeseqnum);
  1244. }
  1245. if (fileset) {
  1246. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1247. *fileset = lelb_to_cpu(la->extLocation);
  1248. udf_debug("FileSet found in LogicalVolDesc at block=%d, "
  1249. "partition=%d\n", fileset->logicalBlockNum,
  1250. fileset->partitionReferenceNum);
  1251. }
  1252. if (lvd->integritySeqExt.extLength)
  1253. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1254. out_bh:
  1255. brelse(bh);
  1256. return ret;
  1257. }
  1258. /*
  1259. * udf_load_logicalvolint
  1260. *
  1261. */
  1262. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1263. {
  1264. struct buffer_head *bh = NULL;
  1265. uint16_t ident;
  1266. struct udf_sb_info *sbi = UDF_SB(sb);
  1267. struct logicalVolIntegrityDesc *lvid;
  1268. while (loc.extLength > 0 &&
  1269. (bh = udf_read_tagged(sb, loc.extLocation,
  1270. loc.extLocation, &ident)) &&
  1271. ident == TAG_IDENT_LVID) {
  1272. sbi->s_lvid_bh = bh;
  1273. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1274. if (lvid->nextIntegrityExt.extLength)
  1275. udf_load_logicalvolint(sb,
  1276. leea_to_cpu(lvid->nextIntegrityExt));
  1277. if (sbi->s_lvid_bh != bh)
  1278. brelse(bh);
  1279. loc.extLength -= sb->s_blocksize;
  1280. loc.extLocation++;
  1281. }
  1282. if (sbi->s_lvid_bh != bh)
  1283. brelse(bh);
  1284. }
  1285. /*
  1286. * udf_process_sequence
  1287. *
  1288. * PURPOSE
  1289. * Process a main/reserve volume descriptor sequence.
  1290. *
  1291. * PRE-CONDITIONS
  1292. * sb Pointer to _locked_ superblock.
  1293. * block First block of first extent of the sequence.
  1294. * lastblock Lastblock of first extent of the sequence.
  1295. *
  1296. * HISTORY
  1297. * July 1, 1997 - Andrew E. Mileski
  1298. * Written, tested, and released.
  1299. */
  1300. static noinline int udf_process_sequence(struct super_block *sb, long block,
  1301. long lastblock, struct kernel_lb_addr *fileset)
  1302. {
  1303. struct buffer_head *bh = NULL;
  1304. struct udf_vds_record vds[VDS_POS_LENGTH];
  1305. struct udf_vds_record *curr;
  1306. struct generic_desc *gd;
  1307. struct volDescPtr *vdp;
  1308. int done = 0;
  1309. uint32_t vdsn;
  1310. uint16_t ident;
  1311. long next_s = 0, next_e = 0;
  1312. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1313. /*
  1314. * Read the main descriptor sequence and find which descriptors
  1315. * are in it.
  1316. */
  1317. for (; (!done && block <= lastblock); block++) {
  1318. bh = udf_read_tagged(sb, block, block, &ident);
  1319. if (!bh) {
  1320. printk(KERN_ERR "udf: Block %Lu of volume descriptor "
  1321. "sequence is corrupted or we could not read "
  1322. "it.\n", (unsigned long long)block);
  1323. return 1;
  1324. }
  1325. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1326. gd = (struct generic_desc *)bh->b_data;
  1327. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1328. switch (ident) {
  1329. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1330. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1331. if (vdsn >= curr->volDescSeqNum) {
  1332. curr->volDescSeqNum = vdsn;
  1333. curr->block = block;
  1334. }
  1335. break;
  1336. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1337. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1338. if (vdsn >= curr->volDescSeqNum) {
  1339. curr->volDescSeqNum = vdsn;
  1340. curr->block = block;
  1341. vdp = (struct volDescPtr *)bh->b_data;
  1342. next_s = le32_to_cpu(
  1343. vdp->nextVolDescSeqExt.extLocation);
  1344. next_e = le32_to_cpu(
  1345. vdp->nextVolDescSeqExt.extLength);
  1346. next_e = next_e >> sb->s_blocksize_bits;
  1347. next_e += next_s;
  1348. }
  1349. break;
  1350. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1351. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1352. if (vdsn >= curr->volDescSeqNum) {
  1353. curr->volDescSeqNum = vdsn;
  1354. curr->block = block;
  1355. }
  1356. break;
  1357. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1358. curr = &vds[VDS_POS_PARTITION_DESC];
  1359. if (!curr->block)
  1360. curr->block = block;
  1361. break;
  1362. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1363. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1364. if (vdsn >= curr->volDescSeqNum) {
  1365. curr->volDescSeqNum = vdsn;
  1366. curr->block = block;
  1367. }
  1368. break;
  1369. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1370. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1371. if (vdsn >= curr->volDescSeqNum) {
  1372. curr->volDescSeqNum = vdsn;
  1373. curr->block = block;
  1374. }
  1375. break;
  1376. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1377. vds[VDS_POS_TERMINATING_DESC].block = block;
  1378. if (next_e) {
  1379. block = next_s;
  1380. lastblock = next_e;
  1381. next_s = next_e = 0;
  1382. } else
  1383. done = 1;
  1384. break;
  1385. }
  1386. brelse(bh);
  1387. }
  1388. /*
  1389. * Now read interesting descriptors again and process them
  1390. * in a suitable order
  1391. */
  1392. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1393. printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
  1394. return 1;
  1395. }
  1396. if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
  1397. return 1;
  1398. if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
  1399. vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
  1400. return 1;
  1401. if (vds[VDS_POS_PARTITION_DESC].block) {
  1402. /*
  1403. * We rescan the whole descriptor sequence to find
  1404. * partition descriptor blocks and process them.
  1405. */
  1406. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1407. block < vds[VDS_POS_TERMINATING_DESC].block;
  1408. block++)
  1409. if (udf_load_partdesc(sb, block))
  1410. return 1;
  1411. }
  1412. return 0;
  1413. }
  1414. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1415. struct kernel_lb_addr *fileset)
  1416. {
  1417. struct anchorVolDescPtr *anchor;
  1418. long main_s, main_e, reserve_s, reserve_e;
  1419. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1420. /* Locate the main sequence */
  1421. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1422. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1423. main_e = main_e >> sb->s_blocksize_bits;
  1424. main_e += main_s;
  1425. /* Locate the reserve sequence */
  1426. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1427. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1428. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1429. reserve_e += reserve_s;
  1430. /* Process the main & reserve sequences */
  1431. /* responsible for finding the PartitionDesc(s) */
  1432. if (!udf_process_sequence(sb, main_s, main_e, fileset))
  1433. return 1;
  1434. return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1435. }
  1436. /*
  1437. * Check whether there is an anchor block in the given block and
  1438. * load Volume Descriptor Sequence if so.
  1439. */
  1440. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1441. struct kernel_lb_addr *fileset)
  1442. {
  1443. struct buffer_head *bh;
  1444. uint16_t ident;
  1445. int ret;
  1446. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1447. udf_fixed_to_variable(block) >=
  1448. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1449. return 0;
  1450. bh = udf_read_tagged(sb, block, block, &ident);
  1451. if (!bh)
  1452. return 0;
  1453. if (ident != TAG_IDENT_AVDP) {
  1454. brelse(bh);
  1455. return 0;
  1456. }
  1457. ret = udf_load_sequence(sb, bh, fileset);
  1458. brelse(bh);
  1459. return ret;
  1460. }
  1461. /* Search for an anchor volume descriptor pointer */
  1462. static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
  1463. struct kernel_lb_addr *fileset)
  1464. {
  1465. sector_t last[6];
  1466. int i;
  1467. struct udf_sb_info *sbi = UDF_SB(sb);
  1468. int last_count = 0;
  1469. /* First try user provided anchor */
  1470. if (sbi->s_anchor) {
  1471. if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
  1472. return lastblock;
  1473. }
  1474. /*
  1475. * according to spec, anchor is in either:
  1476. * block 256
  1477. * lastblock-256
  1478. * lastblock
  1479. * however, if the disc isn't closed, it could be 512.
  1480. */
  1481. if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
  1482. return lastblock;
  1483. /*
  1484. * The trouble is which block is the last one. Drives often misreport
  1485. * this so we try various possibilities.
  1486. */
  1487. last[last_count++] = lastblock;
  1488. if (lastblock >= 1)
  1489. last[last_count++] = lastblock - 1;
  1490. last[last_count++] = lastblock + 1;
  1491. if (lastblock >= 2)
  1492. last[last_count++] = lastblock - 2;
  1493. if (lastblock >= 150)
  1494. last[last_count++] = lastblock - 150;
  1495. if (lastblock >= 152)
  1496. last[last_count++] = lastblock - 152;
  1497. for (i = 0; i < last_count; i++) {
  1498. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1499. sb->s_blocksize_bits)
  1500. continue;
  1501. if (udf_check_anchor_block(sb, last[i], fileset))
  1502. return last[i];
  1503. if (last[i] < 256)
  1504. continue;
  1505. if (udf_check_anchor_block(sb, last[i] - 256, fileset))
  1506. return last[i];
  1507. }
  1508. /* Finally try block 512 in case media is open */
  1509. if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
  1510. return last[0];
  1511. return 0;
  1512. }
  1513. /*
  1514. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1515. * area specified by it. The function expects sbi->s_lastblock to be the last
  1516. * block on the media.
  1517. *
  1518. * Return 1 if ok, 0 if not found.
  1519. *
  1520. */
  1521. static int udf_find_anchor(struct super_block *sb,
  1522. struct kernel_lb_addr *fileset)
  1523. {
  1524. sector_t lastblock;
  1525. struct udf_sb_info *sbi = UDF_SB(sb);
  1526. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1527. if (lastblock)
  1528. goto out;
  1529. /* No anchor found? Try VARCONV conversion of block numbers */
  1530. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1531. /* Firstly, we try to not convert number of the last block */
  1532. lastblock = udf_scan_anchors(sb,
  1533. udf_variable_to_fixed(sbi->s_last_block),
  1534. fileset);
  1535. if (lastblock)
  1536. goto out;
  1537. /* Secondly, we try with converted number of the last block */
  1538. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1539. if (!lastblock) {
  1540. /* VARCONV didn't help. Clear it. */
  1541. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1542. return 0;
  1543. }
  1544. out:
  1545. sbi->s_last_block = lastblock;
  1546. return 1;
  1547. }
  1548. /*
  1549. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1550. * Descriptor Sequence
  1551. */
  1552. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1553. int silent, struct kernel_lb_addr *fileset)
  1554. {
  1555. struct udf_sb_info *sbi = UDF_SB(sb);
  1556. loff_t nsr_off;
  1557. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1558. if (!silent)
  1559. printk(KERN_WARNING "UDF-fs: Bad block size\n");
  1560. return 0;
  1561. }
  1562. sbi->s_last_block = uopt->lastblock;
  1563. if (!uopt->novrs) {
  1564. /* Check that it is NSR02 compliant */
  1565. nsr_off = udf_check_vsd(sb);
  1566. if (!nsr_off) {
  1567. if (!silent)
  1568. printk(KERN_WARNING "UDF-fs: No VRS found\n");
  1569. return 0;
  1570. }
  1571. if (nsr_off == -1)
  1572. udf_debug("Failed to read byte 32768. Assuming open "
  1573. "disc. Skipping validity check\n");
  1574. if (!sbi->s_last_block)
  1575. sbi->s_last_block = udf_get_last_block(sb);
  1576. } else {
  1577. udf_debug("Validity check skipped because of novrs option\n");
  1578. }
  1579. /* Look for anchor block and load Volume Descriptor Sequence */
  1580. sbi->s_anchor = uopt->anchor;
  1581. if (!udf_find_anchor(sb, fileset)) {
  1582. if (!silent)
  1583. printk(KERN_WARNING "UDF-fs: No anchor found\n");
  1584. return 0;
  1585. }
  1586. return 1;
  1587. }
  1588. static void udf_open_lvid(struct super_block *sb)
  1589. {
  1590. struct udf_sb_info *sbi = UDF_SB(sb);
  1591. struct buffer_head *bh = sbi->s_lvid_bh;
  1592. struct logicalVolIntegrityDesc *lvid;
  1593. struct logicalVolIntegrityDescImpUse *lvidiu;
  1594. if (!bh)
  1595. return;
  1596. mutex_lock(&sbi->s_alloc_mutex);
  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. mutex_unlock(&sbi->s_alloc_mutex);
  1611. }
  1612. static void udf_close_lvid(struct super_block *sb)
  1613. {
  1614. struct udf_sb_info *sbi = UDF_SB(sb);
  1615. struct buffer_head *bh = sbi->s_lvid_bh;
  1616. struct logicalVolIntegrityDesc *lvid;
  1617. struct logicalVolIntegrityDescImpUse *lvidiu;
  1618. if (!bh)
  1619. return;
  1620. mutex_lock(&sbi->s_alloc_mutex);
  1621. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1622. lvidiu = udf_sb_lvidiu(sbi);
  1623. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1624. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1625. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1626. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1627. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1628. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1629. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1630. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1631. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1632. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1633. lvid->descTag.descCRC = cpu_to_le16(
  1634. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1635. le16_to_cpu(lvid->descTag.descCRCLength)));
  1636. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1637. mark_buffer_dirty(bh);
  1638. sbi->s_lvid_dirty = 0;
  1639. mutex_unlock(&sbi->s_alloc_mutex);
  1640. }
  1641. u64 lvid_get_unique_id(struct super_block *sb)
  1642. {
  1643. struct buffer_head *bh;
  1644. struct udf_sb_info *sbi = UDF_SB(sb);
  1645. struct logicalVolIntegrityDesc *lvid;
  1646. struct logicalVolHeaderDesc *lvhd;
  1647. u64 uniqueID;
  1648. u64 ret;
  1649. bh = sbi->s_lvid_bh;
  1650. if (!bh)
  1651. return 0;
  1652. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1653. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1654. mutex_lock(&sbi->s_alloc_mutex);
  1655. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1656. if (!(++uniqueID & 0xFFFFFFFF))
  1657. uniqueID += 16;
  1658. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1659. mutex_unlock(&sbi->s_alloc_mutex);
  1660. mark_buffer_dirty(bh);
  1661. return ret;
  1662. }
  1663. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  1664. {
  1665. int i;
  1666. int nr_groups = bitmap->s_nr_groups;
  1667. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  1668. nr_groups);
  1669. for (i = 0; i < nr_groups; i++)
  1670. if (bitmap->s_block_bitmap[i])
  1671. brelse(bitmap->s_block_bitmap[i]);
  1672. if (size <= PAGE_SIZE)
  1673. kfree(bitmap);
  1674. else
  1675. vfree(bitmap);
  1676. }
  1677. static void udf_free_partition(struct udf_part_map *map)
  1678. {
  1679. int i;
  1680. struct udf_meta_data *mdata;
  1681. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  1682. iput(map->s_uspace.s_table);
  1683. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  1684. iput(map->s_fspace.s_table);
  1685. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  1686. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  1687. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  1688. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  1689. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  1690. for (i = 0; i < 4; i++)
  1691. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  1692. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  1693. mdata = &map->s_type_specific.s_metadata;
  1694. iput(mdata->s_metadata_fe);
  1695. mdata->s_metadata_fe = NULL;
  1696. iput(mdata->s_mirror_fe);
  1697. mdata->s_mirror_fe = NULL;
  1698. iput(mdata->s_bitmap_fe);
  1699. mdata->s_bitmap_fe = NULL;
  1700. }
  1701. }
  1702. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1703. {
  1704. int i;
  1705. int ret;
  1706. struct inode *inode = NULL;
  1707. struct udf_options uopt;
  1708. struct kernel_lb_addr rootdir, fileset;
  1709. struct udf_sb_info *sbi;
  1710. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1711. uopt.uid = -1;
  1712. uopt.gid = -1;
  1713. uopt.umask = 0;
  1714. uopt.fmode = UDF_INVALID_MODE;
  1715. uopt.dmode = UDF_INVALID_MODE;
  1716. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1717. if (!sbi)
  1718. return -ENOMEM;
  1719. sb->s_fs_info = sbi;
  1720. mutex_init(&sbi->s_alloc_mutex);
  1721. if (!udf_parse_options((char *)options, &uopt, false))
  1722. goto error_out;
  1723. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1724. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1725. udf_error(sb, "udf_read_super",
  1726. "utf8 cannot be combined with iocharset\n");
  1727. goto error_out;
  1728. }
  1729. #ifdef CONFIG_UDF_NLS
  1730. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1731. uopt.nls_map = load_nls_default();
  1732. if (!uopt.nls_map)
  1733. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1734. else
  1735. udf_debug("Using default NLS map\n");
  1736. }
  1737. #endif
  1738. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1739. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1740. fileset.logicalBlockNum = 0xFFFFFFFF;
  1741. fileset.partitionReferenceNum = 0xFFFF;
  1742. sbi->s_flags = uopt.flags;
  1743. sbi->s_uid = uopt.uid;
  1744. sbi->s_gid = uopt.gid;
  1745. sbi->s_umask = uopt.umask;
  1746. sbi->s_fmode = uopt.fmode;
  1747. sbi->s_dmode = uopt.dmode;
  1748. sbi->s_nls_map = uopt.nls_map;
  1749. rwlock_init(&sbi->s_cred_lock);
  1750. if (uopt.session == 0xFFFFFFFF)
  1751. sbi->s_session = udf_get_last_session(sb);
  1752. else
  1753. sbi->s_session = uopt.session;
  1754. udf_debug("Multi-session=%d\n", sbi->s_session);
  1755. /* Fill in the rest of the superblock */
  1756. sb->s_op = &udf_sb_ops;
  1757. sb->s_export_op = &udf_export_ops;
  1758. sb->s_dirt = 0;
  1759. sb->s_magic = UDF_SUPER_MAGIC;
  1760. sb->s_time_gran = 1000;
  1761. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1762. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1763. } else {
  1764. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1765. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1766. if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1767. if (!silent)
  1768. printk(KERN_NOTICE
  1769. "UDF-fs: Rescanning with blocksize "
  1770. "%d\n", UDF_DEFAULT_BLOCKSIZE);
  1771. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1772. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1773. }
  1774. }
  1775. if (!ret) {
  1776. printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
  1777. goto error_out;
  1778. }
  1779. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1780. if (sbi->s_lvid_bh) {
  1781. struct logicalVolIntegrityDescImpUse *lvidiu =
  1782. udf_sb_lvidiu(sbi);
  1783. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1784. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1785. /* uint16_t maxUDFWriteRev =
  1786. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1787. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1788. printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
  1789. "(max is %x)\n",
  1790. le16_to_cpu(lvidiu->minUDFReadRev),
  1791. UDF_MAX_READ_VERSION);
  1792. goto error_out;
  1793. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1794. sb->s_flags |= MS_RDONLY;
  1795. sbi->s_udfrev = minUDFWriteRev;
  1796. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1797. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1798. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1799. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1800. }
  1801. if (!sbi->s_partitions) {
  1802. printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
  1803. goto error_out;
  1804. }
  1805. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1806. UDF_PART_FLAG_READ_ONLY) {
  1807. printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
  1808. "forcing readonly mount\n");
  1809. sb->s_flags |= MS_RDONLY;
  1810. }
  1811. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1812. printk(KERN_WARNING "UDF-fs: No fileset found\n");
  1813. goto error_out;
  1814. }
  1815. if (!silent) {
  1816. struct timestamp ts;
  1817. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1818. udf_info("UDF: Mounting volume '%s', "
  1819. "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1820. sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
  1821. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1822. }
  1823. if (!(sb->s_flags & MS_RDONLY))
  1824. udf_open_lvid(sb);
  1825. /* Assign the root inode */
  1826. /* assign inodes by physical block number */
  1827. /* perhaps it's not extensible enough, but for now ... */
  1828. inode = udf_iget(sb, &rootdir);
  1829. if (!inode) {
  1830. printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
  1831. "partition=%d\n",
  1832. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1833. goto error_out;
  1834. }
  1835. /* Allocate a dentry for the root inode */
  1836. sb->s_root = d_alloc_root(inode);
  1837. if (!sb->s_root) {
  1838. printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
  1839. iput(inode);
  1840. goto error_out;
  1841. }
  1842. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1843. return 0;
  1844. error_out:
  1845. if (sbi->s_vat_inode)
  1846. iput(sbi->s_vat_inode);
  1847. if (sbi->s_partitions)
  1848. for (i = 0; i < sbi->s_partitions; i++)
  1849. udf_free_partition(&sbi->s_partmaps[i]);
  1850. #ifdef CONFIG_UDF_NLS
  1851. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1852. unload_nls(sbi->s_nls_map);
  1853. #endif
  1854. if (!(sb->s_flags & MS_RDONLY))
  1855. udf_close_lvid(sb);
  1856. brelse(sbi->s_lvid_bh);
  1857. kfree(sbi->s_partmaps);
  1858. kfree(sbi);
  1859. sb->s_fs_info = NULL;
  1860. return -EINVAL;
  1861. }
  1862. static void udf_error(struct super_block *sb, const char *function,
  1863. const char *fmt, ...)
  1864. {
  1865. va_list args;
  1866. if (!(sb->s_flags & MS_RDONLY)) {
  1867. /* mark sb error */
  1868. sb->s_dirt = 1;
  1869. }
  1870. va_start(args, fmt);
  1871. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1872. va_end(args);
  1873. printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
  1874. sb->s_id, function, error_buf);
  1875. }
  1876. void udf_warning(struct super_block *sb, const char *function,
  1877. const char *fmt, ...)
  1878. {
  1879. va_list args;
  1880. va_start(args, fmt);
  1881. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1882. va_end(args);
  1883. printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
  1884. sb->s_id, function, error_buf);
  1885. }
  1886. static void udf_put_super(struct super_block *sb)
  1887. {
  1888. int i;
  1889. struct udf_sb_info *sbi;
  1890. sbi = UDF_SB(sb);
  1891. if (sbi->s_vat_inode)
  1892. iput(sbi->s_vat_inode);
  1893. if (sbi->s_partitions)
  1894. for (i = 0; i < sbi->s_partitions; i++)
  1895. udf_free_partition(&sbi->s_partmaps[i]);
  1896. #ifdef CONFIG_UDF_NLS
  1897. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1898. unload_nls(sbi->s_nls_map);
  1899. #endif
  1900. if (!(sb->s_flags & MS_RDONLY))
  1901. udf_close_lvid(sb);
  1902. brelse(sbi->s_lvid_bh);
  1903. kfree(sbi->s_partmaps);
  1904. kfree(sb->s_fs_info);
  1905. sb->s_fs_info = NULL;
  1906. }
  1907. static int udf_sync_fs(struct super_block *sb, int wait)
  1908. {
  1909. struct udf_sb_info *sbi = UDF_SB(sb);
  1910. mutex_lock(&sbi->s_alloc_mutex);
  1911. if (sbi->s_lvid_dirty) {
  1912. /*
  1913. * Blockdevice will be synced later so we don't have to submit
  1914. * the buffer for IO
  1915. */
  1916. mark_buffer_dirty(sbi->s_lvid_bh);
  1917. sb->s_dirt = 0;
  1918. sbi->s_lvid_dirty = 0;
  1919. }
  1920. mutex_unlock(&sbi->s_alloc_mutex);
  1921. return 0;
  1922. }
  1923. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1924. {
  1925. struct super_block *sb = dentry->d_sb;
  1926. struct udf_sb_info *sbi = UDF_SB(sb);
  1927. struct logicalVolIntegrityDescImpUse *lvidiu;
  1928. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1929. if (sbi->s_lvid_bh != NULL)
  1930. lvidiu = udf_sb_lvidiu(sbi);
  1931. else
  1932. lvidiu = NULL;
  1933. buf->f_type = UDF_SUPER_MAGIC;
  1934. buf->f_bsize = sb->s_blocksize;
  1935. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1936. buf->f_bfree = udf_count_free(sb);
  1937. buf->f_bavail = buf->f_bfree;
  1938. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1939. le32_to_cpu(lvidiu->numDirs)) : 0)
  1940. + buf->f_bfree;
  1941. buf->f_ffree = buf->f_bfree;
  1942. buf->f_namelen = UDF_NAME_LEN - 2;
  1943. buf->f_fsid.val[0] = (u32)id;
  1944. buf->f_fsid.val[1] = (u32)(id >> 32);
  1945. return 0;
  1946. }
  1947. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1948. struct udf_bitmap *bitmap)
  1949. {
  1950. struct buffer_head *bh = NULL;
  1951. unsigned int accum = 0;
  1952. int index;
  1953. int block = 0, newblock;
  1954. struct kernel_lb_addr loc;
  1955. uint32_t bytes;
  1956. uint8_t *ptr;
  1957. uint16_t ident;
  1958. struct spaceBitmapDesc *bm;
  1959. loc.logicalBlockNum = bitmap->s_extPosition;
  1960. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1961. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  1962. if (!bh) {
  1963. printk(KERN_ERR "udf: udf_count_free failed\n");
  1964. goto out;
  1965. } else if (ident != TAG_IDENT_SBD) {
  1966. brelse(bh);
  1967. printk(KERN_ERR "udf: udf_count_free failed\n");
  1968. goto out;
  1969. }
  1970. bm = (struct spaceBitmapDesc *)bh->b_data;
  1971. bytes = le32_to_cpu(bm->numOfBytes);
  1972. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1973. ptr = (uint8_t *)bh->b_data;
  1974. while (bytes > 0) {
  1975. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1976. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1977. cur_bytes * 8);
  1978. bytes -= cur_bytes;
  1979. if (bytes) {
  1980. brelse(bh);
  1981. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  1982. bh = udf_tread(sb, newblock);
  1983. if (!bh) {
  1984. udf_debug("read failed\n");
  1985. goto out;
  1986. }
  1987. index = 0;
  1988. ptr = (uint8_t *)bh->b_data;
  1989. }
  1990. }
  1991. brelse(bh);
  1992. out:
  1993. return accum;
  1994. }
  1995. static unsigned int udf_count_free_table(struct super_block *sb,
  1996. struct inode *table)
  1997. {
  1998. unsigned int accum = 0;
  1999. uint32_t elen;
  2000. struct kernel_lb_addr eloc;
  2001. int8_t etype;
  2002. struct extent_position epos;
  2003. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2004. epos.block = UDF_I(table)->i_location;
  2005. epos.offset = sizeof(struct unallocSpaceEntry);
  2006. epos.bh = NULL;
  2007. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2008. accum += (elen >> table->i_sb->s_blocksize_bits);
  2009. brelse(epos.bh);
  2010. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2011. return accum;
  2012. }
  2013. static unsigned int udf_count_free(struct super_block *sb)
  2014. {
  2015. unsigned int accum = 0;
  2016. struct udf_sb_info *sbi;
  2017. struct udf_part_map *map;
  2018. sbi = UDF_SB(sb);
  2019. if (sbi->s_lvid_bh) {
  2020. struct logicalVolIntegrityDesc *lvid =
  2021. (struct logicalVolIntegrityDesc *)
  2022. sbi->s_lvid_bh->b_data;
  2023. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2024. accum = le32_to_cpu(
  2025. lvid->freeSpaceTable[sbi->s_partition]);
  2026. if (accum == 0xFFFFFFFF)
  2027. accum = 0;
  2028. }
  2029. }
  2030. if (accum)
  2031. return accum;
  2032. map = &sbi->s_partmaps[sbi->s_partition];
  2033. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2034. accum += udf_count_free_bitmap(sb,
  2035. map->s_uspace.s_bitmap);
  2036. }
  2037. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2038. accum += udf_count_free_bitmap(sb,
  2039. map->s_fspace.s_bitmap);
  2040. }
  2041. if (accum)
  2042. return accum;
  2043. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2044. accum += udf_count_free_table(sb,
  2045. map->s_uspace.s_table);
  2046. }
  2047. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2048. accum += udf_count_free_table(sb,
  2049. map->s_fspace.s_table);
  2050. }
  2051. return accum;
  2052. }