super.c 67 KB

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