super.c 60 KB

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