super.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  1. /*
  2. * super.c
  3. *
  4. * PURPOSE
  5. * Super block routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * DESCRIPTION
  8. * OSTA-UDF(tm) = Optical Storage Technology Association
  9. * Universal Disk Format.
  10. *
  11. * This code is based on version 2.00 of the UDF specification,
  12. * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
  13. * http://www.osta.org/
  14. * http://www.ecma.ch/
  15. * http://www.iso.org/
  16. *
  17. * COPYRIGHT
  18. * This file is distributed under the terms of the GNU General Public
  19. * License (GPL). Copies of the GPL can be obtained from:
  20. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  21. * Each contributing author retains all rights to their own work.
  22. *
  23. * (C) 1998 Dave Boynton
  24. * (C) 1998-2004 Ben Fennema
  25. * (C) 2000 Stelias Computing Inc
  26. *
  27. * HISTORY
  28. *
  29. * 09/24/98 dgb changed to allow compiling outside of kernel, and
  30. * added some debugging.
  31. * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
  32. * 10/16/98 attempting some multi-session support
  33. * 10/17/98 added freespace count for "df"
  34. * 11/11/98 gr added novrs option
  35. * 11/26/98 dgb added fileset,anchor mount options
  36. * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
  37. * vol descs. rewrote option handling based on isofs
  38. * 12/20/98 find the free space bitmap (if it exists)
  39. */
  40. #include "udfdecl.h"
  41. #include <linux/blkdev.h>
  42. #include <linux/slab.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/parser.h>
  46. #include <linux/stat.h>
  47. #include <linux/cdrom.h>
  48. #include <linux/nls.h>
  49. #include <linux/buffer_head.h>
  50. #include <linux/vfs.h>
  51. #include <linux/vmalloc.h>
  52. #include <linux/errno.h>
  53. #include <linux/mount.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/bitmap.h>
  56. #include <linux/crc-itu-t.h>
  57. #include <asm/byteorder.h>
  58. #include "udf_sb.h"
  59. #include "udf_i.h"
  60. #include <linux/init.h>
  61. #include <asm/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. /* These are the "meat" - everything else is stuffing */
  72. static int udf_fill_super(struct super_block *, void *, int);
  73. static void udf_put_super(struct super_block *);
  74. static int udf_sync_fs(struct super_block *, int);
  75. static int udf_remount_fs(struct super_block *, int *, char *);
  76. static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
  77. static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
  78. struct kernel_lb_addr *);
  79. static void udf_load_fileset(struct super_block *, struct buffer_head *,
  80. struct kernel_lb_addr *);
  81. static void udf_open_lvid(struct super_block *);
  82. static void udf_close_lvid(struct super_block *);
  83. static unsigned int udf_count_free(struct super_block *);
  84. static int udf_statfs(struct dentry *, struct kstatfs *);
  85. static int udf_show_options(struct seq_file *, struct vfsmount *);
  86. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
  87. {
  88. struct logicalVolIntegrityDesc *lvid =
  89. (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
  90. __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
  91. __u32 offset = number_of_partitions * 2 *
  92. sizeof(uint32_t)/sizeof(uint8_t);
  93. return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
  94. }
  95. /* UDF filesystem type */
  96. static struct dentry *udf_mount(struct file_system_type *fs_type,
  97. int flags, const char *dev_name, void *data)
  98. {
  99. return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
  100. }
  101. static struct file_system_type udf_fstype = {
  102. .owner = THIS_MODULE,
  103. .name = "udf",
  104. .mount = udf_mount,
  105. .kill_sb = kill_block_super,
  106. .fs_flags = FS_REQUIRES_DEV,
  107. };
  108. static struct kmem_cache *udf_inode_cachep;
  109. static struct inode *udf_alloc_inode(struct super_block *sb)
  110. {
  111. struct udf_inode_info *ei;
  112. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  113. if (!ei)
  114. return NULL;
  115. ei->i_unique = 0;
  116. ei->i_lenExtents = 0;
  117. ei->i_next_alloc_block = 0;
  118. ei->i_next_alloc_goal = 0;
  119. ei->i_strat4096 = 0;
  120. init_rwsem(&ei->i_data_sem);
  121. return &ei->vfs_inode;
  122. }
  123. static void udf_i_callback(struct rcu_head *head)
  124. {
  125. struct inode *inode = container_of(head, struct inode, i_rcu);
  126. INIT_LIST_HEAD(&inode->i_dentry);
  127. kmem_cache_free(udf_inode_cachep, UDF_I(inode));
  128. }
  129. static void udf_destroy_inode(struct inode *inode)
  130. {
  131. call_rcu(&inode->i_rcu, udf_i_callback);
  132. }
  133. static void init_once(void *foo)
  134. {
  135. struct udf_inode_info *ei = (struct udf_inode_info *)foo;
  136. ei->i_ext.i_data = NULL;
  137. inode_init_once(&ei->vfs_inode);
  138. }
  139. static int init_inodecache(void)
  140. {
  141. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  142. sizeof(struct udf_inode_info),
  143. 0, (SLAB_RECLAIM_ACCOUNT |
  144. SLAB_MEM_SPREAD),
  145. init_once);
  146. if (!udf_inode_cachep)
  147. return -ENOMEM;
  148. return 0;
  149. }
  150. static void destroy_inodecache(void)
  151. {
  152. kmem_cache_destroy(udf_inode_cachep);
  153. }
  154. /* Superblock operations */
  155. static const struct super_operations udf_sb_ops = {
  156. .alloc_inode = udf_alloc_inode,
  157. .destroy_inode = udf_destroy_inode,
  158. .write_inode = udf_write_inode,
  159. .evict_inode = udf_evict_inode,
  160. .put_super = udf_put_super,
  161. .sync_fs = udf_sync_fs,
  162. .statfs = udf_statfs,
  163. .remount_fs = udf_remount_fs,
  164. .show_options = udf_show_options,
  165. };
  166. struct udf_options {
  167. unsigned char novrs;
  168. unsigned int blocksize;
  169. unsigned int session;
  170. unsigned int lastblock;
  171. unsigned int anchor;
  172. unsigned int volume;
  173. unsigned short partition;
  174. unsigned int fileset;
  175. unsigned int rootdir;
  176. unsigned int flags;
  177. mode_t umask;
  178. gid_t gid;
  179. uid_t uid;
  180. mode_t fmode;
  181. mode_t dmode;
  182. struct nls_table *nls_map;
  183. };
  184. static int __init init_udf_fs(void)
  185. {
  186. int err;
  187. err = init_inodecache();
  188. if (err)
  189. goto out1;
  190. err = register_filesystem(&udf_fstype);
  191. if (err)
  192. goto out;
  193. return 0;
  194. out:
  195. destroy_inodecache();
  196. out1:
  197. return err;
  198. }
  199. static void __exit exit_udf_fs(void)
  200. {
  201. unregister_filesystem(&udf_fstype);
  202. destroy_inodecache();
  203. }
  204. module_init(init_udf_fs)
  205. module_exit(exit_udf_fs)
  206. static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
  207. {
  208. struct udf_sb_info *sbi = UDF_SB(sb);
  209. sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
  210. GFP_KERNEL);
  211. if (!sbi->s_partmaps) {
  212. udf_err(sb, "Unable to allocate space for %d partition maps\n",
  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. pr_err("bad mount option \"%s\" or missing value\n", p);
  506. return 0;
  507. }
  508. }
  509. return 1;
  510. }
  511. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  512. {
  513. struct udf_options uopt;
  514. struct udf_sb_info *sbi = UDF_SB(sb);
  515. int error = 0;
  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. write_lock(&sbi->s_cred_lock);
  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. write_unlock(&sbi->s_cred_lock);
  532. if (sbi->s_lvid_bh) {
  533. int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
  534. if (write_rev > UDF_MAX_WRITE_VERSION)
  535. *flags |= MS_RDONLY;
  536. }
  537. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  538. goto out_unlock;
  539. if (*flags & MS_RDONLY)
  540. udf_close_lvid(sb);
  541. else
  542. udf_open_lvid(sb);
  543. out_unlock:
  544. return error;
  545. }
  546. /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
  547. /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
  548. static loff_t udf_check_vsd(struct super_block *sb)
  549. {
  550. struct volStructDesc *vsd = NULL;
  551. loff_t sector = 32768;
  552. int sectorsize;
  553. struct buffer_head *bh = NULL;
  554. int nsr02 = 0;
  555. int nsr03 = 0;
  556. struct udf_sb_info *sbi;
  557. sbi = UDF_SB(sb);
  558. if (sb->s_blocksize < sizeof(struct volStructDesc))
  559. sectorsize = sizeof(struct volStructDesc);
  560. else
  561. sectorsize = sb->s_blocksize;
  562. sector += (sbi->s_session << sb->s_blocksize_bits);
  563. udf_debug("Starting at sector %u (%ld byte sectors)\n",
  564. (unsigned int)(sector >> sb->s_blocksize_bits),
  565. sb->s_blocksize);
  566. /* Process the sequence (if applicable) */
  567. for (; !nsr02 && !nsr03; sector += sectorsize) {
  568. /* Read a block */
  569. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  570. if (!bh)
  571. break;
  572. /* Look for ISO descriptors */
  573. vsd = (struct volStructDesc *)(bh->b_data +
  574. (sector & (sb->s_blocksize - 1)));
  575. if (vsd->stdIdent[0] == 0) {
  576. brelse(bh);
  577. break;
  578. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
  579. VSD_STD_ID_LEN)) {
  580. switch (vsd->structType) {
  581. case 0:
  582. udf_debug("ISO9660 Boot Record found\n");
  583. break;
  584. case 1:
  585. udf_debug("ISO9660 Primary Volume Descriptor found\n");
  586. break;
  587. case 2:
  588. udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
  589. break;
  590. case 3:
  591. udf_debug("ISO9660 Volume Partition Descriptor found\n");
  592. break;
  593. case 255:
  594. udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
  595. break;
  596. default:
  597. udf_debug("ISO9660 VRS (%u) found\n",
  598. vsd->structType);
  599. break;
  600. }
  601. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  602. VSD_STD_ID_LEN))
  603. ; /* nothing */
  604. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  605. VSD_STD_ID_LEN)) {
  606. brelse(bh);
  607. break;
  608. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  609. VSD_STD_ID_LEN))
  610. nsr02 = sector;
  611. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  612. VSD_STD_ID_LEN))
  613. nsr03 = sector;
  614. brelse(bh);
  615. }
  616. if (nsr03)
  617. return nsr03;
  618. else if (nsr02)
  619. return nsr02;
  620. else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
  621. return -1;
  622. else
  623. return 0;
  624. }
  625. static int udf_find_fileset(struct super_block *sb,
  626. struct kernel_lb_addr *fileset,
  627. struct kernel_lb_addr *root)
  628. {
  629. struct buffer_head *bh = NULL;
  630. long lastblock;
  631. uint16_t ident;
  632. struct udf_sb_info *sbi;
  633. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  634. fileset->partitionReferenceNum != 0xFFFF) {
  635. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  636. if (!bh) {
  637. return 1;
  638. } else if (ident != TAG_IDENT_FSD) {
  639. brelse(bh);
  640. return 1;
  641. }
  642. }
  643. sbi = UDF_SB(sb);
  644. if (!bh) {
  645. /* Search backwards through the partitions */
  646. struct kernel_lb_addr newfileset;
  647. /* --> cvg: FIXME - is it reasonable? */
  648. return 1;
  649. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  650. (newfileset.partitionReferenceNum != 0xFFFF &&
  651. fileset->logicalBlockNum == 0xFFFFFFFF &&
  652. fileset->partitionReferenceNum == 0xFFFF);
  653. newfileset.partitionReferenceNum--) {
  654. lastblock = sbi->s_partmaps
  655. [newfileset.partitionReferenceNum]
  656. .s_partition_len;
  657. newfileset.logicalBlockNum = 0;
  658. do {
  659. bh = udf_read_ptagged(sb, &newfileset, 0,
  660. &ident);
  661. if (!bh) {
  662. newfileset.logicalBlockNum++;
  663. continue;
  664. }
  665. switch (ident) {
  666. case TAG_IDENT_SBD:
  667. {
  668. struct spaceBitmapDesc *sp;
  669. sp = (struct spaceBitmapDesc *)
  670. bh->b_data;
  671. newfileset.logicalBlockNum += 1 +
  672. ((le32_to_cpu(sp->numOfBytes) +
  673. sizeof(struct spaceBitmapDesc)
  674. - 1) >> sb->s_blocksize_bits);
  675. brelse(bh);
  676. break;
  677. }
  678. case TAG_IDENT_FSD:
  679. *fileset = newfileset;
  680. break;
  681. default:
  682. newfileset.logicalBlockNum++;
  683. brelse(bh);
  684. bh = NULL;
  685. break;
  686. }
  687. } while (newfileset.logicalBlockNum < lastblock &&
  688. fileset->logicalBlockNum == 0xFFFFFFFF &&
  689. fileset->partitionReferenceNum == 0xFFFF);
  690. }
  691. }
  692. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  693. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  694. udf_debug("Fileset at block=%d, partition=%d\n",
  695. fileset->logicalBlockNum,
  696. fileset->partitionReferenceNum);
  697. sbi->s_partition = fileset->partitionReferenceNum;
  698. udf_load_fileset(sb, bh, root);
  699. brelse(bh);
  700. return 0;
  701. }
  702. return 1;
  703. }
  704. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  705. {
  706. struct primaryVolDesc *pvoldesc;
  707. struct ustr *instr, *outstr;
  708. struct buffer_head *bh;
  709. uint16_t ident;
  710. int ret = 1;
  711. instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  712. if (!instr)
  713. return 1;
  714. outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  715. if (!outstr)
  716. goto out1;
  717. bh = udf_read_tagged(sb, block, block, &ident);
  718. if (!bh)
  719. goto out2;
  720. BUG_ON(ident != TAG_IDENT_PVD);
  721. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  722. if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  723. pvoldesc->recordingDateAndTime)) {
  724. #ifdef UDFFS_DEBUG
  725. struct timestamp *ts = &pvoldesc->recordingDateAndTime;
  726. udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
  727. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  728. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  729. #endif
  730. }
  731. if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
  732. if (udf_CS0toUTF8(outstr, instr)) {
  733. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  734. outstr->u_len > 31 ? 31 : outstr->u_len);
  735. udf_debug("volIdent[] = '%s'\n",
  736. UDF_SB(sb)->s_volume_ident);
  737. }
  738. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  739. if (udf_CS0toUTF8(outstr, instr))
  740. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  741. brelse(bh);
  742. ret = 0;
  743. out2:
  744. kfree(outstr);
  745. out1:
  746. kfree(instr);
  747. return ret;
  748. }
  749. static int udf_load_metadata_files(struct super_block *sb, int partition)
  750. {
  751. struct udf_sb_info *sbi = UDF_SB(sb);
  752. struct udf_part_map *map;
  753. struct udf_meta_data *mdata;
  754. struct kernel_lb_addr addr;
  755. int fe_error = 0;
  756. map = &sbi->s_partmaps[partition];
  757. mdata = &map->s_type_specific.s_metadata;
  758. /* metadata address */
  759. addr.logicalBlockNum = mdata->s_meta_file_loc;
  760. addr.partitionReferenceNum = map->s_partition_num;
  761. udf_debug("Metadata file location: block = %d part = %d\n",
  762. addr.logicalBlockNum, addr.partitionReferenceNum);
  763. mdata->s_metadata_fe = udf_iget(sb, &addr);
  764. if (mdata->s_metadata_fe == NULL) {
  765. udf_warn(sb, "metadata inode efe not found, will try mirror inode\n");
  766. fe_error = 1;
  767. } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
  768. ICBTAG_FLAG_AD_SHORT) {
  769. udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
  770. fe_error = 1;
  771. iput(mdata->s_metadata_fe);
  772. mdata->s_metadata_fe = NULL;
  773. }
  774. /* mirror file entry */
  775. addr.logicalBlockNum = mdata->s_mirror_file_loc;
  776. addr.partitionReferenceNum = map->s_partition_num;
  777. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  778. addr.logicalBlockNum, addr.partitionReferenceNum);
  779. mdata->s_mirror_fe = udf_iget(sb, &addr);
  780. if (mdata->s_mirror_fe == NULL) {
  781. if (fe_error) {
  782. udf_err(sb, "mirror inode efe not found and metadata inode is missing too, exiting...\n");
  783. goto error_exit;
  784. } else
  785. udf_warn(sb, "mirror inode efe not found, but metadata inode is OK\n");
  786. } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
  787. ICBTAG_FLAG_AD_SHORT) {
  788. udf_warn(sb, "mirror inode efe does not have short allocation descriptors!\n");
  789. iput(mdata->s_mirror_fe);
  790. mdata->s_mirror_fe = NULL;
  791. if (fe_error)
  792. goto error_exit;
  793. }
  794. /*
  795. * bitmap file entry
  796. * Note:
  797. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  798. */
  799. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  800. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  801. addr.partitionReferenceNum = map->s_partition_num;
  802. udf_debug("Bitmap file location: block = %d part = %d\n",
  803. addr.logicalBlockNum, addr.partitionReferenceNum);
  804. mdata->s_bitmap_fe = udf_iget(sb, &addr);
  805. if (mdata->s_bitmap_fe == NULL) {
  806. if (sb->s_flags & MS_RDONLY)
  807. udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
  808. else {
  809. udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
  810. goto error_exit;
  811. }
  812. }
  813. }
  814. udf_debug("udf_load_metadata_files Ok\n");
  815. return 0;
  816. error_exit:
  817. return 1;
  818. }
  819. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  820. struct kernel_lb_addr *root)
  821. {
  822. struct fileSetDesc *fset;
  823. fset = (struct fileSetDesc *)bh->b_data;
  824. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  825. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  826. udf_debug("Rootdir at block=%d, partition=%d\n",
  827. root->logicalBlockNum, root->partitionReferenceNum);
  828. }
  829. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  830. {
  831. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  832. return DIV_ROUND_UP(map->s_partition_len +
  833. (sizeof(struct spaceBitmapDesc) << 3),
  834. sb->s_blocksize * 8);
  835. }
  836. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  837. {
  838. struct udf_bitmap *bitmap;
  839. int nr_groups;
  840. int size;
  841. nr_groups = udf_compute_nr_groups(sb, index);
  842. size = sizeof(struct udf_bitmap) +
  843. (sizeof(struct buffer_head *) * nr_groups);
  844. if (size <= PAGE_SIZE)
  845. bitmap = kzalloc(size, GFP_KERNEL);
  846. else
  847. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  848. if (bitmap == NULL) {
  849. udf_err(sb, "Unable to allocate space for bitmap and %d buffer_head pointers\n",
  850. nr_groups);
  851. return NULL;
  852. }
  853. bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
  854. bitmap->s_nr_groups = nr_groups;
  855. return bitmap;
  856. }
  857. static int udf_fill_partdesc_info(struct super_block *sb,
  858. struct partitionDesc *p, int p_index)
  859. {
  860. struct udf_part_map *map;
  861. struct udf_sb_info *sbi = UDF_SB(sb);
  862. struct partitionHeaderDesc *phd;
  863. map = &sbi->s_partmaps[p_index];
  864. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  865. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  866. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  867. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  868. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  869. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  870. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  871. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  872. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  873. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  874. udf_debug("Partition (%d type %x) starts at physical %d, block length %d\n",
  875. p_index, map->s_partition_type,
  876. map->s_partition_root, map->s_partition_len);
  877. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  878. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  879. return 0;
  880. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  881. if (phd->unallocSpaceTable.extLength) {
  882. struct kernel_lb_addr loc = {
  883. .logicalBlockNum = le32_to_cpu(
  884. phd->unallocSpaceTable.extPosition),
  885. .partitionReferenceNum = p_index,
  886. };
  887. map->s_uspace.s_table = udf_iget(sb, &loc);
  888. if (!map->s_uspace.s_table) {
  889. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  890. p_index);
  891. return 1;
  892. }
  893. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  894. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  895. p_index, map->s_uspace.s_table->i_ino);
  896. }
  897. if (phd->unallocSpaceBitmap.extLength) {
  898. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  899. if (!bitmap)
  900. return 1;
  901. map->s_uspace.s_bitmap = bitmap;
  902. bitmap->s_extLength = le32_to_cpu(
  903. phd->unallocSpaceBitmap.extLength);
  904. bitmap->s_extPosition = le32_to_cpu(
  905. phd->unallocSpaceBitmap.extPosition);
  906. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  907. udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
  908. p_index, bitmap->s_extPosition);
  909. }
  910. if (phd->partitionIntegrityTable.extLength)
  911. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  912. if (phd->freedSpaceTable.extLength) {
  913. struct kernel_lb_addr loc = {
  914. .logicalBlockNum = le32_to_cpu(
  915. phd->freedSpaceTable.extPosition),
  916. .partitionReferenceNum = p_index,
  917. };
  918. map->s_fspace.s_table = udf_iget(sb, &loc);
  919. if (!map->s_fspace.s_table) {
  920. udf_debug("cannot load freedSpaceTable (part %d)\n",
  921. p_index);
  922. return 1;
  923. }
  924. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  925. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  926. p_index, map->s_fspace.s_table->i_ino);
  927. }
  928. if (phd->freedSpaceBitmap.extLength) {
  929. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  930. if (!bitmap)
  931. return 1;
  932. map->s_fspace.s_bitmap = bitmap;
  933. bitmap->s_extLength = le32_to_cpu(
  934. phd->freedSpaceBitmap.extLength);
  935. bitmap->s_extPosition = le32_to_cpu(
  936. phd->freedSpaceBitmap.extPosition);
  937. map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
  938. udf_debug("freedSpaceBitmap (part %d) @ %d\n",
  939. p_index, bitmap->s_extPosition);
  940. }
  941. return 0;
  942. }
  943. static void udf_find_vat_block(struct super_block *sb, int p_index,
  944. int type1_index, sector_t start_block)
  945. {
  946. struct udf_sb_info *sbi = UDF_SB(sb);
  947. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  948. sector_t vat_block;
  949. struct kernel_lb_addr ino;
  950. /*
  951. * VAT file entry is in the last recorded block. Some broken disks have
  952. * it a few blocks before so try a bit harder...
  953. */
  954. ino.partitionReferenceNum = type1_index;
  955. for (vat_block = start_block;
  956. vat_block >= map->s_partition_root &&
  957. vat_block >= start_block - 3 &&
  958. !sbi->s_vat_inode; vat_block--) {
  959. ino.logicalBlockNum = vat_block - map->s_partition_root;
  960. sbi->s_vat_inode = udf_iget(sb, &ino);
  961. }
  962. }
  963. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  964. {
  965. struct udf_sb_info *sbi = UDF_SB(sb);
  966. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  967. struct buffer_head *bh = NULL;
  968. struct udf_inode_info *vati;
  969. uint32_t pos;
  970. struct virtualAllocationTable20 *vat20;
  971. sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  972. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  973. if (!sbi->s_vat_inode &&
  974. sbi->s_last_block != blocks - 1) {
  975. pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
  976. (unsigned long)sbi->s_last_block,
  977. (unsigned long)blocks - 1);
  978. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  979. }
  980. if (!sbi->s_vat_inode)
  981. return 1;
  982. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  983. map->s_type_specific.s_virtual.s_start_offset = 0;
  984. map->s_type_specific.s_virtual.s_num_entries =
  985. (sbi->s_vat_inode->i_size - 36) >> 2;
  986. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  987. vati = UDF_I(sbi->s_vat_inode);
  988. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  989. pos = udf_block_map(sbi->s_vat_inode, 0);
  990. bh = sb_bread(sb, pos);
  991. if (!bh)
  992. return 1;
  993. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  994. } else {
  995. vat20 = (struct virtualAllocationTable20 *)
  996. vati->i_ext.i_data;
  997. }
  998. map->s_type_specific.s_virtual.s_start_offset =
  999. le16_to_cpu(vat20->lengthHeader);
  1000. map->s_type_specific.s_virtual.s_num_entries =
  1001. (sbi->s_vat_inode->i_size -
  1002. map->s_type_specific.s_virtual.
  1003. s_start_offset) >> 2;
  1004. brelse(bh);
  1005. }
  1006. return 0;
  1007. }
  1008. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1009. {
  1010. struct buffer_head *bh;
  1011. struct partitionDesc *p;
  1012. struct udf_part_map *map;
  1013. struct udf_sb_info *sbi = UDF_SB(sb);
  1014. int i, type1_idx;
  1015. uint16_t partitionNumber;
  1016. uint16_t ident;
  1017. int ret = 0;
  1018. bh = udf_read_tagged(sb, block, block, &ident);
  1019. if (!bh)
  1020. return 1;
  1021. if (ident != TAG_IDENT_PD)
  1022. goto out_bh;
  1023. p = (struct partitionDesc *)bh->b_data;
  1024. partitionNumber = le16_to_cpu(p->partitionNumber);
  1025. /* First scan for TYPE1, SPARABLE and METADATA partitions */
  1026. for (i = 0; i < sbi->s_partitions; i++) {
  1027. map = &sbi->s_partmaps[i];
  1028. udf_debug("Searching map: (%d == %d)\n",
  1029. map->s_partition_num, partitionNumber);
  1030. if (map->s_partition_num == partitionNumber &&
  1031. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1032. map->s_partition_type == UDF_SPARABLE_MAP15))
  1033. break;
  1034. }
  1035. if (i >= sbi->s_partitions) {
  1036. udf_debug("Partition (%d) not found in partition map\n",
  1037. partitionNumber);
  1038. goto out_bh;
  1039. }
  1040. ret = udf_fill_partdesc_info(sb, p, i);
  1041. /*
  1042. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1043. * PHYSICAL partitions are already set up
  1044. */
  1045. type1_idx = i;
  1046. for (i = 0; i < sbi->s_partitions; i++) {
  1047. map = &sbi->s_partmaps[i];
  1048. if (map->s_partition_num == partitionNumber &&
  1049. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1050. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1051. map->s_partition_type == UDF_METADATA_MAP25))
  1052. break;
  1053. }
  1054. if (i >= sbi->s_partitions)
  1055. goto out_bh;
  1056. ret = udf_fill_partdesc_info(sb, p, i);
  1057. if (ret)
  1058. goto out_bh;
  1059. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1060. ret = udf_load_metadata_files(sb, i);
  1061. if (ret) {
  1062. udf_err(sb, "error loading MetaData partition map %d\n",
  1063. i);
  1064. goto out_bh;
  1065. }
  1066. } else {
  1067. ret = udf_load_vat(sb, i, type1_idx);
  1068. if (ret)
  1069. goto out_bh;
  1070. /*
  1071. * Mark filesystem read-only if we have a partition with
  1072. * virtual map since we don't handle writing to it (we
  1073. * overwrite blocks instead of relocating them).
  1074. */
  1075. sb->s_flags |= MS_RDONLY;
  1076. pr_notice("Filesystem marked read-only because writing to pseudooverwrite partition is not implemented\n");
  1077. }
  1078. out_bh:
  1079. /* In case loading failed, we handle cleanup in udf_fill_super */
  1080. brelse(bh);
  1081. return ret;
  1082. }
  1083. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1084. struct kernel_lb_addr *fileset)
  1085. {
  1086. struct logicalVolDesc *lvd;
  1087. int i, j, offset;
  1088. uint8_t type;
  1089. struct udf_sb_info *sbi = UDF_SB(sb);
  1090. struct genericPartitionMap *gpm;
  1091. uint16_t ident;
  1092. struct buffer_head *bh;
  1093. int ret = 0;
  1094. bh = udf_read_tagged(sb, block, block, &ident);
  1095. if (!bh)
  1096. return 1;
  1097. BUG_ON(ident != TAG_IDENT_LVD);
  1098. lvd = (struct logicalVolDesc *)bh->b_data;
  1099. i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1100. if (i != 0) {
  1101. ret = i;
  1102. goto out_bh;
  1103. }
  1104. for (i = 0, offset = 0;
  1105. i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
  1106. i++, offset += gpm->partitionMapLength) {
  1107. struct udf_part_map *map = &sbi->s_partmaps[i];
  1108. gpm = (struct genericPartitionMap *)
  1109. &(lvd->partitionMaps[offset]);
  1110. type = gpm->partitionMapType;
  1111. if (type == 1) {
  1112. struct genericPartitionMap1 *gpm1 =
  1113. (struct genericPartitionMap1 *)gpm;
  1114. map->s_partition_type = UDF_TYPE1_MAP15;
  1115. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1116. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1117. map->s_partition_func = NULL;
  1118. } else if (type == 2) {
  1119. struct udfPartitionMap2 *upm2 =
  1120. (struct udfPartitionMap2 *)gpm;
  1121. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1122. strlen(UDF_ID_VIRTUAL))) {
  1123. u16 suf =
  1124. le16_to_cpu(((__le16 *)upm2->partIdent.
  1125. identSuffix)[0]);
  1126. if (suf < 0x0200) {
  1127. map->s_partition_type =
  1128. UDF_VIRTUAL_MAP15;
  1129. map->s_partition_func =
  1130. udf_get_pblock_virt15;
  1131. } else {
  1132. map->s_partition_type =
  1133. UDF_VIRTUAL_MAP20;
  1134. map->s_partition_func =
  1135. udf_get_pblock_virt20;
  1136. }
  1137. } else if (!strncmp(upm2->partIdent.ident,
  1138. UDF_ID_SPARABLE,
  1139. strlen(UDF_ID_SPARABLE))) {
  1140. uint32_t loc;
  1141. struct sparingTable *st;
  1142. struct sparablePartitionMap *spm =
  1143. (struct sparablePartitionMap *)gpm;
  1144. map->s_partition_type = UDF_SPARABLE_MAP15;
  1145. map->s_type_specific.s_sparing.s_packet_len =
  1146. le16_to_cpu(spm->packetLength);
  1147. for (j = 0; j < spm->numSparingTables; j++) {
  1148. struct buffer_head *bh2;
  1149. loc = le32_to_cpu(
  1150. spm->locSparingTable[j]);
  1151. bh2 = udf_read_tagged(sb, loc, loc,
  1152. &ident);
  1153. map->s_type_specific.s_sparing.
  1154. s_spar_map[j] = bh2;
  1155. if (bh2 == NULL)
  1156. continue;
  1157. st = (struct sparingTable *)bh2->b_data;
  1158. if (ident != 0 || strncmp(
  1159. st->sparingIdent.ident,
  1160. UDF_ID_SPARING,
  1161. strlen(UDF_ID_SPARING))) {
  1162. brelse(bh2);
  1163. map->s_type_specific.s_sparing.
  1164. s_spar_map[j] = NULL;
  1165. }
  1166. }
  1167. map->s_partition_func = udf_get_pblock_spar15;
  1168. } else if (!strncmp(upm2->partIdent.ident,
  1169. UDF_ID_METADATA,
  1170. strlen(UDF_ID_METADATA))) {
  1171. struct udf_meta_data *mdata =
  1172. &map->s_type_specific.s_metadata;
  1173. struct metadataPartitionMap *mdm =
  1174. (struct metadataPartitionMap *)
  1175. &(lvd->partitionMaps[offset]);
  1176. udf_debug("Parsing Logical vol part %d type %d id=%s\n",
  1177. i, type, UDF_ID_METADATA);
  1178. map->s_partition_type = UDF_METADATA_MAP25;
  1179. map->s_partition_func = udf_get_pblock_meta25;
  1180. mdata->s_meta_file_loc =
  1181. le32_to_cpu(mdm->metadataFileLoc);
  1182. mdata->s_mirror_file_loc =
  1183. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1184. mdata->s_bitmap_file_loc =
  1185. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1186. mdata->s_alloc_unit_size =
  1187. le32_to_cpu(mdm->allocUnitSize);
  1188. mdata->s_align_unit_size =
  1189. le16_to_cpu(mdm->alignUnitSize);
  1190. mdata->s_dup_md_flag =
  1191. mdm->flags & 0x01;
  1192. udf_debug("Metadata Ident suffix=0x%x\n",
  1193. le16_to_cpu(*(__le16 *)
  1194. mdm->partIdent.identSuffix));
  1195. udf_debug("Metadata part num=%d\n",
  1196. le16_to_cpu(mdm->partitionNum));
  1197. udf_debug("Metadata part alloc unit size=%d\n",
  1198. le32_to_cpu(mdm->allocUnitSize));
  1199. udf_debug("Metadata file loc=%d\n",
  1200. le32_to_cpu(mdm->metadataFileLoc));
  1201. udf_debug("Mirror file loc=%d\n",
  1202. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1203. udf_debug("Bitmap file loc=%d\n",
  1204. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1205. udf_debug("Duplicate Flag: %d %d\n",
  1206. mdata->s_dup_md_flag, mdm->flags);
  1207. } else {
  1208. udf_debug("Unknown ident: %s\n",
  1209. upm2->partIdent.ident);
  1210. continue;
  1211. }
  1212. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1213. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1214. }
  1215. udf_debug("Partition (%d:%d) type %d on volume %d\n",
  1216. i, map->s_partition_num, type, map->s_volumeseqnum);
  1217. }
  1218. if (fileset) {
  1219. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1220. *fileset = lelb_to_cpu(la->extLocation);
  1221. udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
  1222. fileset->logicalBlockNum,
  1223. fileset->partitionReferenceNum);
  1224. }
  1225. if (lvd->integritySeqExt.extLength)
  1226. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1227. out_bh:
  1228. brelse(bh);
  1229. return ret;
  1230. }
  1231. /*
  1232. * udf_load_logicalvolint
  1233. *
  1234. */
  1235. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1236. {
  1237. struct buffer_head *bh = NULL;
  1238. uint16_t ident;
  1239. struct udf_sb_info *sbi = UDF_SB(sb);
  1240. struct logicalVolIntegrityDesc *lvid;
  1241. while (loc.extLength > 0 &&
  1242. (bh = udf_read_tagged(sb, loc.extLocation,
  1243. loc.extLocation, &ident)) &&
  1244. ident == TAG_IDENT_LVID) {
  1245. sbi->s_lvid_bh = bh;
  1246. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1247. if (lvid->nextIntegrityExt.extLength)
  1248. udf_load_logicalvolint(sb,
  1249. leea_to_cpu(lvid->nextIntegrityExt));
  1250. if (sbi->s_lvid_bh != bh)
  1251. brelse(bh);
  1252. loc.extLength -= sb->s_blocksize;
  1253. loc.extLocation++;
  1254. }
  1255. if (sbi->s_lvid_bh != bh)
  1256. brelse(bh);
  1257. }
  1258. /*
  1259. * udf_process_sequence
  1260. *
  1261. * PURPOSE
  1262. * Process a main/reserve volume descriptor sequence.
  1263. *
  1264. * PRE-CONDITIONS
  1265. * sb Pointer to _locked_ superblock.
  1266. * block First block of first extent of the sequence.
  1267. * lastblock Lastblock of first extent of the sequence.
  1268. *
  1269. * HISTORY
  1270. * July 1, 1997 - Andrew E. Mileski
  1271. * Written, tested, and released.
  1272. */
  1273. static noinline int udf_process_sequence(struct super_block *sb, long block,
  1274. long lastblock, struct kernel_lb_addr *fileset)
  1275. {
  1276. struct buffer_head *bh = NULL;
  1277. struct udf_vds_record vds[VDS_POS_LENGTH];
  1278. struct udf_vds_record *curr;
  1279. struct generic_desc *gd;
  1280. struct volDescPtr *vdp;
  1281. int done = 0;
  1282. uint32_t vdsn;
  1283. uint16_t ident;
  1284. long next_s = 0, next_e = 0;
  1285. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1286. /*
  1287. * Read the main descriptor sequence and find which descriptors
  1288. * are in it.
  1289. */
  1290. for (; (!done && block <= lastblock); block++) {
  1291. bh = udf_read_tagged(sb, block, block, &ident);
  1292. if (!bh) {
  1293. udf_err(sb,
  1294. "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
  1295. (unsigned long long)block);
  1296. return 1;
  1297. }
  1298. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1299. gd = (struct generic_desc *)bh->b_data;
  1300. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1301. switch (ident) {
  1302. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1303. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1304. if (vdsn >= curr->volDescSeqNum) {
  1305. curr->volDescSeqNum = vdsn;
  1306. curr->block = block;
  1307. }
  1308. break;
  1309. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1310. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1311. if (vdsn >= curr->volDescSeqNum) {
  1312. curr->volDescSeqNum = vdsn;
  1313. curr->block = block;
  1314. vdp = (struct volDescPtr *)bh->b_data;
  1315. next_s = le32_to_cpu(
  1316. vdp->nextVolDescSeqExt.extLocation);
  1317. next_e = le32_to_cpu(
  1318. vdp->nextVolDescSeqExt.extLength);
  1319. next_e = next_e >> sb->s_blocksize_bits;
  1320. next_e += next_s;
  1321. }
  1322. break;
  1323. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1324. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1325. if (vdsn >= curr->volDescSeqNum) {
  1326. curr->volDescSeqNum = vdsn;
  1327. curr->block = block;
  1328. }
  1329. break;
  1330. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1331. curr = &vds[VDS_POS_PARTITION_DESC];
  1332. if (!curr->block)
  1333. curr->block = block;
  1334. break;
  1335. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1336. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1337. if (vdsn >= curr->volDescSeqNum) {
  1338. curr->volDescSeqNum = vdsn;
  1339. curr->block = block;
  1340. }
  1341. break;
  1342. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1343. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1344. if (vdsn >= curr->volDescSeqNum) {
  1345. curr->volDescSeqNum = vdsn;
  1346. curr->block = block;
  1347. }
  1348. break;
  1349. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1350. vds[VDS_POS_TERMINATING_DESC].block = block;
  1351. if (next_e) {
  1352. block = next_s;
  1353. lastblock = next_e;
  1354. next_s = next_e = 0;
  1355. } else
  1356. done = 1;
  1357. break;
  1358. }
  1359. brelse(bh);
  1360. }
  1361. /*
  1362. * Now read interesting descriptors again and process them
  1363. * in a suitable order
  1364. */
  1365. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1366. udf_err(sb, "Primary Volume Descriptor not found!\n");
  1367. return 1;
  1368. }
  1369. if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
  1370. return 1;
  1371. if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
  1372. vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
  1373. return 1;
  1374. if (vds[VDS_POS_PARTITION_DESC].block) {
  1375. /*
  1376. * We rescan the whole descriptor sequence to find
  1377. * partition descriptor blocks and process them.
  1378. */
  1379. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1380. block < vds[VDS_POS_TERMINATING_DESC].block;
  1381. block++)
  1382. if (udf_load_partdesc(sb, block))
  1383. return 1;
  1384. }
  1385. return 0;
  1386. }
  1387. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1388. struct kernel_lb_addr *fileset)
  1389. {
  1390. struct anchorVolDescPtr *anchor;
  1391. long main_s, main_e, reserve_s, reserve_e;
  1392. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1393. /* Locate the main sequence */
  1394. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1395. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1396. main_e = main_e >> sb->s_blocksize_bits;
  1397. main_e += main_s;
  1398. /* Locate the reserve sequence */
  1399. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1400. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1401. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1402. reserve_e += reserve_s;
  1403. /* Process the main & reserve sequences */
  1404. /* responsible for finding the PartitionDesc(s) */
  1405. if (!udf_process_sequence(sb, main_s, main_e, fileset))
  1406. return 1;
  1407. return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1408. }
  1409. /*
  1410. * Check whether there is an anchor block in the given block and
  1411. * load Volume Descriptor Sequence if so.
  1412. */
  1413. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1414. struct kernel_lb_addr *fileset)
  1415. {
  1416. struct buffer_head *bh;
  1417. uint16_t ident;
  1418. int ret;
  1419. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1420. udf_fixed_to_variable(block) >=
  1421. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1422. return 0;
  1423. bh = udf_read_tagged(sb, block, block, &ident);
  1424. if (!bh)
  1425. return 0;
  1426. if (ident != TAG_IDENT_AVDP) {
  1427. brelse(bh);
  1428. return 0;
  1429. }
  1430. ret = udf_load_sequence(sb, bh, fileset);
  1431. brelse(bh);
  1432. return ret;
  1433. }
  1434. /* Search for an anchor volume descriptor pointer */
  1435. static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
  1436. struct kernel_lb_addr *fileset)
  1437. {
  1438. sector_t last[6];
  1439. int i;
  1440. struct udf_sb_info *sbi = UDF_SB(sb);
  1441. int last_count = 0;
  1442. /* First try user provided anchor */
  1443. if (sbi->s_anchor) {
  1444. if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
  1445. return lastblock;
  1446. }
  1447. /*
  1448. * according to spec, anchor is in either:
  1449. * block 256
  1450. * lastblock-256
  1451. * lastblock
  1452. * however, if the disc isn't closed, it could be 512.
  1453. */
  1454. if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
  1455. return lastblock;
  1456. /*
  1457. * The trouble is which block is the last one. Drives often misreport
  1458. * this so we try various possibilities.
  1459. */
  1460. last[last_count++] = lastblock;
  1461. if (lastblock >= 1)
  1462. last[last_count++] = lastblock - 1;
  1463. last[last_count++] = lastblock + 1;
  1464. if (lastblock >= 2)
  1465. last[last_count++] = lastblock - 2;
  1466. if (lastblock >= 150)
  1467. last[last_count++] = lastblock - 150;
  1468. if (lastblock >= 152)
  1469. last[last_count++] = lastblock - 152;
  1470. for (i = 0; i < last_count; i++) {
  1471. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1472. sb->s_blocksize_bits)
  1473. continue;
  1474. if (udf_check_anchor_block(sb, last[i], fileset))
  1475. return last[i];
  1476. if (last[i] < 256)
  1477. continue;
  1478. if (udf_check_anchor_block(sb, last[i] - 256, fileset))
  1479. return last[i];
  1480. }
  1481. /* Finally try block 512 in case media is open */
  1482. if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
  1483. return last[0];
  1484. return 0;
  1485. }
  1486. /*
  1487. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1488. * area specified by it. The function expects sbi->s_lastblock to be the last
  1489. * block on the media.
  1490. *
  1491. * Return 1 if ok, 0 if not found.
  1492. *
  1493. */
  1494. static int udf_find_anchor(struct super_block *sb,
  1495. struct kernel_lb_addr *fileset)
  1496. {
  1497. sector_t lastblock;
  1498. struct udf_sb_info *sbi = UDF_SB(sb);
  1499. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1500. if (lastblock)
  1501. goto out;
  1502. /* No anchor found? Try VARCONV conversion of block numbers */
  1503. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1504. /* Firstly, we try to not convert number of the last block */
  1505. lastblock = udf_scan_anchors(sb,
  1506. udf_variable_to_fixed(sbi->s_last_block),
  1507. fileset);
  1508. if (lastblock)
  1509. goto out;
  1510. /* Secondly, we try with converted number of the last block */
  1511. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1512. if (!lastblock) {
  1513. /* VARCONV didn't help. Clear it. */
  1514. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1515. return 0;
  1516. }
  1517. out:
  1518. sbi->s_last_block = lastblock;
  1519. return 1;
  1520. }
  1521. /*
  1522. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1523. * Descriptor Sequence
  1524. */
  1525. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1526. int silent, struct kernel_lb_addr *fileset)
  1527. {
  1528. struct udf_sb_info *sbi = UDF_SB(sb);
  1529. loff_t nsr_off;
  1530. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1531. if (!silent)
  1532. udf_warn(sb, "Bad block size\n");
  1533. return 0;
  1534. }
  1535. sbi->s_last_block = uopt->lastblock;
  1536. if (!uopt->novrs) {
  1537. /* Check that it is NSR02 compliant */
  1538. nsr_off = udf_check_vsd(sb);
  1539. if (!nsr_off) {
  1540. if (!silent)
  1541. udf_warn(sb, "No VRS found\n");
  1542. return 0;
  1543. }
  1544. if (nsr_off == -1)
  1545. udf_debug("Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
  1546. if (!sbi->s_last_block)
  1547. sbi->s_last_block = udf_get_last_block(sb);
  1548. } else {
  1549. udf_debug("Validity check skipped because of novrs option\n");
  1550. }
  1551. /* Look for anchor block and load Volume Descriptor Sequence */
  1552. sbi->s_anchor = uopt->anchor;
  1553. if (!udf_find_anchor(sb, fileset)) {
  1554. if (!silent)
  1555. udf_warn(sb, "No anchor found\n");
  1556. return 0;
  1557. }
  1558. return 1;
  1559. }
  1560. static void udf_open_lvid(struct super_block *sb)
  1561. {
  1562. struct udf_sb_info *sbi = UDF_SB(sb);
  1563. struct buffer_head *bh = sbi->s_lvid_bh;
  1564. struct logicalVolIntegrityDesc *lvid;
  1565. struct logicalVolIntegrityDescImpUse *lvidiu;
  1566. if (!bh)
  1567. return;
  1568. mutex_lock(&sbi->s_alloc_mutex);
  1569. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1570. lvidiu = udf_sb_lvidiu(sbi);
  1571. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1572. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1573. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1574. CURRENT_TIME);
  1575. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1576. lvid->descTag.descCRC = cpu_to_le16(
  1577. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1578. le16_to_cpu(lvid->descTag.descCRCLength)));
  1579. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1580. mark_buffer_dirty(bh);
  1581. sbi->s_lvid_dirty = 0;
  1582. mutex_unlock(&sbi->s_alloc_mutex);
  1583. }
  1584. static void udf_close_lvid(struct super_block *sb)
  1585. {
  1586. struct udf_sb_info *sbi = UDF_SB(sb);
  1587. struct buffer_head *bh = sbi->s_lvid_bh;
  1588. struct logicalVolIntegrityDesc *lvid;
  1589. struct logicalVolIntegrityDescImpUse *lvidiu;
  1590. if (!bh)
  1591. return;
  1592. mutex_lock(&sbi->s_alloc_mutex);
  1593. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1594. lvidiu = udf_sb_lvidiu(sbi);
  1595. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1596. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1597. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1598. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1599. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1600. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1601. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1602. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1603. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1604. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1605. lvid->descTag.descCRC = cpu_to_le16(
  1606. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1607. le16_to_cpu(lvid->descTag.descCRCLength)));
  1608. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1609. mark_buffer_dirty(bh);
  1610. sbi->s_lvid_dirty = 0;
  1611. mutex_unlock(&sbi->s_alloc_mutex);
  1612. }
  1613. u64 lvid_get_unique_id(struct super_block *sb)
  1614. {
  1615. struct buffer_head *bh;
  1616. struct udf_sb_info *sbi = UDF_SB(sb);
  1617. struct logicalVolIntegrityDesc *lvid;
  1618. struct logicalVolHeaderDesc *lvhd;
  1619. u64 uniqueID;
  1620. u64 ret;
  1621. bh = sbi->s_lvid_bh;
  1622. if (!bh)
  1623. return 0;
  1624. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1625. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1626. mutex_lock(&sbi->s_alloc_mutex);
  1627. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1628. if (!(++uniqueID & 0xFFFFFFFF))
  1629. uniqueID += 16;
  1630. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1631. mutex_unlock(&sbi->s_alloc_mutex);
  1632. mark_buffer_dirty(bh);
  1633. return ret;
  1634. }
  1635. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  1636. {
  1637. int i;
  1638. int nr_groups = bitmap->s_nr_groups;
  1639. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  1640. nr_groups);
  1641. for (i = 0; i < nr_groups; i++)
  1642. if (bitmap->s_block_bitmap[i])
  1643. brelse(bitmap->s_block_bitmap[i]);
  1644. if (size <= PAGE_SIZE)
  1645. kfree(bitmap);
  1646. else
  1647. vfree(bitmap);
  1648. }
  1649. static void udf_free_partition(struct udf_part_map *map)
  1650. {
  1651. int i;
  1652. struct udf_meta_data *mdata;
  1653. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  1654. iput(map->s_uspace.s_table);
  1655. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  1656. iput(map->s_fspace.s_table);
  1657. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  1658. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  1659. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  1660. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  1661. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  1662. for (i = 0; i < 4; i++)
  1663. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  1664. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  1665. mdata = &map->s_type_specific.s_metadata;
  1666. iput(mdata->s_metadata_fe);
  1667. mdata->s_metadata_fe = NULL;
  1668. iput(mdata->s_mirror_fe);
  1669. mdata->s_mirror_fe = NULL;
  1670. iput(mdata->s_bitmap_fe);
  1671. mdata->s_bitmap_fe = NULL;
  1672. }
  1673. }
  1674. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1675. {
  1676. int i;
  1677. int ret;
  1678. struct inode *inode = NULL;
  1679. struct udf_options uopt;
  1680. struct kernel_lb_addr rootdir, fileset;
  1681. struct udf_sb_info *sbi;
  1682. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1683. uopt.uid = -1;
  1684. uopt.gid = -1;
  1685. uopt.umask = 0;
  1686. uopt.fmode = UDF_INVALID_MODE;
  1687. uopt.dmode = UDF_INVALID_MODE;
  1688. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1689. if (!sbi)
  1690. return -ENOMEM;
  1691. sb->s_fs_info = sbi;
  1692. mutex_init(&sbi->s_alloc_mutex);
  1693. if (!udf_parse_options((char *)options, &uopt, false))
  1694. goto error_out;
  1695. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1696. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1697. udf_err(sb, "utf8 cannot be combined with iocharset\n");
  1698. goto error_out;
  1699. }
  1700. #ifdef CONFIG_UDF_NLS
  1701. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1702. uopt.nls_map = load_nls_default();
  1703. if (!uopt.nls_map)
  1704. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1705. else
  1706. udf_debug("Using default NLS map\n");
  1707. }
  1708. #endif
  1709. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1710. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1711. fileset.logicalBlockNum = 0xFFFFFFFF;
  1712. fileset.partitionReferenceNum = 0xFFFF;
  1713. sbi->s_flags = uopt.flags;
  1714. sbi->s_uid = uopt.uid;
  1715. sbi->s_gid = uopt.gid;
  1716. sbi->s_umask = uopt.umask;
  1717. sbi->s_fmode = uopt.fmode;
  1718. sbi->s_dmode = uopt.dmode;
  1719. sbi->s_nls_map = uopt.nls_map;
  1720. rwlock_init(&sbi->s_cred_lock);
  1721. if (uopt.session == 0xFFFFFFFF)
  1722. sbi->s_session = udf_get_last_session(sb);
  1723. else
  1724. sbi->s_session = uopt.session;
  1725. udf_debug("Multi-session=%d\n", sbi->s_session);
  1726. /* Fill in the rest of the superblock */
  1727. sb->s_op = &udf_sb_ops;
  1728. sb->s_export_op = &udf_export_ops;
  1729. sb->s_dirt = 0;
  1730. sb->s_magic = UDF_SUPER_MAGIC;
  1731. sb->s_time_gran = 1000;
  1732. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1733. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1734. } else {
  1735. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1736. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1737. if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1738. if (!silent)
  1739. pr_notice("Rescanning with blocksize %d\n",
  1740. UDF_DEFAULT_BLOCKSIZE);
  1741. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1742. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1743. }
  1744. }
  1745. if (!ret) {
  1746. udf_warn(sb, "No partition found (1)\n");
  1747. goto error_out;
  1748. }
  1749. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1750. if (sbi->s_lvid_bh) {
  1751. struct logicalVolIntegrityDescImpUse *lvidiu =
  1752. udf_sb_lvidiu(sbi);
  1753. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1754. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1755. /* uint16_t maxUDFWriteRev =
  1756. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1757. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1758. udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
  1759. le16_to_cpu(lvidiu->minUDFReadRev),
  1760. UDF_MAX_READ_VERSION);
  1761. goto error_out;
  1762. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1763. sb->s_flags |= MS_RDONLY;
  1764. sbi->s_udfrev = minUDFWriteRev;
  1765. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1766. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1767. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1768. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1769. }
  1770. if (!sbi->s_partitions) {
  1771. udf_warn(sb, "No partition found (2)\n");
  1772. goto error_out;
  1773. }
  1774. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1775. UDF_PART_FLAG_READ_ONLY) {
  1776. pr_notice("Partition marked readonly; forcing readonly mount\n");
  1777. sb->s_flags |= MS_RDONLY;
  1778. }
  1779. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1780. udf_warn(sb, "No fileset found\n");
  1781. goto error_out;
  1782. }
  1783. if (!silent) {
  1784. struct timestamp ts;
  1785. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1786. udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1787. sbi->s_volume_ident,
  1788. le16_to_cpu(ts.year), ts.month, ts.day,
  1789. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1790. }
  1791. if (!(sb->s_flags & MS_RDONLY))
  1792. udf_open_lvid(sb);
  1793. /* Assign the root inode */
  1794. /* assign inodes by physical block number */
  1795. /* perhaps it's not extensible enough, but for now ... */
  1796. inode = udf_iget(sb, &rootdir);
  1797. if (!inode) {
  1798. udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
  1799. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1800. goto error_out;
  1801. }
  1802. /* Allocate a dentry for the root inode */
  1803. sb->s_root = d_alloc_root(inode);
  1804. if (!sb->s_root) {
  1805. udf_err(sb, "Couldn't allocate root dentry\n");
  1806. iput(inode);
  1807. goto error_out;
  1808. }
  1809. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1810. return 0;
  1811. error_out:
  1812. if (sbi->s_vat_inode)
  1813. iput(sbi->s_vat_inode);
  1814. if (sbi->s_partitions)
  1815. for (i = 0; i < sbi->s_partitions; i++)
  1816. udf_free_partition(&sbi->s_partmaps[i]);
  1817. #ifdef CONFIG_UDF_NLS
  1818. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1819. unload_nls(sbi->s_nls_map);
  1820. #endif
  1821. if (!(sb->s_flags & MS_RDONLY))
  1822. udf_close_lvid(sb);
  1823. brelse(sbi->s_lvid_bh);
  1824. kfree(sbi->s_partmaps);
  1825. kfree(sbi);
  1826. sb->s_fs_info = NULL;
  1827. return -EINVAL;
  1828. }
  1829. void _udf_err(struct super_block *sb, const char *function,
  1830. const char *fmt, ...)
  1831. {
  1832. struct va_format vaf;
  1833. va_list args;
  1834. /* mark sb error */
  1835. if (!(sb->s_flags & MS_RDONLY))
  1836. sb->s_dirt = 1;
  1837. va_start(args, fmt);
  1838. vaf.fmt = fmt;
  1839. vaf.va = &args;
  1840. pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
  1841. va_end(args);
  1842. }
  1843. void _udf_warn(struct super_block *sb, const char *function,
  1844. const char *fmt, ...)
  1845. {
  1846. struct va_format vaf;
  1847. va_list args;
  1848. va_start(args, fmt);
  1849. vaf.fmt = fmt;
  1850. vaf.va = &args;
  1851. pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
  1852. va_end(args);
  1853. }
  1854. static void udf_put_super(struct super_block *sb)
  1855. {
  1856. int i;
  1857. struct udf_sb_info *sbi;
  1858. sbi = UDF_SB(sb);
  1859. if (sbi->s_vat_inode)
  1860. iput(sbi->s_vat_inode);
  1861. if (sbi->s_partitions)
  1862. for (i = 0; i < sbi->s_partitions; i++)
  1863. udf_free_partition(&sbi->s_partmaps[i]);
  1864. #ifdef CONFIG_UDF_NLS
  1865. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1866. unload_nls(sbi->s_nls_map);
  1867. #endif
  1868. if (!(sb->s_flags & MS_RDONLY))
  1869. udf_close_lvid(sb);
  1870. brelse(sbi->s_lvid_bh);
  1871. kfree(sbi->s_partmaps);
  1872. kfree(sb->s_fs_info);
  1873. sb->s_fs_info = NULL;
  1874. }
  1875. static int udf_sync_fs(struct super_block *sb, int wait)
  1876. {
  1877. struct udf_sb_info *sbi = UDF_SB(sb);
  1878. mutex_lock(&sbi->s_alloc_mutex);
  1879. if (sbi->s_lvid_dirty) {
  1880. /*
  1881. * Blockdevice will be synced later so we don't have to submit
  1882. * the buffer for IO
  1883. */
  1884. mark_buffer_dirty(sbi->s_lvid_bh);
  1885. sb->s_dirt = 0;
  1886. sbi->s_lvid_dirty = 0;
  1887. }
  1888. mutex_unlock(&sbi->s_alloc_mutex);
  1889. return 0;
  1890. }
  1891. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1892. {
  1893. struct super_block *sb = dentry->d_sb;
  1894. struct udf_sb_info *sbi = UDF_SB(sb);
  1895. struct logicalVolIntegrityDescImpUse *lvidiu;
  1896. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1897. if (sbi->s_lvid_bh != NULL)
  1898. lvidiu = udf_sb_lvidiu(sbi);
  1899. else
  1900. lvidiu = NULL;
  1901. buf->f_type = UDF_SUPER_MAGIC;
  1902. buf->f_bsize = sb->s_blocksize;
  1903. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1904. buf->f_bfree = udf_count_free(sb);
  1905. buf->f_bavail = buf->f_bfree;
  1906. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1907. le32_to_cpu(lvidiu->numDirs)) : 0)
  1908. + buf->f_bfree;
  1909. buf->f_ffree = buf->f_bfree;
  1910. buf->f_namelen = UDF_NAME_LEN - 2;
  1911. buf->f_fsid.val[0] = (u32)id;
  1912. buf->f_fsid.val[1] = (u32)(id >> 32);
  1913. return 0;
  1914. }
  1915. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1916. struct udf_bitmap *bitmap)
  1917. {
  1918. struct buffer_head *bh = NULL;
  1919. unsigned int accum = 0;
  1920. int index;
  1921. int block = 0, newblock;
  1922. struct kernel_lb_addr loc;
  1923. uint32_t bytes;
  1924. uint8_t *ptr;
  1925. uint16_t ident;
  1926. struct spaceBitmapDesc *bm;
  1927. loc.logicalBlockNum = bitmap->s_extPosition;
  1928. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1929. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  1930. if (!bh) {
  1931. udf_err(sb, "udf_count_free failed\n");
  1932. goto out;
  1933. } else if (ident != TAG_IDENT_SBD) {
  1934. brelse(bh);
  1935. udf_err(sb, "udf_count_free failed\n");
  1936. goto out;
  1937. }
  1938. bm = (struct spaceBitmapDesc *)bh->b_data;
  1939. bytes = le32_to_cpu(bm->numOfBytes);
  1940. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1941. ptr = (uint8_t *)bh->b_data;
  1942. while (bytes > 0) {
  1943. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1944. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1945. cur_bytes * 8);
  1946. bytes -= cur_bytes;
  1947. if (bytes) {
  1948. brelse(bh);
  1949. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  1950. bh = udf_tread(sb, newblock);
  1951. if (!bh) {
  1952. udf_debug("read failed\n");
  1953. goto out;
  1954. }
  1955. index = 0;
  1956. ptr = (uint8_t *)bh->b_data;
  1957. }
  1958. }
  1959. brelse(bh);
  1960. out:
  1961. return accum;
  1962. }
  1963. static unsigned int udf_count_free_table(struct super_block *sb,
  1964. struct inode *table)
  1965. {
  1966. unsigned int accum = 0;
  1967. uint32_t elen;
  1968. struct kernel_lb_addr eloc;
  1969. int8_t etype;
  1970. struct extent_position epos;
  1971. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  1972. epos.block = UDF_I(table)->i_location;
  1973. epos.offset = sizeof(struct unallocSpaceEntry);
  1974. epos.bh = NULL;
  1975. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  1976. accum += (elen >> table->i_sb->s_blocksize_bits);
  1977. brelse(epos.bh);
  1978. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  1979. return accum;
  1980. }
  1981. static unsigned int udf_count_free(struct super_block *sb)
  1982. {
  1983. unsigned int accum = 0;
  1984. struct udf_sb_info *sbi;
  1985. struct udf_part_map *map;
  1986. sbi = UDF_SB(sb);
  1987. if (sbi->s_lvid_bh) {
  1988. struct logicalVolIntegrityDesc *lvid =
  1989. (struct logicalVolIntegrityDesc *)
  1990. sbi->s_lvid_bh->b_data;
  1991. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  1992. accum = le32_to_cpu(
  1993. lvid->freeSpaceTable[sbi->s_partition]);
  1994. if (accum == 0xFFFFFFFF)
  1995. accum = 0;
  1996. }
  1997. }
  1998. if (accum)
  1999. return accum;
  2000. map = &sbi->s_partmaps[sbi->s_partition];
  2001. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2002. accum += udf_count_free_bitmap(sb,
  2003. map->s_uspace.s_bitmap);
  2004. }
  2005. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2006. accum += udf_count_free_bitmap(sb,
  2007. map->s_fspace.s_bitmap);
  2008. }
  2009. if (accum)
  2010. return accum;
  2011. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2012. accum += udf_count_free_table(sb,
  2013. map->s_uspace.s_table);
  2014. }
  2015. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2016. accum += udf_count_free_table(sb,
  2017. map->s_fspace.s_table);
  2018. }
  2019. return accum;
  2020. }