super.c 66 KB

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