super.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  1. /*
  2. * super.c
  3. *
  4. * PURPOSE
  5. * Super block routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * DESCRIPTION
  8. * OSTA-UDF(tm) = Optical Storage Technology Association
  9. * Universal Disk Format.
  10. *
  11. * This code is based on version 2.00 of the UDF specification,
  12. * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
  13. * http://www.osta.org/
  14. * http://www.ecma.ch/
  15. * http://www.iso.org/
  16. *
  17. * COPYRIGHT
  18. * This file is distributed under the terms of the GNU General Public
  19. * License (GPL). Copies of the GPL can be obtained from:
  20. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  21. * Each contributing author retains all rights to their own work.
  22. *
  23. * (C) 1998 Dave Boynton
  24. * (C) 1998-2004 Ben Fennema
  25. * (C) 2000 Stelias Computing Inc
  26. *
  27. * HISTORY
  28. *
  29. * 09/24/98 dgb changed to allow compiling outside of kernel, and
  30. * added some debugging.
  31. * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
  32. * 10/16/98 attempting some multi-session support
  33. * 10/17/98 added freespace count for "df"
  34. * 11/11/98 gr added novrs option
  35. * 11/26/98 dgb added fileset,anchor mount options
  36. * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
  37. * vol descs. rewrote option handling based on isofs
  38. * 12/20/98 find the free space bitmap (if it exists)
  39. */
  40. #include "udfdecl.h"
  41. #include <linux/blkdev.h>
  42. #include <linux/slab.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/parser.h>
  46. #include <linux/stat.h>
  47. #include <linux/cdrom.h>
  48. #include <linux/nls.h>
  49. #include <linux/buffer_head.h>
  50. #include <linux/vfs.h>
  51. #include <linux/vmalloc.h>
  52. #include <linux/errno.h>
  53. #include <linux/mount.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/bitmap.h>
  56. #include <linux/crc-itu-t.h>
  57. #include <linux/log2.h>
  58. #include <asm/byteorder.h>
  59. #include "udf_sb.h"
  60. #include "udf_i.h"
  61. #include <linux/init.h>
  62. #include <linux/uaccess.h>
  63. #define VDS_POS_PRIMARY_VOL_DESC 0
  64. #define VDS_POS_UNALLOC_SPACE_DESC 1
  65. #define VDS_POS_LOGICAL_VOL_DESC 2
  66. #define VDS_POS_PARTITION_DESC 3
  67. #define VDS_POS_IMP_USE_VOL_DESC 4
  68. #define VDS_POS_VOL_DESC_PTR 5
  69. #define VDS_POS_TERMINATING_DESC 6
  70. #define VDS_POS_LENGTH 7
  71. #define UDF_DEFAULT_BLOCKSIZE 2048
  72. #define VSD_FIRST_SECTOR_OFFSET 32768
  73. #define VSD_MAX_SECTOR_OFFSET 0x800000
  74. enum { UDF_MAX_LINKS = 0xffff };
  75. /* These are the "meat" - everything else is stuffing */
  76. static int udf_fill_super(struct super_block *, void *, int);
  77. static void udf_put_super(struct super_block *);
  78. static int udf_sync_fs(struct super_block *, int);
  79. static int udf_remount_fs(struct super_block *, int *, char *);
  80. static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
  81. static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
  82. struct kernel_lb_addr *);
  83. static void udf_load_fileset(struct super_block *, struct buffer_head *,
  84. struct kernel_lb_addr *);
  85. static void udf_open_lvid(struct super_block *);
  86. static void udf_close_lvid(struct super_block *);
  87. static unsigned int udf_count_free(struct super_block *);
  88. static int udf_statfs(struct dentry *, struct kstatfs *);
  89. static int udf_show_options(struct seq_file *, struct dentry *);
  90. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
  91. {
  92. struct logicalVolIntegrityDesc *lvid;
  93. unsigned int partnum;
  94. unsigned int offset;
  95. if (!UDF_SB(sb)->s_lvid_bh)
  96. return NULL;
  97. lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
  98. partnum = le32_to_cpu(lvid->numOfPartitions);
  99. if ((sb->s_blocksize - sizeof(struct logicalVolIntegrityDescImpUse) -
  100. offsetof(struct logicalVolIntegrityDesc, impUse)) /
  101. (2 * sizeof(uint32_t)) < partnum) {
  102. udf_err(sb, "Logical volume integrity descriptor corrupted "
  103. "(numOfPartitions = %u)!\n", partnum);
  104. return NULL;
  105. }
  106. /* The offset is to skip freeSpaceTable and sizeTable arrays */
  107. offset = partnum * 2 * sizeof(uint32_t);
  108. return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
  109. }
  110. /* UDF filesystem type */
  111. static struct dentry *udf_mount(struct file_system_type *fs_type,
  112. int flags, const char *dev_name, void *data)
  113. {
  114. return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
  115. }
  116. static struct file_system_type udf_fstype = {
  117. .owner = THIS_MODULE,
  118. .name = "udf",
  119. .mount = udf_mount,
  120. .kill_sb = kill_block_super,
  121. .fs_flags = FS_REQUIRES_DEV,
  122. };
  123. MODULE_ALIAS_FS("udf");
  124. static struct kmem_cache *udf_inode_cachep;
  125. static struct inode *udf_alloc_inode(struct super_block *sb)
  126. {
  127. struct udf_inode_info *ei;
  128. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  129. if (!ei)
  130. return NULL;
  131. ei->i_unique = 0;
  132. ei->i_lenExtents = 0;
  133. ei->i_next_alloc_block = 0;
  134. ei->i_next_alloc_goal = 0;
  135. ei->i_strat4096 = 0;
  136. init_rwsem(&ei->i_data_sem);
  137. ei->cached_extent.lstart = -1;
  138. spin_lock_init(&ei->i_extent_cache_lock);
  139. return &ei->vfs_inode;
  140. }
  141. static void udf_i_callback(struct rcu_head *head)
  142. {
  143. struct inode *inode = container_of(head, struct inode, i_rcu);
  144. kmem_cache_free(udf_inode_cachep, UDF_I(inode));
  145. }
  146. static void udf_destroy_inode(struct inode *inode)
  147. {
  148. call_rcu(&inode->i_rcu, udf_i_callback);
  149. }
  150. static void init_once(void *foo)
  151. {
  152. struct udf_inode_info *ei = (struct udf_inode_info *)foo;
  153. ei->i_ext.i_data = NULL;
  154. inode_init_once(&ei->vfs_inode);
  155. }
  156. static int __init init_inodecache(void)
  157. {
  158. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  159. sizeof(struct udf_inode_info),
  160. 0, (SLAB_RECLAIM_ACCOUNT |
  161. SLAB_MEM_SPREAD),
  162. init_once);
  163. if (!udf_inode_cachep)
  164. return -ENOMEM;
  165. return 0;
  166. }
  167. static void destroy_inodecache(void)
  168. {
  169. /*
  170. * Make sure all delayed rcu free inodes are flushed before we
  171. * destroy cache.
  172. */
  173. rcu_barrier();
  174. kmem_cache_destroy(udf_inode_cachep);
  175. }
  176. /* Superblock operations */
  177. static const struct super_operations udf_sb_ops = {
  178. .alloc_inode = udf_alloc_inode,
  179. .destroy_inode = udf_destroy_inode,
  180. .write_inode = udf_write_inode,
  181. .evict_inode = udf_evict_inode,
  182. .put_super = udf_put_super,
  183. .sync_fs = udf_sync_fs,
  184. .statfs = udf_statfs,
  185. .remount_fs = udf_remount_fs,
  186. .show_options = udf_show_options,
  187. };
  188. struct udf_options {
  189. unsigned char novrs;
  190. unsigned int blocksize;
  191. unsigned int session;
  192. unsigned int lastblock;
  193. unsigned int anchor;
  194. unsigned int volume;
  195. unsigned short partition;
  196. unsigned int fileset;
  197. unsigned int rootdir;
  198. unsigned int flags;
  199. umode_t umask;
  200. kgid_t gid;
  201. kuid_t uid;
  202. umode_t fmode;
  203. umode_t dmode;
  204. struct nls_table *nls_map;
  205. };
  206. static int __init init_udf_fs(void)
  207. {
  208. int err;
  209. err = init_inodecache();
  210. if (err)
  211. goto out1;
  212. err = register_filesystem(&udf_fstype);
  213. if (err)
  214. goto out;
  215. return 0;
  216. out:
  217. destroy_inodecache();
  218. out1:
  219. return err;
  220. }
  221. static void __exit exit_udf_fs(void)
  222. {
  223. unregister_filesystem(&udf_fstype);
  224. destroy_inodecache();
  225. }
  226. module_init(init_udf_fs)
  227. module_exit(exit_udf_fs)
  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(struct udf_part_map),
  232. GFP_KERNEL);
  233. if (!sbi->s_partmaps) {
  234. udf_err(sb, "Unable to allocate space for %d partition maps\n",
  235. count);
  236. sbi->s_partitions = 0;
  237. return -ENOMEM;
  238. }
  239. sbi->s_partitions = count;
  240. return 0;
  241. }
  242. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  243. {
  244. int i;
  245. int nr_groups = bitmap->s_nr_groups;
  246. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  247. nr_groups);
  248. for (i = 0; i < nr_groups; i++)
  249. if (bitmap->s_block_bitmap[i])
  250. brelse(bitmap->s_block_bitmap[i]);
  251. if (size <= PAGE_SIZE)
  252. kfree(bitmap);
  253. else
  254. vfree(bitmap);
  255. }
  256. static void udf_free_partition(struct udf_part_map *map)
  257. {
  258. int i;
  259. struct udf_meta_data *mdata;
  260. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  261. iput(map->s_uspace.s_table);
  262. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  263. iput(map->s_fspace.s_table);
  264. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  265. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  266. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  267. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  268. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  269. for (i = 0; i < 4; i++)
  270. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  271. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  272. mdata = &map->s_type_specific.s_metadata;
  273. iput(mdata->s_metadata_fe);
  274. mdata->s_metadata_fe = NULL;
  275. iput(mdata->s_mirror_fe);
  276. mdata->s_mirror_fe = NULL;
  277. iput(mdata->s_bitmap_fe);
  278. mdata->s_bitmap_fe = NULL;
  279. }
  280. }
  281. static void udf_sb_free_partitions(struct super_block *sb)
  282. {
  283. struct udf_sb_info *sbi = UDF_SB(sb);
  284. int i;
  285. if (sbi->s_partmaps == NULL)
  286. return;
  287. for (i = 0; i < sbi->s_partitions; i++)
  288. udf_free_partition(&sbi->s_partmaps[i]);
  289. kfree(sbi->s_partmaps);
  290. sbi->s_partmaps = NULL;
  291. }
  292. static int udf_show_options(struct seq_file *seq, struct dentry *root)
  293. {
  294. struct super_block *sb = root->d_sb;
  295. struct udf_sb_info *sbi = UDF_SB(sb);
  296. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
  297. seq_puts(seq, ",nostrict");
  298. if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
  299. seq_printf(seq, ",bs=%lu", sb->s_blocksize);
  300. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  301. seq_puts(seq, ",unhide");
  302. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  303. seq_puts(seq, ",undelete");
  304. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
  305. seq_puts(seq, ",noadinicb");
  306. if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
  307. seq_puts(seq, ",shortad");
  308. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
  309. seq_puts(seq, ",uid=forget");
  310. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
  311. seq_puts(seq, ",uid=ignore");
  312. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
  313. seq_puts(seq, ",gid=forget");
  314. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
  315. seq_puts(seq, ",gid=ignore");
  316. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
  317. seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
  318. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
  319. seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
  320. if (sbi->s_umask != 0)
  321. seq_printf(seq, ",umask=%ho", sbi->s_umask);
  322. if (sbi->s_fmode != UDF_INVALID_MODE)
  323. seq_printf(seq, ",mode=%ho", sbi->s_fmode);
  324. if (sbi->s_dmode != UDF_INVALID_MODE)
  325. seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
  326. if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
  327. seq_printf(seq, ",session=%u", sbi->s_session);
  328. if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
  329. seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
  330. if (sbi->s_anchor != 0)
  331. seq_printf(seq, ",anchor=%u", sbi->s_anchor);
  332. /*
  333. * volume, partition, fileset and rootdir seem to be ignored
  334. * currently
  335. */
  336. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
  337. seq_puts(seq, ",utf8");
  338. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
  339. seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
  340. return 0;
  341. }
  342. /*
  343. * udf_parse_options
  344. *
  345. * PURPOSE
  346. * Parse mount options.
  347. *
  348. * DESCRIPTION
  349. * The following mount options are supported:
  350. *
  351. * gid= Set the default group.
  352. * umask= Set the default umask.
  353. * mode= Set the default file permissions.
  354. * dmode= Set the default directory permissions.
  355. * uid= Set the default user.
  356. * bs= Set the block size.
  357. * unhide Show otherwise hidden files.
  358. * undelete Show deleted files in lists.
  359. * adinicb Embed data in the inode (default)
  360. * noadinicb Don't embed data in the inode
  361. * shortad Use short ad's
  362. * longad Use long ad's (default)
  363. * nostrict Unset strict conformance
  364. * iocharset= Set the NLS character set
  365. *
  366. * The remaining are for debugging and disaster recovery:
  367. *
  368. * novrs Skip volume sequence recognition
  369. *
  370. * The following expect a offset from 0.
  371. *
  372. * session= Set the CDROM session (default= last session)
  373. * anchor= Override standard anchor location. (default= 256)
  374. * volume= Override the VolumeDesc location. (unused)
  375. * partition= Override the PartitionDesc location. (unused)
  376. * lastblock= Set the last block of the filesystem/
  377. *
  378. * The following expect a offset from the partition root.
  379. *
  380. * fileset= Override the fileset block location. (unused)
  381. * rootdir= Override the root directory location. (unused)
  382. * WARNING: overriding the rootdir to a non-directory may
  383. * yield highly unpredictable results.
  384. *
  385. * PRE-CONDITIONS
  386. * options Pointer to mount options string.
  387. * uopts Pointer to mount options variable.
  388. *
  389. * POST-CONDITIONS
  390. * <return> 1 Mount options parsed okay.
  391. * <return> 0 Error parsing mount options.
  392. *
  393. * HISTORY
  394. * July 1, 1997 - Andrew E. Mileski
  395. * Written, tested, and released.
  396. */
  397. enum {
  398. Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
  399. Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
  400. Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
  401. Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
  402. Opt_rootdir, Opt_utf8, Opt_iocharset,
  403. Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
  404. Opt_fmode, Opt_dmode
  405. };
  406. static const match_table_t tokens = {
  407. {Opt_novrs, "novrs"},
  408. {Opt_nostrict, "nostrict"},
  409. {Opt_bs, "bs=%u"},
  410. {Opt_unhide, "unhide"},
  411. {Opt_undelete, "undelete"},
  412. {Opt_noadinicb, "noadinicb"},
  413. {Opt_adinicb, "adinicb"},
  414. {Opt_shortad, "shortad"},
  415. {Opt_longad, "longad"},
  416. {Opt_uforget, "uid=forget"},
  417. {Opt_uignore, "uid=ignore"},
  418. {Opt_gforget, "gid=forget"},
  419. {Opt_gignore, "gid=ignore"},
  420. {Opt_gid, "gid=%u"},
  421. {Opt_uid, "uid=%u"},
  422. {Opt_umask, "umask=%o"},
  423. {Opt_session, "session=%u"},
  424. {Opt_lastblock, "lastblock=%u"},
  425. {Opt_anchor, "anchor=%u"},
  426. {Opt_volume, "volume=%u"},
  427. {Opt_partition, "partition=%u"},
  428. {Opt_fileset, "fileset=%u"},
  429. {Opt_rootdir, "rootdir=%u"},
  430. {Opt_utf8, "utf8"},
  431. {Opt_iocharset, "iocharset=%s"},
  432. {Opt_fmode, "mode=%o"},
  433. {Opt_dmode, "dmode=%o"},
  434. {Opt_err, NULL}
  435. };
  436. static int udf_parse_options(char *options, struct udf_options *uopt,
  437. bool remount)
  438. {
  439. char *p;
  440. int option;
  441. uopt->novrs = 0;
  442. uopt->partition = 0xFFFF;
  443. uopt->session = 0xFFFFFFFF;
  444. uopt->lastblock = 0;
  445. uopt->anchor = 0;
  446. uopt->volume = 0xFFFFFFFF;
  447. uopt->rootdir = 0xFFFFFFFF;
  448. uopt->fileset = 0xFFFFFFFF;
  449. uopt->nls_map = NULL;
  450. if (!options)
  451. return 1;
  452. while ((p = strsep(&options, ",")) != NULL) {
  453. substring_t args[MAX_OPT_ARGS];
  454. int token;
  455. unsigned n;
  456. if (!*p)
  457. continue;
  458. token = match_token(p, tokens, args);
  459. switch (token) {
  460. case Opt_novrs:
  461. uopt->novrs = 1;
  462. break;
  463. case Opt_bs:
  464. if (match_int(&args[0], &option))
  465. return 0;
  466. n = option;
  467. if (n != 512 && n != 1024 && n != 2048 && n != 4096)
  468. return 0;
  469. uopt->blocksize = n;
  470. uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
  471. break;
  472. case Opt_unhide:
  473. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  474. break;
  475. case Opt_undelete:
  476. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  477. break;
  478. case Opt_noadinicb:
  479. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  480. break;
  481. case Opt_adinicb:
  482. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  483. break;
  484. case Opt_shortad:
  485. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  486. break;
  487. case Opt_longad:
  488. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  489. break;
  490. case Opt_gid:
  491. if (match_int(args, &option))
  492. return 0;
  493. uopt->gid = make_kgid(current_user_ns(), option);
  494. if (!gid_valid(uopt->gid))
  495. return 0;
  496. uopt->flags |= (1 << UDF_FLAG_GID_SET);
  497. break;
  498. case Opt_uid:
  499. if (match_int(args, &option))
  500. return 0;
  501. uopt->uid = make_kuid(current_user_ns(), option);
  502. if (!uid_valid(uopt->uid))
  503. return 0;
  504. uopt->flags |= (1 << UDF_FLAG_UID_SET);
  505. break;
  506. case Opt_umask:
  507. if (match_octal(args, &option))
  508. return 0;
  509. uopt->umask = option;
  510. break;
  511. case Opt_nostrict:
  512. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  513. break;
  514. case Opt_session:
  515. if (match_int(args, &option))
  516. return 0;
  517. uopt->session = option;
  518. if (!remount)
  519. uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
  520. break;
  521. case Opt_lastblock:
  522. if (match_int(args, &option))
  523. return 0;
  524. uopt->lastblock = option;
  525. if (!remount)
  526. uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
  527. break;
  528. case Opt_anchor:
  529. if (match_int(args, &option))
  530. return 0;
  531. uopt->anchor = option;
  532. break;
  533. case Opt_volume:
  534. if (match_int(args, &option))
  535. return 0;
  536. uopt->volume = option;
  537. break;
  538. case Opt_partition:
  539. if (match_int(args, &option))
  540. return 0;
  541. uopt->partition = option;
  542. break;
  543. case Opt_fileset:
  544. if (match_int(args, &option))
  545. return 0;
  546. uopt->fileset = option;
  547. break;
  548. case Opt_rootdir:
  549. if (match_int(args, &option))
  550. return 0;
  551. uopt->rootdir = option;
  552. break;
  553. case Opt_utf8:
  554. uopt->flags |= (1 << UDF_FLAG_UTF8);
  555. break;
  556. #ifdef CONFIG_UDF_NLS
  557. case Opt_iocharset:
  558. uopt->nls_map = load_nls(args[0].from);
  559. uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
  560. break;
  561. #endif
  562. case Opt_uignore:
  563. uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
  564. break;
  565. case Opt_uforget:
  566. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  567. break;
  568. case Opt_gignore:
  569. uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
  570. break;
  571. case Opt_gforget:
  572. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  573. break;
  574. case Opt_fmode:
  575. if (match_octal(args, &option))
  576. return 0;
  577. uopt->fmode = option & 0777;
  578. break;
  579. case Opt_dmode:
  580. if (match_octal(args, &option))
  581. return 0;
  582. uopt->dmode = option & 0777;
  583. break;
  584. default:
  585. pr_err("bad mount option \"%s\" or missing value\n", p);
  586. return 0;
  587. }
  588. }
  589. return 1;
  590. }
  591. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  592. {
  593. struct udf_options uopt;
  594. struct udf_sb_info *sbi = UDF_SB(sb);
  595. int error = 0;
  596. struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
  597. sync_filesystem(sb);
  598. if (lvidiu) {
  599. int write_rev = le16_to_cpu(lvidiu->minUDFWriteRev);
  600. if (write_rev > UDF_MAX_WRITE_VERSION && !(*flags & MS_RDONLY))
  601. return -EACCES;
  602. }
  603. uopt.flags = sbi->s_flags;
  604. uopt.uid = sbi->s_uid;
  605. uopt.gid = sbi->s_gid;
  606. uopt.umask = sbi->s_umask;
  607. uopt.fmode = sbi->s_fmode;
  608. uopt.dmode = sbi->s_dmode;
  609. if (!udf_parse_options(options, &uopt, true))
  610. return -EINVAL;
  611. write_lock(&sbi->s_cred_lock);
  612. sbi->s_flags = uopt.flags;
  613. sbi->s_uid = uopt.uid;
  614. sbi->s_gid = uopt.gid;
  615. sbi->s_umask = uopt.umask;
  616. sbi->s_fmode = uopt.fmode;
  617. sbi->s_dmode = uopt.dmode;
  618. write_unlock(&sbi->s_cred_lock);
  619. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  620. goto out_unlock;
  621. if (*flags & MS_RDONLY)
  622. udf_close_lvid(sb);
  623. else
  624. udf_open_lvid(sb);
  625. out_unlock:
  626. return error;
  627. }
  628. /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
  629. /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
  630. static loff_t udf_check_vsd(struct super_block *sb)
  631. {
  632. struct volStructDesc *vsd = NULL;
  633. loff_t sector = VSD_FIRST_SECTOR_OFFSET;
  634. int sectorsize;
  635. struct buffer_head *bh = NULL;
  636. int nsr02 = 0;
  637. int nsr03 = 0;
  638. struct udf_sb_info *sbi;
  639. sbi = UDF_SB(sb);
  640. if (sb->s_blocksize < sizeof(struct volStructDesc))
  641. sectorsize = sizeof(struct volStructDesc);
  642. else
  643. sectorsize = sb->s_blocksize;
  644. sector += (sbi->s_session << sb->s_blocksize_bits);
  645. udf_debug("Starting at sector %u (%ld byte sectors)\n",
  646. (unsigned int)(sector >> sb->s_blocksize_bits),
  647. sb->s_blocksize);
  648. /* Process the sequence (if applicable). The hard limit on the sector
  649. * offset is arbitrary, hopefully large enough so that all valid UDF
  650. * filesystems will be recognised. There is no mention of an upper
  651. * bound to the size of the volume recognition area in the standard.
  652. * The limit will prevent the code to read all the sectors of a
  653. * specially crafted image (like a bluray disc full of CD001 sectors),
  654. * potentially causing minutes or even hours of uninterruptible I/O
  655. * activity. This actually happened with uninitialised SSD partitions
  656. * (all 0xFF) before the check for the limit and all valid IDs were
  657. * added */
  658. for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
  659. sector += sectorsize) {
  660. /* Read a block */
  661. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  662. if (!bh)
  663. break;
  664. /* Look for ISO descriptors */
  665. vsd = (struct volStructDesc *)(bh->b_data +
  666. (sector & (sb->s_blocksize - 1)));
  667. if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
  668. VSD_STD_ID_LEN)) {
  669. switch (vsd->structType) {
  670. case 0:
  671. udf_debug("ISO9660 Boot Record found\n");
  672. break;
  673. case 1:
  674. udf_debug("ISO9660 Primary Volume Descriptor found\n");
  675. break;
  676. case 2:
  677. udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
  678. break;
  679. case 3:
  680. udf_debug("ISO9660 Volume Partition Descriptor found\n");
  681. break;
  682. case 255:
  683. udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
  684. break;
  685. default:
  686. udf_debug("ISO9660 VRS (%u) found\n",
  687. vsd->structType);
  688. break;
  689. }
  690. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  691. VSD_STD_ID_LEN))
  692. ; /* nothing */
  693. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  694. VSD_STD_ID_LEN)) {
  695. brelse(bh);
  696. break;
  697. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  698. VSD_STD_ID_LEN))
  699. nsr02 = sector;
  700. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  701. VSD_STD_ID_LEN))
  702. nsr03 = sector;
  703. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
  704. VSD_STD_ID_LEN))
  705. ; /* nothing */
  706. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
  707. VSD_STD_ID_LEN))
  708. ; /* nothing */
  709. else {
  710. /* invalid id : end of volume recognition area */
  711. brelse(bh);
  712. break;
  713. }
  714. brelse(bh);
  715. }
  716. if (nsr03)
  717. return nsr03;
  718. else if (nsr02)
  719. return nsr02;
  720. else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
  721. VSD_FIRST_SECTOR_OFFSET)
  722. return -1;
  723. else
  724. return 0;
  725. }
  726. static int udf_find_fileset(struct super_block *sb,
  727. struct kernel_lb_addr *fileset,
  728. struct kernel_lb_addr *root)
  729. {
  730. struct buffer_head *bh = NULL;
  731. long lastblock;
  732. uint16_t ident;
  733. struct udf_sb_info *sbi;
  734. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  735. fileset->partitionReferenceNum != 0xFFFF) {
  736. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  737. if (!bh) {
  738. return 1;
  739. } else if (ident != TAG_IDENT_FSD) {
  740. brelse(bh);
  741. return 1;
  742. }
  743. }
  744. sbi = UDF_SB(sb);
  745. if (!bh) {
  746. /* Search backwards through the partitions */
  747. struct kernel_lb_addr newfileset;
  748. /* --> cvg: FIXME - is it reasonable? */
  749. return 1;
  750. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  751. (newfileset.partitionReferenceNum != 0xFFFF &&
  752. fileset->logicalBlockNum == 0xFFFFFFFF &&
  753. fileset->partitionReferenceNum == 0xFFFF);
  754. newfileset.partitionReferenceNum--) {
  755. lastblock = sbi->s_partmaps
  756. [newfileset.partitionReferenceNum]
  757. .s_partition_len;
  758. newfileset.logicalBlockNum = 0;
  759. do {
  760. bh = udf_read_ptagged(sb, &newfileset, 0,
  761. &ident);
  762. if (!bh) {
  763. newfileset.logicalBlockNum++;
  764. continue;
  765. }
  766. switch (ident) {
  767. case TAG_IDENT_SBD:
  768. {
  769. struct spaceBitmapDesc *sp;
  770. sp = (struct spaceBitmapDesc *)
  771. bh->b_data;
  772. newfileset.logicalBlockNum += 1 +
  773. ((le32_to_cpu(sp->numOfBytes) +
  774. sizeof(struct spaceBitmapDesc)
  775. - 1) >> sb->s_blocksize_bits);
  776. brelse(bh);
  777. break;
  778. }
  779. case TAG_IDENT_FSD:
  780. *fileset = newfileset;
  781. break;
  782. default:
  783. newfileset.logicalBlockNum++;
  784. brelse(bh);
  785. bh = NULL;
  786. break;
  787. }
  788. } while (newfileset.logicalBlockNum < lastblock &&
  789. fileset->logicalBlockNum == 0xFFFFFFFF &&
  790. fileset->partitionReferenceNum == 0xFFFF);
  791. }
  792. }
  793. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  794. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  795. udf_debug("Fileset at block=%d, partition=%d\n",
  796. fileset->logicalBlockNum,
  797. fileset->partitionReferenceNum);
  798. sbi->s_partition = fileset->partitionReferenceNum;
  799. udf_load_fileset(sb, bh, root);
  800. brelse(bh);
  801. return 0;
  802. }
  803. return 1;
  804. }
  805. /*
  806. * Load primary Volume Descriptor Sequence
  807. *
  808. * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
  809. * should be tried.
  810. */
  811. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  812. {
  813. struct primaryVolDesc *pvoldesc;
  814. struct ustr *instr, *outstr;
  815. struct buffer_head *bh;
  816. uint16_t ident;
  817. int ret = -ENOMEM;
  818. instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  819. if (!instr)
  820. return -ENOMEM;
  821. outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  822. if (!outstr)
  823. goto out1;
  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. if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
  844. if (udf_CS0toUTF8(outstr, instr)) {
  845. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  846. outstr->u_len > 31 ? 31 : outstr->u_len);
  847. udf_debug("volIdent[] = '%s'\n",
  848. UDF_SB(sb)->s_volume_ident);
  849. }
  850. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  851. if (udf_CS0toUTF8(outstr, instr))
  852. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  853. ret = 0;
  854. out_bh:
  855. brelse(bh);
  856. out2:
  857. kfree(outstr);
  858. out1:
  859. kfree(instr);
  860. return ret;
  861. }
  862. struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
  863. u32 meta_file_loc, u32 partition_num)
  864. {
  865. struct kernel_lb_addr addr;
  866. struct inode *metadata_fe;
  867. addr.logicalBlockNum = meta_file_loc;
  868. addr.partitionReferenceNum = partition_num;
  869. metadata_fe = udf_iget_special(sb, &addr);
  870. if (IS_ERR(metadata_fe)) {
  871. udf_warn(sb, "metadata inode efe not found\n");
  872. return metadata_fe;
  873. }
  874. if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
  875. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  876. iput(metadata_fe);
  877. return ERR_PTR(-EIO);
  878. }
  879. return metadata_fe;
  880. }
  881. static int udf_load_metadata_files(struct super_block *sb, int partition)
  882. {
  883. struct udf_sb_info *sbi = UDF_SB(sb);
  884. struct udf_part_map *map;
  885. struct udf_meta_data *mdata;
  886. struct kernel_lb_addr addr;
  887. struct inode *fe;
  888. map = &sbi->s_partmaps[partition];
  889. mdata = &map->s_type_specific.s_metadata;
  890. /* metadata address */
  891. udf_debug("Metadata file location: block = %d part = %d\n",
  892. mdata->s_meta_file_loc, map->s_partition_num);
  893. fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
  894. map->s_partition_num);
  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, map->s_partition_num);
  899. fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
  900. map->s_partition_num);
  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 = map->s_partition_num;
  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, SPARABLE and METADATA 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);
  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. * udf_load_logicalvolint
  1392. *
  1393. */
  1394. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1395. {
  1396. struct buffer_head *bh = NULL;
  1397. uint16_t ident;
  1398. struct udf_sb_info *sbi = UDF_SB(sb);
  1399. struct logicalVolIntegrityDesc *lvid;
  1400. while (loc.extLength > 0 &&
  1401. (bh = udf_read_tagged(sb, loc.extLocation,
  1402. loc.extLocation, &ident)) &&
  1403. ident == TAG_IDENT_LVID) {
  1404. sbi->s_lvid_bh = bh;
  1405. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1406. if (lvid->nextIntegrityExt.extLength)
  1407. udf_load_logicalvolint(sb,
  1408. leea_to_cpu(lvid->nextIntegrityExt));
  1409. if (sbi->s_lvid_bh != bh)
  1410. brelse(bh);
  1411. loc.extLength -= sb->s_blocksize;
  1412. loc.extLocation++;
  1413. }
  1414. if (sbi->s_lvid_bh != bh)
  1415. brelse(bh);
  1416. }
  1417. /*
  1418. * Process a main/reserve volume descriptor sequence.
  1419. * @block First block of first extent of the sequence.
  1420. * @lastblock Lastblock of first extent of the sequence.
  1421. * @fileset There we store extent containing root fileset
  1422. *
  1423. * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
  1424. * sequence
  1425. */
  1426. static noinline int udf_process_sequence(
  1427. struct super_block *sb,
  1428. sector_t block, sector_t lastblock,
  1429. struct kernel_lb_addr *fileset)
  1430. {
  1431. struct buffer_head *bh = NULL;
  1432. struct udf_vds_record vds[VDS_POS_LENGTH];
  1433. struct udf_vds_record *curr;
  1434. struct generic_desc *gd;
  1435. struct volDescPtr *vdp;
  1436. bool done = false;
  1437. uint32_t vdsn;
  1438. uint16_t ident;
  1439. long next_s = 0, next_e = 0;
  1440. int ret;
  1441. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1442. /*
  1443. * Read the main descriptor sequence and find which descriptors
  1444. * are in it.
  1445. */
  1446. for (; (!done && block <= lastblock); block++) {
  1447. bh = udf_read_tagged(sb, block, block, &ident);
  1448. if (!bh) {
  1449. udf_err(sb,
  1450. "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
  1451. (unsigned long long)block);
  1452. return -EAGAIN;
  1453. }
  1454. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1455. gd = (struct generic_desc *)bh->b_data;
  1456. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1457. switch (ident) {
  1458. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1459. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1460. if (vdsn >= curr->volDescSeqNum) {
  1461. curr->volDescSeqNum = vdsn;
  1462. curr->block = block;
  1463. }
  1464. break;
  1465. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1466. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1467. if (vdsn >= curr->volDescSeqNum) {
  1468. curr->volDescSeqNum = vdsn;
  1469. curr->block = block;
  1470. vdp = (struct volDescPtr *)bh->b_data;
  1471. next_s = le32_to_cpu(
  1472. vdp->nextVolDescSeqExt.extLocation);
  1473. next_e = le32_to_cpu(
  1474. vdp->nextVolDescSeqExt.extLength);
  1475. next_e = next_e >> sb->s_blocksize_bits;
  1476. next_e += next_s;
  1477. }
  1478. break;
  1479. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1480. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1481. if (vdsn >= curr->volDescSeqNum) {
  1482. curr->volDescSeqNum = vdsn;
  1483. curr->block = block;
  1484. }
  1485. break;
  1486. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1487. curr = &vds[VDS_POS_PARTITION_DESC];
  1488. if (!curr->block)
  1489. curr->block = block;
  1490. break;
  1491. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1492. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1493. if (vdsn >= curr->volDescSeqNum) {
  1494. curr->volDescSeqNum = vdsn;
  1495. curr->block = block;
  1496. }
  1497. break;
  1498. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1499. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1500. if (vdsn >= curr->volDescSeqNum) {
  1501. curr->volDescSeqNum = vdsn;
  1502. curr->block = block;
  1503. }
  1504. break;
  1505. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1506. vds[VDS_POS_TERMINATING_DESC].block = block;
  1507. if (next_e) {
  1508. block = next_s;
  1509. lastblock = next_e;
  1510. next_s = next_e = 0;
  1511. } else
  1512. done = true;
  1513. break;
  1514. }
  1515. brelse(bh);
  1516. }
  1517. /*
  1518. * Now read interesting descriptors again and process them
  1519. * in a suitable order
  1520. */
  1521. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1522. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1523. return -EAGAIN;
  1524. }
  1525. ret = udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block);
  1526. if (ret < 0)
  1527. return ret;
  1528. if (vds[VDS_POS_LOGICAL_VOL_DESC].block) {
  1529. ret = udf_load_logicalvol(sb,
  1530. vds[VDS_POS_LOGICAL_VOL_DESC].block,
  1531. fileset);
  1532. if (ret < 0)
  1533. return ret;
  1534. }
  1535. if (vds[VDS_POS_PARTITION_DESC].block) {
  1536. /*
  1537. * We rescan the whole descriptor sequence to find
  1538. * partition descriptor blocks and process them.
  1539. */
  1540. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1541. block < vds[VDS_POS_TERMINATING_DESC].block;
  1542. block++) {
  1543. ret = udf_load_partdesc(sb, block);
  1544. if (ret < 0)
  1545. return ret;
  1546. }
  1547. }
  1548. return 0;
  1549. }
  1550. /*
  1551. * Load Volume Descriptor Sequence described by anchor in bh
  1552. *
  1553. * Returns <0 on error, 0 on success
  1554. */
  1555. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1556. struct kernel_lb_addr *fileset)
  1557. {
  1558. struct anchorVolDescPtr *anchor;
  1559. sector_t main_s, main_e, reserve_s, reserve_e;
  1560. int ret;
  1561. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1562. /* Locate the main sequence */
  1563. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1564. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1565. main_e = main_e >> sb->s_blocksize_bits;
  1566. main_e += main_s;
  1567. /* Locate the reserve sequence */
  1568. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1569. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1570. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1571. reserve_e += reserve_s;
  1572. /* Process the main & reserve sequences */
  1573. /* responsible for finding the PartitionDesc(s) */
  1574. ret = udf_process_sequence(sb, main_s, main_e, fileset);
  1575. if (ret != -EAGAIN)
  1576. return ret;
  1577. udf_sb_free_partitions(sb);
  1578. ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1579. if (ret < 0) {
  1580. udf_sb_free_partitions(sb);
  1581. /* No sequence was OK, return -EIO */
  1582. if (ret == -EAGAIN)
  1583. ret = -EIO;
  1584. }
  1585. return ret;
  1586. }
  1587. /*
  1588. * Check whether there is an anchor block in the given block and
  1589. * load Volume Descriptor Sequence if so.
  1590. *
  1591. * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
  1592. * block
  1593. */
  1594. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1595. struct kernel_lb_addr *fileset)
  1596. {
  1597. struct buffer_head *bh;
  1598. uint16_t ident;
  1599. int ret;
  1600. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1601. udf_fixed_to_variable(block) >=
  1602. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1603. return -EAGAIN;
  1604. bh = udf_read_tagged(sb, block, block, &ident);
  1605. if (!bh)
  1606. return -EAGAIN;
  1607. if (ident != TAG_IDENT_AVDP) {
  1608. brelse(bh);
  1609. return -EAGAIN;
  1610. }
  1611. ret = udf_load_sequence(sb, bh, fileset);
  1612. brelse(bh);
  1613. return ret;
  1614. }
  1615. /*
  1616. * Search for an anchor volume descriptor pointer.
  1617. *
  1618. * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
  1619. * of anchors.
  1620. */
  1621. static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
  1622. struct kernel_lb_addr *fileset)
  1623. {
  1624. sector_t last[6];
  1625. int i;
  1626. struct udf_sb_info *sbi = UDF_SB(sb);
  1627. int last_count = 0;
  1628. int ret;
  1629. /* First try user provided anchor */
  1630. if (sbi->s_anchor) {
  1631. ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
  1632. if (ret != -EAGAIN)
  1633. return ret;
  1634. }
  1635. /*
  1636. * according to spec, anchor is in either:
  1637. * block 256
  1638. * lastblock-256
  1639. * lastblock
  1640. * however, if the disc isn't closed, it could be 512.
  1641. */
  1642. ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
  1643. if (ret != -EAGAIN)
  1644. return ret;
  1645. /*
  1646. * The trouble is which block is the last one. Drives often misreport
  1647. * this so we try various possibilities.
  1648. */
  1649. last[last_count++] = *lastblock;
  1650. if (*lastblock >= 1)
  1651. last[last_count++] = *lastblock - 1;
  1652. last[last_count++] = *lastblock + 1;
  1653. if (*lastblock >= 2)
  1654. last[last_count++] = *lastblock - 2;
  1655. if (*lastblock >= 150)
  1656. last[last_count++] = *lastblock - 150;
  1657. if (*lastblock >= 152)
  1658. last[last_count++] = *lastblock - 152;
  1659. for (i = 0; i < last_count; i++) {
  1660. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1661. sb->s_blocksize_bits)
  1662. continue;
  1663. ret = udf_check_anchor_block(sb, last[i], fileset);
  1664. if (ret != -EAGAIN) {
  1665. if (!ret)
  1666. *lastblock = last[i];
  1667. return ret;
  1668. }
  1669. if (last[i] < 256)
  1670. continue;
  1671. ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
  1672. if (ret != -EAGAIN) {
  1673. if (!ret)
  1674. *lastblock = last[i];
  1675. return ret;
  1676. }
  1677. }
  1678. /* Finally try block 512 in case media is open */
  1679. return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
  1680. }
  1681. /*
  1682. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1683. * area specified by it. The function expects sbi->s_lastblock to be the last
  1684. * block on the media.
  1685. *
  1686. * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
  1687. * was not found.
  1688. */
  1689. static int udf_find_anchor(struct super_block *sb,
  1690. struct kernel_lb_addr *fileset)
  1691. {
  1692. struct udf_sb_info *sbi = UDF_SB(sb);
  1693. sector_t lastblock = sbi->s_last_block;
  1694. int ret;
  1695. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1696. if (ret != -EAGAIN)
  1697. goto out;
  1698. /* No anchor found? Try VARCONV conversion of block numbers */
  1699. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1700. lastblock = udf_variable_to_fixed(sbi->s_last_block);
  1701. /* Firstly, we try to not convert number of the last block */
  1702. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1703. if (ret != -EAGAIN)
  1704. goto out;
  1705. lastblock = sbi->s_last_block;
  1706. /* Secondly, we try with converted number of the last block */
  1707. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1708. if (ret < 0) {
  1709. /* VARCONV didn't help. Clear it. */
  1710. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1711. }
  1712. out:
  1713. if (ret == 0)
  1714. sbi->s_last_block = lastblock;
  1715. return ret;
  1716. }
  1717. /*
  1718. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1719. * Descriptor Sequence.
  1720. *
  1721. * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
  1722. * block was not found.
  1723. */
  1724. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1725. int silent, struct kernel_lb_addr *fileset)
  1726. {
  1727. struct udf_sb_info *sbi = UDF_SB(sb);
  1728. loff_t nsr_off;
  1729. int ret;
  1730. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1731. if (!silent)
  1732. udf_warn(sb, "Bad block size\n");
  1733. return -EINVAL;
  1734. }
  1735. sbi->s_last_block = uopt->lastblock;
  1736. if (!uopt->novrs) {
  1737. /* Check that it is NSR02 compliant */
  1738. nsr_off = udf_check_vsd(sb);
  1739. if (!nsr_off) {
  1740. if (!silent)
  1741. udf_warn(sb, "No VRS found\n");
  1742. return 0;
  1743. }
  1744. if (nsr_off == -1)
  1745. udf_debug("Failed to read sector at offset %d. "
  1746. "Assuming open disc. Skipping validity "
  1747. "check\n", VSD_FIRST_SECTOR_OFFSET);
  1748. if (!sbi->s_last_block)
  1749. sbi->s_last_block = udf_get_last_block(sb);
  1750. } else {
  1751. udf_debug("Validity check skipped because of novrs option\n");
  1752. }
  1753. /* Look for anchor block and load Volume Descriptor Sequence */
  1754. sbi->s_anchor = uopt->anchor;
  1755. ret = udf_find_anchor(sb, fileset);
  1756. if (ret < 0) {
  1757. if (!silent && ret == -EAGAIN)
  1758. udf_warn(sb, "No anchor found\n");
  1759. return ret;
  1760. }
  1761. return 0;
  1762. }
  1763. static void udf_open_lvid(struct super_block *sb)
  1764. {
  1765. struct udf_sb_info *sbi = UDF_SB(sb);
  1766. struct buffer_head *bh = sbi->s_lvid_bh;
  1767. struct logicalVolIntegrityDesc *lvid;
  1768. struct logicalVolIntegrityDescImpUse *lvidiu;
  1769. if (!bh)
  1770. return;
  1771. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1772. lvidiu = udf_sb_lvidiu(sb);
  1773. if (!lvidiu)
  1774. return;
  1775. mutex_lock(&sbi->s_alloc_mutex);
  1776. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1777. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1778. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1779. CURRENT_TIME);
  1780. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1781. lvid->descTag.descCRC = cpu_to_le16(
  1782. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1783. le16_to_cpu(lvid->descTag.descCRCLength)));
  1784. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1785. mark_buffer_dirty(bh);
  1786. sbi->s_lvid_dirty = 0;
  1787. mutex_unlock(&sbi->s_alloc_mutex);
  1788. /* Make opening of filesystem visible on the media immediately */
  1789. sync_dirty_buffer(bh);
  1790. }
  1791. static void udf_close_lvid(struct super_block *sb)
  1792. {
  1793. struct udf_sb_info *sbi = UDF_SB(sb);
  1794. struct buffer_head *bh = sbi->s_lvid_bh;
  1795. struct logicalVolIntegrityDesc *lvid;
  1796. struct logicalVolIntegrityDescImpUse *lvidiu;
  1797. if (!bh)
  1798. return;
  1799. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1800. lvidiu = udf_sb_lvidiu(sb);
  1801. if (!lvidiu)
  1802. return;
  1803. mutex_lock(&sbi->s_alloc_mutex);
  1804. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1805. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1806. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1807. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1808. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1809. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1810. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1811. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1812. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1813. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1814. lvid->descTag.descCRC = cpu_to_le16(
  1815. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1816. le16_to_cpu(lvid->descTag.descCRCLength)));
  1817. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1818. /*
  1819. * We set buffer uptodate unconditionally here to avoid spurious
  1820. * warnings from mark_buffer_dirty() when previous EIO has marked
  1821. * the buffer as !uptodate
  1822. */
  1823. set_buffer_uptodate(bh);
  1824. mark_buffer_dirty(bh);
  1825. sbi->s_lvid_dirty = 0;
  1826. mutex_unlock(&sbi->s_alloc_mutex);
  1827. /* Make closing of filesystem visible on the media immediately */
  1828. sync_dirty_buffer(bh);
  1829. }
  1830. u64 lvid_get_unique_id(struct super_block *sb)
  1831. {
  1832. struct buffer_head *bh;
  1833. struct udf_sb_info *sbi = UDF_SB(sb);
  1834. struct logicalVolIntegrityDesc *lvid;
  1835. struct logicalVolHeaderDesc *lvhd;
  1836. u64 uniqueID;
  1837. u64 ret;
  1838. bh = sbi->s_lvid_bh;
  1839. if (!bh)
  1840. return 0;
  1841. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1842. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1843. mutex_lock(&sbi->s_alloc_mutex);
  1844. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1845. if (!(++uniqueID & 0xFFFFFFFF))
  1846. uniqueID += 16;
  1847. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1848. mutex_unlock(&sbi->s_alloc_mutex);
  1849. mark_buffer_dirty(bh);
  1850. return ret;
  1851. }
  1852. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1853. {
  1854. int ret = -EINVAL;
  1855. struct inode *inode = NULL;
  1856. struct udf_options uopt;
  1857. struct kernel_lb_addr rootdir, fileset;
  1858. struct udf_sb_info *sbi;
  1859. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1860. uopt.uid = INVALID_UID;
  1861. uopt.gid = INVALID_GID;
  1862. uopt.umask = 0;
  1863. uopt.fmode = UDF_INVALID_MODE;
  1864. uopt.dmode = UDF_INVALID_MODE;
  1865. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1866. if (!sbi)
  1867. return -ENOMEM;
  1868. sb->s_fs_info = sbi;
  1869. mutex_init(&sbi->s_alloc_mutex);
  1870. if (!udf_parse_options((char *)options, &uopt, false))
  1871. goto parse_options_failure;
  1872. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1873. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1874. udf_err(sb, "utf8 cannot be combined with iocharset\n");
  1875. goto parse_options_failure;
  1876. }
  1877. #ifdef CONFIG_UDF_NLS
  1878. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1879. uopt.nls_map = load_nls_default();
  1880. if (!uopt.nls_map)
  1881. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1882. else
  1883. udf_debug("Using default NLS map\n");
  1884. }
  1885. #endif
  1886. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1887. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1888. fileset.logicalBlockNum = 0xFFFFFFFF;
  1889. fileset.partitionReferenceNum = 0xFFFF;
  1890. sbi->s_flags = uopt.flags;
  1891. sbi->s_uid = uopt.uid;
  1892. sbi->s_gid = uopt.gid;
  1893. sbi->s_umask = uopt.umask;
  1894. sbi->s_fmode = uopt.fmode;
  1895. sbi->s_dmode = uopt.dmode;
  1896. sbi->s_nls_map = uopt.nls_map;
  1897. rwlock_init(&sbi->s_cred_lock);
  1898. if (uopt.session == 0xFFFFFFFF)
  1899. sbi->s_session = udf_get_last_session(sb);
  1900. else
  1901. sbi->s_session = uopt.session;
  1902. udf_debug("Multi-session=%d\n", sbi->s_session);
  1903. /* Fill in the rest of the superblock */
  1904. sb->s_op = &udf_sb_ops;
  1905. sb->s_export_op = &udf_export_ops;
  1906. sb->s_magic = UDF_SUPER_MAGIC;
  1907. sb->s_time_gran = 1000;
  1908. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1909. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1910. } else {
  1911. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1912. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1913. if (ret == -EAGAIN && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1914. if (!silent)
  1915. pr_notice("Rescanning with blocksize %d\n",
  1916. UDF_DEFAULT_BLOCKSIZE);
  1917. brelse(sbi->s_lvid_bh);
  1918. sbi->s_lvid_bh = NULL;
  1919. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1920. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1921. }
  1922. }
  1923. if (ret < 0) {
  1924. if (ret == -EAGAIN) {
  1925. udf_warn(sb, "No partition found (1)\n");
  1926. ret = -EINVAL;
  1927. }
  1928. goto error_out;
  1929. }
  1930. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1931. if (sbi->s_lvid_bh) {
  1932. struct logicalVolIntegrityDescImpUse *lvidiu =
  1933. udf_sb_lvidiu(sb);
  1934. uint16_t minUDFReadRev;
  1935. uint16_t minUDFWriteRev;
  1936. if (!lvidiu) {
  1937. ret = -EINVAL;
  1938. goto error_out;
  1939. }
  1940. minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1941. minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1942. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1943. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1944. minUDFReadRev,
  1945. UDF_MAX_READ_VERSION);
  1946. ret = -EINVAL;
  1947. goto error_out;
  1948. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
  1949. !(sb->s_flags & MS_RDONLY)) {
  1950. ret = -EACCES;
  1951. goto error_out;
  1952. }
  1953. sbi->s_udfrev = minUDFWriteRev;
  1954. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1955. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1956. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1957. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1958. }
  1959. if (!sbi->s_partitions) {
  1960. udf_warn(sb, "No partition found (2)\n");
  1961. ret = -EINVAL;
  1962. goto error_out;
  1963. }
  1964. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1965. UDF_PART_FLAG_READ_ONLY &&
  1966. !(sb->s_flags & MS_RDONLY)) {
  1967. ret = -EACCES;
  1968. goto error_out;
  1969. }
  1970. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1971. udf_warn(sb, "No fileset found\n");
  1972. ret = -EINVAL;
  1973. goto error_out;
  1974. }
  1975. if (!silent) {
  1976. struct timestamp ts;
  1977. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1978. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1979. sbi->s_volume_ident,
  1980. le16_to_cpu(ts.year), ts.month, ts.day,
  1981. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1982. }
  1983. if (!(sb->s_flags & MS_RDONLY))
  1984. udf_open_lvid(sb);
  1985. /* Assign the root inode */
  1986. /* assign inodes by physical block number */
  1987. /* perhaps it's not extensible enough, but for now ... */
  1988. inode = udf_iget(sb, &rootdir);
  1989. if (IS_ERR(inode)) {
  1990. udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
  1991. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1992. ret = PTR_ERR(inode);
  1993. goto error_out;
  1994. }
  1995. /* Allocate a dentry for the root inode */
  1996. sb->s_root = d_make_root(inode);
  1997. if (!sb->s_root) {
  1998. udf_err(sb, "Couldn't allocate root dentry\n");
  1999. ret = -ENOMEM;
  2000. goto error_out;
  2001. }
  2002. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2003. sb->s_max_links = UDF_MAX_LINKS;
  2004. return 0;
  2005. error_out:
  2006. iput(sbi->s_vat_inode);
  2007. parse_options_failure:
  2008. #ifdef CONFIG_UDF_NLS
  2009. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  2010. unload_nls(sbi->s_nls_map);
  2011. #endif
  2012. if (!(sb->s_flags & MS_RDONLY))
  2013. udf_close_lvid(sb);
  2014. brelse(sbi->s_lvid_bh);
  2015. udf_sb_free_partitions(sb);
  2016. kfree(sbi);
  2017. sb->s_fs_info = NULL;
  2018. return ret;
  2019. }
  2020. void _udf_err(struct super_block *sb, const char *function,
  2021. const char *fmt, ...)
  2022. {
  2023. struct va_format vaf;
  2024. va_list args;
  2025. va_start(args, fmt);
  2026. vaf.fmt = fmt;
  2027. vaf.va = &args;
  2028. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  2029. va_end(args);
  2030. }
  2031. void _udf_warn(struct super_block *sb, const char *function,
  2032. const char *fmt, ...)
  2033. {
  2034. struct va_format vaf;
  2035. va_list args;
  2036. va_start(args, fmt);
  2037. vaf.fmt = fmt;
  2038. vaf.va = &args;
  2039. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  2040. va_end(args);
  2041. }
  2042. static void udf_put_super(struct super_block *sb)
  2043. {
  2044. struct udf_sb_info *sbi;
  2045. sbi = UDF_SB(sb);
  2046. iput(sbi->s_vat_inode);
  2047. #ifdef CONFIG_UDF_NLS
  2048. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  2049. unload_nls(sbi->s_nls_map);
  2050. #endif
  2051. if (!(sb->s_flags & MS_RDONLY))
  2052. udf_close_lvid(sb);
  2053. brelse(sbi->s_lvid_bh);
  2054. udf_sb_free_partitions(sb);
  2055. mutex_destroy(&sbi->s_alloc_mutex);
  2056. kfree(sb->s_fs_info);
  2057. sb->s_fs_info = NULL;
  2058. }
  2059. static int udf_sync_fs(struct super_block *sb, int wait)
  2060. {
  2061. struct udf_sb_info *sbi = UDF_SB(sb);
  2062. mutex_lock(&sbi->s_alloc_mutex);
  2063. if (sbi->s_lvid_dirty) {
  2064. /*
  2065. * Blockdevice will be synced later so we don't have to submit
  2066. * the buffer for IO
  2067. */
  2068. mark_buffer_dirty(sbi->s_lvid_bh);
  2069. sbi->s_lvid_dirty = 0;
  2070. }
  2071. mutex_unlock(&sbi->s_alloc_mutex);
  2072. return 0;
  2073. }
  2074. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  2075. {
  2076. struct super_block *sb = dentry->d_sb;
  2077. struct udf_sb_info *sbi = UDF_SB(sb);
  2078. struct logicalVolIntegrityDescImpUse *lvidiu;
  2079. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  2080. lvidiu = udf_sb_lvidiu(sb);
  2081. buf->f_type = UDF_SUPER_MAGIC;
  2082. buf->f_bsize = sb->s_blocksize;
  2083. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  2084. buf->f_bfree = udf_count_free(sb);
  2085. buf->f_bavail = buf->f_bfree;
  2086. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  2087. le32_to_cpu(lvidiu->numDirs)) : 0)
  2088. + buf->f_bfree;
  2089. buf->f_ffree = buf->f_bfree;
  2090. buf->f_namelen = UDF_NAME_LEN - 2;
  2091. buf->f_fsid.val[0] = (u32)id;
  2092. buf->f_fsid.val[1] = (u32)(id >> 32);
  2093. return 0;
  2094. }
  2095. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  2096. struct udf_bitmap *bitmap)
  2097. {
  2098. struct buffer_head *bh = NULL;
  2099. unsigned int accum = 0;
  2100. int index;
  2101. int block = 0, newblock;
  2102. struct kernel_lb_addr loc;
  2103. uint32_t bytes;
  2104. uint8_t *ptr;
  2105. uint16_t ident;
  2106. struct spaceBitmapDesc *bm;
  2107. loc.logicalBlockNum = bitmap->s_extPosition;
  2108. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  2109. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  2110. if (!bh) {
  2111. udf_err(sb, "udf_count_free failed\n");
  2112. goto out;
  2113. } else if (ident != TAG_IDENT_SBD) {
  2114. brelse(bh);
  2115. udf_err(sb, "udf_count_free failed\n");
  2116. goto out;
  2117. }
  2118. bm = (struct spaceBitmapDesc *)bh->b_data;
  2119. bytes = le32_to_cpu(bm->numOfBytes);
  2120. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  2121. ptr = (uint8_t *)bh->b_data;
  2122. while (bytes > 0) {
  2123. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  2124. accum += bitmap_weight((const unsigned long *)(ptr + index),
  2125. cur_bytes * 8);
  2126. bytes -= cur_bytes;
  2127. if (bytes) {
  2128. brelse(bh);
  2129. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  2130. bh = udf_tread(sb, newblock);
  2131. if (!bh) {
  2132. udf_debug("read failed\n");
  2133. goto out;
  2134. }
  2135. index = 0;
  2136. ptr = (uint8_t *)bh->b_data;
  2137. }
  2138. }
  2139. brelse(bh);
  2140. out:
  2141. return accum;
  2142. }
  2143. static unsigned int udf_count_free_table(struct super_block *sb,
  2144. struct inode *table)
  2145. {
  2146. unsigned int accum = 0;
  2147. uint32_t elen;
  2148. struct kernel_lb_addr eloc;
  2149. int8_t etype;
  2150. struct extent_position epos;
  2151. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2152. epos.block = UDF_I(table)->i_location;
  2153. epos.offset = sizeof(struct unallocSpaceEntry);
  2154. epos.bh = NULL;
  2155. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2156. accum += (elen >> table->i_sb->s_blocksize_bits);
  2157. brelse(epos.bh);
  2158. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2159. return accum;
  2160. }
  2161. static unsigned int udf_count_free(struct super_block *sb)
  2162. {
  2163. unsigned int accum = 0;
  2164. struct udf_sb_info *sbi;
  2165. struct udf_part_map *map;
  2166. sbi = UDF_SB(sb);
  2167. if (sbi->s_lvid_bh) {
  2168. struct logicalVolIntegrityDesc *lvid =
  2169. (struct logicalVolIntegrityDesc *)
  2170. sbi->s_lvid_bh->b_data;
  2171. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2172. accum = le32_to_cpu(
  2173. lvid->freeSpaceTable[sbi->s_partition]);
  2174. if (accum == 0xFFFFFFFF)
  2175. accum = 0;
  2176. }
  2177. }
  2178. if (accum)
  2179. return accum;
  2180. map = &sbi->s_partmaps[sbi->s_partition];
  2181. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2182. accum += udf_count_free_bitmap(sb,
  2183. map->s_uspace.s_bitmap);
  2184. }
  2185. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2186. accum += udf_count_free_bitmap(sb,
  2187. map->s_fspace.s_bitmap);
  2188. }
  2189. if (accum)
  2190. return accum;
  2191. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2192. accum += udf_count_free_table(sb,
  2193. map->s_uspace.s_table);
  2194. }
  2195. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2196. accum += udf_count_free_table(sb,
  2197. map->s_fspace.s_table);
  2198. }
  2199. return accum;
  2200. }