super.c 66 KB

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