super.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  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 <asm/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_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. if (!*p)
  456. continue;
  457. token = match_token(p, tokens, args);
  458. switch (token) {
  459. case Opt_novrs:
  460. uopt->novrs = 1;
  461. break;
  462. case Opt_bs:
  463. if (match_int(&args[0], &option))
  464. return 0;
  465. uopt->blocksize = option;
  466. uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
  467. break;
  468. case Opt_unhide:
  469. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  470. break;
  471. case Opt_undelete:
  472. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  473. break;
  474. case Opt_noadinicb:
  475. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  476. break;
  477. case Opt_adinicb:
  478. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  479. break;
  480. case Opt_shortad:
  481. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  482. break;
  483. case Opt_longad:
  484. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  485. break;
  486. case Opt_gid:
  487. if (match_int(args, &option))
  488. return 0;
  489. uopt->gid = make_kgid(current_user_ns(), option);
  490. if (!gid_valid(uopt->gid))
  491. return 0;
  492. uopt->flags |= (1 << UDF_FLAG_GID_SET);
  493. break;
  494. case Opt_uid:
  495. if (match_int(args, &option))
  496. return 0;
  497. uopt->uid = make_kuid(current_user_ns(), option);
  498. if (!uid_valid(uopt->uid))
  499. return 0;
  500. uopt->flags |= (1 << UDF_FLAG_UID_SET);
  501. break;
  502. case Opt_umask:
  503. if (match_octal(args, &option))
  504. return 0;
  505. uopt->umask = option;
  506. break;
  507. case Opt_nostrict:
  508. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  509. break;
  510. case Opt_session:
  511. if (match_int(args, &option))
  512. return 0;
  513. uopt->session = option;
  514. if (!remount)
  515. uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
  516. break;
  517. case Opt_lastblock:
  518. if (match_int(args, &option))
  519. return 0;
  520. uopt->lastblock = option;
  521. if (!remount)
  522. uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
  523. break;
  524. case Opt_anchor:
  525. if (match_int(args, &option))
  526. return 0;
  527. uopt->anchor = option;
  528. break;
  529. case Opt_volume:
  530. if (match_int(args, &option))
  531. return 0;
  532. uopt->volume = option;
  533. break;
  534. case Opt_partition:
  535. if (match_int(args, &option))
  536. return 0;
  537. uopt->partition = option;
  538. break;
  539. case Opt_fileset:
  540. if (match_int(args, &option))
  541. return 0;
  542. uopt->fileset = option;
  543. break;
  544. case Opt_rootdir:
  545. if (match_int(args, &option))
  546. return 0;
  547. uopt->rootdir = option;
  548. break;
  549. case Opt_utf8:
  550. uopt->flags |= (1 << UDF_FLAG_UTF8);
  551. break;
  552. #ifdef CONFIG_UDF_NLS
  553. case Opt_iocharset:
  554. uopt->nls_map = load_nls(args[0].from);
  555. uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
  556. break;
  557. #endif
  558. case Opt_uignore:
  559. uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
  560. break;
  561. case Opt_uforget:
  562. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  563. break;
  564. case Opt_gignore:
  565. uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
  566. break;
  567. case Opt_gforget:
  568. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  569. break;
  570. case Opt_fmode:
  571. if (match_octal(args, &option))
  572. return 0;
  573. uopt->fmode = option & 0777;
  574. break;
  575. case Opt_dmode:
  576. if (match_octal(args, &option))
  577. return 0;
  578. uopt->dmode = option & 0777;
  579. break;
  580. default:
  581. pr_err("bad mount option \"%s\" or missing value\n", p);
  582. return 0;
  583. }
  584. }
  585. return 1;
  586. }
  587. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  588. {
  589. struct udf_options uopt;
  590. struct udf_sb_info *sbi = UDF_SB(sb);
  591. int error = 0;
  592. struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(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. if (udf_CS0toUTF8(outstr, instr)) {
  840. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  841. outstr->u_len > 31 ? 31 : outstr->u_len);
  842. udf_debug("volIdent[] = '%s'\n",
  843. UDF_SB(sb)->s_volume_ident);
  844. }
  845. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  846. if (udf_CS0toUTF8(outstr, instr))
  847. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  848. ret = 0;
  849. out_bh:
  850. brelse(bh);
  851. out2:
  852. kfree(outstr);
  853. out1:
  854. kfree(instr);
  855. return ret;
  856. }
  857. struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
  858. u32 meta_file_loc, u32 partition_num)
  859. {
  860. struct kernel_lb_addr addr;
  861. struct inode *metadata_fe;
  862. addr.logicalBlockNum = meta_file_loc;
  863. addr.partitionReferenceNum = partition_num;
  864. metadata_fe = udf_iget(sb, &addr);
  865. if (metadata_fe == NULL)
  866. udf_warn(sb, "metadata inode efe not found\n");
  867. else if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
  868. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  869. iput(metadata_fe);
  870. metadata_fe = NULL;
  871. }
  872. return metadata_fe;
  873. }
  874. static int udf_load_metadata_files(struct super_block *sb, int partition)
  875. {
  876. struct udf_sb_info *sbi = UDF_SB(sb);
  877. struct udf_part_map *map;
  878. struct udf_meta_data *mdata;
  879. struct kernel_lb_addr addr;
  880. map = &sbi->s_partmaps[partition];
  881. mdata = &map->s_type_specific.s_metadata;
  882. /* metadata address */
  883. udf_debug("Metadata file location: block = %d part = %d\n",
  884. mdata->s_meta_file_loc, map->s_partition_num);
  885. mdata->s_metadata_fe = udf_find_metadata_inode_efe(sb,
  886. mdata->s_meta_file_loc, map->s_partition_num);
  887. if (mdata->s_metadata_fe == NULL) {
  888. /* mirror file entry */
  889. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  890. mdata->s_mirror_file_loc, map->s_partition_num);
  891. mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
  892. mdata->s_mirror_file_loc, map->s_partition_num);
  893. if (mdata->s_mirror_fe == NULL) {
  894. udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
  895. return -EIO;
  896. }
  897. }
  898. /*
  899. * bitmap file entry
  900. * Note:
  901. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  902. */
  903. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  904. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  905. addr.partitionReferenceNum = map->s_partition_num;
  906. udf_debug("Bitmap file location: block = %d part = %d\n",
  907. addr.logicalBlockNum, addr.partitionReferenceNum);
  908. mdata->s_bitmap_fe = udf_iget(sb, &addr);
  909. if (mdata->s_bitmap_fe == NULL) {
  910. if (sb->s_flags & MS_RDONLY)
  911. udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
  912. else {
  913. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  914. return -EIO;
  915. }
  916. }
  917. }
  918. udf_debug("udf_load_metadata_files Ok\n");
  919. return 0;
  920. }
  921. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  922. struct kernel_lb_addr *root)
  923. {
  924. struct fileSetDesc *fset;
  925. fset = (struct fileSetDesc *)bh->b_data;
  926. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  927. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  928. udf_debug("Rootdir at block=%d, partition=%d\n",
  929. root->logicalBlockNum, root->partitionReferenceNum);
  930. }
  931. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  932. {
  933. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  934. return DIV_ROUND_UP(map->s_partition_len +
  935. (sizeof(struct spaceBitmapDesc) << 3),
  936. sb->s_blocksize * 8);
  937. }
  938. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  939. {
  940. struct udf_bitmap *bitmap;
  941. int nr_groups;
  942. int size;
  943. nr_groups = udf_compute_nr_groups(sb, index);
  944. size = sizeof(struct udf_bitmap) +
  945. (sizeof(struct buffer_head *) * nr_groups);
  946. if (size <= PAGE_SIZE)
  947. bitmap = kzalloc(size, GFP_KERNEL);
  948. else
  949. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  950. if (bitmap == NULL)
  951. return NULL;
  952. bitmap->s_nr_groups = nr_groups;
  953. return bitmap;
  954. }
  955. static int udf_fill_partdesc_info(struct super_block *sb,
  956. struct partitionDesc *p, int p_index)
  957. {
  958. struct udf_part_map *map;
  959. struct udf_sb_info *sbi = UDF_SB(sb);
  960. struct partitionHeaderDesc *phd;
  961. map = &sbi->s_partmaps[p_index];
  962. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  963. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  964. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  965. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  966. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  967. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  968. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  969. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  970. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  971. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  972. udf_debug("Partition (%d type %x) starts at physical %d, block length %d\n",
  973. p_index, map->s_partition_type,
  974. map->s_partition_root, map->s_partition_len);
  975. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  976. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  977. return 0;
  978. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  979. if (phd->unallocSpaceTable.extLength) {
  980. struct kernel_lb_addr loc = {
  981. .logicalBlockNum = le32_to_cpu(
  982. phd->unallocSpaceTable.extPosition),
  983. .partitionReferenceNum = p_index,
  984. };
  985. map->s_uspace.s_table = udf_iget(sb, &loc);
  986. if (!map->s_uspace.s_table) {
  987. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  988. p_index);
  989. return -EIO;
  990. }
  991. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  992. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  993. p_index, map->s_uspace.s_table->i_ino);
  994. }
  995. if (phd->unallocSpaceBitmap.extLength) {
  996. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  997. if (!bitmap)
  998. return -ENOMEM;
  999. map->s_uspace.s_bitmap = bitmap;
  1000. bitmap->s_extPosition = le32_to_cpu(
  1001. phd->unallocSpaceBitmap.extPosition);
  1002. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  1003. udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
  1004. p_index, bitmap->s_extPosition);
  1005. }
  1006. if (phd->partitionIntegrityTable.extLength)
  1007. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  1008. if (phd->freedSpaceTable.extLength) {
  1009. struct kernel_lb_addr loc = {
  1010. .logicalBlockNum = le32_to_cpu(
  1011. phd->freedSpaceTable.extPosition),
  1012. .partitionReferenceNum = p_index,
  1013. };
  1014. map->s_fspace.s_table = udf_iget(sb, &loc);
  1015. if (!map->s_fspace.s_table) {
  1016. udf_debug("cannot load freedSpaceTable (part %d)\n",
  1017. p_index);
  1018. return -EIO;
  1019. }
  1020. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  1021. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  1022. p_index, map->s_fspace.s_table->i_ino);
  1023. }
  1024. if (phd->freedSpaceBitmap.extLength) {
  1025. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  1026. if (!bitmap)
  1027. return -ENOMEM;
  1028. map->s_fspace.s_bitmap = bitmap;
  1029. bitmap->s_extPosition = le32_to_cpu(
  1030. phd->freedSpaceBitmap.extPosition);
  1031. map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
  1032. udf_debug("freedSpaceBitmap (part %d) @ %d\n",
  1033. p_index, bitmap->s_extPosition);
  1034. }
  1035. return 0;
  1036. }
  1037. static void udf_find_vat_block(struct super_block *sb, int p_index,
  1038. int type1_index, sector_t start_block)
  1039. {
  1040. struct udf_sb_info *sbi = UDF_SB(sb);
  1041. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1042. sector_t vat_block;
  1043. struct kernel_lb_addr ino;
  1044. /*
  1045. * VAT file entry is in the last recorded block. Some broken disks have
  1046. * it a few blocks before so try a bit harder...
  1047. */
  1048. ino.partitionReferenceNum = type1_index;
  1049. for (vat_block = start_block;
  1050. vat_block >= map->s_partition_root &&
  1051. vat_block >= start_block - 3 &&
  1052. !sbi->s_vat_inode; vat_block--) {
  1053. ino.logicalBlockNum = vat_block - map->s_partition_root;
  1054. sbi->s_vat_inode = udf_iget(sb, &ino);
  1055. }
  1056. }
  1057. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  1058. {
  1059. struct udf_sb_info *sbi = UDF_SB(sb);
  1060. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  1061. struct buffer_head *bh = NULL;
  1062. struct udf_inode_info *vati;
  1063. uint32_t pos;
  1064. struct virtualAllocationTable20 *vat20;
  1065. sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  1066. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  1067. if (!sbi->s_vat_inode &&
  1068. sbi->s_last_block != blocks - 1) {
  1069. pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
  1070. (unsigned long)sbi->s_last_block,
  1071. (unsigned long)blocks - 1);
  1072. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  1073. }
  1074. if (!sbi->s_vat_inode)
  1075. return -EIO;
  1076. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1077. map->s_type_specific.s_virtual.s_start_offset = 0;
  1078. map->s_type_specific.s_virtual.s_num_entries =
  1079. (sbi->s_vat_inode->i_size - 36) >> 2;
  1080. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1081. vati = UDF_I(sbi->s_vat_inode);
  1082. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1083. pos = udf_block_map(sbi->s_vat_inode, 0);
  1084. bh = sb_bread(sb, pos);
  1085. if (!bh)
  1086. return -EIO;
  1087. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1088. } else {
  1089. vat20 = (struct virtualAllocationTable20 *)
  1090. vati->i_ext.i_data;
  1091. }
  1092. map->s_type_specific.s_virtual.s_start_offset =
  1093. le16_to_cpu(vat20->lengthHeader);
  1094. map->s_type_specific.s_virtual.s_num_entries =
  1095. (sbi->s_vat_inode->i_size -
  1096. map->s_type_specific.s_virtual.
  1097. s_start_offset) >> 2;
  1098. brelse(bh);
  1099. }
  1100. return 0;
  1101. }
  1102. /*
  1103. * Load partition descriptor block
  1104. *
  1105. * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
  1106. * sequence.
  1107. */
  1108. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1109. {
  1110. struct buffer_head *bh;
  1111. struct partitionDesc *p;
  1112. struct udf_part_map *map;
  1113. struct udf_sb_info *sbi = UDF_SB(sb);
  1114. int i, type1_idx;
  1115. uint16_t partitionNumber;
  1116. uint16_t ident;
  1117. int ret;
  1118. bh = udf_read_tagged(sb, block, block, &ident);
  1119. if (!bh)
  1120. return -EAGAIN;
  1121. if (ident != TAG_IDENT_PD) {
  1122. ret = 0;
  1123. goto out_bh;
  1124. }
  1125. p = (struct partitionDesc *)bh->b_data;
  1126. partitionNumber = le16_to_cpu(p->partitionNumber);
  1127. /* First scan for TYPE1, SPARABLE and METADATA partitions */
  1128. for (i = 0; i < sbi->s_partitions; i++) {
  1129. map = &sbi->s_partmaps[i];
  1130. udf_debug("Searching map: (%d == %d)\n",
  1131. map->s_partition_num, partitionNumber);
  1132. if (map->s_partition_num == partitionNumber &&
  1133. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1134. map->s_partition_type == UDF_SPARABLE_MAP15))
  1135. break;
  1136. }
  1137. if (i >= sbi->s_partitions) {
  1138. udf_debug("Partition (%d) not found in partition map\n",
  1139. partitionNumber);
  1140. ret = 0;
  1141. goto out_bh;
  1142. }
  1143. ret = udf_fill_partdesc_info(sb, p, i);
  1144. if (ret < 0)
  1145. goto out_bh;
  1146. /*
  1147. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1148. * PHYSICAL partitions are already set up
  1149. */
  1150. type1_idx = i;
  1151. #ifdef UDFFS_DEBUG
  1152. map = NULL; /* supress 'maybe used uninitialized' warning */
  1153. #endif
  1154. for (i = 0; i < sbi->s_partitions; i++) {
  1155. map = &sbi->s_partmaps[i];
  1156. if (map->s_partition_num == partitionNumber &&
  1157. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1158. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1159. map->s_partition_type == UDF_METADATA_MAP25))
  1160. break;
  1161. }
  1162. if (i >= sbi->s_partitions) {
  1163. ret = 0;
  1164. goto out_bh;
  1165. }
  1166. ret = udf_fill_partdesc_info(sb, p, i);
  1167. if (ret < 0)
  1168. goto out_bh;
  1169. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1170. ret = udf_load_metadata_files(sb, i);
  1171. if (ret < 0) {
  1172. udf_err(sb, "error loading MetaData partition map %d\n",
  1173. i);
  1174. goto out_bh;
  1175. }
  1176. } else {
  1177. /*
  1178. * If we have a partition with virtual map, we don't handle
  1179. * writing to it (we overwrite blocks instead of relocating
  1180. * them).
  1181. */
  1182. if (!(sb->s_flags & MS_RDONLY)) {
  1183. ret = -EACCES;
  1184. goto out_bh;
  1185. }
  1186. ret = udf_load_vat(sb, i, type1_idx);
  1187. if (ret < 0)
  1188. goto out_bh;
  1189. }
  1190. ret = 0;
  1191. out_bh:
  1192. /* In case loading failed, we handle cleanup in udf_fill_super */
  1193. brelse(bh);
  1194. return ret;
  1195. }
  1196. static int udf_load_sparable_map(struct super_block *sb,
  1197. struct udf_part_map *map,
  1198. struct sparablePartitionMap *spm)
  1199. {
  1200. uint32_t loc;
  1201. uint16_t ident;
  1202. struct sparingTable *st;
  1203. struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
  1204. int i;
  1205. struct buffer_head *bh;
  1206. map->s_partition_type = UDF_SPARABLE_MAP15;
  1207. sdata->s_packet_len = le16_to_cpu(spm->packetLength);
  1208. if (!is_power_of_2(sdata->s_packet_len)) {
  1209. udf_err(sb, "error loading logical volume descriptor: "
  1210. "Invalid packet length %u\n",
  1211. (unsigned)sdata->s_packet_len);
  1212. return -EIO;
  1213. }
  1214. if (spm->numSparingTables > 4) {
  1215. udf_err(sb, "error loading logical volume descriptor: "
  1216. "Too many sparing tables (%d)\n",
  1217. (int)spm->numSparingTables);
  1218. return -EIO;
  1219. }
  1220. for (i = 0; i < spm->numSparingTables; i++) {
  1221. loc = le32_to_cpu(spm->locSparingTable[i]);
  1222. bh = udf_read_tagged(sb, loc, loc, &ident);
  1223. if (!bh)
  1224. continue;
  1225. st = (struct sparingTable *)bh->b_data;
  1226. if (ident != 0 ||
  1227. strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
  1228. strlen(UDF_ID_SPARING)) ||
  1229. sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
  1230. sb->s_blocksize) {
  1231. brelse(bh);
  1232. continue;
  1233. }
  1234. sdata->s_spar_map[i] = bh;
  1235. }
  1236. map->s_partition_func = udf_get_pblock_spar15;
  1237. return 0;
  1238. }
  1239. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1240. struct kernel_lb_addr *fileset)
  1241. {
  1242. struct logicalVolDesc *lvd;
  1243. int i, offset;
  1244. uint8_t type;
  1245. struct udf_sb_info *sbi = UDF_SB(sb);
  1246. struct genericPartitionMap *gpm;
  1247. uint16_t ident;
  1248. struct buffer_head *bh;
  1249. unsigned int table_len;
  1250. int ret;
  1251. bh = udf_read_tagged(sb, block, block, &ident);
  1252. if (!bh)
  1253. return -EAGAIN;
  1254. BUG_ON(ident != TAG_IDENT_LVD);
  1255. lvd = (struct logicalVolDesc *)bh->b_data;
  1256. table_len = le32_to_cpu(lvd->mapTableLength);
  1257. if (table_len > sb->s_blocksize - sizeof(*lvd)) {
  1258. udf_err(sb, "error loading logical volume descriptor: "
  1259. "Partition table too long (%u > %lu)\n", table_len,
  1260. sb->s_blocksize - sizeof(*lvd));
  1261. ret = -EIO;
  1262. goto out_bh;
  1263. }
  1264. ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1265. if (ret)
  1266. goto out_bh;
  1267. for (i = 0, offset = 0;
  1268. i < sbi->s_partitions && offset < table_len;
  1269. i++, offset += gpm->partitionMapLength) {
  1270. struct udf_part_map *map = &sbi->s_partmaps[i];
  1271. gpm = (struct genericPartitionMap *)
  1272. &(lvd->partitionMaps[offset]);
  1273. type = gpm->partitionMapType;
  1274. if (type == 1) {
  1275. struct genericPartitionMap1 *gpm1 =
  1276. (struct genericPartitionMap1 *)gpm;
  1277. map->s_partition_type = UDF_TYPE1_MAP15;
  1278. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1279. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1280. map->s_partition_func = NULL;
  1281. } else if (type == 2) {
  1282. struct udfPartitionMap2 *upm2 =
  1283. (struct udfPartitionMap2 *)gpm;
  1284. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1285. strlen(UDF_ID_VIRTUAL))) {
  1286. u16 suf =
  1287. le16_to_cpu(((__le16 *)upm2->partIdent.
  1288. identSuffix)[0]);
  1289. if (suf < 0x0200) {
  1290. map->s_partition_type =
  1291. UDF_VIRTUAL_MAP15;
  1292. map->s_partition_func =
  1293. udf_get_pblock_virt15;
  1294. } else {
  1295. map->s_partition_type =
  1296. UDF_VIRTUAL_MAP20;
  1297. map->s_partition_func =
  1298. udf_get_pblock_virt20;
  1299. }
  1300. } else if (!strncmp(upm2->partIdent.ident,
  1301. UDF_ID_SPARABLE,
  1302. strlen(UDF_ID_SPARABLE))) {
  1303. ret = udf_load_sparable_map(sb, map,
  1304. (struct sparablePartitionMap *)gpm);
  1305. if (ret < 0)
  1306. goto out_bh;
  1307. } else if (!strncmp(upm2->partIdent.ident,
  1308. UDF_ID_METADATA,
  1309. strlen(UDF_ID_METADATA))) {
  1310. struct udf_meta_data *mdata =
  1311. &map->s_type_specific.s_metadata;
  1312. struct metadataPartitionMap *mdm =
  1313. (struct metadataPartitionMap *)
  1314. &(lvd->partitionMaps[offset]);
  1315. udf_debug("Parsing Logical vol part %d type %d id=%s\n",
  1316. i, type, UDF_ID_METADATA);
  1317. map->s_partition_type = UDF_METADATA_MAP25;
  1318. map->s_partition_func = udf_get_pblock_meta25;
  1319. mdata->s_meta_file_loc =
  1320. le32_to_cpu(mdm->metadataFileLoc);
  1321. mdata->s_mirror_file_loc =
  1322. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1323. mdata->s_bitmap_file_loc =
  1324. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1325. mdata->s_alloc_unit_size =
  1326. le32_to_cpu(mdm->allocUnitSize);
  1327. mdata->s_align_unit_size =
  1328. le16_to_cpu(mdm->alignUnitSize);
  1329. if (mdm->flags & 0x01)
  1330. mdata->s_flags |= MF_DUPLICATE_MD;
  1331. udf_debug("Metadata Ident suffix=0x%x\n",
  1332. le16_to_cpu(*(__le16 *)
  1333. mdm->partIdent.identSuffix));
  1334. udf_debug("Metadata part num=%d\n",
  1335. le16_to_cpu(mdm->partitionNum));
  1336. udf_debug("Metadata part alloc unit size=%d\n",
  1337. le32_to_cpu(mdm->allocUnitSize));
  1338. udf_debug("Metadata file loc=%d\n",
  1339. le32_to_cpu(mdm->metadataFileLoc));
  1340. udf_debug("Mirror file loc=%d\n",
  1341. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1342. udf_debug("Bitmap file loc=%d\n",
  1343. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1344. udf_debug("Flags: %d %d\n",
  1345. mdata->s_flags, mdm->flags);
  1346. } else {
  1347. udf_debug("Unknown ident: %s\n",
  1348. upm2->partIdent.ident);
  1349. continue;
  1350. }
  1351. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1352. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1353. }
  1354. udf_debug("Partition (%d:%d) type %d on volume %d\n",
  1355. i, map->s_partition_num, type, map->s_volumeseqnum);
  1356. }
  1357. if (fileset) {
  1358. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1359. *fileset = lelb_to_cpu(la->extLocation);
  1360. udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
  1361. fileset->logicalBlockNum,
  1362. fileset->partitionReferenceNum);
  1363. }
  1364. if (lvd->integritySeqExt.extLength)
  1365. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1366. ret = 0;
  1367. out_bh:
  1368. brelse(bh);
  1369. return ret;
  1370. }
  1371. /*
  1372. * udf_load_logicalvolint
  1373. *
  1374. */
  1375. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1376. {
  1377. struct buffer_head *bh = NULL;
  1378. uint16_t ident;
  1379. struct udf_sb_info *sbi = UDF_SB(sb);
  1380. struct logicalVolIntegrityDesc *lvid;
  1381. while (loc.extLength > 0 &&
  1382. (bh = udf_read_tagged(sb, loc.extLocation,
  1383. loc.extLocation, &ident)) &&
  1384. ident == TAG_IDENT_LVID) {
  1385. sbi->s_lvid_bh = bh;
  1386. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1387. if (lvid->nextIntegrityExt.extLength)
  1388. udf_load_logicalvolint(sb,
  1389. leea_to_cpu(lvid->nextIntegrityExt));
  1390. if (sbi->s_lvid_bh != bh)
  1391. brelse(bh);
  1392. loc.extLength -= sb->s_blocksize;
  1393. loc.extLocation++;
  1394. }
  1395. if (sbi->s_lvid_bh != bh)
  1396. brelse(bh);
  1397. }
  1398. /*
  1399. * Process a main/reserve volume descriptor sequence.
  1400. * @block First block of first extent of the sequence.
  1401. * @lastblock Lastblock of first extent of the sequence.
  1402. * @fileset There we store extent containing root fileset
  1403. *
  1404. * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
  1405. * sequence
  1406. */
  1407. static noinline int udf_process_sequence(
  1408. struct super_block *sb,
  1409. sector_t block, sector_t lastblock,
  1410. struct kernel_lb_addr *fileset)
  1411. {
  1412. struct buffer_head *bh = NULL;
  1413. struct udf_vds_record vds[VDS_POS_LENGTH];
  1414. struct udf_vds_record *curr;
  1415. struct generic_desc *gd;
  1416. struct volDescPtr *vdp;
  1417. int done = 0;
  1418. uint32_t vdsn;
  1419. uint16_t ident;
  1420. long next_s = 0, next_e = 0;
  1421. int ret;
  1422. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1423. /*
  1424. * Read the main descriptor sequence and find which descriptors
  1425. * are in it.
  1426. */
  1427. for (; (!done && block <= lastblock); block++) {
  1428. bh = udf_read_tagged(sb, block, block, &ident);
  1429. if (!bh) {
  1430. udf_err(sb,
  1431. "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
  1432. (unsigned long long)block);
  1433. return -EAGAIN;
  1434. }
  1435. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1436. gd = (struct generic_desc *)bh->b_data;
  1437. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1438. switch (ident) {
  1439. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1440. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1441. if (vdsn >= curr->volDescSeqNum) {
  1442. curr->volDescSeqNum = vdsn;
  1443. curr->block = block;
  1444. }
  1445. break;
  1446. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1447. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1448. if (vdsn >= curr->volDescSeqNum) {
  1449. curr->volDescSeqNum = vdsn;
  1450. curr->block = block;
  1451. vdp = (struct volDescPtr *)bh->b_data;
  1452. next_s = le32_to_cpu(
  1453. vdp->nextVolDescSeqExt.extLocation);
  1454. next_e = le32_to_cpu(
  1455. vdp->nextVolDescSeqExt.extLength);
  1456. next_e = next_e >> sb->s_blocksize_bits;
  1457. next_e += next_s;
  1458. }
  1459. break;
  1460. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1461. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1462. if (vdsn >= curr->volDescSeqNum) {
  1463. curr->volDescSeqNum = vdsn;
  1464. curr->block = block;
  1465. }
  1466. break;
  1467. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1468. curr = &vds[VDS_POS_PARTITION_DESC];
  1469. if (!curr->block)
  1470. curr->block = block;
  1471. break;
  1472. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1473. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1474. if (vdsn >= curr->volDescSeqNum) {
  1475. curr->volDescSeqNum = vdsn;
  1476. curr->block = block;
  1477. }
  1478. break;
  1479. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1480. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1481. if (vdsn >= curr->volDescSeqNum) {
  1482. curr->volDescSeqNum = vdsn;
  1483. curr->block = block;
  1484. }
  1485. break;
  1486. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1487. vds[VDS_POS_TERMINATING_DESC].block = block;
  1488. if (next_e) {
  1489. block = next_s;
  1490. lastblock = next_e;
  1491. next_s = next_e = 0;
  1492. } else
  1493. done = 1;
  1494. break;
  1495. }
  1496. brelse(bh);
  1497. }
  1498. /*
  1499. * Now read interesting descriptors again and process them
  1500. * in a suitable order
  1501. */
  1502. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1503. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1504. return -EAGAIN;
  1505. }
  1506. ret = udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block);
  1507. if (ret < 0)
  1508. return ret;
  1509. if (vds[VDS_POS_LOGICAL_VOL_DESC].block) {
  1510. ret = udf_load_logicalvol(sb,
  1511. vds[VDS_POS_LOGICAL_VOL_DESC].block,
  1512. fileset);
  1513. if (ret < 0)
  1514. return ret;
  1515. }
  1516. if (vds[VDS_POS_PARTITION_DESC].block) {
  1517. /*
  1518. * We rescan the whole descriptor sequence to find
  1519. * partition descriptor blocks and process them.
  1520. */
  1521. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1522. block < vds[VDS_POS_TERMINATING_DESC].block;
  1523. block++) {
  1524. ret = udf_load_partdesc(sb, block);
  1525. if (ret < 0)
  1526. return ret;
  1527. }
  1528. }
  1529. return 0;
  1530. }
  1531. /*
  1532. * Load Volume Descriptor Sequence described by anchor in bh
  1533. *
  1534. * Returns <0 on error, 0 on success
  1535. */
  1536. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1537. struct kernel_lb_addr *fileset)
  1538. {
  1539. struct anchorVolDescPtr *anchor;
  1540. sector_t main_s, main_e, reserve_s, reserve_e;
  1541. int ret;
  1542. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1543. /* Locate the main sequence */
  1544. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1545. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1546. main_e = main_e >> sb->s_blocksize_bits;
  1547. main_e += main_s;
  1548. /* Locate the reserve sequence */
  1549. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1550. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1551. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1552. reserve_e += reserve_s;
  1553. /* Process the main & reserve sequences */
  1554. /* responsible for finding the PartitionDesc(s) */
  1555. ret = udf_process_sequence(sb, main_s, main_e, fileset);
  1556. if (ret != -EAGAIN)
  1557. return ret;
  1558. udf_sb_free_partitions(sb);
  1559. ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1560. if (ret < 0) {
  1561. udf_sb_free_partitions(sb);
  1562. /* No sequence was OK, return -EIO */
  1563. if (ret == -EAGAIN)
  1564. ret = -EIO;
  1565. }
  1566. return ret;
  1567. }
  1568. /*
  1569. * Check whether there is an anchor block in the given block and
  1570. * load Volume Descriptor Sequence if so.
  1571. *
  1572. * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
  1573. * block
  1574. */
  1575. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1576. struct kernel_lb_addr *fileset)
  1577. {
  1578. struct buffer_head *bh;
  1579. uint16_t ident;
  1580. int ret;
  1581. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1582. udf_fixed_to_variable(block) >=
  1583. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1584. return -EAGAIN;
  1585. bh = udf_read_tagged(sb, block, block, &ident);
  1586. if (!bh)
  1587. return -EAGAIN;
  1588. if (ident != TAG_IDENT_AVDP) {
  1589. brelse(bh);
  1590. return -EAGAIN;
  1591. }
  1592. ret = udf_load_sequence(sb, bh, fileset);
  1593. brelse(bh);
  1594. return ret;
  1595. }
  1596. /*
  1597. * Search for an anchor volume descriptor pointer.
  1598. *
  1599. * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
  1600. * of anchors.
  1601. */
  1602. static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
  1603. struct kernel_lb_addr *fileset)
  1604. {
  1605. sector_t last[6];
  1606. int i;
  1607. struct udf_sb_info *sbi = UDF_SB(sb);
  1608. int last_count = 0;
  1609. int ret;
  1610. /* First try user provided anchor */
  1611. if (sbi->s_anchor) {
  1612. ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
  1613. if (ret != -EAGAIN)
  1614. return ret;
  1615. }
  1616. /*
  1617. * according to spec, anchor is in either:
  1618. * block 256
  1619. * lastblock-256
  1620. * lastblock
  1621. * however, if the disc isn't closed, it could be 512.
  1622. */
  1623. ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
  1624. if (ret != -EAGAIN)
  1625. return ret;
  1626. /*
  1627. * The trouble is which block is the last one. Drives often misreport
  1628. * this so we try various possibilities.
  1629. */
  1630. last[last_count++] = *lastblock;
  1631. if (*lastblock >= 1)
  1632. last[last_count++] = *lastblock - 1;
  1633. last[last_count++] = *lastblock + 1;
  1634. if (*lastblock >= 2)
  1635. last[last_count++] = *lastblock - 2;
  1636. if (*lastblock >= 150)
  1637. last[last_count++] = *lastblock - 150;
  1638. if (*lastblock >= 152)
  1639. last[last_count++] = *lastblock - 152;
  1640. for (i = 0; i < last_count; i++) {
  1641. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1642. sb->s_blocksize_bits)
  1643. continue;
  1644. ret = udf_check_anchor_block(sb, last[i], fileset);
  1645. if (ret != -EAGAIN) {
  1646. if (!ret)
  1647. *lastblock = last[i];
  1648. return ret;
  1649. }
  1650. if (last[i] < 256)
  1651. continue;
  1652. ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
  1653. if (ret != -EAGAIN) {
  1654. if (!ret)
  1655. *lastblock = last[i];
  1656. return ret;
  1657. }
  1658. }
  1659. /* Finally try block 512 in case media is open */
  1660. return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
  1661. }
  1662. /*
  1663. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1664. * area specified by it. The function expects sbi->s_lastblock to be the last
  1665. * block on the media.
  1666. *
  1667. * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
  1668. * was not found.
  1669. */
  1670. static int udf_find_anchor(struct super_block *sb,
  1671. struct kernel_lb_addr *fileset)
  1672. {
  1673. struct udf_sb_info *sbi = UDF_SB(sb);
  1674. sector_t lastblock = sbi->s_last_block;
  1675. int ret;
  1676. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1677. if (ret != -EAGAIN)
  1678. goto out;
  1679. /* No anchor found? Try VARCONV conversion of block numbers */
  1680. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1681. lastblock = udf_variable_to_fixed(sbi->s_last_block);
  1682. /* Firstly, we try to not convert number of the last block */
  1683. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1684. if (ret != -EAGAIN)
  1685. goto out;
  1686. lastblock = sbi->s_last_block;
  1687. /* Secondly, we try with converted number of the last block */
  1688. ret = udf_scan_anchors(sb, &lastblock, fileset);
  1689. if (ret < 0) {
  1690. /* VARCONV didn't help. Clear it. */
  1691. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1692. }
  1693. out:
  1694. if (ret == 0)
  1695. sbi->s_last_block = lastblock;
  1696. return ret;
  1697. }
  1698. /*
  1699. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1700. * Descriptor Sequence.
  1701. *
  1702. * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
  1703. * block was not found.
  1704. */
  1705. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1706. int silent, struct kernel_lb_addr *fileset)
  1707. {
  1708. struct udf_sb_info *sbi = UDF_SB(sb);
  1709. loff_t nsr_off;
  1710. int ret;
  1711. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1712. if (!silent)
  1713. udf_warn(sb, "Bad block size\n");
  1714. return -EINVAL;
  1715. }
  1716. sbi->s_last_block = uopt->lastblock;
  1717. if (!uopt->novrs) {
  1718. /* Check that it is NSR02 compliant */
  1719. nsr_off = udf_check_vsd(sb);
  1720. if (!nsr_off) {
  1721. if (!silent)
  1722. udf_warn(sb, "No VRS found\n");
  1723. return 0;
  1724. }
  1725. if (nsr_off == -1)
  1726. udf_debug("Failed to read sector at offset %d. "
  1727. "Assuming open disc. Skipping validity "
  1728. "check\n", VSD_FIRST_SECTOR_OFFSET);
  1729. if (!sbi->s_last_block)
  1730. sbi->s_last_block = udf_get_last_block(sb);
  1731. } else {
  1732. udf_debug("Validity check skipped because of novrs option\n");
  1733. }
  1734. /* Look for anchor block and load Volume Descriptor Sequence */
  1735. sbi->s_anchor = uopt->anchor;
  1736. ret = udf_find_anchor(sb, fileset);
  1737. if (ret < 0) {
  1738. if (!silent && ret == -EAGAIN)
  1739. udf_warn(sb, "No anchor found\n");
  1740. return ret;
  1741. }
  1742. return 0;
  1743. }
  1744. static void udf_open_lvid(struct super_block *sb)
  1745. {
  1746. struct udf_sb_info *sbi = UDF_SB(sb);
  1747. struct buffer_head *bh = sbi->s_lvid_bh;
  1748. struct logicalVolIntegrityDesc *lvid;
  1749. struct logicalVolIntegrityDescImpUse *lvidiu;
  1750. if (!bh)
  1751. return;
  1752. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1753. lvidiu = udf_sb_lvidiu(sb);
  1754. if (!lvidiu)
  1755. return;
  1756. mutex_lock(&sbi->s_alloc_mutex);
  1757. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1758. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1759. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1760. CURRENT_TIME);
  1761. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1762. lvid->descTag.descCRC = cpu_to_le16(
  1763. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1764. le16_to_cpu(lvid->descTag.descCRCLength)));
  1765. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1766. mark_buffer_dirty(bh);
  1767. sbi->s_lvid_dirty = 0;
  1768. mutex_unlock(&sbi->s_alloc_mutex);
  1769. /* Make opening of filesystem visible on the media immediately */
  1770. sync_dirty_buffer(bh);
  1771. }
  1772. static void udf_close_lvid(struct super_block *sb)
  1773. {
  1774. struct udf_sb_info *sbi = UDF_SB(sb);
  1775. struct buffer_head *bh = sbi->s_lvid_bh;
  1776. struct logicalVolIntegrityDesc *lvid;
  1777. struct logicalVolIntegrityDescImpUse *lvidiu;
  1778. if (!bh)
  1779. return;
  1780. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1781. lvidiu = udf_sb_lvidiu(sb);
  1782. if (!lvidiu)
  1783. return;
  1784. mutex_lock(&sbi->s_alloc_mutex);
  1785. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1786. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1787. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1788. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1789. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1790. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1791. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1792. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1793. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1794. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1795. lvid->descTag.descCRC = cpu_to_le16(
  1796. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1797. le16_to_cpu(lvid->descTag.descCRCLength)));
  1798. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1799. /*
  1800. * We set buffer uptodate unconditionally here to avoid spurious
  1801. * warnings from mark_buffer_dirty() when previous EIO has marked
  1802. * the buffer as !uptodate
  1803. */
  1804. set_buffer_uptodate(bh);
  1805. mark_buffer_dirty(bh);
  1806. sbi->s_lvid_dirty = 0;
  1807. mutex_unlock(&sbi->s_alloc_mutex);
  1808. /* Make closing of filesystem visible on the media immediately */
  1809. sync_dirty_buffer(bh);
  1810. }
  1811. u64 lvid_get_unique_id(struct super_block *sb)
  1812. {
  1813. struct buffer_head *bh;
  1814. struct udf_sb_info *sbi = UDF_SB(sb);
  1815. struct logicalVolIntegrityDesc *lvid;
  1816. struct logicalVolHeaderDesc *lvhd;
  1817. u64 uniqueID;
  1818. u64 ret;
  1819. bh = sbi->s_lvid_bh;
  1820. if (!bh)
  1821. return 0;
  1822. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1823. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1824. mutex_lock(&sbi->s_alloc_mutex);
  1825. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1826. if (!(++uniqueID & 0xFFFFFFFF))
  1827. uniqueID += 16;
  1828. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1829. mutex_unlock(&sbi->s_alloc_mutex);
  1830. mark_buffer_dirty(bh);
  1831. return ret;
  1832. }
  1833. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1834. {
  1835. int ret = -EINVAL;
  1836. struct inode *inode = NULL;
  1837. struct udf_options uopt;
  1838. struct kernel_lb_addr rootdir, fileset;
  1839. struct udf_sb_info *sbi;
  1840. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1841. uopt.uid = INVALID_UID;
  1842. uopt.gid = INVALID_GID;
  1843. uopt.umask = 0;
  1844. uopt.fmode = UDF_INVALID_MODE;
  1845. uopt.dmode = UDF_INVALID_MODE;
  1846. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1847. if (!sbi)
  1848. return -ENOMEM;
  1849. sb->s_fs_info = sbi;
  1850. mutex_init(&sbi->s_alloc_mutex);
  1851. if (!udf_parse_options((char *)options, &uopt, false))
  1852. goto error_out;
  1853. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1854. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1855. udf_err(sb, "utf8 cannot be combined with iocharset\n");
  1856. goto error_out;
  1857. }
  1858. #ifdef CONFIG_UDF_NLS
  1859. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1860. uopt.nls_map = load_nls_default();
  1861. if (!uopt.nls_map)
  1862. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1863. else
  1864. udf_debug("Using default NLS map\n");
  1865. }
  1866. #endif
  1867. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1868. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1869. fileset.logicalBlockNum = 0xFFFFFFFF;
  1870. fileset.partitionReferenceNum = 0xFFFF;
  1871. sbi->s_flags = uopt.flags;
  1872. sbi->s_uid = uopt.uid;
  1873. sbi->s_gid = uopt.gid;
  1874. sbi->s_umask = uopt.umask;
  1875. sbi->s_fmode = uopt.fmode;
  1876. sbi->s_dmode = uopt.dmode;
  1877. sbi->s_nls_map = uopt.nls_map;
  1878. rwlock_init(&sbi->s_cred_lock);
  1879. if (uopt.session == 0xFFFFFFFF)
  1880. sbi->s_session = udf_get_last_session(sb);
  1881. else
  1882. sbi->s_session = uopt.session;
  1883. udf_debug("Multi-session=%d\n", sbi->s_session);
  1884. /* Fill in the rest of the superblock */
  1885. sb->s_op = &udf_sb_ops;
  1886. sb->s_export_op = &udf_export_ops;
  1887. sb->s_magic = UDF_SUPER_MAGIC;
  1888. sb->s_time_gran = 1000;
  1889. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1890. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1891. } else {
  1892. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1893. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1894. if (ret == -EAGAIN && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1895. if (!silent)
  1896. pr_notice("Rescanning with blocksize %d\n",
  1897. UDF_DEFAULT_BLOCKSIZE);
  1898. brelse(sbi->s_lvid_bh);
  1899. sbi->s_lvid_bh = NULL;
  1900. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1901. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1902. }
  1903. }
  1904. if (ret < 0) {
  1905. if (ret == -EAGAIN) {
  1906. udf_warn(sb, "No partition found (1)\n");
  1907. ret = -EINVAL;
  1908. }
  1909. goto error_out;
  1910. }
  1911. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1912. if (sbi->s_lvid_bh) {
  1913. struct logicalVolIntegrityDescImpUse *lvidiu =
  1914. udf_sb_lvidiu(sb);
  1915. uint16_t minUDFReadRev;
  1916. uint16_t minUDFWriteRev;
  1917. if (!lvidiu) {
  1918. ret = -EINVAL;
  1919. goto error_out;
  1920. }
  1921. minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1922. minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1923. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1924. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1925. minUDFReadRev,
  1926. UDF_MAX_READ_VERSION);
  1927. ret = -EINVAL;
  1928. goto error_out;
  1929. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
  1930. !(sb->s_flags & MS_RDONLY)) {
  1931. ret = -EACCES;
  1932. goto error_out;
  1933. }
  1934. sbi->s_udfrev = minUDFWriteRev;
  1935. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1936. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1937. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1938. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1939. }
  1940. if (!sbi->s_partitions) {
  1941. udf_warn(sb, "No partition found (2)\n");
  1942. ret = -EINVAL;
  1943. goto error_out;
  1944. }
  1945. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1946. UDF_PART_FLAG_READ_ONLY &&
  1947. !(sb->s_flags & MS_RDONLY)) {
  1948. ret = -EACCES;
  1949. goto error_out;
  1950. }
  1951. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1952. udf_warn(sb, "No fileset found\n");
  1953. ret = -EINVAL;
  1954. goto error_out;
  1955. }
  1956. if (!silent) {
  1957. struct timestamp ts;
  1958. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1959. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1960. sbi->s_volume_ident,
  1961. le16_to_cpu(ts.year), ts.month, ts.day,
  1962. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1963. }
  1964. if (!(sb->s_flags & MS_RDONLY))
  1965. udf_open_lvid(sb);
  1966. /* Assign the root inode */
  1967. /* assign inodes by physical block number */
  1968. /* perhaps it's not extensible enough, but for now ... */
  1969. inode = udf_iget(sb, &rootdir);
  1970. if (!inode) {
  1971. udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
  1972. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1973. ret = -EIO;
  1974. goto error_out;
  1975. }
  1976. /* Allocate a dentry for the root inode */
  1977. sb->s_root = d_make_root(inode);
  1978. if (!sb->s_root) {
  1979. udf_err(sb, "Couldn't allocate root dentry\n");
  1980. ret = -ENOMEM;
  1981. goto error_out;
  1982. }
  1983. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1984. sb->s_max_links = UDF_MAX_LINKS;
  1985. return 0;
  1986. error_out:
  1987. if (sbi->s_vat_inode)
  1988. iput(sbi->s_vat_inode);
  1989. #ifdef CONFIG_UDF_NLS
  1990. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1991. unload_nls(sbi->s_nls_map);
  1992. #endif
  1993. if (!(sb->s_flags & MS_RDONLY))
  1994. udf_close_lvid(sb);
  1995. brelse(sbi->s_lvid_bh);
  1996. udf_sb_free_partitions(sb);
  1997. kfree(sbi);
  1998. sb->s_fs_info = NULL;
  1999. return ret;
  2000. }
  2001. void _udf_err(struct super_block *sb, const char *function,
  2002. const char *fmt, ...)
  2003. {
  2004. struct va_format vaf;
  2005. va_list args;
  2006. va_start(args, fmt);
  2007. vaf.fmt = fmt;
  2008. vaf.va = &args;
  2009. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  2010. va_end(args);
  2011. }
  2012. void _udf_warn(struct super_block *sb, const char *function,
  2013. const char *fmt, ...)
  2014. {
  2015. struct va_format vaf;
  2016. va_list args;
  2017. va_start(args, fmt);
  2018. vaf.fmt = fmt;
  2019. vaf.va = &args;
  2020. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  2021. va_end(args);
  2022. }
  2023. static void udf_put_super(struct super_block *sb)
  2024. {
  2025. struct udf_sb_info *sbi;
  2026. sbi = UDF_SB(sb);
  2027. if (sbi->s_vat_inode)
  2028. iput(sbi->s_vat_inode);
  2029. #ifdef CONFIG_UDF_NLS
  2030. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  2031. unload_nls(sbi->s_nls_map);
  2032. #endif
  2033. if (!(sb->s_flags & MS_RDONLY))
  2034. udf_close_lvid(sb);
  2035. brelse(sbi->s_lvid_bh);
  2036. udf_sb_free_partitions(sb);
  2037. kfree(sb->s_fs_info);
  2038. sb->s_fs_info = NULL;
  2039. }
  2040. static int udf_sync_fs(struct super_block *sb, int wait)
  2041. {
  2042. struct udf_sb_info *sbi = UDF_SB(sb);
  2043. mutex_lock(&sbi->s_alloc_mutex);
  2044. if (sbi->s_lvid_dirty) {
  2045. /*
  2046. * Blockdevice will be synced later so we don't have to submit
  2047. * the buffer for IO
  2048. */
  2049. mark_buffer_dirty(sbi->s_lvid_bh);
  2050. sbi->s_lvid_dirty = 0;
  2051. }
  2052. mutex_unlock(&sbi->s_alloc_mutex);
  2053. return 0;
  2054. }
  2055. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  2056. {
  2057. struct super_block *sb = dentry->d_sb;
  2058. struct udf_sb_info *sbi = UDF_SB(sb);
  2059. struct logicalVolIntegrityDescImpUse *lvidiu;
  2060. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  2061. lvidiu = udf_sb_lvidiu(sb);
  2062. buf->f_type = UDF_SUPER_MAGIC;
  2063. buf->f_bsize = sb->s_blocksize;
  2064. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  2065. buf->f_bfree = udf_count_free(sb);
  2066. buf->f_bavail = buf->f_bfree;
  2067. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  2068. le32_to_cpu(lvidiu->numDirs)) : 0)
  2069. + buf->f_bfree;
  2070. buf->f_ffree = buf->f_bfree;
  2071. buf->f_namelen = UDF_NAME_LEN - 2;
  2072. buf->f_fsid.val[0] = (u32)id;
  2073. buf->f_fsid.val[1] = (u32)(id >> 32);
  2074. return 0;
  2075. }
  2076. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  2077. struct udf_bitmap *bitmap)
  2078. {
  2079. struct buffer_head *bh = NULL;
  2080. unsigned int accum = 0;
  2081. int index;
  2082. int block = 0, newblock;
  2083. struct kernel_lb_addr loc;
  2084. uint32_t bytes;
  2085. uint8_t *ptr;
  2086. uint16_t ident;
  2087. struct spaceBitmapDesc *bm;
  2088. loc.logicalBlockNum = bitmap->s_extPosition;
  2089. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  2090. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  2091. if (!bh) {
  2092. udf_err(sb, "udf_count_free failed\n");
  2093. goto out;
  2094. } else if (ident != TAG_IDENT_SBD) {
  2095. brelse(bh);
  2096. udf_err(sb, "udf_count_free failed\n");
  2097. goto out;
  2098. }
  2099. bm = (struct spaceBitmapDesc *)bh->b_data;
  2100. bytes = le32_to_cpu(bm->numOfBytes);
  2101. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  2102. ptr = (uint8_t *)bh->b_data;
  2103. while (bytes > 0) {
  2104. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  2105. accum += bitmap_weight((const unsigned long *)(ptr + index),
  2106. cur_bytes * 8);
  2107. bytes -= cur_bytes;
  2108. if (bytes) {
  2109. brelse(bh);
  2110. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  2111. bh = udf_tread(sb, newblock);
  2112. if (!bh) {
  2113. udf_debug("read failed\n");
  2114. goto out;
  2115. }
  2116. index = 0;
  2117. ptr = (uint8_t *)bh->b_data;
  2118. }
  2119. }
  2120. brelse(bh);
  2121. out:
  2122. return accum;
  2123. }
  2124. static unsigned int udf_count_free_table(struct super_block *sb,
  2125. struct inode *table)
  2126. {
  2127. unsigned int accum = 0;
  2128. uint32_t elen;
  2129. struct kernel_lb_addr eloc;
  2130. int8_t etype;
  2131. struct extent_position epos;
  2132. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2133. epos.block = UDF_I(table)->i_location;
  2134. epos.offset = sizeof(struct unallocSpaceEntry);
  2135. epos.bh = NULL;
  2136. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2137. accum += (elen >> table->i_sb->s_blocksize_bits);
  2138. brelse(epos.bh);
  2139. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2140. return accum;
  2141. }
  2142. static unsigned int udf_count_free(struct super_block *sb)
  2143. {
  2144. unsigned int accum = 0;
  2145. struct udf_sb_info *sbi;
  2146. struct udf_part_map *map;
  2147. sbi = UDF_SB(sb);
  2148. if (sbi->s_lvid_bh) {
  2149. struct logicalVolIntegrityDesc *lvid =
  2150. (struct logicalVolIntegrityDesc *)
  2151. sbi->s_lvid_bh->b_data;
  2152. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2153. accum = le32_to_cpu(
  2154. lvid->freeSpaceTable[sbi->s_partition]);
  2155. if (accum == 0xFFFFFFFF)
  2156. accum = 0;
  2157. }
  2158. }
  2159. if (accum)
  2160. return accum;
  2161. map = &sbi->s_partmaps[sbi->s_partition];
  2162. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2163. accum += udf_count_free_bitmap(sb,
  2164. map->s_uspace.s_bitmap);
  2165. }
  2166. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2167. accum += udf_count_free_bitmap(sb,
  2168. map->s_fspace.s_bitmap);
  2169. }
  2170. if (accum)
  2171. return accum;
  2172. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2173. accum += udf_count_free_table(sb,
  2174. map->s_uspace.s_table);
  2175. }
  2176. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2177. accum += udf_count_free_table(sb,
  2178. map->s_fspace.s_table);
  2179. }
  2180. return accum;
  2181. }