super.c 64 KB

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