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