super.c 66 KB

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