super.c 65 KB

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